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