wpa_supplicant / hostapd  2.5
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
os.h
Go to the documentation of this file.
1 
5 #ifndef OS_H
6 #define OS_H
7 
8 typedef long os_time_t;
9 
15 void os_sleep(os_time_t sec, os_time_t usec);
16 
17 struct os_time {
18  os_time_t sec;
19  os_time_t usec;
20 };
21 
22 struct os_reltime {
23  os_time_t sec;
24  os_time_t usec;
25 };
26 
32 int os_get_time(struct os_time *t);
33 
39 int os_get_reltime(struct os_reltime *t);
40 
41 
42 /* Helpers for handling struct os_time */
43 
44 static inline int os_time_before(struct os_time *a, struct os_time *b)
45 {
46  return (a->sec < b->sec) ||
47  (a->sec == b->sec && a->usec < b->usec);
48 }
49 
50 
51 static inline void os_time_sub(struct os_time *a, struct os_time *b,
52  struct os_time *res)
53 {
54  res->sec = a->sec - b->sec;
55  res->usec = a->usec - b->usec;
56  if (res->usec < 0) {
57  res->sec--;
58  res->usec += 1000000;
59  }
60 }
61 
62 
63 /* Helpers for handling struct os_reltime */
64 
65 static inline int os_reltime_before(struct os_reltime *a,
66  struct os_reltime *b)
67 {
68  return (a->sec < b->sec) ||
69  (a->sec == b->sec && a->usec < b->usec);
70 }
71 
72 
73 static inline void os_reltime_sub(struct os_reltime *a, struct os_reltime *b,
74  struct os_reltime *res)
75 {
76  res->sec = a->sec - b->sec;
77  res->usec = a->usec - b->usec;
78  if (res->usec < 0) {
79  res->sec--;
80  res->usec += 1000000;
81  }
82 }
83 
84 
85 static inline void os_reltime_age(struct os_reltime *start,
86  struct os_reltime *age)
87 {
88  struct os_reltime now;
89 
90  os_get_reltime(&now);
91  os_reltime_sub(&now, start, age);
92 }
93 
94 
95 static inline int os_reltime_expired(struct os_reltime *now,
96  struct os_reltime *ts,
97  os_time_t timeout_secs)
98 {
99  struct os_reltime age;
100 
101  os_reltime_sub(now, ts, &age);
102  return (age.sec > timeout_secs) ||
103  (age.sec == timeout_secs && age.usec > 0);
104 }
105 
106 
107 static inline int os_reltime_initialized(struct os_reltime *t)
108 {
109  return t->sec != 0 || t->usec != 0;
110 }
111 
112 
128 int os_mktime(int year, int month, int day, int hour, int min, int sec,
129  os_time_t *t);
130 
131 struct os_tm {
132  int sec; /* 0..59 or 60 for leap seconds */
133  int min; /* 0..59 */
134  int hour; /* 0..23 */
135  int day; /* 1..31 */
136  int month; /* 1..12 */
137  int year; /* Four digit year */
138 };
139 
140 int os_gmtime(os_time_t t, struct os_tm *tm);
141 
147 int os_daemonize(const char *pid_file);
148 
153 void os_daemonize_terminate(const char *pid_file);
154 
161 int os_get_random(unsigned char *buf, size_t len);
162 
167 unsigned long os_random(void);
168 
182 char * os_rel2abs_path(const char *rel_path);
183 
192 int os_program_init(void);
193 
203 void os_program_deinit(void);
204 
215 int os_setenv(const char *name, const char *value, int overwrite);
216 
225 int os_unsetenv(const char *name);
226 
237 char * os_readfile(const char *name, size_t *len);
238 
244 int os_file_exists(const char *fname);
245 
251 int os_fdatasync(FILE *stream);
252 
260 void * os_zalloc(size_t size);
261 
274 static inline void * os_calloc(size_t nmemb, size_t size)
275 {
276  if (size && nmemb > (~(size_t) 0) / size)
277  return NULL;
278  return os_zalloc(nmemb * size);
279 }
280 
281 
282 /*
283  * The following functions are wrapper for standard ANSI C or POSIX functions.
284  * By default, they are just defined to use the standard function name and no
285  * os_*.c implementation is needed for them. This avoids extra function calls
286  * by allowing the C pre-processor take care of the function name mapping.
287  *
288  * If the target system uses a C library that does not provide these functions,
289  * build_config.h can be used to define the wrappers to use a different
290  * function name. This can be done on function-by-function basis since the
291  * defines here are only used if build_config.h does not define the os_* name.
292  * If needed, os_*.c file can be used to implement the functions that are not
293  * included in the C library on the target system. Alternatively,
294  * OS_NO_C_LIB_DEFINES can be defined to skip all defines here in which case
295  * these functions need to be implemented in os_*.c file for the target system.
296  */
297 
298 #ifdef OS_NO_C_LIB_DEFINES
299 
307 void * os_malloc(size_t size);
308 
319 void * os_realloc(void *ptr, size_t size);
320 
325 void os_free(void *ptr);
326 
337 void * os_memcpy(void *dest, const void *src, size_t n);
338 
348 void * os_memmove(void *dest, const void *src, size_t n);
349 
357 void * os_memset(void *s, int c, size_t n);
358 
368 int os_memcmp(const void *s1, const void *s2, size_t n);
369 
377 char * os_strdup(const char *s);
378 
384 size_t os_strlen(const char *s);
385 
393 int os_strcasecmp(const char *s1, const char *s2);
394 
404 int os_strncasecmp(const char *s1, const char *s2, size_t n);
405 
412 char * os_strchr(const char *s, int c);
413 
420 char * os_strrchr(const char *s, int c);
421 
429 int os_strcmp(const char *s1, const char *s2);
430 
440 int os_strncmp(const char *s1, const char *s2, size_t n);
441 
448 char * os_strstr(const char *haystack, const char *needle);
449 
471 int os_snprintf(char *str, size_t size, const char *format, ...);
472 
473 #else /* OS_NO_C_LIB_DEFINES */
474 
475 #ifdef WPA_TRACE
476 void * os_malloc(size_t size);
477 void * os_realloc(void *ptr, size_t size);
478 void os_free(void *ptr);
479 char * os_strdup(const char *s);
480 #else /* WPA_TRACE */
481 #ifndef os_malloc
482 #define os_malloc(s) malloc((s))
483 #endif
484 #ifndef os_realloc
485 #define os_realloc(p, s) realloc((p), (s))
486 #endif
487 #ifndef os_free
488 #define os_free(p) free((p))
489 #endif
490 #ifndef os_strdup
491 #ifdef _MSC_VER
492 #define os_strdup(s) _strdup(s)
493 #else
494 #define os_strdup(s) strdup(s)
495 #endif
496 #endif
497 #endif /* WPA_TRACE */
498 
499 #ifndef os_memcpy
500 #define os_memcpy(d, s, n) memcpy((d), (s), (n))
501 #endif
502 #ifndef os_memmove
503 #define os_memmove(d, s, n) memmove((d), (s), (n))
504 #endif
505 #ifndef os_memset
506 #define os_memset(s, c, n) memset(s, c, n)
507 #endif
508 #ifndef os_memcmp
509 #define os_memcmp(s1, s2, n) memcmp((s1), (s2), (n))
510 #endif
511 
512 #ifndef os_strlen
513 #define os_strlen(s) strlen(s)
514 #endif
515 #ifndef os_strcasecmp
516 #ifdef _MSC_VER
517 #define os_strcasecmp(s1, s2) _stricmp((s1), (s2))
518 #else
519 #define os_strcasecmp(s1, s2) strcasecmp((s1), (s2))
520 #endif
521 #endif
522 #ifndef os_strncasecmp
523 #ifdef _MSC_VER
524 #define os_strncasecmp(s1, s2, n) _strnicmp((s1), (s2), (n))
525 #else
526 #define os_strncasecmp(s1, s2, n) strncasecmp((s1), (s2), (n))
527 #endif
528 #endif
529 #ifndef os_strchr
530 #define os_strchr(s, c) strchr((s), (c))
531 #endif
532 #ifndef os_strcmp
533 #define os_strcmp(s1, s2) strcmp((s1), (s2))
534 #endif
535 #ifndef os_strncmp
536 #define os_strncmp(s1, s2, n) strncmp((s1), (s2), (n))
537 #endif
538 #ifndef os_strrchr
539 #define os_strrchr(s, c) strrchr((s), (c))
540 #endif
541 #ifndef os_strstr
542 #define os_strstr(h, n) strstr((h), (n))
543 #endif
544 
545 #ifndef os_snprintf
546 #ifdef _MSC_VER
547 #define os_snprintf _snprintf
548 #else
549 #define os_snprintf snprintf
550 #endif
551 #endif
552 
553 #endif /* OS_NO_C_LIB_DEFINES */
554 
555 
556 static inline int os_snprintf_error(size_t size, int res)
557 {
558  return res < 0 || (unsigned int) res >= size;
559 }
560 
561 
562 static inline void * os_realloc_array(void *ptr, size_t nmemb, size_t size)
563 {
564  if (size && nmemb > (~(size_t) 0) / size)
565  return NULL;
566  return os_realloc(ptr, nmemb * size);
567 }
568 
576 static inline void os_remove_in_array(void *ptr, size_t nmemb, size_t size,
577  size_t idx)
578 {
579  if (idx < nmemb - 1)
580  os_memmove(((unsigned char *) ptr) + idx * size,
581  ((unsigned char *) ptr) + (idx + 1) * size,
582  (nmemb - idx - 1) * size);
583 }
584 
595 size_t os_strlcpy(char *dest, const char *src, size_t siz);
596 
612 int os_memcmp_const(const void *a, const void *b, size_t len);
613 
621 int os_exec(const char *program, const char *arg, int wait_completion);
622 
623 
624 #ifdef OS_REJECT_C_LIB_FUNCTIONS
625 #define malloc OS_DO_NOT_USE_malloc
626 #define realloc OS_DO_NOT_USE_realloc
627 #define free OS_DO_NOT_USE_free
628 #define memcpy OS_DO_NOT_USE_memcpy
629 #define memmove OS_DO_NOT_USE_memmove
630 #define memset OS_DO_NOT_USE_memset
631 #define memcmp OS_DO_NOT_USE_memcmp
632 #undef strdup
633 #define strdup OS_DO_NOT_USE_strdup
634 #define strlen OS_DO_NOT_USE_strlen
635 #define strcasecmp OS_DO_NOT_USE_strcasecmp
636 #define strncasecmp OS_DO_NOT_USE_strncasecmp
637 #undef strchr
638 #define strchr OS_DO_NOT_USE_strchr
639 #undef strcmp
640 #define strcmp OS_DO_NOT_USE_strcmp
641 #undef strncmp
642 #define strncmp OS_DO_NOT_USE_strncmp
643 #undef strncpy
644 #define strncpy OS_DO_NOT_USE_strncpy
645 #define strrchr OS_DO_NOT_USE_strrchr
646 #define strstr OS_DO_NOT_USE_strstr
647 #undef snprintf
648 #define snprintf OS_DO_NOT_USE_snprintf
649 
650 #define strcpy OS_DO_NOT_USE_strcpy
651 #endif /* OS_REJECT_C_LIB_FUNCTIONS */
652 
653 
654 #if defined(WPA_TRACE_BFD) && defined(CONFIG_TESTING_OPTIONS)
655 #define TEST_FAIL() testing_test_fail()
656 int testing_test_fail(void);
657 #else
658 #define TEST_FAIL() 0
659 #endif
660 
661 #endif /* OS_H */
char * os_readfile(const char *name, size_t *len)
Read a file to an allocated memory buffer.
Definition: os_internal.c:211
int os_file_exists(const char *fname)
Check whether the specified file exists.
Definition: os_unix.c:431
Definition: os.h:17
void os_sleep(os_time_t sec, os_time_t usec)
Sleep (sec, usec)
Definition: os_internal.c:22
int os_memcmp_const(const void *a, const void *b, size_t len)
Constant time memory comparison.
Definition: os_internal.c:470
int os_program_init(void)
Program initialization (called at start)
Definition: os_internal.c:183
void * os_zalloc(size_t size)
Allocate and zero memory.
Definition: os_internal.c:248
int os_get_random(unsigned char *buf, size_t len)
Get cryptographically strong pseudo random data.
Definition: os_internal.c:120
int os_fdatasync(FILE *stream)
Sync a file's (for a given stream) state with storage device.
Definition: os_internal.c:242
int os_exec(const char *program, const char *arg, int wait_completion)
Execute an external program.
Definition: os_internal.c:515
Definition: os.h:131
void os_daemonize_terminate(const char *pid_file)
Stop running in the background (remove pid file)
Definition: os_internal.c:113
void os_program_deinit(void)
Program deinitialization (called just before exit)
Definition: os_internal.c:189
int os_get_time(struct os_time *t)
Get current time (sec, usec)
Definition: os_internal.c:31
Definition: os.h:22
int os_mktime(int year, int month, int day, int hour, int min, int sec, os_time_t *t)
Convert broken-down time into seconds since 1970-01-01.
Definition: os_internal.c:53
int os_daemonize(const char *pid_file)
Run in the background (detach from the controlling terminal)
Definition: os_internal.c:94
int os_get_reltime(struct os_reltime *t)
Get relative time (sec, usec)
Definition: os_internal.c:42
size_t os_strlcpy(char *dest, const char *src, size_t siz)
Copy a string with size bound and NUL-termination.
Definition: os_internal.c:445
int os_setenv(const char *name, const char *value, int overwrite)
Set environment variable.
Definition: os_internal.c:194
int os_unsetenv(const char *name)
Delete environent variable.
Definition: os_internal.c:200
char * os_rel2abs_path(const char *rel_path)
Get an absolute path for a file.
Definition: os_internal.c:144
unsigned long os_random(void)
Get pseudo random value (not necessarily very strong)
Definition: os_internal.c:138