The interface index space may be sparsely populated (e.g. when an

interface in the middle is if_detach()'d).  Return (and handle)
 ENOENT when the ifmib(4) is accessed for a nonexistent interface.

MFC after: 14 days
This commit is contained in:
Bill Fenner 2001-10-17 04:12:29 +00:00
parent 2856a77139
commit b9d45cebf4
5 changed files with 22 additions and 4 deletions

View File

@ -51,6 +51,7 @@ static const char rcsid[] =
#include <sys/param.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <kvm.h>
#include <limits.h>
@ -294,6 +295,9 @@ updatestat()
mib[4] = i;
mib[5] = IFDATA_GENERAL;
if (sysctl(mib, 6, &ifmd, &len, 0, 0) < 0) {
if (errno == ENOENT)
continue;
syslog(LOG_ERR, "sysctl(net.link.ifdata.%d.general)"
": %m", i);
exit(1);

View File

@ -76,7 +76,7 @@ MIB. The manifest constants for each level in the
.Ar name
are defined in
.Aq Pa net/if_mib.h .
A count of interfaces (and thus rows in the table) is given by
The index of the last row in the table is given by
.Dq Li net.link.generic.system.ifcount
(or, using the manifest constants,
.Dv CTL_NET ,
@ -87,6 +87,10 @@ A count of interfaces (and thus rows in the table) is given by
A management application searching for a particular interface should
start with row 1 and continue through the table row-by-row until the
desired interface is found, or the interface count is reached.
Note that the table may be sparse, i.e. a given row may not exist,
indicated by an errno of
.Er ENOENT .
Such an error should be ignored, and the next row should be checked.
.Pp
The generic interface information, common to all interfaces,
can be accessed via the following procedure:

View File

@ -80,7 +80,8 @@ sysctl_ifdata(SYSCTL_HANDLER_ARGS) /* XXX bad syntax! */
if (namelen != 2)
return EINVAL;
if (name[0] <= 0 || name[0] > if_index)
if (name[0] <= 0 || name[0] > if_index ||
ifaddr_byindex(name[0]) == NULL)
return ENOENT;
ifp = ifaddr_byindex(name[0])->ifa_ifp;

View File

@ -34,6 +34,7 @@
#include <sys/time.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -99,9 +100,13 @@ main(int argc, char **argv)
name[3] = IFMIB_IFDATA;
name[4] = i;
name[5] = IFDATA_GENERAL;
if (sysctl(name, 6, &ifmd, &len, 0, 0) < 0)
if (sysctl(name, 6, &ifmd, &len, 0, 0) < 0) {
if (errno == ENOENT)
continue;
err(EX_OSERR, "sysctl(net.link.ifdata.%d.general)",
i);
}
if (!isit(argc - optind, argv + optind, ifmd.ifmd_name))
continue;

View File

@ -120,8 +120,12 @@ main(argc, argv)
for (i = 1; ; i++) {
name[4] = i;
if (sysctl(name, 6, &ifmd, &len, 0, 0) < 0)
if (sysctl(name, 6, &ifmd, &len, 0, 0) < 0) {
if (errno == ENOENT)
continue;
err(1, "sysctl");
}
if (strncmp(interface, ifmd.ifmd_name, IFNAMSIZ) == 0
&& ifmd.ifmd_data.ifi_type == IFT_SLIP) {
indx = i;