xref: /titanic_53/usr/src/cmd/fs.d/umount.c (revision a237e38e9161f0acd6451439d4a7dd597e66291d)
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 /*
22*a237e38eSth199096  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include	<stdio.h>
33004388ebScasper #include	<stdio_ext.h>
347c478bd9Sstevel@tonic-gate #include	<limits.h>
357c478bd9Sstevel@tonic-gate #include	<unistd.h>
367c478bd9Sstevel@tonic-gate #include	<stdlib.h>
377c478bd9Sstevel@tonic-gate #include	<string.h>
387c478bd9Sstevel@tonic-gate #include	<sys/signal.h>
397c478bd9Sstevel@tonic-gate #include	<sys/mnttab.h>
407c478bd9Sstevel@tonic-gate #include	<errno.h>
417c478bd9Sstevel@tonic-gate #include	<sys/types.h>
427c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
437c478bd9Sstevel@tonic-gate #include	<sys/param.h>
447c478bd9Sstevel@tonic-gate #include	<sys/wait.h>
457c478bd9Sstevel@tonic-gate #include	<sys/vfstab.h>
467c478bd9Sstevel@tonic-gate #include	<sys/fcntl.h>
477c478bd9Sstevel@tonic-gate #include	<sys/resource.h>
487c478bd9Sstevel@tonic-gate #include	<sys/mntent.h>
497c478bd9Sstevel@tonic-gate #include	<sys/ctfs.h>
507c478bd9Sstevel@tonic-gate #include	<locale.h>
517c478bd9Sstevel@tonic-gate #include	<stdarg.h>
527c478bd9Sstevel@tonic-gate #include	<sys/mount.h>
537c478bd9Sstevel@tonic-gate #include	<sys/objfs.h>
547c478bd9Sstevel@tonic-gate #include	"fslib.h"
55*a237e38eSth199096 #include	<sharefs/share.h>
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #define	FS_PATH		"/usr/lib/fs"
587c478bd9Sstevel@tonic-gate #define	ALT_PATH	"/etc/fs"
597c478bd9Sstevel@tonic-gate #define	FULLPATH_MAX	32
607c478bd9Sstevel@tonic-gate #define	FSTYPE_MAX	8
617c478bd9Sstevel@tonic-gate #define	ARGV_MAX	16
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate int	aflg, oflg, Vflg, dashflg, dflg, fflg;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate extern void	rpterr(), usage(), mnterror();
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate extern	char	*optarg;	/* used by getopt */
687c478bd9Sstevel@tonic-gate extern	int	optind, opterr;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate static char	*myname;
717c478bd9Sstevel@tonic-gate char	fs_path[] = FS_PATH;
727c478bd9Sstevel@tonic-gate char	alt_path[] = ALT_PATH;
737c478bd9Sstevel@tonic-gate char	mnttab[MAXPATHLEN + 1];
747c478bd9Sstevel@tonic-gate char	*oarg, *farg;
757c478bd9Sstevel@tonic-gate int	maxrun, nrun;
767c478bd9Sstevel@tonic-gate int	no_mnttab;
777c478bd9Sstevel@tonic-gate int	lofscnt;		/* presence of lofs prohibits parallel */
787c478bd9Sstevel@tonic-gate 				/* umounting */
797c478bd9Sstevel@tonic-gate int	exitcode;
807c478bd9Sstevel@tonic-gate char	resolve[MAXPATHLEN];
817c478bd9Sstevel@tonic-gate static  char ibuf[BUFSIZ];
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate  * Currently, mounting cachefs's simultaneous uncovers various problems.
857c478bd9Sstevel@tonic-gate  * For the short term, we serialize cachefs activity while we fix
867c478bd9Sstevel@tonic-gate  * these cachefs bugs.
877c478bd9Sstevel@tonic-gate  */
887c478bd9Sstevel@tonic-gate #define	CACHEFS_BUG
897c478bd9Sstevel@tonic-gate #ifdef	CACHEFS_BUG
907c478bd9Sstevel@tonic-gate #include	<sys/fs/cachefs_fs.h>	/* for BACKMNT_NAME */
917c478bd9Sstevel@tonic-gate int	cachefs_running;	/* parallel cachefs not supported yet */
927c478bd9Sstevel@tonic-gate #endif
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * The basic mount struct that describes an mnttab entry.
967c478bd9Sstevel@tonic-gate  * It is used both in an array and as a linked list elem.
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate typedef struct mountent {
1007c478bd9Sstevel@tonic-gate 	struct mnttab	ment;		/* the mnttab data */
1017c478bd9Sstevel@tonic-gate 	int		mlevel;		/* mount level of the mount pt */
1027c478bd9Sstevel@tonic-gate 	pid_t		pid;		/* the pid of this mount process */
1037c478bd9Sstevel@tonic-gate #define	RDPIPE		0
1047c478bd9Sstevel@tonic-gate #define	WRPIPE		1
1057c478bd9Sstevel@tonic-gate 	int		sopipe[2];	/* pipe attached to child's stdout */
1067c478bd9Sstevel@tonic-gate 	int		sepipe[2];	/* pipe attached to child's stderr */
1077c478bd9Sstevel@tonic-gate 	struct mountent *link;		/* used when in linked list */
1087c478bd9Sstevel@tonic-gate } mountent_t;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate static mountent_t	*mntll;		/* head of global linked list of */
1117c478bd9Sstevel@tonic-gate 					/* mountents */
1127c478bd9Sstevel@tonic-gate int			listlength;	/* # of elems in this list */
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate  * If the automatic flag (-a) is given and mount points are not specified
1167c478bd9Sstevel@tonic-gate  * on the command line, then do not attempt to umount these.  These
1177c478bd9Sstevel@tonic-gate  * generally need to be kept mounted until system shutdown.
1187c478bd9Sstevel@tonic-gate  */
1197c478bd9Sstevel@tonic-gate static const char   *keeplist[] = {
1207c478bd9Sstevel@tonic-gate 	"/",
1217c478bd9Sstevel@tonic-gate 	"/dev",
1227c478bd9Sstevel@tonic-gate 	"/dev/fd",
1237c478bd9Sstevel@tonic-gate 	"/devices",
1247c478bd9Sstevel@tonic-gate 	"/etc/mnttab",
1257c478bd9Sstevel@tonic-gate 	"/etc/svc/volatile",
1267c478bd9Sstevel@tonic-gate 	"/lib",
1277c478bd9Sstevel@tonic-gate 	"/proc",
1287c478bd9Sstevel@tonic-gate 	"/sbin",
1297c478bd9Sstevel@tonic-gate 	CTFS_ROOT,
1307c478bd9Sstevel@tonic-gate 	OBJFS_ROOT,
1317c478bd9Sstevel@tonic-gate 	"/tmp",
1327c478bd9Sstevel@tonic-gate 	"/usr",
1337c478bd9Sstevel@tonic-gate 	"/var",
1347c478bd9Sstevel@tonic-gate 	"/var/adm",
1357c478bd9Sstevel@tonic-gate 	"/var/run",
136*a237e38eSth199096 	SHARETAB,
1377c478bd9Sstevel@tonic-gate 	NULL
1387c478bd9Sstevel@tonic-gate };
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate static void	nomem();
1417c478bd9Sstevel@tonic-gate static void	doexec(struct mnttab *);
1427c478bd9Sstevel@tonic-gate static int	setup_iopipe(mountent_t *);
1437c478bd9Sstevel@tonic-gate static void	setup_output(mountent_t *);
1447c478bd9Sstevel@tonic-gate static void	doio(mountent_t *);
1457c478bd9Sstevel@tonic-gate static void	do_umounts(mountent_t **);
1467c478bd9Sstevel@tonic-gate static int	dowait();
1477c478bd9Sstevel@tonic-gate static int	parumount();
1487c478bd9Sstevel@tonic-gate static int	mcompar(const void *, const void *);
1497c478bd9Sstevel@tonic-gate static void	cleanup(int);
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate static mountent_t	**make_mntarray(char **, int);
1527c478bd9Sstevel@tonic-gate static mountent_t	*getmntall();
1537c478bd9Sstevel@tonic-gate static mountent_t 	*new_mountent(struct mnttab *);
1547c478bd9Sstevel@tonic-gate static mountent_t	*getmntlast(mountent_t *, char *, char *);
1557c478bd9Sstevel@tonic-gate 
15608190127Sdh145677 int
15708190127Sdh145677 main(int argc, char **argv)
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate 	int 	cc;
1607c478bd9Sstevel@tonic-gate 	struct mnttab  mget;
1617c478bd9Sstevel@tonic-gate 	char 	*mname, *is_special;
1627c478bd9Sstevel@tonic-gate 	int	fscnt;
1637c478bd9Sstevel@tonic-gate 	mountent_t	*mp;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
1687c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
1697c478bd9Sstevel@tonic-gate #endif
1707c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	myname = strrchr(argv[0], '/');
1737c478bd9Sstevel@tonic-gate 	if (myname)
1747c478bd9Sstevel@tonic-gate 		myname++;
1757c478bd9Sstevel@tonic-gate 	else
1767c478bd9Sstevel@tonic-gate 		myname = argv[0];
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	/*
1797c478bd9Sstevel@tonic-gate 	 * Process the args.
1807c478bd9Sstevel@tonic-gate 	 * "-d" for compatibility
1817c478bd9Sstevel@tonic-gate 	 */
1827c478bd9Sstevel@tonic-gate 	while ((cc = getopt(argc, argv, "ado:Vf?")) != -1)
1837c478bd9Sstevel@tonic-gate 		switch (cc) {
1847c478bd9Sstevel@tonic-gate 		case 'a':
1857c478bd9Sstevel@tonic-gate 			aflg++;
1867c478bd9Sstevel@tonic-gate 			break;
1877c478bd9Sstevel@tonic-gate #ifdef DEBUG
1887c478bd9Sstevel@tonic-gate 		case 'd':
1897c478bd9Sstevel@tonic-gate 			dflg++;
1907c478bd9Sstevel@tonic-gate 			break;
1917c478bd9Sstevel@tonic-gate #endif
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 		case '?':
1947c478bd9Sstevel@tonic-gate 			usage();
1957c478bd9Sstevel@tonic-gate 			break;
1967c478bd9Sstevel@tonic-gate 		case 'o':
1977c478bd9Sstevel@tonic-gate 			if (oflg)
1987c478bd9Sstevel@tonic-gate 				usage();
1997c478bd9Sstevel@tonic-gate 			else {
2007c478bd9Sstevel@tonic-gate 				oflg++;
2017c478bd9Sstevel@tonic-gate 				oarg = optarg;
2027c478bd9Sstevel@tonic-gate 			}
2037c478bd9Sstevel@tonic-gate 			break;
2047c478bd9Sstevel@tonic-gate 		case 'f':
2057c478bd9Sstevel@tonic-gate 			fflg++;
2067c478bd9Sstevel@tonic-gate 			break;
2077c478bd9Sstevel@tonic-gate 		case 'V':
2087c478bd9Sstevel@tonic-gate 			if (Vflg)
2097c478bd9Sstevel@tonic-gate 				usage();
2107c478bd9Sstevel@tonic-gate 			else
2117c478bd9Sstevel@tonic-gate 				Vflg++;
2127c478bd9Sstevel@tonic-gate 			break;
2137c478bd9Sstevel@tonic-gate 		default:
2147c478bd9Sstevel@tonic-gate 			usage();
2157c478bd9Sstevel@tonic-gate 			break;
2167c478bd9Sstevel@tonic-gate 		}
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	fscnt = argc - optind;
2197c478bd9Sstevel@tonic-gate 	if (!aflg && fscnt != 1)
2207c478bd9Sstevel@tonic-gate 		usage();
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	/* copy '--' to specific */
2237c478bd9Sstevel@tonic-gate 	if (strcmp(argv[optind-1], "--") == 0)
2247c478bd9Sstevel@tonic-gate 		dashflg++;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	/*
2277c478bd9Sstevel@tonic-gate 	 * mnttab may be a symlink to a file in another file system.
2287c478bd9Sstevel@tonic-gate 	 * This happens during install when / is mounted read-only
2297c478bd9Sstevel@tonic-gate 	 * and /etc/mnttab is symlinked to a file in /tmp.
2307c478bd9Sstevel@tonic-gate 	 * If this is the case, we need to follow the symlink to the
2317c478bd9Sstevel@tonic-gate 	 * read-write file itself so that the subsequent mnttab.temp
2327c478bd9Sstevel@tonic-gate 	 * open and rename will work.
2337c478bd9Sstevel@tonic-gate 	 */
2347c478bd9Sstevel@tonic-gate 	if (realpath(MNTTAB, mnttab) == NULL) {
2357c478bd9Sstevel@tonic-gate 		strcpy(mnttab, MNTTAB);
2367c478bd9Sstevel@tonic-gate 	}
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	/*
2397c478bd9Sstevel@tonic-gate 	 * bugid 1205242
2407c478bd9Sstevel@tonic-gate 	 * call the realpath() here, so that if the user is
2417c478bd9Sstevel@tonic-gate 	 * trying to umount an autofs directory, the directory
2427c478bd9Sstevel@tonic-gate 	 * is forced to mount.
2437c478bd9Sstevel@tonic-gate 	 */
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	mname = argv[optind];
2467c478bd9Sstevel@tonic-gate 	is_special = realpath(mname, resolve);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	/*
2497c478bd9Sstevel@tonic-gate 	 * Read the whole mnttab into memory.
2507c478bd9Sstevel@tonic-gate 	 */
2517c478bd9Sstevel@tonic-gate 	mntll = getmntall();
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	if (aflg && fscnt != 1)
2547c478bd9Sstevel@tonic-gate 		exit(parumount(argv + optind, fscnt));
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	aflg = 0;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	mntnull(&mget);
2597c478bd9Sstevel@tonic-gate 	if (listlength == 0) {
2607c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(
2617c478bd9Sstevel@tonic-gate 			"%s: warning: no entries found in %s\n"),
2627c478bd9Sstevel@tonic-gate 				myname, mnttab);
2637c478bd9Sstevel@tonic-gate 		mget.mnt_mountp = mname;	/* assume mount point */
2647c478bd9Sstevel@tonic-gate 		no_mnttab++;
2657c478bd9Sstevel@tonic-gate 		doexec(&mget);
2667c478bd9Sstevel@tonic-gate 		exit(0);
2677c478bd9Sstevel@tonic-gate 	}
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	mp = NULL;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	/*
2727c478bd9Sstevel@tonic-gate 	 * if realpath fails, it can't be a mount point, so we'll
2737c478bd9Sstevel@tonic-gate 	 * go straight to the code that treats the arg as a special.
2747c478bd9Sstevel@tonic-gate 	 * if realpath succeeds, it could be a special or a mount point;
2757c478bd9Sstevel@tonic-gate 	 * we'll start by assuming it's a mount point, and if it's not,
2767c478bd9Sstevel@tonic-gate 	 * try to treat it as a special.
2777c478bd9Sstevel@tonic-gate 	 */
2787c478bd9Sstevel@tonic-gate 	if (is_special != NULL) {
2797c478bd9Sstevel@tonic-gate 		/*
2807c478bd9Sstevel@tonic-gate 		 * if this succeeds,
2817c478bd9Sstevel@tonic-gate 		 * we'll have the appropriate record; if it fails
2827c478bd9Sstevel@tonic-gate 		 * we'll assume the arg is a special of some sort
2837c478bd9Sstevel@tonic-gate 		 */
2847c478bd9Sstevel@tonic-gate 		mp = getmntlast(mntll, NULL, resolve);
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 	/*
2877c478bd9Sstevel@tonic-gate 	 * Since stackable mount is allowed (RFE 2001535),
2887c478bd9Sstevel@tonic-gate 	 * we will un-mount the last entry in the MNTTAB that matches.
2897c478bd9Sstevel@tonic-gate 	 */
2907c478bd9Sstevel@tonic-gate 	if (mp == NULL) {
2917c478bd9Sstevel@tonic-gate 		/*
2927c478bd9Sstevel@tonic-gate 		 * Perhaps there is a bogus mnttab entry that
2937c478bd9Sstevel@tonic-gate 		 * can't be resolved:
2947c478bd9Sstevel@tonic-gate 		 */
2957c478bd9Sstevel@tonic-gate 		if ((mp = getmntlast(mntll, NULL, mname)) == NULL)
2967c478bd9Sstevel@tonic-gate 			/*
2977c478bd9Sstevel@tonic-gate 			 * assume it's a device (special) now
2987c478bd9Sstevel@tonic-gate 			 */
2997c478bd9Sstevel@tonic-gate 			mp = getmntlast(mntll, mname, NULL);
3007c478bd9Sstevel@tonic-gate 		if (mp) {
3017c478bd9Sstevel@tonic-gate 			/*
3027c478bd9Sstevel@tonic-gate 			 * Found it.
3037c478bd9Sstevel@tonic-gate 			 * This is a device. Now we want to know if
3047c478bd9Sstevel@tonic-gate 			 * it stackmounted on by something else.
3057c478bd9Sstevel@tonic-gate 			 * The original fix for bug 1103850 has a
3067c478bd9Sstevel@tonic-gate 			 * problem with lockfs (bug 1119731). This
3077c478bd9Sstevel@tonic-gate 			 * is a revised method.
3087c478bd9Sstevel@tonic-gate 			 */
3097c478bd9Sstevel@tonic-gate 			mountent_t *lmp;
3107c478bd9Sstevel@tonic-gate 			lmp = getmntlast(mntll, NULL, mp->ment.mnt_mountp);
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 			if (lmp && strcmp(lmp->ment.mnt_special,
3137c478bd9Sstevel@tonic-gate 					mp->ment.mnt_special)) {
3147c478bd9Sstevel@tonic-gate 				errno = EBUSY;
3157c478bd9Sstevel@tonic-gate 				rpterr(mname);
3167c478bd9Sstevel@tonic-gate 				exit(1);
3177c478bd9Sstevel@tonic-gate 			}
3187c478bd9Sstevel@tonic-gate 		} else {
3197c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(
3207c478bd9Sstevel@tonic-gate 				"%s: warning: %s not in mnttab\n"),
3217c478bd9Sstevel@tonic-gate 				myname, mname);
3227c478bd9Sstevel@tonic-gate 			if (Vflg)
3237c478bd9Sstevel@tonic-gate 				exit(1);
3247c478bd9Sstevel@tonic-gate 				/*
3257c478bd9Sstevel@tonic-gate 				 * same error as mount -V
3267c478bd9Sstevel@tonic-gate 				 * would give for unknown
3277c478bd9Sstevel@tonic-gate 				 * mount point
3287c478bd9Sstevel@tonic-gate 				 */
3297c478bd9Sstevel@tonic-gate 			mget.mnt_special = mget.mnt_mountp = mname;
3307c478bd9Sstevel@tonic-gate 		}
3317c478bd9Sstevel@tonic-gate 	}
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	if (mp)
3347c478bd9Sstevel@tonic-gate 		doexec(&mp->ment);
3357c478bd9Sstevel@tonic-gate 	else
3367c478bd9Sstevel@tonic-gate 		doexec(&mget);
3377c478bd9Sstevel@tonic-gate 
33808190127Sdh145677 	return (0);
3397c478bd9Sstevel@tonic-gate }
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate void
3427c478bd9Sstevel@tonic-gate doexec(struct mnttab *ment)
3437c478bd9Sstevel@tonic-gate {
3447c478bd9Sstevel@tonic-gate 	int 	ret;
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate #ifdef DEBUG
3477c478bd9Sstevel@tonic-gate 	if (dflg)
3487c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%d: umounting %s\n",
3497c478bd9Sstevel@tonic-gate 			getpid(), ment->mnt_mountp);
3507c478bd9Sstevel@tonic-gate #endif
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	/* try to exec the dependent portion */
3537c478bd9Sstevel@tonic-gate 	if ((ment->mnt_fstype != NULL) || Vflg) {
3547c478bd9Sstevel@tonic-gate 		char	full_path[FULLPATH_MAX];
3557c478bd9Sstevel@tonic-gate 		char	alter_path[FULLPATH_MAX];
3567c478bd9Sstevel@tonic-gate 		char	*newargv[ARGV_MAX];
3577c478bd9Sstevel@tonic-gate 		int 	ii;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 		if (strlen(ment->mnt_fstype) > (size_t)FSTYPE_MAX) {
3607c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(
3617c478bd9Sstevel@tonic-gate 				"%s: FSType %s exceeds %d characters\n"),
3627c478bd9Sstevel@tonic-gate 				myname, ment->mnt_fstype, FSTYPE_MAX);
3637c478bd9Sstevel@tonic-gate 			exit(1);
3647c478bd9Sstevel@tonic-gate 		}
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 		/* build the full pathname of the fstype dependent command. */
3677c478bd9Sstevel@tonic-gate 		sprintf(full_path, "%s/%s/%s", fs_path, ment->mnt_fstype,
3687c478bd9Sstevel@tonic-gate 					myname);
3697c478bd9Sstevel@tonic-gate 		sprintf(alter_path, "%s/%s/%s", alt_path, ment->mnt_fstype,
3707c478bd9Sstevel@tonic-gate 					myname);
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 		/*
3737c478bd9Sstevel@tonic-gate 		 * create the new arg list, and end the list with a
3747c478bd9Sstevel@tonic-gate 		 * null pointer
3757c478bd9Sstevel@tonic-gate 		 */
3767c478bd9Sstevel@tonic-gate 		ii = 2;
3777c478bd9Sstevel@tonic-gate 		if (oflg) {
3787c478bd9Sstevel@tonic-gate 			newargv[ii++] = "-o";
3797c478bd9Sstevel@tonic-gate 			newargv[ii++] = oarg;
3807c478bd9Sstevel@tonic-gate 		}
3817c478bd9Sstevel@tonic-gate 		if (dashflg) {
3827c478bd9Sstevel@tonic-gate 			newargv[ii++] = "--";
3837c478bd9Sstevel@tonic-gate 		}
3847c478bd9Sstevel@tonic-gate 		if (fflg) {
3857c478bd9Sstevel@tonic-gate 			newargv[ii++] = "-f";
3867c478bd9Sstevel@tonic-gate 		}
3877c478bd9Sstevel@tonic-gate 		newargv[ii++] = (ment->mnt_mountp)
3887c478bd9Sstevel@tonic-gate 				? ment->mnt_mountp : ment->mnt_special;
3897c478bd9Sstevel@tonic-gate 		newargv[ii] = NULL;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 		/* set the new argv[0] to the filename */
3927c478bd9Sstevel@tonic-gate 		newargv[1] = myname;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 		if (Vflg) {
3957c478bd9Sstevel@tonic-gate 			printf("%s", myname);
3967c478bd9Sstevel@tonic-gate 			for (ii = 2; newargv[ii]; ii++)
3977c478bd9Sstevel@tonic-gate 				printf(" %s", newargv[ii]);
3987c478bd9Sstevel@tonic-gate 			printf("\n");
3997c478bd9Sstevel@tonic-gate 			fflush(stdout);
4007c478bd9Sstevel@tonic-gate 			exit(0);
4017c478bd9Sstevel@tonic-gate 		}
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 		/* Try to exec the fstype dependent umount. */
4047c478bd9Sstevel@tonic-gate 		execv(full_path, &newargv[1]);
4057c478bd9Sstevel@tonic-gate 		if (errno == ENOEXEC) {
4067c478bd9Sstevel@tonic-gate 			newargv[0] = "sh";
4077c478bd9Sstevel@tonic-gate 			newargv[1] = full_path;
4087c478bd9Sstevel@tonic-gate 			execv("/sbin/sh", &newargv[0]);
4097c478bd9Sstevel@tonic-gate 		}
4107c478bd9Sstevel@tonic-gate 		newargv[1] = myname;
4117c478bd9Sstevel@tonic-gate 		execv(alter_path, &newargv[1]);
4127c478bd9Sstevel@tonic-gate 		if (errno == ENOEXEC) {
4137c478bd9Sstevel@tonic-gate 			newargv[0] = "sh";
4147c478bd9Sstevel@tonic-gate 			newargv[1] = alter_path;
4157c478bd9Sstevel@tonic-gate 			execv("/sbin/sh", &newargv[0]);
4167c478bd9Sstevel@tonic-gate 		}
4177c478bd9Sstevel@tonic-gate 		/* exec failed */
4187c478bd9Sstevel@tonic-gate 		if (errno != ENOENT) {
4197c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("umount: cannot execute %s\n"),
4207c478bd9Sstevel@tonic-gate 					full_path);
4217c478bd9Sstevel@tonic-gate 			exit(1);
4227c478bd9Sstevel@tonic-gate 		}
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 	/*
4257c478bd9Sstevel@tonic-gate 	 * No fstype independent executable then.  We'll go generic
4267c478bd9Sstevel@tonic-gate 	 * from here.
4277c478bd9Sstevel@tonic-gate 	 */
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	/* don't use -o with generic */
4307c478bd9Sstevel@tonic-gate 	if (oflg) {
4317c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(
4327c478bd9Sstevel@tonic-gate 	"%s: %s specific umount does not exist; -o suboption ignored\n"),
4337c478bd9Sstevel@tonic-gate 		myname, ment->mnt_fstype ? ment->mnt_fstype : "<null>");
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	signal(SIGHUP,  SIG_IGN);
4377c478bd9Sstevel@tonic-gate 	signal(SIGQUIT, SIG_IGN);
4387c478bd9Sstevel@tonic-gate 	signal(SIGINT,  SIG_IGN);
4397c478bd9Sstevel@tonic-gate 	/*
4407c478bd9Sstevel@tonic-gate 	 * Try to umount the mountpoint.
4417c478bd9Sstevel@tonic-gate 	 * If that fails, try the corresponding special.
4427c478bd9Sstevel@tonic-gate 	 * (This ordering is necessary for nfs umounts.)
4437c478bd9Sstevel@tonic-gate 	 * (for remote resources:  if the first umount returns EBUSY
4447c478bd9Sstevel@tonic-gate 	 * don't call umount again - umount() with a resource name
4457c478bd9Sstevel@tonic-gate 	 * will return a misleading error to the user
4467c478bd9Sstevel@tonic-gate 	 */
4477c478bd9Sstevel@tonic-gate 	if (fflg) {
4487c478bd9Sstevel@tonic-gate 		if (((ret = umount2(ment->mnt_mountp, MS_FORCE)) < 0) &&
4497c478bd9Sstevel@tonic-gate 				(errno != EBUSY && errno != ENOTSUP &&
4507c478bd9Sstevel@tonic-gate 				errno != EPERM))
4517c478bd9Sstevel@tonic-gate 			ret = umount2(ment->mnt_special, MS_FORCE);
4527c478bd9Sstevel@tonic-gate 	} else {
4537c478bd9Sstevel@tonic-gate 		if (((ret = umount2(ment->mnt_mountp, 0)) < 0) &&
4547c478bd9Sstevel@tonic-gate 				(errno != EBUSY) && (errno != EPERM))
4557c478bd9Sstevel@tonic-gate 			ret = umount2(ment->mnt_special, 0);
4567c478bd9Sstevel@tonic-gate 	}
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	if (ret < 0) {
4597c478bd9Sstevel@tonic-gate 		rpterr(ment->mnt_mountp);
4607c478bd9Sstevel@tonic-gate 		if (errno != EINVAL && errno != EFAULT)
4617c478bd9Sstevel@tonic-gate 			exit(1);
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 		exitcode = 1;
4647c478bd9Sstevel@tonic-gate 	}
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	exit(exitcode);
4677c478bd9Sstevel@tonic-gate }
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate void
47008190127Sdh145677 rpterr(char *sp)
4717c478bd9Sstevel@tonic-gate {
4727c478bd9Sstevel@tonic-gate 	switch (errno) {
4737c478bd9Sstevel@tonic-gate 	case EPERM:
4747c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: permission denied\n"), myname);
4757c478bd9Sstevel@tonic-gate 		break;
4767c478bd9Sstevel@tonic-gate 	case ENXIO:
4777c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: %s no device\n"), myname, sp);
4787c478bd9Sstevel@tonic-gate 		break;
4797c478bd9Sstevel@tonic-gate 	case ENOENT:
4807c478bd9Sstevel@tonic-gate 		fprintf(stderr,
4817c478bd9Sstevel@tonic-gate 			gettext("%s: %s no such file or directory\n"),
4827c478bd9Sstevel@tonic-gate 			myname, sp);
4837c478bd9Sstevel@tonic-gate 		break;
4847c478bd9Sstevel@tonic-gate 	case EINVAL:
4857c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: %s not mounted\n"), myname, sp);
4867c478bd9Sstevel@tonic-gate 		break;
4877c478bd9Sstevel@tonic-gate 	case EBUSY:
4887c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: %s busy\n"), myname, sp);
4897c478bd9Sstevel@tonic-gate 		break;
4907c478bd9Sstevel@tonic-gate 	case ENOTBLK:
4917c478bd9Sstevel@tonic-gate 		fprintf(stderr,
4927c478bd9Sstevel@tonic-gate 			gettext("%s: %s block device required\n"), myname, sp);
4937c478bd9Sstevel@tonic-gate 		break;
4947c478bd9Sstevel@tonic-gate 	case ECOMM:
4957c478bd9Sstevel@tonic-gate 		fprintf(stderr,
4967c478bd9Sstevel@tonic-gate 			gettext("%s: warning: broken link detected\n"), myname);
4977c478bd9Sstevel@tonic-gate 		break;
4987c478bd9Sstevel@tonic-gate 	default:
4997c478bd9Sstevel@tonic-gate 		perror(myname);
5007c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: cannot unmount %s\n"), myname, sp);
5017c478bd9Sstevel@tonic-gate 	}
5027c478bd9Sstevel@tonic-gate }
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate void
50508190127Sdh145677 usage(void)
5067c478bd9Sstevel@tonic-gate {
5077c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext(
5087c478bd9Sstevel@tonic-gate "Usage:\n%s [-f] [-V] [-o specific_options] {special | mount-point}\n"),
5097c478bd9Sstevel@tonic-gate 		myname);
5107c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext(
5117c478bd9Sstevel@tonic-gate "%s -a [-f] [-V] [-o specific_options] [mount_point ...]\n"), myname);
5127c478bd9Sstevel@tonic-gate 	exit(1);
5137c478bd9Sstevel@tonic-gate }
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate void
51608190127Sdh145677 mnterror(int flag)
5177c478bd9Sstevel@tonic-gate {
5187c478bd9Sstevel@tonic-gate 	switch (flag) {
5197c478bd9Sstevel@tonic-gate 	case MNT_TOOLONG:
5207c478bd9Sstevel@tonic-gate 		fprintf(stderr,
5217c478bd9Sstevel@tonic-gate 			gettext("%s: line in mnttab exceeds %d characters\n"),
5227c478bd9Sstevel@tonic-gate 			myname, MNT_LINE_MAX-2);
5237c478bd9Sstevel@tonic-gate 		break;
5247c478bd9Sstevel@tonic-gate 	case MNT_TOOFEW:
5257c478bd9Sstevel@tonic-gate 		fprintf(stderr,
5267c478bd9Sstevel@tonic-gate 			gettext("%s: line in mnttab has too few entries\n"),
5277c478bd9Sstevel@tonic-gate 			myname);
5287c478bd9Sstevel@tonic-gate 		break;
5297c478bd9Sstevel@tonic-gate 	default:
5307c478bd9Sstevel@tonic-gate 		break;
5317c478bd9Sstevel@tonic-gate 	}
5327c478bd9Sstevel@tonic-gate }
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate /*
5357c478bd9Sstevel@tonic-gate  * Search the mlist linked list for the
5367c478bd9Sstevel@tonic-gate  * first match of specp or mntp.  The list is expected to be in reverse
5377c478bd9Sstevel@tonic-gate  * order of /etc/mnttab.
5387c478bd9Sstevel@tonic-gate  * If both are specified, then both have to match.
5397c478bd9Sstevel@tonic-gate  * Returns the (mountent_t *) of the match, otherwise returns NULL.
5407c478bd9Sstevel@tonic-gate  */
5417c478bd9Sstevel@tonic-gate mountent_t *
5427c478bd9Sstevel@tonic-gate getmntlast(mountent_t *mlist, char *specp, char *mntp)
5437c478bd9Sstevel@tonic-gate {
5447c478bd9Sstevel@tonic-gate 	int		mfound, sfound;
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	for (/* */; mlist; mlist = mlist->link) {
5477c478bd9Sstevel@tonic-gate 		mfound = sfound = 0;
5487c478bd9Sstevel@tonic-gate 		if (mntp && (strcmp(mlist->ment.mnt_mountp, mntp) == 0)) {
5497c478bd9Sstevel@tonic-gate 			if (specp == NULL)
5507c478bd9Sstevel@tonic-gate 				return (mlist);
5517c478bd9Sstevel@tonic-gate 			mfound++;
5527c478bd9Sstevel@tonic-gate 		}
5537c478bd9Sstevel@tonic-gate 		if (specp && (strcmp(mlist->ment.mnt_special, specp) == 0)) {
5547c478bd9Sstevel@tonic-gate 			if (mntp == NULL)
5557c478bd9Sstevel@tonic-gate 				return (mlist);
5567c478bd9Sstevel@tonic-gate 			sfound++;
5577c478bd9Sstevel@tonic-gate 		}
5587c478bd9Sstevel@tonic-gate 		if (mfound && sfound)
5597c478bd9Sstevel@tonic-gate 			return (mlist);
5607c478bd9Sstevel@tonic-gate 	}
5617c478bd9Sstevel@tonic-gate 	return (NULL);
5627c478bd9Sstevel@tonic-gate }
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate /*
5677c478bd9Sstevel@tonic-gate  * Perform the parallel version of umount.  Returns 0 if no errors occurred,
5687c478bd9Sstevel@tonic-gate  * non zero otherwise.
5697c478bd9Sstevel@tonic-gate  */
5707c478bd9Sstevel@tonic-gate int
5717c478bd9Sstevel@tonic-gate parumount(char **mntlist, int count)
5727c478bd9Sstevel@tonic-gate {
5737c478bd9Sstevel@tonic-gate 	int 		maxfd = OPEN_MAX;
5747c478bd9Sstevel@tonic-gate 	struct rlimit 	rl;
5757c478bd9Sstevel@tonic-gate 	mountent_t	**mntarray, **ml, *mp;
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	/*
5787c478bd9Sstevel@tonic-gate 	 * If no mount points are specified and none were found in mnttab,
5797c478bd9Sstevel@tonic-gate 	 * then end it all here.
5807c478bd9Sstevel@tonic-gate 	 */
5817c478bd9Sstevel@tonic-gate 	if (count == 0 && mntll == NULL)
5827c478bd9Sstevel@tonic-gate 		return (0);
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	/*
5857c478bd9Sstevel@tonic-gate 	 * This is the process scaling section.  After running a series
5867c478bd9Sstevel@tonic-gate 	 * of tests based on the number of simultaneous processes and
5877c478bd9Sstevel@tonic-gate 	 * processors available, optimum performance was achieved near or
5887c478bd9Sstevel@tonic-gate 	 * at (PROCN * 2).
5897c478bd9Sstevel@tonic-gate 	 */
5907c478bd9Sstevel@tonic-gate 	if ((maxrun = sysconf(_SC_NPROCESSORS_ONLN)) == -1)
5917c478bd9Sstevel@tonic-gate 		maxrun = 4;
5927c478bd9Sstevel@tonic-gate 	else
5937c478bd9Sstevel@tonic-gate 		maxrun = maxrun * 2 + 1;
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
5967c478bd9Sstevel@tonic-gate 		rl.rlim_cur = rl.rlim_max;
5977c478bd9Sstevel@tonic-gate 		if (setrlimit(RLIMIT_NOFILE, &rl) == 0)
5987c478bd9Sstevel@tonic-gate 			maxfd = (int)rl.rlim_cur;
599004388ebScasper 		(void) enable_extended_FILE_stdio(-1, -1);
6007c478bd9Sstevel@tonic-gate 	}
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	/*
6037c478bd9Sstevel@tonic-gate 	 * The parent needs to maintain 3 of its own fd's, plus 2 for
6047c478bd9Sstevel@tonic-gate 	 * each child (the stdout and stderr pipes).
6057c478bd9Sstevel@tonic-gate 	 */
6067c478bd9Sstevel@tonic-gate 	maxfd = (maxfd / 2) - 6;	/* 6 takes care of temporary  */
6077c478bd9Sstevel@tonic-gate 					/* periods of open fds */
6087c478bd9Sstevel@tonic-gate 	if (maxfd < maxrun)
6097c478bd9Sstevel@tonic-gate 		maxrun = maxfd;
6107c478bd9Sstevel@tonic-gate 	if (maxrun < 4)
6117c478bd9Sstevel@tonic-gate 		maxrun = 4;		/* sanity check */
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	mntarray = make_mntarray(mntlist, count);
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	if (listlength == 0) {
6167c478bd9Sstevel@tonic-gate 		if (count == 0)		/* not an error, just none found */
6177c478bd9Sstevel@tonic-gate 			return (0);
6187c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: no valid entries found in %s\n"),
6197c478bd9Sstevel@tonic-gate 				myname, mnttab);
6207c478bd9Sstevel@tonic-gate 		return (1);
6217c478bd9Sstevel@tonic-gate 	}
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	/*
6247c478bd9Sstevel@tonic-gate 	 * Sort the entries based on their mount level only if lofs's are
6257c478bd9Sstevel@tonic-gate 	 * not present.
6267c478bd9Sstevel@tonic-gate 	 */
6277c478bd9Sstevel@tonic-gate 	if (lofscnt == 0) {
6287c478bd9Sstevel@tonic-gate 		qsort((void *)mntarray, listlength, sizeof (mountent_t *),
6297c478bd9Sstevel@tonic-gate 			mcompar);
6307c478bd9Sstevel@tonic-gate 		/*
6317c478bd9Sstevel@tonic-gate 		 * If we do not detect a lofs by now, we never will.
6327c478bd9Sstevel@tonic-gate 		 */
6337c478bd9Sstevel@tonic-gate 		lofscnt = -1;
6347c478bd9Sstevel@tonic-gate 	}
6357c478bd9Sstevel@tonic-gate 	/*
6367c478bd9Sstevel@tonic-gate 	 * Now link them up so that a given pid is easier to find when
6377c478bd9Sstevel@tonic-gate 	 * we go to clean up after they are done.
6387c478bd9Sstevel@tonic-gate 	 */
6397c478bd9Sstevel@tonic-gate 	mntll = mntarray[0];
6407c478bd9Sstevel@tonic-gate 	for (ml = mntarray; mp = *ml; /* */)
6417c478bd9Sstevel@tonic-gate 		mp->link = *++ml;
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	/*
6447c478bd9Sstevel@tonic-gate 	 * Try to handle interrupts in a reasonable way.
6457c478bd9Sstevel@tonic-gate 	 */
6467c478bd9Sstevel@tonic-gate 	sigset(SIGHUP, cleanup);
6477c478bd9Sstevel@tonic-gate 	sigset(SIGQUIT, cleanup);
6487c478bd9Sstevel@tonic-gate 	sigset(SIGINT, cleanup);
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	do_umounts(mntarray);	/* do the umounts */
6517c478bd9Sstevel@tonic-gate 	return (exitcode);
6527c478bd9Sstevel@tonic-gate }
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate /*
6557c478bd9Sstevel@tonic-gate  * Returns a mountent_t array based on mntlist.  If mntlist is NULL, then
6567c478bd9Sstevel@tonic-gate  * it returns all mnttab entries with a few exceptions.  Sets the global
6577c478bd9Sstevel@tonic-gate  * variable listlength to the number of entries in the array.
6587c478bd9Sstevel@tonic-gate  */
6597c478bd9Sstevel@tonic-gate mountent_t **
6607c478bd9Sstevel@tonic-gate make_mntarray(char **mntlist, int count)
6617c478bd9Sstevel@tonic-gate {
6627c478bd9Sstevel@tonic-gate 	mountent_t 	*mp, **mpp;
6637c478bd9Sstevel@tonic-gate 	int 		ndx;
6647c478bd9Sstevel@tonic-gate 	char		*cp;
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	if (count > 0)
6677c478bd9Sstevel@tonic-gate 		listlength = count;
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	mpp = (mountent_t **)malloc(sizeof (*mp) * (listlength + 1));
6707c478bd9Sstevel@tonic-gate 	if (mpp == NULL)
6717c478bd9Sstevel@tonic-gate 		nomem();
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	if (count == 0) {
6747c478bd9Sstevel@tonic-gate 		if (mntll == NULL) {	/* no entries? */
6757c478bd9Sstevel@tonic-gate 			listlength = 0;
6767c478bd9Sstevel@tonic-gate 			return (NULL);
6777c478bd9Sstevel@tonic-gate 		}
6787c478bd9Sstevel@tonic-gate 		/*
6797c478bd9Sstevel@tonic-gate 		 * No mount list specified: take all mnttab mount points
6807c478bd9Sstevel@tonic-gate 		 * except for a few cases.
6817c478bd9Sstevel@tonic-gate 		 */
6827c478bd9Sstevel@tonic-gate 		for (ndx = 0, mp = mntll; mp; mp = mp->link) {
6837c478bd9Sstevel@tonic-gate 			if (fsstrinlist(mp->ment.mnt_mountp, keeplist))
6847c478bd9Sstevel@tonic-gate 				continue;
6857c478bd9Sstevel@tonic-gate 			mp->mlevel = fsgetmlevel(mp->ment.mnt_mountp);
6867c478bd9Sstevel@tonic-gate 			if (mp->ment.mnt_fstype &&
6877c478bd9Sstevel@tonic-gate 			    (strcmp(mp->ment.mnt_fstype, MNTTYPE_LOFS) == 0))
6887c478bd9Sstevel@tonic-gate 				lofscnt++;
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 			mpp[ndx++] = mp;
6917c478bd9Sstevel@tonic-gate 		}
6927c478bd9Sstevel@tonic-gate 		mpp[ndx] = NULL;
6937c478bd9Sstevel@tonic-gate 		listlength = ndx;
6947c478bd9Sstevel@tonic-gate 		return (mpp);
6957c478bd9Sstevel@tonic-gate 	}
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 	/*
6987c478bd9Sstevel@tonic-gate 	 * A list of mount points was specified on the command line.
6997c478bd9Sstevel@tonic-gate 	 * Build an array out of these.
7007c478bd9Sstevel@tonic-gate 	 */
7017c478bd9Sstevel@tonic-gate 	for (ndx = 0; count--; ) {
7027c478bd9Sstevel@tonic-gate 		cp = *mntlist++;
7037c478bd9Sstevel@tonic-gate 		if (realpath(cp, resolve) == NULL) {
7047c478bd9Sstevel@tonic-gate 			fprintf(stderr,
7057c478bd9Sstevel@tonic-gate 				gettext("%s: warning: can't resolve %s\n"),
7067c478bd9Sstevel@tonic-gate 				myname, cp);
7077c478bd9Sstevel@tonic-gate 			exitcode = 1;
7087c478bd9Sstevel@tonic-gate 			mp = getmntlast(mntll, NULL, cp); /* try anyways */
7097c478bd9Sstevel@tonic-gate 		} else
7107c478bd9Sstevel@tonic-gate 			mp = getmntlast(mntll, NULL, resolve);
7117c478bd9Sstevel@tonic-gate 		if (mp == NULL) {
7127c478bd9Sstevel@tonic-gate 			struct mnttab mnew;
7137c478bd9Sstevel@tonic-gate 			/*
7147c478bd9Sstevel@tonic-gate 			 * Then we've reached the end without finding
7157c478bd9Sstevel@tonic-gate 			 * what we are looking for, but we still have to
7167c478bd9Sstevel@tonic-gate 			 * try to umount it: append it to mntarray.
7177c478bd9Sstevel@tonic-gate 			 */
7187c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(
7197c478bd9Sstevel@tonic-gate 				"%s: warning: %s not found in %s\n"),
7207c478bd9Sstevel@tonic-gate 				myname, resolve, mnttab);
7217c478bd9Sstevel@tonic-gate 			exitcode = 1;
7227c478bd9Sstevel@tonic-gate 			mntnull(&mnew);
7237c478bd9Sstevel@tonic-gate 			mnew.mnt_special = mnew.mnt_mountp = strdup(resolve);
7247c478bd9Sstevel@tonic-gate 			if (mnew.mnt_special == NULL)
7257c478bd9Sstevel@tonic-gate 				nomem();
7267c478bd9Sstevel@tonic-gate 			mp = new_mountent(&mnew);
7277c478bd9Sstevel@tonic-gate 		}
7287c478bd9Sstevel@tonic-gate 		if (mp->ment.mnt_fstype &&
7297c478bd9Sstevel@tonic-gate 		    (strcmp(mp->ment.mnt_fstype, MNTTYPE_LOFS) == 0))
7307c478bd9Sstevel@tonic-gate 			lofscnt++;
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 		mp->mlevel = fsgetmlevel(mp->ment.mnt_mountp);
7337c478bd9Sstevel@tonic-gate 		mpp[ndx++] = mp;
7347c478bd9Sstevel@tonic-gate 	}
7357c478bd9Sstevel@tonic-gate 	mpp[ndx] = NULL;
7367c478bd9Sstevel@tonic-gate 	listlength = ndx;
7377c478bd9Sstevel@tonic-gate 	return (mpp);
7387c478bd9Sstevel@tonic-gate }
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate /*
7417c478bd9Sstevel@tonic-gate  * Returns the tail of a linked list of all mnttab entries.  I.e, it's faster
7427c478bd9Sstevel@tonic-gate  * to return the mnttab in reverse order.
7437c478bd9Sstevel@tonic-gate  * Sets listlength to the number of entries in the list.
7447c478bd9Sstevel@tonic-gate  * Returns NULL if none are found.
7457c478bd9Sstevel@tonic-gate  */
7467c478bd9Sstevel@tonic-gate mountent_t *
74708190127Sdh145677 getmntall(void)
7487c478bd9Sstevel@tonic-gate {
7497c478bd9Sstevel@tonic-gate 	FILE		*fp;
7507c478bd9Sstevel@tonic-gate 	mountent_t	*mtail;
7517c478bd9Sstevel@tonic-gate 	int		cnt = 0, ret;
7527c478bd9Sstevel@tonic-gate 	struct mnttab	mget;
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	if ((fp = fopen(mnttab, "r")) == NULL) {
7557c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: warning cannot open %s\n"),
7567c478bd9Sstevel@tonic-gate 				myname, mnttab);
7577c478bd9Sstevel@tonic-gate 		return (0);
7587c478bd9Sstevel@tonic-gate 	}
7597c478bd9Sstevel@tonic-gate 	mtail = NULL;
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	while ((ret = getmntent(fp, &mget)) != -1) {
7627c478bd9Sstevel@tonic-gate 		mountent_t	*mp;
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 		if (ret > 0) {
7657c478bd9Sstevel@tonic-gate 			mnterror(ret);
7667c478bd9Sstevel@tonic-gate 			continue;
7677c478bd9Sstevel@tonic-gate 		}
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 		mp = new_mountent(&mget);
7707c478bd9Sstevel@tonic-gate 		mp->link = mtail;
7717c478bd9Sstevel@tonic-gate 		mtail = mp;
7727c478bd9Sstevel@tonic-gate 		cnt++;
7737c478bd9Sstevel@tonic-gate 	}
7747c478bd9Sstevel@tonic-gate 	fclose(fp);
7757c478bd9Sstevel@tonic-gate 	if (mtail == NULL) {
7767c478bd9Sstevel@tonic-gate 		listlength = 0;
7777c478bd9Sstevel@tonic-gate 		return (NULL);
7787c478bd9Sstevel@tonic-gate 	}
7797c478bd9Sstevel@tonic-gate 	listlength = cnt;
7807c478bd9Sstevel@tonic-gate 	return (mtail);
7817c478bd9Sstevel@tonic-gate }
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate void
7847c478bd9Sstevel@tonic-gate do_umounts(mountent_t **mntarray)
7857c478bd9Sstevel@tonic-gate {
7867c478bd9Sstevel@tonic-gate 	mountent_t *mp, *mpprev, **ml = mntarray;
7877c478bd9Sstevel@tonic-gate 	int	cnt = listlength;
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	/*
7907c478bd9Sstevel@tonic-gate 	 * Main loop for the forked children:
7917c478bd9Sstevel@tonic-gate 	 */
7927c478bd9Sstevel@tonic-gate 	for (mpprev = *ml; mp = *ml; mpprev = mp, ml++, cnt--) {
7937c478bd9Sstevel@tonic-gate 		pid_t	pid;
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 		/*
7967c478bd9Sstevel@tonic-gate 		 * Check to see if we cross a mount level: e.g.,
7977c478bd9Sstevel@tonic-gate 		 * /a/b/c -> /a/b.  If so, we need to wait for all current
7987c478bd9Sstevel@tonic-gate 		 * umounts to finish before umounting the rest.
7997c478bd9Sstevel@tonic-gate 		 *
8007c478bd9Sstevel@tonic-gate 		 * Also, we unmount serially as long as there are lofs's
8017c478bd9Sstevel@tonic-gate 		 * to mount to avoid improper umount ordering.
8027c478bd9Sstevel@tonic-gate 		 */
8037c478bd9Sstevel@tonic-gate 		if (mp->mlevel < mpprev->mlevel || lofscnt > 0)
8047c478bd9Sstevel@tonic-gate 			while (nrun > 0 && (dowait() != -1))
8057c478bd9Sstevel@tonic-gate 				;
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 		if (lofscnt == 0) {
8087c478bd9Sstevel@tonic-gate 			/*
8097c478bd9Sstevel@tonic-gate 			 * We can now go to parallel umounting.
8107c478bd9Sstevel@tonic-gate 			 */
8117c478bd9Sstevel@tonic-gate 			qsort((void *)ml, cnt, sizeof (mountent_t *), mcompar);
8127c478bd9Sstevel@tonic-gate 			mp = *ml;	/* possible first entry */
8137c478bd9Sstevel@tonic-gate 			lofscnt--;	/* so we don't do this again */
8147c478bd9Sstevel@tonic-gate 		}
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 		while (setup_iopipe(mp) == -1 && (dowait() != -1))
8177c478bd9Sstevel@tonic-gate 			;
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 		while (nrun >= maxrun && (dowait() != -1))	/* throttle */
8207c478bd9Sstevel@tonic-gate 			;
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate #ifdef CACHEFS_BUG
8237c478bd9Sstevel@tonic-gate 		/*
8247c478bd9Sstevel@tonic-gate 		 * If this is the back file system, then let cachefs/umount
8257c478bd9Sstevel@tonic-gate 		 * unmount it.
8267c478bd9Sstevel@tonic-gate 		 */
8277c478bd9Sstevel@tonic-gate 		if (strstr(mp->ment.mnt_mountp, BACKMNT_NAME))
8287c478bd9Sstevel@tonic-gate 			continue;
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 		if (mp->ment.mnt_fstype &&
8327c478bd9Sstevel@tonic-gate 		    (strcmp(mp->ment.mnt_fstype, "cachefs") == 0)) {
8337c478bd9Sstevel@tonic-gate 			while (cachefs_running && (dowait() != -1))
8347c478bd9Sstevel@tonic-gate 					;
8357c478bd9Sstevel@tonic-gate 			cachefs_running = 1;
8367c478bd9Sstevel@tonic-gate 		}
8377c478bd9Sstevel@tonic-gate #endif
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 		if ((pid = fork()) == -1) {
8407c478bd9Sstevel@tonic-gate 			perror("fork");
8417c478bd9Sstevel@tonic-gate 			cleanup(-1);
8427c478bd9Sstevel@tonic-gate 			/* not reached */
8437c478bd9Sstevel@tonic-gate 		}
8447c478bd9Sstevel@tonic-gate #ifdef DEBUG
8457c478bd9Sstevel@tonic-gate 		if (dflg && pid > 0) {
8467c478bd9Sstevel@tonic-gate 			fprintf(stderr, "parent %d: umounting %d %s\n",
8477c478bd9Sstevel@tonic-gate 				getpid(), pid, mp->ment.mnt_mountp);
8487c478bd9Sstevel@tonic-gate 		}
8497c478bd9Sstevel@tonic-gate #endif
8507c478bd9Sstevel@tonic-gate 		if (pid == 0) {		/* child */
8517c478bd9Sstevel@tonic-gate 			signal(SIGHUP, SIG_IGN);
8527c478bd9Sstevel@tonic-gate 			signal(SIGQUIT, SIG_IGN);
8537c478bd9Sstevel@tonic-gate 			signal(SIGINT, SIG_IGN);
8547c478bd9Sstevel@tonic-gate 			setup_output(mp);
8557c478bd9Sstevel@tonic-gate 			doexec(&mp->ment);
8567c478bd9Sstevel@tonic-gate 			perror("exec");
8577c478bd9Sstevel@tonic-gate 			exit(1);
8587c478bd9Sstevel@tonic-gate 		}
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 		/* parent */
8617c478bd9Sstevel@tonic-gate 		(void) close(mp->sopipe[WRPIPE]);
8627c478bd9Sstevel@tonic-gate 		(void) close(mp->sepipe[WRPIPE]);
8637c478bd9Sstevel@tonic-gate 		mp->pid = pid;
8647c478bd9Sstevel@tonic-gate 		nrun++;
8657c478bd9Sstevel@tonic-gate 	}
8667c478bd9Sstevel@tonic-gate 	cleanup(0);
8677c478bd9Sstevel@tonic-gate }
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate /*
8707c478bd9Sstevel@tonic-gate  * cleanup the existing children and exit with an error
8717c478bd9Sstevel@tonic-gate  * if asig != 0.
8727c478bd9Sstevel@tonic-gate  */
8737c478bd9Sstevel@tonic-gate void
8747c478bd9Sstevel@tonic-gate cleanup(int asig)
8757c478bd9Sstevel@tonic-gate {
8767c478bd9Sstevel@tonic-gate 	/*
8777c478bd9Sstevel@tonic-gate 	 * Let the stragglers finish.
8787c478bd9Sstevel@tonic-gate 	 */
8797c478bd9Sstevel@tonic-gate 	while (nrun > 0 && (dowait() != -1))
8807c478bd9Sstevel@tonic-gate 		;
8817c478bd9Sstevel@tonic-gate 	if (asig != 0)
8827c478bd9Sstevel@tonic-gate 		exit(1);
8837c478bd9Sstevel@tonic-gate }
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate /*
8877c478bd9Sstevel@tonic-gate  * Waits for 1 child to die.
8887c478bd9Sstevel@tonic-gate  *
8897c478bd9Sstevel@tonic-gate  * Returns -1 if no children are left to wait for.
8907c478bd9Sstevel@tonic-gate  * Returns 0 if a child died without an error.
8917c478bd9Sstevel@tonic-gate  * Returns 1 if a child died with an error.
8927c478bd9Sstevel@tonic-gate  * Sets the global exitcode if an error occurred.
8937c478bd9Sstevel@tonic-gate  */
8947c478bd9Sstevel@tonic-gate int
89508190127Sdh145677 dowait(void)
8967c478bd9Sstevel@tonic-gate {
8977c478bd9Sstevel@tonic-gate 	int		wstat, child, ret;
8987c478bd9Sstevel@tonic-gate 	mountent_t 	*mp, *prevp;
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 	if ((child = wait(&wstat)) == -1)
9017c478bd9Sstevel@tonic-gate 		return (-1);
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	if (WIFEXITED(wstat))		/* this should always be true */
9047c478bd9Sstevel@tonic-gate 		ret = WEXITSTATUS(wstat);
9057c478bd9Sstevel@tonic-gate 	else
9067c478bd9Sstevel@tonic-gate 		ret = 1;		/* assume some kind of error */
9077c478bd9Sstevel@tonic-gate 	nrun--;
9087c478bd9Sstevel@tonic-gate 	if (ret)
9097c478bd9Sstevel@tonic-gate 		exitcode = 1;
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 	/*
9127c478bd9Sstevel@tonic-gate 	 * Find our child so we can process its std output, if any.
9137c478bd9Sstevel@tonic-gate 	 * This search gets smaller and smaller as children are cleaned
9147c478bd9Sstevel@tonic-gate 	 * up.
9157c478bd9Sstevel@tonic-gate 	 */
9167c478bd9Sstevel@tonic-gate 	for (prevp = NULL, mp = mntll; mp; mp = mp->link) {
9177c478bd9Sstevel@tonic-gate 		if (mp->pid != child) {
9187c478bd9Sstevel@tonic-gate 			prevp = mp;
9197c478bd9Sstevel@tonic-gate 			continue;
9207c478bd9Sstevel@tonic-gate 		}
9217c478bd9Sstevel@tonic-gate 		/*
9227c478bd9Sstevel@tonic-gate 		 * Found: let's remove it from this list.
9237c478bd9Sstevel@tonic-gate 		 */
9247c478bd9Sstevel@tonic-gate 		if (prevp) {
9257c478bd9Sstevel@tonic-gate 			prevp->link = mp->link;
9267c478bd9Sstevel@tonic-gate 			mp->link = NULL;
9277c478bd9Sstevel@tonic-gate 		}
9287c478bd9Sstevel@tonic-gate 		break;
9297c478bd9Sstevel@tonic-gate 	}
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 	if (mp == NULL) {
9327c478bd9Sstevel@tonic-gate 		/*
9337c478bd9Sstevel@tonic-gate 		 * This should never happen.
9347c478bd9Sstevel@tonic-gate 		 */
9357c478bd9Sstevel@tonic-gate #ifdef DEBUG
9367c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(
9377c478bd9Sstevel@tonic-gate 			"%s: unknown child %d\n"), myname, child);
9387c478bd9Sstevel@tonic-gate #endif
9397c478bd9Sstevel@tonic-gate 		exitcode = 1;
9407c478bd9Sstevel@tonic-gate 		return (1);
9417c478bd9Sstevel@tonic-gate 	}
9427c478bd9Sstevel@tonic-gate 	doio(mp);	/* Any output? */
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 	if (mp->ment.mnt_fstype &&
9457c478bd9Sstevel@tonic-gate 	    (strcmp(mp->ment.mnt_fstype, MNTTYPE_LOFS) == 0))
9467c478bd9Sstevel@tonic-gate 		lofscnt--;
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate #ifdef CACHEFS_BUG
9497c478bd9Sstevel@tonic-gate 	if (mp->ment.mnt_fstype &&
9507c478bd9Sstevel@tonic-gate 	    (strcmp(mp->ment.mnt_fstype, "cachefs") == 0))
9517c478bd9Sstevel@tonic-gate 		cachefs_running = 0;
9527c478bd9Sstevel@tonic-gate #endif
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 	return (ret);
9557c478bd9Sstevel@tonic-gate }
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate static const mountent_t zmount = { 0 };
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate mountent_t *
9607c478bd9Sstevel@tonic-gate new_mountent(struct mnttab *ment)
9617c478bd9Sstevel@tonic-gate {
9627c478bd9Sstevel@tonic-gate 	mountent_t *new;
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 	new = (mountent_t *)malloc(sizeof (*new));
9657c478bd9Sstevel@tonic-gate 	if (new == NULL)
9667c478bd9Sstevel@tonic-gate 		nomem();
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	*new = zmount;
9697c478bd9Sstevel@tonic-gate 	if (ment->mnt_special &&
9707c478bd9Sstevel@tonic-gate 	    (new->ment.mnt_special = strdup(ment->mnt_special)) == NULL)
9717c478bd9Sstevel@tonic-gate 		nomem();
9727c478bd9Sstevel@tonic-gate 	if (ment->mnt_mountp &&
9737c478bd9Sstevel@tonic-gate 	    (new->ment.mnt_mountp = strdup(ment->mnt_mountp)) == NULL)
9747c478bd9Sstevel@tonic-gate 		nomem();
9757c478bd9Sstevel@tonic-gate 	if (ment->mnt_fstype &&
9767c478bd9Sstevel@tonic-gate 	    (new->ment.mnt_fstype = strdup(ment->mnt_fstype)) == NULL)
9777c478bd9Sstevel@tonic-gate 		nomem();
9787c478bd9Sstevel@tonic-gate 	return (new);
9797c478bd9Sstevel@tonic-gate }
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate /*
9837c478bd9Sstevel@tonic-gate  * Sort in descending order of "mount level".  For example, /a/b/c is
9847c478bd9Sstevel@tonic-gate  * placed before /a/b .
9857c478bd9Sstevel@tonic-gate  */
9867c478bd9Sstevel@tonic-gate int
9877c478bd9Sstevel@tonic-gate mcompar(const void *a, const void *b)
9887c478bd9Sstevel@tonic-gate {
9897c478bd9Sstevel@tonic-gate 	mountent_t *a1, *b1;
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 	a1 = *(mountent_t **)a;
9927c478bd9Sstevel@tonic-gate 	b1 = *(mountent_t **)b;
9937c478bd9Sstevel@tonic-gate 	return (b1->mlevel - a1->mlevel);
9947c478bd9Sstevel@tonic-gate }
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate /*
9977c478bd9Sstevel@tonic-gate  * The purpose of this routine is to form stdout and stderr
9987c478bd9Sstevel@tonic-gate  * pipes for the children's output.  The parent then reads and writes it
9997c478bd9Sstevel@tonic-gate  * out it serially in order to ensure that the output is
10007c478bd9Sstevel@tonic-gate  * not garbled.
10017c478bd9Sstevel@tonic-gate  */
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate int
10047c478bd9Sstevel@tonic-gate setup_iopipe(mountent_t *mp)
10057c478bd9Sstevel@tonic-gate {
10067c478bd9Sstevel@tonic-gate 	/*
10077c478bd9Sstevel@tonic-gate 	 * Make a stdout and stderr pipe.  This should never fail.
10087c478bd9Sstevel@tonic-gate 	 */
10097c478bd9Sstevel@tonic-gate 	if (pipe(mp->sopipe) == -1)
10107c478bd9Sstevel@tonic-gate 		return (-1);
10117c478bd9Sstevel@tonic-gate 	if (pipe(mp->sepipe) == -1) {
10127c478bd9Sstevel@tonic-gate 		(void) close(mp->sopipe[RDPIPE]);
10137c478bd9Sstevel@tonic-gate 		(void) close(mp->sopipe[WRPIPE]);
10147c478bd9Sstevel@tonic-gate 		return (-1);
10157c478bd9Sstevel@tonic-gate 	}
10167c478bd9Sstevel@tonic-gate 	/*
10177c478bd9Sstevel@tonic-gate 	 * Don't block on an empty pipe.
10187c478bd9Sstevel@tonic-gate 	 */
10197c478bd9Sstevel@tonic-gate 	(void) fcntl(mp->sopipe[RDPIPE], F_SETFL, O_NDELAY|O_NONBLOCK);
10207c478bd9Sstevel@tonic-gate 	(void) fcntl(mp->sepipe[RDPIPE], F_SETFL, O_NDELAY|O_NONBLOCK);
10217c478bd9Sstevel@tonic-gate 	return (0);
10227c478bd9Sstevel@tonic-gate }
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate /*
10257c478bd9Sstevel@tonic-gate  * Called by a child to attach its stdout and stderr to the write side of
10267c478bd9Sstevel@tonic-gate  * the pipes.
10277c478bd9Sstevel@tonic-gate  */
10287c478bd9Sstevel@tonic-gate void
10297c478bd9Sstevel@tonic-gate setup_output(mountent_t *mp)
10307c478bd9Sstevel@tonic-gate {
10317c478bd9Sstevel@tonic-gate 	(void) close(fileno(stdout));
10327c478bd9Sstevel@tonic-gate 	(void) dup(mp->sopipe[WRPIPE]);
10337c478bd9Sstevel@tonic-gate 	(void) close(mp->sopipe[WRPIPE]);
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 	(void) close(fileno(stderr));
10367c478bd9Sstevel@tonic-gate 	(void) dup(mp->sepipe[WRPIPE]);
10377c478bd9Sstevel@tonic-gate 	(void) close(mp->sepipe[WRPIPE]);
10387c478bd9Sstevel@tonic-gate }
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate /*
10417c478bd9Sstevel@tonic-gate  * Parent uses this to print any stdout or stderr output issued by
10427c478bd9Sstevel@tonic-gate  * the child.
10437c478bd9Sstevel@tonic-gate  */
10447c478bd9Sstevel@tonic-gate static void
10457c478bd9Sstevel@tonic-gate doio(mountent_t *mp)
10467c478bd9Sstevel@tonic-gate {
10477c478bd9Sstevel@tonic-gate 	int bytes;
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 	while ((bytes = read(mp->sepipe[RDPIPE], ibuf, sizeof (ibuf))) > 0)
10507c478bd9Sstevel@tonic-gate 		write(fileno(stderr), ibuf, bytes);
10517c478bd9Sstevel@tonic-gate 	while ((bytes = read(mp->sopipe[RDPIPE], ibuf, sizeof (ibuf))) > 0)
10527c478bd9Sstevel@tonic-gate 		write(fileno(stdout), ibuf, bytes);
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	(void) close(mp->sopipe[RDPIPE]);
10557c478bd9Sstevel@tonic-gate 	(void) close(mp->sepipe[RDPIPE]);
10567c478bd9Sstevel@tonic-gate }
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate void
105908190127Sdh145677 nomem(void)
10607c478bd9Sstevel@tonic-gate {
10617c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext("%s: out of memory\n"), myname);
10627c478bd9Sstevel@tonic-gate 	/*
10637c478bd9Sstevel@tonic-gate 	 * Let the stragglers finish.
10647c478bd9Sstevel@tonic-gate 	 */
10657c478bd9Sstevel@tonic-gate 	while (nrun > 0 && (dowait() != -1))
10667c478bd9Sstevel@tonic-gate 		;
10677c478bd9Sstevel@tonic-gate 	exit(1);
10687c478bd9Sstevel@tonic-gate }
1069