Browse Source

malloc the imsg buffers instead of having them statically, suggested by

micsky some time ago, ok otto
OPENBSD_3_7
henning 20 years ago
parent
commit
1376183807
2 changed files with 32 additions and 25 deletions
  1. +15
    -12
      src/usr.sbin/ntpd/ntp.c
  2. +17
    -13
      src/usr.sbin/ntpd/ntpd.c

+ 15
- 12
src/usr.sbin/ntpd/ntp.c View File

@ -1,4 +1,4 @@
/* $OpenBSD: ntp.c,v 1.29 2004/09/15 00:08:06 henning Exp $ */
/* $OpenBSD: ntp.c,v 1.30 2004/09/15 19:14:11 henning Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@ -33,7 +33,7 @@
#define PFD_MAX 1
volatile sig_atomic_t ntp_quit = 0;
struct imsgbuf ibuf_main;
struct imsgbuf *ibuf_main;
struct ntpd_conf *conf;
u_int peer_cnt;
@ -108,7 +108,9 @@ ntp_main(int pipe_prnt[2], struct ntpd_conf *nconf)
signal(SIGHUP, SIG_IGN);
close(pipe_prnt[0]);
imsg_init(&ibuf_main, pipe_prnt[1]);
if ((ibuf_main = malloc(sizeof(struct imsgbuf))) == NULL)
fatal(NULL);
imsg_init(ibuf_main, pipe_prnt[1]);
TAILQ_FOREACH(p, &conf->ntp_peers, entry)
client_peer_init(p);
@ -155,7 +157,7 @@ ntp_main(int pipe_prnt[2], struct ntpd_conf *nconf)
bzero(pfd, sizeof(struct pollfd) * pfd_elms);
bzero(idx2peer, sizeof(void *) * idx2peer_elms);
nextaction = time(NULL) + 3600;
pfd[PFD_PIPE_MAIN].fd = ibuf_main.fd;
pfd[PFD_PIPE_MAIN].fd = ibuf_main->fd;
pfd[PFD_PIPE_MAIN].events = POLLIN;
i = 1;
@ -195,7 +197,7 @@ ntp_main(int pipe_prnt[2], struct ntpd_conf *nconf)
}
}
if (ibuf_main.w.queued > 0)
if (ibuf_main->w.queued > 0)
pfd[PFD_PIPE_MAIN].events |= POLLOUT;
timeout = nextaction - time(NULL);
@ -209,7 +211,7 @@ ntp_main(int pipe_prnt[2], struct ntpd_conf *nconf)
}
if (nfds > 0 && (pfd[PFD_PIPE_MAIN].revents & POLLOUT))
if (msgbuf_write(&ibuf_main.w) < 0) {
if (msgbuf_write(&ibuf_main->w) < 0) {
log_warn("pipe write error (to parent)");
ntp_quit = 1;
}
@ -236,8 +238,9 @@ ntp_main(int pipe_prnt[2], struct ntpd_conf *nconf)
}
}
msgbuf_write(&ibuf_main.w);
msgbuf_clear(&ibuf_main.w);
msgbuf_write(&ibuf_main->w);
msgbuf_clear(&ibuf_main->w);
free(ibuf_main);
log_info("ntp engine exiting");
_exit(0);
@ -253,7 +256,7 @@ ntp_dispatch_imsg(void)
u_char *p;
struct ntp_addr *h;
if ((n = imsg_read(&ibuf_main)) == -1)
if ((n = imsg_read(ibuf_main)) == -1)
return (-1);
if (n == 0) { /* connection closed */
@ -262,7 +265,7 @@ ntp_dispatch_imsg(void)
}
for (;;) {
if ((n = imsg_get(&ibuf_main, &imsg)) == -1)
if ((n = imsg_get(ibuf_main, &imsg)) == -1)
return (-1);
if (n == 0)
@ -351,7 +354,7 @@ ntp_adjtime(void)
if (offset_cnt > 0) {
offset_median /= offset_cnt;
imsg_compose(&ibuf_main, IMSG_ADJTIME, 0,
imsg_compose(ibuf_main, IMSG_ADJTIME, 0,
&offset_median, sizeof(offset_median));
conf->status.reftime = gettime();
@ -368,5 +371,5 @@ ntp_host_dns(char *name, u_int32_t peerid)
u_int16_t dlen;
dlen = strlen(name) + 1;
imsg_compose(&ibuf_main, IMSG_HOST_DNS, peerid, name, dlen);
imsg_compose(ibuf_main, IMSG_HOST_DNS, peerid, name, dlen);
}

+ 17
- 13
src/usr.sbin/ntpd/ntpd.c View File

@ -1,4 +1,4 @@
/* $OpenBSD: ntpd.c,v 1.15 2004/09/15 00:08:06 henning Exp $ */
/* $OpenBSD: ntpd.c,v 1.16 2004/09/15 19:14:11 henning Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@ -40,10 +40,10 @@ int check_child(pid_t, const char *);
int dispatch_imsg(void);
void ntpd_adjtime(double);
volatile sig_atomic_t quit = 0;
volatile sig_atomic_t reconfig = 0;
volatile sig_atomic_t sigchld = 0;
struct imsgbuf ibuf;
volatile sig_atomic_t quit = 0;
volatile sig_atomic_t reconfig = 0;
volatile sig_atomic_t sigchld = 0;
struct imsgbuf *ibuf;
void
sighdlr(int sig)
@ -139,12 +139,14 @@ main(int argc, char *argv[])
close(pipe_chld[1]);
imsg_init(&ibuf, pipe_chld[0]);
if ((ibuf = malloc(sizeof(struct imsgbuf))) == NULL)
fatal(NULL);
imsg_init(ibuf, pipe_chld[0]);
while (quit == 0) {
pfd[PFD_PIPE].fd = ibuf.fd;
pfd[PFD_PIPE].fd = ibuf->fd;
pfd[PFD_PIPE].events = POLLIN;
if (ibuf.w.queued)
if (ibuf->w.queued)
pfd[PFD_PIPE].events |= POLLOUT;
if ((nfds = poll(pfd, 1, INFTIM)) == -1)
@ -154,7 +156,7 @@ main(int argc, char *argv[])
}
if (nfds > 0 && (pfd[PFD_PIPE].revents & POLLOUT))
if (msgbuf_write(&ibuf.w) < 0) {
if (msgbuf_write(&ibuf->w) < 0) {
log_warn("pipe write error (to child");
quit = 1;
}
@ -184,6 +186,8 @@ main(int argc, char *argv[])
fatal("wait");
} while (pid != -1 || (pid == -1 && errno == EINTR));
msgbuf_clear(&ibuf->w);
free(ibuf);
log_info("Terminating");
return (0);
}
@ -218,7 +222,7 @@ dispatch_imsg(void)
struct ntp_addr *h, *hn;
struct buf *buf;
if ((n = imsg_read(&ibuf)) == -1)
if ((n = imsg_read(ibuf)) == -1)
return (-1);
if (n == 0) { /* connection closed */
@ -227,7 +231,7 @@ dispatch_imsg(void)
}
for (;;) {
if ((n = imsg_get(&ibuf, &imsg)) == -1)
if ((n = imsg_get(ibuf, &imsg)) == -1)
return (-1);
if (n == 0)
@ -245,7 +249,7 @@ dispatch_imsg(void)
if (imsg.hdr.len != strlen(name) + 1 + IMSG_HEADER_SIZE)
fatal("invalid IMSG_HOST_DNS received");
if ((cnt = host_dns(name, &hn)) > 0) {
buf = imsg_create(&ibuf, IMSG_HOST_DNS,
buf = imsg_create(ibuf, IMSG_HOST_DNS,
imsg.hdr.peerid,
cnt * sizeof(struct sockaddr_storage));
if (buf == NULL)
@ -253,7 +257,7 @@ dispatch_imsg(void)
for (h = hn; h != NULL; h = h->next) {
imsg_add(buf, &h->ss, sizeof(h->ss));
}
imsg_close(&ibuf, buf);
imsg_close(ibuf, buf);
}
break;
default:


Loading…
Cancel
Save