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