16d49e1aeSJan Lentfer /*
23ff40c12SJohn Marino  * OS specific functions
33ff40c12SJohn Marino  * Copyright (c) 2005-2009, Jouni Malinen <j@w1.fi>
46d49e1aeSJan Lentfer  *
53ff40c12SJohn Marino  * This software may be distributed under the terms of the BSD license.
63ff40c12SJohn Marino  * See README for more details.
76d49e1aeSJan Lentfer  */
86d49e1aeSJan Lentfer 
96d49e1aeSJan Lentfer #ifndef OS_H
106d49e1aeSJan Lentfer #define OS_H
116d49e1aeSJan Lentfer 
126d49e1aeSJan Lentfer typedef long os_time_t;
136d49e1aeSJan Lentfer 
146d49e1aeSJan Lentfer /**
156d49e1aeSJan Lentfer  * os_sleep - Sleep (sec, usec)
166d49e1aeSJan Lentfer  * @sec: Number of seconds to sleep
176d49e1aeSJan Lentfer  * @usec: Number of microseconds to sleep
186d49e1aeSJan Lentfer  */
196d49e1aeSJan Lentfer void os_sleep(os_time_t sec, os_time_t usec);
206d49e1aeSJan Lentfer 
216d49e1aeSJan Lentfer struct os_time {
226d49e1aeSJan Lentfer 	os_time_t sec;
236d49e1aeSJan Lentfer 	os_time_t usec;
246d49e1aeSJan Lentfer };
256d49e1aeSJan Lentfer 
263ff40c12SJohn Marino struct os_reltime {
273ff40c12SJohn Marino 	os_time_t sec;
283ff40c12SJohn Marino 	os_time_t usec;
293ff40c12SJohn Marino };
303ff40c12SJohn Marino 
316d49e1aeSJan Lentfer /**
326d49e1aeSJan Lentfer  * os_get_time - Get current time (sec, usec)
336d49e1aeSJan Lentfer  * @t: Pointer to buffer for the time
346d49e1aeSJan Lentfer  * Returns: 0 on success, -1 on failure
356d49e1aeSJan Lentfer  */
366d49e1aeSJan Lentfer int os_get_time(struct os_time *t);
376d49e1aeSJan Lentfer 
383ff40c12SJohn Marino /**
393ff40c12SJohn Marino  * os_get_reltime - Get relative time (sec, usec)
403ff40c12SJohn Marino  * @t: Pointer to buffer for the time
413ff40c12SJohn Marino  * Returns: 0 on success, -1 on failure
423ff40c12SJohn Marino  */
433ff40c12SJohn Marino int os_get_reltime(struct os_reltime *t);
446d49e1aeSJan Lentfer 
456d49e1aeSJan Lentfer 
463ff40c12SJohn Marino /* Helpers for handling struct os_time */
476d49e1aeSJan Lentfer 
os_time_before(struct os_time * a,struct os_time * b)483ff40c12SJohn Marino static inline int os_time_before(struct os_time *a, struct os_time *b)
493ff40c12SJohn Marino {
503ff40c12SJohn Marino 	return (a->sec < b->sec) ||
513ff40c12SJohn Marino 	       (a->sec == b->sec && a->usec < b->usec);
523ff40c12SJohn Marino }
533ff40c12SJohn Marino 
543ff40c12SJohn Marino 
os_time_sub(struct os_time * a,struct os_time * b,struct os_time * res)553ff40c12SJohn Marino static inline void os_time_sub(struct os_time *a, struct os_time *b,
563ff40c12SJohn Marino 			       struct os_time *res)
573ff40c12SJohn Marino {
583ff40c12SJohn Marino 	res->sec = a->sec - b->sec;
593ff40c12SJohn Marino 	res->usec = a->usec - b->usec;
603ff40c12SJohn Marino 	if (res->usec < 0) {
613ff40c12SJohn Marino 		res->sec--;
623ff40c12SJohn Marino 		res->usec += 1000000;
633ff40c12SJohn Marino 	}
643ff40c12SJohn Marino }
653ff40c12SJohn Marino 
663ff40c12SJohn Marino 
673ff40c12SJohn Marino /* Helpers for handling struct os_reltime */
683ff40c12SJohn Marino 
os_reltime_before(struct os_reltime * a,struct os_reltime * b)693ff40c12SJohn Marino static inline int os_reltime_before(struct os_reltime *a,
703ff40c12SJohn Marino 				    struct os_reltime *b)
713ff40c12SJohn Marino {
723ff40c12SJohn Marino 	return (a->sec < b->sec) ||
733ff40c12SJohn Marino 	       (a->sec == b->sec && a->usec < b->usec);
743ff40c12SJohn Marino }
753ff40c12SJohn Marino 
763ff40c12SJohn Marino 
os_reltime_sub(struct os_reltime * a,struct os_reltime * b,struct os_reltime * res)773ff40c12SJohn Marino static inline void os_reltime_sub(struct os_reltime *a, struct os_reltime *b,
783ff40c12SJohn Marino 				  struct os_reltime *res)
793ff40c12SJohn Marino {
803ff40c12SJohn Marino 	res->sec = a->sec - b->sec;
813ff40c12SJohn Marino 	res->usec = a->usec - b->usec;
823ff40c12SJohn Marino 	if (res->usec < 0) {
833ff40c12SJohn Marino 		res->sec--;
843ff40c12SJohn Marino 		res->usec += 1000000;
853ff40c12SJohn Marino 	}
863ff40c12SJohn Marino }
873ff40c12SJohn Marino 
883ff40c12SJohn Marino 
os_reltime_age(struct os_reltime * start,struct os_reltime * age)893ff40c12SJohn Marino static inline void os_reltime_age(struct os_reltime *start,
903ff40c12SJohn Marino 				  struct os_reltime *age)
913ff40c12SJohn Marino {
923ff40c12SJohn Marino 	struct os_reltime now;
933ff40c12SJohn Marino 
943ff40c12SJohn Marino 	os_get_reltime(&now);
953ff40c12SJohn Marino 	os_reltime_sub(&now, start, age);
963ff40c12SJohn Marino }
973ff40c12SJohn Marino 
983ff40c12SJohn Marino 
os_reltime_expired(struct os_reltime * now,struct os_reltime * ts,os_time_t timeout_secs)993ff40c12SJohn Marino static inline int os_reltime_expired(struct os_reltime *now,
1003ff40c12SJohn Marino 				     struct os_reltime *ts,
1013ff40c12SJohn Marino 				     os_time_t timeout_secs)
1023ff40c12SJohn Marino {
1033ff40c12SJohn Marino 	struct os_reltime age;
1043ff40c12SJohn Marino 
1053ff40c12SJohn Marino 	os_reltime_sub(now, ts, &age);
1063ff40c12SJohn Marino 	return (age.sec > timeout_secs) ||
1073ff40c12SJohn Marino 	       (age.sec == timeout_secs && age.usec > 0);
1083ff40c12SJohn Marino }
1093ff40c12SJohn Marino 
1103ff40c12SJohn Marino 
os_reltime_initialized(struct os_reltime * t)1113ff40c12SJohn Marino static inline int os_reltime_initialized(struct os_reltime *t)
1123ff40c12SJohn Marino {
1133ff40c12SJohn Marino 	return t->sec != 0 || t->usec != 0;
1143ff40c12SJohn Marino }
1153ff40c12SJohn Marino 
1166d49e1aeSJan Lentfer 
1176d49e1aeSJan Lentfer /**
1186d49e1aeSJan Lentfer  * os_mktime - Convert broken-down time into seconds since 1970-01-01
1196d49e1aeSJan Lentfer  * @year: Four digit year
1206d49e1aeSJan Lentfer  * @month: Month (1 .. 12)
1216d49e1aeSJan Lentfer  * @day: Day of month (1 .. 31)
1226d49e1aeSJan Lentfer  * @hour: Hour (0 .. 23)
1236d49e1aeSJan Lentfer  * @min: Minute (0 .. 59)
1246d49e1aeSJan Lentfer  * @sec: Second (0 .. 60)
1256d49e1aeSJan Lentfer  * @t: Buffer for returning calendar time representation (seconds since
1266d49e1aeSJan Lentfer  * 1970-01-01 00:00:00)
1276d49e1aeSJan Lentfer  * Returns: 0 on success, -1 on failure
1286d49e1aeSJan Lentfer  *
1296d49e1aeSJan Lentfer  * Note: The result is in seconds from Epoch, i.e., in UTC, not in local time
1306d49e1aeSJan Lentfer  * which is used by POSIX mktime().
1316d49e1aeSJan Lentfer  */
1326d49e1aeSJan Lentfer int os_mktime(int year, int month, int day, int hour, int min, int sec,
1336d49e1aeSJan Lentfer 	      os_time_t *t);
1346d49e1aeSJan Lentfer 
1353ff40c12SJohn Marino struct os_tm {
1363ff40c12SJohn Marino 	int sec; /* 0..59 or 60 for leap seconds */
1373ff40c12SJohn Marino 	int min; /* 0..59 */
1383ff40c12SJohn Marino 	int hour; /* 0..23 */
1393ff40c12SJohn Marino 	int day; /* 1..31 */
1403ff40c12SJohn Marino 	int month; /* 1..12 */
1413ff40c12SJohn Marino 	int year; /* Four digit year */
1423ff40c12SJohn Marino };
1433ff40c12SJohn Marino 
1443ff40c12SJohn Marino int os_gmtime(os_time_t t, struct os_tm *tm);
1456d49e1aeSJan Lentfer 
1466d49e1aeSJan Lentfer /**
1476d49e1aeSJan Lentfer  * os_daemonize - Run in the background (detach from the controlling terminal)
1486d49e1aeSJan Lentfer  * @pid_file: File name to write the process ID to or %NULL to skip this
1496d49e1aeSJan Lentfer  * Returns: 0 on success, -1 on failure
1506d49e1aeSJan Lentfer  */
1516d49e1aeSJan Lentfer int os_daemonize(const char *pid_file);
1526d49e1aeSJan Lentfer 
1536d49e1aeSJan Lentfer /**
1546d49e1aeSJan Lentfer  * os_daemonize_terminate - Stop running in the background (remove pid file)
1556d49e1aeSJan Lentfer  * @pid_file: File name to write the process ID to or %NULL to skip this
1566d49e1aeSJan Lentfer  */
1576d49e1aeSJan Lentfer void os_daemonize_terminate(const char *pid_file);
1586d49e1aeSJan Lentfer 
1596d49e1aeSJan Lentfer /**
1606d49e1aeSJan Lentfer  * os_get_random - Get cryptographically strong pseudo random data
1616d49e1aeSJan Lentfer  * @buf: Buffer for pseudo random data
1626d49e1aeSJan Lentfer  * @len: Length of the buffer
1636d49e1aeSJan Lentfer  * Returns: 0 on success, -1 on failure
1646d49e1aeSJan Lentfer  */
1656d49e1aeSJan Lentfer int os_get_random(unsigned char *buf, size_t len);
1666d49e1aeSJan Lentfer 
1676d49e1aeSJan Lentfer /**
1686d49e1aeSJan Lentfer  * os_random - Get pseudo random value (not necessarily very strong)
1696d49e1aeSJan Lentfer  * Returns: Pseudo random value
1706d49e1aeSJan Lentfer  */
1716d49e1aeSJan Lentfer unsigned long os_random(void);
1726d49e1aeSJan Lentfer 
1736d49e1aeSJan Lentfer /**
1746d49e1aeSJan Lentfer  * os_rel2abs_path - Get an absolute path for a file
1756d49e1aeSJan Lentfer  * @rel_path: Relative path to a file
1766d49e1aeSJan Lentfer  * Returns: Absolute path for the file or %NULL on failure
1776d49e1aeSJan Lentfer  *
1786d49e1aeSJan Lentfer  * This function tries to convert a relative path of a file to an absolute path
1796d49e1aeSJan Lentfer  * in order for the file to be found even if current working directory has
1806d49e1aeSJan Lentfer  * changed. The returned value is allocated and caller is responsible for
1816d49e1aeSJan Lentfer  * freeing it. It is acceptable to just return the same path in an allocated
1826d49e1aeSJan Lentfer  * buffer, e.g., return strdup(rel_path). This function is only used to find
1836d49e1aeSJan Lentfer  * configuration files when os_daemonize() may have changed the current working
1846d49e1aeSJan Lentfer  * directory and relative path would be pointing to a different location.
1856d49e1aeSJan Lentfer  */
1866d49e1aeSJan Lentfer char * os_rel2abs_path(const char *rel_path);
1876d49e1aeSJan Lentfer 
1886d49e1aeSJan Lentfer /**
1896d49e1aeSJan Lentfer  * os_program_init - Program initialization (called at start)
1906d49e1aeSJan Lentfer  * Returns: 0 on success, -1 on failure
1916d49e1aeSJan Lentfer  *
1926d49e1aeSJan Lentfer  * This function is called when a programs starts. If there are any OS specific
1936d49e1aeSJan Lentfer  * processing that is needed, it can be placed here. It is also acceptable to
1946d49e1aeSJan Lentfer  * just return 0 if not special processing is needed.
1956d49e1aeSJan Lentfer  */
1966d49e1aeSJan Lentfer int os_program_init(void);
1976d49e1aeSJan Lentfer 
1986d49e1aeSJan Lentfer /**
1996d49e1aeSJan Lentfer  * os_program_deinit - Program deinitialization (called just before exit)
2006d49e1aeSJan Lentfer  *
2016d49e1aeSJan Lentfer  * This function is called just before a program exists. If there are any OS
2026d49e1aeSJan Lentfer  * specific processing, e.g., freeing resourced allocated in os_program_init(),
2036d49e1aeSJan Lentfer  * it should be done here. It is also acceptable for this function to do
2046d49e1aeSJan Lentfer  * nothing.
2056d49e1aeSJan Lentfer  */
2066d49e1aeSJan Lentfer void os_program_deinit(void);
2076d49e1aeSJan Lentfer 
2086d49e1aeSJan Lentfer /**
2096d49e1aeSJan Lentfer  * os_setenv - Set environment variable
2106d49e1aeSJan Lentfer  * @name: Name of the variable
2116d49e1aeSJan Lentfer  * @value: Value to set to the variable
2126d49e1aeSJan Lentfer  * @overwrite: Whether existing variable should be overwritten
2136d49e1aeSJan Lentfer  * Returns: 0 on success, -1 on error
2146d49e1aeSJan Lentfer  *
2156d49e1aeSJan Lentfer  * This function is only used for wpa_cli action scripts. OS wrapper does not
2166d49e1aeSJan Lentfer  * need to implement this if such functionality is not needed.
2176d49e1aeSJan Lentfer  */
2186d49e1aeSJan Lentfer int os_setenv(const char *name, const char *value, int overwrite);
2196d49e1aeSJan Lentfer 
2206d49e1aeSJan Lentfer /**
2216d49e1aeSJan Lentfer  * os_unsetenv - Delete environent variable
2226d49e1aeSJan Lentfer  * @name: Name of the variable
2236d49e1aeSJan Lentfer  * Returns: 0 on success, -1 on error
2246d49e1aeSJan Lentfer  *
2256d49e1aeSJan Lentfer  * This function is only used for wpa_cli action scripts. OS wrapper does not
2266d49e1aeSJan Lentfer  * need to implement this if such functionality is not needed.
2276d49e1aeSJan Lentfer  */
2286d49e1aeSJan Lentfer int os_unsetenv(const char *name);
2296d49e1aeSJan Lentfer 
2306d49e1aeSJan Lentfer /**
2316d49e1aeSJan Lentfer  * os_readfile - Read a file to an allocated memory buffer
2326d49e1aeSJan Lentfer  * @name: Name of the file to read
2336d49e1aeSJan Lentfer  * @len: For returning the length of the allocated buffer
2346d49e1aeSJan Lentfer  * Returns: Pointer to the allocated buffer or %NULL on failure
2356d49e1aeSJan Lentfer  *
2366d49e1aeSJan Lentfer  * This function allocates memory and reads the given file to this buffer. Both
2376d49e1aeSJan Lentfer  * binary and text files can be read with this function. The caller is
2386d49e1aeSJan Lentfer  * responsible for freeing the returned buffer with os_free().
2396d49e1aeSJan Lentfer  */
2406d49e1aeSJan Lentfer char * os_readfile(const char *name, size_t *len);
2416d49e1aeSJan Lentfer 
2426d49e1aeSJan Lentfer /**
243*a1157835SDaniel Fojt  * os_file_exists - Check whether the specified file exists
244*a1157835SDaniel Fojt  * @fname: Path and name of the file
245*a1157835SDaniel Fojt  * Returns: 1 if the file exists or 0 if not
246*a1157835SDaniel Fojt  */
247*a1157835SDaniel Fojt int os_file_exists(const char *fname);
248*a1157835SDaniel Fojt 
249*a1157835SDaniel Fojt /**
250*a1157835SDaniel Fojt  * os_fdatasync - Sync a file's (for a given stream) state with storage device
251*a1157835SDaniel Fojt  * @stream: the stream to be flushed
252*a1157835SDaniel Fojt  * Returns: 0 if the operation succeeded or -1 on failure
253*a1157835SDaniel Fojt  */
254*a1157835SDaniel Fojt int os_fdatasync(FILE *stream);
255*a1157835SDaniel Fojt 
256*a1157835SDaniel Fojt /**
2576d49e1aeSJan Lentfer  * os_zalloc - Allocate and zero memory
2586d49e1aeSJan Lentfer  * @size: Number of bytes to allocate
2596d49e1aeSJan Lentfer  * Returns: Pointer to allocated and zeroed memory or %NULL on failure
2606d49e1aeSJan Lentfer  *
2616d49e1aeSJan Lentfer  * Caller is responsible for freeing the returned buffer with os_free().
2626d49e1aeSJan Lentfer  */
2636d49e1aeSJan Lentfer void * os_zalloc(size_t size);
2646d49e1aeSJan Lentfer 
2653ff40c12SJohn Marino /**
2663ff40c12SJohn Marino  * os_calloc - Allocate and zero memory for an array
2673ff40c12SJohn Marino  * @nmemb: Number of members in the array
2683ff40c12SJohn Marino  * @size: Number of bytes in each member
2693ff40c12SJohn Marino  * Returns: Pointer to allocated and zeroed memory or %NULL on failure
2703ff40c12SJohn Marino  *
2713ff40c12SJohn Marino  * This function can be used as a wrapper for os_zalloc(nmemb * size) when an
2723ff40c12SJohn Marino  * allocation is used for an array. The main benefit over os_zalloc() is in
2733ff40c12SJohn Marino  * having an extra check to catch integer overflows in multiplication.
2743ff40c12SJohn Marino  *
2753ff40c12SJohn Marino  * Caller is responsible for freeing the returned buffer with os_free().
2763ff40c12SJohn Marino  */
os_calloc(size_t nmemb,size_t size)2773ff40c12SJohn Marino static inline void * os_calloc(size_t nmemb, size_t size)
2783ff40c12SJohn Marino {
2793ff40c12SJohn Marino 	if (size && nmemb > (~(size_t) 0) / size)
2803ff40c12SJohn Marino 		return NULL;
2813ff40c12SJohn Marino 	return os_zalloc(nmemb * size);
2823ff40c12SJohn Marino }
2833ff40c12SJohn Marino 
2846d49e1aeSJan Lentfer 
2856d49e1aeSJan Lentfer /*
2866d49e1aeSJan Lentfer  * The following functions are wrapper for standard ANSI C or POSIX functions.
2876d49e1aeSJan Lentfer  * By default, they are just defined to use the standard function name and no
2886d49e1aeSJan Lentfer  * os_*.c implementation is needed for them. This avoids extra function calls
2896d49e1aeSJan Lentfer  * by allowing the C pre-processor take care of the function name mapping.
2906d49e1aeSJan Lentfer  *
2916d49e1aeSJan Lentfer  * If the target system uses a C library that does not provide these functions,
2926d49e1aeSJan Lentfer  * build_config.h can be used to define the wrappers to use a different
2936d49e1aeSJan Lentfer  * function name. This can be done on function-by-function basis since the
2946d49e1aeSJan Lentfer  * defines here are only used if build_config.h does not define the os_* name.
2956d49e1aeSJan Lentfer  * If needed, os_*.c file can be used to implement the functions that are not
2966d49e1aeSJan Lentfer  * included in the C library on the target system. Alternatively,
2976d49e1aeSJan Lentfer  * OS_NO_C_LIB_DEFINES can be defined to skip all defines here in which case
2986d49e1aeSJan Lentfer  * these functions need to be implemented in os_*.c file for the target system.
2996d49e1aeSJan Lentfer  */
3006d49e1aeSJan Lentfer 
3016d49e1aeSJan Lentfer #ifdef OS_NO_C_LIB_DEFINES
3026d49e1aeSJan Lentfer 
3036d49e1aeSJan Lentfer /**
3046d49e1aeSJan Lentfer  * os_malloc - Allocate dynamic memory
3056d49e1aeSJan Lentfer  * @size: Size of the buffer to allocate
3066d49e1aeSJan Lentfer  * Returns: Allocated buffer or %NULL on failure
3076d49e1aeSJan Lentfer  *
3086d49e1aeSJan Lentfer  * Caller is responsible for freeing the returned buffer with os_free().
3096d49e1aeSJan Lentfer  */
3106d49e1aeSJan Lentfer void * os_malloc(size_t size);
3116d49e1aeSJan Lentfer 
3126d49e1aeSJan Lentfer /**
3136d49e1aeSJan Lentfer  * os_realloc - Re-allocate dynamic memory
3146d49e1aeSJan Lentfer  * @ptr: Old buffer from os_malloc() or os_realloc()
3156d49e1aeSJan Lentfer  * @size: Size of the new buffer
3166d49e1aeSJan Lentfer  * Returns: Allocated buffer or %NULL on failure
3176d49e1aeSJan Lentfer  *
3186d49e1aeSJan Lentfer  * Caller is responsible for freeing the returned buffer with os_free().
3196d49e1aeSJan Lentfer  * If re-allocation fails, %NULL is returned and the original buffer (ptr) is
3206d49e1aeSJan Lentfer  * not freed and caller is still responsible for freeing it.
3216d49e1aeSJan Lentfer  */
3226d49e1aeSJan Lentfer void * os_realloc(void *ptr, size_t size);
3236d49e1aeSJan Lentfer 
3246d49e1aeSJan Lentfer /**
3256d49e1aeSJan Lentfer  * os_free - Free dynamic memory
3266d49e1aeSJan Lentfer  * @ptr: Old buffer from os_malloc() or os_realloc(); can be %NULL
3276d49e1aeSJan Lentfer  */
3286d49e1aeSJan Lentfer void os_free(void *ptr);
3296d49e1aeSJan Lentfer 
3306d49e1aeSJan Lentfer /**
3316d49e1aeSJan Lentfer  * os_memcpy - Copy memory area
3326d49e1aeSJan Lentfer  * @dest: Destination
3336d49e1aeSJan Lentfer  * @src: Source
3346d49e1aeSJan Lentfer  * @n: Number of bytes to copy
3356d49e1aeSJan Lentfer  * Returns: dest
3366d49e1aeSJan Lentfer  *
3376d49e1aeSJan Lentfer  * The memory areas src and dst must not overlap. os_memmove() can be used with
3386d49e1aeSJan Lentfer  * overlapping memory.
3396d49e1aeSJan Lentfer  */
3406d49e1aeSJan Lentfer void * os_memcpy(void *dest, const void *src, size_t n);
3416d49e1aeSJan Lentfer 
3426d49e1aeSJan Lentfer /**
3436d49e1aeSJan Lentfer  * os_memmove - Copy memory area
3446d49e1aeSJan Lentfer  * @dest: Destination
3456d49e1aeSJan Lentfer  * @src: Source
3466d49e1aeSJan Lentfer  * @n: Number of bytes to copy
3476d49e1aeSJan Lentfer  * Returns: dest
3486d49e1aeSJan Lentfer  *
3496d49e1aeSJan Lentfer  * The memory areas src and dst may overlap.
3506d49e1aeSJan Lentfer  */
3516d49e1aeSJan Lentfer void * os_memmove(void *dest, const void *src, size_t n);
3526d49e1aeSJan Lentfer 
3536d49e1aeSJan Lentfer /**
3546d49e1aeSJan Lentfer  * os_memset - Fill memory with a constant byte
3556d49e1aeSJan Lentfer  * @s: Memory area to be filled
3566d49e1aeSJan Lentfer  * @c: Constant byte
3576d49e1aeSJan Lentfer  * @n: Number of bytes started from s to fill with c
3586d49e1aeSJan Lentfer  * Returns: s
3596d49e1aeSJan Lentfer  */
3606d49e1aeSJan Lentfer void * os_memset(void *s, int c, size_t n);
3616d49e1aeSJan Lentfer 
3626d49e1aeSJan Lentfer /**
3636d49e1aeSJan Lentfer  * os_memcmp - Compare memory areas
3646d49e1aeSJan Lentfer  * @s1: First buffer
3656d49e1aeSJan Lentfer  * @s2: Second buffer
3666d49e1aeSJan Lentfer  * @n: Maximum numbers of octets to compare
3676d49e1aeSJan Lentfer  * Returns: An integer less than, equal to, or greater than zero if s1 is
3686d49e1aeSJan Lentfer  * found to be less than, to match, or be greater than s2. Only first n
3696d49e1aeSJan Lentfer  * characters will be compared.
3706d49e1aeSJan Lentfer  */
3716d49e1aeSJan Lentfer int os_memcmp(const void *s1, const void *s2, size_t n);
3726d49e1aeSJan Lentfer 
3736d49e1aeSJan Lentfer /**
3746d49e1aeSJan Lentfer  * os_strdup - Duplicate a string
3756d49e1aeSJan Lentfer  * @s: Source string
3766d49e1aeSJan Lentfer  * Returns: Allocated buffer with the string copied into it or %NULL on failure
3776d49e1aeSJan Lentfer  *
3786d49e1aeSJan Lentfer  * Caller is responsible for freeing the returned buffer with os_free().
3796d49e1aeSJan Lentfer  */
3806d49e1aeSJan Lentfer char * os_strdup(const char *s);
3816d49e1aeSJan Lentfer 
3826d49e1aeSJan Lentfer /**
3836d49e1aeSJan Lentfer  * os_strlen - Calculate the length of a string
3846d49e1aeSJan Lentfer  * @s: '\0' terminated string
3856d49e1aeSJan Lentfer  * Returns: Number of characters in s (not counting the '\0' terminator)
3866d49e1aeSJan Lentfer  */
3876d49e1aeSJan Lentfer size_t os_strlen(const char *s);
3886d49e1aeSJan Lentfer 
3896d49e1aeSJan Lentfer /**
3906d49e1aeSJan Lentfer  * os_strcasecmp - Compare two strings ignoring case
3916d49e1aeSJan Lentfer  * @s1: First string
3926d49e1aeSJan Lentfer  * @s2: Second string
3936d49e1aeSJan Lentfer  * Returns: An integer less than, equal to, or greater than zero if s1 is
3946d49e1aeSJan Lentfer  * found to be less than, to match, or be greatred than s2
3956d49e1aeSJan Lentfer  */
3966d49e1aeSJan Lentfer int os_strcasecmp(const char *s1, const char *s2);
3976d49e1aeSJan Lentfer 
3986d49e1aeSJan Lentfer /**
3996d49e1aeSJan Lentfer  * os_strncasecmp - Compare two strings ignoring case
4006d49e1aeSJan Lentfer  * @s1: First string
4016d49e1aeSJan Lentfer  * @s2: Second string
4026d49e1aeSJan Lentfer  * @n: Maximum numbers of characters to compare
4036d49e1aeSJan Lentfer  * Returns: An integer less than, equal to, or greater than zero if s1 is
4046d49e1aeSJan Lentfer  * found to be less than, to match, or be greater than s2. Only first n
4056d49e1aeSJan Lentfer  * characters will be compared.
4066d49e1aeSJan Lentfer  */
4076d49e1aeSJan Lentfer int os_strncasecmp(const char *s1, const char *s2, size_t n);
4086d49e1aeSJan Lentfer 
4096d49e1aeSJan Lentfer /**
4106d49e1aeSJan Lentfer  * os_strchr - Locate the first occurrence of a character in string
4116d49e1aeSJan Lentfer  * @s: String
4126d49e1aeSJan Lentfer  * @c: Character to search for
4136d49e1aeSJan Lentfer  * Returns: Pointer to the matched character or %NULL if not found
4146d49e1aeSJan Lentfer  */
4156d49e1aeSJan Lentfer char * os_strchr(const char *s, int c);
4166d49e1aeSJan Lentfer 
4176d49e1aeSJan Lentfer /**
4186d49e1aeSJan Lentfer  * os_strrchr - Locate the last occurrence of a character in string
4196d49e1aeSJan Lentfer  * @s: String
4206d49e1aeSJan Lentfer  * @c: Character to search for
4216d49e1aeSJan Lentfer  * Returns: Pointer to the matched character or %NULL if not found
4226d49e1aeSJan Lentfer  */
4236d49e1aeSJan Lentfer char * os_strrchr(const char *s, int c);
4246d49e1aeSJan Lentfer 
4256d49e1aeSJan Lentfer /**
4266d49e1aeSJan Lentfer  * os_strcmp - Compare two strings
4276d49e1aeSJan Lentfer  * @s1: First string
4286d49e1aeSJan Lentfer  * @s2: Second string
4296d49e1aeSJan Lentfer  * Returns: An integer less than, equal to, or greater than zero if s1 is
4306d49e1aeSJan Lentfer  * found to be less than, to match, or be greatred than s2
4316d49e1aeSJan Lentfer  */
4326d49e1aeSJan Lentfer int os_strcmp(const char *s1, const char *s2);
4336d49e1aeSJan Lentfer 
4346d49e1aeSJan Lentfer /**
4356d49e1aeSJan Lentfer  * os_strncmp - Compare two strings
4366d49e1aeSJan Lentfer  * @s1: First string
4376d49e1aeSJan Lentfer  * @s2: Second string
4386d49e1aeSJan Lentfer  * @n: Maximum numbers of characters to compare
4396d49e1aeSJan Lentfer  * Returns: An integer less than, equal to, or greater than zero if s1 is
4406d49e1aeSJan Lentfer  * found to be less than, to match, or be greater than s2. Only first n
4416d49e1aeSJan Lentfer  * characters will be compared.
4426d49e1aeSJan Lentfer  */
4436d49e1aeSJan Lentfer int os_strncmp(const char *s1, const char *s2, size_t n);
4446d49e1aeSJan Lentfer 
4456d49e1aeSJan Lentfer /**
4466d49e1aeSJan Lentfer  * os_strstr - Locate a substring
4476d49e1aeSJan Lentfer  * @haystack: String (haystack) to search from
4486d49e1aeSJan Lentfer  * @needle: Needle to search from haystack
4496d49e1aeSJan Lentfer  * Returns: Pointer to the beginning of the substring or %NULL if not found
4506d49e1aeSJan Lentfer  */
4516d49e1aeSJan Lentfer char * os_strstr(const char *haystack, const char *needle);
4526d49e1aeSJan Lentfer 
4536d49e1aeSJan Lentfer /**
4546d49e1aeSJan Lentfer  * os_snprintf - Print to a memory buffer
4556d49e1aeSJan Lentfer  * @str: Memory buffer to print into
4566d49e1aeSJan Lentfer  * @size: Maximum length of the str buffer
4576d49e1aeSJan Lentfer  * @format: printf format
4586d49e1aeSJan Lentfer  * Returns: Number of characters printed (not including trailing '\0').
4596d49e1aeSJan Lentfer  *
4606d49e1aeSJan Lentfer  * If the output buffer is truncated, number of characters which would have
4616d49e1aeSJan Lentfer  * been written is returned. Since some C libraries return -1 in such a case,
4626d49e1aeSJan Lentfer  * the caller must be prepared on that value, too, to indicate truncation.
4636d49e1aeSJan Lentfer  *
4646d49e1aeSJan Lentfer  * Note: Some C library implementations of snprintf() may not guarantee null
4656d49e1aeSJan Lentfer  * termination in case the output is truncated. The OS wrapper function of
4666d49e1aeSJan Lentfer  * os_snprintf() should provide this guarantee, i.e., to null terminate the
4676d49e1aeSJan Lentfer  * output buffer if a C library version of the function is used and if that
4686d49e1aeSJan Lentfer  * function does not guarantee null termination.
4696d49e1aeSJan Lentfer  *
4706d49e1aeSJan Lentfer  * If the target system does not include snprintf(), see, e.g.,
4716d49e1aeSJan Lentfer  * http://www.ijs.si/software/snprintf/ for an example of a portable
4726d49e1aeSJan Lentfer  * implementation of snprintf.
4736d49e1aeSJan Lentfer  */
4746d49e1aeSJan Lentfer int os_snprintf(char *str, size_t size, const char *format, ...);
4756d49e1aeSJan Lentfer 
4766d49e1aeSJan Lentfer #else /* OS_NO_C_LIB_DEFINES */
4776d49e1aeSJan Lentfer 
4783ff40c12SJohn Marino #ifdef WPA_TRACE
4793ff40c12SJohn Marino void * os_malloc(size_t size);
4803ff40c12SJohn Marino void * os_realloc(void *ptr, size_t size);
4813ff40c12SJohn Marino void os_free(void *ptr);
4823ff40c12SJohn Marino char * os_strdup(const char *s);
4833ff40c12SJohn Marino #else /* WPA_TRACE */
4846d49e1aeSJan Lentfer #ifndef os_malloc
4856d49e1aeSJan Lentfer #define os_malloc(s) malloc((s))
4866d49e1aeSJan Lentfer #endif
4876d49e1aeSJan Lentfer #ifndef os_realloc
4886d49e1aeSJan Lentfer #define os_realloc(p, s) realloc((p), (s))
4896d49e1aeSJan Lentfer #endif
4906d49e1aeSJan Lentfer #ifndef os_free
4916d49e1aeSJan Lentfer #define os_free(p) free((p))
4926d49e1aeSJan Lentfer #endif
4933ff40c12SJohn Marino #ifndef os_strdup
4943ff40c12SJohn Marino #ifdef _MSC_VER
4953ff40c12SJohn Marino #define os_strdup(s) _strdup(s)
4963ff40c12SJohn Marino #else
4973ff40c12SJohn Marino #define os_strdup(s) strdup(s)
4983ff40c12SJohn Marino #endif
4993ff40c12SJohn Marino #endif
5003ff40c12SJohn Marino #endif /* WPA_TRACE */
5016d49e1aeSJan Lentfer 
5026d49e1aeSJan Lentfer #ifndef os_memcpy
5036d49e1aeSJan Lentfer #define os_memcpy(d, s, n) memcpy((d), (s), (n))
5046d49e1aeSJan Lentfer #endif
5056d49e1aeSJan Lentfer #ifndef os_memmove
5066d49e1aeSJan Lentfer #define os_memmove(d, s, n) memmove((d), (s), (n))
5076d49e1aeSJan Lentfer #endif
5086d49e1aeSJan Lentfer #ifndef os_memset
5096d49e1aeSJan Lentfer #define os_memset(s, c, n) memset(s, c, n)
5106d49e1aeSJan Lentfer #endif
5116d49e1aeSJan Lentfer #ifndef os_memcmp
5126d49e1aeSJan Lentfer #define os_memcmp(s1, s2, n) memcmp((s1), (s2), (n))
5136d49e1aeSJan Lentfer #endif
5146d49e1aeSJan Lentfer 
5156d49e1aeSJan Lentfer #ifndef os_strlen
5166d49e1aeSJan Lentfer #define os_strlen(s) strlen(s)
5176d49e1aeSJan Lentfer #endif
5186d49e1aeSJan Lentfer #ifndef os_strcasecmp
5196d49e1aeSJan Lentfer #ifdef _MSC_VER
5206d49e1aeSJan Lentfer #define os_strcasecmp(s1, s2) _stricmp((s1), (s2))
5216d49e1aeSJan Lentfer #else
5226d49e1aeSJan Lentfer #define os_strcasecmp(s1, s2) strcasecmp((s1), (s2))
5236d49e1aeSJan Lentfer #endif
5246d49e1aeSJan Lentfer #endif
5256d49e1aeSJan Lentfer #ifndef os_strncasecmp
5266d49e1aeSJan Lentfer #ifdef _MSC_VER
5276d49e1aeSJan Lentfer #define os_strncasecmp(s1, s2, n) _strnicmp((s1), (s2), (n))
5286d49e1aeSJan Lentfer #else
5296d49e1aeSJan Lentfer #define os_strncasecmp(s1, s2, n) strncasecmp((s1), (s2), (n))
5306d49e1aeSJan Lentfer #endif
5316d49e1aeSJan Lentfer #endif
5326d49e1aeSJan Lentfer #ifndef os_strchr
5336d49e1aeSJan Lentfer #define os_strchr(s, c) strchr((s), (c))
5346d49e1aeSJan Lentfer #endif
5356d49e1aeSJan Lentfer #ifndef os_strcmp
5366d49e1aeSJan Lentfer #define os_strcmp(s1, s2) strcmp((s1), (s2))
5376d49e1aeSJan Lentfer #endif
5386d49e1aeSJan Lentfer #ifndef os_strncmp
5396d49e1aeSJan Lentfer #define os_strncmp(s1, s2, n) strncmp((s1), (s2), (n))
5406d49e1aeSJan Lentfer #endif
5416d49e1aeSJan Lentfer #ifndef os_strrchr
5426d49e1aeSJan Lentfer #define os_strrchr(s, c) strrchr((s), (c))
5436d49e1aeSJan Lentfer #endif
5446d49e1aeSJan Lentfer #ifndef os_strstr
5456d49e1aeSJan Lentfer #define os_strstr(h, n) strstr((h), (n))
5466d49e1aeSJan Lentfer #endif
5476d49e1aeSJan Lentfer 
5486d49e1aeSJan Lentfer #ifndef os_snprintf
5496d49e1aeSJan Lentfer #ifdef _MSC_VER
5506d49e1aeSJan Lentfer #define os_snprintf _snprintf
5516d49e1aeSJan Lentfer #else
5526d49e1aeSJan Lentfer #define os_snprintf snprintf
5536d49e1aeSJan Lentfer #endif
5546d49e1aeSJan Lentfer #endif
5556d49e1aeSJan Lentfer 
5566d49e1aeSJan Lentfer #endif /* OS_NO_C_LIB_DEFINES */
5576d49e1aeSJan Lentfer 
5586d49e1aeSJan Lentfer 
os_snprintf_error(size_t size,int res)559*a1157835SDaniel Fojt static inline int os_snprintf_error(size_t size, int res)
560*a1157835SDaniel Fojt {
561*a1157835SDaniel Fojt 	return res < 0 || (unsigned int) res >= size;
562*a1157835SDaniel Fojt }
563*a1157835SDaniel Fojt 
564*a1157835SDaniel Fojt 
os_realloc_array(void * ptr,size_t nmemb,size_t size)5653ff40c12SJohn Marino static inline void * os_realloc_array(void *ptr, size_t nmemb, size_t size)
5663ff40c12SJohn Marino {
5673ff40c12SJohn Marino 	if (size && nmemb > (~(size_t) 0) / size)
5683ff40c12SJohn Marino 		return NULL;
5693ff40c12SJohn Marino 	return os_realloc(ptr, nmemb * size);
5703ff40c12SJohn Marino }
5713ff40c12SJohn Marino 
572*a1157835SDaniel Fojt /**
573*a1157835SDaniel Fojt  * os_remove_in_array - Remove a member from an array by index
574*a1157835SDaniel Fojt  * @ptr: Pointer to the array
575*a1157835SDaniel Fojt  * @nmemb: Current member count of the array
576*a1157835SDaniel Fojt  * @size: The size per member of the array
577*a1157835SDaniel Fojt  * @idx: Index of the member to be removed
578*a1157835SDaniel Fojt  */
os_remove_in_array(void * ptr,size_t nmemb,size_t size,size_t idx)579*a1157835SDaniel Fojt static inline void os_remove_in_array(void *ptr, size_t nmemb, size_t size,
580*a1157835SDaniel Fojt 				      size_t idx)
581*a1157835SDaniel Fojt {
582*a1157835SDaniel Fojt 	if (idx < nmemb - 1)
583*a1157835SDaniel Fojt 		os_memmove(((unsigned char *) ptr) + idx * size,
584*a1157835SDaniel Fojt 			   ((unsigned char *) ptr) + (idx + 1) * size,
585*a1157835SDaniel Fojt 			   (nmemb - idx - 1) * size);
586*a1157835SDaniel Fojt }
5873ff40c12SJohn Marino 
5886d49e1aeSJan Lentfer /**
5896d49e1aeSJan Lentfer  * os_strlcpy - Copy a string with size bound and NUL-termination
5906d49e1aeSJan Lentfer  * @dest: Destination
5916d49e1aeSJan Lentfer  * @src: Source
5926d49e1aeSJan Lentfer  * @siz: Size of the target buffer
5936d49e1aeSJan Lentfer  * Returns: Total length of the target string (length of src) (not including
5946d49e1aeSJan Lentfer  * NUL-termination)
5956d49e1aeSJan Lentfer  *
5966d49e1aeSJan Lentfer  * This function matches in behavior with the strlcpy(3) function in OpenBSD.
5976d49e1aeSJan Lentfer  */
5986d49e1aeSJan Lentfer size_t os_strlcpy(char *dest, const char *src, size_t siz);
5996d49e1aeSJan Lentfer 
600*a1157835SDaniel Fojt /**
601*a1157835SDaniel Fojt  * os_memcmp_const - Constant time memory comparison
602*a1157835SDaniel Fojt  * @a: First buffer to compare
603*a1157835SDaniel Fojt  * @b: Second buffer to compare
604*a1157835SDaniel Fojt  * @len: Number of octets to compare
605*a1157835SDaniel Fojt  * Returns: 0 if buffers are equal, non-zero if not
606*a1157835SDaniel Fojt  *
607*a1157835SDaniel Fojt  * This function is meant for comparing passwords or hash values where
608*a1157835SDaniel Fojt  * difference in execution time could provide external observer information
609*a1157835SDaniel Fojt  * about the location of the difference in the memory buffers. The return value
610*a1157835SDaniel Fojt  * does not behave like os_memcmp(), i.e., os_memcmp_const() cannot be used to
611*a1157835SDaniel Fojt  * sort items into a defined order. Unlike os_memcmp(), execution time of
612*a1157835SDaniel Fojt  * os_memcmp_const() does not depend on the contents of the compared memory
613*a1157835SDaniel Fojt  * buffers, but only on the total compared length.
614*a1157835SDaniel Fojt  */
615*a1157835SDaniel Fojt int os_memcmp_const(const void *a, const void *b, size_t len);
616*a1157835SDaniel Fojt 
617*a1157835SDaniel Fojt 
618*a1157835SDaniel Fojt /**
619*a1157835SDaniel Fojt  * os_memdup - Allocate duplicate of passed memory chunk
620*a1157835SDaniel Fojt  * @src: Source buffer to duplicate
621*a1157835SDaniel Fojt  * @len: Length of source buffer
622*a1157835SDaniel Fojt  * Returns: %NULL if allocation failed, copy of src buffer otherwise
623*a1157835SDaniel Fojt  *
624*a1157835SDaniel Fojt  * This function allocates a memory block like os_malloc() would, and
625*a1157835SDaniel Fojt  * copies the given source buffer into it.
626*a1157835SDaniel Fojt  */
627*a1157835SDaniel Fojt void * os_memdup(const void *src, size_t len);
628*a1157835SDaniel Fojt 
629*a1157835SDaniel Fojt /**
630*a1157835SDaniel Fojt  * os_exec - Execute an external program
631*a1157835SDaniel Fojt  * @program: Path to the program
632*a1157835SDaniel Fojt  * @arg: Command line argument string
633*a1157835SDaniel Fojt  * @wait_completion: Whether to wait until the program execution completes
634*a1157835SDaniel Fojt  * Returns: 0 on success, -1 on error
635*a1157835SDaniel Fojt  */
636*a1157835SDaniel Fojt int os_exec(const char *program, const char *arg, int wait_completion);
637*a1157835SDaniel Fojt 
6386d49e1aeSJan Lentfer 
6396d49e1aeSJan Lentfer #ifdef OS_REJECT_C_LIB_FUNCTIONS
6406d49e1aeSJan Lentfer #define malloc OS_DO_NOT_USE_malloc
6416d49e1aeSJan Lentfer #define realloc OS_DO_NOT_USE_realloc
6426d49e1aeSJan Lentfer #define free OS_DO_NOT_USE_free
6436d49e1aeSJan Lentfer #define memcpy OS_DO_NOT_USE_memcpy
6446d49e1aeSJan Lentfer #define memmove OS_DO_NOT_USE_memmove
6456d49e1aeSJan Lentfer #define memset OS_DO_NOT_USE_memset
6466d49e1aeSJan Lentfer #define memcmp OS_DO_NOT_USE_memcmp
6476d49e1aeSJan Lentfer #undef strdup
6486d49e1aeSJan Lentfer #define strdup OS_DO_NOT_USE_strdup
6496d49e1aeSJan Lentfer #define strlen OS_DO_NOT_USE_strlen
6506d49e1aeSJan Lentfer #define strcasecmp OS_DO_NOT_USE_strcasecmp
6516d49e1aeSJan Lentfer #define strncasecmp OS_DO_NOT_USE_strncasecmp
6526d49e1aeSJan Lentfer #undef strchr
6536d49e1aeSJan Lentfer #define strchr OS_DO_NOT_USE_strchr
6546d49e1aeSJan Lentfer #undef strcmp
6556d49e1aeSJan Lentfer #define strcmp OS_DO_NOT_USE_strcmp
6566d49e1aeSJan Lentfer #undef strncmp
6576d49e1aeSJan Lentfer #define strncmp OS_DO_NOT_USE_strncmp
6586d49e1aeSJan Lentfer #undef strncpy
6596d49e1aeSJan Lentfer #define strncpy OS_DO_NOT_USE_strncpy
6606d49e1aeSJan Lentfer #define strrchr OS_DO_NOT_USE_strrchr
6616d49e1aeSJan Lentfer #define strstr OS_DO_NOT_USE_strstr
6626d49e1aeSJan Lentfer #undef snprintf
6636d49e1aeSJan Lentfer #define snprintf OS_DO_NOT_USE_snprintf
6646d49e1aeSJan Lentfer 
6656d49e1aeSJan Lentfer #define strcpy OS_DO_NOT_USE_strcpy
6666d49e1aeSJan Lentfer #endif /* OS_REJECT_C_LIB_FUNCTIONS */
6676d49e1aeSJan Lentfer 
668*a1157835SDaniel Fojt 
669*a1157835SDaniel Fojt #if defined(WPA_TRACE_BFD) && defined(CONFIG_TESTING_OPTIONS)
670*a1157835SDaniel Fojt #define TEST_FAIL() testing_test_fail()
671*a1157835SDaniel Fojt int testing_test_fail(void);
672*a1157835SDaniel Fojt extern char wpa_trace_fail_func[256];
673*a1157835SDaniel Fojt extern unsigned int wpa_trace_fail_after;
674*a1157835SDaniel Fojt extern char wpa_trace_test_fail_func[256];
675*a1157835SDaniel Fojt extern unsigned int wpa_trace_test_fail_after;
676*a1157835SDaniel Fojt #else
677*a1157835SDaniel Fojt #define TEST_FAIL() 0
678*a1157835SDaniel Fojt #endif
679*a1157835SDaniel Fojt 
6806d49e1aeSJan Lentfer #endif /* OS_H */
681