2022-02-24 11:22:26 +05:30
|
|
|
// Copyright 2022 Nicholas J. Kain <njkain at gmail dot com>
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/prctl.h>
|
|
|
|
#include <sys/stat.h>
|
2022-09-08 02:55:22 +05:30
|
|
|
#include <fcntl.h>
|
2022-02-24 11:22:26 +05:30
|
|
|
#include "nk/log.h"
|
|
|
|
#include "nk/io.h"
|
2022-08-09 23:10:10 +05:30
|
|
|
#include "nk/pspawn.h"
|
2022-02-24 11:22:26 +05:30
|
|
|
#include "scriptd.h"
|
|
|
|
#include "ndhc.h"
|
|
|
|
#include "sys.h"
|
|
|
|
|
2022-08-09 23:10:10 +05:30
|
|
|
extern char **environ;
|
2022-02-24 11:22:26 +05:30
|
|
|
bool valid_script_file = false;
|
|
|
|
|
|
|
|
// Runs the 'script_file'-specified script. Called from ndhc process.
|
2022-09-07 14:15:30 +05:30
|
|
|
// Blocks until the script finishes running.
|
2022-02-24 11:22:26 +05:30
|
|
|
void request_scriptd_run(void)
|
|
|
|
{
|
|
|
|
if (!valid_script_file) return;
|
|
|
|
|
2022-09-07 14:15:30 +05:30
|
|
|
char nl = '\n';
|
|
|
|
ssize_t r = safe_write(scriptdSock[0], &nl, 1);
|
2022-02-24 11:22:26 +05:30
|
|
|
if (r < 0 || (size_t)r != 1)
|
|
|
|
suicide("%s: (%s) write failed: %zd", client_config.interface,
|
|
|
|
__func__, r);
|
2022-09-07 14:15:30 +05:30
|
|
|
char buf[16];
|
2022-09-08 02:38:43 +05:30
|
|
|
r = safe_recv_once(scriptdSock[0], buf, sizeof buf, 0);
|
2022-09-07 14:15:30 +05:30
|
|
|
if (r == 0) {
|
|
|
|
// Remote end hung up.
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
} else if (r < 0) {
|
|
|
|
suicide("%s: (%s) recvmsg failed: %s", client_config.interface,
|
|
|
|
__func__, strerror(errno));
|
|
|
|
}
|
|
|
|
if (r != 1 || buf[0] != '+')
|
|
|
|
suicide("%s: Bad response from recv", __func__);
|
2022-02-24 11:22:26 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void process_client_socket(void)
|
|
|
|
{
|
|
|
|
static char buf[32];
|
|
|
|
static size_t buflen;
|
|
|
|
|
|
|
|
if (buflen == sizeof buf)
|
|
|
|
suicide("%s: (%s) receive buffer exhausted", client_config.interface,
|
|
|
|
__func__);
|
|
|
|
|
|
|
|
int r = safe_recv(scriptdSock[1], buf + buflen, sizeof buf - buflen,
|
|
|
|
MSG_DONTWAIT);
|
|
|
|
if (r == 0) {
|
|
|
|
// Remote end hung up.
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
} else if (r < 0) {
|
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK)
|
|
|
|
return;
|
|
|
|
suicide("%s: (%s) error reading from ndhc -> scriptd socket: %s",
|
|
|
|
client_config.interface, __func__, strerror(errno));
|
|
|
|
}
|
|
|
|
buflen += (size_t)r;
|
|
|
|
if (buflen > 1 || buf[0] != '\n') exit(EXIT_SUCCESS);
|
|
|
|
buflen = 0;
|
|
|
|
|
2022-09-07 14:15:30 +05:30
|
|
|
pid_t pid;
|
|
|
|
int ret = nk_pspawn(&pid, script_file, NULL, NULL, NULL, environ);
|
|
|
|
if (ret) log_line("posix_spawn failed for '%s': %s\n", script_file, strerror(ret));
|
|
|
|
int wstatus;
|
|
|
|
ret = waitpid(pid, &wstatus, 0);
|
|
|
|
if (ret == -1)
|
|
|
|
suicide("%s: (%s) waitpid failed: %s", client_config.interface,
|
|
|
|
__func__, strerror(errno));
|
|
|
|
|
|
|
|
char c = '+';
|
|
|
|
ssize_t rs = safe_write(scriptdSock[1], &c, 1);
|
|
|
|
if (rs == 0) {
|
|
|
|
// Remote end hung up.
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
} else if (rs < 0)
|
|
|
|
suicide("%s: (%s) error writing to scriptd -> ndhc socket: %s",
|
|
|
|
client_config.interface, __func__, strerror(errno));
|
2022-02-24 11:22:26 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void do_scriptd_work(void)
|
|
|
|
{
|
|
|
|
struct pollfd pfds[2] = {0};
|
|
|
|
pfds[0].fd = scriptdSock[1];
|
|
|
|
pfds[0].events = POLLIN|POLLHUP|POLLERR|POLLRDHUP;
|
|
|
|
pfds[1].fd = scriptdStream[1];
|
|
|
|
pfds[1].events = POLLHUP|POLLERR|POLLRDHUP;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (poll(pfds, 2, -1) < 0) {
|
|
|
|
if (errno != EINTR) suicide("poll failed");
|
|
|
|
}
|
|
|
|
if (pfds[0].revents & POLLIN) {
|
|
|
|
process_client_socket();
|
|
|
|
}
|
|
|
|
if (pfds[0].revents & (POLLHUP|POLLERR|POLLRDHUP)) {
|
|
|
|
suicide("scriptdSock closed unexpectedly");
|
|
|
|
}
|
|
|
|
if (pfds[1].revents & (POLLHUP|POLLERR|POLLRDHUP)) {
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void signal_handler(int signo)
|
|
|
|
{
|
2022-02-24 15:36:12 +05:30
|
|
|
int serrno = errno;
|
2022-09-07 14:15:30 +05:30
|
|
|
if (signo == SIGINT || signo == SIGTERM) {
|
2022-02-24 15:36:12 +05:30
|
|
|
_exit(EXIT_FAILURE);
|
2022-02-24 11:22:26 +05:30
|
|
|
}
|
2022-02-24 15:36:12 +05:30
|
|
|
errno = serrno;
|
2022-02-24 11:22:26 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void setup_signals_scriptd(void)
|
|
|
|
{
|
|
|
|
static const int ss[] = {
|
2022-09-07 14:15:30 +05:30
|
|
|
SIGINT, SIGTERM, SIGKILL
|
2022-02-24 11:22:26 +05:30
|
|
|
};
|
|
|
|
sigset_t mask;
|
|
|
|
if (sigprocmask(0, 0, &mask) < 0)
|
|
|
|
suicide("sigprocmask failed");
|
|
|
|
for (int i = 0; ss[i] != SIGKILL; ++i)
|
|
|
|
if (sigdelset(&mask, ss[i]))
|
|
|
|
suicide("sigdelset failed");
|
|
|
|
if (sigaddset(&mask, SIGPIPE))
|
|
|
|
suicide("sigaddset failed");
|
|
|
|
if (sigprocmask(SIG_SETMASK, &mask, (sigset_t *)0) < 0)
|
|
|
|
suicide("sigprocmask failed");
|
|
|
|
|
|
|
|
struct sigaction sa = {
|
|
|
|
.sa_handler = signal_handler,
|
2022-09-08 16:47:11 +05:30
|
|
|
.sa_flags = SA_RESTART,
|
2022-02-24 11:22:26 +05:30
|
|
|
};
|
|
|
|
if (sigemptyset(&sa.sa_mask))
|
|
|
|
suicide("sigemptyset failed");
|
|
|
|
for (int i = 0; ss[i] != SIGKILL; ++i)
|
|
|
|
if (sigaction(ss[i], &sa, NULL))
|
|
|
|
suicide("sigaction failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
void scriptd_main(void)
|
|
|
|
{
|
|
|
|
assert(valid_script_file);
|
|
|
|
prctl(PR_SET_NAME, "ndhc: scriptd");
|
|
|
|
umask(077);
|
|
|
|
setup_signals_scriptd();
|
2022-09-08 02:55:22 +05:30
|
|
|
fcntl(scriptdSock[1], F_SETFD, FD_CLOEXEC);
|
|
|
|
fcntl(scriptdStream[1], F_SETFD, FD_CLOEXEC);
|
2022-02-24 11:22:26 +05:30
|
|
|
do_scriptd_work();
|
|
|
|
}
|
|
|
|
|
|
|
|
|