1 /*- 2 * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/linker.h> 32 #include <sys/module.h> 33 #include <sys/stat.h> 34 #include <sys/sysctl.h> 35 #include <ctype.h> 36 #include <err.h> 37 #include <errno.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <stdarg.h> 41 #include <stdint.h> 42 #include <string.h> 43 #include <unistd.h> 44 #include <libgen.h> 45 #include <libutil.h> 46 #include <inttypes.h> 47 #include <dlfcn.h> 48 #include <assert.h> 49 #include <libgeom.h> 50 #include <geom.h> 51 52 #include "misc/subr.h" 53 54 55 static char comm[MAXPATHLEN], *class_name = NULL, *gclass_name = NULL; 56 static uint32_t *version = NULL; 57 static int verbose = 0; 58 static struct g_command *class_commands = NULL; 59 static void (*usage)(const char *name); 60 61 static struct g_command *find_command(const char *cmdstr, int all); 62 static int std_available(const char *name); 63 64 static void std_help(struct gctl_req *req, unsigned flags); 65 static void std_list(struct gctl_req *req, unsigned flags); 66 static void std_load(struct gctl_req *req, unsigned flags); 67 static void std_unload(struct gctl_req *req, unsigned flags); 68 69 struct g_command std_commands[] = { 70 { "help", 0, std_help, G_NULL_OPTS }, 71 { "list", 0, std_list, G_NULL_OPTS }, 72 { "load", G_FLAG_VERBOSE | G_FLAG_LOADKLD, std_load, G_NULL_OPTS }, 73 { "unload", G_FLAG_VERBOSE, std_unload, G_NULL_OPTS }, 74 G_CMD_SENTINEL 75 }; 76 77 static void 78 std_usage(const char *name) 79 { 80 struct g_command *cmd; 81 struct g_option *opt; 82 unsigned i, j; 83 84 for (i = 0; ; i++) { 85 cmd = &class_commands[i]; 86 if (cmd->gc_name == NULL) 87 break; 88 fprintf(stderr, "%s %s %s %s", i == 0 ? "usage:" : " ", 89 name, class_name, cmd->gc_name); 90 if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0) 91 fprintf(stderr, " [-v]"); 92 for (j = 0; ; j++) { 93 opt = &cmd->gc_options[j]; 94 if (opt->go_name == NULL) 95 break; 96 if (opt->go_val != NULL || opt->go_type == G_TYPE_NONE) 97 fprintf(stderr, " ["); 98 else 99 fprintf(stderr, " "); 100 fprintf(stderr, "-%c", opt->go_char); 101 if (opt->go_type != G_TYPE_NONE) 102 fprintf(stderr, " %s", opt->go_name); 103 if (opt->go_val != NULL || opt->go_type == G_TYPE_NONE) 104 fprintf(stderr, "]"); 105 } 106 fprintf(stderr, " ...\n"); 107 } 108 exit(EXIT_FAILURE); 109 } 110 111 static void 112 geom_usage(void) 113 { 114 115 if (class_name == NULL) { 116 errx(EXIT_FAILURE, "usage: %s <class> <command> [options]", 117 "geom"); 118 } else { 119 const char *prefix; 120 unsigned i; 121 122 if (usage == NULL) 123 prefix = "usage:"; 124 else { 125 usage(comm); 126 prefix = " "; 127 } 128 for (i = 0; ; i++) { 129 struct g_command *cmd; 130 131 cmd = &std_commands[i]; 132 if (cmd->gc_name == NULL) 133 break; 134 if (find_command(cmd->gc_name, 0) != NULL) 135 continue; 136 if (!std_available(cmd->gc_name)) 137 continue; 138 fprintf(stderr, "%s %s %s", prefix, comm, cmd->gc_name); 139 if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0) 140 fprintf(stderr, " [-v]"); 141 fprintf(stderr, "\n"); 142 prefix = " "; 143 } 144 exit(EXIT_FAILURE); 145 } 146 } 147 148 static void 149 load_module(void) 150 { 151 char name1[64], name2[64]; 152 153 snprintf(name1, sizeof(name1), "g_%s", class_name); 154 snprintf(name2, sizeof(name2), "geom_%s", class_name); 155 if (modfind(name1) < 0) { 156 /* Not present in kernel, try loading it. */ 157 if (kldload(name2) < 0 || modfind(name1) < 0) { 158 if (errno != EEXIST) { 159 errx(EXIT_FAILURE, 160 "%s module not available!", name2); 161 } 162 } 163 } 164 } 165 166 static int 167 strlcatf(char *str, size_t size, const char *format, ...) 168 { 169 size_t len; 170 va_list ap; 171 int ret; 172 173 len = strlen(str); 174 str += len; 175 size -= len; 176 177 va_start(ap, format); 178 ret = vsnprintf(str, size, format, ap); 179 va_end(ap); 180 181 return (ret); 182 } 183 184 /* 185 * Find given option in options available for given command. 186 */ 187 static struct g_option * 188 find_option(struct g_command *cmd, char ch) 189 { 190 struct g_option *opt; 191 unsigned i; 192 193 for (i = 0; ; i++) { 194 opt = &cmd->gc_options[i]; 195 if (opt->go_name == NULL) 196 return (NULL); 197 if (opt->go_char == ch) 198 return (opt); 199 } 200 /* NOTREACHED */ 201 return (NULL); 202 } 203 204 /* 205 * Add given option to gctl_req. 206 */ 207 static void 208 set_option(struct gctl_req *req, struct g_option *opt, const char *val) 209 { 210 211 if (opt->go_type == G_TYPE_NUMBER) { 212 intmax_t number; 213 214 errno = 0; 215 number = strtoimax(optarg, NULL, 0); 216 if (errno != 0) { 217 err(EXIT_FAILURE, "Invalid value for '%c' argument.", 218 opt->go_char); 219 } 220 opt->go_val = malloc(sizeof(intmax_t)); 221 if (opt->go_val == NULL) 222 errx(EXIT_FAILURE, "No memory."); 223 *(intmax_t *)opt->go_val = number; 224 225 gctl_ro_param(req, opt->go_name, sizeof(intmax_t), opt->go_val); 226 } else if (opt->go_type == G_TYPE_STRING) { 227 gctl_ro_param(req, opt->go_name, -1, optarg); 228 } else /* if (opt->go_type == G_TYPE_NONE) */ { 229 opt->go_val = malloc(sizeof(int)); 230 if (opt->go_val == NULL) 231 errx(EXIT_FAILURE, "No memory."); 232 *(int *)opt->go_val = *val - '0'; 233 234 gctl_ro_param(req, opt->go_name, sizeof(int), 235 opt->go_val); 236 } 237 } 238 239 /* 240 * 1. Add given argument by caller. 241 * 2. Add default values of not given arguments. 242 * 3. Add the rest of arguments. 243 */ 244 static void 245 parse_arguments(struct g_command *cmd, struct gctl_req *req, int *argc, 246 char ***argv) 247 { 248 struct g_option *opt; 249 char opts[64]; 250 unsigned i; 251 int ch; 252 253 *opts = '\0'; 254 if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0) 255 strlcat(opts, "v", sizeof(opts)); 256 for (i = 0; ; i++) { 257 opt = &cmd->gc_options[i]; 258 if (opt->go_name == NULL) 259 break; 260 strlcatf(opts, sizeof(opts), "%c", opt->go_char); 261 if (opt->go_type != G_TYPE_NONE) 262 strlcat(opts, ":", sizeof(opts)); 263 } 264 265 /* 266 * Add specified arguments. 267 */ 268 while ((ch = getopt(*argc, *argv, opts)) != -1) { 269 /* Standard (not passed to kernel) options. */ 270 switch (ch) { 271 case 'v': 272 verbose = 1; 273 continue; 274 } 275 /* Options passed to kernel. */ 276 opt = find_option(cmd, ch); 277 if (opt == NULL) 278 geom_usage(); 279 if (G_OPT_ISDONE(opt)) { 280 fprintf(stderr, "Flag '%c' specified twice.\n", 281 opt->go_char); 282 geom_usage(); 283 } 284 G_OPT_DONE(opt); 285 286 if (opt->go_type == G_TYPE_NONE) 287 set_option(req, opt, "1"); 288 else 289 set_option(req, opt, optarg); 290 } 291 *argc -= optind; 292 *argv += optind; 293 294 /* 295 * Add not specified arguments, but with default values. 296 */ 297 for (i = 0; ; i++) { 298 opt = &cmd->gc_options[i]; 299 if (opt->go_name == NULL) 300 break; 301 if (G_OPT_ISDONE(opt)) 302 continue; 303 304 if (opt->go_type == G_TYPE_NONE) { 305 assert(opt->go_val == NULL); 306 set_option(req, opt, "0"); 307 } else { 308 if (opt->go_val == NULL) { 309 fprintf(stderr, "Flag '%c' not specified.\n", 310 opt->go_char); 311 geom_usage(); 312 } else { 313 if (opt->go_type == G_TYPE_NUMBER) { 314 gctl_ro_param(req, opt->go_name, 315 sizeof(intmax_t), opt->go_val); 316 } else /* if (opt->go_type == G_TYPE_STRING)*/ { 317 gctl_ro_param(req, opt->go_name, -1, 318 opt->go_val); 319 } 320 } 321 } 322 } 323 /* 324 * Add rest of given arguments. 325 */ 326 gctl_ro_param(req, "nargs", sizeof(int), argc); 327 for (i = 0; i < (unsigned)*argc; i++) { 328 char argname[16]; 329 330 snprintf(argname, sizeof(argname), "arg%u", i); 331 gctl_ro_param(req, argname, -1, (*argv)[i]); 332 } 333 } 334 335 /* 336 * Find given command in commands available for given class. 337 */ 338 static struct g_command * 339 find_command(const char *cmdstr, int all) 340 { 341 struct g_command *cmd; 342 unsigned i; 343 344 /* 345 * First try to find command defined by loaded library. 346 */ 347 if (class_commands != NULL) { 348 for (i = 0; ; i++) { 349 cmd = &class_commands[i]; 350 if (cmd->gc_name == NULL) 351 break; 352 if (strcmp(cmd->gc_name, cmdstr) == 0) 353 return (cmd); 354 } 355 } 356 if (!all) 357 return (NULL); 358 /* 359 * Now try to find in standard commands. 360 */ 361 for (i = 0; ; i++) { 362 cmd = &std_commands[i]; 363 if (cmd->gc_name == NULL) 364 break; 365 if (!std_available(cmd->gc_name)) 366 continue; 367 if (strcmp(cmd->gc_name, cmdstr) == 0) 368 return (cmd); 369 } 370 return (NULL); 371 } 372 373 static unsigned 374 set_flags(struct g_command *cmd) 375 { 376 unsigned flags = 0; 377 378 if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0 && verbose) 379 flags |= G_FLAG_VERBOSE; 380 381 return (flags); 382 } 383 384 /* 385 * Run command. 386 */ 387 static void 388 run_command(int argc, char *argv[]) 389 { 390 struct g_command *cmd; 391 struct gctl_req *req; 392 const char *errstr; 393 char buf[4096]; 394 395 cmd = find_command(argv[0], 1); 396 if (cmd == NULL) { 397 fprintf(stderr, "Unknown command: %s\n", argv[0]); 398 geom_usage(); 399 } 400 if ((cmd->gc_flags & G_FLAG_LOADKLD) != 0) 401 load_module(); 402 403 req = gctl_get_handle(); 404 gctl_ro_param(req, "class", -1, gclass_name); 405 gctl_ro_param(req, "verb", -1, argv[0]); 406 if (version != NULL) 407 gctl_ro_param(req, "version", sizeof(*version), version); 408 parse_arguments(cmd, req, &argc, &argv); 409 410 if (cmd->gc_func != NULL) { 411 unsigned flags; 412 413 flags = set_flags(cmd); 414 cmd->gc_func(req, flags); 415 errstr = req->error; 416 } else { 417 bzero(buf, sizeof(buf)); 418 gctl_rw_param(req, "output", sizeof(buf), buf); 419 errstr = gctl_issue(req); 420 } 421 if (errstr != NULL) { 422 fprintf(stderr, "%s\n", errstr); 423 if (strncmp(errstr, "warning: ", strlen("warning: ")) != 0) { 424 gctl_free(req); 425 exit(EXIT_FAILURE); 426 } 427 } 428 if (*buf != '\0') 429 printf("%s", buf); 430 gctl_free(req); 431 if (verbose) 432 printf("Done.\n"); 433 exit(EXIT_SUCCESS); 434 } 435 436 static void 437 load_library(void) 438 { 439 char path[MAXPATHLEN]; 440 uint32_t *lib_version; 441 void *dlh; 442 443 snprintf(path, sizeof(path), "%s/geom_%s.so", CLASS_DIR, class_name); 444 dlh = dlopen(path, RTLD_NOW); 445 if (dlh == NULL) { 446 #if 0 447 fprintf(stderr, "Cannot open library %s, but continuing " 448 "anyway.\n", path); 449 #endif 450 /* 451 * Even if library cannot be loaded, standard commands are 452 * available, so don't panic! 453 */ 454 return; 455 } 456 lib_version = dlsym(dlh, "lib_version"); 457 if (lib_version == NULL) { 458 fprintf(stderr, "Cannot find symbol %s: %s.\n", "lib_version", 459 dlerror()); 460 dlclose(dlh); 461 exit(EXIT_FAILURE); 462 } 463 if (*lib_version != G_LIB_VERSION) { 464 dlclose(dlh); 465 errx(EXIT_FAILURE, "%s and %s are not synchronized.", 466 getprogname(), path); 467 } 468 version = dlsym(dlh, "version"); 469 if (version == NULL) { 470 fprintf(stderr, "Cannot find symbol %s: %s.\n", "version", 471 dlerror()); 472 dlclose(dlh); 473 exit(EXIT_FAILURE); 474 } 475 class_commands = dlsym(dlh, "class_commands"); 476 if (class_commands == NULL) { 477 fprintf(stderr, "Cannot find symbol %s: %s.\n", 478 "class_commands", dlerror()); 479 dlclose(dlh); 480 exit(EXIT_FAILURE); 481 } 482 usage = dlsym(dlh, "usage"); 483 if (usage == NULL) 484 usage = std_usage; 485 } 486 487 /* 488 * Class name should be all capital letters. 489 */ 490 static void 491 set_class_name(void) 492 { 493 char *s1, *s2; 494 495 gclass_name = malloc(strlen(class_name)); 496 if (gclass_name == NULL) 497 errx(EXIT_FAILURE, "No memory"); 498 s1 = gclass_name; 499 s2 = class_name; 500 for (; *s2 != '\0'; s2++) 501 *s1++ = toupper(*s2); 502 *s1 = '\0'; 503 } 504 505 static void 506 get_class(int *argc, char ***argv) 507 { 508 509 snprintf(comm, sizeof(comm), "%s", basename((*argv)[0])); 510 if (strcmp(comm, "geom") == 0) { 511 if (*argc < 2) 512 geom_usage(); 513 strlcatf(comm, sizeof(comm), " %s", (*argv)[1]); 514 class_name = (*argv)[1]; 515 *argc -= 2; 516 *argv += 2; 517 } else if (*comm == 'g') { 518 class_name = comm + 1; 519 *argc -= 1; 520 *argv += 1; 521 } else { 522 errx(EXIT_FAILURE, "Invalid utility name."); 523 } 524 set_class_name(); 525 load_library(); 526 if (*argc < 1) 527 geom_usage(); 528 } 529 530 int 531 main(int argc, char *argv[]) 532 { 533 534 get_class(&argc, &argv); 535 run_command(argc, argv); 536 /* NOTREACHED */ 537 538 exit(EXIT_FAILURE); 539 } 540 541 static struct gclass * 542 find_class(struct gmesh *mesh, const char *name) 543 { 544 struct gclass *classp; 545 546 LIST_FOREACH(classp, &mesh->lg_class, lg_class) { 547 if (strcmp(classp->lg_name, name) == 0) 548 return (classp); 549 } 550 return (NULL); 551 } 552 553 static struct ggeom * 554 find_geom(struct gclass *classp, const char *name) 555 { 556 struct ggeom *gp; 557 558 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) { 559 if (strcmp(gp->lg_name, name) == 0) 560 return (gp); 561 } 562 return (NULL); 563 } 564 565 static void 566 show_one_provider(struct gprovider *pp, const char *prefix) 567 { 568 struct gconfig *conf; 569 char buf[5]; 570 571 printf("Name: %s\n", pp->lg_name); 572 humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "", 573 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 574 printf("%sMediasize: %jd (%s)\n", prefix, (intmax_t)pp->lg_mediasize, 575 buf); 576 printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize); 577 printf("%sMode: %s\n", prefix, pp->lg_mode); 578 LIST_FOREACH(conf, &pp->lg_config, lg_config) { 579 printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val); 580 } 581 } 582 583 static void 584 show_one_consumer(struct gconsumer *cp, const char *prefix) 585 { 586 struct gprovider *pp; 587 struct gconfig *conf; 588 589 pp = cp->lg_provider; 590 if (pp == NULL) 591 printf("[no provider]\n"); 592 else { 593 char buf[5]; 594 595 printf("Name: %s\n", pp->lg_name); 596 humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "", 597 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 598 printf("%sMediasize: %jd (%s)\n", prefix, 599 (intmax_t)pp->lg_mediasize, buf); 600 printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize); 601 printf("%sMode: %s\n", prefix, cp->lg_mode); 602 } 603 LIST_FOREACH(conf, &cp->lg_config, lg_config) { 604 printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val); 605 } 606 } 607 608 static void 609 show_one_geom(struct ggeom *gp) 610 { 611 struct gprovider *pp; 612 struct gconsumer *cp; 613 struct gconfig *conf; 614 unsigned n; 615 616 printf("Geom name: %s\n", gp->lg_name); 617 LIST_FOREACH(conf, &gp->lg_config, lg_config) { 618 printf("%s: %s\n", conf->lg_name, conf->lg_val); 619 } 620 if (!LIST_EMPTY(&gp->lg_provider)) { 621 printf("Providers:\n"); 622 n = 1; 623 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) { 624 printf("%u. ", n++); 625 show_one_provider(pp, " "); 626 } 627 } 628 if (!LIST_EMPTY(&gp->lg_consumer)) { 629 printf("Consumers:\n"); 630 n = 1; 631 LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) { 632 printf("%u. ", n++); 633 show_one_consumer(cp, " "); 634 } 635 } 636 printf("\n"); 637 } 638 639 static void 640 std_help(struct gctl_req *req __unused, unsigned flags __unused) 641 { 642 643 geom_usage(); 644 } 645 646 static int 647 std_list_available(void) 648 { 649 struct gmesh mesh; 650 struct gclass *classp; 651 int error; 652 653 error = geom_gettree(&mesh); 654 if (error != 0) 655 exit(EXIT_FAILURE); 656 classp = find_class(&mesh, gclass_name); 657 geom_deletetree(&mesh); 658 if (classp != NULL) 659 return (1); 660 return (0); 661 } 662 663 static void 664 std_list(struct gctl_req *req, unsigned flags __unused) 665 { 666 struct gmesh mesh; 667 struct gclass *classp; 668 struct ggeom *gp; 669 int error, *nargs; 670 671 error = geom_gettree(&mesh); 672 if (error != 0) 673 exit(EXIT_FAILURE); 674 classp = find_class(&mesh, gclass_name); 675 if (classp == NULL) { 676 geom_deletetree(&mesh); 677 fprintf(stderr, "Class %s not found.\n", gclass_name); 678 return; 679 } 680 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 681 if (nargs == NULL) { 682 gctl_error(req, "No '%s' argument.", "nargs"); 683 geom_deletetree(&mesh); 684 return; 685 } 686 if (*nargs > 0) { 687 int i; 688 689 for (i = 0; i < *nargs; i++) { 690 const char *name; 691 char param[16]; 692 693 snprintf(param, sizeof(param), "arg%d", i); 694 name = gctl_get_asciiparam(req, param); 695 assert(name != NULL); 696 gp = find_geom(classp, name); 697 if (gp != NULL) 698 show_one_geom(gp); 699 else 700 fprintf(stderr, "No such geom: %s.\n", name); 701 } 702 } else { 703 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) { 704 show_one_geom(gp); 705 } 706 } 707 geom_deletetree(&mesh); 708 } 709 710 static int 711 std_load_available(void) 712 { 713 char name[MAXPATHLEN], paths[MAXPATHLEN * 8], *p; 714 struct stat sb; 715 size_t len; 716 717 snprintf(name, sizeof(name), "g_%s", class_name); 718 /* 719 * If already in kernel, "load" command is not available. 720 */ 721 if (modfind(name) >= 0) 722 return (0); 723 bzero(paths, sizeof(paths)); 724 len = sizeof(paths); 725 if (sysctlbyname("kern.module_path", paths, &len, NULL, 0) < 0) 726 err(EXIT_FAILURE, "sysctl(kern.module_path)"); 727 for (p = strtok(paths, ";"); p != NULL; p = strtok(NULL, ";")) { 728 snprintf(name, sizeof(name), "%s/geom_%s.ko", p, class_name); 729 /* 730 * If geom_<name>.ko file exists, "load" command is available. 731 */ 732 if (stat(name, &sb) == 0) 733 return (1); 734 } 735 return (0); 736 } 737 738 static void 739 std_load(struct gctl_req *req __unused, unsigned flags) 740 { 741 742 /* 743 * Do nothing special here, because of G_FLAG_LOADKLD flag, 744 * module is already loaded. 745 */ 746 if ((flags & G_FLAG_VERBOSE) != 0) 747 printf("Module available.\n"); 748 } 749 750 static int 751 std_unload_available(void) 752 { 753 char name[64]; 754 int id; 755 756 snprintf(name, sizeof(name), "geom_%s", class_name); 757 id = kldfind(name); 758 if (id >= 0) 759 return (1); 760 return (0); 761 } 762 763 static void 764 std_unload(struct gctl_req *req, unsigned flags __unused) 765 { 766 char name[64]; 767 int id; 768 769 snprintf(name, sizeof(name), "geom_%s", class_name); 770 id = kldfind(name); 771 if (id < 0) { 772 gctl_error(req, "Could not find module: %s.", strerror(errno)); 773 return; 774 } 775 if (kldunload(id) < 0) { 776 gctl_error(req, "Could not unload module: %s.", 777 strerror(errno)); 778 return; 779 } 780 } 781 782 static int 783 std_available(const char *name) 784 { 785 786 if (strcmp(name, "help") == 0) 787 return (1); 788 else if (strcmp(name, "list") == 0) 789 return (std_list_available()); 790 else if (strcmp(name, "load") == 0) 791 return (std_load_available()); 792 else if (strcmp(name, "unload") == 0) 793 return (std_unload_available()); 794 else 795 assert(!"Unknown standard command."); 796 return (0); 797 } 798