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), disklabel(8),
29 * newfs(8), and mount(8) that mimics the command line option set of
30 * the deprecated mount_mfs(8).
34 static const char rcsid[] =
38 #include <sys/param.h>
39 #include <sys/mdioctl.h>
55 #include "pathnames.h"
57 typedef enum { false, true } bool;
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. */
76 static void argappend(char **, const char *, ...) __printflike(2, 3);
77 static void debugprintf(const char *, ...) __printflike(1, 2);
78 static void do_disklabel(void);
79 static void do_mdconfig_attach(const char *, const enum md_types);
80 static void do_mdconfig_attach_au(const char *, const enum md_types);
81 static void do_mdconfig_detach(void);
82 static void do_mount(const char *, const char *);
83 static void do_mtptsetup(const char *, struct mtpt_info *);
84 static void do_newfs(const char *);
85 static void extract_ugid(const char *, struct mtpt_info *);
86 static int run(int *, const char *, ...) __printflike(2, 3);
87 static void usage(void);
90 main(int argc, char **argv)
92 struct mtpt_info mi; /* Mountpoint info. */
93 char *mdconfig_arg, *newfs_arg, /* Args to helper programs. */
95 enum md_types mdtype; /* The type of our memory disk. */
97 bool detach, softdep, autounit;
98 char *mtpoint, *unitstr;
101 /* Misc. initialization. */
102 (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)
124 while ((ch = getopt(argc, argv,
125 "a:b:Cc:Dd:e:F:f:hi:LMm:Nn:O:o:p:Ss:t:Uw:X")) != -1)
128 argappend(&newfs_arg, "-a %s", optarg);
131 argappend(&newfs_arg, "-b %s", optarg);
139 argappend(&newfs_arg, "-c %s", optarg);
147 argappend(&newfs_arg, "-d %s", optarg);
150 argappend(&newfs_arg, "-e %s", optarg);
157 argappend(&mdconfig_arg, "-f %s", optarg);
160 argappend(&newfs_arg, "-f %s", optarg);
166 argappend(&newfs_arg, "-i %s", optarg);
180 argappend(&newfs_arg, "-m %s", optarg);
188 argappend(&newfs_arg, "-n %s", optarg);
191 argappend(&newfs_arg, "-o %s", optarg);
194 argappend(&mount_arg, "-o %s", optarg);
199 if (*optarg >= '0' && *optarg <= '7')
200 mi.mi_mode = strtol(optarg, NULL, 8);
201 if ((mi.mi_mode & ~07777) != 0)
203 mi.mi_have_mode = true;
211 argappend(&mdconfig_arg, "-s %s", optarg);
219 extract_ugid(optarg, &mi);
234 /* Make compatibility assumptions. */
237 mi.mi_have_mode = true;
240 /* Derive 'unit' (global). */
242 if (strncmp(unitstr, "/dev/", 5) == 0)
244 if (strncmp(unitstr, mdname, mdnamelen) == 0)
245 unitstr += mdnamelen;
246 if (*unitstr == '\0') {
250 unit = strtoul(unitstr, &p, 10);
251 if ((unsigned)unit == ULONG_MAX || *p != '\0')
252 errx(1, "bad device unit: %s", unitstr);
259 argappend(&newfs_arg, "-U");
262 if (detach && !autounit)
263 do_mdconfig_detach();
265 do_mdconfig_attach_au(mdconfig_arg, mdtype);
267 do_mdconfig_attach(mdconfig_arg, mdtype);
270 do_mount(mount_arg, mtpoint);
271 do_mtptsetup(mtpoint, &mi);
277 * Append the expansion of 'fmt' to the buffer pointed to by '*dstp';
278 * reallocate as required.
281 argappend(char **dstp, const char *fmt, ...)
290 if (vasprintf(&new, fmt,ap) == -1)
291 errx(1, "vasprintf");
295 if (asprintf(&new, "%s %s", old, new) == -1)
304 * If run-time debugging is enabled, print the expansion of 'fmt'.
305 * Otherwise, do nothing.
308 debugprintf(const char *fmt, ...)
314 fprintf(stderr, "DEBUG: ");
316 vfprintf(stderr, fmt, ap);
318 fprintf(stderr, "\n");
323 * Label the memory disk.
330 rv = run(NULL, "%s -r -w %s%d auto", PATH_DISKLABEL, mdname, unit);
332 errx(1, "disklabel exited with error code %d", rv);
336 * Attach a memory disk with a known unit.
339 do_mdconfig_attach(const char *args, const enum md_types mdtype)
342 const char *ta; /* Type arg. */
357 rv = run(NULL, "%s -a %s%s -u %s%d", PATH_MDCONFIG, ta, args,
360 errx(1, "mdconfig (attach) exited with error code %d", rv);
364 * Attach a memory disk with an unknown unit; use autounit.
367 do_mdconfig_attach_au(const char *args, const enum md_types mdtype)
369 const char *ta; /* Type arg. */
370 char *linep, *linebuf; /* Line pointer, line buffer. */
371 int fd; /* Standard output of mdconfig invocation. */
390 rv = run(&fd, "%s -a %s%s", PATH_MDCONFIG, ta, args);
392 errx(1, "mdconfig (attach) exited with error code %d", rv);
394 /* Receive the unit number. */
395 if (norun) { /* Since we didn't run, we can't read. Fake it. */
399 sfd = fdopen(fd, "r");
402 linep = fgetln(sfd, &linelen);
403 if (linep == NULL && linelen < mdnamelen + 1)
404 errx(1, "unexpected output from mdconfig (attach)");
405 /* If the output format changes, we want to know about it. */
406 assert(strncmp(linep, mdname, mdnamelen) == 0);
407 linebuf = malloc(linelen - mdnamelen + 1);
408 assert(linebuf != NULL);
409 /* Can't use strlcpy because linep is not NULL-terminated. */
410 strncpy(linebuf, linep + mdnamelen, linelen);
411 linebuf[linelen] = '\0';
412 unit = strtoul(linebuf, &p, 10);
413 if ((unsigned)unit == ULONG_MAX || *p != '\n')
414 errx(1, "unexpected output from mdconfig (attach)");
421 * Detach a memory disk.
424 do_mdconfig_detach(void)
428 rv = run(NULL, "%s -d -u %s%d", PATH_MDCONFIG, mdname, unit);
429 if (rv && debug) /* This is allowed to fail. */
430 warnx("mdconfig (detach) exited with error code %d (ignored)",
435 * Mount the configured memory disk.
438 do_mount(const char *args, const char *mtpoint)
442 rv = run(NULL, "%s%s /dev/%s%dc %s", PATH_MOUNT, args,
443 mdname, unit, mtpoint);
445 errx(1, "mount exited with error code %d", rv);
449 * Various configuration of the mountpoint. Mostly, enact 'mip'.
452 do_mtptsetup(const char *mtpoint, struct mtpt_info *mip)
455 if (mip->mi_have_mode) {
456 debugprintf("changing mode of %s to %o.", mtpoint,
459 if (chmod(mtpoint, mip->mi_mode) == -1)
460 err(1, "chmod: %s", mtpoint);
463 * We have to do these separately because the user may have
464 * only specified one of them.
466 if (mip->mi_have_uid) {
467 debugprintf("changing owner (user) or %s to %u.", mtpoint,
470 if (chown(mtpoint, mip->mi_uid, -1) == -1)
471 err(1, "chown %s to %u (user)", mtpoint,
474 if (mip->mi_have_gid) {
475 debugprintf("changing owner (group) or %s to %u.", mtpoint,
478 if (chown(mtpoint, -1, mip->mi_gid) == -1)
479 err(1, "chown %s to %u (group)", mtpoint,
485 * Put a filesystem on the memory disk.
488 do_newfs(const char *args)
492 rv = run(NULL, "%s%s /dev/%s%dc", PATH_NEWFS, args, mdname, unit);
494 errx(1, "newfs exited with error code %d", rv);
498 * 'str' should be a user and group name similar to the last argument
499 * to chown(1); i.e., a user, followed by a colon, followed by a
500 * group. The user and group in 'str' may be either a [ug]id or a
501 * name. Upon return, the uid and gid fields in 'mip' will contain
502 * the uid and gid of the user and group name in 'str', respectively.
504 * In other words, this derives a user and group id from a string
505 * formatted like the last argument to chown(1).
508 extract_ugid(const char *str, struct mtpt_info *mip)
510 char *ug; /* Writable 'str'. */
511 char *user, *group; /* Result of extracton. */
520 mip->mi_have_uid = mip->mi_have_gid = false;
522 /* Extract the user and group from 'str'. Format above. */
526 user = strsep(&group, ":");
527 if (user == NULL || group == NULL || *user == '\0' || *group == '\0')
531 *uid = strtoul(user, &p, 10);
532 if ((unsigned)*uid == ULONG_MAX)
537 errx(1, "invalid user: %s", user);
539 mip->mi_have_uid = true;
543 *gid = strtoul(group, &p, 10);
544 if ((unsigned)*gid == ULONG_MAX)
547 gr = getgrnam(group);
549 errx(1, "invalid group: %s", group);
551 mip->mi_have_gid = true;
556 * At this point we don't support only a username or only a
557 * group name. do_mtptsetup already does, so when this
558 * feature is desired, this is the only routine that needs to
561 assert(mip->mi_have_uid);
562 assert(mip->mi_have_gid);
566 * Run a process with command name and arguments pointed to by the
567 * formatted string 'cmdline'. Since system(3) is not used, the first
568 * space-delimited token of 'cmdline' must be the full pathname of the
569 * program to run. The return value is the return code of the process
570 * spawned. If 'ofd' is non-NULL, it is set to the standard output of
571 * the program spawned (i.e., you can read from ofd and get the output
575 run(int *ofd, const char *cmdline, ...)
577 char **argv, **argvp; /* Result of splitting 'cmd'. */
579 char *cmd; /* Expansion of 'cmdline'. */
580 int pid, status; /* Child info. */
581 int pfd[2]; /* Pipe to the child. */
582 int nfd; /* Null (/dev/null) file descriptor. */
583 bool dup2dn; /* Dup /dev/null to stdout? */
589 va_start(ap, cmdline);
590 rv = vasprintf(&cmd, cmdline, ap);
595 /* Split up 'cmd' into 'argv' for use with execve. */
596 for (argc = 1, p = cmd; (p = strchr(p, ' ')) != NULL; p++)
597 argc++; /* 'argc' generation loop. */
598 argv = (char **)malloc(sizeof(*argv) * (argc + 1));
599 assert(argv != NULL);
600 for (p = cmd, argvp = argv; (*argvp = strsep(&p, " ")) != NULL;)
602 if (++argvp >= &argv[argc]) {
608 /* Make sure the above loop works as expected. */
611 * We can't, but should, use debugprintf here. First,
612 * it appends a trailing newline to the output, and
613 * second it prepends "DEBUG: " to the output. The
614 * former is a problem for this would-be first call,
615 * and the latter for the would-be call inside the
618 (void)fprintf(stderr, "DEBUG: running:");
619 /* Should be equivilent to 'cmd' (before strsep, of course). */
620 for (i = 0; argv[i] != NULL; i++)
621 (void)fprintf(stderr, " %s", argv[i]);
622 (void)fprintf(stderr, "\n");
625 /* Create a pipe if necessary and fork the helper program. */
627 if (pipe(&pfd[0]) == -1)
635 /* XXX can we call err() in here? */
639 if (dup2(pfd[1], STDOUT_FILENO) < 0)
642 nfd = open(_PATH_DEVNULL, O_RDWR);
644 err(1, "open: %s", _PATH_DEVNULL);
645 if (dup2(nfd, STDIN_FILENO) < 0)
648 if (dup2(nfd, STDOUT_FILENO) < 0)
650 if (dup2(nfd, STDERR_FILENO) < 0)
654 (void)execv(argv[0], argv);
655 warn("exec: %s", argv[0]);
663 while (waitpid(pid, &status, 0) != pid)
665 return (WEXITSTATUS(status));
674 name = getprogname();
679 "usage: %s [-DLMNSUX] [-a maxcontig [-b block-size] [-c cylinders]\n"
680 "\t[-d rotdelay] [-e maxbpg] [-F file] [-f frag-size] [-i bytes]\n"
681 "\t[-m percent-free] [-n rotational-positions] [-O optimization]\n"
682 "\t[-o mount-options] [-p permissions] [-s size] [-w user:group]\n"
683 "\tmd-device mount-point\n", name);
685 "usage: %s -C [-NU] [-a maxcontig] [-b block-size] [-c cylinders]\n"
686 "\t[-d rotdelay] [-e maxbpg] [-F file] [-f frag-size] [-i bytes]\n"
687 "\t[-m percent-free] [-n rotational-positions] [-O optimization]\n"
688 "\t[-o mount-options] [-s size] md-device mount-point\n", name);