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>
54 #include "misc/subr.h"
56 #ifdef STATIC_GEOM_CLASSES
57 extern uint32_t gpart_version;
58 extern struct g_command gpart_class_commands[];
59 extern uint32_t glabel_version;
60 extern struct g_command glabel_class_commands[];
63 static char comm[MAXPATHLEN], *class_name = NULL, *gclass_name = NULL;
64 static uint32_t *version = NULL;
65 static int verbose = 0;
66 static struct g_command *class_commands = NULL;
68 #define GEOM_CLASS_CMDS 0x01
69 #define GEOM_STD_CMDS 0x02
70 static struct g_command *find_command(const char *cmdstr, int flags);
71 static int std_available(const char *name);
73 static void std_help(struct gctl_req *req, unsigned flags);
74 static void std_list(struct gctl_req *req, unsigned flags);
75 static void std_status(struct gctl_req *req, unsigned flags);
76 static void std_load(struct gctl_req *req, unsigned flags);
77 static void std_unload(struct gctl_req *req, unsigned flags);
79 static struct g_command std_commands[] = {
80 { "help", 0, std_help, G_NULL_OPTS, NULL },
81 { "list", 0, std_list,
83 { 'a', "all", NULL, G_TYPE_BOOL },
88 { "status", 0, std_status,
90 { 'a', "all", NULL, G_TYPE_BOOL },
91 { 'g', "geoms", NULL, G_TYPE_BOOL },
92 { 's', "script", NULL, G_TYPE_BOOL },
97 { "load", G_FLAG_VERBOSE | G_FLAG_LOADKLD, std_load, G_NULL_OPTS,
99 { "unload", G_FLAG_VERBOSE, std_unload, G_NULL_OPTS, NULL },
104 usage_command(struct g_command *cmd, const char *prefix)
106 struct g_option *opt;
109 if (cmd->gc_usage != NULL) {
110 char *pos, *ptr, *sptr;
112 sptr = ptr = strdup(cmd->gc_usage);
113 while ((pos = strsep(&ptr, "\n")) != NULL) {
116 fprintf(stderr, "%s %s %s %s\n", prefix, comm,
123 fprintf(stderr, "%s %s %s", prefix, comm, cmd->gc_name);
124 if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0)
125 fprintf(stderr, " [-v]");
127 opt = &cmd->gc_options[i];
128 if (opt->go_name == NULL)
130 if (opt->go_val != NULL || G_OPT_TYPE(opt) == G_TYPE_BOOL)
131 fprintf(stderr, " [");
133 fprintf(stderr, " ");
134 fprintf(stderr, "-%c", opt->go_char);
135 if (G_OPT_TYPE(opt) != G_TYPE_BOOL)
136 fprintf(stderr, " %s", opt->go_name);
137 if (opt->go_val != NULL || G_OPT_TYPE(opt) == G_TYPE_BOOL)
138 fprintf(stderr, "]");
140 fprintf(stderr, "\n");
147 if (class_name == NULL) {
148 errx(EXIT_FAILURE, "usage: %s <class> <command> [options]",
151 struct g_command *cmd;
156 if (class_commands != NULL) {
158 cmd = &class_commands[i];
159 if (cmd->gc_name == NULL)
161 usage_command(cmd, prefix);
166 cmd = &std_commands[i];
167 if (cmd->gc_name == NULL)
170 * If class defines command, which has the same name as
171 * standard command, skip it, because it was already
174 if (find_command(cmd->gc_name, GEOM_CLASS_CMDS) != NULL)
176 usage_command(cmd, prefix);
186 char name1[64], name2[64];
188 snprintf(name1, sizeof(name1), "g_%s", class_name);
189 snprintf(name2, sizeof(name2), "geom_%s", class_name);
190 if (modfind(name1) < 0) {
191 /* Not present in kernel, try loading it. */
192 if (kldload(name2) < 0 || modfind(name1) < 0) {
193 if (errno != EEXIST) {
195 "%s module not available!", name2);
202 strlcatf(char *str, size_t size, const char *format, ...)
212 va_start(ap, format);
213 ret = vsnprintf(str, size, format, ap);
220 * Find given option in options available for given command.
222 static struct g_option *
223 find_option(struct g_command *cmd, char ch)
225 struct g_option *opt;
229 opt = &cmd->gc_options[i];
230 if (opt->go_name == NULL)
232 if (opt->go_char == ch)
240 * Add given option to gctl_req.
243 set_option(struct gctl_req *req, struct g_option *opt, const char *val)
249 if (G_OPT_ISMULTI(opt)) {
252 if (G_OPT_NUM(opt) == UCHAR_MAX)
253 errx(EXIT_FAILURE, "Too many -%c options.", opt->go_char);
256 * Base option name length plus 3 bytes for option number
257 * (max. 255 options) plus 1 byte for terminating '\0'.
259 optnamesize = strlen(opt->go_name) + 3 + 1;
260 ptr = malloc(optnamesize);
262 errx(EXIT_FAILURE, "No memory.");
263 snprintf(ptr, optnamesize, "%s%u", opt->go_name, G_OPT_NUM(opt));
267 optname = opt->go_name;
270 if (G_OPT_TYPE(opt) == G_TYPE_NUMBER) {
271 if (expand_number(val, &number) == -1) {
272 err(EXIT_FAILURE, "Invalid value for '%c' argument",
275 ptr = malloc(sizeof(intmax_t));
277 errx(EXIT_FAILURE, "No memory.");
278 *(intmax_t *)ptr = number;
280 gctl_ro_param(req, optname, sizeof(intmax_t), opt->go_val);
281 } else if (G_OPT_TYPE(opt) == G_TYPE_STRING) {
282 gctl_ro_param(req, optname, -1, val);
283 } else if (G_OPT_TYPE(opt) == G_TYPE_BOOL) {
284 ptr = malloc(sizeof(int));
286 errx(EXIT_FAILURE, "No memory.");
287 *(int *)ptr = *val - '0';
289 gctl_ro_param(req, optname, sizeof(int), opt->go_val);
291 assert(!"Invalid type");
294 if (G_OPT_ISMULTI(opt))
295 free(__DECONST(char *, optname));
299 * 1. Add given argument by caller.
300 * 2. Add default values of not given arguments.
301 * 3. Add the rest of arguments.
304 parse_arguments(struct g_command *cmd, struct gctl_req *req, int *argc,
307 struct g_option *opt;
313 if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0)
314 strlcat(opts, "v", sizeof(opts));
316 opt = &cmd->gc_options[i];
317 if (opt->go_name == NULL)
319 assert(G_OPT_TYPE(opt) != 0);
320 assert((opt->go_type & ~(G_TYPE_MASK | G_TYPE_MULTI)) == 0);
321 /* Multiple bool arguments makes no sense. */
322 assert(G_OPT_TYPE(opt) != G_TYPE_BOOL ||
323 (opt->go_type & G_TYPE_MULTI) == 0);
324 strlcatf(opts, sizeof(opts), "%c", opt->go_char);
325 if (G_OPT_TYPE(opt) != G_TYPE_BOOL)
326 strlcat(opts, ":", sizeof(opts));
330 * Add specified arguments.
332 while ((ch = getopt(*argc, *argv, opts)) != -1) {
333 /* Standard (not passed to kernel) options. */
339 /* Options passed to kernel. */
340 opt = find_option(cmd, ch);
343 if (!G_OPT_ISMULTI(opt) && G_OPT_ISDONE(opt)) {
344 warnx("Option '%c' specified twice.", opt->go_char);
349 if (G_OPT_TYPE(opt) == G_TYPE_BOOL)
350 set_option(req, opt, "1");
352 set_option(req, opt, optarg);
358 * Add not specified arguments, but with default values.
361 opt = &cmd->gc_options[i];
362 if (opt->go_name == NULL)
364 if (G_OPT_ISDONE(opt))
367 if (G_OPT_TYPE(opt) == G_TYPE_BOOL) {
368 assert(opt->go_val == NULL);
369 set_option(req, opt, "0");
371 if (opt->go_val == NULL) {
372 warnx("Option '%c' not specified.",
375 } else if (opt->go_val == G_VAL_OPTIONAL) {
378 set_option(req, opt, opt->go_val);
384 * Add rest of given arguments.
386 gctl_ro_param(req, "nargs", sizeof(int), argc);
387 for (i = 0; i < (unsigned)*argc; i++) {
390 snprintf(argname, sizeof(argname), "arg%u", i);
391 gctl_ro_param(req, argname, -1, (*argv)[i]);
396 * Find given command in commands available for given class.
398 static struct g_command *
399 find_command(const char *cmdstr, int flags)
401 struct g_command *cmd;
405 * First try to find command defined by loaded library.
407 if ((flags & GEOM_CLASS_CMDS) != 0 && class_commands != NULL) {
409 cmd = &class_commands[i];
410 if (cmd->gc_name == NULL)
412 if (strcmp(cmd->gc_name, cmdstr) == 0)
417 * Now try to find in standard commands.
419 if ((flags & GEOM_STD_CMDS) != 0) {
421 cmd = &std_commands[i];
422 if (cmd->gc_name == NULL)
424 if (strcmp(cmd->gc_name, cmdstr) == 0)
432 set_flags(struct g_command *cmd)
436 if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0 && verbose)
437 flags |= G_FLAG_VERBOSE;
446 run_command(int argc, char *argv[])
448 struct g_command *cmd;
449 struct gctl_req *req;
453 /* First try to find a command defined by a class. */
454 cmd = find_command(argv[0], GEOM_CLASS_CMDS);
456 /* Now, try to find a standard command. */
457 cmd = find_command(argv[0], GEOM_STD_CMDS);
459 warnx("Unknown command: %s.", argv[0]);
462 if (!std_available(cmd->gc_name)) {
463 warnx("Command '%s' not available.", argv[0]);
467 if ((cmd->gc_flags & G_FLAG_LOADKLD) != 0)
470 req = gctl_get_handle();
471 gctl_ro_param(req, "class", -1, gclass_name);
472 gctl_ro_param(req, "verb", -1, argv[0]);
474 gctl_ro_param(req, "version", sizeof(*version), version);
475 parse_arguments(cmd, req, &argc, &argv);
477 bzero(buf, sizeof(buf));
478 if (cmd->gc_func != NULL) {
481 flags = set_flags(cmd);
482 cmd->gc_func(req, flags);
485 gctl_rw_param(req, "output", sizeof(buf), buf);
486 errstr = gctl_issue(req);
488 if (errstr != NULL && errstr[0] != '\0') {
490 if (strncmp(errstr, "warning: ", strlen("warning: ")) != 0) {
503 #ifndef STATIC_GEOM_CLASSES
509 path = getenv("GEOM_LIBRARY_PATH");
511 path = GEOM_CLASS_DIR;
518 char *curpath, path[MAXPATHLEN], *tofree, *totalpath;
519 uint32_t *lib_version;
524 tofree = totalpath = strdup(library_path());
525 if (totalpath == NULL)
526 err(EXIT_FAILURE, "Not enough memory for library path");
528 if (strchr(totalpath, ':') != NULL)
529 curpath = strsep(&totalpath, ":");
532 /* Traverse the paths to find one that contains the library we want. */
533 while (curpath != NULL) {
534 snprintf(path, sizeof(path), "%s/geom_%s.so", curpath,
536 ret = access(path, F_OK);
538 if (errno == ENOENT) {
540 * If we cannot find library, try the next
543 curpath = strsep(&totalpath, ":");
546 err(EXIT_FAILURE, "Cannot access library");
551 /* No library was found, but standard commands can still be used */
554 dlh = dlopen(path, RTLD_NOW);
556 errx(EXIT_FAILURE, "Cannot open library: %s.", dlerror());
557 lib_version = dlsym(dlh, "lib_version");
558 if (lib_version == NULL) {
559 warnx("Cannot find symbol %s: %s.", "lib_version", dlerror());
563 if (*lib_version != G_LIB_VERSION) {
565 errx(EXIT_FAILURE, "%s and %s are not synchronized.",
566 getprogname(), path);
568 version = dlsym(dlh, "version");
569 if (version == NULL) {
570 warnx("Cannot find symbol %s: %s.", "version", dlerror());
574 class_commands = dlsym(dlh, "class_commands");
575 if (class_commands == NULL) {
576 warnx("Cannot find symbol %s: %s.", "class_commands",
582 #endif /* !STATIC_GEOM_CLASSES */
585 * Class name should be all capital letters.
593 for (; *s1 != '\0'; s1++)
595 gclass_name = malloc(strlen(class_name) + 1);
596 if (gclass_name == NULL)
597 errx(EXIT_FAILURE, "No memory");
600 for (; *s2 != '\0'; s2++)
601 *s1++ = toupper(*s2);
606 get_class(int *argc, char ***argv)
609 snprintf(comm, sizeof(comm), "%s", basename((*argv)[0]));
610 if (strcmp(comm, "geom") == 0) {
613 else if (*argc == 2) {
614 if (strcmp((*argv)[1], "-h") == 0 ||
615 strcmp((*argv)[1], "help") == 0) {
619 strlcatf(comm, sizeof(comm), " %s", (*argv)[1]);
620 class_name = (*argv)[1];
623 } else if (*comm == 'g') {
624 class_name = comm + 1;
628 errx(EXIT_FAILURE, "Invalid utility name.");
631 #ifndef STATIC_GEOM_CLASSES
634 if (!strcasecmp(class_name, "part")) {
635 version = &gpart_version;
636 class_commands = gpart_class_commands;
637 } else if (!strcasecmp(class_name, "label")) {
638 version = &glabel_version;
639 class_commands = glabel_class_commands;
641 #endif /* !STATIC_GEOM_CLASSES */
645 /* If we can't load or list, it's not a class. */
646 if (!std_available("load") && !std_available("list"))
647 errx(EXIT_FAILURE, "Invalid class name.");
654 main(int argc, char *argv[])
657 get_class(&argc, &argv);
658 run_command(argc, argv);
664 static struct gclass *
665 find_class(struct gmesh *mesh, const char *name)
667 struct gclass *classp;
669 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
670 if (strcmp(classp->lg_name, name) == 0)
676 static struct ggeom *
677 find_geom(struct gclass *classp, const char *name)
681 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
682 if (strcmp(gp->lg_name, name) == 0)
689 list_one_provider(struct gprovider *pp, const char *prefix)
691 struct gconfig *conf;
694 printf("Name: %s\n", pp->lg_name);
695 humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "",
696 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
697 printf("%sMediasize: %jd (%s)\n", prefix, (intmax_t)pp->lg_mediasize,
699 printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize);
700 if (pp->lg_stripesize > 0 || pp->lg_stripeoffset > 0) {
701 printf("%sStripesize: %ju\n", prefix, pp->lg_stripesize);
702 printf("%sStripeoffset: %ju\n", prefix, pp->lg_stripeoffset);
704 printf("%sMode: %s\n", prefix, pp->lg_mode);
705 LIST_FOREACH(conf, &pp->lg_config, lg_config) {
706 printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val);
711 list_one_consumer(struct gconsumer *cp, const char *prefix)
713 struct gprovider *pp;
714 struct gconfig *conf;
716 pp = cp->lg_provider;
718 printf("[no provider]\n");
722 printf("Name: %s\n", pp->lg_name);
723 humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "",
724 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
725 printf("%sMediasize: %jd (%s)\n", prefix,
726 (intmax_t)pp->lg_mediasize, buf);
727 printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize);
728 if (pp->lg_stripesize > 0 || pp->lg_stripeoffset > 0) {
729 printf("%sStripesize: %ju\n", prefix, pp->lg_stripesize);
730 printf("%sStripeoffset: %ju\n", prefix, pp->lg_stripeoffset);
732 printf("%sMode: %s\n", prefix, cp->lg_mode);
734 LIST_FOREACH(conf, &cp->lg_config, lg_config) {
735 printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val);
740 list_one_geom(struct ggeom *gp)
742 struct gprovider *pp;
743 struct gconsumer *cp;
744 struct gconfig *conf;
747 printf("Geom name: %s\n", gp->lg_name);
748 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
749 printf("%s: %s\n", conf->lg_name, conf->lg_val);
751 if (!LIST_EMPTY(&gp->lg_provider)) {
752 printf("Providers:\n");
754 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
756 list_one_provider(pp, " ");
759 if (!LIST_EMPTY(&gp->lg_consumer)) {
760 printf("Consumers:\n");
762 LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
764 list_one_consumer(cp, " ");
771 std_help(struct gctl_req *req __unused, unsigned flags __unused)
778 std_list_available(void)
781 struct gclass *classp;
784 error = geom_gettree(&mesh);
786 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
787 classp = find_class(&mesh, gclass_name);
788 geom_deletetree(&mesh);
795 std_list(struct gctl_req *req, unsigned flags __unused)
798 struct gclass *classp;
801 int all, error, i, nargs;
803 error = geom_gettree(&mesh);
805 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
806 classp = find_class(&mesh, gclass_name);
807 if (classp == NULL) {
808 geom_deletetree(&mesh);
809 errx(EXIT_FAILURE, "Class %s not found.", gclass_name);
811 nargs = gctl_get_int(req, "nargs");
812 all = gctl_get_int(req, "all");
814 for (i = 0; i < nargs; i++) {
815 name = gctl_get_ascii(req, "arg%d", i);
816 gp = find_geom(classp, name);
818 errx(EXIT_FAILURE, "No such geom: %s.", name);
822 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
823 if (LIST_EMPTY(&gp->lg_provider) && !all)
828 geom_deletetree(&mesh);
832 std_status_available(void)
835 /* 'status' command is available when 'list' command is. */
836 return (std_list_available());
840 status_update_len(struct ggeom *gp, int *name_len, int *status_len)
842 struct gconfig *conf;
846 assert(name_len != NULL);
847 assert(status_len != NULL);
849 len = strlen(gp->lg_name);
852 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
853 if (strcasecmp(conf->lg_name, "state") == 0) {
854 len = strlen(conf->lg_val);
855 if (*status_len < len)
862 status_update_len_prs(struct ggeom *gp, int *name_len, int *status_len)
864 struct gprovider *pp;
865 struct gconfig *conf;
869 assert(name_len != NULL);
870 assert(status_len != NULL);
873 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
874 if (strcasecmp(conf->lg_name, "state") == 0) {
875 glen = strlen(conf->lg_val);
879 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
880 len = strlen(pp->lg_name);
884 LIST_FOREACH(conf, &pp->lg_config, lg_config) {
885 if (strcasecmp(conf->lg_name, "state") == 0) {
886 len = strlen(conf->lg_val);
890 if (*status_len < len)
896 status_one_consumer(struct gconsumer *cp)
898 static char buf[256];
899 struct gprovider *pp;
900 struct gconfig *conf;
901 const char *state, *syncr;
903 pp = cp->lg_provider;
908 LIST_FOREACH(conf, &cp->lg_config, lg_config) {
909 if (strcasecmp(conf->lg_name, "state") == 0)
910 state = conf->lg_val;
911 if (strcasecmp(conf->lg_name, "synchronized") == 0)
912 syncr = conf->lg_val;
914 if (state == NULL && syncr == NULL)
915 snprintf(buf, sizeof(buf), "%s", pp->lg_name);
916 else if (state != NULL && syncr != NULL) {
917 snprintf(buf, sizeof(buf), "%s (%s, %s)", pp->lg_name,
920 snprintf(buf, sizeof(buf), "%s (%s)", pp->lg_name,
921 state ? state : syncr);
927 status_one_geom(struct ggeom *gp, int script, int name_len, int status_len)
929 struct gconsumer *cp;
930 struct gconfig *conf;
931 const char *name, *status, *component;
936 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
937 if (strcasecmp(conf->lg_name, "state") == 0) {
938 status = conf->lg_val;
943 LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
944 component = status_one_consumer(cp);
945 if (component == NULL)
948 printf("%*s %*s %s\n", name_len, name, status_len, status,
954 printf("%*s %*s %s\n", name_len, name, status_len, status,
960 status_one_geom_prs(struct ggeom *gp, int script, int name_len, int status_len)
962 struct gprovider *pp;
963 struct gconsumer *cp;
964 struct gconfig *conf;
965 const char *name, *status, *component;
968 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
971 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
972 if (strcasecmp(conf->lg_name, "state") == 0) {
973 status = conf->lg_val;
977 LIST_FOREACH(conf, &pp->lg_config, lg_config) {
978 if (strcasecmp(conf->lg_name, "state") == 0) {
979 status = conf->lg_val;
984 LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
985 component = status_one_consumer(cp);
986 if (component == NULL)
989 printf("%*s %*s %s\n", name_len, name,
990 status_len, status, component);
995 printf("%*s %*s %s\n", name_len, name,
996 status_len, status, "N/A");
1002 std_status(struct gctl_req *req, unsigned flags __unused)
1005 struct gclass *classp;
1008 int name_len, status_len;
1009 int all, error, geoms, i, n, nargs, script;
1011 error = geom_gettree(&mesh);
1013 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
1014 classp = find_class(&mesh, gclass_name);
1016 errx(EXIT_FAILURE, "Class %s not found.", gclass_name);
1017 nargs = gctl_get_int(req, "nargs");
1018 all = gctl_get_int(req, "all");
1019 geoms = gctl_get_int(req, "geoms");
1020 script = gctl_get_int(req, "script");
1025 name_len = strlen("Name");
1026 status_len = strlen("Status");
1029 for (i = 0, n = 0; i < nargs; i++) {
1030 name = gctl_get_ascii(req, "arg%d", i);
1031 gp = find_geom(classp, name);
1033 errx(EXIT_FAILURE, "No such geom: %s.", name);
1035 status_update_len(gp,
1036 &name_len, &status_len);
1038 status_update_len_prs(gp,
1039 &name_len, &status_len);
1047 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
1048 if (LIST_EMPTY(&gp->lg_provider) && !all)
1051 status_update_len(gp,
1052 &name_len, &status_len);
1054 status_update_len_prs(gp,
1055 &name_len, &status_len);
1063 printf("%*s %*s %s\n", name_len, "Name", status_len, "Status",
1067 for (i = 0; i < nargs; i++) {
1068 name = gctl_get_ascii(req, "arg%d", i);
1069 gp = find_geom(classp, name);
1073 status_one_geom(gp, script, name_len,
1076 status_one_geom_prs(gp, script, name_len,
1081 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
1082 if (LIST_EMPTY(&gp->lg_provider) && !all)
1085 status_one_geom(gp, script, name_len,
1088 status_one_geom_prs(gp, script, name_len,
1094 geom_deletetree(&mesh);
1098 std_load_available(void)
1100 char name[MAXPATHLEN], paths[MAXPATHLEN * 8], *p;
1104 snprintf(name, sizeof(name), "g_%s", class_name);
1106 * If already in kernel, "load" command is not available.
1108 if (modfind(name) >= 0)
1110 bzero(paths, sizeof(paths));
1111 len = sizeof(paths);
1112 if (sysctlbyname("kern.module_path", paths, &len, NULL, 0) < 0)
1113 err(EXIT_FAILURE, "sysctl(kern.module_path)");
1114 for (p = strtok(paths, ";"); p != NULL; p = strtok(NULL, ";")) {
1115 snprintf(name, sizeof(name), "%s/geom_%s.ko", p, class_name);
1117 * If geom_<name>.ko file exists, "load" command is available.
1119 if (stat(name, &sb) == 0)
1126 std_load(struct gctl_req *req __unused, unsigned flags)
1130 * Do nothing special here, because of G_FLAG_LOADKLD flag,
1131 * module is already loaded.
1133 if ((flags & G_FLAG_VERBOSE) != 0)
1134 printf("Module available.\n");
1138 std_unload_available(void)
1143 snprintf(name, sizeof(name), "geom_%s", class_name);
1151 std_unload(struct gctl_req *req, unsigned flags __unused)
1156 snprintf(name, sizeof(name), "geom_%s", class_name);
1159 gctl_error(req, "Could not find module: %s.", strerror(errno));
1162 if (kldunload(id) < 0) {
1163 gctl_error(req, "Could not unload module: %s.",
1170 std_available(const char *name)
1173 if (strcmp(name, "help") == 0)
1175 else if (strcmp(name, "list") == 0)
1176 return (std_list_available());
1177 else if (strcmp(name, "status") == 0)
1178 return (std_status_available());
1179 else if (strcmp(name, "load") == 0)
1180 return (std_load_available());
1181 else if (strcmp(name, "unload") == 0)
1182 return (std_unload_available());
1184 assert(!"Unknown standard command.");