Bump procd-2016-07-29-2c9f5d4af1559b840c42f1443ede9f9fe809c58b

This commit is contained in:
jbnadal
2016-12-06 17:47:39 +01:00
parent 2cdc93987e
commit c52cb8abde
56 changed files with 8207 additions and 0 deletions

View File

@@ -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/mount.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include "../utils/utils.h"
#include "init.h"
#include "../libc-compat.h"
static void
early_dev(void)
{
mkdev("*", 0600);
mknod("/dev/null", 0666, makedev(1, 3));
}
static void
early_console(const char *dev)
{
struct stat s;
if (stat(dev, &s)) {
ERROR("Failed to stat %s\n", dev);
return;
}
if (patch_stdio(dev)) {
ERROR("Failed to setup i/o redirection\n");
return;
}
fcntl(STDERR_FILENO, F_SETFL, fcntl(STDERR_FILENO, F_GETFL) | O_NONBLOCK);
}
static void
early_mounts(void)
{
unsigned int oldumask = umask(0);
mount("proc", "/proc", "proc", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, 0);
mount("sysfs", "/sys", "sysfs", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, 0);
mount("cgroup", "/sys/fs/cgroup", "cgroup", MS_NODEV | MS_NOEXEC | MS_NOSUID, 0);
mount("tmpfs", "/dev", "tmpfs", MS_NOATIME | MS_NOSUID, "mode=0755,size=512K");
ignore(symlink("/tmp/shm", "/dev/shm"));
mkdir("/dev/pts", 0755);
mount("devpts", "/dev/pts", "devpts", MS_NOATIME | MS_NOEXEC | MS_NOSUID, "mode=600");
early_dev();
early_console("/dev/console");
if (mount_zram_on_tmp()) {
mount("tmpfs", "/tmp", "tmpfs", MS_NOSUID | MS_NODEV | MS_NOATIME, 0);
mkdir("/tmp/shm", 01777);
} else {
mkdir("/tmp/shm", 01777);
mount("tmpfs", "/tmp/shm", "tmpfs", MS_NOSUID | MS_NODEV | MS_NOATIME,
"mode=01777");
}
mkdir("/tmp/run", 0755);
mkdir("/tmp/lock", 0755);
mkdir("/tmp/state", 0755);
umask(oldumask);
}
static void
early_env(void)
{
setenv("PATH", EARLY_PATH, 1);
}
void
early(void)
{
if (getpid() != 1)
return;
early_mounts();
early_env();
LOG("Console is alive\n");
}

113
src/3P/procd/initd/init.c Normal file
View File

@@ -0,0 +1,113 @@
/*
* 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 <ubus/libubus.h>
#include <limits.h>
#include <stdlib.h>
#include <fcntl.h>
#include <getopt.h>
#include <libgen.h>
#include <regex.h>
#include <unistd.h>
#include <stdio.h>
#include "../utils/utils.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[20];
char* res;
long r;
res = get_cmdline_val("init_debug", line, sizeof(line));
if (res != NULL) {
r = strtol(line, NULL, 10);
if ((r != LONG_MIN) && (r != LONG_MAX))
debug = (int) r;
}
}
int
main(int argc, char **argv)
{
pid_t pid;
ulog_open(ULOG_KMSG, LOG_DAEMON, "init");
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)
patch_stdio("/dev/null");
execvp(kmod[0], kmod);
ERROR("Failed to start kmodloader\n");
exit(-1);
}
if (pid <= 0) {
ERROR("Failed to start kmodloader instance\n");
} else {
int i;
for (i = 0; i < 1200; i++) {
if (waitpid(pid, NULL, WNOHANG) > 0)
break;
usleep(10 * 1000);
watchdog_ping();
}
}
uloop_init();
preinit();
uloop_run();
return 0;
}

36
src/3P/procd/initd/init.h Normal file
View File

@@ -0,0 +1,36 @@
/*
* 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 <errno.h>
#include "../log.h"
#ifndef EARLY_PATH
#define EARLY_PATH "/usr/sbin:/sbin:/usr/bin:/bin"
#endif
void preinit(void);
void early(void);
int mkdev(const char *progname, int progmode);
#ifdef ZRAM_TMPFS
int mount_zram_on_tmp(void);
#else
static inline int mount_zram_on_tmp(void) {
return -ENOSYS;
}
#endif
#endif

125
src/3P/procd/initd/mkdev.c Normal file
View File

@@ -0,0 +1,125 @@
/*
* 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 _DEFAULT_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"
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);
return chdir("/");
}

View File

@@ -0,0 +1,132 @@
/*
* 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 <fcntl.h>
#include <libubox/uloop.h>
#include <libubox/utils.h>
#include <ubus/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
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", NULL};
struct stat s;
char dbg[2];
if (plugd_proc.pid > 0)
kill(plugd_proc.pid, SIGKILL);
if (!stat("/tmp/sysupgrade", &s))
while (true)
sleep(1);
unsetenv("INITRAMFS");
unsetenv("PREINIT");
unlink("/tmp/.preinit");
DEBUG(2, "Exec to real procd now\n");
if (wdt_fd)
setenv("WDTFD", wdt_fd, 1);
check_dbglvl();
if (debug > 0) {
snprintf(dbg, 2, "%d", debug);
setenv("DBGLVL", dbg, 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 };
int fd;
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);
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) {
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);
}

128
src/3P/procd/initd/zram.c Normal file
View File

@@ -0,0 +1,128 @@
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/utsname.h>
#include <sys/mount.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include "../log.h"
#include "init.h"
#define KB(x) (x * 1024)
#define ZRAM_MOD_PATH "/lib/modules/%s/zram.ko"
#define EXT4_MOD_PATH "/lib/modules/%s/ext4.ko"
static long
proc_meminfo(void)
{
FILE *fp;
char line[256];
char *key;
long val = KB(16);
fp = fopen("/proc/meminfo", "r");
if (fp == NULL) {
ERROR("Can't open /proc/meminfo: %s\n", strerror(errno));
return errno;
}
while (fgets(line, sizeof(line), fp)) {
key = strtok(line, ":");
if (strcasecmp(key, "MemTotal"))
continue;
val = atol(strtok(NULL, " kB\n"));
break;
}
fclose(fp);
if (val > KB(32))
val = KB(32);
return val;
}
static int
early_insmod(char *module)
{
pid_t pid = fork();
if (!pid) {
char *modprobe[] = { "/usr/sbin/modprobe", NULL, NULL };
char *path;
struct utsname ver;
uname(&ver);
path = alloca(sizeof(module) + strlen(ver.release) + 1);
sprintf(path, module, ver.release);
modprobe[1] = path;
execvp(modprobe[0], modprobe);
ERROR("Can't exec /usr/sbin/modprobe\n");
exit(-1);
}
if (pid <= 0) {
ERROR("Can't exec /usr/sbin/modprobe\n");
return -1;
} else {
waitpid(pid, NULL, 0);
}
return 0;
}
int
mount_zram_on_tmp(void)
{
char *mkfs[] = { "/usr/sbin/mkfs.ext4", "-b", "4096", "-F", "-L", "TEMP", "-m", "0", "/dev/zram0", NULL };
FILE *fp;
long zramsize;
pid_t pid;
int ret;
if (early_insmod(ZRAM_MOD_PATH) || early_insmod(EXT4_MOD_PATH)) {
ERROR("failed to insmod zram support\n");
return -1;
}
mkdev("*", 0600);
zramsize = proc_meminfo() / 2;
fp = fopen("/sys/block/zram0/disksize", "r+");
if (fp == NULL) {
ERROR("Can't open /sys/block/zram0/disksize: %s\n", strerror(errno));
return errno;
}
fprintf(fp, "%ld", KB(zramsize));
fclose(fp);
pid = fork();
if (!pid) {
execvp(mkfs[0], mkfs);
ERROR("Can't exec /sbin/mkfs.ext4\n");
exit(-1);
} else if (pid <= 0) {
ERROR("Can't exec /sbin/mkfs.ext4\n");
return -1;
} else {
waitpid(pid, NULL, 0);
}
ret = mount("/dev/zram0", "/tmp", "ext4", MS_NOSUID | MS_NODEV | MS_NOATIME, "errors=continue,noquota");
if (ret < 0) {
ERROR("Can't mount /dev/zram0 on /tmp: %s\n", strerror(errno));
return errno;
}
LOG("Using up to %ld kB of RAM as ZRAM storage on /mnt\n", zramsize);
return 0;
}