-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaeternum.c
86 lines (75 loc) · 1.69 KB
/
aeternum.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
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <assert.h>
#include "aeternum.h"
static int json;
options_t aeternum_options(int argc, char *argv[]) {
options_t opts = options_parse(argc, argv);
return opts;
}
void aeternum_start(options_t opts) {
assert(opts.target != NULL);
assert(opts.child_args != NULL);
if (opts.json) json = 1;
aeternum_fork();
aeternum_redirect(opts.outfile, STDOUT_FILENO);
aeternum_redirect(opts.errfile, STDERR_FILENO);
aeternum_exec(opts.target, opts.child_args);
}
void aeternum_fork() {
signal(SIGCHLD, SIG_IGN);
signal(SIGHUP, SIG_IGN);
int pid = fork();
if (pid < 0) {
if (json) {
printf("{\"error\": \"%s\"}", strerror(errno));
}
else {
printf("An error occurred: %s", strerror(errno));
}
exit(pid);
}
else if (pid > 0) {
if (json) {
printf("{\"pid\": %d}", pid);
}
else {
printf("Child has been spawned and daemonized. PID: %d\n", pid);
}
exit(0);
}
if (setsid() == -1) {
perror("setsid()");
exit(errno);
}
umask(027);
}
void aeternum_redirect(const char *dest, int fileno) {
int out;
if (dest == NULL) {
dest = "/dev/null\0";
}
out = open(dest, O_WRONLY | O_APPEND | O_CREAT,
S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
if (out == -1) {
perror("open()");
exit(errno);
}
if (dup2(out, fileno) == -1) {
perror("dup2()");
exit(errno);
}
}
void aeternum_exec(const char *filename, char *args[]) {
if (execvp(filename, args) != 0) {
perror("execvp()");
exit(errno);
}
}