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"
55 static char comm[MAXPATHLEN], *class_name = NULL, *gclass_name = NULL;
56 static uint32_t *version = NULL;
57 static int verbose = 0;
58 static struct g_command *class_commands = NULL;
60 #define GEOM_CLASS_CMDS 0x01
61 #define GEOM_STD_CMDS 0x02
62 static struct g_command *find_command(const char *cmdstr, int flags);
63 static int std_available(const char *name);
65 static void std_help(struct gctl_req *req, unsigned flags);
66 static void std_list(struct gctl_req *req, unsigned flags);
67 static void std_status(struct gctl_req *req, unsigned flags);
68 static void std_load(struct gctl_req *req, unsigned flags);
69 static void std_unload(struct gctl_req *req, unsigned flags);
71 struct g_command std_commands[] = {
72 { "help", 0, std_help, G_NULL_OPTS, NULL, NULL },
73 { "list", 0, std_list, G_NULL_OPTS, NULL,
76 { "status", 0, std_status,
78 { 's', "script", NULL, G_TYPE_BOOL },
81 NULL, "[-s] [name ...]"
83 { "load", G_FLAG_VERBOSE | G_FLAG_LOADKLD, std_load, G_NULL_OPTS,
85 { "unload", G_FLAG_VERBOSE, std_unload, G_NULL_OPTS, NULL, NULL },
90 usage_command(struct g_command *cmd, const char *prefix)
95 fprintf(stderr, "%s %s %s", prefix, comm, cmd->gc_name);
96 if (cmd->gc_usage != NULL) {
97 fprintf(stderr, " %s\n", cmd->gc_usage);
100 if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0)
101 fprintf(stderr, " [-v]");
103 opt = &cmd->gc_options[i];
104 if (opt->go_name == NULL)
106 if (opt->go_val != NULL || G_OPT_TYPE(opt) == G_TYPE_BOOL)
107 fprintf(stderr, " [");
109 fprintf(stderr, " ");
110 fprintf(stderr, "-%c", opt->go_char);
111 if (G_OPT_TYPE(opt) != G_TYPE_BOOL)
112 fprintf(stderr, " %s", opt->go_name);
113 if (opt->go_val != NULL || G_OPT_TYPE(opt) == G_TYPE_BOOL)
114 fprintf(stderr, "]");
117 fprintf(stderr, " %s", cmd->gc_argname);
118 fprintf(stderr, "\n");
125 if (class_name == NULL) {
126 errx(EXIT_FAILURE, "usage: %s <class> <command> [options]",
129 struct g_command *cmd;
134 if (class_commands != NULL) {
136 cmd = &class_commands[i];
137 if (cmd->gc_name == NULL)
139 usage_command(cmd, prefix);
144 cmd = &std_commands[i];
145 if (cmd->gc_name == NULL)
148 * If class defines command, which has the same name as
149 * standard command, skip it, because it was already
152 if (find_command(cmd->gc_name, GEOM_CLASS_CMDS) != NULL)
154 usage_command(cmd, prefix);
164 char name1[64], name2[64];
166 snprintf(name1, sizeof(name1), "g_%s", class_name);
167 snprintf(name2, sizeof(name2), "geom_%s", class_name);
168 if (modfind(name1) < 0) {
169 /* Not present in kernel, try loading it. */
170 if (kldload(name2) < 0 || modfind(name1) < 0) {
171 if (errno != EEXIST) {
173 "%s module not available!", name2);
180 strlcatf(char *str, size_t size, const char *format, ...)
190 va_start(ap, format);
191 ret = vsnprintf(str, size, format, ap);
198 * Find given option in options available for given command.
200 static struct g_option *
201 find_option(struct g_command *cmd, char ch)
203 struct g_option *opt;
207 opt = &cmd->gc_options[i];
208 if (opt->go_name == NULL)
210 if (opt->go_char == ch)
218 * Add given option to gctl_req.
221 set_option(struct gctl_req *req, struct g_option *opt, const char *val)
224 if (G_OPT_TYPE(opt) == G_TYPE_NUMBER) {
227 if (expand_number(val, &number) == -1) {
228 err(EXIT_FAILURE, "Invalid value for '%c' argument.",
231 opt->go_val = malloc(sizeof(intmax_t));
232 if (opt->go_val == NULL)
233 errx(EXIT_FAILURE, "No memory.");
234 *(intmax_t *)opt->go_val = number;
236 gctl_ro_param(req, opt->go_name, sizeof(intmax_t), opt->go_val);
237 } else if (G_OPT_TYPE(opt) == G_TYPE_STRING) {
238 gctl_ro_param(req, opt->go_name, -1, val);
239 } else if (G_OPT_TYPE(opt) == G_TYPE_BOOL) {
240 opt->go_val = malloc(sizeof(int));
241 if (opt->go_val == NULL)
242 errx(EXIT_FAILURE, "No memory.");
243 *(int *)opt->go_val = *val - '0';
245 gctl_ro_param(req, opt->go_name, sizeof(int),
248 assert(!"Invalid type");
253 * 1. Add given argument by caller.
254 * 2. Add default values of not given arguments.
255 * 3. Add the rest of arguments.
258 parse_arguments(struct g_command *cmd, struct gctl_req *req, int *argc,
261 struct g_option *opt;
267 if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0)
268 strlcat(opts, "v", sizeof(opts));
270 opt = &cmd->gc_options[i];
271 if (opt->go_name == NULL)
273 assert(G_OPT_TYPE(opt) != 0);
274 assert((opt->go_type & ~G_TYPE_MASK) == 0);
275 strlcatf(opts, sizeof(opts), "%c", opt->go_char);
276 if (G_OPT_TYPE(opt) != G_TYPE_BOOL)
277 strlcat(opts, ":", sizeof(opts));
281 * Add specified arguments.
283 while ((ch = getopt(*argc, *argv, opts)) != -1) {
284 /* Standard (not passed to kernel) options. */
290 /* Options passed to kernel. */
291 opt = find_option(cmd, ch);
294 if (G_OPT_ISDONE(opt)) {
295 warnx("Option '%c' specified twice.", opt->go_char);
300 if (G_OPT_TYPE(opt) == G_TYPE_BOOL)
301 set_option(req, opt, "1");
303 set_option(req, opt, optarg);
309 * Add not specified arguments, but with default values.
312 opt = &cmd->gc_options[i];
313 if (opt->go_name == NULL)
315 if (G_OPT_ISDONE(opt))
318 if (G_OPT_TYPE(opt) == G_TYPE_BOOL) {
319 assert(opt->go_val == NULL);
320 set_option(req, opt, "0");
322 if (opt->go_val == NULL) {
323 warnx("Option '%c' not specified.",
327 if (G_OPT_TYPE(opt) == G_TYPE_NUMBER) {
328 gctl_ro_param(req, opt->go_name,
329 sizeof(intmax_t), opt->go_val);
330 } else if (G_OPT_TYPE(opt) == G_TYPE_STRING) {
331 if (cmd->gc_argname == NULL ||
332 opt->go_val == NULL ||
333 *(char *)opt->go_val != '\0')
334 gctl_ro_param(req, opt->go_name,
337 assert(!"Invalid type");
343 if (cmd->gc_argname == NULL) {
345 * Add rest of given arguments.
347 gctl_ro_param(req, "nargs", sizeof(int), argc);
348 for (i = 0; i < (unsigned)*argc; i++) {
351 snprintf(argname, sizeof(argname), "arg%u", i);
352 gctl_ro_param(req, argname, -1, (*argv)[i]);
357 gctl_ro_param(req, cmd->gc_argname, -1, (*argv)[0]);
362 * Find given command in commands available for given class.
364 static struct g_command *
365 find_command(const char *cmdstr, int flags)
367 struct g_command *cmd;
371 * First try to find command defined by loaded library.
373 if ((flags & GEOM_CLASS_CMDS) != 0 && class_commands != NULL) {
375 cmd = &class_commands[i];
376 if (cmd->gc_name == NULL)
378 if (strcmp(cmd->gc_name, cmdstr) == 0)
383 * Now try to find in standard commands.
385 if ((flags & GEOM_STD_CMDS) != 0) {
387 cmd = &std_commands[i];
388 if (cmd->gc_name == NULL)
390 if (strcmp(cmd->gc_name, cmdstr) == 0)
398 set_flags(struct g_command *cmd)
402 if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0 && verbose)
403 flags |= G_FLAG_VERBOSE;
412 run_command(int argc, char *argv[])
414 struct g_command *cmd;
415 struct gctl_req *req;
419 /* First try to find a command defined by a class. */
420 cmd = find_command(argv[0], GEOM_CLASS_CMDS);
422 /* Now, try to find a standard command. */
423 cmd = find_command(argv[0], GEOM_STD_CMDS);
425 warnx("Unknown command: %s.", argv[0]);
428 if (!std_available(cmd->gc_name)) {
429 warnx("Command '%s' not available.", argv[0]);
433 if ((cmd->gc_flags & G_FLAG_LOADKLD) != 0)
436 req = gctl_get_handle();
437 gctl_ro_param(req, "class", -1, gclass_name);
438 gctl_ro_param(req, "verb", -1, argv[0]);
440 gctl_ro_param(req, "version", sizeof(*version), version);
441 parse_arguments(cmd, req, &argc, &argv);
443 bzero(buf, sizeof(buf));
444 if (cmd->gc_func != NULL) {
447 flags = set_flags(cmd);
448 cmd->gc_func(req, flags);
451 gctl_rw_param(req, "output", sizeof(buf), buf);
452 errstr = gctl_issue(req);
454 if (errstr != NULL && errstr[0] != '\0') {
456 if (strncmp(errstr, "warning: ", strlen("warning: ")) != 0) {
474 path = getenv("GEOM_LIBRARY_PATH");
483 char *curpath, path[MAXPATHLEN], *tofree, *totalpath;
484 uint32_t *lib_version;
489 tofree = totalpath = strdup(library_path());
490 if (totalpath == NULL)
491 err(EXIT_FAILURE, "Not enough memory for library path");
493 if (strchr(totalpath, ':') != NULL)
494 curpath = strsep(&totalpath, ":");
497 /* Traverse the paths to find one that contains the library we want. */
498 while (curpath != NULL) {
499 snprintf(path, sizeof(path), "%s/geom_%s.so", curpath,
501 ret = access(path, F_OK);
503 if (errno == ENOENT) {
505 * If we cannot find library, try the next
508 curpath = strsep(&totalpath, ":");
511 err(EXIT_FAILURE, "Cannot access library");
516 /* No library was found, but standard commands can still be used */
519 dlh = dlopen(path, RTLD_NOW);
521 errx(EXIT_FAILURE, "Cannot open library: %s.", dlerror());
522 lib_version = dlsym(dlh, "lib_version");
523 if (lib_version == NULL) {
524 warnx("Cannot find symbol %s: %s.", "lib_version", dlerror());
528 if (*lib_version != G_LIB_VERSION) {
530 errx(EXIT_FAILURE, "%s and %s are not synchronized.",
531 getprogname(), path);
533 version = dlsym(dlh, "version");
534 if (version == NULL) {
535 warnx("Cannot find symbol %s: %s.", "version", dlerror());
539 class_commands = dlsym(dlh, "class_commands");
540 if (class_commands == NULL) {
541 warnx("Cannot find symbol %s: %s.", "class_commands",
549 * Class name should be all capital letters.
557 for (; *s1 != '\0'; s1++)
559 gclass_name = malloc(strlen(class_name) + 1);
560 if (gclass_name == NULL)
561 errx(EXIT_FAILURE, "No memory");
564 for (; *s2 != '\0'; s2++)
565 *s1++ = toupper(*s2);
570 get_class(int *argc, char ***argv)
573 snprintf(comm, sizeof(comm), "%s", basename((*argv)[0]));
574 if (strcmp(comm, "geom") == 0) {
577 else if (*argc == 2) {
578 if (strcmp((*argv)[1], "-h") == 0 ||
579 strcmp((*argv)[1], "help") == 0) {
583 strlcatf(comm, sizeof(comm), " %s", (*argv)[1]);
584 class_name = (*argv)[1];
587 } else if (*comm == 'g') {
588 class_name = comm + 1;
592 errx(EXIT_FAILURE, "Invalid utility name.");
601 main(int argc, char *argv[])
604 get_class(&argc, &argv);
605 run_command(argc, argv);
611 static struct gclass *
612 find_class(struct gmesh *mesh, const char *name)
614 struct gclass *classp;
616 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
617 if (strcmp(classp->lg_name, name) == 0)
623 static struct ggeom *
624 find_geom(struct gclass *classp, const char *name)
628 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
629 if (strcmp(gp->lg_name, name) == 0)
636 list_one_provider(struct gprovider *pp, const char *prefix)
638 struct gconfig *conf;
641 printf("Name: %s\n", pp->lg_name);
642 humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "",
643 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
644 printf("%sMediasize: %jd (%s)\n", prefix, (intmax_t)pp->lg_mediasize,
646 printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize);
647 printf("%sMode: %s\n", prefix, pp->lg_mode);
648 LIST_FOREACH(conf, &pp->lg_config, lg_config) {
649 printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val);
654 list_one_consumer(struct gconsumer *cp, const char *prefix)
656 struct gprovider *pp;
657 struct gconfig *conf;
659 pp = cp->lg_provider;
661 printf("[no provider]\n");
665 printf("Name: %s\n", pp->lg_name);
666 humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "",
667 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
668 printf("%sMediasize: %jd (%s)\n", prefix,
669 (intmax_t)pp->lg_mediasize, buf);
670 printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize);
671 printf("%sMode: %s\n", prefix, cp->lg_mode);
673 LIST_FOREACH(conf, &cp->lg_config, lg_config) {
674 printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val);
679 list_one_geom(struct ggeom *gp)
681 struct gprovider *pp;
682 struct gconsumer *cp;
683 struct gconfig *conf;
686 printf("Geom name: %s\n", gp->lg_name);
687 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
688 printf("%s: %s\n", conf->lg_name, conf->lg_val);
690 if (!LIST_EMPTY(&gp->lg_provider)) {
691 printf("Providers:\n");
693 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
695 list_one_provider(pp, " ");
698 if (!LIST_EMPTY(&gp->lg_consumer)) {
699 printf("Consumers:\n");
701 LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
703 list_one_consumer(cp, " ");
710 std_help(struct gctl_req *req __unused, unsigned flags __unused)
717 std_list_available(void)
720 struct gclass *classp;
723 error = geom_gettree(&mesh);
725 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
726 classp = find_class(&mesh, gclass_name);
727 geom_deletetree(&mesh);
734 std_list(struct gctl_req *req, unsigned flags __unused)
737 struct gclass *classp;
742 error = geom_gettree(&mesh);
744 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
745 classp = find_class(&mesh, gclass_name);
746 if (classp == NULL) {
747 geom_deletetree(&mesh);
748 errx(EXIT_FAILURE, "Class %s not found.", gclass_name);
750 nargs = gctl_get_int(req, "nargs");
752 for (i = 0; i < nargs; i++) {
753 name = gctl_get_ascii(req, "arg%d", i);
754 gp = find_geom(classp, name);
758 errx(EXIT_FAILURE, "No such geom: %s.", name);
761 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
762 if (LIST_EMPTY(&gp->lg_provider))
767 geom_deletetree(&mesh);
771 std_status_available(void)
774 /* 'status' command is available when 'list' command is. */
775 return (std_list_available());
779 status_update_len(struct ggeom *gp, int *name_len, int *status_len)
781 struct gprovider *pp;
782 struct gconfig *conf;
786 assert(name_len != NULL);
787 assert(status_len != NULL);
789 pp = LIST_FIRST(&gp->lg_provider);
791 len = strlen(pp->lg_name);
793 len = strlen(gp->lg_name);
796 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
797 if (strcasecmp(conf->lg_name, "state") == 0) {
798 len = strlen(conf->lg_val);
799 if (*status_len < len)
806 status_one_consumer(struct gconsumer *cp)
808 static char buf[256];
809 struct gprovider *pp;
810 struct gconfig *conf;
812 pp = cp->lg_provider;
815 LIST_FOREACH(conf, &cp->lg_config, lg_config) {
816 if (strcasecmp(conf->lg_name, "synchronized") == 0)
820 snprintf(buf, sizeof(buf), "%s", pp->lg_name);
822 snprintf(buf, sizeof(buf), "%s (%s)", pp->lg_name,
829 status_one_geom(struct ggeom *gp, int script, int name_len, int status_len)
831 struct gprovider *pp;
832 struct gconsumer *cp;
833 struct gconfig *conf;
834 const char *name, *status, *component;
837 pp = LIST_FIRST(&gp->lg_provider);
842 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
843 if (strcasecmp(conf->lg_name, "state") == 0)
849 status = conf->lg_val;
851 LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
852 component = status_one_consumer(cp);
853 if (component == NULL)
856 printf("%*s %*s %s\n", name_len, name, status_len, status,
862 printf("%*s %*s %s\n", name_len, name, status_len, status,
868 std_status(struct gctl_req *req, unsigned flags __unused)
871 struct gclass *classp;
874 int name_len, status_len;
875 int error, i, n, nargs, script;
877 error = geom_gettree(&mesh);
879 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
880 classp = find_class(&mesh, gclass_name);
882 errx(EXIT_FAILURE, "Class %s not found.", gclass_name);
883 nargs = gctl_get_int(req, "nargs");
884 script = gctl_get_int(req, "script");
885 name_len = strlen("Name");
886 status_len = strlen("Status");
888 for (i = 0, n = 0; i < nargs; i++) {
889 name = gctl_get_ascii(req, "arg%d", i);
890 gp = find_geom(classp, name);
892 errx(EXIT_FAILURE, "No such geom: %s.", name);
894 status_update_len(gp, &name_len, &status_len);
902 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
903 if (LIST_EMPTY(&gp->lg_provider))
905 status_update_len(gp, &name_len, &status_len);
912 printf("%*s %*s %s\n", name_len, "Name", status_len, "Status",
916 for (i = 0; i < nargs; i++) {
917 name = gctl_get_ascii(req, "arg%d", i);
918 gp = find_geom(classp, name);
920 status_one_geom(gp, script, name_len,
925 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
926 if (LIST_EMPTY(&gp->lg_provider))
928 status_one_geom(gp, script, name_len, status_len);
932 geom_deletetree(&mesh);
936 std_load_available(void)
938 char name[MAXPATHLEN], paths[MAXPATHLEN * 8], *p;
942 snprintf(name, sizeof(name), "g_%s", class_name);
944 * If already in kernel, "load" command is not available.
946 if (modfind(name) >= 0)
948 bzero(paths, sizeof(paths));
950 if (sysctlbyname("kern.module_path", paths, &len, NULL, 0) < 0)
951 err(EXIT_FAILURE, "sysctl(kern.module_path)");
952 for (p = strtok(paths, ";"); p != NULL; p = strtok(NULL, ";")) {
953 snprintf(name, sizeof(name), "%s/geom_%s.ko", p, class_name);
955 * If geom_<name>.ko file exists, "load" command is available.
957 if (stat(name, &sb) == 0)
964 std_load(struct gctl_req *req __unused, unsigned flags)
968 * Do nothing special here, because of G_FLAG_LOADKLD flag,
969 * module is already loaded.
971 if ((flags & G_FLAG_VERBOSE) != 0)
972 printf("Module available.\n");
976 std_unload_available(void)
981 snprintf(name, sizeof(name), "geom_%s", class_name);
989 std_unload(struct gctl_req *req, unsigned flags __unused)
994 snprintf(name, sizeof(name), "geom_%s", class_name);
997 gctl_error(req, "Could not find module: %s.", strerror(errno));
1000 if (kldunload(id) < 0) {
1001 gctl_error(req, "Could not unload module: %s.",
1008 std_available(const char *name)
1011 if (strcmp(name, "help") == 0)
1013 else if (strcmp(name, "list") == 0)
1014 return (std_list_available());
1015 else if (strcmp(name, "status") == 0)
1016 return (std_status_available());
1017 else if (strcmp(name, "load") == 0)
1018 return (std_load_available());
1019 else if (strcmp(name, "unload") == 0)
1020 return (std_unload_available());
1022 assert(!"Unknown standard command.");