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