forked from kernelslacker/trinity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log-files.c
126 lines (95 loc) · 2.16 KB
/
log-files.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
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include "log.h"
#include "params.h" // logging, quiet_level
#include "pids.h"
#include "shm.h"
#define BUFSIZE 1024 // decoded syscall args are fprintf'd directly, this is for everything else.
FILE *mainlogfile;
static bool logfiles_opened = FALSE;
static FILE *open_logfile(const char *logfilename)
{
FILE *file;
unlink(logfilename);
file = fopen(logfilename, "w");
if (!file)
outputerr("## couldn't open logfile %s\n", logfilename);
return file;
}
void open_main_logfile(void)
{
if (logging == LOGGING_DISABLED)
return;
mainlogfile = open_logfile("trinity.log");
if (!mainlogfile)
exit(EXIT_FAILURE);
logfiles_opened = TRUE; //FIXME: This is a bit crap
}
void open_child_logfile(struct childdata *child)
{
char *logfilename;
if (logging == LOGGING_DISABLED)
return;
logfilename = zmalloc(64);
sprintf(logfilename, "trinity-child%u.log", child->num);
child->logfile = open_logfile(logfilename);
if (!child->logfile) {
shm->exit_reason = EXIT_LOGFILE_OPEN_ERROR;
exit(EXIT_FAILURE);
}
free(logfilename);
child->logdirty = FALSE;
}
void close_logfile(FILE **filehandle)
{
if (logging == LOGGING_DISABLED)
return;
if (*filehandle == NULL)
return;
fclose(*filehandle);
*filehandle = NULL;
}
FILE *find_logfile_handle(void)
{
struct childdata *child;
pid_t pid;
if (logging == LOGGING_DISABLED)
return NULL;
if (!logfiles_opened)
return NULL;
pid = getpid();
if (pid == mainpid)
return mainlogfile;
child = this_child();
if (child != NULL)
return child->logfile;
return NULL;
}
/*
* Flush any pending log writes to disk.
* Only to be called from child context.
*/
void synclogs(void)
{
struct childdata *child;
int fd;
if (logging == LOGGING_DISABLED)
return;
child = this_child();
if (child->logdirty == FALSE)
return;
fflush(child->logfile);
fd = fileno(child->logfile);
if (fd != -1)
(void) fsync(fd);
child->logdirty = FALSE;
/* If we're flushing the child log, may as well flush
* any other logs while we're writing to disk.
*/
(void)fflush(mainlogfile);
fsync(fileno(mainlogfile));
}