mount_fs.c revision 231596
1202006Smarius/*
2202006Smarius * Copyright (c) 1992, 1993, 1994
3202006Smarius *	The Regents of the University of California.  All rights reserved.
4202006Smarius *
5202006Smarius * This code is derived from software donated to Berkeley by
6202006Smarius * Jan-Simon Pendry.
7202006Smarius *
8202006Smarius * Redistribution and use in source and binary forms, with or without
9202006Smarius * modification, are permitted provided that the following conditions
10202006Smarius * are met:
11202006Smarius * 1. Redistributions of source code must retain the above copyright
12202006Smarius *    notice, this list of conditions and the following disclaimer.
13202006Smarius * 2. Redistributions in binary form must reproduce the above copyright
14202006Smarius *    notice, this list of conditions and the following disclaimer in the
15202006Smarius *    documentation and/or other materials provided with the distribution.
16202006Smarius * 4. Neither the name of the University nor the names of its contributors
17202006Smarius *    may be used to endorse or promote products derived from this software
18202006Smarius *    without specific prior written permission.
19202006Smarius *
20202006Smarius * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21202006Smarius * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22202006Smarius * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23202006Smarius * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24202006Smarius * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25202006Smarius * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26202006Smarius * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27202006Smarius * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28202006Smarius * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29202006Smarius * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30202006Smarius * SUCH DAMAGE.
31202006Smarius */
32202006Smarius
33202006Smarius#ifndef lint
34202006Smariusstatic const char copyright[] =
35202006Smarius"@(#) Copyright (c) 1992, 1993, 1994\n\
36202006Smarius	The Regents of the University of California.  All rights reserved.\n";
37202006Smarius#endif /* not lint */
38202006Smarius
39202006Smarius#ifndef lint
40202006Smarius#if 0
41202006Smariusstatic char sccsid[] = "@(#)mount_fs.c	8.6 (Berkeley) 4/26/95";
42202006Smarius#endif
43202006Smariusstatic const char rcsid[] =
44202006Smarius	"$FreeBSD: stable/9/sbin/mount/mount_fs.c 231596 2012-02-13 19:02:11Z jh $";
45202006Smarius#endif /* not lint */
46202006Smarius
47202006Smarius#include <sys/param.h>
48202006Smarius#include <sys/mount.h>
49202006Smarius
50202006Smarius#include <err.h>
51202006Smarius#include <getopt.h>
52202006Smarius#include <libgen.h>
53202006Smarius#include <stdio.h>
54202006Smarius#include <stdlib.h>
55202006Smarius#include <string.h>
56202006Smarius#include <unistd.h>
57202006Smarius
58202006Smarius#include "extern.h"
59202006Smarius#include "mntopts.h"
60202006Smarius
61202006Smariusstruct mntopt mopts[] = {
62202006Smarius	MOPT_STDOPTS,
63202006Smarius	MOPT_END
64202006Smarius};
65202006Smarius
66202006Smariusstatic void
67202006Smariususage(void)
68202006Smarius{
69202006Smarius	(void)fprintf(stderr,
70202006Smarius		"usage: mount [-t fstype] [-o options] target_fs mount_point\n");
71202006Smarius	exit(1);
72202006Smarius}
73202006Smarius
74202006Smariusint
75202006Smariusmount_fs(const char *vfstype, int argc, char *argv[])
76202006Smarius{
77202006Smarius	struct iovec *iov;
78202006Smarius	int iovlen;
79202006Smarius	int mntflags = 0;
80202006Smarius	int ch;
81202006Smarius	char *dev, *dir, mntpath[MAXPATHLEN];
82202006Smarius	char fstype[32];
83202006Smarius	char errmsg[255];
84202006Smarius	char *p, *val;
85202006Smarius
86202006Smarius	strlcpy(fstype, vfstype, sizeof(fstype));
87202006Smarius	memset(errmsg, 0, sizeof(errmsg));
88202006Smarius
89202006Smarius	getmnt_silent = 1;
90202006Smarius	iov = NULL;
91202006Smarius	iovlen = 0;
92202006Smarius
93202006Smarius	optind = optreset = 1;		/* Reset for parse of new argv. */
94202006Smarius	while ((ch = getopt(argc, argv, "o:")) != -1) {
95202006Smarius		switch(ch) {
96202006Smarius		case 'o':
97202006Smarius			getmntopts(optarg, mopts, &mntflags, 0);
98202006Smarius			p = strchr(optarg, '=');
99202006Smarius			val = NULL;
100202006Smarius			if (p != NULL) {
101202006Smarius				*p = '\0';
102202006Smarius				val = p + 1;
103202006Smarius			}
104202006Smarius			build_iovec(&iov, &iovlen, optarg, val, (size_t)-1);
105202006Smarius			break;
106202006Smarius		case '?':
107202006Smarius		default:
108202006Smarius			usage();
109202006Smarius		}
110202006Smarius	}
111202006Smarius
112202006Smarius	argc -= optind;
113202006Smarius	argv += optind;
114202006Smarius	if (argc != 2)
115202006Smarius		usage();
116202006Smarius
117202006Smarius	dev = argv[0];
118202006Smarius	dir = argv[1];
119202006Smarius
120202006Smarius	(void)checkpath(dir, mntpath);
121202006Smarius	(void)rmslashes(dev, dev);
122202006Smarius
123202006Smarius	build_iovec(&iov, &iovlen, "fstype", fstype, (size_t)-1);
124202006Smarius	build_iovec(&iov, &iovlen, "fspath", mntpath, (size_t)-1);
125202006Smarius	build_iovec(&iov, &iovlen, "from", dev, (size_t)-1);
126202006Smarius	build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
127202006Smarius
128227848Smarius	if (nmount(iov, iovlen, mntflags) == -1) {
129202006Smarius		if (*errmsg != '\0')
130202006Smarius			warn("%s: %s", dev, errmsg);
131202006Smarius		else
132202006Smarius			warn("%s", dev);
133202006Smarius		return (1);
134202006Smarius	}
135202006Smarius	return (0);
136202006Smarius}
137202006Smarius