xref: /titanic_52/usr/src/cmd/fs.d/pcfs/mount/mount.c (revision f127cb91970601c2695eead0ee127f1cac48bb7a)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5264a6e74Sfrankho  * Common Development and Distribution License (the "License").
6264a6e74Sfrankho  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate /*
24*f127cb91Sfrankho  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <locale.h>
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <time.h>
327c478bd9Sstevel@tonic-gate #include <signal.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <unistd.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
367c478bd9Sstevel@tonic-gate #include <errno.h>
377c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
387c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
397c478bd9Sstevel@tonic-gate #include <sys/mount.h>
407c478bd9Sstevel@tonic-gate #include <sys/fs/pc_fs.h>
417c478bd9Sstevel@tonic-gate #include <fslib.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate extern int	daylight;
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate static int roflag = 0;
467c478bd9Sstevel@tonic-gate static char optbuf[MAX_MNTOPT_STR] = { '\0', };
477c478bd9Sstevel@tonic-gate static int optsize = 0;
487c478bd9Sstevel@tonic-gate 
49*f127cb91Sfrankho 
50*f127cb91Sfrankho /*
51*f127cb91Sfrankho  * Since the format/value expected for the mount options listed below
52*f127cb91Sfrankho  * differs between what the user mount command expects and what the
53*f127cb91Sfrankho  * kernel module can grok, we transmogrify the mount option string
54*f127cb91Sfrankho  * for such options. Others are copied through as-is.
55*f127cb91Sfrankho  */
56*f127cb91Sfrankho static char *pcfs_opts[] = { MNTOPT_PCFS_TIMEZONE, NULL };
57*f127cb91Sfrankho #define	ARG_PCFS_TIMEZONE	0
58*f127cb91Sfrankho 
59*f127cb91Sfrankho /*
60*f127cb91Sfrankho  * While constructing the mount option string, we need to append
61*f127cb91Sfrankho  * comma separators if there have been previous options copied over
62*f127cb91Sfrankho  * from the input string. This takes care of it.
63*f127cb91Sfrankho  */
64*f127cb91Sfrankho static int
65*f127cb91Sfrankho append_opt(char *str, int strsz, char *opt)
66*f127cb91Sfrankho {
67*f127cb91Sfrankho 	if (str[0] != '\0' && strlcat(str, ",", strsz) >= strsz)
68*f127cb91Sfrankho 		return (0);
69*f127cb91Sfrankho 
70*f127cb91Sfrankho 	return (strlcat(str, opt, strsz) < strsz);
71*f127cb91Sfrankho }
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate int
747c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate 	char *mnt_special;
777c478bd9Sstevel@tonic-gate 	char *mnt_mountp;
787c478bd9Sstevel@tonic-gate 	int c;
797c478bd9Sstevel@tonic-gate 	char *myname;
807c478bd9Sstevel@tonic-gate 	char typename[64];
81*f127cb91Sfrankho 	char tzstr[100];
82*f127cb91Sfrankho 	char *tzval;
83*f127cb91Sfrankho 	char *savedoptbuf = NULL, *savedoptarg = NULL;
84*f127cb91Sfrankho 	char *in_arg, *val, *curarg;
857c478bd9Sstevel@tonic-gate 	extern int optind;
867c478bd9Sstevel@tonic-gate 	extern char *optarg;
877c478bd9Sstevel@tonic-gate 	int error = 0;
887c478bd9Sstevel@tonic-gate 	int verbose = 0;
89*f127cb91Sfrankho 	int mflg = MS_OPTIONSTR;	/* we always pass mount options */
907c478bd9Sstevel@tonic-gate 	int qflg = 0;
917c478bd9Sstevel@tonic-gate 	int optcnt = 0;
92*f127cb91Sfrankho 	int tzdone = 0;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	myname = strrchr(argv[0], '/');
957c478bd9Sstevel@tonic-gate 	myname = myname ? myname + 1 : argv[0];
967c478bd9Sstevel@tonic-gate 	(void) snprintf(typename, sizeof (typename), "%s_%s",
977c478bd9Sstevel@tonic-gate 	    MNTTYPE_PCFS, myname);
987c478bd9Sstevel@tonic-gate 	argv[0] = typename;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "Vvmr?o:Oq")) != EOF) {
1017c478bd9Sstevel@tonic-gate 		switch (c) {
1027c478bd9Sstevel@tonic-gate 		case 'V':
1037c478bd9Sstevel@tonic-gate 		case 'v':
1047c478bd9Sstevel@tonic-gate 			verbose++;
1057c478bd9Sstevel@tonic-gate 			break;
1067c478bd9Sstevel@tonic-gate 		case '?':
1077c478bd9Sstevel@tonic-gate 			error++;
1087c478bd9Sstevel@tonic-gate 			break;
1097c478bd9Sstevel@tonic-gate 		case 'r':
1107c478bd9Sstevel@tonic-gate 			roflag++;
1117c478bd9Sstevel@tonic-gate 			break;
1127c478bd9Sstevel@tonic-gate 		case 'm':
1137c478bd9Sstevel@tonic-gate 			mflg |= MS_NOMNTTAB;
1147c478bd9Sstevel@tonic-gate 			break;
1157c478bd9Sstevel@tonic-gate 		case 'o':
116*f127cb91Sfrankho 			in_arg = optarg;
117*f127cb91Sfrankho 			if ((savedoptarg = strdup(optarg)) == NULL) {
1187c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
119*f127cb91Sfrankho 				    gettext("%s: out of memory\n"), myname);
120*f127cb91Sfrankho 				exit(2);
1217c478bd9Sstevel@tonic-gate 			}
122*f127cb91Sfrankho 			while (*in_arg != '\0') {
123*f127cb91Sfrankho 				curarg = in_arg;
124*f127cb91Sfrankho 
125*f127cb91Sfrankho 				switch (getsubopt(&in_arg, pcfs_opts, &val)) {
126*f127cb91Sfrankho 				case ARG_PCFS_TIMEZONE:
127*f127cb91Sfrankho 					if (tzdone || val == NULL)
128*f127cb91Sfrankho 						goto invalarg;
129*f127cb91Sfrankho 					tzval = val;
130*f127cb91Sfrankho 					(void) snprintf(tzstr, 100,
131*f127cb91Sfrankho 					    "TZ=%s", tzval);
132*f127cb91Sfrankho 					tzstr[99] = '\0';
133*f127cb91Sfrankho 					(void) putenv(tzstr);
134*f127cb91Sfrankho 					tzdone = 1;
135*f127cb91Sfrankho 					break;
136*f127cb91Sfrankho 				default:
137*f127cb91Sfrankho 					/*
138*f127cb91Sfrankho 					 * Remove empty suboptions
139*f127cb91Sfrankho 					 * (happens on sequences of commas)
140*f127cb91Sfrankho 					 */
141*f127cb91Sfrankho 					if (*curarg == '\0')
142*f127cb91Sfrankho 						break;
143*f127cb91Sfrankho 
144*f127cb91Sfrankho 					if (append_opt(optbuf, sizeof (optbuf),
145*f127cb91Sfrankho 					    curarg) == 0)
146*f127cb91Sfrankho 						goto invalarg;
147*f127cb91Sfrankho 				}
148*f127cb91Sfrankho 			}
1497c478bd9Sstevel@tonic-gate 			break;
1507c478bd9Sstevel@tonic-gate 		case 'O':
1517c478bd9Sstevel@tonic-gate 			mflg |= MS_OVERLAY;
1527c478bd9Sstevel@tonic-gate 			break;
1537c478bd9Sstevel@tonic-gate 		case 'q':
1547c478bd9Sstevel@tonic-gate 			qflg = 1;
1557c478bd9Sstevel@tonic-gate 			break;
1567c478bd9Sstevel@tonic-gate 		}
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	if (verbose && !error) {
1607c478bd9Sstevel@tonic-gate 		char *optptr;
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s", typename);
1637c478bd9Sstevel@tonic-gate 		for (optcnt = 1; optcnt < argc; optcnt++) {
1647c478bd9Sstevel@tonic-gate 			optptr = argv[optcnt];
1657c478bd9Sstevel@tonic-gate 			if (optptr)
1667c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, " %s", optptr);
1677c478bd9Sstevel@tonic-gate 		}
1687c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\n");
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	if (argc - optind != 2 || error) {
1727c478bd9Sstevel@tonic-gate 		/*
1737c478bd9Sstevel@tonic-gate 		 * don't hint at options yet (none are really supported)
1747c478bd9Sstevel@tonic-gate 		 */
1757c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
1767c478bd9Sstevel@tonic-gate 		    "Usage: %s [generic options] [-o suboptions] "
1777c478bd9Sstevel@tonic-gate 		    "special mount_point\n"), typename);
1787c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
1797c478bd9Sstevel@tonic-gate 		    "\tpcfs-specific suboptions are:\n"
180264a6e74Sfrankho 		    "\t     clamptime,noclamptime\n"
181*f127cb91Sfrankho 		    "\t     hidden,nohidden\n"
182*f127cb91Sfrankho 		    "\t     atime,noatime\n"
183*f127cb91Sfrankho 		    "\t     foldcase,nofoldcase\n"
184*f127cb91Sfrankho 		    "\t     timezone=<valid TZ string>"));
1857c478bd9Sstevel@tonic-gate 		exit(32);
1867c478bd9Sstevel@tonic-gate 	}
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	mnt_special = argv[optind++];
1897c478bd9Sstevel@tonic-gate 	mnt_mountp = argv[optind++];
1907c478bd9Sstevel@tonic-gate 
191*f127cb91Sfrankho 	/*
192*f127cb91Sfrankho 	 * Pass timezone information to the kernel module so that
193*f127cb91Sfrankho 	 * FAT timestamps, as per spec, can be recorded in local time.
194*f127cb91Sfrankho 	 */
195*f127cb91Sfrankho 	tzset();
196*f127cb91Sfrankho 	/*
197*f127cb91Sfrankho 	 * We perform this validation only in case the user of
198*f127cb91Sfrankho 	 * mount(1m) specified the "timezone=..." option. That's
199*f127cb91Sfrankho 	 * because we don't want PCFS mounts to fail due to a
200*f127cb91Sfrankho 	 * botched $TZ environment variable. If the admin's
201*f127cb91Sfrankho 	 * environment contains garbage, it'll just parse as
202*f127cb91Sfrankho 	 * GMT (timezone=0).
203*f127cb91Sfrankho 	 */
204*f127cb91Sfrankho 	if (tzdone && timezone == 0 && altzone == 0 && daylight == 0 &&
205*f127cb91Sfrankho 	    strcmp(tzname[0], tzval) &&
206*f127cb91Sfrankho 	    strspn(tzname[1], " ") == strlen(tzname[1])) {
207*f127cb91Sfrankho 		goto invalarg;
208*f127cb91Sfrankho 	}
209*f127cb91Sfrankho 	(void) snprintf(tzstr, 100, "timezone=%d", timezone);
210*f127cb91Sfrankho 	tzstr[99] = '\0';
211*f127cb91Sfrankho 	if (append_opt(optbuf, sizeof (optbuf), tzstr) == 0)
212*f127cb91Sfrankho 		goto invalarg;
213*f127cb91Sfrankho 
214*f127cb91Sfrankho 	optsize = strlen(optbuf);
215*f127cb91Sfrankho 
2167c478bd9Sstevel@tonic-gate 	if (roflag)
2177c478bd9Sstevel@tonic-gate 		mflg |= MS_RDONLY;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	if ((savedoptbuf = strdup(optbuf)) == NULL) {
2207c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: out of memory\n"),
2217c478bd9Sstevel@tonic-gate 		    myname);
2227c478bd9Sstevel@tonic-gate 		exit(2);
2237c478bd9Sstevel@tonic-gate 	}
2247c478bd9Sstevel@tonic-gate 	(void) signal(SIGHUP,  SIG_IGN);
2257c478bd9Sstevel@tonic-gate 	(void) signal(SIGQUIT, SIG_IGN);
2267c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT,  SIG_IGN);
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	if (verbose) {
2297c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "mount(%s, \"%s\", %d, %s",
2307c478bd9Sstevel@tonic-gate 		    mnt_special, mnt_mountp, mflg, MNTTYPE_PCFS);
2317c478bd9Sstevel@tonic-gate 	}
232*f127cb91Sfrankho 	if (mount(mnt_special, mnt_mountp, mflg, MNTTYPE_PCFS,
233*f127cb91Sfrankho 	    NULL, 0, optbuf, MAX_MNTOPT_STR)) {
2347c478bd9Sstevel@tonic-gate 		if (errno == EBUSY) {
2357c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
2367c478bd9Sstevel@tonic-gate 			    "mount: %s is already mounted or %s is busy\n"),
2377c478bd9Sstevel@tonic-gate 			    mnt_special, mnt_mountp);
2387c478bd9Sstevel@tonic-gate 		} else if (errno == EINVAL) {
2397c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
2407c478bd9Sstevel@tonic-gate 			    "mount: %s is not a DOS filesystem.\n"),
2417c478bd9Sstevel@tonic-gate 			    mnt_special);
2427c478bd9Sstevel@tonic-gate 		} else {
2437c478bd9Sstevel@tonic-gate 			perror("mount");
2447c478bd9Sstevel@tonic-gate 		}
2457c478bd9Sstevel@tonic-gate 		exit(32);
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	if (optsize && !qflg)
2497c478bd9Sstevel@tonic-gate 		cmp_requested_to_actual_options(savedoptbuf, optbuf,
2507c478bd9Sstevel@tonic-gate 		    mnt_special, mnt_mountp);
2517c478bd9Sstevel@tonic-gate 	return (0);
252*f127cb91Sfrankho 
253*f127cb91Sfrankho invalarg:
254*f127cb91Sfrankho 	(void) fprintf(stderr,
255*f127cb91Sfrankho 	    gettext("%s: Invalid mount options: %s\n"), myname, savedoptarg);
256*f127cb91Sfrankho 	return (2);
2577c478bd9Sstevel@tonic-gate }
258