xref: /illumos-gate/usr/src/cmd/fs.d/pcfs/mount/mount.c (revision bbf21555)
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 
227c478bd9Sstevel@tonic-gate /*
23f127cb91Sfrankho  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/types.h>
287c478bd9Sstevel@tonic-gate #include <locale.h>
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <time.h>
317c478bd9Sstevel@tonic-gate #include <signal.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <unistd.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <errno.h>
367c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
377c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
387c478bd9Sstevel@tonic-gate #include <sys/mount.h>
397c478bd9Sstevel@tonic-gate #include <sys/fs/pc_fs.h>
407c478bd9Sstevel@tonic-gate #include <fslib.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate extern int	daylight;
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate static int roflag = 0;
457c478bd9Sstevel@tonic-gate static char optbuf[MAX_MNTOPT_STR] = { '\0', };
467c478bd9Sstevel@tonic-gate static int optsize = 0;
477c478bd9Sstevel@tonic-gate 
48f127cb91Sfrankho 
49f127cb91Sfrankho /*
50f127cb91Sfrankho  * Since the format/value expected for the mount options listed below
51f127cb91Sfrankho  * differs between what the user mount command expects and what the
52f127cb91Sfrankho  * kernel module can grok, we transmogrify the mount option string
53f127cb91Sfrankho  * for such options. Others are copied through as-is.
54f127cb91Sfrankho  */
55f127cb91Sfrankho static char *pcfs_opts[] = { MNTOPT_PCFS_TIMEZONE, NULL };
56f127cb91Sfrankho #define	ARG_PCFS_TIMEZONE	0
57f127cb91Sfrankho 
58f127cb91Sfrankho /*
59f127cb91Sfrankho  * While constructing the mount option string, we need to append
60f127cb91Sfrankho  * comma separators if there have been previous options copied over
61f127cb91Sfrankho  * from the input string. This takes care of it.
62f127cb91Sfrankho  */
63f127cb91Sfrankho static int
append_opt(char * str,int strsz,char * opt)64f127cb91Sfrankho append_opt(char *str, int strsz, char *opt)
65f127cb91Sfrankho {
66f127cb91Sfrankho 	if (str[0] != '\0' && strlcat(str, ",", strsz) >= strsz)
67f127cb91Sfrankho 		return (0);
68f127cb91Sfrankho 
69f127cb91Sfrankho 	return (strlcat(str, opt, strsz) < strsz);
70f127cb91Sfrankho }
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])737c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
747c478bd9Sstevel@tonic-gate {
757c478bd9Sstevel@tonic-gate 	char *mnt_special;
767c478bd9Sstevel@tonic-gate 	char *mnt_mountp;
777c478bd9Sstevel@tonic-gate 	int c;
787c478bd9Sstevel@tonic-gate 	char *myname;
797c478bd9Sstevel@tonic-gate 	char typename[64];
80f127cb91Sfrankho 	char tzstr[100];
81f127cb91Sfrankho 	char *tzval;
82f127cb91Sfrankho 	char *savedoptbuf = NULL, *savedoptarg = NULL;
83f127cb91Sfrankho 	char *in_arg, *val, *curarg;
847c478bd9Sstevel@tonic-gate 	extern int optind;
857c478bd9Sstevel@tonic-gate 	extern char *optarg;
867c478bd9Sstevel@tonic-gate 	int error = 0;
877c478bd9Sstevel@tonic-gate 	int verbose = 0;
88f127cb91Sfrankho 	int mflg = MS_OPTIONSTR;	/* we always pass mount options */
897c478bd9Sstevel@tonic-gate 	int qflg = 0;
907c478bd9Sstevel@tonic-gate 	int optcnt = 0;
91f127cb91Sfrankho 	int tzdone = 0;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	myname = strrchr(argv[0], '/');
947c478bd9Sstevel@tonic-gate 	myname = myname ? myname + 1 : argv[0];
957c478bd9Sstevel@tonic-gate 	(void) snprintf(typename, sizeof (typename), "%s_%s",
967c478bd9Sstevel@tonic-gate 	    MNTTYPE_PCFS, myname);
977c478bd9Sstevel@tonic-gate 	argv[0] = typename;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "Vvmr?o:Oq")) != EOF) {
1007c478bd9Sstevel@tonic-gate 		switch (c) {
1017c478bd9Sstevel@tonic-gate 		case 'V':
1027c478bd9Sstevel@tonic-gate 		case 'v':
1037c478bd9Sstevel@tonic-gate 			verbose++;
1047c478bd9Sstevel@tonic-gate 			break;
1057c478bd9Sstevel@tonic-gate 		case '?':
1067c478bd9Sstevel@tonic-gate 			error++;
1077c478bd9Sstevel@tonic-gate 			break;
1087c478bd9Sstevel@tonic-gate 		case 'r':
1097c478bd9Sstevel@tonic-gate 			roflag++;
1107c478bd9Sstevel@tonic-gate 			break;
1117c478bd9Sstevel@tonic-gate 		case 'm':
1127c478bd9Sstevel@tonic-gate 			mflg |= MS_NOMNTTAB;
1137c478bd9Sstevel@tonic-gate 			break;
1147c478bd9Sstevel@tonic-gate 		case 'o':
115f127cb91Sfrankho 			in_arg = optarg;
116f127cb91Sfrankho 			if ((savedoptarg = strdup(optarg)) == NULL) {
1177c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
118f127cb91Sfrankho 				    gettext("%s: out of memory\n"), myname);
119f127cb91Sfrankho 				exit(2);
120f127cb91Sfrankho 			}
121f127cb91Sfrankho 			while (*in_arg != '\0') {
122f127cb91Sfrankho 				curarg = in_arg;
123f127cb91Sfrankho 
124f127cb91Sfrankho 				switch (getsubopt(&in_arg, pcfs_opts, &val)) {
125f127cb91Sfrankho 				case ARG_PCFS_TIMEZONE:
126f127cb91Sfrankho 					if (tzdone || val == NULL)
127f127cb91Sfrankho 						goto invalarg;
128f127cb91Sfrankho 					tzval = val;
129f127cb91Sfrankho 					(void) snprintf(tzstr, 100,
130f127cb91Sfrankho 					    "TZ=%s", tzval);
131f127cb91Sfrankho 					tzstr[99] = '\0';
132f127cb91Sfrankho 					(void) putenv(tzstr);
133f127cb91Sfrankho 					tzdone = 1;
134f127cb91Sfrankho 					break;
135f127cb91Sfrankho 				default:
136f127cb91Sfrankho 					/*
137f127cb91Sfrankho 					 * Remove empty suboptions
138f127cb91Sfrankho 					 * (happens on sequences of commas)
139f127cb91Sfrankho 					 */
140f127cb91Sfrankho 					if (*curarg == '\0')
141f127cb91Sfrankho 						break;
142f127cb91Sfrankho 
143f127cb91Sfrankho 					if (append_opt(optbuf, sizeof (optbuf),
144f127cb91Sfrankho 					    curarg) == 0)
145f127cb91Sfrankho 						goto invalarg;
146f127cb91Sfrankho 				}
1477c478bd9Sstevel@tonic-gate 			}
1487c478bd9Sstevel@tonic-gate 			break;
1497c478bd9Sstevel@tonic-gate 		case 'O':
1507c478bd9Sstevel@tonic-gate 			mflg |= MS_OVERLAY;
1517c478bd9Sstevel@tonic-gate 			break;
1527c478bd9Sstevel@tonic-gate 		case 'q':
1537c478bd9Sstevel@tonic-gate 			qflg = 1;
1547c478bd9Sstevel@tonic-gate 			break;
1557c478bd9Sstevel@tonic-gate 		}
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	if (verbose && !error) {
1597c478bd9Sstevel@tonic-gate 		char *optptr;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s", typename);
1627c478bd9Sstevel@tonic-gate 		for (optcnt = 1; optcnt < argc; optcnt++) {
1637c478bd9Sstevel@tonic-gate 			optptr = argv[optcnt];
1647c478bd9Sstevel@tonic-gate 			if (optptr)
1657c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, " %s", optptr);
1667c478bd9Sstevel@tonic-gate 		}
1677c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\n");
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	if (argc - optind != 2 || error) {
1717c478bd9Sstevel@tonic-gate 		/*
1727c478bd9Sstevel@tonic-gate 		 * don't hint at options yet (none are really supported)
1737c478bd9Sstevel@tonic-gate 		 */
1747c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
1757c478bd9Sstevel@tonic-gate 		    "Usage: %s [generic options] [-o suboptions] "
1767c478bd9Sstevel@tonic-gate 		    "special mount_point\n"), typename);
1777c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
1787c478bd9Sstevel@tonic-gate 		    "\tpcfs-specific suboptions are:\n"
179264a6e74Sfrankho 		    "\t     clamptime,noclamptime\n"
180f127cb91Sfrankho 		    "\t     hidden,nohidden\n"
181f127cb91Sfrankho 		    "\t     atime,noatime\n"
182f127cb91Sfrankho 		    "\t     foldcase,nofoldcase\n"
183f127cb91Sfrankho 		    "\t     timezone=<valid TZ string>"));
1847c478bd9Sstevel@tonic-gate 		exit(32);
1857c478bd9Sstevel@tonic-gate 	}
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	mnt_special = argv[optind++];
1887c478bd9Sstevel@tonic-gate 	mnt_mountp = argv[optind++];
1897c478bd9Sstevel@tonic-gate 
190f127cb91Sfrankho 	/*
191f127cb91Sfrankho 	 * Pass timezone information to the kernel module so that
192f127cb91Sfrankho 	 * FAT timestamps, as per spec, can be recorded in local time.
193f127cb91Sfrankho 	 */
194f127cb91Sfrankho 	tzset();
195f127cb91Sfrankho 	/*
196f127cb91Sfrankho 	 * We perform this validation only in case the user of
197*bbf21555SRichard Lowe 	 * mount(8) specified the "timezone=..." option. That's
198f127cb91Sfrankho 	 * because we don't want PCFS mounts to fail due to a
199f127cb91Sfrankho 	 * botched $TZ environment variable. If the admin's
200f127cb91Sfrankho 	 * environment contains garbage, it'll just parse as
201f127cb91Sfrankho 	 * GMT (timezone=0).
202f127cb91Sfrankho 	 */
203f127cb91Sfrankho 	if (tzdone && timezone == 0 && altzone == 0 && daylight == 0 &&
204f127cb91Sfrankho 	    strcmp(tzname[0], tzval) &&
205f127cb91Sfrankho 	    strspn(tzname[1], " ") == strlen(tzname[1])) {
206f127cb91Sfrankho 		goto invalarg;
207f127cb91Sfrankho 	}
208f127cb91Sfrankho 	(void) snprintf(tzstr, 100, "timezone=%d", timezone);
209f127cb91Sfrankho 	tzstr[99] = '\0';
210f127cb91Sfrankho 	if (append_opt(optbuf, sizeof (optbuf), tzstr) == 0)
211f127cb91Sfrankho 		goto invalarg;
212f127cb91Sfrankho 
213f127cb91Sfrankho 	optsize = strlen(optbuf);
214f127cb91Sfrankho 
2157c478bd9Sstevel@tonic-gate 	if (roflag)
2167c478bd9Sstevel@tonic-gate 		mflg |= MS_RDONLY;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	if ((savedoptbuf = strdup(optbuf)) == NULL) {
2197c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: out of memory\n"),
2207c478bd9Sstevel@tonic-gate 		    myname);
2217c478bd9Sstevel@tonic-gate 		exit(2);
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate 	(void) signal(SIGHUP,  SIG_IGN);
2247c478bd9Sstevel@tonic-gate 	(void) signal(SIGQUIT, SIG_IGN);
2257c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT,  SIG_IGN);
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	if (verbose) {
2287c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "mount(%s, \"%s\", %d, %s",
2297c478bd9Sstevel@tonic-gate 		    mnt_special, mnt_mountp, mflg, MNTTYPE_PCFS);
2307c478bd9Sstevel@tonic-gate 	}
231f127cb91Sfrankho 	if (mount(mnt_special, mnt_mountp, mflg, MNTTYPE_PCFS,
232f127cb91Sfrankho 	    NULL, 0, optbuf, MAX_MNTOPT_STR)) {
2337c478bd9Sstevel@tonic-gate 		if (errno == EBUSY) {
2347c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
2357c478bd9Sstevel@tonic-gate 			    "mount: %s is already mounted or %s is busy\n"),
2367c478bd9Sstevel@tonic-gate 			    mnt_special, mnt_mountp);
2377c478bd9Sstevel@tonic-gate 		} else if (errno == EINVAL) {
2387c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
2397c478bd9Sstevel@tonic-gate 			    "mount: %s is not a DOS filesystem.\n"),
2407c478bd9Sstevel@tonic-gate 			    mnt_special);
2417c478bd9Sstevel@tonic-gate 		} else {
2427c478bd9Sstevel@tonic-gate 			perror("mount");
2437c478bd9Sstevel@tonic-gate 		}
2447c478bd9Sstevel@tonic-gate 		exit(32);
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	if (optsize && !qflg)
2487c478bd9Sstevel@tonic-gate 		cmp_requested_to_actual_options(savedoptbuf, optbuf,
2497c478bd9Sstevel@tonic-gate 		    mnt_special, mnt_mountp);
2507c478bd9Sstevel@tonic-gate 	return (0);
251f127cb91Sfrankho 
252f127cb91Sfrankho invalarg:
253f127cb91Sfrankho 	(void) fprintf(stderr,
254f127cb91Sfrankho 	    gettext("%s: Invalid mount options: %s\n"), myname, savedoptarg);
255f127cb91Sfrankho 	return (2);
2567c478bd9Sstevel@tonic-gate }
257