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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* zoneadmd manages zones; one zoneadmd process is launched for each
* non-global zone on the system. This daemon juggles four jobs:
*
* - Implement setup and teardown of the zone "virtual platform": mount and
* unmount filesystems; create and destroy network interfaces; communicate
* with devfsadmd to lay out devices for the zone; instantiate the zone
* console device; configure process runtime attributes such as resource
* controls, pool bindings, fine-grained privileges.
*
* - Launch the zone's init(1M) process.
*
* - Implement a door server; clients (like zoneadm) connect to the door
* server and request zone state changes. The kernel is also a client of
* this door server. A request to halt or reboot the zone which originates
* *inside* the zone results in a door upcall from the kernel into zoneadmd.
*
* One minor problem is that messages emitted by zoneadmd need to be passed
* back to the zoneadm process making the request. These messages need to
* be rendered in the client's locale; so, this is passed in as part of the
* request. The exception is the kernel upcall to zoneadmd, in which case
* messages are syslog'd.
*
* To make all of this work, the Makefile adds -a to xgettext to extract *all*
* strings, and an exclusion file (zoneadmd.xcl) is used to exclude those
* strings which do not need to be translated.
*
* - Act as a console server for zlogin -C processes; see comments in zcons.c
* for more information about the zone console architecture.
*
* DESIGN NOTES
*
* Restart:
* A chief design constraint of zoneadmd is that it should be restartable in
* the case that the administrator kills it off, or it suffers a fatal error,
* without the running zone being impacted; this is akin to being able to
* reboot the service processor of a server without affecting the OS instance.
*/
#include <sys/param.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <bsm/adt.h>
#include <bsm/adt_event.h>
#include <alloca.h>
#include <assert.h>
#include <errno.h>
#include <door.h>
#include <fcntl.h>
#include <locale.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <synch.h>
#include <syslog.h>
#include <thread.h>
#include <unistd.h>
#include <wait.h>
#include <limits.h>
#include <zone.h>
#include <libcontract.h>
#include <libcontract_priv.h>
#include <sys/contract/process.h>
#include <sys/ctfs.h>
#include <libzonecfg.h>
#include "zoneadmd.h"
static char *progname;
char *zone_name; /* zone which we are managing */
static zlog_t logsys;
mutex_t lock = DEFAULTMUTEX; /* to serialize stuff */
mutex_t msglock = DEFAULTMUTEX; /* for calling setlocale() */
static char zone_door_path[MAXPATHLEN];
static int zone_door = -1;
boolean_t in_death_throes = B_FALSE; /* daemon is dying */
boolean_t bringup_failure_recovery = B_FALSE; /* ignore certain failures */
#if !defined(TEXT_DOMAIN) /* should be defined by cc -D */
#define TEXT_DOMAIN "SYS_TEST" /* Use this only if it wasn't */
#endif
#define PATH_TO_INIT "/sbin/init"
#define DEFAULT_LOCALE "C"
static char *
get_execbasename(char *execfullname)
{
char *last_slash, *execbasename;
/* guard against '/' at end of command invocation */
for (;;) {
last_slash = strrchr(execfullname, '/');
if (last_slash == NULL) {
execbasename = execfullname;
break;
} else {
execbasename = last_slash + 1;
if (*execbasename == '\0') {
*last_slash = '\0';
continue;
}
break;
}
}
return (execbasename);
}
static void
usage(void)
{
(void) fprintf(stderr, gettext("Usage: %s -z zonename\n"), progname);
(void) fprintf(stderr,
gettext("\tNote: %s should not be run directly.\n"), progname);
exit(2);
}
/* ARGSUSED */
static void
sigchld(int sig)
{
}
char *
localize_msg(char *locale, const char *msg)
{
char *out;
(void) mutex_lock(&msglock);
(void) setlocale(LC_MESSAGES, locale);
out = gettext(msg);
(void) setlocale(LC_MESSAGES, DEFAULT_LOCALE);
(void) mutex_unlock(&msglock);
return (out);
}
/* PRINTFLIKE3 */
void
zerror(zlog_t *zlogp, boolean_t use_strerror, const char *fmt, ...)
{
va_list alist;
char buf[MAXPATHLEN * 2]; /* enough space for err msg with a path */
char *bp;
int saved_errno = errno;
if (zlogp == NULL)
return;
if (zlogp == &logsys)
(void) snprintf(buf, sizeof (buf), "[zone '%s'] ",
zone_name);
else
buf[0] = '\0';
bp = &(buf[strlen(buf)]);
/*
* In theory, the locale pointer should be set to either "C" or a
* char array, so it should never be NULL
*/
assert(zlogp->locale != NULL);
/* Locale is per process, but we are multi-threaded... */
fmt = localize_msg(zlogp->locale, fmt);
va_start(alist, fmt);
(void) vsnprintf(bp, sizeof (buf) - (bp - buf), fmt, alist);
va_end(alist);
bp = &(buf[strlen(buf)]);
if (use_strerror)
(void) snprintf(bp, sizeof (buf) - (bp - buf), ": %s",
strerror(saved_errno));
if (zlogp == &logsys) {
(void) syslog(LOG_ERR, "%s", buf);
} else if (zlogp->logfile != NULL) {
(void) fprintf(zlogp->logfile, "%s\n", buf);
} else {
size_t buflen;
size_t copylen;
buflen = snprintf(zlogp->log, zlogp->loglen, "%s\n", buf);
copylen = MIN(buflen, zlogp->loglen);
zlogp->log += copylen;
zlogp->loglen -= copylen;
}
}
static int
mkzonedir(zlog_t *zlogp)
{
struct stat st;
/*
* We must create and lock everyone but root out of ZONES_TMPDIR
* since anyone can open any UNIX domain socket, regardless of
* its file system permissions. Sigh...
*/
if (mkdir(ZONES_TMPDIR, S_IRWXU) < 0 && errno != EEXIST) {
zerror(zlogp, B_TRUE, "could not mkdir '%s'", ZONES_TMPDIR);
return (-1);
}
/* paranoia */
if ((stat(ZONES_TMPDIR, &st) < 0) || ((st.st_mode & S_IFDIR) == 0)) {
zerror(zlogp, B_TRUE, "'%s' is not a directory", ZONES_TMPDIR);
return (-1);
}
(void) chmod(ZONES_TMPDIR, S_IRWXU);
return (0);
}
static zoneid_t
zone_ready(zlog_t *zlogp)
{
int err;
if ((err = zonecfg_create_snapshot(zone_name)) != Z_OK) {
zerror(zlogp, B_FALSE, "unable to create snapshot: %s",
zonecfg_strerror(err));
return (-1);
}
if (vplat_create(zlogp) != 0) {
if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK)
zerror(zlogp, B_FALSE, "destroying snapshot: %s",
zonecfg_strerror(err));
return (-1);
}
if (vplat_bringup(zlogp) != 0) {
bringup_failure_recovery = B_TRUE;
(void) vplat_teardown(NULL);
if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK)
zerror(zlogp, B_FALSE, "destroying snapshot: %s",
zonecfg_strerror(err));
return (-1);
}
return (0);
}
static int
init_template()
{
int fd;
int err = 0;
fd = open64(CTFS_ROOT "/process/template", O_RDWR);
if (fd == -1)
return (-1);
/*
* For now, zoneadmd doesn't do anything with the contract.
* Deliver no events, don't inherit, and allow it to be orphaned.
*/
err |= ct_tmpl_set_critical(fd, 0);
err |= ct_tmpl_set_informative(fd, 0);
err |= ct_pr_tmpl_set_fatal(fd, CT_PR_EV_HWERR);
err |= ct_pr_tmpl_set_param(fd, CT_PR_PGRPONLY | CT_PR_REGENT);
if (err || ct_tmpl_activate(fd)) {
(void) close(fd);
return (-1);
}
return (fd);
}
static int
mount_early_fs(zlog_t *zlogp, zoneid_t zoneid, const char *spec,
const char *dir, char *fstype)
{
pid_t child;
int child_status;
int tmpl_fd;
ctid_t ct;
if ((tmpl_fd = init_template()) == -1) {
zerror(zlogp, B_TRUE, "failed to create contract");
return (-1);
}
if ((child = fork()) == -1) {
(void) ct_tmpl_clear(tmpl_fd);
(void) close(tmpl_fd);
zerror(zlogp, B_TRUE, "failed to fork");
return (-1);
} else if (child == 0) { /* child */
(void) ct_tmpl_clear(tmpl_fd);
/*
* Even though there are no procs running in the zone, we
* do this for paranoia's sake.
*/
(void) closefrom(0);
if (zone_enter(zoneid) == -1) {
_exit(errno);
}
if (mount(spec, dir, MS_DATA, fstype, NULL, 0, NULL, 0) != 0)
_exit(errno);
_exit(0);
}
/* parent */
if (contract_latest(&ct) == -1)
ct = -1;
(void) ct_tmpl_clear(tmpl_fd);
(void) close(tmpl_fd);
if (waitpid(child, &child_status, 0) != child) {
/* unexpected: we must have been signalled */
(void) contract_abandon_id(ct);
return (-1);
}
(void) contract_abandon_id(ct);
if (WEXITSTATUS(child_status) != 0) {
errno = WEXITSTATUS(child_status);
zerror(zlogp, B_TRUE, "mount of %s failed", dir);
return (-1);
}
return (0);
}
static int
zone_bootup(zlog_t *zlogp, const char *bootargs)
{
zoneid_t zoneid;
struct stat st;
char zroot[MAXPATHLEN], initpath[MAXPATHLEN];
if (init_console_slave(zlogp) != 0)
return (-1);
reset_slave_terminal(zlogp);
if ((zoneid = getzoneidbyname(zone_name)) == -1) {
zerror(zlogp, B_TRUE, "unable to get zoneid");
return (-1);
}
if (mount_early_fs(zlogp, zoneid, "/proc", "/proc", "proc") != 0)
return (-1);
if (mount_early_fs(zlogp, zoneid, "ctfs", CTFS_ROOT, "ctfs") != 0)
return (-1);
if (mount_early_fs(zlogp, zoneid, "swap", "/etc/svc/volatile",
"tmpfs") != 0)
return (-1);
if (mount_early_fs(zlogp, zoneid, "mnttab", "/etc/mnttab",
"mntfs") != 0)
return (-1);
/*
* Try to anticipate possible problems: Make sure init is executable.
*/
if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
zerror(zlogp, B_FALSE, "unable to determine zone root");
return (-1);
}
(void) snprintf(initpath, sizeof (initpath), "%s%s", zroot,
PATH_TO_INIT);
if (stat(initpath, &st) == -1) {
zerror(zlogp, B_TRUE, "could not stat %s", initpath);
return (-1);
}
if ((st.st_mode & S_IXUSR) == 0) {
zerror(zlogp, B_FALSE, "%s is not executable", initpath);
return (-1);
}
if (zone_boot(zoneid, bootargs) == -1) {
zerror(zlogp, B_TRUE, "unable to boot zone");
return (-1);
}
return (0);
}
static int
zone_halt(zlog_t *zlogp)
{
int err;
if (vplat_teardown(zlogp) != 0) {
if (!bringup_failure_recovery)
zerror(zlogp, B_FALSE, "unable to destroy zone");
return (-1);
}
if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK)
zerror(zlogp, B_FALSE, "destroying snapshot: %s",
zonecfg_strerror(err));
return (0);
}
/*
* Generate AUE_zone_state for a command that boots a zone.
*/
static void
audit_put_record(zlog_t *zlogp, ucred_t *uc, int return_val,
char *new_state)
{
adt_session_data_t *ah;
adt_event_data_t *event;
int pass_fail, fail_reason;
if (!adt_audit_enabled())
return;
if (return_val == 0) {
pass_fail = ADT_SUCCESS;
fail_reason = ADT_SUCCESS;
} else {
pass_fail = ADT_FAILURE;
fail_reason = ADT_FAIL_VALUE_PROGRAM;
}
if (adt_start_session(&ah, NULL, 0)) {
zerror(zlogp, B_TRUE, gettext("audit failure."));
return;
}
if (adt_set_from_ucred(ah, uc, ADT_NEW)) {
zerror(zlogp, B_TRUE, gettext("audit failure."));
(void) adt_end_session(ah);
return;
}
event = adt_alloc_event(ah, ADT_zone_state);
if (event == NULL) {
zerror(zlogp, B_TRUE, gettext("audit failure."));
(void) adt_end_session(ah);
return;
}
event->adt_zone_state.zonename = zone_name;
event->adt_zone_state.new_state = new_state;
if (adt_put_event(event, pass_fail, fail_reason))
zerror(zlogp, B_TRUE, gettext("audit failure."));
adt_free_event(event);
(void) adt_end_session(ah);
}
/*
* The main routine for the door server that deals with zone state transitions.
*/
/* ARGSUSED */
static void
server(void *cookie, char *args, size_t alen, door_desc_t *dp,
uint_t n_desc)
{
ucred_t *uc = NULL;
const priv_set_t *eset;
zone_state_t zstate;
zone_cmd_t cmd;
zone_cmd_arg_t *zargp;
boolean_t kernelcall;
int rval = -1;
uint64_t uniqid;
zoneid_t zoneid = -1;
zlog_t zlog;
zlog_t *zlogp;
zone_cmd_rval_t *rvalp;
size_t rlen = getpagesize(); /* conservative */
char *cmd_str = NULL;
/* LINTED E_BAD_PTR_CAST_ALIGN */
zargp = (zone_cmd_arg_t *)args;
/*
* When we get the door unref message, we've fdetach'd the door, and
* it is time for us to shut down zoneadmd.
*/
if (zargp == DOOR_UNREF_DATA) {
/*
* See comment at end of main() for info on the last rites.
*/
exit(0);
}
if (zargp == NULL) {
(void) door_return(NULL, 0, 0, 0);
}
rvalp = alloca(rlen);
bzero(rvalp, rlen);
zlog.logfile = NULL;
zlog.buflen = zlog.loglen = rlen - sizeof (zone_cmd_rval_t) + 1;
zlog.buf = rvalp->errbuf;
zlog.log = zlog.buf;
/* defer initialization of zlog.locale until after credential check */
zlogp = &zlog;
if (alen != sizeof (zone_cmd_arg_t)) {
/*
* This really shouldn't be happening.
*/
zerror(&logsys, B_FALSE, "invalid argument");
goto out;
}
cmd = zargp->cmd;
if (door_ucred(&uc) != 0) {
zerror(&logsys, B_TRUE, "door_ucred");
goto out;
}
eset = ucred_getprivset(uc, PRIV_EFFECTIVE);
if (ucred_getzoneid(uc) != GLOBAL_ZONEID ||
(eset != NULL ? !priv_ismember(eset, PRIV_SYS_CONFIG) :
ucred_geteuid(uc) != 0)) {
zerror(&logsys, B_FALSE, "insufficient privileges");
goto out;
}
kernelcall = ucred_getpid(uc) == 0;
/*
* This is safe because we only use a zlog_t throughout the
* duration of a door call; i.e., by the time the pointer
* might become invalid, the door call would be over.
*/
zlog.locale = kernelcall ? DEFAULT_LOCALE : zargp->locale;
(void) mutex_lock(&lock);
/*
* Once we start to really die off, we don't want more connections.
*/
if (in_death_throes) {
(void) mutex_unlock(&lock);
ucred_free(uc);
(void) door_return(NULL, 0, 0, 0);
thr_exit(NULL);
}
/*
* Check for validity of command.
*/
if (cmd != Z_READY && cmd != Z_BOOT && cmd != Z_REBOOT &&
cmd != Z_HALT && cmd != Z_NOTE_UNINSTALLING) {
zerror(&logsys, B_FALSE, "invalid command");
goto out;
}
if (kernelcall && (cmd != Z_HALT && cmd != Z_REBOOT)) {
/*
* Can't happen
*/
zerror(&logsys, B_FALSE, "received unexpected kernel upcall %d",
cmd);
goto out;
}
/*
* We ignore the possibility of someone calling zone_create(2)
* explicitly; all requests must come through zoneadmd.
*/
if (zone_get_state(zone_name, &zstate) != Z_OK) {
/*
* Something terribly wrong happened
*/
zerror(&logsys, B_FALSE, "unable to determine state of zone");
goto out;
}
if (kernelcall) {
/*
* Kernel-initiated requests may lose their validity if the
* zone_t the kernel was referring to has gone away.
*/
if ((zoneid = getzoneidbyname(zone_name)) == -1 ||
zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid,
sizeof (uniqid)) == -1 || uniqid != zargp->uniqid) {
/*
* We're not talking about the same zone. The request
* must have arrived too late. Return error.
*/
rval = -1;
goto out;
}
zlogp = &logsys; /* Log errors to syslog */
}
switch (zstate) {
case ZONE_STATE_CONFIGURED:
case ZONE_STATE_INCOMPLETE:
/*
* Not our area of expertise; we just print a nice message
* and die off.
*/
switch (cmd) {
case Z_READY:
cmd_str = "ready";
break;
case Z_BOOT:
cmd_str = "boot";
break;
case Z_HALT:
cmd_str = "halt";
break;
case Z_REBOOT:
cmd_str = "reboot";
break;
}
assert(cmd_str != NULL);
zerror(zlogp, B_FALSE,
"%s operation is invalid for zones in state '%s'",
cmd_str, zone_state_str(zstate));
break;
case ZONE_STATE_INSTALLED:
switch (cmd) {
case Z_READY:
rval = zone_ready(zlogp);
if (rval == 0)
eventstream_write(Z_EVT_ZONE_READIED);
break;
case Z_BOOT:
eventstream_write(Z_EVT_ZONE_BOOTING);
if ((rval = zone_ready(zlogp)) == 0)
rval = zone_bootup(zlogp, zargp->bootbuf);
audit_put_record(zlogp, uc, rval, "boot");
if (rval != 0) {
bringup_failure_recovery = B_TRUE;
(void) zone_halt(zlogp);
}
break;
case Z_HALT:
if (kernelcall) /* Invalid; can't happen */
abort();
/*
* We could have two clients racing to halt this
* zone; the second client loses, but his request
* doesn't fail, since the zone is now in the desired
* state.
*/
zerror(zlogp, B_FALSE, "zone is already halted");
rval = 0;
break;
case Z_REBOOT:
if (kernelcall) /* Invalid; can't happen */
abort();
zerror(zlogp, B_FALSE, "%s operation is invalid "
"for zones in state '%s'", "reboot",
zone_state_str(zstate));
rval = -1;
break;
case Z_NOTE_UNINSTALLING:
if (kernelcall) /* Invalid; can't happen */
abort();
/*
* Tell the console to print out a message about this.
* Once it does, we will be in_death_throes.
*/
eventstream_write(Z_EVT_ZONE_UNINSTALLING);
break;
}
break;
case ZONE_STATE_READY:
switch (cmd) {
case Z_READY:
/*
* We could have two clients racing to ready this
* zone; the second client loses, but his request
* doesn't fail, since the zone is now in the desired
* state.
*/
zerror(zlogp, B_FALSE, "zone is already ready");
rval = 0;
break;
case Z_BOOT:
eventstream_write(Z_EVT_ZONE_BOOTING);
rval = zone_bootup(zlogp, zargp->bootbuf);
audit_put_record(zlogp, uc, rval, "boot");
if (rval != 0) {
bringup_failure_recovery = B_TRUE;
(void) zone_halt(zlogp);
}
break;
case Z_HALT:
if (kernelcall) /* Invalid; can't happen */
abort();
if ((rval = zone_halt(zlogp)) != 0)
break;
eventstream_write(Z_EVT_ZONE_HALTED);
break;
case Z_REBOOT:
if (kernelcall) /* Invalid; can't happen */
abort();
zerror(zlogp, B_FALSE, "%s operation is invalid "
"for zones in state '%s'", "reboot",
zone_state_str(zstate));
rval = -1;
break;
case Z_NOTE_UNINSTALLING:
if (kernelcall) /* Invalid; can't happen */
abort();
zerror(zlogp, B_FALSE, "%s operation is "
"invalid for zones in state '%s'",
"note_uninstall", zone_state_str(zstate));
rval = -1;
break;
}
break;
case ZONE_STATE_RUNNING:
case ZONE_STATE_SHUTTING_DOWN:
case ZONE_STATE_DOWN:
switch (cmd) {
case Z_READY:
if ((rval = zone_halt(zlogp)) != 0)
break;
if ((rval = zone_ready(zlogp)) == 0)
eventstream_write(Z_EVT_ZONE_READIED);
break;
case Z_BOOT:
/*
* We could have two clients racing to boot this
* zone; the second client loses, but his request
* doesn't fail, since the zone is now in the desired
* state.
*/
zerror(zlogp, B_FALSE, "zone is already booted");
rval = 0;
break;
case Z_HALT:
if ((rval = zone_halt(zlogp)) != 0)
break;
eventstream_write(Z_EVT_ZONE_HALTED);
break;
case Z_REBOOT:
eventstream_write(Z_EVT_ZONE_REBOOTING);
if ((rval = zone_halt(zlogp)) != 0)
break;
if ((rval = zone_ready(zlogp)) == 0) {
rval = zone_bootup(zlogp, "");
audit_put_record(zlogp, uc, rval, "reboot");
if (rval != 0)
(void) zone_halt(zlogp);
}
break;
case Z_NOTE_UNINSTALLING:
zerror(zlogp, B_FALSE, "%s operation is "
"invalid for zones in state '%s'",
"note_uninstall", zone_state_str(zstate));
rval = -1;
break;
}
break;
default:
abort();
}
/*
* Because the state of the zone may have changed, we make sure
* to wake the console poller, which is in charge of initiating
* the shutdown procedure as necessary.
*/
eventstream_write(Z_EVT_NULL);
out:
(void) mutex_unlock(&lock);
if (kernelcall) {
rvalp = NULL;
rlen = 0;
} else {
rvalp->rval = rval;
}
if (uc != NULL)
ucred_free(uc);
(void) door_return((char *)rvalp, rlen, NULL, 0);
thr_exit(NULL);
}
static int
setup_door(zlog_t *zlogp)
{
if ((zone_door = door_create(server, NULL,
DOOR_UNREF | DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) < 0) {
zerror(zlogp, B_TRUE, "%s failed", "door_create");
return (-1);
}
(void) fdetach(zone_door_path);
if (fattach(zone_door, zone_door_path) != 0) {
zerror(zlogp, B_TRUE, "fattach to %s failed", zone_door_path);
(void) door_revoke(zone_door);
(void) fdetach(zone_door_path);
zone_door = -1;
return (-1);
}
return (0);
}
/*
* zoneadm(1m) will start zoneadmd if it thinks it isn't running; this
* is where zoneadmd itself will check to see that another instance of
* zoneadmd isn't already controlling this zone.
*
* The idea here is that we want to open the path to which we will
* attach our door, lock it, and then make sure that no-one has beat us
* to fattach(3c)ing onto it.
*
* fattach(3c) is really a mount, so there are actually two possible
* vnodes we could be dealing with. Our strategy is as follows:
*
* - If the file we opened is a regular file (common case):
* There is no fattach(3c)ed door, so we have a chance of becoming
* the managing zoneadmd. We attempt to lock the file: if it is
* already locked, that means someone else raced us here, so we
* lose and give up. zoneadm(1m) will try to contact the zoneadmd
* that beat us to it.
*
* - If the file we opened is a namefs file:
* This means there is already an established door fattach(3c)'ed
* to the rendezvous path. We've lost the race, so we give up.
* Note that in this case we also try to grab the file lock, and
* will succeed in acquiring it since the vnode locked by the
* "winning" zoneadmd was a regular one, and the one we locked was
* the fattach(3c)'ed door node. At any rate, no harm is done, and
* we just return to zoneadm(1m) which knows to retry.
*/
static int
make_daemon_exclusive(zlog_t *zlogp)
{
int doorfd = -1;
int err, ret = -1;
struct stat st;
struct flock flock;
zone_state_t zstate;
top:
if ((err = zone_get_state(zone_name, &zstate)) != Z_OK) {
zerror(zlogp, B_FALSE, "failed to get zone state: %s\n",
zonecfg_strerror(err));
goto out;
}
if ((doorfd = open(zone_door_path, O_CREAT|O_RDWR,
S_IREAD|S_IWRITE)) < 0) {
zerror(zlogp, B_TRUE, "failed to open %s", zone_door_path);
goto out;
}
if (fstat(doorfd, &st) < 0) {
zerror(zlogp, B_TRUE, "failed to stat %s", zone_door_path);
goto out;
}
/*
* Lock the file to synchronize with other zoneadmd
*/
flock.l_type = F_WRLCK;
flock.l_whence = SEEK_SET;
flock.l_start = (off_t)0;
flock.l_len = (off_t)0;
if (fcntl(doorfd, F_SETLK, &flock) < 0) {
/*
* Someone else raced us here and grabbed the lock file
* first. A warning here is inappropriate since nothing
* went wrong.
*/
goto out;
}
if (strcmp(st.st_fstype, "namefs") == 0) {
struct door_info info;
/*
* There is already something fattach()'ed to this file.
* Lets see what the door is up to.
*/
if (door_info(doorfd, &info) == 0 && info.di_target != -1) {
/*
* Another zoneadmd process seems to be in
* control of the situation and we don't need to
* be here. A warning here is inappropriate
* since nothing went wrong.
*
* If the door has been revoked, the zoneadmd
* process currently managing the zone is going
* away. We'll return control to zoneadm(1m)
* which will try again (by which time zoneadmd
* will hopefully have exited).
*/
goto out;
}
/*
* If we got this far, there's a fattach(3c)'ed door
* that belongs to a process that has exited, which can
* happen if the previous zoneadmd died unexpectedly.
*
* Let user know that something is amiss, but that we can
* recover; if the zone is in the installed state, then don't
* message, since having a running zoneadmd isn't really
* expected/needed. We want to keep occurences of this message
* limited to times when zoneadmd is picking back up from a
* zoneadmd that died while the zone was in some non-trivial
* state.
*/
if (zstate > ZONE_STATE_INSTALLED) {
zerror(zlogp, B_FALSE,
"zone '%s': WARNING: zone is in state '%s', but "
"zoneadmd does not appear to be available; "
"restarted zoneadmd to recover.",
zone_name, zone_state_str(zstate));
}
(void) fdetach(zone_door_path);
(void) close(doorfd);
goto top;
}
ret = 0;
out:
(void) close(doorfd);
return (ret);
}
int
main(int argc, char *argv[])
{
int opt;
zoneid_t zid;
priv_set_t *privset;
zone_state_t zstate;
char parents_locale[MAXPATHLEN];
int err;
pid_t pid;
sigset_t blockset;
sigset_t block_cld;
struct {
sema_t sem;
int status;
zlog_t log;
} *shstate;
size_t shstatelen = getpagesize();
zlog_t errlog;
zlog_t *zlogp;
progname = get_execbasename(argv[0]);
/*
* Make sure stderr is unbuffered
*/
(void) setbuffer(stderr, NULL, 0);
/*
* Get out of the way of mounted filesystems, since we will daemonize
* soon.
*/
(void) chdir("/");
/*
* Use the default system umask per PSARC 1998/110 rather than
* anything that may have been set by the caller.
*/
(void) umask(CMASK);
/*
* Initially we want to use our parent's locale.
*/
(void) setlocale(LC_ALL, "");
(void) textdomain(TEXT_DOMAIN);
(void) strlcpy(parents_locale, setlocale(LC_MESSAGES, NULL),
sizeof (parents_locale));
/*
* This zlog_t is used for writing to stderr
*/
errlog.logfile = stderr;
errlog.buflen = errlog.loglen = 0;
errlog.buf = errlog.log = NULL;
errlog.locale = parents_locale;
/*
* We start off writing to stderr until we're ready to daemonize.
*/
zlogp = &errlog;
/*
* Process options.
*/
while ((opt = getopt(argc, argv, "z:")) != EOF) {
switch (opt) {
case 'z':
zone_name = optarg;
break;
default:
usage();
}
}
if (zone_name == NULL)
usage();
/*
* Because usage() prints directly to stderr, it has gettext()
* wrapping, which depends on the locale. But since zerror() calls
* localize() which tweaks the locale, it is not safe to call zerror()
* until after the last call to usage(). Fortunately, the last call
* to usage() is just above and the first call to zerror() is just
* below. Don't mess this up.
*/
if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) {
zerror(zlogp, B_FALSE, "cannot manage the %s zone",
GLOBAL_ZONENAME);
return (1);
}
if (zone_get_id(zone_name, &zid) != 0) {
zerror(zlogp, B_FALSE, "could not manage %s: %s\n", zone_name,
zonecfg_strerror(Z_NO_ZONE));
return (1);
}
if ((err = zone_get_state(zone_name, &zstate)) != Z_OK) {
zerror(zlogp, B_FALSE, "failed to get zone state: %s\n",
zonecfg_strerror(err));
return (1);
}
if (zstate < ZONE_STATE_INSTALLED) {
zerror(zlogp, B_FALSE,
"cannot manage a zone which is in state '%s'",
zone_state_str(zstate));
return (1);
}
/*
* Check that we have all privileges. It would be nice to pare
* this down, but this is at least a first cut.
*/
if ((privset = priv_allocset()) == NULL) {
zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
return (1);
}
if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
zerror(zlogp, B_TRUE, "%s failed", "getppriv");
priv_freeset(privset);
return (1);
}
if (priv_isfullset(privset) == B_FALSE) {
zerror(zlogp, B_FALSE, "You lack sufficient privilege to "
"run this command (all privs required)\n");
priv_freeset(privset);
return (1);
}
priv_freeset(privset);
if (mkzonedir(zlogp) != 0)
return (1);
/*
* Pre-fork: setup shared state
*/
if ((shstate = (void *)mmap(NULL, shstatelen,
PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, (off_t)0)) ==
MAP_FAILED) {
zerror(zlogp, B_TRUE, "%s failed", "mmap");
return (1);
}
if (sema_init(&shstate->sem, 0, USYNC_PROCESS, NULL) != 0) {
zerror(zlogp, B_TRUE, "%s failed", "sema_init()");
(void) munmap((char *)shstate, shstatelen);
return (1);
}
shstate->log.logfile = NULL;
shstate->log.buflen = shstatelen - sizeof (*shstate);
shstate->log.loglen = shstate->log.buflen;
shstate->log.buf = (char *)shstate + sizeof (*shstate);
shstate->log.log = shstate->log.buf;
shstate->log.locale = parents_locale;
shstate->status = -1;
/*
* We need a SIGCHLD handler so the sema_wait() below will wake
* up if the child dies without doing a sema_post().
*/
(void) sigset(SIGCHLD, sigchld);
/*
* We must mask SIGCHLD until after we've coped with the fork
* sufficiently to deal with it; otherwise we can race and
* receive the signal before pid has been initialized
* (yes, this really happens).
*/
(void) sigemptyset(&block_cld);
(void) sigaddset(&block_cld, SIGCHLD);
(void) sigprocmask(SIG_BLOCK, &block_cld, NULL);
/*
* Do not let another thread localize a message while we are forking.
*/
(void) mutex_lock(&msglock);
pid = fork();
(void) mutex_unlock(&msglock);
if (pid == -1) {
zerror(zlogp, B_TRUE, "could not fork");
return (1);
} else if (pid > 0) { /* parent */
(void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL);
/*
* This marks a window of vulnerability in which we receive
* the SIGCLD before falling into sema_wait (normally we would
* get woken up from sema_wait with EINTR upon receipt of
* SIGCLD). So we may need to use some other scheme like
* sema_posting in the sigcld handler.
* blech
*/
(void) sema_wait(&shstate->sem);
(void) sema_destroy(&shstate->sem);
if (shstate->status != 0)
(void) waitpid(pid, NULL, WNOHANG);
/*
* It's ok if we die with SIGPIPE. It's not like we could have
* done anything about it.
*/
(void) fprintf(stderr, "%s", shstate->log.buf);
_exit(shstate->status == 0 ? 0 : 1);
}
/*
* The child charges on.
*/
(void) sigset(SIGCHLD, SIG_DFL);
(void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL);
/*
* SIGPIPE can be delivered if we write to a socket for which the
* peer endpoint is gone. That can lead to too-early termination
* of zoneadmd, and that's not good eats.
*/
(void) sigset(SIGPIPE, SIG_IGN);
/*
* Stop using stderr
*/
zlogp = &shstate->log;
/*
* We don't need stdout/stderr from now on.
*/
closefrom(0);
/*
* Initialize the syslog zlog_t. This needs to be done after
* the call to closefrom().
*/
logsys.buf = logsys.log = NULL;
logsys.buflen = logsys.loglen = 0;
logsys.logfile = NULL;
logsys.locale = DEFAULT_LOCALE;
openlog("zoneadmd", LOG_PID, LOG_DAEMON);
/*
* The eventstream is used to publish state changes in the zone
* from the door threads to the console I/O poller.
*/
if (eventstream_init() == -1) {
zerror(zlogp, B_TRUE, "unable to create eventstream");
goto child_out;
}
(void) snprintf(zone_door_path, sizeof (zone_door_path),
ZONE_DOOR_PATH, zone_name);
/*
* See if another zoneadmd is running for this zone. If not, then we
* can now modify system state.
*/
if (make_daemon_exclusive(zlogp) == -1)
goto child_out;
/*
* Create/join a new session; we need to be careful of what we do with
* the console from now on so we don't end up being the session leader
* for the terminal we're going to be handing out.
*/
(void) setsid();
/*
* This thread shouldn't be receiving any signals; in particular,
* SIGCHLD should be received by the thread doing the fork().
*/
(void) sigfillset(&blockset);
(void) thr_sigsetmask(SIG_BLOCK, &blockset, NULL);
/*
* Setup the console device and get ready to serve the console;
* once this has completed, we're ready to let console clients
* make an attempt to connect (they will block until
* serve_console_sock() below gets called, and any pending
* connection is accept()ed).
*/
if (init_console(zlogp) == -1)
goto child_out;
/*
* Take the lock now, so that when the door server gets going, we
* are guaranteed that it won't take a request until we are sure
* that everything is completely set up. See the child_out: label
* below to see why this matters.
*/
(void) mutex_lock(&lock);
/*
* Note: door setup must occur *after* the console is setup.
* This is so that as zlogin tests the door to see if zoneadmd
* is ready yet, we know that the console will get serviced
* once door_info() indicates that the door is "up".
*/
if (setup_door(zlogp) == -1)
goto child_out;
/*
* Things seem OK so far; tell the parent process that we're done
* with setup tasks. This will cause the parent to exit, signalling
* to zoneadm, zlogin, or whatever forked it that we are ready to
* service requests.
*/
shstate->status = 0;
(void) sema_post(&shstate->sem);
(void) munmap((char *)shstate, shstatelen);
shstate = NULL;
(void) mutex_unlock(&lock);
/*
* zlogp is now invalid, so reset it to the syslog logger.
*/
zlogp = &logsys;
/*
* Now that we are free of any parents, switch to the default locale.
*/
(void) setlocale(LC_ALL, DEFAULT_LOCALE);
/*
* At this point the setup portion of main() is basically done, so
* we reuse this thread to manage the zone console. When
* serve_console() has returned, we are past the point of no return
* in the life of this zoneadmd.
*/
serve_console(zlogp);
assert(in_death_throes);
/*
* This is the next-to-last part of the exit interlock. Upon calling
* fdetach(), the door will go unreferenced; once any
* outstanding requests (like the door thread doing Z_HALT) are
* done, the door will get an UNREF notification; when it handles
* the UNREF, the door server will cause the exit.
*/
assert(!MUTEX_HELD(&lock));
(void) fdetach(zone_door_path);
for (;;)
(void) pause();
child_out:
assert(pid == 0);
if (shstate != NULL) {
shstate->status = -1;
(void) sema_post(&shstate->sem);
(void) munmap((char *)shstate, shstatelen);
}
/*
* This might trigger an unref notification, but if so,
* we are still holding the lock, so our call to exit will
* ultimately win the race and will publish the right exit
* code.
*/
if (zone_door != -1) {
assert(MUTEX_HELD(&lock));
(void) door_revoke(zone_door);
(void) fdetach(zone_door_path);
}
return (1); /* return from main() forcibly exits an MT process */
}
|