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 5*6045Srm88369 * Common Development and Distribution License (the "License"). 6*6045Srm88369 * 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*6045Srm88369 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #ifndef _LIBRESTART_H 270Sstevel@tonic-gate #define _LIBRESTART_H 280Sstevel@tonic-gate 290Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 300Sstevel@tonic-gate 31*6045Srm88369 #include <libsysevent.h> 320Sstevel@tonic-gate #include <libcontract.h> 330Sstevel@tonic-gate #include <libscf.h> 340Sstevel@tonic-gate #include <limits.h> 350Sstevel@tonic-gate #include <priv.h> 360Sstevel@tonic-gate #include <pwd.h> 370Sstevel@tonic-gate #include <sys/types.h> 380Sstevel@tonic-gate 390Sstevel@tonic-gate #ifdef __cplusplus 400Sstevel@tonic-gate extern "C" { 410Sstevel@tonic-gate #endif 420Sstevel@tonic-gate 430Sstevel@tonic-gate /* 440Sstevel@tonic-gate * There are 3 parts to librestart. 450Sstevel@tonic-gate * 1) The event protocol from the master restarter to its delegates. 460Sstevel@tonic-gate * 2) A functional interface for updating the repository. 470Sstevel@tonic-gate * 3) Convenience functions for common restarter tasks. 480Sstevel@tonic-gate * 490Sstevel@tonic-gate * Event protocol 500Sstevel@tonic-gate * We need a reliable event protocol, as there's no way to define 510Sstevel@tonic-gate * restarter events as idempotent. 520Sstevel@tonic-gate * 530Sstevel@tonic-gate * Currently using sysevent channels as the reliable event implementation. 540Sstevel@tonic-gate * This could change if the implementation proves unsuitable, but 550Sstevel@tonic-gate * the API defined here should abstract anything but a change in 560Sstevel@tonic-gate * the fundamental event model. 570Sstevel@tonic-gate * 580Sstevel@tonic-gate * We offer functions to tease apart the event rather than generic 590Sstevel@tonic-gate * nvpair interfaces. This is because each event type has a well- 600Sstevel@tonic-gate * defined set of fields. 610Sstevel@tonic-gate */ 620Sstevel@tonic-gate 630Sstevel@tonic-gate typedef struct restarter_event_handle restarter_event_handle_t; 640Sstevel@tonic-gate typedef struct restarter_event restarter_event_t; 650Sstevel@tonic-gate 660Sstevel@tonic-gate typedef uint32_t restarter_event_type_t; 670Sstevel@tonic-gate 680Sstevel@tonic-gate /* 690Sstevel@tonic-gate * Define an event protocol version. In theory, we could use this in 700Sstevel@tonic-gate * the future to support delegated restarters which use an older 710Sstevel@tonic-gate * protocol. In practice, increment RESTARTER_EVENT_VERSION whenever the 720Sstevel@tonic-gate * protocol might have changed. 730Sstevel@tonic-gate */ 740Sstevel@tonic-gate #define RESTARTER_EVENT_VERSION 4 750Sstevel@tonic-gate 760Sstevel@tonic-gate #define RESTARTER_FLAG_DEBUG 1 770Sstevel@tonic-gate 780Sstevel@tonic-gate /* 790Sstevel@tonic-gate * Event types 800Sstevel@tonic-gate * RESTARTER_EVENT_TYPE_ADD_INSTANCE 810Sstevel@tonic-gate * responsible for a new (stopped) instance 820Sstevel@tonic-gate * RESTARTER_EVENT_TYPE_REMOVE_INSTANCE 830Sstevel@tonic-gate * no longer responsible for this instance; stop it and return 840Sstevel@tonic-gate * RESTARTER_EVENT_TYPE_ENABLE 850Sstevel@tonic-gate * no guarantee that dependencies are met; see 860Sstevel@tonic-gate * RESTARTER_EVENT_TYPE_START 870Sstevel@tonic-gate * RESTARTER_EVENT_TYPE_DISABLE 880Sstevel@tonic-gate * no guarantee that instance was running 890Sstevel@tonic-gate * RESTARTER_EVENT_TYPE_ADMIN_DEGRADED 900Sstevel@tonic-gate * RESTARTER_EVENT_TYPE_ADMIN_REFRESH 910Sstevel@tonic-gate * RESTARTER_EVENT_TYPE_ADMIN_RESTART 920Sstevel@tonic-gate * RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF 930Sstevel@tonic-gate * RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON 940Sstevel@tonic-gate * RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON_IMMEDIATE 950Sstevel@tonic-gate * RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF 960Sstevel@tonic-gate * RESTARTER_EVENT_TYPE_STOP 970Sstevel@tonic-gate * dependencies are, or are becoming, unsatisfied 980Sstevel@tonic-gate * RESTARTER_EVENT_TYPE_START 990Sstevel@tonic-gate * dependencies have become satisfied 1000Sstevel@tonic-gate * RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE 1010Sstevel@tonic-gate * instance caused a dependency cycle 1020Sstevel@tonic-gate * RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY 1030Sstevel@tonic-gate * instance has an invalid dependency 1040Sstevel@tonic-gate */ 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate #define RESTARTER_EVENT_TYPE_INVALID 0 1070Sstevel@tonic-gate #define RESTARTER_EVENT_TYPE_ADD_INSTANCE 1 1080Sstevel@tonic-gate #define RESTARTER_EVENT_TYPE_REMOVE_INSTANCE 2 1090Sstevel@tonic-gate #define RESTARTER_EVENT_TYPE_ENABLE 3 1100Sstevel@tonic-gate #define RESTARTER_EVENT_TYPE_DISABLE 4 1110Sstevel@tonic-gate #define RESTARTER_EVENT_TYPE_ADMIN_DEGRADED 5 1120Sstevel@tonic-gate #define RESTARTER_EVENT_TYPE_ADMIN_REFRESH 6 1130Sstevel@tonic-gate #define RESTARTER_EVENT_TYPE_ADMIN_RESTART 7 1140Sstevel@tonic-gate #define RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF 8 1150Sstevel@tonic-gate #define RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON 9 1160Sstevel@tonic-gate #define RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON_IMMEDIATE 10 1170Sstevel@tonic-gate #define RESTARTER_EVENT_TYPE_STOP 11 1180Sstevel@tonic-gate #define RESTARTER_EVENT_TYPE_START 12 1190Sstevel@tonic-gate #define RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE 13 1200Sstevel@tonic-gate #define RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY 14 1210Sstevel@tonic-gate #define RESTARTER_EVENT_TYPE_ADMIN_DISABLE 15 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate #define RESTARTER_EVENT_ERROR -1 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate #define RESTARTER_EVENT_INSTANCE_DISABLED 0 1260Sstevel@tonic-gate #define RESTARTER_EVENT_INSTANCE_ENABLED 1 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate typedef enum { 1290Sstevel@tonic-gate RESTARTER_STATE_NONE, 1300Sstevel@tonic-gate RESTARTER_STATE_UNINIT, 1310Sstevel@tonic-gate RESTARTER_STATE_MAINT, 1320Sstevel@tonic-gate RESTARTER_STATE_OFFLINE, 1330Sstevel@tonic-gate RESTARTER_STATE_DISABLED, 1340Sstevel@tonic-gate RESTARTER_STATE_ONLINE, 1350Sstevel@tonic-gate RESTARTER_STATE_DEGRADED 1360Sstevel@tonic-gate } restarter_instance_state_t; 1370Sstevel@tonic-gate 1380Sstevel@tonic-gate /* 1390Sstevel@tonic-gate * These values are ordered by severity of required restart, as we use 1400Sstevel@tonic-gate * integer comparisons to determine error flow. 1410Sstevel@tonic-gate */ 1420Sstevel@tonic-gate typedef enum { 1430Sstevel@tonic-gate RERR_UNSUPPORTED = -1, 1440Sstevel@tonic-gate RERR_NONE = 0, /* no error, restart, refresh */ 1450Sstevel@tonic-gate RERR_FAULT, /* fault occurred */ 1460Sstevel@tonic-gate RERR_RESTART, /* transition due to restart */ 1470Sstevel@tonic-gate RERR_REFRESH /* transition due to refresh */ 1480Sstevel@tonic-gate } restarter_error_t; 1490Sstevel@tonic-gate 1500Sstevel@tonic-gate /* 1510Sstevel@tonic-gate * restarter_store_contract() and restarter_remove_contract() types 1520Sstevel@tonic-gate */ 1530Sstevel@tonic-gate typedef enum { 1540Sstevel@tonic-gate RESTARTER_CONTRACT_PRIMARY, 1550Sstevel@tonic-gate RESTARTER_CONTRACT_TRANSIENT 1560Sstevel@tonic-gate } restarter_contract_type_t; 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate /* 1590Sstevel@tonic-gate * restarter_bind_handle() registers a delegate with svc.startd to 1600Sstevel@tonic-gate * begin consuming events. 1610Sstevel@tonic-gate * 1620Sstevel@tonic-gate * On initial bind, the delgated restarter receives an event for each 1630Sstevel@tonic-gate * instance it is responsible for, as if that instance was new. 1640Sstevel@tonic-gate * 1650Sstevel@tonic-gate * callers must have superuser privileges 1660Sstevel@tonic-gate * 1670Sstevel@tonic-gate * The event handler can return 0 for success, or EAGAIN to request 1680Sstevel@tonic-gate * retry of event delivery. EAGAIN may be returned 3 times before the 1690Sstevel@tonic-gate * event is discarded. 1700Sstevel@tonic-gate */ 1710Sstevel@tonic-gate int restarter_bind_handle(uint32_t, const char *, 1720Sstevel@tonic-gate int (*event_handler)(restarter_event_t *), int, 1730Sstevel@tonic-gate restarter_event_handle_t **); 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate restarter_event_type_t restarter_event_get_type(restarter_event_t *); 1760Sstevel@tonic-gate uint64_t restarter_event_get_seq(restarter_event_t *); 1770Sstevel@tonic-gate void restarter_event_get_time(restarter_event_t *, hrtime_t *); 1780Sstevel@tonic-gate ssize_t restarter_event_get_instance(restarter_event_t *, char *, size_t); 1790Sstevel@tonic-gate restarter_event_handle_t *restarter_event_get_handle(restarter_event_t *); 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate /* 1820Sstevel@tonic-gate * The following functions work only on certain types of events. 1830Sstevel@tonic-gate * They fail with a return of -1 if they're called on an inappropriate event. 1840Sstevel@tonic-gate */ 1850Sstevel@tonic-gate int restarter_event_get_enabled(restarter_event_t *); 1860Sstevel@tonic-gate int restarter_event_get_current_states(restarter_event_t *, 1870Sstevel@tonic-gate restarter_instance_state_t *, restarter_instance_state_t *); 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate /* 1900Sstevel@tonic-gate * Functions for updating the repository. 1910Sstevel@tonic-gate */ 1920Sstevel@tonic-gate int restarter_set_states(restarter_event_handle_t *, const char *, 1930Sstevel@tonic-gate restarter_instance_state_t, restarter_instance_state_t, 1940Sstevel@tonic-gate restarter_instance_state_t, restarter_instance_state_t, restarter_error_t, 1950Sstevel@tonic-gate const char *); 196*6045Srm88369 int restarter_event_publish_retry(evchan_t *, const char *, const char *, 197*6045Srm88369 const char *, const char *, nvlist_t *, uint32_t); 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate int restarter_store_contract(scf_instance_t *, ctid_t, 2000Sstevel@tonic-gate restarter_contract_type_t); 2010Sstevel@tonic-gate int restarter_remove_contract(scf_instance_t *, ctid_t, 2020Sstevel@tonic-gate restarter_contract_type_t); 2030Sstevel@tonic-gate 2040Sstevel@tonic-gate ssize_t restarter_state_to_string(restarter_instance_state_t, char *, size_t); 2050Sstevel@tonic-gate restarter_instance_state_t restarter_string_to_state(char *); 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate #define RESTARTER_METHOD_CONTEXT_VERSION 6 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate struct method_context { 2100Sstevel@tonic-gate /* Stable */ 2110Sstevel@tonic-gate uid_t uid, euid; 2120Sstevel@tonic-gate gid_t gid, egid; 2130Sstevel@tonic-gate int ngroups; /* -1 means use initgroups(). */ 2140Sstevel@tonic-gate gid_t groups[NGROUPS_MAX-1]; 2150Sstevel@tonic-gate priv_set_t *lpriv_set, *priv_set; 2160Sstevel@tonic-gate char *corefile_pattern; /* Optional. */ 2170Sstevel@tonic-gate char *project; /* NULL for no change */ 2180Sstevel@tonic-gate char *resource_pool; /* NULL for project default */ 2190Sstevel@tonic-gate char *working_dir; /* NULL for :default */ 2200Sstevel@tonic-gate char **env; /* NULL for no env */ 2210Sstevel@tonic-gate size_t env_sz; /* size of env array */ 2220Sstevel@tonic-gate 2230Sstevel@tonic-gate /* Private */ 2240Sstevel@tonic-gate char *vbuf; 2250Sstevel@tonic-gate ssize_t vbuf_sz; 2260Sstevel@tonic-gate struct passwd pwd; 2270Sstevel@tonic-gate char *pwbuf; 2280Sstevel@tonic-gate ssize_t pwbufsz; 2290Sstevel@tonic-gate }; 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate int restarter_rm_libs_loadable(void); 2320Sstevel@tonic-gate /* instance, restarter name, method name, command line, structure pointer */ 2330Sstevel@tonic-gate const char *restarter_get_method_context(uint_t, scf_instance_t *, 2340Sstevel@tonic-gate scf_snapshot_t *, const char *, const char *, struct method_context **); 2350Sstevel@tonic-gate int restarter_set_method_context(struct method_context *, const char **); 2360Sstevel@tonic-gate void restarter_free_method_context(struct method_context *); 2370Sstevel@tonic-gate 2380Sstevel@tonic-gate 2390Sstevel@tonic-gate int restarter_is_null_method(const char *); 2400Sstevel@tonic-gate int restarter_is_kill_method(const char *); 2410Sstevel@tonic-gate int restarter_is_kill_proc_method(const char *); 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate #ifdef __cplusplus 2440Sstevel@tonic-gate } 2450Sstevel@tonic-gate #endif 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate #endif /* _LIBRESTART_H */ 248