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