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