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 573b709eaSrm88369 * Common Development and Distribution License (the "License"). 673b709eaSrm88369 * 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*13d8aaa1SSean Wilcox * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate /* 277c478bd9Sstevel@tonic-gate * protocol.c - protocols between graph engine and restarters 287c478bd9Sstevel@tonic-gate * 297c478bd9Sstevel@tonic-gate * The graph engine uses restarter_protocol_send_event() to send a 307c478bd9Sstevel@tonic-gate * restarter_event_type_t to the restarter. For delegated restarters, 317c478bd9Sstevel@tonic-gate * this is published on the GPEC queue for the restarter, which can 327c478bd9Sstevel@tonic-gate * then be consumed by the librestart interfaces. For services managed 337c478bd9Sstevel@tonic-gate * by svc.startd, the event is stored on the local restarter_queue list, 347c478bd9Sstevel@tonic-gate * where it can be dequeued by the restarter. 357c478bd9Sstevel@tonic-gate * 367c478bd9Sstevel@tonic-gate * The svc.startd restarter uses graph_protocol_send_event() to send 377c478bd9Sstevel@tonic-gate * a graph_event_type_t to the graph engine when an instance's states are 387c478bd9Sstevel@tonic-gate * updated. 397c478bd9Sstevel@tonic-gate * 407c478bd9Sstevel@tonic-gate * The graph engine uses restarter_protocol_init_delegate() to 417c478bd9Sstevel@tonic-gate * register its interest in a particular delegated restarter's instance 427c478bd9Sstevel@tonic-gate * state events. The state_cb() registered on the event channel then 437c478bd9Sstevel@tonic-gate * invokes graph_protocol_send_event() to communicate the update to 447c478bd9Sstevel@tonic-gate * the graph engine. 457c478bd9Sstevel@tonic-gate */ 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate #include <assert.h> 487c478bd9Sstevel@tonic-gate #include <libintl.h> 497c478bd9Sstevel@tonic-gate #include <libsysevent.h> 507c478bd9Sstevel@tonic-gate #include <pthread.h> 517c478bd9Sstevel@tonic-gate #include <stdarg.h> 527c478bd9Sstevel@tonic-gate #include <stdio.h> 537c478bd9Sstevel@tonic-gate #include <strings.h> 547c478bd9Sstevel@tonic-gate #include <sys/time.h> 557c478bd9Sstevel@tonic-gate #include <errno.h> 567c478bd9Sstevel@tonic-gate #include <libuutil.h> 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate #include <librestart.h> 597c478bd9Sstevel@tonic-gate #include <librestart_priv.h> 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate #include "protocol.h" 627c478bd9Sstevel@tonic-gate #include "startd.h" 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate /* Local event queue structures. */ 657c478bd9Sstevel@tonic-gate typedef struct graph_protocol_event_queue { 667c478bd9Sstevel@tonic-gate uu_list_t *gpeq_event_list; 677c478bd9Sstevel@tonic-gate pthread_mutex_t gpeq_lock; 687c478bd9Sstevel@tonic-gate } graph_protocol_event_queue_t; 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate typedef struct restarter_protocol_event_queue { 717c478bd9Sstevel@tonic-gate uu_list_t *rpeq_event_list; 727c478bd9Sstevel@tonic-gate pthread_mutex_t rpeq_lock; 737c478bd9Sstevel@tonic-gate } restarter_protocol_event_queue_t; 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate static uu_list_pool_t *restarter_protocol_event_queue_pool; 767c478bd9Sstevel@tonic-gate static restarter_protocol_event_queue_t *restarter_queue; 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate static uu_list_pool_t *graph_protocol_event_queue_pool; 797c478bd9Sstevel@tonic-gate static graph_protocol_event_queue_t *graph_queue; 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate void 827c478bd9Sstevel@tonic-gate graph_protocol_init() 837c478bd9Sstevel@tonic-gate { 847c478bd9Sstevel@tonic-gate graph_protocol_event_queue_pool = startd_list_pool_create( 857c478bd9Sstevel@tonic-gate "graph_protocol_events", sizeof (graph_protocol_event_t), 867c478bd9Sstevel@tonic-gate offsetof(graph_protocol_event_t, gpe_link), NULL, 877c478bd9Sstevel@tonic-gate UU_LIST_POOL_DEBUG); 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate graph_queue = startd_zalloc(sizeof (graph_protocol_event_queue_t)); 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate (void) pthread_mutex_init(&graph_queue->gpeq_lock, &mutex_attrs); 927c478bd9Sstevel@tonic-gate graph_queue->gpeq_event_list = startd_list_create( 937c478bd9Sstevel@tonic-gate graph_protocol_event_queue_pool, graph_queue, NULL); 947c478bd9Sstevel@tonic-gate } 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate /* 977c478bd9Sstevel@tonic-gate * "data" will be freed by the consumer 987c478bd9Sstevel@tonic-gate */ 997c478bd9Sstevel@tonic-gate static void 1007c478bd9Sstevel@tonic-gate graph_event_enqueue(const char *inst, graph_event_type_t event, 1017c478bd9Sstevel@tonic-gate protocol_states_t *data) 1027c478bd9Sstevel@tonic-gate { 1037c478bd9Sstevel@tonic-gate graph_protocol_event_t *e; 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate e = startd_zalloc(sizeof (graph_protocol_event_t)); 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate if (inst != NULL) { 1087c478bd9Sstevel@tonic-gate int size = strlen(inst) + 1; 1097c478bd9Sstevel@tonic-gate e->gpe_inst = startd_alloc(size); 1107c478bd9Sstevel@tonic-gate e->gpe_inst_sz = size; 1117c478bd9Sstevel@tonic-gate (void) strlcpy(e->gpe_inst, inst, size); 1127c478bd9Sstevel@tonic-gate } 1137c478bd9Sstevel@tonic-gate e->gpe_type = event; 1147c478bd9Sstevel@tonic-gate e->gpe_data = data; 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate (void) pthread_mutex_init(&e->gpe_lock, &mutex_attrs); 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate MUTEX_LOCK(&graph_queue->gpeq_lock); 1197c478bd9Sstevel@tonic-gate uu_list_node_init(e, &e->gpe_link, graph_protocol_event_queue_pool); 1207c478bd9Sstevel@tonic-gate if (uu_list_insert_before(graph_queue->gpeq_event_list, NULL, e) == -1) 1217c478bd9Sstevel@tonic-gate uu_die("failed to enqueue graph event (%s: %s)\n", 1227c478bd9Sstevel@tonic-gate e->gpe_inst, uu_strerror(uu_error())); 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate MUTEX_UNLOCK(&graph_queue->gpeq_lock); 1257c478bd9Sstevel@tonic-gate } 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate void 1287c478bd9Sstevel@tonic-gate graph_event_release(graph_protocol_event_t *e) 1297c478bd9Sstevel@tonic-gate { 1307c478bd9Sstevel@tonic-gate uu_list_node_fini(e, &e->gpe_link, graph_protocol_event_queue_pool); 1317c478bd9Sstevel@tonic-gate (void) pthread_mutex_destroy(&e->gpe_lock); 1327c478bd9Sstevel@tonic-gate if (e->gpe_inst != NULL) 1337c478bd9Sstevel@tonic-gate startd_free(e->gpe_inst, e->gpe_inst_sz); 1347c478bd9Sstevel@tonic-gate startd_free(e, sizeof (graph_protocol_event_t)); 1357c478bd9Sstevel@tonic-gate } 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate /* 1387c478bd9Sstevel@tonic-gate * graph_protocol_event_t *graph_event_dequeue() 1397c478bd9Sstevel@tonic-gate * The caller must hold gu_lock, and is expected to be a single thread. 1407c478bd9Sstevel@tonic-gate * It is allowed to utilize graph_event_requeue() and abort processing 1417c478bd9Sstevel@tonic-gate * on the event. If graph_event_requeue() is not called, the caller is 1427c478bd9Sstevel@tonic-gate * expected to call graph_event_release() when finished. 1437c478bd9Sstevel@tonic-gate */ 1447c478bd9Sstevel@tonic-gate graph_protocol_event_t * 1457c478bd9Sstevel@tonic-gate graph_event_dequeue() 1467c478bd9Sstevel@tonic-gate { 1477c478bd9Sstevel@tonic-gate graph_protocol_event_t *e; 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate MUTEX_LOCK(&graph_queue->gpeq_lock); 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate e = uu_list_first(graph_queue->gpeq_event_list); 1527c478bd9Sstevel@tonic-gate if (e == NULL) { 1537c478bd9Sstevel@tonic-gate MUTEX_UNLOCK(&graph_queue->gpeq_lock); 1547c478bd9Sstevel@tonic-gate return (NULL); 1557c478bd9Sstevel@tonic-gate } 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate if (uu_list_next(graph_queue->gpeq_event_list, e) != NULL) 1587c478bd9Sstevel@tonic-gate gu->gu_wakeup = 1; 1597c478bd9Sstevel@tonic-gate uu_list_remove(graph_queue->gpeq_event_list, e); 1607c478bd9Sstevel@tonic-gate MUTEX_UNLOCK(&graph_queue->gpeq_lock); 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate return (e); 1637c478bd9Sstevel@tonic-gate } 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate /* 1667c478bd9Sstevel@tonic-gate * void graph_event_requeue() 1677c478bd9Sstevel@tonic-gate * Requeue the event back at the head of the queue. 1687c478bd9Sstevel@tonic-gate */ 1697c478bd9Sstevel@tonic-gate void 1707c478bd9Sstevel@tonic-gate graph_event_requeue(graph_protocol_event_t *e) 1717c478bd9Sstevel@tonic-gate { 1727c478bd9Sstevel@tonic-gate assert(e != NULL); 1737c478bd9Sstevel@tonic-gate 1747c478bd9Sstevel@tonic-gate log_framework(LOG_DEBUG, "Requeing event\n"); 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate MUTEX_LOCK(&graph_queue->gpeq_lock); 1777c478bd9Sstevel@tonic-gate if (uu_list_insert_after(graph_queue->gpeq_event_list, NULL, e) == -1) 1787c478bd9Sstevel@tonic-gate uu_die("failed to requeue graph event (%s: %s)\n", 1797c478bd9Sstevel@tonic-gate e->gpe_inst, uu_strerror(uu_error())); 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate MUTEX_UNLOCK(&graph_queue->gpeq_lock); 1827c478bd9Sstevel@tonic-gate } 1837c478bd9Sstevel@tonic-gate 1847c478bd9Sstevel@tonic-gate void 1857c478bd9Sstevel@tonic-gate graph_protocol_send_event(const char *inst, graph_event_type_t event, 1867c478bd9Sstevel@tonic-gate protocol_states_t *data) 1877c478bd9Sstevel@tonic-gate { 1887c478bd9Sstevel@tonic-gate graph_event_enqueue(inst, event, data); 1897c478bd9Sstevel@tonic-gate MUTEX_LOCK(&gu->gu_lock); 1907c478bd9Sstevel@tonic-gate gu->gu_wakeup = 1; 1917c478bd9Sstevel@tonic-gate (void) pthread_cond_broadcast(&gu->gu_cv); 1927c478bd9Sstevel@tonic-gate MUTEX_UNLOCK(&gu->gu_lock); 1937c478bd9Sstevel@tonic-gate } 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate void 1967c478bd9Sstevel@tonic-gate restarter_protocol_init() 1977c478bd9Sstevel@tonic-gate { 1987c478bd9Sstevel@tonic-gate restarter_protocol_event_queue_pool = startd_list_pool_create( 1997c478bd9Sstevel@tonic-gate "restarter_protocol_events", sizeof (restarter_protocol_event_t), 2007c478bd9Sstevel@tonic-gate offsetof(restarter_protocol_event_t, rpe_link), NULL, 2017c478bd9Sstevel@tonic-gate UU_LIST_POOL_DEBUG); 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate restarter_queue = startd_zalloc( 2047c478bd9Sstevel@tonic-gate sizeof (restarter_protocol_event_queue_t)); 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate (void) pthread_mutex_init(&restarter_queue->rpeq_lock, &mutex_attrs); 2077c478bd9Sstevel@tonic-gate restarter_queue->rpeq_event_list = startd_list_create( 2087c478bd9Sstevel@tonic-gate restarter_protocol_event_queue_pool, restarter_queue, NULL); 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate log_framework(LOG_DEBUG, "Initialized restarter protocol\n"); 2117c478bd9Sstevel@tonic-gate } 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate /* 2147c478bd9Sstevel@tonic-gate * void restarter_event_enqueue() 2157c478bd9Sstevel@tonic-gate * Enqueue a restarter event. 2167c478bd9Sstevel@tonic-gate */ 2177c478bd9Sstevel@tonic-gate static void 2187c478bd9Sstevel@tonic-gate restarter_event_enqueue(const char *inst, restarter_event_type_t event) 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; 2297c478bd9Sstevel@tonic-gate 2307c478bd9Sstevel@tonic-gate MUTEX_LOCK(&restarter_queue->rpeq_lock); 2317c478bd9Sstevel@tonic-gate uu_list_node_init(e, &e->rpe_link, restarter_protocol_event_queue_pool); 2327c478bd9Sstevel@tonic-gate r = uu_list_insert_before(restarter_queue->rpeq_event_list, NULL, e); 2337c478bd9Sstevel@tonic-gate assert(r == 0); 2347c478bd9Sstevel@tonic-gate 2357c478bd9Sstevel@tonic-gate MUTEX_UNLOCK(&restarter_queue->rpeq_lock); 2367c478bd9Sstevel@tonic-gate 2377c478bd9Sstevel@tonic-gate } 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate void 2407c478bd9Sstevel@tonic-gate restarter_event_release(restarter_protocol_event_t *e) 2417c478bd9Sstevel@tonic-gate { 2427c478bd9Sstevel@tonic-gate uu_list_node_fini(e, &e->rpe_link, restarter_protocol_event_queue_pool); 2437c478bd9Sstevel@tonic-gate startd_free(e->rpe_inst, strlen(e->rpe_inst) + 1); 2447c478bd9Sstevel@tonic-gate startd_free(e, sizeof (restarter_protocol_event_t)); 2457c478bd9Sstevel@tonic-gate } 2467c478bd9Sstevel@tonic-gate 2477c478bd9Sstevel@tonic-gate /* 2487c478bd9Sstevel@tonic-gate * restarter_protocol_event_t *restarter_event_dequeue() 2497c478bd9Sstevel@tonic-gate * Dequeue a restarter protocol event. The caller is expected to be 2507c478bd9Sstevel@tonic-gate * a single thread. It is allowed to utilize restarter_event_requeue() 2517c478bd9Sstevel@tonic-gate * and abort processing on the event. The caller is expected to call 2527c478bd9Sstevel@tonic-gate * restarter_event_release() when finished. 2537c478bd9Sstevel@tonic-gate */ 2547c478bd9Sstevel@tonic-gate restarter_protocol_event_t * 2557c478bd9Sstevel@tonic-gate restarter_event_dequeue() 2567c478bd9Sstevel@tonic-gate { 2577c478bd9Sstevel@tonic-gate restarter_protocol_event_t *e = NULL; 2587c478bd9Sstevel@tonic-gate 2597c478bd9Sstevel@tonic-gate MUTEX_LOCK(&restarter_queue->rpeq_lock); 2607c478bd9Sstevel@tonic-gate 2617c478bd9Sstevel@tonic-gate e = uu_list_first(restarter_queue->rpeq_event_list); 2627c478bd9Sstevel@tonic-gate if (e == NULL) { 2637c478bd9Sstevel@tonic-gate MUTEX_UNLOCK(&restarter_queue->rpeq_lock); 2647c478bd9Sstevel@tonic-gate return (NULL); 2657c478bd9Sstevel@tonic-gate } 2667c478bd9Sstevel@tonic-gate 2677c478bd9Sstevel@tonic-gate if (uu_list_next(restarter_queue->rpeq_event_list, e) != NULL) 2687c478bd9Sstevel@tonic-gate ru->restarter_update_wakeup = 1; 2697c478bd9Sstevel@tonic-gate uu_list_remove(restarter_queue->rpeq_event_list, e); 2707c478bd9Sstevel@tonic-gate MUTEX_UNLOCK(&restarter_queue->rpeq_lock); 2717c478bd9Sstevel@tonic-gate 2727c478bd9Sstevel@tonic-gate return (e); 2737c478bd9Sstevel@tonic-gate } 2747c478bd9Sstevel@tonic-gate 2757c478bd9Sstevel@tonic-gate static int 2767c478bd9Sstevel@tonic-gate state_cb(sysevent_t *syse, void *cookie) 2777c478bd9Sstevel@tonic-gate { 2787c478bd9Sstevel@tonic-gate char *fmri = (char *)cookie; 2797c478bd9Sstevel@tonic-gate char *instance_name; 2807c478bd9Sstevel@tonic-gate nvlist_t *attr_list = NULL; 2817c478bd9Sstevel@tonic-gate int state, next_state; 28273b709eaSrm88369 char str_state[MAX_SCF_STATE_STRING_SZ]; 28373b709eaSrm88369 char str_next_state[MAX_SCF_STATE_STRING_SZ]; 2847c478bd9Sstevel@tonic-gate protocol_states_t *states; 2857c478bd9Sstevel@tonic-gate int err; 28673b709eaSrm88369 ssize_t sz; 2877c478bd9Sstevel@tonic-gate 2887c478bd9Sstevel@tonic-gate /* 2897c478bd9Sstevel@tonic-gate * Might fail due to a bad event or a lack of memory. Try 2907c478bd9Sstevel@tonic-gate * the callback again to see if it goes better the next time. 2917c478bd9Sstevel@tonic-gate */ 2927c478bd9Sstevel@tonic-gate if (sysevent_get_attr_list(syse, &attr_list) != 0) 2937c478bd9Sstevel@tonic-gate return (EAGAIN); 2947c478bd9Sstevel@tonic-gate 2957c478bd9Sstevel@tonic-gate if ((nvlist_lookup_int32(attr_list, RESTARTER_NAME_STATE, 2967c478bd9Sstevel@tonic-gate &state) != 0) || 2977c478bd9Sstevel@tonic-gate (nvlist_lookup_int32(attr_list, RESTARTER_NAME_NEXT_STATE, 2987c478bd9Sstevel@tonic-gate &next_state) != 0) || 2997c478bd9Sstevel@tonic-gate (nvlist_lookup_int32(attr_list, RESTARTER_NAME_ERROR, &err) != 0) || 3007c478bd9Sstevel@tonic-gate (nvlist_lookup_string(attr_list, RESTARTER_NAME_INSTANCE, 3017c478bd9Sstevel@tonic-gate &instance_name) != 0)) 3027c478bd9Sstevel@tonic-gate uu_die("%s: can't decode nvlist\n", fmri); 3037c478bd9Sstevel@tonic-gate 3047c478bd9Sstevel@tonic-gate states = startd_alloc(sizeof (protocol_states_t)); 3057c478bd9Sstevel@tonic-gate states->ps_state = state; 3067c478bd9Sstevel@tonic-gate states->ps_state_next = next_state; 3077c478bd9Sstevel@tonic-gate states->ps_err = err; 3087c478bd9Sstevel@tonic-gate 3097c478bd9Sstevel@tonic-gate graph_protocol_send_event(instance_name, GRAPH_UPDATE_STATE_CHANGE, 3107c478bd9Sstevel@tonic-gate states); 3117c478bd9Sstevel@tonic-gate 31273b709eaSrm88369 sz = restarter_state_to_string(state, str_state, sizeof (str_state)); 31373b709eaSrm88369 assert(sz < sizeof (str_state)); 31473b709eaSrm88369 sz = restarter_state_to_string(next_state, str_next_state, 31573b709eaSrm88369 sizeof (str_next_state)); 31673b709eaSrm88369 assert(sz < sizeof (str_next_state)); 31773b709eaSrm88369 log_framework(LOG_DEBUG, "%s: state updates for %s (%s, %s)\n", fmri, 31873b709eaSrm88369 instance_name, str_state, str_next_state); 3197c478bd9Sstevel@tonic-gate nvlist_free(attr_list); 3207c478bd9Sstevel@tonic-gate return (0); 3217c478bd9Sstevel@tonic-gate } 3227c478bd9Sstevel@tonic-gate 3237c478bd9Sstevel@tonic-gate evchan_t * 3247c478bd9Sstevel@tonic-gate restarter_protocol_init_delegate(char *fmri) 3257c478bd9Sstevel@tonic-gate { 3267c478bd9Sstevel@tonic-gate char *delegate_channel_name, *master_channel_name, *sid; 3277c478bd9Sstevel@tonic-gate evchan_t *delegate_channel, *master_channel; 328*13d8aaa1SSean Wilcox int r = 0; 3297c478bd9Sstevel@tonic-gate 3307c478bd9Sstevel@tonic-gate /* master restarter -- nothing to do */ 331*13d8aaa1SSean Wilcox if (strcmp(fmri, SCF_SERVICE_STARTD) == 0) { 332*13d8aaa1SSean Wilcox uu_warn("Attempt to initialize restarter protocol delegate " 333*13d8aaa1SSean Wilcox "with %s\n", fmri); 3347c478bd9Sstevel@tonic-gate return (NULL); 335*13d8aaa1SSean Wilcox } 3367c478bd9Sstevel@tonic-gate 3377c478bd9Sstevel@tonic-gate log_framework(LOG_DEBUG, "%s: Intializing protocol for delegate\n", 3387c478bd9Sstevel@tonic-gate fmri); 3397c478bd9Sstevel@tonic-gate 340*13d8aaa1SSean Wilcox delegate_channel_name = master_channel_name = NULL; 3417c478bd9Sstevel@tonic-gate if ((delegate_channel_name = _restarter_get_channel_name(fmri, 3427c478bd9Sstevel@tonic-gate RESTARTER_CHANNEL_DELEGATE)) == NULL || 3437c478bd9Sstevel@tonic-gate (master_channel_name = _restarter_get_channel_name(fmri, 3447c478bd9Sstevel@tonic-gate RESTARTER_CHANNEL_MASTER)) == NULL || 345*13d8aaa1SSean Wilcox (sid = strdup("svc.startd")) == NULL) { 346*13d8aaa1SSean Wilcox if (delegate_channel_name) { 347*13d8aaa1SSean Wilcox free(delegate_channel_name); 348*13d8aaa1SSean Wilcox } 349*13d8aaa1SSean Wilcox if (master_channel_name) { 350*13d8aaa1SSean Wilcox free(master_channel_name); 351*13d8aaa1SSean Wilcox } 352*13d8aaa1SSean Wilcox uu_warn("Allocation of channel name failed"); 3537c478bd9Sstevel@tonic-gate 354*13d8aaa1SSean Wilcox return (NULL); 355*13d8aaa1SSean Wilcox } 356*13d8aaa1SSean Wilcox 357*13d8aaa1SSean Wilcox if ((r = sysevent_evc_bind(delegate_channel_name, &delegate_channel, 358*13d8aaa1SSean Wilcox EVCH_CREAT|EVCH_HOLD_PEND)) != 0) { 359*13d8aaa1SSean Wilcox uu_warn("%s: sysevent_evc_bind failed: %s\n", 3607c478bd9Sstevel@tonic-gate delegate_channel_name, strerror(errno)); 361*13d8aaa1SSean Wilcox goto out; 362*13d8aaa1SSean Wilcox } 363*13d8aaa1SSean Wilcox 364*13d8aaa1SSean Wilcox if ((r = sysevent_evc_bind(master_channel_name, &master_channel, 365*13d8aaa1SSean Wilcox EVCH_CREAT|EVCH_HOLD_PEND)) != 0) { 366*13d8aaa1SSean Wilcox uu_warn("%s: sysevent_evc_bind failed: %s\n", 3677c478bd9Sstevel@tonic-gate master_channel_name, strerror(errno)); 368*13d8aaa1SSean Wilcox goto out; 369*13d8aaa1SSean Wilcox } 370*13d8aaa1SSean Wilcox 3717c478bd9Sstevel@tonic-gate log_framework(LOG_DEBUG, 3727c478bd9Sstevel@tonic-gate "%s: Bound to channel %s (delegate), %s (master)\n", fmri, 3737c478bd9Sstevel@tonic-gate delegate_channel_name, master_channel_name); 3747c478bd9Sstevel@tonic-gate 375*13d8aaa1SSean Wilcox if ((r = sysevent_evc_subscribe(master_channel, sid, EC_ALL, 376*13d8aaa1SSean Wilcox state_cb, fmri, EVCH_SUB_KEEP)) != 0) { 377*13d8aaa1SSean Wilcox /* 378*13d8aaa1SSean Wilcox * The following errors can be returned in this 379*13d8aaa1SSean Wilcox * case : 380*13d8aaa1SSean Wilcox * EINVAL : inappropriate flags or dump flag 381*13d8aaa1SSean Wilcox * and the dump failed. 382*13d8aaa1SSean Wilcox * EEXIST : svc.startd already has a channel 383*13d8aaa1SSean Wilcox * named as the master channel name 384*13d8aaa1SSean Wilcox * ENOMEM : too many subscribers to the channel 385*13d8aaa1SSean Wilcox */ 386*13d8aaa1SSean Wilcox uu_warn("Failed to subscribe to restarter %s, channel %s with " 387*13d8aaa1SSean Wilcox "subscriber id %s : \n", fmri, master_channel_name, sid); 388*13d8aaa1SSean Wilcox switch (r) { 389*13d8aaa1SSean Wilcox case EEXIST: 390*13d8aaa1SSean Wilcox uu_warn("Channel name already exists\n"); 391*13d8aaa1SSean Wilcox break; 392*13d8aaa1SSean Wilcox case ENOMEM: 393*13d8aaa1SSean Wilcox uu_warn("Too many subscribers for the channel\n"); 394*13d8aaa1SSean Wilcox break; 395*13d8aaa1SSean Wilcox default: 396*13d8aaa1SSean Wilcox uu_warn("%s\n", strerror(errno)); 397*13d8aaa1SSean Wilcox } 398*13d8aaa1SSean Wilcox } else { 3997c478bd9Sstevel@tonic-gate log_framework(LOG_DEBUG, 400*13d8aaa1SSean Wilcox "%s: Subscribed to channel %s with subscriber id %s\n", 401*13d8aaa1SSean Wilcox fmri, master_channel_name, "svc.startd"); 402*13d8aaa1SSean Wilcox } 4037c478bd9Sstevel@tonic-gate 404*13d8aaa1SSean Wilcox 405*13d8aaa1SSean Wilcox out: 4067c478bd9Sstevel@tonic-gate free(delegate_channel_name); 4077c478bd9Sstevel@tonic-gate free(master_channel_name); 4087c478bd9Sstevel@tonic-gate free(sid); 4097c478bd9Sstevel@tonic-gate 410*13d8aaa1SSean Wilcox if (r == 0) 4117c478bd9Sstevel@tonic-gate return (delegate_channel); 412*13d8aaa1SSean Wilcox 413*13d8aaa1SSean Wilcox return (NULL); 4147c478bd9Sstevel@tonic-gate } 4157c478bd9Sstevel@tonic-gate 4167c478bd9Sstevel@tonic-gate void 4177c478bd9Sstevel@tonic-gate restarter_protocol_send_event(const char *inst, evchan_t *chan, 4187c478bd9Sstevel@tonic-gate restarter_event_type_t event) 4197c478bd9Sstevel@tonic-gate { 4207c478bd9Sstevel@tonic-gate nvlist_t *attr; 4212c65c8b0Srm88369 int ret; 4227c478bd9Sstevel@tonic-gate 4237c478bd9Sstevel@tonic-gate /* 4247c478bd9Sstevel@tonic-gate * If the service is managed by the master restarter, 4257c478bd9Sstevel@tonic-gate * queue the event locally. 4267c478bd9Sstevel@tonic-gate */ 4277c478bd9Sstevel@tonic-gate if (chan == NULL) { 4287c478bd9Sstevel@tonic-gate restarter_event_enqueue(inst, event); 4297c478bd9Sstevel@tonic-gate MUTEX_LOCK(&ru->restarter_update_lock); 4307c478bd9Sstevel@tonic-gate ru->restarter_update_wakeup = 1; 4317c478bd9Sstevel@tonic-gate (void) pthread_cond_broadcast(&ru->restarter_update_cv); 4327c478bd9Sstevel@tonic-gate MUTEX_UNLOCK(&ru->restarter_update_lock); 4337c478bd9Sstevel@tonic-gate return; 4347c478bd9Sstevel@tonic-gate } 4357c478bd9Sstevel@tonic-gate 4367c478bd9Sstevel@tonic-gate /* 4377c478bd9Sstevel@tonic-gate * Otherwise, send the event to the delegate. 4387c478bd9Sstevel@tonic-gate */ 4397c478bd9Sstevel@tonic-gate log_framework(LOG_DEBUG, "Sending %s to channel 0x%p for %s.\n", 4407c478bd9Sstevel@tonic-gate event_names[event], chan, inst); 4417c478bd9Sstevel@tonic-gate if (nvlist_alloc(&attr, NV_UNIQUE_NAME, 0) != 0 || 4427c478bd9Sstevel@tonic-gate nvlist_add_uint32(attr, RESTARTER_NAME_TYPE, event) != 0 || 4437c478bd9Sstevel@tonic-gate nvlist_add_string(attr, RESTARTER_NAME_INSTANCE, (char *)inst) != 0) 4447c478bd9Sstevel@tonic-gate uu_die("Allocation failure\n"); 4457c478bd9Sstevel@tonic-gate 4462c65c8b0Srm88369 if ((ret = restarter_event_publish_retry(chan, "protocol", "restarter", 4472c65c8b0Srm88369 "com.sun", "svc.startd", attr, EVCH_NOSLEEP)) != 0) { 4482c65c8b0Srm88369 4492c65c8b0Srm88369 switch (ret) { 4502c65c8b0Srm88369 case ENOSPC: 4512c65c8b0Srm88369 log_framework(LOG_DEBUG, "Dropping %s event for %s. " 4522c65c8b0Srm88369 "Delegate may not be running.\n", 4532c65c8b0Srm88369 event_names[event], inst); 4542c65c8b0Srm88369 break; 4552c65c8b0Srm88369 default: 4562c65c8b0Srm88369 uu_die("%s: can't publish event: %s\n", inst, 4572c65c8b0Srm88369 strerror(errno)); 4587c478bd9Sstevel@tonic-gate } 4592c65c8b0Srm88369 } 4602c65c8b0Srm88369 4617c478bd9Sstevel@tonic-gate nvlist_free(attr); 4627c478bd9Sstevel@tonic-gate 4637c478bd9Sstevel@tonic-gate if (event != RESTARTER_EVENT_TYPE_ADD_INSTANCE) { 4647c478bd9Sstevel@tonic-gate /* 4657c478bd9Sstevel@tonic-gate * Not relevant for graph loading. 4667c478bd9Sstevel@tonic-gate */ 4677c478bd9Sstevel@tonic-gate return; 4687c478bd9Sstevel@tonic-gate } 4697c478bd9Sstevel@tonic-gate 4707c478bd9Sstevel@tonic-gate /* 4717c478bd9Sstevel@tonic-gate * For the purposes of loading state after interruption, this is 4727c478bd9Sstevel@tonic-gate * sufficient, as svc.startd(1M) won't receive events on the contracts 4737c478bd9Sstevel@tonic-gate * associated with each delegate. 4747c478bd9Sstevel@tonic-gate */ 4757c478bd9Sstevel@tonic-gate MUTEX_LOCK(&st->st_load_lock); 4767c478bd9Sstevel@tonic-gate if (--st->st_load_instances == 0) 4777c478bd9Sstevel@tonic-gate (void) pthread_cond_broadcast(&st->st_load_cv); 4787c478bd9Sstevel@tonic-gate MUTEX_UNLOCK(&st->st_load_lock); 4797c478bd9Sstevel@tonic-gate 4807c478bd9Sstevel@tonic-gate } 481