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 (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 /* 29 * graph.c - master restarter graph engine 30 * 31 * The graph engine keeps a dependency graph of all service instances on the 32 * system, as recorded in the repository. It decides when services should 33 * be brought up or down based on service states and dependencies and sends 34 * commands to restarters to effect any changes. It also executes 35 * administrator commands sent by svcadm via the repository. 36 * 37 * The graph is stored in uu_list_t *dgraph and its vertices are 38 * graph_vertex_t's, each of which has a name and an integer id unique to 39 * its name (see dict.c). A vertex's type attribute designates the type 40 * of object it represents: GVT_INST for service instances, GVT_SVC for 41 * service objects (since service instances may depend on another service, 42 * rather than service instance), GVT_FILE for files (which services may 43 * depend on), and GVT_GROUP for dependencies on multiple objects. GVT_GROUP 44 * vertices are necessary because dependency lists may have particular 45 * grouping types (require any, require all, optional, or exclude) and 46 * event-propagation characteristics. 47 * 48 * The initial graph is built by libscf_populate_graph() invoking 49 * dgraph_add_instance() for each instance in the repository. The function 50 * adds a GVT_SVC vertex for the service if one does not already exist, adds 51 * a GVT_INST vertex named by the FMRI of the instance, and sets up the edges. 52 * The resulting web of vertices & edges associated with an instance's vertex 53 * includes 54 * 55 * - an edge from the GVT_SVC vertex for the instance's service 56 * 57 * - an edge to the GVT_INST vertex of the instance's resarter, if its 58 * restarter is not svc.startd 59 * 60 * - edges from other GVT_INST vertices if the instance is a restarter 61 * 62 * - for each dependency property group in the instance's "running" 63 * snapshot, an edge to a GVT_GROUP vertex named by the FMRI of the 64 * instance and the name of the property group 65 * 66 * - for each value of the "entities" property in each dependency property 67 * group, an edge from the corresponding GVT_GROUP vertex to a 68 * GVT_INST, GVT_SVC, or GVT_FILE vertex 69 * 70 * - edges from GVT_GROUP vertices for each dependent instance 71 * 72 * After the edges are set up the vertex's GV_CONFIGURED flag is set. If 73 * there are problems, or if a service is mentioned in a dependency but does 74 * not exist in the repository, the GV_CONFIGURED flag will be clear. 75 * 76 * The graph and all of its vertices are protected by the dgraph_lock mutex. 77 * See restarter.c for more information. 78 * 79 * The properties of an instance fall into two classes: immediate and 80 * snapshotted. Immediate properties should have an immediate effect when 81 * changed. Snapshotted properties should be read from a snapshot, so they 82 * only change when the snapshot changes. The immediate properties used by 83 * the graph engine are general/enabled, general/restarter, and the properties 84 * in the restarter_actions property group. Since they are immediate, they 85 * are not read out of a snapshot. The snapshotted properties used by the 86 * graph engine are those in the property groups with type "dependency" and 87 * are read out of the "running" snapshot. The "running" snapshot is created 88 * by the the graph engine as soon as possible, and it is updated, along with 89 * in-core copies of the data (dependency information for the graph engine) on 90 * receipt of the refresh command from svcadm. In addition, the graph engine 91 * updates the "start" snapshot from the "running" snapshot whenever a service 92 * comes online. 93 */ 94 95 #include <sys/uadmin.h> 96 #include <sys/wait.h> 97 98 #include <assert.h> 99 #include <errno.h> 100 #include <fcntl.h> 101 #include <libscf.h> 102 #include <libscf_priv.h> 103 #include <libuutil.h> 104 #include <locale.h> 105 #include <poll.h> 106 #include <pthread.h> 107 #include <signal.h> 108 #include <stddef.h> 109 #include <stdio.h> 110 #include <stdlib.h> 111 #include <string.h> 112 #include <strings.h> 113 #include <sys/statvfs.h> 114 #include <sys/uadmin.h> 115 #include <zone.h> 116 117 #include "startd.h" 118 #include "protocol.h" 119 120 121 #define MILESTONE_NONE ((graph_vertex_t *)1) 122 123 #define CONSOLE_LOGIN_FMRI "svc:/system/console-login:default" 124 #define FS_MINIMAL_FMRI "svc:/system/filesystem/minimal:default" 125 126 #define VERTEX_REMOVED 0 /* vertex has been freed */ 127 #define VERTEX_INUSE 1 /* vertex is still in use */ 128 129 static uu_list_pool_t *graph_edge_pool, *graph_vertex_pool; 130 static uu_list_t *dgraph; 131 static pthread_mutex_t dgraph_lock; 132 133 /* 134 * milestone indicates the current subgraph. When NULL, it is the entire 135 * graph. When MILESTONE_NONE, it is the empty graph. Otherwise, it is all 136 * services on which the target vertex depends. 137 */ 138 static graph_vertex_t *milestone = NULL; 139 static boolean_t initial_milestone_set = B_FALSE; 140 static pthread_cond_t initial_milestone_cv = PTHREAD_COND_INITIALIZER; 141 142 /* protected by dgraph_lock */ 143 static boolean_t sulogin_thread_running = B_FALSE; 144 static boolean_t sulogin_running = B_FALSE; 145 static boolean_t console_login_ready = B_FALSE; 146 147 /* Number of services to come down to complete milestone transition. */ 148 static uint_t non_subgraph_svcs; 149 150 /* 151 * These variables indicate what should be done when we reach the milestone 152 * target milestone, i.e., when non_subgraph_svcs == 0. They are acted upon in 153 * dgraph_set_instance_state(). 154 */ 155 static int halting = -1; 156 static boolean_t go_single_user_mode = B_FALSE; 157 static boolean_t go_to_level1 = B_FALSE; 158 159 /* 160 * This tracks the legacy runlevel to ensure we signal init and manage 161 * utmpx entries correctly. 162 */ 163 static char current_runlevel = '\0'; 164 165 /* Number of single user threads currently running */ 166 static pthread_mutex_t single_user_thread_lock; 167 static int single_user_thread_count = 0; 168 169 /* Statistics for dependency cycle-checking */ 170 static u_longlong_t dep_inserts = 0; 171 static u_longlong_t dep_cycle_ns = 0; 172 static u_longlong_t dep_insert_ns = 0; 173 174 175 static const char * const emsg_invalid_restarter = 176 "Transitioning %s to maintenance, restarter FMRI %s is invalid " 177 "(see 'svcs -xv' for details).\n"; 178 static const char * const console_login_fmri = CONSOLE_LOGIN_FMRI; 179 static const char * const single_user_fmri = SCF_MILESTONE_SINGLE_USER; 180 static const char * const multi_user_fmri = SCF_MILESTONE_MULTI_USER; 181 static const char * const multi_user_svr_fmri = SCF_MILESTONE_MULTI_USER_SERVER; 182 183 184 /* 185 * These services define the system being "up". If none of them can come 186 * online, then we will run sulogin on the console. Note that the install ones 187 * are for the miniroot and when installing CDs after the first. can_come_up() 188 * does the decision making, and an sulogin_thread() runs sulogin, which can be 189 * started by dgraph_set_instance_state() or single_user_thread(). 190 * 191 * NOTE: can_come_up() relies on SCF_MILESTONE_SINGLE_USER being the first 192 * entry, which is only used when booting_to_single_user (boot -s) is set. 193 * This is because when doing a "boot -s", sulogin is started from specials.c 194 * after milestone/single-user comes online, for backwards compatibility. 195 * In this case, SCF_MILESTONE_SINGLE_USER needs to be part of up_svcs 196 * to ensure sulogin will be spawned if milestone/single-user cannot be reached. 197 */ 198 static const char * const up_svcs[] = { 199 SCF_MILESTONE_SINGLE_USER, 200 CONSOLE_LOGIN_FMRI, 201 "svc:/system/install-setup:default", 202 "svc:/system/install:default", 203 NULL 204 }; 205 206 /* This array must have an element for each non-NULL element of up_svcs[]. */ 207 static graph_vertex_t *up_svcs_p[] = { NULL, NULL, NULL, NULL }; 208 209 /* These are for seed repository magic. See can_come_up(). */ 210 static const char * const manifest_import = 211 "svc:/system/manifest-import:default"; 212 static graph_vertex_t *manifest_import_p = NULL; 213 214 215 static char target_milestone_as_runlevel(void); 216 static void graph_runlevel_changed(char rl, int online); 217 static int dgraph_set_milestone(const char *, scf_handle_t *, boolean_t); 218 static boolean_t should_be_in_subgraph(graph_vertex_t *v); 219 220 /* 221 * graph_vertex_compare() 222 * This function can compare either int *id or * graph_vertex_t *gv 223 * values, as the vertex id is always the first element of a 224 * graph_vertex structure. 225 */ 226 /* ARGSUSED */ 227 static int 228 graph_vertex_compare(const void *lc_arg, const void *rc_arg, void *private) 229 { 230 int lc_id = ((const graph_vertex_t *)lc_arg)->gv_id; 231 int rc_id = *(int *)rc_arg; 232 233 if (lc_id > rc_id) 234 return (1); 235 if (lc_id < rc_id) 236 return (-1); 237 return (0); 238 } 239 240 void 241 graph_init() 242 { 243 graph_edge_pool = startd_list_pool_create("graph_edges", 244 sizeof (graph_edge_t), offsetof(graph_edge_t, ge_link), NULL, 245 UU_LIST_POOL_DEBUG); 246 assert(graph_edge_pool != NULL); 247 248 graph_vertex_pool = startd_list_pool_create("graph_vertices", 249 sizeof (graph_vertex_t), offsetof(graph_vertex_t, gv_link), 250 graph_vertex_compare, UU_LIST_POOL_DEBUG); 251 assert(graph_vertex_pool != NULL); 252 253 (void) pthread_mutex_init(&dgraph_lock, &mutex_attrs); 254 (void) pthread_mutex_init(&single_user_thread_lock, &mutex_attrs); 255 dgraph = startd_list_create(graph_vertex_pool, NULL, UU_LIST_SORTED); 256 assert(dgraph != NULL); 257 258 if (!st->st_initial) 259 current_runlevel = utmpx_get_runlevel(); 260 261 log_framework(LOG_DEBUG, "Initialized graph\n"); 262 } 263 264 static graph_vertex_t * 265 vertex_get_by_name(const char *name) 266 { 267 int id; 268 269 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 270 271 id = dict_lookup_byname(name); 272 if (id == -1) 273 return (NULL); 274 275 return (uu_list_find(dgraph, &id, NULL, NULL)); 276 } 277 278 static graph_vertex_t * 279 vertex_get_by_id(int id) 280 { 281 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 282 283 if (id == -1) 284 return (NULL); 285 286 return (uu_list_find(dgraph, &id, NULL, NULL)); 287 } 288 289 /* 290 * Creates a new vertex with the given name, adds it to the graph, and returns 291 * a pointer to it. The graph lock must be held by this thread on entry. 292 */ 293 static graph_vertex_t * 294 graph_add_vertex(const char *name) 295 { 296 int id; 297 graph_vertex_t *v; 298 void *p; 299 uu_list_index_t idx; 300 301 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 302 303 id = dict_insert(name); 304 305 v = startd_zalloc(sizeof (*v)); 306 307 v->gv_id = id; 308 309 v->gv_name = startd_alloc(strlen(name) + 1); 310 (void) strcpy(v->gv_name, name); 311 312 v->gv_dependencies = startd_list_create(graph_edge_pool, v, 0); 313 v->gv_dependents = startd_list_create(graph_edge_pool, v, 0); 314 315 p = uu_list_find(dgraph, &id, NULL, &idx); 316 assert(p == NULL); 317 318 uu_list_node_init(v, &v->gv_link, graph_vertex_pool); 319 uu_list_insert(dgraph, v, idx); 320 321 return (v); 322 } 323 324 /* 325 * Removes v from the graph and frees it. The graph should be locked by this 326 * thread, and v should have no edges associated with it. 327 */ 328 static void 329 graph_remove_vertex(graph_vertex_t *v) 330 { 331 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 332 333 assert(uu_list_numnodes(v->gv_dependencies) == 0); 334 assert(uu_list_numnodes(v->gv_dependents) == 0); 335 assert(v->gv_refs == 0); 336 337 startd_free(v->gv_name, strlen(v->gv_name) + 1); 338 uu_list_destroy(v->gv_dependencies); 339 uu_list_destroy(v->gv_dependents); 340 uu_list_remove(dgraph, v); 341 342 startd_free(v, sizeof (graph_vertex_t)); 343 } 344 345 static void 346 graph_add_edge(graph_vertex_t *fv, graph_vertex_t *tv) 347 { 348 graph_edge_t *e, *re; 349 int r; 350 351 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 352 353 e = startd_alloc(sizeof (graph_edge_t)); 354 re = startd_alloc(sizeof (graph_edge_t)); 355 356 e->ge_parent = fv; 357 e->ge_vertex = tv; 358 359 re->ge_parent = tv; 360 re->ge_vertex = fv; 361 362 uu_list_node_init(e, &e->ge_link, graph_edge_pool); 363 r = uu_list_insert_before(fv->gv_dependencies, NULL, e); 364 assert(r == 0); 365 366 uu_list_node_init(re, &re->ge_link, graph_edge_pool); 367 r = uu_list_insert_before(tv->gv_dependents, NULL, re); 368 assert(r == 0); 369 } 370 371 static void 372 graph_remove_edge(graph_vertex_t *v, graph_vertex_t *dv) 373 { 374 graph_edge_t *e; 375 376 for (e = uu_list_first(v->gv_dependencies); 377 e != NULL; 378 e = uu_list_next(v->gv_dependencies, e)) { 379 if (e->ge_vertex == dv) { 380 uu_list_remove(v->gv_dependencies, e); 381 startd_free(e, sizeof (graph_edge_t)); 382 break; 383 } 384 } 385 386 for (e = uu_list_first(dv->gv_dependents); 387 e != NULL; 388 e = uu_list_next(dv->gv_dependents, e)) { 389 if (e->ge_vertex == v) { 390 uu_list_remove(dv->gv_dependents, e); 391 startd_free(e, sizeof (graph_edge_t)); 392 break; 393 } 394 } 395 } 396 397 static void 398 remove_inst_vertex(graph_vertex_t *v) 399 { 400 graph_edge_t *e; 401 graph_vertex_t *sv; 402 int i; 403 404 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 405 assert(uu_list_numnodes(v->gv_dependents) == 1); 406 assert(uu_list_numnodes(v->gv_dependencies) == 0); 407 assert(v->gv_refs == 0); 408 assert((v->gv_flags & GV_CONFIGURED) == 0); 409 410 e = uu_list_first(v->gv_dependents); 411 sv = e->ge_vertex; 412 graph_remove_edge(sv, v); 413 414 for (i = 0; up_svcs[i] != NULL; ++i) { 415 if (up_svcs_p[i] == v) 416 up_svcs_p[i] = NULL; 417 } 418 419 if (manifest_import_p == v) 420 manifest_import_p = NULL; 421 422 graph_remove_vertex(v); 423 424 if (uu_list_numnodes(sv->gv_dependencies) == 0 && 425 uu_list_numnodes(sv->gv_dependents) == 0 && 426 sv->gv_refs == 0) 427 graph_remove_vertex(sv); 428 } 429 430 static void 431 graph_walk_dependents(graph_vertex_t *v, void (*func)(graph_vertex_t *, void *), 432 void *arg) 433 { 434 graph_edge_t *e; 435 436 for (e = uu_list_first(v->gv_dependents); 437 e != NULL; 438 e = uu_list_next(v->gv_dependents, e)) 439 func(e->ge_vertex, arg); 440 } 441 442 static void 443 graph_walk_dependencies(graph_vertex_t *v, void (*func)(graph_vertex_t *, 444 void *), void *arg) 445 { 446 graph_edge_t *e; 447 448 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 449 450 for (e = uu_list_first(v->gv_dependencies); 451 e != NULL; 452 e = uu_list_next(v->gv_dependencies, e)) { 453 454 func(e->ge_vertex, arg); 455 } 456 } 457 458 /* 459 * Generic graph walking function. 460 * 461 * Given a vertex, this function will walk either dependencies 462 * (WALK_DEPENDENCIES) or dependents (WALK_DEPENDENTS) of a vertex recursively 463 * for the entire graph. It will avoid cycles and never visit the same vertex 464 * twice. 465 * 466 * We avoid traversing exclusion dependencies, because they are allowed to 467 * create cycles in the graph. When propagating satisfiability, there is no 468 * need to walk exclusion dependencies because exclude_all_satisfied() doesn't 469 * test for satisfiability. 470 * 471 * The walker takes two callbacks. The first is called before examining the 472 * dependents of each vertex. The second is called on each vertex after 473 * examining its dependents. This allows is_path_to() to construct a path only 474 * after the target vertex has been found. 475 */ 476 typedef enum { 477 WALK_DEPENDENTS, 478 WALK_DEPENDENCIES 479 } graph_walk_dir_t; 480 481 typedef int (*graph_walk_cb_t)(graph_vertex_t *, void *); 482 483 typedef struct graph_walk_info { 484 graph_walk_dir_t gi_dir; 485 uchar_t *gi_visited; /* vertex bitmap */ 486 int (*gi_pre)(graph_vertex_t *, void *); 487 void (*gi_post)(graph_vertex_t *, void *); 488 void *gi_arg; /* callback arg */ 489 int gi_ret; /* return value */ 490 } graph_walk_info_t; 491 492 static int 493 graph_walk_recurse(graph_edge_t *e, graph_walk_info_t *gip) 494 { 495 uu_list_t *list; 496 int r; 497 graph_vertex_t *v = e->ge_vertex; 498 int i; 499 uint_t b; 500 501 i = v->gv_id / 8; 502 b = 1 << (v->gv_id % 8); 503 504 /* 505 * Check to see if we've visited this vertex already. 506 */ 507 if (gip->gi_visited[i] & b) 508 return (UU_WALK_NEXT); 509 510 gip->gi_visited[i] |= b; 511 512 /* 513 * Don't follow exclusions. 514 */ 515 if (v->gv_type == GVT_GROUP && v->gv_depgroup == DEPGRP_EXCLUDE_ALL) 516 return (UU_WALK_NEXT); 517 518 /* 519 * Call pre-visit callback. If this doesn't terminate the walk, 520 * continue search. 521 */ 522 if ((gip->gi_ret = gip->gi_pre(v, gip->gi_arg)) == UU_WALK_NEXT) { 523 /* 524 * Recurse using appropriate list. 525 */ 526 if (gip->gi_dir == WALK_DEPENDENTS) 527 list = v->gv_dependents; 528 else 529 list = v->gv_dependencies; 530 531 r = uu_list_walk(list, (uu_walk_fn_t *)graph_walk_recurse, 532 gip, 0); 533 assert(r == 0); 534 } 535 536 /* 537 * Callbacks must return either UU_WALK_NEXT or UU_WALK_DONE. 538 */ 539 assert(gip->gi_ret == UU_WALK_NEXT || gip->gi_ret == UU_WALK_DONE); 540 541 /* 542 * If given a post-callback, call the function for every vertex. 543 */ 544 if (gip->gi_post != NULL) 545 (void) gip->gi_post(v, gip->gi_arg); 546 547 /* 548 * Preserve the callback's return value. If the callback returns 549 * UU_WALK_DONE, then we propagate that to the caller in order to 550 * terminate the walk. 551 */ 552 return (gip->gi_ret); 553 } 554 555 static void 556 graph_walk(graph_vertex_t *v, graph_walk_dir_t dir, 557 int (*pre)(graph_vertex_t *, void *), 558 void (*post)(graph_vertex_t *, void *), void *arg) 559 { 560 graph_walk_info_t gi; 561 graph_edge_t fake; 562 size_t sz = dictionary->dict_new_id / 8 + 1; 563 564 gi.gi_visited = startd_zalloc(sz); 565 gi.gi_pre = pre; 566 gi.gi_post = post; 567 gi.gi_arg = arg; 568 gi.gi_dir = dir; 569 gi.gi_ret = 0; 570 571 /* 572 * Fake up an edge for the first iteration 573 */ 574 fake.ge_vertex = v; 575 (void) graph_walk_recurse(&fake, &gi); 576 577 startd_free(gi.gi_visited, sz); 578 } 579 580 typedef struct child_search { 581 int id; /* id of vertex to look for */ 582 uint_t depth; /* recursion depth */ 583 /* 584 * While the vertex is not found, path is NULL. After the search, if 585 * the vertex was found then path should point to a -1-terminated 586 * array of vertex id's which constitute the path to the vertex. 587 */ 588 int *path; 589 } child_search_t; 590 591 static int 592 child_pre(graph_vertex_t *v, void *arg) 593 { 594 child_search_t *cs = arg; 595 596 cs->depth++; 597 598 if (v->gv_id == cs->id) { 599 cs->path = startd_alloc((cs->depth + 1) * sizeof (int)); 600 cs->path[cs->depth] = -1; 601 return (UU_WALK_DONE); 602 } 603 604 return (UU_WALK_NEXT); 605 } 606 607 static void 608 child_post(graph_vertex_t *v, void *arg) 609 { 610 child_search_t *cs = arg; 611 612 cs->depth--; 613 614 if (cs->path != NULL) 615 cs->path[cs->depth] = v->gv_id; 616 } 617 618 /* 619 * Look for a path from from to to. If one exists, returns a pointer to 620 * a NULL-terminated array of pointers to the vertices along the path. If 621 * there is no path, returns NULL. 622 */ 623 static int * 624 is_path_to(graph_vertex_t *from, graph_vertex_t *to) 625 { 626 child_search_t cs; 627 628 cs.id = to->gv_id; 629 cs.depth = 0; 630 cs.path = NULL; 631 632 graph_walk(from, WALK_DEPENDENCIES, child_pre, child_post, &cs); 633 634 return (cs.path); 635 } 636 637 /* 638 * Given an array of int's as returned by is_path_to, allocates a string of 639 * their names joined by newlines. Returns the size of the allocated buffer 640 * in *sz and frees path. 641 */ 642 static void 643 path_to_str(int *path, char **cpp, size_t *sz) 644 { 645 int i; 646 graph_vertex_t *v; 647 size_t allocd, new_allocd; 648 char *new, *name; 649 650 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 651 assert(path[0] != -1); 652 653 allocd = 1; 654 *cpp = startd_alloc(1); 655 (*cpp)[0] = '\0'; 656 657 for (i = 0; path[i] != -1; ++i) { 658 name = NULL; 659 660 v = vertex_get_by_id(path[i]); 661 662 if (v == NULL) 663 name = "<deleted>"; 664 else if (v->gv_type == GVT_INST || v->gv_type == GVT_SVC) 665 name = v->gv_name; 666 667 if (name != NULL) { 668 new_allocd = allocd + strlen(name) + 1; 669 new = startd_alloc(new_allocd); 670 (void) strcpy(new, *cpp); 671 (void) strcat(new, name); 672 (void) strcat(new, "\n"); 673 674 startd_free(*cpp, allocd); 675 676 *cpp = new; 677 allocd = new_allocd; 678 } 679 } 680 681 startd_free(path, sizeof (int) * (i + 1)); 682 683 *sz = allocd; 684 } 685 686 687 /* 688 * This function along with run_sulogin() implements an exclusion relationship 689 * between system/console-login and sulogin. run_sulogin() will fail if 690 * system/console-login is online, and the graph engine should call 691 * graph_clogin_start() to bring system/console-login online, which defers the 692 * start if sulogin is running. 693 */ 694 static void 695 graph_clogin_start(graph_vertex_t *v) 696 { 697 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 698 699 if (sulogin_running) 700 console_login_ready = B_TRUE; 701 else 702 vertex_send_event(v, RESTARTER_EVENT_TYPE_START); 703 } 704 705 static void 706 graph_su_start(graph_vertex_t *v) 707 { 708 /* 709 * /etc/inittab used to have the initial /sbin/rcS as a 'sysinit' 710 * entry with a runlevel of 'S', before jumping to the final 711 * target runlevel (as set in initdefault). We mimic that legacy 712 * behavior here. 713 */ 714 utmpx_set_runlevel('S', '0', B_FALSE); 715 vertex_send_event(v, RESTARTER_EVENT_TYPE_START); 716 } 717 718 static void 719 graph_post_su_online(void) 720 { 721 graph_runlevel_changed('S', 1); 722 } 723 724 static void 725 graph_post_su_disable(void) 726 { 727 graph_runlevel_changed('S', 0); 728 } 729 730 static void 731 graph_post_mu_online(void) 732 { 733 graph_runlevel_changed('2', 1); 734 } 735 736 static void 737 graph_post_mu_disable(void) 738 { 739 graph_runlevel_changed('2', 0); 740 } 741 742 static void 743 graph_post_mus_online(void) 744 { 745 graph_runlevel_changed('3', 1); 746 } 747 748 static void 749 graph_post_mus_disable(void) 750 { 751 graph_runlevel_changed('3', 0); 752 } 753 754 static struct special_vertex_info { 755 const char *name; 756 void (*start_f)(graph_vertex_t *); 757 void (*post_online_f)(void); 758 void (*post_disable_f)(void); 759 } special_vertices[] = { 760 { CONSOLE_LOGIN_FMRI, graph_clogin_start, NULL, NULL }, 761 { SCF_MILESTONE_SINGLE_USER, graph_su_start, 762 graph_post_su_online, graph_post_su_disable }, 763 { SCF_MILESTONE_MULTI_USER, NULL, 764 graph_post_mu_online, graph_post_mu_disable }, 765 { SCF_MILESTONE_MULTI_USER_SERVER, NULL, 766 graph_post_mus_online, graph_post_mus_disable }, 767 { NULL }, 768 }; 769 770 771 void 772 vertex_send_event(graph_vertex_t *v, restarter_event_type_t e) 773 { 774 switch (e) { 775 case RESTARTER_EVENT_TYPE_ADD_INSTANCE: 776 assert(v->gv_state == RESTARTER_STATE_UNINIT); 777 778 MUTEX_LOCK(&st->st_load_lock); 779 st->st_load_instances++; 780 MUTEX_UNLOCK(&st->st_load_lock); 781 break; 782 783 case RESTARTER_EVENT_TYPE_ENABLE: 784 log_framework(LOG_DEBUG, "Enabling %s.\n", v->gv_name); 785 assert(v->gv_state == RESTARTER_STATE_UNINIT || 786 v->gv_state == RESTARTER_STATE_DISABLED || 787 v->gv_state == RESTARTER_STATE_MAINT); 788 break; 789 790 case RESTARTER_EVENT_TYPE_DISABLE: 791 case RESTARTER_EVENT_TYPE_ADMIN_DISABLE: 792 log_framework(LOG_DEBUG, "Disabling %s.\n", v->gv_name); 793 assert(v->gv_state != RESTARTER_STATE_DISABLED); 794 break; 795 796 case RESTARTER_EVENT_TYPE_STOP: 797 log_framework(LOG_DEBUG, "Stopping %s.\n", v->gv_name); 798 assert(v->gv_state == RESTARTER_STATE_DEGRADED || 799 v->gv_state == RESTARTER_STATE_ONLINE); 800 break; 801 802 case RESTARTER_EVENT_TYPE_START: 803 log_framework(LOG_DEBUG, "Starting %s.\n", v->gv_name); 804 assert(v->gv_state == RESTARTER_STATE_OFFLINE); 805 break; 806 807 case RESTARTER_EVENT_TYPE_REMOVE_INSTANCE: 808 case RESTARTER_EVENT_TYPE_ADMIN_DEGRADED: 809 case RESTARTER_EVENT_TYPE_ADMIN_REFRESH: 810 case RESTARTER_EVENT_TYPE_ADMIN_RESTART: 811 case RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF: 812 case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON: 813 case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON_IMMEDIATE: 814 case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE: 815 case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY: 816 break; 817 818 default: 819 #ifndef NDEBUG 820 uu_warn("%s:%d: Bad event %d.\n", __FILE__, __LINE__, e); 821 #endif 822 abort(); 823 } 824 825 restarter_protocol_send_event(v->gv_name, v->gv_restarter_channel, e); 826 } 827 828 static void 829 graph_unset_restarter(graph_vertex_t *v) 830 { 831 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 832 assert(v->gv_flags & GV_CONFIGURED); 833 834 vertex_send_event(v, RESTARTER_EVENT_TYPE_REMOVE_INSTANCE); 835 836 if (v->gv_restarter_id != -1) { 837 graph_vertex_t *rv; 838 839 rv = vertex_get_by_id(v->gv_restarter_id); 840 graph_remove_edge(v, rv); 841 } 842 843 v->gv_restarter_id = -1; 844 v->gv_restarter_channel = NULL; 845 } 846 847 /* 848 * Return VERTEX_REMOVED when the vertex passed in argument is deleted from the 849 * dgraph otherwise return VERTEX_INUSE. 850 */ 851 static int 852 free_if_unrefed(graph_vertex_t *v) 853 { 854 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 855 856 if (v->gv_refs > 0) 857 return (VERTEX_INUSE); 858 859 if (v->gv_type == GVT_SVC && 860 uu_list_numnodes(v->gv_dependents) == 0 && 861 uu_list_numnodes(v->gv_dependencies) == 0) { 862 graph_remove_vertex(v); 863 return (VERTEX_REMOVED); 864 } else if (v->gv_type == GVT_INST && 865 (v->gv_flags & GV_CONFIGURED) == 0 && 866 uu_list_numnodes(v->gv_dependents) == 1 && 867 uu_list_numnodes(v->gv_dependencies) == 0) { 868 remove_inst_vertex(v); 869 return (VERTEX_REMOVED); 870 } 871 872 return (VERTEX_INUSE); 873 } 874 875 static void 876 delete_depgroup(graph_vertex_t *v) 877 { 878 graph_edge_t *e; 879 graph_vertex_t *dv; 880 881 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 882 assert(v->gv_type == GVT_GROUP); 883 assert(uu_list_numnodes(v->gv_dependents) == 0); 884 885 while ((e = uu_list_first(v->gv_dependencies)) != NULL) { 886 dv = e->ge_vertex; 887 888 graph_remove_edge(v, dv); 889 890 switch (dv->gv_type) { 891 case GVT_INST: /* instance dependency */ 892 case GVT_SVC: /* service dependency */ 893 (void) free_if_unrefed(dv); 894 break; 895 896 case GVT_FILE: /* file dependency */ 897 assert(uu_list_numnodes(dv->gv_dependencies) == 0); 898 if (uu_list_numnodes(dv->gv_dependents) == 0) 899 graph_remove_vertex(dv); 900 break; 901 902 default: 903 #ifndef NDEBUG 904 uu_warn("%s:%d: Unexpected node type %d", __FILE__, 905 __LINE__, dv->gv_type); 906 #endif 907 abort(); 908 } 909 } 910 911 graph_remove_vertex(v); 912 } 913 914 static int 915 delete_instance_deps_cb(graph_edge_t *e, void **ptrs) 916 { 917 graph_vertex_t *v = ptrs[0]; 918 boolean_t delete_restarter_dep = (boolean_t)ptrs[1]; 919 graph_vertex_t *dv; 920 921 dv = e->ge_vertex; 922 923 /* 924 * We have four possibilities here: 925 * - GVT_INST: restarter 926 * - GVT_GROUP - GVT_INST: instance dependency 927 * - GVT_GROUP - GVT_SVC - GV_INST: service dependency 928 * - GVT_GROUP - GVT_FILE: file dependency 929 */ 930 switch (dv->gv_type) { 931 case GVT_INST: /* restarter */ 932 assert(dv->gv_id == v->gv_restarter_id); 933 if (delete_restarter_dep) 934 graph_remove_edge(v, dv); 935 break; 936 937 case GVT_GROUP: /* pg dependency */ 938 graph_remove_edge(v, dv); 939 delete_depgroup(dv); 940 break; 941 942 case GVT_FILE: 943 /* These are currently not direct dependencies */ 944 945 default: 946 #ifndef NDEBUG 947 uu_warn("%s:%d: Bad vertex type %d.\n", __FILE__, __LINE__, 948 dv->gv_type); 949 #endif 950 abort(); 951 } 952 953 return (UU_WALK_NEXT); 954 } 955 956 static void 957 delete_instance_dependencies(graph_vertex_t *v, boolean_t delete_restarter_dep) 958 { 959 void *ptrs[2]; 960 int r; 961 962 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 963 assert(v->gv_type == GVT_INST); 964 965 ptrs[0] = v; 966 ptrs[1] = (void *)delete_restarter_dep; 967 968 r = uu_list_walk(v->gv_dependencies, 969 (uu_walk_fn_t *)delete_instance_deps_cb, &ptrs, UU_WALK_ROBUST); 970 assert(r == 0); 971 } 972 973 /* 974 * int graph_insert_vertex_unconfigured() 975 * Insert a vertex without sending any restarter events. If the vertex 976 * already exists or creation is successful, return a pointer to it in *vp. 977 * 978 * If type is not GVT_GROUP, dt can remain unset. 979 * 980 * Returns 0, EEXIST, or EINVAL if the arguments are invalid (i.e., fmri 981 * doesn't agree with type, or type doesn't agree with dt). 982 */ 983 static int 984 graph_insert_vertex_unconfigured(const char *fmri, gv_type_t type, 985 depgroup_type_t dt, restarter_error_t rt, graph_vertex_t **vp) 986 { 987 int r; 988 int i; 989 990 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 991 992 switch (type) { 993 case GVT_SVC: 994 case GVT_INST: 995 if (strncmp(fmri, "svc:", sizeof ("svc:") - 1) != 0) 996 return (EINVAL); 997 break; 998 999 case GVT_FILE: 1000 if (strncmp(fmri, "file:", sizeof ("file:") - 1) != 0) 1001 return (EINVAL); 1002 break; 1003 1004 case GVT_GROUP: 1005 if (dt <= 0 || rt < 0) 1006 return (EINVAL); 1007 break; 1008 1009 default: 1010 #ifndef NDEBUG 1011 uu_warn("%s:%d: Unknown type %d.\n", __FILE__, __LINE__, type); 1012 #endif 1013 abort(); 1014 } 1015 1016 *vp = vertex_get_by_name(fmri); 1017 if (*vp != NULL) 1018 return (EEXIST); 1019 1020 *vp = graph_add_vertex(fmri); 1021 1022 (*vp)->gv_type = type; 1023 (*vp)->gv_depgroup = dt; 1024 (*vp)->gv_restart = rt; 1025 1026 (*vp)->gv_flags = 0; 1027 (*vp)->gv_state = RESTARTER_STATE_NONE; 1028 1029 for (i = 0; special_vertices[i].name != NULL; ++i) { 1030 if (strcmp(fmri, special_vertices[i].name) == 0) { 1031 (*vp)->gv_start_f = special_vertices[i].start_f; 1032 (*vp)->gv_post_online_f = 1033 special_vertices[i].post_online_f; 1034 (*vp)->gv_post_disable_f = 1035 special_vertices[i].post_disable_f; 1036 break; 1037 } 1038 } 1039 1040 (*vp)->gv_restarter_id = -1; 1041 (*vp)->gv_restarter_channel = 0; 1042 1043 if (type == GVT_INST) { 1044 char *sfmri; 1045 graph_vertex_t *sv; 1046 1047 sfmri = inst_fmri_to_svc_fmri(fmri); 1048 sv = vertex_get_by_name(sfmri); 1049 if (sv == NULL) { 1050 r = graph_insert_vertex_unconfigured(sfmri, GVT_SVC, 0, 1051 0, &sv); 1052 assert(r == 0); 1053 } 1054 startd_free(sfmri, max_scf_fmri_size); 1055 1056 graph_add_edge(sv, *vp); 1057 } 1058 1059 /* 1060 * If this vertex is in the subgraph, mark it as so, for both 1061 * GVT_INST and GVT_SERVICE verteces. 1062 * A GVT_SERVICE vertex can only be in the subgraph if another instance 1063 * depends on it, in which case it's already been added to the graph 1064 * and marked as in the subgraph (by refresh_vertex()). If a 1065 * GVT_SERVICE vertex was freshly added (by the code above), it means 1066 * that it has no dependents, and cannot be in the subgraph. 1067 * Regardless of this, we still check that gv_flags includes 1068 * GV_INSUBGRAPH in the event that future behavior causes the above 1069 * code to add a GVT_SERVICE vertex which should be in the subgraph. 1070 */ 1071 1072 (*vp)->gv_flags |= (should_be_in_subgraph(*vp)? GV_INSUBGRAPH : 0); 1073 1074 return (0); 1075 } 1076 1077 /* 1078 * Returns 0 on success or ELOOP if the dependency would create a cycle. 1079 */ 1080 static int 1081 graph_insert_dependency(graph_vertex_t *fv, graph_vertex_t *tv, int **pathp) 1082 { 1083 hrtime_t now; 1084 1085 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 1086 1087 /* cycle detection */ 1088 now = gethrtime(); 1089 1090 /* Don't follow exclusions. */ 1091 if (!(fv->gv_type == GVT_GROUP && 1092 fv->gv_depgroup == DEPGRP_EXCLUDE_ALL)) { 1093 *pathp = is_path_to(tv, fv); 1094 if (*pathp) 1095 return (ELOOP); 1096 } 1097 1098 dep_cycle_ns += gethrtime() - now; 1099 ++dep_inserts; 1100 now = gethrtime(); 1101 1102 graph_add_edge(fv, tv); 1103 1104 dep_insert_ns += gethrtime() - now; 1105 1106 /* Check if the dependency adds the "to" vertex to the subgraph */ 1107 tv->gv_flags |= (should_be_in_subgraph(tv) ? GV_INSUBGRAPH : 0); 1108 1109 return (0); 1110 } 1111 1112 static int 1113 inst_running(graph_vertex_t *v) 1114 { 1115 assert(v->gv_type == GVT_INST); 1116 1117 if (v->gv_state == RESTARTER_STATE_ONLINE || 1118 v->gv_state == RESTARTER_STATE_DEGRADED) 1119 return (1); 1120 1121 return (0); 1122 } 1123 1124 /* 1125 * The dependency evaluation functions return 1126 * 1 - dependency satisfied 1127 * 0 - dependency unsatisfied 1128 * -1 - dependency unsatisfiable (without administrator intervention) 1129 * 1130 * The functions also take a boolean satbility argument. When true, the 1131 * functions may recurse in order to determine satisfiability. 1132 */ 1133 static int require_any_satisfied(graph_vertex_t *, boolean_t); 1134 static int dependency_satisfied(graph_vertex_t *, boolean_t); 1135 1136 /* 1137 * A require_all dependency is unsatisfied if any elements are unsatisfied. It 1138 * is unsatisfiable if any elements are unsatisfiable. 1139 */ 1140 static int 1141 require_all_satisfied(graph_vertex_t *groupv, boolean_t satbility) 1142 { 1143 graph_edge_t *edge; 1144 int i; 1145 boolean_t any_unsatisfied; 1146 1147 if (uu_list_numnodes(groupv->gv_dependencies) == 0) 1148 return (1); 1149 1150 any_unsatisfied = B_FALSE; 1151 1152 for (edge = uu_list_first(groupv->gv_dependencies); 1153 edge != NULL; 1154 edge = uu_list_next(groupv->gv_dependencies, edge)) { 1155 i = dependency_satisfied(edge->ge_vertex, satbility); 1156 if (i == 1) 1157 continue; 1158 1159 log_framework(LOG_DEBUG, 1160 "require_all(%s): %s is unsatisfi%s.\n", groupv->gv_name, 1161 edge->ge_vertex->gv_name, i == 0 ? "ed" : "able"); 1162 1163 if (!satbility) 1164 return (0); 1165 1166 if (i == -1) 1167 return (-1); 1168 1169 any_unsatisfied = B_TRUE; 1170 } 1171 1172 return (any_unsatisfied ? 0 : 1); 1173 } 1174 1175 /* 1176 * A require_any dependency is satisfied if any element is satisfied. It is 1177 * satisfiable if any element is satisfiable. 1178 */ 1179 static int 1180 require_any_satisfied(graph_vertex_t *groupv, boolean_t satbility) 1181 { 1182 graph_edge_t *edge; 1183 int s; 1184 boolean_t satisfiable; 1185 1186 if (uu_list_numnodes(groupv->gv_dependencies) == 0) 1187 return (1); 1188 1189 satisfiable = B_FALSE; 1190 1191 for (edge = uu_list_first(groupv->gv_dependencies); 1192 edge != NULL; 1193 edge = uu_list_next(groupv->gv_dependencies, edge)) { 1194 s = dependency_satisfied(edge->ge_vertex, satbility); 1195 1196 if (s == 1) 1197 return (1); 1198 1199 log_framework(LOG_DEBUG, 1200 "require_any(%s): %s is unsatisfi%s.\n", 1201 groupv->gv_name, edge->ge_vertex->gv_name, 1202 s == 0 ? "ed" : "able"); 1203 1204 if (satbility && s == 0) 1205 satisfiable = B_TRUE; 1206 } 1207 1208 return (!satbility || satisfiable ? 0 : -1); 1209 } 1210 1211 /* 1212 * An optional_all dependency only considers elements which are configured, 1213 * enabled, and not in maintenance. If any are unsatisfied, then the dependency 1214 * is unsatisfied. 1215 * 1216 * Offline dependencies which are waiting for a dependency to come online are 1217 * unsatisfied. Offline dependences which cannot possibly come online 1218 * (unsatisfiable) are always considered satisfied. 1219 */ 1220 static int 1221 optional_all_satisfied(graph_vertex_t *groupv, boolean_t satbility) 1222 { 1223 graph_edge_t *edge; 1224 graph_vertex_t *v; 1225 boolean_t any_qualified; 1226 boolean_t any_unsatisfied; 1227 int i; 1228 1229 any_qualified = B_FALSE; 1230 any_unsatisfied = B_FALSE; 1231 1232 for (edge = uu_list_first(groupv->gv_dependencies); 1233 edge != NULL; 1234 edge = uu_list_next(groupv->gv_dependencies, edge)) { 1235 v = edge->ge_vertex; 1236 1237 switch (v->gv_type) { 1238 case GVT_INST: 1239 /* Skip missing or disabled instances */ 1240 if ((v->gv_flags & (GV_CONFIGURED | GV_ENABLED)) != 1241 (GV_CONFIGURED | GV_ENABLED)) 1242 continue; 1243 1244 if (v->gv_state == RESTARTER_STATE_MAINT) 1245 continue; 1246 1247 any_qualified = B_TRUE; 1248 if (v->gv_state == RESTARTER_STATE_OFFLINE) { 1249 /* 1250 * For offline dependencies, treat unsatisfiable 1251 * as satisfied. 1252 */ 1253 i = dependency_satisfied(v, B_TRUE); 1254 if (i == -1) 1255 i = 1; 1256 } else if (v->gv_state == RESTARTER_STATE_DISABLED) { 1257 /* 1258 * The service is enabled, but hasn't 1259 * transitioned out of disabled yet. Treat it 1260 * as unsatisfied (not unsatisfiable). 1261 */ 1262 i = 0; 1263 } else { 1264 i = dependency_satisfied(v, satbility); 1265 } 1266 break; 1267 1268 case GVT_FILE: 1269 any_qualified = B_TRUE; 1270 i = dependency_satisfied(v, satbility); 1271 1272 break; 1273 1274 case GVT_SVC: { 1275 boolean_t svc_any_qualified; 1276 boolean_t svc_satisfied; 1277 boolean_t svc_satisfiable; 1278 graph_vertex_t *v2; 1279 graph_edge_t *e2; 1280 1281 svc_any_qualified = B_FALSE; 1282 svc_satisfied = B_FALSE; 1283 svc_satisfiable = B_FALSE; 1284 1285 for (e2 = uu_list_first(v->gv_dependencies); 1286 e2 != NULL; 1287 e2 = uu_list_next(v->gv_dependencies, e2)) { 1288 v2 = e2->ge_vertex; 1289 assert(v2->gv_type == GVT_INST); 1290 1291 if ((v2->gv_flags & 1292 (GV_CONFIGURED | GV_ENABLED)) != 1293 (GV_CONFIGURED | GV_ENABLED)) 1294 continue; 1295 1296 if (v2->gv_state == RESTARTER_STATE_MAINT) 1297 continue; 1298 1299 svc_any_qualified = B_TRUE; 1300 1301 if (v2->gv_state == RESTARTER_STATE_OFFLINE) { 1302 /* 1303 * For offline dependencies, treat 1304 * unsatisfiable as satisfied. 1305 */ 1306 i = dependency_satisfied(v2, B_TRUE); 1307 if (i == -1) 1308 i = 1; 1309 } else if (v2->gv_state == 1310 RESTARTER_STATE_DISABLED) { 1311 i = 0; 1312 } else { 1313 i = dependency_satisfied(v2, satbility); 1314 } 1315 1316 if (i == 1) { 1317 svc_satisfied = B_TRUE; 1318 break; 1319 } 1320 if (i == 0) 1321 svc_satisfiable = B_TRUE; 1322 } 1323 1324 if (!svc_any_qualified) 1325 continue; 1326 any_qualified = B_TRUE; 1327 if (svc_satisfied) { 1328 i = 1; 1329 } else if (svc_satisfiable) { 1330 i = 0; 1331 } else { 1332 i = -1; 1333 } 1334 break; 1335 } 1336 1337 case GVT_GROUP: 1338 default: 1339 #ifndef NDEBUG 1340 uu_warn("%s:%d: Unexpected vertex type %d.\n", __FILE__, 1341 __LINE__, v->gv_type); 1342 #endif 1343 abort(); 1344 } 1345 1346 if (i == 1) 1347 continue; 1348 1349 log_framework(LOG_DEBUG, 1350 "optional_all(%s): %s is unsatisfi%s.\n", groupv->gv_name, 1351 v->gv_name, i == 0 ? "ed" : "able"); 1352 1353 if (!satbility) 1354 return (0); 1355 if (i == -1) 1356 return (-1); 1357 any_unsatisfied = B_TRUE; 1358 } 1359 1360 if (!any_qualified) 1361 return (1); 1362 1363 return (any_unsatisfied ? 0 : 1); 1364 } 1365 1366 /* 1367 * An exclude_all dependency is unsatisfied if any non-service element is 1368 * satisfied or any service instance which is configured, enabled, and not in 1369 * maintenance is satisfied. Usually when unsatisfied, it is also 1370 * unsatisfiable. 1371 */ 1372 #define LOG_EXCLUDE(u, v) \ 1373 log_framework(LOG_DEBUG, "exclude_all(%s): %s is satisfied.\n", \ 1374 (u)->gv_name, (v)->gv_name) 1375 1376 /* ARGSUSED */ 1377 static int 1378 exclude_all_satisfied(graph_vertex_t *groupv, boolean_t satbility) 1379 { 1380 graph_edge_t *edge, *e2; 1381 graph_vertex_t *v, *v2; 1382 1383 for (edge = uu_list_first(groupv->gv_dependencies); 1384 edge != NULL; 1385 edge = uu_list_next(groupv->gv_dependencies, edge)) { 1386 v = edge->ge_vertex; 1387 1388 switch (v->gv_type) { 1389 case GVT_INST: 1390 if ((v->gv_flags & GV_CONFIGURED) == 0) 1391 continue; 1392 1393 switch (v->gv_state) { 1394 case RESTARTER_STATE_ONLINE: 1395 case RESTARTER_STATE_DEGRADED: 1396 LOG_EXCLUDE(groupv, v); 1397 return (v->gv_flags & GV_ENABLED ? -1 : 0); 1398 1399 case RESTARTER_STATE_OFFLINE: 1400 case RESTARTER_STATE_UNINIT: 1401 LOG_EXCLUDE(groupv, v); 1402 return (0); 1403 1404 case RESTARTER_STATE_DISABLED: 1405 case RESTARTER_STATE_MAINT: 1406 continue; 1407 1408 default: 1409 #ifndef NDEBUG 1410 uu_warn("%s:%d: Unexpected vertex state %d.\n", 1411 __FILE__, __LINE__, v->gv_state); 1412 #endif 1413 abort(); 1414 } 1415 /* NOTREACHED */ 1416 1417 case GVT_SVC: 1418 break; 1419 1420 case GVT_FILE: 1421 if (!file_ready(v)) 1422 continue; 1423 LOG_EXCLUDE(groupv, v); 1424 return (-1); 1425 1426 case GVT_GROUP: 1427 default: 1428 #ifndef NDEBUG 1429 uu_warn("%s:%d: Unexpected vertex type %d.\n", __FILE__, 1430 __LINE__, v->gv_type); 1431 #endif 1432 abort(); 1433 } 1434 1435 /* v represents a service */ 1436 if (uu_list_numnodes(v->gv_dependencies) == 0) 1437 continue; 1438 1439 for (e2 = uu_list_first(v->gv_dependencies); 1440 e2 != NULL; 1441 e2 = uu_list_next(v->gv_dependencies, e2)) { 1442 v2 = e2->ge_vertex; 1443 assert(v2->gv_type == GVT_INST); 1444 1445 if ((v2->gv_flags & GV_CONFIGURED) == 0) 1446 continue; 1447 1448 switch (v2->gv_state) { 1449 case RESTARTER_STATE_ONLINE: 1450 case RESTARTER_STATE_DEGRADED: 1451 LOG_EXCLUDE(groupv, v2); 1452 return (v2->gv_flags & GV_ENABLED ? -1 : 0); 1453 1454 case RESTARTER_STATE_OFFLINE: 1455 case RESTARTER_STATE_UNINIT: 1456 LOG_EXCLUDE(groupv, v2); 1457 return (0); 1458 1459 case RESTARTER_STATE_DISABLED: 1460 case RESTARTER_STATE_MAINT: 1461 continue; 1462 1463 default: 1464 #ifndef NDEBUG 1465 uu_warn("%s:%d: Unexpected vertex type %d.\n", 1466 __FILE__, __LINE__, v2->gv_type); 1467 #endif 1468 abort(); 1469 } 1470 } 1471 } 1472 1473 return (1); 1474 } 1475 1476 /* 1477 * int instance_satisfied() 1478 * Determine if all the dependencies are satisfied for the supplied instance 1479 * vertex. Return 1 if they are, 0 if they aren't, and -1 if they won't be 1480 * without administrator intervention. 1481 */ 1482 static int 1483 instance_satisfied(graph_vertex_t *v, boolean_t satbility) 1484 { 1485 assert(v->gv_type == GVT_INST); 1486 assert(!inst_running(v)); 1487 1488 return (require_all_satisfied(v, satbility)); 1489 } 1490 1491 /* 1492 * Decide whether v can satisfy a dependency. v can either be a child of 1493 * a group vertex, or of an instance vertex. 1494 */ 1495 static int 1496 dependency_satisfied(graph_vertex_t *v, boolean_t satbility) 1497 { 1498 switch (v->gv_type) { 1499 case GVT_INST: 1500 if ((v->gv_flags & GV_CONFIGURED) == 0) 1501 return (-1); 1502 1503 switch (v->gv_state) { 1504 case RESTARTER_STATE_ONLINE: 1505 case RESTARTER_STATE_DEGRADED: 1506 return (1); 1507 1508 case RESTARTER_STATE_OFFLINE: 1509 if (!satbility) 1510 return (0); 1511 return (instance_satisfied(v, satbility) != -1 ? 1512 0 : -1); 1513 1514 case RESTARTER_STATE_DISABLED: 1515 case RESTARTER_STATE_MAINT: 1516 return (-1); 1517 1518 case RESTARTER_STATE_UNINIT: 1519 return (0); 1520 1521 default: 1522 #ifndef NDEBUG 1523 uu_warn("%s:%d: Unexpected vertex state %d.\n", 1524 __FILE__, __LINE__, v->gv_state); 1525 #endif 1526 abort(); 1527 /* NOTREACHED */ 1528 } 1529 1530 case GVT_SVC: 1531 if (uu_list_numnodes(v->gv_dependencies) == 0) 1532 return (-1); 1533 return (require_any_satisfied(v, satbility)); 1534 1535 case GVT_FILE: 1536 /* i.e., we assume files will not be automatically generated */ 1537 return (file_ready(v) ? 1 : -1); 1538 1539 case GVT_GROUP: 1540 break; 1541 1542 default: 1543 #ifndef NDEBUG 1544 uu_warn("%s:%d: Unexpected node type %d.\n", __FILE__, __LINE__, 1545 v->gv_type); 1546 #endif 1547 abort(); 1548 /* NOTREACHED */ 1549 } 1550 1551 switch (v->gv_depgroup) { 1552 case DEPGRP_REQUIRE_ANY: 1553 return (require_any_satisfied(v, satbility)); 1554 1555 case DEPGRP_REQUIRE_ALL: 1556 return (require_all_satisfied(v, satbility)); 1557 1558 case DEPGRP_OPTIONAL_ALL: 1559 return (optional_all_satisfied(v, satbility)); 1560 1561 case DEPGRP_EXCLUDE_ALL: 1562 return (exclude_all_satisfied(v, satbility)); 1563 1564 default: 1565 #ifndef NDEBUG 1566 uu_warn("%s:%d: Unknown dependency grouping %d.\n", __FILE__, 1567 __LINE__, v->gv_depgroup); 1568 #endif 1569 abort(); 1570 } 1571 } 1572 1573 void 1574 graph_start_if_satisfied(graph_vertex_t *v) 1575 { 1576 if (v->gv_state == RESTARTER_STATE_OFFLINE && 1577 instance_satisfied(v, B_FALSE) == 1) { 1578 if (v->gv_start_f == NULL) 1579 vertex_send_event(v, RESTARTER_EVENT_TYPE_START); 1580 else 1581 v->gv_start_f(v); 1582 } 1583 } 1584 1585 /* 1586 * propagate_satbility() 1587 * 1588 * This function is used when the given vertex changes state in such a way that 1589 * one of its dependents may become unsatisfiable. This happens when an 1590 * instance transitions between offline -> online, or from !running -> 1591 * maintenance, as well as when an instance is removed from the graph. 1592 * 1593 * We have to walk all the dependents, since optional_all dependencies several 1594 * levels up could become (un)satisfied, instead of unsatisfiable. For example, 1595 * 1596 * +-----+ optional_all +-----+ require_all +-----+ 1597 * | A |--------------->| B |-------------->| C | 1598 * +-----+ +-----+ +-----+ 1599 * 1600 * offline -> maintenance 1601 * 1602 * If C goes into maintenance, it's not enough simply to check B. Because A has 1603 * an optional dependency, what was previously an unsatisfiable situation is now 1604 * satisfied (B will never come online, even though its state hasn't changed). 1605 * 1606 * Note that it's not necessary to continue examining dependents after reaching 1607 * an optional_all dependency. It's not possible for an optional_all dependency 1608 * to change satisfiability without also coming online, in which case we get a 1609 * start event and propagation continues naturally. However, it does no harm to 1610 * continue propagating satisfiability (as it is a relatively rare event), and 1611 * keeps the walker code simple and generic. 1612 */ 1613 /*ARGSUSED*/ 1614 static int 1615 satbility_cb(graph_vertex_t *v, void *arg) 1616 { 1617 if (v->gv_type == GVT_INST) 1618 graph_start_if_satisfied(v); 1619 1620 return (UU_WALK_NEXT); 1621 } 1622 1623 static void 1624 propagate_satbility(graph_vertex_t *v) 1625 { 1626 graph_walk(v, WALK_DEPENDENTS, satbility_cb, NULL, NULL); 1627 } 1628 1629 static void propagate_stop(graph_vertex_t *, void *); 1630 1631 /* ARGSUSED */ 1632 static void 1633 propagate_start(graph_vertex_t *v, void *arg) 1634 { 1635 switch (v->gv_type) { 1636 case GVT_INST: 1637 graph_start_if_satisfied(v); 1638 break; 1639 1640 case GVT_GROUP: 1641 if (v->gv_depgroup == DEPGRP_EXCLUDE_ALL) { 1642 graph_walk_dependents(v, propagate_stop, 1643 (void *)RERR_RESTART); 1644 break; 1645 } 1646 /* FALLTHROUGH */ 1647 1648 case GVT_SVC: 1649 graph_walk_dependents(v, propagate_start, NULL); 1650 break; 1651 1652 case GVT_FILE: 1653 #ifndef NDEBUG 1654 uu_warn("%s:%d: propagate_start() encountered GVT_FILE.\n", 1655 __FILE__, __LINE__); 1656 #endif 1657 abort(); 1658 /* NOTREACHED */ 1659 1660 default: 1661 #ifndef NDEBUG 1662 uu_warn("%s:%d: Unknown vertex type %d.\n", __FILE__, __LINE__, 1663 v->gv_type); 1664 #endif 1665 abort(); 1666 } 1667 } 1668 1669 static void 1670 propagate_stop(graph_vertex_t *v, void *arg) 1671 { 1672 graph_edge_t *e; 1673 graph_vertex_t *svc; 1674 restarter_error_t err = (restarter_error_t)arg; 1675 1676 switch (v->gv_type) { 1677 case GVT_INST: 1678 /* Restarter */ 1679 if (err > RERR_NONE && inst_running(v)) 1680 vertex_send_event(v, RESTARTER_EVENT_TYPE_STOP); 1681 break; 1682 1683 case GVT_SVC: 1684 graph_walk_dependents(v, propagate_stop, arg); 1685 break; 1686 1687 case GVT_FILE: 1688 #ifndef NDEBUG 1689 uu_warn("%s:%d: propagate_stop() encountered GVT_FILE.\n", 1690 __FILE__, __LINE__); 1691 #endif 1692 abort(); 1693 /* NOTREACHED */ 1694 1695 case GVT_GROUP: 1696 if (v->gv_depgroup == DEPGRP_EXCLUDE_ALL) { 1697 graph_walk_dependents(v, propagate_start, NULL); 1698 break; 1699 } 1700 1701 if (err == RERR_NONE || err > v->gv_restart) 1702 break; 1703 1704 assert(uu_list_numnodes(v->gv_dependents) == 1); 1705 e = uu_list_first(v->gv_dependents); 1706 svc = e->ge_vertex; 1707 1708 if (inst_running(svc)) 1709 vertex_send_event(svc, RESTARTER_EVENT_TYPE_STOP); 1710 break; 1711 1712 default: 1713 #ifndef NDEBUG 1714 uu_warn("%s:%d: Unknown vertex type %d.\n", __FILE__, __LINE__, 1715 v->gv_type); 1716 #endif 1717 abort(); 1718 } 1719 } 1720 1721 /* 1722 * void graph_enable_by_vertex() 1723 * If admin is non-zero, this is an administrative request for change 1724 * of the enabled property. Thus, send the ADMIN_DISABLE rather than 1725 * a plain DISABLE restarter event. 1726 */ 1727 void 1728 graph_enable_by_vertex(graph_vertex_t *vertex, int enable, int admin) 1729 { 1730 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 1731 assert((vertex->gv_flags & GV_CONFIGURED)); 1732 1733 vertex->gv_flags = (vertex->gv_flags & ~GV_ENABLED) | 1734 (enable ? GV_ENABLED : 0); 1735 1736 if (enable) { 1737 if (vertex->gv_state != RESTARTER_STATE_OFFLINE && 1738 vertex->gv_state != RESTARTER_STATE_DEGRADED && 1739 vertex->gv_state != RESTARTER_STATE_ONLINE) 1740 vertex_send_event(vertex, RESTARTER_EVENT_TYPE_ENABLE); 1741 } else { 1742 if (vertex->gv_state != RESTARTER_STATE_DISABLED) { 1743 if (admin) 1744 vertex_send_event(vertex, 1745 RESTARTER_EVENT_TYPE_ADMIN_DISABLE); 1746 else 1747 vertex_send_event(vertex, 1748 RESTARTER_EVENT_TYPE_DISABLE); 1749 } 1750 } 1751 1752 /* 1753 * Wait for state update from restarter before sending _START or 1754 * _STOP. 1755 */ 1756 } 1757 1758 static int configure_vertex(graph_vertex_t *, scf_instance_t *); 1759 1760 /* 1761 * Set the restarter for v to fmri_arg. That is, make sure a vertex for 1762 * fmri_arg exists, make v depend on it, and send _ADD_INSTANCE for v. If 1763 * v is already configured and fmri_arg indicates the current restarter, do 1764 * nothing. If v is configured and fmri_arg is a new restarter, delete v's 1765 * dependency on the restarter, send _REMOVE_INSTANCE for v, and set the new 1766 * restarter. Returns 0 on success, EINVAL if the FMRI is invalid, 1767 * ECONNABORTED if the repository connection is broken, and ELOOP 1768 * if the dependency would create a cycle. In the last case, *pathp will 1769 * point to a -1-terminated array of ids which compose the path from v to 1770 * restarter_fmri. 1771 */ 1772 int 1773 graph_change_restarter(graph_vertex_t *v, const char *fmri_arg, scf_handle_t *h, 1774 int **pathp) 1775 { 1776 char *restarter_fmri = NULL; 1777 graph_vertex_t *rv; 1778 int err; 1779 int id; 1780 1781 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 1782 1783 if (fmri_arg[0] != '\0') { 1784 err = fmri_canonify(fmri_arg, &restarter_fmri, B_TRUE); 1785 if (err != 0) { 1786 assert(err == EINVAL); 1787 return (err); 1788 } 1789 } 1790 1791 if (restarter_fmri == NULL || 1792 strcmp(restarter_fmri, SCF_SERVICE_STARTD) == 0) { 1793 if (v->gv_flags & GV_CONFIGURED) { 1794 if (v->gv_restarter_id == -1) { 1795 if (restarter_fmri != NULL) 1796 startd_free(restarter_fmri, 1797 max_scf_fmri_size); 1798 return (0); 1799 } 1800 1801 graph_unset_restarter(v); 1802 } 1803 1804 /* Master restarter, nothing to do. */ 1805 v->gv_restarter_id = -1; 1806 v->gv_restarter_channel = NULL; 1807 vertex_send_event(v, RESTARTER_EVENT_TYPE_ADD_INSTANCE); 1808 return (0); 1809 } 1810 1811 if (v->gv_flags & GV_CONFIGURED) { 1812 id = dict_lookup_byname(restarter_fmri); 1813 if (id != -1 && v->gv_restarter_id == id) { 1814 startd_free(restarter_fmri, max_scf_fmri_size); 1815 return (0); 1816 } 1817 1818 graph_unset_restarter(v); 1819 } 1820 1821 err = graph_insert_vertex_unconfigured(restarter_fmri, GVT_INST, 0, 1822 RERR_NONE, &rv); 1823 startd_free(restarter_fmri, max_scf_fmri_size); 1824 assert(err == 0 || err == EEXIST); 1825 1826 if (rv->gv_delegate_initialized == 0) { 1827 rv->gv_delegate_channel = restarter_protocol_init_delegate( 1828 rv->gv_name); 1829 rv->gv_delegate_initialized = 1; 1830 } 1831 v->gv_restarter_id = rv->gv_id; 1832 v->gv_restarter_channel = rv->gv_delegate_channel; 1833 1834 err = graph_insert_dependency(v, rv, pathp); 1835 if (err != 0) { 1836 assert(err == ELOOP); 1837 return (ELOOP); 1838 } 1839 1840 vertex_send_event(v, RESTARTER_EVENT_TYPE_ADD_INSTANCE); 1841 1842 if (!(rv->gv_flags & GV_CONFIGURED)) { 1843 scf_instance_t *inst; 1844 1845 err = libscf_fmri_get_instance(h, rv->gv_name, &inst); 1846 switch (err) { 1847 case 0: 1848 err = configure_vertex(rv, inst); 1849 scf_instance_destroy(inst); 1850 switch (err) { 1851 case 0: 1852 case ECANCELED: 1853 break; 1854 1855 case ECONNABORTED: 1856 return (ECONNABORTED); 1857 1858 default: 1859 bad_error("configure_vertex", err); 1860 } 1861 break; 1862 1863 case ECONNABORTED: 1864 return (ECONNABORTED); 1865 1866 case ENOENT: 1867 break; 1868 1869 case ENOTSUP: 1870 /* 1871 * The fmri doesn't specify an instance - translate 1872 * to EINVAL. 1873 */ 1874 return (EINVAL); 1875 1876 case EINVAL: 1877 default: 1878 bad_error("libscf_fmri_get_instance", err); 1879 } 1880 } 1881 1882 return (0); 1883 } 1884 1885 1886 /* 1887 * Add all of the instances of the service named by fmri to the graph. 1888 * Returns 1889 * 0 - success 1890 * ENOENT - service indicated by fmri does not exist 1891 * 1892 * In both cases *reboundp will be B_TRUE if the handle was rebound, or B_FALSE 1893 * otherwise. 1894 */ 1895 static int 1896 add_service(const char *fmri, scf_handle_t *h, boolean_t *reboundp) 1897 { 1898 scf_service_t *svc; 1899 scf_instance_t *inst; 1900 scf_iter_t *iter; 1901 char *inst_fmri; 1902 int ret, r; 1903 1904 *reboundp = B_FALSE; 1905 1906 svc = safe_scf_service_create(h); 1907 inst = safe_scf_instance_create(h); 1908 iter = safe_scf_iter_create(h); 1909 inst_fmri = startd_alloc(max_scf_fmri_size); 1910 1911 rebound: 1912 if (scf_handle_decode_fmri(h, fmri, NULL, svc, NULL, NULL, NULL, 1913 SCF_DECODE_FMRI_EXACT) != 0) { 1914 switch (scf_error()) { 1915 case SCF_ERROR_CONNECTION_BROKEN: 1916 default: 1917 libscf_handle_rebind(h); 1918 *reboundp = B_TRUE; 1919 goto rebound; 1920 1921 case SCF_ERROR_NOT_FOUND: 1922 ret = ENOENT; 1923 goto out; 1924 1925 case SCF_ERROR_INVALID_ARGUMENT: 1926 case SCF_ERROR_CONSTRAINT_VIOLATED: 1927 case SCF_ERROR_NOT_BOUND: 1928 case SCF_ERROR_HANDLE_MISMATCH: 1929 bad_error("scf_handle_decode_fmri", scf_error()); 1930 } 1931 } 1932 1933 if (scf_iter_service_instances(iter, svc) != 0) { 1934 switch (scf_error()) { 1935 case SCF_ERROR_CONNECTION_BROKEN: 1936 default: 1937 libscf_handle_rebind(h); 1938 *reboundp = B_TRUE; 1939 goto rebound; 1940 1941 case SCF_ERROR_DELETED: 1942 ret = ENOENT; 1943 goto out; 1944 1945 case SCF_ERROR_HANDLE_MISMATCH: 1946 case SCF_ERROR_NOT_BOUND: 1947 case SCF_ERROR_NOT_SET: 1948 bad_error("scf_iter_service_instances", scf_error()) 1949 } 1950 } 1951 1952 for (;;) { 1953 r = scf_iter_next_instance(iter, inst); 1954 if (r == 0) 1955 break; 1956 if (r != 1) { 1957 switch (scf_error()) { 1958 case SCF_ERROR_CONNECTION_BROKEN: 1959 default: 1960 libscf_handle_rebind(h); 1961 *reboundp = B_TRUE; 1962 goto rebound; 1963 1964 case SCF_ERROR_DELETED: 1965 ret = ENOENT; 1966 goto out; 1967 1968 case SCF_ERROR_HANDLE_MISMATCH: 1969 case SCF_ERROR_NOT_BOUND: 1970 case SCF_ERROR_NOT_SET: 1971 case SCF_ERROR_INVALID_ARGUMENT: 1972 bad_error("scf_iter_next_instance", 1973 scf_error()); 1974 } 1975 } 1976 1977 if (scf_instance_to_fmri(inst, inst_fmri, max_scf_fmri_size) < 1978 0) { 1979 switch (scf_error()) { 1980 case SCF_ERROR_CONNECTION_BROKEN: 1981 libscf_handle_rebind(h); 1982 *reboundp = B_TRUE; 1983 goto rebound; 1984 1985 case SCF_ERROR_DELETED: 1986 continue; 1987 1988 case SCF_ERROR_NOT_BOUND: 1989 case SCF_ERROR_NOT_SET: 1990 bad_error("scf_instance_to_fmri", scf_error()); 1991 } 1992 } 1993 1994 r = dgraph_add_instance(inst_fmri, inst, B_FALSE); 1995 switch (r) { 1996 case 0: 1997 case ECANCELED: 1998 break; 1999 2000 case EEXIST: 2001 continue; 2002 2003 case ECONNABORTED: 2004 libscf_handle_rebind(h); 2005 *reboundp = B_TRUE; 2006 goto rebound; 2007 2008 case EINVAL: 2009 default: 2010 bad_error("dgraph_add_instance", r); 2011 } 2012 } 2013 2014 ret = 0; 2015 2016 out: 2017 startd_free(inst_fmri, max_scf_fmri_size); 2018 scf_iter_destroy(iter); 2019 scf_instance_destroy(inst); 2020 scf_service_destroy(svc); 2021 return (ret); 2022 } 2023 2024 struct depfmri_info { 2025 graph_vertex_t *v; /* GVT_GROUP vertex */ 2026 gv_type_t type; /* type of dependency */ 2027 const char *inst_fmri; /* FMRI of parental GVT_INST vert. */ 2028 const char *pg_name; /* Name of dependency pg */ 2029 scf_handle_t *h; 2030 int err; /* return error code */ 2031 int **pathp; /* return circular dependency path */ 2032 }; 2033 2034 /* 2035 * Find or create a vertex for fmri and make info->v depend on it. 2036 * Returns 2037 * 0 - success 2038 * nonzero - failure 2039 * 2040 * On failure, sets info->err to 2041 * EINVAL - fmri is invalid 2042 * fmri does not match info->type 2043 * ELOOP - Adding the dependency creates a circular dependency. *info->pathp 2044 * will point to an array of the ids of the members of the cycle. 2045 * ECONNABORTED - repository connection was broken 2046 * ECONNRESET - succeeded, but repository connection was reset 2047 */ 2048 static int 2049 process_dependency_fmri(const char *fmri, struct depfmri_info *info) 2050 { 2051 int err; 2052 graph_vertex_t *depgroup_v, *v; 2053 char *fmri_copy, *cfmri; 2054 size_t fmri_copy_sz; 2055 const char *scope, *service, *instance, *pg; 2056 scf_instance_t *inst; 2057 boolean_t rebound; 2058 2059 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 2060 2061 /* Get or create vertex for FMRI */ 2062 depgroup_v = info->v; 2063 2064 if (strncmp(fmri, "file:", sizeof ("file:") - 1) == 0) { 2065 if (info->type != GVT_FILE) { 2066 log_framework(LOG_NOTICE, 2067 "FMRI \"%s\" is not allowed for the \"%s\" " 2068 "dependency's type of instance %s.\n", fmri, 2069 info->pg_name, info->inst_fmri); 2070 return (info->err = EINVAL); 2071 } 2072 2073 err = graph_insert_vertex_unconfigured(fmri, info->type, 0, 2074 RERR_NONE, &v); 2075 switch (err) { 2076 case 0: 2077 break; 2078 2079 case EEXIST: 2080 assert(v->gv_type == GVT_FILE); 2081 break; 2082 2083 case EINVAL: /* prevented above */ 2084 default: 2085 bad_error("graph_insert_vertex_unconfigured", err); 2086 } 2087 } else { 2088 if (info->type != GVT_INST) { 2089 log_framework(LOG_NOTICE, 2090 "FMRI \"%s\" is not allowed for the \"%s\" " 2091 "dependency's type of instance %s.\n", fmri, 2092 info->pg_name, info->inst_fmri); 2093 return (info->err = EINVAL); 2094 } 2095 2096 /* 2097 * We must canonify fmri & add a vertex for it. 2098 */ 2099 fmri_copy_sz = strlen(fmri) + 1; 2100 fmri_copy = startd_alloc(fmri_copy_sz); 2101 (void) strcpy(fmri_copy, fmri); 2102 2103 /* Determine if the FMRI is a property group or instance */ 2104 if (scf_parse_svc_fmri(fmri_copy, &scope, &service, 2105 &instance, &pg, NULL) != 0) { 2106 startd_free(fmri_copy, fmri_copy_sz); 2107 log_framework(LOG_NOTICE, 2108 "Dependency \"%s\" of %s has invalid FMRI " 2109 "\"%s\".\n", info->pg_name, info->inst_fmri, 2110 fmri); 2111 return (info->err = EINVAL); 2112 } 2113 2114 if (service == NULL || pg != NULL) { 2115 startd_free(fmri_copy, fmri_copy_sz); 2116 log_framework(LOG_NOTICE, 2117 "Dependency \"%s\" of %s does not designate a " 2118 "service or instance.\n", info->pg_name, 2119 info->inst_fmri); 2120 return (info->err = EINVAL); 2121 } 2122 2123 if (scope == NULL || strcmp(scope, SCF_SCOPE_LOCAL) == 0) { 2124 cfmri = uu_msprintf("svc:/%s%s%s", 2125 service, instance ? ":" : "", instance ? instance : 2126 ""); 2127 } else { 2128 cfmri = uu_msprintf("svc://%s/%s%s%s", 2129 scope, service, instance ? ":" : "", instance ? 2130 instance : ""); 2131 } 2132 2133 startd_free(fmri_copy, fmri_copy_sz); 2134 2135 err = graph_insert_vertex_unconfigured(cfmri, instance ? 2136 GVT_INST : GVT_SVC, instance ? 0 : DEPGRP_REQUIRE_ANY, 2137 RERR_NONE, &v); 2138 uu_free(cfmri); 2139 switch (err) { 2140 case 0: 2141 break; 2142 2143 case EEXIST: 2144 /* Verify v. */ 2145 if (instance != NULL) 2146 assert(v->gv_type == GVT_INST); 2147 else 2148 assert(v->gv_type == GVT_SVC); 2149 break; 2150 2151 default: 2152 bad_error("graph_insert_vertex_unconfigured", err); 2153 } 2154 } 2155 2156 /* Add dependency from depgroup_v to new vertex */ 2157 info->err = graph_insert_dependency(depgroup_v, v, info->pathp); 2158 switch (info->err) { 2159 case 0: 2160 break; 2161 2162 case ELOOP: 2163 return (ELOOP); 2164 2165 default: 2166 bad_error("graph_insert_dependency", info->err); 2167 } 2168 2169 /* This must be after we insert the dependency, to avoid looping. */ 2170 switch (v->gv_type) { 2171 case GVT_INST: 2172 if ((v->gv_flags & GV_CONFIGURED) != 0) 2173 break; 2174 2175 inst = safe_scf_instance_create(info->h); 2176 2177 rebound = B_FALSE; 2178 2179 rebound: 2180 err = libscf_lookup_instance(v->gv_name, inst); 2181 switch (err) { 2182 case 0: 2183 err = configure_vertex(v, inst); 2184 switch (err) { 2185 case 0: 2186 case ECANCELED: 2187 break; 2188 2189 case ECONNABORTED: 2190 libscf_handle_rebind(info->h); 2191 rebound = B_TRUE; 2192 goto rebound; 2193 2194 default: 2195 bad_error("configure_vertex", err); 2196 } 2197 break; 2198 2199 case ENOENT: 2200 break; 2201 2202 case ECONNABORTED: 2203 libscf_handle_rebind(info->h); 2204 rebound = B_TRUE; 2205 goto rebound; 2206 2207 case EINVAL: 2208 case ENOTSUP: 2209 default: 2210 bad_error("libscf_fmri_get_instance", err); 2211 } 2212 2213 scf_instance_destroy(inst); 2214 2215 if (rebound) 2216 return (info->err = ECONNRESET); 2217 break; 2218 2219 case GVT_SVC: 2220 (void) add_service(v->gv_name, info->h, &rebound); 2221 if (rebound) 2222 return (info->err = ECONNRESET); 2223 } 2224 2225 return (0); 2226 } 2227 2228 struct deppg_info { 2229 graph_vertex_t *v; /* GVT_INST vertex */ 2230 int err; /* return error */ 2231 int **pathp; /* return circular dependency path */ 2232 }; 2233 2234 /* 2235 * Make info->v depend on a new GVT_GROUP node for this property group, 2236 * and then call process_dependency_fmri() for the values of the entity 2237 * property. Return 0 on success, or if something goes wrong return nonzero 2238 * and set info->err to ECONNABORTED, EINVAL, or the error code returned by 2239 * process_dependency_fmri(). 2240 */ 2241 static int 2242 process_dependency_pg(scf_propertygroup_t *pg, struct deppg_info *info) 2243 { 2244 scf_handle_t *h; 2245 depgroup_type_t deptype; 2246 restarter_error_t rerr; 2247 struct depfmri_info linfo; 2248 char *fmri, *pg_name; 2249 size_t fmri_sz; 2250 graph_vertex_t *depgrp; 2251 scf_property_t *prop; 2252 int err; 2253 int empty; 2254 scf_error_t scferr; 2255 ssize_t len; 2256 2257 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 2258 2259 h = scf_pg_handle(pg); 2260 2261 pg_name = startd_alloc(max_scf_name_size); 2262 2263 len = scf_pg_get_name(pg, pg_name, max_scf_name_size); 2264 if (len < 0) { 2265 startd_free(pg_name, max_scf_name_size); 2266 switch (scf_error()) { 2267 case SCF_ERROR_CONNECTION_BROKEN: 2268 default: 2269 return (info->err = ECONNABORTED); 2270 2271 case SCF_ERROR_DELETED: 2272 return (info->err = 0); 2273 2274 case SCF_ERROR_NOT_SET: 2275 bad_error("scf_pg_get_name", scf_error()); 2276 } 2277 } 2278 2279 /* 2280 * Skip over empty dependency groups. Since dependency property 2281 * groups are updated atomically, they are either empty or 2282 * fully populated. 2283 */ 2284 empty = depgroup_empty(h, pg); 2285 if (empty < 0) { 2286 log_error(LOG_INFO, 2287 "Error reading dependency group \"%s\" of %s: %s\n", 2288 pg_name, info->v->gv_name, scf_strerror(scf_error())); 2289 startd_free(pg_name, max_scf_name_size); 2290 return (info->err = EINVAL); 2291 2292 } else if (empty == 1) { 2293 log_framework(LOG_DEBUG, 2294 "Ignoring empty dependency group \"%s\" of %s\n", 2295 pg_name, info->v->gv_name); 2296 startd_free(pg_name, max_scf_name_size); 2297 return (info->err = 0); 2298 } 2299 2300 fmri_sz = strlen(info->v->gv_name) + 1 + len + 1; 2301 fmri = startd_alloc(fmri_sz); 2302 2303 (void) snprintf(fmri, max_scf_name_size, "%s>%s", info->v->gv_name, 2304 pg_name); 2305 2306 /* Validate the pg before modifying the graph */ 2307 deptype = depgroup_read_grouping(h, pg); 2308 if (deptype == DEPGRP_UNSUPPORTED) { 2309 log_error(LOG_INFO, 2310 "Dependency \"%s\" of %s has an unknown grouping value.\n", 2311 pg_name, info->v->gv_name); 2312 startd_free(fmri, fmri_sz); 2313 startd_free(pg_name, max_scf_name_size); 2314 return (info->err = EINVAL); 2315 } 2316 2317 rerr = depgroup_read_restart(h, pg); 2318 if (rerr == RERR_UNSUPPORTED) { 2319 log_error(LOG_INFO, 2320 "Dependency \"%s\" of %s has an unknown restart_on value." 2321 "\n", pg_name, info->v->gv_name); 2322 startd_free(fmri, fmri_sz); 2323 startd_free(pg_name, max_scf_name_size); 2324 return (info->err = EINVAL); 2325 } 2326 2327 prop = safe_scf_property_create(h); 2328 2329 if (scf_pg_get_property(pg, SCF_PROPERTY_ENTITIES, prop) != 0) { 2330 scferr = scf_error(); 2331 scf_property_destroy(prop); 2332 if (scferr == SCF_ERROR_DELETED) { 2333 startd_free(fmri, fmri_sz); 2334 startd_free(pg_name, max_scf_name_size); 2335 return (info->err = 0); 2336 } else if (scferr != SCF_ERROR_NOT_FOUND) { 2337 startd_free(fmri, fmri_sz); 2338 startd_free(pg_name, max_scf_name_size); 2339 return (info->err = ECONNABORTED); 2340 } 2341 2342 log_error(LOG_INFO, 2343 "Dependency \"%s\" of %s is missing a \"%s\" property.\n", 2344 pg_name, info->v->gv_name, SCF_PROPERTY_ENTITIES); 2345 2346 startd_free(fmri, fmri_sz); 2347 startd_free(pg_name, max_scf_name_size); 2348 2349 return (info->err = EINVAL); 2350 } 2351 2352 /* Create depgroup vertex for pg */ 2353 err = graph_insert_vertex_unconfigured(fmri, GVT_GROUP, deptype, 2354 rerr, &depgrp); 2355 assert(err == 0); 2356 startd_free(fmri, fmri_sz); 2357 2358 /* Add dependency from inst vertex to new vertex */ 2359 err = graph_insert_dependency(info->v, depgrp, info->pathp); 2360 /* ELOOP can't happen because this should be a new vertex */ 2361 assert(err == 0); 2362 2363 linfo.v = depgrp; 2364 linfo.type = depgroup_read_scheme(h, pg); 2365 linfo.inst_fmri = info->v->gv_name; 2366 linfo.pg_name = pg_name; 2367 linfo.h = h; 2368 linfo.err = 0; 2369 linfo.pathp = info->pathp; 2370 err = walk_property_astrings(prop, (callback_t)process_dependency_fmri, 2371 &linfo); 2372 2373 scf_property_destroy(prop); 2374 startd_free(pg_name, max_scf_name_size); 2375 2376 switch (err) { 2377 case 0: 2378 case EINTR: 2379 return (info->err = linfo.err); 2380 2381 case ECONNABORTED: 2382 case EINVAL: 2383 return (info->err = err); 2384 2385 case ECANCELED: 2386 return (info->err = 0); 2387 2388 case ECONNRESET: 2389 return (info->err = ECONNABORTED); 2390 2391 default: 2392 bad_error("walk_property_astrings", err); 2393 /* NOTREACHED */ 2394 } 2395 } 2396 2397 /* 2398 * Build the dependency info for v from the repository. Returns 0 on success, 2399 * ECONNABORTED on repository disconnection, EINVAL if the repository 2400 * configuration is invalid, and ELOOP if a dependency would cause a cycle. 2401 * In the last case, *pathp will point to a -1-terminated array of ids which 2402 * constitute the rest of the dependency cycle. 2403 */ 2404 static int 2405 set_dependencies(graph_vertex_t *v, scf_instance_t *inst, int **pathp) 2406 { 2407 struct deppg_info info; 2408 int err; 2409 uint_t old_configured; 2410 2411 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 2412 2413 /* 2414 * Mark the vertex as configured during dependency insertion to avoid 2415 * dependency cycles (which can appear in the graph if one of the 2416 * vertices is an exclusion-group). 2417 */ 2418 old_configured = v->gv_flags & GV_CONFIGURED; 2419 v->gv_flags |= GV_CONFIGURED; 2420 2421 info.err = 0; 2422 info.v = v; 2423 info.pathp = pathp; 2424 2425 err = walk_dependency_pgs(inst, (callback_t)process_dependency_pg, 2426 &info); 2427 2428 if (!old_configured) 2429 v->gv_flags &= ~GV_CONFIGURED; 2430 2431 switch (err) { 2432 case 0: 2433 case EINTR: 2434 return (info.err); 2435 2436 case ECONNABORTED: 2437 return (ECONNABORTED); 2438 2439 case ECANCELED: 2440 /* Should get delete event, so return 0. */ 2441 return (0); 2442 2443 default: 2444 bad_error("walk_dependency_pgs", err); 2445 /* NOTREACHED */ 2446 } 2447 } 2448 2449 2450 static void 2451 handle_cycle(const char *fmri, int *path) 2452 { 2453 const char *cp; 2454 size_t sz; 2455 2456 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 2457 2458 path_to_str(path, (char **)&cp, &sz); 2459 2460 log_error(LOG_ERR, "Transitioning %s to maintenance " 2461 "because it completes a dependency cycle (see svcs -xv for " 2462 "details):\n%s", fmri ? fmri : "?", cp); 2463 2464 startd_free((void *)cp, sz); 2465 } 2466 2467 /* 2468 * Increment the vertex's reference count to prevent the vertex removal 2469 * from the dgraph. 2470 */ 2471 static void 2472 vertex_ref(graph_vertex_t *v) 2473 { 2474 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 2475 2476 v->gv_refs++; 2477 } 2478 2479 /* 2480 * Decrement the vertex's reference count and remove the vertex from 2481 * the dgraph when possible. 2482 * 2483 * Return VERTEX_REMOVED when the vertex has been removed otherwise 2484 * return VERTEX_INUSE. 2485 */ 2486 static int 2487 vertex_unref(graph_vertex_t *v) 2488 { 2489 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 2490 assert(v->gv_refs > 0); 2491 2492 v->gv_refs--; 2493 2494 return (free_if_unrefed(v)); 2495 } 2496 2497 /* 2498 * When run on the dependencies of a vertex, populates list with 2499 * graph_edge_t's which point to the service vertices or the instance 2500 * vertices (no GVT_GROUP nodes) on which the vertex depends. 2501 * 2502 * Increment the vertex's reference count once the vertex is inserted 2503 * in the list. The vertex won't be able to be deleted from the dgraph 2504 * while it is referenced. 2505 */ 2506 static int 2507 append_svcs_or_insts(graph_edge_t *e, uu_list_t *list) 2508 { 2509 graph_vertex_t *v = e->ge_vertex; 2510 graph_edge_t *new; 2511 int r; 2512 2513 switch (v->gv_type) { 2514 case GVT_INST: 2515 case GVT_SVC: 2516 break; 2517 2518 case GVT_GROUP: 2519 r = uu_list_walk(v->gv_dependencies, 2520 (uu_walk_fn_t *)append_svcs_or_insts, list, 0); 2521 assert(r == 0); 2522 return (UU_WALK_NEXT); 2523 2524 case GVT_FILE: 2525 return (UU_WALK_NEXT); 2526 2527 default: 2528 #ifndef NDEBUG 2529 uu_warn("%s:%d: Unexpected vertex type %d.\n", __FILE__, 2530 __LINE__, v->gv_type); 2531 #endif 2532 abort(); 2533 } 2534 2535 new = startd_alloc(sizeof (*new)); 2536 new->ge_vertex = v; 2537 uu_list_node_init(new, &new->ge_link, graph_edge_pool); 2538 r = uu_list_insert_before(list, NULL, new); 2539 assert(r == 0); 2540 2541 /* 2542 * Because we are inserting the vertex in a list, we don't want 2543 * the vertex to be freed while the list is in use. In order to 2544 * achieve that, increment the vertex's reference count. 2545 */ 2546 vertex_ref(v); 2547 2548 return (UU_WALK_NEXT); 2549 } 2550 2551 static boolean_t 2552 should_be_in_subgraph(graph_vertex_t *v) 2553 { 2554 graph_edge_t *e; 2555 2556 if (v == milestone) 2557 return (B_TRUE); 2558 2559 /* 2560 * v is in the subgraph if any of its dependents are in the subgraph. 2561 * Except for EXCLUDE_ALL dependents. And OPTIONAL dependents only 2562 * count if we're enabled. 2563 */ 2564 for (e = uu_list_first(v->gv_dependents); 2565 e != NULL; 2566 e = uu_list_next(v->gv_dependents, e)) { 2567 graph_vertex_t *dv = e->ge_vertex; 2568 2569 if (!(dv->gv_flags & GV_INSUBGRAPH)) 2570 continue; 2571 2572 /* 2573 * Don't include instances that are optional and disabled. 2574 */ 2575 if (v->gv_type == GVT_INST && dv->gv_type == GVT_SVC) { 2576 2577 int in = 0; 2578 graph_edge_t *ee; 2579 2580 for (ee = uu_list_first(dv->gv_dependents); 2581 ee != NULL; 2582 ee = uu_list_next(dv->gv_dependents, ee)) { 2583 2584 graph_vertex_t *ddv = e->ge_vertex; 2585 2586 if (ddv->gv_type == GVT_GROUP && 2587 ddv->gv_depgroup == DEPGRP_EXCLUDE_ALL) 2588 continue; 2589 2590 if (ddv->gv_type == GVT_GROUP && 2591 ddv->gv_depgroup == DEPGRP_OPTIONAL_ALL && 2592 !(v->gv_flags & GV_ENBLD_NOOVR)) 2593 continue; 2594 2595 in = 1; 2596 } 2597 if (!in) 2598 continue; 2599 } 2600 if (v->gv_type == GVT_INST && 2601 dv->gv_type == GVT_GROUP && 2602 dv->gv_depgroup == DEPGRP_OPTIONAL_ALL && 2603 !(v->gv_flags & GV_ENBLD_NOOVR)) 2604 continue; 2605 2606 /* Don't include excluded services and instances */ 2607 if (dv->gv_type == GVT_GROUP && 2608 dv->gv_depgroup == DEPGRP_EXCLUDE_ALL) 2609 continue; 2610 2611 return (B_TRUE); 2612 } 2613 2614 return (B_FALSE); 2615 } 2616 2617 /* 2618 * Ensures that GV_INSUBGRAPH is set properly for v and its descendents. If 2619 * any bits change, manipulate the repository appropriately. Returns 0 or 2620 * ECONNABORTED. 2621 */ 2622 static int 2623 eval_subgraph(graph_vertex_t *v, scf_handle_t *h) 2624 { 2625 boolean_t old = (v->gv_flags & GV_INSUBGRAPH) != 0; 2626 boolean_t new; 2627 graph_edge_t *e; 2628 scf_instance_t *inst; 2629 int ret = 0, r; 2630 2631 assert(milestone != NULL && milestone != MILESTONE_NONE); 2632 2633 new = should_be_in_subgraph(v); 2634 2635 if (new == old) 2636 return (0); 2637 2638 log_framework(LOG_DEBUG, new ? "Adding %s to the subgraph.\n" : 2639 "Removing %s from the subgraph.\n", v->gv_name); 2640 2641 v->gv_flags = (v->gv_flags & ~GV_INSUBGRAPH) | 2642 (new ? GV_INSUBGRAPH : 0); 2643 2644 if (v->gv_type == GVT_INST && (v->gv_flags & GV_CONFIGURED)) { 2645 int err; 2646 2647 get_inst: 2648 err = libscf_fmri_get_instance(h, v->gv_name, &inst); 2649 if (err != 0) { 2650 switch (err) { 2651 case ECONNABORTED: 2652 libscf_handle_rebind(h); 2653 ret = ECONNABORTED; 2654 goto get_inst; 2655 2656 case ENOENT: 2657 break; 2658 2659 case EINVAL: 2660 case ENOTSUP: 2661 default: 2662 bad_error("libscf_fmri_get_instance", err); 2663 } 2664 } else { 2665 const char *f; 2666 2667 if (new) { 2668 err = libscf_delete_enable_ovr(inst); 2669 f = "libscf_delete_enable_ovr"; 2670 } else { 2671 err = libscf_set_enable_ovr(inst, 0); 2672 f = "libscf_set_enable_ovr"; 2673 } 2674 scf_instance_destroy(inst); 2675 switch (err) { 2676 case 0: 2677 case ECANCELED: 2678 break; 2679 2680 case ECONNABORTED: 2681 libscf_handle_rebind(h); 2682 /* 2683 * We must continue so the graph is updated, 2684 * but we must return ECONNABORTED so any 2685 * libscf state held by any callers is reset. 2686 */ 2687 ret = ECONNABORTED; 2688 goto get_inst; 2689 2690 case EROFS: 2691 case EPERM: 2692 log_error(LOG_WARNING, 2693 "Could not set %s/%s for %s: %s.\n", 2694 SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED, 2695 v->gv_name, strerror(err)); 2696 break; 2697 2698 default: 2699 bad_error(f, err); 2700 } 2701 } 2702 } 2703 2704 for (e = uu_list_first(v->gv_dependencies); 2705 e != NULL; 2706 e = uu_list_next(v->gv_dependencies, e)) { 2707 r = eval_subgraph(e->ge_vertex, h); 2708 if (r != 0) { 2709 assert(r == ECONNABORTED); 2710 ret = ECONNABORTED; 2711 } 2712 } 2713 2714 return (ret); 2715 } 2716 2717 /* 2718 * Delete the (property group) dependencies of v & create new ones based on 2719 * inst. If doing so would create a cycle, log a message and put the instance 2720 * into maintenance. Update GV_INSUBGRAPH flags as necessary. Returns 0 or 2721 * ECONNABORTED. 2722 */ 2723 int 2724 refresh_vertex(graph_vertex_t *v, scf_instance_t *inst) 2725 { 2726 int err; 2727 int *path; 2728 char *fmri; 2729 int r; 2730 scf_handle_t *h = scf_instance_handle(inst); 2731 uu_list_t *old_deps; 2732 int ret = 0; 2733 graph_edge_t *e; 2734 graph_vertex_t *vv; 2735 2736 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 2737 assert(v->gv_type == GVT_INST); 2738 2739 log_framework(LOG_DEBUG, "Graph engine: Refreshing %s.\n", v->gv_name); 2740 2741 if (milestone > MILESTONE_NONE) { 2742 /* 2743 * In case some of v's dependencies are being deleted we must 2744 * make a list of them now for GV_INSUBGRAPH-flag evaluation 2745 * after the new dependencies are in place. 2746 */ 2747 old_deps = startd_list_create(graph_edge_pool, NULL, 0); 2748 2749 err = uu_list_walk(v->gv_dependencies, 2750 (uu_walk_fn_t *)append_svcs_or_insts, old_deps, 0); 2751 assert(err == 0); 2752 } 2753 2754 delete_instance_dependencies(v, B_FALSE); 2755 2756 err = set_dependencies(v, inst, &path); 2757 switch (err) { 2758 case 0: 2759 break; 2760 2761 case ECONNABORTED: 2762 ret = err; 2763 goto out; 2764 2765 case EINVAL: 2766 case ELOOP: 2767 r = libscf_instance_get_fmri(inst, &fmri); 2768 switch (r) { 2769 case 0: 2770 break; 2771 2772 case ECONNABORTED: 2773 ret = ECONNABORTED; 2774 goto out; 2775 2776 case ECANCELED: 2777 ret = 0; 2778 goto out; 2779 2780 default: 2781 bad_error("libscf_instance_get_fmri", r); 2782 } 2783 2784 if (err == EINVAL) { 2785 log_error(LOG_ERR, "Transitioning %s " 2786 "to maintenance due to misconfiguration.\n", 2787 fmri ? fmri : "?"); 2788 vertex_send_event(v, 2789 RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY); 2790 } else { 2791 handle_cycle(fmri, path); 2792 vertex_send_event(v, 2793 RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE); 2794 } 2795 startd_free(fmri, max_scf_fmri_size); 2796 ret = 0; 2797 goto out; 2798 2799 default: 2800 bad_error("set_dependencies", err); 2801 } 2802 2803 if (milestone > MILESTONE_NONE) { 2804 boolean_t aborted = B_FALSE; 2805 2806 for (e = uu_list_first(old_deps); 2807 e != NULL; 2808 e = uu_list_next(old_deps, e)) { 2809 vv = e->ge_vertex; 2810 2811 if (vertex_unref(vv) == VERTEX_INUSE && 2812 eval_subgraph(vv, h) == ECONNABORTED) 2813 aborted = B_TRUE; 2814 } 2815 2816 for (e = uu_list_first(v->gv_dependencies); 2817 e != NULL; 2818 e = uu_list_next(v->gv_dependencies, e)) { 2819 if (eval_subgraph(e->ge_vertex, h) == 2820 ECONNABORTED) 2821 aborted = B_TRUE; 2822 } 2823 2824 if (aborted) { 2825 ret = ECONNABORTED; 2826 goto out; 2827 } 2828 } 2829 2830 graph_start_if_satisfied(v); 2831 2832 ret = 0; 2833 2834 out: 2835 if (milestone > MILESTONE_NONE) { 2836 void *cookie = NULL; 2837 2838 while ((e = uu_list_teardown(old_deps, &cookie)) != NULL) 2839 startd_free(e, sizeof (*e)); 2840 2841 uu_list_destroy(old_deps); 2842 } 2843 2844 return (ret); 2845 } 2846 2847 /* 2848 * Set up v according to inst. That is, make sure it depends on its 2849 * restarter and set up its dependencies. Send the ADD_INSTANCE command to 2850 * the restarter, and send ENABLE or DISABLE as appropriate. 2851 * 2852 * Returns 0 on success, ECONNABORTED on repository disconnection, or 2853 * ECANCELED if inst is deleted. 2854 */ 2855 static int 2856 configure_vertex(graph_vertex_t *v, scf_instance_t *inst) 2857 { 2858 scf_handle_t *h; 2859 scf_propertygroup_t *pg; 2860 scf_snapshot_t *snap; 2861 char *restarter_fmri = startd_alloc(max_scf_value_size); 2862 int enabled, enabled_ovr; 2863 int err; 2864 int *path; 2865 2866 restarter_fmri[0] = '\0'; 2867 2868 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 2869 assert(v->gv_type == GVT_INST); 2870 assert((v->gv_flags & GV_CONFIGURED) == 0); 2871 2872 /* GV_INSUBGRAPH should already be set properly. */ 2873 assert(should_be_in_subgraph(v) == 2874 ((v->gv_flags & GV_INSUBGRAPH) != 0)); 2875 2876 log_framework(LOG_DEBUG, "Graph adding %s.\n", v->gv_name); 2877 2878 h = scf_instance_handle(inst); 2879 2880 /* 2881 * If the instance does not have a restarter property group, 2882 * initialize its state to uninitialized/none, in case the restarter 2883 * is not enabled. 2884 */ 2885 pg = safe_scf_pg_create(h); 2886 2887 if (scf_instance_get_pg(inst, SCF_PG_RESTARTER, pg) != 0) { 2888 instance_data_t idata; 2889 uint_t count = 0, msecs = ALLOC_DELAY; 2890 2891 switch (scf_error()) { 2892 case SCF_ERROR_NOT_FOUND: 2893 break; 2894 2895 case SCF_ERROR_CONNECTION_BROKEN: 2896 default: 2897 scf_pg_destroy(pg); 2898 return (ECONNABORTED); 2899 2900 case SCF_ERROR_DELETED: 2901 scf_pg_destroy(pg); 2902 return (ECANCELED); 2903 2904 case SCF_ERROR_NOT_SET: 2905 bad_error("scf_instance_get_pg", scf_error()); 2906 } 2907 2908 switch (err = libscf_instance_get_fmri(inst, 2909 (char **)&idata.i_fmri)) { 2910 case 0: 2911 break; 2912 2913 case ECONNABORTED: 2914 case ECANCELED: 2915 scf_pg_destroy(pg); 2916 return (err); 2917 2918 default: 2919 bad_error("libscf_instance_get_fmri", err); 2920 } 2921 2922 idata.i_state = RESTARTER_STATE_NONE; 2923 idata.i_next_state = RESTARTER_STATE_NONE; 2924 2925 init_state: 2926 switch (err = _restarter_commit_states(h, &idata, 2927 RESTARTER_STATE_UNINIT, RESTARTER_STATE_NONE, NULL)) { 2928 case 0: 2929 break; 2930 2931 case ENOMEM: 2932 ++count; 2933 if (count < ALLOC_RETRY) { 2934 (void) poll(NULL, 0, msecs); 2935 msecs *= ALLOC_DELAY_MULT; 2936 goto init_state; 2937 } 2938 2939 uu_die("Insufficient memory.\n"); 2940 /* NOTREACHED */ 2941 2942 case ECONNABORTED: 2943 startd_free((void *)idata.i_fmri, max_scf_fmri_size); 2944 scf_pg_destroy(pg); 2945 return (ECONNABORTED); 2946 2947 case ENOENT: 2948 startd_free((void *)idata.i_fmri, max_scf_fmri_size); 2949 scf_pg_destroy(pg); 2950 return (ECANCELED); 2951 2952 case EPERM: 2953 case EACCES: 2954 case EROFS: 2955 log_error(LOG_NOTICE, "Could not initialize state for " 2956 "%s: %s.\n", idata.i_fmri, strerror(err)); 2957 break; 2958 2959 case EINVAL: 2960 default: 2961 bad_error("_restarter_commit_states", err); 2962 } 2963 2964 startd_free((void *)idata.i_fmri, max_scf_fmri_size); 2965 } 2966 2967 scf_pg_destroy(pg); 2968 2969 if (milestone != NULL) { 2970 /* 2971 * Make sure the enable-override is set properly before we 2972 * read whether we should be enabled. 2973 */ 2974 if (milestone == MILESTONE_NONE || 2975 !(v->gv_flags & GV_INSUBGRAPH)) { 2976 switch (err = libscf_set_enable_ovr(inst, 0)) { 2977 case 0: 2978 break; 2979 2980 case ECONNABORTED: 2981 case ECANCELED: 2982 return (err); 2983 2984 case EROFS: 2985 log_error(LOG_WARNING, 2986 "Could not set %s/%s for %s: %s.\n", 2987 SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED, 2988 v->gv_name, strerror(err)); 2989 break; 2990 2991 case EPERM: 2992 uu_die("Permission denied.\n"); 2993 /* NOTREACHED */ 2994 2995 default: 2996 bad_error("libscf_set_enable_ovr", err); 2997 } 2998 } else { 2999 assert(v->gv_flags & GV_INSUBGRAPH); 3000 switch (err = libscf_delete_enable_ovr(inst)) { 3001 case 0: 3002 break; 3003 3004 case ECONNABORTED: 3005 case ECANCELED: 3006 return (err); 3007 3008 case EPERM: 3009 uu_die("Permission denied.\n"); 3010 /* NOTREACHED */ 3011 3012 default: 3013 bad_error("libscf_delete_enable_ovr", err); 3014 } 3015 } 3016 } 3017 3018 err = libscf_get_basic_instance_data(h, inst, v->gv_name, &enabled, 3019 &enabled_ovr, &restarter_fmri); 3020 switch (err) { 3021 case 0: 3022 break; 3023 3024 case ECONNABORTED: 3025 case ECANCELED: 3026 startd_free(restarter_fmri, max_scf_value_size); 3027 return (err); 3028 3029 case ENOENT: 3030 log_framework(LOG_DEBUG, 3031 "Ignoring %s because it has no general property group.\n", 3032 v->gv_name); 3033 startd_free(restarter_fmri, max_scf_value_size); 3034 return (0); 3035 3036 default: 3037 bad_error("libscf_get_basic_instance_data", err); 3038 } 3039 3040 if (enabled == -1) { 3041 startd_free(restarter_fmri, max_scf_value_size); 3042 return (0); 3043 } 3044 3045 v->gv_flags = (v->gv_flags & ~GV_ENBLD_NOOVR) | 3046 (enabled ? GV_ENBLD_NOOVR : 0); 3047 3048 if (enabled_ovr != -1) 3049 enabled = enabled_ovr; 3050 3051 v->gv_state = RESTARTER_STATE_UNINIT; 3052 3053 snap = libscf_get_or_make_running_snapshot(inst, v->gv_name, B_TRUE); 3054 scf_snapshot_destroy(snap); 3055 3056 /* Set up the restarter. (Sends _ADD_INSTANCE on success.) */ 3057 err = graph_change_restarter(v, restarter_fmri, h, &path); 3058 if (err != 0) { 3059 instance_data_t idata; 3060 uint_t count = 0, msecs = ALLOC_DELAY; 3061 const char *reason; 3062 3063 if (err == ECONNABORTED) { 3064 startd_free(restarter_fmri, max_scf_value_size); 3065 return (err); 3066 } 3067 3068 assert(err == EINVAL || err == ELOOP); 3069 3070 if (err == EINVAL) { 3071 log_framework(LOG_ERR, emsg_invalid_restarter, 3072 v->gv_name); 3073 reason = "invalid_restarter"; 3074 } else { 3075 handle_cycle(v->gv_name, path); 3076 reason = "dependency_cycle"; 3077 } 3078 3079 startd_free(restarter_fmri, max_scf_value_size); 3080 3081 /* 3082 * We didn't register the instance with the restarter, so we 3083 * must set maintenance mode ourselves. 3084 */ 3085 err = libscf_instance_get_fmri(inst, (char **)&idata.i_fmri); 3086 if (err != 0) { 3087 assert(err == ECONNABORTED || err == ECANCELED); 3088 return (err); 3089 } 3090 3091 idata.i_state = RESTARTER_STATE_NONE; 3092 idata.i_next_state = RESTARTER_STATE_NONE; 3093 3094 set_maint: 3095 switch (err = _restarter_commit_states(h, &idata, 3096 RESTARTER_STATE_MAINT, RESTARTER_STATE_NONE, reason)) { 3097 case 0: 3098 break; 3099 3100 case ENOMEM: 3101 ++count; 3102 if (count < ALLOC_RETRY) { 3103 (void) poll(NULL, 0, msecs); 3104 msecs *= ALLOC_DELAY_MULT; 3105 goto set_maint; 3106 } 3107 3108 uu_die("Insufficient memory.\n"); 3109 /* NOTREACHED */ 3110 3111 case ECONNABORTED: 3112 startd_free((void *)idata.i_fmri, max_scf_fmri_size); 3113 return (ECONNABORTED); 3114 3115 case ENOENT: 3116 startd_free((void *)idata.i_fmri, max_scf_fmri_size); 3117 return (ECANCELED); 3118 3119 case EPERM: 3120 case EACCES: 3121 case EROFS: 3122 log_error(LOG_NOTICE, "Could not initialize state for " 3123 "%s: %s.\n", idata.i_fmri, strerror(err)); 3124 break; 3125 3126 case EINVAL: 3127 default: 3128 bad_error("_restarter_commit_states", err); 3129 } 3130 3131 startd_free((void *)idata.i_fmri, max_scf_fmri_size); 3132 3133 v->gv_state = RESTARTER_STATE_MAINT; 3134 3135 goto out; 3136 } 3137 startd_free(restarter_fmri, max_scf_value_size); 3138 3139 /* Add all the other dependencies. */ 3140 err = refresh_vertex(v, inst); 3141 if (err != 0) { 3142 assert(err == ECONNABORTED); 3143 return (err); 3144 } 3145 3146 out: 3147 v->gv_flags |= GV_CONFIGURED; 3148 3149 graph_enable_by_vertex(v, enabled, 0); 3150 3151 return (0); 3152 } 3153 3154 static void 3155 do_uadmin(void) 3156 { 3157 int fd, left; 3158 struct statvfs vfs; 3159 3160 const char * const resetting = "/etc/svc/volatile/resetting"; 3161 3162 fd = creat(resetting, 0777); 3163 if (fd >= 0) 3164 startd_close(fd); 3165 else 3166 uu_warn("Could not create \"%s\"", resetting); 3167 3168 /* Kill dhcpagent if we're not using nfs for root */ 3169 if ((statvfs("/", &vfs) == 0) && 3170 (strncmp(vfs.f_basetype, "nfs", sizeof ("nfs") - 1) != 0)) 3171 (void) system("/usr/bin/pkill -x -u 0 dhcpagent"); 3172 3173 (void) system("/usr/sbin/killall"); 3174 left = 5; 3175 while (left > 0) 3176 left = sleep(left); 3177 3178 (void) system("/usr/sbin/killall 9"); 3179 left = 10; 3180 while (left > 0) 3181 left = sleep(left); 3182 3183 sync(); 3184 sync(); 3185 sync(); 3186 3187 (void) system("/sbin/umountall"); 3188 (void) system("/sbin/umount /tmp >/dev/null 2>&1"); 3189 (void) system("/sbin/umount /var/adm >/dev/null 2>&1"); 3190 (void) system("/sbin/umount /var/run >/dev/null 2>&1"); 3191 (void) system("/sbin/umount /var >/dev/null 2>&1"); 3192 (void) system("/sbin/umount /usr >/dev/null 2>&1"); 3193 3194 uu_warn("The system is down.\n"); 3195 3196 (void) uadmin(A_SHUTDOWN, halting, NULL); 3197 uu_warn("uadmin() failed"); 3198 3199 if (remove(resetting) != 0 && errno != ENOENT) 3200 uu_warn("Could not remove \"%s\"", resetting); 3201 } 3202 3203 /* 3204 * If any of the up_svcs[] are online or satisfiable, return true. If they are 3205 * all missing, disabled, in maintenance, or unsatisfiable, return false. 3206 */ 3207 boolean_t 3208 can_come_up(void) 3209 { 3210 int i; 3211 3212 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 3213 3214 /* 3215 * If we are booting to single user (boot -s), 3216 * SCF_MILESTONE_SINGLE_USER is needed to come up because startd 3217 * spawns sulogin after single-user is online (see specials.c). 3218 */ 3219 i = (booting_to_single_user ? 0 : 1); 3220 3221 for (; up_svcs[i] != NULL; ++i) { 3222 if (up_svcs_p[i] == NULL) { 3223 up_svcs_p[i] = vertex_get_by_name(up_svcs[i]); 3224 3225 if (up_svcs_p[i] == NULL) 3226 continue; 3227 } 3228 3229 /* 3230 * Ignore unconfigured services (the ones that have been 3231 * mentioned in a dependency from other services, but do 3232 * not exist in the repository). Services which exist 3233 * in the repository but don't have general/enabled 3234 * property will be also ignored. 3235 */ 3236 if (!(up_svcs_p[i]->gv_flags & GV_CONFIGURED)) 3237 continue; 3238 3239 switch (up_svcs_p[i]->gv_state) { 3240 case RESTARTER_STATE_ONLINE: 3241 case RESTARTER_STATE_DEGRADED: 3242 /* 3243 * Deactivate verbose boot once a login service has been 3244 * reached. 3245 */ 3246 st->st_log_login_reached = 1; 3247 /*FALLTHROUGH*/ 3248 case RESTARTER_STATE_UNINIT: 3249 return (B_TRUE); 3250 3251 case RESTARTER_STATE_OFFLINE: 3252 if (instance_satisfied(up_svcs_p[i], B_TRUE) != -1) 3253 return (B_TRUE); 3254 log_framework(LOG_DEBUG, 3255 "can_come_up(): %s is unsatisfiable.\n", 3256 up_svcs_p[i]->gv_name); 3257 continue; 3258 3259 case RESTARTER_STATE_DISABLED: 3260 case RESTARTER_STATE_MAINT: 3261 log_framework(LOG_DEBUG, 3262 "can_come_up(): %s is in state %s.\n", 3263 up_svcs_p[i]->gv_name, 3264 instance_state_str[up_svcs_p[i]->gv_state]); 3265 continue; 3266 3267 default: 3268 #ifndef NDEBUG 3269 uu_warn("%s:%d: Unexpected vertex state %d.\n", 3270 __FILE__, __LINE__, up_svcs_p[i]->gv_state); 3271 #endif 3272 abort(); 3273 } 3274 } 3275 3276 /* 3277 * In the seed repository, console-login is unsatisfiable because 3278 * services are missing. To behave correctly in that case we don't want 3279 * to return false until manifest-import is online. 3280 */ 3281 3282 if (manifest_import_p == NULL) { 3283 manifest_import_p = vertex_get_by_name(manifest_import); 3284 3285 if (manifest_import_p == NULL) 3286 return (B_FALSE); 3287 } 3288 3289 switch (manifest_import_p->gv_state) { 3290 case RESTARTER_STATE_ONLINE: 3291 case RESTARTER_STATE_DEGRADED: 3292 case RESTARTER_STATE_DISABLED: 3293 case RESTARTER_STATE_MAINT: 3294 break; 3295 3296 case RESTARTER_STATE_OFFLINE: 3297 if (instance_satisfied(manifest_import_p, B_TRUE) == -1) 3298 break; 3299 /* FALLTHROUGH */ 3300 3301 case RESTARTER_STATE_UNINIT: 3302 return (B_TRUE); 3303 } 3304 3305 return (B_FALSE); 3306 } 3307 3308 /* 3309 * Runs sulogin. Returns 3310 * 0 - success 3311 * EALREADY - sulogin is already running 3312 * EBUSY - console-login is running 3313 */ 3314 static int 3315 run_sulogin(const char *msg) 3316 { 3317 graph_vertex_t *v; 3318 3319 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 3320 3321 if (sulogin_running) 3322 return (EALREADY); 3323 3324 v = vertex_get_by_name(console_login_fmri); 3325 if (v != NULL && inst_running(v)) 3326 return (EBUSY); 3327 3328 sulogin_running = B_TRUE; 3329 3330 MUTEX_UNLOCK(&dgraph_lock); 3331 3332 fork_sulogin(B_FALSE, msg); 3333 3334 MUTEX_LOCK(&dgraph_lock); 3335 3336 sulogin_running = B_FALSE; 3337 3338 if (console_login_ready) { 3339 v = vertex_get_by_name(console_login_fmri); 3340 3341 if (v != NULL && v->gv_state == RESTARTER_STATE_OFFLINE && 3342 !inst_running(v)) { 3343 if (v->gv_start_f == NULL) 3344 vertex_send_event(v, 3345 RESTARTER_EVENT_TYPE_START); 3346 else 3347 v->gv_start_f(v); 3348 } 3349 3350 console_login_ready = B_FALSE; 3351 } 3352 3353 return (0); 3354 } 3355 3356 /* 3357 * The sulogin thread runs sulogin while can_come_up() is false. run_sulogin() 3358 * keeps sulogin from stepping on console-login's toes. 3359 */ 3360 /* ARGSUSED */ 3361 static void * 3362 sulogin_thread(void *unused) 3363 { 3364 MUTEX_LOCK(&dgraph_lock); 3365 3366 assert(sulogin_thread_running); 3367 3368 do 3369 (void) run_sulogin("Console login service(s) cannot run\n"); 3370 while (!can_come_up()); 3371 3372 sulogin_thread_running = B_FALSE; 3373 MUTEX_UNLOCK(&dgraph_lock); 3374 3375 return (NULL); 3376 } 3377 3378 /* ARGSUSED */ 3379 void * 3380 single_user_thread(void *unused) 3381 { 3382 uint_t left; 3383 scf_handle_t *h; 3384 scf_instance_t *inst; 3385 scf_property_t *prop; 3386 scf_value_t *val; 3387 const char *msg; 3388 char *buf; 3389 int r; 3390 3391 MUTEX_LOCK(&single_user_thread_lock); 3392 single_user_thread_count++; 3393 3394 if (!booting_to_single_user) { 3395 /* 3396 * From rcS.sh: Look for ttymon, in.telnetd, in.rlogind and 3397 * processes in their process groups so they can be terminated. 3398 */ 3399 (void) fputs("svc.startd: Killing user processes: ", stdout); 3400 (void) system("/usr/sbin/killall"); 3401 (void) system("/usr/sbin/killall 9"); 3402 (void) system("/usr/bin/pkill -TERM -v -u 0,1"); 3403 3404 left = 5; 3405 while (left > 0) 3406 left = sleep(left); 3407 3408 (void) system("/usr/bin/pkill -KILL -v -u 0,1"); 3409 (void) puts("done."); 3410 } 3411 3412 if (go_single_user_mode || booting_to_single_user) { 3413 msg = "SINGLE USER MODE\n"; 3414 } else { 3415 assert(go_to_level1); 3416 3417 fork_rc_script('1', "start", B_TRUE); 3418 3419 uu_warn("The system is ready for administration.\n"); 3420 3421 msg = ""; 3422 } 3423 3424 MUTEX_UNLOCK(&single_user_thread_lock); 3425 3426 for (;;) { 3427 MUTEX_LOCK(&dgraph_lock); 3428 r = run_sulogin(msg); 3429 MUTEX_UNLOCK(&dgraph_lock); 3430 if (r == 0) 3431 break; 3432 3433 assert(r == EALREADY || r == EBUSY); 3434 3435 left = 3; 3436 while (left > 0) 3437 left = sleep(left); 3438 } 3439 3440 MUTEX_LOCK(&single_user_thread_lock); 3441 3442 /* 3443 * If another single user thread has started, let it finish changing 3444 * the run level. 3445 */ 3446 if (single_user_thread_count > 1) { 3447 single_user_thread_count--; 3448 MUTEX_UNLOCK(&single_user_thread_lock); 3449 return (NULL); 3450 } 3451 3452 h = libscf_handle_create_bound_loop(); 3453 inst = scf_instance_create(h); 3454 prop = safe_scf_property_create(h); 3455 val = safe_scf_value_create(h); 3456 buf = startd_alloc(max_scf_fmri_size); 3457 3458 lookup: 3459 if (scf_handle_decode_fmri(h, SCF_SERVICE_STARTD, NULL, NULL, inst, 3460 NULL, NULL, SCF_DECODE_FMRI_EXACT) != 0) { 3461 switch (scf_error()) { 3462 case SCF_ERROR_NOT_FOUND: 3463 r = libscf_create_self(h); 3464 if (r == 0) 3465 goto lookup; 3466 assert(r == ECONNABORTED); 3467 /* FALLTHROUGH */ 3468 3469 case SCF_ERROR_CONNECTION_BROKEN: 3470 libscf_handle_rebind(h); 3471 goto lookup; 3472 3473 case SCF_ERROR_INVALID_ARGUMENT: 3474 case SCF_ERROR_CONSTRAINT_VIOLATED: 3475 case SCF_ERROR_NOT_BOUND: 3476 case SCF_ERROR_HANDLE_MISMATCH: 3477 default: 3478 bad_error("scf_handle_decode_fmri", scf_error()); 3479 } 3480 } 3481 3482 MUTEX_LOCK(&dgraph_lock); 3483 3484 r = libscf_inst_delete_prop(inst, SCF_PG_OPTIONS_OVR, 3485 SCF_PROPERTY_MILESTONE); 3486 switch (r) { 3487 case 0: 3488 case ECANCELED: 3489 break; 3490 3491 case ECONNABORTED: 3492 MUTEX_UNLOCK(&dgraph_lock); 3493 libscf_handle_rebind(h); 3494 goto lookup; 3495 3496 case EPERM: 3497 case EACCES: 3498 case EROFS: 3499 log_error(LOG_WARNING, "Could not clear temporary milestone: " 3500 "%s.\n", strerror(r)); 3501 break; 3502 3503 default: 3504 bad_error("libscf_inst_delete_prop", r); 3505 } 3506 3507 MUTEX_UNLOCK(&dgraph_lock); 3508 3509 r = libscf_get_milestone(inst, prop, val, buf, max_scf_fmri_size); 3510 switch (r) { 3511 case ECANCELED: 3512 case ENOENT: 3513 case EINVAL: 3514 (void) strcpy(buf, "all"); 3515 /* FALLTHROUGH */ 3516 3517 case 0: 3518 uu_warn("Returning to milestone %s.\n", buf); 3519 break; 3520 3521 case ECONNABORTED: 3522 libscf_handle_rebind(h); 3523 goto lookup; 3524 3525 default: 3526 bad_error("libscf_get_milestone", r); 3527 } 3528 3529 r = dgraph_set_milestone(buf, h, B_FALSE); 3530 switch (r) { 3531 case 0: 3532 case ECONNRESET: 3533 case EALREADY: 3534 case EINVAL: 3535 case ENOENT: 3536 break; 3537 3538 default: 3539 bad_error("dgraph_set_milestone", r); 3540 } 3541 3542 /* 3543 * See graph_runlevel_changed(). 3544 */ 3545 MUTEX_LOCK(&dgraph_lock); 3546 utmpx_set_runlevel(target_milestone_as_runlevel(), 'S', B_TRUE); 3547 MUTEX_UNLOCK(&dgraph_lock); 3548 3549 startd_free(buf, max_scf_fmri_size); 3550 scf_value_destroy(val); 3551 scf_property_destroy(prop); 3552 scf_instance_destroy(inst); 3553 scf_handle_destroy(h); 3554 3555 /* 3556 * We'll give ourselves 3 seconds to respond to all of the enablings 3557 * that setting the milestone should have created before checking 3558 * whether to run sulogin. 3559 */ 3560 left = 3; 3561 while (left > 0) 3562 left = sleep(left); 3563 3564 MUTEX_LOCK(&dgraph_lock); 3565 /* 3566 * Clearing these variables will allow the sulogin thread to run. We 3567 * check here in case there aren't any more state updates anytime soon. 3568 */ 3569 go_to_level1 = go_single_user_mode = booting_to_single_user = B_FALSE; 3570 if (!sulogin_thread_running && !can_come_up()) { 3571 (void) startd_thread_create(sulogin_thread, NULL); 3572 sulogin_thread_running = B_TRUE; 3573 } 3574 MUTEX_UNLOCK(&dgraph_lock); 3575 single_user_thread_count--; 3576 MUTEX_UNLOCK(&single_user_thread_lock); 3577 return (NULL); 3578 } 3579 3580 3581 /* 3582 * Dependency graph operations API. These are handle-independent thread-safe 3583 * graph manipulation functions which are the entry points for the event 3584 * threads below. 3585 */ 3586 3587 /* 3588 * If a configured vertex exists for inst_fmri, return EEXIST. If no vertex 3589 * exists for inst_fmri, add one. Then fetch the restarter from inst, make 3590 * this vertex dependent on it, and send _ADD_INSTANCE to the restarter. 3591 * Fetch whether the instance should be enabled from inst and send _ENABLE or 3592 * _DISABLE as appropriate. Finally rummage through inst's dependency 3593 * property groups and add vertices and edges as appropriate. If anything 3594 * goes wrong after sending _ADD_INSTANCE, send _ADMIN_MAINT_ON to put the 3595 * instance in maintenance. Don't send _START or _STOP until we get a state 3596 * update in case we're being restarted and the service is already running. 3597 * 3598 * To support booting to a milestone, we must also make sure all dependencies 3599 * encountered are configured, if they exist in the repository. 3600 * 3601 * Returns 0 on success, ECONNABORTED on repository disconnection, EINVAL if 3602 * inst_fmri is an invalid (or not canonical) FMRI, ECANCELED if inst is 3603 * deleted, or EEXIST if a configured vertex for inst_fmri already exists. 3604 */ 3605 int 3606 dgraph_add_instance(const char *inst_fmri, scf_instance_t *inst, 3607 boolean_t lock_graph) 3608 { 3609 graph_vertex_t *v; 3610 int err; 3611 3612 if (strcmp(inst_fmri, SCF_SERVICE_STARTD) == 0) 3613 return (0); 3614 3615 /* Check for a vertex for inst_fmri. */ 3616 if (lock_graph) { 3617 MUTEX_LOCK(&dgraph_lock); 3618 } else { 3619 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 3620 } 3621 3622 v = vertex_get_by_name(inst_fmri); 3623 3624 if (v != NULL) { 3625 assert(v->gv_type == GVT_INST); 3626 3627 if (v->gv_flags & GV_CONFIGURED) { 3628 if (lock_graph) 3629 MUTEX_UNLOCK(&dgraph_lock); 3630 return (EEXIST); 3631 } 3632 } else { 3633 /* Add the vertex. */ 3634 err = graph_insert_vertex_unconfigured(inst_fmri, GVT_INST, 0, 3635 RERR_NONE, &v); 3636 if (err != 0) { 3637 assert(err == EINVAL); 3638 if (lock_graph) 3639 MUTEX_UNLOCK(&dgraph_lock); 3640 return (EINVAL); 3641 } 3642 } 3643 3644 err = configure_vertex(v, inst); 3645 3646 if (lock_graph) 3647 MUTEX_UNLOCK(&dgraph_lock); 3648 3649 return (err); 3650 } 3651 3652 /* 3653 * Locate the vertex for this property group's instance. If it doesn't exist 3654 * or is unconfigured, call dgraph_add_instance() & return. Otherwise fetch 3655 * the restarter for the instance, and if it has changed, send 3656 * _REMOVE_INSTANCE to the old restarter, remove the dependency, make sure the 3657 * new restarter has a vertex, add a new dependency, and send _ADD_INSTANCE to 3658 * the new restarter. Then fetch whether the instance should be enabled, and 3659 * if it is different from what we had, or if we changed the restarter, send 3660 * the appropriate _ENABLE or _DISABLE command. 3661 * 3662 * Returns 0 on success, ENOTSUP if the pg's parent is not an instance, 3663 * ECONNABORTED on repository disconnection, ECANCELED if the instance is 3664 * deleted, or -1 if the instance's general property group is deleted or if 3665 * its enabled property is misconfigured. 3666 */ 3667 static int 3668 dgraph_update_general(scf_propertygroup_t *pg) 3669 { 3670 scf_handle_t *h; 3671 scf_instance_t *inst; 3672 char *fmri; 3673 char *restarter_fmri; 3674 graph_vertex_t *v; 3675 int err; 3676 int enabled, enabled_ovr; 3677 int oldflags; 3678 3679 /* Find the vertex for this service */ 3680 h = scf_pg_handle(pg); 3681 3682 inst = safe_scf_instance_create(h); 3683 3684 if (scf_pg_get_parent_instance(pg, inst) != 0) { 3685 switch (scf_error()) { 3686 case SCF_ERROR_CONSTRAINT_VIOLATED: 3687 return (ENOTSUP); 3688 3689 case SCF_ERROR_CONNECTION_BROKEN: 3690 default: 3691 return (ECONNABORTED); 3692 3693 case SCF_ERROR_DELETED: 3694 return (0); 3695 3696 case SCF_ERROR_NOT_SET: 3697 bad_error("scf_pg_get_parent_instance", scf_error()); 3698 } 3699 } 3700 3701 err = libscf_instance_get_fmri(inst, &fmri); 3702 switch (err) { 3703 case 0: 3704 break; 3705 3706 case ECONNABORTED: 3707 scf_instance_destroy(inst); 3708 return (ECONNABORTED); 3709 3710 case ECANCELED: 3711 scf_instance_destroy(inst); 3712 return (0); 3713 3714 default: 3715 bad_error("libscf_instance_get_fmri", err); 3716 } 3717 3718 log_framework(LOG_DEBUG, 3719 "Graph engine: Reloading general properties for %s.\n", fmri); 3720 3721 MUTEX_LOCK(&dgraph_lock); 3722 3723 v = vertex_get_by_name(fmri); 3724 if (v == NULL || !(v->gv_flags & GV_CONFIGURED)) { 3725 /* Will get the up-to-date properties. */ 3726 MUTEX_UNLOCK(&dgraph_lock); 3727 err = dgraph_add_instance(fmri, inst, B_TRUE); 3728 startd_free(fmri, max_scf_fmri_size); 3729 scf_instance_destroy(inst); 3730 return (err == ECANCELED ? 0 : err); 3731 } 3732 3733 /* Read enabled & restarter from repository. */ 3734 restarter_fmri = startd_alloc(max_scf_value_size); 3735 err = libscf_get_basic_instance_data(h, inst, v->gv_name, &enabled, 3736 &enabled_ovr, &restarter_fmri); 3737 if (err != 0 || enabled == -1) { 3738 MUTEX_UNLOCK(&dgraph_lock); 3739 scf_instance_destroy(inst); 3740 startd_free(fmri, max_scf_fmri_size); 3741 3742 switch (err) { 3743 case ENOENT: 3744 case 0: 3745 startd_free(restarter_fmri, max_scf_value_size); 3746 return (-1); 3747 3748 case ECONNABORTED: 3749 case ECANCELED: 3750 startd_free(restarter_fmri, max_scf_value_size); 3751 return (err); 3752 3753 default: 3754 bad_error("libscf_get_basic_instance_data", err); 3755 } 3756 } 3757 3758 oldflags = v->gv_flags; 3759 v->gv_flags = (v->gv_flags & ~GV_ENBLD_NOOVR) | 3760 (enabled ? GV_ENBLD_NOOVR : 0); 3761 3762 if (enabled_ovr != -1) 3763 enabled = enabled_ovr; 3764 3765 /* 3766 * If GV_ENBLD_NOOVR has changed, then we need to re-evaluate the 3767 * subgraph. 3768 */ 3769 if (milestone > MILESTONE_NONE && v->gv_flags != oldflags) 3770 (void) eval_subgraph(v, h); 3771 3772 scf_instance_destroy(inst); 3773 3774 /* Ignore restarter change for now. */ 3775 3776 startd_free(restarter_fmri, max_scf_value_size); 3777 startd_free(fmri, max_scf_fmri_size); 3778 3779 /* 3780 * Always send _ENABLE or _DISABLE. We could avoid this if the 3781 * restarter didn't change and the enabled value didn't change, but 3782 * that's not easy to check and improbable anyway, so we'll just do 3783 * this. 3784 */ 3785 graph_enable_by_vertex(v, enabled, 1); 3786 3787 MUTEX_UNLOCK(&dgraph_lock); 3788 3789 return (0); 3790 } 3791 3792 /* 3793 * Delete all of the property group dependencies of v, update inst's running 3794 * snapshot, and add the dependencies in the new snapshot. If any of the new 3795 * dependencies would create a cycle, send _ADMIN_MAINT_ON. Otherwise 3796 * reevaluate v's dependencies, send _START or _STOP as appropriate, and do 3797 * the same for v's dependents. 3798 * 3799 * Returns 3800 * 0 - success 3801 * ECONNABORTED - repository connection broken 3802 * ECANCELED - inst was deleted 3803 * EINVAL - inst is invalid (e.g., missing general/enabled) 3804 * -1 - libscf_snapshots_refresh() failed 3805 */ 3806 static int 3807 dgraph_refresh_instance(graph_vertex_t *v, scf_instance_t *inst) 3808 { 3809 int r; 3810 int enabled; 3811 3812 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 3813 assert(v->gv_type == GVT_INST); 3814 3815 /* Only refresh services with valid general/enabled properties. */ 3816 r = libscf_get_basic_instance_data(scf_instance_handle(inst), inst, 3817 v->gv_name, &enabled, NULL, NULL); 3818 switch (r) { 3819 case 0: 3820 break; 3821 3822 case ECONNABORTED: 3823 case ECANCELED: 3824 return (r); 3825 3826 case ENOENT: 3827 log_framework(LOG_DEBUG, 3828 "Ignoring %s because it has no general property group.\n", 3829 v->gv_name); 3830 return (EINVAL); 3831 3832 default: 3833 bad_error("libscf_get_basic_instance_data", r); 3834 } 3835 3836 if (enabled == -1) 3837 return (EINVAL); 3838 3839 r = libscf_snapshots_refresh(inst, v->gv_name); 3840 if (r != 0) { 3841 if (r != -1) 3842 bad_error("libscf_snapshots_refresh", r); 3843 3844 /* error logged */ 3845 return (r); 3846 } 3847 3848 r = refresh_vertex(v, inst); 3849 if (r != 0 && r != ECONNABORTED) 3850 bad_error("refresh_vertex", r); 3851 return (r); 3852 } 3853 3854 /* 3855 * Returns 1 if any instances which directly depend on the passed instance 3856 * (or it's service) are running. 3857 */ 3858 static int 3859 has_running_nonsubgraph_dependents(graph_vertex_t *v) 3860 { 3861 graph_vertex_t *vv; 3862 graph_edge_t *e; 3863 3864 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 3865 3866 for (e = uu_list_first(v->gv_dependents); 3867 e != NULL; 3868 e = uu_list_next(v->gv_dependents, e)) { 3869 3870 vv = e->ge_vertex; 3871 if (vv->gv_type == GVT_INST) { 3872 if (inst_running(vv) && 3873 ((vv->gv_flags & GV_INSUBGRAPH) == 0)) 3874 return (1); 3875 } else { 3876 /* 3877 * For dependency group or service vertices, keep 3878 * traversing to see if instances are running. 3879 */ 3880 if (has_running_nonsubgraph_dependents(vv)) 3881 return (1); 3882 } 3883 } 3884 return (0); 3885 } 3886 3887 /* 3888 * For the dependency, disable the instance which makes up the dependency if 3889 * it is not in the subgraph and running. If the dependency instance is in 3890 * the subgraph or it is not running, continue by disabling all of its 3891 * non-subgraph dependencies. 3892 */ 3893 static void 3894 disable_nonsubgraph_dependencies(graph_vertex_t *v, void *arg) 3895 { 3896 int r; 3897 scf_handle_t *h = (scf_handle_t *)arg; 3898 scf_instance_t *inst = NULL; 3899 3900 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 3901 3902 /* Continue recursing non-inst nodes */ 3903 if (v->gv_type != GVT_INST) 3904 goto recurse; 3905 3906 /* 3907 * For instances that are in the subgraph or already not running, 3908 * skip and attempt to disable their non-dependencies. 3909 */ 3910 if ((v->gv_flags & GV_INSUBGRAPH) || (!inst_running(v))) 3911 goto recurse; 3912 3913 /* 3914 * If not all this instance's dependents have stopped 3915 * running, do not disable. 3916 */ 3917 if (has_running_nonsubgraph_dependents(v)) 3918 return; 3919 3920 inst = scf_instance_create(h); 3921 if (inst == NULL) { 3922 log_error(LOG_WARNING, "Unable to gracefully disable instance:" 3923 " %s due to lack of resources\n", v->gv_name); 3924 goto disable; 3925 } 3926 again: 3927 r = scf_handle_decode_fmri(h, v->gv_name, NULL, NULL, inst, 3928 NULL, NULL, SCF_DECODE_FMRI_EXACT); 3929 if (r != 0) { 3930 switch (scf_error()) { 3931 case SCF_ERROR_CONNECTION_BROKEN: 3932 libscf_handle_rebind(h); 3933 goto again; 3934 3935 case SCF_ERROR_NOT_FOUND: 3936 goto recurse; 3937 3938 case SCF_ERROR_HANDLE_MISMATCH: 3939 case SCF_ERROR_INVALID_ARGUMENT: 3940 case SCF_ERROR_CONSTRAINT_VIOLATED: 3941 case SCF_ERROR_NOT_BOUND: 3942 default: 3943 bad_error("scf_handle_decode_fmri", 3944 scf_error()); 3945 } 3946 } 3947 r = libscf_set_enable_ovr(inst, 0); 3948 switch (r) { 3949 case 0: 3950 scf_instance_destroy(inst); 3951 return; 3952 case ECANCELED: 3953 scf_instance_destroy(inst); 3954 goto recurse; 3955 case ECONNABORTED: 3956 libscf_handle_rebind(h); 3957 goto again; 3958 case EPERM: 3959 case EROFS: 3960 log_error(LOG_WARNING, 3961 "Could not set %s/%s for %s: %s.\n", 3962 SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED, 3963 v->gv_name, strerror(r)); 3964 goto disable; 3965 default: 3966 bad_error("libscf_set_enable_ovr", r); 3967 } 3968 disable: 3969 graph_enable_by_vertex(v, 0, 0); 3970 return; 3971 recurse: 3972 graph_walk_dependencies(v, disable_nonsubgraph_dependencies, 3973 arg); 3974 } 3975 3976 /* 3977 * Find the vertex for inst_name. If it doesn't exist, return ENOENT. 3978 * Otherwise set its state to state. If the instance has entered a state 3979 * which requires automatic action, take it (Uninitialized: do 3980 * dgraph_refresh_instance() without the snapshot update. Disabled: if the 3981 * instance should be enabled, send _ENABLE. Offline: if the instance should 3982 * be disabled, send _DISABLE, and if its dependencies are satisfied, send 3983 * _START. Online, Degraded: if the instance wasn't running, update its start 3984 * snapshot. Maintenance: no action.) 3985 * 3986 * Also fails with ECONNABORTED, or EINVAL if state is invalid. 3987 */ 3988 static int 3989 dgraph_set_instance_state(scf_handle_t *h, const char *inst_name, 3990 restarter_instance_state_t state, restarter_error_t serr) 3991 { 3992 graph_vertex_t *v; 3993 int err = 0; 3994 restarter_instance_state_t old_state; 3995 3996 MUTEX_LOCK(&dgraph_lock); 3997 3998 v = vertex_get_by_name(inst_name); 3999 if (v == NULL) { 4000 MUTEX_UNLOCK(&dgraph_lock); 4001 return (ENOENT); 4002 } 4003 4004 switch (state) { 4005 case RESTARTER_STATE_UNINIT: 4006 case RESTARTER_STATE_DISABLED: 4007 case RESTARTER_STATE_OFFLINE: 4008 case RESTARTER_STATE_ONLINE: 4009 case RESTARTER_STATE_DEGRADED: 4010 case RESTARTER_STATE_MAINT: 4011 break; 4012 4013 default: 4014 MUTEX_UNLOCK(&dgraph_lock); 4015 return (EINVAL); 4016 } 4017 4018 log_framework(LOG_DEBUG, "Graph noting %s %s -> %s.\n", v->gv_name, 4019 instance_state_str[v->gv_state], instance_state_str[state]); 4020 4021 old_state = v->gv_state; 4022 v->gv_state = state; 4023 4024 err = gt_transition(h, v, serr, old_state); 4025 4026 MUTEX_UNLOCK(&dgraph_lock); 4027 return (err); 4028 } 4029 4030 /* 4031 * If this is a service shutdown and we're in the middle of a subgraph 4032 * shutdown, we need to check if either we're the last service to go 4033 * and should kickoff system shutdown, or if we should disable other 4034 * services. 4035 */ 4036 void 4037 vertex_subgraph_dependencies_shutdown(scf_handle_t *h, graph_vertex_t *v, 4038 int was_running) 4039 { 4040 int up_or_down; 4041 4042 up_or_down = was_running ^ inst_running(v); 4043 4044 if (up_or_down && milestone != NULL && !inst_running(v) && 4045 ((v->gv_flags & GV_INSUBGRAPH) == 0 || 4046 milestone == MILESTONE_NONE)) { 4047 --non_subgraph_svcs; 4048 if (non_subgraph_svcs == 0) { 4049 if (halting != -1) { 4050 do_uadmin(); 4051 } else if (go_single_user_mode || go_to_level1) { 4052 (void) startd_thread_create(single_user_thread, 4053 NULL); 4054 } 4055 } else { 4056 graph_walk_dependencies(v, 4057 disable_nonsubgraph_dependencies, (void *)h); 4058 } 4059 } 4060 } 4061 4062 /* 4063 * Decide whether to start up an sulogin thread after a service is 4064 * finished changing state. Only need to do the full can_come_up() 4065 * evaluation if an instance is changing state, we're not halfway through 4066 * loading the thread, and we aren't shutting down or going to the single 4067 * user milestone. 4068 */ 4069 void 4070 graph_transition_sulogin(restarter_instance_state_t state, 4071 restarter_instance_state_t old_state) 4072 { 4073 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 4074 4075 if (state != old_state && st->st_load_complete && 4076 !go_single_user_mode && !go_to_level1 && 4077 halting == -1) { 4078 if (!can_come_up() && !sulogin_thread_running) { 4079 (void) startd_thread_create(sulogin_thread, NULL); 4080 sulogin_thread_running = B_TRUE; 4081 } 4082 } 4083 } 4084 4085 /* 4086 * Propagate a start, stop event, or a satisfiability event. 4087 * 4088 * PROPAGATE_START and PROPAGATE_STOP simply propagate the transition event 4089 * to direct dependents. PROPAGATE_SAT propagates a start then walks the 4090 * full dependent graph to check for newly satisfied nodes. This is 4091 * necessary for cases when non-direct dependents may be effected but direct 4092 * dependents may not (e.g. for optional_all evaluations, see the 4093 * propagate_satbility() comments). 4094 * 4095 * PROPAGATE_SAT should be used whenever a non-running service moves into 4096 * a state which can satisfy optional dependencies, like disabled or 4097 * maintenance. 4098 */ 4099 void 4100 graph_transition_propagate(graph_vertex_t *v, propagate_event_t type, 4101 restarter_error_t rerr) 4102 { 4103 if (type == PROPAGATE_STOP) { 4104 graph_walk_dependents(v, propagate_stop, (void *)rerr); 4105 } else if (type == PROPAGATE_START || type == PROPAGATE_SAT) { 4106 graph_walk_dependents(v, propagate_start, NULL); 4107 4108 if (type == PROPAGATE_SAT) 4109 propagate_satbility(v); 4110 } else { 4111 #ifndef NDEBUG 4112 uu_warn("%s:%d: Unexpected type value %d.\n", __FILE__, 4113 __LINE__, type); 4114 #endif 4115 abort(); 4116 } 4117 } 4118 4119 /* 4120 * If a vertex for fmri exists and it is enabled, send _DISABLE to the 4121 * restarter. If it is running, send _STOP. Send _REMOVE_INSTANCE. Delete 4122 * all property group dependencies, and the dependency on the restarter, 4123 * disposing of vertices as appropriate. If other vertices depend on this 4124 * one, mark it unconfigured and return. Otherwise remove the vertex. Always 4125 * returns 0. 4126 */ 4127 static int 4128 dgraph_remove_instance(const char *fmri, scf_handle_t *h) 4129 { 4130 graph_vertex_t *v; 4131 graph_edge_t *e; 4132 uu_list_t *old_deps; 4133 int err; 4134 4135 log_framework(LOG_DEBUG, "Graph engine: Removing %s.\n", fmri); 4136 4137 MUTEX_LOCK(&dgraph_lock); 4138 4139 v = vertex_get_by_name(fmri); 4140 if (v == NULL) { 4141 MUTEX_UNLOCK(&dgraph_lock); 4142 return (0); 4143 } 4144 4145 /* Send restarter delete event. */ 4146 if (v->gv_flags & GV_CONFIGURED) 4147 graph_unset_restarter(v); 4148 4149 if (milestone > MILESTONE_NONE) { 4150 /* 4151 * Make a list of v's current dependencies so we can 4152 * reevaluate their GV_INSUBGRAPH flags after the dependencies 4153 * are removed. 4154 */ 4155 old_deps = startd_list_create(graph_edge_pool, NULL, 0); 4156 4157 err = uu_list_walk(v->gv_dependencies, 4158 (uu_walk_fn_t *)append_svcs_or_insts, old_deps, 0); 4159 assert(err == 0); 4160 } 4161 4162 delete_instance_dependencies(v, B_TRUE); 4163 4164 /* 4165 * Deleting an instance can both satisfy and unsatisfy dependencies, 4166 * depending on their type. First propagate the stop as a RERR_RESTART 4167 * event -- deletion isn't a fault, just a normal stop. This gives 4168 * dependent services the chance to do a clean shutdown. Then, mark 4169 * the service as unconfigured and propagate the start event for the 4170 * optional_all dependencies that might have become satisfied. 4171 */ 4172 graph_walk_dependents(v, propagate_stop, (void *)RERR_RESTART); 4173 4174 v->gv_flags &= ~GV_CONFIGURED; 4175 4176 graph_walk_dependents(v, propagate_start, NULL); 4177 propagate_satbility(v); 4178 4179 /* 4180 * If there are no (non-service) dependents, the vertex can be 4181 * completely removed. 4182 */ 4183 if (v != milestone && v->gv_refs == 0 && 4184 uu_list_numnodes(v->gv_dependents) == 1) 4185 remove_inst_vertex(v); 4186 4187 if (milestone > MILESTONE_NONE) { 4188 void *cookie = NULL; 4189 4190 while ((e = uu_list_teardown(old_deps, &cookie)) != NULL) { 4191 v = e->ge_vertex; 4192 4193 if (vertex_unref(v) == VERTEX_INUSE) 4194 while (eval_subgraph(v, h) == ECONNABORTED) 4195 libscf_handle_rebind(h); 4196 4197 startd_free(e, sizeof (*e)); 4198 } 4199 4200 uu_list_destroy(old_deps); 4201 } 4202 4203 MUTEX_UNLOCK(&dgraph_lock); 4204 4205 return (0); 4206 } 4207 4208 /* 4209 * Return the eventual (maybe current) milestone in the form of a 4210 * legacy runlevel. 4211 */ 4212 static char 4213 target_milestone_as_runlevel() 4214 { 4215 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 4216 4217 if (milestone == NULL) 4218 return ('3'); 4219 else if (milestone == MILESTONE_NONE) 4220 return ('0'); 4221 4222 if (strcmp(milestone->gv_name, multi_user_fmri) == 0) 4223 return ('2'); 4224 else if (strcmp(milestone->gv_name, single_user_fmri) == 0) 4225 return ('S'); 4226 else if (strcmp(milestone->gv_name, multi_user_svr_fmri) == 0) 4227 return ('3'); 4228 4229 #ifndef NDEBUG 4230 (void) fprintf(stderr, "%s:%d: Unknown milestone name \"%s\".\n", 4231 __FILE__, __LINE__, milestone->gv_name); 4232 #endif 4233 abort(); 4234 /* NOTREACHED */ 4235 } 4236 4237 static struct { 4238 char rl; 4239 int sig; 4240 } init_sigs[] = { 4241 { 'S', SIGBUS }, 4242 { '0', SIGINT }, 4243 { '1', SIGQUIT }, 4244 { '2', SIGILL }, 4245 { '3', SIGTRAP }, 4246 { '4', SIGIOT }, 4247 { '5', SIGEMT }, 4248 { '6', SIGFPE }, 4249 { 0, 0 } 4250 }; 4251 4252 static void 4253 signal_init(char rl) 4254 { 4255 pid_t init_pid; 4256 int i; 4257 4258 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 4259 4260 if (zone_getattr(getzoneid(), ZONE_ATTR_INITPID, &init_pid, 4261 sizeof (init_pid)) != sizeof (init_pid)) { 4262 log_error(LOG_NOTICE, "Could not get pid to signal init.\n"); 4263 return; 4264 } 4265 4266 for (i = 0; init_sigs[i].rl != 0; ++i) 4267 if (init_sigs[i].rl == rl) 4268 break; 4269 4270 if (init_sigs[i].rl != 0) { 4271 if (kill(init_pid, init_sigs[i].sig) != 0) { 4272 switch (errno) { 4273 case EPERM: 4274 case ESRCH: 4275 log_error(LOG_NOTICE, "Could not signal init: " 4276 "%s.\n", strerror(errno)); 4277 break; 4278 4279 case EINVAL: 4280 default: 4281 bad_error("kill", errno); 4282 } 4283 } 4284 } 4285 } 4286 4287 /* 4288 * This is called when one of the major milestones changes state, or when 4289 * init is signalled and tells us it was told to change runlevel. We wait 4290 * to reach the milestone because this allows /etc/inittab entries to retain 4291 * some boot ordering: historically, entries could place themselves before/after 4292 * the running of /sbin/rcX scripts but we can no longer make the 4293 * distinction because the /sbin/rcX scripts no longer exist as punctuation 4294 * marks in /etc/inittab. 4295 * 4296 * Also, we only trigger an update when we reach the eventual target 4297 * milestone: without this, an /etc/inittab entry marked only for 4298 * runlevel 2 would be executed for runlevel 3, which is not how 4299 * /etc/inittab entries work. 4300 * 4301 * If we're single user coming online, then we set utmpx to the target 4302 * runlevel so that legacy scripts can work as expected. 4303 */ 4304 static void 4305 graph_runlevel_changed(char rl, int online) 4306 { 4307 char trl; 4308 4309 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 4310 4311 trl = target_milestone_as_runlevel(); 4312 4313 if (online) { 4314 if (rl == trl) { 4315 current_runlevel = trl; 4316 signal_init(trl); 4317 } else if (rl == 'S') { 4318 /* 4319 * At boot, set the entry early for the benefit of the 4320 * legacy init scripts. 4321 */ 4322 utmpx_set_runlevel(trl, 'S', B_FALSE); 4323 } 4324 } else { 4325 if (rl == '3' && trl == '2') { 4326 current_runlevel = trl; 4327 signal_init(trl); 4328 } else if (rl == '2' && trl == 'S') { 4329 current_runlevel = trl; 4330 signal_init(trl); 4331 } 4332 } 4333 } 4334 4335 /* 4336 * Move to a backwards-compatible runlevel by executing the appropriate 4337 * /etc/rc?.d/K* scripts and/or setting the milestone. 4338 * 4339 * Returns 4340 * 0 - success 4341 * ECONNRESET - success, but handle was reset 4342 * ECONNABORTED - repository connection broken 4343 * ECANCELED - pg was deleted 4344 */ 4345 static int 4346 dgraph_set_runlevel(scf_propertygroup_t *pg, scf_property_t *prop) 4347 { 4348 char rl; 4349 scf_handle_t *h; 4350 int r; 4351 const char *ms = NULL; /* what to commit as options/milestone */ 4352 boolean_t rebound = B_FALSE; 4353 int mark_rl = 0; 4354 4355 const char * const stop = "stop"; 4356 4357 r = libscf_extract_runlevel(prop, &rl); 4358 switch (r) { 4359 case 0: 4360 break; 4361 4362 case ECONNABORTED: 4363 case ECANCELED: 4364 return (r); 4365 4366 case EINVAL: 4367 case ENOENT: 4368 log_error(LOG_WARNING, "runlevel property is misconfigured; " 4369 "ignoring.\n"); 4370 /* delete the bad property */ 4371 goto nolock_out; 4372 4373 default: 4374 bad_error("libscf_extract_runlevel", r); 4375 } 4376 4377 switch (rl) { 4378 case 's': 4379 rl = 'S'; 4380 /* FALLTHROUGH */ 4381 4382 case 'S': 4383 case '2': 4384 case '3': 4385 /* 4386 * These cases cause a milestone change, so 4387 * graph_runlevel_changed() will eventually deal with 4388 * signalling init. 4389 */ 4390 break; 4391 4392 case '0': 4393 case '1': 4394 case '4': 4395 case '5': 4396 case '6': 4397 mark_rl = 1; 4398 break; 4399 4400 default: 4401 log_framework(LOG_NOTICE, "Unknown runlevel '%c'.\n", rl); 4402 ms = NULL; 4403 goto nolock_out; 4404 } 4405 4406 h = scf_pg_handle(pg); 4407 4408 MUTEX_LOCK(&dgraph_lock); 4409 4410 /* 4411 * Since this triggers no milestone changes, force it by hand. 4412 */ 4413 if (current_runlevel == '4' && rl == '3') 4414 mark_rl = 1; 4415 4416 /* 4417 * 1. If we are here after an "init X": 4418 * 4419 * init X 4420 * init/lscf_set_runlevel() 4421 * process_pg_event() 4422 * dgraph_set_runlevel() 4423 * 4424 * then we haven't passed through graph_runlevel_changed() yet, 4425 * therefore 'current_runlevel' has not changed for sure but 'rl' has. 4426 * In consequence, if 'rl' is lower than 'current_runlevel', we change 4427 * the system runlevel and execute the appropriate /etc/rc?.d/K* scripts 4428 * past this test. 4429 * 4430 * 2. On the other hand, if we are here after a "svcadm milestone": 4431 * 4432 * svcadm milestone X 4433 * dgraph_set_milestone() 4434 * handle_graph_update_event() 4435 * dgraph_set_instance_state() 4436 * graph_post_X_[online|offline]() 4437 * graph_runlevel_changed() 4438 * signal_init() 4439 * init/lscf_set_runlevel() 4440 * process_pg_event() 4441 * dgraph_set_runlevel() 4442 * 4443 * then we already passed through graph_runlevel_changed() (by the way 4444 * of dgraph_set_milestone()) and 'current_runlevel' may have changed 4445 * and already be equal to 'rl' so we are going to return immediately 4446 * from dgraph_set_runlevel() without changing the system runlevel and 4447 * without executing the /etc/rc?.d/K* scripts. 4448 */ 4449 if (rl == current_runlevel) { 4450 ms = NULL; 4451 goto out; 4452 } 4453 4454 log_framework(LOG_DEBUG, "Changing to runlevel '%c'.\n", rl); 4455 4456 /* 4457 * Make sure stop rc scripts see the new settings via who -r. 4458 */ 4459 utmpx_set_runlevel(rl, current_runlevel, B_TRUE); 4460 4461 /* 4462 * Some run levels don't have a direct correspondence to any 4463 * milestones, so we have to signal init directly. 4464 */ 4465 if (mark_rl) { 4466 current_runlevel = rl; 4467 signal_init(rl); 4468 } 4469 4470 switch (rl) { 4471 case 'S': 4472 uu_warn("The system is coming down for administration. " 4473 "Please wait.\n"); 4474 fork_rc_script(rl, stop, B_FALSE); 4475 ms = single_user_fmri; 4476 go_single_user_mode = B_TRUE; 4477 break; 4478 4479 case '0': 4480 fork_rc_script(rl, stop, B_TRUE); 4481 halting = AD_HALT; 4482 goto uadmin; 4483 4484 case '5': 4485 fork_rc_script(rl, stop, B_TRUE); 4486 halting = AD_POWEROFF; 4487 goto uadmin; 4488 4489 case '6': 4490 fork_rc_script(rl, stop, B_TRUE); 4491 halting = AD_BOOT; 4492 goto uadmin; 4493 4494 uadmin: 4495 uu_warn("The system is coming down. Please wait.\n"); 4496 ms = "none"; 4497 4498 /* 4499 * We can't wait until all services are offline since this 4500 * thread is responsible for taking them offline. Instead we 4501 * set halting to the second argument for uadmin() and call 4502 * do_uadmin() from dgraph_set_instance_state() when 4503 * appropriate. 4504 */ 4505 break; 4506 4507 case '1': 4508 if (current_runlevel != 'S') { 4509 uu_warn("Changing to state 1.\n"); 4510 fork_rc_script(rl, stop, B_FALSE); 4511 } else { 4512 uu_warn("The system is coming up for administration. " 4513 "Please wait.\n"); 4514 } 4515 ms = single_user_fmri; 4516 go_to_level1 = B_TRUE; 4517 break; 4518 4519 case '2': 4520 if (current_runlevel == '3' || current_runlevel == '4') 4521 fork_rc_script(rl, stop, B_FALSE); 4522 ms = multi_user_fmri; 4523 break; 4524 4525 case '3': 4526 case '4': 4527 ms = "all"; 4528 break; 4529 4530 default: 4531 #ifndef NDEBUG 4532 (void) fprintf(stderr, "%s:%d: Uncaught case %d ('%c').\n", 4533 __FILE__, __LINE__, rl, rl); 4534 #endif 4535 abort(); 4536 } 4537 4538 out: 4539 MUTEX_UNLOCK(&dgraph_lock); 4540 4541 nolock_out: 4542 switch (r = libscf_clear_runlevel(pg, ms)) { 4543 case 0: 4544 break; 4545 4546 case ECONNABORTED: 4547 libscf_handle_rebind(h); 4548 rebound = B_TRUE; 4549 goto nolock_out; 4550 4551 case ECANCELED: 4552 break; 4553 4554 case EPERM: 4555 case EACCES: 4556 case EROFS: 4557 log_error(LOG_NOTICE, "Could not delete \"%s/%s\" property: " 4558 "%s.\n", SCF_PG_OPTIONS, "runlevel", strerror(r)); 4559 break; 4560 4561 default: 4562 bad_error("libscf_clear_runlevel", r); 4563 } 4564 4565 return (rebound ? ECONNRESET : 0); 4566 } 4567 4568 static int 4569 mark_subgraph(graph_edge_t *e, void *arg) 4570 { 4571 graph_vertex_t *v; 4572 int r; 4573 int optional = (int)arg; 4574 4575 v = e->ge_vertex; 4576 4577 /* If it's already in the subgraph, skip. */ 4578 if (v->gv_flags & GV_INSUBGRAPH) 4579 return (UU_WALK_NEXT); 4580 4581 /* 4582 * Keep track if walk has entered an optional dependency group 4583 */ 4584 if (v->gv_type == GVT_GROUP && v->gv_depgroup == DEPGRP_OPTIONAL_ALL) { 4585 optional = 1; 4586 } 4587 /* 4588 * Quit if we are in an optional dependency group and the instance 4589 * is disabled 4590 */ 4591 if (optional && (v->gv_type == GVT_INST) && 4592 (!(v->gv_flags & GV_ENBLD_NOOVR))) 4593 return (UU_WALK_NEXT); 4594 4595 v->gv_flags |= GV_INSUBGRAPH; 4596 4597 /* Skip all excluded dependencies. */ 4598 if (v->gv_type == GVT_GROUP && v->gv_depgroup == DEPGRP_EXCLUDE_ALL) 4599 return (UU_WALK_NEXT); 4600 4601 r = uu_list_walk(v->gv_dependencies, (uu_walk_fn_t *)mark_subgraph, 4602 (void *)optional, 0); 4603 assert(r == 0); 4604 return (UU_WALK_NEXT); 4605 } 4606 4607 /* 4608 * "Restrict" the graph to dependencies of fmri. We implement it by walking 4609 * all services, override-disabling those which are not descendents of the 4610 * instance, and removing any enable-override for the rest. milestone is set 4611 * to the vertex which represents fmri so that the other graph operations may 4612 * act appropriately. 4613 * 4614 * If norepository is true, the function will not change the repository. 4615 * 4616 * The decision to change the system run level in accordance with the milestone 4617 * is taken in dgraph_set_runlevel(). 4618 * 4619 * Returns 4620 * 0 - success 4621 * ECONNRESET - success, but handle was rebound 4622 * EINVAL - fmri is invalid (error is logged) 4623 * EALREADY - the milestone is already set to fmri 4624 * ENOENT - a configured vertex does not exist for fmri (an error is logged) 4625 */ 4626 static int 4627 dgraph_set_milestone(const char *fmri, scf_handle_t *h, boolean_t norepository) 4628 { 4629 const char *cfmri, *fs; 4630 graph_vertex_t *nm, *v; 4631 int ret = 0, r; 4632 scf_instance_t *inst; 4633 boolean_t isall, isnone, rebound = B_FALSE; 4634 4635 /* Validate fmri */ 4636 isall = (strcmp(fmri, "all") == 0); 4637 isnone = (strcmp(fmri, "none") == 0); 4638 4639 if (!isall && !isnone) { 4640 if (fmri_canonify(fmri, (char **)&cfmri, B_FALSE) == EINVAL) 4641 goto reject; 4642 4643 if (strcmp(cfmri, single_user_fmri) != 0 && 4644 strcmp(cfmri, multi_user_fmri) != 0 && 4645 strcmp(cfmri, multi_user_svr_fmri) != 0) { 4646 startd_free((void *)cfmri, max_scf_fmri_size); 4647 reject: 4648 log_framework(LOG_WARNING, 4649 "Rejecting request for invalid milestone \"%s\".\n", 4650 fmri); 4651 return (EINVAL); 4652 } 4653 } 4654 4655 inst = safe_scf_instance_create(h); 4656 4657 MUTEX_LOCK(&dgraph_lock); 4658 4659 if (milestone == NULL) { 4660 if (isall) { 4661 log_framework(LOG_DEBUG, 4662 "Milestone already set to all.\n"); 4663 ret = EALREADY; 4664 goto out; 4665 } 4666 } else if (milestone == MILESTONE_NONE) { 4667 if (isnone) { 4668 log_framework(LOG_DEBUG, 4669 "Milestone already set to none.\n"); 4670 ret = EALREADY; 4671 goto out; 4672 } 4673 } else { 4674 if (!isall && !isnone && 4675 strcmp(cfmri, milestone->gv_name) == 0) { 4676 log_framework(LOG_DEBUG, 4677 "Milestone already set to %s.\n", cfmri); 4678 ret = EALREADY; 4679 goto out; 4680 } 4681 } 4682 4683 if (!isall && !isnone) { 4684 nm = vertex_get_by_name(cfmri); 4685 if (nm == NULL || !(nm->gv_flags & GV_CONFIGURED)) { 4686 log_framework(LOG_WARNING, "Cannot set milestone to %s " 4687 "because no such service exists.\n", cfmri); 4688 ret = ENOENT; 4689 goto out; 4690 } 4691 } 4692 4693 log_framework(LOG_DEBUG, "Changing milestone to %s.\n", fmri); 4694 4695 /* 4696 * Set milestone, removing the old one if this was the last reference. 4697 */ 4698 if (milestone > MILESTONE_NONE) 4699 (void) vertex_unref(milestone); 4700 4701 if (isall) 4702 milestone = NULL; 4703 else if (isnone) 4704 milestone = MILESTONE_NONE; 4705 else { 4706 milestone = nm; 4707 /* milestone should count as a reference */ 4708 vertex_ref(milestone); 4709 } 4710 4711 /* Clear all GV_INSUBGRAPH bits. */ 4712 for (v = uu_list_first(dgraph); v != NULL; v = uu_list_next(dgraph, v)) 4713 v->gv_flags &= ~GV_INSUBGRAPH; 4714 4715 if (!isall && !isnone) { 4716 /* Set GV_INSUBGRAPH for milestone & descendents. */ 4717 milestone->gv_flags |= GV_INSUBGRAPH; 4718 4719 r = uu_list_walk(milestone->gv_dependencies, 4720 (uu_walk_fn_t *)mark_subgraph, NULL, 0); 4721 assert(r == 0); 4722 } 4723 4724 /* Un-override services in the subgraph & override-disable the rest. */ 4725 if (norepository) 4726 goto out; 4727 4728 non_subgraph_svcs = 0; 4729 for (v = uu_list_first(dgraph); 4730 v != NULL; 4731 v = uu_list_next(dgraph, v)) { 4732 if (v->gv_type != GVT_INST || 4733 (v->gv_flags & GV_CONFIGURED) == 0) 4734 continue; 4735 4736 again: 4737 r = scf_handle_decode_fmri(h, v->gv_name, NULL, NULL, inst, 4738 NULL, NULL, SCF_DECODE_FMRI_EXACT); 4739 if (r != 0) { 4740 switch (scf_error()) { 4741 case SCF_ERROR_CONNECTION_BROKEN: 4742 default: 4743 libscf_handle_rebind(h); 4744 rebound = B_TRUE; 4745 goto again; 4746 4747 case SCF_ERROR_NOT_FOUND: 4748 continue; 4749 4750 case SCF_ERROR_HANDLE_MISMATCH: 4751 case SCF_ERROR_INVALID_ARGUMENT: 4752 case SCF_ERROR_CONSTRAINT_VIOLATED: 4753 case SCF_ERROR_NOT_BOUND: 4754 bad_error("scf_handle_decode_fmri", 4755 scf_error()); 4756 } 4757 } 4758 4759 if (isall || (v->gv_flags & GV_INSUBGRAPH)) { 4760 r = libscf_delete_enable_ovr(inst); 4761 fs = "libscf_delete_enable_ovr"; 4762 } else { 4763 assert(isnone || (v->gv_flags & GV_INSUBGRAPH) == 0); 4764 4765 if (inst_running(v)) 4766 ++non_subgraph_svcs; 4767 4768 if (has_running_nonsubgraph_dependents(v)) 4769 continue; 4770 4771 r = libscf_set_enable_ovr(inst, 0); 4772 fs = "libscf_set_enable_ovr"; 4773 } 4774 switch (r) { 4775 case 0: 4776 case ECANCELED: 4777 break; 4778 4779 case ECONNABORTED: 4780 libscf_handle_rebind(h); 4781 rebound = B_TRUE; 4782 goto again; 4783 4784 case EPERM: 4785 case EROFS: 4786 log_error(LOG_WARNING, 4787 "Could not set %s/%s for %s: %s.\n", 4788 SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED, 4789 v->gv_name, strerror(r)); 4790 break; 4791 4792 default: 4793 bad_error(fs, r); 4794 } 4795 } 4796 4797 if (halting != -1) { 4798 if (non_subgraph_svcs > 1) 4799 uu_warn("%d system services are now being stopped.\n", 4800 non_subgraph_svcs); 4801 else if (non_subgraph_svcs == 1) 4802 uu_warn("One system service is now being stopped.\n"); 4803 else if (non_subgraph_svcs == 0) 4804 do_uadmin(); 4805 } 4806 4807 ret = rebound ? ECONNRESET : 0; 4808 4809 out: 4810 MUTEX_UNLOCK(&dgraph_lock); 4811 if (!isall && !isnone) 4812 startd_free((void *)cfmri, max_scf_fmri_size); 4813 scf_instance_destroy(inst); 4814 return (ret); 4815 } 4816 4817 4818 /* 4819 * Returns 0, ECONNABORTED, or EINVAL. 4820 */ 4821 static int 4822 handle_graph_update_event(scf_handle_t *h, graph_protocol_event_t *e) 4823 { 4824 int r; 4825 4826 switch (e->gpe_type) { 4827 case GRAPH_UPDATE_RELOAD_GRAPH: 4828 log_error(LOG_WARNING, 4829 "graph_event: reload graph unimplemented\n"); 4830 break; 4831 4832 case GRAPH_UPDATE_STATE_CHANGE: { 4833 protocol_states_t *states = e->gpe_data; 4834 4835 switch (r = dgraph_set_instance_state(h, e->gpe_inst, 4836 states->ps_state, states->ps_err)) { 4837 case 0: 4838 case ENOENT: 4839 break; 4840 4841 case ECONNABORTED: 4842 return (ECONNABORTED); 4843 4844 case EINVAL: 4845 default: 4846 #ifndef NDEBUG 4847 (void) fprintf(stderr, "dgraph_set_instance_state() " 4848 "failed with unexpected error %d at %s:%d.\n", r, 4849 __FILE__, __LINE__); 4850 #endif 4851 abort(); 4852 } 4853 4854 startd_free(states, sizeof (protocol_states_t)); 4855 break; 4856 } 4857 4858 default: 4859 log_error(LOG_WARNING, 4860 "graph_event_loop received an unknown event: %d\n", 4861 e->gpe_type); 4862 break; 4863 } 4864 4865 return (0); 4866 } 4867 4868 /* 4869 * graph_event_thread() 4870 * Wait for state changes from the restarters. 4871 */ 4872 /*ARGSUSED*/ 4873 void * 4874 graph_event_thread(void *unused) 4875 { 4876 scf_handle_t *h; 4877 int err; 4878 4879 h = libscf_handle_create_bound_loop(); 4880 4881 /*CONSTCOND*/ 4882 while (1) { 4883 graph_protocol_event_t *e; 4884 4885 MUTEX_LOCK(&gu->gu_lock); 4886 4887 while (gu->gu_wakeup == 0) 4888 (void) pthread_cond_wait(&gu->gu_cv, &gu->gu_lock); 4889 4890 gu->gu_wakeup = 0; 4891 4892 while ((e = graph_event_dequeue()) != NULL) { 4893 MUTEX_LOCK(&e->gpe_lock); 4894 MUTEX_UNLOCK(&gu->gu_lock); 4895 4896 while ((err = handle_graph_update_event(h, e)) == 4897 ECONNABORTED) 4898 libscf_handle_rebind(h); 4899 4900 if (err == 0) 4901 graph_event_release(e); 4902 else 4903 graph_event_requeue(e); 4904 4905 MUTEX_LOCK(&gu->gu_lock); 4906 } 4907 4908 MUTEX_UNLOCK(&gu->gu_lock); 4909 } 4910 4911 /* 4912 * Unreachable for now -- there's currently no graceful cleanup 4913 * called on exit(). 4914 */ 4915 MUTEX_UNLOCK(&gu->gu_lock); 4916 scf_handle_destroy(h); 4917 return (NULL); 4918 } 4919 4920 static void 4921 set_initial_milestone(scf_handle_t *h) 4922 { 4923 scf_instance_t *inst; 4924 char *fmri, *cfmri; 4925 size_t sz; 4926 int r; 4927 4928 inst = safe_scf_instance_create(h); 4929 fmri = startd_alloc(max_scf_fmri_size); 4930 4931 /* 4932 * If -m milestone= was specified, we want to set options_ovr/milestone 4933 * to it. Otherwise we want to read what the milestone should be set 4934 * to. Either way we need our inst. 4935 */ 4936 get_self: 4937 if (scf_handle_decode_fmri(h, SCF_SERVICE_STARTD, NULL, NULL, inst, 4938 NULL, NULL, SCF_DECODE_FMRI_EXACT) != 0) { 4939 switch (scf_error()) { 4940 case SCF_ERROR_CONNECTION_BROKEN: 4941 libscf_handle_rebind(h); 4942 goto get_self; 4943 4944 case SCF_ERROR_NOT_FOUND: 4945 if (st->st_subgraph != NULL && 4946 st->st_subgraph[0] != '\0') { 4947 sz = strlcpy(fmri, st->st_subgraph, 4948 max_scf_fmri_size); 4949 assert(sz < max_scf_fmri_size); 4950 } else { 4951 fmri[0] = '\0'; 4952 } 4953 break; 4954 4955 case SCF_ERROR_INVALID_ARGUMENT: 4956 case SCF_ERROR_CONSTRAINT_VIOLATED: 4957 case SCF_ERROR_HANDLE_MISMATCH: 4958 default: 4959 bad_error("scf_handle_decode_fmri", scf_error()); 4960 } 4961 } else { 4962 if (st->st_subgraph != NULL && st->st_subgraph[0] != '\0') { 4963 scf_propertygroup_t *pg; 4964 4965 pg = safe_scf_pg_create(h); 4966 4967 sz = strlcpy(fmri, st->st_subgraph, max_scf_fmri_size); 4968 assert(sz < max_scf_fmri_size); 4969 4970 r = libscf_inst_get_or_add_pg(inst, SCF_PG_OPTIONS_OVR, 4971 SCF_PG_OPTIONS_OVR_TYPE, SCF_PG_OPTIONS_OVR_FLAGS, 4972 pg); 4973 switch (r) { 4974 case 0: 4975 break; 4976 4977 case ECONNABORTED: 4978 libscf_handle_rebind(h); 4979 goto get_self; 4980 4981 case EPERM: 4982 case EACCES: 4983 case EROFS: 4984 log_error(LOG_WARNING, "Could not set %s/%s: " 4985 "%s.\n", SCF_PG_OPTIONS_OVR, 4986 SCF_PROPERTY_MILESTONE, strerror(r)); 4987 /* FALLTHROUGH */ 4988 4989 case ECANCELED: 4990 sz = strlcpy(fmri, st->st_subgraph, 4991 max_scf_fmri_size); 4992 assert(sz < max_scf_fmri_size); 4993 break; 4994 4995 default: 4996 bad_error("libscf_inst_get_or_add_pg", r); 4997 } 4998 4999 r = libscf_clear_runlevel(pg, fmri); 5000 switch (r) { 5001 case 0: 5002 break; 5003 5004 case ECONNABORTED: 5005 libscf_handle_rebind(h); 5006 goto get_self; 5007 5008 case EPERM: 5009 case EACCES: 5010 case EROFS: 5011 log_error(LOG_WARNING, "Could not set %s/%s: " 5012 "%s.\n", SCF_PG_OPTIONS_OVR, 5013 SCF_PROPERTY_MILESTONE, strerror(r)); 5014 /* FALLTHROUGH */ 5015 5016 case ECANCELED: 5017 sz = strlcpy(fmri, st->st_subgraph, 5018 max_scf_fmri_size); 5019 assert(sz < max_scf_fmri_size); 5020 break; 5021 5022 default: 5023 bad_error("libscf_clear_runlevel", r); 5024 } 5025 5026 scf_pg_destroy(pg); 5027 } else { 5028 scf_property_t *prop; 5029 scf_value_t *val; 5030 5031 prop = safe_scf_property_create(h); 5032 val = safe_scf_value_create(h); 5033 5034 r = libscf_get_milestone(inst, prop, val, fmri, 5035 max_scf_fmri_size); 5036 switch (r) { 5037 case 0: 5038 break; 5039 5040 case ECONNABORTED: 5041 libscf_handle_rebind(h); 5042 goto get_self; 5043 5044 case EINVAL: 5045 log_error(LOG_WARNING, "Milestone property is " 5046 "misconfigured. Defaulting to \"all\".\n"); 5047 /* FALLTHROUGH */ 5048 5049 case ECANCELED: 5050 case ENOENT: 5051 fmri[0] = '\0'; 5052 break; 5053 5054 default: 5055 bad_error("libscf_get_milestone", r); 5056 } 5057 5058 scf_value_destroy(val); 5059 scf_property_destroy(prop); 5060 } 5061 } 5062 5063 if (fmri[0] == '\0' || strcmp(fmri, "all") == 0) 5064 goto out; 5065 5066 if (strcmp(fmri, "none") != 0) { 5067 retry: 5068 if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL, 5069 NULL, SCF_DECODE_FMRI_EXACT) != 0) { 5070 switch (scf_error()) { 5071 case SCF_ERROR_INVALID_ARGUMENT: 5072 log_error(LOG_WARNING, 5073 "Requested milestone \"%s\" is invalid. " 5074 "Reverting to \"all\".\n", fmri); 5075 goto out; 5076 5077 case SCF_ERROR_CONSTRAINT_VIOLATED: 5078 log_error(LOG_WARNING, "Requested milestone " 5079 "\"%s\" does not specify an instance. " 5080 "Reverting to \"all\".\n", fmri); 5081 goto out; 5082 5083 case SCF_ERROR_CONNECTION_BROKEN: 5084 libscf_handle_rebind(h); 5085 goto retry; 5086 5087 case SCF_ERROR_NOT_FOUND: 5088 log_error(LOG_WARNING, "Requested milestone " 5089 "\"%s\" not in repository. Reverting to " 5090 "\"all\".\n", fmri); 5091 goto out; 5092 5093 case SCF_ERROR_HANDLE_MISMATCH: 5094 default: 5095 bad_error("scf_handle_decode_fmri", 5096 scf_error()); 5097 } 5098 } 5099 5100 r = fmri_canonify(fmri, &cfmri, B_FALSE); 5101 assert(r == 0); 5102 5103 r = dgraph_add_instance(cfmri, inst, B_TRUE); 5104 startd_free(cfmri, max_scf_fmri_size); 5105 switch (r) { 5106 case 0: 5107 break; 5108 5109 case ECONNABORTED: 5110 goto retry; 5111 5112 case EINVAL: 5113 log_error(LOG_WARNING, 5114 "Requested milestone \"%s\" is invalid. " 5115 "Reverting to \"all\".\n", fmri); 5116 goto out; 5117 5118 case ECANCELED: 5119 log_error(LOG_WARNING, 5120 "Requested milestone \"%s\" not " 5121 "in repository. Reverting to \"all\".\n", 5122 fmri); 5123 goto out; 5124 5125 case EEXIST: 5126 default: 5127 bad_error("dgraph_add_instance", r); 5128 } 5129 } 5130 5131 log_console(LOG_INFO, "Booting to milestone \"%s\".\n", fmri); 5132 5133 r = dgraph_set_milestone(fmri, h, B_FALSE); 5134 switch (r) { 5135 case 0: 5136 case ECONNRESET: 5137 case EALREADY: 5138 break; 5139 5140 case EINVAL: 5141 case ENOENT: 5142 default: 5143 bad_error("dgraph_set_milestone", r); 5144 } 5145 5146 out: 5147 startd_free(fmri, max_scf_fmri_size); 5148 scf_instance_destroy(inst); 5149 } 5150 5151 void 5152 set_restart_milestone(scf_handle_t *h) 5153 { 5154 scf_instance_t *inst; 5155 scf_property_t *prop; 5156 scf_value_t *val; 5157 char *fmri; 5158 int r; 5159 5160 inst = safe_scf_instance_create(h); 5161 5162 get_self: 5163 if (scf_handle_decode_fmri(h, SCF_SERVICE_STARTD, NULL, NULL, 5164 inst, NULL, NULL, SCF_DECODE_FMRI_EXACT) != 0) { 5165 switch (scf_error()) { 5166 case SCF_ERROR_CONNECTION_BROKEN: 5167 libscf_handle_rebind(h); 5168 goto get_self; 5169 5170 case SCF_ERROR_NOT_FOUND: 5171 break; 5172 5173 case SCF_ERROR_INVALID_ARGUMENT: 5174 case SCF_ERROR_CONSTRAINT_VIOLATED: 5175 case SCF_ERROR_HANDLE_MISMATCH: 5176 default: 5177 bad_error("scf_handle_decode_fmri", scf_error()); 5178 } 5179 5180 scf_instance_destroy(inst); 5181 return; 5182 } 5183 5184 prop = safe_scf_property_create(h); 5185 val = safe_scf_value_create(h); 5186 fmri = startd_alloc(max_scf_fmri_size); 5187 5188 r = libscf_get_milestone(inst, prop, val, fmri, max_scf_fmri_size); 5189 switch (r) { 5190 case 0: 5191 break; 5192 5193 case ECONNABORTED: 5194 libscf_handle_rebind(h); 5195 goto get_self; 5196 5197 case ECANCELED: 5198 case ENOENT: 5199 case EINVAL: 5200 goto out; 5201 5202 default: 5203 bad_error("libscf_get_milestone", r); 5204 } 5205 5206 r = dgraph_set_milestone(fmri, h, B_TRUE); 5207 switch (r) { 5208 case 0: 5209 case ECONNRESET: 5210 case EALREADY: 5211 case EINVAL: 5212 case ENOENT: 5213 break; 5214 5215 default: 5216 bad_error("dgraph_set_milestone", r); 5217 } 5218 5219 out: 5220 startd_free(fmri, max_scf_fmri_size); 5221 scf_value_destroy(val); 5222 scf_property_destroy(prop); 5223 scf_instance_destroy(inst); 5224 } 5225 5226 /* 5227 * void *graph_thread(void *) 5228 * 5229 * Graph management thread. 5230 */ 5231 /*ARGSUSED*/ 5232 void * 5233 graph_thread(void *arg) 5234 { 5235 scf_handle_t *h; 5236 int err; 5237 5238 h = libscf_handle_create_bound_loop(); 5239 5240 if (st->st_initial) 5241 set_initial_milestone(h); 5242 5243 MUTEX_LOCK(&dgraph_lock); 5244 initial_milestone_set = B_TRUE; 5245 err = pthread_cond_broadcast(&initial_milestone_cv); 5246 assert(err == 0); 5247 MUTEX_UNLOCK(&dgraph_lock); 5248 5249 libscf_populate_graph(h); 5250 5251 if (!st->st_initial) 5252 set_restart_milestone(h); 5253 5254 MUTEX_LOCK(&st->st_load_lock); 5255 st->st_load_complete = 1; 5256 (void) pthread_cond_broadcast(&st->st_load_cv); 5257 MUTEX_UNLOCK(&st->st_load_lock); 5258 5259 MUTEX_LOCK(&dgraph_lock); 5260 /* 5261 * Now that we've set st_load_complete we need to check can_come_up() 5262 * since if we booted to a milestone, then there won't be any more 5263 * state updates. 5264 */ 5265 if (!go_single_user_mode && !go_to_level1 && 5266 halting == -1) { 5267 if (!can_come_up() && !sulogin_thread_running) { 5268 (void) startd_thread_create(sulogin_thread, NULL); 5269 sulogin_thread_running = B_TRUE; 5270 } 5271 } 5272 MUTEX_UNLOCK(&dgraph_lock); 5273 5274 (void) pthread_mutex_lock(&gu->gu_freeze_lock); 5275 5276 /*CONSTCOND*/ 5277 while (1) { 5278 (void) pthread_cond_wait(&gu->gu_freeze_cv, 5279 &gu->gu_freeze_lock); 5280 } 5281 5282 /* 5283 * Unreachable for now -- there's currently no graceful cleanup 5284 * called on exit(). 5285 */ 5286 (void) pthread_mutex_unlock(&gu->gu_freeze_lock); 5287 scf_handle_destroy(h); 5288 5289 return (NULL); 5290 } 5291 5292 5293 /* 5294 * int next_action() 5295 * Given an array of timestamps 'a' with 'num' elements, find the 5296 * lowest non-zero timestamp and return its index. If there are no 5297 * non-zero elements, return -1. 5298 */ 5299 static int 5300 next_action(hrtime_t *a, int num) 5301 { 5302 hrtime_t t = 0; 5303 int i = 0, smallest = -1; 5304 5305 for (i = 0; i < num; i++) { 5306 if (t == 0) { 5307 t = a[i]; 5308 smallest = i; 5309 } else if (a[i] != 0 && a[i] < t) { 5310 t = a[i]; 5311 smallest = i; 5312 } 5313 } 5314 5315 if (t == 0) 5316 return (-1); 5317 else 5318 return (smallest); 5319 } 5320 5321 /* 5322 * void process_actions() 5323 * Process actions requested by the administrator. Possibilities include: 5324 * refresh, restart, maintenance mode off, maintenance mode on, 5325 * maintenance mode immediate, and degraded. 5326 * 5327 * The set of pending actions is represented in the repository as a 5328 * per-instance property group, with each action being a single property 5329 * in that group. This property group is converted to an array, with each 5330 * action type having an array slot. The actions in the array at the 5331 * time process_actions() is called are acted on in the order of the 5332 * timestamp (which is the value stored in the slot). A value of zero 5333 * indicates that there is no pending action of the type associated with 5334 * a particular slot. 5335 * 5336 * Sending an action event multiple times before the restarter has a 5337 * chance to process that action will force it to be run at the last 5338 * timestamp where it appears in the ordering. 5339 * 5340 * Turning maintenance mode on trumps all other actions. 5341 * 5342 * Returns 0 or ECONNABORTED. 5343 */ 5344 static int 5345 process_actions(scf_handle_t *h, scf_propertygroup_t *pg, scf_instance_t *inst) 5346 { 5347 scf_property_t *prop = NULL; 5348 scf_value_t *val = NULL; 5349 scf_type_t type; 5350 graph_vertex_t *vertex; 5351 admin_action_t a; 5352 int i, ret = 0, r; 5353 hrtime_t action_ts[NACTIONS]; 5354 char *inst_name; 5355 5356 r = libscf_instance_get_fmri(inst, &inst_name); 5357 switch (r) { 5358 case 0: 5359 break; 5360 5361 case ECONNABORTED: 5362 return (ECONNABORTED); 5363 5364 case ECANCELED: 5365 return (0); 5366 5367 default: 5368 bad_error("libscf_instance_get_fmri", r); 5369 } 5370 5371 MUTEX_LOCK(&dgraph_lock); 5372 5373 vertex = vertex_get_by_name(inst_name); 5374 if (vertex == NULL) { 5375 MUTEX_UNLOCK(&dgraph_lock); 5376 startd_free(inst_name, max_scf_fmri_size); 5377 log_framework(LOG_DEBUG, "%s: Can't find graph vertex. " 5378 "The instance must have been removed.\n", inst_name); 5379 return (0); 5380 } 5381 5382 prop = safe_scf_property_create(h); 5383 val = safe_scf_value_create(h); 5384 5385 for (i = 0; i < NACTIONS; i++) { 5386 if (scf_pg_get_property(pg, admin_actions[i], prop) != 0) { 5387 switch (scf_error()) { 5388 case SCF_ERROR_CONNECTION_BROKEN: 5389 default: 5390 ret = ECONNABORTED; 5391 goto out; 5392 5393 case SCF_ERROR_DELETED: 5394 goto out; 5395 5396 case SCF_ERROR_NOT_FOUND: 5397 action_ts[i] = 0; 5398 continue; 5399 5400 case SCF_ERROR_HANDLE_MISMATCH: 5401 case SCF_ERROR_INVALID_ARGUMENT: 5402 case SCF_ERROR_NOT_SET: 5403 bad_error("scf_pg_get_property", scf_error()); 5404 } 5405 } 5406 5407 if (scf_property_type(prop, &type) != 0) { 5408 switch (scf_error()) { 5409 case SCF_ERROR_CONNECTION_BROKEN: 5410 default: 5411 ret = ECONNABORTED; 5412 goto out; 5413 5414 case SCF_ERROR_DELETED: 5415 action_ts[i] = 0; 5416 continue; 5417 5418 case SCF_ERROR_NOT_SET: 5419 bad_error("scf_property_type", scf_error()); 5420 } 5421 } 5422 5423 if (type != SCF_TYPE_INTEGER) { 5424 action_ts[i] = 0; 5425 continue; 5426 } 5427 5428 if (scf_property_get_value(prop, val) != 0) { 5429 switch (scf_error()) { 5430 case SCF_ERROR_CONNECTION_BROKEN: 5431 default: 5432 ret = ECONNABORTED; 5433 goto out; 5434 5435 case SCF_ERROR_DELETED: 5436 goto out; 5437 5438 case SCF_ERROR_NOT_FOUND: 5439 case SCF_ERROR_CONSTRAINT_VIOLATED: 5440 action_ts[i] = 0; 5441 continue; 5442 5443 case SCF_ERROR_NOT_SET: 5444 bad_error("scf_property_get_value", 5445 scf_error()); 5446 } 5447 } 5448 5449 r = scf_value_get_integer(val, &action_ts[i]); 5450 assert(r == 0); 5451 } 5452 5453 a = ADMIN_EVENT_MAINT_ON_IMMEDIATE; 5454 if (action_ts[ADMIN_EVENT_MAINT_ON_IMMEDIATE] || 5455 action_ts[ADMIN_EVENT_MAINT_ON]) { 5456 a = action_ts[ADMIN_EVENT_MAINT_ON_IMMEDIATE] ? 5457 ADMIN_EVENT_MAINT_ON_IMMEDIATE : ADMIN_EVENT_MAINT_ON; 5458 5459 vertex_send_event(vertex, admin_events[a]); 5460 r = libscf_unset_action(h, pg, a, action_ts[a]); 5461 switch (r) { 5462 case 0: 5463 case EACCES: 5464 break; 5465 5466 case ECONNABORTED: 5467 ret = ECONNABORTED; 5468 goto out; 5469 5470 case EPERM: 5471 uu_die("Insufficient privilege.\n"); 5472 /* NOTREACHED */ 5473 5474 default: 5475 bad_error("libscf_unset_action", r); 5476 } 5477 } 5478 5479 while ((a = next_action(action_ts, NACTIONS)) != -1) { 5480 log_framework(LOG_DEBUG, 5481 "Graph: processing %s action for %s.\n", admin_actions[a], 5482 inst_name); 5483 5484 if (a == ADMIN_EVENT_REFRESH) { 5485 r = dgraph_refresh_instance(vertex, inst); 5486 switch (r) { 5487 case 0: 5488 case ECANCELED: 5489 case EINVAL: 5490 case -1: 5491 break; 5492 5493 case ECONNABORTED: 5494 /* pg & inst are reset now, so just return. */ 5495 ret = ECONNABORTED; 5496 goto out; 5497 5498 default: 5499 bad_error("dgraph_refresh_instance", r); 5500 } 5501 } 5502 5503 vertex_send_event(vertex, admin_events[a]); 5504 5505 r = libscf_unset_action(h, pg, a, action_ts[a]); 5506 switch (r) { 5507 case 0: 5508 case EACCES: 5509 break; 5510 5511 case ECONNABORTED: 5512 ret = ECONNABORTED; 5513 goto out; 5514 5515 case EPERM: 5516 uu_die("Insufficient privilege.\n"); 5517 /* NOTREACHED */ 5518 5519 default: 5520 bad_error("libscf_unset_action", r); 5521 } 5522 5523 action_ts[a] = 0; 5524 } 5525 5526 out: 5527 MUTEX_UNLOCK(&dgraph_lock); 5528 5529 scf_property_destroy(prop); 5530 scf_value_destroy(val); 5531 startd_free(inst_name, max_scf_fmri_size); 5532 return (ret); 5533 } 5534 5535 /* 5536 * inst and pg_name are scratch space, and are unset on entry. 5537 * Returns 5538 * 0 - success 5539 * ECONNRESET - success, but repository handle rebound 5540 * ECONNABORTED - repository connection broken 5541 */ 5542 static int 5543 process_pg_event(scf_handle_t *h, scf_propertygroup_t *pg, scf_instance_t *inst, 5544 char *pg_name) 5545 { 5546 int r; 5547 scf_property_t *prop; 5548 scf_value_t *val; 5549 char *fmri; 5550 boolean_t rebound = B_FALSE, rebind_inst = B_FALSE; 5551 5552 if (scf_pg_get_name(pg, pg_name, max_scf_value_size) < 0) { 5553 switch (scf_error()) { 5554 case SCF_ERROR_CONNECTION_BROKEN: 5555 default: 5556 return (ECONNABORTED); 5557 5558 case SCF_ERROR_DELETED: 5559 return (0); 5560 5561 case SCF_ERROR_NOT_SET: 5562 bad_error("scf_pg_get_name", scf_error()); 5563 } 5564 } 5565 5566 if (strcmp(pg_name, SCF_PG_GENERAL) == 0 || 5567 strcmp(pg_name, SCF_PG_GENERAL_OVR) == 0) { 5568 r = dgraph_update_general(pg); 5569 switch (r) { 5570 case 0: 5571 case ENOTSUP: 5572 case ECANCELED: 5573 return (0); 5574 5575 case ECONNABORTED: 5576 return (ECONNABORTED); 5577 5578 case -1: 5579 /* Error should have been logged. */ 5580 return (0); 5581 5582 default: 5583 bad_error("dgraph_update_general", r); 5584 } 5585 } else if (strcmp(pg_name, SCF_PG_RESTARTER_ACTIONS) == 0) { 5586 if (scf_pg_get_parent_instance(pg, inst) != 0) { 5587 switch (scf_error()) { 5588 case SCF_ERROR_CONNECTION_BROKEN: 5589 return (ECONNABORTED); 5590 5591 case SCF_ERROR_DELETED: 5592 case SCF_ERROR_CONSTRAINT_VIOLATED: 5593 /* Ignore commands on services. */ 5594 return (0); 5595 5596 case SCF_ERROR_NOT_BOUND: 5597 case SCF_ERROR_HANDLE_MISMATCH: 5598 case SCF_ERROR_NOT_SET: 5599 default: 5600 bad_error("scf_pg_get_parent_instance", 5601 scf_error()); 5602 } 5603 } 5604 5605 return (process_actions(h, pg, inst)); 5606 } 5607 5608 if (strcmp(pg_name, SCF_PG_OPTIONS) != 0 && 5609 strcmp(pg_name, SCF_PG_OPTIONS_OVR) != 0) 5610 return (0); 5611 5612 /* 5613 * We only care about the options[_ovr] property groups of our own 5614 * instance, so get the fmri and compare. Plus, once we know it's 5615 * correct, if the repository connection is broken we know exactly what 5616 * property group we were operating on, and can look it up again. 5617 */ 5618 if (scf_pg_get_parent_instance(pg, inst) != 0) { 5619 switch (scf_error()) { 5620 case SCF_ERROR_CONNECTION_BROKEN: 5621 return (ECONNABORTED); 5622 5623 case SCF_ERROR_DELETED: 5624 case SCF_ERROR_CONSTRAINT_VIOLATED: 5625 return (0); 5626 5627 case SCF_ERROR_HANDLE_MISMATCH: 5628 case SCF_ERROR_NOT_BOUND: 5629 case SCF_ERROR_NOT_SET: 5630 default: 5631 bad_error("scf_pg_get_parent_instance", 5632 scf_error()); 5633 } 5634 } 5635 5636 switch (r = libscf_instance_get_fmri(inst, &fmri)) { 5637 case 0: 5638 break; 5639 5640 case ECONNABORTED: 5641 return (ECONNABORTED); 5642 5643 case ECANCELED: 5644 return (0); 5645 5646 default: 5647 bad_error("libscf_instance_get_fmri", r); 5648 } 5649 5650 if (strcmp(fmri, SCF_SERVICE_STARTD) != 0) { 5651 startd_free(fmri, max_scf_fmri_size); 5652 return (0); 5653 } 5654 5655 prop = safe_scf_property_create(h); 5656 val = safe_scf_value_create(h); 5657 5658 if (strcmp(pg_name, SCF_PG_OPTIONS_OVR) == 0) { 5659 /* See if we need to set the runlevel. */ 5660 /* CONSTCOND */ 5661 if (0) { 5662 rebind_pg: 5663 libscf_handle_rebind(h); 5664 rebound = B_TRUE; 5665 5666 r = libscf_lookup_instance(SCF_SERVICE_STARTD, inst); 5667 switch (r) { 5668 case 0: 5669 break; 5670 5671 case ECONNABORTED: 5672 goto rebind_pg; 5673 5674 case ENOENT: 5675 goto out; 5676 5677 case EINVAL: 5678 case ENOTSUP: 5679 bad_error("libscf_lookup_instance", r); 5680 } 5681 5682 if (scf_instance_get_pg(inst, pg_name, pg) != 0) { 5683 switch (scf_error()) { 5684 case SCF_ERROR_DELETED: 5685 case SCF_ERROR_NOT_FOUND: 5686 goto out; 5687 5688 case SCF_ERROR_CONNECTION_BROKEN: 5689 goto rebind_pg; 5690 5691 case SCF_ERROR_HANDLE_MISMATCH: 5692 case SCF_ERROR_NOT_BOUND: 5693 case SCF_ERROR_NOT_SET: 5694 case SCF_ERROR_INVALID_ARGUMENT: 5695 default: 5696 bad_error("scf_instance_get_pg", 5697 scf_error()); 5698 } 5699 } 5700 } 5701 5702 if (scf_pg_get_property(pg, "runlevel", prop) == 0) { 5703 r = dgraph_set_runlevel(pg, prop); 5704 switch (r) { 5705 case ECONNRESET: 5706 rebound = B_TRUE; 5707 rebind_inst = B_TRUE; 5708 /* FALLTHROUGH */ 5709 5710 case 0: 5711 break; 5712 5713 case ECONNABORTED: 5714 goto rebind_pg; 5715 5716 case ECANCELED: 5717 goto out; 5718 5719 default: 5720 bad_error("dgraph_set_runlevel", r); 5721 } 5722 } else { 5723 switch (scf_error()) { 5724 case SCF_ERROR_CONNECTION_BROKEN: 5725 default: 5726 goto rebind_pg; 5727 5728 case SCF_ERROR_DELETED: 5729 goto out; 5730 5731 case SCF_ERROR_NOT_FOUND: 5732 break; 5733 5734 case SCF_ERROR_INVALID_ARGUMENT: 5735 case SCF_ERROR_HANDLE_MISMATCH: 5736 case SCF_ERROR_NOT_BOUND: 5737 case SCF_ERROR_NOT_SET: 5738 bad_error("scf_pg_get_property", scf_error()); 5739 } 5740 } 5741 } 5742 5743 if (rebind_inst) { 5744 lookup_inst: 5745 r = libscf_lookup_instance(SCF_SERVICE_STARTD, inst); 5746 switch (r) { 5747 case 0: 5748 break; 5749 5750 case ECONNABORTED: 5751 libscf_handle_rebind(h); 5752 rebound = B_TRUE; 5753 goto lookup_inst; 5754 5755 case ENOENT: 5756 goto out; 5757 5758 case EINVAL: 5759 case ENOTSUP: 5760 bad_error("libscf_lookup_instance", r); 5761 } 5762 } 5763 5764 r = libscf_get_milestone(inst, prop, val, fmri, max_scf_fmri_size); 5765 switch (r) { 5766 case 0: 5767 break; 5768 5769 case ECONNABORTED: 5770 libscf_handle_rebind(h); 5771 rebound = B_TRUE; 5772 goto lookup_inst; 5773 5774 case EINVAL: 5775 log_error(LOG_NOTICE, 5776 "%s/%s property of %s is misconfigured.\n", pg_name, 5777 SCF_PROPERTY_MILESTONE, SCF_SERVICE_STARTD); 5778 /* FALLTHROUGH */ 5779 5780 case ECANCELED: 5781 case ENOENT: 5782 (void) strcpy(fmri, "all"); 5783 break; 5784 5785 default: 5786 bad_error("libscf_get_milestone", r); 5787 } 5788 5789 r = dgraph_set_milestone(fmri, h, B_FALSE); 5790 switch (r) { 5791 case 0: 5792 case ECONNRESET: 5793 case EALREADY: 5794 break; 5795 5796 case EINVAL: 5797 log_error(LOG_WARNING, "Milestone %s is invalid.\n", fmri); 5798 break; 5799 5800 case ENOENT: 5801 log_error(LOG_WARNING, "Milestone %s does not exist.\n", fmri); 5802 break; 5803 5804 default: 5805 bad_error("dgraph_set_milestone", r); 5806 } 5807 5808 out: 5809 startd_free(fmri, max_scf_fmri_size); 5810 scf_value_destroy(val); 5811 scf_property_destroy(prop); 5812 5813 return (rebound ? ECONNRESET : 0); 5814 } 5815 5816 static void 5817 process_delete(char *fmri, scf_handle_t *h) 5818 { 5819 char *lfmri; 5820 const char *inst_name, *pg_name; 5821 5822 lfmri = safe_strdup(fmri); 5823 5824 /* Determine if the FMRI is a property group or instance */ 5825 if (scf_parse_svc_fmri(lfmri, NULL, NULL, &inst_name, &pg_name, 5826 NULL) != SCF_SUCCESS) { 5827 log_error(LOG_WARNING, 5828 "Received invalid FMRI \"%s\" from repository server.\n", 5829 fmri); 5830 } else if (inst_name != NULL && pg_name == NULL) { 5831 (void) dgraph_remove_instance(fmri, h); 5832 } 5833 5834 free(lfmri); 5835 } 5836 5837 /*ARGSUSED*/ 5838 void * 5839 repository_event_thread(void *unused) 5840 { 5841 scf_handle_t *h; 5842 scf_propertygroup_t *pg; 5843 scf_instance_t *inst; 5844 char *fmri = startd_alloc(max_scf_fmri_size); 5845 char *pg_name = startd_alloc(max_scf_value_size); 5846 int r; 5847 5848 h = libscf_handle_create_bound_loop(); 5849 5850 pg = safe_scf_pg_create(h); 5851 inst = safe_scf_instance_create(h); 5852 5853 retry: 5854 if (_scf_notify_add_pgtype(h, SCF_GROUP_FRAMEWORK) != SCF_SUCCESS) { 5855 if (scf_error() == SCF_ERROR_CONNECTION_BROKEN) { 5856 libscf_handle_rebind(h); 5857 } else { 5858 log_error(LOG_WARNING, 5859 "Couldn't set up repository notification " 5860 "for property group type %s: %s\n", 5861 SCF_GROUP_FRAMEWORK, scf_strerror(scf_error())); 5862 5863 (void) sleep(1); 5864 } 5865 5866 goto retry; 5867 } 5868 5869 /*CONSTCOND*/ 5870 while (1) { 5871 ssize_t res; 5872 5873 /* Note: fmri is only set on delete events. */ 5874 res = _scf_notify_wait(pg, fmri, max_scf_fmri_size); 5875 if (res < 0) { 5876 libscf_handle_rebind(h); 5877 goto retry; 5878 } else if (res == 0) { 5879 /* 5880 * property group modified. inst and pg_name are 5881 * pre-allocated scratch space. 5882 */ 5883 if (scf_pg_update(pg) < 0) { 5884 switch (scf_error()) { 5885 case SCF_ERROR_DELETED: 5886 continue; 5887 5888 case SCF_ERROR_CONNECTION_BROKEN: 5889 log_error(LOG_WARNING, 5890 "Lost repository event due to " 5891 "disconnection.\n"); 5892 libscf_handle_rebind(h); 5893 goto retry; 5894 5895 case SCF_ERROR_NOT_BOUND: 5896 case SCF_ERROR_NOT_SET: 5897 default: 5898 bad_error("scf_pg_update", scf_error()); 5899 } 5900 } 5901 5902 r = process_pg_event(h, pg, inst, pg_name); 5903 switch (r) { 5904 case 0: 5905 break; 5906 5907 case ECONNABORTED: 5908 log_error(LOG_WARNING, "Lost repository event " 5909 "due to disconnection.\n"); 5910 libscf_handle_rebind(h); 5911 /* FALLTHROUGH */ 5912 5913 case ECONNRESET: 5914 goto retry; 5915 5916 default: 5917 bad_error("process_pg_event", r); 5918 } 5919 } else { 5920 /* service, instance, or pg deleted. */ 5921 process_delete(fmri, h); 5922 } 5923 } 5924 5925 /*NOTREACHED*/ 5926 return (NULL); 5927 } 5928 5929 void 5930 graph_engine_start() 5931 { 5932 int err; 5933 5934 (void) startd_thread_create(graph_thread, NULL); 5935 5936 MUTEX_LOCK(&dgraph_lock); 5937 while (!initial_milestone_set) { 5938 err = pthread_cond_wait(&initial_milestone_cv, &dgraph_lock); 5939 assert(err == 0); 5940 } 5941 MUTEX_UNLOCK(&dgraph_lock); 5942 5943 (void) startd_thread_create(repository_event_thread, NULL); 5944 (void) startd_thread_create(graph_event_thread, NULL); 5945 } 5946