xref: /illumos-gate/usr/src/cmd/fs.d/proc/mount.c (revision 2a8bcb4e)
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		"proc"
46 
47 static void rpterr(char *, char *);
48 static void do_mount(char *, char *, int);
49 static void usage(void);
50 
51 static char  optbuf[MAX_MNTOPT_STR] = { '\0', };
52 static int   optsize = 0;
53 
54 static int	roflag = 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
main(int argc,char * argv[])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, "?o:rmOq")) != -1)
89 		switch (cc) {
90 		case 'm':
91 			mflg++;
92 			break;
93 		case 'r':
94 			if (roflag)
95 				errflag = 1;
96 			else
97 				roflag++;
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 = 1;
114 			break;
115 		case '?':
116 			errflag = 1;
117 			break;
118 		}
119 
120 	/*
121 	 *	There must be at least 2 more arguments, the
122 	 *	special file and the directory.
123 	 */
124 
125 	if (((argc - optind) != 2) || (errflag))
126 		usage();
127 
128 	special = argv[optind++];
129 	mountp = argv[optind++];
130 
131 	/*
132 	 *	Perform the mount.
133 	 */
134 	do_mount(special, mountp, roflag ? MS_RDONLY : 0);
135 
136 	return (0);
137 }
138 
139 void
rpterr(char * bs,char * mp)140 rpterr(char *bs, char *mp)
141 {
142 	switch (errno) {
143 	case EPERM:
144 		(void) fprintf(stderr, gettext("%s: insufficient privileges\n"),
145 		    myname);
146 		break;
147 	case ENXIO:
148 		(void) fprintf(stderr, gettext("%s: %s no such device\n"),
149 		    myname, bs);
150 		break;
151 	case ENOTDIR:
152 		(void) fprintf(stderr,
153 gettext("%s: %s not a directory\n\tor a component of %s is not a directory\n"),
154 			myname, mp, bs);
155 		break;
156 	case ENOENT:
157 		(void) fprintf(stderr,
158 			gettext("%s: %s or %s, no such file or directory\n"),
159 			myname, bs, mp);
160 		break;
161 	case EINVAL:
162 		(void) fprintf(stderr, gettext("%s: %s is not this fstype.\n"),
163 			myname, bs);
164 		break;
165 	case EBUSY:
166 		(void) fprintf(stderr,
167 			gettext("%s: %s is already mounted or %s is busy\n"),
168 			myname, bs, mp);
169 		break;
170 	case ENOTBLK:
171 		(void) fprintf(stderr, gettext("%s: %s not a block device\n"),
172 				myname, bs);
173 		break;
174 	case EROFS:
175 		(void) fprintf(stderr, gettext("%s: %s write-protected\n"),
176 				myname, bs);
177 		break;
178 	case ENOSPC:
179 		(void) fprintf(stderr,
180 			gettext("%s: the state of %s is not okay\n"
181 			"\tand it was attempted to mount read/write\n"),
182 			myname, bs);
183 		break;
184 	default:
185 		perror(myname);
186 		(void) fprintf(stderr, gettext("%s: cannot mount %s\n"),
187 		    myname, bs);
188 	}
189 }
190 
191 static void
do_mount(char * special,char * mountp,int rflag)192 do_mount(char *special, char *mountp, int rflag)
193 {
194 	char *savedoptbuf;
195 
196 	if ((savedoptbuf = strdup(optbuf)) == NULL) {
197 		(void) fprintf(stderr, gettext("%s: out of memory\n"),
198 		    myname);
199 		exit(2);
200 	}
201 
202 	if (Oflg)
203 		rflag |= MS_OVERLAY;
204 	if (mflg)
205 		rflag |= MS_NOMNTTAB;
206 	if (mount(special, mountp, rflag | MS_OPTIONSTR, fstype, NULL, 0,
207 	    optbuf, MAX_MNTOPT_STR)) {
208 		rpterr(special, mountp);
209 		exit(2);
210 	}
211 	if (optsize && !qflg)
212 		cmp_requested_to_actual_options(savedoptbuf, optbuf,
213 		    special, mountp);
214 }
215 
216 static void
usage(void)217 usage(void)
218 {
219 	(void) fprintf(stderr,
220 		gettext("%s usage:\n%s [-F %s] [-r] [-o specific_options] "
221 		"{special | mount_point}\n%s [-F %s] [-r] [-o specific_options]"
222 		" special mount_point\n"),
223 		fstype, myname, fstype, myname, fstype);
224 	exit(1);
225 }
226