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