xref: /onnv-gate/usr/src/cmd/fs.d/umount.c (revision 12019:cfe7a7d0d7f0)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51914Scasper  * Common Development and Distribution License (the "License").
61914Scasper  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*12019SGordon.Ross@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include	<stdio.h>
311914Scasper #include	<stdio_ext.h>
320Sstevel@tonic-gate #include	<limits.h>
330Sstevel@tonic-gate #include	<unistd.h>
340Sstevel@tonic-gate #include	<stdlib.h>
350Sstevel@tonic-gate #include	<string.h>
360Sstevel@tonic-gate #include	<sys/signal.h>
370Sstevel@tonic-gate #include	<sys/mnttab.h>
380Sstevel@tonic-gate #include	<errno.h>
390Sstevel@tonic-gate #include	<sys/types.h>
400Sstevel@tonic-gate #include	<sys/stat.h>
410Sstevel@tonic-gate #include	<sys/param.h>
420Sstevel@tonic-gate #include	<sys/wait.h>
430Sstevel@tonic-gate #include	<sys/vfstab.h>
440Sstevel@tonic-gate #include	<sys/fcntl.h>
450Sstevel@tonic-gate #include	<sys/resource.h>
460Sstevel@tonic-gate #include	<sys/mntent.h>
470Sstevel@tonic-gate #include	<sys/ctfs.h>
480Sstevel@tonic-gate #include	<locale.h>
490Sstevel@tonic-gate #include	<stdarg.h>
500Sstevel@tonic-gate #include	<sys/mount.h>
510Sstevel@tonic-gate #include	<sys/objfs.h>
520Sstevel@tonic-gate #include	"fslib.h"
533957Sth199096 #include	<sharefs/share.h>
540Sstevel@tonic-gate 
550Sstevel@tonic-gate #define	FS_PATH		"/usr/lib/fs"
560Sstevel@tonic-gate #define	ALT_PATH	"/etc/fs"
570Sstevel@tonic-gate #define	FULLPATH_MAX	32
580Sstevel@tonic-gate #define	FSTYPE_MAX	8
590Sstevel@tonic-gate #define	ARGV_MAX	16
600Sstevel@tonic-gate 
610Sstevel@tonic-gate int	aflg, oflg, Vflg, dashflg, dflg, fflg;
620Sstevel@tonic-gate 
630Sstevel@tonic-gate extern void	rpterr(), usage(), mnterror();
640Sstevel@tonic-gate 
650Sstevel@tonic-gate extern	char	*optarg;	/* used by getopt */
660Sstevel@tonic-gate extern	int	optind, opterr;
670Sstevel@tonic-gate 
680Sstevel@tonic-gate static char	*myname;
690Sstevel@tonic-gate char	fs_path[] = FS_PATH;
700Sstevel@tonic-gate char	alt_path[] = ALT_PATH;
710Sstevel@tonic-gate char	mnttab[MAXPATHLEN + 1];
720Sstevel@tonic-gate char	*oarg, *farg;
730Sstevel@tonic-gate int	maxrun, nrun;
740Sstevel@tonic-gate int	no_mnttab;
750Sstevel@tonic-gate int	lofscnt;		/* presence of lofs prohibits parallel */
760Sstevel@tonic-gate 				/* umounting */
770Sstevel@tonic-gate int	exitcode;
780Sstevel@tonic-gate char	resolve[MAXPATHLEN];
790Sstevel@tonic-gate static  char ibuf[BUFSIZ];
800Sstevel@tonic-gate 
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate  * Currently, mounting cachefs's simultaneous uncovers various problems.
830Sstevel@tonic-gate  * For the short term, we serialize cachefs activity while we fix
840Sstevel@tonic-gate  * these cachefs bugs.
850Sstevel@tonic-gate  */
860Sstevel@tonic-gate #define	CACHEFS_BUG
870Sstevel@tonic-gate #ifdef	CACHEFS_BUG
880Sstevel@tonic-gate #include	<sys/fs/cachefs_fs.h>	/* for BACKMNT_NAME */
890Sstevel@tonic-gate int	cachefs_running;	/* parallel cachefs not supported yet */
900Sstevel@tonic-gate #endif
910Sstevel@tonic-gate 
920Sstevel@tonic-gate /*
930Sstevel@tonic-gate  * The basic mount struct that describes an mnttab entry.
940Sstevel@tonic-gate  * It is used both in an array and as a linked list elem.
950Sstevel@tonic-gate  */
960Sstevel@tonic-gate 
970Sstevel@tonic-gate typedef struct mountent {
980Sstevel@tonic-gate 	struct mnttab	ment;		/* the mnttab data */
990Sstevel@tonic-gate 	int		mlevel;		/* mount level of the mount pt */
1000Sstevel@tonic-gate 	pid_t		pid;		/* the pid of this mount process */
1010Sstevel@tonic-gate #define	RDPIPE		0
1020Sstevel@tonic-gate #define	WRPIPE		1
1030Sstevel@tonic-gate 	int		sopipe[2];	/* pipe attached to child's stdout */
1040Sstevel@tonic-gate 	int		sepipe[2];	/* pipe attached to child's stderr */
1050Sstevel@tonic-gate 	struct mountent *link;		/* used when in linked list */
1060Sstevel@tonic-gate } mountent_t;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate static mountent_t	*mntll;		/* head of global linked list of */
1090Sstevel@tonic-gate 					/* mountents */
1100Sstevel@tonic-gate int			listlength;	/* # of elems in this list */
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate /*
1130Sstevel@tonic-gate  * If the automatic flag (-a) is given and mount points are not specified
1140Sstevel@tonic-gate  * on the command line, then do not attempt to umount these.  These
1150Sstevel@tonic-gate  * generally need to be kept mounted until system shutdown.
1160Sstevel@tonic-gate  */
1170Sstevel@tonic-gate static const char   *keeplist[] = {
1180Sstevel@tonic-gate 	"/",
1190Sstevel@tonic-gate 	"/dev",
1200Sstevel@tonic-gate 	"/dev/fd",
1210Sstevel@tonic-gate 	"/devices",
1220Sstevel@tonic-gate 	"/etc/mnttab",
1230Sstevel@tonic-gate 	"/etc/svc/volatile",
1240Sstevel@tonic-gate 	"/lib",
1250Sstevel@tonic-gate 	"/proc",
1260Sstevel@tonic-gate 	"/sbin",
1270Sstevel@tonic-gate 	CTFS_ROOT,
1280Sstevel@tonic-gate 	OBJFS_ROOT,
1290Sstevel@tonic-gate 	"/tmp",
1300Sstevel@tonic-gate 	"/usr",
1310Sstevel@tonic-gate 	"/var",
1320Sstevel@tonic-gate 	"/var/adm",
1330Sstevel@tonic-gate 	"/var/run",
1343957Sth199096 	SHARETAB,
1350Sstevel@tonic-gate 	NULL
1360Sstevel@tonic-gate };
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate static void	nomem();
1390Sstevel@tonic-gate static void	doexec(struct mnttab *);
1400Sstevel@tonic-gate static int	setup_iopipe(mountent_t *);
1410Sstevel@tonic-gate static void	setup_output(mountent_t *);
1420Sstevel@tonic-gate static void	doio(mountent_t *);
1430Sstevel@tonic-gate static void	do_umounts(mountent_t **);
1440Sstevel@tonic-gate static int	dowait();
1450Sstevel@tonic-gate static int	parumount();
1460Sstevel@tonic-gate static int	mcompar(const void *, const void *);
1470Sstevel@tonic-gate static void	cleanup(int);
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate static mountent_t	**make_mntarray(char **, int);
1500Sstevel@tonic-gate static mountent_t	*getmntall();
1510Sstevel@tonic-gate static mountent_t 	*new_mountent(struct mnttab *);
1520Sstevel@tonic-gate static mountent_t	*getmntlast(mountent_t *, char *, char *);
1530Sstevel@tonic-gate 
154821Sdh145677 int
155821Sdh145677 main(int argc, char **argv)
1560Sstevel@tonic-gate {
1570Sstevel@tonic-gate 	int 	cc;
1580Sstevel@tonic-gate 	struct mnttab  mget;
1590Sstevel@tonic-gate 	char 	*mname, *is_special;
1600Sstevel@tonic-gate 	int	fscnt;
1610Sstevel@tonic-gate 	mountent_t	*mp;
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
1660Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
1670Sstevel@tonic-gate #endif
1680Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	myname = strrchr(argv[0], '/');
1710Sstevel@tonic-gate 	if (myname)
1720Sstevel@tonic-gate 		myname++;
1730Sstevel@tonic-gate 	else
1740Sstevel@tonic-gate 		myname = argv[0];
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	/*
1770Sstevel@tonic-gate 	 * Process the args.
1780Sstevel@tonic-gate 	 * "-d" for compatibility
1790Sstevel@tonic-gate 	 */
1800Sstevel@tonic-gate 	while ((cc = getopt(argc, argv, "ado:Vf?")) != -1)
1810Sstevel@tonic-gate 		switch (cc) {
1820Sstevel@tonic-gate 		case 'a':
1830Sstevel@tonic-gate 			aflg++;
1840Sstevel@tonic-gate 			break;
1850Sstevel@tonic-gate #ifdef DEBUG
1860Sstevel@tonic-gate 		case 'd':
1870Sstevel@tonic-gate 			dflg++;
1880Sstevel@tonic-gate 			break;
1890Sstevel@tonic-gate #endif
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 		case '?':
1920Sstevel@tonic-gate 			usage();
1930Sstevel@tonic-gate 			break;
1940Sstevel@tonic-gate 		case 'o':
1950Sstevel@tonic-gate 			if (oflg)
1960Sstevel@tonic-gate 				usage();
1970Sstevel@tonic-gate 			else {
1980Sstevel@tonic-gate 				oflg++;
1990Sstevel@tonic-gate 				oarg = optarg;
2000Sstevel@tonic-gate 			}
2010Sstevel@tonic-gate 			break;
2020Sstevel@tonic-gate 		case 'f':
2030Sstevel@tonic-gate 			fflg++;
2040Sstevel@tonic-gate 			break;
2050Sstevel@tonic-gate 		case 'V':
2060Sstevel@tonic-gate 			if (Vflg)
2070Sstevel@tonic-gate 				usage();
2080Sstevel@tonic-gate 			else
2090Sstevel@tonic-gate 				Vflg++;
2100Sstevel@tonic-gate 			break;
2110Sstevel@tonic-gate 		default:
2120Sstevel@tonic-gate 			usage();
2130Sstevel@tonic-gate 			break;
2140Sstevel@tonic-gate 		}
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	fscnt = argc - optind;
2170Sstevel@tonic-gate 	if (!aflg && fscnt != 1)
2180Sstevel@tonic-gate 		usage();
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	/* copy '--' to specific */
2210Sstevel@tonic-gate 	if (strcmp(argv[optind-1], "--") == 0)
2220Sstevel@tonic-gate 		dashflg++;
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	/*
2250Sstevel@tonic-gate 	 * mnttab may be a symlink to a file in another file system.
2260Sstevel@tonic-gate 	 * This happens during install when / is mounted read-only
2270Sstevel@tonic-gate 	 * and /etc/mnttab is symlinked to a file in /tmp.
2280Sstevel@tonic-gate 	 * If this is the case, we need to follow the symlink to the
2290Sstevel@tonic-gate 	 * read-write file itself so that the subsequent mnttab.temp
2300Sstevel@tonic-gate 	 * open and rename will work.
2310Sstevel@tonic-gate 	 */
2320Sstevel@tonic-gate 	if (realpath(MNTTAB, mnttab) == NULL) {
2330Sstevel@tonic-gate 		strcpy(mnttab, MNTTAB);
2340Sstevel@tonic-gate 	}
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	/*
2370Sstevel@tonic-gate 	 * bugid 1205242
2380Sstevel@tonic-gate 	 * call the realpath() here, so that if the user is
2390Sstevel@tonic-gate 	 * trying to umount an autofs directory, the directory
2400Sstevel@tonic-gate 	 * is forced to mount.
2410Sstevel@tonic-gate 	 */
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	mname = argv[optind];
2440Sstevel@tonic-gate 	is_special = realpath(mname, resolve);
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	/*
2470Sstevel@tonic-gate 	 * Read the whole mnttab into memory.
2480Sstevel@tonic-gate 	 */
2490Sstevel@tonic-gate 	mntll = getmntall();
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	if (aflg && fscnt != 1)
2520Sstevel@tonic-gate 		exit(parumount(argv + optind, fscnt));
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	aflg = 0;
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	mntnull(&mget);
2570Sstevel@tonic-gate 	if (listlength == 0) {
2580Sstevel@tonic-gate 		fprintf(stderr, gettext(
2596007Sthurlow 		    "%s: warning: no entries found in %s\n"),
2606007Sthurlow 		    myname, mnttab);
2610Sstevel@tonic-gate 		mget.mnt_mountp = mname;	/* assume mount point */
2620Sstevel@tonic-gate 		no_mnttab++;
2630Sstevel@tonic-gate 		doexec(&mget);
2640Sstevel@tonic-gate 		exit(0);
2650Sstevel@tonic-gate 	}
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	mp = NULL;
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	/*
2700Sstevel@tonic-gate 	 * if realpath fails, it can't be a mount point, so we'll
2710Sstevel@tonic-gate 	 * go straight to the code that treats the arg as a special.
2720Sstevel@tonic-gate 	 * if realpath succeeds, it could be a special or a mount point;
2730Sstevel@tonic-gate 	 * we'll start by assuming it's a mount point, and if it's not,
2740Sstevel@tonic-gate 	 * try to treat it as a special.
2750Sstevel@tonic-gate 	 */
2760Sstevel@tonic-gate 	if (is_special != NULL) {
2770Sstevel@tonic-gate 		/*
2780Sstevel@tonic-gate 		 * if this succeeds,
2790Sstevel@tonic-gate 		 * we'll have the appropriate record; if it fails
2800Sstevel@tonic-gate 		 * we'll assume the arg is a special of some sort
2810Sstevel@tonic-gate 		 */
2820Sstevel@tonic-gate 		mp = getmntlast(mntll, NULL, resolve);
2830Sstevel@tonic-gate 	}
2840Sstevel@tonic-gate 	/*
2850Sstevel@tonic-gate 	 * Since stackable mount is allowed (RFE 2001535),
2860Sstevel@tonic-gate 	 * we will un-mount the last entry in the MNTTAB that matches.
2870Sstevel@tonic-gate 	 */
2880Sstevel@tonic-gate 	if (mp == NULL) {
2890Sstevel@tonic-gate 		/*
2900Sstevel@tonic-gate 		 * Perhaps there is a bogus mnttab entry that
2910Sstevel@tonic-gate 		 * can't be resolved:
2920Sstevel@tonic-gate 		 */
2930Sstevel@tonic-gate 		if ((mp = getmntlast(mntll, NULL, mname)) == NULL)
2940Sstevel@tonic-gate 			/*
2950Sstevel@tonic-gate 			 * assume it's a device (special) now
2960Sstevel@tonic-gate 			 */
2970Sstevel@tonic-gate 			mp = getmntlast(mntll, mname, NULL);
2980Sstevel@tonic-gate 		if (mp) {
2990Sstevel@tonic-gate 			/*
3000Sstevel@tonic-gate 			 * Found it.
3010Sstevel@tonic-gate 			 * This is a device. Now we want to know if
3020Sstevel@tonic-gate 			 * it stackmounted on by something else.
3030Sstevel@tonic-gate 			 * The original fix for bug 1103850 has a
3040Sstevel@tonic-gate 			 * problem with lockfs (bug 1119731). This
3050Sstevel@tonic-gate 			 * is a revised method.
3060Sstevel@tonic-gate 			 */
3070Sstevel@tonic-gate 			mountent_t *lmp;
3080Sstevel@tonic-gate 			lmp = getmntlast(mntll, NULL, mp->ment.mnt_mountp);
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 			if (lmp && strcmp(lmp->ment.mnt_special,
3116007Sthurlow 			    mp->ment.mnt_special)) {
3120Sstevel@tonic-gate 				errno = EBUSY;
3130Sstevel@tonic-gate 				rpterr(mname);
3140Sstevel@tonic-gate 				exit(1);
3150Sstevel@tonic-gate 			}
3160Sstevel@tonic-gate 		} else {
3170Sstevel@tonic-gate 			fprintf(stderr, gettext(
3186007Sthurlow 			    "%s: warning: %s not in mnttab\n"),
3196007Sthurlow 			    myname, mname);
3200Sstevel@tonic-gate 			if (Vflg)
3210Sstevel@tonic-gate 				exit(1);
3220Sstevel@tonic-gate 				/*
3230Sstevel@tonic-gate 				 * same error as mount -V
3240Sstevel@tonic-gate 				 * would give for unknown
3250Sstevel@tonic-gate 				 * mount point
3260Sstevel@tonic-gate 				 */
3270Sstevel@tonic-gate 			mget.mnt_special = mget.mnt_mountp = mname;
3280Sstevel@tonic-gate 		}
3290Sstevel@tonic-gate 	}
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	if (mp)
3320Sstevel@tonic-gate 		doexec(&mp->ment);
3330Sstevel@tonic-gate 	else
3340Sstevel@tonic-gate 		doexec(&mget);
3350Sstevel@tonic-gate 
336821Sdh145677 	return (0);
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate void
3400Sstevel@tonic-gate doexec(struct mnttab *ment)
3410Sstevel@tonic-gate {
3420Sstevel@tonic-gate 	int 	ret;
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate #ifdef DEBUG
3450Sstevel@tonic-gate 	if (dflg)
3460Sstevel@tonic-gate 		fprintf(stderr, "%d: umounting %s\n",
3476007Sthurlow 		    getpid(), ment->mnt_mountp);
3480Sstevel@tonic-gate #endif
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	/* try to exec the dependent portion */
3510Sstevel@tonic-gate 	if ((ment->mnt_fstype != NULL) || Vflg) {
3520Sstevel@tonic-gate 		char	full_path[FULLPATH_MAX];
3530Sstevel@tonic-gate 		char	alter_path[FULLPATH_MAX];
3540Sstevel@tonic-gate 		char	*newargv[ARGV_MAX];
3550Sstevel@tonic-gate 		int 	ii;
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 		if (strlen(ment->mnt_fstype) > (size_t)FSTYPE_MAX) {
3580Sstevel@tonic-gate 			fprintf(stderr, gettext(
3596007Sthurlow 			    "%s: FSType %s exceeds %d characters\n"),
3606007Sthurlow 			    myname, ment->mnt_fstype, FSTYPE_MAX);
3610Sstevel@tonic-gate 			exit(1);
3620Sstevel@tonic-gate 		}
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 		/* build the full pathname of the fstype dependent command. */
3650Sstevel@tonic-gate 		sprintf(full_path, "%s/%s/%s", fs_path, ment->mnt_fstype,
3666007Sthurlow 		    myname);
3670Sstevel@tonic-gate 		sprintf(alter_path, "%s/%s/%s", alt_path, ment->mnt_fstype,
3686007Sthurlow 		    myname);
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 		/*
3710Sstevel@tonic-gate 		 * create the new arg list, and end the list with a
3720Sstevel@tonic-gate 		 * null pointer
3730Sstevel@tonic-gate 		 */
3740Sstevel@tonic-gate 		ii = 2;
3750Sstevel@tonic-gate 		if (oflg) {
3760Sstevel@tonic-gate 			newargv[ii++] = "-o";
3770Sstevel@tonic-gate 			newargv[ii++] = oarg;
3780Sstevel@tonic-gate 		}
3790Sstevel@tonic-gate 		if (dashflg) {
3800Sstevel@tonic-gate 			newargv[ii++] = "--";
3810Sstevel@tonic-gate 		}
3820Sstevel@tonic-gate 		if (fflg) {
3830Sstevel@tonic-gate 			newargv[ii++] = "-f";
3840Sstevel@tonic-gate 		}
3850Sstevel@tonic-gate 		newargv[ii++] = (ment->mnt_mountp)
3866007Sthurlow 		    ? ment->mnt_mountp : ment->mnt_special;
3870Sstevel@tonic-gate 		newargv[ii] = NULL;
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 		/* set the new argv[0] to the filename */
3900Sstevel@tonic-gate 		newargv[1] = myname;
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 		if (Vflg) {
3930Sstevel@tonic-gate 			printf("%s", myname);
3940Sstevel@tonic-gate 			for (ii = 2; newargv[ii]; ii++)
3950Sstevel@tonic-gate 				printf(" %s", newargv[ii]);
3960Sstevel@tonic-gate 			printf("\n");
3970Sstevel@tonic-gate 			fflush(stdout);
3980Sstevel@tonic-gate 			exit(0);
3990Sstevel@tonic-gate 		}
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 		/* Try to exec the fstype dependent umount. */
4020Sstevel@tonic-gate 		execv(full_path, &newargv[1]);
4030Sstevel@tonic-gate 		if (errno == ENOEXEC) {
4040Sstevel@tonic-gate 			newargv[0] = "sh";
4050Sstevel@tonic-gate 			newargv[1] = full_path;
4060Sstevel@tonic-gate 			execv("/sbin/sh", &newargv[0]);
4070Sstevel@tonic-gate 		}
4080Sstevel@tonic-gate 		newargv[1] = myname;
4090Sstevel@tonic-gate 		execv(alter_path, &newargv[1]);
4100Sstevel@tonic-gate 		if (errno == ENOEXEC) {
4110Sstevel@tonic-gate 			newargv[0] = "sh";
4120Sstevel@tonic-gate 			newargv[1] = alter_path;
4130Sstevel@tonic-gate 			execv("/sbin/sh", &newargv[0]);
4140Sstevel@tonic-gate 		}
4150Sstevel@tonic-gate 		/* exec failed */
4160Sstevel@tonic-gate 		if (errno != ENOENT) {
4170Sstevel@tonic-gate 			fprintf(stderr, gettext("umount: cannot execute %s\n"),
4186007Sthurlow 			    full_path);
4190Sstevel@tonic-gate 			exit(1);
4200Sstevel@tonic-gate 		}
4210Sstevel@tonic-gate 	}
4220Sstevel@tonic-gate 	/*
4230Sstevel@tonic-gate 	 * No fstype independent executable then.  We'll go generic
4240Sstevel@tonic-gate 	 * from here.
4250Sstevel@tonic-gate 	 */
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	/* don't use -o with generic */
4280Sstevel@tonic-gate 	if (oflg) {
4290Sstevel@tonic-gate 		fprintf(stderr, gettext(
4306007Sthurlow 		    "%s: %s specific umount does not exist;"
4316007Sthurlow 		    " -o suboption ignored\n"),
4326007Sthurlow 		    myname, ment->mnt_fstype ? ment->mnt_fstype : "<null>");
4330Sstevel@tonic-gate 	}
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	signal(SIGHUP,  SIG_IGN);
4360Sstevel@tonic-gate 	signal(SIGQUIT, SIG_IGN);
4370Sstevel@tonic-gate 	signal(SIGINT,  SIG_IGN);
4380Sstevel@tonic-gate 	/*
4390Sstevel@tonic-gate 	 * Try to umount the mountpoint.
4400Sstevel@tonic-gate 	 * If that fails, try the corresponding special.
4410Sstevel@tonic-gate 	 * (This ordering is necessary for nfs umounts.)
4420Sstevel@tonic-gate 	 * (for remote resources:  if the first umount returns EBUSY
4430Sstevel@tonic-gate 	 * don't call umount again - umount() with a resource name
4440Sstevel@tonic-gate 	 * will return a misleading error to the user
4450Sstevel@tonic-gate 	 */
4460Sstevel@tonic-gate 	if (fflg) {
4470Sstevel@tonic-gate 		if (((ret = umount2(ment->mnt_mountp, MS_FORCE)) < 0) &&
4486007Sthurlow 		    (errno != EBUSY && errno != ENOTSUP &&
4496007Sthurlow 		    errno != EPERM))
4500Sstevel@tonic-gate 			ret = umount2(ment->mnt_special, MS_FORCE);
4510Sstevel@tonic-gate 	} else {
4520Sstevel@tonic-gate 		if (((ret = umount2(ment->mnt_mountp, 0)) < 0) &&
4536007Sthurlow 		    (errno != EBUSY) && (errno != EPERM))
4540Sstevel@tonic-gate 			ret = umount2(ment->mnt_special, 0);
4550Sstevel@tonic-gate 	}
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	if (ret < 0) {
4580Sstevel@tonic-gate 		rpterr(ment->mnt_mountp);
4590Sstevel@tonic-gate 		if (errno != EINVAL && errno != EFAULT)
4600Sstevel@tonic-gate 			exit(1);
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 		exitcode = 1;
4630Sstevel@tonic-gate 	}
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	exit(exitcode);
4660Sstevel@tonic-gate }
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate void
469821Sdh145677 rpterr(char *sp)
4700Sstevel@tonic-gate {
4710Sstevel@tonic-gate 	switch (errno) {
4720Sstevel@tonic-gate 	case EPERM:
4730Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: permission denied\n"), myname);
4740Sstevel@tonic-gate 		break;
4750Sstevel@tonic-gate 	case ENXIO:
4760Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: %s no device\n"), myname, sp);
4770Sstevel@tonic-gate 		break;
4780Sstevel@tonic-gate 	case ENOENT:
4790Sstevel@tonic-gate 		fprintf(stderr,
4806007Sthurlow 		    gettext("%s: %s no such file or directory\n"),
4816007Sthurlow 		    myname, sp);
4820Sstevel@tonic-gate 		break;
4830Sstevel@tonic-gate 	case EINVAL:
4840Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: %s not mounted\n"), myname, sp);
4850Sstevel@tonic-gate 		break;
4860Sstevel@tonic-gate 	case EBUSY:
4870Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: %s busy\n"), myname, sp);
4880Sstevel@tonic-gate 		break;
4890Sstevel@tonic-gate 	case ENOTBLK:
4900Sstevel@tonic-gate 		fprintf(stderr,
4916007Sthurlow 		    gettext("%s: %s block device required\n"), myname, sp);
4920Sstevel@tonic-gate 		break;
4930Sstevel@tonic-gate 	case ECOMM:
4940Sstevel@tonic-gate 		fprintf(stderr,
4956007Sthurlow 		    gettext("%s: warning: broken link detected\n"), myname);
4960Sstevel@tonic-gate 		break;
4970Sstevel@tonic-gate 	default:
4980Sstevel@tonic-gate 		perror(myname);
4990Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: cannot unmount %s\n"), myname, sp);
5000Sstevel@tonic-gate 	}
5010Sstevel@tonic-gate }
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate void
504821Sdh145677 usage(void)
5050Sstevel@tonic-gate {
5060Sstevel@tonic-gate 	fprintf(stderr, gettext(
5070Sstevel@tonic-gate "Usage:\n%s [-f] [-V] [-o specific_options] {special | mount-point}\n"),
5086007Sthurlow 	    myname);
5090Sstevel@tonic-gate 	fprintf(stderr, gettext(
5100Sstevel@tonic-gate "%s -a [-f] [-V] [-o specific_options] [mount_point ...]\n"), myname);
5110Sstevel@tonic-gate 	exit(1);
5120Sstevel@tonic-gate }
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate void
515821Sdh145677 mnterror(int flag)
5160Sstevel@tonic-gate {
5170Sstevel@tonic-gate 	switch (flag) {
5180Sstevel@tonic-gate 	case MNT_TOOLONG:
5190Sstevel@tonic-gate 		fprintf(stderr,
5206007Sthurlow 		    gettext("%s: line in mnttab exceeds %d characters\n"),
5216007Sthurlow 		    myname, MNT_LINE_MAX-2);
5220Sstevel@tonic-gate 		break;
5230Sstevel@tonic-gate 	case MNT_TOOFEW:
5240Sstevel@tonic-gate 		fprintf(stderr,
5256007Sthurlow 		    gettext("%s: line in mnttab has too few entries\n"),
5266007Sthurlow 		    myname);
5270Sstevel@tonic-gate 		break;
5280Sstevel@tonic-gate 	default:
5290Sstevel@tonic-gate 		break;
5300Sstevel@tonic-gate 	}
5310Sstevel@tonic-gate }
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate /*
5340Sstevel@tonic-gate  * Search the mlist linked list for the
5350Sstevel@tonic-gate  * first match of specp or mntp.  The list is expected to be in reverse
5360Sstevel@tonic-gate  * order of /etc/mnttab.
5370Sstevel@tonic-gate  * If both are specified, then both have to match.
5380Sstevel@tonic-gate  * Returns the (mountent_t *) of the match, otherwise returns NULL.
5390Sstevel@tonic-gate  */
5400Sstevel@tonic-gate mountent_t *
5410Sstevel@tonic-gate getmntlast(mountent_t *mlist, char *specp, char *mntp)
5420Sstevel@tonic-gate {
5430Sstevel@tonic-gate 	int		mfound, sfound;
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	for (/* */; mlist; mlist = mlist->link) {
5460Sstevel@tonic-gate 		mfound = sfound = 0;
5470Sstevel@tonic-gate 		if (mntp && (strcmp(mlist->ment.mnt_mountp, mntp) == 0)) {
5480Sstevel@tonic-gate 			if (specp == NULL)
5490Sstevel@tonic-gate 				return (mlist);
5500Sstevel@tonic-gate 			mfound++;
5510Sstevel@tonic-gate 		}
5520Sstevel@tonic-gate 		if (specp && (strcmp(mlist->ment.mnt_special, specp) == 0)) {
5530Sstevel@tonic-gate 			if (mntp == NULL)
5540Sstevel@tonic-gate 				return (mlist);
5550Sstevel@tonic-gate 			sfound++;
5560Sstevel@tonic-gate 		}
5570Sstevel@tonic-gate 		if (mfound && sfound)
5580Sstevel@tonic-gate 			return (mlist);
5590Sstevel@tonic-gate 	}
5600Sstevel@tonic-gate 	return (NULL);
5610Sstevel@tonic-gate }
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate /*
5660Sstevel@tonic-gate  * Perform the parallel version of umount.  Returns 0 if no errors occurred,
5670Sstevel@tonic-gate  * non zero otherwise.
5680Sstevel@tonic-gate  */
5690Sstevel@tonic-gate int
5700Sstevel@tonic-gate parumount(char **mntlist, int count)
5710Sstevel@tonic-gate {
5720Sstevel@tonic-gate 	int 		maxfd = OPEN_MAX;
5730Sstevel@tonic-gate 	struct rlimit 	rl;
5740Sstevel@tonic-gate 	mountent_t	**mntarray, **ml, *mp;
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	/*
5770Sstevel@tonic-gate 	 * If no mount points are specified and none were found in mnttab,
5780Sstevel@tonic-gate 	 * then end it all here.
5790Sstevel@tonic-gate 	 */
5800Sstevel@tonic-gate 	if (count == 0 && mntll == NULL)
5810Sstevel@tonic-gate 		return (0);
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	/*
5840Sstevel@tonic-gate 	 * This is the process scaling section.  After running a series
5850Sstevel@tonic-gate 	 * of tests based on the number of simultaneous processes and
5860Sstevel@tonic-gate 	 * processors available, optimum performance was achieved near or
5870Sstevel@tonic-gate 	 * at (PROCN * 2).
5880Sstevel@tonic-gate 	 */
5890Sstevel@tonic-gate 	if ((maxrun = sysconf(_SC_NPROCESSORS_ONLN)) == -1)
5900Sstevel@tonic-gate 		maxrun = 4;
5910Sstevel@tonic-gate 	else
5920Sstevel@tonic-gate 		maxrun = maxrun * 2 + 1;
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
5950Sstevel@tonic-gate 		rl.rlim_cur = rl.rlim_max;
5960Sstevel@tonic-gate 		if (setrlimit(RLIMIT_NOFILE, &rl) == 0)
5970Sstevel@tonic-gate 			maxfd = (int)rl.rlim_cur;
5981914Scasper 		(void) enable_extended_FILE_stdio(-1, -1);
5990Sstevel@tonic-gate 	}
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 	/*
6020Sstevel@tonic-gate 	 * The parent needs to maintain 3 of its own fd's, plus 2 for
6030Sstevel@tonic-gate 	 * each child (the stdout and stderr pipes).
6040Sstevel@tonic-gate 	 */
6050Sstevel@tonic-gate 	maxfd = (maxfd / 2) - 6;	/* 6 takes care of temporary  */
6060Sstevel@tonic-gate 					/* periods of open fds */
6070Sstevel@tonic-gate 	if (maxfd < maxrun)
6080Sstevel@tonic-gate 		maxrun = maxfd;
6090Sstevel@tonic-gate 	if (maxrun < 4)
6100Sstevel@tonic-gate 		maxrun = 4;		/* sanity check */
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 	mntarray = make_mntarray(mntlist, count);
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	if (listlength == 0) {
6150Sstevel@tonic-gate 		if (count == 0)		/* not an error, just none found */
6160Sstevel@tonic-gate 			return (0);
6170Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: no valid entries found in %s\n"),
6186007Sthurlow 		    myname, mnttab);
6190Sstevel@tonic-gate 		return (1);
6200Sstevel@tonic-gate 	}
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	/*
6230Sstevel@tonic-gate 	 * Sort the entries based on their mount level only if lofs's are
6240Sstevel@tonic-gate 	 * not present.
6250Sstevel@tonic-gate 	 */
6260Sstevel@tonic-gate 	if (lofscnt == 0) {
6270Sstevel@tonic-gate 		qsort((void *)mntarray, listlength, sizeof (mountent_t *),
6286007Sthurlow 		    mcompar);
6290Sstevel@tonic-gate 		/*
6300Sstevel@tonic-gate 		 * If we do not detect a lofs by now, we never will.
6310Sstevel@tonic-gate 		 */
6320Sstevel@tonic-gate 		lofscnt = -1;
6330Sstevel@tonic-gate 	}
6340Sstevel@tonic-gate 	/*
6350Sstevel@tonic-gate 	 * Now link them up so that a given pid is easier to find when
6360Sstevel@tonic-gate 	 * we go to clean up after they are done.
6370Sstevel@tonic-gate 	 */
6380Sstevel@tonic-gate 	mntll = mntarray[0];
6390Sstevel@tonic-gate 	for (ml = mntarray; mp = *ml; /* */)
6400Sstevel@tonic-gate 		mp->link = *++ml;
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	/*
6430Sstevel@tonic-gate 	 * Try to handle interrupts in a reasonable way.
6440Sstevel@tonic-gate 	 */
6450Sstevel@tonic-gate 	sigset(SIGHUP, cleanup);
6460Sstevel@tonic-gate 	sigset(SIGQUIT, cleanup);
6470Sstevel@tonic-gate 	sigset(SIGINT, cleanup);
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	do_umounts(mntarray);	/* do the umounts */
6500Sstevel@tonic-gate 	return (exitcode);
6510Sstevel@tonic-gate }
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate /*
6540Sstevel@tonic-gate  * Returns a mountent_t array based on mntlist.  If mntlist is NULL, then
6550Sstevel@tonic-gate  * it returns all mnttab entries with a few exceptions.  Sets the global
6560Sstevel@tonic-gate  * variable listlength to the number of entries in the array.
6570Sstevel@tonic-gate  */
6580Sstevel@tonic-gate mountent_t **
6590Sstevel@tonic-gate make_mntarray(char **mntlist, int count)
6600Sstevel@tonic-gate {
6610Sstevel@tonic-gate 	mountent_t 	*mp, **mpp;
6620Sstevel@tonic-gate 	int 		ndx;
6630Sstevel@tonic-gate 	char		*cp;
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 	if (count > 0)
6660Sstevel@tonic-gate 		listlength = count;
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	mpp = (mountent_t **)malloc(sizeof (*mp) * (listlength + 1));
6690Sstevel@tonic-gate 	if (mpp == NULL)
6700Sstevel@tonic-gate 		nomem();
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	if (count == 0) {
6730Sstevel@tonic-gate 		if (mntll == NULL) {	/* no entries? */
6740Sstevel@tonic-gate 			listlength = 0;
6750Sstevel@tonic-gate 			return (NULL);
6760Sstevel@tonic-gate 		}
6770Sstevel@tonic-gate 		/*
6780Sstevel@tonic-gate 		 * No mount list specified: take all mnttab mount points
6790Sstevel@tonic-gate 		 * except for a few cases.
6800Sstevel@tonic-gate 		 */
6810Sstevel@tonic-gate 		for (ndx = 0, mp = mntll; mp; mp = mp->link) {
6820Sstevel@tonic-gate 			if (fsstrinlist(mp->ment.mnt_mountp, keeplist))
6830Sstevel@tonic-gate 				continue;
6840Sstevel@tonic-gate 			mp->mlevel = fsgetmlevel(mp->ment.mnt_mountp);
6850Sstevel@tonic-gate 			if (mp->ment.mnt_fstype &&
6860Sstevel@tonic-gate 			    (strcmp(mp->ment.mnt_fstype, MNTTYPE_LOFS) == 0))
6870Sstevel@tonic-gate 				lofscnt++;
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 			mpp[ndx++] = mp;
6900Sstevel@tonic-gate 		}
6910Sstevel@tonic-gate 		mpp[ndx] = NULL;
6920Sstevel@tonic-gate 		listlength = ndx;
6930Sstevel@tonic-gate 		return (mpp);
6940Sstevel@tonic-gate 	}
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	/*
6970Sstevel@tonic-gate 	 * A list of mount points was specified on the command line.
6980Sstevel@tonic-gate 	 * Build an array out of these.
6990Sstevel@tonic-gate 	 */
7000Sstevel@tonic-gate 	for (ndx = 0; count--; ) {
7010Sstevel@tonic-gate 		cp = *mntlist++;
7020Sstevel@tonic-gate 		if (realpath(cp, resolve) == NULL) {
7030Sstevel@tonic-gate 			fprintf(stderr,
7046007Sthurlow 			    gettext("%s: warning: can't resolve %s\n"),
7056007Sthurlow 			    myname, cp);
7060Sstevel@tonic-gate 			exitcode = 1;
7070Sstevel@tonic-gate 			mp = getmntlast(mntll, NULL, cp); /* try anyways */
7080Sstevel@tonic-gate 		} else
7090Sstevel@tonic-gate 			mp = getmntlast(mntll, NULL, resolve);
7100Sstevel@tonic-gate 		if (mp == NULL) {
7110Sstevel@tonic-gate 			struct mnttab mnew;
7120Sstevel@tonic-gate 			/*
7130Sstevel@tonic-gate 			 * Then we've reached the end without finding
7140Sstevel@tonic-gate 			 * what we are looking for, but we still have to
7150Sstevel@tonic-gate 			 * try to umount it: append it to mntarray.
7160Sstevel@tonic-gate 			 */
7170Sstevel@tonic-gate 			fprintf(stderr, gettext(
7186007Sthurlow 			    "%s: warning: %s not found in %s\n"),
7196007Sthurlow 			    myname, resolve, mnttab);
7200Sstevel@tonic-gate 			exitcode = 1;
7210Sstevel@tonic-gate 			mntnull(&mnew);
7220Sstevel@tonic-gate 			mnew.mnt_special = mnew.mnt_mountp = strdup(resolve);
7230Sstevel@tonic-gate 			if (mnew.mnt_special == NULL)
7240Sstevel@tonic-gate 				nomem();
7250Sstevel@tonic-gate 			mp = new_mountent(&mnew);
7260Sstevel@tonic-gate 		}
7270Sstevel@tonic-gate 		if (mp->ment.mnt_fstype &&
7280Sstevel@tonic-gate 		    (strcmp(mp->ment.mnt_fstype, MNTTYPE_LOFS) == 0))
7290Sstevel@tonic-gate 			lofscnt++;
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 		mp->mlevel = fsgetmlevel(mp->ment.mnt_mountp);
7320Sstevel@tonic-gate 		mpp[ndx++] = mp;
7330Sstevel@tonic-gate 	}
7340Sstevel@tonic-gate 	mpp[ndx] = NULL;
7350Sstevel@tonic-gate 	listlength = ndx;
7360Sstevel@tonic-gate 	return (mpp);
7370Sstevel@tonic-gate }
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate /*
7400Sstevel@tonic-gate  * Returns the tail of a linked list of all mnttab entries.  I.e, it's faster
7410Sstevel@tonic-gate  * to return the mnttab in reverse order.
7420Sstevel@tonic-gate  * Sets listlength to the number of entries in the list.
7430Sstevel@tonic-gate  * Returns NULL if none are found.
7440Sstevel@tonic-gate  */
7450Sstevel@tonic-gate mountent_t *
746821Sdh145677 getmntall(void)
7470Sstevel@tonic-gate {
7480Sstevel@tonic-gate 	FILE		*fp;
7490Sstevel@tonic-gate 	mountent_t	*mtail;
7500Sstevel@tonic-gate 	int		cnt = 0, ret;
7510Sstevel@tonic-gate 	struct mnttab	mget;
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	if ((fp = fopen(mnttab, "r")) == NULL) {
7540Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: warning cannot open %s\n"),
7556007Sthurlow 		    myname, mnttab);
7560Sstevel@tonic-gate 		return (0);
7570Sstevel@tonic-gate 	}
7580Sstevel@tonic-gate 	mtail = NULL;
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 	while ((ret = getmntent(fp, &mget)) != -1) {
7610Sstevel@tonic-gate 		mountent_t	*mp;
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 		if (ret > 0) {
7640Sstevel@tonic-gate 			mnterror(ret);
7650Sstevel@tonic-gate 			continue;
7660Sstevel@tonic-gate 		}
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 		mp = new_mountent(&mget);
7690Sstevel@tonic-gate 		mp->link = mtail;
7700Sstevel@tonic-gate 		mtail = mp;
7710Sstevel@tonic-gate 		cnt++;
7720Sstevel@tonic-gate 	}
7730Sstevel@tonic-gate 	fclose(fp);
7740Sstevel@tonic-gate 	if (mtail == NULL) {
7750Sstevel@tonic-gate 		listlength = 0;
7760Sstevel@tonic-gate 		return (NULL);
7770Sstevel@tonic-gate 	}
7780Sstevel@tonic-gate 	listlength = cnt;
7790Sstevel@tonic-gate 	return (mtail);
7800Sstevel@tonic-gate }
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate void
7830Sstevel@tonic-gate do_umounts(mountent_t **mntarray)
7840Sstevel@tonic-gate {
7850Sstevel@tonic-gate 	mountent_t *mp, *mpprev, **ml = mntarray;
7860Sstevel@tonic-gate 	int	cnt = listlength;
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 	/*
7890Sstevel@tonic-gate 	 * Main loop for the forked children:
7900Sstevel@tonic-gate 	 */
7910Sstevel@tonic-gate 	for (mpprev = *ml; mp = *ml; mpprev = mp, ml++, cnt--) {
7920Sstevel@tonic-gate 		pid_t	pid;
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 		/*
7950Sstevel@tonic-gate 		 * Check to see if we cross a mount level: e.g.,
7960Sstevel@tonic-gate 		 * /a/b/c -> /a/b.  If so, we need to wait for all current
7970Sstevel@tonic-gate 		 * umounts to finish before umounting the rest.
7980Sstevel@tonic-gate 		 *
7990Sstevel@tonic-gate 		 * Also, we unmount serially as long as there are lofs's
8000Sstevel@tonic-gate 		 * to mount to avoid improper umount ordering.
8010Sstevel@tonic-gate 		 */
8020Sstevel@tonic-gate 		if (mp->mlevel < mpprev->mlevel || lofscnt > 0)
8030Sstevel@tonic-gate 			while (nrun > 0 && (dowait() != -1))
8040Sstevel@tonic-gate 				;
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 		if (lofscnt == 0) {
8070Sstevel@tonic-gate 			/*
8080Sstevel@tonic-gate 			 * We can now go to parallel umounting.
8090Sstevel@tonic-gate 			 */
8100Sstevel@tonic-gate 			qsort((void *)ml, cnt, sizeof (mountent_t *), mcompar);
8110Sstevel@tonic-gate 			mp = *ml;	/* possible first entry */
8120Sstevel@tonic-gate 			lofscnt--;	/* so we don't do this again */
8130Sstevel@tonic-gate 		}
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 		while (setup_iopipe(mp) == -1 && (dowait() != -1))
8160Sstevel@tonic-gate 			;
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 		while (nrun >= maxrun && (dowait() != -1))	/* throttle */
8190Sstevel@tonic-gate 			;
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate #ifdef CACHEFS_BUG
8220Sstevel@tonic-gate 		/*
8230Sstevel@tonic-gate 		 * If this is the back file system, then let cachefs/umount
8240Sstevel@tonic-gate 		 * unmount it.
8250Sstevel@tonic-gate 		 */
8260Sstevel@tonic-gate 		if (strstr(mp->ment.mnt_mountp, BACKMNT_NAME))
8270Sstevel@tonic-gate 			continue;
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 		if (mp->ment.mnt_fstype &&
8310Sstevel@tonic-gate 		    (strcmp(mp->ment.mnt_fstype, "cachefs") == 0)) {
8320Sstevel@tonic-gate 			while (cachefs_running && (dowait() != -1))
8330Sstevel@tonic-gate 					;
8340Sstevel@tonic-gate 			cachefs_running = 1;
8350Sstevel@tonic-gate 		}
8360Sstevel@tonic-gate #endif
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 		if ((pid = fork()) == -1) {
8390Sstevel@tonic-gate 			perror("fork");
8400Sstevel@tonic-gate 			cleanup(-1);
8410Sstevel@tonic-gate 			/* not reached */
8420Sstevel@tonic-gate 		}
8430Sstevel@tonic-gate #ifdef DEBUG
8440Sstevel@tonic-gate 		if (dflg && pid > 0) {
8450Sstevel@tonic-gate 			fprintf(stderr, "parent %d: umounting %d %s\n",
8466007Sthurlow 			    getpid(), pid, mp->ment.mnt_mountp);
8470Sstevel@tonic-gate 		}
8480Sstevel@tonic-gate #endif
8490Sstevel@tonic-gate 		if (pid == 0) {		/* child */
8500Sstevel@tonic-gate 			signal(SIGHUP, SIG_IGN);
8510Sstevel@tonic-gate 			signal(SIGQUIT, SIG_IGN);
8520Sstevel@tonic-gate 			signal(SIGINT, SIG_IGN);
8530Sstevel@tonic-gate 			setup_output(mp);
8540Sstevel@tonic-gate 			doexec(&mp->ment);
8550Sstevel@tonic-gate 			perror("exec");
8560Sstevel@tonic-gate 			exit(1);
8570Sstevel@tonic-gate 		}
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 		/* parent */
8600Sstevel@tonic-gate 		(void) close(mp->sopipe[WRPIPE]);
8610Sstevel@tonic-gate 		(void) close(mp->sepipe[WRPIPE]);
8620Sstevel@tonic-gate 		mp->pid = pid;
8630Sstevel@tonic-gate 		nrun++;
8640Sstevel@tonic-gate 	}
8650Sstevel@tonic-gate 	cleanup(0);
8660Sstevel@tonic-gate }
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate /*
8690Sstevel@tonic-gate  * cleanup the existing children and exit with an error
8700Sstevel@tonic-gate  * if asig != 0.
8710Sstevel@tonic-gate  */
8720Sstevel@tonic-gate void
8730Sstevel@tonic-gate cleanup(int asig)
8740Sstevel@tonic-gate {
8750Sstevel@tonic-gate 	/*
8760Sstevel@tonic-gate 	 * Let the stragglers finish.
8770Sstevel@tonic-gate 	 */
8780Sstevel@tonic-gate 	while (nrun > 0 && (dowait() != -1))
8790Sstevel@tonic-gate 		;
8800Sstevel@tonic-gate 	if (asig != 0)
8810Sstevel@tonic-gate 		exit(1);
8820Sstevel@tonic-gate }
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate /*
8860Sstevel@tonic-gate  * Waits for 1 child to die.
8870Sstevel@tonic-gate  *
8880Sstevel@tonic-gate  * Returns -1 if no children are left to wait for.
8890Sstevel@tonic-gate  * Returns 0 if a child died without an error.
8900Sstevel@tonic-gate  * Returns 1 if a child died with an error.
8910Sstevel@tonic-gate  * Sets the global exitcode if an error occurred.
8920Sstevel@tonic-gate  */
8930Sstevel@tonic-gate int
894821Sdh145677 dowait(void)
8950Sstevel@tonic-gate {
8960Sstevel@tonic-gate 	int		wstat, child, ret;
8970Sstevel@tonic-gate 	mountent_t 	*mp, *prevp;
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 	if ((child = wait(&wstat)) == -1)
9000Sstevel@tonic-gate 		return (-1);
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 	if (WIFEXITED(wstat))		/* this should always be true */
9030Sstevel@tonic-gate 		ret = WEXITSTATUS(wstat);
9040Sstevel@tonic-gate 	else
9050Sstevel@tonic-gate 		ret = 1;		/* assume some kind of error */
9060Sstevel@tonic-gate 	nrun--;
9070Sstevel@tonic-gate 	if (ret)
9080Sstevel@tonic-gate 		exitcode = 1;
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	/*
9110Sstevel@tonic-gate 	 * Find our child so we can process its std output, if any.
9120Sstevel@tonic-gate 	 * This search gets smaller and smaller as children are cleaned
9130Sstevel@tonic-gate 	 * up.
9140Sstevel@tonic-gate 	 */
9150Sstevel@tonic-gate 	for (prevp = NULL, mp = mntll; mp; mp = mp->link) {
9160Sstevel@tonic-gate 		if (mp->pid != child) {
9170Sstevel@tonic-gate 			prevp = mp;
9180Sstevel@tonic-gate 			continue;
9190Sstevel@tonic-gate 		}
9200Sstevel@tonic-gate 		/*
9210Sstevel@tonic-gate 		 * Found: let's remove it from this list.
9220Sstevel@tonic-gate 		 */
9230Sstevel@tonic-gate 		if (prevp) {
9240Sstevel@tonic-gate 			prevp->link = mp->link;
9250Sstevel@tonic-gate 			mp->link = NULL;
9260Sstevel@tonic-gate 		}
9270Sstevel@tonic-gate 		break;
9280Sstevel@tonic-gate 	}
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate 	if (mp == NULL) {
9310Sstevel@tonic-gate 		/*
9320Sstevel@tonic-gate 		 * This should never happen.
9330Sstevel@tonic-gate 		 */
9340Sstevel@tonic-gate #ifdef DEBUG
9350Sstevel@tonic-gate 		fprintf(stderr, gettext(
9366007Sthurlow 		    "%s: unknown child %d\n"), myname, child);
9370Sstevel@tonic-gate #endif
9380Sstevel@tonic-gate 		exitcode = 1;
9390Sstevel@tonic-gate 		return (1);
9400Sstevel@tonic-gate 	}
9410Sstevel@tonic-gate 	doio(mp);	/* Any output? */
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate 	if (mp->ment.mnt_fstype &&
9440Sstevel@tonic-gate 	    (strcmp(mp->ment.mnt_fstype, MNTTYPE_LOFS) == 0))
9450Sstevel@tonic-gate 		lofscnt--;
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate #ifdef CACHEFS_BUG
9480Sstevel@tonic-gate 	if (mp->ment.mnt_fstype &&
9490Sstevel@tonic-gate 	    (strcmp(mp->ment.mnt_fstype, "cachefs") == 0))
9500Sstevel@tonic-gate 		cachefs_running = 0;
9510Sstevel@tonic-gate #endif
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 	return (ret);
9540Sstevel@tonic-gate }
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate static const mountent_t zmount = { 0 };
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate mountent_t *
9590Sstevel@tonic-gate new_mountent(struct mnttab *ment)
9600Sstevel@tonic-gate {
9610Sstevel@tonic-gate 	mountent_t *new;
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate 	new = (mountent_t *)malloc(sizeof (*new));
9640Sstevel@tonic-gate 	if (new == NULL)
9650Sstevel@tonic-gate 		nomem();
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 	*new = zmount;
9680Sstevel@tonic-gate 	if (ment->mnt_special &&
9690Sstevel@tonic-gate 	    (new->ment.mnt_special = strdup(ment->mnt_special)) == NULL)
9700Sstevel@tonic-gate 		nomem();
9710Sstevel@tonic-gate 	if (ment->mnt_mountp &&
9720Sstevel@tonic-gate 	    (new->ment.mnt_mountp = strdup(ment->mnt_mountp)) == NULL)
9730Sstevel@tonic-gate 		nomem();
9740Sstevel@tonic-gate 	if (ment->mnt_fstype &&
9750Sstevel@tonic-gate 	    (new->ment.mnt_fstype = strdup(ment->mnt_fstype)) == NULL)
9760Sstevel@tonic-gate 		nomem();
9770Sstevel@tonic-gate 	return (new);
9780Sstevel@tonic-gate }
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate /*
9820Sstevel@tonic-gate  * Sort in descending order of "mount level".  For example, /a/b/c is
9830Sstevel@tonic-gate  * placed before /a/b .
9840Sstevel@tonic-gate  */
9850Sstevel@tonic-gate int
9860Sstevel@tonic-gate mcompar(const void *a, const void *b)
9870Sstevel@tonic-gate {
9880Sstevel@tonic-gate 	mountent_t *a1, *b1;
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate 	a1 = *(mountent_t **)a;
9910Sstevel@tonic-gate 	b1 = *(mountent_t **)b;
9920Sstevel@tonic-gate 	return (b1->mlevel - a1->mlevel);
9930Sstevel@tonic-gate }
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate /*
9960Sstevel@tonic-gate  * The purpose of this routine is to form stdout and stderr
9970Sstevel@tonic-gate  * pipes for the children's output.  The parent then reads and writes it
9980Sstevel@tonic-gate  * out it serially in order to ensure that the output is
9990Sstevel@tonic-gate  * not garbled.
10000Sstevel@tonic-gate  */
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate int
10030Sstevel@tonic-gate setup_iopipe(mountent_t *mp)
10040Sstevel@tonic-gate {
10050Sstevel@tonic-gate 	/*
10060Sstevel@tonic-gate 	 * Make a stdout and stderr pipe.  This should never fail.
10070Sstevel@tonic-gate 	 */
10080Sstevel@tonic-gate 	if (pipe(mp->sopipe) == -1)
10090Sstevel@tonic-gate 		return (-1);
10100Sstevel@tonic-gate 	if (pipe(mp->sepipe) == -1) {
10110Sstevel@tonic-gate 		(void) close(mp->sopipe[RDPIPE]);
10120Sstevel@tonic-gate 		(void) close(mp->sopipe[WRPIPE]);
10130Sstevel@tonic-gate 		return (-1);
10140Sstevel@tonic-gate 	}
10150Sstevel@tonic-gate 	/*
10160Sstevel@tonic-gate 	 * Don't block on an empty pipe.
10170Sstevel@tonic-gate 	 */
10180Sstevel@tonic-gate 	(void) fcntl(mp->sopipe[RDPIPE], F_SETFL, O_NDELAY|O_NONBLOCK);
10190Sstevel@tonic-gate 	(void) fcntl(mp->sepipe[RDPIPE], F_SETFL, O_NDELAY|O_NONBLOCK);
10200Sstevel@tonic-gate 	return (0);
10210Sstevel@tonic-gate }
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate /*
10240Sstevel@tonic-gate  * Called by a child to attach its stdout and stderr to the write side of
10250Sstevel@tonic-gate  * the pipes.
10260Sstevel@tonic-gate  */
10270Sstevel@tonic-gate void
10280Sstevel@tonic-gate setup_output(mountent_t *mp)
10290Sstevel@tonic-gate {
10300Sstevel@tonic-gate 	(void) close(fileno(stdout));
10310Sstevel@tonic-gate 	(void) dup(mp->sopipe[WRPIPE]);
10320Sstevel@tonic-gate 	(void) close(mp->sopipe[WRPIPE]);
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate 	(void) close(fileno(stderr));
10350Sstevel@tonic-gate 	(void) dup(mp->sepipe[WRPIPE]);
10360Sstevel@tonic-gate 	(void) close(mp->sepipe[WRPIPE]);
10370Sstevel@tonic-gate }
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate /*
10400Sstevel@tonic-gate  * Parent uses this to print any stdout or stderr output issued by
10410Sstevel@tonic-gate  * the child.
10420Sstevel@tonic-gate  */
10430Sstevel@tonic-gate static void
10440Sstevel@tonic-gate doio(mountent_t *mp)
10450Sstevel@tonic-gate {
10460Sstevel@tonic-gate 	int bytes;
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 	while ((bytes = read(mp->sepipe[RDPIPE], ibuf, sizeof (ibuf))) > 0)
10490Sstevel@tonic-gate 		write(fileno(stderr), ibuf, bytes);
10500Sstevel@tonic-gate 	while ((bytes = read(mp->sopipe[RDPIPE], ibuf, sizeof (ibuf))) > 0)
10510Sstevel@tonic-gate 		write(fileno(stdout), ibuf, bytes);
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 	(void) close(mp->sopipe[RDPIPE]);
10540Sstevel@tonic-gate 	(void) close(mp->sepipe[RDPIPE]);
10550Sstevel@tonic-gate }
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate void
1058821Sdh145677 nomem(void)
10590Sstevel@tonic-gate {
10600Sstevel@tonic-gate 	fprintf(stderr, gettext("%s: out of memory\n"), myname);
10610Sstevel@tonic-gate 	/*
10620Sstevel@tonic-gate 	 * Let the stragglers finish.
10630Sstevel@tonic-gate 	 */
10640Sstevel@tonic-gate 	while (nrun > 0 && (dowait() != -1))
10650Sstevel@tonic-gate 		;
10660Sstevel@tonic-gate 	exit(1);
10670Sstevel@tonic-gate }
1068