system: fix localtime value in ubus info method output
[project/procd.git] / initd / preinit.c
index c5f7ada..729978e 100644 (file)
@@ -15,6 +15,7 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <sys/mount.h>
+#include <fcntl.h>
 
 #include <libubox/uloop.h>
 #include <libubox/utils.h>
@@ -31,11 +32,29 @@ static struct uloop_process preinit_proc;
 static struct uloop_process plugd_proc;
 
 static void
+check_dbglvl(void)
+{
+       FILE *fp = fopen("/tmp/debug_level", "r");
+       int lvl = 0;
+
+       if (!fp)
+               return;
+       if (fscanf(fp, "%d", &lvl) == EOF)
+               ERROR("failed to read debug level\n");
+       fclose(fp);
+       unlink("/tmp/debug_level");
+
+       if (lvl > 0 && lvl < 5)
+               debug = lvl;
+}
+
+static void
 spawn_procd(struct uloop_process *proc, int ret)
 {
        char *wdt_fd = watchdog_fd();
-       char *argv[] = { "/sbin/procd", "-d", "0", NULL };
+       char *argv[] = { "/sbin/procd", NULL};
        struct stat s;
+       char dbg[2];
 
        if (plugd_proc.pid > 0)
                kill(plugd_proc.pid, SIGKILL);
@@ -46,13 +65,16 @@ spawn_procd(struct uloop_process *proc, int ret)
 
        unsetenv("INITRAMFS");
        unsetenv("PREINIT");
+       unlink("/tmp/.preinit");
        DEBUG(2, "Exec to real procd now\n");
        if (wdt_fd)
                setenv("WDTFD", wdt_fd, 1);
-       if (debug)
-               snprintf(argv[2], 2, "%d", debug & 0xf);
-       else
-               argv[1] = NULL;
+       check_dbglvl();
+       if (debug > 0) {
+               snprintf(dbg, 2, "%d", debug);
+               setenv("DBGLVL", dbg, 1);
+       }
+
        execvp(argv[0], argv);
 }
 
@@ -67,6 +89,7 @@ preinit(void)
 {
        char *init[] = { "/bin/sh", "/etc/preinit", NULL };
        char *plug[] = { "/sbin/procd", "-h", "/etc/hotplug-preinit.json", NULL };
+       int fd;
 
        LOG("- preinit -\n");
 
@@ -85,6 +108,13 @@ preinit(void)
 
        setenv("PREINIT", "1", 1);
 
+       fd = creat("/tmp/.preinit", 0600);
+
+       if (fd < 0)
+               ERROR("Failed to create sentinel file\n");
+       else
+               close(fd);
+
        preinit_proc.cb = spawn_procd;
        preinit_proc.pid = fork();
        if (!preinit_proc.pid) {