summaryrefslogtreecommitdiff
path: root/usr/src/cmd/fs.d/umount.c
blob: 0139e682058c6319fbf9b60ec68b7b4b157a1c93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved  	*/


#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include	<stdio.h>
#include	<limits.h>
#include	<unistd.h>
#include	<stdlib.h>
#include	<string.h>
#include	<sys/signal.h>
#include	<sys/mnttab.h>
#include	<errno.h>
#include	<sys/types.h>
#include	<sys/stat.h>
#include	<sys/param.h>
#include	<sys/wait.h>
#include	<sys/vfstab.h>
#include	<sys/fcntl.h>
#include	<sys/resource.h>
#include	<sys/mntent.h>
#include	<sys/ctfs.h>
#include	<locale.h>
#include	<stdarg.h>
#include	<sys/mount.h>
#include	<sys/objfs.h>
#include	"fslib.h"

#define	FS_PATH		"/usr/lib/fs"
#define	ALT_PATH	"/etc/fs"
#define	FULLPATH_MAX	32
#define	FSTYPE_MAX	8
#define	ARGV_MAX	16

int	aflg, oflg, Vflg, dashflg, dflg, fflg;

extern void	rpterr(), usage(), mnterror();

extern	char	*optarg;	/* used by getopt */
extern	int	optind, opterr;

static char	*myname;
char	fs_path[] = FS_PATH;
char	alt_path[] = ALT_PATH;
char	mnttab[MAXPATHLEN + 1];
char	*oarg, *farg;
int	maxrun, nrun;
int	no_mnttab;
int	lofscnt;		/* presence of lofs prohibits parallel */
				/* umounting */
int	exitcode;
char	resolve[MAXPATHLEN];
static  char ibuf[BUFSIZ];

/*
 * Currently, mounting cachefs's simultaneous uncovers various problems.
 * For the short term, we serialize cachefs activity while we fix
 * these cachefs bugs.
 */
#define	CACHEFS_BUG
#ifdef	CACHEFS_BUG
#include	<sys/fs/cachefs_fs.h>	/* for BACKMNT_NAME */
int	cachefs_running;	/* parallel cachefs not supported yet */
#endif

/*
 * The basic mount struct that describes an mnttab entry.
 * It is used both in an array and as a linked list elem.
 */

typedef struct mountent {
	struct mnttab	ment;		/* the mnttab data */
	int		mlevel;		/* mount level of the mount pt */
	pid_t		pid;		/* the pid of this mount process */
#define	RDPIPE		0
#define	WRPIPE		1
	int		sopipe[2];	/* pipe attached to child's stdout */
	int		sepipe[2];	/* pipe attached to child's stderr */
	struct mountent *link;		/* used when in linked list */
} mountent_t;

static mountent_t	*mntll;		/* head of global linked list of */
					/* mountents */
int			listlength;	/* # of elems in this list */

/*
 * If the automatic flag (-a) is given and mount points are not specified
 * on the command line, then do not attempt to umount these.  These
 * generally need to be kept mounted until system shutdown.
 */
static const char   *keeplist[] = {
	"/",
	"/dev",
	"/dev/fd",
	"/devices",
	"/etc/mnttab",
	"/etc/svc/volatile",
	"/lib",
	"/proc",
	"/sbin",
	CTFS_ROOT,
	OBJFS_ROOT,
	"/tmp",
	"/usr",
	"/var",
	"/var/adm",
	"/var/run",
	NULL
};

static void	nomem();
static void	doexec(struct mnttab *);
static int	setup_iopipe(mountent_t *);
static void	setup_output(mountent_t *);
static void	doio(mountent_t *);
static void	do_umounts(mountent_t **);
static int	dowait();
static int	parumount();
static int	mcompar(const void *, const void *);
static void	cleanup(int);

static mountent_t	**make_mntarray(char **, int);
static mountent_t	*getmntall();
static mountent_t 	*new_mountent(struct mnttab *);
static mountent_t	*getmntlast(mountent_t *, char *, char *);

int
main(int argc, char **argv)
{
	int 	cc;
	struct mnttab  mget;
	char 	*mname, *is_special;
	int	fscnt;
	mountent_t	*mp;

	(void) setlocale(LC_ALL, "");

#if !defined(TEXT_DOMAIN)
#define	TEXT_DOMAIN "SYS_TEST"
#endif
	(void) textdomain(TEXT_DOMAIN);

	myname = strrchr(argv[0], '/');
	if (myname)
		myname++;
	else
		myname = argv[0];

	/*
	 * Process the args.
	 * "-d" for compatibility
	 */
	while ((cc = getopt(argc, argv, "ado:Vf?")) != -1)
		switch (cc) {
		case 'a':
			aflg++;
			break;
#ifdef DEBUG
		case 'd':
			dflg++;
			break;
#endif

		case '?':
			usage();
			break;
		case 'o':
			if (oflg)
				usage();
			else {
				oflg++;
				oarg = optarg;
			}
			break;
		case 'f':
			fflg++;
			break;
		case 'V':
			if (Vflg)
				usage();
			else
				Vflg++;
			break;
		default:
			usage();
			break;
		}

	fscnt = argc - optind;
	if (!aflg && fscnt != 1)
		usage();

	/* copy '--' to specific */
	if (strcmp(argv[optind-1], "--") == 0)
		dashflg++;

	/*
	 * mnttab may be a symlink to a file in another file system.
	 * This happens during install when / is mounted read-only
	 * and /etc/mnttab is symlinked to a file in /tmp.
	 * If this is the case, we need to follow the symlink to the
	 * read-write file itself so that the subsequent mnttab.temp
	 * open and rename will work.
	 */
	if (realpath(MNTTAB, mnttab) == NULL) {
		strcpy(mnttab, MNTTAB);
	}

	/*
	 * bugid 1205242
	 * call the realpath() here, so that if the user is
	 * trying to umount an autofs directory, the directory
	 * is forced to mount.
	 */

	mname = argv[optind];
	is_special = realpath(mname, resolve);

	/*
	 * Read the whole mnttab into memory.
	 */
	mntll = getmntall();

	if (aflg && fscnt != 1)
		exit(parumount(argv + optind, fscnt));

	aflg = 0;

	mntnull(&mget);
	if (listlength == 0) {
		fprintf(stderr, gettext(
			"%s: warning: no entries found in %s\n"),
				myname, mnttab);
		mget.mnt_mountp = mname;	/* assume mount point */
		no_mnttab++;
		doexec(&mget);
		exit(0);
	}

	mp = NULL;

	/*
	 * if realpath fails, it can't be a mount point, so we'll
	 * go straight to the code that treats the arg as a special.
	 * if realpath succeeds, it could be a special or a mount point;
	 * we'll start by assuming it's a mount point, and if it's not,
	 * try to treat it as a special.
	 */
	if (is_special != NULL) {
		/*
		 * if this succeeds,
		 * we'll have the appropriate record; if it fails
		 * we'll assume the arg is a special of some sort
		 */
		mp = getmntlast(mntll, NULL, resolve);
	}
	/*
	 * Since stackable mount is allowed (RFE 2001535),
	 * we will un-mount the last entry in the MNTTAB that matches.
	 */
	if (mp == NULL) {
		/*
		 * Perhaps there is a bogus mnttab entry that
		 * can't be resolved:
		 */
		if ((mp = getmntlast(mntll, NULL, mname)) == NULL)
			/*
			 * assume it's a device (special) now
			 */
			mp = getmntlast(mntll, mname, NULL);
		if (mp) {
			/*
			 * Found it.
			 * This is a device. Now we want to know if
			 * it stackmounted on by something else.
			 * The original fix for bug 1103850 has a
			 * problem with lockfs (bug 1119731). This
			 * is a revised method.
			 */
			mountent_t *lmp;
			lmp = getmntlast(mntll, NULL, mp->ment.mnt_mountp);

			if (lmp && strcmp(lmp->ment.mnt_special,
					mp->ment.mnt_special)) {
				errno = EBUSY;
				rpterr(mname);
				exit(1);
			}
		} else {
			fprintf(stderr, gettext(
				"%s: warning: %s not in mnttab\n"),
				myname, mname);
			if (Vflg)
				exit(1);
				/*
				 * same error as mount -V
				 * would give for unknown
				 * mount point
				 */
			mget.mnt_special = mget.mnt_mountp = mname;
		}
	}

	if (mp)
		doexec(&mp->ment);
	else
		doexec(&mget);

	return (0);
}

void
doexec(struct mnttab *ment)
{
	int 	ret;

#ifdef DEBUG
	if (dflg)
		fprintf(stderr, "%d: umounting %s\n",
			getpid(), ment->mnt_mountp);
#endif

	/* try to exec the dependent portion */
	if ((ment->mnt_fstype != NULL) || Vflg) {
		char	full_path[FULLPATH_MAX];
		char	alter_path[FULLPATH_MAX];
		char	*newargv[ARGV_MAX];
		int 	ii;

		if (strlen(ment->mnt_fstype) > (size_t)FSTYPE_MAX) {
			fprintf(stderr, gettext(
				"%s: FSType %s exceeds %d characters\n"),
				myname, ment->mnt_fstype, FSTYPE_MAX);
			exit(1);
		}

		/* build the full pathname of the fstype dependent command. */
		sprintf(full_path, "%s/%s/%s", fs_path, ment->mnt_fstype,
					myname);
		sprintf(alter_path, "%s/%s/%s", alt_path, ment->mnt_fstype,
					myname);

		/*
		 * create the new arg list, and end the list with a
		 * null pointer
		 */
		ii = 2;
		if (oflg) {
			newargv[ii++] = "-o";
			newargv[ii++] = oarg;
		}
		if (dashflg) {
			newargv[ii++] = "--";
		}
		if (fflg) {
			newargv[ii++] = "-f";
		}
		newargv[ii++] = (ment->mnt_mountp)
				? ment->mnt_mountp : ment->mnt_special;
		newargv[ii] = NULL;

		/* set the new argv[0] to the filename */
		newargv[1] = myname;

		if (Vflg) {
			printf("%s", myname);
			for (ii = 2; newargv[ii]; ii++)
				printf(" %s", newargv[ii]);
			printf("\n");
			fflush(stdout);
			exit(0);
		}

		/* Try to exec the fstype dependent umount. */
		execv(full_path, &newargv[1]);
		if (errno == ENOEXEC) {
			newargv[0] = "sh";
			newargv[1] = full_path;
			execv("/sbin/sh", &newargv[0]);
		}
		newargv[1] = myname;
		execv(alter_path, &newargv[1]);
		if (errno == ENOEXEC) {
			newargv[0] = "sh";
			newargv[1] = alter_path;
			execv("/sbin/sh", &newargv[0]);
		}
		/* exec failed */
		if (errno != ENOENT) {
			fprintf(stderr, gettext("umount: cannot execute %s\n"),
					full_path);
			exit(1);
		}
	}
	/*
	 * No fstype independent executable then.  We'll go generic
	 * from here.
	 */

	/* don't use -o with generic */
	if (oflg) {
		fprintf(stderr, gettext(
	"%s: %s specific umount does not exist; -o suboption ignored\n"),
		myname, ment->mnt_fstype ? ment->mnt_fstype : "<null>");
	}

	signal(SIGHUP,  SIG_IGN);
	signal(SIGQUIT, SIG_IGN);
	signal(SIGINT,  SIG_IGN);
	/*
	 * Try to umount the mountpoint.
	 * If that fails, try the corresponding special.
	 * (This ordering is necessary for nfs umounts.)
	 * (for remote resources:  if the first umount returns EBUSY
	 * don't call umount again - umount() with a resource name
	 * will return a misleading error to the user
	 */
	if (fflg) {
		if (((ret = umount2(ment->mnt_mountp, MS_FORCE)) < 0) &&
				(errno != EBUSY && errno != ENOTSUP &&
				errno != EPERM))
			ret = umount2(ment->mnt_special, MS_FORCE);
	} else {
		if (((ret = umount2(ment->mnt_mountp, 0)) < 0) &&
				(errno != EBUSY) && (errno != EPERM))
			ret = umount2(ment->mnt_special, 0);
	}

	if (ret < 0) {
		rpterr(ment->mnt_mountp);
		if (errno != EINVAL && errno != EFAULT)
			exit(1);

		exitcode = 1;
	}

	exit(exitcode);
}

void
rpterr(char *sp)
{
	switch (errno) {
	case EPERM:
		fprintf(stderr, gettext("%s: permission denied\n"), myname);
		break;
	case ENXIO:
		fprintf(stderr, gettext("%s: %s no device\n"), myname, sp);
		break;
	case ENOENT:
		fprintf(stderr,
			gettext("%s: %s no such file or directory\n"),
			myname, sp);
		break;
	case EINVAL:
		fprintf(stderr, gettext("%s: %s not mounted\n"), myname, sp);
		break;
	case EBUSY:
		fprintf(stderr, gettext("%s: %s busy\n"), myname, sp);
		break;
	case ENOTBLK:
		fprintf(stderr,
			gettext("%s: %s block device required\n"), myname, sp);
		break;
	case ECOMM:
		fprintf(stderr,
			gettext("%s: warning: broken link detected\n"), myname);
		break;
	default:
		perror(myname);
		fprintf(stderr, gettext("%s: cannot unmount %s\n"), myname, sp);
	}
}

void
usage(void)
{
	fprintf(stderr, gettext(
"Usage:\n%s [-f] [-V] [-o specific_options] {special | mount-point}\n"),
		myname);
	fprintf(stderr, gettext(
"%s -a [-f] [-V] [-o specific_options] [mount_point ...]\n"), myname);
	exit(1);
}

void
mnterror(int flag)
{
	switch (flag) {
	case MNT_TOOLONG:
		fprintf(stderr,
			gettext("%s: line in mnttab exceeds %d characters\n"),
			myname, MNT_LINE_MAX-2);
		break;
	case MNT_TOOFEW:
		fprintf(stderr,
			gettext("%s: line in mnttab has too few entries\n"),
			myname);
		break;
	default:
		break;
	}
}

/*
 * Search the mlist linked list for the
 * first match of specp or mntp.  The list is expected to be in reverse
 * order of /etc/mnttab.
 * If both are specified, then both have to match.
 * Returns the (mountent_t *) of the match, otherwise returns NULL.
 */
mountent_t *
getmntlast(mountent_t *mlist, char *specp, char *mntp)
{
	int		mfound, sfound;

	for (/* */; mlist; mlist = mlist->link) {
		mfound = sfound = 0;
		if (mntp && (strcmp(mlist->ment.mnt_mountp, mntp) == 0)) {
			if (specp == NULL)
				return (mlist);
			mfound++;
		}
		if (specp && (strcmp(mlist->ment.mnt_special, specp) == 0)) {
			if (mntp == NULL)
				return (mlist);
			sfound++;
		}
		if (mfound && sfound)
			return (mlist);
	}
	return (NULL);
}



/*
 * Perform the parallel version of umount.  Returns 0 if no errors occurred,
 * non zero otherwise.
 */
int
parumount(char **mntlist, int count)
{
	int 		maxfd = OPEN_MAX;
	struct rlimit 	rl;
	mountent_t	**mntarray, **ml, *mp;

	/*
	 * If no mount points are specified and none were found in mnttab,
	 * then end it all here.
	 */
	if (count == 0 && mntll == NULL)
		return (0);

	/*
	 * This is the process scaling section.  After running a series
	 * of tests based on the number of simultaneous processes and
	 * processors available, optimum performance was achieved near or
	 * at (PROCN * 2).
	 */
	if ((maxrun = sysconf(_SC_NPROCESSORS_ONLN)) == -1)
		maxrun = 4;
	else
		maxrun = maxrun * 2 + 1;

	if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
		rl.rlim_cur = rl.rlim_max;
		if (setrlimit(RLIMIT_NOFILE, &rl) == 0)
			maxfd = (int)rl.rlim_cur;
	}

	/*
	 * The parent needs to maintain 3 of its own fd's, plus 2 for
	 * each child (the stdout and stderr pipes).
	 */
	maxfd = (maxfd / 2) - 6;	/* 6 takes care of temporary  */
					/* periods of open fds */
	if (maxfd < maxrun)
		maxrun = maxfd;
	if (maxrun < 4)
		maxrun = 4;		/* sanity check */

	mntarray = make_mntarray(mntlist, count);

	if (listlength == 0) {
		if (count == 0)		/* not an error, just none found */
			return (0);
		fprintf(stderr, gettext("%s: no valid entries found in %s\n"),
				myname, mnttab);
		return (1);
	}

	/*
	 * Sort the entries based on their mount level only if lofs's are
	 * not present.
	 */
	if (lofscnt == 0) {
		qsort((void *)mntarray, listlength, sizeof (mountent_t *),
			mcompar);
		/*
		 * If we do not detect a lofs by now, we never will.
		 */
		lofscnt = -1;
	}
	/*
	 * Now link them up so that a given pid is easier to find when
	 * we go to clean up after they are done.
	 */
	mntll = mntarray[0];
	for (ml = mntarray; mp = *ml; /* */)
		mp->link = *++ml;

	/*
	 * Try to handle interrupts in a reasonable way.
	 */
	sigset(SIGHUP, cleanup);
	sigset(SIGQUIT, cleanup);
	sigset(SIGINT, cleanup);

	do_umounts(mntarray);	/* do the umounts */
	return (exitcode);
}

/*
 * Returns a mountent_t array based on mntlist.  If mntlist is NULL, then
 * it returns all mnttab entries with a few exceptions.  Sets the global
 * variable listlength to the number of entries in the array.
 */
mountent_t **
make_mntarray(char **mntlist, int count)
{
	mountent_t 	*mp, **mpp;
	int 		ndx;
	char		*cp;

	if (count > 0)
		listlength = count;

	mpp = (mountent_t **)malloc(sizeof (*mp) * (listlength + 1));
	if (mpp == NULL)
		nomem();

	if (count == 0) {
		if (mntll == NULL) {	/* no entries? */
			listlength = 0;
			return (NULL);
		}
		/*
		 * No mount list specified: take all mnttab mount points
		 * except for a few cases.
		 */
		for (ndx = 0, mp = mntll; mp; mp = mp->link) {
			if (fsstrinlist(mp->ment.mnt_mountp, keeplist))
				continue;
			mp->mlevel = fsgetmlevel(mp->ment.mnt_mountp);
			if (mp->ment.mnt_fstype &&
			    (strcmp(mp->ment.mnt_fstype, MNTTYPE_LOFS) == 0))
				lofscnt++;

			mpp[ndx++] = mp;
		}
		mpp[ndx] = NULL;
		listlength = ndx;
		return (mpp);
	}

	/*
	 * A list of mount points was specified on the command line.
	 * Build an array out of these.
	 */
	for (ndx = 0; count--; ) {
		cp = *mntlist++;
		if (realpath(cp, resolve) == NULL) {
			fprintf(stderr,
				gettext("%s: warning: can't resolve %s\n"),
				myname, cp);
			exitcode = 1;
			mp = getmntlast(mntll, NULL, cp); /* try anyways */
		} else
			mp = getmntlast(mntll, NULL, resolve);
		if (mp == NULL) {
			struct mnttab mnew;
			/*
			 * Then we've reached the end without finding
			 * what we are looking for, but we still have to
			 * try to umount it: append it to mntarray.
			 */
			fprintf(stderr, gettext(
				"%s: warning: %s not found in %s\n"),
				myname, resolve, mnttab);
			exitcode = 1;
			mntnull(&mnew);
			mnew.mnt_special = mnew.mnt_mountp = strdup(resolve);
			if (mnew.mnt_special == NULL)
				nomem();
			mp = new_mountent(&mnew);
		}
		if (mp->ment.mnt_fstype &&
		    (strcmp(mp->ment.mnt_fstype, MNTTYPE_LOFS) == 0))
			lofscnt++;

		mp->mlevel = fsgetmlevel(mp->ment.mnt_mountp);
		mpp[ndx++] = mp;
	}
	mpp[ndx] = NULL;
	listlength = ndx;
	return (mpp);
}

/*
 * Returns the tail of a linked list of all mnttab entries.  I.e, it's faster
 * to return the mnttab in reverse order.
 * Sets listlength to the number of entries in the list.
 * Returns NULL if none are found.
 */
mountent_t *
getmntall(void)
{
	FILE		*fp;
	mountent_t	*mtail;
	int		cnt = 0, ret;
	struct mnttab	mget;

	if ((fp = fopen(mnttab, "r")) == NULL) {
		fprintf(stderr, gettext("%s: warning cannot open %s\n"),
				myname, mnttab);
		return (0);
	}
	mtail = NULL;

	while ((ret = getmntent(fp, &mget)) != -1) {
		mountent_t	*mp;

		if (ret > 0) {
			mnterror(ret);
			continue;
		}

		mp = new_mountent(&mget);
		mp->link = mtail;
		mtail = mp;
		cnt++;
	}
	fclose(fp);
	if (mtail == NULL) {
		listlength = 0;
		return (NULL);
	}
	listlength = cnt;
	return (mtail);
}

void
do_umounts(mountent_t **mntarray)
{
	mountent_t *mp, *mpprev, **ml = mntarray;
	int	cnt = listlength;

	/*
	 * Main loop for the forked children:
	 */
	for (mpprev = *ml; mp = *ml; mpprev = mp, ml++, cnt--) {
		pid_t	pid;

		/*
		 * Check to see if we cross a mount level: e.g.,
		 * /a/b/c -> /a/b.  If so, we need to wait for all current
		 * umounts to finish before umounting the rest.
		 *
		 * Also, we unmount serially as long as there are lofs's
		 * to mount to avoid improper umount ordering.
		 */
		if (mp->mlevel < mpprev->mlevel || lofscnt > 0)
			while (nrun > 0 && (dowait() != -1))
				;

		if (lofscnt == 0) {
			/*
			 * We can now go to parallel umounting.
			 */
			qsort((void *)ml, cnt, sizeof (mountent_t *), mcompar);
			mp = *ml;	/* possible first entry */
			lofscnt--;	/* so we don't do this again */
		}

		while (setup_iopipe(mp) == -1 && (dowait() != -1))
			;

		while (nrun >= maxrun && (dowait() != -1))	/* throttle */
			;

#ifdef CACHEFS_BUG
		/*
		 * If this is the back file system, then let cachefs/umount
		 * unmount it.
		 */
		if (strstr(mp->ment.mnt_mountp, BACKMNT_NAME))
			continue;


		if (mp->ment.mnt_fstype &&
		    (strcmp(mp->ment.mnt_fstype, "cachefs") == 0)) {
			while (cachefs_running && (dowait() != -1))
					;
			cachefs_running = 1;
		}
#endif

		if ((pid = fork()) == -1) {
			perror("fork");
			cleanup(-1);
			/* not reached */
		}
#ifdef DEBUG
		if (dflg && pid > 0) {
			fprintf(stderr, "parent %d: umounting %d %s\n",
				getpid(), pid, mp->ment.mnt_mountp);
		}
#endif
		if (pid == 0) {		/* child */
			signal(SIGHUP, SIG_IGN);
			signal(SIGQUIT, SIG_IGN);
			signal(SIGINT, SIG_IGN);
			setup_output(mp);
			doexec(&mp->ment);
			perror("exec");
			exit(1);
		}

		/* parent */
		(void) close(mp->sopipe[WRPIPE]);
		(void) close(mp->sepipe[WRPIPE]);
		mp->pid = pid;
		nrun++;
	}
	cleanup(0);
}

/*
 * cleanup the existing children and exit with an error
 * if asig != 0.
 */
void
cleanup(int asig)
{
	/*
	 * Let the stragglers finish.
	 */
	while (nrun > 0 && (dowait() != -1))
		;
	if (asig != 0)
		exit(1);
}


/*
 * Waits for 1 child to die.
 *
 * Returns -1 if no children are left to wait for.
 * Returns 0 if a child died without an error.
 * Returns 1 if a child died with an error.
 * Sets the global exitcode if an error occurred.
 */
int
dowait(void)
{
	int		wstat, child, ret;
	mountent_t 	*mp, *prevp;

	if ((child = wait(&wstat)) == -1)
		return (-1);

	if (WIFEXITED(wstat))		/* this should always be true */
		ret = WEXITSTATUS(wstat);
	else
		ret = 1;		/* assume some kind of error */
	nrun--;
	if (ret)
		exitcode = 1;

	/*
	 * Find our child so we can process its std output, if any.
	 * This search gets smaller and smaller as children are cleaned
	 * up.
	 */
	for (prevp = NULL, mp = mntll; mp; mp = mp->link) {
		if (mp->pid != child) {
			prevp = mp;
			continue;
		}
		/*
		 * Found: let's remove it from this list.
		 */
		if (prevp) {
			prevp->link = mp->link;
			mp->link = NULL;
		}
		break;
	}

	if (mp == NULL) {
		/*
		 * This should never happen.
		 */
#ifdef DEBUG
		fprintf(stderr, gettext(
			"%s: unknown child %d\n"), myname, child);
#endif
		exitcode = 1;
		return (1);
	}
	doio(mp);	/* Any output? */

	if (mp->ment.mnt_fstype &&
	    (strcmp(mp->ment.mnt_fstype, MNTTYPE_LOFS) == 0))
		lofscnt--;

#ifdef CACHEFS_BUG
	if (mp->ment.mnt_fstype &&
	    (strcmp(mp->ment.mnt_fstype, "cachefs") == 0))
		cachefs_running = 0;
#endif

	return (ret);
}

static const mountent_t zmount = { 0 };

mountent_t *
new_mountent(struct mnttab *ment)
{
	mountent_t *new;

	new = (mountent_t *)malloc(sizeof (*new));
	if (new == NULL)
		nomem();

	*new = zmount;
	if (ment->mnt_special &&
	    (new->ment.mnt_special = strdup(ment->mnt_special)) == NULL)
		nomem();
	if (ment->mnt_mountp &&
	    (new->ment.mnt_mountp = strdup(ment->mnt_mountp)) == NULL)
		nomem();
	if (ment->mnt_fstype &&
	    (new->ment.mnt_fstype = strdup(ment->mnt_fstype)) == NULL)
		nomem();
	return (new);
}


/*
 * Sort in descending order of "mount level".  For example, /a/b/c is
 * placed before /a/b .
 */
int
mcompar(const void *a, const void *b)
{
	mountent_t *a1, *b1;

	a1 = *(mountent_t **)a;
	b1 = *(mountent_t **)b;
	return (b1->mlevel - a1->mlevel);
}

/*
 * The purpose of this routine is to form stdout and stderr
 * pipes for the children's output.  The parent then reads and writes it
 * out it serially in order to ensure that the output is
 * not garbled.
 */

int
setup_iopipe(mountent_t *mp)
{
	/*
	 * Make a stdout and stderr pipe.  This should never fail.
	 */
	if (pipe(mp->sopipe) == -1)
		return (-1);
	if (pipe(mp->sepipe) == -1) {
		(void) close(mp->sopipe[RDPIPE]);
		(void) close(mp->sopipe[WRPIPE]);
		return (-1);
	}
	/*
	 * Don't block on an empty pipe.
	 */
	(void) fcntl(mp->sopipe[RDPIPE], F_SETFL, O_NDELAY|O_NONBLOCK);
	(void) fcntl(mp->sepipe[RDPIPE], F_SETFL, O_NDELAY|O_NONBLOCK);
	return (0);
}

/*
 * Called by a child to attach its stdout and stderr to the write side of
 * the pipes.
 */
void
setup_output(mountent_t *mp)
{
	(void) close(fileno(stdout));
	(void) dup(mp->sopipe[WRPIPE]);
	(void) close(mp->sopipe[WRPIPE]);

	(void) close(fileno(stderr));
	(void) dup(mp->sepipe[WRPIPE]);
	(void) close(mp->sepipe[WRPIPE]);
}

/*
 * Parent uses this to print any stdout or stderr output issued by
 * the child.
 */
static void
doio(mountent_t *mp)
{
	int bytes;

	while ((bytes = read(mp->sepipe[RDPIPE], ibuf, sizeof (ibuf))) > 0)
		write(fileno(stderr), ibuf, bytes);
	while ((bytes = read(mp->sopipe[RDPIPE], ibuf, sizeof (ibuf))) > 0)
		write(fileno(stdout), ibuf, bytes);

	(void) close(mp->sopipe[RDPIPE]);
	(void) close(mp->sepipe[RDPIPE]);
}

void
nomem(void)
{
	fprintf(stderr, gettext("%s: out of memory\n"), myname);
	/*
	 * Let the stragglers finish.
	 */
	while (nrun > 0 && (dowait() != -1))
		;
	exit(1);
}