105c91076SPawel Jakub Dawidek /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
3005c91076SPawel Jakub Dawidek #include <sys/linker.h>
3105c91076SPawel Jakub Dawidek #include <sys/module.h>
3205c91076SPawel Jakub Dawidek #include <sys/stat.h>
3305c91076SPawel Jakub Dawidek #include <sys/sysctl.h>
3405c91076SPawel Jakub Dawidek #include <ctype.h>
3505c91076SPawel Jakub Dawidek #include <err.h>
3605c91076SPawel Jakub Dawidek #include <errno.h>
37f200cc25SRobert Wing #include <paths.h>
3805c91076SPawel Jakub Dawidek #include <stdio.h>
3905c91076SPawel Jakub Dawidek #include <stdlib.h>
4005c91076SPawel Jakub Dawidek #include <stdarg.h>
410f73f701SEdward Tomasz Napierala #include <stdbool.h>
4205c91076SPawel Jakub Dawidek #include <stdint.h>
4305c91076SPawel Jakub Dawidek #include <string.h>
4405c91076SPawel Jakub Dawidek #include <unistd.h>
4505c91076SPawel Jakub Dawidek #include <libgen.h>
46af565b58SPawel Jakub Dawidek #include <libutil.h>
4705c91076SPawel Jakub Dawidek #include <inttypes.h>
4805c91076SPawel Jakub Dawidek #include <dlfcn.h>
4905c91076SPawel Jakub Dawidek #include <assert.h>
5005c91076SPawel Jakub Dawidek #include <libgeom.h>
5105c91076SPawel Jakub Dawidek #include <geom.h>
5205c91076SPawel Jakub Dawidek
5305c91076SPawel Jakub Dawidek #include "misc/subr.h"
5405c91076SPawel Jakub Dawidek
5514bf405bSMarcel Moolenaar #ifdef STATIC_GEOM_CLASSES
56a16f9b36SMarcel Moolenaar extern uint32_t gpart_version;
57a16f9b36SMarcel Moolenaar extern struct g_command gpart_class_commands[];
58bc69d66fSXin LI extern uint32_t glabel_version;
59bc69d66fSXin LI extern struct g_command glabel_class_commands[];
60a16f9b36SMarcel Moolenaar #endif
6105c91076SPawel Jakub Dawidek
6205c91076SPawel Jakub Dawidek static char comm[MAXPATHLEN], *class_name = NULL, *gclass_name = NULL;
6305c91076SPawel Jakub Dawidek static uint32_t *version = NULL;
6405c91076SPawel Jakub Dawidek static int verbose = 0;
6505c91076SPawel Jakub Dawidek static struct g_command *class_commands = NULL;
6605c91076SPawel Jakub Dawidek
67138cedfdSPawel Jakub Dawidek #define GEOM_CLASS_CMDS 0x01
68138cedfdSPawel Jakub Dawidek #define GEOM_STD_CMDS 0x02
696a027382SEdward Tomasz Napierala
706a027382SEdward Tomasz Napierala #define GEOM_CLASS_WIDTH 10
716a027382SEdward Tomasz Napierala
72138cedfdSPawel Jakub Dawidek static struct g_command *find_command(const char *cmdstr, int flags);
730f73f701SEdward Tomasz Napierala static void list_one_geom_by_provider(const char *provider_name);
7405c91076SPawel Jakub Dawidek static int std_available(const char *name);
755678114cSAlexander Motin static int std_list_available(void);
765678114cSAlexander Motin static int std_load_available(void);
7705c91076SPawel Jakub Dawidek
78b70eccf3SPawel Jakub Dawidek static void std_help(struct gctl_req *req, unsigned flags);
7905c91076SPawel Jakub Dawidek static void std_list(struct gctl_req *req, unsigned flags);
8018ee8840SPawel Jakub Dawidek static void std_status(struct gctl_req *req, unsigned flags);
8105c91076SPawel Jakub Dawidek static void std_load(struct gctl_req *req, unsigned flags);
8205c91076SPawel Jakub Dawidek static void std_unload(struct gctl_req *req, unsigned flags);
8305c91076SPawel Jakub Dawidek
84bf70beceSEd Schouten static struct g_command std_commands[] = {
85946e2f35SPawel Jakub Dawidek { "help", 0, std_help, G_NULL_OPTS, NULL },
8683d165c1SAlexander Motin { "list", 0, std_list,
8783d165c1SAlexander Motin {
8883d165c1SAlexander Motin { 'a', "all", NULL, G_TYPE_BOOL },
8983d165c1SAlexander Motin G_OPT_SENTINEL
9083d165c1SAlexander Motin },
9183d165c1SAlexander Motin "[-a] [name ...]"
92c979e206SPawel Jakub Dawidek },
93ba6821f0SPawel Jakub Dawidek { "status", 0, std_status,
94ba6821f0SPawel Jakub Dawidek {
9583d165c1SAlexander Motin { 'a', "all", NULL, G_TYPE_BOOL },
9683d165c1SAlexander Motin { 'g', "geoms", NULL, G_TYPE_BOOL },
976fc60008SPawel Jakub Dawidek { 's', "script", NULL, G_TYPE_BOOL },
98ba6821f0SPawel Jakub Dawidek G_OPT_SENTINEL
99ba6821f0SPawel Jakub Dawidek },
10083d165c1SAlexander Motin "[-ags] [name ...]"
101c979e206SPawel Jakub Dawidek },
1023cf55d3aSMarcel Moolenaar { "load", G_FLAG_VERBOSE | G_FLAG_LOADKLD, std_load, G_NULL_OPTS,
103946e2f35SPawel Jakub Dawidek NULL },
104946e2f35SPawel Jakub Dawidek { "unload", G_FLAG_VERBOSE, std_unload, G_NULL_OPTS, NULL },
10505c91076SPawel Jakub Dawidek G_CMD_SENTINEL
10605c91076SPawel Jakub Dawidek };
10705c91076SPawel Jakub Dawidek
10805c91076SPawel Jakub Dawidek static void
usage_command(struct g_command * cmd,const char * prefix)109c979e206SPawel Jakub Dawidek usage_command(struct g_command *cmd, const char *prefix)
11005c91076SPawel Jakub Dawidek {
11105c91076SPawel Jakub Dawidek struct g_option *opt;
112c979e206SPawel Jakub Dawidek unsigned i;
11305c91076SPawel Jakub Dawidek
114c979e206SPawel Jakub Dawidek if (cmd->gc_usage != NULL) {
115963feac4SPawel Jakub Dawidek char *pos, *ptr, *sptr;
116963feac4SPawel Jakub Dawidek
117963feac4SPawel Jakub Dawidek sptr = ptr = strdup(cmd->gc_usage);
118963feac4SPawel Jakub Dawidek while ((pos = strsep(&ptr, "\n")) != NULL) {
119963feac4SPawel Jakub Dawidek if (*pos == '\0')
120963feac4SPawel Jakub Dawidek continue;
121963feac4SPawel Jakub Dawidek fprintf(stderr, "%s %s %s %s\n", prefix, comm,
122963feac4SPawel Jakub Dawidek cmd->gc_name, pos);
123963feac4SPawel Jakub Dawidek }
124963feac4SPawel Jakub Dawidek free(sptr);
125c979e206SPawel Jakub Dawidek return;
126c979e206SPawel Jakub Dawidek }
127963feac4SPawel Jakub Dawidek
128963feac4SPawel Jakub Dawidek fprintf(stderr, "%s %s %s", prefix, comm, cmd->gc_name);
12905c91076SPawel Jakub Dawidek if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0)
13005c91076SPawel Jakub Dawidek fprintf(stderr, " [-v]");
131c979e206SPawel Jakub Dawidek for (i = 0; ; i++) {
132c979e206SPawel Jakub Dawidek opt = &cmd->gc_options[i];
13305c91076SPawel Jakub Dawidek if (opt->go_name == NULL)
13405c91076SPawel Jakub Dawidek break;
1356fc60008SPawel Jakub Dawidek if (opt->go_val != NULL || G_OPT_TYPE(opt) == G_TYPE_BOOL)
13605c91076SPawel Jakub Dawidek fprintf(stderr, " [");
13705c91076SPawel Jakub Dawidek else
13805c91076SPawel Jakub Dawidek fprintf(stderr, " ");
13905c91076SPawel Jakub Dawidek fprintf(stderr, "-%c", opt->go_char);
1406fc60008SPawel Jakub Dawidek if (G_OPT_TYPE(opt) != G_TYPE_BOOL)
14105c91076SPawel Jakub Dawidek fprintf(stderr, " %s", opt->go_name);
1426fc60008SPawel Jakub Dawidek if (opt->go_val != NULL || G_OPT_TYPE(opt) == G_TYPE_BOOL)
14305c91076SPawel Jakub Dawidek fprintf(stderr, "]");
14405c91076SPawel Jakub Dawidek }
145c979e206SPawel Jakub Dawidek fprintf(stderr, "\n");
14605c91076SPawel Jakub Dawidek }
14705c91076SPawel Jakub Dawidek
14805c91076SPawel Jakub Dawidek static void
usage(void)149c979e206SPawel Jakub Dawidek usage(void)
15005c91076SPawel Jakub Dawidek {
15105c91076SPawel Jakub Dawidek
15205c91076SPawel Jakub Dawidek if (class_name == NULL) {
153112adef8SEdward Tomasz Napierala fprintf(stderr, "usage: geom <class> <command> [options]\n");
1540f73f701SEdward Tomasz Napierala fprintf(stderr, " geom -p <provider-name>\n");
1556a027382SEdward Tomasz Napierala fprintf(stderr, " geom -t\n");
156112adef8SEdward Tomasz Napierala exit(EXIT_FAILURE);
15705c91076SPawel Jakub Dawidek } else {
158c979e206SPawel Jakub Dawidek struct g_command *cmd;
15905c91076SPawel Jakub Dawidek const char *prefix;
16005c91076SPawel Jakub Dawidek unsigned i;
16105c91076SPawel Jakub Dawidek
16205c91076SPawel Jakub Dawidek prefix = "usage:";
163c979e206SPawel Jakub Dawidek if (class_commands != NULL) {
164c979e206SPawel Jakub Dawidek for (i = 0; ; i++) {
165c979e206SPawel Jakub Dawidek cmd = &class_commands[i];
166c979e206SPawel Jakub Dawidek if (cmd->gc_name == NULL)
167c979e206SPawel Jakub Dawidek break;
168c979e206SPawel Jakub Dawidek usage_command(cmd, prefix);
16905c91076SPawel Jakub Dawidek prefix = " ";
17005c91076SPawel Jakub Dawidek }
171c979e206SPawel Jakub Dawidek }
17205c91076SPawel Jakub Dawidek for (i = 0; ; i++) {
17305c91076SPawel Jakub Dawidek cmd = &std_commands[i];
17405c91076SPawel Jakub Dawidek if (cmd->gc_name == NULL)
17505c91076SPawel Jakub Dawidek break;
176138cedfdSPawel Jakub Dawidek /*
177138cedfdSPawel Jakub Dawidek * If class defines command, which has the same name as
178138cedfdSPawel Jakub Dawidek * standard command, skip it, because it was already
179138cedfdSPawel Jakub Dawidek * shown on usage().
180138cedfdSPawel Jakub Dawidek */
181138cedfdSPawel Jakub Dawidek if (find_command(cmd->gc_name, GEOM_CLASS_CMDS) != NULL)
18205c91076SPawel Jakub Dawidek continue;
183c979e206SPawel Jakub Dawidek usage_command(cmd, prefix);
18405c91076SPawel Jakub Dawidek prefix = " ";
18505c91076SPawel Jakub Dawidek }
18605c91076SPawel Jakub Dawidek exit(EXIT_FAILURE);
18705c91076SPawel Jakub Dawidek }
18805c91076SPawel Jakub Dawidek }
18905c91076SPawel Jakub Dawidek
19005c91076SPawel Jakub Dawidek static void
load_module(void)19105c91076SPawel Jakub Dawidek load_module(void)
19205c91076SPawel Jakub Dawidek {
19305c91076SPawel Jakub Dawidek char name1[64], name2[64];
19405c91076SPawel Jakub Dawidek
19505c91076SPawel Jakub Dawidek snprintf(name1, sizeof(name1), "g_%s", class_name);
19605c91076SPawel Jakub Dawidek snprintf(name2, sizeof(name2), "geom_%s", class_name);
19705c91076SPawel Jakub Dawidek if (modfind(name1) < 0) {
19805c91076SPawel Jakub Dawidek /* Not present in kernel, try loading it. */
19905c91076SPawel Jakub Dawidek if (kldload(name2) < 0 || modfind(name1) < 0) {
20005c91076SPawel Jakub Dawidek if (errno != EEXIST) {
201112adef8SEdward Tomasz Napierala err(EXIT_FAILURE, "cannot load %s", name2);
20205c91076SPawel Jakub Dawidek }
20305c91076SPawel Jakub Dawidek }
20405c91076SPawel Jakub Dawidek }
20505c91076SPawel Jakub Dawidek }
20605c91076SPawel Jakub Dawidek
20705c91076SPawel Jakub Dawidek static int
strlcatf(char * str,size_t size,const char * format,...)20805c91076SPawel Jakub Dawidek strlcatf(char *str, size_t size, const char *format, ...)
20905c91076SPawel Jakub Dawidek {
21005c91076SPawel Jakub Dawidek size_t len;
21105c91076SPawel Jakub Dawidek va_list ap;
21205c91076SPawel Jakub Dawidek int ret;
21305c91076SPawel Jakub Dawidek
21405c91076SPawel Jakub Dawidek len = strlen(str);
21505c91076SPawel Jakub Dawidek str += len;
21605c91076SPawel Jakub Dawidek size -= len;
21705c91076SPawel Jakub Dawidek
21805c91076SPawel Jakub Dawidek va_start(ap, format);
21905c91076SPawel Jakub Dawidek ret = vsnprintf(str, size, format, ap);
22005c91076SPawel Jakub Dawidek va_end(ap);
22105c91076SPawel Jakub Dawidek
22205c91076SPawel Jakub Dawidek return (ret);
22305c91076SPawel Jakub Dawidek }
22405c91076SPawel Jakub Dawidek
22505c91076SPawel Jakub Dawidek /*
22605c91076SPawel Jakub Dawidek * Find given option in options available for given command.
22705c91076SPawel Jakub Dawidek */
22805c91076SPawel Jakub Dawidek static struct g_option *
find_option(struct g_command * cmd,char ch)22905c91076SPawel Jakub Dawidek find_option(struct g_command *cmd, char ch)
23005c91076SPawel Jakub Dawidek {
23105c91076SPawel Jakub Dawidek struct g_option *opt;
23205c91076SPawel Jakub Dawidek unsigned i;
23305c91076SPawel Jakub Dawidek
23405c91076SPawel Jakub Dawidek for (i = 0; ; i++) {
23505c91076SPawel Jakub Dawidek opt = &cmd->gc_options[i];
23605c91076SPawel Jakub Dawidek if (opt->go_name == NULL)
23705c91076SPawel Jakub Dawidek return (NULL);
23805c91076SPawel Jakub Dawidek if (opt->go_char == ch)
23905c91076SPawel Jakub Dawidek return (opt);
24005c91076SPawel Jakub Dawidek }
24105c91076SPawel Jakub Dawidek /* NOTREACHED */
24205c91076SPawel Jakub Dawidek return (NULL);
24305c91076SPawel Jakub Dawidek }
24405c91076SPawel Jakub Dawidek
24505c91076SPawel Jakub Dawidek /*
24605c91076SPawel Jakub Dawidek * Add given option to gctl_req.
24705c91076SPawel Jakub Dawidek */
24805c91076SPawel Jakub Dawidek static void
set_option(struct gctl_req * req,struct g_option * opt,const char * val)24905c91076SPawel Jakub Dawidek set_option(struct gctl_req *req, struct g_option *opt, const char *val)
25005c91076SPawel Jakub Dawidek {
251315fcbf7SPawel Jakub Dawidek const char *optname;
25235efcc8bSDag-Erling Smørgrav uint64_t number;
253a478ea74SPawel Jakub Dawidek void *ptr;
25405c91076SPawel Jakub Dawidek
255315fcbf7SPawel Jakub Dawidek if (G_OPT_ISMULTI(opt)) {
256315fcbf7SPawel Jakub Dawidek size_t optnamesize;
257315fcbf7SPawel Jakub Dawidek
258315fcbf7SPawel Jakub Dawidek if (G_OPT_NUM(opt) == UCHAR_MAX)
259315fcbf7SPawel Jakub Dawidek errx(EXIT_FAILURE, "Too many -%c options.", opt->go_char);
260315fcbf7SPawel Jakub Dawidek
261315fcbf7SPawel Jakub Dawidek /*
262315fcbf7SPawel Jakub Dawidek * Base option name length plus 3 bytes for option number
263315fcbf7SPawel Jakub Dawidek * (max. 255 options) plus 1 byte for terminating '\0'.
264315fcbf7SPawel Jakub Dawidek */
265315fcbf7SPawel Jakub Dawidek optnamesize = strlen(opt->go_name) + 3 + 1;
266315fcbf7SPawel Jakub Dawidek ptr = malloc(optnamesize);
267315fcbf7SPawel Jakub Dawidek if (ptr == NULL)
268315fcbf7SPawel Jakub Dawidek errx(EXIT_FAILURE, "No memory.");
269315fcbf7SPawel Jakub Dawidek snprintf(ptr, optnamesize, "%s%u", opt->go_name, G_OPT_NUM(opt));
270315fcbf7SPawel Jakub Dawidek G_OPT_NUMINC(opt);
271315fcbf7SPawel Jakub Dawidek optname = ptr;
272315fcbf7SPawel Jakub Dawidek } else {
273315fcbf7SPawel Jakub Dawidek optname = opt->go_name;
274315fcbf7SPawel Jakub Dawidek }
275315fcbf7SPawel Jakub Dawidek
276fa5383a2SPawel Jakub Dawidek if (G_OPT_TYPE(opt) == G_TYPE_NUMBER) {
277e84091cbSPawel Jakub Dawidek if (expand_number(val, &number) == -1) {
278f104beb7SPawel Jakub Dawidek err(EXIT_FAILURE, "Invalid value for '%c' argument",
27905c91076SPawel Jakub Dawidek opt->go_char);
28005c91076SPawel Jakub Dawidek }
281a478ea74SPawel Jakub Dawidek ptr = malloc(sizeof(intmax_t));
282a478ea74SPawel Jakub Dawidek if (ptr == NULL)
283a478ea74SPawel Jakub Dawidek errx(EXIT_FAILURE, "No memory.");
284a478ea74SPawel Jakub Dawidek *(intmax_t *)ptr = number;
285a478ea74SPawel Jakub Dawidek opt->go_val = ptr;
286628ec6d3SPawel Jakub Dawidek gctl_ro_param(req, optname, sizeof(intmax_t), opt->go_val);
2876fc60008SPawel Jakub Dawidek } else if (G_OPT_TYPE(opt) == G_TYPE_STRING) {
288315fcbf7SPawel Jakub Dawidek gctl_ro_param(req, optname, -1, val);
2896fc60008SPawel Jakub Dawidek } else if (G_OPT_TYPE(opt) == G_TYPE_BOOL) {
290a478ea74SPawel Jakub Dawidek ptr = malloc(sizeof(int));
291a478ea74SPawel Jakub Dawidek if (ptr == NULL)
29205c91076SPawel Jakub Dawidek errx(EXIT_FAILURE, "No memory.");
293a478ea74SPawel Jakub Dawidek *(int *)ptr = *val - '0';
294a478ea74SPawel Jakub Dawidek opt->go_val = ptr;
295315fcbf7SPawel Jakub Dawidek gctl_ro_param(req, optname, sizeof(int), opt->go_val);
2966fc60008SPawel Jakub Dawidek } else {
2976fc60008SPawel Jakub Dawidek assert(!"Invalid type");
29805c91076SPawel Jakub Dawidek }
299315fcbf7SPawel Jakub Dawidek
300315fcbf7SPawel Jakub Dawidek if (G_OPT_ISMULTI(opt))
301315fcbf7SPawel Jakub Dawidek free(__DECONST(char *, optname));
30205c91076SPawel Jakub Dawidek }
30305c91076SPawel Jakub Dawidek
30405c91076SPawel Jakub Dawidek /*
30505c91076SPawel Jakub Dawidek * 1. Add given argument by caller.
30605c91076SPawel Jakub Dawidek * 2. Add default values of not given arguments.
30705c91076SPawel Jakub Dawidek * 3. Add the rest of arguments.
30805c91076SPawel Jakub Dawidek */
30905c91076SPawel Jakub Dawidek static void
parse_arguments(struct g_command * cmd,struct gctl_req * req,int * argc,char *** argv)31005c91076SPawel Jakub Dawidek parse_arguments(struct g_command *cmd, struct gctl_req *req, int *argc,
31105c91076SPawel Jakub Dawidek char ***argv)
31205c91076SPawel Jakub Dawidek {
31305c91076SPawel Jakub Dawidek struct g_option *opt;
31405c91076SPawel Jakub Dawidek char opts[64];
31505c91076SPawel Jakub Dawidek unsigned i;
31668bff4a0SKirk McKusick int ch, vcount;
31705c91076SPawel Jakub Dawidek
31805c91076SPawel Jakub Dawidek *opts = '\0';
31905c91076SPawel Jakub Dawidek if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0)
32005c91076SPawel Jakub Dawidek strlcat(opts, "v", sizeof(opts));
32105c91076SPawel Jakub Dawidek for (i = 0; ; i++) {
32205c91076SPawel Jakub Dawidek opt = &cmd->gc_options[i];
32305c91076SPawel Jakub Dawidek if (opt->go_name == NULL)
32405c91076SPawel Jakub Dawidek break;
3256fc60008SPawel Jakub Dawidek assert(G_OPT_TYPE(opt) != 0);
326315fcbf7SPawel Jakub Dawidek assert((opt->go_type & ~(G_TYPE_MASK | G_TYPE_MULTI)) == 0);
327315fcbf7SPawel Jakub Dawidek /* Multiple bool arguments makes no sense. */
328315fcbf7SPawel Jakub Dawidek assert(G_OPT_TYPE(opt) != G_TYPE_BOOL ||
329315fcbf7SPawel Jakub Dawidek (opt->go_type & G_TYPE_MULTI) == 0);
33005c91076SPawel Jakub Dawidek strlcatf(opts, sizeof(opts), "%c", opt->go_char);
3316fc60008SPawel Jakub Dawidek if (G_OPT_TYPE(opt) != G_TYPE_BOOL)
33205c91076SPawel Jakub Dawidek strlcat(opts, ":", sizeof(opts));
33305c91076SPawel Jakub Dawidek }
33405c91076SPawel Jakub Dawidek
33505c91076SPawel Jakub Dawidek /*
33605c91076SPawel Jakub Dawidek * Add specified arguments.
33705c91076SPawel Jakub Dawidek */
33868bff4a0SKirk McKusick vcount = 0;
33905c91076SPawel Jakub Dawidek while ((ch = getopt(*argc, *argv, opts)) != -1) {
34005c91076SPawel Jakub Dawidek /* Standard (not passed to kernel) options. */
34168bff4a0SKirk McKusick if (ch == 'v' && (cmd->gc_flags & G_FLAG_VERBOSE) != 0)
34205c91076SPawel Jakub Dawidek verbose = 1;
34305c91076SPawel Jakub Dawidek /* Options passed to kernel. */
34405c91076SPawel Jakub Dawidek opt = find_option(cmd, ch);
34568bff4a0SKirk McKusick if (opt == NULL) {
34668bff4a0SKirk McKusick if (ch == 'v' && (cmd->gc_flags & G_FLAG_VERBOSE) != 0){
34768bff4a0SKirk McKusick if (++vcount < 2)
34868bff4a0SKirk McKusick continue;
34968bff4a0SKirk McKusick else
35068bff4a0SKirk McKusick warnx("Option 'v' specified twice.");
35168bff4a0SKirk McKusick }
352c979e206SPawel Jakub Dawidek usage();
35368bff4a0SKirk McKusick }
354315fcbf7SPawel Jakub Dawidek if (!G_OPT_ISMULTI(opt) && G_OPT_ISDONE(opt)) {
3556fc60008SPawel Jakub Dawidek warnx("Option '%c' specified twice.", opt->go_char);
356c979e206SPawel Jakub Dawidek usage();
35705c91076SPawel Jakub Dawidek }
35805c91076SPawel Jakub Dawidek G_OPT_DONE(opt);
35905c91076SPawel Jakub Dawidek
3606fc60008SPawel Jakub Dawidek if (G_OPT_TYPE(opt) == G_TYPE_BOOL)
36105c91076SPawel Jakub Dawidek set_option(req, opt, "1");
36205c91076SPawel Jakub Dawidek else
36305c91076SPawel Jakub Dawidek set_option(req, opt, optarg);
36405c91076SPawel Jakub Dawidek }
36505c91076SPawel Jakub Dawidek *argc -= optind;
36605c91076SPawel Jakub Dawidek *argv += optind;
36705c91076SPawel Jakub Dawidek
36805c91076SPawel Jakub Dawidek /*
36905c91076SPawel Jakub Dawidek * Add not specified arguments, but with default values.
37005c91076SPawel Jakub Dawidek */
37105c91076SPawel Jakub Dawidek for (i = 0; ; i++) {
37205c91076SPawel Jakub Dawidek opt = &cmd->gc_options[i];
37305c91076SPawel Jakub Dawidek if (opt->go_name == NULL)
37405c91076SPawel Jakub Dawidek break;
37505c91076SPawel Jakub Dawidek if (G_OPT_ISDONE(opt))
37605c91076SPawel Jakub Dawidek continue;
37705c91076SPawel Jakub Dawidek
3786fc60008SPawel Jakub Dawidek if (G_OPT_TYPE(opt) == G_TYPE_BOOL) {
37905c91076SPawel Jakub Dawidek assert(opt->go_val == NULL);
38005c91076SPawel Jakub Dawidek set_option(req, opt, "0");
38105c91076SPawel Jakub Dawidek } else {
38205c91076SPawel Jakub Dawidek if (opt->go_val == NULL) {
3836fc60008SPawel Jakub Dawidek warnx("Option '%c' not specified.",
38405c91076SPawel Jakub Dawidek opt->go_char);
385c979e206SPawel Jakub Dawidek usage();
3867648b1e9SPawel Jakub Dawidek } else if (opt->go_val == G_VAL_OPTIONAL) {
3877648b1e9SPawel Jakub Dawidek /* add nothing. */
38805c91076SPawel Jakub Dawidek } else {
389946e2f35SPawel Jakub Dawidek set_option(req, opt, opt->go_val);
39005c91076SPawel Jakub Dawidek }
39105c91076SPawel Jakub Dawidek }
39205c91076SPawel Jakub Dawidek }
3933cf55d3aSMarcel Moolenaar
39405c91076SPawel Jakub Dawidek /*
39505c91076SPawel Jakub Dawidek * Add rest of given arguments.
39605c91076SPawel Jakub Dawidek */
39705c91076SPawel Jakub Dawidek gctl_ro_param(req, "nargs", sizeof(int), argc);
39805c91076SPawel Jakub Dawidek for (i = 0; i < (unsigned)*argc; i++) {
39905c91076SPawel Jakub Dawidek char argname[16];
40005c91076SPawel Jakub Dawidek
40105c91076SPawel Jakub Dawidek snprintf(argname, sizeof(argname), "arg%u", i);
40205c91076SPawel Jakub Dawidek gctl_ro_param(req, argname, -1, (*argv)[i]);
40305c91076SPawel Jakub Dawidek }
40405c91076SPawel Jakub Dawidek }
40505c91076SPawel Jakub Dawidek
40605c91076SPawel Jakub Dawidek /*
40705c91076SPawel Jakub Dawidek * Find given command in commands available for given class.
40805c91076SPawel Jakub Dawidek */
40905c91076SPawel Jakub Dawidek static struct g_command *
find_command(const char * cmdstr,int flags)410138cedfdSPawel Jakub Dawidek find_command(const char *cmdstr, int flags)
41105c91076SPawel Jakub Dawidek {
41205c91076SPawel Jakub Dawidek struct g_command *cmd;
41305c91076SPawel Jakub Dawidek unsigned i;
41405c91076SPawel Jakub Dawidek
41505c91076SPawel Jakub Dawidek /*
41605c91076SPawel Jakub Dawidek * First try to find command defined by loaded library.
41705c91076SPawel Jakub Dawidek */
418138cedfdSPawel Jakub Dawidek if ((flags & GEOM_CLASS_CMDS) != 0 && class_commands != NULL) {
41905c91076SPawel Jakub Dawidek for (i = 0; ; i++) {
42005c91076SPawel Jakub Dawidek cmd = &class_commands[i];
42105c91076SPawel Jakub Dawidek if (cmd->gc_name == NULL)
42205c91076SPawel Jakub Dawidek break;
42305c91076SPawel Jakub Dawidek if (strcmp(cmd->gc_name, cmdstr) == 0)
42405c91076SPawel Jakub Dawidek return (cmd);
42505c91076SPawel Jakub Dawidek }
42605c91076SPawel Jakub Dawidek }
42705c91076SPawel Jakub Dawidek /*
42805c91076SPawel Jakub Dawidek * Now try to find in standard commands.
42905c91076SPawel Jakub Dawidek */
430138cedfdSPawel Jakub Dawidek if ((flags & GEOM_STD_CMDS) != 0) {
43105c91076SPawel Jakub Dawidek for (i = 0; ; i++) {
43205c91076SPawel Jakub Dawidek cmd = &std_commands[i];
43305c91076SPawel Jakub Dawidek if (cmd->gc_name == NULL)
43405c91076SPawel Jakub Dawidek break;
43505c91076SPawel Jakub Dawidek if (strcmp(cmd->gc_name, cmdstr) == 0)
43605c91076SPawel Jakub Dawidek return (cmd);
43705c91076SPawel Jakub Dawidek }
438138cedfdSPawel Jakub Dawidek }
43905c91076SPawel Jakub Dawidek return (NULL);
44005c91076SPawel Jakub Dawidek }
44105c91076SPawel Jakub Dawidek
44205c91076SPawel Jakub Dawidek static unsigned
set_flags(struct g_command * cmd)44305c91076SPawel Jakub Dawidek set_flags(struct g_command *cmd)
44405c91076SPawel Jakub Dawidek {
44505c91076SPawel Jakub Dawidek unsigned flags = 0;
44605c91076SPawel Jakub Dawidek
447e38717c1SKirk McKusick if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0 && verbose)
44805c91076SPawel Jakub Dawidek flags |= G_FLAG_VERBOSE;
44905c91076SPawel Jakub Dawidek
45005c91076SPawel Jakub Dawidek return (flags);
45105c91076SPawel Jakub Dawidek }
45205c91076SPawel Jakub Dawidek
45305c91076SPawel Jakub Dawidek /*
45405c91076SPawel Jakub Dawidek * Run command.
45505c91076SPawel Jakub Dawidek */
45605c91076SPawel Jakub Dawidek static void
run_command(int argc,char * argv[])45705c91076SPawel Jakub Dawidek run_command(int argc, char *argv[])
45805c91076SPawel Jakub Dawidek {
45905c91076SPawel Jakub Dawidek struct g_command *cmd;
46005c91076SPawel Jakub Dawidek struct gctl_req *req;
46105c91076SPawel Jakub Dawidek const char *errstr;
46205c91076SPawel Jakub Dawidek char buf[4096];
46305c91076SPawel Jakub Dawidek
464138cedfdSPawel Jakub Dawidek /* First try to find a command defined by a class. */
465138cedfdSPawel Jakub Dawidek cmd = find_command(argv[0], GEOM_CLASS_CMDS);
466138cedfdSPawel Jakub Dawidek if (cmd == NULL) {
467138cedfdSPawel Jakub Dawidek /* Now, try to find a standard command. */
468138cedfdSPawel Jakub Dawidek cmd = find_command(argv[0], GEOM_STD_CMDS);
46905c91076SPawel Jakub Dawidek if (cmd == NULL) {
4706fc60008SPawel Jakub Dawidek warnx("Unknown command: %s.", argv[0]);
471c979e206SPawel Jakub Dawidek usage();
47205c91076SPawel Jakub Dawidek }
473138cedfdSPawel Jakub Dawidek if (!std_available(cmd->gc_name)) {
474112adef8SEdward Tomasz Napierala warnx("Command '%s' not available; "
475112adef8SEdward Tomasz Napierala "try 'load' first.", argv[0]);
476138cedfdSPawel Jakub Dawidek exit(EXIT_FAILURE);
477138cedfdSPawel Jakub Dawidek }
478138cedfdSPawel Jakub Dawidek }
47905c91076SPawel Jakub Dawidek if ((cmd->gc_flags & G_FLAG_LOADKLD) != 0)
48005c91076SPawel Jakub Dawidek load_module();
48105c91076SPawel Jakub Dawidek
48205c91076SPawel Jakub Dawidek req = gctl_get_handle();
48305c91076SPawel Jakub Dawidek gctl_ro_param(req, "class", -1, gclass_name);
48405c91076SPawel Jakub Dawidek gctl_ro_param(req, "verb", -1, argv[0]);
48505c91076SPawel Jakub Dawidek if (version != NULL)
48605c91076SPawel Jakub Dawidek gctl_ro_param(req, "version", sizeof(*version), version);
48705c91076SPawel Jakub Dawidek parse_arguments(cmd, req, &argc, &argv);
48805c91076SPawel Jakub Dawidek
4892117cdd4SAlexander Motin buf[0] = '\0';
49005c91076SPawel Jakub Dawidek if (cmd->gc_func != NULL) {
49105c91076SPawel Jakub Dawidek unsigned flags;
49205c91076SPawel Jakub Dawidek
49305c91076SPawel Jakub Dawidek flags = set_flags(cmd);
49405c91076SPawel Jakub Dawidek cmd->gc_func(req, flags);
49505c91076SPawel Jakub Dawidek errstr = req->error;
49605c91076SPawel Jakub Dawidek } else {
4972117cdd4SAlexander Motin gctl_add_param(req, "output", sizeof(buf), buf,
4982117cdd4SAlexander Motin GCTL_PARAM_WR | GCTL_PARAM_ASCII);
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);
50390e29718SKirk McKusick /* Suppress EXIT_FAILURE for warnings */
50490e29718SKirk McKusick if (strncmp(errstr, "warning: ", strlen("warning: ")) == 0)
50590e29718SKirk McKusick req->nerror = 0;
50690e29718SKirk McKusick if (req->nerror != 0) {
50705c91076SPawel Jakub Dawidek gctl_free(req);
50805c91076SPawel Jakub Dawidek exit(EXIT_FAILURE);
50905c91076SPawel Jakub Dawidek }
510f792f1d8SPawel Jakub Dawidek }
5111bcfab7fSPawel Jakub Dawidek if (buf[0] != '\0')
51205c91076SPawel Jakub Dawidek printf("%s", buf);
51305c91076SPawel Jakub Dawidek gctl_free(req);
51405c91076SPawel Jakub Dawidek if (verbose)
51505c91076SPawel Jakub Dawidek printf("Done.\n");
51605c91076SPawel Jakub Dawidek exit(EXIT_SUCCESS);
51705c91076SPawel Jakub Dawidek }
51805c91076SPawel Jakub Dawidek
51914bf405bSMarcel Moolenaar #ifndef STATIC_GEOM_CLASSES
5203bad06e9SPawel Jakub Dawidek static const char *
library_path(void)5213bad06e9SPawel Jakub Dawidek library_path(void)
5223bad06e9SPawel Jakub Dawidek {
5233bad06e9SPawel Jakub Dawidek const char *path;
5243bad06e9SPawel Jakub Dawidek
5253bad06e9SPawel Jakub Dawidek path = getenv("GEOM_LIBRARY_PATH");
5263bad06e9SPawel Jakub Dawidek if (path == NULL)
5278494b738SDavid E. O'Brien path = GEOM_CLASS_DIR;
5283bad06e9SPawel Jakub Dawidek return (path);
5293bad06e9SPawel Jakub Dawidek }
5303bad06e9SPawel Jakub Dawidek
53105c91076SPawel Jakub Dawidek static void
load_library(void)53205c91076SPawel Jakub Dawidek load_library(void)
53305c91076SPawel Jakub Dawidek {
5345d824386SUlf Lilleengen char *curpath, path[MAXPATHLEN], *tofree, *totalpath;
53505c91076SPawel Jakub Dawidek uint32_t *lib_version;
53605c91076SPawel Jakub Dawidek void *dlh;
5372591e96cSUlf Lilleengen int ret;
53805c91076SPawel Jakub Dawidek
5392591e96cSUlf Lilleengen ret = 0;
5405d824386SUlf Lilleengen tofree = totalpath = strdup(library_path());
5412591e96cSUlf Lilleengen if (totalpath == NULL)
5422591e96cSUlf Lilleengen err(EXIT_FAILURE, "Not enough memory for library path");
5432591e96cSUlf Lilleengen
5442591e96cSUlf Lilleengen if (strchr(totalpath, ':') != NULL)
5452591e96cSUlf Lilleengen curpath = strsep(&totalpath, ":");
5462591e96cSUlf Lilleengen else
5472591e96cSUlf Lilleengen curpath = totalpath;
5482591e96cSUlf Lilleengen /* Traverse the paths to find one that contains the library we want. */
5492591e96cSUlf Lilleengen while (curpath != NULL) {
5502591e96cSUlf Lilleengen snprintf(path, sizeof(path), "%s/geom_%s.so", curpath,
5513bad06e9SPawel Jakub Dawidek class_name);
5522591e96cSUlf Lilleengen ret = access(path, F_OK);
5532591e96cSUlf Lilleengen if (ret == -1) {
554a73148d2SPawel Jakub Dawidek if (errno == ENOENT) {
55505c91076SPawel Jakub Dawidek /*
5562591e96cSUlf Lilleengen * If we cannot find library, try the next
5572591e96cSUlf Lilleengen * path.
55805c91076SPawel Jakub Dawidek */
5592591e96cSUlf Lilleengen curpath = strsep(&totalpath, ":");
5602591e96cSUlf Lilleengen continue;
56105c91076SPawel Jakub Dawidek }
562a73148d2SPawel Jakub Dawidek err(EXIT_FAILURE, "Cannot access library");
563a73148d2SPawel Jakub Dawidek }
5642591e96cSUlf Lilleengen break;
5652591e96cSUlf Lilleengen }
5665d824386SUlf Lilleengen free(tofree);
5672591e96cSUlf Lilleengen /* No library was found, but standard commands can still be used */
5682591e96cSUlf Lilleengen if (ret == -1)
5692591e96cSUlf Lilleengen return;
570a73148d2SPawel Jakub Dawidek dlh = dlopen(path, RTLD_NOW);
571a73148d2SPawel Jakub Dawidek if (dlh == NULL)
572a73148d2SPawel Jakub Dawidek errx(EXIT_FAILURE, "Cannot open library: %s.", dlerror());
57305c91076SPawel Jakub Dawidek lib_version = dlsym(dlh, "lib_version");
57405c91076SPawel Jakub Dawidek if (lib_version == NULL) {
5756fc60008SPawel Jakub Dawidek warnx("Cannot find symbol %s: %s.", "lib_version", dlerror());
57605c91076SPawel Jakub Dawidek dlclose(dlh);
57705c91076SPawel Jakub Dawidek exit(EXIT_FAILURE);
57805c91076SPawel Jakub Dawidek }
57905c91076SPawel Jakub Dawidek if (*lib_version != G_LIB_VERSION) {
58005c91076SPawel Jakub Dawidek dlclose(dlh);
581f792f1d8SPawel Jakub Dawidek errx(EXIT_FAILURE, "%s and %s are not synchronized.",
582f792f1d8SPawel Jakub Dawidek getprogname(), path);
58305c91076SPawel Jakub Dawidek }
58405c91076SPawel Jakub Dawidek version = dlsym(dlh, "version");
58505c91076SPawel Jakub Dawidek if (version == NULL) {
5866fc60008SPawel Jakub Dawidek warnx("Cannot find symbol %s: %s.", "version", dlerror());
58705c91076SPawel Jakub Dawidek dlclose(dlh);
58805c91076SPawel Jakub Dawidek exit(EXIT_FAILURE);
58905c91076SPawel Jakub Dawidek }
59005c91076SPawel Jakub Dawidek class_commands = dlsym(dlh, "class_commands");
59105c91076SPawel Jakub Dawidek if (class_commands == NULL) {
5926fc60008SPawel Jakub Dawidek warnx("Cannot find symbol %s: %s.", "class_commands",
5936fc60008SPawel Jakub Dawidek dlerror());
59405c91076SPawel Jakub Dawidek dlclose(dlh);
59505c91076SPawel Jakub Dawidek exit(EXIT_FAILURE);
59605c91076SPawel Jakub Dawidek }
59705c91076SPawel Jakub Dawidek }
59814bf405bSMarcel Moolenaar #endif /* !STATIC_GEOM_CLASSES */
59905c91076SPawel Jakub Dawidek
60005c91076SPawel Jakub Dawidek /*
60105c91076SPawel Jakub Dawidek * Class name should be all capital letters.
60205c91076SPawel Jakub Dawidek */
60305c91076SPawel Jakub Dawidek static void
set_class_name(void)60405c91076SPawel Jakub Dawidek set_class_name(void)
60505c91076SPawel Jakub Dawidek {
60605c91076SPawel Jakub Dawidek char *s1, *s2;
60705c91076SPawel Jakub Dawidek
608ee602fbbSPawel Jakub Dawidek s1 = class_name;
609ee602fbbSPawel Jakub Dawidek for (; *s1 != '\0'; s1++)
610ee602fbbSPawel Jakub Dawidek *s1 = tolower(*s1);
611cb94ab30SPawel Jakub Dawidek gclass_name = malloc(strlen(class_name) + 1);
61205c91076SPawel Jakub Dawidek if (gclass_name == NULL)
61305c91076SPawel Jakub Dawidek errx(EXIT_FAILURE, "No memory");
61405c91076SPawel Jakub Dawidek s1 = gclass_name;
61505c91076SPawel Jakub Dawidek s2 = class_name;
61605c91076SPawel Jakub Dawidek for (; *s2 != '\0'; s2++)
61705c91076SPawel Jakub Dawidek *s1++ = toupper(*s2);
61805c91076SPawel Jakub Dawidek *s1 = '\0';
61905c91076SPawel Jakub Dawidek }
62005c91076SPawel Jakub Dawidek
62105c91076SPawel Jakub Dawidek static void
get_class(int * argc,char *** argv)62205c91076SPawel Jakub Dawidek get_class(int *argc, char ***argv)
62305c91076SPawel Jakub Dawidek {
62405c91076SPawel Jakub Dawidek
62505c91076SPawel Jakub Dawidek snprintf(comm, sizeof(comm), "%s", basename((*argv)[0]));
62605c91076SPawel Jakub Dawidek if (strcmp(comm, "geom") == 0) {
62705c91076SPawel Jakub Dawidek if (*argc < 2)
628c979e206SPawel Jakub Dawidek usage();
629687b0015SPawel Jakub Dawidek else if (*argc == 2) {
630687b0015SPawel Jakub Dawidek if (strcmp((*argv)[1], "-h") == 0 ||
631687b0015SPawel Jakub Dawidek strcmp((*argv)[1], "help") == 0) {
632c979e206SPawel Jakub Dawidek usage();
633687b0015SPawel Jakub Dawidek }
634687b0015SPawel Jakub Dawidek }
63505c91076SPawel Jakub Dawidek strlcatf(comm, sizeof(comm), " %s", (*argv)[1]);
63605c91076SPawel Jakub Dawidek class_name = (*argv)[1];
63705c91076SPawel Jakub Dawidek *argc -= 2;
63805c91076SPawel Jakub Dawidek *argv += 2;
63905c91076SPawel Jakub Dawidek } else if (*comm == 'g') {
64005c91076SPawel Jakub Dawidek class_name = comm + 1;
64105c91076SPawel Jakub Dawidek *argc -= 1;
64205c91076SPawel Jakub Dawidek *argv += 1;
64305c91076SPawel Jakub Dawidek } else {
64405c91076SPawel Jakub Dawidek errx(EXIT_FAILURE, "Invalid utility name.");
64505c91076SPawel Jakub Dawidek }
646a16f9b36SMarcel Moolenaar
64714bf405bSMarcel Moolenaar #ifndef STATIC_GEOM_CLASSES
64805c91076SPawel Jakub Dawidek load_library();
649a16f9b36SMarcel Moolenaar #else
650a16f9b36SMarcel Moolenaar if (!strcasecmp(class_name, "part")) {
651a16f9b36SMarcel Moolenaar version = &gpart_version;
652a16f9b36SMarcel Moolenaar class_commands = gpart_class_commands;
653bc69d66fSXin LI } else if (!strcasecmp(class_name, "label")) {
654bc69d66fSXin LI version = &glabel_version;
655bc69d66fSXin LI class_commands = glabel_class_commands;
656c6678483SAlexander Motin }
65714bf405bSMarcel Moolenaar #endif /* !STATIC_GEOM_CLASSES */
658a16f9b36SMarcel Moolenaar
659a16f9b36SMarcel Moolenaar set_class_name();
660e2d52f14SMark Felder
661e2d52f14SMark Felder /* If we can't load or list, it's not a class. */
6625678114cSAlexander Motin if (!std_load_available() && !std_list_available())
663112adef8SEdward Tomasz Napierala errx(EXIT_FAILURE, "Invalid class name '%s'.", class_name);
664e2d52f14SMark Felder
66505c91076SPawel Jakub Dawidek if (*argc < 1)
666c979e206SPawel Jakub Dawidek usage();
66705c91076SPawel Jakub Dawidek }
66805c91076SPawel Jakub Dawidek
6690f73f701SEdward Tomasz Napierala static struct ggeom *
find_geom_by_provider(struct gmesh * mesh,const char * name)6700f73f701SEdward Tomasz Napierala find_geom_by_provider(struct gmesh *mesh, const char *name)
6710f73f701SEdward Tomasz Napierala {
6720f73f701SEdward Tomasz Napierala struct gclass *classp;
6730f73f701SEdward Tomasz Napierala struct ggeom *gp;
6740f73f701SEdward Tomasz Napierala struct gprovider *pp;
6750f73f701SEdward Tomasz Napierala
6760f73f701SEdward Tomasz Napierala LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
6770f73f701SEdward Tomasz Napierala LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
6780f73f701SEdward Tomasz Napierala LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
6790f73f701SEdward Tomasz Napierala if (strcmp(pp->lg_name, name) == 0)
6800f73f701SEdward Tomasz Napierala return (gp);
6810f73f701SEdward Tomasz Napierala }
6820f73f701SEdward Tomasz Napierala }
6830f73f701SEdward Tomasz Napierala }
6840f73f701SEdward Tomasz Napierala
6850f73f701SEdward Tomasz Napierala return (NULL);
6860f73f701SEdward Tomasz Napierala }
6870f73f701SEdward Tomasz Napierala
6886a027382SEdward Tomasz Napierala static int
compute_tree_width_geom(struct gmesh * mesh,struct ggeom * gp,int indent)6896a027382SEdward Tomasz Napierala compute_tree_width_geom(struct gmesh *mesh, struct ggeom *gp, int indent)
6906a027382SEdward Tomasz Napierala {
6916a027382SEdward Tomasz Napierala struct gclass *classp2;
6926a027382SEdward Tomasz Napierala struct ggeom *gp2;
6936a027382SEdward Tomasz Napierala struct gconsumer *cp2;
6946a027382SEdward Tomasz Napierala struct gprovider *pp;
6956a027382SEdward Tomasz Napierala int max_width, width;
6966a027382SEdward Tomasz Napierala
6976a027382SEdward Tomasz Napierala max_width = width = indent + strlen(gp->lg_name);
6986a027382SEdward Tomasz Napierala
6996a027382SEdward Tomasz Napierala LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
7006a027382SEdward Tomasz Napierala LIST_FOREACH(classp2, &mesh->lg_class, lg_class) {
7016a027382SEdward Tomasz Napierala LIST_FOREACH(gp2, &classp2->lg_geom, lg_geom) {
7026a027382SEdward Tomasz Napierala LIST_FOREACH(cp2,
7036a027382SEdward Tomasz Napierala &gp2->lg_consumer, lg_consumer) {
7046a027382SEdward Tomasz Napierala if (pp != cp2->lg_provider)
7056a027382SEdward Tomasz Napierala continue;
7066a027382SEdward Tomasz Napierala width = compute_tree_width_geom(mesh,
7076a027382SEdward Tomasz Napierala gp2, indent + 2);
7086a027382SEdward Tomasz Napierala if (width > max_width)
7096a027382SEdward Tomasz Napierala max_width = width;
7106a027382SEdward Tomasz Napierala }
7116a027382SEdward Tomasz Napierala }
7126a027382SEdward Tomasz Napierala }
7136a027382SEdward Tomasz Napierala }
7146a027382SEdward Tomasz Napierala
7156a027382SEdward Tomasz Napierala return (max_width);
7166a027382SEdward Tomasz Napierala }
7176a027382SEdward Tomasz Napierala
7186a027382SEdward Tomasz Napierala static int
compute_tree_width(struct gmesh * mesh)7196a027382SEdward Tomasz Napierala compute_tree_width(struct gmesh *mesh)
7206a027382SEdward Tomasz Napierala {
7216a027382SEdward Tomasz Napierala struct gclass *classp;
7226a027382SEdward Tomasz Napierala struct ggeom *gp;
7236a027382SEdward Tomasz Napierala int max_width, width;
7246a027382SEdward Tomasz Napierala
7256a027382SEdward Tomasz Napierala max_width = width = 0;
7266a027382SEdward Tomasz Napierala
7276a027382SEdward Tomasz Napierala LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
7286a027382SEdward Tomasz Napierala LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
7296a027382SEdward Tomasz Napierala if (!LIST_EMPTY(&gp->lg_consumer))
7306a027382SEdward Tomasz Napierala continue;
7316a027382SEdward Tomasz Napierala width = compute_tree_width_geom(mesh, gp, 0);
7326a027382SEdward Tomasz Napierala if (width > max_width)
7336a027382SEdward Tomasz Napierala max_width = width;
7346a027382SEdward Tomasz Napierala }
7356a027382SEdward Tomasz Napierala }
7366a027382SEdward Tomasz Napierala
7376a027382SEdward Tomasz Napierala return (max_width);
7386a027382SEdward Tomasz Napierala }
7396a027382SEdward Tomasz Napierala
7406a027382SEdward Tomasz Napierala static void
show_tree_geom(struct gmesh * mesh,struct ggeom * gp,int indent,int width)7416a027382SEdward Tomasz Napierala show_tree_geom(struct gmesh *mesh, struct ggeom *gp, int indent, int width)
7426a027382SEdward Tomasz Napierala {
7436a027382SEdward Tomasz Napierala struct gclass *classp2;
7446a027382SEdward Tomasz Napierala struct ggeom *gp2;
7456a027382SEdward Tomasz Napierala struct gconsumer *cp2;
7466a027382SEdward Tomasz Napierala struct gprovider *pp;
7476a027382SEdward Tomasz Napierala
7486a027382SEdward Tomasz Napierala if (LIST_EMPTY(&gp->lg_provider)) {
7496a027382SEdward Tomasz Napierala printf("%*s%-*.*s %-*.*s\n", indent, "",
7506a027382SEdward Tomasz Napierala width - indent, width - indent, gp->lg_name,
7516a027382SEdward Tomasz Napierala GEOM_CLASS_WIDTH, GEOM_CLASS_WIDTH, gp->lg_class->lg_name);
7526a027382SEdward Tomasz Napierala return;
7536a027382SEdward Tomasz Napierala }
7546a027382SEdward Tomasz Napierala
7556a027382SEdward Tomasz Napierala LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
7566a027382SEdward Tomasz Napierala printf("%*s%-*.*s %-*.*s %s\n", indent, "",
7576a027382SEdward Tomasz Napierala width - indent, width - indent, gp->lg_name,
7586a027382SEdward Tomasz Napierala GEOM_CLASS_WIDTH, GEOM_CLASS_WIDTH, gp->lg_class->lg_name,
7596a027382SEdward Tomasz Napierala pp->lg_name);
7606a027382SEdward Tomasz Napierala
7616a027382SEdward Tomasz Napierala LIST_FOREACH(classp2, &mesh->lg_class, lg_class) {
7626a027382SEdward Tomasz Napierala LIST_FOREACH(gp2, &classp2->lg_geom, lg_geom) {
7636a027382SEdward Tomasz Napierala LIST_FOREACH(cp2,
7646a027382SEdward Tomasz Napierala &gp2->lg_consumer, lg_consumer) {
7656a027382SEdward Tomasz Napierala if (pp != cp2->lg_provider)
7666a027382SEdward Tomasz Napierala continue;
7676a027382SEdward Tomasz Napierala show_tree_geom(mesh, gp2,
7686a027382SEdward Tomasz Napierala indent + 2, width);
7696a027382SEdward Tomasz Napierala }
7706a027382SEdward Tomasz Napierala }
7716a027382SEdward Tomasz Napierala }
7726a027382SEdward Tomasz Napierala }
7736a027382SEdward Tomasz Napierala }
7746a027382SEdward Tomasz Napierala
7756a027382SEdward Tomasz Napierala static void
show_tree(void)7766a027382SEdward Tomasz Napierala show_tree(void)
7776a027382SEdward Tomasz Napierala {
7786a027382SEdward Tomasz Napierala struct gmesh mesh;
7796a027382SEdward Tomasz Napierala struct gclass *classp;
7806a027382SEdward Tomasz Napierala struct ggeom *gp;
7816a027382SEdward Tomasz Napierala int error, width;
7826a027382SEdward Tomasz Napierala
7836a027382SEdward Tomasz Napierala error = geom_gettree(&mesh);
7846a027382SEdward Tomasz Napierala if (error != 0)
7856a027382SEdward Tomasz Napierala errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
7866a027382SEdward Tomasz Napierala
7876a027382SEdward Tomasz Napierala width = compute_tree_width(&mesh);
7886a027382SEdward Tomasz Napierala
7896a027382SEdward Tomasz Napierala printf("%-*.*s %-*.*s %s\n",
7906a027382SEdward Tomasz Napierala width, width, "Geom",
7916a027382SEdward Tomasz Napierala GEOM_CLASS_WIDTH, GEOM_CLASS_WIDTH, "Class",
7926a027382SEdward Tomasz Napierala "Provider");
7936a027382SEdward Tomasz Napierala
7946a027382SEdward Tomasz Napierala LIST_FOREACH(classp, &mesh.lg_class, lg_class) {
7956a027382SEdward Tomasz Napierala LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
7966a027382SEdward Tomasz Napierala if (!LIST_EMPTY(&gp->lg_consumer))
7976a027382SEdward Tomasz Napierala continue;
7986a027382SEdward Tomasz Napierala show_tree_geom(&mesh, gp, 0, width);
7996a027382SEdward Tomasz Napierala }
8006a027382SEdward Tomasz Napierala }
8016a027382SEdward Tomasz Napierala }
8026a027382SEdward Tomasz Napierala
80305c91076SPawel Jakub Dawidek int
main(int argc,char * argv[])80405c91076SPawel Jakub Dawidek main(int argc, char *argv[])
80505c91076SPawel Jakub Dawidek {
8060f73f701SEdward Tomasz Napierala char *provider_name;
8076a027382SEdward Tomasz Napierala bool tflag;
8080f73f701SEdward Tomasz Napierala int ch;
8090f73f701SEdward Tomasz Napierala
8100f73f701SEdward Tomasz Napierala provider_name = NULL;
8116a027382SEdward Tomasz Napierala tflag = false;
8120f73f701SEdward Tomasz Napierala
8130f73f701SEdward Tomasz Napierala if (strcmp(getprogname(), "geom") == 0) {
8146a027382SEdward Tomasz Napierala while ((ch = getopt(argc, argv, "hp:t")) != -1) {
8150f73f701SEdward Tomasz Napierala switch (ch) {
8160f73f701SEdward Tomasz Napierala case 'p':
8170f73f701SEdward Tomasz Napierala provider_name = strdup(optarg);
8180f73f701SEdward Tomasz Napierala if (provider_name == NULL)
8190f73f701SEdward Tomasz Napierala err(1, "strdup");
8200f73f701SEdward Tomasz Napierala break;
8216a027382SEdward Tomasz Napierala case 't':
8226a027382SEdward Tomasz Napierala tflag = true;
8236a027382SEdward Tomasz Napierala break;
8240f73f701SEdward Tomasz Napierala case 'h':
8250f73f701SEdward Tomasz Napierala default:
8260f73f701SEdward Tomasz Napierala usage();
8270f73f701SEdward Tomasz Napierala }
8280f73f701SEdward Tomasz Napierala }
8290f73f701SEdward Tomasz Napierala
8300f73f701SEdward Tomasz Napierala /*
8310f73f701SEdward Tomasz Napierala * Don't adjust argc and argv, it would break get_class().
8320f73f701SEdward Tomasz Napierala */
8330f73f701SEdward Tomasz Napierala }
8340f73f701SEdward Tomasz Napierala
8356a027382SEdward Tomasz Napierala if (tflag && provider_name != NULL) {
8366a027382SEdward Tomasz Napierala errx(EXIT_FAILURE,
8376a027382SEdward Tomasz Napierala "At most one of -P and -t may be specified.");
8386a027382SEdward Tomasz Napierala }
8396a027382SEdward Tomasz Napierala
8400f73f701SEdward Tomasz Napierala if (provider_name != NULL) {
8410f73f701SEdward Tomasz Napierala list_one_geom_by_provider(provider_name);
8420f73f701SEdward Tomasz Napierala return (0);
8430f73f701SEdward Tomasz Napierala }
84405c91076SPawel Jakub Dawidek
8456a027382SEdward Tomasz Napierala if (tflag) {
8466a027382SEdward Tomasz Napierala show_tree();
8476a027382SEdward Tomasz Napierala return (0);
8486a027382SEdward Tomasz Napierala }
8496a027382SEdward Tomasz Napierala
85005c91076SPawel Jakub Dawidek get_class(&argc, &argv);
85105c91076SPawel Jakub Dawidek run_command(argc, argv);
85205c91076SPawel Jakub Dawidek /* NOTREACHED */
85305c91076SPawel Jakub Dawidek
85405c91076SPawel Jakub Dawidek exit(EXIT_FAILURE);
85505c91076SPawel Jakub Dawidek }
85605c91076SPawel Jakub Dawidek
85705c91076SPawel Jakub Dawidek static struct gclass *
find_class(struct gmesh * mesh,const char * name)85805c91076SPawel Jakub Dawidek find_class(struct gmesh *mesh, const char *name)
85905c91076SPawel Jakub Dawidek {
86005c91076SPawel Jakub Dawidek struct gclass *classp;
86105c91076SPawel Jakub Dawidek
86205c91076SPawel Jakub Dawidek LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
86305c91076SPawel Jakub Dawidek if (strcmp(classp->lg_name, name) == 0)
86405c91076SPawel Jakub Dawidek return (classp);
86505c91076SPawel Jakub Dawidek }
86605c91076SPawel Jakub Dawidek return (NULL);
86705c91076SPawel Jakub Dawidek }
86805c91076SPawel Jakub Dawidek
8691d723f1dSPawel Jakub Dawidek static struct ggeom *
find_geom(struct gclass * classp,const char * name)8701d723f1dSPawel Jakub Dawidek find_geom(struct gclass *classp, const char *name)
87105c91076SPawel Jakub Dawidek {
87205c91076SPawel Jakub Dawidek struct ggeom *gp;
87305c91076SPawel Jakub Dawidek
874f200cc25SRobert Wing if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
875f200cc25SRobert Wing name += sizeof(_PATH_DEV) - 1;
876f200cc25SRobert Wing
87705c91076SPawel Jakub Dawidek LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
8781d723f1dSPawel Jakub Dawidek if (strcmp(gp->lg_name, name) == 0)
8791d723f1dSPawel Jakub Dawidek return (gp);
88005c91076SPawel Jakub Dawidek }
88105c91076SPawel Jakub Dawidek return (NULL);
88205c91076SPawel Jakub Dawidek }
88305c91076SPawel Jakub Dawidek
88405c91076SPawel Jakub Dawidek static void
list_one_provider(struct gprovider * pp,const char * prefix)8852d76e2dbSPawel Jakub Dawidek list_one_provider(struct gprovider *pp, const char *prefix)
88605c91076SPawel Jakub Dawidek {
88705c91076SPawel Jakub Dawidek struct gconfig *conf;
888af565b58SPawel Jakub Dawidek char buf[5];
88905c91076SPawel Jakub Dawidek
8901d723f1dSPawel Jakub Dawidek printf("Name: %s\n", pp->lg_name);
891af565b58SPawel Jakub Dawidek humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "",
892af565b58SPawel Jakub Dawidek HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
8931d723f1dSPawel Jakub Dawidek printf("%sMediasize: %jd (%s)\n", prefix, (intmax_t)pp->lg_mediasize,
8941d723f1dSPawel Jakub Dawidek buf);
8951d723f1dSPawel Jakub Dawidek printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize);
896e192c6e8SXin LI if (pp->lg_stripesize > 0 || pp->lg_stripeoffset > 0) {
89735daa28fSXin LI printf("%sStripesize: %ju\n", prefix, pp->lg_stripesize);
89835daa28fSXin LI printf("%sStripeoffset: %ju\n", prefix, pp->lg_stripeoffset);
89935daa28fSXin LI }
9001d723f1dSPawel Jakub Dawidek printf("%sMode: %s\n", prefix, pp->lg_mode);
90105c91076SPawel Jakub Dawidek LIST_FOREACH(conf, &pp->lg_config, lg_config) {
9021d723f1dSPawel Jakub Dawidek printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val);
9031d723f1dSPawel Jakub Dawidek }
9041d723f1dSPawel Jakub Dawidek }
9051d723f1dSPawel Jakub Dawidek
9061d723f1dSPawel Jakub Dawidek static void
list_one_consumer(struct gconsumer * cp,const char * prefix)9072d76e2dbSPawel Jakub Dawidek list_one_consumer(struct gconsumer *cp, const char *prefix)
9081d723f1dSPawel Jakub Dawidek {
9091d723f1dSPawel Jakub Dawidek struct gprovider *pp;
9101d723f1dSPawel Jakub Dawidek struct gconfig *conf;
9111d723f1dSPawel Jakub Dawidek
9121d723f1dSPawel Jakub Dawidek pp = cp->lg_provider;
9131d723f1dSPawel Jakub Dawidek if (pp == NULL)
9141d723f1dSPawel Jakub Dawidek printf("[no provider]\n");
9151d723f1dSPawel Jakub Dawidek else {
9161d723f1dSPawel Jakub Dawidek char buf[5];
9171d723f1dSPawel Jakub Dawidek
9181d723f1dSPawel Jakub Dawidek printf("Name: %s\n", pp->lg_name);
9191d723f1dSPawel Jakub Dawidek humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "",
9201d723f1dSPawel Jakub Dawidek HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
9211d723f1dSPawel Jakub Dawidek printf("%sMediasize: %jd (%s)\n", prefix,
9221d723f1dSPawel Jakub Dawidek (intmax_t)pp->lg_mediasize, buf);
9231d723f1dSPawel Jakub Dawidek printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize);
92400b236aaSXin LI if (pp->lg_stripesize > 0 || pp->lg_stripeoffset > 0) {
92535daa28fSXin LI printf("%sStripesize: %ju\n", prefix, pp->lg_stripesize);
92635daa28fSXin LI printf("%sStripeoffset: %ju\n", prefix, pp->lg_stripeoffset);
92735daa28fSXin LI }
9281d723f1dSPawel Jakub Dawidek printf("%sMode: %s\n", prefix, cp->lg_mode);
9291d723f1dSPawel Jakub Dawidek }
9301d723f1dSPawel Jakub Dawidek LIST_FOREACH(conf, &cp->lg_config, lg_config) {
9311d723f1dSPawel Jakub Dawidek printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val);
9321d723f1dSPawel Jakub Dawidek }
9331d723f1dSPawel Jakub Dawidek }
9341d723f1dSPawel Jakub Dawidek
9351d723f1dSPawel Jakub Dawidek static void
list_one_geom(struct ggeom * gp)9362d76e2dbSPawel Jakub Dawidek list_one_geom(struct ggeom *gp)
9371d723f1dSPawel Jakub Dawidek {
9381d723f1dSPawel Jakub Dawidek struct gprovider *pp;
9391d723f1dSPawel Jakub Dawidek struct gconsumer *cp;
9401d723f1dSPawel Jakub Dawidek struct gconfig *conf;
9411d723f1dSPawel Jakub Dawidek unsigned n;
9421d723f1dSPawel Jakub Dawidek
9431d723f1dSPawel Jakub Dawidek printf("Geom name: %s\n", gp->lg_name);
9441d723f1dSPawel Jakub Dawidek LIST_FOREACH(conf, &gp->lg_config, lg_config) {
9451d723f1dSPawel Jakub Dawidek printf("%s: %s\n", conf->lg_name, conf->lg_val);
9461d723f1dSPawel Jakub Dawidek }
9471d723f1dSPawel Jakub Dawidek if (!LIST_EMPTY(&gp->lg_provider)) {
9481d723f1dSPawel Jakub Dawidek printf("Providers:\n");
9491d723f1dSPawel Jakub Dawidek n = 1;
9501d723f1dSPawel Jakub Dawidek LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
9511d723f1dSPawel Jakub Dawidek printf("%u. ", n++);
9522d76e2dbSPawel Jakub Dawidek list_one_provider(pp, " ");
9531d723f1dSPawel Jakub Dawidek }
9541d723f1dSPawel Jakub Dawidek }
9551d723f1dSPawel Jakub Dawidek if (!LIST_EMPTY(&gp->lg_consumer)) {
9561d723f1dSPawel Jakub Dawidek printf("Consumers:\n");
9571d723f1dSPawel Jakub Dawidek n = 1;
9581d723f1dSPawel Jakub Dawidek LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
9591d723f1dSPawel Jakub Dawidek printf("%u. ", n++);
9602d76e2dbSPawel Jakub Dawidek list_one_consumer(cp, " ");
9611d723f1dSPawel Jakub Dawidek }
96205c91076SPawel Jakub Dawidek }
96305c91076SPawel Jakub Dawidek printf("\n");
96405c91076SPawel Jakub Dawidek }
96505c91076SPawel Jakub Dawidek
966b70eccf3SPawel Jakub Dawidek static void
list_one_geom_by_provider(const char * provider_name)9670f73f701SEdward Tomasz Napierala list_one_geom_by_provider(const char *provider_name)
9680f73f701SEdward Tomasz Napierala {
9690f73f701SEdward Tomasz Napierala struct gmesh mesh;
9700f73f701SEdward Tomasz Napierala struct ggeom *gp;
9710f73f701SEdward Tomasz Napierala int error;
9720f73f701SEdward Tomasz Napierala
9730f73f701SEdward Tomasz Napierala error = geom_gettree(&mesh);
9740f73f701SEdward Tomasz Napierala if (error != 0)
9750f73f701SEdward Tomasz Napierala errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
9760f73f701SEdward Tomasz Napierala
9770f73f701SEdward Tomasz Napierala gp = find_geom_by_provider(&mesh, provider_name);
9780f73f701SEdward Tomasz Napierala if (gp == NULL)
9790f73f701SEdward Tomasz Napierala errx(EXIT_FAILURE, "Cannot find provider '%s'.", provider_name);
9800f73f701SEdward Tomasz Napierala
9810f73f701SEdward Tomasz Napierala printf("Geom class: %s\n", gp->lg_class->lg_name);
9820f73f701SEdward Tomasz Napierala list_one_geom(gp);
9830f73f701SEdward Tomasz Napierala }
9840f73f701SEdward Tomasz Napierala
9850f73f701SEdward Tomasz Napierala static void
std_help(struct gctl_req * req __unused,unsigned flags __unused)986b70eccf3SPawel Jakub Dawidek std_help(struct gctl_req *req __unused, unsigned flags __unused)
987b70eccf3SPawel Jakub Dawidek {
988b70eccf3SPawel Jakub Dawidek
989c979e206SPawel Jakub Dawidek usage();
990b70eccf3SPawel Jakub Dawidek }
991b70eccf3SPawel Jakub Dawidek
99205c91076SPawel Jakub Dawidek static int
std_list_available(void)99305c91076SPawel Jakub Dawidek std_list_available(void)
99405c91076SPawel Jakub Dawidek {
99505c91076SPawel Jakub Dawidek struct gmesh mesh;
99605c91076SPawel Jakub Dawidek struct gclass *classp;
99705c91076SPawel Jakub Dawidek int error;
99805c91076SPawel Jakub Dawidek
9997f16b501SAlexander Motin error = geom_gettree_geom(&mesh, gclass_name, "", 0);
10006fc60008SPawel Jakub Dawidek if (error != 0)
10016fc60008SPawel Jakub Dawidek errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
100205c91076SPawel Jakub Dawidek classp = find_class(&mesh, gclass_name);
100305c91076SPawel Jakub Dawidek geom_deletetree(&mesh);
100405c91076SPawel Jakub Dawidek if (classp != NULL)
100505c91076SPawel Jakub Dawidek return (1);
100605c91076SPawel Jakub Dawidek return (0);
100705c91076SPawel Jakub Dawidek }
100805c91076SPawel Jakub Dawidek
100905c91076SPawel Jakub Dawidek static void
std_list(struct gctl_req * req,unsigned flags __unused)101005c91076SPawel Jakub Dawidek std_list(struct gctl_req *req, unsigned flags __unused)
101105c91076SPawel Jakub Dawidek {
101205c91076SPawel Jakub Dawidek struct gmesh mesh;
101305c91076SPawel Jakub Dawidek struct gclass *classp;
10141d723f1dSPawel Jakub Dawidek struct ggeom *gp;
1015f13942a7SPawel Jakub Dawidek const char *name;
101683d165c1SAlexander Motin int all, error, i, nargs;
101705c91076SPawel Jakub Dawidek
10187f16b501SAlexander Motin nargs = gctl_get_int(req, "nargs");
10197f16b501SAlexander Motin if (nargs == 1) {
10207f16b501SAlexander Motin error = geom_gettree_geom(&mesh, gclass_name,
10217f16b501SAlexander Motin gctl_get_ascii(req, "arg0"), 1);
10227f16b501SAlexander Motin } else
102305c91076SPawel Jakub Dawidek error = geom_gettree(&mesh);
10246fc60008SPawel Jakub Dawidek if (error != 0)
10256fc60008SPawel Jakub Dawidek errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
102605c91076SPawel Jakub Dawidek classp = find_class(&mesh, gclass_name);
102705c91076SPawel Jakub Dawidek if (classp == NULL) {
102805c91076SPawel Jakub Dawidek geom_deletetree(&mesh);
1029112adef8SEdward Tomasz Napierala errx(EXIT_FAILURE, "Class '%s' not found.", gclass_name);
103005c91076SPawel Jakub Dawidek }
103183d165c1SAlexander Motin all = gctl_get_int(req, "all");
1032f13942a7SPawel Jakub Dawidek if (nargs > 0) {
1033f13942a7SPawel Jakub Dawidek for (i = 0; i < nargs; i++) {
1034f13942a7SPawel Jakub Dawidek name = gctl_get_ascii(req, "arg%d", i);
10351d723f1dSPawel Jakub Dawidek gp = find_geom(classp, name);
1036112adef8SEdward Tomasz Napierala if (gp == NULL) {
1037112adef8SEdward Tomasz Napierala errx(EXIT_FAILURE, "Class '%s' does not have "
1038112adef8SEdward Tomasz Napierala "an instance named '%s'.",
1039112adef8SEdward Tomasz Napierala gclass_name, name);
1040112adef8SEdward Tomasz Napierala }
104183d165c1SAlexander Motin list_one_geom(gp);
104205c91076SPawel Jakub Dawidek }
104305c91076SPawel Jakub Dawidek } else {
104405c91076SPawel Jakub Dawidek LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
104583d165c1SAlexander Motin if (LIST_EMPTY(&gp->lg_provider) && !all)
10465fd66a44SPawel Jakub Dawidek continue;
10472d76e2dbSPawel Jakub Dawidek list_one_geom(gp);
104805c91076SPawel Jakub Dawidek }
104905c91076SPawel Jakub Dawidek }
105005c91076SPawel Jakub Dawidek geom_deletetree(&mesh);
105105c91076SPawel Jakub Dawidek }
105205c91076SPawel Jakub Dawidek
105305c91076SPawel Jakub Dawidek static int
std_status_available(void)105418ee8840SPawel Jakub Dawidek std_status_available(void)
105518ee8840SPawel Jakub Dawidek {
105618ee8840SPawel Jakub Dawidek
105718ee8840SPawel Jakub Dawidek /* 'status' command is available when 'list' command is. */
105818ee8840SPawel Jakub Dawidek return (std_list_available());
105918ee8840SPawel Jakub Dawidek }
106018ee8840SPawel Jakub Dawidek
106118ee8840SPawel Jakub Dawidek static void
status_update_len(struct ggeom * gp,int * name_len,int * status_len)1062da80913dSPawel Jakub Dawidek status_update_len(struct ggeom *gp, int *name_len, int *status_len)
106318ee8840SPawel Jakub Dawidek {
106418ee8840SPawel Jakub Dawidek struct gconfig *conf;
1065da80913dSPawel Jakub Dawidek int len;
106618ee8840SPawel Jakub Dawidek
106718ee8840SPawel Jakub Dawidek assert(gp != NULL);
106818ee8840SPawel Jakub Dawidek assert(name_len != NULL);
106918ee8840SPawel Jakub Dawidek assert(status_len != NULL);
107018ee8840SPawel Jakub Dawidek
107118ee8840SPawel Jakub Dawidek len = strlen(gp->lg_name);
107218ee8840SPawel Jakub Dawidek if (*name_len < len)
107318ee8840SPawel Jakub Dawidek *name_len = len;
107418ee8840SPawel Jakub Dawidek LIST_FOREACH(conf, &gp->lg_config, lg_config) {
107518ee8840SPawel Jakub Dawidek if (strcasecmp(conf->lg_name, "state") == 0) {
107618ee8840SPawel Jakub Dawidek len = strlen(conf->lg_val);
107718ee8840SPawel Jakub Dawidek if (*status_len < len)
107818ee8840SPawel Jakub Dawidek *status_len = len;
107918ee8840SPawel Jakub Dawidek }
108018ee8840SPawel Jakub Dawidek }
108118ee8840SPawel Jakub Dawidek }
108218ee8840SPawel Jakub Dawidek
108383d165c1SAlexander Motin static void
status_update_len_prs(struct ggeom * gp,int * name_len,int * status_len)108483d165c1SAlexander Motin status_update_len_prs(struct ggeom *gp, int *name_len, int *status_len)
108583d165c1SAlexander Motin {
108683d165c1SAlexander Motin struct gprovider *pp;
108783d165c1SAlexander Motin struct gconfig *conf;
108883d165c1SAlexander Motin int len, glen;
108983d165c1SAlexander Motin
109083d165c1SAlexander Motin assert(gp != NULL);
109183d165c1SAlexander Motin assert(name_len != NULL);
109283d165c1SAlexander Motin assert(status_len != NULL);
109383d165c1SAlexander Motin
109483d165c1SAlexander Motin glen = 0;
109583d165c1SAlexander Motin LIST_FOREACH(conf, &gp->lg_config, lg_config) {
109683d165c1SAlexander Motin if (strcasecmp(conf->lg_name, "state") == 0) {
109783d165c1SAlexander Motin glen = strlen(conf->lg_val);
109883d165c1SAlexander Motin break;
109983d165c1SAlexander Motin }
110083d165c1SAlexander Motin }
110183d165c1SAlexander Motin LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
110283d165c1SAlexander Motin len = strlen(pp->lg_name);
110383d165c1SAlexander Motin if (*name_len < len)
110483d165c1SAlexander Motin *name_len = len;
110583d165c1SAlexander Motin len = glen;
110683d165c1SAlexander Motin LIST_FOREACH(conf, &pp->lg_config, lg_config) {
110783d165c1SAlexander Motin if (strcasecmp(conf->lg_name, "state") == 0) {
110883d165c1SAlexander Motin len = strlen(conf->lg_val);
110983d165c1SAlexander Motin break;
111083d165c1SAlexander Motin }
111183d165c1SAlexander Motin }
111283d165c1SAlexander Motin if (*status_len < len)
111383d165c1SAlexander Motin *status_len = len;
111483d165c1SAlexander Motin }
111583d165c1SAlexander Motin }
111683d165c1SAlexander Motin
1117ba6821f0SPawel Jakub Dawidek static char *
status_one_consumer(struct gconsumer * cp)111818ee8840SPawel Jakub Dawidek status_one_consumer(struct gconsumer *cp)
111918ee8840SPawel Jakub Dawidek {
1120ba6821f0SPawel Jakub Dawidek static char buf[256];
112118ee8840SPawel Jakub Dawidek struct gprovider *pp;
112218ee8840SPawel Jakub Dawidek struct gconfig *conf;
112383d165c1SAlexander Motin const char *state, *syncr;
112418ee8840SPawel Jakub Dawidek
112518ee8840SPawel Jakub Dawidek pp = cp->lg_provider;
112618ee8840SPawel Jakub Dawidek if (pp == NULL)
1127ba6821f0SPawel Jakub Dawidek return (NULL);
112883d165c1SAlexander Motin state = NULL;
112983d165c1SAlexander Motin syncr = NULL;
113018ee8840SPawel Jakub Dawidek LIST_FOREACH(conf, &cp->lg_config, lg_config) {
113183d165c1SAlexander Motin if (strcasecmp(conf->lg_name, "state") == 0)
113283d165c1SAlexander Motin state = conf->lg_val;
113318ee8840SPawel Jakub Dawidek if (strcasecmp(conf->lg_name, "synchronized") == 0)
113483d165c1SAlexander Motin syncr = conf->lg_val;
113518ee8840SPawel Jakub Dawidek }
113683d165c1SAlexander Motin if (state == NULL && syncr == NULL)
1137ba6821f0SPawel Jakub Dawidek snprintf(buf, sizeof(buf), "%s", pp->lg_name);
113883d165c1SAlexander Motin else if (state != NULL && syncr != NULL) {
113983d165c1SAlexander Motin snprintf(buf, sizeof(buf), "%s (%s, %s)", pp->lg_name,
114083d165c1SAlexander Motin state, syncr);
114183d165c1SAlexander Motin } else {
1142ba6821f0SPawel Jakub Dawidek snprintf(buf, sizeof(buf), "%s (%s)", pp->lg_name,
114383d165c1SAlexander Motin state ? state : syncr);
1144ba6821f0SPawel Jakub Dawidek }
1145ba6821f0SPawel Jakub Dawidek return (buf);
114618ee8840SPawel Jakub Dawidek }
114718ee8840SPawel Jakub Dawidek
114818ee8840SPawel Jakub Dawidek static void
status_one_geom(struct ggeom * gp,int script,int name_len,int status_len)1149ba6821f0SPawel Jakub Dawidek status_one_geom(struct ggeom *gp, int script, int name_len, int status_len)
115018ee8840SPawel Jakub Dawidek {
115118ee8840SPawel Jakub Dawidek struct gconsumer *cp;
115218ee8840SPawel Jakub Dawidek struct gconfig *conf;
1153ba6821f0SPawel Jakub Dawidek const char *name, *status, *component;
1154ba6821f0SPawel Jakub Dawidek int gotone;
115518ee8840SPawel Jakub Dawidek
115618ee8840SPawel Jakub Dawidek name = gp->lg_name;
115783d165c1SAlexander Motin status = "N/A";
115818ee8840SPawel Jakub Dawidek LIST_FOREACH(conf, &gp->lg_config, lg_config) {
115983d165c1SAlexander Motin if (strcasecmp(conf->lg_name, "state") == 0) {
116083d165c1SAlexander Motin status = conf->lg_val;
116118ee8840SPawel Jakub Dawidek break;
116218ee8840SPawel Jakub Dawidek }
116383d165c1SAlexander Motin }
1164ba6821f0SPawel Jakub Dawidek gotone = 0;
116518ee8840SPawel Jakub Dawidek LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
1166ba6821f0SPawel Jakub Dawidek component = status_one_consumer(cp);
1167ba6821f0SPawel Jakub Dawidek if (component == NULL)
1168ba6821f0SPawel Jakub Dawidek continue;
1169ba6821f0SPawel Jakub Dawidek gotone = 1;
1170ba6821f0SPawel Jakub Dawidek printf("%*s %*s %s\n", name_len, name, status_len, status,
1171ba6821f0SPawel Jakub Dawidek component);
1172ba6821f0SPawel Jakub Dawidek if (!script)
1173ba6821f0SPawel Jakub Dawidek name = status = "";
117418ee8840SPawel Jakub Dawidek }
1175ba6821f0SPawel Jakub Dawidek if (!gotone) {
1176ba6821f0SPawel Jakub Dawidek printf("%*s %*s %s\n", name_len, name, status_len, status,
1177ba6821f0SPawel Jakub Dawidek "N/A");
1178ba6821f0SPawel Jakub Dawidek }
117918ee8840SPawel Jakub Dawidek }
118018ee8840SPawel Jakub Dawidek
118118ee8840SPawel Jakub Dawidek static void
status_one_geom_prs(struct ggeom * gp,int script,int name_len,int status_len)118283d165c1SAlexander Motin status_one_geom_prs(struct ggeom *gp, int script, int name_len, int status_len)
118383d165c1SAlexander Motin {
118483d165c1SAlexander Motin struct gprovider *pp;
118583d165c1SAlexander Motin struct gconsumer *cp;
118683d165c1SAlexander Motin struct gconfig *conf;
118783d165c1SAlexander Motin const char *name, *status, *component;
118883d165c1SAlexander Motin int gotone;
118983d165c1SAlexander Motin
119083d165c1SAlexander Motin LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
119183d165c1SAlexander Motin name = pp->lg_name;
119283d165c1SAlexander Motin status = "N/A";
119383d165c1SAlexander Motin LIST_FOREACH(conf, &gp->lg_config, lg_config) {
119483d165c1SAlexander Motin if (strcasecmp(conf->lg_name, "state") == 0) {
119583d165c1SAlexander Motin status = conf->lg_val;
119683d165c1SAlexander Motin break;
119783d165c1SAlexander Motin }
119883d165c1SAlexander Motin }
119983d165c1SAlexander Motin LIST_FOREACH(conf, &pp->lg_config, lg_config) {
120083d165c1SAlexander Motin if (strcasecmp(conf->lg_name, "state") == 0) {
120183d165c1SAlexander Motin status = conf->lg_val;
120283d165c1SAlexander Motin break;
120383d165c1SAlexander Motin }
120483d165c1SAlexander Motin }
120583d165c1SAlexander Motin gotone = 0;
120683d165c1SAlexander Motin LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
120783d165c1SAlexander Motin component = status_one_consumer(cp);
120883d165c1SAlexander Motin if (component == NULL)
120983d165c1SAlexander Motin continue;
121083d165c1SAlexander Motin gotone = 1;
121183d165c1SAlexander Motin printf("%*s %*s %s\n", name_len, name,
121283d165c1SAlexander Motin status_len, status, component);
121383d165c1SAlexander Motin if (!script)
121483d165c1SAlexander Motin name = status = "";
121583d165c1SAlexander Motin }
121683d165c1SAlexander Motin if (!gotone) {
121783d165c1SAlexander Motin printf("%*s %*s %s\n", name_len, name,
121883d165c1SAlexander Motin status_len, status, "N/A");
121983d165c1SAlexander Motin }
122083d165c1SAlexander Motin }
122183d165c1SAlexander Motin }
122283d165c1SAlexander Motin
122383d165c1SAlexander Motin static void
std_status(struct gctl_req * req,unsigned flags __unused)122418ee8840SPawel Jakub Dawidek std_status(struct gctl_req *req, unsigned flags __unused)
122518ee8840SPawel Jakub Dawidek {
122618ee8840SPawel Jakub Dawidek struct gmesh mesh;
122718ee8840SPawel Jakub Dawidek struct gclass *classp;
122818ee8840SPawel Jakub Dawidek struct ggeom *gp;
1229f13942a7SPawel Jakub Dawidek const char *name;
1230da80913dSPawel Jakub Dawidek int name_len, status_len;
123183d165c1SAlexander Motin int all, error, geoms, i, n, nargs, script;
123218ee8840SPawel Jakub Dawidek
123318ee8840SPawel Jakub Dawidek error = geom_gettree(&mesh);
12346fc60008SPawel Jakub Dawidek if (error != 0)
12356fc60008SPawel Jakub Dawidek errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
123618ee8840SPawel Jakub Dawidek classp = find_class(&mesh, gclass_name);
12375d47cd0fSPawel Jakub Dawidek if (classp == NULL)
12385d47cd0fSPawel Jakub Dawidek errx(EXIT_FAILURE, "Class %s not found.", gclass_name);
1239f13942a7SPawel Jakub Dawidek nargs = gctl_get_int(req, "nargs");
124083d165c1SAlexander Motin all = gctl_get_int(req, "all");
124183d165c1SAlexander Motin geoms = gctl_get_int(req, "geoms");
1242f13942a7SPawel Jakub Dawidek script = gctl_get_int(req, "script");
124383d165c1SAlexander Motin if (script) {
124483d165c1SAlexander Motin name_len = 0;
124583d165c1SAlexander Motin status_len = 0;
124683d165c1SAlexander Motin } else {
124718ee8840SPawel Jakub Dawidek name_len = strlen("Name");
124818ee8840SPawel Jakub Dawidek status_len = strlen("Status");
124983d165c1SAlexander Motin }
1250f13942a7SPawel Jakub Dawidek if (nargs > 0) {
1251f13942a7SPawel Jakub Dawidek for (i = 0, n = 0; i < nargs; i++) {
1252f13942a7SPawel Jakub Dawidek name = gctl_get_ascii(req, "arg%d", i);
125318ee8840SPawel Jakub Dawidek gp = find_geom(classp, name);
125418ee8840SPawel Jakub Dawidek if (gp == NULL)
12555d47cd0fSPawel Jakub Dawidek errx(EXIT_FAILURE, "No such geom: %s.", name);
125683d165c1SAlexander Motin if (geoms) {
125783d165c1SAlexander Motin status_update_len(gp,
125883d165c1SAlexander Motin &name_len, &status_len);
125983d165c1SAlexander Motin } else {
126083d165c1SAlexander Motin status_update_len_prs(gp,
126183d165c1SAlexander Motin &name_len, &status_len);
126218ee8840SPawel Jakub Dawidek }
126383d165c1SAlexander Motin n++;
126418ee8840SPawel Jakub Dawidek }
126518ee8840SPawel Jakub Dawidek if (n == 0)
126618ee8840SPawel Jakub Dawidek goto end;
126718ee8840SPawel Jakub Dawidek } else {
1268f13942a7SPawel Jakub Dawidek n = 0;
126918ee8840SPawel Jakub Dawidek LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
127083d165c1SAlexander Motin if (LIST_EMPTY(&gp->lg_provider) && !all)
127118ee8840SPawel Jakub Dawidek continue;
127283d165c1SAlexander Motin if (geoms) {
127383d165c1SAlexander Motin status_update_len(gp,
127483d165c1SAlexander Motin &name_len, &status_len);
127583d165c1SAlexander Motin } else {
127683d165c1SAlexander Motin status_update_len_prs(gp,
127783d165c1SAlexander Motin &name_len, &status_len);
127883d165c1SAlexander Motin }
127918ee8840SPawel Jakub Dawidek n++;
128018ee8840SPawel Jakub Dawidek }
128118ee8840SPawel Jakub Dawidek if (n == 0)
128218ee8840SPawel Jakub Dawidek goto end;
128318ee8840SPawel Jakub Dawidek }
1284f13942a7SPawel Jakub Dawidek if (!script) {
1285da80913dSPawel Jakub Dawidek printf("%*s %*s %s\n", name_len, "Name", status_len, "Status",
128618ee8840SPawel Jakub Dawidek "Components");
1287ba6821f0SPawel Jakub Dawidek }
1288f13942a7SPawel Jakub Dawidek if (nargs > 0) {
1289f13942a7SPawel Jakub Dawidek for (i = 0; i < nargs; i++) {
1290f13942a7SPawel Jakub Dawidek name = gctl_get_ascii(req, "arg%d", i);
129118ee8840SPawel Jakub Dawidek gp = find_geom(classp, name);
129283d165c1SAlexander Motin if (gp == NULL)
129383d165c1SAlexander Motin continue;
129483d165c1SAlexander Motin if (geoms) {
1295f13942a7SPawel Jakub Dawidek status_one_geom(gp, script, name_len,
1296ba6821f0SPawel Jakub Dawidek status_len);
129783d165c1SAlexander Motin } else {
129883d165c1SAlexander Motin status_one_geom_prs(gp, script, name_len,
129983d165c1SAlexander Motin status_len);
130018ee8840SPawel Jakub Dawidek }
130118ee8840SPawel Jakub Dawidek }
130218ee8840SPawel Jakub Dawidek } else {
130318ee8840SPawel Jakub Dawidek LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
130483d165c1SAlexander Motin if (LIST_EMPTY(&gp->lg_provider) && !all)
130518ee8840SPawel Jakub Dawidek continue;
130683d165c1SAlexander Motin if (geoms) {
130783d165c1SAlexander Motin status_one_geom(gp, script, name_len,
130883d165c1SAlexander Motin status_len);
130983d165c1SAlexander Motin } else {
131083d165c1SAlexander Motin status_one_geom_prs(gp, script, name_len,
131183d165c1SAlexander Motin status_len);
131283d165c1SAlexander Motin }
131318ee8840SPawel Jakub Dawidek }
131418ee8840SPawel Jakub Dawidek }
131518ee8840SPawel Jakub Dawidek end:
131618ee8840SPawel Jakub Dawidek geom_deletetree(&mesh);
131718ee8840SPawel Jakub Dawidek }
131818ee8840SPawel Jakub Dawidek
131918ee8840SPawel Jakub Dawidek static int
std_load_available(void)132005c91076SPawel Jakub Dawidek std_load_available(void)
132105c91076SPawel Jakub Dawidek {
132205c91076SPawel Jakub Dawidek char name[MAXPATHLEN], paths[MAXPATHLEN * 8], *p;
132305c91076SPawel Jakub Dawidek struct stat sb;
132405c91076SPawel Jakub Dawidek size_t len;
132505c91076SPawel Jakub Dawidek
132605c91076SPawel Jakub Dawidek snprintf(name, sizeof(name), "g_%s", class_name);
132705c91076SPawel Jakub Dawidek /*
13285678114cSAlexander Motin * If already in kernel, "load" command is NOP.
132905c91076SPawel Jakub Dawidek */
133005c91076SPawel Jakub Dawidek if (modfind(name) >= 0)
13315678114cSAlexander Motin return (1);
133205c91076SPawel Jakub Dawidek bzero(paths, sizeof(paths));
133305c91076SPawel Jakub Dawidek len = sizeof(paths);
133405c91076SPawel Jakub Dawidek if (sysctlbyname("kern.module_path", paths, &len, NULL, 0) < 0)
133505c91076SPawel Jakub Dawidek err(EXIT_FAILURE, "sysctl(kern.module_path)");
133605c91076SPawel Jakub Dawidek for (p = strtok(paths, ";"); p != NULL; p = strtok(NULL, ";")) {
133705c91076SPawel Jakub Dawidek snprintf(name, sizeof(name), "%s/geom_%s.ko", p, class_name);
133805c91076SPawel Jakub Dawidek /*
133905c91076SPawel Jakub Dawidek * If geom_<name>.ko file exists, "load" command is available.
134005c91076SPawel Jakub Dawidek */
134105c91076SPawel Jakub Dawidek if (stat(name, &sb) == 0)
134205c91076SPawel Jakub Dawidek return (1);
134305c91076SPawel Jakub Dawidek }
134405c91076SPawel Jakub Dawidek return (0);
134505c91076SPawel Jakub Dawidek }
134605c91076SPawel Jakub Dawidek
134705c91076SPawel Jakub Dawidek static void
std_load(struct gctl_req * req __unused,unsigned flags)134805c91076SPawel Jakub Dawidek std_load(struct gctl_req *req __unused, unsigned flags)
134905c91076SPawel Jakub Dawidek {
135005c91076SPawel Jakub Dawidek
135105c91076SPawel Jakub Dawidek /*
135205c91076SPawel Jakub Dawidek * Do nothing special here, because of G_FLAG_LOADKLD flag,
135305c91076SPawel Jakub Dawidek * module is already loaded.
135405c91076SPawel Jakub Dawidek */
135505c91076SPawel Jakub Dawidek if ((flags & G_FLAG_VERBOSE) != 0)
135605c91076SPawel Jakub Dawidek printf("Module available.\n");
135705c91076SPawel Jakub Dawidek }
135805c91076SPawel Jakub Dawidek
135905c91076SPawel Jakub Dawidek static int
std_unload_available(void)136005c91076SPawel Jakub Dawidek std_unload_available(void)
136105c91076SPawel Jakub Dawidek {
136205c91076SPawel Jakub Dawidek char name[64];
136305c91076SPawel Jakub Dawidek int id;
136405c91076SPawel Jakub Dawidek
136505c91076SPawel Jakub Dawidek snprintf(name, sizeof(name), "geom_%s", class_name);
136605c91076SPawel Jakub Dawidek id = kldfind(name);
136705c91076SPawel Jakub Dawidek if (id >= 0)
136805c91076SPawel Jakub Dawidek return (1);
136905c91076SPawel Jakub Dawidek return (0);
137005c91076SPawel Jakub Dawidek }
137105c91076SPawel Jakub Dawidek
137205c91076SPawel Jakub Dawidek static void
std_unload(struct gctl_req * req,unsigned flags __unused)137305c91076SPawel Jakub Dawidek std_unload(struct gctl_req *req, unsigned flags __unused)
137405c91076SPawel Jakub Dawidek {
137505c91076SPawel Jakub Dawidek char name[64];
137605c91076SPawel Jakub Dawidek int id;
137705c91076SPawel Jakub Dawidek
137805c91076SPawel Jakub Dawidek snprintf(name, sizeof(name), "geom_%s", class_name);
137905c91076SPawel Jakub Dawidek id = kldfind(name);
138005c91076SPawel Jakub Dawidek if (id < 0) {
138105c91076SPawel Jakub Dawidek gctl_error(req, "Could not find module: %s.", strerror(errno));
138205c91076SPawel Jakub Dawidek return;
138305c91076SPawel Jakub Dawidek }
138405c91076SPawel Jakub Dawidek if (kldunload(id) < 0) {
138505c91076SPawel Jakub Dawidek gctl_error(req, "Could not unload module: %s.",
138605c91076SPawel Jakub Dawidek strerror(errno));
138705c91076SPawel Jakub Dawidek return;
138805c91076SPawel Jakub Dawidek }
138905c91076SPawel Jakub Dawidek }
139005c91076SPawel Jakub Dawidek
139105c91076SPawel Jakub Dawidek static int
std_available(const char * name)139205c91076SPawel Jakub Dawidek std_available(const char *name)
139305c91076SPawel Jakub Dawidek {
139405c91076SPawel Jakub Dawidek
1395b70eccf3SPawel Jakub Dawidek if (strcmp(name, "help") == 0)
1396b70eccf3SPawel Jakub Dawidek return (1);
1397b70eccf3SPawel Jakub Dawidek else if (strcmp(name, "list") == 0)
139805c91076SPawel Jakub Dawidek return (std_list_available());
139918ee8840SPawel Jakub Dawidek else if (strcmp(name, "status") == 0)
140018ee8840SPawel Jakub Dawidek return (std_status_available());
140105c91076SPawel Jakub Dawidek else if (strcmp(name, "load") == 0)
140205c91076SPawel Jakub Dawidek return (std_load_available());
140305c91076SPawel Jakub Dawidek else if (strcmp(name, "unload") == 0)
140405c91076SPawel Jakub Dawidek return (std_unload_available());
140505c91076SPawel Jakub Dawidek else
140605c91076SPawel Jakub Dawidek assert(!"Unknown standard command.");
140705c91076SPawel Jakub Dawidek return (0);
140805c91076SPawel Jakub Dawidek }
1409