1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* 30 * startd.c - the master restarter 31 * 32 * svc.startd comprises two halves. The graph engine is based in graph.c and 33 * maintains the service dependency graph based on the information in the 34 * repository. For each service it also tracks the current state and the 35 * restarter responsible for the service. Based on the graph, events from the 36 * repository (mostly administrative requests from svcadm), and messages from 37 * the restarters, the graph engine makes decisions about how the services 38 * should be manipulated and sends commands to the appropriate restarters. 39 * Communication between the graph engine and the restarters is embodied in 40 * protocol.c. 41 * 42 * The second half of svc.startd is the restarter for services managed by 43 * svc.startd and is primarily contained in restarter.c. It responds to graph 44 * engine commands by executing methods, updating the repository, and sending 45 * feedback (mostly state updates) to the graph engine. 46 * 47 * Error handling 48 * 49 * In general, when svc.startd runs out of memory it reattempts a few times, 50 * sleeping inbetween, before giving up and exiting (see startd_alloc_retry()). 51 * When a repository connection is broken (libscf calls fail with 52 * SCF_ERROR_CONNECTION_BROKEN, librestart and internal functions return 53 * ECONNABORTED), svc.startd calls libscf_rebind_handle(), which coordinates 54 * with the svc.configd-restarting thread, fork_configd_thread(), via 55 * st->st_configd_live_cv, and rebinds the repository handle. Doing so resets 56 * all libscf state associated with that handle, so functions which do this 57 * should communicate the event to their callers (usually by returning 58 * ECONNRESET) so they may reset their state appropriately. 59 */ 60 61 #include <stdio.h> 62 #include <sys/mnttab.h> /* uses FILE * without including stdio.h */ 63 #include <alloca.h> 64 #include <sys/mount.h> 65 #include <sys/stat.h> 66 #include <sys/types.h> 67 #include <sys/wait.h> 68 #include <assert.h> 69 #include <errno.h> 70 #include <fcntl.h> 71 #include <ftw.h> 72 #include <libintl.h> 73 #include <libscf.h> 74 #include <libscf_priv.h> 75 #include <libuutil.h> 76 #include <locale.h> 77 #include <poll.h> 78 #include <pthread.h> 79 #include <signal.h> 80 #include <stdarg.h> 81 #include <stdlib.h> 82 #include <string.h> 83 #include <strings.h> 84 #include <unistd.h> 85 86 #include "startd.h" 87 #include "protocol.h" 88 89 ssize_t max_scf_name_size; 90 ssize_t max_scf_fmri_size; 91 ssize_t max_scf_value_size; 92 93 mode_t fmask; 94 mode_t dmask; 95 96 graph_update_t *gu; 97 restarter_update_t *ru; 98 99 startd_state_t *st; 100 101 boolean_t booting_to_single_user = B_FALSE; 102 103 const char * const admin_actions[] = { 104 SCF_PROPERTY_DEGRADED, 105 SCF_PROPERTY_MAINT_OFF, 106 SCF_PROPERTY_MAINT_ON, 107 SCF_PROPERTY_MAINT_ON_IMMEDIATE, 108 SCF_PROPERTY_REFRESH, 109 SCF_PROPERTY_RESTART 110 }; 111 112 const int admin_events[NACTIONS] = { 113 RESTARTER_EVENT_TYPE_ADMIN_DEGRADED, 114 RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF, 115 RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON, 116 RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON_IMMEDIATE, 117 RESTARTER_EVENT_TYPE_ADMIN_REFRESH, 118 RESTARTER_EVENT_TYPE_ADMIN_RESTART 119 }; 120 121 const char * const instance_state_str[] = { 122 "none", 123 "uninitialized", 124 "maintenance", 125 "offline", 126 "disabled", 127 "online", 128 "degraded" 129 }; 130 131 static int finished = 0; 132 static int opt_reconfig = 0; 133 static uint8_t prop_reconfig = 0; 134 135 #define INITIAL_REBIND_ATTEMPTS 5 136 #define INITIAL_REBIND_DELAY 3 137 138 pthread_mutexattr_t mutex_attrs; 139 140 const char * 141 _umem_debug_init(void) 142 { 143 return ("default,verbose"); /* UMEM_DEBUG setting */ 144 } 145 146 const char * 147 _umem_logging_init(void) 148 { 149 return ("fail,contents"); /* UMEM_LOGGING setting */ 150 } 151 152 /* 153 * startd_alloc_retry() 154 * Wrapper for allocation functions. Retries with a decaying time 155 * value on failure to allocate, and aborts startd if failure is 156 * persistent. 157 */ 158 void * 159 startd_alloc_retry(void *f(size_t, int), size_t sz) 160 { 161 void *p; 162 uint_t try, msecs; 163 164 p = f(sz, UMEM_DEFAULT); 165 if (p != NULL || sz == 0) 166 return (p); 167 168 msecs = ALLOC_DELAY; 169 170 for (try = 0; p == NULL && try < ALLOC_RETRY; ++try) { 171 (void) poll(NULL, 0, msecs); 172 msecs *= ALLOC_DELAY_MULT; 173 p = f(sz, UMEM_DEFAULT); 174 if (p != NULL) 175 return (p); 176 } 177 178 uu_die("Insufficient memory.\n"); 179 /* NOTREACHED */ 180 } 181 182 void * 183 safe_realloc(void *p, size_t sz) 184 { 185 uint_t try, msecs; 186 187 p = realloc(p, sz); 188 if (p != NULL || sz == 0) 189 return (p); 190 191 msecs = ALLOC_DELAY; 192 193 for (try = 0; errno == EAGAIN && try < ALLOC_RETRY; ++try) { 194 (void) poll(NULL, 0, msecs); 195 p = realloc(p, sz); 196 if (p != NULL) 197 return (p); 198 msecs *= ALLOC_DELAY_MULT; 199 } 200 201 uu_die("Insufficient memory.\n"); 202 /* NOTREACHED */ 203 } 204 205 char * 206 safe_strdup(const char *s) 207 { 208 uint_t try, msecs; 209 char *d; 210 211 d = strdup(s); 212 if (d != NULL) 213 return (d); 214 215 msecs = ALLOC_DELAY; 216 217 for (try = 0; 218 (errno == EAGAIN || errno == ENOMEM) && try < ALLOC_RETRY; 219 ++try) { 220 (void) poll(NULL, 0, msecs); 221 d = strdup(s); 222 if (d != NULL) 223 return (d); 224 msecs *= ALLOC_DELAY_MULT; 225 } 226 227 uu_die("Insufficient memory.\n"); 228 /* NOTREACHED */ 229 } 230 231 232 void 233 startd_free(void *p, size_t sz) 234 { 235 umem_free(p, sz); 236 } 237 238 /* 239 * Creates a uu_list_pool_t with the same retry policy as startd_alloc(). 240 * Only returns NULL for UU_ERROR_UNKNOWN_FLAG and UU_ERROR_NOT_SUPPORTED. 241 */ 242 uu_list_pool_t * 243 startd_list_pool_create(const char *name, size_t e, size_t o, 244 uu_compare_fn_t *f, uint32_t flags) 245 { 246 uu_list_pool_t *pool; 247 uint_t try, msecs; 248 249 pool = uu_list_pool_create(name, e, o, f, flags); 250 if (pool != NULL) 251 return (pool); 252 253 msecs = ALLOC_DELAY; 254 255 for (try = 0; uu_error() == UU_ERROR_NO_MEMORY && try < ALLOC_RETRY; 256 ++try) { 257 (void) poll(NULL, 0, msecs); 258 pool = uu_list_pool_create(name, e, o, f, flags); 259 if (pool != NULL) 260 return (pool); 261 msecs *= ALLOC_DELAY_MULT; 262 } 263 264 if (try < ALLOC_RETRY) 265 return (NULL); 266 267 uu_die("Insufficient memory.\n"); 268 /* NOTREACHED */ 269 } 270 271 /* 272 * Creates a uu_list_t with the same retry policy as startd_alloc(). Only 273 * returns NULL for UU_ERROR_UNKNOWN_FLAG and UU_ERROR_NOT_SUPPORTED. 274 */ 275 uu_list_t * 276 startd_list_create(uu_list_pool_t *pool, void *parent, uint32_t flags) 277 { 278 uu_list_t *list; 279 uint_t try, msecs; 280 281 list = uu_list_create(pool, parent, flags); 282 if (list != NULL) 283 return (list); 284 285 msecs = ALLOC_DELAY; 286 287 for (try = 0; uu_error() == UU_ERROR_NO_MEMORY && try < ALLOC_RETRY; 288 ++try) { 289 (void) poll(NULL, 0, msecs); 290 list = uu_list_create(pool, parent, flags); 291 if (list != NULL) 292 return (list); 293 msecs *= ALLOC_DELAY_MULT; 294 } 295 296 if (try < ALLOC_RETRY) 297 return (NULL); 298 299 uu_die("Insufficient memory.\n"); 300 /* NOTREACHED */ 301 } 302 303 pthread_t 304 startd_thread_create(void *(*func)(void *), void *ptr) 305 { 306 int err; 307 pthread_t tid; 308 309 err = pthread_create(&tid, NULL, func, ptr); 310 if (err != 0) { 311 assert(err == EAGAIN); 312 uu_die("Could not create thread.\n"); 313 } 314 315 err = pthread_detach(tid); 316 assert(err == 0); 317 318 return (tid); 319 } 320 321 322 static int 323 read_startd_config(int log_args) 324 { 325 scf_handle_t *hndl; 326 scf_instance_t *inst; 327 scf_propertygroup_t *pg; 328 scf_property_t *prop; 329 scf_value_t *val; 330 scf_iter_t *iter, *piter; 331 instance_data_t idata; 332 char *buf, *vbuf; 333 char *startd_options_fmri = uu_msprintf("%s/:properties/options", 334 SCF_SERVICE_STARTD); 335 char *startd_reconfigure_fmri = uu_msprintf( 336 "%s/:properties/system/reconfigure", SCF_SERVICE_STARTD); 337 char *env_opts, *lasts, *cp; 338 int bind_fails = 0; 339 int ret = 0, r; 340 uint_t count = 0, msecs = ALLOC_DELAY; 341 size_t sz; 342 ctid_t ctid; 343 uint64_t uint64; 344 345 buf = startd_alloc(max_scf_fmri_size); 346 347 if (startd_options_fmri == NULL || startd_reconfigure_fmri == NULL) 348 uu_die("Allocation failure\n"); 349 350 st->st_log_prefix = LOG_PREFIX_EARLY; 351 352 if ((st->st_log_file = getenv("STARTD_DEFAULT_LOG")) == NULL) { 353 st->st_log_file = startd_alloc(strlen(STARTD_DEFAULT_LOG) + 1); 354 355 (void) strcpy(st->st_log_file, STARTD_DEFAULT_LOG); 356 } 357 358 st->st_door_path = getenv("STARTD_ALT_DOOR"); 359 360 /* 361 * Read "options" property group. 362 */ 363 for (hndl = libscf_handle_create_bound(SCF_VERSION); hndl == NULL; 364 hndl = libscf_handle_create_bound(SCF_VERSION), bind_fails++) { 365 (void) sleep(INITIAL_REBIND_DELAY); 366 367 if (bind_fails > INITIAL_REBIND_ATTEMPTS) { 368 /* 369 * In the case that we can't bind to the repository 370 * (which should have been started), we need to allow 371 * the user into maintenance mode to determine what's 372 * failed. 373 */ 374 log_framework(LOG_INFO, "Couldn't fetch " 375 "default settings: %s\n", 376 scf_strerror(scf_error())); 377 378 ret = -1; 379 380 goto noscfout; 381 } 382 } 383 384 idata.i_fmri = SCF_SERVICE_STARTD; 385 idata.i_state = RESTARTER_STATE_NONE; 386 idata.i_next_state = RESTARTER_STATE_NONE; 387 timestamp: 388 switch (r = _restarter_commit_states(hndl, &idata, 389 RESTARTER_STATE_ONLINE, RESTARTER_STATE_NONE, NULL)) { 390 case 0: 391 break; 392 393 case ENOMEM: 394 ++count; 395 if (count < ALLOC_RETRY) { 396 (void) poll(NULL, 0, msecs); 397 msecs *= ALLOC_DELAY_MULT; 398 goto timestamp; 399 } 400 401 uu_die("Insufficient memory.\n"); 402 /* NOTREACHED */ 403 404 case ECONNABORTED: 405 libscf_handle_rebind(hndl); 406 goto timestamp; 407 408 case ENOENT: 409 case EPERM: 410 case EACCES: 411 case EROFS: 412 log_error(LOG_INFO, "Could set state of %s: %s.\n", 413 idata.i_fmri, strerror(r)); 414 break; 415 416 case EINVAL: 417 default: 418 bad_error("_restarter_commit_states", r); 419 } 420 421 pg = safe_scf_pg_create(hndl); 422 prop = safe_scf_property_create(hndl); 423 val = safe_scf_value_create(hndl); 424 inst = safe_scf_instance_create(hndl); 425 426 /* set startd's restarter properties */ 427 if (scf_handle_decode_fmri(hndl, SCF_SERVICE_STARTD, NULL, NULL, inst, 428 NULL, NULL, SCF_DECODE_FMRI_EXACT) == 0) { 429 (void) libscf_write_start_pid(inst, getpid()); 430 ctid = proc_get_ctid(); 431 if (ctid != -1) { 432 uint64 = (uint64_t)ctid; 433 (void) libscf_inst_set_count_prop(inst, 434 SCF_PG_RESTARTER, SCF_PG_RESTARTER_TYPE, 435 SCF_PG_RESTARTER_FLAGS, SCF_PROPERTY_CONTRACT, 436 uint64); 437 } 438 (void) libscf_note_method_log(inst, LOG_PREFIX_EARLY, 439 STARTD_DEFAULT_LOG); 440 (void) libscf_note_method_log(inst, LOG_PREFIX_NORMAL, 441 STARTD_DEFAULT_LOG); 442 } 443 444 /* Read reconfigure property for recovery. */ 445 if (scf_handle_decode_fmri(hndl, startd_reconfigure_fmri, NULL, NULL, 446 NULL, NULL, prop, NULL) != -1 && 447 scf_property_get_value(prop, val) == 0) 448 (void) scf_value_get_boolean(val, &prop_reconfig); 449 450 if (scf_handle_decode_fmri(hndl, startd_options_fmri, NULL, NULL, NULL, 451 pg, NULL, SCF_DECODE_FMRI_TRUNCATE) == -1) { 452 /* 453 * No configuration options defined. 454 */ 455 if (scf_error() != SCF_ERROR_NOT_FOUND) 456 uu_warn("Couldn't read configuration from 'options' " 457 "group: %s\n", scf_strerror(scf_error())); 458 goto scfout; 459 } 460 461 /* 462 * If there is no "options" group defined, then our defaults are fine. 463 */ 464 if (scf_pg_get_name(pg, NULL, 0) < 0) 465 goto scfout; 466 467 /* Iterate through. */ 468 iter = safe_scf_iter_create(hndl); 469 470 (void) scf_iter_pg_properties(iter, pg); 471 472 piter = safe_scf_iter_create(hndl); 473 vbuf = startd_alloc(max_scf_value_size); 474 475 while ((scf_iter_next_property(iter, prop) == 1)) { 476 scf_type_t ty; 477 478 if (scf_property_get_name(prop, buf, max_scf_fmri_size) < 0) 479 continue; 480 481 if (strcmp(buf, "logging") != 0 && 482 strcmp(buf, "boot_messages") != 0) 483 continue; 484 485 if (scf_property_type(prop, &ty) != 0) { 486 switch (scf_error()) { 487 case SCF_ERROR_CONNECTION_BROKEN: 488 default: 489 libscf_handle_rebind(hndl); 490 continue; 491 492 case SCF_ERROR_DELETED: 493 continue; 494 495 case SCF_ERROR_NOT_BOUND: 496 case SCF_ERROR_NOT_SET: 497 bad_error("scf_property_type", scf_error()); 498 } 499 } 500 501 if (ty != SCF_TYPE_ASTRING) { 502 uu_warn("property \"options/%s\" is not of type " 503 "astring; ignored.\n", buf); 504 continue; 505 } 506 507 if (scf_property_get_value(prop, val) != 0) { 508 switch (scf_error()) { 509 case SCF_ERROR_CONNECTION_BROKEN: 510 default: 511 return (ECONNABORTED); 512 513 case SCF_ERROR_DELETED: 514 case SCF_ERROR_NOT_FOUND: 515 return (0); 516 517 case SCF_ERROR_CONSTRAINT_VIOLATED: 518 uu_warn("property \"options/%s\" has multiple " 519 "values; ignored.\n", buf); 520 continue; 521 522 case SCF_ERROR_HANDLE_MISMATCH: 523 case SCF_ERROR_NOT_BOUND: 524 case SCF_ERROR_NOT_SET: 525 bad_error("scf_property_get_value", 526 scf_error()); 527 } 528 } 529 530 if (scf_value_get_astring(val, vbuf, max_scf_value_size) < 0) 531 bad_error("scf_value_get_astring", scf_error()); 532 533 if (!log_args && strcmp("logging", buf) == 0) { 534 if (strcmp("verbose", vbuf) == 0) { 535 st->st_boot_flags = STARTD_BOOT_VERBOSE; 536 st->st_log_flags = STARTD_LOG_VERBOSE; 537 st->st_log_level_min = LOG_INFO; 538 } else if (strcmp("debug", vbuf) == 0) { 539 st->st_boot_flags = STARTD_BOOT_VERBOSE; 540 st->st_log_flags = STARTD_LOG_DEBUG; 541 st->st_log_level_min = LOG_DEBUG; 542 } else if (strcmp("quiet", vbuf) == 0) { 543 st->st_log_flags = STARTD_LOG_QUIET; 544 st->st_log_level_min = LOG_NOTICE; 545 } else { 546 uu_warn("unknown options/logging " 547 "value '%s' ignored\n", vbuf); 548 } 549 550 } else if (strcmp("boot_messages", buf) == 0) { 551 if (strcmp("quiet", vbuf) == 0) { 552 st->st_boot_flags = STARTD_BOOT_QUIET; 553 } else if (strcmp("verbose", vbuf) == 0) { 554 st->st_boot_flags = STARTD_BOOT_VERBOSE; 555 } else { 556 log_framework(LOG_NOTICE, "unknown " 557 "options/boot_messages value '%s' " 558 "ignored\n", vbuf); 559 } 560 561 } 562 } 563 564 startd_free(vbuf, max_scf_value_size); 565 scf_iter_destroy(piter); 566 567 scf_iter_destroy(iter); 568 569 scfout: 570 scf_value_destroy(val); 571 scf_pg_destroy(pg); 572 scf_property_destroy(prop); 573 scf_instance_destroy(inst); 574 (void) scf_handle_unbind(hndl); 575 scf_handle_destroy(hndl); 576 577 noscfout: 578 startd_free(buf, max_scf_fmri_size); 579 uu_free(startd_options_fmri); 580 uu_free(startd_reconfigure_fmri); 581 582 if (booting_to_single_user) { 583 st->st_subgraph = startd_alloc(max_scf_fmri_size); 584 sz = strlcpy(st->st_subgraph, "milestone/single-user:default", 585 max_scf_fmri_size); 586 assert(sz < max_scf_fmri_size); 587 } 588 589 /* 590 * Options passed in as boot arguments override repository defaults. 591 */ 592 env_opts = getenv("SMF_OPTIONS"); 593 if (env_opts == NULL) 594 return (ret); 595 596 for (cp = strtok_r(env_opts, ",", &lasts); cp != NULL; 597 cp = strtok_r(NULL, ",", &lasts)) { 598 if (strcmp(cp, "debug") == 0) { 599 st->st_boot_flags = STARTD_BOOT_VERBOSE; 600 st->st_log_flags = STARTD_LOG_DEBUG; 601 st->st_log_level_min = LOG_DEBUG; 602 } else if (strcmp(cp, "verbose") == 0) { 603 st->st_boot_flags = STARTD_BOOT_VERBOSE; 604 st->st_log_flags = STARTD_LOG_VERBOSE; 605 st->st_log_level_min = LOG_INFO; 606 } else if (strcmp(cp, "seed") == 0) { 607 uu_warn("SMF option \"%s\" unimplemented.\n", cp); 608 } else if (strcmp(cp, "quiet") == 0) { 609 st->st_log_flags = STARTD_LOG_QUIET; 610 st->st_log_level_min = LOG_NOTICE; 611 } else if (strncmp(cp, "milestone=", 612 sizeof ("milestone=") - 1) == 0) { 613 char *mp = cp + sizeof ("milestone=") - 1; 614 615 if (booting_to_single_user) 616 continue; 617 618 if (st->st_subgraph == NULL) { 619 st->st_subgraph = 620 startd_alloc(max_scf_fmri_size); 621 st->st_subgraph[0] = '\0'; 622 } 623 624 if (mp[0] == '\0' || strcmp(mp, "all") == 0) { 625 (void) strcpy(st->st_subgraph, "all"); 626 } else if (strcmp(mp, "su") == 0 || 627 strcmp(mp, "single-user") == 0) { 628 (void) strcpy(st->st_subgraph, 629 "milestone/single-user:default"); 630 } else if (strcmp(mp, "mu") == 0 || 631 strcmp(mp, "multi-user") == 0) { 632 (void) strcpy(st->st_subgraph, 633 "milestone/multi-user:default"); 634 } else if (strcmp(mp, "mus") == 0 || 635 strcmp(mp, "multi-user-server") == 0) { 636 (void) strcpy(st->st_subgraph, 637 "milestone/multi-user-server:default"); 638 } else if (strcmp(mp, "none") == 0) { 639 (void) strcpy(st->st_subgraph, "none"); 640 } else { 641 log_framework(LOG_NOTICE, 642 "invalid milestone option value " 643 "'%s' ignored\n", mp); 644 } 645 } else { 646 uu_warn("Unknown SMF option \"%s\".\n", cp); 647 } 648 } 649 650 return (ret); 651 } 652 653 /* 654 * void set_boot_env() 655 * 656 * If -r was passed or /reconfigure exists, this is a reconfig 657 * reboot. We need to make sure that this information is given 658 * to the appropriate services the first time they're started 659 * by setting the system/reconfigure repository property, 660 * as well as pass the _INIT_RECONFIG variable on to the rcS 661 * start method so that legacy services can continue to use it. 662 * 663 * This function must never be called before contract_init(), as 664 * it sets st_initial. get_startd_config() sets prop_reconfig from 665 * pre-existing repository state. 666 */ 667 static void 668 set_boot_env() 669 { 670 struct stat sb; 671 int r; 672 673 /* 674 * Check if property still is set -- indicates we didn't get 675 * far enough previously to unset it. Otherwise, if this isn't 676 * the first startup, don't re-process /reconfigure or the 677 * boot flag. 678 */ 679 if (prop_reconfig != 1 && st->st_initial != 1) 680 return; 681 682 /* If /reconfigure exists, also set opt_reconfig. */ 683 if (stat("/reconfigure", &sb) != -1) 684 opt_reconfig = 1; 685 686 /* Nothing to do. Just return. */ 687 if (opt_reconfig == 0 && prop_reconfig == 0) 688 return; 689 690 /* 691 * Set startd's reconfigure property. This property is 692 * then cleared by successful completion of the single-user 693 * milestone. 694 */ 695 if (prop_reconfig != 1) { 696 r = libscf_set_reconfig(1); 697 switch (r) { 698 case 0: 699 break; 700 701 case ENOENT: 702 case EPERM: 703 case EACCES: 704 case EROFS: 705 log_error(LOG_WARNING, "Could not set reconfiguration " 706 "property: %s\n", strerror(r)); 707 break; 708 709 default: 710 bad_error("libscf_set_reconfig", r); 711 } 712 } 713 } 714 715 static void 716 startup(int log_args) 717 { 718 ctid_t configd_ctid; 719 int err; 720 721 /* 722 * Initialize data structures. 723 */ 724 gu = startd_zalloc(sizeof (graph_update_t)); 725 ru = startd_zalloc(sizeof (restarter_update_t)); 726 727 (void) pthread_cond_init(&st->st_load_cv, NULL); 728 (void) pthread_cond_init(&st->st_configd_live_cv, NULL); 729 (void) pthread_cond_init(&gu->gu_cv, NULL); 730 (void) pthread_cond_init(&gu->gu_freeze_cv, NULL); 731 (void) pthread_cond_init(&ru->restarter_update_cv, NULL); 732 (void) pthread_mutex_init(&st->st_load_lock, &mutex_attrs); 733 (void) pthread_mutex_init(&st->st_configd_live_lock, &mutex_attrs); 734 (void) pthread_mutex_init(&gu->gu_lock, &mutex_attrs); 735 (void) pthread_mutex_init(&gu->gu_freeze_lock, &mutex_attrs); 736 (void) pthread_mutex_init(&ru->restarter_update_lock, &mutex_attrs); 737 738 configd_ctid = contract_init(); 739 740 if (configd_ctid != -1) 741 log_framework(LOG_DEBUG, "Existing configd contract %ld; not " 742 "starting svc.configd\n", configd_ctid); 743 744 (void) startd_thread_create(fork_configd_thread, (void *)configd_ctid); 745 746 /* 747 * Await, if necessary, configd's initial arrival. 748 */ 749 MUTEX_LOCK(&st->st_configd_live_lock); 750 while (!st->st_configd_lives) { 751 log_framework(LOG_DEBUG, "Awaiting cv signal on " 752 "configd_live_cv\n"); 753 err = pthread_cond_wait(&st->st_configd_live_cv, 754 &st->st_configd_live_lock); 755 assert(err == 0); 756 } 757 MUTEX_UNLOCK(&st->st_configd_live_lock); 758 759 utmpx_init(); 760 wait_init(); 761 762 if (read_startd_config(log_args)) 763 log_framework(LOG_INFO, "svc.configd unable to provide startd " 764 "optional settings\n"); 765 766 log_init(); 767 dict_init(); 768 timeout_init(); 769 restarter_protocol_init(); 770 restarter_init(); 771 graph_protocol_init(); 772 graph_init(); 773 774 init_env(); 775 776 set_boot_env(); 777 restarter_start(); 778 graph_engine_start(); 779 } 780 781 static void 782 usage(const char *name) 783 { 784 uu_warn(gettext("usage: %s [-dnq]\n"), name); 785 exit(UU_EXIT_USAGE); 786 } 787 788 static int 789 daemonize_start(void) 790 { 791 pid_t pid; 792 int fd; 793 794 if ((pid = fork1()) < 0) 795 return (-1); 796 797 if (pid != 0) 798 exit(0); 799 800 (void) close(0); 801 802 if ((fd = open("/dev/null", O_RDONLY)) == -1) { 803 uu_warn(gettext("can't connect stdin to /dev/null")); 804 } else if (fd != 0) { 805 (void) dup2(fd, 0); 806 startd_close(fd); 807 } 808 809 closefrom(3); 810 (void) dup2(2, 1); 811 812 (void) setsid(); 813 (void) chdir("/"); 814 815 /* Use default umask that init handed us, but 022 to create files. */ 816 dmask = umask(022); 817 fmask = umask(dmask); 818 819 return (0); 820 } 821 822 /*ARGSUSED*/ 823 static void 824 die_handler(int sig, siginfo_t *info, void *data) 825 { 826 finished = 1; 827 } 828 829 int 830 main(int argc, char *argv[]) 831 { 832 int opt; 833 int daemonize = 1; 834 int log_args = 0; 835 struct sigaction act; 836 sigset_t nullset; 837 struct stat sb; 838 839 (void) uu_setpname(argv[0]); 840 841 st = startd_zalloc(sizeof (startd_state_t)); 842 843 (void) pthread_mutexattr_init(&mutex_attrs); 844 #ifndef NDEBUG 845 (void) pthread_mutexattr_settype(&mutex_attrs, 846 PTHREAD_MUTEX_ERRORCHECK); 847 #endif 848 849 max_scf_name_size = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH); 850 max_scf_value_size = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH); 851 max_scf_fmri_size = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH); 852 853 if (max_scf_name_size == -1 || max_scf_value_size == -1 || 854 max_scf_value_size == -1) 855 uu_die("Can't determine repository maximum lengths.\n"); 856 857 max_scf_name_size++; 858 max_scf_value_size++; 859 max_scf_fmri_size++; 860 861 st->st_log_flags = STARTD_LOG_FILE; 862 st->st_log_level_min = LOG_INFO; 863 864 while ((opt = getopt(argc, argv, "dnqrs")) != EOF) { 865 switch (opt) { 866 case 'd': 867 st->st_log_flags = 868 STARTD_LOG_FILE | STARTD_LOG_TERMINAL; 869 st->st_log_level_min = LOG_DEBUG; 870 log_args = 1; 871 break; 872 case 'n': 873 daemonize = 0; 874 break; 875 case 'q': 876 st->st_log_flags = 0; 877 st->st_log_level_min = LOG_NOTICE; 878 log_args = 1; 879 break; 880 case 'r': /* reconfiguration boot */ 881 opt_reconfig = 1; 882 break; 883 case 's': /* single-user mode */ 884 booting_to_single_user = B_TRUE; 885 break; 886 default: 887 usage(argv[0]); /* exits */ 888 } 889 } 890 891 if (optind != argc) 892 usage(argv[0]); 893 894 if (daemonize) 895 if (daemonize_start() < 0) 896 uu_die("Can't daemonize\n"); 897 898 log_init(); 899 900 if (stat("/etc/svc/volatile/resetting", &sb) != -1) { 901 log_framework(LOG_NOTICE, "Restarter quiesced.\n"); 902 903 for (;;) 904 (void) pause(); 905 } 906 907 act.sa_sigaction = &die_handler; 908 (void) sigfillset(&act.sa_mask); 909 act.sa_flags = SA_SIGINFO; 910 (void) sigaction(SIGINT, &act, NULL); 911 (void) sigaction(SIGTERM, &act, NULL); 912 913 startup(log_args); 914 915 (void) sigemptyset(&nullset); 916 while (!finished) { 917 log_framework(LOG_DEBUG, "Main thread paused\n"); 918 (void) sigsuspend(&nullset); 919 } 920 921 (void) log_framework(LOG_DEBUG, "Restarter exiting.\n"); 922 return (0); 923 } 924