18dbcf02cSchristos /* 28dbcf02cSchristos * OS specific functions 38dbcf02cSchristos * Copyright (c) 2005-2009, Jouni Malinen <j@w1.fi> 48dbcf02cSchristos * 5316ee512Schristos * This software may be distributed under the terms of the BSD license. 6316ee512Schristos * See README for more details. 78dbcf02cSchristos */ 88dbcf02cSchristos 98dbcf02cSchristos #ifndef OS_H 108dbcf02cSchristos #define OS_H 118dbcf02cSchristos 12ccfa3bc9Schristos typedef time_t os_time_t; 138dbcf02cSchristos 148dbcf02cSchristos /** 158dbcf02cSchristos * os_sleep - Sleep (sec, usec) 168dbcf02cSchristos * @sec: Number of seconds to sleep 178dbcf02cSchristos * @usec: Number of microseconds to sleep 188dbcf02cSchristos */ 198dbcf02cSchristos void os_sleep(os_time_t sec, os_time_t usec); 208dbcf02cSchristos 218dbcf02cSchristos struct os_time { 228dbcf02cSchristos os_time_t sec; 238dbcf02cSchristos os_time_t usec; 248dbcf02cSchristos }; 258dbcf02cSchristos 263c5783d3Schristos struct os_reltime { 273c5783d3Schristos os_time_t sec; 283c5783d3Schristos os_time_t usec; 293c5783d3Schristos }; 303c5783d3Schristos 318dbcf02cSchristos /** 328dbcf02cSchristos * os_get_time - Get current time (sec, usec) 338dbcf02cSchristos * @t: Pointer to buffer for the time 348dbcf02cSchristos * Returns: 0 on success, -1 on failure 358dbcf02cSchristos */ 368dbcf02cSchristos int os_get_time(struct os_time *t); 378dbcf02cSchristos 383c5783d3Schristos /** 393c5783d3Schristos * os_get_reltime - Get relative time (sec, usec) 403c5783d3Schristos * @t: Pointer to buffer for the time 413c5783d3Schristos * Returns: 0 on success, -1 on failure 423c5783d3Schristos */ 433c5783d3Schristos int os_get_reltime(struct os_reltime *t); 448dbcf02cSchristos 458dbcf02cSchristos 463c5783d3Schristos /* Helpers for handling struct os_time */ 478dbcf02cSchristos 483c5783d3Schristos static inline int os_time_before(struct os_time *a, struct os_time *b) 493c5783d3Schristos { 503c5783d3Schristos return (a->sec < b->sec) || 513c5783d3Schristos (a->sec == b->sec && a->usec < b->usec); 523c5783d3Schristos } 533c5783d3Schristos 543c5783d3Schristos 553c5783d3Schristos static inline void os_time_sub(struct os_time *a, struct os_time *b, 563c5783d3Schristos struct os_time *res) 573c5783d3Schristos { 583c5783d3Schristos res->sec = a->sec - b->sec; 593c5783d3Schristos res->usec = a->usec - b->usec; 603c5783d3Schristos if (res->usec < 0) { 613c5783d3Schristos res->sec--; 623c5783d3Schristos res->usec += 1000000; 633c5783d3Schristos } 643c5783d3Schristos } 653c5783d3Schristos 663c5783d3Schristos 673c5783d3Schristos /* Helpers for handling struct os_reltime */ 683c5783d3Schristos 693c5783d3Schristos static inline int os_reltime_before(struct os_reltime *a, 703c5783d3Schristos struct os_reltime *b) 713c5783d3Schristos { 723c5783d3Schristos return (a->sec < b->sec) || 733c5783d3Schristos (a->sec == b->sec && a->usec < b->usec); 743c5783d3Schristos } 753c5783d3Schristos 763c5783d3Schristos 773c5783d3Schristos static inline void os_reltime_sub(struct os_reltime *a, struct os_reltime *b, 783c5783d3Schristos struct os_reltime *res) 793c5783d3Schristos { 803c5783d3Schristos res->sec = a->sec - b->sec; 813c5783d3Schristos res->usec = a->usec - b->usec; 823c5783d3Schristos if (res->usec < 0) { 833c5783d3Schristos res->sec--; 843c5783d3Schristos res->usec += 1000000; 853c5783d3Schristos } 863c5783d3Schristos } 873c5783d3Schristos 883c5783d3Schristos 893c5783d3Schristos static inline void os_reltime_age(struct os_reltime *start, 903c5783d3Schristos struct os_reltime *age) 913c5783d3Schristos { 923c5783d3Schristos struct os_reltime now; 933c5783d3Schristos 943c5783d3Schristos os_get_reltime(&now); 953c5783d3Schristos os_reltime_sub(&now, start, age); 963c5783d3Schristos } 973c5783d3Schristos 983c5783d3Schristos 993c5783d3Schristos static inline int os_reltime_expired(struct os_reltime *now, 1003c5783d3Schristos struct os_reltime *ts, 1013c5783d3Schristos os_time_t timeout_secs) 1023c5783d3Schristos { 1033c5783d3Schristos struct os_reltime age; 1043c5783d3Schristos 1053c5783d3Schristos os_reltime_sub(now, ts, &age); 1063c5783d3Schristos return (age.sec > timeout_secs) || 1073c5783d3Schristos (age.sec == timeout_secs && age.usec > 0); 1083c5783d3Schristos } 1093c5783d3Schristos 1103c5783d3Schristos 111*45d3cc13Schristos static inline void os_reltime_add_ms(struct os_reltime *ts, int ms) 112*45d3cc13Schristos { 113*45d3cc13Schristos ts->usec += ms * 1000; 114*45d3cc13Schristos while (ts->usec >= 1000000) { 115*45d3cc13Schristos ts->sec++; 116*45d3cc13Schristos ts->usec -= 1000000; 117*45d3cc13Schristos } 118*45d3cc13Schristos while (ts->usec < 0) { 119*45d3cc13Schristos ts->sec--; 120*45d3cc13Schristos ts->usec += 1000000; 121*45d3cc13Schristos } 122*45d3cc13Schristos } 123*45d3cc13Schristos 124*45d3cc13Schristos 125*45d3cc13Schristos static inline int os_reltime_in_ms(struct os_reltime *ts) 126*45d3cc13Schristos { 127*45d3cc13Schristos return ts->sec * 1000 + ts->usec / 1000; 128*45d3cc13Schristos } 129*45d3cc13Schristos 130*45d3cc13Schristos 1313c5783d3Schristos static inline int os_reltime_initialized(struct os_reltime *t) 1323c5783d3Schristos { 1333c5783d3Schristos return t->sec != 0 || t->usec != 0; 1343c5783d3Schristos } 1353c5783d3Schristos 1368dbcf02cSchristos 1378dbcf02cSchristos /** 1388dbcf02cSchristos * os_mktime - Convert broken-down time into seconds since 1970-01-01 1398dbcf02cSchristos * @year: Four digit year 1408dbcf02cSchristos * @month: Month (1 .. 12) 1418dbcf02cSchristos * @day: Day of month (1 .. 31) 1428dbcf02cSchristos * @hour: Hour (0 .. 23) 1438dbcf02cSchristos * @min: Minute (0 .. 59) 1448dbcf02cSchristos * @sec: Second (0 .. 60) 1458dbcf02cSchristos * @t: Buffer for returning calendar time representation (seconds since 1468dbcf02cSchristos * 1970-01-01 00:00:00) 1478dbcf02cSchristos * Returns: 0 on success, -1 on failure 1488dbcf02cSchristos * 1498dbcf02cSchristos * Note: The result is in seconds from Epoch, i.e., in UTC, not in local time 1508dbcf02cSchristos * which is used by POSIX mktime(). 1518dbcf02cSchristos */ 1528dbcf02cSchristos int os_mktime(int year, int month, int day, int hour, int min, int sec, 1538dbcf02cSchristos os_time_t *t); 1548dbcf02cSchristos 155b8fa3219Schristos struct os_tm { 156b8fa3219Schristos int sec; /* 0..59 or 60 for leap seconds */ 157b8fa3219Schristos int min; /* 0..59 */ 158b8fa3219Schristos int hour; /* 0..23 */ 159b8fa3219Schristos int day; /* 1..31 */ 160b8fa3219Schristos int month; /* 1..12 */ 161b8fa3219Schristos int year; /* Four digit year */ 162b8fa3219Schristos }; 163b8fa3219Schristos 164b8fa3219Schristos int os_gmtime(os_time_t t, struct os_tm *tm); 1658dbcf02cSchristos 1668dbcf02cSchristos /** 1678dbcf02cSchristos * os_daemonize - Run in the background (detach from the controlling terminal) 1688dbcf02cSchristos * @pid_file: File name to write the process ID to or %NULL to skip this 1698dbcf02cSchristos * Returns: 0 on success, -1 on failure 1708dbcf02cSchristos */ 1718dbcf02cSchristos int os_daemonize(const char *pid_file); 1728dbcf02cSchristos 1738dbcf02cSchristos /** 1748dbcf02cSchristos * os_daemonize_terminate - Stop running in the background (remove pid file) 1758dbcf02cSchristos * @pid_file: File name to write the process ID to or %NULL to skip this 1768dbcf02cSchristos */ 1778dbcf02cSchristos void os_daemonize_terminate(const char *pid_file); 1788dbcf02cSchristos 1798dbcf02cSchristos /** 1808dbcf02cSchristos * os_get_random - Get cryptographically strong pseudo random data 1818dbcf02cSchristos * @buf: Buffer for pseudo random data 1828dbcf02cSchristos * @len: Length of the buffer 1838dbcf02cSchristos * Returns: 0 on success, -1 on failure 1848dbcf02cSchristos */ 1858dbcf02cSchristos int os_get_random(unsigned char *buf, size_t len); 1868dbcf02cSchristos 1878dbcf02cSchristos /** 1888dbcf02cSchristos * os_random - Get pseudo random value (not necessarily very strong) 1898dbcf02cSchristos * Returns: Pseudo random value 1908dbcf02cSchristos */ 1918dbcf02cSchristos unsigned long os_random(void); 1928dbcf02cSchristos 1938dbcf02cSchristos /** 1948dbcf02cSchristos * os_rel2abs_path - Get an absolute path for a file 1958dbcf02cSchristos * @rel_path: Relative path to a file 1968dbcf02cSchristos * Returns: Absolute path for the file or %NULL on failure 1978dbcf02cSchristos * 1988dbcf02cSchristos * This function tries to convert a relative path of a file to an absolute path 1998dbcf02cSchristos * in order for the file to be found even if current working directory has 2008dbcf02cSchristos * changed. The returned value is allocated and caller is responsible for 2018dbcf02cSchristos * freeing it. It is acceptable to just return the same path in an allocated 2028dbcf02cSchristos * buffer, e.g., return strdup(rel_path). This function is only used to find 2038dbcf02cSchristos * configuration files when os_daemonize() may have changed the current working 2048dbcf02cSchristos * directory and relative path would be pointing to a different location. 2058dbcf02cSchristos */ 2068dbcf02cSchristos char * os_rel2abs_path(const char *rel_path); 2078dbcf02cSchristos 2088dbcf02cSchristos /** 2098dbcf02cSchristos * os_program_init - Program initialization (called at start) 2108dbcf02cSchristos * Returns: 0 on success, -1 on failure 2118dbcf02cSchristos * 2128dbcf02cSchristos * This function is called when a programs starts. If there are any OS specific 2138dbcf02cSchristos * processing that is needed, it can be placed here. It is also acceptable to 2148dbcf02cSchristos * just return 0 if not special processing is needed. 2158dbcf02cSchristos */ 2168dbcf02cSchristos int os_program_init(void); 2178dbcf02cSchristos 2188dbcf02cSchristos /** 2198dbcf02cSchristos * os_program_deinit - Program deinitialization (called just before exit) 2208dbcf02cSchristos * 2218dbcf02cSchristos * This function is called just before a program exists. If there are any OS 2228dbcf02cSchristos * specific processing, e.g., freeing resourced allocated in os_program_init(), 2238dbcf02cSchristos * it should be done here. It is also acceptable for this function to do 2248dbcf02cSchristos * nothing. 2258dbcf02cSchristos */ 2268dbcf02cSchristos void os_program_deinit(void); 2278dbcf02cSchristos 2288dbcf02cSchristos /** 2298dbcf02cSchristos * os_setenv - Set environment variable 2308dbcf02cSchristos * @name: Name of the variable 2318dbcf02cSchristos * @value: Value to set to the variable 2328dbcf02cSchristos * @overwrite: Whether existing variable should be overwritten 2338dbcf02cSchristos * Returns: 0 on success, -1 on error 2348dbcf02cSchristos * 2358dbcf02cSchristos * This function is only used for wpa_cli action scripts. OS wrapper does not 2368dbcf02cSchristos * need to implement this if such functionality is not needed. 2378dbcf02cSchristos */ 2388dbcf02cSchristos int os_setenv(const char *name, const char *value, int overwrite); 2398dbcf02cSchristos 2408dbcf02cSchristos /** 2418dbcf02cSchristos * os_unsetenv - Delete environent variable 2428dbcf02cSchristos * @name: Name of the variable 2438dbcf02cSchristos * Returns: 0 on success, -1 on error 2448dbcf02cSchristos * 2458dbcf02cSchristos * This function is only used for wpa_cli action scripts. OS wrapper does not 2468dbcf02cSchristos * need to implement this if such functionality is not needed. 2478dbcf02cSchristos */ 2488dbcf02cSchristos int os_unsetenv(const char *name); 2498dbcf02cSchristos 2508dbcf02cSchristos /** 2518dbcf02cSchristos * os_readfile - Read a file to an allocated memory buffer 2528dbcf02cSchristos * @name: Name of the file to read 2538dbcf02cSchristos * @len: For returning the length of the allocated buffer 2548dbcf02cSchristos * Returns: Pointer to the allocated buffer or %NULL on failure 2558dbcf02cSchristos * 2568dbcf02cSchristos * This function allocates memory and reads the given file to this buffer. Both 2578dbcf02cSchristos * binary and text files can be read with this function. The caller is 2588dbcf02cSchristos * responsible for freeing the returned buffer with os_free(). 2598dbcf02cSchristos */ 2608dbcf02cSchristos char * os_readfile(const char *name, size_t *len); 2618dbcf02cSchristos 2628dbcf02cSchristos /** 2633c5783d3Schristos * os_file_exists - Check whether the specified file exists 2643c5783d3Schristos * @fname: Path and name of the file 2653c5783d3Schristos * Returns: 1 if the file exists or 0 if not 2663c5783d3Schristos */ 2673c5783d3Schristos int os_file_exists(const char *fname); 2683c5783d3Schristos 2693c5783d3Schristos /** 270ecc36642Schristos * os_fdatasync - Sync a file's (for a given stream) state with storage device 271ecc36642Schristos * @stream: the stream to be flushed 272ecc36642Schristos * Returns: 0 if the operation succeeded or -1 on failure 273ecc36642Schristos */ 274ecc36642Schristos int os_fdatasync(FILE *stream); 275ecc36642Schristos 276ecc36642Schristos /** 2778dbcf02cSchristos * os_zalloc - Allocate and zero memory 2788dbcf02cSchristos * @size: Number of bytes to allocate 2798dbcf02cSchristos * Returns: Pointer to allocated and zeroed memory or %NULL on failure 2808dbcf02cSchristos * 2818dbcf02cSchristos * Caller is responsible for freeing the returned buffer with os_free(). 2828dbcf02cSchristos */ 2838dbcf02cSchristos void * os_zalloc(size_t size); 2848dbcf02cSchristos 285316ee512Schristos /** 286316ee512Schristos * os_calloc - Allocate and zero memory for an array 287316ee512Schristos * @nmemb: Number of members in the array 288316ee512Schristos * @size: Number of bytes in each member 289316ee512Schristos * Returns: Pointer to allocated and zeroed memory or %NULL on failure 290316ee512Schristos * 291316ee512Schristos * This function can be used as a wrapper for os_zalloc(nmemb * size) when an 292316ee512Schristos * allocation is used for an array. The main benefit over os_zalloc() is in 293316ee512Schristos * having an extra check to catch integer overflows in multiplication. 294316ee512Schristos * 295316ee512Schristos * Caller is responsible for freeing the returned buffer with os_free(). 296316ee512Schristos */ 297316ee512Schristos static inline void * os_calloc(size_t nmemb, size_t size) 298316ee512Schristos { 299316ee512Schristos if (size && nmemb > (~(size_t) 0) / size) 300316ee512Schristos return NULL; 301316ee512Schristos return os_zalloc(nmemb * size); 302316ee512Schristos } 303316ee512Schristos 3048dbcf02cSchristos 3058dbcf02cSchristos /* 3068dbcf02cSchristos * The following functions are wrapper for standard ANSI C or POSIX functions. 3078dbcf02cSchristos * By default, they are just defined to use the standard function name and no 3088dbcf02cSchristos * os_*.c implementation is needed for them. This avoids extra function calls 3098dbcf02cSchristos * by allowing the C pre-processor take care of the function name mapping. 3108dbcf02cSchristos * 3118dbcf02cSchristos * If the target system uses a C library that does not provide these functions, 3128dbcf02cSchristos * build_config.h can be used to define the wrappers to use a different 3138dbcf02cSchristos * function name. This can be done on function-by-function basis since the 3148dbcf02cSchristos * defines here are only used if build_config.h does not define the os_* name. 3158dbcf02cSchristos * If needed, os_*.c file can be used to implement the functions that are not 3168dbcf02cSchristos * included in the C library on the target system. Alternatively, 3178dbcf02cSchristos * OS_NO_C_LIB_DEFINES can be defined to skip all defines here in which case 3188dbcf02cSchristos * these functions need to be implemented in os_*.c file for the target system. 3198dbcf02cSchristos */ 3208dbcf02cSchristos 3218dbcf02cSchristos #ifdef OS_NO_C_LIB_DEFINES 3228dbcf02cSchristos 3238dbcf02cSchristos /** 3248dbcf02cSchristos * os_malloc - Allocate dynamic memory 3258dbcf02cSchristos * @size: Size of the buffer to allocate 3268dbcf02cSchristos * Returns: Allocated buffer or %NULL on failure 3278dbcf02cSchristos * 3288dbcf02cSchristos * Caller is responsible for freeing the returned buffer with os_free(). 3298dbcf02cSchristos */ 3308dbcf02cSchristos void * os_malloc(size_t size); 3318dbcf02cSchristos 3328dbcf02cSchristos /** 3338dbcf02cSchristos * os_realloc - Re-allocate dynamic memory 3348dbcf02cSchristos * @ptr: Old buffer from os_malloc() or os_realloc() 3358dbcf02cSchristos * @size: Size of the new buffer 3368dbcf02cSchristos * Returns: Allocated buffer or %NULL on failure 3378dbcf02cSchristos * 3388dbcf02cSchristos * Caller is responsible for freeing the returned buffer with os_free(). 3398dbcf02cSchristos * If re-allocation fails, %NULL is returned and the original buffer (ptr) is 3408dbcf02cSchristos * not freed and caller is still responsible for freeing it. 3418dbcf02cSchristos */ 3428dbcf02cSchristos void * os_realloc(void *ptr, size_t size); 3438dbcf02cSchristos 3448dbcf02cSchristos /** 3458dbcf02cSchristos * os_free - Free dynamic memory 3468dbcf02cSchristos * @ptr: Old buffer from os_malloc() or os_realloc(); can be %NULL 3478dbcf02cSchristos */ 3488dbcf02cSchristos void os_free(void *ptr); 3498dbcf02cSchristos 3508dbcf02cSchristos /** 3518dbcf02cSchristos * os_memcpy - Copy memory area 3528dbcf02cSchristos * @dest: Destination 3538dbcf02cSchristos * @src: Source 3548dbcf02cSchristos * @n: Number of bytes to copy 3558dbcf02cSchristos * Returns: dest 3568dbcf02cSchristos * 3578dbcf02cSchristos * The memory areas src and dst must not overlap. os_memmove() can be used with 3588dbcf02cSchristos * overlapping memory. 3598dbcf02cSchristos */ 3608dbcf02cSchristos void * os_memcpy(void *dest, const void *src, size_t n); 3618dbcf02cSchristos 3628dbcf02cSchristos /** 3638dbcf02cSchristos * os_memmove - Copy memory area 3648dbcf02cSchristos * @dest: Destination 3658dbcf02cSchristos * @src: Source 3668dbcf02cSchristos * @n: Number of bytes to copy 3678dbcf02cSchristos * Returns: dest 3688dbcf02cSchristos * 3698dbcf02cSchristos * The memory areas src and dst may overlap. 3708dbcf02cSchristos */ 3718dbcf02cSchristos void * os_memmove(void *dest, const void *src, size_t n); 3728dbcf02cSchristos 3738dbcf02cSchristos /** 3748dbcf02cSchristos * os_memset - Fill memory with a constant byte 3758dbcf02cSchristos * @s: Memory area to be filled 3768dbcf02cSchristos * @c: Constant byte 3778dbcf02cSchristos * @n: Number of bytes started from s to fill with c 3788dbcf02cSchristos * Returns: s 3798dbcf02cSchristos */ 3808dbcf02cSchristos void * os_memset(void *s, int c, size_t n); 3818dbcf02cSchristos 3828dbcf02cSchristos /** 3838dbcf02cSchristos * os_memcmp - Compare memory areas 3848dbcf02cSchristos * @s1: First buffer 3858dbcf02cSchristos * @s2: Second buffer 3868dbcf02cSchristos * @n: Maximum numbers of octets to compare 3878dbcf02cSchristos * Returns: An integer less than, equal to, or greater than zero if s1 is 3888dbcf02cSchristos * found to be less than, to match, or be greater than s2. Only first n 3898dbcf02cSchristos * characters will be compared. 3908dbcf02cSchristos */ 3918dbcf02cSchristos int os_memcmp(const void *s1, const void *s2, size_t n); 3928dbcf02cSchristos 3938dbcf02cSchristos /** 3948dbcf02cSchristos * os_strdup - Duplicate a string 3958dbcf02cSchristos * @s: Source string 3968dbcf02cSchristos * Returns: Allocated buffer with the string copied into it or %NULL on failure 3978dbcf02cSchristos * 3988dbcf02cSchristos * Caller is responsible for freeing the returned buffer with os_free(). 3998dbcf02cSchristos */ 4008dbcf02cSchristos char * os_strdup(const char *s); 4018dbcf02cSchristos 4028dbcf02cSchristos /** 4038dbcf02cSchristos * os_strlen - Calculate the length of a string 4048dbcf02cSchristos * @s: '\0' terminated string 4058dbcf02cSchristos * Returns: Number of characters in s (not counting the '\0' terminator) 4068dbcf02cSchristos */ 4078dbcf02cSchristos size_t os_strlen(const char *s); 4088dbcf02cSchristos 4098dbcf02cSchristos /** 4108dbcf02cSchristos * os_strcasecmp - Compare two strings ignoring case 4118dbcf02cSchristos * @s1: First string 4128dbcf02cSchristos * @s2: Second string 4138dbcf02cSchristos * Returns: An integer less than, equal to, or greater than zero if s1 is 4148dbcf02cSchristos * found to be less than, to match, or be greatred than s2 4158dbcf02cSchristos */ 4168dbcf02cSchristos int os_strcasecmp(const char *s1, const char *s2); 4178dbcf02cSchristos 4188dbcf02cSchristos /** 4198dbcf02cSchristos * os_strncasecmp - Compare two strings ignoring case 4208dbcf02cSchristos * @s1: First string 4218dbcf02cSchristos * @s2: Second string 4228dbcf02cSchristos * @n: Maximum numbers of characters to compare 4238dbcf02cSchristos * Returns: An integer less than, equal to, or greater than zero if s1 is 4248dbcf02cSchristos * found to be less than, to match, or be greater than s2. Only first n 4258dbcf02cSchristos * characters will be compared. 4268dbcf02cSchristos */ 4278dbcf02cSchristos int os_strncasecmp(const char *s1, const char *s2, size_t n); 4288dbcf02cSchristos 4298dbcf02cSchristos /** 4308dbcf02cSchristos * os_strchr - Locate the first occurrence of a character in string 4318dbcf02cSchristos * @s: String 4328dbcf02cSchristos * @c: Character to search for 4338dbcf02cSchristos * Returns: Pointer to the matched character or %NULL if not found 4348dbcf02cSchristos */ 4358dbcf02cSchristos char * os_strchr(const char *s, int c); 4368dbcf02cSchristos 4378dbcf02cSchristos /** 4388dbcf02cSchristos * os_strrchr - Locate the last occurrence of a character in string 4398dbcf02cSchristos * @s: String 4408dbcf02cSchristos * @c: Character to search for 4418dbcf02cSchristos * Returns: Pointer to the matched character or %NULL if not found 4428dbcf02cSchristos */ 4438dbcf02cSchristos char * os_strrchr(const char *s, int c); 4448dbcf02cSchristos 4458dbcf02cSchristos /** 4468dbcf02cSchristos * os_strcmp - Compare two strings 4478dbcf02cSchristos * @s1: First string 4488dbcf02cSchristos * @s2: Second string 4498dbcf02cSchristos * Returns: An integer less than, equal to, or greater than zero if s1 is 4508dbcf02cSchristos * found to be less than, to match, or be greatred than s2 4518dbcf02cSchristos */ 4528dbcf02cSchristos int os_strcmp(const char *s1, const char *s2); 4538dbcf02cSchristos 4548dbcf02cSchristos /** 4558dbcf02cSchristos * os_strncmp - Compare two strings 4568dbcf02cSchristos * @s1: First string 4578dbcf02cSchristos * @s2: Second string 4588dbcf02cSchristos * @n: Maximum numbers of characters to compare 4598dbcf02cSchristos * Returns: An integer less than, equal to, or greater than zero if s1 is 4608dbcf02cSchristos * found to be less than, to match, or be greater than s2. Only first n 4618dbcf02cSchristos * characters will be compared. 4628dbcf02cSchristos */ 4638dbcf02cSchristos int os_strncmp(const char *s1, const char *s2, size_t n); 4648dbcf02cSchristos 4658dbcf02cSchristos /** 4668dbcf02cSchristos * os_strstr - Locate a substring 4678dbcf02cSchristos * @haystack: String (haystack) to search from 4688dbcf02cSchristos * @needle: Needle to search from haystack 4698dbcf02cSchristos * Returns: Pointer to the beginning of the substring or %NULL if not found 4708dbcf02cSchristos */ 4718dbcf02cSchristos char * os_strstr(const char *haystack, const char *needle); 4728dbcf02cSchristos 4738dbcf02cSchristos /** 4748dbcf02cSchristos * os_snprintf - Print to a memory buffer 4758dbcf02cSchristos * @str: Memory buffer to print into 4768dbcf02cSchristos * @size: Maximum length of the str buffer 4778dbcf02cSchristos * @format: printf format 4788dbcf02cSchristos * Returns: Number of characters printed (not including trailing '\0'). 4798dbcf02cSchristos * 4808dbcf02cSchristos * If the output buffer is truncated, number of characters which would have 4818dbcf02cSchristos * been written is returned. Since some C libraries return -1 in such a case, 4828dbcf02cSchristos * the caller must be prepared on that value, too, to indicate truncation. 4838dbcf02cSchristos * 4848dbcf02cSchristos * Note: Some C library implementations of snprintf() may not guarantee null 4858dbcf02cSchristos * termination in case the output is truncated. The OS wrapper function of 4868dbcf02cSchristos * os_snprintf() should provide this guarantee, i.e., to null terminate the 4878dbcf02cSchristos * output buffer if a C library version of the function is used and if that 4888dbcf02cSchristos * function does not guarantee null termination. 4898dbcf02cSchristos * 4908dbcf02cSchristos * If the target system does not include snprintf(), see, e.g., 4918dbcf02cSchristos * http://www.ijs.si/software/snprintf/ for an example of a portable 4928dbcf02cSchristos * implementation of snprintf. 4938dbcf02cSchristos */ 4948dbcf02cSchristos int os_snprintf(char *str, size_t size, const char *format, ...); 4958dbcf02cSchristos 4968dbcf02cSchristos #else /* OS_NO_C_LIB_DEFINES */ 4978dbcf02cSchristos 4988dbcf02cSchristos #ifdef WPA_TRACE 4998dbcf02cSchristos void * os_malloc(size_t size); 5008dbcf02cSchristos void * os_realloc(void *ptr, size_t size); 5018dbcf02cSchristos void os_free(void *ptr); 5028dbcf02cSchristos char * os_strdup(const char *s); 5038dbcf02cSchristos #else /* WPA_TRACE */ 5048dbcf02cSchristos #ifndef os_malloc 5058dbcf02cSchristos #define os_malloc(s) malloc((s)) 5068dbcf02cSchristos #endif 5078dbcf02cSchristos #ifndef os_realloc 5088dbcf02cSchristos #define os_realloc(p, s) realloc((p), (s)) 5098dbcf02cSchristos #endif 5108dbcf02cSchristos #ifndef os_free 5118dbcf02cSchristos #define os_free(p) free((p)) 5128dbcf02cSchristos #endif 5138dbcf02cSchristos #ifndef os_strdup 5148dbcf02cSchristos #ifdef _MSC_VER 5158dbcf02cSchristos #define os_strdup(s) _strdup(s) 5168dbcf02cSchristos #else 5178dbcf02cSchristos #define os_strdup(s) strdup(s) 5188dbcf02cSchristos #endif 5198dbcf02cSchristos #endif 5208dbcf02cSchristos #endif /* WPA_TRACE */ 5218dbcf02cSchristos 5228dbcf02cSchristos #ifndef os_memcpy 5238dbcf02cSchristos #define os_memcpy(d, s, n) memcpy((d), (s), (n)) 5248dbcf02cSchristos #endif 5258dbcf02cSchristos #ifndef os_memmove 5268dbcf02cSchristos #define os_memmove(d, s, n) memmove((d), (s), (n)) 5278dbcf02cSchristos #endif 5288dbcf02cSchristos #ifndef os_memset 5298dbcf02cSchristos #define os_memset(s, c, n) memset(s, c, n) 5308dbcf02cSchristos #endif 5318dbcf02cSchristos #ifndef os_memcmp 5328dbcf02cSchristos #define os_memcmp(s1, s2, n) memcmp((s1), (s2), (n)) 5338dbcf02cSchristos #endif 5348dbcf02cSchristos 5358dbcf02cSchristos #ifndef os_strlen 5368dbcf02cSchristos #define os_strlen(s) strlen(s) 5378dbcf02cSchristos #endif 5388dbcf02cSchristos #ifndef os_strcasecmp 5398dbcf02cSchristos #ifdef _MSC_VER 5408dbcf02cSchristos #define os_strcasecmp(s1, s2) _stricmp((s1), (s2)) 5418dbcf02cSchristos #else 5428dbcf02cSchristos #define os_strcasecmp(s1, s2) strcasecmp((s1), (s2)) 5438dbcf02cSchristos #endif 5448dbcf02cSchristos #endif 5458dbcf02cSchristos #ifndef os_strncasecmp 5468dbcf02cSchristos #ifdef _MSC_VER 5478dbcf02cSchristos #define os_strncasecmp(s1, s2, n) _strnicmp((s1), (s2), (n)) 5488dbcf02cSchristos #else 5498dbcf02cSchristos #define os_strncasecmp(s1, s2, n) strncasecmp((s1), (s2), (n)) 5508dbcf02cSchristos #endif 5518dbcf02cSchristos #endif 5528dbcf02cSchristos #ifndef os_strchr 5538dbcf02cSchristos #define os_strchr(s, c) strchr((s), (c)) 5548dbcf02cSchristos #endif 5558dbcf02cSchristos #ifndef os_strcmp 5568dbcf02cSchristos #define os_strcmp(s1, s2) strcmp((s1), (s2)) 5578dbcf02cSchristos #endif 5588dbcf02cSchristos #ifndef os_strncmp 5598dbcf02cSchristos #define os_strncmp(s1, s2, n) strncmp((s1), (s2), (n)) 5608dbcf02cSchristos #endif 5618dbcf02cSchristos #ifndef os_strrchr 5628dbcf02cSchristos #define os_strrchr(s, c) strrchr((s), (c)) 5638dbcf02cSchristos #endif 5648dbcf02cSchristos #ifndef os_strstr 5658dbcf02cSchristos #define os_strstr(h, n) strstr((h), (n)) 5668dbcf02cSchristos #endif 5678dbcf02cSchristos 5688dbcf02cSchristos #ifndef os_snprintf 5698dbcf02cSchristos #ifdef _MSC_VER 5708dbcf02cSchristos #define os_snprintf _snprintf 5718dbcf02cSchristos #else 5728dbcf02cSchristos #define os_snprintf snprintf 5738dbcf02cSchristos #endif 5748dbcf02cSchristos #endif 5758dbcf02cSchristos 5768dbcf02cSchristos #endif /* OS_NO_C_LIB_DEFINES */ 5778dbcf02cSchristos 5788dbcf02cSchristos 579299bbf24Schristos static inline int os_snprintf_error(size_t size, int res) 580299bbf24Schristos { 581299bbf24Schristos return res < 0 || (unsigned int) res >= size; 582299bbf24Schristos } 583299bbf24Schristos 584299bbf24Schristos 585316ee512Schristos static inline void * os_realloc_array(void *ptr, size_t nmemb, size_t size) 586316ee512Schristos { 587316ee512Schristos if (size && nmemb > (~(size_t) 0) / size) 588316ee512Schristos return NULL; 589316ee512Schristos return os_realloc(ptr, nmemb * size); 590316ee512Schristos } 591316ee512Schristos 5923c5783d3Schristos /** 5933c5783d3Schristos * os_remove_in_array - Remove a member from an array by index 5943c5783d3Schristos * @ptr: Pointer to the array 5953c5783d3Schristos * @nmemb: Current member count of the array 5963c5783d3Schristos * @size: The size per member of the array 5973c5783d3Schristos * @idx: Index of the member to be removed 5983c5783d3Schristos */ 5993c5783d3Schristos static inline void os_remove_in_array(void *ptr, size_t nmemb, size_t size, 6003c5783d3Schristos size_t idx) 6013c5783d3Schristos { 6023c5783d3Schristos if (idx < nmemb - 1) 6033c5783d3Schristos os_memmove(((unsigned char *) ptr) + idx * size, 6043c5783d3Schristos ((unsigned char *) ptr) + (idx + 1) * size, 6053c5783d3Schristos (nmemb - idx - 1) * size); 6063c5783d3Schristos } 607316ee512Schristos 6088dbcf02cSchristos /** 6098dbcf02cSchristos * os_strlcpy - Copy a string with size bound and NUL-termination 6108dbcf02cSchristos * @dest: Destination 6118dbcf02cSchristos * @src: Source 6128dbcf02cSchristos * @siz: Size of the target buffer 6138dbcf02cSchristos * Returns: Total length of the target string (length of src) (not including 6148dbcf02cSchristos * NUL-termination) 6158dbcf02cSchristos * 6168dbcf02cSchristos * This function matches in behavior with the strlcpy(3) function in OpenBSD. 6178dbcf02cSchristos */ 6188dbcf02cSchristos size_t os_strlcpy(char *dest, const char *src, size_t siz); 6198dbcf02cSchristos 6203c5783d3Schristos /** 6213c5783d3Schristos * os_memcmp_const - Constant time memory comparison 6223c5783d3Schristos * @a: First buffer to compare 6233c5783d3Schristos * @b: Second buffer to compare 6243c5783d3Schristos * @len: Number of octets to compare 6253c5783d3Schristos * Returns: 0 if buffers are equal, non-zero if not 6263c5783d3Schristos * 6273c5783d3Schristos * This function is meant for comparing passwords or hash values where 6283c5783d3Schristos * difference in execution time could provide external observer information 6293c5783d3Schristos * about the location of the difference in the memory buffers. The return value 6303c5783d3Schristos * does not behave like os_memcmp(), i.e., os_memcmp_const() cannot be used to 6313c5783d3Schristos * sort items into a defined order. Unlike os_memcmp(), execution time of 6323c5783d3Schristos * os_memcmp_const() does not depend on the contents of the compared memory 6333c5783d3Schristos * buffers, but only on the total compared length. 6343c5783d3Schristos */ 6353c5783d3Schristos int os_memcmp_const(const void *a, const void *b, size_t len); 6363c5783d3Schristos 637be6b3c4dSchristos 638be6b3c4dSchristos /** 639be6b3c4dSchristos * os_memdup - Allocate duplicate of passed memory chunk 640be6b3c4dSchristos * @src: Source buffer to duplicate 641be6b3c4dSchristos * @len: Length of source buffer 642be6b3c4dSchristos * Returns: %NULL if allocation failed, copy of src buffer otherwise 643be6b3c4dSchristos * 644be6b3c4dSchristos * This function allocates a memory block like os_malloc() would, and 645be6b3c4dSchristos * copies the given source buffer into it. 646be6b3c4dSchristos */ 647be6b3c4dSchristos void * os_memdup(const void *src, size_t len); 648be6b3c4dSchristos 6493c5783d3Schristos /** 6503c5783d3Schristos * os_exec - Execute an external program 6513c5783d3Schristos * @program: Path to the program 6523c5783d3Schristos * @arg: Command line argument string 6533c5783d3Schristos * @wait_completion: Whether to wait until the program execution completes 6543c5783d3Schristos * Returns: 0 on success, -1 on error 6553c5783d3Schristos */ 6563c5783d3Schristos int os_exec(const char *program, const char *arg, int wait_completion); 6573c5783d3Schristos 6588dbcf02cSchristos 6598dbcf02cSchristos #ifdef OS_REJECT_C_LIB_FUNCTIONS 6608dbcf02cSchristos #define malloc OS_DO_NOT_USE_malloc 6618dbcf02cSchristos #define realloc OS_DO_NOT_USE_realloc 6628dbcf02cSchristos #define free OS_DO_NOT_USE_free 6638dbcf02cSchristos #define memcpy OS_DO_NOT_USE_memcpy 6648dbcf02cSchristos #define memmove OS_DO_NOT_USE_memmove 6658dbcf02cSchristos #define memset OS_DO_NOT_USE_memset 6668dbcf02cSchristos #define memcmp OS_DO_NOT_USE_memcmp 6678dbcf02cSchristos #undef strdup 6688dbcf02cSchristos #define strdup OS_DO_NOT_USE_strdup 6698dbcf02cSchristos #define strlen OS_DO_NOT_USE_strlen 6708dbcf02cSchristos #define strcasecmp OS_DO_NOT_USE_strcasecmp 6718dbcf02cSchristos #define strncasecmp OS_DO_NOT_USE_strncasecmp 6728dbcf02cSchristos #undef strchr 6738dbcf02cSchristos #define strchr OS_DO_NOT_USE_strchr 6748dbcf02cSchristos #undef strcmp 6758dbcf02cSchristos #define strcmp OS_DO_NOT_USE_strcmp 6768dbcf02cSchristos #undef strncmp 6778dbcf02cSchristos #define strncmp OS_DO_NOT_USE_strncmp 6788dbcf02cSchristos #undef strncpy 6798dbcf02cSchristos #define strncpy OS_DO_NOT_USE_strncpy 6808dbcf02cSchristos #define strrchr OS_DO_NOT_USE_strrchr 6818dbcf02cSchristos #define strstr OS_DO_NOT_USE_strstr 6828dbcf02cSchristos #undef snprintf 6838dbcf02cSchristos #define snprintf OS_DO_NOT_USE_snprintf 6848dbcf02cSchristos 6858dbcf02cSchristos #define strcpy OS_DO_NOT_USE_strcpy 6868dbcf02cSchristos #endif /* OS_REJECT_C_LIB_FUNCTIONS */ 6878dbcf02cSchristos 688ecc36642Schristos 689ecc36642Schristos #if defined(WPA_TRACE_BFD) && defined(CONFIG_TESTING_OPTIONS) 690*45d3cc13Schristos #define TEST_FAIL() testing_test_fail(NULL, false) 691*45d3cc13Schristos #define TEST_FAIL_TAG(tag) testing_test_fail(tag, false) 692*45d3cc13Schristos int testing_test_fail(const char *tag, bool is_alloc); 693*45d3cc13Schristos int testing_set_fail_pattern(bool is_alloc, char *patterns); 694*45d3cc13Schristos int testing_get_fail_pattern(bool is_alloc, char *buf, size_t buflen); 695ecc36642Schristos #else 696ecc36642Schristos #define TEST_FAIL() 0 697*45d3cc13Schristos #define TEST_FAIL_TAG(tag) 0 698*45d3cc13Schristos static inline int testing_set_fail_pattern(bool is_alloc, char *patterns) 699*45d3cc13Schristos { 700*45d3cc13Schristos return -1; 701*45d3cc13Schristos } 702*45d3cc13Schristos 703*45d3cc13Schristos static inline int testing_get_fail_pattern(bool is_alloc, char *buf, 704*45d3cc13Schristos size_t buflen) 705*45d3cc13Schristos { 706*45d3cc13Schristos return -1; 707*45d3cc13Schristos } 708ecc36642Schristos #endif 709ecc36642Schristos 7108dbcf02cSchristos #endif /* OS_H */ 711