xref: /illumos-gate/usr/src/cmd/fs.d/umount.c (revision 4bff34e37def8a90f9194d81bc345c52ba20086a)
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*4bff34e3Sthurlow  * Copyright 2008 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"
55a237e38eSth199096 #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",
136a237e38eSth199096 	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;
358*4bff34e3Sthurlow 		int	smbfs;
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 		if (strlen(ment->mnt_fstype) > (size_t)FSTYPE_MAX) {
3617c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(
3627c478bd9Sstevel@tonic-gate 			    "%s: FSType %s exceeds %d characters\n"),
3637c478bd9Sstevel@tonic-gate 			    myname, ment->mnt_fstype, FSTYPE_MAX);
3647c478bd9Sstevel@tonic-gate 			exit(1);
3657c478bd9Sstevel@tonic-gate 		}
3667c478bd9Sstevel@tonic-gate 
367*4bff34e3Sthurlow 		/*
368*4bff34e3Sthurlow 		 * Special case smbfs file system.
369*4bff34e3Sthurlow 		 * Execute command in profile if possible.
370*4bff34e3Sthurlow 		 */
371*4bff34e3Sthurlow 		smbfs = strcmp(ment->mnt_fstype, "smbfs") == 0;
372*4bff34e3Sthurlow 
3737c478bd9Sstevel@tonic-gate 		/* build the full pathname of the fstype dependent command. */
3747c478bd9Sstevel@tonic-gate 		sprintf(full_path, "%s/%s/%s", fs_path, ment->mnt_fstype,
3757c478bd9Sstevel@tonic-gate 		    myname);
3767c478bd9Sstevel@tonic-gate 		sprintf(alter_path, "%s/%s/%s", alt_path, ment->mnt_fstype,
3777c478bd9Sstevel@tonic-gate 		    myname);
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 		/*
3807c478bd9Sstevel@tonic-gate 		 * create the new arg list, and end the list with a
3817c478bd9Sstevel@tonic-gate 		 * null pointer
3827c478bd9Sstevel@tonic-gate 		 */
3837c478bd9Sstevel@tonic-gate 		ii = 2;
3847c478bd9Sstevel@tonic-gate 		if (oflg) {
3857c478bd9Sstevel@tonic-gate 			newargv[ii++] = "-o";
3867c478bd9Sstevel@tonic-gate 			newargv[ii++] = oarg;
3877c478bd9Sstevel@tonic-gate 		}
3887c478bd9Sstevel@tonic-gate 		if (dashflg) {
3897c478bd9Sstevel@tonic-gate 			newargv[ii++] = "--";
3907c478bd9Sstevel@tonic-gate 		}
3917c478bd9Sstevel@tonic-gate 		if (fflg) {
3927c478bd9Sstevel@tonic-gate 			newargv[ii++] = "-f";
3937c478bd9Sstevel@tonic-gate 		}
3947c478bd9Sstevel@tonic-gate 		newargv[ii++] = (ment->mnt_mountp)
3957c478bd9Sstevel@tonic-gate 		    ? ment->mnt_mountp : ment->mnt_special;
3967c478bd9Sstevel@tonic-gate 		newargv[ii] = NULL;
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 		/* set the new argv[0] to the filename */
3997c478bd9Sstevel@tonic-gate 		newargv[1] = myname;
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 		if (Vflg) {
4027c478bd9Sstevel@tonic-gate 			printf("%s", myname);
4037c478bd9Sstevel@tonic-gate 			for (ii = 2; newargv[ii]; ii++)
4047c478bd9Sstevel@tonic-gate 				printf(" %s", newargv[ii]);
4057c478bd9Sstevel@tonic-gate 			printf("\n");
4067c478bd9Sstevel@tonic-gate 			fflush(stdout);
4077c478bd9Sstevel@tonic-gate 			exit(0);
4087c478bd9Sstevel@tonic-gate 		}
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 		/* Try to exec the fstype dependent umount. */
411*4bff34e3Sthurlow 		if (smbfs) {
412*4bff34e3Sthurlow 			/*
413*4bff34e3Sthurlow 			 * Run umount_smbfs(1m) with pfexec so that we can
414*4bff34e3Sthurlow 			 * add sys_mount privilege, (via exec_attr, etc.)
415*4bff34e3Sthurlow 			 * allowing normal users to unmount any directory
416*4bff34e3Sthurlow 			 * they own.
417*4bff34e3Sthurlow 			 */
418*4bff34e3Sthurlow 			newargv[0] = "pfexec";
419*4bff34e3Sthurlow 			newargv[1] = full_path;
420*4bff34e3Sthurlow 			execv("/usr/bin/pfexec", &newargv[0]);
421*4bff34e3Sthurlow 			newargv[1] = myname;
422*4bff34e3Sthurlow 		}
4237c478bd9Sstevel@tonic-gate 		execv(full_path, &newargv[1]);
4247c478bd9Sstevel@tonic-gate 		if (errno == ENOEXEC) {
4257c478bd9Sstevel@tonic-gate 			newargv[0] = "sh";
4267c478bd9Sstevel@tonic-gate 			newargv[1] = full_path;
4277c478bd9Sstevel@tonic-gate 			execv("/sbin/sh", &newargv[0]);
4287c478bd9Sstevel@tonic-gate 		}
429*4bff34e3Sthurlow 		if (smbfs) {
430*4bff34e3Sthurlow 			newargv[0] = "pfexec";
431*4bff34e3Sthurlow 			newargv[1] = alter_path;
432*4bff34e3Sthurlow 			execv("/usr/bin/pfexec", &newargv[0]);
433*4bff34e3Sthurlow 		}
4347c478bd9Sstevel@tonic-gate 		newargv[1] = myname;
4357c478bd9Sstevel@tonic-gate 		execv(alter_path, &newargv[1]);
4367c478bd9Sstevel@tonic-gate 		if (errno == ENOEXEC) {
4377c478bd9Sstevel@tonic-gate 			newargv[0] = "sh";
4387c478bd9Sstevel@tonic-gate 			newargv[1] = alter_path;
4397c478bd9Sstevel@tonic-gate 			execv("/sbin/sh", &newargv[0]);
4407c478bd9Sstevel@tonic-gate 		}
4417c478bd9Sstevel@tonic-gate 		/* exec failed */
4427c478bd9Sstevel@tonic-gate 		if (errno != ENOENT) {
4437c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("umount: cannot execute %s\n"),
4447c478bd9Sstevel@tonic-gate 			    full_path);
4457c478bd9Sstevel@tonic-gate 			exit(1);
4467c478bd9Sstevel@tonic-gate 		}
4477c478bd9Sstevel@tonic-gate 	}
4487c478bd9Sstevel@tonic-gate 	/*
4497c478bd9Sstevel@tonic-gate 	 * No fstype independent executable then.  We'll go generic
4507c478bd9Sstevel@tonic-gate 	 * from here.
4517c478bd9Sstevel@tonic-gate 	 */
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	/* don't use -o with generic */
4547c478bd9Sstevel@tonic-gate 	if (oflg) {
4557c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(
456*4bff34e3Sthurlow 		    "%s: %s specific umount does not exist;"
457*4bff34e3Sthurlow 		    " -o suboption ignored\n"),
4587c478bd9Sstevel@tonic-gate 		    myname, ment->mnt_fstype ? ment->mnt_fstype : "<null>");
4597c478bd9Sstevel@tonic-gate 	}
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	signal(SIGHUP,  SIG_IGN);
4627c478bd9Sstevel@tonic-gate 	signal(SIGQUIT, SIG_IGN);
4637c478bd9Sstevel@tonic-gate 	signal(SIGINT,  SIG_IGN);
4647c478bd9Sstevel@tonic-gate 	/*
4657c478bd9Sstevel@tonic-gate 	 * Try to umount the mountpoint.
4667c478bd9Sstevel@tonic-gate 	 * If that fails, try the corresponding special.
4677c478bd9Sstevel@tonic-gate 	 * (This ordering is necessary for nfs umounts.)
4687c478bd9Sstevel@tonic-gate 	 * (for remote resources:  if the first umount returns EBUSY
4697c478bd9Sstevel@tonic-gate 	 * don't call umount again - umount() with a resource name
4707c478bd9Sstevel@tonic-gate 	 * will return a misleading error to the user
4717c478bd9Sstevel@tonic-gate 	 */
4727c478bd9Sstevel@tonic-gate 	if (fflg) {
4737c478bd9Sstevel@tonic-gate 		if (((ret = umount2(ment->mnt_mountp, MS_FORCE)) < 0) &&
4747c478bd9Sstevel@tonic-gate 		    (errno != EBUSY && errno != ENOTSUP &&
4757c478bd9Sstevel@tonic-gate 		    errno != EPERM))
4767c478bd9Sstevel@tonic-gate 			ret = umount2(ment->mnt_special, MS_FORCE);
4777c478bd9Sstevel@tonic-gate 	} else {
4787c478bd9Sstevel@tonic-gate 		if (((ret = umount2(ment->mnt_mountp, 0)) < 0) &&
4797c478bd9Sstevel@tonic-gate 		    (errno != EBUSY) && (errno != EPERM))
4807c478bd9Sstevel@tonic-gate 			ret = umount2(ment->mnt_special, 0);
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	if (ret < 0) {
4847c478bd9Sstevel@tonic-gate 		rpterr(ment->mnt_mountp);
4857c478bd9Sstevel@tonic-gate 		if (errno != EINVAL && errno != EFAULT)
4867c478bd9Sstevel@tonic-gate 			exit(1);
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 		exitcode = 1;
4897c478bd9Sstevel@tonic-gate 	}
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	exit(exitcode);
4927c478bd9Sstevel@tonic-gate }
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate void
49508190127Sdh145677 rpterr(char *sp)
4967c478bd9Sstevel@tonic-gate {
4977c478bd9Sstevel@tonic-gate 	switch (errno) {
4987c478bd9Sstevel@tonic-gate 	case EPERM:
4997c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: permission denied\n"), myname);
5007c478bd9Sstevel@tonic-gate 		break;
5017c478bd9Sstevel@tonic-gate 	case ENXIO:
5027c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: %s no device\n"), myname, sp);
5037c478bd9Sstevel@tonic-gate 		break;
5047c478bd9Sstevel@tonic-gate 	case ENOENT:
5057c478bd9Sstevel@tonic-gate 		fprintf(stderr,
5067c478bd9Sstevel@tonic-gate 		    gettext("%s: %s no such file or directory\n"),
5077c478bd9Sstevel@tonic-gate 		    myname, sp);
5087c478bd9Sstevel@tonic-gate 		break;
5097c478bd9Sstevel@tonic-gate 	case EINVAL:
5107c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: %s not mounted\n"), myname, sp);
5117c478bd9Sstevel@tonic-gate 		break;
5127c478bd9Sstevel@tonic-gate 	case EBUSY:
5137c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: %s busy\n"), myname, sp);
5147c478bd9Sstevel@tonic-gate 		break;
5157c478bd9Sstevel@tonic-gate 	case ENOTBLK:
5167c478bd9Sstevel@tonic-gate 		fprintf(stderr,
5177c478bd9Sstevel@tonic-gate 		    gettext("%s: %s block device required\n"), myname, sp);
5187c478bd9Sstevel@tonic-gate 		break;
5197c478bd9Sstevel@tonic-gate 	case ECOMM:
5207c478bd9Sstevel@tonic-gate 		fprintf(stderr,
5217c478bd9Sstevel@tonic-gate 		    gettext("%s: warning: broken link detected\n"), myname);
5227c478bd9Sstevel@tonic-gate 		break;
5237c478bd9Sstevel@tonic-gate 	default:
5247c478bd9Sstevel@tonic-gate 		perror(myname);
5257c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: cannot unmount %s\n"), myname, sp);
5267c478bd9Sstevel@tonic-gate 	}
5277c478bd9Sstevel@tonic-gate }
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate void
53008190127Sdh145677 usage(void)
5317c478bd9Sstevel@tonic-gate {
5327c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext(
5337c478bd9Sstevel@tonic-gate "Usage:\n%s [-f] [-V] [-o specific_options] {special | mount-point}\n"),
5347c478bd9Sstevel@tonic-gate 	    myname);
5357c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext(
5367c478bd9Sstevel@tonic-gate "%s -a [-f] [-V] [-o specific_options] [mount_point ...]\n"), myname);
5377c478bd9Sstevel@tonic-gate 	exit(1);
5387c478bd9Sstevel@tonic-gate }
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate void
54108190127Sdh145677 mnterror(int flag)
5427c478bd9Sstevel@tonic-gate {
5437c478bd9Sstevel@tonic-gate 	switch (flag) {
5447c478bd9Sstevel@tonic-gate 	case MNT_TOOLONG:
5457c478bd9Sstevel@tonic-gate 		fprintf(stderr,
5467c478bd9Sstevel@tonic-gate 		    gettext("%s: line in mnttab exceeds %d characters\n"),
5477c478bd9Sstevel@tonic-gate 		    myname, MNT_LINE_MAX-2);
5487c478bd9Sstevel@tonic-gate 		break;
5497c478bd9Sstevel@tonic-gate 	case MNT_TOOFEW:
5507c478bd9Sstevel@tonic-gate 		fprintf(stderr,
5517c478bd9Sstevel@tonic-gate 		    gettext("%s: line in mnttab has too few entries\n"),
5527c478bd9Sstevel@tonic-gate 		    myname);
5537c478bd9Sstevel@tonic-gate 		break;
5547c478bd9Sstevel@tonic-gate 	default:
5557c478bd9Sstevel@tonic-gate 		break;
5567c478bd9Sstevel@tonic-gate 	}
5577c478bd9Sstevel@tonic-gate }
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate /*
5607c478bd9Sstevel@tonic-gate  * Search the mlist linked list for the
5617c478bd9Sstevel@tonic-gate  * first match of specp or mntp.  The list is expected to be in reverse
5627c478bd9Sstevel@tonic-gate  * order of /etc/mnttab.
5637c478bd9Sstevel@tonic-gate  * If both are specified, then both have to match.
5647c478bd9Sstevel@tonic-gate  * Returns the (mountent_t *) of the match, otherwise returns NULL.
5657c478bd9Sstevel@tonic-gate  */
5667c478bd9Sstevel@tonic-gate mountent_t *
5677c478bd9Sstevel@tonic-gate getmntlast(mountent_t *mlist, char *specp, char *mntp)
5687c478bd9Sstevel@tonic-gate {
5697c478bd9Sstevel@tonic-gate 	int		mfound, sfound;
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	for (/* */; mlist; mlist = mlist->link) {
5727c478bd9Sstevel@tonic-gate 		mfound = sfound = 0;
5737c478bd9Sstevel@tonic-gate 		if (mntp && (strcmp(mlist->ment.mnt_mountp, mntp) == 0)) {
5747c478bd9Sstevel@tonic-gate 			if (specp == NULL)
5757c478bd9Sstevel@tonic-gate 				return (mlist);
5767c478bd9Sstevel@tonic-gate 			mfound++;
5777c478bd9Sstevel@tonic-gate 		}
5787c478bd9Sstevel@tonic-gate 		if (specp && (strcmp(mlist->ment.mnt_special, specp) == 0)) {
5797c478bd9Sstevel@tonic-gate 			if (mntp == NULL)
5807c478bd9Sstevel@tonic-gate 				return (mlist);
5817c478bd9Sstevel@tonic-gate 			sfound++;
5827c478bd9Sstevel@tonic-gate 		}
5837c478bd9Sstevel@tonic-gate 		if (mfound && sfound)
5847c478bd9Sstevel@tonic-gate 			return (mlist);
5857c478bd9Sstevel@tonic-gate 	}
5867c478bd9Sstevel@tonic-gate 	return (NULL);
5877c478bd9Sstevel@tonic-gate }
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate /*
5927c478bd9Sstevel@tonic-gate  * Perform the parallel version of umount.  Returns 0 if no errors occurred,
5937c478bd9Sstevel@tonic-gate  * non zero otherwise.
5947c478bd9Sstevel@tonic-gate  */
5957c478bd9Sstevel@tonic-gate int
5967c478bd9Sstevel@tonic-gate parumount(char **mntlist, int count)
5977c478bd9Sstevel@tonic-gate {
5987c478bd9Sstevel@tonic-gate 	int 		maxfd = OPEN_MAX;
5997c478bd9Sstevel@tonic-gate 	struct rlimit 	rl;
6007c478bd9Sstevel@tonic-gate 	mountent_t	**mntarray, **ml, *mp;
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	/*
6037c478bd9Sstevel@tonic-gate 	 * If no mount points are specified and none were found in mnttab,
6047c478bd9Sstevel@tonic-gate 	 * then end it all here.
6057c478bd9Sstevel@tonic-gate 	 */
6067c478bd9Sstevel@tonic-gate 	if (count == 0 && mntll == NULL)
6077c478bd9Sstevel@tonic-gate 		return (0);
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	/*
6107c478bd9Sstevel@tonic-gate 	 * This is the process scaling section.  After running a series
6117c478bd9Sstevel@tonic-gate 	 * of tests based on the number of simultaneous processes and
6127c478bd9Sstevel@tonic-gate 	 * processors available, optimum performance was achieved near or
6137c478bd9Sstevel@tonic-gate 	 * at (PROCN * 2).
6147c478bd9Sstevel@tonic-gate 	 */
6157c478bd9Sstevel@tonic-gate 	if ((maxrun = sysconf(_SC_NPROCESSORS_ONLN)) == -1)
6167c478bd9Sstevel@tonic-gate 		maxrun = 4;
6177c478bd9Sstevel@tonic-gate 	else
6187c478bd9Sstevel@tonic-gate 		maxrun = maxrun * 2 + 1;
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
6217c478bd9Sstevel@tonic-gate 		rl.rlim_cur = rl.rlim_max;
6227c478bd9Sstevel@tonic-gate 		if (setrlimit(RLIMIT_NOFILE, &rl) == 0)
6237c478bd9Sstevel@tonic-gate 			maxfd = (int)rl.rlim_cur;
624004388ebScasper 		(void) enable_extended_FILE_stdio(-1, -1);
6257c478bd9Sstevel@tonic-gate 	}
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	/*
6287c478bd9Sstevel@tonic-gate 	 * The parent needs to maintain 3 of its own fd's, plus 2 for
6297c478bd9Sstevel@tonic-gate 	 * each child (the stdout and stderr pipes).
6307c478bd9Sstevel@tonic-gate 	 */
6317c478bd9Sstevel@tonic-gate 	maxfd = (maxfd / 2) - 6;	/* 6 takes care of temporary  */
6327c478bd9Sstevel@tonic-gate 					/* periods of open fds */
6337c478bd9Sstevel@tonic-gate 	if (maxfd < maxrun)
6347c478bd9Sstevel@tonic-gate 		maxrun = maxfd;
6357c478bd9Sstevel@tonic-gate 	if (maxrun < 4)
6367c478bd9Sstevel@tonic-gate 		maxrun = 4;		/* sanity check */
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	mntarray = make_mntarray(mntlist, count);
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	if (listlength == 0) {
6417c478bd9Sstevel@tonic-gate 		if (count == 0)		/* not an error, just none found */
6427c478bd9Sstevel@tonic-gate 			return (0);
6437c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: no valid entries found in %s\n"),
6447c478bd9Sstevel@tonic-gate 		    myname, mnttab);
6457c478bd9Sstevel@tonic-gate 		return (1);
6467c478bd9Sstevel@tonic-gate 	}
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	/*
6497c478bd9Sstevel@tonic-gate 	 * Sort the entries based on their mount level only if lofs's are
6507c478bd9Sstevel@tonic-gate 	 * not present.
6517c478bd9Sstevel@tonic-gate 	 */
6527c478bd9Sstevel@tonic-gate 	if (lofscnt == 0) {
6537c478bd9Sstevel@tonic-gate 		qsort((void *)mntarray, listlength, sizeof (mountent_t *),
6547c478bd9Sstevel@tonic-gate 		    mcompar);
6557c478bd9Sstevel@tonic-gate 		/*
6567c478bd9Sstevel@tonic-gate 		 * If we do not detect a lofs by now, we never will.
6577c478bd9Sstevel@tonic-gate 		 */
6587c478bd9Sstevel@tonic-gate 		lofscnt = -1;
6597c478bd9Sstevel@tonic-gate 	}
6607c478bd9Sstevel@tonic-gate 	/*
6617c478bd9Sstevel@tonic-gate 	 * Now link them up so that a given pid is easier to find when
6627c478bd9Sstevel@tonic-gate 	 * we go to clean up after they are done.
6637c478bd9Sstevel@tonic-gate 	 */
6647c478bd9Sstevel@tonic-gate 	mntll = mntarray[0];
6657c478bd9Sstevel@tonic-gate 	for (ml = mntarray; mp = *ml; /* */)
6667c478bd9Sstevel@tonic-gate 		mp->link = *++ml;
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	/*
6697c478bd9Sstevel@tonic-gate 	 * Try to handle interrupts in a reasonable way.
6707c478bd9Sstevel@tonic-gate 	 */
6717c478bd9Sstevel@tonic-gate 	sigset(SIGHUP, cleanup);
6727c478bd9Sstevel@tonic-gate 	sigset(SIGQUIT, cleanup);
6737c478bd9Sstevel@tonic-gate 	sigset(SIGINT, cleanup);
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	do_umounts(mntarray);	/* do the umounts */
6767c478bd9Sstevel@tonic-gate 	return (exitcode);
6777c478bd9Sstevel@tonic-gate }
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate /*
6807c478bd9Sstevel@tonic-gate  * Returns a mountent_t array based on mntlist.  If mntlist is NULL, then
6817c478bd9Sstevel@tonic-gate  * it returns all mnttab entries with a few exceptions.  Sets the global
6827c478bd9Sstevel@tonic-gate  * variable listlength to the number of entries in the array.
6837c478bd9Sstevel@tonic-gate  */
6847c478bd9Sstevel@tonic-gate mountent_t **
6857c478bd9Sstevel@tonic-gate make_mntarray(char **mntlist, int count)
6867c478bd9Sstevel@tonic-gate {
6877c478bd9Sstevel@tonic-gate 	mountent_t 	*mp, **mpp;
6887c478bd9Sstevel@tonic-gate 	int 		ndx;
6897c478bd9Sstevel@tonic-gate 	char		*cp;
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	if (count > 0)
6927c478bd9Sstevel@tonic-gate 		listlength = count;
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	mpp = (mountent_t **)malloc(sizeof (*mp) * (listlength + 1));
6957c478bd9Sstevel@tonic-gate 	if (mpp == NULL)
6967c478bd9Sstevel@tonic-gate 		nomem();
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	if (count == 0) {
6997c478bd9Sstevel@tonic-gate 		if (mntll == NULL) {	/* no entries? */
7007c478bd9Sstevel@tonic-gate 			listlength = 0;
7017c478bd9Sstevel@tonic-gate 			return (NULL);
7027c478bd9Sstevel@tonic-gate 		}
7037c478bd9Sstevel@tonic-gate 		/*
7047c478bd9Sstevel@tonic-gate 		 * No mount list specified: take all mnttab mount points
7057c478bd9Sstevel@tonic-gate 		 * except for a few cases.
7067c478bd9Sstevel@tonic-gate 		 */
7077c478bd9Sstevel@tonic-gate 		for (ndx = 0, mp = mntll; mp; mp = mp->link) {
7087c478bd9Sstevel@tonic-gate 			if (fsstrinlist(mp->ment.mnt_mountp, keeplist))
7097c478bd9Sstevel@tonic-gate 				continue;
7107c478bd9Sstevel@tonic-gate 			mp->mlevel = fsgetmlevel(mp->ment.mnt_mountp);
7117c478bd9Sstevel@tonic-gate 			if (mp->ment.mnt_fstype &&
7127c478bd9Sstevel@tonic-gate 			    (strcmp(mp->ment.mnt_fstype, MNTTYPE_LOFS) == 0))
7137c478bd9Sstevel@tonic-gate 				lofscnt++;
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 			mpp[ndx++] = mp;
7167c478bd9Sstevel@tonic-gate 		}
7177c478bd9Sstevel@tonic-gate 		mpp[ndx] = NULL;
7187c478bd9Sstevel@tonic-gate 		listlength = ndx;
7197c478bd9Sstevel@tonic-gate 		return (mpp);
7207c478bd9Sstevel@tonic-gate 	}
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	/*
7237c478bd9Sstevel@tonic-gate 	 * A list of mount points was specified on the command line.
7247c478bd9Sstevel@tonic-gate 	 * Build an array out of these.
7257c478bd9Sstevel@tonic-gate 	 */
7267c478bd9Sstevel@tonic-gate 	for (ndx = 0; count--; ) {
7277c478bd9Sstevel@tonic-gate 		cp = *mntlist++;
7287c478bd9Sstevel@tonic-gate 		if (realpath(cp, resolve) == NULL) {
7297c478bd9Sstevel@tonic-gate 			fprintf(stderr,
7307c478bd9Sstevel@tonic-gate 			    gettext("%s: warning: can't resolve %s\n"),
7317c478bd9Sstevel@tonic-gate 			    myname, cp);
7327c478bd9Sstevel@tonic-gate 			exitcode = 1;
7337c478bd9Sstevel@tonic-gate 			mp = getmntlast(mntll, NULL, cp); /* try anyways */
7347c478bd9Sstevel@tonic-gate 		} else
7357c478bd9Sstevel@tonic-gate 			mp = getmntlast(mntll, NULL, resolve);
7367c478bd9Sstevel@tonic-gate 		if (mp == NULL) {
7377c478bd9Sstevel@tonic-gate 			struct mnttab mnew;
7387c478bd9Sstevel@tonic-gate 			/*
7397c478bd9Sstevel@tonic-gate 			 * Then we've reached the end without finding
7407c478bd9Sstevel@tonic-gate 			 * what we are looking for, but we still have to
7417c478bd9Sstevel@tonic-gate 			 * try to umount it: append it to mntarray.
7427c478bd9Sstevel@tonic-gate 			 */
7437c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(
7447c478bd9Sstevel@tonic-gate 			    "%s: warning: %s not found in %s\n"),
7457c478bd9Sstevel@tonic-gate 			    myname, resolve, mnttab);
7467c478bd9Sstevel@tonic-gate 			exitcode = 1;
7477c478bd9Sstevel@tonic-gate 			mntnull(&mnew);
7487c478bd9Sstevel@tonic-gate 			mnew.mnt_special = mnew.mnt_mountp = strdup(resolve);
7497c478bd9Sstevel@tonic-gate 			if (mnew.mnt_special == NULL)
7507c478bd9Sstevel@tonic-gate 				nomem();
7517c478bd9Sstevel@tonic-gate 			mp = new_mountent(&mnew);
7527c478bd9Sstevel@tonic-gate 		}
7537c478bd9Sstevel@tonic-gate 		if (mp->ment.mnt_fstype &&
7547c478bd9Sstevel@tonic-gate 		    (strcmp(mp->ment.mnt_fstype, MNTTYPE_LOFS) == 0))
7557c478bd9Sstevel@tonic-gate 			lofscnt++;
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 		mp->mlevel = fsgetmlevel(mp->ment.mnt_mountp);
7587c478bd9Sstevel@tonic-gate 		mpp[ndx++] = mp;
7597c478bd9Sstevel@tonic-gate 	}
7607c478bd9Sstevel@tonic-gate 	mpp[ndx] = NULL;
7617c478bd9Sstevel@tonic-gate 	listlength = ndx;
7627c478bd9Sstevel@tonic-gate 	return (mpp);
7637c478bd9Sstevel@tonic-gate }
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate /*
7667c478bd9Sstevel@tonic-gate  * Returns the tail of a linked list of all mnttab entries.  I.e, it's faster
7677c478bd9Sstevel@tonic-gate  * to return the mnttab in reverse order.
7687c478bd9Sstevel@tonic-gate  * Sets listlength to the number of entries in the list.
7697c478bd9Sstevel@tonic-gate  * Returns NULL if none are found.
7707c478bd9Sstevel@tonic-gate  */
7717c478bd9Sstevel@tonic-gate mountent_t *
77208190127Sdh145677 getmntall(void)
7737c478bd9Sstevel@tonic-gate {
7747c478bd9Sstevel@tonic-gate 	FILE		*fp;
7757c478bd9Sstevel@tonic-gate 	mountent_t	*mtail;
7767c478bd9Sstevel@tonic-gate 	int		cnt = 0, ret;
7777c478bd9Sstevel@tonic-gate 	struct mnttab	mget;
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	if ((fp = fopen(mnttab, "r")) == NULL) {
7807c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: warning cannot open %s\n"),
7817c478bd9Sstevel@tonic-gate 		    myname, mnttab);
7827c478bd9Sstevel@tonic-gate 		return (0);
7837c478bd9Sstevel@tonic-gate 	}
7847c478bd9Sstevel@tonic-gate 	mtail = NULL;
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 	while ((ret = getmntent(fp, &mget)) != -1) {
7877c478bd9Sstevel@tonic-gate 		mountent_t	*mp;
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 		if (ret > 0) {
7907c478bd9Sstevel@tonic-gate 			mnterror(ret);
7917c478bd9Sstevel@tonic-gate 			continue;
7927c478bd9Sstevel@tonic-gate 		}
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 		mp = new_mountent(&mget);
7957c478bd9Sstevel@tonic-gate 		mp->link = mtail;
7967c478bd9Sstevel@tonic-gate 		mtail = mp;
7977c478bd9Sstevel@tonic-gate 		cnt++;
7987c478bd9Sstevel@tonic-gate 	}
7997c478bd9Sstevel@tonic-gate 	fclose(fp);
8007c478bd9Sstevel@tonic-gate 	if (mtail == NULL) {
8017c478bd9Sstevel@tonic-gate 		listlength = 0;
8027c478bd9Sstevel@tonic-gate 		return (NULL);
8037c478bd9Sstevel@tonic-gate 	}
8047c478bd9Sstevel@tonic-gate 	listlength = cnt;
8057c478bd9Sstevel@tonic-gate 	return (mtail);
8067c478bd9Sstevel@tonic-gate }
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate void
8097c478bd9Sstevel@tonic-gate do_umounts(mountent_t **mntarray)
8107c478bd9Sstevel@tonic-gate {
8117c478bd9Sstevel@tonic-gate 	mountent_t *mp, *mpprev, **ml = mntarray;
8127c478bd9Sstevel@tonic-gate 	int	cnt = listlength;
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	/*
8157c478bd9Sstevel@tonic-gate 	 * Main loop for the forked children:
8167c478bd9Sstevel@tonic-gate 	 */
8177c478bd9Sstevel@tonic-gate 	for (mpprev = *ml; mp = *ml; mpprev = mp, ml++, cnt--) {
8187c478bd9Sstevel@tonic-gate 		pid_t	pid;
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 		/*
8217c478bd9Sstevel@tonic-gate 		 * Check to see if we cross a mount level: e.g.,
8227c478bd9Sstevel@tonic-gate 		 * /a/b/c -> /a/b.  If so, we need to wait for all current
8237c478bd9Sstevel@tonic-gate 		 * umounts to finish before umounting the rest.
8247c478bd9Sstevel@tonic-gate 		 *
8257c478bd9Sstevel@tonic-gate 		 * Also, we unmount serially as long as there are lofs's
8267c478bd9Sstevel@tonic-gate 		 * to mount to avoid improper umount ordering.
8277c478bd9Sstevel@tonic-gate 		 */
8287c478bd9Sstevel@tonic-gate 		if (mp->mlevel < mpprev->mlevel || lofscnt > 0)
8297c478bd9Sstevel@tonic-gate 			while (nrun > 0 && (dowait() != -1))
8307c478bd9Sstevel@tonic-gate 				;
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 		if (lofscnt == 0) {
8337c478bd9Sstevel@tonic-gate 			/*
8347c478bd9Sstevel@tonic-gate 			 * We can now go to parallel umounting.
8357c478bd9Sstevel@tonic-gate 			 */
8367c478bd9Sstevel@tonic-gate 			qsort((void *)ml, cnt, sizeof (mountent_t *), mcompar);
8377c478bd9Sstevel@tonic-gate 			mp = *ml;	/* possible first entry */
8387c478bd9Sstevel@tonic-gate 			lofscnt--;	/* so we don't do this again */
8397c478bd9Sstevel@tonic-gate 		}
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 		while (setup_iopipe(mp) == -1 && (dowait() != -1))
8427c478bd9Sstevel@tonic-gate 			;
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 		while (nrun >= maxrun && (dowait() != -1))	/* throttle */
8457c478bd9Sstevel@tonic-gate 			;
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate #ifdef CACHEFS_BUG
8487c478bd9Sstevel@tonic-gate 		/*
8497c478bd9Sstevel@tonic-gate 		 * If this is the back file system, then let cachefs/umount
8507c478bd9Sstevel@tonic-gate 		 * unmount it.
8517c478bd9Sstevel@tonic-gate 		 */
8527c478bd9Sstevel@tonic-gate 		if (strstr(mp->ment.mnt_mountp, BACKMNT_NAME))
8537c478bd9Sstevel@tonic-gate 			continue;
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 		if (mp->ment.mnt_fstype &&
8577c478bd9Sstevel@tonic-gate 		    (strcmp(mp->ment.mnt_fstype, "cachefs") == 0)) {
8587c478bd9Sstevel@tonic-gate 			while (cachefs_running && (dowait() != -1))
8597c478bd9Sstevel@tonic-gate 					;
8607c478bd9Sstevel@tonic-gate 			cachefs_running = 1;
8617c478bd9Sstevel@tonic-gate 		}
8627c478bd9Sstevel@tonic-gate #endif
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 		if ((pid = fork()) == -1) {
8657c478bd9Sstevel@tonic-gate 			perror("fork");
8667c478bd9Sstevel@tonic-gate 			cleanup(-1);
8677c478bd9Sstevel@tonic-gate 			/* not reached */
8687c478bd9Sstevel@tonic-gate 		}
8697c478bd9Sstevel@tonic-gate #ifdef DEBUG
8707c478bd9Sstevel@tonic-gate 		if (dflg && pid > 0) {
8717c478bd9Sstevel@tonic-gate 			fprintf(stderr, "parent %d: umounting %d %s\n",
8727c478bd9Sstevel@tonic-gate 			    getpid(), pid, mp->ment.mnt_mountp);
8737c478bd9Sstevel@tonic-gate 		}
8747c478bd9Sstevel@tonic-gate #endif
8757c478bd9Sstevel@tonic-gate 		if (pid == 0) {		/* child */
8767c478bd9Sstevel@tonic-gate 			signal(SIGHUP, SIG_IGN);
8777c478bd9Sstevel@tonic-gate 			signal(SIGQUIT, SIG_IGN);
8787c478bd9Sstevel@tonic-gate 			signal(SIGINT, SIG_IGN);
8797c478bd9Sstevel@tonic-gate 			setup_output(mp);
8807c478bd9Sstevel@tonic-gate 			doexec(&mp->ment);
8817c478bd9Sstevel@tonic-gate 			perror("exec");
8827c478bd9Sstevel@tonic-gate 			exit(1);
8837c478bd9Sstevel@tonic-gate 		}
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 		/* parent */
8867c478bd9Sstevel@tonic-gate 		(void) close(mp->sopipe[WRPIPE]);
8877c478bd9Sstevel@tonic-gate 		(void) close(mp->sepipe[WRPIPE]);
8887c478bd9Sstevel@tonic-gate 		mp->pid = pid;
8897c478bd9Sstevel@tonic-gate 		nrun++;
8907c478bd9Sstevel@tonic-gate 	}
8917c478bd9Sstevel@tonic-gate 	cleanup(0);
8927c478bd9Sstevel@tonic-gate }
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate /*
8957c478bd9Sstevel@tonic-gate  * cleanup the existing children and exit with an error
8967c478bd9Sstevel@tonic-gate  * if asig != 0.
8977c478bd9Sstevel@tonic-gate  */
8987c478bd9Sstevel@tonic-gate void
8997c478bd9Sstevel@tonic-gate cleanup(int asig)
9007c478bd9Sstevel@tonic-gate {
9017c478bd9Sstevel@tonic-gate 	/*
9027c478bd9Sstevel@tonic-gate 	 * Let the stragglers finish.
9037c478bd9Sstevel@tonic-gate 	 */
9047c478bd9Sstevel@tonic-gate 	while (nrun > 0 && (dowait() != -1))
9057c478bd9Sstevel@tonic-gate 		;
9067c478bd9Sstevel@tonic-gate 	if (asig != 0)
9077c478bd9Sstevel@tonic-gate 		exit(1);
9087c478bd9Sstevel@tonic-gate }
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate /*
9127c478bd9Sstevel@tonic-gate  * Waits for 1 child to die.
9137c478bd9Sstevel@tonic-gate  *
9147c478bd9Sstevel@tonic-gate  * Returns -1 if no children are left to wait for.
9157c478bd9Sstevel@tonic-gate  * Returns 0 if a child died without an error.
9167c478bd9Sstevel@tonic-gate  * Returns 1 if a child died with an error.
9177c478bd9Sstevel@tonic-gate  * Sets the global exitcode if an error occurred.
9187c478bd9Sstevel@tonic-gate  */
9197c478bd9Sstevel@tonic-gate int
92008190127Sdh145677 dowait(void)
9217c478bd9Sstevel@tonic-gate {
9227c478bd9Sstevel@tonic-gate 	int		wstat, child, ret;
9237c478bd9Sstevel@tonic-gate 	mountent_t 	*mp, *prevp;
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	if ((child = wait(&wstat)) == -1)
9267c478bd9Sstevel@tonic-gate 		return (-1);
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	if (WIFEXITED(wstat))		/* this should always be true */
9297c478bd9Sstevel@tonic-gate 		ret = WEXITSTATUS(wstat);
9307c478bd9Sstevel@tonic-gate 	else
9317c478bd9Sstevel@tonic-gate 		ret = 1;		/* assume some kind of error */
9327c478bd9Sstevel@tonic-gate 	nrun--;
9337c478bd9Sstevel@tonic-gate 	if (ret)
9347c478bd9Sstevel@tonic-gate 		exitcode = 1;
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 	/*
9377c478bd9Sstevel@tonic-gate 	 * Find our child so we can process its std output, if any.
9387c478bd9Sstevel@tonic-gate 	 * This search gets smaller and smaller as children are cleaned
9397c478bd9Sstevel@tonic-gate 	 * up.
9407c478bd9Sstevel@tonic-gate 	 */
9417c478bd9Sstevel@tonic-gate 	for (prevp = NULL, mp = mntll; mp; mp = mp->link) {
9427c478bd9Sstevel@tonic-gate 		if (mp->pid != child) {
9437c478bd9Sstevel@tonic-gate 			prevp = mp;
9447c478bd9Sstevel@tonic-gate 			continue;
9457c478bd9Sstevel@tonic-gate 		}
9467c478bd9Sstevel@tonic-gate 		/*
9477c478bd9Sstevel@tonic-gate 		 * Found: let's remove it from this list.
9487c478bd9Sstevel@tonic-gate 		 */
9497c478bd9Sstevel@tonic-gate 		if (prevp) {
9507c478bd9Sstevel@tonic-gate 			prevp->link = mp->link;
9517c478bd9Sstevel@tonic-gate 			mp->link = NULL;
9527c478bd9Sstevel@tonic-gate 		}
9537c478bd9Sstevel@tonic-gate 		break;
9547c478bd9Sstevel@tonic-gate 	}
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 	if (mp == NULL) {
9577c478bd9Sstevel@tonic-gate 		/*
9587c478bd9Sstevel@tonic-gate 		 * This should never happen.
9597c478bd9Sstevel@tonic-gate 		 */
9607c478bd9Sstevel@tonic-gate #ifdef DEBUG
9617c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(
9627c478bd9Sstevel@tonic-gate 		    "%s: unknown child %d\n"), myname, child);
9637c478bd9Sstevel@tonic-gate #endif
9647c478bd9Sstevel@tonic-gate 		exitcode = 1;
9657c478bd9Sstevel@tonic-gate 		return (1);
9667c478bd9Sstevel@tonic-gate 	}
9677c478bd9Sstevel@tonic-gate 	doio(mp);	/* Any output? */
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	if (mp->ment.mnt_fstype &&
9707c478bd9Sstevel@tonic-gate 	    (strcmp(mp->ment.mnt_fstype, MNTTYPE_LOFS) == 0))
9717c478bd9Sstevel@tonic-gate 		lofscnt--;
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate #ifdef CACHEFS_BUG
9747c478bd9Sstevel@tonic-gate 	if (mp->ment.mnt_fstype &&
9757c478bd9Sstevel@tonic-gate 	    (strcmp(mp->ment.mnt_fstype, "cachefs") == 0))
9767c478bd9Sstevel@tonic-gate 		cachefs_running = 0;
9777c478bd9Sstevel@tonic-gate #endif
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 	return (ret);
9807c478bd9Sstevel@tonic-gate }
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate static const mountent_t zmount = { 0 };
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate mountent_t *
9857c478bd9Sstevel@tonic-gate new_mountent(struct mnttab *ment)
9867c478bd9Sstevel@tonic-gate {
9877c478bd9Sstevel@tonic-gate 	mountent_t *new;
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 	new = (mountent_t *)malloc(sizeof (*new));
9907c478bd9Sstevel@tonic-gate 	if (new == NULL)
9917c478bd9Sstevel@tonic-gate 		nomem();
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	*new = zmount;
9947c478bd9Sstevel@tonic-gate 	if (ment->mnt_special &&
9957c478bd9Sstevel@tonic-gate 	    (new->ment.mnt_special = strdup(ment->mnt_special)) == NULL)
9967c478bd9Sstevel@tonic-gate 		nomem();
9977c478bd9Sstevel@tonic-gate 	if (ment->mnt_mountp &&
9987c478bd9Sstevel@tonic-gate 	    (new->ment.mnt_mountp = strdup(ment->mnt_mountp)) == NULL)
9997c478bd9Sstevel@tonic-gate 		nomem();
10007c478bd9Sstevel@tonic-gate 	if (ment->mnt_fstype &&
10017c478bd9Sstevel@tonic-gate 	    (new->ment.mnt_fstype = strdup(ment->mnt_fstype)) == NULL)
10027c478bd9Sstevel@tonic-gate 		nomem();
10037c478bd9Sstevel@tonic-gate 	return (new);
10047c478bd9Sstevel@tonic-gate }
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate /*
10087c478bd9Sstevel@tonic-gate  * Sort in descending order of "mount level".  For example, /a/b/c is
10097c478bd9Sstevel@tonic-gate  * placed before /a/b .
10107c478bd9Sstevel@tonic-gate  */
10117c478bd9Sstevel@tonic-gate int
10127c478bd9Sstevel@tonic-gate mcompar(const void *a, const void *b)
10137c478bd9Sstevel@tonic-gate {
10147c478bd9Sstevel@tonic-gate 	mountent_t *a1, *b1;
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	a1 = *(mountent_t **)a;
10177c478bd9Sstevel@tonic-gate 	b1 = *(mountent_t **)b;
10187c478bd9Sstevel@tonic-gate 	return (b1->mlevel - a1->mlevel);
10197c478bd9Sstevel@tonic-gate }
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate /*
10227c478bd9Sstevel@tonic-gate  * The purpose of this routine is to form stdout and stderr
10237c478bd9Sstevel@tonic-gate  * pipes for the children's output.  The parent then reads and writes it
10247c478bd9Sstevel@tonic-gate  * out it serially in order to ensure that the output is
10257c478bd9Sstevel@tonic-gate  * not garbled.
10267c478bd9Sstevel@tonic-gate  */
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate int
10297c478bd9Sstevel@tonic-gate setup_iopipe(mountent_t *mp)
10307c478bd9Sstevel@tonic-gate {
10317c478bd9Sstevel@tonic-gate 	/*
10327c478bd9Sstevel@tonic-gate 	 * Make a stdout and stderr pipe.  This should never fail.
10337c478bd9Sstevel@tonic-gate 	 */
10347c478bd9Sstevel@tonic-gate 	if (pipe(mp->sopipe) == -1)
10357c478bd9Sstevel@tonic-gate 		return (-1);
10367c478bd9Sstevel@tonic-gate 	if (pipe(mp->sepipe) == -1) {
10377c478bd9Sstevel@tonic-gate 		(void) close(mp->sopipe[RDPIPE]);
10387c478bd9Sstevel@tonic-gate 		(void) close(mp->sopipe[WRPIPE]);
10397c478bd9Sstevel@tonic-gate 		return (-1);
10407c478bd9Sstevel@tonic-gate 	}
10417c478bd9Sstevel@tonic-gate 	/*
10427c478bd9Sstevel@tonic-gate 	 * Don't block on an empty pipe.
10437c478bd9Sstevel@tonic-gate 	 */
10447c478bd9Sstevel@tonic-gate 	(void) fcntl(mp->sopipe[RDPIPE], F_SETFL, O_NDELAY|O_NONBLOCK);
10457c478bd9Sstevel@tonic-gate 	(void) fcntl(mp->sepipe[RDPIPE], F_SETFL, O_NDELAY|O_NONBLOCK);
10467c478bd9Sstevel@tonic-gate 	return (0);
10477c478bd9Sstevel@tonic-gate }
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate /*
10507c478bd9Sstevel@tonic-gate  * Called by a child to attach its stdout and stderr to the write side of
10517c478bd9Sstevel@tonic-gate  * the pipes.
10527c478bd9Sstevel@tonic-gate  */
10537c478bd9Sstevel@tonic-gate void
10547c478bd9Sstevel@tonic-gate setup_output(mountent_t *mp)
10557c478bd9Sstevel@tonic-gate {
10567c478bd9Sstevel@tonic-gate 	(void) close(fileno(stdout));
10577c478bd9Sstevel@tonic-gate 	(void) dup(mp->sopipe[WRPIPE]);
10587c478bd9Sstevel@tonic-gate 	(void) close(mp->sopipe[WRPIPE]);
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	(void) close(fileno(stderr));
10617c478bd9Sstevel@tonic-gate 	(void) dup(mp->sepipe[WRPIPE]);
10627c478bd9Sstevel@tonic-gate 	(void) close(mp->sepipe[WRPIPE]);
10637c478bd9Sstevel@tonic-gate }
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate /*
10667c478bd9Sstevel@tonic-gate  * Parent uses this to print any stdout or stderr output issued by
10677c478bd9Sstevel@tonic-gate  * the child.
10687c478bd9Sstevel@tonic-gate  */
10697c478bd9Sstevel@tonic-gate static void
10707c478bd9Sstevel@tonic-gate doio(mountent_t *mp)
10717c478bd9Sstevel@tonic-gate {
10727c478bd9Sstevel@tonic-gate 	int bytes;
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	while ((bytes = read(mp->sepipe[RDPIPE], ibuf, sizeof (ibuf))) > 0)
10757c478bd9Sstevel@tonic-gate 		write(fileno(stderr), ibuf, bytes);
10767c478bd9Sstevel@tonic-gate 	while ((bytes = read(mp->sopipe[RDPIPE], ibuf, sizeof (ibuf))) > 0)
10777c478bd9Sstevel@tonic-gate 		write(fileno(stdout), ibuf, bytes);
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 	(void) close(mp->sopipe[RDPIPE]);
10807c478bd9Sstevel@tonic-gate 	(void) close(mp->sepipe[RDPIPE]);
10817c478bd9Sstevel@tonic-gate }
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate void
108408190127Sdh145677 nomem(void)
10857c478bd9Sstevel@tonic-gate {
10867c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext("%s: out of memory\n"), myname);
10877c478bd9Sstevel@tonic-gate 	/*
10887c478bd9Sstevel@tonic-gate 	 * Let the stragglers finish.
10897c478bd9Sstevel@tonic-gate 	 */
10907c478bd9Sstevel@tonic-gate 	while (nrun > 0 && (dowait() != -1))
10917c478bd9Sstevel@tonic-gate 		;
10927c478bd9Sstevel@tonic-gate 	exit(1);
10937c478bd9Sstevel@tonic-gate }
1094