-
Notifications
You must be signed in to change notification settings - Fork 770
/
Copy pathvplat.c
5638 lines (5044 loc) · 149 KB
/
vplat.c
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
/*
* 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) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright 2018, Joyent Inc.
* Copyright (c) 2015, 2016 by Delphix. All rights reserved.
* Copyright 2019 OmniOS Community Edition (OmniOSce) Association.
* Copyright 2020 RackTop Systems Inc.
* Copyright 2023 Oxide Computer Company
*/
/*
* Copyright 2011 Nexenta Systems, Inc. All rights reserved.
*/
/*
* This module contains functions used to bring up and tear down the
* Virtual Platform: [un]mounting file-systems, [un]plumbing network
* interfaces, [un]configuring devices, establishing resource controls,
* and creating/destroying the zone in the kernel. These actions, on
* the way up, ready the zone; on the way down, they halt the zone.
* See the much longer block comment at the beginning of zoneadmd.c
* for a bigger picture of how the whole program functions.
*
* This module also has primary responsibility for the layout of "scratch
* zones." These are mounted, but inactive, zones that are used during
* operating system upgrade and potentially other administrative action. The
* scratch zone environment is similar to the miniroot environment. The zone's
* actual root is mounted read-write on /a, and the standard paths (/usr,
* /sbin, /lib) all lead to read-only copies of the running system's binaries.
* This allows the administrative tools to manipulate the zone using "-R /a"
* without relying on any binaries in the zone itself.
*
* If the scratch zone is on an alternate root (Live Upgrade [LU] boot
* environment), then we must resolve the lofs mounts used there to uncover
* writable (unshared) resources. Shared resources, though, are always
* read-only. In addition, if the "same" zone with a different root path is
* currently running, then "/b" inside the zone points to the running zone's
* root. This allows LU to synchronize configuration files during the upgrade
* process.
*
* To construct this environment, this module creates a tmpfs mount on
* $ZONEPATH/lu. Inside this scratch area, the miniroot-like environment as
* described above is constructed on the fly. The zone is then created using
* $ZONEPATH/lu as the root.
*
* Note that scratch zones are inactive. The zone's bits are not running and
* likely cannot be run correctly until upgrade is done. Init is not running
* there, nor is SMF. Because of this, the "mounted" state of a scratch zone
* is not a part of the usual halt/ready/boot state machine.
*/
#include <sys/param.h>
#include <sys/mount.h>
#include <sys/mntent.h>
#include <sys/socket.h>
#include <sys/utsname.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/sockio.h>
#include <sys/stropts.h>
#include <sys/conf.h>
#include <sys/systeminfo.h>
#include <sys/secflags.h>
#include <sys/vnic.h>
#include <libdlpi.h>
#include <libdllink.h>
#include <libdlvlan.h>
#include <libdlvnic.h>
#include <libdlaggr.h>
#include <inet/tcp.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <net/route.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <rctl.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <wait.h>
#include <limits.h>
#include <libgen.h>
#include <libzfs.h>
#include <libdevinfo.h>
#include <zone.h>
#include <assert.h>
#include <libcontract.h>
#include <libcontract_priv.h>
#include <uuid/uuid.h>
#include <sys/mntio.h>
#include <sys/mnttab.h>
#include <sys/fs/autofs.h> /* for _autofssys() */
#include <sys/fs/lofs_info.h>
#include <sys/fs/zfs.h>
#include <pool.h>
#include <sys/pool.h>
#include <sys/priocntl.h>
#include <libbrand.h>
#include <sys/brand.h>
#include <libzonecfg.h>
#include <synch.h>
#include "zoneadmd.h"
#include <tsol/label.h>
#include <libtsnet.h>
#include <sys/priv.h>
#include <libinetutil.h>
#define V4_ADDR_LEN 32
#define V6_ADDR_LEN 128
#define RESOURCE_DEFAULT_OPTS \
MNTOPT_RO "," MNTOPT_LOFS_NOSUB "," MNTOPT_NODEVICES
#define DFSTYPES "/etc/dfs/fstypes"
#define MAXTNZLEN 2048
#define ALT_MOUNT(mount_cmd) ((mount_cmd) != Z_MNT_BOOT)
/* a reasonable estimate for the number of lwps per process */
#define LWPS_PER_PROCESS 10
/* for routing socket */
static int rts_seqno = 0;
/* mangled zone name when mounting in an alternate root environment */
static char kernzone[ZONENAME_MAX];
/* array of cached mount entries for resolve_lofs */
static struct mnttab *resolve_lofs_mnts, *resolve_lofs_mnt_max;
/* for Trusted Extensions */
static tsol_zcent_t *get_zone_label(zlog_t *, priv_set_t *);
static int tsol_mounts(zlog_t *, char *, char *);
static void tsol_unmounts(zlog_t *, char *);
static m_label_t *zlabel = NULL;
static m_label_t *zid_label = NULL;
static priv_set_t *zprivs = NULL;
static const char *DFLT_FS_ALLOWED = "hsfs,smbfs,nfs,nfs3,nfs4,nfsdyn";
/* from libsocket, not in any header file */
extern int getnetmaskbyaddr(struct in_addr, struct in_addr *);
/* from zoneadmd */
extern char query_hook[];
/*
* For each "net" resource configured in zonecfg, we track a zone_addr_list_t
* node in a linked list that is sorted by linkid. The list is constructed as
* the xml configuration file is parsed, and the information
* contained in each node is added to the kernel before the zone is
* booted, to be retrieved and applied from within the exclusive-IP NGZ
* on boot.
*/
typedef struct zone_addr_list {
struct zone_addr_list *za_next;
datalink_id_t za_linkid; /* datalink_id_t of interface */
struct zone_nwiftab za_nwiftab; /* address, defrouter properties */
} zone_addr_list_t;
/*
* An optimization for build_mnttable: reallocate (and potentially copy the
* data) only once every N times through the loop.
*/
#define MNTTAB_HUNK 32
/* some handy macros */
#define SIN(s) ((struct sockaddr_in *)s)
#define SIN6(s) ((struct sockaddr_in6 *)s)
/*
* Private autofs system call
*/
extern int _autofssys(int, void *);
static int
autofs_cleanup(zoneid_t zoneid)
{
int r;
/*
* Ask autofs to unmount all trigger nodes in the given zone.
* Handle ENOSYS in the case that the autofs kernel module is not
* installed.
*/
r = _autofssys(AUTOFS_UNMOUNTALL, (void *)zoneid);
if (r != 0 && errno == ENOSYS) {
return (0);
}
return (r);
}
static void
free_mnttable(struct mnttab *mnt_array, uint_t nelem)
{
uint_t i;
if (mnt_array == NULL)
return;
for (i = 0; i < nelem; i++) {
free(mnt_array[i].mnt_mountp);
free(mnt_array[i].mnt_fstype);
free(mnt_array[i].mnt_special);
free(mnt_array[i].mnt_mntopts);
assert(mnt_array[i].mnt_time == NULL);
}
free(mnt_array);
}
/*
* Build the mount table for the zone rooted at "zroot", storing the resulting
* array of struct mnttabs in "mnt_arrayp" and the number of elements in the
* array in "nelemp".
*/
static int
build_mnttable(zlog_t *zlogp, const char *zroot, size_t zrootlen, FILE *mnttab,
struct mnttab **mnt_arrayp, uint_t *nelemp)
{
struct mnttab mnt;
struct mnttab *mnts;
struct mnttab *mnp;
uint_t nmnt;
rewind(mnttab);
resetmnttab(mnttab);
nmnt = 0;
mnts = NULL;
while (getmntent(mnttab, &mnt) == 0) {
struct mnttab *tmp_array;
if (strncmp(mnt.mnt_mountp, zroot, zrootlen) != 0)
continue;
if (nmnt % MNTTAB_HUNK == 0) {
tmp_array = realloc(mnts,
(nmnt + MNTTAB_HUNK) * sizeof (*mnts));
if (tmp_array == NULL) {
free_mnttable(mnts, nmnt);
return (-1);
}
mnts = tmp_array;
}
mnp = &mnts[nmnt++];
/*
* Zero out any fields we're not using.
*/
(void) memset(mnp, 0, sizeof (*mnp));
if (mnt.mnt_special != NULL)
mnp->mnt_special = strdup(mnt.mnt_special);
if (mnt.mnt_mntopts != NULL)
mnp->mnt_mntopts = strdup(mnt.mnt_mntopts);
mnp->mnt_mountp = strdup(mnt.mnt_mountp);
mnp->mnt_fstype = strdup(mnt.mnt_fstype);
if ((mnt.mnt_special != NULL && mnp->mnt_special == NULL) ||
(mnt.mnt_mntopts != NULL && mnp->mnt_mntopts == NULL) ||
mnp->mnt_mountp == NULL || mnp->mnt_fstype == NULL) {
zerror(zlogp, B_TRUE, "memory allocation failed");
free_mnttable(mnts, nmnt);
return (-1);
}
}
*mnt_arrayp = mnts;
*nelemp = nmnt;
return (0);
}
/*
* This is an optimization. The resolve_lofs function is used quite frequently
* to manipulate file paths, and on a machine with a large number of zones,
* there will be a huge number of mounted file systems. Thus, we trigger a
* reread of the list of mount points
*/
static void
lofs_discard_mnttab(void)
{
free_mnttable(resolve_lofs_mnts,
resolve_lofs_mnt_max - resolve_lofs_mnts);
resolve_lofs_mnts = resolve_lofs_mnt_max = NULL;
}
static int
lofs_read_mnttab(zlog_t *zlogp)
{
FILE *mnttab;
uint_t nmnts;
if ((mnttab = fopen(MNTTAB, "r")) == NULL)
return (-1);
if (build_mnttable(zlogp, "", 0, mnttab, &resolve_lofs_mnts,
&nmnts) == -1) {
(void) fclose(mnttab);
return (-1);
}
(void) fclose(mnttab);
resolve_lofs_mnt_max = resolve_lofs_mnts + nmnts;
return (0);
}
/*
* This function loops over potential loopback mounts and symlinks in a given
* path and resolves them all down to an absolute path.
*/
void
resolve_lofs(zlog_t *zlogp, char *path, size_t pathlen)
{
int len, arlen;
const char *altroot;
char tmppath[MAXPATHLEN];
boolean_t outside_altroot;
if ((len = resolvepath(path, tmppath, sizeof (tmppath))) == -1)
return;
tmppath[len] = '\0';
(void) strlcpy(path, tmppath, sizeof (tmppath));
/* This happens once per zoneadmd operation. */
if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
return;
altroot = zonecfg_get_root();
arlen = strlen(altroot);
outside_altroot = B_FALSE;
for (;;) {
struct mnttab *mnp;
/* Search in reverse order to find longest match */
for (mnp = resolve_lofs_mnt_max - 1; mnp >= resolve_lofs_mnts;
mnp--) {
if (mnp->mnt_fstype == NULL ||
mnp->mnt_mountp == NULL ||
mnp->mnt_special == NULL)
continue;
len = strlen(mnp->mnt_mountp);
if (strncmp(mnp->mnt_mountp, path, len) == 0 &&
(path[len] == '/' || path[len] == '\0'))
break;
}
if (mnp < resolve_lofs_mnts)
break;
/* If it's not a lofs then we're done */
if (strcmp(mnp->mnt_fstype, MNTTYPE_LOFS) != 0)
break;
if (outside_altroot) {
char *cp;
int olen = sizeof (MNTOPT_RO) - 1;
/*
* If we run into a read-only mount outside of the
* alternate root environment, then the user doesn't
* want this path to be made read-write.
*/
if (mnp->mnt_mntopts != NULL &&
(cp = strstr(mnp->mnt_mntopts, MNTOPT_RO)) !=
NULL &&
(cp == mnp->mnt_mntopts || cp[-1] == ',') &&
(cp[olen] == '\0' || cp[olen] == ',')) {
break;
}
} else if (arlen > 0 &&
(strncmp(mnp->mnt_special, altroot, arlen) != 0 ||
(mnp->mnt_special[arlen] != '\0' &&
mnp->mnt_special[arlen] != '/'))) {
outside_altroot = B_TRUE;
}
/* use temporary buffer because new path might be longer */
(void) snprintf(tmppath, sizeof (tmppath), "%s%s",
mnp->mnt_special, path + len);
if ((len = resolvepath(tmppath, path, pathlen)) == -1)
break;
path[len] = '\0';
}
}
/*
* For a regular mount, check if a replacement lofs mount is needed because the
* referenced device is already mounted somewhere.
*/
static int
check_lofs_needed(zlog_t *zlogp, struct zone_fstab *fsptr)
{
struct mnttab *mnp;
zone_fsopt_t *optptr, *onext;
/* This happens once per zoneadmd operation. */
if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
return (-1);
/*
* If this special node isn't already in use, then it's ours alone;
* no need to worry about conflicting mounts.
*/
for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max;
mnp++) {
if (strcmp(mnp->mnt_special, fsptr->zone_fs_special) == 0)
break;
}
if (mnp >= resolve_lofs_mnt_max)
return (0);
/*
* Convert this duplicate mount into a lofs mount.
*/
(void) strlcpy(fsptr->zone_fs_special, mnp->mnt_mountp,
sizeof (fsptr->zone_fs_special));
(void) strlcpy(fsptr->zone_fs_type, MNTTYPE_LOFS,
sizeof (fsptr->zone_fs_type));
fsptr->zone_fs_raw[0] = '\0';
/*
* Discard all but one of the original options and set that to our
* default set of options used for resources.
*/
optptr = fsptr->zone_fs_options;
if (optptr == NULL) {
optptr = malloc(sizeof (*optptr));
if (optptr == NULL) {
zerror(zlogp, B_TRUE, "cannot mount %s",
fsptr->zone_fs_dir);
return (-1);
}
} else {
while ((onext = optptr->zone_fsopt_next) != NULL) {
optptr->zone_fsopt_next = onext->zone_fsopt_next;
free(onext);
}
}
(void) strcpy(optptr->zone_fsopt_opt, RESOURCE_DEFAULT_OPTS);
optptr->zone_fsopt_next = NULL;
fsptr->zone_fs_options = optptr;
return (0);
}
int
make_one_dir(zlog_t *zlogp, const char *prefix, const char *subdir, mode_t mode,
uid_t userid, gid_t groupid)
{
char path[MAXPATHLEN];
struct stat st;
if (snprintf(path, sizeof (path), "%s%s", prefix, subdir) >
sizeof (path)) {
zerror(zlogp, B_FALSE, "pathname %s%s is too long", prefix,
subdir);
return (-1);
}
if (lstat(path, &st) == 0) {
/*
* We don't check the file mode since presumably the zone
* administrator may have had good reason to change the mode,
* and we don't need to second guess them.
*/
if (!S_ISDIR(st.st_mode)) {
if (S_ISREG(st.st_mode)) {
/*
* Allow readonly mounts of /etc/ files; this
* is needed most by Trusted Extensions.
*/
if (strncmp(subdir, "/etc/",
strlen("/etc/")) != 0) {
zerror(zlogp, B_FALSE,
"%s is not in /etc", path);
return (-1);
}
} else {
zerror(zlogp, B_FALSE,
"%s is not a directory", path);
return (-1);
}
}
return (0);
}
if (mkdirp(path, mode) != 0) {
if (errno == EROFS)
zerror(zlogp, B_FALSE, "Could not mkdir %s.\nIt is on "
"a read-only file system in this local zone.\nMake "
"sure %s exists in the global zone.", path, subdir);
else
zerror(zlogp, B_TRUE, "mkdirp of %s failed", path);
return (-1);
}
(void) chown(path, userid, groupid);
return (0);
}
static void
free_remote_fstypes(char **types)
{
uint_t i;
if (types == NULL)
return;
for (i = 0; types[i] != NULL; i++)
free(types[i]);
free(types);
}
static char **
get_remote_fstypes(zlog_t *zlogp)
{
char **types = NULL;
FILE *fp;
char buf[MAXPATHLEN];
char fstype[MAXPATHLEN];
uint_t lines = 0;
uint_t i;
if ((fp = fopen(DFSTYPES, "r")) == NULL) {
zerror(zlogp, B_TRUE, "failed to open %s", DFSTYPES);
return (NULL);
}
/*
* Count the number of lines
*/
while (fgets(buf, sizeof (buf), fp) != NULL)
lines++;
if (lines == 0) /* didn't read anything; empty file */
goto out;
rewind(fp);
/*
* Allocate enough space for a NULL-terminated array.
*/
types = calloc(lines + 1, sizeof (char *));
if (types == NULL) {
zerror(zlogp, B_TRUE, "memory allocation failed");
goto out;
}
i = 0;
while (fgets(buf, sizeof (buf), fp) != NULL) {
/* LINTED - fstype is big enough to hold buf */
if (sscanf(buf, "%s", fstype) == 0) {
zerror(zlogp, B_FALSE, "unable to parse %s", DFSTYPES);
free_remote_fstypes(types);
types = NULL;
goto out;
}
types[i] = strdup(fstype);
if (types[i] == NULL) {
zerror(zlogp, B_TRUE, "memory allocation failed");
free_remote_fstypes(types);
types = NULL;
goto out;
}
i++;
}
out:
(void) fclose(fp);
return (types);
}
static boolean_t
is_remote_fstype(const char *fstype, char *const *remote_fstypes)
{
uint_t i;
if (remote_fstypes == NULL)
return (B_FALSE);
for (i = 0; remote_fstypes[i] != NULL; i++) {
if (strcmp(remote_fstypes[i], fstype) == 0)
return (B_TRUE);
}
return (B_FALSE);
}
/*
* This converts a zone root path (normally of the form .../root) to a Live
* Upgrade scratch zone root (of the form .../lu).
*/
static void
root_to_lu(zlog_t *zlogp, char *zroot, size_t zrootlen, boolean_t isresolved)
{
if (!isresolved && zonecfg_in_alt_root())
resolve_lofs(zlogp, zroot, zrootlen);
(void) strcpy(strrchr(zroot, '/') + 1, "lu");
}
/*
* The general strategy for unmounting filesystems is as follows:
*
* - Remote filesystems may be dead, and attempting to contact them as
* part of a regular unmount may hang forever; we want to always try to
* forcibly unmount such filesystems and only fall back to regular
* unmounts if the filesystem doesn't support forced unmounts.
*
* - We don't want to unnecessarily corrupt metadata on local
* filesystems (ie UFS), so we want to start off with graceful unmounts,
* and only escalate to doing forced unmounts if we get stuck.
*
* We start off walking backwards through the mount table. This doesn't
* give us strict ordering but ensures that we try to unmount submounts
* first. We thus limit the number of failed umount2(2) calls.
*
* The mechanism for determining if we're stuck is to count the number
* of failed unmounts each iteration through the mount table. This
* gives us an upper bound on the number of filesystems which remain
* mounted (autofs trigger nodes are dealt with separately). If at the
* end of one unmount+autofs_cleanup cycle we still have the same number
* of mounts that we started out with, we're stuck and try a forced
* unmount. If that fails (filesystem doesn't support forced unmounts)
* then we bail and are unable to teardown the zone. If it succeeds,
* we're no longer stuck so we continue with our policy of trying
* graceful mounts first.
*
* Zone must be down (ie, no processes or threads active).
*/
static int
unmount_filesystems(zlog_t *zlogp, zoneid_t zoneid, boolean_t unmount_cmd)
{
int error = 0;
FILE *mnttab;
struct mnttab *mnts;
uint_t nmnt;
char zroot[MAXPATHLEN + 1];
size_t zrootlen;
uint_t oldcount = UINT_MAX;
boolean_t stuck = B_FALSE;
char **remote_fstypes = NULL;
if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
zerror(zlogp, B_FALSE, "unable to determine zone root");
return (-1);
}
if (unmount_cmd)
root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
(void) strcat(zroot, "/");
zrootlen = strlen(zroot);
/*
* For Trusted Extensions unmount each higher level zone's mount
* of our zone's /export/home
*/
if (!unmount_cmd)
tsol_unmounts(zlogp, zone_name);
if ((mnttab = fopen(MNTTAB, "r")) == NULL) {
zerror(zlogp, B_TRUE, "failed to open %s", MNTTAB);
return (-1);
}
/*
* Use our hacky mntfs ioctl so we see everything, even mounts with
* MS_NOMNTTAB.
*/
if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) {
zerror(zlogp, B_TRUE, "unable to configure %s", MNTTAB);
error++;
goto out;
}
/*
* Build the list of remote fstypes so we know which ones we
* should forcibly unmount.
*/
remote_fstypes = get_remote_fstypes(zlogp);
for (; /* ever */; ) {
uint_t newcount = 0;
boolean_t unmounted;
struct mnttab *mnp;
char *path;
uint_t i;
mnts = NULL;
nmnt = 0;
/*
* MNTTAB gives us a way to walk through mounted
* filesystems; we need to be able to walk them in
* reverse order, so we build a list of all mounted
* filesystems.
*/
if (build_mnttable(zlogp, zroot, zrootlen, mnttab, &mnts,
&nmnt) != 0) {
error++;
goto out;
}
for (i = 0; i < nmnt; i++) {
mnp = &mnts[nmnt - i - 1]; /* access in reverse order */
path = mnp->mnt_mountp;
unmounted = B_FALSE;
/*
* Try forced unmount first for remote filesystems.
*
* Not all remote filesystems support forced unmounts,
* so if this fails (ENOTSUP) we'll continue on
* and try a regular unmount.
*/
if (is_remote_fstype(mnp->mnt_fstype, remote_fstypes)) {
if (umount2(path, MS_FORCE) == 0)
unmounted = B_TRUE;
}
/*
* Try forced unmount if we're stuck.
*/
if (stuck) {
if (umount2(path, MS_FORCE) == 0) {
unmounted = B_TRUE;
stuck = B_FALSE;
} else {
/*
* The first failure indicates a
* mount we won't be able to get
* rid of automatically, so we
* bail.
*/
error++;
zerror(zlogp, B_FALSE,
"unable to unmount '%s'", path);
free_mnttable(mnts, nmnt);
goto out;
}
}
/*
* Try regular unmounts for everything else.
*/
if (!unmounted && umount2(path, 0) != 0)
newcount++;
}
free_mnttable(mnts, nmnt);
if (newcount == 0)
break;
if (newcount >= oldcount) {
/*
* Last round didn't unmount anything; we're stuck and
* should start trying forced unmounts.
*/
stuck = B_TRUE;
}
oldcount = newcount;
/*
* Autofs doesn't let you unmount its trigger nodes from
* userland so we have to tell the kernel to cleanup for us.
*/
if (autofs_cleanup(zoneid) != 0) {
zerror(zlogp, B_TRUE, "unable to remove autofs nodes");
error++;
goto out;
}
}
out:
free_remote_fstypes(remote_fstypes);
(void) fclose(mnttab);
return (error ? -1 : 0);
}
static int
fs_compare(const void *m1, const void *m2)
{
struct zone_fstab *i = (struct zone_fstab *)m1;
struct zone_fstab *j = (struct zone_fstab *)m2;
return (strcmp(i->zone_fs_dir, j->zone_fs_dir));
}
/*
* Fork and exec (and wait for) the mentioned binary with the provided
* arguments. Returns (-1) if something went wrong with fork(2) or exec(2),
* returns the exit status otherwise.
*
* If we were unable to exec the provided pathname (for whatever
* reason), we return the special token ZEXIT_EXEC. The current value
* of ZEXIT_EXEC doesn't conflict with legitimate exit codes of the
* consumers of this function; any future consumers must make sure this
* remains the case.
*/
static int
forkexec(zlog_t *zlogp, const char *path, char *const argv[])
{
pid_t child_pid;
int child_status = 0;
/*
* Do not let another thread localize a message while we are forking.
*/
(void) mutex_lock(&msglock);
child_pid = fork();
(void) mutex_unlock(&msglock);
if (child_pid == -1) {
zerror(zlogp, B_TRUE, "could not fork for %s", argv[0]);
return (-1);
} else if (child_pid == 0) {
closefrom(0);
/* redirect stdin, stdout & stderr to /dev/null */
(void) open("/dev/null", O_RDONLY); /* stdin */
(void) open("/dev/null", O_WRONLY); /* stdout */
(void) open("/dev/null", O_WRONLY); /* stderr */
(void) execv(path, argv);
/*
* Since we are in the child, there is no point calling zerror()
* since there is nobody waiting to consume it. So exit with a
* special code that the parent will recognize and call zerror()
* accordingly.
*/
_exit(ZEXIT_EXEC);
} else {
(void) waitpid(child_pid, &child_status, 0);
}
if (WIFSIGNALED(child_status)) {
zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to "
"signal %d", path, WTERMSIG(child_status));
return (-1);
}
assert(WIFEXITED(child_status));
if (WEXITSTATUS(child_status) == ZEXIT_EXEC) {
zerror(zlogp, B_FALSE, "failed to exec %s", path);
return (-1);
}
return (WEXITSTATUS(child_status));
}
static int
isregfile(const char *path)
{
struct stat64 st;
if (stat64(path, &st) == -1)
return (-1);
return (S_ISREG(st.st_mode));
}
static int
dofsck(zlog_t *zlogp, const char *fstype, const char *rawdev)
{
char cmdbuf[MAXPATHLEN];
char *argv[5];
int status;
/*
* We could alternatively have called /usr/sbin/fsck -F <fstype>, but
* that would cost us an extra fork/exec without buying us anything.
*/
if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", fstype)
>= sizeof (cmdbuf)) {
zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
return (-1);
}
/*
* If it doesn't exist, that's OK: we verified this previously
* in zoneadm.
*/
if (isregfile(cmdbuf) == -1)
return (0);
argv[0] = "fsck";
argv[1] = "-o";
argv[2] = "p";
argv[3] = (char *)rawdev;
argv[4] = NULL;
status = forkexec(zlogp, cmdbuf, argv);
if (status == 0 || status == -1)
return (status);
zerror(zlogp, B_FALSE, "fsck of '%s' failed with exit status %d; "
"run fsck manually", rawdev, status);
return (-1);
}
static int
domount(zlog_t *zlogp, const char *fstype, const char *opts,
const char *special, const char *directory)
{
char cmdbuf[MAXPATHLEN];
char *argv[6];
int status;
/*
* We could alternatively have called /usr/sbin/mount -F <fstype>, but
* that would cost us an extra fork/exec without buying us anything.
*/
if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", fstype)
>= sizeof (cmdbuf)) {
zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
return (-1);
}
argv[0] = "mount";
if (opts[0] == '\0') {
argv[1] = (char *)special;
argv[2] = (char *)directory;
argv[3] = NULL;
} else {
argv[1] = "-o";
argv[2] = (char *)opts;
argv[3] = (char *)special;
argv[4] = (char *)directory;
argv[5] = NULL;
}
status = forkexec(zlogp, cmdbuf, argv);
if (status == 0 || status == -1)
return (status);
if (opts[0] == '\0')
zerror(zlogp, B_FALSE, "\"%s %s %s\" "
"failed with exit code %d",
cmdbuf, special, directory, status);
else
zerror(zlogp, B_FALSE, "\"%s -o %s %s %s\" "
"failed with exit code %d",
cmdbuf, opts, special, directory, status);
return (-1);
}
/*
* Check if a given mount point path exists.
* If it does, make sure it doesn't contain any symlinks.
* Note that if "leaf" is false we're checking an intermediate
* component of the mount point path, so it must be a directory.
* If "leaf" is true, then we're checking the entire mount point
* path, so the mount point itself can be anything aside from a
* symbolic link.
*
* If the path is invalid then a negative value is returned. If the
* path exists and is a valid mount point path then 0 is returned.
* If the path doesn't exist return a positive value.
*/
static int
valid_mount_point(zlog_t *zlogp, const char *path, const boolean_t leaf)
{
struct stat statbuf;
char respath[MAXPATHLEN];
int res;
if (lstat(path, &statbuf) != 0) {
if (errno == ENOENT)
return (1);
zerror(zlogp, B_TRUE, "can't stat %s", path);
return (-1);
}
if (S_ISLNK(statbuf.st_mode)) {
zerror(zlogp, B_FALSE, "%s is a symlink", path);
return (-1);
}
if (!leaf && !S_ISDIR(statbuf.st_mode)) {
zerror(zlogp, B_FALSE, "%s is not a directory", path);
return (-1);
}
if ((res = resolvepath(path, respath, sizeof (respath))) == -1) {
zerror(zlogp, B_TRUE, "unable to resolve path %s", path);
return (-1);
}
respath[res] = '\0';
if (strcmp(path, respath) != 0) {
/*
* We don't like ".."s, "."s, or "//"s throwing us off
*/
zerror(zlogp, B_FALSE, "%s is not a canonical path", path);
return (-1);
}
return (0);
}
/*
* Validate a mount point path. A valid mount point path is an
* absolute path that either doesn't exist, or, if it does exists it
* must be an absolute canonical path that doesn't have any symbolic
* links in it. The target of a mount point path can be any filesystem
* object. (Different filesystems can support different mount points,
* for example "lofs" and "mntfs" both support files and directories
* while "ufs" just supports directories.)
*
* If the path is invalid then a negative value is returned. If the
* path exists and is a valid mount point path then 0 is returned.
* If the path doesn't exist return a positive value.