-
Notifications
You must be signed in to change notification settings - Fork 30
/
logdstfile.c
101 lines (88 loc) · 1.68 KB
/
logdstfile.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
/*-
* xnumon - monitor macOS for malicious activity
* https://www.roe.ch/xnumon
*
* Copyright (c) 2017-2019, Daniel Roethlisberger <daniel@roe.ch>.
* All rights reserved.
*
* Licensed under the Open Software License version 3.0.
*/
#include "logdstfile.h"
#include "sys.h"
#include "attrib.h"
#include "config.h"
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
static config_t *config = NULL;
static FILE *f = NULL;
static gid_t gid;
static FILE *
logdstfile_open(void) {
return f;
}
static int
logdstfile_close(FILE *f) {
fflush(f);
return 0;
}
static int logdstfile_reinit(void);
static int
logdstfile_init(config_t *cfg) {
config = cfg;
gid = sys_gidbyname("admin");
logdstfile_reinit();
if (!f)
return -1;
/* remove incomplete last line, if any */
for (int offset = -1;; offset--) {
if (fseek(f, offset, SEEK_END) == -1) {
fseek(f, 0, SEEK_SET);
fflush(f);
ftruncate(fileno(f), ftello(f));
break;
}
if (fgetc(f) == '\n') {
fflush(f);
ftruncate(fileno(f), ftello(f));
break;
}
}
#if 0
/* ensure last log line is terminated */
if ((fseek(f, -1, SEEK_END) == 0) && (fgetc(f) != '\n'))
fputc('\n', f);
#endif
return 0;
}
static int
logdstfile_reinit(void) {
int fd;
assert(config);
if (f)
fclose(f);
f = fopen(config->logfile, "a+");
if (!f)
return -1;
fd = fileno(f);
(void)fchown(fd, 0, gid);
(void)fcntl(fd, F_NOCACHE, 1);
(void)fcntl(fd, F_SINGLE_WRITER, 1);
return 0;
}
static void
logdstfile_fini(void) {
if (f)
fclose(f);
config = NULL;
}
logdst_t logdstfile = {
"file", false, true, true, true,
logdstfile_init,
logdstfile_reinit,
logdstfile_fini,
NULL,
logdstfile_open,
logdstfile_close
};