xref: /freebsd/sbin/geom/core/geom.c (revision 3bad06e9baeb48e351b485cc8cc7045c870d0cae)
1 /*-
2  * Copyright (c) 2004 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 static void (*usage)(const char *name);
60 
61 #define	GEOM_CLASS_CMDS	0x01
62 #define	GEOM_STD_CMDS	0x02
63 static struct g_command *find_command(const char *cmdstr, int flags);
64 static int std_available(const char *name);
65 
66 static void std_help(struct gctl_req *req, unsigned flags);
67 static void std_list(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 },
73 	{ "list", 0, std_list, G_NULL_OPTS },
74 	{ "load", G_FLAG_VERBOSE | G_FLAG_LOADKLD, std_load, G_NULL_OPTS },
75 	{ "unload", G_FLAG_VERBOSE, std_unload, G_NULL_OPTS },
76 	G_CMD_SENTINEL
77 };
78 
79 static void
80 std_usage(const char *name)
81 {
82 	struct g_command *cmd;
83 	struct g_option *opt;
84 	unsigned i, j;
85 
86 	for (i = 0; ; i++) {
87 		cmd = &class_commands[i];
88 		if (cmd->gc_name == NULL)
89 			break;
90 		fprintf(stderr, "%s %s %s %s", i == 0 ? "usage:" : "      ",
91 		    name, class_name, cmd->gc_name);
92 		if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0)
93 			fprintf(stderr, " [-v]");
94 		for (j = 0; ; j++) {
95 			opt = &cmd->gc_options[j];
96 			if (opt->go_name == NULL)
97 				break;
98 			if (opt->go_val != NULL || opt->go_type == G_TYPE_NONE)
99 				fprintf(stderr, " [");
100 			else
101 				fprintf(stderr, " ");
102 			fprintf(stderr, "-%c", opt->go_char);
103 			if (opt->go_type != G_TYPE_NONE)
104 				fprintf(stderr, " %s", opt->go_name);
105 			if (opt->go_val != NULL || opt->go_type == G_TYPE_NONE)
106 				fprintf(stderr, "]");
107 		}
108 		fprintf(stderr, " ...\n");
109 	}
110 	exit(EXIT_FAILURE);
111 }
112 
113 static void
114 geom_usage(void)
115 {
116 
117 	if (class_name == NULL) {
118 		errx(EXIT_FAILURE, "usage: %s <class> <command> [options]",
119 		    "geom");
120 	} else {
121 		const char *prefix;
122 		unsigned i;
123 
124 		if (usage == NULL)
125 			prefix = "usage:";
126 		else {
127 			usage(comm);
128 			prefix = "      ";
129 		}
130 		for (i = 0; ; i++) {
131 			struct g_command *cmd;
132 
133 			cmd = &std_commands[i];
134 			if (cmd->gc_name == NULL)
135 				break;
136 			/*
137 			 * If class defines command, which has the same name as
138 			 * standard command, skip it, because it was already
139 			 * shown on usage().
140 			 */
141 			if (find_command(cmd->gc_name, GEOM_CLASS_CMDS) != NULL)
142 				continue;
143 			fprintf(stderr, "%s %s %s", prefix, comm, cmd->gc_name);
144 			if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0)
145 				fprintf(stderr, " [-v]");
146 			fprintf(stderr, "\n");
147 			prefix = "      ";
148 		}
149 		exit(EXIT_FAILURE);
150 	}
151 }
152 
153 static void
154 load_module(void)
155 {
156 	char name1[64], name2[64];
157 
158 	snprintf(name1, sizeof(name1), "g_%s", class_name);
159 	snprintf(name2, sizeof(name2), "geom_%s", class_name);
160 	if (modfind(name1) < 0) {
161 		/* Not present in kernel, try loading it. */
162 		if (kldload(name2) < 0 || modfind(name1) < 0) {
163 			if (errno != EEXIST) {
164 				errx(EXIT_FAILURE,
165 				    "%s module not available!", name2);
166 			}
167 		}
168 	}
169 }
170 
171 static int
172 strlcatf(char *str, size_t size, const char *format, ...)
173 {
174 	size_t len;
175 	va_list ap;
176 	int ret;
177 
178 	len = strlen(str);
179 	str += len;
180 	size -= len;
181 
182 	va_start(ap, format);
183 	ret = vsnprintf(str, size, format, ap);
184 	va_end(ap);
185 
186 	return (ret);
187 }
188 
189 /*
190  * Find given option in options available for given command.
191  */
192 static struct g_option *
193 find_option(struct g_command *cmd, char ch)
194 {
195 	struct g_option *opt;
196 	unsigned i;
197 
198 	for (i = 0; ; i++) {
199 		opt = &cmd->gc_options[i];
200 		if (opt->go_name == NULL)
201 			return (NULL);
202 		if (opt->go_char == ch)
203 			return (opt);
204 	}
205 	/* NOTREACHED */
206 	return (NULL);
207 }
208 
209 /*
210  * Add given option to gctl_req.
211  */
212 static void
213 set_option(struct gctl_req *req, struct g_option *opt, const char *val)
214 {
215 
216 	if (opt->go_type == G_TYPE_NUMBER) {
217 		intmax_t number;
218 
219 		errno = 0;
220 		number = strtoimax(optarg, NULL, 0);
221 		if (errno != 0) {
222 			err(EXIT_FAILURE, "Invalid value for '%c' argument.",
223 			    opt->go_char);
224 		}
225 		opt->go_val = malloc(sizeof(intmax_t));
226 		if (opt->go_val == NULL)
227 			errx(EXIT_FAILURE, "No memory.");
228 		*(intmax_t *)opt->go_val = number;
229 
230 		gctl_ro_param(req, opt->go_name, sizeof(intmax_t), opt->go_val);
231 	} else if (opt->go_type == G_TYPE_STRING) {
232 		gctl_ro_param(req, opt->go_name, -1, optarg);
233 	} else /* if (opt->go_type == G_TYPE_NONE) */ {
234 		opt->go_val = malloc(sizeof(int));
235 		if (opt->go_val == NULL)
236 			errx(EXIT_FAILURE, "No memory.");
237 		*(int *)opt->go_val = *val - '0';
238 
239 		gctl_ro_param(req, opt->go_name, sizeof(int),
240 		    opt->go_val);
241 	}
242 }
243 
244 /*
245  * 1. Add given argument by caller.
246  * 2. Add default values of not given arguments.
247  * 3. Add the rest of arguments.
248  */
249 static void
250 parse_arguments(struct g_command *cmd, struct gctl_req *req, int *argc,
251     char ***argv)
252 {
253 	struct g_option *opt;
254 	char opts[64];
255 	unsigned i;
256 	int ch;
257 
258 	*opts = '\0';
259 	if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0)
260 		strlcat(opts, "v", sizeof(opts));
261 	for (i = 0; ; i++) {
262 		opt = &cmd->gc_options[i];
263 		if (opt->go_name == NULL)
264 			break;
265 		strlcatf(opts, sizeof(opts), "%c", opt->go_char);
266 		if (opt->go_type != G_TYPE_NONE)
267 			strlcat(opts, ":", sizeof(opts));
268 	}
269 
270 	/*
271 	 * Add specified arguments.
272 	 */
273 	while ((ch = getopt(*argc, *argv, opts)) != -1) {
274 		/* Standard (not passed to kernel) options. */
275 		switch (ch) {
276 		case 'v':
277 			verbose = 1;
278 			continue;
279 		}
280 		/* Options passed to kernel. */
281 		opt = find_option(cmd, ch);
282 		if (opt == NULL)
283 			geom_usage();
284 		if (G_OPT_ISDONE(opt)) {
285 			fprintf(stderr, "Flag '%c' specified twice.\n",
286 			    opt->go_char);
287 			geom_usage();
288 		}
289 		G_OPT_DONE(opt);
290 
291 		if (opt->go_type == G_TYPE_NONE)
292 			set_option(req, opt, "1");
293 		else
294 			set_option(req, opt, optarg);
295 	}
296 	*argc -= optind;
297 	*argv += optind;
298 
299 	/*
300 	 * Add not specified arguments, but with default values.
301 	 */
302 	for (i = 0; ; i++) {
303 		opt = &cmd->gc_options[i];
304 		if (opt->go_name == NULL)
305 			break;
306 		if (G_OPT_ISDONE(opt))
307 			continue;
308 
309 		if (opt->go_type == G_TYPE_NONE) {
310 			assert(opt->go_val == NULL);
311 			set_option(req, opt, "0");
312 		} else {
313 			if (opt->go_val == NULL) {
314 				fprintf(stderr, "Flag '%c' not specified.\n",
315 				    opt->go_char);
316 				geom_usage();
317 			} else {
318 				if (opt->go_type == G_TYPE_NUMBER) {
319 					gctl_ro_param(req, opt->go_name,
320 					    sizeof(intmax_t), opt->go_val);
321 				} else /* if (opt->go_type == G_TYPE_STRING)*/ {
322 					gctl_ro_param(req, opt->go_name, -1,
323 					    opt->go_val);
324 				}
325 			}
326 		}
327 	}
328 	/*
329 	 * Add rest of given arguments.
330 	 */
331 	gctl_ro_param(req, "nargs", sizeof(int), argc);
332 	for (i = 0; i < (unsigned)*argc; i++) {
333 		char argname[16];
334 
335 		snprintf(argname, sizeof(argname), "arg%u", i);
336 		gctl_ro_param(req, argname, -1, (*argv)[i]);
337 	}
338 }
339 
340 /*
341  * Find given command in commands available for given class.
342  */
343 static struct g_command *
344 find_command(const char *cmdstr, int flags)
345 {
346 	struct g_command *cmd;
347 	unsigned i;
348 
349 	/*
350 	 * First try to find command defined by loaded library.
351 	 */
352 	if ((flags & GEOM_CLASS_CMDS) != 0 && class_commands != NULL) {
353 		for (i = 0; ; i++) {
354 			cmd = &class_commands[i];
355 			if (cmd->gc_name == NULL)
356 				break;
357 			if (strcmp(cmd->gc_name, cmdstr) == 0)
358 				return (cmd);
359 		}
360 	}
361 	/*
362 	 * Now try to find in standard commands.
363 	 */
364 	if ((flags & GEOM_STD_CMDS) != 0) {
365 		for (i = 0; ; i++) {
366 			cmd = &std_commands[i];
367 			if (cmd->gc_name == NULL)
368 				break;
369 			if (strcmp(cmd->gc_name, cmdstr) == 0)
370 				return (cmd);
371 		}
372 	}
373 	return (NULL);
374 }
375 
376 static unsigned
377 set_flags(struct g_command *cmd)
378 {
379 	unsigned flags = 0;
380 
381 	if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0 && verbose)
382 		flags |= G_FLAG_VERBOSE;
383 
384 	return (flags);
385 }
386 
387 /*
388  * Run command.
389  */
390 static void
391 run_command(int argc, char *argv[])
392 {
393 	struct g_command *cmd;
394 	struct gctl_req *req;
395 	const char *errstr;
396 	char buf[4096];
397 
398 	/* First try to find a command defined by a class. */
399 	cmd = find_command(argv[0], GEOM_CLASS_CMDS);
400 	if (cmd == NULL) {
401 		/* Now, try to find a standard command. */
402 		cmd = find_command(argv[0], GEOM_STD_CMDS);
403 		if (cmd == NULL) {
404 			fprintf(stderr, "Unknown command: %s\n", argv[0]);
405 			geom_usage();
406 		}
407 		if (!std_available(cmd->gc_name)) {
408 			fprintf(stderr, "Command '%s' not available.\n",
409 			    argv[0]);
410 			exit(EXIT_FAILURE);
411 		}
412 	}
413 	if ((cmd->gc_flags & G_FLAG_LOADKLD) != 0)
414 		load_module();
415 
416 	req = gctl_get_handle();
417 	gctl_ro_param(req, "class", -1, gclass_name);
418 	gctl_ro_param(req, "verb", -1, argv[0]);
419 	if (version != NULL)
420 		gctl_ro_param(req, "version", sizeof(*version), version);
421 	parse_arguments(cmd, req, &argc, &argv);
422 
423 	if (cmd->gc_func != NULL) {
424 		unsigned flags;
425 
426 		flags = set_flags(cmd);
427 		cmd->gc_func(req, flags);
428 		errstr = req->error;
429 	} else {
430 		bzero(buf, sizeof(buf));
431 		gctl_rw_param(req, "output", sizeof(buf), buf);
432 		errstr = gctl_issue(req);
433 	}
434 	if (errstr != NULL) {
435 		fprintf(stderr, "%s\n", errstr);
436 		if (strncmp(errstr, "warning: ", strlen("warning: ")) != 0) {
437 			gctl_free(req);
438 			exit(EXIT_FAILURE);
439 		}
440 	}
441 	if (*buf != '\0')
442 		printf("%s", buf);
443 	gctl_free(req);
444 	if (verbose)
445 		printf("Done.\n");
446 	exit(EXIT_SUCCESS);
447 }
448 
449 static const char *
450 library_path(void)
451 {
452 	const char *path;
453 
454 	path = getenv("GEOM_LIBRARY_PATH");
455 	if (path == NULL)
456 		path = CLASS_DIR;
457 	return (path);
458 }
459 
460 static void
461 load_library(void)
462 {
463 	char path[MAXPATHLEN];
464 	uint32_t *lib_version;
465 	void *dlh;
466 
467 	snprintf(path, sizeof(path), "%s/geom_%s.so", library_path(),
468 	    class_name);
469 	dlh = dlopen(path, RTLD_NOW);
470 	if (dlh == NULL) {
471 #if 0
472 		fprintf(stderr, "Cannot open library %s, but continuing "
473 		    "anyway.\n", path);
474 #endif
475 		/*
476 		 * Even if library cannot be loaded, standard commands are
477 		 * available, so don't panic!
478 		 */
479 		return;
480 	}
481 	lib_version = dlsym(dlh, "lib_version");
482 	if (lib_version == NULL) {
483 		fprintf(stderr, "Cannot find symbol %s: %s.\n", "lib_version",
484 		    dlerror());
485 		dlclose(dlh);
486 		exit(EXIT_FAILURE);
487 	}
488 	if (*lib_version != G_LIB_VERSION) {
489 		dlclose(dlh);
490 		errx(EXIT_FAILURE, "%s and %s are not synchronized.",
491 		    getprogname(), path);
492 	}
493 	version = dlsym(dlh, "version");
494 	if (version == NULL) {
495 		fprintf(stderr, "Cannot find symbol %s: %s.\n", "version",
496 		    dlerror());
497 		dlclose(dlh);
498 		exit(EXIT_FAILURE);
499 	}
500 	class_commands = dlsym(dlh, "class_commands");
501 	if (class_commands == NULL) {
502 		fprintf(stderr, "Cannot find symbol %s: %s.\n",
503 		    "class_commands", dlerror());
504 		dlclose(dlh);
505 		exit(EXIT_FAILURE);
506 	}
507 	usage = dlsym(dlh, "usage");
508 	if (usage == NULL)
509 		usage = std_usage;
510 }
511 
512 /*
513  * Class name should be all capital letters.
514  */
515 static void
516 set_class_name(void)
517 {
518 	char *s1, *s2;
519 
520 	gclass_name = malloc(strlen(class_name));
521 	if (gclass_name == NULL)
522 		errx(EXIT_FAILURE, "No memory");
523 	s1 = gclass_name;
524 	s2 = class_name;
525 	for (; *s2 != '\0'; s2++)
526 		*s1++ = toupper(*s2);
527 	*s1 = '\0';
528 }
529 
530 static void
531 get_class(int *argc, char ***argv)
532 {
533 
534 	snprintf(comm, sizeof(comm), "%s", basename((*argv)[0]));
535 	if (strcmp(comm, "geom") == 0) {
536 		if (*argc < 2)
537 			geom_usage();
538 		else if (*argc == 2) {
539 			if (strcmp((*argv)[1], "-h") == 0 ||
540 			    strcmp((*argv)[1], "help") == 0) {
541 				geom_usage();
542 			}
543 		}
544 		strlcatf(comm, sizeof(comm), " %s", (*argv)[1]);
545 		class_name = (*argv)[1];
546 		*argc -= 2;
547 		*argv += 2;
548 	} else if (*comm == 'g') {
549 		class_name = comm + 1;
550 		*argc -= 1;
551 		*argv += 1;
552 	} else {
553 		errx(EXIT_FAILURE, "Invalid utility name.");
554 	}
555 	set_class_name();
556 	load_library();
557 	if (*argc < 1)
558 		geom_usage();
559 }
560 
561 int
562 main(int argc, char *argv[])
563 {
564 
565 	get_class(&argc, &argv);
566 	run_command(argc, argv);
567 	/* NOTREACHED */
568 
569 	exit(EXIT_FAILURE);
570 }
571 
572 static struct gclass *
573 find_class(struct gmesh *mesh, const char *name)
574 {
575 	struct gclass *classp;
576 
577 	LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
578 		if (strcmp(classp->lg_name, name) == 0)
579 			return (classp);
580 	}
581 	return (NULL);
582 }
583 
584 static struct ggeom *
585 find_geom(struct gclass *classp, const char *name)
586 {
587 	struct ggeom *gp;
588 
589 	LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
590 		if (strcmp(gp->lg_name, name) == 0)
591 			return (gp);
592 	}
593 	return (NULL);
594 }
595 
596 static void
597 show_one_provider(struct gprovider *pp, const char *prefix)
598 {
599 	struct gconfig *conf;
600 	char buf[5];
601 
602 	printf("Name: %s\n", pp->lg_name);
603 	humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "",
604 	    HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
605 	printf("%sMediasize: %jd (%s)\n", prefix, (intmax_t)pp->lg_mediasize,
606 	    buf);
607 	printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize);
608 	printf("%sMode: %s\n", prefix, pp->lg_mode);
609 	LIST_FOREACH(conf, &pp->lg_config, lg_config) {
610 		printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val);
611 	}
612 }
613 
614 static void
615 show_one_consumer(struct gconsumer *cp, const char *prefix)
616 {
617 	struct gprovider *pp;
618 	struct gconfig *conf;
619 
620 	pp = cp->lg_provider;
621 	if (pp == NULL)
622 		printf("[no provider]\n");
623 	else {
624 		char buf[5];
625 
626 		printf("Name: %s\n", pp->lg_name);
627 		humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "",
628 		    HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
629 		printf("%sMediasize: %jd (%s)\n", prefix,
630 		    (intmax_t)pp->lg_mediasize, buf);
631 		printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize);
632 		printf("%sMode: %s\n", prefix, cp->lg_mode);
633 	}
634 	LIST_FOREACH(conf, &cp->lg_config, lg_config) {
635 		printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val);
636 	}
637 }
638 
639 static void
640 show_one_geom(struct ggeom *gp)
641 {
642 	struct gprovider *pp;
643 	struct gconsumer *cp;
644 	struct gconfig *conf;
645 	unsigned n;
646 
647 	printf("Geom name: %s\n", gp->lg_name);
648 	LIST_FOREACH(conf, &gp->lg_config, lg_config) {
649 		printf("%s: %s\n", conf->lg_name, conf->lg_val);
650 	}
651 	if (!LIST_EMPTY(&gp->lg_provider)) {
652 		printf("Providers:\n");
653 		n = 1;
654 		LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
655 			printf("%u. ", n++);
656 			show_one_provider(pp, "   ");
657 		}
658 	}
659 	if (!LIST_EMPTY(&gp->lg_consumer)) {
660 		printf("Consumers:\n");
661 		n = 1;
662 		LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
663 			printf("%u. ", n++);
664 			show_one_consumer(cp, "   ");
665 		}
666 	}
667 	printf("\n");
668 }
669 
670 static void
671 std_help(struct gctl_req *req __unused, unsigned flags __unused)
672 {
673 
674 	geom_usage();
675 }
676 
677 static int
678 std_list_available(void)
679 {
680 	struct gmesh mesh;
681 	struct gclass *classp;
682 	int error;
683 
684 	error = geom_gettree(&mesh);
685 	if (error != 0) {
686 		fprintf(stderr, "Cannot get GEOM tree: %s.\n", strerror(error));
687 		exit(EXIT_FAILURE);
688 	}
689 	classp = find_class(&mesh, gclass_name);
690 	geom_deletetree(&mesh);
691 	if (classp != NULL)
692 		return (1);
693 	return (0);
694 }
695 
696 static void
697 std_list(struct gctl_req *req, unsigned flags __unused)
698 {
699 	struct gmesh mesh;
700 	struct gclass *classp;
701 	struct ggeom *gp;
702 	int error, *nargs;
703 
704 	error = geom_gettree(&mesh);
705 	if (error != 0) {
706 		fprintf(stderr, "Cannot get GEOM tree: %s.\n", strerror(error));
707 		exit(EXIT_FAILURE);
708 	}
709 	classp = find_class(&mesh, gclass_name);
710 	if (classp == NULL) {
711 		geom_deletetree(&mesh);
712 		fprintf(stderr, "Class %s not found.\n", gclass_name);
713 		return;
714 	}
715 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
716 	if (nargs == NULL) {
717 		gctl_error(req, "No '%s' argument.", "nargs");
718 		geom_deletetree(&mesh);
719 		return;
720 	}
721 	if (*nargs > 0) {
722 		int i;
723 
724 		for (i = 0; i < *nargs; i++) {
725 			const char *name;
726 			char param[16];
727 
728 			snprintf(param, sizeof(param), "arg%d", i);
729 			name = gctl_get_asciiparam(req, param);
730 			assert(name != NULL);
731 			gp = find_geom(classp, name);
732 			if (gp != NULL)
733 				show_one_geom(gp);
734 			else
735 				fprintf(stderr, "No such geom: %s.\n", name);
736 		}
737 	} else {
738 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
739 			show_one_geom(gp);
740 		}
741 	}
742 	geom_deletetree(&mesh);
743 }
744 
745 static int
746 std_load_available(void)
747 {
748 	char name[MAXPATHLEN], paths[MAXPATHLEN * 8], *p;
749 	struct stat sb;
750 	size_t len;
751 
752 	snprintf(name, sizeof(name), "g_%s", class_name);
753 	/*
754 	 * If already in kernel, "load" command is not available.
755 	 */
756 	if (modfind(name) >= 0)
757 		return (0);
758 	bzero(paths, sizeof(paths));
759 	len = sizeof(paths);
760 	if (sysctlbyname("kern.module_path", paths, &len, NULL, 0) < 0)
761 		err(EXIT_FAILURE, "sysctl(kern.module_path)");
762 	for (p = strtok(paths, ";"); p != NULL; p = strtok(NULL, ";")) {
763 		snprintf(name, sizeof(name), "%s/geom_%s.ko", p, class_name);
764 		/*
765 		 * If geom_<name>.ko file exists, "load" command is available.
766 		 */
767 		if (stat(name, &sb) == 0)
768 			return (1);
769 	}
770 	return (0);
771 }
772 
773 static void
774 std_load(struct gctl_req *req __unused, unsigned flags)
775 {
776 
777 	/*
778 	 * Do nothing special here, because of G_FLAG_LOADKLD flag,
779 	 * module is already loaded.
780 	 */
781 	if ((flags & G_FLAG_VERBOSE) != 0)
782 		printf("Module available.\n");
783 }
784 
785 static int
786 std_unload_available(void)
787 {
788 	char name[64];
789 	int id;
790 
791 	snprintf(name, sizeof(name), "geom_%s", class_name);
792 	id = kldfind(name);
793 	if (id >= 0)
794 		return (1);
795 	return (0);
796 }
797 
798 static void
799 std_unload(struct gctl_req *req, unsigned flags __unused)
800 {
801 	char name[64];
802 	int id;
803 
804 	snprintf(name, sizeof(name), "geom_%s", class_name);
805 	id = kldfind(name);
806 	if (id < 0) {
807 		gctl_error(req, "Could not find module: %s.", strerror(errno));
808 		return;
809 	}
810 	if (kldunload(id) < 0) {
811 		gctl_error(req, "Could not unload module: %s.",
812 		    strerror(errno));
813 		return;
814 	}
815 }
816 
817 static int
818 std_available(const char *name)
819 {
820 
821 	if (strcmp(name, "help") == 0)
822 		return (1);
823 	else if (strcmp(name, "list") == 0)
824 		return (std_list_available());
825 	else if (strcmp(name, "load") == 0)
826 		return (std_load_available());
827 	else if (strcmp(name, "unload") == 0)
828 		return (std_unload_available());
829 	else
830 		assert(!"Unknown standard command.");
831 	return (0);
832 }
833