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_object.h"
34 #include "got_path.h"
36 #include "got_lib_poll.h"
38 #include "gotd.h"
40 const struct got_error *
41 gotd_imsg_recv_error(uint32_t *client_id, struct imsg *imsg)
42 {
43 struct gotd_imsg_error ierr;
44 size_t datalen;
46 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
47 if (datalen != sizeof(ierr))
48 return got_error(GOT_ERR_PRIVSEP_LEN);
49 memcpy(&ierr, imsg->data, sizeof(ierr));
51 if (client_id)
52 *client_id = ierr.client_id;
54 if (ierr.code == GOT_ERR_ERRNO)
55 errno = ierr.errno_code;
57 return got_error_msg(ierr.code, ierr.msg);
58 }
60 const struct got_error *
61 gotd_imsg_flush(struct imsgbuf *ibuf)
62 {
63 const struct got_error *err = NULL;
65 while (ibuf->w.queued > 0) {
66 err = got_poll_fd(ibuf->fd, POLLOUT, INFTIM);
67 if (err)
68 break;
70 if (imsg_flush(ibuf) == -1) {
71 if (errno != EAGAIN) {
72 imsg_clear(ibuf);
73 err = got_error_from_errno("imsg_flush");
74 break;
75 }
76 }
77 }
79 return err;
80 }
82 const struct got_error *
83 gotd_imsg_recv(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
84 {
85 ssize_t n;
87 n = imsg_get(ibuf, imsg);
88 if (n == -1)
89 return got_error_from_errno("imsg_get");
91 if (n == 0) {
92 n = imsg_read(ibuf);
93 if (n == -1) {
94 if (errno == EAGAIN)
95 return got_error(GOT_ERR_PRIVSEP_READ);
96 return got_error_from_errno("imsg_read");
97 }
98 if (n == 0)
99 return got_error(GOT_ERR_EOF);
100 n = imsg_get(ibuf, imsg);
101 if (n == -1)
102 return got_error_from_errno("imsg_get");
105 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
106 return got_error(GOT_ERR_PRIVSEP_LEN);
108 return NULL;
111 const struct got_error *
112 gotd_imsg_poll_recv(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
114 const struct got_error *err = NULL;
116 for (;;) {
117 err = gotd_imsg_recv(imsg, ibuf, min_datalen);
118 if (err == NULL || err->code != GOT_ERR_PRIVSEP_READ)
119 return err;
121 err = got_poll_fd(ibuf->fd, POLLIN, INFTIM);
122 if (err)
123 break;
126 return err;
129 int
130 gotd_imsg_send_error(struct imsgbuf *ibuf, uint32_t peerid,
131 uint32_t client_id, const struct got_error *err)
133 const struct got_error *flush_err;
134 struct gotd_imsg_error ierr;
135 int ret;
137 ierr.code = err->code;
138 if (err->code == GOT_ERR_ERRNO)
139 ierr.errno_code = errno;
140 else
141 ierr.errno_code = 0;
142 ierr.client_id = client_id;
143 strlcpy(ierr.msg, err->msg, sizeof(ierr.msg));
145 ret = imsg_compose(ibuf, GOTD_IMSG_ERROR, peerid, getpid(), -1,
146 &ierr, sizeof(ierr));
147 if (ret == -1)
148 return -1;
150 flush_err = gotd_imsg_flush(ibuf);
151 if (flush_err)
152 return -1;
154 return 0;
157 int
158 gotd_imsg_send_error_event(struct gotd_imsgev *iev, uint32_t peerid,
159 uint32_t client_id, const struct got_error *err)
161 struct gotd_imsg_error ierr;
162 int ret;
164 ierr.code = err->code;
165 if (err->code == GOT_ERR_ERRNO)
166 ierr.errno_code = errno;
167 else
168 ierr.errno_code = 0;
169 ierr.client_id = client_id;
170 strlcpy(ierr.msg, err->msg, sizeof(ierr.msg));
172 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_ERROR, peerid, -1,
173 &ierr, sizeof(ierr));
174 if (ret == -1)
175 return -1;
177 return 0;
180 void
181 gotd_imsg_event_add(struct gotd_imsgev *iev)
183 iev->events = EV_READ;
184 if (iev->ibuf.w.queued)
185 iev->events |= EV_WRITE;
187 event_del(&iev->ev);
188 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
189 event_add(&iev->ev, NULL);
192 int
193 gotd_imsg_compose_event(struct gotd_imsgev *iev, uint16_t type, uint32_t peerid,
194 int fd, void *data, uint16_t datalen)
196 int ret;
198 ret = imsg_compose(&iev->ibuf, type, peerid, getpid(), fd,
199 data, datalen);
200 if (ret != -1)
201 gotd_imsg_event_add(iev);
203 return ret;
206 int
207 gotd_imsg_forward(struct gotd_imsgev *iev, struct imsg *imsg, int fd)
209 return gotd_imsg_compose_event(iev, imsg->hdr.type, imsg->hdr.peerid,
210 fd, imsg->data, imsg->hdr.len - IMSG_HEADER_SIZE);