• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * wpa_supplicant/hostapd / Internal implementation of OS specific functions
3  * Copyright (c) 2005-2006, 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  * 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.
11  *
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.
17  */
18 
19 #include "includes.h"
20 #include <time.h>
21 #include <sys/wait.h>
22 
23 #undef OS_REJECT_C_LIB_FUNCTIONS
24 #include "common.h"
25 
os_sleep(os_time_t sec,os_time_t usec)26 void os_sleep(os_time_t sec, os_time_t usec)
27 {
28 #if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200809L)
29 	const struct timespec req = { sec, usec * 1000 };
30 
31 	nanosleep(&req, NULL);
32 #else
33 	if (sec)
34 		sleep(sec);
35 	if (usec)
36 		usleep(usec);
37 #endif
38 }
39 
40 
os_get_time(struct os_time * t)41 int os_get_time(struct os_time *t)
42 {
43 	int res;
44 	struct timeval tv;
45 	res = gettimeofday(&tv, NULL);
46 	t->sec = tv.tv_sec;
47 	t->usec = tv.tv_usec;
48 	return res;
49 }
50 
51 
os_get_reltime(struct os_reltime * t)52 int os_get_reltime(struct os_reltime *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 
os_mktime(int year,int month,int day,int hour,int min,int sec,os_time_t * t)63 int os_mktime(int year, int month, int day, int hour, int min, int sec,
64 	      os_time_t *t)
65 {
66 	struct tm tm;
67 
68 	if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
69 	    hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 ||
70 	    sec > 60)
71 		return -1;
72 
73 	os_memset(&tm, 0, sizeof(tm));
74 	tm.tm_year = year - 1900;
75 	tm.tm_mon = month - 1;
76 	tm.tm_mday = day;
77 	tm.tm_hour = hour;
78 	tm.tm_min = min;
79 	tm.tm_sec = sec;
80 
81 	*t = (os_time_t) mktime(&tm);
82 	return 0;
83 }
84 
85 
os_gmtime(os_time_t t,struct os_tm * tm)86 int os_gmtime(os_time_t t, struct os_tm *tm)
87 {
88 	struct tm *tm2;
89 	time_t t2 = t;
90 
91 	tm2 = gmtime(&t2);
92 	if (tm2 == NULL)
93 		return -1;
94 	tm->sec = tm2->tm_sec;
95 	tm->min = tm2->tm_min;
96 	tm->hour = tm2->tm_hour;
97 	tm->day = tm2->tm_mday;
98 	tm->month = tm2->tm_mon + 1;
99 	tm->year = tm2->tm_year + 1900;
100 	return 0;
101 }
102 
103 
os_daemonize(const char * pid_file)104 int os_daemonize(const char *pid_file)
105 {
106 	if (daemon(0, 0)) {
107 		wpa_printf(MSG_ERROR, "daemon: %s", strerror(errno));
108 		return -1;
109 	}
110 
111 	if (pid_file) {
112 		FILE *f = fopen(pid_file, "w");
113 		if (f) {
114 			fprintf(f, "%u\n", getpid());
115 			fclose(f);
116 		}
117 	}
118 
119 	return -0;
120 }
121 
122 
os_daemonize_terminate(const char * pid_file)123 void os_daemonize_terminate(const char *pid_file)
124 {
125 	if (pid_file)
126 		unlink(pid_file);
127 }
128 
129 
os_get_random(unsigned char * buf,size_t len)130 int os_get_random(unsigned char *buf, size_t len)
131 {
132 #if defined(TEST_FUZZ) || defined(CONFIG_TEST_RANDOM)
133   size_t i;
134 
135   for (i = 0; i < len; i++)
136     buf[i] = i & 0xff;
137   return 0;
138 #else /* TEST_FUZZ */
139 	FILE *f;
140 	size_t rc;
141 
142 	f = fopen("/dev/urandom", "rb");
143 	if (f == NULL) {
144 		printf("Could not open /dev/urandom.\n");
145 		return -1;
146 	}
147 
148 	rc = fread(buf, 1, len, f);
149 	fclose(f);
150 
151 	return rc != len ? -1 : 0;
152 #endif
153 }
154 
155 
os_random(void)156 unsigned long os_random(void)
157 {
158 	return random();
159 }
160 
161 
os_rel2abs_path(const char * rel_path)162 char * os_rel2abs_path(const char *rel_path)
163 {
164 	char *buf = NULL, *cwd, *ret;
165 	size_t len = 128, cwd_len, rel_len, ret_len;
166 
167 	if (rel_path[0] == '/')
168 		return os_strdup(rel_path);
169 
170 	for (;;) {
171 		buf = os_malloc(len);
172 		if (buf == NULL)
173 			return NULL;
174 		cwd = getcwd(buf, len);
175 		if (cwd == NULL) {
176 			os_free(buf);
177 			if (errno != ERANGE) {
178 				return NULL;
179 			}
180 			len *= 2;
181 		} else {
182 			break;
183 		}
184 	}
185 
186 	cwd_len = os_strlen(cwd);
187 	rel_len = os_strlen(rel_path);
188 	ret_len = cwd_len + 1 + rel_len + 1;
189 	ret = os_malloc(ret_len);
190 	if (ret) {
191 		os_memcpy(ret, cwd, cwd_len);
192 		ret[cwd_len] = '/';
193 		os_memcpy(ret + cwd_len + 1, rel_path, rel_len);
194 		ret[ret_len - 1] = '\0';
195 	}
196 	os_free(buf);
197 	return ret;
198 }
199 
200 
os_program_init(void)201 int os_program_init(void)
202 {
203 	return 0;
204 }
205 
206 
os_program_deinit(void)207 void os_program_deinit(void)
208 {
209 }
210 
211 
os_setenv(const char * name,const char * value,int overwrite)212 int os_setenv(const char *name, const char *value, int overwrite)
213 {
214 	return setenv(name, value, overwrite);
215 }
216 
217 
os_unsetenv(const char * name)218 int os_unsetenv(const char *name)
219 {
220 #if defined(__FreeBSD__) || defined(__NetBSD__)
221 	unsetenv(name);
222 	return 0;
223 #else
224 	return unsetenv(name);
225 #endif
226 }
227 
228 
os_readfile(const char * name,size_t * len)229 char * os_readfile(const char *name, size_t *len)
230 {
231 	FILE *f;
232 	char *buf;
233 
234 	f = fopen(name, "rb");
235 	if (f == NULL)
236 		return NULL;
237 
238 	fseek(f, 0, SEEK_END);
239 	*len = ftell(f);
240 	fseek(f, 0, SEEK_SET);
241 
242 	buf = os_malloc(*len);
243 	if (buf == NULL) {
244 		fclose(f);
245 		return NULL;
246 	}
247 
248 	if (fread(buf, 1, *len, f) != *len) {
249 		fclose(f);
250 		os_free(buf);
251 		return NULL;
252 	}
253 
254 	fclose(f);
255 
256 	return buf;
257 }
258 
259 
os_fdatasync(FILE * stream)260 int os_fdatasync(FILE *stream)
261 {
262 	return 0;
263 }
264 
265 
os_zalloc(size_t size)266 void * os_zalloc(size_t size)
267 {
268 	void *n = os_malloc(size);
269 	if (n)
270 		os_memset(n, 0, size);
271 	return n;
272 }
273 
274 
os_malloc(size_t size)275 void * os_malloc(size_t size)
276 {
277 	return malloc(size);
278 }
279 
280 
os_realloc(void * ptr,size_t size)281 void * os_realloc(void *ptr, size_t size)
282 {
283 	return realloc(ptr, size);
284 }
285 
286 
os_free(void * ptr)287 void os_free(void *ptr)
288 {
289 	free(ptr);
290 }
291 
292 
os_memcpy(void * dest,const void * src,size_t n)293 void * os_memcpy(void *dest, const void *src, size_t n)
294 {
295 	char *d = dest;
296 	const char *s = src;
297 	while (n--)
298 		*d++ = *s++;
299 	return dest;
300 }
301 
302 
os_memmove(void * dest,const void * src,size_t n)303 void * os_memmove(void *dest, const void *src, size_t n)
304 {
305 	if (dest < src)
306 		os_memcpy(dest, src, n);
307 	else {
308 		/* overlapping areas */
309 		char *d = (char *) dest + n;
310 		const char *s = (const char *) src + n;
311 		while (n--)
312 			*--d = *--s;
313 	}
314 	return dest;
315 }
316 
317 
os_memset(void * s,int c,size_t n)318 void * os_memset(void *s, int c, size_t n)
319 {
320 	char *p = s;
321 	while (n--)
322 		*p++ = c;
323 	return s;
324 }
325 
326 
os_memcmp(const void * s1,const void * s2,size_t n)327 int os_memcmp(const void *s1, const void *s2, size_t n)
328 {
329 	const unsigned char *p1 = s1, *p2 = s2;
330 
331 	if (n == 0)
332 		return 0;
333 
334 	while (*p1 == *p2) {
335 		p1++;
336 		p2++;
337 		n--;
338 		if (n == 0)
339 			return 0;
340 	}
341 
342 	return *p1 - *p2;
343 }
344 
345 
os_strdup(const char * s)346 char * os_strdup(const char *s)
347 {
348 	char *res;
349 	size_t len;
350 	if (s == NULL)
351 		return NULL;
352 	len = os_strlen(s);
353 	res = os_malloc(len + 1);
354 	if (res)
355 		os_memcpy(res, s, len + 1);
356 	return res;
357 }
358 
359 
os_strlen(const char * s)360 size_t os_strlen(const char *s)
361 {
362 	const char *p = s;
363 	while (*p)
364 		p++;
365 	return p - s;
366 }
367 
368 
os_strcasecmp(const char * s1,const char * s2)369 int os_strcasecmp(const char *s1, const char *s2)
370 {
371 	/*
372 	 * Ignoring case is not required for main functionality, so just use
373 	 * the case sensitive version of the function.
374 	 */
375 	return os_strcmp(s1, s2);
376 }
377 
378 
os_strncasecmp(const char * s1,const char * s2,size_t n)379 int os_strncasecmp(const char *s1, const char *s2, size_t n)
380 {
381 	/*
382 	 * Ignoring case is not required for main functionality, so just use
383 	 * the case sensitive version of the function.
384 	 */
385 	return os_strncmp(s1, s2, n);
386 }
387 
388 
os_strchr(const char * s,int c)389 char * os_strchr(const char *s, int c)
390 {
391 	while (*s) {
392 		if (*s == c)
393 			return (char *) s;
394 		s++;
395 	}
396 	return NULL;
397 }
398 
399 
os_strrchr(const char * s,int c)400 char * os_strrchr(const char *s, int c)
401 {
402 	const char *p = s;
403 	while (*p)
404 		p++;
405 	p--;
406 	while (p >= s) {
407 		if (*p == c)
408 			return (char *) p;
409 		p--;
410 	}
411 	return NULL;
412 }
413 
414 
os_strcmp(const char * s1,const char * s2)415 int os_strcmp(const char *s1, const char *s2)
416 {
417 	while (*s1 == *s2) {
418 		if (*s1 == '\0')
419 			break;
420 		s1++;
421 		s2++;
422 	}
423 
424 	return *s1 - *s2;
425 }
426 
427 
os_strncmp(const char * s1,const char * s2,size_t n)428 int os_strncmp(const char *s1, const char *s2, size_t n)
429 {
430 	if (n == 0)
431 		return 0;
432 
433 	while (*s1 == *s2) {
434 		if (*s1 == '\0')
435 			break;
436 		s1++;
437 		s2++;
438 		n--;
439 		if (n == 0)
440 			return 0;
441 	}
442 
443 	return *s1 - *s2;
444 }
445 
446 
os_strlcpy(char * dest,const char * src,size_t siz)447 size_t os_strlcpy(char *dest, const char *src, size_t siz)
448 {
449 	const char *s = src;
450 	size_t left = siz;
451 
452 	if (left) {
453 		/* Copy string up to the maximum size of the dest buffer */
454 		while (--left != 0) {
455 			if ((*dest++ = *s++) == '\0')
456 				break;
457 		}
458 	}
459 
460 	if (left == 0) {
461 		/* Not enough room for the string; force NUL-termination */
462 		if (siz != 0)
463 			*dest = '\0';
464 		while (*s++)
465 			; /* determine total src string length */
466 	}
467 
468 	return s - src - 1;
469 }
470 
471 
os_memcmp_const(const void * a,const void * b,size_t len)472 int os_memcmp_const(const void *a, const void *b, size_t len)
473 {
474 	const u8 *aa = a;
475 	const u8 *bb = b;
476 	size_t i;
477 	u8 res;
478 
479 	for (res = 0, i = 0; i < len; i++)
480 		res |= aa[i] ^ bb[i];
481 
482 	return res;
483 }
484 
485 
os_strstr(const char * haystack,const char * needle)486 char * os_strstr(const char *haystack, const char *needle)
487 {
488 	size_t len = os_strlen(needle);
489 	while (*haystack) {
490 		if (os_strncmp(haystack, needle, len) == 0)
491 			return (char *) haystack;
492 		haystack++;
493 	}
494 
495 	return NULL;
496 }
497 
498 
os_snprintf(char * str,size_t size,const char * format,...)499 int os_snprintf(char *str, size_t size, const char *format, ...)
500 {
501 	va_list ap;
502 	int ret;
503 
504 	/* See http://www.ijs.si/software/snprintf/ for portable
505 	 * implementation of snprintf.
506 	 */
507 
508 	va_start(ap, format);
509 	ret = vsnprintf(str, size, format, ap);
510 	va_end(ap);
511 	if (size > 0)
512 		str[size - 1] = '\0';
513 	return ret;
514 }
515 
516 
os_exec(const char * program,const char * arg,int wait_completion)517 int os_exec(const char *program, const char *arg, int wait_completion)
518 {
519 	pid_t pid;
520 	int pid_status;
521 
522 	pid = fork();
523 	if (pid < 0) {
524 		wpa_printf(MSG_ERROR, "fork: %s", strerror(errno));
525 		return -1;
526 	}
527 
528 	if (pid == 0) {
529 		/* run the external command in the child process */
530 		const int MAX_ARG = 30;
531 		char *_program, *_arg, *pos;
532 		char *argv[MAX_ARG + 1];
533 		int i;
534 
535 		_program = os_strdup(program);
536 		_arg = os_strdup(arg);
537 
538 		argv[0] = _program;
539 
540 		i = 1;
541 		pos = _arg;
542 		while (i < MAX_ARG && pos && *pos) {
543 			while (*pos == ' ')
544 				pos++;
545 			if (*pos == '\0')
546 				break;
547 			argv[i++] = pos;
548 			pos = os_strchr(pos, ' ');
549 			if (pos)
550 				*pos++ = '\0';
551 		}
552 		argv[i] = NULL;
553 
554 		execv(program, argv);
555 		wpa_printf(MSG_ERROR, "execv: %s", strerror(errno));
556 		os_free(_program);
557 		os_free(_arg);
558 		exit(0);
559 		return -1;
560 	}
561 
562 	if (wait_completion) {
563 		/* wait for the child process to complete in the parent */
564 		waitpid(pid, &pid_status, 0);
565 	}
566 
567 	return 0;
568 }
569