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, G_NULL_OPTS, NULL,
82 { "status", 0, std_status,
84 { 's', "script", NULL, G_TYPE_BOOL },
87 NULL, "[-s] [name ...]"
89 { "load", G_FLAG_VERBOSE | G_FLAG_LOADKLD, std_load, G_NULL_OPTS,
91 { "unload", G_FLAG_VERBOSE, std_unload, G_NULL_OPTS, NULL, NULL },
96 usage_command(struct g_command *cmd, const char *prefix)
101 fprintf(stderr, "%s %s %s", prefix, comm, cmd->gc_name);
102 if (cmd->gc_usage != NULL) {
103 fprintf(stderr, " %s\n", cmd->gc_usage);
106 if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0)
107 fprintf(stderr, " [-v]");
109 opt = &cmd->gc_options[i];
110 if (opt->go_name == NULL)
112 if (opt->go_val != NULL || G_OPT_TYPE(opt) == G_TYPE_BOOL)
113 fprintf(stderr, " [");
115 fprintf(stderr, " ");
116 fprintf(stderr, "-%c", opt->go_char);
117 if (G_OPT_TYPE(opt) != G_TYPE_BOOL)
118 fprintf(stderr, " %s", opt->go_name);
119 if (opt->go_val != NULL || G_OPT_TYPE(opt) == G_TYPE_BOOL)
120 fprintf(stderr, "]");
123 fprintf(stderr, " %s", cmd->gc_argname);
124 fprintf(stderr, "\n");
131 if (class_name == NULL) {
132 errx(EXIT_FAILURE, "usage: %s <class> <command> [options]",
135 struct g_command *cmd;
140 if (class_commands != NULL) {
142 cmd = &class_commands[i];
143 if (cmd->gc_name == NULL)
145 usage_command(cmd, prefix);
150 cmd = &std_commands[i];
151 if (cmd->gc_name == NULL)
154 * If class defines command, which has the same name as
155 * standard command, skip it, because it was already
158 if (find_command(cmd->gc_name, GEOM_CLASS_CMDS) != NULL)
160 usage_command(cmd, prefix);
170 char name1[64], name2[64];
172 snprintf(name1, sizeof(name1), "g_%s", class_name);
173 snprintf(name2, sizeof(name2), "geom_%s", class_name);
174 if (modfind(name1) < 0) {
175 /* Not present in kernel, try loading it. */
176 if (kldload(name2) < 0 || modfind(name1) < 0) {
177 if (errno != EEXIST) {
179 "%s module not available!", name2);
186 strlcatf(char *str, size_t size, const char *format, ...)
196 va_start(ap, format);
197 ret = vsnprintf(str, size, format, ap);
204 * Find given option in options available for given command.
206 static struct g_option *
207 find_option(struct g_command *cmd, char ch)
209 struct g_option *opt;
213 opt = &cmd->gc_options[i];
214 if (opt->go_name == NULL)
216 if (opt->go_char == ch)
224 * Add given option to gctl_req.
227 set_option(struct gctl_req *req, struct g_option *opt, const char *val)
232 if (G_OPT_TYPE(opt) == G_TYPE_NUMBER ||
233 G_OPT_TYPE(opt) == G_TYPE_ASCNUM) {
234 if (expand_number(val, &number) == -1) {
235 err(EXIT_FAILURE, "Invalid value for '%c' argument.",
238 if (G_OPT_TYPE(opt) == G_TYPE_NUMBER)
239 opt->go_val = malloc(sizeof(intmax_t));
241 asprintf(&s, "%jd", number);
244 if (opt->go_val == NULL)
245 errx(EXIT_FAILURE, "No memory.");
246 if (G_OPT_TYPE(opt) == G_TYPE_NUMBER) {
247 *(intmax_t *)opt->go_val = number;
248 gctl_ro_param(req, opt->go_name, sizeof(intmax_t),
251 gctl_ro_param(req, opt->go_name, -1, opt->go_val);
252 } else if (G_OPT_TYPE(opt) == G_TYPE_ASCLBA) {
254 * LBAs are ugly. The argument is a sector. The size of a
255 * sector is context specific (i.e. determined by the media),
256 * which we don't know here. But when users enter a value
257 * with a SI unit, they really mean the byte-size or byte-
258 * offset and not the size or offset in sectors.
259 * So how can we map the byte-oriented value into a sector-
260 * oriented value if we don't know the sector size in bytes?
261 * The approach taken here is:
262 * o Sectors are 512 bytes in size. Mostly the case anyway.
263 * o When no SI unit is specified the value is in sectors.
264 * o With an SI unit the value is in bytes.
265 * o The 'b' suffix forces byte interpretation and the 's'
266 * suffix forces sector interpretation.
269 * o 2 and 2s mean 2 sectors, and 2b means 2 bytes.
270 * o 4k and 4kb mean 4096 bytes, and 4ks means 4096 sectors.
272 * "This seemed like a good idea at the time"
276 number = strtoimax(val, &s, 0);
278 errc(EXIT_FAILURE, EINVAL, "argument '%c'",
281 unit = 512; /* sector */
306 unit = 1; /* bytes */
313 unit = 512; /* sector */
316 unit = 1; /* bytes */
319 errc(EXIT_FAILURE, EINVAL, "argument '%c': suffix '%c'",
324 errx(EXIT_FAILURE, "argument '%c': junk at end (%s)",
327 if (mult * unit < mult || number * mult * unit < number)
328 errc(EXIT_FAILURE, ERANGE, "argument '%c'",
330 number *= mult * unit;
332 errx(EXIT_FAILURE, "argument '%c': "
333 "not a valid block address", opt->go_char);
335 asprintf(&s, "%jd", number);
337 err(EXIT_FAILURE, NULL);
339 gctl_ro_param(req, opt->go_name, -1, s);
340 } else if (G_OPT_TYPE(opt) == G_TYPE_STRING) {
341 gctl_ro_param(req, opt->go_name, -1, val);
342 } else if (G_OPT_TYPE(opt) == G_TYPE_BOOL) {
343 opt->go_val = malloc(sizeof(int));
344 if (opt->go_val == NULL)
345 errx(EXIT_FAILURE, "No memory.");
346 *(int *)opt->go_val = *val - '0';
347 gctl_ro_param(req, opt->go_name, sizeof(int), opt->go_val);
349 assert(!"Invalid type");
354 * 1. Add given argument by caller.
355 * 2. Add default values of not given arguments.
356 * 3. Add the rest of arguments.
359 parse_arguments(struct g_command *cmd, struct gctl_req *req, int *argc,
362 struct g_option *opt;
368 if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0)
369 strlcat(opts, "v", sizeof(opts));
371 opt = &cmd->gc_options[i];
372 if (opt->go_name == NULL)
374 assert(G_OPT_TYPE(opt) != 0);
375 assert((opt->go_type & ~G_TYPE_MASK) == 0);
376 strlcatf(opts, sizeof(opts), "%c", opt->go_char);
377 if (G_OPT_TYPE(opt) != G_TYPE_BOOL)
378 strlcat(opts, ":", sizeof(opts));
382 * Add specified arguments.
384 while ((ch = getopt(*argc, *argv, opts)) != -1) {
385 /* Standard (not passed to kernel) options. */
391 /* Options passed to kernel. */
392 opt = find_option(cmd, ch);
395 if (G_OPT_ISDONE(opt)) {
396 warnx("Option '%c' specified twice.", opt->go_char);
401 if (G_OPT_TYPE(opt) == G_TYPE_BOOL)
402 set_option(req, opt, "1");
404 set_option(req, opt, optarg);
410 * Add not specified arguments, but with default values.
413 opt = &cmd->gc_options[i];
414 if (opt->go_name == NULL)
416 if (G_OPT_ISDONE(opt))
419 if (G_OPT_TYPE(opt) == G_TYPE_BOOL) {
420 assert(opt->go_val == NULL);
421 set_option(req, opt, "0");
423 if (opt->go_val == NULL) {
424 warnx("Option '%c' not specified.",
428 if (G_OPT_TYPE(opt) == G_TYPE_NUMBER) {
429 gctl_ro_param(req, opt->go_name,
430 sizeof(intmax_t), opt->go_val);
431 } else if (G_OPT_TYPE(opt) == G_TYPE_STRING ||
432 G_OPT_TYPE(opt) == G_TYPE_ASCNUM ||
433 G_OPT_TYPE(opt) == G_TYPE_ASCLBA) {
434 if (cmd->gc_argname == NULL ||
435 opt->go_val == NULL ||
436 *(char *)opt->go_val != '\0')
437 gctl_ro_param(req, opt->go_name,
440 assert(!"Invalid type");
446 if (cmd->gc_argname == NULL) {
448 * Add rest of given arguments.
450 gctl_ro_param(req, "nargs", sizeof(int), argc);
451 for (i = 0; i < (unsigned)*argc; i++) {
454 snprintf(argname, sizeof(argname), "arg%u", i);
455 gctl_ro_param(req, argname, -1, (*argv)[i]);
460 gctl_ro_param(req, cmd->gc_argname, -1, (*argv)[0]);
465 * Find given command in commands available for given class.
467 static struct g_command *
468 find_command(const char *cmdstr, int flags)
470 struct g_command *cmd;
474 * First try to find command defined by loaded library.
476 if ((flags & GEOM_CLASS_CMDS) != 0 && class_commands != NULL) {
478 cmd = &class_commands[i];
479 if (cmd->gc_name == NULL)
481 if (strcmp(cmd->gc_name, cmdstr) == 0)
486 * Now try to find in standard commands.
488 if ((flags & GEOM_STD_CMDS) != 0) {
490 cmd = &std_commands[i];
491 if (cmd->gc_name == NULL)
493 if (strcmp(cmd->gc_name, cmdstr) == 0)
501 set_flags(struct g_command *cmd)
505 if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0 && verbose)
506 flags |= G_FLAG_VERBOSE;
515 run_command(int argc, char *argv[])
517 struct g_command *cmd;
518 struct gctl_req *req;
522 /* First try to find a command defined by a class. */
523 cmd = find_command(argv[0], GEOM_CLASS_CMDS);
525 /* Now, try to find a standard command. */
526 cmd = find_command(argv[0], GEOM_STD_CMDS);
528 warnx("Unknown command: %s.", argv[0]);
531 if (!std_available(cmd->gc_name)) {
532 warnx("Command '%s' not available.", argv[0]);
536 if ((cmd->gc_flags & G_FLAG_LOADKLD) != 0)
539 req = gctl_get_handle();
540 gctl_ro_param(req, "class", -1, gclass_name);
541 gctl_ro_param(req, "verb", -1, argv[0]);
543 gctl_ro_param(req, "version", sizeof(*version), version);
544 parse_arguments(cmd, req, &argc, &argv);
546 bzero(buf, sizeof(buf));
547 if (cmd->gc_func != NULL) {
550 flags = set_flags(cmd);
551 cmd->gc_func(req, flags);
554 gctl_rw_param(req, "output", sizeof(buf), buf);
555 errstr = gctl_issue(req);
557 if (errstr != NULL && errstr[0] != '\0') {
559 if (strncmp(errstr, "warning: ", strlen("warning: ")) != 0) {
572 #ifndef STATIC_GEOM_CLASSES
578 path = getenv("GEOM_LIBRARY_PATH");
587 char *curpath, path[MAXPATHLEN], *tofree, *totalpath;
588 uint32_t *lib_version;
593 tofree = totalpath = strdup(library_path());
594 if (totalpath == NULL)
595 err(EXIT_FAILURE, "Not enough memory for library path");
597 if (strchr(totalpath, ':') != NULL)
598 curpath = strsep(&totalpath, ":");
601 /* Traverse the paths to find one that contains the library we want. */
602 while (curpath != NULL) {
603 snprintf(path, sizeof(path), "%s/geom_%s.so", curpath,
605 ret = access(path, F_OK);
607 if (errno == ENOENT) {
609 * If we cannot find library, try the next
612 curpath = strsep(&totalpath, ":");
615 err(EXIT_FAILURE, "Cannot access library");
620 /* No library was found, but standard commands can still be used */
623 dlh = dlopen(path, RTLD_NOW);
625 errx(EXIT_FAILURE, "Cannot open library: %s.", dlerror());
626 lib_version = dlsym(dlh, "lib_version");
627 if (lib_version == NULL) {
628 warnx("Cannot find symbol %s: %s.", "lib_version", dlerror());
632 if (*lib_version != G_LIB_VERSION) {
634 errx(EXIT_FAILURE, "%s and %s are not synchronized.",
635 getprogname(), path);
637 version = dlsym(dlh, "version");
638 if (version == NULL) {
639 warnx("Cannot find symbol %s: %s.", "version", dlerror());
643 class_commands = dlsym(dlh, "class_commands");
644 if (class_commands == NULL) {
645 warnx("Cannot find symbol %s: %s.", "class_commands",
651 #endif /* !STATIC_GEOM_CLASSES */
654 * Class name should be all capital letters.
662 for (; *s1 != '\0'; s1++)
664 gclass_name = malloc(strlen(class_name) + 1);
665 if (gclass_name == NULL)
666 errx(EXIT_FAILURE, "No memory");
669 for (; *s2 != '\0'; s2++)
670 *s1++ = toupper(*s2);
675 get_class(int *argc, char ***argv)
678 snprintf(comm, sizeof(comm), "%s", basename((*argv)[0]));
679 if (strcmp(comm, "geom") == 0) {
682 else if (*argc == 2) {
683 if (strcmp((*argv)[1], "-h") == 0 ||
684 strcmp((*argv)[1], "help") == 0) {
688 strlcatf(comm, sizeof(comm), " %s", (*argv)[1]);
689 class_name = (*argv)[1];
692 } else if (*comm == 'g') {
693 class_name = comm + 1;
697 errx(EXIT_FAILURE, "Invalid utility name.");
700 #ifndef STATIC_GEOM_CLASSES
703 if (!strcasecmp(class_name, "part")) {
704 version = &gpart_version;
705 class_commands = gpart_class_commands;
706 } else if (!strcasecmp(class_name, "label")) {
707 version = &glabel_version;
708 class_commands = glabel_class_commands;
710 errx(EXIT_FAILURE, "Invalid class name.");
711 #endif /* !STATIC_GEOM_CLASSES */
719 main(int argc, char *argv[])
722 get_class(&argc, &argv);
723 run_command(argc, argv);
729 static struct gclass *
730 find_class(struct gmesh *mesh, const char *name)
732 struct gclass *classp;
734 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
735 if (strcmp(classp->lg_name, name) == 0)
741 static struct ggeom *
742 find_geom(struct gclass *classp, const char *name)
746 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
747 if (strcmp(gp->lg_name, name) == 0)
754 list_one_provider(struct gprovider *pp, const char *prefix)
756 struct gconfig *conf;
759 printf("Name: %s\n", pp->lg_name);
760 humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "",
761 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
762 printf("%sMediasize: %jd (%s)\n", prefix, (intmax_t)pp->lg_mediasize,
764 printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize);
765 printf("%sMode: %s\n", prefix, pp->lg_mode);
766 LIST_FOREACH(conf, &pp->lg_config, lg_config) {
767 printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val);
772 list_one_consumer(struct gconsumer *cp, const char *prefix)
774 struct gprovider *pp;
775 struct gconfig *conf;
777 pp = cp->lg_provider;
779 printf("[no provider]\n");
783 printf("Name: %s\n", pp->lg_name);
784 humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "",
785 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
786 printf("%sMediasize: %jd (%s)\n", prefix,
787 (intmax_t)pp->lg_mediasize, buf);
788 printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize);
789 printf("%sMode: %s\n", prefix, cp->lg_mode);
791 LIST_FOREACH(conf, &cp->lg_config, lg_config) {
792 printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val);
797 list_one_geom(struct ggeom *gp)
799 struct gprovider *pp;
800 struct gconsumer *cp;
801 struct gconfig *conf;
804 printf("Geom name: %s\n", gp->lg_name);
805 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
806 printf("%s: %s\n", conf->lg_name, conf->lg_val);
808 if (!LIST_EMPTY(&gp->lg_provider)) {
809 printf("Providers:\n");
811 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
813 list_one_provider(pp, " ");
816 if (!LIST_EMPTY(&gp->lg_consumer)) {
817 printf("Consumers:\n");
819 LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
821 list_one_consumer(cp, " ");
828 std_help(struct gctl_req *req __unused, unsigned flags __unused)
835 std_list_available(void)
838 struct gclass *classp;
841 error = geom_gettree(&mesh);
843 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
844 classp = find_class(&mesh, gclass_name);
845 geom_deletetree(&mesh);
852 std_list(struct gctl_req *req, unsigned flags __unused)
855 struct gclass *classp;
860 error = geom_gettree(&mesh);
862 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
863 classp = find_class(&mesh, gclass_name);
864 if (classp == NULL) {
865 geom_deletetree(&mesh);
866 errx(EXIT_FAILURE, "Class %s not found.", gclass_name);
868 nargs = gctl_get_int(req, "nargs");
870 for (i = 0; i < nargs; i++) {
871 name = gctl_get_ascii(req, "arg%d", i);
872 gp = find_geom(classp, name);
876 errx(EXIT_FAILURE, "No such geom: %s.", name);
879 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
880 if (LIST_EMPTY(&gp->lg_provider))
885 geom_deletetree(&mesh);
889 std_status_available(void)
892 /* 'status' command is available when 'list' command is. */
893 return (std_list_available());
897 status_update_len(struct ggeom *gp, int *name_len, int *status_len)
899 struct gprovider *pp;
900 struct gconfig *conf;
904 assert(name_len != NULL);
905 assert(status_len != NULL);
907 pp = LIST_FIRST(&gp->lg_provider);
909 len = strlen(pp->lg_name);
911 len = strlen(gp->lg_name);
914 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
915 if (strcasecmp(conf->lg_name, "state") == 0) {
916 len = strlen(conf->lg_val);
917 if (*status_len < len)
924 status_one_consumer(struct gconsumer *cp)
926 static char buf[256];
927 struct gprovider *pp;
928 struct gconfig *conf;
930 pp = cp->lg_provider;
933 LIST_FOREACH(conf, &cp->lg_config, lg_config) {
934 if (strcasecmp(conf->lg_name, "synchronized") == 0)
938 snprintf(buf, sizeof(buf), "%s", pp->lg_name);
940 snprintf(buf, sizeof(buf), "%s (%s)", pp->lg_name,
947 status_one_geom(struct ggeom *gp, int script, int name_len, int status_len)
949 struct gprovider *pp;
950 struct gconsumer *cp;
951 struct gconfig *conf;
952 const char *name, *status, *component;
955 pp = LIST_FIRST(&gp->lg_provider);
960 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
961 if (strcasecmp(conf->lg_name, "state") == 0)
967 status = conf->lg_val;
969 LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
970 component = status_one_consumer(cp);
971 if (component == NULL)
974 printf("%*s %*s %s\n", name_len, name, status_len, status,
980 printf("%*s %*s %s\n", name_len, name, status_len, status,
986 std_status(struct gctl_req *req, unsigned flags __unused)
989 struct gclass *classp;
992 int name_len, status_len;
993 int error, i, n, nargs, script;
995 error = geom_gettree(&mesh);
997 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
998 classp = find_class(&mesh, gclass_name);
1000 errx(EXIT_FAILURE, "Class %s not found.", gclass_name);
1001 nargs = gctl_get_int(req, "nargs");
1002 script = gctl_get_int(req, "script");
1003 name_len = strlen("Name");
1004 status_len = strlen("Status");
1006 for (i = 0, n = 0; i < nargs; i++) {
1007 name = gctl_get_ascii(req, "arg%d", i);
1008 gp = find_geom(classp, name);
1010 errx(EXIT_FAILURE, "No such geom: %s.", name);
1012 status_update_len(gp, &name_len, &status_len);
1020 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
1021 if (LIST_EMPTY(&gp->lg_provider))
1023 status_update_len(gp, &name_len, &status_len);
1030 printf("%*s %*s %s\n", name_len, "Name", status_len, "Status",
1034 for (i = 0; i < nargs; i++) {
1035 name = gctl_get_ascii(req, "arg%d", i);
1036 gp = find_geom(classp, name);
1038 status_one_geom(gp, script, name_len,
1043 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
1044 if (LIST_EMPTY(&gp->lg_provider))
1046 status_one_geom(gp, script, name_len, status_len);
1050 geom_deletetree(&mesh);
1054 std_load_available(void)
1056 char name[MAXPATHLEN], paths[MAXPATHLEN * 8], *p;
1060 snprintf(name, sizeof(name), "g_%s", class_name);
1062 * If already in kernel, "load" command is not available.
1064 if (modfind(name) >= 0)
1066 bzero(paths, sizeof(paths));
1067 len = sizeof(paths);
1068 if (sysctlbyname("kern.module_path", paths, &len, NULL, 0) < 0)
1069 err(EXIT_FAILURE, "sysctl(kern.module_path)");
1070 for (p = strtok(paths, ";"); p != NULL; p = strtok(NULL, ";")) {
1071 snprintf(name, sizeof(name), "%s/geom_%s.ko", p, class_name);
1073 * If geom_<name>.ko file exists, "load" command is available.
1075 if (stat(name, &sb) == 0)
1082 std_load(struct gctl_req *req __unused, unsigned flags)
1086 * Do nothing special here, because of G_FLAG_LOADKLD flag,
1087 * module is already loaded.
1089 if ((flags & G_FLAG_VERBOSE) != 0)
1090 printf("Module available.\n");
1094 std_unload_available(void)
1099 snprintf(name, sizeof(name), "geom_%s", class_name);
1107 std_unload(struct gctl_req *req, unsigned flags __unused)
1112 snprintf(name, sizeof(name), "geom_%s", class_name);
1115 gctl_error(req, "Could not find module: %s.", strerror(errno));
1118 if (kldunload(id) < 0) {
1119 gctl_error(req, "Could not unload module: %s.",
1126 std_available(const char *name)
1129 if (strcmp(name, "help") == 0)
1131 else if (strcmp(name, "list") == 0)
1132 return (std_list_available());
1133 else if (strcmp(name, "status") == 0)
1134 return (std_status_available());
1135 else if (strcmp(name, "load") == 0)
1136 return (std_load_available());
1137 else if (strcmp(name, "unload") == 0)
1138 return (std_unload_available());
1140 assert(!"Unknown standard command.");