]> git.proxmox.com Git - mirror_frr.git/blame - lib/imsg.c
lib: inline route_node_lock()/route_node_unlock()
[mirror_frr.git] / lib / imsg.c
CommitLineData
8429abe0
RW
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
eac6e3f0 19#include <zebra.h>
8429abe0 20
eac6e3f0 21#include "openbsd-queue.h"
8429abe0
RW
22#include "imsg.h"
23
52535bee 24int imsg_fd_overhead = 0;
8429abe0 25
52535bee 26int imsg_get_fd(struct imsgbuf *);
8429abe0 27
eac6e3f0
RW
28#ifndef __OpenBSD__
29/*
30 * The original code calls getdtablecount() which is OpenBSD specific. Use
31 * available_fds() from OpenSMTPD instead.
32 */
52535bee
RW
33static int
34available_fds(unsigned int n)
eac6e3f0 35{
52535bee
RW
36 unsigned int i;
37 int ret, fds[256];
eac6e3f0 38
52535bee 39 if (n > (sizeof(fds)/sizeof(fds[0])))
eac6e3f0
RW
40 return (1);
41
42 ret = 0;
43 for (i = 0; i < n; i++) {
44 fds[i] = -1;
45 if ((fds[i] = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
46 if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT)
47 fds[i] = socket(AF_INET6, SOCK_DGRAM, 0);
48 if (fds[i] < 0) {
49 ret = 1;
50 break;
51 }
52 }
53 }
54
55 for (i = 0; i < n && fds[i] >= 0; i++)
56 close(fds[i]);
57
58 return (ret);
59}
60#endif
61
52535bee
RW
62void
63imsg_init(struct imsgbuf *ibuf, int fd)
8429abe0
RW
64{
65 msgbuf_init(&ibuf->w);
66 memset(&ibuf->r, 0, sizeof(ibuf->r));
67 ibuf->fd = fd;
68 ibuf->w.fd = fd;
69 ibuf->pid = getpid();
70 TAILQ_INIT(&ibuf->fds);
71}
72
52535bee
RW
73ssize_t
74imsg_read(struct imsgbuf *ibuf)
8429abe0 75{
52535bee
RW
76 struct msghdr msg;
77 struct cmsghdr *cmsg;
8429abe0
RW
78 union {
79 struct cmsghdr hdr;
52535bee 80 char buf[CMSG_SPACE(sizeof(int) * 1)];
8429abe0 81 } cmsgbuf;
52535bee
RW
82 struct iovec iov;
83 ssize_t n = -1;
84 int fd;
85 struct imsg_fd *ifd;
8429abe0
RW
86
87 memset(&msg, 0, sizeof(msg));
88 memset(&cmsgbuf, 0, sizeof(cmsgbuf));
89
90 iov.iov_base = ibuf->r.buf + ibuf->r.wpos;
91 iov.iov_len = sizeof(ibuf->r.buf) - ibuf->r.wpos;
92 msg.msg_iov = &iov;
93 msg.msg_iovlen = 1;
94 msg.msg_control = &cmsgbuf.buf;
95 msg.msg_controllen = sizeof(cmsgbuf.buf);
96
97 if ((ifd = calloc(1, sizeof(struct imsg_fd))) == NULL)
98 return (-1);
99
100again:
eac6e3f0 101#ifdef __OpenBSD__
52535bee
RW
102 if (getdtablecount() + imsg_fd_overhead +
103 (int)((CMSG_SPACE(sizeof(int))-CMSG_SPACE(0))/sizeof(int))
8429abe0 104 >= getdtablesize()) {
eac6e3f0 105#else
52535bee
RW
106 if (available_fds(imsg_fd_overhead +
107 (CMSG_SPACE(sizeof(int))-CMSG_SPACE(0))/sizeof(int))) {
eac6e3f0 108#endif
8429abe0
RW
109 errno = EAGAIN;
110 free(ifd);
111 return (-1);
112 }
113
114 if ((n = recvmsg(ibuf->fd, &msg, 0)) == -1) {
115 if (errno == EINTR)
116 goto again;
117 goto fail;
118 }
119
120 ibuf->r.wpos += n;
121
122 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
52535bee
RW
123 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
124 if (cmsg->cmsg_level == SOL_SOCKET &&
125 cmsg->cmsg_type == SCM_RIGHTS) {
8429abe0
RW
126 int i;
127 int j;
128
129 /*
130 * We only accept one file descriptor. Due to C
131 * padding rules, our control buffer might contain
132 * more than one fd, and we must close them.
133 */
52535bee
RW
134 j = ((char *)cmsg + cmsg->cmsg_len -
135 (char *)CMSG_DATA(cmsg)) / sizeof(int);
8429abe0
RW
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,
52535bee 141 entry);
8429abe0
RW
142 ifd = NULL;
143 } else
144 close(fd);
145 }
146 }
147 /* we do not handle other ctl data level */
148 }
149
150fail:
151 free(ifd);
152 return (n);
153}
154
52535bee
RW
155ssize_t
156imsg_get(struct imsgbuf *ibuf, struct imsg *imsg)
8429abe0 157{
52535bee 158 size_t av, left, datalen;
8429abe0
RW
159
160 av = ibuf->r.wpos;
161
162 if (IMSG_HEADER_SIZE > av)
163 return (0);
164
165 memcpy(&imsg->hdr, ibuf->r.buf, sizeof(imsg->hdr));
52535bee
RW
166 if (imsg->hdr.len < IMSG_HEADER_SIZE ||
167 imsg->hdr.len > MAX_IMSGSIZE) {
8429abe0
RW
168 errno = ERANGE;
169 return (-1);
170 }
171 if (imsg->hdr.len > av)
172 return (0);
173 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
174 ibuf->r.rptr = ibuf->r.buf + IMSG_HEADER_SIZE;
175 if (datalen == 0)
176 imsg->data = NULL;
177 else if ((imsg->data = malloc(datalen)) == NULL)
178 return (-1);
179
180 if (imsg->hdr.flags & IMSGF_HASFD)
181 imsg->fd = imsg_get_fd(ibuf);
182 else
183 imsg->fd = -1;
184
dd3415b7 185 if (imsg->data)
52535bee 186 memcpy(imsg->data, ibuf->r.rptr, datalen);
8429abe0
RW
187
188 if (imsg->hdr.len < av) {
189 left = av - imsg->hdr.len;
190 memmove(&ibuf->r.buf, ibuf->r.buf + imsg->hdr.len, left);
191 ibuf->r.wpos = left;
192 } else
193 ibuf->r.wpos = 0;
194
195 return (datalen + IMSG_HEADER_SIZE);
196}
197
52535bee
RW
198int
199imsg_compose(struct imsgbuf *ibuf, u_int32_t type, u_int32_t peerid,
200 pid_t pid, int fd, const void *data, u_int16_t datalen)
8429abe0 201{
52535bee 202 struct ibuf *wbuf;
8429abe0
RW
203
204 if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
205 return (-1);
206
207 if (imsg_add(wbuf, data, datalen) == -1)
208 return (-1);
209
210 wbuf->fd = fd;
211
212 imsg_close(ibuf, wbuf);
213
214 return (1);
215}
216
52535bee
RW
217int
218imsg_composev(struct imsgbuf *ibuf, u_int32_t type, u_int32_t peerid,
219 pid_t pid, int fd, const struct iovec *iov, int iovcnt)
8429abe0 220{
52535bee
RW
221 struct ibuf *wbuf;
222 int i, datalen = 0;
8429abe0
RW
223
224 for (i = 0; i < iovcnt; i++)
225 datalen += iov[i].iov_len;
226
227 if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
228 return (-1);
229
230 for (i = 0; i < iovcnt; i++)
231 if (imsg_add(wbuf, iov[i].iov_base, iov[i].iov_len) == -1)
232 return (-1);
233
234 wbuf->fd = fd;
235
236 imsg_close(ibuf, wbuf);
237
238 return (1);
239}
240
241/* ARGSUSED */
52535bee
RW
242struct ibuf *
243imsg_create(struct imsgbuf *ibuf, u_int32_t type, u_int32_t peerid,
244 pid_t pid, u_int16_t datalen)
8429abe0 245{
52535bee
RW
246 struct ibuf *wbuf;
247 struct imsg_hdr hdr;
8429abe0
RW
248
249 datalen += IMSG_HEADER_SIZE;
250 if (datalen > MAX_IMSGSIZE) {
251 errno = ERANGE;
252 return (NULL);
253 }
254
255 hdr.type = type;
256 hdr.flags = 0;
257 hdr.peerid = peerid;
258 if ((hdr.pid = pid) == 0)
259 hdr.pid = ibuf->pid;
260 if ((wbuf = ibuf_dynamic(datalen, MAX_IMSGSIZE)) == NULL) {
261 return (NULL);
262 }
263 if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1)
264 return (NULL);
265
266 return (wbuf);
267}
268
52535bee
RW
269int
270imsg_add(struct ibuf *msg, const void *data, u_int16_t datalen)
8429abe0
RW
271{
272 if (datalen)
273 if (ibuf_add(msg, data, datalen) == -1) {
274 ibuf_free(msg);
275 return (-1);
276 }
277 return (datalen);
278}
279
52535bee
RW
280void
281imsg_close(struct imsgbuf *ibuf, struct ibuf *msg)
8429abe0 282{
52535bee 283 struct imsg_hdr *hdr;
8429abe0
RW
284
285 hdr = (struct imsg_hdr *)msg->buf;
286
287 hdr->flags &= ~IMSGF_HASFD;
288 if (msg->fd != -1)
289 hdr->flags |= IMSGF_HASFD;
290
291 hdr->len = (u_int16_t)msg->wpos;
292
293 ibuf_close(&ibuf->w, msg);
294}
295
52535bee
RW
296void
297imsg_free(struct imsg *imsg)
8429abe0
RW
298{
299 free(imsg->data);
300}
301
52535bee
RW
302int
303imsg_get_fd(struct imsgbuf *ibuf)
8429abe0 304{
52535bee
RW
305 int fd;
306 struct imsg_fd *ifd;
8429abe0
RW
307
308 if ((ifd = TAILQ_FIRST(&ibuf->fds)) == NULL)
309 return (-1);
310
311 fd = ifd->fd;
312 TAILQ_REMOVE(&ibuf->fds, ifd, entry);
313 free(ifd);
314
315 return (fd);
316}
317
52535bee
RW
318int
319imsg_flush(struct imsgbuf *ibuf)
8429abe0
RW
320{
321 while (ibuf->w.queued)
322 if (msgbuf_write(&ibuf->w) <= 0)
323 return (-1);
324 return (0);
325}
326
52535bee
RW
327void
328imsg_clear(struct imsgbuf *ibuf)
8429abe0 329{
52535bee 330 int fd;
8429abe0
RW
331
332 msgbuf_clear(&ibuf->w);
333 while ((fd = imsg_get_fd(ibuf)) != -1)
334 close(fd);
335}