test: new regression test for initctl restart self

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2022-05-30 06:59:14 +02:00
1 parent ccb84ced2e
commit 75c5c85f07
3 files changed
+87 -2

No files matched your search

+38 -2
View File
@@ -47,6 +47,28 @@ static void verify_noenv(char *key)
errx(1, "Error, key %s is set in environment to '%s'", key, val);
}
static void inc_restarts(void)
{
char buf[10];
int cnt = 0;
FILE *fp;
fp = fopen("/tmp/serv-restart.cnt", "r+");
if (!fp) {
fp = fopen("/tmp/serv-restart.cnt", "w");
if (!fp)
err(1, "failed creating restart counter file");
} else {
if (fgets(buf, sizeof(buf), fp))
cnt = atoi(buf);
}
cnt++;
fseek(fp, SEEK_SET, 0);
fprintf(fp, "%d", cnt);
fclose(fp);
}
static void sig(int signo)
{
warnx("We got signal %d ...", signo);
@@ -86,6 +108,7 @@ static int usage(int rc)
" -n Run in foreground\n"
" -p Create PID file despite running in foreground\n"
" -P FILE Create PID file using FILE\n"
" -r SVC Call initctl to restart service SVC (self)\n"
"\n"
"By default this program daemonizes itself to the background, and,\n"
"when it's done setting up its signal handler(s), creates a PID file\n"
@@ -100,10 +123,12 @@ int main(int argc, char *argv[])
{
int do_background = 1;
int do_pidfile = 1;
int do_restart = 0;
char *pidfn = NULL;
char cmd[80];
int c;
while ((c = getopt(argc, argv, "e:E:hnpP:")) != EOF) {
while ((c = getopt(argc, argv, "e:E:hnpP:r:")) != EOF) {
switch (c) {
case 'h':
return usage(0);
@@ -123,6 +148,10 @@ int main(int argc, char *argv[])
case 'P':
pidfn = optarg;
break;
case 'r':
snprintf(cmd, sizeof(cmd), "initctl restart %s", optarg);
do_restart = 1;
break;
default:
return usage(1);
}
@@ -142,8 +171,15 @@ int main(int argc, char *argv[])
pidfile(pidfn);
warnx("Entering while(1) loop");
while (1)
while (1) {
sleep(1);
if (do_restart) {
inc_restarts();
if (system(cmd))
return 1;
break;
}
}
warnx("Leaving ...");
return 0;