xref: /onnv-gate/usr/src/cmd/fs.d/mntfs/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 2003 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	<errno.h>
37*0Sstevel@tonic-gate #include	<sys/mnttab.h>
38*0Sstevel@tonic-gate #include	<sys/mount.h>
39*0Sstevel@tonic-gate #include	<sys/types.h>
40*0Sstevel@tonic-gate #include 	<locale.h>
41*0Sstevel@tonic-gate #include	<sys/stat.h>
42*0Sstevel@tonic-gate #include	<string.h>
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate #include	<fslib.h>
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate #define	NAME_MAX	64	/* sizeof "fstype myname" */
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate #define	FSTYPE		"mntfs"
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate static void do_mount(char *, char *, int);
51*0Sstevel@tonic-gate static void usage(void);
52*0Sstevel@tonic-gate static void rpterr(char *, char *);
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate static char  optbuf[MAX_MNTOPT_STR] = { '\0', };
55*0Sstevel@tonic-gate static int   optsize = 0;
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate static int	flags = 0;
58*0Sstevel@tonic-gate static int	mflg = 0;	/* don't update /etc/mnttab flag */
59*0Sstevel@tonic-gate static int	Oflg = 0;
60*0Sstevel@tonic-gate static int	qflg = 0;
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate static char	typename[NAME_MAX], *myname;
63*0Sstevel@tonic-gate static char	fstype[] = FSTYPE;
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate int
main(int argc,char * argv[])66*0Sstevel@tonic-gate main(int argc, char *argv[])
67*0Sstevel@tonic-gate {
68*0Sstevel@tonic-gate 	char	*special, *mountp;
69*0Sstevel@tonic-gate 	int	errflag = 0;
70*0Sstevel@tonic-gate 	int	cc;
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
75*0Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
76*0Sstevel@tonic-gate #endif
77*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate 	myname = strrchr(argv[0], '/');
80*0Sstevel@tonic-gate 	if (myname)
81*0Sstevel@tonic-gate 		myname++;
82*0Sstevel@tonic-gate 	else
83*0Sstevel@tonic-gate 		myname = argv[0];
84*0Sstevel@tonic-gate 	(void) snprintf(typename, sizeof (typename), "%s %s", fstype, myname);
85*0Sstevel@tonic-gate 	argv[0] = typename;
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 	/*
88*0Sstevel@tonic-gate 	 *	check for proper arguments
89*0Sstevel@tonic-gate 	 */
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	while ((cc = getopt(argc, argv, "?rmOo:q")) != -1)
92*0Sstevel@tonic-gate 		switch (cc) {
93*0Sstevel@tonic-gate 		case 'm':
94*0Sstevel@tonic-gate 			mflg++;
95*0Sstevel@tonic-gate 			break;
96*0Sstevel@tonic-gate 		case 'r':
97*0Sstevel@tonic-gate 			flags |= MS_RDONLY;
98*0Sstevel@tonic-gate 			break;
99*0Sstevel@tonic-gate 		case 'O':
100*0Sstevel@tonic-gate 			Oflg++;
101*0Sstevel@tonic-gate 			break;
102*0Sstevel@tonic-gate 		case 'o':
103*0Sstevel@tonic-gate 			if (strlcpy(optbuf, optarg, sizeof (optbuf)) >=
104*0Sstevel@tonic-gate 			    sizeof (optbuf)) {
105*0Sstevel@tonic-gate 				(void) fprintf(stderr,
106*0Sstevel@tonic-gate 				    gettext("%s: Invalid argument: %s\n"),
107*0Sstevel@tonic-gate 				    myname, optarg);
108*0Sstevel@tonic-gate 				return (2);
109*0Sstevel@tonic-gate 			}
110*0Sstevel@tonic-gate 			optsize = strlen(optbuf);
111*0Sstevel@tonic-gate 			break;
112*0Sstevel@tonic-gate 		case 'q':
113*0Sstevel@tonic-gate 			qflg++;
114*0Sstevel@tonic-gate 			break;
115*0Sstevel@tonic-gate 		case '?':
116*0Sstevel@tonic-gate 		default:
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 	 */
135*0Sstevel@tonic-gate 	if (mflg)
136*0Sstevel@tonic-gate 		flags |= MS_NOMNTTAB;
137*0Sstevel@tonic-gate 	do_mount(special, mountp, flags);
138*0Sstevel@tonic-gate 	return (0);
139*0Sstevel@tonic-gate }
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate static void
rpterr(char * bs,char * mp)142*0Sstevel@tonic-gate rpterr(char *bs, char *mp)
143*0Sstevel@tonic-gate {
144*0Sstevel@tonic-gate 	switch (errno) {
145*0Sstevel@tonic-gate 	case EPERM:
146*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: insufficient privileges\n"),
147*0Sstevel@tonic-gate 		    myname);
148*0Sstevel@tonic-gate 		break;
149*0Sstevel@tonic-gate 	case ENXIO:
150*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: %s no such device\n"),
151*0Sstevel@tonic-gate 		    myname, bs);
152*0Sstevel@tonic-gate 		break;
153*0Sstevel@tonic-gate 	case ENOTDIR:
154*0Sstevel@tonic-gate 		(void) fprintf(stderr,
155*0Sstevel@tonic-gate gettext("%s: %s not a directory\n\tor a component of %s is not a directory\n"),
156*0Sstevel@tonic-gate 			myname, mp, bs);
157*0Sstevel@tonic-gate 		break;
158*0Sstevel@tonic-gate 	case ENOENT:
159*0Sstevel@tonic-gate 		(void) fprintf(stderr,
160*0Sstevel@tonic-gate 			gettext("%s: %s or %s, no such file or directory\n"),
161*0Sstevel@tonic-gate 			myname, bs, mp);
162*0Sstevel@tonic-gate 		break;
163*0Sstevel@tonic-gate 	case EINVAL:
164*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: %s is not this fstype.\n"),
165*0Sstevel@tonic-gate 			myname, bs);
166*0Sstevel@tonic-gate 		break;
167*0Sstevel@tonic-gate 	case EBUSY:
168*0Sstevel@tonic-gate 		(void) fprintf(stderr,
169*0Sstevel@tonic-gate 			gettext("%s: %s is already mounted or %s is busy\n"),
170*0Sstevel@tonic-gate 			myname, bs, mp);
171*0Sstevel@tonic-gate 		break;
172*0Sstevel@tonic-gate 	case EROFS:
173*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: %s write-protected\n"),
174*0Sstevel@tonic-gate 				myname, bs);
175*0Sstevel@tonic-gate 		break;
176*0Sstevel@tonic-gate 	case ENOSPC:
177*0Sstevel@tonic-gate 		(void) fprintf(stderr,
178*0Sstevel@tonic-gate 			gettext("%s: the state of %s is not okay\n"
179*0Sstevel@tonic-gate 			"\tand it was attempted to mount read/write\n"),
180*0Sstevel@tonic-gate 			myname, bs);
181*0Sstevel@tonic-gate 		break;
182*0Sstevel@tonic-gate 	default:
183*0Sstevel@tonic-gate 		perror(myname);
184*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: cannot mount %s\n"),
185*0Sstevel@tonic-gate 		    myname, bs);
186*0Sstevel@tonic-gate 	}
187*0Sstevel@tonic-gate }
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate static void
do_mount(char * special,char * mountp,int rflag)190*0Sstevel@tonic-gate do_mount(char *special, char *mountp, int rflag)
191*0Sstevel@tonic-gate {
192*0Sstevel@tonic-gate 	char *savedoptbuf;
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 	if ((savedoptbuf = strdup(optbuf)) == NULL) {
195*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: out of memory\n"),
196*0Sstevel@tonic-gate 		    myname);
197*0Sstevel@tonic-gate 		exit(1);
198*0Sstevel@tonic-gate 	}
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 	rflag |= Oflg ? MS_OVERLAY : 0;
201*0Sstevel@tonic-gate 	if (mount(special, mountp, rflag | MS_OPTIONSTR, fstype, NULL, 0,
202*0Sstevel@tonic-gate 	    optbuf, MAX_MNTOPT_STR)) {
203*0Sstevel@tonic-gate 		rpterr(special, mountp);
204*0Sstevel@tonic-gate 		exit(1);
205*0Sstevel@tonic-gate 	}
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate 	if (optsize && !qflg)
208*0Sstevel@tonic-gate 		cmp_requested_to_actual_options(savedoptbuf, optbuf,
209*0Sstevel@tonic-gate 		    special, mountp);
210*0Sstevel@tonic-gate }
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate static void
usage(void)213*0Sstevel@tonic-gate usage(void)
214*0Sstevel@tonic-gate {
215*0Sstevel@tonic-gate 	(void) fprintf(stderr,
216*0Sstevel@tonic-gate 		gettext("%s usage:\n%s [-F %s] [-r] [-o specific_options] "
217*0Sstevel@tonic-gate 		"{special | mount_point}\n%s [-F %s] [-r] [-o specific_options]"
218*0Sstevel@tonic-gate 		" special mount_point\n"),
219*0Sstevel@tonic-gate 		fstype, myname, fstype, myname, fstype);
220*0Sstevel@tonic-gate 	exit(2);
221*0Sstevel@tonic-gate }
222