graph.c revision 3efb42ec6e35fd13783a0ff5ba7b73acc8c8ed0e
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* graph.c - master restarter graph engine
*
* The graph engine keeps a dependency graph of all service instances on the
* system, as recorded in the repository. It decides when services should
* be brought up or down based on service states and dependencies and sends
* commands to restarters to effect any changes. It also executes
* administrator commands sent by svcadm via the repository.
*
* The graph is stored in uu_list_t *dgraph and its vertices are
* graph_vertex_t's, each of which has a name and an integer id unique to
* its name (see dict.c). A vertex's type attribute designates the type
* of object it represents: GVT_INST for service instances, GVT_SVC for
* service objects (since service instances may depend on another service,
* rather than service instance), GVT_FILE for files (which services may
* depend on), and GVT_GROUP for dependencies on multiple objects. GVT_GROUP
* vertices are necessary because dependency lists may have particular
* grouping types (require any, require all, optional, or exclude) and
* event-propagation characteristics.
*
* The initial graph is built by libscf_populate_graph() invoking
* dgraph_add_instance() for each instance in the repository. The function
* adds a GVT_SVC vertex for the service if one does not already exist, adds
* a GVT_INST vertex named by the FMRI of the instance, and sets up the edges.
* The resulting web of vertices & edges associated with an instance's vertex
* includes
*
* - an edge from the GVT_SVC vertex for the instance's service
*
* - an edge to the GVT_INST vertex of the instance's resarter, if its
* restarter is not svc.startd
*
* - edges from other GVT_INST vertices if the instance is a restarter
*
* - for each dependency property group in the instance's "running"
* snapshot, an edge to a GVT_GROUP vertex named by the FMRI of the
* instance and the name of the property group
*
* - for each value of the "entities" property in each dependency property
* group, an edge from the corresponding GVT_GROUP vertex to a
* GVT_INST, GVT_SVC, or GVT_FILE vertex
*
* - edges from GVT_GROUP vertices for each dependent instance
*
* After the edges are set up the vertex's GV_CONFIGURED flag is set. If
* there are problems, or if a service is mentioned in a dependency but does
* not exist in the repository, the GV_CONFIGURED flag will be clear.
*
* The graph and all of its vertices are protected by the dgraph_lock mutex.
* See restarter.c for more information.
*
* The properties of an instance fall into two classes: immediate and
* snapshotted. Immediate properties should have an immediate effect when
* changed. Snapshotted properties should be read from a snapshot, so they
* only change when the snapshot changes. The immediate properties used by
* in the restarter_actions property group. Since they are immediate, they
* are not read out of a snapshot. The snapshotted properties used by the
* graph engine are those in the property groups with type "dependency" and
* are read out of the "running" snapshot. The "running" snapshot is created
* by the the graph engine as soon as possible, and it is updated, along with
* in-core copies of the data (dependency information for the graph engine) on
* receipt of the refresh command from svcadm. In addition, the graph engine
* updates the "start" snapshot from the "running" snapshot whenever a service
* comes online.
*/
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <libscf.h>
#include <libscf_priv.h>
#include <libuutil.h>
#include <locale.h>
#include <poll.h>
#include <pthread.h>
#include <signal.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <zone.h>
#include "startd.h"
#include "protocol.h"
#define CONSOLE_LOGIN_FMRI "svc:/system/console-login:default"
#define FS_MINIMAL_FMRI "svc:/system/filesystem/minimal:default"
#define VERTEX_REMOVED 0 /* vertex has been freed */
/*
* Services in these states are not considered 'down' by the
*/
(state) == RESTARTER_STATE_DEGRADED || \
(state) == RESTARTER_STATE_OFFLINE)
static pthread_mutex_t dgraph_lock;
/*
* milestone indicates the current subgraph. When NULL, it is the entire
* graph. When MILESTONE_NONE, it is the empty graph. Otherwise, it is all
* services on which the target vertex depends.
*/
/* protected by dgraph_lock */
/* Number of services to come down to complete milestone transition. */
static uint_t non_subgraph_svcs;
/*
* These variables indicate what should be done when we reach the milestone
* target milestone, i.e., when non_subgraph_svcs == 0. They are acted upon in
* dgraph_set_instance_state().
*/
static int halting = -1;
/*
* This tracks the legacy runlevel to ensure we signal init and manage
* utmpx entries correctly.
*/
static char current_runlevel = '\0';
/* Number of single user threads currently running */
static int single_user_thread_count = 0;
/* Statistics for dependency cycle-checking */
static u_longlong_t dep_inserts = 0;
static u_longlong_t dep_cycle_ns = 0;
static u_longlong_t dep_insert_ns = 0;
static const char * const emsg_invalid_restarter =
"Transitioning %s to maintenance, restarter FMRI %s is invalid "
"(see 'svcs -xv' for details).\n";
static const char * const console_login_fmri = CONSOLE_LOGIN_FMRI;
static const char * const single_user_fmri = SCF_MILESTONE_SINGLE_USER;
static const char * const multi_user_fmri = SCF_MILESTONE_MULTI_USER;
static const char * const multi_user_svr_fmri = SCF_MILESTONE_MULTI_USER_SERVER;
/*
* These services define the system being "up". If none of them can come
* online, then we will run sulogin on the console. Note that the install ones
* are for the miniroot and when installing CDs after the first. can_come_up()
* does the decision making, and an sulogin_thread() runs sulogin, which can be
* started by dgraph_set_instance_state() or single_user_thread().
*
* NOTE: can_come_up() relies on SCF_MILESTONE_SINGLE_USER being the first
* entry, which is only used when booting_to_single_user (boot -s) is set.
* This is because when doing a "boot -s", sulogin is started from specials.c
* after milestone/single-user comes online, for backwards compatibility.
* In this case, SCF_MILESTONE_SINGLE_USER needs to be part of up_svcs
* to ensure sulogin will be spawned if milestone/single-user cannot be reached.
*/
static const char * const up_svcs[] = {
"svc:/system/install-setup:default",
};
/* This array must have an element for each non-NULL element of up_svcs[]. */
/* These are for seed repository magic. See can_come_up(). */
static const char * const manifest_import =
"svc:/system/manifest-import:default";
static char target_milestone_as_runlevel(void);
/*
* graph_vertex_compare()
* This function can compare either int *id or * graph_vertex_t *gv
* values, as the vertex id is always the first element of a
* graph_vertex structure.
*/
/* ARGSUSED */
static int
{
return (1);
return (-1);
return (0);
}
void
{
if (!st->st_initial)
}
static graph_vertex_t *
vertex_get_by_name(const char *name)
{
int id;
if (id == -1)
return (NULL);
}
static graph_vertex_t *
vertex_get_by_id(int id)
{
if (id == -1)
return (NULL);
}
/*
* Creates a new vertex with the given name, adds it to the graph, and returns
* a pointer to it. The graph lock must be held by this thread on entry.
*/
static graph_vertex_t *
graph_add_vertex(const char *name)
{
int id;
graph_vertex_t *v;
void *p;
v = startd_zalloc(sizeof (*v));
return (v);
}
/*
* Removes v from the graph and frees it. The graph should be locked by this
* thread, and v should have no edges associated with it.
*/
static void
{
uu_list_remove(dgraph, v);
startd_free(v, sizeof (graph_vertex_t));
}
static void
{
graph_edge_t *e, *re;
int r;
e = startd_alloc(sizeof (graph_edge_t));
assert(r == 0);
assert(r == 0);
}
static void
{
graph_edge_t *e;
for (e = uu_list_first(v->gv_dependencies);
e != NULL;
e = uu_list_next(v->gv_dependencies, e)) {
uu_list_remove(v->gv_dependencies, e);
startd_free(e, sizeof (graph_edge_t));
break;
}
}
e != NULL;
if (e->ge_vertex == v) {
startd_free(e, sizeof (graph_edge_t));
break;
}
}
}
static void
{
graph_edge_t *e;
int i;
e = uu_list_first(v->gv_dependents);
graph_remove_edge(sv, v);
if (up_svcs_p[i] == v)
}
if (manifest_import_p == v)
}
static void
void *arg)
{
graph_edge_t *e;
for (e = uu_list_first(v->gv_dependents);
e != NULL;
e = uu_list_next(v->gv_dependents, e))
}
static void
void *), void *arg)
{
graph_edge_t *e;
for (e = uu_list_first(v->gv_dependencies);
e != NULL;
e = uu_list_next(v->gv_dependencies, e)) {
}
}
/*
* Generic graph walking function.
*
* Given a vertex, this function will walk either dependencies
* (WALK_DEPENDENCIES) or dependents (WALK_DEPENDENTS) of a vertex recursively
* for the entire graph. It will avoid cycles and never visit the same vertex
* twice.
*
* We avoid traversing exclusion dependencies, because they are allowed to
* create cycles in the graph. When propagating satisfiability, there is no
* need to walk exclusion dependencies because exclude_all_satisfied() doesn't
* test for satisfiability.
*
* The walker takes two callbacks. The first is called before examining the
* dependents of each vertex. The second is called on each vertex after
* examining its dependents. This allows is_path_to() to construct a path only
* after the target vertex has been found.
*/
typedef enum {
typedef int (*graph_walk_cb_t)(graph_vertex_t *, void *);
typedef struct graph_walk_info {
int (*gi_pre)(graph_vertex_t *, void *);
void (*gi_post)(graph_vertex_t *, void *);
void *gi_arg; /* callback arg */
int gi_ret; /* return value */
static int
{
int r;
graph_vertex_t *v = e->ge_vertex;
int i;
uint_t b;
i = v->gv_id / 8;
/*
* Check to see if we've visited this vertex already.
*/
if (gip->gi_visited[i] & b)
return (UU_WALK_NEXT);
gip->gi_visited[i] |= b;
/*
* Don't follow exclusions.
*/
return (UU_WALK_NEXT);
/*
* Call pre-visit callback. If this doesn't terminate the walk,
* continue search.
*/
/*
* Recurse using appropriate list.
*/
list = v->gv_dependents;
else
list = v->gv_dependencies;
gip, 0);
assert(r == 0);
}
/*
* Callbacks must return either UU_WALK_NEXT or UU_WALK_DONE.
*/
/*
* If given a post-callback, call the function for every vertex.
*/
/*
* Preserve the callback's return value. If the callback returns
* UU_WALK_DONE, then we propagate that to the caller in order to
* terminate the walk.
*/
}
static void
int (*pre)(graph_vertex_t *, void *),
{
/*
* Fake up an edge for the first iteration
*/
}
typedef struct child_search {
int id; /* id of vertex to look for */
/*
* While the vertex is not found, path is NULL. After the search, if
* the vertex was found then path should point to a -1-terminated
* array of vertex id's which constitute the path to the vertex.
*/
int *path;
static int
{
return (UU_WALK_DONE);
}
return (UU_WALK_NEXT);
}
static void
{
}
/*
* Look for a path from from to to. If one exists, returns a pointer to
* a NULL-terminated array of pointers to the vertices along the path. If
* there is no path, returns NULL.
*/
static int *
{
}
/*
* Given an array of int's as returned by is_path_to, allocates a string of
* their names joined by newlines. Returns the size of the allocated buffer
* in *sz and frees path.
*/
static void
{
int i;
graph_vertex_t *v;
allocd = 1;
(*cpp)[0] = '\0';
for (i = 0; path[i] != -1; ++i) {
v = vertex_get_by_id(path[i]);
if (v == NULL)
name = "<deleted>";
allocd = new_allocd;
}
}
}
/*
* This function along with run_sulogin() implements an exclusion relationship
* between system/console-login and sulogin. run_sulogin() will fail if
* system/console-login is online, and the graph engine should call
* graph_clogin_start() to bring system/console-login online, which defers the
* start if sulogin is running.
*/
static void
{
if (sulogin_running)
else
}
static void
{
/*
* entry with a runlevel of 'S', before jumping to the final
* target runlevel (as set in initdefault). We mimic that legacy
* behavior here.
*/
}
static void
graph_post_su_online(void)
{
}
static void
graph_post_su_disable(void)
{
graph_runlevel_changed('S', 0);
}
static void
graph_post_mu_online(void)
{
}
static void
graph_post_mu_disable(void)
{
graph_runlevel_changed('2', 0);
}
static void
graph_post_mus_online(void)
{
}
static void
graph_post_mus_disable(void)
{
graph_runlevel_changed('3', 0);
}
static struct special_vertex_info {
const char *name;
void (*start_f)(graph_vertex_t *);
void (*post_online_f)(void);
void (*post_disable_f)(void);
} special_vertices[] = {
{ NULL },
};
void
{
switch (e) {
st->st_load_instances++;
break;
v->gv_state == RESTARTER_STATE_DISABLED ||
v->gv_state == RESTARTER_STATE_MAINT);
break;
break;
v->gv_state == RESTARTER_STATE_ONLINE);
break;
break;
break;
default:
#ifndef NDEBUG
#endif
abort();
}
}
static void
{
if (v->gv_restarter_id != -1) {
graph_remove_edge(v, rv);
}
v->gv_restarter_id = -1;
v->gv_restarter_channel = NULL;
}
/*
* Return VERTEX_REMOVED when the vertex passed in argument is deleted from the
* dgraph otherwise return VERTEX_INUSE.
*/
static int
{
if (v->gv_refs > 0)
return (VERTEX_INUSE);
uu_list_numnodes(v->gv_dependents) == 0 &&
uu_list_numnodes(v->gv_dependencies) == 0) {
return (VERTEX_REMOVED);
(v->gv_flags & GV_CONFIGURED) == 0 &&
uu_list_numnodes(v->gv_dependencies) == 0) {
return (VERTEX_REMOVED);
}
return (VERTEX_INUSE);
}
static void
{
graph_edge_t *e;
graph_remove_edge(v, dv);
case GVT_INST: /* instance dependency */
case GVT_SVC: /* service dependency */
(void) free_if_unrefed(dv);
break;
case GVT_FILE: /* file dependency */
break;
default:
#ifndef NDEBUG
#endif
abort();
}
}
}
static int
{
graph_vertex_t *v = ptrs[0];
/*
* We have four possibilities here:
* - GVT_INST: restarter
* - GVT_GROUP - GVT_INST: instance dependency
* - GVT_GROUP - GVT_SVC - GV_INST: service dependency
* - GVT_GROUP - GVT_FILE: file dependency
*/
case GVT_INST: /* restarter */
if (delete_restarter_dep)
graph_remove_edge(v, dv);
break;
case GVT_GROUP: /* pg dependency */
graph_remove_edge(v, dv);
break;
case GVT_FILE:
/* These are currently not direct dependencies */
default:
#ifndef NDEBUG
#endif
abort();
}
return (UU_WALK_NEXT);
}
static void
{
void *ptrs[2];
int r;
ptrs[0] = v;
r = uu_list_walk(v->gv_dependencies,
assert(r == 0);
}
/*
* int graph_insert_vertex_unconfigured()
* Insert a vertex without sending any restarter events. If the vertex
* already exists or creation is successful, return a pointer to it in *vp.
*
* If type is not GVT_GROUP, dt can remain unset.
*
* Returns 0, EEXIST, or EINVAL if the arguments are invalid (i.e., fmri
* doesn't agree with type, or type doesn't agree with dt).
*/
static int
{
int r;
int i;
switch (type) {
case GVT_SVC:
case GVT_INST:
return (EINVAL);
break;
case GVT_FILE:
return (EINVAL);
break;
case GVT_GROUP:
return (EINVAL);
break;
default:
#ifndef NDEBUG
#endif
abort();
}
return (EEXIST);
(*vp)->gv_post_online_f =
(*vp)->gv_post_disable_f =
break;
}
}
(*vp)->gv_restarter_channel = 0;
char *sfmri;
0, &sv);
assert(r == 0);
}
}
/*
* If this vertex is in the subgraph, mark it as so, for both
* GVT_INST and GVT_SERVICE verteces.
* A GVT_SERVICE vertex can only be in the subgraph if another instance
* depends on it, in which case it's already been added to the graph
* and marked as in the subgraph (by refresh_vertex()). If a
* GVT_SERVICE vertex was freshly added (by the code above), it means
* that it has no dependents, and cannot be in the subgraph.
* Regardless of this, we still check that gv_flags includes
* GV_INSUBGRAPH in the event that future behavior causes the above
* code to add a GVT_SERVICE vertex which should be in the subgraph.
*/
return (0);
}
/*
* Returns 0 on success or ELOOP if the dependency would create a cycle.
*/
static int
{
/* cycle detection */
/* Don't follow exclusions. */
if (*pathp)
return (ELOOP);
}
++dep_inserts;
/* Check if the dependency adds the "to" vertex to the subgraph */
return (0);
}
static int
{
if (v->gv_state == RESTARTER_STATE_ONLINE ||
v->gv_state == RESTARTER_STATE_DEGRADED)
return (1);
return (0);
}
/*
* The dependency evaluation functions return
* 1 - dependency satisfied
* 0 - dependency unsatisfied
* -1 - dependency unsatisfiable (without administrator intervention)
*
* The functions also take a boolean satbility argument. When true, the
* functions may recurse in order to determine satisfiability.
*/
/*
* A require_all dependency is unsatisfied if any elements are unsatisfied. It
* is unsatisfiable if any elements are unsatisfiable.
*/
static int
{
int i;
return (1);
if (i == 1)
continue;
if (!satbility)
return (0);
if (i == -1)
return (-1);
}
return (any_unsatisfied ? 0 : 1);
}
/*
* A require_any dependency is satisfied if any element is satisfied. It is
* satisfiable if any element is satisfiable.
*/
static int
{
int s;
return (1);
if (s == 1)
return (1);
"require_any(%s): %s is unsatisfi%s.\n",
s == 0 ? "ed" : "able");
if (satbility && s == 0)
}
}
/*
* An optional_all dependency only considers elements which are configured,
* enabled, and not in maintenance. If any are unsatisfied, then the dependency
* is unsatisfied.
*
* Offline dependencies which are waiting for a dependency to come online are
* unsatisfied. Offline dependences which cannot possibly come online
* (unsatisfiable) are always considered satisfied.
*/
static int
{
graph_vertex_t *v;
int i;
switch (v->gv_type) {
case GVT_INST:
/* Skip missing or disabled instances */
(GV_CONFIGURED | GV_ENABLED))
continue;
if (v->gv_state == RESTARTER_STATE_MAINT)
continue;
if (v->gv_state == RESTARTER_STATE_OFFLINE) {
/*
* For offline dependencies, treat unsatisfiable
* as satisfied.
*/
i = dependency_satisfied(v, B_TRUE);
if (i == -1)
i = 1;
} else if (v->gv_state == RESTARTER_STATE_DISABLED) {
/*
* The service is enabled, but hasn't
* transitioned out of disabled yet. Treat it
* as unsatisfied (not unsatisfiable).
*/
i = 0;
} else {
i = dependency_satisfied(v, satbility);
}
break;
case GVT_FILE:
i = dependency_satisfied(v, satbility);
break;
case GVT_SVC: {
(GV_CONFIGURED | GV_ENABLED)) !=
(GV_CONFIGURED | GV_ENABLED))
continue;
continue;
/*
* For offline dependencies, treat
* unsatisfiable as satisfied.
*/
if (i == -1)
i = 1;
i = 0;
} else {
}
if (i == 1) {
break;
}
if (i == 0)
}
if (!svc_any_qualified)
continue;
if (svc_satisfied) {
i = 1;
} else if (svc_satisfiable) {
i = 0;
} else {
i = -1;
}
break;
}
case GVT_GROUP:
default:
#ifndef NDEBUG
#endif
abort();
}
if (i == 1)
continue;
if (!satbility)
return (0);
if (i == -1)
return (-1);
}
if (!any_qualified)
return (1);
return (any_unsatisfied ? 0 : 1);
}
/*
* An exclude_all dependency is unsatisfied if any non-service element is
* satisfied or any service instance which is configured, enabled, and not in
* maintenance is satisfied. Usually when unsatisfied, it is also
* unsatisfiable.
*/
#define LOG_EXCLUDE(u, v) \
"exclude_all(%s): %s is satisfied.\n", \
/* ARGSUSED */
static int
{
graph_vertex_t *v, *v2;
switch (v->gv_type) {
case GVT_INST:
if ((v->gv_flags & GV_CONFIGURED) == 0)
continue;
switch (v->gv_state) {
case RESTARTER_STATE_ONLINE:
case RESTARTER_STATE_DEGRADED:
LOG_EXCLUDE(groupv, v);
case RESTARTER_STATE_OFFLINE:
case RESTARTER_STATE_UNINIT:
LOG_EXCLUDE(groupv, v);
return (0);
case RESTARTER_STATE_DISABLED:
case RESTARTER_STATE_MAINT:
continue;
default:
#ifndef NDEBUG
uu_warn("%s:%d: Unexpected vertex state %d.\n",
#endif
abort();
}
/* NOTREACHED */
case GVT_SVC:
break;
case GVT_FILE:
if (!file_ready(v))
continue;
LOG_EXCLUDE(groupv, v);
return (-1);
case GVT_GROUP:
default:
#ifndef NDEBUG
#endif
abort();
}
/* v represents a service */
if (uu_list_numnodes(v->gv_dependencies) == 0)
continue;
continue;
case RESTARTER_STATE_ONLINE:
case RESTARTER_STATE_DEGRADED:
case RESTARTER_STATE_OFFLINE:
case RESTARTER_STATE_UNINIT:
return (0);
case RESTARTER_STATE_DISABLED:
case RESTARTER_STATE_MAINT:
continue;
default:
#ifndef NDEBUG
uu_warn("%s:%d: Unexpected vertex type %d.\n",
#endif
abort();
}
}
}
return (1);
}
/*
* int instance_satisfied()
* Determine if all the dependencies are satisfied for the supplied instance
* vertex. Return 1 if they are, 0 if they aren't, and -1 if they won't be
* without administrator intervention.
*/
static int
{
assert(!inst_running(v));
return (require_all_satisfied(v, satbility));
}
/*
* Decide whether v can satisfy a dependency. v can either be a child of
* a group vertex, or of an instance vertex.
*/
static int
{
switch (v->gv_type) {
case GVT_INST:
if ((v->gv_flags & GV_CONFIGURED) == 0)
return (-1);
switch (v->gv_state) {
case RESTARTER_STATE_ONLINE:
case RESTARTER_STATE_DEGRADED:
return (1);
case RESTARTER_STATE_OFFLINE:
if (!satbility)
return (0);
0 : -1);
case RESTARTER_STATE_DISABLED:
case RESTARTER_STATE_MAINT:
return (-1);
case RESTARTER_STATE_UNINIT:
return (0);
default:
#ifndef NDEBUG
uu_warn("%s:%d: Unexpected vertex state %d.\n",
#endif
abort();
/* NOTREACHED */
}
case GVT_SVC:
if (uu_list_numnodes(v->gv_dependencies) == 0)
return (-1);
return (require_any_satisfied(v, satbility));
case GVT_FILE:
/* i.e., we assume files will not be automatically generated */
case GVT_GROUP:
break;
default:
#ifndef NDEBUG
v->gv_type);
#endif
abort();
/* NOTREACHED */
}
switch (v->gv_depgroup) {
case DEPGRP_REQUIRE_ANY:
return (require_any_satisfied(v, satbility));
case DEPGRP_REQUIRE_ALL:
return (require_all_satisfied(v, satbility));
case DEPGRP_OPTIONAL_ALL:
return (optional_all_satisfied(v, satbility));
case DEPGRP_EXCLUDE_ALL:
return (exclude_all_satisfied(v, satbility));
default:
#ifndef NDEBUG
__LINE__, v->gv_depgroup);
#endif
abort();
}
}
void
{
if (v->gv_state == RESTARTER_STATE_OFFLINE &&
if (v->gv_start_f == NULL)
else
v->gv_start_f(v);
}
}
/*
* propagate_satbility()
*
* This function is used when the given vertex changes state in such a way that
* one of its dependents may become unsatisfiable. This happens when an
* instance transitions between offline -> online, or from !running ->
* maintenance, as well as when an instance is removed from the graph.
*
* We have to walk all the dependents, since optional_all dependencies several
* levels up could become (un)satisfied, instead of unsatisfiable. For example,
*
* +-----+ optional_all +-----+ require_all +-----+
* | A |--------------->| B |-------------->| C |
* +-----+ +-----+ +-----+
*
* offline -> maintenance
*
* If C goes into maintenance, it's not enough simply to check B. Because A has
* an optional dependency, what was previously an unsatisfiable situation is now
* satisfied (B will never come online, even though its state hasn't changed).
*
* Note that it's not necessary to continue examining dependents after reaching
* an optional_all dependency. It's not possible for an optional_all dependency
* to change satisfiability without also coming online, in which case we get a
* start event and propagation continues naturally. However, it does no harm to
* continue propagating satisfiability (as it is a relatively rare event), and
* keeps the walker code simple and generic.
*/
/*ARGSUSED*/
static int
{
return (UU_WALK_NEXT);
}
static void
{
}
static void propagate_stop(graph_vertex_t *, void *);
/* ARGSUSED */
static void
{
switch (v->gv_type) {
case GVT_INST:
break;
case GVT_GROUP:
if (v->gv_depgroup == DEPGRP_EXCLUDE_ALL) {
(void *)RERR_RESTART);
break;
}
/* FALLTHROUGH */
case GVT_SVC:
break;
case GVT_FILE:
#ifndef NDEBUG
uu_warn("%s:%d: propagate_start() encountered GVT_FILE.\n",
#endif
abort();
/* NOTREACHED */
default:
#ifndef NDEBUG
v->gv_type);
#endif
abort();
}
}
static void
{
graph_edge_t *e;
switch (v->gv_type) {
case GVT_INST:
/* Restarter */
break;
case GVT_SVC:
break;
case GVT_FILE:
#ifndef NDEBUG
uu_warn("%s:%d: propagate_stop() encountered GVT_FILE.\n",
#endif
abort();
/* NOTREACHED */
case GVT_GROUP:
if (v->gv_depgroup == DEPGRP_EXCLUDE_ALL) {
break;
}
break;
e = uu_list_first(v->gv_dependents);
if (inst_running(svc))
break;
default:
#ifndef NDEBUG
v->gv_type);
#endif
abort();
}
}
/*
* void graph_enable_by_vertex()
* If admin is non-zero, this is an administrative request for change
* of the enabled property. Thus, send the ADMIN_DISABLE rather than
* a plain DISABLE restarter event.
*/
void
{
(enable ? GV_ENABLED : 0);
if (enable) {
} else {
if (admin)
else
}
}
/*
* Wait for state update from restarter before sending _START or
* _STOP.
*/
}
/*
* Set the restarter for v to fmri_arg. That is, make sure a vertex for
* fmri_arg exists, make v depend on it, and send _ADD_INSTANCE for v. If
* v is already configured and fmri_arg indicates the current restarter, do
* nothing. If v is configured and fmri_arg is a new restarter, delete v's
* dependency on the restarter, send _REMOVE_INSTANCE for v, and set the new
* restarter. Returns 0 on success, EINVAL if the FMRI is invalid,
* ECONNABORTED if the repository connection is broken, and ELOOP
* if the dependency would create a cycle. In the last case, *pathp will
* point to a -1-terminated array of ids which compose the path from v to
* restarter_fmri.
*/
int
int **pathp)
{
char *restarter_fmri = NULL;
int err;
int id;
if (fmri_arg[0] != '\0') {
if (err != 0) {
return (err);
}
}
if (restarter_fmri == NULL ||
if (v->gv_flags & GV_CONFIGURED) {
if (v->gv_restarter_id == -1) {
if (restarter_fmri != NULL)
return (0);
}
}
/* Master restarter, nothing to do. */
v->gv_restarter_id = -1;
v->gv_restarter_channel = NULL;
return (0);
}
if (v->gv_flags & GV_CONFIGURED) {
return (0);
}
}
if (rv->gv_delegate_initialized == 0) {
}
if (err != 0) {
return (ELOOP);
}
switch (err) {
case 0:
switch (err) {
case 0:
case ECANCELED:
break;
case ECONNABORTED:
return (ECONNABORTED);
default:
}
break;
case ECONNABORTED:
return (ECONNABORTED);
case ENOENT:
break;
case ENOTSUP:
/*
* The fmri doesn't specify an instance - translate
* to EINVAL.
*/
return (EINVAL);
case EINVAL:
default:
}
}
return (0);
}
/*
* Add all of the instances of the service named by fmri to the graph.
* Returns
* 0 - success
* ENOENT - service indicated by fmri does not exist
*
* In both cases *reboundp will be B_TRUE if the handle was rebound, or B_FALSE
* otherwise.
*/
static int
{
char *inst_fmri;
int ret, r;
svc = safe_scf_service_create(h);
inst = safe_scf_instance_create(h);
iter = safe_scf_iter_create(h);
SCF_DECODE_FMRI_EXACT) != 0) {
switch (scf_error()) {
default:
goto rebound;
case SCF_ERROR_NOT_FOUND:
goto out;
case SCF_ERROR_NOT_BOUND:
}
}
switch (scf_error()) {
default:
goto rebound;
case SCF_ERROR_DELETED:
goto out;
case SCF_ERROR_NOT_BOUND:
case SCF_ERROR_NOT_SET:
}
}
for (;;) {
if (r == 0)
break;
if (r != 1) {
switch (scf_error()) {
default:
goto rebound;
case SCF_ERROR_DELETED:
goto out;
case SCF_ERROR_NOT_BOUND:
case SCF_ERROR_NOT_SET:
bad_error("scf_iter_next_instance",
scf_error());
}
}
0) {
switch (scf_error()) {
goto rebound;
case SCF_ERROR_DELETED:
continue;
case SCF_ERROR_NOT_BOUND:
case SCF_ERROR_NOT_SET:
}
}
switch (r) {
case 0:
case ECANCELED:
break;
case EEXIST:
continue;
case ECONNABORTED:
goto rebound;
case EINVAL:
default:
bad_error("dgraph_add_instance", r);
}
}
ret = 0;
out:
return (ret);
}
struct depfmri_info {
graph_vertex_t *v; /* GVT_GROUP vertex */
const char *inst_fmri; /* FMRI of parental GVT_INST vert. */
const char *pg_name; /* Name of dependency pg */
scf_handle_t *h;
int err; /* return error code */
int **pathp; /* return circular dependency path */
};
/*
* Find or create a vertex for fmri and make info->v depend on it.
* Returns
* 0 - success
* nonzero - failure
*
* On failure, sets info->err to
* EINVAL - fmri is invalid
* fmri does not match info->type
* ELOOP - Adding the dependency creates a circular dependency. *info->pathp
* will point to an array of the ids of the members of the cycle.
* ECONNABORTED - repository connection was broken
* ECONNRESET - succeeded, but repository connection was reset
*/
static int
{
int err;
graph_vertex_t *depgroup_v, *v;
/* Get or create vertex for FMRI */
depgroup_v = info->v;
"FMRI \"%s\" is not allowed for the \"%s\" "
"dependency's type of instance %s.\n", fmri,
}
RERR_NONE, &v);
switch (err) {
case 0:
break;
case EEXIST:
break;
case EINVAL: /* prevented above */
default:
}
} else {
"FMRI \"%s\" is not allowed for the \"%s\" "
"dependency's type of instance %s.\n", fmri,
}
/*
* We must canonify fmri & add a vertex for it.
*/
/* Determine if the FMRI is a property group or instance */
"Dependency \"%s\" of %s has invalid FMRI "
fmri);
}
"Dependency \"%s\" of %s does not designate a "
}
"");
} else {
instance : "");
}
RERR_NONE, &v);
switch (err) {
case 0:
break;
case EEXIST:
/* Verify v. */
else
break;
default:
}
}
/* Add dependency from depgroup_v to new vertex */
case 0:
break;
case ELOOP:
return (ELOOP);
default:
}
/* This must be after we insert the dependency, to avoid looping. */
switch (v->gv_type) {
case GVT_INST:
if ((v->gv_flags & GV_CONFIGURED) != 0)
break;
switch (err) {
case 0:
switch (err) {
case 0:
case ECANCELED:
break;
case ECONNABORTED:
libscf_handle_rebind(info->h);
goto rebound;
default:
}
break;
case ENOENT:
break;
case ECONNABORTED:
libscf_handle_rebind(info->h);
goto rebound;
case EINVAL:
case ENOTSUP:
default:
}
if (rebound)
break;
case GVT_SVC:
if (rebound)
}
return (0);
}
struct deppg_info {
graph_vertex_t *v; /* GVT_INST vertex */
int err; /* return error */
int **pathp; /* return circular dependency path */
};
/*
* Make info->v depend on a new GVT_GROUP node for this property group,
* and then call process_dependency_fmri() for the values of the entity
* property. Return 0 on success, or if something goes wrong return nonzero
* and set info->err to ECONNABORTED, EINVAL, or the error code returned by
* process_dependency_fmri().
*/
static int
{
scf_handle_t *h;
struct depfmri_info linfo;
int err;
int empty;
h = scf_pg_handle(pg);
if (len < 0) {
switch (scf_error()) {
default:
case SCF_ERROR_DELETED:
case SCF_ERROR_NOT_SET:
}
}
/*
* Skip over empty dependency groups. Since dependency property
* groups are updated atomically, they are either empty or
* fully populated.
*/
if (empty < 0) {
"Error reading dependency group \"%s\" of %s: %s\n",
} else if (empty == 1) {
"Ignoring empty dependency group \"%s\" of %s\n",
}
pg_name);
/* Validate the pg before modifying the graph */
if (deptype == DEPGRP_UNSUPPORTED) {
"Dependency \"%s\" of %s has an unknown grouping value.\n",
}
if (rerr == RERR_UNSUPPORTED) {
"Dependency \"%s\" of %s has an unknown restart_on value."
}
prop = safe_scf_property_create(h);
if (scferr == SCF_ERROR_DELETED) {
} else if (scferr != SCF_ERROR_NOT_FOUND) {
}
"Dependency \"%s\" of %s is missing a \"%s\" property.\n",
}
/* Create depgroup vertex for pg */
/* Add dependency from inst vertex to new vertex */
/* ELOOP can't happen because this should be a new vertex */
linfo.h = h;
&linfo);
switch (err) {
case 0:
case EINTR:
case ECONNABORTED:
case EINVAL:
case ECANCELED:
case ECONNRESET:
default:
/* NOTREACHED */
}
}
/*
* Build the dependency info for v from the repository. Returns 0 on success,
* ECONNABORTED on repository disconnection, EINVAL if the repository
* configuration is invalid, and ELOOP if a dependency would cause a cycle.
* In the last case, *pathp will point to a -1-terminated array of ids which
* constitute the rest of the dependency cycle.
*/
static int
{
struct deppg_info info;
int err;
/*
* Mark the vertex as configured during dependency insertion to avoid
* dependency cycles (which can appear in the graph if one of the
* vertices is an exclusion-group).
*/
v->gv_flags |= GV_CONFIGURED;
info.v = v;
&info);
if (!old_configured)
v->gv_flags &= ~GV_CONFIGURED;
switch (err) {
case 0:
case EINTR:
case ECONNABORTED:
return (ECONNABORTED);
case ECANCELED:
/* Should get delete event, so return 0. */
return (0);
default:
/* NOTREACHED */
}
}
static void
{
const char *cp;
"because it completes a dependency cycle (see svcs -xv for "
}
/*
* Increment the vertex's reference count to prevent the vertex removal
* from the dgraph.
*/
static void
{
v->gv_refs++;
}
/*
* Decrement the vertex's reference count and remove the vertex from
* the dgraph when possible.
*
* Return VERTEX_REMOVED when the vertex has been removed otherwise
* return VERTEX_INUSE.
*/
static int
{
v->gv_refs--;
return (free_if_unrefed(v));
}
/*
* When run on the dependencies of a vertex, populates list with
* graph_edge_t's which point to the service vertices or the instance
* vertices (no GVT_GROUP nodes) on which the vertex depends.
*
* Increment the vertex's reference count once the vertex is inserted
* in the list. The vertex won't be able to be deleted from the dgraph
* while it is referenced.
*/
static int
{
graph_vertex_t *v = e->ge_vertex;
int r;
switch (v->gv_type) {
case GVT_INST:
case GVT_SVC:
break;
case GVT_GROUP:
r = uu_list_walk(v->gv_dependencies,
assert(r == 0);
return (UU_WALK_NEXT);
case GVT_FILE:
return (UU_WALK_NEXT);
default:
#ifndef NDEBUG
#endif
abort();
}
assert(r == 0);
/*
* Because we are inserting the vertex in a list, we don't want
* the vertex to be freed while the list is in use. In order to
* achieve that, increment the vertex's reference count.
*/
vertex_ref(v);
return (UU_WALK_NEXT);
}
static boolean_t
{
graph_edge_t *e;
if (v == milestone)
return (B_TRUE);
/*
* v is in the subgraph if any of its dependents are in the subgraph.
* Except for EXCLUDE_ALL dependents. And OPTIONAL dependents only
* count if we're enabled.
*/
for (e = uu_list_first(v->gv_dependents);
e != NULL;
e = uu_list_next(v->gv_dependents, e)) {
continue;
/*
* Don't include instances that are optional and disabled.
*/
int in = 0;
continue;
!(v->gv_flags & GV_ENBLD_NOOVR))
continue;
in = 1;
}
if (!in)
continue;
}
!(v->gv_flags & GV_ENBLD_NOOVR))
continue;
/* Don't include excluded services and instances */
continue;
return (B_TRUE);
}
return (B_FALSE);
}
/*
* Ensures that GV_INSUBGRAPH is set properly for v and its descendents. If
* any bits change, manipulate the repository appropriately. Returns 0 or
* ECONNABORTED.
*/
static int
{
graph_edge_t *e;
int ret = 0, r;
new = should_be_in_subgraph(v);
return (0);
"Removing %s from the subgraph.\n", v->gv_name);
(new ? GV_INSUBGRAPH : 0);
int err;
if (err != 0) {
switch (err) {
case ECONNABORTED:
ret = ECONNABORTED;
goto get_inst;
case ENOENT:
break;
case EINVAL:
case ENOTSUP:
default:
}
} else {
const char *f;
if (new) {
f = "libscf_delete_enable_ovr";
} else {
f = "libscf_set_enable_ovr";
}
switch (err) {
case 0:
case ECANCELED:
break;
case ECONNABORTED:
/*
* We must continue so the graph is updated,
* but we must return ECONNABORTED so any
* libscf state held by any callers is reset.
*/
ret = ECONNABORTED;
goto get_inst;
case EROFS:
case EPERM:
"Could not set %s/%s for %s: %s.\n",
break;
default:
}
}
}
for (e = uu_list_first(v->gv_dependencies);
e != NULL;
e = uu_list_next(v->gv_dependencies, e)) {
r = eval_subgraph(e->ge_vertex, h);
if (r != 0) {
assert(r == ECONNABORTED);
ret = ECONNABORTED;
}
}
return (ret);
}
/*
* Delete the (property group) dependencies of v & create new ones based on
* inst. If doing so would create a cycle, log a message and put the instance
* into maintenance. Update GV_INSUBGRAPH flags as necessary. Returns 0 or
* ECONNABORTED.
*/
int
{
int err;
int *path;
char *fmri;
int r;
int ret = 0;
graph_edge_t *e;
if (milestone > MILESTONE_NONE) {
/*
* In case some of v's dependencies are being deleted we must
* make a list of them now for GV_INSUBGRAPH-flag evaluation
* after the new dependencies are in place.
*/
}
switch (err) {
case 0:
break;
case ECONNABORTED:
goto out;
case EINVAL:
case ELOOP:
switch (r) {
case 0:
break;
case ECONNABORTED:
ret = ECONNABORTED;
goto out;
case ECANCELED:
ret = 0;
goto out;
default:
bad_error("libscf_instance_get_fmri", r);
}
"to maintenance due to misconfiguration.\n",
} else {
}
ret = 0;
goto out;
default:
}
if (milestone > MILESTONE_NONE) {
for (e = uu_list_first(old_deps);
e != NULL;
e = uu_list_next(old_deps, e)) {
}
for (e = uu_list_first(v->gv_dependencies);
e != NULL;
e = uu_list_next(v->gv_dependencies, e)) {
if (eval_subgraph(e->ge_vertex, h) ==
}
if (aborted) {
ret = ECONNABORTED;
goto out;
}
}
ret = 0;
out:
if (milestone > MILESTONE_NONE) {
startd_free(e, sizeof (*e));
}
return (ret);
}
/*
* Set up v according to inst. That is, make sure it depends on its
* restarter and set up its dependencies. Send the ADD_INSTANCE command to
* the restarter, and send ENABLE or DISABLE as appropriate.
*
* Returns 0 on success, ECONNABORTED on repository disconnection, or
* ECANCELED if inst is deleted.
*/
static int
{
scf_handle_t *h;
int enabled, enabled_ovr;
int err;
int *path;
restarter_fmri[0] = '\0';
/* GV_INSUBGRAPH should already be set properly. */
assert(should_be_in_subgraph(v) ==
((v->gv_flags & GV_INSUBGRAPH) != 0));
h = scf_instance_handle(inst);
/*
* If the instance does not have a restarter property group,
* initialize its state to uninitialized/none, in case the restarter
* is not enabled.
*/
pg = safe_scf_pg_create(h);
switch (scf_error()) {
case SCF_ERROR_NOT_FOUND:
break;
default:
return (ECONNABORTED);
case SCF_ERROR_DELETED:
return (ECANCELED);
case SCF_ERROR_NOT_SET:
}
case 0:
break;
case ECONNABORTED:
case ECANCELED:
return (err);
default:
}
case 0:
break;
case ENOMEM:
++count;
if (count < ALLOC_RETRY) {
goto init_state;
}
uu_die("Insufficient memory.\n");
/* NOTREACHED */
case ECONNABORTED:
return (ECONNABORTED);
case ENOENT:
return (ECANCELED);
case EPERM:
case EACCES:
case EROFS:
break;
case EINVAL:
default:
}
}
/*
* Make sure the enable-override is set properly before we
* read whether we should be enabled.
*/
if (milestone == MILESTONE_NONE ||
!(v->gv_flags & GV_INSUBGRAPH)) {
/*
* This might seem unjustified after the milestone
* transition has completed (non_subgraph_svcs == 0),
* but it's important because when we boot to
* a milestone, we set the milestone before populating
* the graph, and all of the new non-subgraph services
* need to be disabled here.
*/
case 0:
break;
case ECONNABORTED:
case ECANCELED:
return (err);
case EROFS:
"Could not set %s/%s for %s: %s.\n",
break;
case EPERM:
uu_die("Permission denied.\n");
/* NOTREACHED */
default:
}
} else {
case 0:
break;
case ECONNABORTED:
case ECANCELED:
return (err);
case EPERM:
uu_die("Permission denied.\n");
/* NOTREACHED */
default:
}
}
}
switch (err) {
case 0:
break;
case ECONNABORTED:
case ECANCELED:
return (err);
case ENOENT:
"Ignoring %s because it has no general property group.\n",
v->gv_name);
return (0);
default:
}
if (enabled == -1) {
return (0);
}
(enabled ? GV_ENBLD_NOOVR : 0);
if (enabled_ovr != -1)
/* Set up the restarter. (Sends _ADD_INSTANCE on success.) */
if (err != 0) {
const char *reason;
if (err == ECONNABORTED) {
return (err);
}
v->gv_name);
reason = "invalid_restarter";
} else {
reason = "dependency_cycle";
}
/*
* We didn't register the instance with the restarter, so we
* must set maintenance mode ourselves.
*/
if (err != 0) {
return (err);
}
case 0:
break;
case ENOMEM:
++count;
if (count < ALLOC_RETRY) {
goto set_maint;
}
uu_die("Insufficient memory.\n");
/* NOTREACHED */
case ECONNABORTED:
return (ECONNABORTED);
case ENOENT:
return (ECANCELED);
case EPERM:
case EACCES:
case EROFS:
break;
case EINVAL:
default:
}
goto out;
}
/* Add all the other dependencies. */
if (err != 0) {
return (err);
}
out:
v->gv_flags |= GV_CONFIGURED;
graph_enable_by_vertex(v, enabled, 0);
return (0);
}
static void
do_uadmin(void)
{
if (fd >= 0)
else
/* Kill dhcpagent if we're not using nfs for root */
left = 5;
while (left > 0)
left = 10;
while (left > 0)
sync();
sync();
sync();
uu_warn("The system is down.\n");
uu_warn("uadmin() failed");
}
/*
* If any of the up_svcs[] are online or satisfiable, return true. If they are
* all missing, disabled, in maintenance, or unsatisfiable, return false.
*/
can_come_up(void)
{
int i;
/*
* If we are booting to single user (boot -s),
* SCF_MILESTONE_SINGLE_USER is needed to come up because startd
* spawns sulogin after single-user is online (see specials.c).
*/
i = (booting_to_single_user ? 0 : 1);
continue;
}
/*
* Ignore unconfigured services (the ones that have been
* mentioned in a dependency from other services, but do
* not exist in the repository). Services which exist
* property will be also ignored.
*/
continue;
case RESTARTER_STATE_ONLINE:
case RESTARTER_STATE_DEGRADED:
/*
* Deactivate verbose boot once a login service has been
* reached.
*/
/*FALLTHROUGH*/
case RESTARTER_STATE_UNINIT:
return (B_TRUE);
case RESTARTER_STATE_OFFLINE:
return (B_TRUE);
"can_come_up(): %s is unsatisfiable.\n",
continue;
case RESTARTER_STATE_DISABLED:
case RESTARTER_STATE_MAINT:
"can_come_up(): %s is in state %s.\n",
continue;
default:
#ifndef NDEBUG
uu_warn("%s:%d: Unexpected vertex state %d.\n",
#endif
abort();
}
}
/*
* In the seed repository, console-login is unsatisfiable because
* services are missing. To behave correctly in that case we don't want
* to return false until manifest-import is online.
*/
if (manifest_import_p == NULL) {
if (manifest_import_p == NULL)
return (B_FALSE);
}
switch (manifest_import_p->gv_state) {
case RESTARTER_STATE_ONLINE:
case RESTARTER_STATE_DEGRADED:
case RESTARTER_STATE_DISABLED:
case RESTARTER_STATE_MAINT:
break;
case RESTARTER_STATE_OFFLINE:
break;
/* FALLTHROUGH */
case RESTARTER_STATE_UNINIT:
return (B_TRUE);
}
return (B_FALSE);
}
/*
* Runs sulogin. Returns
* 0 - success
* EALREADY - sulogin is already running
* EBUSY - console-login is running
*/
static int
run_sulogin(const char *msg)
{
graph_vertex_t *v;
if (sulogin_running)
return (EALREADY);
if (v != NULL && inst_running(v))
return (EBUSY);
if (console_login_ready) {
!inst_running(v)) {
if (v->gv_start_f == NULL)
else
v->gv_start_f(v);
}
}
return (0);
}
/*
* The sulogin thread runs sulogin while can_come_up() is false. run_sulogin()
* keeps sulogin from stepping on console-login's toes.
*/
/* ARGSUSED */
static void *
sulogin_thread(void *unused)
{
do {
(void) run_sulogin("Console login service(s) cannot run\n");
} while (!can_come_up());
return (NULL);
}
/* ARGSUSED */
void *
single_user_thread(void *unused)
{
scf_handle_t *h;
const char *msg;
char *buf;
int r;
if (!booting_to_single_user) {
/*
* processes in their process groups so they can be terminated.
*/
left = 5;
while (left > 0)
(void) puts("done.");
}
if (go_single_user_mode || booting_to_single_user) {
msg = "SINGLE USER MODE\n";
} else {
uu_warn("The system is ready for administration.\n");
msg = "";
}
for (;;) {
r = run_sulogin(msg);
if (r == 0)
break;
left = 3;
while (left > 0)
}
/*
* If another single user thread has started, let it finish changing
* the run level.
*/
if (single_user_thread_count > 1) {
return (NULL);
}
inst = scf_instance_create(h);
prop = safe_scf_property_create(h);
val = safe_scf_value_create(h);
switch (scf_error()) {
case SCF_ERROR_NOT_FOUND:
r = libscf_create_self(h);
if (r == 0)
goto lookup;
assert(r == ECONNABORTED);
/* FALLTHROUGH */
goto lookup;
case SCF_ERROR_NOT_BOUND:
default:
}
}
switch (r) {
case 0:
case ECANCELED:
break;
case ECONNABORTED:
goto lookup;
case EPERM:
case EACCES:
case EROFS:
"%s.\n", strerror(r));
break;
default:
bad_error("libscf_inst_delete_prop", r);
}
switch (r) {
case ECANCELED:
case ENOENT:
case EINVAL:
/* FALLTHROUGH */
case 0:
break;
case ECONNABORTED:
goto lookup;
default:
bad_error("libscf_get_milestone", r);
}
switch (r) {
case 0:
case ECONNRESET:
case EALREADY:
case EINVAL:
case ENOENT:
break;
default:
bad_error("dgraph_set_milestone", r);
}
/*
* See graph_runlevel_changed().
*/
/*
* We'll give ourselves 3 seconds to respond to all of the enablings
* that setting the milestone should have created before checking
* whether to run sulogin.
*/
left = 3;
while (left > 0)
/*
* Clearing these variables will allow the sulogin thread to run. We
* check here in case there aren't any more state updates anytime soon.
*/
if (!sulogin_thread_running && !can_come_up()) {
}
return (NULL);
}
/*
* Dependency graph operations API. These are handle-independent thread-safe
* graph manipulation functions which are the entry points for the event
* threads below.
*/
/*
* If a configured vertex exists for inst_fmri, return EEXIST. If no vertex
* exists for inst_fmri, add one. Then fetch the restarter from inst, make
* this vertex dependent on it, and send _ADD_INSTANCE to the restarter.
* Fetch whether the instance should be enabled from inst and send _ENABLE or
* _DISABLE as appropriate. Finally rummage through inst's dependency
* property groups and add vertices and edges as appropriate. If anything
* goes wrong after sending _ADD_INSTANCE, send _ADMIN_MAINT_ON to put the
* instance in maintenance. Don't send _START or _STOP until we get a state
* update in case we're being restarted and the service is already running.
*
* To support booting to a milestone, we must also make sure all dependencies
* encountered are configured, if they exist in the repository.
*
* Returns 0 on success, ECONNABORTED on repository disconnection, EINVAL if
* inst_fmri is an invalid (or not canonical) FMRI, ECANCELED if inst is
* deleted, or EEXIST if a configured vertex for inst_fmri already exists.
*/
int
{
graph_vertex_t *v;
int err;
return (0);
/* Check for a vertex for inst_fmri. */
if (lock_graph) {
} else {
}
v = vertex_get_by_name(inst_fmri);
if (v != NULL) {
if (v->gv_flags & GV_CONFIGURED) {
if (lock_graph)
return (EEXIST);
}
} else {
/* Add the vertex. */
RERR_NONE, &v);
if (err != 0) {
if (lock_graph)
return (EINVAL);
}
}
if (lock_graph)
return (err);
}
/*
* Locate the vertex for this property group's instance. If it doesn't exist
* or is unconfigured, call dgraph_add_instance() & return. Otherwise fetch
* the restarter for the instance, and if it has changed, send
* _REMOVE_INSTANCE to the old restarter, remove the dependency, make sure the
* new restarter has a vertex, add a new dependency, and send _ADD_INSTANCE to
* the new restarter. Then fetch whether the instance should be enabled, and
* if it is different from what we had, or if we changed the restarter, send
* the appropriate _ENABLE or _DISABLE command.
*
* Returns 0 on success, ENOTSUP if the pg's parent is not an instance,
* ECONNABORTED on repository disconnection, ECANCELED if the instance is
* deleted, or -1 if the instance's general property group is deleted or if
* its enabled property is misconfigured.
*/
static int
{
scf_handle_t *h;
char *fmri;
char *restarter_fmri;
graph_vertex_t *v;
int err;
int enabled, enabled_ovr;
int oldflags;
/* Find the vertex for this service */
h = scf_pg_handle(pg);
inst = safe_scf_instance_create(h);
switch (scf_error()) {
return (ENOTSUP);
default:
return (ECONNABORTED);
case SCF_ERROR_DELETED:
return (0);
case SCF_ERROR_NOT_SET:
}
}
switch (err) {
case 0:
break;
case ECONNABORTED:
return (ECONNABORTED);
case ECANCELED:
return (0);
default:
}
"Graph engine: Reloading general properties for %s.\n", fmri);
v = vertex_get_by_name(fmri);
/* Will get the up-to-date properties. */
}
/* Read enabled & restarter from repository. */
switch (err) {
case ENOENT:
case 0:
return (-1);
case ECONNABORTED:
case ECANCELED:
return (err);
default:
}
}
(enabled ? GV_ENBLD_NOOVR : 0);
if (enabled_ovr != -1)
/*
* If GV_ENBLD_NOOVR has changed, then we need to re-evaluate the
* subgraph.
*/
(void) eval_subgraph(v, h);
/* Ignore restarter change for now. */
/*
* Always send _ENABLE or _DISABLE. We could avoid this if the
* restarter didn't change and the enabled value didn't change, but
* that's not easy to check and improbable anyway, so we'll just do
* this.
*/
return (0);
}
/*
* Delete all of the property group dependencies of v, update inst's running
* snapshot, and add the dependencies in the new snapshot. If any of the new
* dependencies would create a cycle, send _ADMIN_MAINT_ON. Otherwise
* reevaluate v's dependencies, send _START or _STOP as appropriate, and do
* the same for v's dependents.
*
* Returns
* 0 - success
* ECONNABORTED - repository connection broken
* ECANCELED - inst was deleted
* -1 - libscf_snapshots_refresh() failed
*/
static int
{
int r;
int enabled;
switch (r) {
case 0:
break;
case ECONNABORTED:
case ECANCELED:
return (r);
case ENOENT:
"Ignoring %s because it has no general property group.\n",
v->gv_name);
return (EINVAL);
default:
bad_error("libscf_get_basic_instance_data", r);
}
if (enabled == -1)
return (EINVAL);
if (r != 0) {
if (r != -1)
bad_error("libscf_snapshots_refresh", r);
/* error logged */
return (r);
}
r = refresh_vertex(v, inst);
if (r != 0 && r != ECONNABORTED)
bad_error("refresh_vertex", r);
return (r);
}
/*
* Returns true only if none of this service's dependents are 'up' -- online,
* degraded, or offline.
*/
static int
{
graph_edge_t *e;
for (e = uu_list_first(v->gv_dependents);
e != NULL;
e = uu_list_next(v->gv_dependents, e)) {
continue;
continue;
return (0);
} else {
/*
* For dependency group or service vertices, keep
* traversing to see if instances are running.
*/
if (!is_nonsubgraph_leaf(vv))
return (0);
}
}
return (1);
}
/*
* Disable v temporarily. Attempt to do this by setting its enabled override
* property in the repository. If that fails, send a _DISABLE command.
* Returns 0 on success and ECONNABORTED if the repository connection is
* broken.
*/
static int
{
const char * const emsg = "Could not temporarily disable %s because "
"%s. Will stop service anyways. Repository status for the "
"service may be inaccurate.\n";
const char * const emsg_cbroken =
"the repository connection was broken";
int r;
inst = scf_instance_create(h);
char buf[100];
"scf_instance_create() failed (%s)",
scf_strerror(scf_error()));
graph_enable_by_vertex(v, 0, 0);
return (0);
}
if (r != 0) {
switch (scf_error()) {
graph_enable_by_vertex(v, 0, 0);
return (ECONNABORTED);
case SCF_ERROR_NOT_FOUND:
return (0);
case SCF_ERROR_NOT_BOUND:
default:
bad_error("scf_handle_decode_fmri",
scf_error());
}
}
r = libscf_set_enable_ovr(inst, 0);
switch (r) {
case 0:
return (0);
case ECANCELED:
return (0);
case ECONNABORTED:
graph_enable_by_vertex(v, 0, 0);
return (ECONNABORTED);
case EPERM:
"the repository denied permission");
graph_enable_by_vertex(v, 0, 0);
return (0);
case EROFS:
"the repository is read-only");
graph_enable_by_vertex(v, 0, 0);
return (0);
default:
bad_error("libscf_set_enable_ovr", r);
/* NOTREACHED */
}
}
/*
* Of the transitive instance dependencies of v, disable those which are not
* in the subgraph and which are leaves (i.e., have no dependents which are
* "up").
*/
static void
{
/*
* We must skip exclusion dependencies because they are allowed to
* complete dependency cycles. This is correct because A's exclusion
* dependency on B doesn't bear on the order in which they should be
* stopped. Indeed, the exclusion dependency should guarantee that
* they are never online at the same time.
*/
return;
/* If v isn't an instance, recurse on its dependencies. */
goto recurse;
if ((v->gv_flags & GV_CONFIGURED) == 0)
/*
* Unconfigured instances should have no dependencies, but in
* case they ever get them,
*/
goto recurse;
/*
* If v is in the subgraph, so should all of its dependencies, so do
* nothing.
*/
if (v->gv_flags & GV_INSUBGRAPH)
return;
/* If v isn't a leaf because it's already down, recurse. */
goto recurse;
/* If v is disabled but not down yet, be patient. */
if ((v->gv_flags & GV_ENABLED) == 0)
return;
/* If v is a leaf, disable it. */
if (is_nonsubgraph_leaf(v))
return;
}
/*
* Find the vertex for inst_name. If it doesn't exist, return ENOENT.
* Otherwise set its state to state. If the instance has entered a state
* which requires automatic action, take it (Uninitialized: do
* dgraph_refresh_instance() without the snapshot update. Disabled: if the
* instance should be enabled, send _ENABLE. Offline: if the instance should
* be disabled, send _DISABLE, and if its dependencies are satisfied, send
* _START. Online, Degraded: if the instance wasn't running, update its start
* snapshot. Maintenance: no action.)
*
* Also fails with ECONNABORTED, or EINVAL if state is invalid.
*/
static int
{
graph_vertex_t *v;
int err = 0;
v = vertex_get_by_name(inst_name);
if (v == NULL) {
return (ENOENT);
}
switch (state) {
case RESTARTER_STATE_UNINIT:
case RESTARTER_STATE_DISABLED:
case RESTARTER_STATE_OFFLINE:
case RESTARTER_STATE_ONLINE:
case RESTARTER_STATE_DEGRADED:
case RESTARTER_STATE_MAINT:
break;
default:
return (EINVAL);
}
return (err);
}
/*
* Handle state changes during milestone shutdown. See
* dgraph_set_milestone(). If the repository connection is broken,
* ECONNABORTED will be returned, though a _DISABLE command will be sent for
* the vertex anyway.
*/
int
{
int ret = 0;
/* Don't care if we're not going to a milestone. */
return (0);
/* Don't care if we already finished coming down. */
if (non_subgraph_svcs == 0)
return (0);
/* Don't care if the service is in the subgraph. */
if (v->gv_flags & GV_INSUBGRAPH)
return (0);
/*
* Update non_subgraph_svcs. It is the number of non-subgraph
* services which are in online, degraded, or offline.
*/
if (non_subgraph_svcs == 0) {
if (halting != -1) {
do_uadmin();
} else if (go_single_user_mode || go_to_level1) {
NULL);
}
return (0);
}
}
/* If this service is a leaf, it should be disabled. */
int r;
r = disable_service_temporarily(v, h);
switch (r) {
case 0:
break;
case ECONNABORTED:
ret = ECONNABORTED;
break;
default:
bad_error("disable_service_temporarily", r);
}
}
/*
* If the service just came down, propagate the disable to the newly
* exposed leaves.
*/
(void *)h);
return (ret);
}
/*
* Decide whether to start up an sulogin thread after a service is
* finished changing state. Only need to do the full can_come_up()
* evaluation if an instance is changing state, we're not halfway through
* loading the thread, and we aren't shutting down or going to the single
* user milestone.
*/
void
{
!go_single_user_mode && !go_to_level1 &&
halting == -1) {
if (!sulogin_thread_running && !can_come_up()) {
}
}
}
/*
* Propagate a start, stop event, or a satisfiability event.
*
* PROPAGATE_START and PROPAGATE_STOP simply propagate the transition event
* to direct dependents. PROPAGATE_SAT propagates a start then walks the
* full dependent graph to check for newly satisfied nodes. This is
* necessary for cases when non-direct dependents may be effected but direct
* dependents may not (e.g. for optional_all evaluations, see the
* propagate_satbility() comments).
*
* PROPAGATE_SAT should be used whenever a non-running service moves into
* a state which can satisfy optional dependencies, like disabled or
* maintenance.
*/
void
{
if (type == PROPAGATE_STOP) {
if (type == PROPAGATE_SAT)
} else {
#ifndef NDEBUG
#endif
abort();
}
}
/*
* If a vertex for fmri exists and it is enabled, send _DISABLE to the
* restarter. If it is running, send _STOP. Send _REMOVE_INSTANCE. Delete
* all property group dependencies, and the dependency on the restarter,
* disposing of vertices as appropriate. If other vertices depend on this
* one, mark it unconfigured and return. Otherwise remove the vertex. Always
* returns 0.
*/
static int
{
graph_vertex_t *v;
graph_edge_t *e;
int err;
v = vertex_get_by_name(fmri);
if (v == NULL) {
return (0);
}
/* Send restarter delete event. */
if (v->gv_flags & GV_CONFIGURED)
if (milestone > MILESTONE_NONE) {
/*
* Make a list of v's current dependencies so we can
* reevaluate their GV_INSUBGRAPH flags after the dependencies
* are removed.
*/
}
/*
* Deleting an instance can both satisfy and unsatisfy dependencies,
* depending on their type. First propagate the stop as a RERR_RESTART
* event -- deletion isn't a fault, just a normal stop. This gives
* dependent services the chance to do a clean shutdown. Then, mark
* the service as unconfigured and propagate the start event for the
* optional_all dependencies that might have become satisfied.
*/
v->gv_flags &= ~GV_CONFIGURED;
/*
* If there are no (non-service) dependents, the vertex can be
* completely removed.
*/
if (milestone > MILESTONE_NONE) {
v = e->ge_vertex;
if (vertex_unref(v) == VERTEX_INUSE)
while (eval_subgraph(v, h) == ECONNABORTED)
startd_free(e, sizeof (*e));
}
}
return (0);
}
/*
* Return the eventual (maybe current) milestone in the form of a
* legacy runlevel.
*/
static char
{
return ('3');
else if (milestone == MILESTONE_NONE)
return ('0');
return ('2');
return ('S');
return ('3');
#ifndef NDEBUG
#endif
abort();
/* NOTREACHED */
}
static struct {
char rl;
int sig;
} init_sigs[] = {
{ 'S', SIGBUS },
{ '0', SIGINT },
{ '1', SIGQUIT },
{ '2', SIGILL },
{ '3', SIGTRAP },
{ '4', SIGIOT },
{ '5', SIGEMT },
{ '6', SIGFPE },
{ 0, 0 }
};
static void
signal_init(char rl)
{
int i;
return;
}
break;
switch (errno) {
case EPERM:
case ESRCH:
break;
case EINVAL:
default:
}
}
}
}
/*
* This is called when one of the major milestones changes state, or when
* init is signalled and tells us it was told to change runlevel. We wait
*
* Also, we only trigger an update when we reach the eventual target
* runlevel 2 would be executed for runlevel 3, which is not how
*
* If we're single user coming online, then we set utmpx to the target
* runlevel so that legacy scripts can work as expected.
*/
static void
{
char trl;
if (online) {
} else if (rl == 'S') {
/*
* At boot, set the entry early for the benefit of the
* legacy init scripts.
*/
}
} else {
}
}
}
/*
* Move to a backwards-compatible runlevel by executing the appropriate
*
* Returns
* 0 - success
* ECONNRESET - success, but handle was reset
* ECONNABORTED - repository connection broken
* ECANCELED - pg was deleted
*/
static int
{
char rl;
scf_handle_t *h;
int r;
int mark_rl = 0;
const char * const stop = "stop";
switch (r) {
case 0:
break;
case ECONNABORTED:
case ECANCELED:
return (r);
case EINVAL:
case ENOENT:
"ignoring.\n");
/* delete the bad property */
goto nolock_out;
default:
bad_error("libscf_extract_runlevel", r);
}
switch (rl) {
case 's':
rl = 'S';
/* FALLTHROUGH */
case 'S':
case '2':
case '3':
/*
* These cases cause a milestone change, so
* graph_runlevel_changed() will eventually deal with
* signalling init.
*/
break;
case '0':
case '1':
case '4':
case '5':
case '6':
mark_rl = 1;
break;
default:
goto nolock_out;
}
h = scf_pg_handle(pg);
/*
* Since this triggers no milestone changes, force it by hand.
*/
mark_rl = 1;
/*
* 1. If we are here after an "init X":
*
* init X
* process_pg_event()
* dgraph_set_runlevel()
*
* then we haven't passed through graph_runlevel_changed() yet,
* therefore 'current_runlevel' has not changed for sure but 'rl' has.
* In consequence, if 'rl' is lower than 'current_runlevel', we change
* past this test.
*
* 2. On the other hand, if we are here after a "svcadm milestone":
*
* svcadm milestone X
* dgraph_set_milestone()
* handle_graph_update_event()
* dgraph_set_instance_state()
* graph_post_X_[online|offline]()
* graph_runlevel_changed()
* signal_init()
* process_pg_event()
* dgraph_set_runlevel()
*
* then we already passed through graph_runlevel_changed() (by the way
* of dgraph_set_milestone()) and 'current_runlevel' may have changed
* and already be equal to 'rl' so we are going to return immediately
* from dgraph_set_runlevel() without changing the system runlevel and
*/
if (rl == current_runlevel) {
goto out;
}
/*
* Make sure stop rc scripts see the new settings via who -r.
*/
/*
* Some run levels don't have a direct correspondence to any
* milestones, so we have to signal init directly.
*/
if (mark_rl) {
}
switch (rl) {
case 'S':
uu_warn("The system is coming down for administration. "
"Please wait.\n");
break;
case '0':
goto uadmin;
case '5':
goto uadmin;
case '6':
goto uadmin;
uu_warn("The system is coming down. Please wait.\n");
ms = "none";
/*
* We can't wait until all services are offline since this
* thread is responsible for taking them offline. Instead we
* set halting to the second argument for uadmin() and call
* do_uadmin() from dgraph_set_instance_state() when
* appropriate.
*/
break;
case '1':
if (current_runlevel != 'S') {
uu_warn("Changing to state 1.\n");
} else {
uu_warn("The system is coming up for administration. "
"Please wait.\n");
}
break;
case '2':
break;
case '3':
case '4':
ms = "all";
break;
default:
#ifndef NDEBUG
#endif
abort();
}
out:
case 0:
break;
case ECONNABORTED:
goto nolock_out;
case ECANCELED:
break;
case EPERM:
case EACCES:
case EROFS:
break;
default:
bad_error("libscf_clear_runlevel", r);
}
return (rebound ? ECONNRESET : 0);
}
static int
{
graph_vertex_t *v;
int r;
v = e->ge_vertex;
/* If it's already in the subgraph, skip. */
if (v->gv_flags & GV_INSUBGRAPH)
return (UU_WALK_NEXT);
/*
* Keep track if walk has entered an optional dependency group
*/
optional = 1;
}
/*
* Quit if we are in an optional dependency group and the instance
* is disabled
*/
(!(v->gv_flags & GV_ENBLD_NOOVR)))
return (UU_WALK_NEXT);
v->gv_flags |= GV_INSUBGRAPH;
/* Skip all excluded dependencies. */
return (UU_WALK_NEXT);
(void *)optional, 0);
assert(r == 0);
return (UU_WALK_NEXT);
}
/*
* Bring down all services which are not dependencies of fmri. The
* dependencies of fmri (direct & indirect) will constitute the "subgraph",
* and will have the GV_INSUBGRAPH flag set. The rest must be brought down,
* which means the state is "disabled", "maintenance", or "uninitialized". We
* could consider "offline" to be down, and refrain from sending start
* commands for such services, but that's not strictly necessary, so we'll
* decline to intrude on the state machine. It would probably confuse users
* anyway.
*
* The services should be brought down in reverse-dependency order, so we
* can't do it all at once here. We initiate by override-disabling the leaves
* of the dependency tree -- those services which are up but have no
* dependents which are up. When they come down,
* vertex_subgraph_dependencies_shutdown() will override-disable the newly
* exposed leaves. Perseverance will ensure completion.
*
* Sometimes we need to take action when the transition is complete, like
* start sulogin or halt the system. To tell when we're done, we initialize
* non_subgraph_svcs here to be the number of services which need to come
* down. As each does, we decrement the counter. When it hits zero, we take
* the appropriate action. See vertex_subgraph_dependencies_shutdown().
*
* In case we're coming up, we also remove any enable-overrides for the
* services which are dependencies of fmri.
*
* If norepository is true, the function will not change the repository.
*
* The decision to change the system run level in accordance with the milestone
* is taken in dgraph_set_runlevel().
*
* Returns
* 0 - success
* ECONNRESET - success, but handle was rebound
* EINVAL - fmri is invalid (error is logged)
* EALREADY - the milestone is already set to fmri
* ENOENT - a configured vertex does not exist for fmri (an error is logged)
*/
static int
{
graph_vertex_t *nm, *v;
int ret = 0, r;
/* Validate fmri */
goto reject;
"Rejecting request for invalid milestone \"%s\".\n",
fmri);
return (EINVAL);
}
}
inst = safe_scf_instance_create(h);
if (isall) {
"Milestone already set to all.\n");
goto out;
}
} else if (milestone == MILESTONE_NONE) {
if (isnone) {
"Milestone already set to none.\n");
goto out;
}
} else {
"Milestone already set to %s.\n", cfmri);
goto out;
}
}
"because no such service exists.\n", cfmri);
goto out;
}
}
/*
* Set milestone, removing the old one if this was the last reference.
*/
if (milestone > MILESTONE_NONE)
(void) vertex_unref(milestone);
if (isall)
else if (isnone)
else {
/* milestone should count as a reference */
}
/* Clear all GV_INSUBGRAPH bits. */
v->gv_flags &= ~GV_INSUBGRAPH;
/* Set GV_INSUBGRAPH for milestone & descendents. */
assert(r == 0);
}
/* Un-override services in the subgraph & override-disable the rest. */
if (norepository)
goto out;
non_subgraph_svcs = 0;
for (v = uu_list_first(dgraph);
v != NULL;
v = uu_list_next(dgraph, v)) {
(v->gv_flags & GV_CONFIGURED) == 0)
continue;
if (r != 0) {
switch (scf_error()) {
default:
goto again;
case SCF_ERROR_NOT_FOUND:
continue;
case SCF_ERROR_NOT_BOUND:
bad_error("scf_handle_decode_fmri",
scf_error());
}
}
r = libscf_delete_enable_ovr(inst);
fs = "libscf_delete_enable_ovr";
} else {
/*
* Services which are up need to come down before
* we're done, but we can only disable the leaves
* here.
*/
/* If it's already disabled, don't bother. */
if ((v->gv_flags & GV_ENABLED) == 0)
continue;
if (!is_nonsubgraph_leaf(v))
continue;
r = libscf_set_enable_ovr(inst, 0);
fs = "libscf_set_enable_ovr";
}
switch (r) {
case 0:
case ECANCELED:
break;
case ECONNABORTED:
goto again;
case EPERM:
case EROFS:
"Could not set %s/%s for %s: %s.\n",
break;
default:
}
}
if (halting != -1) {
if (non_subgraph_svcs > 1)
uu_warn("%d system services are now being stopped.\n",
else if (non_subgraph_svcs == 1)
uu_warn("One system service is now being stopped.\n");
else if (non_subgraph_svcs == 0)
do_uadmin();
}
out:
return (ret);
}
/*
* Returns 0, ECONNABORTED, or EINVAL.
*/
static int
{
int r;
switch (e->gpe_type) {
"graph_event: reload graph unimplemented\n");
break;
case GRAPH_UPDATE_STATE_CHANGE: {
switch (r = dgraph_set_instance_state(h, e->gpe_inst,
case 0:
case ENOENT:
break;
case ECONNABORTED:
return (ECONNABORTED);
case EINVAL:
default:
#ifndef NDEBUG
"failed with unexpected error %d at %s:%d.\n", r,
#endif
abort();
}
break;
}
default:
"graph_event_loop received an unknown event: %d\n",
e->gpe_type);
break;
}
return (0);
}
/*
* graph_event_thread()
* Wait for state changes from the restarters.
*/
/*ARGSUSED*/
void *
graph_event_thread(void *unused)
{
scf_handle_t *h;
int err;
/*CONSTCOND*/
while (1) {
while ((e = graph_event_dequeue()) != NULL) {
MUTEX_LOCK(&e->gpe_lock);
while ((err = handle_graph_update_event(h, e)) ==
if (err == 0)
else
}
}
/*
* Unreachable for now -- there's currently no graceful cleanup
* called on exit().
*/
return (NULL);
}
static void
{
int r;
inst = safe_scf_instance_create(h);
/*
* If -m milestone= was specified, we want to set options_ovr/milestone
* to it. Otherwise we want to read what the milestone should be set
* to. Either way we need our inst.
*/
switch (scf_error()) {
goto get_self;
case SCF_ERROR_NOT_FOUND:
} else {
fmri[0] = '\0';
}
break;
default:
}
} else {
pg = safe_scf_pg_create(h);
pg);
switch (r) {
case 0:
break;
case ECONNABORTED:
goto get_self;
case EPERM:
case EACCES:
case EROFS:
"%s.\n", SCF_PG_OPTIONS_OVR,
/* FALLTHROUGH */
case ECANCELED:
break;
default:
bad_error("libscf_inst_get_or_add_pg", r);
}
switch (r) {
case 0:
break;
case ECONNABORTED:
goto get_self;
case EPERM:
case EACCES:
case EROFS:
"%s.\n", SCF_PG_OPTIONS_OVR,
/* FALLTHROUGH */
case ECANCELED:
break;
default:
bad_error("libscf_clear_runlevel", r);
}
} else {
prop = safe_scf_property_create(h);
val = safe_scf_value_create(h);
switch (r) {
case 0:
break;
case ECONNABORTED:
goto get_self;
case EINVAL:
"misconfigured. Defaulting to \"all\".\n");
/* FALLTHROUGH */
case ECANCELED:
case ENOENT:
fmri[0] = '\0';
break;
default:
bad_error("libscf_get_milestone", r);
}
}
}
goto out;
NULL, SCF_DECODE_FMRI_EXACT) != 0) {
switch (scf_error()) {
"Requested milestone \"%s\" is invalid. "
"Reverting to \"all\".\n", fmri);
goto out;
"\"%s\" does not specify an instance. "
"Reverting to \"all\".\n", fmri);
goto out;
goto retry;
case SCF_ERROR_NOT_FOUND:
"\"%s\" not in repository. Reverting to "
"\"all\".\n", fmri);
goto out;
default:
bad_error("scf_handle_decode_fmri",
scf_error());
}
}
assert(r == 0);
switch (r) {
case 0:
break;
case ECONNABORTED:
goto retry;
case EINVAL:
"Requested milestone \"%s\" is invalid. "
"Reverting to \"all\".\n", fmri);
goto out;
case ECANCELED:
"Requested milestone \"%s\" not "
"in repository. Reverting to \"all\".\n",
fmri);
goto out;
case EEXIST:
default:
bad_error("dgraph_add_instance", r);
}
}
switch (r) {
case 0:
case ECONNRESET:
case EALREADY:
break;
case EINVAL:
case ENOENT:
default:
bad_error("dgraph_set_milestone", r);
}
out:
}
void
{
char *fmri;
int r;
inst = safe_scf_instance_create(h);
switch (scf_error()) {
goto get_self;
case SCF_ERROR_NOT_FOUND:
break;
default:
}
return;
}
prop = safe_scf_property_create(h);
val = safe_scf_value_create(h);
switch (r) {
case 0:
break;
case ECONNABORTED:
goto get_self;
case ECANCELED:
case ENOENT:
case EINVAL:
goto out;
default:
bad_error("libscf_get_milestone", r);
}
switch (r) {
case 0:
case ECONNRESET:
case EALREADY:
case EINVAL:
case ENOENT:
break;
default:
bad_error("dgraph_set_milestone", r);
}
out:
}
/*
* void *graph_thread(void *)
*
* Graph management thread.
*/
/*ARGSUSED*/
void *
graph_thread(void *arg)
{
scf_handle_t *h;
int err;
if (st->st_initial)
if (!st->st_initial)
/*
* Now that we've set st_load_complete we need to check can_come_up()
* since if we booted to a milestone, then there won't be any more
* state updates.
*/
if (!go_single_user_mode && !go_to_level1 &&
halting == -1) {
if (!sulogin_thread_running && !can_come_up()) {
}
}
/*CONSTCOND*/
while (1) {
&gu->gu_freeze_lock);
}
/*
* Unreachable for now -- there's currently no graceful cleanup
* called on exit().
*/
return (NULL);
}
/*
* int next_action()
* Given an array of timestamps 'a' with 'num' elements, find the
* lowest non-zero timestamp and return its index. If there are no
* non-zero elements, return -1.
*/
static int
{
hrtime_t t = 0;
int i = 0, smallest = -1;
for (i = 0; i < num; i++) {
if (t == 0) {
t = a[i];
smallest = i;
} else if (a[i] != 0 && a[i] < t) {
t = a[i];
smallest = i;
}
}
if (t == 0)
return (-1);
else
return (smallest);
}
/*
* void process_actions()
* Process actions requested by the administrator. Possibilities include:
* refresh, restart, maintenance mode off, maintenance mode on,
* maintenance mode immediate, and degraded.
*
* The set of pending actions is represented in the repository as a
* per-instance property group, with each action being a single property
* in that group. This property group is converted to an array, with each
* action type having an array slot. The actions in the array at the
* time process_actions() is called are acted on in the order of the
* timestamp (which is the value stored in the slot). A value of zero
* indicates that there is no pending action of the type associated with
* a particular slot.
*
* Sending an action event multiple times before the restarter has a
* chance to process that action will force it to be run at the last
* timestamp where it appears in the ordering.
*
* Turning maintenance mode on trumps all other actions.
*
* Returns 0 or ECONNABORTED.
*/
static int
{
int i, ret = 0, r;
char *inst_name;
switch (r) {
case 0:
break;
case ECONNABORTED:
return (ECONNABORTED);
case ECANCELED:
return (0);
default:
bad_error("libscf_instance_get_fmri", r);
}
"The instance must have been removed.\n", inst_name);
return (0);
}
prop = safe_scf_property_create(h);
val = safe_scf_value_create(h);
for (i = 0; i < NACTIONS; i++) {
switch (scf_error()) {
default:
ret = ECONNABORTED;
goto out;
case SCF_ERROR_DELETED:
goto out;
case SCF_ERROR_NOT_FOUND:
action_ts[i] = 0;
continue;
case SCF_ERROR_NOT_SET:
}
}
switch (scf_error()) {
default:
ret = ECONNABORTED;
goto out;
case SCF_ERROR_DELETED:
action_ts[i] = 0;
continue;
case SCF_ERROR_NOT_SET:
}
}
if (type != SCF_TYPE_INTEGER) {
action_ts[i] = 0;
continue;
}
switch (scf_error()) {
default:
ret = ECONNABORTED;
goto out;
case SCF_ERROR_DELETED:
goto out;
case SCF_ERROR_NOT_FOUND:
action_ts[i] = 0;
continue;
case SCF_ERROR_NOT_SET:
bad_error("scf_property_get_value",
scf_error());
}
}
assert(r == 0);
}
switch (r) {
case 0:
case EACCES:
break;
case ECONNABORTED:
ret = ECONNABORTED;
goto out;
case EPERM:
uu_die("Insufficient privilege.\n");
/* NOTREACHED */
default:
bad_error("libscf_unset_action", r);
}
}
"Graph: processing %s action for %s.\n", admin_actions[a],
if (a == ADMIN_EVENT_REFRESH) {
switch (r) {
case 0:
case ECANCELED:
case EINVAL:
case -1:
break;
case ECONNABORTED:
/* pg & inst are reset now, so just return. */
ret = ECONNABORTED;
goto out;
default:
bad_error("dgraph_refresh_instance", r);
}
}
switch (r) {
case 0:
case EACCES:
break;
case ECONNABORTED:
ret = ECONNABORTED;
goto out;
case EPERM:
uu_die("Insufficient privilege.\n");
/* NOTREACHED */
default:
bad_error("libscf_unset_action", r);
}
action_ts[a] = 0;
}
out:
return (ret);
}
/*
* inst and pg_name are scratch space, and are unset on entry.
* Returns
* 0 - success
* ECONNRESET - success, but repository handle rebound
* ECONNABORTED - repository connection broken
*/
static int
char *pg_name)
{
int r;
char *fmri;
switch (scf_error()) {
default:
return (ECONNABORTED);
case SCF_ERROR_DELETED:
return (0);
case SCF_ERROR_NOT_SET:
}
}
r = dgraph_update_general(pg);
switch (r) {
case 0:
case ENOTSUP:
case ECANCELED:
return (0);
case ECONNABORTED:
return (ECONNABORTED);
case -1:
/* Error should have been logged. */
return (0);
default:
bad_error("dgraph_update_general", r);
}
switch (scf_error()) {
return (ECONNABORTED);
case SCF_ERROR_DELETED:
/* Ignore commands on services. */
return (0);
case SCF_ERROR_NOT_BOUND:
case SCF_ERROR_NOT_SET:
default:
bad_error("scf_pg_get_parent_instance",
scf_error());
}
}
}
return (0);
/*
* We only care about the options[_ovr] property groups of our own
* instance, so get the fmri and compare. Plus, once we know it's
* correct, if the repository connection is broken we know exactly what
* property group we were operating on, and can look it up again.
*/
switch (scf_error()) {
return (ECONNABORTED);
case SCF_ERROR_DELETED:
return (0);
case SCF_ERROR_NOT_BOUND:
case SCF_ERROR_NOT_SET:
default:
bad_error("scf_pg_get_parent_instance",
scf_error());
}
}
case 0:
break;
case ECONNABORTED:
return (ECONNABORTED);
case ECANCELED:
return (0);
default:
bad_error("libscf_instance_get_fmri", r);
}
return (0);
}
prop = safe_scf_property_create(h);
val = safe_scf_value_create(h);
/* See if we need to set the runlevel. */
/* CONSTCOND */
if (0) {
switch (r) {
case 0:
break;
case ECONNABORTED:
goto rebind_pg;
case ENOENT:
goto out;
case EINVAL:
case ENOTSUP:
bad_error("libscf_lookup_instance", r);
}
switch (scf_error()) {
case SCF_ERROR_DELETED:
case SCF_ERROR_NOT_FOUND:
goto out;
goto rebind_pg;
case SCF_ERROR_NOT_BOUND:
case SCF_ERROR_NOT_SET:
default:
bad_error("scf_instance_get_pg",
scf_error());
}
}
}
switch (r) {
case ECONNRESET:
/* FALLTHROUGH */
case 0:
break;
case ECONNABORTED:
goto rebind_pg;
case ECANCELED:
goto out;
default:
bad_error("dgraph_set_runlevel", r);
}
} else {
switch (scf_error()) {
default:
goto rebind_pg;
case SCF_ERROR_DELETED:
goto out;
case SCF_ERROR_NOT_FOUND:
break;
case SCF_ERROR_NOT_BOUND:
case SCF_ERROR_NOT_SET:
}
}
}
if (rebind_inst) {
switch (r) {
case 0:
break;
case ECONNABORTED:
goto lookup_inst;
case ENOENT:
goto out;
case EINVAL:
case ENOTSUP:
bad_error("libscf_lookup_instance", r);
}
}
switch (r) {
case 0:
break;
case ECONNABORTED:
goto lookup_inst;
case EINVAL:
"%s/%s property of %s is misconfigured.\n", pg_name,
/* FALLTHROUGH */
case ECANCELED:
case ENOENT:
break;
default:
bad_error("libscf_get_milestone", r);
}
switch (r) {
case 0:
case ECONNRESET:
case EALREADY:
break;
case EINVAL:
break;
case ENOENT:
break;
default:
bad_error("dgraph_set_milestone", r);
}
out:
return (rebound ? ECONNRESET : 0);
}
static void
{
char *lfmri;
/* Determine if the FMRI is a property group or instance */
NULL) != SCF_SUCCESS) {
"Received invalid FMRI \"%s\" from repository server.\n",
fmri);
(void) dgraph_remove_instance(fmri, h);
}
}
/*ARGSUSED*/
void *
repository_event_thread(void *unused)
{
scf_handle_t *h;
int r;
pg = safe_scf_pg_create(h);
inst = safe_scf_instance_create(h);
if (scf_error() == SCF_ERROR_CONNECTION_BROKEN) {
} else {
"Couldn't set up repository notification "
"for property group type %s: %s\n",
(void) sleep(1);
}
goto retry;
}
/*CONSTCOND*/
while (1) {
/* Note: fmri is only set on delete events. */
if (res < 0) {
goto retry;
} else if (res == 0) {
/*
* property group modified. inst and pg_name are
* pre-allocated scratch space.
*/
if (scf_pg_update(pg) < 0) {
switch (scf_error()) {
case SCF_ERROR_DELETED:
continue;
"Lost repository event due to "
"disconnection.\n");
goto retry;
case SCF_ERROR_NOT_BOUND:
case SCF_ERROR_NOT_SET:
default:
}
}
switch (r) {
case 0:
break;
case ECONNABORTED:
"due to disconnection.\n");
/* FALLTHROUGH */
case ECONNRESET:
goto retry;
default:
bad_error("process_pg_event", r);
}
} else {
/* service, instance, or pg deleted. */
process_delete(fmri, h);
}
}
/*NOTREACHED*/
return (NULL);
}
void
{
int err;
while (!initial_milestone_set) {
}
}