2 * OS specific functions
3 * Copyright (c) 2005-2009, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 typedef long os_time_t;
15 * os_sleep - Sleep (sec, usec)
16 * @sec: Number of seconds to sleep
17 * @usec: Number of microseconds to sleep
19 void os_sleep(os_time_t sec, os_time_t usec);
32 * os_get_time - Get current time (sec, usec)
33 * @t: Pointer to buffer for the time
34 * Returns: 0 on success, -1 on failure
36 int os_get_time(struct os_time *t);
39 * os_get_reltime - Get relative time (sec, usec)
40 * @t: Pointer to buffer for the time
41 * Returns: 0 on success, -1 on failure
43 int os_get_reltime(struct os_reltime *t);
46 /* Helpers for handling struct os_time */
48 static inline int os_time_before(struct os_time *a, struct os_time *b)
50 return (a->sec < b->sec) ||
51 (a->sec == b->sec && a->usec < b->usec);
55 static inline void os_time_sub(struct os_time *a, struct os_time *b,
58 res->sec = a->sec - b->sec;
59 res->usec = a->usec - b->usec;
67 /* Helpers for handling struct os_reltime */
69 static inline int os_reltime_before(struct os_reltime *a,
72 return (a->sec < b->sec) ||
73 (a->sec == b->sec && a->usec < b->usec);
77 static inline void os_reltime_sub(struct os_reltime *a, struct os_reltime *b,
78 struct os_reltime *res)
80 res->sec = a->sec - b->sec;
81 res->usec = a->usec - b->usec;
89 static inline void os_reltime_age(struct os_reltime *start,
90 struct os_reltime *age)
92 struct os_reltime now;
95 os_reltime_sub(&now, start, age);
99 static inline int os_reltime_expired(struct os_reltime *now,
100 struct os_reltime *ts,
101 os_time_t timeout_secs)
103 struct os_reltime age;
105 os_reltime_sub(now, ts, &age);
106 return (age.sec > timeout_secs) ||
107 (age.sec == timeout_secs && age.usec > 0);
111 static inline int os_reltime_initialized(struct os_reltime *t)
113 return t->sec != 0 || t->usec != 0;
118 * os_mktime - Convert broken-down time into seconds since 1970-01-01
119 * @year: Four digit year
120 * @month: Month (1 .. 12)
121 * @day: Day of month (1 .. 31)
122 * @hour: Hour (0 .. 23)
123 * @min: Minute (0 .. 59)
124 * @sec: Second (0 .. 60)
125 * @t: Buffer for returning calendar time representation (seconds since
126 * 1970-01-01 00:00:00)
127 * Returns: 0 on success, -1 on failure
129 * Note: The result is in seconds from Epoch, i.e., in UTC, not in local time
130 * which is used by POSIX mktime().
132 int os_mktime(int year, int month, int day, int hour, int min, int sec,
136 int sec; /* 0..59 or 60 for leap seconds */
138 int hour; /* 0..23 */
140 int month; /* 1..12 */
141 int year; /* Four digit year */
144 int os_gmtime(os_time_t t, struct os_tm *tm);
147 * os_daemonize - Run in the background (detach from the controlling terminal)
148 * @pid_file: File name to write the process ID to or %NULL to skip this
149 * Returns: 0 on success, -1 on failure
151 int os_daemonize(const char *pid_file);
154 * os_daemonize_terminate - Stop running in the background (remove pid file)
155 * @pid_file: File name to write the process ID to or %NULL to skip this
157 void os_daemonize_terminate(const char *pid_file);
160 * os_get_random - Get cryptographically strong pseudo random data
161 * @buf: Buffer for pseudo random data
162 * @len: Length of the buffer
163 * Returns: 0 on success, -1 on failure
165 int os_get_random(unsigned char *buf, size_t len);
168 * os_random - Get pseudo random value (not necessarily very strong)
169 * Returns: Pseudo random value
171 unsigned long os_random(void);
174 * os_rel2abs_path - Get an absolute path for a file
175 * @rel_path: Relative path to a file
176 * Returns: Absolute path for the file or %NULL on failure
178 * This function tries to convert a relative path of a file to an absolute path
179 * in order for the file to be found even if current working directory has
180 * changed. The returned value is allocated and caller is responsible for
181 * freeing it. It is acceptable to just return the same path in an allocated
182 * buffer, e.g., return strdup(rel_path). This function is only used to find
183 * configuration files when os_daemonize() may have changed the current working
184 * directory and relative path would be pointing to a different location.
186 char * os_rel2abs_path(const char *rel_path);
189 * os_program_init - Program initialization (called at start)
190 * Returns: 0 on success, -1 on failure
192 * This function is called when a programs starts. If there are any OS specific
193 * processing that is needed, it can be placed here. It is also acceptable to
194 * just return 0 if not special processing is needed.
196 int os_program_init(void);
199 * os_program_deinit - Program deinitialization (called just before exit)
201 * This function is called just before a program exists. If there are any OS
202 * specific processing, e.g., freeing resourced allocated in os_program_init(),
203 * it should be done here. It is also acceptable for this function to do
206 void os_program_deinit(void);
209 * os_setenv - Set environment variable
210 * @name: Name of the variable
211 * @value: Value to set to the variable
212 * @overwrite: Whether existing variable should be overwritten
213 * Returns: 0 on success, -1 on error
215 * This function is only used for wpa_cli action scripts. OS wrapper does not
216 * need to implement this if such functionality is not needed.
218 int os_setenv(const char *name, const char *value, int overwrite);
221 * os_unsetenv - Delete environent variable
222 * @name: Name of the variable
223 * Returns: 0 on success, -1 on error
225 * This function is only used for wpa_cli action scripts. OS wrapper does not
226 * need to implement this if such functionality is not needed.
228 int os_unsetenv(const char *name);
231 * os_readfile - Read a file to an allocated memory buffer
232 * @name: Name of the file to read
233 * @len: For returning the length of the allocated buffer
234 * Returns: Pointer to the allocated buffer or %NULL on failure
236 * This function allocates memory and reads the given file to this buffer. Both
237 * binary and text files can be read with this function. The caller is
238 * responsible for freeing the returned buffer with os_free().
240 char * os_readfile(const char *name, size_t *len);
243 * os_file_exists - Check whether the specified file exists
244 * @fname: Path and name of the file
245 * Returns: 1 if the file exists or 0 if not
247 int os_file_exists(const char *fname);
250 * os_zalloc - Allocate and zero memory
251 * @size: Number of bytes to allocate
252 * Returns: Pointer to allocated and zeroed memory or %NULL on failure
254 * Caller is responsible for freeing the returned buffer with os_free().
256 void * os_zalloc(size_t size);
259 * os_calloc - Allocate and zero memory for an array
260 * @nmemb: Number of members in the array
261 * @size: Number of bytes in each member
262 * Returns: Pointer to allocated and zeroed memory or %NULL on failure
264 * This function can be used as a wrapper for os_zalloc(nmemb * size) when an
265 * allocation is used for an array. The main benefit over os_zalloc() is in
266 * having an extra check to catch integer overflows in multiplication.
268 * Caller is responsible for freeing the returned buffer with os_free().
270 static inline void * os_calloc(size_t nmemb, size_t size)
272 if (size && nmemb > (~(size_t) 0) / size)
274 return os_zalloc(nmemb * size);
279 * The following functions are wrapper for standard ANSI C or POSIX functions.
280 * By default, they are just defined to use the standard function name and no
281 * os_*.c implementation is needed for them. This avoids extra function calls
282 * by allowing the C pre-processor take care of the function name mapping.
284 * If the target system uses a C library that does not provide these functions,
285 * build_config.h can be used to define the wrappers to use a different
286 * function name. This can be done on function-by-function basis since the
287 * defines here are only used if build_config.h does not define the os_* name.
288 * If needed, os_*.c file can be used to implement the functions that are not
289 * included in the C library on the target system. Alternatively,
290 * OS_NO_C_LIB_DEFINES can be defined to skip all defines here in which case
291 * these functions need to be implemented in os_*.c file for the target system.
294 #ifdef OS_NO_C_LIB_DEFINES
297 * os_malloc - Allocate dynamic memory
298 * @size: Size of the buffer to allocate
299 * Returns: Allocated buffer or %NULL on failure
301 * Caller is responsible for freeing the returned buffer with os_free().
303 void * os_malloc(size_t size);
306 * os_realloc - Re-allocate dynamic memory
307 * @ptr: Old buffer from os_malloc() or os_realloc()
308 * @size: Size of the new buffer
309 * Returns: Allocated buffer or %NULL on failure
311 * Caller is responsible for freeing the returned buffer with os_free().
312 * If re-allocation fails, %NULL is returned and the original buffer (ptr) is
313 * not freed and caller is still responsible for freeing it.
315 void * os_realloc(void *ptr, size_t size);
318 * os_free - Free dynamic memory
319 * @ptr: Old buffer from os_malloc() or os_realloc(); can be %NULL
321 void os_free(void *ptr);
324 * os_memcpy - Copy memory area
327 * @n: Number of bytes to copy
330 * The memory areas src and dst must not overlap. os_memmove() can be used with
331 * overlapping memory.
333 void * os_memcpy(void *dest, const void *src, size_t n);
336 * os_memmove - Copy memory area
339 * @n: Number of bytes to copy
342 * The memory areas src and dst may overlap.
344 void * os_memmove(void *dest, const void *src, size_t n);
347 * os_memset - Fill memory with a constant byte
348 * @s: Memory area to be filled
350 * @n: Number of bytes started from s to fill with c
353 void * os_memset(void *s, int c, size_t n);
356 * os_memcmp - Compare memory areas
359 * @n: Maximum numbers of octets to compare
360 * Returns: An integer less than, equal to, or greater than zero if s1 is
361 * found to be less than, to match, or be greater than s2. Only first n
362 * characters will be compared.
364 int os_memcmp(const void *s1, const void *s2, size_t n);
367 * os_strdup - Duplicate a string
369 * Returns: Allocated buffer with the string copied into it or %NULL on failure
371 * Caller is responsible for freeing the returned buffer with os_free().
373 char * os_strdup(const char *s);
376 * os_strlen - Calculate the length of a string
377 * @s: '\0' terminated string
378 * Returns: Number of characters in s (not counting the '\0' terminator)
380 size_t os_strlen(const char *s);
383 * os_strcasecmp - Compare two strings ignoring case
386 * Returns: An integer less than, equal to, or greater than zero if s1 is
387 * found to be less than, to match, or be greatred than s2
389 int os_strcasecmp(const char *s1, const char *s2);
392 * os_strncasecmp - Compare two strings ignoring case
395 * @n: Maximum numbers of characters to compare
396 * Returns: An integer less than, equal to, or greater than zero if s1 is
397 * found to be less than, to match, or be greater than s2. Only first n
398 * characters will be compared.
400 int os_strncasecmp(const char *s1, const char *s2, size_t n);
403 * os_strchr - Locate the first occurrence of a character in string
405 * @c: Character to search for
406 * Returns: Pointer to the matched character or %NULL if not found
408 char * os_strchr(const char *s, int c);
411 * os_strrchr - Locate the last occurrence of a character in string
413 * @c: Character to search for
414 * Returns: Pointer to the matched character or %NULL if not found
416 char * os_strrchr(const char *s, int c);
419 * os_strcmp - Compare two strings
422 * Returns: An integer less than, equal to, or greater than zero if s1 is
423 * found to be less than, to match, or be greatred than s2
425 int os_strcmp(const char *s1, const char *s2);
428 * os_strncmp - Compare two strings
431 * @n: Maximum numbers of characters to compare
432 * Returns: An integer less than, equal to, or greater than zero if s1 is
433 * found to be less than, to match, or be greater than s2. Only first n
434 * characters will be compared.
436 int os_strncmp(const char *s1, const char *s2, size_t n);
439 * os_strstr - Locate a substring
440 * @haystack: String (haystack) to search from
441 * @needle: Needle to search from haystack
442 * Returns: Pointer to the beginning of the substring or %NULL if not found
444 char * os_strstr(const char *haystack, const char *needle);
447 * os_snprintf - Print to a memory buffer
448 * @str: Memory buffer to print into
449 * @size: Maximum length of the str buffer
450 * @format: printf format
451 * Returns: Number of characters printed (not including trailing '\0').
453 * If the output buffer is truncated, number of characters which would have
454 * been written is returned. Since some C libraries return -1 in such a case,
455 * the caller must be prepared on that value, too, to indicate truncation.
457 * Note: Some C library implementations of snprintf() may not guarantee null
458 * termination in case the output is truncated. The OS wrapper function of
459 * os_snprintf() should provide this guarantee, i.e., to null terminate the
460 * output buffer if a C library version of the function is used and if that
461 * function does not guarantee null termination.
463 * If the target system does not include snprintf(), see, e.g.,
464 * http://www.ijs.si/software/snprintf/ for an example of a portable
465 * implementation of snprintf.
467 int os_snprintf(char *str, size_t size, const char *format, ...);
469 #else /* OS_NO_C_LIB_DEFINES */
472 void * os_malloc(size_t size);
473 void * os_realloc(void *ptr, size_t size);
474 void os_free(void *ptr);
475 char * os_strdup(const char *s);
476 #else /* WPA_TRACE */
478 #define os_malloc(s) malloc((s))
481 #define os_realloc(p, s) realloc((p), (s))
484 #define os_free(p) free((p))
488 #define os_strdup(s) _strdup(s)
490 #define os_strdup(s) strdup(s)
493 #endif /* WPA_TRACE */
496 #define os_memcpy(d, s, n) memcpy((d), (s), (n))
499 #define os_memmove(d, s, n) memmove((d), (s), (n))
502 #define os_memset(s, c, n) memset(s, c, n)
505 #define os_memcmp(s1, s2, n) memcmp((s1), (s2), (n))
509 #define os_strlen(s) strlen(s)
511 #ifndef os_strcasecmp
513 #define os_strcasecmp(s1, s2) _stricmp((s1), (s2))
515 #define os_strcasecmp(s1, s2) strcasecmp((s1), (s2))
518 #ifndef os_strncasecmp
520 #define os_strncasecmp(s1, s2, n) _strnicmp((s1), (s2), (n))
522 #define os_strncasecmp(s1, s2, n) strncasecmp((s1), (s2), (n))
526 #define os_strchr(s, c) strchr((s), (c))
529 #define os_strcmp(s1, s2) strcmp((s1), (s2))
532 #define os_strncmp(s1, s2, n) strncmp((s1), (s2), (n))
535 #define os_strrchr(s, c) strrchr((s), (c))
538 #define os_strstr(h, n) strstr((h), (n))
543 #define os_snprintf _snprintf
545 #define os_snprintf snprintf
549 #endif /* OS_NO_C_LIB_DEFINES */
552 static inline int os_snprintf_error(size_t size, int res)
554 return res < 0 || (unsigned int) res >= size;
558 static inline void * os_realloc_array(void *ptr, size_t nmemb, size_t size)
560 if (size && nmemb > (~(size_t) 0) / size)
562 return os_realloc(ptr, nmemb * size);
566 * os_remove_in_array - Remove a member from an array by index
567 * @ptr: Pointer to the array
568 * @nmemb: Current member count of the array
569 * @size: The size per member of the array
570 * @idx: Index of the member to be removed
572 static inline void os_remove_in_array(void *ptr, size_t nmemb, size_t size,
576 os_memmove(((unsigned char *) ptr) + idx * size,
577 ((unsigned char *) ptr) + (idx + 1) * size,
578 (nmemb - idx - 1) * size);
582 * os_strlcpy - Copy a string with size bound and NUL-termination
585 * @siz: Size of the target buffer
586 * Returns: Total length of the target string (length of src) (not including
589 * This function matches in behavior with the strlcpy(3) function in OpenBSD.
591 size_t os_strlcpy(char *dest, const char *src, size_t siz);
594 * os_memcmp_const - Constant time memory comparison
595 * @a: First buffer to compare
596 * @b: Second buffer to compare
597 * @len: Number of octets to compare
598 * Returns: 0 if buffers are equal, non-zero if not
600 * This function is meant for comparing passwords or hash values where
601 * difference in execution time could provide external observer information
602 * about the location of the difference in the memory buffers. The return value
603 * does not behave like os_memcmp(), i.e., os_memcmp_const() cannot be used to
604 * sort items into a defined order. Unlike os_memcmp(), execution time of
605 * os_memcmp_const() does not depend on the contents of the compared memory
606 * buffers, but only on the total compared length.
608 int os_memcmp_const(const void *a, const void *b, size_t len);
611 * os_exec - Execute an external program
612 * @program: Path to the program
613 * @arg: Command line argument string
614 * @wait_completion: Whether to wait until the program execution completes
615 * Returns: 0 on success, -1 on error
617 int os_exec(const char *program, const char *arg, int wait_completion);
620 #ifdef OS_REJECT_C_LIB_FUNCTIONS
621 #define malloc OS_DO_NOT_USE_malloc
622 #define realloc OS_DO_NOT_USE_realloc
623 #define free OS_DO_NOT_USE_free
624 #define memcpy OS_DO_NOT_USE_memcpy
625 #define memmove OS_DO_NOT_USE_memmove
626 #define memset OS_DO_NOT_USE_memset
627 #define memcmp OS_DO_NOT_USE_memcmp
629 #define strdup OS_DO_NOT_USE_strdup
630 #define strlen OS_DO_NOT_USE_strlen
631 #define strcasecmp OS_DO_NOT_USE_strcasecmp
632 #define strncasecmp OS_DO_NOT_USE_strncasecmp
634 #define strchr OS_DO_NOT_USE_strchr
636 #define strcmp OS_DO_NOT_USE_strcmp
638 #define strncmp OS_DO_NOT_USE_strncmp
640 #define strncpy OS_DO_NOT_USE_strncpy
641 #define strrchr OS_DO_NOT_USE_strrchr
642 #define strstr OS_DO_NOT_USE_strstr
644 #define snprintf OS_DO_NOT_USE_snprintf
646 #define strcpy OS_DO_NOT_USE_strcpy
647 #endif /* OS_REJECT_C_LIB_FUNCTIONS */