-
Notifications
You must be signed in to change notification settings - Fork 3
/
lib.c
87 lines (68 loc) · 1.42 KB
/
lib.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
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <ccan/talloc/talloc.h>
#include <ccan/grab_file/grab_file.h>
#include "lib.h"
static FILE *logfile;
int verbose = 0;
void log_init(FILE *f)
{
logfile = f;
if (!f)
openlog("quickcalld", LOG_PID, LOG_DAEMON);
}
void log_vprintf(int priority, const char *fmt, va_list ap)
{
if (logfile)
vfprintf(logfile, fmt, ap);
else
vsyslog(priority, fmt, ap);
}
void log_printf(int priority, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
log_vprintf(priority, fmt, ap);
va_end(ap);
}
void debug(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if (verbose)
log_vprintf(LOG_DEBUG, fmt, ap);
va_end(ap);
}
void __attribute__((noreturn)) __attribute__((format(printf, 1, 2))) die(char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
log_vprintf(LOG_ERR, fmt, ap);
va_end(ap);
exit(1);
}
int grab_file_strtol(const char *filename, int base, long *val)
{
char *file;
file = grab_file(NULL, filename, NULL);
if (!file)
return -1;
*val = strtol(file, NULL, base);
talloc_free(file);
return 0;
}
int get_sys_attrib(const char *sysdir, const char *attrib, int base, long *val)
{
char *name = talloc_asprintf(NULL, "%s/%s", sysdir, attrib);
int rc;
rc = grab_file_strtol(name, base, val);
talloc_free(name);
return rc;
}