|
root / base / usr / src / cmd / halt / halt.c
halt.c C 1661 lines 38.1 KB
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
/*
 * 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 2010 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */
/*
 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
 * Copyright (c) 2015 by Delphix. All rights reserved.
 * Copyright 2016 Toomas Soome <tsoome@me.com>
 * Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
 */

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

/*
 * University Copyright- Copyright (c) 1982, 1986, 1988
 * The Regents of the University of California
 * All Rights Reserved
 *
 * University Acknowledgment- Portions of this document are derived from
 * software developed by the University of California, Berkeley, and its
 * contributors.
 * Portions contributed by Juergen Keil, <jk@tools.de>.
 */


/*
 * Common code for halt(8), poweroff(8), and reboot(8).  We use
 * argv[0] to determine which behavior to exhibit.
 */

#include <stdio.h>
#include <procfs.h>
#include <sys/types.h>
#include <sys/elf.h>
#include <sys/systeminfo.h>
#include <sys/stat.h>
#include <sys/uadmin.h>
#include <sys/mntent.h>
#include <sys/mnttab.h>
#include <sys/mount.h>
#include <sys/fs/ufs_mount.h>
#include <alloca.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <libscf.h>
#include <libscf_priv.h>
#include <limits.h>
#include <locale.h>
#include <libintl.h>
#include <syslog.h>
#include <signal.h>
#include <strings.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
#include <time.h>
#include <wait.h>
#include <ctype.h>
#include <utmpx.h>
#include <pwd.h>
#include <zone.h>
#include <spawn.h>

#include <libzfs.h>
#if defined(__x86)
#include <libbe.h>
#endif

#if !defined(TEXT_DOMAIN)
#define	TEXT_DOMAIN	"SYS_TEST"
#endif

#if defined(__sparc)
#define	CUR_ELFDATA	ELFDATA2MSB
#elif defined(__x86)
#define	CUR_ELFDATA	ELFDATA2LSB
#endif

static libzfs_handle_t *g_zfs;

extern int audit_halt_setup(int, char **);
extern int audit_halt_success(void);
extern int audit_halt_fail(void);

extern int audit_reboot_setup(void);
extern int audit_reboot_success(void);
extern int audit_reboot_fail(void);

static char *cmdname;	/* basename(argv[0]), the name of the command */

typedef struct ctidlist_struct {
	ctid_t ctid;
	struct ctidlist_struct *next;
} ctidlist_t;

static ctidlist_t *ctidlist = NULL;
static ctid_t startdct = -1;

#define	FMRI_STARTD_CONTRACT \
	"svc:/system/svc/restarter:default/:properties/restarter/contract"

#define	BEADM_PROG	"/sbin/beadm"
#define	BOOTADM_PROG	"/sbin/bootadm"
#define	ZONEADM_PROG	"/usr/sbin/zoneadm"

/*
 * The length of FASTBOOT_MOUNTPOINT must be less than MAXPATHLEN.
 */
#define	FASTBOOT_MOUNTPOINT	"/tmp/.fastboot.root"

/*
 * Fast Reboot related variables
 */
static char	fastboot_mounted[MAXPATHLEN];

#if defined(__x86)
static char *fbarg;
static char *fbarg_used;
static int fbarg_entnum = BE_ENTRY_DEFAULT;
#endif	/* __x86 */

static int validate_ufs_disk(char *, char *);
static int validate_zfs_pool(char *, char *);

static pid_t
get_initpid()
{
	static int init_pid = -1;

	if (init_pid == -1) {
		if (zone_getattr(getzoneid(), ZONE_ATTR_INITPID, &init_pid,
		    sizeof (init_pid)) != sizeof (init_pid)) {
			assert(errno == ESRCH);
			init_pid = -1;
		}
	}
	return (init_pid);
}

/*
 * Quiesce or resume init using /proc.  When stopping init, we can't send
 * SIGTSTP (since init ignores it) or SIGSTOP (since the kernel won't permit
 * it).
 */
static int
direct_init(long command)
{
	char ctlfile[MAXPATHLEN];
	pid_t pid;
	int ctlfd;

	assert(command == PCDSTOP || command == PCRUN);
	if ((pid = get_initpid()) == -1) {
		return (-1);
	}

	(void) snprintf(ctlfile, sizeof (ctlfile), "/proc/%d/ctl", pid);
	if ((ctlfd = open(ctlfile, O_WRONLY)) == -1)
		return (-1);

	if (command == PCDSTOP) {
		if (write(ctlfd, &command, sizeof (long)) == -1) {
			(void) close(ctlfd);
			return (-1);
		}
	} else {	/* command == PCRUN */
		long cmds[2];
		cmds[0] = command;
		cmds[1] = 0;
		if (write(ctlfd, cmds, sizeof (cmds)) == -1) {
			(void) close(ctlfd);
			return (-1);
		}
	}
	(void) close(ctlfd);
	return (0);
}

static void
stop_startd()
{
	scf_handle_t *h;
	scf_property_t *prop = NULL;
	scf_value_t *val = NULL;
	uint64_t uint64;

	if ((h = scf_handle_create(SCF_VERSION)) == NULL)
		return;

	if ((scf_handle_bind(h) != 0) ||
	    ((prop = scf_property_create(h)) == NULL) ||
	    ((val = scf_value_create(h)) == NULL))
		goto out;

	if (scf_handle_decode_fmri(h, FMRI_STARTD_CONTRACT,
	    NULL, NULL, NULL, NULL, prop, SCF_DECODE_FMRI_EXACT) != 0)
		goto out;

	if (scf_property_is_type(prop, SCF_TYPE_COUNT) != 0 ||
	    scf_property_get_value(prop, val) != 0 ||
	    scf_value_get_count(val, &uint64) != 0)
		goto out;

	startdct = (ctid_t)uint64;
	(void) sigsend(P_CTID, startdct, SIGSTOP);

out:
	scf_property_destroy(prop);
	scf_value_destroy(val);
	scf_handle_destroy(h);
}

static void
continue_startd()
{
	if (startdct != -1)
		(void) sigsend(P_CTID, startdct, SIGCONT);
}

#define	FMRI_RESTARTER_PROP "/:properties/general/restarter"
#define	FMRI_CONTRACT_PROP "/:properties/restarter/contract"

static int
save_ctid(ctid_t ctid)
{
	ctidlist_t *next;

	for (next = ctidlist; next != NULL; next = next->next)
		if (next->ctid == ctid)
			return (-1);

	next = (ctidlist_t *)malloc(sizeof (ctidlist_t));
	if (next == NULL)
		return (-1);

	next->ctid = ctid;
	next->next = ctidlist;
	ctidlist = next;
	return (0);
}

static void
stop_delegates()
{
	ctid_t ctid;
	scf_handle_t *h;
	scf_scope_t *sc = NULL;
	scf_service_t *svc = NULL;
	scf_instance_t *inst = NULL;
	scf_snapshot_t *snap = NULL;
	scf_snapshot_t *isnap = NULL;
	scf_propertygroup_t *pg = NULL;
	scf_property_t *prop = NULL;
	scf_value_t *val = NULL;
	scf_iter_t *siter = NULL;
	scf_iter_t *iiter = NULL;
	char *fmri;
	ssize_t length;

	uint64_t uint64;
	ssize_t bytes;

	length = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH);
	if (length <= 0)
		return;

	length++;
	fmri = alloca(length * sizeof (char));

	if ((h = scf_handle_create(SCF_VERSION)) == NULL)
		return;

	if (scf_handle_bind(h) != 0) {
		scf_handle_destroy(h);
		return;
	}

	if ((sc = scf_scope_create(h)) == NULL ||
	    (svc = scf_service_create(h)) == NULL ||
	    (inst = scf_instance_create(h)) == NULL ||
	    (snap = scf_snapshot_create(h)) == NULL ||
	    (pg = scf_pg_create(h)) == NULL ||
	    (prop = scf_property_create(h)) == NULL ||
	    (val = scf_value_create(h)) == NULL ||
	    (siter = scf_iter_create(h)) == NULL ||
	    (iiter = scf_iter_create(h)) == NULL)
		goto out;

	if (scf_handle_get_scope(h, SCF_SCOPE_LOCAL, sc) != 0)
		goto out;

	if (scf_iter_scope_services(siter, sc) != 0)
		goto out;

	while (scf_iter_next_service(siter, svc) == 1) {

		if (scf_iter_service_instances(iiter, svc) != 0)
			continue;

		while (scf_iter_next_instance(iiter, inst) == 1) {

			if ((scf_instance_get_snapshot(inst, "running",
			    snap)) != 0)
				isnap = NULL;
			else
				isnap = snap;

			if (scf_instance_get_pg_composed(inst, isnap,
			    SCF_PG_GENERAL, pg) != 0)
				continue;

			if (scf_pg_get_property(pg, SCF_PROPERTY_RESTARTER,
			    prop) != 0 ||
			    scf_property_get_value(prop, val) != 0)
				continue;

			bytes = scf_value_get_astring(val, fmri, length);
			if (bytes <= 0 || bytes >= length)
				continue;

			if (strlcat(fmri, FMRI_CONTRACT_PROP, length) >=
			    length)
				continue;

			if (scf_handle_decode_fmri(h, fmri, NULL, NULL,
			    NULL, NULL, prop, SCF_DECODE_FMRI_EXACT) != 0)
				continue;

			if (scf_property_is_type(prop, SCF_TYPE_COUNT) != 0 ||
			    scf_property_get_value(prop, val) != 0 ||
			    scf_value_get_count(val, &uint64) != 0)
				continue;

			ctid = (ctid_t)uint64;
			if (save_ctid(ctid) == 0) {
				(void) sigsend(P_CTID, ctid, SIGSTOP);
			}
		}
	}
out:
	scf_scope_destroy(sc);
	scf_service_destroy(svc);
	scf_instance_destroy(inst);
	scf_snapshot_destroy(snap);
	scf_pg_destroy(pg);
	scf_property_destroy(prop);
	scf_value_destroy(val);
	scf_iter_destroy(siter);
	scf_iter_destroy(iiter);

	(void) scf_handle_unbind(h);
	scf_handle_destroy(h);
}

static void
continue_delegates()
{
	ctidlist_t *next;
	for (next = ctidlist; next != NULL; next = next->next)
		(void) sigsend(P_CTID, next->ctid, SIGCONT);
}

#define	FMRI_GDM "svc:/application/graphical-login/gdm:default"
#define	GDM_STOP_TIMEOUT	10	/* Give gdm 10 seconds to shut down */

/*
 * If gdm is running, try to stop gdm.
 * Returns  0 on success, -1 on failure.
 */
static int
stop_gdm()
{
	char *gdm_state = NULL;
	int retry = 0;

	/*
	 * If gdm is running, try to stop gdm.
	 */
	while ((gdm_state = smf_get_state(FMRI_GDM)) != NULL &&
	    strcmp(gdm_state, SCF_STATE_STRING_ONLINE) == 0 &&
	    retry++ < GDM_STOP_TIMEOUT) {

		free(gdm_state);

		/*
		 * Only need to disable once.
		 */
		if (retry == 1 &&
		    smf_disable_instance(FMRI_GDM, SMF_TEMPORARY) != 0) {
			(void) fprintf(stderr,
			    gettext("%s: Failed to stop %s: %s.\n"),
			    cmdname, FMRI_GDM, scf_strerror(scf_error()));
			return (-1);
		}
		(void) sleep(1);
	}

	if (retry >= GDM_STOP_TIMEOUT) {
		(void) fprintf(stderr, gettext("%s: Failed to stop %s.\n"),
		    cmdname, FMRI_GDM);
		return (-1);
	}

	return (0);
}


static void
stop_restarters()
{
	stop_startd();
	stop_delegates();
}

static void
continue_restarters()
{
	continue_startd();
	continue_delegates();
}

/*
 * Copy an array of strings into buf, separated by spaces.  Returns 0 on
 * success.
 */
static int
gather_args(char **args, char *buf, size_t buf_sz)
{
	if (strlcpy(buf, *args, buf_sz) >= buf_sz)
		return (-1);

	for (++args; *args != NULL; ++args) {
		if (strlcat(buf, " ", buf_sz) >= buf_sz)
			return (-1);
		if (strlcat(buf, *args, buf_sz) >= buf_sz)
			return (-1);
	}

	return (0);
}

/*
 * Halt every zone on the system.  We are committed to doing a shutdown
 * even if something goes wrong here. If something goes wrong, we just
 * continue with the shutdown.  Return non-zero if we need to wait for zones to
 * halt later on.
 */
static int
halt_zones()
{
	pid_t pid;
	zoneid_t *zones;
	size_t nz = 0, old_nz;
	int i;
	char zname[ZONENAME_MAX];

	/*
	 * Get a list of zones. If the number of zones changes in between the
	 * two zone_list calls, try again.
	 */

	for (;;) {
		(void) zone_list(NULL, &nz);
		if (nz == 1)
			return (0);
		old_nz = nz;
		zones = calloc(sizeof (zoneid_t), nz);
		if (zones == NULL) {
			(void) fprintf(stderr,
			    gettext("%s: Could not halt zones"
			    " (out of memory).\n"), cmdname);
			return (0);
		}

		(void) zone_list(zones, &nz);
		if (old_nz == nz)
			break;
		free(zones);
	}

	if (nz == 2) {
		(void) fprintf(stderr, gettext("%s: Halting 1 zone.\n"),
		    cmdname);
	} else {
		(void) fprintf(stderr, gettext("%s: Halting %i zones.\n"),
		    cmdname, nz - 1);
	}

	for (i = 0; i < nz; i++) {
		if (zones[i] == GLOBAL_ZONEID)
			continue;
		if (getzonenamebyid(zones[i], zname, sizeof (zname)) < 0) {
			/*
			 * getzonenamebyid should only fail if we raced with
			 * another process trying to shut down the zone.
			 * We assume this happened and ignore the error.
			 */
			if (errno != EINVAL) {
				(void) fprintf(stderr,
				    gettext("%s: Unexpected error while "
				    "looking up zone %ul: %s.\n"),
				    cmdname, zones[i], strerror(errno));
			}

			continue;
		}
		pid = fork();
		if (pid < 0) {
			(void) fprintf(stderr,
			    gettext("%s: Zone \"%s\" could not be"
			    " halted (could not fork(): %s).\n"),
			    cmdname, zname, strerror(errno));
			continue;
		}
		if (pid == 0) {
			(void) execl(ZONEADM_PROG, ZONEADM_PROG,
			    "-z", zname, "halt", NULL);
			(void) fprintf(stderr,
			    gettext("%s: Zone \"%s\" could not be halted"
			    " (cannot exec(" ZONEADM_PROG "): %s).\n"),
			    cmdname, zname, strerror(errno));
			exit(0);
		}
	}

	return (1);
}

/*
 * This function tries to wait for all non-global zones to go away.
 * It will timeout if no progress is made for 5 seconds, or a total of
 * 30 seconds elapses.
 */

static void
check_zones_haltedness()
{
	int t = 0, t_prog = 0;
	size_t nz = 0, last_nz;

	do {
		last_nz = nz;
		(void) zone_list(NULL, &nz);
		if (nz == 1)
			return;

		(void) sleep(1);

		if (last_nz > nz)
			t_prog = 0;

		t++;
		t_prog++;

		if (t == 10) {
			if (nz == 2) {
				(void) fprintf(stderr,
				    gettext("%s: Still waiting for 1 zone to "
				    "halt. Will wait up to 20 seconds.\n"),
				    cmdname);
			} else {
				(void) fprintf(stderr,
				    gettext("%s: Still waiting for %i zones "
				    "to halt. Will wait up to 20 seconds.\n"),
				    cmdname, nz - 1);
			}
		}

	} while ((t < 30) && (t_prog < 5));
}


/*
 * Validate that this is a root disk or dataset
 * Returns 0 if it is a root disk or dataset;
 * returns 1 if it is a disk argument or dataset, but not valid or not root;
 * returns -1 if it is not a valid argument or a disk argument.
 */
static int
validate_disk(char *arg, char *mountpoint)
{
	static char root_dev_path[] = "/dev/dsk";
	char kernpath[MAXPATHLEN];
	struct stat64 statbuf;
	int rc = 0;

	if (strlen(arg) > MAXPATHLEN) {
		(void) fprintf(stderr,
		    gettext("%s: Argument is too long\n"), cmdname);
		return (-1);
	}

	bcopy(FASTBOOT_MOUNTPOINT, mountpoint, sizeof (FASTBOOT_MOUNTPOINT));

	if (strstr(arg, mountpoint) == NULL) {
		/*
		 * Do a force umount just in case some other filesystem has
		 * been mounted there.
		 */
		(void) umount2(mountpoint, MS_FORCE);
	}

	/* Create the directory if it doesn't already exist */
	if (lstat64(mountpoint, &statbuf) != 0) {
		if (mkdirp(mountpoint, 0755) != 0) {
			(void) fprintf(stderr,
			    gettext("Failed to create mountpoint %s\n"),
			    mountpoint);
			return (-1);
		}
	}

	if (strncmp(arg, root_dev_path, strlen(root_dev_path)) == 0) {
		/* ufs root disk argument */
		rc = validate_ufs_disk(arg, mountpoint);
	} else {
		/* zfs root pool argument */
		rc = validate_zfs_pool(arg, mountpoint);
	}

	if (rc != 0)
		return (rc);

	/*
	 * Check for the kernel at the flat path (/kernel/unix)
	 */
	(void) snprintf(kernpath, MAXPATHLEN, "%s/kernel/unix", mountpoint);
	if (stat64(kernpath, &statbuf) == 0)
		return (0);

	/*
	 * Legacy fallback: check old platform path for older installations
	 */
	(void) snprintf(kernpath, MAXPATHLEN,
	    "%s/platform/i86pc/kernel/amd64/unix", mountpoint);
	if (stat64(kernpath, &statbuf) == 0)
		return (0);

	(void) fprintf(stderr,
	    gettext("%s: %s is not a root disk or dataset\n"),
	    cmdname, arg);
	return (1);
}


static int
validate_ufs_disk(char *arg, char *mountpoint)
{
	struct ufs_args	ufs_args = { 0 };
	char mntopts[MNT_LINE_MAX] = MNTOPT_LARGEFILES;

	/* perform the mount */
	ufs_args.flags = UFSMNT_LARGEFILES;
	if (mount(arg, mountpoint, MS_DATA|MS_OPTIONSTR,
	    MNTTYPE_UFS, &ufs_args, sizeof (ufs_args),
	    mntopts, sizeof (mntopts)) != 0) {
		perror(cmdname);
		(void) fprintf(stderr,
		    gettext("%s: Failed to mount %s\n"), cmdname, arg);
		return (-1);
	}

	return (0);
}

static int
validate_zfs_pool(char *arg, char *mountpoint)
{
	zfs_handle_t *zhp = NULL;
	char mntopts[MNT_LINE_MAX] = { '\0' };
	int rc = 0;

	if ((g_zfs = libzfs_init()) == NULL) {
		(void) fprintf(stderr, gettext("Internal error: failed to "
		    "initialize ZFS library\n"));
		return (-1);
	}

	/* Try to open the dataset */
	if ((zhp = zfs_open(g_zfs, arg,
	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_DATASET)) == NULL)
		return (-1);

	/* perform the mount */
	if (mount(zfs_get_name(zhp), mountpoint, MS_DATA|MS_OPTIONSTR|MS_RDONLY,
	    MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) {
		perror(cmdname);
		(void) fprintf(stderr,
		    gettext("%s: Failed to mount %s\n"), cmdname, arg);
		rc = -1;
	}

	if (zhp != NULL)
		zfs_close(zhp);

	libzfs_fini(g_zfs);
	return (rc);
}

/*
 * Return 0 if not zfs, or is zfs and have successfully constructed the
 * boot argument; returns non-zero otherwise.
 * At successful completion fpth contains pointer where mount point ends.
 * NOTE: arg is supposed to be the resolved path
 */
static int
get_zfs_bootfs_arg(const char *arg, const char ** fpth, int *is_zfs,
    char *bootfs_arg)
{
	zfs_handle_t *zhp = NULL;
	zpool_handle_t *zpoolp = NULL;
	FILE *mtabp = NULL;
	struct mnttab mnt;
	char *poolname = NULL;
	char physpath[MAXPATHLEN];
	char mntsp[ZFS_MAX_DATASET_NAME_LEN];
	char bootfs[ZFS_MAX_DATASET_NAME_LEN];
	int rc = 0;
	size_t mntlen = 0;
	size_t msz;
	static char fmt[] = "-B zfs-bootfs=%s,bootpath=\"%s\"";

	*fpth = arg;
	*is_zfs = 0;

	bzero(physpath, sizeof (physpath));
	bzero(bootfs, sizeof (bootfs));

	if ((mtabp = fopen(MNTTAB, "r")) == NULL) {
		return (-1);
	}

	while (getmntent(mtabp, &mnt) == 0) {
		if (strstr(arg, mnt.mnt_mountp) == arg &&
		    (msz = strlen(mnt.mnt_mountp)) > mntlen) {
			mntlen = msz;
			*is_zfs = strcmp(MNTTYPE_ZFS, mnt.mnt_fstype) == 0;
			(void) strlcpy(mntsp, mnt.mnt_special, sizeof (mntsp));
		}
	}

	(void) fclose(mtabp);

	if (mntlen > 1)
		*fpth += mntlen;

	if (!*is_zfs)
		return (0);

	if ((g_zfs = libzfs_init()) == NULL)
		return (-1);

	/* Try to open the dataset */
	if ((zhp = zfs_open(g_zfs, mntsp,
	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_DATASET)) == NULL) {
		(void) fprintf(stderr, gettext("Cannot open %s\n"), mntsp);
		rc = -1;
		goto validate_zfs_err_out;
	}

	(void) strlcpy(bootfs, mntsp, sizeof (bootfs));

	if ((poolname = strtok(mntsp, "/")) == NULL) {
		rc = -1;
		goto validate_zfs_err_out;
	}

	if ((zpoolp = zpool_open(g_zfs, poolname)) == NULL) {
		(void) fprintf(stderr, gettext("Cannot open %s\n"), poolname);
		rc = -1;
		goto validate_zfs_err_out;
	}

	if (zpool_get_physpath(zpoolp, physpath, sizeof (physpath)) != 0) {
		(void) fprintf(stderr, gettext("Cannot find phys_path\n"));
		rc = -1;
		goto validate_zfs_err_out;
	}

	/*
	 * For the mirror physpath would contain the list of all
	 * bootable devices, pick up the first one.
	 */
	(void) strtok(physpath, " ");
	if (snprintf(bootfs_arg, BOOTARGS_MAX, fmt, bootfs, physpath) >=
	    BOOTARGS_MAX) {
		rc = E2BIG;
		(void) fprintf(stderr,
		    gettext("Boot arguments are too long\n"));
	}

validate_zfs_err_out:
	if (zhp != NULL)
		zfs_close(zhp);

	if (zpoolp != NULL)
		zpool_close(zpoolp);

	libzfs_fini(g_zfs);
	return (rc);
}

/*
 * Validate that the file exists, and is an ELF file.
 * Returns 0 on success, -1 on failure.
 */
static int
validate_unix(char *arg, int *mplen, int *is_zfs, char *bootfs_arg)
{
	const char *location;
	int class, format;
	unsigned char ident[EI_NIDENT];
	char physpath[MAXPATHLEN];
	int elffd = -1;
	size_t	sz;

	if ((sz = resolvepath(arg, physpath, sizeof (physpath) - 1)) ==
	    (size_t)-1) {
		(void) fprintf(stderr,
		    gettext("Cannot resolve path for %s: %s\n"),
		    arg, strerror(errno));
		return (-1);
	}
	(void) strlcpy(arg, physpath, sz + 1);

	if (strlen(arg) > MAXPATHLEN) {
		(void) fprintf(stderr,
		    gettext("%s: New kernel name is too long\n"), cmdname);
		return (-1);
	}

	if (strncmp(basename(arg), "unix", 4) != 0) {
		(void) fprintf(stderr,
		    gettext("%s: %s: Kernel name must be unix\n"),
		    cmdname, arg);
		return (-1);
	}

	if (get_zfs_bootfs_arg(arg, &location, is_zfs, bootfs_arg) != 0)
		goto err_out;

	*mplen = location - arg;

	if (strstr(location, "/boot/platform") == location) {
		/*
		 * Rebooting to failsafe.
		 * Clear bootfs_arg and is_zfs flag.
		 */
		bootfs_arg[0] = 0;
		*is_zfs = 0;
	} else if (strstr(location, "/platform") != location) {
		(void) fprintf(stderr,
		    gettext("%s: %s: No /platform in file name\n"),
		    cmdname, arg);
		goto err_out;
	}

	if ((elffd = open64(arg, O_RDONLY)) < 0 ||
	    (pread64(elffd, ident, EI_NIDENT, 0) != EI_NIDENT)) {
		(void) fprintf(stderr, "%s: %s: %s\n",
		    cmdname, arg, strerror(errno));
		goto err_out;
	}

	class = ident[EI_CLASS];

	if ((class != ELFCLASS32 && class != ELFCLASS64) ||
	    memcmp(&ident[EI_MAG0], ELFMAG, 4) != 0) {
		(void) fprintf(stderr,
		    gettext("%s: %s: Not a valid ELF file\n"), cmdname, arg);
		goto err_out;
	}

	format = ident[EI_DATA];

	if (format != CUR_ELFDATA) {
		(void) fprintf(stderr, gettext("%s: %s: Invalid data format\n"),
		    cmdname, arg);
		goto err_out;
	}

	return (0);

err_out:
	if (elffd >= 0) {
		(void) close(elffd);
		elffd = -1;
	}
	return (-1);
}

static int
halt_exec(const char *path, ...)
{
	pid_t		pid;
	int		i;
	int		st;
	const char	*arg;
	va_list	vp;
	const char	*argv[256];

	if ((pid = fork()) == -1) {
		return (errno);
	} else if (pid == 0) {
		(void) fclose(stdout);
		(void) fclose(stderr);

		argv[0] = path;
		i = 1;

		va_start(vp, path);

		do {
			arg = va_arg(vp, const char *);
			argv[i] = arg;
		} while (arg != NULL &&
		    ++i != sizeof (argv) / sizeof (argv[0]));

		va_end(vp);

		(void) execve(path, (char * const *)argv, NULL);
		(void) fprintf(stderr, gettext("Cannot execute %s: %s\n"),
		    path, strerror(errno));
		exit(-1);
	} else {
		if (waitpid(pid, &st, 0) == pid &&
		    !WIFSIGNALED(st) && WIFEXITED(st))
			st = WEXITSTATUS(st);
		else
			st = -1;
	}
	return (st);
}

/*
 * Mount the specified BE.
 *
 * Upon success returns zero and copies bename string to mountpoint[]
 */
static int
fastboot_bename(const char *bename, char *mountpoint, size_t mpsz)
{
	int rc;

	/*
	 * Attempt to unmount the BE first in case it's already mounted
	 * elsewhere.
	 */
	(void) halt_exec(BEADM_PROG, "umount", bename, NULL);

	if ((rc = halt_exec(BEADM_PROG, "mount", bename, FASTBOOT_MOUNTPOINT,
	    NULL)) != 0)
		(void) fprintf(stderr,
		    gettext("%s: Unable to mount BE \"%s\" at %s\n"),
		    cmdname, bename, FASTBOOT_MOUNTPOINT);
	else
		(void) strlcpy(mountpoint, FASTBOOT_MOUNTPOINT, mpsz);

	return (rc);
}

/*
 * Returns 0 on successful parsing of the arguments;
 * returns EINVAL on parsing failures that should abort the reboot attempt;
 * returns other error code to fall back to regular reboot.
 */
static int
parse_fastboot_args(char *bootargs_buf, size_t buf_size,
    int *is_dryrun, const char *bename)
{
	char mountpoint[MAXPATHLEN];
	char bootargs_saved[BOOTARGS_MAX];
	char bootargs_scratch[BOOTARGS_MAX];
	char bootfs_arg[BOOTARGS_MAX];
	char unixfile[BOOTARGS_MAX];
	char *head, *newarg;
	int buflen;		/* length of the bootargs_buf */
	int mplen;		/* length of the mount point */
	int rootlen = 0;	/* length of the root argument */
	int unixlen = 0;	/* length of the unix argument */
	int off = 0;		/* offset into the new boot argument */
	int is_zfs = 0;
	int rc = 0;

	bzero(mountpoint, sizeof (mountpoint));

	/*
	 * If argc is not 0, buflen is length of the argument being passed in;
	 * else it is 0 as bootargs_buf has been initialized to all 0's.
	 */
	buflen = strlen(bootargs_buf);

	/* Save a copy of the original argument */
	bcopy(bootargs_buf, bootargs_saved, buflen);
	bzero(&bootargs_saved[buflen], sizeof (bootargs_saved) - buflen);

	/* Save another copy to be used by strtok */
	bcopy(bootargs_buf, bootargs_scratch, buflen);
	bzero(&bootargs_scratch[buflen], sizeof (bootargs_scratch) - buflen);
	head = &bootargs_scratch[0];

	/* Get the first argument */
	newarg = strtok(bootargs_scratch, " ");

	/*
	 * If this is a dry run request, verify that the drivers can handle
	 * fast reboot.
	 */
	if (newarg && strncasecmp(newarg, "dryrun", strlen("dryrun")) == 0) {
		*is_dryrun = 1;
		(void) system("/sbin/devfsadm");
	}

	/*
	 * Always perform a dry run to identify all the drivers that
	 * need to implement devo_reset().
	 */
	if (uadmin(A_SHUTDOWN, AD_FASTREBOOT_DRYRUN,
	    (uintptr_t)bootargs_saved) != 0) {
		(void) fprintf(stderr, gettext("%s: Not all drivers "
		    "have implemented quiesce(9E)\n"
		    "\tPlease see /var/adm/messages for drivers that haven't\n"
		    "\timplemented quiesce(9E).\n"), cmdname);
	} else if (*is_dryrun) {
		(void) fprintf(stderr, gettext("%s: All drivers have "
		    "implemented quiesce(9E)\n"), cmdname);
	}

	/* Return if it is a true dry run. */
	if (*is_dryrun)
		return (rc);

#if defined(__x86)
	/* Read boot args from Boot Environment */
	if ((bootargs_buf[0] == 0 || isdigit(bootargs_buf[0])) &&
	    bename == NULL) {
		/*
		 * If no boot arguments are given, or a BE entry
		 * number is provided, process the boot arguments from BE.
		 */
		int entnum;
		if (bootargs_buf[0] == 0)
			entnum = BE_ENTRY_DEFAULT;
		else {
			errno = 0;
			entnum = strtoul(bootargs_buf, NULL, 10);
			rc = errno;
		}

		if (rc == 0 && (rc = be_get_boot_args(&fbarg, entnum)) == 0) {
			if (strlcpy(bootargs_buf, fbarg,
			    buf_size) >= buf_size) {
				free(fbarg);
				bcopy(bootargs_saved, bootargs_buf, buf_size);
				rc = E2BIG;
			}
		}
		/* Failed to read FB args, fall back to normal reboot */
		if (rc != 0) {
			(void) fprintf(stderr,
			    gettext("%s: Failed to process boot "
			    "arguments from Boot Environment.\n"), cmdname);
			(void) fprintf(stderr,
			    gettext("%s: Falling back to regular reboot.\n"),
			    cmdname);
			return (-1);
		}
		/* No need to process further */
		fbarg_used = fbarg;
		fbarg_entnum = entnum;
		return (0);
	}
#endif	/* __x86 */

	/* Zero out the boot argument buffer as we will reconstruct it */
	bzero(bootargs_buf, buf_size);
	bzero(bootfs_arg, sizeof (bootfs_arg));
	bzero(unixfile, sizeof (unixfile));

	if (bename && (rc = fastboot_bename(bename, mountpoint,
	    sizeof (mountpoint))) != 0)
		return (EINVAL);


	/*
	 * If BE is not specified, look for disk argument to construct
	 * mountpoint; if BE has been specified, mountpoint has already been
	 * constructed.
	 */
	if (newarg && newarg[0] != '-' && !bename) {
		int tmprc;

		if ((tmprc = validate_disk(newarg, mountpoint)) == 0) {
			/*
			 * The first argument is a valid root argument.
			 * Get the next argument.
			 */
			newarg = strtok(NULL, " ");
			rootlen = (newarg) ? (newarg - head) : buflen;
			(void) strlcpy(fastboot_mounted, mountpoint,
			    sizeof (fastboot_mounted));

		} else if (tmprc == -1) {
			/*
			 * Not a disk argument.  Use / as default root.
			 */
			bcopy("/", mountpoint, 1);
			bzero(&mountpoint[1], sizeof (mountpoint) - 1);
		} else {
			/*
			 * Disk argument, but not valid or not root.
			 * Return failure.
			 */
			return (EINVAL);
		}
	}

	/*
	 * Make mountpoint the first part of unixfile.
	 * If there is not disk argument, and BE has not been specified,
	 * mountpoint could be empty.
	 */
	mplen = strlen(mountpoint);
	bcopy(mountpoint, unixfile, mplen);

	/*
	 * Look for unix argument
	 */
	if (newarg && newarg[0] != '-') {
		bcopy(newarg, &unixfile[mplen], strlen(newarg));
		newarg = strtok(NULL, " ");
		rootlen = (newarg) ? (newarg - head) : buflen;
	} else if (mplen != 0) {
		/*
		 * No unix argument, but mountpoint is not empty, use
		 * /kernel/unix as default (flat kernel layout).
		 */
		(void) snprintf(&unixfile[mplen],
		    sizeof (unixfile) - mplen, "/kernel/unix");
	}

	/*
	 * We now have the complete unix argument.  Verify that it exists and
	 * is an ELF file.  Split the argument up into mountpoint and unix
	 * portions again.  This is necessary to handle cases where mountpoint
	 * is specified on the command line as part of the unix argument,
	 * such as this:
	 *	# reboot -f /.alt/platform/i86pc/kernel/amd64/unix
	 */
	unixlen = strlen(unixfile);
	if (unixlen > 0) {
		if (validate_unix(unixfile, &mplen, &is_zfs,
		    bootfs_arg) != 0) {
			/* Not a valid unix file */
			return (EINVAL);
		} else {
			int space = 0;
			/*
			 * Construct boot argument.
			 */
			unixlen = strlen(unixfile);

			/*
			 * mdep cannot start with space because bootadm
			 * creates bogus menu entries if it does.
			 */
			if (mplen > 0) {
				bcopy(unixfile, bootargs_buf, mplen);
				(void) strcat(bootargs_buf, " ");
				space = 1;
			}
			bcopy(&unixfile[mplen], &bootargs_buf[mplen + space],
			    unixlen - mplen);
			(void) strcat(bootargs_buf, " ");
			off += unixlen + space + 1;
		}
	} else {
		/* Check to see if root is zfs */
		const char	*dp;
		(void) get_zfs_bootfs_arg("/", &dp, &is_zfs, bootfs_arg);
	}

	if (is_zfs && (buflen != 0 || bename != NULL))	{
		/* do not copy existing zfs boot args */
		if (strstr(&bootargs_saved[rootlen], "-B") == NULL ||
		    strstr(&bootargs_saved[rootlen], "zfs-bootfs=") == NULL ||
		    (strstr(&bootargs_saved[rootlen], "bootpath=") == NULL &&
		    strstr(&bootargs_saved[rootlen], "diskdevid=") == NULL))
			/* LINTED E_SEC_SPRINTF_UNBOUNDED_COPY */
			off += sprintf(bootargs_buf + off, "%s ", bootfs_arg);
	}

	/*
	 * Copy the rest of the arguments
	 */
	bcopy(&bootargs_saved[rootlen], &bootargs_buf[off], buflen - rootlen);

	return (rc);
}

#define	MAXARGS		5

static void
do_archives_update(int do_fast_reboot)
{
	int	r, i = 0;
	pid_t	pid;
	char	*cmd_argv[MAXARGS];


	cmd_argv[i++] = "/sbin/bootadm";
	cmd_argv[i++] = "-ea";
	cmd_argv[i++] = "update_all";
	if (do_fast_reboot)
		cmd_argv[i++] = "fastboot";
	cmd_argv[i] = NULL;

	r = posix_spawn(&pid, cmd_argv[0], NULL, NULL, cmd_argv, NULL);

	/* if posix_spawn fails we emit a warning and continue */

	if (r != 0)
		(void) fprintf(stderr, gettext("%s: WARNING, unable to start "
		    "boot archive update\n"), cmdname);
	else
		while (waitpid(pid, NULL, 0) == -1 && errno == EINTR)
			;
}

int
main(int argc, char *argv[])
{
	int qflag = 0, needlog = 1, nosync = 0;
	int fast_reboot = 0;
	int prom_reboot = 0;
	uintptr_t mdep = 0;
	int cmd, fcn, c, aval, r;
	const char *usage;
	const char *optstring;
	zoneid_t zoneid = getzoneid();
	int need_check_zones = 0;
	char bootargs_buf[BOOTARGS_MAX];
	char *bootargs_orig = NULL;
	char *bename = NULL;

	const char * const resetting = "/etc/svc/volatile/resetting";

	(void) setlocale(LC_ALL, "");
	(void) textdomain(TEXT_DOMAIN);

	cmdname = basename(argv[0]);

	if (strcmp(cmdname, "halt") == 0) {
		(void) audit_halt_setup(argc, argv);
		optstring = "dlnqy";
		usage = gettext("usage: %s [ -dlnqy ]\n");
		cmd = A_SHUTDOWN;
		fcn = AD_HALT;
	} else if (strcmp(cmdname, "poweroff") == 0) {
		(void) audit_halt_setup(argc, argv);
		optstring = "dlnqy";
		usage = gettext("usage: %s [ -dlnqy ]\n");
		cmd = A_SHUTDOWN;
		fcn = AD_POWEROFF;
	} else if (strcmp(cmdname, "reboot") == 0) {
		(void) audit_reboot_setup();
#if defined(__x86)
		optstring = "dlnqpfe:";
		usage = gettext("usage: %s [ -dlnq(p|fe:) ] [ boot args ]\n");
#else
		optstring = "dlnqfp";
		usage = gettext("usage: %s [ -dlnq(p|f) ] [ boot args ]\n");
#endif
		cmd = A_SHUTDOWN;
		fcn = AD_BOOT;
	} else {
		(void) fprintf(stderr,
		    gettext("%s: not installed properly\n"), cmdname);
		return (1);
	}

	while ((c = getopt(argc, argv, optstring)) != EOF) {
		switch (c) {
		case 'd':
			if (zoneid == GLOBAL_ZONEID)
				cmd = A_DUMP;
			else {
				(void) fprintf(stderr,
				    gettext("%s: -d only valid from global"
				    " zone\n"), cmdname);
				return (1);
			}
			break;
		case 'l':
			needlog = 0;
			break;
		case 'n':
			nosync = 1;
			break;
		case 'q':
			qflag = 1;
			break;
		case 'y':
			/*
			 * Option ignored for backwards compatibility.
			 */
			break;
		case 'f':
			fast_reboot = 1;
			break;
		case 'p':
			prom_reboot = 1;
			break;
#if defined(__x86)
		case 'e':
			bename = optarg;
			break;
#endif
		default:
			/*
			 * TRANSLATION_NOTE
			 * Don't translate the words "halt" or "reboot"
			 */
			(void) fprintf(stderr, usage, cmdname);
			return (1);
		}
	}

	argc -= optind;
	argv += optind;

	if (argc != 0) {
		if (fcn != AD_BOOT) {
			(void) fprintf(stderr, usage, cmdname);
			return (1);
		}

		/* Gather the arguments into bootargs_buf. */
		if (gather_args(argv, bootargs_buf, sizeof (bootargs_buf)) !=
		    0) {
			(void) fprintf(stderr,
			    gettext("%s: Boot arguments too long.\n"), cmdname);
			return (1);
		}

		bootargs_orig = strdup(bootargs_buf);
		mdep = (uintptr_t)bootargs_buf;
	} else {
		/*
		 * Initialize it to 0 in case of fastboot, the buffer
		 * will be used.
		 */
		bzero(bootargs_buf, sizeof (bootargs_buf));
	}

	if (geteuid() != 0) {
		(void) fprintf(stderr,
		    gettext("%s: permission denied\n"), cmdname);
		goto fail;
	}

	if (fast_reboot && prom_reboot) {
		(void) fprintf(stderr,
		    gettext("%s: -p and -f are mutually exclusive\n"),
		    cmdname);
		return (EINVAL);
	}
	/*
	 * Check whether fast reboot is the default operating mode
	 */
	if (fcn == AD_BOOT && !fast_reboot && !prom_reboot &&
	    zoneid == GLOBAL_ZONEID) {
		fast_reboot = scf_is_fastboot_default();

	}

	if (bename && !fast_reboot)	{
		(void) fprintf(stderr, gettext("%s: -e only valid with -f\n"),
		    cmdname);
		return (EINVAL);
	}

#if defined(__sparc)
	if (fast_reboot) {
		fast_reboot = 2;	/* need to distinguish each case */
	}
#endif

	/*
	 * If fast reboot, do some sanity check on the argument
	 */
	if (fast_reboot == 1) {
		int rc;
		int is_dryrun = 0;

		if (zoneid != GLOBAL_ZONEID)	{
			(void) fprintf(stderr,
			    gettext("%s: Fast reboot only valid from global"
			    " zone\n"), cmdname);
			return (EINVAL);
		}

		rc = parse_fastboot_args(bootargs_buf, sizeof (bootargs_buf),
		    &is_dryrun, bename);

		/*
		 * If dry run, or if arguments are invalid, return.
		 */
		if (is_dryrun)
			return (rc);
		else if (rc == EINVAL)
			goto fail;
		else if (rc != 0)
			fast_reboot = 0;

		/*
		 * For all the other errors, we continue on in case user
		 * user want to force fast reboot, or fall back to regular
		 * reboot.
		 */
		if (strlen(bootargs_buf) != 0)
			mdep = (uintptr_t)bootargs_buf;
	}

#if 0	/* For debugging */
	if (mdep != NULL)
		(void) fprintf(stderr, "mdep = %s\n", (char *)mdep);
#endif

	if (needlog) {
		char *user = getlogin();
		struct passwd *pw;
		char *tty;

		openlog(cmdname, 0, LOG_AUTH);
		if (user == NULL && (pw = getpwuid(getuid())) != NULL)
			user = pw->pw_name;
		if (user == NULL)
			user = "root";

		tty = ttyname(1);

		if (tty == NULL)
			syslog(LOG_CRIT, "initiated by %s", user);
		else
			syslog(LOG_CRIT, "initiated by %s on %s", user, tty);
	}

	/*
	 * We must assume success and log it before auditd is terminated.
	 */
	if (fcn == AD_BOOT)
		aval = audit_reboot_success();
	else
		aval = audit_halt_success();

	if (aval == -1) {
		(void) fprintf(stderr,
		    gettext("%s: can't turn off auditd\n"), cmdname);
		if (needlog)
			(void) sleep(5); /* Give syslogd time to record this */
	}

	(void) signal(SIGHUP, SIG_IGN);	/* for remote connections */

	/*
	 * We start to fork a bunch of zoneadms to halt any active zones.
	 * This will proceed with halt in parallel until we call
	 * check_zone_haltedness later on.
	 */
	if (zoneid == GLOBAL_ZONEID && cmd != A_DUMP) {
		need_check_zones = halt_zones();
	}

#if defined(__x86)
	/* set new default entry in the GRUB entry */
	if (fbarg_entnum != BE_ENTRY_DEFAULT) {
		char buf[32];
		(void) snprintf(buf, sizeof (buf), "default=%u", fbarg_entnum);
		(void) halt_exec(BOOTADM_PROG, "set-menu", buf, NULL);
	}
#endif	/* __x86 */

	/* if we're dumping, do the archive update here and don't defer it */
	if (cmd == A_DUMP && zoneid == GLOBAL_ZONEID && !nosync)
		do_archives_update(fast_reboot);

	/*
	 * If we're not forcing a crash dump, mark the system as quiescing for
	 * smf(7)'s benefit, and idle the init process.
	 */
	if (cmd != A_DUMP) {
		if (direct_init(PCDSTOP) == -1) {
			/*
			 * TRANSLATION_NOTE
			 * Don't translate the word "init"
			 */
			(void) fprintf(stderr,
			    gettext("%s: can't idle init\n"), cmdname);
			goto fail;
		}

		if (creat(resetting, 0755) == -1)
			(void) fprintf(stderr,
			    gettext("%s: could not create %s.\n"),
			    cmdname, resetting);
	}

	/*
	 * Make sure we don't get stopped by a jobcontrol shell
	 * once we start killing everybody.
	 */
	(void) signal(SIGTSTP, SIG_IGN);
	(void) signal(SIGTTIN, SIG_IGN);
	(void) signal(SIGTTOU, SIG_IGN);
	(void) signal(SIGPIPE, SIG_IGN);
	(void) signal(SIGTERM, SIG_IGN);

	/*
	 * Try to stop gdm so X has a chance to return the screen and
	 * keyboard to a sane state.
	 */
	if (fast_reboot == 1 && stop_gdm() != 0) {
		(void) fprintf(stderr,
		    gettext("%s: Falling back to regular reboot.\n"), cmdname);
		fast_reboot = 0;
		mdep = (uintptr_t)bootargs_orig;
	} else if (bootargs_orig) {
		free(bootargs_orig);
	}

	if (cmd != A_DUMP) {
		/*
		 * Stop all restarters so they do not try to restart services
		 * that are terminated.
		 */
		stop_restarters();

		/*
		 * Wait a little while for zones to shutdown.
		 */
		if (need_check_zones) {
			check_zones_haltedness();

			(void) fprintf(stderr,
			    gettext("%s: Completing system halt.\n"),
			    cmdname);
		}
	}

	/*
	 * If we're not forcing a crash dump, give everyone 5 seconds to
	 * handle a SIGTERM and clean up properly.
	 */
	if (cmd != A_DUMP) {
		if (zoneid == GLOBAL_ZONEID && !nosync)
			do_archives_update(fast_reboot);
		(void) kill(-1, SIGTERM);
		(void) sleep(5);
	}

	(void) signal(SIGINT, SIG_IGN);

	if (!qflag && !nosync) {
		struct utmpx wtmpx;

		bzero(&wtmpx, sizeof (struct utmpx));
		(void) strcpy(wtmpx.ut_line, "~");
		(void) time(&wtmpx.ut_tv.tv_sec);

		if (cmd == A_DUMP)
			(void) strcpy(wtmpx.ut_name, "crash dump");
		else
			(void) strcpy(wtmpx.ut_name, "shutdown");

		(void) updwtmpx(WTMPX_FILE, &wtmpx);
		sync();
	}

	if (cmd == A_DUMP && nosync != 0)
		(void) uadmin(A_DUMP, AD_NOSYNC, 0);

	if (fast_reboot)
		fcn = AD_FASTREBOOT;

	if (uadmin(cmd, fcn, mdep) == -1)
		(void) fprintf(stderr, "%s: uadmin failed: %s\n",
		    cmdname, strerror(errno));
	else
		(void) fprintf(stderr, "%s: uadmin unexpectedly returned 0\n",
		    cmdname);

	do {
		r = remove(resetting);
	} while (r != 0 && errno == EINTR);

	if (r != 0 && errno != ENOENT)
		(void) fprintf(stderr, gettext("%s: could not remove %s.\n"),
		    cmdname, resetting);

	if (direct_init(PCRUN) == -1) {
		/*
		 * TRANSLATION_NOTE
		 * Don't translate the word "init"
		 */
		(void) fprintf(stderr,
		    gettext("%s: can't resume init\n"), cmdname);
	}

	continue_restarters();

	if (get_initpid() != -1)
		/* tell init to restate current level */
		(void) kill(get_initpid(), SIGHUP);

fail:
	if (fcn == AD_BOOT)
		(void) audit_reboot_fail();
	else
		(void) audit_halt_fail();

	if (fast_reboot == 1) {
		if (bename) {
			(void) halt_exec(BEADM_PROG, "umount", bename, NULL);

		} else if (strlen(fastboot_mounted) != 0) {
			(void) umount(fastboot_mounted);
#if defined(__x86)
		} else {
			free(fbarg_used);
#endif	/* __x86 */
		}
	}

	return (1);
}