~ubuntu-branches/ubuntu/hardy/wpasupplicant/hardy

« back to all changes in this revision

Viewing changes to src/utils/os_unix.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler, Alexander Sack
  • Date: 2007-08-26 16:06:57 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20070826160657-2m8pxoweuxe8f93t
Tags: 0.6.0+0.5.8-0ubuntu1
* New upstream release
* remove patch 11_erroneous_manpage_ref, applied upstream
* remove patch 25_wpas_dbus_unregister_iface_fix, applied upstream

[ Alexander Sack ]
* bumping upstream version to replace development version 0.6.0 with
  this package from stable release branch.
* attempt to fix wierd timeout and high latency issues by going
  back to stable upstream version (0.5.9) (LP: #140763,
  LP: #141233).

Show diffs side-by-side

added added

removed removed

Lines of Context:
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;
43
 
 
44
 
        if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
45
 
            hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 ||
46
 
            sec > 60)
47
 
                return -1;
48
 
 
49
 
        memset(&tm, 0, sizeof(tm));
50
 
        tm.tm_year = year - 1900;
51
 
        tm.tm_mon = month - 1;
52
 
        tm.tm_mday = day;
53
 
        tm.tm_hour = hour;
54
 
        tm.tm_min = min;
55
 
        tm.tm_sec = sec;
56
 
 
57
 
        *t = (os_time_t) mktime(&tm);
58
 
        return 0;
59
 
}
60
 
 
61
 
 
62
 
int os_daemonize(const char *pid_file)
63
 
{
64
 
#ifdef __unclinux
65
 
        return -1;
66
 
#else /* __uclinux */
67
 
        if (daemon(0, 0)) {
68
 
                perror("daemon");
69
 
                return -1;
70
 
        }
71
 
 
72
 
        if (pid_file) {
73
 
                FILE *f = fopen(pid_file, "w");
74
 
                if (f) {
75
 
                        fprintf(f, "%u\n", getpid());
76
 
                        fclose(f);
77
 
                }
78
 
        }
79
 
 
80
 
        return -0;
81
 
#endif /* __uclinux */
82
 
}
83
 
 
84
 
 
85
 
void os_daemonize_terminate(const char *pid_file)
86
 
{
87
 
        if (pid_file)
88
 
                unlink(pid_file);
89
 
}
90
 
 
91
 
 
92
 
int os_get_random(unsigned char *buf, size_t len)
93
 
{
94
 
        FILE *f;
95
 
        size_t rc;
96
 
 
97
 
        f = fopen("/dev/urandom", "rb");
98
 
        if (f == NULL) {
99
 
                printf("Could not open /dev/urandom.\n");
100
 
                return -1;
101
 
        }
102
 
 
103
 
        rc = fread(buf, 1, len, f);
104
 
        fclose(f);
105
 
 
106
 
        return rc != len ? -1 : 0;
107
 
}
108
 
 
109
 
 
110
 
unsigned long os_random(void)
111
 
{
112
 
        return random();
113
 
}
114
 
 
115
 
 
116
 
char * os_rel2abs_path(const char *rel_path)
117
 
{
118
 
        char *buf = NULL, *cwd, *ret;
119
 
        size_t len = 128, cwd_len, rel_len, ret_len;
120
 
        int last_errno;
121
 
 
122
 
        if (rel_path[0] == '/')
123
 
                return strdup(rel_path);
124
 
 
125
 
        for (;;) {
126
 
                buf = malloc(len);
127
 
                if (buf == NULL)
128
 
                        return NULL;
129
 
                cwd = getcwd(buf, len);
130
 
                if (cwd == NULL) {
131
 
                        last_errno = errno;
132
 
                        free(buf);
133
 
                        if (last_errno != ERANGE)
134
 
                                return NULL;
135
 
                        len *= 2;
136
 
                        if (len > 2000)
137
 
                                return NULL;
138
 
                } else {
139
 
                        buf[len - 1] = '\0';
140
 
                        break;
141
 
                }
142
 
        }
143
 
 
144
 
        cwd_len = strlen(cwd);
145
 
        rel_len = strlen(rel_path);
146
 
        ret_len = cwd_len + 1 + rel_len + 1;
147
 
        ret = malloc(ret_len);
148
 
        if (ret) {
149
 
                memcpy(ret, cwd, cwd_len);
150
 
                ret[cwd_len] = '/';
151
 
                memcpy(ret + cwd_len + 1, rel_path, rel_len);
152
 
                ret[ret_len - 1] = '\0';
153
 
        }
154
 
        free(buf);
155
 
        return ret;
156
 
}
157
 
 
158
 
 
159
 
int os_program_init(void)
160
 
{
161
 
        return 0;
162
 
}
163
 
 
164
 
 
165
 
void os_program_deinit(void)
166
 
{
167
 
}
168
 
 
169
 
 
170
 
int os_setenv(const char *name, const char *value, int overwrite)
171
 
{
172
 
        return setenv(name, value, overwrite);
173
 
}
174
 
 
175
 
 
176
 
int os_unsetenv(const char *name)
177
 
{
178
 
#if defined(__FreeBSD__) || defined(__NetBSD__)
179
 
        unsetenv(name);
180
 
        return 0;
181
 
#else
182
 
        return unsetenv(name);
183
 
#endif
184
 
}
185
 
 
186
 
 
187
 
char * os_readfile(const char *name, size_t *len)
188
 
{
189
 
        FILE *f;
190
 
        char *buf;
191
 
 
192
 
        f = fopen(name, "rb");
193
 
        if (f == NULL)
194
 
                return NULL;
195
 
 
196
 
        fseek(f, 0, SEEK_END);
197
 
        *len = ftell(f);
198
 
        fseek(f, 0, SEEK_SET);
199
 
 
200
 
        buf = malloc(*len);
201
 
        if (buf == NULL) {
202
 
                fclose(f);
203
 
                return NULL;
204
 
        }
205
 
 
206
 
        fread(buf, 1, *len, f);
207
 
        fclose(f);
208
 
 
209
 
        return buf;
210
 
}
211
 
 
212
 
 
213
 
void * os_zalloc(size_t size)
214
 
{
215
 
        return calloc(1, size);
216
 
}
217
 
 
218
 
 
219
 
size_t os_strlcpy(char *dest, const char *src, size_t siz)
220
 
{
221
 
        const char *s = src;
222
 
        size_t left = siz;
223
 
 
224
 
        if (left) {
225
 
                /* Copy string up to the maximum size of the dest buffer */
226
 
                while (--left != 0) {
227
 
                        if ((*dest++ = *s++) == '\0')
228
 
                                break;
229
 
                }
230
 
        }
231
 
 
232
 
        if (left == 0) {
233
 
                /* Not enough room for the string; force NUL-termination */
234
 
                if (siz != 0)
235
 
                        *dest = '\0';
236
 
                while (*s++)
237
 
                        ; /* determine total src string length */
238
 
        }
239
 
 
240
 
        return s - src - 1;
241
 
}