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