Fix id of the 945GME chip.
[dragonfly/netmp.git] / contrib / ntpd / imsg.c
blobd12f8f01802a3728ad16b0c4835e52dea335ad9f
1 /* $OpenBSD: src/usr.sbin/ntpd/imsg.c,v 1.10 2005/03/23 10:42:04 henning Exp $ */
3 /*
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>
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
26 #include "ntpd.h"
28 void
29 imsg_init(struct imsgbuf *ibuf, int fd)
31 msgbuf_init(&ibuf->w);
32 bzero(&ibuf->r, sizeof(ibuf->r));
33 ibuf->fd = fd;
34 ibuf->w.fd = fd;
35 ibuf->pid = getpid();
38 int
39 imsg_read(struct imsgbuf *ibuf)
41 ssize_t n;
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");
47 return (-1);
49 return (0);
52 ibuf->r.wpos += n;
54 return (n);
57 int
58 imsg_get(struct imsgbuf *ibuf, struct imsg *imsg)
60 size_t av, left, datalen;
62 av = ibuf->r.wpos;
64 if (IMSG_HEADER_SIZE > av)
65 return (0);
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");
71 return (-1);
73 if (imsg->hdr.len > av)
74 return (0);
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) {
78 log_warn("imsg_get");
79 return (-1);
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);
86 ibuf->r.wpos = left;
87 } else
88 ibuf->r.wpos = 0;
90 return (datalen + IMSG_HEADER_SIZE);
93 int
94 imsg_compose(struct imsgbuf *ibuf, enum imsg_type type, u_int32_t peerid,
95 pid_t pid, void *data, u_int16_t datalen)
97 struct buf *wbuf;
98 int n;
100 if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
101 return (-1);
103 if (imsg_add(wbuf, data, datalen) == -1)
104 return (-1);
106 if ((n = imsg_close(ibuf, wbuf)) < 0)
107 return (-1);
109 return (n);
112 struct buf *
113 imsg_create(struct imsgbuf *ibuf, enum imsg_type type, u_int32_t peerid,
114 pid_t pid, u_int16_t datalen)
116 struct buf *wbuf;
117 struct imsg_hdr hdr;
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,
122 type, peerid);
123 return (NULL);
126 hdr.len = datalen + IMSG_HEADER_SIZE;
127 hdr.type = type;
128 hdr.peerid = peerid;
129 hdr.pid = pid;
130 if ((wbuf = buf_open(hdr.len)) == NULL) {
131 log_warn("imsg_create: buf_open");
132 return (NULL);
134 if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1)
135 return (NULL);
137 return (wbuf);
141 imsg_add(struct buf *msg, void *data, u_int16_t datalen)
143 if (datalen)
144 if (buf_add(msg, data, datalen) == -1) {
145 log_warnx("imsg_add: buf_add error");
146 buf_free(msg);
147 return (-1);
149 return (datalen);
153 imsg_close(struct imsgbuf *ibuf, struct buf *msg)
155 int n;
157 if ((n = buf_close(&ibuf->w, msg)) < 0) {
158 log_warnx("imsg_close: buf_close error");
159 buf_free(msg);
160 return (-1);
162 return (n);
165 void
166 imsg_free(struct imsg *imsg)
168 free(imsg->data);