xref: /titanic_53/usr/src/cmd/svc/startd/protocol.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * protocol.c - protocols between graph engine and restarters
31*7c478bd9Sstevel@tonic-gate  *
32*7c478bd9Sstevel@tonic-gate  *   The graph engine uses restarter_protocol_send_event() to send a
33*7c478bd9Sstevel@tonic-gate  *   restarter_event_type_t to the restarter.  For delegated restarters,
34*7c478bd9Sstevel@tonic-gate  *   this is published on the GPEC queue for the restarter, which can
35*7c478bd9Sstevel@tonic-gate  *   then be consumed by the librestart interfaces.  For services managed
36*7c478bd9Sstevel@tonic-gate  *   by svc.startd, the event is stored on the local restarter_queue list,
37*7c478bd9Sstevel@tonic-gate  *   where it can be dequeued by the restarter.
38*7c478bd9Sstevel@tonic-gate  *
39*7c478bd9Sstevel@tonic-gate  *   The svc.startd restarter uses graph_protocol_send_event() to send
40*7c478bd9Sstevel@tonic-gate  *   a graph_event_type_t to the graph engine when an instance's states are
41*7c478bd9Sstevel@tonic-gate  *   updated.
42*7c478bd9Sstevel@tonic-gate  *
43*7c478bd9Sstevel@tonic-gate  *   The graph engine uses restarter_protocol_init_delegate() to
44*7c478bd9Sstevel@tonic-gate  *   register its interest in a particular delegated restarter's instance
45*7c478bd9Sstevel@tonic-gate  *   state events.  The state_cb() registered on the event channel then
46*7c478bd9Sstevel@tonic-gate  *   invokes graph_protocol_send_event() to communicate the update to
47*7c478bd9Sstevel@tonic-gate  *   the graph engine.
48*7c478bd9Sstevel@tonic-gate  */
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate #include <assert.h>
51*7c478bd9Sstevel@tonic-gate #include <libintl.h>
52*7c478bd9Sstevel@tonic-gate #include <libsysevent.h>
53*7c478bd9Sstevel@tonic-gate #include <pthread.h>
54*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
55*7c478bd9Sstevel@tonic-gate #include <stdio.h>
56*7c478bd9Sstevel@tonic-gate #include <strings.h>
57*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
58*7c478bd9Sstevel@tonic-gate #include <errno.h>
59*7c478bd9Sstevel@tonic-gate #include <libuutil.h>
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate #include <librestart.h>
62*7c478bd9Sstevel@tonic-gate #include <librestart_priv.h>
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate #include "protocol.h"
65*7c478bd9Sstevel@tonic-gate #include "startd.h"
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate /* Local event queue structures. */
68*7c478bd9Sstevel@tonic-gate typedef struct graph_protocol_event_queue {
69*7c478bd9Sstevel@tonic-gate 	uu_list_t		*gpeq_event_list;
70*7c478bd9Sstevel@tonic-gate 	pthread_mutex_t		gpeq_lock;
71*7c478bd9Sstevel@tonic-gate } graph_protocol_event_queue_t;
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate typedef struct restarter_protocol_event_queue {
74*7c478bd9Sstevel@tonic-gate 	uu_list_t		*rpeq_event_list;
75*7c478bd9Sstevel@tonic-gate 	pthread_mutex_t		rpeq_lock;
76*7c478bd9Sstevel@tonic-gate } restarter_protocol_event_queue_t;
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate static uu_list_pool_t *restarter_protocol_event_queue_pool;
79*7c478bd9Sstevel@tonic-gate static restarter_protocol_event_queue_t *restarter_queue;
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate static uu_list_pool_t *graph_protocol_event_queue_pool;
82*7c478bd9Sstevel@tonic-gate static graph_protocol_event_queue_t *graph_queue;
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate void
85*7c478bd9Sstevel@tonic-gate graph_protocol_init()
86*7c478bd9Sstevel@tonic-gate {
87*7c478bd9Sstevel@tonic-gate 	graph_protocol_event_queue_pool = startd_list_pool_create(
88*7c478bd9Sstevel@tonic-gate 	    "graph_protocol_events", sizeof (graph_protocol_event_t),
89*7c478bd9Sstevel@tonic-gate 	    offsetof(graph_protocol_event_t, gpe_link), NULL,
90*7c478bd9Sstevel@tonic-gate 	    UU_LIST_POOL_DEBUG);
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	graph_queue = startd_zalloc(sizeof (graph_protocol_event_queue_t));
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&graph_queue->gpeq_lock, &mutex_attrs);
95*7c478bd9Sstevel@tonic-gate 	graph_queue->gpeq_event_list = startd_list_create(
96*7c478bd9Sstevel@tonic-gate 	    graph_protocol_event_queue_pool, graph_queue, NULL);
97*7c478bd9Sstevel@tonic-gate }
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate /*
100*7c478bd9Sstevel@tonic-gate  * "data" will be freed by the consumer
101*7c478bd9Sstevel@tonic-gate  */
102*7c478bd9Sstevel@tonic-gate static void
103*7c478bd9Sstevel@tonic-gate graph_event_enqueue(const char *inst, graph_event_type_t event,
104*7c478bd9Sstevel@tonic-gate     protocol_states_t *data)
105*7c478bd9Sstevel@tonic-gate {
106*7c478bd9Sstevel@tonic-gate 	graph_protocol_event_t *e;
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 	e = startd_zalloc(sizeof (graph_protocol_event_t));
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 	if (inst != NULL) {
111*7c478bd9Sstevel@tonic-gate 		int size = strlen(inst) + 1;
112*7c478bd9Sstevel@tonic-gate 		e->gpe_inst = startd_alloc(size);
113*7c478bd9Sstevel@tonic-gate 		e->gpe_inst_sz = size;
114*7c478bd9Sstevel@tonic-gate 		(void) strlcpy(e->gpe_inst, inst, size);
115*7c478bd9Sstevel@tonic-gate 	}
116*7c478bd9Sstevel@tonic-gate 	e->gpe_type = event;
117*7c478bd9Sstevel@tonic-gate 	e->gpe_data = data;
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&e->gpe_lock, &mutex_attrs);
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&graph_queue->gpeq_lock);
122*7c478bd9Sstevel@tonic-gate 	uu_list_node_init(e, &e->gpe_link, graph_protocol_event_queue_pool);
123*7c478bd9Sstevel@tonic-gate 	if (uu_list_insert_before(graph_queue->gpeq_event_list, NULL, e) == -1)
124*7c478bd9Sstevel@tonic-gate 		uu_die("failed to enqueue graph event (%s: %s)\n",
125*7c478bd9Sstevel@tonic-gate 		    e->gpe_inst, uu_strerror(uu_error()));
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&graph_queue->gpeq_lock);
128*7c478bd9Sstevel@tonic-gate }
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate void
131*7c478bd9Sstevel@tonic-gate graph_event_release(graph_protocol_event_t *e)
132*7c478bd9Sstevel@tonic-gate {
133*7c478bd9Sstevel@tonic-gate 	uu_list_node_fini(e, &e->gpe_link, graph_protocol_event_queue_pool);
134*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_destroy(&e->gpe_lock);
135*7c478bd9Sstevel@tonic-gate 	if (e->gpe_inst != NULL)
136*7c478bd9Sstevel@tonic-gate 		startd_free(e->gpe_inst, e->gpe_inst_sz);
137*7c478bd9Sstevel@tonic-gate 	startd_free(e, sizeof (graph_protocol_event_t));
138*7c478bd9Sstevel@tonic-gate }
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate /*
141*7c478bd9Sstevel@tonic-gate  * graph_protocol_event_t *graph_event_dequeue()
142*7c478bd9Sstevel@tonic-gate  *   The caller must hold gu_lock, and is expected to be a single thread.
143*7c478bd9Sstevel@tonic-gate  *   It is allowed to utilize graph_event_requeue() and abort processing
144*7c478bd9Sstevel@tonic-gate  *   on the event. If graph_event_requeue() is not called, the caller is
145*7c478bd9Sstevel@tonic-gate  *   expected to call graph_event_release() when finished.
146*7c478bd9Sstevel@tonic-gate  */
147*7c478bd9Sstevel@tonic-gate graph_protocol_event_t *
148*7c478bd9Sstevel@tonic-gate graph_event_dequeue()
149*7c478bd9Sstevel@tonic-gate {
150*7c478bd9Sstevel@tonic-gate 	graph_protocol_event_t *e;
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&graph_queue->gpeq_lock);
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 	e = uu_list_first(graph_queue->gpeq_event_list);
155*7c478bd9Sstevel@tonic-gate 	if (e == NULL) {
156*7c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&graph_queue->gpeq_lock);
157*7c478bd9Sstevel@tonic-gate 		return (NULL);
158*7c478bd9Sstevel@tonic-gate 	}
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 	if (uu_list_next(graph_queue->gpeq_event_list, e) != NULL)
161*7c478bd9Sstevel@tonic-gate 		gu->gu_wakeup = 1;
162*7c478bd9Sstevel@tonic-gate 	uu_list_remove(graph_queue->gpeq_event_list, e);
163*7c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&graph_queue->gpeq_lock);
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 	return (e);
166*7c478bd9Sstevel@tonic-gate }
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate /*
169*7c478bd9Sstevel@tonic-gate  * void graph_event_requeue()
170*7c478bd9Sstevel@tonic-gate  *   Requeue the event back at the head of the queue.
171*7c478bd9Sstevel@tonic-gate  */
172*7c478bd9Sstevel@tonic-gate void
173*7c478bd9Sstevel@tonic-gate graph_event_requeue(graph_protocol_event_t *e)
174*7c478bd9Sstevel@tonic-gate {
175*7c478bd9Sstevel@tonic-gate 	assert(e != NULL);
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Requeing event\n");
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&graph_queue->gpeq_lock);
180*7c478bd9Sstevel@tonic-gate 	if (uu_list_insert_after(graph_queue->gpeq_event_list, NULL, e) == -1)
181*7c478bd9Sstevel@tonic-gate 		uu_die("failed to requeue graph event (%s: %s)\n",
182*7c478bd9Sstevel@tonic-gate 		    e->gpe_inst, uu_strerror(uu_error()));
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&graph_queue->gpeq_lock);
185*7c478bd9Sstevel@tonic-gate }
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate void
188*7c478bd9Sstevel@tonic-gate graph_protocol_send_event(const char *inst, graph_event_type_t event,
189*7c478bd9Sstevel@tonic-gate     protocol_states_t *data)
190*7c478bd9Sstevel@tonic-gate {
191*7c478bd9Sstevel@tonic-gate 	graph_event_enqueue(inst, event, data);
192*7c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&gu->gu_lock);
193*7c478bd9Sstevel@tonic-gate 	gu->gu_wakeup = 1;
194*7c478bd9Sstevel@tonic-gate 	(void) pthread_cond_broadcast(&gu->gu_cv);
195*7c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&gu->gu_lock);
196*7c478bd9Sstevel@tonic-gate }
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate void
199*7c478bd9Sstevel@tonic-gate restarter_protocol_init()
200*7c478bd9Sstevel@tonic-gate {
201*7c478bd9Sstevel@tonic-gate 	restarter_protocol_event_queue_pool = startd_list_pool_create(
202*7c478bd9Sstevel@tonic-gate 	    "restarter_protocol_events", sizeof (restarter_protocol_event_t),
203*7c478bd9Sstevel@tonic-gate 	    offsetof(restarter_protocol_event_t, rpe_link), NULL,
204*7c478bd9Sstevel@tonic-gate 	    UU_LIST_POOL_DEBUG);
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	restarter_queue = startd_zalloc(
207*7c478bd9Sstevel@tonic-gate 	    sizeof (restarter_protocol_event_queue_t));
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&restarter_queue->rpeq_lock, &mutex_attrs);
210*7c478bd9Sstevel@tonic-gate 	restarter_queue->rpeq_event_list = startd_list_create(
211*7c478bd9Sstevel@tonic-gate 	    restarter_protocol_event_queue_pool, restarter_queue, NULL);
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Initialized restarter protocol\n");
214*7c478bd9Sstevel@tonic-gate }
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate /*
217*7c478bd9Sstevel@tonic-gate  * void restarter_event_enqueue()
218*7c478bd9Sstevel@tonic-gate  *   Enqueue a restarter event.
219*7c478bd9Sstevel@tonic-gate  */
220*7c478bd9Sstevel@tonic-gate static void
221*7c478bd9Sstevel@tonic-gate restarter_event_enqueue(const char *inst, restarter_event_type_t event)
222*7c478bd9Sstevel@tonic-gate {
223*7c478bd9Sstevel@tonic-gate 	restarter_protocol_event_t *e;
224*7c478bd9Sstevel@tonic-gate 	int r;
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate 	/* Allocate and populate the event structure. */
227*7c478bd9Sstevel@tonic-gate 	e = startd_zalloc(sizeof (restarter_protocol_event_t));
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate 	e->rpe_inst = startd_alloc(strlen(inst) + 1);
230*7c478bd9Sstevel@tonic-gate 	(void) strlcpy(e->rpe_inst, inst, strlen(inst)+1);
231*7c478bd9Sstevel@tonic-gate 	e->rpe_type = event;
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&restarter_queue->rpeq_lock);
234*7c478bd9Sstevel@tonic-gate 	uu_list_node_init(e, &e->rpe_link, restarter_protocol_event_queue_pool);
235*7c478bd9Sstevel@tonic-gate 	r = uu_list_insert_before(restarter_queue->rpeq_event_list, NULL, e);
236*7c478bd9Sstevel@tonic-gate 	assert(r == 0);
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&restarter_queue->rpeq_lock);
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate }
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate void
243*7c478bd9Sstevel@tonic-gate restarter_event_release(restarter_protocol_event_t *e)
244*7c478bd9Sstevel@tonic-gate {
245*7c478bd9Sstevel@tonic-gate 	uu_list_node_fini(e, &e->rpe_link, restarter_protocol_event_queue_pool);
246*7c478bd9Sstevel@tonic-gate 	startd_free(e->rpe_inst, strlen(e->rpe_inst) + 1);
247*7c478bd9Sstevel@tonic-gate 	startd_free(e, sizeof (restarter_protocol_event_t));
248*7c478bd9Sstevel@tonic-gate }
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate /*
251*7c478bd9Sstevel@tonic-gate  * restarter_protocol_event_t *restarter_event_dequeue()
252*7c478bd9Sstevel@tonic-gate  *   Dequeue a restarter protocol event. The caller is expected to be
253*7c478bd9Sstevel@tonic-gate  *   a single thread. It is allowed to utilize restarter_event_requeue()
254*7c478bd9Sstevel@tonic-gate  *   and abort processing on the event. The caller is expected to call
255*7c478bd9Sstevel@tonic-gate  *   restarter_event_release() when finished.
256*7c478bd9Sstevel@tonic-gate  */
257*7c478bd9Sstevel@tonic-gate restarter_protocol_event_t *
258*7c478bd9Sstevel@tonic-gate restarter_event_dequeue()
259*7c478bd9Sstevel@tonic-gate {
260*7c478bd9Sstevel@tonic-gate 	restarter_protocol_event_t *e = NULL;
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&restarter_queue->rpeq_lock);
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate 	e = uu_list_first(restarter_queue->rpeq_event_list);
265*7c478bd9Sstevel@tonic-gate 	if (e == NULL) {
266*7c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&restarter_queue->rpeq_lock);
267*7c478bd9Sstevel@tonic-gate 		return (NULL);
268*7c478bd9Sstevel@tonic-gate 	}
269*7c478bd9Sstevel@tonic-gate 
270*7c478bd9Sstevel@tonic-gate 	if (uu_list_next(restarter_queue->rpeq_event_list, e) != NULL)
271*7c478bd9Sstevel@tonic-gate 		ru->restarter_update_wakeup = 1;
272*7c478bd9Sstevel@tonic-gate 	uu_list_remove(restarter_queue->rpeq_event_list, e);
273*7c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&restarter_queue->rpeq_lock);
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate 	return (e);
276*7c478bd9Sstevel@tonic-gate }
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate static int
279*7c478bd9Sstevel@tonic-gate state_cb(sysevent_t *syse, void *cookie)
280*7c478bd9Sstevel@tonic-gate {
281*7c478bd9Sstevel@tonic-gate 	char *fmri = (char *)cookie;
282*7c478bd9Sstevel@tonic-gate 	char *instance_name;
283*7c478bd9Sstevel@tonic-gate 	nvlist_t *attr_list = NULL;
284*7c478bd9Sstevel@tonic-gate 	int state, next_state;
285*7c478bd9Sstevel@tonic-gate 	protocol_states_t *states;
286*7c478bd9Sstevel@tonic-gate 	int err;
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate 	/*
289*7c478bd9Sstevel@tonic-gate 	 * Might fail due to a bad event or a lack of memory. Try
290*7c478bd9Sstevel@tonic-gate 	 * the callback again to see if it goes better the next time.
291*7c478bd9Sstevel@tonic-gate 	 */
292*7c478bd9Sstevel@tonic-gate 	if (sysevent_get_attr_list(syse, &attr_list) != 0)
293*7c478bd9Sstevel@tonic-gate 		return (EAGAIN);
294*7c478bd9Sstevel@tonic-gate 
295*7c478bd9Sstevel@tonic-gate 	if ((nvlist_lookup_int32(attr_list, RESTARTER_NAME_STATE,
296*7c478bd9Sstevel@tonic-gate 	    &state) != 0) ||
297*7c478bd9Sstevel@tonic-gate 	    (nvlist_lookup_int32(attr_list, RESTARTER_NAME_NEXT_STATE,
298*7c478bd9Sstevel@tonic-gate 	    &next_state) != 0) ||
299*7c478bd9Sstevel@tonic-gate 	    (nvlist_lookup_int32(attr_list, RESTARTER_NAME_ERROR, &err) != 0) ||
300*7c478bd9Sstevel@tonic-gate 	    (nvlist_lookup_string(attr_list, RESTARTER_NAME_INSTANCE,
301*7c478bd9Sstevel@tonic-gate 	    &instance_name) != 0))
302*7c478bd9Sstevel@tonic-gate 		uu_die("%s: can't decode nvlist\n", fmri);
303*7c478bd9Sstevel@tonic-gate 
304*7c478bd9Sstevel@tonic-gate 	states = startd_alloc(sizeof (protocol_states_t));
305*7c478bd9Sstevel@tonic-gate 	states->ps_state = state;
306*7c478bd9Sstevel@tonic-gate 	states->ps_state_next = next_state;
307*7c478bd9Sstevel@tonic-gate 	states->ps_err = err;
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate 	graph_protocol_send_event(instance_name, GRAPH_UPDATE_STATE_CHANGE,
310*7c478bd9Sstevel@tonic-gate 	    states);
311*7c478bd9Sstevel@tonic-gate 
312*7c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "%s: state updates for %s (%d, %d)\n", fmri,
313*7c478bd9Sstevel@tonic-gate 	    instance_name, state, next_state);
314*7c478bd9Sstevel@tonic-gate 	nvlist_free(attr_list);
315*7c478bd9Sstevel@tonic-gate 	return (0);
316*7c478bd9Sstevel@tonic-gate }
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate evchan_t *
319*7c478bd9Sstevel@tonic-gate restarter_protocol_init_delegate(char *fmri)
320*7c478bd9Sstevel@tonic-gate {
321*7c478bd9Sstevel@tonic-gate 	char *delegate_channel_name, *master_channel_name, *sid;
322*7c478bd9Sstevel@tonic-gate 	evchan_t *delegate_channel, *master_channel;
323*7c478bd9Sstevel@tonic-gate 
324*7c478bd9Sstevel@tonic-gate 	/* master restarter -- nothing to do */
325*7c478bd9Sstevel@tonic-gate 	if (strcmp(fmri, SCF_SERVICE_STARTD) == 0)
326*7c478bd9Sstevel@tonic-gate 		return (NULL);
327*7c478bd9Sstevel@tonic-gate 
328*7c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "%s: Intializing protocol for delegate\n",
329*7c478bd9Sstevel@tonic-gate 	    fmri);
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate 	if ((delegate_channel_name = _restarter_get_channel_name(fmri,
332*7c478bd9Sstevel@tonic-gate 	    RESTARTER_CHANNEL_DELEGATE)) == NULL ||
333*7c478bd9Sstevel@tonic-gate 	    (master_channel_name = _restarter_get_channel_name(fmri,
334*7c478bd9Sstevel@tonic-gate 	    RESTARTER_CHANNEL_MASTER)) == NULL ||
335*7c478bd9Sstevel@tonic-gate 	    (sid = strdup("svc.startd")) == NULL)
336*7c478bd9Sstevel@tonic-gate 		uu_die("Allocation failure\n");
337*7c478bd9Sstevel@tonic-gate 
338*7c478bd9Sstevel@tonic-gate 	if (sysevent_evc_bind(delegate_channel_name, &delegate_channel,
339*7c478bd9Sstevel@tonic-gate 	    EVCH_CREAT|EVCH_HOLD_PEND) != 0)
340*7c478bd9Sstevel@tonic-gate 		uu_die("%s: sysevent_evc_bind failed: %s\n",
341*7c478bd9Sstevel@tonic-gate 		    delegate_channel_name, strerror(errno));
342*7c478bd9Sstevel@tonic-gate 	if (sysevent_evc_bind(master_channel_name, &master_channel,
343*7c478bd9Sstevel@tonic-gate 	    EVCH_CREAT|EVCH_HOLD_PEND) != 0)
344*7c478bd9Sstevel@tonic-gate 		uu_die("%s: sysevent_evc_bind failed: %s\n",
345*7c478bd9Sstevel@tonic-gate 		    master_channel_name, strerror(errno));
346*7c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG,
347*7c478bd9Sstevel@tonic-gate 	    "%s: Bound to channel %s (delegate), %s (master)\n", fmri,
348*7c478bd9Sstevel@tonic-gate 	    delegate_channel_name, master_channel_name);
349*7c478bd9Sstevel@tonic-gate 
350*7c478bd9Sstevel@tonic-gate 	if (sysevent_evc_subscribe(master_channel, sid, EC_ALL,
351*7c478bd9Sstevel@tonic-gate 	    state_cb, fmri, EVCH_SUB_KEEP) != 0)
352*7c478bd9Sstevel@tonic-gate 		uu_die("%s: Failed to subscribe to channel %s with "
353*7c478bd9Sstevel@tonic-gate 		    "subscriber id %s: %s\n", fmri,
354*7c478bd9Sstevel@tonic-gate 		    master_channel_name, sid, strerror(errno));
355*7c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG,
356*7c478bd9Sstevel@tonic-gate 	    "%s: Subscribed to channel %s with subscriber id %s\n", fmri,
357*7c478bd9Sstevel@tonic-gate 	    master_channel_name, "svc.startd");
358*7c478bd9Sstevel@tonic-gate 
359*7c478bd9Sstevel@tonic-gate 	free(delegate_channel_name);
360*7c478bd9Sstevel@tonic-gate 	free(master_channel_name);
361*7c478bd9Sstevel@tonic-gate 	free(sid);
362*7c478bd9Sstevel@tonic-gate 
363*7c478bd9Sstevel@tonic-gate 	return (delegate_channel);
364*7c478bd9Sstevel@tonic-gate }
365*7c478bd9Sstevel@tonic-gate 
366*7c478bd9Sstevel@tonic-gate void
367*7c478bd9Sstevel@tonic-gate restarter_protocol_send_event(const char *inst, evchan_t *chan,
368*7c478bd9Sstevel@tonic-gate     restarter_event_type_t event)
369*7c478bd9Sstevel@tonic-gate {
370*7c478bd9Sstevel@tonic-gate 	nvlist_t *attr;
371*7c478bd9Sstevel@tonic-gate 
372*7c478bd9Sstevel@tonic-gate 	/*
373*7c478bd9Sstevel@tonic-gate 	 * If the service is managed by the master restarter,
374*7c478bd9Sstevel@tonic-gate 	 * queue the event locally.
375*7c478bd9Sstevel@tonic-gate 	 */
376*7c478bd9Sstevel@tonic-gate 	if (chan == NULL) {
377*7c478bd9Sstevel@tonic-gate 		restarter_event_enqueue(inst, event);
378*7c478bd9Sstevel@tonic-gate 		MUTEX_LOCK(&ru->restarter_update_lock);
379*7c478bd9Sstevel@tonic-gate 		ru->restarter_update_wakeup = 1;
380*7c478bd9Sstevel@tonic-gate 		(void) pthread_cond_broadcast(&ru->restarter_update_cv);
381*7c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&ru->restarter_update_lock);
382*7c478bd9Sstevel@tonic-gate 		return;
383*7c478bd9Sstevel@tonic-gate 	}
384*7c478bd9Sstevel@tonic-gate 
385*7c478bd9Sstevel@tonic-gate 	/*
386*7c478bd9Sstevel@tonic-gate 	 * Otherwise, send the event to the delegate.
387*7c478bd9Sstevel@tonic-gate 	 */
388*7c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Sending %s to channel 0x%p for %s.\n",
389*7c478bd9Sstevel@tonic-gate 	    event_names[event], chan, inst);
390*7c478bd9Sstevel@tonic-gate 	if (nvlist_alloc(&attr, NV_UNIQUE_NAME, 0) != 0 ||
391*7c478bd9Sstevel@tonic-gate 	    nvlist_add_uint32(attr, RESTARTER_NAME_TYPE, event) != 0 ||
392*7c478bd9Sstevel@tonic-gate 	    nvlist_add_string(attr, RESTARTER_NAME_INSTANCE, (char *)inst) != 0)
393*7c478bd9Sstevel@tonic-gate 		uu_die("Allocation failure\n");
394*7c478bd9Sstevel@tonic-gate 
395*7c478bd9Sstevel@tonic-gate 	if (sysevent_evc_publish(chan, "protocol", "restarter", "com.sun",
396*7c478bd9Sstevel@tonic-gate 	    "svc.startd", attr, EVCH_NOSLEEP) != 0) {
397*7c478bd9Sstevel@tonic-gate 		if (errno == EAGAIN)
398*7c478bd9Sstevel@tonic-gate 			uu_die("%s: queue is full\n", inst);
399*7c478bd9Sstevel@tonic-gate 		uu_die("%s: can't publish event: %s\n", inst, strerror(errno));
400*7c478bd9Sstevel@tonic-gate 	}
401*7c478bd9Sstevel@tonic-gate 	nvlist_free(attr);
402*7c478bd9Sstevel@tonic-gate 
403*7c478bd9Sstevel@tonic-gate 	if (event != RESTARTER_EVENT_TYPE_ADD_INSTANCE) {
404*7c478bd9Sstevel@tonic-gate 		/*
405*7c478bd9Sstevel@tonic-gate 		 * Not relevant for graph loading.
406*7c478bd9Sstevel@tonic-gate 		 */
407*7c478bd9Sstevel@tonic-gate 		return;
408*7c478bd9Sstevel@tonic-gate 	}
409*7c478bd9Sstevel@tonic-gate 
410*7c478bd9Sstevel@tonic-gate 	/*
411*7c478bd9Sstevel@tonic-gate 	 * For the purposes of loading state after interruption, this is
412*7c478bd9Sstevel@tonic-gate 	 * sufficient, as svc.startd(1M) won't receive events on the contracts
413*7c478bd9Sstevel@tonic-gate 	 * associated with each delegate.
414*7c478bd9Sstevel@tonic-gate 	 */
415*7c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&st->st_load_lock);
416*7c478bd9Sstevel@tonic-gate 	if (--st->st_load_instances == 0)
417*7c478bd9Sstevel@tonic-gate 		(void) pthread_cond_broadcast(&st->st_load_cv);
418*7c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&st->st_load_lock);
419*7c478bd9Sstevel@tonic-gate 
420*7c478bd9Sstevel@tonic-gate }
421