-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
482 lines (403 loc) · 8.57 KB
/
utils.c
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
* Audio Scheduler - An audio clip scheduler for use in radio broadcasting
* Various utilities/helpers
*
* Copyright (C) 2016 Nick Kossifidis <[email protected]>
*
* 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 3 of the License, or
* any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE /* Needed for vasprintf() */
#include "utils.h"
#include <stdio.h> /* For v/printf() */
#include <stdlib.h> /* For free()/random() */
#include <errno.h> /* For errno */
#include <string.h> /* For strerror()/memmove() */
#include <limits.h> /* For PATH_MAX */
#include <sys/stat.h> /* For struct stat */
#include <unistd.h> /* For stat()/access()/syscall() */
/* For getrandom syscall available on linux 3.17+ */
#if defined(__linux__) && defined(__GLIBC__)
#include <sys/syscall.h>
#if defined(SYS_getrandom)
#define GETRANDOM_DEFINED
#endif
#endif
/* Some codes for prety output on the terminal */
#define NORMAL "\x1B[0m"
#define BRIGHT "\x1B[1m"
#define DIM "\x1B[2m"
#define RED "\x1B[31m"
#define GREEN "\x1B[32m"
#define YELLOW "\x1B[33m"
#define BLUE "\x1B[34m"
#define MAGENTA "\x1B[35m"
#define CYAN "\x1B[36m"
#define WHITE "\x1B[37m"
static int log_level = 0;
#ifdef DEBUG
static int debug_mask = 0;
void
utils_set_debug_mask(int debug_msk)
{
debug_mask = debug_msk;
}
int
utils_is_debug_enabled(int facility)
{
return ((debug_mask & facility) == facility ? 1 : 0);
}
#else
void utils_set_debug_mask(int debug_msk) {}
int utils_is_debug_enabled(int facility) {return 0;}
#endif
void
utils_set_log_level(int log_lvl)
{
log_level = log_lvl;
}
static const char*
utils_get_facility_name(int facility)
{
if(facility & SKIP)
return "";
switch(facility & 0xFF) {
case NONE:
return "";
case SCHED:
return "[SCHED] ";
case PLR:
return "[PLR] ";
case CFG:
return "[CFG] ";
case PLS:
return "[PLS] ";
case SHUF:
return "[SHUF] ";
case META:
return "[META] ";
case UTILS:
return "[UTILS] ";
default:
return "[UNK] ";
}
}
void
utils_verr(int facility, const char* fmt, va_list args)
{
char *msg = NULL;
int ret = 0;
if(log_level < ERROR)
return;
ret = vasprintf(&msg, fmt, args);
if(ret < 0)
return;
fprintf(stderr, RED"%s%s"NORMAL,
utils_get_facility_name(facility), msg);
fflush(stderr);
free(msg);
}
void
utils_err(int facility, const char* fmt,...)
{
va_list args;
if(log_level < ERROR)
return;
va_start(args, fmt);
utils_verr(facility, fmt, args);
va_end(args);
}
void
utils_vperr(int facility, const char* fmt, va_list args)
{
char *msg = NULL;
int ret = 0;
if(log_level < ERROR)
return;
ret = vasprintf(&msg, fmt, args);
if(ret < 0)
return;
fprintf(stderr, RED"%s%s: %s"NORMAL"\n",
utils_get_facility_name(facility), msg, strerror(errno));
fflush(stderr);
free(msg);
}
void
utils_perr(int facility, const char* fmt,...)
{
va_list args;
if(log_level < ERROR)
return;
va_start(args, fmt);
utils_vperr(facility, fmt, args);
va_end(args);
}
void
utils_vwrn(int facility, const char* fmt, va_list args)
{
char *msg = NULL;
int ret = 0;
if(log_level < WARN)
return;
ret = vasprintf(&msg, fmt, args);
if(ret < 0)
return;
fprintf(stderr, YELLOW"%s%s"NORMAL, utils_get_facility_name(facility), msg);
fflush(stderr);
free(msg);
}
void
utils_wrn(int facility, const char* fmt,...)
{
va_list args;
if(log_level < WARN)
return;
va_start(args, fmt);
utils_vwrn(facility, fmt, args);
va_end(args);
}
void
utils_vpwrn(int facility, const char* fmt, va_list args)
{
char *msg = NULL;;
int ret = 0;
if(log_level < WARN)
return;
ret = vasprintf(&msg, fmt, args);
if(ret < 0)
return;
fprintf(stderr, YELLOW"%s%s: %s"NORMAL"\n",
utils_get_facility_name(facility), msg, strerror(errno));
fflush(stderr);
free(msg);
}
void
utils_pwrn(int facility, const char* fmt,...)
{
va_list args;
if(log_level < WARN)
return;
va_start(args, fmt);
utils_vpwrn(facility, fmt, args);
va_end(args);
}
void
utils_vinfo(int facility, const char* fmt, va_list args)
{
char *msg = NULL;
int ret = 0;
if(log_level < INFO)
return;
ret = vasprintf(&msg, fmt, args);
if(ret < 0)
return;
printf(CYAN"%s%s"NORMAL, utils_get_facility_name(facility), msg);
fflush(stdout);
free(msg);
}
void
utils_info(int facility, const char* fmt,...)
{
va_list args;
if(log_level < INFO)
return;
va_start(args, fmt);
utils_vinfo(facility, fmt, args);
va_end(args);
}
#ifdef DEBUG
void
utils_vdbg(int facility, const char* fmt, va_list args)
{
char *msg = NULL;
int ret = 0;
if(log_level < DBG)
return;
if(!(facility & debug_mask))
return;
ret = vasprintf(&msg, fmt, args);
if(ret < 0)
return;
fprintf(stderr, MAGENTA"%s%s"NORMAL,
utils_get_facility_name(facility), msg);
fflush(stderr);
free(msg);
}
void
utils_dbg(int facility, const char* fmt,...)
{
va_list args;
if(log_level < DBG)
return;
va_start(args, fmt);
utils_vdbg(facility, fmt, args);
va_end(args);
}
#else
void utils_vdbg(int facility, const char* fmt, va_list args) {}
void utils_dbg(int facility, const char* fmt,...){}
#endif
void
utils_trim_string(char* string)
{
char* start = NULL;
char* end = NULL;
int len = strnlen(string, PATH_MAX);
int i = 0;
/* Find start/end of actual string */
while(i < len) {
start = string + i;
if(((*start) != ' ') && ((*start) != '\n') &&
((*start) != '\r'))
break;
i++;
}
i = len - 1;
while(i >= 0) {
end = string + i;
if(((*end) != ' ') && ((*end) != '\n') &&
((*end) != '\r') && ((*end) != '\0'))
break;
i--;
}
/* NULL-terminate it */
(*(end + 1)) = '\0';
/* Move it to the beginning of the buffer */
len = end - start + 1;
memmove(string, start, len);
}
static int
utils_platform_getrandom(void *buf, size_t len)
{
static int source_reported = 0;
int ret = 0;
FILE *file = NULL;
#if defined(GETRANDOM_DEFINED)
ret = syscall(SYS_getrandom, buf, len, 0);
if (ret == len) {
if(!source_reported) {
utils_dbg(UTILS, "Got random data through syscall\n");
source_reported = 1;
}
return 0;
}
#endif
/* Syscall failed, open urandom instead */
file = fopen("/dev/urandom", "rb");
if (file == NULL)
return -1;
ret = fread(buf, 1, len, file);
if (ret != len) {
fclose(file);
return -1;
}
if(!source_reported) {
utils_dbg(UTILS, "Got random data through /dev/urandom\n");
source_reported = 1;
}
fclose(file);
return ret;
}
unsigned int
utils_get_random_uint()
{
static int source_reported = 0;
unsigned int value = 0;
int ret = 0;
/* Try to get a random number from the os, if it fails
* fallback to libc's random() */
ret = utils_platform_getrandom(&value, sizeof(unsigned int));
if(ret < 0) {
value = (unsigned int) random();
if(!source_reported) {
utils_dbg(UTILS, "Got random data through random()\n");
source_reported = 1;
}
}
return value;
}
time_t
utils_get_mtime(char* filepath)
{
struct stat st;
if (stat(filepath, &st) < 0) {
utils_perr(UTILS, "Could not stat(%s)", filepath);
return 0;
}
return st.st_mtime;
}
int
utils_is_regular_file(char* filepath)
{
struct stat st;
int ret = 1;
ret = stat(filepath, &st);
if (!S_ISREG(st.st_mode)) {
utils_wrn(UTILS, "Not a regular file: %s\n", filepath);
ret = 0;
} else if (ret < 0) {
utils_pwrn(UTILS, "Could not stat(%s)", filepath);
ret = 0;
}
return 1;
}
int
utils_is_readable_file(char*filepath)
{
#ifndef TEST
int ret = 0;
if(!utils_is_regular_file(filepath))
return 0;
ret = access(filepath, R_OK);
if(ret < 0) {
utils_pwrn(UTILS, "access(%s) failed", filepath);
return 0;
}
#endif
return 1;
}
static void
utils_tm_cleanup_date(struct tm *tm)
{
/* Zero-out the date part */
tm->tm_mday = 1;
tm->tm_mon = 0;
tm->tm_year = 70;
tm->tm_wday = 0;
tm->tm_yday = 0;
tm->tm_isdst = -1;
}
int
utils_compare_time(struct tm *tm1, struct tm* tm0, int no_date)
{
time_t t1 = 0;
time_t t0 = 0;
double diff = 0.0L;
if(no_date) {
utils_tm_cleanup_date(tm0);
utils_tm_cleanup_date(tm1);
}
errno = 0;
t1 = mktime(tm1);
t0 = mktime(tm0);
diff = difftime(t1, t0);
if (errno != 0)
utils_perr(UTILS, "compare_time");
utils_dbg(UTILS, "compare_time: (t1: %li) - (t0: %li) = %lf\n", t1, t0, diff);
if(diff > 0.0L)
return 1;
else if(diff < 0.0L)
return -1;
else
return 0;
}