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