1 /* 2 * wpa_supplicant/hostapd / OS specific functions 3 * Copyright (c) 2005-2006, Jouni Malinen <j@w1.fi> 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License version 2 as 7 * published by the Free Software Foundation. 8 * 9 * Alternatively, this software may be distributed under the terms of BSD 10 * license. 11 * 12 * See README and COPYING for more details. 13 */ 14 15 #ifndef OS_H 16 #define OS_H 17 18 typedef long os_time_t; 19 20 /** 21 * os_sleep - Sleep (sec, usec) 22 * @sec: Number of seconds to sleep 23 * @usec: Number of microseconds to sleep 24 */ 25 void os_sleep(os_time_t sec, os_time_t usec); 26 27 struct os_time { 28 os_time_t sec; 29 os_time_t usec; 30 }; 31 32 /** 33 * os_get_time - Get current time (sec, usec) 34 * @t: Pointer to buffer for the time 35 * Returns: 0 on success, -1 on failure 36 */ 37 int os_get_time(struct os_time *t); 38 39 40 /* Helper macros for handling struct os_time */ 41 42 #define os_time_before(a, b) \ 43 ((a)->sec < (b)->sec || \ 44 ((a)->sec == (b)->sec && (a)->usec < (b)->usec)) 45 46 #define os_time_sub(a, b, res) do { \ 47 (res)->sec = (a)->sec - (b)->sec; \ 48 (res)->usec = (a)->usec - (b)->usec; \ 49 if ((res)->usec < 0) { \ 50 (res)->sec--; \ 51 (res)->usec += 1000000; \ 52 } \ 53 } while (0) 54 55 /** 56 * os_mktime - Convert broken-down time into seconds since 1970-01-01 57 * @year: Four digit year 58 * @month: Month (1 .. 12) 59 * @day: Day of month (1 .. 31) 60 * @hour: Hour (0 .. 23) 61 * @min: Minute (0 .. 59) 62 * @sec: Second (0 .. 60) 63 * @t: Buffer for returning calendar time representation (seconds since 64 * 1970-01-01 00:00:00) 65 * Returns: 0 on success, -1 on failure 66 * 67 * Note: The result is in seconds from Epoch, i.e., in UTC, not in local time 68 * which is used by POSIX mktime(). 69 */ 70 int os_mktime(int year, int month, int day, int hour, int min, int sec, 71 os_time_t *t); 72 73 74 /** 75 * os_daemonize - Run in the background (detach from the controlling terminal) 76 * @pid_file: File name to write the process ID to or %NULL to skip this 77 * Returns: 0 on success, -1 on failure 78 */ 79 int os_daemonize(const char *pid_file); 80 81 /** 82 * os_daemonize_terminate - Stop running in the background (remove pid file) 83 * @pid_file: File name to write the process ID to or %NULL to skip this 84 */ 85 void os_daemonize_terminate(const char *pid_file); 86 87 /** 88 * os_get_random - Get cryptographically strong pseudo random data 89 * @buf: Buffer for pseudo random data 90 * @len: Length of the buffer 91 * Returns: 0 on success, -1 on failure 92 */ 93 int os_get_random(unsigned char *buf, size_t len); 94 95 /** 96 * os_random - Get pseudo random value (not necessarily very strong) 97 * Returns: Pseudo random value 98 */ 99 unsigned long os_random(void); 100 101 /** 102 * os_rel2abs_path - Get an absolute path for a file 103 * @rel_path: Relative path to a file 104 * Returns: Absolute path for the file or %NULL on failure 105 * 106 * This function tries to convert a relative path of a file to an absolute path 107 * in order for the file to be found even if current working directory has 108 * changed. The returned value is allocated and caller is responsible for 109 * freeing it. It is acceptable to just return the same path in an allocated 110 * buffer, e.g., return strdup(rel_path). This function is only used to find 111 * configuration files when os_daemonize() may have changed the current working 112 * directory and relative path would be pointing to a different location. 113 */ 114 char * os_rel2abs_path(const char *rel_path); 115 116 /** 117 * os_program_init - Program initialization (called at start) 118 * Returns: 0 on success, -1 on failure 119 * 120 * This function is called when a programs starts. If there are any OS specific 121 * processing that is needed, it can be placed here. It is also acceptable to 122 * just return 0 if not special processing is needed. 123 */ 124 int os_program_init(void); 125 126 /** 127 * os_program_deinit - Program deinitialization (called just before exit) 128 * 129 * This function is called just before a program exists. If there are any OS 130 * specific processing, e.g., freeing resourced allocated in os_program_init(), 131 * it should be done here. It is also acceptable for this function to do 132 * nothing. 133 */ 134 void os_program_deinit(void); 135 136 /** 137 * os_setenv - Set environment variable 138 * @name: Name of the variable 139 * @value: Value to set to the variable 140 * @overwrite: Whether existing variable should be overwritten 141 * Returns: 0 on success, -1 on error 142 * 143 * This function is only used for wpa_cli action scripts. OS wrapper does not 144 * need to implement this if such functionality is not needed. 145 */ 146 int os_setenv(const char *name, const char *value, int overwrite); 147 148 /** 149 * os_unsetenv - Delete environent variable 150 * @name: Name of the variable 151 * Returns: 0 on success, -1 on error 152 * 153 * This function is only used for wpa_cli action scripts. OS wrapper does not 154 * need to implement this if such functionality is not needed. 155 */ 156 int os_unsetenv(const char *name); 157 158 /** 159 * os_readfile - Read a file to an allocated memory buffer 160 * @name: Name of the file to read 161 * @len: For returning the length of the allocated buffer 162 * Returns: Pointer to the allocated buffer or %NULL on failure 163 * 164 * This function allocates memory and reads the given file to this buffer. Both 165 * binary and text files can be read with this function. The caller is 166 * responsible for freeing the returned buffer with os_free(). 167 */ 168 char * os_readfile(const char *name, size_t *len); 169 170 /** 171 * os_zalloc - Allocate and zero memory 172 * @size: Number of bytes to allocate 173 * Returns: Pointer to allocated and zeroed memory or %NULL on failure 174 * 175 * Caller is responsible for freeing the returned buffer with os_free(). 176 */ 177 void * os_zalloc(size_t size); 178 179 180 /* 181 * The following functions are wrapper for standard ANSI C or POSIX functions. 182 * By default, they are just defined to use the standard function name and no 183 * os_*.c implementation is needed for them. This avoids extra function calls 184 * by allowing the C pre-processor take care of the function name mapping. 185 * 186 * If the target system uses a C library that does not provide these functions, 187 * build_config.h can be used to define the wrappers to use a different 188 * function name. This can be done on function-by-function basis since the 189 * defines here are only used if build_config.h does not define the os_* name. 190 * If needed, os_*.c file can be used to implement the functions that are not 191 * included in the C library on the target system. Alternatively, 192 * OS_NO_C_LIB_DEFINES can be defined to skip all defines here in which case 193 * these functions need to be implemented in os_*.c file for the target system. 194 */ 195 196 #ifdef OS_NO_C_LIB_DEFINES 197 198 /** 199 * os_malloc - Allocate dynamic memory 200 * @size: Size of the buffer to allocate 201 * Returns: Allocated buffer or %NULL on failure 202 * 203 * Caller is responsible for freeing the returned buffer with os_free(). 204 */ 205 void * os_malloc(size_t size); 206 207 /** 208 * os_realloc - Re-allocate dynamic memory 209 * @ptr: Old buffer from os_malloc() or os_realloc() 210 * @size: Size of the new buffer 211 * Returns: Allocated buffer or %NULL on failure 212 * 213 * Caller is responsible for freeing the returned buffer with os_free(). 214 * If re-allocation fails, %NULL is returned and the original buffer (ptr) is 215 * not freed and caller is still responsible for freeing it. 216 */ 217 void * os_realloc(void *ptr, size_t size); 218 219 /** 220 * os_free - Free dynamic memory 221 * @ptr: Old buffer from os_malloc() or os_realloc(); can be %NULL 222 */ 223 void os_free(void *ptr); 224 225 /** 226 * os_memcpy - Copy memory area 227 * @dest: Destination 228 * @src: Source 229 * @n: Number of bytes to copy 230 * Returns: dest 231 * 232 * The memory areas src and dst must not overlap. os_memmove() can be used with 233 * overlapping memory. 234 */ 235 void * os_memcpy(void *dest, const void *src, size_t n); 236 237 /** 238 * os_memmove - Copy memory area 239 * @dest: Destination 240 * @src: Source 241 * @n: Number of bytes to copy 242 * Returns: dest 243 * 244 * The memory areas src and dst may overlap. 245 */ 246 void * os_memmove(void *dest, const void *src, size_t n); 247 248 /** 249 * os_memset - Fill memory with a constant byte 250 * @s: Memory area to be filled 251 * @c: Constant byte 252 * @n: Number of bytes started from s to fill with c 253 * Returns: s 254 */ 255 void * os_memset(void *s, int c, size_t n); 256 257 /** 258 * os_memcmp - Compare memory areas 259 * @s1: First buffer 260 * @s2: Second buffer 261 * @n: Maximum numbers of octets to compare 262 * Returns: An integer less than, equal to, or greater than zero if s1 is 263 * found to be less than, to match, or be greater than s2. Only first n 264 * characters will be compared. 265 */ 266 int os_memcmp(const void *s1, const void *s2, size_t n); 267 268 /** 269 * os_strdup - Duplicate a string 270 * @s: Source string 271 * Returns: Allocated buffer with the string copied into it or %NULL on failure 272 * 273 * Caller is responsible for freeing the returned buffer with os_free(). 274 */ 275 char * os_strdup(const char *s); 276 277 /** 278 * os_strlen - Calculate the length of a string 279 * @s: '\0' terminated string 280 * Returns: Number of characters in s (not counting the '\0' terminator) 281 */ 282 size_t os_strlen(const char *s); 283 284 /** 285 * os_strcasecmp - Compare two strings ignoring case 286 * @s1: First string 287 * @s2: Second string 288 * Returns: An integer less than, equal to, or greater than zero if s1 is 289 * found to be less than, to match, or be greatred than s2 290 */ 291 int os_strcasecmp(const char *s1, const char *s2); 292 293 /** 294 * os_strncasecmp - Compare two strings ignoring case 295 * @s1: First string 296 * @s2: Second string 297 * @n: Maximum numbers of characters to compare 298 * Returns: An integer less than, equal to, or greater than zero if s1 is 299 * found to be less than, to match, or be greater than s2. Only first n 300 * characters will be compared. 301 */ 302 int os_strncasecmp(const char *s1, const char *s2, size_t n); 303 304 /** 305 * os_strchr - Locate the first occurrence of a character in string 306 * @s: String 307 * @c: Character to search for 308 * Returns: Pointer to the matched character or %NULL if not found 309 */ 310 char * os_strchr(const char *s, int c); 311 312 /** 313 * os_strrchr - Locate the last occurrence of a character in string 314 * @s: String 315 * @c: Character to search for 316 * Returns: Pointer to the matched character or %NULL if not found 317 */ 318 char * os_strrchr(const char *s, int c); 319 320 /** 321 * os_strcmp - Compare two strings 322 * @s1: First string 323 * @s2: Second string 324 * Returns: An integer less than, equal to, or greater than zero if s1 is 325 * found to be less than, to match, or be greatred than s2 326 */ 327 int os_strcmp(const char *s1, const char *s2); 328 329 /** 330 * os_strncmp - Compare two strings 331 * @s1: First string 332 * @s2: Second string 333 * @n: Maximum numbers of characters to compare 334 * Returns: An integer less than, equal to, or greater than zero if s1 is 335 * found to be less than, to match, or be greater than s2. Only first n 336 * characters will be compared. 337 */ 338 int os_strncmp(const char *s1, const char *s2, size_t n); 339 340 /** 341 * os_strncpy - Copy a string 342 * @dest: Destination 343 * @src: Source 344 * @n: Maximum number of characters to copy 345 * Returns: dest 346 */ 347 char * os_strncpy(char *dest, const char *src, size_t n); 348 349 /** 350 * os_strstr - Locate a substring 351 * @haystack: String (haystack) to search from 352 * @needle: Needle to search from haystack 353 * Returns: Pointer to the beginning of the substring or %NULL if not found 354 */ 355 char * os_strstr(const char *haystack, const char *needle); 356 357 /** 358 * os_snprintf - Print to a memory buffer 359 * @str: Memory buffer to print into 360 * @size: Maximum length of the str buffer 361 * @format: printf format 362 * Returns: Number of characters printed (not including trailing '\0'). 363 * 364 * If the output buffer is truncated, number of characters which would have 365 * been written is returned. Since some C libraries return -1 in such a case, 366 * the caller must be prepared on that value, too, to indicate truncation. 367 * 368 * Note: Some C library implementations of snprintf() may not guarantee null 369 * termination in case the output is truncated. The OS wrapper function of 370 * os_snprintf() should provide this guarantee, i.e., to null terminate the 371 * output buffer if a C library version of the function is used and if that 372 * function does not guarantee null termination. 373 * 374 * If the target system does not include snprintf(), see, e.g., 375 * http://www.ijs.si/software/snprintf/ for an example of a portable 376 * implementation of snprintf. 377 */ 378 int os_snprintf(char *str, size_t size, const char *format, ...); 379 380 #else /* OS_NO_C_LIB_DEFINES */ 381 382 #ifndef os_malloc 383 #define os_malloc(s) malloc((s)) 384 #endif 385 #ifndef os_realloc 386 #define os_realloc(p, s) realloc((p), (s)) 387 #endif 388 #ifndef os_free 389 #define os_free(p) free((p)) 390 #endif 391 392 #ifndef os_memcpy 393 #define os_memcpy(d, s, n) memcpy((d), (s), (n)) 394 #endif 395 #ifndef os_memmove 396 #define os_memmove(d, s, n) memmove((d), (s), (n)) 397 #endif 398 #ifndef os_memset 399 #define os_memset(s, c, n) memset(s, c, n) 400 #endif 401 #ifndef os_memcmp 402 #define os_memcmp(s1, s2, n) memcmp((s1), (s2), (n)) 403 #endif 404 405 #ifndef os_strdup 406 #ifdef _MSC_VER 407 #define os_strdup(s) _strdup(s) 408 #else 409 #define os_strdup(s) strdup(s) 410 #endif 411 #endif 412 #ifndef os_strlen 413 #define os_strlen(s) strlen(s) 414 #endif 415 #ifndef os_strcasecmp 416 #ifdef _MSC_VER 417 #define os_strcasecmp(s1, s2) _stricmp((s1), (s2)) 418 #else 419 #define os_strcasecmp(s1, s2) strcasecmp((s1), (s2)) 420 #endif 421 #endif 422 #ifndef os_strncasecmp 423 #ifdef _MSC_VER 424 #define os_strncasecmp(s1, s2, n) _strnicmp((s1), (s2), (n)) 425 #else 426 #define os_strncasecmp(s1, s2, n) strncasecmp((s1), (s2), (n)) 427 #endif 428 #endif 429 #ifndef os_strchr 430 #define os_strchr(s, c) strchr((s), (c)) 431 #endif 432 #ifndef os_strcmp 433 #define os_strcmp(s1, s2) strcmp((s1), (s2)) 434 #endif 435 #ifndef os_strncmp 436 #define os_strncmp(s1, s2, n) strncmp((s1), (s2), (n)) 437 #endif 438 #ifndef os_strncpy 439 #define os_strncpy(d, s, n) strncpy((d), (s), (n)) 440 #endif 441 #ifndef os_strrchr 442 #define os_strrchr(s, c) strrchr((s), (c)) 443 #endif 444 #ifndef os_strstr 445 #define os_strstr(h, n) strstr((h), (n)) 446 #endif 447 448 #ifndef os_snprintf 449 #ifdef _MSC_VER 450 #define os_snprintf _snprintf 451 #else 452 #define os_snprintf snprintf 453 #endif 454 #endif 455 456 #endif /* OS_NO_C_LIB_DEFINES */ 457 458 459 /** 460 * os_strlcpy - Copy a string with size bound and NUL-termination 461 * @dest: Destination 462 * @src: Source 463 * @siz: Size of the target buffer 464 * Returns: Total length of the target string (length of src) (not including 465 * NUL-termination) 466 * 467 * This function matches in behavior with the strlcpy(3) function in OpenBSD. 468 */ 469 size_t os_strlcpy(char *dest, const char *src, size_t siz); 470 471 472 #ifdef OS_REJECT_C_LIB_FUNCTIONS 473 #define malloc OS_DO_NOT_USE_malloc 474 #define realloc OS_DO_NOT_USE_realloc 475 #define free OS_DO_NOT_USE_free 476 #define memcpy OS_DO_NOT_USE_memcpy 477 #define memmove OS_DO_NOT_USE_memmove 478 #define memset OS_DO_NOT_USE_memset 479 #define memcmp OS_DO_NOT_USE_memcmp 480 #undef strdup 481 #define strdup OS_DO_NOT_USE_strdup 482 #define strlen OS_DO_NOT_USE_strlen 483 #define strcasecmp OS_DO_NOT_USE_strcasecmp 484 #define strncasecmp OS_DO_NOT_USE_strncasecmp 485 #undef strchr 486 #define strchr OS_DO_NOT_USE_strchr 487 #undef strcmp 488 #define strcmp OS_DO_NOT_USE_strcmp 489 #undef strncmp 490 #define strncmp OS_DO_NOT_USE_strncmp 491 #undef strncpy 492 #define strncpy OS_DO_NOT_USE_strncpy 493 #define strrchr OS_DO_NOT_USE_strrchr 494 #define strstr OS_DO_NOT_USE_strstr 495 #undef snprintf 496 #define snprintf OS_DO_NOT_USE_snprintf 497 498 #define strcpy OS_DO_NOT_USE_strcpy 499 #endif /* OS_REJECT_C_LIB_FUNCTIONS */ 500 501 #endif /* OS_H */ 502