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