2 * wpa_supplicant/hostapd / OS specific functions for UNIX/POSIX systems
3 * Copyright (c) 2005-2006, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
20 #include <linux/capability.h>
21 #include <linux/prctl.h>
22 #include <private/android_filesystem_config.h>
25 void os_sleep(os_time_t sec, os_time_t usec)
34 int os_get_time(struct os_time *t)
38 res = gettimeofday(&tv, NULL);
45 int os_mktime(int year, int month, int day, int hour, int min, int sec,
49 time_t t_local, t1, t2;
52 if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
53 hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 ||
57 memset(&tm, 0, sizeof(tm));
58 tm.tm_year = year - 1900;
59 tm.tm_mon = month - 1;
65 t_local = mktime(&tm);
67 /* figure out offset to UTC */
68 tm1 = localtime(&t_local);
71 tm1 = gmtime(&t_local);
80 *t = (os_time_t) t_local - tz_offset;
85 int os_daemonize(const char *pid_file)
93 FILE *f = fopen(pid_file, "w");
95 fprintf(f, "%u\n", getpid());
104 void os_daemonize_terminate(const char *pid_file)
111 int os_get_random(unsigned char *buf, size_t len)
116 f = fopen("/dev/urandom", "rb");
118 printf("Could not open /dev/urandom.\n");
122 rc = fread(buf, 1, len, f);
125 return rc != len ? -1 : 0;
129 unsigned long os_random(void)
135 char * os_rel2abs_path(const char *rel_path)
137 char *buf = NULL, *cwd, *ret;
138 size_t len = 128, cwd_len, rel_len, ret_len;
141 if (rel_path[0] == '/')
142 return strdup(rel_path);
148 cwd = getcwd(buf, len);
152 if (last_errno != ERANGE)
163 cwd_len = strlen(cwd);
164 rel_len = strlen(rel_path);
165 ret_len = cwd_len + 1 + rel_len + 1;
166 ret = malloc(ret_len);
168 memcpy(ret, cwd, cwd_len);
170 memcpy(ret + cwd_len + 1, rel_path, rel_len);
171 ret[ret_len - 1] = '\0';
178 int os_program_init(void)
181 /* We ignore errors here since errors are normal if we
182 * are already running as non-root.
184 gid_t groups[] = { AID_INET, AID_WIFI, AID_KEYSTORE };
185 setgroups(sizeof(groups)/sizeof(groups[0]), groups);
187 prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
192 struct __user_cap_header_struct header;
193 struct __user_cap_data_struct cap;
194 header.version = _LINUX_CAPABILITY_VERSION;
196 cap.effective = cap.permitted =
197 (1 << CAP_NET_ADMIN) | (1 << CAP_NET_RAW);
199 capset(&header, &cap);
206 void os_program_deinit(void)
211 int os_setenv(const char *name, const char *value, int overwrite)
213 return setenv(name, value, overwrite);
217 int os_unsetenv(const char *name)
219 #if defined(__FreeBSD__) || defined(__NetBSD__)
223 return unsetenv(name);
228 char * os_readfile(const char *name, size_t *len)
233 f = fopen(name, "rb");
237 fseek(f, 0, SEEK_END);
239 fseek(f, 0, SEEK_SET);
247 if (fread(buf, 1, *len, f) != *len) {
259 void * os_zalloc(size_t size)
261 return calloc(1, size);