diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/askfirst/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/askfirst/askfirst.c | 46 | ||||
-rw-r--r-- | src/unitd/CMakeLists.txt | 8 | ||||
-rw-r--r-- | src/unitd/askconsole.c | 63 | ||||
-rw-r--r-- | src/unitd/early.c | 96 | ||||
-rw-r--r-- | src/unitd/log.h | 33 | ||||
-rw-r--r-- | src/unitd/service/instance.c | 750 | ||||
-rw-r--r-- | src/unitd/service/instance.h | 80 | ||||
-rw-r--r-- | src/unitd/service/service.c | 465 | ||||
-rw-r--r-- | src/unitd/service/service.h | 42 | ||||
-rw-r--r-- | src/unitd/signal.c | 101 | ||||
-rw-r--r-- | src/unitd/state.c | 182 | ||||
-rw-r--r-- | src/unitd/system.c | 291 | ||||
-rw-r--r-- | src/unitd/ubus.c | 71 | ||||
-rw-r--r-- | src/unitd/unitd.c | 73 | ||||
-rw-r--r-- | src/unitd/unitd.h | 48 | ||||
-rw-r--r-- | src/unitd/utils.c | 160 | ||||
-rw-r--r-- | src/unitd/utils.h | 56 | ||||
-rw-r--r-- | src/unitd/watchdog.c | 139 | ||||
-rw-r--r-- | src/unitd/watchdog.h | 27 |
21 files changed, 2736 insertions, 0 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..ea743d7 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,2 @@ +add_subdirectory(askfirst) +add_subdirectory(unitd) diff --git a/src/askfirst/CMakeLists.txt b/src/askfirst/CMakeLists.txt new file mode 100644 index 0000000..aa8c89a --- /dev/null +++ b/src/askfirst/CMakeLists.txt @@ -0,0 +1,3 @@ +add_executable(askfirst askfirst.c) + +install(TARGETS askfirst RUNTIME DESTINATION ${CMAKE_INSTALL_LIBDIR}/unitd) diff --git a/src/askfirst/askfirst.c b/src/askfirst/askfirst.c new file mode 100644 index 0000000..cdb5074 --- /dev/null +++ b/src/askfirst/askfirst.c @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2015 Matthias Schiffer <mschiffer@universe-factory.net> + * + * Based on "procd" by: + * 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/types.h> +#include <sys/stat.h> + +#include <stdio.h> +#include <unistd.h> +#include <fcntl.h> + +int main(int argc, char **argv) +{ + int c; + + printf("Please press Enter to activate this console.\n"); + do { + c = getchar(); + if (c == EOF) + return -1; + } + while (c != 0xA); + + if (argc < 2) { + printf("%s needs to be called with at least 1 parameter\n", argv[0]); + return -1; + } + + execvp(argv[1], &argv[1]); + printf("Failed to execute %s\n", argv[1]); + + return -1; +} diff --git a/src/unitd/CMakeLists.txt b/src/unitd/CMakeLists.txt new file mode 100644 index 0000000..6ef73f6 --- /dev/null +++ b/src/unitd/CMakeLists.txt @@ -0,0 +1,8 @@ +add_executable(unitd unitd.c signal.c watchdog.c state.c askconsole.c ubus.c system.c early.c + service/service.c service/instance.c utils.c) +set_property(TARGET unitd PROPERTY COMPILE_FLAGS "-std=gnu99 -Wall ${JSON_C_CFLAGS_OTHER}") +set_property(TARGET unitd PROPERTY LINK_FLAGS "${JSON_C_LDFLAGS_OTHER}") +set_property(TARGET unitd PROPERTY INCLUDE_DIRECTORIES ${JSON_C_INCLUDE_DIR}) +target_link_libraries(unitd ubox ubus blobmsg_json ${JSON_C_LIBRARIES}) + +install(TARGETS unitd RUNTIME DESTINATION ${CMAKE_INSTALL_LIBDIR}/unitd) diff --git a/src/unitd/askconsole.c b/src/unitd/askconsole.c new file mode 100644 index 0000000..d7d4cb5 --- /dev/null +++ b/src/unitd/askconsole.c @@ -0,0 +1,63 @@ +/* + * + * 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 "unitd.h" + +#include <sys/types.h> +#include <sys/ioctl.h> + +#include <fcntl.h> +#include <unistd.h> + + +static void askconsole(struct uloop_process *proc) { + char *const ask[] = { + "/lib/unitd/askfirst", + "/bin/ash", + "--login", + NULL, + }; + + pid_t p; + + proc->pid = fork(); + if (!proc->pid) { + p = setsid(); + + fcntl(STDERR_FILENO, F_SETFL, fcntl(STDERR_FILENO, F_GETFL) & ~O_NONBLOCK); + + ioctl(STDIN_FILENO, TIOCSCTTY, 1); + tcsetpgrp(STDIN_FILENO, p); + + execvp(ask[0], ask); + ERROR("Failed to execute %s\n", ask[0]); + exit(-1); + } + + if (proc->pid > 0) { + DEBUG(4, "Launched askconsole, pid=%d\n", + (int) proc->pid); + uloop_process_add(proc); + } +} + +static void child_exit(struct uloop_process *proc, int ret) +{ + DEBUG(4, "pid:%d\n", proc->pid); + askconsole(proc); +} + +void unitd_askconsole(void) { + struct uloop_process *proc = calloc(1, sizeof(*proc)); + proc->cb = child_exit; + askconsole(proc); +} diff --git a/src/unitd/early.c b/src/unitd/early.c new file mode 100644 index 0000000..644c15d --- /dev/null +++ b/src/unitd/early.c @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2015 Matthias Schiffer <mschiffer@universe-factory.net> + * + * Based on "procd" by: + * 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 "unitd.h" + +#include <sys/mount.h> +#include <sys/types.h> +#include <sys/stat.h> + +#include <stdio.h> +#include <fcntl.h> +#include <unistd.h> +#include <stdlib.h> + + +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); + + 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, NULL); + mount("sys", "/sys", "sysfs", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, NULL); + mount("cgroup", "/sys/fs/cgroup", "cgroup", MS_NODEV | MS_NOEXEC | MS_NOSUID, NULL); + mount("dev", "/dev", "devtmpfs", MS_NOATIME | MS_NOSUID, "mode=0755,size=512K"); + + mkdir("/dev/pts", 0755); + mount("devpts", "/dev/pts", "devpts", MS_NOATIME | MS_NOEXEC | MS_NOSUID, "mode=600"); + + mount("run", "/run", "tmpfs", MS_NOATIME, "mode=0755"); + + mkdir("/run/tmp", 01777); + mount("/run/tmp", "/tmp", NULL, MS_BIND, NULL); + + mkdir("/run/shm", 01777); + mkdir("/dev/shm", 0755); + mount("/run/shm", "/dev/shm", NULL, MS_BIND, NULL); + + umask(oldumask); +} + +static void +early_env(void) +{ + setenv("PATH", "/bin:/sbin:/usr/bin:/usr/sbin", 1); +} + +void +unitd_early(void) +{ + early_mounts(); + early_console("/dev/console"); + early_env(); + + LOG("Console is alive\n"); +} diff --git a/src/unitd/log.h b/src/unitd/log.h new file mode 100644 index 0000000..3ad6a8c --- /dev/null +++ b/src/unitd/log.h @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2015 Matthias Schiffer <mschiffer@universe-factory.net> + * + * Based on "procd" by: + * 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. + */ + +#ifndef __LOG_H +#define __LOG_H + +#include <libubox/ulog.h> + +#define DEBUG(level, fmt, ...) do { \ + if (debug >= level) { \ + ulog(LOG_DEBUG, fmt, ## __VA_ARGS__); \ + } } while (0) + +#define LOG ULOG_INFO +#define ERROR ULOG_ERR + +extern unsigned int debug; + +#endif diff --git a/src/unitd/service/instance.c b/src/unitd/service/instance.c new file mode 100644 index 0000000..0949c0f --- /dev/null +++ b/src/unitd/service/instance.c @@ -0,0 +1,750 @@ +/* + * Copyright (C) 2015 Matthias Schiffer <mschiffer@universe-factory.net> + * + * Based on "procd" by: + * 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 _GNU_SOURCE +#include <sys/resource.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <sys/stat.h> +#include <net/if.h> +#include <unistd.h> +#include <stdint.h> +#include <stdio.h> +#include <fcntl.h> +#include <pwd.h> +#include <libgen.h> +#include <unistd.h> + +#include <libubox/md5.h> + +#include "../unitd.h" + +#include "service.h" +#include "instance.h" + + +enum { + INSTANCE_ATTR_COMMAND, + INSTANCE_ATTR_ENV, + INSTANCE_ATTR_DATA, + INSTANCE_ATTR_NETDEV, + INSTANCE_ATTR_FILE, + INSTANCE_ATTR_RESPAWN, + INSTANCE_ATTR_NICE, + INSTANCE_ATTR_LIMITS, + INSTANCE_ATTR_ERROR, + INSTANCE_ATTR_USER, + INSTANCE_ATTR_STDOUT, + INSTANCE_ATTR_STDERR, + __INSTANCE_ATTR_MAX +}; + +static const struct blobmsg_policy instance_attr[__INSTANCE_ATTR_MAX] = { + [INSTANCE_ATTR_COMMAND] = { "command", BLOBMSG_TYPE_ARRAY }, + [INSTANCE_ATTR_ENV] = { "env", BLOBMSG_TYPE_TABLE }, + [INSTANCE_ATTR_DATA] = { "data", BLOBMSG_TYPE_TABLE }, + [INSTANCE_ATTR_NETDEV] = { "netdev", BLOBMSG_TYPE_ARRAY }, + [INSTANCE_ATTR_FILE] = { "file", BLOBMSG_TYPE_ARRAY }, + [INSTANCE_ATTR_RESPAWN] = { "respawn", BLOBMSG_TYPE_ARRAY }, + [INSTANCE_ATTR_NICE] = { "nice", BLOBMSG_TYPE_INT32 }, + [INSTANCE_ATTR_LIMITS] = { "limits", BLOBMSG_TYPE_TABLE }, + [INSTANCE_ATTR_ERROR] = { "error", BLOBMSG_TYPE_ARRAY }, + [INSTANCE_ATTR_USER] = { "user", BLOBMSG_TYPE_STRING }, + [INSTANCE_ATTR_STDOUT] = { "stdout", BLOBMSG_TYPE_BOOL }, + [INSTANCE_ATTR_STDERR] = { "stderr", BLOBMSG_TYPE_BOOL }, +}; + +struct instance_netdev { + struct blobmsg_list_node node; + int ifindex; +}; + +struct instance_file { + struct blobmsg_list_node node; + uint32_t md5[4]; +}; + +struct rlimit_name { + const char *name; + int resource; +}; + +static const struct rlimit_name rlimit_names[] = { + { "as", RLIMIT_AS }, + { "core", RLIMIT_CORE }, + { "cpu", RLIMIT_CPU }, + { "data", RLIMIT_DATA }, + { "fsize", RLIMIT_FSIZE }, + { "memlock", RLIMIT_MEMLOCK }, + { "msgqueue", RLIMIT_MSGQUEUE }, + { "nice", RLIMIT_NICE }, + { "nofile", RLIMIT_NOFILE }, + { "nproc", RLIMIT_NPROC }, + { "rss", RLIMIT_RSS }, + { "rtprio", RLIMIT_RTPRIO }, + { "sigpending", RLIMIT_SIGPENDING }, + { "stack", RLIMIT_STACK }, + { NULL, 0 } +}; + +static void closefd(int fd) +{ + if (fd > STDERR_FILENO) + close(fd); +} + +static void +instance_limits(const char *limit, const char *value) +{ + int i; + struct rlimit rlim; + unsigned long cur, max; + + for (i = 0; rlimit_names[i].name != NULL; i++) { + if (strcmp(rlimit_names[i].name, limit)) + continue; + if (!strcmp(value, "unlimited")) { + rlim.rlim_cur = RLIM_INFINITY; + rlim.rlim_max = RLIM_INFINITY; + } else { + if (getrlimit(rlimit_names[i].resource, &rlim)) + return; + + cur = rlim.rlim_cur; + max = rlim.rlim_max; + + if (sscanf(value, "%lu %lu", &cur, &max) < 1) + return; + + rlim.rlim_cur = cur; + rlim.rlim_max = max; + } + + setrlimit(rlimit_names[i].resource, &rlim); + return; + } +} + +static void +instance_run(struct service_instance *in, int _stdout, int _stderr) +{ + struct blobmsg_list_node *var; + struct blob_attr *cur; + char **argv; + int argc = 1; /* NULL terminated */ + int rem, _stdin; + + if (in->nice) + setpriority(PRIO_PROCESS, 0, in->nice); + + blobmsg_for_each_attr(cur, in->command, rem) + argc++; + + blobmsg_list_for_each(&in->env, var) + setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1); + + blobmsg_list_for_each(&in->limits, var) + instance_limits(blobmsg_name(var->data), blobmsg_data(var->data)); + + argv = alloca(sizeof(char *) * argc); + argc = 0; + + blobmsg_for_each_attr(cur, in->command, rem) + argv[argc++] = blobmsg_data(cur); + + argv[argc] = NULL; + + _stdin = open("/dev/null", O_RDONLY); + + if (_stdout == -1) + _stdout = open("/dev/null", O_WRONLY); + + if (_stderr == -1) + _stderr = open("/dev/null", O_WRONLY); + + if (_stdin > -1) { + dup2(_stdin, STDIN_FILENO); + closefd(_stdin); + } + if (_stdout > -1) { + dup2(_stdout, STDOUT_FILENO); + closefd(_stdout); + } + if (_stderr > -1) { + dup2(_stderr, STDERR_FILENO); + closefd(_stderr); + } + + if (in->gid && setgid(in->gid)) { + ERROR("failed to set group id %d: %d (%s)\n", in->gid, errno, strerror(errno)); + exit(127); + } + if (in->uid && setuid(in->uid)) { + ERROR("failed to set user id %d: %d (%s)\n", in->uid, errno, strerror(errno)); + exit(127); + } + + execvp(argv[0], argv); + exit(127); +} + +static void +instance_free_stdio(struct service_instance *in) +{ + if (in->_stdout.fd.fd > -1) { + ustream_free(&in->_stdout.stream); + close(in->_stdout.fd.fd); + in->_stdout.fd.fd = -1; + } + + if (in->_stderr.fd.fd > -1) { + ustream_free(&in->_stderr.stream); + close(in->_stderr.fd.fd); + in->_stderr.fd.fd = -1; + } +} + +void +instance_start(struct service_instance *in) +{ + int pid; + int opipe[2] = { -1, -1 }; + int epipe[2] = { -1, -1 }; + + if (!avl_is_empty(&in->errors.avl)) { + LOG("Not starting instance %s::%s, an error was indicated\n", in->srv->name, in->name); + return; + } + + if (in->proc.pending) + return; + + instance_free_stdio(in); + if (in->_stdout.fd.fd > -2) { + if (pipe(opipe)) { + ULOG_WARN("pipe() failed: %d (%s)\n", errno, strerror(errno)); + opipe[0] = opipe[1] = -1; + } + } + + if (in->_stderr.fd.fd > -2) { + if (pipe(epipe)) { + ULOG_WARN("pipe() failed: %d (%s)\n", errno, strerror(errno)); + epipe[0] = epipe[1] = -1; + } + } + + in->restart = false; + in->halt = !in->respawn; + + if (!in->valid) + return; + + pid = fork(); + if (pid < 0) + return; + + if (!pid) { + uloop_done(); + closefd(opipe[0]); + closefd(epipe[0]); + instance_run(in, opipe[1], epipe[1]); + return; + } + + DEBUG(2, "Started instance %s::%s\n", in->srv->name, in->name); + in->proc.pid = pid; + clock_gettime(CLOCK_MONOTONIC, &in->start); + uloop_process_add(&in->proc); + + if (opipe[0] > -1) { + ustream_fd_init(&in->_stdout, opipe[0]); + closefd(opipe[1]); + } + + if (epipe[0] > -1) { + ustream_fd_init(&in->_stderr, epipe[0]); + closefd(epipe[1]); + } + + service_event("instance.start", in->srv->name, in->name); +} + +static void +instance_stdio(struct ustream *s, int prio, struct service_instance *in) +{ + char *newline, *str, *arg0, ident[32]; + int len; + + arg0 = basename(blobmsg_data(blobmsg_data(in->command))); + snprintf(ident, sizeof(ident), "%s[%d]", arg0, in->proc.pid); + ulog_open(ULOG_SYSLOG, LOG_DAEMON, ident); + + do { + str = ustream_get_read_buf(s, NULL); + if (!str) + break; + + newline = strchr(str, '\n'); + if (!newline) + break; + + *newline = 0; + ulog(prio, "%s\n", str); + + len = newline + 1 - str; + ustream_consume(s, len); + } while (1); + + ulog_open(ULOG_SYSLOG, LOG_DAEMON, "unitd"); +} + +static void +instance_stdout(struct ustream *s, int bytes) +{ + instance_stdio(s, LOG_INFO, + container_of(s, struct service_instance, _stdout.stream)); +} + +static void +instance_stderr(struct ustream *s, int bytes) +{ + instance_stdio(s, LOG_ERR, + container_of(s, struct service_instance, _stderr.stream)); +} + +static void +instance_timeout(struct uloop_timeout *t) +{ + struct service_instance *in; + + in = container_of(t, struct service_instance, timeout); + + if (!in->halt && (in->restart || in->respawn)) + instance_start(in); +} + +static void +instance_exit(struct uloop_process *p, int ret) +{ + struct service_instance *in; + struct timespec tp; + long runtime; + + in = container_of(p, struct service_instance, proc); + + clock_gettime(CLOCK_MONOTONIC, &tp); + runtime = tp.tv_sec - in->start.tv_sec; + + DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in->srv->name, in->name, ret, runtime); + + uloop_timeout_cancel(&in->timeout); + if (in->halt) { + /* no action */ + } else if (in->restart) { + instance_start(in); + } else if (in->respawn) { + if (runtime < in->respawn_threshold) + in->respawn_count++; + else + in->respawn_count = 0; + if (in->respawn_count > in->respawn_retry && in->respawn_retry > 0 ) { + LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n", + in->srv->name, in->name, in->respawn_count, runtime); + in->restart = in->respawn = 0; + in->halt = 1; + } else { + uloop_timeout_set(&in->timeout, in->respawn_timeout * 1000); + } + } + service_event("instance.stop", in->srv->name, in->name); +} + +void +instance_stop(struct service_instance *in) +{ + if (!in->proc.pending) + return; + in->halt = true; + in->restart = in->respawn = false; + kill(in->proc.pid, SIGTERM); +} + +static void +instance_restart(struct service_instance *in) +{ + if (!in->proc.pending) + return; + in->halt = false; + in->restart = true; + kill(in->proc.pid, SIGTERM); +} + +static bool +instance_config_changed(struct service_instance *in, struct service_instance *in_new) +{ + if (!in->valid) + return true; + + if (!blob_attr_equal(in->command, in_new->command)) + return true; + + if (!blobmsg_list_equal(&in->env, &in_new->env)) + return true; + + if (!blobmsg_list_equal(&in->data, &in_new->data)) + return true; + + if (!blobmsg_list_equal(&in->netdev, &in_new->netdev)) + return true; + + if (!blobmsg_list_equal(&in->file, &in_new->file)) + return true; + + if (in->nice != in_new->nice) + return true; + + if (in->uid != in_new->uid) + return true; + + if (in->gid != in_new->gid) + return true; + + if (!blobmsg_list_equal(&in->limits, &in_new->limits)) + return true; + + if (!blobmsg_list_equal(&in->errors, &in_new->errors)) + return true; + + return false; +} + +static bool +instance_netdev_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2) +{ + struct instance_netdev *n1 = container_of(l1, struct instance_netdev, node); + struct instance_netdev *n2 = container_of(l2, struct instance_netdev, node); + + return n1->ifindex == n2->ifindex; +} + +static void +instance_netdev_update(struct blobmsg_list_node *l) +{ + struct instance_netdev *n = container_of(l, struct instance_netdev, node); + + n->ifindex = if_nametoindex(n->node.avl.key); +} + +static bool +instance_file_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2) +{ + struct instance_file *f1 = container_of(l1, struct instance_file, node); + struct instance_file *f2 = container_of(l2, struct instance_file, node); + + return !memcmp(f1->md5, f2->md5, sizeof(f1->md5)); +} + +static void +instance_file_update(struct blobmsg_list_node *l) +{ + struct instance_file *f = container_of(l, struct instance_file, node); + md5_ctx_t md5; + char buf[256]; + int len, fd; + + memset(f->md5, 0, sizeof(f->md5)); + + fd = open(l->avl.key, O_RDONLY); + if (fd < 0) + return; + + md5_begin(&md5); + do { + len = read(fd, buf, sizeof(buf)); + if (len < 0) { + if (errno == EINTR) + continue; + + break; + } + if (!len) + break; + + md5_hash(buf, len, &md5); + } while(1); + + md5_end(f->md5, &md5); + close(fd); +} + +static void +instance_fill_any(struct blobmsg_list *l, struct blob_attr *cur) +{ + if (!cur) + return; + + blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), false); +} + +static bool +instance_fill_array(struct blobmsg_list *l, struct blob_attr *cur, blobmsg_update_cb cb, bool array) +{ + struct blobmsg_list_node *node; + + if (!cur) + return true; + + if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING)) + return false; + + blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), array); + if (cb) { + blobmsg_list_for_each(l, node) + cb(node); + } + return true; +} + +static bool +instance_config_parse(struct service_instance *in) +{ + struct blob_attr *tb[__INSTANCE_ATTR_MAX]; + struct blob_attr *cur, *cur2; + int argc = 0; + int rem; + + blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb, + blobmsg_data(in->config), blobmsg_data_len(in->config)); + + cur = tb[INSTANCE_ATTR_COMMAND]; + if (!cur) + return false; + + if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING)) + return false; + + blobmsg_for_each_attr(cur2, cur, rem) { + argc++; + break; + } + if (!argc) + return false; + + in->command = cur; + + if (tb[INSTANCE_ATTR_RESPAWN]) { + int i = 0; + uint32_t vals[3] = { 3600, 5, 5}; + + blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_RESPAWN], rem) { + if ((i >= 3) && (blobmsg_type(cur2) == BLOBMSG_TYPE_STRING)) + continue; + vals[i] = atoi(blobmsg_get_string(cur2)); + i++; + } + in->respawn = true; + in->respawn_count = 0; + in->respawn_threshold = vals[0]; + in->respawn_timeout = vals[1]; + in->respawn_retry = vals[2]; + } + + if ((cur = tb[INSTANCE_ATTR_NICE])) { + in->nice = (int8_t) blobmsg_get_u32(cur); + if (in->nice < -20 || in->nice > 20) + return false; + } + + if (tb[INSTANCE_ATTR_USER]) { + struct passwd *p = getpwnam(blobmsg_get_string(tb[INSTANCE_ATTR_USER])); + if (p) { + in->uid = p->pw_uid; + in->gid = p->pw_gid; + } + } + + if (tb[INSTANCE_ATTR_STDOUT] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDOUT])) + in->_stdout.fd.fd = -1; + + if (tb[INSTANCE_ATTR_STDERR] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDERR])) + in->_stderr.fd.fd = -1; + + instance_fill_any(&in->data, tb[INSTANCE_ATTR_DATA]); + + if (!instance_fill_array(&in->env, tb[INSTANCE_ATTR_ENV], NULL, false)) + return false; + + if (!instance_fill_array(&in->netdev, tb[INSTANCE_ATTR_NETDEV], instance_netdev_update, true)) + return false; + + if (!instance_fill_array(&in->file, tb[INSTANCE_ATTR_FILE], instance_file_update, true)) + return false; + + if (!instance_fill_array(&in->limits, tb[INSTANCE_ATTR_LIMITS], NULL, false)) + return false; + + if (!instance_fill_array(&in->errors, tb[INSTANCE_ATTR_ERROR], NULL, true)) + return false; + + return true; +} + +static void +instance_config_cleanup(struct service_instance *in) +{ + blobmsg_list_free(&in->env); + blobmsg_list_free(&in->data); + blobmsg_list_free(&in->netdev); + blobmsg_list_free(&in->file); + blobmsg_list_free(&in->limits); + blobmsg_list_free(&in->errors); +} + +static void +instance_config_move(struct service_instance *in, struct service_instance *in_src) +{ + instance_config_cleanup(in); + blobmsg_list_move(&in->env, &in_src->env); + blobmsg_list_move(&in->data, &in_src->data); + blobmsg_list_move(&in->netdev, &in_src->netdev); + blobmsg_list_move(&in->file, &in_src->file); + blobmsg_list_move(&in->limits, &in_src->limits); + blobmsg_list_move(&in->errors, &in_src->errors); + in->command = in_src->command; + in->name = in_src->name; + in->node.avl.key = in_src->node.avl.key; + + free(in->config); + in->config = in_src->config; + in_src->config = NULL; +} + +bool +instance_update(struct service_instance *in, struct service_instance *in_new) +{ + bool changed = instance_config_changed(in, in_new); + bool running = in->proc.pending; + + if (!changed && running) + return false; + + if (!running) { + if (changed) + instance_config_move(in, in_new); + instance_start(in); + } else { + instance_restart(in); + instance_config_move(in, in_new); + /* restart happens in the child callback handler */ + } + return true; +} + +void +instance_free(struct service_instance *in) +{ + instance_free_stdio(in); + uloop_process_delete(&in->proc); + uloop_timeout_cancel(&in->timeout); + instance_config_cleanup(in); + free(in->config); + free(in); +} + +void +instance_init(struct service_instance *in, struct service *s, struct blob_attr *config) +{ + config = blob_memdup(config); + in->srv = s; + in->name = blobmsg_name(config); + in->config = config; + in->timeout.cb = instance_timeout; + in->proc.cb = instance_exit; + + in->_stdout.fd.fd = -2; + in->_stdout.stream.string_data = true; + in->_stdout.stream.notify_read = instance_stdout; + + in->_stderr.fd.fd = -2; + in->_stderr.stream.string_data = true; + in->_stderr.stream.notify_read = instance_stderr; + + blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp); + blobmsg_list_init(&in->file, struct instance_file, node, instance_file_cmp); + blobmsg_list_simple_init(&in->env); + blobmsg_list_simple_init(&in->data); + blobmsg_list_simple_init(&in->limits); + blobmsg_list_simple_init(&in->errors); + in->valid = instance_config_parse(in); +} + +void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose) +{ + void *i; + + if (!in->valid) + return; + + i = blobmsg_open_table(b, in->name); + blobmsg_add_u8(b, "running", in->proc.pending); + if (in->proc.pending) + blobmsg_add_u32(b, "pid", in->proc.pid); + blobmsg_add_blob(b, in->command); + + if (!avl_is_empty(&in->errors.avl)) { + struct blobmsg_list_node *var; + void *e = blobmsg_open_array(b, "errors"); + blobmsg_list_for_each(&in->errors, var) + blobmsg_add_string(b, NULL, blobmsg_data(var->data)); + blobmsg_close_table(b, e); + } + + if (!avl_is_empty(&in->env.avl)) { + struct blobmsg_list_node *var; + void *e = blobmsg_open_table(b, "env"); + blobmsg_list_for_each(&in->env, var) + blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data)); + blobmsg_close_table(b, e); + } + + if (!avl_is_empty(&in->data.avl)) { + struct blobmsg_list_node *var; + void *e = blobmsg_open_table(b, "data"); + blobmsg_list_for_each(&in->data, var) + blobmsg_add_blob(b, var->data); + blobmsg_close_table(b, e); + } + + if (!avl_is_empty(&in->limits.avl)) { + struct blobmsg_list_node *var; + void *e = blobmsg_open_table(b, "limits"); + blobmsg_list_for_each(&in->limits, var) + blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data)); + blobmsg_close_table(b, e); + } + + if (in->respawn) { + void *r = blobmsg_open_table(b, "respawn"); + blobmsg_add_u32(b, "threshold", in->respawn_threshold); + blobmsg_add_u32(b, "timeout", in->respawn_timeout); + blobmsg_add_u32(b, "retry", in->respawn_retry); + blobmsg_close_table(b, r); + } + + blobmsg_close_table(b, i); +} diff --git a/src/unitd/service/instance.h b/src/unitd/service/instance.h new file mode 100644 index 0000000..8f5e61b --- /dev/null +++ b/src/unitd/service/instance.h @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2015 Matthias Schiffer <mschiffer@universe-factory.net> + * + * Based on "procd" by: + * 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. + */ + +#pragma once + +#include "../utils.h" + +#include <libubox/vlist.h> +#include <libubox/uloop.h> +#include <libubox/ustream.h> + +#define RESPAWN_ERROR (5 * 60) + +struct jail { + bool procfs; + bool sysfs; + bool ubus; + bool log; + char *name; + char *root; + struct blobmsg_list mount; + int argc; +}; + +struct service_instance { + struct vlist_node node; + struct service *srv; + const char *name; + + int8_t nice; + bool valid; + + uid_t uid; + gid_t gid; + + bool halt; + bool restart; + bool respawn; + int respawn_count; + struct timespec start; + + uint32_t respawn_timeout; + uint32_t respawn_threshold; + uint32_t respawn_retry; + + struct blob_attr *config; + struct uloop_process proc; + struct uloop_timeout timeout; + struct ustream_fd _stdout; + struct ustream_fd _stderr; + + struct blob_attr *command; + struct blobmsg_list env; + struct blobmsg_list data; + struct blobmsg_list netdev; + struct blobmsg_list file; + struct blobmsg_list limits; + struct blobmsg_list errors; +}; + +void instance_start(struct service_instance *in); +void instance_stop(struct service_instance *in); +bool instance_update(struct service_instance *in, struct service_instance *in_new); +void instance_init(struct service_instance *in, struct service *s, struct blob_attr *config); +void instance_free(struct service_instance *in); +void instance_dump(struct blob_buf *b, struct service_instance *in, int debug); diff --git a/src/unitd/service/service.c b/src/unitd/service/service.c new file mode 100644 index 0000000..a57d470 --- /dev/null +++ b/src/unitd/service/service.c @@ -0,0 +1,465 @@ +/* + * Copyright (C) 2015 Matthias Schiffer <mschiffer@universe-factory.net> + * + * Based on "procd" by: + * 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 <libubox/blobmsg_json.h> +#include <libubox/avl-cmp.h> + +#include "../unitd.h" + +#include "service.h" +#include "instance.h" + +struct avl_tree services; +static struct blob_buf b; +static struct ubus_context *ctx; + +static void +service_instance_add(struct service *s, struct blob_attr *attr) +{ + struct service_instance *in; + + if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE) + return; + + in = calloc(1, sizeof(*in)); + if (!in) + return; + + instance_init(in, s, attr); + vlist_add(&s->instances, &in->node, (void *) in->name); +} + +static void +service_instance_update(struct vlist_tree *tree, struct vlist_node *node_new, + struct vlist_node *node_old) +{ + struct service_instance *in_o = NULL, *in_n = NULL; + + if (node_old) + in_o = container_of(node_old, struct service_instance, node); + + if (node_new) + in_n = container_of(node_new, struct service_instance, node); + + if (in_o && in_n) { + DEBUG(2, "Update instance %s::%s\n", in_o->srv->name, in_o->name); + instance_update(in_o, in_n); + instance_free(in_n); + } else if (in_o) { + DEBUG(2, "Free instance %s::%s\n", in_o->srv->name, in_o->name); + instance_stop(in_o); + instance_free(in_o); + } else if (in_n) { + DEBUG(2, "Create instance %s::%s\n", in_n->srv->name, in_n->name); + instance_start(in_n); + } + blob_buf_init(&b, 0); +} + +static struct service * +service_alloc(const char *name) +{ + struct service *s; + char *new_name; + + s = calloc_a(sizeof(*s), &new_name, strlen(name) + 1); + strcpy(new_name, name); + + vlist_init(&s->instances, avl_strcmp, service_instance_update); + s->instances.keep_old = true; + s->name = new_name; + s->avl.key = s->name; + + return s; +} + +enum { + SERVICE_SET_NAME, + SERVICE_SET_SCRIPT, + SERVICE_SET_INSTANCES, + __SERVICE_SET_MAX +}; + +static const struct blobmsg_policy service_set_attrs[__SERVICE_SET_MAX] = { + [SERVICE_SET_NAME] = { "name", BLOBMSG_TYPE_STRING }, + [SERVICE_SET_SCRIPT] = { "script", BLOBMSG_TYPE_STRING }, + [SERVICE_SET_INSTANCES] = { "instances", BLOBMSG_TYPE_TABLE }, +}; + +static int +service_update(struct service *s, struct blob_attr **tb, bool add) +{ + struct blob_attr *cur; + int rem; + + if (tb[SERVICE_SET_INSTANCES]) { + if (!add) + vlist_update(&s->instances); + blobmsg_for_each_attr(cur, tb[SERVICE_SET_INSTANCES], rem) { + service_instance_add(s, cur); + } + if (!add) + vlist_flush(&s->instances); + } + + return 0; +} + +static void +service_delete(struct service *s) +{ + service_event("service.stop", s->name, NULL); + vlist_flush_all(&s->instances); + avl_delete(&services, &s->avl); + free(s); +} + +enum { + SERVICE_ATTR_NAME, + __SERVICE_ATTR_MAX, +}; + +static const struct blobmsg_policy service_attrs[__SERVICE_ATTR_MAX] = { + [SERVICE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING }, +}; + +enum { + SERVICE_DEL_ATTR_NAME, + SERVICE_DEL_ATTR_INSTANCE, + __SERVICE_DEL_ATTR_MAX, +}; + +static const struct blobmsg_policy service_del_attrs[__SERVICE_DEL_ATTR_MAX] = { + [SERVICE_DEL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING }, + [SERVICE_DEL_ATTR_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING }, +}; + +enum { + SERVICE_LIST_ATTR_NAME, + SERVICE_LIST_ATTR_VERBOSE, + __SERVICE_LIST_ATTR_MAX, +}; + +static const struct blobmsg_policy service_list_attrs[__SERVICE_LIST_ATTR_MAX] = { + [SERVICE_LIST_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING }, + [SERVICE_LIST_ATTR_VERBOSE] = { "verbose", BLOBMSG_TYPE_BOOL }, +}; + +enum { + EVENT_TYPE, + EVENT_DATA, + __EVENT_MAX +}; + +static const struct blobmsg_policy event_policy[__EVENT_MAX] = { + [EVENT_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING }, + [EVENT_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE }, +}; + +enum { + DATA_NAME, + DATA_INSTANCE, + DATA_TYPE, + __DATA_MAX +}; + +static const struct blobmsg_policy get_data_policy[] = { + [DATA_NAME] = { "name", BLOBMSG_TYPE_STRING }, + [DATA_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING }, + [DATA_TYPE] = { "type", BLOBMSG_TYPE_STRING }, +}; + +static int +service_handle_set(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + struct blob_attr *tb[__SERVICE_SET_MAX], *cur; + struct service *s = NULL; + const char *name; + bool add = !strcmp(method, "add"); + int ret; + + blobmsg_parse(service_set_attrs, __SERVICE_SET_MAX, tb, blob_data(msg), blob_len(msg)); + cur = tb[SERVICE_ATTR_NAME]; + if (!cur) + return UBUS_STATUS_INVALID_ARGUMENT; + + name = blobmsg_data(cur); + + s = avl_find_element(&services, name, s, avl); + if (s) { + DEBUG(2, "Update service %s\n", name); + return service_update(s, tb, add); + } + + DEBUG(2, "Create service %s\n", name); + s = service_alloc(name); + if (!s) + return UBUS_STATUS_UNKNOWN_ERROR; + + ret = service_update(s, tb, add); + if (ret) + return ret; + + avl_insert(&services, &s->avl); + + service_event("service.start", s->name, NULL); + + return 0; +} + +static void +service_dump(struct service *s, bool verbose) +{ + struct service_instance *in; + void *c, *i; + + c = blobmsg_open_table(&b, s->name); + + if (!avl_is_empty(&s->instances.avl)) { + i = blobmsg_open_table(&b, "instances"); + vlist_for_each_element(&s->instances, in, node) + instance_dump(&b, in, verbose); + blobmsg_close_table(&b, i); + } + blobmsg_close_table(&b, c); +} + +static int +service_handle_list(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + struct blob_attr *tb[__SERVICE_LIST_ATTR_MAX]; + struct service *s; + const char *name = NULL; + bool verbose = false; + + blobmsg_parse(service_list_attrs, __SERVICE_LIST_ATTR_MAX, tb, blob_data(msg), blob_len(msg)); + + if (tb[SERVICE_LIST_ATTR_VERBOSE]) + verbose = blobmsg_get_bool(tb[SERVICE_LIST_ATTR_VERBOSE]); + if (tb[SERVICE_LIST_ATTR_NAME]) + name = blobmsg_get_string(tb[SERVICE_LIST_ATTR_NAME]); + + blob_buf_init(&b, 0); + avl_for_each_element(&services, s, avl) { + if (name && strcmp(s->name, name) != 0) + continue; + + service_dump(s, verbose); + } + + ubus_send_reply(ctx, req, b.head); + + return 0; +} + +static int +service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + struct blob_attr *tb[__SERVICE_DEL_ATTR_MAX], *cur; + struct service *s; + struct service_instance *in; + + blobmsg_parse(service_del_attrs, __SERVICE_DEL_ATTR_MAX, tb, blob_data(msg), blob_len(msg)); + + cur = tb[SERVICE_DEL_ATTR_NAME]; + if (!cur) + return UBUS_STATUS_NOT_FOUND; + + s = avl_find_element(&services, blobmsg_data(cur), s, avl); + if (!s) + return UBUS_STATUS_NOT_FOUND; + + cur = tb[SERVICE_DEL_ATTR_INSTANCE]; + if (!cur) { + service_delete(s); + return 0; + } + + in = vlist_find(&s->instances, blobmsg_data(cur), in, node); + if (!in) { + ERROR("instance %s not found\n", (char *) blobmsg_data(cur)); + return UBUS_STATUS_NOT_FOUND; + } + + vlist_delete(&s->instances, &in->node); + + return 0; +} + +static int +service_handle_update(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur; + struct service *s; + + blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blob_data(msg), blob_len(msg)); + + cur = tb[SERVICE_ATTR_NAME]; + if (!cur) + return UBUS_STATUS_INVALID_ARGUMENT; + + s = avl_find_element(&services, blobmsg_data(cur), s, avl); + if (!s) + return UBUS_STATUS_NOT_FOUND; + + if (!strcmp(method, "update_start")) + vlist_update(&s->instances); + else + vlist_flush(&s->instances); + + return 0; +} + +static int +service_get_data(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + struct service_instance *in; + struct service *s; + struct blob_attr *tb[__DATA_MAX]; + const char *name = NULL; + const char *instance = NULL; + const char *type = NULL; + + blobmsg_parse(get_data_policy, __DATA_MAX, tb, blob_data(msg), blob_len(msg)); + if (tb[DATA_NAME]) + name = blobmsg_data(tb[DATA_NAME]); + if (tb[DATA_INSTANCE]) + instance = blobmsg_data(tb[DATA_INSTANCE]); + if (tb[DATA_TYPE]) + type = blobmsg_data(tb[DATA_TYPE]); + + blob_buf_init(&b, 0); + avl_for_each_element(&services, s, avl) { + void *cs = NULL; + + if (name && strcmp(name, s->name)) + continue; + + vlist_for_each_element(&s->instances, in, node) { + struct blobmsg_list_node *var; + void *ci = NULL; + + if (instance && strcmp(instance, in->name)) + continue; + + blobmsg_list_for_each(&in->data, var) { + if (type && + strcmp(blobmsg_name(var->data), type)) + continue; + + if (!cs) + cs = blobmsg_open_table(&b, s->name); + if (!ci) + ci = blobmsg_open_table(&b, in->name); + + blobmsg_add_blob(&b, var->data); + } + + if (ci) + blobmsg_close_table(&b, ci); + } + + if (cs) + blobmsg_close_table(&b, cs); + } + + ubus_send_reply(ctx, req, b.head); + return 0; +} + +static struct ubus_method main_object_methods[] = { + UBUS_METHOD("set", service_handle_set, service_set_attrs), + UBUS_METHOD("add", service_handle_set, service_set_attrs), + UBUS_METHOD("list", service_handle_list, service_list_attrs), + UBUS_METHOD("delete", service_handle_delete, service_del_attrs), + UBUS_METHOD("update_start", service_handle_update, service_attrs), + UBUS_METHOD("update_complete", service_handle_update, service_attrs), + UBUS_METHOD("get_data", service_get_data, get_data_policy), +}; + +static struct ubus_object_type main_object_type = + UBUS_OBJECT_TYPE("service", main_object_methods); + +static struct ubus_object main_object = { + .name = "service", + .type = &main_object_type, + .methods = main_object_methods, + .n_methods = ARRAY_SIZE(main_object_methods), +}; + +int +service_start_early(char *name, char *cmdline) +{ + void *instances, *instance, *command, *respawn; + char *t; + + blob_buf_init(&b, 0); + blobmsg_add_string(&b, "name", name); + instances = blobmsg_open_table(&b, "instances"); + instance = blobmsg_open_table(&b, "instance1"); + command = blobmsg_open_array(&b, "command"); + t = strtok(cmdline, " "); + while (t) { + blobmsg_add_string(&b, NULL, t); + t = strtok(NULL, " "); + } + blobmsg_close_array(&b, command); + respawn = blobmsg_open_array(&b, "respawn"); + blobmsg_add_string(&b, NULL, "3600"); + blobmsg_add_string(&b, NULL, "1"); + blobmsg_add_string(&b, NULL, "0"); + blobmsg_close_array(&b, respawn); + blobmsg_close_table(&b, instance); + blobmsg_close_table(&b, instances); + + return service_handle_set(NULL, NULL, NULL, "add", b.head); +} + +void service_event(const char *type, const char *service, const char *instance) +{ + if (!ctx) + return; + + blob_buf_init(&b, 0); + blobmsg_add_string(&b, "service", service); + if (instance) + blobmsg_add_string(&b, "instance", instance); + ubus_notify(ctx, &main_object, type, b.head, -1); +} + +void ubus_init_service(struct ubus_context *_ctx) +{ + ctx = _ctx; + ubus_add_object(ctx, &main_object); +} + +void +service_init(void) +{ + avl_init(&services, avl_strcmp, false, NULL); +} + diff --git a/src/unitd/service/service.h b/src/unitd/service/service.h new file mode 100644 index 0000000..2b6071c --- /dev/null +++ b/src/unitd/service/service.h @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2015 Matthias Schiffer <mschiffer@universe-factory.net> + * + * Based on "procd" by: + * 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. + */ + +#pragma once + +#include <libubox/avl.h> +#include <libubox/vlist.h> +#include <libubox/list.h> + +extern struct avl_tree services; + +struct vrule { + struct avl_node avl; + char *option; + char *rule; +}; + +struct service { + struct avl_node avl; + const char *name; + + struct blob_attr *trigger; + struct vlist_tree instances; +}; + +int service_start_early(char *name, char *cmdline); +void service_init(void); +void service_event(const char *type, const char *service, const char *instance); diff --git a/src/unitd/signal.c b/src/unitd/signal.c new file mode 100644 index 0000000..b888760 --- /dev/null +++ b/src/unitd/signal.c @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2015 Matthias Schiffer <mschiffer@universe-factory.net> + * + * Based on "procd" by: + * 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 "unitd.h" + +#include <sys/reboot.h> +#include <sys/types.h> + +#include <unistd.h> + + +static void do_reboot(void) +{ + LOG("reboot\n"); + fflush(stderr); + sync(); + sleep(2); + reboot(RB_AUTOBOOT); + while (1) + ; +} + +static void signal_shutdown(int signal, siginfo_t *siginfo, void *data) +{ + int event = 0; + char *msg = NULL; + + switch(signal) { + case SIGINT: + case SIGTERM: + event = RB_AUTOBOOT; + msg = "reboot"; + break; + case SIGUSR1: + case SIGUSR2: + event = RB_POWER_OFF; + msg = "poweroff"; + break; + } + + DEBUG(1, "Triggering %s\n", msg); + if (event) + unitd_shutdown(event); +} + +struct sigaction sa_shutdown = { + .sa_sigaction = signal_shutdown, + .sa_flags = SA_SIGINFO +}; + +static void signal_crash(int signal, siginfo_t *siginfo, void *data) +{ + ERROR("Rebooting as unitd has crashed\n"); + do_reboot(); +} + +struct sigaction sa_crash = { + .sa_sigaction = signal_crash, + .sa_flags = SA_SIGINFO +}; + +static void signal_dummy(int signal, siginfo_t *siginfo, void *data) +{ + ERROR("Got unexpected signal %d\n", signal); +} + +struct sigaction sa_dummy = { + .sa_sigaction = signal_dummy, + .sa_flags = SA_SIGINFO +}; + +void unitd_signal(void) +{ + signal(SIGPIPE, SIG_IGN); + if (getpid() != 1) + return; + sigaction(SIGTERM, &sa_shutdown, NULL); + sigaction(SIGINT, &sa_shutdown, NULL); + sigaction(SIGUSR1, &sa_shutdown, NULL); + sigaction(SIGUSR2, &sa_shutdown, NULL); + sigaction(SIGSEGV, &sa_crash, NULL); + sigaction(SIGBUS, &sa_crash, NULL); + sigaction(SIGHUP, &sa_dummy, NULL); + sigaction(SIGKILL, &sa_dummy, NULL); + sigaction(SIGSTOP, &sa_dummy, NULL); + reboot(RB_DISABLE_CAD); +} diff --git a/src/unitd/state.c b/src/unitd/state.c new file mode 100644 index 0000000..c22d225 --- /dev/null +++ b/src/unitd/state.c @@ -0,0 +1,182 @@ +/* + * Copyright (C) 2015 Matthias Schiffer <mschiffer@universe-factory.net> + * + * Based on "procd" by: + * 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 "unitd.h" +#include "syslog.h" +#include "utils.h" +#include "watchdog.h" +#include "service/service.h" + +#include <fcntl.h> +#include <sys/reboot.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <sys/types.h> +#include <signal.h> + + +enum { + STATE_NONE = 0, + STATE_EARLY, + STATE_RUNNING, + STATE_SHUTDOWN, + __STATE_MAX, +}; + +static int state = STATE_NONE; +static int reboot_event; + +static void set_stdio(const char* tty) +{ + if (chdir("/dev") || + !freopen(tty, "r", stdin) || + !freopen(tty, "w", stdout) || + !freopen(tty, "w", stderr) || + chdir("/")) + ERROR("failed to set stdio\n"); + else + fcntl(STDERR_FILENO, F_SETFL, fcntl(STDERR_FILENO, F_GETFL) | O_NONBLOCK); +} + +static void set_console(void) +{ + const char* tty; + char* split; + char line[ 20 ]; + const char* try[] = { "tty0", "console", NULL }; /* Try the most common outputs */ + int f, i = 0; + + tty = get_cmdline_val("console",line,sizeof(line)); + if (tty != NULL) { + split = strchr(tty, ','); + if ( split != NULL ) + *split = '\0'; + } else { + // Try a default + tty=try[i]; + i++; + } + + if (chdir("/dev")) { + ERROR("failed to change dir to /dev\n"); + return; + } + while (tty!=NULL) { + f = open(tty, O_RDONLY); + if (f >= 0) { + close(f); + break; + } + + tty=try[i]; + i++; + } + if (chdir("/")) + ERROR("failed to change dir to /\n"); + + if (tty != NULL) + set_stdio(tty); +} + +static void state_enter(void) +{ + char ubus_cmd[] = "/sbin/ubusd"; + + switch (state) { + case STATE_EARLY: + watchdog_init(0); + LOG("- early -\n"); + unitd_early(); + unitd_connect_ubus(); + service_init(); + service_start_early("ubus", ubus_cmd); + break; + + case STATE_RUNNING: + LOG("- init -\n"); + unitd_askconsole(); + + // switch to syslog log channel + ulog_open(ULOG_SYSLOG, LOG_DAEMON, "unitd"); + + LOG("- init complete -\n"); + break; + + case STATE_SHUTDOWN: + /* Redirect output to the console for the users' benefit */ + set_console(); + LOG("- shutdown -\n"); + sync(); + // To prevent killed processes from interrupting the sleep + signal(SIGCHLD, SIG_IGN); + LOG("- SIGTERM processes -\n"); + kill(-1, SIGTERM); + sync(); + sleep(1); + LOG("- SIGKILL processes -\n"); + kill(-1, SIGKILL); + sync(); + sleep(1); + if (reboot_event == RB_POWER_OFF) + LOG("- power down -\n"); + else + LOG("- reboot -\n"); + + /* Allow time for last message to reach serial console, etc */ + sleep(1); + + /* We have to fork here, since the kernel calls do_exit(EXIT_SUCCESS) + * in linux/kernel/sys.c, which can cause the machine to panic when + * the init process exits... */ + if (!vfork( )) { /* child */ + reboot(reboot_event); + _exit(EXIT_SUCCESS); + } + + while (1) + sleep(1); + break; + + default: + ERROR("Unhandled state %d\n", state); + return; + }; +} + +void unitd_state_next(void) +{ + DEBUG(4, "Change state %d -> %d\n", state, state + 1); + state++; + state_enter(); +} + +void unitd_state_ubus_connect(void) +{ + if (state == STATE_EARLY) + unitd_state_next(); +} + +void unitd_shutdown(int event) +{ + if (state >= STATE_SHUTDOWN) + return; + DEBUG(2, "Shutting down system with event %x\n", event); + reboot_event = event; + state = STATE_SHUTDOWN; + state_enter(); +} diff --git a/src/unitd/system.c b/src/unitd/system.c new file mode 100644 index 0000000..2316f96 --- /dev/null +++ b/src/unitd/system.c @@ -0,0 +1,291 @@ +/* + * Copyright (C) 2015 Matthias Schiffer <mschiffer@universe-factory.net> + * + * Based on "procd" by: + * 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 "unitd.h" +#include "watchdog.h" + +#include <sys/utsname.h> +#include <sys/sysinfo.h> +#include <sys/ioctl.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <signal.h> +#include <unistd.h> +#include <stdlib.h> + +#include <libubox/uloop.h> + + +static struct blob_buf b; +static int notify; +static struct ubus_context *_ctx; + +static int system_board(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + char line[256]; + char *key, *val; + struct utsname utsname; + FILE *f; + + blob_buf_init(&b, 0); + + if (uname(&utsname) >= 0) + { + blobmsg_add_string(&b, "kernel", utsname.release); + blobmsg_add_string(&b, "hostname", utsname.nodename); + } + + if ((f = fopen("/proc/cpuinfo", "r")) != NULL) + { + while(fgets(line, sizeof(line), f)) + { + key = strtok(line, "\t:"); + val = strtok(NULL, "\t\n"); + + if (!key || !val) + continue; + + if (!strcasecmp(key, "system type") || + !strcasecmp(key, "processor") || + !strcasecmp(key, "model name")) + { + strtoul(val + 2, &key, 0); + + if (key == (val + 2) || *key != 0) + { + blobmsg_add_string(&b, "system", val + 2); + break; + } + } + } + + fclose(f); + } + + else if ((f = fopen("/proc/cpuinfo", "r")) != NULL) + { + while(fgets(line, sizeof(line), f)) + { + key = strtok(line, "\t:"); + val = strtok(NULL, "\t\n"); + + if (!key || !val) + continue; + + if (!strcasecmp(key, "machine") || + !strcasecmp(key, "hardware")) + { + blobmsg_add_string(&b, "model", val + 2); + break; + } + } + + fclose(f); + } + + ubus_send_reply(ctx, req, b.head); + + return UBUS_STATUS_OK; +} + +static int system_info(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + void *c; + time_t now; + struct tm *tm; + struct sysinfo info; + + now = time(NULL); + + if (!(tm = localtime(&now))) + return UBUS_STATUS_UNKNOWN_ERROR; + + if (sysinfo(&info)) + return UBUS_STATUS_UNKNOWN_ERROR; + + blob_buf_init(&b, 0); + + blobmsg_add_u32(&b, "uptime", info.uptime); + blobmsg_add_u32(&b, "localtime", mktime(tm)); + + c = blobmsg_open_array(&b, "load"); + blobmsg_add_u32(&b, NULL, info.loads[0]); + blobmsg_add_u32(&b, NULL, info.loads[1]); + blobmsg_add_u32(&b, NULL, info.loads[2]); + blobmsg_close_array(&b, c); + + c = blobmsg_open_table(&b, "memory"); + blobmsg_add_u64(&b, "total", info.mem_unit * info.totalram); + blobmsg_add_u64(&b, "free", info.mem_unit * info.freeram); + blobmsg_add_u64(&b, "shared", info.mem_unit * info.sharedram); + blobmsg_add_u64(&b, "buffered", info.mem_unit * info.bufferram); + blobmsg_close_table(&b, c); + + c = blobmsg_open_table(&b, "swap"); + blobmsg_add_u64(&b, "total", info.mem_unit * info.totalswap); + blobmsg_add_u64(&b, "free", info.mem_unit * info.freeswap); + blobmsg_close_table(&b, c); + + ubus_send_reply(ctx, req, b.head); + + return UBUS_STATUS_OK; +} + +enum { + WDT_FREQUENCY, + WDT_TIMEOUT, + WDT_STOP, + __WDT_MAX +}; + +static const struct blobmsg_policy watchdog_policy[__WDT_MAX] = { + [WDT_FREQUENCY] = { .name = "frequency", .type = BLOBMSG_TYPE_INT32 }, + [WDT_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 }, + [WDT_STOP] = { .name = "stop", .type = BLOBMSG_TYPE_BOOL }, +}; + +static int watchdog_set(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + struct blob_attr *tb[__WDT_MAX]; + const char *status; + + if (!msg) + return UBUS_STATUS_INVALID_ARGUMENT; + + blobmsg_parse(watchdog_policy, __WDT_MAX, tb, blob_data(msg), blob_len(msg)); + if (tb[WDT_FREQUENCY]) { + unsigned int timeout = watchdog_timeout(0); + unsigned int freq = blobmsg_get_u32(tb[WDT_FREQUENCY]); + + if (freq) { + if (freq > timeout / 2) + freq = timeout / 2; + watchdog_frequency(freq); + } + } + + if (tb[WDT_TIMEOUT]) { + unsigned int timeout = blobmsg_get_u32(tb[WDT_TIMEOUT]); + unsigned int frequency = watchdog_frequency(0); + + if (timeout <= frequency) + timeout = frequency * 2; + watchdog_timeout(timeout); + } + + if (tb[WDT_STOP]) + watchdog_set_stopped(blobmsg_get_bool(tb[WDT_STOP])); + + if (watchdog_fd() == NULL) + status = "offline"; + else if (watchdog_get_stopped()) + status = "stopped"; + else + status = "running"; + + blob_buf_init(&b, 0); + blobmsg_add_string(&b, "status", status); + blobmsg_add_u32(&b, "timeout", watchdog_timeout(0)); + blobmsg_add_u32(&b, "frequency", watchdog_frequency(0)); + ubus_send_reply(ctx, req, b.head); + + return 0; +} + +enum { + SIGNAL_PID, + SIGNAL_NUM, + __SIGNAL_MAX +}; + +static const struct blobmsg_policy signal_policy[__SIGNAL_MAX] = { + [SIGNAL_PID] = { .name = "pid", .type = BLOBMSG_TYPE_INT32 }, + [SIGNAL_NUM] = { .name = "signum", .type = BLOBMSG_TYPE_INT32 }, +}; + +static int proc_signal(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + struct blob_attr *tb[__SIGNAL_MAX]; + + if (!msg) + return UBUS_STATUS_INVALID_ARGUMENT; + + blobmsg_parse(signal_policy, __SIGNAL_MAX, tb, blob_data(msg), blob_len(msg)); + if (!tb[SIGNAL_PID || !tb[SIGNAL_NUM]]) + return UBUS_STATUS_INVALID_ARGUMENT; + + kill(blobmsg_get_u32(tb[SIGNAL_PID]), blobmsg_get_u32(tb[SIGNAL_NUM])); + + return 0; +} + +static void +unitd_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj) +{ + notify = obj->has_subscribers; +} + + +static const struct ubus_method system_methods[] = { + UBUS_METHOD_NOARG("board", system_board), + UBUS_METHOD_NOARG("info", system_info), + UBUS_METHOD("watchdog", watchdog_set, watchdog_policy), + UBUS_METHOD("signal", proc_signal, signal_policy), +}; + +static struct ubus_object_type system_object_type = + UBUS_OBJECT_TYPE("system", system_methods); + +static struct ubus_object system_object = { + .name = "system", + .type = &system_object_type, + .methods = system_methods, + .n_methods = ARRAY_SIZE(system_methods), + .subscribe_cb = unitd_subscribe_cb, +}; + +void +unitd_bcast_event(char *event, struct blob_attr *msg) +{ + int ret; + + if (!notify) + return; + + ret = ubus_notify(_ctx, &system_object, event, msg, -1); + if (ret) + fprintf(stderr, "Failed to notify log: %s\n", ubus_strerror(ret)); +} + +void ubus_init_system(struct ubus_context *ctx) +{ + int ret; + + _ctx = ctx; + ret = ubus_add_object(ctx, &system_object); + if (ret) + ERROR("Failed to add object: %s\n", ubus_strerror(ret)); +} diff --git a/src/unitd/ubus.c b/src/unitd/ubus.c new file mode 100644 index 0000000..370d4ba --- /dev/null +++ b/src/unitd/ubus.c @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2015 Matthias Schiffer <mschiffer@universe-factory.net> + * + * Based on "procd" by: + * 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 "unitd.h" + +#include <sys/resource.h> +#include <stdlib.h> +#include <unistd.h> +#include <signal.h> + + +char *ubus_socket = NULL; +static struct ubus_context *ctx; +static struct uloop_timeout ubus_timer; + +static void +ubus_reconnect_cb(struct uloop_timeout *timeout) +{ + if (!ubus_reconnect(ctx, ubus_socket)) + ubus_add_uloop(ctx); + else + uloop_timeout_set(timeout, 2000); +} + +static void +ubus_disconnect_cb(struct ubus_context *ctx) +{ + ubus_timer.cb = ubus_reconnect_cb; + uloop_timeout_set(&ubus_timer, 2000); +} + +static void +ubus_connect_cb(struct uloop_timeout *timeout) +{ + ctx = ubus_connect(ubus_socket); + + if (!ctx) { + DEBUG(4, "Connection to ubus failed\n"); + uloop_timeout_set(&ubus_timer, 1000); + return; + } + + ctx->connection_lost = ubus_disconnect_cb; + ubus_init_service(ctx); + ubus_init_system(ctx); + + DEBUG(2, "Connected to ubus, id=%08x\n", ctx->local_id); + ubus_add_uloop(ctx); + unitd_state_ubus_connect(); +} + +void +unitd_connect_ubus(void) +{ + ubus_timer.cb = ubus_connect_cb; + uloop_timeout_set(&ubus_timer, 1000); +} diff --git a/src/unitd/unitd.c b/src/unitd/unitd.c new file mode 100644 index 0000000..aa26be3 --- /dev/null +++ b/src/unitd/unitd.c @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2015 Matthias Schiffer <mschiffer@universe-factory.net> + * + * Based on "procd" by: + * 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 "unitd.h" +#include "watchdog.h" + +#include <sys/wait.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/reboot.h> + +#include <unistd.h> +#include <getopt.h> +#include <libgen.h> + + +unsigned int debug = 4; + +static int usage(const char *prog) +{ + ERROR("Usage: %s [options]\n" + "Options:\n" + "\t-s <path>\tPath to ubus socket\n" + "\t-d <level>\tEnable debug messages\n" + "\n", prog); + return 1; +} + +int main(int argc, char **argv) +{ + int ch; + if (getpid() != 1) { + fprintf(stderr, "error: must run as PID 1\n"); + return 1; + } + + ulog_open(ULOG_KMSG, LOG_DAEMON, "unitd"); + + while ((ch = getopt(argc, argv, "d:s:")) != -1) { + switch (ch) { + case 's': + ubus_socket = optarg; + break; + case 'd': + debug = atoi(optarg); + break; + default: + return usage(argv[0]); + } + } + setsid(); + uloop_init(); + unitd_signal(); + unitd_state_next(); + uloop_run(); + uloop_done(); + + return 0; +} diff --git a/src/unitd/unitd.h b/src/unitd/unitd.h new file mode 100644 index 0000000..e9bebf1 --- /dev/null +++ b/src/unitd/unitd.h @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2015 Matthias Schiffer <mschiffer@universe-factory.net> + * + * Based on "procd" by: + * 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. + */ + +#pragma once + +#include "log.h" + +#include <libubox/uloop.h> +#include <libubox/utils.h> +#include <libubus.h> + +#include <stdio.h> +#include <syslog.h> + + +extern char *ubus_socket; + +void unitd_early(void); + +void unitd_connect_ubus(void); +void unitd_reconnect_ubus(int reconnect); +void ubus_init_service(struct ubus_context *ctx); +void ubus_init_system(struct ubus_context *ctx); + +void unitd_state_next(void); +void unitd_state_ubus_connect(void); +void unitd_shutdown(int event); +void unitd_early(void); +void unitd_preinit(void); +void unitd_coldplug(void); +void unitd_signal(void); +void unitd_signal_preinit(void); +void unitd_askconsole(void); +void unitd_bcast_event(char *event, struct blob_attr *msg); diff --git a/src/unitd/utils.c b/src/unitd/utils.c new file mode 100644 index 0000000..acf065c --- /dev/null +++ b/src/unitd/utils.c @@ -0,0 +1,160 @@ +/* + * Copyright (C) 2015 Matthias Schiffer <mschiffer@universe-factory.net> + * + * Based on "procd" by: + * 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 "utils.h" + +#include <libubox/avl.h> +#include <libubox/avl-cmp.h> + +#include <asm-generic/setup.h> +#include <regex.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> + +void +__blobmsg_list_init(struct blobmsg_list *list, int offset, int len, blobmsg_list_cmp cmp) +{ + avl_init(&list->avl, avl_strcmp, false, NULL); + list->node_offset = offset; + list->node_len = len; + list->cmp = cmp; +} + +int +blobmsg_list_fill(struct blobmsg_list *list, void *data, int len, bool array) +{ + struct avl_tree *tree = &list->avl; + struct blobmsg_list_node *node; + struct blob_attr *cur; + void *ptr; + int count = 0; + int rem = len; + + __blob_for_each_attr(cur, data, rem) { + if (!blobmsg_check_attr(cur, !array)) + continue; + + ptr = calloc(1, list->node_len); + if (!ptr) + return -1; + + node = (void *) ((char *)ptr + list->node_offset); + if (array) + node->avl.key = blobmsg_data(cur); + else + node->avl.key = blobmsg_name(cur); + node->data = cur; + if (avl_insert(tree, &node->avl)) { + free(ptr); + continue; + } + + count++; + } + + return count; +} + +void +blobmsg_list_move(struct blobmsg_list *list, struct blobmsg_list *src) +{ + struct blobmsg_list_node *node, *tmp; + void *ptr; + + avl_remove_all_elements(&src->avl, node, avl, tmp) { + if (avl_insert(&list->avl, &node->avl)) { + ptr = ((char *) node - list->node_offset); + free(ptr); + } + } +} + +void +blobmsg_list_free(struct blobmsg_list *list) +{ + struct blobmsg_list_node *node, *tmp; + void *ptr; + + avl_remove_all_elements(&list->avl, node, avl, tmp) { + ptr = ((char *) node - list->node_offset); + free(ptr); + } +} + +bool +blobmsg_list_equal(struct blobmsg_list *l1, struct blobmsg_list *l2) +{ + struct blobmsg_list_node *n1, *n2; + int count = l1->avl.count; + + if (count != l2->avl.count) + return false; + + n1 = avl_first_element(&l1->avl, n1, avl); + n2 = avl_first_element(&l2->avl, n2, avl); + + while (count-- > 0) { + int len; + + len = blob_len(n1->data); + if (len != blob_len(n2->data)) + return false; + + if (memcmp(n1->data, n2->data, len) != 0) + return false; + + if (l1->cmp && !l1->cmp(n1, n2)) + return false; + + if (!count) + break; + + n1 = avl_next_element(n1, avl); + n2 = avl_next_element(n2, avl); + } + + return true; +} + +char* get_cmdline_val(const char* name, char* out, int len) +{ + char line[CMDLINE_SIZE + 1], *c, *sptr; + int fd = open("/proc/cmdline", O_RDONLY); + ssize_t r = read(fd, line, sizeof(line) - 1); + close(fd); + + if (r <= 0) + return NULL; + + line[r] = 0; + + for (c = strtok_r(line, " \t\n", &sptr); c; + c = strtok_r(NULL, " \t\n", &sptr)) { + char *sep = strchr(c, '='); + ssize_t klen = sep - c; + if (klen < 0 || strncmp(name, c, klen) || name[klen] != 0) + continue; + + strncpy(out, &sep[1], len); + out[len-1] = 0; + return out; + } + + return NULL; +} diff --git a/src/unitd/utils.h b/src/unitd/utils.h new file mode 100644 index 0000000..6ab19ed --- /dev/null +++ b/src/unitd/utils.h @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2015 Matthias Schiffer <mschiffer@universe-factory.net> + * + * Based on "procd" by: + * 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. + */ + +#pragma once + +#include <libubox/avl.h> +#include <libubox/blob.h> +#include <libubox/blobmsg.h> + +#define CMDLINE_SIZE 2048 + +struct blobmsg_list_node { + struct avl_node avl; + struct blob_attr *data; +}; + +typedef bool (*blobmsg_list_cmp)(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2); +typedef void (*blobmsg_update_cb)(struct blobmsg_list_node *n); + +struct blobmsg_list { + struct avl_tree avl; + int node_offset; + int node_len; + + blobmsg_list_cmp cmp; +}; + +#define blobmsg_list_simple_init(list) \ + __blobmsg_list_init(list, 0, sizeof(struct blobmsg_list_node), NULL) + +#define blobmsg_list_init(list, type, field, cmp) \ + __blobmsg_list_init(list, offsetof(type, field), sizeof(type), cmp) + +#define blobmsg_list_for_each(list, element) \ + avl_for_each_element(&(list)->avl, element, avl) + +void __blobmsg_list_init(struct blobmsg_list *list, int offset, int len, blobmsg_list_cmp cmp); +int blobmsg_list_fill(struct blobmsg_list *list, void *data, int len, bool array); +void blobmsg_list_free(struct blobmsg_list *list); +bool blobmsg_list_equal(struct blobmsg_list *l1, struct blobmsg_list *l2); +void blobmsg_list_move(struct blobmsg_list *list, struct blobmsg_list *src); +char* get_cmdline_val(const char* name, char* out, int len); diff --git a/src/unitd/watchdog.c b/src/unitd/watchdog.c new file mode 100644 index 0000000..f462d00 --- /dev/null +++ b/src/unitd/watchdog.c @@ -0,0 +1,139 @@ +/* + * Copyright (C) 2015 Matthias Schiffer <mschiffer@universe-factory.net> + * + * Based on "procd" by: + * 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 "unitd.h" +#include "watchdog.h" + +#include <linux/watchdog.h> + +#include <sys/ioctl.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> + +#include <unistd.h> + +#include <libubox/uloop.h> + + +#define WDT_PATH "/dev/watchdog" + +static struct uloop_timeout wdt_timeout; +static int wdt_fd = -1; +static int wdt_frequency = 5; + +void watchdog_ping(void) +{ + DEBUG(4, "Ping\n"); + if (wdt_fd >= 0 && write(wdt_fd, "X", 1) < 0) + ERROR("WDT failed to write: %s\n", strerror(errno)); +} + +static void watchdog_timeout_cb(struct uloop_timeout *t) +{ + watchdog_ping(); + uloop_timeout_set(t, wdt_frequency * 1000); +} + +void watchdog_set_stopped(bool val) +{ + if (val) + uloop_timeout_cancel(&wdt_timeout); + else + watchdog_timeout_cb(&wdt_timeout); +} + +bool watchdog_get_stopped(void) +{ + return !wdt_timeout.pending; +} + +int watchdog_timeout(int timeout) +{ + if (wdt_fd < 0) + return 0; + + if (timeout) { + DEBUG(4, "Set watchdog timeout: %ds\n", timeout); + ioctl(wdt_fd, WDIOC_SETTIMEOUT, &timeout); + } + ioctl(wdt_fd, WDIOC_GETTIMEOUT, &timeout); + + return timeout; +} + +int watchdog_frequency(int frequency) +{ + if (wdt_fd < 0) + return 0; + + if (frequency) { + DEBUG(4, "Set watchdog frequency: %ds\n", frequency); + wdt_frequency = frequency; + } + + return wdt_frequency; +} + +char* watchdog_fd(void) +{ + static char fd_buf[3]; + + if (wdt_fd < 0) + return NULL; + snprintf(fd_buf, sizeof(fd_buf), "%d", wdt_fd); + + return fd_buf; +} + +void watchdog_init(int preinit) +{ + char *env = getenv("WDTFD"); + + if (wdt_fd >= 0) + return; + + wdt_timeout.cb = watchdog_timeout_cb; + if (env) { + DEBUG(2, "Watchdog handover: fd=%s\n", env); + wdt_fd = atoi(env); + unsetenv("WDTFD"); + } else { + wdt_fd = open("/dev/watchdog", O_WRONLY); + } + + if (wdt_fd < 0) + return; + + if (!preinit) + fcntl(wdt_fd, F_SETFD, fcntl(wdt_fd, F_GETFD) | FD_CLOEXEC); + + LOG("- watchdog -\n"); + watchdog_timeout(30); + watchdog_timeout_cb(&wdt_timeout); + + DEBUG(4, "Opened watchdog with timeout %ds\n", watchdog_timeout(0)); +} + + +void watchdog_no_cloexec(void) +{ + if (wdt_fd < 0) + return; + + fcntl(wdt_fd, F_SETFD, fcntl(wdt_fd, F_GETFD) & ~FD_CLOEXEC); +} diff --git a/src/unitd/watchdog.h b/src/unitd/watchdog.h new file mode 100644 index 0000000..6c4bc6a --- /dev/null +++ b/src/unitd/watchdog.h @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2015 Matthias Schiffer <mschiffer@universe-factory.net> + * + * Based on "procd" by: + * 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. + */ + +#pragma once + +void watchdog_init(int preinit); +char* watchdog_fd(void); +int watchdog_timeout(int timeout); +int watchdog_frequency(int frequency); +void watchdog_set_stopped(bool val); +bool watchdog_get_stopped(void); +void watchdog_no_cloexec(void); +void watchdog_ping(void); |