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