summaryrefslogtreecommitdiffstats
path: root/initd/preinit.c
diff options
context:
space:
mode:
authorJohn Crispin <blogic@openwrt.org>2013-11-14 13:41:13 +0100
committerJohn Crispin <blogic@openwrt.org>2013-11-15 17:05:20 +0100
commit916f95cb58604038695347ee41a430d8ca1f0556 (patch)
tree5dbb52a6adaf28c6c6989ea37e6975aa52075160 /initd/preinit.c
parentf9d31edb8938341b9217ee4c14eb58111414eb97 (diff)
downloadunitd-916f95cb58604038695347ee41a430d8ca1f0556.tar
unitd-916f95cb58604038695347ee41a430d8ca1f0556.zip
debloat and reorganize code
split app into procd and init binaries remove log support, this is an external service now Signed-off-by: John Crispin <blogic@openwrt.org>
Diffstat (limited to 'initd/preinit.c')
-rw-r--r--initd/preinit.c98
1 files changed, 98 insertions, 0 deletions
diff --git a/initd/preinit.c b/initd/preinit.c
new file mode 100644
index 0000000..eeadbeb
--- /dev/null
+++ b/initd/preinit.c
@@ -0,0 +1,98 @@
+/*
+ * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
+ * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1
+ * as published by the Free Software Foundation
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/mount.h>
+
+#include <libubox/uloop.h>
+#include <libubox/utils.h>
+#include <libubus.h>
+
+#include <stdio.h>
+
+#include <unistd.h>
+
+#include "init.h"
+#include "../watchdog.h"
+
+static struct uloop_process preinit_proc;
+static struct uloop_process plugd_proc;
+
+static void
+spawn_procd(struct uloop_process *proc, int ret)
+{
+ char *wdt_fd = watchdog_fd();
+ char *argv[] = { "/sbin/procd", NULL };
+ struct stat s;
+
+ if (plugd_proc.pid > 0)
+ kill(plugd_proc.pid, SIGKILL);
+
+ if (!stat("/tmp/sysupgrade", &s))
+ while (true)
+ sleep(1);
+
+ unsetenv("INITRAMFS");
+ unsetenv("PREINIT");
+ DEBUG(2, "Exec to real procd now\n");
+ if (wdt_fd)
+ setenv("WDTFD", wdt_fd, 1);
+ execvp(argv[0], argv);
+}
+
+static void
+plugd_proc_cb(struct uloop_process *proc, int ret)
+{
+ proc->pid = 0;
+}
+
+void
+preinit(void)
+{
+ char *init[] = { "/bin/sh", "/etc/preinit", NULL };
+ char *plug[] = { "/sbin/procd", "-h", "/etc/hotplug-preinit.json", NULL };
+
+ LOG("- preinit -\n");
+
+ plugd_proc.cb = plugd_proc_cb;
+ plugd_proc.pid = fork();
+ if (!plugd_proc.pid) {
+ execvp(plug[0], plug);
+ ERROR("Failed to start plugd\n");
+ exit(-1);
+ }
+ if (plugd_proc.pid <= 0) {
+ ERROR("Failed to start new plugd instance\n");
+ return;
+ }
+ uloop_process_add(&plugd_proc);
+
+ setenv("PREINIT", "1", 1);
+
+ preinit_proc.cb = spawn_procd;
+ preinit_proc.pid = fork();
+ if (!preinit_proc.pid) {
+ execvp(init[0], init);
+ ERROR("Failed to start preinit\n");
+ exit(-1);
+ }
+ if (preinit_proc.pid <= 0) {
+ ERROR("Failed to start new preinit instance\n");
+ return;
+ }
+ uloop_process_add(&preinit_proc);
+
+ DEBUG(4, "Launched preinit instance, pid=%d\n", (int) preinit_proc.pid);
+}