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