-
Notifications
You must be signed in to change notification settings - Fork 30
/
log.c
393 lines (353 loc) · 8.55 KB
/
log.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
/*-
* xnumon - monitor macOS for malicious activity
* https://www.roe.ch/xnumon
*
* Copyright (c) 2017-2018, Daniel Roethlisberger <[email protected]>.
* All rights reserved.
*
* Licensed under the Open Software License version 3.0.
*/
#include "log.h"
#include "logfmt.h"
#include "logfmtjson.h"
#include "logfmtyaml.h"
#include "logdstfile.h"
#include "logdststdout.h"
#include "logdstsyslog.h"
#include "queue.h"
#include "attrib.h"
#include "policy.h"
#include "time.h"
#include "work.h"
#include "evtloop.h"
#include <string.h>
#include <assert.h>
/*
* Log events.
*/
typedef int (*logevt_func_t)(logfmt_t *, FILE *, void *) NONNULL(1,2) WUNRES;
logevt_func_t le_logevt[LOGEVT_SIZE] = {
logevt_xnumon_ops,
logevt_xnumon_stats,
logevt_image_exec,
logevt_process_access,
logevt_launchd_add,
};
_Static_assert(LOGEVT_SIZE == 5, "number of logevt types initialized above");
/*
* Log formats.
*/
#define LOGFMTS 3
static logfmt_t *logfmttab[LOGFMTS] = {
&logfmtjson,
&logfmtjsonseq,
&logfmtyaml
};
/*
* Log destinations.
*
* We may want to refactor this along the lines of logfmt above.
*
* There are two different kinds of log destination drivers. Raw drivers
* implement ld_event and receive the raw event struct for fully custom
* logging. Normal drivers implement ld_open and ld_close for FILE * based
* formatted logging. The FILE * produced by ld_open will be passed to the
* event formatter, which will use the log format driver to write a formatted
* log record to the FILE *.
*/
typedef int (*logdst_init_func_t)(config_t *);
typedef int (*logdst_reinit_func_t)(void);
typedef void (*logdst_fini_func_t)(void);
typedef FILE * (*logdst_open_func_t)(void);
typedef int (*logdst_close_func_t)(FILE *);
typedef int (*logdst_event_func_t)(const logevt_header_t *);
typedef struct {
const char *ld_name;
bool ld_raw; /* wants raw event, not formatted buffer */
bool ld_oneline; /* supports compact one-line format */
bool ld_multiline; /* supports readable multi-line format */
bool ld_onelineprefered; /* prefers oneline if both are available */
logdst_init_func_t ld_init;
logdst_reinit_func_t ld_reinit;
logdst_fini_func_t ld_fini;
logdst_event_func_t ld_event; /* raw mode only */
logdst_open_func_t ld_open; /* normal mode only */
logdst_close_func_t ld_close; /* normal mode only */
} logdst_t;
#define LOGDSTS 3
static logdst_t logdsttab[LOGDSTS] = {
{
"file", false, true, true, true,
logdstfile_init,
logdstfile_reinit,
logdstfile_fini,
NULL,
logdstfile_open,
logdstfile_close
},
{
"-", false, true, true, false,
logdststdout_init,
NULL,
logdststdout_fini,
NULL,
logdststdout_open,
logdststdout_close
},
{
"syslog", false, true, false, true,
logdstsyslog_init,
NULL,
logdstsyslog_fini,
NULL,
logdstsyslog_open,
logdstsyslog_close
}
};
int
logdst_parse(config_t *cfg, const char *name) {
assert(cfg);
assert(name);
for (size_t i = 1; i < LOGDSTS; i++) {
if (!strcmp(logdsttab[i].ld_name, name)) {
cfg->logdst = i;
return 0;
}
}
if (cfg->logfile)
free(cfg->logfile);
cfg->logfile = strdup(name);
if (!cfg->logfile)
return -1;
cfg->logdst = 0;
return 0;
}
const char *
logdst_s(config_t *cfg) {
return logdsttab[cfg->logdst].ld_name;
}
int
logfmt_parse(config_t *cfg, const char *name) {
assert(cfg);
assert(name);
for (size_t i = 0; i < LOGFMTS; i++) {
if (!strcmp(logfmttab[i]->lf_name, name)) {
cfg->logfmt = i;
return 0;
}
}
return -1;
}
const char *
logfmt_s(config_t *cfg) {
return logfmttab[cfg->logfmt]->lf_name;
}
static bool log_initialized = false;
static int logfmt = -1;
static int logdst = -1;
static queue_t log_queue;
static pthread_t log_thr;
static logevt_header_t log_sentinel;
static uint64_t counts[LOGEVT_SIZE];
static uint64_t errors;
static int
log_log(logevt_header_t *hdr) {
FILE *f;
int rv;
assert(logdst != -1);
assert(logdsttab[logdst].ld_raw || logfmt != -1);
assert(hdr->code >= 0 && hdr->code < LOGEVT_SIZE);
if (logdsttab[logdst].ld_raw) {
rv = logdsttab[logdst].ld_event(hdr);
} else {
f = logdsttab[logdst].ld_open();
if (!f)
return -1;
rv = le_logevt[hdr->code](logfmttab[logfmt], f, hdr);
if (logdsttab[logdst].ld_close(f) == -1)
errors++;
}
if (rv == 0)
counts[hdr->code]++;
else
errors++;
assert(hdr->le_free);
hdr->le_free(hdr);
return rv;
}
static void *
log_thread(UNUSED void *arg) {
logevt_header_t *hdr;
#if 0 /* terra pericolosa */
(void)policy_thread_sched_standard();
#endif
(void)policy_thread_diskio_utility();
for (;;) {
hdr = queue_dequeue(&log_queue);
if (hdr == &log_sentinel)
break;
(void)log_log(hdr);
}
return NULL;
}
int
log_init(config_t *cfg) {
logdst = cfg->logdst;
if (!logdsttab[logdst].ld_raw) {
logfmt = cfg->logfmt;
if ((!logfmttab[logfmt]->lf_oneline &&
!logdsttab[logdst].ld_multiline) ||
(!logfmttab[logfmt]->lf_multiline &&
!logdsttab[logdst].ld_oneline)) {
fprintf(stderr, "Incompatible logfmt and logdst\n");
return -1;
}
if (cfg->logoneline == -1)
cfg->logoneline =
logdsttab[logdst].ld_onelineprefered ? 1 : 0;
if (cfg->logoneline && (!logfmttab[logfmt]->lf_oneline ||
!logdsttab[logdst].ld_oneline))
cfg->logoneline = 0;
if (!cfg->logoneline && (!logfmttab[logfmt]->lf_multiline ||
!logdsttab[logdst].ld_multiline))
cfg->logoneline = 1;
}
logevt_init(cfg);
if (!logdsttab[logdst].ld_raw) {
if (logfmttab[logfmt]->lf_init(cfg) == -1) {
fprintf(stderr, "Failed to initialize logfmt %i\n",
logfmt);
return -1;
}
}
if (logdsttab[logdst].ld_init(cfg) == -1) {
fprintf(stderr, "Failed to initialize logdst %i\n", logdst);
return -1;
}
queue_init(&log_queue);
if (pthread_create(&log_thr, NULL, log_thread, NULL) != 0) {
queue_destroy(&log_queue);
logdsttab[logdst].ld_fini();
return -1;
}
errors = 0;
for (int i = 0; i < LOGEVT_SIZE; i++) {
counts[i] = 0;
}
log_initialized = true;
return 0;
}
int
log_reinit(void) {
assert(log_initialized);
if (!logdsttab[logdst].ld_reinit)
return 0;
if (logdsttab[logdst].ld_reinit() == -1) {
fprintf(stderr, "Failed to reinitialize logdst %i\n", logdst);
return -1;
}
return 0;
}
void
log_fini(void) {
if (!log_initialized)
return;
bzero(&log_sentinel, sizeof(log_sentinel));
queue_enqueue(&log_queue, &log_sentinel.node, &log_sentinel);
if (pthread_join(log_thr, NULL) != 0) {
fprintf(stderr, "Failed to join logger thread - exiting\n");
exit(EXIT_FAILURE);
}
assert(queue_size(&log_queue) == 0);
queue_destroy(&log_queue);
logdsttab[logdst].ld_fini();
logfmt = -1;
logdst = -1;
log_initialized = false;
}
void
log_submit(void *data) {
logevt_header_t *hdr = data;
assert(hdr);
assert(hdr->code >= 0);
assert(hdr->code <= LOGEVT_SIZE);
assert(hdr->tv.tv_sec > 0);
assert(hdr->le_free);
queue_enqueue(&log_queue, &hdr->node, hdr);
}
void
log_stats(log_stat_t *st) {
assert(st);
st->qsize = queue_size(&log_queue);
st->errors = errors;
for (size_t i = 0; i < LOGEVT_SIZE; i++)
st->counts[i] = counts[i];
}
void
log_version(FILE *f) {
fprintf(f, "Log event version: %i\n", LOGEVT_VERSION);
fprintf(f, "Available log formats are:");
for (size_t i = 0; i < LOGFMTS; i++) {
fprintf(f, " %s", logfmttab[i]->lf_name);
}
fprintf(f, "\n");
fprintf(f, "Available log destinations are:");
for (size_t i = 0; i < LOGDSTS; i++) {
fprintf(f, " %s", logdsttab[i].ld_name);
}
fprintf(f, "\n");
}
/*
* Convenience function to generate and submit a xnumon-ops event.
*/
static int
log_event_xnumon_ops(const char *subtype) {
xnumon_ops_t *evt;
evt = malloc(sizeof(xnumon_ops_t));
if (!evt)
return -1;
bzero(evt, sizeof(xnumon_ops_t));
evt->hdr.code = LOGEVT_XNUMON_OPS;
if (timespec_nanotime(&evt->hdr.tv) == -1) {
free(evt);
return -1;
}
evt->hdr.le_free = free;
evt->subtype = subtype;
work_submit(evt);
return 0;
}
/*
* Convenience function to generate and submit a xnumon-ops(start) event.
*/
int
log_event_xnumon_start(void) {
return log_event_xnumon_ops("start");
}
/*
* Convenience function to generate and submit a xnumon-ops(stop) event.
*/
int
log_event_xnumon_stop(void) {
return log_event_xnumon_ops("stop");
}
/*
* Convenience function to generate and submit a xnumon stats event.
*/
int
log_event_xnumon_stats(void) {
evtloop_stat_t *st;
st = malloc(sizeof(evtloop_stat_t));
if (!st)
return -1;
bzero(st, sizeof(evtloop_stat_t));
evtloop_stats(st);
st->hdr.code = LOGEVT_XNUMON_STATS;
if (timespec_nanotime(&st->hdr.tv) == -1) {
free(st);
return -1;
}
st->hdr.le_free = free;
work_submit(st);
return 0;
}