mdmfs.c revision 114589
178447Sdd/*
280607Sdd * Copyright (c) 2001 Dima Dorfman.
378447Sdd * All rights reserved.
478447Sdd *
578447Sdd * Redistribution and use in source and binary forms, with or without
678447Sdd * modification, are permitted provided that the following conditions
778447Sdd * are met:
878447Sdd * 1. Redistributions of source code must retain the above copyright
978447Sdd *    notice, this list of conditions and the following disclaimer.
1078447Sdd * 2. Redistributions in binary form must reproduce the above copyright
1178447Sdd *    notice, this list of conditions and the following disclaimer in the
1278447Sdd *    documentation and/or other materials provided with the distribution.
1378447Sdd *
1478447Sdd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1578447Sdd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1678447Sdd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1778447Sdd * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1878447Sdd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1978447Sdd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2078447Sdd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2178447Sdd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2278447Sdd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2378447Sdd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2478447Sdd * SUCH DAMAGE.
2578447Sdd */
2678447Sdd
2778447Sdd/*
28103798Sphk * mdmfs (md/MFS) is a wrapper around mdconfig(8),
2978447Sdd * newfs(8), and mount(8) that mimics the command line option set of
3078447Sdd * the deprecated mount_mfs(8).
3178447Sdd */
3278447Sdd
33114589Sobrien#include <sys/cdefs.h>
34114589Sobrien__FBSDID("$FreeBSD: head/sbin/mdmfs/mdmfs.c 114589 2003-05-03 18:41:59Z obrien $");
3578447Sdd
3678447Sdd#include <sys/param.h>
3778447Sdd#include <sys/mdioctl.h>
3878447Sdd#include <sys/stat.h>
3978447Sdd#include <sys/wait.h>
4078447Sdd
4178447Sdd#include <assert.h>
4278447Sdd#include <err.h>
4378447Sdd#include <fcntl.h>
4478447Sdd#include <grp.h>
4578447Sdd#include <paths.h>
4678447Sdd#include <pwd.h>
4778447Sdd#include <stdarg.h>
4878447Sdd#include <stdio.h>
4978447Sdd#include <stdlib.h>
5078447Sdd#include <string.h>
5178447Sdd#include <unistd.h>
5278447Sdd
5378447Sdd#include "pathnames.h"
5478447Sdd
5578447Sddtypedef enum { false, true } bool;
5678447Sdd
5778447Sddstruct mtpt_info {
5878447Sdd	uid_t		 mi_uid;
5978447Sdd	bool		 mi_have_uid;
6078447Sdd	gid_t		 mi_gid;
6178447Sdd	bool		 mi_have_gid;
6278447Sdd	mode_t		 mi_mode;
6378447Sdd	bool		 mi_have_mode;
6478447Sdd};
6578447Sdd
6681742Sddstatic	bool compat;		/* Full compatibility with mount_mfs? */
6778447Sddstatic	bool debug;		/* Emit debugging information? */
6878447Sddstatic	bool loudsubs;		/* Suppress output from helper programs? */
6978447Sddstatic	bool norun;		/* Actually run the helper programs? */
7078447Sddstatic	int unit;      		/* The unit we're working with. */
7178447Sddstatic	const char *mdname;	/* Name of memory disk device (e.g., "md"). */
7278447Sddstatic	size_t mdnamelen;	/* Length of mdname. */
7378447Sdd
7479052Skrisstatic void	 argappend(char **, const char *, ...) __printflike(2, 3);
7579052Skrisstatic void	 debugprintf(const char *, ...) __printflike(1, 2);
7678447Sddstatic void	 do_mdconfig_attach(const char *, const enum md_types);
7778447Sddstatic void	 do_mdconfig_attach_au(const char *, const enum md_types);
7878447Sddstatic void	 do_mdconfig_detach(void);
7978447Sddstatic void	 do_mount(const char *, const char *);
8078447Sddstatic void	 do_mtptsetup(const char *, struct mtpt_info *);
8178447Sddstatic void	 do_newfs(const char *);
8278447Sddstatic void	 extract_ugid(const char *, struct mtpt_info *);
8379052Skrisstatic int	 run(int *, const char *, ...) __printflike(2, 3);
8478447Sddstatic void	 usage(void);
8578447Sdd
8678447Sddint
8781628Sobrienmain(int argc, char **argv)
8878447Sdd{
8978447Sdd	struct mtpt_info mi;		/* Mountpoint info. */
9078447Sdd	char *mdconfig_arg, *newfs_arg,	/* Args to helper programs. */
9178447Sdd	    *mount_arg;
9278447Sdd	enum md_types mdtype;		/* The type of our memory disk. */
9378447Sdd	bool have_mdtype;
9478447Sdd	bool detach, softdep, autounit;
9578447Sdd	char *mtpoint, *unitstr;
9678447Sdd	char ch, *p;
9778447Sdd
9878447Sdd	/* Misc. initialization. */
9978447Sdd	(void)memset(&mi, '\0', sizeof(mi));
10078447Sdd	detach = true;
10178447Sdd	softdep = true;
10278447Sdd	autounit = false;
10378447Sdd	have_mdtype = false;
10478447Sdd	mdname = MD_NAME;
10578447Sdd	mdnamelen = strlen(mdname);
10678447Sdd	/*
10778447Sdd	 * Can't set these to NULL.  They may be passed to the
10878447Sdd	 * respective programs without modification.  I.e., we may not
10978447Sdd	 * receive any command-line options which will caused them to
11078447Sdd	 * be modified.
11178447Sdd	 */
11278447Sdd	mdconfig_arg = strdup("");
11378447Sdd	newfs_arg = strdup("");
11478447Sdd	mount_arg = strdup("");
11578447Sdd
11684167Siedowse	/* If we were started as mount_mfs or mfs, imply -C. */
11784167Siedowse	if (strcmp(getprogname(), "mount_mfs") == 0 ||
11884167Siedowse	    strcmp(getprogname(), "mfs") == 0)
11981742Sdd		compat = true;
12081742Sdd
12181628Sobrien	while ((ch = getopt(argc, argv,
122107475Srwatson	    "a:b:Cc:Dd:e:F:f:hi:LMm:Nn:O:o:p:Ss:t:Uv:w:X")) != -1)
12378447Sdd		switch (ch) {
12478447Sdd		case 'a':
12578447Sdd			argappend(&newfs_arg, "-a %s", optarg);
12678447Sdd			break;
12778447Sdd		case 'b':
12878447Sdd			argappend(&newfs_arg, "-b %s", optarg);
12978447Sdd			break;
13081742Sdd		case 'C':
13181742Sdd			if (compat)
13281742Sdd				usage();
13381742Sdd			compat = true;
13481742Sdd			break;
13578447Sdd		case 'c':
13678447Sdd			argappend(&newfs_arg, "-c %s", optarg);
13778447Sdd			break;
13878447Sdd		case 'D':
13981742Sdd			if (compat)
14081742Sdd				usage();
14178447Sdd			detach = false;
14278447Sdd			break;
14378447Sdd		case 'd':
14478447Sdd			argappend(&newfs_arg, "-d %s", optarg);
14578447Sdd			break;
14678447Sdd		case 'e':
14778447Sdd			argappend(&newfs_arg, "-e %s", optarg);
14878447Sdd			break;
14978447Sdd		case 'F':
15078447Sdd			if (have_mdtype)
15178447Sdd				usage();
15278447Sdd			mdtype = MD_VNODE;
15378447Sdd			have_mdtype = true;
15478447Sdd			argappend(&mdconfig_arg, "-f %s", optarg);
15578447Sdd			break;
15678447Sdd		case 'f':
15778447Sdd			argappend(&newfs_arg, "-f %s", optarg);
15878447Sdd			break;
15978447Sdd		case 'h':
16078447Sdd			usage();
16178447Sdd			break;
16278447Sdd		case 'i':
16378447Sdd			argappend(&newfs_arg, "-i %s", optarg);
16478447Sdd			break;
16578447Sdd		case 'L':
16681742Sdd			if (compat)
16781742Sdd				usage();
16878447Sdd			loudsubs = true;
16978447Sdd			break;
17078447Sdd		case 'M':
17178447Sdd			if (have_mdtype)
17278447Sdd				usage();
17378447Sdd			mdtype = MD_MALLOC;
17478447Sdd			have_mdtype = true;
17578447Sdd			break;
17678447Sdd		case 'm':
17778447Sdd			argappend(&newfs_arg, "-m %s", optarg);
17878447Sdd			break;
17978447Sdd		case 'N':
18081742Sdd			if (compat)
18181742Sdd				usage();
18278447Sdd			norun = true;
18378447Sdd			break;
18478447Sdd		case 'n':
18578447Sdd			argappend(&newfs_arg, "-n %s", optarg);
18678447Sdd			break;
18778447Sdd		case 'O':
18878447Sdd			argappend(&newfs_arg, "-o %s", optarg);
18978447Sdd			break;
19078447Sdd		case 'o':
19178447Sdd			argappend(&mount_arg, "-o %s", optarg);
19278447Sdd			break;
19378447Sdd		case 'p':
19481742Sdd			if (compat)
19581742Sdd				usage();
19678447Sdd			if (*optarg >= '0' && *optarg <= '7')
19778447Sdd				mi.mi_mode = strtol(optarg, NULL, 8);
19878447Sdd			if ((mi.mi_mode & ~07777) != 0)
19978447Sdd				usage();
20078447Sdd			mi.mi_have_mode = true;
20178447Sdd			break;
20278447Sdd		case 'S':
20381742Sdd			if (compat)
20481742Sdd				usage();
20578447Sdd			softdep = false;
20678447Sdd			break;
20778447Sdd		case 's':
20878447Sdd			argappend(&mdconfig_arg, "-s %s", optarg);
20978447Sdd			break;
21081742Sdd		case 'U':
21181742Sdd			softdep = true;
21281742Sdd			break;
213107475Srwatson		case 'v':
214107475Srwatson			argappend(&newfs_arg, "-O %s", optarg);
215107475Srwatson			break;
21678447Sdd		case 'w':
21781742Sdd			if (compat)
21881742Sdd				usage();
21978447Sdd			extract_ugid(optarg, &mi);
22078447Sdd			break;
22178447Sdd		case 'X':
22281742Sdd			if (compat)
22381742Sdd				usage();
22478447Sdd			debug = true;
22578447Sdd			break;
22678447Sdd		default:
22778447Sdd			usage();
22878447Sdd		}
22981628Sobrien	argc -= optind;
23081628Sobrien	argv += optind;
23181628Sobrien	if (argc < 2)
23278447Sdd		usage();
23378447Sdd
23481742Sdd	/* Make compatibility assumptions. */
23581742Sdd	if (compat) {
23681742Sdd		mi.mi_mode = 01777;
23781742Sdd		mi.mi_have_mode = true;
23881742Sdd	}
23981742Sdd
24078447Sdd	/* Derive 'unit' (global). */
24181628Sobrien	unitstr = argv[0];
24278447Sdd	if (strncmp(unitstr, "/dev/", 5) == 0)
24378447Sdd		unitstr += 5;
24478447Sdd	if (strncmp(unitstr, mdname, mdnamelen) == 0)
24578447Sdd		unitstr += mdnamelen;
24678447Sdd	if (*unitstr == '\0') {
24778447Sdd		autounit = true;
24878447Sdd		unit = -1;
24978447Sdd	} else {
25078447Sdd		unit = strtoul(unitstr, &p, 10);
25178447Sdd		if ((unsigned)unit == ULONG_MAX || *p != '\0')
25278447Sdd			errx(1, "bad device unit: %s", unitstr);
25378447Sdd	}
25478447Sdd
25581628Sobrien	mtpoint = argv[1];
25678447Sdd	if (!have_mdtype)
25778447Sdd		mdtype = MD_SWAP;
25878447Sdd	if (softdep)
25978447Sdd		argappend(&newfs_arg, "-U");
26078447Sdd
26178447Sdd	/* Do the work. */
26278447Sdd	if (detach && !autounit)
26378447Sdd		do_mdconfig_detach();
26478447Sdd	if (autounit)
26578447Sdd		do_mdconfig_attach_au(mdconfig_arg, mdtype);
26678447Sdd	else
26778447Sdd		do_mdconfig_attach(mdconfig_arg, mdtype);
26878447Sdd	do_newfs(newfs_arg);
26978447Sdd	do_mount(mount_arg, mtpoint);
27078447Sdd	do_mtptsetup(mtpoint, &mi);
27178447Sdd
27278447Sdd	return (0);
27378447Sdd}
27478447Sdd
27578447Sdd/*
27678447Sdd * Append the expansion of 'fmt' to the buffer pointed to by '*dstp';
27778447Sdd * reallocate as required.
27878447Sdd */
27978447Sddstatic void
28078447Sddargappend(char **dstp, const char *fmt, ...)
28178447Sdd{
28278447Sdd	char *old, *new;
28378447Sdd	va_list ap;
28478447Sdd
28578447Sdd	old = *dstp;
28678447Sdd	assert(old != NULL);
28778447Sdd
28878447Sdd	va_start(ap, fmt);
28978447Sdd	if (vasprintf(&new, fmt,ap) == -1)
29078447Sdd		errx(1, "vasprintf");
29178447Sdd	va_end(ap);
29278447Sdd
29378447Sdd	*dstp = new;
29478447Sdd	if (asprintf(&new, "%s %s", old, new) == -1)
29578447Sdd		errx(1, "asprintf");
29678447Sdd	free(*dstp);
29778447Sdd	free(old);
29878447Sdd
29978447Sdd	*dstp = new;
30078447Sdd}
30178447Sdd
30278447Sdd/*
30378447Sdd * If run-time debugging is enabled, print the expansion of 'fmt'.
30478447Sdd * Otherwise, do nothing.
30578447Sdd */
30678447Sddstatic void
30778447Sdddebugprintf(const char *fmt, ...)
30878447Sdd{
30978447Sdd	va_list ap;
31078447Sdd
31178447Sdd	if (!debug)
31278447Sdd		return;
31378447Sdd	fprintf(stderr, "DEBUG: ");
31478447Sdd	va_start(ap, fmt);
31578447Sdd	vfprintf(stderr, fmt, ap);
31678447Sdd	va_end(ap);
31778447Sdd	fprintf(stderr, "\n");
31878447Sdd	fflush(stderr);
31978447Sdd}
32078447Sdd
32178447Sdd/*
32278447Sdd * Attach a memory disk with a known unit.
32378447Sdd */
32478447Sddstatic void
32578447Sdddo_mdconfig_attach(const char *args, const enum md_types mdtype)
32678447Sdd{
32778447Sdd	int rv;
32878447Sdd	const char *ta;		/* Type arg. */
32978447Sdd
33078447Sdd	switch (mdtype) {
33178447Sdd	case MD_SWAP:
33278447Sdd		ta = "-t swap";
33378447Sdd		break;
33478447Sdd	case MD_VNODE:
33578447Sdd		ta = "-t vnode";
33678447Sdd		break;
33778447Sdd	case MD_MALLOC:
33878447Sdd		ta = "-t malloc";
33978447Sdd		break;
34078447Sdd	default:
34178447Sdd		abort();
34278447Sdd	}
34378447Sdd	rv = run(NULL, "%s -a %s%s -u %s%d", PATH_MDCONFIG, ta, args,
34478447Sdd	    mdname, unit);
34578447Sdd	if (rv)
34678447Sdd		errx(1, "mdconfig (attach) exited with error code %d", rv);
34778447Sdd}
34878447Sdd
34978447Sdd/*
35078447Sdd * Attach a memory disk with an unknown unit; use autounit.
35178447Sdd */
35278447Sddstatic void
35378447Sdddo_mdconfig_attach_au(const char *args, const enum md_types mdtype)
35478447Sdd{
35578447Sdd	const char *ta;		/* Type arg. */
35678447Sdd	char *linep, *linebuf; 	/* Line pointer, line buffer. */
35778447Sdd	int fd;			/* Standard output of mdconfig invocation. */
35878447Sdd	FILE *sfd;
35978447Sdd	int rv;
36078447Sdd	char *p;
36178447Sdd	size_t linelen;
36278447Sdd
36378447Sdd	switch (mdtype) {
36478447Sdd	case MD_SWAP:
36578447Sdd		ta = "-t swap";
36678447Sdd		break;
36778447Sdd	case MD_VNODE:
36878447Sdd		ta = "-t vnode";
36978447Sdd		break;
37078447Sdd	case MD_MALLOC:
37178447Sdd		ta = "-t malloc";
37278447Sdd		break;
37378447Sdd	default:
37478447Sdd		abort();
37578447Sdd	}
37678447Sdd	rv = run(&fd, "%s -a %s%s", PATH_MDCONFIG, ta, args);
37778447Sdd	if (rv)
37878447Sdd		errx(1, "mdconfig (attach) exited with error code %d", rv);
37978447Sdd
38078447Sdd	/* Receive the unit number. */
38178447Sdd	if (norun) {	/* Since we didn't run, we can't read.  Fake it. */
38278447Sdd		unit = -1;
38378447Sdd		return;
38478447Sdd	}
38578447Sdd	sfd = fdopen(fd, "r");
38678447Sdd	if (sfd == NULL)
38778447Sdd		err(1, "fdopen");
38878447Sdd	linep = fgetln(sfd, &linelen);
38978447Sdd	if (linep == NULL && linelen < mdnamelen + 1)
39078447Sdd		errx(1, "unexpected output from mdconfig (attach)");
39178447Sdd	/* If the output format changes, we want to know about it. */
39278447Sdd	assert(strncmp(linep, mdname, mdnamelen) == 0);
39378447Sdd	linebuf = malloc(linelen - mdnamelen + 1);
39478447Sdd	assert(linebuf != NULL);
39578447Sdd	/* Can't use strlcpy because linep is not NULL-terminated. */
39678447Sdd	strncpy(linebuf, linep + mdnamelen, linelen);
39778447Sdd	linebuf[linelen] = '\0';
39878447Sdd	unit = strtoul(linebuf, &p, 10);
39978447Sdd	if ((unsigned)unit == ULONG_MAX || *p != '\n')
40078447Sdd		errx(1, "unexpected output from mdconfig (attach)");
40178447Sdd
40278447Sdd	fclose(sfd);
40378447Sdd	close(fd);
40478447Sdd}
40578447Sdd
40678447Sdd/*
40778447Sdd * Detach a memory disk.
40878447Sdd */
40978447Sddstatic void
41078447Sdddo_mdconfig_detach(void)
41178447Sdd{
41278447Sdd	int rv;
41378447Sdd
41478447Sdd	rv = run(NULL, "%s -d -u %s%d", PATH_MDCONFIG, mdname, unit);
41578447Sdd	if (rv && debug)	/* This is allowed to fail. */
41678447Sdd		warnx("mdconfig (detach) exited with error code %d (ignored)",
41778447Sdd		      rv);
41878447Sdd}
41978447Sdd
42078447Sdd/*
42178447Sdd * Mount the configured memory disk.
42278447Sdd */
42378447Sddstatic void
42478447Sdddo_mount(const char *args, const char *mtpoint)
42578447Sdd{
42678447Sdd	int rv;
42778447Sdd
428103798Sphk	rv = run(NULL, "%s%s /dev/%s%d %s", PATH_MOUNT, args,
42978447Sdd	    mdname, unit, mtpoint);
43078447Sdd	if (rv)
43178447Sdd		errx(1, "mount exited with error code %d", rv);
43278447Sdd}
43378447Sdd
43478447Sdd/*
43578447Sdd * Various configuration of the mountpoint.  Mostly, enact 'mip'.
43678447Sdd */
43778447Sddstatic void
43878447Sdddo_mtptsetup(const char *mtpoint, struct mtpt_info *mip)
43978447Sdd{
44078447Sdd
44178447Sdd	if (mip->mi_have_mode) {
44278447Sdd		debugprintf("changing mode of %s to %o.", mtpoint,
44378447Sdd		    mip->mi_mode);
44478447Sdd		if (!norun)
44578447Sdd			if (chmod(mtpoint, mip->mi_mode) == -1)
44678447Sdd				err(1, "chmod: %s", mtpoint);
44778447Sdd	}
44878447Sdd	/*
44978447Sdd	 * We have to do these separately because the user may have
45078447Sdd	 * only specified one of them.
45178447Sdd	 */
45278447Sdd	if (mip->mi_have_uid) {
45378447Sdd		debugprintf("changing owner (user) or %s to %u.", mtpoint,
45478447Sdd		    mip->mi_uid);
45578447Sdd		if (!norun)
45678447Sdd			if (chown(mtpoint, mip->mi_uid, -1) == -1)
45778447Sdd				err(1, "chown %s to %u (user)", mtpoint,
45878447Sdd				    mip->mi_uid);
45978447Sdd	}
46078447Sdd	if (mip->mi_have_gid) {
46178447Sdd		debugprintf("changing owner (group) or %s to %u.", mtpoint,
46278447Sdd		    mip->mi_gid);
46378447Sdd		if (!norun)
46478447Sdd			if (chown(mtpoint, -1, mip->mi_gid) == -1)
46578447Sdd				err(1, "chown %s to %u (group)", mtpoint,
46678447Sdd				    mip->mi_gid);
46778447Sdd	}
46878447Sdd}
46978447Sdd
47078447Sdd/*
471102231Strhodes * Put a file system on the memory disk.
47278447Sdd */
47378447Sddstatic void
47478447Sdddo_newfs(const char *args)
47578447Sdd{
47678447Sdd	int rv;
47778447Sdd
478103798Sphk	rv = run(NULL, "%s%s /dev/%s%d", PATH_NEWFS, args, mdname, unit);
47978447Sdd	if (rv)
48078447Sdd		errx(1, "newfs exited with error code %d", rv);
48178447Sdd}
48278447Sdd
48378447Sdd/*
48478447Sdd * 'str' should be a user and group name similar to the last argument
48578447Sdd * to chown(1); i.e., a user, followed by a colon, followed by a
48678447Sdd * group.  The user and group in 'str' may be either a [ug]id or a
48778447Sdd * name.  Upon return, the uid and gid fields in 'mip' will contain
48878447Sdd * the uid and gid of the user and group name in 'str', respectively.
48978447Sdd *
49078447Sdd * In other words, this derives a user and group id from a string
49178447Sdd * formatted like the last argument to chown(1).
49278447Sdd */
49378447Sddstatic void
49478447Sddextract_ugid(const char *str, struct mtpt_info *mip)
49578447Sdd{
49678447Sdd	char *ug;			/* Writable 'str'. */
49778447Sdd	char *user, *group;		/* Result of extracton. */
49878447Sdd	struct passwd *pw;
49978447Sdd	struct group *gr;
50078447Sdd	char *p;
50178447Sdd	uid_t *uid;
50278447Sdd	gid_t *gid;
50378447Sdd
50478447Sdd	uid = &mip->mi_uid;
50578447Sdd	gid = &mip->mi_gid;
50678447Sdd	mip->mi_have_uid = mip->mi_have_gid = false;
50778447Sdd
50878447Sdd	/* Extract the user and group from 'str'.  Format above. */
50978711Sdd	ug = strdup(str);
51078447Sdd	assert(ug != NULL);
51178447Sdd	group = ug;
51278447Sdd	user = strsep(&group, ":");
51378447Sdd	if (user == NULL || group == NULL || *user == '\0' || *group == '\0')
51478447Sdd		usage();
51578447Sdd
51678447Sdd	/* Derive uid. */
51778447Sdd	*uid = strtoul(user, &p, 10);
51878447Sdd	if ((unsigned)*uid == ULONG_MAX)
51978447Sdd		usage();
52078447Sdd	if (*p != '\0') {
52178447Sdd		pw = getpwnam(user);
52278447Sdd		if (pw == NULL)
52378447Sdd			errx(1, "invalid user: %s", user);
52478447Sdd		*uid = pw->pw_uid;
52578447Sdd		mip->mi_have_uid = true;
52678447Sdd	}
52778447Sdd
52878447Sdd	/* Derive gid. */
52978447Sdd	*gid = strtoul(group, &p, 10);
53078447Sdd	if ((unsigned)*gid == ULONG_MAX)
53178447Sdd		usage();
53278447Sdd	if (*p != '\0') {
53378447Sdd		gr = getgrnam(group);
53478447Sdd		if (gr == NULL)
53578447Sdd			errx(1, "invalid group: %s", group);
53678447Sdd		*gid = gr->gr_gid;
53778447Sdd		mip->mi_have_gid = true;
53878447Sdd	}
53978447Sdd
54078447Sdd	free(ug);
54178447Sdd	/*
54278447Sdd	 * At this point we don't support only a username or only a
54378447Sdd	 * group name.  do_mtptsetup already does, so when this
54478447Sdd	 * feature is desired, this is the only routine that needs to
54578447Sdd	 * be changed.
54678447Sdd	 */
54778447Sdd	assert(mip->mi_have_uid);
54878447Sdd	assert(mip->mi_have_gid);
54978447Sdd}
55078447Sdd
55178447Sdd/*
55278447Sdd * Run a process with command name and arguments pointed to by the
55378447Sdd * formatted string 'cmdline'.  Since system(3) is not used, the first
55478447Sdd * space-delimited token of 'cmdline' must be the full pathname of the
55578447Sdd * program to run.  The return value is the return code of the process
55678447Sdd * spawned.  If 'ofd' is non-NULL, it is set to the standard output of
55778447Sdd * the program spawned (i.e., you can read from ofd and get the output
55878447Sdd * of the program).
55978447Sdd */
56078447Sddstatic int
56178447Sddrun(int *ofd, const char *cmdline, ...)
56278447Sdd{
56381628Sobrien	char **argv, **argvp;		/* Result of splitting 'cmd'. */
56481628Sobrien	int argc;
56578447Sdd	char *cmd;			/* Expansion of 'cmdline'. */
56678447Sdd	int pid, status;		/* Child info. */
56778447Sdd	int pfd[2];			/* Pipe to the child. */
56878447Sdd	int nfd;			/* Null (/dev/null) file descriptor. */
56978447Sdd	bool dup2dn;			/* Dup /dev/null to stdout? */
57078447Sdd	va_list ap;
57178447Sdd	char *p;
57278447Sdd	int rv, i;
57378447Sdd
57478447Sdd	dup2dn = true;
57578447Sdd	va_start(ap, cmdline);
57678447Sdd	rv = vasprintf(&cmd, cmdline, ap);
57778447Sdd	if (rv == -1)
57878447Sdd		err(1, "vasprintf");
57978447Sdd	va_end(ap);
58078447Sdd
58181628Sobrien	/* Split up 'cmd' into 'argv' for use with execve. */
58281628Sobrien	for (argc = 1, p = cmd; (p = strchr(p, ' ')) != NULL; p++)
58381628Sobrien		argc++;		/* 'argc' generation loop. */
58481628Sobrien	argv = (char **)malloc(sizeof(*argv) * (argc + 1));
58581628Sobrien	assert(argv != NULL);
58681628Sobrien	for (p = cmd, argvp = argv; (*argvp = strsep(&p, " ")) != NULL;)
58781628Sobrien		if (**argv != '\0')
58881628Sobrien			if (++argvp >= &argv[argc]) {
58981628Sobrien				*argvp = NULL;
59078447Sdd				break;
59178447Sdd			}
59281628Sobrien	assert(*argv);
59378447Sdd
59478447Sdd	/* Make sure the above loop works as expected. */
59578447Sdd	if (debug) {
59678447Sdd		/*
59778447Sdd		 * We can't, but should, use debugprintf here.  First,
59878447Sdd		 * it appends a trailing newline to the output, and
59978447Sdd		 * second it prepends "DEBUG: " to the output.  The
60078447Sdd		 * former is a problem for this would-be first call,
60178447Sdd		 * and the latter for the would-be call inside the
60278447Sdd		 * loop.
60378447Sdd		 */
60478447Sdd		(void)fprintf(stderr, "DEBUG: running:");
60578447Sdd		/* Should be equivilent to 'cmd' (before strsep, of course). */
60681628Sobrien		for (i = 0; argv[i] != NULL; i++)
60781628Sobrien			(void)fprintf(stderr, " %s", argv[i]);
60878447Sdd		(void)fprintf(stderr, "\n");
60978447Sdd	}
61078447Sdd
61178447Sdd	/* Create a pipe if necessary and fork the helper program. */
61278447Sdd	if (ofd != NULL) {
61378447Sdd		if (pipe(&pfd[0]) == -1)
61478447Sdd			err(1, "pipe");
61578447Sdd		*ofd = pfd[0];
61678447Sdd		dup2dn = false;
61778447Sdd	}
61878447Sdd	pid = fork();
61978447Sdd	switch (pid) {
62078447Sdd	case 0:
62178447Sdd		/* XXX can we call err() in here? */
62278447Sdd		if (norun)
62378447Sdd			_exit(0);
62478447Sdd		if (ofd != NULL)
62578447Sdd			if (dup2(pfd[1], STDOUT_FILENO) < 0)
62678447Sdd				err(1, "dup2");
62778447Sdd		if (!loudsubs) {
62878447Sdd			nfd = open(_PATH_DEVNULL, O_RDWR);
62978447Sdd			if (nfd == -1)
63078447Sdd				err(1, "open: %s", _PATH_DEVNULL);
63178447Sdd			if (dup2(nfd, STDIN_FILENO) < 0)
63278447Sdd				err(1, "dup2");
63378447Sdd			if (dup2dn)
63478447Sdd				if (dup2(nfd, STDOUT_FILENO) < 0)
63578447Sdd				   err(1, "dup2");
63678447Sdd			if (dup2(nfd, STDERR_FILENO) < 0)
63778447Sdd				err(1, "dup2");
63878447Sdd		}
63978447Sdd
64081628Sobrien		(void)execv(argv[0], argv);
64181628Sobrien		warn("exec: %s", argv[0]);
64278447Sdd		_exit(-1);
64378447Sdd	case -1:
64478447Sdd		err(1, "fork");
64578447Sdd	}
64678447Sdd
64778447Sdd	free(cmd);
64881628Sobrien	free(argv);
64978447Sdd	while (waitpid(pid, &status, 0) != pid)
65078447Sdd		;
65178447Sdd	return (WEXITSTATUS(status));
65278447Sdd}
65378447Sdd
65478447Sddstatic void
65578447Sddusage(void)
65678447Sdd{
65781742Sdd	const char *name;
65878447Sdd
65981742Sdd	if (compat)
66081742Sdd		name = getprogname();
66181742Sdd	else
66281742Sdd		name = "mdmfs";
66381742Sdd	if (!compat)
66481742Sdd		fprintf(stderr,
66595258Sdes"usage: %s [-DLMNSUX] [-a maxcontig [-b block-size] [-c cylinders]\n"
66681742Sdd"\t[-d rotdelay] [-e maxbpg] [-F file] [-f frag-size] [-i bytes]\n"
66781742Sdd"\t[-m percent-free] [-n rotational-positions] [-O optimization]\n"
66881742Sdd"\t[-o mount-options] [-p permissions] [-s size] [-w user:group]\n"
66981742Sdd"\tmd-device mount-point\n", name);
67078447Sdd	fprintf(stderr,
67195258Sdes"usage: %s -C [-NU] [-a maxcontig] [-b block-size] [-c cylinders]\n"
67278447Sdd"\t[-d rotdelay] [-e maxbpg] [-F file] [-f frag-size] [-i bytes]\n"
67378447Sdd"\t[-m percent-free] [-n rotational-positions] [-O optimization]\n"
67481742Sdd"\t[-o mount-options] [-s size] md-device mount-point\n", name);
67578447Sdd	exit(1);
67678447Sdd}
677