Blob


1 /*
2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/queue.h>
18 #include <sys/types.h>
19 #include <sys/uio.h>
21 #include <errno.h>
22 #include <event.h>
23 #include <imsg.h>
24 #include <limits.h>
25 #include <poll.h>
26 #include <sha1.h>
27 #include <sha2.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <unistd.h>
32 #include "got_error.h"
33 #include "got_path.h"
35 #include "got_lib_poll.h"
37 #include "gotd.h"
39 const struct got_error *
40 gotd_imsg_recv_error(uint32_t *client_id, struct imsg *imsg)
41 {
42 struct gotd_imsg_error ierr;
43 size_t datalen;
45 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
46 if (datalen != sizeof(ierr))
47 return got_error(GOT_ERR_PRIVSEP_LEN);
48 memcpy(&ierr, imsg->data, sizeof(ierr));
50 if (client_id)
51 *client_id = ierr.client_id;
53 if (ierr.code == GOT_ERR_ERRNO)
54 errno = ierr.errno_code;
56 return got_error_msg(ierr.code, ierr.msg);
57 }
59 const struct got_error *
60 gotd_imsg_flush(struct imsgbuf *ibuf)
61 {
62 const struct got_error *err = NULL;
64 while (ibuf->w.queued > 0) {
65 err = got_poll_fd(ibuf->fd, POLLOUT, INFTIM);
66 if (err)
67 break;
69 if (imsg_flush(ibuf) == -1) {
70 if (errno != EAGAIN) {
71 imsg_clear(ibuf);
72 err = got_error_from_errno("imsg_flush");
73 break;
74 }
75 }
76 }
78 return err;
79 }
81 const struct got_error *
82 gotd_imsg_recv(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
83 {
84 ssize_t n;
86 n = imsg_get(ibuf, imsg);
87 if (n == -1)
88 return got_error_from_errno("imsg_get");
90 if (n == 0) {
91 n = imsg_read(ibuf);
92 if (n == -1) {
93 if (errno == EAGAIN)
94 return got_error(GOT_ERR_PRIVSEP_READ);
95 return got_error_from_errno("imsg_read");
96 }
97 if (n == 0)
98 return got_error(GOT_ERR_EOF);
99 n = imsg_get(ibuf, imsg);
100 if (n == -1)
101 return got_error_from_errno("imsg_get");
104 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
105 return got_error(GOT_ERR_PRIVSEP_LEN);
107 return NULL;
110 const struct got_error *
111 gotd_imsg_poll_recv(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
113 const struct got_error *err = NULL;
115 for (;;) {
116 err = gotd_imsg_recv(imsg, ibuf, min_datalen);
117 if (err == NULL || err->code != GOT_ERR_PRIVSEP_READ)
118 return err;
120 err = got_poll_fd(ibuf->fd, POLLIN, INFTIM);
121 if (err)
122 break;
125 return err;
128 int
129 gotd_imsg_send_error(struct imsgbuf *ibuf, uint32_t peerid,
130 uint32_t client_id, const struct got_error *err)
132 const struct got_error *flush_err;
133 struct gotd_imsg_error ierr;
134 int ret;
136 ierr.code = err->code;
137 if (err->code == GOT_ERR_ERRNO)
138 ierr.errno_code = errno;
139 else
140 ierr.errno_code = 0;
141 ierr.client_id = client_id;
142 strlcpy(ierr.msg, err->msg, sizeof(ierr.msg));
144 ret = imsg_compose(ibuf, GOTD_IMSG_ERROR, peerid, getpid(), -1,
145 &ierr, sizeof(ierr));
146 if (ret == -1)
147 return -1;
149 flush_err = gotd_imsg_flush(ibuf);
150 if (flush_err)
151 return -1;
153 return 0;
156 int
157 gotd_imsg_send_error_event(struct gotd_imsgev *iev, uint32_t peerid,
158 uint32_t client_id, const struct got_error *err)
160 struct gotd_imsg_error ierr;
161 int ret;
163 ierr.code = err->code;
164 if (err->code == GOT_ERR_ERRNO)
165 ierr.errno_code = errno;
166 else
167 ierr.errno_code = 0;
168 ierr.client_id = client_id;
169 strlcpy(ierr.msg, err->msg, sizeof(ierr.msg));
171 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_ERROR, peerid, -1,
172 &ierr, sizeof(ierr));
173 if (ret == -1)
174 return -1;
176 return 0;
179 void
180 gotd_imsg_event_add(struct gotd_imsgev *iev)
182 iev->events = EV_READ;
183 if (iev->ibuf.w.queued)
184 iev->events |= EV_WRITE;
186 event_del(&iev->ev);
187 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
188 event_add(&iev->ev, NULL);
191 int
192 gotd_imsg_compose_event(struct gotd_imsgev *iev, uint16_t type, uint32_t peerid,
193 int fd, void *data, uint16_t datalen)
195 int ret;
197 ret = imsg_compose(&iev->ibuf, type, peerid, getpid(), fd,
198 data, datalen);
199 if (ret != -1)
200 gotd_imsg_event_add(iev);
202 return ret;
205 int
206 gotd_imsg_forward(struct gotd_imsgev *iev, struct imsg *imsg, int fd)
208 return gotd_imsg_compose_event(iev, imsg->hdr.type, imsg->hdr.peerid,
209 fd, imsg->data, imsg->hdr.len - IMSG_HEADER_SIZE);