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