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>
53 typedef enum { false, true } bool;
64 static bool debug; /* Emit debugging information? */
65 static bool loudsubs; /* Suppress output from helper programs? */
66 static bool norun; /* Actually run the helper programs? */
67 static int unit; /* The unit we're working with. */
68 static const char *mdname; /* Name of memory disk device (e.g., "md"). */
69 static size_t mdnamelen; /* Length of mdname. */
70 static const char *path_mdconfig =_PATH_MDCONFIG;
72 static void argappend(char **, const char *, ...) __printflike(2, 3);
73 static void debugprintf(const char *, ...) __printflike(1, 2);
74 static void do_mdconfig_attach(const char *, const enum md_types);
75 static void do_mdconfig_attach_au(const char *, const enum md_types);
76 static void do_mdconfig_detach(void);
77 static void do_mount(const char *, const char *);
78 static void do_mtptsetup(const char *, struct mtpt_info *);
79 static void do_newfs(const char *);
80 static void extract_ugid(const char *, struct mtpt_info *);
81 static int run(int *, const char *, ...) __printflike(2, 3);
82 static void usage(void);
85 main(int argc, char **argv)
87 struct mtpt_info mi; /* Mountpoint info. */
88 char *mdconfig_arg, *newfs_arg, /* Args to helper programs. */
90 enum md_types mdtype; /* The type of our memory disk. */
92 bool detach, softdep, autounit, newfs;
93 char *mtpoint, *unitstr;
99 /* Misc. initialization. */
100 (void)memset(&mi, '\0', sizeof(mi));
108 mdnamelen = strlen(mdname);
110 * Can't set these to NULL. They may be passed to the
111 * respective programs without modification. I.e., we may not
112 * receive any command-line options which will caused them to
115 mdconfig_arg = strdup("");
116 newfs_arg = strdup("");
117 mount_arg = strdup("");
119 /* If we were started as mount_mfs or mfs, imply -C. */
120 if (strcmp(getprogname(), "mount_mfs") == 0 ||
121 strcmp(getprogname(), "mfs") == 0) {
122 /* Make compatibility assumptions. */
124 mi.mi_have_mode = true;
127 while ((ch = getopt(argc, argv,
128 "a:b:Cc:Dd:E:e:F:f:hi:LlMm:Nn:O:o:Pp:Ss:t:Uv:w:X")) != -1)
131 argappend(&newfs_arg, "-a %s", optarg);
134 argappend(&newfs_arg, "-b %s", optarg);
137 /* Ignored for compatibility. */
140 argappend(&newfs_arg, "-c %s", optarg);
146 argappend(&newfs_arg, "-d %s", optarg);
149 path_mdconfig = optarg;
152 argappend(&newfs_arg, "-e %s", optarg);
159 argappend(&mdconfig_arg, "-f %s", optarg);
162 argappend(&newfs_arg, "-f %s", optarg);
168 argappend(&newfs_arg, "-i %s", optarg);
174 argappend(&newfs_arg, "-l");
183 argappend(&newfs_arg, "-m %s", optarg);
189 argappend(&newfs_arg, "-n %s", optarg);
192 argappend(&newfs_arg, "-o %s", optarg);
195 argappend(&mount_arg, "-o %s", optarg);
201 if ((set = setmode(optarg)) == NULL)
203 mi.mi_mode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO);
204 mi.mi_have_mode = true;
211 argappend(&mdconfig_arg, "-s %s", optarg);
217 argappend(&newfs_arg, "-O %s", optarg);
220 extract_ugid(optarg, &mi);
233 /* Derive 'unit' (global). */
235 if (strncmp(unitstr, "/dev/", 5) == 0)
237 if (strncmp(unitstr, mdname, mdnamelen) == 0)
238 unitstr += mdnamelen;
239 if (*unitstr == '\0') {
243 ul = strtoul(unitstr, &p, 10);
244 if (ul == ULONG_MAX || *p != '\0')
245 errx(1, "bad device unit: %s", unitstr);
253 argappend(&newfs_arg, "-U");
254 if (mdtype != MD_VNODE && !newfs)
255 errx(1, "-P requires a vnode-backed disk");
258 if (detach && !autounit)
259 do_mdconfig_detach();
261 do_mdconfig_attach_au(mdconfig_arg, mdtype);
263 do_mdconfig_attach(mdconfig_arg, mdtype);
266 do_mount(mount_arg, mtpoint);
267 do_mtptsetup(mtpoint, &mi);
273 * Append the expansion of 'fmt' to the buffer pointed to by '*dstp';
274 * reallocate as required.
277 argappend(char **dstp, const char *fmt, ...)
286 if (vasprintf(&new, fmt,ap) == -1)
287 errx(1, "vasprintf");
291 if (asprintf(&new, "%s %s", old, new) == -1)
300 * If run-time debugging is enabled, print the expansion of 'fmt'.
301 * Otherwise, do nothing.
304 debugprintf(const char *fmt, ...)
310 fprintf(stderr, "DEBUG: ");
312 vfprintf(stderr, fmt, ap);
314 fprintf(stderr, "\n");
319 * Attach a memory disk with a known unit.
322 do_mdconfig_attach(const char *args, const enum md_types mdtype)
325 const char *ta; /* Type arg. */
340 rv = run(NULL, "%s -a %s%s -u %s%d", path_mdconfig, ta, args,
343 errx(1, "mdconfig (attach) exited with error code %d", rv);
347 * Attach a memory disk with an unknown unit; use autounit.
350 do_mdconfig_attach_au(const char *args, const enum md_types mdtype)
352 const char *ta; /* Type arg. */
353 char *linep, *linebuf; /* Line pointer, line buffer. */
354 int fd; /* Standard output of mdconfig invocation. */
374 rv = run(&fd, "%s -a %s%s", path_mdconfig, ta, args);
376 errx(1, "mdconfig (attach) exited with error code %d", rv);
378 /* Receive the unit number. */
379 if (norun) { /* Since we didn't run, we can't read. Fake it. */
383 sfd = fdopen(fd, "r");
386 linep = fgetln(sfd, &linelen);
387 if (linep == NULL && linelen < mdnamelen + 1)
388 errx(1, "unexpected output from mdconfig (attach)");
389 /* If the output format changes, we want to know about it. */
390 assert(strncmp(linep, mdname, mdnamelen) == 0);
391 linebuf = malloc(linelen - mdnamelen + 1);
392 assert(linebuf != NULL);
393 /* Can't use strlcpy because linep is not NULL-terminated. */
394 strncpy(linebuf, linep + mdnamelen, linelen);
395 linebuf[linelen] = '\0';
396 ul = strtoul(linebuf, &p, 10);
397 if (ul == ULONG_MAX || *p != '\n')
398 errx(1, "unexpected output from mdconfig (attach)");
406 * Detach a memory disk.
409 do_mdconfig_detach(void)
413 rv = run(NULL, "%s -d -u %s%d", path_mdconfig, mdname, unit);
414 if (rv && debug) /* This is allowed to fail. */
415 warnx("mdconfig (detach) exited with error code %d (ignored)",
420 * Mount the configured memory disk.
423 do_mount(const char *args, const char *mtpoint)
427 rv = run(NULL, "%s%s /dev/%s%d %s", _PATH_MOUNT, args,
428 mdname, unit, mtpoint);
430 errx(1, "mount exited with error code %d", rv);
434 * Various configuration of the mountpoint. Mostly, enact 'mip'.
437 do_mtptsetup(const char *mtpoint, struct mtpt_info *mip)
440 if (mip->mi_have_mode) {
441 debugprintf("changing mode of %s to %o.", mtpoint,
444 if (chmod(mtpoint, mip->mi_mode) == -1)
445 err(1, "chmod: %s", mtpoint);
448 * We have to do these separately because the user may have
449 * only specified one of them.
451 if (mip->mi_have_uid) {
452 debugprintf("changing owner (user) or %s to %u.", mtpoint,
455 if (chown(mtpoint, mip->mi_uid, -1) == -1)
456 err(1, "chown %s to %u (user)", mtpoint,
459 if (mip->mi_have_gid) {
460 debugprintf("changing owner (group) or %s to %u.", mtpoint,
463 if (chown(mtpoint, -1, mip->mi_gid) == -1)
464 err(1, "chown %s to %u (group)", mtpoint,
470 * Put a file system on the memory disk.
473 do_newfs(const char *args)
477 rv = run(NULL, "%s%s /dev/%s%d", _PATH_NEWFS, args, mdname, unit);
479 errx(1, "newfs exited with error code %d", rv);
483 * 'str' should be a user and group name similar to the last argument
484 * to chown(1); i.e., a user, followed by a colon, followed by a
485 * group. The user and group in 'str' may be either a [ug]id or a
486 * name. Upon return, the uid and gid fields in 'mip' will contain
487 * the uid and gid of the user and group name in 'str', respectively.
489 * In other words, this derives a user and group id from a string
490 * formatted like the last argument to chown(1).
492 * Notice: At this point we don't support only a username or only a
493 * group name. do_mtptsetup already does, so when this feature is
494 * desired, this is the only routine that needs to be changed.
497 extract_ugid(const char *str, struct mtpt_info *mip)
499 char *ug; /* Writable 'str'. */
500 char *user, *group; /* Result of extracton. */
509 mip->mi_have_uid = mip->mi_have_gid = false;
511 /* Extract the user and group from 'str'. Format above. */
515 user = strsep(&group, ":");
516 if (user == NULL || group == NULL || *user == '\0' || *group == '\0')
520 *uid = strtoul(user, &p, 10);
521 if (*uid == (uid_t)ULONG_MAX)
526 errx(1, "invalid user: %s", user);
529 mip->mi_have_uid = true;
532 *gid = strtoul(group, &p, 10);
533 if (*gid == (gid_t)ULONG_MAX)
536 gr = getgrnam(group);
538 errx(1, "invalid group: %s", group);
541 mip->mi_have_gid = true;
547 * Run a process with command name and arguments pointed to by the
548 * formatted string 'cmdline'. Since system(3) is not used, the first
549 * space-delimited token of 'cmdline' must be the full pathname of the
550 * program to run. The return value is the return code of the process
551 * spawned. If 'ofd' is non-NULL, it is set to the standard output of
552 * the program spawned (i.e., you can read from ofd and get the output
556 run(int *ofd, const char *cmdline, ...)
558 char **argv, **argvp; /* Result of splitting 'cmd'. */
560 char *cmd; /* Expansion of 'cmdline'. */
561 int pid, status; /* Child info. */
562 int pfd[2]; /* Pipe to the child. */
563 int nfd; /* Null (/dev/null) file descriptor. */
564 bool dup2dn; /* Dup /dev/null to stdout? */
570 va_start(ap, cmdline);
571 rv = vasprintf(&cmd, cmdline, ap);
576 /* Split up 'cmd' into 'argv' for use with execve. */
577 for (argc = 1, p = cmd; (p = strchr(p, ' ')) != NULL; p++)
578 argc++; /* 'argc' generation loop. */
579 argv = (char **)malloc(sizeof(*argv) * (argc + 1));
580 assert(argv != NULL);
581 for (p = cmd, argvp = argv; (*argvp = strsep(&p, " ")) != NULL;)
583 if (++argvp >= &argv[argc]) {
589 /* Make sure the above loop works as expected. */
592 * We can't, but should, use debugprintf here. First,
593 * it appends a trailing newline to the output, and
594 * second it prepends "DEBUG: " to the output. The
595 * former is a problem for this would-be first call,
596 * and the latter for the would-be call inside the
599 (void)fprintf(stderr, "DEBUG: running:");
600 /* Should be equivilent to 'cmd' (before strsep, of course). */
601 for (i = 0; argv[i] != NULL; i++)
602 (void)fprintf(stderr, " %s", argv[i]);
603 (void)fprintf(stderr, "\n");
606 /* Create a pipe if necessary and fork the helper program. */
608 if (pipe(&pfd[0]) == -1)
616 /* XXX can we call err() in here? */
620 if (dup2(pfd[1], STDOUT_FILENO) < 0)
623 nfd = open(_PATH_DEVNULL, O_RDWR);
625 err(1, "open: %s", _PATH_DEVNULL);
626 if (dup2(nfd, STDIN_FILENO) < 0)
629 if (dup2(nfd, STDOUT_FILENO) < 0)
631 if (dup2(nfd, STDERR_FILENO) < 0)
635 (void)execv(argv[0], argv);
636 warn("exec: %s", argv[0]);
644 while (waitpid(pid, &status, 0) != pid)
646 return (WEXITSTATUS(status));
654 "usage: %s [-DLlMNPSUX] [-a maxcontig] [-b block-size] [-c cylinders]\n"
655 "\t[-d rotdelay] [-E path-mdconfig] [-e maxbpg] [-F file] [-f frag-size]\n"
656 "\t[-i bytes] [-m percent-free] [-n rotational-positions] [-O optimization]\n"
657 "\t[-o mount-options] [-p permissions] [-s size] [-v version]\n"
658 "\t[-w user:group] md-device mount-point\n", getprogname());