Source-Changes-HG archive

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

[src/trunk]: src/sbin/ccdconfig one less kvm groveller. Use sysctl to get ccd...



details:   https://anonhg.NetBSD.org/src/rev/0afd6b249c90
branches:  trunk
changeset: 786371:0afd6b249c90
user:      christos <christos%NetBSD.org@localhost>
date:      Sat Apr 27 17:12:36 2013 +0000

description:
one less kvm groveller. Use sysctl to get ccd info.

diffstat:

 sbin/ccdconfig/Makefile    |    9 +-
 sbin/ccdconfig/ccdconfig.8 |   14 +-
 sbin/ccdconfig/ccdconfig.c |  339 +++++++++++++++-----------------------------
 3 files changed, 119 insertions(+), 243 deletions(-)

diffs (truncated from 525 to 300 lines):

diff -r 58bde69dd58a -r 0afd6b249c90 sbin/ccdconfig/Makefile
--- a/sbin/ccdconfig/Makefile   Sat Apr 27 16:56:29 2013 +0000
+++ b/sbin/ccdconfig/Makefile   Sat Apr 27 17:12:36 2013 +0000
@@ -1,14 +1,11 @@
-#      $NetBSD: Makefile,v 1.8 2007/05/28 12:06:22 tls Exp $
+#      $NetBSD: Makefile,v 1.9 2013/04/27 17:12:36 christos Exp $
 
 USE_FORT?=yes  # setgid
 
 PROG=  ccdconfig
 MAN=   ccdconfig.8 ccd.conf.5
 
-BINGRP=        kmem
-BINMODE=2555
-
-DPADD= ${LIBKVM} ${LIBUTIL}
-LDADD= -lkvm -lutil
+DPADD= ${LIBUTIL}
+LDADD= -lutil
 
 .include <bsd.prog.mk>
diff -r 58bde69dd58a -r 0afd6b249c90 sbin/ccdconfig/ccdconfig.8
--- a/sbin/ccdconfig/ccdconfig.8        Sat Apr 27 16:56:29 2013 +0000
+++ b/sbin/ccdconfig/ccdconfig.8        Sat Apr 27 17:12:36 2013 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: ccdconfig.8,v 1.25 2009/08/22 00:14:21 joerg Exp $
+.\"    $NetBSD: ccdconfig.8,v 1.26 2013/04/27 17:12:36 christos Exp $
 .\"
 .\" Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
 .\" All rights reserved.
@@ -56,8 +56,6 @@
 .Op Fl f Ar config_file
 .Nm
 .Fl g
-.Op Fl M Ar core
-.Op Fl N Ar system
 .Oo
 .Ar ccd Oo ...
 .Oc
@@ -87,16 +85,6 @@
 for use as the ccd configuration file.
 If no arguments are specified, every configured ccd is dumped.
 Otherwise, the configuration of each listed ccd is dumped.
-.It Fl M Ar core
-Extract values associated with the name list from
-.Pa core
-instead of the default
-.Pa /dev/mem .
-.It Fl N Ar system
-Extract the name list from
-.Pa system
-instead of the default
-.Pa /netbsd .
 .It Fl u
 Unconfigure a ccd.
 .It Fl U
diff -r 58bde69dd58a -r 0afd6b249c90 sbin/ccdconfig/ccdconfig.c
--- a/sbin/ccdconfig/ccdconfig.c        Sat Apr 27 16:56:29 2013 +0000
+++ b/sbin/ccdconfig/ccdconfig.c        Sat Apr 27 17:12:36 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ccdconfig.c,v 1.51 2011/08/27 16:29:51 joerg Exp $     */
+/*     $NetBSD: ccdconfig.c,v 1.52 2013/04/27 17:12:36 christos Exp $  */
 
 /*-
  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
@@ -33,7 +33,7 @@
 #ifndef lint
 __COPYRIGHT("@(#) Copyright (c) 1996, 1997\
  The NetBSD Foundation, Inc.  All rights reserved.");
-__RCSID("$NetBSD: ccdconfig.c,v 1.51 2011/08/27 16:29:51 joerg Exp $");
+__RCSID("$NetBSD: ccdconfig.c,v 1.52 2013/04/27 17:12:36 christos Exp $");
 #endif
 
 #include <sys/param.h>
@@ -46,7 +46,6 @@
 #include <err.h>
 #include <errno.h>
 #include <fcntl.h>
-#include <kvm.h>
 #include <limits.h>
 #include <nlist.h>
 #include <stdio.h>
@@ -65,9 +64,6 @@
 static int verbose;
 static const char *ccdconf = _PATH_CCDCONF;
 
-static char *core;
-static char *kernel;
-
 static struct  flagval {
        const char *fv_flag;
        int     fv_val;
@@ -77,16 +73,6 @@
        { NULL,                 0 },
 };
 
-static struct nlist nl[] = {
-       { .n_name = "_ccd_softc" },
-#define SYM_CCDSOFTC           0
-       { .n_name = "_numccd" },
-#define SYM_NUMCCD             1
-       { .n_name = "_ccd_softc_elemsize" },
-#define SYM_CCDSOFTCELEMSIZE   2
-       { .n_name = NULL },
-};
-
 #define CCD_CONFIG             0       /* configure a device */
 #define CCD_CONFIGALL          1       /* configure all devices */
 #define CCD_UNCONFIG           2       /* unconfigure a device */
@@ -100,7 +86,6 @@
 static int dump_ccd(int, char **, int);
 static int flags_to_val(char *);
 static int pathtounit(char *, int *);
-static void print_ccd_info(struct ccd_softc *, kvm_t *);
 static char *resolve_ccdname(char *);
 __dead static  void usage(void);
 
@@ -111,7 +96,7 @@
 
        egid = getegid();
        setegid(getgid());
-       while ((ch = getopt(argc, argv, "cCf:gM:N:uUv")) != -1) {
+       while ((ch = getopt(argc, argv, "cCf:guUv")) != -1) {
                switch (ch) {
                case 'c':
                        action = CCD_CONFIG;
@@ -131,14 +116,6 @@
                        action = CCD_DUMP;
                        break;
 
-               case 'M':
-                       core = optarg;
-                       break;
-
-               case 'N':
-                       kernel = optarg;
-                       break;
-
                case 'u':
                        action = CCD_UNCONFIG;
                        ++options;
@@ -163,17 +140,6 @@
        if (options > 1)
                usage();
 
-       /*
-        * Discard setgid privileges.  If not the running kernel, we toss
-        * them away totally so that bad guys can't print interesting stuff
-        * from kernel memory, otherwise switch back to kmem for the
-        * duration of the kvm_openfiles() call.
-        *
-        * We also do this if we aren't just looking...
-        */
-       if (core != NULL || kernel != NULL || action != CCD_DUMP)
-               setgid(getgid());
-
        switch (action) {
                case CCD_CONFIG:
                case CCD_UNCONFIG:
@@ -268,12 +234,7 @@
        }
 
        /* Next is the list of disks to make the ccd from. */
-       disks = malloc(argc * sizeof(char *));
-       if (disks == NULL) {
-               warnx("no memory to configure ccd");
-               free(ccd);
-               return (1);
-       }
+       disks = emalloc(argc * sizeof(char *));
        for (ui = 0; argc != 0; ) {
                cp = *argv++; --argc;
                if ((j = checkdev(cp)) == 0)
@@ -353,11 +314,7 @@
                        if (vp == NULL)
                                continue;
 
-                       if ((nargv = realloc(argv,
-                           sizeof(char *) * (argc + 1))) == NULL) {
-                               warnx("no memory to configure ccds");
-                               return (1);
-                       }
+                       nargv = erealloc(argv, sizeof(char *) * (argc + 1));
                        argv = nargv;
                        argc++;
                        argv[argc - 1] = vp;
@@ -428,7 +385,7 @@
 
        if (name[0] == '/' || name[0] == '.') {
                /* Assume they gave the correct pathname. */
-               return (strdup(name));
+               return estrdup(name);
        }
 
        len = strlen(name);
@@ -436,14 +393,12 @@
 
        if (isdigit((unsigned char)c)) {
                if ((rawpart = getrawpartition()) < 0)
-                       return (NULL);
-               if (asprintf(&path, "/dev/%s%c", name, 'a' + rawpart) < 0)
-                       return (NULL);
+                       return NULL;
+               easprintf(&path, "/dev/%s%c", name, 'a' + rawpart);
        } else
-               if (asprintf(&path, "/dev/%s", name) < 0)
-                       return (NULL);
+               easprintf(&path, "/dev/%s", name);
 
-       return (path);
+       return path;
 }
 
 static int
@@ -479,188 +434,125 @@
        return (0);
 }
 
-#define KVM_ABORT(kd, str) {                                           \
-       (void)kvm_close((kd));                                          \
-       warnx("%s", (str));                                             \
-       warnx("%s", kvm_geterr((kd)));                                  \
-       return (1);                                                     \
+static void
+print_ccd_info(int u, struct ccddiskinfo *ccd, char *str)
+{
+       static int header_printed = 0;
+
+       if (header_printed == 0 && verbose) {
+               printf("# ccd\t\tileave\tflags\t\tsize\tcomponent devices\n");
+               header_printed = 1;
+       }
+
+       /* Dump out softc information. */
+       printf("ccd%d\t\t%d\t0x%x\t%zu\t", u, ccd->ccd_ileave,
+           ccd->ccd_flags & CCDF_USERMASK, ccd->ccd_size * DEV_BSIZE);
+
+       /* Read component pathname and display component info. */
+       for (size_t i = 0; i < ccd->ccd_ndisks; ++i) {
+               fputs(str, stdout);
+               fputc((i + 1 < ccd->ccd_ndisks) ? ' ' : '\n', stdout);
+               str += strlen(str) + 1;
+       }
+       fflush(stdout);
+}
+
+static int
+printccdinfo(int u)
+{
+       struct ccddiskinfo ccd;
+       size_t s = sizeof(ccd);
+       size_t len;
+       const char *str;
+
+       if (sysctlbyname(str = "kern.ccd.info", &ccd, &s, &u, sizeof(u))
+           == -1) {
+               if (errno == ENOENT)
+                       warnx("ccd unit %d not configured", u);
+               else
+                       warn("error getting %s for ccd%d", str, u);
+               return 1;
+       }
+
+       if (sysctlbyname(str = "kern.ccd.components", NULL, &len, &u, sizeof(u))
+           == -1) {
+               warn("Error getting %s for ccd%d", str, u);
+               return 1;
+       }
+
+       char *names;
+       names = emalloc(len);
+       if (sysctlbyname(str = "kern.ccd.components", names, &len, &u,
+           sizeof(u)) == -1) {
+               warn("error getting %s for ccd%d", str, u);
+               free(names);
+               return 1;
+       }
+       print_ccd_info(u, &ccd, names);
+       free(names);
+       return 0;
 }
 
 static int
 dump_ccd(int argc, char **argv, int action)
 {
-       char errbuf[_POSIX2_LINE_MAX], *ccd, *cp;
-       struct ccd_softc *cs, *kcs;
-       void *vcs;
-       size_t readsize;
-       int i, error, numccd, ccd_softc_elemsize, numconfiged = 0;
-       kvm_t *kd;
-
-       memset(errbuf, 0, sizeof(errbuf));
-
-       vcs = NULL;
-       (void)setegid(egid);
-       if ((kd = kvm_openfiles(kernel, core, NULL, O_RDONLY,
-           errbuf)) == NULL) {
-               warnx("can't open kvm: %s", errbuf);
-               return (1);



Home | Main Index | Thread Index | Old Index