os_unix.c revision 68d0e3ed07847339aedfac8e02f50db68c702e52
1/*
2 * OS specific functions for UNIX/POSIX systems
3 * Copyright (c) 2005-2009, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9#include "includes.h"
10
11#include <time.h>
12
13#ifdef ANDROID
14#include <sys/capability.h>
15#include <linux/prctl.h>
16#include <private/android_filesystem_config.h>
17#endif /* ANDROID */
18
19#include "os.h"
20#include "common.h"
21
22#ifdef WPA_TRACE
23
24#include "wpa_debug.h"
25#include "trace.h"
26#include "list.h"
27
28static struct dl_list alloc_list;
29
30#define ALLOC_MAGIC 0xa84ef1b2
31#define FREED_MAGIC 0x67fd487a
32
33struct os_alloc_trace {
34	unsigned int magic;
35	struct dl_list list;
36	size_t len;
37	WPA_TRACE_INFO
38};
39
40#endif /* WPA_TRACE */
41
42
43void os_sleep(os_time_t sec, os_time_t usec)
44{
45	if (sec)
46		sleep(sec);
47	if (usec)
48		usleep(usec);
49}
50
51
52int os_get_time(struct os_time *t)
53{
54	int res;
55	struct timeval tv;
56	res = gettimeofday(&tv, NULL);
57	t->sec = tv.tv_sec;
58	t->usec = tv.tv_usec;
59	return res;
60}
61
62
63int os_mktime(int year, int month, int day, int hour, int min, int sec,
64	      os_time_t *t)
65{
66	struct tm tm, *tm1;
67	time_t t_local, t1, t2;
68	os_time_t tz_offset;
69
70	if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
71	    hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 ||
72	    sec > 60)
73		return -1;
74
75	memset(&tm, 0, sizeof(tm));
76	tm.tm_year = year - 1900;
77	tm.tm_mon = month - 1;
78	tm.tm_mday = day;
79	tm.tm_hour = hour;
80	tm.tm_min = min;
81	tm.tm_sec = sec;
82
83	t_local = mktime(&tm);
84
85	/* figure out offset to UTC */
86	tm1 = localtime(&t_local);
87	if (tm1) {
88		t1 = mktime(tm1);
89		tm1 = gmtime(&t_local);
90		if (tm1) {
91			t2 = mktime(tm1);
92			tz_offset = t2 - t1;
93		} else
94			tz_offset = 0;
95	} else
96		tz_offset = 0;
97
98	*t = (os_time_t) t_local - tz_offset;
99	return 0;
100}
101
102
103int os_gmtime(os_time_t t, struct os_tm *tm)
104{
105	struct tm *tm2;
106	time_t t2 = t;
107
108	tm2 = gmtime(&t2);
109	if (tm2 == NULL)
110		return -1;
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;
117	return 0;
118}
119
120
121#ifdef __APPLE__
122#include <fcntl.h>
123static int os_daemon(int nochdir, int noclose)
124{
125	int devnull;
126
127	if (chdir("/") < 0)
128		return -1;
129
130	devnull = open("/dev/null", O_RDWR);
131	if (devnull < 0)
132		return -1;
133
134	if (dup2(devnull, STDIN_FILENO) < 0) {
135		close(devnull);
136		return -1;
137	}
138
139	if (dup2(devnull, STDOUT_FILENO) < 0) {
140		close(devnull);
141		return -1;
142	}
143
144	if (dup2(devnull, STDERR_FILENO) < 0) {
145		close(devnull);
146		return -1;
147	}
148
149	return 0;
150}
151#else /* __APPLE__ */
152#define os_daemon daemon
153#endif /* __APPLE__ */
154
155
156int os_daemonize(const char *pid_file)
157{
158#if defined(__uClinux__) || defined(__sun__)
159	return -1;
160#else /* defined(__uClinux__) || defined(__sun__) */
161	if (os_daemon(0, 0)) {
162		perror("daemon");
163		return -1;
164	}
165
166	if (pid_file) {
167		FILE *f = fopen(pid_file, "w");
168		if (f) {
169			fprintf(f, "%u\n", getpid());
170			fclose(f);
171		}
172	}
173
174	return -0;
175#endif /* defined(__uClinux__) || defined(__sun__) */
176}
177
178
179void os_daemonize_terminate(const char *pid_file)
180{
181	if (pid_file)
182		unlink(pid_file);
183}
184
185
186int os_get_random(unsigned char *buf, size_t len)
187{
188	FILE *f;
189	size_t rc;
190
191	f = fopen("/dev/urandom", "rb");
192	if (f == NULL) {
193		printf("Could not open /dev/urandom.\n");
194		return -1;
195	}
196
197	rc = fread(buf, 1, len, f);
198	fclose(f);
199
200	return rc != len ? -1 : 0;
201}
202
203
204unsigned long os_random(void)
205{
206	return random();
207}
208
209
210char * os_rel2abs_path(const char *rel_path)
211{
212	char *buf = NULL, *cwd, *ret;
213	size_t len = 128, cwd_len, rel_len, ret_len;
214	int last_errno;
215
216	if (!rel_path)
217		return NULL;
218
219	if (rel_path[0] == '/')
220		return os_strdup(rel_path);
221
222	for (;;) {
223		buf = os_malloc(len);
224		if (buf == NULL)
225			return NULL;
226		cwd = getcwd(buf, len);
227		if (cwd == NULL) {
228			last_errno = errno;
229			os_free(buf);
230			if (last_errno != ERANGE)
231				return NULL;
232			len *= 2;
233			if (len > 2000)
234				return NULL;
235		} else {
236			buf[len - 1] = '\0';
237			break;
238		}
239	}
240
241	cwd_len = os_strlen(cwd);
242	rel_len = os_strlen(rel_path);
243	ret_len = cwd_len + 1 + rel_len + 1;
244	ret = os_malloc(ret_len);
245	if (ret) {
246		os_memcpy(ret, cwd, cwd_len);
247		ret[cwd_len] = '/';
248		os_memcpy(ret + cwd_len + 1, rel_path, rel_len);
249		ret[ret_len - 1] = '\0';
250	}
251	os_free(buf);
252	return ret;
253}
254
255
256int os_program_init(void)
257{
258#ifdef ANDROID
259	/*
260	 * We ignore errors here since errors are normal if we
261	 * are already running as non-root.
262	 */
263#ifdef ANDROID_SETGROUPS_OVERRIDE
264	gid_t groups[] = { ANDROID_SETGROUPS_OVERRIDE };
265#else /* ANDROID_SETGROUPS_OVERRIDE */
266	gid_t groups[] = { AID_INET, AID_WIFI, AID_KEYSTORE };
267#endif /* ANDROID_SETGROUPS_OVERRIDE */
268	struct __user_cap_header_struct header;
269	struct __user_cap_data_struct cap;
270
271	setgroups(ARRAY_SIZE(groups), groups);
272
273	prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
274
275	setgid(AID_WIFI);
276	setuid(AID_WIFI);
277
278	header.version = _LINUX_CAPABILITY_VERSION;
279	header.pid = 0;
280	cap.effective = cap.permitted =
281		(1 << CAP_NET_ADMIN) | (1 << CAP_NET_RAW);
282	cap.inheritable = 0;
283	capset(&header, &cap);
284#endif /* ANDROID */
285
286#ifdef WPA_TRACE
287	dl_list_init(&alloc_list);
288#endif /* WPA_TRACE */
289	return 0;
290}
291
292
293void os_program_deinit(void)
294{
295#ifdef WPA_TRACE
296	struct os_alloc_trace *a;
297	unsigned long total = 0;
298	dl_list_for_each(a, &alloc_list, struct os_alloc_trace, list) {
299		total += a->len;
300		if (a->magic != ALLOC_MAGIC) {
301			wpa_printf(MSG_INFO, "MEMLEAK[%p]: invalid magic 0x%x "
302				   "len %lu",
303				   a, a->magic, (unsigned long) a->len);
304			continue;
305		}
306		wpa_printf(MSG_INFO, "MEMLEAK[%p]: len %lu",
307			   a, (unsigned long) a->len);
308		wpa_trace_dump("memleak", a);
309	}
310	if (total)
311		wpa_printf(MSG_INFO, "MEMLEAK: total %lu bytes",
312			   (unsigned long) total);
313#endif /* WPA_TRACE */
314}
315
316
317int os_setenv(const char *name, const char *value, int overwrite)
318{
319	return setenv(name, value, overwrite);
320}
321
322
323int os_unsetenv(const char *name)
324{
325#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__) || \
326    defined(__OpenBSD__)
327	unsetenv(name);
328	return 0;
329#else
330	return unsetenv(name);
331#endif
332}
333
334
335char * os_readfile(const char *name, size_t *len)
336{
337	FILE *f;
338	char *buf;
339	long pos;
340
341	f = fopen(name, "rb");
342	if (f == NULL)
343		return NULL;
344
345	if (fseek(f, 0, SEEK_END) < 0 || (pos = ftell(f)) < 0) {
346		fclose(f);
347		return NULL;
348	}
349	*len = pos;
350	if (fseek(f, 0, SEEK_SET) < 0) {
351		fclose(f);
352		return NULL;
353	}
354
355	buf = os_malloc(*len);
356	if (buf == NULL) {
357		fclose(f);
358		return NULL;
359	}
360
361	if (fread(buf, 1, *len, f) != *len) {
362		fclose(f);
363		os_free(buf);
364		return NULL;
365	}
366
367	fclose(f);
368
369	return buf;
370}
371
372
373#ifndef WPA_TRACE
374void * os_zalloc(size_t size)
375{
376	return calloc(1, size);
377}
378#endif /* WPA_TRACE */
379
380
381size_t os_strlcpy(char *dest, const char *src, size_t siz)
382{
383	const char *s = src;
384	size_t left = siz;
385
386	if (left) {
387		/* Copy string up to the maximum size of the dest buffer */
388		while (--left != 0) {
389			if ((*dest++ = *s++) == '\0')
390				break;
391		}
392	}
393
394	if (left == 0) {
395		/* Not enough room for the string; force NUL-termination */
396		if (siz != 0)
397			*dest = '\0';
398		while (*s++)
399			; /* determine total src string length */
400	}
401
402	return s - src - 1;
403}
404
405
406#ifdef WPA_TRACE
407
408void * os_malloc(size_t size)
409{
410	struct os_alloc_trace *a;
411	a = malloc(sizeof(*a) + size);
412	if (a == NULL)
413		return NULL;
414	a->magic = ALLOC_MAGIC;
415	dl_list_add(&alloc_list, &a->list);
416	a->len = size;
417	wpa_trace_record(a);
418	return a + 1;
419}
420
421
422void * os_realloc(void *ptr, size_t size)
423{
424	struct os_alloc_trace *a;
425	size_t copy_len;
426	void *n;
427
428	if (ptr == NULL)
429		return os_malloc(size);
430
431	a = (struct os_alloc_trace *) ptr - 1;
432	if (a->magic != ALLOC_MAGIC) {
433		wpa_printf(MSG_INFO, "REALLOC[%p]: invalid magic 0x%x%s",
434			   a, a->magic,
435			   a->magic == FREED_MAGIC ? " (already freed)" : "");
436		wpa_trace_show("Invalid os_realloc() call");
437		abort();
438	}
439	n = os_malloc(size);
440	if (n == NULL)
441		return NULL;
442	copy_len = a->len;
443	if (copy_len > size)
444		copy_len = size;
445	os_memcpy(n, a + 1, copy_len);
446	os_free(ptr);
447	return n;
448}
449
450
451void os_free(void *ptr)
452{
453	struct os_alloc_trace *a;
454
455	if (ptr == NULL)
456		return;
457	a = (struct os_alloc_trace *) ptr - 1;
458	if (a->magic != ALLOC_MAGIC) {
459		wpa_printf(MSG_INFO, "FREE[%p]: invalid magic 0x%x%s",
460			   a, a->magic,
461			   a->magic == FREED_MAGIC ? " (already freed)" : "");
462		wpa_trace_show("Invalid os_free() call");
463		abort();
464	}
465	dl_list_del(&a->list);
466	a->magic = FREED_MAGIC;
467
468	wpa_trace_check_ref(ptr);
469	free(a);
470}
471
472
473void * os_zalloc(size_t size)
474{
475	void *ptr = os_malloc(size);
476	if (ptr)
477		os_memset(ptr, 0, size);
478	return ptr;
479}
480
481
482char * os_strdup(const char *s)
483{
484	size_t len;
485	char *d;
486	len = os_strlen(s);
487	d = os_malloc(len + 1);
488	if (d == NULL)
489		return NULL;
490	os_memcpy(d, s, len);
491	d[len] = '\0';
492	return d;
493}
494
495#endif /* WPA_TRACE */
496