Blame


1 c26f2460 2023-06-08 op /* $OpenBSD: proc.c,v 1.41 2021/12/04 06:52:58 florian Exp $ */
2 c26f2460 2023-06-08 op
3 c26f2460 2023-06-08 op /*
4 c26f2460 2023-06-08 op * Copyright (c) 2010 - 2016 Reyk Floeter <reyk@openbsd.org>
5 c26f2460 2023-06-08 op * Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org>
6 c26f2460 2023-06-08 op *
7 c26f2460 2023-06-08 op * Permission to use, copy, modify, and distribute this software for any
8 c26f2460 2023-06-08 op * purpose with or without fee is hereby granted, provided that the above
9 c26f2460 2023-06-08 op * copyright notice and this permission notice appear in all copies.
10 c26f2460 2023-06-08 op *
11 c26f2460 2023-06-08 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 c26f2460 2023-06-08 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 c26f2460 2023-06-08 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 c26f2460 2023-06-08 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 c26f2460 2023-06-08 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 c26f2460 2023-06-08 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 c26f2460 2023-06-08 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 c26f2460 2023-06-08 op */
19 c26f2460 2023-06-08 op
20 9b89eaeb 2023-06-08 op #include "gmid.h"
21 9b89eaeb 2023-06-08 op
22 c26f2460 2023-06-08 op #include <sys/types.h>
23 9b89eaeb 2023-06-08 op /* #include <sys/queue.h> XXX provided by gmid.h */
24 9b89eaeb 2023-06-08 op /* #include <sys/tree.h> XXX provided by gmid.h */
25 c26f2460 2023-06-08 op #include <sys/socket.h>
26 c26f2460 2023-06-08 op #include <sys/wait.h>
27 c26f2460 2023-06-08 op
28 c26f2460 2023-06-08 op #include <fcntl.h>
29 c26f2460 2023-06-08 op #include <grp.h>
30 c26f2460 2023-06-08 op #include <limits.h>
31 c26f2460 2023-06-08 op #include <stdio.h>
32 c26f2460 2023-06-08 op #include <stdlib.h>
33 c26f2460 2023-06-08 op #include <unistd.h>
34 c26f2460 2023-06-08 op #include <string.h>
35 c26f2460 2023-06-08 op #include <errno.h>
36 c26f2460 2023-06-08 op #include <signal.h>
37 c26f2460 2023-06-08 op #include <paths.h>
38 c26f2460 2023-06-08 op #include <pwd.h>
39 9b89eaeb 2023-06-08 op /* #include <event.h> XXX provided by gmid.h */
40 9b89eaeb 2023-06-08 op /* #include <imsg.h> XXX provided by gmid.h */
41 c26f2460 2023-06-08 op
42 c26f2460 2023-06-08 op #include "log.h"
43 c26f2460 2023-06-08 op #include "proc.h"
44 c26f2460 2023-06-08 op
45 c26f2460 2023-06-08 op void proc_exec(struct privsep *, struct privsep_proc *, unsigned int, int,
46 c26f2460 2023-06-08 op int, char **);
47 c26f2460 2023-06-08 op void proc_setup(struct privsep *, struct privsep_proc *, unsigned int);
48 c26f2460 2023-06-08 op void proc_open(struct privsep *, int, int);
49 c26f2460 2023-06-08 op void proc_accept(struct privsep *, int, enum privsep_procid,
50 c26f2460 2023-06-08 op unsigned int);
51 c26f2460 2023-06-08 op void proc_close(struct privsep *);
52 c26f2460 2023-06-08 op void proc_shutdown(struct privsep_proc *);
53 c26f2460 2023-06-08 op void proc_sig_handler(int, short, void *);
54 c26f2460 2023-06-08 op void proc_range(struct privsep *, enum privsep_procid, int *, int *);
55 c26f2460 2023-06-08 op int proc_dispatch_null(int, struct privsep_proc *, struct imsg *);
56 c26f2460 2023-06-08 op
57 c26f2460 2023-06-08 op enum privsep_procid
58 c26f2460 2023-06-08 op proc_getid(struct privsep_proc *procs, unsigned int nproc,
59 c26f2460 2023-06-08 op const char *proc_name)
60 c26f2460 2023-06-08 op {
61 c26f2460 2023-06-08 op struct privsep_proc *p;
62 c26f2460 2023-06-08 op unsigned int proc;
63 c26f2460 2023-06-08 op
64 c26f2460 2023-06-08 op for (proc = 0; proc < nproc; proc++) {
65 c26f2460 2023-06-08 op p = &procs[proc];
66 c26f2460 2023-06-08 op if (strcmp(p->p_title, proc_name))
67 c26f2460 2023-06-08 op continue;
68 c26f2460 2023-06-08 op
69 c26f2460 2023-06-08 op return (p->p_id);
70 c26f2460 2023-06-08 op }
71 c26f2460 2023-06-08 op
72 c26f2460 2023-06-08 op return (PROC_MAX);
73 c26f2460 2023-06-08 op }
74 c26f2460 2023-06-08 op
75 c26f2460 2023-06-08 op void
76 c26f2460 2023-06-08 op proc_exec(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
77 c26f2460 2023-06-08 op int debug, int argc, char **argv)
78 c26f2460 2023-06-08 op {
79 7fff8aa6 2023-06-09 op unsigned int proc, nargc, i, proc_i, proc_X = 0;
80 c26f2460 2023-06-08 op const char **nargv;
81 c26f2460 2023-06-08 op struct privsep_proc *p;
82 c26f2460 2023-06-08 op char num[32];
83 c26f2460 2023-06-08 op int fd;
84 c26f2460 2023-06-08 op
85 c26f2460 2023-06-08 op /* Prepare the new process argv. */
86 7fff8aa6 2023-06-09 op nargv = calloc(argc + 9, sizeof(char *));
87 c26f2460 2023-06-08 op if (nargv == NULL)
88 c26f2460 2023-06-08 op fatal("%s: calloc", __func__);
89 c26f2460 2023-06-08 op
90 c26f2460 2023-06-08 op /* Copy call argument first. */
91 c26f2460 2023-06-08 op nargc = 0;
92 c26f2460 2023-06-08 op nargv[nargc++] = argv[0];
93 c26f2460 2023-06-08 op
94 c26f2460 2023-06-08 op /* Set process name argument and save the position. */
95 c26f2460 2023-06-08 op nargv[nargc++] = "-T";
96 c26f2460 2023-06-08 op proc_i = nargc;
97 c26f2460 2023-06-08 op nargc++;
98 7fff8aa6 2023-06-09 op
99 7fff8aa6 2023-06-09 op /* Set user and chroot */
100 7fff8aa6 2023-06-09 op if (ps->ps_pw != NULL) {
101 7fff8aa6 2023-06-09 op nargv[nargc++] = "-U";
102 7fff8aa6 2023-06-09 op nargv[nargc++] = ps->ps_pw->pw_name;
103 7fff8aa6 2023-06-09 op
104 7fff8aa6 2023-06-09 op nargv[nargc++] = "-X";
105 7fff8aa6 2023-06-09 op proc_X = nargc;
106 7fff8aa6 2023-06-09 op nargc++;
107 7fff8aa6 2023-06-09 op }
108 c26f2460 2023-06-08 op
109 c26f2460 2023-06-08 op /* Point process instance arg to stack and copy the original args. */
110 c26f2460 2023-06-08 op nargv[nargc++] = "-I";
111 c26f2460 2023-06-08 op nargv[nargc++] = num;
112 c26f2460 2023-06-08 op for (i = 1; i < (unsigned int) argc; i++)
113 c26f2460 2023-06-08 op nargv[nargc++] = argv[i];
114 c26f2460 2023-06-08 op
115 c26f2460 2023-06-08 op nargv[nargc] = NULL;
116 c26f2460 2023-06-08 op
117 c26f2460 2023-06-08 op for (proc = 0; proc < nproc; proc++) {
118 c26f2460 2023-06-08 op p = &procs[proc];
119 c26f2460 2023-06-08 op
120 7fff8aa6 2023-06-09 op /* Update args with process title and chroot. */
121 c26f2460 2023-06-08 op nargv[proc_i] = (char *)(uintptr_t)p->p_title;
122 a452496a 2023-06-24 op if (proc_X)
123 7fff8aa6 2023-06-09 op nargv[proc_X] = p->p_chroot;
124 c26f2460 2023-06-08 op
125 c26f2460 2023-06-08 op /* Fire children processes. */
126 c26f2460 2023-06-08 op for (i = 0; i < ps->ps_instances[p->p_id]; i++) {
127 c26f2460 2023-06-08 op /* Update the process instance number. */
128 c26f2460 2023-06-08 op snprintf(num, sizeof(num), "%u", i);
129 c26f2460 2023-06-08 op
130 c26f2460 2023-06-08 op fd = ps->ps_pipes[p->p_id][i].pp_pipes[PROC_PARENT][0];
131 c26f2460 2023-06-08 op ps->ps_pipes[p->p_id][i].pp_pipes[PROC_PARENT][0] = -1;
132 c26f2460 2023-06-08 op
133 c26f2460 2023-06-08 op switch (fork()) {
134 c26f2460 2023-06-08 op case -1:
135 c26f2460 2023-06-08 op fatal("%s: fork", __func__);
136 c26f2460 2023-06-08 op break;
137 c26f2460 2023-06-08 op case 0:
138 c26f2460 2023-06-08 op /* First create a new session */
139 c26f2460 2023-06-08 op if (setsid() == -1)
140 c26f2460 2023-06-08 op fatal("setsid");
141 c26f2460 2023-06-08 op
142 c26f2460 2023-06-08 op /* Prepare parent socket. */
143 c26f2460 2023-06-08 op if (fd != PROC_PARENT_SOCK_FILENO) {
144 c26f2460 2023-06-08 op if (dup2(fd, PROC_PARENT_SOCK_FILENO)
145 c26f2460 2023-06-08 op == -1)
146 c26f2460 2023-06-08 op fatal("dup2");
147 c26f2460 2023-06-08 op } else if (fcntl(fd, F_SETFD, 0) == -1)
148 c26f2460 2023-06-08 op fatal("fcntl");
149 c26f2460 2023-06-08 op
150 c26f2460 2023-06-08 op /* Daemons detach from terminal. */
151 c26f2460 2023-06-08 op if (!debug && (fd =
152 c26f2460 2023-06-08 op open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
153 c26f2460 2023-06-08 op (void)dup2(fd, STDIN_FILENO);
154 c26f2460 2023-06-08 op (void)dup2(fd, STDOUT_FILENO);
155 c26f2460 2023-06-08 op (void)dup2(fd, STDERR_FILENO);
156 c26f2460 2023-06-08 op if (fd > 2)
157 c26f2460 2023-06-08 op (void)close(fd);
158 c26f2460 2023-06-08 op }
159 c26f2460 2023-06-08 op
160 c26f2460 2023-06-08 op /* obnoxious casts */
161 c26f2460 2023-06-08 op execvp(argv[0], (char *const *)nargv);
162 c26f2460 2023-06-08 op fatal("%s: execvp", __func__);
163 c26f2460 2023-06-08 op break;
164 c26f2460 2023-06-08 op default:
165 c26f2460 2023-06-08 op /* Close child end. */
166 c26f2460 2023-06-08 op close(fd);
167 c26f2460 2023-06-08 op break;
168 c26f2460 2023-06-08 op }
169 c26f2460 2023-06-08 op }
170 c26f2460 2023-06-08 op }
171 c26f2460 2023-06-08 op free(nargv);
172 c26f2460 2023-06-08 op }
173 c26f2460 2023-06-08 op
174 c26f2460 2023-06-08 op void
175 c26f2460 2023-06-08 op proc_connect(struct privsep *ps)
176 c26f2460 2023-06-08 op {
177 c26f2460 2023-06-08 op struct imsgev *iev;
178 c26f2460 2023-06-08 op unsigned int src, dst, inst;
179 c26f2460 2023-06-08 op
180 c26f2460 2023-06-08 op /* Don't distribute any sockets if we are not really going to run. */
181 c26f2460 2023-06-08 op if (ps->ps_noaction)
182 c26f2460 2023-06-08 op return;
183 c26f2460 2023-06-08 op
184 c26f2460 2023-06-08 op for (dst = 0; dst < PROC_MAX; dst++) {
185 c26f2460 2023-06-08 op /* We don't communicate with ourselves. */
186 c26f2460 2023-06-08 op if (dst == PROC_PARENT)
187 c26f2460 2023-06-08 op continue;
188 c26f2460 2023-06-08 op
189 c26f2460 2023-06-08 op for (inst = 0; inst < ps->ps_instances[dst]; inst++) {
190 c26f2460 2023-06-08 op iev = &ps->ps_ievs[dst][inst];
191 c26f2460 2023-06-08 op imsg_init(&iev->ibuf, ps->ps_pp->pp_pipes[dst][inst]);
192 c26f2460 2023-06-08 op event_set(&iev->ev, iev->ibuf.fd, iev->events,
193 c26f2460 2023-06-08 op iev->handler, iev->data);
194 c26f2460 2023-06-08 op event_add(&iev->ev, NULL);
195 c26f2460 2023-06-08 op }
196 c26f2460 2023-06-08 op }
197 c26f2460 2023-06-08 op
198 c26f2460 2023-06-08 op /* Distribute the socketpair()s for everyone. */
199 c26f2460 2023-06-08 op for (src = 0; src < PROC_MAX; src++)
200 c26f2460 2023-06-08 op for (dst = src; dst < PROC_MAX; dst++) {
201 c26f2460 2023-06-08 op /* Parent already distributed its fds. */
202 c26f2460 2023-06-08 op if (src == PROC_PARENT || dst == PROC_PARENT)
203 c26f2460 2023-06-08 op continue;
204 c26f2460 2023-06-08 op
205 c26f2460 2023-06-08 op proc_open(ps, src, dst);
206 c26f2460 2023-06-08 op }
207 c26f2460 2023-06-08 op }
208 c26f2460 2023-06-08 op
209 c26f2460 2023-06-08 op void
210 c26f2460 2023-06-08 op proc_init(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
211 c26f2460 2023-06-08 op int debug, int argc, char **argv, enum privsep_procid proc_id)
212 c26f2460 2023-06-08 op {
213 c26f2460 2023-06-08 op struct privsep_proc *p = NULL;
214 c26f2460 2023-06-08 op struct privsep_pipes *pa, *pb;
215 c26f2460 2023-06-08 op unsigned int proc;
216 c26f2460 2023-06-08 op unsigned int dst;
217 c26f2460 2023-06-08 op int fds[2];
218 c26f2460 2023-06-08 op
219 c26f2460 2023-06-08 op /* Don't initiate anything if we are not really going to run. */
220 c26f2460 2023-06-08 op if (ps->ps_noaction)
221 c26f2460 2023-06-08 op return;
222 c26f2460 2023-06-08 op
223 c26f2460 2023-06-08 op if (proc_id == PROC_PARENT) {
224 c26f2460 2023-06-08 op privsep_process = PROC_PARENT;
225 c26f2460 2023-06-08 op proc_setup(ps, procs, nproc);
226 c26f2460 2023-06-08 op
227 c26f2460 2023-06-08 op /*
228 c26f2460 2023-06-08 op * Create the children sockets so we can use them
229 c26f2460 2023-06-08 op * to distribute the rest of the socketpair()s using
230 c26f2460 2023-06-08 op * proc_connect() later.
231 c26f2460 2023-06-08 op */
232 c26f2460 2023-06-08 op for (dst = 0; dst < PROC_MAX; dst++) {
233 c26f2460 2023-06-08 op /* Don't create socket for ourselves. */
234 c26f2460 2023-06-08 op if (dst == PROC_PARENT)
235 c26f2460 2023-06-08 op continue;
236 c26f2460 2023-06-08 op
237 c26f2460 2023-06-08 op for (proc = 0; proc < ps->ps_instances[dst]; proc++) {
238 c26f2460 2023-06-08 op pa = &ps->ps_pipes[PROC_PARENT][0];
239 c26f2460 2023-06-08 op pb = &ps->ps_pipes[dst][proc];
240 c26f2460 2023-06-08 op if (socketpair(AF_UNIX,
241 1b9031f1 2023-06-13 op SOCK_STREAM,
242 c26f2460 2023-06-08 op PF_UNSPEC, fds) == -1)
243 c26f2460 2023-06-08 op fatal("%s: socketpair", __func__);
244 1b9031f1 2023-06-13 op
245 1b9031f1 2023-06-13 op mark_nonblock(fds[0]);
246 1b9031f1 2023-06-13 op mark_nonblock(fds[1]);
247 1b9031f1 2023-06-13 op if (fcntl(fds[0], F_SETFD, FD_CLOEXEC) == -1 ||
248 1b9031f1 2023-06-13 op fcntl(fds[1], F_SETFD, FD_CLOEXEC) == -1)
249 1b9031f1 2023-06-13 op fatal("%s: fcntl F_SETFD", __func__);
250 c26f2460 2023-06-08 op
251 c26f2460 2023-06-08 op pa->pp_pipes[dst][proc] = fds[0];
252 c26f2460 2023-06-08 op pb->pp_pipes[PROC_PARENT][0] = fds[1];
253 c26f2460 2023-06-08 op }
254 c26f2460 2023-06-08 op }
255 c26f2460 2023-06-08 op
256 c26f2460 2023-06-08 op /* Engage! */
257 c26f2460 2023-06-08 op proc_exec(ps, procs, nproc, debug, argc, argv);
258 c26f2460 2023-06-08 op return;
259 c26f2460 2023-06-08 op }
260 c26f2460 2023-06-08 op
261 c26f2460 2023-06-08 op /* Initialize a child */
262 c26f2460 2023-06-08 op for (proc = 0; proc < nproc; proc++) {
263 c26f2460 2023-06-08 op if (procs[proc].p_id != proc_id)
264 c26f2460 2023-06-08 op continue;
265 c26f2460 2023-06-08 op p = &procs[proc];
266 c26f2460 2023-06-08 op break;
267 c26f2460 2023-06-08 op }
268 c26f2460 2023-06-08 op if (p == NULL || p->p_init == NULL)
269 c26f2460 2023-06-08 op fatalx("%s: process %d missing process initialization",
270 c26f2460 2023-06-08 op __func__, proc_id);
271 c26f2460 2023-06-08 op
272 c26f2460 2023-06-08 op p->p_init(ps, p);
273 c26f2460 2023-06-08 op
274 c26f2460 2023-06-08 op fatalx("failed to initiate child process");
275 c26f2460 2023-06-08 op }
276 c26f2460 2023-06-08 op
277 c26f2460 2023-06-08 op void
278 c26f2460 2023-06-08 op proc_accept(struct privsep *ps, int fd, enum privsep_procid dst,
279 c26f2460 2023-06-08 op unsigned int n)
280 c26f2460 2023-06-08 op {
281 c26f2460 2023-06-08 op struct privsep_pipes *pp = ps->ps_pp;
282 c26f2460 2023-06-08 op struct imsgev *iev;
283 c26f2460 2023-06-08 op
284 c26f2460 2023-06-08 op if (ps->ps_ievs[dst] == NULL) {
285 c26f2460 2023-06-08 op #if DEBUG > 1
286 c26f2460 2023-06-08 op log_debug("%s: %s src %d %d to dst %d %d not connected",
287 c26f2460 2023-06-08 op __func__, ps->ps_title[privsep_process],
288 c26f2460 2023-06-08 op privsep_process, ps->ps_instance + 1,
289 c26f2460 2023-06-08 op dst, n + 1);
290 c26f2460 2023-06-08 op #endif
291 c26f2460 2023-06-08 op close(fd);
292 c26f2460 2023-06-08 op return;
293 c26f2460 2023-06-08 op }
294 c26f2460 2023-06-08 op
295 c26f2460 2023-06-08 op if (pp->pp_pipes[dst][n] != -1) {
296 c26f2460 2023-06-08 op log_warnx("%s: duplicated descriptor", __func__);
297 c26f2460 2023-06-08 op close(fd);
298 c26f2460 2023-06-08 op return;
299 c26f2460 2023-06-08 op } else
300 c26f2460 2023-06-08 op pp->pp_pipes[dst][n] = fd;
301 c26f2460 2023-06-08 op
302 c26f2460 2023-06-08 op iev = &ps->ps_ievs[dst][n];
303 c26f2460 2023-06-08 op imsg_init(&iev->ibuf, fd);
304 c26f2460 2023-06-08 op event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
305 c26f2460 2023-06-08 op event_add(&iev->ev, NULL);
306 c26f2460 2023-06-08 op }
307 c26f2460 2023-06-08 op
308 c26f2460 2023-06-08 op void
309 c26f2460 2023-06-08 op proc_setup(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc)
310 c26f2460 2023-06-08 op {
311 c26f2460 2023-06-08 op unsigned int i, j, src, dst, id;
312 c26f2460 2023-06-08 op struct privsep_pipes *pp;
313 c26f2460 2023-06-08 op
314 c26f2460 2023-06-08 op /* Initialize parent title, ps_instances and procs. */
315 c26f2460 2023-06-08 op ps->ps_title[PROC_PARENT] = "parent";
316 c26f2460 2023-06-08 op
317 c26f2460 2023-06-08 op for (src = 0; src < PROC_MAX; src++)
318 c26f2460 2023-06-08 op /* Default to 1 process instance */
319 c26f2460 2023-06-08 op if (ps->ps_instances[src] < 1)
320 c26f2460 2023-06-08 op ps->ps_instances[src] = 1;
321 c26f2460 2023-06-08 op
322 c26f2460 2023-06-08 op for (src = 0; src < nproc; src++) {
323 c26f2460 2023-06-08 op procs[src].p_ps = ps;
324 c26f2460 2023-06-08 op if (procs[src].p_cb == NULL)
325 c26f2460 2023-06-08 op procs[src].p_cb = proc_dispatch_null;
326 c26f2460 2023-06-08 op
327 c26f2460 2023-06-08 op id = procs[src].p_id;
328 c26f2460 2023-06-08 op ps->ps_title[id] = procs[src].p_title;
329 c26f2460 2023-06-08 op if ((ps->ps_ievs[id] = calloc(ps->ps_instances[id],
330 c26f2460 2023-06-08 op sizeof(struct imsgev))) == NULL)
331 c26f2460 2023-06-08 op fatal("%s: calloc", __func__);
332 c26f2460 2023-06-08 op
333 c26f2460 2023-06-08 op /* With this set up, we are ready to call imsg_init(). */
334 c26f2460 2023-06-08 op for (i = 0; i < ps->ps_instances[id]; i++) {
335 c26f2460 2023-06-08 op ps->ps_ievs[id][i].handler = proc_dispatch;
336 c26f2460 2023-06-08 op ps->ps_ievs[id][i].events = EV_READ;
337 c26f2460 2023-06-08 op ps->ps_ievs[id][i].proc = &procs[src];
338 c26f2460 2023-06-08 op ps->ps_ievs[id][i].data = &ps->ps_ievs[id][i];
339 c26f2460 2023-06-08 op }
340 c26f2460 2023-06-08 op }
341 c26f2460 2023-06-08 op
342 c26f2460 2023-06-08 op /*
343 c26f2460 2023-06-08 op * Allocate pipes for all process instances (incl. parent)
344 c26f2460 2023-06-08 op *
345 c26f2460 2023-06-08 op * - ps->ps_pipes: N:M mapping
346 c26f2460 2023-06-08 op * N source processes connected to M destination processes:
347 c26f2460 2023-06-08 op * [src][instances][dst][instances], for example
348 c26f2460 2023-06-08 op * [PROC_RELAY][3][PROC_CA][3]
349 c26f2460 2023-06-08 op *
350 c26f2460 2023-06-08 op * - ps->ps_pp: per-process 1:M part of ps->ps_pipes
351 c26f2460 2023-06-08 op * Each process instance has a destination array of socketpair fds:
352 c26f2460 2023-06-08 op * [dst][instances], for example
353 c26f2460 2023-06-08 op * [PROC_PARENT][0]
354 c26f2460 2023-06-08 op */
355 c26f2460 2023-06-08 op for (src = 0; src < PROC_MAX; src++) {
356 c26f2460 2023-06-08 op /* Allocate destination array for each process */
357 c26f2460 2023-06-08 op if ((ps->ps_pipes[src] = calloc(ps->ps_instances[src],
358 c26f2460 2023-06-08 op sizeof(struct privsep_pipes))) == NULL)
359 c26f2460 2023-06-08 op fatal("%s: calloc", __func__);
360 c26f2460 2023-06-08 op
361 c26f2460 2023-06-08 op for (i = 0; i < ps->ps_instances[src]; i++) {
362 c26f2460 2023-06-08 op pp = &ps->ps_pipes[src][i];
363 c26f2460 2023-06-08 op
364 c26f2460 2023-06-08 op for (dst = 0; dst < PROC_MAX; dst++) {
365 c26f2460 2023-06-08 op /* Allocate maximum fd integers */
366 c26f2460 2023-06-08 op if ((pp->pp_pipes[dst] =
367 c26f2460 2023-06-08 op calloc(ps->ps_instances[dst],
368 c26f2460 2023-06-08 op sizeof(int))) == NULL)
369 c26f2460 2023-06-08 op fatal("%s: calloc", __func__);
370 c26f2460 2023-06-08 op
371 c26f2460 2023-06-08 op /* Mark fd as unused */
372 c26f2460 2023-06-08 op for (j = 0; j < ps->ps_instances[dst]; j++)
373 c26f2460 2023-06-08 op pp->pp_pipes[dst][j] = -1;
374 c26f2460 2023-06-08 op }
375 c26f2460 2023-06-08 op }
376 c26f2460 2023-06-08 op }
377 c26f2460 2023-06-08 op
378 c26f2460 2023-06-08 op ps->ps_pp = &ps->ps_pipes[privsep_process][ps->ps_instance];
379 c26f2460 2023-06-08 op }
380 c26f2460 2023-06-08 op
381 c26f2460 2023-06-08 op void
382 c26f2460 2023-06-08 op proc_kill(struct privsep *ps)
383 c26f2460 2023-06-08 op {
384 c26f2460 2023-06-08 op char *cause;
385 c26f2460 2023-06-08 op pid_t pid;
386 c26f2460 2023-06-08 op int len, status;
387 c26f2460 2023-06-08 op
388 c26f2460 2023-06-08 op if (privsep_process != PROC_PARENT)
389 c26f2460 2023-06-08 op return;
390 c26f2460 2023-06-08 op
391 c26f2460 2023-06-08 op proc_close(ps);
392 c26f2460 2023-06-08 op
393 c26f2460 2023-06-08 op do {
394 c26f2460 2023-06-08 op pid = waitpid(WAIT_ANY, &status, 0);
395 c26f2460 2023-06-08 op if (pid <= 0)
396 c26f2460 2023-06-08 op continue;
397 c26f2460 2023-06-08 op
398 c26f2460 2023-06-08 op if (WIFSIGNALED(status)) {
399 c26f2460 2023-06-08 op len = asprintf(&cause, "terminated; signal %d",
400 c26f2460 2023-06-08 op WTERMSIG(status));
401 c26f2460 2023-06-08 op } else if (WIFEXITED(status)) {
402 c26f2460 2023-06-08 op if (WEXITSTATUS(status) != 0)
403 c26f2460 2023-06-08 op len = asprintf(&cause, "exited abnormally");
404 c26f2460 2023-06-08 op else
405 c26f2460 2023-06-08 op len = 0;
406 c26f2460 2023-06-08 op } else
407 c26f2460 2023-06-08 op len = -1;
408 c26f2460 2023-06-08 op
409 c26f2460 2023-06-08 op if (len == 0) {
410 c26f2460 2023-06-08 op /* child exited OK, don't print a warning message */
411 c26f2460 2023-06-08 op } else if (len != -1) {
412 c26f2460 2023-06-08 op log_warnx("lost child: pid %u %s", pid, cause);
413 c26f2460 2023-06-08 op free(cause);
414 c26f2460 2023-06-08 op } else
415 c26f2460 2023-06-08 op log_warnx("lost child: pid %u", pid);
416 c26f2460 2023-06-08 op } while (pid != -1 || errno == EINTR);
417 c26f2460 2023-06-08 op }
418 c26f2460 2023-06-08 op
419 c26f2460 2023-06-08 op void
420 c26f2460 2023-06-08 op proc_open(struct privsep *ps, int src, int dst)
421 c26f2460 2023-06-08 op {
422 c26f2460 2023-06-08 op struct privsep_pipes *pa, *pb;
423 c26f2460 2023-06-08 op struct privsep_fd pf;
424 c26f2460 2023-06-08 op int fds[2];
425 c26f2460 2023-06-08 op unsigned int i, j;
426 c26f2460 2023-06-08 op
427 c26f2460 2023-06-08 op /* Exchange pipes between process. */
428 c26f2460 2023-06-08 op for (i = 0; i < ps->ps_instances[src]; i++) {
429 c26f2460 2023-06-08 op for (j = 0; j < ps->ps_instances[dst]; j++) {
430 c26f2460 2023-06-08 op /* Don't create sockets for ourself. */
431 c26f2460 2023-06-08 op if (src == dst && i == j)
432 c26f2460 2023-06-08 op continue;
433 c26f2460 2023-06-08 op
434 c26f2460 2023-06-08 op /* Servers don't talk to each other. */
435 c26f2460 2023-06-08 op if (src == PROC_SERVER && dst == PROC_SERVER)
436 c26f2460 2023-06-08 op continue;
437 c26f2460 2023-06-08 op
438 c26f2460 2023-06-08 op pa = &ps->ps_pipes[src][i];
439 c26f2460 2023-06-08 op pb = &ps->ps_pipes[dst][j];
440 c26f2460 2023-06-08 op if (socketpair(AF_UNIX,
441 1b9031f1 2023-06-13 op SOCK_STREAM,
442 c26f2460 2023-06-08 op PF_UNSPEC, fds) == -1)
443 c26f2460 2023-06-08 op fatal("%s: socketpair", __func__);
444 1b9031f1 2023-06-13 op
445 1b9031f1 2023-06-13 op mark_nonblock(fds[0]);
446 1b9031f1 2023-06-13 op mark_nonblock(fds[1]);
447 1b9031f1 2023-06-13 op if (fcntl(fds[0], F_SETFD, FD_CLOEXEC) == -1 ||
448 1b9031f1 2023-06-13 op fcntl(fds[1], F_SETFD, FD_CLOEXEC) == -1)
449 1b9031f1 2023-06-13 op fatal("%s: fcntl F_SETFD", __func__);
450 c26f2460 2023-06-08 op
451 c26f2460 2023-06-08 op pa->pp_pipes[dst][j] = fds[0];
452 c26f2460 2023-06-08 op pb->pp_pipes[src][i] = fds[1];
453 c26f2460 2023-06-08 op
454 c26f2460 2023-06-08 op pf.pf_procid = src;
455 c26f2460 2023-06-08 op pf.pf_instance = i;
456 c26f2460 2023-06-08 op if (proc_compose_imsg(ps, dst, j, IMSG_CTL_PROCFD,
457 c26f2460 2023-06-08 op -1, pb->pp_pipes[src][i], &pf, sizeof(pf)) == -1)
458 c26f2460 2023-06-08 op fatal("%s: proc_compose_imsg", __func__);
459 c26f2460 2023-06-08 op
460 c26f2460 2023-06-08 op pf.pf_procid = dst;
461 c26f2460 2023-06-08 op pf.pf_instance = j;
462 c26f2460 2023-06-08 op if (proc_compose_imsg(ps, src, i, IMSG_CTL_PROCFD,
463 c26f2460 2023-06-08 op -1, pa->pp_pipes[dst][j], &pf, sizeof(pf)) == -1)
464 c26f2460 2023-06-08 op fatal("%s: proc_compose_imsg", __func__);
465 c26f2460 2023-06-08 op
466 c26f2460 2023-06-08 op /*
467 c26f2460 2023-06-08 op * We have to flush to send the descriptors and close
468 c26f2460 2023-06-08 op * them to avoid the fd ramp on startup.
469 c26f2460 2023-06-08 op */
470 c26f2460 2023-06-08 op if (proc_flush_imsg(ps, src, i) == -1 ||
471 c26f2460 2023-06-08 op proc_flush_imsg(ps, dst, j) == -1)
472 c26f2460 2023-06-08 op fatal("%s: imsg_flush", __func__);
473 c26f2460 2023-06-08 op }
474 c26f2460 2023-06-08 op }
475 c26f2460 2023-06-08 op }
476 c26f2460 2023-06-08 op
477 c26f2460 2023-06-08 op void
478 c26f2460 2023-06-08 op proc_close(struct privsep *ps)
479 c26f2460 2023-06-08 op {
480 c26f2460 2023-06-08 op unsigned int dst, n;
481 c26f2460 2023-06-08 op struct privsep_pipes *pp;
482 c26f2460 2023-06-08 op
483 c26f2460 2023-06-08 op if (ps == NULL)
484 c26f2460 2023-06-08 op return;
485 c26f2460 2023-06-08 op
486 c26f2460 2023-06-08 op pp = ps->ps_pp;
487 c26f2460 2023-06-08 op
488 c26f2460 2023-06-08 op for (dst = 0; dst < PROC_MAX; dst++) {
489 c26f2460 2023-06-08 op if (ps->ps_ievs[dst] == NULL)
490 c26f2460 2023-06-08 op continue;
491 c26f2460 2023-06-08 op
492 c26f2460 2023-06-08 op for (n = 0; n < ps->ps_instances[dst]; n++) {
493 c26f2460 2023-06-08 op if (pp->pp_pipes[dst][n] == -1)
494 c26f2460 2023-06-08 op continue;
495 c26f2460 2023-06-08 op
496 c26f2460 2023-06-08 op /* Cancel the fd, close and invalidate the fd */
497 c26f2460 2023-06-08 op event_del(&(ps->ps_ievs[dst][n].ev));
498 c26f2460 2023-06-08 op imsg_clear(&(ps->ps_ievs[dst][n].ibuf));
499 c26f2460 2023-06-08 op close(pp->pp_pipes[dst][n]);
500 c26f2460 2023-06-08 op pp->pp_pipes[dst][n] = -1;
501 c26f2460 2023-06-08 op }
502 c26f2460 2023-06-08 op free(ps->ps_ievs[dst]);
503 c26f2460 2023-06-08 op }
504 c26f2460 2023-06-08 op }
505 c26f2460 2023-06-08 op
506 c26f2460 2023-06-08 op void
507 c26f2460 2023-06-08 op proc_shutdown(struct privsep_proc *p)
508 c26f2460 2023-06-08 op {
509 c26f2460 2023-06-08 op struct privsep *ps = p->p_ps;
510 c26f2460 2023-06-08 op
511 c26f2460 2023-06-08 op if (p->p_shutdown != NULL)
512 c26f2460 2023-06-08 op (*p->p_shutdown)();
513 c26f2460 2023-06-08 op
514 c26f2460 2023-06-08 op proc_close(ps);
515 c26f2460 2023-06-08 op
516 c26f2460 2023-06-08 op log_info("%s exiting, pid %d", p->p_title, getpid());
517 c26f2460 2023-06-08 op
518 c26f2460 2023-06-08 op exit(0);
519 c26f2460 2023-06-08 op }
520 c26f2460 2023-06-08 op
521 c26f2460 2023-06-08 op void
522 c26f2460 2023-06-08 op proc_sig_handler(int sig, short event, void *arg)
523 c26f2460 2023-06-08 op {
524 c26f2460 2023-06-08 op struct privsep_proc *p = arg;
525 c26f2460 2023-06-08 op
526 c26f2460 2023-06-08 op switch (sig) {
527 c26f2460 2023-06-08 op case SIGINT:
528 c26f2460 2023-06-08 op case SIGTERM:
529 c26f2460 2023-06-08 op proc_shutdown(p);
530 c26f2460 2023-06-08 op break;
531 c26f2460 2023-06-08 op case SIGCHLD:
532 c26f2460 2023-06-08 op case SIGHUP:
533 c26f2460 2023-06-08 op /* ignore */
534 c26f2460 2023-06-08 op break;
535 c26f2460 2023-06-08 op default:
536 c26f2460 2023-06-08 op fatalx("%s: unexpected signal", __func__);
537 c26f2460 2023-06-08 op /* NOTREACHED */
538 c26f2460 2023-06-08 op }
539 c26f2460 2023-06-08 op }
540 c26f2460 2023-06-08 op
541 c26f2460 2023-06-08 op void
542 c26f2460 2023-06-08 op proc_run(struct privsep *ps, struct privsep_proc *p,
543 c26f2460 2023-06-08 op struct privsep_proc *procs, unsigned int nproc,
544 c26f2460 2023-06-08 op void (*run)(struct privsep *, struct privsep_proc *, void *), void *arg)
545 c26f2460 2023-06-08 op {
546 c26f2460 2023-06-08 op struct passwd *pw;
547 c26f2460 2023-06-08 op const char *root;
548 c26f2460 2023-06-08 op
549 c26f2460 2023-06-08 op log_procinit(p->p_title);
550 725457a9 2023-06-11 op setproctitle("%s", p->p_title);
551 c26f2460 2023-06-08 op
552 725457a9 2023-06-11 op privsep_process = p->p_id;
553 725457a9 2023-06-11 op
554 c26f2460 2023-06-08 op if (ps->ps_pw == NULL)
555 c26f2460 2023-06-08 op goto init;
556 c26f2460 2023-06-08 op
557 c26f2460 2023-06-08 op /* Set the process group of the current process */
558 c26f2460 2023-06-08 op setpgid(0, 0);
559 c26f2460 2023-06-08 op
560 c26f2460 2023-06-08 op /* Use non-standard user */
561 c26f2460 2023-06-08 op if (p->p_pw != NULL)
562 c26f2460 2023-06-08 op pw = p->p_pw;
563 c26f2460 2023-06-08 op else
564 c26f2460 2023-06-08 op pw = ps->ps_pw;
565 c26f2460 2023-06-08 op
566 c26f2460 2023-06-08 op /* Change root directory */
567 c26f2460 2023-06-08 op if (p->p_chroot != NULL)
568 c26f2460 2023-06-08 op root = p->p_chroot;
569 c26f2460 2023-06-08 op else
570 c26f2460 2023-06-08 op root = pw->pw_dir;
571 c26f2460 2023-06-08 op
572 c26f2460 2023-06-08 op if (chroot(root) == -1)
573 c26f2460 2023-06-08 op fatal("%s: chroot", __func__);
574 c26f2460 2023-06-08 op if (chdir("/") == -1)
575 c26f2460 2023-06-08 op fatal("%s: chdir(\"/\")", __func__);
576 c26f2460 2023-06-08 op
577 c26f2460 2023-06-08 op if (setgroups(1, &pw->pw_gid) ||
578 c26f2460 2023-06-08 op setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
579 c26f2460 2023-06-08 op setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
580 c26f2460 2023-06-08 op fatal("%s: cannot drop privileges", __func__);
581 c26f2460 2023-06-08 op
582 c26f2460 2023-06-08 op init:
583 c26f2460 2023-06-08 op event_init();
584 c26f2460 2023-06-08 op
585 c26f2460 2023-06-08 op signal(SIGPIPE, SIG_IGN);
586 c26f2460 2023-06-08 op
587 c26f2460 2023-06-08 op signal_set(&ps->ps_evsigint, SIGINT, proc_sig_handler, p);
588 c26f2460 2023-06-08 op signal_set(&ps->ps_evsigterm, SIGTERM, proc_sig_handler, p);
589 c26f2460 2023-06-08 op signal_set(&ps->ps_evsigchld, SIGCHLD, proc_sig_handler, p);
590 c26f2460 2023-06-08 op signal_set(&ps->ps_evsighup, SIGHUP, proc_sig_handler, p);
591 c26f2460 2023-06-08 op
592 c26f2460 2023-06-08 op signal_add(&ps->ps_evsigint, NULL);
593 c26f2460 2023-06-08 op signal_add(&ps->ps_evsigterm, NULL);
594 c26f2460 2023-06-08 op signal_add(&ps->ps_evsigchld, NULL);
595 c26f2460 2023-06-08 op signal_add(&ps->ps_evsighup, NULL);
596 c26f2460 2023-06-08 op
597 c26f2460 2023-06-08 op proc_setup(ps, procs, nproc);
598 c26f2460 2023-06-08 op proc_accept(ps, PROC_PARENT_SOCK_FILENO, PROC_PARENT, 0);
599 c26f2460 2023-06-08 op
600 c26f2460 2023-06-08 op log_debug("%s: %s %d/%d, pid %d", __func__, p->p_title,
601 c26f2460 2023-06-08 op ps->ps_instance + 1, ps->ps_instances[p->p_id], getpid());
602 c26f2460 2023-06-08 op
603 c26f2460 2023-06-08 op if (run != NULL)
604 c26f2460 2023-06-08 op run(ps, p, arg);
605 c26f2460 2023-06-08 op
606 c26f2460 2023-06-08 op event_dispatch();
607 c26f2460 2023-06-08 op
608 c26f2460 2023-06-08 op proc_shutdown(p);
609 c26f2460 2023-06-08 op }
610 c26f2460 2023-06-08 op
611 c26f2460 2023-06-08 op void
612 c26f2460 2023-06-08 op proc_dispatch(int fd, short event, void *arg)
613 c26f2460 2023-06-08 op {
614 c26f2460 2023-06-08 op struct imsgev *iev = arg;
615 c26f2460 2023-06-08 op struct privsep_proc *p = iev->proc;
616 c26f2460 2023-06-08 op struct privsep *ps = p->p_ps;
617 c26f2460 2023-06-08 op struct imsgbuf *ibuf;
618 c26f2460 2023-06-08 op struct imsg imsg;
619 c26f2460 2023-06-08 op ssize_t n;
620 c26f2460 2023-06-08 op const char *title;
621 c26f2460 2023-06-08 op struct privsep_fd pf;
622 c26f2460 2023-06-08 op
623 c26f2460 2023-06-08 op title = ps->ps_title[privsep_process];
624 c26f2460 2023-06-08 op ibuf = &iev->ibuf;
625 c26f2460 2023-06-08 op
626 c26f2460 2023-06-08 op if (event & EV_READ) {
627 c26f2460 2023-06-08 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
628 c26f2460 2023-06-08 op fatal("%s: imsg_read", __func__);
629 c26f2460 2023-06-08 op if (n == 0) {
630 c26f2460 2023-06-08 op /* this pipe is dead, so remove the event handler */
631 c26f2460 2023-06-08 op event_del(&iev->ev);
632 c26f2460 2023-06-08 op event_loopexit(NULL);
633 c26f2460 2023-06-08 op return;
634 c26f2460 2023-06-08 op }
635 c26f2460 2023-06-08 op }
636 c26f2460 2023-06-08 op
637 c26f2460 2023-06-08 op if (event & EV_WRITE) {
638 c26f2460 2023-06-08 op if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
639 c26f2460 2023-06-08 op fatal("%s: msgbuf_write", __func__);
640 c26f2460 2023-06-08 op if (n == 0) {
641 c26f2460 2023-06-08 op /* this pipe is dead, so remove the event handler */
642 c26f2460 2023-06-08 op event_del(&iev->ev);
643 c26f2460 2023-06-08 op event_loopexit(NULL);
644 c26f2460 2023-06-08 op return;
645 c26f2460 2023-06-08 op }
646 c26f2460 2023-06-08 op }
647 c26f2460 2023-06-08 op
648 c26f2460 2023-06-08 op for (;;) {
649 c26f2460 2023-06-08 op if ((n = imsg_get(ibuf, &imsg)) == -1)
650 c26f2460 2023-06-08 op fatal("%s: imsg_get", __func__);
651 c26f2460 2023-06-08 op if (n == 0)
652 c26f2460 2023-06-08 op break;
653 c26f2460 2023-06-08 op
654 c26f2460 2023-06-08 op #if DEBUG > 1
655 c26f2460 2023-06-08 op log_debug("%s: %s %d got imsg %d peerid %d from %s %d",
656 c26f2460 2023-06-08 op __func__, title, ps->ps_instance + 1,
657 c26f2460 2023-06-08 op imsg.hdr.type, imsg.hdr.peerid, p->p_title, imsg.hdr.pid);
658 c26f2460 2023-06-08 op #endif
659 c26f2460 2023-06-08 op
660 c26f2460 2023-06-08 op /*
661 c26f2460 2023-06-08 op * Check the message with the program callback
662 c26f2460 2023-06-08 op */
663 c26f2460 2023-06-08 op if ((p->p_cb)(fd, p, &imsg) == 0) {
664 c26f2460 2023-06-08 op /* Message was handled by the callback, continue */
665 c26f2460 2023-06-08 op imsg_free(&imsg);
666 c26f2460 2023-06-08 op continue;
667 c26f2460 2023-06-08 op }
668 c26f2460 2023-06-08 op
669 c26f2460 2023-06-08 op /*
670 c26f2460 2023-06-08 op * Generic message handling
671 c26f2460 2023-06-08 op */
672 c26f2460 2023-06-08 op switch (imsg.hdr.type) {
673 c26f2460 2023-06-08 op case IMSG_CTL_PROCFD:
674 6dec2ad7 2024-01-21 op if (imsg_get_data(&imsg, &pf, sizeof(pf)))
675 6dec2ad7 2024-01-21 op fatalx("bad length imsg CTL_PROCFD");
676 b03e976a 2024-01-21 op proc_accept(ps, imsg_get_fd(&imsg), pf.pf_procid,
677 c26f2460 2023-06-08 op pf.pf_instance);
678 c26f2460 2023-06-08 op break;
679 c26f2460 2023-06-08 op default:
680 c26f2460 2023-06-08 op fatalx("%s: %s %d got invalid imsg %d peerid %d "
681 c26f2460 2023-06-08 op "from %s %d",
682 c26f2460 2023-06-08 op __func__, title, ps->ps_instance + 1,
683 c26f2460 2023-06-08 op imsg.hdr.type, imsg.hdr.peerid,
684 c26f2460 2023-06-08 op p->p_title, imsg.hdr.pid);
685 c26f2460 2023-06-08 op }
686 c26f2460 2023-06-08 op imsg_free(&imsg);
687 c26f2460 2023-06-08 op }
688 c26f2460 2023-06-08 op imsg_event_add(iev);
689 c26f2460 2023-06-08 op }
690 c26f2460 2023-06-08 op
691 c26f2460 2023-06-08 op int
692 c26f2460 2023-06-08 op proc_dispatch_null(int fd, struct privsep_proc *p, struct imsg *imsg)
693 c26f2460 2023-06-08 op {
694 c26f2460 2023-06-08 op return (-1);
695 c26f2460 2023-06-08 op }
696 c26f2460 2023-06-08 op
697 c26f2460 2023-06-08 op /*
698 c26f2460 2023-06-08 op * imsg helper functions
699 c26f2460 2023-06-08 op */
700 c26f2460 2023-06-08 op
701 c26f2460 2023-06-08 op void
702 c26f2460 2023-06-08 op imsg_event_add(struct imsgev *iev)
703 c26f2460 2023-06-08 op {
704 c26f2460 2023-06-08 op if (iev->handler == NULL) {
705 c26f2460 2023-06-08 op imsg_flush(&iev->ibuf);
706 c26f2460 2023-06-08 op return;
707 c26f2460 2023-06-08 op }
708 c26f2460 2023-06-08 op
709 c26f2460 2023-06-08 op iev->events = EV_READ;
710 c26f2460 2023-06-08 op if (iev->ibuf.w.queued)
711 c26f2460 2023-06-08 op iev->events |= EV_WRITE;
712 c26f2460 2023-06-08 op
713 c26f2460 2023-06-08 op event_del(&iev->ev);
714 c26f2460 2023-06-08 op event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
715 c26f2460 2023-06-08 op event_add(&iev->ev, NULL);
716 c26f2460 2023-06-08 op }
717 c26f2460 2023-06-08 op
718 c26f2460 2023-06-08 op int
719 c26f2460 2023-06-08 op imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
720 c26f2460 2023-06-08 op pid_t pid, int fd, void *data, uint16_t datalen)
721 c26f2460 2023-06-08 op {
722 c26f2460 2023-06-08 op int ret;
723 c26f2460 2023-06-08 op
724 c26f2460 2023-06-08 op if ((ret = imsg_compose(&iev->ibuf, type, peerid,
725 c26f2460 2023-06-08 op pid, fd, data, datalen)) == -1)
726 c26f2460 2023-06-08 op return (ret);
727 c26f2460 2023-06-08 op imsg_event_add(iev);
728 c26f2460 2023-06-08 op return (ret);
729 c26f2460 2023-06-08 op }
730 c26f2460 2023-06-08 op
731 c26f2460 2023-06-08 op int
732 c26f2460 2023-06-08 op imsg_composev_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
733 c26f2460 2023-06-08 op pid_t pid, int fd, const struct iovec *iov, int iovcnt)
734 c26f2460 2023-06-08 op {
735 c26f2460 2023-06-08 op int ret;
736 c26f2460 2023-06-08 op
737 c26f2460 2023-06-08 op if ((ret = imsg_composev(&iev->ibuf, type, peerid,
738 c26f2460 2023-06-08 op pid, fd, iov, iovcnt)) == -1)
739 c26f2460 2023-06-08 op return (ret);
740 c26f2460 2023-06-08 op imsg_event_add(iev);
741 c26f2460 2023-06-08 op return (ret);
742 c26f2460 2023-06-08 op }
743 c26f2460 2023-06-08 op
744 c26f2460 2023-06-08 op void
745 c26f2460 2023-06-08 op proc_range(struct privsep *ps, enum privsep_procid id, int *n, int *m)
746 c26f2460 2023-06-08 op {
747 c26f2460 2023-06-08 op if (*n == -1) {
748 c26f2460 2023-06-08 op /* Use a range of all target instances */
749 c26f2460 2023-06-08 op *n = 0;
750 c26f2460 2023-06-08 op *m = ps->ps_instances[id];
751 c26f2460 2023-06-08 op } else {
752 c26f2460 2023-06-08 op /* Use only a single slot of the specified peer process */
753 c26f2460 2023-06-08 op *m = *n + 1;
754 c26f2460 2023-06-08 op }
755 c26f2460 2023-06-08 op }
756 c26f2460 2023-06-08 op
757 c26f2460 2023-06-08 op int
758 c26f2460 2023-06-08 op proc_compose_imsg(struct privsep *ps, enum privsep_procid id, int n,
759 c26f2460 2023-06-08 op uint16_t type, uint32_t peerid, int fd, void *data, uint16_t datalen)
760 c26f2460 2023-06-08 op {
761 c26f2460 2023-06-08 op int m;
762 c26f2460 2023-06-08 op
763 c26f2460 2023-06-08 op proc_range(ps, id, &n, &m);
764 c26f2460 2023-06-08 op for (; n < m; n++) {
765 c26f2460 2023-06-08 op if (imsg_compose_event(&ps->ps_ievs[id][n],
766 c26f2460 2023-06-08 op type, peerid, ps->ps_instance + 1, fd, data, datalen) == -1)
767 c26f2460 2023-06-08 op return (-1);
768 c26f2460 2023-06-08 op }
769 c26f2460 2023-06-08 op
770 c26f2460 2023-06-08 op return (0);
771 c26f2460 2023-06-08 op }
772 c26f2460 2023-06-08 op
773 c26f2460 2023-06-08 op int
774 c26f2460 2023-06-08 op proc_compose(struct privsep *ps, enum privsep_procid id,
775 c26f2460 2023-06-08 op uint16_t type, void *data, uint16_t datalen)
776 c26f2460 2023-06-08 op {
777 c26f2460 2023-06-08 op return (proc_compose_imsg(ps, id, -1, type, -1, -1, data, datalen));
778 c26f2460 2023-06-08 op }
779 c26f2460 2023-06-08 op
780 c26f2460 2023-06-08 op int
781 c26f2460 2023-06-08 op proc_composev_imsg(struct privsep *ps, enum privsep_procid id, int n,
782 c26f2460 2023-06-08 op uint16_t type, uint32_t peerid, int fd, const struct iovec *iov, int iovcnt)
783 c26f2460 2023-06-08 op {
784 c26f2460 2023-06-08 op int m;
785 c26f2460 2023-06-08 op
786 c26f2460 2023-06-08 op proc_range(ps, id, &n, &m);
787 c26f2460 2023-06-08 op for (; n < m; n++)
788 c26f2460 2023-06-08 op if (imsg_composev_event(&ps->ps_ievs[id][n],
789 c26f2460 2023-06-08 op type, peerid, ps->ps_instance + 1, fd, iov, iovcnt) == -1)
790 c26f2460 2023-06-08 op return (-1);
791 c26f2460 2023-06-08 op
792 c26f2460 2023-06-08 op return (0);
793 c26f2460 2023-06-08 op }
794 c26f2460 2023-06-08 op
795 c26f2460 2023-06-08 op int
796 c26f2460 2023-06-08 op proc_composev(struct privsep *ps, enum privsep_procid id,
797 c26f2460 2023-06-08 op uint16_t type, const struct iovec *iov, int iovcnt)
798 c26f2460 2023-06-08 op {
799 c26f2460 2023-06-08 op return (proc_composev_imsg(ps, id, -1, type, -1, -1, iov, iovcnt));
800 c26f2460 2023-06-08 op }
801 c26f2460 2023-06-08 op
802 c26f2460 2023-06-08 op struct imsgbuf *
803 c26f2460 2023-06-08 op proc_ibuf(struct privsep *ps, enum privsep_procid id, int n)
804 c26f2460 2023-06-08 op {
805 c26f2460 2023-06-08 op int m;
806 c26f2460 2023-06-08 op
807 c26f2460 2023-06-08 op proc_range(ps, id, &n, &m);
808 c26f2460 2023-06-08 op return (&ps->ps_ievs[id][n].ibuf);
809 c26f2460 2023-06-08 op }
810 c26f2460 2023-06-08 op
811 c26f2460 2023-06-08 op struct imsgev *
812 c26f2460 2023-06-08 op proc_iev(struct privsep *ps, enum privsep_procid id, int n)
813 c26f2460 2023-06-08 op {
814 c26f2460 2023-06-08 op int m;
815 c26f2460 2023-06-08 op
816 c26f2460 2023-06-08 op proc_range(ps, id, &n, &m);
817 c26f2460 2023-06-08 op return (&ps->ps_ievs[id][n]);
818 c26f2460 2023-06-08 op }
819 c26f2460 2023-06-08 op
820 c26f2460 2023-06-08 op /* This function should only be called with care as it breaks async I/O */
821 c26f2460 2023-06-08 op int
822 c26f2460 2023-06-08 op proc_flush_imsg(struct privsep *ps, enum privsep_procid id, int n)
823 c26f2460 2023-06-08 op {
824 c26f2460 2023-06-08 op struct imsgbuf *ibuf;
825 c26f2460 2023-06-08 op int m, ret = 0;
826 c26f2460 2023-06-08 op
827 c26f2460 2023-06-08 op proc_range(ps, id, &n, &m);
828 c26f2460 2023-06-08 op for (; n < m; n++) {
829 c26f2460 2023-06-08 op if ((ibuf = proc_ibuf(ps, id, n)) == NULL)
830 c26f2460 2023-06-08 op return (-1);
831 c26f2460 2023-06-08 op
832 c26f2460 2023-06-08 op do {
833 c26f2460 2023-06-08 op ret = imsg_flush(ibuf);
834 c26f2460 2023-06-08 op } while (ret == -1 && errno == EAGAIN);
835 c26f2460 2023-06-08 op if (ret == -1)
836 c26f2460 2023-06-08 op break;
837 c26f2460 2023-06-08 op imsg_event_add(&ps->ps_ievs[id][n]);
838 c26f2460 2023-06-08 op }
839 c26f2460 2023-06-08 op
840 c26f2460 2023-06-08 op return (ret);
841 c26f2460 2023-06-08 op }