/
workthread.c
328 lines (252 loc) · 8.12 KB
/
workthread.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
/*
* workthread.c
* Execute command, read input from a FIFO, write output to a logfile.
*
* Copyright (c) 2021-2023 jjb
* All rights reserved.
*
* This source code is licensed under the MIT license found
* in the root directory of this source tree.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <pwd.h>
#include <unistd.h>
#include "sentinal.h"
#include "basename.h"
#define PIPEBUFSIZ (4 << 20) /* 4MiB, better size for IPC i/o */
#define ROTATE(lim,n,sig) ((lim && n > lim) || sig == SIGHUP)
#define STAT(file,buf) (stat(file, &buf) == -1 ? -1 : buf.st_size)
#define SLOWEXIT(code) { sleep(5); exit(code); }
static int fifoopen(struct thread_info *);
static void fifosize(struct thread_info *, int);
void *workthread(void *arg)
{
char filename[PATH_MAX]; /* full pathname */
char homebuf[BUFSIZ]; /* hone env var */
char pathbuf[BUFSIZ]; /* path env var */
char pcrebuf[BUFSIZ]; /* env convenience */
char pipebuf[PIPEBUFSIZ]; /* pipe read buffer */
char shellbuf[BUFSIZ]; /* shell env var */
char tmplbuf[BUFSIZ]; /* env convenience */
char *home; /* from passwd file entry */
char *zargv[MAXARGS]; /* arglist for fifo reader */
int holdfd = 0; /* fd to hold FIFO open */
int i;
int logfd; /* primary FIFO fd */
int pipefd[2]; /* pipe readers and writers */
int status; /* child status codes */
ssize_t n; /* FIFO read */
struct passwd *p;
struct stat stbuf; /* file status */
struct thread_info *ti = arg; /* thread settings */
/*
* this thread requires:
* - ti_command
* - ti_pipename
* - ti_template
*
* optional, but recommended:
* - ti_rotatesiz
*
* optional:
* - ti_postcmd
*
* optional, likely required by use case:
* - ti_uid
* - ti_gid
*/
pthread_setname_np(pthread_self(), threadname(ti, _WRK_THR));
fprintf(stderr, "%s: command: %s\n", ti->ti_section, ti->ti_command);
if(ti->ti_rotatesiz)
fprintf(stderr, "%s: monitor file: %s for size %s\n",
ti->ti_section, ti->ti_pcrestr, ti->ti_rotatestr);
for(;;) {
/* set up pipes */
if((logfd = fifoopen(ti)) == -1) {
/* open/create FIFO failed */
return ((void *)0);
}
if(pipe(pipefd) == -1) {
fprintf(stderr, "%s: can't create IPC pipe\n", ti->ti_section);
return ((void *)0);
}
logname(ti->ti_template, ti->ti_filename);
fullpath(ti->ti_dirname, ti->ti_filename, filename);
workcmd(ti->ti_argc, ti->ti_argv, zargv);
/* for systemctl status sentinal */
fprintf(stderr, "%s: ", ti->ti_section);
for(i = 0; zargv[i]; i++)
fprintf(stderr, "%s ", zargv[i]);
fprintf(stderr, "> %s\n", ti->ti_filename); /* show redirect */
/* get going */
switch (ti->ti_pid = fork()) {
case -1:
fprintf(stderr, "%s: can't fork command\n", ti->ti_section);
return ((void *)0);
case 0:
/*
* child: command reads from sentinal, writes to stdout
* application -> FIFO -> sentinal -> IPC pipe -> command -> logfile
*/
if(geteuid() == (uid_t) 0) {
(void)setgid(ti->ti_gid);
(void)setuid(ti->ti_uid);
}
if(access(ti->ti_dirname, R_OK | W_OK | X_OK) == -1) {
fprintf(stderr, "%s: insufficient permissions for %s\n",
ti->ti_section, ti->ti_dirname);
SLOWEXIT(EXIT_FAILURE);
}
if(chdir(ti->ti_dirname) == -1) {
fprintf(stderr, "%s: can't cd to %s\n", ti->ti_section, ti->ti_dirname);
SLOWEXIT(EXIT_FAILURE);
}
close(pipefd[1]); /* close unused write end */
dup2(pipefd[0], STDIN_FILENO); /* new stdin */
close(STDOUT_FILENO);
if(open(filename, O_WRONLY | O_CREAT, 0644) == -1) {
/* shouldn't happen */
fprintf(stderr, "%s: can't create %s\n", ti->ti_section, ti->ti_filename);
SLOWEXIT(EXIT_FAILURE);
} else {
/* new stdout -- close parent's and unused fds */
for(i = 3; i < MAXFILES; i++)
close(i);
/* execution environment */
home = (p = getpwuid(ti->ti_uid)) ? p->pw_dir : "/tmp";
if(snprintf(homebuf, BUFSIZ, "HOME=%s", home) < BUFSIZ)
putenv(homebuf);
if(snprintf(pathbuf, BUFSIZ, "PATH=%s", PATH) < BUFSIZ)
putenv(pathbuf);
if(snprintf(shellbuf, BUFSIZ, "SHELL=%s", BASH) < BUFSIZ)
putenv(shellbuf);
if(snprintf(tmplbuf, BUFSIZ, "TEMPLATE=%s", ti->ti_template) < BUFSIZ)
putenv(tmplbuf);
if(snprintf(pcrebuf, BUFSIZ, "PCRESTR=%s", ti->ti_pcrestr) < BUFSIZ)
putenv(pcrebuf);
umask(umask(0) | 022); /* don't set less restrictive */
nice(1);
execv(ti->ti_path, zargv);
exit(EXIT_FAILURE);
}
default:
/*
* parent: sentinal reads from FIFO, writes to stdout
* application -> FIFO -> sentinal -> IPC pipe -> command -> logfile
*/
close(pipefd[0]); /* close unused read end */
ti->ti_wfd = pipefd[1]; /* save fd for close */
/* parent needs to keep the pipe open for reading */
if(holdfd > 0)
close(holdfd);
holdfd = open(ti->ti_pipename, O_RDONLY | O_NONBLOCK);
/* begin */
ti->ti_sig = 0; /* reset */
for(;;) {
if((n = read(logfd, pipebuf, PIPEBUFSIZ)) <= 0)
break;
if(write(ti->ti_wfd, pipebuf, (size_t)n) == -1) {
fprintf(stderr, "%s: write failed %s\n", ti->ti_section,
ti->ti_filename);
break;
}
if(ROTATE(ti->ti_rotatesiz, STAT(filename, stbuf), ti->ti_sig)) {
/* ti_rotatesiz or signaled to logrotate */
fprintf(stderr, "%s: rotate %s\n", ti->ti_section, ti->ti_filename);
ti->ti_sig = 0; /* reset */
break;
}
}
/* done */
if(n == 0) { /* writer is gone */
close(holdfd);
holdfd = 0;
}
close(ti->ti_wfd);
waitpid(ti->ti_pid, &status, 0);
ti->ti_wfd = EOF; /* done with this file */
/* if file is empty, write failed, e.g. */
/* No space left on device (cannot write compressed block) */
if(STAT(filename, stbuf) > 0) { /* success */
if(NOT_NULL(ti->ti_postcmd))
if((status = postcmd(ti, filename)) != 0) {
fprintf(stderr, "%s: postcmd exit: %d\n", ti->ti_section, status);
sleep(5); /* be nice */
}
} else { /* fail */
remove(filename); /* exists, CWE-367 N/A */
sleep(5); /* be nice */
}
close(logfd); /* pipe remains held open by holdfd */
}
}
/* notreached */
return ((void *)0);
}
static void fifosize(struct thread_info *ti, int size)
{
/* max pipesize is in /proc/sys/fs/pipe-max-size */
/* kernels 2.6.35 and newer */
#ifdef F_SETPIPE_SZ /* undefined in < 2.6.35 kernels */
int fd;
if((fd = open(ti->ti_pipename, O_RDONLY | O_NONBLOCK)) == -1)
return;
if(fcntl(fd, F_GETPIPE_SZ) != size)
fcntl(fd, F_SETPIPE_SZ, size);
close(fd);
#endif /* F_SETPIPE_SZ */
}
static int fifoopen(struct thread_info *ti)
{
int fd;
int status;
pid_t pid;
short pflag = FALSE;
struct stat stbuf; /* file status */
/* create a FIFO. note: this permits symlinks to FIFOs */
if(lstat(ti->ti_pipename, &stbuf) == 0) { /* found something */
if(!S_ISFIFO(stbuf.st_mode)) {
fprintf(stderr, "%s: not a FIFO: %s\n", ti->ti_section,
base(ti->ti_pipename));
return (-1);
}
} else
pflag = TRUE;
if(pflag) { /* need a FIFO */
/* fork to set ids */
if((pid = fork()) == 0) {
if(geteuid() == (uid_t) 0) {
(void)setgid(ti->ti_gid);
(void)setuid(ti->ti_uid);
}
if(mkfifo(ti->ti_pipename, 0600) == -1) {
fprintf(stderr, "%s: can't mkfifo %s: %s\n", ti->ti_section,
base(ti->ti_pipename), strerror(errno));
exit(EXIT_FAILURE);
}
fifosize(ti, FIFOSIZ);
exit(EXIT_SUCCESS);
}
if(pid > 0)
waitpid(pid, &status, 0);
}
if((fd = open(ti->ti_pipename, O_RDONLY)) == -1) {
/* systemctl restart can cause EINTR */
if(errno != EINTR)
fprintf(stderr, "%s: can't open %s: %s\n", ti->ti_section,
base(ti->ti_pipename), strerror(errno));
} else {
/* reenforce in case these were changed externally */
chown(ti->ti_pipename, ti->ti_uid, ti->ti_gid);
chmod(ti->ti_pipename, 0600);
}
return (fd);
}
/* vim: set tabstop=4 shiftwidth=4 noexpandtab: */