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 extern uint32_t gpart_version;
56 extern struct g_command gpart_class_commands[];
59 static char comm[MAXPATHLEN], *class_name = NULL, *gclass_name = NULL;
60 static uint32_t *version = NULL;
61 static int verbose = 0;
62 static struct g_command *class_commands = NULL;
64 #define GEOM_CLASS_CMDS 0x01
65 #define GEOM_STD_CMDS 0x02
66 static struct g_command *find_command(const char *cmdstr, int flags);
67 static int std_available(const char *name);
69 static void std_help(struct gctl_req *req, unsigned flags);
70 static void std_list(struct gctl_req *req, unsigned flags);
71 static void std_status(struct gctl_req *req, unsigned flags);
72 static void std_load(struct gctl_req *req, unsigned flags);
73 static void std_unload(struct gctl_req *req, unsigned flags);
75 struct g_command std_commands[] = {
76 { "help", 0, std_help, G_NULL_OPTS, NULL, NULL },
77 { "list", 0, std_list, G_NULL_OPTS, NULL,
80 { "status", 0, std_status,
82 { 's', "script", NULL, G_TYPE_BOOL },
85 NULL, "[-s] [name ...]"
87 { "load", G_FLAG_VERBOSE | G_FLAG_LOADKLD, std_load, G_NULL_OPTS,
89 { "unload", G_FLAG_VERBOSE, std_unload, G_NULL_OPTS, NULL, NULL },
94 usage_command(struct g_command *cmd, const char *prefix)
99 fprintf(stderr, "%s %s %s", prefix, comm, cmd->gc_name);
100 if (cmd->gc_usage != NULL) {
101 fprintf(stderr, " %s\n", cmd->gc_usage);
104 if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0)
105 fprintf(stderr, " [-v]");
107 opt = &cmd->gc_options[i];
108 if (opt->go_name == NULL)
110 if (opt->go_val != NULL || G_OPT_TYPE(opt) == G_TYPE_BOOL)
111 fprintf(stderr, " [");
113 fprintf(stderr, " ");
114 fprintf(stderr, "-%c", opt->go_char);
115 if (G_OPT_TYPE(opt) != G_TYPE_BOOL)
116 fprintf(stderr, " %s", opt->go_name);
117 if (opt->go_val != NULL || G_OPT_TYPE(opt) == G_TYPE_BOOL)
118 fprintf(stderr, "]");
121 fprintf(stderr, " %s", cmd->gc_argname);
122 fprintf(stderr, "\n");
129 if (class_name == NULL) {
130 errx(EXIT_FAILURE, "usage: %s <class> <command> [options]",
133 struct g_command *cmd;
138 if (class_commands != NULL) {
140 cmd = &class_commands[i];
141 if (cmd->gc_name == NULL)
143 usage_command(cmd, prefix);
148 cmd = &std_commands[i];
149 if (cmd->gc_name == NULL)
152 * If class defines command, which has the same name as
153 * standard command, skip it, because it was already
156 if (find_command(cmd->gc_name, GEOM_CLASS_CMDS) != NULL)
158 usage_command(cmd, prefix);
168 char name1[64], name2[64];
170 snprintf(name1, sizeof(name1), "g_%s", class_name);
171 snprintf(name2, sizeof(name2), "geom_%s", class_name);
172 if (modfind(name1) < 0) {
173 /* Not present in kernel, try loading it. */
174 if (kldload(name2) < 0 || modfind(name1) < 0) {
175 if (errno != EEXIST) {
177 "%s module not available!", name2);
184 strlcatf(char *str, size_t size, const char *format, ...)
194 va_start(ap, format);
195 ret = vsnprintf(str, size, format, ap);
202 * Find given option in options available for given command.
204 static struct g_option *
205 find_option(struct g_command *cmd, char ch)
207 struct g_option *opt;
211 opt = &cmd->gc_options[i];
212 if (opt->go_name == NULL)
214 if (opt->go_char == ch)
222 * Add given option to gctl_req.
225 set_option(struct gctl_req *req, struct g_option *opt, const char *val)
228 if (G_OPT_TYPE(opt) == G_TYPE_NUMBER) {
231 if (expand_number(val, &number) == -1) {
232 err(EXIT_FAILURE, "Invalid value for '%c' argument.",
235 opt->go_val = malloc(sizeof(intmax_t));
236 if (opt->go_val == NULL)
237 errx(EXIT_FAILURE, "No memory.");
238 *(intmax_t *)opt->go_val = number;
240 gctl_ro_param(req, opt->go_name, sizeof(intmax_t), opt->go_val);
241 } else if (G_OPT_TYPE(opt) == G_TYPE_STRING) {
242 gctl_ro_param(req, opt->go_name, -1, val);
243 } else if (G_OPT_TYPE(opt) == G_TYPE_BOOL) {
244 opt->go_val = malloc(sizeof(int));
245 if (opt->go_val == NULL)
246 errx(EXIT_FAILURE, "No memory.");
247 *(int *)opt->go_val = *val - '0';
249 gctl_ro_param(req, opt->go_name, sizeof(int),
252 assert(!"Invalid type");
257 * 1. Add given argument by caller.
258 * 2. Add default values of not given arguments.
259 * 3. Add the rest of arguments.
262 parse_arguments(struct g_command *cmd, struct gctl_req *req, int *argc,
265 struct g_option *opt;
271 if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0)
272 strlcat(opts, "v", sizeof(opts));
274 opt = &cmd->gc_options[i];
275 if (opt->go_name == NULL)
277 assert(G_OPT_TYPE(opt) != 0);
278 assert((opt->go_type & ~G_TYPE_MASK) == 0);
279 strlcatf(opts, sizeof(opts), "%c", opt->go_char);
280 if (G_OPT_TYPE(opt) != G_TYPE_BOOL)
281 strlcat(opts, ":", sizeof(opts));
285 * Add specified arguments.
287 while ((ch = getopt(*argc, *argv, opts)) != -1) {
288 /* Standard (not passed to kernel) options. */
294 /* Options passed to kernel. */
295 opt = find_option(cmd, ch);
298 if (G_OPT_ISDONE(opt)) {
299 warnx("Option '%c' specified twice.", opt->go_char);
304 if (G_OPT_TYPE(opt) == G_TYPE_BOOL)
305 set_option(req, opt, "1");
307 set_option(req, opt, optarg);
313 * Add not specified arguments, but with default values.
316 opt = &cmd->gc_options[i];
317 if (opt->go_name == NULL)
319 if (G_OPT_ISDONE(opt))
322 if (G_OPT_TYPE(opt) == G_TYPE_BOOL) {
323 assert(opt->go_val == NULL);
324 set_option(req, opt, "0");
326 if (opt->go_val == NULL) {
327 warnx("Option '%c' not specified.",
331 if (G_OPT_TYPE(opt) == G_TYPE_NUMBER) {
332 gctl_ro_param(req, opt->go_name,
333 sizeof(intmax_t), opt->go_val);
334 } else if (G_OPT_TYPE(opt) == G_TYPE_STRING) {
335 if (cmd->gc_argname == NULL ||
336 opt->go_val == NULL ||
337 *(char *)opt->go_val != '\0')
338 gctl_ro_param(req, opt->go_name,
341 assert(!"Invalid type");
347 if (cmd->gc_argname == NULL) {
349 * Add rest of given arguments.
351 gctl_ro_param(req, "nargs", sizeof(int), argc);
352 for (i = 0; i < (unsigned)*argc; i++) {
355 snprintf(argname, sizeof(argname), "arg%u", i);
356 gctl_ro_param(req, argname, -1, (*argv)[i]);
361 gctl_ro_param(req, cmd->gc_argname, -1, (*argv)[0]);
366 * Find given command in commands available for given class.
368 static struct g_command *
369 find_command(const char *cmdstr, int flags)
371 struct g_command *cmd;
375 * First try to find command defined by loaded library.
377 if ((flags & GEOM_CLASS_CMDS) != 0 && class_commands != NULL) {
379 cmd = &class_commands[i];
380 if (cmd->gc_name == NULL)
382 if (strcmp(cmd->gc_name, cmdstr) == 0)
387 * Now try to find in standard commands.
389 if ((flags & GEOM_STD_CMDS) != 0) {
391 cmd = &std_commands[i];
392 if (cmd->gc_name == NULL)
394 if (strcmp(cmd->gc_name, cmdstr) == 0)
402 set_flags(struct g_command *cmd)
406 if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0 && verbose)
407 flags |= G_FLAG_VERBOSE;
416 run_command(int argc, char *argv[])
418 struct g_command *cmd;
419 struct gctl_req *req;
423 /* First try to find a command defined by a class. */
424 cmd = find_command(argv[0], GEOM_CLASS_CMDS);
426 /* Now, try to find a standard command. */
427 cmd = find_command(argv[0], GEOM_STD_CMDS);
429 warnx("Unknown command: %s.", argv[0]);
432 if (!std_available(cmd->gc_name)) {
433 warnx("Command '%s' not available.", argv[0]);
437 if ((cmd->gc_flags & G_FLAG_LOADKLD) != 0)
440 req = gctl_get_handle();
441 gctl_ro_param(req, "class", -1, gclass_name);
442 gctl_ro_param(req, "verb", -1, argv[0]);
444 gctl_ro_param(req, "version", sizeof(*version), version);
445 parse_arguments(cmd, req, &argc, &argv);
447 bzero(buf, sizeof(buf));
448 if (cmd->gc_func != NULL) {
451 flags = set_flags(cmd);
452 cmd->gc_func(req, flags);
455 gctl_rw_param(req, "output", sizeof(buf), buf);
456 errstr = gctl_issue(req);
458 if (errstr != NULL && errstr[0] != '\0') {
460 if (strncmp(errstr, "warning: ", strlen("warning: ")) != 0) {
479 path = getenv("GEOM_LIBRARY_PATH");
488 char *curpath, path[MAXPATHLEN], *totalpath;
489 uint32_t *lib_version;
494 totalpath = strdup(library_path());
495 if (totalpath == NULL)
496 err(EXIT_FAILURE, "Not enough memory for library path");
498 if (strchr(totalpath, ':') != NULL)
499 curpath = strsep(&totalpath, ":");
502 /* Traverse the paths to find one that contains the library we want. */
503 while (curpath != NULL) {
504 snprintf(path, sizeof(path), "%s/geom_%s.so", curpath,
506 ret = access(path, F_OK);
508 if (errno == ENOENT) {
510 * If we cannot find library, try the next
513 curpath = strsep(&totalpath, ":");
516 err(EXIT_FAILURE, "Cannot access library");
521 /* No library was found, but standard commands can still be used */
524 dlh = dlopen(path, RTLD_NOW);
526 errx(EXIT_FAILURE, "Cannot open library: %s.", dlerror());
527 lib_version = dlsym(dlh, "lib_version");
528 if (lib_version == NULL) {
529 warnx("Cannot find symbol %s: %s.", "lib_version", dlerror());
533 if (*lib_version != G_LIB_VERSION) {
535 errx(EXIT_FAILURE, "%s and %s are not synchronized.",
536 getprogname(), path);
538 version = dlsym(dlh, "version");
539 if (version == NULL) {
540 warnx("Cannot find symbol %s: %s.", "version", dlerror());
544 class_commands = dlsym(dlh, "class_commands");
545 if (class_commands == NULL) {
546 warnx("Cannot find symbol %s: %s.", "class_commands",
555 * Class name should be all capital letters.
563 for (; *s1 != '\0'; s1++)
565 gclass_name = malloc(strlen(class_name) + 1);
566 if (gclass_name == NULL)
567 errx(EXIT_FAILURE, "No memory");
570 for (; *s2 != '\0'; s2++)
571 *s1++ = toupper(*s2);
576 get_class(int *argc, char ***argv)
579 snprintf(comm, sizeof(comm), "%s", basename((*argv)[0]));
580 if (strcmp(comm, "geom") == 0) {
583 else if (*argc == 2) {
584 if (strcmp((*argv)[1], "-h") == 0 ||
585 strcmp((*argv)[1], "help") == 0) {
589 strlcatf(comm, sizeof(comm), " %s", (*argv)[1]);
590 class_name = (*argv)[1];
593 } else if (*comm == 'g') {
594 class_name = comm + 1;
598 errx(EXIT_FAILURE, "Invalid utility name.");
604 if (!strcasecmp(class_name, "part")) {
605 version = &gpart_version;
606 class_commands = gpart_class_commands;
608 errx(EXIT_FAILURE, "Invalid class name.");
617 main(int argc, char *argv[])
620 get_class(&argc, &argv);
621 run_command(argc, argv);
627 static struct gclass *
628 find_class(struct gmesh *mesh, const char *name)
630 struct gclass *classp;
632 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
633 if (strcmp(classp->lg_name, name) == 0)
639 static struct ggeom *
640 find_geom(struct gclass *classp, const char *name)
644 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
645 if (strcmp(gp->lg_name, name) == 0)
652 list_one_provider(struct gprovider *pp, const char *prefix)
654 struct gconfig *conf;
657 printf("Name: %s\n", pp->lg_name);
658 humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "",
659 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
660 printf("%sMediasize: %jd (%s)\n", prefix, (intmax_t)pp->lg_mediasize,
662 printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize);
663 printf("%sMode: %s\n", prefix, pp->lg_mode);
664 LIST_FOREACH(conf, &pp->lg_config, lg_config) {
665 printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val);
670 list_one_consumer(struct gconsumer *cp, const char *prefix)
672 struct gprovider *pp;
673 struct gconfig *conf;
675 pp = cp->lg_provider;
677 printf("[no provider]\n");
681 printf("Name: %s\n", pp->lg_name);
682 humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "",
683 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
684 printf("%sMediasize: %jd (%s)\n", prefix,
685 (intmax_t)pp->lg_mediasize, buf);
686 printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize);
687 printf("%sMode: %s\n", prefix, cp->lg_mode);
689 LIST_FOREACH(conf, &cp->lg_config, lg_config) {
690 printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val);
695 list_one_geom(struct ggeom *gp)
697 struct gprovider *pp;
698 struct gconsumer *cp;
699 struct gconfig *conf;
702 printf("Geom name: %s\n", gp->lg_name);
703 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
704 printf("%s: %s\n", conf->lg_name, conf->lg_val);
706 if (!LIST_EMPTY(&gp->lg_provider)) {
707 printf("Providers:\n");
709 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
711 list_one_provider(pp, " ");
714 if (!LIST_EMPTY(&gp->lg_consumer)) {
715 printf("Consumers:\n");
717 LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
719 list_one_consumer(cp, " ");
726 std_help(struct gctl_req *req __unused, unsigned flags __unused)
733 std_list_available(void)
736 struct gclass *classp;
739 error = geom_gettree(&mesh);
741 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
742 classp = find_class(&mesh, gclass_name);
743 geom_deletetree(&mesh);
750 std_list(struct gctl_req *req, unsigned flags __unused)
753 struct gclass *classp;
758 error = geom_gettree(&mesh);
760 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
761 classp = find_class(&mesh, gclass_name);
762 if (classp == NULL) {
763 geom_deletetree(&mesh);
764 errx(EXIT_FAILURE, "Class %s not found.", gclass_name);
766 nargs = gctl_get_int(req, "nargs");
768 for (i = 0; i < nargs; i++) {
769 name = gctl_get_ascii(req, "arg%d", i);
770 gp = find_geom(classp, name);
774 errx(EXIT_FAILURE, "No such geom: %s.", name);
777 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
778 if (LIST_EMPTY(&gp->lg_provider))
783 geom_deletetree(&mesh);
787 std_status_available(void)
790 /* 'status' command is available when 'list' command is. */
791 return (std_list_available());
795 status_update_len(struct ggeom *gp, int *name_len, int *status_len)
797 struct gprovider *pp;
798 struct gconfig *conf;
802 assert(name_len != NULL);
803 assert(status_len != NULL);
805 pp = LIST_FIRST(&gp->lg_provider);
807 len = strlen(pp->lg_name);
809 len = strlen(gp->lg_name);
812 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
813 if (strcasecmp(conf->lg_name, "state") == 0) {
814 len = strlen(conf->lg_val);
815 if (*status_len < len)
822 status_one_consumer(struct gconsumer *cp)
824 static char buf[256];
825 struct gprovider *pp;
826 struct gconfig *conf;
828 pp = cp->lg_provider;
831 LIST_FOREACH(conf, &cp->lg_config, lg_config) {
832 if (strcasecmp(conf->lg_name, "synchronized") == 0)
836 snprintf(buf, sizeof(buf), "%s", pp->lg_name);
838 snprintf(buf, sizeof(buf), "%s (%s)", pp->lg_name,
845 status_one_geom(struct ggeom *gp, int script, int name_len, int status_len)
847 struct gprovider *pp;
848 struct gconsumer *cp;
849 struct gconfig *conf;
850 const char *name, *status, *component;
853 pp = LIST_FIRST(&gp->lg_provider);
858 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
859 if (strcasecmp(conf->lg_name, "state") == 0)
865 status = conf->lg_val;
867 LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
868 component = status_one_consumer(cp);
869 if (component == NULL)
872 printf("%*s %*s %s\n", name_len, name, status_len, status,
878 printf("%*s %*s %s\n", name_len, name, status_len, status,
884 std_status(struct gctl_req *req, unsigned flags __unused)
887 struct gclass *classp;
890 int name_len, status_len;
891 int error, i, n, nargs, script;
893 error = geom_gettree(&mesh);
895 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
896 classp = find_class(&mesh, gclass_name);
898 errx(EXIT_FAILURE, "Class %s not found.", gclass_name);
899 nargs = gctl_get_int(req, "nargs");
900 script = gctl_get_int(req, "script");
901 name_len = strlen("Name");
902 status_len = strlen("Status");
904 for (i = 0, n = 0; i < nargs; i++) {
905 name = gctl_get_ascii(req, "arg%d", i);
906 gp = find_geom(classp, name);
908 errx(EXIT_FAILURE, "No such geom: %s.", name);
910 status_update_len(gp, &name_len, &status_len);
918 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
919 if (LIST_EMPTY(&gp->lg_provider))
921 status_update_len(gp, &name_len, &status_len);
928 printf("%*s %*s %s\n", name_len, "Name", status_len, "Status",
932 for (i = 0; i < nargs; i++) {
933 name = gctl_get_ascii(req, "arg%d", i);
934 gp = find_geom(classp, name);
936 status_one_geom(gp, script, name_len,
941 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
942 if (LIST_EMPTY(&gp->lg_provider))
944 status_one_geom(gp, script, name_len, status_len);
948 geom_deletetree(&mesh);
952 std_load_available(void)
954 char name[MAXPATHLEN], paths[MAXPATHLEN * 8], *p;
958 snprintf(name, sizeof(name), "g_%s", class_name);
960 * If already in kernel, "load" command is not available.
962 if (modfind(name) >= 0)
964 bzero(paths, sizeof(paths));
966 if (sysctlbyname("kern.module_path", paths, &len, NULL, 0) < 0)
967 err(EXIT_FAILURE, "sysctl(kern.module_path)");
968 for (p = strtok(paths, ";"); p != NULL; p = strtok(NULL, ";")) {
969 snprintf(name, sizeof(name), "%s/geom_%s.ko", p, class_name);
971 * If geom_<name>.ko file exists, "load" command is available.
973 if (stat(name, &sb) == 0)
980 std_load(struct gctl_req *req __unused, unsigned flags)
984 * Do nothing special here, because of G_FLAG_LOADKLD flag,
985 * module is already loaded.
987 if ((flags & G_FLAG_VERBOSE) != 0)
988 printf("Module available.\n");
992 std_unload_available(void)
997 snprintf(name, sizeof(name), "geom_%s", class_name);
1005 std_unload(struct gctl_req *req, unsigned flags __unused)
1010 snprintf(name, sizeof(name), "geom_%s", class_name);
1013 gctl_error(req, "Could not find module: %s.", strerror(errno));
1016 if (kldunload(id) < 0) {
1017 gctl_error(req, "Could not unload module: %s.",
1024 std_available(const char *name)
1027 if (strcmp(name, "help") == 0)
1029 else if (strcmp(name, "list") == 0)
1030 return (std_list_available());
1031 else if (strcmp(name, "status") == 0)
1032 return (std_status_available());
1033 else if (strcmp(name, "load") == 0)
1034 return (std_load_available());
1035 else if (strcmp(name, "unload") == 0)
1036 return (std_unload_available());
1038 assert(!"Unknown standard command.");