forked from dubiousjim/dcron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subs.c
169 lines (144 loc) · 4.38 KB
/
subs.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
/*
* SUBS.C
*
* Copyright 1994 Matthew Dillon (dillon@apollo.backplane.com)
* Copyright 2009-2019 James Pryor <dubiousjim@gmail.com>
* May be distributed under the GNU General Public License version 2 or any later version.
*/
#include "defs.h"
Prototype void printlogf(int level, const char *ctl, ...);
Prototype void fdprintlogf(int level, int fd, const char *ctl, ...);
Prototype void fdprintf(int fd, const char *ctl, ...);
Prototype void initsignals(void);
Prototype char Hostname[SMALL_BUFFER];
void vlog(int level, int fd, const char *ctl, va_list va);
char Hostname[SMALL_BUFFER];
void
printlogf(int level, const char *ctl, ...)
{
va_list va;
va_start(va, ctl);
vlog(level, 2, ctl, va);
va_end(va);
}
void
fdprintlogf(int level, int fd, const char *ctl, ...)
{
va_list va;
va_start(va, ctl);
vlog(level, fd, ctl, va);
va_end(va);
}
void
fdprintf(int fd, const char *ctl, ...)
{
va_list va;
char buf[LOG_BUFFER];
va_start(va, ctl);
vsnprintf(buf, sizeof(buf), ctl, va);
write(fd, buf, strlen(buf));
va_end(va);
}
void
vlog(int level, int fd, const char *ctl, va_list va)
{
char buf[LOG_BUFFER];
static short suppressHeader = 0;
if (level <= LogLevel) {
if (ForegroundOpt) {
/*
* when -d or -f, we always (and only) log to stderr
* fd will be 2 except when 2 is bound to a execing subprocess, then it will be 8
* [v]snprintf write at most size including \0; they'll null-terminate, even when they truncate
* we don't care here whether it truncates
*/
vsnprintf(buf, sizeof(buf), ctl, va);
write(fd, buf, strlen(buf));
} else if (SyslogOpt) {
/* log to syslog */
vsnprintf(buf, sizeof(buf), ctl, va);
syslog(level, "%s", buf);
} else {
/* log to file */
time_t t = time(NULL);
struct tm *tp = localtime(&t);
int buflen, hdrlen = 0;
buf[0] = 0; /* in case suppressHeader or strftime fails */
if (!suppressHeader) {
/*
* run LogHeader through strftime --> [yields hdr] plug in Hostname --> [yields buf]
*/
char hdr[SMALL_BUFFER];
/* strftime returns strlen of result, provided that result plus a \0 fit into buf of size */
if (strftime(hdr, sizeof(hdr), LogHeader, tp)) {
if (gethostname(Hostname, sizeof(Hostname))==0)
/* gethostname successful */
/* result will be \0-terminated except gethostname doesn't promise to do so if it has to truncate */
Hostname[sizeof(Hostname)-1] = 0;
else
Hostname[0] = 0; /* gethostname() call failed */
/* [v]snprintf write at most size including \0; they'll null-terminate, even when they truncate */
/* return value >= size means result was truncated */
if ((hdrlen = snprintf(buf, sizeof(hdr), hdr, Hostname)) >= sizeof(hdr))
hdrlen = sizeof(hdr) - 1;
}
}
if ((buflen = vsnprintf(buf + hdrlen, sizeof(buf) - hdrlen, ctl, va) + hdrlen) >= sizeof(buf))
buflen = sizeof(buf) - 1;
write(fd, buf, buflen);
/* if previous write wasn't \n-terminated, we suppress header on next write */
suppressHeader = (buf[buflen-1] != '\n');
}
}
}
void reopenlogger(int sig) {
int fd;
if (getpid() == DaemonPid) {
/* only daemon handles, children should ignore */
if ((fd = open(LogFile, O_WRONLY|O_CREAT|O_APPEND, 0600)) < 0) {
/* can't reopen log file, exit */
exit(errno);
}
dup2(fd, 2);
close(fd);
}
}
void waitmailjob(int sig) {
/*
* Wait for any children in our process group.
* These will all be mailjobs.
*/
pid_t child;
do {
child = waitpid(-DaemonPid, NULL, WNOHANG);
/* call was interrupted, try again: won't happen because we use SA_RESTART */
/* if (child == (pid_t)-1 && errno == EINTR) continue; */
} while (child > (pid_t) 0);
/* if no pending children, child,errno == -1,ECHILD */
/* if all children still running, child == 0 */
}
void
initsignals (void) {
struct sigaction sa;
int n;
/* save daemon's pid globally */
DaemonPid = getpid();
/* restart any system calls that were interrupted by signal */
sa.sa_flags = SA_RESTART;
if (!ForegroundOpt && !SyslogOpt)
sa.sa_handler = reopenlogger;
else
sa.sa_handler = SIG_IGN;
if (sigaction (SIGHUP, &sa, NULL) != 0) {
n = errno;
fdprintf(2, "failed to start SIGHUP handling, reason: %s", strerror(errno));
exit(n);
}
sa.sa_flags = SA_RESTART;
sa.sa_handler = waitmailjob;
if (sigaction (SIGCHLD, &sa, NULL) != 0) {
n = errno;
fdprintf(2, "failed to start SIGCHLD handling, reason: %s", strerror(errno));
exit(n);
}
}