xref: /freebsd/sbin/geom/core/geom.c (revision 1de7b4b805ddbf2429da511c053686ac4591ed89)
105c91076SPawel Jakub Dawidek /*-
2*1de7b4b8SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*1de7b4b8SPedro F. Giffuni  *
487070efbSPawel Jakub Dawidek  * Copyright (c) 2004-2009 Pawel Jakub Dawidek <pjd@FreeBSD.org>
505c91076SPawel Jakub Dawidek  * All rights reserved.
605c91076SPawel Jakub Dawidek  *
705c91076SPawel Jakub Dawidek  * Redistribution and use in source and binary forms, with or without
805c91076SPawel Jakub Dawidek  * modification, are permitted provided that the following conditions
905c91076SPawel Jakub Dawidek  * are met:
1005c91076SPawel Jakub Dawidek  * 1. Redistributions of source code must retain the above copyright
1105c91076SPawel Jakub Dawidek  *    notice, this list of conditions and the following disclaimer.
1205c91076SPawel Jakub Dawidek  * 2. Redistributions in binary form must reproduce the above copyright
1305c91076SPawel Jakub Dawidek  *    notice, this list of conditions and the following disclaimer in the
1405c91076SPawel Jakub Dawidek  *    documentation and/or other materials provided with the distribution.
1505c91076SPawel Jakub Dawidek  *
1605c91076SPawel Jakub Dawidek  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
1705c91076SPawel Jakub Dawidek  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1805c91076SPawel Jakub Dawidek  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1905c91076SPawel Jakub Dawidek  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
2005c91076SPawel Jakub Dawidek  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2105c91076SPawel Jakub Dawidek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2205c91076SPawel Jakub Dawidek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2305c91076SPawel Jakub Dawidek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2405c91076SPawel Jakub Dawidek  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2505c91076SPawel Jakub Dawidek  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2605c91076SPawel Jakub Dawidek  * SUCH DAMAGE.
2705c91076SPawel Jakub Dawidek  */
2805c91076SPawel Jakub Dawidek 
2905c91076SPawel Jakub Dawidek #include <sys/cdefs.h>
3005c91076SPawel Jakub Dawidek __FBSDID("$FreeBSD$");
3105c91076SPawel Jakub Dawidek 
3205c91076SPawel Jakub Dawidek #include <sys/param.h>
3305c91076SPawel Jakub Dawidek #include <sys/linker.h>
3405c91076SPawel Jakub Dawidek #include <sys/module.h>
3505c91076SPawel Jakub Dawidek #include <sys/stat.h>
3605c91076SPawel Jakub Dawidek #include <sys/sysctl.h>
3705c91076SPawel Jakub Dawidek #include <ctype.h>
3805c91076SPawel Jakub Dawidek #include <err.h>
3905c91076SPawel Jakub Dawidek #include <errno.h>
4005c91076SPawel Jakub Dawidek #include <stdio.h>
4105c91076SPawel Jakub Dawidek #include <stdlib.h>
4205c91076SPawel Jakub Dawidek #include <stdarg.h>
4305c91076SPawel Jakub Dawidek #include <stdint.h>
4405c91076SPawel Jakub Dawidek #include <string.h>
4505c91076SPawel Jakub Dawidek #include <unistd.h>
4605c91076SPawel Jakub Dawidek #include <libgen.h>
47af565b58SPawel Jakub Dawidek #include <libutil.h>
4805c91076SPawel Jakub Dawidek #include <inttypes.h>
4905c91076SPawel Jakub Dawidek #include <dlfcn.h>
5005c91076SPawel Jakub Dawidek #include <assert.h>
5105c91076SPawel Jakub Dawidek #include <libgeom.h>
5205c91076SPawel Jakub Dawidek #include <geom.h>
5305c91076SPawel Jakub Dawidek 
5405c91076SPawel Jakub Dawidek #include "misc/subr.h"
5505c91076SPawel Jakub Dawidek 
5614bf405bSMarcel Moolenaar #ifdef STATIC_GEOM_CLASSES
57a16f9b36SMarcel Moolenaar extern uint32_t gpart_version;
58a16f9b36SMarcel Moolenaar extern struct g_command gpart_class_commands[];
59bc69d66fSXin LI extern uint32_t glabel_version;
60bc69d66fSXin LI extern struct g_command glabel_class_commands[];
61a16f9b36SMarcel Moolenaar #endif
6205c91076SPawel Jakub Dawidek 
6305c91076SPawel Jakub Dawidek static char comm[MAXPATHLEN], *class_name = NULL, *gclass_name = NULL;
6405c91076SPawel Jakub Dawidek static uint32_t *version = NULL;
6505c91076SPawel Jakub Dawidek static int verbose = 0;
6605c91076SPawel Jakub Dawidek static struct g_command *class_commands = NULL;
6705c91076SPawel Jakub Dawidek 
68138cedfdSPawel Jakub Dawidek #define	GEOM_CLASS_CMDS	0x01
69138cedfdSPawel Jakub Dawidek #define	GEOM_STD_CMDS	0x02
70138cedfdSPawel Jakub Dawidek static struct g_command *find_command(const char *cmdstr, int flags);
7105c91076SPawel Jakub Dawidek static int std_available(const char *name);
7205c91076SPawel Jakub Dawidek 
73b70eccf3SPawel Jakub Dawidek static void std_help(struct gctl_req *req, unsigned flags);
7405c91076SPawel Jakub Dawidek static void std_list(struct gctl_req *req, unsigned flags);
7518ee8840SPawel Jakub Dawidek static void std_status(struct gctl_req *req, unsigned flags);
7605c91076SPawel Jakub Dawidek static void std_load(struct gctl_req *req, unsigned flags);
7705c91076SPawel Jakub Dawidek static void std_unload(struct gctl_req *req, unsigned flags);
7805c91076SPawel Jakub Dawidek 
79bf70beceSEd Schouten static struct g_command std_commands[] = {
80946e2f35SPawel Jakub Dawidek 	{ "help", 0, std_help, G_NULL_OPTS, NULL },
8183d165c1SAlexander Motin 	{ "list", 0, std_list,
8283d165c1SAlexander Motin 	    {
8383d165c1SAlexander Motin 		{ 'a', "all", NULL, G_TYPE_BOOL },
8483d165c1SAlexander Motin 		G_OPT_SENTINEL
8583d165c1SAlexander Motin 	    },
8683d165c1SAlexander Motin 	    "[-a] [name ...]"
87c979e206SPawel Jakub Dawidek 	},
88ba6821f0SPawel Jakub Dawidek 	{ "status", 0, std_status,
89ba6821f0SPawel Jakub Dawidek 	    {
9083d165c1SAlexander Motin 		{ 'a', "all", NULL, G_TYPE_BOOL },
9183d165c1SAlexander Motin 		{ 'g', "geoms", NULL, G_TYPE_BOOL },
926fc60008SPawel Jakub Dawidek 		{ 's', "script", NULL, G_TYPE_BOOL },
93ba6821f0SPawel Jakub Dawidek 		G_OPT_SENTINEL
94ba6821f0SPawel Jakub Dawidek 	    },
9583d165c1SAlexander Motin 	    "[-ags] [name ...]"
96c979e206SPawel Jakub Dawidek 	},
973cf55d3aSMarcel Moolenaar 	{ "load", G_FLAG_VERBOSE | G_FLAG_LOADKLD, std_load, G_NULL_OPTS,
98946e2f35SPawel Jakub Dawidek 	    NULL },
99946e2f35SPawel Jakub Dawidek 	{ "unload", G_FLAG_VERBOSE, std_unload, G_NULL_OPTS, NULL },
10005c91076SPawel Jakub Dawidek 	G_CMD_SENTINEL
10105c91076SPawel Jakub Dawidek };
10205c91076SPawel Jakub Dawidek 
10305c91076SPawel Jakub Dawidek static void
104c979e206SPawel Jakub Dawidek usage_command(struct g_command *cmd, const char *prefix)
10505c91076SPawel Jakub Dawidek {
10605c91076SPawel Jakub Dawidek 	struct g_option *opt;
107c979e206SPawel Jakub Dawidek 	unsigned i;
10805c91076SPawel Jakub Dawidek 
109c979e206SPawel Jakub Dawidek 	if (cmd->gc_usage != NULL) {
110963feac4SPawel Jakub Dawidek 		char *pos, *ptr, *sptr;
111963feac4SPawel Jakub Dawidek 
112963feac4SPawel Jakub Dawidek 		sptr = ptr = strdup(cmd->gc_usage);
113963feac4SPawel Jakub Dawidek 		while ((pos = strsep(&ptr, "\n")) != NULL) {
114963feac4SPawel Jakub Dawidek 			if (*pos == '\0')
115963feac4SPawel Jakub Dawidek 				continue;
116963feac4SPawel Jakub Dawidek 			fprintf(stderr, "%s %s %s %s\n", prefix, comm,
117963feac4SPawel Jakub Dawidek 			    cmd->gc_name, pos);
118963feac4SPawel Jakub Dawidek 		}
119963feac4SPawel Jakub Dawidek 		free(sptr);
120c979e206SPawel Jakub Dawidek 		return;
121c979e206SPawel Jakub Dawidek 	}
122963feac4SPawel Jakub Dawidek 
123963feac4SPawel Jakub Dawidek 	fprintf(stderr, "%s %s %s", prefix, comm, cmd->gc_name);
12405c91076SPawel Jakub Dawidek 	if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0)
12505c91076SPawel Jakub Dawidek 		fprintf(stderr, " [-v]");
126c979e206SPawel Jakub Dawidek 	for (i = 0; ; i++) {
127c979e206SPawel Jakub Dawidek 		opt = &cmd->gc_options[i];
12805c91076SPawel Jakub Dawidek 		if (opt->go_name == NULL)
12905c91076SPawel Jakub Dawidek 			break;
1306fc60008SPawel Jakub Dawidek 		if (opt->go_val != NULL || G_OPT_TYPE(opt) == G_TYPE_BOOL)
13105c91076SPawel Jakub Dawidek 			fprintf(stderr, " [");
13205c91076SPawel Jakub Dawidek 		else
13305c91076SPawel Jakub Dawidek 			fprintf(stderr, " ");
13405c91076SPawel Jakub Dawidek 		fprintf(stderr, "-%c", opt->go_char);
1356fc60008SPawel Jakub Dawidek 		if (G_OPT_TYPE(opt) != G_TYPE_BOOL)
13605c91076SPawel Jakub Dawidek 			fprintf(stderr, " %s", opt->go_name);
1376fc60008SPawel Jakub Dawidek 		if (opt->go_val != NULL || G_OPT_TYPE(opt) == G_TYPE_BOOL)
13805c91076SPawel Jakub Dawidek 			fprintf(stderr, "]");
13905c91076SPawel Jakub Dawidek 	}
140c979e206SPawel Jakub Dawidek 	fprintf(stderr, "\n");
14105c91076SPawel Jakub Dawidek }
14205c91076SPawel Jakub Dawidek 
14305c91076SPawel Jakub Dawidek static void
144c979e206SPawel Jakub Dawidek usage(void)
14505c91076SPawel Jakub Dawidek {
14605c91076SPawel Jakub Dawidek 
14705c91076SPawel Jakub Dawidek 	if (class_name == NULL) {
148b70eccf3SPawel Jakub Dawidek 		errx(EXIT_FAILURE, "usage: %s <class> <command> [options]",
149b70eccf3SPawel Jakub Dawidek 		    "geom");
15005c91076SPawel Jakub Dawidek 	} else {
151c979e206SPawel Jakub Dawidek 		struct g_command *cmd;
15205c91076SPawel Jakub Dawidek 		const char *prefix;
15305c91076SPawel Jakub Dawidek 		unsigned i;
15405c91076SPawel Jakub Dawidek 
15505c91076SPawel Jakub Dawidek 		prefix = "usage:";
156c979e206SPawel Jakub Dawidek 		if (class_commands != NULL) {
157c979e206SPawel Jakub Dawidek 			for (i = 0; ; i++) {
158c979e206SPawel Jakub Dawidek 				cmd = &class_commands[i];
159c979e206SPawel Jakub Dawidek 				if (cmd->gc_name == NULL)
160c979e206SPawel Jakub Dawidek 					break;
161c979e206SPawel Jakub Dawidek 				usage_command(cmd, prefix);
16205c91076SPawel Jakub Dawidek 				prefix = "      ";
16305c91076SPawel Jakub Dawidek 			}
164c979e206SPawel Jakub Dawidek 		}
16505c91076SPawel Jakub Dawidek 		for (i = 0; ; i++) {
16605c91076SPawel Jakub Dawidek 			cmd = &std_commands[i];
16705c91076SPawel Jakub Dawidek 			if (cmd->gc_name == NULL)
16805c91076SPawel Jakub Dawidek 				break;
169138cedfdSPawel Jakub Dawidek 			/*
170138cedfdSPawel Jakub Dawidek 			 * If class defines command, which has the same name as
171138cedfdSPawel Jakub Dawidek 			 * standard command, skip it, because it was already
172138cedfdSPawel Jakub Dawidek 			 * shown on usage().
173138cedfdSPawel Jakub Dawidek 			 */
174138cedfdSPawel Jakub Dawidek 			if (find_command(cmd->gc_name, GEOM_CLASS_CMDS) != NULL)
17505c91076SPawel Jakub Dawidek 				continue;
176c979e206SPawel Jakub Dawidek 			usage_command(cmd, prefix);
17705c91076SPawel Jakub Dawidek 			prefix = "      ";
17805c91076SPawel Jakub Dawidek 		}
17905c91076SPawel Jakub Dawidek 		exit(EXIT_FAILURE);
18005c91076SPawel Jakub Dawidek 	}
18105c91076SPawel Jakub Dawidek }
18205c91076SPawel Jakub Dawidek 
18305c91076SPawel Jakub Dawidek static void
18405c91076SPawel Jakub Dawidek load_module(void)
18505c91076SPawel Jakub Dawidek {
18605c91076SPawel Jakub Dawidek 	char name1[64], name2[64];
18705c91076SPawel Jakub Dawidek 
18805c91076SPawel Jakub Dawidek 	snprintf(name1, sizeof(name1), "g_%s", class_name);
18905c91076SPawel Jakub Dawidek 	snprintf(name2, sizeof(name2), "geom_%s", class_name);
19005c91076SPawel Jakub Dawidek 	if (modfind(name1) < 0) {
19105c91076SPawel Jakub Dawidek 		/* Not present in kernel, try loading it. */
19205c91076SPawel Jakub Dawidek 		if (kldload(name2) < 0 || modfind(name1) < 0) {
19305c91076SPawel Jakub Dawidek 			if (errno != EEXIST) {
19405c91076SPawel Jakub Dawidek 				errx(EXIT_FAILURE,
19505c91076SPawel Jakub Dawidek 				    "%s module not available!", name2);
19605c91076SPawel Jakub Dawidek 			}
19705c91076SPawel Jakub Dawidek 		}
19805c91076SPawel Jakub Dawidek 	}
19905c91076SPawel Jakub Dawidek }
20005c91076SPawel Jakub Dawidek 
20105c91076SPawel Jakub Dawidek static int
20205c91076SPawel Jakub Dawidek strlcatf(char *str, size_t size, const char *format, ...)
20305c91076SPawel Jakub Dawidek {
20405c91076SPawel Jakub Dawidek 	size_t len;
20505c91076SPawel Jakub Dawidek 	va_list ap;
20605c91076SPawel Jakub Dawidek 	int ret;
20705c91076SPawel Jakub Dawidek 
20805c91076SPawel Jakub Dawidek 	len = strlen(str);
20905c91076SPawel Jakub Dawidek 	str += len;
21005c91076SPawel Jakub Dawidek 	size -= len;
21105c91076SPawel Jakub Dawidek 
21205c91076SPawel Jakub Dawidek 	va_start(ap, format);
21305c91076SPawel Jakub Dawidek 	ret = vsnprintf(str, size, format, ap);
21405c91076SPawel Jakub Dawidek 	va_end(ap);
21505c91076SPawel Jakub Dawidek 
21605c91076SPawel Jakub Dawidek 	return (ret);
21705c91076SPawel Jakub Dawidek }
21805c91076SPawel Jakub Dawidek 
21905c91076SPawel Jakub Dawidek /*
22005c91076SPawel Jakub Dawidek  * Find given option in options available for given command.
22105c91076SPawel Jakub Dawidek  */
22205c91076SPawel Jakub Dawidek static struct g_option *
22305c91076SPawel Jakub Dawidek find_option(struct g_command *cmd, char ch)
22405c91076SPawel Jakub Dawidek {
22505c91076SPawel Jakub Dawidek 	struct g_option *opt;
22605c91076SPawel Jakub Dawidek 	unsigned i;
22705c91076SPawel Jakub Dawidek 
22805c91076SPawel Jakub Dawidek 	for (i = 0; ; i++) {
22905c91076SPawel Jakub Dawidek 		opt = &cmd->gc_options[i];
23005c91076SPawel Jakub Dawidek 		if (opt->go_name == NULL)
23105c91076SPawel Jakub Dawidek 			return (NULL);
23205c91076SPawel Jakub Dawidek 		if (opt->go_char == ch)
23305c91076SPawel Jakub Dawidek 			return (opt);
23405c91076SPawel Jakub Dawidek 	}
23505c91076SPawel Jakub Dawidek 	/* NOTREACHED */
23605c91076SPawel Jakub Dawidek 	return (NULL);
23705c91076SPawel Jakub Dawidek }
23805c91076SPawel Jakub Dawidek 
23905c91076SPawel Jakub Dawidek /*
24005c91076SPawel Jakub Dawidek  * Add given option to gctl_req.
24105c91076SPawel Jakub Dawidek  */
24205c91076SPawel Jakub Dawidek static void
24305c91076SPawel Jakub Dawidek set_option(struct gctl_req *req, struct g_option *opt, const char *val)
24405c91076SPawel Jakub Dawidek {
245315fcbf7SPawel Jakub Dawidek 	const char *optname;
24635efcc8bSDag-Erling Smørgrav 	uint64_t number;
247a478ea74SPawel Jakub Dawidek 	void *ptr;
24805c91076SPawel Jakub Dawidek 
249315fcbf7SPawel Jakub Dawidek 	if (G_OPT_ISMULTI(opt)) {
250315fcbf7SPawel Jakub Dawidek 		size_t optnamesize;
251315fcbf7SPawel Jakub Dawidek 
252315fcbf7SPawel Jakub Dawidek 		if (G_OPT_NUM(opt) == UCHAR_MAX)
253315fcbf7SPawel Jakub Dawidek 			errx(EXIT_FAILURE, "Too many -%c options.", opt->go_char);
254315fcbf7SPawel Jakub Dawidek 
255315fcbf7SPawel Jakub Dawidek 		/*
256315fcbf7SPawel Jakub Dawidek 		 * Base option name length plus 3 bytes for option number
257315fcbf7SPawel Jakub Dawidek 		 * (max. 255 options) plus 1 byte for terminating '\0'.
258315fcbf7SPawel Jakub Dawidek 		 */
259315fcbf7SPawel Jakub Dawidek 		optnamesize = strlen(opt->go_name) + 3 + 1;
260315fcbf7SPawel Jakub Dawidek 		ptr = malloc(optnamesize);
261315fcbf7SPawel Jakub Dawidek 		if (ptr == NULL)
262315fcbf7SPawel Jakub Dawidek 			errx(EXIT_FAILURE, "No memory.");
263315fcbf7SPawel Jakub Dawidek 		snprintf(ptr, optnamesize, "%s%u", opt->go_name, G_OPT_NUM(opt));
264315fcbf7SPawel Jakub Dawidek 		G_OPT_NUMINC(opt);
265315fcbf7SPawel Jakub Dawidek 		optname = ptr;
266315fcbf7SPawel Jakub Dawidek 	} else {
267315fcbf7SPawel Jakub Dawidek 		optname = opt->go_name;
268315fcbf7SPawel Jakub Dawidek 	}
269315fcbf7SPawel Jakub Dawidek 
270fa5383a2SPawel Jakub Dawidek 	if (G_OPT_TYPE(opt) == G_TYPE_NUMBER) {
271e84091cbSPawel Jakub Dawidek 		if (expand_number(val, &number) == -1) {
272f104beb7SPawel Jakub Dawidek 			err(EXIT_FAILURE, "Invalid value for '%c' argument",
27305c91076SPawel Jakub Dawidek 			    opt->go_char);
27405c91076SPawel Jakub Dawidek 		}
275a478ea74SPawel Jakub Dawidek 		ptr = malloc(sizeof(intmax_t));
276a478ea74SPawel Jakub Dawidek 		if (ptr == NULL)
277a478ea74SPawel Jakub Dawidek 			errx(EXIT_FAILURE, "No memory.");
278a478ea74SPawel Jakub Dawidek 		*(intmax_t *)ptr = number;
279a478ea74SPawel Jakub Dawidek 		opt->go_val = ptr;
280628ec6d3SPawel Jakub Dawidek 		gctl_ro_param(req, optname, sizeof(intmax_t), opt->go_val);
2816fc60008SPawel Jakub Dawidek 	} else if (G_OPT_TYPE(opt) == G_TYPE_STRING) {
282315fcbf7SPawel Jakub Dawidek 		gctl_ro_param(req, optname, -1, val);
2836fc60008SPawel Jakub Dawidek 	} else if (G_OPT_TYPE(opt) == G_TYPE_BOOL) {
284a478ea74SPawel Jakub Dawidek 		ptr = malloc(sizeof(int));
285a478ea74SPawel Jakub Dawidek 		if (ptr == NULL)
28605c91076SPawel Jakub Dawidek 			errx(EXIT_FAILURE, "No memory.");
287a478ea74SPawel Jakub Dawidek 		*(int *)ptr = *val - '0';
288a478ea74SPawel Jakub Dawidek 		opt->go_val = ptr;
289315fcbf7SPawel Jakub Dawidek 		gctl_ro_param(req, optname, sizeof(int), opt->go_val);
2906fc60008SPawel Jakub Dawidek 	} else {
2916fc60008SPawel Jakub Dawidek 		assert(!"Invalid type");
29205c91076SPawel Jakub Dawidek 	}
293315fcbf7SPawel Jakub Dawidek 
294315fcbf7SPawel Jakub Dawidek 	if (G_OPT_ISMULTI(opt))
295315fcbf7SPawel Jakub Dawidek 		free(__DECONST(char *, optname));
29605c91076SPawel Jakub Dawidek }
29705c91076SPawel Jakub Dawidek 
29805c91076SPawel Jakub Dawidek /*
29905c91076SPawel Jakub Dawidek  * 1. Add given argument by caller.
30005c91076SPawel Jakub Dawidek  * 2. Add default values of not given arguments.
30105c91076SPawel Jakub Dawidek  * 3. Add the rest of arguments.
30205c91076SPawel Jakub Dawidek  */
30305c91076SPawel Jakub Dawidek static void
30405c91076SPawel Jakub Dawidek parse_arguments(struct g_command *cmd, struct gctl_req *req, int *argc,
30505c91076SPawel Jakub Dawidek     char ***argv)
30605c91076SPawel Jakub Dawidek {
30705c91076SPawel Jakub Dawidek 	struct g_option *opt;
30805c91076SPawel Jakub Dawidek 	char opts[64];
30905c91076SPawel Jakub Dawidek 	unsigned i;
31005c91076SPawel Jakub Dawidek 	int ch;
31105c91076SPawel Jakub Dawidek 
31205c91076SPawel Jakub Dawidek 	*opts = '\0';
31305c91076SPawel Jakub Dawidek 	if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0)
31405c91076SPawel Jakub Dawidek 		strlcat(opts, "v", sizeof(opts));
31505c91076SPawel Jakub Dawidek 	for (i = 0; ; i++) {
31605c91076SPawel Jakub Dawidek 		opt = &cmd->gc_options[i];
31705c91076SPawel Jakub Dawidek 		if (opt->go_name == NULL)
31805c91076SPawel Jakub Dawidek 			break;
3196fc60008SPawel Jakub Dawidek 		assert(G_OPT_TYPE(opt) != 0);
320315fcbf7SPawel Jakub Dawidek 		assert((opt->go_type & ~(G_TYPE_MASK | G_TYPE_MULTI)) == 0);
321315fcbf7SPawel Jakub Dawidek 		/* Multiple bool arguments makes no sense. */
322315fcbf7SPawel Jakub Dawidek 		assert(G_OPT_TYPE(opt) != G_TYPE_BOOL ||
323315fcbf7SPawel Jakub Dawidek 		    (opt->go_type & G_TYPE_MULTI) == 0);
32405c91076SPawel Jakub Dawidek 		strlcatf(opts, sizeof(opts), "%c", opt->go_char);
3256fc60008SPawel Jakub Dawidek 		if (G_OPT_TYPE(opt) != G_TYPE_BOOL)
32605c91076SPawel Jakub Dawidek 			strlcat(opts, ":", sizeof(opts));
32705c91076SPawel Jakub Dawidek 	}
32805c91076SPawel Jakub Dawidek 
32905c91076SPawel Jakub Dawidek 	/*
33005c91076SPawel Jakub Dawidek 	 * Add specified arguments.
33105c91076SPawel Jakub Dawidek 	 */
33205c91076SPawel Jakub Dawidek 	while ((ch = getopt(*argc, *argv, opts)) != -1) {
33305c91076SPawel Jakub Dawidek 		/* Standard (not passed to kernel) options. */
33405c91076SPawel Jakub Dawidek 		switch (ch) {
33505c91076SPawel Jakub Dawidek 		case 'v':
33605c91076SPawel Jakub Dawidek 			verbose = 1;
33705c91076SPawel Jakub Dawidek 			continue;
33805c91076SPawel Jakub Dawidek 		}
33905c91076SPawel Jakub Dawidek 		/* Options passed to kernel. */
34005c91076SPawel Jakub Dawidek 		opt = find_option(cmd, ch);
34105c91076SPawel Jakub Dawidek 		if (opt == NULL)
342c979e206SPawel Jakub Dawidek 			usage();
343315fcbf7SPawel Jakub Dawidek 		if (!G_OPT_ISMULTI(opt) && G_OPT_ISDONE(opt)) {
3446fc60008SPawel Jakub Dawidek 			warnx("Option '%c' specified twice.", opt->go_char);
345c979e206SPawel Jakub Dawidek 			usage();
34605c91076SPawel Jakub Dawidek 		}
34705c91076SPawel Jakub Dawidek 		G_OPT_DONE(opt);
34805c91076SPawel Jakub Dawidek 
3496fc60008SPawel Jakub Dawidek 		if (G_OPT_TYPE(opt) == G_TYPE_BOOL)
35005c91076SPawel Jakub Dawidek 			set_option(req, opt, "1");
35105c91076SPawel Jakub Dawidek 		else
35205c91076SPawel Jakub Dawidek 			set_option(req, opt, optarg);
35305c91076SPawel Jakub Dawidek 	}
35405c91076SPawel Jakub Dawidek 	*argc -= optind;
35505c91076SPawel Jakub Dawidek 	*argv += optind;
35605c91076SPawel Jakub Dawidek 
35705c91076SPawel Jakub Dawidek 	/*
35805c91076SPawel Jakub Dawidek 	 * Add not specified arguments, but with default values.
35905c91076SPawel Jakub Dawidek 	 */
36005c91076SPawel Jakub Dawidek 	for (i = 0; ; i++) {
36105c91076SPawel Jakub Dawidek 		opt = &cmd->gc_options[i];
36205c91076SPawel Jakub Dawidek 		if (opt->go_name == NULL)
36305c91076SPawel Jakub Dawidek 			break;
36405c91076SPawel Jakub Dawidek 		if (G_OPT_ISDONE(opt))
36505c91076SPawel Jakub Dawidek 			continue;
36605c91076SPawel Jakub Dawidek 
3676fc60008SPawel Jakub Dawidek 		if (G_OPT_TYPE(opt) == G_TYPE_BOOL) {
36805c91076SPawel Jakub Dawidek 			assert(opt->go_val == NULL);
36905c91076SPawel Jakub Dawidek 			set_option(req, opt, "0");
37005c91076SPawel Jakub Dawidek 		} else {
37105c91076SPawel Jakub Dawidek 			if (opt->go_val == NULL) {
3726fc60008SPawel Jakub Dawidek 				warnx("Option '%c' not specified.",
37305c91076SPawel Jakub Dawidek 				    opt->go_char);
374c979e206SPawel Jakub Dawidek 				usage();
3757648b1e9SPawel Jakub Dawidek 			} else if (opt->go_val == G_VAL_OPTIONAL) {
3767648b1e9SPawel Jakub Dawidek 				/* add nothing. */
37705c91076SPawel Jakub Dawidek 			} else {
378946e2f35SPawel Jakub Dawidek 				set_option(req, opt, opt->go_val);
37905c91076SPawel Jakub Dawidek 			}
38005c91076SPawel Jakub Dawidek 		}
38105c91076SPawel Jakub Dawidek 	}
3823cf55d3aSMarcel Moolenaar 
38305c91076SPawel Jakub Dawidek 	/*
38405c91076SPawel Jakub Dawidek 	 * Add rest of given arguments.
38505c91076SPawel Jakub Dawidek 	 */
38605c91076SPawel Jakub Dawidek 	gctl_ro_param(req, "nargs", sizeof(int), argc);
38705c91076SPawel Jakub Dawidek 	for (i = 0; i < (unsigned)*argc; i++) {
38805c91076SPawel Jakub Dawidek 		char argname[16];
38905c91076SPawel Jakub Dawidek 
39005c91076SPawel Jakub Dawidek 		snprintf(argname, sizeof(argname), "arg%u", i);
39105c91076SPawel Jakub Dawidek 		gctl_ro_param(req, argname, -1, (*argv)[i]);
39205c91076SPawel Jakub Dawidek 	}
39305c91076SPawel Jakub Dawidek }
39405c91076SPawel Jakub Dawidek 
39505c91076SPawel Jakub Dawidek /*
39605c91076SPawel Jakub Dawidek  * Find given command in commands available for given class.
39705c91076SPawel Jakub Dawidek  */
39805c91076SPawel Jakub Dawidek static struct g_command *
399138cedfdSPawel Jakub Dawidek find_command(const char *cmdstr, int flags)
40005c91076SPawel Jakub Dawidek {
40105c91076SPawel Jakub Dawidek 	struct g_command *cmd;
40205c91076SPawel Jakub Dawidek 	unsigned i;
40305c91076SPawel Jakub Dawidek 
40405c91076SPawel Jakub Dawidek 	/*
40505c91076SPawel Jakub Dawidek 	 * First try to find command defined by loaded library.
40605c91076SPawel Jakub Dawidek 	 */
407138cedfdSPawel Jakub Dawidek 	if ((flags & GEOM_CLASS_CMDS) != 0 && class_commands != NULL) {
40805c91076SPawel Jakub Dawidek 		for (i = 0; ; i++) {
40905c91076SPawel Jakub Dawidek 			cmd = &class_commands[i];
41005c91076SPawel Jakub Dawidek 			if (cmd->gc_name == NULL)
41105c91076SPawel Jakub Dawidek 				break;
41205c91076SPawel Jakub Dawidek 			if (strcmp(cmd->gc_name, cmdstr) == 0)
41305c91076SPawel Jakub Dawidek 				return (cmd);
41405c91076SPawel Jakub Dawidek 		}
41505c91076SPawel Jakub Dawidek 	}
41605c91076SPawel Jakub Dawidek 	/*
41705c91076SPawel Jakub Dawidek 	 * Now try to find in standard commands.
41805c91076SPawel Jakub Dawidek 	 */
419138cedfdSPawel Jakub Dawidek 	if ((flags & GEOM_STD_CMDS) != 0) {
42005c91076SPawel Jakub Dawidek 		for (i = 0; ; i++) {
42105c91076SPawel Jakub Dawidek 			cmd = &std_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 		}
427138cedfdSPawel Jakub Dawidek 	}
42805c91076SPawel Jakub Dawidek 	return (NULL);
42905c91076SPawel Jakub Dawidek }
43005c91076SPawel Jakub Dawidek 
43105c91076SPawel Jakub Dawidek static unsigned
43205c91076SPawel Jakub Dawidek set_flags(struct g_command *cmd)
43305c91076SPawel Jakub Dawidek {
43405c91076SPawel Jakub Dawidek 	unsigned flags = 0;
43505c91076SPawel Jakub Dawidek 
43605c91076SPawel Jakub Dawidek 	if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0 && verbose)
43705c91076SPawel Jakub Dawidek 		flags |= G_FLAG_VERBOSE;
43805c91076SPawel Jakub Dawidek 
43905c91076SPawel Jakub Dawidek 	return (flags);
44005c91076SPawel Jakub Dawidek }
44105c91076SPawel Jakub Dawidek 
44205c91076SPawel Jakub Dawidek /*
44305c91076SPawel Jakub Dawidek  * Run command.
44405c91076SPawel Jakub Dawidek  */
44505c91076SPawel Jakub Dawidek static void
44605c91076SPawel Jakub Dawidek run_command(int argc, char *argv[])
44705c91076SPawel Jakub Dawidek {
44805c91076SPawel Jakub Dawidek 	struct g_command *cmd;
44905c91076SPawel Jakub Dawidek 	struct gctl_req *req;
45005c91076SPawel Jakub Dawidek 	const char *errstr;
45105c91076SPawel Jakub Dawidek 	char buf[4096];
45205c91076SPawel Jakub Dawidek 
453138cedfdSPawel Jakub Dawidek 	/* First try to find a command defined by a class. */
454138cedfdSPawel Jakub Dawidek 	cmd = find_command(argv[0], GEOM_CLASS_CMDS);
455138cedfdSPawel Jakub Dawidek 	if (cmd == NULL) {
456138cedfdSPawel Jakub Dawidek 		/* Now, try to find a standard command. */
457138cedfdSPawel Jakub Dawidek 		cmd = find_command(argv[0], GEOM_STD_CMDS);
45805c91076SPawel Jakub Dawidek 		if (cmd == NULL) {
4596fc60008SPawel Jakub Dawidek 			warnx("Unknown command: %s.", argv[0]);
460c979e206SPawel Jakub Dawidek 			usage();
46105c91076SPawel Jakub Dawidek 		}
462138cedfdSPawel Jakub Dawidek 		if (!std_available(cmd->gc_name)) {
4636fc60008SPawel Jakub Dawidek 			warnx("Command '%s' not available.", argv[0]);
464138cedfdSPawel Jakub Dawidek 			exit(EXIT_FAILURE);
465138cedfdSPawel Jakub Dawidek 		}
466138cedfdSPawel Jakub Dawidek 	}
46705c91076SPawel Jakub Dawidek 	if ((cmd->gc_flags & G_FLAG_LOADKLD) != 0)
46805c91076SPawel Jakub Dawidek 		load_module();
46905c91076SPawel Jakub Dawidek 
47005c91076SPawel Jakub Dawidek 	req = gctl_get_handle();
47105c91076SPawel Jakub Dawidek 	gctl_ro_param(req, "class", -1, gclass_name);
47205c91076SPawel Jakub Dawidek 	gctl_ro_param(req, "verb", -1, argv[0]);
47305c91076SPawel Jakub Dawidek 	if (version != NULL)
47405c91076SPawel Jakub Dawidek 		gctl_ro_param(req, "version", sizeof(*version), version);
47505c91076SPawel Jakub Dawidek 	parse_arguments(cmd, req, &argc, &argv);
47605c91076SPawel Jakub Dawidek 
4771bcfab7fSPawel Jakub Dawidek 	bzero(buf, sizeof(buf));
47805c91076SPawel Jakub Dawidek 	if (cmd->gc_func != NULL) {
47905c91076SPawel Jakub Dawidek 		unsigned flags;
48005c91076SPawel Jakub Dawidek 
48105c91076SPawel Jakub Dawidek 		flags = set_flags(cmd);
48205c91076SPawel Jakub Dawidek 		cmd->gc_func(req, flags);
48305c91076SPawel Jakub Dawidek 		errstr = req->error;
48405c91076SPawel Jakub Dawidek 	} else {
48505c91076SPawel Jakub Dawidek 		gctl_rw_param(req, "output", sizeof(buf), buf);
48605c91076SPawel Jakub Dawidek 		errstr = gctl_issue(req);
48705c91076SPawel Jakub Dawidek 	}
48853767efdSPawel Jakub Dawidek 	if (errstr != NULL && errstr[0] != '\0') {
4896fc60008SPawel Jakub Dawidek 		warnx("%s", errstr);
490f792f1d8SPawel Jakub Dawidek 		if (strncmp(errstr, "warning: ", strlen("warning: ")) != 0) {
49105c91076SPawel Jakub Dawidek 			gctl_free(req);
49205c91076SPawel Jakub Dawidek 			exit(EXIT_FAILURE);
49305c91076SPawel Jakub Dawidek 		}
494f792f1d8SPawel Jakub Dawidek 	}
4951bcfab7fSPawel Jakub Dawidek 	if (buf[0] != '\0')
49605c91076SPawel Jakub Dawidek 		printf("%s", buf);
49705c91076SPawel Jakub Dawidek 	gctl_free(req);
49805c91076SPawel Jakub Dawidek 	if (verbose)
49905c91076SPawel Jakub Dawidek 		printf("Done.\n");
50005c91076SPawel Jakub Dawidek 	exit(EXIT_SUCCESS);
50105c91076SPawel Jakub Dawidek }
50205c91076SPawel Jakub Dawidek 
50314bf405bSMarcel Moolenaar #ifndef STATIC_GEOM_CLASSES
5043bad06e9SPawel Jakub Dawidek static const char *
5053bad06e9SPawel Jakub Dawidek library_path(void)
5063bad06e9SPawel Jakub Dawidek {
5073bad06e9SPawel Jakub Dawidek 	const char *path;
5083bad06e9SPawel Jakub Dawidek 
5093bad06e9SPawel Jakub Dawidek 	path = getenv("GEOM_LIBRARY_PATH");
5103bad06e9SPawel Jakub Dawidek 	if (path == NULL)
5118494b738SDavid E. O'Brien 		path = GEOM_CLASS_DIR;
5123bad06e9SPawel Jakub Dawidek 	return (path);
5133bad06e9SPawel Jakub Dawidek }
5143bad06e9SPawel Jakub Dawidek 
51505c91076SPawel Jakub Dawidek static void
51605c91076SPawel Jakub Dawidek load_library(void)
51705c91076SPawel Jakub Dawidek {
5185d824386SUlf Lilleengen 	char *curpath, path[MAXPATHLEN], *tofree, *totalpath;
51905c91076SPawel Jakub Dawidek 	uint32_t *lib_version;
52005c91076SPawel Jakub Dawidek 	void *dlh;
5212591e96cSUlf Lilleengen 	int ret;
52205c91076SPawel Jakub Dawidek 
5232591e96cSUlf Lilleengen 	ret = 0;
5245d824386SUlf Lilleengen 	tofree = totalpath = strdup(library_path());
5252591e96cSUlf Lilleengen 	if (totalpath == NULL)
5262591e96cSUlf Lilleengen 		err(EXIT_FAILURE, "Not enough memory for library path");
5272591e96cSUlf Lilleengen 
5282591e96cSUlf Lilleengen 	if (strchr(totalpath, ':') != NULL)
5292591e96cSUlf Lilleengen 		curpath = strsep(&totalpath, ":");
5302591e96cSUlf Lilleengen 	else
5312591e96cSUlf Lilleengen 		curpath = totalpath;
5322591e96cSUlf Lilleengen 	/* Traverse the paths to find one that contains the library we want. */
5332591e96cSUlf Lilleengen 	while (curpath != NULL) {
5342591e96cSUlf Lilleengen 		snprintf(path, sizeof(path), "%s/geom_%s.so", curpath,
5353bad06e9SPawel Jakub Dawidek 		    class_name);
5362591e96cSUlf Lilleengen 		ret = access(path, F_OK);
5372591e96cSUlf Lilleengen 		if (ret == -1) {
538a73148d2SPawel Jakub Dawidek 			if (errno == ENOENT) {
53905c91076SPawel Jakub Dawidek 				/*
5402591e96cSUlf Lilleengen 				 * If we cannot find library, try the next
5412591e96cSUlf Lilleengen 				 * path.
54205c91076SPawel Jakub Dawidek 				 */
5432591e96cSUlf Lilleengen 				curpath = strsep(&totalpath, ":");
5442591e96cSUlf Lilleengen 				continue;
54505c91076SPawel Jakub Dawidek 			}
546a73148d2SPawel Jakub Dawidek 			err(EXIT_FAILURE, "Cannot access library");
547a73148d2SPawel Jakub Dawidek 		}
5482591e96cSUlf Lilleengen 		break;
5492591e96cSUlf Lilleengen 	}
5505d824386SUlf Lilleengen 	free(tofree);
5512591e96cSUlf Lilleengen 	/* No library was found, but standard commands can still be used */
5522591e96cSUlf Lilleengen 	if (ret == -1)
5532591e96cSUlf Lilleengen 		return;
554a73148d2SPawel Jakub Dawidek 	dlh = dlopen(path, RTLD_NOW);
555a73148d2SPawel Jakub Dawidek 	if (dlh == NULL)
556a73148d2SPawel Jakub Dawidek 		errx(EXIT_FAILURE, "Cannot open library: %s.", dlerror());
55705c91076SPawel Jakub Dawidek 	lib_version = dlsym(dlh, "lib_version");
55805c91076SPawel Jakub Dawidek 	if (lib_version == NULL) {
5596fc60008SPawel Jakub Dawidek 		warnx("Cannot find symbol %s: %s.", "lib_version", dlerror());
56005c91076SPawel Jakub Dawidek 		dlclose(dlh);
56105c91076SPawel Jakub Dawidek 		exit(EXIT_FAILURE);
56205c91076SPawel Jakub Dawidek 	}
56305c91076SPawel Jakub Dawidek 	if (*lib_version != G_LIB_VERSION) {
56405c91076SPawel Jakub Dawidek 		dlclose(dlh);
565f792f1d8SPawel Jakub Dawidek 		errx(EXIT_FAILURE, "%s and %s are not synchronized.",
566f792f1d8SPawel Jakub Dawidek 		    getprogname(), path);
56705c91076SPawel Jakub Dawidek 	}
56805c91076SPawel Jakub Dawidek 	version = dlsym(dlh, "version");
56905c91076SPawel Jakub Dawidek 	if (version == NULL) {
5706fc60008SPawel Jakub Dawidek 		warnx("Cannot find symbol %s: %s.", "version", dlerror());
57105c91076SPawel Jakub Dawidek 		dlclose(dlh);
57205c91076SPawel Jakub Dawidek 		exit(EXIT_FAILURE);
57305c91076SPawel Jakub Dawidek 	}
57405c91076SPawel Jakub Dawidek 	class_commands = dlsym(dlh, "class_commands");
57505c91076SPawel Jakub Dawidek 	if (class_commands == NULL) {
5766fc60008SPawel Jakub Dawidek 		warnx("Cannot find symbol %s: %s.", "class_commands",
5776fc60008SPawel Jakub Dawidek 		    dlerror());
57805c91076SPawel Jakub Dawidek 		dlclose(dlh);
57905c91076SPawel Jakub Dawidek 		exit(EXIT_FAILURE);
58005c91076SPawel Jakub Dawidek 	}
58105c91076SPawel Jakub Dawidek }
58214bf405bSMarcel Moolenaar #endif	/* !STATIC_GEOM_CLASSES */
58305c91076SPawel Jakub Dawidek 
58405c91076SPawel Jakub Dawidek /*
58505c91076SPawel Jakub Dawidek  * Class name should be all capital letters.
58605c91076SPawel Jakub Dawidek  */
58705c91076SPawel Jakub Dawidek static void
58805c91076SPawel Jakub Dawidek set_class_name(void)
58905c91076SPawel Jakub Dawidek {
59005c91076SPawel Jakub Dawidek 	char *s1, *s2;
59105c91076SPawel Jakub Dawidek 
592ee602fbbSPawel Jakub Dawidek 	s1 = class_name;
593ee602fbbSPawel Jakub Dawidek 	for (; *s1 != '\0'; s1++)
594ee602fbbSPawel Jakub Dawidek 		*s1 = tolower(*s1);
595cb94ab30SPawel Jakub Dawidek 	gclass_name = malloc(strlen(class_name) + 1);
59605c91076SPawel Jakub Dawidek 	if (gclass_name == NULL)
59705c91076SPawel Jakub Dawidek 		errx(EXIT_FAILURE, "No memory");
59805c91076SPawel Jakub Dawidek 	s1 = gclass_name;
59905c91076SPawel Jakub Dawidek 	s2 = class_name;
60005c91076SPawel Jakub Dawidek 	for (; *s2 != '\0'; s2++)
60105c91076SPawel Jakub Dawidek 		*s1++ = toupper(*s2);
60205c91076SPawel Jakub Dawidek 	*s1 = '\0';
60305c91076SPawel Jakub Dawidek }
60405c91076SPawel Jakub Dawidek 
60505c91076SPawel Jakub Dawidek static void
60605c91076SPawel Jakub Dawidek get_class(int *argc, char ***argv)
60705c91076SPawel Jakub Dawidek {
60805c91076SPawel Jakub Dawidek 
60905c91076SPawel Jakub Dawidek 	snprintf(comm, sizeof(comm), "%s", basename((*argv)[0]));
61005c91076SPawel Jakub Dawidek 	if (strcmp(comm, "geom") == 0) {
61105c91076SPawel Jakub Dawidek 		if (*argc < 2)
612c979e206SPawel Jakub Dawidek 			usage();
613687b0015SPawel Jakub Dawidek 		else if (*argc == 2) {
614687b0015SPawel Jakub Dawidek 			if (strcmp((*argv)[1], "-h") == 0 ||
615687b0015SPawel Jakub Dawidek 			    strcmp((*argv)[1], "help") == 0) {
616c979e206SPawel Jakub Dawidek 				usage();
617687b0015SPawel Jakub Dawidek 			}
618687b0015SPawel Jakub Dawidek 		}
61905c91076SPawel Jakub Dawidek 		strlcatf(comm, sizeof(comm), " %s", (*argv)[1]);
62005c91076SPawel Jakub Dawidek 		class_name = (*argv)[1];
62105c91076SPawel Jakub Dawidek 		*argc -= 2;
62205c91076SPawel Jakub Dawidek 		*argv += 2;
62305c91076SPawel Jakub Dawidek 	} else if (*comm == 'g') {
62405c91076SPawel Jakub Dawidek 		class_name = comm + 1;
62505c91076SPawel Jakub Dawidek 		*argc -= 1;
62605c91076SPawel Jakub Dawidek 		*argv += 1;
62705c91076SPawel Jakub Dawidek 	} else {
62805c91076SPawel Jakub Dawidek 		errx(EXIT_FAILURE, "Invalid utility name.");
62905c91076SPawel Jakub Dawidek 	}
630a16f9b36SMarcel Moolenaar 
63114bf405bSMarcel Moolenaar #ifndef STATIC_GEOM_CLASSES
63205c91076SPawel Jakub Dawidek 	load_library();
633a16f9b36SMarcel Moolenaar #else
634a16f9b36SMarcel Moolenaar 	if (!strcasecmp(class_name, "part")) {
635a16f9b36SMarcel Moolenaar 		version = &gpart_version;
636a16f9b36SMarcel Moolenaar 		class_commands = gpart_class_commands;
637bc69d66fSXin LI 	} else if (!strcasecmp(class_name, "label")) {
638bc69d66fSXin LI 		version = &glabel_version;
639bc69d66fSXin LI 		class_commands = glabel_class_commands;
640c6678483SAlexander Motin 	}
64114bf405bSMarcel Moolenaar #endif /* !STATIC_GEOM_CLASSES */
642a16f9b36SMarcel Moolenaar 
643a16f9b36SMarcel Moolenaar 	set_class_name();
644e2d52f14SMark Felder 
645e2d52f14SMark Felder 	/* If we can't load or list, it's not a class. */
646e2d52f14SMark Felder 	if (!std_available("load") && !std_available("list"))
647e2d52f14SMark Felder 		errx(EXIT_FAILURE, "Invalid class name.");
648e2d52f14SMark Felder 
64905c91076SPawel Jakub Dawidek 	if (*argc < 1)
650c979e206SPawel Jakub Dawidek 		usage();
65105c91076SPawel Jakub Dawidek }
65205c91076SPawel Jakub Dawidek 
65305c91076SPawel Jakub Dawidek int
65405c91076SPawel Jakub Dawidek main(int argc, char *argv[])
65505c91076SPawel Jakub Dawidek {
65605c91076SPawel Jakub Dawidek 
65705c91076SPawel Jakub Dawidek 	get_class(&argc, &argv);
65805c91076SPawel Jakub Dawidek 	run_command(argc, argv);
65905c91076SPawel Jakub Dawidek 	/* NOTREACHED */
66005c91076SPawel Jakub Dawidek 
66105c91076SPawel Jakub Dawidek 	exit(EXIT_FAILURE);
66205c91076SPawel Jakub Dawidek }
66305c91076SPawel Jakub Dawidek 
66405c91076SPawel Jakub Dawidek static struct gclass *
66505c91076SPawel Jakub Dawidek find_class(struct gmesh *mesh, const char *name)
66605c91076SPawel Jakub Dawidek {
66705c91076SPawel Jakub Dawidek 	struct gclass *classp;
66805c91076SPawel Jakub Dawidek 
66905c91076SPawel Jakub Dawidek 	LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
67005c91076SPawel Jakub Dawidek 		if (strcmp(classp->lg_name, name) == 0)
67105c91076SPawel Jakub Dawidek 			return (classp);
67205c91076SPawel Jakub Dawidek 	}
67305c91076SPawel Jakub Dawidek 	return (NULL);
67405c91076SPawel Jakub Dawidek }
67505c91076SPawel Jakub Dawidek 
6761d723f1dSPawel Jakub Dawidek static struct ggeom *
6771d723f1dSPawel Jakub Dawidek find_geom(struct gclass *classp, const char *name)
67805c91076SPawel Jakub Dawidek {
67905c91076SPawel Jakub Dawidek 	struct ggeom *gp;
68005c91076SPawel Jakub Dawidek 
68105c91076SPawel Jakub Dawidek 	LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
6821d723f1dSPawel Jakub Dawidek 		if (strcmp(gp->lg_name, name) == 0)
6831d723f1dSPawel Jakub Dawidek 			return (gp);
68405c91076SPawel Jakub Dawidek 	}
68505c91076SPawel Jakub Dawidek 	return (NULL);
68605c91076SPawel Jakub Dawidek }
68705c91076SPawel Jakub Dawidek 
68805c91076SPawel Jakub Dawidek static void
6892d76e2dbSPawel Jakub Dawidek list_one_provider(struct gprovider *pp, const char *prefix)
69005c91076SPawel Jakub Dawidek {
69105c91076SPawel Jakub Dawidek 	struct gconfig *conf;
692af565b58SPawel Jakub Dawidek 	char buf[5];
69305c91076SPawel Jakub Dawidek 
6941d723f1dSPawel Jakub Dawidek 	printf("Name: %s\n", pp->lg_name);
695af565b58SPawel Jakub Dawidek 	humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "",
696af565b58SPawel Jakub Dawidek 	    HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
6971d723f1dSPawel Jakub Dawidek 	printf("%sMediasize: %jd (%s)\n", prefix, (intmax_t)pp->lg_mediasize,
6981d723f1dSPawel Jakub Dawidek 	    buf);
6991d723f1dSPawel Jakub Dawidek 	printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize);
700e192c6e8SXin LI 	if (pp->lg_stripesize > 0 || pp->lg_stripeoffset > 0) {
70135daa28fSXin LI 		printf("%sStripesize: %ju\n", prefix, pp->lg_stripesize);
70235daa28fSXin LI 		printf("%sStripeoffset: %ju\n", prefix, pp->lg_stripeoffset);
70335daa28fSXin LI 	}
7041d723f1dSPawel Jakub Dawidek 	printf("%sMode: %s\n", prefix, pp->lg_mode);
70505c91076SPawel Jakub Dawidek 	LIST_FOREACH(conf, &pp->lg_config, lg_config) {
7061d723f1dSPawel Jakub Dawidek 		printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val);
7071d723f1dSPawel Jakub Dawidek 	}
7081d723f1dSPawel Jakub Dawidek }
7091d723f1dSPawel Jakub Dawidek 
7101d723f1dSPawel Jakub Dawidek static void
7112d76e2dbSPawel Jakub Dawidek list_one_consumer(struct gconsumer *cp, const char *prefix)
7121d723f1dSPawel Jakub Dawidek {
7131d723f1dSPawel Jakub Dawidek 	struct gprovider *pp;
7141d723f1dSPawel Jakub Dawidek 	struct gconfig *conf;
7151d723f1dSPawel Jakub Dawidek 
7161d723f1dSPawel Jakub Dawidek 	pp = cp->lg_provider;
7171d723f1dSPawel Jakub Dawidek 	if (pp == NULL)
7181d723f1dSPawel Jakub Dawidek 		printf("[no provider]\n");
7191d723f1dSPawel Jakub Dawidek 	else {
7201d723f1dSPawel Jakub Dawidek 		char buf[5];
7211d723f1dSPawel Jakub Dawidek 
7221d723f1dSPawel Jakub Dawidek 		printf("Name: %s\n", pp->lg_name);
7231d723f1dSPawel Jakub Dawidek 		humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "",
7241d723f1dSPawel Jakub Dawidek 		    HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
7251d723f1dSPawel Jakub Dawidek 		printf("%sMediasize: %jd (%s)\n", prefix,
7261d723f1dSPawel Jakub Dawidek 		    (intmax_t)pp->lg_mediasize, buf);
7271d723f1dSPawel Jakub Dawidek 		printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize);
72800b236aaSXin LI 		if (pp->lg_stripesize > 0 || pp->lg_stripeoffset > 0) {
72935daa28fSXin LI 			printf("%sStripesize: %ju\n", prefix, pp->lg_stripesize);
73035daa28fSXin LI 			printf("%sStripeoffset: %ju\n", prefix, pp->lg_stripeoffset);
73135daa28fSXin LI 		}
7321d723f1dSPawel Jakub Dawidek 		printf("%sMode: %s\n", prefix, cp->lg_mode);
7331d723f1dSPawel Jakub Dawidek 	}
7341d723f1dSPawel Jakub Dawidek 	LIST_FOREACH(conf, &cp->lg_config, lg_config) {
7351d723f1dSPawel Jakub Dawidek 		printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val);
7361d723f1dSPawel Jakub Dawidek 	}
7371d723f1dSPawel Jakub Dawidek }
7381d723f1dSPawel Jakub Dawidek 
7391d723f1dSPawel Jakub Dawidek static void
7402d76e2dbSPawel Jakub Dawidek list_one_geom(struct ggeom *gp)
7411d723f1dSPawel Jakub Dawidek {
7421d723f1dSPawel Jakub Dawidek 	struct gprovider *pp;
7431d723f1dSPawel Jakub Dawidek 	struct gconsumer *cp;
7441d723f1dSPawel Jakub Dawidek 	struct gconfig *conf;
7451d723f1dSPawel Jakub Dawidek 	unsigned n;
7461d723f1dSPawel Jakub Dawidek 
7471d723f1dSPawel Jakub Dawidek 	printf("Geom name: %s\n", gp->lg_name);
7481d723f1dSPawel Jakub Dawidek 	LIST_FOREACH(conf, &gp->lg_config, lg_config) {
7491d723f1dSPawel Jakub Dawidek 		printf("%s: %s\n", conf->lg_name, conf->lg_val);
7501d723f1dSPawel Jakub Dawidek 	}
7511d723f1dSPawel Jakub Dawidek 	if (!LIST_EMPTY(&gp->lg_provider)) {
7521d723f1dSPawel Jakub Dawidek 		printf("Providers:\n");
7531d723f1dSPawel Jakub Dawidek 		n = 1;
7541d723f1dSPawel Jakub Dawidek 		LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
7551d723f1dSPawel Jakub Dawidek 			printf("%u. ", n++);
7562d76e2dbSPawel Jakub Dawidek 			list_one_provider(pp, "   ");
7571d723f1dSPawel Jakub Dawidek 		}
7581d723f1dSPawel Jakub Dawidek 	}
7591d723f1dSPawel Jakub Dawidek 	if (!LIST_EMPTY(&gp->lg_consumer)) {
7601d723f1dSPawel Jakub Dawidek 		printf("Consumers:\n");
7611d723f1dSPawel Jakub Dawidek 		n = 1;
7621d723f1dSPawel Jakub Dawidek 		LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
7631d723f1dSPawel Jakub Dawidek 			printf("%u. ", n++);
7642d76e2dbSPawel Jakub Dawidek 			list_one_consumer(cp, "   ");
7651d723f1dSPawel Jakub Dawidek 		}
76605c91076SPawel Jakub Dawidek 	}
76705c91076SPawel Jakub Dawidek 	printf("\n");
76805c91076SPawel Jakub Dawidek }
76905c91076SPawel Jakub Dawidek 
770b70eccf3SPawel Jakub Dawidek static void
771b70eccf3SPawel Jakub Dawidek std_help(struct gctl_req *req __unused, unsigned flags __unused)
772b70eccf3SPawel Jakub Dawidek {
773b70eccf3SPawel Jakub Dawidek 
774c979e206SPawel Jakub Dawidek 	usage();
775b70eccf3SPawel Jakub Dawidek }
776b70eccf3SPawel Jakub Dawidek 
77705c91076SPawel Jakub Dawidek static int
77805c91076SPawel Jakub Dawidek std_list_available(void)
77905c91076SPawel Jakub Dawidek {
78005c91076SPawel Jakub Dawidek 	struct gmesh mesh;
78105c91076SPawel Jakub Dawidek 	struct gclass *classp;
78205c91076SPawel Jakub Dawidek 	int error;
78305c91076SPawel Jakub Dawidek 
78405c91076SPawel Jakub Dawidek 	error = geom_gettree(&mesh);
7856fc60008SPawel Jakub Dawidek 	if (error != 0)
7866fc60008SPawel Jakub Dawidek 		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
78705c91076SPawel Jakub Dawidek 	classp = find_class(&mesh, gclass_name);
78805c91076SPawel Jakub Dawidek 	geom_deletetree(&mesh);
78905c91076SPawel Jakub Dawidek 	if (classp != NULL)
79005c91076SPawel Jakub Dawidek 		return (1);
79105c91076SPawel Jakub Dawidek 	return (0);
79205c91076SPawel Jakub Dawidek }
79305c91076SPawel Jakub Dawidek 
79405c91076SPawel Jakub Dawidek static void
79505c91076SPawel Jakub Dawidek std_list(struct gctl_req *req, unsigned flags __unused)
79605c91076SPawel Jakub Dawidek {
79705c91076SPawel Jakub Dawidek 	struct gmesh mesh;
79805c91076SPawel Jakub Dawidek 	struct gclass *classp;
7991d723f1dSPawel Jakub Dawidek 	struct ggeom *gp;
800f13942a7SPawel Jakub Dawidek 	const char *name;
80183d165c1SAlexander Motin 	int all, error, i, nargs;
80205c91076SPawel Jakub Dawidek 
80305c91076SPawel Jakub Dawidek 	error = geom_gettree(&mesh);
8046fc60008SPawel Jakub Dawidek 	if (error != 0)
8056fc60008SPawel Jakub Dawidek 		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
80605c91076SPawel Jakub Dawidek 	classp = find_class(&mesh, gclass_name);
80705c91076SPawel Jakub Dawidek 	if (classp == NULL) {
80805c91076SPawel Jakub Dawidek 		geom_deletetree(&mesh);
8095d47cd0fSPawel Jakub Dawidek 		errx(EXIT_FAILURE, "Class %s not found.", gclass_name);
81005c91076SPawel Jakub Dawidek 	}
811f13942a7SPawel Jakub Dawidek 	nargs = gctl_get_int(req, "nargs");
81283d165c1SAlexander Motin 	all = gctl_get_int(req, "all");
813f13942a7SPawel Jakub Dawidek 	if (nargs > 0) {
814f13942a7SPawel Jakub Dawidek 		for (i = 0; i < nargs; i++) {
815f13942a7SPawel Jakub Dawidek 			name = gctl_get_ascii(req, "arg%d", i);
8161d723f1dSPawel Jakub Dawidek 			gp = find_geom(classp, name);
81783d165c1SAlexander Motin 			if (gp == NULL)
8185d47cd0fSPawel Jakub Dawidek 				errx(EXIT_FAILURE, "No such geom: %s.", name);
81983d165c1SAlexander Motin 			list_one_geom(gp);
82005c91076SPawel Jakub Dawidek 		}
82105c91076SPawel Jakub Dawidek 	} else {
82205c91076SPawel Jakub Dawidek 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
82383d165c1SAlexander Motin 			if (LIST_EMPTY(&gp->lg_provider) && !all)
8245fd66a44SPawel Jakub Dawidek 				continue;
8252d76e2dbSPawel Jakub Dawidek 			list_one_geom(gp);
82605c91076SPawel Jakub Dawidek 		}
82705c91076SPawel Jakub Dawidek 	}
82805c91076SPawel Jakub Dawidek 	geom_deletetree(&mesh);
82905c91076SPawel Jakub Dawidek }
83005c91076SPawel Jakub Dawidek 
83105c91076SPawel Jakub Dawidek static int
83218ee8840SPawel Jakub Dawidek std_status_available(void)
83318ee8840SPawel Jakub Dawidek {
83418ee8840SPawel Jakub Dawidek 
83518ee8840SPawel Jakub Dawidek 	/* 'status' command is available when 'list' command is. */
83618ee8840SPawel Jakub Dawidek 	return (std_list_available());
83718ee8840SPawel Jakub Dawidek }
83818ee8840SPawel Jakub Dawidek 
83918ee8840SPawel Jakub Dawidek static void
840da80913dSPawel Jakub Dawidek status_update_len(struct ggeom *gp, int *name_len, int *status_len)
84118ee8840SPawel Jakub Dawidek {
84218ee8840SPawel Jakub Dawidek 	struct gconfig *conf;
843da80913dSPawel Jakub Dawidek 	int len;
84418ee8840SPawel Jakub Dawidek 
84518ee8840SPawel Jakub Dawidek 	assert(gp != NULL);
84618ee8840SPawel Jakub Dawidek 	assert(name_len != NULL);
84718ee8840SPawel Jakub Dawidek 	assert(status_len != NULL);
84818ee8840SPawel Jakub Dawidek 
84918ee8840SPawel Jakub Dawidek 	len = strlen(gp->lg_name);
85018ee8840SPawel Jakub Dawidek 	if (*name_len < len)
85118ee8840SPawel Jakub Dawidek 		*name_len = len;
85218ee8840SPawel Jakub Dawidek 	LIST_FOREACH(conf, &gp->lg_config, lg_config) {
85318ee8840SPawel Jakub Dawidek 		if (strcasecmp(conf->lg_name, "state") == 0) {
85418ee8840SPawel Jakub Dawidek 			len = strlen(conf->lg_val);
85518ee8840SPawel Jakub Dawidek 			if (*status_len < len)
85618ee8840SPawel Jakub Dawidek 				*status_len = len;
85718ee8840SPawel Jakub Dawidek 		}
85818ee8840SPawel Jakub Dawidek 	}
85918ee8840SPawel Jakub Dawidek }
86018ee8840SPawel Jakub Dawidek 
86183d165c1SAlexander Motin static void
86283d165c1SAlexander Motin status_update_len_prs(struct ggeom *gp, int *name_len, int *status_len)
86383d165c1SAlexander Motin {
86483d165c1SAlexander Motin 	struct gprovider *pp;
86583d165c1SAlexander Motin 	struct gconfig *conf;
86683d165c1SAlexander Motin 	int len, glen;
86783d165c1SAlexander Motin 
86883d165c1SAlexander Motin 	assert(gp != NULL);
86983d165c1SAlexander Motin 	assert(name_len != NULL);
87083d165c1SAlexander Motin 	assert(status_len != NULL);
87183d165c1SAlexander Motin 
87283d165c1SAlexander Motin 	glen = 0;
87383d165c1SAlexander Motin 	LIST_FOREACH(conf, &gp->lg_config, lg_config) {
87483d165c1SAlexander Motin 		if (strcasecmp(conf->lg_name, "state") == 0) {
87583d165c1SAlexander Motin 			glen = strlen(conf->lg_val);
87683d165c1SAlexander Motin 			break;
87783d165c1SAlexander Motin 		}
87883d165c1SAlexander Motin 	}
87983d165c1SAlexander Motin 	LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
88083d165c1SAlexander Motin 		len = strlen(pp->lg_name);
88183d165c1SAlexander Motin 		if (*name_len < len)
88283d165c1SAlexander Motin 			*name_len = len;
88383d165c1SAlexander Motin 		len = glen;
88483d165c1SAlexander Motin 		LIST_FOREACH(conf, &pp->lg_config, lg_config) {
88583d165c1SAlexander Motin 			if (strcasecmp(conf->lg_name, "state") == 0) {
88683d165c1SAlexander Motin 				len = strlen(conf->lg_val);
88783d165c1SAlexander Motin 				break;
88883d165c1SAlexander Motin 			}
88983d165c1SAlexander Motin 		}
89083d165c1SAlexander Motin 		if (*status_len < len)
89183d165c1SAlexander Motin 			*status_len = len;
89283d165c1SAlexander Motin 	}
89383d165c1SAlexander Motin }
89483d165c1SAlexander Motin 
895ba6821f0SPawel Jakub Dawidek static char *
89618ee8840SPawel Jakub Dawidek status_one_consumer(struct gconsumer *cp)
89718ee8840SPawel Jakub Dawidek {
898ba6821f0SPawel Jakub Dawidek 	static char buf[256];
89918ee8840SPawel Jakub Dawidek 	struct gprovider *pp;
90018ee8840SPawel Jakub Dawidek 	struct gconfig *conf;
90183d165c1SAlexander Motin 	const char *state, *syncr;
90218ee8840SPawel Jakub Dawidek 
90318ee8840SPawel Jakub Dawidek 	pp = cp->lg_provider;
90418ee8840SPawel Jakub Dawidek 	if (pp == NULL)
905ba6821f0SPawel Jakub Dawidek 		return (NULL);
90683d165c1SAlexander Motin 	state = NULL;
90783d165c1SAlexander Motin 	syncr = NULL;
90818ee8840SPawel Jakub Dawidek 	LIST_FOREACH(conf, &cp->lg_config, lg_config) {
90983d165c1SAlexander Motin 		if (strcasecmp(conf->lg_name, "state") == 0)
91083d165c1SAlexander Motin 			state = conf->lg_val;
91118ee8840SPawel Jakub Dawidek 		if (strcasecmp(conf->lg_name, "synchronized") == 0)
91283d165c1SAlexander Motin 			syncr = conf->lg_val;
91318ee8840SPawel Jakub Dawidek 	}
91483d165c1SAlexander Motin 	if (state == NULL && syncr == NULL)
915ba6821f0SPawel Jakub Dawidek 		snprintf(buf, sizeof(buf), "%s", pp->lg_name);
91683d165c1SAlexander Motin 	else if (state != NULL && syncr != NULL) {
91783d165c1SAlexander Motin 		snprintf(buf, sizeof(buf), "%s (%s, %s)", pp->lg_name,
91883d165c1SAlexander Motin 		    state, syncr);
91983d165c1SAlexander Motin 	} else {
920ba6821f0SPawel Jakub Dawidek 		snprintf(buf, sizeof(buf), "%s (%s)", pp->lg_name,
92183d165c1SAlexander Motin 		    state ? state : syncr);
922ba6821f0SPawel Jakub Dawidek 	}
923ba6821f0SPawel Jakub Dawidek 	return (buf);
92418ee8840SPawel Jakub Dawidek }
92518ee8840SPawel Jakub Dawidek 
92618ee8840SPawel Jakub Dawidek static void
927ba6821f0SPawel Jakub Dawidek status_one_geom(struct ggeom *gp, int script, int name_len, int status_len)
92818ee8840SPawel Jakub Dawidek {
92918ee8840SPawel Jakub Dawidek 	struct gconsumer *cp;
93018ee8840SPawel Jakub Dawidek 	struct gconfig *conf;
931ba6821f0SPawel Jakub Dawidek 	const char *name, *status, *component;
932ba6821f0SPawel Jakub Dawidek 	int gotone;
93318ee8840SPawel Jakub Dawidek 
93418ee8840SPawel Jakub Dawidek 	name = gp->lg_name;
93583d165c1SAlexander Motin 	status = "N/A";
93618ee8840SPawel Jakub Dawidek 	LIST_FOREACH(conf, &gp->lg_config, lg_config) {
93783d165c1SAlexander Motin 		if (strcasecmp(conf->lg_name, "state") == 0) {
93883d165c1SAlexander Motin 			status = conf->lg_val;
93918ee8840SPawel Jakub Dawidek 			break;
94018ee8840SPawel Jakub Dawidek 		}
94183d165c1SAlexander Motin 	}
942ba6821f0SPawel Jakub Dawidek 	gotone = 0;
94318ee8840SPawel Jakub Dawidek 	LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
944ba6821f0SPawel Jakub Dawidek 		component = status_one_consumer(cp);
945ba6821f0SPawel Jakub Dawidek 		if (component == NULL)
946ba6821f0SPawel Jakub Dawidek 			continue;
947ba6821f0SPawel Jakub Dawidek 		gotone = 1;
948ba6821f0SPawel Jakub Dawidek 		printf("%*s  %*s  %s\n", name_len, name, status_len, status,
949ba6821f0SPawel Jakub Dawidek 		    component);
950ba6821f0SPawel Jakub Dawidek 		if (!script)
951ba6821f0SPawel Jakub Dawidek 			name = status = "";
95218ee8840SPawel Jakub Dawidek 	}
953ba6821f0SPawel Jakub Dawidek 	if (!gotone) {
954ba6821f0SPawel Jakub Dawidek 		printf("%*s  %*s  %s\n", name_len, name, status_len, status,
955ba6821f0SPawel Jakub Dawidek 		    "N/A");
956ba6821f0SPawel Jakub Dawidek 	}
95718ee8840SPawel Jakub Dawidek }
95818ee8840SPawel Jakub Dawidek 
95918ee8840SPawel Jakub Dawidek static void
96083d165c1SAlexander Motin status_one_geom_prs(struct ggeom *gp, int script, int name_len, int status_len)
96183d165c1SAlexander Motin {
96283d165c1SAlexander Motin 	struct gprovider *pp;
96383d165c1SAlexander Motin 	struct gconsumer *cp;
96483d165c1SAlexander Motin 	struct gconfig *conf;
96583d165c1SAlexander Motin 	const char *name, *status, *component;
96683d165c1SAlexander Motin 	int gotone;
96783d165c1SAlexander Motin 
96883d165c1SAlexander Motin 	LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
96983d165c1SAlexander Motin 		name = pp->lg_name;
97083d165c1SAlexander Motin 		status = "N/A";
97183d165c1SAlexander Motin 		LIST_FOREACH(conf, &gp->lg_config, lg_config) {
97283d165c1SAlexander Motin 			if (strcasecmp(conf->lg_name, "state") == 0) {
97383d165c1SAlexander Motin 				status = conf->lg_val;
97483d165c1SAlexander Motin 				break;
97583d165c1SAlexander Motin 			}
97683d165c1SAlexander Motin 		}
97783d165c1SAlexander Motin 		LIST_FOREACH(conf, &pp->lg_config, lg_config) {
97883d165c1SAlexander Motin 			if (strcasecmp(conf->lg_name, "state") == 0) {
97983d165c1SAlexander Motin 				status = conf->lg_val;
98083d165c1SAlexander Motin 				break;
98183d165c1SAlexander Motin 			}
98283d165c1SAlexander Motin 		}
98383d165c1SAlexander Motin 		gotone = 0;
98483d165c1SAlexander Motin 		LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
98583d165c1SAlexander Motin 			component = status_one_consumer(cp);
98683d165c1SAlexander Motin 			if (component == NULL)
98783d165c1SAlexander Motin 				continue;
98883d165c1SAlexander Motin 			gotone = 1;
98983d165c1SAlexander Motin 			printf("%*s  %*s  %s\n", name_len, name,
99083d165c1SAlexander Motin 			    status_len, status, component);
99183d165c1SAlexander Motin 			if (!script)
99283d165c1SAlexander Motin 				name = status = "";
99383d165c1SAlexander Motin 		}
99483d165c1SAlexander Motin 		if (!gotone) {
99583d165c1SAlexander Motin 			printf("%*s  %*s  %s\n", name_len, name,
99683d165c1SAlexander Motin 			    status_len, status, "N/A");
99783d165c1SAlexander Motin 		}
99883d165c1SAlexander Motin 	}
99983d165c1SAlexander Motin }
100083d165c1SAlexander Motin 
100183d165c1SAlexander Motin static void
100218ee8840SPawel Jakub Dawidek std_status(struct gctl_req *req, unsigned flags __unused)
100318ee8840SPawel Jakub Dawidek {
100418ee8840SPawel Jakub Dawidek 	struct gmesh mesh;
100518ee8840SPawel Jakub Dawidek 	struct gclass *classp;
100618ee8840SPawel Jakub Dawidek 	struct ggeom *gp;
1007f13942a7SPawel Jakub Dawidek 	const char *name;
1008da80913dSPawel Jakub Dawidek 	int name_len, status_len;
100983d165c1SAlexander Motin 	int all, error, geoms, i, n, nargs, script;
101018ee8840SPawel Jakub Dawidek 
101118ee8840SPawel Jakub Dawidek 	error = geom_gettree(&mesh);
10126fc60008SPawel Jakub Dawidek 	if (error != 0)
10136fc60008SPawel Jakub Dawidek 		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
101418ee8840SPawel Jakub Dawidek 	classp = find_class(&mesh, gclass_name);
10155d47cd0fSPawel Jakub Dawidek 	if (classp == NULL)
10165d47cd0fSPawel Jakub Dawidek 		errx(EXIT_FAILURE, "Class %s not found.", gclass_name);
1017f13942a7SPawel Jakub Dawidek 	nargs = gctl_get_int(req, "nargs");
101883d165c1SAlexander Motin 	all = gctl_get_int(req, "all");
101983d165c1SAlexander Motin 	geoms = gctl_get_int(req, "geoms");
1020f13942a7SPawel Jakub Dawidek 	script = gctl_get_int(req, "script");
102183d165c1SAlexander Motin 	if (script) {
102283d165c1SAlexander Motin 		name_len = 0;
102383d165c1SAlexander Motin 		status_len = 0;
102483d165c1SAlexander Motin 	} else {
102518ee8840SPawel Jakub Dawidek 		name_len = strlen("Name");
102618ee8840SPawel Jakub Dawidek 		status_len = strlen("Status");
102783d165c1SAlexander Motin 	}
1028f13942a7SPawel Jakub Dawidek 	if (nargs > 0) {
1029f13942a7SPawel Jakub Dawidek 		for (i = 0, n = 0; i < nargs; i++) {
1030f13942a7SPawel Jakub Dawidek 			name = gctl_get_ascii(req, "arg%d", i);
103118ee8840SPawel Jakub Dawidek 			gp = find_geom(classp, name);
103218ee8840SPawel Jakub Dawidek 			if (gp == NULL)
10335d47cd0fSPawel Jakub Dawidek 				errx(EXIT_FAILURE, "No such geom: %s.", name);
103483d165c1SAlexander Motin 			if (geoms) {
103583d165c1SAlexander Motin 				status_update_len(gp,
103683d165c1SAlexander Motin 				    &name_len, &status_len);
103783d165c1SAlexander Motin 			} else {
103883d165c1SAlexander Motin 				status_update_len_prs(gp,
103983d165c1SAlexander Motin 				    &name_len, &status_len);
104018ee8840SPawel Jakub Dawidek 			}
104183d165c1SAlexander Motin 			n++;
104218ee8840SPawel Jakub Dawidek 		}
104318ee8840SPawel Jakub Dawidek 		if (n == 0)
104418ee8840SPawel Jakub Dawidek 			goto end;
104518ee8840SPawel Jakub Dawidek 	} else {
1046f13942a7SPawel Jakub Dawidek 		n = 0;
104718ee8840SPawel Jakub Dawidek 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
104883d165c1SAlexander Motin 			if (LIST_EMPTY(&gp->lg_provider) && !all)
104918ee8840SPawel Jakub Dawidek 				continue;
105083d165c1SAlexander Motin 			if (geoms) {
105183d165c1SAlexander Motin 				status_update_len(gp,
105283d165c1SAlexander Motin 				    &name_len, &status_len);
105383d165c1SAlexander Motin 			} else {
105483d165c1SAlexander Motin 				status_update_len_prs(gp,
105583d165c1SAlexander Motin 				    &name_len, &status_len);
105683d165c1SAlexander Motin 			}
105718ee8840SPawel Jakub Dawidek 			n++;
105818ee8840SPawel Jakub Dawidek 		}
105918ee8840SPawel Jakub Dawidek 		if (n == 0)
106018ee8840SPawel Jakub Dawidek 			goto end;
106118ee8840SPawel Jakub Dawidek 	}
1062f13942a7SPawel Jakub Dawidek 	if (!script) {
1063da80913dSPawel Jakub Dawidek 		printf("%*s  %*s  %s\n", name_len, "Name", status_len, "Status",
106418ee8840SPawel Jakub Dawidek 		    "Components");
1065ba6821f0SPawel Jakub Dawidek 	}
1066f13942a7SPawel Jakub Dawidek 	if (nargs > 0) {
1067f13942a7SPawel Jakub Dawidek 		for (i = 0; i < nargs; i++) {
1068f13942a7SPawel Jakub Dawidek 			name = gctl_get_ascii(req, "arg%d", i);
106918ee8840SPawel Jakub Dawidek 			gp = find_geom(classp, name);
107083d165c1SAlexander Motin 			if (gp == NULL)
107183d165c1SAlexander Motin 				continue;
107283d165c1SAlexander Motin 			if (geoms) {
1073f13942a7SPawel Jakub Dawidek 				status_one_geom(gp, script, name_len,
1074ba6821f0SPawel Jakub Dawidek 				    status_len);
107583d165c1SAlexander Motin 			} else {
107683d165c1SAlexander Motin 				status_one_geom_prs(gp, script, name_len,
107783d165c1SAlexander Motin 				    status_len);
107818ee8840SPawel Jakub Dawidek 			}
107918ee8840SPawel Jakub Dawidek 		}
108018ee8840SPawel Jakub Dawidek 	} else {
108118ee8840SPawel Jakub Dawidek 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
108283d165c1SAlexander Motin 			if (LIST_EMPTY(&gp->lg_provider) && !all)
108318ee8840SPawel Jakub Dawidek 				continue;
108483d165c1SAlexander Motin 			if (geoms) {
108583d165c1SAlexander Motin 				status_one_geom(gp, script, name_len,
108683d165c1SAlexander Motin 				    status_len);
108783d165c1SAlexander Motin 			} else {
108883d165c1SAlexander Motin 				status_one_geom_prs(gp, script, name_len,
108983d165c1SAlexander Motin 				    status_len);
109083d165c1SAlexander Motin 			}
109118ee8840SPawel Jakub Dawidek 		}
109218ee8840SPawel Jakub Dawidek 	}
109318ee8840SPawel Jakub Dawidek end:
109418ee8840SPawel Jakub Dawidek 	geom_deletetree(&mesh);
109518ee8840SPawel Jakub Dawidek }
109618ee8840SPawel Jakub Dawidek 
109718ee8840SPawel Jakub Dawidek static int
109805c91076SPawel Jakub Dawidek std_load_available(void)
109905c91076SPawel Jakub Dawidek {
110005c91076SPawel Jakub Dawidek 	char name[MAXPATHLEN], paths[MAXPATHLEN * 8], *p;
110105c91076SPawel Jakub Dawidek 	struct stat sb;
110205c91076SPawel Jakub Dawidek 	size_t len;
110305c91076SPawel Jakub Dawidek 
110405c91076SPawel Jakub Dawidek 	snprintf(name, sizeof(name), "g_%s", class_name);
110505c91076SPawel Jakub Dawidek 	/*
110605c91076SPawel Jakub Dawidek 	 * If already in kernel, "load" command is not available.
110705c91076SPawel Jakub Dawidek 	 */
110805c91076SPawel Jakub Dawidek 	if (modfind(name) >= 0)
110905c91076SPawel Jakub Dawidek 		return (0);
111005c91076SPawel Jakub Dawidek 	bzero(paths, sizeof(paths));
111105c91076SPawel Jakub Dawidek 	len = sizeof(paths);
111205c91076SPawel Jakub Dawidek 	if (sysctlbyname("kern.module_path", paths, &len, NULL, 0) < 0)
111305c91076SPawel Jakub Dawidek 		err(EXIT_FAILURE, "sysctl(kern.module_path)");
111405c91076SPawel Jakub Dawidek 	for (p = strtok(paths, ";"); p != NULL; p = strtok(NULL, ";")) {
111505c91076SPawel Jakub Dawidek 		snprintf(name, sizeof(name), "%s/geom_%s.ko", p, class_name);
111605c91076SPawel Jakub Dawidek 		/*
111705c91076SPawel Jakub Dawidek 		 * If geom_<name>.ko file exists, "load" command is available.
111805c91076SPawel Jakub Dawidek 		 */
111905c91076SPawel Jakub Dawidek 		if (stat(name, &sb) == 0)
112005c91076SPawel Jakub Dawidek 			return (1);
112105c91076SPawel Jakub Dawidek 	}
112205c91076SPawel Jakub Dawidek 	return (0);
112305c91076SPawel Jakub Dawidek }
112405c91076SPawel Jakub Dawidek 
112505c91076SPawel Jakub Dawidek static void
112605c91076SPawel Jakub Dawidek std_load(struct gctl_req *req __unused, unsigned flags)
112705c91076SPawel Jakub Dawidek {
112805c91076SPawel Jakub Dawidek 
112905c91076SPawel Jakub Dawidek 	/*
113005c91076SPawel Jakub Dawidek 	 * Do nothing special here, because of G_FLAG_LOADKLD flag,
113105c91076SPawel Jakub Dawidek 	 * module is already loaded.
113205c91076SPawel Jakub Dawidek 	 */
113305c91076SPawel Jakub Dawidek 	if ((flags & G_FLAG_VERBOSE) != 0)
113405c91076SPawel Jakub Dawidek 		printf("Module available.\n");
113505c91076SPawel Jakub Dawidek }
113605c91076SPawel Jakub Dawidek 
113705c91076SPawel Jakub Dawidek static int
113805c91076SPawel Jakub Dawidek std_unload_available(void)
113905c91076SPawel Jakub Dawidek {
114005c91076SPawel Jakub Dawidek 	char name[64];
114105c91076SPawel Jakub Dawidek 	int id;
114205c91076SPawel Jakub Dawidek 
114305c91076SPawel Jakub Dawidek 	snprintf(name, sizeof(name), "geom_%s", class_name);
114405c91076SPawel Jakub Dawidek 	id = kldfind(name);
114505c91076SPawel Jakub Dawidek 	if (id >= 0)
114605c91076SPawel Jakub Dawidek 		return (1);
114705c91076SPawel Jakub Dawidek 	return (0);
114805c91076SPawel Jakub Dawidek }
114905c91076SPawel Jakub Dawidek 
115005c91076SPawel Jakub Dawidek static void
115105c91076SPawel Jakub Dawidek std_unload(struct gctl_req *req, unsigned flags __unused)
115205c91076SPawel Jakub Dawidek {
115305c91076SPawel Jakub Dawidek 	char name[64];
115405c91076SPawel Jakub Dawidek 	int id;
115505c91076SPawel Jakub Dawidek 
115605c91076SPawel Jakub Dawidek 	snprintf(name, sizeof(name), "geom_%s", class_name);
115705c91076SPawel Jakub Dawidek 	id = kldfind(name);
115805c91076SPawel Jakub Dawidek 	if (id < 0) {
115905c91076SPawel Jakub Dawidek 		gctl_error(req, "Could not find module: %s.", strerror(errno));
116005c91076SPawel Jakub Dawidek 		return;
116105c91076SPawel Jakub Dawidek 	}
116205c91076SPawel Jakub Dawidek 	if (kldunload(id) < 0) {
116305c91076SPawel Jakub Dawidek 		gctl_error(req, "Could not unload module: %s.",
116405c91076SPawel Jakub Dawidek 		    strerror(errno));
116505c91076SPawel Jakub Dawidek 		return;
116605c91076SPawel Jakub Dawidek 	}
116705c91076SPawel Jakub Dawidek }
116805c91076SPawel Jakub Dawidek 
116905c91076SPawel Jakub Dawidek static int
117005c91076SPawel Jakub Dawidek std_available(const char *name)
117105c91076SPawel Jakub Dawidek {
117205c91076SPawel Jakub Dawidek 
1173b70eccf3SPawel Jakub Dawidek 	if (strcmp(name, "help") == 0)
1174b70eccf3SPawel Jakub Dawidek 		return (1);
1175b70eccf3SPawel Jakub Dawidek 	else if (strcmp(name, "list") == 0)
117605c91076SPawel Jakub Dawidek 		return (std_list_available());
117718ee8840SPawel Jakub Dawidek 	else if (strcmp(name, "status") == 0)
117818ee8840SPawel Jakub Dawidek 		return (std_status_available());
117905c91076SPawel Jakub Dawidek 	else if (strcmp(name, "load") == 0)
118005c91076SPawel Jakub Dawidek 		return (std_load_available());
118105c91076SPawel Jakub Dawidek 	else if (strcmp(name, "unload") == 0)
118205c91076SPawel Jakub Dawidek 		return (std_unload_available());
118305c91076SPawel Jakub Dawidek 	else
118405c91076SPawel Jakub Dawidek 		assert(!"Unknown standard command.");
118505c91076SPawel Jakub Dawidek 	return (0);
118605c91076SPawel Jakub Dawidek }
1187