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