]> git.proxmox.com Git - mirror_frr.git/blob - lib/imsg.c
Merge branch 'cmaster-next' into vtysh-grammar
[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 "openbsd-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
34 available_fds(unsigned int n)
35 {
36 unsigned int i;
37 int ret, fds[256];
38
39 if (n > (sizeof(fds)/sizeof(fds[0])))
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
62 void
63 imsg_init(struct imsgbuf *ibuf, int fd)
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
73 ssize_t
74 imsg_read(struct imsgbuf *ibuf)
75 {
76 struct msghdr msg;
77 struct cmsghdr *cmsg;
78 union {
79 struct cmsghdr hdr;
80 char buf[CMSG_SPACE(sizeof(int) * 1)];
81 } cmsgbuf;
82 struct iovec iov;
83 ssize_t n = -1;
84 int fd;
85 struct imsg_fd *ifd;
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
100 again:
101 #ifdef __OpenBSD__
102 if (getdtablecount() + imsg_fd_overhead +
103 (int)((CMSG_SPACE(sizeof(int))-CMSG_SPACE(0))/sizeof(int))
104 >= getdtablesize()) {
105 #else
106 if (available_fds(imsg_fd_overhead +
107 (CMSG_SPACE(sizeof(int))-CMSG_SPACE(0))/sizeof(int))) {
108 #endif
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;
123 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
124 if (cmsg->cmsg_level == SOL_SOCKET &&
125 cmsg->cmsg_type == SCM_RIGHTS) {
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 */
134 j = ((char *)cmsg + cmsg->cmsg_len -
135 (char *)CMSG_DATA(cmsg)) / 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
156 imsg_get(struct imsgbuf *ibuf, struct imsg *imsg)
157 {
158 size_t av, left, datalen;
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));
166 if (imsg->hdr.len < IMSG_HEADER_SIZE ||
167 imsg->hdr.len > MAX_IMSGSIZE) {
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
185 memcpy(imsg->data, ibuf->r.rptr, datalen);
186
187 if (imsg->hdr.len < av) {
188 left = av - imsg->hdr.len;
189 memmove(&ibuf->r.buf, ibuf->r.buf + imsg->hdr.len, left);
190 ibuf->r.wpos = left;
191 } else
192 ibuf->r.wpos = 0;
193
194 return (datalen + IMSG_HEADER_SIZE);
195 }
196
197 int
198 imsg_compose(struct imsgbuf *ibuf, u_int32_t type, u_int32_t peerid,
199 pid_t pid, int fd, const void *data, u_int16_t datalen)
200 {
201 struct ibuf *wbuf;
202
203 if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
204 return (-1);
205
206 if (imsg_add(wbuf, data, datalen) == -1)
207 return (-1);
208
209 wbuf->fd = fd;
210
211 imsg_close(ibuf, wbuf);
212
213 return (1);
214 }
215
216 int
217 imsg_composev(struct imsgbuf *ibuf, u_int32_t type, u_int32_t peerid,
218 pid_t pid, int fd, const struct iovec *iov, int iovcnt)
219 {
220 struct ibuf *wbuf;
221 int i, datalen = 0;
222
223 for (i = 0; i < iovcnt; i++)
224 datalen += iov[i].iov_len;
225
226 if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
227 return (-1);
228
229 for (i = 0; i < iovcnt; i++)
230 if (imsg_add(wbuf, iov[i].iov_base, iov[i].iov_len) == -1)
231 return (-1);
232
233 wbuf->fd = fd;
234
235 imsg_close(ibuf, wbuf);
236
237 return (1);
238 }
239
240 /* ARGSUSED */
241 struct ibuf *
242 imsg_create(struct imsgbuf *ibuf, u_int32_t type, u_int32_t peerid,
243 pid_t pid, u_int16_t datalen)
244 {
245 struct ibuf *wbuf;
246 struct imsg_hdr hdr;
247
248 datalen += IMSG_HEADER_SIZE;
249 if (datalen > MAX_IMSGSIZE) {
250 errno = ERANGE;
251 return (NULL);
252 }
253
254 hdr.type = type;
255 hdr.flags = 0;
256 hdr.peerid = peerid;
257 if ((hdr.pid = pid) == 0)
258 hdr.pid = ibuf->pid;
259 if ((wbuf = ibuf_dynamic(datalen, MAX_IMSGSIZE)) == NULL) {
260 return (NULL);
261 }
262 if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1)
263 return (NULL);
264
265 return (wbuf);
266 }
267
268 int
269 imsg_add(struct ibuf *msg, const void *data, u_int16_t datalen)
270 {
271 if (datalen)
272 if (ibuf_add(msg, data, datalen) == -1) {
273 ibuf_free(msg);
274 return (-1);
275 }
276 return (datalen);
277 }
278
279 void
280 imsg_close(struct imsgbuf *ibuf, struct ibuf *msg)
281 {
282 struct imsg_hdr *hdr;
283
284 hdr = (struct imsg_hdr *)msg->buf;
285
286 hdr->flags &= ~IMSGF_HASFD;
287 if (msg->fd != -1)
288 hdr->flags |= IMSGF_HASFD;
289
290 hdr->len = (u_int16_t)msg->wpos;
291
292 ibuf_close(&ibuf->w, msg);
293 }
294
295 void
296 imsg_free(struct imsg *imsg)
297 {
298 free(imsg->data);
299 }
300
301 int
302 imsg_get_fd(struct imsgbuf *ibuf)
303 {
304 int fd;
305 struct imsg_fd *ifd;
306
307 if ((ifd = TAILQ_FIRST(&ibuf->fds)) == NULL)
308 return (-1);
309
310 fd = ifd->fd;
311 TAILQ_REMOVE(&ibuf->fds, ifd, entry);
312 free(ifd);
313
314 return (fd);
315 }
316
317 int
318 imsg_flush(struct imsgbuf *ibuf)
319 {
320 while (ibuf->w.queued)
321 if (msgbuf_write(&ibuf->w) <= 0)
322 return (-1);
323 return (0);
324 }
325
326 void
327 imsg_clear(struct imsgbuf *ibuf)
328 {
329 int fd;
330
331 msgbuf_clear(&ibuf->w);
332 while ((fd = imsg_get_fd(ibuf)) != -1)
333 close(fd);
334 }