mdmfs.c revision 163952
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 163952 2006-11-03 12:02:24Z ru $");
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
5378447Sddtypedef enum { false, true } bool;
5478447Sdd
5578447Sddstruct mtpt_info {
5678447Sdd	uid_t		 mi_uid;
5778447Sdd	bool		 mi_have_uid;
5878447Sdd	gid_t		 mi_gid;
5978447Sdd	bool		 mi_have_gid;
6078447Sdd	mode_t		 mi_mode;
6178447Sdd	bool		 mi_have_mode;
6278447Sdd};
6378447Sdd
6478447Sddstatic	bool debug;		/* Emit debugging information? */
6578447Sddstatic	bool loudsubs;		/* Suppress output from helper programs? */
6678447Sddstatic	bool norun;		/* Actually run the helper programs? */
6778447Sddstatic	int unit;      		/* The unit we're working with. */
6878447Sddstatic	const char *mdname;	/* Name of memory disk device (e.g., "md"). */
6978447Sddstatic	size_t mdnamelen;	/* Length of mdname. */
70155769Ssobomaxstatic	const char *path_mdconfig =_PATH_MDCONFIG;
7178447Sdd
7279052Skrisstatic void	 argappend(char **, const char *, ...) __printflike(2, 3);
7379052Skrisstatic void	 debugprintf(const char *, ...) __printflike(1, 2);
7478447Sddstatic void	 do_mdconfig_attach(const char *, const enum md_types);
7578447Sddstatic void	 do_mdconfig_attach_au(const char *, const enum md_types);
7678447Sddstatic void	 do_mdconfig_detach(void);
7778447Sddstatic void	 do_mount(const char *, const char *);
7878447Sddstatic void	 do_mtptsetup(const char *, struct mtpt_info *);
7978447Sddstatic void	 do_newfs(const char *);
8078447Sddstatic void	 extract_ugid(const char *, struct mtpt_info *);
8179052Skrisstatic int	 run(int *, const char *, ...) __printflike(2, 3);
8278447Sddstatic void	 usage(void);
8378447Sdd
8478447Sddint
8581628Sobrienmain(int argc, char **argv)
8678447Sdd{
8778447Sdd	struct mtpt_info mi;		/* Mountpoint info. */
8878447Sdd	char *mdconfig_arg, *newfs_arg,	/* Args to helper programs. */
8978447Sdd	    *mount_arg;
9078447Sdd	enum md_types mdtype;		/* The type of our memory disk. */
9178447Sdd	bool have_mdtype;
92153961Sdd	bool detach, softdep, autounit, newfs;
9378447Sdd	char *mtpoint, *unitstr;
94124830Sgrehan	char *p;
95124830Sgrehan	int ch;
96118500Syar	void *set;
97141082Sssouhlal	unsigned long ul;
9878447Sdd
9978447Sdd	/* Misc. initialization. */
10078447Sdd	(void)memset(&mi, '\0', sizeof(mi));
10178447Sdd	detach = true;
10278447Sdd	softdep = true;
10378447Sdd	autounit = false;
104153961Sdd	newfs = true;
10578447Sdd	have_mdtype = false;
106140815Sssouhlal	mdtype = MD_SWAP;
10778447Sdd	mdname = MD_NAME;
10878447Sdd	mdnamelen = strlen(mdname);
10978447Sdd	/*
11078447Sdd	 * Can't set these to NULL.  They may be passed to the
11178447Sdd	 * respective programs without modification.  I.e., we may not
11278447Sdd	 * receive any command-line options which will caused them to
11378447Sdd	 * be modified.
11478447Sdd	 */
11578447Sdd	mdconfig_arg = strdup("");
11678447Sdd	newfs_arg = strdup("");
11778447Sdd	mount_arg = strdup("");
11878447Sdd
11984167Siedowse	/* If we were started as mount_mfs or mfs, imply -C. */
12084167Siedowse	if (strcmp(getprogname(), "mount_mfs") == 0 ||
121163952Sru	    strcmp(getprogname(), "mfs") == 0) {
122163952Sru		/* Make compatibility assumptions. */
123163952Sru		mi.mi_mode = 01777;
124163952Sru		mi.mi_have_mode = true;
125163952Sru	}
12681742Sdd
12781628Sobrien	while ((ch = getopt(argc, argv,
128155769Ssobomax	    "a:b:Cc:Dd:E:e:F:f:hi:LlMm:Nn:O:o:Pp:Ss:t:Uv:w:X")) != -1)
12978447Sdd		switch (ch) {
13078447Sdd		case 'a':
13178447Sdd			argappend(&newfs_arg, "-a %s", optarg);
13278447Sdd			break;
13378447Sdd		case 'b':
13478447Sdd			argappend(&newfs_arg, "-b %s", optarg);
13578447Sdd			break;
13681742Sdd		case 'C':
137163952Sru			/* Ignored for compatibility. */
13881742Sdd			break;
13978447Sdd		case 'c':
14078447Sdd			argappend(&newfs_arg, "-c %s", optarg);
14178447Sdd			break;
14278447Sdd		case 'D':
14378447Sdd			detach = false;
14478447Sdd			break;
14578447Sdd		case 'd':
14678447Sdd			argappend(&newfs_arg, "-d %s", optarg);
14778447Sdd			break;
148155769Ssobomax		case 'E':
149155769Ssobomax			path_mdconfig = optarg;
150155769Ssobomax			break;
15178447Sdd		case 'e':
15278447Sdd			argappend(&newfs_arg, "-e %s", optarg);
15378447Sdd			break;
15478447Sdd		case 'F':
15578447Sdd			if (have_mdtype)
15678447Sdd				usage();
15778447Sdd			mdtype = MD_VNODE;
15878447Sdd			have_mdtype = true;
15978447Sdd			argappend(&mdconfig_arg, "-f %s", optarg);
16078447Sdd			break;
16178447Sdd		case 'f':
16278447Sdd			argappend(&newfs_arg, "-f %s", optarg);
16378447Sdd			break;
16478447Sdd		case 'h':
16578447Sdd			usage();
16678447Sdd			break;
16778447Sdd		case 'i':
16878447Sdd			argappend(&newfs_arg, "-i %s", optarg);
16978447Sdd			break;
17078447Sdd		case 'L':
17178447Sdd			loudsubs = true;
17278447Sdd			break;
173126255Srwatson		case 'l':
174126255Srwatson			argappend(&newfs_arg, "-l");
175126255Srwatson			break;
17678447Sdd		case 'M':
17778447Sdd			if (have_mdtype)
17878447Sdd				usage();
17978447Sdd			mdtype = MD_MALLOC;
18078447Sdd			have_mdtype = true;
18178447Sdd			break;
18278447Sdd		case 'm':
18378447Sdd			argappend(&newfs_arg, "-m %s", optarg);
18478447Sdd			break;
18578447Sdd		case 'N':
18678447Sdd			norun = true;
18778447Sdd			break;
18878447Sdd		case 'n':
18978447Sdd			argappend(&newfs_arg, "-n %s", optarg);
19078447Sdd			break;
19178447Sdd		case 'O':
19278447Sdd			argappend(&newfs_arg, "-o %s", optarg);
19378447Sdd			break;
19478447Sdd		case 'o':
19578447Sdd			argappend(&mount_arg, "-o %s", optarg);
19678447Sdd			break;
197153961Sdd		case 'P':
198153961Sdd			newfs = false;
199153961Sdd			break;
20078447Sdd		case 'p':
201118500Syar			if ((set = setmode(optarg)) == NULL)
20278447Sdd				usage();
203118500Syar			mi.mi_mode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO);
20478447Sdd			mi.mi_have_mode = true;
205118500Syar			free(set);
20678447Sdd			break;
20778447Sdd		case 'S':
20878447Sdd			softdep = false;
20978447Sdd			break;
21078447Sdd		case 's':
21178447Sdd			argappend(&mdconfig_arg, "-s %s", optarg);
21278447Sdd			break;
21381742Sdd		case 'U':
21481742Sdd			softdep = true;
21581742Sdd			break;
216107475Srwatson		case 'v':
217107475Srwatson			argappend(&newfs_arg, "-O %s", optarg);
218107475Srwatson			break;
21978447Sdd		case 'w':
22078447Sdd			extract_ugid(optarg, &mi);
22178447Sdd			break;
22278447Sdd		case 'X':
22378447Sdd			debug = true;
22478447Sdd			break;
22578447Sdd		default:
22678447Sdd			usage();
22778447Sdd		}
22881628Sobrien	argc -= optind;
22981628Sobrien	argv += optind;
23081628Sobrien	if (argc < 2)
23178447Sdd		usage();
23278447Sdd
23378447Sdd	/* Derive 'unit' (global). */
23481628Sobrien	unitstr = argv[0];
23578447Sdd	if (strncmp(unitstr, "/dev/", 5) == 0)
23678447Sdd		unitstr += 5;
23778447Sdd	if (strncmp(unitstr, mdname, mdnamelen) == 0)
23878447Sdd		unitstr += mdnamelen;
23978447Sdd	if (*unitstr == '\0') {
24078447Sdd		autounit = true;
24178447Sdd		unit = -1;
24278447Sdd	} else {
243141082Sssouhlal		ul = strtoul(unitstr, &p, 10);
244141082Sssouhlal		if (ul == ULONG_MAX || *p != '\0')
24578447Sdd			errx(1, "bad device unit: %s", unitstr);
246141082Sssouhlal		unit = ul;
24778447Sdd	}
24878447Sdd
24981628Sobrien	mtpoint = argv[1];
25078447Sdd	if (!have_mdtype)
25178447Sdd		mdtype = MD_SWAP;
25278447Sdd	if (softdep)
25378447Sdd		argappend(&newfs_arg, "-U");
254153961Sdd	if (mdtype != MD_VNODE && !newfs)
255153961Sdd		errx(1, "-P requires a vnode-backed disk");
25678447Sdd
25778447Sdd	/* Do the work. */
25878447Sdd	if (detach && !autounit)
25978447Sdd		do_mdconfig_detach();
26078447Sdd	if (autounit)
26178447Sdd		do_mdconfig_attach_au(mdconfig_arg, mdtype);
26278447Sdd	else
26378447Sdd		do_mdconfig_attach(mdconfig_arg, mdtype);
264153961Sdd	if (newfs)
265153961Sdd		do_newfs(newfs_arg);
26678447Sdd	do_mount(mount_arg, mtpoint);
26778447Sdd	do_mtptsetup(mtpoint, &mi);
26878447Sdd
26978447Sdd	return (0);
27078447Sdd}
27178447Sdd
27278447Sdd/*
27378447Sdd * Append the expansion of 'fmt' to the buffer pointed to by '*dstp';
27478447Sdd * reallocate as required.
27578447Sdd */
27678447Sddstatic void
27778447Sddargappend(char **dstp, const char *fmt, ...)
27878447Sdd{
27978447Sdd	char *old, *new;
28078447Sdd	va_list ap;
28178447Sdd
28278447Sdd	old = *dstp;
28378447Sdd	assert(old != NULL);
28478447Sdd
28578447Sdd	va_start(ap, fmt);
28678447Sdd	if (vasprintf(&new, fmt,ap) == -1)
28778447Sdd		errx(1, "vasprintf");
28878447Sdd	va_end(ap);
28978447Sdd
29078447Sdd	*dstp = new;
29178447Sdd	if (asprintf(&new, "%s %s", old, new) == -1)
29278447Sdd		errx(1, "asprintf");
29378447Sdd	free(*dstp);
29478447Sdd	free(old);
29578447Sdd
29678447Sdd	*dstp = new;
29778447Sdd}
29878447Sdd
29978447Sdd/*
30078447Sdd * If run-time debugging is enabled, print the expansion of 'fmt'.
30178447Sdd * Otherwise, do nothing.
30278447Sdd */
30378447Sddstatic void
30478447Sdddebugprintf(const char *fmt, ...)
30578447Sdd{
30678447Sdd	va_list ap;
30778447Sdd
30878447Sdd	if (!debug)
30978447Sdd		return;
31078447Sdd	fprintf(stderr, "DEBUG: ");
31178447Sdd	va_start(ap, fmt);
31278447Sdd	vfprintf(stderr, fmt, ap);
31378447Sdd	va_end(ap);
31478447Sdd	fprintf(stderr, "\n");
31578447Sdd	fflush(stderr);
31678447Sdd}
31778447Sdd
31878447Sdd/*
31978447Sdd * Attach a memory disk with a known unit.
32078447Sdd */
32178447Sddstatic void
32278447Sdddo_mdconfig_attach(const char *args, const enum md_types mdtype)
32378447Sdd{
32478447Sdd	int rv;
32578447Sdd	const char *ta;		/* Type arg. */
32678447Sdd
32778447Sdd	switch (mdtype) {
32878447Sdd	case MD_SWAP:
32978447Sdd		ta = "-t swap";
33078447Sdd		break;
33178447Sdd	case MD_VNODE:
33278447Sdd		ta = "-t vnode";
33378447Sdd		break;
33478447Sdd	case MD_MALLOC:
33578447Sdd		ta = "-t malloc";
33678447Sdd		break;
33778447Sdd	default:
33878447Sdd		abort();
33978447Sdd	}
340155769Ssobomax	rv = run(NULL, "%s -a %s%s -u %s%d", path_mdconfig, ta, args,
34178447Sdd	    mdname, unit);
34278447Sdd	if (rv)
34378447Sdd		errx(1, "mdconfig (attach) exited with error code %d", rv);
34478447Sdd}
34578447Sdd
34678447Sdd/*
34778447Sdd * Attach a memory disk with an unknown unit; use autounit.
34878447Sdd */
34978447Sddstatic void
35078447Sdddo_mdconfig_attach_au(const char *args, const enum md_types mdtype)
35178447Sdd{
35278447Sdd	const char *ta;		/* Type arg. */
35378447Sdd	char *linep, *linebuf; 	/* Line pointer, line buffer. */
35478447Sdd	int fd;			/* Standard output of mdconfig invocation. */
35578447Sdd	FILE *sfd;
35678447Sdd	int rv;
35778447Sdd	char *p;
35878447Sdd	size_t linelen;
359141082Sssouhlal	unsigned long ul;
36078447Sdd
36178447Sdd	switch (mdtype) {
36278447Sdd	case MD_SWAP:
36378447Sdd		ta = "-t swap";
36478447Sdd		break;
36578447Sdd	case MD_VNODE:
36678447Sdd		ta = "-t vnode";
36778447Sdd		break;
36878447Sdd	case MD_MALLOC:
36978447Sdd		ta = "-t malloc";
37078447Sdd		break;
37178447Sdd	default:
37278447Sdd		abort();
37378447Sdd	}
374155769Ssobomax	rv = run(&fd, "%s -a %s%s", path_mdconfig, ta, args);
37578447Sdd	if (rv)
37678447Sdd		errx(1, "mdconfig (attach) exited with error code %d", rv);
37778447Sdd
37878447Sdd	/* Receive the unit number. */
37978447Sdd	if (norun) {	/* Since we didn't run, we can't read.  Fake it. */
380153637Sdd		unit = 0;
38178447Sdd		return;
38278447Sdd	}
38378447Sdd	sfd = fdopen(fd, "r");
38478447Sdd	if (sfd == NULL)
38578447Sdd		err(1, "fdopen");
38678447Sdd	linep = fgetln(sfd, &linelen);
38778447Sdd	if (linep == NULL && linelen < mdnamelen + 1)
38878447Sdd		errx(1, "unexpected output from mdconfig (attach)");
38978447Sdd	/* If the output format changes, we want to know about it. */
39078447Sdd	assert(strncmp(linep, mdname, mdnamelen) == 0);
39178447Sdd	linebuf = malloc(linelen - mdnamelen + 1);
39278447Sdd	assert(linebuf != NULL);
39378447Sdd	/* Can't use strlcpy because linep is not NULL-terminated. */
39478447Sdd	strncpy(linebuf, linep + mdnamelen, linelen);
39578447Sdd	linebuf[linelen] = '\0';
396141082Sssouhlal	ul = strtoul(linebuf, &p, 10);
397141082Sssouhlal	if (ul == ULONG_MAX || *p != '\n')
39878447Sdd		errx(1, "unexpected output from mdconfig (attach)");
399141082Sssouhlal	unit = ul;
40078447Sdd
40178447Sdd	fclose(sfd);
40278447Sdd	close(fd);
40378447Sdd}
40478447Sdd
40578447Sdd/*
40678447Sdd * Detach a memory disk.
40778447Sdd */
40878447Sddstatic void
40978447Sdddo_mdconfig_detach(void)
41078447Sdd{
41178447Sdd	int rv;
41278447Sdd
413155769Ssobomax	rv = run(NULL, "%s -d -u %s%d", path_mdconfig, mdname, unit);
41478447Sdd	if (rv && debug)	/* This is allowed to fail. */
41578447Sdd		warnx("mdconfig (detach) exited with error code %d (ignored)",
41678447Sdd		      rv);
41778447Sdd}
41878447Sdd
41978447Sdd/*
42078447Sdd * Mount the configured memory disk.
42178447Sdd */
42278447Sddstatic void
42378447Sdddo_mount(const char *args, const char *mtpoint)
42478447Sdd{
42578447Sdd	int rv;
42678447Sdd
427117033Sgordon	rv = run(NULL, "%s%s /dev/%s%d %s", _PATH_MOUNT, args,
42878447Sdd	    mdname, unit, mtpoint);
42978447Sdd	if (rv)
43078447Sdd		errx(1, "mount exited with error code %d", rv);
43178447Sdd}
43278447Sdd
43378447Sdd/*
43478447Sdd * Various configuration of the mountpoint.  Mostly, enact 'mip'.
43578447Sdd */
43678447Sddstatic void
43778447Sdddo_mtptsetup(const char *mtpoint, struct mtpt_info *mip)
43878447Sdd{
43978447Sdd
44078447Sdd	if (mip->mi_have_mode) {
44178447Sdd		debugprintf("changing mode of %s to %o.", mtpoint,
44278447Sdd		    mip->mi_mode);
44378447Sdd		if (!norun)
44478447Sdd			if (chmod(mtpoint, mip->mi_mode) == -1)
44578447Sdd				err(1, "chmod: %s", mtpoint);
44678447Sdd	}
44778447Sdd	/*
44878447Sdd	 * We have to do these separately because the user may have
44978447Sdd	 * only specified one of them.
45078447Sdd	 */
45178447Sdd	if (mip->mi_have_uid) {
45278447Sdd		debugprintf("changing owner (user) or %s to %u.", mtpoint,
45378447Sdd		    mip->mi_uid);
45478447Sdd		if (!norun)
45578447Sdd			if (chown(mtpoint, mip->mi_uid, -1) == -1)
45678447Sdd				err(1, "chown %s to %u (user)", mtpoint,
45778447Sdd				    mip->mi_uid);
45878447Sdd	}
45978447Sdd	if (mip->mi_have_gid) {
46078447Sdd		debugprintf("changing owner (group) or %s to %u.", mtpoint,
46178447Sdd		    mip->mi_gid);
46278447Sdd		if (!norun)
46378447Sdd			if (chown(mtpoint, -1, mip->mi_gid) == -1)
46478447Sdd				err(1, "chown %s to %u (group)", mtpoint,
46578447Sdd				    mip->mi_gid);
46678447Sdd	}
46778447Sdd}
46878447Sdd
46978447Sdd/*
470102231Strhodes * Put a file system on the memory disk.
47178447Sdd */
47278447Sddstatic void
47378447Sdddo_newfs(const char *args)
47478447Sdd{
47578447Sdd	int rv;
47678447Sdd
477117033Sgordon	rv = run(NULL, "%s%s /dev/%s%d", _PATH_NEWFS, args, mdname, unit);
47878447Sdd	if (rv)
47978447Sdd		errx(1, "newfs exited with error code %d", rv);
48078447Sdd}
48178447Sdd
48278447Sdd/*
48378447Sdd * 'str' should be a user and group name similar to the last argument
48478447Sdd * to chown(1); i.e., a user, followed by a colon, followed by a
48578447Sdd * group.  The user and group in 'str' may be either a [ug]id or a
48678447Sdd * name.  Upon return, the uid and gid fields in 'mip' will contain
48778447Sdd * the uid and gid of the user and group name in 'str', respectively.
48878447Sdd *
48978447Sdd * In other words, this derives a user and group id from a string
49078447Sdd * formatted like the last argument to chown(1).
491151315Srse *
492151315Srse * Notice: At this point we don't support only a username or only a
493151315Srse * group name. do_mtptsetup already does, so when this feature is
494151315Srse * desired, this is the only routine that needs to be changed.
49578447Sdd */
49678447Sddstatic void
49778447Sddextract_ugid(const char *str, struct mtpt_info *mip)
49878447Sdd{
49978447Sdd	char *ug;			/* Writable 'str'. */
50078447Sdd	char *user, *group;		/* Result of extracton. */
50178447Sdd	struct passwd *pw;
50278447Sdd	struct group *gr;
50378447Sdd	char *p;
50478447Sdd	uid_t *uid;
50578447Sdd	gid_t *gid;
50678447Sdd
50778447Sdd	uid = &mip->mi_uid;
50878447Sdd	gid = &mip->mi_gid;
50978447Sdd	mip->mi_have_uid = mip->mi_have_gid = false;
51078447Sdd
51178447Sdd	/* Extract the user and group from 'str'.  Format above. */
51278711Sdd	ug = strdup(str);
51378447Sdd	assert(ug != NULL);
51478447Sdd	group = ug;
51578447Sdd	user = strsep(&group, ":");
51678447Sdd	if (user == NULL || group == NULL || *user == '\0' || *group == '\0')
51778447Sdd		usage();
51878447Sdd
51978447Sdd	/* Derive uid. */
52078447Sdd	*uid = strtoul(user, &p, 10);
521117430Skan	if (*uid == (uid_t)ULONG_MAX)
52278447Sdd		usage();
52378447Sdd	if (*p != '\0') {
52478447Sdd		pw = getpwnam(user);
52578447Sdd		if (pw == NULL)
52678447Sdd			errx(1, "invalid user: %s", user);
52778447Sdd		*uid = pw->pw_uid;
52878447Sdd	}
529151315Srse	mip->mi_have_uid = true;
53078447Sdd
53178447Sdd	/* Derive gid. */
53278447Sdd	*gid = strtoul(group, &p, 10);
533117430Skan	if (*gid == (gid_t)ULONG_MAX)
53478447Sdd		usage();
53578447Sdd	if (*p != '\0') {
53678447Sdd		gr = getgrnam(group);
53778447Sdd		if (gr == NULL)
53878447Sdd			errx(1, "invalid group: %s", group);
53978447Sdd		*gid = gr->gr_gid;
54078447Sdd	}
541151315Srse	mip->mi_have_gid = true;
54278447Sdd
54378447Sdd	free(ug);
54478447Sdd}
54578447Sdd
54678447Sdd/*
54778447Sdd * Run a process with command name and arguments pointed to by the
54878447Sdd * formatted string 'cmdline'.  Since system(3) is not used, the first
54978447Sdd * space-delimited token of 'cmdline' must be the full pathname of the
55078447Sdd * program to run.  The return value is the return code of the process
55178447Sdd * spawned.  If 'ofd' is non-NULL, it is set to the standard output of
55278447Sdd * the program spawned (i.e., you can read from ofd and get the output
55378447Sdd * of the program).
55478447Sdd */
55578447Sddstatic int
55678447Sddrun(int *ofd, const char *cmdline, ...)
55778447Sdd{
55881628Sobrien	char **argv, **argvp;		/* Result of splitting 'cmd'. */
55981628Sobrien	int argc;
56078447Sdd	char *cmd;			/* Expansion of 'cmdline'. */
56178447Sdd	int pid, status;		/* Child info. */
56278447Sdd	int pfd[2];			/* Pipe to the child. */
56378447Sdd	int nfd;			/* Null (/dev/null) file descriptor. */
56478447Sdd	bool dup2dn;			/* Dup /dev/null to stdout? */
56578447Sdd	va_list ap;
56678447Sdd	char *p;
56778447Sdd	int rv, i;
56878447Sdd
56978447Sdd	dup2dn = true;
57078447Sdd	va_start(ap, cmdline);
57178447Sdd	rv = vasprintf(&cmd, cmdline, ap);
57278447Sdd	if (rv == -1)
57378447Sdd		err(1, "vasprintf");
57478447Sdd	va_end(ap);
57578447Sdd
57681628Sobrien	/* Split up 'cmd' into 'argv' for use with execve. */
57781628Sobrien	for (argc = 1, p = cmd; (p = strchr(p, ' ')) != NULL; p++)
57881628Sobrien		argc++;		/* 'argc' generation loop. */
57981628Sobrien	argv = (char **)malloc(sizeof(*argv) * (argc + 1));
58081628Sobrien	assert(argv != NULL);
58181628Sobrien	for (p = cmd, argvp = argv; (*argvp = strsep(&p, " ")) != NULL;)
58281628Sobrien		if (**argv != '\0')
58381628Sobrien			if (++argvp >= &argv[argc]) {
58481628Sobrien				*argvp = NULL;
58578447Sdd				break;
58678447Sdd			}
58781628Sobrien	assert(*argv);
58878447Sdd
58978447Sdd	/* Make sure the above loop works as expected. */
59078447Sdd	if (debug) {
59178447Sdd		/*
59278447Sdd		 * We can't, but should, use debugprintf here.  First,
59378447Sdd		 * it appends a trailing newline to the output, and
59478447Sdd		 * second it prepends "DEBUG: " to the output.  The
59578447Sdd		 * former is a problem for this would-be first call,
59678447Sdd		 * and the latter for the would-be call inside the
59778447Sdd		 * loop.
59878447Sdd		 */
59978447Sdd		(void)fprintf(stderr, "DEBUG: running:");
60078447Sdd		/* Should be equivilent to 'cmd' (before strsep, of course). */
60181628Sobrien		for (i = 0; argv[i] != NULL; i++)
60281628Sobrien			(void)fprintf(stderr, " %s", argv[i]);
60378447Sdd		(void)fprintf(stderr, "\n");
60478447Sdd	}
60578447Sdd
60678447Sdd	/* Create a pipe if necessary and fork the helper program. */
60778447Sdd	if (ofd != NULL) {
60878447Sdd		if (pipe(&pfd[0]) == -1)
60978447Sdd			err(1, "pipe");
61078447Sdd		*ofd = pfd[0];
61178447Sdd		dup2dn = false;
61278447Sdd	}
61378447Sdd	pid = fork();
61478447Sdd	switch (pid) {
61578447Sdd	case 0:
61678447Sdd		/* XXX can we call err() in here? */
61778447Sdd		if (norun)
61878447Sdd			_exit(0);
61978447Sdd		if (ofd != NULL)
62078447Sdd			if (dup2(pfd[1], STDOUT_FILENO) < 0)
62178447Sdd				err(1, "dup2");
62278447Sdd		if (!loudsubs) {
62378447Sdd			nfd = open(_PATH_DEVNULL, O_RDWR);
62478447Sdd			if (nfd == -1)
62578447Sdd				err(1, "open: %s", _PATH_DEVNULL);
62678447Sdd			if (dup2(nfd, STDIN_FILENO) < 0)
62778447Sdd				err(1, "dup2");
62878447Sdd			if (dup2dn)
62978447Sdd				if (dup2(nfd, STDOUT_FILENO) < 0)
63078447Sdd				   err(1, "dup2");
63178447Sdd			if (dup2(nfd, STDERR_FILENO) < 0)
63278447Sdd				err(1, "dup2");
63378447Sdd		}
63478447Sdd
63581628Sobrien		(void)execv(argv[0], argv);
63681628Sobrien		warn("exec: %s", argv[0]);
63778447Sdd		_exit(-1);
63878447Sdd	case -1:
63978447Sdd		err(1, "fork");
64078447Sdd	}
64178447Sdd
64278447Sdd	free(cmd);
64381628Sobrien	free(argv);
64478447Sdd	while (waitpid(pid, &status, 0) != pid)
64578447Sdd		;
64678447Sdd	return (WEXITSTATUS(status));
64778447Sdd}
64878447Sdd
64978447Sddstatic void
65078447Sddusage(void)
65178447Sdd{
65278447Sdd
653163952Sru	fprintf(stderr,
654153961Sdd"usage: %s [-DLlMNPSUX] [-a maxcontig] [-b block-size] [-c cylinders]\n"
655155769Ssobomax"\t[-d rotdelay] [-E path-mdconfig] [-e maxbpg] [-F file] [-f frag-size]\n"
656155769Ssobomax"\t[-i bytes] [-m percent-free] [-n rotational-positions] [-O optimization]\n"
657141611Sru"\t[-o mount-options] [-p permissions] [-s size] [-v version]\n"
658163952Sru"\t[-w user:group] md-device mount-point\n", getprogname());
65978447Sdd	exit(1);
66078447Sdd}
661