]>
Commit | Line | Data |
---|---|---|
552cc11b | 1 | /* |
38031382 | 2 | * Copyright (C) 2008-2010 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 | 30 | |
a8809bb0 | 31 | ENUM(status_names, SUCCESS, NEED_MORE, |
552cc11b MW |
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 | ||
ed678b52 MW |
102 | /** |
103 | * Described in header. | |
104 | */ | |
105 | void memwipe_noinline(void *ptr, size_t n) | |
106 | { | |
107 | memwipe_inline(ptr, n); | |
108 | } | |
109 | ||
81736d7d TB |
110 | /** |
111 | * Described in header. | |
112 | */ | |
113 | void *memstr(const void *haystack, const char *needle, size_t n) | |
114 | { | |
115 | unsigned const char *pos = haystack; | |
116 | size_t l = strlen(needle); | |
117 | for (; n >= l; ++pos, --n) | |
118 | { | |
119 | if (memeq(pos, needle, l)) | |
120 | { | |
121 | return (void*)pos; | |
122 | } | |
123 | } | |
124 | return NULL; | |
125 | } | |
126 | ||
d543d9ca TB |
127 | /** |
128 | * Described in header. | |
129 | */ | |
130 | char* translate(char *str, const char *from, const char *to) | |
131 | { | |
132 | char *pos = str; | |
133 | if (strlen(from) != strlen(to)) | |
134 | { | |
135 | return str; | |
136 | } | |
137 | while (pos && *pos) | |
138 | { | |
139 | char *match; | |
140 | if ((match = strchr(from, *pos)) != NULL) | |
141 | { | |
142 | *pos = to[match - from]; | |
143 | } | |
144 | pos++; | |
145 | } | |
146 | return str; | |
147 | } | |
148 | ||
6c20579a TB |
149 | /** |
150 | * Described in header. | |
151 | */ | |
152 | bool mkdir_p(const char *path, mode_t mode) | |
153 | { | |
fc1afcc8 | 154 | int len; |
6c20579a TB |
155 | char *pos, full[PATH_MAX]; |
156 | pos = full; | |
157 | if (!path || *path == '\0') | |
158 | { | |
159 | return TRUE; | |
160 | } | |
161 | len = snprintf(full, sizeof(full)-1, "%s", path); | |
162 | if (len < 0 || len >= sizeof(full)-1) | |
163 | { | |
8b0e0910 | 164 | DBG1(DBG_LIB, "path string %s too long", path); |
6c20579a TB |
165 | return FALSE; |
166 | } | |
167 | /* ensure that the path ends with a '/' */ | |
168 | if (full[len-1] != '/') | |
169 | { | |
170 | full[len++] = '/'; | |
171 | full[len] = '\0'; | |
172 | } | |
173 | /* skip '/' at the beginning */ | |
174 | while (*pos == '/') | |
175 | { | |
176 | pos++; | |
177 | } | |
178 | while ((pos = strchr(pos, '/'))) | |
179 | { | |
180 | *pos = '\0'; | |
181 | if (access(full, F_OK) < 0) | |
182 | { | |
183 | if (mkdir(full, mode) < 0) | |
184 | { | |
8b0e0910 | 185 | DBG1(DBG_LIB, "failed to create directory %s", full); |
6c20579a TB |
186 | return FALSE; |
187 | } | |
188 | } | |
189 | *pos = '/'; | |
190 | pos++; | |
191 | } | |
192 | return TRUE; | |
193 | } | |
194 | ||
3f310c0d MW |
195 | /** |
196 | * Return monotonic time | |
197 | */ | |
198 | time_t time_monotonic(timeval_t *tv) | |
199 | { | |
b2944d71 TB |
200 | #if defined(HAVE_CLOCK_GETTIME) && \ |
201 | (defined(HAVE_CONDATTR_CLOCK_MONOTONIC) || \ | |
202 | defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC)) | |
3d5818ec MW |
203 | /* as we use time_monotonic() for condvar operations, we use the |
204 | * monotonic time source only if it is also supported by pthread. */ | |
3f310c0d | 205 | timespec_t ts; |
7daf5226 | 206 | |
3f310c0d MW |
207 | if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) |
208 | { | |
209 | if (tv) | |
210 | { | |
211 | tv->tv_sec = ts.tv_sec; | |
212 | tv->tv_usec = ts.tv_nsec / 1000; | |
213 | } | |
214 | return ts.tv_sec; | |
215 | } | |
b2944d71 | 216 | #endif /* HAVE_CLOCK_GETTIME && (...) */ |
3f310c0d MW |
217 | /* Fallback to non-monotonic timestamps: |
218 | * On MAC OS X, creating monotonic timestamps is rather difficult. We | |
219 | * could use mach_absolute_time() and catch sleep/wakeup notifications. | |
3d5818ec MW |
220 | * We stick to the simpler (non-monotonic) gettimeofday() for now. |
221 | * But keep in mind: we need the same time source here as in condvar! */ | |
3f310c0d MW |
222 | if (!tv) |
223 | { | |
224 | return time(NULL); | |
225 | } | |
226 | if (gettimeofday(tv, NULL) != 0) | |
227 | { /* should actually never fail if passed pointers are valid */ | |
228 | return -1; | |
229 | } | |
230 | return tv->tv_sec; | |
231 | } | |
232 | ||
081ae2eb MW |
233 | /** |
234 | * return null | |
235 | */ | |
236 | void *return_null() | |
237 | { | |
238 | return NULL; | |
239 | } | |
240 | ||
da17b016 MW |
241 | /** |
242 | * returns TRUE | |
243 | */ | |
244 | bool return_true() | |
245 | { | |
246 | return TRUE; | |
247 | } | |
248 | ||
249 | /** | |
250 | * returns FALSE | |
251 | */ | |
252 | bool return_false() | |
253 | { | |
254 | return FALSE; | |
255 | } | |
256 | ||
502edf42 MW |
257 | /** |
258 | * returns FAILED | |
259 | */ | |
260 | status_t return_failed() | |
261 | { | |
262 | return FAILED; | |
263 | } | |
264 | ||
233b853d MW |
265 | /** |
266 | * nop operation | |
267 | */ | |
268 | void nop() | |
269 | { | |
270 | } | |
271 | ||
efd0fe21 MW |
272 | #ifndef HAVE_GCC_ATOMIC_OPERATIONS |
273 | #include <pthread.h> | |
274 | ||
552cc11b | 275 | /** |
7daf5226 | 276 | * We use a single mutex for all refcount variables. |
552cc11b MW |
277 | */ |
278 | static pthread_mutex_t ref_mutex = PTHREAD_MUTEX_INITIALIZER; | |
279 | ||
280 | /** | |
efd0fe21 | 281 | * Increase refcount |
552cc11b MW |
282 | */ |
283 | void ref_get(refcount_t *ref) | |
284 | { | |
285 | pthread_mutex_lock(&ref_mutex); | |
286 | (*ref)++; | |
287 | pthread_mutex_unlock(&ref_mutex); | |
288 | } | |
289 | ||
290 | /** | |
efd0fe21 | 291 | * Decrease refcount |
552cc11b MW |
292 | */ |
293 | bool ref_put(refcount_t *ref) | |
294 | { | |
295 | bool more_refs; | |
7daf5226 | 296 | |
552cc11b | 297 | pthread_mutex_lock(&ref_mutex); |
21f411b8 | 298 | more_refs = --(*ref) > 0; |
552cc11b MW |
299 | pthread_mutex_unlock(&ref_mutex); |
300 | return !more_refs; | |
301 | } | |
efd0fe21 | 302 | #endif /* HAVE_GCC_ATOMIC_OPERATIONS */ |
552cc11b MW |
303 | |
304 | /** | |
d25ce370 | 305 | * Described in header. |
552cc11b | 306 | */ |
d25ce370 TB |
307 | int time_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec, |
308 | const void *const *args) | |
552cc11b MW |
309 | { |
310 | static const char* months[] = { | |
311 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
312 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" | |
313 | }; | |
314 | time_t *time = *((time_t**)(args[0])); | |
d25ce370 | 315 | bool utc = *((bool*)(args[1]));; |
552cc11b | 316 | struct tm t; |
7daf5226 | 317 | |
552cc11b MW |
318 | if (time == UNDEFINED_TIME) |
319 | { | |
d25ce370 TB |
320 | return print_in_hook(dst, len, "--- -- --:--:--%s----", |
321 | utc ? " UTC " : " "); | |
552cc11b MW |
322 | } |
323 | if (utc) | |
324 | { | |
325 | gmtime_r(time, &t); | |
326 | } | |
327 | else | |
328 | { | |
329 | localtime_r(time, &t); | |
330 | } | |
d25ce370 TB |
331 | return print_in_hook(dst, len, "%s %02d %02d:%02d:%02d%s%04d", |
332 | months[t.tm_mon], t.tm_mday, t.tm_hour, t.tm_min, | |
333 | t.tm_sec, utc ? " UTC " : " ", t.tm_year + 1900); | |
552cc11b MW |
334 | } |
335 | ||
336 | /** | |
d25ce370 | 337 | * Described in header. |
552cc11b | 338 | */ |
d25ce370 TB |
339 | int time_delta_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec, |
340 | const void *const *args) | |
552cc11b MW |
341 | { |
342 | char* unit = "second"; | |
d25ce370 TB |
343 | time_t *arg1 = *((time_t**)(args[0])); |
344 | time_t *arg2 = *((time_t**)(args[1])); | |
345 | time_t delta = abs(*arg1 - *arg2); | |
7daf5226 | 346 | |
552cc11b MW |
347 | if (delta > 2 * 60 * 60 * 24) |
348 | { | |
349 | delta /= 60 * 60 * 24; | |
350 | unit = "day"; | |
351 | } | |
352 | else if (delta > 2 * 60 * 60) | |
353 | { | |
354 | delta /= 60 * 60; | |
355 | unit = "hour"; | |
356 | } | |
357 | else if (delta > 2 * 60) | |
358 | { | |
359 | delta /= 60; | |
360 | unit = "minute"; | |
361 | } | |
d25ce370 | 362 | return print_in_hook(dst, len, "%d %s%s", delta, unit, (delta == 1)? "":"s"); |
552cc11b MW |
363 | } |
364 | ||
365 | /** | |
366 | * Number of bytes per line to dump raw data | |
367 | */ | |
368 | #define BYTES_PER_LINE 16 | |
369 | ||
370 | static char hexdig_upper[] = "0123456789ABCDEF"; | |
371 | ||
372 | /** | |
d25ce370 | 373 | * Described in header. |
552cc11b | 374 | */ |
d25ce370 TB |
375 | int mem_printf_hook(char *dst, size_t dstlen, |
376 | printf_hook_spec_t *spec, const void *const *args) | |
552cc11b MW |
377 | { |
378 | char *bytes = *((void**)(args[0])); | |
379 | int len = *((size_t*)(args[1])); | |
7daf5226 | 380 | |
552cc11b MW |
381 | char buffer[BYTES_PER_LINE * 3]; |
382 | char ascii_buffer[BYTES_PER_LINE + 1]; | |
383 | char *buffer_pos = buffer; | |
384 | char *bytes_pos = bytes; | |
385 | char *bytes_roof = bytes + len; | |
386 | int line_start = 0; | |
387 | int i = 0; | |
388 | int written = 0; | |
7daf5226 | 389 | |
d25ce370 | 390 | written += print_in_hook(dst, dstlen, "=> %d bytes @ %p", len, bytes); |
7daf5226 | 391 | |
552cc11b MW |
392 | while (bytes_pos < bytes_roof) |
393 | { | |
394 | *buffer_pos++ = hexdig_upper[(*bytes_pos >> 4) & 0xF]; | |
395 | *buffer_pos++ = hexdig_upper[ *bytes_pos & 0xF]; | |
396 | ||
397 | ascii_buffer[i++] = | |
398 | (*bytes_pos > 31 && *bytes_pos < 127) ? *bytes_pos : '.'; | |
399 | ||
7daf5226 | 400 | if (++bytes_pos == bytes_roof || i == BYTES_PER_LINE) |
552cc11b MW |
401 | { |
402 | int padding = 3 * (BYTES_PER_LINE - i); | |
7daf5226 | 403 | |
552cc11b MW |
404 | while (padding--) |
405 | { | |
406 | *buffer_pos++ = ' '; | |
407 | } | |
408 | *buffer_pos++ = '\0'; | |
409 | ascii_buffer[i] = '\0'; | |
7daf5226 | 410 | |
d25ce370 | 411 | written += print_in_hook(dst, dstlen, "\n%4d: %s %s", |
323f9f99 | 412 | line_start, buffer, ascii_buffer); |
7daf5226 | 413 | |
552cc11b MW |
414 | buffer_pos = buffer; |
415 | line_start += BYTES_PER_LINE; | |
416 | i = 0; | |
417 | } | |
418 | else | |
419 | { | |
420 | *buffer_pos++ = ' '; | |
421 | } | |
422 | } | |
423 | return written; | |
424 | } |