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 52c65c8b0Srm88369 * Common Development and Distribution License (the "License"). 62c65c8b0Srm88369 * 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 /* 22eb1a3463STruong Nguyen * 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 #ifndef _LIBRESTART_H 277c478bd9Sstevel@tonic-gate #define _LIBRESTART_H 287c478bd9Sstevel@tonic-gate 292c65c8b0Srm88369 #include <libsysevent.h> 307c478bd9Sstevel@tonic-gate #include <libcontract.h> 317c478bd9Sstevel@tonic-gate #include <libscf.h> 327c478bd9Sstevel@tonic-gate #include <limits.h> 337c478bd9Sstevel@tonic-gate #include <priv.h> 347c478bd9Sstevel@tonic-gate #include <pwd.h> 357c478bd9Sstevel@tonic-gate #include <sys/types.h> 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate #ifdef __cplusplus 387c478bd9Sstevel@tonic-gate extern "C" { 397c478bd9Sstevel@tonic-gate #endif 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate /* 427c478bd9Sstevel@tonic-gate * There are 3 parts to librestart. 437c478bd9Sstevel@tonic-gate * 1) The event protocol from the master restarter to its delegates. 447c478bd9Sstevel@tonic-gate * 2) A functional interface for updating the repository. 457c478bd9Sstevel@tonic-gate * 3) Convenience functions for common restarter tasks. 467c478bd9Sstevel@tonic-gate * 477c478bd9Sstevel@tonic-gate * Event protocol 487c478bd9Sstevel@tonic-gate * We need a reliable event protocol, as there's no way to define 497c478bd9Sstevel@tonic-gate * restarter events as idempotent. 507c478bd9Sstevel@tonic-gate * 517c478bd9Sstevel@tonic-gate * Currently using sysevent channels as the reliable event implementation. 527c478bd9Sstevel@tonic-gate * This could change if the implementation proves unsuitable, but 537c478bd9Sstevel@tonic-gate * the API defined here should abstract anything but a change in 547c478bd9Sstevel@tonic-gate * the fundamental event model. 557c478bd9Sstevel@tonic-gate * 567c478bd9Sstevel@tonic-gate * We offer functions to tease apart the event rather than generic 577c478bd9Sstevel@tonic-gate * nvpair interfaces. This is because each event type has a well- 587c478bd9Sstevel@tonic-gate * defined set of fields. 597c478bd9Sstevel@tonic-gate */ 607c478bd9Sstevel@tonic-gate 61eb1a3463STruong Nguyen /* 62eb1a3463STruong Nguyen * Some of the functions have external contracted consumers, review contracts 63eb1a3463STruong Nguyen * when making incompatible changes. 64eb1a3463STruong Nguyen */ 65eb1a3463STruong Nguyen 667c478bd9Sstevel@tonic-gate typedef struct restarter_event_handle restarter_event_handle_t; 677c478bd9Sstevel@tonic-gate typedef struct restarter_event restarter_event_t; 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate typedef uint32_t restarter_event_type_t; 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate /* 727c478bd9Sstevel@tonic-gate * Define an event protocol version. In theory, we could use this in 737c478bd9Sstevel@tonic-gate * the future to support delegated restarters which use an older 747c478bd9Sstevel@tonic-gate * protocol. In practice, increment RESTARTER_EVENT_VERSION whenever the 757c478bd9Sstevel@tonic-gate * protocol might have changed. 767c478bd9Sstevel@tonic-gate */ 777c478bd9Sstevel@tonic-gate #define RESTARTER_EVENT_VERSION 4 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate #define RESTARTER_FLAG_DEBUG 1 807c478bd9Sstevel@tonic-gate 81*870ad75aSSean Wilcox #define RESTARTER_ERRMSGSZ 1024 82*870ad75aSSean Wilcox 837c478bd9Sstevel@tonic-gate /* 847c478bd9Sstevel@tonic-gate * Event types 857c478bd9Sstevel@tonic-gate * RESTARTER_EVENT_TYPE_ADD_INSTANCE 867c478bd9Sstevel@tonic-gate * responsible for a new (stopped) instance 877c478bd9Sstevel@tonic-gate * RESTARTER_EVENT_TYPE_REMOVE_INSTANCE 887c478bd9Sstevel@tonic-gate * no longer responsible for this instance; stop it and return 897c478bd9Sstevel@tonic-gate * RESTARTER_EVENT_TYPE_ENABLE 907c478bd9Sstevel@tonic-gate * no guarantee that dependencies are met; see 917c478bd9Sstevel@tonic-gate * RESTARTER_EVENT_TYPE_START 927c478bd9Sstevel@tonic-gate * RESTARTER_EVENT_TYPE_DISABLE 937c478bd9Sstevel@tonic-gate * no guarantee that instance was running 947c478bd9Sstevel@tonic-gate * RESTARTER_EVENT_TYPE_ADMIN_DEGRADED 957c478bd9Sstevel@tonic-gate * RESTARTER_EVENT_TYPE_ADMIN_REFRESH 967c478bd9Sstevel@tonic-gate * RESTARTER_EVENT_TYPE_ADMIN_RESTART 977c478bd9Sstevel@tonic-gate * RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF 987c478bd9Sstevel@tonic-gate * RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON 997c478bd9Sstevel@tonic-gate * RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON_IMMEDIATE 1007c478bd9Sstevel@tonic-gate * RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF 1017c478bd9Sstevel@tonic-gate * RESTARTER_EVENT_TYPE_STOP 1027c478bd9Sstevel@tonic-gate * dependencies are, or are becoming, unsatisfied 1037c478bd9Sstevel@tonic-gate * RESTARTER_EVENT_TYPE_START 1047c478bd9Sstevel@tonic-gate * dependencies have become satisfied 1057c478bd9Sstevel@tonic-gate * RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE 1067c478bd9Sstevel@tonic-gate * instance caused a dependency cycle 1077c478bd9Sstevel@tonic-gate * RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY 1087c478bd9Sstevel@tonic-gate * instance has an invalid dependency 1097c478bd9Sstevel@tonic-gate */ 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate #define RESTARTER_EVENT_TYPE_INVALID 0 1127c478bd9Sstevel@tonic-gate #define RESTARTER_EVENT_TYPE_ADD_INSTANCE 1 1137c478bd9Sstevel@tonic-gate #define RESTARTER_EVENT_TYPE_REMOVE_INSTANCE 2 1147c478bd9Sstevel@tonic-gate #define RESTARTER_EVENT_TYPE_ENABLE 3 1157c478bd9Sstevel@tonic-gate #define RESTARTER_EVENT_TYPE_DISABLE 4 1167c478bd9Sstevel@tonic-gate #define RESTARTER_EVENT_TYPE_ADMIN_DEGRADED 5 1177c478bd9Sstevel@tonic-gate #define RESTARTER_EVENT_TYPE_ADMIN_REFRESH 6 1187c478bd9Sstevel@tonic-gate #define RESTARTER_EVENT_TYPE_ADMIN_RESTART 7 1197c478bd9Sstevel@tonic-gate #define RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF 8 1207c478bd9Sstevel@tonic-gate #define RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON 9 1217c478bd9Sstevel@tonic-gate #define RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON_IMMEDIATE 10 1227c478bd9Sstevel@tonic-gate #define RESTARTER_EVENT_TYPE_STOP 11 1237c478bd9Sstevel@tonic-gate #define RESTARTER_EVENT_TYPE_START 12 1247c478bd9Sstevel@tonic-gate #define RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE 13 1257c478bd9Sstevel@tonic-gate #define RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY 14 1267c478bd9Sstevel@tonic-gate #define RESTARTER_EVENT_TYPE_ADMIN_DISABLE 15 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate #define RESTARTER_EVENT_ERROR -1 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate #define RESTARTER_EVENT_INSTANCE_DISABLED 0 1317c478bd9Sstevel@tonic-gate #define RESTARTER_EVENT_INSTANCE_ENABLED 1 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate typedef enum { 1347c478bd9Sstevel@tonic-gate RESTARTER_STATE_NONE, 1357c478bd9Sstevel@tonic-gate RESTARTER_STATE_UNINIT, 1367c478bd9Sstevel@tonic-gate RESTARTER_STATE_MAINT, 1377c478bd9Sstevel@tonic-gate RESTARTER_STATE_OFFLINE, 1387c478bd9Sstevel@tonic-gate RESTARTER_STATE_DISABLED, 1397c478bd9Sstevel@tonic-gate RESTARTER_STATE_ONLINE, 1407c478bd9Sstevel@tonic-gate RESTARTER_STATE_DEGRADED 1417c478bd9Sstevel@tonic-gate } restarter_instance_state_t; 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate /* 1447c478bd9Sstevel@tonic-gate * These values are ordered by severity of required restart, as we use 1457c478bd9Sstevel@tonic-gate * integer comparisons to determine error flow. 1467c478bd9Sstevel@tonic-gate */ 1477c478bd9Sstevel@tonic-gate typedef enum { 1487c478bd9Sstevel@tonic-gate RERR_UNSUPPORTED = -1, 1497c478bd9Sstevel@tonic-gate RERR_NONE = 0, /* no error, restart, refresh */ 1507c478bd9Sstevel@tonic-gate RERR_FAULT, /* fault occurred */ 1517c478bd9Sstevel@tonic-gate RERR_RESTART, /* transition due to restart */ 1527c478bd9Sstevel@tonic-gate RERR_REFRESH /* transition due to refresh */ 1537c478bd9Sstevel@tonic-gate } restarter_error_t; 1547c478bd9Sstevel@tonic-gate /* 1557c478bd9Sstevel@tonic-gate * restarter_store_contract() and restarter_remove_contract() types 1567c478bd9Sstevel@tonic-gate */ 1577c478bd9Sstevel@tonic-gate typedef enum { 1587c478bd9Sstevel@tonic-gate RESTARTER_CONTRACT_PRIMARY, 1597c478bd9Sstevel@tonic-gate RESTARTER_CONTRACT_TRANSIENT 1607c478bd9Sstevel@tonic-gate } restarter_contract_type_t; 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate /* 1637c478bd9Sstevel@tonic-gate * restarter_bind_handle() registers a delegate with svc.startd to 1647c478bd9Sstevel@tonic-gate * begin consuming events. 1657c478bd9Sstevel@tonic-gate * 1667c478bd9Sstevel@tonic-gate * On initial bind, the delgated restarter receives an event for each 1677c478bd9Sstevel@tonic-gate * instance it is responsible for, as if that instance was new. 1687c478bd9Sstevel@tonic-gate * 1697c478bd9Sstevel@tonic-gate * callers must have superuser privileges 1707c478bd9Sstevel@tonic-gate * 1717c478bd9Sstevel@tonic-gate * The event handler can return 0 for success, or EAGAIN to request 1727c478bd9Sstevel@tonic-gate * retry of event delivery. EAGAIN may be returned 3 times before the 1737c478bd9Sstevel@tonic-gate * event is discarded. 1747c478bd9Sstevel@tonic-gate */ 1757c478bd9Sstevel@tonic-gate int restarter_bind_handle(uint32_t, const char *, 1767c478bd9Sstevel@tonic-gate int (*event_handler)(restarter_event_t *), int, 1777c478bd9Sstevel@tonic-gate restarter_event_handle_t **); 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate restarter_event_type_t restarter_event_get_type(restarter_event_t *); 1807c478bd9Sstevel@tonic-gate uint64_t restarter_event_get_seq(restarter_event_t *); 1817c478bd9Sstevel@tonic-gate void restarter_event_get_time(restarter_event_t *, hrtime_t *); 1827c478bd9Sstevel@tonic-gate ssize_t restarter_event_get_instance(restarter_event_t *, char *, size_t); 1837c478bd9Sstevel@tonic-gate restarter_event_handle_t *restarter_event_get_handle(restarter_event_t *); 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate /* 1867c478bd9Sstevel@tonic-gate * The following functions work only on certain types of events. 1877c478bd9Sstevel@tonic-gate * They fail with a return of -1 if they're called on an inappropriate event. 1887c478bd9Sstevel@tonic-gate */ 1897c478bd9Sstevel@tonic-gate int restarter_event_get_enabled(restarter_event_t *); 1907c478bd9Sstevel@tonic-gate int restarter_event_get_current_states(restarter_event_t *, 1917c478bd9Sstevel@tonic-gate restarter_instance_state_t *, restarter_instance_state_t *); 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate /* 1947c478bd9Sstevel@tonic-gate * Functions for updating the repository. 1957c478bd9Sstevel@tonic-gate */ 196eb1a3463STruong Nguyen 197eb1a3463STruong Nguyen /* 198eb1a3463STruong Nguyen * When setting state to "maintenance", callers of restarter_set_states() can 199eb1a3463STruong Nguyen * set aux_state to "service_request" to communicate that another service has 200eb1a3463STruong Nguyen * requested maintenance state for the target service. 201eb1a3463STruong Nguyen * 202eb1a3463STruong Nguyen * Callers should use restarter_inst_validate_aux_fmri() to validate the fmri 203eb1a3463STruong Nguyen * of the requested service and pass "service_request" for aux_state when 204eb1a3463STruong Nguyen * calling restarter_set_states(). See inetd and startd for examples. 205eb1a3463STruong Nguyen */ 2067c478bd9Sstevel@tonic-gate int restarter_set_states(restarter_event_handle_t *, const char *, 2077c478bd9Sstevel@tonic-gate restarter_instance_state_t, restarter_instance_state_t, 2087c478bd9Sstevel@tonic-gate restarter_instance_state_t, restarter_instance_state_t, restarter_error_t, 2097c478bd9Sstevel@tonic-gate const char *); 2102c65c8b0Srm88369 int restarter_event_publish_retry(evchan_t *, const char *, const char *, 2112c65c8b0Srm88369 const char *, const char *, nvlist_t *, uint32_t); 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate int restarter_store_contract(scf_instance_t *, ctid_t, 2147c478bd9Sstevel@tonic-gate restarter_contract_type_t); 2157c478bd9Sstevel@tonic-gate int restarter_remove_contract(scf_instance_t *, ctid_t, 2167c478bd9Sstevel@tonic-gate restarter_contract_type_t); 2177c478bd9Sstevel@tonic-gate 2187c478bd9Sstevel@tonic-gate ssize_t restarter_state_to_string(restarter_instance_state_t, char *, size_t); 2197c478bd9Sstevel@tonic-gate restarter_instance_state_t restarter_string_to_state(char *); 2207c478bd9Sstevel@tonic-gate 221*870ad75aSSean Wilcox #define RESTARTER_METHOD_CONTEXT_VERSION 7 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate struct method_context { 2247c478bd9Sstevel@tonic-gate /* Stable */ 2257c478bd9Sstevel@tonic-gate uid_t uid, euid; 2267c478bd9Sstevel@tonic-gate gid_t gid, egid; 2277c478bd9Sstevel@tonic-gate int ngroups; /* -1 means use initgroups(). */ 22813d8aaa1SSean Wilcox gid_t groups[NGROUPS_MAX]; 2297c478bd9Sstevel@tonic-gate priv_set_t *lpriv_set, *priv_set; 2307c478bd9Sstevel@tonic-gate char *corefile_pattern; /* Optional. */ 2317c478bd9Sstevel@tonic-gate char *project; /* NULL for no change */ 2327c478bd9Sstevel@tonic-gate char *resource_pool; /* NULL for project default */ 2337c478bd9Sstevel@tonic-gate char *working_dir; /* NULL for :default */ 2347c478bd9Sstevel@tonic-gate char **env; /* NULL for no env */ 2357c478bd9Sstevel@tonic-gate size_t env_sz; /* size of env array */ 2367c478bd9Sstevel@tonic-gate 2377c478bd9Sstevel@tonic-gate /* Private */ 2387c478bd9Sstevel@tonic-gate char *vbuf; 2397c478bd9Sstevel@tonic-gate ssize_t vbuf_sz; 2407c478bd9Sstevel@tonic-gate struct passwd pwd; 2417c478bd9Sstevel@tonic-gate char *pwbuf; 2427c478bd9Sstevel@tonic-gate ssize_t pwbufsz; 2437c478bd9Sstevel@tonic-gate }; 2447c478bd9Sstevel@tonic-gate 245*870ad75aSSean Wilcox /* 246*870ad75aSSean Wilcox * An error structure that contains a message string, and a type 247*870ad75aSSean Wilcox * that can be used to determine course of action by the reciever 248*870ad75aSSean Wilcox * of the error structure. 249*870ad75aSSean Wilcox * 250*870ad75aSSean Wilcox * type - usually will be an errno equivalent but could contain 251*870ad75aSSean Wilcox * defined error types for exampe SCF_ERROR_XXX 252*870ad75aSSean Wilcox * msg - must be at the end of the structure as if the message is 253*870ad75aSSean Wilcox * longer than EMSGSIZE we will reallocate the structure to 254*870ad75aSSean Wilcox * handle the overflow 255*870ad75aSSean Wilcox */ 256*870ad75aSSean Wilcox typedef struct mc_error { 257*870ad75aSSean Wilcox int destroy; /* Flag to indicate destruction steps */ 258*870ad75aSSean Wilcox int type; /* Type of error for decision making */ 259*870ad75aSSean Wilcox int size; /* The size of the error message string */ 260*870ad75aSSean Wilcox char msg[RESTARTER_ERRMSGSZ]; 261*870ad75aSSean Wilcox } mc_error_t; 262*870ad75aSSean Wilcox 2637c478bd9Sstevel@tonic-gate int restarter_rm_libs_loadable(void); 2647c478bd9Sstevel@tonic-gate /* instance, restarter name, method name, command line, structure pointer */ 265*870ad75aSSean Wilcox mc_error_t *restarter_get_method_context(uint_t, scf_instance_t *, 2667c478bd9Sstevel@tonic-gate scf_snapshot_t *, const char *, const char *, struct method_context **); 267*870ad75aSSean Wilcox void restarter_mc_error_destroy(mc_error_t *); 2687c478bd9Sstevel@tonic-gate int restarter_set_method_context(struct method_context *, const char **); 2697c478bd9Sstevel@tonic-gate void restarter_free_method_context(struct method_context *); 2707c478bd9Sstevel@tonic-gate 2717c478bd9Sstevel@tonic-gate 2727c478bd9Sstevel@tonic-gate int restarter_is_null_method(const char *); 2737c478bd9Sstevel@tonic-gate int restarter_is_kill_method(const char *); 2747c478bd9Sstevel@tonic-gate int restarter_is_kill_proc_method(const char *); 2757c478bd9Sstevel@tonic-gate 276eb1a3463STruong Nguyen /* Validate the inst fmri specified in restarter_actions/auxiliary_fmri */ 277eb1a3463STruong Nguyen int restarter_inst_validate_ractions_aux_fmri(scf_instance_t *); 278eb1a3463STruong Nguyen 279eb1a3463STruong Nguyen /* Delete instance's restarter_actions/auxiliary_fmri property */ 280eb1a3463STruong Nguyen int restarter_inst_reset_ractions_aux_fmri(scf_instance_t *); 281eb1a3463STruong Nguyen 282eb1a3463STruong Nguyen /* Get boolean value from instance's restarter_actions/auxiliary_tty */ 283eb1a3463STruong Nguyen int restarter_inst_ractions_from_tty(scf_instance_t *); 284eb1a3463STruong Nguyen 285eb1a3463STruong Nguyen /* Delete instance's restarter/auxiliary_fmri property */ 286eb1a3463STruong Nguyen int restarter_inst_reset_aux_fmri(scf_instance_t *); 287eb1a3463STruong Nguyen 288eb1a3463STruong Nguyen /* 289eb1a3463STruong Nguyen * Set instance's restarter/auxiliary_fmri, value come from 290eb1a3463STruong Nguyen * restarter_actions/auxliary_fmri 291eb1a3463STruong Nguyen */ 292eb1a3463STruong Nguyen int restarter_inst_set_aux_fmri(scf_instance_t *); 293eb1a3463STruong Nguyen 2947c478bd9Sstevel@tonic-gate #ifdef __cplusplus 2957c478bd9Sstevel@tonic-gate } 2967c478bd9Sstevel@tonic-gate #endif 2977c478bd9Sstevel@tonic-gate 2987c478bd9Sstevel@tonic-gate #endif /* _LIBRESTART_H */ 299