]> git.proxmox.com Git - mirror_frr.git/blame - lib/imsg.c
tools, doc: update checkpatch for u_int_*
[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
cd85bc2e 21#include "queue.h"
8429abe0
RW
22#include "imsg.h"
23
996c9314 24int imsg_fd_overhead = 0;
8429abe0 25
996c9314 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 */
996c9314 33static int available_fds(unsigned int n)
eac6e3f0 34{
996c9314
LB
35 unsigned int i;
36 int ret, fds[256];
eac6e3f0 37
996c9314 38 if (n > (sizeof(fds) / sizeof(fds[0])))
eac6e3f0
RW
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
996c9314 61void imsg_init(struct imsgbuf *ibuf, int fd)
8429abe0
RW
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
996c9314 71ssize_t imsg_read(struct imsgbuf *ibuf)
8429abe0 72{
996c9314
LB
73 struct msghdr msg;
74 struct cmsghdr *cmsg;
8429abe0
RW
75 union {
76 struct cmsghdr hdr;
996c9314 77 char buf[CMSG_SPACE(sizeof(int) * 1)];
8429abe0 78 } cmsgbuf;
996c9314
LB
79 struct iovec iov;
80 ssize_t n = -1;
81 int fd;
82 struct imsg_fd *ifd;
8429abe0
RW
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
97again:
eac6e3f0 98#ifdef __OpenBSD__
996c9314
LB
99 if (getdtablecount() + imsg_fd_overhead
100 + (int)((CMSG_SPACE(sizeof(int)) - CMSG_SPACE(0))
101 / sizeof(int))
8429abe0 102 >= getdtablesize()) {
eac6e3f0 103#else
996c9314
LB
104 if (available_fds(imsg_fd_overhead
105 + (CMSG_SPACE(sizeof(int)) - CMSG_SPACE(0))
106 / sizeof(int))) {
eac6e3f0 107#endif
8429abe0
RW
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;
996c9314
LB
122 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
123 if (cmsg->cmsg_level == SOL_SOCKET
124 && cmsg->cmsg_type == SCM_RIGHTS) {
8429abe0
RW
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 */
996c9314
LB
133 j = ((char *)cmsg + cmsg->cmsg_len
134 - (char *)CMSG_DATA(cmsg))
135 / 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,
996c9314 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
996c9314 155ssize_t imsg_get(struct imsgbuf *ibuf, struct imsg *imsg)
8429abe0 156{
996c9314 157 size_t av, left, datalen;
8429abe0
RW
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));
996c9314 165 if (imsg->hdr.len < IMSG_HEADER_SIZE || imsg->hdr.len > MAX_IMSGSIZE) {
8429abe0
RW
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
dd3415b7 183 if (imsg->data)
996c9314 184 memcpy(imsg->data, ibuf->r.rptr, datalen);
8429abe0
RW
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
996c9314
LB
196int imsg_compose(struct imsgbuf *ibuf, u_int32_t type, u_int32_t peerid,
197 pid_t pid, int fd, const void *data, u_int16_t datalen)
8429abe0 198{
996c9314 199 struct ibuf *wbuf;
8429abe0
RW
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
996c9314
LB
214int imsg_composev(struct imsgbuf *ibuf, u_int32_t type, u_int32_t peerid,
215 pid_t pid, int fd, const struct iovec *iov, int iovcnt)
8429abe0 216{
996c9314
LB
217 struct ibuf *wbuf;
218 int i, datalen = 0;
8429abe0
RW
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 */
996c9314
LB
238struct ibuf *imsg_create(struct imsgbuf *ibuf, u_int32_t type, u_int32_t peerid,
239 pid_t pid, u_int16_t datalen)
8429abe0 240{
996c9314
LB
241 struct ibuf *wbuf;
242 struct imsg_hdr hdr;
8429abe0
RW
243
244 datalen += IMSG_HEADER_SIZE;
245 if (datalen > MAX_IMSGSIZE) {
246 errno = ERANGE;
247 return (NULL);
248 }
249
250 hdr.type = type;
251 hdr.flags = 0;
252 hdr.peerid = peerid;
253 if ((hdr.pid = pid) == 0)
254 hdr.pid = ibuf->pid;
255 if ((wbuf = ibuf_dynamic(datalen, MAX_IMSGSIZE)) == NULL) {
256 return (NULL);
257 }
258 if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1)
259 return (NULL);
260
261 return (wbuf);
262}
263
996c9314 264int imsg_add(struct ibuf *msg, const void *data, u_int16_t datalen)
8429abe0
RW
265{
266 if (datalen)
267 if (ibuf_add(msg, data, datalen) == -1) {
268 ibuf_free(msg);
269 return (-1);
270 }
271 return (datalen);
272}
273
996c9314 274void imsg_close(struct imsgbuf *ibuf, struct ibuf *msg)
8429abe0 275{
996c9314 276 struct imsg_hdr *hdr;
8429abe0
RW
277
278 hdr = (struct imsg_hdr *)msg->buf;
279
280 hdr->flags &= ~IMSGF_HASFD;
281 if (msg->fd != -1)
282 hdr->flags |= IMSGF_HASFD;
283
284 hdr->len = (u_int16_t)msg->wpos;
285
286 ibuf_close(&ibuf->w, msg);
287}
288
996c9314 289void imsg_free(struct imsg *imsg)
8429abe0
RW
290{
291 free(imsg->data);
292}
293
996c9314 294int imsg_get_fd(struct imsgbuf *ibuf)
8429abe0 295{
996c9314
LB
296 int fd;
297 struct imsg_fd *ifd;
8429abe0
RW
298
299 if ((ifd = TAILQ_FIRST(&ibuf->fds)) == NULL)
300 return (-1);
301
302 fd = ifd->fd;
303 TAILQ_REMOVE(&ibuf->fds, ifd, entry);
304 free(ifd);
305
306 return (fd);
307}
308
996c9314 309int imsg_flush(struct imsgbuf *ibuf)
8429abe0
RW
310{
311 while (ibuf->w.queued)
312 if (msgbuf_write(&ibuf->w) <= 0)
313 return (-1);
314 return (0);
315}
316
996c9314 317void imsg_clear(struct imsgbuf *ibuf)
8429abe0 318{
996c9314 319 int fd;
8429abe0
RW
320
321 msgbuf_clear(&ibuf->w);
322 while ((fd = imsg_get_fd(ibuf)) != -1)
323 close(fd);
324}