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