Disconnect hostapd from building in base
[dragonfly.git] / contrib / hostapd / src / utils / os.h
CommitLineData
ebfa2275 1/*
4781064b
JM
2 * OS specific functions
3 * Copyright (c) 2005-2009, Jouni Malinen <j@w1.fi>
ebfa2275 4 *
4781064b
JM
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
ebfa2275
SZ
7 */
8
9#ifndef OS_H
10#define OS_H
11
12typedef 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 */
19void os_sleep(os_time_t sec, os_time_t usec);
20
21struct os_time {
22 os_time_t sec;
23 os_time_t usec;
24};
25
4781064b
JM
26struct os_reltime {
27 os_time_t sec;
28 os_time_t usec;
29};
30
ebfa2275
SZ
31/**
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
35 */
36int os_get_time(struct os_time *t);
37
4781064b
JM
38/**
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
42 */
43int os_get_reltime(struct os_reltime *t);
44
45
46/* Helpers for handling struct os_time */
47
48static inline int os_time_before(struct os_time *a, struct os_time *b)
49{
50 return (a->sec < b->sec) ||
51 (a->sec == b->sec && a->usec < b->usec);
52}
53
54
55static inline void os_time_sub(struct os_time *a, struct os_time *b,
56 struct os_time *res)
57{
58 res->sec = a->sec - b->sec;
59 res->usec = a->usec - b->usec;
60 if (res->usec < 0) {
61 res->sec--;
62 res->usec += 1000000;
63 }
64}
65
ebfa2275 66
4781064b 67/* Helpers for handling struct os_reltime */
ebfa2275 68
4781064b
JM
69static inline int os_reltime_before(struct os_reltime *a,
70 struct os_reltime *b)
71{
72 return (a->sec < b->sec) ||
73 (a->sec == b->sec && a->usec < b->usec);
74}
75
76
77static inline void os_reltime_sub(struct os_reltime *a, struct os_reltime *b,
78 struct os_reltime *res)
79{
80 res->sec = a->sec - b->sec;
81 res->usec = a->usec - b->usec;
82 if (res->usec < 0) {
83 res->sec--;
84 res->usec += 1000000;
85 }
86}
87
88
89static inline void os_reltime_age(struct os_reltime *start,
90 struct os_reltime *age)
91{
92 struct os_reltime now;
93
94 os_get_reltime(&now);
95 os_reltime_sub(&now, start, age);
96}
97
98
99static inline int os_reltime_expired(struct os_reltime *now,
100 struct os_reltime *ts,
101 os_time_t timeout_secs)
102{
103 struct os_reltime age;
104
105 os_reltime_sub(now, ts, &age);
106 return (age.sec > timeout_secs) ||
107 (age.sec == timeout_secs && age.usec > 0);
108}
109
110
111static inline int os_reltime_initialized(struct os_reltime *t)
112{
113 return t->sec != 0 || t->usec != 0;
114}
ebfa2275 115
ebfa2275
SZ
116
117/**
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
a875087d
JL
128 *
129 * Note: The result is in seconds from Epoch, i.e., in UTC, not in local time
130 * which is used by POSIX mktime().
ebfa2275
SZ
131 */
132int os_mktime(int year, int month, int day, int hour, int min, int sec,
133 os_time_t *t);
134
4781064b
JM
135struct os_tm {
136 int sec; /* 0..59 or 60 for leap seconds */
137 int min; /* 0..59 */
138 int hour; /* 0..23 */
139 int day; /* 1..31 */
140 int month; /* 1..12 */
141 int year; /* Four digit year */
142};
143
144int os_gmtime(os_time_t t, struct os_tm *tm);
ebfa2275
SZ
145
146/**
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
150 */
151int os_daemonize(const char *pid_file);
152
153/**
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
156 */
157void os_daemonize_terminate(const char *pid_file);
158
159/**
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
164 */
165int os_get_random(unsigned char *buf, size_t len);
166
167/**
168 * os_random - Get pseudo random value (not necessarily very strong)
169 * Returns: Pseudo random value
170 */
171unsigned long os_random(void);
172
173/**
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
177 *
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.
185 */
186char * os_rel2abs_path(const char *rel_path);
187
188/**
189 * os_program_init - Program initialization (called at start)
190 * Returns: 0 on success, -1 on failure
191 *
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.
195 */
196int os_program_init(void);
197
198/**
199 * os_program_deinit - Program deinitialization (called just before exit)
200 *
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
204 * nothing.
205 */
206void os_program_deinit(void);
207
208/**
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
214 *
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.
217 */
218int os_setenv(const char *name, const char *value, int overwrite);
219
220/**
221 * os_unsetenv - Delete environent variable
222 * @name: Name of the variable
223 * Returns: 0 on success, -1 on error
224 *
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.
227 */
228int os_unsetenv(const char *name);
229
230/**
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
235 *
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().
239 */
240char * os_readfile(const char *name, size_t *len);
241
242/**
243 * os_zalloc - Allocate and zero memory
244 * @size: Number of bytes to allocate
245 * Returns: Pointer to allocated and zeroed memory or %NULL on failure
246 *
247 * Caller is responsible for freeing the returned buffer with os_free().
248 */
249void * os_zalloc(size_t size);
250
4781064b
JM
251/**
252 * os_calloc - Allocate and zero memory for an array
253 * @nmemb: Number of members in the array
254 * @size: Number of bytes in each member
255 * Returns: Pointer to allocated and zeroed memory or %NULL on failure
256 *
257 * This function can be used as a wrapper for os_zalloc(nmemb * size) when an
258 * allocation is used for an array. The main benefit over os_zalloc() is in
259 * having an extra check to catch integer overflows in multiplication.
260 *
261 * Caller is responsible for freeing the returned buffer with os_free().
262 */
263static inline void * os_calloc(size_t nmemb, size_t size)
264{
265 if (size && nmemb > (~(size_t) 0) / size)
266 return NULL;
267 return os_zalloc(nmemb * size);
268}
269
ebfa2275
SZ
270
271/*
272 * The following functions are wrapper for standard ANSI C or POSIX functions.
273 * By default, they are just defined to use the standard function name and no
274 * os_*.c implementation is needed for them. This avoids extra function calls
275 * by allowing the C pre-processor take care of the function name mapping.
276 *
277 * If the target system uses a C library that does not provide these functions,
278 * build_config.h can be used to define the wrappers to use a different
279 * function name. This can be done on function-by-function basis since the
280 * defines here are only used if build_config.h does not define the os_* name.
281 * If needed, os_*.c file can be used to implement the functions that are not
282 * included in the C library on the target system. Alternatively,
283 * OS_NO_C_LIB_DEFINES can be defined to skip all defines here in which case
284 * these functions need to be implemented in os_*.c file for the target system.
285 */
286
287#ifdef OS_NO_C_LIB_DEFINES
288
289/**
290 * os_malloc - Allocate dynamic memory
291 * @size: Size of the buffer to allocate
292 * Returns: Allocated buffer or %NULL on failure
293 *
294 * Caller is responsible for freeing the returned buffer with os_free().
295 */
296void * os_malloc(size_t size);
297
298/**
299 * os_realloc - Re-allocate dynamic memory
300 * @ptr: Old buffer from os_malloc() or os_realloc()
301 * @size: Size of the new buffer
302 * Returns: Allocated buffer or %NULL on failure
303 *
304 * Caller is responsible for freeing the returned buffer with os_free().
305 * If re-allocation fails, %NULL is returned and the original buffer (ptr) is
306 * not freed and caller is still responsible for freeing it.
307 */
308void * os_realloc(void *ptr, size_t size);
309
310/**
311 * os_free - Free dynamic memory
312 * @ptr: Old buffer from os_malloc() or os_realloc(); can be %NULL
313 */
314void os_free(void *ptr);
315
316/**
317 * os_memcpy - Copy memory area
318 * @dest: Destination
319 * @src: Source
320 * @n: Number of bytes to copy
321 * Returns: dest
322 *
323 * The memory areas src and dst must not overlap. os_memmove() can be used with
324 * overlapping memory.
325 */
326void * os_memcpy(void *dest, const void *src, size_t n);
327
328/**
329 * os_memmove - Copy memory area
330 * @dest: Destination
331 * @src: Source
332 * @n: Number of bytes to copy
333 * Returns: dest
334 *
335 * The memory areas src and dst may overlap.
336 */
337void * os_memmove(void *dest, const void *src, size_t n);
338
339/**
340 * os_memset - Fill memory with a constant byte
341 * @s: Memory area to be filled
342 * @c: Constant byte
343 * @n: Number of bytes started from s to fill with c
344 * Returns: s
345 */
346void * os_memset(void *s, int c, size_t n);
347
348/**
349 * os_memcmp - Compare memory areas
350 * @s1: First buffer
351 * @s2: Second buffer
352 * @n: Maximum numbers of octets to compare
353 * Returns: An integer less than, equal to, or greater than zero if s1 is
354 * found to be less than, to match, or be greater than s2. Only first n
355 * characters will be compared.
356 */
357int os_memcmp(const void *s1, const void *s2, size_t n);
358
359/**
360 * os_strdup - Duplicate a string
361 * @s: Source string
362 * Returns: Allocated buffer with the string copied into it or %NULL on failure
363 *
364 * Caller is responsible for freeing the returned buffer with os_free().
365 */
366char * os_strdup(const char *s);
367
368/**
369 * os_strlen - Calculate the length of a string
370 * @s: '\0' terminated string
371 * Returns: Number of characters in s (not counting the '\0' terminator)
372 */
373size_t os_strlen(const char *s);
374
375/**
376 * os_strcasecmp - Compare two strings ignoring case
377 * @s1: First string
378 * @s2: Second string
379 * Returns: An integer less than, equal to, or greater than zero if s1 is
380 * found to be less than, to match, or be greatred than s2
381 */
382int os_strcasecmp(const char *s1, const char *s2);
383
384/**
385 * os_strncasecmp - Compare two strings ignoring case
386 * @s1: First string
387 * @s2: Second string
388 * @n: Maximum numbers of characters to compare
389 * Returns: An integer less than, equal to, or greater than zero if s1 is
390 * found to be less than, to match, or be greater than s2. Only first n
391 * characters will be compared.
392 */
393int os_strncasecmp(const char *s1, const char *s2, size_t n);
394
395/**
396 * os_strchr - Locate the first occurrence of a character in string
397 * @s: String
398 * @c: Character to search for
399 * Returns: Pointer to the matched character or %NULL if not found
400 */
401char * os_strchr(const char *s, int c);
402
403/**
404 * os_strrchr - Locate the last occurrence of a character in string
405 * @s: String
406 * @c: Character to search for
407 * Returns: Pointer to the matched character or %NULL if not found
408 */
409char * os_strrchr(const char *s, int c);
410
411/**
412 * os_strcmp - Compare two strings
413 * @s1: First string
414 * @s2: Second string
415 * Returns: An integer less than, equal to, or greater than zero if s1 is
416 * found to be less than, to match, or be greatred than s2
417 */
418int os_strcmp(const char *s1, const char *s2);
419
420/**
421 * os_strncmp - Compare two strings
422 * @s1: First string
423 * @s2: Second string
424 * @n: Maximum numbers of characters to compare
425 * Returns: An integer less than, equal to, or greater than zero if s1 is
426 * found to be less than, to match, or be greater than s2. Only first n
427 * characters will be compared.
428 */
429int os_strncmp(const char *s1, const char *s2, size_t n);
430
ebfa2275
SZ
431/**
432 * os_strstr - Locate a substring
433 * @haystack: String (haystack) to search from
434 * @needle: Needle to search from haystack
435 * Returns: Pointer to the beginning of the substring or %NULL if not found
436 */
437char * os_strstr(const char *haystack, const char *needle);
438
439/**
440 * os_snprintf - Print to a memory buffer
441 * @str: Memory buffer to print into
442 * @size: Maximum length of the str buffer
443 * @format: printf format
444 * Returns: Number of characters printed (not including trailing '\0').
445 *
446 * If the output buffer is truncated, number of characters which would have
447 * been written is returned. Since some C libraries return -1 in such a case,
448 * the caller must be prepared on that value, too, to indicate truncation.
449 *
450 * Note: Some C library implementations of snprintf() may not guarantee null
451 * termination in case the output is truncated. The OS wrapper function of
452 * os_snprintf() should provide this guarantee, i.e., to null terminate the
453 * output buffer if a C library version of the function is used and if that
454 * function does not guarantee null termination.
455 *
456 * If the target system does not include snprintf(), see, e.g.,
457 * http://www.ijs.si/software/snprintf/ for an example of a portable
458 * implementation of snprintf.
459 */
460int os_snprintf(char *str, size_t size, const char *format, ...);
461
462#else /* OS_NO_C_LIB_DEFINES */
463
4781064b
JM
464#ifdef WPA_TRACE
465void * os_malloc(size_t size);
466void * os_realloc(void *ptr, size_t size);
467void os_free(void *ptr);
468char * os_strdup(const char *s);
469#else /* WPA_TRACE */
ebfa2275
SZ
470#ifndef os_malloc
471#define os_malloc(s) malloc((s))
472#endif
473#ifndef os_realloc
474#define os_realloc(p, s) realloc((p), (s))
475#endif
476#ifndef os_free
477#define os_free(p) free((p))
478#endif
4781064b
JM
479#ifndef os_strdup
480#ifdef _MSC_VER
481#define os_strdup(s) _strdup(s)
482#else
483#define os_strdup(s) strdup(s)
484#endif
485#endif
486#endif /* WPA_TRACE */
ebfa2275
SZ
487
488#ifndef os_memcpy
489#define os_memcpy(d, s, n) memcpy((d), (s), (n))
490#endif
491#ifndef os_memmove
492#define os_memmove(d, s, n) memmove((d), (s), (n))
493#endif
494#ifndef os_memset
495#define os_memset(s, c, n) memset(s, c, n)
496#endif
497#ifndef os_memcmp
498#define os_memcmp(s1, s2, n) memcmp((s1), (s2), (n))
499#endif
500
ebfa2275
SZ
501#ifndef os_strlen
502#define os_strlen(s) strlen(s)
503#endif
504#ifndef os_strcasecmp
505#ifdef _MSC_VER
506#define os_strcasecmp(s1, s2) _stricmp((s1), (s2))
507#else
508#define os_strcasecmp(s1, s2) strcasecmp((s1), (s2))
509#endif
510#endif
511#ifndef os_strncasecmp
512#ifdef _MSC_VER
513#define os_strncasecmp(s1, s2, n) _strnicmp((s1), (s2), (n))
514#else
515#define os_strncasecmp(s1, s2, n) strncasecmp((s1), (s2), (n))
516#endif
517#endif
518#ifndef os_strchr
519#define os_strchr(s, c) strchr((s), (c))
520#endif
521#ifndef os_strcmp
522#define os_strcmp(s1, s2) strcmp((s1), (s2))
523#endif
524#ifndef os_strncmp
525#define os_strncmp(s1, s2, n) strncmp((s1), (s2), (n))
526#endif
ebfa2275
SZ
527#ifndef os_strrchr
528#define os_strrchr(s, c) strrchr((s), (c))
529#endif
530#ifndef os_strstr
531#define os_strstr(h, n) strstr((h), (n))
532#endif
533
534#ifndef os_snprintf
535#ifdef _MSC_VER
536#define os_snprintf _snprintf
537#else
538#define os_snprintf snprintf
539#endif
540#endif
541
542#endif /* OS_NO_C_LIB_DEFINES */
543
544
4781064b
JM
545static inline void * os_realloc_array(void *ptr, size_t nmemb, size_t size)
546{
547 if (size && nmemb > (~(size_t) 0) / size)
548 return NULL;
549 return os_realloc(ptr, nmemb * size);
550}
551
552
a875087d
JL
553/**
554 * os_strlcpy - Copy a string with size bound and NUL-termination
555 * @dest: Destination
556 * @src: Source
557 * @siz: Size of the target buffer
558 * Returns: Total length of the target string (length of src) (not including
559 * NUL-termination)
560 *
561 * This function matches in behavior with the strlcpy(3) function in OpenBSD.
562 */
563size_t os_strlcpy(char *dest, const char *src, size_t siz);
564
565
ebfa2275
SZ
566#ifdef OS_REJECT_C_LIB_FUNCTIONS
567#define malloc OS_DO_NOT_USE_malloc
568#define realloc OS_DO_NOT_USE_realloc
569#define free OS_DO_NOT_USE_free
570#define memcpy OS_DO_NOT_USE_memcpy
571#define memmove OS_DO_NOT_USE_memmove
572#define memset OS_DO_NOT_USE_memset
573#define memcmp OS_DO_NOT_USE_memcmp
574#undef strdup
575#define strdup OS_DO_NOT_USE_strdup
576#define strlen OS_DO_NOT_USE_strlen
577#define strcasecmp OS_DO_NOT_USE_strcasecmp
578#define strncasecmp OS_DO_NOT_USE_strncasecmp
579#undef strchr
580#define strchr OS_DO_NOT_USE_strchr
581#undef strcmp
582#define strcmp OS_DO_NOT_USE_strcmp
583#undef strncmp
584#define strncmp OS_DO_NOT_USE_strncmp
585#undef strncpy
586#define strncpy OS_DO_NOT_USE_strncpy
587#define strrchr OS_DO_NOT_USE_strrchr
588#define strstr OS_DO_NOT_USE_strstr
589#undef snprintf
590#define snprintf OS_DO_NOT_USE_snprintf
591
592#define strcpy OS_DO_NOT_USE_strcpy
593#endif /* OS_REJECT_C_LIB_FUNCTIONS */
594
595#endif /* OS_H */