|
root / base / usr / src / cmd / fs.d / mount.c
mount.c C 1640 lines 35.9 KB
   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
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (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 (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved	*/


/*
 * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 *
 * Copyright 2019 Nexenta by DDN, Inc. All rights reserved.
 */

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

#define	VFS_PATH	"/usr/lib/fs"
#define	ALT_PATH	"/etc/fs"
#define	REMOTE		"/etc/dfs/fstypes"

#define	ARGV_MAX	16
#define	TIME_MAX	50
#define	FSTYPE_MAX	8
#define	REMOTE_MAX	64

#define	OLD	0
#define	NEW	1

#define	READONLY	0
#define	READWRITE	1
#define	SUID		2
#define	NOSUID		3
#define	SETUID		4
#define	NOSETUID	5
#define	DEVICES		6
#define	NODEVICES	7

#define	FORMAT	"%a %b %e %H:%M:%S %Y\n"	/* date time format */
				/* a - abbreviated weekday name */
				/* b - abbreviated month name */
				/* e - day of month */
				/* H - hour */
				/* M - minute */
				/* S - second */
				/* Y - Year */
				/* n - newline */

/*
 * The fs-local method understands this exit code to mean that one or
 * more failures occurred and that all the failures were of attempted
 * lofs mounts.
 */
#define	ALL_LOFS_FAILURES	111

extern int	optind;
extern char	*optarg;

extern void	usage(void);
extern char	*flags(char *, int);
extern char	*remote(char *, FILE *);
extern char	*default_fstype(char *);

char	*myopts[] = {
	MNTOPT_RO,
	MNTOPT_RW,
	MNTOPT_SUID,
	MNTOPT_NOSUID,
	MNTOPT_SETUID,
	MNTOPT_NOSETUID,
	MNTOPT_DEVICES,
	MNTOPT_NODEVICES,
	NULL
};

static char	*myname;		/* point to argv[0] */

/*
 * Set the limit to double the number of characters a user should be allowed to
 * type in one line.
 * This should cover the different shells, which don't use POSIX_MAX_INPUT,
 * and should cover the case where a long option string can be in
 * the /etc/vfstab file.
 */
char	mntflags[(_POSIX_MAX_INPUT+1) * 2];

char	realdir[MAXPATHLEN];	/* buffer for realpath() calls */
char	*vfstab = VFSTAB;
char	*mnttab = MNTTAB;
char	*specific_opts;		/* holds specific mount options */
char	*generic_opts;		/* holds generic mount options */
int	maxrun;
int	nrun;
int	failcnt;		/* total count of failures */
int	lofscnt;		/* presence of lofs prohibits parallel */
				/* mounting */
int	lofsfail;		/* count of failures of lofs mounts */
int	exitcode;
int	aflg, cflg, fflg, Fflg, gflg, oflg, pflg, rflg, vflg, Vflg, mflg, Oflg,
	dashflg, questflg, dflg, qflg;


/*
 * Each vfsent_t describes a vfstab entry.  It is used to manage and cleanup
 * each child that performs the particular mount for the entry.
 */

typedef struct vfsent {
	struct vfstab	v;		/* the vfstab entry */
	char		*rpath;		/* resolved pathname so far */
	int		mlevel;		/* how deep is this mount point */
	int		order;		/* vfstab serial order of this vfs */
	int		flag;
	pid_t		pid;		/* the pid of this mount process */
	int		exitcode;	/* process's exitcode */
#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 vfsent	*next;		/* used when in linked list */
} vfsent_t;

#define	VRPFAILED	0x01		/* most recent realpath failed on */
					/* this mount point */
#define	VNOTMOUNTED	0x02		/* mount point could not be mounted */

vfsent_t	*vfsll, *vfslltail;	/* head and tail of the global */
					/* linked list of vfstab entries */
vfsent_t	**vfsarray;		/* global array of vfsent_t's */
int		vfsarraysize;		/* length of the list */

/*
 * This structure is used to build a linked list of
 * mnttab structures from /etc/mnttab.
 */
typedef struct mountent {
	struct extmnttab	*ment;
	int		flag;
	struct mountent	*next;
} mountent_t;

#define	MSORTED		0x1

static vfsent_t **make_vfsarray(char **, int);
static vfsent_t	*new_vfsent(struct vfstab *, int);
static vfsent_t *getvfsall(char *, int);

static void	doexec(char *, char **);
static void	nomem();
static void	cleanup(int);
static char	*setrpath(vfsent_t *);
static int	dowait();
static int	setup_iopipe(vfsent_t *);
static void	setup_output(vfsent_t *);
static void	doio(vfsent_t *);
static void	do_mounts();
static int	parmount(char **, int, char *);
static int	mlevelcmp(const void *, const void *);
static int	mordercmp(const void *, const void *);
static int	check_fields(char *, char *);
static int	cleanupkid(pid_t, int);
static void	print_mnttab(int, int);
static void	vfserror(int, char *);
static void	mnterror(int);
static int	ignore(char *);

/*
 * This is /sbin/mount: the generic command that in turn
 * execs the appropriate /usr/lib/fs/{fstype}/mount.
 * The -F flag and argument are NOT passed.
 * If the usr file system is not mounted a duplicate copy
 * can be found in /sbin and this version execs the
 * appropriate /etc/fs/{fstype}/mount
 *
 * If the -F fstype, special or directory are missing,
 * /etc/vfstab is searched to fill in the missing arguments.
 *
 * -V will print the built command on the stdout.
 * It isn't passed either.
 */
int
main(int argc, char *argv[])
{
	char	*special,	/* argument of special/resource */
	    *mountp,		/* argument of mount directory */
	    *fstype,		/* wherein the fstype name is filled */
	    *newargv[ARGV_MAX],	/* arg list for specific command */
	    *farg = NULL, *Farg = NULL;
	int	ii, ret, cc, fscnt;
	struct stat64	stbuf;
	struct vfstab	vget, vref;
	mode_t mode;
	FILE	*fd;

	(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];
	if (myname == 0) myname = "path unknown";

	/* Process the args.  */

	while ((cc = getopt(argc, argv, "?acd:f:F:gmno:pqrvVO")) != -1)
		switch (cc) {
			case 'a':
				aflg++;
				break;
			case 'c':
				cflg++;
				break;

#ifdef DEBUG
			case 'd':
				dflg = atoi(optarg);
				break;
#endif

			case 'f':
				fflg++;
				farg = optarg;
				break;
			case 'F':
				Fflg++;
				Farg = optarg;
				break;
			case 'g':
				gflg++;
				break;
			case 'm':
				mflg++;
				break; /* do not update /etc/mnttab */
			case 'o':
				oflg++;
				if ((specific_opts = strdup(optarg)) == NULL)
					nomem();
				break; /* fstype dependent options */
			case 'O':
				Oflg++;
				break;
			case 'p':
				pflg++;
				break;
			case 'q':
				qflg++;
				break;
			case 'r':
				rflg++;
				generic_opts = "ro";
				break;
			case 'v':
				vflg++;
				break;
			case 'V':
				Vflg++;
				break;
			case '?':
				questflg++;
				break;
		}

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

	/* option checking */
	/* more than two args not allowed if !aflg */
	if (!aflg && (argc - optind > 2))
		usage();

	/* pv mututally exclusive */
	if (pflg + vflg + aflg > 1) {
		fprintf(stderr, gettext
		    ("%s: -a, -p, and -v are mutually exclusive\n"),
		    myname);
		usage();
	}

	/*
	 * Can't have overlaying mounts on the same mount point during
	 * a parallel mount.
	 */
	if (aflg && Oflg) {
		fprintf(stderr, gettext
		    ("%s: -a and -O are mutually exclusive\n"), myname);
		usage();
	}

	/* dfF mutually exclusive */
	if (fflg + Fflg > 1) {
		fprintf(stderr, gettext
		    ("%s: More than one FSType specified\n"), myname);
		usage();
	}

	/* no arguments, only allow p,v,V or [F]? */
	if (!aflg && optind == argc) {
		if (cflg || fflg || mflg || oflg || rflg || qflg)
			usage();

		if (Fflg && !questflg)
			usage();

		if (questflg) {
			if (Fflg) {
				newargv[2] = "-?";
				newargv[3] = NULL;
				doexec(Farg, newargv);
			}
			usage();
		}
	}

	if (questflg)
		usage();

	/* one or two args, allow any but p,v */
	if (optind != argc && (pflg || vflg)) {
		fprintf(stderr,
gettext("%s: Cannot use -p and -v with arguments\n"), myname);
		usage();
	}


	/* if only reporting mnttab, generic prints mnttab and exits */
	if (!aflg && optind == argc) {
		if (Vflg) {
			printf("%s", myname);
			if (pflg)
				printf(" -p");
			if (vflg)
				printf(" -v");
			printf("\n");
			exit(0);
		}

		print_mnttab(vflg, pflg);
		exit(0);
	}

	/*
	 * Get filesystem type here.  If "-F FStype" is specified, use
	 * that fs type.  Otherwise, determine the fs type from /etc/vfstab
	 * if the entry exists.  Otherwise, determine the local or remote
	 * fs type from /etc/default/df or /etc/dfs/fstypes respectively.
	 */
	if (fflg) {
		if ((strcmp(farg, "S51K") != 0) &&
		    (strcmp(farg, "S52K") != 0)) {
			fstype = farg;
		}
		else
			fstype = "ufs";
	} else /* if (Fflg) */
		fstype = Farg;

	fscnt = argc - optind;
	if (aflg && (fscnt != 1))
		exit(parmount(argv + optind, fscnt, fstype));

	/*
	 * Then don't bother with the parallel over head.  Everything
	 * from this point is simple/normal single execution.
	 */
	aflg = 0;

	/* get special and/or mount-point from arg(s) */
	if (fscnt == 2)
		special = argv[optind++];
	else
		special = NULL;
	if (optind < argc)
		mountp = argv[optind++];
	else
		mountp = NULL;

	/* lookup only if we need to */
	if (fstype == NULL || specific_opts == NULL || special == NULL ||
	    mountp == NULL) {
		if ((fd = fopen(vfstab, "r")) == NULL) {
			if (fstype == NULL || special == NULL ||
			    mountp == NULL) {
				fprintf(stderr, gettext(
				    "%s: Cannot open %s\n"),
				    myname, vfstab);
				exit(1);
			} else {
				/*
				 * No vfstab, but we know what we want
				 * to mount.
				 */
				goto out;
			}
		}
		vfsnull(&vref);
		vref.vfs_special = special;
		vref.vfs_mountp = mountp;
		vref.vfs_fstype = fstype;

		/* get a vfstab entry matching mountp or special */
		while ((ret = getvfsany(fd, &vget, &vref)) > 0)
			vfserror(ret, vget.vfs_special);

		/* if no entry and there was only one argument */
		/* then the argument could be the special */
		/* and not mount point as we thought earlier */
		if (ret == -1 && special == NULL) {
			rewind(fd);
			special = vref.vfs_special = mountp;
			mountp = vref.vfs_mountp = NULL;
			/* skip erroneous lines; they were reported above */
			while ((ret = getvfsany(fd, &vget, &vref)) > 0)
				;
		}

		fclose(fd);

		if (ret == 0) {
			if (fstype == NULL)
				fstype = vget.vfs_fstype;
			if (special == NULL)
				special = vget.vfs_special;
			if (mountp == NULL)
				mountp = vget.vfs_mountp;
			if (oflg == 0 && vget.vfs_mntopts) {
				oflg++;
				specific_opts = vget.vfs_mntopts;
			}
		} else if (special == NULL) {
			if (stat64(mountp, &stbuf) == -1) {
				fprintf(stderr, gettext("%s: cannot stat %s\n"),
				    myname, mountp);
				exit(2);
			}
			if (((mode = (stbuf.st_mode & S_IFMT)) == S_IFBLK) ||
			    (mode == S_IFCHR)) {
				fprintf(stderr,
gettext("%s: mount point cannot be determined\n"),
				    myname);
				exit(1);
			} else
				{
				fprintf(stderr,
gettext("%s: special cannot be determined\n"),
				    myname);
				exit(1);
			}
		} else if (fstype == NULL)
			fstype = default_fstype(special);
	}

out:
	if (realpath(mountp, realdir) == NULL) {
		(void) fprintf(stderr, "mount: ");
		perror(mountp);
		exit(1);
	}

	if ((mountp = strdup(realdir)) == NULL)
		nomem();

	if (check_fields(fstype, mountp))
		exit(1);

	/* create the new arg list, and end the list with a null pointer */
	ii = 2;
	if (cflg)
		newargv[ii++] = "-c";
	if (gflg)
		newargv[ii++] = "-g";
	if (mflg)
		newargv[ii++] = "-m";
	/*
	 * The q option needs to go before the -o option as some
	 * filesystems complain during first pass option parsing.
	 */
	if (qflg)
		newargv[ii++] = "-q";
	if (oflg) {
		newargv[ii++] = "-o";
		newargv[ii++] = specific_opts;
	}
	if (Oflg)
		newargv[ii++] = "-O";
	if (rflg)
		newargv[ii++] = "-r";
	if (dashflg)
		newargv[ii++] = "--";
	newargv[ii++] = special;
	newargv[ii++] = mountp;
	newargv[ii] = NULL;

	doexec(fstype, newargv);
	return (0);
}

void
usage(void)
{
	fprintf(stderr,	gettext("Usage:\n%s [-v | -p]\n"), myname);
	fprintf(stderr, gettext(
	    "%s [-F FSType] [-V] [current_options] [-o specific_options]"),
	    myname);
	fprintf(stderr, gettext("\n\t{special | mount_point}\n"));

	fprintf(stderr, gettext(
	    "%s [-F FSType] [-V] [current_options] [-o specific_options]"),
	    myname);
	fprintf(stderr, gettext("\n\tspecial mount_point\n"));

	fprintf(stderr, gettext(
	"%s -a [-F FSType ] [-V] [current_options] [-o specific_options]\n"),
	    myname);
	fprintf(stderr, gettext("\t[mount_point ...]\n"));

	exit(1);
}

/*
 * Get rid of "dev=[hex string]" clause, if any.  It's not legal
 * when printing in vfstab format.
 */
void
elide_dev(char *mntopts)
{
	char *dev, *other;

	if (mntopts != NULL) {
		dev = strstr(mntopts, "dev=");
		if (dev != NULL) {
			other = strpbrk(dev, ",");
			if (other == NULL) {
				/* last option */
				if (dev != mntopts) {
					*--dev = '\0';
				} else {
					*dev = '\0';
				}
			} else {
				/* first or intermediate option */
				memmove(dev, other+1, strlen(other+1)+1);
			}
		}
	}
}

void
print_mnttab(int vflg, int pflg)
{
	FILE	*fd;
	FILE	*rfp;			/* this will be NULL if fopen fails */
	int	ret;
	char	time_buf[TIME_MAX];	/* array to hold date and time */
	struct extmnttab	mget;
	time_t	ltime;

	if ((fd = fopen(mnttab, "r")) == NULL) {
		fprintf(stderr, gettext("%s: Cannot open mnttab\n"), myname);
		exit(1);
	}
	rfp = fopen(REMOTE, "r");
	while ((ret = getextmntent(fd, &mget, sizeof (struct extmnttab)))
	    == 0) {
		if (ignore(mget.mnt_mntopts))
			continue;
		if (mget.mnt_special && mget.mnt_mountp &&
		    mget.mnt_fstype && mget.mnt_time) {
			ltime = atol(mget.mnt_time);
			cftime(time_buf, FORMAT, &ltime);
			if (pflg) {
				elide_dev(mget.mnt_mntopts);
				printf("%s - %s %s - no %s\n",
				    mget.mnt_special,
				    mget.mnt_mountp,
				    mget.mnt_fstype,
				    mget.mnt_mntopts != NULL ?
				    mget.mnt_mntopts : "-");
			} else if (vflg) {
				printf("%s on %s type %s %s%s on %s",
				    mget.mnt_special,
				    mget.mnt_mountp,
				    mget.mnt_fstype,
				    remote(mget.mnt_fstype, rfp),
				    flags(mget.mnt_mntopts, NEW),
				    time_buf);
			} else
				printf("%s on %s %s%s on %s",
				    mget.mnt_mountp,
				    mget.mnt_special,
				    remote(mget.mnt_fstype, rfp),
				    flags(mget.mnt_mntopts, OLD),
				    time_buf);
		}
	}
	if (ret > 0)
		mnterror(ret);
}

char	*
flags(char *mntopts, int flag)
{
	char	opts[sizeof (mntflags)];
	char	*value;
	int	rdwr = 1;
	int	suid = 1;
	int	devices = 1;
	int	setuid = 1;

	if (mntopts == NULL || *mntopts == '\0')
		return ("read/write/setuid/devices");

	strcpy(opts, "");
	while (*mntopts != '\0')  {
		switch (getsubopt(&mntopts, myopts, &value)) {
		case READONLY:
			rdwr = 0;
			break;
		case READWRITE:
			rdwr = 1;
			break;
		case SUID:
			suid = 1;
			break;
		case NOSUID:
			suid = 0;
			break;
		case SETUID:
			setuid = 1;
			break;
		case NOSETUID:
			setuid = 0;
			break;
		case DEVICES:
			devices = 1;
			break;
		case NODEVICES:
			devices = 0;
			break;
		default:
			/* cat '/' separator to mntflags */
			if (*opts != '\0' && value != NULL)
				strcat(opts, "/");
			strcat(opts, value);
			break;
		}
	}

	strcpy(mntflags, "");
	if (rdwr)
		strcat(mntflags, "read/write");
	else if (flag == OLD)
		strcat(mntflags, "read only");
	else
		strcat(mntflags, "read-only");
	if (suid) {
		if (setuid)
			strcat(mntflags, "/setuid");
		else
			strcat(mntflags, "/nosetuid");
		if (devices)
			strcat(mntflags, "/devices");
		else
			strcat(mntflags, "/nodevices");
	} else {
		strcat(mntflags, "/nosetuid/nodevices");
	}
	if (*opts != '\0') {
		strcat(mntflags, "/");
		strcat(mntflags, opts);
	}

	/*
	 * The assumed assertion
	 *	assert (strlen(mntflags) < sizeof mntflags);
	 * is valid at this point in the code. Note that a call to "assert"
	 * is not appropriate in production code since it halts the program.
	 */
	return (mntflags);
}

char	*
remote(char *fstype, FILE *rfp)
{
	char	buf[BUFSIZ];
	char	*fs;
	extern char *strtok();

	if (rfp == NULL || fstype == NULL ||
	    strlen(fstype) > (size_t)FSTYPE_MAX)
		return ("");	/* not a remote */
	rewind(rfp);
	while (fgets(buf, sizeof (buf), rfp) != NULL) {
		fs = strtok(buf, " \t\n");
		if (strcmp(fstype, fs) == 0)
			return ("remote/");	/* is a remote fs */
	}
	return ("");	/* not a remote */
}


void
vfserror(int flag, char *special)
{
	if (special == NULL)
		special = "<null>";
	switch (flag) {
	case VFS_TOOLONG:
		fprintf(stderr,
gettext("%s: Warning: Line in vfstab for \"%s\" exceeds %d characters\n"),
		    myname, special, VFS_LINE_MAX-1);
		break;
	case VFS_TOOFEW:
		fprintf(stderr,
gettext("%s: Warning: Line for \"%s\" in vfstab has too few entries\n"),
		    myname, special);
		break;
	case VFS_TOOMANY:
		fprintf(stderr,
gettext("%s: Warning: Line for \"%s\" in vfstab has too many entries\n"),
		    myname, special);
		break;
	default:
		fprintf(stderr, gettext(
		    "%s: Warning: Error in line for \"%s\" in vfstab\n"),
		    myname, special);
	}
}

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;
	case MNT_TOOMANY:
		fprintf(stderr,
		    gettext("%s: Line in mnttab has too many entries\n"),
		    myname);
		break;
	}
	exit(1);
}

void
doexec(char *fstype, char *newargv[])
{
	char	full_path[PATH_MAX];
	char	alter_path[PATH_MAX];
	char	*vfs_path = VFS_PATH;
	char	*alt_path = ALT_PATH;
	int	i;

	/* build the full pathname of the fstype dependent command. */
	sprintf(full_path, "%s/%s/%s", vfs_path, fstype, myname);
	sprintf(alter_path, "%s/%s/%s", alt_path, fstype, myname);
	newargv[1] = myname;

	if (Vflg) {
		printf("%s -F %s", newargv[1], fstype);
		for (i = 2; newargv[i]; i++)
			printf(" %s", newargv[i]);
		printf("\n");
		fflush(stdout);
		exit(0);
	}

	/*
	 * Some file system types need pfexec.
	 */
	if (strcmp(fstype, "smbfs") == 0 &&
	    setpflags(PRIV_PFEXEC, 1) != 0) {
		(void) fprintf(stderr,
		    gettext("mount: unable to set PFEXEC flag: %s\n"),
		    strerror(errno));
		exit(1);
	}

	/*
	 * Try to exec the fstype dependent portion of the mount.
	 * See if the directory is there before trying to exec dependent
	 * portion.  This is only useful for eliminating the
	 * '..mount: not found' message when '/usr' is mounted
	 */
	if (access(full_path, 0) == 0) {
		execv(full_path, &newargv[1]);
		if (errno == EACCES) {
			fprintf(stderr,
			gettext("%s: Cannot execute %s - permission denied\n"),
			    myname, full_path);
		}
		if (errno == ENOEXEC) {
			newargv[0] = "sh";
			newargv[1] = full_path;
			execv("/sbin/sh", &newargv[0]);
		}
	}
	execv(alter_path, &newargv[1]);
	if (errno == EACCES) {
		fprintf(stderr, gettext(
		    "%s: Cannot execute %s - permission denied\n"),
		    myname, alter_path);
		exit(1);
	}
	if (errno == ENOEXEC) {
		newargv[0] = "sh";
		newargv[1] = alter_path;
		execv("/sbin/sh", &newargv[0]);
	}
	fprintf(stderr,
	    gettext("%s: Operation not applicable to FSType %s\n"),
	    myname, fstype);
	exit(1);
}

char *mntopts[] = { MNTOPT_IGNORE, NULL };
#define	IGNORE    0

/*
 * Return 1 if "ignore" appears in the options string
 */
int
ignore(char *opts)
{
	char *value;
	char *saveptr, *my_opts;
	int rval = 0;

	if (opts == NULL || *opts == '\0')
		return (0);

	/*
	 * we make a copy of the option string to pass to getsubopt(),
	 * because getsubopt() modifies the string.  We also save
	 * the original pointer returned by strdup, because getsubopt
	 * changes the pointer passed into it.  If strdup fails (unlikely),
	 * we act as if the "ignore" option isn't set rather than fail.
	 */

	if ((saveptr = my_opts = strdup(opts)) == NULL)
		nomem();

	while (*my_opts != '\0') {
		if (getsubopt(&my_opts, mntopts, &value) == IGNORE)
			rval = 1;
	}

	free(saveptr);

	return (rval);
}

/*
 * Perform the parallel version of mount.  If count == 0, mount all
 * vfstab filesystems with the automnt field == "yes".  Use fstype if
 * supplied.  If mntlist supplied, then attempt to only mount those.
 */

int
parmount(char **mntlist, int count, char *fstype)
{
	int		maxfd =	OPEN_MAX;
	struct		rlimit rl;
	vfsent_t	**vl, *vp;

	/*
	 * Process scaling.  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;
	}
	(void) enable_extended_FILE_stdio(-1, -1);

	/*
	 * 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 */

	if (count == 0)
		mntlist = NULL;		/* used as a flag later */
	else
		fstype = NULL;		/* mount points supplied: */
					/* ignore fstype */
	/*
	 * Read the whole vfstab into a linked list for quick processing.
	 * On average, this is the most efficient way to collect and
	 * manipulate the vfstab data.
	 */
	vfsll = getvfsall(fstype, mntlist == NULL);

	/*
	 * Make an array out of the vfs linked list for sorting purposes.
	 */
	if (vfsll == NULL ||
	    (vfsarray = make_vfsarray(mntlist, count)) == NULL) {
		if (mntlist == NULL)	/* not an error - just none found */
			return (0);

		fprintf(stderr, gettext("%s: No valid entries found in %s\n"),
		    myname, vfstab);
		return (1);
	}

	/*
	 * Sort the entries based on their resolved path names
	 *
	 * If an lofs is encountered, then the original order of the vfstab
	 * file needs to be maintained until we are done mounting lofs's.
	 */
	if (!lofscnt)
		qsort((void *)vfsarray, vfsarraysize, sizeof (vfsent_t *),
		    mlevelcmp);

	/*
	 * Shrink the vfsll linked list down to the new list.  This will
	 * speed up the pid search in cleanupkid() later.
	 */
	vfsll = vfsarray[0];
	for (vl = vfsarray; vp = *vl; )
		vp->next = *++vl;

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

	do_mounts();		/* do the mounts */

	if (failcnt > 0 && failcnt == lofsfail)
		return (ALL_LOFS_FAILURES);

	return (exitcode);
}

/*
 * Read all vstab (fp) entries into memory if fstype == NULL.
 * If fstype is specified, than read all those that match it.
 *
 * Returns a linked list.
 */
vfsent_t *
getvfsall(char *fstype, int takeall)
{
	vfsent_t	*vhead, *vtail;
	struct vfstab	vget;
	FILE		*fp;
	int		cnt = 0, ret;

	if ((fp = fopen(vfstab, "r")) == NULL) {
		fprintf(stderr, gettext("%s: Cannot open %s\n"),
		    myname, vfstab);
		exit(1);
	}

	vhead = vtail = NULL;

	while ((ret = getvfsent(fp, &vget)) != -1) {
		vfsent_t *vp;

		if (ret > 0) {
			vfserror(ret, vget.vfs_mountp);
			continue;
		}

		/*
		 * If mount points were not specified, then we ignore
		 * entries that aren't marked "yes".
		 */
		if (takeall &&
		    (vget.vfs_automnt == NULL ||
		    strcmp(vget.vfs_automnt, "yes")))
			continue;

		if (fstype && vget.vfs_fstype &&
		    strcmp(fstype, vget.vfs_fstype))
			continue;

		if (vget.vfs_mountp == NULL ||
		    (vget.vfs_fstype && (strcmp(vget.vfs_fstype, "swap") == 0)))
			continue;

		if (check_fields(vget.vfs_fstype, vget.vfs_mountp)) {
			exitcode = 1;
			continue;
		}

		vp = new_vfsent(&vget, cnt);	/* create new vfs entry */
		if (vhead == NULL)
			vhead = vp;
		else
			vtail->next = vp;
		vtail = vp;
		cnt++;
	}
	fclose(fp);
	if (vtail == NULL) {
		vfsarraysize = 0;
		vfslltail = NULL;
		return (NULL);
	}
	vtail->next = NULL;
	vfslltail = vtail;	/* save it in the global variable */
	vfsarraysize = cnt;
	return (vhead);
}


/*
 * Returns an array of vfsent_t's based on vfsll & mntlist.
 */
vfsent_t **
make_vfsarray(char **mntlist, int count)
{
	vfsent_t	*vp, *vmark, *vpprev, **vpp;
	int		ndx, found;

	if (vfsll == NULL)
		return (NULL);

	if (count > 0)
		vfsarraysize = count;

	vpp = (vfsent_t **)malloc(sizeof (*vpp) * (vfsarraysize + 1));
	if (vpp == NULL)
		nomem();

	if (mntlist == NULL) {
		/*
		 * No mount list specified: take all vfstab mount points.
		 */
		for (ndx = 0, vp = vfsll; vp; vp = vp->next) {
			(void) setrpath(vp);
			/*
			 * Sigh. lofs entries can complicate matters so much
			 * that the best way to avoid problems is to
			 * stop parallel mounting when an lofs is
			 * encountered, so we keep a count of how many
			 * there are.
			 * Fortunately this is rare.
			 */
			if (vp->v.vfs_fstype &&
			    (strcmp(vp->v.vfs_fstype, MNTTYPE_LOFS) == 0))
				lofscnt++;

			vpp[ndx++] = vp;
		}
		vpp[ndx] = NULL;
		return (vpp);
	}

	/*
	 * A list of mount points was specified on the command line
	 * and we need to search for each one.
	 */
	vpprev = vfslltail;
	vpprev->next = vfsll;	/* make a circle out of it */
	vmark = vp = vfsll;
	/*
	 * For each specified mount point:
	 */
	for (ndx = 0; *mntlist; mntlist++) {
		found = 0;
		/*
		 * Circle our entire linked list, looking for *mntlist.
		 */
		while (vp) {
			if (strcmp(*mntlist, vp->v.vfs_mountp) == 0) {
				vpp[ndx++] = vp;	/* found it. */
				(void) setrpath(vp);
				if (vp->v.vfs_fstype &&
				    (strcmp(vp->v.vfs_fstype,
				    MNTTYPE_LOFS) == 0))
					lofscnt++;

				if (vp == vpprev) {	/* list exhausted */
					vp = NULL;
					found++;
					break;
				}
				/*
				 * Remove it from the circular list.  vpprev
				 * remains unchanged.
				 */
				vp = vp->next;
				vpprev->next->next = NULL;
				vpprev->next = vp;
				/*
				 * Set vmark to the first elem that we check
				 * each time.
				 */
				vmark = vp;
				found++;
				break;
			}
			vpprev = vp;
			vp = vp->next;
			if (vp == vmark)	/* break out if we completed */
						/* the circle */
				break;
		}

		if (!found) {
			fprintf(stderr, gettext(
			    "%s: Warning: %s not found in %s\n"),
			    myname, *mntlist, vfstab);
			exitcode = 1;
		}
	}
	if (ndx == 0)
		return (NULL);

	vpp[ndx] = NULL;	/* null terminate the list */
	vfsarraysize = ndx;	/* adjust vfsarraysize */
	return (vpp);
}

/*
 * Performs the exec argument processing, all  of the child forking and
 * execing, and child cleanup.
 * Sets exitcode to non-zero if any errors occurred.
 */
void
do_mounts(void)
{
	int		i, isave, cnt;
	vfsent_t	*vp, *vpprev, **vl;
	char		*newargv[ARGV_MAX];
	pid_t		child;

	/*
	 * create the arg list once;  the only differences among
	 * the calls are the options, special and mountp fields.
	 */
	i = 2;
	if (cflg)
		newargv[i++] = "-c";
	if (gflg)
		newargv[i++] = "-g";
	if (mflg)
		newargv[i++] = "-m";
	if (Oflg)
		newargv[i++] = "-O";
	if (qflg)
		newargv[i++] = "-q";
	if (rflg)
		newargv[i++] = "-r";
	if (dashflg)
		newargv[i++] = "--";
	if (oflg) {
		newargv[i++] = "-o";
		newargv[i++] = specific_opts;
	}
	isave = i;

	/*
	 * Main loop for the mount processes
	 */
	vl = vfsarray;
	cnt = vfsarraysize;
	for (vpprev = *vl; vp = *vl; vpprev = vp, vl++, cnt--) {
		/*
		 * Check to see if we cross a mount level: e.g.,
		 * /a/b -> /a/b/c.  If so, we need to wait for all current
		 * mounts to finish, rerun realpath on the remaining mount
		 * points, and resort the list.
		 *
		 * Also, we mount serially as long as there are lofs's
		 * to mount to avoid improper mount ordering.
		 */
		if (vp->mlevel > vpprev->mlevel || lofscnt > 0) {
			vfsent_t **vlp;

			while (nrun > 0 && (dowait() != -1))
				;
			/*
			 * Gads! It's possible for real path mounts points to
			 * change after mounts are done at a lower mount
			 * level.
			 * Thus, we need to recalculate mount levels and
			 * resort the list from this point.
			 */
			for (vlp = vl; *vlp; vlp++)
				(void) setrpath(*vlp);
			/*
			 * Sort the remaining entries based on their newly
			 * resolved path names.
			 * Do not sort if we still have lofs's to mount.
			 */
			if (lofscnt == 0) {
				qsort((void *)vl, cnt, sizeof (vfsent_t *),
				    mlevelcmp);
				vp = *vl;
			}
		}

		if (vp->flag & VRPFAILED) {
			fprintf(stderr, gettext(
			    "%s: Nonexistent mount point: %s\n"),
			    myname, vp->v.vfs_mountp);
			vp->flag |= VNOTMOUNTED;
			exitcode = 1;
			continue;
		}

		/*
		 * If mount options were not specified on the command
		 * line, then use the ones found in the vfstab entry,
		 * if any.
		 */
		i = isave;
		if (!oflg && vp->v.vfs_mntopts) {
			newargv[i++] = "-o";
			newargv[i++] = vp->v.vfs_mntopts;
		}
		newargv[i++] = vp->v.vfs_special;
		newargv[i++] = vp->rpath;
		newargv[i] = NULL;

		/*
		 * This should never really fail.
		 */
		while (setup_iopipe(vp) == -1 && (dowait() != -1))
			;

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

		if ((child = fork()) == -1) {
			perror("fork");
			cleanup(-1);
			/* not reached */
		}
		if (child == 0) {		/* child */
			signal(SIGHUP, SIG_IGN);
			signal(SIGQUIT, SIG_IGN);
			signal(SIGINT, SIG_IGN);
			setup_output(vp);
			doexec(vp->v.vfs_fstype, newargv);
			perror("exec");
			exit(1);
		}

		/* parent */
		(void) close(vp->sopipe[WRPIPE]);
		(void) close(vp->sepipe[WRPIPE]);
		vp->pid = child;
		nrun++;
	}
	/*
	 * Mostly done by now - wait and clean up the stragglers.
	 */
	cleanup(0);
}


/*
 * Setup stdout and stderr pipes for the children's output.
 */
int
setup_iopipe(vfsent_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);
	/*
	 * Don't pass extra fds into children.
	 */
	(void) fcntl(mp->sopipe[RDPIPE], F_SETFD, FD_CLOEXEC);
	(void) fcntl(mp->sepipe[RDPIPE], F_SETFD, FD_CLOEXEC);

	return (0);
}

/*
 * Called by a child to attach its stdout and stderr to the write side of
 * the pipes.
 */
void
setup_output(vfsent_t *vp)
{

	(void) close(fileno(stdout));
	(void) dup(vp->sopipe[WRPIPE]);
	(void) close(vp->sopipe[WRPIPE]);

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

/*
 * Parent uses this to print any stdout or stderr output issued by
 * the child.
 */
static void
doio(vfsent_t *vp)
{
	int bytes;
	char ibuf[BUFSIZ];

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

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

/*
 * 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.
 */
int
dowait(void)
{
	int child, wstat;

	if ((child = wait(&wstat)) == -1)
		return (-1);
	nrun--;
	return (cleanupkid(child, wstat) != 0);
}

/*
 * Locates the child mount process represented by pid, outputs any io
 * it may have, and returns its exit code.
 * Sets the global exitcode if an error occurred.
 */
int
cleanupkid(pid_t pid, int wstat)
{
	vfsent_t *vp, *prevp;
	int ret;

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

	/*
	 * Find our child.
	 * This search gets smaller and smaller as children are cleaned
	 * up.
	 */
	for (prevp = NULL, vp = vfsll; vp; vp = vp->next) {
		if (vp->pid != pid) {
			prevp = vp;
			continue;
		}
		/*
		 * Found: let's remove it from this linked list.
		 */
		if (prevp) {
			prevp->next = vp->next;
			vp->next = NULL;
		}
		break;
	}

	if (vp == NULL) {
		/*
		 * This should never happen.
		 */
		fprintf(stderr, gettext(
		    "%s: Unknown child %d\n"), myname, pid);
		exitcode = 1;
		return (ret);
	}
	doio(vp);	/* Any output? */

	if (vp->v.vfs_fstype &&
	    (strcmp(vp->v.vfs_fstype, MNTTYPE_LOFS) == 0)) {
		lofscnt--;
		if (ret)
			lofsfail++;
	}

	vp->exitcode = ret;
	return (ret);
}


static vfsent_t zvmount = { 0 };

vfsent_t *
new_vfsent(struct vfstab *vin, int order)
{
	vfsent_t *new;

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

	*new = zvmount;
	if (vin->vfs_special &&
	    (new->v.vfs_special = strdup(vin->vfs_special)) == NULL)
		nomem();
	if (vin->vfs_mountp &&
	    (new->v.vfs_mountp = strdup(vin->vfs_mountp)) == NULL)
		nomem();
	if (vin->vfs_fstype &&
	    (new->v.vfs_fstype = strdup(vin->vfs_fstype)) == NULL)
		nomem();
	/*
	 * If specific mount options were specified on the command
	 * line, then use those.  Else, use the ones on the vfstab
	 * line, if any.  In other words, specific options on the
	 * command line override those in /etc/vfstab.
	 */
	if (oflg) {
		if ((new->v.vfs_mntopts = strdup(specific_opts)) == NULL)
			nomem();
	} else if (vin->vfs_mntopts &&
	    (new->v.vfs_mntopts = strdup(vin->vfs_mntopts)) == NULL)
			nomem();

	new->order = order;
	return (new);
}

/*
 * Runs realpath on vp's mount point, records success or failure,
 * resets the mount level based on the new realpath, and returns
 * realpath()'s return value.
 */
char *
setrpath(vfsent_t *vp)
{
	char *rp;

	if ((rp = realpath(vp->v.vfs_mountp, realdir)) == NULL)
		vp->flag |= VRPFAILED;
	else
		vp->flag &= ~VRPFAILED;

	if (vp->rpath)
		free(vp->rpath);
	if ((vp->rpath = strdup(realdir)) == NULL)
		nomem();
	vp->mlevel = fsgetmlevel(vp->rpath);
	return (rp);
}


/*
 * sort first by mlevel (1...N), then by vfstab order.
 */
int
mlevelcmp(const void *a, const void *b)
{
	vfsent_t *a1, *b1;
	int	lcmp;

	a1 = *(vfsent_t **)a;
	b1 = *(vfsent_t **)b;

	lcmp = a1->mlevel - b1->mlevel;
	if (lcmp == 0)
		lcmp = a1->order - b1->order;
	return (lcmp);
}

/* sort by vfstab order.  0..N */
static int
mordercmp(const void *a, const void *b)
{
	vfsent_t *a1, *b1;

	a1 = *(vfsent_t **)a;
	b1 = *(vfsent_t **)b;
	return (a1->order - b1->order);
}

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

	if (asig != 0)
		exit(1);
}


int
check_fields(char *fstype, char *mountp)
{
	struct stat64 stbuf;

	if (fstype == NULL) {
		fprintf(stderr,
		    gettext("%s: FSType cannot be determined\n"),
		    myname);
		return (1);
	}
	if (strlen(fstype) > (size_t)FSTYPE_MAX) {
		fprintf(stderr,
		    gettext("%s: FSType %s exceeds %d characters\n"),
		    myname, fstype, FSTYPE_MAX);
		return (1);
	}

	if (mountp == NULL) {
		fprintf(stderr,
		    gettext("%s: Mount point cannot be determined\n"),
		    myname);
		return (1);
	}
	if (*mountp != '/') {
		fprintf(stderr, gettext(
		    "%s: Mount point %s is not an absolute pathname.\n"),
		    myname, mountp);
		return (1);
	}
	/*
	 * Don't do some of these checks if aflg because a mount point may
	 * not exist now, but will be mounted before we get to it.
	 * This is one of the quirks of "secondary mounting".
	 */
	if (!aflg && stat64(mountp, &stbuf) < 0) {
		if (errno == ENOENT || errno == ENOTDIR)
			fprintf(stderr,
			    gettext("%s: Mount point %s does not exist.\n"),
			    myname, mountp);
		else {
			fprintf(stderr,
			    gettext("%s: Cannot stat mount point %s.\n"),
			    myname, mountp);
			perror(myname);
		}
		return (1);
	}
	return (0);
}

void
nomem(void)
{
	fprintf(stderr, gettext("%s: Out of memory\n"), myname);
	while (nrun > 0 && (dowait() != -1))
		;
	exit(1);
}