2 * OS specific functions for UNIX/POSIX systems
3 * Copyright (c) 2005-2009, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
14 #include <sys/capability.h>
15 #include <linux/prctl.h>
16 #include <private/android_filesystem_config.h>
24 #include "wpa_debug.h"
28 static struct dl_list alloc_list;
30 #define ALLOC_MAGIC 0xa84ef1b2
31 #define FREED_MAGIC 0x67fd487a
33 struct os_alloc_trace {
40 #endif /* WPA_TRACE */
43 void os_sleep(os_time_t sec, os_time_t usec)
52 int os_get_time(struct os_time *t)
56 res = gettimeofday(&tv, NULL);
63 int os_get_reltime(struct os_reltime *t)
65 #if defined(CLOCK_BOOTTIME)
66 static clockid_t clock_id = CLOCK_BOOTTIME;
67 #elif defined(CLOCK_MONOTONIC)
68 static clockid_t clock_id = CLOCK_MONOTONIC;
70 static clockid_t clock_id = CLOCK_REALTIME;
76 res = clock_gettime(clock_id, &ts);
79 t->usec = ts.tv_nsec / 1000;
85 clock_id = CLOCK_MONOTONIC;
88 #ifdef CLOCK_MONOTONIC
90 clock_id = CLOCK_REALTIME;
100 int os_mktime(int year, int month, int day, int hour, int min, int sec,
104 time_t t_local, t1, t2;
107 if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
108 hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 ||
112 memset(&tm, 0, sizeof(tm));
113 tm.tm_year = year - 1900;
114 tm.tm_mon = month - 1;
120 t_local = mktime(&tm);
122 /* figure out offset to UTC */
123 tm1 = localtime(&t_local);
126 tm1 = gmtime(&t_local);
135 *t = (os_time_t) t_local - tz_offset;
140 int os_gmtime(os_time_t t, struct os_tm *tm)
148 tm->sec = tm2->tm_sec;
149 tm->min = tm2->tm_min;
150 tm->hour = tm2->tm_hour;
151 tm->day = tm2->tm_mday;
152 tm->month = tm2->tm_mon + 1;
153 tm->year = tm2->tm_year + 1900;
160 static int os_daemon(int nochdir, int noclose)
167 devnull = open("/dev/null", O_RDWR);
171 if (dup2(devnull, STDIN_FILENO) < 0) {
176 if (dup2(devnull, STDOUT_FILENO) < 0) {
181 if (dup2(devnull, STDERR_FILENO) < 0) {
188 #else /* __APPLE__ */
189 #define os_daemon daemon
190 #endif /* __APPLE__ */
193 int os_daemonize(const char *pid_file)
195 #if defined(__uClinux__) || defined(__sun__)
197 #else /* defined(__uClinux__) || defined(__sun__) */
198 if (os_daemon(0, 0)) {
204 FILE *f = fopen(pid_file, "w");
206 fprintf(f, "%u\n", getpid());
212 #endif /* defined(__uClinux__) || defined(__sun__) */
216 void os_daemonize_terminate(const char *pid_file)
223 int os_get_random(unsigned char *buf, size_t len)
228 f = fopen("/dev/urandom", "rb");
230 printf("Could not open /dev/urandom.\n");
234 rc = fread(buf, 1, len, f);
237 return rc != len ? -1 : 0;
241 unsigned long os_random(void)
247 char * os_rel2abs_path(const char *rel_path)
249 char *buf = NULL, *cwd, *ret;
250 size_t len = 128, cwd_len, rel_len, ret_len;
256 if (rel_path[0] == '/')
257 return os_strdup(rel_path);
260 buf = os_malloc(len);
263 cwd = getcwd(buf, len);
267 if (last_errno != ERANGE)
278 cwd_len = os_strlen(cwd);
279 rel_len = os_strlen(rel_path);
280 ret_len = cwd_len + 1 + rel_len + 1;
281 ret = os_malloc(ret_len);
283 os_memcpy(ret, cwd, cwd_len);
285 os_memcpy(ret + cwd_len + 1, rel_path, rel_len);
286 ret[ret_len - 1] = '\0';
293 int os_program_init(void)
297 * We ignore errors here since errors are normal if we
298 * are already running as non-root.
300 #ifdef ANDROID_SETGROUPS_OVERRIDE
301 gid_t groups[] = { ANDROID_SETGROUPS_OVERRIDE };
302 #else /* ANDROID_SETGROUPS_OVERRIDE */
303 gid_t groups[] = { AID_INET, AID_WIFI, AID_KEYSTORE };
304 #endif /* ANDROID_SETGROUPS_OVERRIDE */
305 struct __user_cap_header_struct header;
306 struct __user_cap_data_struct cap;
308 setgroups(ARRAY_SIZE(groups), groups);
310 prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
315 header.version = _LINUX_CAPABILITY_VERSION;
317 cap.effective = cap.permitted =
318 (1 << CAP_NET_ADMIN) | (1 << CAP_NET_RAW);
320 capset(&header, &cap);
324 dl_list_init(&alloc_list);
325 #endif /* WPA_TRACE */
330 void os_program_deinit(void)
333 struct os_alloc_trace *a;
334 unsigned long total = 0;
335 dl_list_for_each(a, &alloc_list, struct os_alloc_trace, list) {
337 if (a->magic != ALLOC_MAGIC) {
338 wpa_printf(MSG_INFO, "MEMLEAK[%p]: invalid magic 0x%x "
340 a, a->magic, (unsigned long) a->len);
343 wpa_printf(MSG_INFO, "MEMLEAK[%p]: len %lu",
344 a, (unsigned long) a->len);
345 wpa_trace_dump("memleak", a);
348 wpa_printf(MSG_INFO, "MEMLEAK: total %lu bytes",
349 (unsigned long) total);
350 #endif /* WPA_TRACE */
354 int os_setenv(const char *name, const char *value, int overwrite)
356 return setenv(name, value, overwrite);
360 int os_unsetenv(const char *name)
362 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__) || \
367 return unsetenv(name);
372 char * os_readfile(const char *name, size_t *len)
378 f = fopen(name, "rb");
382 if (fseek(f, 0, SEEK_END) < 0 || (pos = ftell(f)) < 0) {
387 if (fseek(f, 0, SEEK_SET) < 0) {
392 buf = os_malloc(*len);
398 if (fread(buf, 1, *len, f) != *len) {
411 void * os_zalloc(size_t size)
413 return calloc(1, size);
415 #endif /* WPA_TRACE */
418 size_t os_strlcpy(char *dest, const char *src, size_t siz)
424 /* Copy string up to the maximum size of the dest buffer */
425 while (--left != 0) {
426 if ((*dest++ = *s++) == '\0')
432 /* Not enough room for the string; force NUL-termination */
436 ; /* determine total src string length */
445 void * os_malloc(size_t size)
447 struct os_alloc_trace *a;
448 a = malloc(sizeof(*a) + size);
451 a->magic = ALLOC_MAGIC;
452 dl_list_add(&alloc_list, &a->list);
459 void * os_realloc(void *ptr, size_t size)
461 struct os_alloc_trace *a;
466 return os_malloc(size);
468 a = (struct os_alloc_trace *) ptr - 1;
469 if (a->magic != ALLOC_MAGIC) {
470 wpa_printf(MSG_INFO, "REALLOC[%p]: invalid magic 0x%x%s",
472 a->magic == FREED_MAGIC ? " (already freed)" : "");
473 wpa_trace_show("Invalid os_realloc() call");
482 os_memcpy(n, a + 1, copy_len);
488 void os_free(void *ptr)
490 struct os_alloc_trace *a;
494 a = (struct os_alloc_trace *) ptr - 1;
495 if (a->magic != ALLOC_MAGIC) {
496 wpa_printf(MSG_INFO, "FREE[%p]: invalid magic 0x%x%s",
498 a->magic == FREED_MAGIC ? " (already freed)" : "");
499 wpa_trace_show("Invalid os_free() call");
502 dl_list_del(&a->list);
503 a->magic = FREED_MAGIC;
505 wpa_trace_check_ref(ptr);
510 void * os_zalloc(size_t size)
512 void *ptr = os_malloc(size);
514 os_memset(ptr, 0, size);
519 char * os_strdup(const char *s)
524 d = os_malloc(len + 1);
527 os_memcpy(d, s, len);
532 #endif /* WPA_TRACE */