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 intmax_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_ASCLBA) { 263 /* 264 * LBAs are ugly. The argument is a sector. The size of a 265 * sector is context specific (i.e. determined by the media), 266 * which we don't know here. But when users enter a value 267 * with a SI unit, they really mean the byte-size or byte- 268 * offset and not the size or offset in sectors. 269 * So how can we map the byte-oriented value into a sector- 270 * oriented value if we don't know the sector size in bytes? 271 * The approach taken here is: 272 * o Sectors are 512 bytes in size. Mostly the case anyway. 273 * o When no SI unit is specified the value is in sectors. 274 * o With an SI unit the value is in bytes. 275 * o The 'b' suffix forces byte interpretation and the 's' 276 * suffix forces sector interpretation. 277 * 278 * Thus: 279 * o 2 and 2s mean 2 sectors, and 2b means 2 bytes. 280 * o 4k and 4kb mean 4096 bytes, and 4ks means 4096 sectors. 281 * 282 * "This seemed like a good idea at the time" 283 */ 284 intmax_t mult, unit; 285 286 number = strtoimax(val, &s, 0); 287 if (s == val) 288 errc(EXIT_FAILURE, EINVAL, "argument '%c'", 289 opt->go_char); 290 mult = 1; 291 unit = 512; /* sector */ 292 if (*s == '\0') 293 goto done; 294 switch (*s) { 295 case 'e': case 'E': 296 mult *= 1024; 297 /*FALLTHROUGH*/ 298 case 'p': case 'P': 299 mult *= 1024; 300 /*FALLTHROUGH*/ 301 case 't': case 'T': 302 mult *= 1024; 303 /*FALLTHROUGH*/ 304 case 'g': case 'G': 305 mult *= 1024; 306 /*FALLTHROUGH*/ 307 case 'm': case 'M': 308 mult *= 1024; 309 /*FALLTHROUGH*/ 310 case 'k': case 'K': 311 mult *= 1024; 312 break; 313 default: 314 goto sfx; 315 } 316 unit = 1; /* bytes */ 317 s++; 318 if (*s == '\0') 319 goto done; 320 sfx: 321 switch (*s) { 322 case 's': case 'S': 323 unit = 512; /* sector */ 324 break; 325 case 'b': case 'B': 326 unit = 1; /* bytes */ 327 break; 328 default: 329 errc(EXIT_FAILURE, EINVAL, "argument '%c': suffix '%c'", 330 opt->go_char, *s); 331 } 332 s++; 333 if (*s != '\0') 334 errx(EXIT_FAILURE, "argument '%c': junk at end (%s)", 335 opt->go_char, s); 336 done: 337 if (mult * unit < mult || number * mult * unit < number) 338 errc(EXIT_FAILURE, ERANGE, "argument '%c'", 339 opt->go_char); 340 number *= mult * unit; 341 if (number % 512) 342 errx(EXIT_FAILURE, "argument '%c': " 343 "not a valid block address", opt->go_char); 344 number /= 512; 345 asprintf(&s, "%jd", number); 346 if (s == NULL) 347 err(EXIT_FAILURE, NULL); 348 opt->go_val = s; 349 gctl_ro_param(req, opt->go_name, -1, s); 350 } else if (G_OPT_TYPE(opt) == G_TYPE_STRING) { 351 gctl_ro_param(req, opt->go_name, -1, val); 352 } else if (G_OPT_TYPE(opt) == G_TYPE_BOOL) { 353 opt->go_val = malloc(sizeof(int)); 354 if (opt->go_val == NULL) 355 errx(EXIT_FAILURE, "No memory."); 356 *(int *)opt->go_val = *val - '0'; 357 gctl_ro_param(req, opt->go_name, sizeof(int), opt->go_val); 358 } else { 359 assert(!"Invalid type"); 360 } 361 } 362 363 /* 364 * 1. Add given argument by caller. 365 * 2. Add default values of not given arguments. 366 * 3. Add the rest of arguments. 367 */ 368 static void 369 parse_arguments(struct g_command *cmd, struct gctl_req *req, int *argc, 370 char ***argv) 371 { 372 struct g_option *opt; 373 char opts[64]; 374 unsigned i; 375 int ch; 376 377 *opts = '\0'; 378 if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0) 379 strlcat(opts, "v", sizeof(opts)); 380 for (i = 0; ; i++) { 381 opt = &cmd->gc_options[i]; 382 if (opt->go_name == NULL) 383 break; 384 assert(G_OPT_TYPE(opt) != 0); 385 assert((opt->go_type & ~G_TYPE_MASK) == 0); 386 strlcatf(opts, sizeof(opts), "%c", opt->go_char); 387 if (G_OPT_TYPE(opt) != G_TYPE_BOOL) 388 strlcat(opts, ":", sizeof(opts)); 389 } 390 391 /* 392 * Add specified arguments. 393 */ 394 while ((ch = getopt(*argc, *argv, opts)) != -1) { 395 /* Standard (not passed to kernel) options. */ 396 switch (ch) { 397 case 'v': 398 verbose = 1; 399 continue; 400 } 401 /* Options passed to kernel. */ 402 opt = find_option(cmd, ch); 403 if (opt == NULL) 404 usage(); 405 if (G_OPT_ISDONE(opt)) { 406 warnx("Option '%c' specified twice.", opt->go_char); 407 usage(); 408 } 409 G_OPT_DONE(opt); 410 411 if (G_OPT_TYPE(opt) == G_TYPE_BOOL) 412 set_option(req, opt, "1"); 413 else 414 set_option(req, opt, optarg); 415 } 416 *argc -= optind; 417 *argv += optind; 418 419 /* 420 * Add not specified arguments, but with default values. 421 */ 422 for (i = 0; ; i++) { 423 opt = &cmd->gc_options[i]; 424 if (opt->go_name == NULL) 425 break; 426 if (G_OPT_ISDONE(opt)) 427 continue; 428 429 if (G_OPT_TYPE(opt) == G_TYPE_BOOL) { 430 assert(opt->go_val == NULL); 431 set_option(req, opt, "0"); 432 } else { 433 if (opt->go_val == NULL) { 434 warnx("Option '%c' not specified.", 435 opt->go_char); 436 usage(); 437 } else { 438 if (G_OPT_TYPE(opt) == G_TYPE_NUMBER) { 439 gctl_ro_param(req, opt->go_name, 440 sizeof(intmax_t), opt->go_val); 441 } else if (G_OPT_TYPE(opt) == G_TYPE_STRING || 442 G_OPT_TYPE(opt) == G_TYPE_ASCNUM || 443 G_OPT_TYPE(opt) == G_TYPE_ASCLBA) { 444 if (cmd->gc_argname == NULL || 445 opt->go_val == NULL || 446 *(char *)opt->go_val != '\0') 447 gctl_ro_param(req, opt->go_name, 448 -1, opt->go_val); 449 } else { 450 assert(!"Invalid type"); 451 } 452 } 453 } 454 } 455 456 if (cmd->gc_argname == NULL) { 457 /* 458 * Add rest of given arguments. 459 */ 460 gctl_ro_param(req, "nargs", sizeof(int), argc); 461 for (i = 0; i < (unsigned)*argc; i++) { 462 char argname[16]; 463 464 snprintf(argname, sizeof(argname), "arg%u", i); 465 gctl_ro_param(req, argname, -1, (*argv)[i]); 466 } 467 } else { 468 if (*argc != 1) 469 usage(); 470 gctl_ro_param(req, cmd->gc_argname, -1, (*argv)[0]); 471 } 472 } 473 474 /* 475 * Find given command in commands available for given class. 476 */ 477 static struct g_command * 478 find_command(const char *cmdstr, int flags) 479 { 480 struct g_command *cmd; 481 unsigned i; 482 483 /* 484 * First try to find command defined by loaded library. 485 */ 486 if ((flags & GEOM_CLASS_CMDS) != 0 && class_commands != NULL) { 487 for (i = 0; ; i++) { 488 cmd = &class_commands[i]; 489 if (cmd->gc_name == NULL) 490 break; 491 if (strcmp(cmd->gc_name, cmdstr) == 0) 492 return (cmd); 493 } 494 } 495 /* 496 * Now try to find in standard commands. 497 */ 498 if ((flags & GEOM_STD_CMDS) != 0) { 499 for (i = 0; ; i++) { 500 cmd = &std_commands[i]; 501 if (cmd->gc_name == NULL) 502 break; 503 if (strcmp(cmd->gc_name, cmdstr) == 0) 504 return (cmd); 505 } 506 } 507 return (NULL); 508 } 509 510 static unsigned 511 set_flags(struct g_command *cmd) 512 { 513 unsigned flags = 0; 514 515 if ((cmd->gc_flags & G_FLAG_VERBOSE) != 0 && verbose) 516 flags |= G_FLAG_VERBOSE; 517 518 return (flags); 519 } 520 521 /* 522 * Run command. 523 */ 524 static void 525 run_command(int argc, char *argv[]) 526 { 527 struct g_command *cmd; 528 struct gctl_req *req; 529 const char *errstr; 530 char buf[4096]; 531 532 /* First try to find a command defined by a class. */ 533 cmd = find_command(argv[0], GEOM_CLASS_CMDS); 534 if (cmd == NULL) { 535 /* Now, try to find a standard command. */ 536 cmd = find_command(argv[0], GEOM_STD_CMDS); 537 if (cmd == NULL) { 538 warnx("Unknown command: %s.", argv[0]); 539 usage(); 540 } 541 if (!std_available(cmd->gc_name)) { 542 warnx("Command '%s' not available.", argv[0]); 543 exit(EXIT_FAILURE); 544 } 545 } 546 if ((cmd->gc_flags & G_FLAG_LOADKLD) != 0) 547 load_module(); 548 549 req = gctl_get_handle(); 550 gctl_ro_param(req, "class", -1, gclass_name); 551 gctl_ro_param(req, "verb", -1, argv[0]); 552 if (version != NULL) 553 gctl_ro_param(req, "version", sizeof(*version), version); 554 parse_arguments(cmd, req, &argc, &argv); 555 556 bzero(buf, sizeof(buf)); 557 if (cmd->gc_func != NULL) { 558 unsigned flags; 559 560 flags = set_flags(cmd); 561 cmd->gc_func(req, flags); 562 errstr = req->error; 563 } else { 564 gctl_rw_param(req, "output", sizeof(buf), buf); 565 errstr = gctl_issue(req); 566 } 567 if (errstr != NULL && errstr[0] != '\0') { 568 warnx("%s", errstr); 569 if (strncmp(errstr, "warning: ", strlen("warning: ")) != 0) { 570 gctl_free(req); 571 exit(EXIT_FAILURE); 572 } 573 } 574 if (buf[0] != '\0') 575 printf("%s", buf); 576 gctl_free(req); 577 if (verbose) 578 printf("Done.\n"); 579 exit(EXIT_SUCCESS); 580 } 581 582 #ifndef STATIC_GEOM_CLASSES 583 static const char * 584 library_path(void) 585 { 586 const char *path; 587 588 path = getenv("GEOM_LIBRARY_PATH"); 589 if (path == NULL) 590 path = CLASS_DIR; 591 return (path); 592 } 593 594 static void 595 load_library(void) 596 { 597 char *curpath, path[MAXPATHLEN], *tofree, *totalpath; 598 uint32_t *lib_version; 599 void *dlh; 600 int ret; 601 602 ret = 0; 603 tofree = totalpath = strdup(library_path()); 604 if (totalpath == NULL) 605 err(EXIT_FAILURE, "Not enough memory for library path"); 606 607 if (strchr(totalpath, ':') != NULL) 608 curpath = strsep(&totalpath, ":"); 609 else 610 curpath = totalpath; 611 /* Traverse the paths to find one that contains the library we want. */ 612 while (curpath != NULL) { 613 snprintf(path, sizeof(path), "%s/geom_%s.so", curpath, 614 class_name); 615 ret = access(path, F_OK); 616 if (ret == -1) { 617 if (errno == ENOENT) { 618 /* 619 * If we cannot find library, try the next 620 * path. 621 */ 622 curpath = strsep(&totalpath, ":"); 623 continue; 624 } 625 err(EXIT_FAILURE, "Cannot access library"); 626 } 627 break; 628 } 629 free(tofree); 630 /* No library was found, but standard commands can still be used */ 631 if (ret == -1) 632 return; 633 dlh = dlopen(path, RTLD_NOW); 634 if (dlh == NULL) 635 errx(EXIT_FAILURE, "Cannot open library: %s.", dlerror()); 636 lib_version = dlsym(dlh, "lib_version"); 637 if (lib_version == NULL) { 638 warnx("Cannot find symbol %s: %s.", "lib_version", dlerror()); 639 dlclose(dlh); 640 exit(EXIT_FAILURE); 641 } 642 if (*lib_version != G_LIB_VERSION) { 643 dlclose(dlh); 644 errx(EXIT_FAILURE, "%s and %s are not synchronized.", 645 getprogname(), path); 646 } 647 version = dlsym(dlh, "version"); 648 if (version == NULL) { 649 warnx("Cannot find symbol %s: %s.", "version", dlerror()); 650 dlclose(dlh); 651 exit(EXIT_FAILURE); 652 } 653 class_commands = dlsym(dlh, "class_commands"); 654 if (class_commands == NULL) { 655 warnx("Cannot find symbol %s: %s.", "class_commands", 656 dlerror()); 657 dlclose(dlh); 658 exit(EXIT_FAILURE); 659 } 660 } 661 #endif /* !STATIC_GEOM_CLASSES */ 662 663 /* 664 * Class name should be all capital letters. 665 */ 666 static void 667 set_class_name(void) 668 { 669 char *s1, *s2; 670 671 s1 = class_name; 672 for (; *s1 != '\0'; s1++) 673 *s1 = tolower(*s1); 674 gclass_name = malloc(strlen(class_name) + 1); 675 if (gclass_name == NULL) 676 errx(EXIT_FAILURE, "No memory"); 677 s1 = gclass_name; 678 s2 = class_name; 679 for (; *s2 != '\0'; s2++) 680 *s1++ = toupper(*s2); 681 *s1 = '\0'; 682 } 683 684 static void 685 get_class(int *argc, char ***argv) 686 { 687 688 snprintf(comm, sizeof(comm), "%s", basename((*argv)[0])); 689 if (strcmp(comm, "geom") == 0) { 690 if (*argc < 2) 691 usage(); 692 else if (*argc == 2) { 693 if (strcmp((*argv)[1], "-h") == 0 || 694 strcmp((*argv)[1], "help") == 0) { 695 usage(); 696 } 697 } 698 strlcatf(comm, sizeof(comm), " %s", (*argv)[1]); 699 class_name = (*argv)[1]; 700 *argc -= 2; 701 *argv += 2; 702 } else if (*comm == 'g') { 703 class_name = comm + 1; 704 *argc -= 1; 705 *argv += 1; 706 } else { 707 errx(EXIT_FAILURE, "Invalid utility name."); 708 } 709 710 #ifndef STATIC_GEOM_CLASSES 711 load_library(); 712 #else 713 if (!strcasecmp(class_name, "part")) { 714 version = &gpart_version; 715 class_commands = gpart_class_commands; 716 } else if (!strcasecmp(class_name, "label")) { 717 version = &glabel_version; 718 class_commands = glabel_class_commands; 719 } else 720 errx(EXIT_FAILURE, "Invalid class name."); 721 #endif /* !STATIC_GEOM_CLASSES */ 722 723 set_class_name(); 724 if (*argc < 1) 725 usage(); 726 } 727 728 int 729 main(int argc, char *argv[]) 730 { 731 732 get_class(&argc, &argv); 733 run_command(argc, argv); 734 /* NOTREACHED */ 735 736 exit(EXIT_FAILURE); 737 } 738 739 static struct gclass * 740 find_class(struct gmesh *mesh, const char *name) 741 { 742 struct gclass *classp; 743 744 LIST_FOREACH(classp, &mesh->lg_class, lg_class) { 745 if (strcmp(classp->lg_name, name) == 0) 746 return (classp); 747 } 748 return (NULL); 749 } 750 751 static struct ggeom * 752 find_geom(struct gclass *classp, const char *name) 753 { 754 struct ggeom *gp; 755 756 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) { 757 if (strcmp(gp->lg_name, name) == 0) 758 return (gp); 759 } 760 return (NULL); 761 } 762 763 static void 764 list_one_provider(struct gprovider *pp, const char *prefix) 765 { 766 struct gconfig *conf; 767 char buf[5]; 768 769 printf("Name: %s\n", pp->lg_name); 770 humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "", 771 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 772 printf("%sMediasize: %jd (%s)\n", prefix, (intmax_t)pp->lg_mediasize, 773 buf); 774 printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize); 775 if (pp->lg_stripesize > 0 || pp->lg_stripeoffset > 0) { 776 printf("%sStripesize: %ju\n", prefix, pp->lg_stripesize); 777 printf("%sStripeoffset: %ju\n", prefix, pp->lg_stripeoffset); 778 } 779 printf("%sMode: %s\n", prefix, pp->lg_mode); 780 LIST_FOREACH(conf, &pp->lg_config, lg_config) { 781 printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val); 782 } 783 } 784 785 static void 786 list_one_consumer(struct gconsumer *cp, const char *prefix) 787 { 788 struct gprovider *pp; 789 struct gconfig *conf; 790 791 pp = cp->lg_provider; 792 if (pp == NULL) 793 printf("[no provider]\n"); 794 else { 795 char buf[5]; 796 797 printf("Name: %s\n", pp->lg_name); 798 humanize_number(buf, sizeof(buf), (int64_t)pp->lg_mediasize, "", 799 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 800 printf("%sMediasize: %jd (%s)\n", prefix, 801 (intmax_t)pp->lg_mediasize, buf); 802 printf("%sSectorsize: %u\n", prefix, pp->lg_sectorsize); 803 if (pp->lg_stripesize > 0 || pp->lg_stripeoffset > 0) { 804 printf("%sStripesize: %ju\n", prefix, pp->lg_stripesize); 805 printf("%sStripeoffset: %ju\n", prefix, pp->lg_stripeoffset); 806 } 807 printf("%sMode: %s\n", prefix, cp->lg_mode); 808 } 809 LIST_FOREACH(conf, &cp->lg_config, lg_config) { 810 printf("%s%s: %s\n", prefix, conf->lg_name, conf->lg_val); 811 } 812 } 813 814 static void 815 list_one_geom(struct ggeom *gp) 816 { 817 struct gprovider *pp; 818 struct gconsumer *cp; 819 struct gconfig *conf; 820 unsigned n; 821 822 printf("Geom name: %s\n", gp->lg_name); 823 LIST_FOREACH(conf, &gp->lg_config, lg_config) { 824 printf("%s: %s\n", conf->lg_name, conf->lg_val); 825 } 826 if (!LIST_EMPTY(&gp->lg_provider)) { 827 printf("Providers:\n"); 828 n = 1; 829 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) { 830 printf("%u. ", n++); 831 list_one_provider(pp, " "); 832 } 833 } 834 if (!LIST_EMPTY(&gp->lg_consumer)) { 835 printf("Consumers:\n"); 836 n = 1; 837 LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) { 838 printf("%u. ", n++); 839 list_one_consumer(cp, " "); 840 } 841 } 842 printf("\n"); 843 } 844 845 static void 846 std_help(struct gctl_req *req __unused, unsigned flags __unused) 847 { 848 849 usage(); 850 } 851 852 static int 853 std_list_available(void) 854 { 855 struct gmesh mesh; 856 struct gclass *classp; 857 int error; 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 geom_deletetree(&mesh); 864 if (classp != NULL) 865 return (1); 866 return (0); 867 } 868 869 static void 870 std_list(struct gctl_req *req, unsigned flags __unused) 871 { 872 struct gmesh mesh; 873 struct gclass *classp; 874 struct ggeom *gp; 875 const char *name; 876 int error, i, nargs; 877 878 error = geom_gettree(&mesh); 879 if (error != 0) 880 errc(EXIT_FAILURE, error, "Cannot get GEOM tree"); 881 classp = find_class(&mesh, gclass_name); 882 if (classp == NULL) { 883 geom_deletetree(&mesh); 884 errx(EXIT_FAILURE, "Class %s not found.", gclass_name); 885 } 886 nargs = gctl_get_int(req, "nargs"); 887 if (nargs > 0) { 888 for (i = 0; i < nargs; i++) { 889 name = gctl_get_ascii(req, "arg%d", i); 890 gp = find_geom(classp, name); 891 if (gp != NULL) 892 list_one_geom(gp); 893 else 894 errx(EXIT_FAILURE, "No such geom: %s.", name); 895 } 896 } else { 897 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) { 898 if (LIST_EMPTY(&gp->lg_provider)) 899 continue; 900 list_one_geom(gp); 901 } 902 } 903 geom_deletetree(&mesh); 904 } 905 906 static int 907 std_status_available(void) 908 { 909 910 /* 'status' command is available when 'list' command is. */ 911 return (std_list_available()); 912 } 913 914 static void 915 status_update_len(struct ggeom *gp, int *name_len, int *status_len) 916 { 917 struct gprovider *pp; 918 struct gconfig *conf; 919 int len; 920 921 assert(gp != NULL); 922 assert(name_len != NULL); 923 assert(status_len != NULL); 924 925 pp = LIST_FIRST(&gp->lg_provider); 926 if (pp != NULL) 927 len = strlen(pp->lg_name); 928 else 929 len = strlen(gp->lg_name); 930 if (*name_len < len) 931 *name_len = len; 932 LIST_FOREACH(conf, &gp->lg_config, lg_config) { 933 if (strcasecmp(conf->lg_name, "state") == 0) { 934 len = strlen(conf->lg_val); 935 if (*status_len < len) 936 *status_len = len; 937 } 938 } 939 } 940 941 static char * 942 status_one_consumer(struct gconsumer *cp) 943 { 944 static char buf[256]; 945 struct gprovider *pp; 946 struct gconfig *conf; 947 948 pp = cp->lg_provider; 949 if (pp == NULL) 950 return (NULL); 951 LIST_FOREACH(conf, &cp->lg_config, lg_config) { 952 if (strcasecmp(conf->lg_name, "synchronized") == 0) 953 break; 954 } 955 if (conf == NULL) 956 snprintf(buf, sizeof(buf), "%s", pp->lg_name); 957 else { 958 snprintf(buf, sizeof(buf), "%s (%s)", pp->lg_name, 959 conf->lg_val); 960 } 961 return (buf); 962 } 963 964 static void 965 status_one_geom(struct ggeom *gp, int script, int name_len, int status_len) 966 { 967 struct gprovider *pp; 968 struct gconsumer *cp; 969 struct gconfig *conf; 970 const char *name, *status, *component; 971 int gotone; 972 973 pp = LIST_FIRST(&gp->lg_provider); 974 if (pp != NULL) 975 name = pp->lg_name; 976 else 977 name = gp->lg_name; 978 LIST_FOREACH(conf, &gp->lg_config, lg_config) { 979 if (strcasecmp(conf->lg_name, "state") == 0) 980 break; 981 } 982 if (conf == NULL) 983 status = "N/A"; 984 else 985 status = conf->lg_val; 986 gotone = 0; 987 LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) { 988 component = status_one_consumer(cp); 989 if (component == NULL) 990 continue; 991 gotone = 1; 992 printf("%*s %*s %s\n", name_len, name, status_len, status, 993 component); 994 if (!script) 995 name = status = ""; 996 } 997 if (!gotone) { 998 printf("%*s %*s %s\n", name_len, name, status_len, status, 999 "N/A"); 1000 } 1001 } 1002 1003 static void 1004 std_status(struct gctl_req *req, unsigned flags __unused) 1005 { 1006 struct gmesh mesh; 1007 struct gclass *classp; 1008 struct ggeom *gp; 1009 const char *name; 1010 int name_len, status_len; 1011 int error, i, n, nargs, script; 1012 1013 error = geom_gettree(&mesh); 1014 if (error != 0) 1015 errc(EXIT_FAILURE, error, "Cannot get GEOM tree"); 1016 classp = find_class(&mesh, gclass_name); 1017 if (classp == NULL) 1018 errx(EXIT_FAILURE, "Class %s not found.", gclass_name); 1019 nargs = gctl_get_int(req, "nargs"); 1020 script = gctl_get_int(req, "script"); 1021 name_len = strlen("Name"); 1022 status_len = strlen("Status"); 1023 if (nargs > 0) { 1024 for (i = 0, n = 0; i < nargs; i++) { 1025 name = gctl_get_ascii(req, "arg%d", i); 1026 gp = find_geom(classp, name); 1027 if (gp == NULL) 1028 errx(EXIT_FAILURE, "No such geom: %s.", name); 1029 else { 1030 status_update_len(gp, &name_len, &status_len); 1031 n++; 1032 } 1033 } 1034 if (n == 0) 1035 goto end; 1036 } else { 1037 n = 0; 1038 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) { 1039 if (LIST_EMPTY(&gp->lg_provider)) 1040 continue; 1041 status_update_len(gp, &name_len, &status_len); 1042 n++; 1043 } 1044 if (n == 0) 1045 goto end; 1046 } 1047 if (!script) { 1048 printf("%*s %*s %s\n", name_len, "Name", status_len, "Status", 1049 "Components"); 1050 } 1051 if (nargs > 0) { 1052 for (i = 0; i < nargs; i++) { 1053 name = gctl_get_ascii(req, "arg%d", i); 1054 gp = find_geom(classp, name); 1055 if (gp != NULL) { 1056 status_one_geom(gp, script, name_len, 1057 status_len); 1058 } 1059 } 1060 } else { 1061 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) { 1062 if (LIST_EMPTY(&gp->lg_provider)) 1063 continue; 1064 status_one_geom(gp, script, name_len, status_len); 1065 } 1066 } 1067 end: 1068 geom_deletetree(&mesh); 1069 } 1070 1071 static int 1072 std_load_available(void) 1073 { 1074 char name[MAXPATHLEN], paths[MAXPATHLEN * 8], *p; 1075 struct stat sb; 1076 size_t len; 1077 1078 snprintf(name, sizeof(name), "g_%s", class_name); 1079 /* 1080 * If already in kernel, "load" command is not available. 1081 */ 1082 if (modfind(name) >= 0) 1083 return (0); 1084 bzero(paths, sizeof(paths)); 1085 len = sizeof(paths); 1086 if (sysctlbyname("kern.module_path", paths, &len, NULL, 0) < 0) 1087 err(EXIT_FAILURE, "sysctl(kern.module_path)"); 1088 for (p = strtok(paths, ";"); p != NULL; p = strtok(NULL, ";")) { 1089 snprintf(name, sizeof(name), "%s/geom_%s.ko", p, class_name); 1090 /* 1091 * If geom_<name>.ko file exists, "load" command is available. 1092 */ 1093 if (stat(name, &sb) == 0) 1094 return (1); 1095 } 1096 return (0); 1097 } 1098 1099 static void 1100 std_load(struct gctl_req *req __unused, unsigned flags) 1101 { 1102 1103 /* 1104 * Do nothing special here, because of G_FLAG_LOADKLD flag, 1105 * module is already loaded. 1106 */ 1107 if ((flags & G_FLAG_VERBOSE) != 0) 1108 printf("Module available.\n"); 1109 } 1110 1111 static int 1112 std_unload_available(void) 1113 { 1114 char name[64]; 1115 int id; 1116 1117 snprintf(name, sizeof(name), "geom_%s", class_name); 1118 id = kldfind(name); 1119 if (id >= 0) 1120 return (1); 1121 return (0); 1122 } 1123 1124 static void 1125 std_unload(struct gctl_req *req, unsigned flags __unused) 1126 { 1127 char name[64]; 1128 int id; 1129 1130 snprintf(name, sizeof(name), "geom_%s", class_name); 1131 id = kldfind(name); 1132 if (id < 0) { 1133 gctl_error(req, "Could not find module: %s.", strerror(errno)); 1134 return; 1135 } 1136 if (kldunload(id) < 0) { 1137 gctl_error(req, "Could not unload module: %s.", 1138 strerror(errno)); 1139 return; 1140 } 1141 } 1142 1143 static int 1144 std_available(const char *name) 1145 { 1146 1147 if (strcmp(name, "help") == 0) 1148 return (1); 1149 else if (strcmp(name, "list") == 0) 1150 return (std_list_available()); 1151 else if (strcmp(name, "status") == 0) 1152 return (std_status_available()); 1153 else if (strcmp(name, "load") == 0) 1154 return (std_load_available()); 1155 else if (strcmp(name, "unload") == 0) 1156 return (std_unload_available()); 1157 else 1158 assert(!"Unknown standard command."); 1159 return (0); 1160 } 1161