xref: /freebsd/sbin/geom/core/geom.c (revision 68bff4a07e3fa6c30a0c0ff6cf5f0bef95dcbd72)
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;
317*68bff4a0SKirk 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 	 */
339*68bff4a0SKirk McKusick 	vcount = 0;
34005c91076SPawel Jakub Dawidek 	while ((ch = getopt(*argc, *argv, opts)) != -1) {
34105c91076SPawel Jakub Dawidek 		/* Standard (not passed to kernel) options. */
342*68bff4a0SKirk 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);
346*68bff4a0SKirk McKusick 		if (opt == NULL) {
347*68bff4a0SKirk McKusick 			if (ch == 'v' && (cmd->gc_flags & G_FLAG_VERBOSE) != 0){
348*68bff4a0SKirk McKusick 				if (++vcount < 2)
349*68bff4a0SKirk McKusick 					continue;
350*68bff4a0SKirk McKusick 				else
351*68bff4a0SKirk McKusick 					warnx("Option 'v' specified twice.");
352*68bff4a0SKirk McKusick 			}
353c979e206SPawel Jakub Dawidek 			usage();
354*68bff4a0SKirk 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 
448*68bff4a0SKirk McKusick 	if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0)
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 
4901bcfab7fSPawel Jakub Dawidek 	bzero(buf, sizeof(buf));
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 {
49805c91076SPawel Jakub Dawidek 		gctl_rw_param(req, "output", sizeof(buf), buf);
49905c91076SPawel Jakub Dawidek 		errstr = gctl_issue(req);
50005c91076SPawel Jakub Dawidek 	}
50153767efdSPawel Jakub Dawidek 	if (errstr != NULL && errstr[0] != '\0') {
5026fc60008SPawel Jakub Dawidek 		warnx("%s", errstr);
503f792f1d8SPawel Jakub Dawidek 		if (strncmp(errstr, "warning: ", strlen("warning: ")) != 0) {
50405c91076SPawel Jakub Dawidek 			gctl_free(req);
50505c91076SPawel Jakub Dawidek 			exit(EXIT_FAILURE);
50605c91076SPawel Jakub Dawidek 		}
507f792f1d8SPawel Jakub Dawidek 	}
5081bcfab7fSPawel Jakub Dawidek 	if (buf[0] != '\0')
50905c91076SPawel Jakub Dawidek 		printf("%s", buf);
51005c91076SPawel Jakub Dawidek 	gctl_free(req);
51105c91076SPawel Jakub Dawidek 	if (verbose)
51205c91076SPawel Jakub Dawidek 		printf("Done.\n");
51305c91076SPawel Jakub Dawidek 	exit(EXIT_SUCCESS);
51405c91076SPawel Jakub Dawidek }
51505c91076SPawel Jakub Dawidek 
51614bf405bSMarcel Moolenaar #ifndef STATIC_GEOM_CLASSES
5173bad06e9SPawel Jakub Dawidek static const char *
5183bad06e9SPawel Jakub Dawidek library_path(void)
5193bad06e9SPawel Jakub Dawidek {
5203bad06e9SPawel Jakub Dawidek 	const char *path;
5213bad06e9SPawel Jakub Dawidek 
5223bad06e9SPawel Jakub Dawidek 	path = getenv("GEOM_LIBRARY_PATH");
5233bad06e9SPawel Jakub Dawidek 	if (path == NULL)
5248494b738SDavid E. O'Brien 		path = GEOM_CLASS_DIR;
5253bad06e9SPawel Jakub Dawidek 	return (path);
5263bad06e9SPawel Jakub Dawidek }
5273bad06e9SPawel Jakub Dawidek 
52805c91076SPawel Jakub Dawidek static void
52905c91076SPawel Jakub Dawidek load_library(void)
53005c91076SPawel Jakub Dawidek {
5315d824386SUlf Lilleengen 	char *curpath, path[MAXPATHLEN], *tofree, *totalpath;
53205c91076SPawel Jakub Dawidek 	uint32_t *lib_version;
53305c91076SPawel Jakub Dawidek 	void *dlh;
5342591e96cSUlf Lilleengen 	int ret;
53505c91076SPawel Jakub Dawidek 
5362591e96cSUlf Lilleengen 	ret = 0;
5375d824386SUlf Lilleengen 	tofree = totalpath = strdup(library_path());
5382591e96cSUlf Lilleengen 	if (totalpath == NULL)
5392591e96cSUlf Lilleengen 		err(EXIT_FAILURE, "Not enough memory for library path");
5402591e96cSUlf Lilleengen 
5412591e96cSUlf Lilleengen 	if (strchr(totalpath, ':') != NULL)
5422591e96cSUlf Lilleengen 		curpath = strsep(&totalpath, ":");
5432591e96cSUlf Lilleengen 	else
5442591e96cSUlf Lilleengen 		curpath = totalpath;
5452591e96cSUlf Lilleengen 	/* Traverse the paths to find one that contains the library we want. */
5462591e96cSUlf Lilleengen 	while (curpath != NULL) {
5472591e96cSUlf Lilleengen 		snprintf(path, sizeof(path), "%s/geom_%s.so", curpath,
5483bad06e9SPawel Jakub Dawidek 		    class_name);
5492591e96cSUlf Lilleengen 		ret = access(path, F_OK);
5502591e96cSUlf Lilleengen 		if (ret == -1) {
551a73148d2SPawel Jakub Dawidek 			if (errno == ENOENT) {
55205c91076SPawel Jakub Dawidek 				/*
5532591e96cSUlf Lilleengen 				 * If we cannot find library, try the next
5542591e96cSUlf Lilleengen 				 * path.
55505c91076SPawel Jakub Dawidek 				 */
5562591e96cSUlf Lilleengen 				curpath = strsep(&totalpath, ":");
5572591e96cSUlf Lilleengen 				continue;
55805c91076SPawel Jakub Dawidek 			}
559a73148d2SPawel Jakub Dawidek 			err(EXIT_FAILURE, "Cannot access library");
560a73148d2SPawel Jakub Dawidek 		}
5612591e96cSUlf Lilleengen 		break;
5622591e96cSUlf Lilleengen 	}
5635d824386SUlf Lilleengen 	free(tofree);
5642591e96cSUlf Lilleengen 	/* No library was found, but standard commands can still be used */
5652591e96cSUlf Lilleengen 	if (ret == -1)
5662591e96cSUlf Lilleengen 		return;
567a73148d2SPawel Jakub Dawidek 	dlh = dlopen(path, RTLD_NOW);
568a73148d2SPawel Jakub Dawidek 	if (dlh == NULL)
569a73148d2SPawel Jakub Dawidek 		errx(EXIT_FAILURE, "Cannot open library: %s.", dlerror());
57005c91076SPawel Jakub Dawidek 	lib_version = dlsym(dlh, "lib_version");
57105c91076SPawel Jakub Dawidek 	if (lib_version == NULL) {
5726fc60008SPawel Jakub Dawidek 		warnx("Cannot find symbol %s: %s.", "lib_version", dlerror());
57305c91076SPawel Jakub Dawidek 		dlclose(dlh);
57405c91076SPawel Jakub Dawidek 		exit(EXIT_FAILURE);
57505c91076SPawel Jakub Dawidek 	}
57605c91076SPawel Jakub Dawidek 	if (*lib_version != G_LIB_VERSION) {
57705c91076SPawel Jakub Dawidek 		dlclose(dlh);
578f792f1d8SPawel Jakub Dawidek 		errx(EXIT_FAILURE, "%s and %s are not synchronized.",
579f792f1d8SPawel Jakub Dawidek 		    getprogname(), path);
58005c91076SPawel Jakub Dawidek 	}
58105c91076SPawel Jakub Dawidek 	version = dlsym(dlh, "version");
58205c91076SPawel Jakub Dawidek 	if (version == NULL) {
5836fc60008SPawel Jakub Dawidek 		warnx("Cannot find symbol %s: %s.", "version", dlerror());
58405c91076SPawel Jakub Dawidek 		dlclose(dlh);
58505c91076SPawel Jakub Dawidek 		exit(EXIT_FAILURE);
58605c91076SPawel Jakub Dawidek 	}
58705c91076SPawel Jakub Dawidek 	class_commands = dlsym(dlh, "class_commands");
58805c91076SPawel Jakub Dawidek 	if (class_commands == NULL) {
5896fc60008SPawel Jakub Dawidek 		warnx("Cannot find symbol %s: %s.", "class_commands",
5906fc60008SPawel Jakub Dawidek 		    dlerror());
59105c91076SPawel Jakub Dawidek 		dlclose(dlh);
59205c91076SPawel Jakub Dawidek 		exit(EXIT_FAILURE);
59305c91076SPawel Jakub Dawidek 	}
59405c91076SPawel Jakub Dawidek }
59514bf405bSMarcel Moolenaar #endif	/* !STATIC_GEOM_CLASSES */
59605c91076SPawel Jakub Dawidek 
59705c91076SPawel Jakub Dawidek /*
59805c91076SPawel Jakub Dawidek  * Class name should be all capital letters.
59905c91076SPawel Jakub Dawidek  */
60005c91076SPawel Jakub Dawidek static void
60105c91076SPawel Jakub Dawidek set_class_name(void)
60205c91076SPawel Jakub Dawidek {
60305c91076SPawel Jakub Dawidek 	char *s1, *s2;
60405c91076SPawel Jakub Dawidek 
605ee602fbbSPawel Jakub Dawidek 	s1 = class_name;
606ee602fbbSPawel Jakub Dawidek 	for (; *s1 != '\0'; s1++)
607ee602fbbSPawel Jakub Dawidek 		*s1 = tolower(*s1);
608cb94ab30SPawel Jakub Dawidek 	gclass_name = malloc(strlen(class_name) + 1);
60905c91076SPawel Jakub Dawidek 	if (gclass_name == NULL)
61005c91076SPawel Jakub Dawidek 		errx(EXIT_FAILURE, "No memory");
61105c91076SPawel Jakub Dawidek 	s1 = gclass_name;
61205c91076SPawel Jakub Dawidek 	s2 = class_name;
61305c91076SPawel Jakub Dawidek 	for (; *s2 != '\0'; s2++)
61405c91076SPawel Jakub Dawidek 		*s1++ = toupper(*s2);
61505c91076SPawel Jakub Dawidek 	*s1 = '\0';
61605c91076SPawel Jakub Dawidek }
61705c91076SPawel Jakub Dawidek 
61805c91076SPawel Jakub Dawidek static void
61905c91076SPawel Jakub Dawidek get_class(int *argc, char ***argv)
62005c91076SPawel Jakub Dawidek {
62105c91076SPawel Jakub Dawidek 
62205c91076SPawel Jakub Dawidek 	snprintf(comm, sizeof(comm), "%s", basename((*argv)[0]));
62305c91076SPawel Jakub Dawidek 	if (strcmp(comm, "geom") == 0) {
62405c91076SPawel Jakub Dawidek 		if (*argc < 2)
625c979e206SPawel Jakub Dawidek 			usage();
626687b0015SPawel Jakub Dawidek 		else if (*argc == 2) {
627687b0015SPawel Jakub Dawidek 			if (strcmp((*argv)[1], "-h") == 0 ||
628687b0015SPawel Jakub Dawidek 			    strcmp((*argv)[1], "help") == 0) {
629c979e206SPawel Jakub Dawidek 				usage();
630687b0015SPawel Jakub Dawidek 			}
631687b0015SPawel Jakub Dawidek 		}
63205c91076SPawel Jakub Dawidek 		strlcatf(comm, sizeof(comm), " %s", (*argv)[1]);
63305c91076SPawel Jakub Dawidek 		class_name = (*argv)[1];
63405c91076SPawel Jakub Dawidek 		*argc -= 2;
63505c91076SPawel Jakub Dawidek 		*argv += 2;
63605c91076SPawel Jakub Dawidek 	} else if (*comm == 'g') {
63705c91076SPawel Jakub Dawidek 		class_name = comm + 1;
63805c91076SPawel Jakub Dawidek 		*argc -= 1;
63905c91076SPawel Jakub Dawidek 		*argv += 1;
64005c91076SPawel Jakub Dawidek 	} else {
64105c91076SPawel Jakub Dawidek 		errx(EXIT_FAILURE, "Invalid utility name.");
64205c91076SPawel Jakub Dawidek 	}
643a16f9b36SMarcel Moolenaar 
64414bf405bSMarcel Moolenaar #ifndef STATIC_GEOM_CLASSES
64505c91076SPawel Jakub Dawidek 	load_library();
646a16f9b36SMarcel Moolenaar #else
647a16f9b36SMarcel Moolenaar 	if (!strcasecmp(class_name, "part")) {
648a16f9b36SMarcel Moolenaar 		version = &gpart_version;
649a16f9b36SMarcel Moolenaar 		class_commands = gpart_class_commands;
650bc69d66fSXin LI 	} else if (!strcasecmp(class_name, "label")) {
651bc69d66fSXin LI 		version = &glabel_version;
652bc69d66fSXin LI 		class_commands = glabel_class_commands;
653c6678483SAlexander Motin 	}
65414bf405bSMarcel Moolenaar #endif /* !STATIC_GEOM_CLASSES */
655a16f9b36SMarcel Moolenaar 
656a16f9b36SMarcel Moolenaar 	set_class_name();
657e2d52f14SMark Felder 
658e2d52f14SMark Felder 	/* If we can't load or list, it's not a class. */
659e2d52f14SMark Felder 	if (!std_available("load") && !std_available("list"))
660112adef8SEdward Tomasz Napierala 		errx(EXIT_FAILURE, "Invalid class name '%s'.", class_name);
661e2d52f14SMark Felder 
66205c91076SPawel Jakub Dawidek 	if (*argc < 1)
663c979e206SPawel Jakub Dawidek 		usage();
66405c91076SPawel Jakub Dawidek }
66505c91076SPawel Jakub Dawidek 
6660f73f701SEdward Tomasz Napierala static struct ggeom *
6670f73f701SEdward Tomasz Napierala find_geom_by_provider(struct gmesh *mesh, const char *name)
6680f73f701SEdward Tomasz Napierala {
6690f73f701SEdward Tomasz Napierala 	struct gclass *classp;
6700f73f701SEdward Tomasz Napierala 	struct ggeom *gp;
6710f73f701SEdward Tomasz Napierala 	struct gprovider *pp;
6720f73f701SEdward Tomasz Napierala 
6730f73f701SEdward Tomasz Napierala 	LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
6740f73f701SEdward Tomasz Napierala 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
6750f73f701SEdward Tomasz Napierala 			LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
6760f73f701SEdward Tomasz Napierala 				if (strcmp(pp->lg_name, name) == 0)
6770f73f701SEdward Tomasz Napierala 					return (gp);
6780f73f701SEdward Tomasz Napierala 			}
6790f73f701SEdward Tomasz Napierala 		}
6800f73f701SEdward Tomasz Napierala 	}
6810f73f701SEdward Tomasz Napierala 
6820f73f701SEdward Tomasz Napierala 	return (NULL);
6830f73f701SEdward Tomasz Napierala }
6840f73f701SEdward Tomasz Napierala 
6856a027382SEdward Tomasz Napierala static int
6866a027382SEdward Tomasz Napierala compute_tree_width_geom(struct gmesh *mesh, struct ggeom *gp, int indent)
6876a027382SEdward Tomasz Napierala {
6886a027382SEdward Tomasz Napierala 	struct gclass *classp2;
6896a027382SEdward Tomasz Napierala 	struct ggeom *gp2;
6906a027382SEdward Tomasz Napierala 	struct gconsumer *cp2;
6916a027382SEdward Tomasz Napierala 	struct gprovider *pp;
6926a027382SEdward Tomasz Napierala 	int max_width, width;
6936a027382SEdward Tomasz Napierala 
6946a027382SEdward Tomasz Napierala 	max_width = width = indent + strlen(gp->lg_name);
6956a027382SEdward Tomasz Napierala 
6966a027382SEdward Tomasz Napierala 	LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
6976a027382SEdward Tomasz Napierala 		LIST_FOREACH(classp2, &mesh->lg_class, lg_class) {
6986a027382SEdward Tomasz Napierala 			LIST_FOREACH(gp2, &classp2->lg_geom, lg_geom) {
6996a027382SEdward Tomasz Napierala 				LIST_FOREACH(cp2,
7006a027382SEdward Tomasz Napierala 				    &gp2->lg_consumer, lg_consumer) {
7016a027382SEdward Tomasz Napierala 					if (pp != cp2->lg_provider)
7026a027382SEdward Tomasz Napierala 						continue;
7036a027382SEdward Tomasz Napierala 					width = compute_tree_width_geom(mesh,
7046a027382SEdward Tomasz Napierala 					    gp2, indent + 2);
7056a027382SEdward Tomasz Napierala 					if (width > max_width)
7066a027382SEdward Tomasz Napierala 						max_width = width;
7076a027382SEdward Tomasz Napierala 				}
7086a027382SEdward Tomasz Napierala 			}
7096a027382SEdward Tomasz Napierala 		}
7106a027382SEdward Tomasz Napierala 	}
7116a027382SEdward Tomasz Napierala 
7126a027382SEdward Tomasz Napierala 	return (max_width);
7136a027382SEdward Tomasz Napierala }
7146a027382SEdward Tomasz Napierala 
7156a027382SEdward Tomasz Napierala static int
7166a027382SEdward Tomasz Napierala compute_tree_width(struct gmesh *mesh)
7176a027382SEdward Tomasz Napierala {
7186a027382SEdward Tomasz Napierala 	struct gclass *classp;
7196a027382SEdward Tomasz Napierala 	struct ggeom *gp;
7206a027382SEdward Tomasz Napierala 	int max_width, width;
7216a027382SEdward Tomasz Napierala 
7226a027382SEdward Tomasz Napierala 	max_width = width = 0;
7236a027382SEdward Tomasz Napierala 
7246a027382SEdward Tomasz Napierala 	LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
7256a027382SEdward Tomasz Napierala 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
7266a027382SEdward Tomasz Napierala 			if (!LIST_EMPTY(&gp->lg_consumer))
7276a027382SEdward Tomasz Napierala 				continue;
7286a027382SEdward Tomasz Napierala 			width = compute_tree_width_geom(mesh, gp, 0);
7296a027382SEdward Tomasz Napierala 			if (width > max_width)
7306a027382SEdward Tomasz Napierala 				max_width = width;
7316a027382SEdward Tomasz Napierala 		}
7326a027382SEdward Tomasz Napierala 	}
7336a027382SEdward Tomasz Napierala 
7346a027382SEdward Tomasz Napierala 	return (max_width);
7356a027382SEdward Tomasz Napierala }
7366a027382SEdward Tomasz Napierala 
7376a027382SEdward Tomasz Napierala static void
7386a027382SEdward Tomasz Napierala show_tree_geom(struct gmesh *mesh, struct ggeom *gp, int indent, int width)
7396a027382SEdward Tomasz Napierala {
7406a027382SEdward Tomasz Napierala 	struct gclass *classp2;
7416a027382SEdward Tomasz Napierala 	struct ggeom *gp2;
7426a027382SEdward Tomasz Napierala 	struct gconsumer *cp2;
7436a027382SEdward Tomasz Napierala 	struct gprovider *pp;
7446a027382SEdward Tomasz Napierala 
7456a027382SEdward Tomasz Napierala 	if (LIST_EMPTY(&gp->lg_provider)) {
7466a027382SEdward Tomasz Napierala 		printf("%*s%-*.*s %-*.*s\n", indent, "",
7476a027382SEdward Tomasz Napierala 		    width - indent, width - indent, gp->lg_name,
7486a027382SEdward Tomasz Napierala 		    GEOM_CLASS_WIDTH, GEOM_CLASS_WIDTH, gp->lg_class->lg_name);
7496a027382SEdward Tomasz Napierala 		return;
7506a027382SEdward Tomasz Napierala 	}
7516a027382SEdward Tomasz Napierala 
7526a027382SEdward Tomasz Napierala 	LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
7536a027382SEdward Tomasz Napierala 		printf("%*s%-*.*s %-*.*s %s\n", indent, "",
7546a027382SEdward Tomasz Napierala 		    width - indent, width - indent, gp->lg_name,
7556a027382SEdward Tomasz Napierala 		    GEOM_CLASS_WIDTH, GEOM_CLASS_WIDTH, gp->lg_class->lg_name,
7566a027382SEdward Tomasz Napierala 		    pp->lg_name);
7576a027382SEdward Tomasz Napierala 
7586a027382SEdward Tomasz Napierala 		LIST_FOREACH(classp2, &mesh->lg_class, lg_class) {
7596a027382SEdward Tomasz Napierala 			LIST_FOREACH(gp2, &classp2->lg_geom, lg_geom) {
7606a027382SEdward Tomasz Napierala 				LIST_FOREACH(cp2,
7616a027382SEdward Tomasz Napierala 				    &gp2->lg_consumer, lg_consumer) {
7626a027382SEdward Tomasz Napierala 					if (pp != cp2->lg_provider)
7636a027382SEdward Tomasz Napierala 						continue;
7646a027382SEdward Tomasz Napierala 					show_tree_geom(mesh, gp2,
7656a027382SEdward Tomasz Napierala 					    indent + 2, width);
7666a027382SEdward Tomasz Napierala 				}
7676a027382SEdward Tomasz Napierala 			}
7686a027382SEdward Tomasz Napierala 		}
7696a027382SEdward Tomasz Napierala 	}
7706a027382SEdward Tomasz Napierala }
7716a027382SEdward Tomasz Napierala 
7726a027382SEdward Tomasz Napierala static void
7736a027382SEdward Tomasz Napierala show_tree(void)
7746a027382SEdward Tomasz Napierala {
7756a027382SEdward Tomasz Napierala 	struct gmesh mesh;
7766a027382SEdward Tomasz Napierala 	struct gclass *classp;
7776a027382SEdward Tomasz Napierala 	struct ggeom *gp;
7786a027382SEdward Tomasz Napierala 	int error, width;
7796a027382SEdward Tomasz Napierala 
7806a027382SEdward Tomasz Napierala 	error = geom_gettree(&mesh);
7816a027382SEdward Tomasz Napierala 	if (error != 0)
7826a027382SEdward Tomasz Napierala 		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
7836a027382SEdward Tomasz Napierala 
7846a027382SEdward Tomasz Napierala 	width = compute_tree_width(&mesh);
7856a027382SEdward Tomasz Napierala 
7866a027382SEdward Tomasz Napierala 	printf("%-*.*s %-*.*s %s\n",
7876a027382SEdward Tomasz Napierala 	    width, width, "Geom",
7886a027382SEdward Tomasz Napierala 	    GEOM_CLASS_WIDTH, GEOM_CLASS_WIDTH, "Class",
7896a027382SEdward Tomasz Napierala 	    "Provider");
7906a027382SEdward Tomasz Napierala 
7916a027382SEdward Tomasz Napierala 	LIST_FOREACH(classp, &mesh.lg_class, lg_class) {
7926a027382SEdward Tomasz Napierala 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
7936a027382SEdward Tomasz Napierala 			if (!LIST_EMPTY(&gp->lg_consumer))
7946a027382SEdward Tomasz Napierala 				continue;
7956a027382SEdward Tomasz Napierala 			show_tree_geom(&mesh, gp, 0, width);
7966a027382SEdward Tomasz Napierala 		}
7976a027382SEdward Tomasz Napierala 	}
7986a027382SEdward Tomasz Napierala }
7996a027382SEdward Tomasz Napierala 
80005c91076SPawel Jakub Dawidek int
80105c91076SPawel Jakub Dawidek main(int argc, char *argv[])
80205c91076SPawel Jakub Dawidek {
8030f73f701SEdward Tomasz Napierala 	char *provider_name;
8046a027382SEdward Tomasz Napierala 	bool tflag;
8050f73f701SEdward Tomasz Napierala 	int ch;
8060f73f701SEdward Tomasz Napierala 
8070f73f701SEdward Tomasz Napierala 	provider_name = NULL;
8086a027382SEdward Tomasz Napierala 	tflag = false;
8090f73f701SEdward Tomasz Napierala 
8100f73f701SEdward Tomasz Napierala 	if (strcmp(getprogname(), "geom") == 0) {
8116a027382SEdward Tomasz Napierala 		while ((ch = getopt(argc, argv, "hp:t")) != -1) {
8120f73f701SEdward Tomasz Napierala 			switch (ch) {
8130f73f701SEdward Tomasz Napierala 			case 'p':
8140f73f701SEdward Tomasz Napierala 				provider_name = strdup(optarg);
8150f73f701SEdward Tomasz Napierala 				if (provider_name == NULL)
8160f73f701SEdward Tomasz Napierala 					err(1, "strdup");
8170f73f701SEdward Tomasz Napierala 				break;
8186a027382SEdward Tomasz Napierala 			case 't':
8196a027382SEdward Tomasz Napierala 				tflag = true;
8206a027382SEdward Tomasz Napierala 				break;
8210f73f701SEdward Tomasz Napierala 			case 'h':
8220f73f701SEdward Tomasz Napierala 			default:
8230f73f701SEdward Tomasz Napierala 				usage();
8240f73f701SEdward Tomasz Napierala 			}
8250f73f701SEdward Tomasz Napierala 		}
8260f73f701SEdward Tomasz Napierala 
8270f73f701SEdward Tomasz Napierala 		/*
8280f73f701SEdward Tomasz Napierala 		 * Don't adjust argc and argv, it would break get_class().
8290f73f701SEdward Tomasz Napierala 		 */
8300f73f701SEdward Tomasz Napierala 	}
8310f73f701SEdward Tomasz Napierala 
8326a027382SEdward Tomasz Napierala 	if (tflag && provider_name != NULL) {
8336a027382SEdward Tomasz Napierala 		errx(EXIT_FAILURE,
8346a027382SEdward Tomasz Napierala 		    "At most one of -P and -t may be specified.");
8356a027382SEdward Tomasz Napierala 	}
8366a027382SEdward Tomasz Napierala 
8370f73f701SEdward Tomasz Napierala 	if (provider_name != NULL) {
8380f73f701SEdward Tomasz Napierala 		list_one_geom_by_provider(provider_name);
8390f73f701SEdward Tomasz Napierala 		return (0);
8400f73f701SEdward Tomasz Napierala 	}
84105c91076SPawel Jakub Dawidek 
8426a027382SEdward Tomasz Napierala 	if (tflag) {
8436a027382SEdward Tomasz Napierala 		show_tree();
8446a027382SEdward Tomasz Napierala 		return (0);
8456a027382SEdward Tomasz Napierala 	}
8466a027382SEdward Tomasz Napierala 
84705c91076SPawel Jakub Dawidek 	get_class(&argc, &argv);
84805c91076SPawel Jakub Dawidek 	run_command(argc, argv);
84905c91076SPawel Jakub Dawidek 	/* NOTREACHED */
85005c91076SPawel Jakub Dawidek 
85105c91076SPawel Jakub Dawidek 	exit(EXIT_FAILURE);
85205c91076SPawel Jakub Dawidek }
85305c91076SPawel Jakub Dawidek 
85405c91076SPawel Jakub Dawidek static struct gclass *
85505c91076SPawel Jakub Dawidek find_class(struct gmesh *mesh, const char *name)
85605c91076SPawel Jakub Dawidek {
85705c91076SPawel Jakub Dawidek 	struct gclass *classp;
85805c91076SPawel Jakub Dawidek 
85905c91076SPawel Jakub Dawidek 	LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
86005c91076SPawel Jakub Dawidek 		if (strcmp(classp->lg_name, name) == 0)
86105c91076SPawel Jakub Dawidek 			return (classp);
86205c91076SPawel Jakub Dawidek 	}
86305c91076SPawel Jakub Dawidek 	return (NULL);
86405c91076SPawel Jakub Dawidek }
86505c91076SPawel Jakub Dawidek 
8661d723f1dSPawel Jakub Dawidek static struct ggeom *
8671d723f1dSPawel Jakub Dawidek find_geom(struct gclass *classp, const char *name)
86805c91076SPawel Jakub Dawidek {
86905c91076SPawel Jakub Dawidek 	struct ggeom *gp;
87005c91076SPawel Jakub Dawidek 
871f200cc25SRobert Wing 	if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
872f200cc25SRobert Wing 		name += sizeof(_PATH_DEV) - 1;
873f200cc25SRobert Wing 
87405c91076SPawel Jakub Dawidek 	LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
8751d723f1dSPawel Jakub Dawidek 		if (strcmp(gp->lg_name, name) == 0)
8761d723f1dSPawel Jakub Dawidek 			return (gp);
87705c91076SPawel Jakub Dawidek 	}
87805c91076SPawel Jakub Dawidek 	return (NULL);
87905c91076SPawel Jakub Dawidek }
88005c91076SPawel Jakub Dawidek 
88105c91076SPawel Jakub Dawidek static void
8822d76e2dbSPawel Jakub Dawidek list_one_provider(struct gprovider *pp, const char *prefix)
88305c91076SPawel Jakub Dawidek {
88405c91076SPawel Jakub Dawidek 	struct gconfig *conf;
885af565b58SPawel Jakub Dawidek 	char buf[5];
88605c91076SPawel Jakub Dawidek 
8871d723f1dSPawel Jakub Dawidek 	printf("Name: %s\n", pp->lg_name);
888af565b58SPawel Jakub Dawidek 	humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "",
889af565b58SPawel Jakub Dawidek 	    HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
8901d723f1dSPawel Jakub Dawidek 	printf("%sMediasize: %jd (%s)\n", prefix, (intmax_t)pp->lg_mediasize,
8911d723f1dSPawel Jakub Dawidek 	    buf);
8921d723f1dSPawel Jakub Dawidek 	printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize);
893e192c6e8SXin LI 	if (pp->lg_stripesize > 0 || pp->lg_stripeoffset > 0) {
89435daa28fSXin LI 		printf("%sStripesize: %ju\n", prefix, pp->lg_stripesize);
89535daa28fSXin LI 		printf("%sStripeoffset: %ju\n", prefix, pp->lg_stripeoffset);
89635daa28fSXin LI 	}
8971d723f1dSPawel Jakub Dawidek 	printf("%sMode: %s\n", prefix, pp->lg_mode);
89805c91076SPawel Jakub Dawidek 	LIST_FOREACH(conf, &pp->lg_config, lg_config) {
8991d723f1dSPawel Jakub Dawidek 		printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val);
9001d723f1dSPawel Jakub Dawidek 	}
9011d723f1dSPawel Jakub Dawidek }
9021d723f1dSPawel Jakub Dawidek 
9031d723f1dSPawel Jakub Dawidek static void
9042d76e2dbSPawel Jakub Dawidek list_one_consumer(struct gconsumer *cp, const char *prefix)
9051d723f1dSPawel Jakub Dawidek {
9061d723f1dSPawel Jakub Dawidek 	struct gprovider *pp;
9071d723f1dSPawel Jakub Dawidek 	struct gconfig *conf;
9081d723f1dSPawel Jakub Dawidek 
9091d723f1dSPawel Jakub Dawidek 	pp = cp->lg_provider;
9101d723f1dSPawel Jakub Dawidek 	if (pp == NULL)
9111d723f1dSPawel Jakub Dawidek 		printf("[no provider]\n");
9121d723f1dSPawel Jakub Dawidek 	else {
9131d723f1dSPawel Jakub Dawidek 		char buf[5];
9141d723f1dSPawel Jakub Dawidek 
9151d723f1dSPawel Jakub Dawidek 		printf("Name: %s\n", pp->lg_name);
9161d723f1dSPawel Jakub Dawidek 		humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "",
9171d723f1dSPawel Jakub Dawidek 		    HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
9181d723f1dSPawel Jakub Dawidek 		printf("%sMediasize: %jd (%s)\n", prefix,
9191d723f1dSPawel Jakub Dawidek 		    (intmax_t)pp->lg_mediasize, buf);
9201d723f1dSPawel Jakub Dawidek 		printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize);
92100b236aaSXin LI 		if (pp->lg_stripesize > 0 || pp->lg_stripeoffset > 0) {
92235daa28fSXin LI 			printf("%sStripesize: %ju\n", prefix, pp->lg_stripesize);
92335daa28fSXin LI 			printf("%sStripeoffset: %ju\n", prefix, pp->lg_stripeoffset);
92435daa28fSXin LI 		}
9251d723f1dSPawel Jakub Dawidek 		printf("%sMode: %s\n", prefix, cp->lg_mode);
9261d723f1dSPawel Jakub Dawidek 	}
9271d723f1dSPawel Jakub Dawidek 	LIST_FOREACH(conf, &cp->lg_config, lg_config) {
9281d723f1dSPawel Jakub Dawidek 		printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val);
9291d723f1dSPawel Jakub Dawidek 	}
9301d723f1dSPawel Jakub Dawidek }
9311d723f1dSPawel Jakub Dawidek 
9321d723f1dSPawel Jakub Dawidek static void
9332d76e2dbSPawel Jakub Dawidek list_one_geom(struct ggeom *gp)
9341d723f1dSPawel Jakub Dawidek {
9351d723f1dSPawel Jakub Dawidek 	struct gprovider *pp;
9361d723f1dSPawel Jakub Dawidek 	struct gconsumer *cp;
9371d723f1dSPawel Jakub Dawidek 	struct gconfig *conf;
9381d723f1dSPawel Jakub Dawidek 	unsigned n;
9391d723f1dSPawel Jakub Dawidek 
9401d723f1dSPawel Jakub Dawidek 	printf("Geom name: %s\n", gp->lg_name);
9411d723f1dSPawel Jakub Dawidek 	LIST_FOREACH(conf, &gp->lg_config, lg_config) {
9421d723f1dSPawel Jakub Dawidek 		printf("%s: %s\n", conf->lg_name, conf->lg_val);
9431d723f1dSPawel Jakub Dawidek 	}
9441d723f1dSPawel Jakub Dawidek 	if (!LIST_EMPTY(&gp->lg_provider)) {
9451d723f1dSPawel Jakub Dawidek 		printf("Providers:\n");
9461d723f1dSPawel Jakub Dawidek 		n = 1;
9471d723f1dSPawel Jakub Dawidek 		LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
9481d723f1dSPawel Jakub Dawidek 			printf("%u. ", n++);
9492d76e2dbSPawel Jakub Dawidek 			list_one_provider(pp, "   ");
9501d723f1dSPawel Jakub Dawidek 		}
9511d723f1dSPawel Jakub Dawidek 	}
9521d723f1dSPawel Jakub Dawidek 	if (!LIST_EMPTY(&gp->lg_consumer)) {
9531d723f1dSPawel Jakub Dawidek 		printf("Consumers:\n");
9541d723f1dSPawel Jakub Dawidek 		n = 1;
9551d723f1dSPawel Jakub Dawidek 		LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
9561d723f1dSPawel Jakub Dawidek 			printf("%u. ", n++);
9572d76e2dbSPawel Jakub Dawidek 			list_one_consumer(cp, "   ");
9581d723f1dSPawel Jakub Dawidek 		}
95905c91076SPawel Jakub Dawidek 	}
96005c91076SPawel Jakub Dawidek 	printf("\n");
96105c91076SPawel Jakub Dawidek }
96205c91076SPawel Jakub Dawidek 
963b70eccf3SPawel Jakub Dawidek static void
9640f73f701SEdward Tomasz Napierala list_one_geom_by_provider(const char *provider_name)
9650f73f701SEdward Tomasz Napierala {
9660f73f701SEdward Tomasz Napierala 	struct gmesh mesh;
9670f73f701SEdward Tomasz Napierala 	struct ggeom *gp;
9680f73f701SEdward Tomasz Napierala 	int error;
9690f73f701SEdward Tomasz Napierala 
9700f73f701SEdward Tomasz Napierala 	error = geom_gettree(&mesh);
9710f73f701SEdward Tomasz Napierala 	if (error != 0)
9720f73f701SEdward Tomasz Napierala 		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
9730f73f701SEdward Tomasz Napierala 
9740f73f701SEdward Tomasz Napierala 	gp = find_geom_by_provider(&mesh, provider_name);
9750f73f701SEdward Tomasz Napierala 	if (gp == NULL)
9760f73f701SEdward Tomasz Napierala 		errx(EXIT_FAILURE, "Cannot find provider '%s'.", provider_name);
9770f73f701SEdward Tomasz Napierala 
9780f73f701SEdward Tomasz Napierala 	printf("Geom class: %s\n", gp->lg_class->lg_name);
9790f73f701SEdward Tomasz Napierala 	list_one_geom(gp);
9800f73f701SEdward Tomasz Napierala }
9810f73f701SEdward Tomasz Napierala 
9820f73f701SEdward Tomasz Napierala static void
983b70eccf3SPawel Jakub Dawidek std_help(struct gctl_req *req __unused, unsigned flags __unused)
984b70eccf3SPawel Jakub Dawidek {
985b70eccf3SPawel Jakub Dawidek 
986c979e206SPawel Jakub Dawidek 	usage();
987b70eccf3SPawel Jakub Dawidek }
988b70eccf3SPawel Jakub Dawidek 
98905c91076SPawel Jakub Dawidek static int
99005c91076SPawel Jakub Dawidek std_list_available(void)
99105c91076SPawel Jakub Dawidek {
99205c91076SPawel Jakub Dawidek 	struct gmesh mesh;
99305c91076SPawel Jakub Dawidek 	struct gclass *classp;
99405c91076SPawel Jakub Dawidek 	int error;
99505c91076SPawel Jakub Dawidek 
99605c91076SPawel Jakub Dawidek 	error = geom_gettree(&mesh);
9976fc60008SPawel Jakub Dawidek 	if (error != 0)
9986fc60008SPawel Jakub Dawidek 		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
99905c91076SPawel Jakub Dawidek 	classp = find_class(&mesh, gclass_name);
100005c91076SPawel Jakub Dawidek 	geom_deletetree(&mesh);
100105c91076SPawel Jakub Dawidek 	if (classp != NULL)
100205c91076SPawel Jakub Dawidek 		return (1);
100305c91076SPawel Jakub Dawidek 	return (0);
100405c91076SPawel Jakub Dawidek }
100505c91076SPawel Jakub Dawidek 
100605c91076SPawel Jakub Dawidek static void
100705c91076SPawel Jakub Dawidek std_list(struct gctl_req *req, unsigned flags __unused)
100805c91076SPawel Jakub Dawidek {
100905c91076SPawel Jakub Dawidek 	struct gmesh mesh;
101005c91076SPawel Jakub Dawidek 	struct gclass *classp;
10111d723f1dSPawel Jakub Dawidek 	struct ggeom *gp;
1012f13942a7SPawel Jakub Dawidek 	const char *name;
101383d165c1SAlexander Motin 	int all, error, i, nargs;
101405c91076SPawel Jakub Dawidek 
101505c91076SPawel Jakub Dawidek 	error = geom_gettree(&mesh);
10166fc60008SPawel Jakub Dawidek 	if (error != 0)
10176fc60008SPawel Jakub Dawidek 		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
101805c91076SPawel Jakub Dawidek 	classp = find_class(&mesh, gclass_name);
101905c91076SPawel Jakub Dawidek 	if (classp == NULL) {
102005c91076SPawel Jakub Dawidek 		geom_deletetree(&mesh);
1021112adef8SEdward Tomasz Napierala 		errx(EXIT_FAILURE, "Class '%s' not found.", gclass_name);
102205c91076SPawel Jakub Dawidek 	}
1023f13942a7SPawel Jakub Dawidek 	nargs = gctl_get_int(req, "nargs");
102483d165c1SAlexander Motin 	all = gctl_get_int(req, "all");
1025f13942a7SPawel Jakub Dawidek 	if (nargs > 0) {
1026f13942a7SPawel Jakub Dawidek 		for (i = 0; i < nargs; i++) {
1027f13942a7SPawel Jakub Dawidek 			name = gctl_get_ascii(req, "arg%d", i);
10281d723f1dSPawel Jakub Dawidek 			gp = find_geom(classp, name);
1029112adef8SEdward Tomasz Napierala 			if (gp == NULL) {
1030112adef8SEdward Tomasz Napierala 				errx(EXIT_FAILURE, "Class '%s' does not have "
1031112adef8SEdward Tomasz Napierala 				    "an instance named '%s'.",
1032112adef8SEdward Tomasz Napierala 				    gclass_name, name);
1033112adef8SEdward Tomasz Napierala 			}
103483d165c1SAlexander Motin 			list_one_geom(gp);
103505c91076SPawel Jakub Dawidek 		}
103605c91076SPawel Jakub Dawidek 	} else {
103705c91076SPawel Jakub Dawidek 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
103883d165c1SAlexander Motin 			if (LIST_EMPTY(&gp->lg_provider) && !all)
10395fd66a44SPawel Jakub Dawidek 				continue;
10402d76e2dbSPawel Jakub Dawidek 			list_one_geom(gp);
104105c91076SPawel Jakub Dawidek 		}
104205c91076SPawel Jakub Dawidek 	}
104305c91076SPawel Jakub Dawidek 	geom_deletetree(&mesh);
104405c91076SPawel Jakub Dawidek }
104505c91076SPawel Jakub Dawidek 
104605c91076SPawel Jakub Dawidek static int
104718ee8840SPawel Jakub Dawidek std_status_available(void)
104818ee8840SPawel Jakub Dawidek {
104918ee8840SPawel Jakub Dawidek 
105018ee8840SPawel Jakub Dawidek 	/* 'status' command is available when 'list' command is. */
105118ee8840SPawel Jakub Dawidek 	return (std_list_available());
105218ee8840SPawel Jakub Dawidek }
105318ee8840SPawel Jakub Dawidek 
105418ee8840SPawel Jakub Dawidek static void
1055da80913dSPawel Jakub Dawidek status_update_len(struct ggeom *gp, int *name_len, int *status_len)
105618ee8840SPawel Jakub Dawidek {
105718ee8840SPawel Jakub Dawidek 	struct gconfig *conf;
1058da80913dSPawel Jakub Dawidek 	int len;
105918ee8840SPawel Jakub Dawidek 
106018ee8840SPawel Jakub Dawidek 	assert(gp != NULL);
106118ee8840SPawel Jakub Dawidek 	assert(name_len != NULL);
106218ee8840SPawel Jakub Dawidek 	assert(status_len != NULL);
106318ee8840SPawel Jakub Dawidek 
106418ee8840SPawel Jakub Dawidek 	len = strlen(gp->lg_name);
106518ee8840SPawel Jakub Dawidek 	if (*name_len < len)
106618ee8840SPawel Jakub Dawidek 		*name_len = len;
106718ee8840SPawel Jakub Dawidek 	LIST_FOREACH(conf, &gp->lg_config, lg_config) {
106818ee8840SPawel Jakub Dawidek 		if (strcasecmp(conf->lg_name, "state") == 0) {
106918ee8840SPawel Jakub Dawidek 			len = strlen(conf->lg_val);
107018ee8840SPawel Jakub Dawidek 			if (*status_len < len)
107118ee8840SPawel Jakub Dawidek 				*status_len = len;
107218ee8840SPawel Jakub Dawidek 		}
107318ee8840SPawel Jakub Dawidek 	}
107418ee8840SPawel Jakub Dawidek }
107518ee8840SPawel Jakub Dawidek 
107683d165c1SAlexander Motin static void
107783d165c1SAlexander Motin status_update_len_prs(struct ggeom *gp, int *name_len, int *status_len)
107883d165c1SAlexander Motin {
107983d165c1SAlexander Motin 	struct gprovider *pp;
108083d165c1SAlexander Motin 	struct gconfig *conf;
108183d165c1SAlexander Motin 	int len, glen;
108283d165c1SAlexander Motin 
108383d165c1SAlexander Motin 	assert(gp != NULL);
108483d165c1SAlexander Motin 	assert(name_len != NULL);
108583d165c1SAlexander Motin 	assert(status_len != NULL);
108683d165c1SAlexander Motin 
108783d165c1SAlexander Motin 	glen = 0;
108883d165c1SAlexander Motin 	LIST_FOREACH(conf, &gp->lg_config, lg_config) {
108983d165c1SAlexander Motin 		if (strcasecmp(conf->lg_name, "state") == 0) {
109083d165c1SAlexander Motin 			glen = strlen(conf->lg_val);
109183d165c1SAlexander Motin 			break;
109283d165c1SAlexander Motin 		}
109383d165c1SAlexander Motin 	}
109483d165c1SAlexander Motin 	LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
109583d165c1SAlexander Motin 		len = strlen(pp->lg_name);
109683d165c1SAlexander Motin 		if (*name_len < len)
109783d165c1SAlexander Motin 			*name_len = len;
109883d165c1SAlexander Motin 		len = glen;
109983d165c1SAlexander Motin 		LIST_FOREACH(conf, &pp->lg_config, lg_config) {
110083d165c1SAlexander Motin 			if (strcasecmp(conf->lg_name, "state") == 0) {
110183d165c1SAlexander Motin 				len = strlen(conf->lg_val);
110283d165c1SAlexander Motin 				break;
110383d165c1SAlexander Motin 			}
110483d165c1SAlexander Motin 		}
110583d165c1SAlexander Motin 		if (*status_len < len)
110683d165c1SAlexander Motin 			*status_len = len;
110783d165c1SAlexander Motin 	}
110883d165c1SAlexander Motin }
110983d165c1SAlexander Motin 
1110ba6821f0SPawel Jakub Dawidek static char *
111118ee8840SPawel Jakub Dawidek status_one_consumer(struct gconsumer *cp)
111218ee8840SPawel Jakub Dawidek {
1113ba6821f0SPawel Jakub Dawidek 	static char buf[256];
111418ee8840SPawel Jakub Dawidek 	struct gprovider *pp;
111518ee8840SPawel Jakub Dawidek 	struct gconfig *conf;
111683d165c1SAlexander Motin 	const char *state, *syncr;
111718ee8840SPawel Jakub Dawidek 
111818ee8840SPawel Jakub Dawidek 	pp = cp->lg_provider;
111918ee8840SPawel Jakub Dawidek 	if (pp == NULL)
1120ba6821f0SPawel Jakub Dawidek 		return (NULL);
112183d165c1SAlexander Motin 	state = NULL;
112283d165c1SAlexander Motin 	syncr = NULL;
112318ee8840SPawel Jakub Dawidek 	LIST_FOREACH(conf, &cp->lg_config, lg_config) {
112483d165c1SAlexander Motin 		if (strcasecmp(conf->lg_name, "state") == 0)
112583d165c1SAlexander Motin 			state = conf->lg_val;
112618ee8840SPawel Jakub Dawidek 		if (strcasecmp(conf->lg_name, "synchronized") == 0)
112783d165c1SAlexander Motin 			syncr = conf->lg_val;
112818ee8840SPawel Jakub Dawidek 	}
112983d165c1SAlexander Motin 	if (state == NULL && syncr == NULL)
1130ba6821f0SPawel Jakub Dawidek 		snprintf(buf, sizeof(buf), "%s", pp->lg_name);
113183d165c1SAlexander Motin 	else if (state != NULL && syncr != NULL) {
113283d165c1SAlexander Motin 		snprintf(buf, sizeof(buf), "%s (%s, %s)", pp->lg_name,
113383d165c1SAlexander Motin 		    state, syncr);
113483d165c1SAlexander Motin 	} else {
1135ba6821f0SPawel Jakub Dawidek 		snprintf(buf, sizeof(buf), "%s (%s)", pp->lg_name,
113683d165c1SAlexander Motin 		    state ? state : syncr);
1137ba6821f0SPawel Jakub Dawidek 	}
1138ba6821f0SPawel Jakub Dawidek 	return (buf);
113918ee8840SPawel Jakub Dawidek }
114018ee8840SPawel Jakub Dawidek 
114118ee8840SPawel Jakub Dawidek static void
1142ba6821f0SPawel Jakub Dawidek status_one_geom(struct ggeom *gp, int script, int name_len, int status_len)
114318ee8840SPawel Jakub Dawidek {
114418ee8840SPawel Jakub Dawidek 	struct gconsumer *cp;
114518ee8840SPawel Jakub Dawidek 	struct gconfig *conf;
1146ba6821f0SPawel Jakub Dawidek 	const char *name, *status, *component;
1147ba6821f0SPawel Jakub Dawidek 	int gotone;
114818ee8840SPawel Jakub Dawidek 
114918ee8840SPawel Jakub Dawidek 	name = gp->lg_name;
115083d165c1SAlexander Motin 	status = "N/A";
115118ee8840SPawel Jakub Dawidek 	LIST_FOREACH(conf, &gp->lg_config, lg_config) {
115283d165c1SAlexander Motin 		if (strcasecmp(conf->lg_name, "state") == 0) {
115383d165c1SAlexander Motin 			status = conf->lg_val;
115418ee8840SPawel Jakub Dawidek 			break;
115518ee8840SPawel Jakub Dawidek 		}
115683d165c1SAlexander Motin 	}
1157ba6821f0SPawel Jakub Dawidek 	gotone = 0;
115818ee8840SPawel Jakub Dawidek 	LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
1159ba6821f0SPawel Jakub Dawidek 		component = status_one_consumer(cp);
1160ba6821f0SPawel Jakub Dawidek 		if (component == NULL)
1161ba6821f0SPawel Jakub Dawidek 			continue;
1162ba6821f0SPawel Jakub Dawidek 		gotone = 1;
1163ba6821f0SPawel Jakub Dawidek 		printf("%*s  %*s  %s\n", name_len, name, status_len, status,
1164ba6821f0SPawel Jakub Dawidek 		    component);
1165ba6821f0SPawel Jakub Dawidek 		if (!script)
1166ba6821f0SPawel Jakub Dawidek 			name = status = "";
116718ee8840SPawel Jakub Dawidek 	}
1168ba6821f0SPawel Jakub Dawidek 	if (!gotone) {
1169ba6821f0SPawel Jakub Dawidek 		printf("%*s  %*s  %s\n", name_len, name, status_len, status,
1170ba6821f0SPawel Jakub Dawidek 		    "N/A");
1171ba6821f0SPawel Jakub Dawidek 	}
117218ee8840SPawel Jakub Dawidek }
117318ee8840SPawel Jakub Dawidek 
117418ee8840SPawel Jakub Dawidek static void
117583d165c1SAlexander Motin status_one_geom_prs(struct ggeom *gp, int script, int name_len, int status_len)
117683d165c1SAlexander Motin {
117783d165c1SAlexander Motin 	struct gprovider *pp;
117883d165c1SAlexander Motin 	struct gconsumer *cp;
117983d165c1SAlexander Motin 	struct gconfig *conf;
118083d165c1SAlexander Motin 	const char *name, *status, *component;
118183d165c1SAlexander Motin 	int gotone;
118283d165c1SAlexander Motin 
118383d165c1SAlexander Motin 	LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
118483d165c1SAlexander Motin 		name = pp->lg_name;
118583d165c1SAlexander Motin 		status = "N/A";
118683d165c1SAlexander Motin 		LIST_FOREACH(conf, &gp->lg_config, lg_config) {
118783d165c1SAlexander Motin 			if (strcasecmp(conf->lg_name, "state") == 0) {
118883d165c1SAlexander Motin 				status = conf->lg_val;
118983d165c1SAlexander Motin 				break;
119083d165c1SAlexander Motin 			}
119183d165c1SAlexander Motin 		}
119283d165c1SAlexander Motin 		LIST_FOREACH(conf, &pp->lg_config, lg_config) {
119383d165c1SAlexander Motin 			if (strcasecmp(conf->lg_name, "state") == 0) {
119483d165c1SAlexander Motin 				status = conf->lg_val;
119583d165c1SAlexander Motin 				break;
119683d165c1SAlexander Motin 			}
119783d165c1SAlexander Motin 		}
119883d165c1SAlexander Motin 		gotone = 0;
119983d165c1SAlexander Motin 		LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
120083d165c1SAlexander Motin 			component = status_one_consumer(cp);
120183d165c1SAlexander Motin 			if (component == NULL)
120283d165c1SAlexander Motin 				continue;
120383d165c1SAlexander Motin 			gotone = 1;
120483d165c1SAlexander Motin 			printf("%*s  %*s  %s\n", name_len, name,
120583d165c1SAlexander Motin 			    status_len, status, component);
120683d165c1SAlexander Motin 			if (!script)
120783d165c1SAlexander Motin 				name = status = "";
120883d165c1SAlexander Motin 		}
120983d165c1SAlexander Motin 		if (!gotone) {
121083d165c1SAlexander Motin 			printf("%*s  %*s  %s\n", name_len, name,
121183d165c1SAlexander Motin 			    status_len, status, "N/A");
121283d165c1SAlexander Motin 		}
121383d165c1SAlexander Motin 	}
121483d165c1SAlexander Motin }
121583d165c1SAlexander Motin 
121683d165c1SAlexander Motin static void
121718ee8840SPawel Jakub Dawidek std_status(struct gctl_req *req, unsigned flags __unused)
121818ee8840SPawel Jakub Dawidek {
121918ee8840SPawel Jakub Dawidek 	struct gmesh mesh;
122018ee8840SPawel Jakub Dawidek 	struct gclass *classp;
122118ee8840SPawel Jakub Dawidek 	struct ggeom *gp;
1222f13942a7SPawel Jakub Dawidek 	const char *name;
1223da80913dSPawel Jakub Dawidek 	int name_len, status_len;
122483d165c1SAlexander Motin 	int all, error, geoms, i, n, nargs, script;
122518ee8840SPawel Jakub Dawidek 
122618ee8840SPawel Jakub Dawidek 	error = geom_gettree(&mesh);
12276fc60008SPawel Jakub Dawidek 	if (error != 0)
12286fc60008SPawel Jakub Dawidek 		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
122918ee8840SPawel Jakub Dawidek 	classp = find_class(&mesh, gclass_name);
12305d47cd0fSPawel Jakub Dawidek 	if (classp == NULL)
12315d47cd0fSPawel Jakub Dawidek 		errx(EXIT_FAILURE, "Class %s not found.", gclass_name);
1232f13942a7SPawel Jakub Dawidek 	nargs = gctl_get_int(req, "nargs");
123383d165c1SAlexander Motin 	all = gctl_get_int(req, "all");
123483d165c1SAlexander Motin 	geoms = gctl_get_int(req, "geoms");
1235f13942a7SPawel Jakub Dawidek 	script = gctl_get_int(req, "script");
123683d165c1SAlexander Motin 	if (script) {
123783d165c1SAlexander Motin 		name_len = 0;
123883d165c1SAlexander Motin 		status_len = 0;
123983d165c1SAlexander Motin 	} else {
124018ee8840SPawel Jakub Dawidek 		name_len = strlen("Name");
124118ee8840SPawel Jakub Dawidek 		status_len = strlen("Status");
124283d165c1SAlexander Motin 	}
1243f13942a7SPawel Jakub Dawidek 	if (nargs > 0) {
1244f13942a7SPawel Jakub Dawidek 		for (i = 0, n = 0; i < nargs; i++) {
1245f13942a7SPawel Jakub Dawidek 			name = gctl_get_ascii(req, "arg%d", i);
124618ee8840SPawel Jakub Dawidek 			gp = find_geom(classp, name);
124718ee8840SPawel Jakub Dawidek 			if (gp == NULL)
12485d47cd0fSPawel Jakub Dawidek 				errx(EXIT_FAILURE, "No such geom: %s.", name);
124983d165c1SAlexander Motin 			if (geoms) {
125083d165c1SAlexander Motin 				status_update_len(gp,
125183d165c1SAlexander Motin 				    &name_len, &status_len);
125283d165c1SAlexander Motin 			} else {
125383d165c1SAlexander Motin 				status_update_len_prs(gp,
125483d165c1SAlexander Motin 				    &name_len, &status_len);
125518ee8840SPawel Jakub Dawidek 			}
125683d165c1SAlexander Motin 			n++;
125718ee8840SPawel Jakub Dawidek 		}
125818ee8840SPawel Jakub Dawidek 		if (n == 0)
125918ee8840SPawel Jakub Dawidek 			goto end;
126018ee8840SPawel Jakub Dawidek 	} else {
1261f13942a7SPawel Jakub Dawidek 		n = 0;
126218ee8840SPawel Jakub Dawidek 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
126383d165c1SAlexander Motin 			if (LIST_EMPTY(&gp->lg_provider) && !all)
126418ee8840SPawel Jakub Dawidek 				continue;
126583d165c1SAlexander Motin 			if (geoms) {
126683d165c1SAlexander Motin 				status_update_len(gp,
126783d165c1SAlexander Motin 				    &name_len, &status_len);
126883d165c1SAlexander Motin 			} else {
126983d165c1SAlexander Motin 				status_update_len_prs(gp,
127083d165c1SAlexander Motin 				    &name_len, &status_len);
127183d165c1SAlexander Motin 			}
127218ee8840SPawel Jakub Dawidek 			n++;
127318ee8840SPawel Jakub Dawidek 		}
127418ee8840SPawel Jakub Dawidek 		if (n == 0)
127518ee8840SPawel Jakub Dawidek 			goto end;
127618ee8840SPawel Jakub Dawidek 	}
1277f13942a7SPawel Jakub Dawidek 	if (!script) {
1278da80913dSPawel Jakub Dawidek 		printf("%*s  %*s  %s\n", name_len, "Name", status_len, "Status",
127918ee8840SPawel Jakub Dawidek 		    "Components");
1280ba6821f0SPawel Jakub Dawidek 	}
1281f13942a7SPawel Jakub Dawidek 	if (nargs > 0) {
1282f13942a7SPawel Jakub Dawidek 		for (i = 0; i < nargs; i++) {
1283f13942a7SPawel Jakub Dawidek 			name = gctl_get_ascii(req, "arg%d", i);
128418ee8840SPawel Jakub Dawidek 			gp = find_geom(classp, name);
128583d165c1SAlexander Motin 			if (gp == NULL)
128683d165c1SAlexander Motin 				continue;
128783d165c1SAlexander Motin 			if (geoms) {
1288f13942a7SPawel Jakub Dawidek 				status_one_geom(gp, script, name_len,
1289ba6821f0SPawel Jakub Dawidek 				    status_len);
129083d165c1SAlexander Motin 			} else {
129183d165c1SAlexander Motin 				status_one_geom_prs(gp, script, name_len,
129283d165c1SAlexander Motin 				    status_len);
129318ee8840SPawel Jakub Dawidek 			}
129418ee8840SPawel Jakub Dawidek 		}
129518ee8840SPawel Jakub Dawidek 	} else {
129618ee8840SPawel Jakub Dawidek 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
129783d165c1SAlexander Motin 			if (LIST_EMPTY(&gp->lg_provider) && !all)
129818ee8840SPawel Jakub Dawidek 				continue;
129983d165c1SAlexander Motin 			if (geoms) {
130083d165c1SAlexander Motin 				status_one_geom(gp, script, name_len,
130183d165c1SAlexander Motin 				    status_len);
130283d165c1SAlexander Motin 			} else {
130383d165c1SAlexander Motin 				status_one_geom_prs(gp, script, name_len,
130483d165c1SAlexander Motin 				    status_len);
130583d165c1SAlexander Motin 			}
130618ee8840SPawel Jakub Dawidek 		}
130718ee8840SPawel Jakub Dawidek 	}
130818ee8840SPawel Jakub Dawidek end:
130918ee8840SPawel Jakub Dawidek 	geom_deletetree(&mesh);
131018ee8840SPawel Jakub Dawidek }
131118ee8840SPawel Jakub Dawidek 
131218ee8840SPawel Jakub Dawidek static int
131305c91076SPawel Jakub Dawidek std_load_available(void)
131405c91076SPawel Jakub Dawidek {
131505c91076SPawel Jakub Dawidek 	char name[MAXPATHLEN], paths[MAXPATHLEN * 8], *p;
131605c91076SPawel Jakub Dawidek 	struct stat sb;
131705c91076SPawel Jakub Dawidek 	size_t len;
131805c91076SPawel Jakub Dawidek 
131905c91076SPawel Jakub Dawidek 	snprintf(name, sizeof(name), "g_%s", class_name);
132005c91076SPawel Jakub Dawidek 	/*
132105c91076SPawel Jakub Dawidek 	 * If already in kernel, "load" command is not available.
132205c91076SPawel Jakub Dawidek 	 */
132305c91076SPawel Jakub Dawidek 	if (modfind(name) >= 0)
132405c91076SPawel Jakub Dawidek 		return (0);
132505c91076SPawel Jakub Dawidek 	bzero(paths, sizeof(paths));
132605c91076SPawel Jakub Dawidek 	len = sizeof(paths);
132705c91076SPawel Jakub Dawidek 	if (sysctlbyname("kern.module_path", paths, &len, NULL, 0) < 0)
132805c91076SPawel Jakub Dawidek 		err(EXIT_FAILURE, "sysctl(kern.module_path)");
132905c91076SPawel Jakub Dawidek 	for (p = strtok(paths, ";"); p != NULL; p = strtok(NULL, ";")) {
133005c91076SPawel Jakub Dawidek 		snprintf(name, sizeof(name), "%s/geom_%s.ko", p, class_name);
133105c91076SPawel Jakub Dawidek 		/*
133205c91076SPawel Jakub Dawidek 		 * If geom_<name>.ko file exists, "load" command is available.
133305c91076SPawel Jakub Dawidek 		 */
133405c91076SPawel Jakub Dawidek 		if (stat(name, &sb) == 0)
133505c91076SPawel Jakub Dawidek 			return (1);
133605c91076SPawel Jakub Dawidek 	}
133705c91076SPawel Jakub Dawidek 	return (0);
133805c91076SPawel Jakub Dawidek }
133905c91076SPawel Jakub Dawidek 
134005c91076SPawel Jakub Dawidek static void
134105c91076SPawel Jakub Dawidek std_load(struct gctl_req *req __unused, unsigned flags)
134205c91076SPawel Jakub Dawidek {
134305c91076SPawel Jakub Dawidek 
134405c91076SPawel Jakub Dawidek 	/*
134505c91076SPawel Jakub Dawidek 	 * Do nothing special here, because of G_FLAG_LOADKLD flag,
134605c91076SPawel Jakub Dawidek 	 * module is already loaded.
134705c91076SPawel Jakub Dawidek 	 */
134805c91076SPawel Jakub Dawidek 	if ((flags & G_FLAG_VERBOSE) != 0)
134905c91076SPawel Jakub Dawidek 		printf("Module available.\n");
135005c91076SPawel Jakub Dawidek }
135105c91076SPawel Jakub Dawidek 
135205c91076SPawel Jakub Dawidek static int
135305c91076SPawel Jakub Dawidek std_unload_available(void)
135405c91076SPawel Jakub Dawidek {
135505c91076SPawel Jakub Dawidek 	char name[64];
135605c91076SPawel Jakub Dawidek 	int id;
135705c91076SPawel Jakub Dawidek 
135805c91076SPawel Jakub Dawidek 	snprintf(name, sizeof(name), "geom_%s", class_name);
135905c91076SPawel Jakub Dawidek 	id = kldfind(name);
136005c91076SPawel Jakub Dawidek 	if (id >= 0)
136105c91076SPawel Jakub Dawidek 		return (1);
136205c91076SPawel Jakub Dawidek 	return (0);
136305c91076SPawel Jakub Dawidek }
136405c91076SPawel Jakub Dawidek 
136505c91076SPawel Jakub Dawidek static void
136605c91076SPawel Jakub Dawidek std_unload(struct gctl_req *req, unsigned flags __unused)
136705c91076SPawel Jakub Dawidek {
136805c91076SPawel Jakub Dawidek 	char name[64];
136905c91076SPawel Jakub Dawidek 	int id;
137005c91076SPawel Jakub Dawidek 
137105c91076SPawel Jakub Dawidek 	snprintf(name, sizeof(name), "geom_%s", class_name);
137205c91076SPawel Jakub Dawidek 	id = kldfind(name);
137305c91076SPawel Jakub Dawidek 	if (id < 0) {
137405c91076SPawel Jakub Dawidek 		gctl_error(req, "Could not find module: %s.", strerror(errno));
137505c91076SPawel Jakub Dawidek 		return;
137605c91076SPawel Jakub Dawidek 	}
137705c91076SPawel Jakub Dawidek 	if (kldunload(id) < 0) {
137805c91076SPawel Jakub Dawidek 		gctl_error(req, "Could not unload module: %s.",
137905c91076SPawel Jakub Dawidek 		    strerror(errno));
138005c91076SPawel Jakub Dawidek 		return;
138105c91076SPawel Jakub Dawidek 	}
138205c91076SPawel Jakub Dawidek }
138305c91076SPawel Jakub Dawidek 
138405c91076SPawel Jakub Dawidek static int
138505c91076SPawel Jakub Dawidek std_available(const char *name)
138605c91076SPawel Jakub Dawidek {
138705c91076SPawel Jakub Dawidek 
1388b70eccf3SPawel Jakub Dawidek 	if (strcmp(name, "help") == 0)
1389b70eccf3SPawel Jakub Dawidek 		return (1);
1390b70eccf3SPawel Jakub Dawidek 	else if (strcmp(name, "list") == 0)
139105c91076SPawel Jakub Dawidek 		return (std_list_available());
139218ee8840SPawel Jakub Dawidek 	else if (strcmp(name, "status") == 0)
139318ee8840SPawel Jakub Dawidek 		return (std_status_available());
139405c91076SPawel Jakub Dawidek 	else if (strcmp(name, "load") == 0)
139505c91076SPawel Jakub Dawidek 		return (std_load_available());
139605c91076SPawel Jakub Dawidek 	else if (strcmp(name, "unload") == 0)
139705c91076SPawel Jakub Dawidek 		return (std_unload_available());
139805c91076SPawel Jakub Dawidek 	else
139905c91076SPawel Jakub Dawidek 		assert(!"Unknown standard command.");
140005c91076SPawel Jakub Dawidek 	return (0);
140105c91076SPawel Jakub Dawidek }
1402