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 compat; /* Full compatibility with mount_mfs? */
65 static bool debug; /* Emit debugging information? */
66 static bool loudsubs; /* Suppress output from helper programs? */
67 static bool norun; /* Actually run the helper programs? */
68 static int unit; /* The unit we're working with. */
69 static const char *mdname; /* Name of memory disk device (e.g., "md"). */
70 static size_t mdnamelen; /* Length of mdname. */
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;
93 char *mtpoint, *unitstr;
99 /* Misc. initialization. */
100 (void)memset(&mi, '\0', sizeof(mi));
107 mdnamelen = strlen(mdname);
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
114 mdconfig_arg = strdup("");
115 newfs_arg = strdup("");
116 mount_arg = strdup("");
118 /* If we were started as mount_mfs or mfs, imply -C. */
119 if (strcmp(getprogname(), "mount_mfs") == 0 ||
120 strcmp(getprogname(), "mfs") == 0)
123 while ((ch = getopt(argc, argv,
124 "a:b:Cc:Dd:e:F:f:hi:LlMm:Nn:O:o:p:Ss:t:Uv:w:X")) != -1)
127 argappend(&newfs_arg, "-a %s", optarg);
130 argappend(&newfs_arg, "-b %s", optarg);
138 argappend(&newfs_arg, "-c %s", optarg);
146 argappend(&newfs_arg, "-d %s", optarg);
149 argappend(&newfs_arg, "-e %s", optarg);
156 argappend(&mdconfig_arg, "-f %s", optarg);
159 argappend(&newfs_arg, "-f %s", optarg);
165 argappend(&newfs_arg, "-i %s", optarg);
173 argappend(&newfs_arg, "-l");
182 argappend(&newfs_arg, "-m %s", optarg);
190 argappend(&newfs_arg, "-n %s", optarg);
193 argappend(&newfs_arg, "-o %s", optarg);
196 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;
213 argappend(&mdconfig_arg, "-s %s", optarg);
219 argappend(&newfs_arg, "-O %s", optarg);
224 extract_ugid(optarg, &mi);
239 /* Make compatibility assumptions. */
242 mi.mi_have_mode = true;
245 /* Derive 'unit' (global). */
247 if (strncmp(unitstr, "/dev/", 5) == 0)
249 if (strncmp(unitstr, mdname, mdnamelen) == 0)
250 unitstr += mdnamelen;
251 if (*unitstr == '\0') {
255 ul = strtoul(unitstr, &p, 10);
256 if (ul == ULONG_MAX || *p != '\0')
257 errx(1, "bad device unit: %s", unitstr);
265 argappend(&newfs_arg, "-U");
268 if (detach && !autounit)
269 do_mdconfig_detach();
271 do_mdconfig_attach_au(mdconfig_arg, mdtype);
273 do_mdconfig_attach(mdconfig_arg, mdtype);
275 do_mount(mount_arg, mtpoint);
276 do_mtptsetup(mtpoint, &mi);
282 * Append the expansion of 'fmt' to the buffer pointed to by '*dstp';
283 * reallocate as required.
286 argappend(char **dstp, const char *fmt, ...)
295 if (vasprintf(&new, fmt,ap) == -1)
296 errx(1, "vasprintf");
300 if (asprintf(&new, "%s %s", old, new) == -1)
309 * If run-time debugging is enabled, print the expansion of 'fmt'.
310 * Otherwise, do nothing.
313 debugprintf(const char *fmt, ...)
319 fprintf(stderr, "DEBUG: ");
321 vfprintf(stderr, fmt, ap);
323 fprintf(stderr, "\n");
328 * Attach a memory disk with a known unit.
331 do_mdconfig_attach(const char *args, const enum md_types mdtype)
334 const char *ta; /* Type arg. */
349 rv = run(NULL, "%s -a %s%s -u %s%d", _PATH_MDCONFIG, ta, args,
352 errx(1, "mdconfig (attach) exited with error code %d", rv);
356 * Attach a memory disk with an unknown unit; use autounit.
359 do_mdconfig_attach_au(const char *args, const enum md_types mdtype)
361 const char *ta; /* Type arg. */
362 char *linep, *linebuf; /* Line pointer, line buffer. */
363 int fd; /* Standard output of mdconfig invocation. */
383 rv = run(&fd, "%s -a %s%s", _PATH_MDCONFIG, ta, args);
385 errx(1, "mdconfig (attach) exited with error code %d", rv);
387 /* Receive the unit number. */
388 if (norun) { /* Since we didn't run, we can't read. Fake it. */
392 sfd = fdopen(fd, "r");
395 linep = fgetln(sfd, &linelen);
396 if (linep == NULL && linelen < mdnamelen + 1)
397 errx(1, "unexpected output from mdconfig (attach)");
398 /* If the output format changes, we want to know about it. */
399 assert(strncmp(linep, mdname, mdnamelen) == 0);
400 linebuf = malloc(linelen - mdnamelen + 1);
401 assert(linebuf != NULL);
402 /* Can't use strlcpy because linep is not NULL-terminated. */
403 strncpy(linebuf, linep + mdnamelen, linelen);
404 linebuf[linelen] = '\0';
405 ul = strtoul(linebuf, &p, 10);
406 if (ul == ULONG_MAX || *p != '\n')
407 errx(1, "unexpected output from mdconfig (attach)");
415 * Detach a memory disk.
418 do_mdconfig_detach(void)
422 rv = run(NULL, "%s -d -u %s%d", _PATH_MDCONFIG, mdname, unit);
423 if (rv && debug) /* This is allowed to fail. */
424 warnx("mdconfig (detach) exited with error code %d (ignored)",
429 * Mount the configured memory disk.
432 do_mount(const char *args, const char *mtpoint)
436 rv = run(NULL, "%s%s /dev/%s%d %s", _PATH_MOUNT, args,
437 mdname, unit, mtpoint);
439 errx(1, "mount exited with error code %d", rv);
443 * Various configuration of the mountpoint. Mostly, enact 'mip'.
446 do_mtptsetup(const char *mtpoint, struct mtpt_info *mip)
449 if (mip->mi_have_mode) {
450 debugprintf("changing mode of %s to %o.", mtpoint,
453 if (chmod(mtpoint, mip->mi_mode) == -1)
454 err(1, "chmod: %s", mtpoint);
457 * We have to do these separately because the user may have
458 * only specified one of them.
460 if (mip->mi_have_uid) {
461 debugprintf("changing owner (user) or %s to %u.", mtpoint,
464 if (chown(mtpoint, mip->mi_uid, -1) == -1)
465 err(1, "chown %s to %u (user)", mtpoint,
468 if (mip->mi_have_gid) {
469 debugprintf("changing owner (group) or %s to %u.", mtpoint,
472 if (chown(mtpoint, -1, mip->mi_gid) == -1)
473 err(1, "chown %s to %u (group)", mtpoint,
479 * Put a file system on the memory disk.
482 do_newfs(const char *args)
486 rv = run(NULL, "%s%s /dev/%s%d", _PATH_NEWFS, args, mdname, unit);
488 errx(1, "newfs exited with error code %d", rv);
492 * 'str' should be a user and group name similar to the last argument
493 * to chown(1); i.e., a user, followed by a colon, followed by a
494 * group. The user and group in 'str' may be either a [ug]id or a
495 * name. Upon return, the uid and gid fields in 'mip' will contain
496 * the uid and gid of the user and group name in 'str', respectively.
498 * In other words, this derives a user and group id from a string
499 * formatted like the last argument to chown(1).
502 extract_ugid(const char *str, struct mtpt_info *mip)
504 char *ug; /* Writable 'str'. */
505 char *user, *group; /* Result of extracton. */
514 mip->mi_have_uid = mip->mi_have_gid = false;
516 /* Extract the user and group from 'str'. Format above. */
520 user = strsep(&group, ":");
521 if (user == NULL || group == NULL || *user == '\0' || *group == '\0')
525 *uid = strtoul(user, &p, 10);
526 if (*uid == (uid_t)ULONG_MAX)
531 errx(1, "invalid user: %s", user);
533 mip->mi_have_uid = true;
537 *gid = strtoul(group, &p, 10);
538 if (*gid == (gid_t)ULONG_MAX)
541 gr = getgrnam(group);
543 errx(1, "invalid group: %s", group);
545 mip->mi_have_gid = true;
550 * At this point we don't support only a username or only a
551 * group name. do_mtptsetup already does, so when this
552 * feature is desired, this is the only routine that needs to
555 assert(mip->mi_have_uid);
556 assert(mip->mi_have_gid);
560 * Run a process with command name and arguments pointed to by the
561 * formatted string 'cmdline'. Since system(3) is not used, the first
562 * space-delimited token of 'cmdline' must be the full pathname of the
563 * program to run. The return value is the return code of the process
564 * spawned. If 'ofd' is non-NULL, it is set to the standard output of
565 * the program spawned (i.e., you can read from ofd and get the output
569 run(int *ofd, const char *cmdline, ...)
571 char **argv, **argvp; /* Result of splitting 'cmd'. */
573 char *cmd; /* Expansion of 'cmdline'. */
574 int pid, status; /* Child info. */
575 int pfd[2]; /* Pipe to the child. */
576 int nfd; /* Null (/dev/null) file descriptor. */
577 bool dup2dn; /* Dup /dev/null to stdout? */
583 va_start(ap, cmdline);
584 rv = vasprintf(&cmd, cmdline, ap);
589 /* Split up 'cmd' into 'argv' for use with execve. */
590 for (argc = 1, p = cmd; (p = strchr(p, ' ')) != NULL; p++)
591 argc++; /* 'argc' generation loop. */
592 argv = (char **)malloc(sizeof(*argv) * (argc + 1));
593 assert(argv != NULL);
594 for (p = cmd, argvp = argv; (*argvp = strsep(&p, " ")) != NULL;)
596 if (++argvp >= &argv[argc]) {
602 /* Make sure the above loop works as expected. */
605 * We can't, but should, use debugprintf here. First,
606 * it appends a trailing newline to the output, and
607 * second it prepends "DEBUG: " to the output. The
608 * former is a problem for this would-be first call,
609 * and the latter for the would-be call inside the
612 (void)fprintf(stderr, "DEBUG: running:");
613 /* Should be equivilent to 'cmd' (before strsep, of course). */
614 for (i = 0; argv[i] != NULL; i++)
615 (void)fprintf(stderr, " %s", argv[i]);
616 (void)fprintf(stderr, "\n");
619 /* Create a pipe if necessary and fork the helper program. */
621 if (pipe(&pfd[0]) == -1)
629 /* XXX can we call err() in here? */
633 if (dup2(pfd[1], STDOUT_FILENO) < 0)
636 nfd = open(_PATH_DEVNULL, O_RDWR);
638 err(1, "open: %s", _PATH_DEVNULL);
639 if (dup2(nfd, STDIN_FILENO) < 0)
642 if (dup2(nfd, STDOUT_FILENO) < 0)
644 if (dup2(nfd, STDERR_FILENO) < 0)
648 (void)execv(argv[0], argv);
649 warn("exec: %s", argv[0]);
657 while (waitpid(pid, &status, 0) != pid)
659 return (WEXITSTATUS(status));
668 name = getprogname();
673 "usage: %s [-DLlMNSUX] [-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] [-p permissions] [-s size] [-v version]\n"
677 "\t[-w user:group] md-device mount-point\n", name);
679 "usage: %s -C [-lNU] [-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] [-s size] [-v version] md-device mount-point\n", name);