2 ce1bfad9 2024-03-30 thomas * Copyright (c) 2024 Stefan Sperling <stsp@openbsd.org>
4 ce1bfad9 2024-03-30 thomas * Permission to use, copy, modify, and distribute this software for any
5 ce1bfad9 2024-03-30 thomas * purpose with or without fee is hereby granted, provided that the above
6 ce1bfad9 2024-03-30 thomas * copyright notice and this permission notice appear in all copies.
8 ce1bfad9 2024-03-30 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 ce1bfad9 2024-03-30 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 ce1bfad9 2024-03-30 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 ce1bfad9 2024-03-30 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 ce1bfad9 2024-03-30 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 ce1bfad9 2024-03-30 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 ce1bfad9 2024-03-30 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 ce1bfad9 2024-03-30 thomas #include <sys/types.h>
18 ce1bfad9 2024-03-30 thomas #include <sys/queue.h>
19 ce1bfad9 2024-03-30 thomas #include <sys/tree.h>
20 ce1bfad9 2024-03-30 thomas #include <sys/socket.h>
21 ce1bfad9 2024-03-30 thomas #include <sys/wait.h>
23 ce1bfad9 2024-03-30 thomas #include <errno.h>
24 ce1bfad9 2024-03-30 thomas #include <event.h>
25 ce1bfad9 2024-03-30 thomas #include <siphash.h>
26 ce1bfad9 2024-03-30 thomas #include <limits.h>
27 ce1bfad9 2024-03-30 thomas #include <sha1.h>
28 ce1bfad9 2024-03-30 thomas #include <sha2.h>
29 ce1bfad9 2024-03-30 thomas #include <signal.h>
30 ce1bfad9 2024-03-30 thomas #include <stdio.h>
31 ce1bfad9 2024-03-30 thomas #include <stdlib.h>
32 ce1bfad9 2024-03-30 thomas #include <string.h>
33 ce1bfad9 2024-03-30 thomas #include <imsg.h>
34 ce1bfad9 2024-03-30 thomas #include <unistd.h>
36 ce1bfad9 2024-03-30 thomas #include "got_error.h"
37 ce1bfad9 2024-03-30 thomas #include "got_path.h"
39 ce1bfad9 2024-03-30 thomas #include "gotd.h"
40 ce1bfad9 2024-03-30 thomas #include "log.h"
41 ce1bfad9 2024-03-30 thomas #include "notify.h"
43 ce1bfad9 2024-03-30 thomas #ifndef nitems
44 ce1bfad9 2024-03-30 thomas #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
47 ce1bfad9 2024-03-30 thomas static struct gotd_notify {
48 ce1bfad9 2024-03-30 thomas pid_t pid;
49 ce1bfad9 2024-03-30 thomas const char *title;
50 ce1bfad9 2024-03-30 thomas struct gotd_imsgev parent_iev;
51 ce1bfad9 2024-03-30 thomas struct gotd_repolist *repos;
52 ce1bfad9 2024-03-30 thomas const char *default_sender;
53 ce1bfad9 2024-03-30 thomas } gotd_notify;
55 ce1bfad9 2024-03-30 thomas struct gotd_notify_session {
56 ce1bfad9 2024-03-30 thomas STAILQ_ENTRY(gotd_notify_session) entry;
57 ce1bfad9 2024-03-30 thomas uint32_t id;
58 ce1bfad9 2024-03-30 thomas struct gotd_imsgev iev;
60 ce1bfad9 2024-03-30 thomas STAILQ_HEAD(gotd_notify_sessions, gotd_notify_session);
62 ce1bfad9 2024-03-30 thomas static struct gotd_notify_sessions gotd_notify_sessions[GOTD_CLIENT_TABLE_SIZE];
63 ce1bfad9 2024-03-30 thomas static SIPHASH_KEY sessions_hash_key;
65 ce1bfad9 2024-03-30 thomas static void gotd_notify_shutdown(void);
67 ce1bfad9 2024-03-30 thomas static uint64_t
68 ce1bfad9 2024-03-30 thomas session_hash(uint32_t session_id)
70 ce1bfad9 2024-03-30 thomas return SipHash24(&sessions_hash_key, &session_id, sizeof(session_id));
73 ce1bfad9 2024-03-30 thomas static void
74 ce1bfad9 2024-03-30 thomas add_session(struct gotd_notify_session *session)
76 ce1bfad9 2024-03-30 thomas uint64_t slot;
78 ce1bfad9 2024-03-30 thomas slot = session_hash(session->id) % nitems(gotd_notify_sessions);
79 ce1bfad9 2024-03-30 thomas STAILQ_INSERT_HEAD(&gotd_notify_sessions[slot], session, entry);
82 ce1bfad9 2024-03-30 thomas static struct gotd_notify_session *
83 ce1bfad9 2024-03-30 thomas find_session(uint32_t session_id)
85 ce1bfad9 2024-03-30 thomas uint64_t slot;
86 ce1bfad9 2024-03-30 thomas struct gotd_notify_session *s;
88 ce1bfad9 2024-03-30 thomas slot = session_hash(session_id) % nitems(gotd_notify_sessions);
89 ce1bfad9 2024-03-30 thomas STAILQ_FOREACH(s, &gotd_notify_sessions[slot], entry) {
90 ce1bfad9 2024-03-30 thomas if (s->id == session_id)
94 ce1bfad9 2024-03-30 thomas return NULL;
97 ce1bfad9 2024-03-30 thomas static struct gotd_notify_session *
98 ce1bfad9 2024-03-30 thomas find_session_by_fd(int fd)
100 ce1bfad9 2024-03-30 thomas uint64_t slot;
101 ce1bfad9 2024-03-30 thomas struct gotd_notify_session *s;
103 ce1bfad9 2024-03-30 thomas for (slot = 0; slot < nitems(gotd_notify_sessions); slot++) {
104 ce1bfad9 2024-03-30 thomas STAILQ_FOREACH(s, &gotd_notify_sessions[slot], entry) {
105 ce1bfad9 2024-03-30 thomas if (s->iev.ibuf.fd == fd)
106 ce1bfad9 2024-03-30 thomas return s;
111 ce1bfad9 2024-03-30 thomas return NULL;
115 ce1bfad9 2024-03-30 thomas static void
116 ce1bfad9 2024-03-30 thomas remove_session(struct gotd_notify_session *session)
118 ce1bfad9 2024-03-30 thomas uint64_t slot;
120 ce1bfad9 2024-03-30 thomas slot = session_hash(session->id) % nitems(gotd_notify_sessions);
121 ce1bfad9 2024-03-30 thomas STAILQ_REMOVE(&gotd_notify_sessions[slot], session,
122 ce1bfad9 2024-03-30 thomas gotd_notify_session, entry);
123 ce1bfad9 2024-03-30 thomas free(session);
126 ce1bfad9 2024-03-30 thomas static uint32_t
127 ce1bfad9 2024-03-30 thomas get_session_id(void)
129 ce1bfad9 2024-03-30 thomas int duplicate = 0;
130 ce1bfad9 2024-03-30 thomas uint32_t id;
133 ce1bfad9 2024-03-30 thomas id = arc4random();
134 ce1bfad9 2024-03-30 thomas duplicate = (find_session(id) != NULL);
135 ce1bfad9 2024-03-30 thomas } while (duplicate || id == 0);
137 ce1bfad9 2024-03-30 thomas return id;
140 ce1bfad9 2024-03-30 thomas static void
141 ce1bfad9 2024-03-30 thomas gotd_notify_sighdlr(int sig, short event, void *arg)
144 ce1bfad9 2024-03-30 thomas * Normal signal handler rules don't apply because libevent
145 ce1bfad9 2024-03-30 thomas * decouples for us.
148 ce1bfad9 2024-03-30 thomas switch (sig) {
149 ce1bfad9 2024-03-30 thomas case SIGHUP:
150 ce1bfad9 2024-03-30 thomas log_info("%s: ignoring SIGHUP", __func__);
152 ce1bfad9 2024-03-30 thomas case SIGUSR1:
153 ce1bfad9 2024-03-30 thomas log_info("%s: ignoring SIGUSR1", __func__);
155 ce1bfad9 2024-03-30 thomas case SIGTERM:
156 ce1bfad9 2024-03-30 thomas case SIGINT:
157 ce1bfad9 2024-03-30 thomas gotd_notify_shutdown();
158 ce1bfad9 2024-03-30 thomas /* NOTREACHED */
161 ce1bfad9 2024-03-30 thomas fatalx("unexpected signal");
165 ce1bfad9 2024-03-30 thomas static void
166 ce1bfad9 2024-03-30 thomas run_notification_helper(const char *prog, const char **argv, int fd)
168 ce1bfad9 2024-03-30 thomas const struct got_error *err = NULL;
169 ce1bfad9 2024-03-30 thomas pid_t pid;
170 ce1bfad9 2024-03-30 thomas int child_status;
172 ce1bfad9 2024-03-30 thomas pid = fork();
173 ce1bfad9 2024-03-30 thomas if (pid == -1) {
174 ce1bfad9 2024-03-30 thomas err = got_error_from_errno("fork");
175 ce1bfad9 2024-03-30 thomas log_warn("%s", err->msg);
177 ce1bfad9 2024-03-30 thomas } else if (pid == 0) {
178 ce1bfad9 2024-03-30 thomas signal(SIGQUIT, SIG_DFL);
179 ce1bfad9 2024-03-30 thomas signal(SIGINT, SIG_DFL);
180 ce1bfad9 2024-03-30 thomas signal(SIGCHLD, SIG_DFL);
182 ce1bfad9 2024-03-30 thomas if (dup2(fd, STDIN_FILENO) == -1) {
183 ce1bfad9 2024-03-30 thomas fprintf(stderr, "%s: dup2: %s\n", getprogname(),
184 ce1bfad9 2024-03-30 thomas strerror(errno));
185 ce1bfad9 2024-03-30 thomas _exit(1);
188 ce1bfad9 2024-03-30 thomas closefrom(STDERR_FILENO + 1);
190 ce1bfad9 2024-03-30 thomas if (execv(prog, (char *const *)argv) == -1) {
191 ce1bfad9 2024-03-30 thomas fprintf(stderr, "%s: exec %s: %s\n", getprogname(),
192 ce1bfad9 2024-03-30 thomas prog, strerror(errno));
193 ce1bfad9 2024-03-30 thomas _exit(1);
196 ce1bfad9 2024-03-30 thomas /* not reached */
199 ce1bfad9 2024-03-30 thomas if (waitpid(pid, &child_status, 0) == -1) {
200 ce1bfad9 2024-03-30 thomas err = got_error_from_errno("waitpid");
201 ce1bfad9 2024-03-30 thomas goto done;
204 ce1bfad9 2024-03-30 thomas if (!WIFEXITED(child_status)) {
205 ce1bfad9 2024-03-30 thomas err = got_error(GOT_ERR_PRIVSEP_DIED);
206 ce1bfad9 2024-03-30 thomas goto done;
209 ce1bfad9 2024-03-30 thomas if (WEXITSTATUS(child_status) != 0)
210 ce1bfad9 2024-03-30 thomas err = got_error(GOT_ERR_PRIVSEP_EXIT);
213 ce1bfad9 2024-03-30 thomas log_warnx("%s: child %s pid %d: %s", gotd_notify.title,
214 ce1bfad9 2024-03-30 thomas prog, pid, err->msg);
217 ce1bfad9 2024-03-30 thomas static void
218 ce1bfad9 2024-03-30 thomas notify_email(struct gotd_notification_target *target, const char *subject_line,
221 ce1bfad9 2024-03-30 thomas const char *argv[13];
222 ce1bfad9 2024-03-30 thomas int i = 0;
224 ce1bfad9 2024-03-30 thomas argv[i++] = GOTD_PATH_PROG_NOTIFY_EMAIL;
226 ce1bfad9 2024-03-30 thomas argv[i++] = "-f";
227 ce1bfad9 2024-03-30 thomas if (target->conf.email.sender)
228 ce1bfad9 2024-03-30 thomas argv[i++] = target->conf.email.sender;
230 ce1bfad9 2024-03-30 thomas argv[i++] = gotd_notify.default_sender;
232 ce1bfad9 2024-03-30 thomas if (target->conf.email.responder) {
233 ce1bfad9 2024-03-30 thomas argv[i++] = "-r";
234 ce1bfad9 2024-03-30 thomas argv[i++] = target->conf.email.responder;
237 ce1bfad9 2024-03-30 thomas if (target->conf.email.hostname) {
238 ce1bfad9 2024-03-30 thomas argv[i++] = "-h";
239 ce1bfad9 2024-03-30 thomas argv[i++] = target->conf.email.hostname;
242 ce1bfad9 2024-03-30 thomas if (target->conf.email.port) {
243 ce1bfad9 2024-03-30 thomas argv[i++] = "-p";
244 ce1bfad9 2024-03-30 thomas argv[i++] = target->conf.email.port;
247 ce1bfad9 2024-03-30 thomas argv[i++] = "-s";
248 ce1bfad9 2024-03-30 thomas argv[i++] = subject_line;
250 ce1bfad9 2024-03-30 thomas argv[i++] = target->conf.email.recipient;
252 ce1bfad9 2024-03-30 thomas argv[i] = NULL;
254 ce1bfad9 2024-03-30 thomas run_notification_helper(GOTD_PATH_PROG_NOTIFY_EMAIL, argv, fd);
257 ce1bfad9 2024-03-30 thomas static void
258 ce1bfad9 2024-03-30 thomas notify_http(struct gotd_notification_target *target, const char *subject_line,
261 ce1bfad9 2024-03-30 thomas const char *argv[10] = { 0 }; /* TODO */
263 ce1bfad9 2024-03-30 thomas run_notification_helper(GOTD_PATH_PROG_NOTIFY_HTTP, argv, fd);
266 ce1bfad9 2024-03-30 thomas static const struct got_error *
267 ce1bfad9 2024-03-30 thomas send_notification(struct imsg *imsg, struct gotd_imsgev *iev)
269 ce1bfad9 2024-03-30 thomas const struct got_error *err = NULL;
270 ce1bfad9 2024-03-30 thomas struct gotd_imsg_notify inotify;
271 ce1bfad9 2024-03-30 thomas size_t datalen;
272 ce1bfad9 2024-03-30 thomas struct gotd_repo *repo;
273 ce1bfad9 2024-03-30 thomas struct gotd_notification_target *target;
276 ce1bfad9 2024-03-30 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
277 ce1bfad9 2024-03-30 thomas if (datalen != sizeof(inotify))
278 ce1bfad9 2024-03-30 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
280 ce1bfad9 2024-03-30 thomas memcpy(&inotify, imsg->data, datalen);
282 ce1bfad9 2024-03-30 thomas repo = gotd_find_repo_by_name(inotify.repo_name, gotd_notify.repos);
283 ce1bfad9 2024-03-30 thomas if (repo == NULL)
284 ce1bfad9 2024-03-30 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
286 ce1bfad9 2024-03-30 thomas fd = imsg_get_fd(imsg);
287 ce1bfad9 2024-03-30 thomas if (fd == -1)
288 ce1bfad9 2024-03-30 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
290 ce1bfad9 2024-03-30 thomas if (lseek(fd, 0, SEEK_SET) == -1) {
291 ce1bfad9 2024-03-30 thomas err = got_error_from_errno("lseek");
292 ce1bfad9 2024-03-30 thomas goto done;
295 ce1bfad9 2024-03-30 thomas STAILQ_FOREACH(target, &repo->notification_targets, entry) {
296 ce1bfad9 2024-03-30 thomas switch (target->type) {
297 ce1bfad9 2024-03-30 thomas case GOTD_NOTIFICATION_VIA_EMAIL:
298 ce1bfad9 2024-03-30 thomas notify_email(target, inotify.subject_line, fd);
300 ce1bfad9 2024-03-30 thomas case GOTD_NOTIFICATION_VIA_HTTP:
301 ce1bfad9 2024-03-30 thomas notify_http(target, inotify.subject_line, fd);
306 ce1bfad9 2024-03-30 thomas if (gotd_imsg_compose_event(iev, GOTD_IMSG_NOTIFICATION_SENT,
307 ce1bfad9 2024-03-30 thomas PROC_NOTIFY, -1, NULL, 0) == -1) {
308 ce1bfad9 2024-03-30 thomas err = got_error_from_errno("imsg compose NOTIFY");
309 ce1bfad9 2024-03-30 thomas goto done;
312 ce1bfad9 2024-03-30 thomas close(fd);
313 ce1bfad9 2024-03-30 thomas return err;
316 ce1bfad9 2024-03-30 thomas static void
317 ce1bfad9 2024-03-30 thomas notify_dispatch_session(int fd, short event, void *arg)
319 ce1bfad9 2024-03-30 thomas struct gotd_imsgev *iev = arg;
320 ce1bfad9 2024-03-30 thomas struct imsgbuf *ibuf = &iev->ibuf;
321 ce1bfad9 2024-03-30 thomas ssize_t n;
322 ce1bfad9 2024-03-30 thomas int shut = 0;
323 ce1bfad9 2024-03-30 thomas struct imsg imsg;
325 ce1bfad9 2024-03-30 thomas if (event & EV_READ) {
326 ce1bfad9 2024-03-30 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
327 ce1bfad9 2024-03-30 thomas fatal("imsg_read error");
328 ce1bfad9 2024-03-30 thomas if (n == 0) {
329 ce1bfad9 2024-03-30 thomas /* Connection closed. */
330 ce1bfad9 2024-03-30 thomas shut = 1;
331 ce1bfad9 2024-03-30 thomas goto done;
335 ce1bfad9 2024-03-30 thomas if (event & EV_WRITE) {
336 ce1bfad9 2024-03-30 thomas n = msgbuf_write(&ibuf->w);
337 ce1bfad9 2024-03-30 thomas if (n == -1 && errno != EAGAIN)
338 ce1bfad9 2024-03-30 thomas fatal("msgbuf_write");
339 ce1bfad9 2024-03-30 thomas if (n == 0) {
340 ce1bfad9 2024-03-30 thomas /* Connection closed. */
341 ce1bfad9 2024-03-30 thomas shut = 1;
342 ce1bfad9 2024-03-30 thomas goto done;
346 ce1bfad9 2024-03-30 thomas for (;;) {
347 ce1bfad9 2024-03-30 thomas const struct got_error *err = NULL;
349 ce1bfad9 2024-03-30 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
350 ce1bfad9 2024-03-30 thomas fatal("%s: imsg_get error", __func__);
351 ce1bfad9 2024-03-30 thomas if (n == 0) /* No more messages. */
354 ce1bfad9 2024-03-30 thomas switch (imsg.hdr.type) {
355 ce1bfad9 2024-03-30 thomas case GOTD_IMSG_NOTIFY:
356 ce1bfad9 2024-03-30 thomas err = send_notification(&imsg, iev);
359 ce1bfad9 2024-03-30 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
362 ce1bfad9 2024-03-30 thomas imsg_free(&imsg);
365 ce1bfad9 2024-03-30 thomas log_warnx("%s: %s", __func__, err->msg);
368 ce1bfad9 2024-03-30 thomas if (!shut) {
369 ce1bfad9 2024-03-30 thomas gotd_imsg_event_add(iev);
371 ce1bfad9 2024-03-30 thomas struct gotd_notify_session *session;
373 ce1bfad9 2024-03-30 thomas /* This pipe is dead. Remove its event handler */
374 ce1bfad9 2024-03-30 thomas event_del(&iev->ev);
375 ce1bfad9 2024-03-30 thomas imsg_clear(&iev->ibuf);
377 ce1bfad9 2024-03-30 thomas session = find_session_by_fd(fd);
378 ce1bfad9 2024-03-30 thomas if (session)
379 ce1bfad9 2024-03-30 thomas remove_session(session);
383 ce1bfad9 2024-03-30 thomas static const struct got_error *
384 ce1bfad9 2024-03-30 thomas recv_session(struct imsg *imsg)
386 ce1bfad9 2024-03-30 thomas struct gotd_notify_session *session;
387 ce1bfad9 2024-03-30 thomas size_t datalen;
390 ce1bfad9 2024-03-30 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
391 ce1bfad9 2024-03-30 thomas if (datalen != 0)
392 ce1bfad9 2024-03-30 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
394 ce1bfad9 2024-03-30 thomas fd = imsg_get_fd(imsg);
395 ce1bfad9 2024-03-30 thomas if (fd == -1)
396 ce1bfad9 2024-03-30 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
398 ce1bfad9 2024-03-30 thomas session = calloc(1, sizeof(*session));
399 ce1bfad9 2024-03-30 thomas if (session == NULL)
400 ce1bfad9 2024-03-30 thomas return got_error_from_errno("calloc");
402 ce1bfad9 2024-03-30 thomas session->id = get_session_id();
403 ce1bfad9 2024-03-30 thomas imsg_init(&session->iev.ibuf, fd);
404 ce1bfad9 2024-03-30 thomas session->iev.handler = notify_dispatch_session;
405 ce1bfad9 2024-03-30 thomas session->iev.events = EV_READ;
406 ce1bfad9 2024-03-30 thomas session->iev.handler_arg = NULL;
407 ce1bfad9 2024-03-30 thomas event_set(&session->iev.ev, session->iev.ibuf.fd, EV_READ,
408 ce1bfad9 2024-03-30 thomas notify_dispatch_session, &session->iev);
409 ce1bfad9 2024-03-30 thomas gotd_imsg_event_add(&session->iev);
410 ce1bfad9 2024-03-30 thomas add_session(session);
412 ce1bfad9 2024-03-30 thomas return NULL;
415 ce1bfad9 2024-03-30 thomas static void
416 ce1bfad9 2024-03-30 thomas notify_dispatch(int fd, short event, void *arg)
418 ce1bfad9 2024-03-30 thomas struct gotd_imsgev *iev = arg;
419 ce1bfad9 2024-03-30 thomas struct imsgbuf *ibuf = &iev->ibuf;
420 ce1bfad9 2024-03-30 thomas ssize_t n;
421 ce1bfad9 2024-03-30 thomas int shut = 0;
422 ce1bfad9 2024-03-30 thomas struct imsg imsg;
424 ce1bfad9 2024-03-30 thomas if (event & EV_READ) {
425 ce1bfad9 2024-03-30 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
426 ce1bfad9 2024-03-30 thomas fatal("imsg_read error");
427 ce1bfad9 2024-03-30 thomas if (n == 0) {
428 ce1bfad9 2024-03-30 thomas /* Connection closed. */
429 ce1bfad9 2024-03-30 thomas shut = 1;
430 ce1bfad9 2024-03-30 thomas goto done;
434 ce1bfad9 2024-03-30 thomas if (event & EV_WRITE) {
435 ce1bfad9 2024-03-30 thomas n = msgbuf_write(&ibuf->w);
436 ce1bfad9 2024-03-30 thomas if (n == -1 && errno != EAGAIN)
437 ce1bfad9 2024-03-30 thomas fatal("msgbuf_write");
438 ce1bfad9 2024-03-30 thomas if (n == 0) {
439 ce1bfad9 2024-03-30 thomas /* Connection closed. */
440 ce1bfad9 2024-03-30 thomas shut = 1;
441 ce1bfad9 2024-03-30 thomas goto done;
445 ce1bfad9 2024-03-30 thomas for (;;) {
446 ce1bfad9 2024-03-30 thomas const struct got_error *err = NULL;
448 ce1bfad9 2024-03-30 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
449 ce1bfad9 2024-03-30 thomas fatal("%s: imsg_get error", __func__);
450 ce1bfad9 2024-03-30 thomas if (n == 0) /* No more messages. */
453 ce1bfad9 2024-03-30 thomas switch (imsg.hdr.type) {
454 ce1bfad9 2024-03-30 thomas case GOTD_IMSG_CONNECT_SESSION:
455 ce1bfad9 2024-03-30 thomas err = recv_session(&imsg);
458 ce1bfad9 2024-03-30 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
461 ce1bfad9 2024-03-30 thomas imsg_free(&imsg);
464 ce1bfad9 2024-03-30 thomas log_warnx("%s: %s", __func__, err->msg);
467 ce1bfad9 2024-03-30 thomas if (!shut) {
468 ce1bfad9 2024-03-30 thomas gotd_imsg_event_add(iev);
470 ce1bfad9 2024-03-30 thomas /* This pipe is dead. Remove its event handler */
471 ce1bfad9 2024-03-30 thomas event_del(&iev->ev);
472 ce1bfad9 2024-03-30 thomas event_loopexit(NULL);
478 ce1bfad9 2024-03-30 thomas notify_main(const char *title, struct gotd_repolist *repos,
479 ce1bfad9 2024-03-30 thomas const char *default_sender)
481 ce1bfad9 2024-03-30 thomas const struct got_error *err = NULL;
482 ce1bfad9 2024-03-30 thomas struct event evsigint, evsigterm, evsighup, evsigusr1;
484 ce1bfad9 2024-03-30 thomas arc4random_buf(&sessions_hash_key, sizeof(sessions_hash_key));
486 ce1bfad9 2024-03-30 thomas gotd_notify.title = title;
487 ce1bfad9 2024-03-30 thomas gotd_notify.repos = repos;
488 ce1bfad9 2024-03-30 thomas gotd_notify.default_sender = default_sender;
489 ce1bfad9 2024-03-30 thomas gotd_notify.pid = getpid();
491 ce1bfad9 2024-03-30 thomas signal_set(&evsigint, SIGINT, gotd_notify_sighdlr, NULL);
492 ce1bfad9 2024-03-30 thomas signal_set(&evsigterm, SIGTERM, gotd_notify_sighdlr, NULL);
493 ce1bfad9 2024-03-30 thomas signal_set(&evsighup, SIGHUP, gotd_notify_sighdlr, NULL);
494 ce1bfad9 2024-03-30 thomas signal_set(&evsigusr1, SIGUSR1, gotd_notify_sighdlr, NULL);
495 ce1bfad9 2024-03-30 thomas signal(SIGPIPE, SIG_IGN);
497 ce1bfad9 2024-03-30 thomas signal_add(&evsigint, NULL);
498 ce1bfad9 2024-03-30 thomas signal_add(&evsigterm, NULL);
499 ce1bfad9 2024-03-30 thomas signal_add(&evsighup, NULL);
500 ce1bfad9 2024-03-30 thomas signal_add(&evsigusr1, NULL);
502 ce1bfad9 2024-03-30 thomas imsg_init(&gotd_notify.parent_iev.ibuf, GOTD_FILENO_MSG_PIPE);
503 ce1bfad9 2024-03-30 thomas gotd_notify.parent_iev.handler = notify_dispatch;
504 ce1bfad9 2024-03-30 thomas gotd_notify.parent_iev.events = EV_READ;
505 ce1bfad9 2024-03-30 thomas gotd_notify.parent_iev.handler_arg = NULL;
506 ce1bfad9 2024-03-30 thomas event_set(&gotd_notify.parent_iev.ev, gotd_notify.parent_iev.ibuf.fd,
507 ce1bfad9 2024-03-30 thomas EV_READ, notify_dispatch, &gotd_notify.parent_iev);
508 ce1bfad9 2024-03-30 thomas gotd_imsg_event_add(&gotd_notify.parent_iev);
510 ce1bfad9 2024-03-30 thomas event_dispatch();
513 ce1bfad9 2024-03-30 thomas log_warnx("%s: %s", title, err->msg);
514 ce1bfad9 2024-03-30 thomas gotd_notify_shutdown();
518 ce1bfad9 2024-03-30 thomas gotd_notify_shutdown(void)
520 ce1bfad9 2024-03-30 thomas log_debug("shutting down");