Blame


1 3efd8e31 2022-10-23 thomas /*
2 3efd8e31 2022-10-23 thomas * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 3efd8e31 2022-10-23 thomas *
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.
7 3efd8e31 2022-10-23 thomas *
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.
15 3efd8e31 2022-10-23 thomas */
16 3efd8e31 2022-10-23 thomas
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>
20 3efd8e31 2022-10-23 thomas
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>
29 3efd8e31 2022-10-23 thomas
30 3efd8e31 2022-10-23 thomas #include "got_error.h"
31 3efd8e31 2022-10-23 thomas
32 3efd8e31 2022-10-23 thomas #include "got_lib_poll.h"
33 3efd8e31 2022-10-23 thomas
34 3efd8e31 2022-10-23 thomas #include "gotd.h"
35 3efd8e31 2022-10-23 thomas
36 3efd8e31 2022-10-23 thomas const struct got_error *
37 3efd8e31 2022-10-23 thomas gotd_imsg_recv_error(uint32_t *client_id, struct imsg *imsg)
38 3efd8e31 2022-10-23 thomas {
39 3efd8e31 2022-10-23 thomas struct gotd_imsg_error ierr;
40 3efd8e31 2022-10-23 thomas size_t datalen;
41 3efd8e31 2022-10-23 thomas
42 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
43 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ierr))
44 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
45 3efd8e31 2022-10-23 thomas memcpy(&ierr, imsg->data, sizeof(ierr));
46 3efd8e31 2022-10-23 thomas
47 3efd8e31 2022-10-23 thomas if (client_id)
48 3efd8e31 2022-10-23 thomas *client_id = ierr.client_id;
49 3efd8e31 2022-10-23 thomas
50 3efd8e31 2022-10-23 thomas if (ierr.code == GOT_ERR_ERRNO)
51 3efd8e31 2022-10-23 thomas errno = ierr.errno_code;
52 3efd8e31 2022-10-23 thomas
53 3efd8e31 2022-10-23 thomas return got_error_msg(ierr.code, ierr.msg);
54 3efd8e31 2022-10-23 thomas }
55 3efd8e31 2022-10-23 thomas
56 3efd8e31 2022-10-23 thomas const struct got_error *
57 3efd8e31 2022-10-23 thomas gotd_imsg_flush(struct imsgbuf *ibuf)
58 3efd8e31 2022-10-23 thomas {
59 eac60ea1 2022-12-04 thomas const struct got_error *err = NULL;
60 3efd8e31 2022-10-23 thomas
61 eac60ea1 2022-12-04 thomas while (ibuf->w.queued > 0) {
62 eac60ea1 2022-12-04 thomas err = got_poll_fd(ibuf->fd, POLLOUT, INFTIM);
63 eac60ea1 2022-12-04 thomas if (err)
64 eac60ea1 2022-12-04 thomas break;
65 3efd8e31 2022-10-23 thomas
66 eac60ea1 2022-12-04 thomas if (imsg_flush(ibuf) == -1) {
67 eac60ea1 2022-12-04 thomas if (errno != EAGAIN) {
68 eac60ea1 2022-12-04 thomas imsg_clear(ibuf);
69 eac60ea1 2022-12-04 thomas err = got_error_from_errno("imsg_flush");
70 eac60ea1 2022-12-04 thomas break;
71 eac60ea1 2022-12-04 thomas }
72 eac60ea1 2022-12-04 thomas }
73 d4628c48 2023-02-03 thomas }
74 3efd8e31 2022-10-23 thomas
75 eac60ea1 2022-12-04 thomas return err;
76 3efd8e31 2022-10-23 thomas }
77 3efd8e31 2022-10-23 thomas
78 3efd8e31 2022-10-23 thomas const struct got_error *
79 3efd8e31 2022-10-23 thomas gotd_imsg_recv(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
80 3efd8e31 2022-10-23 thomas {
81 3efd8e31 2022-10-23 thomas ssize_t n;
82 3efd8e31 2022-10-23 thomas
83 3efd8e31 2022-10-23 thomas n = imsg_get(ibuf, imsg);
84 3efd8e31 2022-10-23 thomas if (n == -1)
85 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_get");
86 3efd8e31 2022-10-23 thomas
87 3efd8e31 2022-10-23 thomas if (n == 0) {
88 3efd8e31 2022-10-23 thomas n = imsg_read(ibuf);
89 3efd8e31 2022-10-23 thomas if (n == -1) {
90 3efd8e31 2022-10-23 thomas if (errno == EAGAIN)
91 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_READ);
92 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_read");
93 3efd8e31 2022-10-23 thomas }
94 3efd8e31 2022-10-23 thomas if (n == 0)
95 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_EOF);
96 3efd8e31 2022-10-23 thomas n = imsg_get(ibuf, imsg);
97 3efd8e31 2022-10-23 thomas if (n == -1)
98 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_get");
99 3efd8e31 2022-10-23 thomas }
100 3efd8e31 2022-10-23 thomas
101 3efd8e31 2022-10-23 thomas if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
102 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
103 3efd8e31 2022-10-23 thomas
104 3efd8e31 2022-10-23 thomas return NULL;
105 3efd8e31 2022-10-23 thomas }
106 3efd8e31 2022-10-23 thomas
107 3efd8e31 2022-10-23 thomas const struct got_error *
108 3efd8e31 2022-10-23 thomas gotd_imsg_poll_recv(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
109 3efd8e31 2022-10-23 thomas {
110 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
111 3efd8e31 2022-10-23 thomas
112 3efd8e31 2022-10-23 thomas for (;;) {
113 3efd8e31 2022-10-23 thomas err = gotd_imsg_recv(imsg, ibuf, min_datalen);
114 3efd8e31 2022-10-23 thomas if (err == NULL || err->code != GOT_ERR_PRIVSEP_READ)
115 3efd8e31 2022-10-23 thomas return err;
116 3efd8e31 2022-10-23 thomas
117 3efd8e31 2022-10-23 thomas err = got_poll_fd(ibuf->fd, POLLIN, INFTIM);
118 3efd8e31 2022-10-23 thomas if (err)
119 3efd8e31 2022-10-23 thomas break;
120 3efd8e31 2022-10-23 thomas }
121 3efd8e31 2022-10-23 thomas
122 3efd8e31 2022-10-23 thomas return err;
123 3efd8e31 2022-10-23 thomas }
124 3efd8e31 2022-10-23 thomas
125 3efd8e31 2022-10-23 thomas int
126 3efd8e31 2022-10-23 thomas gotd_imsg_send_error(struct imsgbuf *ibuf, uint32_t peerid,
127 3efd8e31 2022-10-23 thomas uint32_t client_id, const struct got_error *err)
128 3efd8e31 2022-10-23 thomas {
129 3efd8e31 2022-10-23 thomas const struct got_error *flush_err;
130 3efd8e31 2022-10-23 thomas struct gotd_imsg_error ierr;
131 3efd8e31 2022-10-23 thomas int ret;
132 3efd8e31 2022-10-23 thomas
133 3efd8e31 2022-10-23 thomas ierr.code = err->code;
134 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_ERRNO)
135 3efd8e31 2022-10-23 thomas ierr.errno_code = errno;
136 3efd8e31 2022-10-23 thomas else
137 3efd8e31 2022-10-23 thomas ierr.errno_code = 0;
138 3efd8e31 2022-10-23 thomas ierr.client_id = client_id;
139 3efd8e31 2022-10-23 thomas strlcpy(ierr.msg, err->msg, sizeof(ierr.msg));
140 3efd8e31 2022-10-23 thomas
141 3efd8e31 2022-10-23 thomas ret = imsg_compose(ibuf, GOTD_IMSG_ERROR, peerid, getpid(), -1,
142 3efd8e31 2022-10-23 thomas &ierr, sizeof(ierr));
143 3efd8e31 2022-10-23 thomas if (ret == -1)
144 3efd8e31 2022-10-23 thomas return -1;
145 3efd8e31 2022-10-23 thomas
146 3efd8e31 2022-10-23 thomas flush_err = gotd_imsg_flush(ibuf);
147 3efd8e31 2022-10-23 thomas if (flush_err)
148 3efd8e31 2022-10-23 thomas return -1;
149 3efd8e31 2022-10-23 thomas
150 3efd8e31 2022-10-23 thomas return 0;
151 3efd8e31 2022-10-23 thomas }
152 3efd8e31 2022-10-23 thomas
153 3efd8e31 2022-10-23 thomas int
154 3efd8e31 2022-10-23 thomas gotd_imsg_send_error_event(struct gotd_imsgev *iev, uint32_t peerid,
155 3efd8e31 2022-10-23 thomas uint32_t client_id, const struct got_error *err)
156 3efd8e31 2022-10-23 thomas {
157 3efd8e31 2022-10-23 thomas struct gotd_imsg_error ierr;
158 3efd8e31 2022-10-23 thomas int ret;
159 3efd8e31 2022-10-23 thomas
160 3efd8e31 2022-10-23 thomas ierr.code = err->code;
161 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_ERRNO)
162 3efd8e31 2022-10-23 thomas ierr.errno_code = errno;
163 3efd8e31 2022-10-23 thomas else
164 3efd8e31 2022-10-23 thomas ierr.errno_code = 0;
165 3efd8e31 2022-10-23 thomas ierr.client_id = client_id;
166 3efd8e31 2022-10-23 thomas strlcpy(ierr.msg, err->msg, sizeof(ierr.msg));
167 3efd8e31 2022-10-23 thomas
168 3efd8e31 2022-10-23 thomas ret = gotd_imsg_compose_event(iev, GOTD_IMSG_ERROR, peerid, -1,
169 3efd8e31 2022-10-23 thomas &ierr, sizeof(ierr));
170 3efd8e31 2022-10-23 thomas if (ret == -1)
171 3efd8e31 2022-10-23 thomas return -1;
172 3efd8e31 2022-10-23 thomas
173 3efd8e31 2022-10-23 thomas return 0;
174 3efd8e31 2022-10-23 thomas }
175 3efd8e31 2022-10-23 thomas
176 3efd8e31 2022-10-23 thomas void
177 3efd8e31 2022-10-23 thomas gotd_imsg_event_add(struct gotd_imsgev *iev)
178 3efd8e31 2022-10-23 thomas {
179 3efd8e31 2022-10-23 thomas iev->events = EV_READ;
180 3efd8e31 2022-10-23 thomas if (iev->ibuf.w.queued)
181 3efd8e31 2022-10-23 thomas iev->events |= EV_WRITE;
182 3efd8e31 2022-10-23 thomas
183 3efd8e31 2022-10-23 thomas event_del(&iev->ev);
184 3efd8e31 2022-10-23 thomas event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
185 3efd8e31 2022-10-23 thomas event_add(&iev->ev, NULL);
186 3efd8e31 2022-10-23 thomas }
187 3efd8e31 2022-10-23 thomas
188 3efd8e31 2022-10-23 thomas int
189 3efd8e31 2022-10-23 thomas gotd_imsg_compose_event(struct gotd_imsgev *iev, uint16_t type, uint32_t peerid,
190 3efd8e31 2022-10-23 thomas int fd, void *data, uint16_t datalen)
191 3efd8e31 2022-10-23 thomas {
192 3efd8e31 2022-10-23 thomas int ret;
193 3efd8e31 2022-10-23 thomas
194 3efd8e31 2022-10-23 thomas ret = imsg_compose(&iev->ibuf, type, peerid, getpid(), fd,
195 3efd8e31 2022-10-23 thomas data, datalen);
196 3efd8e31 2022-10-23 thomas if (ret != -1)
197 3efd8e31 2022-10-23 thomas gotd_imsg_event_add(iev);
198 3efd8e31 2022-10-23 thomas
199 3efd8e31 2022-10-23 thomas return ret;
200 3efd8e31 2022-10-23 thomas }
201 3efd8e31 2022-10-23 thomas
202 3efd8e31 2022-10-23 thomas int
203 3efd8e31 2022-10-23 thomas gotd_imsg_forward(struct gotd_imsgev *iev, struct imsg *imsg, int fd)
204 3efd8e31 2022-10-23 thomas {
205 3efd8e31 2022-10-23 thomas return gotd_imsg_compose_event(iev, imsg->hdr.type, imsg->hdr.peerid,
206 3efd8e31 2022-10-23 thomas fd, imsg->data, imsg->hdr.len - IMSG_HEADER_SIZE);
207 3efd8e31 2022-10-23 thomas }