2 * Copyright (c) 2004-2005 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 struct g_command std_commands[] = {
78 { "help", 0, std_help, G_NULL_OPTS, NULL, NULL },
79 { "list", 0, std_list,
81 { 'a', "all", NULL, G_TYPE_BOOL },
84 NULL, "[-a] [name ...]"
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 },
93 NULL, "[-ags] [name ...]"
95 { "load", G_FLAG_VERBOSE | G_FLAG_LOADKLD, std_load, G_NULL_OPTS,
97 { "unload", G_FLAG_VERBOSE, std_unload, G_NULL_OPTS, NULL, NULL },
102 usage_command(struct g_command *cmd, const char *prefix)
104 struct g_option *opt;
107 fprintf(stderr, "%s %s %s", prefix, comm, cmd->gc_name);
108 if (cmd->gc_usage != NULL) {
109 fprintf(stderr, " %s\n", cmd->gc_usage);
112 if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0)
113 fprintf(stderr, " [-v]");
115 opt = &cmd->gc_options[i];
116 if (opt->go_name == NULL)
118 if (opt->go_val != NULL || G_OPT_TYPE(opt) == G_TYPE_BOOL)
119 fprintf(stderr, " [");
121 fprintf(stderr, " ");
122 fprintf(stderr, "-%c", opt->go_char);
123 if (G_OPT_TYPE(opt) != G_TYPE_BOOL)
124 fprintf(stderr, " %s", opt->go_name);
125 if (opt->go_val != NULL || G_OPT_TYPE(opt) == G_TYPE_BOOL)
126 fprintf(stderr, "]");
129 fprintf(stderr, " %s", cmd->gc_argname);
130 fprintf(stderr, "\n");
137 if (class_name == NULL) {
138 errx(EXIT_FAILURE, "usage: %s <class> <command> [options]",
141 struct g_command *cmd;
146 if (class_commands != NULL) {
148 cmd = &class_commands[i];
149 if (cmd->gc_name == NULL)
151 usage_command(cmd, prefix);
156 cmd = &std_commands[i];
157 if (cmd->gc_name == NULL)
160 * If class defines command, which has the same name as
161 * standard command, skip it, because it was already
164 if (find_command(cmd->gc_name, GEOM_CLASS_CMDS) != NULL)
166 usage_command(cmd, prefix);
176 char name1[64], name2[64];
178 snprintf(name1, sizeof(name1), "g_%s", class_name);
179 snprintf(name2, sizeof(name2), "geom_%s", class_name);
180 if (modfind(name1) < 0) {
181 /* Not present in kernel, try loading it. */
182 if (kldload(name2) < 0 || modfind(name1) < 0) {
183 if (errno != EEXIST) {
185 "%s module not available!", name2);
192 strlcatf(char *str, size_t size, const char *format, ...)
202 va_start(ap, format);
203 ret = vsnprintf(str, size, format, ap);
210 * Find given option in options available for given command.
212 static struct g_option *
213 find_option(struct g_command *cmd, char ch)
215 struct g_option *opt;
219 opt = &cmd->gc_options[i];
220 if (opt->go_name == NULL)
222 if (opt->go_char == ch)
230 * Add given option to gctl_req.
233 set_option(struct g_command *cmd, struct gctl_req *req, struct g_option *opt,
240 if (G_OPT_ISMULTI(opt)) {
243 if (G_OPT_NUM(opt) == UCHAR_MAX)
244 errx(EXIT_FAILURE, "Too many -%c options.", opt->go_char);
247 * Base option name length plus 3 bytes for option number
248 * (max. 255 options) plus 1 byte for terminating '\0'.
250 optnamesize = strlen(opt->go_name) + 3 + 1;
251 ptr = malloc(optnamesize);
253 errx(EXIT_FAILURE, "No memory.");
254 snprintf(ptr, optnamesize, "%s%u", opt->go_name, G_OPT_NUM(opt));
258 optname = opt->go_name;
261 if (G_OPT_TYPE(opt) == G_TYPE_NUMBER) {
262 if (expand_number(val, &number) == -1) {
263 err(EXIT_FAILURE, "Invalid value for '%c' argument",
266 ptr = malloc(sizeof(intmax_t));
268 errx(EXIT_FAILURE, "No memory.");
269 *(intmax_t *)ptr = number;
271 gctl_ro_param(req, optname, sizeof(intmax_t), opt->go_val);
272 } else if (G_OPT_TYPE(opt) == G_TYPE_ASCNUM) {
273 if (cmd->gc_argname == NULL || *val != '\0') {
276 if (expand_number(val, &number) == -1) {
277 err(EXIT_FAILURE, "Invalid value for '%c' argument",
280 asprintf(&s, "%jd", number);
282 errx(EXIT_FAILURE, "No memory.");
284 gctl_ro_param(req, optname, -1, opt->go_val);
286 } else if (G_OPT_TYPE(opt) == G_TYPE_STRING) {
287 if (cmd->gc_argname == NULL || *val != '\0')
288 gctl_ro_param(req, optname, -1, val);
289 } else if (G_OPT_TYPE(opt) == G_TYPE_BOOL) {
290 ptr = malloc(sizeof(int));
292 errx(EXIT_FAILURE, "No memory.");
293 *(int *)ptr = *val - '0';
295 gctl_ro_param(req, optname, sizeof(int), opt->go_val);
297 assert(!"Invalid type");
300 if (G_OPT_ISMULTI(opt))
301 free(__DECONST(char *, optname));
305 * 1. Add given argument by caller.
306 * 2. Add default values of not given arguments.
307 * 3. Add the rest of arguments.
310 parse_arguments(struct g_command *cmd, struct gctl_req *req, int *argc,
313 struct g_option *opt;
319 if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0)
320 strlcat(opts, "v", sizeof(opts));
322 opt = &cmd->gc_options[i];
323 if (opt->go_name == NULL)
325 assert(G_OPT_TYPE(opt) != 0);
326 assert((opt->go_type & ~(G_TYPE_MASK | G_TYPE_MULTI)) == 0);
327 /* Multiple bool arguments makes no sense. */
328 assert(G_OPT_TYPE(opt) != G_TYPE_BOOL ||
329 (opt->go_type & G_TYPE_MULTI) == 0);
330 strlcatf(opts, sizeof(opts), "%c", opt->go_char);
331 if (G_OPT_TYPE(opt) != G_TYPE_BOOL)
332 strlcat(opts, ":", sizeof(opts));
336 * Add specified arguments.
338 while ((ch = getopt(*argc, *argv, opts)) != -1) {
339 /* Standard (not passed to kernel) options. */
345 /* Options passed to kernel. */
346 opt = find_option(cmd, ch);
349 if (!G_OPT_ISMULTI(opt) && G_OPT_ISDONE(opt)) {
350 warnx("Option '%c' specified twice.", opt->go_char);
355 if (G_OPT_TYPE(opt) == G_TYPE_BOOL)
356 set_option(cmd, req, opt, "1");
358 set_option(cmd, req, opt, optarg);
364 * Add not specified arguments, but with default values.
367 opt = &cmd->gc_options[i];
368 if (opt->go_name == NULL)
370 if (G_OPT_ISDONE(opt))
373 if (G_OPT_TYPE(opt) == G_TYPE_BOOL) {
374 assert(opt->go_val == NULL);
375 set_option(cmd, req, opt, "0");
377 if (opt->go_val == NULL) {
378 warnx("Option '%c' not specified.",
381 } else if (opt->go_val == G_VAL_OPTIONAL) {
384 set_option(cmd, req, opt, opt->go_val);
389 if (cmd->gc_argname == NULL) {
391 * Add rest of given arguments.
393 gctl_ro_param(req, "nargs", sizeof(int), argc);
394 for (i = 0; i < (unsigned)*argc; i++) {
397 snprintf(argname, sizeof(argname), "arg%u", i);
398 gctl_ro_param(req, argname, -1, (*argv)[i]);
403 gctl_ro_param(req, cmd->gc_argname, -1, (*argv)[0]);
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.", argv[0]);
479 if ((cmd->gc_flags & G_FLAG_LOADKLD) != 0)
482 req = gctl_get_handle();
483 gctl_ro_param(req, "class", -1, gclass_name);
484 gctl_ro_param(req, "verb", -1, argv[0]);
486 gctl_ro_param(req, "version", sizeof(*version), version);
487 parse_arguments(cmd, req, &argc, &argv);
489 bzero(buf, sizeof(buf));
490 if (cmd->gc_func != NULL) {
493 flags = set_flags(cmd);
494 cmd->gc_func(req, flags);
497 gctl_rw_param(req, "output", sizeof(buf), buf);
498 errstr = gctl_issue(req);
500 if (errstr != NULL && errstr[0] != '\0') {
502 if (strncmp(errstr, "warning: ", strlen("warning: ")) != 0) {
515 #ifndef STATIC_GEOM_CLASSES
521 path = getenv("GEOM_LIBRARY_PATH");
530 char *curpath, path[MAXPATHLEN], *tofree, *totalpath;
531 uint32_t *lib_version;
536 tofree = totalpath = strdup(library_path());
537 if (totalpath == NULL)
538 err(EXIT_FAILURE, "Not enough memory for library path");
540 if (strchr(totalpath, ':') != NULL)
541 curpath = strsep(&totalpath, ":");
544 /* Traverse the paths to find one that contains the library we want. */
545 while (curpath != NULL) {
546 snprintf(path, sizeof(path), "%s/geom_%s.so", curpath,
548 ret = access(path, F_OK);
550 if (errno == ENOENT) {
552 * If we cannot find library, try the next
555 curpath = strsep(&totalpath, ":");
558 err(EXIT_FAILURE, "Cannot access library");
563 /* No library was found, but standard commands can still be used */
566 dlh = dlopen(path, RTLD_NOW);
568 errx(EXIT_FAILURE, "Cannot open library: %s.", dlerror());
569 lib_version = dlsym(dlh, "lib_version");
570 if (lib_version == NULL) {
571 warnx("Cannot find symbol %s: %s.", "lib_version", dlerror());
575 if (*lib_version != G_LIB_VERSION) {
577 errx(EXIT_FAILURE, "%s and %s are not synchronized.",
578 getprogname(), path);
580 version = dlsym(dlh, "version");
581 if (version == NULL) {
582 warnx("Cannot find symbol %s: %s.", "version", dlerror());
586 class_commands = dlsym(dlh, "class_commands");
587 if (class_commands == NULL) {
588 warnx("Cannot find symbol %s: %s.", "class_commands",
594 #endif /* !STATIC_GEOM_CLASSES */
597 * Class name should be all capital letters.
605 for (; *s1 != '\0'; s1++)
607 gclass_name = malloc(strlen(class_name) + 1);
608 if (gclass_name == NULL)
609 errx(EXIT_FAILURE, "No memory");
612 for (; *s2 != '\0'; s2++)
613 *s1++ = toupper(*s2);
618 get_class(int *argc, char ***argv)
621 snprintf(comm, sizeof(comm), "%s", basename((*argv)[0]));
622 if (strcmp(comm, "geom") == 0) {
625 else if (*argc == 2) {
626 if (strcmp((*argv)[1], "-h") == 0 ||
627 strcmp((*argv)[1], "help") == 0) {
631 strlcatf(comm, sizeof(comm), " %s", (*argv)[1]);
632 class_name = (*argv)[1];
635 } else if (*comm == 'g') {
636 class_name = comm + 1;
640 errx(EXIT_FAILURE, "Invalid utility name.");
643 #ifndef STATIC_GEOM_CLASSES
646 if (!strcasecmp(class_name, "part")) {
647 version = &gpart_version;
648 class_commands = gpart_class_commands;
649 } else if (!strcasecmp(class_name, "label")) {
650 version = &glabel_version;
651 class_commands = glabel_class_commands;
653 errx(EXIT_FAILURE, "Invalid class name.");
654 #endif /* !STATIC_GEOM_CLASSES */
662 main(int argc, char *argv[])
665 get_class(&argc, &argv);
666 run_command(argc, argv);
672 static struct gclass *
673 find_class(struct gmesh *mesh, const char *name)
675 struct gclass *classp;
677 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
678 if (strcmp(classp->lg_name, name) == 0)
684 static struct ggeom *
685 find_geom(struct gclass *classp, const char *name)
689 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
690 if (strcmp(gp->lg_name, name) == 0)
697 list_one_provider(struct gprovider *pp, const char *prefix)
699 struct gconfig *conf;
702 printf("Name: %s\n", pp->lg_name);
703 humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "",
704 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
705 printf("%sMediasize: %jd (%s)\n", prefix, (intmax_t)pp->lg_mediasize,
707 printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize);
708 if (pp->lg_stripesize > 0 || pp->lg_stripeoffset > 0) {
709 printf("%sStripesize: %ju\n", prefix, pp->lg_stripesize);
710 printf("%sStripeoffset: %ju\n", prefix, pp->lg_stripeoffset);
712 printf("%sMode: %s\n", prefix, pp->lg_mode);
713 LIST_FOREACH(conf, &pp->lg_config, lg_config) {
714 printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val);
719 list_one_consumer(struct gconsumer *cp, const char *prefix)
721 struct gprovider *pp;
722 struct gconfig *conf;
724 pp = cp->lg_provider;
726 printf("[no provider]\n");
730 printf("Name: %s\n", pp->lg_name);
731 humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "",
732 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
733 printf("%sMediasize: %jd (%s)\n", prefix,
734 (intmax_t)pp->lg_mediasize, buf);
735 printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize);
736 if (pp->lg_stripesize > 0 || pp->lg_stripeoffset > 0) {
737 printf("%sStripesize: %ju\n", prefix, pp->lg_stripesize);
738 printf("%sStripeoffset: %ju\n", prefix, pp->lg_stripeoffset);
740 printf("%sMode: %s\n", prefix, cp->lg_mode);
742 LIST_FOREACH(conf, &cp->lg_config, lg_config) {
743 printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val);
748 list_one_geom(struct ggeom *gp)
750 struct gprovider *pp;
751 struct gconsumer *cp;
752 struct gconfig *conf;
755 printf("Geom name: %s\n", gp->lg_name);
756 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
757 printf("%s: %s\n", conf->lg_name, conf->lg_val);
759 if (!LIST_EMPTY(&gp->lg_provider)) {
760 printf("Providers:\n");
762 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
764 list_one_provider(pp, " ");
767 if (!LIST_EMPTY(&gp->lg_consumer)) {
768 printf("Consumers:\n");
770 LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
772 list_one_consumer(cp, " ");
779 std_help(struct gctl_req *req __unused, unsigned flags __unused)
786 std_list_available(void)
789 struct gclass *classp;
792 error = geom_gettree(&mesh);
794 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
795 classp = find_class(&mesh, gclass_name);
796 geom_deletetree(&mesh);
803 std_list(struct gctl_req *req, unsigned flags __unused)
806 struct gclass *classp;
809 int all, error, i, nargs;
811 error = geom_gettree(&mesh);
813 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
814 classp = find_class(&mesh, gclass_name);
815 if (classp == NULL) {
816 geom_deletetree(&mesh);
817 errx(EXIT_FAILURE, "Class %s not found.", gclass_name);
819 nargs = gctl_get_int(req, "nargs");
820 all = gctl_get_int(req, "all");
822 for (i = 0; i < nargs; i++) {
823 name = gctl_get_ascii(req, "arg%d", i);
824 gp = find_geom(classp, name);
826 errx(EXIT_FAILURE, "No such geom: %s.", name);
830 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
831 if (LIST_EMPTY(&gp->lg_provider) && !all)
836 geom_deletetree(&mesh);
840 std_status_available(void)
843 /* 'status' command is available when 'list' command is. */
844 return (std_list_available());
848 status_update_len(struct ggeom *gp, int *name_len, int *status_len)
850 struct gconfig *conf;
854 assert(name_len != NULL);
855 assert(status_len != NULL);
857 len = strlen(gp->lg_name);
860 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
861 if (strcasecmp(conf->lg_name, "state") == 0) {
862 len = strlen(conf->lg_val);
863 if (*status_len < len)
870 status_update_len_prs(struct ggeom *gp, int *name_len, int *status_len)
872 struct gprovider *pp;
873 struct gconfig *conf;
877 assert(name_len != NULL);
878 assert(status_len != NULL);
881 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
882 if (strcasecmp(conf->lg_name, "state") == 0) {
883 glen = strlen(conf->lg_val);
887 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
888 len = strlen(pp->lg_name);
892 LIST_FOREACH(conf, &pp->lg_config, lg_config) {
893 if (strcasecmp(conf->lg_name, "state") == 0) {
894 len = strlen(conf->lg_val);
898 if (*status_len < len)
904 status_one_consumer(struct gconsumer *cp)
906 static char buf[256];
907 struct gprovider *pp;
908 struct gconfig *conf;
909 const char *state, *syncr;
911 pp = cp->lg_provider;
916 LIST_FOREACH(conf, &cp->lg_config, lg_config) {
917 if (strcasecmp(conf->lg_name, "state") == 0)
918 state = conf->lg_val;
919 if (strcasecmp(conf->lg_name, "synchronized") == 0)
920 syncr = conf->lg_val;
922 if (state == NULL && syncr == NULL)
923 snprintf(buf, sizeof(buf), "%s", pp->lg_name);
924 else if (state != NULL && syncr != NULL) {
925 snprintf(buf, sizeof(buf), "%s (%s, %s)", pp->lg_name,
928 snprintf(buf, sizeof(buf), "%s (%s)", pp->lg_name,
929 state ? state : syncr);
935 status_one_geom(struct ggeom *gp, int script, int name_len, int status_len)
937 struct gconsumer *cp;
938 struct gconfig *conf;
939 const char *name, *status, *component;
944 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
945 if (strcasecmp(conf->lg_name, "state") == 0) {
946 status = conf->lg_val;
951 LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
952 component = status_one_consumer(cp);
953 if (component == NULL)
956 printf("%*s %*s %s\n", name_len, name, status_len, status,
962 printf("%*s %*s %s\n", name_len, name, status_len, status,
968 status_one_geom_prs(struct ggeom *gp, int script, int name_len, int status_len)
970 struct gprovider *pp;
971 struct gconsumer *cp;
972 struct gconfig *conf;
973 const char *name, *status, *component;
976 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
979 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
980 if (strcasecmp(conf->lg_name, "state") == 0) {
981 status = conf->lg_val;
985 LIST_FOREACH(conf, &pp->lg_config, lg_config) {
986 if (strcasecmp(conf->lg_name, "state") == 0) {
987 status = conf->lg_val;
992 LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
993 component = status_one_consumer(cp);
994 if (component == NULL)
997 printf("%*s %*s %s\n", name_len, name,
998 status_len, status, component);
1003 printf("%*s %*s %s\n", name_len, name,
1004 status_len, status, "N/A");
1010 std_status(struct gctl_req *req, unsigned flags __unused)
1013 struct gclass *classp;
1016 int name_len, status_len;
1017 int all, error, geoms, i, n, nargs, script;
1019 error = geom_gettree(&mesh);
1021 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
1022 classp = find_class(&mesh, gclass_name);
1024 errx(EXIT_FAILURE, "Class %s not found.", gclass_name);
1025 nargs = gctl_get_int(req, "nargs");
1026 all = gctl_get_int(req, "all");
1027 geoms = gctl_get_int(req, "geoms");
1028 script = gctl_get_int(req, "script");
1033 name_len = strlen("Name");
1034 status_len = strlen("Status");
1037 for (i = 0, n = 0; i < nargs; i++) {
1038 name = gctl_get_ascii(req, "arg%d", i);
1039 gp = find_geom(classp, name);
1041 errx(EXIT_FAILURE, "No such geom: %s.", name);
1043 status_update_len(gp,
1044 &name_len, &status_len);
1046 status_update_len_prs(gp,
1047 &name_len, &status_len);
1055 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
1056 if (LIST_EMPTY(&gp->lg_provider) && !all)
1059 status_update_len(gp,
1060 &name_len, &status_len);
1062 status_update_len_prs(gp,
1063 &name_len, &status_len);
1071 printf("%*s %*s %s\n", name_len, "Name", status_len, "Status",
1075 for (i = 0; i < nargs; i++) {
1076 name = gctl_get_ascii(req, "arg%d", i);
1077 gp = find_geom(classp, name);
1081 status_one_geom(gp, script, name_len,
1084 status_one_geom_prs(gp, script, name_len,
1089 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
1090 if (LIST_EMPTY(&gp->lg_provider) && !all)
1093 status_one_geom(gp, script, name_len,
1096 status_one_geom_prs(gp, script, name_len,
1102 geom_deletetree(&mesh);
1106 std_load_available(void)
1108 char name[MAXPATHLEN], paths[MAXPATHLEN * 8], *p;
1112 snprintf(name, sizeof(name), "g_%s", class_name);
1114 * If already in kernel, "load" command is not available.
1116 if (modfind(name) >= 0)
1118 bzero(paths, sizeof(paths));
1119 len = sizeof(paths);
1120 if (sysctlbyname("kern.module_path", paths, &len, NULL, 0) < 0)
1121 err(EXIT_FAILURE, "sysctl(kern.module_path)");
1122 for (p = strtok(paths, ";"); p != NULL; p = strtok(NULL, ";")) {
1123 snprintf(name, sizeof(name), "%s/geom_%s.ko", p, class_name);
1125 * If geom_<name>.ko file exists, "load" command is available.
1127 if (stat(name, &sb) == 0)
1134 std_load(struct gctl_req *req __unused, unsigned flags)
1138 * Do nothing special here, because of G_FLAG_LOADKLD flag,
1139 * module is already loaded.
1141 if ((flags & G_FLAG_VERBOSE) != 0)
1142 printf("Module available.\n");
1146 std_unload_available(void)
1151 snprintf(name, sizeof(name), "geom_%s", class_name);
1159 std_unload(struct gctl_req *req, unsigned flags __unused)
1164 snprintf(name, sizeof(name), "geom_%s", class_name);
1167 gctl_error(req, "Could not find module: %s.", strerror(errno));
1170 if (kldunload(id) < 0) {
1171 gctl_error(req, "Could not unload module: %s.",
1178 std_available(const char *name)
1181 if (strcmp(name, "help") == 0)
1183 else if (strcmp(name, "list") == 0)
1184 return (std_list_available());
1185 else if (strcmp(name, "status") == 0)
1186 return (std_status_available());
1187 else if (strcmp(name, "load") == 0)
1188 return (std_load_available());
1189 else if (strcmp(name, "unload") == 0)
1190 return (std_unload_available());
1192 assert(!"Unknown standard command.");