a42819530b6752c50bc5af347c60568ab136aa3c
[libeap.git] / src / utils / os_unix.c
1 /*
2  * wpa_supplicant/hostapd / OS specific functions for UNIX/POSIX systems
3  * Copyright (c) 2005-2006, Jouni Malinen <j@w1.fi>
4  *
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.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #include "os.h"
18
19 void os_sleep(os_time_t sec, os_time_t usec)
20 {
21         if (sec)
22                 sleep(sec);
23         if (usec)
24                 usleep(usec);
25 }
26
27
28 int os_get_time(struct os_time *t)
29 {
30         int res;
31         struct timeval tv;
32         res = gettimeofday(&tv, NULL);
33         t->sec = tv.tv_sec;
34         t->usec = tv.tv_usec;
35         return res;
36 }
37
38
39 int os_mktime(int year, int month, int day, int hour, int min, int sec,
40               os_time_t *t)
41 {
42         struct tm tm, *tm1;
43         time_t t_local, t1, t2;
44         os_time_t tz_offset;
45
46         if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
47             hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 ||
48             sec > 60)
49                 return -1;
50
51         memset(&tm, 0, sizeof(tm));
52         tm.tm_year = year - 1900;
53         tm.tm_mon = month - 1;
54         tm.tm_mday = day;
55         tm.tm_hour = hour;
56         tm.tm_min = min;
57         tm.tm_sec = sec;
58
59         t_local = mktime(&tm);
60
61         /* figure out offset to UTC */
62         tm1 = localtime(&t_local);
63         if (tm1) {
64                 t1 = mktime(tm1);
65                 tm1 = gmtime(&t_local);
66                 if (tm1) {
67                         t2 = mktime(tm1);
68                         tz_offset = t2 - t1;
69                 } else
70                         tz_offset = 0;
71         } else
72                 tz_offset = 0;
73
74         *t = (os_time_t) t_local - tz_offset;
75         return 0;
76 }
77
78
79 int os_daemonize(const char *pid_file)
80 {
81 #ifdef __unclinux
82         return -1;
83 #else /* __uclinux */
84         if (daemon(0, 0)) {
85                 perror("daemon");
86                 return -1;
87         }
88
89         if (pid_file) {
90                 FILE *f = fopen(pid_file, "w");
91                 if (f) {
92                         fprintf(f, "%u\n", getpid());
93                         fclose(f);
94                 }
95         }
96
97         return -0;
98 #endif /* __uclinux */
99 }
100
101
102 void os_daemonize_terminate(const char *pid_file)
103 {
104         if (pid_file)
105                 unlink(pid_file);
106 }
107
108
109 int os_get_random(unsigned char *buf, size_t len)
110 {
111         FILE *f;
112         size_t rc;
113
114         f = fopen("/dev/urandom", "rb");
115         if (f == NULL) {
116                 printf("Could not open /dev/urandom.\n");
117                 return -1;
118         }
119
120         rc = fread(buf, 1, len, f);
121         fclose(f);
122
123         return rc != len ? -1 : 0;
124 }
125
126
127 unsigned long os_random(void)
128 {
129         return random();
130 }
131
132
133 char * os_rel2abs_path(const char *rel_path)
134 {
135         char *buf = NULL, *cwd, *ret;
136         size_t len = 128, cwd_len, rel_len, ret_len;
137         int last_errno;
138
139         if (rel_path[0] == '/')
140                 return strdup(rel_path);
141
142         for (;;) {
143                 buf = malloc(len);
144                 if (buf == NULL)
145                         return NULL;
146                 cwd = getcwd(buf, len);
147                 if (cwd == NULL) {
148                         last_errno = errno;
149                         free(buf);
150                         if (last_errno != ERANGE)
151                                 return NULL;
152                         len *= 2;
153                         if (len > 2000)
154                                 return NULL;
155                 } else {
156                         buf[len - 1] = '\0';
157                         break;
158                 }
159         }
160
161         cwd_len = strlen(cwd);
162         rel_len = strlen(rel_path);
163         ret_len = cwd_len + 1 + rel_len + 1;
164         ret = malloc(ret_len);
165         if (ret) {
166                 memcpy(ret, cwd, cwd_len);
167                 ret[cwd_len] = '/';
168                 memcpy(ret + cwd_len + 1, rel_path, rel_len);
169                 ret[ret_len - 1] = '\0';
170         }
171         free(buf);
172         return ret;
173 }
174
175
176 int os_program_init(void)
177 {
178         return 0;
179 }
180
181
182 void os_program_deinit(void)
183 {
184 }
185
186
187 int os_setenv(const char *name, const char *value, int overwrite)
188 {
189         return setenv(name, value, overwrite);
190 }
191
192
193 int os_unsetenv(const char *name)
194 {
195 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__)
196         unsetenv(name);
197         return 0;
198 #else
199         return unsetenv(name);
200 #endif
201 }
202
203
204 char * os_readfile(const char *name, size_t *len)
205 {
206         FILE *f;
207         char *buf;
208
209         f = fopen(name, "rb");
210         if (f == NULL)
211                 return NULL;
212
213         fseek(f, 0, SEEK_END);
214         *len = ftell(f);
215         fseek(f, 0, SEEK_SET);
216
217         buf = malloc(*len);
218         if (buf == NULL) {
219                 fclose(f);
220                 return NULL;
221         }
222
223         if (fread(buf, 1, *len, f) != *len) {
224                 fclose(f);
225                 free(buf);
226                 return NULL;
227         }
228
229         fclose(f);
230
231         return buf;
232 }
233
234
235 void * os_zalloc(size_t size)
236 {
237         return calloc(1, size);
238 }
239
240
241 size_t os_strlcpy(char *dest, const char *src, size_t siz)
242 {
243         const char *s = src;
244         size_t left = siz;
245
246         if (left) {
247                 /* Copy string up to the maximum size of the dest buffer */
248                 while (--left != 0) {
249                         if ((*dest++ = *s++) == '\0')
250                                 break;
251                 }
252         }
253
254         if (left == 0) {
255                 /* Not enough room for the string; force NUL-termination */
256                 if (siz != 0)
257                         *dest = '\0';
258                 while (*s++)
259                         ; /* determine total src string length */
260         }
261
262         return s - src - 1;
263 }