xref: /freebsd/sbin/geom/core/geom.c (revision f200cc255f8e90b1905d94d0506a22f5dcd467c0)
105c91076SPawel Jakub Dawidek /*-
21de7b4b8SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
31de7b4b8SPedro F. Giffuni  *
487070efbSPawel Jakub Dawidek  * Copyright (c) 2004-2009 Pawel Jakub Dawidek <pjd@FreeBSD.org>
505c91076SPawel Jakub Dawidek  * All rights reserved.
605c91076SPawel Jakub Dawidek  *
705c91076SPawel Jakub Dawidek  * Redistribution and use in source and binary forms, with or without
805c91076SPawel Jakub Dawidek  * modification, are permitted provided that the following conditions
905c91076SPawel Jakub Dawidek  * are met:
1005c91076SPawel Jakub Dawidek  * 1. Redistributions of source code must retain the above copyright
1105c91076SPawel Jakub Dawidek  *    notice, this list of conditions and the following disclaimer.
1205c91076SPawel Jakub Dawidek  * 2. Redistributions in binary form must reproduce the above copyright
1305c91076SPawel Jakub Dawidek  *    notice, this list of conditions and the following disclaimer in the
1405c91076SPawel Jakub Dawidek  *    documentation and/or other materials provided with the distribution.
1505c91076SPawel Jakub Dawidek  *
1605c91076SPawel Jakub Dawidek  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
1705c91076SPawel Jakub Dawidek  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1805c91076SPawel Jakub Dawidek  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1905c91076SPawel Jakub Dawidek  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
2005c91076SPawel Jakub Dawidek  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2105c91076SPawel Jakub Dawidek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2205c91076SPawel Jakub Dawidek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2305c91076SPawel Jakub Dawidek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2405c91076SPawel Jakub Dawidek  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2505c91076SPawel Jakub Dawidek  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2605c91076SPawel Jakub Dawidek  * SUCH DAMAGE.
2705c91076SPawel Jakub Dawidek  */
2805c91076SPawel Jakub Dawidek 
2905c91076SPawel Jakub Dawidek #include <sys/cdefs.h>
3005c91076SPawel Jakub Dawidek __FBSDID("$FreeBSD$");
3105c91076SPawel Jakub Dawidek 
3205c91076SPawel Jakub Dawidek #include <sys/param.h>
3305c91076SPawel Jakub Dawidek #include <sys/linker.h>
3405c91076SPawel Jakub Dawidek #include <sys/module.h>
3505c91076SPawel Jakub Dawidek #include <sys/stat.h>
3605c91076SPawel Jakub Dawidek #include <sys/sysctl.h>
3705c91076SPawel Jakub Dawidek #include <ctype.h>
3805c91076SPawel Jakub Dawidek #include <err.h>
3905c91076SPawel Jakub Dawidek #include <errno.h>
40*f200cc25SRobert Wing #include <paths.h>
4105c91076SPawel Jakub Dawidek #include <stdio.h>
4205c91076SPawel Jakub Dawidek #include <stdlib.h>
4305c91076SPawel Jakub Dawidek #include <stdarg.h>
440f73f701SEdward Tomasz Napierala #include <stdbool.h>
4505c91076SPawel Jakub Dawidek #include <stdint.h>
4605c91076SPawel Jakub Dawidek #include <string.h>
4705c91076SPawel Jakub Dawidek #include <unistd.h>
4805c91076SPawel Jakub Dawidek #include <libgen.h>
49af565b58SPawel Jakub Dawidek #include <libutil.h>
5005c91076SPawel Jakub Dawidek #include <inttypes.h>
5105c91076SPawel Jakub Dawidek #include <dlfcn.h>
5205c91076SPawel Jakub Dawidek #include <assert.h>
5305c91076SPawel Jakub Dawidek #include <libgeom.h>
5405c91076SPawel Jakub Dawidek #include <geom.h>
5505c91076SPawel Jakub Dawidek 
5605c91076SPawel Jakub Dawidek #include "misc/subr.h"
5705c91076SPawel Jakub Dawidek 
5814bf405bSMarcel Moolenaar #ifdef STATIC_GEOM_CLASSES
59a16f9b36SMarcel Moolenaar extern uint32_t gpart_version;
60a16f9b36SMarcel Moolenaar extern struct g_command gpart_class_commands[];
61bc69d66fSXin LI extern uint32_t glabel_version;
62bc69d66fSXin LI extern struct g_command glabel_class_commands[];
63a16f9b36SMarcel Moolenaar #endif
6405c91076SPawel Jakub Dawidek 
6505c91076SPawel Jakub Dawidek static char comm[MAXPATHLEN], *class_name = NULL, *gclass_name = NULL;
6605c91076SPawel Jakub Dawidek static uint32_t *version = NULL;
6705c91076SPawel Jakub Dawidek static int verbose = 0;
6805c91076SPawel Jakub Dawidek static struct g_command *class_commands = NULL;
6905c91076SPawel Jakub Dawidek 
70138cedfdSPawel Jakub Dawidek #define	GEOM_CLASS_CMDS		0x01
71138cedfdSPawel Jakub Dawidek #define	GEOM_STD_CMDS		0x02
726a027382SEdward Tomasz Napierala 
736a027382SEdward Tomasz Napierala #define	GEOM_CLASS_WIDTH	10
746a027382SEdward Tomasz Napierala 
75138cedfdSPawel Jakub Dawidek static struct g_command *find_command(const char *cmdstr, int flags);
760f73f701SEdward Tomasz Napierala static void list_one_geom_by_provider(const char *provider_name);
7705c91076SPawel Jakub Dawidek static int std_available(const char *name);
7805c91076SPawel Jakub Dawidek 
79b70eccf3SPawel Jakub Dawidek static void std_help(struct gctl_req *req, unsigned flags);
8005c91076SPawel Jakub Dawidek static void std_list(struct gctl_req *req, unsigned flags);
8118ee8840SPawel Jakub Dawidek static void std_status(struct gctl_req *req, unsigned flags);
8205c91076SPawel Jakub Dawidek static void std_load(struct gctl_req *req, unsigned flags);
8305c91076SPawel Jakub Dawidek static void std_unload(struct gctl_req *req, unsigned flags);
8405c91076SPawel Jakub Dawidek 
85bf70beceSEd Schouten static struct g_command std_commands[] = {
86946e2f35SPawel Jakub Dawidek 	{ "help", 0, std_help, G_NULL_OPTS, NULL },
8783d165c1SAlexander Motin 	{ "list", 0, std_list,
8883d165c1SAlexander Motin 	    {
8983d165c1SAlexander Motin 		{ 'a', "all", NULL, G_TYPE_BOOL },
9083d165c1SAlexander Motin 		G_OPT_SENTINEL
9183d165c1SAlexander Motin 	    },
9283d165c1SAlexander Motin 	    "[-a] [name ...]"
93c979e206SPawel Jakub Dawidek 	},
94ba6821f0SPawel Jakub Dawidek 	{ "status", 0, std_status,
95ba6821f0SPawel Jakub Dawidek 	    {
9683d165c1SAlexander Motin 		{ 'a', "all", NULL, G_TYPE_BOOL },
9783d165c1SAlexander Motin 		{ 'g', "geoms", NULL, G_TYPE_BOOL },
986fc60008SPawel Jakub Dawidek 		{ 's', "script", NULL, G_TYPE_BOOL },
99ba6821f0SPawel Jakub Dawidek 		G_OPT_SENTINEL
100ba6821f0SPawel Jakub Dawidek 	    },
10183d165c1SAlexander Motin 	    "[-ags] [name ...]"
102c979e206SPawel Jakub Dawidek 	},
1033cf55d3aSMarcel Moolenaar 	{ "load", G_FLAG_VERBOSE | G_FLAG_LOADKLD, std_load, G_NULL_OPTS,
104946e2f35SPawel Jakub Dawidek 	    NULL },
105946e2f35SPawel Jakub Dawidek 	{ "unload", G_FLAG_VERBOSE, std_unload, G_NULL_OPTS, NULL },
10605c91076SPawel Jakub Dawidek 	G_CMD_SENTINEL
10705c91076SPawel Jakub Dawidek };
10805c91076SPawel Jakub Dawidek 
10905c91076SPawel Jakub Dawidek static void
110c979e206SPawel Jakub Dawidek usage_command(struct g_command *cmd, const char *prefix)
11105c91076SPawel Jakub Dawidek {
11205c91076SPawel Jakub Dawidek 	struct g_option *opt;
113c979e206SPawel Jakub Dawidek 	unsigned i;
11405c91076SPawel Jakub Dawidek 
115c979e206SPawel Jakub Dawidek 	if (cmd->gc_usage != NULL) {
116963feac4SPawel Jakub Dawidek 		char *pos, *ptr, *sptr;
117963feac4SPawel Jakub Dawidek 
118963feac4SPawel Jakub Dawidek 		sptr = ptr = strdup(cmd->gc_usage);
119963feac4SPawel Jakub Dawidek 		while ((pos = strsep(&ptr, "\n")) != NULL) {
120963feac4SPawel Jakub Dawidek 			if (*pos == '\0')
121963feac4SPawel Jakub Dawidek 				continue;
122963feac4SPawel Jakub Dawidek 			fprintf(stderr, "%s %s %s %s\n", prefix, comm,
123963feac4SPawel Jakub Dawidek 			    cmd->gc_name, pos);
124963feac4SPawel Jakub Dawidek 		}
125963feac4SPawel Jakub Dawidek 		free(sptr);
126c979e206SPawel Jakub Dawidek 		return;
127c979e206SPawel Jakub Dawidek 	}
128963feac4SPawel Jakub Dawidek 
129963feac4SPawel Jakub Dawidek 	fprintf(stderr, "%s %s %s", prefix, comm, cmd->gc_name);
13005c91076SPawel Jakub Dawidek 	if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0)
13105c91076SPawel Jakub Dawidek 		fprintf(stderr, " [-v]");
132c979e206SPawel Jakub Dawidek 	for (i = 0; ; i++) {
133c979e206SPawel Jakub Dawidek 		opt = &cmd->gc_options[i];
13405c91076SPawel Jakub Dawidek 		if (opt->go_name == NULL)
13505c91076SPawel Jakub Dawidek 			break;
1366fc60008SPawel Jakub Dawidek 		if (opt->go_val != NULL || G_OPT_TYPE(opt) == G_TYPE_BOOL)
13705c91076SPawel Jakub Dawidek 			fprintf(stderr, " [");
13805c91076SPawel Jakub Dawidek 		else
13905c91076SPawel Jakub Dawidek 			fprintf(stderr, " ");
14005c91076SPawel Jakub Dawidek 		fprintf(stderr, "-%c", opt->go_char);
1416fc60008SPawel Jakub Dawidek 		if (G_OPT_TYPE(opt) != G_TYPE_BOOL)
14205c91076SPawel Jakub Dawidek 			fprintf(stderr, " %s", opt->go_name);
1436fc60008SPawel Jakub Dawidek 		if (opt->go_val != NULL || G_OPT_TYPE(opt) == G_TYPE_BOOL)
14405c91076SPawel Jakub Dawidek 			fprintf(stderr, "]");
14505c91076SPawel Jakub Dawidek 	}
146c979e206SPawel Jakub Dawidek 	fprintf(stderr, "\n");
14705c91076SPawel Jakub Dawidek }
14805c91076SPawel Jakub Dawidek 
14905c91076SPawel Jakub Dawidek static void
150c979e206SPawel Jakub Dawidek usage(void)
15105c91076SPawel Jakub Dawidek {
15205c91076SPawel Jakub Dawidek 
15305c91076SPawel Jakub Dawidek 	if (class_name == NULL) {
154112adef8SEdward Tomasz Napierala 		fprintf(stderr, "usage: geom <class> <command> [options]\n");
1550f73f701SEdward Tomasz Napierala 		fprintf(stderr, "       geom -p <provider-name>\n");
1566a027382SEdward Tomasz Napierala 		fprintf(stderr, "       geom -t\n");
157112adef8SEdward Tomasz Napierala 		exit(EXIT_FAILURE);
15805c91076SPawel Jakub Dawidek 	} else {
159c979e206SPawel Jakub Dawidek 		struct g_command *cmd;
16005c91076SPawel Jakub Dawidek 		const char *prefix;
16105c91076SPawel Jakub Dawidek 		unsigned i;
16205c91076SPawel Jakub Dawidek 
16305c91076SPawel Jakub Dawidek 		prefix = "usage:";
164c979e206SPawel Jakub Dawidek 		if (class_commands != NULL) {
165c979e206SPawel Jakub Dawidek 			for (i = 0; ; i++) {
166c979e206SPawel Jakub Dawidek 				cmd = &class_commands[i];
167c979e206SPawel Jakub Dawidek 				if (cmd->gc_name == NULL)
168c979e206SPawel Jakub Dawidek 					break;
169c979e206SPawel Jakub Dawidek 				usage_command(cmd, prefix);
17005c91076SPawel Jakub Dawidek 				prefix = "      ";
17105c91076SPawel Jakub Dawidek 			}
172c979e206SPawel Jakub Dawidek 		}
17305c91076SPawel Jakub Dawidek 		for (i = 0; ; i++) {
17405c91076SPawel Jakub Dawidek 			cmd = &std_commands[i];
17505c91076SPawel Jakub Dawidek 			if (cmd->gc_name == NULL)
17605c91076SPawel Jakub Dawidek 				break;
177138cedfdSPawel Jakub Dawidek 			/*
178138cedfdSPawel Jakub Dawidek 			 * If class defines command, which has the same name as
179138cedfdSPawel Jakub Dawidek 			 * standard command, skip it, because it was already
180138cedfdSPawel Jakub Dawidek 			 * shown on usage().
181138cedfdSPawel Jakub Dawidek 			 */
182138cedfdSPawel Jakub Dawidek 			if (find_command(cmd->gc_name, GEOM_CLASS_CMDS) != NULL)
18305c91076SPawel Jakub Dawidek 				continue;
184c979e206SPawel Jakub Dawidek 			usage_command(cmd, prefix);
18505c91076SPawel Jakub Dawidek 			prefix = "      ";
18605c91076SPawel Jakub Dawidek 		}
18705c91076SPawel Jakub Dawidek 		exit(EXIT_FAILURE);
18805c91076SPawel Jakub Dawidek 	}
18905c91076SPawel Jakub Dawidek }
19005c91076SPawel Jakub Dawidek 
19105c91076SPawel Jakub Dawidek static void
19205c91076SPawel Jakub Dawidek load_module(void)
19305c91076SPawel Jakub Dawidek {
19405c91076SPawel Jakub Dawidek 	char name1[64], name2[64];
19505c91076SPawel Jakub Dawidek 
19605c91076SPawel Jakub Dawidek 	snprintf(name1, sizeof(name1), "g_%s", class_name);
19705c91076SPawel Jakub Dawidek 	snprintf(name2, sizeof(name2), "geom_%s", class_name);
19805c91076SPawel Jakub Dawidek 	if (modfind(name1) < 0) {
19905c91076SPawel Jakub Dawidek 		/* Not present in kernel, try loading it. */
20005c91076SPawel Jakub Dawidek 		if (kldload(name2) < 0 || modfind(name1) < 0) {
20105c91076SPawel Jakub Dawidek 			if (errno != EEXIST) {
202112adef8SEdward Tomasz Napierala 				err(EXIT_FAILURE, "cannot load %s", name2);
20305c91076SPawel Jakub Dawidek 			}
20405c91076SPawel Jakub Dawidek 		}
20505c91076SPawel Jakub Dawidek 	}
20605c91076SPawel Jakub Dawidek }
20705c91076SPawel Jakub Dawidek 
20805c91076SPawel Jakub Dawidek static int
20905c91076SPawel Jakub Dawidek strlcatf(char *str, size_t size, const char *format, ...)
21005c91076SPawel Jakub Dawidek {
21105c91076SPawel Jakub Dawidek 	size_t len;
21205c91076SPawel Jakub Dawidek 	va_list ap;
21305c91076SPawel Jakub Dawidek 	int ret;
21405c91076SPawel Jakub Dawidek 
21505c91076SPawel Jakub Dawidek 	len = strlen(str);
21605c91076SPawel Jakub Dawidek 	str += len;
21705c91076SPawel Jakub Dawidek 	size -= len;
21805c91076SPawel Jakub Dawidek 
21905c91076SPawel Jakub Dawidek 	va_start(ap, format);
22005c91076SPawel Jakub Dawidek 	ret = vsnprintf(str, size, format, ap);
22105c91076SPawel Jakub Dawidek 	va_end(ap);
22205c91076SPawel Jakub Dawidek 
22305c91076SPawel Jakub Dawidek 	return (ret);
22405c91076SPawel Jakub Dawidek }
22505c91076SPawel Jakub Dawidek 
22605c91076SPawel Jakub Dawidek /*
22705c91076SPawel Jakub Dawidek  * Find given option in options available for given command.
22805c91076SPawel Jakub Dawidek  */
22905c91076SPawel Jakub Dawidek static struct g_option *
23005c91076SPawel Jakub Dawidek find_option(struct g_command *cmd, char ch)
23105c91076SPawel Jakub Dawidek {
23205c91076SPawel Jakub Dawidek 	struct g_option *opt;
23305c91076SPawel Jakub Dawidek 	unsigned i;
23405c91076SPawel Jakub Dawidek 
23505c91076SPawel Jakub Dawidek 	for (i = 0; ; i++) {
23605c91076SPawel Jakub Dawidek 		opt = &cmd->gc_options[i];
23705c91076SPawel Jakub Dawidek 		if (opt->go_name == NULL)
23805c91076SPawel Jakub Dawidek 			return (NULL);
23905c91076SPawel Jakub Dawidek 		if (opt->go_char == ch)
24005c91076SPawel Jakub Dawidek 			return (opt);
24105c91076SPawel Jakub Dawidek 	}
24205c91076SPawel Jakub Dawidek 	/* NOTREACHED */
24305c91076SPawel Jakub Dawidek 	return (NULL);
24405c91076SPawel Jakub Dawidek }
24505c91076SPawel Jakub Dawidek 
24605c91076SPawel Jakub Dawidek /*
24705c91076SPawel Jakub Dawidek  * Add given option to gctl_req.
24805c91076SPawel Jakub Dawidek  */
24905c91076SPawel Jakub Dawidek static void
25005c91076SPawel Jakub Dawidek set_option(struct gctl_req *req, struct g_option *opt, const char *val)
25105c91076SPawel Jakub Dawidek {
252315fcbf7SPawel Jakub Dawidek 	const char *optname;
25335efcc8bSDag-Erling Smørgrav 	uint64_t number;
254a478ea74SPawel Jakub Dawidek 	void *ptr;
25505c91076SPawel Jakub Dawidek 
256315fcbf7SPawel Jakub Dawidek 	if (G_OPT_ISMULTI(opt)) {
257315fcbf7SPawel Jakub Dawidek 		size_t optnamesize;
258315fcbf7SPawel Jakub Dawidek 
259315fcbf7SPawel Jakub Dawidek 		if (G_OPT_NUM(opt) == UCHAR_MAX)
260315fcbf7SPawel Jakub Dawidek 			errx(EXIT_FAILURE, "Too many -%c options.", opt->go_char);
261315fcbf7SPawel Jakub Dawidek 
262315fcbf7SPawel Jakub Dawidek 		/*
263315fcbf7SPawel Jakub Dawidek 		 * Base option name length plus 3 bytes for option number
264315fcbf7SPawel Jakub Dawidek 		 * (max. 255 options) plus 1 byte for terminating '\0'.
265315fcbf7SPawel Jakub Dawidek 		 */
266315fcbf7SPawel Jakub Dawidek 		optnamesize = strlen(opt->go_name) + 3 + 1;
267315fcbf7SPawel Jakub Dawidek 		ptr = malloc(optnamesize);
268315fcbf7SPawel Jakub Dawidek 		if (ptr == NULL)
269315fcbf7SPawel Jakub Dawidek 			errx(EXIT_FAILURE, "No memory.");
270315fcbf7SPawel Jakub Dawidek 		snprintf(ptr, optnamesize, "%s%u", opt->go_name, G_OPT_NUM(opt));
271315fcbf7SPawel Jakub Dawidek 		G_OPT_NUMINC(opt);
272315fcbf7SPawel Jakub Dawidek 		optname = ptr;
273315fcbf7SPawel Jakub Dawidek 	} else {
274315fcbf7SPawel Jakub Dawidek 		optname = opt->go_name;
275315fcbf7SPawel Jakub Dawidek 	}
276315fcbf7SPawel Jakub Dawidek 
277fa5383a2SPawel Jakub Dawidek 	if (G_OPT_TYPE(opt) == G_TYPE_NUMBER) {
278e84091cbSPawel Jakub Dawidek 		if (expand_number(val, &number) == -1) {
279f104beb7SPawel Jakub Dawidek 			err(EXIT_FAILURE, "Invalid value for '%c' argument",
28005c91076SPawel Jakub Dawidek 			    opt->go_char);
28105c91076SPawel Jakub Dawidek 		}
282a478ea74SPawel Jakub Dawidek 		ptr = malloc(sizeof(intmax_t));
283a478ea74SPawel Jakub Dawidek 		if (ptr == NULL)
284a478ea74SPawel Jakub Dawidek 			errx(EXIT_FAILURE, "No memory.");
285a478ea74SPawel Jakub Dawidek 		*(intmax_t *)ptr = number;
286a478ea74SPawel Jakub Dawidek 		opt->go_val = ptr;
287628ec6d3SPawel Jakub Dawidek 		gctl_ro_param(req, optname, sizeof(intmax_t), opt->go_val);
2886fc60008SPawel Jakub Dawidek 	} else if (G_OPT_TYPE(opt) == G_TYPE_STRING) {
289315fcbf7SPawel Jakub Dawidek 		gctl_ro_param(req, optname, -1, val);
2906fc60008SPawel Jakub Dawidek 	} else if (G_OPT_TYPE(opt) == G_TYPE_BOOL) {
291a478ea74SPawel Jakub Dawidek 		ptr = malloc(sizeof(int));
292a478ea74SPawel Jakub Dawidek 		if (ptr == NULL)
29305c91076SPawel Jakub Dawidek 			errx(EXIT_FAILURE, "No memory.");
294a478ea74SPawel Jakub Dawidek 		*(int *)ptr = *val - '0';
295a478ea74SPawel Jakub Dawidek 		opt->go_val = ptr;
296315fcbf7SPawel Jakub Dawidek 		gctl_ro_param(req, optname, sizeof(int), opt->go_val);
2976fc60008SPawel Jakub Dawidek 	} else {
2986fc60008SPawel Jakub Dawidek 		assert(!"Invalid type");
29905c91076SPawel Jakub Dawidek 	}
300315fcbf7SPawel Jakub Dawidek 
301315fcbf7SPawel Jakub Dawidek 	if (G_OPT_ISMULTI(opt))
302315fcbf7SPawel Jakub Dawidek 		free(__DECONST(char *, optname));
30305c91076SPawel Jakub Dawidek }
30405c91076SPawel Jakub Dawidek 
30505c91076SPawel Jakub Dawidek /*
30605c91076SPawel Jakub Dawidek  * 1. Add given argument by caller.
30705c91076SPawel Jakub Dawidek  * 2. Add default values of not given arguments.
30805c91076SPawel Jakub Dawidek  * 3. Add the rest of arguments.
30905c91076SPawel Jakub Dawidek  */
31005c91076SPawel Jakub Dawidek static void
31105c91076SPawel Jakub Dawidek parse_arguments(struct g_command *cmd, struct gctl_req *req, int *argc,
31205c91076SPawel Jakub Dawidek     char ***argv)
31305c91076SPawel Jakub Dawidek {
31405c91076SPawel Jakub Dawidek 	struct g_option *opt;
31505c91076SPawel Jakub Dawidek 	char opts[64];
31605c91076SPawel Jakub Dawidek 	unsigned i;
31705c91076SPawel Jakub Dawidek 	int ch;
31805c91076SPawel Jakub Dawidek 
31905c91076SPawel Jakub Dawidek 	*opts = '\0';
32005c91076SPawel Jakub Dawidek 	if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0)
32105c91076SPawel Jakub Dawidek 		strlcat(opts, "v", sizeof(opts));
32205c91076SPawel Jakub Dawidek 	for (i = 0; ; i++) {
32305c91076SPawel Jakub Dawidek 		opt = &cmd->gc_options[i];
32405c91076SPawel Jakub Dawidek 		if (opt->go_name == NULL)
32505c91076SPawel Jakub Dawidek 			break;
3266fc60008SPawel Jakub Dawidek 		assert(G_OPT_TYPE(opt) != 0);
327315fcbf7SPawel Jakub Dawidek 		assert((opt->go_type & ~(G_TYPE_MASK | G_TYPE_MULTI)) == 0);
328315fcbf7SPawel Jakub Dawidek 		/* Multiple bool arguments makes no sense. */
329315fcbf7SPawel Jakub Dawidek 		assert(G_OPT_TYPE(opt) != G_TYPE_BOOL ||
330315fcbf7SPawel Jakub Dawidek 		    (opt->go_type & G_TYPE_MULTI) == 0);
33105c91076SPawel Jakub Dawidek 		strlcatf(opts, sizeof(opts), "%c", opt->go_char);
3326fc60008SPawel Jakub Dawidek 		if (G_OPT_TYPE(opt) != G_TYPE_BOOL)
33305c91076SPawel Jakub Dawidek 			strlcat(opts, ":", sizeof(opts));
33405c91076SPawel Jakub Dawidek 	}
33505c91076SPawel Jakub Dawidek 
33605c91076SPawel Jakub Dawidek 	/*
33705c91076SPawel Jakub Dawidek 	 * Add specified arguments.
33805c91076SPawel Jakub Dawidek 	 */
33905c91076SPawel Jakub Dawidek 	while ((ch = getopt(*argc, *argv, opts)) != -1) {
34005c91076SPawel Jakub Dawidek 		/* Standard (not passed to kernel) options. */
34105c91076SPawel Jakub Dawidek 		switch (ch) {
34205c91076SPawel Jakub Dawidek 		case 'v':
34305c91076SPawel Jakub Dawidek 			verbose = 1;
34405c91076SPawel Jakub Dawidek 			continue;
34505c91076SPawel Jakub Dawidek 		}
34605c91076SPawel Jakub Dawidek 		/* Options passed to kernel. */
34705c91076SPawel Jakub Dawidek 		opt = find_option(cmd, ch);
34805c91076SPawel Jakub Dawidek 		if (opt == NULL)
349c979e206SPawel Jakub Dawidek 			usage();
350315fcbf7SPawel Jakub Dawidek 		if (!G_OPT_ISMULTI(opt) && G_OPT_ISDONE(opt)) {
3516fc60008SPawel Jakub Dawidek 			warnx("Option '%c' specified twice.", opt->go_char);
352c979e206SPawel Jakub Dawidek 			usage();
35305c91076SPawel Jakub Dawidek 		}
35405c91076SPawel Jakub Dawidek 		G_OPT_DONE(opt);
35505c91076SPawel Jakub Dawidek 
3566fc60008SPawel Jakub Dawidek 		if (G_OPT_TYPE(opt) == G_TYPE_BOOL)
35705c91076SPawel Jakub Dawidek 			set_option(req, opt, "1");
35805c91076SPawel Jakub Dawidek 		else
35905c91076SPawel Jakub Dawidek 			set_option(req, opt, optarg);
36005c91076SPawel Jakub Dawidek 	}
36105c91076SPawel Jakub Dawidek 	*argc -= optind;
36205c91076SPawel Jakub Dawidek 	*argv += optind;
36305c91076SPawel Jakub Dawidek 
36405c91076SPawel Jakub Dawidek 	/*
36505c91076SPawel Jakub Dawidek 	 * Add not specified arguments, but with default values.
36605c91076SPawel Jakub Dawidek 	 */
36705c91076SPawel Jakub Dawidek 	for (i = 0; ; i++) {
36805c91076SPawel Jakub Dawidek 		opt = &cmd->gc_options[i];
36905c91076SPawel Jakub Dawidek 		if (opt->go_name == NULL)
37005c91076SPawel Jakub Dawidek 			break;
37105c91076SPawel Jakub Dawidek 		if (G_OPT_ISDONE(opt))
37205c91076SPawel Jakub Dawidek 			continue;
37305c91076SPawel Jakub Dawidek 
3746fc60008SPawel Jakub Dawidek 		if (G_OPT_TYPE(opt) == G_TYPE_BOOL) {
37505c91076SPawel Jakub Dawidek 			assert(opt->go_val == NULL);
37605c91076SPawel Jakub Dawidek 			set_option(req, opt, "0");
37705c91076SPawel Jakub Dawidek 		} else {
37805c91076SPawel Jakub Dawidek 			if (opt->go_val == NULL) {
3796fc60008SPawel Jakub Dawidek 				warnx("Option '%c' not specified.",
38005c91076SPawel Jakub Dawidek 				    opt->go_char);
381c979e206SPawel Jakub Dawidek 				usage();
3827648b1e9SPawel Jakub Dawidek 			} else if (opt->go_val == G_VAL_OPTIONAL) {
3837648b1e9SPawel Jakub Dawidek 				/* add nothing. */
38405c91076SPawel Jakub Dawidek 			} else {
385946e2f35SPawel Jakub Dawidek 				set_option(req, opt, opt->go_val);
38605c91076SPawel Jakub Dawidek 			}
38705c91076SPawel Jakub Dawidek 		}
38805c91076SPawel Jakub Dawidek 	}
3893cf55d3aSMarcel Moolenaar 
39005c91076SPawel Jakub Dawidek 	/*
39105c91076SPawel Jakub Dawidek 	 * Add rest of given arguments.
39205c91076SPawel Jakub Dawidek 	 */
39305c91076SPawel Jakub Dawidek 	gctl_ro_param(req, "nargs", sizeof(int), argc);
39405c91076SPawel Jakub Dawidek 	for (i = 0; i < (unsigned)*argc; i++) {
39505c91076SPawel Jakub Dawidek 		char argname[16];
39605c91076SPawel Jakub Dawidek 
39705c91076SPawel Jakub Dawidek 		snprintf(argname, sizeof(argname), "arg%u", i);
39805c91076SPawel Jakub Dawidek 		gctl_ro_param(req, argname, -1, (*argv)[i]);
39905c91076SPawel Jakub Dawidek 	}
40005c91076SPawel Jakub Dawidek }
40105c91076SPawel Jakub Dawidek 
40205c91076SPawel Jakub Dawidek /*
40305c91076SPawel Jakub Dawidek  * Find given command in commands available for given class.
40405c91076SPawel Jakub Dawidek  */
40505c91076SPawel Jakub Dawidek static struct g_command *
406138cedfdSPawel Jakub Dawidek find_command(const char *cmdstr, int flags)
40705c91076SPawel Jakub Dawidek {
40805c91076SPawel Jakub Dawidek 	struct g_command *cmd;
40905c91076SPawel Jakub Dawidek 	unsigned i;
41005c91076SPawel Jakub Dawidek 
41105c91076SPawel Jakub Dawidek 	/*
41205c91076SPawel Jakub Dawidek 	 * First try to find command defined by loaded library.
41305c91076SPawel Jakub Dawidek 	 */
414138cedfdSPawel Jakub Dawidek 	if ((flags & GEOM_CLASS_CMDS) != 0 && class_commands != NULL) {
41505c91076SPawel Jakub Dawidek 		for (i = 0; ; i++) {
41605c91076SPawel Jakub Dawidek 			cmd = &class_commands[i];
41705c91076SPawel Jakub Dawidek 			if (cmd->gc_name == NULL)
41805c91076SPawel Jakub Dawidek 				break;
41905c91076SPawel Jakub Dawidek 			if (strcmp(cmd->gc_name, cmdstr) == 0)
42005c91076SPawel Jakub Dawidek 				return (cmd);
42105c91076SPawel Jakub Dawidek 		}
42205c91076SPawel Jakub Dawidek 	}
42305c91076SPawel Jakub Dawidek 	/*
42405c91076SPawel Jakub Dawidek 	 * Now try to find in standard commands.
42505c91076SPawel Jakub Dawidek 	 */
426138cedfdSPawel Jakub Dawidek 	if ((flags & GEOM_STD_CMDS) != 0) {
42705c91076SPawel Jakub Dawidek 		for (i = 0; ; i++) {
42805c91076SPawel Jakub Dawidek 			cmd = &std_commands[i];
42905c91076SPawel Jakub Dawidek 			if (cmd->gc_name == NULL)
43005c91076SPawel Jakub Dawidek 				break;
43105c91076SPawel Jakub Dawidek 			if (strcmp(cmd->gc_name, cmdstr) == 0)
43205c91076SPawel Jakub Dawidek 				return (cmd);
43305c91076SPawel Jakub Dawidek 		}
434138cedfdSPawel Jakub Dawidek 	}
43505c91076SPawel Jakub Dawidek 	return (NULL);
43605c91076SPawel Jakub Dawidek }
43705c91076SPawel Jakub Dawidek 
43805c91076SPawel Jakub Dawidek static unsigned
43905c91076SPawel Jakub Dawidek set_flags(struct g_command *cmd)
44005c91076SPawel Jakub Dawidek {
44105c91076SPawel Jakub Dawidek 	unsigned flags = 0;
44205c91076SPawel Jakub Dawidek 
44305c91076SPawel Jakub Dawidek 	if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0 && verbose)
44405c91076SPawel Jakub Dawidek 		flags |= G_FLAG_VERBOSE;
44505c91076SPawel Jakub Dawidek 
44605c91076SPawel Jakub Dawidek 	return (flags);
44705c91076SPawel Jakub Dawidek }
44805c91076SPawel Jakub Dawidek 
44905c91076SPawel Jakub Dawidek /*
45005c91076SPawel Jakub Dawidek  * Run command.
45105c91076SPawel Jakub Dawidek  */
45205c91076SPawel Jakub Dawidek static void
45305c91076SPawel Jakub Dawidek run_command(int argc, char *argv[])
45405c91076SPawel Jakub Dawidek {
45505c91076SPawel Jakub Dawidek 	struct g_command *cmd;
45605c91076SPawel Jakub Dawidek 	struct gctl_req *req;
45705c91076SPawel Jakub Dawidek 	const char *errstr;
45805c91076SPawel Jakub Dawidek 	char buf[4096];
45905c91076SPawel Jakub Dawidek 
460138cedfdSPawel Jakub Dawidek 	/* First try to find a command defined by a class. */
461138cedfdSPawel Jakub Dawidek 	cmd = find_command(argv[0], GEOM_CLASS_CMDS);
462138cedfdSPawel Jakub Dawidek 	if (cmd == NULL) {
463138cedfdSPawel Jakub Dawidek 		/* Now, try to find a standard command. */
464138cedfdSPawel Jakub Dawidek 		cmd = find_command(argv[0], GEOM_STD_CMDS);
46505c91076SPawel Jakub Dawidek 		if (cmd == NULL) {
4666fc60008SPawel Jakub Dawidek 			warnx("Unknown command: %s.", argv[0]);
467c979e206SPawel Jakub Dawidek 			usage();
46805c91076SPawel Jakub Dawidek 		}
469138cedfdSPawel Jakub Dawidek 		if (!std_available(cmd->gc_name)) {
470112adef8SEdward Tomasz Napierala 			warnx("Command '%s' not available; "
471112adef8SEdward Tomasz Napierala 			    "try 'load' first.", argv[0]);
472138cedfdSPawel Jakub Dawidek 			exit(EXIT_FAILURE);
473138cedfdSPawel Jakub Dawidek 		}
474138cedfdSPawel Jakub Dawidek 	}
47505c91076SPawel Jakub Dawidek 	if ((cmd->gc_flags & G_FLAG_LOADKLD) != 0)
47605c91076SPawel Jakub Dawidek 		load_module();
47705c91076SPawel Jakub Dawidek 
47805c91076SPawel Jakub Dawidek 	req = gctl_get_handle();
47905c91076SPawel Jakub Dawidek 	gctl_ro_param(req, "class", -1, gclass_name);
48005c91076SPawel Jakub Dawidek 	gctl_ro_param(req, "verb", -1, argv[0]);
48105c91076SPawel Jakub Dawidek 	if (version != NULL)
48205c91076SPawel Jakub Dawidek 		gctl_ro_param(req, "version", sizeof(*version), version);
48305c91076SPawel Jakub Dawidek 	parse_arguments(cmd, req, &argc, &argv);
48405c91076SPawel Jakub Dawidek 
4851bcfab7fSPawel Jakub Dawidek 	bzero(buf, sizeof(buf));
48605c91076SPawel Jakub Dawidek 	if (cmd->gc_func != NULL) {
48705c91076SPawel Jakub Dawidek 		unsigned flags;
48805c91076SPawel Jakub Dawidek 
48905c91076SPawel Jakub Dawidek 		flags = set_flags(cmd);
49005c91076SPawel Jakub Dawidek 		cmd->gc_func(req, flags);
49105c91076SPawel Jakub Dawidek 		errstr = req->error;
49205c91076SPawel Jakub Dawidek 	} else {
49305c91076SPawel Jakub Dawidek 		gctl_rw_param(req, "output", sizeof(buf), buf);
49405c91076SPawel Jakub Dawidek 		errstr = gctl_issue(req);
49505c91076SPawel Jakub Dawidek 	}
49653767efdSPawel Jakub Dawidek 	if (errstr != NULL && errstr[0] != '\0') {
4976fc60008SPawel Jakub Dawidek 		warnx("%s", errstr);
498f792f1d8SPawel Jakub Dawidek 		if (strncmp(errstr, "warning: ", strlen("warning: ")) != 0) {
49905c91076SPawel Jakub Dawidek 			gctl_free(req);
50005c91076SPawel Jakub Dawidek 			exit(EXIT_FAILURE);
50105c91076SPawel Jakub Dawidek 		}
502f792f1d8SPawel Jakub Dawidek 	}
5031bcfab7fSPawel Jakub Dawidek 	if (buf[0] != '\0')
50405c91076SPawel Jakub Dawidek 		printf("%s", buf);
50505c91076SPawel Jakub Dawidek 	gctl_free(req);
50605c91076SPawel Jakub Dawidek 	if (verbose)
50705c91076SPawel Jakub Dawidek 		printf("Done.\n");
50805c91076SPawel Jakub Dawidek 	exit(EXIT_SUCCESS);
50905c91076SPawel Jakub Dawidek }
51005c91076SPawel Jakub Dawidek 
51114bf405bSMarcel Moolenaar #ifndef STATIC_GEOM_CLASSES
5123bad06e9SPawel Jakub Dawidek static const char *
5133bad06e9SPawel Jakub Dawidek library_path(void)
5143bad06e9SPawel Jakub Dawidek {
5153bad06e9SPawel Jakub Dawidek 	const char *path;
5163bad06e9SPawel Jakub Dawidek 
5173bad06e9SPawel Jakub Dawidek 	path = getenv("GEOM_LIBRARY_PATH");
5183bad06e9SPawel Jakub Dawidek 	if (path == NULL)
5198494b738SDavid E. O'Brien 		path = GEOM_CLASS_DIR;
5203bad06e9SPawel Jakub Dawidek 	return (path);
5213bad06e9SPawel Jakub Dawidek }
5223bad06e9SPawel Jakub Dawidek 
52305c91076SPawel Jakub Dawidek static void
52405c91076SPawel Jakub Dawidek load_library(void)
52505c91076SPawel Jakub Dawidek {
5265d824386SUlf Lilleengen 	char *curpath, path[MAXPATHLEN], *tofree, *totalpath;
52705c91076SPawel Jakub Dawidek 	uint32_t *lib_version;
52805c91076SPawel Jakub Dawidek 	void *dlh;
5292591e96cSUlf Lilleengen 	int ret;
53005c91076SPawel Jakub Dawidek 
5312591e96cSUlf Lilleengen 	ret = 0;
5325d824386SUlf Lilleengen 	tofree = totalpath = strdup(library_path());
5332591e96cSUlf Lilleengen 	if (totalpath == NULL)
5342591e96cSUlf Lilleengen 		err(EXIT_FAILURE, "Not enough memory for library path");
5352591e96cSUlf Lilleengen 
5362591e96cSUlf Lilleengen 	if (strchr(totalpath, ':') != NULL)
5372591e96cSUlf Lilleengen 		curpath = strsep(&totalpath, ":");
5382591e96cSUlf Lilleengen 	else
5392591e96cSUlf Lilleengen 		curpath = totalpath;
5402591e96cSUlf Lilleengen 	/* Traverse the paths to find one that contains the library we want. */
5412591e96cSUlf Lilleengen 	while (curpath != NULL) {
5422591e96cSUlf Lilleengen 		snprintf(path, sizeof(path), "%s/geom_%s.so", curpath,
5433bad06e9SPawel Jakub Dawidek 		    class_name);
5442591e96cSUlf Lilleengen 		ret = access(path, F_OK);
5452591e96cSUlf Lilleengen 		if (ret == -1) {
546a73148d2SPawel Jakub Dawidek 			if (errno == ENOENT) {
54705c91076SPawel Jakub Dawidek 				/*
5482591e96cSUlf Lilleengen 				 * If we cannot find library, try the next
5492591e96cSUlf Lilleengen 				 * path.
55005c91076SPawel Jakub Dawidek 				 */
5512591e96cSUlf Lilleengen 				curpath = strsep(&totalpath, ":");
5522591e96cSUlf Lilleengen 				continue;
55305c91076SPawel Jakub Dawidek 			}
554a73148d2SPawel Jakub Dawidek 			err(EXIT_FAILURE, "Cannot access library");
555a73148d2SPawel Jakub Dawidek 		}
5562591e96cSUlf Lilleengen 		break;
5572591e96cSUlf Lilleengen 	}
5585d824386SUlf Lilleengen 	free(tofree);
5592591e96cSUlf Lilleengen 	/* No library was found, but standard commands can still be used */
5602591e96cSUlf Lilleengen 	if (ret == -1)
5612591e96cSUlf Lilleengen 		return;
562a73148d2SPawel Jakub Dawidek 	dlh = dlopen(path, RTLD_NOW);
563a73148d2SPawel Jakub Dawidek 	if (dlh == NULL)
564a73148d2SPawel Jakub Dawidek 		errx(EXIT_FAILURE, "Cannot open library: %s.", dlerror());
56505c91076SPawel Jakub Dawidek 	lib_version = dlsym(dlh, "lib_version");
56605c91076SPawel Jakub Dawidek 	if (lib_version == NULL) {
5676fc60008SPawel Jakub Dawidek 		warnx("Cannot find symbol %s: %s.", "lib_version", dlerror());
56805c91076SPawel Jakub Dawidek 		dlclose(dlh);
56905c91076SPawel Jakub Dawidek 		exit(EXIT_FAILURE);
57005c91076SPawel Jakub Dawidek 	}
57105c91076SPawel Jakub Dawidek 	if (*lib_version != G_LIB_VERSION) {
57205c91076SPawel Jakub Dawidek 		dlclose(dlh);
573f792f1d8SPawel Jakub Dawidek 		errx(EXIT_FAILURE, "%s and %s are not synchronized.",
574f792f1d8SPawel Jakub Dawidek 		    getprogname(), path);
57505c91076SPawel Jakub Dawidek 	}
57605c91076SPawel Jakub Dawidek 	version = dlsym(dlh, "version");
57705c91076SPawel Jakub Dawidek 	if (version == NULL) {
5786fc60008SPawel Jakub Dawidek 		warnx("Cannot find symbol %s: %s.", "version", dlerror());
57905c91076SPawel Jakub Dawidek 		dlclose(dlh);
58005c91076SPawel Jakub Dawidek 		exit(EXIT_FAILURE);
58105c91076SPawel Jakub Dawidek 	}
58205c91076SPawel Jakub Dawidek 	class_commands = dlsym(dlh, "class_commands");
58305c91076SPawel Jakub Dawidek 	if (class_commands == NULL) {
5846fc60008SPawel Jakub Dawidek 		warnx("Cannot find symbol %s: %s.", "class_commands",
5856fc60008SPawel Jakub Dawidek 		    dlerror());
58605c91076SPawel Jakub Dawidek 		dlclose(dlh);
58705c91076SPawel Jakub Dawidek 		exit(EXIT_FAILURE);
58805c91076SPawel Jakub Dawidek 	}
58905c91076SPawel Jakub Dawidek }
59014bf405bSMarcel Moolenaar #endif	/* !STATIC_GEOM_CLASSES */
59105c91076SPawel Jakub Dawidek 
59205c91076SPawel Jakub Dawidek /*
59305c91076SPawel Jakub Dawidek  * Class name should be all capital letters.
59405c91076SPawel Jakub Dawidek  */
59505c91076SPawel Jakub Dawidek static void
59605c91076SPawel Jakub Dawidek set_class_name(void)
59705c91076SPawel Jakub Dawidek {
59805c91076SPawel Jakub Dawidek 	char *s1, *s2;
59905c91076SPawel Jakub Dawidek 
600ee602fbbSPawel Jakub Dawidek 	s1 = class_name;
601ee602fbbSPawel Jakub Dawidek 	for (; *s1 != '\0'; s1++)
602ee602fbbSPawel Jakub Dawidek 		*s1 = tolower(*s1);
603cb94ab30SPawel Jakub Dawidek 	gclass_name = malloc(strlen(class_name) + 1);
60405c91076SPawel Jakub Dawidek 	if (gclass_name == NULL)
60505c91076SPawel Jakub Dawidek 		errx(EXIT_FAILURE, "No memory");
60605c91076SPawel Jakub Dawidek 	s1 = gclass_name;
60705c91076SPawel Jakub Dawidek 	s2 = class_name;
60805c91076SPawel Jakub Dawidek 	for (; *s2 != '\0'; s2++)
60905c91076SPawel Jakub Dawidek 		*s1++ = toupper(*s2);
61005c91076SPawel Jakub Dawidek 	*s1 = '\0';
61105c91076SPawel Jakub Dawidek }
61205c91076SPawel Jakub Dawidek 
61305c91076SPawel Jakub Dawidek static void
61405c91076SPawel Jakub Dawidek get_class(int *argc, char ***argv)
61505c91076SPawel Jakub Dawidek {
61605c91076SPawel Jakub Dawidek 
61705c91076SPawel Jakub Dawidek 	snprintf(comm, sizeof(comm), "%s", basename((*argv)[0]));
61805c91076SPawel Jakub Dawidek 	if (strcmp(comm, "geom") == 0) {
61905c91076SPawel Jakub Dawidek 		if (*argc < 2)
620c979e206SPawel Jakub Dawidek 			usage();
621687b0015SPawel Jakub Dawidek 		else if (*argc == 2) {
622687b0015SPawel Jakub Dawidek 			if (strcmp((*argv)[1], "-h") == 0 ||
623687b0015SPawel Jakub Dawidek 			    strcmp((*argv)[1], "help") == 0) {
624c979e206SPawel Jakub Dawidek 				usage();
625687b0015SPawel Jakub Dawidek 			}
626687b0015SPawel Jakub Dawidek 		}
62705c91076SPawel Jakub Dawidek 		strlcatf(comm, sizeof(comm), " %s", (*argv)[1]);
62805c91076SPawel Jakub Dawidek 		class_name = (*argv)[1];
62905c91076SPawel Jakub Dawidek 		*argc -= 2;
63005c91076SPawel Jakub Dawidek 		*argv += 2;
63105c91076SPawel Jakub Dawidek 	} else if (*comm == 'g') {
63205c91076SPawel Jakub Dawidek 		class_name = comm + 1;
63305c91076SPawel Jakub Dawidek 		*argc -= 1;
63405c91076SPawel Jakub Dawidek 		*argv += 1;
63505c91076SPawel Jakub Dawidek 	} else {
63605c91076SPawel Jakub Dawidek 		errx(EXIT_FAILURE, "Invalid utility name.");
63705c91076SPawel Jakub Dawidek 	}
638a16f9b36SMarcel Moolenaar 
63914bf405bSMarcel Moolenaar #ifndef STATIC_GEOM_CLASSES
64005c91076SPawel Jakub Dawidek 	load_library();
641a16f9b36SMarcel Moolenaar #else
642a16f9b36SMarcel Moolenaar 	if (!strcasecmp(class_name, "part")) {
643a16f9b36SMarcel Moolenaar 		version = &gpart_version;
644a16f9b36SMarcel Moolenaar 		class_commands = gpart_class_commands;
645bc69d66fSXin LI 	} else if (!strcasecmp(class_name, "label")) {
646bc69d66fSXin LI 		version = &glabel_version;
647bc69d66fSXin LI 		class_commands = glabel_class_commands;
648c6678483SAlexander Motin 	}
64914bf405bSMarcel Moolenaar #endif /* !STATIC_GEOM_CLASSES */
650a16f9b36SMarcel Moolenaar 
651a16f9b36SMarcel Moolenaar 	set_class_name();
652e2d52f14SMark Felder 
653e2d52f14SMark Felder 	/* If we can't load or list, it's not a class. */
654e2d52f14SMark Felder 	if (!std_available("load") && !std_available("list"))
655112adef8SEdward Tomasz Napierala 		errx(EXIT_FAILURE, "Invalid class name '%s'.", class_name);
656e2d52f14SMark Felder 
65705c91076SPawel Jakub Dawidek 	if (*argc < 1)
658c979e206SPawel Jakub Dawidek 		usage();
65905c91076SPawel Jakub Dawidek }
66005c91076SPawel Jakub Dawidek 
6610f73f701SEdward Tomasz Napierala static struct ggeom *
6620f73f701SEdward Tomasz Napierala find_geom_by_provider(struct gmesh *mesh, const char *name)
6630f73f701SEdward Tomasz Napierala {
6640f73f701SEdward Tomasz Napierala 	struct gclass *classp;
6650f73f701SEdward Tomasz Napierala 	struct ggeom *gp;
6660f73f701SEdward Tomasz Napierala 	struct gprovider *pp;
6670f73f701SEdward Tomasz Napierala 
6680f73f701SEdward Tomasz Napierala 	LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
6690f73f701SEdward Tomasz Napierala 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
6700f73f701SEdward Tomasz Napierala 			LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
6710f73f701SEdward Tomasz Napierala 				if (strcmp(pp->lg_name, name) == 0)
6720f73f701SEdward Tomasz Napierala 					return (gp);
6730f73f701SEdward Tomasz Napierala 			}
6740f73f701SEdward Tomasz Napierala 		}
6750f73f701SEdward Tomasz Napierala 	}
6760f73f701SEdward Tomasz Napierala 
6770f73f701SEdward Tomasz Napierala 	return (NULL);
6780f73f701SEdward Tomasz Napierala }
6790f73f701SEdward Tomasz Napierala 
6806a027382SEdward Tomasz Napierala static int
6816a027382SEdward Tomasz Napierala compute_tree_width_geom(struct gmesh *mesh, struct ggeom *gp, int indent)
6826a027382SEdward Tomasz Napierala {
6836a027382SEdward Tomasz Napierala 	struct gclass *classp2;
6846a027382SEdward Tomasz Napierala 	struct ggeom *gp2;
6856a027382SEdward Tomasz Napierala 	struct gconsumer *cp2;
6866a027382SEdward Tomasz Napierala 	struct gprovider *pp;
6876a027382SEdward Tomasz Napierala 	int max_width, width;
6886a027382SEdward Tomasz Napierala 
6896a027382SEdward Tomasz Napierala 	max_width = width = indent + strlen(gp->lg_name);
6906a027382SEdward Tomasz Napierala 
6916a027382SEdward Tomasz Napierala 	LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
6926a027382SEdward Tomasz Napierala 		LIST_FOREACH(classp2, &mesh->lg_class, lg_class) {
6936a027382SEdward Tomasz Napierala 			LIST_FOREACH(gp2, &classp2->lg_geom, lg_geom) {
6946a027382SEdward Tomasz Napierala 				LIST_FOREACH(cp2,
6956a027382SEdward Tomasz Napierala 				    &gp2->lg_consumer, lg_consumer) {
6966a027382SEdward Tomasz Napierala 					if (pp != cp2->lg_provider)
6976a027382SEdward Tomasz Napierala 						continue;
6986a027382SEdward Tomasz Napierala 					width = compute_tree_width_geom(mesh,
6996a027382SEdward Tomasz Napierala 					    gp2, indent + 2);
7006a027382SEdward Tomasz Napierala 					if (width > max_width)
7016a027382SEdward Tomasz Napierala 						max_width = width;
7026a027382SEdward Tomasz Napierala 				}
7036a027382SEdward Tomasz Napierala 			}
7046a027382SEdward Tomasz Napierala 		}
7056a027382SEdward Tomasz Napierala 	}
7066a027382SEdward Tomasz Napierala 
7076a027382SEdward Tomasz Napierala 	return (max_width);
7086a027382SEdward Tomasz Napierala }
7096a027382SEdward Tomasz Napierala 
7106a027382SEdward Tomasz Napierala static int
7116a027382SEdward Tomasz Napierala compute_tree_width(struct gmesh *mesh)
7126a027382SEdward Tomasz Napierala {
7136a027382SEdward Tomasz Napierala 	struct gclass *classp;
7146a027382SEdward Tomasz Napierala 	struct ggeom *gp;
7156a027382SEdward Tomasz Napierala 	int max_width, width;
7166a027382SEdward Tomasz Napierala 
7176a027382SEdward Tomasz Napierala 	max_width = width = 0;
7186a027382SEdward Tomasz Napierala 
7196a027382SEdward Tomasz Napierala 	LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
7206a027382SEdward Tomasz Napierala 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
7216a027382SEdward Tomasz Napierala 			if (!LIST_EMPTY(&gp->lg_consumer))
7226a027382SEdward Tomasz Napierala 				continue;
7236a027382SEdward Tomasz Napierala 			width = compute_tree_width_geom(mesh, gp, 0);
7246a027382SEdward Tomasz Napierala 			if (width > max_width)
7256a027382SEdward Tomasz Napierala 				max_width = width;
7266a027382SEdward Tomasz Napierala 		}
7276a027382SEdward Tomasz Napierala 	}
7286a027382SEdward Tomasz Napierala 
7296a027382SEdward Tomasz Napierala 	return (max_width);
7306a027382SEdward Tomasz Napierala }
7316a027382SEdward Tomasz Napierala 
7326a027382SEdward Tomasz Napierala static void
7336a027382SEdward Tomasz Napierala show_tree_geom(struct gmesh *mesh, struct ggeom *gp, int indent, int width)
7346a027382SEdward Tomasz Napierala {
7356a027382SEdward Tomasz Napierala 	struct gclass *classp2;
7366a027382SEdward Tomasz Napierala 	struct ggeom *gp2;
7376a027382SEdward Tomasz Napierala 	struct gconsumer *cp2;
7386a027382SEdward Tomasz Napierala 	struct gprovider *pp;
7396a027382SEdward Tomasz Napierala 
7406a027382SEdward Tomasz Napierala 	if (LIST_EMPTY(&gp->lg_provider)) {
7416a027382SEdward Tomasz Napierala 		printf("%*s%-*.*s %-*.*s\n", indent, "",
7426a027382SEdward Tomasz Napierala 		    width - indent, width - indent, gp->lg_name,
7436a027382SEdward Tomasz Napierala 		    GEOM_CLASS_WIDTH, GEOM_CLASS_WIDTH, gp->lg_class->lg_name);
7446a027382SEdward Tomasz Napierala 		return;
7456a027382SEdward Tomasz Napierala 	}
7466a027382SEdward Tomasz Napierala 
7476a027382SEdward Tomasz Napierala 	LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
7486a027382SEdward Tomasz Napierala 		printf("%*s%-*.*s %-*.*s %s\n", indent, "",
7496a027382SEdward Tomasz Napierala 		    width - indent, width - indent, gp->lg_name,
7506a027382SEdward Tomasz Napierala 		    GEOM_CLASS_WIDTH, GEOM_CLASS_WIDTH, gp->lg_class->lg_name,
7516a027382SEdward Tomasz Napierala 		    pp->lg_name);
7526a027382SEdward Tomasz Napierala 
7536a027382SEdward Tomasz Napierala 		LIST_FOREACH(classp2, &mesh->lg_class, lg_class) {
7546a027382SEdward Tomasz Napierala 			LIST_FOREACH(gp2, &classp2->lg_geom, lg_geom) {
7556a027382SEdward Tomasz Napierala 				LIST_FOREACH(cp2,
7566a027382SEdward Tomasz Napierala 				    &gp2->lg_consumer, lg_consumer) {
7576a027382SEdward Tomasz Napierala 					if (pp != cp2->lg_provider)
7586a027382SEdward Tomasz Napierala 						continue;
7596a027382SEdward Tomasz Napierala 					show_tree_geom(mesh, gp2,
7606a027382SEdward Tomasz Napierala 					    indent + 2, width);
7616a027382SEdward Tomasz Napierala 				}
7626a027382SEdward Tomasz Napierala 			}
7636a027382SEdward Tomasz Napierala 		}
7646a027382SEdward Tomasz Napierala 	}
7656a027382SEdward Tomasz Napierala }
7666a027382SEdward Tomasz Napierala 
7676a027382SEdward Tomasz Napierala static void
7686a027382SEdward Tomasz Napierala show_tree(void)
7696a027382SEdward Tomasz Napierala {
7706a027382SEdward Tomasz Napierala 	struct gmesh mesh;
7716a027382SEdward Tomasz Napierala 	struct gclass *classp;
7726a027382SEdward Tomasz Napierala 	struct ggeom *gp;
7736a027382SEdward Tomasz Napierala 	int error, width;
7746a027382SEdward Tomasz Napierala 
7756a027382SEdward Tomasz Napierala 	error = geom_gettree(&mesh);
7766a027382SEdward Tomasz Napierala 	if (error != 0)
7776a027382SEdward Tomasz Napierala 		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
7786a027382SEdward Tomasz Napierala 
7796a027382SEdward Tomasz Napierala 	width = compute_tree_width(&mesh);
7806a027382SEdward Tomasz Napierala 
7816a027382SEdward Tomasz Napierala 	printf("%-*.*s %-*.*s %s\n",
7826a027382SEdward Tomasz Napierala 	    width, width, "Geom",
7836a027382SEdward Tomasz Napierala 	    GEOM_CLASS_WIDTH, GEOM_CLASS_WIDTH, "Class",
7846a027382SEdward Tomasz Napierala 	    "Provider");
7856a027382SEdward Tomasz Napierala 
7866a027382SEdward Tomasz Napierala 	LIST_FOREACH(classp, &mesh.lg_class, lg_class) {
7876a027382SEdward Tomasz Napierala 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
7886a027382SEdward Tomasz Napierala 			if (!LIST_EMPTY(&gp->lg_consumer))
7896a027382SEdward Tomasz Napierala 				continue;
7906a027382SEdward Tomasz Napierala 			show_tree_geom(&mesh, gp, 0, width);
7916a027382SEdward Tomasz Napierala 		}
7926a027382SEdward Tomasz Napierala 	}
7936a027382SEdward Tomasz Napierala }
7946a027382SEdward Tomasz Napierala 
79505c91076SPawel Jakub Dawidek int
79605c91076SPawel Jakub Dawidek main(int argc, char *argv[])
79705c91076SPawel Jakub Dawidek {
7980f73f701SEdward Tomasz Napierala 	char *provider_name;
7996a027382SEdward Tomasz Napierala 	bool tflag;
8000f73f701SEdward Tomasz Napierala 	int ch;
8010f73f701SEdward Tomasz Napierala 
8020f73f701SEdward Tomasz Napierala 	provider_name = NULL;
8036a027382SEdward Tomasz Napierala 	tflag = false;
8040f73f701SEdward Tomasz Napierala 
8050f73f701SEdward Tomasz Napierala 	if (strcmp(getprogname(), "geom") == 0) {
8066a027382SEdward Tomasz Napierala 		while ((ch = getopt(argc, argv, "hp:t")) != -1) {
8070f73f701SEdward Tomasz Napierala 			switch (ch) {
8080f73f701SEdward Tomasz Napierala 			case 'p':
8090f73f701SEdward Tomasz Napierala 				provider_name = strdup(optarg);
8100f73f701SEdward Tomasz Napierala 				if (provider_name == NULL)
8110f73f701SEdward Tomasz Napierala 					err(1, "strdup");
8120f73f701SEdward Tomasz Napierala 				break;
8136a027382SEdward Tomasz Napierala 			case 't':
8146a027382SEdward Tomasz Napierala 				tflag = true;
8156a027382SEdward Tomasz Napierala 				break;
8160f73f701SEdward Tomasz Napierala 			case 'h':
8170f73f701SEdward Tomasz Napierala 			default:
8180f73f701SEdward Tomasz Napierala 				usage();
8190f73f701SEdward Tomasz Napierala 			}
8200f73f701SEdward Tomasz Napierala 		}
8210f73f701SEdward Tomasz Napierala 
8220f73f701SEdward Tomasz Napierala 		/*
8230f73f701SEdward Tomasz Napierala 		 * Don't adjust argc and argv, it would break get_class().
8240f73f701SEdward Tomasz Napierala 		 */
8250f73f701SEdward Tomasz Napierala 	}
8260f73f701SEdward Tomasz Napierala 
8276a027382SEdward Tomasz Napierala 	if (tflag && provider_name != NULL) {
8286a027382SEdward Tomasz Napierala 		errx(EXIT_FAILURE,
8296a027382SEdward Tomasz Napierala 		    "At most one of -P and -t may be specified.");
8306a027382SEdward Tomasz Napierala 	}
8316a027382SEdward Tomasz Napierala 
8320f73f701SEdward Tomasz Napierala 	if (provider_name != NULL) {
8330f73f701SEdward Tomasz Napierala 		list_one_geom_by_provider(provider_name);
8340f73f701SEdward Tomasz Napierala 		return (0);
8350f73f701SEdward Tomasz Napierala 	}
83605c91076SPawel Jakub Dawidek 
8376a027382SEdward Tomasz Napierala 	if (tflag) {
8386a027382SEdward Tomasz Napierala 		show_tree();
8396a027382SEdward Tomasz Napierala 		return (0);
8406a027382SEdward Tomasz Napierala 	}
8416a027382SEdward Tomasz Napierala 
84205c91076SPawel Jakub Dawidek 	get_class(&argc, &argv);
84305c91076SPawel Jakub Dawidek 	run_command(argc, argv);
84405c91076SPawel Jakub Dawidek 	/* NOTREACHED */
84505c91076SPawel Jakub Dawidek 
84605c91076SPawel Jakub Dawidek 	exit(EXIT_FAILURE);
84705c91076SPawel Jakub Dawidek }
84805c91076SPawel Jakub Dawidek 
84905c91076SPawel Jakub Dawidek static struct gclass *
85005c91076SPawel Jakub Dawidek find_class(struct gmesh *mesh, const char *name)
85105c91076SPawel Jakub Dawidek {
85205c91076SPawel Jakub Dawidek 	struct gclass *classp;
85305c91076SPawel Jakub Dawidek 
85405c91076SPawel Jakub Dawidek 	LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
85505c91076SPawel Jakub Dawidek 		if (strcmp(classp->lg_name, name) == 0)
85605c91076SPawel Jakub Dawidek 			return (classp);
85705c91076SPawel Jakub Dawidek 	}
85805c91076SPawel Jakub Dawidek 	return (NULL);
85905c91076SPawel Jakub Dawidek }
86005c91076SPawel Jakub Dawidek 
8611d723f1dSPawel Jakub Dawidek static struct ggeom *
8621d723f1dSPawel Jakub Dawidek find_geom(struct gclass *classp, const char *name)
86305c91076SPawel Jakub Dawidek {
86405c91076SPawel Jakub Dawidek 	struct ggeom *gp;
86505c91076SPawel Jakub Dawidek 
866*f200cc25SRobert Wing 	if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
867*f200cc25SRobert Wing 		name += sizeof(_PATH_DEV) - 1;
868*f200cc25SRobert Wing 
86905c91076SPawel Jakub Dawidek 	LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
8701d723f1dSPawel Jakub Dawidek 		if (strcmp(gp->lg_name, name) == 0)
8711d723f1dSPawel Jakub Dawidek 			return (gp);
87205c91076SPawel Jakub Dawidek 	}
87305c91076SPawel Jakub Dawidek 	return (NULL);
87405c91076SPawel Jakub Dawidek }
87505c91076SPawel Jakub Dawidek 
87605c91076SPawel Jakub Dawidek static void
8772d76e2dbSPawel Jakub Dawidek list_one_provider(struct gprovider *pp, const char *prefix)
87805c91076SPawel Jakub Dawidek {
87905c91076SPawel Jakub Dawidek 	struct gconfig *conf;
880af565b58SPawel Jakub Dawidek 	char buf[5];
88105c91076SPawel Jakub Dawidek 
8821d723f1dSPawel Jakub Dawidek 	printf("Name: %s\n", pp->lg_name);
883af565b58SPawel Jakub Dawidek 	humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "",
884af565b58SPawel Jakub Dawidek 	    HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
8851d723f1dSPawel Jakub Dawidek 	printf("%sMediasize: %jd (%s)\n", prefix, (intmax_t)pp->lg_mediasize,
8861d723f1dSPawel Jakub Dawidek 	    buf);
8871d723f1dSPawel Jakub Dawidek 	printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize);
888e192c6e8SXin LI 	if (pp->lg_stripesize > 0 || pp->lg_stripeoffset > 0) {
88935daa28fSXin LI 		printf("%sStripesize: %ju\n", prefix, pp->lg_stripesize);
89035daa28fSXin LI 		printf("%sStripeoffset: %ju\n", prefix, pp->lg_stripeoffset);
89135daa28fSXin LI 	}
8921d723f1dSPawel Jakub Dawidek 	printf("%sMode: %s\n", prefix, pp->lg_mode);
89305c91076SPawel Jakub Dawidek 	LIST_FOREACH(conf, &pp->lg_config, lg_config) {
8941d723f1dSPawel Jakub Dawidek 		printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val);
8951d723f1dSPawel Jakub Dawidek 	}
8961d723f1dSPawel Jakub Dawidek }
8971d723f1dSPawel Jakub Dawidek 
8981d723f1dSPawel Jakub Dawidek static void
8992d76e2dbSPawel Jakub Dawidek list_one_consumer(struct gconsumer *cp, const char *prefix)
9001d723f1dSPawel Jakub Dawidek {
9011d723f1dSPawel Jakub Dawidek 	struct gprovider *pp;
9021d723f1dSPawel Jakub Dawidek 	struct gconfig *conf;
9031d723f1dSPawel Jakub Dawidek 
9041d723f1dSPawel Jakub Dawidek 	pp = cp->lg_provider;
9051d723f1dSPawel Jakub Dawidek 	if (pp == NULL)
9061d723f1dSPawel Jakub Dawidek 		printf("[no provider]\n");
9071d723f1dSPawel Jakub Dawidek 	else {
9081d723f1dSPawel Jakub Dawidek 		char buf[5];
9091d723f1dSPawel Jakub Dawidek 
9101d723f1dSPawel Jakub Dawidek 		printf("Name: %s\n", pp->lg_name);
9111d723f1dSPawel Jakub Dawidek 		humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "",
9121d723f1dSPawel Jakub Dawidek 		    HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
9131d723f1dSPawel Jakub Dawidek 		printf("%sMediasize: %jd (%s)\n", prefix,
9141d723f1dSPawel Jakub Dawidek 		    (intmax_t)pp->lg_mediasize, buf);
9151d723f1dSPawel Jakub Dawidek 		printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize);
91600b236aaSXin LI 		if (pp->lg_stripesize > 0 || pp->lg_stripeoffset > 0) {
91735daa28fSXin LI 			printf("%sStripesize: %ju\n", prefix, pp->lg_stripesize);
91835daa28fSXin LI 			printf("%sStripeoffset: %ju\n", prefix, pp->lg_stripeoffset);
91935daa28fSXin LI 		}
9201d723f1dSPawel Jakub Dawidek 		printf("%sMode: %s\n", prefix, cp->lg_mode);
9211d723f1dSPawel Jakub Dawidek 	}
9221d723f1dSPawel Jakub Dawidek 	LIST_FOREACH(conf, &cp->lg_config, lg_config) {
9231d723f1dSPawel Jakub Dawidek 		printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val);
9241d723f1dSPawel Jakub Dawidek 	}
9251d723f1dSPawel Jakub Dawidek }
9261d723f1dSPawel Jakub Dawidek 
9271d723f1dSPawel Jakub Dawidek static void
9282d76e2dbSPawel Jakub Dawidek list_one_geom(struct ggeom *gp)
9291d723f1dSPawel Jakub Dawidek {
9301d723f1dSPawel Jakub Dawidek 	struct gprovider *pp;
9311d723f1dSPawel Jakub Dawidek 	struct gconsumer *cp;
9321d723f1dSPawel Jakub Dawidek 	struct gconfig *conf;
9331d723f1dSPawel Jakub Dawidek 	unsigned n;
9341d723f1dSPawel Jakub Dawidek 
9351d723f1dSPawel Jakub Dawidek 	printf("Geom name: %s\n", gp->lg_name);
9361d723f1dSPawel Jakub Dawidek 	LIST_FOREACH(conf, &gp->lg_config, lg_config) {
9371d723f1dSPawel Jakub Dawidek 		printf("%s: %s\n", conf->lg_name, conf->lg_val);
9381d723f1dSPawel Jakub Dawidek 	}
9391d723f1dSPawel Jakub Dawidek 	if (!LIST_EMPTY(&gp->lg_provider)) {
9401d723f1dSPawel Jakub Dawidek 		printf("Providers:\n");
9411d723f1dSPawel Jakub Dawidek 		n = 1;
9421d723f1dSPawel Jakub Dawidek 		LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
9431d723f1dSPawel Jakub Dawidek 			printf("%u. ", n++);
9442d76e2dbSPawel Jakub Dawidek 			list_one_provider(pp, "   ");
9451d723f1dSPawel Jakub Dawidek 		}
9461d723f1dSPawel Jakub Dawidek 	}
9471d723f1dSPawel Jakub Dawidek 	if (!LIST_EMPTY(&gp->lg_consumer)) {
9481d723f1dSPawel Jakub Dawidek 		printf("Consumers:\n");
9491d723f1dSPawel Jakub Dawidek 		n = 1;
9501d723f1dSPawel Jakub Dawidek 		LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
9511d723f1dSPawel Jakub Dawidek 			printf("%u. ", n++);
9522d76e2dbSPawel Jakub Dawidek 			list_one_consumer(cp, "   ");
9531d723f1dSPawel Jakub Dawidek 		}
95405c91076SPawel Jakub Dawidek 	}
95505c91076SPawel Jakub Dawidek 	printf("\n");
95605c91076SPawel Jakub Dawidek }
95705c91076SPawel Jakub Dawidek 
958b70eccf3SPawel Jakub Dawidek static void
9590f73f701SEdward Tomasz Napierala list_one_geom_by_provider(const char *provider_name)
9600f73f701SEdward Tomasz Napierala {
9610f73f701SEdward Tomasz Napierala 	struct gmesh mesh;
9620f73f701SEdward Tomasz Napierala 	struct ggeom *gp;
9630f73f701SEdward Tomasz Napierala 	int error;
9640f73f701SEdward Tomasz Napierala 
9650f73f701SEdward Tomasz Napierala 	error = geom_gettree(&mesh);
9660f73f701SEdward Tomasz Napierala 	if (error != 0)
9670f73f701SEdward Tomasz Napierala 		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
9680f73f701SEdward Tomasz Napierala 
9690f73f701SEdward Tomasz Napierala 	gp = find_geom_by_provider(&mesh, provider_name);
9700f73f701SEdward Tomasz Napierala 	if (gp == NULL)
9710f73f701SEdward Tomasz Napierala 		errx(EXIT_FAILURE, "Cannot find provider '%s'.", provider_name);
9720f73f701SEdward Tomasz Napierala 
9730f73f701SEdward Tomasz Napierala 	printf("Geom class: %s\n", gp->lg_class->lg_name);
9740f73f701SEdward Tomasz Napierala 	list_one_geom(gp);
9750f73f701SEdward Tomasz Napierala }
9760f73f701SEdward Tomasz Napierala 
9770f73f701SEdward Tomasz Napierala static void
978b70eccf3SPawel Jakub Dawidek std_help(struct gctl_req *req __unused, unsigned flags __unused)
979b70eccf3SPawel Jakub Dawidek {
980b70eccf3SPawel Jakub Dawidek 
981c979e206SPawel Jakub Dawidek 	usage();
982b70eccf3SPawel Jakub Dawidek }
983b70eccf3SPawel Jakub Dawidek 
98405c91076SPawel Jakub Dawidek static int
98505c91076SPawel Jakub Dawidek std_list_available(void)
98605c91076SPawel Jakub Dawidek {
98705c91076SPawel Jakub Dawidek 	struct gmesh mesh;
98805c91076SPawel Jakub Dawidek 	struct gclass *classp;
98905c91076SPawel Jakub Dawidek 	int error;
99005c91076SPawel Jakub Dawidek 
99105c91076SPawel Jakub Dawidek 	error = geom_gettree(&mesh);
9926fc60008SPawel Jakub Dawidek 	if (error != 0)
9936fc60008SPawel Jakub Dawidek 		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
99405c91076SPawel Jakub Dawidek 	classp = find_class(&mesh, gclass_name);
99505c91076SPawel Jakub Dawidek 	geom_deletetree(&mesh);
99605c91076SPawel Jakub Dawidek 	if (classp != NULL)
99705c91076SPawel Jakub Dawidek 		return (1);
99805c91076SPawel Jakub Dawidek 	return (0);
99905c91076SPawel Jakub Dawidek }
100005c91076SPawel Jakub Dawidek 
100105c91076SPawel Jakub Dawidek static void
100205c91076SPawel Jakub Dawidek std_list(struct gctl_req *req, unsigned flags __unused)
100305c91076SPawel Jakub Dawidek {
100405c91076SPawel Jakub Dawidek 	struct gmesh mesh;
100505c91076SPawel Jakub Dawidek 	struct gclass *classp;
10061d723f1dSPawel Jakub Dawidek 	struct ggeom *gp;
1007f13942a7SPawel Jakub Dawidek 	const char *name;
100883d165c1SAlexander Motin 	int all, error, i, nargs;
100905c91076SPawel Jakub Dawidek 
101005c91076SPawel Jakub Dawidek 	error = geom_gettree(&mesh);
10116fc60008SPawel Jakub Dawidek 	if (error != 0)
10126fc60008SPawel Jakub Dawidek 		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
101305c91076SPawel Jakub Dawidek 	classp = find_class(&mesh, gclass_name);
101405c91076SPawel Jakub Dawidek 	if (classp == NULL) {
101505c91076SPawel Jakub Dawidek 		geom_deletetree(&mesh);
1016112adef8SEdward Tomasz Napierala 		errx(EXIT_FAILURE, "Class '%s' not found.", gclass_name);
101705c91076SPawel Jakub Dawidek 	}
1018f13942a7SPawel Jakub Dawidek 	nargs = gctl_get_int(req, "nargs");
101983d165c1SAlexander Motin 	all = gctl_get_int(req, "all");
1020f13942a7SPawel Jakub Dawidek 	if (nargs > 0) {
1021f13942a7SPawel Jakub Dawidek 		for (i = 0; i < nargs; i++) {
1022f13942a7SPawel Jakub Dawidek 			name = gctl_get_ascii(req, "arg%d", i);
10231d723f1dSPawel Jakub Dawidek 			gp = find_geom(classp, name);
1024112adef8SEdward Tomasz Napierala 			if (gp == NULL) {
1025112adef8SEdward Tomasz Napierala 				errx(EXIT_FAILURE, "Class '%s' does not have "
1026112adef8SEdward Tomasz Napierala 				    "an instance named '%s'.",
1027112adef8SEdward Tomasz Napierala 				    gclass_name, name);
1028112adef8SEdward Tomasz Napierala 			}
102983d165c1SAlexander Motin 			list_one_geom(gp);
103005c91076SPawel Jakub Dawidek 		}
103105c91076SPawel Jakub Dawidek 	} else {
103205c91076SPawel Jakub Dawidek 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
103383d165c1SAlexander Motin 			if (LIST_EMPTY(&gp->lg_provider) && !all)
10345fd66a44SPawel Jakub Dawidek 				continue;
10352d76e2dbSPawel Jakub Dawidek 			list_one_geom(gp);
103605c91076SPawel Jakub Dawidek 		}
103705c91076SPawel Jakub Dawidek 	}
103805c91076SPawel Jakub Dawidek 	geom_deletetree(&mesh);
103905c91076SPawel Jakub Dawidek }
104005c91076SPawel Jakub Dawidek 
104105c91076SPawel Jakub Dawidek static int
104218ee8840SPawel Jakub Dawidek std_status_available(void)
104318ee8840SPawel Jakub Dawidek {
104418ee8840SPawel Jakub Dawidek 
104518ee8840SPawel Jakub Dawidek 	/* 'status' command is available when 'list' command is. */
104618ee8840SPawel Jakub Dawidek 	return (std_list_available());
104718ee8840SPawel Jakub Dawidek }
104818ee8840SPawel Jakub Dawidek 
104918ee8840SPawel Jakub Dawidek static void
1050da80913dSPawel Jakub Dawidek status_update_len(struct ggeom *gp, int *name_len, int *status_len)
105118ee8840SPawel Jakub Dawidek {
105218ee8840SPawel Jakub Dawidek 	struct gconfig *conf;
1053da80913dSPawel Jakub Dawidek 	int len;
105418ee8840SPawel Jakub Dawidek 
105518ee8840SPawel Jakub Dawidek 	assert(gp != NULL);
105618ee8840SPawel Jakub Dawidek 	assert(name_len != NULL);
105718ee8840SPawel Jakub Dawidek 	assert(status_len != NULL);
105818ee8840SPawel Jakub Dawidek 
105918ee8840SPawel Jakub Dawidek 	len = strlen(gp->lg_name);
106018ee8840SPawel Jakub Dawidek 	if (*name_len < len)
106118ee8840SPawel Jakub Dawidek 		*name_len = len;
106218ee8840SPawel Jakub Dawidek 	LIST_FOREACH(conf, &gp->lg_config, lg_config) {
106318ee8840SPawel Jakub Dawidek 		if (strcasecmp(conf->lg_name, "state") == 0) {
106418ee8840SPawel Jakub Dawidek 			len = strlen(conf->lg_val);
106518ee8840SPawel Jakub Dawidek 			if (*status_len < len)
106618ee8840SPawel Jakub Dawidek 				*status_len = len;
106718ee8840SPawel Jakub Dawidek 		}
106818ee8840SPawel Jakub Dawidek 	}
106918ee8840SPawel Jakub Dawidek }
107018ee8840SPawel Jakub Dawidek 
107183d165c1SAlexander Motin static void
107283d165c1SAlexander Motin status_update_len_prs(struct ggeom *gp, int *name_len, int *status_len)
107383d165c1SAlexander Motin {
107483d165c1SAlexander Motin 	struct gprovider *pp;
107583d165c1SAlexander Motin 	struct gconfig *conf;
107683d165c1SAlexander Motin 	int len, glen;
107783d165c1SAlexander Motin 
107883d165c1SAlexander Motin 	assert(gp != NULL);
107983d165c1SAlexander Motin 	assert(name_len != NULL);
108083d165c1SAlexander Motin 	assert(status_len != NULL);
108183d165c1SAlexander Motin 
108283d165c1SAlexander Motin 	glen = 0;
108383d165c1SAlexander Motin 	LIST_FOREACH(conf, &gp->lg_config, lg_config) {
108483d165c1SAlexander Motin 		if (strcasecmp(conf->lg_name, "state") == 0) {
108583d165c1SAlexander Motin 			glen = strlen(conf->lg_val);
108683d165c1SAlexander Motin 			break;
108783d165c1SAlexander Motin 		}
108883d165c1SAlexander Motin 	}
108983d165c1SAlexander Motin 	LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
109083d165c1SAlexander Motin 		len = strlen(pp->lg_name);
109183d165c1SAlexander Motin 		if (*name_len < len)
109283d165c1SAlexander Motin 			*name_len = len;
109383d165c1SAlexander Motin 		len = glen;
109483d165c1SAlexander Motin 		LIST_FOREACH(conf, &pp->lg_config, lg_config) {
109583d165c1SAlexander Motin 			if (strcasecmp(conf->lg_name, "state") == 0) {
109683d165c1SAlexander Motin 				len = strlen(conf->lg_val);
109783d165c1SAlexander Motin 				break;
109883d165c1SAlexander Motin 			}
109983d165c1SAlexander Motin 		}
110083d165c1SAlexander Motin 		if (*status_len < len)
110183d165c1SAlexander Motin 			*status_len = len;
110283d165c1SAlexander Motin 	}
110383d165c1SAlexander Motin }
110483d165c1SAlexander Motin 
1105ba6821f0SPawel Jakub Dawidek static char *
110618ee8840SPawel Jakub Dawidek status_one_consumer(struct gconsumer *cp)
110718ee8840SPawel Jakub Dawidek {
1108ba6821f0SPawel Jakub Dawidek 	static char buf[256];
110918ee8840SPawel Jakub Dawidek 	struct gprovider *pp;
111018ee8840SPawel Jakub Dawidek 	struct gconfig *conf;
111183d165c1SAlexander Motin 	const char *state, *syncr;
111218ee8840SPawel Jakub Dawidek 
111318ee8840SPawel Jakub Dawidek 	pp = cp->lg_provider;
111418ee8840SPawel Jakub Dawidek 	if (pp == NULL)
1115ba6821f0SPawel Jakub Dawidek 		return (NULL);
111683d165c1SAlexander Motin 	state = NULL;
111783d165c1SAlexander Motin 	syncr = NULL;
111818ee8840SPawel Jakub Dawidek 	LIST_FOREACH(conf, &cp->lg_config, lg_config) {
111983d165c1SAlexander Motin 		if (strcasecmp(conf->lg_name, "state") == 0)
112083d165c1SAlexander Motin 			state = conf->lg_val;
112118ee8840SPawel Jakub Dawidek 		if (strcasecmp(conf->lg_name, "synchronized") == 0)
112283d165c1SAlexander Motin 			syncr = conf->lg_val;
112318ee8840SPawel Jakub Dawidek 	}
112483d165c1SAlexander Motin 	if (state == NULL && syncr == NULL)
1125ba6821f0SPawel Jakub Dawidek 		snprintf(buf, sizeof(buf), "%s", pp->lg_name);
112683d165c1SAlexander Motin 	else if (state != NULL && syncr != NULL) {
112783d165c1SAlexander Motin 		snprintf(buf, sizeof(buf), "%s (%s, %s)", pp->lg_name,
112883d165c1SAlexander Motin 		    state, syncr);
112983d165c1SAlexander Motin 	} else {
1130ba6821f0SPawel Jakub Dawidek 		snprintf(buf, sizeof(buf), "%s (%s)", pp->lg_name,
113183d165c1SAlexander Motin 		    state ? state : syncr);
1132ba6821f0SPawel Jakub Dawidek 	}
1133ba6821f0SPawel Jakub Dawidek 	return (buf);
113418ee8840SPawel Jakub Dawidek }
113518ee8840SPawel Jakub Dawidek 
113618ee8840SPawel Jakub Dawidek static void
1137ba6821f0SPawel Jakub Dawidek status_one_geom(struct ggeom *gp, int script, int name_len, int status_len)
113818ee8840SPawel Jakub Dawidek {
113918ee8840SPawel Jakub Dawidek 	struct gconsumer *cp;
114018ee8840SPawel Jakub Dawidek 	struct gconfig *conf;
1141ba6821f0SPawel Jakub Dawidek 	const char *name, *status, *component;
1142ba6821f0SPawel Jakub Dawidek 	int gotone;
114318ee8840SPawel Jakub Dawidek 
114418ee8840SPawel Jakub Dawidek 	name = gp->lg_name;
114583d165c1SAlexander Motin 	status = "N/A";
114618ee8840SPawel Jakub Dawidek 	LIST_FOREACH(conf, &gp->lg_config, lg_config) {
114783d165c1SAlexander Motin 		if (strcasecmp(conf->lg_name, "state") == 0) {
114883d165c1SAlexander Motin 			status = conf->lg_val;
114918ee8840SPawel Jakub Dawidek 			break;
115018ee8840SPawel Jakub Dawidek 		}
115183d165c1SAlexander Motin 	}
1152ba6821f0SPawel Jakub Dawidek 	gotone = 0;
115318ee8840SPawel Jakub Dawidek 	LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
1154ba6821f0SPawel Jakub Dawidek 		component = status_one_consumer(cp);
1155ba6821f0SPawel Jakub Dawidek 		if (component == NULL)
1156ba6821f0SPawel Jakub Dawidek 			continue;
1157ba6821f0SPawel Jakub Dawidek 		gotone = 1;
1158ba6821f0SPawel Jakub Dawidek 		printf("%*s  %*s  %s\n", name_len, name, status_len, status,
1159ba6821f0SPawel Jakub Dawidek 		    component);
1160ba6821f0SPawel Jakub Dawidek 		if (!script)
1161ba6821f0SPawel Jakub Dawidek 			name = status = "";
116218ee8840SPawel Jakub Dawidek 	}
1163ba6821f0SPawel Jakub Dawidek 	if (!gotone) {
1164ba6821f0SPawel Jakub Dawidek 		printf("%*s  %*s  %s\n", name_len, name, status_len, status,
1165ba6821f0SPawel Jakub Dawidek 		    "N/A");
1166ba6821f0SPawel Jakub Dawidek 	}
116718ee8840SPawel Jakub Dawidek }
116818ee8840SPawel Jakub Dawidek 
116918ee8840SPawel Jakub Dawidek static void
117083d165c1SAlexander Motin status_one_geom_prs(struct ggeom *gp, int script, int name_len, int status_len)
117183d165c1SAlexander Motin {
117283d165c1SAlexander Motin 	struct gprovider *pp;
117383d165c1SAlexander Motin 	struct gconsumer *cp;
117483d165c1SAlexander Motin 	struct gconfig *conf;
117583d165c1SAlexander Motin 	const char *name, *status, *component;
117683d165c1SAlexander Motin 	int gotone;
117783d165c1SAlexander Motin 
117883d165c1SAlexander Motin 	LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
117983d165c1SAlexander Motin 		name = pp->lg_name;
118083d165c1SAlexander Motin 		status = "N/A";
118183d165c1SAlexander Motin 		LIST_FOREACH(conf, &gp->lg_config, lg_config) {
118283d165c1SAlexander Motin 			if (strcasecmp(conf->lg_name, "state") == 0) {
118383d165c1SAlexander Motin 				status = conf->lg_val;
118483d165c1SAlexander Motin 				break;
118583d165c1SAlexander Motin 			}
118683d165c1SAlexander Motin 		}
118783d165c1SAlexander Motin 		LIST_FOREACH(conf, &pp->lg_config, lg_config) {
118883d165c1SAlexander Motin 			if (strcasecmp(conf->lg_name, "state") == 0) {
118983d165c1SAlexander Motin 				status = conf->lg_val;
119083d165c1SAlexander Motin 				break;
119183d165c1SAlexander Motin 			}
119283d165c1SAlexander Motin 		}
119383d165c1SAlexander Motin 		gotone = 0;
119483d165c1SAlexander Motin 		LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
119583d165c1SAlexander Motin 			component = status_one_consumer(cp);
119683d165c1SAlexander Motin 			if (component == NULL)
119783d165c1SAlexander Motin 				continue;
119883d165c1SAlexander Motin 			gotone = 1;
119983d165c1SAlexander Motin 			printf("%*s  %*s  %s\n", name_len, name,
120083d165c1SAlexander Motin 			    status_len, status, component);
120183d165c1SAlexander Motin 			if (!script)
120283d165c1SAlexander Motin 				name = status = "";
120383d165c1SAlexander Motin 		}
120483d165c1SAlexander Motin 		if (!gotone) {
120583d165c1SAlexander Motin 			printf("%*s  %*s  %s\n", name_len, name,
120683d165c1SAlexander Motin 			    status_len, status, "N/A");
120783d165c1SAlexander Motin 		}
120883d165c1SAlexander Motin 	}
120983d165c1SAlexander Motin }
121083d165c1SAlexander Motin 
121183d165c1SAlexander Motin static void
121218ee8840SPawel Jakub Dawidek std_status(struct gctl_req *req, unsigned flags __unused)
121318ee8840SPawel Jakub Dawidek {
121418ee8840SPawel Jakub Dawidek 	struct gmesh mesh;
121518ee8840SPawel Jakub Dawidek 	struct gclass *classp;
121618ee8840SPawel Jakub Dawidek 	struct ggeom *gp;
1217f13942a7SPawel Jakub Dawidek 	const char *name;
1218da80913dSPawel Jakub Dawidek 	int name_len, status_len;
121983d165c1SAlexander Motin 	int all, error, geoms, i, n, nargs, script;
122018ee8840SPawel Jakub Dawidek 
122118ee8840SPawel Jakub Dawidek 	error = geom_gettree(&mesh);
12226fc60008SPawel Jakub Dawidek 	if (error != 0)
12236fc60008SPawel Jakub Dawidek 		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
122418ee8840SPawel Jakub Dawidek 	classp = find_class(&mesh, gclass_name);
12255d47cd0fSPawel Jakub Dawidek 	if (classp == NULL)
12265d47cd0fSPawel Jakub Dawidek 		errx(EXIT_FAILURE, "Class %s not found.", gclass_name);
1227f13942a7SPawel Jakub Dawidek 	nargs = gctl_get_int(req, "nargs");
122883d165c1SAlexander Motin 	all = gctl_get_int(req, "all");
122983d165c1SAlexander Motin 	geoms = gctl_get_int(req, "geoms");
1230f13942a7SPawel Jakub Dawidek 	script = gctl_get_int(req, "script");
123183d165c1SAlexander Motin 	if (script) {
123283d165c1SAlexander Motin 		name_len = 0;
123383d165c1SAlexander Motin 		status_len = 0;
123483d165c1SAlexander Motin 	} else {
123518ee8840SPawel Jakub Dawidek 		name_len = strlen("Name");
123618ee8840SPawel Jakub Dawidek 		status_len = strlen("Status");
123783d165c1SAlexander Motin 	}
1238f13942a7SPawel Jakub Dawidek 	if (nargs > 0) {
1239f13942a7SPawel Jakub Dawidek 		for (i = 0, n = 0; i < nargs; i++) {
1240f13942a7SPawel Jakub Dawidek 			name = gctl_get_ascii(req, "arg%d", i);
124118ee8840SPawel Jakub Dawidek 			gp = find_geom(classp, name);
124218ee8840SPawel Jakub Dawidek 			if (gp == NULL)
12435d47cd0fSPawel Jakub Dawidek 				errx(EXIT_FAILURE, "No such geom: %s.", name);
124483d165c1SAlexander Motin 			if (geoms) {
124583d165c1SAlexander Motin 				status_update_len(gp,
124683d165c1SAlexander Motin 				    &name_len, &status_len);
124783d165c1SAlexander Motin 			} else {
124883d165c1SAlexander Motin 				status_update_len_prs(gp,
124983d165c1SAlexander Motin 				    &name_len, &status_len);
125018ee8840SPawel Jakub Dawidek 			}
125183d165c1SAlexander Motin 			n++;
125218ee8840SPawel Jakub Dawidek 		}
125318ee8840SPawel Jakub Dawidek 		if (n == 0)
125418ee8840SPawel Jakub Dawidek 			goto end;
125518ee8840SPawel Jakub Dawidek 	} else {
1256f13942a7SPawel Jakub Dawidek 		n = 0;
125718ee8840SPawel Jakub Dawidek 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
125883d165c1SAlexander Motin 			if (LIST_EMPTY(&gp->lg_provider) && !all)
125918ee8840SPawel Jakub Dawidek 				continue;
126083d165c1SAlexander Motin 			if (geoms) {
126183d165c1SAlexander Motin 				status_update_len(gp,
126283d165c1SAlexander Motin 				    &name_len, &status_len);
126383d165c1SAlexander Motin 			} else {
126483d165c1SAlexander Motin 				status_update_len_prs(gp,
126583d165c1SAlexander Motin 				    &name_len, &status_len);
126683d165c1SAlexander Motin 			}
126718ee8840SPawel Jakub Dawidek 			n++;
126818ee8840SPawel Jakub Dawidek 		}
126918ee8840SPawel Jakub Dawidek 		if (n == 0)
127018ee8840SPawel Jakub Dawidek 			goto end;
127118ee8840SPawel Jakub Dawidek 	}
1272f13942a7SPawel Jakub Dawidek 	if (!script) {
1273da80913dSPawel Jakub Dawidek 		printf("%*s  %*s  %s\n", name_len, "Name", status_len, "Status",
127418ee8840SPawel Jakub Dawidek 		    "Components");
1275ba6821f0SPawel Jakub Dawidek 	}
1276f13942a7SPawel Jakub Dawidek 	if (nargs > 0) {
1277f13942a7SPawel Jakub Dawidek 		for (i = 0; i < nargs; i++) {
1278f13942a7SPawel Jakub Dawidek 			name = gctl_get_ascii(req, "arg%d", i);
127918ee8840SPawel Jakub Dawidek 			gp = find_geom(classp, name);
128083d165c1SAlexander Motin 			if (gp == NULL)
128183d165c1SAlexander Motin 				continue;
128283d165c1SAlexander Motin 			if (geoms) {
1283f13942a7SPawel Jakub Dawidek 				status_one_geom(gp, script, name_len,
1284ba6821f0SPawel Jakub Dawidek 				    status_len);
128583d165c1SAlexander Motin 			} else {
128683d165c1SAlexander Motin 				status_one_geom_prs(gp, script, name_len,
128783d165c1SAlexander Motin 				    status_len);
128818ee8840SPawel Jakub Dawidek 			}
128918ee8840SPawel Jakub Dawidek 		}
129018ee8840SPawel Jakub Dawidek 	} else {
129118ee8840SPawel Jakub Dawidek 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
129283d165c1SAlexander Motin 			if (LIST_EMPTY(&gp->lg_provider) && !all)
129318ee8840SPawel Jakub Dawidek 				continue;
129483d165c1SAlexander Motin 			if (geoms) {
129583d165c1SAlexander Motin 				status_one_geom(gp, script, name_len,
129683d165c1SAlexander Motin 				    status_len);
129783d165c1SAlexander Motin 			} else {
129883d165c1SAlexander Motin 				status_one_geom_prs(gp, script, name_len,
129983d165c1SAlexander Motin 				    status_len);
130083d165c1SAlexander Motin 			}
130118ee8840SPawel Jakub Dawidek 		}
130218ee8840SPawel Jakub Dawidek 	}
130318ee8840SPawel Jakub Dawidek end:
130418ee8840SPawel Jakub Dawidek 	geom_deletetree(&mesh);
130518ee8840SPawel Jakub Dawidek }
130618ee8840SPawel Jakub Dawidek 
130718ee8840SPawel Jakub Dawidek static int
130805c91076SPawel Jakub Dawidek std_load_available(void)
130905c91076SPawel Jakub Dawidek {
131005c91076SPawel Jakub Dawidek 	char name[MAXPATHLEN], paths[MAXPATHLEN * 8], *p;
131105c91076SPawel Jakub Dawidek 	struct stat sb;
131205c91076SPawel Jakub Dawidek 	size_t len;
131305c91076SPawel Jakub Dawidek 
131405c91076SPawel Jakub Dawidek 	snprintf(name, sizeof(name), "g_%s", class_name);
131505c91076SPawel Jakub Dawidek 	/*
131605c91076SPawel Jakub Dawidek 	 * If already in kernel, "load" command is not available.
131705c91076SPawel Jakub Dawidek 	 */
131805c91076SPawel Jakub Dawidek 	if (modfind(name) >= 0)
131905c91076SPawel Jakub Dawidek 		return (0);
132005c91076SPawel Jakub Dawidek 	bzero(paths, sizeof(paths));
132105c91076SPawel Jakub Dawidek 	len = sizeof(paths);
132205c91076SPawel Jakub Dawidek 	if (sysctlbyname("kern.module_path", paths, &len, NULL, 0) < 0)
132305c91076SPawel Jakub Dawidek 		err(EXIT_FAILURE, "sysctl(kern.module_path)");
132405c91076SPawel Jakub Dawidek 	for (p = strtok(paths, ";"); p != NULL; p = strtok(NULL, ";")) {
132505c91076SPawel Jakub Dawidek 		snprintf(name, sizeof(name), "%s/geom_%s.ko", p, class_name);
132605c91076SPawel Jakub Dawidek 		/*
132705c91076SPawel Jakub Dawidek 		 * If geom_<name>.ko file exists, "load" command is available.
132805c91076SPawel Jakub Dawidek 		 */
132905c91076SPawel Jakub Dawidek 		if (stat(name, &sb) == 0)
133005c91076SPawel Jakub Dawidek 			return (1);
133105c91076SPawel Jakub Dawidek 	}
133205c91076SPawel Jakub Dawidek 	return (0);
133305c91076SPawel Jakub Dawidek }
133405c91076SPawel Jakub Dawidek 
133505c91076SPawel Jakub Dawidek static void
133605c91076SPawel Jakub Dawidek std_load(struct gctl_req *req __unused, unsigned flags)
133705c91076SPawel Jakub Dawidek {
133805c91076SPawel Jakub Dawidek 
133905c91076SPawel Jakub Dawidek 	/*
134005c91076SPawel Jakub Dawidek 	 * Do nothing special here, because of G_FLAG_LOADKLD flag,
134105c91076SPawel Jakub Dawidek 	 * module is already loaded.
134205c91076SPawel Jakub Dawidek 	 */
134305c91076SPawel Jakub Dawidek 	if ((flags & G_FLAG_VERBOSE) != 0)
134405c91076SPawel Jakub Dawidek 		printf("Module available.\n");
134505c91076SPawel Jakub Dawidek }
134605c91076SPawel Jakub Dawidek 
134705c91076SPawel Jakub Dawidek static int
134805c91076SPawel Jakub Dawidek std_unload_available(void)
134905c91076SPawel Jakub Dawidek {
135005c91076SPawel Jakub Dawidek 	char name[64];
135105c91076SPawel Jakub Dawidek 	int id;
135205c91076SPawel Jakub Dawidek 
135305c91076SPawel Jakub Dawidek 	snprintf(name, sizeof(name), "geom_%s", class_name);
135405c91076SPawel Jakub Dawidek 	id = kldfind(name);
135505c91076SPawel Jakub Dawidek 	if (id >= 0)
135605c91076SPawel Jakub Dawidek 		return (1);
135705c91076SPawel Jakub Dawidek 	return (0);
135805c91076SPawel Jakub Dawidek }
135905c91076SPawel Jakub Dawidek 
136005c91076SPawel Jakub Dawidek static void
136105c91076SPawel Jakub Dawidek std_unload(struct gctl_req *req, unsigned flags __unused)
136205c91076SPawel Jakub Dawidek {
136305c91076SPawel Jakub Dawidek 	char name[64];
136405c91076SPawel Jakub Dawidek 	int id;
136505c91076SPawel Jakub Dawidek 
136605c91076SPawel Jakub Dawidek 	snprintf(name, sizeof(name), "geom_%s", class_name);
136705c91076SPawel Jakub Dawidek 	id = kldfind(name);
136805c91076SPawel Jakub Dawidek 	if (id < 0) {
136905c91076SPawel Jakub Dawidek 		gctl_error(req, "Could not find module: %s.", strerror(errno));
137005c91076SPawel Jakub Dawidek 		return;
137105c91076SPawel Jakub Dawidek 	}
137205c91076SPawel Jakub Dawidek 	if (kldunload(id) < 0) {
137305c91076SPawel Jakub Dawidek 		gctl_error(req, "Could not unload module: %s.",
137405c91076SPawel Jakub Dawidek 		    strerror(errno));
137505c91076SPawel Jakub Dawidek 		return;
137605c91076SPawel Jakub Dawidek 	}
137705c91076SPawel Jakub Dawidek }
137805c91076SPawel Jakub Dawidek 
137905c91076SPawel Jakub Dawidek static int
138005c91076SPawel Jakub Dawidek std_available(const char *name)
138105c91076SPawel Jakub Dawidek {
138205c91076SPawel Jakub Dawidek 
1383b70eccf3SPawel Jakub Dawidek 	if (strcmp(name, "help") == 0)
1384b70eccf3SPawel Jakub Dawidek 		return (1);
1385b70eccf3SPawel Jakub Dawidek 	else if (strcmp(name, "list") == 0)
138605c91076SPawel Jakub Dawidek 		return (std_list_available());
138718ee8840SPawel Jakub Dawidek 	else if (strcmp(name, "status") == 0)
138818ee8840SPawel Jakub Dawidek 		return (std_status_available());
138905c91076SPawel Jakub Dawidek 	else if (strcmp(name, "load") == 0)
139005c91076SPawel Jakub Dawidek 		return (std_load_available());
139105c91076SPawel Jakub Dawidek 	else if (strcmp(name, "unload") == 0)
139205c91076SPawel Jakub Dawidek 		return (std_unload_available());
139305c91076SPawel Jakub Dawidek 	else
139405c91076SPawel Jakub Dawidek 		assert(!"Unknown standard command.");
139505c91076SPawel Jakub Dawidek 	return (0);
139605c91076SPawel Jakub Dawidek }
1397