2 * Copyright (c) 2004-2009 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/linker.h>
32 #include <sys/module.h>
34 #include <sys/sysctl.h>
52 #include "misc/subr.h"
54 #ifdef STATIC_GEOM_CLASSES
55 extern uint32_t gpart_version;
56 extern struct g_command gpart_class_commands[];
57 extern uint32_t glabel_version;
58 extern struct g_command glabel_class_commands[];
61 static char comm[MAXPATHLEN], *class_name = NULL, *gclass_name = NULL;
62 static uint32_t *version = NULL;
63 static int verbose = 0;
64 static struct g_command *class_commands = NULL;
66 #define GEOM_CLASS_CMDS 0x01
67 #define GEOM_STD_CMDS 0x02
68 static struct g_command *find_command(const char *cmdstr, int flags);
69 static int std_available(const char *name);
71 static void std_help(struct gctl_req *req, unsigned flags);
72 static void std_list(struct gctl_req *req, unsigned flags);
73 static void std_status(struct gctl_req *req, unsigned flags);
74 static void std_load(struct gctl_req *req, unsigned flags);
75 static void std_unload(struct gctl_req *req, unsigned flags);
77 static struct g_command std_commands[] = {
78 { "help", 0, std_help, G_NULL_OPTS, NULL },
79 { "list", 0, std_list,
81 { 'a', "all", NULL, G_TYPE_BOOL },
86 { "status", 0, std_status,
88 { 'a', "all", NULL, G_TYPE_BOOL },
89 { 'g', "geoms", NULL, G_TYPE_BOOL },
90 { 's', "script", NULL, G_TYPE_BOOL },
95 { "load", G_FLAG_VERBOSE | G_FLAG_LOADKLD, std_load, G_NULL_OPTS,
97 { "unload", G_FLAG_VERBOSE, std_unload, G_NULL_OPTS, NULL },
102 usage_command(struct g_command *cmd, const char *prefix)
104 struct g_option *opt;
107 if (cmd->gc_usage != NULL) {
108 char *pos, *ptr, *sptr;
110 sptr = ptr = strdup(cmd->gc_usage);
111 while ((pos = strsep(&ptr, "\n")) != NULL) {
114 fprintf(stderr, "%s %s %s %s\n", prefix, comm,
121 fprintf(stderr, "%s %s %s", prefix, comm, cmd->gc_name);
122 if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0)
123 fprintf(stderr, " [-v]");
125 opt = &cmd->gc_options[i];
126 if (opt->go_name == NULL)
128 if (opt->go_val != NULL || G_OPT_TYPE(opt) == G_TYPE_BOOL)
129 fprintf(stderr, " [");
131 fprintf(stderr, " ");
132 fprintf(stderr, "-%c", opt->go_char);
133 if (G_OPT_TYPE(opt) != G_TYPE_BOOL)
134 fprintf(stderr, " %s", opt->go_name);
135 if (opt->go_val != NULL || G_OPT_TYPE(opt) == G_TYPE_BOOL)
136 fprintf(stderr, "]");
138 fprintf(stderr, "\n");
145 if (class_name == NULL) {
146 errx(EXIT_FAILURE, "usage: %s <class> <command> [options]",
149 struct g_command *cmd;
154 if (class_commands != NULL) {
156 cmd = &class_commands[i];
157 if (cmd->gc_name == NULL)
159 usage_command(cmd, prefix);
164 cmd = &std_commands[i];
165 if (cmd->gc_name == NULL)
168 * If class defines command, which has the same name as
169 * standard command, skip it, because it was already
172 if (find_command(cmd->gc_name, GEOM_CLASS_CMDS) != NULL)
174 usage_command(cmd, prefix);
184 char name1[64], name2[64];
186 snprintf(name1, sizeof(name1), "g_%s", class_name);
187 snprintf(name2, sizeof(name2), "geom_%s", class_name);
188 if (modfind(name1) < 0) {
189 /* Not present in kernel, try loading it. */
190 if (kldload(name2) < 0 || modfind(name1) < 0) {
191 if (errno != EEXIST) {
193 "%s module not available!", name2);
200 strlcatf(char *str, size_t size, const char *format, ...)
210 va_start(ap, format);
211 ret = vsnprintf(str, size, format, ap);
218 * Find given option in options available for given command.
220 static struct g_option *
221 find_option(struct g_command *cmd, char ch)
223 struct g_option *opt;
227 opt = &cmd->gc_options[i];
228 if (opt->go_name == NULL)
230 if (opt->go_char == ch)
238 * Add given option to gctl_req.
241 set_option(struct gctl_req *req, struct g_option *opt, const char *val)
247 if (G_OPT_ISMULTI(opt)) {
250 if (G_OPT_NUM(opt) == UCHAR_MAX)
251 errx(EXIT_FAILURE, "Too many -%c options.", opt->go_char);
254 * Base option name length plus 3 bytes for option number
255 * (max. 255 options) plus 1 byte for terminating '\0'.
257 optnamesize = strlen(opt->go_name) + 3 + 1;
258 ptr = malloc(optnamesize);
260 errx(EXIT_FAILURE, "No memory.");
261 snprintf(ptr, optnamesize, "%s%u", opt->go_name, G_OPT_NUM(opt));
265 optname = opt->go_name;
268 if (G_OPT_TYPE(opt) == G_TYPE_NUMBER) {
269 if (expand_number(val, &number) == -1) {
270 err(EXIT_FAILURE, "Invalid value for '%c' argument",
273 ptr = malloc(sizeof(intmax_t));
275 errx(EXIT_FAILURE, "No memory.");
276 *(intmax_t *)ptr = number;
278 gctl_ro_param(req, optname, sizeof(intmax_t), opt->go_val);
279 } else if (G_OPT_TYPE(opt) == G_TYPE_STRING) {
280 gctl_ro_param(req, optname, -1, val);
281 } else if (G_OPT_TYPE(opt) == G_TYPE_BOOL) {
282 ptr = malloc(sizeof(int));
284 errx(EXIT_FAILURE, "No memory.");
285 *(int *)ptr = *val - '0';
287 gctl_ro_param(req, optname, sizeof(int), opt->go_val);
289 assert(!"Invalid type");
292 if (G_OPT_ISMULTI(opt))
293 free(__DECONST(char *, optname));
297 * 1. Add given argument by caller.
298 * 2. Add default values of not given arguments.
299 * 3. Add the rest of arguments.
302 parse_arguments(struct g_command *cmd, struct gctl_req *req, int *argc,
305 struct g_option *opt;
311 if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0)
312 strlcat(opts, "v", sizeof(opts));
314 opt = &cmd->gc_options[i];
315 if (opt->go_name == NULL)
317 assert(G_OPT_TYPE(opt) != 0);
318 assert((opt->go_type & ~(G_TYPE_MASK | G_TYPE_MULTI)) == 0);
319 /* Multiple bool arguments makes no sense. */
320 assert(G_OPT_TYPE(opt) != G_TYPE_BOOL ||
321 (opt->go_type & G_TYPE_MULTI) == 0);
322 strlcatf(opts, sizeof(opts), "%c", opt->go_char);
323 if (G_OPT_TYPE(opt) != G_TYPE_BOOL)
324 strlcat(opts, ":", sizeof(opts));
328 * Add specified arguments.
330 while ((ch = getopt(*argc, *argv, opts)) != -1) {
331 /* Standard (not passed to kernel) options. */
337 /* Options passed to kernel. */
338 opt = find_option(cmd, ch);
341 if (!G_OPT_ISMULTI(opt) && G_OPT_ISDONE(opt)) {
342 warnx("Option '%c' specified twice.", opt->go_char);
347 if (G_OPT_TYPE(opt) == G_TYPE_BOOL)
348 set_option(req, opt, "1");
350 set_option(req, opt, optarg);
356 * Add not specified arguments, but with default values.
359 opt = &cmd->gc_options[i];
360 if (opt->go_name == NULL)
362 if (G_OPT_ISDONE(opt))
365 if (G_OPT_TYPE(opt) == G_TYPE_BOOL) {
366 assert(opt->go_val == NULL);
367 set_option(req, opt, "0");
369 if (opt->go_val == NULL) {
370 warnx("Option '%c' not specified.",
373 } else if (opt->go_val == G_VAL_OPTIONAL) {
376 set_option(req, opt, opt->go_val);
382 * Add rest of given arguments.
384 gctl_ro_param(req, "nargs", sizeof(int), argc);
385 for (i = 0; i < (unsigned)*argc; i++) {
388 snprintf(argname, sizeof(argname), "arg%u", i);
389 gctl_ro_param(req, argname, -1, (*argv)[i]);
394 * Find given command in commands available for given class.
396 static struct g_command *
397 find_command(const char *cmdstr, int flags)
399 struct g_command *cmd;
403 * First try to find command defined by loaded library.
405 if ((flags & GEOM_CLASS_CMDS) != 0 && class_commands != NULL) {
407 cmd = &class_commands[i];
408 if (cmd->gc_name == NULL)
410 if (strcmp(cmd->gc_name, cmdstr) == 0)
415 * Now try to find in standard commands.
417 if ((flags & GEOM_STD_CMDS) != 0) {
419 cmd = &std_commands[i];
420 if (cmd->gc_name == NULL)
422 if (strcmp(cmd->gc_name, cmdstr) == 0)
430 set_flags(struct g_command *cmd)
434 if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0 && verbose)
435 flags |= G_FLAG_VERBOSE;
444 run_command(int argc, char *argv[])
446 struct g_command *cmd;
447 struct gctl_req *req;
451 /* First try to find a command defined by a class. */
452 cmd = find_command(argv[0], GEOM_CLASS_CMDS);
454 /* Now, try to find a standard command. */
455 cmd = find_command(argv[0], GEOM_STD_CMDS);
457 warnx("Unknown command: %s.", argv[0]);
460 if (!std_available(cmd->gc_name)) {
461 warnx("Command '%s' not available.", argv[0]);
465 if ((cmd->gc_flags & G_FLAG_LOADKLD) != 0)
468 req = gctl_get_handle();
469 gctl_ro_param(req, "class", -1, gclass_name);
470 gctl_ro_param(req, "verb", -1, argv[0]);
472 gctl_ro_param(req, "version", sizeof(*version), version);
473 parse_arguments(cmd, req, &argc, &argv);
475 bzero(buf, sizeof(buf));
476 if (cmd->gc_func != NULL) {
479 flags = set_flags(cmd);
480 cmd->gc_func(req, flags);
483 gctl_rw_param(req, "output", sizeof(buf), buf);
484 errstr = gctl_issue(req);
486 if (errstr != NULL && errstr[0] != '\0') {
488 if (strncmp(errstr, "warning: ", strlen("warning: ")) != 0) {
501 #ifndef STATIC_GEOM_CLASSES
507 path = getenv("GEOM_LIBRARY_PATH");
509 path = GEOM_CLASS_DIR;
516 char *curpath, path[MAXPATHLEN], *tofree, *totalpath;
517 uint32_t *lib_version;
522 tofree = totalpath = strdup(library_path());
523 if (totalpath == NULL)
524 err(EXIT_FAILURE, "Not enough memory for library path");
526 if (strchr(totalpath, ':') != NULL)
527 curpath = strsep(&totalpath, ":");
530 /* Traverse the paths to find one that contains the library we want. */
531 while (curpath != NULL) {
532 snprintf(path, sizeof(path), "%s/geom_%s.so", curpath,
534 ret = access(path, F_OK);
536 if (errno == ENOENT) {
538 * If we cannot find library, try the next
541 curpath = strsep(&totalpath, ":");
544 err(EXIT_FAILURE, "Cannot access library");
549 /* No library was found, but standard commands can still be used */
552 dlh = dlopen(path, RTLD_NOW);
554 errx(EXIT_FAILURE, "Cannot open library: %s.", dlerror());
555 lib_version = dlsym(dlh, "lib_version");
556 if (lib_version == NULL) {
557 warnx("Cannot find symbol %s: %s.", "lib_version", dlerror());
561 if (*lib_version != G_LIB_VERSION) {
563 errx(EXIT_FAILURE, "%s and %s are not synchronized.",
564 getprogname(), path);
566 version = dlsym(dlh, "version");
567 if (version == NULL) {
568 warnx("Cannot find symbol %s: %s.", "version", dlerror());
572 class_commands = dlsym(dlh, "class_commands");
573 if (class_commands == NULL) {
574 warnx("Cannot find symbol %s: %s.", "class_commands",
580 #endif /* !STATIC_GEOM_CLASSES */
583 * Class name should be all capital letters.
591 for (; *s1 != '\0'; s1++)
593 gclass_name = malloc(strlen(class_name) + 1);
594 if (gclass_name == NULL)
595 errx(EXIT_FAILURE, "No memory");
598 for (; *s2 != '\0'; s2++)
599 *s1++ = toupper(*s2);
604 get_class(int *argc, char ***argv)
607 snprintf(comm, sizeof(comm), "%s", basename((*argv)[0]));
608 if (strcmp(comm, "geom") == 0) {
611 else if (*argc == 2) {
612 if (strcmp((*argv)[1], "-h") == 0 ||
613 strcmp((*argv)[1], "help") == 0) {
617 strlcatf(comm, sizeof(comm), " %s", (*argv)[1]);
618 class_name = (*argv)[1];
621 } else if (*comm == 'g') {
622 class_name = comm + 1;
626 errx(EXIT_FAILURE, "Invalid utility name.");
629 #ifndef STATIC_GEOM_CLASSES
632 if (!strcasecmp(class_name, "part")) {
633 version = &gpart_version;
634 class_commands = gpart_class_commands;
635 } else if (!strcasecmp(class_name, "label")) {
636 version = &glabel_version;
637 class_commands = glabel_class_commands;
639 errx(EXIT_FAILURE, "Invalid class name.");
640 #endif /* !STATIC_GEOM_CLASSES */
648 main(int argc, char *argv[])
651 get_class(&argc, &argv);
652 run_command(argc, argv);
658 static struct gclass *
659 find_class(struct gmesh *mesh, const char *name)
661 struct gclass *classp;
663 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
664 if (strcmp(classp->lg_name, name) == 0)
670 static struct ggeom *
671 find_geom(struct gclass *classp, const char *name)
675 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
676 if (strcmp(gp->lg_name, name) == 0)
683 list_one_provider(struct gprovider *pp, const char *prefix)
685 struct gconfig *conf;
688 printf("Name: %s\n", pp->lg_name);
689 humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "",
690 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
691 printf("%sMediasize: %jd (%s)\n", prefix, (intmax_t)pp->lg_mediasize,
693 printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize);
694 if (pp->lg_stripesize > 0 || pp->lg_stripeoffset > 0) {
695 printf("%sStripesize: %ju\n", prefix, pp->lg_stripesize);
696 printf("%sStripeoffset: %ju\n", prefix, pp->lg_stripeoffset);
698 printf("%sMode: %s\n", prefix, pp->lg_mode);
699 LIST_FOREACH(conf, &pp->lg_config, lg_config) {
700 printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val);
705 list_one_consumer(struct gconsumer *cp, const char *prefix)
707 struct gprovider *pp;
708 struct gconfig *conf;
710 pp = cp->lg_provider;
712 printf("[no provider]\n");
716 printf("Name: %s\n", pp->lg_name);
717 humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "",
718 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
719 printf("%sMediasize: %jd (%s)\n", prefix,
720 (intmax_t)pp->lg_mediasize, buf);
721 printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize);
722 if (pp->lg_stripesize > 0 || pp->lg_stripeoffset > 0) {
723 printf("%sStripesize: %ju\n", prefix, pp->lg_stripesize);
724 printf("%sStripeoffset: %ju\n", prefix, pp->lg_stripeoffset);
726 printf("%sMode: %s\n", prefix, cp->lg_mode);
728 LIST_FOREACH(conf, &cp->lg_config, lg_config) {
729 printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val);
734 list_one_geom(struct ggeom *gp)
736 struct gprovider *pp;
737 struct gconsumer *cp;
738 struct gconfig *conf;
741 printf("Geom name: %s\n", gp->lg_name);
742 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
743 printf("%s: %s\n", conf->lg_name, conf->lg_val);
745 if (!LIST_EMPTY(&gp->lg_provider)) {
746 printf("Providers:\n");
748 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
750 list_one_provider(pp, " ");
753 if (!LIST_EMPTY(&gp->lg_consumer)) {
754 printf("Consumers:\n");
756 LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
758 list_one_consumer(cp, " ");
765 std_help(struct gctl_req *req __unused, unsigned flags __unused)
772 std_list_available(void)
775 struct gclass *classp;
778 error = geom_gettree(&mesh);
780 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
781 classp = find_class(&mesh, gclass_name);
782 geom_deletetree(&mesh);
789 std_list(struct gctl_req *req, unsigned flags __unused)
792 struct gclass *classp;
795 int all, error, i, nargs;
797 error = geom_gettree(&mesh);
799 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
800 classp = find_class(&mesh, gclass_name);
801 if (classp == NULL) {
802 geom_deletetree(&mesh);
803 errx(EXIT_FAILURE, "Class %s not found.", gclass_name);
805 nargs = gctl_get_int(req, "nargs");
806 all = gctl_get_int(req, "all");
808 for (i = 0; i < nargs; i++) {
809 name = gctl_get_ascii(req, "arg%d", i);
810 gp = find_geom(classp, name);
812 errx(EXIT_FAILURE, "No such geom: %s.", name);
816 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
817 if (LIST_EMPTY(&gp->lg_provider) && !all)
822 geom_deletetree(&mesh);
826 std_status_available(void)
829 /* 'status' command is available when 'list' command is. */
830 return (std_list_available());
834 status_update_len(struct ggeom *gp, int *name_len, int *status_len)
836 struct gconfig *conf;
840 assert(name_len != NULL);
841 assert(status_len != NULL);
843 len = strlen(gp->lg_name);
846 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
847 if (strcasecmp(conf->lg_name, "state") == 0) {
848 len = strlen(conf->lg_val);
849 if (*status_len < len)
856 status_update_len_prs(struct ggeom *gp, int *name_len, int *status_len)
858 struct gprovider *pp;
859 struct gconfig *conf;
863 assert(name_len != NULL);
864 assert(status_len != NULL);
867 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
868 if (strcasecmp(conf->lg_name, "state") == 0) {
869 glen = strlen(conf->lg_val);
873 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
874 len = strlen(pp->lg_name);
878 LIST_FOREACH(conf, &pp->lg_config, lg_config) {
879 if (strcasecmp(conf->lg_name, "state") == 0) {
880 len = strlen(conf->lg_val);
884 if (*status_len < len)
890 status_one_consumer(struct gconsumer *cp)
892 static char buf[256];
893 struct gprovider *pp;
894 struct gconfig *conf;
895 const char *state, *syncr;
897 pp = cp->lg_provider;
902 LIST_FOREACH(conf, &cp->lg_config, lg_config) {
903 if (strcasecmp(conf->lg_name, "state") == 0)
904 state = conf->lg_val;
905 if (strcasecmp(conf->lg_name, "synchronized") == 0)
906 syncr = conf->lg_val;
908 if (state == NULL && syncr == NULL)
909 snprintf(buf, sizeof(buf), "%s", pp->lg_name);
910 else if (state != NULL && syncr != NULL) {
911 snprintf(buf, sizeof(buf), "%s (%s, %s)", pp->lg_name,
914 snprintf(buf, sizeof(buf), "%s (%s)", pp->lg_name,
915 state ? state : syncr);
921 status_one_geom(struct ggeom *gp, int script, int name_len, int status_len)
923 struct gconsumer *cp;
924 struct gconfig *conf;
925 const char *name, *status, *component;
930 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
931 if (strcasecmp(conf->lg_name, "state") == 0) {
932 status = conf->lg_val;
937 LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
938 component = status_one_consumer(cp);
939 if (component == NULL)
942 printf("%*s %*s %s\n", name_len, name, status_len, status,
948 printf("%*s %*s %s\n", name_len, name, status_len, status,
954 status_one_geom_prs(struct ggeom *gp, int script, int name_len, int status_len)
956 struct gprovider *pp;
957 struct gconsumer *cp;
958 struct gconfig *conf;
959 const char *name, *status, *component;
962 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
965 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
966 if (strcasecmp(conf->lg_name, "state") == 0) {
967 status = conf->lg_val;
971 LIST_FOREACH(conf, &pp->lg_config, lg_config) {
972 if (strcasecmp(conf->lg_name, "state") == 0) {
973 status = conf->lg_val;
978 LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
979 component = status_one_consumer(cp);
980 if (component == NULL)
983 printf("%*s %*s %s\n", name_len, name,
984 status_len, status, component);
989 printf("%*s %*s %s\n", name_len, name,
990 status_len, status, "N/A");
996 std_status(struct gctl_req *req, unsigned flags __unused)
999 struct gclass *classp;
1002 int name_len, status_len;
1003 int all, error, geoms, i, n, nargs, script;
1005 error = geom_gettree(&mesh);
1007 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
1008 classp = find_class(&mesh, gclass_name);
1010 errx(EXIT_FAILURE, "Class %s not found.", gclass_name);
1011 nargs = gctl_get_int(req, "nargs");
1012 all = gctl_get_int(req, "all");
1013 geoms = gctl_get_int(req, "geoms");
1014 script = gctl_get_int(req, "script");
1019 name_len = strlen("Name");
1020 status_len = strlen("Status");
1023 for (i = 0, n = 0; i < nargs; i++) {
1024 name = gctl_get_ascii(req, "arg%d", i);
1025 gp = find_geom(classp, name);
1027 errx(EXIT_FAILURE, "No such geom: %s.", name);
1029 status_update_len(gp,
1030 &name_len, &status_len);
1032 status_update_len_prs(gp,
1033 &name_len, &status_len);
1041 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
1042 if (LIST_EMPTY(&gp->lg_provider) && !all)
1045 status_update_len(gp,
1046 &name_len, &status_len);
1048 status_update_len_prs(gp,
1049 &name_len, &status_len);
1057 printf("%*s %*s %s\n", name_len, "Name", status_len, "Status",
1061 for (i = 0; i < nargs; i++) {
1062 name = gctl_get_ascii(req, "arg%d", i);
1063 gp = find_geom(classp, name);
1067 status_one_geom(gp, script, name_len,
1070 status_one_geom_prs(gp, script, name_len,
1075 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
1076 if (LIST_EMPTY(&gp->lg_provider) && !all)
1079 status_one_geom(gp, script, name_len,
1082 status_one_geom_prs(gp, script, name_len,
1088 geom_deletetree(&mesh);
1092 std_load_available(void)
1094 char name[MAXPATHLEN], paths[MAXPATHLEN * 8], *p;
1098 snprintf(name, sizeof(name), "g_%s", class_name);
1100 * If already in kernel, "load" command is not available.
1102 if (modfind(name) >= 0)
1104 bzero(paths, sizeof(paths));
1105 len = sizeof(paths);
1106 if (sysctlbyname("kern.module_path", paths, &len, NULL, 0) < 0)
1107 err(EXIT_FAILURE, "sysctl(kern.module_path)");
1108 for (p = strtok(paths, ";"); p != NULL; p = strtok(NULL, ";")) {
1109 snprintf(name, sizeof(name), "%s/geom_%s.ko", p, class_name);
1111 * If geom_<name>.ko file exists, "load" command is available.
1113 if (stat(name, &sb) == 0)
1120 std_load(struct gctl_req *req __unused, unsigned flags)
1124 * Do nothing special here, because of G_FLAG_LOADKLD flag,
1125 * module is already loaded.
1127 if ((flags & G_FLAG_VERBOSE) != 0)
1128 printf("Module available.\n");
1132 std_unload_available(void)
1137 snprintf(name, sizeof(name), "geom_%s", class_name);
1145 std_unload(struct gctl_req *req, unsigned flags __unused)
1150 snprintf(name, sizeof(name), "geom_%s", class_name);
1153 gctl_error(req, "Could not find module: %s.", strerror(errno));
1156 if (kldunload(id) < 0) {
1157 gctl_error(req, "Could not unload module: %s.",
1164 std_available(const char *name)
1167 if (strcmp(name, "help") == 0)
1169 else if (strcmp(name, "list") == 0)
1170 return (std_list_available());
1171 else if (strcmp(name, "status") == 0)
1172 return (std_status_available());
1173 else if (strcmp(name, "load") == 0)
1174 return (std_load_available());
1175 else if (strcmp(name, "unload") == 0)
1176 return (std_unload_available());
1178 assert(!"Unknown standard command.");