]>
Commit | Line | Data |
---|---|---|
552cc11b | 1 | /* |
d25ce370 | 2 | * Copyright (C) 2008-2009 Tobias Brunner |
552cc11b MW |
3 | * Copyright (C) 2005-2008 Martin Willi |
4 | * Hochschule fuer Technik Rapperswil | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2 of the License, or (at your | |
9 | * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
13 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | * for more details. | |
552cc11b MW |
15 | */ |
16 | ||
17 | #include "utils.h" | |
18 | ||
6c20579a | 19 | #include <sys/stat.h> |
552cc11b | 20 | #include <string.h> |
552cc11b | 21 | #include <stdio.h> |
6c20579a | 22 | #include <unistd.h> |
74b14b40 | 23 | #include <stdint.h> |
d24a74c5 | 24 | #include <limits.h> |
6c20579a | 25 | #include <dirent.h> |
f464d750 | 26 | #include <time.h> |
552cc11b | 27 | |
fac3bfa5 TB |
28 | #include "enum.h" |
29 | #include "debug.h" | |
552cc11b MW |
30 | |
31 | ENUM(status_names, SUCCESS, DESTROY_ME, | |
32 | "SUCCESS", | |
33 | "FAILED", | |
34 | "OUT_OF_RES", | |
35 | "ALREADY_DONE", | |
36 | "NOT_SUPPORTED", | |
37 | "INVALID_ARG", | |
38 | "NOT_FOUND", | |
39 | "PARSE_ERROR", | |
40 | "VERIFY_ERROR", | |
41 | "INVALID_STATE", | |
42 | "DESTROY_ME", | |
43 | "NEED_MORE", | |
44 | ); | |
45 | ||
46 | /** | |
47 | * Described in header. | |
48 | */ | |
49 | void *clalloc(void * pointer, size_t size) | |
50 | { | |
51 | void *data; | |
52 | data = malloc(size); | |
7daf5226 | 53 | |
552cc11b | 54 | memcpy(data, pointer, size); |
7daf5226 | 55 | |
552cc11b MW |
56 | return (data); |
57 | } | |
58 | ||
59 | /** | |
60 | * Described in header. | |
61 | */ | |
01e43e31 | 62 | void memxor(u_int8_t dst[], u_int8_t src[], size_t n) |
552cc11b | 63 | { |
01e43e31 | 64 | int m, i; |
7daf5226 | 65 | |
01e43e31 | 66 | /* byte wise XOR until dst aligned */ |
09846603 | 67 | for (i = 0; (uintptr_t)&dst[i] % sizeof(long) && i < n; i++) |
4fd233a7 | 68 | { |
01e43e31 | 69 | dst[i] ^= src[i]; |
4fd233a7 | 70 | } |
01e43e31 | 71 | /* try to use words if src shares an aligment with dst */ |
74b14b40 | 72 | switch (((uintptr_t)&src[i] % sizeof(long))) |
552cc11b | 73 | { |
01e43e31 MW |
74 | case 0: |
75 | for (m = n - sizeof(long); i <= m; i += sizeof(long)) | |
76 | { | |
77 | *(long*)&dst[i] ^= *(long*)&src[i]; | |
78 | } | |
79 | break; | |
80 | case sizeof(int): | |
81 | for (m = n - sizeof(int); i <= m; i += sizeof(int)) | |
82 | { | |
83 | *(int*)&dst[i] ^= *(int*)&src[i]; | |
84 | } | |
85 | break; | |
86 | case sizeof(short): | |
87 | for (m = n - sizeof(short); i <= m; i += sizeof(short)) | |
88 | { | |
89 | *(short*)&dst[i] ^= *(short*)&src[i]; | |
90 | } | |
91 | break; | |
92 | default: | |
93 | break; | |
94 | } | |
95 | /* byte wise XOR of the rest */ | |
96 | for (; i < n; i++) | |
97 | { | |
98 | dst[i] ^= src[i]; | |
552cc11b MW |
99 | } |
100 | } | |
101 | ||
81736d7d TB |
102 | /** |
103 | * Described in header. | |
104 | */ | |
105 | void *memstr(const void *haystack, const char *needle, size_t n) | |
106 | { | |
107 | unsigned const char *pos = haystack; | |
108 | size_t l = strlen(needle); | |
109 | for (; n >= l; ++pos, --n) | |
110 | { | |
111 | if (memeq(pos, needle, l)) | |
112 | { | |
113 | return (void*)pos; | |
114 | } | |
115 | } | |
116 | return NULL; | |
117 | } | |
118 | ||
6c20579a TB |
119 | /** |
120 | * Described in header. | |
121 | */ | |
122 | bool mkdir_p(const char *path, mode_t mode) | |
123 | { | |
124 | size_t len; | |
125 | char *pos, full[PATH_MAX]; | |
126 | pos = full; | |
127 | if (!path || *path == '\0') | |
128 | { | |
129 | return TRUE; | |
130 | } | |
131 | len = snprintf(full, sizeof(full)-1, "%s", path); | |
132 | if (len < 0 || len >= sizeof(full)-1) | |
133 | { | |
134 | DBG1("path string %s too long", path); | |
135 | return FALSE; | |
136 | } | |
137 | /* ensure that the path ends with a '/' */ | |
138 | if (full[len-1] != '/') | |
139 | { | |
140 | full[len++] = '/'; | |
141 | full[len] = '\0'; | |
142 | } | |
143 | /* skip '/' at the beginning */ | |
144 | while (*pos == '/') | |
145 | { | |
146 | pos++; | |
147 | } | |
148 | while ((pos = strchr(pos, '/'))) | |
149 | { | |
150 | *pos = '\0'; | |
151 | if (access(full, F_OK) < 0) | |
152 | { | |
153 | if (mkdir(full, mode) < 0) | |
154 | { | |
155 | DBG1("failed to create directory %s", full); | |
156 | return FALSE; | |
157 | } | |
158 | } | |
159 | *pos = '/'; | |
160 | pos++; | |
161 | } | |
162 | return TRUE; | |
163 | } | |
164 | ||
3f310c0d MW |
165 | /** |
166 | * Return monotonic time | |
167 | */ | |
168 | time_t time_monotonic(timeval_t *tv) | |
169 | { | |
b2944d71 TB |
170 | #if defined(HAVE_CLOCK_GETTIME) && \ |
171 | (defined(HAVE_CONDATTR_CLOCK_MONOTONIC) || \ | |
172 | defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC)) | |
3d5818ec MW |
173 | /* as we use time_monotonic() for condvar operations, we use the |
174 | * monotonic time source only if it is also supported by pthread. */ | |
3f310c0d | 175 | timespec_t ts; |
7daf5226 | 176 | |
3f310c0d MW |
177 | if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) |
178 | { | |
179 | if (tv) | |
180 | { | |
181 | tv->tv_sec = ts.tv_sec; | |
182 | tv->tv_usec = ts.tv_nsec / 1000; | |
183 | } | |
184 | return ts.tv_sec; | |
185 | } | |
b2944d71 | 186 | #endif /* HAVE_CLOCK_GETTIME && (...) */ |
3f310c0d MW |
187 | /* Fallback to non-monotonic timestamps: |
188 | * On MAC OS X, creating monotonic timestamps is rather difficult. We | |
189 | * could use mach_absolute_time() and catch sleep/wakeup notifications. | |
3d5818ec MW |
190 | * We stick to the simpler (non-monotonic) gettimeofday() for now. |
191 | * But keep in mind: we need the same time source here as in condvar! */ | |
3f310c0d MW |
192 | if (!tv) |
193 | { | |
194 | return time(NULL); | |
195 | } | |
196 | if (gettimeofday(tv, NULL) != 0) | |
197 | { /* should actually never fail if passed pointers are valid */ | |
198 | return -1; | |
199 | } | |
200 | return tv->tv_sec; | |
201 | } | |
202 | ||
081ae2eb MW |
203 | /** |
204 | * return null | |
205 | */ | |
206 | void *return_null() | |
207 | { | |
208 | return NULL; | |
209 | } | |
210 | ||
da17b016 MW |
211 | /** |
212 | * returns TRUE | |
213 | */ | |
214 | bool return_true() | |
215 | { | |
216 | return TRUE; | |
217 | } | |
218 | ||
219 | /** | |
220 | * returns FALSE | |
221 | */ | |
222 | bool return_false() | |
223 | { | |
224 | return FALSE; | |
225 | } | |
226 | ||
233b853d MW |
227 | /** |
228 | * nop operation | |
229 | */ | |
230 | void nop() | |
231 | { | |
232 | } | |
233 | ||
efd0fe21 MW |
234 | #ifndef HAVE_GCC_ATOMIC_OPERATIONS |
235 | #include <pthread.h> | |
236 | ||
552cc11b | 237 | /** |
7daf5226 | 238 | * We use a single mutex for all refcount variables. |
552cc11b MW |
239 | */ |
240 | static pthread_mutex_t ref_mutex = PTHREAD_MUTEX_INITIALIZER; | |
241 | ||
242 | /** | |
efd0fe21 | 243 | * Increase refcount |
552cc11b MW |
244 | */ |
245 | void ref_get(refcount_t *ref) | |
246 | { | |
247 | pthread_mutex_lock(&ref_mutex); | |
248 | (*ref)++; | |
249 | pthread_mutex_unlock(&ref_mutex); | |
250 | } | |
251 | ||
252 | /** | |
efd0fe21 | 253 | * Decrease refcount |
552cc11b MW |
254 | */ |
255 | bool ref_put(refcount_t *ref) | |
256 | { | |
257 | bool more_refs; | |
7daf5226 | 258 | |
552cc11b MW |
259 | pthread_mutex_lock(&ref_mutex); |
260 | more_refs = --(*ref); | |
261 | pthread_mutex_unlock(&ref_mutex); | |
262 | return !more_refs; | |
263 | } | |
efd0fe21 | 264 | #endif /* HAVE_GCC_ATOMIC_OPERATIONS */ |
552cc11b MW |
265 | |
266 | /** | |
d25ce370 | 267 | * Described in header. |
552cc11b | 268 | */ |
d25ce370 TB |
269 | int time_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec, |
270 | const void *const *args) | |
552cc11b MW |
271 | { |
272 | static const char* months[] = { | |
273 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
274 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" | |
275 | }; | |
276 | time_t *time = *((time_t**)(args[0])); | |
d25ce370 | 277 | bool utc = *((bool*)(args[1]));; |
552cc11b | 278 | struct tm t; |
7daf5226 | 279 | |
552cc11b MW |
280 | if (time == UNDEFINED_TIME) |
281 | { | |
d25ce370 TB |
282 | return print_in_hook(dst, len, "--- -- --:--:--%s----", |
283 | utc ? " UTC " : " "); | |
552cc11b MW |
284 | } |
285 | if (utc) | |
286 | { | |
287 | gmtime_r(time, &t); | |
288 | } | |
289 | else | |
290 | { | |
291 | localtime_r(time, &t); | |
292 | } | |
d25ce370 TB |
293 | return print_in_hook(dst, len, "%s %02d %02d:%02d:%02d%s%04d", |
294 | months[t.tm_mon], t.tm_mday, t.tm_hour, t.tm_min, | |
295 | t.tm_sec, utc ? " UTC " : " ", t.tm_year + 1900); | |
552cc11b MW |
296 | } |
297 | ||
298 | /** | |
d25ce370 | 299 | * Described in header. |
552cc11b | 300 | */ |
d25ce370 TB |
301 | int time_delta_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec, |
302 | const void *const *args) | |
552cc11b MW |
303 | { |
304 | char* unit = "second"; | |
d25ce370 TB |
305 | time_t *arg1 = *((time_t**)(args[0])); |
306 | time_t *arg2 = *((time_t**)(args[1])); | |
307 | time_t delta = abs(*arg1 - *arg2); | |
7daf5226 | 308 | |
552cc11b MW |
309 | if (delta > 2 * 60 * 60 * 24) |
310 | { | |
311 | delta /= 60 * 60 * 24; | |
312 | unit = "day"; | |
313 | } | |
314 | else if (delta > 2 * 60 * 60) | |
315 | { | |
316 | delta /= 60 * 60; | |
317 | unit = "hour"; | |
318 | } | |
319 | else if (delta > 2 * 60) | |
320 | { | |
321 | delta /= 60; | |
322 | unit = "minute"; | |
323 | } | |
d25ce370 | 324 | return print_in_hook(dst, len, "%d %s%s", delta, unit, (delta == 1)? "":"s"); |
552cc11b MW |
325 | } |
326 | ||
327 | /** | |
328 | * Number of bytes per line to dump raw data | |
329 | */ | |
330 | #define BYTES_PER_LINE 16 | |
331 | ||
332 | static char hexdig_upper[] = "0123456789ABCDEF"; | |
333 | ||
334 | /** | |
d25ce370 | 335 | * Described in header. |
552cc11b | 336 | */ |
d25ce370 TB |
337 | int mem_printf_hook(char *dst, size_t dstlen, |
338 | printf_hook_spec_t *spec, const void *const *args) | |
552cc11b MW |
339 | { |
340 | char *bytes = *((void**)(args[0])); | |
341 | int len = *((size_t*)(args[1])); | |
7daf5226 | 342 | |
552cc11b MW |
343 | char buffer[BYTES_PER_LINE * 3]; |
344 | char ascii_buffer[BYTES_PER_LINE + 1]; | |
345 | char *buffer_pos = buffer; | |
346 | char *bytes_pos = bytes; | |
347 | char *bytes_roof = bytes + len; | |
348 | int line_start = 0; | |
349 | int i = 0; | |
350 | int written = 0; | |
7daf5226 | 351 | |
d25ce370 | 352 | written += print_in_hook(dst, dstlen, "=> %d bytes @ %p", len, bytes); |
7daf5226 | 353 | |
552cc11b MW |
354 | while (bytes_pos < bytes_roof) |
355 | { | |
356 | *buffer_pos++ = hexdig_upper[(*bytes_pos >> 4) & 0xF]; | |
357 | *buffer_pos++ = hexdig_upper[ *bytes_pos & 0xF]; | |
358 | ||
359 | ascii_buffer[i++] = | |
360 | (*bytes_pos > 31 && *bytes_pos < 127) ? *bytes_pos : '.'; | |
361 | ||
7daf5226 | 362 | if (++bytes_pos == bytes_roof || i == BYTES_PER_LINE) |
552cc11b MW |
363 | { |
364 | int padding = 3 * (BYTES_PER_LINE - i); | |
7daf5226 | 365 | |
552cc11b MW |
366 | while (padding--) |
367 | { | |
368 | *buffer_pos++ = ' '; | |
369 | } | |
370 | *buffer_pos++ = '\0'; | |
371 | ascii_buffer[i] = '\0'; | |
7daf5226 | 372 | |
d25ce370 | 373 | written += print_in_hook(dst, dstlen, "\n%4d: %s %s", |
323f9f99 | 374 | line_start, buffer, ascii_buffer); |
7daf5226 | 375 | |
552cc11b MW |
376 | buffer_pos = buffer; |
377 | line_start += BYTES_PER_LINE; | |
378 | i = 0; | |
379 | } | |
380 | else | |
381 | { | |
382 | *buffer_pos++ = ' '; | |
383 | } | |
384 | } | |
385 | return written; | |
386 | } |