xref: /titanic_41/usr/src/cmd/fs.d/umount.c (revision 4496171313bed39e96f21bc2f9faf2868e267ae3)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved  	*/
28 
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include	<stdio.h>
33 #include	<stdio_ext.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 		(void) enable_extended_FILE_stdio(-1, -1);
598 	}
599 
600 	/*
601 	 * The parent needs to maintain 3 of its own fd's, plus 2 for
602 	 * each child (the stdout and stderr pipes).
603 	 */
604 	maxfd = (maxfd / 2) - 6;	/* 6 takes care of temporary  */
605 					/* periods of open fds */
606 	if (maxfd < maxrun)
607 		maxrun = maxfd;
608 	if (maxrun < 4)
609 		maxrun = 4;		/* sanity check */
610 
611 	mntarray = make_mntarray(mntlist, count);
612 
613 	if (listlength == 0) {
614 		if (count == 0)		/* not an error, just none found */
615 			return (0);
616 		fprintf(stderr, gettext("%s: no valid entries found in %s\n"),
617 				myname, mnttab);
618 		return (1);
619 	}
620 
621 	/*
622 	 * Sort the entries based on their mount level only if lofs's are
623 	 * not present.
624 	 */
625 	if (lofscnt == 0) {
626 		qsort((void *)mntarray, listlength, sizeof (mountent_t *),
627 			mcompar);
628 		/*
629 		 * If we do not detect a lofs by now, we never will.
630 		 */
631 		lofscnt = -1;
632 	}
633 	/*
634 	 * Now link them up so that a given pid is easier to find when
635 	 * we go to clean up after they are done.
636 	 */
637 	mntll = mntarray[0];
638 	for (ml = mntarray; mp = *ml; /* */)
639 		mp->link = *++ml;
640 
641 	/*
642 	 * Try to handle interrupts in a reasonable way.
643 	 */
644 	sigset(SIGHUP, cleanup);
645 	sigset(SIGQUIT, cleanup);
646 	sigset(SIGINT, cleanup);
647 
648 	do_umounts(mntarray);	/* do the umounts */
649 	return (exitcode);
650 }
651 
652 /*
653  * Returns a mountent_t array based on mntlist.  If mntlist is NULL, then
654  * it returns all mnttab entries with a few exceptions.  Sets the global
655  * variable listlength to the number of entries in the array.
656  */
657 mountent_t **
658 make_mntarray(char **mntlist, int count)
659 {
660 	mountent_t 	*mp, **mpp;
661 	int 		ndx;
662 	char		*cp;
663 
664 	if (count > 0)
665 		listlength = count;
666 
667 	mpp = (mountent_t **)malloc(sizeof (*mp) * (listlength + 1));
668 	if (mpp == NULL)
669 		nomem();
670 
671 	if (count == 0) {
672 		if (mntll == NULL) {	/* no entries? */
673 			listlength = 0;
674 			return (NULL);
675 		}
676 		/*
677 		 * No mount list specified: take all mnttab mount points
678 		 * except for a few cases.
679 		 */
680 		for (ndx = 0, mp = mntll; mp; mp = mp->link) {
681 			if (fsstrinlist(mp->ment.mnt_mountp, keeplist))
682 				continue;
683 			mp->mlevel = fsgetmlevel(mp->ment.mnt_mountp);
684 			if (mp->ment.mnt_fstype &&
685 			    (strcmp(mp->ment.mnt_fstype, MNTTYPE_LOFS) == 0))
686 				lofscnt++;
687 
688 			mpp[ndx++] = mp;
689 		}
690 		mpp[ndx] = NULL;
691 		listlength = ndx;
692 		return (mpp);
693 	}
694 
695 	/*
696 	 * A list of mount points was specified on the command line.
697 	 * Build an array out of these.
698 	 */
699 	for (ndx = 0; count--; ) {
700 		cp = *mntlist++;
701 		if (realpath(cp, resolve) == NULL) {
702 			fprintf(stderr,
703 				gettext("%s: warning: can't resolve %s\n"),
704 				myname, cp);
705 			exitcode = 1;
706 			mp = getmntlast(mntll, NULL, cp); /* try anyways */
707 		} else
708 			mp = getmntlast(mntll, NULL, resolve);
709 		if (mp == NULL) {
710 			struct mnttab mnew;
711 			/*
712 			 * Then we've reached the end without finding
713 			 * what we are looking for, but we still have to
714 			 * try to umount it: append it to mntarray.
715 			 */
716 			fprintf(stderr, gettext(
717 				"%s: warning: %s not found in %s\n"),
718 				myname, resolve, mnttab);
719 			exitcode = 1;
720 			mntnull(&mnew);
721 			mnew.mnt_special = mnew.mnt_mountp = strdup(resolve);
722 			if (mnew.mnt_special == NULL)
723 				nomem();
724 			mp = new_mountent(&mnew);
725 		}
726 		if (mp->ment.mnt_fstype &&
727 		    (strcmp(mp->ment.mnt_fstype, MNTTYPE_LOFS) == 0))
728 			lofscnt++;
729 
730 		mp->mlevel = fsgetmlevel(mp->ment.mnt_mountp);
731 		mpp[ndx++] = mp;
732 	}
733 	mpp[ndx] = NULL;
734 	listlength = ndx;
735 	return (mpp);
736 }
737 
738 /*
739  * Returns the tail of a linked list of all mnttab entries.  I.e, it's faster
740  * to return the mnttab in reverse order.
741  * Sets listlength to the number of entries in the list.
742  * Returns NULL if none are found.
743  */
744 mountent_t *
745 getmntall(void)
746 {
747 	FILE		*fp;
748 	mountent_t	*mtail;
749 	int		cnt = 0, ret;
750 	struct mnttab	mget;
751 
752 	if ((fp = fopen(mnttab, "r")) == NULL) {
753 		fprintf(stderr, gettext("%s: warning cannot open %s\n"),
754 				myname, mnttab);
755 		return (0);
756 	}
757 	mtail = NULL;
758 
759 	while ((ret = getmntent(fp, &mget)) != -1) {
760 		mountent_t	*mp;
761 
762 		if (ret > 0) {
763 			mnterror(ret);
764 			continue;
765 		}
766 
767 		mp = new_mountent(&mget);
768 		mp->link = mtail;
769 		mtail = mp;
770 		cnt++;
771 	}
772 	fclose(fp);
773 	if (mtail == NULL) {
774 		listlength = 0;
775 		return (NULL);
776 	}
777 	listlength = cnt;
778 	return (mtail);
779 }
780 
781 void
782 do_umounts(mountent_t **mntarray)
783 {
784 	mountent_t *mp, *mpprev, **ml = mntarray;
785 	int	cnt = listlength;
786 
787 	/*
788 	 * Main loop for the forked children:
789 	 */
790 	for (mpprev = *ml; mp = *ml; mpprev = mp, ml++, cnt--) {
791 		pid_t	pid;
792 
793 		/*
794 		 * Check to see if we cross a mount level: e.g.,
795 		 * /a/b/c -> /a/b.  If so, we need to wait for all current
796 		 * umounts to finish before umounting the rest.
797 		 *
798 		 * Also, we unmount serially as long as there are lofs's
799 		 * to mount to avoid improper umount ordering.
800 		 */
801 		if (mp->mlevel < mpprev->mlevel || lofscnt > 0)
802 			while (nrun > 0 && (dowait() != -1))
803 				;
804 
805 		if (lofscnt == 0) {
806 			/*
807 			 * We can now go to parallel umounting.
808 			 */
809 			qsort((void *)ml, cnt, sizeof (mountent_t *), mcompar);
810 			mp = *ml;	/* possible first entry */
811 			lofscnt--;	/* so we don't do this again */
812 		}
813 
814 		while (setup_iopipe(mp) == -1 && (dowait() != -1))
815 			;
816 
817 		while (nrun >= maxrun && (dowait() != -1))	/* throttle */
818 			;
819 
820 #ifdef CACHEFS_BUG
821 		/*
822 		 * If this is the back file system, then let cachefs/umount
823 		 * unmount it.
824 		 */
825 		if (strstr(mp->ment.mnt_mountp, BACKMNT_NAME))
826 			continue;
827 
828 
829 		if (mp->ment.mnt_fstype &&
830 		    (strcmp(mp->ment.mnt_fstype, "cachefs") == 0)) {
831 			while (cachefs_running && (dowait() != -1))
832 					;
833 			cachefs_running = 1;
834 		}
835 #endif
836 
837 		if ((pid = fork()) == -1) {
838 			perror("fork");
839 			cleanup(-1);
840 			/* not reached */
841 		}
842 #ifdef DEBUG
843 		if (dflg && pid > 0) {
844 			fprintf(stderr, "parent %d: umounting %d %s\n",
845 				getpid(), pid, mp->ment.mnt_mountp);
846 		}
847 #endif
848 		if (pid == 0) {		/* child */
849 			signal(SIGHUP, SIG_IGN);
850 			signal(SIGQUIT, SIG_IGN);
851 			signal(SIGINT, SIG_IGN);
852 			setup_output(mp);
853 			doexec(&mp->ment);
854 			perror("exec");
855 			exit(1);
856 		}
857 
858 		/* parent */
859 		(void) close(mp->sopipe[WRPIPE]);
860 		(void) close(mp->sepipe[WRPIPE]);
861 		mp->pid = pid;
862 		nrun++;
863 	}
864 	cleanup(0);
865 }
866 
867 /*
868  * cleanup the existing children and exit with an error
869  * if asig != 0.
870  */
871 void
872 cleanup(int asig)
873 {
874 	/*
875 	 * Let the stragglers finish.
876 	 */
877 	while (nrun > 0 && (dowait() != -1))
878 		;
879 	if (asig != 0)
880 		exit(1);
881 }
882 
883 
884 /*
885  * Waits for 1 child to die.
886  *
887  * Returns -1 if no children are left to wait for.
888  * Returns 0 if a child died without an error.
889  * Returns 1 if a child died with an error.
890  * Sets the global exitcode if an error occurred.
891  */
892 int
893 dowait(void)
894 {
895 	int		wstat, child, ret;
896 	mountent_t 	*mp, *prevp;
897 
898 	if ((child = wait(&wstat)) == -1)
899 		return (-1);
900 
901 	if (WIFEXITED(wstat))		/* this should always be true */
902 		ret = WEXITSTATUS(wstat);
903 	else
904 		ret = 1;		/* assume some kind of error */
905 	nrun--;
906 	if (ret)
907 		exitcode = 1;
908 
909 	/*
910 	 * Find our child so we can process its std output, if any.
911 	 * This search gets smaller and smaller as children are cleaned
912 	 * up.
913 	 */
914 	for (prevp = NULL, mp = mntll; mp; mp = mp->link) {
915 		if (mp->pid != child) {
916 			prevp = mp;
917 			continue;
918 		}
919 		/*
920 		 * Found: let's remove it from this list.
921 		 */
922 		if (prevp) {
923 			prevp->link = mp->link;
924 			mp->link = NULL;
925 		}
926 		break;
927 	}
928 
929 	if (mp == NULL) {
930 		/*
931 		 * This should never happen.
932 		 */
933 #ifdef DEBUG
934 		fprintf(stderr, gettext(
935 			"%s: unknown child %d\n"), myname, child);
936 #endif
937 		exitcode = 1;
938 		return (1);
939 	}
940 	doio(mp);	/* Any output? */
941 
942 	if (mp->ment.mnt_fstype &&
943 	    (strcmp(mp->ment.mnt_fstype, MNTTYPE_LOFS) == 0))
944 		lofscnt--;
945 
946 #ifdef CACHEFS_BUG
947 	if (mp->ment.mnt_fstype &&
948 	    (strcmp(mp->ment.mnt_fstype, "cachefs") == 0))
949 		cachefs_running = 0;
950 #endif
951 
952 	return (ret);
953 }
954 
955 static const mountent_t zmount = { 0 };
956 
957 mountent_t *
958 new_mountent(struct mnttab *ment)
959 {
960 	mountent_t *new;
961 
962 	new = (mountent_t *)malloc(sizeof (*new));
963 	if (new == NULL)
964 		nomem();
965 
966 	*new = zmount;
967 	if (ment->mnt_special &&
968 	    (new->ment.mnt_special = strdup(ment->mnt_special)) == NULL)
969 		nomem();
970 	if (ment->mnt_mountp &&
971 	    (new->ment.mnt_mountp = strdup(ment->mnt_mountp)) == NULL)
972 		nomem();
973 	if (ment->mnt_fstype &&
974 	    (new->ment.mnt_fstype = strdup(ment->mnt_fstype)) == NULL)
975 		nomem();
976 	return (new);
977 }
978 
979 
980 /*
981  * Sort in descending order of "mount level".  For example, /a/b/c is
982  * placed before /a/b .
983  */
984 int
985 mcompar(const void *a, const void *b)
986 {
987 	mountent_t *a1, *b1;
988 
989 	a1 = *(mountent_t **)a;
990 	b1 = *(mountent_t **)b;
991 	return (b1->mlevel - a1->mlevel);
992 }
993 
994 /*
995  * The purpose of this routine is to form stdout and stderr
996  * pipes for the children's output.  The parent then reads and writes it
997  * out it serially in order to ensure that the output is
998  * not garbled.
999  */
1000 
1001 int
1002 setup_iopipe(mountent_t *mp)
1003 {
1004 	/*
1005 	 * Make a stdout and stderr pipe.  This should never fail.
1006 	 */
1007 	if (pipe(mp->sopipe) == -1)
1008 		return (-1);
1009 	if (pipe(mp->sepipe) == -1) {
1010 		(void) close(mp->sopipe[RDPIPE]);
1011 		(void) close(mp->sopipe[WRPIPE]);
1012 		return (-1);
1013 	}
1014 	/*
1015 	 * Don't block on an empty pipe.
1016 	 */
1017 	(void) fcntl(mp->sopipe[RDPIPE], F_SETFL, O_NDELAY|O_NONBLOCK);
1018 	(void) fcntl(mp->sepipe[RDPIPE], F_SETFL, O_NDELAY|O_NONBLOCK);
1019 	return (0);
1020 }
1021 
1022 /*
1023  * Called by a child to attach its stdout and stderr to the write side of
1024  * the pipes.
1025  */
1026 void
1027 setup_output(mountent_t *mp)
1028 {
1029 	(void) close(fileno(stdout));
1030 	(void) dup(mp->sopipe[WRPIPE]);
1031 	(void) close(mp->sopipe[WRPIPE]);
1032 
1033 	(void) close(fileno(stderr));
1034 	(void) dup(mp->sepipe[WRPIPE]);
1035 	(void) close(mp->sepipe[WRPIPE]);
1036 }
1037 
1038 /*
1039  * Parent uses this to print any stdout or stderr output issued by
1040  * the child.
1041  */
1042 static void
1043 doio(mountent_t *mp)
1044 {
1045 	int bytes;
1046 
1047 	while ((bytes = read(mp->sepipe[RDPIPE], ibuf, sizeof (ibuf))) > 0)
1048 		write(fileno(stderr), ibuf, bytes);
1049 	while ((bytes = read(mp->sopipe[RDPIPE], ibuf, sizeof (ibuf))) > 0)
1050 		write(fileno(stdout), ibuf, bytes);
1051 
1052 	(void) close(mp->sopipe[RDPIPE]);
1053 	(void) close(mp->sepipe[RDPIPE]);
1054 }
1055 
1056 void
1057 nomem(void)
1058 {
1059 	fprintf(stderr, gettext("%s: out of memory\n"), myname);
1060 	/*
1061 	 * Let the stragglers finish.
1062 	 */
1063 	while (nrun > 0 && (dowait() != -1))
1064 		;
1065 	exit(1);
1066 }
1067