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