-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscriptd.c
160 lines (144 loc) · 4.5 KB
/
scriptd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// 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>
#include <fcntl.h>
#include "nk/log.h"
#include "nk/io.h"
#include "nk/pspawn.h"
#include "scriptd.h"
#include "ndhc.h"
#include "sys.h"
extern char **environ;
bool valid_script_file = false;
// Runs the 'script_file'-specified script. Called from ndhc process.
// Blocks until the script finishes running.
void request_scriptd_run(void)
{
if (!valid_script_file) return;
char nl = '\n';
ssize_t r = safe_write(scriptdSock[0], &nl, 1);
if (r < 0 || (size_t)r != 1)
suicide("%s: (%s) write failed: %zd\n", client_config.interface,
__func__, r);
char buf[16];
r = safe_recv_once(scriptdSock[0], buf, sizeof buf, 0);
if (r == 0) {
// Remote end hung up.
exit(EXIT_SUCCESS);
} else if (r < 0) {
suicide("%s: (%s) recvmsg failed: %s\n", client_config.interface,
__func__, strerror(errno));
}
if (r != 1 || buf[0] != '+')
suicide("%s: Bad response from recv\n", __func__);
}
static void process_client_socket(void)
{
static char buf[32];
static size_t buflen;
if (buflen == sizeof buf)
suicide("%s: (%s) receive buffer exhausted\n", 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\n",
client_config.interface, __func__, strerror(errno));
}
buflen += (size_t)r;
if (buflen > 1 || buf[0] != '\n') exit(EXIT_SUCCESS);
buflen = 0;
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\n", 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\n",
client_config.interface, __func__, strerror(errno));
}
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\n");
}
if (pfds[0].revents & POLLIN) {
process_client_socket();
}
if (pfds[0].revents & (POLLHUP|POLLERR|POLLRDHUP)) {
suicide("scriptdSock closed unexpectedly\n");
}
if (pfds[1].revents & (POLLHUP|POLLERR|POLLRDHUP)) {
exit(EXIT_SUCCESS);
}
}
}
static void signal_handler(int signo)
{
int serrno = errno;
if (signo == SIGINT || signo == SIGTERM) {
_exit(EXIT_FAILURE);
}
errno = serrno;
}
static void setup_signals_scriptd(void)
{
static const int ss[] = {
SIGINT, SIGTERM, SIGPIPE, SIGKILL
};
sigset_t mask;
if (sigprocmask(0, 0, &mask) < 0)
suicide("sigprocmask failed\n");
for (int i = 0; ss[i] != SIGKILL; ++i)
if (sigdelset(&mask, ss[i]))
suicide("sigdelset failed\n");
if (sigprocmask(SIG_SETMASK, &mask, (sigset_t *)0) < 0)
suicide("sigprocmask failed\n");
struct sigaction sa = {
.sa_handler = signal_handler,
.sa_flags = SA_RESTART,
};
if (sigemptyset(&sa.sa_mask))
suicide("sigemptyset failed\n");
for (int i = 0; ss[i] != SIGKILL; ++i)
if (sigaction(ss[i], &sa, NULL))
suicide("sigaction failed\n");
}
void scriptd_main(void)
{
assert(valid_script_file);
prctl(PR_SET_NAME, "ndhc: scriptd");
umask(077);
setup_signals_scriptd();
fcntl(scriptdSock[1], F_SETFD, FD_CLOEXEC);
fcntl(scriptdStream[1], F_SETFD, FD_CLOEXEC);
do_scriptd_work();
}