10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
53639Srm88369 * Common Development and Distribution License (the "License").
63639Srm88369 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*12967Sgavin.maltby@oracle.com * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
250Sstevel@tonic-gate /*
260Sstevel@tonic-gate * protocol.c - protocols between graph engine and restarters
270Sstevel@tonic-gate *
280Sstevel@tonic-gate * The graph engine uses restarter_protocol_send_event() to send a
290Sstevel@tonic-gate * restarter_event_type_t to the restarter. For delegated restarters,
300Sstevel@tonic-gate * this is published on the GPEC queue for the restarter, which can
310Sstevel@tonic-gate * then be consumed by the librestart interfaces. For services managed
320Sstevel@tonic-gate * by svc.startd, the event is stored on the local restarter_queue list,
330Sstevel@tonic-gate * where it can be dequeued by the restarter.
340Sstevel@tonic-gate *
350Sstevel@tonic-gate * The svc.startd restarter uses graph_protocol_send_event() to send
360Sstevel@tonic-gate * a graph_event_type_t to the graph engine when an instance's states are
370Sstevel@tonic-gate * updated.
380Sstevel@tonic-gate *
390Sstevel@tonic-gate * The graph engine uses restarter_protocol_init_delegate() to
400Sstevel@tonic-gate * register its interest in a particular delegated restarter's instance
410Sstevel@tonic-gate * state events. The state_cb() registered on the event channel then
420Sstevel@tonic-gate * invokes graph_protocol_send_event() to communicate the update to
430Sstevel@tonic-gate * the graph engine.
440Sstevel@tonic-gate */
450Sstevel@tonic-gate
460Sstevel@tonic-gate #include <assert.h>
470Sstevel@tonic-gate #include <libintl.h>
480Sstevel@tonic-gate #include <libsysevent.h>
490Sstevel@tonic-gate #include <pthread.h>
500Sstevel@tonic-gate #include <stdarg.h>
510Sstevel@tonic-gate #include <stdio.h>
520Sstevel@tonic-gate #include <strings.h>
530Sstevel@tonic-gate #include <sys/time.h>
540Sstevel@tonic-gate #include <errno.h>
550Sstevel@tonic-gate #include <libuutil.h>
560Sstevel@tonic-gate
570Sstevel@tonic-gate #include <librestart.h>
580Sstevel@tonic-gate #include <librestart_priv.h>
590Sstevel@tonic-gate
600Sstevel@tonic-gate #include "protocol.h"
610Sstevel@tonic-gate #include "startd.h"
620Sstevel@tonic-gate
630Sstevel@tonic-gate /* Local event queue structures. */
640Sstevel@tonic-gate typedef struct graph_protocol_event_queue {
650Sstevel@tonic-gate uu_list_t *gpeq_event_list;
660Sstevel@tonic-gate pthread_mutex_t gpeq_lock;
670Sstevel@tonic-gate } graph_protocol_event_queue_t;
680Sstevel@tonic-gate
690Sstevel@tonic-gate typedef struct restarter_protocol_event_queue {
700Sstevel@tonic-gate uu_list_t *rpeq_event_list;
710Sstevel@tonic-gate pthread_mutex_t rpeq_lock;
720Sstevel@tonic-gate } restarter_protocol_event_queue_t;
730Sstevel@tonic-gate
740Sstevel@tonic-gate static uu_list_pool_t *restarter_protocol_event_queue_pool;
750Sstevel@tonic-gate static restarter_protocol_event_queue_t *restarter_queue;
760Sstevel@tonic-gate
770Sstevel@tonic-gate static uu_list_pool_t *graph_protocol_event_queue_pool;
780Sstevel@tonic-gate static graph_protocol_event_queue_t *graph_queue;
790Sstevel@tonic-gate
800Sstevel@tonic-gate void
graph_protocol_init()810Sstevel@tonic-gate graph_protocol_init()
820Sstevel@tonic-gate {
830Sstevel@tonic-gate graph_protocol_event_queue_pool = startd_list_pool_create(
840Sstevel@tonic-gate "graph_protocol_events", sizeof (graph_protocol_event_t),
850Sstevel@tonic-gate offsetof(graph_protocol_event_t, gpe_link), NULL,
860Sstevel@tonic-gate UU_LIST_POOL_DEBUG);
870Sstevel@tonic-gate
880Sstevel@tonic-gate graph_queue = startd_zalloc(sizeof (graph_protocol_event_queue_t));
890Sstevel@tonic-gate
900Sstevel@tonic-gate (void) pthread_mutex_init(&graph_queue->gpeq_lock, &mutex_attrs);
910Sstevel@tonic-gate graph_queue->gpeq_event_list = startd_list_create(
920Sstevel@tonic-gate graph_protocol_event_queue_pool, graph_queue, NULL);
930Sstevel@tonic-gate }
940Sstevel@tonic-gate
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate * "data" will be freed by the consumer
970Sstevel@tonic-gate */
980Sstevel@tonic-gate static void
graph_event_enqueue(const char * inst,graph_event_type_t event,protocol_states_t * data)990Sstevel@tonic-gate graph_event_enqueue(const char *inst, graph_event_type_t event,
1000Sstevel@tonic-gate protocol_states_t *data)
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate graph_protocol_event_t *e;
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate e = startd_zalloc(sizeof (graph_protocol_event_t));
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate if (inst != NULL) {
1070Sstevel@tonic-gate int size = strlen(inst) + 1;
1080Sstevel@tonic-gate e->gpe_inst = startd_alloc(size);
1090Sstevel@tonic-gate e->gpe_inst_sz = size;
1100Sstevel@tonic-gate (void) strlcpy(e->gpe_inst, inst, size);
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate e->gpe_type = event;
1130Sstevel@tonic-gate e->gpe_data = data;
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate (void) pthread_mutex_init(&e->gpe_lock, &mutex_attrs);
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate MUTEX_LOCK(&graph_queue->gpeq_lock);
1180Sstevel@tonic-gate uu_list_node_init(e, &e->gpe_link, graph_protocol_event_queue_pool);
1190Sstevel@tonic-gate if (uu_list_insert_before(graph_queue->gpeq_event_list, NULL, e) == -1)
1200Sstevel@tonic-gate uu_die("failed to enqueue graph event (%s: %s)\n",
1210Sstevel@tonic-gate e->gpe_inst, uu_strerror(uu_error()));
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate MUTEX_UNLOCK(&graph_queue->gpeq_lock);
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate void
graph_event_release(graph_protocol_event_t * e)1270Sstevel@tonic-gate graph_event_release(graph_protocol_event_t *e)
1280Sstevel@tonic-gate {
1290Sstevel@tonic-gate uu_list_node_fini(e, &e->gpe_link, graph_protocol_event_queue_pool);
1300Sstevel@tonic-gate (void) pthread_mutex_destroy(&e->gpe_lock);
1310Sstevel@tonic-gate if (e->gpe_inst != NULL)
1320Sstevel@tonic-gate startd_free(e->gpe_inst, e->gpe_inst_sz);
1330Sstevel@tonic-gate startd_free(e, sizeof (graph_protocol_event_t));
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate /*
1370Sstevel@tonic-gate * graph_protocol_event_t *graph_event_dequeue()
1380Sstevel@tonic-gate * The caller must hold gu_lock, and is expected to be a single thread.
1390Sstevel@tonic-gate * It is allowed to utilize graph_event_requeue() and abort processing
1400Sstevel@tonic-gate * on the event. If graph_event_requeue() is not called, the caller is
1410Sstevel@tonic-gate * expected to call graph_event_release() when finished.
1420Sstevel@tonic-gate */
1430Sstevel@tonic-gate graph_protocol_event_t *
graph_event_dequeue()1440Sstevel@tonic-gate graph_event_dequeue()
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate graph_protocol_event_t *e;
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate MUTEX_LOCK(&graph_queue->gpeq_lock);
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate e = uu_list_first(graph_queue->gpeq_event_list);
1510Sstevel@tonic-gate if (e == NULL) {
1520Sstevel@tonic-gate MUTEX_UNLOCK(&graph_queue->gpeq_lock);
1530Sstevel@tonic-gate return (NULL);
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate if (uu_list_next(graph_queue->gpeq_event_list, e) != NULL)
1570Sstevel@tonic-gate gu->gu_wakeup = 1;
1580Sstevel@tonic-gate uu_list_remove(graph_queue->gpeq_event_list, e);
1590Sstevel@tonic-gate MUTEX_UNLOCK(&graph_queue->gpeq_lock);
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate return (e);
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate /*
1650Sstevel@tonic-gate * void graph_event_requeue()
1660Sstevel@tonic-gate * Requeue the event back at the head of the queue.
1670Sstevel@tonic-gate */
1680Sstevel@tonic-gate void
graph_event_requeue(graph_protocol_event_t * e)1690Sstevel@tonic-gate graph_event_requeue(graph_protocol_event_t *e)
1700Sstevel@tonic-gate {
1710Sstevel@tonic-gate assert(e != NULL);
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate log_framework(LOG_DEBUG, "Requeing event\n");
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate MUTEX_LOCK(&graph_queue->gpeq_lock);
1760Sstevel@tonic-gate if (uu_list_insert_after(graph_queue->gpeq_event_list, NULL, e) == -1)
1770Sstevel@tonic-gate uu_die("failed to requeue graph event (%s: %s)\n",
1780Sstevel@tonic-gate e->gpe_inst, uu_strerror(uu_error()));
1790Sstevel@tonic-gate
1800Sstevel@tonic-gate MUTEX_UNLOCK(&graph_queue->gpeq_lock);
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate void
graph_protocol_send_event(const char * inst,graph_event_type_t event,protocol_states_t * data)1840Sstevel@tonic-gate graph_protocol_send_event(const char *inst, graph_event_type_t event,
1850Sstevel@tonic-gate protocol_states_t *data)
1860Sstevel@tonic-gate {
1870Sstevel@tonic-gate graph_event_enqueue(inst, event, data);
1880Sstevel@tonic-gate MUTEX_LOCK(&gu->gu_lock);
1890Sstevel@tonic-gate gu->gu_wakeup = 1;
1900Sstevel@tonic-gate (void) pthread_cond_broadcast(&gu->gu_cv);
1910Sstevel@tonic-gate MUTEX_UNLOCK(&gu->gu_lock);
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate void
restarter_protocol_init()1950Sstevel@tonic-gate restarter_protocol_init()
1960Sstevel@tonic-gate {
1970Sstevel@tonic-gate restarter_protocol_event_queue_pool = startd_list_pool_create(
1980Sstevel@tonic-gate "restarter_protocol_events", sizeof (restarter_protocol_event_t),
1990Sstevel@tonic-gate offsetof(restarter_protocol_event_t, rpe_link), NULL,
2000Sstevel@tonic-gate UU_LIST_POOL_DEBUG);
2010Sstevel@tonic-gate
2020Sstevel@tonic-gate restarter_queue = startd_zalloc(
2030Sstevel@tonic-gate sizeof (restarter_protocol_event_queue_t));
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate (void) pthread_mutex_init(&restarter_queue->rpeq_lock, &mutex_attrs);
2060Sstevel@tonic-gate restarter_queue->rpeq_event_list = startd_list_create(
2070Sstevel@tonic-gate restarter_protocol_event_queue_pool, restarter_queue, NULL);
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate log_framework(LOG_DEBUG, "Initialized restarter protocol\n");
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate /*
2130Sstevel@tonic-gate * void restarter_event_enqueue()
2140Sstevel@tonic-gate * Enqueue a restarter event.
2150Sstevel@tonic-gate */
2160Sstevel@tonic-gate static void
restarter_event_enqueue(const char * inst,restarter_event_type_t event,int32_t reason)217*12967Sgavin.maltby@oracle.com restarter_event_enqueue(const char *inst, restarter_event_type_t event,
218*12967Sgavin.maltby@oracle.com int32_t reason)
2190Sstevel@tonic-gate {
2200Sstevel@tonic-gate restarter_protocol_event_t *e;
2210Sstevel@tonic-gate int r;
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate /* Allocate and populate the event structure. */
2240Sstevel@tonic-gate e = startd_zalloc(sizeof (restarter_protocol_event_t));
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate e->rpe_inst = startd_alloc(strlen(inst) + 1);
2270Sstevel@tonic-gate (void) strlcpy(e->rpe_inst, inst, strlen(inst)+1);
2280Sstevel@tonic-gate e->rpe_type = event;
229*12967Sgavin.maltby@oracle.com e->rpe_reason = reason;
2300Sstevel@tonic-gate
2310Sstevel@tonic-gate MUTEX_LOCK(&restarter_queue->rpeq_lock);
2320Sstevel@tonic-gate uu_list_node_init(e, &e->rpe_link, restarter_protocol_event_queue_pool);
2330Sstevel@tonic-gate r = uu_list_insert_before(restarter_queue->rpeq_event_list, NULL, e);
2340Sstevel@tonic-gate assert(r == 0);
2350Sstevel@tonic-gate
2360Sstevel@tonic-gate MUTEX_UNLOCK(&restarter_queue->rpeq_lock);
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate
2400Sstevel@tonic-gate void
restarter_event_release(restarter_protocol_event_t * e)2410Sstevel@tonic-gate restarter_event_release(restarter_protocol_event_t *e)
2420Sstevel@tonic-gate {
2430Sstevel@tonic-gate uu_list_node_fini(e, &e->rpe_link, restarter_protocol_event_queue_pool);
2440Sstevel@tonic-gate startd_free(e->rpe_inst, strlen(e->rpe_inst) + 1);
2450Sstevel@tonic-gate startd_free(e, sizeof (restarter_protocol_event_t));
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate
2480Sstevel@tonic-gate /*
2490Sstevel@tonic-gate * restarter_protocol_event_t *restarter_event_dequeue()
2500Sstevel@tonic-gate * Dequeue a restarter protocol event. The caller is expected to be
2510Sstevel@tonic-gate * a single thread. It is allowed to utilize restarter_event_requeue()
2520Sstevel@tonic-gate * and abort processing on the event. The caller is expected to call
2530Sstevel@tonic-gate * restarter_event_release() when finished.
2540Sstevel@tonic-gate */
2550Sstevel@tonic-gate restarter_protocol_event_t *
restarter_event_dequeue()2560Sstevel@tonic-gate restarter_event_dequeue()
2570Sstevel@tonic-gate {
2580Sstevel@tonic-gate restarter_protocol_event_t *e = NULL;
2590Sstevel@tonic-gate
2600Sstevel@tonic-gate MUTEX_LOCK(&restarter_queue->rpeq_lock);
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate e = uu_list_first(restarter_queue->rpeq_event_list);
2630Sstevel@tonic-gate if (e == NULL) {
2640Sstevel@tonic-gate MUTEX_UNLOCK(&restarter_queue->rpeq_lock);
2650Sstevel@tonic-gate return (NULL);
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate
2680Sstevel@tonic-gate if (uu_list_next(restarter_queue->rpeq_event_list, e) != NULL)
2690Sstevel@tonic-gate ru->restarter_update_wakeup = 1;
2700Sstevel@tonic-gate uu_list_remove(restarter_queue->rpeq_event_list, e);
2710Sstevel@tonic-gate MUTEX_UNLOCK(&restarter_queue->rpeq_lock);
2720Sstevel@tonic-gate
2730Sstevel@tonic-gate return (e);
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate
2760Sstevel@tonic-gate static int
state_cb(sysevent_t * syse,void * cookie)2770Sstevel@tonic-gate state_cb(sysevent_t *syse, void *cookie)
2780Sstevel@tonic-gate {
2790Sstevel@tonic-gate char *fmri = (char *)cookie;
2800Sstevel@tonic-gate char *instance_name;
281*12967Sgavin.maltby@oracle.com int32_t reason;
2820Sstevel@tonic-gate nvlist_t *attr_list = NULL;
2830Sstevel@tonic-gate int state, next_state;
2843639Srm88369 char str_state[MAX_SCF_STATE_STRING_SZ];
2853639Srm88369 char str_next_state[MAX_SCF_STATE_STRING_SZ];
2860Sstevel@tonic-gate protocol_states_t *states;
2870Sstevel@tonic-gate int err;
2883639Srm88369 ssize_t sz;
2890Sstevel@tonic-gate
2900Sstevel@tonic-gate /*
2910Sstevel@tonic-gate * Might fail due to a bad event or a lack of memory. Try
2920Sstevel@tonic-gate * the callback again to see if it goes better the next time.
2930Sstevel@tonic-gate */
2940Sstevel@tonic-gate if (sysevent_get_attr_list(syse, &attr_list) != 0)
2950Sstevel@tonic-gate return (EAGAIN);
2960Sstevel@tonic-gate
2970Sstevel@tonic-gate if ((nvlist_lookup_int32(attr_list, RESTARTER_NAME_STATE,
2980Sstevel@tonic-gate &state) != 0) ||
2990Sstevel@tonic-gate (nvlist_lookup_int32(attr_list, RESTARTER_NAME_NEXT_STATE,
3000Sstevel@tonic-gate &next_state) != 0) ||
3010Sstevel@tonic-gate (nvlist_lookup_int32(attr_list, RESTARTER_NAME_ERROR, &err) != 0) ||
3020Sstevel@tonic-gate (nvlist_lookup_string(attr_list, RESTARTER_NAME_INSTANCE,
303*12967Sgavin.maltby@oracle.com &instance_name) != 0) ||
304*12967Sgavin.maltby@oracle.com (nvlist_lookup_int32(attr_list, RESTARTER_NAME_REASON, &reason) !=
305*12967Sgavin.maltby@oracle.com 0))
3060Sstevel@tonic-gate uu_die("%s: can't decode nvlist\n", fmri);
3070Sstevel@tonic-gate
3080Sstevel@tonic-gate states = startd_alloc(sizeof (protocol_states_t));
3090Sstevel@tonic-gate states->ps_state = state;
3100Sstevel@tonic-gate states->ps_state_next = next_state;
3110Sstevel@tonic-gate states->ps_err = err;
312*12967Sgavin.maltby@oracle.com states->ps_reason = reason;
3130Sstevel@tonic-gate
3140Sstevel@tonic-gate graph_protocol_send_event(instance_name, GRAPH_UPDATE_STATE_CHANGE,
3150Sstevel@tonic-gate states);
3160Sstevel@tonic-gate
3173639Srm88369 sz = restarter_state_to_string(state, str_state, sizeof (str_state));
3183639Srm88369 assert(sz < sizeof (str_state));
3193639Srm88369 sz = restarter_state_to_string(next_state, str_next_state,
3203639Srm88369 sizeof (str_next_state));
3213639Srm88369 assert(sz < sizeof (str_next_state));
3223639Srm88369 log_framework(LOG_DEBUG, "%s: state updates for %s (%s, %s)\n", fmri,
3233639Srm88369 instance_name, str_state, str_next_state);
3240Sstevel@tonic-gate nvlist_free(attr_list);
3250Sstevel@tonic-gate return (0);
3260Sstevel@tonic-gate }
3270Sstevel@tonic-gate
3280Sstevel@tonic-gate evchan_t *
restarter_protocol_init_delegate(char * fmri)3290Sstevel@tonic-gate restarter_protocol_init_delegate(char *fmri)
3300Sstevel@tonic-gate {
3310Sstevel@tonic-gate char *delegate_channel_name, *master_channel_name, *sid;
3320Sstevel@tonic-gate evchan_t *delegate_channel, *master_channel;
3339263SSean.Wilcox@Sun.COM int r = 0;
3340Sstevel@tonic-gate
3350Sstevel@tonic-gate /* master restarter -- nothing to do */
3369263SSean.Wilcox@Sun.COM if (strcmp(fmri, SCF_SERVICE_STARTD) == 0) {
3379263SSean.Wilcox@Sun.COM uu_warn("Attempt to initialize restarter protocol delegate "
3389263SSean.Wilcox@Sun.COM "with %s\n", fmri);
3390Sstevel@tonic-gate return (NULL);
3409263SSean.Wilcox@Sun.COM }
3410Sstevel@tonic-gate
3420Sstevel@tonic-gate log_framework(LOG_DEBUG, "%s: Intializing protocol for delegate\n",
3430Sstevel@tonic-gate fmri);
3440Sstevel@tonic-gate
3459263SSean.Wilcox@Sun.COM delegate_channel_name = master_channel_name = NULL;
3460Sstevel@tonic-gate if ((delegate_channel_name = _restarter_get_channel_name(fmri,
3470Sstevel@tonic-gate RESTARTER_CHANNEL_DELEGATE)) == NULL ||
3480Sstevel@tonic-gate (master_channel_name = _restarter_get_channel_name(fmri,
3490Sstevel@tonic-gate RESTARTER_CHANNEL_MASTER)) == NULL ||
3509263SSean.Wilcox@Sun.COM (sid = strdup("svc.startd")) == NULL) {
3519263SSean.Wilcox@Sun.COM if (delegate_channel_name) {
3529263SSean.Wilcox@Sun.COM free(delegate_channel_name);
3539263SSean.Wilcox@Sun.COM }
3549263SSean.Wilcox@Sun.COM if (master_channel_name) {
3559263SSean.Wilcox@Sun.COM free(master_channel_name);
3569263SSean.Wilcox@Sun.COM }
3579263SSean.Wilcox@Sun.COM uu_warn("Allocation of channel name failed");
3589263SSean.Wilcox@Sun.COM
3599263SSean.Wilcox@Sun.COM return (NULL);
3609263SSean.Wilcox@Sun.COM }
3610Sstevel@tonic-gate
3629263SSean.Wilcox@Sun.COM if ((r = sysevent_evc_bind(delegate_channel_name, &delegate_channel,
3639263SSean.Wilcox@Sun.COM EVCH_CREAT|EVCH_HOLD_PEND)) != 0) {
3649263SSean.Wilcox@Sun.COM uu_warn("%s: sysevent_evc_bind failed: %s\n",
3650Sstevel@tonic-gate delegate_channel_name, strerror(errno));
3669263SSean.Wilcox@Sun.COM goto out;
3679263SSean.Wilcox@Sun.COM }
3689263SSean.Wilcox@Sun.COM
3699263SSean.Wilcox@Sun.COM if ((r = sysevent_evc_bind(master_channel_name, &master_channel,
3709263SSean.Wilcox@Sun.COM EVCH_CREAT|EVCH_HOLD_PEND)) != 0) {
3719263SSean.Wilcox@Sun.COM uu_warn("%s: sysevent_evc_bind failed: %s\n",
3720Sstevel@tonic-gate master_channel_name, strerror(errno));
3739263SSean.Wilcox@Sun.COM goto out;
3749263SSean.Wilcox@Sun.COM }
3759263SSean.Wilcox@Sun.COM
3760Sstevel@tonic-gate log_framework(LOG_DEBUG,
3770Sstevel@tonic-gate "%s: Bound to channel %s (delegate), %s (master)\n", fmri,
3780Sstevel@tonic-gate delegate_channel_name, master_channel_name);
3790Sstevel@tonic-gate
3809263SSean.Wilcox@Sun.COM if ((r = sysevent_evc_subscribe(master_channel, sid, EC_ALL,
3819263SSean.Wilcox@Sun.COM state_cb, fmri, EVCH_SUB_KEEP)) != 0) {
3829263SSean.Wilcox@Sun.COM /*
3839263SSean.Wilcox@Sun.COM * The following errors can be returned in this
3849263SSean.Wilcox@Sun.COM * case :
3859263SSean.Wilcox@Sun.COM * EINVAL : inappropriate flags or dump flag
3869263SSean.Wilcox@Sun.COM * and the dump failed.
3879263SSean.Wilcox@Sun.COM * EEXIST : svc.startd already has a channel
3889263SSean.Wilcox@Sun.COM * named as the master channel name
3899263SSean.Wilcox@Sun.COM * ENOMEM : too many subscribers to the channel
3909263SSean.Wilcox@Sun.COM */
3919263SSean.Wilcox@Sun.COM uu_warn("Failed to subscribe to restarter %s, channel %s with "
3929263SSean.Wilcox@Sun.COM "subscriber id %s : \n", fmri, master_channel_name, sid);
3939263SSean.Wilcox@Sun.COM switch (r) {
3949263SSean.Wilcox@Sun.COM case EEXIST:
3959263SSean.Wilcox@Sun.COM uu_warn("Channel name already exists\n");
3969263SSean.Wilcox@Sun.COM break;
3979263SSean.Wilcox@Sun.COM case ENOMEM:
3989263SSean.Wilcox@Sun.COM uu_warn("Too many subscribers for the channel\n");
3999263SSean.Wilcox@Sun.COM break;
4009263SSean.Wilcox@Sun.COM default:
4019263SSean.Wilcox@Sun.COM uu_warn("%s\n", strerror(errno));
4029263SSean.Wilcox@Sun.COM }
4039263SSean.Wilcox@Sun.COM } else {
4049263SSean.Wilcox@Sun.COM log_framework(LOG_DEBUG,
4059263SSean.Wilcox@Sun.COM "%s: Subscribed to channel %s with subscriber id %s\n",
4069263SSean.Wilcox@Sun.COM fmri, master_channel_name, "svc.startd");
4079263SSean.Wilcox@Sun.COM }
4080Sstevel@tonic-gate
4099263SSean.Wilcox@Sun.COM
4109263SSean.Wilcox@Sun.COM out:
4110Sstevel@tonic-gate free(delegate_channel_name);
4120Sstevel@tonic-gate free(master_channel_name);
4130Sstevel@tonic-gate free(sid);
4140Sstevel@tonic-gate
4159263SSean.Wilcox@Sun.COM if (r == 0)
4169263SSean.Wilcox@Sun.COM return (delegate_channel);
4179263SSean.Wilcox@Sun.COM
4189263SSean.Wilcox@Sun.COM return (NULL);
4190Sstevel@tonic-gate }
4200Sstevel@tonic-gate
4210Sstevel@tonic-gate void
restarter_protocol_send_event(const char * inst,evchan_t * chan,restarter_event_type_t event,int32_t reason)4220Sstevel@tonic-gate restarter_protocol_send_event(const char *inst, evchan_t *chan,
423*12967Sgavin.maltby@oracle.com restarter_event_type_t event, int32_t reason)
4240Sstevel@tonic-gate {
4250Sstevel@tonic-gate nvlist_t *attr;
4266045Srm88369 int ret;
4270Sstevel@tonic-gate
4280Sstevel@tonic-gate /*
4290Sstevel@tonic-gate * If the service is managed by the master restarter,
4300Sstevel@tonic-gate * queue the event locally.
4310Sstevel@tonic-gate */
4320Sstevel@tonic-gate if (chan == NULL) {
433*12967Sgavin.maltby@oracle.com restarter_event_enqueue(inst, event, reason);
4340Sstevel@tonic-gate MUTEX_LOCK(&ru->restarter_update_lock);
4350Sstevel@tonic-gate ru->restarter_update_wakeup = 1;
4360Sstevel@tonic-gate (void) pthread_cond_broadcast(&ru->restarter_update_cv);
4370Sstevel@tonic-gate MUTEX_UNLOCK(&ru->restarter_update_lock);
4380Sstevel@tonic-gate return;
4390Sstevel@tonic-gate }
4400Sstevel@tonic-gate
4410Sstevel@tonic-gate /*
4420Sstevel@tonic-gate * Otherwise, send the event to the delegate.
4430Sstevel@tonic-gate */
4440Sstevel@tonic-gate log_framework(LOG_DEBUG, "Sending %s to channel 0x%p for %s.\n",
4450Sstevel@tonic-gate event_names[event], chan, inst);
4460Sstevel@tonic-gate if (nvlist_alloc(&attr, NV_UNIQUE_NAME, 0) != 0 ||
4470Sstevel@tonic-gate nvlist_add_uint32(attr, RESTARTER_NAME_TYPE, event) != 0 ||
448*12967Sgavin.maltby@oracle.com nvlist_add_string(attr, RESTARTER_NAME_INSTANCE, (char *)inst) !=
449*12967Sgavin.maltby@oracle.com 0 || nvlist_add_uint32(attr, RESTARTER_NAME_REASON,
450*12967Sgavin.maltby@oracle.com reason) != 0)
4510Sstevel@tonic-gate uu_die("Allocation failure\n");
4520Sstevel@tonic-gate
4536045Srm88369 if ((ret = restarter_event_publish_retry(chan, "protocol", "restarter",
4546045Srm88369 "com.sun", "svc.startd", attr, EVCH_NOSLEEP)) != 0) {
4556045Srm88369
4566045Srm88369 switch (ret) {
4576045Srm88369 case ENOSPC:
4586045Srm88369 log_framework(LOG_DEBUG, "Dropping %s event for %s. "
4596045Srm88369 "Delegate may not be running.\n",
4606045Srm88369 event_names[event], inst);
4616045Srm88369 break;
4626045Srm88369 default:
4636045Srm88369 uu_die("%s: can't publish event: %s\n", inst,
4646045Srm88369 strerror(errno));
4656045Srm88369 }
4660Sstevel@tonic-gate }
4676045Srm88369
4680Sstevel@tonic-gate nvlist_free(attr);
4690Sstevel@tonic-gate
4700Sstevel@tonic-gate if (event != RESTARTER_EVENT_TYPE_ADD_INSTANCE) {
4710Sstevel@tonic-gate /*
4720Sstevel@tonic-gate * Not relevant for graph loading.
4730Sstevel@tonic-gate */
4740Sstevel@tonic-gate return;
4750Sstevel@tonic-gate }
4760Sstevel@tonic-gate
4770Sstevel@tonic-gate /*
4780Sstevel@tonic-gate * For the purposes of loading state after interruption, this is
4790Sstevel@tonic-gate * sufficient, as svc.startd(1M) won't receive events on the contracts
4800Sstevel@tonic-gate * associated with each delegate.
4810Sstevel@tonic-gate */
4820Sstevel@tonic-gate MUTEX_LOCK(&st->st_load_lock);
4830Sstevel@tonic-gate if (--st->st_load_instances == 0)
4840Sstevel@tonic-gate (void) pthread_cond_broadcast(&st->st_load_cv);
4850Sstevel@tonic-gate MUTEX_UNLOCK(&st->st_load_lock);
4860Sstevel@tonic-gate
4870Sstevel@tonic-gate }
488