xref: /titanic_50/usr/src/cmd/svc/startd/protocol.c (revision 73b709ea25679d17a96a8d7f8db6531cbcc800d5)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*73b709eaSrm88369  * Common Development and Distribution License (the "License").
6*73b709eaSrm88369  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*73b709eaSrm88369  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * protocol.c - protocols between graph engine and restarters
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  *   The graph engine uses restarter_protocol_send_event() to send a
327c478bd9Sstevel@tonic-gate  *   restarter_event_type_t to the restarter.  For delegated restarters,
337c478bd9Sstevel@tonic-gate  *   this is published on the GPEC queue for the restarter, which can
347c478bd9Sstevel@tonic-gate  *   then be consumed by the librestart interfaces.  For services managed
357c478bd9Sstevel@tonic-gate  *   by svc.startd, the event is stored on the local restarter_queue list,
367c478bd9Sstevel@tonic-gate  *   where it can be dequeued by the restarter.
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  *   The svc.startd restarter uses graph_protocol_send_event() to send
397c478bd9Sstevel@tonic-gate  *   a graph_event_type_t to the graph engine when an instance's states are
407c478bd9Sstevel@tonic-gate  *   updated.
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  *   The graph engine uses restarter_protocol_init_delegate() to
437c478bd9Sstevel@tonic-gate  *   register its interest in a particular delegated restarter's instance
447c478bd9Sstevel@tonic-gate  *   state events.  The state_cb() registered on the event channel then
457c478bd9Sstevel@tonic-gate  *   invokes graph_protocol_send_event() to communicate the update to
467c478bd9Sstevel@tonic-gate  *   the graph engine.
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #include <assert.h>
507c478bd9Sstevel@tonic-gate #include <libintl.h>
517c478bd9Sstevel@tonic-gate #include <libsysevent.h>
527c478bd9Sstevel@tonic-gate #include <pthread.h>
537c478bd9Sstevel@tonic-gate #include <stdarg.h>
547c478bd9Sstevel@tonic-gate #include <stdio.h>
557c478bd9Sstevel@tonic-gate #include <strings.h>
567c478bd9Sstevel@tonic-gate #include <sys/time.h>
577c478bd9Sstevel@tonic-gate #include <errno.h>
587c478bd9Sstevel@tonic-gate #include <libuutil.h>
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #include <librestart.h>
617c478bd9Sstevel@tonic-gate #include <librestart_priv.h>
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #include "protocol.h"
647c478bd9Sstevel@tonic-gate #include "startd.h"
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /* Local event queue structures. */
677c478bd9Sstevel@tonic-gate typedef struct graph_protocol_event_queue {
687c478bd9Sstevel@tonic-gate 	uu_list_t		*gpeq_event_list;
697c478bd9Sstevel@tonic-gate 	pthread_mutex_t		gpeq_lock;
707c478bd9Sstevel@tonic-gate } graph_protocol_event_queue_t;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate typedef struct restarter_protocol_event_queue {
737c478bd9Sstevel@tonic-gate 	uu_list_t		*rpeq_event_list;
747c478bd9Sstevel@tonic-gate 	pthread_mutex_t		rpeq_lock;
757c478bd9Sstevel@tonic-gate } restarter_protocol_event_queue_t;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate static uu_list_pool_t *restarter_protocol_event_queue_pool;
787c478bd9Sstevel@tonic-gate static restarter_protocol_event_queue_t *restarter_queue;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate static uu_list_pool_t *graph_protocol_event_queue_pool;
817c478bd9Sstevel@tonic-gate static graph_protocol_event_queue_t *graph_queue;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate void
847c478bd9Sstevel@tonic-gate graph_protocol_init()
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate 	graph_protocol_event_queue_pool = startd_list_pool_create(
877c478bd9Sstevel@tonic-gate 	    "graph_protocol_events", sizeof (graph_protocol_event_t),
887c478bd9Sstevel@tonic-gate 	    offsetof(graph_protocol_event_t, gpe_link), NULL,
897c478bd9Sstevel@tonic-gate 	    UU_LIST_POOL_DEBUG);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	graph_queue = startd_zalloc(sizeof (graph_protocol_event_queue_t));
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&graph_queue->gpeq_lock, &mutex_attrs);
947c478bd9Sstevel@tonic-gate 	graph_queue->gpeq_event_list = startd_list_create(
957c478bd9Sstevel@tonic-gate 	    graph_protocol_event_queue_pool, graph_queue, NULL);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate  * "data" will be freed by the consumer
1007c478bd9Sstevel@tonic-gate  */
1017c478bd9Sstevel@tonic-gate static void
1027c478bd9Sstevel@tonic-gate graph_event_enqueue(const char *inst, graph_event_type_t event,
1037c478bd9Sstevel@tonic-gate     protocol_states_t *data)
1047c478bd9Sstevel@tonic-gate {
1057c478bd9Sstevel@tonic-gate 	graph_protocol_event_t *e;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	e = startd_zalloc(sizeof (graph_protocol_event_t));
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	if (inst != NULL) {
1107c478bd9Sstevel@tonic-gate 		int size = strlen(inst) + 1;
1117c478bd9Sstevel@tonic-gate 		e->gpe_inst = startd_alloc(size);
1127c478bd9Sstevel@tonic-gate 		e->gpe_inst_sz = size;
1137c478bd9Sstevel@tonic-gate 		(void) strlcpy(e->gpe_inst, inst, size);
1147c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate 	e->gpe_type = event;
1167c478bd9Sstevel@tonic-gate 	e->gpe_data = data;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&e->gpe_lock, &mutex_attrs);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&graph_queue->gpeq_lock);
1217c478bd9Sstevel@tonic-gate 	uu_list_node_init(e, &e->gpe_link, graph_protocol_event_queue_pool);
1227c478bd9Sstevel@tonic-gate 	if (uu_list_insert_before(graph_queue->gpeq_event_list, NULL, e) == -1)
1237c478bd9Sstevel@tonic-gate 		uu_die("failed to enqueue graph event (%s: %s)\n",
1247c478bd9Sstevel@tonic-gate 		    e->gpe_inst, uu_strerror(uu_error()));
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&graph_queue->gpeq_lock);
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate void
1307c478bd9Sstevel@tonic-gate graph_event_release(graph_protocol_event_t *e)
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate 	uu_list_node_fini(e, &e->gpe_link, graph_protocol_event_queue_pool);
1337c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_destroy(&e->gpe_lock);
1347c478bd9Sstevel@tonic-gate 	if (e->gpe_inst != NULL)
1357c478bd9Sstevel@tonic-gate 		startd_free(e->gpe_inst, e->gpe_inst_sz);
1367c478bd9Sstevel@tonic-gate 	startd_free(e, sizeof (graph_protocol_event_t));
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate /*
1407c478bd9Sstevel@tonic-gate  * graph_protocol_event_t *graph_event_dequeue()
1417c478bd9Sstevel@tonic-gate  *   The caller must hold gu_lock, and is expected to be a single thread.
1427c478bd9Sstevel@tonic-gate  *   It is allowed to utilize graph_event_requeue() and abort processing
1437c478bd9Sstevel@tonic-gate  *   on the event. If graph_event_requeue() is not called, the caller is
1447c478bd9Sstevel@tonic-gate  *   expected to call graph_event_release() when finished.
1457c478bd9Sstevel@tonic-gate  */
1467c478bd9Sstevel@tonic-gate graph_protocol_event_t *
1477c478bd9Sstevel@tonic-gate graph_event_dequeue()
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate 	graph_protocol_event_t *e;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&graph_queue->gpeq_lock);
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	e = uu_list_first(graph_queue->gpeq_event_list);
1547c478bd9Sstevel@tonic-gate 	if (e == NULL) {
1557c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&graph_queue->gpeq_lock);
1567c478bd9Sstevel@tonic-gate 		return (NULL);
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	if (uu_list_next(graph_queue->gpeq_event_list, e) != NULL)
1607c478bd9Sstevel@tonic-gate 		gu->gu_wakeup = 1;
1617c478bd9Sstevel@tonic-gate 	uu_list_remove(graph_queue->gpeq_event_list, e);
1627c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&graph_queue->gpeq_lock);
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	return (e);
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate /*
1687c478bd9Sstevel@tonic-gate  * void graph_event_requeue()
1697c478bd9Sstevel@tonic-gate  *   Requeue the event back at the head of the queue.
1707c478bd9Sstevel@tonic-gate  */
1717c478bd9Sstevel@tonic-gate void
1727c478bd9Sstevel@tonic-gate graph_event_requeue(graph_protocol_event_t *e)
1737c478bd9Sstevel@tonic-gate {
1747c478bd9Sstevel@tonic-gate 	assert(e != NULL);
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Requeing event\n");
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&graph_queue->gpeq_lock);
1797c478bd9Sstevel@tonic-gate 	if (uu_list_insert_after(graph_queue->gpeq_event_list, NULL, e) == -1)
1807c478bd9Sstevel@tonic-gate 		uu_die("failed to requeue graph event (%s: %s)\n",
1817c478bd9Sstevel@tonic-gate 		    e->gpe_inst, uu_strerror(uu_error()));
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&graph_queue->gpeq_lock);
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate void
1877c478bd9Sstevel@tonic-gate graph_protocol_send_event(const char *inst, graph_event_type_t event,
1887c478bd9Sstevel@tonic-gate     protocol_states_t *data)
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate 	graph_event_enqueue(inst, event, data);
1917c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&gu->gu_lock);
1927c478bd9Sstevel@tonic-gate 	gu->gu_wakeup = 1;
1937c478bd9Sstevel@tonic-gate 	(void) pthread_cond_broadcast(&gu->gu_cv);
1947c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&gu->gu_lock);
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate void
1987c478bd9Sstevel@tonic-gate restarter_protocol_init()
1997c478bd9Sstevel@tonic-gate {
2007c478bd9Sstevel@tonic-gate 	restarter_protocol_event_queue_pool = startd_list_pool_create(
2017c478bd9Sstevel@tonic-gate 	    "restarter_protocol_events", sizeof (restarter_protocol_event_t),
2027c478bd9Sstevel@tonic-gate 	    offsetof(restarter_protocol_event_t, rpe_link), NULL,
2037c478bd9Sstevel@tonic-gate 	    UU_LIST_POOL_DEBUG);
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	restarter_queue = startd_zalloc(
2067c478bd9Sstevel@tonic-gate 	    sizeof (restarter_protocol_event_queue_t));
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&restarter_queue->rpeq_lock, &mutex_attrs);
2097c478bd9Sstevel@tonic-gate 	restarter_queue->rpeq_event_list = startd_list_create(
2107c478bd9Sstevel@tonic-gate 	    restarter_protocol_event_queue_pool, restarter_queue, NULL);
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Initialized restarter protocol\n");
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate /*
2167c478bd9Sstevel@tonic-gate  * void restarter_event_enqueue()
2177c478bd9Sstevel@tonic-gate  *   Enqueue a restarter event.
2187c478bd9Sstevel@tonic-gate  */
2197c478bd9Sstevel@tonic-gate static void
2207c478bd9Sstevel@tonic-gate restarter_event_enqueue(const char *inst, restarter_event_type_t event)
2217c478bd9Sstevel@tonic-gate {
2227c478bd9Sstevel@tonic-gate 	restarter_protocol_event_t *e;
2237c478bd9Sstevel@tonic-gate 	int r;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	/* Allocate and populate the event structure. */
2267c478bd9Sstevel@tonic-gate 	e = startd_zalloc(sizeof (restarter_protocol_event_t));
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	e->rpe_inst = startd_alloc(strlen(inst) + 1);
2297c478bd9Sstevel@tonic-gate 	(void) strlcpy(e->rpe_inst, inst, strlen(inst)+1);
2307c478bd9Sstevel@tonic-gate 	e->rpe_type = event;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&restarter_queue->rpeq_lock);
2337c478bd9Sstevel@tonic-gate 	uu_list_node_init(e, &e->rpe_link, restarter_protocol_event_queue_pool);
2347c478bd9Sstevel@tonic-gate 	r = uu_list_insert_before(restarter_queue->rpeq_event_list, NULL, e);
2357c478bd9Sstevel@tonic-gate 	assert(r == 0);
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&restarter_queue->rpeq_lock);
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate void
2427c478bd9Sstevel@tonic-gate restarter_event_release(restarter_protocol_event_t *e)
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate 	uu_list_node_fini(e, &e->rpe_link, restarter_protocol_event_queue_pool);
2457c478bd9Sstevel@tonic-gate 	startd_free(e->rpe_inst, strlen(e->rpe_inst) + 1);
2467c478bd9Sstevel@tonic-gate 	startd_free(e, sizeof (restarter_protocol_event_t));
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate /*
2507c478bd9Sstevel@tonic-gate  * restarter_protocol_event_t *restarter_event_dequeue()
2517c478bd9Sstevel@tonic-gate  *   Dequeue a restarter protocol event. The caller is expected to be
2527c478bd9Sstevel@tonic-gate  *   a single thread. It is allowed to utilize restarter_event_requeue()
2537c478bd9Sstevel@tonic-gate  *   and abort processing on the event. The caller is expected to call
2547c478bd9Sstevel@tonic-gate  *   restarter_event_release() when finished.
2557c478bd9Sstevel@tonic-gate  */
2567c478bd9Sstevel@tonic-gate restarter_protocol_event_t *
2577c478bd9Sstevel@tonic-gate restarter_event_dequeue()
2587c478bd9Sstevel@tonic-gate {
2597c478bd9Sstevel@tonic-gate 	restarter_protocol_event_t *e = NULL;
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&restarter_queue->rpeq_lock);
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	e = uu_list_first(restarter_queue->rpeq_event_list);
2647c478bd9Sstevel@tonic-gate 	if (e == NULL) {
2657c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&restarter_queue->rpeq_lock);
2667c478bd9Sstevel@tonic-gate 		return (NULL);
2677c478bd9Sstevel@tonic-gate 	}
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	if (uu_list_next(restarter_queue->rpeq_event_list, e) != NULL)
2707c478bd9Sstevel@tonic-gate 		ru->restarter_update_wakeup = 1;
2717c478bd9Sstevel@tonic-gate 	uu_list_remove(restarter_queue->rpeq_event_list, e);
2727c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&restarter_queue->rpeq_lock);
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	return (e);
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate static int
2787c478bd9Sstevel@tonic-gate state_cb(sysevent_t *syse, void *cookie)
2797c478bd9Sstevel@tonic-gate {
2807c478bd9Sstevel@tonic-gate 	char *fmri = (char *)cookie;
2817c478bd9Sstevel@tonic-gate 	char *instance_name;
2827c478bd9Sstevel@tonic-gate 	nvlist_t *attr_list = NULL;
2837c478bd9Sstevel@tonic-gate 	int state, next_state;
284*73b709eaSrm88369 	char str_state[MAX_SCF_STATE_STRING_SZ];
285*73b709eaSrm88369 	char str_next_state[MAX_SCF_STATE_STRING_SZ];
2867c478bd9Sstevel@tonic-gate 	protocol_states_t *states;
2877c478bd9Sstevel@tonic-gate 	int err;
288*73b709eaSrm88369 	ssize_t sz;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	/*
2917c478bd9Sstevel@tonic-gate 	 * Might fail due to a bad event or a lack of memory. Try
2927c478bd9Sstevel@tonic-gate 	 * the callback again to see if it goes better the next time.
2937c478bd9Sstevel@tonic-gate 	 */
2947c478bd9Sstevel@tonic-gate 	if (sysevent_get_attr_list(syse, &attr_list) != 0)
2957c478bd9Sstevel@tonic-gate 		return (EAGAIN);
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	if ((nvlist_lookup_int32(attr_list, RESTARTER_NAME_STATE,
2987c478bd9Sstevel@tonic-gate 	    &state) != 0) ||
2997c478bd9Sstevel@tonic-gate 	    (nvlist_lookup_int32(attr_list, RESTARTER_NAME_NEXT_STATE,
3007c478bd9Sstevel@tonic-gate 	    &next_state) != 0) ||
3017c478bd9Sstevel@tonic-gate 	    (nvlist_lookup_int32(attr_list, RESTARTER_NAME_ERROR, &err) != 0) ||
3027c478bd9Sstevel@tonic-gate 	    (nvlist_lookup_string(attr_list, RESTARTER_NAME_INSTANCE,
3037c478bd9Sstevel@tonic-gate 	    &instance_name) != 0))
3047c478bd9Sstevel@tonic-gate 		uu_die("%s: can't decode nvlist\n", fmri);
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	states = startd_alloc(sizeof (protocol_states_t));
3077c478bd9Sstevel@tonic-gate 	states->ps_state = state;
3087c478bd9Sstevel@tonic-gate 	states->ps_state_next = next_state;
3097c478bd9Sstevel@tonic-gate 	states->ps_err = err;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	graph_protocol_send_event(instance_name, GRAPH_UPDATE_STATE_CHANGE,
3127c478bd9Sstevel@tonic-gate 	    states);
3137c478bd9Sstevel@tonic-gate 
314*73b709eaSrm88369 	sz = restarter_state_to_string(state, str_state, sizeof (str_state));
315*73b709eaSrm88369 	assert(sz < sizeof (str_state));
316*73b709eaSrm88369 	sz = restarter_state_to_string(next_state, str_next_state,
317*73b709eaSrm88369 	    sizeof (str_next_state));
318*73b709eaSrm88369 	assert(sz < sizeof (str_next_state));
319*73b709eaSrm88369 	log_framework(LOG_DEBUG, "%s: state updates for %s (%s, %s)\n", fmri,
320*73b709eaSrm88369 	    instance_name, str_state, str_next_state);
3217c478bd9Sstevel@tonic-gate 	nvlist_free(attr_list);
3227c478bd9Sstevel@tonic-gate 	return (0);
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate evchan_t *
3267c478bd9Sstevel@tonic-gate restarter_protocol_init_delegate(char *fmri)
3277c478bd9Sstevel@tonic-gate {
3287c478bd9Sstevel@tonic-gate 	char *delegate_channel_name, *master_channel_name, *sid;
3297c478bd9Sstevel@tonic-gate 	evchan_t *delegate_channel, *master_channel;
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	/* master restarter -- nothing to do */
3327c478bd9Sstevel@tonic-gate 	if (strcmp(fmri, SCF_SERVICE_STARTD) == 0)
3337c478bd9Sstevel@tonic-gate 		return (NULL);
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "%s: Intializing protocol for delegate\n",
3367c478bd9Sstevel@tonic-gate 	    fmri);
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	if ((delegate_channel_name = _restarter_get_channel_name(fmri,
3397c478bd9Sstevel@tonic-gate 	    RESTARTER_CHANNEL_DELEGATE)) == NULL ||
3407c478bd9Sstevel@tonic-gate 	    (master_channel_name = _restarter_get_channel_name(fmri,
3417c478bd9Sstevel@tonic-gate 	    RESTARTER_CHANNEL_MASTER)) == NULL ||
3427c478bd9Sstevel@tonic-gate 	    (sid = strdup("svc.startd")) == NULL)
3437c478bd9Sstevel@tonic-gate 		uu_die("Allocation failure\n");
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	if (sysevent_evc_bind(delegate_channel_name, &delegate_channel,
3467c478bd9Sstevel@tonic-gate 	    EVCH_CREAT|EVCH_HOLD_PEND) != 0)
3477c478bd9Sstevel@tonic-gate 		uu_die("%s: sysevent_evc_bind failed: %s\n",
3487c478bd9Sstevel@tonic-gate 		    delegate_channel_name, strerror(errno));
3497c478bd9Sstevel@tonic-gate 	if (sysevent_evc_bind(master_channel_name, &master_channel,
3507c478bd9Sstevel@tonic-gate 	    EVCH_CREAT|EVCH_HOLD_PEND) != 0)
3517c478bd9Sstevel@tonic-gate 		uu_die("%s: sysevent_evc_bind failed: %s\n",
3527c478bd9Sstevel@tonic-gate 		    master_channel_name, strerror(errno));
3537c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG,
3547c478bd9Sstevel@tonic-gate 	    "%s: Bound to channel %s (delegate), %s (master)\n", fmri,
3557c478bd9Sstevel@tonic-gate 	    delegate_channel_name, master_channel_name);
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	if (sysevent_evc_subscribe(master_channel, sid, EC_ALL,
3587c478bd9Sstevel@tonic-gate 	    state_cb, fmri, EVCH_SUB_KEEP) != 0)
3597c478bd9Sstevel@tonic-gate 		uu_die("%s: Failed to subscribe to channel %s with "
3607c478bd9Sstevel@tonic-gate 		    "subscriber id %s: %s\n", fmri,
3617c478bd9Sstevel@tonic-gate 		    master_channel_name, sid, strerror(errno));
3627c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG,
3637c478bd9Sstevel@tonic-gate 	    "%s: Subscribed to channel %s with subscriber id %s\n", fmri,
3647c478bd9Sstevel@tonic-gate 	    master_channel_name, "svc.startd");
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	free(delegate_channel_name);
3677c478bd9Sstevel@tonic-gate 	free(master_channel_name);
3687c478bd9Sstevel@tonic-gate 	free(sid);
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	return (delegate_channel);
3717c478bd9Sstevel@tonic-gate }
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate void
3747c478bd9Sstevel@tonic-gate restarter_protocol_send_event(const char *inst, evchan_t *chan,
3757c478bd9Sstevel@tonic-gate     restarter_event_type_t event)
3767c478bd9Sstevel@tonic-gate {
3777c478bd9Sstevel@tonic-gate 	nvlist_t *attr;
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	/*
3807c478bd9Sstevel@tonic-gate 	 * If the service is managed by the master restarter,
3817c478bd9Sstevel@tonic-gate 	 * queue the event locally.
3827c478bd9Sstevel@tonic-gate 	 */
3837c478bd9Sstevel@tonic-gate 	if (chan == NULL) {
3847c478bd9Sstevel@tonic-gate 		restarter_event_enqueue(inst, event);
3857c478bd9Sstevel@tonic-gate 		MUTEX_LOCK(&ru->restarter_update_lock);
3867c478bd9Sstevel@tonic-gate 		ru->restarter_update_wakeup = 1;
3877c478bd9Sstevel@tonic-gate 		(void) pthread_cond_broadcast(&ru->restarter_update_cv);
3887c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&ru->restarter_update_lock);
3897c478bd9Sstevel@tonic-gate 		return;
3907c478bd9Sstevel@tonic-gate 	}
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	/*
3937c478bd9Sstevel@tonic-gate 	 * Otherwise, send the event to the delegate.
3947c478bd9Sstevel@tonic-gate 	 */
3957c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Sending %s to channel 0x%p for %s.\n",
3967c478bd9Sstevel@tonic-gate 	    event_names[event], chan, inst);
3977c478bd9Sstevel@tonic-gate 	if (nvlist_alloc(&attr, NV_UNIQUE_NAME, 0) != 0 ||
3987c478bd9Sstevel@tonic-gate 	    nvlist_add_uint32(attr, RESTARTER_NAME_TYPE, event) != 0 ||
3997c478bd9Sstevel@tonic-gate 	    nvlist_add_string(attr, RESTARTER_NAME_INSTANCE, (char *)inst) != 0)
4007c478bd9Sstevel@tonic-gate 		uu_die("Allocation failure\n");
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	if (sysevent_evc_publish(chan, "protocol", "restarter", "com.sun",
4037c478bd9Sstevel@tonic-gate 	    "svc.startd", attr, EVCH_NOSLEEP) != 0) {
4047c478bd9Sstevel@tonic-gate 		if (errno == EAGAIN)
4057c478bd9Sstevel@tonic-gate 			uu_die("%s: queue is full\n", inst);
4067c478bd9Sstevel@tonic-gate 		uu_die("%s: can't publish event: %s\n", inst, strerror(errno));
4077c478bd9Sstevel@tonic-gate 	}
4087c478bd9Sstevel@tonic-gate 	nvlist_free(attr);
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	if (event != RESTARTER_EVENT_TYPE_ADD_INSTANCE) {
4117c478bd9Sstevel@tonic-gate 		/*
4127c478bd9Sstevel@tonic-gate 		 * Not relevant for graph loading.
4137c478bd9Sstevel@tonic-gate 		 */
4147c478bd9Sstevel@tonic-gate 		return;
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	/*
4187c478bd9Sstevel@tonic-gate 	 * For the purposes of loading state after interruption, this is
4197c478bd9Sstevel@tonic-gate 	 * sufficient, as svc.startd(1M) won't receive events on the contracts
4207c478bd9Sstevel@tonic-gate 	 * associated with each delegate.
4217c478bd9Sstevel@tonic-gate 	 */
4227c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&st->st_load_lock);
4237c478bd9Sstevel@tonic-gate 	if (--st->st_load_instances == 0)
4247c478bd9Sstevel@tonic-gate 		(void) pthread_cond_broadcast(&st->st_load_cv);
4257c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&st->st_load_lock);
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate }
428