]> git.proxmox.com Git - mirror_frr.git/blob - lib/imsg.c
Merge pull request #2035 from vincentbernat/fix/no-etag-esi-ignore
[mirror_frr.git] / lib / imsg.c
1 /* $OpenBSD$ */
2
3 /*
4 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <zebra.h>
20
21 #include "queue.h"
22 #include "imsg.h"
23
24 int imsg_fd_overhead = 0;
25
26 int imsg_get_fd(struct imsgbuf *);
27
28 #ifndef __OpenBSD__
29 /*
30 * The original code calls getdtablecount() which is OpenBSD specific. Use
31 * available_fds() from OpenSMTPD instead.
32 */
33 static int available_fds(unsigned int n)
34 {
35 unsigned int i;
36 int ret, fds[256];
37
38 if (n > (sizeof(fds) / sizeof(fds[0])))
39 return (1);
40
41 ret = 0;
42 for (i = 0; i < n; i++) {
43 fds[i] = -1;
44 if ((fds[i] = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
45 if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT)
46 fds[i] = socket(AF_INET6, SOCK_DGRAM, 0);
47 if (fds[i] < 0) {
48 ret = 1;
49 break;
50 }
51 }
52 }
53
54 for (i = 0; i < n && fds[i] >= 0; i++)
55 close(fds[i]);
56
57 return (ret);
58 }
59 #endif
60
61 void imsg_init(struct imsgbuf *ibuf, int fd)
62 {
63 msgbuf_init(&ibuf->w);
64 memset(&ibuf->r, 0, sizeof(ibuf->r));
65 ibuf->fd = fd;
66 ibuf->w.fd = fd;
67 ibuf->pid = getpid();
68 TAILQ_INIT(&ibuf->fds);
69 }
70
71 ssize_t imsg_read(struct imsgbuf *ibuf)
72 {
73 struct msghdr msg;
74 struct cmsghdr *cmsg;
75 union {
76 struct cmsghdr hdr;
77 char buf[CMSG_SPACE(sizeof(int) * 1)];
78 } cmsgbuf;
79 struct iovec iov;
80 ssize_t n = -1;
81 int fd;
82 struct imsg_fd *ifd;
83
84 memset(&msg, 0, sizeof(msg));
85 memset(&cmsgbuf, 0, sizeof(cmsgbuf));
86
87 iov.iov_base = ibuf->r.buf + ibuf->r.wpos;
88 iov.iov_len = sizeof(ibuf->r.buf) - ibuf->r.wpos;
89 msg.msg_iov = &iov;
90 msg.msg_iovlen = 1;
91 msg.msg_control = &cmsgbuf.buf;
92 msg.msg_controllen = sizeof(cmsgbuf.buf);
93
94 if ((ifd = calloc(1, sizeof(struct imsg_fd))) == NULL)
95 return (-1);
96
97 again:
98 #ifdef __OpenBSD__
99 if (getdtablecount() + imsg_fd_overhead
100 + (int)((CMSG_SPACE(sizeof(int)) - CMSG_SPACE(0))
101 / sizeof(int))
102 >= getdtablesize()) {
103 #else
104 if (available_fds(imsg_fd_overhead
105 + (CMSG_SPACE(sizeof(int)) - CMSG_SPACE(0))
106 / sizeof(int))) {
107 #endif
108 errno = EAGAIN;
109 free(ifd);
110 return (-1);
111 }
112
113 if ((n = recvmsg(ibuf->fd, &msg, 0)) == -1) {
114 if (errno == EINTR)
115 goto again;
116 goto fail;
117 }
118
119 ibuf->r.wpos += n;
120
121 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
122 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
123 if (cmsg->cmsg_level == SOL_SOCKET
124 && cmsg->cmsg_type == SCM_RIGHTS) {
125 int i;
126 int j;
127
128 /*
129 * We only accept one file descriptor. Due to C
130 * padding rules, our control buffer might contain
131 * more than one fd, and we must close them.
132 */
133 j = ((char *)cmsg + cmsg->cmsg_len
134 - (char *)CMSG_DATA(cmsg))
135 / sizeof(int);
136 for (i = 0; i < j; i++) {
137 fd = ((int *)CMSG_DATA(cmsg))[i];
138 if (ifd != NULL) {
139 ifd->fd = fd;
140 TAILQ_INSERT_TAIL(&ibuf->fds, ifd,
141 entry);
142 ifd = NULL;
143 } else
144 close(fd);
145 }
146 }
147 /* we do not handle other ctl data level */
148 }
149
150 fail:
151 free(ifd);
152 return (n);
153 }
154
155 ssize_t imsg_get(struct imsgbuf *ibuf, struct imsg *imsg)
156 {
157 size_t av, left, datalen;
158
159 av = ibuf->r.wpos;
160
161 if (IMSG_HEADER_SIZE > av)
162 return (0);
163
164 memcpy(&imsg->hdr, ibuf->r.buf, sizeof(imsg->hdr));
165 if (imsg->hdr.len < IMSG_HEADER_SIZE || imsg->hdr.len > MAX_IMSGSIZE) {
166 errno = ERANGE;
167 return (-1);
168 }
169 if (imsg->hdr.len > av)
170 return (0);
171 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
172 ibuf->r.rptr = ibuf->r.buf + IMSG_HEADER_SIZE;
173 if (datalen == 0)
174 imsg->data = NULL;
175 else if ((imsg->data = malloc(datalen)) == NULL)
176 return (-1);
177
178 if (imsg->hdr.flags & IMSGF_HASFD)
179 imsg->fd = imsg_get_fd(ibuf);
180 else
181 imsg->fd = -1;
182
183 if (imsg->data)
184 memcpy(imsg->data, ibuf->r.rptr, datalen);
185
186 if (imsg->hdr.len < av) {
187 left = av - imsg->hdr.len;
188 memmove(&ibuf->r.buf, ibuf->r.buf + imsg->hdr.len, left);
189 ibuf->r.wpos = left;
190 } else
191 ibuf->r.wpos = 0;
192
193 return (datalen + IMSG_HEADER_SIZE);
194 }
195
196 int imsg_compose(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid,
197 pid_t pid, int fd, const void *data, uint16_t datalen)
198 {
199 struct ibuf *wbuf;
200
201 if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
202 return (-1);
203
204 if (imsg_add(wbuf, data, datalen) == -1)
205 return (-1);
206
207 wbuf->fd = fd;
208
209 imsg_close(ibuf, wbuf);
210
211 return (1);
212 }
213
214 int imsg_composev(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid,
215 pid_t pid, int fd, const struct iovec *iov, int iovcnt)
216 {
217 struct ibuf *wbuf;
218 int i, datalen = 0;
219
220 for (i = 0; i < iovcnt; i++)
221 datalen += iov[i].iov_len;
222
223 if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
224 return (-1);
225
226 for (i = 0; i < iovcnt; i++)
227 if (imsg_add(wbuf, iov[i].iov_base, iov[i].iov_len) == -1)
228 return (-1);
229
230 wbuf->fd = fd;
231
232 imsg_close(ibuf, wbuf);
233
234 return (1);
235 }
236
237 /* ARGSUSED */
238 struct ibuf *imsg_create(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid,
239 pid_t pid, uint16_t datalen)
240 {
241 struct ibuf *wbuf;
242 struct imsg_hdr hdr;
243
244 memset(&hdr, 0x00, IMSG_HEADER_SIZE);
245
246 datalen += IMSG_HEADER_SIZE;
247 if (datalen > MAX_IMSGSIZE) {
248 errno = ERANGE;
249 return (NULL);
250 }
251
252 hdr.type = type;
253 hdr.flags = 0;
254 hdr.peerid = peerid;
255 if ((hdr.pid = pid) == 0)
256 hdr.pid = ibuf->pid;
257 if ((wbuf = ibuf_dynamic(datalen, MAX_IMSGSIZE)) == NULL) {
258 return (NULL);
259 }
260 if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1)
261 return (NULL);
262
263 return (wbuf);
264 }
265
266 int imsg_add(struct ibuf *msg, const void *data, uint16_t datalen)
267 {
268 if (datalen)
269 if (ibuf_add(msg, data, datalen) == -1) {
270 ibuf_free(msg);
271 return (-1);
272 }
273 return (datalen);
274 }
275
276 void imsg_close(struct imsgbuf *ibuf, struct ibuf *msg)
277 {
278 struct imsg_hdr *hdr;
279
280 hdr = (struct imsg_hdr *)msg->buf;
281
282 hdr->flags &= ~IMSGF_HASFD;
283 if (msg->fd != -1)
284 hdr->flags |= IMSGF_HASFD;
285
286 hdr->len = (uint16_t)msg->wpos;
287
288 ibuf_close(&ibuf->w, msg);
289 }
290
291 void imsg_free(struct imsg *imsg)
292 {
293 free(imsg->data);
294 }
295
296 int imsg_get_fd(struct imsgbuf *ibuf)
297 {
298 int fd;
299 struct imsg_fd *ifd;
300
301 if ((ifd = TAILQ_FIRST(&ibuf->fds)) == NULL)
302 return (-1);
303
304 fd = ifd->fd;
305 TAILQ_REMOVE(&ibuf->fds, ifd, entry);
306 free(ifd);
307
308 return (fd);
309 }
310
311 int imsg_flush(struct imsgbuf *ibuf)
312 {
313 while (ibuf->w.queued)
314 if (msgbuf_write(&ibuf->w) <= 0)
315 return (-1);
316 return (0);
317 }
318
319 void imsg_clear(struct imsgbuf *ibuf)
320 {
321 int fd;
322
323 msgbuf_clear(&ibuf->w);
324 while ((fd = imsg_get_fd(ibuf)) != -1)
325 close(fd);
326 }