mirror of
https://github.com/troglobit/finit.git
synced 2026-10-25 12:04:36 +00:00
Revert new experimental libev backend due to time constraints
Due to time constraints I had to revert the new libev backend to the
service, I/O and signals monitor. Employed instead is the tried and
tested signals based service monitor, with a small extension for an
additional I/O monitor using poll() for the I/O plugins to use.
Tried restoring the startx finit.conf directive on top of the service
implementation. However, it's currently not usable to run /usr/bin/startx
for any other user than root. I recommend using the examples/finit.conf
and run /usr/sbin/gdm instead.
finit.c:
New run_loop() function, replaces libev ev_run().
helpers.c:
Refactor start_process() into svc_start().
plugin.c:
Add fds[] array for use in the io_monitor() called by the
new run_loop().
signal.c:
Ignore SIGCHLD. Handled by waitpid() in svc_monitor() now,
which is called by the new run_loop().
svc.c:
svc_register(): Optional @username parsing for the command line.
svc_monitor(): Replaced libev callback with waitpid()
svc_start(): Refactored to use most of helpers.c:start_process()
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
1 parent
75eb5cc043
commit
c49d89dfe0
13 files changed
+198
-131
No files matched your search
@@ -42,7 +42,7 @@ CPPFLAGS += -D_XOPEN_SOURCE=600 -D_BSD_SOURCE -D_GNU_SOURCE
|
|||||||
CPPFLAGS += -DVERSION=\"$(VERSION)\" -DWHOAMI=\"`whoami`@`hostname`\"
|
CPPFLAGS += -DVERSION=\"$(VERSION)\" -DWHOAMI=\"`whoami`@`hostname`\"
|
||||||
CPPFLAGS += -DPLUGIN_PATH=\"$(PLUGIN_DIR)\"
|
CPPFLAGS += -DPLUGIN_PATH=\"$(PLUGIN_DIR)\"
|
||||||
LDFLAGS += -rdynamic
|
LDFLAGS += -rdynamic
|
||||||
LDLIBS += -ldl -lev
|
LDLIBS += -ldl
|
||||||
|
|
||||||
prefix ?= /usr/local
|
prefix ?= /usr/local
|
||||||
sysconfdir ?= /etc
|
sysconfdir ?= /etc
|
||||||
|
|||||||
@@ -122,9 +122,12 @@ void parse_finit_conf(char *file)
|
|||||||
network = build_cmd(NULL, x, CMD_SIZE);
|
network = build_cmd(NULL, x, CMD_SIZE);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (MATCH_CMD(line, "startx ", x)) {
|
||||||
|
svc_register(x, username);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (MATCH_CMD(line, "service ", x)) {
|
if (MATCH_CMD(line, "service ", x)) {
|
||||||
if (svc_register(x))
|
svc_register(x, NULL);
|
||||||
_e("Failed, too many services to monitor.\n");
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-2
@@ -1,6 +1,5 @@
|
|||||||
# Simple Debian finit.conf
|
# Simple Debian finit.conf
|
||||||
check /dev/vda1
|
check /dev/vda1
|
||||||
module tipc
|
|
||||||
module evdev
|
module evdev
|
||||||
module loop
|
module loop
|
||||||
module psmouse
|
module psmouse
|
||||||
@@ -10,4 +9,4 @@ network service networking start
|
|||||||
service /sbin/klogd -n -- Kernel logging server
|
service /sbin/klogd -n -- Kernel logging server
|
||||||
service /sbin/syslogd -n -- Syslog server
|
service /sbin/syslogd -n -- Syslog server
|
||||||
service /usr/sbin/sshd -D -- OpenSSH server
|
service /usr/sbin/sshd -D -- OpenSSH server
|
||||||
service /usr/bin/startx -- X Window System
|
service /usr/sbin/gdm -- GNOME Display Manager
|
||||||
@@ -22,10 +22,11 @@
|
|||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <ctype.h>
|
|
||||||
#include <sys/mount.h>
|
#include <sys/mount.h>
|
||||||
|
#include <ctype.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <linux/fs.h>
|
#include <linux/fs.h>
|
||||||
|
#include <sys/stat.h> /* umask(), mkdir() */
|
||||||
|
|
||||||
#include "finit.h"
|
#include "finit.h"
|
||||||
#include "helpers.h"
|
#include "helpers.h"
|
||||||
@@ -60,10 +61,18 @@ static void parse_kernel_cmdline(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int run_loop(void)
|
||||||
|
{
|
||||||
|
while (1) {
|
||||||
|
svc_monitor();
|
||||||
|
io_monitor();
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int UNUSED(args), char *argv[])
|
int main(int UNUSED(args), char *argv[])
|
||||||
{
|
{
|
||||||
struct ev_loop *loop = ev_default_loop(0);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Initial setup of signals, ignore all until we're up.
|
* Initial setup of signals, ignore all until we're up.
|
||||||
*/
|
*/
|
||||||
@@ -133,7 +142,7 @@ int main(int UNUSED(args), char *argv[])
|
|||||||
* Load plugins and run first level hooks.
|
* Load plugins and run first level hooks.
|
||||||
*/
|
*/
|
||||||
_d("Loading plugins ...");
|
_d("Loading plugins ...");
|
||||||
load_plugins(loop, PLUGIN_PATH);
|
load_plugins(PLUGIN_PATH);
|
||||||
|
|
||||||
_d("Running first level hooks ...");
|
_d("Running first level hooks ...");
|
||||||
run_hooks(HOOK_BASEFS_UP);
|
run_hooks(HOOK_BASEFS_UP);
|
||||||
@@ -183,9 +192,7 @@ int main(int UNUSED(args), char *argv[])
|
|||||||
* Enter main loop to monior /dev/initctl and services
|
* Enter main loop to monior /dev/initctl and services
|
||||||
*/
|
*/
|
||||||
_d("Entering main loop ...");
|
_d("Entering main loop ...");
|
||||||
ev_run(loop, 0);
|
return run_loop();
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -29,6 +29,8 @@
|
|||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <syslog.h>
|
#include <syslog.h>
|
||||||
|
|||||||
@@ -174,37 +174,6 @@ int print_result(int fail)
|
|||||||
return fail;
|
return fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
int start_process(char *cmd, char *args[], int console)
|
|
||||||
{
|
|
||||||
pid_t pid;
|
|
||||||
|
|
||||||
if (sig_stopped())
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
pid = fork();
|
|
||||||
if (!pid) {
|
|
||||||
int i;
|
|
||||||
struct sigaction sa;
|
|
||||||
|
|
||||||
/* Reset signal handlers that were set by the parent process */
|
|
||||||
for (i = 1; i < NSIG; i++)
|
|
||||||
DFLSIG(sa, i, 0);
|
|
||||||
|
|
||||||
if (console) {
|
|
||||||
int fd = open (CONSOLE, O_WRONLY | O_APPEND);
|
|
||||||
if (-1 != fd) {
|
|
||||||
dup2(STDOUT_FILENO, fd);
|
|
||||||
dup2(STDERR_FILENO, fd);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
execvp(cmd, args);
|
|
||||||
exit(!console ? print_result(0) : 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
return pid;
|
|
||||||
}
|
|
||||||
|
|
||||||
int run(char *cmd)
|
int run(char *cmd)
|
||||||
{
|
{
|
||||||
int status, result, i = 0;
|
int status, result, i = 0;
|
||||||
@@ -353,12 +322,14 @@ pid_t run_getty(char *cmd, char *argv[])
|
|||||||
sigset_t nmask;
|
sigset_t nmask;
|
||||||
struct sigaction sa;
|
struct sigaction sa;
|
||||||
|
|
||||||
|
/* Detach from initial controlling TTY */
|
||||||
vhangup();
|
vhangup();
|
||||||
|
|
||||||
close(2);
|
close(2);
|
||||||
close(1);
|
close(1);
|
||||||
close(0);
|
close(0);
|
||||||
|
|
||||||
|
/* Attach TTY to console */
|
||||||
if (open(CONSOLE, O_RDWR) != 0)
|
if (open(CONSOLE, O_RDWR) != 0)
|
||||||
exit(1);
|
exit(1);
|
||||||
|
|
||||||
@@ -479,7 +450,7 @@ int getuser(char *s)
|
|||||||
{
|
{
|
||||||
struct passwd *usr;
|
struct passwd *usr;
|
||||||
|
|
||||||
if ((usr = getpwnam(s)) == NULL)
|
if (!s || (usr = getpwnam(s)) == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
return usr->pw_uid;
|
return usr->pw_uid;
|
||||||
|
|||||||
@@ -24,6 +24,7 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <dlfcn.h> /* dlopen() et al */
|
#include <dlfcn.h> /* dlopen() et al */
|
||||||
#include <dirent.h> /* readdir() et al */
|
#include <dirent.h> /* readdir() et al */
|
||||||
|
#include <poll.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/queue.h> /* BSD sys/queue.h API */
|
#include <sys/queue.h> /* BSD sys/queue.h API */
|
||||||
|
|
||||||
@@ -33,10 +34,16 @@
|
|||||||
#include "plugin.h"
|
#include "plugin.h"
|
||||||
#include "svc.h"
|
#include "svc.h"
|
||||||
|
|
||||||
|
#define is_io_plugin(p) ((p)->io.cb && (p)->io.fd >= 0)
|
||||||
|
|
||||||
|
static size_t num_fds = 0;
|
||||||
|
static struct pollfd fds[MAX_NUM_FDS];
|
||||||
static LIST_HEAD(, plugin) plugins = LIST_HEAD_INITIALIZER();
|
static LIST_HEAD(, plugin) plugins = LIST_HEAD_INITIALIZER();
|
||||||
|
|
||||||
int plugin_register(plugin_t *plugin)
|
int plugin_register(plugin_t *plugin)
|
||||||
{
|
{
|
||||||
|
int inuse = 0;
|
||||||
|
|
||||||
if (!plugin) {
|
if (!plugin) {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return 1;
|
return 1;
|
||||||
@@ -51,15 +58,29 @@ int plugin_register(plugin_t *plugin)
|
|||||||
plugin->name = (char *)info.dli_fname;
|
plugin->name = (char *)info.dli_fname;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (is_io_plugin(plugin)) {
|
||||||
|
if (num_fds + 1 >= MAX_NUM_FDS) {
|
||||||
|
num_fds = MAX_NUM_SVC;
|
||||||
|
errno = ENOMEM;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
num_fds++;
|
||||||
|
inuse++;
|
||||||
|
}
|
||||||
|
|
||||||
if (plugin->svc.cb) {
|
if (plugin->svc.cb) {
|
||||||
svc_t *svc = svc_find_by_name(plugin->name);
|
svc_t *svc = svc_find_by_name(plugin->name);
|
||||||
|
|
||||||
if (!svc) {
|
if (svc) {
|
||||||
_e("No service \"%s\" loaded, skipping plugin.", basename(plugin->name));
|
plugin->svc.id = svc_id(svc);
|
||||||
return 1;
|
svc->plugin = &plugin->svc;
|
||||||
|
inuse++;
|
||||||
}
|
}
|
||||||
plugin->svc.id = svc_id(svc);
|
}
|
||||||
svc->plugin = &plugin->svc;
|
|
||||||
|
if (!inuse) {
|
||||||
|
_e("No service \"%s\" loaded, skipping plugin.", basename(plugin->name));
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
LIST_INSERT_HEAD(&plugins, plugin, link);
|
LIST_INSERT_HEAD(&plugins, plugin, link);
|
||||||
@@ -71,6 +92,8 @@ int plugin_unregister(plugin_t *plugin)
|
|||||||
{
|
{
|
||||||
LIST_REMOVE(plugin, link);
|
LIST_REMOVE(plugin, link);
|
||||||
|
|
||||||
|
/* XXX: Unfinished, add cleanup code here! */
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,44 +123,62 @@ void run_services(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Generic libev I/O callback, looks up correct plugin and calls its callback */
|
/* Generic libev I/O callback, looks up correct plugin and calls its callback */
|
||||||
static void generic_io_cb(struct ev_loop *loop, ev_io *w, int revents)
|
static void generic_io_cb(int fd, int events)
|
||||||
{
|
{
|
||||||
plugin_t *p;
|
plugin_t *p;
|
||||||
|
|
||||||
ev_io_stop(loop, w);
|
|
||||||
|
|
||||||
/* Find matching plugin, pick first matching fd */
|
/* Find matching plugin, pick first matching fd */
|
||||||
PLUGIN_ITERATOR(p) {
|
PLUGIN_ITERATOR(p) {
|
||||||
if (p->io.cb && p->io.fd == w->fd) {
|
if (is_io_plugin(p) && p->io.fd == fd) {
|
||||||
p->io.cb(p->io.arg, w->fd, revents);
|
_d("Calling I/O %s from runloop...", basename(p->name));
|
||||||
|
p->io.cb(p->io.arg, fd, events);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ev_io_start(loop, w);
|
void io_monitor(void)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
while ((ret = poll(fds, num_fds, 500))) {
|
||||||
|
if (-1 == ret) {
|
||||||
|
if (EINTR == errno)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
_e("Failed polling I/O plugin descriptors, error %d: %s",
|
||||||
|
errno, strerror(errno));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Traverse all I/O fds and run callbacks */
|
||||||
|
for (i = 0; i < num_fds; i++) {
|
||||||
|
if (fds[i].revents)
|
||||||
|
generic_io_cb(fds[i].fd, fds[i].revents);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Setup any I/O callbacks for plugins that use them */
|
/* Setup any I/O callbacks for plugins that use them */
|
||||||
static void init_plugins(struct ev_loop *loop)
|
static void init_plugins(void)
|
||||||
{
|
{
|
||||||
|
int i = 0;
|
||||||
plugin_t *p;
|
plugin_t *p;
|
||||||
|
|
||||||
PLUGIN_ITERATOR(p) {
|
PLUGIN_ITERATOR(p) {
|
||||||
if (p->io.cb && p->io.fd >= 0) {
|
if (is_io_plugin(p)) {
|
||||||
ev_io *w = calloc(1, sizeof(ev_io));
|
|
||||||
|
|
||||||
_d("Initializing plugin %s for I/O", basename(p->name));
|
_d("Initializing plugin %s for I/O", basename(p->name));
|
||||||
if (!w) {
|
fds[i].revents = 0;
|
||||||
_e("Failed setting up plugin %s: Out of memory", basename(p->name));
|
fds[i].events = p->io.flags;
|
||||||
continue;
|
fds[num_fds++].fd = p->io.fd;
|
||||||
}
|
|
||||||
ev_io_init(w, generic_io_cb, p->io.fd, p->io.flags);
|
|
||||||
ev_io_start(loop, w);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int load_plugins(struct ev_loop *loop, char *path)
|
int load_plugins(char *path)
|
||||||
{
|
{
|
||||||
DIR *dp = opendir(path);
|
DIR *dp = opendir(path);
|
||||||
struct dirent *entry;
|
struct dirent *entry;
|
||||||
@@ -167,7 +208,7 @@ int load_plugins(struct ev_loop *loop, char *path)
|
|||||||
}
|
}
|
||||||
|
|
||||||
closedir(dp);
|
closedir(dp);
|
||||||
init_plugins(loop);
|
init_plugins();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,11 +24,11 @@
|
|||||||
#ifndef FINIT_PLUGIN_H_
|
#ifndef FINIT_PLUGIN_H_
|
||||||
#define FINIT_PLUGIN_H_
|
#define FINIT_PLUGIN_H_
|
||||||
|
|
||||||
#include <ev.h>
|
#include <poll.h>
|
||||||
#include <sys/queue.h> /* BSD sys/queue.h API */
|
#include <sys/queue.h> /* BSD sys/queue.h API */
|
||||||
|
|
||||||
#define PLUGIN_IO_READ EV_READ
|
#define PLUGIN_IO_READ POLLIN
|
||||||
#define PLUGIN_IO_WRITE EV_READ
|
#define PLUGIN_IO_WRITE POLLOUT
|
||||||
|
|
||||||
#define PLUGIN_INIT(x) static void __attribute__ ((constructor)) x(void)
|
#define PLUGIN_INIT(x) static void __attribute__ ((constructor)) x(void)
|
||||||
#define PLUGIN_EXIT(x) static void __attribute__ ((destructor)) x(void)
|
#define PLUGIN_EXIT(x) static void __attribute__ ((destructor)) x(void)
|
||||||
|
|||||||
@@ -77,6 +77,8 @@ static void read_initctl(void *UNUSED(arg), int fd, int UNUSED(events))
|
|||||||
_d("Unsupported runlevel: %d", request.runlevel);
|
_d("Unsupported runlevel: %d", request.runlevel);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
_d("Unsupported cmd: %d", request.cmd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,7 +30,8 @@
|
|||||||
/* plugin.c */
|
/* plugin.c */
|
||||||
void run_hooks (hook_point_t no);
|
void run_hooks (hook_point_t no);
|
||||||
void run_services (void);
|
void run_services (void);
|
||||||
int load_plugins (struct ev_loop *loop, char *path);
|
int load_plugins (char *path);
|
||||||
|
void io_monitor (void);
|
||||||
|
|
||||||
#endif /* FINIT_PRIVATE_H_ */
|
#endif /* FINIT_PRIVATE_H_ */
|
||||||
|
|
||||||
|
|||||||
@@ -98,13 +98,7 @@ static void shutdown_handler(int sig, siginfo_t *info, void *UNUSED(ctx))
|
|||||||
*/
|
*/
|
||||||
static void chld_handler(int UNUSED(sig), siginfo_t *UNUSED(info), void *UNUSED(ctx))
|
static void chld_handler(int UNUSED(sig), siginfo_t *UNUSED(info), void *UNUSED(ctx))
|
||||||
{
|
{
|
||||||
int status;
|
/* Do nothing, the svc_monitor() is the designated child reaper. */
|
||||||
|
|
||||||
// _d("Child %d died due to signal %d, harvesting.", info->si_pid, sig);
|
|
||||||
while (waitpid(-1, &status, WNOHANG) != 0) {
|
|
||||||
if (errno == ECHILD)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -23,6 +23,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
|
||||||
#include "finit.h"
|
#include "finit.h"
|
||||||
#include "helpers.h"
|
#include "helpers.h"
|
||||||
@@ -35,7 +36,7 @@
|
|||||||
* liveness state of all daemons that should run. */
|
* liveness state of all daemons that should run. */
|
||||||
static int svc_counter = 0; /* Number of registered services to monitor. */
|
static int svc_counter = 0; /* Number of registered services to monitor. */
|
||||||
static svc_t *services = NULL; /* List of registered services, in shared memory. */
|
static svc_t *services = NULL; /* List of registered services, in shared memory. */
|
||||||
static ev_child watchers[MAX_NUM_SVC];
|
|
||||||
|
|
||||||
/* Connect to/create shared memory area of registered servies */
|
/* Connect to/create shared memory area of registered servies */
|
||||||
static void __connect_shm(void)
|
static void __connect_shm(void)
|
||||||
@@ -138,28 +139,61 @@ svc_t *svc_iterator(int restart)
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* svc_register - Register a non-backgrounding service to be monitored
|
* svc_register - Register a non-backgrounding service to be monitored
|
||||||
* @line: A complete command line with -- separated description text
|
* @line: A complete command line with -- separated description text
|
||||||
|
* @username: Optional username to run service as, or %NULL to run as root
|
||||||
|
*
|
||||||
|
* Actually, the @line can optionally start with a username, denoted by
|
||||||
|
* an @ character. Like this:
|
||||||
|
*
|
||||||
|
* "service [@username] /path/to/daemon -- Description text"
|
||||||
|
*
|
||||||
|
* The [] brackets means the argument is optional. If the username is
|
||||||
|
* left out the daemon is started as root.
|
||||||
*
|
*
|
||||||
* Returns:
|
* Returns:
|
||||||
* POSIX OK(0) on success, or non-zero errno exit status on failure.
|
* POSIX OK(0) on success, or non-zero errno exit status on failure.
|
||||||
*/
|
*/
|
||||||
int svc_register(char *line)
|
int svc_register(char *line, char *username)
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
char *cmd, *descr;
|
char *cmd, *descr;
|
||||||
svc_t *svc = svc_new();
|
svc_t *svc;
|
||||||
|
|
||||||
if (!svc || !line) {
|
if (!line) {
|
||||||
return -EINVAL;
|
_e("Invalid input argument.");
|
||||||
|
return errno = EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
descr = strstr(line, "-- ");
|
descr = strstr(line, "-- ");
|
||||||
if (descr) {
|
if (descr)
|
||||||
*descr = 0;
|
*descr = 0;
|
||||||
strlcpy(svc->descr, descr + 3, sizeof(svc->descr));
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd = strtok(line, " ");
|
cmd = strtok(line, " ");
|
||||||
|
if (!cmd) {
|
||||||
|
incomplete:
|
||||||
|
_e("Incomplete service, cannot register.");
|
||||||
|
return errno = ENOENT;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cmd[0] == '@') { /* @username */
|
||||||
|
username = &cmd[1];
|
||||||
|
|
||||||
|
/* Check if valid command follows... */
|
||||||
|
cmd = strtok(NULL, " ");
|
||||||
|
if (!cmd)
|
||||||
|
goto incomplete;
|
||||||
|
}
|
||||||
|
|
||||||
|
svc = svc_new();
|
||||||
|
if (!svc) {
|
||||||
|
_e("Out of memorym, cannot register service %s", cmd);
|
||||||
|
return errno = ENOMEM;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (descr)
|
||||||
|
strlcpy(svc->descr, descr + 3, sizeof(svc->descr));
|
||||||
|
if (username)
|
||||||
|
strlcpy(svc->username, username, sizeof(svc->username));
|
||||||
strlcpy(svc->cmd, cmd, sizeof(svc->cmd));
|
strlcpy(svc->cmd, cmd, sizeof(svc->cmd));
|
||||||
strlcpy(svc->args[i++], cmd, sizeof(svc->args[0]));
|
strlcpy(svc->args[i++], cmd, sizeof(svc->args[0]));
|
||||||
|
|
||||||
@@ -206,27 +240,14 @@ static int is_norespawn(void)
|
|||||||
fexist("/tmp/norespawn");
|
fexist("/tmp/norespawn");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Used to cleanup any lingering or semi-restarting tasks before respawning. */
|
void svc_monitor(void)
|
||||||
static int kill_service(svc_t *svc, int signo)
|
|
||||||
{
|
{
|
||||||
char *name = basename(svc->cmd);
|
pid_t lost;
|
||||||
|
|
||||||
_d("Sending signal %d to all processes named %s", signo, name);
|
|
||||||
kill_procname(name, signo);
|
|
||||||
|
|
||||||
/* Don't restart immediately, some throttling necessary.
|
|
||||||
* Also it's good form to let the application terminate properly. */
|
|
||||||
sleep(1);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void svc_monitor(EV_P_ ev_child *w, int UNUSED(revents))
|
|
||||||
{
|
|
||||||
pid_t lost = w->rpid;
|
|
||||||
svc_t *svc;
|
svc_t *svc;
|
||||||
|
|
||||||
ev_child_stop(EV_A_ w);
|
lost = waitpid(-1, NULL, WNOHANG);
|
||||||
|
if (lost < 1)
|
||||||
|
return;
|
||||||
|
|
||||||
if (fexist(SYNC_SHUTDOWN) || lost <= 1)
|
if (fexist(SYNC_SHUTDOWN) || lost <= 1)
|
||||||
return;
|
return;
|
||||||
@@ -236,16 +257,20 @@ static void svc_monitor(EV_P_ ev_child *w, int UNUSED(revents))
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
for (svc = svc_iterator(1); svc; svc = svc_iterator(0)) {
|
for (svc = svc_iterator(1); svc; svc = svc_iterator(0)) {
|
||||||
|
char *name = basename(svc->cmd);
|
||||||
|
|
||||||
if (lost != svc->pid)
|
if (lost != svc->pid)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
_d("Ouch, lost pid %d - %s(%d)", lost, name, svc->pid);
|
||||||
if (sig_stopped()) {
|
if (sig_stopped()) {
|
||||||
_e("Stopped, not respawning killed processes.");
|
_e("Stopped, not respawning killed processes.");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
_d("Ouch, lost pid %d - %s(%d)", lost, svc->cmd, svc->pid);
|
/* Cleanup any lingering or semi-restarting tasks before respawning */
|
||||||
kill_service(svc, SIGTERM);
|
_d("Sending SIGTERM to service group %s", name);
|
||||||
|
kill_procname(name, SIGTERM);
|
||||||
|
|
||||||
/* Restarting lost service. */
|
/* Restarting lost service. */
|
||||||
if (svc_enabled(svc, 0))
|
if (svc_enabled(svc, 0))
|
||||||
@@ -257,36 +282,56 @@ static void svc_monitor(EV_P_ ev_child *w, int UNUSED(revents))
|
|||||||
|
|
||||||
int svc_start(svc_t *svc)
|
int svc_start(svc_t *svc)
|
||||||
{
|
{
|
||||||
|
int respawn = svc->pid != 0;
|
||||||
|
pid_t pid;
|
||||||
char *args[MAX_NUM_SVC_ARGS];
|
char *args[MAX_NUM_SVC_ARGS];
|
||||||
int i = 0, restart = 0;
|
|
||||||
ev_child *cw = &watchers[svc->id];
|
|
||||||
|
|
||||||
/* Ignore if finit is SIGSTOP'ed */
|
/* Ignore if finit is SIGSTOP'ed */
|
||||||
if (is_norespawn())
|
if (is_norespawn())
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
restart = svc->pid != 0;
|
if (!respawn)
|
||||||
if (!restart) print_descr("Starting ", svc->descr);
|
print_descr("Starting ", svc->descr);
|
||||||
|
|
||||||
/* Serve copy of args to process in case it modifies them. */
|
pid = fork();
|
||||||
while (svc->args[i][0] != 0 && i < MAX_NUM_SVC_ARGS) {
|
if (pid == 0) {
|
||||||
args[i] = svc->args[i];
|
int i = 0;
|
||||||
i++;
|
int uid = getuser(svc->username);
|
||||||
|
struct sigaction sa;
|
||||||
|
|
||||||
|
/* Reset signal handlers that were set by the parent process */
|
||||||
|
for (i = 1; i < NSIG; i++)
|
||||||
|
DFLSIG(sa, i, 0);
|
||||||
|
|
||||||
|
/* Set desired user */
|
||||||
|
if (uid >= 0)
|
||||||
|
setuid(uid);
|
||||||
|
|
||||||
|
/* Serve copy of args to process in case it modifies them. */
|
||||||
|
for (i = 0; svc->args[i][0] != 0 && i < MAX_NUM_SVC_ARGS; i++)
|
||||||
|
args[i] = svc->args[i];
|
||||||
|
args[i] = NULL;
|
||||||
|
|
||||||
|
if (debug) {
|
||||||
|
int fd = open (CONSOLE, O_WRONLY | O_APPEND);
|
||||||
|
if (-1 != fd) {
|
||||||
|
dup2(STDOUT_FILENO, fd);
|
||||||
|
dup2(STDERR_FILENO, fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
_e("%starting %s ", respawn ? "Res" : "S", svc->cmd);
|
||||||
|
for (i = 0; args[i] && i < MAX_NUM_SVC_ARGS; i++)
|
||||||
|
_e("%s ", args[i]);
|
||||||
|
_e("");
|
||||||
|
}
|
||||||
|
|
||||||
|
execvp(svc->cmd, args);
|
||||||
|
exit(0);
|
||||||
}
|
}
|
||||||
args[i] = NULL;
|
svc->pid = pid;
|
||||||
|
|
||||||
if (debug) {
|
if (!respawn)
|
||||||
_e("(re)starting %s ", svc->cmd);
|
print_result(svc->pid > 1 ? 0 : 1);
|
||||||
for (i = 0; args[i] && i < MAX_NUM_SVC_ARGS; i++)
|
|
||||||
_e("%s ", args[i]);
|
|
||||||
_e("");
|
|
||||||
}
|
|
||||||
|
|
||||||
svc->pid = start_process(svc->cmd, args, debug);
|
|
||||||
ev_child_init (cw, svc_monitor, svc->pid, 0);
|
|
||||||
ev_child_start (EV_DEFAULT_ cw);
|
|
||||||
|
|
||||||
if (!restart) print_result(0);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -304,7 +349,6 @@ int svc_start_by_name(char *name)
|
|||||||
int svc_stop(svc_t *svc)
|
int svc_stop(svc_t *svc)
|
||||||
{
|
{
|
||||||
int res = 1;
|
int res = 1;
|
||||||
ev_child *cw = &watchers[svc->id];
|
|
||||||
|
|
||||||
if (!svc) {
|
if (!svc) {
|
||||||
_e("Failed, no svc pointer.");
|
_e("Failed, no svc pointer.");
|
||||||
@@ -319,7 +363,6 @@ int svc_stop(svc_t *svc)
|
|||||||
|
|
||||||
print_descr("Stopping ", svc->descr);
|
print_descr("Stopping ", svc->descr);
|
||||||
_d("Sending SIGTERM to pid:%d name:'%s'", svc->pid, get_pidname(svc->pid, NULL, 0));
|
_d("Sending SIGTERM to pid:%d name:'%s'", svc->pid, get_pidname(svc->pid, NULL, 0));
|
||||||
ev_child_stop(ev_default_loop(0), cw);
|
|
||||||
res = kill(svc->pid, SIGTERM);
|
res = kill(svc->pid, SIGTERM);
|
||||||
print_result(res);
|
print_result(res);
|
||||||
svc->pid = 0;
|
svc->pid = 0;
|
||||||
|
|||||||
@@ -42,6 +42,8 @@ typedef enum {
|
|||||||
#define FINIT_SHM_ID 0x494E4954 /* "INIT", see ascii(7) */
|
#define FINIT_SHM_ID 0x494E4954 /* "INIT", see ascii(7) */
|
||||||
#define MAX_ARG_LEN 64
|
#define MAX_ARG_LEN 64
|
||||||
#define MAX_STR_LEN 64
|
#define MAX_STR_LEN 64
|
||||||
|
#define MAX_USER_LEN 16
|
||||||
|
#define MAX_NUM_FDS 64 /* Max number of I/O plugins */
|
||||||
#define MAX_NUM_SVC 64 /* Enough? */
|
#define MAX_NUM_SVC 64 /* Enough? */
|
||||||
#define MAX_NUM_SVC_ARGS 16
|
#define MAX_NUM_SVC_ARGS 16
|
||||||
|
|
||||||
@@ -55,6 +57,7 @@ typedef struct svc {
|
|||||||
char cmd[MAX_ARG_LEN];
|
char cmd[MAX_ARG_LEN];
|
||||||
char args[MAX_NUM_SVC_ARGS][MAX_ARG_LEN];
|
char args[MAX_NUM_SVC_ARGS][MAX_ARG_LEN];
|
||||||
char descr[MAX_STR_LEN];
|
char descr[MAX_STR_LEN];
|
||||||
|
char username[MAX_USER_LEN];
|
||||||
/* Public */
|
/* Public */
|
||||||
int delayed_reload; /* For external service plugin. */
|
int delayed_reload; /* For external service plugin. */
|
||||||
plugin_svc_t *plugin;
|
plugin_svc_t *plugin;
|
||||||
@@ -94,7 +97,7 @@ svc_t *svc_find_by_id (int id);
|
|||||||
svc_t *svc_find_by_name (char *name);
|
svc_t *svc_find_by_name (char *name);
|
||||||
svc_t *svc_iterator (int restart);
|
svc_t *svc_iterator (int restart);
|
||||||
|
|
||||||
int svc_register (char *line);
|
int svc_register (char *line, char *username);
|
||||||
int svc_id_by_name (char *name);
|
int svc_id_by_name (char *name);
|
||||||
svc_cmd_t svc_enabled (svc_t *svc, int dynamic);
|
svc_cmd_t svc_enabled (svc_t *svc, int dynamic);
|
||||||
int svc_start (svc_t *svc);
|
int svc_start (svc_t *svc);
|
||||||
@@ -102,6 +105,7 @@ int svc_start_by_name (char *name);
|
|||||||
int svc_stop (svc_t *svc);
|
int svc_stop (svc_t *svc);
|
||||||
int svc_reload (svc_t *svc);
|
int svc_reload (svc_t *svc);
|
||||||
void svc_start_all (void);
|
void svc_start_all (void);
|
||||||
|
void svc_monitor (void);
|
||||||
|
|
||||||
#endif /* FINIT_SVC_H_ */
|
#endif /* FINIT_SVC_H_ */
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user