xref: /illumos-gate/usr/src/cmd/fs.d/mount.c (revision 0463c800)
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
5004388ebScasper  * Common Development and Distribution License (the "License").
6004388ebScasper  * 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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
228509e9caSToomas Soome /*	  All Rights Reserved	*/
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
2642d15982SGordon Ross  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
277c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
28*0463c800SGordon Ross  *
29*0463c800SGordon Ross  * Copyright 2019 Nexenta by DDN, Inc. All rights reserved.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include	<stdio.h>
33004388ebScasper #include	<stdio_ext.h>
348509e9caSToomas Soome #include	<limits.h>
358509e9caSToomas Soome #include	<fcntl.h>
368509e9caSToomas Soome #include	<unistd.h>
377c478bd9Sstevel@tonic-gate #include	<stdlib.h>
387c478bd9Sstevel@tonic-gate #include	<string.h>
397c478bd9Sstevel@tonic-gate #include	<stdarg.h>
407c478bd9Sstevel@tonic-gate #include	<sys/types.h>
417c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
427c478bd9Sstevel@tonic-gate #include	<sys/statvfs.h>
437c478bd9Sstevel@tonic-gate #include	<errno.h>
447c478bd9Sstevel@tonic-gate #include	<sys/mnttab.h>
457c478bd9Sstevel@tonic-gate #include	<sys/mntent.h>
467c478bd9Sstevel@tonic-gate #include	<sys/mount.h>
477c478bd9Sstevel@tonic-gate #include	<sys/vfstab.h>
487c478bd9Sstevel@tonic-gate #include	<sys/param.h>
497c478bd9Sstevel@tonic-gate #include	<sys/wait.h>
507c478bd9Sstevel@tonic-gate #include	<sys/signal.h>
517c478bd9Sstevel@tonic-gate #include	<sys/resource.h>
527c478bd9Sstevel@tonic-gate #include	<stropts.h>
537c478bd9Sstevel@tonic-gate #include	<sys/conf.h>
547c478bd9Sstevel@tonic-gate #include	<locale.h>
55*0463c800SGordon Ross #include	<priv.h>
567c478bd9Sstevel@tonic-gate #include	"fslib.h"
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #define	VFS_PATH	"/usr/lib/fs"
597c478bd9Sstevel@tonic-gate #define	ALT_PATH	"/etc/fs"
607c478bd9Sstevel@tonic-gate #define	REMOTE		"/etc/dfs/fstypes"
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #define	ARGV_MAX	16
637c478bd9Sstevel@tonic-gate #define	TIME_MAX	50
647c478bd9Sstevel@tonic-gate #define	FSTYPE_MAX	8
657c478bd9Sstevel@tonic-gate #define	REMOTE_MAX	64
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate #define	OLD	0
687c478bd9Sstevel@tonic-gate #define	NEW	1
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #define	READONLY	0
717c478bd9Sstevel@tonic-gate #define	READWRITE	1
728509e9caSToomas Soome #define	SUID		2
737c478bd9Sstevel@tonic-gate #define	NOSUID		3
748509e9caSToomas Soome #define	SETUID		4
757c478bd9Sstevel@tonic-gate #define	NOSETUID	5
767c478bd9Sstevel@tonic-gate #define	DEVICES		6
777c478bd9Sstevel@tonic-gate #define	NODEVICES	7
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate #define	FORMAT	"%a %b %e %H:%M:%S %Y\n"	/* date time format */
807c478bd9Sstevel@tonic-gate 				/* a - abbreviated weekday name */
817c478bd9Sstevel@tonic-gate 				/* b - abbreviated month name */
827c478bd9Sstevel@tonic-gate 				/* e - day of month */
837c478bd9Sstevel@tonic-gate 				/* H - hour */
847c478bd9Sstevel@tonic-gate 				/* M - minute */
857c478bd9Sstevel@tonic-gate 				/* S - second */
867c478bd9Sstevel@tonic-gate 				/* Y - Year */
877c478bd9Sstevel@tonic-gate 				/* n - newline */
887c478bd9Sstevel@tonic-gate 
89a38ddfeeSTim Haley /*
90a38ddfeeSTim Haley  * The fs-local method understands this exit code to mean that one or
91a38ddfeeSTim Haley  * more failures occurred and that all the failures were of attempted
92a38ddfeeSTim Haley  * lofs mounts.
93a38ddfeeSTim Haley  */
94a38ddfeeSTim Haley #define	ALL_LOFS_FAILURES	111
95a38ddfeeSTim Haley 
967c478bd9Sstevel@tonic-gate extern int	optind;
977c478bd9Sstevel@tonic-gate extern char	*optarg;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate extern void	usage(void);
1007c478bd9Sstevel@tonic-gate extern char	*flags(char *, int);
1017c478bd9Sstevel@tonic-gate extern char	*remote(char *, FILE *);
1027c478bd9Sstevel@tonic-gate extern char	*default_fstype(char *);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate char	*myopts[] = {
1057c478bd9Sstevel@tonic-gate 	MNTOPT_RO,
1067c478bd9Sstevel@tonic-gate 	MNTOPT_RW,
1077c478bd9Sstevel@tonic-gate 	MNTOPT_SUID,
1087c478bd9Sstevel@tonic-gate 	MNTOPT_NOSUID,
1097c478bd9Sstevel@tonic-gate 	MNTOPT_SETUID,
1107c478bd9Sstevel@tonic-gate 	MNTOPT_NOSETUID,
1117c478bd9Sstevel@tonic-gate 	MNTOPT_DEVICES,
1127c478bd9Sstevel@tonic-gate 	MNTOPT_NODEVICES,
1137c478bd9Sstevel@tonic-gate 	NULL
1147c478bd9Sstevel@tonic-gate };
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate static char	*myname;		/* point to argv[0] */
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate /*
1197c478bd9Sstevel@tonic-gate  * Set the limit to double the number of characters a user should be allowed to
1207c478bd9Sstevel@tonic-gate  * type in one line.
1217c478bd9Sstevel@tonic-gate  * This should cover the different shells, which don't use POSIX_MAX_INPUT,
1227c478bd9Sstevel@tonic-gate  * and should cover the case where a long option string can be in
1237c478bd9Sstevel@tonic-gate  * the /etc/vfstab file.
1247c478bd9Sstevel@tonic-gate  */
1257c478bd9Sstevel@tonic-gate char	mntflags[(_POSIX_MAX_INPUT+1) * 2];
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate char	realdir[MAXPATHLEN];	/* buffer for realpath() calls */
1287c478bd9Sstevel@tonic-gate char	*vfstab = VFSTAB;
1297c478bd9Sstevel@tonic-gate char	*mnttab = MNTTAB;
1307c478bd9Sstevel@tonic-gate char	*specific_opts;		/* holds specific mount options */
1317c478bd9Sstevel@tonic-gate char	*generic_opts;		/* holds generic mount options */
1327c478bd9Sstevel@tonic-gate int	maxrun;
1337c478bd9Sstevel@tonic-gate int	nrun;
134a38ddfeeSTim Haley int	failcnt;		/* total count of failures */
1357c478bd9Sstevel@tonic-gate int	lofscnt;		/* presence of lofs prohibits parallel */
1367c478bd9Sstevel@tonic-gate 				/* mounting */
137a38ddfeeSTim Haley int	lofsfail;		/* count of failures of lofs mounts */
1387c478bd9Sstevel@tonic-gate int	exitcode;
1397c478bd9Sstevel@tonic-gate int	aflg, cflg, fflg, Fflg, gflg, oflg, pflg, rflg, vflg, Vflg, mflg, Oflg,
1407c478bd9Sstevel@tonic-gate 	dashflg, questflg, dflg, qflg;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate /*
1447c478bd9Sstevel@tonic-gate  * Each vfsent_t describes a vfstab entry.  It is used to manage and cleanup
1457c478bd9Sstevel@tonic-gate  * each child that performs the particular mount for the entry.
1467c478bd9Sstevel@tonic-gate  */
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate typedef struct vfsent {
1497c478bd9Sstevel@tonic-gate 	struct vfstab	v;		/* the vfstab entry */
1507c478bd9Sstevel@tonic-gate 	char		*rpath;		/* resolved pathname so far */
1517c478bd9Sstevel@tonic-gate 	int		mlevel;		/* how deep is this mount point */
1527c478bd9Sstevel@tonic-gate 	int		order;		/* vfstab serial order of this vfs */
1537c478bd9Sstevel@tonic-gate 	int		flag;
1547c478bd9Sstevel@tonic-gate 	pid_t		pid;		/* the pid of this mount process */
1557c478bd9Sstevel@tonic-gate 	int		exitcode;	/* process's exitcode */
1567c478bd9Sstevel@tonic-gate #define	RDPIPE		0
1577c478bd9Sstevel@tonic-gate #define	WRPIPE		1
1587c478bd9Sstevel@tonic-gate 	int		sopipe[2];	/* pipe attached to child's stdout */
1597c478bd9Sstevel@tonic-gate 	int		sepipe[2];	/* pipe attached to child's stderr */
1607c478bd9Sstevel@tonic-gate 	struct vfsent	*next;		/* used when in linked list */
1617c478bd9Sstevel@tonic-gate } vfsent_t;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate #define	VRPFAILED	0x01		/* most recent realpath failed on */
1647c478bd9Sstevel@tonic-gate 					/* this mount point */
1657c478bd9Sstevel@tonic-gate #define	VNOTMOUNTED	0x02		/* mount point could not be mounted */
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate vfsent_t	*vfsll, *vfslltail;	/* head and tail of the global */
1687c478bd9Sstevel@tonic-gate 					/* linked list of vfstab entries */
1697c478bd9Sstevel@tonic-gate vfsent_t	**vfsarray;		/* global array of vfsent_t's */
1707c478bd9Sstevel@tonic-gate int		vfsarraysize;		/* length of the list */
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate /*
1737c478bd9Sstevel@tonic-gate  * This structure is used to build a linked list of
1747c478bd9Sstevel@tonic-gate  * mnttab structures from /etc/mnttab.
1757c478bd9Sstevel@tonic-gate  */
1767c478bd9Sstevel@tonic-gate typedef struct mountent {
1777c478bd9Sstevel@tonic-gate 	struct extmnttab	*ment;
1787c478bd9Sstevel@tonic-gate 	int		flag;
1797c478bd9Sstevel@tonic-gate 	struct mountent	*next;
1807c478bd9Sstevel@tonic-gate } mountent_t;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate #define	MSORTED		0x1
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate static vfsent_t **make_vfsarray(char **, int);
1857c478bd9Sstevel@tonic-gate static vfsent_t	*new_vfsent(struct vfstab *, int);
1867c478bd9Sstevel@tonic-gate static vfsent_t *getvfsall(char *, int);
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate static void	doexec(char *, char **);
1897c478bd9Sstevel@tonic-gate static void	nomem();
1907c478bd9Sstevel@tonic-gate static void	cleanup(int);
1917c478bd9Sstevel@tonic-gate static char	*setrpath(vfsent_t *);
1927c478bd9Sstevel@tonic-gate static int	dowait();
1937c478bd9Sstevel@tonic-gate static int	setup_iopipe(vfsent_t *);
1947c478bd9Sstevel@tonic-gate static void	setup_output(vfsent_t *);
1957c478bd9Sstevel@tonic-gate static void	doio(vfsent_t *);
1967c478bd9Sstevel@tonic-gate static void	do_mounts();
1977c478bd9Sstevel@tonic-gate static int	parmount(char **, int, char *);
1987c478bd9Sstevel@tonic-gate static int	mlevelcmp(const void *, const void *);
1997c478bd9Sstevel@tonic-gate static int	mordercmp(const void *, const void *);
2007c478bd9Sstevel@tonic-gate static int	check_fields(char *, char *);
2017c478bd9Sstevel@tonic-gate static int	cleanupkid(pid_t, int);
2027c478bd9Sstevel@tonic-gate static void	print_mnttab(int, int);
2037c478bd9Sstevel@tonic-gate static void	vfserror(int, char *);
2047c478bd9Sstevel@tonic-gate static void	mnterror(int);
2057c478bd9Sstevel@tonic-gate static int	ignore(char *);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate /*
2087c478bd9Sstevel@tonic-gate  * This is /usr/sbin/mount: the generic command that in turn
2097c478bd9Sstevel@tonic-gate  * execs the appropriate /usr/lib/fs/{fstype}/mount.
2107c478bd9Sstevel@tonic-gate  * The -F flag and argument are NOT passed.
2117c478bd9Sstevel@tonic-gate  * If the usr file system is not mounted a duplicate copy
2127c478bd9Sstevel@tonic-gate  * can be found in /sbin and this version execs the
2137c478bd9Sstevel@tonic-gate  * appropriate /etc/fs/{fstype}/mount
2147c478bd9Sstevel@tonic-gate  *
2157c478bd9Sstevel@tonic-gate  * If the -F fstype, special or directory are missing,
2167c478bd9Sstevel@tonic-gate  * /etc/vfstab is searched to fill in the missing arguments.
2177c478bd9Sstevel@tonic-gate  *
2187c478bd9Sstevel@tonic-gate  * -V will print the built command on the stdout.
2197c478bd9Sstevel@tonic-gate  * It isn't passed either.
2207c478bd9Sstevel@tonic-gate  */
22108190127Sdh int
22208190127Sdh main(int argc, char *argv[])
2237c478bd9Sstevel@tonic-gate {
2244bff34e3Sthurlow 	char	*special,	/* argument of special/resource */
2254bff34e3Sthurlow 	    *mountp,		/* argument of mount directory */
2264bff34e3Sthurlow 	    *fstype,		/* wherein the fstype name is filled */
2274bff34e3Sthurlow 	    *newargv[ARGV_MAX],	/* arg list for specific command */
2284bff34e3Sthurlow 	    *farg = NULL, *Farg = NULL;
2297c478bd9Sstevel@tonic-gate 	int	ii, ret, cc, fscnt;
2307c478bd9Sstevel@tonic-gate 	struct stat64	stbuf;
2317c478bd9Sstevel@tonic-gate 	struct vfstab	vget, vref;
2327c478bd9Sstevel@tonic-gate 	mode_t mode;
2337c478bd9Sstevel@tonic-gate 	FILE	*fd;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
2387c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
2397c478bd9Sstevel@tonic-gate #endif
2407c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	myname = strrchr(argv[0], '/');
2437c478bd9Sstevel@tonic-gate 	if (myname)
2447c478bd9Sstevel@tonic-gate 		myname++;
2457c478bd9Sstevel@tonic-gate 	else
2467c478bd9Sstevel@tonic-gate 		myname = argv[0];
2477c478bd9Sstevel@tonic-gate 	if (myname == 0) myname = "path unknown";
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	/* Process the args.  */
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	while ((cc = getopt(argc, argv, "?acd:f:F:gmno:pqrvVO")) != -1)
2527c478bd9Sstevel@tonic-gate 		switch (cc) {
2537c478bd9Sstevel@tonic-gate 			case 'a':
2547c478bd9Sstevel@tonic-gate 				aflg++;
2557c478bd9Sstevel@tonic-gate 				break;
2567c478bd9Sstevel@tonic-gate 			case 'c':
2577c478bd9Sstevel@tonic-gate 				cflg++;
2587c478bd9Sstevel@tonic-gate 				break;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate #ifdef DEBUG
2617c478bd9Sstevel@tonic-gate 			case 'd':
2627c478bd9Sstevel@tonic-gate 				dflg = atoi(optarg);
2637c478bd9Sstevel@tonic-gate 				break;
2647c478bd9Sstevel@tonic-gate #endif
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 			case 'f':
2677c478bd9Sstevel@tonic-gate 				fflg++;
2687c478bd9Sstevel@tonic-gate 				farg = optarg;
2697c478bd9Sstevel@tonic-gate 				break;
2707c478bd9Sstevel@tonic-gate 			case 'F':
2717c478bd9Sstevel@tonic-gate 				Fflg++;
2727c478bd9Sstevel@tonic-gate 				Farg = optarg;
2737c478bd9Sstevel@tonic-gate 				break;
2747c478bd9Sstevel@tonic-gate 			case 'g':
2757c478bd9Sstevel@tonic-gate 				gflg++;
2767c478bd9Sstevel@tonic-gate 				break;
2777c478bd9Sstevel@tonic-gate 			case 'm':
2787c478bd9Sstevel@tonic-gate 				mflg++;
2797c478bd9Sstevel@tonic-gate 				break; /* do not update /etc/mnttab */
2807c478bd9Sstevel@tonic-gate 			case 'o':
2817c478bd9Sstevel@tonic-gate 				oflg++;
2827c478bd9Sstevel@tonic-gate 				if ((specific_opts = strdup(optarg)) == NULL)
2837c478bd9Sstevel@tonic-gate 					nomem();
2847c478bd9Sstevel@tonic-gate 				break; /* fstype dependent options */
2857c478bd9Sstevel@tonic-gate 			case 'O':
2867c478bd9Sstevel@tonic-gate 				Oflg++;
2877c478bd9Sstevel@tonic-gate 				break;
2887c478bd9Sstevel@tonic-gate 			case 'p':
2897c478bd9Sstevel@tonic-gate 				pflg++;
2907c478bd9Sstevel@tonic-gate 				break;
2917c478bd9Sstevel@tonic-gate 			case 'q':
2927c478bd9Sstevel@tonic-gate 				qflg++;
2937c478bd9Sstevel@tonic-gate 				break;
2947c478bd9Sstevel@tonic-gate 			case 'r':
2957c478bd9Sstevel@tonic-gate 				rflg++;
2967c478bd9Sstevel@tonic-gate 				generic_opts = "ro";
2977c478bd9Sstevel@tonic-gate 				break;
2987c478bd9Sstevel@tonic-gate 			case 'v':
2997c478bd9Sstevel@tonic-gate 				vflg++;
3007c478bd9Sstevel@tonic-gate 				break;
3017c478bd9Sstevel@tonic-gate 			case 'V':
3027c478bd9Sstevel@tonic-gate 				Vflg++;
3037c478bd9Sstevel@tonic-gate 				break;
3047c478bd9Sstevel@tonic-gate 			case '?':
3057c478bd9Sstevel@tonic-gate 				questflg++;
3067c478bd9Sstevel@tonic-gate 				break;
3077c478bd9Sstevel@tonic-gate 		}
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	/* copy '--' to specific */
3107c478bd9Sstevel@tonic-gate 	if (strcmp(argv[optind-1], "--") == 0)
3117c478bd9Sstevel@tonic-gate 		dashflg++;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	/* option checking */
3147c478bd9Sstevel@tonic-gate 	/* more than two args not allowed if !aflg */
3157c478bd9Sstevel@tonic-gate 	if (!aflg && (argc - optind > 2))
3167c478bd9Sstevel@tonic-gate 		usage();
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	/* pv mututally exclusive */
3197c478bd9Sstevel@tonic-gate 	if (pflg + vflg + aflg > 1) {
3207c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext
3214bff34e3Sthurlow 		    ("%s: -a, -p, and -v are mutually exclusive\n"),
3224bff34e3Sthurlow 		    myname);
3237c478bd9Sstevel@tonic-gate 		usage();
3247c478bd9Sstevel@tonic-gate 	}
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	/*
3277c478bd9Sstevel@tonic-gate 	 * Can't have overlaying mounts on the same mount point during
3287c478bd9Sstevel@tonic-gate 	 * a parallel mount.
3297c478bd9Sstevel@tonic-gate 	 */
3307c478bd9Sstevel@tonic-gate 	if (aflg && Oflg) {
3317c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext
3324bff34e3Sthurlow 		    ("%s: -a and -O are mutually exclusive\n"), myname);
3337c478bd9Sstevel@tonic-gate 		usage();
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	/* dfF mutually exclusive */
3377c478bd9Sstevel@tonic-gate 	if (fflg + Fflg > 1) {
3387c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext
3394bff34e3Sthurlow 		    ("%s: More than one FSType specified\n"), myname);
3407c478bd9Sstevel@tonic-gate 		usage();
3417c478bd9Sstevel@tonic-gate 	}
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	/* no arguments, only allow p,v,V or [F]? */
3447c478bd9Sstevel@tonic-gate 	if (!aflg && optind == argc) {
3457c478bd9Sstevel@tonic-gate 		if (cflg || fflg || mflg || oflg || rflg || qflg)
3467c478bd9Sstevel@tonic-gate 			usage();
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 		if (Fflg && !questflg)
3497c478bd9Sstevel@tonic-gate 			usage();
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 		if (questflg) {
3527c478bd9Sstevel@tonic-gate 			if (Fflg) {
3537c478bd9Sstevel@tonic-gate 				newargv[2] = "-?";
3547c478bd9Sstevel@tonic-gate 				newargv[3] = NULL;
3557c478bd9Sstevel@tonic-gate 				doexec(Farg, newargv);
3567c478bd9Sstevel@tonic-gate 			}
3577c478bd9Sstevel@tonic-gate 			usage();
3587c478bd9Sstevel@tonic-gate 		}
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	if (questflg)
3627c478bd9Sstevel@tonic-gate 		usage();
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	/* one or two args, allow any but p,v */
3657c478bd9Sstevel@tonic-gate 	if (optind != argc && (pflg || vflg)) {
3667c478bd9Sstevel@tonic-gate 		fprintf(stderr,
3677c478bd9Sstevel@tonic-gate gettext("%s: Cannot use -p and -v with arguments\n"), myname);
3687c478bd9Sstevel@tonic-gate 		usage();
3697c478bd9Sstevel@tonic-gate 	}
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	/* if only reporting mnttab, generic prints mnttab and exits */
3737c478bd9Sstevel@tonic-gate 	if (!aflg && optind == argc) {
3747c478bd9Sstevel@tonic-gate 		if (Vflg) {
3757c478bd9Sstevel@tonic-gate 			printf("%s", myname);
3767c478bd9Sstevel@tonic-gate 			if (pflg)
3777c478bd9Sstevel@tonic-gate 				printf(" -p");
3787c478bd9Sstevel@tonic-gate 			if (vflg)
3797c478bd9Sstevel@tonic-gate 				printf(" -v");
3807c478bd9Sstevel@tonic-gate 			printf("\n");
3817c478bd9Sstevel@tonic-gate 			exit(0);
3827c478bd9Sstevel@tonic-gate 		}
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 		print_mnttab(vflg, pflg);
3857c478bd9Sstevel@tonic-gate 		exit(0);
3867c478bd9Sstevel@tonic-gate 	}
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	/*
3897c478bd9Sstevel@tonic-gate 	 * Get filesystem type here.  If "-F FStype" is specified, use
3907c478bd9Sstevel@tonic-gate 	 * that fs type.  Otherwise, determine the fs type from /etc/vfstab
3917c478bd9Sstevel@tonic-gate 	 * if the entry exists.  Otherwise, determine the local or remote
3927c478bd9Sstevel@tonic-gate 	 * fs type from /etc/default/df or /etc/dfs/fstypes respectively.
3937c478bd9Sstevel@tonic-gate 	 */
3947c478bd9Sstevel@tonic-gate 	if (fflg) {
3957c478bd9Sstevel@tonic-gate 		if ((strcmp(farg, "S51K") != 0) &&
3967c478bd9Sstevel@tonic-gate 		    (strcmp(farg, "S52K") != 0)) {
3977c478bd9Sstevel@tonic-gate 			fstype = farg;
3987c478bd9Sstevel@tonic-gate 		}
3997c478bd9Sstevel@tonic-gate 		else
4007c478bd9Sstevel@tonic-gate 			fstype = "ufs";
4017c478bd9Sstevel@tonic-gate 	} else /* if (Fflg) */
4027c478bd9Sstevel@tonic-gate 		fstype = Farg;
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	fscnt = argc - optind;
4057c478bd9Sstevel@tonic-gate 	if (aflg && (fscnt != 1))
4067c478bd9Sstevel@tonic-gate 		exit(parmount(argv + optind, fscnt, fstype));
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	/*
4097c478bd9Sstevel@tonic-gate 	 * Then don't bother with the parallel over head.  Everything
4107c478bd9Sstevel@tonic-gate 	 * from this point is simple/normal single execution.
4117c478bd9Sstevel@tonic-gate 	 */
4127c478bd9Sstevel@tonic-gate 	aflg = 0;
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	/* get special and/or mount-point from arg(s) */
4157c478bd9Sstevel@tonic-gate 	if (fscnt == 2)
4167c478bd9Sstevel@tonic-gate 		special = argv[optind++];
4177c478bd9Sstevel@tonic-gate 	else
4187c478bd9Sstevel@tonic-gate 		special = NULL;
4197c478bd9Sstevel@tonic-gate 	if (optind < argc)
4207c478bd9Sstevel@tonic-gate 		mountp = argv[optind++];
4217c478bd9Sstevel@tonic-gate 	else
4227c478bd9Sstevel@tonic-gate 		mountp = NULL;
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	/* lookup only if we need to */
4257c478bd9Sstevel@tonic-gate 	if (fstype == NULL || specific_opts == NULL || special == NULL ||
4267c478bd9Sstevel@tonic-gate 	    mountp == NULL) {
4277c478bd9Sstevel@tonic-gate 		if ((fd = fopen(vfstab, "r")) == NULL) {
4287c478bd9Sstevel@tonic-gate 			if (fstype == NULL || special == NULL ||
4294bff34e3Sthurlow 			    mountp == NULL) {
4307c478bd9Sstevel@tonic-gate 				fprintf(stderr, gettext(
4314bff34e3Sthurlow 				    "%s: Cannot open %s\n"),
4324bff34e3Sthurlow 				    myname, vfstab);
4337c478bd9Sstevel@tonic-gate 				exit(1);
4347c478bd9Sstevel@tonic-gate 			} else {
4357c478bd9Sstevel@tonic-gate 				/*
4367c478bd9Sstevel@tonic-gate 				 * No vfstab, but we know what we want
4377c478bd9Sstevel@tonic-gate 				 * to mount.
4387c478bd9Sstevel@tonic-gate 				 */
4397c478bd9Sstevel@tonic-gate 				goto out;
4407c478bd9Sstevel@tonic-gate 			}
4417c478bd9Sstevel@tonic-gate 		}
4427c478bd9Sstevel@tonic-gate 		vfsnull(&vref);
4437c478bd9Sstevel@tonic-gate 		vref.vfs_special = special;
4447c478bd9Sstevel@tonic-gate 		vref.vfs_mountp = mountp;
4457c478bd9Sstevel@tonic-gate 		vref.vfs_fstype = fstype;
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 		/* get a vfstab entry matching mountp or special */
4487c478bd9Sstevel@tonic-gate 		while ((ret = getvfsany(fd, &vget, &vref)) > 0)
4497c478bd9Sstevel@tonic-gate 			vfserror(ret, vget.vfs_special);
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 		/* if no entry and there was only one argument */
4527c478bd9Sstevel@tonic-gate 		/* then the argument could be the special */
4537c478bd9Sstevel@tonic-gate 		/* and not mount point as we thought earlier */
4547c478bd9Sstevel@tonic-gate 		if (ret == -1 && special == NULL) {
4557c478bd9Sstevel@tonic-gate 			rewind(fd);
4567c478bd9Sstevel@tonic-gate 			special = vref.vfs_special = mountp;
4577c478bd9Sstevel@tonic-gate 			mountp = vref.vfs_mountp = NULL;
4587c478bd9Sstevel@tonic-gate 			/* skip erroneous lines; they were reported above */
4594bff34e3Sthurlow 			while ((ret = getvfsany(fd, &vget, &vref)) > 0)
4604bff34e3Sthurlow 				;
4617c478bd9Sstevel@tonic-gate 		}
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 		fclose(fd);
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 		if (ret == 0) {
4667c478bd9Sstevel@tonic-gate 			if (fstype == NULL)
4677c478bd9Sstevel@tonic-gate 				fstype = vget.vfs_fstype;
4687c478bd9Sstevel@tonic-gate 			if (special == NULL)
4697c478bd9Sstevel@tonic-gate 				special = vget.vfs_special;
4707c478bd9Sstevel@tonic-gate 			if (mountp == NULL)
4717c478bd9Sstevel@tonic-gate 				mountp = vget.vfs_mountp;
4727c478bd9Sstevel@tonic-gate 			if (oflg == 0 && vget.vfs_mntopts) {
4737c478bd9Sstevel@tonic-gate 				oflg++;
4747c478bd9Sstevel@tonic-gate 				specific_opts = vget.vfs_mntopts;
4757c478bd9Sstevel@tonic-gate 			}
4767c478bd9Sstevel@tonic-gate 		} else if (special == NULL) {
4777c478bd9Sstevel@tonic-gate 			if (stat64(mountp, &stbuf) == -1) {
4787c478bd9Sstevel@tonic-gate 				fprintf(stderr, gettext("%s: cannot stat %s\n"),
4794bff34e3Sthurlow 				    myname, mountp);
4807c478bd9Sstevel@tonic-gate 				exit(2);
4817c478bd9Sstevel@tonic-gate 			}
4827c478bd9Sstevel@tonic-gate 			if (((mode = (stbuf.st_mode & S_IFMT)) == S_IFBLK) ||
4834bff34e3Sthurlow 			    (mode == S_IFCHR)) {
4847c478bd9Sstevel@tonic-gate 				fprintf(stderr,
4857c478bd9Sstevel@tonic-gate gettext("%s: mount point cannot be determined\n"),
4864bff34e3Sthurlow 				    myname);
4877c478bd9Sstevel@tonic-gate 				exit(1);
4887c478bd9Sstevel@tonic-gate 			} else
4897c478bd9Sstevel@tonic-gate 				{
4907c478bd9Sstevel@tonic-gate 				fprintf(stderr,
4917c478bd9Sstevel@tonic-gate gettext("%s: special cannot be determined\n"),
4924bff34e3Sthurlow 				    myname);
4937c478bd9Sstevel@tonic-gate 				exit(1);
4947c478bd9Sstevel@tonic-gate 			}
4957c478bd9Sstevel@tonic-gate 		} else if (fstype == NULL)
4967c478bd9Sstevel@tonic-gate 			fstype = default_fstype(special);
4977c478bd9Sstevel@tonic-gate 	}
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate out:
5007c478bd9Sstevel@tonic-gate 	if (realpath(mountp, realdir) == NULL) {
5017c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "mount: ");
5027c478bd9Sstevel@tonic-gate 		perror(mountp);
5037c478bd9Sstevel@tonic-gate 		exit(1);
5047c478bd9Sstevel@tonic-gate 	}
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	if ((mountp = strdup(realdir)) == NULL)
5077c478bd9Sstevel@tonic-gate 		nomem();
5087c478bd9Sstevel@tonic-gate 
509eb36692eSDan Kruchinin 	if (check_fields(fstype, mountp))
510eb36692eSDan Kruchinin 		exit(1);
511eb36692eSDan Kruchinin 
5127c478bd9Sstevel@tonic-gate 	/* create the new arg list, and end the list with a null pointer */
5137c478bd9Sstevel@tonic-gate 	ii = 2;
5147c478bd9Sstevel@tonic-gate 	if (cflg)
5157c478bd9Sstevel@tonic-gate 		newargv[ii++] = "-c";
5167c478bd9Sstevel@tonic-gate 	if (gflg)
5177c478bd9Sstevel@tonic-gate 		newargv[ii++] = "-g";
5187c478bd9Sstevel@tonic-gate 	if (mflg)
5197c478bd9Sstevel@tonic-gate 		newargv[ii++] = "-m";
5207c478bd9Sstevel@tonic-gate 	/*
5217c478bd9Sstevel@tonic-gate 	 * The q option needs to go before the -o option as some
5227c478bd9Sstevel@tonic-gate 	 * filesystems complain during first pass option parsing.
5237c478bd9Sstevel@tonic-gate 	 */
5247c478bd9Sstevel@tonic-gate 	if (qflg)
5257c478bd9Sstevel@tonic-gate 		newargv[ii++] = "-q";
5267c478bd9Sstevel@tonic-gate 	if (oflg) {
5277c478bd9Sstevel@tonic-gate 		newargv[ii++] = "-o";
5287c478bd9Sstevel@tonic-gate 		newargv[ii++] = specific_opts;
5297c478bd9Sstevel@tonic-gate 	}
5307c478bd9Sstevel@tonic-gate 	if (Oflg)
5317c478bd9Sstevel@tonic-gate 		newargv[ii++] = "-O";
5327c478bd9Sstevel@tonic-gate 	if (rflg)
5337c478bd9Sstevel@tonic-gate 		newargv[ii++] = "-r";
5347c478bd9Sstevel@tonic-gate 	if (dashflg)
5357c478bd9Sstevel@tonic-gate 		newargv[ii++] = "--";
5367c478bd9Sstevel@tonic-gate 	newargv[ii++] = special;
5377c478bd9Sstevel@tonic-gate 	newargv[ii++] = mountp;
5387c478bd9Sstevel@tonic-gate 	newargv[ii] = NULL;
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	doexec(fstype, newargv);
54108190127Sdh 	return (0);
5427c478bd9Sstevel@tonic-gate }
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate void
54508190127Sdh usage(void)
5467c478bd9Sstevel@tonic-gate {
5477c478bd9Sstevel@tonic-gate 	fprintf(stderr,	gettext("Usage:\n%s [-v | -p]\n"), myname);
5487c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext(
5494bff34e3Sthurlow 	    "%s [-F FSType] [-V] [current_options] [-o specific_options]"),
5504bff34e3Sthurlow 	    myname);
5517c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext("\n\t{special | mount_point}\n"));
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext(
5544bff34e3Sthurlow 	    "%s [-F FSType] [-V] [current_options] [-o specific_options]"),
5554bff34e3Sthurlow 	    myname);
5567c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext("\n\tspecial mount_point\n"));
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext(
5597c478bd9Sstevel@tonic-gate 	"%s -a [-F FSType ] [-V] [current_options] [-o specific_options]\n"),
5604bff34e3Sthurlow 	    myname);
5617c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext("\t[mount_point ...]\n"));
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	exit(1);
5647c478bd9Sstevel@tonic-gate }
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate /*
5677c478bd9Sstevel@tonic-gate  * Get rid of "dev=[hex string]" clause, if any.  It's not legal
5687c478bd9Sstevel@tonic-gate  * when printing in vfstab format.
5697c478bd9Sstevel@tonic-gate  */
5707c478bd9Sstevel@tonic-gate void
5717c478bd9Sstevel@tonic-gate elide_dev(char *mntopts)
5727c478bd9Sstevel@tonic-gate {
5737c478bd9Sstevel@tonic-gate 	char *dev, *other;
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	if (mntopts != NULL) {
5767c478bd9Sstevel@tonic-gate 		dev = strstr(mntopts, "dev=");
5777c478bd9Sstevel@tonic-gate 		if (dev != NULL) {
5787c478bd9Sstevel@tonic-gate 			other = strpbrk(dev, ",");
5797c478bd9Sstevel@tonic-gate 			if (other == NULL) {
5807c478bd9Sstevel@tonic-gate 				/* last option */
5817c478bd9Sstevel@tonic-gate 				if (dev != mntopts) {
5827c478bd9Sstevel@tonic-gate 					*--dev = '\0';
5837c478bd9Sstevel@tonic-gate 				} else {
5847c478bd9Sstevel@tonic-gate 					*dev = '\0';
5857c478bd9Sstevel@tonic-gate 				}
5867c478bd9Sstevel@tonic-gate 			} else {
5877c478bd9Sstevel@tonic-gate 				/* first or intermediate option */
5887c478bd9Sstevel@tonic-gate 				memmove(dev, other+1, strlen(other+1)+1);
5897c478bd9Sstevel@tonic-gate 			}
5907c478bd9Sstevel@tonic-gate 		}
5917c478bd9Sstevel@tonic-gate 	}
5927c478bd9Sstevel@tonic-gate }
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate void
59508190127Sdh print_mnttab(int vflg, int pflg)
5967c478bd9Sstevel@tonic-gate {
5977c478bd9Sstevel@tonic-gate 	FILE	*fd;
5987c478bd9Sstevel@tonic-gate 	FILE	*rfp;			/* this will be NULL if fopen fails */
5997c478bd9Sstevel@tonic-gate 	int	ret;
6007c478bd9Sstevel@tonic-gate 	char	time_buf[TIME_MAX];	/* array to hold date and time */
6017c478bd9Sstevel@tonic-gate 	struct extmnttab	mget;
6027c478bd9Sstevel@tonic-gate 	time_t	ltime;
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	if ((fd = fopen(mnttab, "r")) == NULL) {
6057c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: Cannot open mnttab\n"), myname);
6067c478bd9Sstevel@tonic-gate 		exit(1);
6077c478bd9Sstevel@tonic-gate 	}
6087c478bd9Sstevel@tonic-gate 	rfp = fopen(REMOTE, "r");
6097c478bd9Sstevel@tonic-gate 	while ((ret = getextmntent(fd, &mget, sizeof (struct extmnttab)))
6104bff34e3Sthurlow 	    == 0) {
6117c478bd9Sstevel@tonic-gate 		if (ignore(mget.mnt_mntopts))
6127c478bd9Sstevel@tonic-gate 			continue;
6137c478bd9Sstevel@tonic-gate 		if (mget.mnt_special && mget.mnt_mountp &&
6147c478bd9Sstevel@tonic-gate 		    mget.mnt_fstype && mget.mnt_time) {
6157c478bd9Sstevel@tonic-gate 			ltime = atol(mget.mnt_time);
6167c478bd9Sstevel@tonic-gate 			cftime(time_buf, FORMAT, &ltime);
6177c478bd9Sstevel@tonic-gate 			if (pflg) {
6187c478bd9Sstevel@tonic-gate 				elide_dev(mget.mnt_mntopts);
6197c478bd9Sstevel@tonic-gate 				printf("%s - %s %s - no %s\n",
6204bff34e3Sthurlow 				    mget.mnt_special,
6214bff34e3Sthurlow 				    mget.mnt_mountp,
6224bff34e3Sthurlow 				    mget.mnt_fstype,
6234bff34e3Sthurlow 				    mget.mnt_mntopts != NULL ?
6244bff34e3Sthurlow 				    mget.mnt_mntopts : "-");
6257c478bd9Sstevel@tonic-gate 			} else if (vflg) {
6267c478bd9Sstevel@tonic-gate 				printf("%s on %s type %s %s%s on %s",
6274bff34e3Sthurlow 				    mget.mnt_special,
6284bff34e3Sthurlow 				    mget.mnt_mountp,
6294bff34e3Sthurlow 				    mget.mnt_fstype,
6304bff34e3Sthurlow 				    remote(mget.mnt_fstype, rfp),
6314bff34e3Sthurlow 				    flags(mget.mnt_mntopts, NEW),
6324bff34e3Sthurlow 				    time_buf);
6337c478bd9Sstevel@tonic-gate 			} else
6347c478bd9Sstevel@tonic-gate 				printf("%s on %s %s%s on %s",
6354bff34e3Sthurlow 				    mget.mnt_mountp,
6364bff34e3Sthurlow 				    mget.mnt_special,
6374bff34e3Sthurlow 				    remote(mget.mnt_fstype, rfp),
6384bff34e3Sthurlow 				    flags(mget.mnt_mntopts, OLD),
6394bff34e3Sthurlow 				    time_buf);
6407c478bd9Sstevel@tonic-gate 		}
6417c478bd9Sstevel@tonic-gate 	}
6427c478bd9Sstevel@tonic-gate 	if (ret > 0)
6437c478bd9Sstevel@tonic-gate 		mnterror(ret);
6447c478bd9Sstevel@tonic-gate }
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate char	*
64708190127Sdh flags(char *mntopts, int flag)
6487c478bd9Sstevel@tonic-gate {
6497c478bd9Sstevel@tonic-gate 	char	opts[sizeof (mntflags)];
6507c478bd9Sstevel@tonic-gate 	char	*value;
6517c478bd9Sstevel@tonic-gate 	int	rdwr = 1;
6527c478bd9Sstevel@tonic-gate 	int	suid = 1;
6537c478bd9Sstevel@tonic-gate 	int	devices = 1;
6547c478bd9Sstevel@tonic-gate 	int	setuid = 1;
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	if (mntopts == NULL || *mntopts == '\0')
6577c478bd9Sstevel@tonic-gate 		return ("read/write/setuid/devices");
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	strcpy(opts, "");
6607c478bd9Sstevel@tonic-gate 	while (*mntopts != '\0')  {
6617c478bd9Sstevel@tonic-gate 		switch (getsubopt(&mntopts, myopts, &value)) {
6627c478bd9Sstevel@tonic-gate 		case READONLY:
6637c478bd9Sstevel@tonic-gate 			rdwr = 0;
6647c478bd9Sstevel@tonic-gate 			break;
6657c478bd9Sstevel@tonic-gate 		case READWRITE:
6667c478bd9Sstevel@tonic-gate 			rdwr = 1;
6677c478bd9Sstevel@tonic-gate 			break;
6687c478bd9Sstevel@tonic-gate 		case SUID:
6697c478bd9Sstevel@tonic-gate 			suid = 1;
6707c478bd9Sstevel@tonic-gate 			break;
6717c478bd9Sstevel@tonic-gate 		case NOSUID:
6727c478bd9Sstevel@tonic-gate 			suid = 0;
6737c478bd9Sstevel@tonic-gate 			break;
6747c478bd9Sstevel@tonic-gate 		case SETUID:
6757c478bd9Sstevel@tonic-gate 			setuid = 1;
6767c478bd9Sstevel@tonic-gate 			break;
6777c478bd9Sstevel@tonic-gate 		case NOSETUID:
6787c478bd9Sstevel@tonic-gate 			setuid = 0;
6797c478bd9Sstevel@tonic-gate 			break;
6807c478bd9Sstevel@tonic-gate 		case DEVICES:
6817c478bd9Sstevel@tonic-gate 			devices = 1;
6827c478bd9Sstevel@tonic-gate 			break;
6837c478bd9Sstevel@tonic-gate 		case NODEVICES:
6847c478bd9Sstevel@tonic-gate 			devices = 0;
6857c478bd9Sstevel@tonic-gate 			break;
6867c478bd9Sstevel@tonic-gate 		default:
6877c478bd9Sstevel@tonic-gate 			/* cat '/' separator to mntflags */
6887c478bd9Sstevel@tonic-gate 			if (*opts != '\0' && value != NULL)
6897c478bd9Sstevel@tonic-gate 				strcat(opts, "/");
6907c478bd9Sstevel@tonic-gate 			strcat(opts, value);
6917c478bd9Sstevel@tonic-gate 			break;
6927c478bd9Sstevel@tonic-gate 		}
6937c478bd9Sstevel@tonic-gate 	}
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	strcpy(mntflags, "");
6967c478bd9Sstevel@tonic-gate 	if (rdwr)
6977c478bd9Sstevel@tonic-gate 		strcat(mntflags, "read/write");
6987c478bd9Sstevel@tonic-gate 	else if (flag == OLD)
6997c478bd9Sstevel@tonic-gate 		strcat(mntflags, "read only");
7007c478bd9Sstevel@tonic-gate 	else
7017c478bd9Sstevel@tonic-gate 		strcat(mntflags, "read-only");
7027c478bd9Sstevel@tonic-gate 	if (suid) {
7037c478bd9Sstevel@tonic-gate 		if (setuid)
7047c478bd9Sstevel@tonic-gate 			strcat(mntflags, "/setuid");
7057c478bd9Sstevel@tonic-gate 		else
7067c478bd9Sstevel@tonic-gate 			strcat(mntflags, "/nosetuid");
7077c478bd9Sstevel@tonic-gate 		if (devices)
7087c478bd9Sstevel@tonic-gate 			strcat(mntflags, "/devices");
7097c478bd9Sstevel@tonic-gate 		else
7107c478bd9Sstevel@tonic-gate 			strcat(mntflags, "/nodevices");
7117c478bd9Sstevel@tonic-gate 	} else {
7127c478bd9Sstevel@tonic-gate 		strcat(mntflags, "/nosetuid/nodevices");
7137c478bd9Sstevel@tonic-gate 	}
7147c478bd9Sstevel@tonic-gate 	if (*opts != '\0') {
7157c478bd9Sstevel@tonic-gate 		strcat(mntflags, "/");
7167c478bd9Sstevel@tonic-gate 		strcat(mntflags, opts);
7177c478bd9Sstevel@tonic-gate 	}
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	/*
7207c478bd9Sstevel@tonic-gate 	 * The assumed assertion
7218509e9caSToomas Soome 	 *	assert (strlen(mntflags) < sizeof mntflags);
7227c478bd9Sstevel@tonic-gate 	 * is valid at this point in the code. Note that a call to "assert"
7237c478bd9Sstevel@tonic-gate 	 * is not appropriate in production code since it halts the program.
7247c478bd9Sstevel@tonic-gate 	 */
7257c478bd9Sstevel@tonic-gate 	return (mntflags);
7267c478bd9Sstevel@tonic-gate }
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate char	*
72908190127Sdh remote(char *fstype, FILE *rfp)
7307c478bd9Sstevel@tonic-gate {
7317c478bd9Sstevel@tonic-gate 	char	buf[BUFSIZ];
7327c478bd9Sstevel@tonic-gate 	char	*fs;
7337c478bd9Sstevel@tonic-gate 	extern char *strtok();
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 	if (rfp == NULL || fstype == NULL ||
7364bff34e3Sthurlow 	    strlen(fstype) > (size_t)FSTYPE_MAX)
7377c478bd9Sstevel@tonic-gate 		return ("");	/* not a remote */
7387c478bd9Sstevel@tonic-gate 	rewind(rfp);
7397c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), rfp) != NULL) {
7407c478bd9Sstevel@tonic-gate 		fs = strtok(buf, " \t\n");
7417c478bd9Sstevel@tonic-gate 		if (strcmp(fstype, fs) == 0)
7427c478bd9Sstevel@tonic-gate 			return ("remote/");	/* is a remote fs */
7437c478bd9Sstevel@tonic-gate 	}
7447c478bd9Sstevel@tonic-gate 	return ("");	/* not a remote */
7457c478bd9Sstevel@tonic-gate }
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate void
7497c478bd9Sstevel@tonic-gate vfserror(int flag, char *special)
7507c478bd9Sstevel@tonic-gate {
7517c478bd9Sstevel@tonic-gate 	if (special == NULL)
7527c478bd9Sstevel@tonic-gate 		special = "<null>";
7537c478bd9Sstevel@tonic-gate 	switch (flag) {
7547c478bd9Sstevel@tonic-gate 	case VFS_TOOLONG:
7557c478bd9Sstevel@tonic-gate 		fprintf(stderr,
7567c478bd9Sstevel@tonic-gate gettext("%s: Warning: Line in vfstab for \"%s\" exceeds %d characters\n"),
7574bff34e3Sthurlow 		    myname, special, VFS_LINE_MAX-1);
7587c478bd9Sstevel@tonic-gate 		break;
7597c478bd9Sstevel@tonic-gate 	case VFS_TOOFEW:
7607c478bd9Sstevel@tonic-gate 		fprintf(stderr,
7617c478bd9Sstevel@tonic-gate gettext("%s: Warning: Line for \"%s\" in vfstab has too few entries\n"),
7624bff34e3Sthurlow 		    myname, special);
7637c478bd9Sstevel@tonic-gate 		break;
7647c478bd9Sstevel@tonic-gate 	case VFS_TOOMANY:
7657c478bd9Sstevel@tonic-gate 		fprintf(stderr,
7667c478bd9Sstevel@tonic-gate gettext("%s: Warning: Line for \"%s\" in vfstab has too many entries\n"),
7674bff34e3Sthurlow 		    myname, special);
7687c478bd9Sstevel@tonic-gate 		break;
7697c478bd9Sstevel@tonic-gate 	default:
7707c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(
7714bff34e3Sthurlow 		    "%s: Warning: Error in line for \"%s\" in vfstab\n"),
7724bff34e3Sthurlow 		    myname, special);
7737c478bd9Sstevel@tonic-gate 	}
7747c478bd9Sstevel@tonic-gate }
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate void
77708190127Sdh mnterror(int flag)
7787c478bd9Sstevel@tonic-gate {
7797c478bd9Sstevel@tonic-gate 	switch (flag) {
7807c478bd9Sstevel@tonic-gate 	case MNT_TOOLONG:
7817c478bd9Sstevel@tonic-gate 		fprintf(stderr,
7824bff34e3Sthurlow 		    gettext("%s: Line in mnttab exceeds %d characters\n"),
7834bff34e3Sthurlow 		    myname, MNT_LINE_MAX-2);
7847c478bd9Sstevel@tonic-gate 		break;
7857c478bd9Sstevel@tonic-gate 	case MNT_TOOFEW:
7867c478bd9Sstevel@tonic-gate 		fprintf(stderr,
7874bff34e3Sthurlow 		    gettext("%s: Line in mnttab has too few entries\n"),
7884bff34e3Sthurlow 		    myname);
7897c478bd9Sstevel@tonic-gate 		break;
7907c478bd9Sstevel@tonic-gate 	case MNT_TOOMANY:
7917c478bd9Sstevel@tonic-gate 		fprintf(stderr,
7924bff34e3Sthurlow 		    gettext("%s: Line in mnttab has too many entries\n"),
7934bff34e3Sthurlow 		    myname);
7947c478bd9Sstevel@tonic-gate 		break;
7957c478bd9Sstevel@tonic-gate 	}
7967c478bd9Sstevel@tonic-gate 	exit(1);
7977c478bd9Sstevel@tonic-gate }
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate void
80008190127Sdh doexec(char *fstype, char *newargv[])
8017c478bd9Sstevel@tonic-gate {
8027c478bd9Sstevel@tonic-gate 	char	full_path[PATH_MAX];
8037c478bd9Sstevel@tonic-gate 	char	alter_path[PATH_MAX];
8047c478bd9Sstevel@tonic-gate 	char	*vfs_path = VFS_PATH;
8057c478bd9Sstevel@tonic-gate 	char	*alt_path = ALT_PATH;
8067c478bd9Sstevel@tonic-gate 	int	i;
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	/* build the full pathname of the fstype dependent command. */
8097c478bd9Sstevel@tonic-gate 	sprintf(full_path, "%s/%s/%s", vfs_path, fstype, myname);
8107c478bd9Sstevel@tonic-gate 	sprintf(alter_path, "%s/%s/%s", alt_path, fstype, myname);
8117c478bd9Sstevel@tonic-gate 	newargv[1] = myname;
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	if (Vflg) {
8147c478bd9Sstevel@tonic-gate 		printf("%s -F %s", newargv[1], fstype);
8157c478bd9Sstevel@tonic-gate 		for (i = 2; newargv[i]; i++)
8167c478bd9Sstevel@tonic-gate 			printf(" %s", newargv[i]);
8177c478bd9Sstevel@tonic-gate 		printf("\n");
8187c478bd9Sstevel@tonic-gate 		fflush(stdout);
8197c478bd9Sstevel@tonic-gate 		exit(0);
8207c478bd9Sstevel@tonic-gate 	}
8217c478bd9Sstevel@tonic-gate 
822*0463c800SGordon Ross 	/*
823*0463c800SGordon Ross 	 * Some file system types need pfexec.
824*0463c800SGordon Ross 	 */
825*0463c800SGordon Ross 	if (strcmp(fstype, "smbfs") == 0 &&
826*0463c800SGordon Ross 	    setpflags(PRIV_PFEXEC, 1) != 0) {
827*0463c800SGordon Ross 		(void) fprintf(stderr,
828*0463c800SGordon Ross 		    gettext("mount: unable to set PFEXEC flag: %s\n"),
829*0463c800SGordon Ross 		    strerror(errno));
830*0463c800SGordon Ross 		exit(1);
831*0463c800SGordon Ross 	}
832*0463c800SGordon Ross 
8337c478bd9Sstevel@tonic-gate 	/*
8347c478bd9Sstevel@tonic-gate 	 * Try to exec the fstype dependent portion of the mount.
8357c478bd9Sstevel@tonic-gate 	 * See if the directory is there before trying to exec dependent
8367c478bd9Sstevel@tonic-gate 	 * portion.  This is only useful for eliminating the
8377c478bd9Sstevel@tonic-gate 	 * '..mount: not found' message when '/usr' is mounted
8387c478bd9Sstevel@tonic-gate 	 */
8397c478bd9Sstevel@tonic-gate 	if (access(full_path, 0) == 0) {
8407c478bd9Sstevel@tonic-gate 		execv(full_path, &newargv[1]);
8417c478bd9Sstevel@tonic-gate 		if (errno == EACCES) {
8427c478bd9Sstevel@tonic-gate 			fprintf(stderr,
8437c478bd9Sstevel@tonic-gate 			gettext("%s: Cannot execute %s - permission denied\n"),
8444bff34e3Sthurlow 			    myname, full_path);
8457c478bd9Sstevel@tonic-gate 		}
8467c478bd9Sstevel@tonic-gate 		if (errno == ENOEXEC) {
8477c478bd9Sstevel@tonic-gate 			newargv[0] = "sh";
8487c478bd9Sstevel@tonic-gate 			newargv[1] = full_path;
8497c478bd9Sstevel@tonic-gate 			execv("/sbin/sh", &newargv[0]);
8507c478bd9Sstevel@tonic-gate 		}
8517c478bd9Sstevel@tonic-gate 	}
8527c478bd9Sstevel@tonic-gate 	execv(alter_path, &newargv[1]);
8537c478bd9Sstevel@tonic-gate 	if (errno == EACCES) {
8547c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(
8554bff34e3Sthurlow 		    "%s: Cannot execute %s - permission denied\n"),
8564bff34e3Sthurlow 		    myname, alter_path);
8577c478bd9Sstevel@tonic-gate 		exit(1);
8587c478bd9Sstevel@tonic-gate 	}
8597c478bd9Sstevel@tonic-gate 	if (errno == ENOEXEC) {
8607c478bd9Sstevel@tonic-gate 		newargv[0] = "sh";
8617c478bd9Sstevel@tonic-gate 		newargv[1] = alter_path;
8627c478bd9Sstevel@tonic-gate 		execv("/sbin/sh", &newargv[0]);
8637c478bd9Sstevel@tonic-gate 	}
8647c478bd9Sstevel@tonic-gate 	fprintf(stderr,
8654bff34e3Sthurlow 	    gettext("%s: Operation not applicable to FSType %s\n"),
8664bff34e3Sthurlow 	    myname, fstype);
8677c478bd9Sstevel@tonic-gate 	exit(1);
8687c478bd9Sstevel@tonic-gate }
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate char *mntopts[] = { MNTOPT_IGNORE, NULL };
8717c478bd9Sstevel@tonic-gate #define	IGNORE    0
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate /*
8747c478bd9Sstevel@tonic-gate  * Return 1 if "ignore" appears in the options string
8757c478bd9Sstevel@tonic-gate  */
8767c478bd9Sstevel@tonic-gate int
87708190127Sdh ignore(char *opts)
8787c478bd9Sstevel@tonic-gate {
8797c478bd9Sstevel@tonic-gate 	char *value;
8807c478bd9Sstevel@tonic-gate 	char *saveptr, *my_opts;
8817c478bd9Sstevel@tonic-gate 	int rval = 0;
8827c478bd9Sstevel@tonic-gate 
8838509e9caSToomas Soome 	if (opts == NULL || *opts == '\0')
8847c478bd9Sstevel@tonic-gate 		return (0);
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	/*
8877c478bd9Sstevel@tonic-gate 	 * we make a copy of the option string to pass to getsubopt(),
8887c478bd9Sstevel@tonic-gate 	 * because getsubopt() modifies the string.  We also save
8897c478bd9Sstevel@tonic-gate 	 * the original pointer returned by strdup, because getsubopt
8907c478bd9Sstevel@tonic-gate 	 * changes the pointer passed into it.  If strdup fails (unlikely),
8917c478bd9Sstevel@tonic-gate 	 * we act as if the "ignore" option isn't set rather than fail.
8927c478bd9Sstevel@tonic-gate 	 */
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 	if ((saveptr = my_opts = strdup(opts)) == NULL)
8957c478bd9Sstevel@tonic-gate 		nomem();
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 	while (*my_opts != '\0') {
8987c478bd9Sstevel@tonic-gate 		if (getsubopt(&my_opts, mntopts, &value) == IGNORE)
8997c478bd9Sstevel@tonic-gate 			rval = 1;
9007c478bd9Sstevel@tonic-gate 	}
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 	free(saveptr);
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	return (rval);
9057c478bd9Sstevel@tonic-gate }
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate /*
9087c478bd9Sstevel@tonic-gate  * Perform the parallel version of mount.  If count == 0, mount all
9097c478bd9Sstevel@tonic-gate  * vfstab filesystems with the automnt field == "yes".  Use fstype if
9107c478bd9Sstevel@tonic-gate  * supplied.  If mntlist supplied, then attempt to only mount those.
9117c478bd9Sstevel@tonic-gate  */
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate int
9147c478bd9Sstevel@tonic-gate parmount(char **mntlist, int count, char *fstype)
9157c478bd9Sstevel@tonic-gate {
9168509e9caSToomas Soome 	int		maxfd =	OPEN_MAX;
9178509e9caSToomas Soome 	struct		rlimit rl;
9187c478bd9Sstevel@tonic-gate 	vfsent_t	**vl, *vp;
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 	/*
9217c478bd9Sstevel@tonic-gate 	 * Process scaling.  After running a series
9227c478bd9Sstevel@tonic-gate 	 * of tests based on the number of simultaneous processes and
9237c478bd9Sstevel@tonic-gate 	 * processors available, optimum performance was achieved near or
9247c478bd9Sstevel@tonic-gate 	 * at (PROCN * 2).
9257c478bd9Sstevel@tonic-gate 	 */
9267c478bd9Sstevel@tonic-gate 	if ((maxrun = sysconf(_SC_NPROCESSORS_ONLN)) == -1)
9277c478bd9Sstevel@tonic-gate 		maxrun = 4;
9287c478bd9Sstevel@tonic-gate 	else
9297c478bd9Sstevel@tonic-gate 		maxrun = maxrun * 2 + 1;
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
9327c478bd9Sstevel@tonic-gate 		rl.rlim_cur = rl.rlim_max;
9337c478bd9Sstevel@tonic-gate 		if (setrlimit(RLIMIT_NOFILE, &rl) == 0)
9347c478bd9Sstevel@tonic-gate 			maxfd = (int)rl.rlim_cur;
9357c478bd9Sstevel@tonic-gate 	}
936004388ebScasper 	(void) enable_extended_FILE_stdio(-1, -1);
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 	/*
9397c478bd9Sstevel@tonic-gate 	 * The parent needs to maintain 3 of its own fd's, plus 2 for
9407c478bd9Sstevel@tonic-gate 	 * each child (the stdout and stderr pipes).
9417c478bd9Sstevel@tonic-gate 	 */
9427c478bd9Sstevel@tonic-gate 	maxfd = (maxfd / 2) - 6;	/* 6 takes care of temporary  */
9437c478bd9Sstevel@tonic-gate 					/* periods of open fds */
9447c478bd9Sstevel@tonic-gate 	if (maxfd < maxrun)
9457c478bd9Sstevel@tonic-gate 		maxrun = maxfd;
9467c478bd9Sstevel@tonic-gate 	if (maxrun < 4)
9477c478bd9Sstevel@tonic-gate 		maxrun = 4;		/* sanity check */
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 	if (count == 0)
9507c478bd9Sstevel@tonic-gate 		mntlist = NULL;		/* used as a flag later */
9517c478bd9Sstevel@tonic-gate 	else
9527c478bd9Sstevel@tonic-gate 		fstype = NULL;		/* mount points supplied: */
9537c478bd9Sstevel@tonic-gate 					/* ignore fstype */
9547c478bd9Sstevel@tonic-gate 	/*
9557c478bd9Sstevel@tonic-gate 	 * Read the whole vfstab into a linked list for quick processing.
9567c478bd9Sstevel@tonic-gate 	 * On average, this is the most efficient way to collect and
9577c478bd9Sstevel@tonic-gate 	 * manipulate the vfstab data.
9587c478bd9Sstevel@tonic-gate 	 */
9597c478bd9Sstevel@tonic-gate 	vfsll = getvfsall(fstype, mntlist == NULL);
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 	/*
9627c478bd9Sstevel@tonic-gate 	 * Make an array out of the vfs linked list for sorting purposes.
9637c478bd9Sstevel@tonic-gate 	 */
9647c478bd9Sstevel@tonic-gate 	if (vfsll == NULL ||
9657c478bd9Sstevel@tonic-gate 	    (vfsarray = make_vfsarray(mntlist, count)) == NULL) {
9667c478bd9Sstevel@tonic-gate 		if (mntlist == NULL)	/* not an error - just none found */
9677c478bd9Sstevel@tonic-gate 			return (0);
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: No valid entries found in %s\n"),
9704bff34e3Sthurlow 		    myname, vfstab);
9717c478bd9Sstevel@tonic-gate 		return (1);
9727c478bd9Sstevel@tonic-gate 	}
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	/*
9757c478bd9Sstevel@tonic-gate 	 * Sort the entries based on their resolved path names
9767c478bd9Sstevel@tonic-gate 	 *
9777c478bd9Sstevel@tonic-gate 	 * If an lofs is encountered, then the original order of the vfstab
9787c478bd9Sstevel@tonic-gate 	 * file needs to be maintained until we are done mounting lofs's.
9797c478bd9Sstevel@tonic-gate 	 */
9807c478bd9Sstevel@tonic-gate 	if (!lofscnt)
9817c478bd9Sstevel@tonic-gate 		qsort((void *)vfsarray, vfsarraysize, sizeof (vfsent_t *),
9824bff34e3Sthurlow 		    mlevelcmp);
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 	/*
9857c478bd9Sstevel@tonic-gate 	 * Shrink the vfsll linked list down to the new list.  This will
9867c478bd9Sstevel@tonic-gate 	 * speed up the pid search in cleanupkid() later.
9877c478bd9Sstevel@tonic-gate 	 */
9887c478bd9Sstevel@tonic-gate 	vfsll = vfsarray[0];
9897c478bd9Sstevel@tonic-gate 	for (vl = vfsarray; vp = *vl; )
9907c478bd9Sstevel@tonic-gate 		vp->next = *++vl;
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 	/*
9937c478bd9Sstevel@tonic-gate 	 * Try to handle interrupts in a reasonable way.
9947c478bd9Sstevel@tonic-gate 	 */
9957c478bd9Sstevel@tonic-gate 	sigset(SIGHUP, cleanup);
9967c478bd9Sstevel@tonic-gate 	sigset(SIGQUIT, cleanup);
9977c478bd9Sstevel@tonic-gate 	sigset(SIGINT, cleanup);
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 	do_mounts();		/* do the mounts */
1000a38ddfeeSTim Haley 
1001a38ddfeeSTim Haley 	if (failcnt > 0 && failcnt == lofsfail)
1002a38ddfeeSTim Haley 		return (ALL_LOFS_FAILURES);
1003a38ddfeeSTim Haley 
10047c478bd9Sstevel@tonic-gate 	return (exitcode);
10057c478bd9Sstevel@tonic-gate }
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate /*
10087c478bd9Sstevel@tonic-gate  * Read all vstab (fp) entries into memory if fstype == NULL.
10097c478bd9Sstevel@tonic-gate  * If fstype is specified, than read all those that match it.
10107c478bd9Sstevel@tonic-gate  *
10117c478bd9Sstevel@tonic-gate  * Returns a linked list.
10127c478bd9Sstevel@tonic-gate  */
10137c478bd9Sstevel@tonic-gate vfsent_t *
10147c478bd9Sstevel@tonic-gate getvfsall(char *fstype, int takeall)
10157c478bd9Sstevel@tonic-gate {
10167c478bd9Sstevel@tonic-gate 	vfsent_t	*vhead, *vtail;
10178509e9caSToomas Soome 	struct vfstab	vget;
10187c478bd9Sstevel@tonic-gate 	FILE		*fp;
10197c478bd9Sstevel@tonic-gate 	int		cnt = 0, ret;
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	if ((fp = fopen(vfstab, "r")) == NULL) {
10227c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: Cannot open %s\n"),
10234bff34e3Sthurlow 		    myname, vfstab);
10247c478bd9Sstevel@tonic-gate 		exit(1);
10257c478bd9Sstevel@tonic-gate 	}
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 	vhead = vtail = NULL;
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 	while ((ret = getvfsent(fp, &vget)) != -1) {
10307c478bd9Sstevel@tonic-gate 		vfsent_t *vp;
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 		if (ret > 0) {
10337c478bd9Sstevel@tonic-gate 			vfserror(ret, vget.vfs_mountp);
10347c478bd9Sstevel@tonic-gate 			continue;
10357c478bd9Sstevel@tonic-gate 		}
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 		/*
10387c478bd9Sstevel@tonic-gate 		 * If mount points were not specified, then we ignore
10397c478bd9Sstevel@tonic-gate 		 * entries that aren't marked "yes".
10407c478bd9Sstevel@tonic-gate 		 */
10417c478bd9Sstevel@tonic-gate 		if (takeall &&
10427c478bd9Sstevel@tonic-gate 		    (vget.vfs_automnt == NULL ||
10437c478bd9Sstevel@tonic-gate 		    strcmp(vget.vfs_automnt, "yes")))
10447c478bd9Sstevel@tonic-gate 			continue;
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 		if (fstype && vget.vfs_fstype &&
10477c478bd9Sstevel@tonic-gate 		    strcmp(fstype, vget.vfs_fstype))
10487c478bd9Sstevel@tonic-gate 			continue;
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 		if (vget.vfs_mountp == NULL ||
10517c478bd9Sstevel@tonic-gate 		    (vget.vfs_fstype && (strcmp(vget.vfs_fstype, "swap") == 0)))
10527c478bd9Sstevel@tonic-gate 			continue;
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 		if (check_fields(vget.vfs_fstype, vget.vfs_mountp)) {
10557c478bd9Sstevel@tonic-gate 			exitcode = 1;
10567c478bd9Sstevel@tonic-gate 			continue;
10577c478bd9Sstevel@tonic-gate 		}
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 		vp = new_vfsent(&vget, cnt);	/* create new vfs entry */
10607c478bd9Sstevel@tonic-gate 		if (vhead == NULL)
10617c478bd9Sstevel@tonic-gate 			vhead = vp;
10627c478bd9Sstevel@tonic-gate 		else
10637c478bd9Sstevel@tonic-gate 			vtail->next = vp;
10647c478bd9Sstevel@tonic-gate 		vtail = vp;
10657c478bd9Sstevel@tonic-gate 		cnt++;
10667c478bd9Sstevel@tonic-gate 	}
10677c478bd9Sstevel@tonic-gate 	fclose(fp);
10687c478bd9Sstevel@tonic-gate 	if (vtail == NULL) {
10697c478bd9Sstevel@tonic-gate 		vfsarraysize = 0;
10707c478bd9Sstevel@tonic-gate 		vfslltail = NULL;
10717c478bd9Sstevel@tonic-gate 		return (NULL);
10727c478bd9Sstevel@tonic-gate 	}
10737c478bd9Sstevel@tonic-gate 	vtail->next = NULL;
10747c478bd9Sstevel@tonic-gate 	vfslltail = vtail;	/* save it in the global variable */
10757c478bd9Sstevel@tonic-gate 	vfsarraysize = cnt;
10767c478bd9Sstevel@tonic-gate 	return (vhead);
10777c478bd9Sstevel@tonic-gate }
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate /*
10817c478bd9Sstevel@tonic-gate  * Returns an array of vfsent_t's based on vfsll & mntlist.
10827c478bd9Sstevel@tonic-gate  */
10837c478bd9Sstevel@tonic-gate vfsent_t **
10847c478bd9Sstevel@tonic-gate make_vfsarray(char **mntlist, int count)
10857c478bd9Sstevel@tonic-gate {
10868509e9caSToomas Soome 	vfsent_t	*vp, *vmark, *vpprev, **vpp;
10877c478bd9Sstevel@tonic-gate 	int		ndx, found;
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 	if (vfsll == NULL)
10907c478bd9Sstevel@tonic-gate 		return (NULL);
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	if (count > 0)
10937c478bd9Sstevel@tonic-gate 		vfsarraysize = count;
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 	vpp = (vfsent_t **)malloc(sizeof (*vpp) * (vfsarraysize + 1));
10967c478bd9Sstevel@tonic-gate 	if (vpp == NULL)
10977c478bd9Sstevel@tonic-gate 		nomem();
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 	if (mntlist == NULL) {
11007c478bd9Sstevel@tonic-gate 		/*
11017c478bd9Sstevel@tonic-gate 		 * No mount list specified: take all vfstab mount points.
11027c478bd9Sstevel@tonic-gate 		 */
11037c478bd9Sstevel@tonic-gate 		for (ndx = 0, vp = vfsll; vp; vp = vp->next) {
11047c478bd9Sstevel@tonic-gate 			(void) setrpath(vp);
11057c478bd9Sstevel@tonic-gate 			/*
11067c478bd9Sstevel@tonic-gate 			 * Sigh. lofs entries can complicate matters so much
11077c478bd9Sstevel@tonic-gate 			 * that the best way to avoid problems is to
11087c478bd9Sstevel@tonic-gate 			 * stop parallel mounting when an lofs is
11097c478bd9Sstevel@tonic-gate 			 * encountered, so we keep a count of how many
11107c478bd9Sstevel@tonic-gate 			 * there are.
11117c478bd9Sstevel@tonic-gate 			 * Fortunately this is rare.
11127c478bd9Sstevel@tonic-gate 			 */
11137c478bd9Sstevel@tonic-gate 			if (vp->v.vfs_fstype &&
11147c478bd9Sstevel@tonic-gate 			    (strcmp(vp->v.vfs_fstype, MNTTYPE_LOFS) == 0))
11157c478bd9Sstevel@tonic-gate 				lofscnt++;
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 			vpp[ndx++] = vp;
11187c478bd9Sstevel@tonic-gate 		}
11197c478bd9Sstevel@tonic-gate 		vpp[ndx] = NULL;
11207c478bd9Sstevel@tonic-gate 		return (vpp);
11217c478bd9Sstevel@tonic-gate 	}
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 	/*
11247c478bd9Sstevel@tonic-gate 	 * A list of mount points was specified on the command line
11257c478bd9Sstevel@tonic-gate 	 * and we need to search for each one.
11267c478bd9Sstevel@tonic-gate 	 */
11277c478bd9Sstevel@tonic-gate 	vpprev = vfslltail;
11287c478bd9Sstevel@tonic-gate 	vpprev->next = vfsll;	/* make a circle out of it */
11297c478bd9Sstevel@tonic-gate 	vmark = vp = vfsll;
11307c478bd9Sstevel@tonic-gate 	/*
11317c478bd9Sstevel@tonic-gate 	 * For each specified mount point:
11327c478bd9Sstevel@tonic-gate 	 */
11337c478bd9Sstevel@tonic-gate 	for (ndx = 0; *mntlist; mntlist++) {
11347c478bd9Sstevel@tonic-gate 		found = 0;
11357c478bd9Sstevel@tonic-gate 		/*
11367c478bd9Sstevel@tonic-gate 		 * Circle our entire linked list, looking for *mntlist.
11377c478bd9Sstevel@tonic-gate 		 */
11387c478bd9Sstevel@tonic-gate 		while (vp) {
11397c478bd9Sstevel@tonic-gate 			if (strcmp(*mntlist, vp->v.vfs_mountp) == 0) {
11407c478bd9Sstevel@tonic-gate 				vpp[ndx++] = vp;	/* found it. */
11417c478bd9Sstevel@tonic-gate 				(void) setrpath(vp);
11427c478bd9Sstevel@tonic-gate 				if (vp->v.vfs_fstype &&
11437c478bd9Sstevel@tonic-gate 				    (strcmp(vp->v.vfs_fstype,
11447c478bd9Sstevel@tonic-gate 				    MNTTYPE_LOFS) == 0))
11457c478bd9Sstevel@tonic-gate 					lofscnt++;
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 				if (vp == vpprev) {	/* list exhausted */
11487c478bd9Sstevel@tonic-gate 					vp = NULL;
11497c478bd9Sstevel@tonic-gate 					found++;
11507c478bd9Sstevel@tonic-gate 					break;
11517c478bd9Sstevel@tonic-gate 				}
11527c478bd9Sstevel@tonic-gate 				/*
11537c478bd9Sstevel@tonic-gate 				 * Remove it from the circular list.  vpprev
11547c478bd9Sstevel@tonic-gate 				 * remains unchanged.
11557c478bd9Sstevel@tonic-gate 				 */
11567c478bd9Sstevel@tonic-gate 				vp = vp->next;
11577c478bd9Sstevel@tonic-gate 				vpprev->next->next = NULL;
11587c478bd9Sstevel@tonic-gate 				vpprev->next = vp;
11597c478bd9Sstevel@tonic-gate 				/*
11607c478bd9Sstevel@tonic-gate 				 * Set vmark to the first elem that we check
11617c478bd9Sstevel@tonic-gate 				 * each time.
11627c478bd9Sstevel@tonic-gate 				 */
11637c478bd9Sstevel@tonic-gate 				vmark = vp;
11647c478bd9Sstevel@tonic-gate 				found++;
11657c478bd9Sstevel@tonic-gate 				break;
11667c478bd9Sstevel@tonic-gate 			}
11677c478bd9Sstevel@tonic-gate 			vpprev = vp;
11687c478bd9Sstevel@tonic-gate 			vp = vp->next;
11697c478bd9Sstevel@tonic-gate 			if (vp == vmark)	/* break out if we completed */
11707c478bd9Sstevel@tonic-gate 						/* the circle */
11717c478bd9Sstevel@tonic-gate 				break;
11727c478bd9Sstevel@tonic-gate 		}
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 		if (!found) {
11757c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(
11764bff34e3Sthurlow 			    "%s: Warning: %s not found in %s\n"),
11774bff34e3Sthurlow 			    myname, *mntlist, vfstab);
11787c478bd9Sstevel@tonic-gate 			exitcode = 1;
11797c478bd9Sstevel@tonic-gate 		}
11807c478bd9Sstevel@tonic-gate 	}
11817c478bd9Sstevel@tonic-gate 	if (ndx == 0)
11827c478bd9Sstevel@tonic-gate 		return (NULL);
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 	vpp[ndx] = NULL;	/* null terminate the list */
11857c478bd9Sstevel@tonic-gate 	vfsarraysize = ndx;	/* adjust vfsarraysize */
11867c478bd9Sstevel@tonic-gate 	return (vpp);
11877c478bd9Sstevel@tonic-gate }
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate /*
11907c478bd9Sstevel@tonic-gate  * Performs the exec argument processing, all  of the child forking and
11917c478bd9Sstevel@tonic-gate  * execing, and child cleanup.
11927c478bd9Sstevel@tonic-gate  * Sets exitcode to non-zero if any errors occurred.
11937c478bd9Sstevel@tonic-gate  */
11947c478bd9Sstevel@tonic-gate void
119508190127Sdh do_mounts(void)
11967c478bd9Sstevel@tonic-gate {
11978509e9caSToomas Soome 	int		i, isave, cnt;
11988509e9caSToomas Soome 	vfsent_t	*vp, *vpprev, **vl;
11997c478bd9Sstevel@tonic-gate 	char		*newargv[ARGV_MAX];
12007c478bd9Sstevel@tonic-gate 	pid_t		child;
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 	/*
12037c478bd9Sstevel@tonic-gate 	 * create the arg list once;  the only differences among
12047c478bd9Sstevel@tonic-gate 	 * the calls are the options, special and mountp fields.
12057c478bd9Sstevel@tonic-gate 	 */
12067c478bd9Sstevel@tonic-gate 	i = 2;
12077c478bd9Sstevel@tonic-gate 	if (cflg)
12087c478bd9Sstevel@tonic-gate 		newargv[i++] = "-c";
12097c478bd9Sstevel@tonic-gate 	if (gflg)
12107c478bd9Sstevel@tonic-gate 		newargv[i++] = "-g";
12117c478bd9Sstevel@tonic-gate 	if (mflg)
12127c478bd9Sstevel@tonic-gate 		newargv[i++] = "-m";
12137c478bd9Sstevel@tonic-gate 	if (Oflg)
12147c478bd9Sstevel@tonic-gate 		newargv[i++] = "-O";
12157c478bd9Sstevel@tonic-gate 	if (qflg)
12167c478bd9Sstevel@tonic-gate 		newargv[i++] = "-q";
12177c478bd9Sstevel@tonic-gate 	if (rflg)
12187c478bd9Sstevel@tonic-gate 		newargv[i++] = "-r";
12197c478bd9Sstevel@tonic-gate 	if (dashflg)
12207c478bd9Sstevel@tonic-gate 		newargv[i++] = "--";
12217c478bd9Sstevel@tonic-gate 	if (oflg) {
12227c478bd9Sstevel@tonic-gate 		newargv[i++] = "-o";
12237c478bd9Sstevel@tonic-gate 		newargv[i++] = specific_opts;
12247c478bd9Sstevel@tonic-gate 	}
12257c478bd9Sstevel@tonic-gate 	isave = i;
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 	/*
12287c478bd9Sstevel@tonic-gate 	 * Main loop for the mount processes
12297c478bd9Sstevel@tonic-gate 	 */
12307c478bd9Sstevel@tonic-gate 	vl = vfsarray;
12317c478bd9Sstevel@tonic-gate 	cnt = vfsarraysize;
12327c478bd9Sstevel@tonic-gate 	for (vpprev = *vl; vp = *vl; vpprev = vp, vl++, cnt--) {
12337c478bd9Sstevel@tonic-gate 		/*
12347c478bd9Sstevel@tonic-gate 		 * Check to see if we cross a mount level: e.g.,
12357c478bd9Sstevel@tonic-gate 		 * /a/b -> /a/b/c.  If so, we need to wait for all current
12367c478bd9Sstevel@tonic-gate 		 * mounts to finish, rerun realpath on the remaining mount
12377c478bd9Sstevel@tonic-gate 		 * points, and resort the list.
12387c478bd9Sstevel@tonic-gate 		 *
12397c478bd9Sstevel@tonic-gate 		 * Also, we mount serially as long as there are lofs's
12407c478bd9Sstevel@tonic-gate 		 * to mount to avoid improper mount ordering.
12417c478bd9Sstevel@tonic-gate 		 */
12427c478bd9Sstevel@tonic-gate 		if (vp->mlevel > vpprev->mlevel || lofscnt > 0) {
12437c478bd9Sstevel@tonic-gate 			vfsent_t **vlp;
12447c478bd9Sstevel@tonic-gate 
12457c478bd9Sstevel@tonic-gate 			while (nrun > 0 && (dowait() != -1))
12467c478bd9Sstevel@tonic-gate 				;
12477c478bd9Sstevel@tonic-gate 			/*
12487c478bd9Sstevel@tonic-gate 			 * Gads! It's possible for real path mounts points to
12497c478bd9Sstevel@tonic-gate 			 * change after mounts are done at a lower mount
12507c478bd9Sstevel@tonic-gate 			 * level.
12517c478bd9Sstevel@tonic-gate 			 * Thus, we need to recalculate mount levels and
12527c478bd9Sstevel@tonic-gate 			 * resort the list from this point.
12537c478bd9Sstevel@tonic-gate 			 */
12547c478bd9Sstevel@tonic-gate 			for (vlp = vl; *vlp; vlp++)
12557c478bd9Sstevel@tonic-gate 				(void) setrpath(*vlp);
12567c478bd9Sstevel@tonic-gate 			/*
12577c478bd9Sstevel@tonic-gate 			 * Sort the remaining entries based on their newly
12587c478bd9Sstevel@tonic-gate 			 * resolved path names.
12597c478bd9Sstevel@tonic-gate 			 * Do not sort if we still have lofs's to mount.
12607c478bd9Sstevel@tonic-gate 			 */
12617c478bd9Sstevel@tonic-gate 			if (lofscnt == 0) {
12627c478bd9Sstevel@tonic-gate 				qsort((void *)vl, cnt, sizeof (vfsent_t *),
12634bff34e3Sthurlow 				    mlevelcmp);
12647c478bd9Sstevel@tonic-gate 				vp = *vl;
12657c478bd9Sstevel@tonic-gate 			}
12667c478bd9Sstevel@tonic-gate 		}
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate 		if (vp->flag & VRPFAILED) {
12697c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(
12704bff34e3Sthurlow 			    "%s: Nonexistent mount point: %s\n"),
12714bff34e3Sthurlow 			    myname, vp->v.vfs_mountp);
12727c478bd9Sstevel@tonic-gate 			vp->flag |= VNOTMOUNTED;
12737c478bd9Sstevel@tonic-gate 			exitcode = 1;
12747c478bd9Sstevel@tonic-gate 			continue;
12757c478bd9Sstevel@tonic-gate 		}
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate 		/*
12787c478bd9Sstevel@tonic-gate 		 * If mount options were not specified on the command
12797c478bd9Sstevel@tonic-gate 		 * line, then use the ones found in the vfstab entry,
12807c478bd9Sstevel@tonic-gate 		 * if any.
12817c478bd9Sstevel@tonic-gate 		 */
12827c478bd9Sstevel@tonic-gate 		i = isave;
12837c478bd9Sstevel@tonic-gate 		if (!oflg && vp->v.vfs_mntopts) {
12847c478bd9Sstevel@tonic-gate 			newargv[i++] = "-o";
12857c478bd9Sstevel@tonic-gate 			newargv[i++] = vp->v.vfs_mntopts;
12867c478bd9Sstevel@tonic-gate 		}
12877c478bd9Sstevel@tonic-gate 		newargv[i++] = vp->v.vfs_special;
12887c478bd9Sstevel@tonic-gate 		newargv[i++] = vp->rpath;
12897c478bd9Sstevel@tonic-gate 		newargv[i] = NULL;
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 		/*
12927c478bd9Sstevel@tonic-gate 		 * This should never really fail.
12937c478bd9Sstevel@tonic-gate 		 */
12947c478bd9Sstevel@tonic-gate 		while (setup_iopipe(vp) == -1 && (dowait() != -1))
12957c478bd9Sstevel@tonic-gate 			;
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate 		while (nrun >= maxrun && (dowait() != -1))	/* throttle */
12987c478bd9Sstevel@tonic-gate 			;
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate 		if ((child = fork()) == -1) {
13017c478bd9Sstevel@tonic-gate 			perror("fork");
13027c478bd9Sstevel@tonic-gate 			cleanup(-1);
13037c478bd9Sstevel@tonic-gate 			/* not reached */
13047c478bd9Sstevel@tonic-gate 		}
13057c478bd9Sstevel@tonic-gate 		if (child == 0) {		/* child */
13067c478bd9Sstevel@tonic-gate 			signal(SIGHUP, SIG_IGN);
13077c478bd9Sstevel@tonic-gate 			signal(SIGQUIT, SIG_IGN);
13087c478bd9Sstevel@tonic-gate 			signal(SIGINT, SIG_IGN);
13097c478bd9Sstevel@tonic-gate 			setup_output(vp);
13107c478bd9Sstevel@tonic-gate 			doexec(vp->v.vfs_fstype, newargv);
13117c478bd9Sstevel@tonic-gate 			perror("exec");
13127c478bd9Sstevel@tonic-gate 			exit(1);
13137c478bd9Sstevel@tonic-gate 		}
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate 		/* parent */
13167c478bd9Sstevel@tonic-gate 		(void) close(vp->sopipe[WRPIPE]);
13177c478bd9Sstevel@tonic-gate 		(void) close(vp->sepipe[WRPIPE]);
13187c478bd9Sstevel@tonic-gate 		vp->pid = child;
13197c478bd9Sstevel@tonic-gate 		nrun++;
13207c478bd9Sstevel@tonic-gate 	}
13217c478bd9Sstevel@tonic-gate 	/*
13227c478bd9Sstevel@tonic-gate 	 * Mostly done by now - wait and clean up the stragglers.
13237c478bd9Sstevel@tonic-gate 	 */
13247c478bd9Sstevel@tonic-gate 	cleanup(0);
13257c478bd9Sstevel@tonic-gate }
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate /*
13297c478bd9Sstevel@tonic-gate  * Setup stdout and stderr pipes for the children's output.
13307c478bd9Sstevel@tonic-gate  */
13317c478bd9Sstevel@tonic-gate int
13327c478bd9Sstevel@tonic-gate setup_iopipe(vfsent_t *mp)
13337c478bd9Sstevel@tonic-gate {
13347c478bd9Sstevel@tonic-gate 	/*
13357c478bd9Sstevel@tonic-gate 	 * Make a stdout and stderr pipe.  This should never fail.
13367c478bd9Sstevel@tonic-gate 	 */
13377c478bd9Sstevel@tonic-gate 	if (pipe(mp->sopipe) == -1)
13387c478bd9Sstevel@tonic-gate 		return (-1);
13397c478bd9Sstevel@tonic-gate 	if (pipe(mp->sepipe) == -1) {
13407c478bd9Sstevel@tonic-gate 		(void) close(mp->sopipe[RDPIPE]);
13417c478bd9Sstevel@tonic-gate 		(void) close(mp->sopipe[WRPIPE]);
13427c478bd9Sstevel@tonic-gate 		return (-1);
13437c478bd9Sstevel@tonic-gate 	}
13447c478bd9Sstevel@tonic-gate 	/*
13457c478bd9Sstevel@tonic-gate 	 * Don't block on an empty pipe.
13467c478bd9Sstevel@tonic-gate 	 */
13477c478bd9Sstevel@tonic-gate 	(void) fcntl(mp->sopipe[RDPIPE], F_SETFL, O_NDELAY|O_NONBLOCK);
13487c478bd9Sstevel@tonic-gate 	(void) fcntl(mp->sepipe[RDPIPE], F_SETFL, O_NDELAY|O_NONBLOCK);
13497c478bd9Sstevel@tonic-gate 	/*
13507c478bd9Sstevel@tonic-gate 	 * Don't pass extra fds into children.
13517c478bd9Sstevel@tonic-gate 	 */
13527c478bd9Sstevel@tonic-gate 	(void) fcntl(mp->sopipe[RDPIPE], F_SETFD, FD_CLOEXEC);
13537c478bd9Sstevel@tonic-gate 	(void) fcntl(mp->sepipe[RDPIPE], F_SETFD, FD_CLOEXEC);
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 	return (0);
13567c478bd9Sstevel@tonic-gate }
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate /*
13597c478bd9Sstevel@tonic-gate  * Called by a child to attach its stdout and stderr to the write side of
13607c478bd9Sstevel@tonic-gate  * the pipes.
13617c478bd9Sstevel@tonic-gate  */
13627c478bd9Sstevel@tonic-gate void
13637c478bd9Sstevel@tonic-gate setup_output(vfsent_t *vp)
13647c478bd9Sstevel@tonic-gate {
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate 	(void) close(fileno(stdout));
13677c478bd9Sstevel@tonic-gate 	(void) dup(vp->sopipe[WRPIPE]);
13687c478bd9Sstevel@tonic-gate 	(void) close(vp->sopipe[WRPIPE]);
13697c478bd9Sstevel@tonic-gate 
13707c478bd9Sstevel@tonic-gate 	(void) close(fileno(stderr));
13717c478bd9Sstevel@tonic-gate 	(void) dup(vp->sepipe[WRPIPE]);
13727c478bd9Sstevel@tonic-gate 	(void) close(vp->sepipe[WRPIPE]);
13737c478bd9Sstevel@tonic-gate }
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate /*
13767c478bd9Sstevel@tonic-gate  * Parent uses this to print any stdout or stderr output issued by
13777c478bd9Sstevel@tonic-gate  * the child.
13787c478bd9Sstevel@tonic-gate  */
13797c478bd9Sstevel@tonic-gate static void
13807c478bd9Sstevel@tonic-gate doio(vfsent_t *vp)
13817c478bd9Sstevel@tonic-gate {
13827c478bd9Sstevel@tonic-gate 	int bytes;
13837c478bd9Sstevel@tonic-gate 	char ibuf[BUFSIZ];
13847c478bd9Sstevel@tonic-gate 
13857c478bd9Sstevel@tonic-gate 	while ((bytes = read(vp->sepipe[RDPIPE], ibuf, sizeof (ibuf))) > 0)
13867c478bd9Sstevel@tonic-gate 		write(fileno(stderr), ibuf, bytes);
13877c478bd9Sstevel@tonic-gate 	while ((bytes = read(vp->sopipe[RDPIPE], ibuf, sizeof (ibuf))) > 0)
13887c478bd9Sstevel@tonic-gate 		write(fileno(stdout), ibuf, bytes);
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate 	(void) close(vp->sopipe[RDPIPE]);
13917c478bd9Sstevel@tonic-gate 	(void) close(vp->sepipe[RDPIPE]);
13927c478bd9Sstevel@tonic-gate }
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate /*
13957c478bd9Sstevel@tonic-gate  * Waits for 1 child to die.
13967c478bd9Sstevel@tonic-gate  *
13977c478bd9Sstevel@tonic-gate  * Returns -1 if no children are left to wait for.
13987c478bd9Sstevel@tonic-gate  * Returns 0 if a child died without an error.
13997c478bd9Sstevel@tonic-gate  * Returns 1 if a child died with an error.
14007c478bd9Sstevel@tonic-gate  */
14017c478bd9Sstevel@tonic-gate int
140208190127Sdh dowait(void)
14037c478bd9Sstevel@tonic-gate {
14047c478bd9Sstevel@tonic-gate 	int child, wstat;
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate 	if ((child = wait(&wstat)) == -1)
14077c478bd9Sstevel@tonic-gate 		return (-1);
14087c478bd9Sstevel@tonic-gate 	nrun--;
14097c478bd9Sstevel@tonic-gate 	return (cleanupkid(child, wstat) != 0);
14107c478bd9Sstevel@tonic-gate }
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate /*
14137c478bd9Sstevel@tonic-gate  * Locates the child mount process represented by pid, outputs any io
14147c478bd9Sstevel@tonic-gate  * it may have, and returns its exit code.
14157c478bd9Sstevel@tonic-gate  * Sets the global exitcode if an error occurred.
14167c478bd9Sstevel@tonic-gate  */
14177c478bd9Sstevel@tonic-gate int
14187c478bd9Sstevel@tonic-gate cleanupkid(pid_t pid, int wstat)
14197c478bd9Sstevel@tonic-gate {
14207c478bd9Sstevel@tonic-gate 	vfsent_t *vp, *prevp;
14217c478bd9Sstevel@tonic-gate 	int ret;
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 	if (WIFEXITED(wstat))		/* this should always be true */
14247c478bd9Sstevel@tonic-gate 		ret = WEXITSTATUS(wstat);
14257c478bd9Sstevel@tonic-gate 	else
14267c478bd9Sstevel@tonic-gate 		ret = 1;		/* assume some kind of error */
1427a38ddfeeSTim Haley 	if (ret) {
14287c478bd9Sstevel@tonic-gate 		exitcode = 1;
1429a38ddfeeSTim Haley 		failcnt++;
1430a38ddfeeSTim Haley 	}
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate 	/*
14337c478bd9Sstevel@tonic-gate 	 * Find our child.
14347c478bd9Sstevel@tonic-gate 	 * This search gets smaller and smaller as children are cleaned
14357c478bd9Sstevel@tonic-gate 	 * up.
14367c478bd9Sstevel@tonic-gate 	 */
14377c478bd9Sstevel@tonic-gate 	for (prevp = NULL, vp = vfsll; vp; vp = vp->next) {
14387c478bd9Sstevel@tonic-gate 		if (vp->pid != pid) {
14397c478bd9Sstevel@tonic-gate 			prevp = vp;
14407c478bd9Sstevel@tonic-gate 			continue;
14417c478bd9Sstevel@tonic-gate 		}
14427c478bd9Sstevel@tonic-gate 		/*
14437c478bd9Sstevel@tonic-gate 		 * Found: let's remove it from this linked list.
14447c478bd9Sstevel@tonic-gate 		 */
14457c478bd9Sstevel@tonic-gate 		if (prevp) {
14467c478bd9Sstevel@tonic-gate 			prevp->next = vp->next;
14477c478bd9Sstevel@tonic-gate 			vp->next = NULL;
14487c478bd9Sstevel@tonic-gate 		}
14497c478bd9Sstevel@tonic-gate 		break;
14507c478bd9Sstevel@tonic-gate 	}
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate 	if (vp == NULL) {
14537c478bd9Sstevel@tonic-gate 		/*
14547c478bd9Sstevel@tonic-gate 		 * This should never happen.
14557c478bd9Sstevel@tonic-gate 		 */
14567c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(
14574bff34e3Sthurlow 		    "%s: Unknown child %d\n"), myname, pid);
14587c478bd9Sstevel@tonic-gate 		exitcode = 1;
14597c478bd9Sstevel@tonic-gate 		return (ret);
14607c478bd9Sstevel@tonic-gate 	}
14617c478bd9Sstevel@tonic-gate 	doio(vp);	/* Any output? */
14627c478bd9Sstevel@tonic-gate 
1463a38ddfeeSTim Haley 	if (vp->v.vfs_fstype &&
1464a38ddfeeSTim Haley 	    (strcmp(vp->v.vfs_fstype, MNTTYPE_LOFS) == 0)) {
14657c478bd9Sstevel@tonic-gate 		lofscnt--;
1466a38ddfeeSTim Haley 		if (ret)
1467a38ddfeeSTim Haley 			lofsfail++;
1468a38ddfeeSTim Haley 	}
14697c478bd9Sstevel@tonic-gate 
14707c478bd9Sstevel@tonic-gate 	vp->exitcode = ret;
14717c478bd9Sstevel@tonic-gate 	return (ret);
14727c478bd9Sstevel@tonic-gate }
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate 
14757c478bd9Sstevel@tonic-gate static vfsent_t zvmount = { 0 };
14767c478bd9Sstevel@tonic-gate 
14777c478bd9Sstevel@tonic-gate vfsent_t *
14787c478bd9Sstevel@tonic-gate new_vfsent(struct vfstab *vin, int order)
14797c478bd9Sstevel@tonic-gate {
14807c478bd9Sstevel@tonic-gate 	vfsent_t *new;
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate 	new = (vfsent_t *)malloc(sizeof (*new));
14837c478bd9Sstevel@tonic-gate 	if (new == NULL)
14847c478bd9Sstevel@tonic-gate 		nomem();
14857c478bd9Sstevel@tonic-gate 
14867c478bd9Sstevel@tonic-gate 	*new = zvmount;
14877c478bd9Sstevel@tonic-gate 	if (vin->vfs_special &&
14887c478bd9Sstevel@tonic-gate 	    (new->v.vfs_special = strdup(vin->vfs_special)) == NULL)
14897c478bd9Sstevel@tonic-gate 		nomem();
14907c478bd9Sstevel@tonic-gate 	if (vin->vfs_mountp &&
14917c478bd9Sstevel@tonic-gate 	    (new->v.vfs_mountp = strdup(vin->vfs_mountp)) == NULL)
14927c478bd9Sstevel@tonic-gate 		nomem();
14937c478bd9Sstevel@tonic-gate 	if (vin->vfs_fstype &&
14947c478bd9Sstevel@tonic-gate 	    (new->v.vfs_fstype = strdup(vin->vfs_fstype)) == NULL)
14957c478bd9Sstevel@tonic-gate 		nomem();
14967c478bd9Sstevel@tonic-gate 	/*
14977c478bd9Sstevel@tonic-gate 	 * If specific mount options were specified on the command
14987c478bd9Sstevel@tonic-gate 	 * line, then use those.  Else, use the ones on the vfstab
14997c478bd9Sstevel@tonic-gate 	 * line, if any.  In other words, specific options on the
15007c478bd9Sstevel@tonic-gate 	 * command line override those in /etc/vfstab.
15017c478bd9Sstevel@tonic-gate 	 */
15027c478bd9Sstevel@tonic-gate 	if (oflg) {
15037c478bd9Sstevel@tonic-gate 		if ((new->v.vfs_mntopts = strdup(specific_opts)) == NULL)
15047c478bd9Sstevel@tonic-gate 			nomem();
15057c478bd9Sstevel@tonic-gate 	} else if (vin->vfs_mntopts &&
15064bff34e3Sthurlow 	    (new->v.vfs_mntopts = strdup(vin->vfs_mntopts)) == NULL)
15077c478bd9Sstevel@tonic-gate 			nomem();
15087c478bd9Sstevel@tonic-gate 
15097c478bd9Sstevel@tonic-gate 	new->order = order;
15107c478bd9Sstevel@tonic-gate 	return (new);
15117c478bd9Sstevel@tonic-gate }
15127c478bd9Sstevel@tonic-gate 
15137c478bd9Sstevel@tonic-gate /*
15147c478bd9Sstevel@tonic-gate  * Runs realpath on vp's mount point, records success or failure,
15157c478bd9Sstevel@tonic-gate  * resets the mount level based on the new realpath, and returns
15167c478bd9Sstevel@tonic-gate  * realpath()'s return value.
15177c478bd9Sstevel@tonic-gate  */
15187c478bd9Sstevel@tonic-gate char *
15197c478bd9Sstevel@tonic-gate setrpath(vfsent_t *vp)
15207c478bd9Sstevel@tonic-gate {
15217c478bd9Sstevel@tonic-gate 	char *rp;
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 	if ((rp = realpath(vp->v.vfs_mountp, realdir)) == NULL)
15247c478bd9Sstevel@tonic-gate 		vp->flag |= VRPFAILED;
15257c478bd9Sstevel@tonic-gate 	else
15267c478bd9Sstevel@tonic-gate 		vp->flag &= ~VRPFAILED;
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate 	if (vp->rpath)
15297c478bd9Sstevel@tonic-gate 		free(vp->rpath);
15307c478bd9Sstevel@tonic-gate 	if ((vp->rpath = strdup(realdir)) == NULL)
15317c478bd9Sstevel@tonic-gate 		nomem();
15327c478bd9Sstevel@tonic-gate 	vp->mlevel = fsgetmlevel(vp->rpath);
15337c478bd9Sstevel@tonic-gate 	return (rp);
15347c478bd9Sstevel@tonic-gate }
15357c478bd9Sstevel@tonic-gate 
15367c478bd9Sstevel@tonic-gate 
15377c478bd9Sstevel@tonic-gate /*
15387c478bd9Sstevel@tonic-gate  * sort first by mlevel (1...N), then by vfstab order.
15397c478bd9Sstevel@tonic-gate  */
15407c478bd9Sstevel@tonic-gate int
15417c478bd9Sstevel@tonic-gate mlevelcmp(const void *a, const void *b)
15427c478bd9Sstevel@tonic-gate {
15437c478bd9Sstevel@tonic-gate 	vfsent_t *a1, *b1;
15447c478bd9Sstevel@tonic-gate 	int	lcmp;
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate 	a1 = *(vfsent_t **)a;
15477c478bd9Sstevel@tonic-gate 	b1 = *(vfsent_t **)b;
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate 	lcmp = a1->mlevel - b1->mlevel;
15507c478bd9Sstevel@tonic-gate 	if (lcmp == 0)
15517c478bd9Sstevel@tonic-gate 		lcmp = a1->order - b1->order;
15527c478bd9Sstevel@tonic-gate 	return (lcmp);
15537c478bd9Sstevel@tonic-gate }
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate /* sort by vfstab order.  0..N */
155608190127Sdh static int
15577c478bd9Sstevel@tonic-gate mordercmp(const void *a, const void *b)
15587c478bd9Sstevel@tonic-gate {
15597c478bd9Sstevel@tonic-gate 	vfsent_t *a1, *b1;
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate 	a1 = *(vfsent_t **)a;
15627c478bd9Sstevel@tonic-gate 	b1 = *(vfsent_t **)b;
15637c478bd9Sstevel@tonic-gate 	return (a1->order - b1->order);
15647c478bd9Sstevel@tonic-gate }
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate /*
15677c478bd9Sstevel@tonic-gate  * cleanup the existing children and exit with an error
15687c478bd9Sstevel@tonic-gate  * if asig != 0.
15697c478bd9Sstevel@tonic-gate  */
15707c478bd9Sstevel@tonic-gate void
15717c478bd9Sstevel@tonic-gate cleanup(int asig)
15727c478bd9Sstevel@tonic-gate {
15737c478bd9Sstevel@tonic-gate 	while (nrun > 0 && (dowait() != -1))
15747c478bd9Sstevel@tonic-gate 		;
15757c478bd9Sstevel@tonic-gate 
15767c478bd9Sstevel@tonic-gate 	if (asig != 0)
15777c478bd9Sstevel@tonic-gate 		exit(1);
15787c478bd9Sstevel@tonic-gate }
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate 
15817c478bd9Sstevel@tonic-gate int
15827c478bd9Sstevel@tonic-gate check_fields(char *fstype, char *mountp)
15837c478bd9Sstevel@tonic-gate {
15847c478bd9Sstevel@tonic-gate 	struct stat64 stbuf;
15857c478bd9Sstevel@tonic-gate 
158696ca3711SMarcel Telka 	if (fstype == NULL) {
158796ca3711SMarcel Telka 		fprintf(stderr,
158896ca3711SMarcel Telka 		    gettext("%s: FSType cannot be determined\n"),
158996ca3711SMarcel Telka 		    myname);
159096ca3711SMarcel Telka 		return (1);
159196ca3711SMarcel Telka 	}
15927c478bd9Sstevel@tonic-gate 	if (strlen(fstype) > (size_t)FSTYPE_MAX) {
15937c478bd9Sstevel@tonic-gate 		fprintf(stderr,
15944bff34e3Sthurlow 		    gettext("%s: FSType %s exceeds %d characters\n"),
15954bff34e3Sthurlow 		    myname, fstype, FSTYPE_MAX);
15967c478bd9Sstevel@tonic-gate 		return (1);
15977c478bd9Sstevel@tonic-gate 	}
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate 	if (mountp == NULL) {
16007c478bd9Sstevel@tonic-gate 		fprintf(stderr,
16014bff34e3Sthurlow 		    gettext("%s: Mount point cannot be determined\n"),
16024bff34e3Sthurlow 		    myname);
16037c478bd9Sstevel@tonic-gate 		return (1);
16047c478bd9Sstevel@tonic-gate 	}
16057c478bd9Sstevel@tonic-gate 	if (*mountp != '/') {
16067c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(
16074bff34e3Sthurlow 		    "%s: Mount point %s is not an absolute pathname.\n"),
16084bff34e3Sthurlow 		    myname, mountp);
16097c478bd9Sstevel@tonic-gate 		return (1);
16107c478bd9Sstevel@tonic-gate 	}
16117c478bd9Sstevel@tonic-gate 	/*
16127c478bd9Sstevel@tonic-gate 	 * Don't do some of these checks if aflg because a mount point may
16137c478bd9Sstevel@tonic-gate 	 * not exist now, but will be mounted before we get to it.
16147c478bd9Sstevel@tonic-gate 	 * This is one of the quirks of "secondary mounting".
16157c478bd9Sstevel@tonic-gate 	 */
16167c478bd9Sstevel@tonic-gate 	if (!aflg && stat64(mountp, &stbuf) < 0) {
16177c478bd9Sstevel@tonic-gate 		if (errno == ENOENT || errno == ENOTDIR)
16187c478bd9Sstevel@tonic-gate 			fprintf(stderr,
16194bff34e3Sthurlow 			    gettext("%s: Mount point %s does not exist.\n"),
16204bff34e3Sthurlow 			    myname, mountp);
16217c478bd9Sstevel@tonic-gate 		else {
16227c478bd9Sstevel@tonic-gate 			fprintf(stderr,
16234bff34e3Sthurlow 			    gettext("%s: Cannot stat mount point %s.\n"),
16244bff34e3Sthurlow 			    myname, mountp);
16257c478bd9Sstevel@tonic-gate 			perror(myname);
16267c478bd9Sstevel@tonic-gate 		}
16277c478bd9Sstevel@tonic-gate 		return (1);
16287c478bd9Sstevel@tonic-gate 	}
16297c478bd9Sstevel@tonic-gate 	return (0);
16307c478bd9Sstevel@tonic-gate }
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate void
163308190127Sdh nomem(void)
16347c478bd9Sstevel@tonic-gate {
16357c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext("%s: Out of memory\n"), myname);
16367c478bd9Sstevel@tonic-gate 	while (nrun > 0 && (dowait() != -1))
16377c478bd9Sstevel@tonic-gate 		;
16387c478bd9Sstevel@tonic-gate 	exit(1);
16397c478bd9Sstevel@tonic-gate }
1640