Browse Source

make ntpd imsg-in-a-lib ready as well. extensive testing done, no

behavior change.
ok eric@
OPENBSD_4_6
pyr 15 years ago
parent
commit
239fd25471
6 changed files with 412 additions and 132 deletions
  1. +175
    -20
      src/usr.sbin/ntpd/buffer.c
  2. +121
    -37
      src/usr.sbin/ntpd/imsg.c
  3. +105
    -0
      src/usr.sbin/ntpd/imsg.h
  4. +6
    -5
      src/usr.sbin/ntpd/ntp.c
  5. +3
    -2
      src/usr.sbin/ntpd/ntpd.c
  6. +2
    -68
      src/usr.sbin/ntpd/ntpd.h

+ 175
- 20
src/usr.sbin/ntpd/buffer.c View File

@ -1,4 +1,4 @@
/* $OpenBSD: buffer.c,v 1.9 2005/08/11 16:26:29 henning Exp $ */
/* $OpenBSD: buffer.c,v 1.10 2009/06/06 18:14:25 pyr Exp $ */
/* /*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@ -11,23 +11,24 @@
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/ */
#include <sys/types.h>
#include <sys/param.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/uio.h> #include <sys/uio.h>
#include <errno.h> #include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "ntpd.h"
#include "imsg.h"
int buf_realloc(struct buf *, size_t);
void buf_enqueue(struct msgbuf *, struct buf *); void buf_enqueue(struct msgbuf *, struct buf *);
void buf_dequeue(struct msgbuf *, struct buf *); void buf_dequeue(struct msgbuf *, struct buf *);
@ -42,27 +43,146 @@ buf_open(size_t len)
free(buf); free(buf);
return (NULL); return (NULL);
} }
buf->size = len;
buf->size = buf->max = len;
buf->fd = -1;
return (buf);
}
struct buf *
buf_dynamic(size_t len, size_t max)
{
struct buf *buf;
if (max < len)
return (NULL);
if ((buf = buf_open(len)) == NULL)
return (NULL);
if (max > 0)
buf->max = max;
return (buf); return (buf);
} }
int int
buf_add(struct buf *buf, void *data, size_t len)
buf_realloc(struct buf *buf, size_t len)
{ {
if (buf->wpos + len > buf->size)
u_char *b;
/* on static buffers max is eq size and so the following fails */
if (buf->wpos + len > buf->max) {
errno = ENOMEM;
return (-1);
}
b = realloc(buf->buf, buf->wpos + len);
if (b == NULL)
return (-1); return (-1);
buf->buf = b;
buf->size = buf->wpos + len;
return (0);
}
int
buf_add(struct buf *buf, const void *data, size_t len)
{
if (buf->wpos + len > buf->size)
if (buf_realloc(buf, len) == -1)
return (-1);
memcpy(buf->buf + buf->wpos, data, len); memcpy(buf->buf + buf->wpos, data, len);
buf->wpos += len; buf->wpos += len;
return (0); return (0);
} }
int
void *
buf_reserve(struct buf *buf, size_t len)
{
void *b;
if (buf->wpos + len > buf->size)
if (buf_realloc(buf, len) == -1)
return (NULL);
b = buf->buf + buf->wpos;
buf->wpos += len;
return (b);
}
void *
buf_seek(struct buf *buf, size_t pos, size_t len)
{
/* only allowed to seek in already written parts */
if (pos + len > buf->wpos)
return (NULL);
return (buf->buf + pos);
}
size_t
buf_size(struct buf *buf)
{
return (buf->wpos);
}
size_t
buf_left(struct buf *buf)
{
return (buf->max - buf->wpos);
}
void
buf_close(struct msgbuf *msgbuf, struct buf *buf) buf_close(struct msgbuf *msgbuf, struct buf *buf)
{ {
buf_enqueue(msgbuf, buf); buf_enqueue(msgbuf, buf);
return (1);
}
int
buf_write(struct msgbuf *msgbuf)
{
struct iovec iov[IOV_MAX];
struct buf *buf, *next;
unsigned int i = 0;
ssize_t n;
bzero(&iov, sizeof(iov));
TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
if (i >= IOV_MAX)
break;
iov[i].iov_base = buf->buf + buf->rpos;
iov[i].iov_len = buf->size - buf->rpos;
i++;
}
if ((n = writev(msgbuf->fd, iov, i)) == -1) {
if (errno == EAGAIN || errno == ENOBUFS ||
errno == EINTR) /* try later */
return (0);
else
return (-1);
}
if (n == 0) { /* connection closed */
errno = 0;
return (-2);
}
for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0;
buf = next) {
next = TAILQ_NEXT(buf, entry);
if (buf->rpos + n >= buf->size) {
n -= buf->size - buf->rpos;
buf_dequeue(msgbuf, buf);
} else {
buf->rpos += n;
n = 0;
}
}
return (0);
} }
void void
@ -94,21 +214,43 @@ msgbuf_write(struct msgbuf *msgbuf)
{ {
struct iovec iov[IOV_MAX]; struct iovec iov[IOV_MAX];
struct buf *buf, *next; struct buf *buf, *next;
int i = 0;
unsigned int i = 0;
ssize_t n; ssize_t n;
struct msghdr msg;
struct cmsghdr *cmsg;
union {
struct cmsghdr hdr;
char buf[CMSG_SPACE(sizeof(int))];
} cmsgbuf;
bzero(&iov, sizeof(iov)); bzero(&iov, sizeof(iov));
bzero(&msg, sizeof(msg));
TAILQ_FOREACH(buf, &msgbuf->bufs, entry) { TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
if (i >= IOV_MAX) if (i >= IOV_MAX)
break; break;
iov[i].iov_base = buf->buf + buf->rpos; iov[i].iov_base = buf->buf + buf->rpos;
iov[i].iov_len = buf->size - buf->rpos;
iov[i].iov_len = buf->wpos - buf->rpos;
i++; i++;
if (buf->fd != -1)
break;
} }
if ((n = writev(msgbuf->fd, iov, i)) == -1) {
if (errno == EAGAIN || errno == EINTR ||
errno == ENOBUFS) /* try again later */
msg.msg_iov = iov;
msg.msg_iovlen = i;
if (buf != NULL && buf->fd != -1) {
msg.msg_control = (caddr_t)&cmsgbuf.buf;
msg.msg_controllen = sizeof(cmsgbuf.buf);
cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_len = CMSG_LEN(sizeof(int));
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
*(int *)CMSG_DATA(cmsg) = buf->fd;
}
if ((n = sendmsg(msgbuf->fd, &msg, 0)) == -1) {
if (errno == EAGAIN || errno == ENOBUFS ||
errno == EINTR) /* try later */
return (0); return (0);
else else
return (-1); return (-1);
@ -119,11 +261,20 @@ msgbuf_write(struct msgbuf *msgbuf)
return (-2); return (-2);
} }
/*
* assumption: fd got sent if sendmsg sent anything
* this works because fds are passed one at a time
*/
if (buf != NULL && buf->fd != -1) {
close(buf->fd);
buf->fd = -1;
}
for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0; for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0;
buf = next) { buf = next) {
next = TAILQ_NEXT(buf, entry); next = TAILQ_NEXT(buf, entry);
if (buf->rpos + n >= buf->size) {
n -= buf->size - buf->rpos;
if (buf->rpos + n >= buf->wpos) {
n -= buf->wpos - buf->rpos;
buf_dequeue(msgbuf, buf); buf_dequeue(msgbuf, buf);
} else { } else {
buf->rpos += n; buf->rpos += n;
@ -145,6 +296,10 @@ void
buf_dequeue(struct msgbuf *msgbuf, struct buf *buf) buf_dequeue(struct msgbuf *msgbuf, struct buf *buf)
{ {
TAILQ_REMOVE(&msgbuf->bufs, buf, entry); TAILQ_REMOVE(&msgbuf->bufs, buf, entry);
if (buf->fd != -1)
close(buf->fd);
msgbuf->queued--; msgbuf->queued--;
buf_free(buf); buf_free(buf);
} }

+ 121
- 37
src/usr.sbin/ntpd/imsg.c View File

@ -1,4 +1,4 @@
/* $OpenBSD: imsg.c,v 1.12 2007/03/19 10:03:25 henning Exp $ */
/* $OpenBSD: imsg.c,v 1.13 2009/06/06 18:14:25 pyr Exp $ */
/* /*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@ -11,19 +11,22 @@
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/ */
#include <sys/types.h>
#include <sys/param.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "ntpd.h"
#include "imsg.h"
void void
imsg_init(struct imsgbuf *ibuf, int fd) imsg_init(struct imsgbuf *ibuf, int fd)
@ -33,17 +36,34 @@ imsg_init(struct imsgbuf *ibuf, int fd)
ibuf->fd = fd; ibuf->fd = fd;
ibuf->w.fd = fd; ibuf->w.fd = fd;
ibuf->pid = getpid(); ibuf->pid = getpid();
TAILQ_INIT(&ibuf->fds);
} }
int
ssize_t
imsg_read(struct imsgbuf *ibuf) imsg_read(struct imsgbuf *ibuf)
{ {
struct msghdr msg;
struct cmsghdr *cmsg;
union {
struct cmsghdr hdr;
char buf[CMSG_SPACE(sizeof(int) * 16)];
} cmsgbuf;
struct iovec iov;
ssize_t n; ssize_t n;
int fd;
struct imsg_fd *ifd;
bzero(&msg, sizeof(msg));
iov.iov_base = ibuf->r.buf + ibuf->r.wpos;
iov.iov_len = sizeof(ibuf->r.buf) - ibuf->r.wpos;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = &cmsgbuf.buf;
msg.msg_controllen = sizeof(cmsgbuf.buf);
if ((n = read(ibuf->fd, ibuf->r.buf + ibuf->r.wpos,
sizeof(ibuf->r.buf) - ibuf->r.wpos)) == -1) {
if ((n = recvmsg(ibuf->fd, &msg, 0)) == -1) {
if (errno != EINTR && errno != EAGAIN) { if (errno != EINTR && errno != EAGAIN) {
log_warn("imsg_read: pipe read error");
return (-1); return (-1);
} }
return (-2); return (-2);
@ -51,10 +71,25 @@ imsg_read(struct imsgbuf *ibuf)
ibuf->r.wpos += n; ibuf->r.wpos += n;
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
cmsg = CMSG_NXTHDR(&msg, cmsg)) {
if (cmsg->cmsg_level == SOL_SOCKET &&
cmsg->cmsg_type == SCM_RIGHTS) {
fd = (*(int *)CMSG_DATA(cmsg));
if ((ifd = calloc(1, sizeof(struct imsg_fd))) == NULL) {
/* XXX: this return can leak */
return (-1);
}
ifd->fd = fd;
TAILQ_INSERT_TAIL(&ibuf->fds, ifd, entry);
}
/* we do not handle other ctl data level */
}
return (n); return (n);
} }
int
ssize_t
imsg_get(struct imsgbuf *ibuf, struct imsg *imsg) imsg_get(struct imsgbuf *ibuf, struct imsg *imsg)
{ {
size_t av, left, datalen; size_t av, left, datalen;
@ -67,8 +102,7 @@ imsg_get(struct imsgbuf *ibuf, struct imsg *imsg)
memcpy(&imsg->hdr, ibuf->r.buf, sizeof(imsg->hdr)); memcpy(&imsg->hdr, ibuf->r.buf, sizeof(imsg->hdr));
if (imsg->hdr.len < IMSG_HEADER_SIZE || if (imsg->hdr.len < IMSG_HEADER_SIZE ||
imsg->hdr.len > MAX_IMSGSIZE) { imsg->hdr.len > MAX_IMSGSIZE) {
log_warnx("imsg_get: imsg hdr len %u out of bounds, type=%u",
imsg->hdr.len, imsg->hdr.type);
errno = ERANGE;
return (-1); return (-1);
} }
if (imsg->hdr.len > av) if (imsg->hdr.len > av)
@ -76,7 +110,6 @@ imsg_get(struct imsgbuf *ibuf, struct imsg *imsg)
datalen = imsg->hdr.len - IMSG_HEADER_SIZE; datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
ibuf->r.rptr = ibuf->r.buf + IMSG_HEADER_SIZE; ibuf->r.rptr = ibuf->r.buf + IMSG_HEADER_SIZE;
if ((imsg->data = malloc(datalen)) == NULL) { if ((imsg->data = malloc(datalen)) == NULL) {
log_warn("imsg_get");
return (-1); return (-1);
} }
memcpy(imsg->data, ibuf->r.rptr, datalen); memcpy(imsg->data, ibuf->r.rptr, datalen);
@ -92,11 +125,10 @@ imsg_get(struct imsgbuf *ibuf, struct imsg *imsg)
} }
int int
imsg_compose(struct imsgbuf *ibuf, enum imsg_type type, u_int32_t peerid,
pid_t pid, void *data, u_int16_t datalen)
imsg_compose(struct imsgbuf *ibuf, u_int16_t type, u_int32_t peerid,
pid_t pid, int fd, void *data, u_int16_t datalen)
{ {
struct buf *wbuf; struct buf *wbuf;
int n;
if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL) if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
return (-1); return (-1);
@ -104,32 +136,56 @@ imsg_compose(struct imsgbuf *ibuf, enum imsg_type type, u_int32_t peerid,
if (imsg_add(wbuf, data, datalen) == -1) if (imsg_add(wbuf, data, datalen) == -1)
return (-1); return (-1);
if ((n = imsg_close(ibuf, wbuf)) < 0)
wbuf->fd = fd;
imsg_close(ibuf, wbuf);
return (1);
}
int
imsg_composev(struct imsgbuf *ibuf, u_int16_t type, u_int32_t peerid,
pid_t pid, int fd, const struct iovec *iov, int iovcnt)
{
struct buf *wbuf;
int i, datalen = 0;
for (i = 0; i < iovcnt; i++)
datalen += iov[i].iov_len;
if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
return (-1); return (-1);
return (n);
for (i = 0; i < iovcnt; i++)
if (imsg_add(wbuf, iov[i].iov_base, iov[i].iov_len) == -1)
return (-1);
wbuf->fd = fd;
imsg_close(ibuf, wbuf);
return (1);
} }
/* ARGSUSED */
struct buf * struct buf *
imsg_create(struct imsgbuf *ibuf, enum imsg_type type, u_int32_t peerid,
imsg_create(struct imsgbuf *ibuf, u_int16_t type, u_int32_t peerid,
pid_t pid, u_int16_t datalen) pid_t pid, u_int16_t datalen)
{ {
struct buf *wbuf; struct buf *wbuf;
struct imsg_hdr hdr; struct imsg_hdr hdr;
if (datalen > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
log_warnx("imsg_create: len %u > MAX_IMSGSIZE; "
"type %u peerid %lu", datalen + IMSG_HEADER_SIZE,
type, peerid);
datalen += IMSG_HEADER_SIZE;
if (datalen > MAX_IMSGSIZE) {
errno = ERANGE;
return (NULL); return (NULL);
} }
hdr.len = datalen + IMSG_HEADER_SIZE;
hdr.type = type; hdr.type = type;
hdr.peerid = peerid; hdr.peerid = peerid;
hdr.pid = pid;
if ((wbuf = buf_open(hdr.len)) == NULL) {
log_warn("imsg_create: buf_open");
if ((hdr.pid = pid) == 0)
hdr.pid = ibuf->pid;
if ((wbuf = buf_dynamic(datalen, MAX_IMSGSIZE)) == NULL) {
return (NULL); return (NULL);
} }
if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1) if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1)
@ -143,24 +199,20 @@ imsg_add(struct buf *msg, void *data, u_int16_t datalen)
{ {
if (datalen) if (datalen)
if (buf_add(msg, data, datalen) == -1) { if (buf_add(msg, data, datalen) == -1) {
log_warnx("imsg_add: buf_add error");
buf_free(msg); buf_free(msg);
return (-1); return (-1);
} }
return (datalen); return (datalen);
} }
int
void
imsg_close(struct imsgbuf *ibuf, struct buf *msg) imsg_close(struct imsgbuf *ibuf, struct buf *msg)
{ {
int n;
struct imsg_hdr *hdr;
if ((n = buf_close(&ibuf->w, msg)) < 0) {
log_warnx("imsg_close: buf_close error");
buf_free(msg);
return (-1);
}
return (n);
hdr = (struct imsg_hdr *)msg->buf;
hdr->len = (u_int16_t)msg->wpos;
buf_close(&ibuf->w, msg);
} }
void void
@ -168,3 +220,35 @@ imsg_free(struct imsg *imsg)
{ {
free(imsg->data); free(imsg->data);
} }
int
imsg_get_fd(struct imsgbuf *ibuf)
{
int fd;
struct imsg_fd *ifd;
if ((ifd = TAILQ_FIRST(&ibuf->fds)) == NULL)
return (-1);
fd = ifd->fd;
TAILQ_REMOVE(&ibuf->fds, ifd, entry);
free(ifd);
return (fd);
}
int
imsg_flush(struct imsgbuf *ibuf)
{
while (ibuf->w.queued)
if (msgbuf_write(&ibuf->w) < 0)
return (-1);
return (0);
}
void
imsg_clear(struct imsgbuf *ibuf)
{
while (ibuf->w.queued)
msgbuf_clear(&ibuf->w);
}

+ 105
- 0
src/usr.sbin/ntpd/imsg.h View File

@ -0,0 +1,105 @@
/* $OpenBSD: imsg.h,v 1.1 2009/06/06 18:14:25 pyr Exp $ */
/*
* Copyright (c) 2006, 2007 Pierre-Yves Ritschard <pyr@openbsd.org>
* Copyright (c) 2006, 2007, 2008 Reyk Floeter <reyk@openbsd.org>
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/tree.h>
#define READ_BUF_SIZE 65535
#define IMSG_HEADER_SIZE sizeof(struct imsg_hdr)
#define MAX_IMSGSIZE 16384
struct buf {
TAILQ_ENTRY(buf) entry;
u_char *buf;
size_t size;
size_t max;
size_t wpos;
size_t rpos;
int fd;
};
struct msgbuf {
TAILQ_HEAD(, buf) bufs;
u_int32_t queued;
int fd;
};
struct buf_read {
u_char buf[READ_BUF_SIZE];
u_char *rptr;
size_t wpos;
};
struct imsg_fd {
TAILQ_ENTRY(imsg_fd) entry;
int fd;
};
struct imsgbuf {
TAILQ_HEAD(, imsg_fd) fds;
struct buf_read r;
struct msgbuf w;
int fd;
pid_t pid;
};
struct imsg_hdr {
u_int16_t type;
u_int16_t len;
u_int32_t peerid;
pid_t pid;
};
struct imsg {
struct imsg_hdr hdr;
void *data;
};
/* buffer.c */
struct buf *buf_open(size_t);
struct buf *buf_dynamic(size_t, size_t);
int buf_add(struct buf *, const void *, size_t);
void *buf_reserve(struct buf *, size_t);
void *buf_seek(struct buf *, size_t, size_t);
size_t buf_size(struct buf *);
size_t buf_left(struct buf *);
void buf_close(struct msgbuf *, struct buf *);
int buf_write(struct msgbuf *);
void buf_free(struct buf *);
void msgbuf_init(struct msgbuf *);
void msgbuf_clear(struct msgbuf *);
int msgbuf_write(struct msgbuf *);
/* imsg.c */
void imsg_init(struct imsgbuf *, int);
ssize_t imsg_read(struct imsgbuf *);
ssize_t imsg_get(struct imsgbuf *, struct imsg *);
int imsg_compose(struct imsgbuf *, u_int16_t, u_int32_t, pid_t,
int, void *, u_int16_t);
int imsg_composev(struct imsgbuf *, u_int16_t, u_int32_t, pid_t,
int, const struct iovec *, int);
struct buf *imsg_create(struct imsgbuf *, u_int16_t, u_int32_t, pid_t,
u_int16_t);
int imsg_add(struct buf *, void *, u_int16_t);
void imsg_close(struct imsgbuf *, struct buf *);
void imsg_free(struct imsg *);
int imsg_get_fd(struct imsgbuf *);
int imsg_flush(struct imsgbuf *);
void imsg_clear(struct imsgbuf *);

+ 6
- 5
src/usr.sbin/ntpd/ntp.c View File

@ -1,4 +1,4 @@
/* $OpenBSD: ntp.c,v 1.111 2009/06/04 23:39:45 ckuethe Exp $ */
/* $OpenBSD: ntp.c,v 1.112 2009/06/06 18:14:25 pyr Exp $ */
/* /*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@ -562,7 +562,7 @@ priv_adjfreq(double offset)
else if (freq < -MAX_FREQUENCY_ADJUST) else if (freq < -MAX_FREQUENCY_ADJUST)
freq = -MAX_FREQUENCY_ADJUST; freq = -MAX_FREQUENCY_ADJUST;
imsg_compose(ibuf_main, IMSG_ADJFREQ, 0, 0, &freq, sizeof(freq));
imsg_compose(ibuf_main, IMSG_ADJFREQ, 0, 0, -1, &freq, sizeof(freq));
conf->filters |= FILTER_ADJFREQ; conf->filters |= FILTER_ADJFREQ;
conf->freq.xy = 0.0; conf->freq.xy = 0.0;
conf->freq.x = 0.0; conf->freq.x = 0.0;
@ -633,7 +633,7 @@ priv_adjtime(void)
} }
conf->status.leap = offsets[i]->status.leap; conf->status.leap = offsets[i]->status.leap;
imsg_compose(ibuf_main, IMSG_ADJTIME, 0, 0,
imsg_compose(ibuf_main, IMSG_ADJTIME, 0, 0, -1,
&offset_median, sizeof(offset_median)); &offset_median, sizeof(offset_median));
priv_adjfreq(offset_median); priv_adjfreq(offset_median);
@ -680,7 +680,8 @@ offset_compare(const void *aa, const void *bb)
void void
priv_settime(double offset) priv_settime(double offset)
{ {
imsg_compose(ibuf_main, IMSG_SETTIME, 0, 0, &offset, sizeof(offset));
imsg_compose(ibuf_main, IMSG_SETTIME, 0, 0, -1,
&offset, sizeof(offset));
conf->settime = 0; conf->settime = 0;
} }
@ -690,7 +691,7 @@ priv_host_dns(char *name, u_int32_t peerid)
u_int16_t dlen; u_int16_t dlen;
dlen = strlen(name) + 1; dlen = strlen(name) + 1;
imsg_compose(ibuf_dns, IMSG_HOST_DNS, peerid, 0, name, dlen);
imsg_compose(ibuf_dns, IMSG_HOST_DNS, peerid, 0, -1, name, dlen);
} }
void void


+ 3
- 2
src/usr.sbin/ntpd/ntpd.c View File

@ -1,4 +1,4 @@
/* $OpenBSD: ntpd.c,v 1.65 2009/06/01 23:21:09 henning Exp $ */
/* $OpenBSD: ntpd.c,v 1.66 2009/06/06 18:14:25 pyr Exp $ */
/* /*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@ -289,7 +289,8 @@ dispatch_imsg(struct ntpd_conf *lconf)
fatalx("invalid IMSG_ADJTIME received"); fatalx("invalid IMSG_ADJTIME received");
memcpy(&d, imsg.data, sizeof(d)); memcpy(&d, imsg.data, sizeof(d));
n = ntpd_adjtime(d); n = ntpd_adjtime(d);
imsg_compose(ibuf, IMSG_ADJTIME, 0, 0, &n, sizeof(n));
imsg_compose(ibuf, IMSG_ADJTIME, 0, 0, -1,
&n, sizeof(n));
break; break;
case IMSG_ADJFREQ: case IMSG_ADJFREQ:
if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(d)) if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(d))


+ 2
- 68
src/usr.sbin/ntpd/ntpd.h View File

@ -1,4 +1,4 @@
/* $OpenBSD: ntpd.h,v 1.101 2009/06/04 23:39:46 ckuethe Exp $ */
/* $OpenBSD: ntpd.h,v 1.102 2009/06/06 18:14:25 pyr Exp $ */
/* /*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@ -30,13 +30,12 @@
#include <stdarg.h> #include <stdarg.h>
#include "ntp.h" #include "ntp.h"
#include <imsg.h>
#define NTPD_USER "_ntp" #define NTPD_USER "_ntp"
#define CONFFILE "/etc/ntpd.conf" #define CONFFILE "/etc/ntpd.conf"
#define DRIFTFILE "/var/db/ntpd.drift" #define DRIFTFILE "/var/db/ntpd.drift"
#define READ_BUF_SIZE 8192
#define INTERVAL_QUERY_NORMAL 30 /* sync to peers every n secs */ #define INTERVAL_QUERY_NORMAL 30 /* sync to peers every n secs */
#define INTERVAL_QUERY_PATHETIC 60 #define INTERVAL_QUERY_PATHETIC 60
#define INTERVAL_QUERY_AGGRESSIVE 5 #define INTERVAL_QUERY_AGGRESSIVE 5
@ -180,38 +179,6 @@ struct ntpd_conf {
u_int8_t filters; u_int8_t filters;
}; };
struct buf {
TAILQ_ENTRY(buf) entry;
u_char *buf;
size_t size;
size_t wpos;
size_t rpos;
};
struct msgbuf {
TAILQ_HEAD(, buf) bufs;
u_int32_t queued;
int fd;
};
struct buf_read {
size_t wpos;
u_char buf[READ_BUF_SIZE];
u_char *rptr;
};
/* ipc messages */
#define IMSG_HEADER_SIZE sizeof(struct imsg_hdr)
#define MAX_IMSGSIZE 8192
struct imsgbuf {
int fd;
pid_t pid;
struct buf_read r;
struct msgbuf w;
};
enum imsg_type { enum imsg_type {
IMSG_NONE, IMSG_NONE,
IMSG_ADJTIME, IMSG_ADJTIME,
@ -220,18 +187,6 @@ enum imsg_type {
IMSG_HOST_DNS IMSG_HOST_DNS
}; };
struct imsg_hdr {
enum imsg_type type;
u_int32_t peerid;
pid_t pid;
u_int16_t len;
};
struct imsg {
struct imsg_hdr hdr;
void *data;
};
/* prototypes */ /* prototypes */
/* log.c */ /* log.c */
void log_init(int); void log_init(int);
@ -244,27 +199,6 @@ void fatal(const char *);
void fatalx(const char *); void fatalx(const char *);
const char * log_sockaddr(struct sockaddr *); const char * log_sockaddr(struct sockaddr *);
/* buffer.c */
struct buf *buf_open(size_t);
int buf_add(struct buf *, void *, size_t);
int buf_close(struct msgbuf *, struct buf *);
void buf_free(struct buf *);
void msgbuf_init(struct msgbuf *);
void msgbuf_clear(struct msgbuf *);
int msgbuf_write(struct msgbuf *);
/* imsg.c */
void imsg_init(struct imsgbuf *, int);
int imsg_read(struct imsgbuf *);
int imsg_get(struct imsgbuf *, struct imsg *);
int imsg_compose(struct imsgbuf *, enum imsg_type, u_int32_t, pid_t,
void *, u_int16_t);
struct buf *imsg_create(struct imsgbuf *, enum imsg_type, u_int32_t, pid_t,
u_int16_t);
int imsg_add(struct buf *, void *, u_int16_t);
int imsg_close(struct imsgbuf *, struct buf *);
void imsg_free(struct imsg *);
/* ntp.c */ /* ntp.c */
pid_t ntp_main(int[2], struct ntpd_conf *, struct passwd *); pid_t ntp_main(int[2], struct ntpd_conf *, struct passwd *);
int priv_adjtime(void); int priv_adjtime(void);


Loading…
Cancel
Save