xref: /onnv-gate/usr/src/lib/librestart/common/librestart.h (revision 11482:7315713fb22c)
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
56045Srm88369  * Common Development and Distribution License (the "License").
66045Srm88369  * 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*11482SSean.Wilcox@Sun.COM  * Copyright 2010 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 
296045Srm88369 #include <libsysevent.h>
300Sstevel@tonic-gate #include <libcontract.h>
310Sstevel@tonic-gate #include <libscf.h>
320Sstevel@tonic-gate #include <limits.h>
330Sstevel@tonic-gate #include <priv.h>
340Sstevel@tonic-gate #include <pwd.h>
350Sstevel@tonic-gate #include <sys/types.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #ifdef	__cplusplus
380Sstevel@tonic-gate extern "C" {
390Sstevel@tonic-gate #endif
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /*
420Sstevel@tonic-gate  * There are 3 parts to librestart.
430Sstevel@tonic-gate  *	1) The event protocol from the master restarter to its delegates.
440Sstevel@tonic-gate  *	2) A functional interface for updating the repository.
450Sstevel@tonic-gate  *	3) Convenience functions for common restarter tasks.
460Sstevel@tonic-gate  *
470Sstevel@tonic-gate  * Event protocol
480Sstevel@tonic-gate  *	We need a reliable event protocol, as there's no way to define
490Sstevel@tonic-gate  *	restarter events as idempotent.
500Sstevel@tonic-gate  *
510Sstevel@tonic-gate  *	Currently using sysevent channels as the reliable event implementation.
520Sstevel@tonic-gate  *	This could change if the implementation proves unsuitable, but
530Sstevel@tonic-gate  *	the API defined here should abstract anything but a change in
540Sstevel@tonic-gate  *	the fundamental event model.
550Sstevel@tonic-gate  *
560Sstevel@tonic-gate  *	We offer functions to tease apart the event rather than generic
570Sstevel@tonic-gate  *	nvpair interfaces. This is because each event type has a well-
580Sstevel@tonic-gate  *	defined set of fields.
590Sstevel@tonic-gate  */
600Sstevel@tonic-gate 
618823STruong.Q.Nguyen@Sun.COM /*
628823STruong.Q.Nguyen@Sun.COM  * Some of the functions have external contracted consumers, review contracts
638823STruong.Q.Nguyen@Sun.COM  * when making incompatible changes.
648823STruong.Q.Nguyen@Sun.COM  */
658823STruong.Q.Nguyen@Sun.COM 
660Sstevel@tonic-gate typedef struct restarter_event_handle restarter_event_handle_t;
670Sstevel@tonic-gate typedef struct restarter_event restarter_event_t;
680Sstevel@tonic-gate 
690Sstevel@tonic-gate typedef uint32_t restarter_event_type_t;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate /*
720Sstevel@tonic-gate  * Define an event protocol version. In theory, we could use this in
730Sstevel@tonic-gate  * the future to support delegated restarters which use an older
740Sstevel@tonic-gate  * protocol. In practice, increment RESTARTER_EVENT_VERSION whenever the
750Sstevel@tonic-gate  * protocol might have changed.
760Sstevel@tonic-gate  */
770Sstevel@tonic-gate #define	RESTARTER_EVENT_VERSION		4
780Sstevel@tonic-gate 
790Sstevel@tonic-gate #define	RESTARTER_FLAG_DEBUG		1
800Sstevel@tonic-gate 
819765SSean.Wilcox@Sun.COM #define	RESTARTER_ERRMSGSZ		1024
829765SSean.Wilcox@Sun.COM 
830Sstevel@tonic-gate /*
840Sstevel@tonic-gate  * Event types
850Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ADD_INSTANCE
860Sstevel@tonic-gate  *		responsible for a new (stopped) instance
870Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_REMOVE_INSTANCE
880Sstevel@tonic-gate  *		no longer responsible for this instance; stop it and return
890Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ENABLE
900Sstevel@tonic-gate  *		no guarantee that dependencies are met; see
910Sstevel@tonic-gate  *		RESTARTER_EVENT_TYPE_START
920Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_DISABLE
930Sstevel@tonic-gate  *		no guarantee that instance was running
940Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ADMIN_DEGRADED
950Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ADMIN_REFRESH
960Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ADMIN_RESTART
970Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF
980Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON
990Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON_IMMEDIATE
1000Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF
1010Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_STOP
1020Sstevel@tonic-gate  *		dependencies are, or are becoming, unsatisfied
1030Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_START
1040Sstevel@tonic-gate  *		dependencies have become satisfied
1050Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE
1060Sstevel@tonic-gate  *		instance caused a dependency cycle
1070Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY
1080Sstevel@tonic-gate  *		instance has an invalid dependency
1090Sstevel@tonic-gate  */
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_INVALID			0
1120Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ADD_INSTANCE		1
1130Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_REMOVE_INSTANCE		2
1140Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ENABLE			3
1150Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_DISABLE			4
1160Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ADMIN_DEGRADED		5
1170Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ADMIN_REFRESH		6
1180Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ADMIN_RESTART		7
1190Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF		8
1200Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON		9
1210Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON_IMMEDIATE	10
1220Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_STOP			11
1230Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_START			12
1240Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE		13
1250Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY		14
1260Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ADMIN_DISABLE		15
127*11482SSean.Wilcox@Sun.COM #define	RESTARTER_EVENT_TYPE_STOP_RESET			16
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate #define	RESTARTER_EVENT_ERROR			-1
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate #define	RESTARTER_EVENT_INSTANCE_DISABLED	0
1320Sstevel@tonic-gate #define	RESTARTER_EVENT_INSTANCE_ENABLED	1
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate typedef enum {
1350Sstevel@tonic-gate 	RESTARTER_STATE_NONE,
1360Sstevel@tonic-gate 	RESTARTER_STATE_UNINIT,
1370Sstevel@tonic-gate 	RESTARTER_STATE_MAINT,
1380Sstevel@tonic-gate 	RESTARTER_STATE_OFFLINE,
1390Sstevel@tonic-gate 	RESTARTER_STATE_DISABLED,
1400Sstevel@tonic-gate 	RESTARTER_STATE_ONLINE,
1410Sstevel@tonic-gate 	RESTARTER_STATE_DEGRADED
1420Sstevel@tonic-gate } restarter_instance_state_t;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate /*
1450Sstevel@tonic-gate  * These values are ordered by severity of required restart, as we use
1460Sstevel@tonic-gate  * integer comparisons to determine error flow.
1470Sstevel@tonic-gate  */
1480Sstevel@tonic-gate typedef enum {
1490Sstevel@tonic-gate 	RERR_UNSUPPORTED = -1,
1500Sstevel@tonic-gate 	RERR_NONE = 0,			/* no error, restart, refresh */
1510Sstevel@tonic-gate 	RERR_FAULT,			/* fault occurred */
1520Sstevel@tonic-gate 	RERR_RESTART,			/* transition due to restart */
1530Sstevel@tonic-gate 	RERR_REFRESH			/* transition due to refresh */
1540Sstevel@tonic-gate } restarter_error_t;
1550Sstevel@tonic-gate /*
1560Sstevel@tonic-gate  * restarter_store_contract() and restarter_remove_contract() types
1570Sstevel@tonic-gate  */
1580Sstevel@tonic-gate typedef enum {
1590Sstevel@tonic-gate 	RESTARTER_CONTRACT_PRIMARY,
1600Sstevel@tonic-gate 	RESTARTER_CONTRACT_TRANSIENT
1610Sstevel@tonic-gate } restarter_contract_type_t;
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate /*
1640Sstevel@tonic-gate  * restarter_bind_handle() registers a delegate with svc.startd to
1650Sstevel@tonic-gate  * begin consuming events.
1660Sstevel@tonic-gate  *
1670Sstevel@tonic-gate  * On initial bind, the delgated restarter receives an event for each
1680Sstevel@tonic-gate  * instance it is responsible for, as if that instance was new.
1690Sstevel@tonic-gate  *
1700Sstevel@tonic-gate  * callers must have superuser privileges
1710Sstevel@tonic-gate  *
1720Sstevel@tonic-gate  * The event handler can return 0 for success, or EAGAIN to request
1730Sstevel@tonic-gate  * retry of event delivery. EAGAIN may be returned 3 times before the
1740Sstevel@tonic-gate  * event is discarded.
1750Sstevel@tonic-gate  */
1760Sstevel@tonic-gate int restarter_bind_handle(uint32_t, const char *,
1770Sstevel@tonic-gate     int (*event_handler)(restarter_event_t *), int,
1780Sstevel@tonic-gate     restarter_event_handle_t **);
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate restarter_event_type_t restarter_event_get_type(restarter_event_t *);
1810Sstevel@tonic-gate uint64_t restarter_event_get_seq(restarter_event_t *);
1820Sstevel@tonic-gate void restarter_event_get_time(restarter_event_t *, hrtime_t *);
1830Sstevel@tonic-gate ssize_t restarter_event_get_instance(restarter_event_t *, char *, size_t);
1840Sstevel@tonic-gate restarter_event_handle_t *restarter_event_get_handle(restarter_event_t *);
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate /*
1870Sstevel@tonic-gate  * The following functions work only on certain types of events.
1880Sstevel@tonic-gate  * They fail with a return of -1 if they're called on an inappropriate event.
1890Sstevel@tonic-gate  */
1900Sstevel@tonic-gate int restarter_event_get_enabled(restarter_event_t *);
1910Sstevel@tonic-gate int restarter_event_get_current_states(restarter_event_t *,
1920Sstevel@tonic-gate     restarter_instance_state_t *, restarter_instance_state_t *);
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate /*
1950Sstevel@tonic-gate  * Functions for updating the repository.
1960Sstevel@tonic-gate  */
1978823STruong.Q.Nguyen@Sun.COM 
1988823STruong.Q.Nguyen@Sun.COM /*
1998823STruong.Q.Nguyen@Sun.COM  * When setting state to "maintenance", callers of restarter_set_states() can
2008823STruong.Q.Nguyen@Sun.COM  * set aux_state to "service_request" to communicate that another service has
2018823STruong.Q.Nguyen@Sun.COM  * requested maintenance state for the target service.
2028823STruong.Q.Nguyen@Sun.COM  *
2038823STruong.Q.Nguyen@Sun.COM  * Callers should use restarter_inst_validate_aux_fmri() to validate the fmri
2048823STruong.Q.Nguyen@Sun.COM  * of the requested service and pass "service_request" for aux_state when
2058823STruong.Q.Nguyen@Sun.COM  * calling restarter_set_states(). See inetd and startd for examples.
2068823STruong.Q.Nguyen@Sun.COM  */
2070Sstevel@tonic-gate int restarter_set_states(restarter_event_handle_t *, const char *,
2080Sstevel@tonic-gate     restarter_instance_state_t, restarter_instance_state_t,
2090Sstevel@tonic-gate     restarter_instance_state_t, restarter_instance_state_t, restarter_error_t,
2100Sstevel@tonic-gate     const char *);
2116045Srm88369 int restarter_event_publish_retry(evchan_t *, const char *, const char *,
2126045Srm88369     const char *, const char *, nvlist_t *, uint32_t);
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate int restarter_store_contract(scf_instance_t *, ctid_t,
2150Sstevel@tonic-gate     restarter_contract_type_t);
2160Sstevel@tonic-gate int restarter_remove_contract(scf_instance_t *, ctid_t,
2170Sstevel@tonic-gate     restarter_contract_type_t);
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate ssize_t restarter_state_to_string(restarter_instance_state_t, char *, size_t);
2200Sstevel@tonic-gate restarter_instance_state_t restarter_string_to_state(char *);
2210Sstevel@tonic-gate 
2229765SSean.Wilcox@Sun.COM #define	RESTARTER_METHOD_CONTEXT_VERSION	7
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate struct method_context {
2250Sstevel@tonic-gate 	/* Stable */
2260Sstevel@tonic-gate 	uid_t		uid, euid;
2270Sstevel@tonic-gate 	gid_t		gid, egid;
2280Sstevel@tonic-gate 	int		ngroups;		/* -1 means use initgroups(). */
2299263SSean.Wilcox@Sun.COM 	gid_t		groups[NGROUPS_MAX];
2300Sstevel@tonic-gate 	priv_set_t	*lpriv_set, *priv_set;
2310Sstevel@tonic-gate 	char		*corefile_pattern;	/* Optional. */
2320Sstevel@tonic-gate 	char		*project;		/* NULL for no change */
2330Sstevel@tonic-gate 	char		*resource_pool;		/* NULL for project default */
2340Sstevel@tonic-gate 	char		*working_dir;		/* NULL for :default */
2350Sstevel@tonic-gate 	char		**env;			/* NULL for no env */
2360Sstevel@tonic-gate 	size_t		env_sz;			/* size of env array */
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	/* Private */
2390Sstevel@tonic-gate 	char		*vbuf;
2400Sstevel@tonic-gate 	ssize_t		vbuf_sz;
2410Sstevel@tonic-gate 	struct passwd	pwd;
2420Sstevel@tonic-gate 	char		*pwbuf;
2430Sstevel@tonic-gate 	ssize_t		pwbufsz;
2440Sstevel@tonic-gate };
2450Sstevel@tonic-gate 
2469765SSean.Wilcox@Sun.COM /*
2479765SSean.Wilcox@Sun.COM  * An error structure that contains a message string, and a type
2489765SSean.Wilcox@Sun.COM  * that can be used to determine course of action by the reciever
2499765SSean.Wilcox@Sun.COM  * of the error structure.
2509765SSean.Wilcox@Sun.COM  *
2519765SSean.Wilcox@Sun.COM  * type - usually will be an errno equivalent but could contain
2529765SSean.Wilcox@Sun.COM  * 	defined error types for exampe SCF_ERROR_XXX
2539765SSean.Wilcox@Sun.COM  * msg - must be at the end of the structure as if the message is
2549765SSean.Wilcox@Sun.COM  * 	longer than EMSGSIZE we will reallocate the structure to
2559765SSean.Wilcox@Sun.COM  * 	handle the overflow
2569765SSean.Wilcox@Sun.COM  */
2579765SSean.Wilcox@Sun.COM typedef struct mc_error {
2589765SSean.Wilcox@Sun.COM 	int	destroy;	/* Flag to indicate destruction steps */
2599765SSean.Wilcox@Sun.COM 	int	type;		/* Type of error for decision making */
2609765SSean.Wilcox@Sun.COM 	int	size;		/* The size of the error message string */
2619765SSean.Wilcox@Sun.COM 	char 	msg[RESTARTER_ERRMSGSZ];
2629765SSean.Wilcox@Sun.COM } mc_error_t;
2639765SSean.Wilcox@Sun.COM 
2640Sstevel@tonic-gate int restarter_rm_libs_loadable(void);
2650Sstevel@tonic-gate /* instance, restarter name, method name, command line, structure pointer */
2669765SSean.Wilcox@Sun.COM mc_error_t *restarter_get_method_context(uint_t, scf_instance_t *,
2670Sstevel@tonic-gate     scf_snapshot_t *, const char *, const char *, struct method_context **);
2689765SSean.Wilcox@Sun.COM void restarter_mc_error_destroy(mc_error_t *);
2690Sstevel@tonic-gate int restarter_set_method_context(struct method_context *, const char **);
2700Sstevel@tonic-gate void restarter_free_method_context(struct method_context *);
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate int restarter_is_null_method(const char *);
2740Sstevel@tonic-gate int restarter_is_kill_method(const char *);
2750Sstevel@tonic-gate int restarter_is_kill_proc_method(const char *);
2760Sstevel@tonic-gate 
2778823STruong.Q.Nguyen@Sun.COM /* Validate the inst fmri specified in  restarter_actions/auxiliary_fmri */
2788823STruong.Q.Nguyen@Sun.COM int restarter_inst_validate_ractions_aux_fmri(scf_instance_t *);
2798823STruong.Q.Nguyen@Sun.COM 
2808823STruong.Q.Nguyen@Sun.COM /* Delete instance's restarter_actions/auxiliary_fmri property */
2818823STruong.Q.Nguyen@Sun.COM int restarter_inst_reset_ractions_aux_fmri(scf_instance_t *);
2828823STruong.Q.Nguyen@Sun.COM 
2838823STruong.Q.Nguyen@Sun.COM /* Get boolean value from instance's restarter_actions/auxiliary_tty */
2848823STruong.Q.Nguyen@Sun.COM int restarter_inst_ractions_from_tty(scf_instance_t *);
2858823STruong.Q.Nguyen@Sun.COM 
2868823STruong.Q.Nguyen@Sun.COM /* Delete instance's restarter/auxiliary_fmri property */
2878823STruong.Q.Nguyen@Sun.COM int restarter_inst_reset_aux_fmri(scf_instance_t *);
2888823STruong.Q.Nguyen@Sun.COM 
2898823STruong.Q.Nguyen@Sun.COM /*
2908823STruong.Q.Nguyen@Sun.COM  * Set instance's restarter/auxiliary_fmri, value come from
2918823STruong.Q.Nguyen@Sun.COM  * restarter_actions/auxliary_fmri
2928823STruong.Q.Nguyen@Sun.COM  */
2938823STruong.Q.Nguyen@Sun.COM int restarter_inst_set_aux_fmri(scf_instance_t *);
2948823STruong.Q.Nguyen@Sun.COM 
2950Sstevel@tonic-gate #ifdef	__cplusplus
2960Sstevel@tonic-gate }
2970Sstevel@tonic-gate #endif
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate #endif	/* _LIBRESTART_H */
300