os.h revision 289549
155714Skris/* 255714Skris * OS specific functions 355714Skris * Copyright (c) 2005-2009, Jouni Malinen <j@w1.fi> 455714Skris * 555714Skris * This software may be distributed under the terms of the BSD license. 655714Skris * See README for more details. 755714Skris */ 855714Skris 955714Skris#ifndef OS_H 1055714Skris#define OS_H 1155714Skris 1255714Skristypedef long os_time_t; 1355714Skris 1455714Skris/** 1555714Skris * os_sleep - Sleep (sec, usec) 1655714Skris * @sec: Number of seconds to sleep 1755714Skris * @usec: Number of microseconds to sleep 1855714Skris */ 1955714Skrisvoid os_sleep(os_time_t sec, os_time_t usec); 2055714Skris 2155714Skrisstruct os_time { 2255714Skris os_time_t sec; 2355714Skris os_time_t usec; 2455714Skris}; 2555714Skris 2655714Skrisstruct os_reltime { 2755714Skris os_time_t sec; 2855714Skris os_time_t usec; 2955714Skris}; 3055714Skris 3155714Skris/** 3255714Skris * os_get_time - Get current time (sec, usec) 3355714Skris * @t: Pointer to buffer for the time 3455714Skris * Returns: 0 on success, -1 on failure 3555714Skris */ 3655714Skrisint os_get_time(struct os_time *t); 3755714Skris 3855714Skris/** 3955714Skris * os_get_reltime - Get relative time (sec, usec) 4055714Skris * @t: Pointer to buffer for the time 4155714Skris * Returns: 0 on success, -1 on failure 4255714Skris */ 4355714Skrisint os_get_reltime(struct os_reltime *t); 4455714Skris 4555714Skris 4655714Skris/* Helpers for handling struct os_time */ 4755714Skris 4855714Skrisstatic inline int os_time_before(struct os_time *a, struct os_time *b) 4955714Skris{ 5055714Skris return (a->sec < b->sec) || 5155714Skris (a->sec == b->sec && a->usec < b->usec); 5255714Skris} 5355714Skris 5455714Skris 5555714Skrisstatic inline void os_time_sub(struct os_time *a, struct os_time *b, 5655714Skris struct os_time *res) 5755714Skris{ 58109998Smarkm res->sec = a->sec - b->sec; 59109998Smarkm res->usec = a->usec - b->usec; 60109998Smarkm if (res->usec < 0) { 61109998Smarkm res->sec--; 62109998Smarkm res->usec += 1000000; 63109998Smarkm } 64109998Smarkm} 65109998Smarkm 66109998Smarkm 67109998Smarkm/* Helpers for handling struct os_reltime */ 68109998Smarkm 69109998Smarkmstatic inline int os_reltime_before(struct os_reltime *a, 70109998Smarkm struct os_reltime *b) 71109998Smarkm{ 72109998Smarkm return (a->sec < b->sec) || 73109998Smarkm (a->sec == b->sec && a->usec < b->usec); 74109998Smarkm} 75109998Smarkm 76109998Smarkm 77109998Smarkmstatic inline void os_reltime_sub(struct os_reltime *a, struct os_reltime *b, 78109998Smarkm struct os_reltime *res) 79109998Smarkm{ 80109998Smarkm res->sec = a->sec - b->sec; 81109998Smarkm res->usec = a->usec - b->usec; 82109998Smarkm if (res->usec < 0) { 83109998Smarkm res->sec--; 84109998Smarkm res->usec += 1000000; 85109998Smarkm } 86109998Smarkm} 87109998Smarkm 88109998Smarkm 89109998Smarkmstatic inline void os_reltime_age(struct os_reltime *start, 90109998Smarkm struct os_reltime *age) 91109998Smarkm{ 92109998Smarkm struct os_reltime now; 93109998Smarkm 94109998Smarkm os_get_reltime(&now); 95109998Smarkm os_reltime_sub(&now, start, age); 96109998Smarkm} 97109998Smarkm 98109998Smarkm 99109998Smarkmstatic inline int os_reltime_expired(struct os_reltime *now, 100109998Smarkm struct os_reltime *ts, 101109998Smarkm os_time_t timeout_secs) 102109998Smarkm{ 103109998Smarkm struct os_reltime age; 104109998Smarkm 105109998Smarkm os_reltime_sub(now, ts, &age); 106109998Smarkm return (age.sec > timeout_secs) || 107109998Smarkm (age.sec == timeout_secs && age.usec > 0); 108109998Smarkm} 109109998Smarkm 110109998Smarkm 111160814Ssimonstatic inline int os_reltime_initialized(struct os_reltime *t) 112160814Ssimon{ 113160814Ssimon return t->sec != 0 || t->usec != 0; 114160814Ssimon} 115160814Ssimon 11655714Skris 117109998Smarkm/** 118109998Smarkm * os_mktime - Convert broken-down time into seconds since 1970-01-01 119109998Smarkm * @year: Four digit year 12059191Skris * @month: Month (1 .. 12) 12159191Skris * @day: Day of month (1 .. 31) 12259191Skris * @hour: Hour (0 .. 23) 12355714Skris * @min: Minute (0 .. 59) 12455714Skris * @sec: Second (0 .. 60) 12555714Skris * @t: Buffer for returning calendar time representation (seconds since 12659191Skris * 1970-01-01 00:00:00) 12755714Skris * Returns: 0 on success, -1 on failure 128109998Smarkm * 129109998Smarkm * Note: The result is in seconds from Epoch, i.e., in UTC, not in local time 13055714Skris * which is used by POSIX mktime(). 131160814Ssimon */ 132160814Ssimonint os_mktime(int year, int month, int day, int hour, int min, int sec, 133160814Ssimon os_time_t *t); 134160814Ssimon 13555714Skrisstruct os_tm { 13655714Skris int sec; /* 0..59 or 60 for leap seconds */ 13759191Skris int min; /* 0..59 */ 13855714Skris int hour; /* 0..23 */ 139160814Ssimon int day; /* 1..31 */ 14055714Skris int month; /* 1..12 */ 141111147Snectar int year; /* Four digit year */ 142109998Smarkm}; 143111147Snectar 14455714Skrisint os_gmtime(os_time_t t, struct os_tm *tm); 14559191Skris 146160814Ssimon/** 147160814Ssimon * os_daemonize - Run in the background (detach from the controlling terminal) 148160814Ssimon * @pid_file: File name to write the process ID to or %NULL to skip this 149160814Ssimon * Returns: 0 on success, -1 on failure 150160814Ssimon */ 151160814Ssimonint os_daemonize(const char *pid_file); 152160814Ssimon 153160814Ssimon/** 154160814Ssimon * os_daemonize_terminate - Stop running in the background (remove pid file) 155160814Ssimon * @pid_file: File name to write the process ID to or %NULL to skip this 156109998Smarkm */ 157109998Smarkmvoid os_daemonize_terminate(const char *pid_file); 158109998Smarkm 159109998Smarkm/** 160109998Smarkm * os_get_random - Get cryptographically strong pseudo random data 161109998Smarkm * @buf: Buffer for pseudo random data 162109998Smarkm * @len: Length of the buffer 163109998Smarkm * Returns: 0 on success, -1 on failure 164109998Smarkm */ 165109998Smarkmint os_get_random(unsigned char *buf, size_t len); 166109998Smarkm 16755714Skris/** 16855714Skris * os_random - Get pseudo random value (not necessarily very strong) 169109998Smarkm * Returns: Pseudo random value 17055714Skris */ 17155714Skrisunsigned long os_random(void); 172109998Smarkm 173109998Smarkm/** 174109998Smarkm * os_rel2abs_path - Get an absolute path for a file 175160814Ssimon * @rel_path: Relative path to a file 176160814Ssimon * Returns: Absolute path for the file or %NULL on failure 177160814Ssimon * 17855714Skris * This function tries to convert a relative path of a file to an absolute path 17955714Skris * in order for the file to be found even if current working directory has 18055714Skris * changed. The returned value is allocated and caller is responsible for 18155714Skris * freeing it. It is acceptable to just return the same path in an allocated 18255714Skris * buffer, e.g., return strdup(rel_path). This function is only used to find 183109998Smarkm * configuration files when os_daemonize() may have changed the current working 184109998Smarkm * directory and relative path would be pointing to a different location. 185160814Ssimon */ 186160814Ssimonchar * os_rel2abs_path(const char *rel_path); 187109998Smarkm 18859191Skris/** 189109998Smarkm * os_program_init - Program initialization (called at start) 19055714Skris * Returns: 0 on success, -1 on failure 19168651Skris * 19255714Skris * This function is called when a programs starts. If there are any OS specific 193109998Smarkm * processing that is needed, it can be placed here. It is also acceptable to 194160814Ssimon * just return 0 if not special processing is needed. 195160814Ssimon */ 196160814Ssimonint os_program_init(void); 197160814Ssimon 198160814Ssimon/** 199160814Ssimon * os_program_deinit - Program deinitialization (called just before exit) 200160814Ssimon * 201160814Ssimon * This function is called just before a program exists. If there are any OS 202160814Ssimon * specific processing, e.g., freeing resourced allocated in os_program_init(), 203109998Smarkm * it should be done here. It is also acceptable for this function to do 204109998Smarkm * nothing. 20555714Skris */ 20659191Skrisvoid os_program_deinit(void); 20759191Skris 20855714Skris/** 20955714Skris * os_setenv - Set environment variable 21059191Skris * @name: Name of the variable 21159191Skris * @value: Value to set to the variable 21259191Skris * @overwrite: Whether existing variable should be overwritten 21355714Skris * Returns: 0 on success, -1 on error 21459191Skris * 21559191Skris * This function is only used for wpa_cli action scripts. OS wrapper does not 21655714Skris * need to implement this if such functionality is not needed. 21755714Skris */ 21855714Skrisint os_setenv(const char *name, const char *value, int overwrite); 21955714Skris 22055714Skris/** 22155714Skris * os_unsetenv - Delete environent variable 22255714Skris * @name: Name of the variable 22359191Skris * Returns: 0 on success, -1 on error 22455714Skris * 22559191Skris * This function is only used for wpa_cli action scripts. OS wrapper does not 22655714Skris * need to implement this if such functionality is not needed. 227160814Ssimon */ 22855714Skrisint os_unsetenv(const char *name); 22955714Skris 23055714Skris/** 23155714Skris * os_readfile - Read a file to an allocated memory buffer 23255714Skris * @name: Name of the file to read 23355714Skris * @len: For returning the length of the allocated buffer 234160814Ssimon * Returns: Pointer to the allocated buffer or %NULL on failure 235160814Ssimon * 236160814Ssimon * This function allocates memory and reads the given file to this buffer. Both 23755714Skris * binary and text files can be read with this function. The caller is 23855714Skris * responsible for freeing the returned buffer with os_free(). 23955714Skris */ 24055714Skrischar * os_readfile(const char *name, size_t *len); 24155714Skris 242109998Smarkm/** 24359191Skris * os_file_exists - Check whether the specified file exists 24459191Skris * @fname: Path and name of the file 24559191Skris * Returns: 1 if the file exists or 0 if not 24655714Skris */ 247160814Ssimonint os_file_exists(const char *fname); 248160814Ssimon 249160814Ssimon/** 250109998Smarkm * os_fdatasync - Sync a file's (for a given stream) state with storage device 25155714Skris * @stream: the stream to be flushed 25255714Skris * Returns: 0 if the operation succeeded or -1 on failure 253109998Smarkm */ 25455714Skrisint os_fdatasync(FILE *stream); 25555714Skris 256109998Smarkm/** 25755714Skris * os_zalloc - Allocate and zero memory 25855714Skris * @size: Number of bytes to allocate 25955714Skris * Returns: Pointer to allocated and zeroed memory or %NULL on failure 26055714Skris * 26159191Skris * Caller is responsible for freeing the returned buffer with os_free(). 26259191Skris */ 26359191Skrisvoid * os_zalloc(size_t size); 26459191Skris 26555714Skris/** 26655714Skris * os_calloc - Allocate and zero memory for an array 26755714Skris * @nmemb: Number of members in the array 26859191Skris * @size: Number of bytes in each member 269109998Smarkm * Returns: Pointer to allocated and zeroed memory or %NULL on failure 270160814Ssimon * 271160814Ssimon * This function can be used as a wrapper for os_zalloc(nmemb * size) when an 272160814Ssimon * allocation is used for an array. The main benefit over os_zalloc() is in 273160814Ssimon * having an extra check to catch integer overflows in multiplication. 274160814Ssimon * 275160814Ssimon * Caller is responsible for freeing the returned buffer with os_free(). 276160814Ssimon */ 27755714Skrisstatic inline void * os_calloc(size_t nmemb, size_t size) 27855714Skris{ 27959191Skris if (size && nmemb > (~(size_t) 0) / size) 28059191Skris return NULL; 28159191Skris return os_zalloc(nmemb * size); 28259191Skris} 28359191Skris 28459191Skris 28559191Skris/* 28659191Skris * The following functions are wrapper for standard ANSI C or POSIX functions. 28759191Skris * By default, they are just defined to use the standard function name and no 28859191Skris * os_*.c implementation is needed for them. This avoids extra function calls 28959191Skris * by allowing the C pre-processor take care of the function name mapping. 29059191Skris * 29159191Skris * If the target system uses a C library that does not provide these functions, 29259191Skris * build_config.h can be used to define the wrappers to use a different 29359191Skris * function name. This can be done on function-by-function basis since the 29459191Skris * defines here are only used if build_config.h does not define the os_* name. 29559191Skris * If needed, os_*.c file can be used to implement the functions that are not 29659191Skris * included in the C library on the target system. Alternatively, 29759191Skris * OS_NO_C_LIB_DEFINES can be defined to skip all defines here in which case 298109998Smarkm * these functions need to be implemented in os_*.c file for the target system. 29959191Skris */ 30059191Skris 30159191Skris#ifdef OS_NO_C_LIB_DEFINES 30259191Skris 30359191Skris/** 30459191Skris * os_malloc - Allocate dynamic memory 30559191Skris * @size: Size of the buffer to allocate 306109998Smarkm * Returns: Allocated buffer or %NULL on failure 30759191Skris * 30859191Skris * Caller is responsible for freeing the returned buffer with os_free(). 30959191Skris */ 31059191Skrisvoid * os_malloc(size_t size); 31159191Skris 31259191Skris/** 31359191Skris * os_realloc - Re-allocate dynamic memory 31459191Skris * @ptr: Old buffer from os_malloc() or os_realloc() 31559191Skris * @size: Size of the new buffer 31659191Skris * Returns: Allocated buffer or %NULL on failure 31759191Skris * 31859191Skris * Caller is responsible for freeing the returned buffer with os_free(). 31959191Skris * If re-allocation fails, %NULL is returned and the original buffer (ptr) is 32059191Skris * not freed and caller is still responsible for freeing it. 32159191Skris */ 32259191Skrisvoid * os_realloc(void *ptr, size_t size); 323109998Smarkm 324109998Smarkm/** 325109998Smarkm * os_free - Free dynamic memory 326109998Smarkm * @ptr: Old buffer from os_malloc() or os_realloc(); can be %NULL 327109998Smarkm */ 328109998Smarkmvoid os_free(void *ptr); 329109998Smarkm 330109998Smarkm/** 331109998Smarkm * os_memcpy - Copy memory area 332109998Smarkm * @dest: Destination 333109998Smarkm * @src: Source 334109998Smarkm * @n: Number of bytes to copy 335109998Smarkm * Returns: dest 336120631Snectar * 337109998Smarkm * The memory areas src and dst must not overlap. os_memmove() can be used with 338109998Smarkm * overlapping memory. 339109998Smarkm */ 340109998Smarkmvoid * os_memcpy(void *dest, const void *src, size_t n); 341109998Smarkm 342109998Smarkm/** 343109998Smarkm * os_memmove - Copy memory area 344109998Smarkm * @dest: Destination 345109998Smarkm * @src: Source 346109998Smarkm * @n: Number of bytes to copy 347109998Smarkm * Returns: dest 348109998Smarkm * 349109998Smarkm * The memory areas src and dst may overlap. 350109998Smarkm */ 351109998Smarkmvoid * os_memmove(void *dest, const void *src, size_t n); 352109998Smarkm 353109998Smarkm/** 354109998Smarkm * os_memset - Fill memory with a constant byte 355109998Smarkm * @s: Memory area to be filled 356109998Smarkm * @c: Constant byte 357109998Smarkm * @n: Number of bytes started from s to fill with c 358109998Smarkm * Returns: s 359109998Smarkm */ 360109998Smarkmvoid * os_memset(void *s, int c, size_t n); 361109998Smarkm 362109998Smarkm/** 363109998Smarkm * os_memcmp - Compare memory areas 364109998Smarkm * @s1: First buffer 365109998Smarkm * @s2: Second buffer 366109998Smarkm * @n: Maximum numbers of octets to compare 367109998Smarkm * Returns: An integer less than, equal to, or greater than zero if s1 is 368109998Smarkm * found to be less than, to match, or be greater than s2. Only first n 369109998Smarkm * characters will be compared. 370109998Smarkm */ 371109998Smarkmint os_memcmp(const void *s1, const void *s2, size_t n); 372109998Smarkm 373109998Smarkm/** 374109998Smarkm * os_strdup - Duplicate a string 375109998Smarkm * @s: Source string 376109998Smarkm * Returns: Allocated buffer with the string copied into it or %NULL on failure 377109998Smarkm * 378109998Smarkm * Caller is responsible for freeing the returned buffer with os_free(). 379109998Smarkm */ 380109998Smarkmchar * os_strdup(const char *s); 381109998Smarkm 382109998Smarkm/** 383109998Smarkm * os_strlen - Calculate the length of a string 384109998Smarkm * @s: '\0' terminated string 385109998Smarkm * Returns: Number of characters in s (not counting the '\0' terminator) 386160814Ssimon */ 38755714Skrissize_t os_strlen(const char *s); 38855714Skris 38955714Skris/** 39055714Skris * os_strcasecmp - Compare two strings ignoring case 39155714Skris * @s1: First string 39255714Skris * @s2: Second string 39355714Skris * Returns: An integer less than, equal to, or greater than zero if s1 is 39455714Skris * found to be less than, to match, or be greatred than s2 39555714Skris */ 396160814Ssimonint os_strcasecmp(const char *s1, const char *s2); 397160814Ssimon 39855714Skris/** 39959191Skris * os_strncasecmp - Compare two strings ignoring case 40055714Skris * @s1: First string 40159191Skris * @s2: Second string 402160814Ssimon * @n: Maximum numbers of characters to compare 403160814Ssimon * Returns: An integer less than, equal to, or greater than zero if s1 is 404160814Ssimon * found to be less than, to match, or be greater than s2. Only first n 40555714Skris * characters will be compared. 40655714Skris */ 40755714Skrisint os_strncasecmp(const char *s1, const char *s2, size_t n); 40855714Skris 40955714Skris/** 410160814Ssimon * os_strchr - Locate the first occurrence of a character in string 411109998Smarkm * @s: String 41255714Skris * @c: Character to search for 41359191Skris * Returns: Pointer to the matched character or %NULL if not found 41455714Skris */ 415160814Ssimonchar * os_strchr(const char *s, int c); 416160814Ssimon 417160814Ssimon/** 41859191Skris * os_strrchr - Locate the last occurrence of a character in string 419160814Ssimon * @s: String 42059191Skris * @c: Character to search for 42159191Skris * Returns: Pointer to the matched character or %NULL if not found 422109998Smarkm */ 423160814Ssimonchar * os_strrchr(const char *s, int c); 424109998Smarkm 425142425Snectar/** 426160814Ssimon * os_strcmp - Compare two strings 427160814Ssimon * @s1: First string 42855714Skris * @s2: Second string 42959191Skris * Returns: An integer less than, equal to, or greater than zero if s1 is 43059191Skris * found to be less than, to match, or be greatred than s2 43159191Skris */ 432109998Smarkmint os_strcmp(const char *s1, const char *s2); 433109998Smarkm 434109998Smarkm/** 435109998Smarkm * os_strncmp - Compare two strings 436109998Smarkm * @s1: First string 437109998Smarkm * @s2: Second string 438109998Smarkm * @n: Maximum numbers of characters to compare 439109998Smarkm * Returns: An integer less than, equal to, or greater than zero if s1 is 440109998Smarkm * found to be less than, to match, or be greater than s2. Only first n 441109998Smarkm * characters will be compared. 442109998Smarkm */ 443109998Smarkmint os_strncmp(const char *s1, const char *s2, size_t n); 444109998Smarkm 445109998Smarkm/** 446109998Smarkm * os_strstr - Locate a substring 447109998Smarkm * @haystack: String (haystack) to search from 44859191Skris * @needle: Needle to search from haystack 44959191Skris * Returns: Pointer to the beginning of the substring or %NULL if not found 45059191Skris */ 45159191Skrischar * os_strstr(const char *haystack, const char *needle); 45255714Skris 45355714Skris/** 45455714Skris * os_snprintf - Print to a memory buffer 45555714Skris * @str: Memory buffer to print into 45655714Skris * @size: Maximum length of the str buffer 45755714Skris * @format: printf format 45855714Skris * Returns: Number of characters printed (not including trailing '\0'). 459160814Ssimon * 46055714Skris * If the output buffer is truncated, number of characters which would have 46155714Skris * been written is returned. Since some C libraries return -1 in such a case, 46255714Skris * the caller must be prepared on that value, too, to indicate truncation. 463160814Ssimon * 464160814Ssimon * Note: Some C library implementations of snprintf() may not guarantee null 465160814Ssimon * termination in case the output is truncated. The OS wrapper function of 466160814Ssimon * os_snprintf() should provide this guarantee, i.e., to null terminate the 467160814Ssimon * output buffer if a C library version of the function is used and if that 468160814Ssimon * function does not guarantee null termination. 469160814Ssimon * 470160814Ssimon * If the target system does not include snprintf(), see, e.g., 471160814Ssimon * http://www.ijs.si/software/snprintf/ for an example of a portable 472160814Ssimon * implementation of snprintf. 47355714Skris */ 47455714Skrisint os_snprintf(char *str, size_t size, const char *format, ...); 47555714Skris 47655714Skris#else /* OS_NO_C_LIB_DEFINES */ 47755714Skris 47855714Skris#ifdef WPA_TRACE 47955714Skrisvoid * os_malloc(size_t size); 480109998Smarkmvoid * os_realloc(void *ptr, size_t size); 481109998Smarkmvoid os_free(void *ptr); 48255714Skrischar * os_strdup(const char *s); 483109998Smarkm#else /* WPA_TRACE */ 484109998Smarkm#ifndef os_malloc 485109998Smarkm#define os_malloc(s) malloc((s)) 486109998Smarkm#endif 48759191Skris#ifndef os_realloc 488109998Smarkm#define os_realloc(p, s) realloc((p), (s)) 489109998Smarkm#endif 49059191Skris#ifndef os_free 491109998Smarkm#define os_free(p) free((p)) 492109998Smarkm#endif 49359191Skris#ifndef os_strdup 494109998Smarkm#ifdef _MSC_VER 49559191Skris#define os_strdup(s) _strdup(s) 49659191Skris#else 497160814Ssimon#define os_strdup(s) strdup(s) 498160814Ssimon#endif 49955714Skris#endif 50055714Skris#endif /* WPA_TRACE */ 50155714Skris 50255714Skris#ifndef os_memcpy 50355714Skris#define os_memcpy(d, s, n) memcpy((d), (s), (n)) 50455714Skris#endif 50555714Skris#ifndef os_memmove 50655714Skris#define os_memmove(d, s, n) memmove((d), (s), (n)) 50755714Skris#endif 50855714Skris#ifndef os_memset 50955714Skris#define os_memset(s, c, n) memset(s, c, n) 51055714Skris#endif 51155714Skris#ifndef os_memcmp 51255714Skris#define os_memcmp(s1, s2, n) memcmp((s1), (s2), (n)) 51355714Skris#endif 51455714Skris 51555714Skris#ifndef os_strlen 51655714Skris#define os_strlen(s) strlen(s) 51755714Skris#endif 51855714Skris#ifndef os_strcasecmp 51955714Skris#ifdef _MSC_VER 52055714Skris#define os_strcasecmp(s1, s2) _stricmp((s1), (s2)) 52155714Skris#else 52255714Skris#define os_strcasecmp(s1, s2) strcasecmp((s1), (s2)) 52355714Skris#endif 52455714Skris#endif 52555714Skris#ifndef os_strncasecmp 52655714Skris#ifdef _MSC_VER 52755714Skris#define os_strncasecmp(s1, s2, n) _strnicmp((s1), (s2), (n)) 52855714Skris#else 52955714Skris#define os_strncasecmp(s1, s2, n) strncasecmp((s1), (s2), (n)) 53059191Skris#endif 53159191Skris#endif 53259191Skris#ifndef os_strchr 53359191Skris#define os_strchr(s, c) strchr((s), (c)) 53459191Skris#endif 53559191Skris#ifndef os_strcmp 53659191Skris#define os_strcmp(s1, s2) strcmp((s1), (s2)) 53759191Skris#endif 53859191Skris#ifndef os_strncmp 53959191Skris#define os_strncmp(s1, s2, n) strncmp((s1), (s2), (n)) 54055714Skris#endif 54155714Skris#ifndef os_strrchr 54255714Skris#define os_strrchr(s, c) strrchr((s), (c)) 54355714Skris#endif 54455714Skris#ifndef os_strstr 54559191Skris#define os_strstr(h, n) strstr((h), (n)) 54659191Skris#endif 54759191Skris 54859191Skris#ifndef os_snprintf 54959191Skris#ifdef _MSC_VER 55055714Skris#define os_snprintf _snprintf 55155714Skris#else 55255714Skris#define os_snprintf snprintf 55355714Skris#endif 55455714Skris#endif 55555714Skris 55655714Skris#endif /* OS_NO_C_LIB_DEFINES */ 55755714Skris 55855714Skris 55955714Skrisstatic inline int os_snprintf_error(size_t size, int res) 56055714Skris{ 56155714Skris return res < 0 || (unsigned int) res >= size; 56255714Skris} 56355714Skris 56455714Skris 56555714Skrisstatic inline void * os_realloc_array(void *ptr, size_t nmemb, size_t size) 56655714Skris{ 56755714Skris if (size && nmemb > (~(size_t) 0) / size) 56855714Skris return NULL; 56955714Skris return os_realloc(ptr, nmemb * size); 57055714Skris} 57155714Skris 57255714Skris/** 57359191Skris * os_remove_in_array - Remove a member from an array by index 57459191Skris * @ptr: Pointer to the array 57559191Skris * @nmemb: Current member count of the array 57659191Skris * @size: The size per member of the array 577109998Smarkm * @idx: Index of the member to be removed 578109998Smarkm */ 579109998Smarkmstatic inline void os_remove_in_array(void *ptr, size_t nmemb, size_t size, 580109998Smarkm size_t idx) 581109998Smarkm{ 582109998Smarkm if (idx < nmemb - 1) 583109998Smarkm os_memmove(((unsigned char *) ptr) + idx * size, 584109998Smarkm ((unsigned char *) ptr) + (idx + 1) * size, 585160814Ssimon (nmemb - idx - 1) * size); 586160814Ssimon} 587160814Ssimon 588160814Ssimon/** 589160814Ssimon * os_strlcpy - Copy a string with size bound and NUL-termination 590160814Ssimon * @dest: Destination 591160814Ssimon * @src: Source 592160814Ssimon * @siz: Size of the target buffer 593160814Ssimon * Returns: Total length of the target string (length of src) (not including 594160814Ssimon * NUL-termination) 595109998Smarkm * 596109998Smarkm * This function matches in behavior with the strlcpy(3) function in OpenBSD. 597160814Ssimon */ 598109998Smarkmsize_t os_strlcpy(char *dest, const char *src, size_t siz); 599160814Ssimon 600160814Ssimon/** 601160814Ssimon * os_memcmp_const - Constant time memory comparison 602160814Ssimon * @a: First buffer to compare 603160814Ssimon * @b: Second buffer to compare 604160814Ssimon * @len: Number of octets to compare 605160814Ssimon * Returns: 0 if buffers are equal, non-zero if not 606160814Ssimon * 60755714Skris * This function is meant for comparing passwords or hash values where 60855714Skris * difference in execution time could provide external observer information 60955714Skris * about the location of the difference in the memory buffers. The return value 61055714Skris * does not behave like os_memcmp(), i.e., os_memcmp_const() cannot be used to 61155714Skris * sort items into a defined order. Unlike os_memcmp(), execution time of 61255714Skris * os_memcmp_const() does not depend on the contents of the compared memory 61355714Skris * buffers, but only on the total compared length. 61455714Skris */ 61555714Skrisint os_memcmp_const(const void *a, const void *b, size_t len); 61655714Skris 61755714Skris/** 61855714Skris * os_exec - Execute an external program 61955714Skris * @program: Path to the program 62055714Skris * @arg: Command line argument string 62155714Skris * @wait_completion: Whether to wait until the program execution completes 62255714Skris * Returns: 0 on success, -1 on error 623160814Ssimon */ 624160814Ssimonint os_exec(const char *program, const char *arg, int wait_completion); 625160814Ssimon 626160814Ssimon 627160814Ssimon#ifdef OS_REJECT_C_LIB_FUNCTIONS 628160814Ssimon#define malloc OS_DO_NOT_USE_malloc 629160814Ssimon#define realloc OS_DO_NOT_USE_realloc 630160814Ssimon#define free OS_DO_NOT_USE_free 631142425Snectar#define memcpy OS_DO_NOT_USE_memcpy 63255714Skris#define memmove OS_DO_NOT_USE_memmove 63355714Skris#define memset OS_DO_NOT_USE_memset 63459191Skris#define memcmp OS_DO_NOT_USE_memcmp 63559191Skris#undef strdup 63659191Skris#define strdup OS_DO_NOT_USE_strdup 63759191Skris#define strlen OS_DO_NOT_USE_strlen 638109998Smarkm#define strcasecmp OS_DO_NOT_USE_strcasecmp 63955714Skris#define strncasecmp OS_DO_NOT_USE_strncasecmp 64055714Skris#undef strchr 64159191Skris#define strchr OS_DO_NOT_USE_strchr 64259191Skris#undef strcmp 64359191Skris#define strcmp OS_DO_NOT_USE_strcmp 64459191Skris#undef strncmp 64559191Skris#define strncmp OS_DO_NOT_USE_strncmp 64659191Skris#undef strncpy 64759191Skris#define strncpy OS_DO_NOT_USE_strncpy 64859191Skris#define strrchr OS_DO_NOT_USE_strrchr 64959191Skris#define strstr OS_DO_NOT_USE_strstr 65059191Skris#undef snprintf 65159191Skris#define snprintf OS_DO_NOT_USE_snprintf 65255714Skris 65355714Skris#define strcpy OS_DO_NOT_USE_strcpy 65455714Skris#endif /* OS_REJECT_C_LIB_FUNCTIONS */ 65555714Skris 65655714Skris 657160814Ssimon#if defined(WPA_TRACE_BFD) && defined(CONFIG_TESTING_OPTIONS) 658109998Smarkm#define TEST_FAIL() testing_test_fail() 659109998Smarkmint testing_test_fail(void); 660109998Smarkm#else 661109998Smarkm#define TEST_FAIL() 0 662109998Smarkm#endif 663127128Snectar 664127128Snectar#endif /* OS_H */ 665127128Snectar