Skip to content
pi-c Documentation
Login

Documentation

/* The monitor runner: see monitor.h. A command runs as /bin/sh's child in a session of its own,
 * under a wrapper shell that writes its own pid to SPOOL.pid first and the command's exit status
 * to SPOOL.exit last, so a pic that comes later can adopt it, or tell how it ended, with no parent
 * to ask. It holds none of its starter's descriptors: a lease is never inherited. */
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE 1
#endif
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>

#include "monitor.h"
#include "procs.h"

/* PATH's directory made (0700) when missing. */
static void mon_dir(const char *path) {
    char dir[4096];
    const char *slash = strrchr(path, '/');
    if (!slash || (size_t)(slash - path) >= sizeof dir) {
        return;
    }
    memcpy(dir, path, (size_t)(slash - path));
    dir[slash - path] = '\0';
    (void)mkdir(dir, 0700);
}

static int mon_start(const char *command, const char *cwd, const char *spool, long *pid, long long *token) {
    static const char wrapper[] = "echo $$ > \"$1.pid\"; (eval \"$2\") >> \"$1\" 2>&1 < /dev/null; echo $? > \"$1.exit\"";
    int fds[2];
    pid_t mid;
    long got = 0;
    *pid = 0;
    *token = 0;
    mon_dir(spool);
    if (pipe(fds) != 0) {
        return -1;
    }
    mid = fork();
    if (mid < 0) {
        close(fds[0]);
        close(fds[1]);
        return -1;
    }
    if (mid == 0) {
        pid_t run = fork();
        if (run != 0) { /* the command is nobody's child: no zombie, no wait */
            ssize_t n = run > 0 ? write(fds[1], &run, sizeof run) : 0;
            (void)n;
            _exit(0);
        }
        {
            const char *argv[] = {"/bin/sh", "-c", wrapper, "sh", spool, command, NULL};
            int null = open("/dev/null", O_RDWR), out = open(spool, O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC, 0600);
            (void)setsid(); /* a session and group of its own, which stop signals as one */
            if (null < 0 || out < 0 || (cwd && *cwd && chdir(cwd) != 0) || dup2(null, STDIN_FILENO) < 0 ||
                dup2(null, STDOUT_FILENO) < 0 || dup2(null, STDERR_FILENO) < 0) {
                _exit(127);
            }
            for (int fd = 3; fd < 1024; fd++) { /* none of the starter's pipes or locks */
                (void)close(fd);
            }
            execv("/bin/sh", (char *const *)(void *)argv);
            _exit(127);
        }
    }
    close(fds[1]);
    {
        pid_t run = 0;
        ssize_t n;
        while ((n = read(fds[0], &run, sizeof run)) < 0 && errno == EINTR) {
        }
        got = n == (ssize_t)sizeof run ? (long)run : 0;
    }
    close(fds[0]);
    while (waitpid(mid, NULL, 0) < 0 && errno == EINTR) {
    }
    if (got <= 0) {
        return -1;
    }
    *pid = got;
    *token = pic_proc_start_token(got);
    return 0;
}

static int mon_alive(long pid, long long token) { return pid > 0 && pic_proc_alive(pid, token); }

static long long mon_size(const char *spool) {
    struct stat st;
    return stat(spool, &st) == 0 ? (long long)st.st_size : -1;
}

static char *mon_read(const char *spool, long long from, long long to, size_t *len) {
    int fd = open(spool, O_RDONLY | O_CLOEXEC);
    char *buf;
    size_t want = to > from ? (size_t)(to - from) : 0, have = 0;
    *len = 0;
    if (fd < 0) {
        return NULL;
    }
    if (!(buf = malloc(want + 1))) {
        close(fd);
        return NULL;
    }
    while (have < want) {
        ssize_t n = pread(fd, buf + have, want - have, (off_t)(from + (long long)have));
        if (n < 0 && errno == EINTR) {
            continue;
        }
        if (n <= 0) {
            break;
        }
        have += (size_t)n;
    }
    close(fd);
    buf[have] = '\0';
    *len = have;
    return buf;
}

/* The number in PATH, or -1 when there is none. */
static long mon_number(const char *path) {
    char buf[32] = "", *end = NULL;
    int fd = open(path, O_RDONLY | O_CLOEXEC);
    ssize_t n;
    long v;
    if (fd < 0) {
        return -1;
    }
    n = read(fd, buf, sizeof buf - 1);
    close(fd);
    if (n <= 0) {
        return -1;
    }
    buf[n] = '\0';
    v = strtol(buf, &end, 10);
    return end == buf ? -1 : v;
}

static int mon_exit_status(const char *spool) {
    char path[4200];
    (void)snprintf(path, sizeof path, "%s.exit", spool);
    return (int)mon_number(path);
}

static long mon_pidfile(const char *spool) {
    char path[4200];
    (void)snprintf(path, sizeof path, "%s.pid", spool);
    return mon_number(path);
}

static void mon_stop(long pid, long long token) {
    if (mon_alive(pid, token)) {
        (void)kill(-(pid_t)pid, SIGTERM); /* its session's group: the wrapper and the command */
    }
}

const pic_wake_monitor_ops pic_monitor_ops = {mon_start, mon_alive, mon_size, mon_read, mon_exit_status, mon_pidfile, mon_stop,
                                              pic_proc_start_token};