diff options
author | Theo de Raadt <deraadt@cvs.openbsd.org> | 2013-03-21 04:43:18 +0000 |
---|---|---|
committer | Theo de Raadt <deraadt@cvs.openbsd.org> | 2013-03-21 04:43:18 +0000 |
commit | 754cb70adb1ca0ca659a5ce03277ef4d80963a6c (patch) | |
tree | c99c4d3ade0562fb129f705dbc06ba1ad3e5ac59 /usr.sbin | |
parent | 58df33a7a6c5d7ffd49bd390800651664a203c43 (diff) |
create realloc() loops around sysctl for array-based mibs, in programs
which want a "full" dump
ok dlg
Diffstat (limited to 'usr.sbin')
-rw-r--r-- | usr.sbin/arp/arp.c | 28 | ||||
-rw-r--r-- | usr.sbin/ndp/ndp.c | 25 | ||||
-rw-r--r-- | usr.sbin/route6d/route6d.c | 30 | ||||
-rw-r--r-- | usr.sbin/rtadvd/if.c | 24 | ||||
-rw-r--r-- | usr.sbin/rwhod/rwhod.c | 24 |
5 files changed, 79 insertions, 52 deletions
diff --git a/usr.sbin/arp/arp.c b/usr.sbin/arp/arp.c index 95e079cf7ff..56b8390f954 100644 --- a/usr.sbin/arp/arp.c +++ b/usr.sbin/arp/arp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: arp.c,v 1.51 2012/11/08 11:05:41 phessler Exp $ */ +/* $OpenBSD: arp.c,v 1.52 2013/03/21 04:43:17 deraadt Exp $ */ /* $NetBSD: arp.c,v 1.12 1995/04/24 13:25:18 cgd Exp $ */ /* @@ -442,7 +442,7 @@ search(in_addr_t addr, void (*action)(struct sockaddr_dl *sdl, { int mib[7]; size_t needed; - char *lim, *buf, *next; + char *lim, *buf = NULL, *next; struct rt_msghdr *rtm; struct sockaddr_inarp *sin; struct sockaddr_dl *sdl; @@ -454,15 +454,21 @@ search(in_addr_t addr, void (*action)(struct sockaddr_dl *sdl, mib[4] = NET_RT_FLAGS; mib[5] = RTF_LLINFO; mib[6] = rdomain; - if (sysctl(mib, 7, NULL, &needed, NULL, 0) < 0) - err(1, "route-sysctl-estimate"); - if (needed == 0) - return; - if ((buf = malloc(needed)) == NULL) - err(1, "malloc"); - if (sysctl(mib, 7, buf, &needed, NULL, 0) < 0) - err(1, "actual retrieval of routing table"); - lim = buf + needed; + while (1) { + if (sysctl(mib, 7, NULL, &needed, NULL, 0) == -1) + err(1, "route-sysctl-estimate"); + if (needed == 0) + return; + if ((buf = realloc(buf, needed)) == NULL) + err(1, "malloc"); + if (sysctl(mib, 7, buf, &needed, NULL, 0) == -1) { + if (errno == ENOMEM) + continue; + err(1, "actual retrieval of routing table"); + } + lim = buf + needed; + break; + } for (next = buf; next < lim; next += rtm->rtm_msglen) { rtm = (struct rt_msghdr *)next; if (rtm->rtm_version != RTM_VERSION) diff --git a/usr.sbin/ndp/ndp.c b/usr.sbin/ndp/ndp.c index 5fb2db7cd29..28aed85098b 100644 --- a/usr.sbin/ndp/ndp.c +++ b/usr.sbin/ndp/ndp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ndp.c,v 1.46 2013/03/11 14:02:37 mpi Exp $ */ +/* $OpenBSD: ndp.c,v 1.47 2013/03/21 04:43:17 deraadt Exp $ */ /* $KAME: ndp.c,v 1.101 2002/07/17 08:46:33 itojun Exp $ */ /* @@ -544,7 +544,7 @@ dump(struct in6_addr *addr, int cflag) { int mib[6]; size_t needed; - char *lim, *buf, *next; + char *lim, *buf = NULL, *next; struct rt_msghdr *rtm; struct sockaddr_in6 *sin; struct sockaddr_dl *sdl; @@ -569,16 +569,21 @@ again:; mib[3] = AF_INET6; mib[4] = NET_RT_FLAGS; mib[5] = RTF_LLINFO; - if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) - err(1, "sysctl(PF_ROUTE estimate)"); - if (needed > 0) { - if ((buf = malloc(needed)) == NULL) - err(1, "malloc"); - if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) + while (1) { + if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1) + err(1, "sysctl(PF_ROUTE estimate)"); + if (needed == 0) + break; + if ((buf = realloc(buf, needed)) == NULL) + err(1, "realloc"); + if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1) { + if (errno == ENOMEM) + continue; err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)"); + } lim = buf + needed; - } else - buf = lim = NULL; + break; + } for (next = buf; next && next < lim; next += rtm->rtm_msglen) { int isrouter = 0, prbs = 0; diff --git a/usr.sbin/route6d/route6d.c b/usr.sbin/route6d/route6d.c index 00ba8e12a0f..450e7140db4 100644 --- a/usr.sbin/route6d/route6d.c +++ b/usr.sbin/route6d/route6d.c @@ -1,4 +1,4 @@ -/* $OpenBSD: route6d.c,v 1.55 2009/10/27 23:59:54 deraadt Exp $ */ +/* $OpenBSD: route6d.c,v 1.56 2013/03/21 04:43:17 deraadt Exp $ */ /* $KAME: route6d.c,v 1.111 2006/10/25 06:38:13 jinmei Exp $ */ /* @@ -2257,8 +2257,8 @@ int getifmtu(int ifindex) { int mib[6]; - char *buf; - size_t msize; + char *buf = NULL; + size_t needed; struct if_msghdr *ifm; int mtu; @@ -2268,17 +2268,19 @@ getifmtu(int ifindex) mib[3] = AF_INET6; mib[4] = NET_RT_IFLIST; mib[5] = ifindex; - if (sysctl(mib, 6, NULL, &msize, NULL, 0) < 0) { - fatal("sysctl estimate NET_RT_IFLIST"); - /*NOTREACHED*/ - } - if ((buf = malloc(msize)) == NULL) { - fatal("malloc"); - /*NOTREACHED*/ - } - if (sysctl(mib, 6, buf, &msize, NULL, 0) < 0) { - fatal("sysctl NET_RT_IFLIST"); - /*NOTREACHED*/ + while (1) { + if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1) + fatal("sysctl estimate NET_RT_IFLIST"); + if (needed == 0) + break; + if ((buf = realloc(buf, needed)) == NULL) + fatal("malloc"); + if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1) { + if (errno == ENOMEM) + continue; + fatal("sysctl NET_RT_IFLIST"); + } + break; } ifm = (struct if_msghdr *)buf; mtu = ifm->ifm_data.ifi_mtu; diff --git a/usr.sbin/rtadvd/if.c b/usr.sbin/rtadvd/if.c index 0917ff2851c..eb3f464adbc 100644 --- a/usr.sbin/rtadvd/if.c +++ b/usr.sbin/rtadvd/if.c @@ -1,4 +1,4 @@ -/* $OpenBSD: if.c,v 1.24 2011/07/05 05:13:04 claudio Exp $ */ +/* $OpenBSD: if.c,v 1.25 2013/03/21 04:43:15 deraadt Exp $ */ /* $KAME: if.c,v 1.17 2001/01/21 15:27:30 itojun Exp $ */ /* @@ -437,14 +437,20 @@ get_iflist(char **buf, size_t *size) mib[3] = AF_INET6; mib[4] = NET_RT_IFLIST; mib[5] = 0; - - if (sysctl(mib, 6, NULL, size, NULL, 0) < 0) - fatal("sysctl: iflist size get failed"); - if ((*buf = malloc(*size)) == NULL) - fatal("malloc"); - if (sysctl(mib, 6, *buf, size, NULL, 0) < 0) - fatal("sysctl: iflist get failed"); - return; + while (1) { + if (sysctl(mib, 6, NULL, size, NULL, 0) == -1) + fatal("sysctl: iflist size get failed"); + if (*size == 0) + break; + if ((*buf = realloc(*buf, *size)) == NULL) + fatal("malloc"); + if (sysctl(mib, 6, *buf, size, NULL, 0) == -1) { + if (errno == ENOMEM) + continue; + fatal("sysctl: iflist get failed"); + } + break; + } } /* diff --git a/usr.sbin/rwhod/rwhod.c b/usr.sbin/rwhod/rwhod.c index 5a77492d007..61b6c8288a5 100644 --- a/usr.sbin/rwhod/rwhod.c +++ b/usr.sbin/rwhod/rwhod.c @@ -469,7 +469,7 @@ configure(void) struct sockaddr_dl *sdl; size_t needed; int mib[6], flags = 0, len; - char *buf, *lim, *next; + char *buf = NULL, *lim, *next; struct rt_addrinfo info; mib[0] = CTL_NET; @@ -478,13 +478,21 @@ configure(void) mib[3] = AF_INET; mib[4] = NET_RT_IFLIST; mib[5] = 0; - if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) - quit("route-sysctl-estimate"); - if ((buf = malloc(needed)) == NULL) - quit("malloc"); - if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) - quit("actual retrieval of interface table"); - lim = buf + needed; + while (1) { + if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1) + quit("route-sysctl-estimate"); + if (needed == 0) + break; + if ((buf = realloc(buf, needed)) == NULL) + quit("realloc"); + if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1) { + if (errno == ENOMEM) + continue; + quit("actual retrieval of interface table"); + } + lim = buf + needed; + break; + } sdl = NULL; /* XXX just to keep gcc -Wall happy */ for (next = buf; next < lim; next += ifm->ifm_msglen) { |