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