2 ba97b2d7 2024-03-20 stsp * Copyright (c) 2024 Stefan Sperling <stsp@openbsd.org>
4 ba97b2d7 2024-03-20 stsp * Permission to use, copy, modify, and distribute this software for any
5 ba97b2d7 2024-03-20 stsp * purpose with or without fee is hereby granted, provided that the above
6 ba97b2d7 2024-03-20 stsp * copyright notice and this permission notice appear in all copies.
8 ba97b2d7 2024-03-20 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 ba97b2d7 2024-03-20 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 ba97b2d7 2024-03-20 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 ba97b2d7 2024-03-20 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 ba97b2d7 2024-03-20 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 ba97b2d7 2024-03-20 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 ba97b2d7 2024-03-20 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 ba97b2d7 2024-03-20 stsp #include <sys/types.h>
18 ba97b2d7 2024-03-20 stsp #include <sys/queue.h>
19 ba97b2d7 2024-03-20 stsp #include <sys/tree.h>
20 ba97b2d7 2024-03-20 stsp #include <sys/socket.h>
21 ba97b2d7 2024-03-20 stsp #include <sys/wait.h>
23 ba97b2d7 2024-03-20 stsp #include <errno.h>
24 ba97b2d7 2024-03-20 stsp #include <event.h>
25 ba97b2d7 2024-03-20 stsp #include <siphash.h>
26 ba97b2d7 2024-03-20 stsp #include <limits.h>
27 ba97b2d7 2024-03-20 stsp #include <sha1.h>
28 ba97b2d7 2024-03-20 stsp #include <sha2.h>
29 ba97b2d7 2024-03-20 stsp #include <signal.h>
30 ba97b2d7 2024-03-20 stsp #include <stdio.h>
31 ba97b2d7 2024-03-20 stsp #include <stdlib.h>
32 ba97b2d7 2024-03-20 stsp #include <string.h>
33 ba97b2d7 2024-03-20 stsp #include <imsg.h>
34 ba97b2d7 2024-03-20 stsp #include <unistd.h>
36 ba97b2d7 2024-03-20 stsp #include "got_error.h"
37 ba97b2d7 2024-03-20 stsp #include "got_path.h"
39 ba97b2d7 2024-03-20 stsp #include "gotd.h"
40 ba97b2d7 2024-03-20 stsp #include "log.h"
41 ba97b2d7 2024-03-20 stsp #include "notify.h"
43 ba97b2d7 2024-03-20 stsp #ifndef nitems
44 ba97b2d7 2024-03-20 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
47 ba97b2d7 2024-03-20 stsp static struct gotd_notify {
49 ba97b2d7 2024-03-20 stsp const char *title;
50 ba97b2d7 2024-03-20 stsp struct gotd_imsgev parent_iev;
51 ba97b2d7 2024-03-20 stsp struct gotd_repolist *repos;
52 ba97b2d7 2024-03-20 stsp const char *default_sender;
53 ba97b2d7 2024-03-20 stsp } gotd_notify;
55 ba97b2d7 2024-03-20 stsp struct gotd_notify_session {
56 ba97b2d7 2024-03-20 stsp STAILQ_ENTRY(gotd_notify_session) entry;
57 ba97b2d7 2024-03-20 stsp uint32_t id;
58 ba97b2d7 2024-03-20 stsp struct gotd_imsgev iev;
60 ba97b2d7 2024-03-20 stsp STAILQ_HEAD(gotd_notify_sessions, gotd_notify_session);
62 ba97b2d7 2024-03-20 stsp static struct gotd_notify_sessions gotd_notify_sessions[GOTD_CLIENT_TABLE_SIZE];
63 ba97b2d7 2024-03-20 stsp static SIPHASH_KEY sessions_hash_key;
65 ba97b2d7 2024-03-20 stsp static void gotd_notify_shutdown(void);
67 ba97b2d7 2024-03-20 stsp static uint64_t
68 ba97b2d7 2024-03-20 stsp session_hash(uint32_t session_id)
70 ba97b2d7 2024-03-20 stsp return SipHash24(&sessions_hash_key, &session_id, sizeof(session_id));
74 ba97b2d7 2024-03-20 stsp add_session(struct gotd_notify_session *session)
76 ba97b2d7 2024-03-20 stsp uint64_t slot;
78 ba97b2d7 2024-03-20 stsp slot = session_hash(session->id) % nitems(gotd_notify_sessions);
79 ba97b2d7 2024-03-20 stsp STAILQ_INSERT_HEAD(&gotd_notify_sessions[slot], session, entry);
82 ba97b2d7 2024-03-20 stsp static struct gotd_notify_session *
83 ba97b2d7 2024-03-20 stsp find_session(uint32_t session_id)
85 ba97b2d7 2024-03-20 stsp uint64_t slot;
86 ba97b2d7 2024-03-20 stsp struct gotd_notify_session *s;
88 ba97b2d7 2024-03-20 stsp slot = session_hash(session_id) % nitems(gotd_notify_sessions);
89 ba97b2d7 2024-03-20 stsp STAILQ_FOREACH(s, &gotd_notify_sessions[slot], entry) {
90 ba97b2d7 2024-03-20 stsp if (s->id == session_id)
94 ba97b2d7 2024-03-20 stsp return NULL;
97 ba97b2d7 2024-03-20 stsp static struct gotd_notify_session *
98 ba97b2d7 2024-03-20 stsp find_session_by_fd(int fd)
100 ba97b2d7 2024-03-20 stsp uint64_t slot;
101 ba97b2d7 2024-03-20 stsp struct gotd_notify_session *s;
103 ba97b2d7 2024-03-20 stsp for (slot = 0; slot < nitems(gotd_notify_sessions); slot++) {
104 ba97b2d7 2024-03-20 stsp STAILQ_FOREACH(s, &gotd_notify_sessions[slot], entry) {
105 ba97b2d7 2024-03-20 stsp if (s->iev.ibuf.fd == fd)
110 ba97b2d7 2024-03-20 stsp return NULL;
113 ba97b2d7 2024-03-20 stsp static void
114 ba97b2d7 2024-03-20 stsp remove_session(struct gotd_notify_session *session)
116 ba97b2d7 2024-03-20 stsp uint64_t slot;
118 ba97b2d7 2024-03-20 stsp slot = session_hash(session->id) % nitems(gotd_notify_sessions);
119 ba97b2d7 2024-03-20 stsp STAILQ_REMOVE(&gotd_notify_sessions[slot], session,
120 ba97b2d7 2024-03-20 stsp gotd_notify_session, entry);
121 ba97b2d7 2024-03-20 stsp free(session);
124 ba97b2d7 2024-03-20 stsp static uint32_t
125 ba97b2d7 2024-03-20 stsp get_session_id(void)
127 ba97b2d7 2024-03-20 stsp int duplicate = 0;
128 ba97b2d7 2024-03-20 stsp uint32_t id;
131 ba97b2d7 2024-03-20 stsp id = arc4random();
132 ba97b2d7 2024-03-20 stsp duplicate = (find_session(id) != NULL);
133 ba97b2d7 2024-03-20 stsp } while (duplicate || id == 0);
138 ba97b2d7 2024-03-20 stsp static void
139 ba97b2d7 2024-03-20 stsp gotd_notify_sighdlr(int sig, short event, void *arg)
142 ba97b2d7 2024-03-20 stsp * Normal signal handler rules don't apply because libevent
143 ba97b2d7 2024-03-20 stsp * decouples for us.
146 ba97b2d7 2024-03-20 stsp switch (sig) {
147 ba97b2d7 2024-03-20 stsp case SIGHUP:
148 ba97b2d7 2024-03-20 stsp log_info("%s: ignoring SIGHUP", __func__);
150 ba97b2d7 2024-03-20 stsp case SIGUSR1:
151 ba97b2d7 2024-03-20 stsp log_info("%s: ignoring SIGUSR1", __func__);
153 ba97b2d7 2024-03-20 stsp case SIGTERM:
154 ba97b2d7 2024-03-20 stsp case SIGINT:
155 ba97b2d7 2024-03-20 stsp gotd_notify_shutdown();
156 ba97b2d7 2024-03-20 stsp /* NOTREACHED */
159 ba97b2d7 2024-03-20 stsp fatalx("unexpected signal");
163 ba97b2d7 2024-03-20 stsp static void
164 ba97b2d7 2024-03-20 stsp run_notification_helper(const char *prog, const char **argv, int fd)
166 ba97b2d7 2024-03-20 stsp const struct got_error *err = NULL;
168 ba97b2d7 2024-03-20 stsp int child_status;
170 ba97b2d7 2024-03-20 stsp pid = fork();
171 ba97b2d7 2024-03-20 stsp if (pid == -1) {
172 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("fork");
173 ba97b2d7 2024-03-20 stsp log_warn("%s", err->msg);
175 ba97b2d7 2024-03-20 stsp } else if (pid == 0) {
176 ba97b2d7 2024-03-20 stsp signal(SIGQUIT, SIG_DFL);
177 ba97b2d7 2024-03-20 stsp signal(SIGINT, SIG_DFL);
178 ba97b2d7 2024-03-20 stsp signal(SIGCHLD, SIG_DFL);
180 ba97b2d7 2024-03-20 stsp if (dup2(fd, STDIN_FILENO) == -1) {
181 ba97b2d7 2024-03-20 stsp fprintf(stderr, "%s: dup2: %s\n", getprogname(),
182 ba97b2d7 2024-03-20 stsp strerror(errno));
186 ba97b2d7 2024-03-20 stsp closefrom(STDERR_FILENO + 1);
188 ba97b2d7 2024-03-20 stsp if (execv(prog, (char *const *)argv) == -1) {
189 ba97b2d7 2024-03-20 stsp fprintf(stderr, "%s: exec %s: %s\n", getprogname(),
190 ba97b2d7 2024-03-20 stsp prog, strerror(errno));
194 ba97b2d7 2024-03-20 stsp /* not reached */
197 ba97b2d7 2024-03-20 stsp if (waitpid(pid, &child_status, 0) == -1) {
198 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("waitpid");
202 ba97b2d7 2024-03-20 stsp if (!WIFEXITED(child_status)) {
203 ba97b2d7 2024-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_DIED);
207 ba97b2d7 2024-03-20 stsp if (WEXITSTATUS(child_status) != 0)
208 ba97b2d7 2024-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_EXIT);
211 ba97b2d7 2024-03-20 stsp log_warnx("%s: child %s pid %d: %s", gotd_notify.title,
212 ba97b2d7 2024-03-20 stsp prog, pid, err->msg);
215 ba97b2d7 2024-03-20 stsp static void
216 ba97b2d7 2024-03-20 stsp notify_email(struct gotd_notification_target *target, const char *subject_line,
219 ba97b2d7 2024-03-20 stsp const char *argv[13];
222 ba97b2d7 2024-03-20 stsp argv[i++] = GOTD_PATH_PROG_NOTIFY_EMAIL;
224 ba97b2d7 2024-03-20 stsp argv[i++] = "-f";
225 ba97b2d7 2024-03-20 stsp if (target->conf.email.sender)
226 ba97b2d7 2024-03-20 stsp argv[i++] = target->conf.email.sender;
228 ba97b2d7 2024-03-20 stsp argv[i++] = gotd_notify.default_sender;
230 ba97b2d7 2024-03-20 stsp if (target->conf.email.responder) {
231 ba97b2d7 2024-03-20 stsp argv[i++] = "-r";
232 ba97b2d7 2024-03-20 stsp argv[i++] = target->conf.email.responder;
235 ba97b2d7 2024-03-20 stsp if (target->conf.email.hostname) {
236 ba97b2d7 2024-03-20 stsp argv[i++] = "-h";
237 ba97b2d7 2024-03-20 stsp argv[i++] = target->conf.email.hostname;
240 ba97b2d7 2024-03-20 stsp if (target->conf.email.port) {
241 ba97b2d7 2024-03-20 stsp argv[i++] = "-p";
242 ba97b2d7 2024-03-20 stsp argv[i++] = target->conf.email.port;
245 ba97b2d7 2024-03-20 stsp argv[i++] = "-s";
246 ba97b2d7 2024-03-20 stsp argv[i++] = subject_line;
248 ba97b2d7 2024-03-20 stsp argv[i++] = target->conf.email.recipient;
250 ba97b2d7 2024-03-20 stsp argv[i] = NULL;
252 ba97b2d7 2024-03-20 stsp run_notification_helper(GOTD_PATH_PROG_NOTIFY_EMAIL, argv, fd);
255 ba97b2d7 2024-03-20 stsp static void
256 5565365c 2024-03-27 op notify_http(struct gotd_notification_target *target, int fd)
258 5565365c 2024-03-27 op const char *argv[8];
259 5565365c 2024-03-27 op int argc = 0;
261 5565365c 2024-03-27 op argv[argc++] = GOTD_PATH_PROG_NOTIFY_HTTP;
262 5565365c 2024-03-27 op if (target->conf.http.tls)
263 5565365c 2024-03-27 op argv[argc++] = "-c";
265 5565365c 2024-03-27 op argv[argc++] = "-h";
266 5565365c 2024-03-27 op argv[argc++] = target->conf.http.hostname;
267 5565365c 2024-03-27 op argv[argc++] = "-p";
268 5565365c 2024-03-27 op argv[argc++] = target->conf.http.port;
270 5565365c 2024-03-27 op argv[argc++] = target->conf.http.path;
272 5565365c 2024-03-27 op argv[argc] = NULL;
274 ba97b2d7 2024-03-20 stsp run_notification_helper(GOTD_PATH_PROG_NOTIFY_HTTP, argv, fd);
277 ba97b2d7 2024-03-20 stsp static const struct got_error *
278 ba97b2d7 2024-03-20 stsp send_notification(struct imsg *imsg, struct gotd_imsgev *iev)
280 ba97b2d7 2024-03-20 stsp const struct got_error *err = NULL;
281 ba97b2d7 2024-03-20 stsp struct gotd_imsg_notify inotify;
282 ba97b2d7 2024-03-20 stsp size_t datalen;
283 ba97b2d7 2024-03-20 stsp struct gotd_repo *repo;
284 ba97b2d7 2024-03-20 stsp struct gotd_notification_target *target;
287 ba97b2d7 2024-03-20 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
288 ba97b2d7 2024-03-20 stsp if (datalen != sizeof(inotify))
289 ba97b2d7 2024-03-20 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
291 ba97b2d7 2024-03-20 stsp memcpy(&inotify, imsg->data, datalen);
293 ba97b2d7 2024-03-20 stsp repo = gotd_find_repo_by_name(inotify.repo_name, gotd_notify.repos);
294 ba97b2d7 2024-03-20 stsp if (repo == NULL)
295 ba97b2d7 2024-03-20 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
297 ba97b2d7 2024-03-20 stsp fd = imsg_get_fd(imsg);
298 ba97b2d7 2024-03-20 stsp if (fd == -1)
299 ba97b2d7 2024-03-20 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
301 ba97b2d7 2024-03-20 stsp if (lseek(fd, 0, SEEK_SET) == -1) {
302 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("lseek");
306 ba97b2d7 2024-03-20 stsp STAILQ_FOREACH(target, &repo->notification_targets, entry) {
307 ba97b2d7 2024-03-20 stsp switch (target->type) {
308 ba97b2d7 2024-03-20 stsp case GOTD_NOTIFICATION_VIA_EMAIL:
309 ba97b2d7 2024-03-20 stsp notify_email(target, inotify.subject_line, fd);
311 ba97b2d7 2024-03-20 stsp case GOTD_NOTIFICATION_VIA_HTTP:
312 5565365c 2024-03-27 op notify_http(target, fd);
317 ba97b2d7 2024-03-20 stsp if (gotd_imsg_compose_event(iev, GOTD_IMSG_NOTIFICATION_SENT,
318 ba97b2d7 2024-03-20 stsp PROC_NOTIFY, -1, NULL, 0) == -1) {
319 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("imsg compose NOTIFY");
324 ba97b2d7 2024-03-20 stsp return err;
327 ba97b2d7 2024-03-20 stsp static void
328 ba97b2d7 2024-03-20 stsp notify_dispatch_session(int fd, short event, void *arg)
330 ba97b2d7 2024-03-20 stsp struct gotd_imsgev *iev = arg;
331 ba97b2d7 2024-03-20 stsp struct imsgbuf *ibuf = &iev->ibuf;
333 ba97b2d7 2024-03-20 stsp int shut = 0;
334 ba97b2d7 2024-03-20 stsp struct imsg imsg;
336 ba97b2d7 2024-03-20 stsp if (event & EV_READ) {
337 ba97b2d7 2024-03-20 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
338 ba97b2d7 2024-03-20 stsp fatal("imsg_read error");
339 ba97b2d7 2024-03-20 stsp if (n == 0) {
340 ba97b2d7 2024-03-20 stsp /* Connection closed. */
346 ba97b2d7 2024-03-20 stsp if (event & EV_WRITE) {
347 ba97b2d7 2024-03-20 stsp n = msgbuf_write(&ibuf->w);
348 ba97b2d7 2024-03-20 stsp if (n == -1 && errno != EAGAIN)
349 ba97b2d7 2024-03-20 stsp fatal("msgbuf_write");
350 ba97b2d7 2024-03-20 stsp if (n == 0) {
351 ba97b2d7 2024-03-20 stsp /* Connection closed. */
358 ba97b2d7 2024-03-20 stsp const struct got_error *err = NULL;
360 ba97b2d7 2024-03-20 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
361 ba97b2d7 2024-03-20 stsp fatal("%s: imsg_get error", __func__);
362 ba97b2d7 2024-03-20 stsp if (n == 0) /* No more messages. */
365 ba97b2d7 2024-03-20 stsp switch (imsg.hdr.type) {
366 ba97b2d7 2024-03-20 stsp case GOTD_IMSG_NOTIFY:
367 ba97b2d7 2024-03-20 stsp err = send_notification(&imsg, iev);
370 ba97b2d7 2024-03-20 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
373 ba97b2d7 2024-03-20 stsp imsg_free(&imsg);
376 ba97b2d7 2024-03-20 stsp log_warnx("%s: %s", __func__, err->msg);
379 ba97b2d7 2024-03-20 stsp if (!shut) {
380 ba97b2d7 2024-03-20 stsp gotd_imsg_event_add(iev);
382 ba97b2d7 2024-03-20 stsp struct gotd_notify_session *session;
384 ba97b2d7 2024-03-20 stsp /* This pipe is dead. Remove its event handler */
385 ba97b2d7 2024-03-20 stsp event_del(&iev->ev);
386 ba97b2d7 2024-03-20 stsp imsg_clear(&iev->ibuf);
388 ba97b2d7 2024-03-20 stsp session = find_session_by_fd(fd);
389 ba97b2d7 2024-03-20 stsp if (session)
390 ba97b2d7 2024-03-20 stsp remove_session(session);
394 ba97b2d7 2024-03-20 stsp static const struct got_error *
395 ba97b2d7 2024-03-20 stsp recv_session(struct imsg *imsg)
397 ba97b2d7 2024-03-20 stsp struct gotd_notify_session *session;
398 ba97b2d7 2024-03-20 stsp size_t datalen;
401 ba97b2d7 2024-03-20 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
402 ba97b2d7 2024-03-20 stsp if (datalen != 0)
403 ba97b2d7 2024-03-20 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
405 ba97b2d7 2024-03-20 stsp fd = imsg_get_fd(imsg);
406 ba97b2d7 2024-03-20 stsp if (fd == -1)
407 ba97b2d7 2024-03-20 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
409 ba97b2d7 2024-03-20 stsp session = calloc(1, sizeof(*session));
410 ba97b2d7 2024-03-20 stsp if (session == NULL)
411 ba97b2d7 2024-03-20 stsp return got_error_from_errno("calloc");
413 ba97b2d7 2024-03-20 stsp session->id = get_session_id();
414 ba97b2d7 2024-03-20 stsp imsg_init(&session->iev.ibuf, fd);
415 ba97b2d7 2024-03-20 stsp session->iev.handler = notify_dispatch_session;
416 ba97b2d7 2024-03-20 stsp session->iev.events = EV_READ;
417 ba97b2d7 2024-03-20 stsp session->iev.handler_arg = NULL;
418 ba97b2d7 2024-03-20 stsp event_set(&session->iev.ev, session->iev.ibuf.fd, EV_READ,
419 ba97b2d7 2024-03-20 stsp notify_dispatch_session, &session->iev);
420 ba97b2d7 2024-03-20 stsp gotd_imsg_event_add(&session->iev);
421 ba97b2d7 2024-03-20 stsp add_session(session);
423 ba97b2d7 2024-03-20 stsp return NULL;
426 ba97b2d7 2024-03-20 stsp static void
427 ba97b2d7 2024-03-20 stsp notify_dispatch(int fd, short event, void *arg)
429 ba97b2d7 2024-03-20 stsp struct gotd_imsgev *iev = arg;
430 ba97b2d7 2024-03-20 stsp struct imsgbuf *ibuf = &iev->ibuf;
432 ba97b2d7 2024-03-20 stsp int shut = 0;
433 ba97b2d7 2024-03-20 stsp struct imsg imsg;
435 ba97b2d7 2024-03-20 stsp if (event & EV_READ) {
436 ba97b2d7 2024-03-20 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
437 ba97b2d7 2024-03-20 stsp fatal("imsg_read error");
438 ba97b2d7 2024-03-20 stsp if (n == 0) {
439 ba97b2d7 2024-03-20 stsp /* Connection closed. */
445 ba97b2d7 2024-03-20 stsp if (event & EV_WRITE) {
446 ba97b2d7 2024-03-20 stsp n = msgbuf_write(&ibuf->w);
447 ba97b2d7 2024-03-20 stsp if (n == -1 && errno != EAGAIN)
448 ba97b2d7 2024-03-20 stsp fatal("msgbuf_write");
449 ba97b2d7 2024-03-20 stsp if (n == 0) {
450 ba97b2d7 2024-03-20 stsp /* Connection closed. */
457 ba97b2d7 2024-03-20 stsp const struct got_error *err = NULL;
459 ba97b2d7 2024-03-20 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
460 ba97b2d7 2024-03-20 stsp fatal("%s: imsg_get error", __func__);
461 ba97b2d7 2024-03-20 stsp if (n == 0) /* No more messages. */
464 ba97b2d7 2024-03-20 stsp switch (imsg.hdr.type) {
465 ba97b2d7 2024-03-20 stsp case GOTD_IMSG_CONNECT_SESSION:
466 ba97b2d7 2024-03-20 stsp err = recv_session(&imsg);
469 ba97b2d7 2024-03-20 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
472 ba97b2d7 2024-03-20 stsp imsg_free(&imsg);
475 ba97b2d7 2024-03-20 stsp log_warnx("%s: %s", __func__, err->msg);
478 ba97b2d7 2024-03-20 stsp if (!shut) {
479 ba97b2d7 2024-03-20 stsp gotd_imsg_event_add(iev);
481 ba97b2d7 2024-03-20 stsp /* This pipe is dead. Remove its event handler */
482 ba97b2d7 2024-03-20 stsp event_del(&iev->ev);
483 ba97b2d7 2024-03-20 stsp event_loopexit(NULL);
489 ba97b2d7 2024-03-20 stsp notify_main(const char *title, struct gotd_repolist *repos,
490 ba97b2d7 2024-03-20 stsp const char *default_sender)
492 ba97b2d7 2024-03-20 stsp const struct got_error *err = NULL;
493 ba97b2d7 2024-03-20 stsp struct event evsigint, evsigterm, evsighup, evsigusr1;
495 ba97b2d7 2024-03-20 stsp arc4random_buf(&sessions_hash_key, sizeof(sessions_hash_key));
497 ba97b2d7 2024-03-20 stsp gotd_notify.title = title;
498 ba97b2d7 2024-03-20 stsp gotd_notify.repos = repos;
499 ba97b2d7 2024-03-20 stsp gotd_notify.default_sender = default_sender;
500 ba97b2d7 2024-03-20 stsp gotd_notify.pid = getpid();
502 ba97b2d7 2024-03-20 stsp signal_set(&evsigint, SIGINT, gotd_notify_sighdlr, NULL);
503 ba97b2d7 2024-03-20 stsp signal_set(&evsigterm, SIGTERM, gotd_notify_sighdlr, NULL);
504 ba97b2d7 2024-03-20 stsp signal_set(&evsighup, SIGHUP, gotd_notify_sighdlr, NULL);
505 ba97b2d7 2024-03-20 stsp signal_set(&evsigusr1, SIGUSR1, gotd_notify_sighdlr, NULL);
506 ba97b2d7 2024-03-20 stsp signal(SIGPIPE, SIG_IGN);
508 ba97b2d7 2024-03-20 stsp signal_add(&evsigint, NULL);
509 ba97b2d7 2024-03-20 stsp signal_add(&evsigterm, NULL);
510 ba97b2d7 2024-03-20 stsp signal_add(&evsighup, NULL);
511 ba97b2d7 2024-03-20 stsp signal_add(&evsigusr1, NULL);
513 ba97b2d7 2024-03-20 stsp imsg_init(&gotd_notify.parent_iev.ibuf, GOTD_FILENO_MSG_PIPE);
514 ba97b2d7 2024-03-20 stsp gotd_notify.parent_iev.handler = notify_dispatch;
515 ba97b2d7 2024-03-20 stsp gotd_notify.parent_iev.events = EV_READ;
516 ba97b2d7 2024-03-20 stsp gotd_notify.parent_iev.handler_arg = NULL;
517 ba97b2d7 2024-03-20 stsp event_set(&gotd_notify.parent_iev.ev, gotd_notify.parent_iev.ibuf.fd,
518 ba97b2d7 2024-03-20 stsp EV_READ, notify_dispatch, &gotd_notify.parent_iev);
519 ba97b2d7 2024-03-20 stsp gotd_imsg_event_add(&gotd_notify.parent_iev);
521 ba97b2d7 2024-03-20 stsp event_dispatch();
524 ba97b2d7 2024-03-20 stsp log_warnx("%s: %s", title, err->msg);
525 ba97b2d7 2024-03-20 stsp gotd_notify_shutdown();
529 ba97b2d7 2024-03-20 stsp gotd_notify_shutdown(void)
531 e8d451cc 2024-03-22 stsp log_debug("%s: shutting down", gotd_notify.title);