xref: /onnv-gate/usr/src/cmd/fs.d/tmpfs/mount.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
23*0Sstevel@tonic-gate 
24*0Sstevel@tonic-gate /*
25*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
26*0Sstevel@tonic-gate  * Use is subject to license terms.
27*0Sstevel@tonic-gate  */
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdio.h>
30*0Sstevel@tonic-gate #include <stdlib.h>
31*0Sstevel@tonic-gate #include <string.h>
32*0Sstevel@tonic-gate #include <errno.h>
33*0Sstevel@tonic-gate #include <sys/mount.h>
34*0Sstevel@tonic-gate #include <sys/types.h>
35*0Sstevel@tonic-gate #include <locale.h>
36*0Sstevel@tonic-gate #include <sys/stat.h>
37*0Sstevel@tonic-gate #include <fslib.h>
38*0Sstevel@tonic-gate #include <stdio.h>
39*0Sstevel@tonic-gate #include <sys/mnttab.h>
40*0Sstevel@tonic-gate #include <poll.h>
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate #define	MNTTYPE_TMPFS	"tmpfs"
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate static int	nmflg = 0;
45*0Sstevel@tonic-gate static int	qflg = 0;
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate static boolean_t
in_mnttab(char * mountp)48*0Sstevel@tonic-gate in_mnttab(char *mountp)
49*0Sstevel@tonic-gate {
50*0Sstevel@tonic-gate 	FILE *file;
51*0Sstevel@tonic-gate 	int found = B_FALSE;
52*0Sstevel@tonic-gate 	struct mnttab mntent;
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate 	if ((file = fopen("/etc/mnttab", "r")) == NULL)
55*0Sstevel@tonic-gate 		return (B_FALSE);
56*0Sstevel@tonic-gate 	while (getmntent(file, &mntent) == 0) {
57*0Sstevel@tonic-gate 		if (mntent.mnt_mountp != NULL &&
58*0Sstevel@tonic-gate 		    strcmp(mntent.mnt_mountp, mountp) == 0 &&
59*0Sstevel@tonic-gate 		    mntent.mnt_fstype != NULL &&
60*0Sstevel@tonic-gate 		    strcmp(mntent.mnt_fstype, MNTTYPE_TMPFS) == 0) {
61*0Sstevel@tonic-gate 			found = B_TRUE;
62*0Sstevel@tonic-gate 			break;
63*0Sstevel@tonic-gate 		}
64*0Sstevel@tonic-gate 	}
65*0Sstevel@tonic-gate 	(void) fclose(file);
66*0Sstevel@tonic-gate 	return (found);
67*0Sstevel@tonic-gate }
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate int
main(int argc,char * argv[])70*0Sstevel@tonic-gate main(int argc, char *argv[])
71*0Sstevel@tonic-gate {
72*0Sstevel@tonic-gate 	/* mount information */
73*0Sstevel@tonic-gate 	char *special;
74*0Sstevel@tonic-gate 	char *mountp;
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 	int c;
77*0Sstevel@tonic-gate 	char *myname;
78*0Sstevel@tonic-gate 	char typename[64];
79*0Sstevel@tonic-gate 	extern int optind;
80*0Sstevel@tonic-gate 	extern char *optarg;
81*0Sstevel@tonic-gate 	int error = 0;
82*0Sstevel@tonic-gate 	int verbose = 0;
83*0Sstevel@tonic-gate 	int mflg = 0;
84*0Sstevel@tonic-gate 	int optcnt = 0;
85*0Sstevel@tonic-gate 	int mount_attempts = 5;
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 	char optbuf[MAX_MNTOPT_STR];
88*0Sstevel@tonic-gate 	int optsize = 0;
89*0Sstevel@tonic-gate 	char *saveoptbuf;
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
94*0Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
95*0Sstevel@tonic-gate #endif
96*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 	myname = strrchr(argv[0], '/');
99*0Sstevel@tonic-gate 	myname = myname ? myname + 1 : argv[0];
100*0Sstevel@tonic-gate 	(void) snprintf(typename, sizeof (typename), "%s_%s",
101*0Sstevel@tonic-gate 	    MNTTYPE_TMPFS, myname);
102*0Sstevel@tonic-gate 	argv[0] = typename;
103*0Sstevel@tonic-gate 	optbuf[0] = '\0';
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "?o:VmOq")) != EOF) {
106*0Sstevel@tonic-gate 		switch (c) {
107*0Sstevel@tonic-gate 		case 'V':
108*0Sstevel@tonic-gate 			verbose++;
109*0Sstevel@tonic-gate 			break;
110*0Sstevel@tonic-gate 		case '?':
111*0Sstevel@tonic-gate 			error++;
112*0Sstevel@tonic-gate 			break;
113*0Sstevel@tonic-gate 		case 'm':
114*0Sstevel@tonic-gate 			nmflg++;
115*0Sstevel@tonic-gate 			break;
116*0Sstevel@tonic-gate 		case 'O':
117*0Sstevel@tonic-gate 			mflg |= MS_OVERLAY;
118*0Sstevel@tonic-gate 			break;
119*0Sstevel@tonic-gate 		case 'o':
120*0Sstevel@tonic-gate 			(void) strncpy(optbuf, optarg, MAX_MNTOPT_STR);
121*0Sstevel@tonic-gate 			optbuf[MAX_MNTOPT_STR - 1] = '\0';
122*0Sstevel@tonic-gate 			optsize = strlen(optbuf);
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 			if (verbose)
125*0Sstevel@tonic-gate 				(void) fprintf(stderr, "optsize:%d optbuf:%s\n",
126*0Sstevel@tonic-gate 				    optsize, optbuf);
127*0Sstevel@tonic-gate 			break;
128*0Sstevel@tonic-gate 		case 'q':
129*0Sstevel@tonic-gate 			qflg++;
130*0Sstevel@tonic-gate 			break;
131*0Sstevel@tonic-gate 		}
132*0Sstevel@tonic-gate 	}
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 	if (verbose && !error) {
135*0Sstevel@tonic-gate 		char *optptr;
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 		(void) fprintf(stderr, "%s", typename);
138*0Sstevel@tonic-gate 		for (optcnt = 1; optcnt < argc; optcnt++) {
139*0Sstevel@tonic-gate 			optptr = argv[optcnt];
140*0Sstevel@tonic-gate 			if (optptr)
141*0Sstevel@tonic-gate 				(void) fprintf(stderr, " %s", optptr);
142*0Sstevel@tonic-gate 		}
143*0Sstevel@tonic-gate 		(void) fprintf(stderr, "\n");
144*0Sstevel@tonic-gate 	}
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 	if (argc - optind != 2 || error) {
147*0Sstevel@tonic-gate 		(void) fprintf(stderr,
148*0Sstevel@tonic-gate 		    gettext("Usage: %s [-o size] swap mount_point\n"),
149*0Sstevel@tonic-gate 		    typename);
150*0Sstevel@tonic-gate 		exit(32);
151*0Sstevel@tonic-gate 	}
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 	special = argv[optind++];
154*0Sstevel@tonic-gate 	mountp = argv[optind++];
155*0Sstevel@tonic-gate 	mflg |= MS_OPTIONSTR;
156*0Sstevel@tonic-gate 	mflg |= (nmflg ? MS_NOMNTTAB : 0);
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 	if (verbose) {
159*0Sstevel@tonic-gate 		(void) fprintf(stderr, "mount(%s, \"%s\", %d, %s",
160*0Sstevel@tonic-gate 		    special, mountp, mflg, MNTTYPE_TMPFS);
161*0Sstevel@tonic-gate 		if (optsize)
162*0Sstevel@tonic-gate 			(void) fprintf(stderr, ", \"%s\", %d)\n",
163*0Sstevel@tonic-gate 			    optbuf, strlen(optbuf));
164*0Sstevel@tonic-gate 		else
165*0Sstevel@tonic-gate 			(void) fprintf(stderr, ")\n");
166*0Sstevel@tonic-gate 	}
167*0Sstevel@tonic-gate 	if (optsize) {
168*0Sstevel@tonic-gate 		if ((saveoptbuf = strdup(optbuf)) == NULL) {
169*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: out of memory\n"),
170*0Sstevel@tonic-gate 				"mount");
171*0Sstevel@tonic-gate 			exit(1);
172*0Sstevel@tonic-gate 		}
173*0Sstevel@tonic-gate 	}
174*0Sstevel@tonic-gate again:	if (mount(special, mountp, mflg, MNTTYPE_TMPFS, NULL, 0,
175*0Sstevel@tonic-gate 	    optbuf, MAX_MNTOPT_STR)) {
176*0Sstevel@tonic-gate 		if (errno == EBUSY && !(mflg & MS_OVERLAY)) {
177*0Sstevel@tonic-gate 			/*
178*0Sstevel@tonic-gate 			 * Because of bug 6176743, any attempt to mount
179*0Sstevel@tonic-gate 			 * tmpfs filesystem could fail for reasons
180*0Sstevel@tonic-gate 			 * described in that bug.  We're trying to detect
181*0Sstevel@tonic-gate 			 * that situation here by checking that the filesystem
182*0Sstevel@tonic-gate 			 * we're mounting is not in /etc/mnttab yet.
183*0Sstevel@tonic-gate 			 * When that bug is fixed, this code can be removed.
184*0Sstevel@tonic-gate 			 */
185*0Sstevel@tonic-gate 			if (!in_mnttab(mountp) && mount_attempts-- > 0) {
186*0Sstevel@tonic-gate 				(void) poll(NULL, 0, 50);
187*0Sstevel@tonic-gate 				goto again;
188*0Sstevel@tonic-gate 			}
189*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
190*0Sstevel@tonic-gate 			    "%s: %s is already mounted or %s is busy\n"),
191*0Sstevel@tonic-gate 			    myname, mountp, special);
192*0Sstevel@tonic-gate 		} else {
193*0Sstevel@tonic-gate 			perror("mount");
194*0Sstevel@tonic-gate 		}
195*0Sstevel@tonic-gate 		exit(32);
196*0Sstevel@tonic-gate 	}
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 	if (optsize && !qflg)
199*0Sstevel@tonic-gate 		cmp_requested_to_actual_options(saveoptbuf, optbuf,
200*0Sstevel@tonic-gate 			special, mountp);
201*0Sstevel@tonic-gate 	return (0);
202*0Sstevel@tonic-gate }
203