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 <linux/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_mktime(int year, int month, int day, int hour, int min, int sec,
67 time_t t_local, t1, t2;
70 if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
71 hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 ||
75 memset(&tm, 0, sizeof(tm));
76 tm.tm_year = year - 1900;
77 tm.tm_mon = month - 1;
83 t_local = mktime(&tm);
85 /* figure out offset to UTC */
86 tm1 = localtime(&t_local);
89 tm1 = gmtime(&t_local);
98 *t = (os_time_t) t_local - tz_offset;
103 int os_gmtime(os_time_t t, struct os_tm *tm)
111 tm->sec = tm2->tm_sec;
112 tm->min = tm2->tm_min;
113 tm->hour = tm2->tm_hour;
114 tm->day = tm2->tm_mday;
115 tm->month = tm2->tm_mon + 1;
116 tm->year = tm2->tm_year + 1900;
123 static int os_daemon(int nochdir, int noclose)
130 devnull = open("/dev/null", O_RDWR);
134 if (dup2(devnull, STDIN_FILENO) < 0) {
139 if (dup2(devnull, STDOUT_FILENO) < 0) {
144 if (dup2(devnull, STDERR_FILENO) < 0) {
151 #else /* __APPLE__ */
152 #define os_daemon daemon
153 #endif /* __APPLE__ */
156 int os_daemonize(const char *pid_file)
158 #if defined(__uClinux__) || defined(__sun__)
160 #else /* defined(__uClinux__) || defined(__sun__) */
161 if (os_daemon(0, 0)) {
167 FILE *f = fopen(pid_file, "w");
169 fprintf(f, "%u\n", getpid());
175 #endif /* defined(__uClinux__) || defined(__sun__) */
179 void os_daemonize_terminate(const char *pid_file)
186 int os_get_random(unsigned char *buf, size_t len)
191 f = fopen("/dev/urandom", "rb");
193 printf("Could not open /dev/urandom.\n");
197 rc = fread(buf, 1, len, f);
200 return rc != len ? -1 : 0;
204 unsigned long os_random(void)
210 char * os_rel2abs_path(const char *rel_path)
212 char *buf = NULL, *cwd, *ret;
213 size_t len = 128, cwd_len, rel_len, ret_len;
216 if (rel_path[0] == '/')
217 return os_strdup(rel_path);
220 buf = os_malloc(len);
223 cwd = getcwd(buf, len);
227 if (last_errno != ERANGE)
238 cwd_len = os_strlen(cwd);
239 rel_len = os_strlen(rel_path);
240 ret_len = cwd_len + 1 + rel_len + 1;
241 ret = os_malloc(ret_len);
243 os_memcpy(ret, cwd, cwd_len);
245 os_memcpy(ret + cwd_len + 1, rel_path, rel_len);
246 ret[ret_len - 1] = '\0';
253 int os_program_init(void)
257 * We ignore errors here since errors are normal if we
258 * are already running as non-root.
260 gid_t groups[] = { AID_INET, AID_WIFI, AID_KEYSTORE };
261 struct __user_cap_header_struct header;
262 struct __user_cap_data_struct cap;
264 setgroups(sizeof(groups)/sizeof(groups[0]), groups);
266 prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
271 header.version = _LINUX_CAPABILITY_VERSION;
273 cap.effective = cap.permitted =
274 (1 << CAP_NET_ADMIN) | (1 << CAP_NET_RAW);
276 capset(&header, &cap);
280 dl_list_init(&alloc_list);
281 #endif /* WPA_TRACE */
286 void os_program_deinit(void)
289 struct os_alloc_trace *a;
290 unsigned long total = 0;
291 dl_list_for_each(a, &alloc_list, struct os_alloc_trace, list) {
293 if (a->magic != ALLOC_MAGIC) {
294 wpa_printf(MSG_INFO, "MEMLEAK[%p]: invalid magic 0x%x "
296 a, a->magic, (unsigned long) a->len);
299 wpa_printf(MSG_INFO, "MEMLEAK[%p]: len %lu",
300 a, (unsigned long) a->len);
301 wpa_trace_dump("memleak", a);
304 wpa_printf(MSG_INFO, "MEMLEAK: total %lu bytes",
305 (unsigned long) total);
306 #endif /* WPA_TRACE */
310 int os_setenv(const char *name, const char *value, int overwrite)
312 return setenv(name, value, overwrite);
316 int os_unsetenv(const char *name)
318 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__) || \
323 return unsetenv(name);
328 char * os_readfile(const char *name, size_t *len)
334 f = fopen(name, "rb");
338 if (fseek(f, 0, SEEK_END) < 0 || (pos = ftell(f)) < 0) {
343 if (fseek(f, 0, SEEK_SET) < 0) {
348 buf = os_malloc(*len);
354 if (fread(buf, 1, *len, f) != *len) {
367 void * os_zalloc(size_t size)
369 return calloc(1, size);
371 #endif /* WPA_TRACE */
374 size_t os_strlcpy(char *dest, const char *src, size_t siz)
380 /* Copy string up to the maximum size of the dest buffer */
381 while (--left != 0) {
382 if ((*dest++ = *s++) == '\0')
388 /* Not enough room for the string; force NUL-termination */
392 ; /* determine total src string length */
401 void * os_malloc(size_t size)
403 struct os_alloc_trace *a;
404 a = malloc(sizeof(*a) + size);
407 a->magic = ALLOC_MAGIC;
408 dl_list_add(&alloc_list, &a->list);
415 void * os_realloc(void *ptr, size_t size)
417 struct os_alloc_trace *a;
422 return os_malloc(size);
424 a = (struct os_alloc_trace *) ptr - 1;
425 if (a->magic != ALLOC_MAGIC) {
426 wpa_printf(MSG_INFO, "REALLOC[%p]: invalid magic 0x%x%s",
428 a->magic == FREED_MAGIC ? " (already freed)" : "");
429 wpa_trace_show("Invalid os_realloc() call");
438 os_memcpy(n, a + 1, copy_len);
444 void os_free(void *ptr)
446 struct os_alloc_trace *a;
450 a = (struct os_alloc_trace *) ptr - 1;
451 if (a->magic != ALLOC_MAGIC) {
452 wpa_printf(MSG_INFO, "FREE[%p]: invalid magic 0x%x%s",
454 a->magic == FREED_MAGIC ? " (already freed)" : "");
455 wpa_trace_show("Invalid os_free() call");
458 dl_list_del(&a->list);
459 a->magic = FREED_MAGIC;
461 wpa_trace_check_ref(ptr);
466 void * os_zalloc(size_t size)
468 void *ptr = os_malloc(size);
470 os_memset(ptr, 0, size);
475 char * os_strdup(const char *s)
480 d = os_malloc(len + 1);
483 os_memcpy(d, s, len);
488 #endif /* WPA_TRACE */