2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004-2009 Pawel Jakub Dawidek <pjd@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/linker.h>
34 #include <sys/module.h>
36 #include <sys/sysctl.h>
56 #include "misc/subr.h"
58 #ifdef STATIC_GEOM_CLASSES
59 extern uint32_t gpart_version;
60 extern struct g_command gpart_class_commands[];
61 extern uint32_t glabel_version;
62 extern struct g_command glabel_class_commands[];
65 static char comm[MAXPATHLEN], *class_name = NULL, *gclass_name = NULL;
66 static uint32_t *version = NULL;
67 static int verbose = 0;
68 static struct g_command *class_commands = NULL;
70 #define GEOM_CLASS_CMDS 0x01
71 #define GEOM_STD_CMDS 0x02
73 #define GEOM_CLASS_WIDTH 10
75 static struct g_command *find_command(const char *cmdstr, int flags);
76 static void list_one_geom_by_provider(const char *provider_name);
77 static int std_available(const char *name);
79 static void std_help(struct gctl_req *req, unsigned flags);
80 static void std_list(struct gctl_req *req, unsigned flags);
81 static void std_status(struct gctl_req *req, unsigned flags);
82 static void std_load(struct gctl_req *req, unsigned flags);
83 static void std_unload(struct gctl_req *req, unsigned flags);
85 static struct g_command std_commands[] = {
86 { "help", 0, std_help, G_NULL_OPTS, NULL },
87 { "list", 0, std_list,
89 { 'a', "all", NULL, G_TYPE_BOOL },
94 { "status", 0, std_status,
96 { 'a', "all", NULL, G_TYPE_BOOL },
97 { 'g', "geoms", NULL, G_TYPE_BOOL },
98 { 's', "script", NULL, G_TYPE_BOOL },
103 { "load", G_FLAG_VERBOSE | G_FLAG_LOADKLD, std_load, G_NULL_OPTS,
105 { "unload", G_FLAG_VERBOSE, std_unload, G_NULL_OPTS, NULL },
110 usage_command(struct g_command *cmd, const char *prefix)
112 struct g_option *opt;
115 if (cmd->gc_usage != NULL) {
116 char *pos, *ptr, *sptr;
118 sptr = ptr = strdup(cmd->gc_usage);
119 while ((pos = strsep(&ptr, "\n")) != NULL) {
122 fprintf(stderr, "%s %s %s %s\n", prefix, comm,
129 fprintf(stderr, "%s %s %s", prefix, comm, cmd->gc_name);
130 if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0)
131 fprintf(stderr, " [-v]");
133 opt = &cmd->gc_options[i];
134 if (opt->go_name == NULL)
136 if (opt->go_val != NULL || G_OPT_TYPE(opt) == G_TYPE_BOOL)
137 fprintf(stderr, " [");
139 fprintf(stderr, " ");
140 fprintf(stderr, "-%c", opt->go_char);
141 if (G_OPT_TYPE(opt) != G_TYPE_BOOL)
142 fprintf(stderr, " %s", opt->go_name);
143 if (opt->go_val != NULL || G_OPT_TYPE(opt) == G_TYPE_BOOL)
144 fprintf(stderr, "]");
146 fprintf(stderr, "\n");
153 if (class_name == NULL) {
154 fprintf(stderr, "usage: geom <class> <command> [options]\n");
155 fprintf(stderr, " geom -p <provider-name>\n");
156 fprintf(stderr, " geom -t\n");
159 struct g_command *cmd;
164 if (class_commands != NULL) {
166 cmd = &class_commands[i];
167 if (cmd->gc_name == NULL)
169 usage_command(cmd, prefix);
174 cmd = &std_commands[i];
175 if (cmd->gc_name == NULL)
178 * If class defines command, which has the same name as
179 * standard command, skip it, because it was already
182 if (find_command(cmd->gc_name, GEOM_CLASS_CMDS) != NULL)
184 usage_command(cmd, prefix);
194 char name1[64], name2[64];
196 snprintf(name1, sizeof(name1), "g_%s", class_name);
197 snprintf(name2, sizeof(name2), "geom_%s", class_name);
198 if (modfind(name1) < 0) {
199 /* Not present in kernel, try loading it. */
200 if (kldload(name2) < 0 || modfind(name1) < 0) {
201 if (errno != EEXIST) {
202 err(EXIT_FAILURE, "cannot load %s", name2);
209 strlcatf(char *str, size_t size, const char *format, ...)
219 va_start(ap, format);
220 ret = vsnprintf(str, size, format, ap);
227 * Find given option in options available for given command.
229 static struct g_option *
230 find_option(struct g_command *cmd, char ch)
232 struct g_option *opt;
236 opt = &cmd->gc_options[i];
237 if (opt->go_name == NULL)
239 if (opt->go_char == ch)
247 * Add given option to gctl_req.
250 set_option(struct gctl_req *req, struct g_option *opt, const char *val)
256 if (G_OPT_ISMULTI(opt)) {
259 if (G_OPT_NUM(opt) == UCHAR_MAX)
260 errx(EXIT_FAILURE, "Too many -%c options.", opt->go_char);
263 * Base option name length plus 3 bytes for option number
264 * (max. 255 options) plus 1 byte for terminating '\0'.
266 optnamesize = strlen(opt->go_name) + 3 + 1;
267 ptr = malloc(optnamesize);
269 errx(EXIT_FAILURE, "No memory.");
270 snprintf(ptr, optnamesize, "%s%u", opt->go_name, G_OPT_NUM(opt));
274 optname = opt->go_name;
277 if (G_OPT_TYPE(opt) == G_TYPE_NUMBER) {
278 if (expand_number(val, &number) == -1) {
279 err(EXIT_FAILURE, "Invalid value for '%c' argument",
282 ptr = malloc(sizeof(intmax_t));
284 errx(EXIT_FAILURE, "No memory.");
285 *(intmax_t *)ptr = number;
287 gctl_ro_param(req, optname, sizeof(intmax_t), opt->go_val);
288 } else if (G_OPT_TYPE(opt) == G_TYPE_STRING) {
289 gctl_ro_param(req, optname, -1, val);
290 } else if (G_OPT_TYPE(opt) == G_TYPE_BOOL) {
291 ptr = malloc(sizeof(int));
293 errx(EXIT_FAILURE, "No memory.");
294 *(int *)ptr = *val - '0';
296 gctl_ro_param(req, optname, sizeof(int), opt->go_val);
298 assert(!"Invalid type");
301 if (G_OPT_ISMULTI(opt))
302 free(__DECONST(char *, optname));
306 * 1. Add given argument by caller.
307 * 2. Add default values of not given arguments.
308 * 3. Add the rest of arguments.
311 parse_arguments(struct g_command *cmd, struct gctl_req *req, int *argc,
314 struct g_option *opt;
320 if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0)
321 strlcat(opts, "v", sizeof(opts));
323 opt = &cmd->gc_options[i];
324 if (opt->go_name == NULL)
326 assert(G_OPT_TYPE(opt) != 0);
327 assert((opt->go_type & ~(G_TYPE_MASK | G_TYPE_MULTI)) == 0);
328 /* Multiple bool arguments makes no sense. */
329 assert(G_OPT_TYPE(opt) != G_TYPE_BOOL ||
330 (opt->go_type & G_TYPE_MULTI) == 0);
331 strlcatf(opts, sizeof(opts), "%c", opt->go_char);
332 if (G_OPT_TYPE(opt) != G_TYPE_BOOL)
333 strlcat(opts, ":", sizeof(opts));
337 * Add specified arguments.
340 while ((ch = getopt(*argc, *argv, opts)) != -1) {
341 /* Standard (not passed to kernel) options. */
342 if (ch == 'v' && (cmd->gc_flags & G_FLAG_VERBOSE) != 0)
344 /* Options passed to kernel. */
345 opt = find_option(cmd, ch);
347 if (ch == 'v' && (cmd->gc_flags & G_FLAG_VERBOSE) != 0){
351 warnx("Option 'v' specified twice.");
355 if (!G_OPT_ISMULTI(opt) && G_OPT_ISDONE(opt)) {
356 warnx("Option '%c' specified twice.", opt->go_char);
361 if (G_OPT_TYPE(opt) == G_TYPE_BOOL)
362 set_option(req, opt, "1");
364 set_option(req, opt, optarg);
370 * Add not specified arguments, but with default values.
373 opt = &cmd->gc_options[i];
374 if (opt->go_name == NULL)
376 if (G_OPT_ISDONE(opt))
379 if (G_OPT_TYPE(opt) == G_TYPE_BOOL) {
380 assert(opt->go_val == NULL);
381 set_option(req, opt, "0");
383 if (opt->go_val == NULL) {
384 warnx("Option '%c' not specified.",
387 } else if (opt->go_val == G_VAL_OPTIONAL) {
390 set_option(req, opt, opt->go_val);
396 * Add rest of given arguments.
398 gctl_ro_param(req, "nargs", sizeof(int), argc);
399 for (i = 0; i < (unsigned)*argc; i++) {
402 snprintf(argname, sizeof(argname), "arg%u", i);
403 gctl_ro_param(req, argname, -1, (*argv)[i]);
408 * Find given command in commands available for given class.
410 static struct g_command *
411 find_command(const char *cmdstr, int flags)
413 struct g_command *cmd;
417 * First try to find command defined by loaded library.
419 if ((flags & GEOM_CLASS_CMDS) != 0 && class_commands != NULL) {
421 cmd = &class_commands[i];
422 if (cmd->gc_name == NULL)
424 if (strcmp(cmd->gc_name, cmdstr) == 0)
429 * Now try to find in standard commands.
431 if ((flags & GEOM_STD_CMDS) != 0) {
433 cmd = &std_commands[i];
434 if (cmd->gc_name == NULL)
436 if (strcmp(cmd->gc_name, cmdstr) == 0)
444 set_flags(struct g_command *cmd)
448 if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0 && verbose)
449 flags |= G_FLAG_VERBOSE;
458 run_command(int argc, char *argv[])
460 struct g_command *cmd;
461 struct gctl_req *req;
465 /* First try to find a command defined by a class. */
466 cmd = find_command(argv[0], GEOM_CLASS_CMDS);
468 /* Now, try to find a standard command. */
469 cmd = find_command(argv[0], GEOM_STD_CMDS);
471 warnx("Unknown command: %s.", argv[0]);
474 if (!std_available(cmd->gc_name)) {
475 warnx("Command '%s' not available; "
476 "try 'load' first.", argv[0]);
480 if ((cmd->gc_flags & G_FLAG_LOADKLD) != 0)
483 req = gctl_get_handle();
484 gctl_ro_param(req, "class", -1, gclass_name);
485 gctl_ro_param(req, "verb", -1, argv[0]);
487 gctl_ro_param(req, "version", sizeof(*version), version);
488 parse_arguments(cmd, req, &argc, &argv);
490 bzero(buf, sizeof(buf));
491 if (cmd->gc_func != NULL) {
494 flags = set_flags(cmd);
495 cmd->gc_func(req, flags);
498 gctl_rw_param(req, "output", sizeof(buf), buf);
499 errstr = gctl_issue(req);
501 if (errstr != NULL && errstr[0] != '\0') {
503 if (strncmp(errstr, "warning: ", strlen("warning: ")) != 0) {
516 #ifndef STATIC_GEOM_CLASSES
522 path = getenv("GEOM_LIBRARY_PATH");
524 path = GEOM_CLASS_DIR;
531 char *curpath, path[MAXPATHLEN], *tofree, *totalpath;
532 uint32_t *lib_version;
537 tofree = totalpath = strdup(library_path());
538 if (totalpath == NULL)
539 err(EXIT_FAILURE, "Not enough memory for library path");
541 if (strchr(totalpath, ':') != NULL)
542 curpath = strsep(&totalpath, ":");
545 /* Traverse the paths to find one that contains the library we want. */
546 while (curpath != NULL) {
547 snprintf(path, sizeof(path), "%s/geom_%s.so", curpath,
549 ret = access(path, F_OK);
551 if (errno == ENOENT) {
553 * If we cannot find library, try the next
556 curpath = strsep(&totalpath, ":");
559 err(EXIT_FAILURE, "Cannot access library");
564 /* No library was found, but standard commands can still be used */
567 dlh = dlopen(path, RTLD_NOW);
569 errx(EXIT_FAILURE, "Cannot open library: %s.", dlerror());
570 lib_version = dlsym(dlh, "lib_version");
571 if (lib_version == NULL) {
572 warnx("Cannot find symbol %s: %s.", "lib_version", dlerror());
576 if (*lib_version != G_LIB_VERSION) {
578 errx(EXIT_FAILURE, "%s and %s are not synchronized.",
579 getprogname(), path);
581 version = dlsym(dlh, "version");
582 if (version == NULL) {
583 warnx("Cannot find symbol %s: %s.", "version", dlerror());
587 class_commands = dlsym(dlh, "class_commands");
588 if (class_commands == NULL) {
589 warnx("Cannot find symbol %s: %s.", "class_commands",
595 #endif /* !STATIC_GEOM_CLASSES */
598 * Class name should be all capital letters.
606 for (; *s1 != '\0'; s1++)
608 gclass_name = malloc(strlen(class_name) + 1);
609 if (gclass_name == NULL)
610 errx(EXIT_FAILURE, "No memory");
613 for (; *s2 != '\0'; s2++)
614 *s1++ = toupper(*s2);
619 get_class(int *argc, char ***argv)
622 snprintf(comm, sizeof(comm), "%s", basename((*argv)[0]));
623 if (strcmp(comm, "geom") == 0) {
626 else if (*argc == 2) {
627 if (strcmp((*argv)[1], "-h") == 0 ||
628 strcmp((*argv)[1], "help") == 0) {
632 strlcatf(comm, sizeof(comm), " %s", (*argv)[1]);
633 class_name = (*argv)[1];
636 } else if (*comm == 'g') {
637 class_name = comm + 1;
641 errx(EXIT_FAILURE, "Invalid utility name.");
644 #ifndef STATIC_GEOM_CLASSES
647 if (!strcasecmp(class_name, "part")) {
648 version = &gpart_version;
649 class_commands = gpart_class_commands;
650 } else if (!strcasecmp(class_name, "label")) {
651 version = &glabel_version;
652 class_commands = glabel_class_commands;
654 #endif /* !STATIC_GEOM_CLASSES */
658 /* If we can't load or list, it's not a class. */
659 if (!std_available("load") && !std_available("list"))
660 errx(EXIT_FAILURE, "Invalid class name '%s'.", class_name);
666 static struct ggeom *
667 find_geom_by_provider(struct gmesh *mesh, const char *name)
669 struct gclass *classp;
671 struct gprovider *pp;
673 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
674 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
675 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
676 if (strcmp(pp->lg_name, name) == 0)
686 compute_tree_width_geom(struct gmesh *mesh, struct ggeom *gp, int indent)
688 struct gclass *classp2;
690 struct gconsumer *cp2;
691 struct gprovider *pp;
692 int max_width, width;
694 max_width = width = indent + strlen(gp->lg_name);
696 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
697 LIST_FOREACH(classp2, &mesh->lg_class, lg_class) {
698 LIST_FOREACH(gp2, &classp2->lg_geom, lg_geom) {
700 &gp2->lg_consumer, lg_consumer) {
701 if (pp != cp2->lg_provider)
703 width = compute_tree_width_geom(mesh,
705 if (width > max_width)
716 compute_tree_width(struct gmesh *mesh)
718 struct gclass *classp;
720 int max_width, width;
722 max_width = width = 0;
724 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
725 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
726 if (!LIST_EMPTY(&gp->lg_consumer))
728 width = compute_tree_width_geom(mesh, gp, 0);
729 if (width > max_width)
738 show_tree_geom(struct gmesh *mesh, struct ggeom *gp, int indent, int width)
740 struct gclass *classp2;
742 struct gconsumer *cp2;
743 struct gprovider *pp;
745 if (LIST_EMPTY(&gp->lg_provider)) {
746 printf("%*s%-*.*s %-*.*s\n", indent, "",
747 width - indent, width - indent, gp->lg_name,
748 GEOM_CLASS_WIDTH, GEOM_CLASS_WIDTH, gp->lg_class->lg_name);
752 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
753 printf("%*s%-*.*s %-*.*s %s\n", indent, "",
754 width - indent, width - indent, gp->lg_name,
755 GEOM_CLASS_WIDTH, GEOM_CLASS_WIDTH, gp->lg_class->lg_name,
758 LIST_FOREACH(classp2, &mesh->lg_class, lg_class) {
759 LIST_FOREACH(gp2, &classp2->lg_geom, lg_geom) {
761 &gp2->lg_consumer, lg_consumer) {
762 if (pp != cp2->lg_provider)
764 show_tree_geom(mesh, gp2,
776 struct gclass *classp;
780 error = geom_gettree(&mesh);
782 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
784 width = compute_tree_width(&mesh);
786 printf("%-*.*s %-*.*s %s\n",
787 width, width, "Geom",
788 GEOM_CLASS_WIDTH, GEOM_CLASS_WIDTH, "Class",
791 LIST_FOREACH(classp, &mesh.lg_class, lg_class) {
792 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
793 if (!LIST_EMPTY(&gp->lg_consumer))
795 show_tree_geom(&mesh, gp, 0, width);
801 main(int argc, char *argv[])
807 provider_name = NULL;
810 if (strcmp(getprogname(), "geom") == 0) {
811 while ((ch = getopt(argc, argv, "hp:t")) != -1) {
814 provider_name = strdup(optarg);
815 if (provider_name == NULL)
828 * Don't adjust argc and argv, it would break get_class().
832 if (tflag && provider_name != NULL) {
834 "At most one of -P and -t may be specified.");
837 if (provider_name != NULL) {
838 list_one_geom_by_provider(provider_name);
847 get_class(&argc, &argv);
848 run_command(argc, argv);
854 static struct gclass *
855 find_class(struct gmesh *mesh, const char *name)
857 struct gclass *classp;
859 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
860 if (strcmp(classp->lg_name, name) == 0)
866 static struct ggeom *
867 find_geom(struct gclass *classp, const char *name)
871 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
872 name += sizeof(_PATH_DEV) - 1;
874 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
875 if (strcmp(gp->lg_name, name) == 0)
882 list_one_provider(struct gprovider *pp, const char *prefix)
884 struct gconfig *conf;
887 printf("Name: %s\n", pp->lg_name);
888 humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "",
889 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
890 printf("%sMediasize: %jd (%s)\n", prefix, (intmax_t)pp->lg_mediasize,
892 printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize);
893 if (pp->lg_stripesize > 0 || pp->lg_stripeoffset > 0) {
894 printf("%sStripesize: %ju\n", prefix, pp->lg_stripesize);
895 printf("%sStripeoffset: %ju\n", prefix, pp->lg_stripeoffset);
897 printf("%sMode: %s\n", prefix, pp->lg_mode);
898 LIST_FOREACH(conf, &pp->lg_config, lg_config) {
899 printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val);
904 list_one_consumer(struct gconsumer *cp, const char *prefix)
906 struct gprovider *pp;
907 struct gconfig *conf;
909 pp = cp->lg_provider;
911 printf("[no provider]\n");
915 printf("Name: %s\n", pp->lg_name);
916 humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "",
917 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
918 printf("%sMediasize: %jd (%s)\n", prefix,
919 (intmax_t)pp->lg_mediasize, buf);
920 printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize);
921 if (pp->lg_stripesize > 0 || pp->lg_stripeoffset > 0) {
922 printf("%sStripesize: %ju\n", prefix, pp->lg_stripesize);
923 printf("%sStripeoffset: %ju\n", prefix, pp->lg_stripeoffset);
925 printf("%sMode: %s\n", prefix, cp->lg_mode);
927 LIST_FOREACH(conf, &cp->lg_config, lg_config) {
928 printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val);
933 list_one_geom(struct ggeom *gp)
935 struct gprovider *pp;
936 struct gconsumer *cp;
937 struct gconfig *conf;
940 printf("Geom name: %s\n", gp->lg_name);
941 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
942 printf("%s: %s\n", conf->lg_name, conf->lg_val);
944 if (!LIST_EMPTY(&gp->lg_provider)) {
945 printf("Providers:\n");
947 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
949 list_one_provider(pp, " ");
952 if (!LIST_EMPTY(&gp->lg_consumer)) {
953 printf("Consumers:\n");
955 LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
957 list_one_consumer(cp, " ");
964 list_one_geom_by_provider(const char *provider_name)
970 error = geom_gettree(&mesh);
972 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
974 gp = find_geom_by_provider(&mesh, provider_name);
976 errx(EXIT_FAILURE, "Cannot find provider '%s'.", provider_name);
978 printf("Geom class: %s\n", gp->lg_class->lg_name);
983 std_help(struct gctl_req *req __unused, unsigned flags __unused)
990 std_list_available(void)
993 struct gclass *classp;
996 error = geom_gettree(&mesh);
998 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
999 classp = find_class(&mesh, gclass_name);
1000 geom_deletetree(&mesh);
1007 std_list(struct gctl_req *req, unsigned flags __unused)
1010 struct gclass *classp;
1013 int all, error, i, nargs;
1015 error = geom_gettree(&mesh);
1017 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
1018 classp = find_class(&mesh, gclass_name);
1019 if (classp == NULL) {
1020 geom_deletetree(&mesh);
1021 errx(EXIT_FAILURE, "Class '%s' not found.", gclass_name);
1023 nargs = gctl_get_int(req, "nargs");
1024 all = gctl_get_int(req, "all");
1026 for (i = 0; i < nargs; i++) {
1027 name = gctl_get_ascii(req, "arg%d", i);
1028 gp = find_geom(classp, name);
1030 errx(EXIT_FAILURE, "Class '%s' does not have "
1031 "an instance named '%s'.",
1037 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
1038 if (LIST_EMPTY(&gp->lg_provider) && !all)
1043 geom_deletetree(&mesh);
1047 std_status_available(void)
1050 /* 'status' command is available when 'list' command is. */
1051 return (std_list_available());
1055 status_update_len(struct ggeom *gp, int *name_len, int *status_len)
1057 struct gconfig *conf;
1061 assert(name_len != NULL);
1062 assert(status_len != NULL);
1064 len = strlen(gp->lg_name);
1065 if (*name_len < len)
1067 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
1068 if (strcasecmp(conf->lg_name, "state") == 0) {
1069 len = strlen(conf->lg_val);
1070 if (*status_len < len)
1077 status_update_len_prs(struct ggeom *gp, int *name_len, int *status_len)
1079 struct gprovider *pp;
1080 struct gconfig *conf;
1084 assert(name_len != NULL);
1085 assert(status_len != NULL);
1088 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
1089 if (strcasecmp(conf->lg_name, "state") == 0) {
1090 glen = strlen(conf->lg_val);
1094 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
1095 len = strlen(pp->lg_name);
1096 if (*name_len < len)
1099 LIST_FOREACH(conf, &pp->lg_config, lg_config) {
1100 if (strcasecmp(conf->lg_name, "state") == 0) {
1101 len = strlen(conf->lg_val);
1105 if (*status_len < len)
1111 status_one_consumer(struct gconsumer *cp)
1113 static char buf[256];
1114 struct gprovider *pp;
1115 struct gconfig *conf;
1116 const char *state, *syncr;
1118 pp = cp->lg_provider;
1123 LIST_FOREACH(conf, &cp->lg_config, lg_config) {
1124 if (strcasecmp(conf->lg_name, "state") == 0)
1125 state = conf->lg_val;
1126 if (strcasecmp(conf->lg_name, "synchronized") == 0)
1127 syncr = conf->lg_val;
1129 if (state == NULL && syncr == NULL)
1130 snprintf(buf, sizeof(buf), "%s", pp->lg_name);
1131 else if (state != NULL && syncr != NULL) {
1132 snprintf(buf, sizeof(buf), "%s (%s, %s)", pp->lg_name,
1135 snprintf(buf, sizeof(buf), "%s (%s)", pp->lg_name,
1136 state ? state : syncr);
1142 status_one_geom(struct ggeom *gp, int script, int name_len, int status_len)
1144 struct gconsumer *cp;
1145 struct gconfig *conf;
1146 const char *name, *status, *component;
1151 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
1152 if (strcasecmp(conf->lg_name, "state") == 0) {
1153 status = conf->lg_val;
1158 LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
1159 component = status_one_consumer(cp);
1160 if (component == NULL)
1163 printf("%*s %*s %s\n", name_len, name, status_len, status,
1169 printf("%*s %*s %s\n", name_len, name, status_len, status,
1175 status_one_geom_prs(struct ggeom *gp, int script, int name_len, int status_len)
1177 struct gprovider *pp;
1178 struct gconsumer *cp;
1179 struct gconfig *conf;
1180 const char *name, *status, *component;
1183 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
1186 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
1187 if (strcasecmp(conf->lg_name, "state") == 0) {
1188 status = conf->lg_val;
1192 LIST_FOREACH(conf, &pp->lg_config, lg_config) {
1193 if (strcasecmp(conf->lg_name, "state") == 0) {
1194 status = conf->lg_val;
1199 LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
1200 component = status_one_consumer(cp);
1201 if (component == NULL)
1204 printf("%*s %*s %s\n", name_len, name,
1205 status_len, status, component);
1210 printf("%*s %*s %s\n", name_len, name,
1211 status_len, status, "N/A");
1217 std_status(struct gctl_req *req, unsigned flags __unused)
1220 struct gclass *classp;
1223 int name_len, status_len;
1224 int all, error, geoms, i, n, nargs, script;
1226 error = geom_gettree(&mesh);
1228 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
1229 classp = find_class(&mesh, gclass_name);
1231 errx(EXIT_FAILURE, "Class %s not found.", gclass_name);
1232 nargs = gctl_get_int(req, "nargs");
1233 all = gctl_get_int(req, "all");
1234 geoms = gctl_get_int(req, "geoms");
1235 script = gctl_get_int(req, "script");
1240 name_len = strlen("Name");
1241 status_len = strlen("Status");
1244 for (i = 0, n = 0; i < nargs; i++) {
1245 name = gctl_get_ascii(req, "arg%d", i);
1246 gp = find_geom(classp, name);
1248 errx(EXIT_FAILURE, "No such geom: %s.", name);
1250 status_update_len(gp,
1251 &name_len, &status_len);
1253 status_update_len_prs(gp,
1254 &name_len, &status_len);
1262 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
1263 if (LIST_EMPTY(&gp->lg_provider) && !all)
1266 status_update_len(gp,
1267 &name_len, &status_len);
1269 status_update_len_prs(gp,
1270 &name_len, &status_len);
1278 printf("%*s %*s %s\n", name_len, "Name", status_len, "Status",
1282 for (i = 0; i < nargs; i++) {
1283 name = gctl_get_ascii(req, "arg%d", i);
1284 gp = find_geom(classp, name);
1288 status_one_geom(gp, script, name_len,
1291 status_one_geom_prs(gp, script, name_len,
1296 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
1297 if (LIST_EMPTY(&gp->lg_provider) && !all)
1300 status_one_geom(gp, script, name_len,
1303 status_one_geom_prs(gp, script, name_len,
1309 geom_deletetree(&mesh);
1313 std_load_available(void)
1315 char name[MAXPATHLEN], paths[MAXPATHLEN * 8], *p;
1319 snprintf(name, sizeof(name), "g_%s", class_name);
1321 * If already in kernel, "load" command is not available.
1323 if (modfind(name) >= 0)
1325 bzero(paths, sizeof(paths));
1326 len = sizeof(paths);
1327 if (sysctlbyname("kern.module_path", paths, &len, NULL, 0) < 0)
1328 err(EXIT_FAILURE, "sysctl(kern.module_path)");
1329 for (p = strtok(paths, ";"); p != NULL; p = strtok(NULL, ";")) {
1330 snprintf(name, sizeof(name), "%s/geom_%s.ko", p, class_name);
1332 * If geom_<name>.ko file exists, "load" command is available.
1334 if (stat(name, &sb) == 0)
1341 std_load(struct gctl_req *req __unused, unsigned flags)
1345 * Do nothing special here, because of G_FLAG_LOADKLD flag,
1346 * module is already loaded.
1348 if ((flags & G_FLAG_VERBOSE) != 0)
1349 printf("Module available.\n");
1353 std_unload_available(void)
1358 snprintf(name, sizeof(name), "geom_%s", class_name);
1366 std_unload(struct gctl_req *req, unsigned flags __unused)
1371 snprintf(name, sizeof(name), "geom_%s", class_name);
1374 gctl_error(req, "Could not find module: %s.", strerror(errno));
1377 if (kldunload(id) < 0) {
1378 gctl_error(req, "Could not unload module: %s.",
1385 std_available(const char *name)
1388 if (strcmp(name, "help") == 0)
1390 else if (strcmp(name, "list") == 0)
1391 return (std_list_available());
1392 else if (strcmp(name, "status") == 0)
1393 return (std_status_available());
1394 else if (strcmp(name, "load") == 0)
1395 return (std_load_available());
1396 else if (strcmp(name, "unload") == 0)
1397 return (std_unload_available());
1399 assert(!"Unknown standard command.");