1 /* Id */
2 /* $OpenBSD: imsg-buffer.c,v 1.3 2010/05/26 13:56:07 nicm Exp $ */
3
4 /*
5 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 #include <sys/param.h>
21 #include <sys/socket.h>
22 #include <sys/uio.h>
23
24 #include <errno.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <strings.h>
28 #include <unistd.h>
29
30 #include "tmux.h"
31
32 int ibuf_realloc(struct ibuf *, size_t);
33 void ibuf_enqueue(struct msgbuf *, struct ibuf *);
34 void ibuf_dequeue(struct msgbuf *, struct ibuf *);
35
36 struct ibuf *
ibuf_open(size_t len)37 ibuf_open(size_t len)
38 {
39 struct ibuf *buf;
40
41 if ((buf = calloc(1, sizeof(struct ibuf))) == NULL)
42 return (NULL);
43 if ((buf->buf = malloc(len)) == NULL) {
44 free(buf);
45 return (NULL);
46 }
47 buf->size = buf->max = len;
48 buf->fd = -1;
49
50 return (buf);
51 }
52
53 struct ibuf *
ibuf_dynamic(size_t len,size_t max)54 ibuf_dynamic(size_t len, size_t max)
55 {
56 struct ibuf *buf;
57
58 if (max < len)
59 return (NULL);
60
61 if ((buf = ibuf_open(len)) == NULL)
62 return (NULL);
63
64 if (max > 0)
65 buf->max = max;
66
67 return (buf);
68 }
69
70 int
ibuf_realloc(struct ibuf * buf,size_t len)71 ibuf_realloc(struct ibuf *buf, size_t len)
72 {
73 u_char *b;
74
75 /* on static buffers max is eq size and so the following fails */
76 if (buf->wpos + len > buf->max) {
77 errno = ENOMEM;
78 return (-1);
79 }
80
81 b = realloc(buf->buf, buf->wpos + len);
82 if (b == NULL)
83 return (-1);
84 buf->buf = b;
85 buf->size = buf->wpos + len;
86
87 return (0);
88 }
89
90 int
ibuf_add(struct ibuf * buf,const void * data,size_t len)91 ibuf_add(struct ibuf *buf, const void *data, size_t len)
92 {
93 if (buf->wpos + len > buf->size)
94 if (ibuf_realloc(buf, len) == -1)
95 return (-1);
96
97 memcpy(buf->buf + buf->wpos, data, len);
98 buf->wpos += len;
99 return (0);
100 }
101
102 void *
ibuf_reserve(struct ibuf * buf,size_t len)103 ibuf_reserve(struct ibuf *buf, size_t len)
104 {
105 void *b;
106
107 if (buf->wpos + len > buf->size)
108 if (ibuf_realloc(buf, len) == -1)
109 return (NULL);
110
111 b = buf->buf + buf->wpos;
112 buf->wpos += len;
113 return (b);
114 }
115
116 void *
ibuf_seek(struct ibuf * buf,size_t pos,size_t len)117 ibuf_seek(struct ibuf *buf, size_t pos, size_t len)
118 {
119 /* only allowed to seek in already written parts */
120 if (pos + len > buf->wpos)
121 return (NULL);
122
123 return (buf->buf + pos);
124 }
125
126 size_t
ibuf_size(struct ibuf * buf)127 ibuf_size(struct ibuf *buf)
128 {
129 return (buf->wpos);
130 }
131
132 size_t
ibuf_left(struct ibuf * buf)133 ibuf_left(struct ibuf *buf)
134 {
135 return (buf->max - buf->wpos);
136 }
137
138 void
ibuf_close(struct msgbuf * msgbuf,struct ibuf * buf)139 ibuf_close(struct msgbuf *msgbuf, struct ibuf *buf)
140 {
141 ibuf_enqueue(msgbuf, buf);
142 }
143
144 int
ibuf_write(struct msgbuf * msgbuf)145 ibuf_write(struct msgbuf *msgbuf)
146 {
147 struct iovec iov[IOV_MAX];
148 struct ibuf *buf;
149 unsigned int i = 0;
150 ssize_t n;
151
152 bzero(&iov, sizeof(iov));
153 TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
154 if (i >= IOV_MAX)
155 break;
156 iov[i].iov_base = buf->buf + buf->rpos;
157 iov[i].iov_len = buf->wpos - buf->rpos;
158 i++;
159 }
160
161 if ((n = writev(msgbuf->fd, iov, i)) == -1) {
162 if (errno == EAGAIN || errno == ENOBUFS ||
163 errno == EINTR) /* try later */
164 return (0);
165 else
166 return (-1);
167 }
168
169 if (n == 0) { /* connection closed */
170 errno = 0;
171 return (-2);
172 }
173
174 msgbuf_drain(msgbuf, n);
175
176 return (0);
177 }
178
179 void
ibuf_free(struct ibuf * buf)180 ibuf_free(struct ibuf *buf)
181 {
182 free(buf->buf);
183 free(buf);
184 }
185
186 void
msgbuf_init(struct msgbuf * msgbuf)187 msgbuf_init(struct msgbuf *msgbuf)
188 {
189 msgbuf->queued = 0;
190 msgbuf->fd = -1;
191 TAILQ_INIT(&msgbuf->bufs);
192 }
193
194 void
msgbuf_drain(struct msgbuf * msgbuf,size_t n)195 msgbuf_drain(struct msgbuf *msgbuf, size_t n)
196 {
197 struct ibuf *buf, *next;
198
199 for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0;
200 buf = next) {
201 next = TAILQ_NEXT(buf, entry);
202 if (buf->rpos + n >= buf->wpos) {
203 n -= buf->wpos - buf->rpos;
204 ibuf_dequeue(msgbuf, buf);
205 } else {
206 buf->rpos += n;
207 n = 0;
208 }
209 }
210 }
211
212 void
msgbuf_clear(struct msgbuf * msgbuf)213 msgbuf_clear(struct msgbuf *msgbuf)
214 {
215 struct ibuf *buf;
216
217 while ((buf = TAILQ_FIRST(&msgbuf->bufs)) != NULL)
218 ibuf_dequeue(msgbuf, buf);
219 }
220
221 int
msgbuf_write(struct msgbuf * msgbuf)222 msgbuf_write(struct msgbuf *msgbuf)
223 {
224 struct iovec iov[IOV_MAX];
225 struct ibuf *buf;
226 unsigned int i = 0;
227 ssize_t n;
228 struct msghdr msg;
229 struct cmsghdr *cmsg;
230 union {
231 struct cmsghdr hdr;
232 char buf[CMSG_SPACE(sizeof(int))];
233 } cmsgbuf;
234
235 bzero(&iov, sizeof(iov));
236 bzero(&msg, sizeof(msg));
237 TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
238 if (i >= IOV_MAX)
239 break;
240 iov[i].iov_base = buf->buf + buf->rpos;
241 iov[i].iov_len = buf->wpos - buf->rpos;
242 i++;
243 if (buf->fd != -1)
244 break;
245 }
246
247 msg.msg_iov = iov;
248 msg.msg_iovlen = i;
249
250 if (buf != NULL && buf->fd != -1) {
251 msg.msg_control = (caddr_t)&cmsgbuf.buf;
252 msg.msg_controllen = CMSG_SPACE(sizeof(int));
253 cmsg = CMSG_FIRSTHDR(&msg);
254 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
255 cmsg->cmsg_level = SOL_SOCKET;
256 cmsg->cmsg_type = SCM_RIGHTS;
257 *(int *)CMSG_DATA(cmsg) = buf->fd;
258 }
259
260 if ((n = sendmsg(msgbuf->fd, &msg, 0)) == -1) {
261 if (errno == EAGAIN || errno == ENOBUFS ||
262 errno == EINTR) /* try later */
263 return (0);
264 else
265 return (-1);
266 }
267
268 if (n == 0) { /* connection closed */
269 errno = 0;
270 return (-2);
271 }
272
273 /*
274 * assumption: fd got sent if sendmsg sent anything
275 * this works because fds are passed one at a time
276 */
277 if (buf != NULL && buf->fd != -1) {
278 close(buf->fd);
279 buf->fd = -1;
280 }
281
282 msgbuf_drain(msgbuf, n);
283
284 return (0);
285 }
286
287 void
ibuf_enqueue(struct msgbuf * msgbuf,struct ibuf * buf)288 ibuf_enqueue(struct msgbuf *msgbuf, struct ibuf *buf)
289 {
290 TAILQ_INSERT_TAIL(&msgbuf->bufs, buf, entry);
291 msgbuf->queued++;
292 }
293
294 void
ibuf_dequeue(struct msgbuf * msgbuf,struct ibuf * buf)295 ibuf_dequeue(struct msgbuf *msgbuf, struct ibuf *buf)
296 {
297 TAILQ_REMOVE(&msgbuf->bufs, buf, entry);
298
299 if (buf->fd != -1)
300 close(buf->fd);
301
302 msgbuf->queued--;
303 ibuf_free(buf);
304 }
305