mount_fs.c revision 164977
1152344Srodrigc/*
2152344Srodrigc * Copyright (c) 1992, 1993, 1994
3152344Srodrigc *	The Regents of the University of California.  All rights reserved.
4152344Srodrigc *
5152344Srodrigc * This code is derived from software donated to Berkeley by
6152344Srodrigc * Jan-Simon Pendry.
7152344Srodrigc *
8152344Srodrigc * Redistribution and use in source and binary forms, with or without
9152344Srodrigc * modification, are permitted provided that the following conditions
10152344Srodrigc * are met:
11152344Srodrigc * 1. Redistributions of source code must retain the above copyright
12152344Srodrigc *    notice, this list of conditions and the following disclaimer.
13152344Srodrigc * 2. Redistributions in binary form must reproduce the above copyright
14152344Srodrigc *    notice, this list of conditions and the following disclaimer in the
15152344Srodrigc *    documentation and/or other materials provided with the distribution.
16152344Srodrigc * 3. All advertising materials mentioning features or use of this software
17152344Srodrigc *    must display the following acknowledgement:
18152344Srodrigc *	This product includes software developed by the University of
19152344Srodrigc *	California, Berkeley and its contributors.
20152344Srodrigc * 4. Neither the name of the University nor the names of its contributors
21152344Srodrigc *    may be used to endorse or promote products derived from this software
22152344Srodrigc *    without specific prior written permission.
23152344Srodrigc *
24152344Srodrigc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25152344Srodrigc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26152344Srodrigc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27152344Srodrigc * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28152344Srodrigc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29152344Srodrigc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30152344Srodrigc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31152344Srodrigc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32152344Srodrigc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33152344Srodrigc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34152344Srodrigc * SUCH DAMAGE.
35152344Srodrigc */
36152344Srodrigc
37152344Srodrigc#ifndef lint
38152344Srodrigcstatic const char copyright[] =
39152344Srodrigc"@(#) Copyright (c) 1992, 1993, 1994\n\
40152344Srodrigc	The Regents of the University of California.  All rights reserved.\n";
41152344Srodrigc#endif /* not lint */
42152344Srodrigc
43152344Srodrigc#ifndef lint
44152344Srodrigc#if 0
45152344Srodrigcstatic char sccsid[] = "@(#)mount_fs.c	8.6 (Berkeley) 4/26/95";
46152344Srodrigc#endif
47152344Srodrigcstatic const char rcsid[] =
48152344Srodrigc	"$FreeBSD: head/sbin/mount/mount_fs.c 164977 2006-12-07 03:24:43Z rodrigc $";
49152344Srodrigc#endif /* not lint */
50152344Srodrigc
51152344Srodrigc#include <sys/param.h>
52152344Srodrigc#include <sys/mount.h>
53152344Srodrigc
54152344Srodrigc#include <err.h>
55152344Srodrigc#include <getopt.h>
56152344Srodrigc#include <libgen.h>
57152344Srodrigc#include <stdio.h>
58152344Srodrigc#include <stdlib.h>
59152344Srodrigc#include <string.h>
60152344Srodrigc#include <unistd.h>
61152344Srodrigc
62152344Srodrigc#include "extern.h"
63152344Srodrigc#include "mntopts.h"
64152344Srodrigc
65152344Srodrigcstruct mntopt mopts[] = {
66152344Srodrigc	MOPT_STDOPTS,
67152344Srodrigc	MOPT_END
68152344Srodrigc};
69152344Srodrigc
70152344Srodrigcstatic void
71152344Srodrigcusage(void)
72152344Srodrigc{
73152344Srodrigc	(void)fprintf(stderr,
74152344Srodrigc		"usage: mount [-t fstype] [-o options] target_fs mount_point\n");
75152344Srodrigc	exit(1);
76152344Srodrigc}
77152344Srodrigc
78152344Srodrigcint
79152344Srodrigcmount_fs(const char *vfstype, int argc, char *argv[])
80152344Srodrigc{
81152344Srodrigc	struct iovec *iov;
82152344Srodrigc	int iovlen;
83152344Srodrigc	int mntflags = 0;
84152344Srodrigc	int ch;
85152344Srodrigc	char *dev, *dir, mntpath[MAXPATHLEN];
86152344Srodrigc	char fstype[32];
87164977Srodrigc	char errmsg[255];
88152344Srodrigc	char *p, *val;
89152344Srodrigc	int ret;
90152344Srodrigc
91152344Srodrigc	strncpy(fstype, vfstype, sizeof(fstype));
92164977Srodrigc	memset(errmsg, 0, sizeof(errmsg));
93152344Srodrigc
94152344Srodrigc	getmnt_silent = 1;
95152344Srodrigc	iov = NULL;
96152344Srodrigc	iovlen = 0;
97152344Srodrigc
98152344Srodrigc	optind = optreset = 1;		/* Reset for parse of new argv. */
99152344Srodrigc	while ((ch = getopt(argc, argv, "o:")) != -1) {
100152344Srodrigc		switch(ch) {
101152344Srodrigc		case 'o':
102152344Srodrigc			getmntopts(optarg, mopts, &mntflags, 0);
103152344Srodrigc			p = strchr(optarg, '=');
104152344Srodrigc			val = NULL;
105152344Srodrigc			if (p != NULL) {
106152344Srodrigc				*p = '\0';
107152344Srodrigc				val = p + 1;
108152344Srodrigc			}
109152354Srodrigc			build_iovec(&iov, &iovlen, optarg, val, (size_t)-1);
110152344Srodrigc			break;
111152344Srodrigc		case '?':
112152344Srodrigc		default:
113152344Srodrigc			usage();
114152344Srodrigc		}
115152344Srodrigc	}
116152344Srodrigc
117152344Srodrigc	argc -= optind;
118152344Srodrigc	argv += optind;
119152344Srodrigc	if (argc != 2)
120152344Srodrigc		usage();
121152344Srodrigc
122152344Srodrigc	dev = argv[0];
123152344Srodrigc	dir = argv[1];
124152344Srodrigc
125152344Srodrigc	(void)checkpath(dir, mntpath);
126152344Srodrigc	(void)rmslashes(dev, dev);
127152344Srodrigc
128152354Srodrigc	build_iovec(&iov, &iovlen, "fstype", fstype, (size_t)-1);
129152354Srodrigc	build_iovec(&iov, &iovlen, "fspath", mntpath, (size_t)-1);
130152354Srodrigc	build_iovec(&iov, &iovlen, "from", dev, (size_t)-1);
131164977Srodrigc	build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
132152344Srodrigc
133152344Srodrigc	ret = nmount(iov, iovlen, mntflags);
134152344Srodrigc	if (ret < 0)
135164977Srodrigc		err(1, "%s %s", dev, errmsg);
136152344Srodrigc
137152344Srodrigc	return (ret);
138152344Srodrigc}
139