mount_fs.c revision 152354
1/*
2 * Copyright (c) 1992, 1993, 1994
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software donated to Berkeley by
6 * Jan-Simon Pendry.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#ifndef lint
38static const char copyright[] =
39"@(#) Copyright (c) 1992, 1993, 1994\n\
40	The Regents of the University of California.  All rights reserved.\n";
41#endif /* not lint */
42
43#ifndef lint
44#if 0
45static char sccsid[] = "@(#)mount_fs.c	8.6 (Berkeley) 4/26/95";
46#endif
47static const char rcsid[] =
48	"$FreeBSD: head/sbin/mount/mount_fs.c 152354 2005-11-13 01:27:57Z rodrigc $";
49#endif /* not lint */
50
51#include <sys/param.h>
52#include <sys/mount.h>
53
54#include <err.h>
55#include <getopt.h>
56#include <libgen.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60#include <unistd.h>
61
62#include "extern.h"
63#include "mntopts.h"
64
65struct mntopt mopts[] = {
66	MOPT_STDOPTS,
67	MOPT_END
68};
69
70static void
71usage(void)
72{
73	(void)fprintf(stderr,
74		"usage: mount [-t fstype] [-o options] target_fs mount_point\n");
75	exit(1);
76}
77
78int
79mount_fs(const char *vfstype, int argc, char *argv[])
80{
81	struct iovec *iov;
82	int iovlen;
83	int mntflags = 0;
84	int ch;
85	char *dev, *dir, mntpath[MAXPATHLEN];
86	char fstype[32];
87	char *p, *val;
88	int ret;
89
90	strncpy(fstype, vfstype, sizeof(fstype));
91
92	getmnt_silent = 1;
93	iov = NULL;
94	iovlen = 0;
95
96	optind = optreset = 1;		/* Reset for parse of new argv. */
97	while ((ch = getopt(argc, argv, "o:")) != -1) {
98		switch(ch) {
99		case 'o':
100			getmntopts(optarg, mopts, &mntflags, 0);
101			p = strchr(optarg, '=');
102			val = NULL;
103			if (p != NULL) {
104				*p = '\0';
105				val = p + 1;
106			}
107			build_iovec(&iov, &iovlen, optarg, val, (size_t)-1);
108			break;
109		case '?':
110		default:
111			usage();
112		}
113	}
114
115	argc -= optind;
116	argv += optind;
117	if (argc != 2)
118		usage();
119
120	dev = argv[0];
121	dir = argv[1];
122
123	(void)checkpath(dir, mntpath);
124	(void)rmslashes(dev, dev);
125
126	build_iovec(&iov, &iovlen, "fstype", fstype, (size_t)-1);
127	build_iovec(&iov, &iovlen, "fspath", mntpath, (size_t)-1);
128	build_iovec(&iov, &iovlen, "from", dev, (size_t)-1);
129
130	ret = nmount(iov, iovlen, mntflags);
131	if (ret < 0)
132		err(1, "%s", dev);
133
134	return (ret);
135}
136