10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
52720Sfrankho * Common Development and Distribution License (the "License").
62720Sfrankho * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
220Sstevel@tonic-gate
230Sstevel@tonic-gate /*
24*5121Sfrankho * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
250Sstevel@tonic-gate * Use is subject to license terms.
260Sstevel@tonic-gate */
270Sstevel@tonic-gate
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <locale.h>
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <time.h>
320Sstevel@tonic-gate #include <signal.h>
330Sstevel@tonic-gate #include <string.h>
340Sstevel@tonic-gate #include <unistd.h>
350Sstevel@tonic-gate #include <stdlib.h>
360Sstevel@tonic-gate #include <errno.h>
370Sstevel@tonic-gate #include <sys/mntent.h>
380Sstevel@tonic-gate #include <sys/mnttab.h>
390Sstevel@tonic-gate #include <sys/mount.h>
400Sstevel@tonic-gate #include <sys/fs/pc_fs.h>
410Sstevel@tonic-gate #include <fslib.h>
420Sstevel@tonic-gate
430Sstevel@tonic-gate extern int daylight;
440Sstevel@tonic-gate
450Sstevel@tonic-gate static int roflag = 0;
460Sstevel@tonic-gate static char optbuf[MAX_MNTOPT_STR] = { '\0', };
470Sstevel@tonic-gate static int optsize = 0;
480Sstevel@tonic-gate
49*5121Sfrankho
50*5121Sfrankho /*
51*5121Sfrankho * Since the format/value expected for the mount options listed below
52*5121Sfrankho * differs between what the user mount command expects and what the
53*5121Sfrankho * kernel module can grok, we transmogrify the mount option string
54*5121Sfrankho * for such options. Others are copied through as-is.
55*5121Sfrankho */
56*5121Sfrankho static char *pcfs_opts[] = { MNTOPT_PCFS_TIMEZONE, NULL };
57*5121Sfrankho #define ARG_PCFS_TIMEZONE 0
58*5121Sfrankho
59*5121Sfrankho /*
60*5121Sfrankho * While constructing the mount option string, we need to append
61*5121Sfrankho * comma separators if there have been previous options copied over
62*5121Sfrankho * from the input string. This takes care of it.
63*5121Sfrankho */
64*5121Sfrankho static int
append_opt(char * str,int strsz,char * opt)65*5121Sfrankho append_opt(char *str, int strsz, char *opt)
66*5121Sfrankho {
67*5121Sfrankho if (str[0] != '\0' && strlcat(str, ",", strsz) >= strsz)
68*5121Sfrankho return (0);
69*5121Sfrankho
70*5121Sfrankho return (strlcat(str, opt, strsz) < strsz);
71*5121Sfrankho }
720Sstevel@tonic-gate
730Sstevel@tonic-gate int
main(int argc,char * argv[])740Sstevel@tonic-gate main(int argc, char *argv[])
750Sstevel@tonic-gate {
760Sstevel@tonic-gate char *mnt_special;
770Sstevel@tonic-gate char *mnt_mountp;
780Sstevel@tonic-gate int c;
790Sstevel@tonic-gate char *myname;
800Sstevel@tonic-gate char typename[64];
81*5121Sfrankho char tzstr[100];
82*5121Sfrankho char *tzval;
83*5121Sfrankho char *savedoptbuf = NULL, *savedoptarg = NULL;
84*5121Sfrankho char *in_arg, *val, *curarg;
850Sstevel@tonic-gate extern int optind;
860Sstevel@tonic-gate extern char *optarg;
870Sstevel@tonic-gate int error = 0;
880Sstevel@tonic-gate int verbose = 0;
89*5121Sfrankho int mflg = MS_OPTIONSTR; /* we always pass mount options */
900Sstevel@tonic-gate int qflg = 0;
910Sstevel@tonic-gate int optcnt = 0;
92*5121Sfrankho int tzdone = 0;
930Sstevel@tonic-gate
940Sstevel@tonic-gate myname = strrchr(argv[0], '/');
950Sstevel@tonic-gate myname = myname ? myname + 1 : argv[0];
960Sstevel@tonic-gate (void) snprintf(typename, sizeof (typename), "%s_%s",
970Sstevel@tonic-gate MNTTYPE_PCFS, myname);
980Sstevel@tonic-gate argv[0] = typename;
990Sstevel@tonic-gate
1000Sstevel@tonic-gate while ((c = getopt(argc, argv, "Vvmr?o:Oq")) != EOF) {
1010Sstevel@tonic-gate switch (c) {
1020Sstevel@tonic-gate case 'V':
1030Sstevel@tonic-gate case 'v':
1040Sstevel@tonic-gate verbose++;
1050Sstevel@tonic-gate break;
1060Sstevel@tonic-gate case '?':
1070Sstevel@tonic-gate error++;
1080Sstevel@tonic-gate break;
1090Sstevel@tonic-gate case 'r':
1100Sstevel@tonic-gate roflag++;
1110Sstevel@tonic-gate break;
1120Sstevel@tonic-gate case 'm':
1130Sstevel@tonic-gate mflg |= MS_NOMNTTAB;
1140Sstevel@tonic-gate break;
1150Sstevel@tonic-gate case 'o':
116*5121Sfrankho in_arg = optarg;
117*5121Sfrankho if ((savedoptarg = strdup(optarg)) == NULL) {
1180Sstevel@tonic-gate (void) fprintf(stderr,
119*5121Sfrankho gettext("%s: out of memory\n"), myname);
120*5121Sfrankho exit(2);
1210Sstevel@tonic-gate }
122*5121Sfrankho while (*in_arg != '\0') {
123*5121Sfrankho curarg = in_arg;
124*5121Sfrankho
125*5121Sfrankho switch (getsubopt(&in_arg, pcfs_opts, &val)) {
126*5121Sfrankho case ARG_PCFS_TIMEZONE:
127*5121Sfrankho if (tzdone || val == NULL)
128*5121Sfrankho goto invalarg;
129*5121Sfrankho tzval = val;
130*5121Sfrankho (void) snprintf(tzstr, 100,
131*5121Sfrankho "TZ=%s", tzval);
132*5121Sfrankho tzstr[99] = '\0';
133*5121Sfrankho (void) putenv(tzstr);
134*5121Sfrankho tzdone = 1;
135*5121Sfrankho break;
136*5121Sfrankho default:
137*5121Sfrankho /*
138*5121Sfrankho * Remove empty suboptions
139*5121Sfrankho * (happens on sequences of commas)
140*5121Sfrankho */
141*5121Sfrankho if (*curarg == '\0')
142*5121Sfrankho break;
143*5121Sfrankho
144*5121Sfrankho if (append_opt(optbuf, sizeof (optbuf),
145*5121Sfrankho curarg) == 0)
146*5121Sfrankho goto invalarg;
147*5121Sfrankho }
148*5121Sfrankho }
1490Sstevel@tonic-gate break;
1500Sstevel@tonic-gate case 'O':
1510Sstevel@tonic-gate mflg |= MS_OVERLAY;
1520Sstevel@tonic-gate break;
1530Sstevel@tonic-gate case 'q':
1540Sstevel@tonic-gate qflg = 1;
1550Sstevel@tonic-gate break;
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate if (verbose && !error) {
1600Sstevel@tonic-gate char *optptr;
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate (void) fprintf(stderr, "%s", typename);
1630Sstevel@tonic-gate for (optcnt = 1; optcnt < argc; optcnt++) {
1640Sstevel@tonic-gate optptr = argv[optcnt];
1650Sstevel@tonic-gate if (optptr)
1660Sstevel@tonic-gate (void) fprintf(stderr, " %s", optptr);
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate (void) fprintf(stderr, "\n");
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate if (argc - optind != 2 || error) {
1720Sstevel@tonic-gate /*
1730Sstevel@tonic-gate * don't hint at options yet (none are really supported)
1740Sstevel@tonic-gate */
1750Sstevel@tonic-gate (void) fprintf(stderr, gettext(
1760Sstevel@tonic-gate "Usage: %s [generic options] [-o suboptions] "
1770Sstevel@tonic-gate "special mount_point\n"), typename);
1780Sstevel@tonic-gate (void) fprintf(stderr, gettext(
1790Sstevel@tonic-gate "\tpcfs-specific suboptions are:\n"
1802720Sfrankho "\t clamptime,noclamptime\n"
181*5121Sfrankho "\t hidden,nohidden\n"
182*5121Sfrankho "\t atime,noatime\n"
183*5121Sfrankho "\t foldcase,nofoldcase\n"
184*5121Sfrankho "\t timezone=<valid TZ string>"));
1850Sstevel@tonic-gate exit(32);
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate mnt_special = argv[optind++];
1890Sstevel@tonic-gate mnt_mountp = argv[optind++];
1900Sstevel@tonic-gate
191*5121Sfrankho /*
192*5121Sfrankho * Pass timezone information to the kernel module so that
193*5121Sfrankho * FAT timestamps, as per spec, can be recorded in local time.
194*5121Sfrankho */
195*5121Sfrankho tzset();
196*5121Sfrankho /*
197*5121Sfrankho * We perform this validation only in case the user of
198*5121Sfrankho * mount(1m) specified the "timezone=..." option. That's
199*5121Sfrankho * because we don't want PCFS mounts to fail due to a
200*5121Sfrankho * botched $TZ environment variable. If the admin's
201*5121Sfrankho * environment contains garbage, it'll just parse as
202*5121Sfrankho * GMT (timezone=0).
203*5121Sfrankho */
204*5121Sfrankho if (tzdone && timezone == 0 && altzone == 0 && daylight == 0 &&
205*5121Sfrankho strcmp(tzname[0], tzval) &&
206*5121Sfrankho strspn(tzname[1], " ") == strlen(tzname[1])) {
207*5121Sfrankho goto invalarg;
208*5121Sfrankho }
209*5121Sfrankho (void) snprintf(tzstr, 100, "timezone=%d", timezone);
210*5121Sfrankho tzstr[99] = '\0';
211*5121Sfrankho if (append_opt(optbuf, sizeof (optbuf), tzstr) == 0)
212*5121Sfrankho goto invalarg;
213*5121Sfrankho
214*5121Sfrankho optsize = strlen(optbuf);
215*5121Sfrankho
2160Sstevel@tonic-gate if (roflag)
2170Sstevel@tonic-gate mflg |= MS_RDONLY;
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate if ((savedoptbuf = strdup(optbuf)) == NULL) {
2200Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: out of memory\n"),
2210Sstevel@tonic-gate myname);
2220Sstevel@tonic-gate exit(2);
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate (void) signal(SIGHUP, SIG_IGN);
2250Sstevel@tonic-gate (void) signal(SIGQUIT, SIG_IGN);
2260Sstevel@tonic-gate (void) signal(SIGINT, SIG_IGN);
2270Sstevel@tonic-gate
2280Sstevel@tonic-gate if (verbose) {
2290Sstevel@tonic-gate (void) fprintf(stderr, "mount(%s, \"%s\", %d, %s",
2300Sstevel@tonic-gate mnt_special, mnt_mountp, mflg, MNTTYPE_PCFS);
2310Sstevel@tonic-gate }
232*5121Sfrankho if (mount(mnt_special, mnt_mountp, mflg, MNTTYPE_PCFS,
233*5121Sfrankho NULL, 0, optbuf, MAX_MNTOPT_STR)) {
2340Sstevel@tonic-gate if (errno == EBUSY) {
2350Sstevel@tonic-gate (void) fprintf(stderr, gettext(
2360Sstevel@tonic-gate "mount: %s is already mounted or %s is busy\n"),
2370Sstevel@tonic-gate mnt_special, mnt_mountp);
2380Sstevel@tonic-gate } else if (errno == EINVAL) {
2390Sstevel@tonic-gate (void) fprintf(stderr, gettext(
2400Sstevel@tonic-gate "mount: %s is not a DOS filesystem.\n"),
2410Sstevel@tonic-gate mnt_special);
2420Sstevel@tonic-gate } else {
2430Sstevel@tonic-gate perror("mount");
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate exit(32);
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate
2480Sstevel@tonic-gate if (optsize && !qflg)
2490Sstevel@tonic-gate cmp_requested_to_actual_options(savedoptbuf, optbuf,
2500Sstevel@tonic-gate mnt_special, mnt_mountp);
2510Sstevel@tonic-gate return (0);
252*5121Sfrankho
253*5121Sfrankho invalarg:
254*5121Sfrankho (void) fprintf(stderr,
255*5121Sfrankho gettext("%s: Invalid mount options: %s\n"), myname, savedoptarg);
256*5121Sfrankho return (2);
2570Sstevel@tonic-gate }
258