xref: /titanic_41/usr/src/cmd/fs.d/hsfs/mount/mount.c (revision 70025d765b044c6d8594bb965a2247a61e991a99)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <ctype.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include <unistd.h>	/* defines F_LOCK for lockf */
33 #include <stdlib.h>	/* for getopt(3) */
34 #include <signal.h>
35 #include <locale.h>
36 #include <fslib.h>
37 #include <sys/types.h>
38 #include <errno.h>
39 #include <sys/mnttab.h>
40 #include <sys/mntent.h>
41 #include <sys/mount.h>
42 #include <sys/vfs.h>
43 #include <sys/fs/hsfs_susp.h>
44 #include <sys/fs/hsfs_rrip.h>
45 
46 extern int optind;
47 extern char *optarg;
48 
49 #define	NAME_MAX	64
50 #define	GLOBAL		0
51 #define	NOGLOBAL	1
52 
53 #ifndef	MNTOPT_NOGLOBAL
54 #define	MNTOPT_NOGLOBAL	"noglobal"
55 #endif	/* MNTOPT_NOGLOBAL */
56 
57 static int gflg		= 0;	/* mount into global name space: flag form */
58 static int global	= 0;	/* mount into global name space: option form */
59 static int havegblopt	= 0;	/* global value supercedes gflg value */
60 static int qflg		= 0;	/* quiet option - don't flag bad options */
61 
62 static char fstype[] = MNTTYPE_HSFS;
63 
64 static char typename[NAME_MAX], *myname;
65 /*
66  * Mount options that require special handling
67  */
68 static char *myopts[] = {
69 	MNTOPT_GLOBAL,
70 	MNTOPT_NOGLOBAL,
71 	NULL
72 };
73 
74 
75 static void rpterr(char *, char *);
76 static void usage(void);
77 
78 int
79 main(int argc, char **argv)
80 {
81 	char *options, *value;
82 	char *special, *mountp;
83 	char *gopt;
84 	struct mnttab mm;
85 	int c;
86 	char	obuff[MAX_MNTOPT_STR];
87 	char	saved_input_options[MAX_MNTOPT_STR];
88 	int hsfs_flags;
89 
90 	int flags;
91 	int Oflg = 0;   /* Overlay mounts */
92 
93 	(void) setlocale(LC_ALL, "");
94 
95 #if !defined(TEXT_DOMAIN)
96 #define	TEXT_DOMAIN "SYS_TEST"
97 #endif
98 	(void) textdomain(TEXT_DOMAIN);
99 
100 	myname = strrchr(argv[0], '/');
101 	if (myname)
102 		myname++;
103 	else
104 		myname = argv[0];
105 	snprintf(typename, sizeof (typename), "%s %s", fstype, myname);
106 	argv[0] = typename;
107 
108 	/*
109 	 * Check for arguments requiring special handling.  Ignore
110 	 * unrecognized options.
111 	 */
112 	strcpy(obuff, "ro");	/* default */
113 	while ((c = getopt(argc, argv, "o:rmOgq")) != EOF) {
114 		switch (c) {
115 			case 'o':
116 				if (strlen(optarg) > MAX_MNTOPT_STR) {
117 					(void) fprintf(stderr, gettext(
118 					    "%s: option set too long\n"),
119 					    myname);
120 					exit(1);
121 				}
122 				if (strlen(optarg) == 0) {
123 					(void) fprintf(stderr, gettext(
124 					    "%s: missing suboptions\n"),
125 					    myname);
126 					exit(1);
127 				}
128 				strcpy(obuff, optarg);
129 				options = optarg;
130 				while (*options != '\0') {
131 					switch (getsubopt(&options, myopts,
132 					    &value)) {
133 					case GLOBAL:
134 						havegblopt = 1;
135 						global = 1;
136 						break;
137 					case NOGLOBAL:
138 						havegblopt = 1;
139 						global = 0;
140 						break;
141 					}
142 				}
143 				break;
144 			case 'O':
145 				Oflg++;
146 				break;
147 			case 'r':
148 				/* accept for backwards compatibility */
149 				break;
150 			case 'm':
151 				break;
152 			case 'g':
153 				gflg++;
154 				break;
155 			case 'q':
156 				qflg++;
157 				break;
158 
159 		}
160 	}
161 
162 	if ((argc - optind) != 2)
163 		usage();
164 
165 	special = argv[optind++];
166 	mountp = argv[optind++];
167 
168 	/*
169 	 * Force readonly.  obuff is guaranteed to have something in
170 	 * it.  We might end up with "ro,ro", but that's acceptable.
171 	 */
172 	flags = MS_RDONLY;
173 
174 	if ((strlen(obuff) + strlen(MNTOPT_RO) + 2) > MAX_MNTOPT_STR) {
175 		(void) fprintf(stderr, gettext("%s: option set too long\n"),
176 		    myname);
177 		exit(1);
178 	}
179 
180 	strcat(obuff, ",");
181 	strcat(obuff, MNTOPT_RO);
182 
183 	flags |= Oflg ? MS_OVERLAY : 0;
184 
185 	/*
186 	 * xxx it's not clear if should just put MS_GLOBAL in flags,
187 	 * or provide it as a string.  Be safe, do both.  The subopt
188 	 * version has precedence over the switch version.
189 	 */
190 	gopt = NULL;
191 	if ((havegblopt && global) || gflg) {
192 		gopt = MNTOPT_GLOBAL;
193 		flags |= MS_GLOBAL;
194 	} else if (havegblopt) {
195 		gopt = MNTOPT_NOGLOBAL;
196 	}
197 
198 	if (gopt != NULL) {
199 		if ((strlen(obuff) + strlen(gopt) + 2) > MAX_MNTOPT_STR) {
200 			(void) fprintf(stderr,
201 			    gettext("%s: option set too long\n"), myname);
202 			exit(1);
203 		}
204 
205 		strcat(obuff, ",");
206 		strcat(obuff, gopt);
207 	}
208 
209 	signal(SIGHUP,  SIG_IGN);
210 	signal(SIGQUIT, SIG_IGN);
211 	signal(SIGINT,  SIG_IGN);
212 
213 	/*
214 	 * Save a copy of the options to compare with the options that
215 	 * were actually recognized and supported by the kernel.
216 	 */
217 
218 	(void) strcpy(saved_input_options, obuff);
219 
220 	/*
221 	 * Perform the mount.
222 	 */
223 
224 	if (mount(special, mountp, flags | MS_OPTIONSTR, fstype, NULL, 0,
225 		obuff, sizeof (obuff)) == -1) {
226 		rpterr(special, mountp);
227 		exit(31+2);
228 	}
229 
230 	if (!qflg) {
231 		cmp_requested_to_actual_options(saved_input_options, obuff,
232 			special, mountp);
233 	}
234 
235 	exit(0);
236 	/* NOTREACHED */
237 }
238 
239 
240 static void
241 rpterr(char *bs, char *mp)
242 {
243 	switch (errno) {
244 	case EPERM:
245 		(void) fprintf(stderr, gettext("%s: insufficient privileges\n"),
246 		    myname);
247 		break;
248 	case ENXIO:
249 		(void) fprintf(stderr, gettext("%s: %s no such device\n"),
250 		    myname, bs);
251 		break;
252 	case ENOTDIR:
253 		(void) fprintf(stderr, gettext("%s: %s not a directory\n\tor a "
254 		    "component of %s is not a directory\n"), myname, mp, bs);
255 		break;
256 	case ENOENT:
257 		(void) fprintf(stderr,
258 		    gettext("%s: %s or %s, no such file or directory\n"),
259 		    myname, bs, mp);
260 		break;
261 	case EINVAL:
262 		(void) fprintf(stderr, gettext("%s: %s is not an hsfs file "
263 		    "system.\n"), typename, bs);
264 		break;
265 	case EBUSY:
266 		(void) fprintf(stderr,
267 		    gettext("%s: %s is already mounted or %s is busy\n"),
268 		    myname, bs, mp);
269 		break;
270 	case ENOTBLK:
271 		(void) fprintf(stderr,
272 		    gettext("%s: %s not a block device\n"), myname, bs);
273 		break;
274 	case EROFS:
275 		(void) fprintf(stderr, gettext("%s: %s write-protected\n"),
276 		    myname, bs);
277 		break;
278 	case ENOSPC:
279 		(void) fprintf(stderr,
280 		    gettext("%s: %s is corrupted. needs checking\n"),
281 		    myname, bs);
282 		break;
283 	default:
284 		perror(myname);
285 		(void) fprintf(stderr, gettext("%s: cannot mount %s\n"), myname,
286 		    bs);
287 	}
288 }
289 
290 
291 static void
292 usage()
293 {
294 	char *opts;
295 
296 opts = "{-r | -o ro | -o nrr | -o nosuid | -o notraildot | -o nomaplcase}";
297 	(void) fprintf(stdout,
298 gettext("hsfs usage: mount [-F hsfs] %s {special | mount_point}\n"), opts);
299 	(void) fprintf(stdout,
300 gettext("hsfs usage: mount [-F hsfs] %s special mount_point\n"), opts);
301 	exit(32);
302 }
303