procd: skip respawn in case device disappeared
[project/procd.git] / inittab.c
index 6dde11af1a7ec9a1744b6fbd73e627f7061a840a..b67730c4a5b0a1ae3bba7736f0be4f43c01c3c30 100644 (file)
--- a/inittab.c
+++ b/inittab.c
 #include "procd.h"
 #include "rcS.h"
 
+#ifndef O_PATH
+#define O_PATH         010000000
+#endif
+
 #define TAG_ID         0
 #define TAG_RUNLVL     1
 #define TAG_ACTION     2
@@ -100,7 +104,7 @@ static void fork_worker(struct init_action *a)
                tcsetpgrp(STDIN_FILENO, p);
 
                execvp(a->argv[0], a->argv);
-               ERROR("Failed to execute %s\n", a->argv[0]);
+               ERROR("Failed to execute %s: %m\n", a->argv[0]);
                exit(-1);
        }
 
@@ -116,14 +120,22 @@ static void child_exit(struct uloop_process *proc, int ret)
 {
        struct init_action *a = container_of(proc, struct init_action, proc);
 
-       DEBUG(4, "pid:%d\n", proc->pid);
-        uloop_timeout_set(&a->tout, a->respawn);
+       DEBUG(4, "pid:%d, exitcode:%d\n", proc->pid, ret);
+       proc->pid = 0;
+
+       if (!dev_exist(a->id)) {
+               DEBUG(4, "Skipping respawn: device '%s' does not exist anymore\n", a->id);
+               return;
+       }
+
+       uloop_timeout_set(&a->tout, a->respawn);
 }
 
 static void respawn(struct uloop_timeout *tout)
 {
        struct init_action *a = container_of(tout, struct init_action, tout);
-       fork_worker(a);
+       if (!a->proc.pid)
+               fork_worker(a);
 }
 
 static void rcdone(struct runqueue *q)
@@ -159,7 +171,8 @@ static void askfirst(struct init_action *a)
        a->respawn = 500;
 
        a->proc.cb = child_exit;
-       fork_worker(a);
+       if (!a->proc.pid)
+               fork_worker(a);
 }
 
 static void askconsole(struct init_action *a)
@@ -193,7 +206,8 @@ static void askconsole(struct init_action *a)
        a->respawn = 500;
 
        a->proc.cb = child_exit;
-       fork_worker(a);
+       if (!a->proc.pid)
+               fork_worker(a);
 }
 
 static void rcrespawn(struct init_action *a)
@@ -202,7 +216,8 @@ static void rcrespawn(struct init_action *a)
        a->respawn = 500;
 
        a->proc.cb = child_exit;
-       fork_worker(a);
+       if (!a->proc.pid)
+               fork_worker(a);
 }
 
 static struct init_handler handlers[] = {
@@ -224,6 +239,14 @@ static struct init_handler handlers[] = {
                .name = "respawn",
                .cb = rcrespawn,
                .multi = 1,
+       }, {
+               .name = "askconsolelate",
+               .cb = askconsole,
+               .multi = 1,
+       }, {
+               .name = "respawnlate",
+               .cb = rcrespawn,
+               .multi = 1,
        }
 };
 
@@ -266,14 +289,13 @@ void procd_inittab(void)
        char *line;
 
        if (!fp) {
-               ERROR("Failed to open %s\n", tab);
+               ERROR("Failed to open %s: %m\n", tab);
                return;
        }
 
        regcomp(&pat_inittab, "([a-zA-Z0-9]*):([a-zA-Z0-9]*):([a-zA-Z0-9]*):(.*)", REG_EXTENDED);
        line = malloc(LINE_LEN);
-       a = malloc(sizeof(struct init_action));
-       memset(a, 0, sizeof(struct init_action));
+       a = calloc(1, sizeof(struct init_action));
 
        while (fgets(line, LINE_LEN, fp)) {
                char *tags[TAG_PROCESS + 1];
@@ -291,7 +313,7 @@ void procd_inittab(void)
                if (regexec(&pat_inittab, line, 5, matches, 0))
                        continue;
 
-               DEBUG(4, "Parsing inittab - %s", line);
+               DEBUG(4, "Parsing inittab - %s\n", line);
 
                for (i = TAG_ID; i <= TAG_PROCESS; i++) {
                        line[matches[i].rm_eo] = '\0';
@@ -310,8 +332,7 @@ void procd_inittab(void)
                if (add_action(a, tags[TAG_ACTION]))
                        continue;
                line = malloc(LINE_LEN);
-               a = malloc(sizeof(struct init_action));
-               memset(a, 0, sizeof(struct init_action));
+               a = calloc(1, sizeof(struct init_action));
        }
 
        fclose(fp);