1 /* $OpenBSD: src/usr.sbin/ntpd/imsg.c,v 1.10 2005/03/23 10:42:04 henning Exp $ */
4 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
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.
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 MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
29 imsg_init(struct imsgbuf
*ibuf
, int fd
)
31 msgbuf_init(&ibuf
->w
);
32 bzero(&ibuf
->r
, sizeof(ibuf
->r
));
39 imsg_read(struct imsgbuf
*ibuf
)
43 if ((n
= read(ibuf
->fd
, ibuf
->r
.buf
+ ibuf
->r
.wpos
,
44 sizeof(ibuf
->r
.buf
) - ibuf
->r
.wpos
)) == -1) {
45 if (errno
!= EINTR
&& errno
!= EAGAIN
) {
46 log_warn("imsg_read: pipe read error");
58 imsg_get(struct imsgbuf
*ibuf
, struct imsg
*imsg
)
60 size_t av
, left
, datalen
;
64 if (IMSG_HEADER_SIZE
> av
)
67 memcpy(&imsg
->hdr
, ibuf
->r
.buf
, sizeof(imsg
->hdr
));
68 if (imsg
->hdr
.len
< IMSG_HEADER_SIZE
||
69 imsg
->hdr
.len
> MAX_IMSGSIZE
) {
70 log_warnx("imsg_get: imsg hdr len out of bounds");
73 if (imsg
->hdr
.len
> av
)
75 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
76 ibuf
->r
.rptr
= ibuf
->r
.buf
+ IMSG_HEADER_SIZE
;
77 if ((imsg
->data
= malloc(datalen
)) == NULL
) {
81 memcpy(imsg
->data
, ibuf
->r
.rptr
, datalen
);
83 if (imsg
->hdr
.len
< av
) {
84 left
= av
- imsg
->hdr
.len
;
85 memmove(&ibuf
->r
.buf
, ibuf
->r
.buf
+ imsg
->hdr
.len
, left
);
90 return (datalen
+ IMSG_HEADER_SIZE
);
94 imsg_compose(struct imsgbuf
*ibuf
, enum imsg_type type
, u_int32_t peerid
,
95 pid_t pid
, void *data
, u_int16_t datalen
)
100 if ((wbuf
= imsg_create(ibuf
, type
, peerid
, pid
, datalen
)) == NULL
)
103 if (imsg_add(wbuf
, data
, datalen
) == -1)
106 if ((n
= imsg_close(ibuf
, wbuf
)) < 0)
113 imsg_create(struct imsgbuf
*ibuf
, enum imsg_type type
, u_int32_t peerid
,
114 pid_t pid
, u_int16_t datalen
)
119 if (datalen
> MAX_IMSGSIZE
- IMSG_HEADER_SIZE
) {
120 log_warnx("imsg_create: len %u > MAX_IMSGSIZE; "
121 "type %u peerid %lu", datalen
+ IMSG_HEADER_SIZE
,
126 hdr
.len
= datalen
+ IMSG_HEADER_SIZE
;
130 if ((wbuf
= buf_open(hdr
.len
)) == NULL
) {
131 log_warn("imsg_create: buf_open");
134 if (imsg_add(wbuf
, &hdr
, sizeof(hdr
)) == -1)
141 imsg_add(struct buf
*msg
, void *data
, u_int16_t datalen
)
144 if (buf_add(msg
, data
, datalen
) == -1) {
145 log_warnx("imsg_add: buf_add error");
153 imsg_close(struct imsgbuf
*ibuf
, struct buf
*msg
)
157 if ((n
= buf_close(&ibuf
->w
, msg
)) < 0) {
158 log_warnx("imsg_close: buf_close error");
166 imsg_free(struct imsg
*imsg
)