2 * wpa_supplicant/hostapd / Internal implementation of OS specific functions
3 * Copyright (c) 2005-2006, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
8 * This file is an example of operating system specific wrapper functions.
9 * This version implements many of the functions internally, so it can be used
10 * to fill in missing functions from the target system C libraries.
12 * Some of the functions are using standard C library calls in order to keep
13 * this file in working condition to allow the functions to be tested on a
14 * Linux target. Please note that OS_NO_C_LIB_DEFINES needs to be defined for
15 * this file to work correctly. Note that these implementations are only
16 * examples and are not optimized for speed.
23 #undef OS_REJECT_C_LIB_FUNCTIONS
26 void os_sleep(os_time_t sec, os_time_t usec)
35 int os_get_time(struct os_time *t)
39 res = gettimeofday(&tv, NULL);
46 int os_get_reltime(struct os_reltime *t)
50 res = gettimeofday(&tv, NULL);
57 int os_mktime(int year, int month, int day, int hour, int min, int sec,
62 if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
63 hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 ||
67 os_memset(&tm, 0, sizeof(tm));
68 tm.tm_year = year - 1900;
69 tm.tm_mon = month - 1;
75 *t = (os_time_t) mktime(&tm);
80 int os_gmtime(os_time_t t, struct os_tm *tm)
88 tm->sec = tm2->tm_sec;
89 tm->min = tm2->tm_min;
90 tm->hour = tm2->tm_hour;
91 tm->day = tm2->tm_mday;
92 tm->month = tm2->tm_mon + 1;
93 tm->year = tm2->tm_year + 1900;
98 int os_daemonize(const char *pid_file)
101 wpa_printf(MSG_ERROR, "daemon: %s", strerror(errno));
106 FILE *f = fopen(pid_file, "w");
108 fprintf(f, "%u\n", getpid());
117 void os_daemonize_terminate(const char *pid_file)
124 int os_get_random(unsigned char *buf, size_t len)
129 f = fopen("/dev/urandom", "rb");
131 printf("Could not open /dev/urandom.\n");
135 rc = fread(buf, 1, len, f);
138 return rc != len ? -1 : 0;
142 unsigned long os_random(void)
148 char * os_rel2abs_path(const char *rel_path)
150 char *buf = NULL, *cwd, *ret;
151 size_t len = 128, cwd_len, rel_len, ret_len;
153 if (rel_path[0] == '/')
154 return os_strdup(rel_path);
157 buf = os_malloc(len);
160 cwd = getcwd(buf, len);
163 if (errno != ERANGE) {
172 cwd_len = os_strlen(cwd);
173 rel_len = os_strlen(rel_path);
174 ret_len = cwd_len + 1 + rel_len + 1;
175 ret = os_malloc(ret_len);
177 os_memcpy(ret, cwd, cwd_len);
179 os_memcpy(ret + cwd_len + 1, rel_path, rel_len);
180 ret[ret_len - 1] = '\0';
187 int os_program_init(void)
193 void os_program_deinit(void)
198 int os_setenv(const char *name, const char *value, int overwrite)
200 return setenv(name, value, overwrite);
204 int os_unsetenv(const char *name)
206 #if defined(__FreeBSD__) || defined(__NetBSD__)
210 return unsetenv(name);
215 char * os_readfile(const char *name, size_t *len)
220 f = fopen(name, "rb");
224 fseek(f, 0, SEEK_END);
226 fseek(f, 0, SEEK_SET);
228 buf = os_malloc(*len);
234 if (fread(buf, 1, *len, f) != *len) {
246 int os_fdatasync(FILE *stream)
252 void * os_zalloc(size_t size)
254 void *n = os_malloc(size);
256 os_memset(n, 0, size);
261 void * os_malloc(size_t size)
267 void * os_realloc(void *ptr, size_t size)
269 return realloc(ptr, size);
273 void os_free(void *ptr)
279 void * os_memcpy(void *dest, const void *src, size_t n)
289 void * os_memmove(void *dest, const void *src, size_t n)
292 os_memcpy(dest, src, n);
294 /* overlapping areas */
295 char *d = (char *) dest + n;
296 const char *s = (const char *) src + n;
304 void * os_memset(void *s, int c, size_t n)
313 int os_memcmp(const void *s1, const void *s2, size_t n)
315 const unsigned char *p1 = s1, *p2 = s2;
332 char * os_strdup(const char *s)
339 res = os_malloc(len + 1);
341 os_memcpy(res, s, len + 1);
346 size_t os_strlen(const char *s)
355 int os_strcasecmp(const char *s1, const char *s2)
358 * Ignoring case is not required for main functionality, so just use
359 * the case sensitive version of the function.
361 return os_strcmp(s1, s2);
365 int os_strncasecmp(const char *s1, const char *s2, size_t n)
368 * Ignoring case is not required for main functionality, so just use
369 * the case sensitive version of the function.
371 return os_strncmp(s1, s2, n);
375 char * os_strchr(const char *s, int c)
386 char * os_strrchr(const char *s, int c)
401 int os_strcmp(const char *s1, const char *s2)
414 int os_strncmp(const char *s1, const char *s2, size_t n)
433 char * os_strncpy(char *dest, const char *src, size_t n)
449 size_t os_strlcpy(char *dest, const char *src, size_t siz)
455 /* Copy string up to the maximum size of the dest buffer */
456 while (--left != 0) {
457 if ((*dest++ = *s++) == '\0')
463 /* Not enough room for the string; force NUL-termination */
467 ; /* determine total src string length */
474 int os_memcmp_const(const void *a, const void *b, size_t len)
481 for (res = 0, i = 0; i < len; i++)
482 res |= aa[i] ^ bb[i];
488 char * os_strstr(const char *haystack, const char *needle)
490 size_t len = os_strlen(needle);
492 if (os_strncmp(haystack, needle, len) == 0)
493 return (char *) haystack;
501 int os_snprintf(char *str, size_t size, const char *format, ...)
506 /* See http://www.ijs.si/software/snprintf/ for portable
507 * implementation of snprintf.
510 va_start(ap, format);
511 ret = vsnprintf(str, size, format, ap);
514 str[size - 1] = '\0';
519 int os_exec(const char *program, const char *arg, int wait_completion)
526 wpa_printf(MSG_ERROR, "fork: %s", strerror(errno));
531 /* run the external command in the child process */
532 const int MAX_ARG = 30;
533 char *_program, *_arg, *pos;
534 char *argv[MAX_ARG + 1];
537 _program = os_strdup(program);
538 _arg = os_strdup(arg);
544 while (i < MAX_ARG && pos && *pos) {
550 pos = os_strchr(pos, ' ');
556 execv(program, argv);
557 wpa_printf(MSG_ERROR, "execv: %s", strerror(errno));
564 if (wait_completion) {
565 /* wait for the child process to complete in the parent */
566 waitpid(pid, &pid_status, 0);