summaryrefslogtreecommitdiffabout
path: root/src/genrc.c
Unidiff
Diffstat (limited to 'src/genrc.c') (more/less context) (show whitespace changes)
-rw-r--r--src/genrc.c41
1 files changed, 34 insertions, 7 deletions
diff --git a/src/genrc.c b/src/genrc.c
index ae3070d..9052987 100644
--- a/src/genrc.c
+++ b/src/genrc.c
@@ -25,7 +25,9 @@ enum {
25 OPT_SIGNAL_RELOAD, 25 OPT_SIGNAL_RELOAD,
26 OPT_NO_RELOAD, 26 OPT_NO_RELOAD,
27 OPT_SIGNAL_STOP, 27 OPT_SIGNAL_STOP,
28 OPT_CREATE_PIDFILE 28 OPT_CREATE_PIDFILE,
29 OPT_RESTART_ON_EXIT,
30 OPT_RESTART_ON_SIGNAL,
29}; 31};
30 32
31struct option longopts[] = { 33struct option longopts[] = {
@@ -45,6 +47,8 @@ struct option longopts[] = {
45 { "verbose", no_argument, 0, 'v' }, 47 { "verbose", no_argument, 0, 'v' },
46 { "user", required_argument, 0, 'u' }, 48 { "user", required_argument, 0, 'u' },
47 { "group", required_argument, 0, 'g' }, 49 { "group", required_argument, 0, 'g' },
50 { "restart-on-exit", required_argument, 0, OPT_RESTART_ON_EXIT },
51 { "restart-on-signal", required_argument, 0, OPT_RESTART_ON_SIGNAL },
48 { NULL } 52 { NULL }
49}; 53};
50char shortopts[] = "c:hF:g:P:p:St:u:v"; 54char shortopts[] = "c:hF:g:P:p:St:u:v";
@@ -127,9 +131,8 @@ is_numeric_str(char const *s)
127} 131}
128 132
129int 133int
130sig_name_to_str(char const *s) 134str_to_int(char const *s)
131{ 135{
132 if (is_numeric_str(s)) {
133 char *end; 136 char *end;
134 unsigned long n; 137 unsigned long n;
135 errno = 0; 138 errno = 0;
@@ -137,6 +140,13 @@ sig_name_to_str(char const *s)
137 if (errno || *end || n > UINT_MAX) 140 if (errno || *end || n > UINT_MAX)
138 return -1; 141 return -1;
139 return n; 142 return n;
143}
144
145int
146str_to_sig(char const *s)
147{
148 if (is_numeric_str(s)) {
149 return str_to_int(s);
140 } else { 150 } else {
141 struct sigdefn *sd; 151 struct sigdefn *sd;
142 152
@@ -183,8 +193,6 @@ char const *help_msg[] = {
183 "", 193 "",
184 " -t, --timeout=SECONDS time to wait for the program to start up or", 194 " -t, --timeout=SECONDS time to wait for the program to start up or",
185 " terminate", 195 " terminate",
186 " --sentinel PROGRAM runs in foreground; disconnect from the",
187 " controlling terminal, run it and act as a sentinel",
188 " -P, --pid-from=SOURCE where to look for PIDs of the running programs", 196 " -P, --pid-from=SOURCE where to look for PIDs of the running programs",
189 " -F, --pidfile=NAME name of the PID file", 197 " -F, --pidfile=NAME name of the PID file",
190 " (same as --pid-from=FILE:NAME)", 198 " (same as --pid-from=FILE:NAME)",
@@ -194,6 +202,17 @@ char const *help_msg[] = {
194 " --signal-stop=SIG signal to send in order to terminate the program", 202 " --signal-stop=SIG signal to send in order to terminate the program",
195 " (default: SIGTERM)", 203 " (default: SIGTERM)",
196 "", 204 "",
205 "Sentinel mode:",
206 "",
207 " --sentinel PROGRAM runs in foreground; disconnect from the",
208 " controlling terminal, run it and act as a sentinel",
209 " --restart-on-exit=[!]CODE[,...]",
210 " restart the program if it exits with one of the",
211 " listed status codes",
212 " --restart-on-signal=[!]SIG[,...]",
213 " restart the program if it terminates on one of the",
214 " listed signals",
215 "",
197 "Informational options:", 216 "Informational options:",
198 "", 217 "",
199 " -h, --help display this help list", 218 " -h, --help display this help list",
@@ -272,6 +291,8 @@ char const *usage_msg[] = {
272 "[--pid-from=SOURCE]", 291 "[--pid-from=SOURCE]",
273 "[--pidfile=PIDFILE]", 292 "[--pidfile=PIDFILE]",
274 "[--program=PROGRAM]", 293 "[--program=PROGRAM]",
294 "[--restart-on-exit=[!]CODE[,...]]",
295 "[--restart-on-signal=[!]SIG[,...]]",
275 "[--sentinel]", 296 "[--sentinel]",
276 "[--signal-reload=SIG]", 297 "[--signal-reload=SIG]",
277 "[--signal-stop=SIG]", 298 "[--signal-stop=SIG]",
@@ -422,6 +443,12 @@ main(int argc, char **argv)
422 case 'S': 443 case 'S':
423 setenv("GENRC_SENTINEL", "1", 1); 444 setenv("GENRC_SENTINEL", "1", 1);
424 break; 445 break;
446 case OPT_RESTART_ON_EXIT:
447 add_restart_condition(RESTART_ON_EXIT, optarg);
448 break;
449 case OPT_RESTART_ON_SIGNAL:
450 add_restart_condition(RESTART_ON_SIGNAL, optarg);
451 break;
425 case OPT_NO_RELOAD: 452 case OPT_NO_RELOAD:
426 no_reload = 1; 453 no_reload = 1;
427 break; 454 break;
@@ -450,7 +477,7 @@ main(int argc, char **argv)
450 if (no_reload) 477 if (no_reload)
451 genrc_no_reload = 1; 478 genrc_no_reload = 1;
452 else if ((p = getenv("GENRC_SIGNAL_RELOAD")) != NULL) { 479 else if ((p = getenv("GENRC_SIGNAL_RELOAD")) != NULL) {
453 genrc_signal_reload = sig_name_to_str(p); 480 genrc_signal_reload = str_to_sig(p);
454 if (genrc_signal_reload == -1) 481 if (genrc_signal_reload == -1)
455 usage_error("%s: invalid signal number", p); 482 usage_error("%s: invalid signal number", p);
456 else if (genrc_signal_reload == 0) 483 else if (genrc_signal_reload == 0)
@@ -458,7 +485,7 @@ main(int argc, char **argv)
458 } 485 }
459 486
460 if ((p = getenv("GENRC_SIGNAL_STOP")) != NULL) { 487 if ((p = getenv("GENRC_SIGNAL_STOP")) != NULL) {
461 genrc_signal_stop = sig_name_to_str(p); 488 genrc_signal_stop = str_to_sig(p);
462 if (genrc_signal_stop <= 0) 489 if (genrc_signal_stop <= 0)
463 usage_error("%s: invalid signal number", p); 490 usage_error("%s: invalid signal number", p);
464 } 491 }

Return to:

Send suggestions and report system problems to the System administrator.