Print this page
3913 there is no dialup, only zuul
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/halt/halt.c
+++ new/usr/src/cmd/halt/halt.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
↓ open down ↓ |
14 lines elided |
↑ open up ↑ |
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 +/*
26 + * Copyright (c) 2013, Joyent, Inc. All rights reserved.
27 + */
25 28
26 29 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
27 30 /* All Rights Reserved */
28 31
29 32 /*
30 33 * University Copyright- Copyright (c) 1982, 1986, 1988
31 34 * The Regents of the University of California
32 35 * All Rights Reserved
33 36 *
34 37 * University Acknowledgment- Portions of this document are derived from
35 38 * software developed by the University of California, Berkeley, and its
36 39 * contributors.
37 40 * Portions contributed by Juergen Keil, <jk@tools.de>.
38 41 */
39 42
40 43
41 44 /*
42 45 * Common code for halt(1M), poweroff(1M), and reboot(1M). We use
43 46 * argv[0] to determine which behavior to exhibit.
44 47 */
45 48
46 49 #include <stdio.h>
47 50 #include <procfs.h>
48 51 #include <sys/types.h>
49 52 #include <sys/elf.h>
50 53 #include <sys/systeminfo.h>
51 54 #include <sys/stat.h>
52 55 #include <sys/uadmin.h>
53 56 #include <sys/mntent.h>
54 57 #include <sys/mnttab.h>
55 58 #include <sys/mount.h>
56 59 #include <sys/fs/ufs_mount.h>
57 60 #include <alloca.h>
58 61 #include <assert.h>
59 62 #include <errno.h>
60 63 #include <fcntl.h>
61 64 #include <libgen.h>
62 65 #include <libscf.h>
63 66 #include <libscf_priv.h>
64 67 #include <limits.h>
65 68 #include <locale.h>
66 69 #include <libintl.h>
67 70 #include <syslog.h>
68 71 #include <signal.h>
69 72 #include <strings.h>
70 73 #include <unistd.h>
71 74 #include <stdlib.h>
72 75 #include <stdio.h>
73 76 #include <strings.h>
74 77 #include <time.h>
75 78 #include <wait.h>
76 79 #include <ctype.h>
77 80 #include <utmpx.h>
78 81 #include <pwd.h>
79 82 #include <zone.h>
80 83 #include <spawn.h>
81 84
82 85 #include <libzfs.h>
83 86 #if defined(__i386)
84 87 #include <libgrubmgmt.h>
85 88 #endif
86 89
87 90 #if !defined(TEXT_DOMAIN)
88 91 #define TEXT_DOMAIN "SYS_TEST"
89 92 #endif
90 93
91 94 #if defined(__sparc)
92 95 #define CUR_ELFDATA ELFDATA2MSB
93 96 #elif defined(__i386)
94 97 #define CUR_ELFDATA ELFDATA2LSB
95 98 #endif
96 99
97 100 static libzfs_handle_t *g_zfs;
98 101
99 102 extern int audit_halt_setup(int, char **);
100 103 extern int audit_halt_success(void);
101 104 extern int audit_halt_fail(void);
102 105
103 106 extern int audit_reboot_setup(void);
104 107 extern int audit_reboot_success(void);
105 108 extern int audit_reboot_fail(void);
106 109
107 110 static char *cmdname; /* basename(argv[0]), the name of the command */
108 111
109 112 typedef struct ctidlist_struct {
110 113 ctid_t ctid;
111 114 struct ctidlist_struct *next;
112 115 } ctidlist_t;
113 116
114 117 static ctidlist_t *ctidlist = NULL;
115 118 static ctid_t startdct = -1;
116 119
117 120 #define FMRI_STARTD_CONTRACT \
118 121 "svc:/system/svc/restarter:default/:properties/restarter/contract"
119 122
120 123 #define BEADM_PROG "/usr/sbin/beadm"
121 124 #define BOOTADM_PROG "/sbin/bootadm"
122 125 #define ZONEADM_PROG "/usr/sbin/zoneadm"
123 126
124 127 /*
125 128 * The length of FASTBOOT_MOUNTPOINT must be less than MAXPATHLEN.
126 129 */
127 130 #define FASTBOOT_MOUNTPOINT "/tmp/.fastboot.root"
128 131
129 132 /*
130 133 * Fast Reboot related variables
131 134 */
132 135 static char fastboot_mounted[MAXPATHLEN];
133 136
134 137 #if defined(__i386)
135 138 static grub_boot_args_t fbarg;
136 139 static grub_boot_args_t *fbarg_used;
137 140 static int fbarg_entnum = GRUB_ENTRY_DEFAULT;
138 141 #endif /* __i386 */
139 142
140 143 static int validate_ufs_disk(char *, char *);
141 144 static int validate_zfs_pool(char *, char *);
142 145
143 146 static pid_t
144 147 get_initpid()
145 148 {
146 149 static int init_pid = -1;
147 150
148 151 if (init_pid == -1) {
149 152 if (zone_getattr(getzoneid(), ZONE_ATTR_INITPID, &init_pid,
150 153 sizeof (init_pid)) != sizeof (init_pid)) {
151 154 assert(errno == ESRCH);
152 155 init_pid = -1;
153 156 }
154 157 }
155 158 return (init_pid);
156 159 }
157 160
158 161 /*
159 162 * Quiesce or resume init using /proc. When stopping init, we can't send
160 163 * SIGTSTP (since init ignores it) or SIGSTOP (since the kernel won't permit
161 164 * it).
162 165 */
163 166 static int
164 167 direct_init(long command)
165 168 {
166 169 char ctlfile[MAXPATHLEN];
167 170 pid_t pid;
168 171 int ctlfd;
169 172
170 173 assert(command == PCDSTOP || command == PCRUN);
171 174 if ((pid = get_initpid()) == -1) {
172 175 return (-1);
173 176 }
174 177
175 178 (void) snprintf(ctlfile, sizeof (ctlfile), "/proc/%d/ctl", pid);
176 179 if ((ctlfd = open(ctlfile, O_WRONLY)) == -1)
177 180 return (-1);
178 181
179 182 if (command == PCDSTOP) {
180 183 if (write(ctlfd, &command, sizeof (long)) == -1) {
181 184 (void) close(ctlfd);
182 185 return (-1);
183 186 }
184 187 } else { /* command == PCRUN */
185 188 long cmds[2];
186 189 cmds[0] = command;
187 190 cmds[1] = 0;
188 191 if (write(ctlfd, cmds, sizeof (cmds)) == -1) {
189 192 (void) close(ctlfd);
190 193 return (-1);
191 194 }
192 195 }
193 196 (void) close(ctlfd);
194 197 return (0);
195 198 }
196 199
197 200 static void
198 201 stop_startd()
199 202 {
200 203 scf_handle_t *h;
201 204 scf_property_t *prop = NULL;
202 205 scf_value_t *val = NULL;
203 206 uint64_t uint64;
204 207
205 208 if ((h = scf_handle_create(SCF_VERSION)) == NULL)
206 209 return;
207 210
208 211 if ((scf_handle_bind(h) != 0) ||
209 212 ((prop = scf_property_create(h)) == NULL) ||
210 213 ((val = scf_value_create(h)) == NULL))
211 214 goto out;
212 215
213 216 if (scf_handle_decode_fmri(h, FMRI_STARTD_CONTRACT,
214 217 NULL, NULL, NULL, NULL, prop, SCF_DECODE_FMRI_EXACT) != 0)
215 218 goto out;
216 219
217 220 if (scf_property_is_type(prop, SCF_TYPE_COUNT) != 0 ||
218 221 scf_property_get_value(prop, val) != 0 ||
219 222 scf_value_get_count(val, &uint64) != 0)
220 223 goto out;
221 224
222 225 startdct = (ctid_t)uint64;
223 226 (void) sigsend(P_CTID, startdct, SIGSTOP);
224 227
225 228 out:
226 229 scf_property_destroy(prop);
227 230 scf_value_destroy(val);
228 231 scf_handle_destroy(h);
229 232 }
230 233
231 234 static void
232 235 continue_startd()
233 236 {
234 237 if (startdct != -1)
235 238 (void) sigsend(P_CTID, startdct, SIGCONT);
236 239 }
237 240
238 241 #define FMRI_RESTARTER_PROP "/:properties/general/restarter"
239 242 #define FMRI_CONTRACT_PROP "/:properties/restarter/contract"
240 243
241 244 static int
242 245 save_ctid(ctid_t ctid)
243 246 {
244 247 ctidlist_t *next;
245 248
246 249 for (next = ctidlist; next != NULL; next = next->next)
247 250 if (next->ctid == ctid)
248 251 return (-1);
249 252
250 253 next = (ctidlist_t *)malloc(sizeof (ctidlist_t));
251 254 if (next == NULL)
252 255 return (-1);
253 256
254 257 next->ctid = ctid;
255 258 next->next = ctidlist;
256 259 ctidlist = next;
257 260 return (0);
258 261 }
259 262
260 263 static void
261 264 stop_delegates()
262 265 {
263 266 ctid_t ctid;
264 267 scf_handle_t *h;
265 268 scf_scope_t *sc = NULL;
266 269 scf_service_t *svc = NULL;
267 270 scf_instance_t *inst = NULL;
268 271 scf_snapshot_t *snap = NULL;
269 272 scf_snapshot_t *isnap = NULL;
270 273 scf_propertygroup_t *pg = NULL;
271 274 scf_property_t *prop = NULL;
272 275 scf_value_t *val = NULL;
273 276 scf_iter_t *siter = NULL;
274 277 scf_iter_t *iiter = NULL;
275 278 char *fmri;
276 279 ssize_t length;
277 280
278 281 uint64_t uint64;
279 282 ssize_t bytes;
280 283
281 284 length = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH);
282 285 if (length <= 0)
283 286 return;
284 287
285 288 length++;
286 289 fmri = alloca(length * sizeof (char));
287 290
288 291 if ((h = scf_handle_create(SCF_VERSION)) == NULL)
289 292 return;
290 293
291 294 if (scf_handle_bind(h) != 0) {
292 295 scf_handle_destroy(h);
293 296 return;
294 297 }
295 298
296 299 if ((sc = scf_scope_create(h)) == NULL ||
297 300 (svc = scf_service_create(h)) == NULL ||
298 301 (inst = scf_instance_create(h)) == NULL ||
299 302 (snap = scf_snapshot_create(h)) == NULL ||
300 303 (pg = scf_pg_create(h)) == NULL ||
301 304 (prop = scf_property_create(h)) == NULL ||
302 305 (val = scf_value_create(h)) == NULL ||
303 306 (siter = scf_iter_create(h)) == NULL ||
304 307 (iiter = scf_iter_create(h)) == NULL)
305 308 goto out;
306 309
307 310 if (scf_handle_get_scope(h, SCF_SCOPE_LOCAL, sc) != 0)
308 311 goto out;
309 312
310 313 if (scf_iter_scope_services(siter, sc) != 0)
311 314 goto out;
312 315
313 316 while (scf_iter_next_service(siter, svc) == 1) {
314 317
315 318 if (scf_iter_service_instances(iiter, svc) != 0)
316 319 continue;
317 320
318 321 while (scf_iter_next_instance(iiter, inst) == 1) {
319 322
320 323 if ((scf_instance_get_snapshot(inst, "running",
321 324 snap)) != 0)
322 325 isnap = NULL;
323 326 else
324 327 isnap = snap;
325 328
326 329 if (scf_instance_get_pg_composed(inst, isnap,
327 330 SCF_PG_GENERAL, pg) != 0)
328 331 continue;
329 332
330 333 if (scf_pg_get_property(pg, SCF_PROPERTY_RESTARTER,
331 334 prop) != 0 ||
332 335 scf_property_get_value(prop, val) != 0)
333 336 continue;
334 337
335 338 bytes = scf_value_get_astring(val, fmri, length);
336 339 if (bytes <= 0 || bytes >= length)
337 340 continue;
338 341
339 342 if (strlcat(fmri, FMRI_CONTRACT_PROP, length) >=
340 343 length)
341 344 continue;
342 345
343 346 if (scf_handle_decode_fmri(h, fmri, NULL, NULL,
344 347 NULL, NULL, prop, SCF_DECODE_FMRI_EXACT) != 0)
345 348 continue;
346 349
347 350 if (scf_property_is_type(prop, SCF_TYPE_COUNT) != 0 ||
348 351 scf_property_get_value(prop, val) != 0 ||
349 352 scf_value_get_count(val, &uint64) != 0)
350 353 continue;
351 354
352 355 ctid = (ctid_t)uint64;
353 356 if (save_ctid(ctid) == 0) {
354 357 (void) sigsend(P_CTID, ctid, SIGSTOP);
355 358 }
356 359 }
357 360 }
358 361 out:
359 362 scf_scope_destroy(sc);
360 363 scf_service_destroy(svc);
361 364 scf_instance_destroy(inst);
362 365 scf_snapshot_destroy(snap);
363 366 scf_pg_destroy(pg);
364 367 scf_property_destroy(prop);
365 368 scf_value_destroy(val);
366 369 scf_iter_destroy(siter);
367 370 scf_iter_destroy(iiter);
368 371
369 372 (void) scf_handle_unbind(h);
370 373 scf_handle_destroy(h);
371 374 }
372 375
373 376 static void
374 377 continue_delegates()
375 378 {
376 379 ctidlist_t *next;
377 380 for (next = ctidlist; next != NULL; next = next->next)
378 381 (void) sigsend(P_CTID, next->ctid, SIGCONT);
379 382 }
380 383
381 384 #define FMRI_GDM "svc:/application/graphical-login/gdm:default"
382 385 #define GDM_STOP_TIMEOUT 10 /* Give gdm 10 seconds to shut down */
383 386
384 387 /*
385 388 * If gdm is running, try to stop gdm.
386 389 * Returns 0 on success, -1 on failure.
387 390 */
388 391 static int
389 392 stop_gdm()
390 393 {
391 394 char *gdm_state = NULL;
392 395 int retry = 0;
393 396
394 397 /*
395 398 * If gdm is running, try to stop gdm.
396 399 */
397 400 while ((gdm_state = smf_get_state(FMRI_GDM)) != NULL &&
398 401 strcmp(gdm_state, SCF_STATE_STRING_ONLINE) == 0 &&
399 402 retry++ < GDM_STOP_TIMEOUT) {
400 403
401 404 free(gdm_state);
402 405
403 406 /*
404 407 * Only need to disable once.
405 408 */
406 409 if (retry == 1 &&
407 410 smf_disable_instance(FMRI_GDM, SMF_TEMPORARY) != 0) {
408 411 (void) fprintf(stderr,
409 412 gettext("%s: Failed to stop %s: %s.\n"),
410 413 cmdname, FMRI_GDM, scf_strerror(scf_error()));
411 414 return (-1);
412 415 }
413 416 (void) sleep(1);
414 417 }
415 418
416 419 if (retry >= GDM_STOP_TIMEOUT) {
417 420 (void) fprintf(stderr, gettext("%s: Failed to stop %s.\n"),
418 421 cmdname, FMRI_GDM);
419 422 return (-1);
420 423 }
421 424
422 425 return (0);
423 426 }
424 427
425 428
426 429 static void
427 430 stop_restarters()
428 431 {
429 432 stop_startd();
430 433 stop_delegates();
431 434 }
432 435
433 436 static void
434 437 continue_restarters()
435 438 {
436 439 continue_startd();
437 440 continue_delegates();
438 441 }
439 442
440 443 /*
441 444 * Copy an array of strings into buf, separated by spaces. Returns 0 on
442 445 * success.
443 446 */
444 447 static int
445 448 gather_args(char **args, char *buf, size_t buf_sz)
446 449 {
447 450 if (strlcpy(buf, *args, buf_sz) >= buf_sz)
448 451 return (-1);
449 452
450 453 for (++args; *args != NULL; ++args) {
451 454 if (strlcat(buf, " ", buf_sz) >= buf_sz)
452 455 return (-1);
453 456 if (strlcat(buf, *args, buf_sz) >= buf_sz)
454 457 return (-1);
455 458 }
456 459
457 460 return (0);
458 461 }
459 462
460 463 /*
461 464 * Halt every zone on the system. We are committed to doing a shutdown
462 465 * even if something goes wrong here. If something goes wrong, we just
463 466 * continue with the shutdown. Return non-zero if we need to wait for zones to
464 467 * halt later on.
465 468 */
466 469 static int
467 470 halt_zones()
468 471 {
469 472 pid_t pid;
470 473 zoneid_t *zones;
471 474 size_t nz = 0, old_nz;
472 475 int i;
473 476 char zname[ZONENAME_MAX];
474 477
475 478 /*
476 479 * Get a list of zones. If the number of zones changes in between the
477 480 * two zone_list calls, try again.
478 481 */
479 482
480 483 for (;;) {
481 484 (void) zone_list(NULL, &nz);
482 485 if (nz == 1)
483 486 return (0);
484 487 old_nz = nz;
485 488 zones = calloc(sizeof (zoneid_t), nz);
486 489 if (zones == NULL) {
487 490 (void) fprintf(stderr,
488 491 gettext("%s: Could not halt zones"
489 492 " (out of memory).\n"), cmdname);
490 493 return (0);
491 494 }
492 495
493 496 (void) zone_list(zones, &nz);
494 497 if (old_nz == nz)
495 498 break;
496 499 free(zones);
497 500 }
498 501
499 502 if (nz == 2) {
500 503 (void) fprintf(stderr, gettext("%s: Halting 1 zone.\n"),
501 504 cmdname);
502 505 } else {
503 506 (void) fprintf(stderr, gettext("%s: Halting %i zones.\n"),
504 507 cmdname, nz - 1);
505 508 }
506 509
507 510 for (i = 0; i < nz; i++) {
508 511 if (zones[i] == GLOBAL_ZONEID)
509 512 continue;
510 513 if (getzonenamebyid(zones[i], zname, sizeof (zname)) < 0) {
511 514 /*
512 515 * getzonenamebyid should only fail if we raced with
513 516 * another process trying to shut down the zone.
514 517 * We assume this happened and ignore the error.
515 518 */
516 519 if (errno != EINVAL) {
517 520 (void) fprintf(stderr,
518 521 gettext("%s: Unexpected error while "
519 522 "looking up zone %ul: %s.\n"),
520 523 cmdname, zones[i], strerror(errno));
521 524 }
522 525
523 526 continue;
524 527 }
525 528 pid = fork();
526 529 if (pid < 0) {
527 530 (void) fprintf(stderr,
528 531 gettext("%s: Zone \"%s\" could not be"
529 532 " halted (could not fork(): %s).\n"),
530 533 cmdname, zname, strerror(errno));
531 534 continue;
532 535 }
533 536 if (pid == 0) {
534 537 (void) execl(ZONEADM_PROG, ZONEADM_PROG,
535 538 "-z", zname, "halt", NULL);
536 539 (void) fprintf(stderr,
537 540 gettext("%s: Zone \"%s\" could not be halted"
538 541 " (cannot exec(" ZONEADM_PROG "): %s).\n"),
539 542 cmdname, zname, strerror(errno));
540 543 exit(0);
541 544 }
542 545 }
543 546
544 547 return (1);
545 548 }
546 549
547 550 /*
548 551 * This function tries to wait for all non-global zones to go away.
549 552 * It will timeout if no progress is made for 5 seconds, or a total of
550 553 * 30 seconds elapses.
551 554 */
552 555
553 556 static void
554 557 check_zones_haltedness()
555 558 {
556 559 int t = 0, t_prog = 0;
557 560 size_t nz = 0, last_nz;
558 561
559 562 do {
560 563 last_nz = nz;
561 564 (void) zone_list(NULL, &nz);
562 565 if (nz == 1)
563 566 return;
564 567
565 568 (void) sleep(1);
566 569
567 570 if (last_nz > nz)
568 571 t_prog = 0;
569 572
570 573 t++;
571 574 t_prog++;
572 575
573 576 if (t == 10) {
574 577 if (nz == 2) {
575 578 (void) fprintf(stderr,
576 579 gettext("%s: Still waiting for 1 zone to "
577 580 "halt. Will wait up to 20 seconds.\n"),
578 581 cmdname);
579 582 } else {
580 583 (void) fprintf(stderr,
581 584 gettext("%s: Still waiting for %i zones "
582 585 "to halt. Will wait up to 20 seconds.\n"),
583 586 cmdname, nz - 1);
584 587 }
585 588 }
586 589
587 590 } while ((t < 30) && (t_prog < 5));
588 591 }
589 592
590 593
591 594 /*
592 595 * Validate that this is a root disk or dataset
593 596 * Returns 0 if it is a root disk or dataset;
594 597 * returns 1 if it is a disk argument or dataset, but not valid or not root;
595 598 * returns -1 if it is not a valid argument or a disk argument.
596 599 */
597 600 static int
598 601 validate_disk(char *arg, char *mountpoint)
599 602 {
600 603 static char root_dev_path[] = "/dev/dsk";
601 604 char kernpath[MAXPATHLEN];
602 605 struct stat64 statbuf;
603 606 int rc = 0;
604 607
605 608 if (strlen(arg) > MAXPATHLEN) {
606 609 (void) fprintf(stderr,
607 610 gettext("%s: Argument is too long\n"), cmdname);
608 611 return (-1);
609 612 }
610 613
611 614 bcopy(FASTBOOT_MOUNTPOINT, mountpoint, sizeof (FASTBOOT_MOUNTPOINT));
612 615
613 616 if (strstr(arg, mountpoint) == NULL) {
614 617 /*
615 618 * Do a force umount just in case some other filesystem has
616 619 * been mounted there.
617 620 */
618 621 (void) umount2(mountpoint, MS_FORCE);
619 622 }
620 623
621 624 /* Create the directory if it doesn't already exist */
622 625 if (lstat64(mountpoint, &statbuf) != 0) {
623 626 if (mkdirp(mountpoint, 0755) != 0) {
624 627 (void) fprintf(stderr,
625 628 gettext("Failed to create mountpoint %s\n"),
626 629 mountpoint);
627 630 return (-1);
628 631 }
629 632 }
630 633
631 634 if (strncmp(arg, root_dev_path, strlen(root_dev_path)) == 0) {
632 635 /* ufs root disk argument */
633 636 rc = validate_ufs_disk(arg, mountpoint);
634 637 } else {
635 638 /* zfs root pool argument */
636 639 rc = validate_zfs_pool(arg, mountpoint);
637 640 }
638 641
639 642 if (rc != 0)
640 643 return (rc);
641 644
642 645 (void) snprintf(kernpath, MAXPATHLEN, "%s/platform/i86pc/kernel/unix",
643 646 mountpoint);
644 647
645 648 if (stat64(kernpath, &statbuf) != 0) {
646 649 (void) fprintf(stderr,
647 650 gettext("%s: %s is not a root disk or dataset\n"),
648 651 cmdname, arg);
649 652 return (1);
650 653 }
651 654
652 655 return (0);
653 656 }
654 657
655 658
656 659 static int
657 660 validate_ufs_disk(char *arg, char *mountpoint)
658 661 {
659 662 struct ufs_args ufs_args = { 0 };
660 663 char mntopts[MNT_LINE_MAX] = MNTOPT_LARGEFILES;
661 664
662 665 /* perform the mount */
663 666 ufs_args.flags = UFSMNT_LARGEFILES;
664 667 if (mount(arg, mountpoint, MS_DATA|MS_OPTIONSTR,
665 668 MNTTYPE_UFS, &ufs_args, sizeof (ufs_args),
666 669 mntopts, sizeof (mntopts)) != 0) {
667 670 perror(cmdname);
668 671 (void) fprintf(stderr,
669 672 gettext("%s: Failed to mount %s\n"), cmdname, arg);
670 673 return (-1);
671 674 }
672 675
673 676 return (0);
674 677 }
675 678
676 679 static int
677 680 validate_zfs_pool(char *arg, char *mountpoint)
678 681 {
679 682 zfs_handle_t *zhp = NULL;
680 683 char mntopts[MNT_LINE_MAX] = { '\0' };
681 684 int rc = 0;
682 685
683 686 if ((g_zfs = libzfs_init()) == NULL) {
684 687 (void) fprintf(stderr, gettext("Internal error: failed to "
685 688 "initialize ZFS library\n"));
686 689 return (-1);
687 690 }
688 691
689 692 /* Try to open the dataset */
690 693 if ((zhp = zfs_open(g_zfs, arg,
691 694 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_DATASET)) == NULL)
692 695 return (-1);
693 696
694 697 /* perform the mount */
695 698 if (mount(zfs_get_name(zhp), mountpoint, MS_DATA|MS_OPTIONSTR|MS_RDONLY,
696 699 MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) {
697 700 perror(cmdname);
698 701 (void) fprintf(stderr,
699 702 gettext("%s: Failed to mount %s\n"), cmdname, arg);
700 703 rc = -1;
701 704 }
702 705
703 706 validate_zfs_err_out:
704 707 if (zhp != NULL)
705 708 zfs_close(zhp);
706 709
707 710 libzfs_fini(g_zfs);
708 711 return (rc);
709 712 }
710 713
711 714 /*
712 715 * Return 0 if not zfs, or is zfs and have successfully constructed the
713 716 * boot argument; returns non-zero otherwise.
714 717 * At successful completion fpth contains pointer where mount point ends.
715 718 * NOTE: arg is supposed to be the resolved path
716 719 */
717 720 static int
718 721 get_zfs_bootfs_arg(const char *arg, const char ** fpth, int *is_zfs,
719 722 char *bootfs_arg)
720 723 {
721 724 zfs_handle_t *zhp = NULL;
722 725 zpool_handle_t *zpoolp = NULL;
723 726 FILE *mtabp = NULL;
724 727 struct mnttab mnt;
725 728 char *poolname = NULL;
726 729 char physpath[MAXPATHLEN];
727 730 char mntsp[ZPOOL_MAXNAMELEN];
728 731 char bootfs[ZPOOL_MAXNAMELEN];
729 732 int rc = 0;
730 733 size_t mntlen = 0;
731 734 size_t msz;
732 735 static char fmt[] = "-B zfs-bootfs=%s,bootpath=\"%s\"";
733 736
734 737 *fpth = arg;
735 738 *is_zfs = 0;
736 739
737 740 bzero(physpath, sizeof (physpath));
738 741 bzero(bootfs, sizeof (bootfs));
739 742
740 743 if ((mtabp = fopen(MNTTAB, "r")) == NULL) {
741 744 return (-1);
742 745 }
743 746
744 747 while (getmntent(mtabp, &mnt) == 0) {
745 748 if (strstr(arg, mnt.mnt_mountp) == arg &&
746 749 (msz = strlen(mnt.mnt_mountp)) > mntlen) {
747 750 mntlen = msz;
748 751 *is_zfs = strcmp(MNTTYPE_ZFS, mnt.mnt_fstype) == 0;
749 752 (void) strlcpy(mntsp, mnt.mnt_special, sizeof (mntsp));
750 753 }
751 754 }
752 755
753 756 (void) fclose(mtabp);
754 757
755 758 if (mntlen > 1)
756 759 *fpth += mntlen;
757 760
758 761 if (!*is_zfs)
759 762 return (0);
760 763
761 764 if ((g_zfs = libzfs_init()) == NULL)
762 765 return (-1);
763 766
764 767 /* Try to open the dataset */
765 768 if ((zhp = zfs_open(g_zfs, mntsp,
766 769 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_DATASET)) == NULL) {
767 770 (void) fprintf(stderr, gettext("Cannot open %s\n"), mntsp);
768 771 rc = -1;
769 772 goto validate_zfs_err_out;
770 773 }
771 774
772 775 (void) strlcpy(bootfs, mntsp, sizeof (bootfs));
773 776
774 777 if ((poolname = strtok(mntsp, "/")) == NULL) {
775 778 rc = -1;
776 779 goto validate_zfs_err_out;
777 780 }
778 781
779 782 if ((zpoolp = zpool_open(g_zfs, poolname)) == NULL) {
780 783 (void) fprintf(stderr, gettext("Cannot open %s\n"), poolname);
781 784 rc = -1;
782 785 goto validate_zfs_err_out;
783 786 }
784 787
785 788 if (zpool_get_physpath(zpoolp, physpath, sizeof (physpath)) != 0) {
786 789 (void) fprintf(stderr, gettext("Cannot find phys_path\n"));
787 790 rc = -1;
788 791 goto validate_zfs_err_out;
789 792 }
790 793
791 794 /*
792 795 * For the mirror physpath would contain the list of all
793 796 * bootable devices, pick up the first one.
794 797 */
795 798 (void) strtok(physpath, " ");
796 799 if (snprintf(bootfs_arg, BOOTARGS_MAX, fmt, bootfs, physpath) >=
797 800 BOOTARGS_MAX) {
798 801 rc = E2BIG;
799 802 (void) fprintf(stderr,
800 803 gettext("Boot arguments are too long\n"));
801 804 }
802 805
803 806 validate_zfs_err_out:
804 807 if (zhp != NULL)
805 808 zfs_close(zhp);
806 809
807 810 if (zpoolp != NULL)
808 811 zpool_close(zpoolp);
809 812
810 813 libzfs_fini(g_zfs);
811 814 return (rc);
812 815 }
813 816
814 817 /*
815 818 * Validate that the file exists, and is an ELF file.
816 819 * Returns 0 on success, -1 on failure.
817 820 */
818 821 static int
819 822 validate_unix(char *arg, int *mplen, int *is_zfs, char *bootfs_arg)
820 823 {
821 824 const char *location;
822 825 int class, format;
823 826 unsigned char ident[EI_NIDENT];
824 827 char physpath[MAXPATHLEN];
825 828 int elffd = -1;
826 829 size_t sz;
827 830
828 831 if ((sz = resolvepath(arg, physpath, sizeof (physpath) - 1)) ==
829 832 (size_t)-1) {
830 833 (void) fprintf(stderr,
831 834 gettext("Cannot resolve path for %s: %s\n"),
832 835 arg, strerror(errno));
833 836 return (-1);
834 837 }
835 838 (void) strlcpy(arg, physpath, sz + 1);
836 839
837 840 if (strlen(arg) > MAXPATHLEN) {
838 841 (void) fprintf(stderr,
839 842 gettext("%s: New kernel name is too long\n"), cmdname);
840 843 return (-1);
841 844 }
842 845
843 846 if (strncmp(basename(arg), "unix", 4) != 0) {
844 847 (void) fprintf(stderr,
845 848 gettext("%s: %s: Kernel name must be unix\n"),
846 849 cmdname, arg);
847 850 return (-1);
848 851 }
849 852
850 853 if (get_zfs_bootfs_arg(arg, &location, is_zfs, bootfs_arg) != 0)
851 854 goto err_out;
852 855
853 856 *mplen = location - arg;
854 857
855 858 if (strstr(location, "/boot/platform") == location) {
856 859 /*
857 860 * Rebooting to failsafe.
858 861 * Clear bootfs_arg and is_zfs flag.
859 862 */
860 863 bootfs_arg[0] = 0;
861 864 *is_zfs = 0;
862 865 } else if (strstr(location, "/platform") != location) {
863 866 (void) fprintf(stderr,
864 867 gettext("%s: %s: No /platform in file name\n"),
865 868 cmdname, arg);
866 869 goto err_out;
867 870 }
868 871
869 872 if ((elffd = open64(arg, O_RDONLY)) < 0 ||
870 873 (pread64(elffd, ident, EI_NIDENT, 0) != EI_NIDENT)) {
871 874 (void) fprintf(stderr, "%s: %s: %s\n",
872 875 cmdname, arg, strerror(errno));
873 876 goto err_out;
874 877 }
875 878
876 879 class = ident[EI_CLASS];
877 880
878 881 if ((class != ELFCLASS32 && class != ELFCLASS64) ||
879 882 memcmp(&ident[EI_MAG0], ELFMAG, 4) != 0) {
880 883 (void) fprintf(stderr,
881 884 gettext("%s: %s: Not a valid ELF file\n"), cmdname, arg);
882 885 goto err_out;
883 886 }
884 887
885 888 format = ident[EI_DATA];
886 889
887 890 if (format != CUR_ELFDATA) {
888 891 (void) fprintf(stderr, gettext("%s: %s: Invalid data format\n"),
889 892 cmdname, arg);
890 893 goto err_out;
891 894 }
892 895
893 896 return (0);
894 897
895 898 err_out:
896 899 if (elffd >= 0) {
897 900 (void) close(elffd);
898 901 elffd = -1;
899 902 }
900 903 return (-1);
901 904 }
902 905
903 906 static int
904 907 halt_exec(const char *path, ...)
905 908 {
906 909 pid_t pid;
907 910 int i;
908 911 int st;
909 912 const char *arg;
910 913 va_list vp;
911 914 const char *argv[256];
912 915
913 916 if ((pid = fork()) == -1) {
914 917 return (errno);
915 918 } else if (pid == 0) {
916 919 (void) fclose(stdout);
917 920 (void) fclose(stderr);
918 921
919 922 argv[0] = path;
920 923 i = 1;
921 924
922 925 va_start(vp, path);
923 926
924 927 do {
925 928 arg = va_arg(vp, const char *);
926 929 argv[i] = arg;
927 930 } while (arg != NULL &&
928 931 ++i != sizeof (argv) / sizeof (argv[0]));
929 932
930 933 va_end(vp);
931 934
932 935 (void) execve(path, (char * const *)argv, NULL);
933 936 (void) fprintf(stderr, gettext("Cannot execute %s: %s\n"),
934 937 path, strerror(errno));
935 938 exit(-1);
936 939 } else {
937 940 if (waitpid(pid, &st, 0) == pid &&
938 941 !WIFSIGNALED(st) && WIFEXITED(st))
939 942 st = WEXITSTATUS(st);
940 943 else
941 944 st = -1;
942 945 }
943 946 return (st);
944 947 }
945 948
946 949 /*
947 950 * Mount the specified BE.
948 951 *
949 952 * Upon success returns zero and copies bename string to mountpoint[]
950 953 */
951 954 static int
952 955 fastboot_bename(const char *bename, char *mountpoint, size_t mpsz)
953 956 {
954 957 int rc;
955 958
956 959 /*
957 960 * Attempt to unmount the BE first in case it's already mounted
958 961 * elsewhere.
959 962 */
960 963 (void) halt_exec(BEADM_PROG, "umount", bename, NULL);
961 964
962 965 if ((rc = halt_exec(BEADM_PROG, "mount", bename, FASTBOOT_MOUNTPOINT,
963 966 NULL)) != 0)
964 967 (void) fprintf(stderr,
965 968 gettext("%s: Unable to mount BE \"%s\" at %s\n"),
966 969 cmdname, bename, FASTBOOT_MOUNTPOINT);
967 970 else
968 971 (void) strlcpy(mountpoint, FASTBOOT_MOUNTPOINT, mpsz);
969 972
970 973 return (rc);
971 974 }
972 975
973 976 /*
974 977 * Returns 0 on successful parsing of the arguments;
975 978 * returns EINVAL on parsing failures that should abort the reboot attempt;
976 979 * returns other error code to fall back to regular reboot.
977 980 */
978 981 static int
979 982 parse_fastboot_args(char *bootargs_buf, size_t buf_size,
980 983 int *is_dryrun, const char *bename)
981 984 {
982 985 char mountpoint[MAXPATHLEN];
983 986 char bootargs_saved[BOOTARGS_MAX];
984 987 char bootargs_scratch[BOOTARGS_MAX];
985 988 char bootfs_arg[BOOTARGS_MAX];
986 989 char unixfile[BOOTARGS_MAX];
987 990 char *head, *newarg;
988 991 int buflen; /* length of the bootargs_buf */
989 992 int mplen; /* length of the mount point */
990 993 int rootlen = 0; /* length of the root argument */
991 994 int unixlen = 0; /* length of the unix argument */
992 995 int off = 0; /* offset into the new boot argument */
993 996 int is_zfs = 0;
994 997 int rc = 0;
995 998
996 999 bzero(mountpoint, sizeof (mountpoint));
997 1000
998 1001 /*
999 1002 * If argc is not 0, buflen is length of the argument being passed in;
1000 1003 * else it is 0 as bootargs_buf has been initialized to all 0's.
1001 1004 */
1002 1005 buflen = strlen(bootargs_buf);
1003 1006
1004 1007 /* Save a copy of the original argument */
1005 1008 bcopy(bootargs_buf, bootargs_saved, buflen);
1006 1009 bzero(&bootargs_saved[buflen], sizeof (bootargs_saved) - buflen);
1007 1010
1008 1011 /* Save another copy to be used by strtok */
1009 1012 bcopy(bootargs_buf, bootargs_scratch, buflen);
1010 1013 bzero(&bootargs_scratch[buflen], sizeof (bootargs_scratch) - buflen);
1011 1014 head = &bootargs_scratch[0];
1012 1015
1013 1016 /* Get the first argument */
1014 1017 newarg = strtok(bootargs_scratch, " ");
1015 1018
1016 1019 /*
1017 1020 * If this is a dry run request, verify that the drivers can handle
1018 1021 * fast reboot.
1019 1022 */
1020 1023 if (newarg && strncasecmp(newarg, "dryrun", strlen("dryrun")) == 0) {
1021 1024 *is_dryrun = 1;
1022 1025 (void) system("/usr/sbin/devfsadm");
1023 1026 }
1024 1027
1025 1028 /*
1026 1029 * Always perform a dry run to identify all the drivers that
1027 1030 * need to implement devo_reset().
1028 1031 */
1029 1032 if (uadmin(A_SHUTDOWN, AD_FASTREBOOT_DRYRUN,
1030 1033 (uintptr_t)bootargs_saved) != 0) {
1031 1034 (void) fprintf(stderr, gettext("%s: Not all drivers "
1032 1035 "have implemented quiesce(9E)\n"
1033 1036 "\tPlease see /var/adm/messages for drivers that haven't\n"
1034 1037 "\timplemented quiesce(9E).\n"), cmdname);
1035 1038 } else if (*is_dryrun) {
1036 1039 (void) fprintf(stderr, gettext("%s: All drivers have "
1037 1040 "implemented quiesce(9E)\n"), cmdname);
1038 1041 }
1039 1042
1040 1043 /* Return if it is a true dry run. */
1041 1044 if (*is_dryrun)
1042 1045 return (rc);
1043 1046
1044 1047 #if defined(__i386)
1045 1048 /* Read boot args from GRUB menu */
1046 1049 if ((bootargs_buf[0] == 0 || isdigit(bootargs_buf[0])) &&
1047 1050 bename == NULL) {
1048 1051 /*
1049 1052 * If no boot arguments are given, or a GRUB menu entry
1050 1053 * number is provided, process the GRUB menu.
1051 1054 */
1052 1055 int entnum;
1053 1056 if (bootargs_buf[0] == 0)
1054 1057 entnum = GRUB_ENTRY_DEFAULT;
1055 1058 else {
1056 1059 errno = 0;
1057 1060 entnum = strtoul(bootargs_buf, NULL, 10);
1058 1061 rc = errno;
1059 1062 }
1060 1063
1061 1064 if (rc == 0 && (rc = grub_get_boot_args(&fbarg, NULL,
1062 1065 entnum)) == 0) {
1063 1066 if (strlcpy(bootargs_buf, fbarg.gba_bootargs,
1064 1067 buf_size) >= buf_size) {
1065 1068 grub_cleanup_boot_args(&fbarg);
1066 1069 bcopy(bootargs_saved, bootargs_buf, buf_size);
1067 1070 rc = E2BIG;
1068 1071 }
1069 1072 }
1070 1073 /* Failed to read GRUB menu, fall back to normal reboot */
1071 1074 if (rc != 0) {
1072 1075 (void) fprintf(stderr,
1073 1076 gettext("%s: Failed to process GRUB menu "
1074 1077 "entry for fast reboot.\n\t%s\n"),
1075 1078 cmdname, grub_strerror(rc));
1076 1079 (void) fprintf(stderr,
1077 1080 gettext("%s: Falling back to regular reboot.\n"),
1078 1081 cmdname);
1079 1082 return (-1);
1080 1083 }
1081 1084 /* No need to process further */
1082 1085 fbarg_used = &fbarg;
1083 1086 fbarg_entnum = entnum;
1084 1087 return (0);
1085 1088 }
1086 1089 #endif /* __i386 */
1087 1090
1088 1091 /* Zero out the boot argument buffer as we will reconstruct it */
1089 1092 bzero(bootargs_buf, buf_size);
1090 1093 bzero(bootfs_arg, sizeof (bootfs_arg));
1091 1094 bzero(unixfile, sizeof (unixfile));
1092 1095
1093 1096 if (bename && (rc = fastboot_bename(bename, mountpoint,
1094 1097 sizeof (mountpoint))) != 0)
1095 1098 return (EINVAL);
1096 1099
1097 1100
1098 1101 /*
1099 1102 * If BE is not specified, look for disk argument to construct
1100 1103 * mountpoint; if BE has been specified, mountpoint has already been
1101 1104 * constructed.
1102 1105 */
1103 1106 if (newarg && newarg[0] != '-' && !bename) {
1104 1107 int tmprc;
1105 1108
1106 1109 if ((tmprc = validate_disk(newarg, mountpoint)) == 0) {
1107 1110 /*
1108 1111 * The first argument is a valid root argument.
1109 1112 * Get the next argument.
1110 1113 */
1111 1114 newarg = strtok(NULL, " ");
1112 1115 rootlen = (newarg) ? (newarg - head) : buflen;
1113 1116 (void) strlcpy(fastboot_mounted, mountpoint,
1114 1117 sizeof (fastboot_mounted));
1115 1118
1116 1119 } else if (tmprc == -1) {
1117 1120 /*
1118 1121 * Not a disk argument. Use / as default root.
1119 1122 */
1120 1123 bcopy("/", mountpoint, 1);
1121 1124 bzero(&mountpoint[1], sizeof (mountpoint) - 1);
1122 1125 } else {
1123 1126 /*
1124 1127 * Disk argument, but not valid or not root.
1125 1128 * Return failure.
1126 1129 */
1127 1130 return (EINVAL);
1128 1131 }
1129 1132 }
1130 1133
1131 1134 /*
1132 1135 * Make mountpoint the first part of unixfile.
1133 1136 * If there is not disk argument, and BE has not been specified,
1134 1137 * mountpoint could be empty.
1135 1138 */
1136 1139 mplen = strlen(mountpoint);
1137 1140 bcopy(mountpoint, unixfile, mplen);
1138 1141
1139 1142 /*
1140 1143 * Look for unix argument
1141 1144 */
1142 1145 if (newarg && newarg[0] != '-') {
1143 1146 bcopy(newarg, &unixfile[mplen], strlen(newarg));
1144 1147 newarg = strtok(NULL, " ");
1145 1148 rootlen = (newarg) ? (newarg - head) : buflen;
1146 1149 } else if (mplen != 0) {
1147 1150 /*
1148 1151 * No unix argument, but mountpoint is not empty, use
1149 1152 * /platform/i86pc/$ISADIR/kernel/unix as default.
1150 1153 */
1151 1154 char isa[20];
1152 1155
1153 1156 if (sysinfo(SI_ARCHITECTURE_64, isa, sizeof (isa)) != -1)
1154 1157 (void) snprintf(&unixfile[mplen],
1155 1158 sizeof (unixfile) - mplen,
1156 1159 "/platform/i86pc/kernel/%s/unix", isa);
1157 1160 else if (sysinfo(SI_ARCHITECTURE_32, isa, sizeof (isa)) != -1) {
1158 1161 (void) snprintf(&unixfile[mplen],
1159 1162 sizeof (unixfile) - mplen,
1160 1163 "/platform/i86pc/kernel/unix");
1161 1164 } else {
1162 1165 (void) fprintf(stderr,
1163 1166 gettext("%s: Unknown architecture"), cmdname);
1164 1167 return (EINVAL);
1165 1168 }
1166 1169 }
1167 1170
1168 1171 /*
1169 1172 * We now have the complete unix argument. Verify that it exists and
1170 1173 * is an ELF file. Split the argument up into mountpoint and unix
1171 1174 * portions again. This is necessary to handle cases where mountpoint
1172 1175 * is specified on the command line as part of the unix argument,
1173 1176 * such as this:
1174 1177 * # reboot -f /.alt/platform/i86pc/kernel/amd64/unix
1175 1178 */
1176 1179 unixlen = strlen(unixfile);
1177 1180 if (unixlen > 0) {
1178 1181 if (validate_unix(unixfile, &mplen, &is_zfs,
1179 1182 bootfs_arg) != 0) {
1180 1183 /* Not a valid unix file */
1181 1184 return (EINVAL);
1182 1185 } else {
1183 1186 int space = 0;
1184 1187 /*
1185 1188 * Construct boot argument.
1186 1189 */
1187 1190 unixlen = strlen(unixfile);
1188 1191
1189 1192 /*
1190 1193 * mdep cannot start with space because bootadm
1191 1194 * creates bogus menu entries if it does.
1192 1195 */
1193 1196 if (mplen > 0) {
1194 1197 bcopy(unixfile, bootargs_buf, mplen);
1195 1198 (void) strcat(bootargs_buf, " ");
1196 1199 space = 1;
1197 1200 }
1198 1201 bcopy(&unixfile[mplen], &bootargs_buf[mplen + space],
1199 1202 unixlen - mplen);
1200 1203 (void) strcat(bootargs_buf, " ");
1201 1204 off += unixlen + space + 1;
1202 1205 }
1203 1206 } else {
1204 1207 /* Check to see if root is zfs */
1205 1208 const char *dp;
1206 1209 (void) get_zfs_bootfs_arg("/", &dp, &is_zfs, bootfs_arg);
1207 1210 }
1208 1211
1209 1212 if (is_zfs && (buflen != 0 || bename != NULL)) {
1210 1213 /* LINTED E_SEC_SPRINTF_UNBOUNDED_COPY */
1211 1214 off += sprintf(bootargs_buf + off, "%s ", bootfs_arg);
1212 1215 }
1213 1216
1214 1217 /*
1215 1218 * Copy the rest of the arguments
1216 1219 */
1217 1220 bcopy(&bootargs_saved[rootlen], &bootargs_buf[off], buflen - rootlen);
1218 1221
1219 1222 return (rc);
1220 1223 }
1221 1224
1222 1225 #define MAXARGS 5
1223 1226
1224 1227 static void
1225 1228 do_archives_update(int do_fast_reboot)
1226 1229 {
1227 1230 int r, i = 0;
1228 1231 pid_t pid;
1229 1232 char *cmd_argv[MAXARGS];
1230 1233
1231 1234
1232 1235 cmd_argv[i++] = "/sbin/bootadm";
1233 1236 cmd_argv[i++] = "-ea";
1234 1237 cmd_argv[i++] = "update_all";
1235 1238 if (do_fast_reboot)
1236 1239 cmd_argv[i++] = "fastboot";
1237 1240 cmd_argv[i] = NULL;
1238 1241
1239 1242 r = posix_spawn(&pid, cmd_argv[0], NULL, NULL, cmd_argv, NULL);
1240 1243
1241 1244 /* if posix_spawn fails we emit a warning and continue */
1242 1245
1243 1246 if (r != 0)
↓ open down ↓ |
1209 lines elided |
↑ open up ↑ |
1244 1247 (void) fprintf(stderr, gettext("%s: WARNING, unable to start "
1245 1248 "boot archive update\n"), cmdname);
1246 1249 else
1247 1250 while (waitpid(pid, NULL, 0) == -1 && errno == EINTR)
1248 1251 ;
1249 1252 }
1250 1253
1251 1254 int
1252 1255 main(int argc, char *argv[])
1253 1256 {
1254 - char *ttyn = ttyname(STDERR_FILENO);
1255 -
1256 1257 int qflag = 0, needlog = 1, nosync = 0;
1257 1258 int fast_reboot = 0;
1258 1259 int prom_reboot = 0;
1259 1260 uintptr_t mdep = NULL;
1260 1261 int cmd, fcn, c, aval, r;
1261 1262 const char *usage;
1262 1263 const char *optstring;
1263 1264 zoneid_t zoneid = getzoneid();
1264 1265 int need_check_zones = 0;
1265 1266 char bootargs_buf[BOOTARGS_MAX];
1266 1267 char *bootargs_orig = NULL;
1267 1268 char *bename = NULL;
1268 1269
1269 1270 const char * const resetting = "/etc/svc/volatile/resetting";
1270 1271
1271 1272 (void) setlocale(LC_ALL, "");
1272 1273 (void) textdomain(TEXT_DOMAIN);
1273 1274
1274 1275 cmdname = basename(argv[0]);
1275 1276
1276 1277 if (strcmp(cmdname, "halt") == 0) {
1277 1278 (void) audit_halt_setup(argc, argv);
1278 1279 optstring = "dlnqy";
1279 1280 usage = gettext("usage: %s [ -dlnqy ]\n");
1280 1281 cmd = A_SHUTDOWN;
1281 1282 fcn = AD_HALT;
1282 1283 } else if (strcmp(cmdname, "poweroff") == 0) {
1283 1284 (void) audit_halt_setup(argc, argv);
1284 1285 optstring = "dlnqy";
1285 1286 usage = gettext("usage: %s [ -dlnqy ]\n");
1286 1287 cmd = A_SHUTDOWN;
1287 1288 fcn = AD_POWEROFF;
1288 1289 } else if (strcmp(cmdname, "reboot") == 0) {
1289 1290 (void) audit_reboot_setup();
1290 1291 #if defined(__i386)
1291 1292 optstring = "dlnqpfe:";
1292 1293 usage = gettext("usage: %s [ -dlnq(p|fe:) ] [ boot args ]\n");
1293 1294 #else
1294 1295 optstring = "dlnqfp";
1295 1296 usage = gettext("usage: %s [ -dlnq(p|f) ] [ boot args ]\n");
1296 1297 #endif
1297 1298 cmd = A_SHUTDOWN;
1298 1299 fcn = AD_BOOT;
1299 1300 } else {
1300 1301 (void) fprintf(stderr,
1301 1302 gettext("%s: not installed properly\n"), cmdname);
1302 1303 return (1);
1303 1304 }
1304 1305
1305 1306 while ((c = getopt(argc, argv, optstring)) != EOF) {
1306 1307 switch (c) {
1307 1308 case 'd':
1308 1309 if (zoneid == GLOBAL_ZONEID)
1309 1310 cmd = A_DUMP;
1310 1311 else {
1311 1312 (void) fprintf(stderr,
1312 1313 gettext("%s: -d only valid from global"
1313 1314 " zone\n"), cmdname);
1314 1315 return (1);
1315 1316 }
1316 1317 break;
↓ open down ↓ |
51 lines elided |
↑ open up ↑ |
1317 1318 case 'l':
1318 1319 needlog = 0;
1319 1320 break;
1320 1321 case 'n':
1321 1322 nosync = 1;
1322 1323 break;
1323 1324 case 'q':
1324 1325 qflag = 1;
1325 1326 break;
1326 1327 case 'y':
1327 - ttyn = NULL;
1328 + /*
1329 + * Option ignored for backwards compatibility.
1330 + */
1328 1331 break;
1329 1332 case 'f':
1330 1333 fast_reboot = 1;
1331 1334 break;
1332 1335 case 'p':
1333 1336 prom_reboot = 1;
1334 1337 break;
1335 1338 #if defined(__i386)
1336 1339 case 'e':
1337 1340 bename = optarg;
1338 1341 break;
1339 1342 #endif
1340 1343 default:
1341 1344 /*
1342 1345 * TRANSLATION_NOTE
1343 1346 * Don't translate the words "halt" or "reboot"
1344 1347 */
1345 1348 (void) fprintf(stderr, usage, cmdname);
1346 1349 return (1);
1347 1350 }
1348 1351 }
1349 1352
1350 1353 argc -= optind;
1351 1354 argv += optind;
1352 1355
1353 1356 if (argc != 0) {
1354 1357 if (fcn != AD_BOOT) {
1355 1358 (void) fprintf(stderr, usage, cmdname);
1356 1359 return (1);
1357 1360 }
1358 1361
1359 1362 /* Gather the arguments into bootargs_buf. */
1360 1363 if (gather_args(argv, bootargs_buf, sizeof (bootargs_buf)) !=
1361 1364 0) {
1362 1365 (void) fprintf(stderr,
1363 1366 gettext("%s: Boot arguments too long.\n"), cmdname);
1364 1367 return (1);
1365 1368 }
1366 1369
1367 1370 bootargs_orig = strdup(bootargs_buf);
1368 1371 mdep = (uintptr_t)bootargs_buf;
1369 1372 } else {
1370 1373 /*
1371 1374 * Initialize it to 0 in case of fastboot, the buffer
1372 1375 * will be used.
1373 1376 */
1374 1377 bzero(bootargs_buf, sizeof (bootargs_buf));
1375 1378 }
1376 1379
1377 1380 if (geteuid() != 0) {
1378 1381 (void) fprintf(stderr,
1379 1382 gettext("%s: permission denied\n"), cmdname);
1380 1383 goto fail;
1381 1384 }
1382 1385
1383 1386 if (fast_reboot && prom_reboot) {
1384 1387 (void) fprintf(stderr,
1385 1388 gettext("%s: -p and -f are mutually exclusive\n"),
1386 1389 cmdname);
1387 1390 return (EINVAL);
1388 1391 }
1389 1392 /*
1390 1393 * Check whether fast reboot is the default operating mode
1391 1394 */
1392 1395 if (fcn == AD_BOOT && !fast_reboot && !prom_reboot &&
1393 1396 zoneid == GLOBAL_ZONEID) {
1394 1397 fast_reboot = scf_is_fastboot_default();
1395 1398
1396 1399 }
1397 1400
1398 1401 if (bename && !fast_reboot) {
1399 1402 (void) fprintf(stderr, gettext("%s: -e only valid with -f\n"),
1400 1403 cmdname);
1401 1404 return (EINVAL);
1402 1405 }
1403 1406
1404 1407 #if defined(__sparc)
1405 1408 if (fast_reboot) {
1406 1409 fast_reboot = 2; /* need to distinguish each case */
1407 1410 }
1408 1411 #endif
1409 1412
1410 1413 /*
1411 1414 * If fast reboot, do some sanity check on the argument
1412 1415 */
1413 1416 if (fast_reboot == 1) {
1414 1417 int rc;
1415 1418 int is_dryrun = 0;
1416 1419
1417 1420 if (zoneid != GLOBAL_ZONEID) {
1418 1421 (void) fprintf(stderr,
1419 1422 gettext("%s: Fast reboot only valid from global"
1420 1423 " zone\n"), cmdname);
1421 1424 return (EINVAL);
1422 1425 }
1423 1426
1424 1427 rc = parse_fastboot_args(bootargs_buf, sizeof (bootargs_buf),
1425 1428 &is_dryrun, bename);
1426 1429
1427 1430 /*
1428 1431 * If dry run, or if arguments are invalid, return.
1429 1432 */
1430 1433 if (is_dryrun)
1431 1434 return (rc);
1432 1435 else if (rc == EINVAL)
1433 1436 goto fail;
1434 1437 else if (rc != 0)
1435 1438 fast_reboot = 0;
1436 1439
1437 1440 /*
1438 1441 * For all the other errors, we continue on in case user
1439 1442 * user want to force fast reboot, or fall back to regular
1440 1443 * reboot.
↓ open down ↓ |
103 lines elided |
↑ open up ↑ |
1441 1444 */
1442 1445 if (strlen(bootargs_buf) != 0)
1443 1446 mdep = (uintptr_t)bootargs_buf;
1444 1447 }
1445 1448
1446 1449 #if 0 /* For debugging */
1447 1450 if (mdep != NULL)
1448 1451 (void) fprintf(stderr, "mdep = %s\n", (char *)mdep);
1449 1452 #endif
1450 1453
1451 - if (fcn != AD_BOOT && ttyn != NULL &&
1452 - strncmp(ttyn, "/dev/term/", strlen("/dev/term/")) == 0) {
1453 - /*
1454 - * TRANSLATION_NOTE
1455 - * Don't translate ``halt -y''
1456 - */
1457 - (void) fprintf(stderr,
1458 - gettext("%s: dangerous on a dialup;"), cmdname);
1459 - (void) fprintf(stderr,
1460 - gettext("use ``%s -y'' if you are really sure\n"), cmdname);
1461 - goto fail;
1462 - }
1463 -
1464 1454 if (needlog) {
1465 1455 char *user = getlogin();
1466 1456 struct passwd *pw;
1467 1457 char *tty;
1468 1458
1469 1459 openlog(cmdname, 0, LOG_AUTH);
1470 1460 if (user == NULL && (pw = getpwuid(getuid())) != NULL)
1471 1461 user = pw->pw_name;
1472 1462 if (user == NULL)
1473 1463 user = "root";
1474 1464
1475 1465 tty = ttyname(1);
1476 1466
1477 1467 if (tty == NULL)
1478 1468 syslog(LOG_CRIT, "initiated by %s", user);
1479 1469 else
1480 1470 syslog(LOG_CRIT, "initiated by %s on %s", user, tty);
1481 1471 }
1482 1472
1483 1473 /*
1484 1474 * We must assume success and log it before auditd is terminated.
1485 1475 */
1486 1476 if (fcn == AD_BOOT)
1487 1477 aval = audit_reboot_success();
1488 1478 else
1489 1479 aval = audit_halt_success();
1490 1480
1491 1481 if (aval == -1) {
1492 1482 (void) fprintf(stderr,
1493 1483 gettext("%s: can't turn off auditd\n"), cmdname);
1494 1484 if (needlog)
1495 1485 (void) sleep(5); /* Give syslogd time to record this */
1496 1486 }
1497 1487
1498 1488 (void) signal(SIGHUP, SIG_IGN); /* for remote connections */
1499 1489
1500 1490 /*
1501 1491 * We start to fork a bunch of zoneadms to halt any active zones.
1502 1492 * This will proceed with halt in parallel until we call
1503 1493 * check_zone_haltedness later on.
1504 1494 */
1505 1495 if (zoneid == GLOBAL_ZONEID && cmd != A_DUMP) {
1506 1496 need_check_zones = halt_zones();
1507 1497 }
1508 1498
1509 1499 #if defined(__i386)
1510 1500 /* set new default entry in the GRUB entry */
1511 1501 if (fbarg_entnum != GRUB_ENTRY_DEFAULT) {
1512 1502 char buf[32];
1513 1503 (void) snprintf(buf, sizeof (buf), "default=%u", fbarg_entnum);
1514 1504 (void) halt_exec(BOOTADM_PROG, "set-menu", buf, NULL);
1515 1505 }
1516 1506 #endif /* __i386 */
1517 1507
1518 1508 /* if we're dumping, do the archive update here and don't defer it */
1519 1509 if (cmd == A_DUMP && zoneid == GLOBAL_ZONEID && !nosync)
1520 1510 do_archives_update(fast_reboot);
1521 1511
1522 1512 /*
1523 1513 * If we're not forcing a crash dump, mark the system as quiescing for
1524 1514 * smf(5)'s benefit, and idle the init process.
1525 1515 */
1526 1516 if (cmd != A_DUMP) {
1527 1517 if (direct_init(PCDSTOP) == -1) {
1528 1518 /*
1529 1519 * TRANSLATION_NOTE
1530 1520 * Don't translate the word "init"
1531 1521 */
1532 1522 (void) fprintf(stderr,
1533 1523 gettext("%s: can't idle init\n"), cmdname);
1534 1524 goto fail;
1535 1525 }
1536 1526
1537 1527 if (creat(resetting, 0755) == -1)
1538 1528 (void) fprintf(stderr,
1539 1529 gettext("%s: could not create %s.\n"),
1540 1530 cmdname, resetting);
1541 1531 }
1542 1532
1543 1533 /*
1544 1534 * Make sure we don't get stopped by a jobcontrol shell
1545 1535 * once we start killing everybody.
1546 1536 */
1547 1537 (void) signal(SIGTSTP, SIG_IGN);
1548 1538 (void) signal(SIGTTIN, SIG_IGN);
1549 1539 (void) signal(SIGTTOU, SIG_IGN);
1550 1540 (void) signal(SIGPIPE, SIG_IGN);
1551 1541 (void) signal(SIGTERM, SIG_IGN);
1552 1542
1553 1543 /*
1554 1544 * Try to stop gdm so X has a chance to return the screen and
1555 1545 * keyboard to a sane state.
1556 1546 */
1557 1547 if (fast_reboot == 1 && stop_gdm() != 0) {
1558 1548 (void) fprintf(stderr,
1559 1549 gettext("%s: Falling back to regular reboot.\n"), cmdname);
1560 1550 fast_reboot = 0;
1561 1551 mdep = (uintptr_t)bootargs_orig;
1562 1552 } else if (bootargs_orig) {
1563 1553 free(bootargs_orig);
1564 1554 }
1565 1555
1566 1556 if (cmd != A_DUMP) {
1567 1557 /*
1568 1558 * Stop all restarters so they do not try to restart services
1569 1559 * that are terminated.
1570 1560 */
1571 1561 stop_restarters();
1572 1562
1573 1563 /*
1574 1564 * Wait a little while for zones to shutdown.
1575 1565 */
1576 1566 if (need_check_zones) {
1577 1567 check_zones_haltedness();
1578 1568
1579 1569 (void) fprintf(stderr,
1580 1570 gettext("%s: Completing system halt.\n"),
1581 1571 cmdname);
1582 1572 }
1583 1573 }
1584 1574
1585 1575 /*
1586 1576 * If we're not forcing a crash dump, give everyone 5 seconds to
1587 1577 * handle a SIGTERM and clean up properly.
1588 1578 */
1589 1579 if (cmd != A_DUMP) {
1590 1580 int start, end, delta;
1591 1581
1592 1582 (void) kill(-1, SIGTERM);
1593 1583 start = time(NULL);
1594 1584
1595 1585 if (zoneid == GLOBAL_ZONEID && !nosync)
1596 1586 do_archives_update(fast_reboot);
1597 1587
1598 1588 end = time(NULL);
1599 1589 delta = end - start;
1600 1590 if (delta < 5)
1601 1591 (void) sleep(5 - delta);
1602 1592 }
1603 1593
1604 1594 (void) signal(SIGINT, SIG_IGN);
1605 1595
1606 1596 if (!qflag && !nosync) {
1607 1597 struct utmpx wtmpx;
1608 1598
1609 1599 bzero(&wtmpx, sizeof (struct utmpx));
1610 1600 (void) strcpy(wtmpx.ut_line, "~");
1611 1601 (void) time(&wtmpx.ut_tv.tv_sec);
1612 1602
1613 1603 if (cmd == A_DUMP)
1614 1604 (void) strcpy(wtmpx.ut_name, "crash dump");
1615 1605 else
1616 1606 (void) strcpy(wtmpx.ut_name, "shutdown");
1617 1607
1618 1608 (void) updwtmpx(WTMPX_FILE, &wtmpx);
1619 1609 sync();
1620 1610 }
1621 1611
1622 1612 if (cmd == A_DUMP && nosync != 0)
1623 1613 (void) uadmin(A_DUMP, AD_NOSYNC, NULL);
1624 1614
1625 1615 if (fast_reboot)
1626 1616 fcn = AD_FASTREBOOT;
1627 1617
1628 1618 if (uadmin(cmd, fcn, mdep) == -1)
1629 1619 (void) fprintf(stderr, "%s: uadmin failed: %s\n",
1630 1620 cmdname, strerror(errno));
1631 1621 else
1632 1622 (void) fprintf(stderr, "%s: uadmin unexpectedly returned 0\n",
1633 1623 cmdname);
1634 1624
1635 1625 do {
1636 1626 r = remove(resetting);
1637 1627 } while (r != 0 && errno == EINTR);
1638 1628
1639 1629 if (r != 0 && errno != ENOENT)
1640 1630 (void) fprintf(stderr, gettext("%s: could not remove %s.\n"),
1641 1631 cmdname, resetting);
1642 1632
1643 1633 if (direct_init(PCRUN) == -1) {
1644 1634 /*
1645 1635 * TRANSLATION_NOTE
1646 1636 * Don't translate the word "init"
1647 1637 */
1648 1638 (void) fprintf(stderr,
1649 1639 gettext("%s: can't resume init\n"), cmdname);
1650 1640 }
1651 1641
1652 1642 continue_restarters();
1653 1643
1654 1644 if (get_initpid() != -1)
1655 1645 /* tell init to restate current level */
1656 1646 (void) kill(get_initpid(), SIGHUP);
1657 1647
1658 1648 fail:
1659 1649 if (fcn == AD_BOOT)
1660 1650 (void) audit_reboot_fail();
1661 1651 else
1662 1652 (void) audit_halt_fail();
1663 1653
1664 1654 if (fast_reboot == 1) {
1665 1655 if (bename) {
1666 1656 (void) halt_exec(BEADM_PROG, "umount", bename, NULL);
1667 1657
1668 1658 } else if (strlen(fastboot_mounted) != 0) {
1669 1659 (void) umount(fastboot_mounted);
1670 1660 #if defined(__i386)
1671 1661 } else if (fbarg_used != NULL) {
1672 1662 grub_cleanup_boot_args(fbarg_used);
1673 1663 #endif /* __i386 */
1674 1664 }
1675 1665 }
1676 1666
1677 1667 return (1);
1678 1668 }
↓ open down ↓ |
205 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX