xref: /freebsd/sbin/geom/core/geom.c (revision 2117cdd4b4ebda7db0270c56f5a3cbdd37ec8cfc)
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>
40f200cc25SRobert 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;
31768bff4a0SKirk McKusick 	int ch, vcount;
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 	 */
33968bff4a0SKirk McKusick 	vcount = 0;
34005c91076SPawel Jakub Dawidek 	while ((ch = getopt(*argc, *argv, opts)) != -1) {
34105c91076SPawel Jakub Dawidek 		/* Standard (not passed to kernel) options. */
34268bff4a0SKirk McKusick 		if (ch == 'v' && (cmd->gc_flags & G_FLAG_VERBOSE) != 0)
34305c91076SPawel Jakub Dawidek 			verbose = 1;
34405c91076SPawel Jakub Dawidek 		/* Options passed to kernel. */
34505c91076SPawel Jakub Dawidek 		opt = find_option(cmd, ch);
34668bff4a0SKirk McKusick 		if (opt == NULL) {
34768bff4a0SKirk McKusick 			if (ch == 'v' && (cmd->gc_flags & G_FLAG_VERBOSE) != 0){
34868bff4a0SKirk McKusick 				if (++vcount < 2)
34968bff4a0SKirk McKusick 					continue;
35068bff4a0SKirk McKusick 				else
35168bff4a0SKirk McKusick 					warnx("Option 'v' specified twice.");
35268bff4a0SKirk McKusick 			}
353c979e206SPawel Jakub Dawidek 			usage();
35468bff4a0SKirk McKusick 		}
355315fcbf7SPawel Jakub Dawidek 		if (!G_OPT_ISMULTI(opt) && G_OPT_ISDONE(opt)) {
3566fc60008SPawel Jakub Dawidek 			warnx("Option '%c' specified twice.", opt->go_char);
357c979e206SPawel Jakub Dawidek 			usage();
35805c91076SPawel Jakub Dawidek 		}
35905c91076SPawel Jakub Dawidek 		G_OPT_DONE(opt);
36005c91076SPawel Jakub Dawidek 
3616fc60008SPawel Jakub Dawidek 		if (G_OPT_TYPE(opt) == G_TYPE_BOOL)
36205c91076SPawel Jakub Dawidek 			set_option(req, opt, "1");
36305c91076SPawel Jakub Dawidek 		else
36405c91076SPawel Jakub Dawidek 			set_option(req, opt, optarg);
36505c91076SPawel Jakub Dawidek 	}
36605c91076SPawel Jakub Dawidek 	*argc -= optind;
36705c91076SPawel Jakub Dawidek 	*argv += optind;
36805c91076SPawel Jakub Dawidek 
36905c91076SPawel Jakub Dawidek 	/*
37005c91076SPawel Jakub Dawidek 	 * Add not specified arguments, but with default values.
37105c91076SPawel Jakub Dawidek 	 */
37205c91076SPawel Jakub Dawidek 	for (i = 0; ; i++) {
37305c91076SPawel Jakub Dawidek 		opt = &cmd->gc_options[i];
37405c91076SPawel Jakub Dawidek 		if (opt->go_name == NULL)
37505c91076SPawel Jakub Dawidek 			break;
37605c91076SPawel Jakub Dawidek 		if (G_OPT_ISDONE(opt))
37705c91076SPawel Jakub Dawidek 			continue;
37805c91076SPawel Jakub Dawidek 
3796fc60008SPawel Jakub Dawidek 		if (G_OPT_TYPE(opt) == G_TYPE_BOOL) {
38005c91076SPawel Jakub Dawidek 			assert(opt->go_val == NULL);
38105c91076SPawel Jakub Dawidek 			set_option(req, opt, "0");
38205c91076SPawel Jakub Dawidek 		} else {
38305c91076SPawel Jakub Dawidek 			if (opt->go_val == NULL) {
3846fc60008SPawel Jakub Dawidek 				warnx("Option '%c' not specified.",
38505c91076SPawel Jakub Dawidek 				    opt->go_char);
386c979e206SPawel Jakub Dawidek 				usage();
3877648b1e9SPawel Jakub Dawidek 			} else if (opt->go_val == G_VAL_OPTIONAL) {
3887648b1e9SPawel Jakub Dawidek 				/* add nothing. */
38905c91076SPawel Jakub Dawidek 			} else {
390946e2f35SPawel Jakub Dawidek 				set_option(req, opt, opt->go_val);
39105c91076SPawel Jakub Dawidek 			}
39205c91076SPawel Jakub Dawidek 		}
39305c91076SPawel Jakub Dawidek 	}
3943cf55d3aSMarcel Moolenaar 
39505c91076SPawel Jakub Dawidek 	/*
39605c91076SPawel Jakub Dawidek 	 * Add rest of given arguments.
39705c91076SPawel Jakub Dawidek 	 */
39805c91076SPawel Jakub Dawidek 	gctl_ro_param(req, "nargs", sizeof(int), argc);
39905c91076SPawel Jakub Dawidek 	for (i = 0; i < (unsigned)*argc; i++) {
40005c91076SPawel Jakub Dawidek 		char argname[16];
40105c91076SPawel Jakub Dawidek 
40205c91076SPawel Jakub Dawidek 		snprintf(argname, sizeof(argname), "arg%u", i);
40305c91076SPawel Jakub Dawidek 		gctl_ro_param(req, argname, -1, (*argv)[i]);
40405c91076SPawel Jakub Dawidek 	}
40505c91076SPawel Jakub Dawidek }
40605c91076SPawel Jakub Dawidek 
40705c91076SPawel Jakub Dawidek /*
40805c91076SPawel Jakub Dawidek  * Find given command in commands available for given class.
40905c91076SPawel Jakub Dawidek  */
41005c91076SPawel Jakub Dawidek static struct g_command *
411138cedfdSPawel Jakub Dawidek find_command(const char *cmdstr, int flags)
41205c91076SPawel Jakub Dawidek {
41305c91076SPawel Jakub Dawidek 	struct g_command *cmd;
41405c91076SPawel Jakub Dawidek 	unsigned i;
41505c91076SPawel Jakub Dawidek 
41605c91076SPawel Jakub Dawidek 	/*
41705c91076SPawel Jakub Dawidek 	 * First try to find command defined by loaded library.
41805c91076SPawel Jakub Dawidek 	 */
419138cedfdSPawel Jakub Dawidek 	if ((flags & GEOM_CLASS_CMDS) != 0 && class_commands != NULL) {
42005c91076SPawel Jakub Dawidek 		for (i = 0; ; i++) {
42105c91076SPawel Jakub Dawidek 			cmd = &class_commands[i];
42205c91076SPawel Jakub Dawidek 			if (cmd->gc_name == NULL)
42305c91076SPawel Jakub Dawidek 				break;
42405c91076SPawel Jakub Dawidek 			if (strcmp(cmd->gc_name, cmdstr) == 0)
42505c91076SPawel Jakub Dawidek 				return (cmd);
42605c91076SPawel Jakub Dawidek 		}
42705c91076SPawel Jakub Dawidek 	}
42805c91076SPawel Jakub Dawidek 	/*
42905c91076SPawel Jakub Dawidek 	 * Now try to find in standard commands.
43005c91076SPawel Jakub Dawidek 	 */
431138cedfdSPawel Jakub Dawidek 	if ((flags & GEOM_STD_CMDS) != 0) {
43205c91076SPawel Jakub Dawidek 		for (i = 0; ; i++) {
43305c91076SPawel Jakub Dawidek 			cmd = &std_commands[i];
43405c91076SPawel Jakub Dawidek 			if (cmd->gc_name == NULL)
43505c91076SPawel Jakub Dawidek 				break;
43605c91076SPawel Jakub Dawidek 			if (strcmp(cmd->gc_name, cmdstr) == 0)
43705c91076SPawel Jakub Dawidek 				return (cmd);
43805c91076SPawel Jakub Dawidek 		}
439138cedfdSPawel Jakub Dawidek 	}
44005c91076SPawel Jakub Dawidek 	return (NULL);
44105c91076SPawel Jakub Dawidek }
44205c91076SPawel Jakub Dawidek 
44305c91076SPawel Jakub Dawidek static unsigned
44405c91076SPawel Jakub Dawidek set_flags(struct g_command *cmd)
44505c91076SPawel Jakub Dawidek {
44605c91076SPawel Jakub Dawidek 	unsigned flags = 0;
44705c91076SPawel Jakub Dawidek 
448e38717c1SKirk McKusick 	if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0 && verbose)
44905c91076SPawel Jakub Dawidek 		flags |= G_FLAG_VERBOSE;
45005c91076SPawel Jakub Dawidek 
45105c91076SPawel Jakub Dawidek 	return (flags);
45205c91076SPawel Jakub Dawidek }
45305c91076SPawel Jakub Dawidek 
45405c91076SPawel Jakub Dawidek /*
45505c91076SPawel Jakub Dawidek  * Run command.
45605c91076SPawel Jakub Dawidek  */
45705c91076SPawel Jakub Dawidek static void
45805c91076SPawel Jakub Dawidek run_command(int argc, char *argv[])
45905c91076SPawel Jakub Dawidek {
46005c91076SPawel Jakub Dawidek 	struct g_command *cmd;
46105c91076SPawel Jakub Dawidek 	struct gctl_req *req;
46205c91076SPawel Jakub Dawidek 	const char *errstr;
46305c91076SPawel Jakub Dawidek 	char buf[4096];
46405c91076SPawel Jakub Dawidek 
465138cedfdSPawel Jakub Dawidek 	/* First try to find a command defined by a class. */
466138cedfdSPawel Jakub Dawidek 	cmd = find_command(argv[0], GEOM_CLASS_CMDS);
467138cedfdSPawel Jakub Dawidek 	if (cmd == NULL) {
468138cedfdSPawel Jakub Dawidek 		/* Now, try to find a standard command. */
469138cedfdSPawel Jakub Dawidek 		cmd = find_command(argv[0], GEOM_STD_CMDS);
47005c91076SPawel Jakub Dawidek 		if (cmd == NULL) {
4716fc60008SPawel Jakub Dawidek 			warnx("Unknown command: %s.", argv[0]);
472c979e206SPawel Jakub Dawidek 			usage();
47305c91076SPawel Jakub Dawidek 		}
474138cedfdSPawel Jakub Dawidek 		if (!std_available(cmd->gc_name)) {
475112adef8SEdward Tomasz Napierala 			warnx("Command '%s' not available; "
476112adef8SEdward Tomasz Napierala 			    "try 'load' first.", argv[0]);
477138cedfdSPawel Jakub Dawidek 			exit(EXIT_FAILURE);
478138cedfdSPawel Jakub Dawidek 		}
479138cedfdSPawel Jakub Dawidek 	}
48005c91076SPawel Jakub Dawidek 	if ((cmd->gc_flags & G_FLAG_LOADKLD) != 0)
48105c91076SPawel Jakub Dawidek 		load_module();
48205c91076SPawel Jakub Dawidek 
48305c91076SPawel Jakub Dawidek 	req = gctl_get_handle();
48405c91076SPawel Jakub Dawidek 	gctl_ro_param(req, "class", -1, gclass_name);
48505c91076SPawel Jakub Dawidek 	gctl_ro_param(req, "verb", -1, argv[0]);
48605c91076SPawel Jakub Dawidek 	if (version != NULL)
48705c91076SPawel Jakub Dawidek 		gctl_ro_param(req, "version", sizeof(*version), version);
48805c91076SPawel Jakub Dawidek 	parse_arguments(cmd, req, &argc, &argv);
48905c91076SPawel Jakub Dawidek 
490*2117cdd4SAlexander Motin 	buf[0] = '\0';
49105c91076SPawel Jakub Dawidek 	if (cmd->gc_func != NULL) {
49205c91076SPawel Jakub Dawidek 		unsigned flags;
49305c91076SPawel Jakub Dawidek 
49405c91076SPawel Jakub Dawidek 		flags = set_flags(cmd);
49505c91076SPawel Jakub Dawidek 		cmd->gc_func(req, flags);
49605c91076SPawel Jakub Dawidek 		errstr = req->error;
49705c91076SPawel Jakub Dawidek 	} else {
498*2117cdd4SAlexander Motin 		gctl_add_param(req, "output", sizeof(buf), buf,
499*2117cdd4SAlexander Motin 		    GCTL_PARAM_WR | GCTL_PARAM_ASCII);
50005c91076SPawel Jakub Dawidek 		errstr = gctl_issue(req);
50105c91076SPawel Jakub Dawidek 	}
50253767efdSPawel Jakub Dawidek 	if (errstr != NULL && errstr[0] != '\0') {
5036fc60008SPawel Jakub Dawidek 		warnx("%s", errstr);
504f792f1d8SPawel Jakub Dawidek 		if (strncmp(errstr, "warning: ", strlen("warning: ")) != 0) {
50505c91076SPawel Jakub Dawidek 			gctl_free(req);
50605c91076SPawel Jakub Dawidek 			exit(EXIT_FAILURE);
50705c91076SPawel Jakub Dawidek 		}
508f792f1d8SPawel Jakub Dawidek 	}
5091bcfab7fSPawel Jakub Dawidek 	if (buf[0] != '\0')
51005c91076SPawel Jakub Dawidek 		printf("%s", buf);
51105c91076SPawel Jakub Dawidek 	gctl_free(req);
51205c91076SPawel Jakub Dawidek 	if (verbose)
51305c91076SPawel Jakub Dawidek 		printf("Done.\n");
51405c91076SPawel Jakub Dawidek 	exit(EXIT_SUCCESS);
51505c91076SPawel Jakub Dawidek }
51605c91076SPawel Jakub Dawidek 
51714bf405bSMarcel Moolenaar #ifndef STATIC_GEOM_CLASSES
5183bad06e9SPawel Jakub Dawidek static const char *
5193bad06e9SPawel Jakub Dawidek library_path(void)
5203bad06e9SPawel Jakub Dawidek {
5213bad06e9SPawel Jakub Dawidek 	const char *path;
5223bad06e9SPawel Jakub Dawidek 
5233bad06e9SPawel Jakub Dawidek 	path = getenv("GEOM_LIBRARY_PATH");
5243bad06e9SPawel Jakub Dawidek 	if (path == NULL)
5258494b738SDavid E. O'Brien 		path = GEOM_CLASS_DIR;
5263bad06e9SPawel Jakub Dawidek 	return (path);
5273bad06e9SPawel Jakub Dawidek }
5283bad06e9SPawel Jakub Dawidek 
52905c91076SPawel Jakub Dawidek static void
53005c91076SPawel Jakub Dawidek load_library(void)
53105c91076SPawel Jakub Dawidek {
5325d824386SUlf Lilleengen 	char *curpath, path[MAXPATHLEN], *tofree, *totalpath;
53305c91076SPawel Jakub Dawidek 	uint32_t *lib_version;
53405c91076SPawel Jakub Dawidek 	void *dlh;
5352591e96cSUlf Lilleengen 	int ret;
53605c91076SPawel Jakub Dawidek 
5372591e96cSUlf Lilleengen 	ret = 0;
5385d824386SUlf Lilleengen 	tofree = totalpath = strdup(library_path());
5392591e96cSUlf Lilleengen 	if (totalpath == NULL)
5402591e96cSUlf Lilleengen 		err(EXIT_FAILURE, "Not enough memory for library path");
5412591e96cSUlf Lilleengen 
5422591e96cSUlf Lilleengen 	if (strchr(totalpath, ':') != NULL)
5432591e96cSUlf Lilleengen 		curpath = strsep(&totalpath, ":");
5442591e96cSUlf Lilleengen 	else
5452591e96cSUlf Lilleengen 		curpath = totalpath;
5462591e96cSUlf Lilleengen 	/* Traverse the paths to find one that contains the library we want. */
5472591e96cSUlf Lilleengen 	while (curpath != NULL) {
5482591e96cSUlf Lilleengen 		snprintf(path, sizeof(path), "%s/geom_%s.so", curpath,
5493bad06e9SPawel Jakub Dawidek 		    class_name);
5502591e96cSUlf Lilleengen 		ret = access(path, F_OK);
5512591e96cSUlf Lilleengen 		if (ret == -1) {
552a73148d2SPawel Jakub Dawidek 			if (errno == ENOENT) {
55305c91076SPawel Jakub Dawidek 				/*
5542591e96cSUlf Lilleengen 				 * If we cannot find library, try the next
5552591e96cSUlf Lilleengen 				 * path.
55605c91076SPawel Jakub Dawidek 				 */
5572591e96cSUlf Lilleengen 				curpath = strsep(&totalpath, ":");
5582591e96cSUlf Lilleengen 				continue;
55905c91076SPawel Jakub Dawidek 			}
560a73148d2SPawel Jakub Dawidek 			err(EXIT_FAILURE, "Cannot access library");
561a73148d2SPawel Jakub Dawidek 		}
5622591e96cSUlf Lilleengen 		break;
5632591e96cSUlf Lilleengen 	}
5645d824386SUlf Lilleengen 	free(tofree);
5652591e96cSUlf Lilleengen 	/* No library was found, but standard commands can still be used */
5662591e96cSUlf Lilleengen 	if (ret == -1)
5672591e96cSUlf Lilleengen 		return;
568a73148d2SPawel Jakub Dawidek 	dlh = dlopen(path, RTLD_NOW);
569a73148d2SPawel Jakub Dawidek 	if (dlh == NULL)
570a73148d2SPawel Jakub Dawidek 		errx(EXIT_FAILURE, "Cannot open library: %s.", dlerror());
57105c91076SPawel Jakub Dawidek 	lib_version = dlsym(dlh, "lib_version");
57205c91076SPawel Jakub Dawidek 	if (lib_version == NULL) {
5736fc60008SPawel Jakub Dawidek 		warnx("Cannot find symbol %s: %s.", "lib_version", dlerror());
57405c91076SPawel Jakub Dawidek 		dlclose(dlh);
57505c91076SPawel Jakub Dawidek 		exit(EXIT_FAILURE);
57605c91076SPawel Jakub Dawidek 	}
57705c91076SPawel Jakub Dawidek 	if (*lib_version != G_LIB_VERSION) {
57805c91076SPawel Jakub Dawidek 		dlclose(dlh);
579f792f1d8SPawel Jakub Dawidek 		errx(EXIT_FAILURE, "%s and %s are not synchronized.",
580f792f1d8SPawel Jakub Dawidek 		    getprogname(), path);
58105c91076SPawel Jakub Dawidek 	}
58205c91076SPawel Jakub Dawidek 	version = dlsym(dlh, "version");
58305c91076SPawel Jakub Dawidek 	if (version == NULL) {
5846fc60008SPawel Jakub Dawidek 		warnx("Cannot find symbol %s: %s.", "version", dlerror());
58505c91076SPawel Jakub Dawidek 		dlclose(dlh);
58605c91076SPawel Jakub Dawidek 		exit(EXIT_FAILURE);
58705c91076SPawel Jakub Dawidek 	}
58805c91076SPawel Jakub Dawidek 	class_commands = dlsym(dlh, "class_commands");
58905c91076SPawel Jakub Dawidek 	if (class_commands == NULL) {
5906fc60008SPawel Jakub Dawidek 		warnx("Cannot find symbol %s: %s.", "class_commands",
5916fc60008SPawel Jakub Dawidek 		    dlerror());
59205c91076SPawel Jakub Dawidek 		dlclose(dlh);
59305c91076SPawel Jakub Dawidek 		exit(EXIT_FAILURE);
59405c91076SPawel Jakub Dawidek 	}
59505c91076SPawel Jakub Dawidek }
59614bf405bSMarcel Moolenaar #endif	/* !STATIC_GEOM_CLASSES */
59705c91076SPawel Jakub Dawidek 
59805c91076SPawel Jakub Dawidek /*
59905c91076SPawel Jakub Dawidek  * Class name should be all capital letters.
60005c91076SPawel Jakub Dawidek  */
60105c91076SPawel Jakub Dawidek static void
60205c91076SPawel Jakub Dawidek set_class_name(void)
60305c91076SPawel Jakub Dawidek {
60405c91076SPawel Jakub Dawidek 	char *s1, *s2;
60505c91076SPawel Jakub Dawidek 
606ee602fbbSPawel Jakub Dawidek 	s1 = class_name;
607ee602fbbSPawel Jakub Dawidek 	for (; *s1 != '\0'; s1++)
608ee602fbbSPawel Jakub Dawidek 		*s1 = tolower(*s1);
609cb94ab30SPawel Jakub Dawidek 	gclass_name = malloc(strlen(class_name) + 1);
61005c91076SPawel Jakub Dawidek 	if (gclass_name == NULL)
61105c91076SPawel Jakub Dawidek 		errx(EXIT_FAILURE, "No memory");
61205c91076SPawel Jakub Dawidek 	s1 = gclass_name;
61305c91076SPawel Jakub Dawidek 	s2 = class_name;
61405c91076SPawel Jakub Dawidek 	for (; *s2 != '\0'; s2++)
61505c91076SPawel Jakub Dawidek 		*s1++ = toupper(*s2);
61605c91076SPawel Jakub Dawidek 	*s1 = '\0';
61705c91076SPawel Jakub Dawidek }
61805c91076SPawel Jakub Dawidek 
61905c91076SPawel Jakub Dawidek static void
62005c91076SPawel Jakub Dawidek get_class(int *argc, char ***argv)
62105c91076SPawel Jakub Dawidek {
62205c91076SPawel Jakub Dawidek 
62305c91076SPawel Jakub Dawidek 	snprintf(comm, sizeof(comm), "%s", basename((*argv)[0]));
62405c91076SPawel Jakub Dawidek 	if (strcmp(comm, "geom") == 0) {
62505c91076SPawel Jakub Dawidek 		if (*argc < 2)
626c979e206SPawel Jakub Dawidek 			usage();
627687b0015SPawel Jakub Dawidek 		else if (*argc == 2) {
628687b0015SPawel Jakub Dawidek 			if (strcmp((*argv)[1], "-h") == 0 ||
629687b0015SPawel Jakub Dawidek 			    strcmp((*argv)[1], "help") == 0) {
630c979e206SPawel Jakub Dawidek 				usage();
631687b0015SPawel Jakub Dawidek 			}
632687b0015SPawel Jakub Dawidek 		}
63305c91076SPawel Jakub Dawidek 		strlcatf(comm, sizeof(comm), " %s", (*argv)[1]);
63405c91076SPawel Jakub Dawidek 		class_name = (*argv)[1];
63505c91076SPawel Jakub Dawidek 		*argc -= 2;
63605c91076SPawel Jakub Dawidek 		*argv += 2;
63705c91076SPawel Jakub Dawidek 	} else if (*comm == 'g') {
63805c91076SPawel Jakub Dawidek 		class_name = comm + 1;
63905c91076SPawel Jakub Dawidek 		*argc -= 1;
64005c91076SPawel Jakub Dawidek 		*argv += 1;
64105c91076SPawel Jakub Dawidek 	} else {
64205c91076SPawel Jakub Dawidek 		errx(EXIT_FAILURE, "Invalid utility name.");
64305c91076SPawel Jakub Dawidek 	}
644a16f9b36SMarcel Moolenaar 
64514bf405bSMarcel Moolenaar #ifndef STATIC_GEOM_CLASSES
64605c91076SPawel Jakub Dawidek 	load_library();
647a16f9b36SMarcel Moolenaar #else
648a16f9b36SMarcel Moolenaar 	if (!strcasecmp(class_name, "part")) {
649a16f9b36SMarcel Moolenaar 		version = &gpart_version;
650a16f9b36SMarcel Moolenaar 		class_commands = gpart_class_commands;
651bc69d66fSXin LI 	} else if (!strcasecmp(class_name, "label")) {
652bc69d66fSXin LI 		version = &glabel_version;
653bc69d66fSXin LI 		class_commands = glabel_class_commands;
654c6678483SAlexander Motin 	}
65514bf405bSMarcel Moolenaar #endif /* !STATIC_GEOM_CLASSES */
656a16f9b36SMarcel Moolenaar 
657a16f9b36SMarcel Moolenaar 	set_class_name();
658e2d52f14SMark Felder 
659e2d52f14SMark Felder 	/* If we can't load or list, it's not a class. */
660e2d52f14SMark Felder 	if (!std_available("load") && !std_available("list"))
661112adef8SEdward Tomasz Napierala 		errx(EXIT_FAILURE, "Invalid class name '%s'.", class_name);
662e2d52f14SMark Felder 
66305c91076SPawel Jakub Dawidek 	if (*argc < 1)
664c979e206SPawel Jakub Dawidek 		usage();
66505c91076SPawel Jakub Dawidek }
66605c91076SPawel Jakub Dawidek 
6670f73f701SEdward Tomasz Napierala static struct ggeom *
6680f73f701SEdward Tomasz Napierala find_geom_by_provider(struct gmesh *mesh, const char *name)
6690f73f701SEdward Tomasz Napierala {
6700f73f701SEdward Tomasz Napierala 	struct gclass *classp;
6710f73f701SEdward Tomasz Napierala 	struct ggeom *gp;
6720f73f701SEdward Tomasz Napierala 	struct gprovider *pp;
6730f73f701SEdward Tomasz Napierala 
6740f73f701SEdward Tomasz Napierala 	LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
6750f73f701SEdward Tomasz Napierala 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
6760f73f701SEdward Tomasz Napierala 			LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
6770f73f701SEdward Tomasz Napierala 				if (strcmp(pp->lg_name, name) == 0)
6780f73f701SEdward Tomasz Napierala 					return (gp);
6790f73f701SEdward Tomasz Napierala 			}
6800f73f701SEdward Tomasz Napierala 		}
6810f73f701SEdward Tomasz Napierala 	}
6820f73f701SEdward Tomasz Napierala 
6830f73f701SEdward Tomasz Napierala 	return (NULL);
6840f73f701SEdward Tomasz Napierala }
6850f73f701SEdward Tomasz Napierala 
6866a027382SEdward Tomasz Napierala static int
6876a027382SEdward Tomasz Napierala compute_tree_width_geom(struct gmesh *mesh, struct ggeom *gp, int indent)
6886a027382SEdward Tomasz Napierala {
6896a027382SEdward Tomasz Napierala 	struct gclass *classp2;
6906a027382SEdward Tomasz Napierala 	struct ggeom *gp2;
6916a027382SEdward Tomasz Napierala 	struct gconsumer *cp2;
6926a027382SEdward Tomasz Napierala 	struct gprovider *pp;
6936a027382SEdward Tomasz Napierala 	int max_width, width;
6946a027382SEdward Tomasz Napierala 
6956a027382SEdward Tomasz Napierala 	max_width = width = indent + strlen(gp->lg_name);
6966a027382SEdward Tomasz Napierala 
6976a027382SEdward Tomasz Napierala 	LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
6986a027382SEdward Tomasz Napierala 		LIST_FOREACH(classp2, &mesh->lg_class, lg_class) {
6996a027382SEdward Tomasz Napierala 			LIST_FOREACH(gp2, &classp2->lg_geom, lg_geom) {
7006a027382SEdward Tomasz Napierala 				LIST_FOREACH(cp2,
7016a027382SEdward Tomasz Napierala 				    &gp2->lg_consumer, lg_consumer) {
7026a027382SEdward Tomasz Napierala 					if (pp != cp2->lg_provider)
7036a027382SEdward Tomasz Napierala 						continue;
7046a027382SEdward Tomasz Napierala 					width = compute_tree_width_geom(mesh,
7056a027382SEdward Tomasz Napierala 					    gp2, indent + 2);
7066a027382SEdward Tomasz Napierala 					if (width > max_width)
7076a027382SEdward Tomasz Napierala 						max_width = width;
7086a027382SEdward Tomasz Napierala 				}
7096a027382SEdward Tomasz Napierala 			}
7106a027382SEdward Tomasz Napierala 		}
7116a027382SEdward Tomasz Napierala 	}
7126a027382SEdward Tomasz Napierala 
7136a027382SEdward Tomasz Napierala 	return (max_width);
7146a027382SEdward Tomasz Napierala }
7156a027382SEdward Tomasz Napierala 
7166a027382SEdward Tomasz Napierala static int
7176a027382SEdward Tomasz Napierala compute_tree_width(struct gmesh *mesh)
7186a027382SEdward Tomasz Napierala {
7196a027382SEdward Tomasz Napierala 	struct gclass *classp;
7206a027382SEdward Tomasz Napierala 	struct ggeom *gp;
7216a027382SEdward Tomasz Napierala 	int max_width, width;
7226a027382SEdward Tomasz Napierala 
7236a027382SEdward Tomasz Napierala 	max_width = width = 0;
7246a027382SEdward Tomasz Napierala 
7256a027382SEdward Tomasz Napierala 	LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
7266a027382SEdward Tomasz Napierala 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
7276a027382SEdward Tomasz Napierala 			if (!LIST_EMPTY(&gp->lg_consumer))
7286a027382SEdward Tomasz Napierala 				continue;
7296a027382SEdward Tomasz Napierala 			width = compute_tree_width_geom(mesh, gp, 0);
7306a027382SEdward Tomasz Napierala 			if (width > max_width)
7316a027382SEdward Tomasz Napierala 				max_width = width;
7326a027382SEdward Tomasz Napierala 		}
7336a027382SEdward Tomasz Napierala 	}
7346a027382SEdward Tomasz Napierala 
7356a027382SEdward Tomasz Napierala 	return (max_width);
7366a027382SEdward Tomasz Napierala }
7376a027382SEdward Tomasz Napierala 
7386a027382SEdward Tomasz Napierala static void
7396a027382SEdward Tomasz Napierala show_tree_geom(struct gmesh *mesh, struct ggeom *gp, int indent, int width)
7406a027382SEdward Tomasz Napierala {
7416a027382SEdward Tomasz Napierala 	struct gclass *classp2;
7426a027382SEdward Tomasz Napierala 	struct ggeom *gp2;
7436a027382SEdward Tomasz Napierala 	struct gconsumer *cp2;
7446a027382SEdward Tomasz Napierala 	struct gprovider *pp;
7456a027382SEdward Tomasz Napierala 
7466a027382SEdward Tomasz Napierala 	if (LIST_EMPTY(&gp->lg_provider)) {
7476a027382SEdward Tomasz Napierala 		printf("%*s%-*.*s %-*.*s\n", indent, "",
7486a027382SEdward Tomasz Napierala 		    width - indent, width - indent, gp->lg_name,
7496a027382SEdward Tomasz Napierala 		    GEOM_CLASS_WIDTH, GEOM_CLASS_WIDTH, gp->lg_class->lg_name);
7506a027382SEdward Tomasz Napierala 		return;
7516a027382SEdward Tomasz Napierala 	}
7526a027382SEdward Tomasz Napierala 
7536a027382SEdward Tomasz Napierala 	LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
7546a027382SEdward Tomasz Napierala 		printf("%*s%-*.*s %-*.*s %s\n", indent, "",
7556a027382SEdward Tomasz Napierala 		    width - indent, width - indent, gp->lg_name,
7566a027382SEdward Tomasz Napierala 		    GEOM_CLASS_WIDTH, GEOM_CLASS_WIDTH, gp->lg_class->lg_name,
7576a027382SEdward Tomasz Napierala 		    pp->lg_name);
7586a027382SEdward Tomasz Napierala 
7596a027382SEdward Tomasz Napierala 		LIST_FOREACH(classp2, &mesh->lg_class, lg_class) {
7606a027382SEdward Tomasz Napierala 			LIST_FOREACH(gp2, &classp2->lg_geom, lg_geom) {
7616a027382SEdward Tomasz Napierala 				LIST_FOREACH(cp2,
7626a027382SEdward Tomasz Napierala 				    &gp2->lg_consumer, lg_consumer) {
7636a027382SEdward Tomasz Napierala 					if (pp != cp2->lg_provider)
7646a027382SEdward Tomasz Napierala 						continue;
7656a027382SEdward Tomasz Napierala 					show_tree_geom(mesh, gp2,
7666a027382SEdward Tomasz Napierala 					    indent + 2, width);
7676a027382SEdward Tomasz Napierala 				}
7686a027382SEdward Tomasz Napierala 			}
7696a027382SEdward Tomasz Napierala 		}
7706a027382SEdward Tomasz Napierala 	}
7716a027382SEdward Tomasz Napierala }
7726a027382SEdward Tomasz Napierala 
7736a027382SEdward Tomasz Napierala static void
7746a027382SEdward Tomasz Napierala show_tree(void)
7756a027382SEdward Tomasz Napierala {
7766a027382SEdward Tomasz Napierala 	struct gmesh mesh;
7776a027382SEdward Tomasz Napierala 	struct gclass *classp;
7786a027382SEdward Tomasz Napierala 	struct ggeom *gp;
7796a027382SEdward Tomasz Napierala 	int error, width;
7806a027382SEdward Tomasz Napierala 
7816a027382SEdward Tomasz Napierala 	error = geom_gettree(&mesh);
7826a027382SEdward Tomasz Napierala 	if (error != 0)
7836a027382SEdward Tomasz Napierala 		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
7846a027382SEdward Tomasz Napierala 
7856a027382SEdward Tomasz Napierala 	width = compute_tree_width(&mesh);
7866a027382SEdward Tomasz Napierala 
7876a027382SEdward Tomasz Napierala 	printf("%-*.*s %-*.*s %s\n",
7886a027382SEdward Tomasz Napierala 	    width, width, "Geom",
7896a027382SEdward Tomasz Napierala 	    GEOM_CLASS_WIDTH, GEOM_CLASS_WIDTH, "Class",
7906a027382SEdward Tomasz Napierala 	    "Provider");
7916a027382SEdward Tomasz Napierala 
7926a027382SEdward Tomasz Napierala 	LIST_FOREACH(classp, &mesh.lg_class, lg_class) {
7936a027382SEdward Tomasz Napierala 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
7946a027382SEdward Tomasz Napierala 			if (!LIST_EMPTY(&gp->lg_consumer))
7956a027382SEdward Tomasz Napierala 				continue;
7966a027382SEdward Tomasz Napierala 			show_tree_geom(&mesh, gp, 0, width);
7976a027382SEdward Tomasz Napierala 		}
7986a027382SEdward Tomasz Napierala 	}
7996a027382SEdward Tomasz Napierala }
8006a027382SEdward Tomasz Napierala 
80105c91076SPawel Jakub Dawidek int
80205c91076SPawel Jakub Dawidek main(int argc, char *argv[])
80305c91076SPawel Jakub Dawidek {
8040f73f701SEdward Tomasz Napierala 	char *provider_name;
8056a027382SEdward Tomasz Napierala 	bool tflag;
8060f73f701SEdward Tomasz Napierala 	int ch;
8070f73f701SEdward Tomasz Napierala 
8080f73f701SEdward Tomasz Napierala 	provider_name = NULL;
8096a027382SEdward Tomasz Napierala 	tflag = false;
8100f73f701SEdward Tomasz Napierala 
8110f73f701SEdward Tomasz Napierala 	if (strcmp(getprogname(), "geom") == 0) {
8126a027382SEdward Tomasz Napierala 		while ((ch = getopt(argc, argv, "hp:t")) != -1) {
8130f73f701SEdward Tomasz Napierala 			switch (ch) {
8140f73f701SEdward Tomasz Napierala 			case 'p':
8150f73f701SEdward Tomasz Napierala 				provider_name = strdup(optarg);
8160f73f701SEdward Tomasz Napierala 				if (provider_name == NULL)
8170f73f701SEdward Tomasz Napierala 					err(1, "strdup");
8180f73f701SEdward Tomasz Napierala 				break;
8196a027382SEdward Tomasz Napierala 			case 't':
8206a027382SEdward Tomasz Napierala 				tflag = true;
8216a027382SEdward Tomasz Napierala 				break;
8220f73f701SEdward Tomasz Napierala 			case 'h':
8230f73f701SEdward Tomasz Napierala 			default:
8240f73f701SEdward Tomasz Napierala 				usage();
8250f73f701SEdward Tomasz Napierala 			}
8260f73f701SEdward Tomasz Napierala 		}
8270f73f701SEdward Tomasz Napierala 
8280f73f701SEdward Tomasz Napierala 		/*
8290f73f701SEdward Tomasz Napierala 		 * Don't adjust argc and argv, it would break get_class().
8300f73f701SEdward Tomasz Napierala 		 */
8310f73f701SEdward Tomasz Napierala 	}
8320f73f701SEdward Tomasz Napierala 
8336a027382SEdward Tomasz Napierala 	if (tflag && provider_name != NULL) {
8346a027382SEdward Tomasz Napierala 		errx(EXIT_FAILURE,
8356a027382SEdward Tomasz Napierala 		    "At most one of -P and -t may be specified.");
8366a027382SEdward Tomasz Napierala 	}
8376a027382SEdward Tomasz Napierala 
8380f73f701SEdward Tomasz Napierala 	if (provider_name != NULL) {
8390f73f701SEdward Tomasz Napierala 		list_one_geom_by_provider(provider_name);
8400f73f701SEdward Tomasz Napierala 		return (0);
8410f73f701SEdward Tomasz Napierala 	}
84205c91076SPawel Jakub Dawidek 
8436a027382SEdward Tomasz Napierala 	if (tflag) {
8446a027382SEdward Tomasz Napierala 		show_tree();
8456a027382SEdward Tomasz Napierala 		return (0);
8466a027382SEdward Tomasz Napierala 	}
8476a027382SEdward Tomasz Napierala 
84805c91076SPawel Jakub Dawidek 	get_class(&argc, &argv);
84905c91076SPawel Jakub Dawidek 	run_command(argc, argv);
85005c91076SPawel Jakub Dawidek 	/* NOTREACHED */
85105c91076SPawel Jakub Dawidek 
85205c91076SPawel Jakub Dawidek 	exit(EXIT_FAILURE);
85305c91076SPawel Jakub Dawidek }
85405c91076SPawel Jakub Dawidek 
85505c91076SPawel Jakub Dawidek static struct gclass *
85605c91076SPawel Jakub Dawidek find_class(struct gmesh *mesh, const char *name)
85705c91076SPawel Jakub Dawidek {
85805c91076SPawel Jakub Dawidek 	struct gclass *classp;
85905c91076SPawel Jakub Dawidek 
86005c91076SPawel Jakub Dawidek 	LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
86105c91076SPawel Jakub Dawidek 		if (strcmp(classp->lg_name, name) == 0)
86205c91076SPawel Jakub Dawidek 			return (classp);
86305c91076SPawel Jakub Dawidek 	}
86405c91076SPawel Jakub Dawidek 	return (NULL);
86505c91076SPawel Jakub Dawidek }
86605c91076SPawel Jakub Dawidek 
8671d723f1dSPawel Jakub Dawidek static struct ggeom *
8681d723f1dSPawel Jakub Dawidek find_geom(struct gclass *classp, const char *name)
86905c91076SPawel Jakub Dawidek {
87005c91076SPawel Jakub Dawidek 	struct ggeom *gp;
87105c91076SPawel Jakub Dawidek 
872f200cc25SRobert Wing 	if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
873f200cc25SRobert Wing 		name += sizeof(_PATH_DEV) - 1;
874f200cc25SRobert Wing 
87505c91076SPawel Jakub Dawidek 	LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
8761d723f1dSPawel Jakub Dawidek 		if (strcmp(gp->lg_name, name) == 0)
8771d723f1dSPawel Jakub Dawidek 			return (gp);
87805c91076SPawel Jakub Dawidek 	}
87905c91076SPawel Jakub Dawidek 	return (NULL);
88005c91076SPawel Jakub Dawidek }
88105c91076SPawel Jakub Dawidek 
88205c91076SPawel Jakub Dawidek static void
8832d76e2dbSPawel Jakub Dawidek list_one_provider(struct gprovider *pp, const char *prefix)
88405c91076SPawel Jakub Dawidek {
88505c91076SPawel Jakub Dawidek 	struct gconfig *conf;
886af565b58SPawel Jakub Dawidek 	char buf[5];
88705c91076SPawel Jakub Dawidek 
8881d723f1dSPawel Jakub Dawidek 	printf("Name: %s\n", pp->lg_name);
889af565b58SPawel Jakub Dawidek 	humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "",
890af565b58SPawel Jakub Dawidek 	    HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
8911d723f1dSPawel Jakub Dawidek 	printf("%sMediasize: %jd (%s)\n", prefix, (intmax_t)pp->lg_mediasize,
8921d723f1dSPawel Jakub Dawidek 	    buf);
8931d723f1dSPawel Jakub Dawidek 	printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize);
894e192c6e8SXin LI 	if (pp->lg_stripesize > 0 || pp->lg_stripeoffset > 0) {
89535daa28fSXin LI 		printf("%sStripesize: %ju\n", prefix, pp->lg_stripesize);
89635daa28fSXin LI 		printf("%sStripeoffset: %ju\n", prefix, pp->lg_stripeoffset);
89735daa28fSXin LI 	}
8981d723f1dSPawel Jakub Dawidek 	printf("%sMode: %s\n", prefix, pp->lg_mode);
89905c91076SPawel Jakub Dawidek 	LIST_FOREACH(conf, &pp->lg_config, lg_config) {
9001d723f1dSPawel Jakub Dawidek 		printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val);
9011d723f1dSPawel Jakub Dawidek 	}
9021d723f1dSPawel Jakub Dawidek }
9031d723f1dSPawel Jakub Dawidek 
9041d723f1dSPawel Jakub Dawidek static void
9052d76e2dbSPawel Jakub Dawidek list_one_consumer(struct gconsumer *cp, const char *prefix)
9061d723f1dSPawel Jakub Dawidek {
9071d723f1dSPawel Jakub Dawidek 	struct gprovider *pp;
9081d723f1dSPawel Jakub Dawidek 	struct gconfig *conf;
9091d723f1dSPawel Jakub Dawidek 
9101d723f1dSPawel Jakub Dawidek 	pp = cp->lg_provider;
9111d723f1dSPawel Jakub Dawidek 	if (pp == NULL)
9121d723f1dSPawel Jakub Dawidek 		printf("[no provider]\n");
9131d723f1dSPawel Jakub Dawidek 	else {
9141d723f1dSPawel Jakub Dawidek 		char buf[5];
9151d723f1dSPawel Jakub Dawidek 
9161d723f1dSPawel Jakub Dawidek 		printf("Name: %s\n", pp->lg_name);
9171d723f1dSPawel Jakub Dawidek 		humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "",
9181d723f1dSPawel Jakub Dawidek 		    HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
9191d723f1dSPawel Jakub Dawidek 		printf("%sMediasize: %jd (%s)\n", prefix,
9201d723f1dSPawel Jakub Dawidek 		    (intmax_t)pp->lg_mediasize, buf);
9211d723f1dSPawel Jakub Dawidek 		printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize);
92200b236aaSXin LI 		if (pp->lg_stripesize > 0 || pp->lg_stripeoffset > 0) {
92335daa28fSXin LI 			printf("%sStripesize: %ju\n", prefix, pp->lg_stripesize);
92435daa28fSXin LI 			printf("%sStripeoffset: %ju\n", prefix, pp->lg_stripeoffset);
92535daa28fSXin LI 		}
9261d723f1dSPawel Jakub Dawidek 		printf("%sMode: %s\n", prefix, cp->lg_mode);
9271d723f1dSPawel Jakub Dawidek 	}
9281d723f1dSPawel Jakub Dawidek 	LIST_FOREACH(conf, &cp->lg_config, lg_config) {
9291d723f1dSPawel Jakub Dawidek 		printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val);
9301d723f1dSPawel Jakub Dawidek 	}
9311d723f1dSPawel Jakub Dawidek }
9321d723f1dSPawel Jakub Dawidek 
9331d723f1dSPawel Jakub Dawidek static void
9342d76e2dbSPawel Jakub Dawidek list_one_geom(struct ggeom *gp)
9351d723f1dSPawel Jakub Dawidek {
9361d723f1dSPawel Jakub Dawidek 	struct gprovider *pp;
9371d723f1dSPawel Jakub Dawidek 	struct gconsumer *cp;
9381d723f1dSPawel Jakub Dawidek 	struct gconfig *conf;
9391d723f1dSPawel Jakub Dawidek 	unsigned n;
9401d723f1dSPawel Jakub Dawidek 
9411d723f1dSPawel Jakub Dawidek 	printf("Geom name: %s\n", gp->lg_name);
9421d723f1dSPawel Jakub Dawidek 	LIST_FOREACH(conf, &gp->lg_config, lg_config) {
9431d723f1dSPawel Jakub Dawidek 		printf("%s: %s\n", conf->lg_name, conf->lg_val);
9441d723f1dSPawel Jakub Dawidek 	}
9451d723f1dSPawel Jakub Dawidek 	if (!LIST_EMPTY(&gp->lg_provider)) {
9461d723f1dSPawel Jakub Dawidek 		printf("Providers:\n");
9471d723f1dSPawel Jakub Dawidek 		n = 1;
9481d723f1dSPawel Jakub Dawidek 		LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
9491d723f1dSPawel Jakub Dawidek 			printf("%u. ", n++);
9502d76e2dbSPawel Jakub Dawidek 			list_one_provider(pp, "   ");
9511d723f1dSPawel Jakub Dawidek 		}
9521d723f1dSPawel Jakub Dawidek 	}
9531d723f1dSPawel Jakub Dawidek 	if (!LIST_EMPTY(&gp->lg_consumer)) {
9541d723f1dSPawel Jakub Dawidek 		printf("Consumers:\n");
9551d723f1dSPawel Jakub Dawidek 		n = 1;
9561d723f1dSPawel Jakub Dawidek 		LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
9571d723f1dSPawel Jakub Dawidek 			printf("%u. ", n++);
9582d76e2dbSPawel Jakub Dawidek 			list_one_consumer(cp, "   ");
9591d723f1dSPawel Jakub Dawidek 		}
96005c91076SPawel Jakub Dawidek 	}
96105c91076SPawel Jakub Dawidek 	printf("\n");
96205c91076SPawel Jakub Dawidek }
96305c91076SPawel Jakub Dawidek 
964b70eccf3SPawel Jakub Dawidek static void
9650f73f701SEdward Tomasz Napierala list_one_geom_by_provider(const char *provider_name)
9660f73f701SEdward Tomasz Napierala {
9670f73f701SEdward Tomasz Napierala 	struct gmesh mesh;
9680f73f701SEdward Tomasz Napierala 	struct ggeom *gp;
9690f73f701SEdward Tomasz Napierala 	int error;
9700f73f701SEdward Tomasz Napierala 
9710f73f701SEdward Tomasz Napierala 	error = geom_gettree(&mesh);
9720f73f701SEdward Tomasz Napierala 	if (error != 0)
9730f73f701SEdward Tomasz Napierala 		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
9740f73f701SEdward Tomasz Napierala 
9750f73f701SEdward Tomasz Napierala 	gp = find_geom_by_provider(&mesh, provider_name);
9760f73f701SEdward Tomasz Napierala 	if (gp == NULL)
9770f73f701SEdward Tomasz Napierala 		errx(EXIT_FAILURE, "Cannot find provider '%s'.", provider_name);
9780f73f701SEdward Tomasz Napierala 
9790f73f701SEdward Tomasz Napierala 	printf("Geom class: %s\n", gp->lg_class->lg_name);
9800f73f701SEdward Tomasz Napierala 	list_one_geom(gp);
9810f73f701SEdward Tomasz Napierala }
9820f73f701SEdward Tomasz Napierala 
9830f73f701SEdward Tomasz Napierala static void
984b70eccf3SPawel Jakub Dawidek std_help(struct gctl_req *req __unused, unsigned flags __unused)
985b70eccf3SPawel Jakub Dawidek {
986b70eccf3SPawel Jakub Dawidek 
987c979e206SPawel Jakub Dawidek 	usage();
988b70eccf3SPawel Jakub Dawidek }
989b70eccf3SPawel Jakub Dawidek 
99005c91076SPawel Jakub Dawidek static int
99105c91076SPawel Jakub Dawidek std_list_available(void)
99205c91076SPawel Jakub Dawidek {
99305c91076SPawel Jakub Dawidek 	struct gmesh mesh;
99405c91076SPawel Jakub Dawidek 	struct gclass *classp;
99505c91076SPawel Jakub Dawidek 	int error;
99605c91076SPawel Jakub Dawidek 
99705c91076SPawel Jakub Dawidek 	error = geom_gettree(&mesh);
9986fc60008SPawel Jakub Dawidek 	if (error != 0)
9996fc60008SPawel Jakub Dawidek 		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
100005c91076SPawel Jakub Dawidek 	classp = find_class(&mesh, gclass_name);
100105c91076SPawel Jakub Dawidek 	geom_deletetree(&mesh);
100205c91076SPawel Jakub Dawidek 	if (classp != NULL)
100305c91076SPawel Jakub Dawidek 		return (1);
100405c91076SPawel Jakub Dawidek 	return (0);
100505c91076SPawel Jakub Dawidek }
100605c91076SPawel Jakub Dawidek 
100705c91076SPawel Jakub Dawidek static void
100805c91076SPawel Jakub Dawidek std_list(struct gctl_req *req, unsigned flags __unused)
100905c91076SPawel Jakub Dawidek {
101005c91076SPawel Jakub Dawidek 	struct gmesh mesh;
101105c91076SPawel Jakub Dawidek 	struct gclass *classp;
10121d723f1dSPawel Jakub Dawidek 	struct ggeom *gp;
1013f13942a7SPawel Jakub Dawidek 	const char *name;
101483d165c1SAlexander Motin 	int all, error, i, nargs;
101505c91076SPawel Jakub Dawidek 
101605c91076SPawel Jakub Dawidek 	error = geom_gettree(&mesh);
10176fc60008SPawel Jakub Dawidek 	if (error != 0)
10186fc60008SPawel Jakub Dawidek 		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
101905c91076SPawel Jakub Dawidek 	classp = find_class(&mesh, gclass_name);
102005c91076SPawel Jakub Dawidek 	if (classp == NULL) {
102105c91076SPawel Jakub Dawidek 		geom_deletetree(&mesh);
1022112adef8SEdward Tomasz Napierala 		errx(EXIT_FAILURE, "Class '%s' not found.", gclass_name);
102305c91076SPawel Jakub Dawidek 	}
1024f13942a7SPawel Jakub Dawidek 	nargs = gctl_get_int(req, "nargs");
102583d165c1SAlexander Motin 	all = gctl_get_int(req, "all");
1026f13942a7SPawel Jakub Dawidek 	if (nargs > 0) {
1027f13942a7SPawel Jakub Dawidek 		for (i = 0; i < nargs; i++) {
1028f13942a7SPawel Jakub Dawidek 			name = gctl_get_ascii(req, "arg%d", i);
10291d723f1dSPawel Jakub Dawidek 			gp = find_geom(classp, name);
1030112adef8SEdward Tomasz Napierala 			if (gp == NULL) {
1031112adef8SEdward Tomasz Napierala 				errx(EXIT_FAILURE, "Class '%s' does not have "
1032112adef8SEdward Tomasz Napierala 				    "an instance named '%s'.",
1033112adef8SEdward Tomasz Napierala 				    gclass_name, name);
1034112adef8SEdward Tomasz Napierala 			}
103583d165c1SAlexander Motin 			list_one_geom(gp);
103605c91076SPawel Jakub Dawidek 		}
103705c91076SPawel Jakub Dawidek 	} else {
103805c91076SPawel Jakub Dawidek 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
103983d165c1SAlexander Motin 			if (LIST_EMPTY(&gp->lg_provider) && !all)
10405fd66a44SPawel Jakub Dawidek 				continue;
10412d76e2dbSPawel Jakub Dawidek 			list_one_geom(gp);
104205c91076SPawel Jakub Dawidek 		}
104305c91076SPawel Jakub Dawidek 	}
104405c91076SPawel Jakub Dawidek 	geom_deletetree(&mesh);
104505c91076SPawel Jakub Dawidek }
104605c91076SPawel Jakub Dawidek 
104705c91076SPawel Jakub Dawidek static int
104818ee8840SPawel Jakub Dawidek std_status_available(void)
104918ee8840SPawel Jakub Dawidek {
105018ee8840SPawel Jakub Dawidek 
105118ee8840SPawel Jakub Dawidek 	/* 'status' command is available when 'list' command is. */
105218ee8840SPawel Jakub Dawidek 	return (std_list_available());
105318ee8840SPawel Jakub Dawidek }
105418ee8840SPawel Jakub Dawidek 
105518ee8840SPawel Jakub Dawidek static void
1056da80913dSPawel Jakub Dawidek status_update_len(struct ggeom *gp, int *name_len, int *status_len)
105718ee8840SPawel Jakub Dawidek {
105818ee8840SPawel Jakub Dawidek 	struct gconfig *conf;
1059da80913dSPawel Jakub Dawidek 	int len;
106018ee8840SPawel Jakub Dawidek 
106118ee8840SPawel Jakub Dawidek 	assert(gp != NULL);
106218ee8840SPawel Jakub Dawidek 	assert(name_len != NULL);
106318ee8840SPawel Jakub Dawidek 	assert(status_len != NULL);
106418ee8840SPawel Jakub Dawidek 
106518ee8840SPawel Jakub Dawidek 	len = strlen(gp->lg_name);
106618ee8840SPawel Jakub Dawidek 	if (*name_len < len)
106718ee8840SPawel Jakub Dawidek 		*name_len = len;
106818ee8840SPawel Jakub Dawidek 	LIST_FOREACH(conf, &gp->lg_config, lg_config) {
106918ee8840SPawel Jakub Dawidek 		if (strcasecmp(conf->lg_name, "state") == 0) {
107018ee8840SPawel Jakub Dawidek 			len = strlen(conf->lg_val);
107118ee8840SPawel Jakub Dawidek 			if (*status_len < len)
107218ee8840SPawel Jakub Dawidek 				*status_len = len;
107318ee8840SPawel Jakub Dawidek 		}
107418ee8840SPawel Jakub Dawidek 	}
107518ee8840SPawel Jakub Dawidek }
107618ee8840SPawel Jakub Dawidek 
107783d165c1SAlexander Motin static void
107883d165c1SAlexander Motin status_update_len_prs(struct ggeom *gp, int *name_len, int *status_len)
107983d165c1SAlexander Motin {
108083d165c1SAlexander Motin 	struct gprovider *pp;
108183d165c1SAlexander Motin 	struct gconfig *conf;
108283d165c1SAlexander Motin 	int len, glen;
108383d165c1SAlexander Motin 
108483d165c1SAlexander Motin 	assert(gp != NULL);
108583d165c1SAlexander Motin 	assert(name_len != NULL);
108683d165c1SAlexander Motin 	assert(status_len != NULL);
108783d165c1SAlexander Motin 
108883d165c1SAlexander Motin 	glen = 0;
108983d165c1SAlexander Motin 	LIST_FOREACH(conf, &gp->lg_config, lg_config) {
109083d165c1SAlexander Motin 		if (strcasecmp(conf->lg_name, "state") == 0) {
109183d165c1SAlexander Motin 			glen = strlen(conf->lg_val);
109283d165c1SAlexander Motin 			break;
109383d165c1SAlexander Motin 		}
109483d165c1SAlexander Motin 	}
109583d165c1SAlexander Motin 	LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
109683d165c1SAlexander Motin 		len = strlen(pp->lg_name);
109783d165c1SAlexander Motin 		if (*name_len < len)
109883d165c1SAlexander Motin 			*name_len = len;
109983d165c1SAlexander Motin 		len = glen;
110083d165c1SAlexander Motin 		LIST_FOREACH(conf, &pp->lg_config, lg_config) {
110183d165c1SAlexander Motin 			if (strcasecmp(conf->lg_name, "state") == 0) {
110283d165c1SAlexander Motin 				len = strlen(conf->lg_val);
110383d165c1SAlexander Motin 				break;
110483d165c1SAlexander Motin 			}
110583d165c1SAlexander Motin 		}
110683d165c1SAlexander Motin 		if (*status_len < len)
110783d165c1SAlexander Motin 			*status_len = len;
110883d165c1SAlexander Motin 	}
110983d165c1SAlexander Motin }
111083d165c1SAlexander Motin 
1111ba6821f0SPawel Jakub Dawidek static char *
111218ee8840SPawel Jakub Dawidek status_one_consumer(struct gconsumer *cp)
111318ee8840SPawel Jakub Dawidek {
1114ba6821f0SPawel Jakub Dawidek 	static char buf[256];
111518ee8840SPawel Jakub Dawidek 	struct gprovider *pp;
111618ee8840SPawel Jakub Dawidek 	struct gconfig *conf;
111783d165c1SAlexander Motin 	const char *state, *syncr;
111818ee8840SPawel Jakub Dawidek 
111918ee8840SPawel Jakub Dawidek 	pp = cp->lg_provider;
112018ee8840SPawel Jakub Dawidek 	if (pp == NULL)
1121ba6821f0SPawel Jakub Dawidek 		return (NULL);
112283d165c1SAlexander Motin 	state = NULL;
112383d165c1SAlexander Motin 	syncr = NULL;
112418ee8840SPawel Jakub Dawidek 	LIST_FOREACH(conf, &cp->lg_config, lg_config) {
112583d165c1SAlexander Motin 		if (strcasecmp(conf->lg_name, "state") == 0)
112683d165c1SAlexander Motin 			state = conf->lg_val;
112718ee8840SPawel Jakub Dawidek 		if (strcasecmp(conf->lg_name, "synchronized") == 0)
112883d165c1SAlexander Motin 			syncr = conf->lg_val;
112918ee8840SPawel Jakub Dawidek 	}
113083d165c1SAlexander Motin 	if (state == NULL && syncr == NULL)
1131ba6821f0SPawel Jakub Dawidek 		snprintf(buf, sizeof(buf), "%s", pp->lg_name);
113283d165c1SAlexander Motin 	else if (state != NULL && syncr != NULL) {
113383d165c1SAlexander Motin 		snprintf(buf, sizeof(buf), "%s (%s, %s)", pp->lg_name,
113483d165c1SAlexander Motin 		    state, syncr);
113583d165c1SAlexander Motin 	} else {
1136ba6821f0SPawel Jakub Dawidek 		snprintf(buf, sizeof(buf), "%s (%s)", pp->lg_name,
113783d165c1SAlexander Motin 		    state ? state : syncr);
1138ba6821f0SPawel Jakub Dawidek 	}
1139ba6821f0SPawel Jakub Dawidek 	return (buf);
114018ee8840SPawel Jakub Dawidek }
114118ee8840SPawel Jakub Dawidek 
114218ee8840SPawel Jakub Dawidek static void
1143ba6821f0SPawel Jakub Dawidek status_one_geom(struct ggeom *gp, int script, int name_len, int status_len)
114418ee8840SPawel Jakub Dawidek {
114518ee8840SPawel Jakub Dawidek 	struct gconsumer *cp;
114618ee8840SPawel Jakub Dawidek 	struct gconfig *conf;
1147ba6821f0SPawel Jakub Dawidek 	const char *name, *status, *component;
1148ba6821f0SPawel Jakub Dawidek 	int gotone;
114918ee8840SPawel Jakub Dawidek 
115018ee8840SPawel Jakub Dawidek 	name = gp->lg_name;
115183d165c1SAlexander Motin 	status = "N/A";
115218ee8840SPawel Jakub Dawidek 	LIST_FOREACH(conf, &gp->lg_config, lg_config) {
115383d165c1SAlexander Motin 		if (strcasecmp(conf->lg_name, "state") == 0) {
115483d165c1SAlexander Motin 			status = conf->lg_val;
115518ee8840SPawel Jakub Dawidek 			break;
115618ee8840SPawel Jakub Dawidek 		}
115783d165c1SAlexander Motin 	}
1158ba6821f0SPawel Jakub Dawidek 	gotone = 0;
115918ee8840SPawel Jakub Dawidek 	LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
1160ba6821f0SPawel Jakub Dawidek 		component = status_one_consumer(cp);
1161ba6821f0SPawel Jakub Dawidek 		if (component == NULL)
1162ba6821f0SPawel Jakub Dawidek 			continue;
1163ba6821f0SPawel Jakub Dawidek 		gotone = 1;
1164ba6821f0SPawel Jakub Dawidek 		printf("%*s  %*s  %s\n", name_len, name, status_len, status,
1165ba6821f0SPawel Jakub Dawidek 		    component);
1166ba6821f0SPawel Jakub Dawidek 		if (!script)
1167ba6821f0SPawel Jakub Dawidek 			name = status = "";
116818ee8840SPawel Jakub Dawidek 	}
1169ba6821f0SPawel Jakub Dawidek 	if (!gotone) {
1170ba6821f0SPawel Jakub Dawidek 		printf("%*s  %*s  %s\n", name_len, name, status_len, status,
1171ba6821f0SPawel Jakub Dawidek 		    "N/A");
1172ba6821f0SPawel Jakub Dawidek 	}
117318ee8840SPawel Jakub Dawidek }
117418ee8840SPawel Jakub Dawidek 
117518ee8840SPawel Jakub Dawidek static void
117683d165c1SAlexander Motin status_one_geom_prs(struct ggeom *gp, int script, int name_len, int status_len)
117783d165c1SAlexander Motin {
117883d165c1SAlexander Motin 	struct gprovider *pp;
117983d165c1SAlexander Motin 	struct gconsumer *cp;
118083d165c1SAlexander Motin 	struct gconfig *conf;
118183d165c1SAlexander Motin 	const char *name, *status, *component;
118283d165c1SAlexander Motin 	int gotone;
118383d165c1SAlexander Motin 
118483d165c1SAlexander Motin 	LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
118583d165c1SAlexander Motin 		name = pp->lg_name;
118683d165c1SAlexander Motin 		status = "N/A";
118783d165c1SAlexander Motin 		LIST_FOREACH(conf, &gp->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 		LIST_FOREACH(conf, &pp->lg_config, lg_config) {
119483d165c1SAlexander Motin 			if (strcasecmp(conf->lg_name, "state") == 0) {
119583d165c1SAlexander Motin 				status = conf->lg_val;
119683d165c1SAlexander Motin 				break;
119783d165c1SAlexander Motin 			}
119883d165c1SAlexander Motin 		}
119983d165c1SAlexander Motin 		gotone = 0;
120083d165c1SAlexander Motin 		LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
120183d165c1SAlexander Motin 			component = status_one_consumer(cp);
120283d165c1SAlexander Motin 			if (component == NULL)
120383d165c1SAlexander Motin 				continue;
120483d165c1SAlexander Motin 			gotone = 1;
120583d165c1SAlexander Motin 			printf("%*s  %*s  %s\n", name_len, name,
120683d165c1SAlexander Motin 			    status_len, status, component);
120783d165c1SAlexander Motin 			if (!script)
120883d165c1SAlexander Motin 				name = status = "";
120983d165c1SAlexander Motin 		}
121083d165c1SAlexander Motin 		if (!gotone) {
121183d165c1SAlexander Motin 			printf("%*s  %*s  %s\n", name_len, name,
121283d165c1SAlexander Motin 			    status_len, status, "N/A");
121383d165c1SAlexander Motin 		}
121483d165c1SAlexander Motin 	}
121583d165c1SAlexander Motin }
121683d165c1SAlexander Motin 
121783d165c1SAlexander Motin static void
121818ee8840SPawel Jakub Dawidek std_status(struct gctl_req *req, unsigned flags __unused)
121918ee8840SPawel Jakub Dawidek {
122018ee8840SPawel Jakub Dawidek 	struct gmesh mesh;
122118ee8840SPawel Jakub Dawidek 	struct gclass *classp;
122218ee8840SPawel Jakub Dawidek 	struct ggeom *gp;
1223f13942a7SPawel Jakub Dawidek 	const char *name;
1224da80913dSPawel Jakub Dawidek 	int name_len, status_len;
122583d165c1SAlexander Motin 	int all, error, geoms, i, n, nargs, script;
122618ee8840SPawel Jakub Dawidek 
122718ee8840SPawel Jakub Dawidek 	error = geom_gettree(&mesh);
12286fc60008SPawel Jakub Dawidek 	if (error != 0)
12296fc60008SPawel Jakub Dawidek 		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
123018ee8840SPawel Jakub Dawidek 	classp = find_class(&mesh, gclass_name);
12315d47cd0fSPawel Jakub Dawidek 	if (classp == NULL)
12325d47cd0fSPawel Jakub Dawidek 		errx(EXIT_FAILURE, "Class %s not found.", gclass_name);
1233f13942a7SPawel Jakub Dawidek 	nargs = gctl_get_int(req, "nargs");
123483d165c1SAlexander Motin 	all = gctl_get_int(req, "all");
123583d165c1SAlexander Motin 	geoms = gctl_get_int(req, "geoms");
1236f13942a7SPawel Jakub Dawidek 	script = gctl_get_int(req, "script");
123783d165c1SAlexander Motin 	if (script) {
123883d165c1SAlexander Motin 		name_len = 0;
123983d165c1SAlexander Motin 		status_len = 0;
124083d165c1SAlexander Motin 	} else {
124118ee8840SPawel Jakub Dawidek 		name_len = strlen("Name");
124218ee8840SPawel Jakub Dawidek 		status_len = strlen("Status");
124383d165c1SAlexander Motin 	}
1244f13942a7SPawel Jakub Dawidek 	if (nargs > 0) {
1245f13942a7SPawel Jakub Dawidek 		for (i = 0, n = 0; i < nargs; i++) {
1246f13942a7SPawel Jakub Dawidek 			name = gctl_get_ascii(req, "arg%d", i);
124718ee8840SPawel Jakub Dawidek 			gp = find_geom(classp, name);
124818ee8840SPawel Jakub Dawidek 			if (gp == NULL)
12495d47cd0fSPawel Jakub Dawidek 				errx(EXIT_FAILURE, "No such geom: %s.", name);
125083d165c1SAlexander Motin 			if (geoms) {
125183d165c1SAlexander Motin 				status_update_len(gp,
125283d165c1SAlexander Motin 				    &name_len, &status_len);
125383d165c1SAlexander Motin 			} else {
125483d165c1SAlexander Motin 				status_update_len_prs(gp,
125583d165c1SAlexander Motin 				    &name_len, &status_len);
125618ee8840SPawel Jakub Dawidek 			}
125783d165c1SAlexander Motin 			n++;
125818ee8840SPawel Jakub Dawidek 		}
125918ee8840SPawel Jakub Dawidek 		if (n == 0)
126018ee8840SPawel Jakub Dawidek 			goto end;
126118ee8840SPawel Jakub Dawidek 	} else {
1262f13942a7SPawel Jakub Dawidek 		n = 0;
126318ee8840SPawel Jakub Dawidek 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
126483d165c1SAlexander Motin 			if (LIST_EMPTY(&gp->lg_provider) && !all)
126518ee8840SPawel Jakub Dawidek 				continue;
126683d165c1SAlexander Motin 			if (geoms) {
126783d165c1SAlexander Motin 				status_update_len(gp,
126883d165c1SAlexander Motin 				    &name_len, &status_len);
126983d165c1SAlexander Motin 			} else {
127083d165c1SAlexander Motin 				status_update_len_prs(gp,
127183d165c1SAlexander Motin 				    &name_len, &status_len);
127283d165c1SAlexander Motin 			}
127318ee8840SPawel Jakub Dawidek 			n++;
127418ee8840SPawel Jakub Dawidek 		}
127518ee8840SPawel Jakub Dawidek 		if (n == 0)
127618ee8840SPawel Jakub Dawidek 			goto end;
127718ee8840SPawel Jakub Dawidek 	}
1278f13942a7SPawel Jakub Dawidek 	if (!script) {
1279da80913dSPawel Jakub Dawidek 		printf("%*s  %*s  %s\n", name_len, "Name", status_len, "Status",
128018ee8840SPawel Jakub Dawidek 		    "Components");
1281ba6821f0SPawel Jakub Dawidek 	}
1282f13942a7SPawel Jakub Dawidek 	if (nargs > 0) {
1283f13942a7SPawel Jakub Dawidek 		for (i = 0; i < nargs; i++) {
1284f13942a7SPawel Jakub Dawidek 			name = gctl_get_ascii(req, "arg%d", i);
128518ee8840SPawel Jakub Dawidek 			gp = find_geom(classp, name);
128683d165c1SAlexander Motin 			if (gp == NULL)
128783d165c1SAlexander Motin 				continue;
128883d165c1SAlexander Motin 			if (geoms) {
1289f13942a7SPawel Jakub Dawidek 				status_one_geom(gp, script, name_len,
1290ba6821f0SPawel Jakub Dawidek 				    status_len);
129183d165c1SAlexander Motin 			} else {
129283d165c1SAlexander Motin 				status_one_geom_prs(gp, script, name_len,
129383d165c1SAlexander Motin 				    status_len);
129418ee8840SPawel Jakub Dawidek 			}
129518ee8840SPawel Jakub Dawidek 		}
129618ee8840SPawel Jakub Dawidek 	} else {
129718ee8840SPawel Jakub Dawidek 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
129883d165c1SAlexander Motin 			if (LIST_EMPTY(&gp->lg_provider) && !all)
129918ee8840SPawel Jakub Dawidek 				continue;
130083d165c1SAlexander Motin 			if (geoms) {
130183d165c1SAlexander Motin 				status_one_geom(gp, script, name_len,
130283d165c1SAlexander Motin 				    status_len);
130383d165c1SAlexander Motin 			} else {
130483d165c1SAlexander Motin 				status_one_geom_prs(gp, script, name_len,
130583d165c1SAlexander Motin 				    status_len);
130683d165c1SAlexander Motin 			}
130718ee8840SPawel Jakub Dawidek 		}
130818ee8840SPawel Jakub Dawidek 	}
130918ee8840SPawel Jakub Dawidek end:
131018ee8840SPawel Jakub Dawidek 	geom_deletetree(&mesh);
131118ee8840SPawel Jakub Dawidek }
131218ee8840SPawel Jakub Dawidek 
131318ee8840SPawel Jakub Dawidek static int
131405c91076SPawel Jakub Dawidek std_load_available(void)
131505c91076SPawel Jakub Dawidek {
131605c91076SPawel Jakub Dawidek 	char name[MAXPATHLEN], paths[MAXPATHLEN * 8], *p;
131705c91076SPawel Jakub Dawidek 	struct stat sb;
131805c91076SPawel Jakub Dawidek 	size_t len;
131905c91076SPawel Jakub Dawidek 
132005c91076SPawel Jakub Dawidek 	snprintf(name, sizeof(name), "g_%s", class_name);
132105c91076SPawel Jakub Dawidek 	/*
132205c91076SPawel Jakub Dawidek 	 * If already in kernel, "load" command is not available.
132305c91076SPawel Jakub Dawidek 	 */
132405c91076SPawel Jakub Dawidek 	if (modfind(name) >= 0)
132505c91076SPawel Jakub Dawidek 		return (0);
132605c91076SPawel Jakub Dawidek 	bzero(paths, sizeof(paths));
132705c91076SPawel Jakub Dawidek 	len = sizeof(paths);
132805c91076SPawel Jakub Dawidek 	if (sysctlbyname("kern.module_path", paths, &len, NULL, 0) < 0)
132905c91076SPawel Jakub Dawidek 		err(EXIT_FAILURE, "sysctl(kern.module_path)");
133005c91076SPawel Jakub Dawidek 	for (p = strtok(paths, ";"); p != NULL; p = strtok(NULL, ";")) {
133105c91076SPawel Jakub Dawidek 		snprintf(name, sizeof(name), "%s/geom_%s.ko", p, class_name);
133205c91076SPawel Jakub Dawidek 		/*
133305c91076SPawel Jakub Dawidek 		 * If geom_<name>.ko file exists, "load" command is available.
133405c91076SPawel Jakub Dawidek 		 */
133505c91076SPawel Jakub Dawidek 		if (stat(name, &sb) == 0)
133605c91076SPawel Jakub Dawidek 			return (1);
133705c91076SPawel Jakub Dawidek 	}
133805c91076SPawel Jakub Dawidek 	return (0);
133905c91076SPawel Jakub Dawidek }
134005c91076SPawel Jakub Dawidek 
134105c91076SPawel Jakub Dawidek static void
134205c91076SPawel Jakub Dawidek std_load(struct gctl_req *req __unused, unsigned flags)
134305c91076SPawel Jakub Dawidek {
134405c91076SPawel Jakub Dawidek 
134505c91076SPawel Jakub Dawidek 	/*
134605c91076SPawel Jakub Dawidek 	 * Do nothing special here, because of G_FLAG_LOADKLD flag,
134705c91076SPawel Jakub Dawidek 	 * module is already loaded.
134805c91076SPawel Jakub Dawidek 	 */
134905c91076SPawel Jakub Dawidek 	if ((flags & G_FLAG_VERBOSE) != 0)
135005c91076SPawel Jakub Dawidek 		printf("Module available.\n");
135105c91076SPawel Jakub Dawidek }
135205c91076SPawel Jakub Dawidek 
135305c91076SPawel Jakub Dawidek static int
135405c91076SPawel Jakub Dawidek std_unload_available(void)
135505c91076SPawel Jakub Dawidek {
135605c91076SPawel Jakub Dawidek 	char name[64];
135705c91076SPawel Jakub Dawidek 	int id;
135805c91076SPawel Jakub Dawidek 
135905c91076SPawel Jakub Dawidek 	snprintf(name, sizeof(name), "geom_%s", class_name);
136005c91076SPawel Jakub Dawidek 	id = kldfind(name);
136105c91076SPawel Jakub Dawidek 	if (id >= 0)
136205c91076SPawel Jakub Dawidek 		return (1);
136305c91076SPawel Jakub Dawidek 	return (0);
136405c91076SPawel Jakub Dawidek }
136505c91076SPawel Jakub Dawidek 
136605c91076SPawel Jakub Dawidek static void
136705c91076SPawel Jakub Dawidek std_unload(struct gctl_req *req, unsigned flags __unused)
136805c91076SPawel Jakub Dawidek {
136905c91076SPawel Jakub Dawidek 	char name[64];
137005c91076SPawel Jakub Dawidek 	int id;
137105c91076SPawel Jakub Dawidek 
137205c91076SPawel Jakub Dawidek 	snprintf(name, sizeof(name), "geom_%s", class_name);
137305c91076SPawel Jakub Dawidek 	id = kldfind(name);
137405c91076SPawel Jakub Dawidek 	if (id < 0) {
137505c91076SPawel Jakub Dawidek 		gctl_error(req, "Could not find module: %s.", strerror(errno));
137605c91076SPawel Jakub Dawidek 		return;
137705c91076SPawel Jakub Dawidek 	}
137805c91076SPawel Jakub Dawidek 	if (kldunload(id) < 0) {
137905c91076SPawel Jakub Dawidek 		gctl_error(req, "Could not unload module: %s.",
138005c91076SPawel Jakub Dawidek 		    strerror(errno));
138105c91076SPawel Jakub Dawidek 		return;
138205c91076SPawel Jakub Dawidek 	}
138305c91076SPawel Jakub Dawidek }
138405c91076SPawel Jakub Dawidek 
138505c91076SPawel Jakub Dawidek static int
138605c91076SPawel Jakub Dawidek std_available(const char *name)
138705c91076SPawel Jakub Dawidek {
138805c91076SPawel Jakub Dawidek 
1389b70eccf3SPawel Jakub Dawidek 	if (strcmp(name, "help") == 0)
1390b70eccf3SPawel Jakub Dawidek 		return (1);
1391b70eccf3SPawel Jakub Dawidek 	else if (strcmp(name, "list") == 0)
139205c91076SPawel Jakub Dawidek 		return (std_list_available());
139318ee8840SPawel Jakub Dawidek 	else if (strcmp(name, "status") == 0)
139418ee8840SPawel Jakub Dawidek 		return (std_status_available());
139505c91076SPawel Jakub Dawidek 	else if (strcmp(name, "load") == 0)
139605c91076SPawel Jakub Dawidek 		return (std_load_available());
139705c91076SPawel Jakub Dawidek 	else if (strcmp(name, "unload") == 0)
139805c91076SPawel Jakub Dawidek 		return (std_unload_available());
139905c91076SPawel Jakub Dawidek 	else
140005c91076SPawel Jakub Dawidek 		assert(!"Unknown standard command.");
140105c91076SPawel Jakub Dawidek 	return (0);
140205c91076SPawel Jakub Dawidek }
1403