Browse Source

size struct pollfd and idx2peer dynamically instead of imposing an arbitary

limit on OPEN_MAX, modeled after bgpd
OPENBSD_3_6
henning 20 years ago
parent
commit
c1beef1cf2
3 changed files with 55 additions and 15 deletions
  1. +43
    -10
      src/usr.sbin/ntpd/ntp.c
  2. +5
    -3
      src/usr.sbin/ntpd/ntpd.h
  3. +7
    -2
      src/usr.sbin/ntpd/server.c

+ 43
- 10
src/usr.sbin/ntpd/ntp.c View File

@ -1,4 +1,4 @@
/* $OpenBSD: ntp.c,v 1.7 2004/06/17 19:17:48 henning Exp $ */
/* $OpenBSD: ntp.c,v 1.8 2004/06/18 04:51:31 henning Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@ -29,6 +29,7 @@
#include "ntp.h"
#define PFD_PIPE_MAIN 0
#define PFD_MAX 1
volatile sig_atomic_t ntp_quit = 0;
struct imsgbuf ibuf_main;
@ -53,14 +54,17 @@ pid_t
ntp_main(int pipe_prnt[2], struct ntpd_conf *conf)
{
int nfds, i, j, idx_peers, timeout;
u_int pfd_elms = 0, idx2peer_elms = 0;
u_int listener_cnt, peer_cnt, new_cnt;
pid_t pid;
struct pollfd pfd[OPEN_MAX];
struct pollfd *pfd = NULL;
struct passwd *pw;
struct servent *se;
struct listen_addr *la;
struct ntp_peer *p;
struct ntp_peer *idx2peer[OPEN_MAX];
struct ntp_peer **idx2peer = NULL;
time_t nextaction;
void *newp;
switch (pid = fork()) {
case -1:
@ -84,7 +88,7 @@ ntp_main(int pipe_prnt[2], struct ntpd_conf *conf)
setproctitle("ntp engine");
setup_listeners(se, conf);
setup_listeners(se, conf, &listener_cnt);
if (setgroups(1, &pw->pw_gid) ||
setegid(pw->pw_gid) || setgid(pw->pw_gid) ||
@ -106,9 +110,42 @@ ntp_main(int pipe_prnt[2], struct ntpd_conf *conf)
log_info("ntp engine ready");
peer_cnt = 0;
TAILQ_FOREACH(p, &conf->ntp_peers, entry)
peer_cnt++;
while (ntp_quit == 0) {
bzero(&pfd, sizeof(pfd));
bzero(idx2peer, sizeof(idx2peer));
if (peer_cnt > idx2peer_elms ||
peer_cnt + IDX2PEER_RESERVE < idx2peer_elms) {
if ((newp = realloc(idx2peer, sizeof(void *) *
peer_cnt + IDX2PEER_RESERVE)) == NULL) {
/* panic for now */
log_warn("could not resize idx2peer from %u -> "
"%u entries", idx2peer_elms,
peer_cnt + IDX2PEER_RESERVE);
fatalx("exiting");
}
idx2peer = newp;
idx2peer_elms = peer_cnt + IDX2PEER_RESERVE;
}
new_cnt = PFD_MAX + peer_cnt + listener_cnt;
if (new_cnt > pfd_elms ||
new_cnt + PFD_RESERVE < pfd_elms) {
if ((newp = realloc(pfd, sizeof(void *) *
new_cnt + PFD_RESERVE)) == NULL) {
/* panic for now */
log_warn("could not resize pfd from %u -> "
"%u entries", pfd_elms,
new_cnt + PFD_RESERVE);
fatalx("exiting");
}
pfd = newp;
pfd_elms = new_cnt + PFD_RESERVE;
}
bzero(pfd, sizeof(struct pollfd) * pfd_elms);
bzero(idx2peer, sizeof(struct ntp_peer) * idx2peer_elms);
nextaction = time(NULL) + 240;
pfd[PFD_PIPE_MAIN].fd = ibuf_main.fd;
pfd[PFD_PIPE_MAIN].events = POLLIN;
@ -120,8 +157,6 @@ ntp_main(int pipe_prnt[2], struct ntpd_conf *conf)
pfd[i].fd = la->fd;
pfd[i].events = POLLIN;
i++;
if (i > OPEN_MAX)
fatal("i > OPEN_MAX");
}
@ -138,8 +173,6 @@ ntp_main(int pipe_prnt[2], struct ntpd_conf *conf)
pfd[i].events = POLLIN;
idx2peer[i - idx_peers] = p;
i++;
if (i > OPEN_MAX)
fatal("i > OPEN_MAX");
}
}


+ 5
- 3
src/usr.sbin/ntpd/ntpd.h View File

@ -1,4 +1,4 @@
/* $OpenBSD: ntpd.h,v 1.6 2004/06/17 19:17:48 henning Exp $ */
/* $OpenBSD: ntpd.h,v 1.7 2004/06/18 04:51:31 henning Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@ -29,7 +29,9 @@
#define NTPD_USER "_ntp"
#define CONFFILE "/etc/ntpd.conf"
#define READ_BUF_SIZE 65535
#define READ_BUF_SIZE 65535
#define IDX2PEER_RESERVE 5
#define PFD_RESERVE 10
#define NTPD_OPT_VERBOSE 0x0001
#define NTPD_OPT_VERBOSE2 0x0002
@ -159,7 +161,7 @@ int ntp_getmsg(char *, ssize_t, struct ntp_msg *);
int ntp_sendmsg(int, struct sockaddr *, struct ntp_msg *, ssize_t, int);
/* server.c */
int setup_listeners(struct servent *, struct ntpd_conf *);
int setup_listeners(struct servent *, struct ntpd_conf *, u_int *);
int ntp_reply(int, struct sockaddr *, struct ntp_msg *, int);
/* client.c */


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

@ -1,4 +1,4 @@
/* $OpenBSD: server.c,v 1.1 2004/06/02 10:08:59 henning Exp $ */
/* $OpenBSD: server.c,v 1.2 2004/06/18 04:51:31 henning Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@ -26,9 +26,10 @@
#include "ntp.h"
int
setup_listeners(struct servent *se, struct ntpd_conf *conf)
setup_listeners(struct servent *se, struct ntpd_conf *conf, u_int *cnt)
{
struct listen_addr *la;
u_int new_cnt = 0;
if (TAILQ_EMPTY(&conf->listen_addrs)) {
if ((la = calloc(1, sizeof(struct listen_addr))) == NULL)
@ -49,6 +50,8 @@ setup_listeners(struct servent *se, struct ntpd_conf *conf)
}
TAILQ_FOREACH(la, &conf->listen_addrs, entry) {
new_cnt++;
switch (la->sa.ss_family) {
case AF_INET:
if (((struct sockaddr_in *)&la->sa)->sin_port == 0)
@ -73,6 +76,8 @@ setup_listeners(struct servent *se, struct ntpd_conf *conf)
fatal("bind");
}
*cnt = new_cnt;
return (0);
}


Loading…
Cancel
Save