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