summaryrefslogtreecommitdiff
path: root/usr/src/cmd/svr4pkg/libinst/mntinfo.c
blob: 45a21362e6b7aec87d4b357d17de671c89d2e867 (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
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
/*
 * 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 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */


/*
 * System includes
 */

#include <stdio.h>
#include <limits.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <libgen.h>
#include <string.h>
#include <wait.h>
#include <signal.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/systeminfo.h>
#include <pkgstrct.h>
#include <pkginfo.h>
#include <locale.h>
#include <libintl.h>

#include <sys/mnttab.h>
#include <sys/mntent.h>
#include <sys/vfstab.h>

/*
 * consolidation pkg command library includes
 */

#include <pkglib.h>

/*
 * local pkg command library includes
 */

#include "install.h"
#include "libinst.h"
#include "libadm.h"
#include "messages.h"

extern char **environ;

static int match_mount;		/* This holds the mount of interest. */

int	fs_tab_used  = 0;
int	fs_tab_alloc = 0;
static int	fs_list = -1;

struct	fstable	**fs_tab = NULL;

#define	PKGDBROOT	"/var/sadm"
#define	MOUNT		"/sbin/mount"
#define	UMOUNT		"/sbin/umount"

#define	setmntent	fopen
#define	endmntent	fclose
#define	MOUNT_TABLE	MNTTAB

/* returned by already_mounted() */
#define	MNT_NOT		0
#define	MNT_EXACT	1
#define	MNT_AVAIL	2

/* used with is_remote_src() */
#define	NOT_REMOTE	0
#define	REAL_REMOTE	1
#define	SELF_SERVE	2

/*
 * Due to /etc/mnttab files containing entries for multiple nfs hosts
 * HOST_NM_LN needs to be accommodating. The recommended value in the sysinfo
 * man page of 257 needs to be expanded. See bugid 4076513.
 * 1024 chars is defined in the mnttab.h header as the max size of an entry.
 */

#define	HOST_NM_LN	MNT_LINE_MAX

/* These cachefs definitions should be in mntent.h. Maybe some day. */
#define	MNTTYPE_CFS		"cachefs"
#define	MNTOPT_BACKFSTYPE	"backfstype"
#define	MNTTYPE_AUTO		"autofs"

/*
 * Utilities for getting filesystem information from the mount table.
 *
 * Note: vanilla SVr4 code (pkginstall/dockspace.c) used the output from
 * popen() on the "/etc/mount" command.  However, we need to get more
 * information about mounted filesystems, so we use the C interfaces to
 * the mount table, which also happens to be much faster than running
 * another process.  Since several of the pkg commands need access to the
 * the code has been placed here, to be included in the libinst library.
 */

#define	ALLOC_CHUNK	30

/*
 * fs_tab_ent_comp -	compare fstable entries first by length in reverse
 *			order, then alphabetically.
 */
static int
fs_tab_ent_comp(const void *e1, const void *e2)
{
	struct fstable	*fs1 = *((struct fstable **)e1);
	struct fstable	*fs2 = *((struct fstable **)e2);

	if (fs1->namlen == fs2->namlen)
		return (strcmp(fs1->name, fs2->name));
	else
		return (fs2->namlen - fs1->namlen);
}

/*
 * This determines if the source of the mount is from another host. If it's
 * from this host, then it might be writable. This returns NOT_REMOTE if it's
 * pure local, REAL_REMOTE if it's being served from another host and
 * SELF_SERVE if it's being served by the current host.
 */
static int
is_remote_src(char *source)
{
	static char host_name[HOST_NM_LN];
	char source_host[HOST_NM_LN], *src_ptr, *src_host_ptr;
	static int hn_len;

	if (hn_len == 0) {
		/* Find out what host this is. */
		(void) sysinfo(SI_HOSTNAME, host_name, HOST_NM_LN);
		hn_len = strlen(host_name);
	}

	if (source[0] == '/')
		return (NOT_REMOTE);	/* No server name, so it's local. */

	if (strchr(source, ':') == NULL)
		return (NOT_REMOTE);	/* it's a floppy disk or something */

	src_ptr = source;
	src_host_ptr = source_host;

	/* Scan to the end of the hostname (find the ":"). */
	while (*src_ptr != ':')
		*src_host_ptr++ = *src_ptr++;
	*src_host_ptr = '\0';

	/* Multiple hosts: failover with multiple servers; this is remote. */
	if (strchr(source_host, ',') != NULL)
		return (REAL_REMOTE);

	if (strncmp(source, host_name, hn_len) == 0 &&
	    *(source+hn_len) == ':' || is_local_host(source_host))
		return (SELF_SERVE);	/* Exporting from itself, it's local. */

	return (REAL_REMOTE);
}

/*
 * This determines if an apparently writeable filesystem is really writeable
 * or if it's been shared over the network with root-restrictive options.
 */
static int
really_write(char *mountpt)
{
	char testfile[PATH_MAX];
	int fd, retval = 0;
	struct stat status;

	(void) snprintf(testfile, sizeof (testfile), "%s/testXXXXXX", mountpt);

	if (mktemp(testfile) == NULL)
		return (0);	/* may as well be read-only */
	/* LINTED do not use creat(); use open(path,... */
	else if ((fd = creat(testfile, 0777)) == -1)
		return (0);	/* can't write */
	else if (fstat(fd, &status) == -1)
		retval = 0;	/* may as well be read-only */
	else if (status.st_uid != 0)
		retval = 0;	/* too many restrictions */
	else
		retval = 1;

	(void) close(fd);
	(void) unlink(testfile);

	return (retval);
}

/* This returns the hostname portion of a remote path. */
char *
get_server_host(short n)
{
	static char hostname[HOST_NM_LN], *host_end;

	if (fs_tab_used == 0) {
		return ("unknown source");
	}

	if (n >= 0 && n < fs_tab_used) {
		(void) strcpy(hostname, fs_tab[n]->remote_name);
		if ((host_end = strchr(hostname, ':')) == NULL) {
			if ((strcmp(fs_tab[n]->fstype, MNTTYPE_AUTO)) == NULL)
				return ("automounter");
			else
				return (fs_tab[n]->fstype);
		} else {
			*host_end = '\0';
			return (hostname);
		}
	}

	return ("unknown source");
}

/*
 * This pulls the path out of a hostpath which may be of the form host:path
 * where path is an absolute path. NOTE: If path turns out to be relative,
 * this returns NULL.
 */
static char *
path_part(char *hostpath)
{
	char *host_end;

	if ((host_end = strchr(hostpath, ':')) == NULL && hostpath[0] == '/')
		return (hostpath);	/* It's already legit. */

	if (*(host_end+1) == '/')
		return (host_end+1);	/* Here's the path part. */

	return (NULL);
}

/*
 * This scans the filesystems already mounted to see if this remote mount is
 * already in place on the server. This scans the fs_tab for a remote_name
 * exactly matching the client's. It stores the current entry number
 * corresponding to this mount in the static match_mount.
 *
 * Returns:
 *	MNT_NOT		Couldn't find it.
 *	MNT_EXACT	This has actually been manually mounted for us
 *	MNT_AVAIL	This is mounted for the server, but needs to be
 *			loopback mounted from the client's perspective.
 */
static int
already_mounted(struct vfstab *vfs, int is_local_host, char *client_path,
    char *host_path)
{
	int i;

	match_mount = -1;

	if (fs_tab_used == 0) {
		return (MNT_NOT);
	}

	for (i = 0; i < fs_tab_used; i++) {
		/*
		 * Determine if this has been manually mounted exactly as we
		 * require. Begin by finding a mount on our current
		 * mountpoint.
		 */
		if (strcmp(fs_tab[i]->name, client_path) == 0) {
			/*
			 * Now see if it is really the same mount. This isn't
			 * smart enough to find mounts on top of mounts, but
			 * assuming there is no conspiracy to fool this
			 * function, it will be good enough.
			 */
			if (is_local_host &&
			    strcmp(fs_tab[i]->remote_name, host_path) == 0) {
				match_mount = i;
				return (MNT_EXACT);
			}
		}

		/* Determine if this mount is available to the server. */
		if (strcmp(fs_tab[i]->remote_name, vfs->vfs_special) == 0) {
			match_mount = i;
			return (MNT_AVAIL);
		}
	}
	return (MNT_NOT);
}

/*
 * This function unmounts all of the loopback mounts created for the client.
 * If no client stuff is mounted, this is completely benign, it finds that
 * nothing is mounted up and returns. It returns "1" for unmounted everything
 * OK and "0" for failure.
 */
int
unmount_client()
{
	int	errcode;
	int	exit_no;
	int	n;
	int	retcode = 1;
	int	status;
	pid_t	pid;
	pid_t	pid_return;

	if (fs_tab_used == 0) {
		return (1);
	}

	for (n = 0; n < fs_tab_used-1; n++) {
		/* If the filesystem is mounted and this utility did it ... */
		if (fs_tab[n]->cl_mounted && fs_tab[n]->srvr_map) {
			char	*arg[3];

			/* create arglist for umount command */

			arg[0] = UMOUNT;
			arg[1] = fs_tab[n]->name;
			arg[2] = (char *)NULL;

			/* flush standard i/o before creating new process */

			(void) fflush(stderr);
			(void) fflush(stdout);

			/*
			 * create new process to execute command in;
			 * vfork is being used to avoid duplicating the parents
			 * memory space - this means that the child process may
			 * not modify any of the parents memory including the
			 * standard i/o descriptors - all the child can do is
			 * adjust interrupts and open files as a prelude to a
			 * call to exec().
			 */

			pid = vfork();
			if (pid < 0) {
				/* fork failed! */

				logerr(WRN_BAD_FORK, errno, strerror(errno));
				retcode = 0;
			} else if (pid > 0) {
				/*
				 * this is the parent process
				 */

				status = 0;
				pid_return = waitpid(pid, &status, 0);

				if (pid_return != pid) {
					logerr(WRN_BAD_WAIT, pid, pid_return,
						(unsigned long)status, errno,
						strerror(errno));
					retcode = 0;
				}

				/*
				 * If the child was stopped or killed by a
				 * signal or exied with any code but 0, we
				 * assume the mount has failed.
				 */

				if (!WIFEXITED(status) ||
				    (errcode = WEXITSTATUS(status))) {
					retcode = 0;
					logerr(WRN_FSTAB_UMOUNT,
						fs_tab[n]->name, errcode);
				} else {
					fs_tab[n]->cl_mounted = 0;
				}
			} else {
				/*
				 * this is the child process
				 */

				int	i;

				/* reset any signals to default */

				for (i = 0; i < NSIG; i++) {
					(void) sigset(i, SIG_DFL);
				}

				/*
				 * Redirect output to /dev/null because the
				 * umount error message may be confusing to
				 * the user.
				 */

				i = open("/dev/null", O_WRONLY);
				if (i >= 0) {
					dup2(2, STDERR_FILENO);
				}

				/* close all file descriptors except stdio */

				closefrom(3);

				exit_no = execve(arg[0], arg, environ);
				_exit(exit_no);
			}
		}
	}

	return (retcode);
}

/*
 * This function creates the necessary loopback mounts to emulate the client
 * configuration with respect to the server. If this is being run on a
 * standalone or the installation is actually to the local system, this call
 * is benign since srvr_map won't be set anywhere. It returns "1" for mounted
 * everything OK and "0" for failure.
 */
int
mount_client()
{
	int	errcode;
	int	exit_no;
	int	n;
	int	retcode = 1;
	int	status;
	pid_t	pid;
	pid_t	pid_return;

	if (fs_tab_used == 0) {
		return (1);
	}

	for (n = fs_tab_used-1; n >= 0; n--) {
		/*
		 * If the filesystem is mounted (meaning available) and the
		 * apparent filesystem can be mapped to a local filesystem
		 * AND the local filesystem is not the same as the target
		 * filesystem, mount it.
		 */
		if (fs_tab[n]->mounted && fs_tab[n]->srvr_map) {
			char	*arg[6];

			/* create arglist for mount command */

			arg[0] = MOUNT;
			arg[1] = "-F";
			arg[2] = "lofs";
			arg[3] = fs_tab[n]->remote_name;
			arg[4] = fs_tab[n]->name;
			arg[5] = (char *)NULL;

			/* flush standard i/o before creating new process */

			(void) fflush(stderr);
			(void) fflush(stdout);

			/*
			 * create new process to execute command in;
			 * vfork is being used to avoid duplicating the parents
			 * memory space - this means that the child process may
			 * not modify any of the parents memory including the
			 * standard i/o descriptors - all the child can do is
			 * adjust interrupts and open files as a prelude to a
			 * call to exec().
			 */

			pid = vfork();
			if (pid < 0) {
				/* fork failed! */

				logerr(WRN_BAD_FORK, errno, strerror(errno));
				retcode = 0;
			} else if (pid > 0) {
				/*
				 * this is the parent process
				 */

				pid_return = waitpid(pid, &status, 0);

				if (pid_return != pid) {
					logerr(WRN_BAD_WAIT, pid, pid_return,
						(unsigned long)status, errno,
						strerror(errno));
					retcode = 0;
				}

				/*
				 * If the child was stopped or killed by a
				 * signal or exied with any code but 0, we
				 * assume the mount has failed.
				 */

				if (!WIFEXITED(status) ||
				    (errcode = WEXITSTATUS(status))) {
					retcode = 0;
					fs_tab[n]->mnt_failed = 1;
					logerr(WRN_FSTAB_MOUNT,
					    fs_tab[n]->name, errcode);
				} else {
					fs_tab[n]->cl_mounted = 1;
				}
			} else {
				/*
				 * this is the child process
				 */

				int	i;

				/* reset all signals to default */

				for (i = 0; i < NSIG; i++) {
					(void) sigset(i, SIG_DFL);
				}

				/*
				 * Redirect output to /dev/null because the
				 * mount error message may be confusing to
				 * the user.
				 */

				i = open("/dev/null", O_WRONLY);
				if (i >= 0) {
					dup2(i, STDERR_FILENO);
				}

				/* close all file descriptors except stdio */

				closefrom(3);

				exit_no = execve(arg[0], arg, environ);
				_exit(exit_no);
				/*NOTREACHED*/
			}
		}
	}
	return (retcode);
}

/*
 * This function maps path, on a loopback filesystem, back to the real server
 * filesystem. fsys_value is the fs_tab[] entry to which the loopback'd path is
 * mapped. This returns a pointer to a static area. If the result is needed
 * for further processing, it should be strdup()'d or something.
 */
char *
server_map(char *path, short fsys_value)
{
	static char server_construction[PATH_MAX];

	if (fs_tab_used == 0) {
		(void) strcpy(server_construction, path);
	} else if (fsys_value >= 0 && fsys_value < fs_tab_used) {
		(void) snprintf(server_construction,
			sizeof (server_construction),
			"%s%s", fs_tab[fsys_value]->remote_name,
			path+strlen(fs_tab[fsys_value]->name));
	} else {
		(void) strcpy(server_construction, path);
	}

	return (server_construction);
}

/* This function sets up the standard parts of the fs_tab. */
static struct fstable *
fs_tab_init(char *mountp, char *fstype)
{
	struct fstable *nfte;

	/* Create the array if necessary. */
	if (fs_list == -1) {
		fs_list = ar_create(ALLOC_CHUNK,
		    (unsigned)sizeof (struct fstable),
		    "filesystem mount data");
		if (fs_list == -1) {
			progerr(ERR_MALLOC, "fs_list", errno, strerror(errno));
			return (NULL);
		}
	}

	/*
	 * Allocate an fstable entry for this mnttab entry.
	 */
	if ((nfte = *(struct fstable **)ar_next_avail(fs_list))
	    == NULL) {
		progerr(ERR_MALLOC, "nfte", errno, strerror(errno));
		return (NULL);
	}

	/*
	 * Point fs_tab at the head of the array again, since it may have
	 * moved due to realloc in ar_next_avail(). If ar_next_avail() realizes
	 * that there is no more room to grow the array, it reallocates the
	 * array. Because we stored pointer to that array in fs_tab, we need
	 * to make sure that it is updated as well.
	 */
	if ((fs_tab = (struct fstable **)ar_get_head(fs_list)) == NULL) {
		progerr(ERR_NOTABLE, "mount", MOUNT_TABLE, strerror(errno));
		return (NULL);
	}

	/*
	 * Get the length of the 'mount point' name.
	 */
	nfte->namlen = strlen(mountp);
	/*
	 * Allocate space for the 'mount point' name.
	 */
	if ((nfte->name = malloc(nfte->namlen+1)) == NULL) {
		progerr(ERR_MALLOC, "name", errno, strerror(errno));
		return (NULL);
	}
	(void) strcpy(nfte->name, mountp);

	if ((nfte->fstype = malloc(strlen(fstype)+1)) == NULL) {
		progerr(ERR_MALLOC, "fstype", errno, strerror(errno));
		return (NULL);
	}
	(void) strcpy(nfte->fstype, fstype);

	fs_tab_used++;

	return (nfte);
}

/* This function frees all memory associated with the filesystem table. */
void
fs_tab_free(void)
{
	int n;

	if (fs_tab_used == 0) {
		return;
	}

	for (n = 0; n < fs_tab_used; n++) {
		free(fs_tab[n]->fstype);
		free(fs_tab[n]->name);
		free(fs_tab[n]->remote_name);
	}

	ar_free(fs_list);
}

/* This function scans a string of mount options for a specific keyword. */
static int
hasopt(char *options, char *keyword)
{
	char vfs_options[VFS_LINE_MAX], *optptr;

	if (!options) {
		(void) strcpy(vfs_options, "ro");
	} else {
		(void) strcpy(vfs_options, options);
	}

	while (optptr = strrchr(vfs_options, ',')) {
		*optptr++ = '\0';

		if (strcmp(optptr, keyword) == 0)
			return (1);
	}

	/* Now deal with the remainder. */
	if (strcmp(vfs_options, keyword) == 0)
		return (1);

	return (0);
}

/*
 * This function constructs a new filesystem table (fs_tab[]) entry based on
 * an /etc/mnttab entry. When it returns, the new entry has been inserted
 * into fs_tab[].
 */
static int
construct_mt(struct mnttab *mt)
{
	struct	fstable	*nfte;

	/*
	 * Initialize fstable structure and make the standard entries.
	 */
	if ((nfte = fs_tab_init(mt->mnt_mountp, mt->mnt_fstype)) == NULL)
		return (1);

	/*
	 * See if this is served from another host.
	 * Testing the type is cheap; finding the hostname is not.
	 * At this point, we're using the REAL mnttab; since we're not
	 * allowed to mount ourself with "NFS", "NFS" must be remote.
	 * The automount will translate "nfs:self" to a lofs mount.
	 */
	if (strcmp(mt->mnt_fstype, MNTTYPE_AUTO) == 0 ||
	    strcmp(mt->mnt_fstype, MNTTYPE_NFS) == 0 ||
	    is_remote_src(mt->mnt_special) == REAL_REMOTE)
		nfte->remote = 1;
	else
		nfte->remote = 0;

	/* It's mounted now (by definition), so we don't have to remap it. */
	nfte->srvr_map = 0;
	nfte->mounted = 1;

	nfte->remote_name = strdup(mt->mnt_special);

	/*
	 * This checks the mount commands which establish the most
	 * basic level of access. Later further tests may be
	 * necessary to fully qualify this. We set this bit
	 * preliminarily because we have access to the mount data
	 * now.
	 */
	nfte->writeable = 0;	/* Assume read-only. */
	if (hasmntopt(mt, MNTOPT_RO) == NULL) {
		nfte->writeable = 1;
		if (!(nfte->remote))
			/*
			 * There's no network involved, so this
			 * assessment is confirmed.
			 */
			nfte->write_tested = 1;
	} else
		/* read-only is read-only */
		nfte->write_tested = 1;

	/* Is this coming to us from a server? */
	if (nfte->remote && !(nfte->writeable))
		nfte->served = 1;

	return (0);
}

/*
 * This function modifies an existing fs_tab[] entry. It was found mounted up
 * exactly the way we would have mounted it in mount_client() only at the
 * time we didn't know it was for the client. Now we do, so we're setting the
 * various permissions to conform to the client view.
 */
static void
mod_existing(struct vfstab *vfsent, int fstab_entry, int is_remote)
{
	/*
	 * Establish whether the client will see this as served.
	 */
	if (is_remote && hasopt(vfsent->vfs_mntopts, MNTOPT_RO))
		fs_tab[fstab_entry]->served = 1;

	fs_tab[fstab_entry]->cl_mounted = 1;
}

/*
 * This function constructs a new fs_tab[] entry based on
 * an /etc/vfstab entry. When it returns, the new entry has been inserted
 * into fstab[].
 */
static int
construct_vfs(struct vfstab *vfsent, char *client_path, char *link_name,
    int is_remote, int mnt_stat)
{
	int use_link;
	struct	fstable	*nfte;

	if ((nfte = fs_tab_init(client_path, vfsent->vfs_fstype)) == NULL)
		return (1);

	nfte->remote = (is_remote == REAL_REMOTE);

	/*
	 * The file system mounted on the client may or may not be writeable.
	 * So we hand it over to fsys() to evaluate. This will have the same
	 * read/write attributes as the corresponding mounted filesystem.
	 */
	use_link = 0;
	if (nfte->remote) {
		/*
		 * Deal here with mount points actually on a system remote
		 * from the server.
		 */
		if (mnt_stat == MNT_NOT) {
			/*
			 * This filesystem isn't in the current mount table
			 * meaning it isn't mounted, the current host can't
			 * write to it and there's no point to mapping it for
			 * the server.
			 */
			link_name = NULL;
			nfte->mounted = 0;
			nfte->srvr_map = 0;
			nfte->writeable = 0;
		} else {	/* It's MNT_AVAIL. */
			/*
			 * This filesystem is associated with a current
			 * mountpoint. Since it's mounted, it needs to be
			 * remapped and it is writable if the real mounted
			 * filesystem is writeable.
			 */
			use_link = 1;
			link_name = strdup(fs_tab[match_mount]->name);
			nfte->mounted = 1;
			nfte->srvr_map = 1;
			nfte->writeable = fs_tab[match_mount]->writeable;
			nfte->write_tested = fs_tab[match_mount]->write_tested;
		}
	} else {	/* local filesystem */
		use_link = 1;
		nfte->mounted = 1;
		nfte->srvr_map = 1;
		nfte->writeable = fs_tab[fsys(link_name)]->writeable;
		nfte->write_tested = 1;
	}

	/*
	 * Now we establish whether the client will see this as served.
	 */
	if (is_remote && hasopt(vfsent->vfs_mntopts, MNTOPT_RO))
		nfte->served = 1;

	if (use_link) {
		nfte->remote_name = link_name;
	} else {
		nfte->remote_name = strdup(vfsent->vfs_special);
	}

	return (0);
}

/*
 * get_mntinfo - get the mount table, now dynamically allocated. Returns 0 if
 * no problem and 1 if there's a fatal error.
 */
int
get_mntinfo(int map_client, char *vfstab_file)
{
	static 	char 	*rn = "/";
	FILE		*pp;
	struct	mnttab	mtbuf;
	struct	mnttab	*mt = &mtbuf;
	char		*install_root;
	int 		is_remote;

	/*
	 * Open the mount table for the current host and establish a global
	 * table that holds data about current mount status.
	 */
	if ((pp = setmntent(MOUNT_TABLE, "r")) == NULL) {
		progerr(ERR_NOTABLE, "mount", MOUNT_TABLE, strerror(errno));
		return (1);
	}

	/*
	 * First, review the mounted filesystems on the managing host. This
	 * may also be the target host but we haven't decided that for sure
	 * yet.
	 */
	while (!getmntent(pp, mt))
		if (construct_mt(mt))
			return (1);

	(void) endmntent(pp);

	/*
	 * Now, we see if this installation is to a client. If it is, we scan
	 * the client's vfstab to determine what filesystems are
	 * inappropriate to write to. This simply adds the vfstab entries
	 * representing what will be remote file systems for the client.
	 * Everything that isn't remote to the client is already accounted
	 * for in the fs_tab[] so far. If the remote filesystem is really on
	 * this server, we will write through to the server from this client.
	 */
	install_root = get_inst_root();
	if (install_root && strcmp(install_root, "/") != 0 && map_client) {
		/* OK, this is a legitimate remote client. */
		struct	vfstab	vfsbuf;
		struct	vfstab	*vfs = &vfsbuf;
		char VFS_TABLE[PATH_MAX];

		/*
		 * Since we use the fsys() function later, and it depends on
		 * an ordered list, we have to sort the list here.
		 */
		qsort(fs_tab, fs_tab_used,
		    sizeof (struct fstable *), fs_tab_ent_comp);

		/*
		 * Here's where the vfstab for the target is. If we can get
		 * to it, we'll scan it for what the client will see as
		 * remote filesystems, otherwise, we'll just skip this.
		 */
		if (vfstab_file) {
			(void) snprintf(VFS_TABLE, sizeof (VFS_TABLE), "%s",
				vfstab_file);
		} else {
			(void) snprintf(VFS_TABLE, sizeof (VFS_TABLE), "%s%s",
				install_root, VFSTAB);
		}

		if (access(VFS_TABLE, R_OK) == 0) {
			char *link_name;

			/*
			 * Open the vfs table for the target host.
			 */
			if ((pp = setmntent(VFS_TABLE, "r")) == NULL) {
				progerr(ERR_NOTABLE, "vfs", VFS_TABLE,
					strerror(errno));
				return (1);
			}

			/* Do this for each entry in the vfstab. */
			while (!getvfsent(pp, vfs)) {
				char client_mountp[PATH_MAX];
				int mnt_stat;

				/*
				 * We put it into the fs table if it's
				 * remote mounted (even from this server) or
				 * loopback mounted from the client's point
				 * of view.
				 */
				if (!(is_remote =
				    is_remote_src(vfs->vfs_special)) &&
				    strcmp(vfs->vfs_fstype, MNTTYPE_LOFS) !=
				    0)
					continue;	/* not interesting */

				/*
				 * Construct client_mountp by prepending the
				 * install_root to the 'mount point' name.
				 */
				if (strcmp(vfs->vfs_mountp, "/") == 0) {
					(void) strcpy(client_mountp,
					    install_root);
				} else {
					(void) snprintf(client_mountp,
						sizeof (client_mountp), "%s%s",
						install_root, vfs->vfs_mountp);
				}

				/*
				 * We also skip the entry if the vfs_special
				 * path and the client_path are the same.
				 * There's no need to mount it, it's just a
				 * cachefs optimization that mounts a
				 * directory over itself from this server.
				 */
				if ((is_remote == SELF_SERVE) &&
				    strcmp(path_part(vfs->vfs_special),
				    client_mountp) == 0)
					continue;

				/* Determine if this is already mounted. */
				link_name = strdup(path_part(vfs->vfs_special));
				mnt_stat = already_mounted(vfs,
				    (is_remote != REAL_REMOTE), client_mountp,
				    link_name);

				if (mnt_stat == MNT_EXACT) {
					mod_existing(vfs, match_mount,
					    is_remote);
				} else {	/* MNT_NOT */
					if (construct_vfs(vfs, client_mountp,
					    link_name, is_remote, mnt_stat))
						return (1);
				}
			}
			(void) endmntent(pp);
		}	/* end of if(access()) */
	}	/* end of if(install_root) */

	/* This next one may look stupid, but it can really happen. */
	if (fs_tab_used <= 0) {
		progerr(ERR_MNT_NOMOUNTS);
		return (1);
	}

	/*
	 * Now that we have the complete list of mounted (or virtually
	 * mounted) filesystems, we sort the mountpoints in reverse order
	 * based on the length of the 'mount point' name.
	 */
	qsort(fs_tab, fs_tab_used, sizeof (struct fstable *), fs_tab_ent_comp);
	if (strcmp(fs_tab[fs_tab_used-1]->name, rn) != 0) {
		progerr(ERR_MNT_NOROOT, fs_tab[fs_tab_used-1]->name, rn, errno,
		    strerror(errno));
		return (1);
	} else {
		return (0);
	}
}

/*
 * This function supports dryrun mode by allowing the filesystem table to be
 * directly loaded from the continuation file.
 */
int
load_fsentry(struct fstable *fs_entry, char *name, char *fstype,
    char *remote_name)
{
	struct fstable *nfte;

	if ((nfte = fs_tab_init(name, fstype)) == NULL)
		return (1);

	/* Grab the name and fstype from the new structure. */
	fs_entry->name = nfte->name;
	fs_entry->fstype = nfte->fstype;

	/* Copy the basic structure into place. */
	(void) memcpy(nfte, fs_entry, sizeof (struct fstable));

	/*
	 * Allocate space for the 'special' name.
	 */
	if ((nfte->remote_name = malloc(strlen(remote_name)+1)) == NULL) {
		progerr(ERR_MALLOC, "remote_name", errno, strerror(errno));
		return (1);
	}

	(void) strcpy(nfte->remote_name, remote_name);

	return (0);
}

/*
 * Given a path, return the table index of the filesystem the file apparently
 * resides on. This doesn't put any time into resolving filesystems that
 * refer to other filesystems. It just returns the entry containing this
 * path.
 */
short
fsys(char *path)
{
	register int i;
	char	real_path[PATH_MAX];
	char	path_copy[PATH_MAX];
	char	*path2use;
	char	*cp;
	int	pathlen;
	boolean_t found = B_FALSE;

	/*
	 * The loop below represents our best effort to identify real path of
	 * a file, which doesn't need to exist. realpath() returns error for
	 * nonexistent path, therefore we need to cut off trailing components
	 * of path until we get path which exists and can be resolved by
	 * realpath(). Lookup of "/dir/symlink/nonexistent-file" would fail
	 * to resolve symlink without this.
	 */
	(void) strlcpy(path_copy, path, PATH_MAX);
	for (cp = dirname(path_copy); strlen(cp) > 1; cp = dirname(cp)) {
		if (realpath(cp, real_path) != NULL) {
			found = B_TRUE;
			break;
		} else if (errno != ENOENT)
			break;
	}
	if (found)
		path2use = real_path;
	else
		/* fall back to original path in case of unexpected failure */
		path2use = path;

	pathlen = strlen(path2use);

	/*
	 * The following algorithm scans the list of attached file systems
	 * for the one containing path. At this point the file names in
	 * fs_tab[] are sorted by decreasing length to facilitate the scan.
	 * The first for() scans past all the file system names too short to
	 * contain path. The second for() does the actual string comparison.
	 * It tests first to assure that the comparison is against a complete
	 * token by assuring that the end of the filesystem name aligns with
	 * the end of a token in path2use (ie: '/' or NULL) then it does a
	 * string compare. -- JST
	 */

	if (fs_tab_used == 0) {
		return (-1);
	}

	for (i = 0; i < fs_tab_used; i++)
		if (fs_tab[i] == NULL)
			continue;
		else if (fs_tab[i]->namlen <= pathlen)
			break;
	for (; i < fs_tab_used; i++) {
		int fs_namelen;
		char term_char;

		if (fs_tab[i] == NULL)
			continue;

		fs_namelen = fs_tab[i]->namlen;
		term_char = path2use[fs_namelen];

		/*
		 * If we're putting the file "/a/kernel" into the filesystem
		 * "/a", then fs_namelen == 2 and term_char == '/'. If, we're
		 * putting "/etc/termcap" into "/", fs_namelen == 1 and
		 * term_char (unfortunately) == 'e'. In the case of
		 * fs_namelen == 1, we check to make sure the filesystem is
		 * "/" and if it is, we have a guaranteed fit, otherwise we
		 * do the string compare. -- JST
		 */
		if ((fs_namelen == 1 && *(fs_tab[i]->name) == '/') ||
		    ((term_char == '/' || term_char == NULL) &&
		    strncmp(fs_tab[i]->name, path2use, fs_namelen) == 0))
			return (i);
	}

	/*
	 * It only gets here if the root filesystem is fundamentally corrupt.
	 * (This can happen!)
	 */
	progerr(ERR_FSYS_FELLOUT, path2use);

	return (-1);
}

/*
 * This function returns the entry in the fs_tab[] corresponding to the
 * actual filesystem of record. It won't return a loopback filesystem entry,
 * it will return the filesystem that the loopback filesystem is mounted
 * over.
 */
short
resolved_fsys(char *path)
{
	int i = -1;
	char path2use[PATH_MAX];

	(void) strcpy(path2use, path);

	/* If this isn't a "real" filesystem, resolve the map. */
	do {
		(void) strcpy(path2use, server_map(path2use, i));
		i = fsys(path2use);
	} while (fs_tab[i]->srvr_map);

	return (i);
}

/*
 * This function returns the srvr_map status based upon the fs_tab entry
 * number. This tells us if the server path constructed from the package
 * install root is really the target filesystem.
 */
int
use_srvr_map_n(short n)
{
	return ((int)fs_tab[n]->srvr_map);
}

/*
 * This function returns the mount status based upon the fs_tab entry
 * number. This tells us if there is any hope of gaining access
 * to this file system.
 */
int
is_mounted_n(short n)
{
	return ((int)fs_tab[n]->mounted);
}

/*
 * is_fs_writeable_n - given an fstab index, return 1
 *	if it's writeable, 0 if read-only.
 */
int
is_fs_writeable_n(short n)
{
	/*
	 * If the write access permissions haven't been confirmed, do that
	 * now. Note that the only reason we need to do the special check is
	 * in the case of an NFS mount (remote) because we can't determine if
	 * root has access in any other way.
	 */
	if (fs_tab[n]->remote && fs_tab[n]->mounted &&
	    !fs_tab[n]->write_tested) {
		if (fs_tab[n]->writeable && !really_write(fs_tab[n]->name))
			fs_tab[n]->writeable = 0;	/* not really */

		fs_tab[n]->write_tested = 1;	/* confirmed */
	}

	return ((int)fs_tab[n]->writeable);
}

/*
 * is_remote_fs_n - given an fstab index, return 1
 *	if it's a remote filesystem, 0 if local.
 *
 *	Note: Upon entry, a valid fsys() is required.
 */
int
is_remote_fs_n(short n)
{
	return ((int)fs_tab[n]->remote);
}

/* index-driven is_served() */
int
is_served_n(short n)
{
	return ((int)fs_tab[n]->served);
}

/*
 * This returns the number of blocks available on the indicated filesystem.
 *
 *	Note: Upon entry, a valid fsys() is required.
 */
fsblkcnt_t
get_blk_free_n(short n)
{
	return (fs_tab[n]->bfree);
}

/*
 * This returns the number of blocks being used on the indicated filesystem.
 *
 *	Note: Upon entry, a valid fsys() is required.
 */
fsblkcnt_t
get_blk_used_n(short n)
{
	return (fs_tab[n]->bused);
}

/*
 * This returns the number of inodes available on the indicated filesystem.
 *
 *	Note: Upon entry, a valid fsys() is required.
 */
fsblkcnt_t
get_inode_free_n(short n)
{
	return (fs_tab[n]->ffree);
}

/*
 * This returns the number of inodes being used on the indicated filesystem.
 *
 *	Note: Upon entry, a valid fsys() is required.
 */
fsblkcnt_t
get_inode_used_n(short n)
{
	return (fs_tab[n]->fused);
}

/*
 * Sets the number of blocks being used on the indicated filesystem.
 *
 *	Note: Upon entry, a valid fsys() is required.
 */
void
set_blk_used_n(short n, fsblkcnt_t value)
{
	fs_tab[n]->bused = value;
}

/* Get the filesystem block size. */
fsblkcnt_t
get_blk_size_n(short n)
{
	return (fs_tab[n]->bsize);
}

/* Get the filesystem fragment size. */
fsblkcnt_t
get_frag_size_n(short n)
{
	return (fs_tab[n]->bsize);
}

/*
 * This returns the name of the indicated filesystem.
 */
char *
get_fs_name_n(short n)
{
	if (fs_tab_used == 0) {
		return (NULL);
	} else if (n >= fs_tab_used) {
		return (NULL);
	} else {
		return (fs_tab[n]->name);
	}
}

/*
 * This returns the remote name of the indicated filesystem.
 *
 *	Note: Upon entry, a valid fsys() is required.
 */
char *
get_source_name_n(short n)
{
	return (fs_tab[n]->remote_name);
}

/*
 * This function returns the srvr_map status based upon the path.
 */
int
use_srvr_map(char *path, short *fsys_value)
{
	if (*fsys_value == BADFSYS)
		*fsys_value = fsys(path);

	return (use_srvr_map_n(*fsys_value));
}

/*
 * This function returns the mount status based upon the path.
 */
int
is_mounted(char *path, short *fsys_value)
{
	if (*fsys_value == BADFSYS)
		*fsys_value = fsys(path);

	return (is_mounted_n(*fsys_value));
}

/*
 * is_fs_writeable - given a cfent entry, return 1
 *	if it's writeable, 0 if read-only.
 *
 *	Note: Upon exit, a valid fsys() is guaranteed. This is
 *	an interface requirement.
 */
int
is_fs_writeable(char *path, short *fsys_value)
{
	if (*fsys_value == BADFSYS)
		*fsys_value = fsys(path);

	return (is_fs_writeable_n(*fsys_value));
}

/*
 * is_remote_fs - given a cfent entry, return 1
 *	if it's a remote filesystem, 0 if local.
 *
 *	Also Note: Upon exit, a valid fsys() is guaranteed. This is
 *	an interface requirement.
 */
int
is_remote_fs(char *path, short *fsys_value)
{
	if (*fsys_value == BADFSYS)
		*fsys_value = fsys(path);

	return (is_remote_fs_n(*fsys_value));
}

/*
 * This function returns the served status of the filesystem. Served means a
 * client is getting this file from a server and it is not writeable by the
 * client. It has nothing to do with whether or not this particular operation
 * (eg: pkgadd or pkgrm) will be writing to it.
 */
int
is_served(char *path, short *fsys_value)
{
	if (*fsys_value == BADFSYS)
		*fsys_value = fsys(path);

	return (is_served_n(*fsys_value));
}

/*
 * get_remote_path - given a filesystem table index, return the
 *	path of the filesystem on the remote system.  Otherwise,
 *	return NULL if it's a local filesystem.
 */
char *
get_remote_path(short n)
{
	char	*p;

	if (!is_remote_fs_n(n))
		return (NULL); 	/* local */
	p = strchr(fs_tab[n]->remote_name, ':');
	if (!p)
		p = fs_tab[n]->remote_name; 	/* Loopback */
	else
		p++; 	/* remote */
	return (p);
}

/*
 * get_mount_point - given a filesystem table index, return the
 *	path of the mount point.  Otherwise,
 *	return NULL if it's a local filesystem.
 */
char *
get_mount_point(short n)
{
	if (!is_remote_fs_n(n))
		return (NULL); 	/* local */
	return (fs_tab[n]->name);
}

struct fstable *
get_fs_entry(short n)
{
	if (fs_tab_used == 0) {
		return (NULL);
	} else if (n >= fs_tab_used) {
		return (NULL);
	} else {
		return (fs_tab[n]);
	}
}