1 /* 2 * Copyright (c) 2001 Dima Dorfman. 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 AUTHOR 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 AUTHOR 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 /* 28 * mdmfs (md/MFS) is a wrapper around mdconfig(8), 29 * newfs(8), and mount(8) that mimics the command line option set of 30 * the deprecated mount_mfs(8). 31 */ 32 33 #ifndef lint 34 static const char rcsid[] = 35 "$FreeBSD$"; 36 #endif /* not lint */ 37 38 #include <sys/param.h> 39 #include <sys/mdioctl.h> 40 #include <sys/stat.h> 41 #include <sys/wait.h> 42 43 #include <assert.h> 44 #include <err.h> 45 #include <fcntl.h> 46 #include <grp.h> 47 #include <paths.h> 48 #include <pwd.h> 49 #include <stdarg.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <string.h> 53 #include <unistd.h> 54 55 #include "pathnames.h" 56 57 typedef enum { false, true } bool; 58 59 struct mtpt_info { 60 uid_t mi_uid; 61 bool mi_have_uid; 62 gid_t mi_gid; 63 bool mi_have_gid; 64 mode_t mi_mode; 65 bool mi_have_mode; 66 }; 67 68 static bool compat; /* Full compatibility with mount_mfs? */ 69 static bool debug; /* Emit debugging information? */ 70 static bool loudsubs; /* Suppress output from helper programs? */ 71 static bool norun; /* Actually run the helper programs? */ 72 static int unit; /* The unit we're working with. */ 73 static const char *mdname; /* Name of memory disk device (e.g., "md"). */ 74 static size_t mdnamelen; /* Length of mdname. */ 75 76 static void argappend(char **, const char *, ...) __printflike(2, 3); 77 static void debugprintf(const char *, ...) __printflike(1, 2); 78 static void do_mdconfig_attach(const char *, const enum md_types); 79 static void do_mdconfig_attach_au(const char *, const enum md_types); 80 static void do_mdconfig_detach(void); 81 static void do_mount(const char *, const char *); 82 static void do_mtptsetup(const char *, struct mtpt_info *); 83 static void do_newfs(const char *); 84 static void extract_ugid(const char *, struct mtpt_info *); 85 static int run(int *, const char *, ...) __printflike(2, 3); 86 static void usage(void); 87 88 int 89 main(int argc, char **argv) 90 { 91 struct mtpt_info mi; /* Mountpoint info. */ 92 char *mdconfig_arg, *newfs_arg, /* Args to helper programs. */ 93 *mount_arg; 94 enum md_types mdtype; /* The type of our memory disk. */ 95 bool have_mdtype; 96 bool detach, softdep, autounit; 97 char *mtpoint, *unitstr; 98 char ch, *p; 99 100 /* Misc. initialization. */ 101 (void)memset(&mi, '\0', sizeof(mi)); 102 detach = true; 103 softdep = true; 104 autounit = false; 105 have_mdtype = false; 106 mdname = MD_NAME; 107 mdnamelen = strlen(mdname); 108 /* 109 * Can't set these to NULL. They may be passed to the 110 * respective programs without modification. I.e., we may not 111 * receive any command-line options which will caused them to 112 * be modified. 113 */ 114 mdconfig_arg = strdup(""); 115 newfs_arg = strdup(""); 116 mount_arg = strdup(""); 117 118 /* If we were started as mount_mfs or mfs, imply -C. */ 119 if (strcmp(getprogname(), "mount_mfs") == 0 || 120 strcmp(getprogname(), "mfs") == 0) 121 compat = true; 122 123 while ((ch = getopt(argc, argv, 124 "a:b:Cc:Dd:e:F:f:hi:LMm:Nn:O:o:p:Ss:t:Uv:w:X")) != -1) 125 switch (ch) { 126 case 'a': 127 argappend(&newfs_arg, "-a %s", optarg); 128 break; 129 case 'b': 130 argappend(&newfs_arg, "-b %s", optarg); 131 break; 132 case 'C': 133 if (compat) 134 usage(); 135 compat = true; 136 break; 137 case 'c': 138 argappend(&newfs_arg, "-c %s", optarg); 139 break; 140 case 'D': 141 if (compat) 142 usage(); 143 detach = false; 144 break; 145 case 'd': 146 argappend(&newfs_arg, "-d %s", optarg); 147 break; 148 case 'e': 149 argappend(&newfs_arg, "-e %s", optarg); 150 break; 151 case 'F': 152 if (have_mdtype) 153 usage(); 154 mdtype = MD_VNODE; 155 have_mdtype = true; 156 argappend(&mdconfig_arg, "-f %s", optarg); 157 break; 158 case 'f': 159 argappend(&newfs_arg, "-f %s", optarg); 160 break; 161 case 'h': 162 usage(); 163 break; 164 case 'i': 165 argappend(&newfs_arg, "-i %s", optarg); 166 break; 167 case 'L': 168 if (compat) 169 usage(); 170 loudsubs = true; 171 break; 172 case 'M': 173 if (have_mdtype) 174 usage(); 175 mdtype = MD_MALLOC; 176 have_mdtype = true; 177 break; 178 case 'm': 179 argappend(&newfs_arg, "-m %s", optarg); 180 break; 181 case 'N': 182 if (compat) 183 usage(); 184 norun = true; 185 break; 186 case 'n': 187 argappend(&newfs_arg, "-n %s", optarg); 188 break; 189 case 'O': 190 argappend(&newfs_arg, "-o %s", optarg); 191 break; 192 case 'o': 193 argappend(&mount_arg, "-o %s", optarg); 194 break; 195 case 'p': 196 if (compat) 197 usage(); 198 if (*optarg >= '0' && *optarg <= '7') 199 mi.mi_mode = strtol(optarg, NULL, 8); 200 if ((mi.mi_mode & ~07777) != 0) 201 usage(); 202 mi.mi_have_mode = true; 203 break; 204 case 'S': 205 if (compat) 206 usage(); 207 softdep = false; 208 break; 209 case 's': 210 argappend(&mdconfig_arg, "-s %s", optarg); 211 break; 212 case 'U': 213 softdep = true; 214 break; 215 case 'v': 216 argappend(&newfs_arg, "-O %s", optarg); 217 break; 218 case 'w': 219 if (compat) 220 usage(); 221 extract_ugid(optarg, &mi); 222 break; 223 case 'X': 224 if (compat) 225 usage(); 226 debug = true; 227 break; 228 default: 229 usage(); 230 } 231 argc -= optind; 232 argv += optind; 233 if (argc < 2) 234 usage(); 235 236 /* Make compatibility assumptions. */ 237 if (compat) { 238 mi.mi_mode = 01777; 239 mi.mi_have_mode = true; 240 } 241 242 /* Derive 'unit' (global). */ 243 unitstr = argv[0]; 244 if (strncmp(unitstr, "/dev/", 5) == 0) 245 unitstr += 5; 246 if (strncmp(unitstr, mdname, mdnamelen) == 0) 247 unitstr += mdnamelen; 248 if (*unitstr == '\0') { 249 autounit = true; 250 unit = -1; 251 } else { 252 unit = strtoul(unitstr, &p, 10); 253 if ((unsigned)unit == ULONG_MAX || *p != '\0') 254 errx(1, "bad device unit: %s", unitstr); 255 } 256 257 mtpoint = argv[1]; 258 if (!have_mdtype) 259 mdtype = MD_SWAP; 260 if (softdep) 261 argappend(&newfs_arg, "-U"); 262 263 /* Do the work. */ 264 if (detach && !autounit) 265 do_mdconfig_detach(); 266 if (autounit) 267 do_mdconfig_attach_au(mdconfig_arg, mdtype); 268 else 269 do_mdconfig_attach(mdconfig_arg, mdtype); 270 do_newfs(newfs_arg); 271 do_mount(mount_arg, mtpoint); 272 do_mtptsetup(mtpoint, &mi); 273 274 return (0); 275 } 276 277 /* 278 * Append the expansion of 'fmt' to the buffer pointed to by '*dstp'; 279 * reallocate as required. 280 */ 281 static void 282 argappend(char **dstp, const char *fmt, ...) 283 { 284 char *old, *new; 285 va_list ap; 286 287 old = *dstp; 288 assert(old != NULL); 289 290 va_start(ap, fmt); 291 if (vasprintf(&new, fmt,ap) == -1) 292 errx(1, "vasprintf"); 293 va_end(ap); 294 295 *dstp = new; 296 if (asprintf(&new, "%s %s", old, new) == -1) 297 errx(1, "asprintf"); 298 free(*dstp); 299 free(old); 300 301 *dstp = new; 302 } 303 304 /* 305 * If run-time debugging is enabled, print the expansion of 'fmt'. 306 * Otherwise, do nothing. 307 */ 308 static void 309 debugprintf(const char *fmt, ...) 310 { 311 va_list ap; 312 313 if (!debug) 314 return; 315 fprintf(stderr, "DEBUG: "); 316 va_start(ap, fmt); 317 vfprintf(stderr, fmt, ap); 318 va_end(ap); 319 fprintf(stderr, "\n"); 320 fflush(stderr); 321 } 322 323 /* 324 * Attach a memory disk with a known unit. 325 */ 326 static void 327 do_mdconfig_attach(const char *args, const enum md_types mdtype) 328 { 329 int rv; 330 const char *ta; /* Type arg. */ 331 332 switch (mdtype) { 333 case MD_SWAP: 334 ta = "-t swap"; 335 break; 336 case MD_VNODE: 337 ta = "-t vnode"; 338 break; 339 case MD_MALLOC: 340 ta = "-t malloc"; 341 break; 342 default: 343 abort(); 344 } 345 rv = run(NULL, "%s -a %s%s -u %s%d", PATH_MDCONFIG, ta, args, 346 mdname, unit); 347 if (rv) 348 errx(1, "mdconfig (attach) exited with error code %d", rv); 349 } 350 351 /* 352 * Attach a memory disk with an unknown unit; use autounit. 353 */ 354 static void 355 do_mdconfig_attach_au(const char *args, const enum md_types mdtype) 356 { 357 const char *ta; /* Type arg. */ 358 char *linep, *linebuf; /* Line pointer, line buffer. */ 359 int fd; /* Standard output of mdconfig invocation. */ 360 FILE *sfd; 361 int rv; 362 char *p; 363 size_t linelen; 364 365 switch (mdtype) { 366 case MD_SWAP: 367 ta = "-t swap"; 368 break; 369 case MD_VNODE: 370 ta = "-t vnode"; 371 break; 372 case MD_MALLOC: 373 ta = "-t malloc"; 374 break; 375 default: 376 abort(); 377 } 378 rv = run(&fd, "%s -a %s%s", PATH_MDCONFIG, ta, args); 379 if (rv) 380 errx(1, "mdconfig (attach) exited with error code %d", rv); 381 382 /* Receive the unit number. */ 383 if (norun) { /* Since we didn't run, we can't read. Fake it. */ 384 unit = -1; 385 return; 386 } 387 sfd = fdopen(fd, "r"); 388 if (sfd == NULL) 389 err(1, "fdopen"); 390 linep = fgetln(sfd, &linelen); 391 if (linep == NULL && linelen < mdnamelen + 1) 392 errx(1, "unexpected output from mdconfig (attach)"); 393 /* If the output format changes, we want to know about it. */ 394 assert(strncmp(linep, mdname, mdnamelen) == 0); 395 linebuf = malloc(linelen - mdnamelen + 1); 396 assert(linebuf != NULL); 397 /* Can't use strlcpy because linep is not NULL-terminated. */ 398 strncpy(linebuf, linep + mdnamelen, linelen); 399 linebuf[linelen] = '\0'; 400 unit = strtoul(linebuf, &p, 10); 401 if ((unsigned)unit == ULONG_MAX || *p != '\n') 402 errx(1, "unexpected output from mdconfig (attach)"); 403 404 fclose(sfd); 405 close(fd); 406 } 407 408 /* 409 * Detach a memory disk. 410 */ 411 static void 412 do_mdconfig_detach(void) 413 { 414 int rv; 415 416 rv = run(NULL, "%s -d -u %s%d", PATH_MDCONFIG, mdname, unit); 417 if (rv && debug) /* This is allowed to fail. */ 418 warnx("mdconfig (detach) exited with error code %d (ignored)", 419 rv); 420 } 421 422 /* 423 * Mount the configured memory disk. 424 */ 425 static void 426 do_mount(const char *args, const char *mtpoint) 427 { 428 int rv; 429 430 rv = run(NULL, "%s%s /dev/%s%d %s", PATH_MOUNT, args, 431 mdname, unit, mtpoint); 432 if (rv) 433 errx(1, "mount exited with error code %d", rv); 434 } 435 436 /* 437 * Various configuration of the mountpoint. Mostly, enact 'mip'. 438 */ 439 static void 440 do_mtptsetup(const char *mtpoint, struct mtpt_info *mip) 441 { 442 443 if (mip->mi_have_mode) { 444 debugprintf("changing mode of %s to %o.", mtpoint, 445 mip->mi_mode); 446 if (!norun) 447 if (chmod(mtpoint, mip->mi_mode) == -1) 448 err(1, "chmod: %s", mtpoint); 449 } 450 /* 451 * We have to do these separately because the user may have 452 * only specified one of them. 453 */ 454 if (mip->mi_have_uid) { 455 debugprintf("changing owner (user) or %s to %u.", mtpoint, 456 mip->mi_uid); 457 if (!norun) 458 if (chown(mtpoint, mip->mi_uid, -1) == -1) 459 err(1, "chown %s to %u (user)", mtpoint, 460 mip->mi_uid); 461 } 462 if (mip->mi_have_gid) { 463 debugprintf("changing owner (group) or %s to %u.", mtpoint, 464 mip->mi_gid); 465 if (!norun) 466 if (chown(mtpoint, -1, mip->mi_gid) == -1) 467 err(1, "chown %s to %u (group)", mtpoint, 468 mip->mi_gid); 469 } 470 } 471 472 /* 473 * Put a file system on the memory disk. 474 */ 475 static void 476 do_newfs(const char *args) 477 { 478 int rv; 479 480 rv = run(NULL, "%s%s /dev/%s%d", PATH_NEWFS, args, mdname, unit); 481 if (rv) 482 errx(1, "newfs exited with error code %d", rv); 483 } 484 485 /* 486 * 'str' should be a user and group name similar to the last argument 487 * to chown(1); i.e., a user, followed by a colon, followed by a 488 * group. The user and group in 'str' may be either a [ug]id or a 489 * name. Upon return, the uid and gid fields in 'mip' will contain 490 * the uid and gid of the user and group name in 'str', respectively. 491 * 492 * In other words, this derives a user and group id from a string 493 * formatted like the last argument to chown(1). 494 */ 495 static void 496 extract_ugid(const char *str, struct mtpt_info *mip) 497 { 498 char *ug; /* Writable 'str'. */ 499 char *user, *group; /* Result of extracton. */ 500 struct passwd *pw; 501 struct group *gr; 502 char *p; 503 uid_t *uid; 504 gid_t *gid; 505 506 uid = &mip->mi_uid; 507 gid = &mip->mi_gid; 508 mip->mi_have_uid = mip->mi_have_gid = false; 509 510 /* Extract the user and group from 'str'. Format above. */ 511 ug = strdup(str); 512 assert(ug != NULL); 513 group = ug; 514 user = strsep(&group, ":"); 515 if (user == NULL || group == NULL || *user == '\0' || *group == '\0') 516 usage(); 517 518 /* Derive uid. */ 519 *uid = strtoul(user, &p, 10); 520 if ((unsigned)*uid == ULONG_MAX) 521 usage(); 522 if (*p != '\0') { 523 pw = getpwnam(user); 524 if (pw == NULL) 525 errx(1, "invalid user: %s", user); 526 *uid = pw->pw_uid; 527 mip->mi_have_uid = true; 528 } 529 530 /* Derive gid. */ 531 *gid = strtoul(group, &p, 10); 532 if ((unsigned)*gid == ULONG_MAX) 533 usage(); 534 if (*p != '\0') { 535 gr = getgrnam(group); 536 if (gr == NULL) 537 errx(1, "invalid group: %s", group); 538 *gid = gr->gr_gid; 539 mip->mi_have_gid = true; 540 } 541 542 free(ug); 543 /* 544 * At this point we don't support only a username or only a 545 * group name. do_mtptsetup already does, so when this 546 * feature is desired, this is the only routine that needs to 547 * be changed. 548 */ 549 assert(mip->mi_have_uid); 550 assert(mip->mi_have_gid); 551 } 552 553 /* 554 * Run a process with command name and arguments pointed to by the 555 * formatted string 'cmdline'. Since system(3) is not used, the first 556 * space-delimited token of 'cmdline' must be the full pathname of the 557 * program to run. The return value is the return code of the process 558 * spawned. If 'ofd' is non-NULL, it is set to the standard output of 559 * the program spawned (i.e., you can read from ofd and get the output 560 * of the program). 561 */ 562 static int 563 run(int *ofd, const char *cmdline, ...) 564 { 565 char **argv, **argvp; /* Result of splitting 'cmd'. */ 566 int argc; 567 char *cmd; /* Expansion of 'cmdline'. */ 568 int pid, status; /* Child info. */ 569 int pfd[2]; /* Pipe to the child. */ 570 int nfd; /* Null (/dev/null) file descriptor. */ 571 bool dup2dn; /* Dup /dev/null to stdout? */ 572 va_list ap; 573 char *p; 574 int rv, i; 575 576 dup2dn = true; 577 va_start(ap, cmdline); 578 rv = vasprintf(&cmd, cmdline, ap); 579 if (rv == -1) 580 err(1, "vasprintf"); 581 va_end(ap); 582 583 /* Split up 'cmd' into 'argv' for use with execve. */ 584 for (argc = 1, p = cmd; (p = strchr(p, ' ')) != NULL; p++) 585 argc++; /* 'argc' generation loop. */ 586 argv = (char **)malloc(sizeof(*argv) * (argc + 1)); 587 assert(argv != NULL); 588 for (p = cmd, argvp = argv; (*argvp = strsep(&p, " ")) != NULL;) 589 if (**argv != '\0') 590 if (++argvp >= &argv[argc]) { 591 *argvp = NULL; 592 break; 593 } 594 assert(*argv); 595 596 /* Make sure the above loop works as expected. */ 597 if (debug) { 598 /* 599 * We can't, but should, use debugprintf here. First, 600 * it appends a trailing newline to the output, and 601 * second it prepends "DEBUG: " to the output. The 602 * former is a problem for this would-be first call, 603 * and the latter for the would-be call inside the 604 * loop. 605 */ 606 (void)fprintf(stderr, "DEBUG: running:"); 607 /* Should be equivilent to 'cmd' (before strsep, of course). */ 608 for (i = 0; argv[i] != NULL; i++) 609 (void)fprintf(stderr, " %s", argv[i]); 610 (void)fprintf(stderr, "\n"); 611 } 612 613 /* Create a pipe if necessary and fork the helper program. */ 614 if (ofd != NULL) { 615 if (pipe(&pfd[0]) == -1) 616 err(1, "pipe"); 617 *ofd = pfd[0]; 618 dup2dn = false; 619 } 620 pid = fork(); 621 switch (pid) { 622 case 0: 623 /* XXX can we call err() in here? */ 624 if (norun) 625 _exit(0); 626 if (ofd != NULL) 627 if (dup2(pfd[1], STDOUT_FILENO) < 0) 628 err(1, "dup2"); 629 if (!loudsubs) { 630 nfd = open(_PATH_DEVNULL, O_RDWR); 631 if (nfd == -1) 632 err(1, "open: %s", _PATH_DEVNULL); 633 if (dup2(nfd, STDIN_FILENO) < 0) 634 err(1, "dup2"); 635 if (dup2dn) 636 if (dup2(nfd, STDOUT_FILENO) < 0) 637 err(1, "dup2"); 638 if (dup2(nfd, STDERR_FILENO) < 0) 639 err(1, "dup2"); 640 } 641 642 (void)execv(argv[0], argv); 643 warn("exec: %s", argv[0]); 644 _exit(-1); 645 case -1: 646 err(1, "fork"); 647 } 648 649 free(cmd); 650 free(argv); 651 while (waitpid(pid, &status, 0) != pid) 652 ; 653 return (WEXITSTATUS(status)); 654 } 655 656 static void 657 usage(void) 658 { 659 const char *name; 660 661 if (compat) 662 name = getprogname(); 663 else 664 name = "mdmfs"; 665 if (!compat) 666 fprintf(stderr, 667 "usage: %s [-DLMNSUX] [-a maxcontig [-b block-size] [-c cylinders]\n" 668 "\t[-d rotdelay] [-e maxbpg] [-F file] [-f frag-size] [-i bytes]\n" 669 "\t[-m percent-free] [-n rotational-positions] [-O optimization]\n" 670 "\t[-o mount-options] [-p permissions] [-s size] [-w user:group]\n" 671 "\tmd-device mount-point\n", name); 672 fprintf(stderr, 673 "usage: %s -C [-NU] [-a maxcontig] [-b block-size] [-c cylinders]\n" 674 "\t[-d rotdelay] [-e maxbpg] [-F file] [-f frag-size] [-i bytes]\n" 675 "\t[-m percent-free] [-n rotational-positions] [-O optimization]\n" 676 "\t[-o mount-options] [-s size] md-device mount-point\n", name); 677 exit(1); 678 } 679