make got-read-object clear its imsgbuf before exit in an error case
[got-portable.git] / gotd / imsg.c
blobd112297cf0600782055ab0b74611517b0d8dab7b
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/tree.h>
21 #include <sys/types.h>
22 #include <sys/uio.h>
24 #include <errno.h>
25 #include <event.h>
26 #include <imsg.h>
27 #include <limits.h>
28 #include <poll.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <unistd.h>
33 #include "got_error.h"
34 #include "got_object.h"
35 #include "got_path.h"
37 #include "got_lib_poll.h"
39 #include "gotd.h"
41 const struct got_error *
42 gotd_imsg_recv_error(uint32_t *client_id, struct imsg *imsg)
44 struct gotd_imsg_error ierr;
45 size_t datalen;
47 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
48 if (datalen != sizeof(ierr))
49 return got_error(GOT_ERR_PRIVSEP_LEN);
50 memcpy(&ierr, imsg->data, sizeof(ierr));
52 if (client_id)
53 *client_id = ierr.client_id;
55 if (ierr.code == GOT_ERR_ERRNO)
56 errno = ierr.errno_code;
58 return got_error_msg(ierr.code, ierr.msg);
61 const struct got_error *
62 gotd_imsg_flush(struct imsgbuf *ibuf)
64 const struct got_error *err = NULL;
66 while (imsgbuf_queuelen(ibuf) > 0) {
67 err = got_poll_fd(ibuf->fd, POLLOUT, INFTIM);
68 if (err)
69 break;
71 if (imsgbuf_write(ibuf) == -1) {
72 err = got_error_from_errno("imsgbuf_write");
73 break;
77 return err;
80 static const struct got_error *
81 gotd_imsg_recv(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
83 ssize_t n;
85 n = imsg_get(ibuf, imsg);
86 if (n == -1)
87 return got_error_from_errno("imsg_get");
89 if (n == 0) {
90 n = imsgbuf_read(ibuf);
91 if (n == -1)
92 return got_error_from_errno("imsgbuf_read");
93 if (n == 0)
94 return got_error(GOT_ERR_EOF);
95 n = imsg_get(ibuf, imsg);
96 if (n == -1)
97 return got_error_from_errno("imsg_get");
98 if (n == 0)
99 return got_error(GOT_ERR_PRIVSEP_READ);
102 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
103 return got_error(GOT_ERR_PRIVSEP_LEN);
105 return NULL;
108 const struct got_error *
109 gotd_imsg_poll_recv(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
111 const struct got_error *err = NULL;
113 for (;;) {
114 err = gotd_imsg_recv(imsg, ibuf, min_datalen);
115 if (err == NULL || err->code != GOT_ERR_PRIVSEP_READ)
116 return err;
118 err = got_poll_fd(ibuf->fd, POLLIN, INFTIM);
119 if (err)
120 break;
123 return err;
127 gotd_imsg_send_error(struct imsgbuf *ibuf, uint32_t peerid,
128 uint32_t client_id, const struct got_error *err)
130 const struct got_error *flush_err;
131 struct gotd_imsg_error ierr;
132 int ret;
134 ierr.code = err->code;
135 if (err->code == GOT_ERR_ERRNO)
136 ierr.errno_code = errno;
137 else
138 ierr.errno_code = 0;
139 ierr.client_id = client_id;
140 strlcpy(ierr.msg, err->msg, sizeof(ierr.msg));
142 ret = imsg_compose(ibuf, GOTD_IMSG_ERROR, peerid, getpid(), -1,
143 &ierr, sizeof(ierr));
144 if (ret == -1)
145 return -1;
147 flush_err = gotd_imsg_flush(ibuf);
148 if (flush_err)
149 return -1;
151 return 0;
155 gotd_imsg_send_error_event(struct gotd_imsgev *iev, uint32_t peerid,
156 uint32_t client_id, const struct got_error *err)
158 struct gotd_imsg_error ierr;
159 int ret;
161 ierr.code = err->code;
162 if (err->code == GOT_ERR_ERRNO)
163 ierr.errno_code = errno;
164 else
165 ierr.errno_code = 0;
166 ierr.client_id = client_id;
167 strlcpy(ierr.msg, err->msg, sizeof(ierr.msg));
169 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_ERROR, peerid, -1,
170 &ierr, sizeof(ierr));
171 if (ret == -1)
172 return -1;
174 return 0;
177 void
178 gotd_imsg_event_add(struct gotd_imsgev *iev)
180 iev->events = EV_READ;
181 if (imsgbuf_queuelen(&iev->ibuf))
182 iev->events |= EV_WRITE;
184 event_del(&iev->ev);
185 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
186 event_add(&iev->ev, NULL);
190 gotd_imsg_compose_event(struct gotd_imsgev *iev, uint16_t type, uint32_t peerid,
191 int fd, void *data, uint16_t datalen)
193 int ret;
195 ret = imsg_compose(&iev->ibuf, type, peerid, getpid(), fd,
196 data, datalen);
197 if (ret != -1)
198 gotd_imsg_event_add(iev);
200 return ret;
204 gotd_imsg_forward(struct gotd_imsgev *iev, struct imsg *imsg, int fd)
206 return gotd_imsg_compose_event(iev, imsg->hdr.type, imsg->hdr.peerid,
207 fd, imsg->data, imsg->hdr.len - IMSG_HEADER_SIZE);