/
aeternum.c
237 lines (199 loc) · 4.97 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
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
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <pwd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include "uv.h"
#include "options.h"
#ifdef __WIN32
#include <shellapi.h>
#include <Winerror.h>
#else
extern char **environ;
#endif
static char *pidfile;
static char *outfile;
static char *errfile;
options_t opts;
uv_loop_t *loop;
uv_process_t child_req;
uv_process_options_t options;
void spawn_cb(uv_process_t*, int, int);
void configure_stdio();
void write_pid_file(int pid, char *pidname) {
char buf[33];
int fd = -1, n = 0;
fd = open(pidname, O_WRONLY | O_TRUNC | O_CREAT, 0660);
if (fd == -1)
goto cleanup;
if ((n = snprintf(buf, sizeof buf, "%d", pid)) < 0)
goto cleanup;
if (write(fd, buf, n) < 0)
goto cleanup;
cleanup:
if (errno != 0) fprintf(stderr, "%s\n", strerror(errno));
if (fd != -1) close(fd);
}
void cleanup_pid_file(char *pidname) {
if (unlink(pidname) == -1) {
fprintf(stderr, "Could not remove %s: %s\n", pidname, strerror(errno));
}
free(pidname);
}
void spawn_child(int detach) {
uv_stdio_container_t stdio[3];
int i;
options.stdio_count = 3;
if (detach) {
for (i = 0; i < options.stdio_count; i++) {
stdio[i].flags = UV_IGNORE;
}
}
else {
for (i = 0; i < options.stdio_count; i++) {
stdio[i].flags = UV_INHERIT_FD;
stdio[i].data.fd = i;
}
}
options.file = opts.child_args[0];
options.args = opts.child_args;
options.stdio = stdio;
options.env = environ;
options.exit_cb = detach ? NULL : spawn_cb;
if (detach) options.flags = UV_PROCESS_DETACHED;
if (uv_spawn(loop, &child_req, options)) {
fprintf(stderr, "Error %s\n", uv_err_name(uv_last_error(loop)));
fprintf(stderr, "%s\n", uv_strerror(uv_last_error(loop)));
}
if (detach) {
uv_unref((uv_handle_t*)&child_req);
return;
}
write_pid_file(child_req.pid, pidfile);
}
void spawn_cb(uv_process_t *req, int exit_status, int signal_status) {
if (signal_status) {
fprintf(stderr, "Child killed by signal %d, restarting.\n", signal_status);
}
else {
fprintf(stderr, "Child exited with %d, restarting.\n", exit_status);
}
spawn_child(0);
}
void handle_signal(int signal_status) {
fprintf(stderr, "Aeternum kill by signal %d, exiting.\n", signal_status);
uv_process_kill(&child_req, SIGKILL);
cleanup_pid_file(pidfile);
}
int stdio_redirect(char *dest, int fd) {
int out;
if (dest == NULL) {
dest = "/dev/null";
}
out = open(dest, O_WRONLY | O_APPEND | O_CREAT, 0660);
if (out == -1) {
close(out);
perror("stdio_redirect");
return -1;
}
else if (dup2(out, fd) == -1) {
close(out);
perror("stdio_redirect");
return -1;
}
return out;
}
void configure_stdio() {
outfile = opts.outfile;
errfile = opts.errfile;
stdio_redirect(opts.infile, STDIN_FILENO);
stdio_redirect(opts.outfile, STDOUT_FILENO);
if (opts.errfile != NULL) {
stdio_redirect(opts.errfile, STDERR_FILENO);
}
else {
stdio_redirect(opts.outfile, STDERR_FILENO);
}
}
void set_pidfile_path(char *pidname) {
#ifdef _WIN32
WCHAR homedir[MAX_PATH];
if (FAILED(SHGetFolderPathW(NULL, CSIDL_PROFILE, NULL, 0, path)) {
perror("SHGetFolderPathW");
return;
}
#else
const char *homedir = getenv("HOME");
if (!homedir) {
struct passwd *pw = getpwuid(getuid());
homedir = pw->pw_dir;
}
#endif
char *basepath = malloc(strlen(homedir) + 12);
struct stat has_dir;
sprintf(basepath, "%s/.aeternum/", homedir);
if (lstat(basepath, &has_dir) == -1) {
if (errno == ENOENT) {
if (mkdir(basepath, 0755) == -1) {
perror("mkdir");
}
}
else {
return;
}
}
pidfile = malloc(snprintf(NULL, 0, "%s%s", basepath, pidname) + 1);
sprintf(pidfile, "%s%s", basepath, pidname);
free(basepath);
}
int main(int argc, char *argv[]) {
int r;
if (signal (SIGINT, handle_signal) == SIG_IGN)
signal (SIGINT, SIG_IGN);
if (signal (SIGHUP, handle_signal) == SIG_IGN)
signal (SIGHUP, SIG_IGN);
if (signal (SIGTERM, handle_signal) == SIG_IGN)
signal (SIGTERM, SIG_IGN);
loop = uv_default_loop();
if (argc == 1) {
printf("Usage: aeternum [action] [options] -- program\n");
return 1;
}
if (strcmp(argv[1], "start") == 0) {
argv[1] = "run";
opts.infile = NULL;
opts.outfile = NULL;
opts.errfile = NULL;
opts.pidname = NULL;
opts.target = argv[0];
opts.json = 0;
opts.child_args = argv;
spawn_child(1);
printf("%d\n", child_req.pid);
return 0;
}
if (strcmp(argv[1], "run") == 0) {
argv[1] = argv[0];
argv = &argv[1];
argc--;
}
opts = options_parse(argc, argv);
if (opts.pidname != NULL) {
if (strcspn(opts.pidname, "/") < strlen(opts.pidname))
pidfile = strdup(opts.pidname);
else set_pidfile_path(opts.pidname);
}
else {
set_pidfile_path(opts.target);
}
configure_stdio();
spawn_child(0);
r = uv_run(loop, UV_RUN_DEFAULT);
free(pidfile);
return r;
}