105c91076SPawel Jakub Dawidek /*- 287070efbSPawel Jakub Dawidek * Copyright (c) 2004-2009 Pawel Jakub Dawidek <pjd@FreeBSD.org> 305c91076SPawel Jakub Dawidek * All rights reserved. 405c91076SPawel Jakub Dawidek * 505c91076SPawel Jakub Dawidek * Redistribution and use in source and binary forms, with or without 605c91076SPawel Jakub Dawidek * modification, are permitted provided that the following conditions 705c91076SPawel Jakub Dawidek * are met: 805c91076SPawel Jakub Dawidek * 1. Redistributions of source code must retain the above copyright 905c91076SPawel Jakub Dawidek * notice, this list of conditions and the following disclaimer. 1005c91076SPawel Jakub Dawidek * 2. Redistributions in binary form must reproduce the above copyright 1105c91076SPawel Jakub Dawidek * notice, this list of conditions and the following disclaimer in the 1205c91076SPawel Jakub Dawidek * documentation and/or other materials provided with the distribution. 1305c91076SPawel Jakub Dawidek * 1405c91076SPawel Jakub Dawidek * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 1505c91076SPawel Jakub Dawidek * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1605c91076SPawel Jakub Dawidek * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1705c91076SPawel Jakub Dawidek * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 1805c91076SPawel Jakub Dawidek * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1905c91076SPawel Jakub Dawidek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2005c91076SPawel Jakub Dawidek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2105c91076SPawel Jakub Dawidek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2205c91076SPawel Jakub Dawidek * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2305c91076SPawel Jakub Dawidek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2405c91076SPawel Jakub Dawidek * SUCH DAMAGE. 2505c91076SPawel Jakub Dawidek */ 2605c91076SPawel Jakub Dawidek 2705c91076SPawel Jakub Dawidek #include <sys/cdefs.h> 2805c91076SPawel Jakub Dawidek __FBSDID("$FreeBSD$"); 2905c91076SPawel Jakub Dawidek 3005c91076SPawel Jakub Dawidek #include <sys/param.h> 3105c91076SPawel Jakub Dawidek #include <sys/linker.h> 3205c91076SPawel Jakub Dawidek #include <sys/module.h> 3305c91076SPawel Jakub Dawidek #include <sys/stat.h> 3405c91076SPawel Jakub Dawidek #include <sys/sysctl.h> 3505c91076SPawel Jakub Dawidek #include <ctype.h> 3605c91076SPawel Jakub Dawidek #include <err.h> 3705c91076SPawel Jakub Dawidek #include <errno.h> 3805c91076SPawel Jakub Dawidek #include <stdio.h> 3905c91076SPawel Jakub Dawidek #include <stdlib.h> 4005c91076SPawel Jakub Dawidek #include <stdarg.h> 4105c91076SPawel Jakub Dawidek #include <stdint.h> 4205c91076SPawel Jakub Dawidek #include <string.h> 4305c91076SPawel Jakub Dawidek #include <unistd.h> 4405c91076SPawel Jakub Dawidek #include <libgen.h> 45af565b58SPawel Jakub Dawidek #include <libutil.h> 4605c91076SPawel Jakub Dawidek #include <inttypes.h> 4705c91076SPawel Jakub Dawidek #include <dlfcn.h> 4805c91076SPawel Jakub Dawidek #include <assert.h> 4905c91076SPawel Jakub Dawidek #include <libgeom.h> 5005c91076SPawel Jakub Dawidek #include <geom.h> 5105c91076SPawel Jakub Dawidek 5205c91076SPawel Jakub Dawidek #include "misc/subr.h" 5305c91076SPawel Jakub Dawidek 5414bf405bSMarcel Moolenaar #ifdef STATIC_GEOM_CLASSES 55a16f9b36SMarcel Moolenaar extern uint32_t gpart_version; 56a16f9b36SMarcel Moolenaar extern struct g_command gpart_class_commands[]; 57bc69d66fSXin LI extern uint32_t glabel_version; 58bc69d66fSXin LI extern struct g_command glabel_class_commands[]; 59a16f9b36SMarcel Moolenaar #endif 6005c91076SPawel Jakub Dawidek 6105c91076SPawel Jakub Dawidek static char comm[MAXPATHLEN], *class_name = NULL, *gclass_name = NULL; 6205c91076SPawel Jakub Dawidek static uint32_t *version = NULL; 6305c91076SPawel Jakub Dawidek static int verbose = 0; 6405c91076SPawel Jakub Dawidek static struct g_command *class_commands = NULL; 6505c91076SPawel Jakub Dawidek 66138cedfdSPawel Jakub Dawidek #define GEOM_CLASS_CMDS 0x01 67138cedfdSPawel Jakub Dawidek #define GEOM_STD_CMDS 0x02 68138cedfdSPawel Jakub Dawidek static struct g_command *find_command(const char *cmdstr, int flags); 6905c91076SPawel Jakub Dawidek static int std_available(const char *name); 7005c91076SPawel Jakub Dawidek 71b70eccf3SPawel Jakub Dawidek static void std_help(struct gctl_req *req, unsigned flags); 7205c91076SPawel Jakub Dawidek static void std_list(struct gctl_req *req, unsigned flags); 7318ee8840SPawel Jakub Dawidek static void std_status(struct gctl_req *req, unsigned flags); 7405c91076SPawel Jakub Dawidek static void std_load(struct gctl_req *req, unsigned flags); 7505c91076SPawel Jakub Dawidek static void std_unload(struct gctl_req *req, unsigned flags); 7605c91076SPawel Jakub Dawidek 7705c91076SPawel Jakub Dawidek struct g_command std_commands[] = { 78946e2f35SPawel Jakub Dawidek { "help", 0, std_help, G_NULL_OPTS, NULL }, 79*83d165c1SAlexander Motin { "list", 0, std_list, 80*83d165c1SAlexander Motin { 81*83d165c1SAlexander Motin { 'a', "all", NULL, G_TYPE_BOOL }, 82*83d165c1SAlexander Motin G_OPT_SENTINEL 83*83d165c1SAlexander Motin }, 84*83d165c1SAlexander Motin "[-a] [name ...]" 85c979e206SPawel Jakub Dawidek }, 86ba6821f0SPawel Jakub Dawidek { "status", 0, std_status, 87ba6821f0SPawel Jakub Dawidek { 88*83d165c1SAlexander Motin { 'a', "all", NULL, G_TYPE_BOOL }, 89*83d165c1SAlexander Motin { 'g', "geoms", NULL, G_TYPE_BOOL }, 906fc60008SPawel Jakub Dawidek { 's', "script", NULL, G_TYPE_BOOL }, 91ba6821f0SPawel Jakub Dawidek G_OPT_SENTINEL 92ba6821f0SPawel Jakub Dawidek }, 93*83d165c1SAlexander Motin "[-ags] [name ...]" 94c979e206SPawel Jakub Dawidek }, 953cf55d3aSMarcel Moolenaar { "load", G_FLAG_VERBOSE | G_FLAG_LOADKLD, std_load, G_NULL_OPTS, 96946e2f35SPawel Jakub Dawidek NULL }, 97946e2f35SPawel Jakub Dawidek { "unload", G_FLAG_VERBOSE, std_unload, G_NULL_OPTS, NULL }, 9805c91076SPawel Jakub Dawidek G_CMD_SENTINEL 9905c91076SPawel Jakub Dawidek }; 10005c91076SPawel Jakub Dawidek 10105c91076SPawel Jakub Dawidek static void 102c979e206SPawel Jakub Dawidek usage_command(struct g_command *cmd, const char *prefix) 10305c91076SPawel Jakub Dawidek { 10405c91076SPawel Jakub Dawidek struct g_option *opt; 105c979e206SPawel Jakub Dawidek unsigned i; 10605c91076SPawel Jakub Dawidek 107c979e206SPawel Jakub Dawidek if (cmd->gc_usage != NULL) { 108963feac4SPawel Jakub Dawidek char *pos, *ptr, *sptr; 109963feac4SPawel Jakub Dawidek 110963feac4SPawel Jakub Dawidek sptr = ptr = strdup(cmd->gc_usage); 111963feac4SPawel Jakub Dawidek while ((pos = strsep(&ptr, "\n")) != NULL) { 112963feac4SPawel Jakub Dawidek if (*pos == '\0') 113963feac4SPawel Jakub Dawidek continue; 114963feac4SPawel Jakub Dawidek fprintf(stderr, "%s %s %s %s\n", prefix, comm, 115963feac4SPawel Jakub Dawidek cmd->gc_name, pos); 116963feac4SPawel Jakub Dawidek } 117963feac4SPawel Jakub Dawidek free(sptr); 118c979e206SPawel Jakub Dawidek return; 119c979e206SPawel Jakub Dawidek } 120963feac4SPawel Jakub Dawidek 121963feac4SPawel Jakub Dawidek fprintf(stderr, "%s %s %s", prefix, comm, cmd->gc_name); 12205c91076SPawel Jakub Dawidek if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0) 12305c91076SPawel Jakub Dawidek fprintf(stderr, " [-v]"); 124c979e206SPawel Jakub Dawidek for (i = 0; ; i++) { 125c979e206SPawel Jakub Dawidek opt = &cmd->gc_options[i]; 12605c91076SPawel Jakub Dawidek if (opt->go_name == NULL) 12705c91076SPawel Jakub Dawidek break; 1286fc60008SPawel Jakub Dawidek if (opt->go_val != NULL || G_OPT_TYPE(opt) == G_TYPE_BOOL) 12905c91076SPawel Jakub Dawidek fprintf(stderr, " ["); 13005c91076SPawel Jakub Dawidek else 13105c91076SPawel Jakub Dawidek fprintf(stderr, " "); 13205c91076SPawel Jakub Dawidek fprintf(stderr, "-%c", opt->go_char); 1336fc60008SPawel Jakub Dawidek if (G_OPT_TYPE(opt) != G_TYPE_BOOL) 13405c91076SPawel Jakub Dawidek fprintf(stderr, " %s", opt->go_name); 1356fc60008SPawel Jakub Dawidek if (opt->go_val != NULL || G_OPT_TYPE(opt) == G_TYPE_BOOL) 13605c91076SPawel Jakub Dawidek fprintf(stderr, "]"); 13705c91076SPawel Jakub Dawidek } 138c979e206SPawel Jakub Dawidek fprintf(stderr, "\n"); 13905c91076SPawel Jakub Dawidek } 14005c91076SPawel Jakub Dawidek 14105c91076SPawel Jakub Dawidek static void 142c979e206SPawel Jakub Dawidek usage(void) 14305c91076SPawel Jakub Dawidek { 14405c91076SPawel Jakub Dawidek 14505c91076SPawel Jakub Dawidek if (class_name == NULL) { 146b70eccf3SPawel Jakub Dawidek errx(EXIT_FAILURE, "usage: %s <class> <command> [options]", 147b70eccf3SPawel Jakub Dawidek "geom"); 14805c91076SPawel Jakub Dawidek } else { 149c979e206SPawel Jakub Dawidek struct g_command *cmd; 15005c91076SPawel Jakub Dawidek const char *prefix; 15105c91076SPawel Jakub Dawidek unsigned i; 15205c91076SPawel Jakub Dawidek 15305c91076SPawel Jakub Dawidek prefix = "usage:"; 154c979e206SPawel Jakub Dawidek if (class_commands != NULL) { 155c979e206SPawel Jakub Dawidek for (i = 0; ; i++) { 156c979e206SPawel Jakub Dawidek cmd = &class_commands[i]; 157c979e206SPawel Jakub Dawidek if (cmd->gc_name == NULL) 158c979e206SPawel Jakub Dawidek break; 159c979e206SPawel Jakub Dawidek usage_command(cmd, prefix); 16005c91076SPawel Jakub Dawidek prefix = " "; 16105c91076SPawel Jakub Dawidek } 162c979e206SPawel Jakub Dawidek } 16305c91076SPawel Jakub Dawidek for (i = 0; ; i++) { 16405c91076SPawel Jakub Dawidek cmd = &std_commands[i]; 16505c91076SPawel Jakub Dawidek if (cmd->gc_name == NULL) 16605c91076SPawel Jakub Dawidek break; 167138cedfdSPawel Jakub Dawidek /* 168138cedfdSPawel Jakub Dawidek * If class defines command, which has the same name as 169138cedfdSPawel Jakub Dawidek * standard command, skip it, because it was already 170138cedfdSPawel Jakub Dawidek * shown on usage(). 171138cedfdSPawel Jakub Dawidek */ 172138cedfdSPawel Jakub Dawidek if (find_command(cmd->gc_name, GEOM_CLASS_CMDS) != NULL) 17305c91076SPawel Jakub Dawidek continue; 174c979e206SPawel Jakub Dawidek usage_command(cmd, prefix); 17505c91076SPawel Jakub Dawidek prefix = " "; 17605c91076SPawel Jakub Dawidek } 17705c91076SPawel Jakub Dawidek exit(EXIT_FAILURE); 17805c91076SPawel Jakub Dawidek } 17905c91076SPawel Jakub Dawidek } 18005c91076SPawel Jakub Dawidek 18105c91076SPawel Jakub Dawidek static void 18205c91076SPawel Jakub Dawidek load_module(void) 18305c91076SPawel Jakub Dawidek { 18405c91076SPawel Jakub Dawidek char name1[64], name2[64]; 18505c91076SPawel Jakub Dawidek 18605c91076SPawel Jakub Dawidek snprintf(name1, sizeof(name1), "g_%s", class_name); 18705c91076SPawel Jakub Dawidek snprintf(name2, sizeof(name2), "geom_%s", class_name); 18805c91076SPawel Jakub Dawidek if (modfind(name1) < 0) { 18905c91076SPawel Jakub Dawidek /* Not present in kernel, try loading it. */ 19005c91076SPawel Jakub Dawidek if (kldload(name2) < 0 || modfind(name1) < 0) { 19105c91076SPawel Jakub Dawidek if (errno != EEXIST) { 19205c91076SPawel Jakub Dawidek errx(EXIT_FAILURE, 19305c91076SPawel Jakub Dawidek "%s module not available!", name2); 19405c91076SPawel Jakub Dawidek } 19505c91076SPawel Jakub Dawidek } 19605c91076SPawel Jakub Dawidek } 19705c91076SPawel Jakub Dawidek } 19805c91076SPawel Jakub Dawidek 19905c91076SPawel Jakub Dawidek static int 20005c91076SPawel Jakub Dawidek strlcatf(char *str, size_t size, const char *format, ...) 20105c91076SPawel Jakub Dawidek { 20205c91076SPawel Jakub Dawidek size_t len; 20305c91076SPawel Jakub Dawidek va_list ap; 20405c91076SPawel Jakub Dawidek int ret; 20505c91076SPawel Jakub Dawidek 20605c91076SPawel Jakub Dawidek len = strlen(str); 20705c91076SPawel Jakub Dawidek str += len; 20805c91076SPawel Jakub Dawidek size -= len; 20905c91076SPawel Jakub Dawidek 21005c91076SPawel Jakub Dawidek va_start(ap, format); 21105c91076SPawel Jakub Dawidek ret = vsnprintf(str, size, format, ap); 21205c91076SPawel Jakub Dawidek va_end(ap); 21305c91076SPawel Jakub Dawidek 21405c91076SPawel Jakub Dawidek return (ret); 21505c91076SPawel Jakub Dawidek } 21605c91076SPawel Jakub Dawidek 21705c91076SPawel Jakub Dawidek /* 21805c91076SPawel Jakub Dawidek * Find given option in options available for given command. 21905c91076SPawel Jakub Dawidek */ 22005c91076SPawel Jakub Dawidek static struct g_option * 22105c91076SPawel Jakub Dawidek find_option(struct g_command *cmd, char ch) 22205c91076SPawel Jakub Dawidek { 22305c91076SPawel Jakub Dawidek struct g_option *opt; 22405c91076SPawel Jakub Dawidek unsigned i; 22505c91076SPawel Jakub Dawidek 22605c91076SPawel Jakub Dawidek for (i = 0; ; i++) { 22705c91076SPawel Jakub Dawidek opt = &cmd->gc_options[i]; 22805c91076SPawel Jakub Dawidek if (opt->go_name == NULL) 22905c91076SPawel Jakub Dawidek return (NULL); 23005c91076SPawel Jakub Dawidek if (opt->go_char == ch) 23105c91076SPawel Jakub Dawidek return (opt); 23205c91076SPawel Jakub Dawidek } 23305c91076SPawel Jakub Dawidek /* NOTREACHED */ 23405c91076SPawel Jakub Dawidek return (NULL); 23505c91076SPawel Jakub Dawidek } 23605c91076SPawel Jakub Dawidek 23705c91076SPawel Jakub Dawidek /* 23805c91076SPawel Jakub Dawidek * Add given option to gctl_req. 23905c91076SPawel Jakub Dawidek */ 24005c91076SPawel Jakub Dawidek static void 24105c91076SPawel Jakub Dawidek set_option(struct gctl_req *req, struct g_option *opt, const char *val) 24205c91076SPawel Jakub Dawidek { 243315fcbf7SPawel Jakub Dawidek const char *optname; 24435efcc8bSDag-Erling Smørgrav uint64_t number; 245a478ea74SPawel Jakub Dawidek void *ptr; 24605c91076SPawel Jakub Dawidek 247315fcbf7SPawel Jakub Dawidek if (G_OPT_ISMULTI(opt)) { 248315fcbf7SPawel Jakub Dawidek size_t optnamesize; 249315fcbf7SPawel Jakub Dawidek 250315fcbf7SPawel Jakub Dawidek if (G_OPT_NUM(opt) == UCHAR_MAX) 251315fcbf7SPawel Jakub Dawidek errx(EXIT_FAILURE, "Too many -%c options.", opt->go_char); 252315fcbf7SPawel Jakub Dawidek 253315fcbf7SPawel Jakub Dawidek /* 254315fcbf7SPawel Jakub Dawidek * Base option name length plus 3 bytes for option number 255315fcbf7SPawel Jakub Dawidek * (max. 255 options) plus 1 byte for terminating '\0'. 256315fcbf7SPawel Jakub Dawidek */ 257315fcbf7SPawel Jakub Dawidek optnamesize = strlen(opt->go_name) + 3 + 1; 258315fcbf7SPawel Jakub Dawidek ptr = malloc(optnamesize); 259315fcbf7SPawel Jakub Dawidek if (ptr == NULL) 260315fcbf7SPawel Jakub Dawidek errx(EXIT_FAILURE, "No memory."); 261315fcbf7SPawel Jakub Dawidek snprintf(ptr, optnamesize, "%s%u", opt->go_name, G_OPT_NUM(opt)); 262315fcbf7SPawel Jakub Dawidek G_OPT_NUMINC(opt); 263315fcbf7SPawel Jakub Dawidek optname = ptr; 264315fcbf7SPawel Jakub Dawidek } else { 265315fcbf7SPawel Jakub Dawidek optname = opt->go_name; 266315fcbf7SPawel Jakub Dawidek } 267315fcbf7SPawel Jakub Dawidek 268fa5383a2SPawel Jakub Dawidek if (G_OPT_TYPE(opt) == G_TYPE_NUMBER) { 269e84091cbSPawel Jakub Dawidek if (expand_number(val, &number) == -1) { 270f104beb7SPawel Jakub Dawidek err(EXIT_FAILURE, "Invalid value for '%c' argument", 27105c91076SPawel Jakub Dawidek opt->go_char); 27205c91076SPawel Jakub Dawidek } 273a478ea74SPawel Jakub Dawidek ptr = malloc(sizeof(intmax_t)); 274a478ea74SPawel Jakub Dawidek if (ptr == NULL) 275a478ea74SPawel Jakub Dawidek errx(EXIT_FAILURE, "No memory."); 276a478ea74SPawel Jakub Dawidek *(intmax_t *)ptr = number; 277a478ea74SPawel Jakub Dawidek opt->go_val = ptr; 278628ec6d3SPawel Jakub Dawidek gctl_ro_param(req, optname, sizeof(intmax_t), opt->go_val); 2796fc60008SPawel Jakub Dawidek } else if (G_OPT_TYPE(opt) == G_TYPE_STRING) { 280315fcbf7SPawel Jakub Dawidek gctl_ro_param(req, optname, -1, val); 2816fc60008SPawel Jakub Dawidek } else if (G_OPT_TYPE(opt) == G_TYPE_BOOL) { 282a478ea74SPawel Jakub Dawidek ptr = malloc(sizeof(int)); 283a478ea74SPawel Jakub Dawidek if (ptr == NULL) 28405c91076SPawel Jakub Dawidek errx(EXIT_FAILURE, "No memory."); 285a478ea74SPawel Jakub Dawidek *(int *)ptr = *val - '0'; 286a478ea74SPawel Jakub Dawidek opt->go_val = ptr; 287315fcbf7SPawel Jakub Dawidek gctl_ro_param(req, optname, sizeof(int), opt->go_val); 2886fc60008SPawel Jakub Dawidek } else { 2896fc60008SPawel Jakub Dawidek assert(!"Invalid type"); 29005c91076SPawel Jakub Dawidek } 291315fcbf7SPawel Jakub Dawidek 292315fcbf7SPawel Jakub Dawidek if (G_OPT_ISMULTI(opt)) 293315fcbf7SPawel Jakub Dawidek free(__DECONST(char *, optname)); 29405c91076SPawel Jakub Dawidek } 29505c91076SPawel Jakub Dawidek 29605c91076SPawel Jakub Dawidek /* 29705c91076SPawel Jakub Dawidek * 1. Add given argument by caller. 29805c91076SPawel Jakub Dawidek * 2. Add default values of not given arguments. 29905c91076SPawel Jakub Dawidek * 3. Add the rest of arguments. 30005c91076SPawel Jakub Dawidek */ 30105c91076SPawel Jakub Dawidek static void 30205c91076SPawel Jakub Dawidek parse_arguments(struct g_command *cmd, struct gctl_req *req, int *argc, 30305c91076SPawel Jakub Dawidek char ***argv) 30405c91076SPawel Jakub Dawidek { 30505c91076SPawel Jakub Dawidek struct g_option *opt; 30605c91076SPawel Jakub Dawidek char opts[64]; 30705c91076SPawel Jakub Dawidek unsigned i; 30805c91076SPawel Jakub Dawidek int ch; 30905c91076SPawel Jakub Dawidek 31005c91076SPawel Jakub Dawidek *opts = '\0'; 31105c91076SPawel Jakub Dawidek if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0) 31205c91076SPawel Jakub Dawidek strlcat(opts, "v", sizeof(opts)); 31305c91076SPawel Jakub Dawidek for (i = 0; ; i++) { 31405c91076SPawel Jakub Dawidek opt = &cmd->gc_options[i]; 31505c91076SPawel Jakub Dawidek if (opt->go_name == NULL) 31605c91076SPawel Jakub Dawidek break; 3176fc60008SPawel Jakub Dawidek assert(G_OPT_TYPE(opt) != 0); 318315fcbf7SPawel Jakub Dawidek assert((opt->go_type & ~(G_TYPE_MASK | G_TYPE_MULTI)) == 0); 319315fcbf7SPawel Jakub Dawidek /* Multiple bool arguments makes no sense. */ 320315fcbf7SPawel Jakub Dawidek assert(G_OPT_TYPE(opt) != G_TYPE_BOOL || 321315fcbf7SPawel Jakub Dawidek (opt->go_type & G_TYPE_MULTI) == 0); 32205c91076SPawel Jakub Dawidek strlcatf(opts, sizeof(opts), "%c", opt->go_char); 3236fc60008SPawel Jakub Dawidek if (G_OPT_TYPE(opt) != G_TYPE_BOOL) 32405c91076SPawel Jakub Dawidek strlcat(opts, ":", sizeof(opts)); 32505c91076SPawel Jakub Dawidek } 32605c91076SPawel Jakub Dawidek 32705c91076SPawel Jakub Dawidek /* 32805c91076SPawel Jakub Dawidek * Add specified arguments. 32905c91076SPawel Jakub Dawidek */ 33005c91076SPawel Jakub Dawidek while ((ch = getopt(*argc, *argv, opts)) != -1) { 33105c91076SPawel Jakub Dawidek /* Standard (not passed to kernel) options. */ 33205c91076SPawel Jakub Dawidek switch (ch) { 33305c91076SPawel Jakub Dawidek case 'v': 33405c91076SPawel Jakub Dawidek verbose = 1; 33505c91076SPawel Jakub Dawidek continue; 33605c91076SPawel Jakub Dawidek } 33705c91076SPawel Jakub Dawidek /* Options passed to kernel. */ 33805c91076SPawel Jakub Dawidek opt = find_option(cmd, ch); 33905c91076SPawel Jakub Dawidek if (opt == NULL) 340c979e206SPawel Jakub Dawidek usage(); 341315fcbf7SPawel Jakub Dawidek if (!G_OPT_ISMULTI(opt) && G_OPT_ISDONE(opt)) { 3426fc60008SPawel Jakub Dawidek warnx("Option '%c' specified twice.", opt->go_char); 343c979e206SPawel Jakub Dawidek usage(); 34405c91076SPawel Jakub Dawidek } 34505c91076SPawel Jakub Dawidek G_OPT_DONE(opt); 34605c91076SPawel Jakub Dawidek 3476fc60008SPawel Jakub Dawidek if (G_OPT_TYPE(opt) == G_TYPE_BOOL) 34805c91076SPawel Jakub Dawidek set_option(req, opt, "1"); 34905c91076SPawel Jakub Dawidek else 35005c91076SPawel Jakub Dawidek set_option(req, opt, optarg); 35105c91076SPawel Jakub Dawidek } 35205c91076SPawel Jakub Dawidek *argc -= optind; 35305c91076SPawel Jakub Dawidek *argv += optind; 35405c91076SPawel Jakub Dawidek 35505c91076SPawel Jakub Dawidek /* 35605c91076SPawel Jakub Dawidek * Add not specified arguments, but with default values. 35705c91076SPawel Jakub Dawidek */ 35805c91076SPawel Jakub Dawidek for (i = 0; ; i++) { 35905c91076SPawel Jakub Dawidek opt = &cmd->gc_options[i]; 36005c91076SPawel Jakub Dawidek if (opt->go_name == NULL) 36105c91076SPawel Jakub Dawidek break; 36205c91076SPawel Jakub Dawidek if (G_OPT_ISDONE(opt)) 36305c91076SPawel Jakub Dawidek continue; 36405c91076SPawel Jakub Dawidek 3656fc60008SPawel Jakub Dawidek if (G_OPT_TYPE(opt) == G_TYPE_BOOL) { 36605c91076SPawel Jakub Dawidek assert(opt->go_val == NULL); 36705c91076SPawel Jakub Dawidek set_option(req, opt, "0"); 36805c91076SPawel Jakub Dawidek } else { 36905c91076SPawel Jakub Dawidek if (opt->go_val == NULL) { 3706fc60008SPawel Jakub Dawidek warnx("Option '%c' not specified.", 37105c91076SPawel Jakub Dawidek opt->go_char); 372c979e206SPawel Jakub Dawidek usage(); 3737648b1e9SPawel Jakub Dawidek } else if (opt->go_val == G_VAL_OPTIONAL) { 3747648b1e9SPawel Jakub Dawidek /* add nothing. */ 37505c91076SPawel Jakub Dawidek } else { 376946e2f35SPawel Jakub Dawidek set_option(req, opt, opt->go_val); 37705c91076SPawel Jakub Dawidek } 37805c91076SPawel Jakub Dawidek } 37905c91076SPawel Jakub Dawidek } 3803cf55d3aSMarcel Moolenaar 38105c91076SPawel Jakub Dawidek /* 38205c91076SPawel Jakub Dawidek * Add rest of given arguments. 38305c91076SPawel Jakub Dawidek */ 38405c91076SPawel Jakub Dawidek gctl_ro_param(req, "nargs", sizeof(int), argc); 38505c91076SPawel Jakub Dawidek for (i = 0; i < (unsigned)*argc; i++) { 38605c91076SPawel Jakub Dawidek char argname[16]; 38705c91076SPawel Jakub Dawidek 38805c91076SPawel Jakub Dawidek snprintf(argname, sizeof(argname), "arg%u", i); 38905c91076SPawel Jakub Dawidek gctl_ro_param(req, argname, -1, (*argv)[i]); 39005c91076SPawel Jakub Dawidek } 39105c91076SPawel Jakub Dawidek } 39205c91076SPawel Jakub Dawidek 39305c91076SPawel Jakub Dawidek /* 39405c91076SPawel Jakub Dawidek * Find given command in commands available for given class. 39505c91076SPawel Jakub Dawidek */ 39605c91076SPawel Jakub Dawidek static struct g_command * 397138cedfdSPawel Jakub Dawidek find_command(const char *cmdstr, int flags) 39805c91076SPawel Jakub Dawidek { 39905c91076SPawel Jakub Dawidek struct g_command *cmd; 40005c91076SPawel Jakub Dawidek unsigned i; 40105c91076SPawel Jakub Dawidek 40205c91076SPawel Jakub Dawidek /* 40305c91076SPawel Jakub Dawidek * First try to find command defined by loaded library. 40405c91076SPawel Jakub Dawidek */ 405138cedfdSPawel Jakub Dawidek if ((flags & GEOM_CLASS_CMDS) != 0 && class_commands != NULL) { 40605c91076SPawel Jakub Dawidek for (i = 0; ; i++) { 40705c91076SPawel Jakub Dawidek cmd = &class_commands[i]; 40805c91076SPawel Jakub Dawidek if (cmd->gc_name == NULL) 40905c91076SPawel Jakub Dawidek break; 41005c91076SPawel Jakub Dawidek if (strcmp(cmd->gc_name, cmdstr) == 0) 41105c91076SPawel Jakub Dawidek return (cmd); 41205c91076SPawel Jakub Dawidek } 41305c91076SPawel Jakub Dawidek } 41405c91076SPawel Jakub Dawidek /* 41505c91076SPawel Jakub Dawidek * Now try to find in standard commands. 41605c91076SPawel Jakub Dawidek */ 417138cedfdSPawel Jakub Dawidek if ((flags & GEOM_STD_CMDS) != 0) { 41805c91076SPawel Jakub Dawidek for (i = 0; ; i++) { 41905c91076SPawel Jakub Dawidek cmd = &std_commands[i]; 42005c91076SPawel Jakub Dawidek if (cmd->gc_name == NULL) 42105c91076SPawel Jakub Dawidek break; 42205c91076SPawel Jakub Dawidek if (strcmp(cmd->gc_name, cmdstr) == 0) 42305c91076SPawel Jakub Dawidek return (cmd); 42405c91076SPawel Jakub Dawidek } 425138cedfdSPawel Jakub Dawidek } 42605c91076SPawel Jakub Dawidek return (NULL); 42705c91076SPawel Jakub Dawidek } 42805c91076SPawel Jakub Dawidek 42905c91076SPawel Jakub Dawidek static unsigned 43005c91076SPawel Jakub Dawidek set_flags(struct g_command *cmd) 43105c91076SPawel Jakub Dawidek { 43205c91076SPawel Jakub Dawidek unsigned flags = 0; 43305c91076SPawel Jakub Dawidek 43405c91076SPawel Jakub Dawidek if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0 && verbose) 43505c91076SPawel Jakub Dawidek flags |= G_FLAG_VERBOSE; 43605c91076SPawel Jakub Dawidek 43705c91076SPawel Jakub Dawidek return (flags); 43805c91076SPawel Jakub Dawidek } 43905c91076SPawel Jakub Dawidek 44005c91076SPawel Jakub Dawidek /* 44105c91076SPawel Jakub Dawidek * Run command. 44205c91076SPawel Jakub Dawidek */ 44305c91076SPawel Jakub Dawidek static void 44405c91076SPawel Jakub Dawidek run_command(int argc, char *argv[]) 44505c91076SPawel Jakub Dawidek { 44605c91076SPawel Jakub Dawidek struct g_command *cmd; 44705c91076SPawel Jakub Dawidek struct gctl_req *req; 44805c91076SPawel Jakub Dawidek const char *errstr; 44905c91076SPawel Jakub Dawidek char buf[4096]; 45005c91076SPawel Jakub Dawidek 451138cedfdSPawel Jakub Dawidek /* First try to find a command defined by a class. */ 452138cedfdSPawel Jakub Dawidek cmd = find_command(argv[0], GEOM_CLASS_CMDS); 453138cedfdSPawel Jakub Dawidek if (cmd == NULL) { 454138cedfdSPawel Jakub Dawidek /* Now, try to find a standard command. */ 455138cedfdSPawel Jakub Dawidek cmd = find_command(argv[0], GEOM_STD_CMDS); 45605c91076SPawel Jakub Dawidek if (cmd == NULL) { 4576fc60008SPawel Jakub Dawidek warnx("Unknown command: %s.", argv[0]); 458c979e206SPawel Jakub Dawidek usage(); 45905c91076SPawel Jakub Dawidek } 460138cedfdSPawel Jakub Dawidek if (!std_available(cmd->gc_name)) { 4616fc60008SPawel Jakub Dawidek warnx("Command '%s' not available.", argv[0]); 462138cedfdSPawel Jakub Dawidek exit(EXIT_FAILURE); 463138cedfdSPawel Jakub Dawidek } 464138cedfdSPawel Jakub Dawidek } 46505c91076SPawel Jakub Dawidek if ((cmd->gc_flags & G_FLAG_LOADKLD) != 0) 46605c91076SPawel Jakub Dawidek load_module(); 46705c91076SPawel Jakub Dawidek 46805c91076SPawel Jakub Dawidek req = gctl_get_handle(); 46905c91076SPawel Jakub Dawidek gctl_ro_param(req, "class", -1, gclass_name); 47005c91076SPawel Jakub Dawidek gctl_ro_param(req, "verb", -1, argv[0]); 47105c91076SPawel Jakub Dawidek if (version != NULL) 47205c91076SPawel Jakub Dawidek gctl_ro_param(req, "version", sizeof(*version), version); 47305c91076SPawel Jakub Dawidek parse_arguments(cmd, req, &argc, &argv); 47405c91076SPawel Jakub Dawidek 4751bcfab7fSPawel Jakub Dawidek bzero(buf, sizeof(buf)); 47605c91076SPawel Jakub Dawidek if (cmd->gc_func != NULL) { 47705c91076SPawel Jakub Dawidek unsigned flags; 47805c91076SPawel Jakub Dawidek 47905c91076SPawel Jakub Dawidek flags = set_flags(cmd); 48005c91076SPawel Jakub Dawidek cmd->gc_func(req, flags); 48105c91076SPawel Jakub Dawidek errstr = req->error; 48205c91076SPawel Jakub Dawidek } else { 48305c91076SPawel Jakub Dawidek gctl_rw_param(req, "output", sizeof(buf), buf); 48405c91076SPawel Jakub Dawidek errstr = gctl_issue(req); 48505c91076SPawel Jakub Dawidek } 48653767efdSPawel Jakub Dawidek if (errstr != NULL && errstr[0] != '\0') { 4876fc60008SPawel Jakub Dawidek warnx("%s", errstr); 488f792f1d8SPawel Jakub Dawidek if (strncmp(errstr, "warning: ", strlen("warning: ")) != 0) { 48905c91076SPawel Jakub Dawidek gctl_free(req); 49005c91076SPawel Jakub Dawidek exit(EXIT_FAILURE); 49105c91076SPawel Jakub Dawidek } 492f792f1d8SPawel Jakub Dawidek } 4931bcfab7fSPawel Jakub Dawidek if (buf[0] != '\0') 49405c91076SPawel Jakub Dawidek printf("%s", buf); 49505c91076SPawel Jakub Dawidek gctl_free(req); 49605c91076SPawel Jakub Dawidek if (verbose) 49705c91076SPawel Jakub Dawidek printf("Done.\n"); 49805c91076SPawel Jakub Dawidek exit(EXIT_SUCCESS); 49905c91076SPawel Jakub Dawidek } 50005c91076SPawel Jakub Dawidek 50114bf405bSMarcel Moolenaar #ifndef STATIC_GEOM_CLASSES 5023bad06e9SPawel Jakub Dawidek static const char * 5033bad06e9SPawel Jakub Dawidek library_path(void) 5043bad06e9SPawel Jakub Dawidek { 5053bad06e9SPawel Jakub Dawidek const char *path; 5063bad06e9SPawel Jakub Dawidek 5073bad06e9SPawel Jakub Dawidek path = getenv("GEOM_LIBRARY_PATH"); 5083bad06e9SPawel Jakub Dawidek if (path == NULL) 5098494b738SDavid E. O'Brien path = GEOM_CLASS_DIR; 5103bad06e9SPawel Jakub Dawidek return (path); 5113bad06e9SPawel Jakub Dawidek } 5123bad06e9SPawel Jakub Dawidek 51305c91076SPawel Jakub Dawidek static void 51405c91076SPawel Jakub Dawidek load_library(void) 51505c91076SPawel Jakub Dawidek { 5165d824386SUlf Lilleengen char *curpath, path[MAXPATHLEN], *tofree, *totalpath; 51705c91076SPawel Jakub Dawidek uint32_t *lib_version; 51805c91076SPawel Jakub Dawidek void *dlh; 5192591e96cSUlf Lilleengen int ret; 52005c91076SPawel Jakub Dawidek 5212591e96cSUlf Lilleengen ret = 0; 5225d824386SUlf Lilleengen tofree = totalpath = strdup(library_path()); 5232591e96cSUlf Lilleengen if (totalpath == NULL) 5242591e96cSUlf Lilleengen err(EXIT_FAILURE, "Not enough memory for library path"); 5252591e96cSUlf Lilleengen 5262591e96cSUlf Lilleengen if (strchr(totalpath, ':') != NULL) 5272591e96cSUlf Lilleengen curpath = strsep(&totalpath, ":"); 5282591e96cSUlf Lilleengen else 5292591e96cSUlf Lilleengen curpath = totalpath; 5302591e96cSUlf Lilleengen /* Traverse the paths to find one that contains the library we want. */ 5312591e96cSUlf Lilleengen while (curpath != NULL) { 5322591e96cSUlf Lilleengen snprintf(path, sizeof(path), "%s/geom_%s.so", curpath, 5333bad06e9SPawel Jakub Dawidek class_name); 5342591e96cSUlf Lilleengen ret = access(path, F_OK); 5352591e96cSUlf Lilleengen if (ret == -1) { 536a73148d2SPawel Jakub Dawidek if (errno == ENOENT) { 53705c91076SPawel Jakub Dawidek /* 5382591e96cSUlf Lilleengen * If we cannot find library, try the next 5392591e96cSUlf Lilleengen * path. 54005c91076SPawel Jakub Dawidek */ 5412591e96cSUlf Lilleengen curpath = strsep(&totalpath, ":"); 5422591e96cSUlf Lilleengen continue; 54305c91076SPawel Jakub Dawidek } 544a73148d2SPawel Jakub Dawidek err(EXIT_FAILURE, "Cannot access library"); 545a73148d2SPawel Jakub Dawidek } 5462591e96cSUlf Lilleengen break; 5472591e96cSUlf Lilleengen } 5485d824386SUlf Lilleengen free(tofree); 5492591e96cSUlf Lilleengen /* No library was found, but standard commands can still be used */ 5502591e96cSUlf Lilleengen if (ret == -1) 5512591e96cSUlf Lilleengen return; 552a73148d2SPawel Jakub Dawidek dlh = dlopen(path, RTLD_NOW); 553a73148d2SPawel Jakub Dawidek if (dlh == NULL) 554a73148d2SPawel Jakub Dawidek errx(EXIT_FAILURE, "Cannot open library: %s.", dlerror()); 55505c91076SPawel Jakub Dawidek lib_version = dlsym(dlh, "lib_version"); 55605c91076SPawel Jakub Dawidek if (lib_version == NULL) { 5576fc60008SPawel Jakub Dawidek warnx("Cannot find symbol %s: %s.", "lib_version", dlerror()); 55805c91076SPawel Jakub Dawidek dlclose(dlh); 55905c91076SPawel Jakub Dawidek exit(EXIT_FAILURE); 56005c91076SPawel Jakub Dawidek } 56105c91076SPawel Jakub Dawidek if (*lib_version != G_LIB_VERSION) { 56205c91076SPawel Jakub Dawidek dlclose(dlh); 563f792f1d8SPawel Jakub Dawidek errx(EXIT_FAILURE, "%s and %s are not synchronized.", 564f792f1d8SPawel Jakub Dawidek getprogname(), path); 56505c91076SPawel Jakub Dawidek } 56605c91076SPawel Jakub Dawidek version = dlsym(dlh, "version"); 56705c91076SPawel Jakub Dawidek if (version == NULL) { 5686fc60008SPawel Jakub Dawidek warnx("Cannot find symbol %s: %s.", "version", dlerror()); 56905c91076SPawel Jakub Dawidek dlclose(dlh); 57005c91076SPawel Jakub Dawidek exit(EXIT_FAILURE); 57105c91076SPawel Jakub Dawidek } 57205c91076SPawel Jakub Dawidek class_commands = dlsym(dlh, "class_commands"); 57305c91076SPawel Jakub Dawidek if (class_commands == NULL) { 5746fc60008SPawel Jakub Dawidek warnx("Cannot find symbol %s: %s.", "class_commands", 5756fc60008SPawel Jakub Dawidek dlerror()); 57605c91076SPawel Jakub Dawidek dlclose(dlh); 57705c91076SPawel Jakub Dawidek exit(EXIT_FAILURE); 57805c91076SPawel Jakub Dawidek } 57905c91076SPawel Jakub Dawidek } 58014bf405bSMarcel Moolenaar #endif /* !STATIC_GEOM_CLASSES */ 58105c91076SPawel Jakub Dawidek 58205c91076SPawel Jakub Dawidek /* 58305c91076SPawel Jakub Dawidek * Class name should be all capital letters. 58405c91076SPawel Jakub Dawidek */ 58505c91076SPawel Jakub Dawidek static void 58605c91076SPawel Jakub Dawidek set_class_name(void) 58705c91076SPawel Jakub Dawidek { 58805c91076SPawel Jakub Dawidek char *s1, *s2; 58905c91076SPawel Jakub Dawidek 590ee602fbbSPawel Jakub Dawidek s1 = class_name; 591ee602fbbSPawel Jakub Dawidek for (; *s1 != '\0'; s1++) 592ee602fbbSPawel Jakub Dawidek *s1 = tolower(*s1); 593cb94ab30SPawel Jakub Dawidek gclass_name = malloc(strlen(class_name) + 1); 59405c91076SPawel Jakub Dawidek if (gclass_name == NULL) 59505c91076SPawel Jakub Dawidek errx(EXIT_FAILURE, "No memory"); 59605c91076SPawel Jakub Dawidek s1 = gclass_name; 59705c91076SPawel Jakub Dawidek s2 = class_name; 59805c91076SPawel Jakub Dawidek for (; *s2 != '\0'; s2++) 59905c91076SPawel Jakub Dawidek *s1++ = toupper(*s2); 60005c91076SPawel Jakub Dawidek *s1 = '\0'; 60105c91076SPawel Jakub Dawidek } 60205c91076SPawel Jakub Dawidek 60305c91076SPawel Jakub Dawidek static void 60405c91076SPawel Jakub Dawidek get_class(int *argc, char ***argv) 60505c91076SPawel Jakub Dawidek { 60605c91076SPawel Jakub Dawidek 60705c91076SPawel Jakub Dawidek snprintf(comm, sizeof(comm), "%s", basename((*argv)[0])); 60805c91076SPawel Jakub Dawidek if (strcmp(comm, "geom") == 0) { 60905c91076SPawel Jakub Dawidek if (*argc < 2) 610c979e206SPawel Jakub Dawidek usage(); 611687b0015SPawel Jakub Dawidek else if (*argc == 2) { 612687b0015SPawel Jakub Dawidek if (strcmp((*argv)[1], "-h") == 0 || 613687b0015SPawel Jakub Dawidek strcmp((*argv)[1], "help") == 0) { 614c979e206SPawel Jakub Dawidek usage(); 615687b0015SPawel Jakub Dawidek } 616687b0015SPawel Jakub Dawidek } 61705c91076SPawel Jakub Dawidek strlcatf(comm, sizeof(comm), " %s", (*argv)[1]); 61805c91076SPawel Jakub Dawidek class_name = (*argv)[1]; 61905c91076SPawel Jakub Dawidek *argc -= 2; 62005c91076SPawel Jakub Dawidek *argv += 2; 62105c91076SPawel Jakub Dawidek } else if (*comm == 'g') { 62205c91076SPawel Jakub Dawidek class_name = comm + 1; 62305c91076SPawel Jakub Dawidek *argc -= 1; 62405c91076SPawel Jakub Dawidek *argv += 1; 62505c91076SPawel Jakub Dawidek } else { 62605c91076SPawel Jakub Dawidek errx(EXIT_FAILURE, "Invalid utility name."); 62705c91076SPawel Jakub Dawidek } 628a16f9b36SMarcel Moolenaar 62914bf405bSMarcel Moolenaar #ifndef STATIC_GEOM_CLASSES 63005c91076SPawel Jakub Dawidek load_library(); 631a16f9b36SMarcel Moolenaar #else 632a16f9b36SMarcel Moolenaar if (!strcasecmp(class_name, "part")) { 633a16f9b36SMarcel Moolenaar version = &gpart_version; 634a16f9b36SMarcel Moolenaar class_commands = gpart_class_commands; 635bc69d66fSXin LI } else if (!strcasecmp(class_name, "label")) { 636bc69d66fSXin LI version = &glabel_version; 637bc69d66fSXin LI class_commands = glabel_class_commands; 638a16f9b36SMarcel Moolenaar } else 639a16f9b36SMarcel Moolenaar errx(EXIT_FAILURE, "Invalid class name."); 64014bf405bSMarcel Moolenaar #endif /* !STATIC_GEOM_CLASSES */ 641a16f9b36SMarcel Moolenaar 642a16f9b36SMarcel Moolenaar set_class_name(); 64305c91076SPawel Jakub Dawidek if (*argc < 1) 644c979e206SPawel Jakub Dawidek usage(); 64505c91076SPawel Jakub Dawidek } 64605c91076SPawel Jakub Dawidek 64705c91076SPawel Jakub Dawidek int 64805c91076SPawel Jakub Dawidek main(int argc, char *argv[]) 64905c91076SPawel Jakub Dawidek { 65005c91076SPawel Jakub Dawidek 65105c91076SPawel Jakub Dawidek get_class(&argc, &argv); 65205c91076SPawel Jakub Dawidek run_command(argc, argv); 65305c91076SPawel Jakub Dawidek /* NOTREACHED */ 65405c91076SPawel Jakub Dawidek 65505c91076SPawel Jakub Dawidek exit(EXIT_FAILURE); 65605c91076SPawel Jakub Dawidek } 65705c91076SPawel Jakub Dawidek 65805c91076SPawel Jakub Dawidek static struct gclass * 65905c91076SPawel Jakub Dawidek find_class(struct gmesh *mesh, const char *name) 66005c91076SPawel Jakub Dawidek { 66105c91076SPawel Jakub Dawidek struct gclass *classp; 66205c91076SPawel Jakub Dawidek 66305c91076SPawel Jakub Dawidek LIST_FOREACH(classp, &mesh->lg_class, lg_class) { 66405c91076SPawel Jakub Dawidek if (strcmp(classp->lg_name, name) == 0) 66505c91076SPawel Jakub Dawidek return (classp); 66605c91076SPawel Jakub Dawidek } 66705c91076SPawel Jakub Dawidek return (NULL); 66805c91076SPawel Jakub Dawidek } 66905c91076SPawel Jakub Dawidek 6701d723f1dSPawel Jakub Dawidek static struct ggeom * 6711d723f1dSPawel Jakub Dawidek find_geom(struct gclass *classp, const char *name) 67205c91076SPawel Jakub Dawidek { 67305c91076SPawel Jakub Dawidek struct ggeom *gp; 67405c91076SPawel Jakub Dawidek 67505c91076SPawel Jakub Dawidek LIST_FOREACH(gp, &classp->lg_geom, lg_geom) { 6761d723f1dSPawel Jakub Dawidek if (strcmp(gp->lg_name, name) == 0) 6771d723f1dSPawel Jakub Dawidek return (gp); 67805c91076SPawel Jakub Dawidek } 67905c91076SPawel Jakub Dawidek return (NULL); 68005c91076SPawel Jakub Dawidek } 68105c91076SPawel Jakub Dawidek 68205c91076SPawel Jakub Dawidek static void 6832d76e2dbSPawel Jakub Dawidek list_one_provider(struct gprovider *pp, const char *prefix) 68405c91076SPawel Jakub Dawidek { 68505c91076SPawel Jakub Dawidek struct gconfig *conf; 686af565b58SPawel Jakub Dawidek char buf[5]; 68705c91076SPawel Jakub Dawidek 6881d723f1dSPawel Jakub Dawidek printf("Name: %s\n", pp->lg_name); 689af565b58SPawel Jakub Dawidek humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "", 690af565b58SPawel Jakub Dawidek HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 6911d723f1dSPawel Jakub Dawidek printf("%sMediasize: %jd (%s)\n", prefix, (intmax_t)pp->lg_mediasize, 6921d723f1dSPawel Jakub Dawidek buf); 6931d723f1dSPawel Jakub Dawidek printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize); 694e192c6e8SXin LI if (pp->lg_stripesize > 0 || pp->lg_stripeoffset > 0) { 69535daa28fSXin LI printf("%sStripesize: %ju\n", prefix, pp->lg_stripesize); 69635daa28fSXin LI printf("%sStripeoffset: %ju\n", prefix, pp->lg_stripeoffset); 69735daa28fSXin LI } 6981d723f1dSPawel Jakub Dawidek printf("%sMode: %s\n", prefix, pp->lg_mode); 69905c91076SPawel Jakub Dawidek LIST_FOREACH(conf, &pp->lg_config, lg_config) { 7001d723f1dSPawel Jakub Dawidek printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val); 7011d723f1dSPawel Jakub Dawidek } 7021d723f1dSPawel Jakub Dawidek } 7031d723f1dSPawel Jakub Dawidek 7041d723f1dSPawel Jakub Dawidek static void 7052d76e2dbSPawel Jakub Dawidek list_one_consumer(struct gconsumer *cp, const char *prefix) 7061d723f1dSPawel Jakub Dawidek { 7071d723f1dSPawel Jakub Dawidek struct gprovider *pp; 7081d723f1dSPawel Jakub Dawidek struct gconfig *conf; 7091d723f1dSPawel Jakub Dawidek 7101d723f1dSPawel Jakub Dawidek pp = cp->lg_provider; 7111d723f1dSPawel Jakub Dawidek if (pp == NULL) 7121d723f1dSPawel Jakub Dawidek printf("[no provider]\n"); 7131d723f1dSPawel Jakub Dawidek else { 7141d723f1dSPawel Jakub Dawidek char buf[5]; 7151d723f1dSPawel Jakub Dawidek 7161d723f1dSPawel Jakub Dawidek printf("Name: %s\n", pp->lg_name); 7171d723f1dSPawel Jakub Dawidek humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "", 7181d723f1dSPawel Jakub Dawidek HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 7191d723f1dSPawel Jakub Dawidek printf("%sMediasize: %jd (%s)\n", prefix, 7201d723f1dSPawel Jakub Dawidek (intmax_t)pp->lg_mediasize, buf); 7211d723f1dSPawel Jakub Dawidek printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize); 72200b236aaSXin LI if (pp->lg_stripesize > 0 || pp->lg_stripeoffset > 0) { 72335daa28fSXin LI printf("%sStripesize: %ju\n", prefix, pp->lg_stripesize); 72435daa28fSXin LI printf("%sStripeoffset: %ju\n", prefix, pp->lg_stripeoffset); 72535daa28fSXin LI } 7261d723f1dSPawel Jakub Dawidek printf("%sMode: %s\n", prefix, cp->lg_mode); 7271d723f1dSPawel Jakub Dawidek } 7281d723f1dSPawel Jakub Dawidek LIST_FOREACH(conf, &cp->lg_config, lg_config) { 7291d723f1dSPawel Jakub Dawidek printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val); 7301d723f1dSPawel Jakub Dawidek } 7311d723f1dSPawel Jakub Dawidek } 7321d723f1dSPawel Jakub Dawidek 7331d723f1dSPawel Jakub Dawidek static void 7342d76e2dbSPawel Jakub Dawidek list_one_geom(struct ggeom *gp) 7351d723f1dSPawel Jakub Dawidek { 7361d723f1dSPawel Jakub Dawidek struct gprovider *pp; 7371d723f1dSPawel Jakub Dawidek struct gconsumer *cp; 7381d723f1dSPawel Jakub Dawidek struct gconfig *conf; 7391d723f1dSPawel Jakub Dawidek unsigned n; 7401d723f1dSPawel Jakub Dawidek 7411d723f1dSPawel Jakub Dawidek printf("Geom name: %s\n", gp->lg_name); 7421d723f1dSPawel Jakub Dawidek LIST_FOREACH(conf, &gp->lg_config, lg_config) { 7431d723f1dSPawel Jakub Dawidek printf("%s: %s\n", conf->lg_name, conf->lg_val); 7441d723f1dSPawel Jakub Dawidek } 7451d723f1dSPawel Jakub Dawidek if (!LIST_EMPTY(&gp->lg_provider)) { 7461d723f1dSPawel Jakub Dawidek printf("Providers:\n"); 7471d723f1dSPawel Jakub Dawidek n = 1; 7481d723f1dSPawel Jakub Dawidek LIST_FOREACH(pp, &gp->lg_provider, lg_provider) { 7491d723f1dSPawel Jakub Dawidek printf("%u. ", n++); 7502d76e2dbSPawel Jakub Dawidek list_one_provider(pp, " "); 7511d723f1dSPawel Jakub Dawidek } 7521d723f1dSPawel Jakub Dawidek } 7531d723f1dSPawel Jakub Dawidek if (!LIST_EMPTY(&gp->lg_consumer)) { 7541d723f1dSPawel Jakub Dawidek printf("Consumers:\n"); 7551d723f1dSPawel Jakub Dawidek n = 1; 7561d723f1dSPawel Jakub Dawidek LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) { 7571d723f1dSPawel Jakub Dawidek printf("%u. ", n++); 7582d76e2dbSPawel Jakub Dawidek list_one_consumer(cp, " "); 7591d723f1dSPawel Jakub Dawidek } 76005c91076SPawel Jakub Dawidek } 76105c91076SPawel Jakub Dawidek printf("\n"); 76205c91076SPawel Jakub Dawidek } 76305c91076SPawel Jakub Dawidek 764b70eccf3SPawel Jakub Dawidek static void 765b70eccf3SPawel Jakub Dawidek std_help(struct gctl_req *req __unused, unsigned flags __unused) 766b70eccf3SPawel Jakub Dawidek { 767b70eccf3SPawel Jakub Dawidek 768c979e206SPawel Jakub Dawidek usage(); 769b70eccf3SPawel Jakub Dawidek } 770b70eccf3SPawel Jakub Dawidek 77105c91076SPawel Jakub Dawidek static int 77205c91076SPawel Jakub Dawidek std_list_available(void) 77305c91076SPawel Jakub Dawidek { 77405c91076SPawel Jakub Dawidek struct gmesh mesh; 77505c91076SPawel Jakub Dawidek struct gclass *classp; 77605c91076SPawel Jakub Dawidek int error; 77705c91076SPawel Jakub Dawidek 77805c91076SPawel Jakub Dawidek error = geom_gettree(&mesh); 7796fc60008SPawel Jakub Dawidek if (error != 0) 7806fc60008SPawel Jakub Dawidek errc(EXIT_FAILURE, error, "Cannot get GEOM tree"); 78105c91076SPawel Jakub Dawidek classp = find_class(&mesh, gclass_name); 78205c91076SPawel Jakub Dawidek geom_deletetree(&mesh); 78305c91076SPawel Jakub Dawidek if (classp != NULL) 78405c91076SPawel Jakub Dawidek return (1); 78505c91076SPawel Jakub Dawidek return (0); 78605c91076SPawel Jakub Dawidek } 78705c91076SPawel Jakub Dawidek 78805c91076SPawel Jakub Dawidek static void 78905c91076SPawel Jakub Dawidek std_list(struct gctl_req *req, unsigned flags __unused) 79005c91076SPawel Jakub Dawidek { 79105c91076SPawel Jakub Dawidek struct gmesh mesh; 79205c91076SPawel Jakub Dawidek struct gclass *classp; 7931d723f1dSPawel Jakub Dawidek struct ggeom *gp; 794f13942a7SPawel Jakub Dawidek const char *name; 795*83d165c1SAlexander Motin int all, error, i, nargs; 79605c91076SPawel Jakub Dawidek 79705c91076SPawel Jakub Dawidek error = geom_gettree(&mesh); 7986fc60008SPawel Jakub Dawidek if (error != 0) 7996fc60008SPawel Jakub Dawidek errc(EXIT_FAILURE, error, "Cannot get GEOM tree"); 80005c91076SPawel Jakub Dawidek classp = find_class(&mesh, gclass_name); 80105c91076SPawel Jakub Dawidek if (classp == NULL) { 80205c91076SPawel Jakub Dawidek geom_deletetree(&mesh); 8035d47cd0fSPawel Jakub Dawidek errx(EXIT_FAILURE, "Class %s not found.", gclass_name); 80405c91076SPawel Jakub Dawidek } 805f13942a7SPawel Jakub Dawidek nargs = gctl_get_int(req, "nargs"); 806*83d165c1SAlexander Motin all = gctl_get_int(req, "all"); 807f13942a7SPawel Jakub Dawidek if (nargs > 0) { 808f13942a7SPawel Jakub Dawidek for (i = 0; i < nargs; i++) { 809f13942a7SPawel Jakub Dawidek name = gctl_get_ascii(req, "arg%d", i); 8101d723f1dSPawel Jakub Dawidek gp = find_geom(classp, name); 811*83d165c1SAlexander Motin if (gp == NULL) 8125d47cd0fSPawel Jakub Dawidek errx(EXIT_FAILURE, "No such geom: %s.", name); 813*83d165c1SAlexander Motin list_one_geom(gp); 81405c91076SPawel Jakub Dawidek } 81505c91076SPawel Jakub Dawidek } else { 81605c91076SPawel Jakub Dawidek LIST_FOREACH(gp, &classp->lg_geom, lg_geom) { 817*83d165c1SAlexander Motin if (LIST_EMPTY(&gp->lg_provider) && !all) 8185fd66a44SPawel Jakub Dawidek continue; 8192d76e2dbSPawel Jakub Dawidek list_one_geom(gp); 82005c91076SPawel Jakub Dawidek } 82105c91076SPawel Jakub Dawidek } 82205c91076SPawel Jakub Dawidek geom_deletetree(&mesh); 82305c91076SPawel Jakub Dawidek } 82405c91076SPawel Jakub Dawidek 82505c91076SPawel Jakub Dawidek static int 82618ee8840SPawel Jakub Dawidek std_status_available(void) 82718ee8840SPawel Jakub Dawidek { 82818ee8840SPawel Jakub Dawidek 82918ee8840SPawel Jakub Dawidek /* 'status' command is available when 'list' command is. */ 83018ee8840SPawel Jakub Dawidek return (std_list_available()); 83118ee8840SPawel Jakub Dawidek } 83218ee8840SPawel Jakub Dawidek 83318ee8840SPawel Jakub Dawidek static void 834da80913dSPawel Jakub Dawidek status_update_len(struct ggeom *gp, int *name_len, int *status_len) 83518ee8840SPawel Jakub Dawidek { 83618ee8840SPawel Jakub Dawidek struct gconfig *conf; 837da80913dSPawel Jakub Dawidek int len; 83818ee8840SPawel Jakub Dawidek 83918ee8840SPawel Jakub Dawidek assert(gp != NULL); 84018ee8840SPawel Jakub Dawidek assert(name_len != NULL); 84118ee8840SPawel Jakub Dawidek assert(status_len != NULL); 84218ee8840SPawel Jakub Dawidek 84318ee8840SPawel Jakub Dawidek len = strlen(gp->lg_name); 84418ee8840SPawel Jakub Dawidek if (*name_len < len) 84518ee8840SPawel Jakub Dawidek *name_len = len; 84618ee8840SPawel Jakub Dawidek LIST_FOREACH(conf, &gp->lg_config, lg_config) { 84718ee8840SPawel Jakub Dawidek if (strcasecmp(conf->lg_name, "state") == 0) { 84818ee8840SPawel Jakub Dawidek len = strlen(conf->lg_val); 84918ee8840SPawel Jakub Dawidek if (*status_len < len) 85018ee8840SPawel Jakub Dawidek *status_len = len; 85118ee8840SPawel Jakub Dawidek } 85218ee8840SPawel Jakub Dawidek } 85318ee8840SPawel Jakub Dawidek } 85418ee8840SPawel Jakub Dawidek 855*83d165c1SAlexander Motin static void 856*83d165c1SAlexander Motin status_update_len_prs(struct ggeom *gp, int *name_len, int *status_len) 857*83d165c1SAlexander Motin { 858*83d165c1SAlexander Motin struct gprovider *pp; 859*83d165c1SAlexander Motin struct gconfig *conf; 860*83d165c1SAlexander Motin int len, glen; 861*83d165c1SAlexander Motin 862*83d165c1SAlexander Motin assert(gp != NULL); 863*83d165c1SAlexander Motin assert(name_len != NULL); 864*83d165c1SAlexander Motin assert(status_len != NULL); 865*83d165c1SAlexander Motin 866*83d165c1SAlexander Motin glen = 0; 867*83d165c1SAlexander Motin LIST_FOREACH(conf, &gp->lg_config, lg_config) { 868*83d165c1SAlexander Motin if (strcasecmp(conf->lg_name, "state") == 0) { 869*83d165c1SAlexander Motin glen = strlen(conf->lg_val); 870*83d165c1SAlexander Motin break; 871*83d165c1SAlexander Motin } 872*83d165c1SAlexander Motin } 873*83d165c1SAlexander Motin LIST_FOREACH(pp, &gp->lg_provider, lg_provider) { 874*83d165c1SAlexander Motin len = strlen(pp->lg_name); 875*83d165c1SAlexander Motin if (*name_len < len) 876*83d165c1SAlexander Motin *name_len = len; 877*83d165c1SAlexander Motin len = glen; 878*83d165c1SAlexander Motin LIST_FOREACH(conf, &pp->lg_config, lg_config) { 879*83d165c1SAlexander Motin if (strcasecmp(conf->lg_name, "state") == 0) { 880*83d165c1SAlexander Motin len = strlen(conf->lg_val); 881*83d165c1SAlexander Motin break; 882*83d165c1SAlexander Motin } 883*83d165c1SAlexander Motin } 884*83d165c1SAlexander Motin if (*status_len < len) 885*83d165c1SAlexander Motin *status_len = len; 886*83d165c1SAlexander Motin } 887*83d165c1SAlexander Motin } 888*83d165c1SAlexander Motin 889ba6821f0SPawel Jakub Dawidek static char * 89018ee8840SPawel Jakub Dawidek status_one_consumer(struct gconsumer *cp) 89118ee8840SPawel Jakub Dawidek { 892ba6821f0SPawel Jakub Dawidek static char buf[256]; 89318ee8840SPawel Jakub Dawidek struct gprovider *pp; 89418ee8840SPawel Jakub Dawidek struct gconfig *conf; 895*83d165c1SAlexander Motin const char *state, *syncr; 89618ee8840SPawel Jakub Dawidek 89718ee8840SPawel Jakub Dawidek pp = cp->lg_provider; 89818ee8840SPawel Jakub Dawidek if (pp == NULL) 899ba6821f0SPawel Jakub Dawidek return (NULL); 900*83d165c1SAlexander Motin state = NULL; 901*83d165c1SAlexander Motin syncr = NULL; 90218ee8840SPawel Jakub Dawidek LIST_FOREACH(conf, &cp->lg_config, lg_config) { 903*83d165c1SAlexander Motin if (strcasecmp(conf->lg_name, "state") == 0) 904*83d165c1SAlexander Motin state = conf->lg_val; 90518ee8840SPawel Jakub Dawidek if (strcasecmp(conf->lg_name, "synchronized") == 0) 906*83d165c1SAlexander Motin syncr = conf->lg_val; 90718ee8840SPawel Jakub Dawidek } 908*83d165c1SAlexander Motin if (state == NULL && syncr == NULL) 909ba6821f0SPawel Jakub Dawidek snprintf(buf, sizeof(buf), "%s", pp->lg_name); 910*83d165c1SAlexander Motin else if (state != NULL && syncr != NULL) { 911*83d165c1SAlexander Motin snprintf(buf, sizeof(buf), "%s (%s, %s)", pp->lg_name, 912*83d165c1SAlexander Motin state, syncr); 913*83d165c1SAlexander Motin } else { 914ba6821f0SPawel Jakub Dawidek snprintf(buf, sizeof(buf), "%s (%s)", pp->lg_name, 915*83d165c1SAlexander Motin state ? state : syncr); 916ba6821f0SPawel Jakub Dawidek } 917ba6821f0SPawel Jakub Dawidek return (buf); 91818ee8840SPawel Jakub Dawidek } 91918ee8840SPawel Jakub Dawidek 92018ee8840SPawel Jakub Dawidek static void 921ba6821f0SPawel Jakub Dawidek status_one_geom(struct ggeom *gp, int script, int name_len, int status_len) 92218ee8840SPawel Jakub Dawidek { 92318ee8840SPawel Jakub Dawidek struct gconsumer *cp; 92418ee8840SPawel Jakub Dawidek struct gconfig *conf; 925ba6821f0SPawel Jakub Dawidek const char *name, *status, *component; 926ba6821f0SPawel Jakub Dawidek int gotone; 92718ee8840SPawel Jakub Dawidek 92818ee8840SPawel Jakub Dawidek name = gp->lg_name; 929*83d165c1SAlexander Motin status = "N/A"; 93018ee8840SPawel Jakub Dawidek LIST_FOREACH(conf, &gp->lg_config, lg_config) { 931*83d165c1SAlexander Motin if (strcasecmp(conf->lg_name, "state") == 0) { 932*83d165c1SAlexander Motin status = conf->lg_val; 93318ee8840SPawel Jakub Dawidek break; 93418ee8840SPawel Jakub Dawidek } 935*83d165c1SAlexander Motin } 936ba6821f0SPawel Jakub Dawidek gotone = 0; 93718ee8840SPawel Jakub Dawidek LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) { 938ba6821f0SPawel Jakub Dawidek component = status_one_consumer(cp); 939ba6821f0SPawel Jakub Dawidek if (component == NULL) 940ba6821f0SPawel Jakub Dawidek continue; 941ba6821f0SPawel Jakub Dawidek gotone = 1; 942ba6821f0SPawel Jakub Dawidek printf("%*s %*s %s\n", name_len, name, status_len, status, 943ba6821f0SPawel Jakub Dawidek component); 944ba6821f0SPawel Jakub Dawidek if (!script) 945ba6821f0SPawel Jakub Dawidek name = status = ""; 94618ee8840SPawel Jakub Dawidek } 947ba6821f0SPawel Jakub Dawidek if (!gotone) { 948ba6821f0SPawel Jakub Dawidek printf("%*s %*s %s\n", name_len, name, status_len, status, 949ba6821f0SPawel Jakub Dawidek "N/A"); 950ba6821f0SPawel Jakub Dawidek } 95118ee8840SPawel Jakub Dawidek } 95218ee8840SPawel Jakub Dawidek 95318ee8840SPawel Jakub Dawidek static void 954*83d165c1SAlexander Motin status_one_geom_prs(struct ggeom *gp, int script, int name_len, int status_len) 955*83d165c1SAlexander Motin { 956*83d165c1SAlexander Motin struct gprovider *pp; 957*83d165c1SAlexander Motin struct gconsumer *cp; 958*83d165c1SAlexander Motin struct gconfig *conf; 959*83d165c1SAlexander Motin const char *name, *status, *component; 960*83d165c1SAlexander Motin int gotone; 961*83d165c1SAlexander Motin 962*83d165c1SAlexander Motin LIST_FOREACH(pp, &gp->lg_provider, lg_provider) { 963*83d165c1SAlexander Motin name = pp->lg_name; 964*83d165c1SAlexander Motin status = "N/A"; 965*83d165c1SAlexander Motin LIST_FOREACH(conf, &gp->lg_config, lg_config) { 966*83d165c1SAlexander Motin if (strcasecmp(conf->lg_name, "state") == 0) { 967*83d165c1SAlexander Motin status = conf->lg_val; 968*83d165c1SAlexander Motin break; 969*83d165c1SAlexander Motin } 970*83d165c1SAlexander Motin } 971*83d165c1SAlexander Motin LIST_FOREACH(conf, &pp->lg_config, lg_config) { 972*83d165c1SAlexander Motin if (strcasecmp(conf->lg_name, "state") == 0) { 973*83d165c1SAlexander Motin status = conf->lg_val; 974*83d165c1SAlexander Motin break; 975*83d165c1SAlexander Motin } 976*83d165c1SAlexander Motin } 977*83d165c1SAlexander Motin gotone = 0; 978*83d165c1SAlexander Motin LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) { 979*83d165c1SAlexander Motin component = status_one_consumer(cp); 980*83d165c1SAlexander Motin if (component == NULL) 981*83d165c1SAlexander Motin continue; 982*83d165c1SAlexander Motin gotone = 1; 983*83d165c1SAlexander Motin printf("%*s %*s %s\n", name_len, name, 984*83d165c1SAlexander Motin status_len, status, component); 985*83d165c1SAlexander Motin if (!script) 986*83d165c1SAlexander Motin name = status = ""; 987*83d165c1SAlexander Motin } 988*83d165c1SAlexander Motin if (!gotone) { 989*83d165c1SAlexander Motin printf("%*s %*s %s\n", name_len, name, 990*83d165c1SAlexander Motin status_len, status, "N/A"); 991*83d165c1SAlexander Motin } 992*83d165c1SAlexander Motin } 993*83d165c1SAlexander Motin } 994*83d165c1SAlexander Motin 995*83d165c1SAlexander Motin static void 99618ee8840SPawel Jakub Dawidek std_status(struct gctl_req *req, unsigned flags __unused) 99718ee8840SPawel Jakub Dawidek { 99818ee8840SPawel Jakub Dawidek struct gmesh mesh; 99918ee8840SPawel Jakub Dawidek struct gclass *classp; 100018ee8840SPawel Jakub Dawidek struct ggeom *gp; 1001f13942a7SPawel Jakub Dawidek const char *name; 1002da80913dSPawel Jakub Dawidek int name_len, status_len; 1003*83d165c1SAlexander Motin int all, error, geoms, i, n, nargs, script; 100418ee8840SPawel Jakub Dawidek 100518ee8840SPawel Jakub Dawidek error = geom_gettree(&mesh); 10066fc60008SPawel Jakub Dawidek if (error != 0) 10076fc60008SPawel Jakub Dawidek errc(EXIT_FAILURE, error, "Cannot get GEOM tree"); 100818ee8840SPawel Jakub Dawidek classp = find_class(&mesh, gclass_name); 10095d47cd0fSPawel Jakub Dawidek if (classp == NULL) 10105d47cd0fSPawel Jakub Dawidek errx(EXIT_FAILURE, "Class %s not found.", gclass_name); 1011f13942a7SPawel Jakub Dawidek nargs = gctl_get_int(req, "nargs"); 1012*83d165c1SAlexander Motin all = gctl_get_int(req, "all"); 1013*83d165c1SAlexander Motin geoms = gctl_get_int(req, "geoms"); 1014f13942a7SPawel Jakub Dawidek script = gctl_get_int(req, "script"); 1015*83d165c1SAlexander Motin if (script) { 1016*83d165c1SAlexander Motin name_len = 0; 1017*83d165c1SAlexander Motin status_len = 0; 1018*83d165c1SAlexander Motin } else { 101918ee8840SPawel Jakub Dawidek name_len = strlen("Name"); 102018ee8840SPawel Jakub Dawidek status_len = strlen("Status"); 1021*83d165c1SAlexander Motin } 1022f13942a7SPawel Jakub Dawidek if (nargs > 0) { 1023f13942a7SPawel Jakub Dawidek for (i = 0, n = 0; i < nargs; i++) { 1024f13942a7SPawel Jakub Dawidek name = gctl_get_ascii(req, "arg%d", i); 102518ee8840SPawel Jakub Dawidek gp = find_geom(classp, name); 102618ee8840SPawel Jakub Dawidek if (gp == NULL) 10275d47cd0fSPawel Jakub Dawidek errx(EXIT_FAILURE, "No such geom: %s.", name); 1028*83d165c1SAlexander Motin if (geoms) { 1029*83d165c1SAlexander Motin status_update_len(gp, 1030*83d165c1SAlexander Motin &name_len, &status_len); 1031*83d165c1SAlexander Motin } else { 1032*83d165c1SAlexander Motin status_update_len_prs(gp, 1033*83d165c1SAlexander Motin &name_len, &status_len); 103418ee8840SPawel Jakub Dawidek } 1035*83d165c1SAlexander Motin n++; 103618ee8840SPawel Jakub Dawidek } 103718ee8840SPawel Jakub Dawidek if (n == 0) 103818ee8840SPawel Jakub Dawidek goto end; 103918ee8840SPawel Jakub Dawidek } else { 1040f13942a7SPawel Jakub Dawidek n = 0; 104118ee8840SPawel Jakub Dawidek LIST_FOREACH(gp, &classp->lg_geom, lg_geom) { 1042*83d165c1SAlexander Motin if (LIST_EMPTY(&gp->lg_provider) && !all) 104318ee8840SPawel Jakub Dawidek continue; 1044*83d165c1SAlexander Motin if (geoms) { 1045*83d165c1SAlexander Motin status_update_len(gp, 1046*83d165c1SAlexander Motin &name_len, &status_len); 1047*83d165c1SAlexander Motin } else { 1048*83d165c1SAlexander Motin status_update_len_prs(gp, 1049*83d165c1SAlexander Motin &name_len, &status_len); 1050*83d165c1SAlexander Motin } 105118ee8840SPawel Jakub Dawidek n++; 105218ee8840SPawel Jakub Dawidek } 105318ee8840SPawel Jakub Dawidek if (n == 0) 105418ee8840SPawel Jakub Dawidek goto end; 105518ee8840SPawel Jakub Dawidek } 1056f13942a7SPawel Jakub Dawidek if (!script) { 1057da80913dSPawel Jakub Dawidek printf("%*s %*s %s\n", name_len, "Name", status_len, "Status", 105818ee8840SPawel Jakub Dawidek "Components"); 1059ba6821f0SPawel Jakub Dawidek } 1060f13942a7SPawel Jakub Dawidek if (nargs > 0) { 1061f13942a7SPawel Jakub Dawidek for (i = 0; i < nargs; i++) { 1062f13942a7SPawel Jakub Dawidek name = gctl_get_ascii(req, "arg%d", i); 106318ee8840SPawel Jakub Dawidek gp = find_geom(classp, name); 1064*83d165c1SAlexander Motin if (gp == NULL) 1065*83d165c1SAlexander Motin continue; 1066*83d165c1SAlexander Motin if (geoms) { 1067f13942a7SPawel Jakub Dawidek status_one_geom(gp, script, name_len, 1068ba6821f0SPawel Jakub Dawidek status_len); 1069*83d165c1SAlexander Motin } else { 1070*83d165c1SAlexander Motin status_one_geom_prs(gp, script, name_len, 1071*83d165c1SAlexander Motin status_len); 107218ee8840SPawel Jakub Dawidek } 107318ee8840SPawel Jakub Dawidek } 107418ee8840SPawel Jakub Dawidek } else { 107518ee8840SPawel Jakub Dawidek LIST_FOREACH(gp, &classp->lg_geom, lg_geom) { 1076*83d165c1SAlexander Motin if (LIST_EMPTY(&gp->lg_provider) && !all) 107718ee8840SPawel Jakub Dawidek continue; 1078*83d165c1SAlexander Motin if (geoms) { 1079*83d165c1SAlexander Motin status_one_geom(gp, script, name_len, 1080*83d165c1SAlexander Motin status_len); 1081*83d165c1SAlexander Motin } else { 1082*83d165c1SAlexander Motin status_one_geom_prs(gp, script, name_len, 1083*83d165c1SAlexander Motin status_len); 1084*83d165c1SAlexander Motin } 108518ee8840SPawel Jakub Dawidek } 108618ee8840SPawel Jakub Dawidek } 108718ee8840SPawel Jakub Dawidek end: 108818ee8840SPawel Jakub Dawidek geom_deletetree(&mesh); 108918ee8840SPawel Jakub Dawidek } 109018ee8840SPawel Jakub Dawidek 109118ee8840SPawel Jakub Dawidek static int 109205c91076SPawel Jakub Dawidek std_load_available(void) 109305c91076SPawel Jakub Dawidek { 109405c91076SPawel Jakub Dawidek char name[MAXPATHLEN], paths[MAXPATHLEN * 8], *p; 109505c91076SPawel Jakub Dawidek struct stat sb; 109605c91076SPawel Jakub Dawidek size_t len; 109705c91076SPawel Jakub Dawidek 109805c91076SPawel Jakub Dawidek snprintf(name, sizeof(name), "g_%s", class_name); 109905c91076SPawel Jakub Dawidek /* 110005c91076SPawel Jakub Dawidek * If already in kernel, "load" command is not available. 110105c91076SPawel Jakub Dawidek */ 110205c91076SPawel Jakub Dawidek if (modfind(name) >= 0) 110305c91076SPawel Jakub Dawidek return (0); 110405c91076SPawel Jakub Dawidek bzero(paths, sizeof(paths)); 110505c91076SPawel Jakub Dawidek len = sizeof(paths); 110605c91076SPawel Jakub Dawidek if (sysctlbyname("kern.module_path", paths, &len, NULL, 0) < 0) 110705c91076SPawel Jakub Dawidek err(EXIT_FAILURE, "sysctl(kern.module_path)"); 110805c91076SPawel Jakub Dawidek for (p = strtok(paths, ";"); p != NULL; p = strtok(NULL, ";")) { 110905c91076SPawel Jakub Dawidek snprintf(name, sizeof(name), "%s/geom_%s.ko", p, class_name); 111005c91076SPawel Jakub Dawidek /* 111105c91076SPawel Jakub Dawidek * If geom_<name>.ko file exists, "load" command is available. 111205c91076SPawel Jakub Dawidek */ 111305c91076SPawel Jakub Dawidek if (stat(name, &sb) == 0) 111405c91076SPawel Jakub Dawidek return (1); 111505c91076SPawel Jakub Dawidek } 111605c91076SPawel Jakub Dawidek return (0); 111705c91076SPawel Jakub Dawidek } 111805c91076SPawel Jakub Dawidek 111905c91076SPawel Jakub Dawidek static void 112005c91076SPawel Jakub Dawidek std_load(struct gctl_req *req __unused, unsigned flags) 112105c91076SPawel Jakub Dawidek { 112205c91076SPawel Jakub Dawidek 112305c91076SPawel Jakub Dawidek /* 112405c91076SPawel Jakub Dawidek * Do nothing special here, because of G_FLAG_LOADKLD flag, 112505c91076SPawel Jakub Dawidek * module is already loaded. 112605c91076SPawel Jakub Dawidek */ 112705c91076SPawel Jakub Dawidek if ((flags & G_FLAG_VERBOSE) != 0) 112805c91076SPawel Jakub Dawidek printf("Module available.\n"); 112905c91076SPawel Jakub Dawidek } 113005c91076SPawel Jakub Dawidek 113105c91076SPawel Jakub Dawidek static int 113205c91076SPawel Jakub Dawidek std_unload_available(void) 113305c91076SPawel Jakub Dawidek { 113405c91076SPawel Jakub Dawidek char name[64]; 113505c91076SPawel Jakub Dawidek int id; 113605c91076SPawel Jakub Dawidek 113705c91076SPawel Jakub Dawidek snprintf(name, sizeof(name), "geom_%s", class_name); 113805c91076SPawel Jakub Dawidek id = kldfind(name); 113905c91076SPawel Jakub Dawidek if (id >= 0) 114005c91076SPawel Jakub Dawidek return (1); 114105c91076SPawel Jakub Dawidek return (0); 114205c91076SPawel Jakub Dawidek } 114305c91076SPawel Jakub Dawidek 114405c91076SPawel Jakub Dawidek static void 114505c91076SPawel Jakub Dawidek std_unload(struct gctl_req *req, unsigned flags __unused) 114605c91076SPawel Jakub Dawidek { 114705c91076SPawel Jakub Dawidek char name[64]; 114805c91076SPawel Jakub Dawidek int id; 114905c91076SPawel Jakub Dawidek 115005c91076SPawel Jakub Dawidek snprintf(name, sizeof(name), "geom_%s", class_name); 115105c91076SPawel Jakub Dawidek id = kldfind(name); 115205c91076SPawel Jakub Dawidek if (id < 0) { 115305c91076SPawel Jakub Dawidek gctl_error(req, "Could not find module: %s.", strerror(errno)); 115405c91076SPawel Jakub Dawidek return; 115505c91076SPawel Jakub Dawidek } 115605c91076SPawel Jakub Dawidek if (kldunload(id) < 0) { 115705c91076SPawel Jakub Dawidek gctl_error(req, "Could not unload module: %s.", 115805c91076SPawel Jakub Dawidek strerror(errno)); 115905c91076SPawel Jakub Dawidek return; 116005c91076SPawel Jakub Dawidek } 116105c91076SPawel Jakub Dawidek } 116205c91076SPawel Jakub Dawidek 116305c91076SPawel Jakub Dawidek static int 116405c91076SPawel Jakub Dawidek std_available(const char *name) 116505c91076SPawel Jakub Dawidek { 116605c91076SPawel Jakub Dawidek 1167b70eccf3SPawel Jakub Dawidek if (strcmp(name, "help") == 0) 1168b70eccf3SPawel Jakub Dawidek return (1); 1169b70eccf3SPawel Jakub Dawidek else if (strcmp(name, "list") == 0) 117005c91076SPawel Jakub Dawidek return (std_list_available()); 117118ee8840SPawel Jakub Dawidek else if (strcmp(name, "status") == 0) 117218ee8840SPawel Jakub Dawidek return (std_status_available()); 117305c91076SPawel Jakub Dawidek else if (strcmp(name, "load") == 0) 117405c91076SPawel Jakub Dawidek return (std_load_available()); 117505c91076SPawel Jakub Dawidek else if (strcmp(name, "unload") == 0) 117605c91076SPawel Jakub Dawidek return (std_unload_available()); 117705c91076SPawel Jakub Dawidek else 117805c91076SPawel Jakub Dawidek assert(!"Unknown standard command."); 117905c91076SPawel Jakub Dawidek return (0); 118005c91076SPawel Jakub Dawidek } 1181