1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
|
/*
* Copyright (C) 2008-2014 Tobias Brunner
* Copyright (C) 2005-2008 Martin Willi
* Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#ifdef WIN32
/* for GetTickCount64, Windows 7 */
# define _WIN32_WINNT 0x0601
#endif
#include "utils.h"
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <inttypes.h>
#include <stdint.h>
#include <dirent.h>
#include <time.h>
#ifndef WIN32
# include <signal.h>
#endif
#include <library.h>
#include <utils/debug.h>
#include <utils/chunk.h>
#include <collections/enumerator.h>
#include <threading/mutex.h>
#include <threading/condvar.h>
ENUM(status_names, SUCCESS, NEED_MORE,
"SUCCESS",
"FAILED",
"OUT_OF_RES",
"ALREADY_DONE",
"NOT_SUPPORTED",
"INVALID_ARG",
"NOT_FOUND",
"PARSE_ERROR",
"VERIFY_ERROR",
"INVALID_STATE",
"DESTROY_ME",
"NEED_MORE",
);
/**
* Described in header.
*/
void* malloc_align(size_t size, u_int8_t align)
{
u_int8_t pad;
void *ptr;
if (align == 0)
{
align = 1;
}
ptr = malloc(align + sizeof(pad) + size);
if (!ptr)
{
return NULL;
}
/* store padding length just before data, down to the allocation boundary
* to do some verification during free_align() */
pad = align - ((uintptr_t)ptr % align);
memset(ptr, pad, pad);
return ptr + pad;
}
/**
* Described in header.
*/
void free_align(void *ptr)
{
u_int8_t pad, *pos;
pos = ptr - 1;
/* verify padding to check any corruption */
for (pad = *pos; (void*)pos >= ptr - pad; pos--)
{
if (*pos != pad)
{
DBG1(DBG_LIB, "!!!! invalid free_align() !!!!");
return;
}
}
free(ptr - pad);
}
#ifdef WIN32
/**
* Flag to indicate signaled wait_sigint()
*/
static bool sigint_signaled = FALSE;
/**
* Condvar to wait in wait_sigint()
*/
static condvar_t *sigint_cond;
/**
* Mutex to check signaling()
*/
static mutex_t *sigint_mutex;
/**
* Control handler to catch ^C
*/
static BOOL WINAPI handler(DWORD dwCtrlType)
{
switch (dwCtrlType)
{
case CTRL_C_EVENT:
case CTRL_BREAK_EVENT:
case CTRL_CLOSE_EVENT:
sigint_mutex->lock(sigint_mutex);
sigint_signaled = TRUE;
sigint_cond->signal(sigint_cond);
sigint_mutex->unlock(sigint_mutex);
return TRUE;
default:
return FALSE;
}
}
/**
* Windows variant
*/
void wait_sigint()
{
SetConsoleCtrlHandler(handler, TRUE);
sigint_mutex = mutex_create(MUTEX_TYPE_DEFAULT);
sigint_cond = condvar_create(CONDVAR_TYPE_DEFAULT);
sigint_mutex->lock(sigint_mutex);
while (!sigint_signaled)
{
sigint_cond->wait(sigint_cond, sigint_mutex);
}
sigint_mutex->unlock(sigint_mutex);
sigint_mutex->destroy(sigint_mutex);
sigint_cond->destroy(sigint_cond);
}
#else /* !WIN32 */
/**
* Unix variant
*/
void wait_sigint()
{
sigset_t set;
int sig;
sigemptyset(&set);
sigaddset(&set, SIGINT);
sigaddset(&set, SIGTERM);
sigprocmask(SIG_BLOCK, &set, NULL);
sigwait(&set, &sig);
}
#endif
#ifndef HAVE_CLOSEFROM
/**
* Described in header.
*/
void closefrom(int lowfd)
{
char fd_dir[PATH_MAX];
int maxfd, fd, len;
/* try to close only open file descriptors on Linux... */
len = snprintf(fd_dir, sizeof(fd_dir), "/proc/%u/fd", getpid());
if (len > 0 && len < sizeof(fd_dir) && access(fd_dir, F_OK) == 0)
{
enumerator_t *enumerator = enumerator_create_directory(fd_dir);
if (enumerator)
{
char *rel;
while (enumerator->enumerate(enumerator, &rel, NULL, NULL))
{
fd = atoi(rel);
if (fd >= lowfd)
{
close(fd);
}
}
enumerator->destroy(enumerator);
return;
}
}
/* ...fall back to closing all fds otherwise */
#ifdef WIN32
maxfd = _getmaxstdio();
#else
maxfd = (int)sysconf(_SC_OPEN_MAX);
#endif
if (maxfd < 0)
{
maxfd = 256;
}
for (fd = lowfd; fd < maxfd; fd++)
{
close(fd);
}
}
#endif /* HAVE_CLOSEFROM */
/**
* Return monotonic time
*/
time_t time_monotonic(timeval_t *tv)
{
#ifdef WIN32
ULONGLONG ms;
time_t s;
ms = GetTickCount64();
s = ms / 1000;
if (tv)
{
tv->tv_sec = s;
tv->tv_usec = (ms - (s * 1000)) * 1000;
}
return s;
#else /* !WIN32 */
#if defined(HAVE_CLOCK_GETTIME) && \
(defined(HAVE_CONDATTR_CLOCK_MONOTONIC) || \
defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC))
/* as we use time_monotonic() for condvar operations, we use the
* monotonic time source only if it is also supported by pthread. */
timespec_t ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
{
if (tv)
{
tv->tv_sec = ts.tv_sec;
tv->tv_usec = ts.tv_nsec / 1000;
}
return ts.tv_sec;
}
#endif /* HAVE_CLOCK_GETTIME && (...) */
/* Fallback to non-monotonic timestamps:
* On MAC OS X, creating monotonic timestamps is rather difficult. We
* could use mach_absolute_time() and catch sleep/wakeup notifications.
* We stick to the simpler (non-monotonic) gettimeofday() for now.
* But keep in mind: we need the same time source here as in condvar! */
if (!tv)
{
return time(NULL);
}
if (gettimeofday(tv, NULL) != 0)
{ /* should actually never fail if passed pointers are valid */
return -1;
}
return tv->tv_sec;
#endif /* !WIN32 */
}
/**
* return null
*/
void *return_null()
{
return NULL;
}
/**
* returns TRUE
*/
bool return_true()
{
return TRUE;
}
/**
* returns FALSE
*/
bool return_false()
{
return FALSE;
}
/**
* returns FAILED
*/
status_t return_failed()
{
return FAILED;
}
/**
* returns SUCCESS
*/
status_t return_success()
{
return SUCCESS;
}
/**
* nop operation
*/
void nop()
{
}
/**
* See header
*/
void utils_init()
{
#ifdef WIN32
windows_init();
#endif
atomics_init();
strerror_init();
}
/**
* See header
*/
void utils_deinit()
{
#ifdef WIN32
windows_deinit();
#endif
atomics_deinit();
strerror_deinit();
}
/**
* Described in header.
*/
int time_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec,
const void *const *args)
{
static const char* months[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
time_t *time = *((time_t**)(args[0]));
bool utc = *((int*)(args[1]));
struct tm t, *ret = NULL;
if (*time != UNDEFINED_TIME)
{
if (utc)
{
ret = gmtime_r(time, &t);
}
else
{
ret = localtime_r(time, &t);
}
}
if (ret == NULL)
{
return print_in_hook(data, "--- -- --:--:--%s----",
utc ? " UTC " : " ");
}
return print_in_hook(data, "%s %02d %02d:%02d:%02d%s%04d",
months[t.tm_mon], t.tm_mday, t.tm_hour, t.tm_min,
t.tm_sec, utc ? " UTC " : " ", t.tm_year + 1900);
}
/**
* Described in header.
*/
int time_delta_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec,
const void *const *args)
{
char* unit = "second";
time_t *arg1 = *((time_t**)(args[0]));
time_t *arg2 = *((time_t**)(args[1]));
u_int64_t delta = llabs(*arg1 - *arg2);
if (delta > 2 * 60 * 60 * 24)
{
delta /= 60 * 60 * 24;
unit = "day";
}
else if (delta > 2 * 60 * 60)
{
delta /= 60 * 60;
unit = "hour";
}
else if (delta > 2 * 60)
{
delta /= 60;
unit = "minute";
}
return print_in_hook(data, "%" PRIu64 " %s%s", delta, unit,
(delta == 1) ? "" : "s");
}
|