fix failure of gotwebd action_patch test on single-digit days of the month
[got-portable.git] / gotd / imsg.c
bloba6c2042797bcb31951c4e2f37c573f0c9ec333bb
1 /*
2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include "got_compat.h"
19 #include <sys/queue.h>
20 #include <sys/types.h>
21 #include <sys/uio.h>
23 #include <errno.h>
24 #include <event.h>
25 #include <imsg.h>
26 #include <limits.h>
27 #include <poll.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <unistd.h>
32 #include "got_error.h"
33 #include "got_object.h"
34 #include "got_path.h"
36 #include "got_lib_poll.h"
38 #include "gotd.h"
40 const struct got_error *
41 gotd_imsg_recv_error(uint32_t *client_id, struct imsg *imsg)
43 struct gotd_imsg_error ierr;
44 size_t datalen;
46 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
47 if (datalen != sizeof(ierr))
48 return got_error(GOT_ERR_PRIVSEP_LEN);
49 memcpy(&ierr, imsg->data, sizeof(ierr));
51 if (client_id)
52 *client_id = ierr.client_id;
54 if (ierr.code == GOT_ERR_ERRNO)
55 errno = ierr.errno_code;
57 return got_error_msg(ierr.code, ierr.msg);
60 const struct got_error *
61 gotd_imsg_flush(struct imsgbuf *ibuf)
63 const struct got_error *err = NULL;
65 while (imsgbuf_queuelen(ibuf) > 0) {
66 err = got_poll_fd(ibuf->fd, POLLOUT, INFTIM);
67 if (err)
68 break;
70 if (imsgbuf_write(ibuf) == -1) {
71 err = got_error_from_errno("imsgbuf_write");
72 break;
76 return err;
79 static const struct got_error *
80 gotd_imsg_recv(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
82 ssize_t n;
84 n = imsg_get(ibuf, imsg);
85 if (n == -1)
86 return got_error_from_errno("imsg_get");
88 if (n == 0) {
89 n = imsgbuf_read(ibuf);
90 if (n == -1)
91 return got_error_from_errno("imsg_read");
92 if (n == 0)
93 return got_error(GOT_ERR_EOF);
94 n = imsg_get(ibuf, imsg);
95 if (n == -1)
96 return got_error_from_errno("imsg_get");
97 if (n == 0)
98 return got_error(GOT_ERR_PRIVSEP_READ);
101 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
102 return got_error(GOT_ERR_PRIVSEP_LEN);
104 return NULL;
107 const struct got_error *
108 gotd_imsg_poll_recv(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
110 const struct got_error *err = NULL;
112 for (;;) {
113 err = gotd_imsg_recv(imsg, ibuf, min_datalen);
114 if (err == NULL || err->code != GOT_ERR_PRIVSEP_READ)
115 return err;
117 err = got_poll_fd(ibuf->fd, POLLIN, INFTIM);
118 if (err)
119 break;
122 return err;
126 gotd_imsg_send_error(struct imsgbuf *ibuf, uint32_t peerid,
127 uint32_t client_id, const struct got_error *err)
129 const struct got_error *flush_err;
130 struct gotd_imsg_error ierr;
131 int ret;
133 ierr.code = err->code;
134 if (err->code == GOT_ERR_ERRNO)
135 ierr.errno_code = errno;
136 else
137 ierr.errno_code = 0;
138 ierr.client_id = client_id;
139 strlcpy(ierr.msg, err->msg, sizeof(ierr.msg));
141 ret = imsg_compose(ibuf, GOTD_IMSG_ERROR, peerid, getpid(), -1,
142 &ierr, sizeof(ierr));
143 if (ret == -1)
144 return -1;
146 flush_err = gotd_imsg_flush(ibuf);
147 if (flush_err)
148 return -1;
150 return 0;
154 gotd_imsg_send_error_event(struct gotd_imsgev *iev, uint32_t peerid,
155 uint32_t client_id, const struct got_error *err)
157 struct gotd_imsg_error ierr;
158 int ret;
160 ierr.code = err->code;
161 if (err->code == GOT_ERR_ERRNO)
162 ierr.errno_code = errno;
163 else
164 ierr.errno_code = 0;
165 ierr.client_id = client_id;
166 strlcpy(ierr.msg, err->msg, sizeof(ierr.msg));
168 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_ERROR, peerid, -1,
169 &ierr, sizeof(ierr));
170 if (ret == -1)
171 return -1;
173 return 0;
176 void
177 gotd_imsg_event_add(struct gotd_imsgev *iev)
179 iev->events = EV_READ;
180 if (imsgbuf_queuelen(&iev->ibuf))
181 iev->events |= EV_WRITE;
183 event_del(&iev->ev);
184 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
185 event_add(&iev->ev, NULL);
189 gotd_imsg_compose_event(struct gotd_imsgev *iev, uint16_t type, uint32_t peerid,
190 int fd, void *data, uint16_t datalen)
192 int ret;
194 ret = imsg_compose(&iev->ibuf, type, peerid, getpid(), fd,
195 data, datalen);
196 if (ret != -1)
197 gotd_imsg_event_add(iev);
199 return ret;
203 gotd_imsg_forward(struct gotd_imsgev *iev, struct imsg *imsg, int fd)
205 return gotd_imsg_compose_event(iev, imsg->hdr.type, imsg->hdr.peerid,
206 fd, imsg->data, imsg->hdr.len - IMSG_HEADER_SIZE);