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