Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.sbin Use asysctl.



details:   https://anonhg.NetBSD.org/src/rev/cbe6ad73cbe6
branches:  trunk
changeset: 329910:cbe6ad73cbe6
user:      joerg <joerg%NetBSD.org@localhost>
date:      Fri Jun 13 16:00:55 2014 +0000

description:
Use asysctl.

diffstat:

 usr.sbin/ifmcstat/ifmcstat.c |  56 +++++++++++++++++--------------------------
 usr.sbin/mlxctl/dklist.c     |  31 +++---------------------
 2 files changed, 26 insertions(+), 61 deletions(-)

diffs (168 lines):

diff -r ac145f2e652c -r cbe6ad73cbe6 usr.sbin/ifmcstat/ifmcstat.c
--- a/usr.sbin/ifmcstat/ifmcstat.c      Fri Jun 13 15:51:13 2014 +0000
+++ b/usr.sbin/ifmcstat/ifmcstat.c      Fri Jun 13 16:00:55 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ifmcstat.c,v 1.18 2014/06/11 14:05:28 joerg Exp $      */
+/*     $NetBSD: ifmcstat.c,v 1.19 2014/06/13 16:04:41 joerg Exp $      */
 
 /*
  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
@@ -29,7 +29,7 @@
  * SUCH DAMAGE.
  */
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: ifmcstat.c,v 1.18 2014/06/11 14:05:28 joerg Exp $");
+__RCSID("$NetBSD: ifmcstat.c,v 1.19 2014/06/13 16:04:41 joerg Exp $");
 
 #include <err.h>
 #include <errno.h>
@@ -100,31 +100,6 @@
        /*NOTREACHED*/
 }
 
-static void *
-fetch_sysctl(size_t *len, const int oids[], size_t oidlen, const char *msg)
-{
-       void *data;
-
-       *len = 0;
-       data = NULL;
-
-       for (;;) {
-               if (sysctl(oids, oidlen, data, len, NULL, 0) == 0) {
-                       if (data != NULL || len == 0)
-                               return data;
-                       errno = ENOMEM;
-               }
-               free(data);
-               if (errno == ENOMEM) {
-                       data = emalloc(*len);
-                       continue;
-               }
-               if (errno != ENODEV)
-                       warn("%s", msg);
-               return NULL;
-       }
-}
-
 static void
 print_hwaddr(const uint8_t *hwaddr, size_t len)
 {
@@ -157,18 +132,25 @@
        }
 
        sdl_oids[2] = ifindex;
-       hwaddr = fetch_sysctl(&sdl_len, sdl_oids, 3, "failed to read net.sdl");
+       hwaddr = asysctl(sdl_oids, 3, &sdl_len);
 
        if (sdl_len == 0) {
                free(hwaddr);
                return;
        }
+       if (hwaddr == NULL) {
+               warn("failed to read net.sdl");
+       }
 
        ems_oids[3] = ifindex;
-       ems = fetch_sysctl(&ems_len, ems_oids, 4,
-           "failed to read net.ether.multicast");
+       ems = asysctl(ems_oids, 4, &ems_len);
        ems_len /= sizeof(*ems);
 
+       if (ems == NULL && ems_len != 0) {
+               warn("failed to read net.ether.multicast");
+               return;
+       }
+
        printf("\tenaddr ");
        print_hwaddr(hwaddr, sdl_len);
        printf(" multicnt %zu\n", ems_len);
@@ -214,8 +196,11 @@
        mcast_oids[3] = ifindex;
        kludge_oids[3] = ifindex;
 
-       mcast_addrs = fetch_sysctl(&len, mcast_oids, 4,
-           "failed to read net.inet6.multicast");
+       mcast_addrs = asysctl(mcast_oids, 4, &len);
+       if (mcast_addrs == NULL && len != 0) {
+               warn("failed to read net.inet6.multicast");
+               return;
+       }
        if (len) {
                p = mcast_addrs;
                last_p = NULL;
@@ -236,8 +221,11 @@
        }
        free(mcast_addrs);
 
-       kludge_addrs = fetch_sysctl(&len, kludge_oids, 4,
-           "failed to read net.inet6.multicast_kludge");
+       kludge_addrs = asysctl(kludge_oids, 4, &len);
+       if (kludge_addrs == NULL && len != 0) {
+               warn("failed to read net.inet6.multicast_kludge");
+               return;
+       }
        if (len) {
                printf("\t(on kludge entry for %s)\n", ifname);
                p = kludge_addrs;
diff -r ac145f2e652c -r cbe6ad73cbe6 usr.sbin/mlxctl/dklist.c
--- a/usr.sbin/mlxctl/dklist.c  Fri Jun 13 15:51:13 2014 +0000
+++ b/usr.sbin/mlxctl/dklist.c  Fri Jun 13 16:00:55 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: dklist.c,v 1.10 2014/06/11 14:51:49 joerg Exp $        */
+/*     $NetBSD: dklist.c,v 1.11 2014/06/13 16:00:55 joerg Exp $        */
 
 /*-
  * Copyright (c) 2001 The NetBSD Foundation, Inc.
@@ -63,7 +63,7 @@
 
 #ifndef lint
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: dklist.c,v 1.10 2014/06/11 14:51:49 joerg Exp $");
+__RCSID("$NetBSD: dklist.c,v 1.11 2014/06/13 16:00:55 joerg Exp $");
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -146,29 +146,6 @@
                errx(EXIT_FAILURE, "%s is not attached to %s", name, mlxname);
 }
 
-static void *
-fetch_sysctl(size_t *len, const int oids[], size_t oidlen, const char *msg)
-{
-       void *data;
-
-       *len = 0;
-       data = NULL;
-
-       for (;;) {
-               if (sysctl(oids, oidlen, data, len, NULL, 0) == 0) {
-                       if (data != NULL || len == 0)
-                               return data;
-                       errno = ENOMEM;
-               }
-               free(data);
-               if (errno == ENOMEM) {
-                       data = emalloc(*len);
-                       continue;
-               }
-               err(1, "%s", msg);
-       }
-}
-
 void
 mlx_disk_add_all(void)
 {
@@ -176,10 +153,10 @@
        size_t i, len;
        static const int mib[3] = { CTL_HW, HW_IOSTATS, sizeof(*data) };
 
-       data = fetch_sysctl(&len, mib, __arraycount(mib), "hw.iostats failed");
+       data = asysctl(mib, __arraycount(mib), &len);
        len /= sizeof(*data);
 
-       if (len == 0)
+       if (data == NULL || len == 0)
                errx(EXIT_FAILURE, "no drives attached.");
 
        for (i = 0; i < len; ++i) {



Home | Main Index | Thread Index | Old Index