xref: /illumos-gate/usr/src/cmd/fs.d/proc/mount.c (revision 7c478bd9)
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		"proc"
49 
50 static void rpterr(char *, char *);
51 static void do_mount(char *, char *, int);
52 static void usage(void);
53 
54 static char  optbuf[MAX_MNTOPT_STR] = { '\0', };
55 static int   optsize = 0;
56 
57 static int	roflag = 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, "?o:rmOq")) != -1)
92 		switch (cc) {
93 		case 'm':
94 			mflg++;
95 			break;
96 		case 'r':
97 			if (roflag)
98 				errflag = 1;
99 			else
100 				roflag++;
101 			break;
102 		case 'O':
103 			Oflg++;
104 			break;
105 		case 'o':
106 			if (strlcpy(optbuf, optarg, sizeof (optbuf)) >=
107 			    sizeof (optbuf)) {
108 				(void) fprintf(stderr,
109 				    gettext("%s: Invalid argument: %s\n"),
110 				    myname, optarg);
111 				return (2);
112 			}
113 			optsize = strlen(optbuf);
114 			break;
115 		case 'q':
116 			qflg = 1;
117 			break;
118 		case '?':
119 			errflag = 1;
120 			break;
121 		}
122 
123 	/*
124 	 *	There must be at least 2 more arguments, the
125 	 *	special file and the directory.
126 	 */
127 
128 	if (((argc - optind) != 2) || (errflag))
129 		usage();
130 
131 	special = argv[optind++];
132 	mountp = argv[optind++];
133 
134 	/*
135 	 *	Perform the mount.
136 	 */
137 	do_mount(special, mountp, roflag ? MS_RDONLY : 0);
138 
139 	return (0);
140 }
141 
142 void
143 rpterr(char *bs, char *mp)
144 {
145 	switch (errno) {
146 	case EPERM:
147 		(void) fprintf(stderr, gettext("%s: insufficient privileges\n"),
148 		    myname);
149 		break;
150 	case ENXIO:
151 		(void) fprintf(stderr, gettext("%s: %s no such device\n"),
152 		    myname, bs);
153 		break;
154 	case ENOTDIR:
155 		(void) fprintf(stderr,
156 gettext("%s: %s not a directory\n\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, gettext("%s: %s write-protected\n"),
179 				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 static void
195 do_mount(char *special, char *mountp, int rflag)
196 {
197 	char *savedoptbuf;
198 
199 	if ((savedoptbuf = strdup(optbuf)) == NULL) {
200 		(void) fprintf(stderr, gettext("%s: out of memory\n"),
201 		    myname);
202 		exit(2);
203 	}
204 
205 	if (Oflg)
206 		rflag |= MS_OVERLAY;
207 	if (mflg)
208 		rflag |= MS_NOMNTTAB;
209 	if (mount(special, mountp, rflag | MS_OPTIONSTR, fstype, NULL, 0,
210 	    optbuf, MAX_MNTOPT_STR)) {
211 		rpterr(special, mountp);
212 		exit(2);
213 	}
214 	if (optsize && !qflg)
215 		cmp_requested_to_actual_options(savedoptbuf, optbuf,
216 		    special, mountp);
217 }
218 
219 static void
220 usage(void)
221 {
222 	(void) fprintf(stderr,
223 		gettext("%s usage:\n%s [-F %s] [-r] [-o specific_options] "
224 		"{special | mount_point}\n%s [-F %s] [-r] [-o specific_options]"
225 		" special mount_point\n"),
226 		fstype, myname, fstype, myname, fstype);
227 	exit(1);
228 }
229