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