2 3efd8e31 2022-10-23 thomas * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
4 3efd8e31 2022-10-23 thomas * Permission to use, copy, modify, and distribute this software for any
5 3efd8e31 2022-10-23 thomas * purpose with or without fee is hereby granted, provided that the above
6 3efd8e31 2022-10-23 thomas * copyright notice and this permission notice appear in all copies.
8 3efd8e31 2022-10-23 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3efd8e31 2022-10-23 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3efd8e31 2022-10-23 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3efd8e31 2022-10-23 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3efd8e31 2022-10-23 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3efd8e31 2022-10-23 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3efd8e31 2022-10-23 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 3efd8e31 2022-10-23 thomas #include <sys/queue.h>
18 3efd8e31 2022-10-23 thomas #include <sys/types.h>
19 3efd8e31 2022-10-23 thomas #include <sys/uio.h>
21 3efd8e31 2022-10-23 thomas #include <errno.h>
22 3efd8e31 2022-10-23 thomas #include <event.h>
23 3efd8e31 2022-10-23 thomas #include <imsg.h>
24 3efd8e31 2022-10-23 thomas #include <limits.h>
25 3efd8e31 2022-10-23 thomas #include <poll.h>
26 3efd8e31 2022-10-23 thomas #include <stdio.h>
27 3efd8e31 2022-10-23 thomas #include <string.h>
28 3efd8e31 2022-10-23 thomas #include <unistd.h>
30 3efd8e31 2022-10-23 thomas #include "got_error.h"
31 6d7eb4f7 2023-04-04 thomas #include "got_path.h"
33 3efd8e31 2022-10-23 thomas #include "got_lib_poll.h"
35 3efd8e31 2022-10-23 thomas #include "gotd.h"
37 3efd8e31 2022-10-23 thomas const struct got_error *
38 3efd8e31 2022-10-23 thomas gotd_imsg_recv_error(uint32_t *client_id, struct imsg *imsg)
40 3efd8e31 2022-10-23 thomas struct gotd_imsg_error ierr;
41 3efd8e31 2022-10-23 thomas size_t datalen;
43 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
44 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ierr))
45 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
46 3efd8e31 2022-10-23 thomas memcpy(&ierr, imsg->data, sizeof(ierr));
48 3efd8e31 2022-10-23 thomas if (client_id)
49 3efd8e31 2022-10-23 thomas *client_id = ierr.client_id;
51 3efd8e31 2022-10-23 thomas if (ierr.code == GOT_ERR_ERRNO)
52 3efd8e31 2022-10-23 thomas errno = ierr.errno_code;
54 3efd8e31 2022-10-23 thomas return got_error_msg(ierr.code, ierr.msg);
57 3efd8e31 2022-10-23 thomas const struct got_error *
58 3efd8e31 2022-10-23 thomas gotd_imsg_flush(struct imsgbuf *ibuf)
60 eac60ea1 2022-12-04 thomas const struct got_error *err = NULL;
62 eac60ea1 2022-12-04 thomas while (ibuf->w.queued > 0) {
63 eac60ea1 2022-12-04 thomas err = got_poll_fd(ibuf->fd, POLLOUT, INFTIM);
67 eac60ea1 2022-12-04 thomas if (imsg_flush(ibuf) == -1) {
68 eac60ea1 2022-12-04 thomas if (errno != EAGAIN) {
69 eac60ea1 2022-12-04 thomas imsg_clear(ibuf);
70 eac60ea1 2022-12-04 thomas err = got_error_from_errno("imsg_flush");
76 eac60ea1 2022-12-04 thomas return err;
79 3efd8e31 2022-10-23 thomas const struct got_error *
80 3efd8e31 2022-10-23 thomas gotd_imsg_recv(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
82 3efd8e31 2022-10-23 thomas ssize_t n;
84 3efd8e31 2022-10-23 thomas n = imsg_get(ibuf, imsg);
85 3efd8e31 2022-10-23 thomas if (n == -1)
86 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_get");
88 3efd8e31 2022-10-23 thomas if (n == 0) {
89 3efd8e31 2022-10-23 thomas n = imsg_read(ibuf);
90 3efd8e31 2022-10-23 thomas if (n == -1) {
91 3efd8e31 2022-10-23 thomas if (errno == EAGAIN)
92 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_READ);
93 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_read");
95 3efd8e31 2022-10-23 thomas if (n == 0)
96 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_EOF);
97 3efd8e31 2022-10-23 thomas n = imsg_get(ibuf, imsg);
98 3efd8e31 2022-10-23 thomas if (n == -1)
99 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_get");
102 3efd8e31 2022-10-23 thomas if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
103 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
105 3efd8e31 2022-10-23 thomas return NULL;
108 3efd8e31 2022-10-23 thomas const struct got_error *
109 3efd8e31 2022-10-23 thomas gotd_imsg_poll_recv(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
111 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
113 3efd8e31 2022-10-23 thomas for (;;) {
114 3efd8e31 2022-10-23 thomas err = gotd_imsg_recv(imsg, ibuf, min_datalen);
115 3efd8e31 2022-10-23 thomas if (err == NULL || err->code != GOT_ERR_PRIVSEP_READ)
116 3efd8e31 2022-10-23 thomas return err;
118 3efd8e31 2022-10-23 thomas err = got_poll_fd(ibuf->fd, POLLIN, INFTIM);
123 3efd8e31 2022-10-23 thomas return err;
127 3efd8e31 2022-10-23 thomas gotd_imsg_send_error(struct imsgbuf *ibuf, uint32_t peerid,
128 3efd8e31 2022-10-23 thomas uint32_t client_id, const struct got_error *err)
130 3efd8e31 2022-10-23 thomas const struct got_error *flush_err;
131 3efd8e31 2022-10-23 thomas struct gotd_imsg_error ierr;
134 3efd8e31 2022-10-23 thomas ierr.code = err->code;
135 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_ERRNO)
136 3efd8e31 2022-10-23 thomas ierr.errno_code = errno;
138 3efd8e31 2022-10-23 thomas ierr.errno_code = 0;
139 3efd8e31 2022-10-23 thomas ierr.client_id = client_id;
140 3efd8e31 2022-10-23 thomas strlcpy(ierr.msg, err->msg, sizeof(ierr.msg));
142 3efd8e31 2022-10-23 thomas ret = imsg_compose(ibuf, GOTD_IMSG_ERROR, peerid, getpid(), -1,
143 3efd8e31 2022-10-23 thomas &ierr, sizeof(ierr));
144 3efd8e31 2022-10-23 thomas if (ret == -1)
145 3efd8e31 2022-10-23 thomas return -1;
147 3efd8e31 2022-10-23 thomas flush_err = gotd_imsg_flush(ibuf);
148 3efd8e31 2022-10-23 thomas if (flush_err)
149 3efd8e31 2022-10-23 thomas return -1;
151 3efd8e31 2022-10-23 thomas return 0;
155 3efd8e31 2022-10-23 thomas gotd_imsg_send_error_event(struct gotd_imsgev *iev, uint32_t peerid,
156 3efd8e31 2022-10-23 thomas uint32_t client_id, const struct got_error *err)
158 3efd8e31 2022-10-23 thomas struct gotd_imsg_error ierr;
161 3efd8e31 2022-10-23 thomas ierr.code = err->code;
162 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_ERRNO)
163 3efd8e31 2022-10-23 thomas ierr.errno_code = errno;
165 3efd8e31 2022-10-23 thomas ierr.errno_code = 0;
166 3efd8e31 2022-10-23 thomas ierr.client_id = client_id;
167 3efd8e31 2022-10-23 thomas strlcpy(ierr.msg, err->msg, sizeof(ierr.msg));
169 3efd8e31 2022-10-23 thomas ret = gotd_imsg_compose_event(iev, GOTD_IMSG_ERROR, peerid, -1,
170 3efd8e31 2022-10-23 thomas &ierr, sizeof(ierr));
171 3efd8e31 2022-10-23 thomas if (ret == -1)
172 3efd8e31 2022-10-23 thomas return -1;
174 3efd8e31 2022-10-23 thomas return 0;
178 3efd8e31 2022-10-23 thomas gotd_imsg_event_add(struct gotd_imsgev *iev)
180 3efd8e31 2022-10-23 thomas iev->events = EV_READ;
181 3efd8e31 2022-10-23 thomas if (iev->ibuf.w.queued)
182 3efd8e31 2022-10-23 thomas iev->events |= EV_WRITE;
184 3efd8e31 2022-10-23 thomas event_del(&iev->ev);
185 3efd8e31 2022-10-23 thomas event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
186 3efd8e31 2022-10-23 thomas event_add(&iev->ev, NULL);
190 3efd8e31 2022-10-23 thomas gotd_imsg_compose_event(struct gotd_imsgev *iev, uint16_t type, uint32_t peerid,
191 3efd8e31 2022-10-23 thomas int fd, void *data, uint16_t datalen)
195 3efd8e31 2022-10-23 thomas ret = imsg_compose(&iev->ibuf, type, peerid, getpid(), fd,
196 3efd8e31 2022-10-23 thomas data, datalen);
197 3efd8e31 2022-10-23 thomas if (ret != -1)
198 3efd8e31 2022-10-23 thomas gotd_imsg_event_add(iev);
200 3efd8e31 2022-10-23 thomas return ret;
204 3efd8e31 2022-10-23 thomas gotd_imsg_forward(struct gotd_imsgev *iev, struct imsg *imsg, int fd)
206 3efd8e31 2022-10-23 thomas return gotd_imsg_compose_event(iev, imsg->hdr.type, imsg->hdr.peerid,
207 3efd8e31 2022-10-23 thomas fd, imsg->data, imsg->hdr.len - IMSG_HEADER_SIZE);