xref: /titanic_52/usr/src/cmd/fs.d/fd/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 2004 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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include	<stdio.h>
34*7c478bd9Sstevel@tonic-gate #include	<stdlib.h>
35*7c478bd9Sstevel@tonic-gate #include	<signal.h>
36*7c478bd9Sstevel@tonic-gate #include	<string.h>
37*7c478bd9Sstevel@tonic-gate #include	<unistd.h>
38*7c478bd9Sstevel@tonic-gate #include	<errno.h>
39*7c478bd9Sstevel@tonic-gate #include	<sys/mnttab.h>
40*7c478bd9Sstevel@tonic-gate #include	<sys/mount.h>
41*7c478bd9Sstevel@tonic-gate #include	<sys/types.h>
42*7c478bd9Sstevel@tonic-gate #include	<locale.h>
43*7c478bd9Sstevel@tonic-gate #include	<fslib.h>
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate #define	NAME_MAX	64	/* sizeof "fstype myname" */
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate #define	FSTYPE		"fd"
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate static char  optbuf[MAX_MNTOPT_STR] = { '\0', };
50*7c478bd9Sstevel@tonic-gate static int   optsize = 0;
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate static int	flags = 0;
53*7c478bd9Sstevel@tonic-gate static int	mflg = 0;
54*7c478bd9Sstevel@tonic-gate static int	qflg = 0;
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate static char	typename[NAME_MAX], *myname;
57*7c478bd9Sstevel@tonic-gate static char	fstype[] = FSTYPE;
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate static void usage(void);
60*7c478bd9Sstevel@tonic-gate static void do_mount(char *, char *, int);
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate int
63*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
64*7c478bd9Sstevel@tonic-gate {
65*7c478bd9Sstevel@tonic-gate 	char	*special, *mountp;
66*7c478bd9Sstevel@tonic-gate 	int	errflag = 0;
67*7c478bd9Sstevel@tonic-gate 	int	cc;
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
72*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
73*7c478bd9Sstevel@tonic-gate #endif
74*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 	myname = strrchr(argv[0], '/');
77*7c478bd9Sstevel@tonic-gate 	if (myname)
78*7c478bd9Sstevel@tonic-gate 		myname++;
79*7c478bd9Sstevel@tonic-gate 	else
80*7c478bd9Sstevel@tonic-gate 		myname = argv[0];
81*7c478bd9Sstevel@tonic-gate 	(void) snprintf(typename, sizeof (typename), "%s %s", fstype, myname);
82*7c478bd9Sstevel@tonic-gate 	argv[0] = typename;
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	/*
85*7c478bd9Sstevel@tonic-gate 	 *	check for proper arguments
86*7c478bd9Sstevel@tonic-gate 	 */
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate 	while ((cc = getopt(argc, argv, "o:rmOq")) != -1)
89*7c478bd9Sstevel@tonic-gate 		switch (cc) {
90*7c478bd9Sstevel@tonic-gate 		case 'r':
91*7c478bd9Sstevel@tonic-gate 			if (flags & MS_RDONLY)
92*7c478bd9Sstevel@tonic-gate 				errflag = 1;
93*7c478bd9Sstevel@tonic-gate 			else
94*7c478bd9Sstevel@tonic-gate 				flags |= MS_RDONLY;
95*7c478bd9Sstevel@tonic-gate 			break;
96*7c478bd9Sstevel@tonic-gate 		case 'O':
97*7c478bd9Sstevel@tonic-gate 			flags |= MS_OVERLAY;
98*7c478bd9Sstevel@tonic-gate 			break;
99*7c478bd9Sstevel@tonic-gate 		case 'q':
100*7c478bd9Sstevel@tonic-gate 			qflg = 1;
101*7c478bd9Sstevel@tonic-gate 			break;
102*7c478bd9Sstevel@tonic-gate 		case 'm':
103*7c478bd9Sstevel@tonic-gate 			mflg++;
104*7c478bd9Sstevel@tonic-gate 			break;
105*7c478bd9Sstevel@tonic-gate 		case 'o':
106*7c478bd9Sstevel@tonic-gate 			if (strlcpy(optbuf, optarg, sizeof (optbuf)) >=
107*7c478bd9Sstevel@tonic-gate 			    sizeof (optbuf)) {
108*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
109*7c478bd9Sstevel@tonic-gate 				    gettext("%s: Invalid argument: %s\n"),
110*7c478bd9Sstevel@tonic-gate 				    myname, optarg);
111*7c478bd9Sstevel@tonic-gate 				return (2);
112*7c478bd9Sstevel@tonic-gate 			}
113*7c478bd9Sstevel@tonic-gate 			optsize = strlen(optbuf);
114*7c478bd9Sstevel@tonic-gate 			break;
115*7c478bd9Sstevel@tonic-gate 		default:
116*7c478bd9Sstevel@tonic-gate 		case '?':
117*7c478bd9Sstevel@tonic-gate 			errflag = 1;
118*7c478bd9Sstevel@tonic-gate 			break;
119*7c478bd9Sstevel@tonic-gate 		}
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 	/*
122*7c478bd9Sstevel@tonic-gate 	 *	There must be at least 2 more arguments, the
123*7c478bd9Sstevel@tonic-gate 	 *	special file and the directory.
124*7c478bd9Sstevel@tonic-gate 	 */
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	if (((argc - optind) != 2) || (errflag))
127*7c478bd9Sstevel@tonic-gate 		usage();
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 	special = argv[optind++];
130*7c478bd9Sstevel@tonic-gate 	mountp = argv[optind++];
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 	/*
133*7c478bd9Sstevel@tonic-gate 	 *	Perform the mount.
134*7c478bd9Sstevel@tonic-gate 	 *	Only the low-order bit of "flags" is used by the system
135*7c478bd9Sstevel@tonic-gate 	 *	calls (to denote read-only or read-write).
136*7c478bd9Sstevel@tonic-gate 	 */
137*7c478bd9Sstevel@tonic-gate 	if (mflg)
138*7c478bd9Sstevel@tonic-gate 		flags |= MS_NOMNTTAB;
139*7c478bd9Sstevel@tonic-gate 	do_mount(special, mountp, flags);
140*7c478bd9Sstevel@tonic-gate 	exit(0);
141*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
142*7c478bd9Sstevel@tonic-gate }
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate static void
145*7c478bd9Sstevel@tonic-gate rpterr(char *bs, char *mp)
146*7c478bd9Sstevel@tonic-gate {
147*7c478bd9Sstevel@tonic-gate 	switch (errno) {
148*7c478bd9Sstevel@tonic-gate 	case EPERM:
149*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
150*7c478bd9Sstevel@tonic-gate 		    gettext("%s: insufficient privileges\n"), myname);
151*7c478bd9Sstevel@tonic-gate 		break;
152*7c478bd9Sstevel@tonic-gate 	case ENXIO:
153*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
154*7c478bd9Sstevel@tonic-gate 			gettext("%s: %s no such device\n"), myname, bs);
155*7c478bd9Sstevel@tonic-gate 		break;
156*7c478bd9Sstevel@tonic-gate 	case ENOTDIR:
157*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
158*7c478bd9Sstevel@tonic-gate 		    gettext("%s: %s not a directory\n"
159*7c478bd9Sstevel@tonic-gate 		    "\tor a component of %s is not a directory\n"),
160*7c478bd9Sstevel@tonic-gate 			myname, mp, bs);
161*7c478bd9Sstevel@tonic-gate 		break;
162*7c478bd9Sstevel@tonic-gate 	case ENOENT:
163*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
164*7c478bd9Sstevel@tonic-gate 			gettext("%s: %s or %s, no such file or directory\n"),
165*7c478bd9Sstevel@tonic-gate 			myname, bs, mp);
166*7c478bd9Sstevel@tonic-gate 		break;
167*7c478bd9Sstevel@tonic-gate 	case EINVAL:
168*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: %s is not this fstype.\n"),
169*7c478bd9Sstevel@tonic-gate 			myname, bs);
170*7c478bd9Sstevel@tonic-gate 		break;
171*7c478bd9Sstevel@tonic-gate 	case EBUSY:
172*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
173*7c478bd9Sstevel@tonic-gate 			gettext("%s: %s is already mounted or %s is busy\n"),
174*7c478bd9Sstevel@tonic-gate 			myname, bs, mp);
175*7c478bd9Sstevel@tonic-gate 		break;
176*7c478bd9Sstevel@tonic-gate 	case ENOTBLK:
177*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: %s not a block device\n"),
178*7c478bd9Sstevel@tonic-gate 			myname, bs);
179*7c478bd9Sstevel@tonic-gate 		break;
180*7c478bd9Sstevel@tonic-gate 	case EROFS:
181*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
182*7c478bd9Sstevel@tonic-gate 			gettext("%s: %s write-protected\n"), myname, bs);
183*7c478bd9Sstevel@tonic-gate 		break;
184*7c478bd9Sstevel@tonic-gate 	case ENOSPC:
185*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
186*7c478bd9Sstevel@tonic-gate 			gettext("%s: the state of %s is not okay\n"
187*7c478bd9Sstevel@tonic-gate 			    "\tand it was attempted to mount read/write\n"),
188*7c478bd9Sstevel@tonic-gate 			myname, bs);
189*7c478bd9Sstevel@tonic-gate 		break;
190*7c478bd9Sstevel@tonic-gate 	default:
191*7c478bd9Sstevel@tonic-gate 		perror(myname);
192*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: cannot mount %s\n"),
193*7c478bd9Sstevel@tonic-gate 		    myname, bs);
194*7c478bd9Sstevel@tonic-gate 	}
195*7c478bd9Sstevel@tonic-gate }
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate static void
199*7c478bd9Sstevel@tonic-gate do_mount(char *special, char *mountp, int rflag)
200*7c478bd9Sstevel@tonic-gate {
201*7c478bd9Sstevel@tonic-gate 	char *savedoptbuf;
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate 	if ((savedoptbuf = strdup(optbuf)) == NULL) {
204*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: out of memory\n"),
205*7c478bd9Sstevel@tonic-gate 		    myname);
206*7c478bd9Sstevel@tonic-gate 		exit(2);
207*7c478bd9Sstevel@tonic-gate 	}
208*7c478bd9Sstevel@tonic-gate 	if (mount(special, mountp, rflag | MS_OPTIONSTR,
209*7c478bd9Sstevel@tonic-gate 		fstype, NULL, 0, optbuf, MAX_MNTOPT_STR)) {
210*7c478bd9Sstevel@tonic-gate 		rpterr(special, mountp);
211*7c478bd9Sstevel@tonic-gate 		exit(2);
212*7c478bd9Sstevel@tonic-gate 	}
213*7c478bd9Sstevel@tonic-gate 	if (optsize && !qflg)
214*7c478bd9Sstevel@tonic-gate 		cmp_requested_to_actual_options(savedoptbuf, optbuf,
215*7c478bd9Sstevel@tonic-gate 		    special, mountp);
216*7c478bd9Sstevel@tonic-gate }
217*7c478bd9Sstevel@tonic-gate 
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate static void
220*7c478bd9Sstevel@tonic-gate usage(void)
221*7c478bd9Sstevel@tonic-gate {
222*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
223*7c478bd9Sstevel@tonic-gate 		gettext("Usage: %s [-rmOq] [-o specific_options]"
224*7c478bd9Sstevel@tonic-gate 		" special mount_point\n"), myname);
225*7c478bd9Sstevel@tonic-gate 	exit(1);
226*7c478bd9Sstevel@tonic-gate }
227