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