-
Notifications
You must be signed in to change notification settings - Fork 36
/
god.c
415 lines (370 loc) · 9.04 KB
/
god.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// Copyright 2013-2016 Alexandre Fiori
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <grp.h>
#include <pwd.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#ifndef VERSION
#define VERSION "tip"
#endif
void usage() {
printf(
"Use: god [options] [--] program [arguments]\n"
"Options:\n"
"-h --help show this help and exit\n"
"-v --version show version and exit\n"
"-f --foreground run in foreground\n"
"-n --nohup make the program immune to SIGHUP\n"
"-l --logfile FILE write the program's stdout and stderr to FILE\n"
"-p --pidfile FILE write pid to FILE\n"
"-r --rundir DIR switch to DIR before executing the program\n"
"-u --user USER switch to USER before executing the program\n"
"-g --group GROUP switch to GROUP before executing the program\n"
"\nThe program's output go to a blackhole if no logfile is set.\n"
"Log files are recycled on SIGHUP.\n"
);
exit(1);
}
static int nohup = 0;
static int logfd[2]; // pipe
static pid_t childpid = 0;
static FILE *logfp = NULL;
static FILE *pidfp = NULL;
static char logfile[PATH_MAX];
static char pidfile[PATH_MAX];
static char linebuf[1024];
static pthread_mutex_t logger_mutex;
void daemon_main(int optind, char **argv);
void signal_init(sigset_t* old_sigmask);
void *signal_thread(void* arg);
void *logger_thread(void *cmdname);
char *exec_abspath(char *filename);
int main(int argc, char **argv) {
char rundir[PATH_MAX];
char user[64];
char group[64];
int foreground = 0;
memset(logfile, 0, sizeof logfile);
memset(pidfile, 0, sizeof pidfile);
memset(rundir, 0, sizeof rundir);
memset(user, 0, sizeof user);
memset(group, 0, sizeof group);
static struct option opts[] = {
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'v' },
{ "foreground", no_argument, NULL, 'f' },
{ "nohup", no_argument, NULL, 'n' },
{ "logfile", required_argument, NULL, 'l' },
{ "pidfile", required_argument, NULL, 'p' },
{ "rundir", required_argument, NULL, 'r' },
{ "user", required_argument, NULL, 'u' },
{ "group", required_argument, NULL, 'g' },
{ NULL, 0, NULL, 0 },
};
int ch;
while (1) {
ch = getopt_long(argc, argv, "l:p:r:u:g:hvfns", opts, NULL);
if (ch == -1)
break;
switch (ch) {
case 'v':
printf("Go daemon %s\n", VERSION);
printf("http://github.com/fiorix/go-daemon\n");
return 0;
case 'f':
foreground = 1;
break;
case 'n':
nohup = 1;
break;
case 'l':
strncpy(logfile, optarg, sizeof logfile - 1);
break;
case 'p':
strncpy(pidfile, optarg, sizeof pidfile - 1);
break;
case 'r':
strncpy(rundir, optarg, sizeof rundir - 1);
break;
case 'u':
strncpy(user, optarg, sizeof user - 1);
break;
case 'g':
strncpy(group, optarg, sizeof group - 1);
break;
default:
usage();
}
}
// utility is expected to be argv's leftovers.
if (optind >= argc)
usage();
if (*rundir != 0 && chdir(rundir) == -1) {
perror("failed to switch to rundir");
return 1;
}
struct passwd *pwd = NULL;
if ((*user && !(pwd = getpwnam(user)))) {
fprintf(stderr, "failed to get user %s: %s\n",
user, strerror(errno));
return 1;
}
struct group *grp = NULL;
if ((*group && !(grp = getgrnam(group)))) {
fprintf(stderr, "failed to get group %s: %s\n",
group, strerror(errno));
return 1;
}
if (grp && setregid(grp->gr_gid, grp->gr_gid) == -1) {
fprintf(stderr, "failed to switch to group %s: %s\n",
group, strerror(errno));
return 1;
}
const gid_t gid = getgid();
setgroups(1, &gid);
if (pwd && setreuid(pwd->pw_uid, pwd->pw_uid) == -1) {
fprintf(stderr, "failed to switch to user %s: %s\n",
user, strerror(errno));
return 1;
}
if (*logfile && (logfp = fopen(logfile, "a")) == NULL) {
perror("failed to open logfile");
return 1;
}
if (logfp)
setvbuf(logfp, linebuf, _IOLBF, sizeof linebuf);
if (*pidfile && (pidfp = fopen(pidfile, "w+")) == NULL) {
perror("failed to open pidfile");
return 1;
}
char *abspath;
if (!(abspath = exec_abspath(argv[optind]))) {
perror(argv[optind]);
return 1;
}
argv[optind] = abspath;
if (foreground) {
daemon_main(optind, argv);
return 0;
}
// Daemonize.
pid_t pid = fork();
if (pid > 0) {
waitpid(pid, NULL, 0);
} else if (!pid) {
if(setsid() < 0) {
perror("setsid");
exit(1);
}
if ((pid = fork()) > 0) {
exit(0);
} else if (!pid) {
daemon_main(optind, argv);
} else {
perror("fork");
exit(1);
}
} else {
perror("fork");
exit(1);
}
return 0;
}
void daemon_main(int optind, char **argv) {
if (pidfp) {
fprintf(pidfp, "%d\n", getpid());
fclose(pidfp);
}
sigset_t old_sigmask;
signal_init(&old_sigmask);
pipe(logfd);
if ((childpid = fork()) > 0) {
close(0);
close(1);
close(2);
close(logfd[1]);
pthread_t logth;
pthread_create(&logth, NULL, logger_thread, argv[optind]);
waitpid(childpid, NULL, 0);
pthread_join(logth, NULL);
} else if (!childpid) {
pthread_sigmask(SIG_SETMASK, &old_sigmask, NULL);
close(logfd[0]);
dup2(logfd[1], 1);
dup2(logfd[1], 2);
if (logfp) {
fclose(logfp);
}
execvp(argv[optind], argv + optind);
printf("\x1b%s", strerror(errno));
fflush(stdout);
close(logfd[1]);
close(1);
close(2);
} else {
perror("fork");
exit(1);
}
if (pidfp)
unlink(pidfile);
}
void signal_init(sigset_t* old_sigmask) {
sigset_t new_sigmask;
if(sigfillset(&new_sigmask)) {
perror("sigfillset");
exit(1);
}
size_t i;
int no_fwd_signals[] = {SIGFPE, SIGILL, SIGSEGV, SIGBUS, SIGABRT, SIGTRAP, SIGSYS};
for (i = 0; i < (sizeof(no_fwd_signals)/sizeof(no_fwd_signals[0])); i++) {
if(sigdelset(&new_sigmask, no_fwd_signals[i])) {
perror("sigdelset");
exit(1);
}
}
if(pthread_sigmask(SIG_SETMASK, &new_sigmask, old_sigmask)) {
perror("pthread_sigmask");
exit(1);
}
pthread_t signalth;
if(pthread_create(&signalth, NULL, signal_thread, &new_sigmask)) {
perror("pthread_create");
exit(1);
}
}
void *signal_thread(void* arg) {
sigset_t *sigmask = (sigset_t *)arg;
while (1) {
int signum;
if(sigwait(sigmask, &signum)) {
perror("sigwait");
exit(1);
}
if(signum == SIGHUP) {
pthread_mutex_lock(&logger_mutex);
if (logfp) {
FILE *fp = fopen(logfile, "a");
if (fp != NULL) {
fclose(logfp);
logfp = fp;
setvbuf(logfp, linebuf, _IOLBF, sizeof linebuf);
}
}
pthread_mutex_unlock(&logger_mutex);
if (!nohup && childpid) // nonohup :~
kill(childpid, signum);
} else {
if (childpid)
kill(childpid, signum);
}
}
}
void *logger_thread(void *cmdname) {
int n;
char buf[4096];
int has_read = 0;
while(1) {
// read() will fail when the child process fails
// to execute or dies, and closes its terminal.
// This is what terminates this thread and therefore
// the main thread can move along.
n = read(logfd[0], buf, sizeof buf);
if (n <= 0)
break;
buf[n] = 0;
if (!has_read) {
has_read = 1;
if (*buf == '\x1b') {
char *p = buf;
printf("%s: %s\n", (char *) cmdname, ++p);
close(logfd[0]);
break;
}
}
pthread_mutex_lock(&logger_mutex);
if (logfp) {
fwrite(buf, 1, n, logfp);
//fflush(logfp);
}
pthread_mutex_unlock(&logger_mutex);
}
pthread_mutex_lock(&logger_mutex);
if (logfp) {
fflush(logfp);
fclose(logfp);
}
pthread_mutex_unlock(&logger_mutex);
return NULL;
}
int exec_ok(char *filename) {
struct stat st;
if (stat(filename, &st) < 0) {
errno = ENOENT;
return 0;
}
if (S_ISDIR(st.st_mode)) {
errno = EISDIR;
return 0;
}
if (!(st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) {
errno = EACCES;
return 0;
}
return 1;
}
// exec_abspath returns the absolute path of the given file name by
// looking it up in the current directory and the directories listed
// in the PATH environment variable, only if the file is executable.
// The returned pointer points to a static buffer that is reused on
// subsequent calls. In case the file does not exist, or does not have
// executable permissions, returns NULL and errno is set accordingly.
char *exec_abspath(char *filename) {
static char abspath[PATH_MAX];
if (!filename) {
errno = ENOENT;
return NULL;
}
if (*filename == '.' || *filename == '/') {
switch (exec_ok(filename)) {
case 1: return realpath(filename, abspath);
default: return NULL;
}
}
char *path = getenv("PATH");
if (!path) return NULL;
char *v[(PATH_MAX/2)], **paths = v;
char *p = strdup(path);
path = p;
while (*p) {
while (*p && *p == ':') *p++ = 0;
if (*p) *(paths++) = p;
while (*p && *p != ':') p++;
}
*paths = NULL;
int l, fnlen = strlen(filename);
for (paths = v; *paths; paths++) {
l = strlen(*paths) + fnlen + 2;
if (l > sizeof(abspath)) continue;
snprintf(abspath, l, "%s/%s", *paths, filename);
if (exec_ok(abspath)) {
free(path);
return abspath;
}
}
free(path);
return NULL;
}