xref: /freebsd/sbin/mount/mount_fs.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
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
38 static 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
45 static char sccsid[] = "@(#)mount_fs.c	8.6 (Berkeley) 4/26/95";
46 #endif
47 static const char rcsid[] =
48 	"$FreeBSD$";
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 
65 struct mntopt mopts[] = {
66 	MOPT_STDOPTS,
67 	MOPT_END
68 };
69 
70 static void
71 usage(void)
72 {
73 	(void)fprintf(stderr,
74 		"usage: mount [-t fstype] [-o options] target_fs mount_point\n");
75 	exit(1);
76 }
77 
78 int
79 mount_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 errmsg[255];
88 	char *p, *val;
89 	int ret;
90 
91 	strncpy(fstype, vfstype, sizeof(fstype));
92 	memset(errmsg, 0, sizeof(errmsg));
93 
94 	getmnt_silent = 1;
95 	iov = NULL;
96 	iovlen = 0;
97 
98 	optind = optreset = 1;		/* Reset for parse of new argv. */
99 	while ((ch = getopt(argc, argv, "o:")) != -1) {
100 		switch(ch) {
101 		case 'o':
102 			getmntopts(optarg, mopts, &mntflags, 0);
103 			p = strchr(optarg, '=');
104 			val = NULL;
105 			if (p != NULL) {
106 				*p = '\0';
107 				val = p + 1;
108 			}
109 			build_iovec(&iov, &iovlen, optarg, val, (size_t)-1);
110 			break;
111 		case '?':
112 		default:
113 			usage();
114 		}
115 	}
116 
117 	argc -= optind;
118 	argv += optind;
119 	if (argc != 2)
120 		usage();
121 
122 	dev = argv[0];
123 	dir = argv[1];
124 
125 	(void)checkpath(dir, mntpath);
126 	(void)rmslashes(dev, dev);
127 
128 	build_iovec(&iov, &iovlen, "fstype", fstype, (size_t)-1);
129 	build_iovec(&iov, &iovlen, "fspath", mntpath, (size_t)-1);
130 	build_iovec(&iov, &iovlen, "from", dev, (size_t)-1);
131 	build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
132 
133 	ret = nmount(iov, iovlen, mntflags);
134 	if (ret < 0)
135 		err(1, "%s %s", dev, errmsg);
136 
137 	return (ret);
138 }
139