xref: /illumos-gate/usr/src/cmd/svc/startd/protocol.c (revision bbf21555)
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
573b709eaSrm  * Common Development and Distribution License (the "License").
673b709eaSrm  * 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 /*
22f6e214c7SGavin Maltby  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
267c478bd9Sstevel@tonic-gate  * protocol.c - protocols between graph engine and restarters
277c478bd9Sstevel@tonic-gate  *
287c478bd9Sstevel@tonic-gate  *   The graph engine uses restarter_protocol_send_event() to send a
297c478bd9Sstevel@tonic-gate  *   restarter_event_type_t to the restarter.  For delegated restarters,
307c478bd9Sstevel@tonic-gate  *   this is published on the GPEC queue for the restarter, which can
317c478bd9Sstevel@tonic-gate  *   then be consumed by the librestart interfaces.  For services managed
327c478bd9Sstevel@tonic-gate  *   by svc.startd, the event is stored on the local restarter_queue list,
337c478bd9Sstevel@tonic-gate  *   where it can be dequeued by the restarter.
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  *   The svc.startd restarter uses graph_protocol_send_event() to send
367c478bd9Sstevel@tonic-gate  *   a graph_event_type_t to the graph engine when an instance's states are
377c478bd9Sstevel@tonic-gate  *   updated.
387c478bd9Sstevel@tonic-gate  *
397c478bd9Sstevel@tonic-gate  *   The graph engine uses restarter_protocol_init_delegate() to
407c478bd9Sstevel@tonic-gate  *   register its interest in a particular delegated restarter's instance
417c478bd9Sstevel@tonic-gate  *   state events.  The state_cb() registered on the event channel then
427c478bd9Sstevel@tonic-gate  *   invokes graph_protocol_send_event() to communicate the update to
437c478bd9Sstevel@tonic-gate  *   the graph engine.
447c478bd9Sstevel@tonic-gate  */
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #include <assert.h>
477c478bd9Sstevel@tonic-gate #include <libintl.h>
487c478bd9Sstevel@tonic-gate #include <libsysevent.h>
497c478bd9Sstevel@tonic-gate #include <pthread.h>
507c478bd9Sstevel@tonic-gate #include <stdarg.h>
517c478bd9Sstevel@tonic-gate #include <stdio.h>
527c478bd9Sstevel@tonic-gate #include <strings.h>
537c478bd9Sstevel@tonic-gate #include <sys/time.h>
547c478bd9Sstevel@tonic-gate #include <errno.h>
557c478bd9Sstevel@tonic-gate #include <libuutil.h>
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #include <librestart.h>
587c478bd9Sstevel@tonic-gate #include <librestart_priv.h>
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #include "protocol.h"
617c478bd9Sstevel@tonic-gate #include "startd.h"
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /* Local event queue structures. */
647c478bd9Sstevel@tonic-gate typedef struct graph_protocol_event_queue {
657c478bd9Sstevel@tonic-gate 	uu_list_t		*gpeq_event_list;
667c478bd9Sstevel@tonic-gate 	pthread_mutex_t		gpeq_lock;
677c478bd9Sstevel@tonic-gate } graph_protocol_event_queue_t;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate typedef struct restarter_protocol_event_queue {
707c478bd9Sstevel@tonic-gate 	uu_list_t		*rpeq_event_list;
717c478bd9Sstevel@tonic-gate 	pthread_mutex_t		rpeq_lock;
727c478bd9Sstevel@tonic-gate } restarter_protocol_event_queue_t;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate static uu_list_pool_t *restarter_protocol_event_queue_pool;
757c478bd9Sstevel@tonic-gate static restarter_protocol_event_queue_t *restarter_queue;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate static uu_list_pool_t *graph_protocol_event_queue_pool;
787c478bd9Sstevel@tonic-gate static graph_protocol_event_queue_t *graph_queue;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate void
graph_protocol_init()817c478bd9Sstevel@tonic-gate graph_protocol_init()
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate 	graph_protocol_event_queue_pool = startd_list_pool_create(
847c478bd9Sstevel@tonic-gate 	    "graph_protocol_events", sizeof (graph_protocol_event_t),
857c478bd9Sstevel@tonic-gate 	    offsetof(graph_protocol_event_t, gpe_link), NULL,
867c478bd9Sstevel@tonic-gate 	    UU_LIST_POOL_DEBUG);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	graph_queue = startd_zalloc(sizeof (graph_protocol_event_queue_t));
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&graph_queue->gpeq_lock, &mutex_attrs);
917c478bd9Sstevel@tonic-gate 	graph_queue->gpeq_event_list = startd_list_create(
925f8fc372SToomas Soome 	    graph_protocol_event_queue_pool, graph_queue, 0);
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  * "data" will be freed by the consumer
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate static void
graph_event_enqueue(const char * inst,graph_event_type_t event,protocol_states_t * data)997c478bd9Sstevel@tonic-gate graph_event_enqueue(const char *inst, graph_event_type_t event,
1007c478bd9Sstevel@tonic-gate     protocol_states_t *data)
1017c478bd9Sstevel@tonic-gate {
1027c478bd9Sstevel@tonic-gate 	graph_protocol_event_t *e;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	e = startd_zalloc(sizeof (graph_protocol_event_t));
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	if (inst != NULL) {
1077c478bd9Sstevel@tonic-gate 		int size = strlen(inst) + 1;
1087c478bd9Sstevel@tonic-gate 		e->gpe_inst = startd_alloc(size);
1097c478bd9Sstevel@tonic-gate 		e->gpe_inst_sz = size;
1107c478bd9Sstevel@tonic-gate 		(void) strlcpy(e->gpe_inst, inst, size);
1117c478bd9Sstevel@tonic-gate 	}
1127c478bd9Sstevel@tonic-gate 	e->gpe_type = event;
1137c478bd9Sstevel@tonic-gate 	e->gpe_data = data;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&e->gpe_lock, &mutex_attrs);
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&graph_queue->gpeq_lock);
1187c478bd9Sstevel@tonic-gate 	uu_list_node_init(e, &e->gpe_link, graph_protocol_event_queue_pool);
1197c478bd9Sstevel@tonic-gate 	if (uu_list_insert_before(graph_queue->gpeq_event_list, NULL, e) == -1)
1207c478bd9Sstevel@tonic-gate 		uu_die("failed to enqueue graph event (%s: %s)\n",
1217c478bd9Sstevel@tonic-gate 		    e->gpe_inst, uu_strerror(uu_error()));
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&graph_queue->gpeq_lock);
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate void
graph_event_release(graph_protocol_event_t * e)1277c478bd9Sstevel@tonic-gate graph_event_release(graph_protocol_event_t *e)
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate 	uu_list_node_fini(e, &e->gpe_link, graph_protocol_event_queue_pool);
1307c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_destroy(&e->gpe_lock);
1317c478bd9Sstevel@tonic-gate 	if (e->gpe_inst != NULL)
1327c478bd9Sstevel@tonic-gate 		startd_free(e->gpe_inst, e->gpe_inst_sz);
1337c478bd9Sstevel@tonic-gate 	startd_free(e, sizeof (graph_protocol_event_t));
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /*
1377c478bd9Sstevel@tonic-gate  * graph_protocol_event_t *graph_event_dequeue()
1387c478bd9Sstevel@tonic-gate  *   The caller must hold gu_lock, and is expected to be a single thread.
1397c478bd9Sstevel@tonic-gate  *   It is allowed to utilize graph_event_requeue() and abort processing
1407c478bd9Sstevel@tonic-gate  *   on the event. If graph_event_requeue() is not called, the caller is
1417c478bd9Sstevel@tonic-gate  *   expected to call graph_event_release() when finished.
1427c478bd9Sstevel@tonic-gate  */
1437c478bd9Sstevel@tonic-gate graph_protocol_event_t *
graph_event_dequeue()1447c478bd9Sstevel@tonic-gate graph_event_dequeue()
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate 	graph_protocol_event_t *e;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&graph_queue->gpeq_lock);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	e = uu_list_first(graph_queue->gpeq_event_list);
1517c478bd9Sstevel@tonic-gate 	if (e == NULL) {
1527c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&graph_queue->gpeq_lock);
1537c478bd9Sstevel@tonic-gate 		return (NULL);
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	if (uu_list_next(graph_queue->gpeq_event_list, e) != NULL)
1577c478bd9Sstevel@tonic-gate 		gu->gu_wakeup = 1;
1587c478bd9Sstevel@tonic-gate 	uu_list_remove(graph_queue->gpeq_event_list, e);
1597c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&graph_queue->gpeq_lock);
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	return (e);
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate /*
1657c478bd9Sstevel@tonic-gate  * void graph_event_requeue()
1667c478bd9Sstevel@tonic-gate  *   Requeue the event back at the head of the queue.
1677c478bd9Sstevel@tonic-gate  */
1687c478bd9Sstevel@tonic-gate void
graph_event_requeue(graph_protocol_event_t * e)1697c478bd9Sstevel@tonic-gate graph_event_requeue(graph_protocol_event_t *e)
1707c478bd9Sstevel@tonic-gate {
1717c478bd9Sstevel@tonic-gate 	assert(e != NULL);
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Requeing event\n");
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&graph_queue->gpeq_lock);
1767c478bd9Sstevel@tonic-gate 	if (uu_list_insert_after(graph_queue->gpeq_event_list, NULL, e) == -1)
1777c478bd9Sstevel@tonic-gate 		uu_die("failed to requeue graph event (%s: %s)\n",
1787c478bd9Sstevel@tonic-gate 		    e->gpe_inst, uu_strerror(uu_error()));
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&graph_queue->gpeq_lock);
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate void
graph_protocol_send_event(const char * inst,graph_event_type_t event,protocol_states_t * data)1847c478bd9Sstevel@tonic-gate graph_protocol_send_event(const char *inst, graph_event_type_t event,
1857c478bd9Sstevel@tonic-gate     protocol_states_t *data)
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate 	graph_event_enqueue(inst, event, data);
1887c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&gu->gu_lock);
1897c478bd9Sstevel@tonic-gate 	gu->gu_wakeup = 1;
1907c478bd9Sstevel@tonic-gate 	(void) pthread_cond_broadcast(&gu->gu_cv);
1917c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&gu->gu_lock);
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate void
restarter_protocol_init()1957c478bd9Sstevel@tonic-gate restarter_protocol_init()
1967c478bd9Sstevel@tonic-gate {
1977c478bd9Sstevel@tonic-gate 	restarter_protocol_event_queue_pool = startd_list_pool_create(
1987c478bd9Sstevel@tonic-gate 	    "restarter_protocol_events", sizeof (restarter_protocol_event_t),
1997c478bd9Sstevel@tonic-gate 	    offsetof(restarter_protocol_event_t, rpe_link), NULL,
2007c478bd9Sstevel@tonic-gate 	    UU_LIST_POOL_DEBUG);
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	restarter_queue = startd_zalloc(
2037c478bd9Sstevel@tonic-gate 	    sizeof (restarter_protocol_event_queue_t));
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&restarter_queue->rpeq_lock, &mutex_attrs);
2067c478bd9Sstevel@tonic-gate 	restarter_queue->rpeq_event_list = startd_list_create(
2075f8fc372SToomas Soome 	    restarter_protocol_event_queue_pool, restarter_queue, 0);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Initialized restarter protocol\n");
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate /*
2137c478bd9Sstevel@tonic-gate  * void restarter_event_enqueue()
2147c478bd9Sstevel@tonic-gate  *   Enqueue a restarter event.
2157c478bd9Sstevel@tonic-gate  */
2167c478bd9Sstevel@tonic-gate static void
restarter_event_enqueue(const char * inst,restarter_event_type_t event,int32_t reason)217f6e214c7SGavin Maltby restarter_event_enqueue(const char *inst, restarter_event_type_t event,
218f6e214c7SGavin Maltby     int32_t reason)
2197c478bd9Sstevel@tonic-gate {
2207c478bd9Sstevel@tonic-gate 	restarter_protocol_event_t *e;
2217c478bd9Sstevel@tonic-gate 	int r;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	/* Allocate and populate the event structure. */
2247c478bd9Sstevel@tonic-gate 	e = startd_zalloc(sizeof (restarter_protocol_event_t));
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	e->rpe_inst = startd_alloc(strlen(inst) + 1);
2277c478bd9Sstevel@tonic-gate 	(void) strlcpy(e->rpe_inst, inst, strlen(inst)+1);
2287c478bd9Sstevel@tonic-gate 	e->rpe_type = event;
229f6e214c7SGavin Maltby 	e->rpe_reason = reason;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&restarter_queue->rpeq_lock);
2327c478bd9Sstevel@tonic-gate 	uu_list_node_init(e, &e->rpe_link, restarter_protocol_event_queue_pool);
2337c478bd9Sstevel@tonic-gate 	r = uu_list_insert_before(restarter_queue->rpeq_event_list, NULL, e);
2347c478bd9Sstevel@tonic-gate 	assert(r == 0);
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&restarter_queue->rpeq_lock);
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate void
restarter_event_release(restarter_protocol_event_t * e)2417c478bd9Sstevel@tonic-gate restarter_event_release(restarter_protocol_event_t *e)
2427c478bd9Sstevel@tonic-gate {
2437c478bd9Sstevel@tonic-gate 	uu_list_node_fini(e, &e->rpe_link, restarter_protocol_event_queue_pool);
2447c478bd9Sstevel@tonic-gate 	startd_free(e->rpe_inst, strlen(e->rpe_inst) + 1);
2457c478bd9Sstevel@tonic-gate 	startd_free(e, sizeof (restarter_protocol_event_t));
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate /*
2497c478bd9Sstevel@tonic-gate  * restarter_protocol_event_t *restarter_event_dequeue()
2507c478bd9Sstevel@tonic-gate  *   Dequeue a restarter protocol event. The caller is expected to be
2517c478bd9Sstevel@tonic-gate  *   a single thread. It is allowed to utilize restarter_event_requeue()
2527c478bd9Sstevel@tonic-gate  *   and abort processing on the event. The caller is expected to call
2537c478bd9Sstevel@tonic-gate  *   restarter_event_release() when finished.
2547c478bd9Sstevel@tonic-gate  */
2557c478bd9Sstevel@tonic-gate restarter_protocol_event_t *
restarter_event_dequeue()2567c478bd9Sstevel@tonic-gate restarter_event_dequeue()
2577c478bd9Sstevel@tonic-gate {
2587c478bd9Sstevel@tonic-gate 	restarter_protocol_event_t *e = NULL;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&restarter_queue->rpeq_lock);
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	e = uu_list_first(restarter_queue->rpeq_event_list);
2637c478bd9Sstevel@tonic-gate 	if (e == NULL) {
2647c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&restarter_queue->rpeq_lock);
2657c478bd9Sstevel@tonic-gate 		return (NULL);
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	if (uu_list_next(restarter_queue->rpeq_event_list, e) != NULL)
2697c478bd9Sstevel@tonic-gate 		ru->restarter_update_wakeup = 1;
2707c478bd9Sstevel@tonic-gate 	uu_list_remove(restarter_queue->rpeq_event_list, e);
2717c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&restarter_queue->rpeq_lock);
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	return (e);
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate static int
state_cb(sysevent_t * syse,void * cookie)2777c478bd9Sstevel@tonic-gate state_cb(sysevent_t *syse, void *cookie)
2787c478bd9Sstevel@tonic-gate {
2797c478bd9Sstevel@tonic-gate 	char *fmri = (char *)cookie;
2807c478bd9Sstevel@tonic-gate 	char *instance_name;
281f6e214c7SGavin Maltby 	int32_t reason;
2827c478bd9Sstevel@tonic-gate 	nvlist_t *attr_list = NULL;
2837c478bd9Sstevel@tonic-gate 	int state, next_state;
28473b709eaSrm 	char str_state[MAX_SCF_STATE_STRING_SZ];
28573b709eaSrm 	char str_next_state[MAX_SCF_STATE_STRING_SZ];
2867c478bd9Sstevel@tonic-gate 	protocol_states_t *states;
2877c478bd9Sstevel@tonic-gate 	int err;
28873b709eaSrm 	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,
303f6e214c7SGavin Maltby 	    &instance_name) != 0) ||
304f6e214c7SGavin Maltby 	    (nvlist_lookup_int32(attr_list, RESTARTER_NAME_REASON, &reason) !=
305f6e214c7SGavin Maltby 	    0))
3067c478bd9Sstevel@tonic-gate 		uu_die("%s: can't decode nvlist\n", fmri);
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	states = startd_alloc(sizeof (protocol_states_t));
3097c478bd9Sstevel@tonic-gate 	states->ps_state = state;
3107c478bd9Sstevel@tonic-gate 	states->ps_state_next = next_state;
3117c478bd9Sstevel@tonic-gate 	states->ps_err = err;
312f6e214c7SGavin Maltby 	states->ps_reason = reason;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	graph_protocol_send_event(instance_name, GRAPH_UPDATE_STATE_CHANGE,
3157c478bd9Sstevel@tonic-gate 	    states);
3167c478bd9Sstevel@tonic-gate 
31773b709eaSrm 	sz = restarter_state_to_string(state, str_state, sizeof (str_state));
31873b709eaSrm 	assert(sz < sizeof (str_state));
31973b709eaSrm 	sz = restarter_state_to_string(next_state, str_next_state,
32073b709eaSrm 	    sizeof (str_next_state));
32173b709eaSrm 	assert(sz < sizeof (str_next_state));
32273b709eaSrm 	log_framework(LOG_DEBUG, "%s: state updates for %s (%s, %s)\n", fmri,
32373b709eaSrm 	    instance_name, str_state, str_next_state);
3247c478bd9Sstevel@tonic-gate 	nvlist_free(attr_list);
3257c478bd9Sstevel@tonic-gate 	return (0);
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate evchan_t *
restarter_protocol_init_delegate(char * fmri)3297c478bd9Sstevel@tonic-gate restarter_protocol_init_delegate(char *fmri)
3307c478bd9Sstevel@tonic-gate {
3317c478bd9Sstevel@tonic-gate 	char *delegate_channel_name, *master_channel_name, *sid;
3327c478bd9Sstevel@tonic-gate 	evchan_t *delegate_channel, *master_channel;
33313d8aaa1SSean Wilcox 	int r = 0;
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	/* master restarter -- nothing to do */
33613d8aaa1SSean Wilcox 	if (strcmp(fmri, SCF_SERVICE_STARTD) == 0) {
33713d8aaa1SSean Wilcox 		uu_warn("Attempt to initialize restarter protocol delegate "
33813d8aaa1SSean Wilcox 		    "with %s\n", fmri);
3397c478bd9Sstevel@tonic-gate 		return (NULL);
34013d8aaa1SSean Wilcox 	}
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "%s: Intializing protocol for delegate\n",
3437c478bd9Sstevel@tonic-gate 	    fmri);
3447c478bd9Sstevel@tonic-gate 
34513d8aaa1SSean Wilcox 	delegate_channel_name = master_channel_name = NULL;
3467c478bd9Sstevel@tonic-gate 	if ((delegate_channel_name = _restarter_get_channel_name(fmri,
3477c478bd9Sstevel@tonic-gate 	    RESTARTER_CHANNEL_DELEGATE)) == NULL ||
3487c478bd9Sstevel@tonic-gate 	    (master_channel_name = _restarter_get_channel_name(fmri,
3497c478bd9Sstevel@tonic-gate 	    RESTARTER_CHANNEL_MASTER)) == NULL ||
35013d8aaa1SSean Wilcox 	    (sid = strdup("svc.startd")) == NULL) {
35113d8aaa1SSean Wilcox 		if (delegate_channel_name) {
35213d8aaa1SSean Wilcox 			free(delegate_channel_name);
35313d8aaa1SSean Wilcox 		}
35413d8aaa1SSean Wilcox 		if (master_channel_name) {
35513d8aaa1SSean Wilcox 			free(master_channel_name);
35613d8aaa1SSean Wilcox 		}
35713d8aaa1SSean Wilcox 		uu_warn("Allocation of channel name failed");
3587c478bd9Sstevel@tonic-gate 
35913d8aaa1SSean Wilcox 		return (NULL);
36013d8aaa1SSean Wilcox 	}
36113d8aaa1SSean Wilcox 
36213d8aaa1SSean Wilcox 	if ((r = sysevent_evc_bind(delegate_channel_name, &delegate_channel,
36313d8aaa1SSean Wilcox 	    EVCH_CREAT|EVCH_HOLD_PEND)) != 0) {
36413d8aaa1SSean Wilcox 		uu_warn("%s: sysevent_evc_bind failed: %s\n",
3657c478bd9Sstevel@tonic-gate 		    delegate_channel_name, strerror(errno));
36613d8aaa1SSean Wilcox 		goto out;
36713d8aaa1SSean Wilcox 	}
36813d8aaa1SSean Wilcox 
36913d8aaa1SSean Wilcox 	if ((r = sysevent_evc_bind(master_channel_name, &master_channel,
37013d8aaa1SSean Wilcox 	    EVCH_CREAT|EVCH_HOLD_PEND)) != 0) {
37113d8aaa1SSean Wilcox 		uu_warn("%s: sysevent_evc_bind failed: %s\n",
3727c478bd9Sstevel@tonic-gate 		    master_channel_name, strerror(errno));
37313d8aaa1SSean Wilcox 		goto out;
37413d8aaa1SSean Wilcox 	}
37513d8aaa1SSean Wilcox 
3767c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG,
3777c478bd9Sstevel@tonic-gate 	    "%s: Bound to channel %s (delegate), %s (master)\n", fmri,
3787c478bd9Sstevel@tonic-gate 	    delegate_channel_name, master_channel_name);
3797c478bd9Sstevel@tonic-gate 
38013d8aaa1SSean Wilcox 	if ((r = sysevent_evc_subscribe(master_channel, sid, EC_ALL,
38113d8aaa1SSean Wilcox 	    state_cb, fmri, EVCH_SUB_KEEP)) != 0) {
38213d8aaa1SSean Wilcox 		/*
38313d8aaa1SSean Wilcox 		 * The following errors can be returned in this
38413d8aaa1SSean Wilcox 		 * case :
3855f8fc372SToomas Soome 		 *	EINVAL : inappropriate flags or dump flag
3865f8fc372SToomas Soome 		 *		and the dump failed.
3875f8fc372SToomas Soome 		 *	EEXIST : svc.startd already has a channel
3885f8fc372SToomas Soome 		 *		named as the master channel name
3895f8fc372SToomas Soome 		 *	ENOMEM : too many subscribers to the channel
39013d8aaa1SSean Wilcox 		 */
39113d8aaa1SSean Wilcox 		uu_warn("Failed to subscribe to restarter %s, channel %s with "
39213d8aaa1SSean Wilcox 		    "subscriber id %s : \n", fmri, master_channel_name, sid);
39313d8aaa1SSean Wilcox 		switch (r) {
39413d8aaa1SSean Wilcox 		case EEXIST:
39513d8aaa1SSean Wilcox 			uu_warn("Channel name already exists\n");
39613d8aaa1SSean Wilcox 			break;
39713d8aaa1SSean Wilcox 		case ENOMEM:
39813d8aaa1SSean Wilcox 			uu_warn("Too many subscribers for the channel\n");
39913d8aaa1SSean Wilcox 			break;
40013d8aaa1SSean Wilcox 		default:
40113d8aaa1SSean Wilcox 			uu_warn("%s\n", strerror(errno));
40213d8aaa1SSean Wilcox 		}
40313d8aaa1SSean Wilcox 	} else {
40413d8aaa1SSean Wilcox 		log_framework(LOG_DEBUG,
40513d8aaa1SSean Wilcox 		    "%s: Subscribed to channel %s with subscriber id %s\n",
40613d8aaa1SSean Wilcox 		    fmri, master_channel_name, "svc.startd");
40713d8aaa1SSean Wilcox 	}
40813d8aaa1SSean Wilcox 
4097c478bd9Sstevel@tonic-gate 
41013d8aaa1SSean Wilcox out:
4117c478bd9Sstevel@tonic-gate 	free(delegate_channel_name);
4127c478bd9Sstevel@tonic-gate 	free(master_channel_name);
4137c478bd9Sstevel@tonic-gate 	free(sid);
4147c478bd9Sstevel@tonic-gate 
41513d8aaa1SSean Wilcox 	if (r == 0)
41613d8aaa1SSean Wilcox 		return (delegate_channel);
41713d8aaa1SSean Wilcox 
41813d8aaa1SSean Wilcox 	return (NULL);
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate void
restarter_protocol_send_event(const char * inst,evchan_t * chan,restarter_event_type_t event,int32_t reason)4227c478bd9Sstevel@tonic-gate restarter_protocol_send_event(const char *inst, evchan_t *chan,
423f6e214c7SGavin Maltby     restarter_event_type_t event, int32_t reason)
4247c478bd9Sstevel@tonic-gate {
4257c478bd9Sstevel@tonic-gate 	nvlist_t *attr;
4262c65c8b0Srm 	int ret;
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	/*
4297c478bd9Sstevel@tonic-gate 	 * If the service is managed by the master restarter,
4307c478bd9Sstevel@tonic-gate 	 * queue the event locally.
4317c478bd9Sstevel@tonic-gate 	 */
4327c478bd9Sstevel@tonic-gate 	if (chan == NULL) {
433f6e214c7SGavin Maltby 		restarter_event_enqueue(inst, event, reason);
4347c478bd9Sstevel@tonic-gate 		MUTEX_LOCK(&ru->restarter_update_lock);
4357c478bd9Sstevel@tonic-gate 		ru->restarter_update_wakeup = 1;
4367c478bd9Sstevel@tonic-gate 		(void) pthread_cond_broadcast(&ru->restarter_update_cv);
4377c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&ru->restarter_update_lock);
4387c478bd9Sstevel@tonic-gate 		return;
4397c478bd9Sstevel@tonic-gate 	}
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	/*
4427c478bd9Sstevel@tonic-gate 	 * Otherwise, send the event to the delegate.
4437c478bd9Sstevel@tonic-gate 	 */
4447c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Sending %s to channel 0x%p for %s.\n",
4457c478bd9Sstevel@tonic-gate 	    event_names[event], chan, inst);
4467c478bd9Sstevel@tonic-gate 	if (nvlist_alloc(&attr, NV_UNIQUE_NAME, 0) != 0 ||
4477c478bd9Sstevel@tonic-gate 	    nvlist_add_uint32(attr, RESTARTER_NAME_TYPE, event) != 0 ||
448f6e214c7SGavin Maltby 	    nvlist_add_string(attr, RESTARTER_NAME_INSTANCE, (char *)inst) !=
449f6e214c7SGavin Maltby 	    0 || nvlist_add_uint32(attr, RESTARTER_NAME_REASON,
450f6e214c7SGavin Maltby 	    reason) != 0)
4517c478bd9Sstevel@tonic-gate 		uu_die("Allocation failure\n");
4527c478bd9Sstevel@tonic-gate 
4532c65c8b0Srm 	if ((ret = restarter_event_publish_retry(chan, "protocol", "restarter",
4542c65c8b0Srm 	    "com.sun", "svc.startd", attr, EVCH_NOSLEEP)) != 0) {
4552c65c8b0Srm 
4562c65c8b0Srm 		switch (ret) {
4572c65c8b0Srm 		case ENOSPC:
4582c65c8b0Srm 			log_framework(LOG_DEBUG, "Dropping %s event for %s. "
4592c65c8b0Srm 			    "Delegate may not be running.\n",
4602c65c8b0Srm 			    event_names[event], inst);
4612c65c8b0Srm 			break;
4622c65c8b0Srm 		default:
4632c65c8b0Srm 			uu_die("%s: can't publish event: %s\n", inst,
4642c65c8b0Srm 			    strerror(errno));
4652c65c8b0Srm 		}
4667c478bd9Sstevel@tonic-gate 	}
4672c65c8b0Srm 
4687c478bd9Sstevel@tonic-gate 	nvlist_free(attr);
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	if (event != RESTARTER_EVENT_TYPE_ADD_INSTANCE) {
4717c478bd9Sstevel@tonic-gate 		/*
4727c478bd9Sstevel@tonic-gate 		 * Not relevant for graph loading.
4737c478bd9Sstevel@tonic-gate 		 */
4747c478bd9Sstevel@tonic-gate 		return;
4757c478bd9Sstevel@tonic-gate 	}
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	/*
4787c478bd9Sstevel@tonic-gate 	 * For the purposes of loading state after interruption, this is
479*bbf21555SRichard Lowe 	 * sufficient, as svc.startd(8) won't receive events on the contracts
4807c478bd9Sstevel@tonic-gate 	 * associated with each delegate.
4817c478bd9Sstevel@tonic-gate 	 */
4827c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&st->st_load_lock);
4837c478bd9Sstevel@tonic-gate 	if (--st->st_load_instances == 0)
4847c478bd9Sstevel@tonic-gate 		(void) pthread_cond_broadcast(&st->st_load_cv);
4857c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&st->st_load_lock);
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate }
488