]>
Commit | Line | Data |
---|---|---|
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 | ||
24 | int ibuf_realloc(struct ibuf *, size_t); | |
25 | void ibuf_enqueue(struct msgbuf *, struct ibuf *); | |
26 | void ibuf_dequeue(struct msgbuf *, struct ibuf *); | |
27 | ||
28 | struct ibuf * | |
29 | ibuf_open(size_t len) | |
30 | { | |
31 | struct ibuf *buf; | |
32 | ||
33 | if ((buf = calloc(1, sizeof(struct ibuf))) == NULL) | |
34 | return (NULL); | |
35 | if ((buf->buf = malloc(len)) == NULL) { | |
36 | free(buf); | |
37 | return (NULL); | |
38 | } | |
39 | buf->size = buf->max = len; | |
40 | buf->fd = -1; | |
41 | ||
42 | return (buf); | |
43 | } | |
44 | ||
45 | struct ibuf * | |
46 | ibuf_dynamic(size_t len, size_t max) | |
47 | { | |
48 | struct ibuf *buf; | |
49 | ||
50 | if (max < len) | |
51 | return (NULL); | |
52 | ||
53 | if ((buf = ibuf_open(len)) == NULL) | |
54 | return (NULL); | |
55 | ||
56 | if (max > 0) | |
57 | buf->max = max; | |
58 | ||
59 | return (buf); | |
60 | } | |
61 | ||
62 | int | |
63 | ibuf_realloc(struct ibuf *buf, size_t len) | |
64 | { | |
65 | u_char *b; | |
66 | ||
67 | /* on static buffers max is eq size and so the following fails */ | |
68 | if (buf->wpos + len > buf->max) { | |
69 | errno = ERANGE; | |
70 | return (-1); | |
71 | } | |
72 | ||
73 | b = realloc(buf->buf, buf->wpos + len); | |
74 | if (b == NULL) | |
75 | return (-1); | |
76 | buf->buf = b; | |
77 | buf->size = buf->wpos + len; | |
78 | ||
79 | return (0); | |
80 | } | |
81 | ||
82 | int | |
83 | ibuf_add(struct ibuf *buf, const void *data, size_t len) | |
84 | { | |
85 | if (buf->wpos + len > buf->size) | |
86 | if (ibuf_realloc(buf, len) == -1) | |
87 | return (-1); | |
88 | ||
89 | memcpy(buf->buf + buf->wpos, data, len); | |
90 | buf->wpos += len; | |
91 | return (0); | |
92 | } | |
93 | ||
94 | void * | |
95 | ibuf_reserve(struct ibuf *buf, size_t len) | |
96 | { | |
97 | void *b; | |
98 | ||
99 | if (buf->wpos + len > buf->size) | |
100 | if (ibuf_realloc(buf, len) == -1) | |
101 | return (NULL); | |
102 | ||
103 | b = buf->buf + buf->wpos; | |
104 | buf->wpos += len; | |
105 | return (b); | |
106 | } | |
107 | ||
108 | void * | |
109 | ibuf_seek(struct ibuf *buf, size_t pos, size_t len) | |
110 | { | |
111 | /* only allowed to seek in already written parts */ | |
112 | if (pos + len > buf->wpos) | |
113 | return (NULL); | |
114 | ||
115 | return (buf->buf + pos); | |
116 | } | |
117 | ||
118 | size_t | |
119 | ibuf_size(struct ibuf *buf) | |
120 | { | |
121 | return (buf->wpos); | |
122 | } | |
123 | ||
124 | size_t | |
125 | ibuf_left(struct ibuf *buf) | |
126 | { | |
127 | return (buf->max - buf->wpos); | |
128 | } | |
129 | ||
130 | void | |
131 | ibuf_close(struct msgbuf *msgbuf, struct ibuf *buf) | |
132 | { | |
133 | ibuf_enqueue(msgbuf, buf); | |
134 | } | |
135 | ||
136 | int | |
137 | ibuf_write(struct msgbuf *msgbuf) | |
138 | { | |
139 | struct iovec iov[IOV_MAX]; | |
140 | struct ibuf *buf; | |
141 | unsigned int i = 0; | |
142 | ssize_t n; | |
143 | ||
144 | memset(&iov, 0, sizeof(iov)); | |
145 | TAILQ_FOREACH(buf, &msgbuf->bufs, entry) { | |
146 | if (i >= IOV_MAX) | |
147 | break; | |
148 | iov[i].iov_base = buf->buf + buf->rpos; | |
149 | iov[i].iov_len = buf->wpos - buf->rpos; | |
150 | i++; | |
151 | } | |
152 | ||
153 | again: | |
154 | if ((n = writev(msgbuf->fd, iov, i)) == -1) { | |
155 | if (errno == EINTR) | |
156 | goto again; | |
157 | if (errno == ENOBUFS) | |
158 | errno = EAGAIN; | |
159 | return (-1); | |
160 | } | |
161 | ||
162 | if (n == 0) { /* connection closed */ | |
163 | errno = 0; | |
164 | return (0); | |
165 | } | |
166 | ||
167 | msgbuf_drain(msgbuf, n); | |
168 | ||
169 | return (1); | |
170 | } | |
171 | ||
172 | void | |
173 | ibuf_free(struct ibuf *buf) | |
174 | { | |
175 | if (buf == NULL) | |
176 | return; | |
177 | free(buf->buf); | |
178 | free(buf); | |
179 | } | |
180 | ||
181 | void | |
182 | msgbuf_init(struct msgbuf *msgbuf) | |
183 | { | |
184 | msgbuf->queued = 0; | |
185 | msgbuf->fd = -1; | |
186 | TAILQ_INIT(&msgbuf->bufs); | |
187 | } | |
188 | ||
189 | void | |
190 | msgbuf_drain(struct msgbuf *msgbuf, size_t n) | |
191 | { | |
192 | struct ibuf *buf, *next; | |
193 | ||
194 | for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0; | |
195 | buf = next) { | |
196 | next = TAILQ_NEXT(buf, entry); | |
197 | if (buf->rpos + n >= buf->wpos) { | |
198 | n -= buf->wpos - buf->rpos; | |
199 | ibuf_dequeue(msgbuf, buf); | |
200 | } else { | |
201 | buf->rpos += n; | |
202 | n = 0; | |
203 | } | |
204 | } | |
205 | } | |
206 | ||
207 | void | |
208 | msgbuf_clear(struct msgbuf *msgbuf) | |
209 | { | |
210 | struct ibuf *buf; | |
211 | ||
212 | while ((buf = TAILQ_FIRST(&msgbuf->bufs)) != NULL) | |
213 | ibuf_dequeue(msgbuf, buf); | |
214 | } | |
215 | ||
216 | int | |
217 | msgbuf_write(struct msgbuf *msgbuf) | |
218 | { | |
219 | struct iovec iov[IOV_MAX]; | |
220 | struct ibuf *buf; | |
221 | unsigned int i = 0; | |
222 | ssize_t n; | |
223 | struct msghdr msg; | |
224 | struct cmsghdr *cmsg; | |
225 | union { | |
226 | struct cmsghdr hdr; | |
227 | char buf[CMSG_SPACE(sizeof(int))]; | |
228 | } cmsgbuf; | |
229 | ||
230 | memset(&iov, 0, sizeof(iov)); | |
231 | memset(&msg, 0, sizeof(msg)); | |
232 | memset(&cmsgbuf, 0, sizeof(cmsgbuf)); | |
233 | TAILQ_FOREACH(buf, &msgbuf->bufs, entry) { | |
234 | if (i >= IOV_MAX) | |
235 | break; | |
236 | iov[i].iov_base = buf->buf + buf->rpos; | |
237 | iov[i].iov_len = buf->wpos - buf->rpos; | |
238 | i++; | |
239 | if (buf->fd != -1) | |
240 | break; | |
241 | } | |
242 | ||
243 | msg.msg_iov = iov; | |
244 | msg.msg_iovlen = i; | |
245 | ||
246 | if (buf != NULL && buf->fd != -1) { | |
247 | msg.msg_control = (caddr_t)&cmsgbuf.buf; | |
248 | msg.msg_controllen = sizeof(cmsgbuf.buf); | |
249 | cmsg = CMSG_FIRSTHDR(&msg); | |
250 | cmsg->cmsg_len = CMSG_LEN(sizeof(int)); | |
251 | cmsg->cmsg_level = SOL_SOCKET; | |
252 | cmsg->cmsg_type = SCM_RIGHTS; | |
eac6e3f0 | 253 | memcpy(CMSG_DATA(cmsg), &buf->fd, sizeof(int)); |
8429abe0 RW |
254 | } |
255 | ||
256 | again: | |
257 | if ((n = sendmsg(msgbuf->fd, &msg, 0)) == -1) { | |
258 | if (errno == EINTR) | |
259 | goto again; | |
260 | if (errno == ENOBUFS) | |
261 | errno = EAGAIN; | |
262 | return (-1); | |
263 | } | |
264 | ||
265 | if (n == 0) { /* connection closed */ | |
266 | errno = 0; | |
267 | return (0); | |
268 | } | |
269 | ||
270 | /* | |
271 | * assumption: fd got sent if sendmsg sent anything | |
272 | * this works because fds are passed one at a time | |
273 | */ | |
274 | if (buf != NULL && buf->fd != -1) { | |
275 | close(buf->fd); | |
276 | buf->fd = -1; | |
277 | } | |
278 | ||
279 | msgbuf_drain(msgbuf, n); | |
280 | ||
281 | return (1); | |
282 | } | |
283 | ||
284 | void | |
285 | ibuf_enqueue(struct msgbuf *msgbuf, struct ibuf *buf) | |
286 | { | |
287 | TAILQ_INSERT_TAIL(&msgbuf->bufs, buf, entry); | |
288 | msgbuf->queued++; | |
289 | } | |
290 | ||
291 | void | |
292 | ibuf_dequeue(struct msgbuf *msgbuf, struct ibuf *buf) | |
293 | { | |
294 | TAILQ_REMOVE(&msgbuf->bufs, buf, entry); | |
295 | ||
296 | if (buf->fd != -1) | |
297 | close(buf->fd); | |
298 | ||
299 | msgbuf->queued--; | |
300 | ibuf_free(buf); | |
301 | } |