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