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.
19 void os_sleep(os_time_t sec, os_time_t usec)
28 int os_get_time(struct os_time *t)
32 res = gettimeofday(&tv, NULL);
39 int os_mktime(int year, int month, int day, int hour, int min, int sec,
43 time_t t_local, t1, t2;
46 if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
47 hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 ||
51 memset(&tm, 0, sizeof(tm));
52 tm.tm_year = year - 1900;
53 tm.tm_mon = month - 1;
59 t_local = mktime(&tm);
61 /* figure out offset to UTC */
62 tm1 = localtime(&t_local);
65 tm1 = gmtime(&t_local);
74 *t = (os_time_t) t_local - tz_offset;
81 static int os_daemon(int nochdir, int noclose)
88 devnull = open("/dev/null", O_RDWR);
92 if (dup2(devnull, STDIN_FILENO) < 0) {
97 if (dup2(devnull, STDOUT_FILENO) < 0) {
102 if (dup2(devnull, STDERR_FILENO) < 0) {
109 #else /* __APPLE__ */
110 #define os_daemon daemon
111 #endif /* __APPLE__ */
114 int os_daemonize(const char *pid_file)
118 #else /* __uClinux__ */
119 if (os_daemon(0, 0)) {
125 FILE *f = fopen(pid_file, "w");
127 fprintf(f, "%u\n", getpid());
133 #endif /* __uClinux__ */
137 void os_daemonize_terminate(const char *pid_file)
144 int os_get_random(unsigned char *buf, size_t len)
149 f = fopen("/dev/urandom", "rb");
151 printf("Could not open /dev/urandom.\n");
155 rc = fread(buf, 1, len, f);
158 return rc != len ? -1 : 0;
162 unsigned long os_random(void)
168 char * os_rel2abs_path(const char *rel_path)
170 char *buf = NULL, *cwd, *ret;
171 size_t len = 128, cwd_len, rel_len, ret_len;
174 if (rel_path[0] == '/')
175 return strdup(rel_path);
181 cwd = getcwd(buf, len);
185 if (last_errno != ERANGE)
196 cwd_len = strlen(cwd);
197 rel_len = strlen(rel_path);
198 ret_len = cwd_len + 1 + rel_len + 1;
199 ret = malloc(ret_len);
201 memcpy(ret, cwd, cwd_len);
203 memcpy(ret + cwd_len + 1, rel_path, rel_len);
204 ret[ret_len - 1] = '\0';
211 int os_program_init(void)
217 void os_program_deinit(void)
222 int os_setenv(const char *name, const char *value, int overwrite)
224 return setenv(name, value, overwrite);
228 int os_unsetenv(const char *name)
230 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__)
234 return unsetenv(name);
239 char * os_readfile(const char *name, size_t *len)
244 f = fopen(name, "rb");
248 fseek(f, 0, SEEK_END);
250 fseek(f, 0, SEEK_SET);
258 if (fread(buf, 1, *len, f) != *len) {
270 void * os_zalloc(size_t size)
272 return calloc(1, size);
276 size_t os_strlcpy(char *dest, const char *src, size_t siz)
282 /* Copy string up to the maximum size of the dest buffer */
283 while (--left != 0) {
284 if ((*dest++ = *s++) == '\0')
290 /* Not enough room for the string; force NUL-termination */
294 ; /* determine total src string length */