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
|
/*
* 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.
*/
/* 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.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Common code for halt(1M), poweroff(1M), and reboot(1M). We use
* argv[0] to determine which behavior to exhibit.
*/
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/uadmin.h>
#include <alloca.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <libscf.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 <utmpx.h>
#include <pwd.h>
#include <zone.h>
#if !defined(TEXT_DOMAIN)
#define TEXT_DOMAIN "SYS_TEST"
#endif
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);
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"
static void
stop_startd()
{
ctid_t ctid;
scf_handle_t *h;
scf_property_t *prop = NULL;
scf_value_t *val = NULL;
uint64_t uint64;
int ret;
h = scf_handle_create(SCF_VERSION);
if (h == NULL)
return;
ret = scf_handle_bind(h);
if (ret) {
scf_handle_destroy(h);
return;
}
prop = scf_property_create(h);
val = scf_value_create(h);
if (!(prop && val))
goto out;
ret = scf_handle_decode_fmri(h, FMRI_STARTD_CONTRACT,
NULL, NULL, NULL, NULL, prop, SCF_DECODE_FMRI_EXACT);
if (ret)
goto out;
ret = scf_property_is_type(prop, SCF_TYPE_COUNT);
if (ret)
goto out;
ret = scf_property_get_value(prop, val);
if (ret)
goto out;
ret = scf_value_get_count(val, &uint64);
if (ret)
goto out;
ctid = (ctid_t)uint64;
startdct = ctid;
(void) sigsend(P_CTID, ctid, SIGSTOP);
out:
if (prop)
scf_property_destroy(prop);
if (val)
scf_value_destroy(val);
(void) scf_handle_unbind(h);
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;
int ret;
length = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH);
if (length <= 0)
return;
length++;
fmri = alloca(length * sizeof (char));
h = scf_handle_create(SCF_VERSION);
if (!h)
return;
ret = scf_handle_bind(h);
if (ret) {
scf_handle_destroy(h);
return;
}
sc = scf_scope_create(h);
svc = scf_service_create(h);
inst = scf_instance_create(h);
snap = scf_snapshot_create(h);
pg = scf_pg_create(h);
prop = scf_property_create(h);
val = scf_value_create(h);
siter = scf_iter_create(h);
iiter = scf_iter_create(h);
if (!(sc && svc && inst && snap &&
pg && prop && val && siter && iiter))
goto out;
ret = scf_handle_get_scope(h, SCF_SCOPE_LOCAL, sc);
if (ret)
goto out;
ret = scf_iter_scope_services(siter, sc);
if (ret)
goto out;
while (scf_iter_next_service(siter, svc) == 1) {
ret = scf_iter_service_instances(iiter, svc);
if (ret)
continue;
while (scf_iter_next_instance(iiter, inst) == 1) {
ret = scf_instance_get_snapshot(inst, "running", snap);
if (ret)
isnap = NULL;
else
isnap = snap;
ret = scf_instance_get_pg_composed(inst, isnap,
SCF_PG_GENERAL, pg);
if (ret)
continue;
ret = scf_pg_get_property(pg, "restarter", prop);
if (ret)
continue;
ret = scf_property_is_type(prop, SCF_TYPE_ASTRING);
if (ret)
continue;
ret = scf_property_get_value(prop, val);
if (ret)
continue;
bytes = scf_value_get_astring(val, fmri, length);
if (bytes <= 0 || bytes >= length)
continue;
if (strlcat(fmri, FMRI_CONTRACT_PROP, length) >=
length)
continue;
ret = scf_handle_decode_fmri(h, fmri, NULL, NULL,
NULL, NULL, prop, SCF_DECODE_FMRI_EXACT);
if (ret)
continue;
ret = scf_property_is_type(prop, SCF_TYPE_COUNT);
if (ret)
continue;
ret = scf_property_get_value(prop, val);
if (ret)
continue;
ret = scf_value_get_count(val, &uint64);
if (ret)
continue;
ctid = (ctid_t)uint64;
if (save_ctid(ctid) == 0) {
(void) sigsend(P_CTID, ctid, SIGSTOP);
}
}
}
out:
if (sc)
scf_scope_destroy(sc);
if (svc)
scf_service_destroy(svc);
if (inst)
scf_instance_destroy(inst);
if (snap)
scf_snapshot_destroy(snap);
if (pg)
scf_pg_destroy(pg);
if (prop)
scf_property_destroy(prop);
if (val)
scf_value_destroy(val);
if (siter)
scf_iter_destroy(siter);
if (iiter)
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);
}
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);
}
int
main(int argc, char *argv[])
{
char *cmdname = basename(argv[0]);
char *ttyn = ttyname(STDERR_FILENO);
int qflag = 0, needlog = 1, nosync = 0;
uintptr_t mdep = NULL;
int cmd, fcn, c, aval, r;
const char *usage;
zoneid_t zoneid = getzoneid();
pid_t init_pid = 1;
char bootargs_buf[257]; /* uadmin()'s buffer is 257 bytes. */
const char * const resetting = "/etc/svc/volatile/resetting";
(void) setlocale(LC_ALL, "");
(void) textdomain(TEXT_DOMAIN);
if (strcmp(cmdname, "halt") == 0) {
(void) audit_halt_setup(argc, argv);
usage = gettext("usage: %s [ -dlnqy ]\n");
cmd = A_SHUTDOWN;
fcn = AD_HALT;
} else if (strcmp(cmdname, "poweroff") == 0) {
(void) audit_halt_setup(argc, argv);
usage = gettext("usage: %s [ -dlnqy ]\n");
cmd = A_SHUTDOWN;
fcn = AD_POWEROFF;
} else if (strcmp(cmdname, "reboot") == 0) {
(void) audit_reboot_setup();
usage = gettext("usage: %s [ -dlnq ] [ boot args ]\n");
cmd = A_SHUTDOWN;
fcn = AD_BOOT;
} else {
(void) fprintf(stderr,
gettext("%s: not installed properly\n"), cmdname);
return (1);
}
while ((c = getopt(argc, argv, "dlnqy")) != 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':
ttyn = NULL;
break;
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);
}
mdep = (uintptr_t)bootargs_buf;
}
if (geteuid() != 0) {
(void) fprintf(stderr,
gettext("%s: permission denied\n"), cmdname);
goto fail;
}
if (fcn != AD_BOOT && ttyn != NULL &&
strncmp(ttyn, "/dev/term/", strlen("/dev/term/")) == 0) {
/*
* TRANSLATION_NOTE
* Don't translate ``halt -y''
*/
(void) fprintf(stderr,
gettext("%s: dangerous on a dialup;"), cmdname);
(void) fprintf(stderr,
gettext("use ``%s -y'' if you are really sure\n"), cmdname);
goto fail;
}
if (needlog) {
char *user = getlogin();
struct passwd *pw;
openlog(cmdname, 0, LOG_AUTH);
if (user == NULL && (pw = getpwuid(getuid())) != NULL)
user = pw->pw_name;
if (user == NULL)
user = "root";
syslog(LOG_CRIT, "%sed by %s", cmdname, user);
}
/*
* 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 */
if (zone_getattr(getzoneid(), ZONE_ATTR_INITPID, &init_pid,
sizeof (init_pid)) != sizeof (init_pid)) {
assert(errno == ESRCH);
init_pid = -1;
}
/* sync boot archive in the global zone */
if (getzoneid() == GLOBAL_ZONEID && !nosync) {
(void) system("/sbin/bootadm -a update_all");
}
/*
* If we're not forcing a crash dump, mark the system as quiescing for
* smf(5)'s benefit, and idle the init process.
*/
if (cmd != A_DUMP) {
if (init_pid != -1 && kill(init_pid, SIGTSTP) == -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);
/*
* Stop all restarters so they do not try to restart services
* that are terminated.
*/
stop_restarters();
}
/*
* 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(SIGTERM, SIG_IGN);
/*
* If we're not forcing a crash dump, give everyone 5 seconds to
* handle a SIGTERM and clean up properly.
*/
if (cmd != A_DUMP) {
(void) kill(-1, SIGTERM);
(void) sleep(5);
}
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, NULL);
(void) uadmin(cmd, fcn, mdep);
perror(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);
continue_restarters();
if (init_pid != -1)
/* tell init to restate current level */
(void) kill(init_pid, SIGHUP);
fail:
if (fcn == AD_BOOT)
(void) audit_reboot_fail();
else
(void) audit_halt_fail();
return (1);
}
|