aboutsummaryrefslogtreecommitdiff
path: root/sbin/geom/core/geom.c
blob: 0202be9a063e3aa19b995153b450815d236b84e6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (c) 2004-2009 Pawel Jakub Dawidek <pjd@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/linker.h>
#include <sys/module.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <paths.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <libgen.h>
#include <libutil.h>
#include <inttypes.h>
#include <dlfcn.h>
#include <assert.h>
#include <libgeom.h>
#include <geom.h>

#include "misc/subr.h"

#ifdef STATIC_GEOM_CLASSES
extern uint32_t gpart_version;
extern struct g_command gpart_class_commands[];
extern uint32_t glabel_version;
extern struct g_command glabel_class_commands[];
#endif

static char comm[MAXPATHLEN], *class_name = NULL, *gclass_name = NULL;
static uint32_t *version = NULL;
static int verbose = 0;
static struct g_command *class_commands = NULL;

#define	GEOM_CLASS_CMDS		0x01
#define	GEOM_STD_CMDS		0x02

#define	GEOM_CLASS_WIDTH	10

static struct g_command *find_command(const char *cmdstr, int flags);
static void list_one_geom_by_provider(const char *provider_name);
static int std_available(const char *name);

static void std_help(struct gctl_req *req, unsigned flags);
static void std_list(struct gctl_req *req, unsigned flags);
static void std_status(struct gctl_req *req, unsigned flags);
static void std_load(struct gctl_req *req, unsigned flags);
static void std_unload(struct gctl_req *req, unsigned flags);

static struct g_command std_commands[] = {
	{ "help", 0, std_help, G_NULL_OPTS, NULL },
	{ "list", 0, std_list,
	    {
		{ 'a', "all", NULL, G_TYPE_BOOL },
		G_OPT_SENTINEL
	    },
	    "[-a] [name ...]"
	},
	{ "status", 0, std_status,
	    {
		{ 'a', "all", NULL, G_TYPE_BOOL },
		{ 'g', "geoms", NULL, G_TYPE_BOOL },
		{ 's', "script", NULL, G_TYPE_BOOL },
		G_OPT_SENTINEL
	    },
	    "[-ags] [name ...]"
	},
	{ "load", G_FLAG_VERBOSE | G_FLAG_LOADKLD, std_load, G_NULL_OPTS,
	    NULL },
	{ "unload", G_FLAG_VERBOSE, std_unload, G_NULL_OPTS, NULL },
	G_CMD_SENTINEL
};

static void
usage_command(struct g_command *cmd, const char *prefix)
{
	struct g_option *opt;
	unsigned i;

	if (cmd->gc_usage != NULL) {
		char *pos, *ptr, *sptr;

		sptr = ptr = strdup(cmd->gc_usage);
		while ((pos = strsep(&ptr, "\n")) != NULL) {
			if (*pos == '\0')
				continue;
			fprintf(stderr, "%s %s %s %s\n", prefix, comm,
			    cmd->gc_name, pos);
		}
		free(sptr);
		return;
	}

	fprintf(stderr, "%s %s %s", prefix, comm, cmd->gc_name);
	if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0)
		fprintf(stderr, " [-v]");
	for (i = 0; ; i++) {
		opt = &cmd->gc_options[i];
		if (opt->go_name == NULL)
			break;
		if (opt->go_val != NULL || G_OPT_TYPE(opt) == G_TYPE_BOOL)
			fprintf(stderr, " [");
		else
			fprintf(stderr, " ");
		fprintf(stderr, "-%c", opt->go_char);
		if (G_OPT_TYPE(opt) != G_TYPE_BOOL)
			fprintf(stderr, " %s", opt->go_name);
		if (opt->go_val != NULL || G_OPT_TYPE(opt) == G_TYPE_BOOL)
			fprintf(stderr, "]");
	}
	fprintf(stderr, "\n");
}

static void
usage(void)
{

	if (class_name == NULL) {
		fprintf(stderr, "usage: geom <class> <command> [options]\n");
		fprintf(stderr, "       geom -p <provider-name>\n");
		fprintf(stderr, "       geom -t\n");
		exit(EXIT_FAILURE);
	} else {
		struct g_command *cmd;
		const char *prefix;
		unsigned i;

		prefix = "usage:";
		if (class_commands != NULL) {
			for (i = 0; ; i++) {
				cmd = &class_commands[i];
				if (cmd->gc_name == NULL)
					break;
				usage_command(cmd, prefix);
				prefix = "      ";
			}
		}
		for (i = 0; ; i++) {
			cmd = &std_commands[i];
			if (cmd->gc_name == NULL)
				break;
			/*
			 * If class defines command, which has the same name as
			 * standard command, skip it, because it was already
			 * shown on usage().
			 */
			if (find_command(cmd->gc_name, GEOM_CLASS_CMDS) != NULL)
				continue;
			usage_command(cmd, prefix);
			prefix = "      ";
		}
		exit(EXIT_FAILURE);
	}
}

static void
load_module(void)
{
	char name1[64], name2[64];

	snprintf(name1, sizeof(name1), "g_%s", class_name);
	snprintf(name2, sizeof(name2), "geom_%s", class_name);
	if (modfind(name1) < 0) {
		/* Not present in kernel, try loading it. */
		if (kldload(name2) < 0 || modfind(name1) < 0) {
			if (errno != EEXIST) {
				err(EXIT_FAILURE, "cannot load %s", name2);
			}
		}
	}
}

static int
strlcatf(char *str, size_t size, const char *format, ...)
{
	size_t len;
	va_list ap;
	int ret;

	len = strlen(str);
	str += len;
	size -= len;

	va_start(ap, format);
	ret = vsnprintf(str, size, format, ap);
	va_end(ap);

	return (ret);
}

/*
 * Find given option in options available for given command.
 */
static struct g_option *
find_option(struct g_command *cmd, char ch)
{
	struct g_option *opt;
	unsigned i;

	for (i = 0; ; i++) {
		opt = &cmd->gc_options[i];
		if (opt->go_name == NULL)
			return (NULL);
		if (opt->go_char == ch)
			return (opt);
	}
	/* NOTREACHED */
	return (NULL);
}

/*
 * Add given option to gctl_req.
 */
static void
set_option(struct gctl_req *req, struct g_option *opt, const char *val)
{
	const char *optname;
	uint64_t number;
	void *ptr;

	if (G_OPT_ISMULTI(opt)) {
		size_t optnamesize;

		if (G_OPT_NUM(opt) == UCHAR_MAX)
			errx(EXIT_FAILURE, "Too many -%c options.", opt->go_char);

		/*
		 * Base option name length plus 3 bytes for option number
		 * (max. 255 options) plus 1 byte for terminating '\0'.
		 */
		optnamesize = strlen(opt->go_name) + 3 + 1;
		ptr = malloc(optnamesize);
		if (ptr == NULL)
			errx(EXIT_FAILURE, "No memory.");
		snprintf(ptr, optnamesize, "%s%u", opt->go_name, G_OPT_NUM(opt));
		G_OPT_NUMINC(opt);
		optname = ptr;
	} else {
		optname = opt->go_name;
	}

	if (G_OPT_TYPE(opt) == G_TYPE_NUMBER) {
		if (expand_number(val, &number) == -1) {
			err(EXIT_FAILURE, "Invalid value for '%c' argument",
			    opt->go_char);
		}
		ptr = malloc(sizeof(intmax_t));
		if (ptr == NULL)
			errx(EXIT_FAILURE, "No memory.");
		*(intmax_t *)ptr = number;
		opt->go_val = ptr;
		gctl_ro_param(req, optname, sizeof(intmax_t), opt->go_val);
	} else if (G_OPT_TYPE(opt) == G_TYPE_STRING) {
		gctl_ro_param(req, optname, -1, val);
	} else if (G_OPT_TYPE(opt) == G_TYPE_BOOL) {
		ptr = malloc(sizeof(int));
		if (ptr == NULL)
			errx(EXIT_FAILURE, "No memory.");
		*(int *)ptr = *val - '0';
		opt->go_val = ptr;
		gctl_ro_param(req, optname, sizeof(int), opt->go_val);
	} else {
		assert(!"Invalid type");
	}

	if (G_OPT_ISMULTI(opt))
		free(__DECONST(char *, optname));
}

/*
 * 1. Add given argument by caller.
 * 2. Add default values of not given arguments.
 * 3. Add the rest of arguments.
 */
static void
parse_arguments(struct g_command *cmd, struct gctl_req *req, int *argc,
    char ***argv)
{
	struct g_option *opt;
	char opts[64];
	unsigned i;
	int ch, vcount;

	*opts = '\0';
	if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0)
		strlcat(opts, "v", sizeof(opts));
	for (i = 0; ; i++) {
		opt = &cmd->gc_options[i];
		if (opt->go_name == NULL)
			break;
		assert(G_OPT_TYPE(opt) != 0);
		assert((opt->go_type & ~(G_TYPE_MASK | G_TYPE_MULTI)) == 0);
		/* Multiple bool arguments makes no sense. */
		assert(G_OPT_TYPE(opt) != G_TYPE_BOOL ||
		    (opt->go_type & G_TYPE_MULTI) == 0);
		strlcatf(opts, sizeof(opts), "%c", opt->go_char);
		if (G_OPT_TYPE(opt) != G_TYPE_BOOL)
			strlcat(opts, ":", sizeof(opts));
	}

	/*
	 * Add specified arguments.
	 */
	vcount = 0;
	while ((ch = getopt(*argc, *argv, opts)) != -1) {
		/* Standard (not passed to kernel) options. */
		if (ch == 'v' && (cmd->gc_flags & G_FLAG_VERBOSE) != 0)
			verbose = 1;
		/* Options passed to kernel. */
		opt = find_option(cmd, ch);
		if (opt == NULL) {
			if (ch == 'v' && (cmd->gc_flags & G_FLAG_VERBOSE) != 0){
				if (++vcount < 2)
					continue;
				else
					warnx("Option 'v' specified twice.");
			}
			usage();
		}
		if (!G_OPT_ISMULTI(opt) && G_OPT_ISDONE(opt)) {
			warnx("Option '%c' specified twice.", opt->go_char);
			usage();
		}
		G_OPT_DONE(opt);

		if (G_OPT_TYPE(opt) == G_TYPE_BOOL)
			set_option(req, opt, "1");
		else
			set_option(req, opt, optarg);
	}
	*argc -= optind;
	*argv += optind;

	/*
	 * Add not specified arguments, but with default values.
	 */
	for (i = 0; ; i++) {
		opt = &cmd->gc_options[i];
		if (opt->go_name == NULL)
			break;
		if (G_OPT_ISDONE(opt))
			continue;

		if (G_OPT_TYPE(opt) == G_TYPE_BOOL) {
			assert(opt->go_val == NULL);
			set_option(req, opt, "0");
		} else {
			if (opt->go_val == NULL) {
				warnx("Option '%c' not specified.",
				    opt->go_char);
				usage();
			} else if (opt->go_val == G_VAL_OPTIONAL) {
				/* add nothing. */
			} else {
				set_option(req, opt, opt->go_val);
			}
		}
	}

	/*
	 * Add rest of given arguments.
	 */
	gctl_ro_param(req, "nargs", sizeof(int), argc);
	for (i = 0; i < (unsigned)*argc; i++) {
		char argname[16];

		snprintf(argname, sizeof(argname), "arg%u", i);
		gctl_ro_param(req, argname, -1, (*argv)[i]);
	}
}

/*
 * Find given command in commands available for given class.
 */
static struct g_command *
find_command(const char *cmdstr, int flags)
{
	struct g_command *cmd;
	unsigned i;

	/*
	 * First try to find command defined by loaded library.
	 */
	if ((flags & GEOM_CLASS_CMDS) != 0 && class_commands != NULL) {
		for (i = 0; ; i++) {
			cmd = &class_commands[i];
			if (cmd->gc_name == NULL)
				break;
			if (strcmp(cmd->gc_name, cmdstr) == 0)
				return (cmd);
		}
	}
	/*
	 * Now try to find in standard commands.
	 */
	if ((flags & GEOM_STD_CMDS) != 0) {
		for (i = 0; ; i++) {
			cmd = &std_commands[i];
			if (cmd->gc_name == NULL)
				break;
			if (strcmp(cmd->gc_name, cmdstr) == 0)
				return (cmd);
		}
	}
	return (NULL);
}

static unsigned
set_flags(struct g_command *cmd)
{
	unsigned flags = 0;

	if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0 && verbose)
		flags |= G_FLAG_VERBOSE;

	return (flags);
}

/*
 * Run command.
 */
static void
run_command(int argc, char *argv[])
{
	struct g_command *cmd;
	struct gctl_req *req;
	const char *errstr;
	char buf[4096];

	/* First try to find a command defined by a class. */
	cmd = find_command(argv[0], GEOM_CLASS_CMDS);
	if (cmd == NULL) {
		/* Now, try to find a standard command. */
		cmd = find_command(argv[0], GEOM_STD_CMDS);
		if (cmd == NULL) {
			warnx("Unknown command: %s.", argv[0]);
			usage();
		}
		if (!std_available(cmd->gc_name)) {
			warnx("Command '%s' not available; "
			    "try 'load' first.", argv[0]);
			exit(EXIT_FAILURE);
		}
	}
	if ((cmd->gc_flags & G_FLAG_LOADKLD) != 0)
		load_module();

	req = gctl_get_handle();
	gctl_ro_param(req, "class", -1, gclass_name);
	gctl_ro_param(req, "verb", -1, argv[0]);
	if (version != NULL)
		gctl_ro_param(req, "version", sizeof(*version), version);
	parse_arguments(cmd, req, &argc, &argv);

	bzero(buf, sizeof(buf));
	if (cmd->gc_func != NULL) {
		unsigned flags;

		flags = set_flags(cmd);
		cmd->gc_func(req, flags);
		errstr = req->error;
	} else {
		gctl_rw_param(req, "output", sizeof(buf), buf);
		errstr = gctl_issue(req);
	}
	if (errstr != NULL && errstr[0] != '\0') {
		warnx("%s", errstr);
		if (strncmp(errstr, "warning: ", strlen("warning: ")) != 0) {
			gctl_free(req);
			exit(EXIT_FAILURE);
		}
	}
	if (buf[0] != '\0')
		printf("%s", buf);
	gctl_free(req);
	if (verbose)
		printf("Done.\n");
	exit(EXIT_SUCCESS);
}

#ifndef STATIC_GEOM_CLASSES
static const char *
library_path(void)
{
	const char *path;

	path = getenv("GEOM_LIBRARY_PATH");
	if (path == NULL)
		path = GEOM_CLASS_DIR;
	return (path);
}

static void
load_library(void)
{
	char *curpath, path[MAXPATHLEN], *tofree, *totalpath;
	uint32_t *lib_version;
	void *dlh;
	int ret;

	ret = 0;
	tofree = totalpath = strdup(library_path());
	if (totalpath == NULL)
		err(EXIT_FAILURE, "Not enough memory for library path");

	if (strchr(totalpath, ':') != NULL)
		curpath = strsep(&totalpath, ":");
	else
		curpath = totalpath;
	/* Traverse the paths to find one that contains the library we want. */
	while (curpath != NULL) {
		snprintf(path, sizeof(path), "%s/geom_%s.so", curpath,
		    class_name);
		ret = access(path, F_OK);
		if (ret == -1) {
			if (errno == ENOENT) {
				/*
				 * If we cannot find library, try the next
				 * path.
				 */
				curpath = strsep(&totalpath, ":");
				continue;
			}
			err(EXIT_FAILURE, "Cannot access library");
		}
		break;
	}
	free(tofree);
	/* No library was found, but standard commands can still be used */
	if (ret == -1)
		return;
	dlh = dlopen(path, RTLD_NOW);
	if (dlh == NULL)
		errx(EXIT_FAILURE, "Cannot open library: %s.", dlerror());
	lib_version = dlsym(dlh, "lib_version");
	if (lib_version == NULL) {
		warnx("Cannot find symbol %s: %s.", "lib_version", dlerror());
		dlclose(dlh);
		exit(EXIT_FAILURE);
	}
	if (*lib_version != G_LIB_VERSION) {
		dlclose(dlh);
		errx(EXIT_FAILURE, "%s and %s are not synchronized.",
		    getprogname(), path);
	}
	version = dlsym(dlh, "version");
	if (version == NULL) {
		warnx("Cannot find symbol %s: %s.", "version", dlerror());
		dlclose(dlh);
		exit(EXIT_FAILURE);
	}
	class_commands = dlsym(dlh, "class_commands");
	if (class_commands == NULL) {
		warnx("Cannot find symbol %s: %s.", "class_commands",
		    dlerror());
		dlclose(dlh);
		exit(EXIT_FAILURE);
	}
}
#endif	/* !STATIC_GEOM_CLASSES */

/*
 * Class name should be all capital letters.
 */
static void
set_class_name(void)
{
	char *s1, *s2;

	s1 = class_name;
	for (; *s1 != '\0'; s1++)
		*s1 = tolower(*s1);
	gclass_name = malloc(strlen(class_name) + 1);
	if (gclass_name == NULL)
		errx(EXIT_FAILURE, "No memory");
	s1 = gclass_name;
	s2 = class_name;
	for (; *s2 != '\0'; s2++)
		*s1++ = toupper(*s2);
	*s1 = '\0';
}

static void
get_class(int *argc, char ***argv)
{

	snprintf(comm, sizeof(comm), "%s", basename((*argv)[0]));
	if (strcmp(comm, "geom") == 0) {
		if (*argc < 2)
			usage();
		else if (*argc == 2) {
			if (strcmp((*argv)[1], "-h") == 0 ||
			    strcmp((*argv)[1], "help") == 0) {
				usage();
			}
		}
		strlcatf(comm, sizeof(comm), " %s", (*argv)[1]);
		class_name = (*argv)[1];
		*argc -= 2;
		*argv += 2;
	} else if (*comm == 'g') {
		class_name = comm + 1;
		*argc -= 1;
		*argv += 1;
	} else {
		errx(EXIT_FAILURE, "Invalid utility name.");
	}

#ifndef STATIC_GEOM_CLASSES
	load_library();
#else
	if (!strcasecmp(class_name, "part")) {
		version = &gpart_version;
		class_commands = gpart_class_commands;
	} else if (!strcasecmp(class_name, "label")) {
		version = &glabel_version;
		class_commands = glabel_class_commands;
	}
#endif /* !STATIC_GEOM_CLASSES */

	set_class_name();

	/* If we can't load or list, it's not a class. */
	if (!std_available("load") && !std_available("list"))
		errx(EXIT_FAILURE, "Invalid class name '%s'.", class_name);

	if (*argc < 1)
		usage();
}

static struct ggeom *
find_geom_by_provider(struct gmesh *mesh, const char *name)
{
	struct gclass *classp;
	struct ggeom *gp;
	struct gprovider *pp;

	LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
			LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
				if (strcmp(pp->lg_name, name) == 0)
					return (gp);
			}
		}
	}

	return (NULL);
}

static int
compute_tree_width_geom(struct gmesh *mesh, struct ggeom *gp, int indent)
{
	struct gclass *classp2;
	struct ggeom *gp2;
	struct gconsumer *cp2;
	struct gprovider *pp;
	int max_width, width;

	max_width = width = indent + strlen(gp->lg_name);

	LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
		LIST_FOREACH(classp2, &mesh->lg_class, lg_class) {
			LIST_FOREACH(gp2, &classp2->lg_geom, lg_geom) {
				LIST_FOREACH(cp2,
				    &gp2->lg_consumer, lg_consumer) {
					if (pp != cp2->lg_provider)
						continue;
					width = compute_tree_width_geom(mesh,
					    gp2, indent + 2);
					if (width > max_width)
						max_width = width;
				}
			}
		}
	}

	return (max_width);
}

static int
compute_tree_width(struct gmesh *mesh)
{
	struct gclass *classp;
	struct ggeom *gp;
	int max_width, width;

	max_width = width = 0;

	LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
			if (!LIST_EMPTY(&gp->lg_consumer))
				continue;
			width = compute_tree_width_geom(mesh, gp, 0);
			if (width > max_width)
				max_width = width;
		}
	}

	return (max_width);
}

static void
show_tree_geom(struct gmesh *mesh, struct ggeom *gp, int indent, int width)
{
	struct gclass *classp2;
	struct ggeom *gp2;
	struct gconsumer *cp2;
	struct gprovider *pp;

	if (LIST_EMPTY(&gp->lg_provider)) {
		printf("%*s%-*.*s %-*.*s\n", indent, "",
		    width - indent, width - indent, gp->lg_name,
		    GEOM_CLASS_WIDTH, GEOM_CLASS_WIDTH, gp->lg_class->lg_name);
		return;
	}

	LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
		printf("%*s%-*.*s %-*.*s %s\n", indent, "",
		    width - indent, width - indent, gp->lg_name,
		    GEOM_CLASS_WIDTH, GEOM_CLASS_WIDTH, gp->lg_class->lg_name,
		    pp->lg_name);

		LIST_FOREACH(classp2, &mesh->lg_class, lg_class) {
			LIST_FOREACH(gp2, &classp2->lg_geom, lg_geom) {
				LIST_FOREACH(cp2,
				    &gp2->lg_consumer, lg_consumer) {
					if (pp != cp2->lg_provider)
						continue;
					show_tree_geom(mesh, gp2,
					    indent + 2, width);
				}
			}
		}
	}
}

static void
show_tree(void)
{
	struct gmesh mesh;
	struct gclass *classp;
	struct ggeom *gp;
	int error, width;

	error = geom_gettree(&mesh);
	if (error != 0)
		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");

	width = compute_tree_width(&mesh);

	printf("%-*.*s %-*.*s %s\n",
	    width, width, "Geom",
	    GEOM_CLASS_WIDTH, GEOM_CLASS_WIDTH, "Class",
	    "Provider");

	LIST_FOREACH(classp, &mesh.lg_class, lg_class) {
		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
			if (!LIST_EMPTY(&gp->lg_consumer))
				continue;
			show_tree_geom(&mesh, gp, 0, width);
		}
	}
}

int
main(int argc, char *argv[])
{
	char *provider_name;
	bool tflag;
	int ch;

	provider_name = NULL;
	tflag = false;

	if (strcmp(getprogname(), "geom") == 0) {
		while ((ch = getopt(argc, argv, "hp:t")) != -1) {
			switch (ch) {
			case 'p':
				provider_name = strdup(optarg);
				if (provider_name == NULL)
					err(1, "strdup");
				break;
			case 't':
				tflag = true;
				break;
			case 'h':
			default:
				usage();
			}
		}

		/*
		 * Don't adjust argc and argv, it would break get_class().
		 */
	}

	if (tflag && provider_name != NULL) {
		errx(EXIT_FAILURE,
		    "At most one of -P and -t may be specified.");
	}

	if (provider_name != NULL) {
		list_one_geom_by_provider(provider_name);
		return (0);
	}

	if (tflag) {
		show_tree();
		return (0);
	}

	get_class(&argc, &argv);
	run_command(argc, argv);
	/* NOTREACHED */

	exit(EXIT_FAILURE);
}

static struct gclass *
find_class(struct gmesh *mesh, const char *name)
{
	struct gclass *classp;

	LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
		if (strcmp(classp->lg_name, name) == 0)
			return (classp);
	}
	return (NULL);
}

static struct ggeom *
find_geom(struct gclass *classp, const char *name)
{
	struct ggeom *gp;

	if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
		name += sizeof(_PATH_DEV) - 1;

	LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
		if (strcmp(gp->lg_name, name) == 0)
			return (gp);
	}
	return (NULL);
}

static void
list_one_provider(struct gprovider *pp, const char *prefix)
{
	struct gconfig *conf;
	char buf[5];

	printf("Name: %s\n", pp->lg_name);
	humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "",
	    HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
	printf("%sMediasize: %jd (%s)\n", prefix, (intmax_t)pp->lg_mediasize,
	    buf);
	printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize);
	if (pp->lg_stripesize > 0 || pp->lg_stripeoffset > 0) {
		printf("%sStripesize: %ju\n", prefix, pp->lg_stripesize);
		printf("%sStripeoffset: %ju\n", prefix, pp->lg_stripeoffset);
	}
	printf("%sMode: %s\n", prefix, pp->lg_mode);
	LIST_FOREACH(conf, &pp->lg_config, lg_config) {
		printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val);
	}
}

static void
list_one_consumer(struct gconsumer *cp, const char *prefix)
{
	struct gprovider *pp;
	struct gconfig *conf;

	pp = cp->lg_provider;
	if (pp == NULL)
		printf("[no provider]\n");
	else {
		char buf[5];

		printf("Name: %s\n", pp->lg_name);
		humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "",
		    HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
		printf("%sMediasize: %jd (%s)\n", prefix,
		    (intmax_t)pp->lg_mediasize, buf);
		printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize);
		if (pp->lg_stripesize > 0 || pp->lg_stripeoffset > 0) {
			printf("%sStripesize: %ju\n", prefix, pp->lg_stripesize);
			printf("%sStripeoffset: %ju\n", prefix, pp->lg_stripeoffset);
		}
		printf("%sMode: %s\n", prefix, cp->lg_mode);
	}
	LIST_FOREACH(conf, &cp->lg_config, lg_config) {
		printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val);
	}
}

static void
list_one_geom(struct ggeom *gp)
{
	struct gprovider *pp;
	struct gconsumer *cp;
	struct gconfig *conf;
	unsigned n;

	printf("Geom name: %s\n", gp->lg_name);
	LIST_FOREACH(conf, &gp->lg_config, lg_config) {
		printf("%s: %s\n", conf->lg_name, conf->lg_val);
	}
	if (!LIST_EMPTY(&gp->lg_provider)) {
		printf("Providers:\n");
		n = 1;
		LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
			printf("%u. ", n++);
			list_one_provider(pp, "   ");
		}
	}
	if (!LIST_EMPTY(&gp->lg_consumer)) {
		printf("Consumers:\n");
		n = 1;
		LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
			printf("%u. ", n++);
			list_one_consumer(cp, "   ");
		}
	}
	printf("\n");
}

static void
list_one_geom_by_provider(const char *provider_name)
{
	struct gmesh mesh;
	struct ggeom *gp;
	int error;

	error = geom_gettree(&mesh);
	if (error != 0)
		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");

	gp = find_geom_by_provider(&mesh, provider_name);
	if (gp == NULL)
		errx(EXIT_FAILURE, "Cannot find provider '%s'.", provider_name);

	printf("Geom class: %s\n", gp->lg_class->lg_name);
	list_one_geom(gp);
}

static void
std_help(struct gctl_req *req __unused, unsigned flags __unused)
{

	usage();
}

static int
std_list_available(void)
{
	struct gmesh mesh;
	struct gclass *classp;
	int error;

	error = geom_gettree(&mesh);
	if (error != 0)
		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
	classp = find_class(&mesh, gclass_name);
	geom_deletetree(&mesh);
	if (classp != NULL)
		return (1);
	return (0);
}

static void
std_list(struct gctl_req *req, unsigned flags __unused)
{
	struct gmesh mesh;
	struct gclass *classp;
	struct ggeom *gp;
	const char *name;
	int all, error, i, nargs;

	error = geom_gettree(&mesh);
	if (error != 0)
		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
	classp = find_class(&mesh, gclass_name);
	if (classp == NULL) {
		geom_deletetree(&mesh);
		errx(EXIT_FAILURE, "Class '%s' not found.", gclass_name);
	}
	nargs = gctl_get_int(req, "nargs");
	all = gctl_get_int(req, "all");
	if (nargs > 0) {
		for (i = 0; i < nargs; i++) {
			name = gctl_get_ascii(req, "arg%d", i);
			gp = find_geom(classp, name);
			if (gp == NULL) {
				errx(EXIT_FAILURE, "Class '%s' does not have "
				    "an instance named '%s'.",
				    gclass_name, name);
			}
			list_one_geom(gp);
		}
	} else {
		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
			if (LIST_EMPTY(&gp->lg_provider) && !all)
				continue;
			list_one_geom(gp);
		}
	}
	geom_deletetree(&mesh);
}

static int
std_status_available(void)
{

	/* 'status' command is available when 'list' command is. */
	return (std_list_available());
}

static void
status_update_len(struct ggeom *gp, int *name_len, int *status_len)
{
	struct gconfig *conf;
	int len;

	assert(gp != NULL);
	assert(name_len != NULL);
	assert(status_len != NULL);

	len = strlen(gp->lg_name);
	if (*name_len < len)
		*name_len = len;
	LIST_FOREACH(conf, &gp->lg_config, lg_config) {
		if (strcasecmp(conf->lg_name, "state") == 0) {
			len = strlen(conf->lg_val);
			if (*status_len < len)
				*status_len = len;
		}
	}
}

static void
status_update_len_prs(struct ggeom *gp, int *name_len, int *status_len)
{
	struct gprovider *pp;
	struct gconfig *conf;
	int len, glen;

	assert(gp != NULL);
	assert(name_len != NULL);
	assert(status_len != NULL);

	glen = 0;
	LIST_FOREACH(conf, &gp->lg_config, lg_config) {
		if (strcasecmp(conf->lg_name, "state") == 0) {
			glen = strlen(conf->lg_val);
			break;
		}
	}
	LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
		len = strlen(pp->lg_name);
		if (*name_len < len)
			*name_len = len;
		len = glen;
		LIST_FOREACH(conf, &pp->lg_config, lg_config) {
			if (strcasecmp(conf->lg_name, "state") == 0) {
				len = strlen(conf->lg_val);
				break;
			}
		}
		if (*status_len < len)
			*status_len = len;
	}
}

static char *
status_one_consumer(struct gconsumer *cp)
{
	static char buf[256];
	struct gprovider *pp;
	struct gconfig *conf;
	const char *state, *syncr;

	pp = cp->lg_provider;
	if (pp == NULL)
		return (NULL);
	state = NULL;
	syncr = NULL;
	LIST_FOREACH(conf, &cp->lg_config, lg_config) {
		if (strcasecmp(conf->lg_name, "state") == 0)
			state = conf->lg_val;
		if (strcasecmp(conf->lg_name, "synchronized") == 0)
			syncr = conf->lg_val;
	}
	if (state == NULL && syncr == NULL)
		snprintf(buf, sizeof(buf), "%s", pp->lg_name);
	else if (state != NULL && syncr != NULL) {
		snprintf(buf, sizeof(buf), "%s (%s, %s)", pp->lg_name,
		    state, syncr);
	} else {
		snprintf(buf, sizeof(buf), "%s (%s)", pp->lg_name,
		    state ? state : syncr);
	}
	return (buf);
}

static void
status_one_geom(struct ggeom *gp, int script, int name_len, int status_len)
{
	struct gconsumer *cp;
	struct gconfig *conf;
	const char *name, *status, *component;
	int gotone;

	name = gp->lg_name;
	status = "N/A";
	LIST_FOREACH(conf, &gp->lg_config, lg_config) {
		if (strcasecmp(conf->lg_name, "state") == 0) {
			status = conf->lg_val;
			break;
		}
	}
	gotone = 0;
	LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
		component = status_one_consumer(cp);
		if (component == NULL)
			continue;
		gotone = 1;
		printf("%*s  %*s  %s\n", name_len, name, status_len, status,
		    component);
		if (!script)
			name = status = "";
	}
	if (!gotone) {
		printf("%*s  %*s  %s\n", name_len, name, status_len, status,
		    "N/A");
	}
}

static void
status_one_geom_prs(struct ggeom *gp, int script, int name_len, int status_len)
{
	struct gprovider *pp;
	struct gconsumer *cp;
	struct gconfig *conf;
	const char *name, *status, *component;
	int gotone;

	LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
		name = pp->lg_name;
		status = "N/A";
		LIST_FOREACH(conf, &gp->lg_config, lg_config) {
			if (strcasecmp(conf->lg_name, "state") == 0) {
				status = conf->lg_val;
				break;
			}
		}
		LIST_FOREACH(conf, &pp->lg_config, lg_config) {
			if (strcasecmp(conf->lg_name, "state") == 0) {
				status = conf->lg_val;
				break;
			}
		}
		gotone = 0;
		LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
			component = status_one_consumer(cp);
			if (component == NULL)
				continue;
			gotone = 1;
			printf("%*s  %*s  %s\n", name_len, name,
			    status_len, status, component);
			if (!script)
				name = status = "";
		}
		if (!gotone) {
			printf("%*s  %*s  %s\n", name_len, name,
			    status_len, status, "N/A");
		}
	}
}

static void
std_status(struct gctl_req *req, unsigned flags __unused)
{
	struct gmesh mesh;
	struct gclass *classp;
	struct ggeom *gp;
	const char *name;
	int name_len, status_len;
	int all, error, geoms, i, n, nargs, script;

	error = geom_gettree(&mesh);
	if (error != 0)
		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
	classp = find_class(&mesh, gclass_name);
	if (classp == NULL)
		errx(EXIT_FAILURE, "Class %s not found.", gclass_name);
	nargs = gctl_get_int(req, "nargs");
	all = gctl_get_int(req, "all");
	geoms = gctl_get_int(req, "geoms");
	script = gctl_get_int(req, "script");
	if (script) {
		name_len = 0;
		status_len = 0;
	} else {
		name_len = strlen("Name");
		status_len = strlen("Status");
	}
	if (nargs > 0) {
		for (i = 0, n = 0; i < nargs; i++) {
			name = gctl_get_ascii(req, "arg%d", i);
			gp = find_geom(classp, name);
			if (gp == NULL)
				errx(EXIT_FAILURE, "No such geom: %s.", name);
			if (geoms) {
				status_update_len(gp,
				    &name_len, &status_len);
			} else {
				status_update_len_prs(gp,
				    &name_len, &status_len);
			}
			n++;
		}
		if (n == 0)
			goto end;
	} else {
		n = 0;
		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
			if (LIST_EMPTY(&gp->lg_provider) && !all)
				continue;
			if (geoms) {
				status_update_len(gp,
				    &name_len, &status_len);
			} else {
				status_update_len_prs(gp,
				    &name_len, &status_len);
			}
			n++;
		}
		if (n == 0)
			goto end;
	}
	if (!script) {
		printf("%*s  %*s  %s\n", name_len, "Name", status_len, "Status",
		    "Components");
	}
	if (nargs > 0) {
		for (i = 0; i < nargs; i++) {
			name = gctl_get_ascii(req, "arg%d", i);
			gp = find_geom(classp, name);
			if (gp == NULL)
				continue;
			if (geoms) {
				status_one_geom(gp, script, name_len,
				    status_len);
			} else {
				status_one_geom_prs(gp, script, name_len,
				    status_len);
			}
		}
	} else {
		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
			if (LIST_EMPTY(&gp->lg_provider) && !all)
				continue;
			if (geoms) {
				status_one_geom(gp, script, name_len,
				    status_len);
			} else {
				status_one_geom_prs(gp, script, name_len,
				    status_len);
			}
		}
	}
end:
	geom_deletetree(&mesh);
}

static int
std_load_available(void)
{
	char name[MAXPATHLEN], paths[MAXPATHLEN * 8], *p;
	struct stat sb;
	size_t len;

	snprintf(name, sizeof(name), "g_%s", class_name);
	/*
	 * If already in kernel, "load" command is not available.
	 */
	if (modfind(name) >= 0)
		return (0);
	bzero(paths, sizeof(paths));
	len = sizeof(paths);
	if (sysctlbyname("kern.module_path", paths, &len, NULL, 0) < 0)
		err(EXIT_FAILURE, "sysctl(kern.module_path)");
	for (p = strtok(paths, ";"); p != NULL; p = strtok(NULL, ";")) {
		snprintf(name, sizeof(name), "%s/geom_%s.ko", p, class_name);
		/*
		 * If geom_<name>.ko file exists, "load" command is available.
		 */
		if (stat(name, &sb) == 0)
			return (1);
	}
	return (0);
}

static void
std_load(struct gctl_req *req __unused, unsigned flags)
{

	/*
	 * Do nothing special here, because of G_FLAG_LOADKLD flag,
	 * module is already loaded.
	 */
	if ((flags & G_FLAG_VERBOSE) != 0)
		printf("Module available.\n");
}

static int
std_unload_available(void)
{
	char name[64];
	int id;

	snprintf(name, sizeof(name), "geom_%s", class_name);
	id = kldfind(name);
	if (id >= 0)
		return (1);
	return (0);
}

static void
std_unload(struct gctl_req *req, unsigned flags __unused)
{
	char name[64];
	int id;

	snprintf(name, sizeof(name), "geom_%s", class_name);
	id = kldfind(name);
	if (id < 0) {
		gctl_error(req, "Could not find module: %s.", strerror(errno));
		return;
	}
	if (kldunload(id) < 0) {
		gctl_error(req, "Could not unload module: %s.",
		    strerror(errno));
		return;
	}
}

static int
std_available(const char *name)
{

	if (strcmp(name, "help") == 0)
		return (1);
	else if (strcmp(name, "list") == 0)
		return (std_list_available());
	else if (strcmp(name, "status") == 0)
		return (std_status_available());
	else if (strcmp(name, "load") == 0)
		return (std_load_available());
	else if (strcmp(name, "unload") == 0)
		return (std_unload_available());
	else
		assert(!"Unknown standard command.");
	return (0);
}