2 * Copyright (c) 2001 Dima Dorfman.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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).
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/mdioctl.h>
38 #include <sys/mount.h>
55 typedef enum { false, true } bool;
67 static bool debug; /* Emit debugging information? */
68 static bool loudsubs; /* Suppress output from helper programs? */
69 static bool norun; /* Actually run the helper programs? */
70 static int unit; /* The unit we're working with. */
71 static const char *mdname; /* Name of memory disk device (e.g., "md"). */
72 static const char *mdsuffix; /* Suffix of memory disk device (e.g., ".uzip"). */
73 static size_t mdnamelen; /* Length of mdname. */
74 static const char *path_mdconfig =_PATH_MDCONFIG;
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);
89 main(int argc, char **argv)
91 struct mtpt_info mi; /* Mountpoint info. */
92 char *mdconfig_arg, *newfs_arg, /* Args to helper programs. */
94 enum md_types mdtype; /* The type of our memory disk. */
96 bool detach, softdep, autounit, newfs;
97 char *mtpoint, *unitstr;
103 /* Misc. initialization. */
104 (void)memset(&mi, '\0', sizeof(mi));
112 mdnamelen = strlen(mdname);
114 * Can't set these to NULL. They may be passed to the
115 * respective programs without modification. I.e., we may not
116 * receive any command-line options which will caused them to
119 mdconfig_arg = strdup("");
120 newfs_arg = strdup("");
121 mount_arg = strdup("");
123 /* If we were started as mount_mfs or mfs, imply -C. */
124 if (strcmp(getprogname(), "mount_mfs") == 0 ||
125 strcmp(getprogname(), "mfs") == 0) {
126 /* Make compatibility assumptions. */
128 mi.mi_have_mode = true;
131 while ((ch = getopt(argc, argv,
132 "a:b:Cc:Dd:E:e:F:f:hi:LlMm:NnO:o:Pp:Ss:tUv:w:X")) != -1)
135 argappend(&newfs_arg, "-a %s", optarg);
138 argappend(&newfs_arg, "-b %s", optarg);
141 /* Ignored for compatibility. */
144 argappend(&newfs_arg, "-c %s", optarg);
150 argappend(&newfs_arg, "-d %s", optarg);
153 path_mdconfig = optarg;
156 argappend(&newfs_arg, "-e %s", optarg);
163 argappend(&mdconfig_arg, "-f %s", optarg);
166 argappend(&newfs_arg, "-f %s", optarg);
172 argappend(&newfs_arg, "-i %s", optarg);
178 argappend(&newfs_arg, "-l");
187 argappend(&newfs_arg, "-m %s", optarg);
193 argappend(&newfs_arg, "-n");
196 argappend(&newfs_arg, "-o %s", optarg);
199 argappend(&mount_arg, "-o %s", optarg);
205 if ((set = setmode(optarg)) == NULL)
207 mi.mi_mode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO);
208 mi.mi_have_mode = true;
209 mi.mi_forced_pw = true;
216 argappend(&mdconfig_arg, "-s %s", optarg);
219 argappend(&newfs_arg, "-t");
225 argappend(&newfs_arg, "-O %s", optarg);
228 extract_ugid(optarg, &mi);
229 mi.mi_forced_pw = true;
242 /* Derive 'unit' (global). */
244 if (strncmp(unitstr, "/dev/", 5) == 0)
246 if (strncmp(unitstr, mdname, mdnamelen) == 0)
247 unitstr += mdnamelen;
248 if (!isdigit(*unitstr)) {
253 ul = strtoul(unitstr, &p, 10);
255 errx(1, "bad device unit: %s", unitstr);
257 mdsuffix = p; /* can be empty */
264 argappend(&newfs_arg, "-U");
265 if (mdtype != MD_VNODE && !newfs)
266 errx(1, "-P requires a vnode-backed disk");
269 if (detach && !autounit)
270 do_mdconfig_detach();
272 do_mdconfig_attach_au(mdconfig_arg, mdtype);
274 do_mdconfig_attach(mdconfig_arg, mdtype);
277 do_mount(mount_arg, mtpoint);
278 do_mtptsetup(mtpoint, &mi);
284 * Append the expansion of 'fmt' to the buffer pointed to by '*dstp';
285 * reallocate as required.
288 argappend(char **dstp, const char *fmt, ...)
297 if (vasprintf(&new, fmt,ap) == -1)
298 errx(1, "vasprintf");
302 if (asprintf(&new, "%s %s", old, new) == -1)
311 * If run-time debugging is enabled, print the expansion of 'fmt'.
312 * Otherwise, do nothing.
315 debugprintf(const char *fmt, ...)
321 fprintf(stderr, "DEBUG: ");
323 vfprintf(stderr, fmt, ap);
325 fprintf(stderr, "\n");
330 * Attach a memory disk with a known unit.
333 do_mdconfig_attach(const char *args, const enum md_types mdtype)
336 const char *ta; /* Type arg. */
351 rv = run(NULL, "%s -a %s%s -u %s%d", path_mdconfig, ta, args,
354 errx(1, "mdconfig (attach) exited with error code %d", rv);
358 * Attach a memory disk with an unknown unit; use autounit.
361 do_mdconfig_attach_au(const char *args, const enum md_types mdtype)
363 const char *ta; /* Type arg. */
364 char *linep, *linebuf; /* Line pointer, line buffer. */
365 int fd; /* Standard output of mdconfig invocation. */
385 rv = run(&fd, "%s -a %s%s", path_mdconfig, ta, args);
387 errx(1, "mdconfig (attach) exited with error code %d", rv);
389 /* Receive the unit number. */
390 if (norun) { /* Since we didn't run, we can't read. Fake it. */
394 sfd = fdopen(fd, "r");
397 linep = fgetln(sfd, &linelen);
398 if (linep == NULL && linelen < mdnamelen + 1)
399 errx(1, "unexpected output from mdconfig (attach)");
400 /* If the output format changes, we want to know about it. */
401 assert(strncmp(linep, mdname, mdnamelen) == 0);
402 linebuf = malloc(linelen - mdnamelen + 1);
403 assert(linebuf != NULL);
404 /* Can't use strlcpy because linep is not NULL-terminated. */
405 strncpy(linebuf, linep + mdnamelen, linelen);
406 linebuf[linelen] = '\0';
407 ul = strtoul(linebuf, &p, 10);
408 if (ul == ULONG_MAX || *p != '\n')
409 errx(1, "unexpected output from mdconfig (attach)");
417 * Detach a memory disk.
420 do_mdconfig_detach(void)
424 rv = run(NULL, "%s -d -u %s%d", path_mdconfig, mdname, unit);
425 if (rv && debug) /* This is allowed to fail. */
426 warnx("mdconfig (detach) exited with error code %d (ignored)",
431 * Mount the configured memory disk.
434 do_mount(const char *args, const char *mtpoint)
438 rv = run(NULL, "%s%s /dev/%s%d%s %s", _PATH_MOUNT, args,
439 mdname, unit, mdsuffix, mtpoint);
441 errx(1, "mount exited with error code %d", rv);
445 * Various configuration of the mountpoint. Mostly, enact 'mip'.
448 do_mtptsetup(const char *mtpoint, struct mtpt_info *mip)
452 if (!mip->mi_have_mode && !mip->mi_have_uid && !mip->mi_have_gid)
456 if (statfs(mtpoint, &sfs) == -1) {
457 warn("statfs: %s", mtpoint);
460 if ((sfs.f_flags & MNT_RDONLY) != 0) {
461 if (mip->mi_forced_pw) {
463 "Not changing mode/owner of %s since it is read-only",
467 "Not changing mode/owner of %s since it is read-only",
474 if (mip->mi_have_mode) {
475 debugprintf("changing mode of %s to %o.", mtpoint,
478 if (chmod(mtpoint, mip->mi_mode) == -1)
479 err(1, "chmod: %s", mtpoint);
482 * We have to do these separately because the user may have
483 * only specified one of them.
485 if (mip->mi_have_uid) {
486 debugprintf("changing owner (user) or %s to %u.", mtpoint,
489 if (chown(mtpoint, mip->mi_uid, -1) == -1)
490 err(1, "chown %s to %u (user)", mtpoint,
493 if (mip->mi_have_gid) {
494 debugprintf("changing owner (group) or %s to %u.", mtpoint,
497 if (chown(mtpoint, -1, mip->mi_gid) == -1)
498 err(1, "chown %s to %u (group)", mtpoint,
504 * Put a file system on the memory disk.
507 do_newfs(const char *args)
511 rv = run(NULL, "%s%s /dev/%s%d", _PATH_NEWFS, args, mdname, unit);
513 errx(1, "newfs exited with error code %d", rv);
517 * 'str' should be a user and group name similar to the last argument
518 * to chown(1); i.e., a user, followed by a colon, followed by a
519 * group. The user and group in 'str' may be either a [ug]id or a
520 * name. Upon return, the uid and gid fields in 'mip' will contain
521 * the uid and gid of the user and group name in 'str', respectively.
523 * In other words, this derives a user and group id from a string
524 * formatted like the last argument to chown(1).
526 * Notice: At this point we don't support only a username or only a
527 * group name. do_mtptsetup already does, so when this feature is
528 * desired, this is the only routine that needs to be changed.
531 extract_ugid(const char *str, struct mtpt_info *mip)
533 char *ug; /* Writable 'str'. */
534 char *user, *group; /* Result of extracton. */
543 mip->mi_have_uid = mip->mi_have_gid = false;
545 /* Extract the user and group from 'str'. Format above. */
549 user = strsep(&group, ":");
550 if (user == NULL || group == NULL || *user == '\0' || *group == '\0')
554 *uid = strtoul(user, &p, 10);
555 if (*uid == (uid_t)ULONG_MAX)
560 errx(1, "invalid user: %s", user);
563 mip->mi_have_uid = true;
566 *gid = strtoul(group, &p, 10);
567 if (*gid == (gid_t)ULONG_MAX)
570 gr = getgrnam(group);
572 errx(1, "invalid group: %s", group);
575 mip->mi_have_gid = true;
581 * Run a process with command name and arguments pointed to by the
582 * formatted string 'cmdline'. Since system(3) is not used, the first
583 * space-delimited token of 'cmdline' must be the full pathname of the
584 * program to run. The return value is the return code of the process
585 * spawned. If 'ofd' is non-NULL, it is set to the standard output of
586 * the program spawned (i.e., you can read from ofd and get the output
590 run(int *ofd, const char *cmdline, ...)
592 char **argv, **argvp; /* Result of splitting 'cmd'. */
594 char *cmd; /* Expansion of 'cmdline'. */
595 int pid, status; /* Child info. */
596 int pfd[2]; /* Pipe to the child. */
597 int nfd; /* Null (/dev/null) file descriptor. */
598 bool dup2dn; /* Dup /dev/null to stdout? */
604 va_start(ap, cmdline);
605 rv = vasprintf(&cmd, cmdline, ap);
610 /* Split up 'cmd' into 'argv' for use with execve. */
611 for (argc = 1, p = cmd; (p = strchr(p, ' ')) != NULL; p++)
612 argc++; /* 'argc' generation loop. */
613 argv = (char **)malloc(sizeof(*argv) * (argc + 1));
614 assert(argv != NULL);
615 for (p = cmd, argvp = argv; (*argvp = strsep(&p, " ")) != NULL;)
617 if (++argvp >= &argv[argc]) {
622 /* The argv array ends up NULL-terminated here. */
624 /* Make sure the above loop works as expected. */
627 * We can't, but should, use debugprintf here. First,
628 * it appends a trailing newline to the output, and
629 * second it prepends "DEBUG: " to the output. The
630 * former is a problem for this would-be first call,
631 * and the latter for the would-be call inside the
634 (void)fprintf(stderr, "DEBUG: running:");
635 /* Should be equivalent to 'cmd' (before strsep, of course). */
636 for (i = 0; argv[i] != NULL; i++)
637 (void)fprintf(stderr, " %s", argv[i]);
638 (void)fprintf(stderr, "\n");
641 /* Create a pipe if necessary and fork the helper program. */
643 if (pipe(&pfd[0]) == -1)
651 /* XXX can we call err() in here? */
655 if (dup2(pfd[1], STDOUT_FILENO) < 0)
658 nfd = open(_PATH_DEVNULL, O_RDWR);
660 err(1, "open: %s", _PATH_DEVNULL);
661 if (dup2(nfd, STDIN_FILENO) < 0)
664 if (dup2(nfd, STDOUT_FILENO) < 0)
666 if (dup2(nfd, STDERR_FILENO) < 0)
670 (void)execv(argv[0], argv);
671 warn("exec: %s", argv[0]);
679 while (waitpid(pid, &status, 0) != pid)
681 return (WEXITSTATUS(status));
689 "usage: %s [-DLlMNnPStUX] [-a maxcontig] [-b block-size]\n"
690 "\t[-c blocks-per-cylinder-group][-d max-extent-size] [-E path-mdconfig]\n"
691 "\t[-e maxbpg] [-F file] [-f frag-size] [-i bytes] [-m percent-free]\n"
692 "\t[-O optimization] [-o mount-options]\n"
693 "\t[-p permissions] [-s size] [-v version] [-w user:group]\n"
694 "\tmd-device mount-point\n", getprogname());