xref: /titanic_44/usr/src/cmd/fs.d/umount.c (revision fb3fb4f3d76d55b64440afd0af72775dfad3bd1d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include	<stdio.h>
34 #include	<limits.h>
35 #include	<unistd.h>
36 #include	<stdlib.h>
37 #include	<string.h>
38 #include	<sys/signal.h>
39 #include	<sys/mnttab.h>
40 #include	<errno.h>
41 #include	<sys/types.h>
42 #include	<sys/stat.h>
43 #include	<sys/param.h>
44 #include	<sys/wait.h>
45 #include	<sys/vfstab.h>
46 #include	<sys/fcntl.h>
47 #include	<sys/resource.h>
48 #include	<sys/mntent.h>
49 #include	<sys/ctfs.h>
50 #include	<locale.h>
51 #include	<stdarg.h>
52 #include	<sys/mount.h>
53 #include	<sys/objfs.h>
54 #include	"fslib.h"
55 
56 #define	FS_PATH		"/usr/lib/fs"
57 #define	ALT_PATH	"/etc/fs"
58 #define	FULLPATH_MAX	32
59 #define	FSTYPE_MAX	8
60 #define	ARGV_MAX	16
61 
62 int	aflg, oflg, Vflg, dashflg, dflg, fflg;
63 
64 extern void	rpterr(), usage(), mnterror();
65 
66 extern	char	*optarg;	/* used by getopt */
67 extern	int	optind, opterr;
68 
69 static char	*myname;
70 char	fs_path[] = FS_PATH;
71 char	alt_path[] = ALT_PATH;
72 char	mnttab[MAXPATHLEN + 1];
73 char	*oarg, *farg;
74 int	maxrun, nrun;
75 int	no_mnttab;
76 int	lofscnt;		/* presence of lofs prohibits parallel */
77 				/* umounting */
78 int	exitcode;
79 char	resolve[MAXPATHLEN];
80 static  char ibuf[BUFSIZ];
81 
82 /*
83  * Currently, mounting cachefs's simultaneous uncovers various problems.
84  * For the short term, we serialize cachefs activity while we fix
85  * these cachefs bugs.
86  */
87 #define	CACHEFS_BUG
88 #ifdef	CACHEFS_BUG
89 #include	<sys/fs/cachefs_fs.h>	/* for BACKMNT_NAME */
90 int	cachefs_running;	/* parallel cachefs not supported yet */
91 #endif
92 
93 /*
94  * The basic mount struct that describes an mnttab entry.
95  * It is used both in an array and as a linked list elem.
96  */
97 
98 typedef struct mountent {
99 	struct mnttab	ment;		/* the mnttab data */
100 	int		mlevel;		/* mount level of the mount pt */
101 	pid_t		pid;		/* the pid of this mount process */
102 #define	RDPIPE		0
103 #define	WRPIPE		1
104 	int		sopipe[2];	/* pipe attached to child's stdout */
105 	int		sepipe[2];	/* pipe attached to child's stderr */
106 	struct mountent *link;		/* used when in linked list */
107 } mountent_t;
108 
109 static mountent_t	*mntll;		/* head of global linked list of */
110 					/* mountents */
111 int			listlength;	/* # of elems in this list */
112 
113 /*
114  * If the automatic flag (-a) is given and mount points are not specified
115  * on the command line, then do not attempt to umount these.  These
116  * generally need to be kept mounted until system shutdown.
117  */
118 static const char   *keeplist[] = {
119 	"/",
120 	"/dev",
121 	"/dev/fd",
122 	"/devices",
123 	"/etc/mnttab",
124 	"/etc/svc/volatile",
125 	"/lib",
126 	"/proc",
127 	"/sbin",
128 	CTFS_ROOT,
129 	OBJFS_ROOT,
130 	"/tmp",
131 	"/usr",
132 	"/var",
133 	"/var/adm",
134 	"/var/run",
135 	NULL
136 };
137 
138 static void	nomem();
139 static void	doexec(struct mnttab *);
140 static int	setup_iopipe(mountent_t *);
141 static void	setup_output(mountent_t *);
142 static void	doio(mountent_t *);
143 static void	do_umounts(mountent_t **);
144 static int	dowait();
145 static int	parumount();
146 static int	mcompar(const void *, const void *);
147 static void	cleanup(int);
148 
149 static mountent_t	**make_mntarray(char **, int);
150 static mountent_t	*getmntall();
151 static mountent_t 	*new_mountent(struct mnttab *);
152 static mountent_t	*getmntlast(mountent_t *, char *, char *);
153 
154 int
155 main(int argc, char **argv)
156 {
157 	int 	cc;
158 	struct mnttab  mget;
159 	char 	*mname, *is_special;
160 	int	fscnt;
161 	mountent_t	*mp;
162 
163 	(void) setlocale(LC_ALL, "");
164 
165 #if !defined(TEXT_DOMAIN)
166 #define	TEXT_DOMAIN "SYS_TEST"
167 #endif
168 	(void) textdomain(TEXT_DOMAIN);
169 
170 	myname = strrchr(argv[0], '/');
171 	if (myname)
172 		myname++;
173 	else
174 		myname = argv[0];
175 
176 	/*
177 	 * Process the args.
178 	 * "-d" for compatibility
179 	 */
180 	while ((cc = getopt(argc, argv, "ado:Vf?")) != -1)
181 		switch (cc) {
182 		case 'a':
183 			aflg++;
184 			break;
185 #ifdef DEBUG
186 		case 'd':
187 			dflg++;
188 			break;
189 #endif
190 
191 		case '?':
192 			usage();
193 			break;
194 		case 'o':
195 			if (oflg)
196 				usage();
197 			else {
198 				oflg++;
199 				oarg = optarg;
200 			}
201 			break;
202 		case 'f':
203 			fflg++;
204 			break;
205 		case 'V':
206 			if (Vflg)
207 				usage();
208 			else
209 				Vflg++;
210 			break;
211 		default:
212 			usage();
213 			break;
214 		}
215 
216 	fscnt = argc - optind;
217 	if (!aflg && fscnt != 1)
218 		usage();
219 
220 	/* copy '--' to specific */
221 	if (strcmp(argv[optind-1], "--") == 0)
222 		dashflg++;
223 
224 	/*
225 	 * mnttab may be a symlink to a file in another file system.
226 	 * This happens during install when / is mounted read-only
227 	 * and /etc/mnttab is symlinked to a file in /tmp.
228 	 * If this is the case, we need to follow the symlink to the
229 	 * read-write file itself so that the subsequent mnttab.temp
230 	 * open and rename will work.
231 	 */
232 	if (realpath(MNTTAB, mnttab) == NULL) {
233 		strcpy(mnttab, MNTTAB);
234 	}
235 
236 	/*
237 	 * bugid 1205242
238 	 * call the realpath() here, so that if the user is
239 	 * trying to umount an autofs directory, the directory
240 	 * is forced to mount.
241 	 */
242 
243 	mname = argv[optind];
244 	is_special = realpath(mname, resolve);
245 
246 	/*
247 	 * Read the whole mnttab into memory.
248 	 */
249 	mntll = getmntall();
250 
251 	if (aflg && fscnt != 1)
252 		exit(parumount(argv + optind, fscnt));
253 
254 	aflg = 0;
255 
256 	mntnull(&mget);
257 	if (listlength == 0) {
258 		fprintf(stderr, gettext(
259 			"%s: warning: no entries found in %s\n"),
260 				myname, mnttab);
261 		mget.mnt_mountp = mname;	/* assume mount point */
262 		no_mnttab++;
263 		doexec(&mget);
264 		exit(0);
265 	}
266 
267 	mp = NULL;
268 
269 	/*
270 	 * if realpath fails, it can't be a mount point, so we'll
271 	 * go straight to the code that treats the arg as a special.
272 	 * if realpath succeeds, it could be a special or a mount point;
273 	 * we'll start by assuming it's a mount point, and if it's not,
274 	 * try to treat it as a special.
275 	 */
276 	if (is_special != NULL) {
277 		/*
278 		 * if this succeeds,
279 		 * we'll have the appropriate record; if it fails
280 		 * we'll assume the arg is a special of some sort
281 		 */
282 		mp = getmntlast(mntll, NULL, resolve);
283 	}
284 	/*
285 	 * Since stackable mount is allowed (RFE 2001535),
286 	 * we will un-mount the last entry in the MNTTAB that matches.
287 	 */
288 	if (mp == NULL) {
289 		/*
290 		 * Perhaps there is a bogus mnttab entry that
291 		 * can't be resolved:
292 		 */
293 		if ((mp = getmntlast(mntll, NULL, mname)) == NULL)
294 			/*
295 			 * assume it's a device (special) now
296 			 */
297 			mp = getmntlast(mntll, mname, NULL);
298 		if (mp) {
299 			/*
300 			 * Found it.
301 			 * This is a device. Now we want to know if
302 			 * it stackmounted on by something else.
303 			 * The original fix for bug 1103850 has a
304 			 * problem with lockfs (bug 1119731). This
305 			 * is a revised method.
306 			 */
307 			mountent_t *lmp;
308 			lmp = getmntlast(mntll, NULL, mp->ment.mnt_mountp);
309 
310 			if (lmp && strcmp(lmp->ment.mnt_special,
311 					mp->ment.mnt_special)) {
312 				errno = EBUSY;
313 				rpterr(mname);
314 				exit(1);
315 			}
316 		} else {
317 			fprintf(stderr, gettext(
318 				"%s: warning: %s not in mnttab\n"),
319 				myname, mname);
320 			if (Vflg)
321 				exit(1);
322 				/*
323 				 * same error as mount -V
324 				 * would give for unknown
325 				 * mount point
326 				 */
327 			mget.mnt_special = mget.mnt_mountp = mname;
328 		}
329 	}
330 
331 	if (mp)
332 		doexec(&mp->ment);
333 	else
334 		doexec(&mget);
335 
336 	return (0);
337 }
338 
339 void
340 doexec(struct mnttab *ment)
341 {
342 	int 	ret;
343 
344 #ifdef DEBUG
345 	if (dflg)
346 		fprintf(stderr, "%d: umounting %s\n",
347 			getpid(), ment->mnt_mountp);
348 #endif
349 
350 	/* try to exec the dependent portion */
351 	if ((ment->mnt_fstype != NULL) || Vflg) {
352 		char	full_path[FULLPATH_MAX];
353 		char	alter_path[FULLPATH_MAX];
354 		char	*newargv[ARGV_MAX];
355 		int 	ii;
356 
357 		if (strlen(ment->mnt_fstype) > (size_t)FSTYPE_MAX) {
358 			fprintf(stderr, gettext(
359 				"%s: FSType %s exceeds %d characters\n"),
360 				myname, ment->mnt_fstype, FSTYPE_MAX);
361 			exit(1);
362 		}
363 
364 		/* build the full pathname of the fstype dependent command. */
365 		sprintf(full_path, "%s/%s/%s", fs_path, ment->mnt_fstype,
366 					myname);
367 		sprintf(alter_path, "%s/%s/%s", alt_path, ment->mnt_fstype,
368 					myname);
369 
370 		/*
371 		 * create the new arg list, and end the list with a
372 		 * null pointer
373 		 */
374 		ii = 2;
375 		if (oflg) {
376 			newargv[ii++] = "-o";
377 			newargv[ii++] = oarg;
378 		}
379 		if (dashflg) {
380 			newargv[ii++] = "--";
381 		}
382 		if (fflg) {
383 			newargv[ii++] = "-f";
384 		}
385 		newargv[ii++] = (ment->mnt_mountp)
386 				? ment->mnt_mountp : ment->mnt_special;
387 		newargv[ii] = NULL;
388 
389 		/* set the new argv[0] to the filename */
390 		newargv[1] = myname;
391 
392 		if (Vflg) {
393 			printf("%s", myname);
394 			for (ii = 2; newargv[ii]; ii++)
395 				printf(" %s", newargv[ii]);
396 			printf("\n");
397 			fflush(stdout);
398 			exit(0);
399 		}
400 
401 		/* Try to exec the fstype dependent umount. */
402 		execv(full_path, &newargv[1]);
403 		if (errno == ENOEXEC) {
404 			newargv[0] = "sh";
405 			newargv[1] = full_path;
406 			execv("/sbin/sh", &newargv[0]);
407 		}
408 		newargv[1] = myname;
409 		execv(alter_path, &newargv[1]);
410 		if (errno == ENOEXEC) {
411 			newargv[0] = "sh";
412 			newargv[1] = alter_path;
413 			execv("/sbin/sh", &newargv[0]);
414 		}
415 		/* exec failed */
416 		if (errno != ENOENT) {
417 			fprintf(stderr, gettext("umount: cannot execute %s\n"),
418 					full_path);
419 			exit(1);
420 		}
421 	}
422 	/*
423 	 * No fstype independent executable then.  We'll go generic
424 	 * from here.
425 	 */
426 
427 	/* don't use -o with generic */
428 	if (oflg) {
429 		fprintf(stderr, gettext(
430 	"%s: %s specific umount does not exist; -o suboption ignored\n"),
431 		myname, ment->mnt_fstype ? ment->mnt_fstype : "<null>");
432 	}
433 
434 	signal(SIGHUP,  SIG_IGN);
435 	signal(SIGQUIT, SIG_IGN);
436 	signal(SIGINT,  SIG_IGN);
437 	/*
438 	 * Try to umount the mountpoint.
439 	 * If that fails, try the corresponding special.
440 	 * (This ordering is necessary for nfs umounts.)
441 	 * (for remote resources:  if the first umount returns EBUSY
442 	 * don't call umount again - umount() with a resource name
443 	 * will return a misleading error to the user
444 	 */
445 	if (fflg) {
446 		if (((ret = umount2(ment->mnt_mountp, MS_FORCE)) < 0) &&
447 				(errno != EBUSY && errno != ENOTSUP &&
448 				errno != EPERM))
449 			ret = umount2(ment->mnt_special, MS_FORCE);
450 	} else {
451 		if (((ret = umount2(ment->mnt_mountp, 0)) < 0) &&
452 				(errno != EBUSY) && (errno != EPERM))
453 			ret = umount2(ment->mnt_special, 0);
454 	}
455 
456 	if (ret < 0) {
457 		rpterr(ment->mnt_mountp);
458 		if (errno != EINVAL && errno != EFAULT)
459 			exit(1);
460 
461 		exitcode = 1;
462 	}
463 
464 	exit(exitcode);
465 }
466 
467 void
468 rpterr(char *sp)
469 {
470 	switch (errno) {
471 	case EPERM:
472 		fprintf(stderr, gettext("%s: permission denied\n"), myname);
473 		break;
474 	case ENXIO:
475 		fprintf(stderr, gettext("%s: %s no device\n"), myname, sp);
476 		break;
477 	case ENOENT:
478 		fprintf(stderr,
479 			gettext("%s: %s no such file or directory\n"),
480 			myname, sp);
481 		break;
482 	case EINVAL:
483 		fprintf(stderr, gettext("%s: %s not mounted\n"), myname, sp);
484 		break;
485 	case EBUSY:
486 		fprintf(stderr, gettext("%s: %s busy\n"), myname, sp);
487 		break;
488 	case ENOTBLK:
489 		fprintf(stderr,
490 			gettext("%s: %s block device required\n"), myname, sp);
491 		break;
492 	case ECOMM:
493 		fprintf(stderr,
494 			gettext("%s: warning: broken link detected\n"), myname);
495 		break;
496 	default:
497 		perror(myname);
498 		fprintf(stderr, gettext("%s: cannot unmount %s\n"), myname, sp);
499 	}
500 }
501 
502 void
503 usage(void)
504 {
505 	fprintf(stderr, gettext(
506 "Usage:\n%s [-f] [-V] [-o specific_options] {special | mount-point}\n"),
507 		myname);
508 	fprintf(stderr, gettext(
509 "%s -a [-f] [-V] [-o specific_options] [mount_point ...]\n"), myname);
510 	exit(1);
511 }
512 
513 void
514 mnterror(int flag)
515 {
516 	switch (flag) {
517 	case MNT_TOOLONG:
518 		fprintf(stderr,
519 			gettext("%s: line in mnttab exceeds %d characters\n"),
520 			myname, MNT_LINE_MAX-2);
521 		break;
522 	case MNT_TOOFEW:
523 		fprintf(stderr,
524 			gettext("%s: line in mnttab has too few entries\n"),
525 			myname);
526 		break;
527 	default:
528 		break;
529 	}
530 }
531 
532 /*
533  * Search the mlist linked list for the
534  * first match of specp or mntp.  The list is expected to be in reverse
535  * order of /etc/mnttab.
536  * If both are specified, then both have to match.
537  * Returns the (mountent_t *) of the match, otherwise returns NULL.
538  */
539 mountent_t *
540 getmntlast(mountent_t *mlist, char *specp, char *mntp)
541 {
542 	int		mfound, sfound;
543 
544 	for (/* */; mlist; mlist = mlist->link) {
545 		mfound = sfound = 0;
546 		if (mntp && (strcmp(mlist->ment.mnt_mountp, mntp) == 0)) {
547 			if (specp == NULL)
548 				return (mlist);
549 			mfound++;
550 		}
551 		if (specp && (strcmp(mlist->ment.mnt_special, specp) == 0)) {
552 			if (mntp == NULL)
553 				return (mlist);
554 			sfound++;
555 		}
556 		if (mfound && sfound)
557 			return (mlist);
558 	}
559 	return (NULL);
560 }
561 
562 
563 
564 /*
565  * Perform the parallel version of umount.  Returns 0 if no errors occurred,
566  * non zero otherwise.
567  */
568 int
569 parumount(char **mntlist, int count)
570 {
571 	int 		maxfd = OPEN_MAX;
572 	struct rlimit 	rl;
573 	mountent_t	**mntarray, **ml, *mp;
574 
575 	/*
576 	 * If no mount points are specified and none were found in mnttab,
577 	 * then end it all here.
578 	 */
579 	if (count == 0 && mntll == NULL)
580 		return (0);
581 
582 	/*
583 	 * This is the process scaling section.  After running a series
584 	 * of tests based on the number of simultaneous processes and
585 	 * processors available, optimum performance was achieved near or
586 	 * at (PROCN * 2).
587 	 */
588 	if ((maxrun = sysconf(_SC_NPROCESSORS_ONLN)) == -1)
589 		maxrun = 4;
590 	else
591 		maxrun = maxrun * 2 + 1;
592 
593 	if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
594 		rl.rlim_cur = rl.rlim_max;
595 		if (setrlimit(RLIMIT_NOFILE, &rl) == 0)
596 			maxfd = (int)rl.rlim_cur;
597 	}
598 
599 	/*
600 	 * The parent needs to maintain 3 of its own fd's, plus 2 for
601 	 * each child (the stdout and stderr pipes).
602 	 */
603 	maxfd = (maxfd / 2) - 6;	/* 6 takes care of temporary  */
604 					/* periods of open fds */
605 	if (maxfd < maxrun)
606 		maxrun = maxfd;
607 	if (maxrun < 4)
608 		maxrun = 4;		/* sanity check */
609 
610 	mntarray = make_mntarray(mntlist, count);
611 
612 	if (listlength == 0) {
613 		if (count == 0)		/* not an error, just none found */
614 			return (0);
615 		fprintf(stderr, gettext("%s: no valid entries found in %s\n"),
616 				myname, mnttab);
617 		return (1);
618 	}
619 
620 	/*
621 	 * Sort the entries based on their mount level only if lofs's are
622 	 * not present.
623 	 */
624 	if (lofscnt == 0) {
625 		qsort((void *)mntarray, listlength, sizeof (mountent_t *),
626 			mcompar);
627 		/*
628 		 * If we do not detect a lofs by now, we never will.
629 		 */
630 		lofscnt = -1;
631 	}
632 	/*
633 	 * Now link them up so that a given pid is easier to find when
634 	 * we go to clean up after they are done.
635 	 */
636 	mntll = mntarray[0];
637 	for (ml = mntarray; mp = *ml; /* */)
638 		mp->link = *++ml;
639 
640 	/*
641 	 * Try to handle interrupts in a reasonable way.
642 	 */
643 	sigset(SIGHUP, cleanup);
644 	sigset(SIGQUIT, cleanup);
645 	sigset(SIGINT, cleanup);
646 
647 	do_umounts(mntarray);	/* do the umounts */
648 	return (exitcode);
649 }
650 
651 /*
652  * Returns a mountent_t array based on mntlist.  If mntlist is NULL, then
653  * it returns all mnttab entries with a few exceptions.  Sets the global
654  * variable listlength to the number of entries in the array.
655  */
656 mountent_t **
657 make_mntarray(char **mntlist, int count)
658 {
659 	mountent_t 	*mp, **mpp;
660 	int 		ndx;
661 	char		*cp;
662 
663 	if (count > 0)
664 		listlength = count;
665 
666 	mpp = (mountent_t **)malloc(sizeof (*mp) * (listlength + 1));
667 	if (mpp == NULL)
668 		nomem();
669 
670 	if (count == 0) {
671 		if (mntll == NULL) {	/* no entries? */
672 			listlength = 0;
673 			return (NULL);
674 		}
675 		/*
676 		 * No mount list specified: take all mnttab mount points
677 		 * except for a few cases.
678 		 */
679 		for (ndx = 0, mp = mntll; mp; mp = mp->link) {
680 			if (fsstrinlist(mp->ment.mnt_mountp, keeplist))
681 				continue;
682 			mp->mlevel = fsgetmlevel(mp->ment.mnt_mountp);
683 			if (mp->ment.mnt_fstype &&
684 			    (strcmp(mp->ment.mnt_fstype, MNTTYPE_LOFS) == 0))
685 				lofscnt++;
686 
687 			mpp[ndx++] = mp;
688 		}
689 		mpp[ndx] = NULL;
690 		listlength = ndx;
691 		return (mpp);
692 	}
693 
694 	/*
695 	 * A list of mount points was specified on the command line.
696 	 * Build an array out of these.
697 	 */
698 	for (ndx = 0; count--; ) {
699 		cp = *mntlist++;
700 		if (realpath(cp, resolve) == NULL) {
701 			fprintf(stderr,
702 				gettext("%s: warning: can't resolve %s\n"),
703 				myname, cp);
704 			exitcode = 1;
705 			mp = getmntlast(mntll, NULL, cp); /* try anyways */
706 		} else
707 			mp = getmntlast(mntll, NULL, resolve);
708 		if (mp == NULL) {
709 			struct mnttab mnew;
710 			/*
711 			 * Then we've reached the end without finding
712 			 * what we are looking for, but we still have to
713 			 * try to umount it: append it to mntarray.
714 			 */
715 			fprintf(stderr, gettext(
716 				"%s: warning: %s not found in %s\n"),
717 				myname, resolve, mnttab);
718 			exitcode = 1;
719 			mntnull(&mnew);
720 			mnew.mnt_special = mnew.mnt_mountp = strdup(resolve);
721 			if (mnew.mnt_special == NULL)
722 				nomem();
723 			mp = new_mountent(&mnew);
724 		}
725 		if (mp->ment.mnt_fstype &&
726 		    (strcmp(mp->ment.mnt_fstype, MNTTYPE_LOFS) == 0))
727 			lofscnt++;
728 
729 		mp->mlevel = fsgetmlevel(mp->ment.mnt_mountp);
730 		mpp[ndx++] = mp;
731 	}
732 	mpp[ndx] = NULL;
733 	listlength = ndx;
734 	return (mpp);
735 }
736 
737 /*
738  * Returns the tail of a linked list of all mnttab entries.  I.e, it's faster
739  * to return the mnttab in reverse order.
740  * Sets listlength to the number of entries in the list.
741  * Returns NULL if none are found.
742  */
743 mountent_t *
744 getmntall(void)
745 {
746 	FILE		*fp;
747 	mountent_t	*mtail;
748 	int		cnt = 0, ret;
749 	struct mnttab	mget;
750 
751 	if ((fp = fopen(mnttab, "r")) == NULL) {
752 		fprintf(stderr, gettext("%s: warning cannot open %s\n"),
753 				myname, mnttab);
754 		return (0);
755 	}
756 	mtail = NULL;
757 
758 	while ((ret = getmntent(fp, &mget)) != -1) {
759 		mountent_t	*mp;
760 
761 		if (ret > 0) {
762 			mnterror(ret);
763 			continue;
764 		}
765 
766 		mp = new_mountent(&mget);
767 		mp->link = mtail;
768 		mtail = mp;
769 		cnt++;
770 	}
771 	fclose(fp);
772 	if (mtail == NULL) {
773 		listlength = 0;
774 		return (NULL);
775 	}
776 	listlength = cnt;
777 	return (mtail);
778 }
779 
780 void
781 do_umounts(mountent_t **mntarray)
782 {
783 	mountent_t *mp, *mpprev, **ml = mntarray;
784 	int	cnt = listlength;
785 
786 	/*
787 	 * Main loop for the forked children:
788 	 */
789 	for (mpprev = *ml; mp = *ml; mpprev = mp, ml++, cnt--) {
790 		pid_t	pid;
791 
792 		/*
793 		 * Check to see if we cross a mount level: e.g.,
794 		 * /a/b/c -> /a/b.  If so, we need to wait for all current
795 		 * umounts to finish before umounting the rest.
796 		 *
797 		 * Also, we unmount serially as long as there are lofs's
798 		 * to mount to avoid improper umount ordering.
799 		 */
800 		if (mp->mlevel < mpprev->mlevel || lofscnt > 0)
801 			while (nrun > 0 && (dowait() != -1))
802 				;
803 
804 		if (lofscnt == 0) {
805 			/*
806 			 * We can now go to parallel umounting.
807 			 */
808 			qsort((void *)ml, cnt, sizeof (mountent_t *), mcompar);
809 			mp = *ml;	/* possible first entry */
810 			lofscnt--;	/* so we don't do this again */
811 		}
812 
813 		while (setup_iopipe(mp) == -1 && (dowait() != -1))
814 			;
815 
816 		while (nrun >= maxrun && (dowait() != -1))	/* throttle */
817 			;
818 
819 #ifdef CACHEFS_BUG
820 		/*
821 		 * If this is the back file system, then let cachefs/umount
822 		 * unmount it.
823 		 */
824 		if (strstr(mp->ment.mnt_mountp, BACKMNT_NAME))
825 			continue;
826 
827 
828 		if (mp->ment.mnt_fstype &&
829 		    (strcmp(mp->ment.mnt_fstype, "cachefs") == 0)) {
830 			while (cachefs_running && (dowait() != -1))
831 					;
832 			cachefs_running = 1;
833 		}
834 #endif
835 
836 		if ((pid = fork()) == -1) {
837 			perror("fork");
838 			cleanup(-1);
839 			/* not reached */
840 		}
841 #ifdef DEBUG
842 		if (dflg && pid > 0) {
843 			fprintf(stderr, "parent %d: umounting %d %s\n",
844 				getpid(), pid, mp->ment.mnt_mountp);
845 		}
846 #endif
847 		if (pid == 0) {		/* child */
848 			signal(SIGHUP, SIG_IGN);
849 			signal(SIGQUIT, SIG_IGN);
850 			signal(SIGINT, SIG_IGN);
851 			setup_output(mp);
852 			doexec(&mp->ment);
853 			perror("exec");
854 			exit(1);
855 		}
856 
857 		/* parent */
858 		(void) close(mp->sopipe[WRPIPE]);
859 		(void) close(mp->sepipe[WRPIPE]);
860 		mp->pid = pid;
861 		nrun++;
862 	}
863 	cleanup(0);
864 }
865 
866 /*
867  * cleanup the existing children and exit with an error
868  * if asig != 0.
869  */
870 void
871 cleanup(int asig)
872 {
873 	/*
874 	 * Let the stragglers finish.
875 	 */
876 	while (nrun > 0 && (dowait() != -1))
877 		;
878 	if (asig != 0)
879 		exit(1);
880 }
881 
882 
883 /*
884  * Waits for 1 child to die.
885  *
886  * Returns -1 if no children are left to wait for.
887  * Returns 0 if a child died without an error.
888  * Returns 1 if a child died with an error.
889  * Sets the global exitcode if an error occurred.
890  */
891 int
892 dowait(void)
893 {
894 	int		wstat, child, ret;
895 	mountent_t 	*mp, *prevp;
896 
897 	if ((child = wait(&wstat)) == -1)
898 		return (-1);
899 
900 	if (WIFEXITED(wstat))		/* this should always be true */
901 		ret = WEXITSTATUS(wstat);
902 	else
903 		ret = 1;		/* assume some kind of error */
904 	nrun--;
905 	if (ret)
906 		exitcode = 1;
907 
908 	/*
909 	 * Find our child so we can process its std output, if any.
910 	 * This search gets smaller and smaller as children are cleaned
911 	 * up.
912 	 */
913 	for (prevp = NULL, mp = mntll; mp; mp = mp->link) {
914 		if (mp->pid != child) {
915 			prevp = mp;
916 			continue;
917 		}
918 		/*
919 		 * Found: let's remove it from this list.
920 		 */
921 		if (prevp) {
922 			prevp->link = mp->link;
923 			mp->link = NULL;
924 		}
925 		break;
926 	}
927 
928 	if (mp == NULL) {
929 		/*
930 		 * This should never happen.
931 		 */
932 #ifdef DEBUG
933 		fprintf(stderr, gettext(
934 			"%s: unknown child %d\n"), myname, child);
935 #endif
936 		exitcode = 1;
937 		return (1);
938 	}
939 	doio(mp);	/* Any output? */
940 
941 	if (mp->ment.mnt_fstype &&
942 	    (strcmp(mp->ment.mnt_fstype, MNTTYPE_LOFS) == 0))
943 		lofscnt--;
944 
945 #ifdef CACHEFS_BUG
946 	if (mp->ment.mnt_fstype &&
947 	    (strcmp(mp->ment.mnt_fstype, "cachefs") == 0))
948 		cachefs_running = 0;
949 #endif
950 
951 	return (ret);
952 }
953 
954 static const mountent_t zmount = { 0 };
955 
956 mountent_t *
957 new_mountent(struct mnttab *ment)
958 {
959 	mountent_t *new;
960 
961 	new = (mountent_t *)malloc(sizeof (*new));
962 	if (new == NULL)
963 		nomem();
964 
965 	*new = zmount;
966 	if (ment->mnt_special &&
967 	    (new->ment.mnt_special = strdup(ment->mnt_special)) == NULL)
968 		nomem();
969 	if (ment->mnt_mountp &&
970 	    (new->ment.mnt_mountp = strdup(ment->mnt_mountp)) == NULL)
971 		nomem();
972 	if (ment->mnt_fstype &&
973 	    (new->ment.mnt_fstype = strdup(ment->mnt_fstype)) == NULL)
974 		nomem();
975 	return (new);
976 }
977 
978 
979 /*
980  * Sort in descending order of "mount level".  For example, /a/b/c is
981  * placed before /a/b .
982  */
983 int
984 mcompar(const void *a, const void *b)
985 {
986 	mountent_t *a1, *b1;
987 
988 	a1 = *(mountent_t **)a;
989 	b1 = *(mountent_t **)b;
990 	return (b1->mlevel - a1->mlevel);
991 }
992 
993 /*
994  * The purpose of this routine is to form stdout and stderr
995  * pipes for the children's output.  The parent then reads and writes it
996  * out it serially in order to ensure that the output is
997  * not garbled.
998  */
999 
1000 int
1001 setup_iopipe(mountent_t *mp)
1002 {
1003 	/*
1004 	 * Make a stdout and stderr pipe.  This should never fail.
1005 	 */
1006 	if (pipe(mp->sopipe) == -1)
1007 		return (-1);
1008 	if (pipe(mp->sepipe) == -1) {
1009 		(void) close(mp->sopipe[RDPIPE]);
1010 		(void) close(mp->sopipe[WRPIPE]);
1011 		return (-1);
1012 	}
1013 	/*
1014 	 * Don't block on an empty pipe.
1015 	 */
1016 	(void) fcntl(mp->sopipe[RDPIPE], F_SETFL, O_NDELAY|O_NONBLOCK);
1017 	(void) fcntl(mp->sepipe[RDPIPE], F_SETFL, O_NDELAY|O_NONBLOCK);
1018 	return (0);
1019 }
1020 
1021 /*
1022  * Called by a child to attach its stdout and stderr to the write side of
1023  * the pipes.
1024  */
1025 void
1026 setup_output(mountent_t *mp)
1027 {
1028 	(void) close(fileno(stdout));
1029 	(void) dup(mp->sopipe[WRPIPE]);
1030 	(void) close(mp->sopipe[WRPIPE]);
1031 
1032 	(void) close(fileno(stderr));
1033 	(void) dup(mp->sepipe[WRPIPE]);
1034 	(void) close(mp->sepipe[WRPIPE]);
1035 }
1036 
1037 /*
1038  * Parent uses this to print any stdout or stderr output issued by
1039  * the child.
1040  */
1041 static void
1042 doio(mountent_t *mp)
1043 {
1044 	int bytes;
1045 
1046 	while ((bytes = read(mp->sepipe[RDPIPE], ibuf, sizeof (ibuf))) > 0)
1047 		write(fileno(stderr), ibuf, bytes);
1048 	while ((bytes = read(mp->sopipe[RDPIPE], ibuf, sizeof (ibuf))) > 0)
1049 		write(fileno(stdout), ibuf, bytes);
1050 
1051 	(void) close(mp->sopipe[RDPIPE]);
1052 	(void) close(mp->sepipe[RDPIPE]);
1053 }
1054 
1055 void
1056 nomem(void)
1057 {
1058 	fprintf(stderr, gettext("%s: out of memory\n"), myname);
1059 	/*
1060 	 * Let the stragglers finish.
1061 	 */
1062 	while (nrun > 0 && (dowait() != -1))
1063 		;
1064 	exit(1);
1065 }
1066