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