GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libutil/imsg-buffer.c Lines: 92 131 70.2 %
Date: 2017-11-07 Branches: 36 66 54.5 %

Line Branch Exec Source
1
/*	$OpenBSD: imsg-buffer.c,v 1.10 2017/04/11 09:57:19 reyk Exp $	*/
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 <sys/types.h>
20
#include <sys/queue.h>
21
#include <sys/socket.h>
22
#include <sys/uio.h>
23
24
#include <limits.h>
25
#include <errno.h>
26
#include <stdlib.h>
27
#include <string.h>
28
#include <unistd.h>
29
30
#include "imsg.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 *
37
ibuf_open(size_t len)
38
{
39
	struct ibuf	*buf;
40
41
133389968
	if ((buf = calloc(1, sizeof(struct ibuf))) == NULL)
42
		return (NULL);
43
66694984
	if ((buf->buf = malloc(len)) == NULL) {
44
		free(buf);
45
		return (NULL);
46
	}
47
66694984
	buf->size = buf->max = len;
48
66694984
	buf->fd = -1;
49
50
66694984
	return (buf);
51
66694984
}
52
53
struct ibuf *
54
ibuf_dynamic(size_t len, size_t max)
55
{
56
	struct ibuf	*buf;
57
58
133389968
	if (max < len)
59
		return (NULL);
60
61
66694984
	if ((buf = ibuf_open(len)) == NULL)
62
		return (NULL);
63
64
66694984
	if (max > 0)
65
66694984
		buf->max = max;
66
67
66694984
	return (buf);
68
66694984
}
69
70
int
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
124
	if (buf->wpos + len > buf->max) {
77
		errno = ERANGE;
78
		return (-1);
79
	}
80
81
62
	b = recallocarray(buf->buf, buf->size, buf->wpos + len, 1);
82
62
	if (b == NULL)
83
		return (-1);
84
62
	buf->buf = b;
85
62
	buf->size = buf->wpos + len;
86
87
62
	return (0);
88
62
}
89
90
int
91
ibuf_add(struct ibuf *buf, const void *data, size_t len)
92
{
93
133405632
	if (buf->wpos + len > buf->size)
94
62
		if (ibuf_realloc(buf, len) == -1)
95
			return (-1);
96
97
66702816
	memcpy(buf->buf + buf->wpos, data, len);
98
66702816
	buf->wpos += len;
99
66702816
	return (0);
100
66702816
}
101
102
void *
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 *
117
ibuf_seek(struct ibuf *buf, size_t pos, size_t len)
118
{
119
	/* only allowed to seek in already written parts */
120
2303546544
	if (pos + len > buf->wpos)
121
		return (NULL);
122
123
1151773272
	return (buf->buf + pos);
124
1151773272
}
125
126
size_t
127
ibuf_size(struct ibuf *buf)
128
{
129
400036900
	return (buf->wpos);
130
}
131
132
size_t
133
ibuf_left(struct ibuf *buf)
134
{
135
	return (buf->max - buf->wpos);
136
}
137
138
void
139
ibuf_close(struct msgbuf *msgbuf, struct ibuf *buf)
140
{
141
15752
	ibuf_enqueue(msgbuf, buf);
142
7876
}
143
144
int
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
	memset(&iov, 0, 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
again:
162
	if ((n = writev(msgbuf->fd, iov, i)) == -1) {
163
		if (errno == EINTR)
164
			goto again;
165
		if (errno == ENOBUFS)
166
			errno = EAGAIN;
167
		return (-1);
168
	}
169
170
	if (n == 0) {			/* connection closed */
171
		errno = 0;
172
		return (0);
173
	}
174
175
	msgbuf_drain(msgbuf, n);
176
177
	return (1);
178
}
179
180
void
181
ibuf_free(struct ibuf *buf)
182
{
183
133389976
	if (buf == NULL)
184
		return;
185
66694984
	freezero(buf->buf, buf->size);
186
66694984
	free(buf);
187
133389972
}
188
189
void
190
msgbuf_init(struct msgbuf *msgbuf)
191
{
192
2408
	msgbuf->queued = 0;
193
1204
	msgbuf->fd = -1;
194
1204
	TAILQ_INIT(&msgbuf->bufs);
195
1204
}
196
197
void
198
msgbuf_drain(struct msgbuf *msgbuf, size_t n)
199
{
200
	struct ibuf	*buf, *next;
201
202
37504
	for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0;
203
	    buf = next) {
204
8576
		next = TAILQ_NEXT(buf, entry);
205
8576
		if (buf->rpos + n >= buf->wpos) {
206
7876
			n -= buf->wpos - buf->rpos;
207
7876
			ibuf_dequeue(msgbuf, buf);
208
7876
		} else {
209
700
			buf->rpos += n;
210
			n = 0;
211
		}
212
	}
213
6784
}
214
215
void
216
msgbuf_clear(struct msgbuf *msgbuf)
217
{
218
	struct ibuf	*buf;
219
220
3522
	while ((buf = TAILQ_FIRST(&msgbuf->bufs)) != NULL)
221
		ibuf_dequeue(msgbuf, buf);
222
1174
}
223
224
int
225
msgbuf_write(struct msgbuf *msgbuf)
226
{
227
50646238
	struct iovec	 iov[IOV_MAX];
228
	struct ibuf	*buf;
229
	unsigned int	 i = 0;
230
	ssize_t		 n;
231
25323119
	struct msghdr	 msg;
232
	struct cmsghdr	*cmsg;
233
25323119
	union {
234
		struct cmsghdr	hdr;
235
		char		buf[CMSG_SPACE(sizeof(int))];
236
	} cmsgbuf;
237
238
25323119
	memset(&iov, 0, sizeof(iov));
239
25323119
	memset(&msg, 0, sizeof(msg));
240
25323119
	memset(&cmsgbuf, 0, sizeof(cmsgbuf));
241
326537157
	TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
242
108845719
		if (i >= IOV_MAX)
243
			break;
244
83526370
		iov[i].iov_base = buf->buf + buf->rpos;
245
83526370
		iov[i].iov_len = buf->wpos - buf->rpos;
246
83526370
		i++;
247
83526370
		if (buf->fd != -1)
248
			break;
249
	}
250
251
25323119
	msg.msg_iov = iov;
252
25323119
	msg.msg_iovlen = i;
253
254

25326889
	if (buf != NULL && buf->fd != -1) {
255
3770
		msg.msg_control = (caddr_t)&cmsgbuf.buf;
256
3770
		msg.msg_controllen = sizeof(cmsgbuf.buf);
257
3770
		cmsg = CMSG_FIRSTHDR(&msg);
258
3770
		cmsg->cmsg_len = CMSG_LEN(sizeof(int));
259
3770
		cmsg->cmsg_level = SOL_SOCKET;
260
3770
		cmsg->cmsg_type = SCM_RIGHTS;
261
3770
		*(int *)CMSG_DATA(cmsg) = buf->fd;
262
3770
	}
263
264
again:
265
25323119
	if ((n = sendmsg(msgbuf->fd, &msg, 0)) == -1) {
266
25316335
		if (errno == EINTR)
267
			goto again;
268
25316335
		if (errno == ENOBUFS)
269
			errno = EAGAIN;
270
25316335
		return (-1);
271
	}
272
273
6784
	if (n == 0) {			/* connection closed */
274
		errno = 0;
275
		return (0);
276
	}
277
278
	/*
279
	 * assumption: fd got sent if sendmsg sent anything
280
	 * this works because fds are passed one at a time
281
	 */
282

10554
	if (buf != NULL && buf->fd != -1) {
283
3770
		close(buf->fd);
284
3770
		buf->fd = -1;
285
3770
	}
286
287
6784
	msgbuf_drain(msgbuf, n);
288
289
6784
	return (1);
290
25323119
}
291
292
void
293
ibuf_enqueue(struct msgbuf *msgbuf, struct ibuf *buf)
294
{
295
15752
	TAILQ_INSERT_TAIL(&msgbuf->bufs, buf, entry);
296
7876
	msgbuf->queued++;
297
7876
}
298
299
void
300
ibuf_dequeue(struct msgbuf *msgbuf, struct ibuf *buf)
301
{
302
31504
	TAILQ_REMOVE(&msgbuf->bufs, buf, entry);
303
304
7876
	if (buf->fd != -1)
305
		close(buf->fd);
306
307
7876
	msgbuf->queued--;
308
7876
	ibuf_free(buf);
309
7876
}