xref: /freebsd/sbin/geom/core/geom.c (revision 7afc53b8dfcc7d5897920ce6cc7e842fbb4ab813)
1 /*-
2  * Copyright (c) 2004-2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/linker.h>
32 #include <sys/module.h>
33 #include <sys/stat.h>
34 #include <sys/sysctl.h>
35 #include <ctype.h>
36 #include <err.h>
37 #include <errno.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <stdarg.h>
41 #include <stdint.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <libgen.h>
45 #include <libutil.h>
46 #include <inttypes.h>
47 #include <dlfcn.h>
48 #include <assert.h>
49 #include <libgeom.h>
50 #include <geom.h>
51 
52 #include "misc/subr.h"
53 
54 
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;
59 
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);
64 
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);
70 
71 struct g_command std_commands[] = {
72 	{ "help", 0, std_help, G_NULL_OPTS, NULL },
73 	{ "list", 0, std_list, G_NULL_OPTS,
74 	    "[name ...]"
75 	},
76 	{ "status", 0, std_status,
77 	    {
78 		{ 's', "script", NULL, G_TYPE_NONE },
79 		G_OPT_SENTINEL
80 	    },
81 	    "[-s] [name ...]"
82 	},
83 	{ "load", G_FLAG_VERBOSE | G_FLAG_LOADKLD, std_load, G_NULL_OPTS, NULL },
84 	{ "unload", G_FLAG_VERBOSE, std_unload, G_NULL_OPTS, NULL },
85 	G_CMD_SENTINEL
86 };
87 
88 static void
89 usage_command(struct g_command *cmd, const char *prefix)
90 {
91 	struct g_option *opt;
92 	unsigned i;
93 
94 	fprintf(stderr, "%s %s %s", prefix, comm, cmd->gc_name);
95 	if (cmd->gc_usage != NULL) {
96 		fprintf(stderr, " %s\n", cmd->gc_usage);
97 		return;
98 	}
99 	if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0)
100 		fprintf(stderr, " [-v]");
101 	for (i = 0; ; i++) {
102 		opt = &cmd->gc_options[i];
103 		if (opt->go_name == NULL)
104 			break;
105 		if (opt->go_val != NULL || opt->go_type == G_TYPE_NONE)
106 			fprintf(stderr, " [");
107 		else
108 			fprintf(stderr, " ");
109 		fprintf(stderr, "-%c", opt->go_char);
110 		if (opt->go_type != G_TYPE_NONE)
111 			fprintf(stderr, " %s", opt->go_name);
112 		if (opt->go_val != NULL || opt->go_type == G_TYPE_NONE)
113 			fprintf(stderr, "]");
114 	}
115 	fprintf(stderr, "\n");
116 }
117 
118 static void
119 usage(void)
120 {
121 
122 	if (class_name == NULL) {
123 		errx(EXIT_FAILURE, "usage: %s <class> <command> [options]",
124 		    "geom");
125 	} else {
126 		struct g_command *cmd;
127 		const char *prefix;
128 		unsigned i;
129 
130 		prefix = "usage:";
131 		if (class_commands != NULL) {
132 			for (i = 0; ; i++) {
133 				cmd = &class_commands[i];
134 				if (cmd->gc_name == NULL)
135 					break;
136 				usage_command(cmd, prefix);
137 				prefix = "      ";
138 			}
139 		}
140 		for (i = 0; ; i++) {
141 			cmd = &std_commands[i];
142 			if (cmd->gc_name == NULL)
143 				break;
144 			/*
145 			 * If class defines command, which has the same name as
146 			 * standard command, skip it, because it was already
147 			 * shown on usage().
148 			 */
149 			if (find_command(cmd->gc_name, GEOM_CLASS_CMDS) != NULL)
150 				continue;
151 			usage_command(cmd, prefix);
152 			prefix = "      ";
153 		}
154 		exit(EXIT_FAILURE);
155 	}
156 }
157 
158 static void
159 load_module(void)
160 {
161 	char name1[64], name2[64];
162 
163 	snprintf(name1, sizeof(name1), "g_%s", class_name);
164 	snprintf(name2, sizeof(name2), "geom_%s", class_name);
165 	if (modfind(name1) < 0) {
166 		/* Not present in kernel, try loading it. */
167 		if (kldload(name2) < 0 || modfind(name1) < 0) {
168 			if (errno != EEXIST) {
169 				errx(EXIT_FAILURE,
170 				    "%s module not available!", name2);
171 			}
172 		}
173 	}
174 }
175 
176 static int
177 strlcatf(char *str, size_t size, const char *format, ...)
178 {
179 	size_t len;
180 	va_list ap;
181 	int ret;
182 
183 	len = strlen(str);
184 	str += len;
185 	size -= len;
186 
187 	va_start(ap, format);
188 	ret = vsnprintf(str, size, format, ap);
189 	va_end(ap);
190 
191 	return (ret);
192 }
193 
194 /*
195  * Find given option in options available for given command.
196  */
197 static struct g_option *
198 find_option(struct g_command *cmd, char ch)
199 {
200 	struct g_option *opt;
201 	unsigned i;
202 
203 	for (i = 0; ; i++) {
204 		opt = &cmd->gc_options[i];
205 		if (opt->go_name == NULL)
206 			return (NULL);
207 		if (opt->go_char == ch)
208 			return (opt);
209 	}
210 	/* NOTREACHED */
211 	return (NULL);
212 }
213 
214 /*
215  * Add given option to gctl_req.
216  */
217 static void
218 set_option(struct gctl_req *req, struct g_option *opt, const char *val)
219 {
220 
221 	if (opt->go_type == G_TYPE_NUMBER) {
222 		intmax_t number;
223 
224 		errno = 0;
225 		number = strtoimax(optarg, NULL, 0);
226 		if (errno != 0) {
227 			err(EXIT_FAILURE, "Invalid value for '%c' argument.",
228 			    opt->go_char);
229 		}
230 		opt->go_val = malloc(sizeof(intmax_t));
231 		if (opt->go_val == NULL)
232 			errx(EXIT_FAILURE, "No memory.");
233 		*(intmax_t *)opt->go_val = number;
234 
235 		gctl_ro_param(req, opt->go_name, sizeof(intmax_t), opt->go_val);
236 	} else if (opt->go_type == G_TYPE_STRING) {
237 		gctl_ro_param(req, opt->go_name, -1, optarg);
238 	} else /* if (opt->go_type == G_TYPE_NONE) */ {
239 		opt->go_val = malloc(sizeof(int));
240 		if (opt->go_val == NULL)
241 			errx(EXIT_FAILURE, "No memory.");
242 		*(int *)opt->go_val = *val - '0';
243 
244 		gctl_ro_param(req, opt->go_name, sizeof(int),
245 		    opt->go_val);
246 	}
247 }
248 
249 /*
250  * 1. Add given argument by caller.
251  * 2. Add default values of not given arguments.
252  * 3. Add the rest of arguments.
253  */
254 static void
255 parse_arguments(struct g_command *cmd, struct gctl_req *req, int *argc,
256     char ***argv)
257 {
258 	struct g_option *opt;
259 	char opts[64];
260 	unsigned i;
261 	int ch;
262 
263 	*opts = '\0';
264 	if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0)
265 		strlcat(opts, "v", sizeof(opts));
266 	for (i = 0; ; i++) {
267 		opt = &cmd->gc_options[i];
268 		if (opt->go_name == NULL)
269 			break;
270 		strlcatf(opts, sizeof(opts), "%c", opt->go_char);
271 		if (opt->go_type != G_TYPE_NONE)
272 			strlcat(opts, ":", sizeof(opts));
273 	}
274 
275 	/*
276 	 * Add specified arguments.
277 	 */
278 	while ((ch = getopt(*argc, *argv, opts)) != -1) {
279 		/* Standard (not passed to kernel) options. */
280 		switch (ch) {
281 		case 'v':
282 			verbose = 1;
283 			continue;
284 		}
285 		/* Options passed to kernel. */
286 		opt = find_option(cmd, ch);
287 		if (opt == NULL)
288 			usage();
289 		if (G_OPT_ISDONE(opt)) {
290 			fprintf(stderr, "Flag '%c' specified twice.\n",
291 			    opt->go_char);
292 			usage();
293 		}
294 		G_OPT_DONE(opt);
295 
296 		if (opt->go_type == G_TYPE_NONE)
297 			set_option(req, opt, "1");
298 		else
299 			set_option(req, opt, optarg);
300 	}
301 	*argc -= optind;
302 	*argv += optind;
303 
304 	/*
305 	 * Add not specified arguments, but with default values.
306 	 */
307 	for (i = 0; ; i++) {
308 		opt = &cmd->gc_options[i];
309 		if (opt->go_name == NULL)
310 			break;
311 		if (G_OPT_ISDONE(opt))
312 			continue;
313 
314 		if (opt->go_type == G_TYPE_NONE) {
315 			assert(opt->go_val == NULL);
316 			set_option(req, opt, "0");
317 		} else {
318 			if (opt->go_val == NULL) {
319 				fprintf(stderr, "Flag '%c' not specified.\n",
320 				    opt->go_char);
321 				usage();
322 			} else {
323 				if (opt->go_type == G_TYPE_NUMBER) {
324 					gctl_ro_param(req, opt->go_name,
325 					    sizeof(intmax_t), opt->go_val);
326 				} else /* if (opt->go_type == G_TYPE_STRING)*/ {
327 					gctl_ro_param(req, opt->go_name, -1,
328 					    opt->go_val);
329 				}
330 			}
331 		}
332 	}
333 	/*
334 	 * Add rest of given arguments.
335 	 */
336 	gctl_ro_param(req, "nargs", sizeof(int), argc);
337 	for (i = 0; i < (unsigned)*argc; i++) {
338 		char argname[16];
339 
340 		snprintf(argname, sizeof(argname), "arg%u", i);
341 		gctl_ro_param(req, argname, -1, (*argv)[i]);
342 	}
343 }
344 
345 /*
346  * Find given command in commands available for given class.
347  */
348 static struct g_command *
349 find_command(const char *cmdstr, int flags)
350 {
351 	struct g_command *cmd;
352 	unsigned i;
353 
354 	/*
355 	 * First try to find command defined by loaded library.
356 	 */
357 	if ((flags & GEOM_CLASS_CMDS) != 0 && class_commands != NULL) {
358 		for (i = 0; ; i++) {
359 			cmd = &class_commands[i];
360 			if (cmd->gc_name == NULL)
361 				break;
362 			if (strcmp(cmd->gc_name, cmdstr) == 0)
363 				return (cmd);
364 		}
365 	}
366 	/*
367 	 * Now try to find in standard commands.
368 	 */
369 	if ((flags & GEOM_STD_CMDS) != 0) {
370 		for (i = 0; ; i++) {
371 			cmd = &std_commands[i];
372 			if (cmd->gc_name == NULL)
373 				break;
374 			if (strcmp(cmd->gc_name, cmdstr) == 0)
375 				return (cmd);
376 		}
377 	}
378 	return (NULL);
379 }
380 
381 static unsigned
382 set_flags(struct g_command *cmd)
383 {
384 	unsigned flags = 0;
385 
386 	if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0 && verbose)
387 		flags |= G_FLAG_VERBOSE;
388 
389 	return (flags);
390 }
391 
392 /*
393  * Run command.
394  */
395 static void
396 run_command(int argc, char *argv[])
397 {
398 	struct g_command *cmd;
399 	struct gctl_req *req;
400 	const char *errstr;
401 	char buf[4096];
402 
403 	/* First try to find a command defined by a class. */
404 	cmd = find_command(argv[0], GEOM_CLASS_CMDS);
405 	if (cmd == NULL) {
406 		/* Now, try to find a standard command. */
407 		cmd = find_command(argv[0], GEOM_STD_CMDS);
408 		if (cmd == NULL) {
409 			fprintf(stderr, "Unknown command: %s\n", argv[0]);
410 			usage();
411 		}
412 		if (!std_available(cmd->gc_name)) {
413 			fprintf(stderr, "Command '%s' not available.\n",
414 			    argv[0]);
415 			exit(EXIT_FAILURE);
416 		}
417 	}
418 	if ((cmd->gc_flags & G_FLAG_LOADKLD) != 0)
419 		load_module();
420 
421 	req = gctl_get_handle();
422 	gctl_ro_param(req, "class", -1, gclass_name);
423 	gctl_ro_param(req, "verb", -1, argv[0]);
424 	if (version != NULL)
425 		gctl_ro_param(req, "version", sizeof(*version), version);
426 	parse_arguments(cmd, req, &argc, &argv);
427 
428 	bzero(buf, sizeof(buf));
429 	if (cmd->gc_func != NULL) {
430 		unsigned flags;
431 
432 		flags = set_flags(cmd);
433 		cmd->gc_func(req, flags);
434 		errstr = req->error;
435 	} else {
436 		gctl_rw_param(req, "output", sizeof(buf), buf);
437 		errstr = gctl_issue(req);
438 	}
439 	if (errstr != NULL && errstr[0] != '\0') {
440 		fprintf(stderr, "%s\n", errstr);
441 		if (strncmp(errstr, "warning: ", strlen("warning: ")) != 0) {
442 			gctl_free(req);
443 			exit(EXIT_FAILURE);
444 		}
445 	}
446 	if (buf[0] != '\0')
447 		printf("%s", buf);
448 	gctl_free(req);
449 	if (verbose)
450 		printf("Done.\n");
451 	exit(EXIT_SUCCESS);
452 }
453 
454 static const char *
455 library_path(void)
456 {
457 	const char *path;
458 
459 	path = getenv("GEOM_LIBRARY_PATH");
460 	if (path == NULL)
461 		path = CLASS_DIR;
462 	return (path);
463 }
464 
465 static void
466 load_library(void)
467 {
468 	char path[MAXPATHLEN];
469 	uint32_t *lib_version;
470 	void *dlh;
471 
472 	snprintf(path, sizeof(path), "%s/geom_%s.so", library_path(),
473 	    class_name);
474 	dlh = dlopen(path, RTLD_NOW);
475 	if (dlh == NULL) {
476 #if 0
477 		fprintf(stderr, "Cannot open library %s, but continuing "
478 		    "anyway.\n", path);
479 #endif
480 		/*
481 		 * Even if library cannot be loaded, standard commands are
482 		 * available, so don't panic!
483 		 */
484 		return;
485 	}
486 	lib_version = dlsym(dlh, "lib_version");
487 	if (lib_version == NULL) {
488 		fprintf(stderr, "Cannot find symbol %s: %s.\n", "lib_version",
489 		    dlerror());
490 		dlclose(dlh);
491 		exit(EXIT_FAILURE);
492 	}
493 	if (*lib_version != G_LIB_VERSION) {
494 		dlclose(dlh);
495 		errx(EXIT_FAILURE, "%s and %s are not synchronized.",
496 		    getprogname(), path);
497 	}
498 	version = dlsym(dlh, "version");
499 	if (version == NULL) {
500 		fprintf(stderr, "Cannot find symbol %s: %s.\n", "version",
501 		    dlerror());
502 		dlclose(dlh);
503 		exit(EXIT_FAILURE);
504 	}
505 	class_commands = dlsym(dlh, "class_commands");
506 	if (class_commands == NULL) {
507 		fprintf(stderr, "Cannot find symbol %s: %s.\n",
508 		    "class_commands", dlerror());
509 		dlclose(dlh);
510 		exit(EXIT_FAILURE);
511 	}
512 }
513 
514 /*
515  * Class name should be all capital letters.
516  */
517 static void
518 set_class_name(void)
519 {
520 	char *s1, *s2;
521 
522 	s1 = class_name;
523 	for (; *s1 != '\0'; s1++)
524 		*s1 = tolower(*s1);
525 	gclass_name = malloc(strlen(class_name));
526 	if (gclass_name == NULL)
527 		errx(EXIT_FAILURE, "No memory");
528 	s1 = gclass_name;
529 	s2 = class_name;
530 	for (; *s2 != '\0'; s2++)
531 		*s1++ = toupper(*s2);
532 	*s1 = '\0';
533 }
534 
535 static void
536 get_class(int *argc, char ***argv)
537 {
538 
539 	snprintf(comm, sizeof(comm), "%s", basename((*argv)[0]));
540 	if (strcmp(comm, "geom") == 0) {
541 		if (*argc < 2)
542 			usage();
543 		else if (*argc == 2) {
544 			if (strcmp((*argv)[1], "-h") == 0 ||
545 			    strcmp((*argv)[1], "help") == 0) {
546 				usage();
547 			}
548 		}
549 		strlcatf(comm, sizeof(comm), " %s", (*argv)[1]);
550 		class_name = (*argv)[1];
551 		*argc -= 2;
552 		*argv += 2;
553 	} else if (*comm == 'g') {
554 		class_name = comm + 1;
555 		*argc -= 1;
556 		*argv += 1;
557 	} else {
558 		errx(EXIT_FAILURE, "Invalid utility name.");
559 	}
560 	set_class_name();
561 	load_library();
562 	if (*argc < 1)
563 		usage();
564 }
565 
566 int
567 main(int argc, char *argv[])
568 {
569 
570 	get_class(&argc, &argv);
571 	run_command(argc, argv);
572 	/* NOTREACHED */
573 
574 	exit(EXIT_FAILURE);
575 }
576 
577 static struct gclass *
578 find_class(struct gmesh *mesh, const char *name)
579 {
580 	struct gclass *classp;
581 
582 	LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
583 		if (strcmp(classp->lg_name, name) == 0)
584 			return (classp);
585 	}
586 	return (NULL);
587 }
588 
589 static struct ggeom *
590 find_geom(struct gclass *classp, const char *name)
591 {
592 	struct ggeom *gp;
593 
594 	LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
595 		if (strcmp(gp->lg_name, name) == 0)
596 			return (gp);
597 	}
598 	return (NULL);
599 }
600 
601 static void
602 list_one_provider(struct gprovider *pp, const char *prefix)
603 {
604 	struct gconfig *conf;
605 	char buf[5];
606 
607 	printf("Name: %s\n", pp->lg_name);
608 	humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "",
609 	    HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
610 	printf("%sMediasize: %jd (%s)\n", prefix, (intmax_t)pp->lg_mediasize,
611 	    buf);
612 	printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize);
613 	printf("%sMode: %s\n", prefix, pp->lg_mode);
614 	LIST_FOREACH(conf, &pp->lg_config, lg_config) {
615 		printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val);
616 	}
617 }
618 
619 static void
620 list_one_consumer(struct gconsumer *cp, const char *prefix)
621 {
622 	struct gprovider *pp;
623 	struct gconfig *conf;
624 
625 	pp = cp->lg_provider;
626 	if (pp == NULL)
627 		printf("[no provider]\n");
628 	else {
629 		char buf[5];
630 
631 		printf("Name: %s\n", pp->lg_name);
632 		humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "",
633 		    HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
634 		printf("%sMediasize: %jd (%s)\n", prefix,
635 		    (intmax_t)pp->lg_mediasize, buf);
636 		printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize);
637 		printf("%sMode: %s\n", prefix, cp->lg_mode);
638 	}
639 	LIST_FOREACH(conf, &cp->lg_config, lg_config) {
640 		printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val);
641 	}
642 }
643 
644 static void
645 list_one_geom(struct ggeom *gp)
646 {
647 	struct gprovider *pp;
648 	struct gconsumer *cp;
649 	struct gconfig *conf;
650 	unsigned n;
651 
652 	printf("Geom name: %s\n", gp->lg_name);
653 	LIST_FOREACH(conf, &gp->lg_config, lg_config) {
654 		printf("%s: %s\n", conf->lg_name, conf->lg_val);
655 	}
656 	if (!LIST_EMPTY(&gp->lg_provider)) {
657 		printf("Providers:\n");
658 		n = 1;
659 		LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
660 			printf("%u. ", n++);
661 			list_one_provider(pp, "   ");
662 		}
663 	}
664 	if (!LIST_EMPTY(&gp->lg_consumer)) {
665 		printf("Consumers:\n");
666 		n = 1;
667 		LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
668 			printf("%u. ", n++);
669 			list_one_consumer(cp, "   ");
670 		}
671 	}
672 	printf("\n");
673 }
674 
675 static void
676 std_help(struct gctl_req *req __unused, unsigned flags __unused)
677 {
678 
679 	usage();
680 }
681 
682 static int
683 std_list_available(void)
684 {
685 	struct gmesh mesh;
686 	struct gclass *classp;
687 	int error;
688 
689 	error = geom_gettree(&mesh);
690 	if (error != 0) {
691 		fprintf(stderr, "Cannot get GEOM tree: %s.\n", strerror(error));
692 		exit(EXIT_FAILURE);
693 	}
694 	classp = find_class(&mesh, gclass_name);
695 	geom_deletetree(&mesh);
696 	if (classp != NULL)
697 		return (1);
698 	return (0);
699 }
700 
701 static void
702 std_list(struct gctl_req *req, unsigned flags __unused)
703 {
704 	struct gmesh mesh;
705 	struct gclass *classp;
706 	struct ggeom *gp;
707 	int error, *nargs;
708 
709 	error = geom_gettree(&mesh);
710 	if (error != 0) {
711 		fprintf(stderr, "Cannot get GEOM tree: %s.\n", strerror(error));
712 		exit(EXIT_FAILURE);
713 	}
714 	classp = find_class(&mesh, gclass_name);
715 	if (classp == NULL) {
716 		geom_deletetree(&mesh);
717 		fprintf(stderr, "Class %s not found.\n", gclass_name);
718 		return;
719 	}
720 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
721 	if (nargs == NULL) {
722 		gctl_error(req, "No '%s' argument.", "nargs");
723 		geom_deletetree(&mesh);
724 		return;
725 	}
726 	if (*nargs > 0) {
727 		int i;
728 
729 		for (i = 0; i < *nargs; i++) {
730 			const char *name;
731 			char param[16];
732 
733 			snprintf(param, sizeof(param), "arg%d", i);
734 			name = gctl_get_asciiparam(req, param);
735 			assert(name != NULL);
736 			gp = find_geom(classp, name);
737 			if (gp != NULL)
738 				list_one_geom(gp);
739 			else
740 				fprintf(stderr, "No such geom: %s.\n", name);
741 		}
742 	} else {
743 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
744 			if (LIST_EMPTY(&gp->lg_provider))
745 				continue;
746 			list_one_geom(gp);
747 		}
748 	}
749 	geom_deletetree(&mesh);
750 }
751 
752 static int
753 std_status_available(void)
754 {
755 
756 	/* 'status' command is available when 'list' command is. */
757 	return (std_list_available());
758 }
759 
760 static void
761 status_update_len(struct ggeom *gp, int *name_len, int *status_len)
762 {
763 	struct gprovider *pp;
764 	struct gconfig *conf;
765 	int len;
766 
767 	assert(gp != NULL);
768 	assert(name_len != NULL);
769 	assert(status_len != NULL);
770 
771 	pp = LIST_FIRST(&gp->lg_provider);
772 	if (pp != NULL)
773 		len = strlen(pp->lg_name);
774 	else
775 		len = strlen(gp->lg_name);
776 	if (*name_len < len)
777 		*name_len = len;
778 	LIST_FOREACH(conf, &gp->lg_config, lg_config) {
779 		if (strcasecmp(conf->lg_name, "state") == 0) {
780 			len = strlen(conf->lg_val);
781 			if (*status_len < len)
782 				*status_len = len;
783 		}
784 	}
785 }
786 
787 static char *
788 status_one_consumer(struct gconsumer *cp)
789 {
790 	static char buf[256];
791 	struct gprovider *pp;
792 	struct gconfig *conf;
793 
794 	pp = cp->lg_provider;
795 	if (pp == NULL)
796 		return (NULL);
797 	LIST_FOREACH(conf, &cp->lg_config, lg_config) {
798 		if (strcasecmp(conf->lg_name, "synchronized") == 0)
799 			break;
800 	}
801 	if (conf == NULL)
802 		snprintf(buf, sizeof(buf), "%s", pp->lg_name);
803 	else {
804 		snprintf(buf, sizeof(buf), "%s (%s)", pp->lg_name,
805 		    conf->lg_val);
806 	}
807 	return (buf);
808 }
809 
810 static void
811 status_one_geom(struct ggeom *gp, int script, int name_len, int status_len)
812 {
813 	struct gprovider *pp;
814 	struct gconsumer *cp;
815 	struct gconfig *conf;
816 	const char *name, *status, *component;
817 	int gotone;
818 
819 	pp = LIST_FIRST(&gp->lg_provider);
820 	if (pp != NULL)
821 		name = pp->lg_name;
822 	else
823 		name = gp->lg_name;
824 	LIST_FOREACH(conf, &gp->lg_config, lg_config) {
825 		if (strcasecmp(conf->lg_name, "state") == 0)
826 			break;
827 	}
828 	if (conf == NULL)
829 		status = "N/A";
830 	else
831 		status = conf->lg_val;
832 	gotone = 0;
833 	LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
834 		component = status_one_consumer(cp);
835 		if (component == NULL)
836 			continue;
837 		gotone = 1;
838 		printf("%*s  %*s  %s\n", name_len, name, status_len, status,
839 		    component);
840 		if (!script)
841 			name = status = "";
842 	}
843 	if (!gotone) {
844 		printf("%*s  %*s  %s\n", name_len, name, status_len, status,
845 		    "N/A");
846 	}
847 }
848 
849 static void
850 std_status(struct gctl_req *req, unsigned flags __unused)
851 {
852 	struct gmesh mesh;
853 	struct gclass *classp;
854 	struct ggeom *gp;
855 	int name_len, status_len;
856 	int error, *nargs, *script;
857 
858 	error = geom_gettree(&mesh);
859 	if (error != 0) {
860 		fprintf(stderr, "Cannot get GEOM tree: %s.\n", strerror(error));
861 		exit(EXIT_FAILURE);
862 	}
863 	classp = find_class(&mesh, gclass_name);
864 	if (classp == NULL) {
865 		fprintf(stderr, "Class %s not found.\n", gclass_name);
866 		goto end;
867 	}
868 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
869 	if (nargs == NULL) {
870 		gctl_error(req, "No '%s' argument.", "nargs");
871 		goto end;
872 	}
873 	script = gctl_get_paraml(req, "script", sizeof(*script));
874 	if (script == NULL) {
875 		gctl_error(req, "No '%s' argument.", "script");
876 		goto end;
877 	}
878 	name_len = strlen("Name");
879 	status_len = strlen("Status");
880 	if (*nargs > 0) {
881 		int i, n = 0;
882 
883 		for (i = 0; i < *nargs; i++) {
884 			const char *name;
885 			char param[16];
886 
887 			snprintf(param, sizeof(param), "arg%d", i);
888 			name = gctl_get_asciiparam(req, param);
889 			assert(name != NULL);
890 			gp = find_geom(classp, name);
891 			if (gp == NULL)
892 				fprintf(stderr, "No such geom: %s.\n", name);
893 			else {
894 				status_update_len(gp, &name_len, &status_len);
895 				n++;
896 			}
897 		}
898 		if (n == 0)
899 			goto end;
900 	} else {
901 		int n = 0;
902 
903 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
904 			if (LIST_EMPTY(&gp->lg_provider))
905 				continue;
906 			status_update_len(gp, &name_len, &status_len);
907 			n++;
908 		}
909 		if (n == 0)
910 			goto end;
911 	}
912 	if (!*script) {
913 		printf("%*s  %*s  %s\n", name_len, "Name", status_len, "Status",
914 		    "Components");
915 	}
916 	if (*nargs > 0) {
917 		int i;
918 
919 		for (i = 0; i < *nargs; i++) {
920 			const char *name;
921 			char param[16];
922 
923 			snprintf(param, sizeof(param), "arg%d", i);
924 			name = gctl_get_asciiparam(req, param);
925 			assert(name != NULL);
926 			gp = find_geom(classp, name);
927 			if (gp != NULL) {
928 				status_one_geom(gp, *script, name_len,
929 				    status_len);
930 			}
931 		}
932 	} else {
933 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
934 			if (LIST_EMPTY(&gp->lg_provider))
935 				continue;
936 			status_one_geom(gp, *script, name_len, status_len);
937 		}
938 	}
939 end:
940 	geom_deletetree(&mesh);
941 }
942 
943 static int
944 std_load_available(void)
945 {
946 	char name[MAXPATHLEN], paths[MAXPATHLEN * 8], *p;
947 	struct stat sb;
948 	size_t len;
949 
950 	snprintf(name, sizeof(name), "g_%s", class_name);
951 	/*
952 	 * If already in kernel, "load" command is not available.
953 	 */
954 	if (modfind(name) >= 0)
955 		return (0);
956 	bzero(paths, sizeof(paths));
957 	len = sizeof(paths);
958 	if (sysctlbyname("kern.module_path", paths, &len, NULL, 0) < 0)
959 		err(EXIT_FAILURE, "sysctl(kern.module_path)");
960 	for (p = strtok(paths, ";"); p != NULL; p = strtok(NULL, ";")) {
961 		snprintf(name, sizeof(name), "%s/geom_%s.ko", p, class_name);
962 		/*
963 		 * If geom_<name>.ko file exists, "load" command is available.
964 		 */
965 		if (stat(name, &sb) == 0)
966 			return (1);
967 	}
968 	return (0);
969 }
970 
971 static void
972 std_load(struct gctl_req *req __unused, unsigned flags)
973 {
974 
975 	/*
976 	 * Do nothing special here, because of G_FLAG_LOADKLD flag,
977 	 * module is already loaded.
978 	 */
979 	if ((flags & G_FLAG_VERBOSE) != 0)
980 		printf("Module available.\n");
981 }
982 
983 static int
984 std_unload_available(void)
985 {
986 	char name[64];
987 	int id;
988 
989 	snprintf(name, sizeof(name), "geom_%s", class_name);
990 	id = kldfind(name);
991 	if (id >= 0)
992 		return (1);
993 	return (0);
994 }
995 
996 static void
997 std_unload(struct gctl_req *req, unsigned flags __unused)
998 {
999 	char name[64];
1000 	int id;
1001 
1002 	snprintf(name, sizeof(name), "geom_%s", class_name);
1003 	id = kldfind(name);
1004 	if (id < 0) {
1005 		gctl_error(req, "Could not find module: %s.", strerror(errno));
1006 		return;
1007 	}
1008 	if (kldunload(id) < 0) {
1009 		gctl_error(req, "Could not unload module: %s.",
1010 		    strerror(errno));
1011 		return;
1012 	}
1013 }
1014 
1015 static int
1016 std_available(const char *name)
1017 {
1018 
1019 	if (strcmp(name, "help") == 0)
1020 		return (1);
1021 	else if (strcmp(name, "list") == 0)
1022 		return (std_list_available());
1023 	else if (strcmp(name, "status") == 0)
1024 		return (std_status_available());
1025 	else if (strcmp(name, "load") == 0)
1026 		return (std_load_available());
1027 	else if (strcmp(name, "unload") == 0)
1028 		return (std_unload_available());
1029 	else
1030 		assert(!"Unknown standard command.");
1031 	return (0);
1032 }
1033