xref: /freebsd/sbin/geom/core/geom.c (revision 6a02738229be1d312f4e4af22d64e662a2084eee)
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>
4005c91076SPawel Jakub Dawidek #include <stdio.h>
4105c91076SPawel Jakub Dawidek #include <stdlib.h>
4205c91076SPawel Jakub Dawidek #include <stdarg.h>
430f73f701SEdward Tomasz Napierala #include <stdbool.h>
4405c91076SPawel Jakub Dawidek #include <stdint.h>
4505c91076SPawel Jakub Dawidek #include <string.h>
4605c91076SPawel Jakub Dawidek #include <unistd.h>
4705c91076SPawel Jakub Dawidek #include <libgen.h>
48af565b58SPawel Jakub Dawidek #include <libutil.h>
4905c91076SPawel Jakub Dawidek #include <inttypes.h>
5005c91076SPawel Jakub Dawidek #include <dlfcn.h>
5105c91076SPawel Jakub Dawidek #include <assert.h>
5205c91076SPawel Jakub Dawidek #include <libgeom.h>
5305c91076SPawel Jakub Dawidek #include <geom.h>
5405c91076SPawel Jakub Dawidek 
5505c91076SPawel Jakub Dawidek #include "misc/subr.h"
5605c91076SPawel Jakub Dawidek 
5714bf405bSMarcel Moolenaar #ifdef STATIC_GEOM_CLASSES
58a16f9b36SMarcel Moolenaar extern uint32_t gpart_version;
59a16f9b36SMarcel Moolenaar extern struct g_command gpart_class_commands[];
60bc69d66fSXin LI extern uint32_t glabel_version;
61bc69d66fSXin LI extern struct g_command glabel_class_commands[];
62a16f9b36SMarcel Moolenaar #endif
6305c91076SPawel Jakub Dawidek 
6405c91076SPawel Jakub Dawidek static char comm[MAXPATHLEN], *class_name = NULL, *gclass_name = NULL;
6505c91076SPawel Jakub Dawidek static uint32_t *version = NULL;
6605c91076SPawel Jakub Dawidek static int verbose = 0;
6705c91076SPawel Jakub Dawidek static struct g_command *class_commands = NULL;
6805c91076SPawel Jakub Dawidek 
69138cedfdSPawel Jakub Dawidek #define	GEOM_CLASS_CMDS		0x01
70138cedfdSPawel Jakub Dawidek #define	GEOM_STD_CMDS		0x02
71*6a027382SEdward Tomasz Napierala 
72*6a027382SEdward Tomasz Napierala #define	GEOM_CLASS_WIDTH	10
73*6a027382SEdward Tomasz Napierala 
74138cedfdSPawel Jakub Dawidek static struct g_command *find_command(const char *cmdstr, int flags);
750f73f701SEdward Tomasz Napierala static void list_one_geom_by_provider(const char *provider_name);
7605c91076SPawel Jakub Dawidek static int std_available(const char *name);
7705c91076SPawel Jakub Dawidek 
78b70eccf3SPawel Jakub Dawidek static void std_help(struct gctl_req *req, unsigned flags);
7905c91076SPawel Jakub Dawidek static void std_list(struct gctl_req *req, unsigned flags);
8018ee8840SPawel Jakub Dawidek static void std_status(struct gctl_req *req, unsigned flags);
8105c91076SPawel Jakub Dawidek static void std_load(struct gctl_req *req, unsigned flags);
8205c91076SPawel Jakub Dawidek static void std_unload(struct gctl_req *req, unsigned flags);
8305c91076SPawel Jakub Dawidek 
84bf70beceSEd Schouten static struct g_command std_commands[] = {
85946e2f35SPawel Jakub Dawidek 	{ "help", 0, std_help, G_NULL_OPTS, NULL },
8683d165c1SAlexander Motin 	{ "list", 0, std_list,
8783d165c1SAlexander Motin 	    {
8883d165c1SAlexander Motin 		{ 'a', "all", NULL, G_TYPE_BOOL },
8983d165c1SAlexander Motin 		G_OPT_SENTINEL
9083d165c1SAlexander Motin 	    },
9183d165c1SAlexander Motin 	    "[-a] [name ...]"
92c979e206SPawel Jakub Dawidek 	},
93ba6821f0SPawel Jakub Dawidek 	{ "status", 0, std_status,
94ba6821f0SPawel Jakub Dawidek 	    {
9583d165c1SAlexander Motin 		{ 'a', "all", NULL, G_TYPE_BOOL },
9683d165c1SAlexander Motin 		{ 'g', "geoms", NULL, G_TYPE_BOOL },
976fc60008SPawel Jakub Dawidek 		{ 's', "script", NULL, G_TYPE_BOOL },
98ba6821f0SPawel Jakub Dawidek 		G_OPT_SENTINEL
99ba6821f0SPawel Jakub Dawidek 	    },
10083d165c1SAlexander Motin 	    "[-ags] [name ...]"
101c979e206SPawel Jakub Dawidek 	},
1023cf55d3aSMarcel Moolenaar 	{ "load", G_FLAG_VERBOSE | G_FLAG_LOADKLD, std_load, G_NULL_OPTS,
103946e2f35SPawel Jakub Dawidek 	    NULL },
104946e2f35SPawel Jakub Dawidek 	{ "unload", G_FLAG_VERBOSE, std_unload, G_NULL_OPTS, NULL },
10505c91076SPawel Jakub Dawidek 	G_CMD_SENTINEL
10605c91076SPawel Jakub Dawidek };
10705c91076SPawel Jakub Dawidek 
10805c91076SPawel Jakub Dawidek static void
109c979e206SPawel Jakub Dawidek usage_command(struct g_command *cmd, const char *prefix)
11005c91076SPawel Jakub Dawidek {
11105c91076SPawel Jakub Dawidek 	struct g_option *opt;
112c979e206SPawel Jakub Dawidek 	unsigned i;
11305c91076SPawel Jakub Dawidek 
114c979e206SPawel Jakub Dawidek 	if (cmd->gc_usage != NULL) {
115963feac4SPawel Jakub Dawidek 		char *pos, *ptr, *sptr;
116963feac4SPawel Jakub Dawidek 
117963feac4SPawel Jakub Dawidek 		sptr = ptr = strdup(cmd->gc_usage);
118963feac4SPawel Jakub Dawidek 		while ((pos = strsep(&ptr, "\n")) != NULL) {
119963feac4SPawel Jakub Dawidek 			if (*pos == '\0')
120963feac4SPawel Jakub Dawidek 				continue;
121963feac4SPawel Jakub Dawidek 			fprintf(stderr, "%s %s %s %s\n", prefix, comm,
122963feac4SPawel Jakub Dawidek 			    cmd->gc_name, pos);
123963feac4SPawel Jakub Dawidek 		}
124963feac4SPawel Jakub Dawidek 		free(sptr);
125c979e206SPawel Jakub Dawidek 		return;
126c979e206SPawel Jakub Dawidek 	}
127963feac4SPawel Jakub Dawidek 
128963feac4SPawel Jakub Dawidek 	fprintf(stderr, "%s %s %s", prefix, comm, cmd->gc_name);
12905c91076SPawel Jakub Dawidek 	if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0)
13005c91076SPawel Jakub Dawidek 		fprintf(stderr, " [-v]");
131c979e206SPawel Jakub Dawidek 	for (i = 0; ; i++) {
132c979e206SPawel Jakub Dawidek 		opt = &cmd->gc_options[i];
13305c91076SPawel Jakub Dawidek 		if (opt->go_name == NULL)
13405c91076SPawel Jakub Dawidek 			break;
1356fc60008SPawel Jakub Dawidek 		if (opt->go_val != NULL || G_OPT_TYPE(opt) == G_TYPE_BOOL)
13605c91076SPawel Jakub Dawidek 			fprintf(stderr, " [");
13705c91076SPawel Jakub Dawidek 		else
13805c91076SPawel Jakub Dawidek 			fprintf(stderr, " ");
13905c91076SPawel Jakub Dawidek 		fprintf(stderr, "-%c", opt->go_char);
1406fc60008SPawel Jakub Dawidek 		if (G_OPT_TYPE(opt) != G_TYPE_BOOL)
14105c91076SPawel Jakub Dawidek 			fprintf(stderr, " %s", opt->go_name);
1426fc60008SPawel Jakub Dawidek 		if (opt->go_val != NULL || G_OPT_TYPE(opt) == G_TYPE_BOOL)
14305c91076SPawel Jakub Dawidek 			fprintf(stderr, "]");
14405c91076SPawel Jakub Dawidek 	}
145c979e206SPawel Jakub Dawidek 	fprintf(stderr, "\n");
14605c91076SPawel Jakub Dawidek }
14705c91076SPawel Jakub Dawidek 
14805c91076SPawel Jakub Dawidek static void
149c979e206SPawel Jakub Dawidek usage(void)
15005c91076SPawel Jakub Dawidek {
15105c91076SPawel Jakub Dawidek 
15205c91076SPawel Jakub Dawidek 	if (class_name == NULL) {
153112adef8SEdward Tomasz Napierala 		fprintf(stderr, "usage: geom <class> <command> [options]\n");
1540f73f701SEdward Tomasz Napierala 		fprintf(stderr, "       geom -p <provider-name>\n");
155*6a027382SEdward Tomasz Napierala 		fprintf(stderr, "       geom -t\n");
156112adef8SEdward Tomasz Napierala 		exit(EXIT_FAILURE);
15705c91076SPawel Jakub Dawidek 	} else {
158c979e206SPawel Jakub Dawidek 		struct g_command *cmd;
15905c91076SPawel Jakub Dawidek 		const char *prefix;
16005c91076SPawel Jakub Dawidek 		unsigned i;
16105c91076SPawel Jakub Dawidek 
16205c91076SPawel Jakub Dawidek 		prefix = "usage:";
163c979e206SPawel Jakub Dawidek 		if (class_commands != NULL) {
164c979e206SPawel Jakub Dawidek 			for (i = 0; ; i++) {
165c979e206SPawel Jakub Dawidek 				cmd = &class_commands[i];
166c979e206SPawel Jakub Dawidek 				if (cmd->gc_name == NULL)
167c979e206SPawel Jakub Dawidek 					break;
168c979e206SPawel Jakub Dawidek 				usage_command(cmd, prefix);
16905c91076SPawel Jakub Dawidek 				prefix = "      ";
17005c91076SPawel Jakub Dawidek 			}
171c979e206SPawel Jakub Dawidek 		}
17205c91076SPawel Jakub Dawidek 		for (i = 0; ; i++) {
17305c91076SPawel Jakub Dawidek 			cmd = &std_commands[i];
17405c91076SPawel Jakub Dawidek 			if (cmd->gc_name == NULL)
17505c91076SPawel Jakub Dawidek 				break;
176138cedfdSPawel Jakub Dawidek 			/*
177138cedfdSPawel Jakub Dawidek 			 * If class defines command, which has the same name as
178138cedfdSPawel Jakub Dawidek 			 * standard command, skip it, because it was already
179138cedfdSPawel Jakub Dawidek 			 * shown on usage().
180138cedfdSPawel Jakub Dawidek 			 */
181138cedfdSPawel Jakub Dawidek 			if (find_command(cmd->gc_name, GEOM_CLASS_CMDS) != NULL)
18205c91076SPawel Jakub Dawidek 				continue;
183c979e206SPawel Jakub Dawidek 			usage_command(cmd, prefix);
18405c91076SPawel Jakub Dawidek 			prefix = "      ";
18505c91076SPawel Jakub Dawidek 		}
18605c91076SPawel Jakub Dawidek 		exit(EXIT_FAILURE);
18705c91076SPawel Jakub Dawidek 	}
18805c91076SPawel Jakub Dawidek }
18905c91076SPawel Jakub Dawidek 
19005c91076SPawel Jakub Dawidek static void
19105c91076SPawel Jakub Dawidek load_module(void)
19205c91076SPawel Jakub Dawidek {
19305c91076SPawel Jakub Dawidek 	char name1[64], name2[64];
19405c91076SPawel Jakub Dawidek 
19505c91076SPawel Jakub Dawidek 	snprintf(name1, sizeof(name1), "g_%s", class_name);
19605c91076SPawel Jakub Dawidek 	snprintf(name2, sizeof(name2), "geom_%s", class_name);
19705c91076SPawel Jakub Dawidek 	if (modfind(name1) < 0) {
19805c91076SPawel Jakub Dawidek 		/* Not present in kernel, try loading it. */
19905c91076SPawel Jakub Dawidek 		if (kldload(name2) < 0 || modfind(name1) < 0) {
20005c91076SPawel Jakub Dawidek 			if (errno != EEXIST) {
201112adef8SEdward Tomasz Napierala 				err(EXIT_FAILURE, "cannot load %s", name2);
20205c91076SPawel Jakub Dawidek 			}
20305c91076SPawel Jakub Dawidek 		}
20405c91076SPawel Jakub Dawidek 	}
20505c91076SPawel Jakub Dawidek }
20605c91076SPawel Jakub Dawidek 
20705c91076SPawel Jakub Dawidek static int
20805c91076SPawel Jakub Dawidek strlcatf(char *str, size_t size, const char *format, ...)
20905c91076SPawel Jakub Dawidek {
21005c91076SPawel Jakub Dawidek 	size_t len;
21105c91076SPawel Jakub Dawidek 	va_list ap;
21205c91076SPawel Jakub Dawidek 	int ret;
21305c91076SPawel Jakub Dawidek 
21405c91076SPawel Jakub Dawidek 	len = strlen(str);
21505c91076SPawel Jakub Dawidek 	str += len;
21605c91076SPawel Jakub Dawidek 	size -= len;
21705c91076SPawel Jakub Dawidek 
21805c91076SPawel Jakub Dawidek 	va_start(ap, format);
21905c91076SPawel Jakub Dawidek 	ret = vsnprintf(str, size, format, ap);
22005c91076SPawel Jakub Dawidek 	va_end(ap);
22105c91076SPawel Jakub Dawidek 
22205c91076SPawel Jakub Dawidek 	return (ret);
22305c91076SPawel Jakub Dawidek }
22405c91076SPawel Jakub Dawidek 
22505c91076SPawel Jakub Dawidek /*
22605c91076SPawel Jakub Dawidek  * Find given option in options available for given command.
22705c91076SPawel Jakub Dawidek  */
22805c91076SPawel Jakub Dawidek static struct g_option *
22905c91076SPawel Jakub Dawidek find_option(struct g_command *cmd, char ch)
23005c91076SPawel Jakub Dawidek {
23105c91076SPawel Jakub Dawidek 	struct g_option *opt;
23205c91076SPawel Jakub Dawidek 	unsigned i;
23305c91076SPawel Jakub Dawidek 
23405c91076SPawel Jakub Dawidek 	for (i = 0; ; i++) {
23505c91076SPawel Jakub Dawidek 		opt = &cmd->gc_options[i];
23605c91076SPawel Jakub Dawidek 		if (opt->go_name == NULL)
23705c91076SPawel Jakub Dawidek 			return (NULL);
23805c91076SPawel Jakub Dawidek 		if (opt->go_char == ch)
23905c91076SPawel Jakub Dawidek 			return (opt);
24005c91076SPawel Jakub Dawidek 	}
24105c91076SPawel Jakub Dawidek 	/* NOTREACHED */
24205c91076SPawel Jakub Dawidek 	return (NULL);
24305c91076SPawel Jakub Dawidek }
24405c91076SPawel Jakub Dawidek 
24505c91076SPawel Jakub Dawidek /*
24605c91076SPawel Jakub Dawidek  * Add given option to gctl_req.
24705c91076SPawel Jakub Dawidek  */
24805c91076SPawel Jakub Dawidek static void
24905c91076SPawel Jakub Dawidek set_option(struct gctl_req *req, struct g_option *opt, const char *val)
25005c91076SPawel Jakub Dawidek {
251315fcbf7SPawel Jakub Dawidek 	const char *optname;
25235efcc8bSDag-Erling Smørgrav 	uint64_t number;
253a478ea74SPawel Jakub Dawidek 	void *ptr;
25405c91076SPawel Jakub Dawidek 
255315fcbf7SPawel Jakub Dawidek 	if (G_OPT_ISMULTI(opt)) {
256315fcbf7SPawel Jakub Dawidek 		size_t optnamesize;
257315fcbf7SPawel Jakub Dawidek 
258315fcbf7SPawel Jakub Dawidek 		if (G_OPT_NUM(opt) == UCHAR_MAX)
259315fcbf7SPawel Jakub Dawidek 			errx(EXIT_FAILURE, "Too many -%c options.", opt->go_char);
260315fcbf7SPawel Jakub Dawidek 
261315fcbf7SPawel Jakub Dawidek 		/*
262315fcbf7SPawel Jakub Dawidek 		 * Base option name length plus 3 bytes for option number
263315fcbf7SPawel Jakub Dawidek 		 * (max. 255 options) plus 1 byte for terminating '\0'.
264315fcbf7SPawel Jakub Dawidek 		 */
265315fcbf7SPawel Jakub Dawidek 		optnamesize = strlen(opt->go_name) + 3 + 1;
266315fcbf7SPawel Jakub Dawidek 		ptr = malloc(optnamesize);
267315fcbf7SPawel Jakub Dawidek 		if (ptr == NULL)
268315fcbf7SPawel Jakub Dawidek 			errx(EXIT_FAILURE, "No memory.");
269315fcbf7SPawel Jakub Dawidek 		snprintf(ptr, optnamesize, "%s%u", opt->go_name, G_OPT_NUM(opt));
270315fcbf7SPawel Jakub Dawidek 		G_OPT_NUMINC(opt);
271315fcbf7SPawel Jakub Dawidek 		optname = ptr;
272315fcbf7SPawel Jakub Dawidek 	} else {
273315fcbf7SPawel Jakub Dawidek 		optname = opt->go_name;
274315fcbf7SPawel Jakub Dawidek 	}
275315fcbf7SPawel Jakub Dawidek 
276fa5383a2SPawel Jakub Dawidek 	if (G_OPT_TYPE(opt) == G_TYPE_NUMBER) {
277e84091cbSPawel Jakub Dawidek 		if (expand_number(val, &number) == -1) {
278f104beb7SPawel Jakub Dawidek 			err(EXIT_FAILURE, "Invalid value for '%c' argument",
27905c91076SPawel Jakub Dawidek 			    opt->go_char);
28005c91076SPawel Jakub Dawidek 		}
281a478ea74SPawel Jakub Dawidek 		ptr = malloc(sizeof(intmax_t));
282a478ea74SPawel Jakub Dawidek 		if (ptr == NULL)
283a478ea74SPawel Jakub Dawidek 			errx(EXIT_FAILURE, "No memory.");
284a478ea74SPawel Jakub Dawidek 		*(intmax_t *)ptr = number;
285a478ea74SPawel Jakub Dawidek 		opt->go_val = ptr;
286628ec6d3SPawel Jakub Dawidek 		gctl_ro_param(req, optname, sizeof(intmax_t), opt->go_val);
2876fc60008SPawel Jakub Dawidek 	} else if (G_OPT_TYPE(opt) == G_TYPE_STRING) {
288315fcbf7SPawel Jakub Dawidek 		gctl_ro_param(req, optname, -1, val);
2896fc60008SPawel Jakub Dawidek 	} else if (G_OPT_TYPE(opt) == G_TYPE_BOOL) {
290a478ea74SPawel Jakub Dawidek 		ptr = malloc(sizeof(int));
291a478ea74SPawel Jakub Dawidek 		if (ptr == NULL)
29205c91076SPawel Jakub Dawidek 			errx(EXIT_FAILURE, "No memory.");
293a478ea74SPawel Jakub Dawidek 		*(int *)ptr = *val - '0';
294a478ea74SPawel Jakub Dawidek 		opt->go_val = ptr;
295315fcbf7SPawel Jakub Dawidek 		gctl_ro_param(req, optname, sizeof(int), opt->go_val);
2966fc60008SPawel Jakub Dawidek 	} else {
2976fc60008SPawel Jakub Dawidek 		assert(!"Invalid type");
29805c91076SPawel Jakub Dawidek 	}
299315fcbf7SPawel Jakub Dawidek 
300315fcbf7SPawel Jakub Dawidek 	if (G_OPT_ISMULTI(opt))
301315fcbf7SPawel Jakub Dawidek 		free(__DECONST(char *, optname));
30205c91076SPawel Jakub Dawidek }
30305c91076SPawel Jakub Dawidek 
30405c91076SPawel Jakub Dawidek /*
30505c91076SPawel Jakub Dawidek  * 1. Add given argument by caller.
30605c91076SPawel Jakub Dawidek  * 2. Add default values of not given arguments.
30705c91076SPawel Jakub Dawidek  * 3. Add the rest of arguments.
30805c91076SPawel Jakub Dawidek  */
30905c91076SPawel Jakub Dawidek static void
31005c91076SPawel Jakub Dawidek parse_arguments(struct g_command *cmd, struct gctl_req *req, int *argc,
31105c91076SPawel Jakub Dawidek     char ***argv)
31205c91076SPawel Jakub Dawidek {
31305c91076SPawel Jakub Dawidek 	struct g_option *opt;
31405c91076SPawel Jakub Dawidek 	char opts[64];
31505c91076SPawel Jakub Dawidek 	unsigned i;
31605c91076SPawel Jakub Dawidek 	int ch;
31705c91076SPawel Jakub Dawidek 
31805c91076SPawel Jakub Dawidek 	*opts = '\0';
31905c91076SPawel Jakub Dawidek 	if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0)
32005c91076SPawel Jakub Dawidek 		strlcat(opts, "v", sizeof(opts));
32105c91076SPawel Jakub Dawidek 	for (i = 0; ; i++) {
32205c91076SPawel Jakub Dawidek 		opt = &cmd->gc_options[i];
32305c91076SPawel Jakub Dawidek 		if (opt->go_name == NULL)
32405c91076SPawel Jakub Dawidek 			break;
3256fc60008SPawel Jakub Dawidek 		assert(G_OPT_TYPE(opt) != 0);
326315fcbf7SPawel Jakub Dawidek 		assert((opt->go_type & ~(G_TYPE_MASK | G_TYPE_MULTI)) == 0);
327315fcbf7SPawel Jakub Dawidek 		/* Multiple bool arguments makes no sense. */
328315fcbf7SPawel Jakub Dawidek 		assert(G_OPT_TYPE(opt) != G_TYPE_BOOL ||
329315fcbf7SPawel Jakub Dawidek 		    (opt->go_type & G_TYPE_MULTI) == 0);
33005c91076SPawel Jakub Dawidek 		strlcatf(opts, sizeof(opts), "%c", opt->go_char);
3316fc60008SPawel Jakub Dawidek 		if (G_OPT_TYPE(opt) != G_TYPE_BOOL)
33205c91076SPawel Jakub Dawidek 			strlcat(opts, ":", sizeof(opts));
33305c91076SPawel Jakub Dawidek 	}
33405c91076SPawel Jakub Dawidek 
33505c91076SPawel Jakub Dawidek 	/*
33605c91076SPawel Jakub Dawidek 	 * Add specified arguments.
33705c91076SPawel Jakub Dawidek 	 */
33805c91076SPawel Jakub Dawidek 	while ((ch = getopt(*argc, *argv, opts)) != -1) {
33905c91076SPawel Jakub Dawidek 		/* Standard (not passed to kernel) options. */
34005c91076SPawel Jakub Dawidek 		switch (ch) {
34105c91076SPawel Jakub Dawidek 		case 'v':
34205c91076SPawel Jakub Dawidek 			verbose = 1;
34305c91076SPawel Jakub Dawidek 			continue;
34405c91076SPawel Jakub Dawidek 		}
34505c91076SPawel Jakub Dawidek 		/* Options passed to kernel. */
34605c91076SPawel Jakub Dawidek 		opt = find_option(cmd, ch);
34705c91076SPawel Jakub Dawidek 		if (opt == NULL)
348c979e206SPawel Jakub Dawidek 			usage();
349315fcbf7SPawel Jakub Dawidek 		if (!G_OPT_ISMULTI(opt) && G_OPT_ISDONE(opt)) {
3506fc60008SPawel Jakub Dawidek 			warnx("Option '%c' specified twice.", opt->go_char);
351c979e206SPawel Jakub Dawidek 			usage();
35205c91076SPawel Jakub Dawidek 		}
35305c91076SPawel Jakub Dawidek 		G_OPT_DONE(opt);
35405c91076SPawel Jakub Dawidek 
3556fc60008SPawel Jakub Dawidek 		if (G_OPT_TYPE(opt) == G_TYPE_BOOL)
35605c91076SPawel Jakub Dawidek 			set_option(req, opt, "1");
35705c91076SPawel Jakub Dawidek 		else
35805c91076SPawel Jakub Dawidek 			set_option(req, opt, optarg);
35905c91076SPawel Jakub Dawidek 	}
36005c91076SPawel Jakub Dawidek 	*argc -= optind;
36105c91076SPawel Jakub Dawidek 	*argv += optind;
36205c91076SPawel Jakub Dawidek 
36305c91076SPawel Jakub Dawidek 	/*
36405c91076SPawel Jakub Dawidek 	 * Add not specified arguments, but with default values.
36505c91076SPawel Jakub Dawidek 	 */
36605c91076SPawel Jakub Dawidek 	for (i = 0; ; i++) {
36705c91076SPawel Jakub Dawidek 		opt = &cmd->gc_options[i];
36805c91076SPawel Jakub Dawidek 		if (opt->go_name == NULL)
36905c91076SPawel Jakub Dawidek 			break;
37005c91076SPawel Jakub Dawidek 		if (G_OPT_ISDONE(opt))
37105c91076SPawel Jakub Dawidek 			continue;
37205c91076SPawel Jakub Dawidek 
3736fc60008SPawel Jakub Dawidek 		if (G_OPT_TYPE(opt) == G_TYPE_BOOL) {
37405c91076SPawel Jakub Dawidek 			assert(opt->go_val == NULL);
37505c91076SPawel Jakub Dawidek 			set_option(req, opt, "0");
37605c91076SPawel Jakub Dawidek 		} else {
37705c91076SPawel Jakub Dawidek 			if (opt->go_val == NULL) {
3786fc60008SPawel Jakub Dawidek 				warnx("Option '%c' not specified.",
37905c91076SPawel Jakub Dawidek 				    opt->go_char);
380c979e206SPawel Jakub Dawidek 				usage();
3817648b1e9SPawel Jakub Dawidek 			} else if (opt->go_val == G_VAL_OPTIONAL) {
3827648b1e9SPawel Jakub Dawidek 				/* add nothing. */
38305c91076SPawel Jakub Dawidek 			} else {
384946e2f35SPawel Jakub Dawidek 				set_option(req, opt, opt->go_val);
38505c91076SPawel Jakub Dawidek 			}
38605c91076SPawel Jakub Dawidek 		}
38705c91076SPawel Jakub Dawidek 	}
3883cf55d3aSMarcel Moolenaar 
38905c91076SPawel Jakub Dawidek 	/*
39005c91076SPawel Jakub Dawidek 	 * Add rest of given arguments.
39105c91076SPawel Jakub Dawidek 	 */
39205c91076SPawel Jakub Dawidek 	gctl_ro_param(req, "nargs", sizeof(int), argc);
39305c91076SPawel Jakub Dawidek 	for (i = 0; i < (unsigned)*argc; i++) {
39405c91076SPawel Jakub Dawidek 		char argname[16];
39505c91076SPawel Jakub Dawidek 
39605c91076SPawel Jakub Dawidek 		snprintf(argname, sizeof(argname), "arg%u", i);
39705c91076SPawel Jakub Dawidek 		gctl_ro_param(req, argname, -1, (*argv)[i]);
39805c91076SPawel Jakub Dawidek 	}
39905c91076SPawel Jakub Dawidek }
40005c91076SPawel Jakub Dawidek 
40105c91076SPawel Jakub Dawidek /*
40205c91076SPawel Jakub Dawidek  * Find given command in commands available for given class.
40305c91076SPawel Jakub Dawidek  */
40405c91076SPawel Jakub Dawidek static struct g_command *
405138cedfdSPawel Jakub Dawidek find_command(const char *cmdstr, int flags)
40605c91076SPawel Jakub Dawidek {
40705c91076SPawel Jakub Dawidek 	struct g_command *cmd;
40805c91076SPawel Jakub Dawidek 	unsigned i;
40905c91076SPawel Jakub Dawidek 
41005c91076SPawel Jakub Dawidek 	/*
41105c91076SPawel Jakub Dawidek 	 * First try to find command defined by loaded library.
41205c91076SPawel Jakub Dawidek 	 */
413138cedfdSPawel Jakub Dawidek 	if ((flags & GEOM_CLASS_CMDS) != 0 && class_commands != NULL) {
41405c91076SPawel Jakub Dawidek 		for (i = 0; ; i++) {
41505c91076SPawel Jakub Dawidek 			cmd = &class_commands[i];
41605c91076SPawel Jakub Dawidek 			if (cmd->gc_name == NULL)
41705c91076SPawel Jakub Dawidek 				break;
41805c91076SPawel Jakub Dawidek 			if (strcmp(cmd->gc_name, cmdstr) == 0)
41905c91076SPawel Jakub Dawidek 				return (cmd);
42005c91076SPawel Jakub Dawidek 		}
42105c91076SPawel Jakub Dawidek 	}
42205c91076SPawel Jakub Dawidek 	/*
42305c91076SPawel Jakub Dawidek 	 * Now try to find in standard commands.
42405c91076SPawel Jakub Dawidek 	 */
425138cedfdSPawel Jakub Dawidek 	if ((flags & GEOM_STD_CMDS) != 0) {
42605c91076SPawel Jakub Dawidek 		for (i = 0; ; i++) {
42705c91076SPawel Jakub Dawidek 			cmd = &std_commands[i];
42805c91076SPawel Jakub Dawidek 			if (cmd->gc_name == NULL)
42905c91076SPawel Jakub Dawidek 				break;
43005c91076SPawel Jakub Dawidek 			if (strcmp(cmd->gc_name, cmdstr) == 0)
43105c91076SPawel Jakub Dawidek 				return (cmd);
43205c91076SPawel Jakub Dawidek 		}
433138cedfdSPawel Jakub Dawidek 	}
43405c91076SPawel Jakub Dawidek 	return (NULL);
43505c91076SPawel Jakub Dawidek }
43605c91076SPawel Jakub Dawidek 
43705c91076SPawel Jakub Dawidek static unsigned
43805c91076SPawel Jakub Dawidek set_flags(struct g_command *cmd)
43905c91076SPawel Jakub Dawidek {
44005c91076SPawel Jakub Dawidek 	unsigned flags = 0;
44105c91076SPawel Jakub Dawidek 
44205c91076SPawel Jakub Dawidek 	if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0 && verbose)
44305c91076SPawel Jakub Dawidek 		flags |= G_FLAG_VERBOSE;
44405c91076SPawel Jakub Dawidek 
44505c91076SPawel Jakub Dawidek 	return (flags);
44605c91076SPawel Jakub Dawidek }
44705c91076SPawel Jakub Dawidek 
44805c91076SPawel Jakub Dawidek /*
44905c91076SPawel Jakub Dawidek  * Run command.
45005c91076SPawel Jakub Dawidek  */
45105c91076SPawel Jakub Dawidek static void
45205c91076SPawel Jakub Dawidek run_command(int argc, char *argv[])
45305c91076SPawel Jakub Dawidek {
45405c91076SPawel Jakub Dawidek 	struct g_command *cmd;
45505c91076SPawel Jakub Dawidek 	struct gctl_req *req;
45605c91076SPawel Jakub Dawidek 	const char *errstr;
45705c91076SPawel Jakub Dawidek 	char buf[4096];
45805c91076SPawel Jakub Dawidek 
459138cedfdSPawel Jakub Dawidek 	/* First try to find a command defined by a class. */
460138cedfdSPawel Jakub Dawidek 	cmd = find_command(argv[0], GEOM_CLASS_CMDS);
461138cedfdSPawel Jakub Dawidek 	if (cmd == NULL) {
462138cedfdSPawel Jakub Dawidek 		/* Now, try to find a standard command. */
463138cedfdSPawel Jakub Dawidek 		cmd = find_command(argv[0], GEOM_STD_CMDS);
46405c91076SPawel Jakub Dawidek 		if (cmd == NULL) {
4656fc60008SPawel Jakub Dawidek 			warnx("Unknown command: %s.", argv[0]);
466c979e206SPawel Jakub Dawidek 			usage();
46705c91076SPawel Jakub Dawidek 		}
468138cedfdSPawel Jakub Dawidek 		if (!std_available(cmd->gc_name)) {
469112adef8SEdward Tomasz Napierala 			warnx("Command '%s' not available; "
470112adef8SEdward Tomasz Napierala 			    "try 'load' first.", argv[0]);
471138cedfdSPawel Jakub Dawidek 			exit(EXIT_FAILURE);
472138cedfdSPawel Jakub Dawidek 		}
473138cedfdSPawel Jakub Dawidek 	}
47405c91076SPawel Jakub Dawidek 	if ((cmd->gc_flags & G_FLAG_LOADKLD) != 0)
47505c91076SPawel Jakub Dawidek 		load_module();
47605c91076SPawel Jakub Dawidek 
47705c91076SPawel Jakub Dawidek 	req = gctl_get_handle();
47805c91076SPawel Jakub Dawidek 	gctl_ro_param(req, "class", -1, gclass_name);
47905c91076SPawel Jakub Dawidek 	gctl_ro_param(req, "verb", -1, argv[0]);
48005c91076SPawel Jakub Dawidek 	if (version != NULL)
48105c91076SPawel Jakub Dawidek 		gctl_ro_param(req, "version", sizeof(*version), version);
48205c91076SPawel Jakub Dawidek 	parse_arguments(cmd, req, &argc, &argv);
48305c91076SPawel Jakub Dawidek 
4841bcfab7fSPawel Jakub Dawidek 	bzero(buf, sizeof(buf));
48505c91076SPawel Jakub Dawidek 	if (cmd->gc_func != NULL) {
48605c91076SPawel Jakub Dawidek 		unsigned flags;
48705c91076SPawel Jakub Dawidek 
48805c91076SPawel Jakub Dawidek 		flags = set_flags(cmd);
48905c91076SPawel Jakub Dawidek 		cmd->gc_func(req, flags);
49005c91076SPawel Jakub Dawidek 		errstr = req->error;
49105c91076SPawel Jakub Dawidek 	} else {
49205c91076SPawel Jakub Dawidek 		gctl_rw_param(req, "output", sizeof(buf), buf);
49305c91076SPawel Jakub Dawidek 		errstr = gctl_issue(req);
49405c91076SPawel Jakub Dawidek 	}
49553767efdSPawel Jakub Dawidek 	if (errstr != NULL && errstr[0] != '\0') {
4966fc60008SPawel Jakub Dawidek 		warnx("%s", errstr);
497f792f1d8SPawel Jakub Dawidek 		if (strncmp(errstr, "warning: ", strlen("warning: ")) != 0) {
49805c91076SPawel Jakub Dawidek 			gctl_free(req);
49905c91076SPawel Jakub Dawidek 			exit(EXIT_FAILURE);
50005c91076SPawel Jakub Dawidek 		}
501f792f1d8SPawel Jakub Dawidek 	}
5021bcfab7fSPawel Jakub Dawidek 	if (buf[0] != '\0')
50305c91076SPawel Jakub Dawidek 		printf("%s", buf);
50405c91076SPawel Jakub Dawidek 	gctl_free(req);
50505c91076SPawel Jakub Dawidek 	if (verbose)
50605c91076SPawel Jakub Dawidek 		printf("Done.\n");
50705c91076SPawel Jakub Dawidek 	exit(EXIT_SUCCESS);
50805c91076SPawel Jakub Dawidek }
50905c91076SPawel Jakub Dawidek 
51014bf405bSMarcel Moolenaar #ifndef STATIC_GEOM_CLASSES
5113bad06e9SPawel Jakub Dawidek static const char *
5123bad06e9SPawel Jakub Dawidek library_path(void)
5133bad06e9SPawel Jakub Dawidek {
5143bad06e9SPawel Jakub Dawidek 	const char *path;
5153bad06e9SPawel Jakub Dawidek 
5163bad06e9SPawel Jakub Dawidek 	path = getenv("GEOM_LIBRARY_PATH");
5173bad06e9SPawel Jakub Dawidek 	if (path == NULL)
5188494b738SDavid E. O'Brien 		path = GEOM_CLASS_DIR;
5193bad06e9SPawel Jakub Dawidek 	return (path);
5203bad06e9SPawel Jakub Dawidek }
5213bad06e9SPawel Jakub Dawidek 
52205c91076SPawel Jakub Dawidek static void
52305c91076SPawel Jakub Dawidek load_library(void)
52405c91076SPawel Jakub Dawidek {
5255d824386SUlf Lilleengen 	char *curpath, path[MAXPATHLEN], *tofree, *totalpath;
52605c91076SPawel Jakub Dawidek 	uint32_t *lib_version;
52705c91076SPawel Jakub Dawidek 	void *dlh;
5282591e96cSUlf Lilleengen 	int ret;
52905c91076SPawel Jakub Dawidek 
5302591e96cSUlf Lilleengen 	ret = 0;
5315d824386SUlf Lilleengen 	tofree = totalpath = strdup(library_path());
5322591e96cSUlf Lilleengen 	if (totalpath == NULL)
5332591e96cSUlf Lilleengen 		err(EXIT_FAILURE, "Not enough memory for library path");
5342591e96cSUlf Lilleengen 
5352591e96cSUlf Lilleengen 	if (strchr(totalpath, ':') != NULL)
5362591e96cSUlf Lilleengen 		curpath = strsep(&totalpath, ":");
5372591e96cSUlf Lilleengen 	else
5382591e96cSUlf Lilleengen 		curpath = totalpath;
5392591e96cSUlf Lilleengen 	/* Traverse the paths to find one that contains the library we want. */
5402591e96cSUlf Lilleengen 	while (curpath != NULL) {
5412591e96cSUlf Lilleengen 		snprintf(path, sizeof(path), "%s/geom_%s.so", curpath,
5423bad06e9SPawel Jakub Dawidek 		    class_name);
5432591e96cSUlf Lilleengen 		ret = access(path, F_OK);
5442591e96cSUlf Lilleengen 		if (ret == -1) {
545a73148d2SPawel Jakub Dawidek 			if (errno == ENOENT) {
54605c91076SPawel Jakub Dawidek 				/*
5472591e96cSUlf Lilleengen 				 * If we cannot find library, try the next
5482591e96cSUlf Lilleengen 				 * path.
54905c91076SPawel Jakub Dawidek 				 */
5502591e96cSUlf Lilleengen 				curpath = strsep(&totalpath, ":");
5512591e96cSUlf Lilleengen 				continue;
55205c91076SPawel Jakub Dawidek 			}
553a73148d2SPawel Jakub Dawidek 			err(EXIT_FAILURE, "Cannot access library");
554a73148d2SPawel Jakub Dawidek 		}
5552591e96cSUlf Lilleengen 		break;
5562591e96cSUlf Lilleengen 	}
5575d824386SUlf Lilleengen 	free(tofree);
5582591e96cSUlf Lilleengen 	/* No library was found, but standard commands can still be used */
5592591e96cSUlf Lilleengen 	if (ret == -1)
5602591e96cSUlf Lilleengen 		return;
561a73148d2SPawel Jakub Dawidek 	dlh = dlopen(path, RTLD_NOW);
562a73148d2SPawel Jakub Dawidek 	if (dlh == NULL)
563a73148d2SPawel Jakub Dawidek 		errx(EXIT_FAILURE, "Cannot open library: %s.", dlerror());
56405c91076SPawel Jakub Dawidek 	lib_version = dlsym(dlh, "lib_version");
56505c91076SPawel Jakub Dawidek 	if (lib_version == NULL) {
5666fc60008SPawel Jakub Dawidek 		warnx("Cannot find symbol %s: %s.", "lib_version", dlerror());
56705c91076SPawel Jakub Dawidek 		dlclose(dlh);
56805c91076SPawel Jakub Dawidek 		exit(EXIT_FAILURE);
56905c91076SPawel Jakub Dawidek 	}
57005c91076SPawel Jakub Dawidek 	if (*lib_version != G_LIB_VERSION) {
57105c91076SPawel Jakub Dawidek 		dlclose(dlh);
572f792f1d8SPawel Jakub Dawidek 		errx(EXIT_FAILURE, "%s and %s are not synchronized.",
573f792f1d8SPawel Jakub Dawidek 		    getprogname(), path);
57405c91076SPawel Jakub Dawidek 	}
57505c91076SPawel Jakub Dawidek 	version = dlsym(dlh, "version");
57605c91076SPawel Jakub Dawidek 	if (version == NULL) {
5776fc60008SPawel Jakub Dawidek 		warnx("Cannot find symbol %s: %s.", "version", dlerror());
57805c91076SPawel Jakub Dawidek 		dlclose(dlh);
57905c91076SPawel Jakub Dawidek 		exit(EXIT_FAILURE);
58005c91076SPawel Jakub Dawidek 	}
58105c91076SPawel Jakub Dawidek 	class_commands = dlsym(dlh, "class_commands");
58205c91076SPawel Jakub Dawidek 	if (class_commands == NULL) {
5836fc60008SPawel Jakub Dawidek 		warnx("Cannot find symbol %s: %s.", "class_commands",
5846fc60008SPawel Jakub Dawidek 		    dlerror());
58505c91076SPawel Jakub Dawidek 		dlclose(dlh);
58605c91076SPawel Jakub Dawidek 		exit(EXIT_FAILURE);
58705c91076SPawel Jakub Dawidek 	}
58805c91076SPawel Jakub Dawidek }
58914bf405bSMarcel Moolenaar #endif	/* !STATIC_GEOM_CLASSES */
59005c91076SPawel Jakub Dawidek 
59105c91076SPawel Jakub Dawidek /*
59205c91076SPawel Jakub Dawidek  * Class name should be all capital letters.
59305c91076SPawel Jakub Dawidek  */
59405c91076SPawel Jakub Dawidek static void
59505c91076SPawel Jakub Dawidek set_class_name(void)
59605c91076SPawel Jakub Dawidek {
59705c91076SPawel Jakub Dawidek 	char *s1, *s2;
59805c91076SPawel Jakub Dawidek 
599ee602fbbSPawel Jakub Dawidek 	s1 = class_name;
600ee602fbbSPawel Jakub Dawidek 	for (; *s1 != '\0'; s1++)
601ee602fbbSPawel Jakub Dawidek 		*s1 = tolower(*s1);
602cb94ab30SPawel Jakub Dawidek 	gclass_name = malloc(strlen(class_name) + 1);
60305c91076SPawel Jakub Dawidek 	if (gclass_name == NULL)
60405c91076SPawel Jakub Dawidek 		errx(EXIT_FAILURE, "No memory");
60505c91076SPawel Jakub Dawidek 	s1 = gclass_name;
60605c91076SPawel Jakub Dawidek 	s2 = class_name;
60705c91076SPawel Jakub Dawidek 	for (; *s2 != '\0'; s2++)
60805c91076SPawel Jakub Dawidek 		*s1++ = toupper(*s2);
60905c91076SPawel Jakub Dawidek 	*s1 = '\0';
61005c91076SPawel Jakub Dawidek }
61105c91076SPawel Jakub Dawidek 
61205c91076SPawel Jakub Dawidek static void
61305c91076SPawel Jakub Dawidek get_class(int *argc, char ***argv)
61405c91076SPawel Jakub Dawidek {
61505c91076SPawel Jakub Dawidek 
61605c91076SPawel Jakub Dawidek 	snprintf(comm, sizeof(comm), "%s", basename((*argv)[0]));
61705c91076SPawel Jakub Dawidek 	if (strcmp(comm, "geom") == 0) {
61805c91076SPawel Jakub Dawidek 		if (*argc < 2)
619c979e206SPawel Jakub Dawidek 			usage();
620687b0015SPawel Jakub Dawidek 		else if (*argc == 2) {
621687b0015SPawel Jakub Dawidek 			if (strcmp((*argv)[1], "-h") == 0 ||
622687b0015SPawel Jakub Dawidek 			    strcmp((*argv)[1], "help") == 0) {
623c979e206SPawel Jakub Dawidek 				usage();
624687b0015SPawel Jakub Dawidek 			}
625687b0015SPawel Jakub Dawidek 		}
62605c91076SPawel Jakub Dawidek 		strlcatf(comm, sizeof(comm), " %s", (*argv)[1]);
62705c91076SPawel Jakub Dawidek 		class_name = (*argv)[1];
62805c91076SPawel Jakub Dawidek 		*argc -= 2;
62905c91076SPawel Jakub Dawidek 		*argv += 2;
63005c91076SPawel Jakub Dawidek 	} else if (*comm == 'g') {
63105c91076SPawel Jakub Dawidek 		class_name = comm + 1;
63205c91076SPawel Jakub Dawidek 		*argc -= 1;
63305c91076SPawel Jakub Dawidek 		*argv += 1;
63405c91076SPawel Jakub Dawidek 	} else {
63505c91076SPawel Jakub Dawidek 		errx(EXIT_FAILURE, "Invalid utility name.");
63605c91076SPawel Jakub Dawidek 	}
637a16f9b36SMarcel Moolenaar 
63814bf405bSMarcel Moolenaar #ifndef STATIC_GEOM_CLASSES
63905c91076SPawel Jakub Dawidek 	load_library();
640a16f9b36SMarcel Moolenaar #else
641a16f9b36SMarcel Moolenaar 	if (!strcasecmp(class_name, "part")) {
642a16f9b36SMarcel Moolenaar 		version = &gpart_version;
643a16f9b36SMarcel Moolenaar 		class_commands = gpart_class_commands;
644bc69d66fSXin LI 	} else if (!strcasecmp(class_name, "label")) {
645bc69d66fSXin LI 		version = &glabel_version;
646bc69d66fSXin LI 		class_commands = glabel_class_commands;
647c6678483SAlexander Motin 	}
64814bf405bSMarcel Moolenaar #endif /* !STATIC_GEOM_CLASSES */
649a16f9b36SMarcel Moolenaar 
650a16f9b36SMarcel Moolenaar 	set_class_name();
651e2d52f14SMark Felder 
652e2d52f14SMark Felder 	/* If we can't load or list, it's not a class. */
653e2d52f14SMark Felder 	if (!std_available("load") && !std_available("list"))
654112adef8SEdward Tomasz Napierala 		errx(EXIT_FAILURE, "Invalid class name '%s'.", class_name);
655e2d52f14SMark Felder 
65605c91076SPawel Jakub Dawidek 	if (*argc < 1)
657c979e206SPawel Jakub Dawidek 		usage();
65805c91076SPawel Jakub Dawidek }
65905c91076SPawel Jakub Dawidek 
6600f73f701SEdward Tomasz Napierala static struct ggeom *
6610f73f701SEdward Tomasz Napierala find_geom_by_provider(struct gmesh *mesh, const char *name)
6620f73f701SEdward Tomasz Napierala {
6630f73f701SEdward Tomasz Napierala 	struct gclass *classp;
6640f73f701SEdward Tomasz Napierala 	struct ggeom *gp;
6650f73f701SEdward Tomasz Napierala 	struct gprovider *pp;
6660f73f701SEdward Tomasz Napierala 
6670f73f701SEdward Tomasz Napierala 	LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
6680f73f701SEdward Tomasz Napierala 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
6690f73f701SEdward Tomasz Napierala 			LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
6700f73f701SEdward Tomasz Napierala 				if (strcmp(pp->lg_name, name) == 0)
6710f73f701SEdward Tomasz Napierala 					return (gp);
6720f73f701SEdward Tomasz Napierala 			}
6730f73f701SEdward Tomasz Napierala 		}
6740f73f701SEdward Tomasz Napierala 	}
6750f73f701SEdward Tomasz Napierala 
6760f73f701SEdward Tomasz Napierala 	return (NULL);
6770f73f701SEdward Tomasz Napierala }
6780f73f701SEdward Tomasz Napierala 
679*6a027382SEdward Tomasz Napierala static int
680*6a027382SEdward Tomasz Napierala compute_tree_width_geom(struct gmesh *mesh, struct ggeom *gp, int indent)
681*6a027382SEdward Tomasz Napierala {
682*6a027382SEdward Tomasz Napierala 	struct gclass *classp2;
683*6a027382SEdward Tomasz Napierala 	struct ggeom *gp2;
684*6a027382SEdward Tomasz Napierala 	struct gconsumer *cp2;
685*6a027382SEdward Tomasz Napierala 	struct gprovider *pp;
686*6a027382SEdward Tomasz Napierala 	int max_width, width;
687*6a027382SEdward Tomasz Napierala 
688*6a027382SEdward Tomasz Napierala 	max_width = width = indent + strlen(gp->lg_name);
689*6a027382SEdward Tomasz Napierala 
690*6a027382SEdward Tomasz Napierala 	LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
691*6a027382SEdward Tomasz Napierala 		LIST_FOREACH(classp2, &mesh->lg_class, lg_class) {
692*6a027382SEdward Tomasz Napierala 			LIST_FOREACH(gp2, &classp2->lg_geom, lg_geom) {
693*6a027382SEdward Tomasz Napierala 				LIST_FOREACH(cp2,
694*6a027382SEdward Tomasz Napierala 				    &gp2->lg_consumer, lg_consumer) {
695*6a027382SEdward Tomasz Napierala 					if (pp != cp2->lg_provider)
696*6a027382SEdward Tomasz Napierala 						continue;
697*6a027382SEdward Tomasz Napierala 					width = compute_tree_width_geom(mesh,
698*6a027382SEdward Tomasz Napierala 					    gp2, indent + 2);
699*6a027382SEdward Tomasz Napierala 					if (width > max_width)
700*6a027382SEdward Tomasz Napierala 						max_width = width;
701*6a027382SEdward Tomasz Napierala 				}
702*6a027382SEdward Tomasz Napierala 			}
703*6a027382SEdward Tomasz Napierala 		}
704*6a027382SEdward Tomasz Napierala 	}
705*6a027382SEdward Tomasz Napierala 
706*6a027382SEdward Tomasz Napierala 	return (max_width);
707*6a027382SEdward Tomasz Napierala }
708*6a027382SEdward Tomasz Napierala 
709*6a027382SEdward Tomasz Napierala static int
710*6a027382SEdward Tomasz Napierala compute_tree_width(struct gmesh *mesh)
711*6a027382SEdward Tomasz Napierala {
712*6a027382SEdward Tomasz Napierala 	struct gclass *classp;
713*6a027382SEdward Tomasz Napierala 	struct ggeom *gp;
714*6a027382SEdward Tomasz Napierala 	int max_width, width;
715*6a027382SEdward Tomasz Napierala 
716*6a027382SEdward Tomasz Napierala 	max_width = width = 0;
717*6a027382SEdward Tomasz Napierala 
718*6a027382SEdward Tomasz Napierala 	LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
719*6a027382SEdward Tomasz Napierala 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
720*6a027382SEdward Tomasz Napierala 			if (!LIST_EMPTY(&gp->lg_consumer))
721*6a027382SEdward Tomasz Napierala 				continue;
722*6a027382SEdward Tomasz Napierala 			width = compute_tree_width_geom(mesh, gp, 0);
723*6a027382SEdward Tomasz Napierala 			if (width > max_width)
724*6a027382SEdward Tomasz Napierala 				max_width = width;
725*6a027382SEdward Tomasz Napierala 		}
726*6a027382SEdward Tomasz Napierala 	}
727*6a027382SEdward Tomasz Napierala 
728*6a027382SEdward Tomasz Napierala 	return (max_width);
729*6a027382SEdward Tomasz Napierala }
730*6a027382SEdward Tomasz Napierala 
731*6a027382SEdward Tomasz Napierala static void
732*6a027382SEdward Tomasz Napierala show_tree_geom(struct gmesh *mesh, struct ggeom *gp, int indent, int width)
733*6a027382SEdward Tomasz Napierala {
734*6a027382SEdward Tomasz Napierala 	struct gclass *classp2;
735*6a027382SEdward Tomasz Napierala 	struct ggeom *gp2;
736*6a027382SEdward Tomasz Napierala 	struct gconsumer *cp2;
737*6a027382SEdward Tomasz Napierala 	struct gprovider *pp;
738*6a027382SEdward Tomasz Napierala 
739*6a027382SEdward Tomasz Napierala 	if (LIST_EMPTY(&gp->lg_provider)) {
740*6a027382SEdward Tomasz Napierala 		printf("%*s%-*.*s %-*.*s\n", indent, "",
741*6a027382SEdward Tomasz Napierala 		    width - indent, width - indent, gp->lg_name,
742*6a027382SEdward Tomasz Napierala 		    GEOM_CLASS_WIDTH, GEOM_CLASS_WIDTH, gp->lg_class->lg_name);
743*6a027382SEdward Tomasz Napierala 		return;
744*6a027382SEdward Tomasz Napierala 	}
745*6a027382SEdward Tomasz Napierala 
746*6a027382SEdward Tomasz Napierala 	LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
747*6a027382SEdward Tomasz Napierala 		printf("%*s%-*.*s %-*.*s %s\n", indent, "",
748*6a027382SEdward Tomasz Napierala 		    width - indent, width - indent, gp->lg_name,
749*6a027382SEdward Tomasz Napierala 		    GEOM_CLASS_WIDTH, GEOM_CLASS_WIDTH, gp->lg_class->lg_name,
750*6a027382SEdward Tomasz Napierala 		    pp->lg_name);
751*6a027382SEdward Tomasz Napierala 
752*6a027382SEdward Tomasz Napierala 		LIST_FOREACH(classp2, &mesh->lg_class, lg_class) {
753*6a027382SEdward Tomasz Napierala 			LIST_FOREACH(gp2, &classp2->lg_geom, lg_geom) {
754*6a027382SEdward Tomasz Napierala 				LIST_FOREACH(cp2,
755*6a027382SEdward Tomasz Napierala 				    &gp2->lg_consumer, lg_consumer) {
756*6a027382SEdward Tomasz Napierala 					if (pp != cp2->lg_provider)
757*6a027382SEdward Tomasz Napierala 						continue;
758*6a027382SEdward Tomasz Napierala 					show_tree_geom(mesh, gp2,
759*6a027382SEdward Tomasz Napierala 					    indent + 2, width);
760*6a027382SEdward Tomasz Napierala 				}
761*6a027382SEdward Tomasz Napierala 			}
762*6a027382SEdward Tomasz Napierala 		}
763*6a027382SEdward Tomasz Napierala 	}
764*6a027382SEdward Tomasz Napierala }
765*6a027382SEdward Tomasz Napierala 
766*6a027382SEdward Tomasz Napierala static void
767*6a027382SEdward Tomasz Napierala show_tree(void)
768*6a027382SEdward Tomasz Napierala {
769*6a027382SEdward Tomasz Napierala 	struct gmesh mesh;
770*6a027382SEdward Tomasz Napierala 	struct gclass *classp;
771*6a027382SEdward Tomasz Napierala 	struct ggeom *gp;
772*6a027382SEdward Tomasz Napierala 	int error, width;
773*6a027382SEdward Tomasz Napierala 
774*6a027382SEdward Tomasz Napierala 	error = geom_gettree(&mesh);
775*6a027382SEdward Tomasz Napierala 	if (error != 0)
776*6a027382SEdward Tomasz Napierala 		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
777*6a027382SEdward Tomasz Napierala 
778*6a027382SEdward Tomasz Napierala 	width = compute_tree_width(&mesh);
779*6a027382SEdward Tomasz Napierala 
780*6a027382SEdward Tomasz Napierala 	printf("%-*.*s %-*.*s %s\n",
781*6a027382SEdward Tomasz Napierala 	    width, width, "Geom",
782*6a027382SEdward Tomasz Napierala 	    GEOM_CLASS_WIDTH, GEOM_CLASS_WIDTH, "Class",
783*6a027382SEdward Tomasz Napierala 	    "Provider");
784*6a027382SEdward Tomasz Napierala 
785*6a027382SEdward Tomasz Napierala 	LIST_FOREACH(classp, &mesh.lg_class, lg_class) {
786*6a027382SEdward Tomasz Napierala 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
787*6a027382SEdward Tomasz Napierala 			if (!LIST_EMPTY(&gp->lg_consumer))
788*6a027382SEdward Tomasz Napierala 				continue;
789*6a027382SEdward Tomasz Napierala 			show_tree_geom(&mesh, gp, 0, width);
790*6a027382SEdward Tomasz Napierala 		}
791*6a027382SEdward Tomasz Napierala 	}
792*6a027382SEdward Tomasz Napierala }
793*6a027382SEdward Tomasz Napierala 
79405c91076SPawel Jakub Dawidek int
79505c91076SPawel Jakub Dawidek main(int argc, char *argv[])
79605c91076SPawel Jakub Dawidek {
7970f73f701SEdward Tomasz Napierala 	char *provider_name;
798*6a027382SEdward Tomasz Napierala 	bool tflag;
7990f73f701SEdward Tomasz Napierala 	int ch;
8000f73f701SEdward Tomasz Napierala 
8010f73f701SEdward Tomasz Napierala 	provider_name = NULL;
802*6a027382SEdward Tomasz Napierala 	tflag = false;
8030f73f701SEdward Tomasz Napierala 
8040f73f701SEdward Tomasz Napierala 	if (strcmp(getprogname(), "geom") == 0) {
805*6a027382SEdward Tomasz Napierala 		while ((ch = getopt(argc, argv, "hp:t")) != -1) {
8060f73f701SEdward Tomasz Napierala 			switch (ch) {
8070f73f701SEdward Tomasz Napierala 			case 'p':
8080f73f701SEdward Tomasz Napierala 				provider_name = strdup(optarg);
8090f73f701SEdward Tomasz Napierala 				if (provider_name == NULL)
8100f73f701SEdward Tomasz Napierala 					err(1, "strdup");
8110f73f701SEdward Tomasz Napierala 				break;
812*6a027382SEdward Tomasz Napierala 			case 't':
813*6a027382SEdward Tomasz Napierala 				tflag = true;
814*6a027382SEdward Tomasz Napierala 				break;
8150f73f701SEdward Tomasz Napierala 			case 'h':
8160f73f701SEdward Tomasz Napierala 			default:
8170f73f701SEdward Tomasz Napierala 				usage();
8180f73f701SEdward Tomasz Napierala 			}
8190f73f701SEdward Tomasz Napierala 		}
8200f73f701SEdward Tomasz Napierala 
8210f73f701SEdward Tomasz Napierala 		/*
8220f73f701SEdward Tomasz Napierala 		 * Don't adjust argc and argv, it would break get_class().
8230f73f701SEdward Tomasz Napierala 		 */
8240f73f701SEdward Tomasz Napierala 	}
8250f73f701SEdward Tomasz Napierala 
826*6a027382SEdward Tomasz Napierala 	if (tflag && provider_name != NULL) {
827*6a027382SEdward Tomasz Napierala 		errx(EXIT_FAILURE,
828*6a027382SEdward Tomasz Napierala 		    "At most one of -P and -t may be specified.");
829*6a027382SEdward Tomasz Napierala 	}
830*6a027382SEdward Tomasz Napierala 
8310f73f701SEdward Tomasz Napierala 	if (provider_name != NULL) {
8320f73f701SEdward Tomasz Napierala 		list_one_geom_by_provider(provider_name);
8330f73f701SEdward Tomasz Napierala 		return (0);
8340f73f701SEdward Tomasz Napierala 	}
83505c91076SPawel Jakub Dawidek 
836*6a027382SEdward Tomasz Napierala 	if (tflag) {
837*6a027382SEdward Tomasz Napierala 		show_tree();
838*6a027382SEdward Tomasz Napierala 		return (0);
839*6a027382SEdward Tomasz Napierala 	}
840*6a027382SEdward Tomasz Napierala 
84105c91076SPawel Jakub Dawidek 	get_class(&argc, &argv);
84205c91076SPawel Jakub Dawidek 	run_command(argc, argv);
84305c91076SPawel Jakub Dawidek 	/* NOTREACHED */
84405c91076SPawel Jakub Dawidek 
84505c91076SPawel Jakub Dawidek 	exit(EXIT_FAILURE);
84605c91076SPawel Jakub Dawidek }
84705c91076SPawel Jakub Dawidek 
84805c91076SPawel Jakub Dawidek static struct gclass *
84905c91076SPawel Jakub Dawidek find_class(struct gmesh *mesh, const char *name)
85005c91076SPawel Jakub Dawidek {
85105c91076SPawel Jakub Dawidek 	struct gclass *classp;
85205c91076SPawel Jakub Dawidek 
85305c91076SPawel Jakub Dawidek 	LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
85405c91076SPawel Jakub Dawidek 		if (strcmp(classp->lg_name, name) == 0)
85505c91076SPawel Jakub Dawidek 			return (classp);
85605c91076SPawel Jakub Dawidek 	}
85705c91076SPawel Jakub Dawidek 	return (NULL);
85805c91076SPawel Jakub Dawidek }
85905c91076SPawel Jakub Dawidek 
8601d723f1dSPawel Jakub Dawidek static struct ggeom *
8611d723f1dSPawel Jakub Dawidek find_geom(struct gclass *classp, const char *name)
86205c91076SPawel Jakub Dawidek {
86305c91076SPawel Jakub Dawidek 	struct ggeom *gp;
86405c91076SPawel Jakub Dawidek 
86505c91076SPawel Jakub Dawidek 	LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
8661d723f1dSPawel Jakub Dawidek 		if (strcmp(gp->lg_name, name) == 0)
8671d723f1dSPawel Jakub Dawidek 			return (gp);
86805c91076SPawel Jakub Dawidek 	}
86905c91076SPawel Jakub Dawidek 	return (NULL);
87005c91076SPawel Jakub Dawidek }
87105c91076SPawel Jakub Dawidek 
87205c91076SPawel Jakub Dawidek static void
8732d76e2dbSPawel Jakub Dawidek list_one_provider(struct gprovider *pp, const char *prefix)
87405c91076SPawel Jakub Dawidek {
87505c91076SPawel Jakub Dawidek 	struct gconfig *conf;
876af565b58SPawel Jakub Dawidek 	char buf[5];
87705c91076SPawel Jakub Dawidek 
8781d723f1dSPawel Jakub Dawidek 	printf("Name: %s\n", pp->lg_name);
879af565b58SPawel Jakub Dawidek 	humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "",
880af565b58SPawel Jakub Dawidek 	    HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
8811d723f1dSPawel Jakub Dawidek 	printf("%sMediasize: %jd (%s)\n", prefix, (intmax_t)pp->lg_mediasize,
8821d723f1dSPawel Jakub Dawidek 	    buf);
8831d723f1dSPawel Jakub Dawidek 	printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize);
884e192c6e8SXin LI 	if (pp->lg_stripesize > 0 || pp->lg_stripeoffset > 0) {
88535daa28fSXin LI 		printf("%sStripesize: %ju\n", prefix, pp->lg_stripesize);
88635daa28fSXin LI 		printf("%sStripeoffset: %ju\n", prefix, pp->lg_stripeoffset);
88735daa28fSXin LI 	}
8881d723f1dSPawel Jakub Dawidek 	printf("%sMode: %s\n", prefix, pp->lg_mode);
88905c91076SPawel Jakub Dawidek 	LIST_FOREACH(conf, &pp->lg_config, lg_config) {
8901d723f1dSPawel Jakub Dawidek 		printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val);
8911d723f1dSPawel Jakub Dawidek 	}
8921d723f1dSPawel Jakub Dawidek }
8931d723f1dSPawel Jakub Dawidek 
8941d723f1dSPawel Jakub Dawidek static void
8952d76e2dbSPawel Jakub Dawidek list_one_consumer(struct gconsumer *cp, const char *prefix)
8961d723f1dSPawel Jakub Dawidek {
8971d723f1dSPawel Jakub Dawidek 	struct gprovider *pp;
8981d723f1dSPawel Jakub Dawidek 	struct gconfig *conf;
8991d723f1dSPawel Jakub Dawidek 
9001d723f1dSPawel Jakub Dawidek 	pp = cp->lg_provider;
9011d723f1dSPawel Jakub Dawidek 	if (pp == NULL)
9021d723f1dSPawel Jakub Dawidek 		printf("[no provider]\n");
9031d723f1dSPawel Jakub Dawidek 	else {
9041d723f1dSPawel Jakub Dawidek 		char buf[5];
9051d723f1dSPawel Jakub Dawidek 
9061d723f1dSPawel Jakub Dawidek 		printf("Name: %s\n", pp->lg_name);
9071d723f1dSPawel Jakub Dawidek 		humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "",
9081d723f1dSPawel Jakub Dawidek 		    HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
9091d723f1dSPawel Jakub Dawidek 		printf("%sMediasize: %jd (%s)\n", prefix,
9101d723f1dSPawel Jakub Dawidek 		    (intmax_t)pp->lg_mediasize, buf);
9111d723f1dSPawel Jakub Dawidek 		printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize);
91200b236aaSXin LI 		if (pp->lg_stripesize > 0 || pp->lg_stripeoffset > 0) {
91335daa28fSXin LI 			printf("%sStripesize: %ju\n", prefix, pp->lg_stripesize);
91435daa28fSXin LI 			printf("%sStripeoffset: %ju\n", prefix, pp->lg_stripeoffset);
91535daa28fSXin LI 		}
9161d723f1dSPawel Jakub Dawidek 		printf("%sMode: %s\n", prefix, cp->lg_mode);
9171d723f1dSPawel Jakub Dawidek 	}
9181d723f1dSPawel Jakub Dawidek 	LIST_FOREACH(conf, &cp->lg_config, lg_config) {
9191d723f1dSPawel Jakub Dawidek 		printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val);
9201d723f1dSPawel Jakub Dawidek 	}
9211d723f1dSPawel Jakub Dawidek }
9221d723f1dSPawel Jakub Dawidek 
9231d723f1dSPawel Jakub Dawidek static void
9242d76e2dbSPawel Jakub Dawidek list_one_geom(struct ggeom *gp)
9251d723f1dSPawel Jakub Dawidek {
9261d723f1dSPawel Jakub Dawidek 	struct gprovider *pp;
9271d723f1dSPawel Jakub Dawidek 	struct gconsumer *cp;
9281d723f1dSPawel Jakub Dawidek 	struct gconfig *conf;
9291d723f1dSPawel Jakub Dawidek 	unsigned n;
9301d723f1dSPawel Jakub Dawidek 
9311d723f1dSPawel Jakub Dawidek 	printf("Geom name: %s\n", gp->lg_name);
9321d723f1dSPawel Jakub Dawidek 	LIST_FOREACH(conf, &gp->lg_config, lg_config) {
9331d723f1dSPawel Jakub Dawidek 		printf("%s: %s\n", conf->lg_name, conf->lg_val);
9341d723f1dSPawel Jakub Dawidek 	}
9351d723f1dSPawel Jakub Dawidek 	if (!LIST_EMPTY(&gp->lg_provider)) {
9361d723f1dSPawel Jakub Dawidek 		printf("Providers:\n");
9371d723f1dSPawel Jakub Dawidek 		n = 1;
9381d723f1dSPawel Jakub Dawidek 		LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
9391d723f1dSPawel Jakub Dawidek 			printf("%u. ", n++);
9402d76e2dbSPawel Jakub Dawidek 			list_one_provider(pp, "   ");
9411d723f1dSPawel Jakub Dawidek 		}
9421d723f1dSPawel Jakub Dawidek 	}
9431d723f1dSPawel Jakub Dawidek 	if (!LIST_EMPTY(&gp->lg_consumer)) {
9441d723f1dSPawel Jakub Dawidek 		printf("Consumers:\n");
9451d723f1dSPawel Jakub Dawidek 		n = 1;
9461d723f1dSPawel Jakub Dawidek 		LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
9471d723f1dSPawel Jakub Dawidek 			printf("%u. ", n++);
9482d76e2dbSPawel Jakub Dawidek 			list_one_consumer(cp, "   ");
9491d723f1dSPawel Jakub Dawidek 		}
95005c91076SPawel Jakub Dawidek 	}
95105c91076SPawel Jakub Dawidek 	printf("\n");
95205c91076SPawel Jakub Dawidek }
95305c91076SPawel Jakub Dawidek 
954b70eccf3SPawel Jakub Dawidek static void
9550f73f701SEdward Tomasz Napierala list_one_geom_by_provider(const char *provider_name)
9560f73f701SEdward Tomasz Napierala {
9570f73f701SEdward Tomasz Napierala 	struct gmesh mesh;
9580f73f701SEdward Tomasz Napierala 	struct ggeom *gp;
9590f73f701SEdward Tomasz Napierala 	int error;
9600f73f701SEdward Tomasz Napierala 
9610f73f701SEdward Tomasz Napierala 	error = geom_gettree(&mesh);
9620f73f701SEdward Tomasz Napierala 	if (error != 0)
9630f73f701SEdward Tomasz Napierala 		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
9640f73f701SEdward Tomasz Napierala 
9650f73f701SEdward Tomasz Napierala 	gp = find_geom_by_provider(&mesh, provider_name);
9660f73f701SEdward Tomasz Napierala 	if (gp == NULL)
9670f73f701SEdward Tomasz Napierala 		errx(EXIT_FAILURE, "Cannot find provider '%s'.", provider_name);
9680f73f701SEdward Tomasz Napierala 
9690f73f701SEdward Tomasz Napierala 	printf("Geom class: %s\n", gp->lg_class->lg_name);
9700f73f701SEdward Tomasz Napierala 	list_one_geom(gp);
9710f73f701SEdward Tomasz Napierala }
9720f73f701SEdward Tomasz Napierala 
9730f73f701SEdward Tomasz Napierala static void
974b70eccf3SPawel Jakub Dawidek std_help(struct gctl_req *req __unused, unsigned flags __unused)
975b70eccf3SPawel Jakub Dawidek {
976b70eccf3SPawel Jakub Dawidek 
977c979e206SPawel Jakub Dawidek 	usage();
978b70eccf3SPawel Jakub Dawidek }
979b70eccf3SPawel Jakub Dawidek 
98005c91076SPawel Jakub Dawidek static int
98105c91076SPawel Jakub Dawidek std_list_available(void)
98205c91076SPawel Jakub Dawidek {
98305c91076SPawel Jakub Dawidek 	struct gmesh mesh;
98405c91076SPawel Jakub Dawidek 	struct gclass *classp;
98505c91076SPawel Jakub Dawidek 	int error;
98605c91076SPawel Jakub Dawidek 
98705c91076SPawel Jakub Dawidek 	error = geom_gettree(&mesh);
9886fc60008SPawel Jakub Dawidek 	if (error != 0)
9896fc60008SPawel Jakub Dawidek 		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
99005c91076SPawel Jakub Dawidek 	classp = find_class(&mesh, gclass_name);
99105c91076SPawel Jakub Dawidek 	geom_deletetree(&mesh);
99205c91076SPawel Jakub Dawidek 	if (classp != NULL)
99305c91076SPawel Jakub Dawidek 		return (1);
99405c91076SPawel Jakub Dawidek 	return (0);
99505c91076SPawel Jakub Dawidek }
99605c91076SPawel Jakub Dawidek 
99705c91076SPawel Jakub Dawidek static void
99805c91076SPawel Jakub Dawidek std_list(struct gctl_req *req, unsigned flags __unused)
99905c91076SPawel Jakub Dawidek {
100005c91076SPawel Jakub Dawidek 	struct gmesh mesh;
100105c91076SPawel Jakub Dawidek 	struct gclass *classp;
10021d723f1dSPawel Jakub Dawidek 	struct ggeom *gp;
1003f13942a7SPawel Jakub Dawidek 	const char *name;
100483d165c1SAlexander Motin 	int all, error, i, nargs;
100505c91076SPawel Jakub Dawidek 
100605c91076SPawel Jakub Dawidek 	error = geom_gettree(&mesh);
10076fc60008SPawel Jakub Dawidek 	if (error != 0)
10086fc60008SPawel Jakub Dawidek 		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
100905c91076SPawel Jakub Dawidek 	classp = find_class(&mesh, gclass_name);
101005c91076SPawel Jakub Dawidek 	if (classp == NULL) {
101105c91076SPawel Jakub Dawidek 		geom_deletetree(&mesh);
1012112adef8SEdward Tomasz Napierala 		errx(EXIT_FAILURE, "Class '%s' not found.", gclass_name);
101305c91076SPawel Jakub Dawidek 	}
1014f13942a7SPawel Jakub Dawidek 	nargs = gctl_get_int(req, "nargs");
101583d165c1SAlexander Motin 	all = gctl_get_int(req, "all");
1016f13942a7SPawel Jakub Dawidek 	if (nargs > 0) {
1017f13942a7SPawel Jakub Dawidek 		for (i = 0; i < nargs; i++) {
1018f13942a7SPawel Jakub Dawidek 			name = gctl_get_ascii(req, "arg%d", i);
10191d723f1dSPawel Jakub Dawidek 			gp = find_geom(classp, name);
1020112adef8SEdward Tomasz Napierala 			if (gp == NULL) {
1021112adef8SEdward Tomasz Napierala 				errx(EXIT_FAILURE, "Class '%s' does not have "
1022112adef8SEdward Tomasz Napierala 				    "an instance named '%s'.",
1023112adef8SEdward Tomasz Napierala 				    gclass_name, name);
1024112adef8SEdward Tomasz Napierala 			}
102583d165c1SAlexander Motin 			list_one_geom(gp);
102605c91076SPawel Jakub Dawidek 		}
102705c91076SPawel Jakub Dawidek 	} else {
102805c91076SPawel Jakub Dawidek 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
102983d165c1SAlexander Motin 			if (LIST_EMPTY(&gp->lg_provider) && !all)
10305fd66a44SPawel Jakub Dawidek 				continue;
10312d76e2dbSPawel Jakub Dawidek 			list_one_geom(gp);
103205c91076SPawel Jakub Dawidek 		}
103305c91076SPawel Jakub Dawidek 	}
103405c91076SPawel Jakub Dawidek 	geom_deletetree(&mesh);
103505c91076SPawel Jakub Dawidek }
103605c91076SPawel Jakub Dawidek 
103705c91076SPawel Jakub Dawidek static int
103818ee8840SPawel Jakub Dawidek std_status_available(void)
103918ee8840SPawel Jakub Dawidek {
104018ee8840SPawel Jakub Dawidek 
104118ee8840SPawel Jakub Dawidek 	/* 'status' command is available when 'list' command is. */
104218ee8840SPawel Jakub Dawidek 	return (std_list_available());
104318ee8840SPawel Jakub Dawidek }
104418ee8840SPawel Jakub Dawidek 
104518ee8840SPawel Jakub Dawidek static void
1046da80913dSPawel Jakub Dawidek status_update_len(struct ggeom *gp, int *name_len, int *status_len)
104718ee8840SPawel Jakub Dawidek {
104818ee8840SPawel Jakub Dawidek 	struct gconfig *conf;
1049da80913dSPawel Jakub Dawidek 	int len;
105018ee8840SPawel Jakub Dawidek 
105118ee8840SPawel Jakub Dawidek 	assert(gp != NULL);
105218ee8840SPawel Jakub Dawidek 	assert(name_len != NULL);
105318ee8840SPawel Jakub Dawidek 	assert(status_len != NULL);
105418ee8840SPawel Jakub Dawidek 
105518ee8840SPawel Jakub Dawidek 	len = strlen(gp->lg_name);
105618ee8840SPawel Jakub Dawidek 	if (*name_len < len)
105718ee8840SPawel Jakub Dawidek 		*name_len = len;
105818ee8840SPawel Jakub Dawidek 	LIST_FOREACH(conf, &gp->lg_config, lg_config) {
105918ee8840SPawel Jakub Dawidek 		if (strcasecmp(conf->lg_name, "state") == 0) {
106018ee8840SPawel Jakub Dawidek 			len = strlen(conf->lg_val);
106118ee8840SPawel Jakub Dawidek 			if (*status_len < len)
106218ee8840SPawel Jakub Dawidek 				*status_len = len;
106318ee8840SPawel Jakub Dawidek 		}
106418ee8840SPawel Jakub Dawidek 	}
106518ee8840SPawel Jakub Dawidek }
106618ee8840SPawel Jakub Dawidek 
106783d165c1SAlexander Motin static void
106883d165c1SAlexander Motin status_update_len_prs(struct ggeom *gp, int *name_len, int *status_len)
106983d165c1SAlexander Motin {
107083d165c1SAlexander Motin 	struct gprovider *pp;
107183d165c1SAlexander Motin 	struct gconfig *conf;
107283d165c1SAlexander Motin 	int len, glen;
107383d165c1SAlexander Motin 
107483d165c1SAlexander Motin 	assert(gp != NULL);
107583d165c1SAlexander Motin 	assert(name_len != NULL);
107683d165c1SAlexander Motin 	assert(status_len != NULL);
107783d165c1SAlexander Motin 
107883d165c1SAlexander Motin 	glen = 0;
107983d165c1SAlexander Motin 	LIST_FOREACH(conf, &gp->lg_config, lg_config) {
108083d165c1SAlexander Motin 		if (strcasecmp(conf->lg_name, "state") == 0) {
108183d165c1SAlexander Motin 			glen = strlen(conf->lg_val);
108283d165c1SAlexander Motin 			break;
108383d165c1SAlexander Motin 		}
108483d165c1SAlexander Motin 	}
108583d165c1SAlexander Motin 	LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
108683d165c1SAlexander Motin 		len = strlen(pp->lg_name);
108783d165c1SAlexander Motin 		if (*name_len < len)
108883d165c1SAlexander Motin 			*name_len = len;
108983d165c1SAlexander Motin 		len = glen;
109083d165c1SAlexander Motin 		LIST_FOREACH(conf, &pp->lg_config, lg_config) {
109183d165c1SAlexander Motin 			if (strcasecmp(conf->lg_name, "state") == 0) {
109283d165c1SAlexander Motin 				len = strlen(conf->lg_val);
109383d165c1SAlexander Motin 				break;
109483d165c1SAlexander Motin 			}
109583d165c1SAlexander Motin 		}
109683d165c1SAlexander Motin 		if (*status_len < len)
109783d165c1SAlexander Motin 			*status_len = len;
109883d165c1SAlexander Motin 	}
109983d165c1SAlexander Motin }
110083d165c1SAlexander Motin 
1101ba6821f0SPawel Jakub Dawidek static char *
110218ee8840SPawel Jakub Dawidek status_one_consumer(struct gconsumer *cp)
110318ee8840SPawel Jakub Dawidek {
1104ba6821f0SPawel Jakub Dawidek 	static char buf[256];
110518ee8840SPawel Jakub Dawidek 	struct gprovider *pp;
110618ee8840SPawel Jakub Dawidek 	struct gconfig *conf;
110783d165c1SAlexander Motin 	const char *state, *syncr;
110818ee8840SPawel Jakub Dawidek 
110918ee8840SPawel Jakub Dawidek 	pp = cp->lg_provider;
111018ee8840SPawel Jakub Dawidek 	if (pp == NULL)
1111ba6821f0SPawel Jakub Dawidek 		return (NULL);
111283d165c1SAlexander Motin 	state = NULL;
111383d165c1SAlexander Motin 	syncr = NULL;
111418ee8840SPawel Jakub Dawidek 	LIST_FOREACH(conf, &cp->lg_config, lg_config) {
111583d165c1SAlexander Motin 		if (strcasecmp(conf->lg_name, "state") == 0)
111683d165c1SAlexander Motin 			state = conf->lg_val;
111718ee8840SPawel Jakub Dawidek 		if (strcasecmp(conf->lg_name, "synchronized") == 0)
111883d165c1SAlexander Motin 			syncr = conf->lg_val;
111918ee8840SPawel Jakub Dawidek 	}
112083d165c1SAlexander Motin 	if (state == NULL && syncr == NULL)
1121ba6821f0SPawel Jakub Dawidek 		snprintf(buf, sizeof(buf), "%s", pp->lg_name);
112283d165c1SAlexander Motin 	else if (state != NULL && syncr != NULL) {
112383d165c1SAlexander Motin 		snprintf(buf, sizeof(buf), "%s (%s, %s)", pp->lg_name,
112483d165c1SAlexander Motin 		    state, syncr);
112583d165c1SAlexander Motin 	} else {
1126ba6821f0SPawel Jakub Dawidek 		snprintf(buf, sizeof(buf), "%s (%s)", pp->lg_name,
112783d165c1SAlexander Motin 		    state ? state : syncr);
1128ba6821f0SPawel Jakub Dawidek 	}
1129ba6821f0SPawel Jakub Dawidek 	return (buf);
113018ee8840SPawel Jakub Dawidek }
113118ee8840SPawel Jakub Dawidek 
113218ee8840SPawel Jakub Dawidek static void
1133ba6821f0SPawel Jakub Dawidek status_one_geom(struct ggeom *gp, int script, int name_len, int status_len)
113418ee8840SPawel Jakub Dawidek {
113518ee8840SPawel Jakub Dawidek 	struct gconsumer *cp;
113618ee8840SPawel Jakub Dawidek 	struct gconfig *conf;
1137ba6821f0SPawel Jakub Dawidek 	const char *name, *status, *component;
1138ba6821f0SPawel Jakub Dawidek 	int gotone;
113918ee8840SPawel Jakub Dawidek 
114018ee8840SPawel Jakub Dawidek 	name = gp->lg_name;
114183d165c1SAlexander Motin 	status = "N/A";
114218ee8840SPawel Jakub Dawidek 	LIST_FOREACH(conf, &gp->lg_config, lg_config) {
114383d165c1SAlexander Motin 		if (strcasecmp(conf->lg_name, "state") == 0) {
114483d165c1SAlexander Motin 			status = conf->lg_val;
114518ee8840SPawel Jakub Dawidek 			break;
114618ee8840SPawel Jakub Dawidek 		}
114783d165c1SAlexander Motin 	}
1148ba6821f0SPawel Jakub Dawidek 	gotone = 0;
114918ee8840SPawel Jakub Dawidek 	LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
1150ba6821f0SPawel Jakub Dawidek 		component = status_one_consumer(cp);
1151ba6821f0SPawel Jakub Dawidek 		if (component == NULL)
1152ba6821f0SPawel Jakub Dawidek 			continue;
1153ba6821f0SPawel Jakub Dawidek 		gotone = 1;
1154ba6821f0SPawel Jakub Dawidek 		printf("%*s  %*s  %s\n", name_len, name, status_len, status,
1155ba6821f0SPawel Jakub Dawidek 		    component);
1156ba6821f0SPawel Jakub Dawidek 		if (!script)
1157ba6821f0SPawel Jakub Dawidek 			name = status = "";
115818ee8840SPawel Jakub Dawidek 	}
1159ba6821f0SPawel Jakub Dawidek 	if (!gotone) {
1160ba6821f0SPawel Jakub Dawidek 		printf("%*s  %*s  %s\n", name_len, name, status_len, status,
1161ba6821f0SPawel Jakub Dawidek 		    "N/A");
1162ba6821f0SPawel Jakub Dawidek 	}
116318ee8840SPawel Jakub Dawidek }
116418ee8840SPawel Jakub Dawidek 
116518ee8840SPawel Jakub Dawidek static void
116683d165c1SAlexander Motin status_one_geom_prs(struct ggeom *gp, int script, int name_len, int status_len)
116783d165c1SAlexander Motin {
116883d165c1SAlexander Motin 	struct gprovider *pp;
116983d165c1SAlexander Motin 	struct gconsumer *cp;
117083d165c1SAlexander Motin 	struct gconfig *conf;
117183d165c1SAlexander Motin 	const char *name, *status, *component;
117283d165c1SAlexander Motin 	int gotone;
117383d165c1SAlexander Motin 
117483d165c1SAlexander Motin 	LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
117583d165c1SAlexander Motin 		name = pp->lg_name;
117683d165c1SAlexander Motin 		status = "N/A";
117783d165c1SAlexander Motin 		LIST_FOREACH(conf, &gp->lg_config, lg_config) {
117883d165c1SAlexander Motin 			if (strcasecmp(conf->lg_name, "state") == 0) {
117983d165c1SAlexander Motin 				status = conf->lg_val;
118083d165c1SAlexander Motin 				break;
118183d165c1SAlexander Motin 			}
118283d165c1SAlexander Motin 		}
118383d165c1SAlexander Motin 		LIST_FOREACH(conf, &pp->lg_config, lg_config) {
118483d165c1SAlexander Motin 			if (strcasecmp(conf->lg_name, "state") == 0) {
118583d165c1SAlexander Motin 				status = conf->lg_val;
118683d165c1SAlexander Motin 				break;
118783d165c1SAlexander Motin 			}
118883d165c1SAlexander Motin 		}
118983d165c1SAlexander Motin 		gotone = 0;
119083d165c1SAlexander Motin 		LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
119183d165c1SAlexander Motin 			component = status_one_consumer(cp);
119283d165c1SAlexander Motin 			if (component == NULL)
119383d165c1SAlexander Motin 				continue;
119483d165c1SAlexander Motin 			gotone = 1;
119583d165c1SAlexander Motin 			printf("%*s  %*s  %s\n", name_len, name,
119683d165c1SAlexander Motin 			    status_len, status, component);
119783d165c1SAlexander Motin 			if (!script)
119883d165c1SAlexander Motin 				name = status = "";
119983d165c1SAlexander Motin 		}
120083d165c1SAlexander Motin 		if (!gotone) {
120183d165c1SAlexander Motin 			printf("%*s  %*s  %s\n", name_len, name,
120283d165c1SAlexander Motin 			    status_len, status, "N/A");
120383d165c1SAlexander Motin 		}
120483d165c1SAlexander Motin 	}
120583d165c1SAlexander Motin }
120683d165c1SAlexander Motin 
120783d165c1SAlexander Motin static void
120818ee8840SPawel Jakub Dawidek std_status(struct gctl_req *req, unsigned flags __unused)
120918ee8840SPawel Jakub Dawidek {
121018ee8840SPawel Jakub Dawidek 	struct gmesh mesh;
121118ee8840SPawel Jakub Dawidek 	struct gclass *classp;
121218ee8840SPawel Jakub Dawidek 	struct ggeom *gp;
1213f13942a7SPawel Jakub Dawidek 	const char *name;
1214da80913dSPawel Jakub Dawidek 	int name_len, status_len;
121583d165c1SAlexander Motin 	int all, error, geoms, i, n, nargs, script;
121618ee8840SPawel Jakub Dawidek 
121718ee8840SPawel Jakub Dawidek 	error = geom_gettree(&mesh);
12186fc60008SPawel Jakub Dawidek 	if (error != 0)
12196fc60008SPawel Jakub Dawidek 		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
122018ee8840SPawel Jakub Dawidek 	classp = find_class(&mesh, gclass_name);
12215d47cd0fSPawel Jakub Dawidek 	if (classp == NULL)
12225d47cd0fSPawel Jakub Dawidek 		errx(EXIT_FAILURE, "Class %s not found.", gclass_name);
1223f13942a7SPawel Jakub Dawidek 	nargs = gctl_get_int(req, "nargs");
122483d165c1SAlexander Motin 	all = gctl_get_int(req, "all");
122583d165c1SAlexander Motin 	geoms = gctl_get_int(req, "geoms");
1226f13942a7SPawel Jakub Dawidek 	script = gctl_get_int(req, "script");
122783d165c1SAlexander Motin 	if (script) {
122883d165c1SAlexander Motin 		name_len = 0;
122983d165c1SAlexander Motin 		status_len = 0;
123083d165c1SAlexander Motin 	} else {
123118ee8840SPawel Jakub Dawidek 		name_len = strlen("Name");
123218ee8840SPawel Jakub Dawidek 		status_len = strlen("Status");
123383d165c1SAlexander Motin 	}
1234f13942a7SPawel Jakub Dawidek 	if (nargs > 0) {
1235f13942a7SPawel Jakub Dawidek 		for (i = 0, n = 0; i < nargs; i++) {
1236f13942a7SPawel Jakub Dawidek 			name = gctl_get_ascii(req, "arg%d", i);
123718ee8840SPawel Jakub Dawidek 			gp = find_geom(classp, name);
123818ee8840SPawel Jakub Dawidek 			if (gp == NULL)
12395d47cd0fSPawel Jakub Dawidek 				errx(EXIT_FAILURE, "No such geom: %s.", name);
124083d165c1SAlexander Motin 			if (geoms) {
124183d165c1SAlexander Motin 				status_update_len(gp,
124283d165c1SAlexander Motin 				    &name_len, &status_len);
124383d165c1SAlexander Motin 			} else {
124483d165c1SAlexander Motin 				status_update_len_prs(gp,
124583d165c1SAlexander Motin 				    &name_len, &status_len);
124618ee8840SPawel Jakub Dawidek 			}
124783d165c1SAlexander Motin 			n++;
124818ee8840SPawel Jakub Dawidek 		}
124918ee8840SPawel Jakub Dawidek 		if (n == 0)
125018ee8840SPawel Jakub Dawidek 			goto end;
125118ee8840SPawel Jakub Dawidek 	} else {
1252f13942a7SPawel Jakub Dawidek 		n = 0;
125318ee8840SPawel Jakub Dawidek 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
125483d165c1SAlexander Motin 			if (LIST_EMPTY(&gp->lg_provider) && !all)
125518ee8840SPawel Jakub Dawidek 				continue;
125683d165c1SAlexander Motin 			if (geoms) {
125783d165c1SAlexander Motin 				status_update_len(gp,
125883d165c1SAlexander Motin 				    &name_len, &status_len);
125983d165c1SAlexander Motin 			} else {
126083d165c1SAlexander Motin 				status_update_len_prs(gp,
126183d165c1SAlexander Motin 				    &name_len, &status_len);
126283d165c1SAlexander Motin 			}
126318ee8840SPawel Jakub Dawidek 			n++;
126418ee8840SPawel Jakub Dawidek 		}
126518ee8840SPawel Jakub Dawidek 		if (n == 0)
126618ee8840SPawel Jakub Dawidek 			goto end;
126718ee8840SPawel Jakub Dawidek 	}
1268f13942a7SPawel Jakub Dawidek 	if (!script) {
1269da80913dSPawel Jakub Dawidek 		printf("%*s  %*s  %s\n", name_len, "Name", status_len, "Status",
127018ee8840SPawel Jakub Dawidek 		    "Components");
1271ba6821f0SPawel Jakub Dawidek 	}
1272f13942a7SPawel Jakub Dawidek 	if (nargs > 0) {
1273f13942a7SPawel Jakub Dawidek 		for (i = 0; i < nargs; i++) {
1274f13942a7SPawel Jakub Dawidek 			name = gctl_get_ascii(req, "arg%d", i);
127518ee8840SPawel Jakub Dawidek 			gp = find_geom(classp, name);
127683d165c1SAlexander Motin 			if (gp == NULL)
127783d165c1SAlexander Motin 				continue;
127883d165c1SAlexander Motin 			if (geoms) {
1279f13942a7SPawel Jakub Dawidek 				status_one_geom(gp, script, name_len,
1280ba6821f0SPawel Jakub Dawidek 				    status_len);
128183d165c1SAlexander Motin 			} else {
128283d165c1SAlexander Motin 				status_one_geom_prs(gp, script, name_len,
128383d165c1SAlexander Motin 				    status_len);
128418ee8840SPawel Jakub Dawidek 			}
128518ee8840SPawel Jakub Dawidek 		}
128618ee8840SPawel Jakub Dawidek 	} else {
128718ee8840SPawel Jakub Dawidek 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
128883d165c1SAlexander Motin 			if (LIST_EMPTY(&gp->lg_provider) && !all)
128918ee8840SPawel Jakub Dawidek 				continue;
129083d165c1SAlexander Motin 			if (geoms) {
129183d165c1SAlexander Motin 				status_one_geom(gp, script, name_len,
129283d165c1SAlexander Motin 				    status_len);
129383d165c1SAlexander Motin 			} else {
129483d165c1SAlexander Motin 				status_one_geom_prs(gp, script, name_len,
129583d165c1SAlexander Motin 				    status_len);
129683d165c1SAlexander Motin 			}
129718ee8840SPawel Jakub Dawidek 		}
129818ee8840SPawel Jakub Dawidek 	}
129918ee8840SPawel Jakub Dawidek end:
130018ee8840SPawel Jakub Dawidek 	geom_deletetree(&mesh);
130118ee8840SPawel Jakub Dawidek }
130218ee8840SPawel Jakub Dawidek 
130318ee8840SPawel Jakub Dawidek static int
130405c91076SPawel Jakub Dawidek std_load_available(void)
130505c91076SPawel Jakub Dawidek {
130605c91076SPawel Jakub Dawidek 	char name[MAXPATHLEN], paths[MAXPATHLEN * 8], *p;
130705c91076SPawel Jakub Dawidek 	struct stat sb;
130805c91076SPawel Jakub Dawidek 	size_t len;
130905c91076SPawel Jakub Dawidek 
131005c91076SPawel Jakub Dawidek 	snprintf(name, sizeof(name), "g_%s", class_name);
131105c91076SPawel Jakub Dawidek 	/*
131205c91076SPawel Jakub Dawidek 	 * If already in kernel, "load" command is not available.
131305c91076SPawel Jakub Dawidek 	 */
131405c91076SPawel Jakub Dawidek 	if (modfind(name) >= 0)
131505c91076SPawel Jakub Dawidek 		return (0);
131605c91076SPawel Jakub Dawidek 	bzero(paths, sizeof(paths));
131705c91076SPawel Jakub Dawidek 	len = sizeof(paths);
131805c91076SPawel Jakub Dawidek 	if (sysctlbyname("kern.module_path", paths, &len, NULL, 0) < 0)
131905c91076SPawel Jakub Dawidek 		err(EXIT_FAILURE, "sysctl(kern.module_path)");
132005c91076SPawel Jakub Dawidek 	for (p = strtok(paths, ";"); p != NULL; p = strtok(NULL, ";")) {
132105c91076SPawel Jakub Dawidek 		snprintf(name, sizeof(name), "%s/geom_%s.ko", p, class_name);
132205c91076SPawel Jakub Dawidek 		/*
132305c91076SPawel Jakub Dawidek 		 * If geom_<name>.ko file exists, "load" command is available.
132405c91076SPawel Jakub Dawidek 		 */
132505c91076SPawel Jakub Dawidek 		if (stat(name, &sb) == 0)
132605c91076SPawel Jakub Dawidek 			return (1);
132705c91076SPawel Jakub Dawidek 	}
132805c91076SPawel Jakub Dawidek 	return (0);
132905c91076SPawel Jakub Dawidek }
133005c91076SPawel Jakub Dawidek 
133105c91076SPawel Jakub Dawidek static void
133205c91076SPawel Jakub Dawidek std_load(struct gctl_req *req __unused, unsigned flags)
133305c91076SPawel Jakub Dawidek {
133405c91076SPawel Jakub Dawidek 
133505c91076SPawel Jakub Dawidek 	/*
133605c91076SPawel Jakub Dawidek 	 * Do nothing special here, because of G_FLAG_LOADKLD flag,
133705c91076SPawel Jakub Dawidek 	 * module is already loaded.
133805c91076SPawel Jakub Dawidek 	 */
133905c91076SPawel Jakub Dawidek 	if ((flags & G_FLAG_VERBOSE) != 0)
134005c91076SPawel Jakub Dawidek 		printf("Module available.\n");
134105c91076SPawel Jakub Dawidek }
134205c91076SPawel Jakub Dawidek 
134305c91076SPawel Jakub Dawidek static int
134405c91076SPawel Jakub Dawidek std_unload_available(void)
134505c91076SPawel Jakub Dawidek {
134605c91076SPawel Jakub Dawidek 	char name[64];
134705c91076SPawel Jakub Dawidek 	int id;
134805c91076SPawel Jakub Dawidek 
134905c91076SPawel Jakub Dawidek 	snprintf(name, sizeof(name), "geom_%s", class_name);
135005c91076SPawel Jakub Dawidek 	id = kldfind(name);
135105c91076SPawel Jakub Dawidek 	if (id >= 0)
135205c91076SPawel Jakub Dawidek 		return (1);
135305c91076SPawel Jakub Dawidek 	return (0);
135405c91076SPawel Jakub Dawidek }
135505c91076SPawel Jakub Dawidek 
135605c91076SPawel Jakub Dawidek static void
135705c91076SPawel Jakub Dawidek std_unload(struct gctl_req *req, unsigned flags __unused)
135805c91076SPawel Jakub Dawidek {
135905c91076SPawel Jakub Dawidek 	char name[64];
136005c91076SPawel Jakub Dawidek 	int id;
136105c91076SPawel Jakub Dawidek 
136205c91076SPawel Jakub Dawidek 	snprintf(name, sizeof(name), "geom_%s", class_name);
136305c91076SPawel Jakub Dawidek 	id = kldfind(name);
136405c91076SPawel Jakub Dawidek 	if (id < 0) {
136505c91076SPawel Jakub Dawidek 		gctl_error(req, "Could not find module: %s.", strerror(errno));
136605c91076SPawel Jakub Dawidek 		return;
136705c91076SPawel Jakub Dawidek 	}
136805c91076SPawel Jakub Dawidek 	if (kldunload(id) < 0) {
136905c91076SPawel Jakub Dawidek 		gctl_error(req, "Could not unload module: %s.",
137005c91076SPawel Jakub Dawidek 		    strerror(errno));
137105c91076SPawel Jakub Dawidek 		return;
137205c91076SPawel Jakub Dawidek 	}
137305c91076SPawel Jakub Dawidek }
137405c91076SPawel Jakub Dawidek 
137505c91076SPawel Jakub Dawidek static int
137605c91076SPawel Jakub Dawidek std_available(const char *name)
137705c91076SPawel Jakub Dawidek {
137805c91076SPawel Jakub Dawidek 
1379b70eccf3SPawel Jakub Dawidek 	if (strcmp(name, "help") == 0)
1380b70eccf3SPawel Jakub Dawidek 		return (1);
1381b70eccf3SPawel Jakub Dawidek 	else if (strcmp(name, "list") == 0)
138205c91076SPawel Jakub Dawidek 		return (std_list_available());
138318ee8840SPawel Jakub Dawidek 	else if (strcmp(name, "status") == 0)
138418ee8840SPawel Jakub Dawidek 		return (std_status_available());
138505c91076SPawel Jakub Dawidek 	else if (strcmp(name, "load") == 0)
138605c91076SPawel Jakub Dawidek 		return (std_load_available());
138705c91076SPawel Jakub Dawidek 	else if (strcmp(name, "unload") == 0)
138805c91076SPawel Jakub Dawidek 		return (std_unload_available());
138905c91076SPawel Jakub Dawidek 	else
139005c91076SPawel Jakub Dawidek 		assert(!"Unknown standard command.");
139105c91076SPawel Jakub Dawidek 	return (0);
139205c91076SPawel Jakub Dawidek }
1393