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