summaryrefslogtreecommitdiffstats
path: root/initd
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
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')
-rw-r--r--initd/early.c95
-rw-r--r--initd/init.c115
-rw-r--r--initd/init.h23
-rw-r--r--initd/mkdev.c129
-rw-r--r--initd/preinit.c98
5 files changed, 460 insertions, 0 deletions
diff --git a/initd/early.c b/initd/early.c
new file mode 100644
index 0000000..77ced77
--- /dev/null
+++ b/initd/early.c
@@ -0,0 +1,95 @@
+/*
+ * 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/mount.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+#include "../log.h"
+#include "init.h"
+
+static void
+early_mounts(void)
+{
+ mount("proc", "/proc", "proc", MS_NOATIME, 0);
+ mount("sysfs", "/sys", "sysfs", MS_NOATIME, 0);
+
+ mount("tmpfs", "/tmp", "tmpfs", MS_NOSUID | MS_NODEV | MS_NOATIME, NULL);
+ mkdir("/tmp/run", 0777);
+ mkdir("/tmp/lock", 0777);
+ mkdir("/tmp/state", 0777);
+ symlink("/tmp", "/var");
+
+ mount("tmpfs", "/dev", "tmpfs", MS_NOATIME, "mode=0755,size=512K");
+ mkdir("/dev/shm", 0755);
+ mkdir("/dev/pts", 0755);
+ mount("devpts", "/dev/pts", "devpts", MS_NOATIME, "mode=600");
+}
+
+static void
+early_dev(void)
+{
+ mkdev("*", 0600);
+ mknod("/dev/null", 0666, makedev(1, 3));
+}
+
+static void
+early_console(const char *dev)
+{
+ struct stat s;
+ int dd;
+
+ if (stat(dev, &s)) {
+ ERROR("Failed to stat %s\n", dev);
+ return;
+ }
+
+ dd = open(dev, O_RDWR);
+ if (dd < 0)
+ dd = open("/dev/null", O_RDWR);
+
+ dup2(dd, STDIN_FILENO);
+ dup2(dd, STDOUT_FILENO);
+ dup2(dd, STDERR_FILENO);
+
+ if (dd != STDIN_FILENO &&
+ dd != STDOUT_FILENO &&
+ dd != STDERR_FILENO)
+ close(dd);
+}
+
+static void
+early_env(void)
+{
+ setenv("PATH", "/bin:/sbin:/usr/bin:/usr/sbin", 1);
+}
+
+void
+early(void)
+{
+ if (getpid() != 1)
+ return;
+
+ early_mounts();
+ early_dev();
+ early_env();
+ early_console("/dev/console");
+
+ LOG("Console is alive\n");
+}
diff --git a/initd/init.c b/initd/init.c
new file mode 100644
index 0000000..d458f29
--- /dev/null
+++ b/initd/init.c
@@ -0,0 +1,115 @@
+/*
+ * 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/wait.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/reboot.h>
+
+#include <libubox/uloop.h>
+#include <libubus.h>
+
+#include <stdlib.h>
+#include <fcntl.h>
+#include <getopt.h>
+#include <libgen.h>
+#include <regex.h>
+#include <unistd.h>
+#include <stdio.h>
+
+#include "init.h"
+#include "../watchdog.h"
+
+unsigned int debug = 0;
+
+static void
+signal_shutdown(int signal, siginfo_t *siginfo, void *data)
+{
+ fprintf(stderr, "reboot\n");
+ fflush(stderr);
+ sync();
+ sleep(2);
+ reboot(RB_AUTOBOOT);
+ while (1)
+ ;
+}
+
+static struct sigaction sa_shutdown = {
+ .sa_sigaction = signal_shutdown,
+ .sa_flags = SA_SIGINFO
+};
+
+static void
+cmdline(void)
+{
+ char line[256];
+ int r, fd = open("/proc/cmdline", O_RDONLY);
+ regex_t pat_cmdline;
+ regmatch_t matches[2];
+
+ if (fd < 0)
+ return;
+
+ r = read(fd, line, sizeof(line) - 1);
+ line[r] = '\0';
+ close(fd);
+
+ regcomp(&pat_cmdline, "init_debug=([0-9]+)", REG_EXTENDED);
+ if (!regexec(&pat_cmdline, line, 2, matches, 0)) {
+ line[matches[1].rm_eo] = '\0';
+ debug = atoi(&line[matches[1].rm_so]);
+ }
+ regfree(&pat_cmdline);
+}
+
+int
+main(int argc, char **argv)
+{
+ pid_t pid;
+
+ sigaction(SIGTERM, &sa_shutdown, NULL);
+ sigaction(SIGUSR1, &sa_shutdown, NULL);
+ sigaction(SIGUSR2, &sa_shutdown, NULL);
+
+ early();
+ cmdline();
+ watchdog_init(1);
+
+ pid = fork();
+ if (!pid) {
+ char *kmod[] = { "/sbin/kmodloader", "/etc/modules-boot.d/", NULL };
+
+ if (debug < 3) {
+ int fd = open("/dev/null", O_RDWR);
+
+ if (fd > -1) {
+ dup2(fd, STDIN_FILENO);
+ dup2(fd, STDOUT_FILENO);
+ dup2(fd, STDERR_FILENO);
+ if (fd > STDERR_FILENO)
+ close(fd);
+ }
+ }
+ execvp(kmod[0], kmod);
+ ERROR("Failed to start kmodloader\n");
+ exit(-1);
+ }
+ if (pid <= 0)
+ ERROR("Failed to start kmodloader instance\n");
+ uloop_init();
+ preinit();
+ uloop_run();
+
+ return 0;
+}
diff --git a/initd/init.h b/initd/init.h
new file mode 100644
index 0000000..1321cf8
--- /dev/null
+++ b/initd/init.h
@@ -0,0 +1,23 @@
+/*
+ * 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.
+ */
+
+#ifndef _INIT_H__
+#define _INIT_H__
+
+#include "../log.h"
+
+void preinit(void);
+void early(void);
+int mkdev(const char *progname, int progmode);
+
+#endif
diff --git a/initd/mkdev.c b/initd/mkdev.c
new file mode 100644
index 0000000..3471461
--- /dev/null
+++ b/initd/mkdev.c
@@ -0,0 +1,129 @@
+/*
+ * 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.
+ */
+
+#define _BSD_SOURCE
+
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdbool.h>
+#include <dirent.h>
+#include <limits.h>
+#include <fnmatch.h>
+
+#include "init.h"
+
+#include "../log.h"
+
+static char **patterns;
+static int n_patterns;
+static char buf[PATH_MAX];
+static char buf2[PATH_MAX];
+static unsigned int mode = 0600;
+
+static bool find_pattern(const char *name)
+{
+ int i;
+
+ for (i = 0; i < n_patterns; i++)
+ if (!fnmatch(patterns[i], name, 0))
+ return true;
+
+ return false;
+}
+
+static void make_dev(const char *path, bool block, int major, int minor)
+{
+ unsigned int oldumask = umask(0);
+ unsigned int _mode = mode | (block ? S_IFBLK : S_IFCHR);
+
+ DEBUG(4, "Creating %s device %s(%d,%d)\n",
+ block ? "block" : "character",
+ path, major, minor);
+
+ mknod(path, _mode, makedev(major, minor));
+ umask(oldumask);
+}
+
+static void find_devs(bool block)
+{
+ char *path = block ? "/sys/dev/block" : "/sys/dev/char";
+ struct dirent *dp;
+ DIR *dir;
+
+ dir = opendir(path);
+ if (!dir)
+ return;
+
+ path = buf2 + sprintf(buf2, "%s/", path);
+ while ((dp = readdir(dir)) != NULL) {
+ char *c;
+ int major = 0, minor = 0;
+ int len;
+
+ if (dp->d_type != DT_LNK)
+ continue;
+
+ if (sscanf(dp->d_name, "%d:%d", &major, &minor) != 2)
+ continue;
+
+ strcpy(path, dp->d_name);
+ len = readlink(buf2, buf, sizeof(buf));
+ if (len <= 0)
+ continue;
+
+ buf[len] = 0;
+ if (!find_pattern(buf))
+ continue;
+
+ c = strrchr(buf, '/');
+ if (!c)
+ continue;
+
+ c++;
+ make_dev(c, block, major, minor);
+ }
+ closedir(dir);
+}
+
+static char *add_pattern(const char *name)
+{
+ char *str = malloc(strlen(name) + 2);
+
+ str[0] = '*';
+ strcpy(str + 1, name);
+ return str;
+}
+
+int mkdev(const char *name, int _mode)
+{
+ char *pattern;
+
+ if (chdir("/dev"))
+ return 1;
+
+ pattern = add_pattern(name);
+ patterns = &pattern;
+ mode = _mode;
+ n_patterns = 1;
+ find_devs(true);
+ find_devs(false);
+ chdir("/");
+
+ return 0;
+}
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);
+}