forked from jas88/smideploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smiinit.cpp
193 lines (171 loc) · 5 KB
/
smiinit.cpp
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
/*
* Copyright 2021 James A Sutherland / University of Dundee
* */
#include <yaml-cpp/yaml.h>
#include <assert.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <poll.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <string.h>
#include <map>
#include <vector>
static int debug = 0;
std::vector <pid_t> kids;
std::map <pid_t, char * const> jobs;
static void shutdown();
static pid_t spawn(char * const cmd, const char **env);
static void shutdown() {
auto it = jobs.begin();
while (it != jobs.end()) {
if (kill(it->first, SIGTERM)) {
fprintf(stderr, "Error %d sending SIGTERM to PID %d (%s)\n",
errno, it->first, it->second);
free(it->second);
}
it++;
}
jobs.clear();
sleep(3);
for (pid_t kid : kids) {
if ((waitpid(kid, NULL, WNOHANG) == 0)
&& kill(kid, SIGKILL)
&& errno != ESRCH) {
fprintf(stderr, "Error %d sending SIGTERM to PID %d\n", errno, kid);
}
}
kids.clear();
}
static pid_t spawn(char * cmd, const char **env) {
static const char *shell = "/bin/sh", *dashc = "-c";
pid_t kid;
switch (kid = fork()) {
case -1:
perror("fork");
return -1;
case 0:
{
const char *args[] = { shell, dashc, cmd, 0 };
execve(shell, (char*const*)args, (char*const*)env);
perror(cmd);
return -1; // Only really returns on failure
}
default:
cmd = strdup(cmd);
jobs.insert(std::pair <pid_t, char * const>(kid, cmd));
return kid;
}
}
static int init(const char *conffile, const char **env) {
YAML::Node config;
try {
config = YAML::LoadFile(conffile);
} catch (std::exception&) {
fprintf(stderr, "Missing or invalid config.yaml\n");
return 1;
}
if (config["env"] && config["env"].IsMap()) {
for(YAML::const_iterator it=config["env"].begin();it!=config["env"].end();++it) {
setenv(it->first.as<std::string>().c_str(),it->second.as<std::string>().c_str(),1);
}
}
if (!config["jobs"] || !config["jobs"].IsSequence()) {
fprintf(stderr, "No jobs configured in %s\n", conffile);
return 1;
}
for (auto job : config["jobs"]) {
auto jobstring = job.as<std::string>();
char *s = strdup(jobstring.c_str());
if (strcmp(s, "wait") == 0) {
fprintf(stderr, "Waiting 30 seconds for daemons to initialise\n");
sleep(30);
} else {
spawn(s, env);
fprintf(stderr, "Job: %s\n", s);
}
free(s);
}
return 0;
}
static int help(char *name) {
fprintf(stderr, "Usage:\t%s [switches]\n"
"\t-c <dir> \tChange to directory\n"
"\t-d \tDebug\n"
"\t-f <file>\tUse the specified YAML configuration file\n"
"\t-h \tPrint help text and exit\n"
"\t-v \tPrint version number and exit\n", name);
return 0;
}
static int version() {
return 0;
}
static int loop(const char *conffile, const char **env) {
int signum;
sigset_t mask = {0};
sigemptyset(&mask);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGTERM);
sigaddset(&mask, SIGCHLD);
if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1) {
perror("sigprocmask");
return 1;
}
if (init(conffile, env) > 0)
return 1;
while (true) {
if (sigwait(&mask, &signum)) {
perror("sigwait");
return 1;
}
if (signum != SIGCHLD) {
shutdown();
return 0;
} else {
pid_t n;
int status;
while ((n = waitpid(-1, &status, WNOHANG)) > 0) {
fprintf(stderr, "%d exited with status %d, respawning\n",
n, status);
try {
spawn(jobs.at(n), env);
free(jobs.at(n));
jobs.erase(n);
} catch (std::out_of_range&) {
// Unwanted orphan, ignore
}
}
}
}
}
int main(int argc, char * const *argv, const char **env) {
const char *conffile = "config.yaml";
YAML::Node config;
int option;
while ((option=getopt(argc, argv, "c:df:hv")) != -1) {
switch (option) {
case 'c':
if (chdir(optarg)==-1) {
perror("chdir");
return 1;
}
break;
case 'd':
debug++;
break;
case 'f':
conffile = optarg;
break;
case 'h':
return help(*argv);
case 'v':
return version();
default:
fprintf(stderr, "Warning: Unknown option '%c'\n", option);
break;
}
}
return loop(conffile, env);
}