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