xref: /onnv-gate/usr/src/lib/librestart/common/librestart.h (revision 9263:48d14e1f550f)
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 /*
228823STruong.Q.Nguyen@Sun.COM  * Copyright 2009 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 
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate  * Event types
830Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ADD_INSTANCE
840Sstevel@tonic-gate  *		responsible for a new (stopped) instance
850Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_REMOVE_INSTANCE
860Sstevel@tonic-gate  *		no longer responsible for this instance; stop it and return
870Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ENABLE
880Sstevel@tonic-gate  *		no guarantee that dependencies are met; see
890Sstevel@tonic-gate  *		RESTARTER_EVENT_TYPE_START
900Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_DISABLE
910Sstevel@tonic-gate  *		no guarantee that instance was running
920Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ADMIN_DEGRADED
930Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ADMIN_REFRESH
940Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ADMIN_RESTART
950Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF
960Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON
970Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON_IMMEDIATE
980Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF
990Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_STOP
1000Sstevel@tonic-gate  *		dependencies are, or are becoming, unsatisfied
1010Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_START
1020Sstevel@tonic-gate  *		dependencies have become satisfied
1030Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE
1040Sstevel@tonic-gate  *		instance caused a dependency cycle
1050Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY
1060Sstevel@tonic-gate  *		instance has an invalid dependency
1070Sstevel@tonic-gate  */
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_INVALID			0
1100Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ADD_INSTANCE		1
1110Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_REMOVE_INSTANCE		2
1120Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ENABLE			3
1130Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_DISABLE			4
1140Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ADMIN_DEGRADED		5
1150Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ADMIN_REFRESH		6
1160Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ADMIN_RESTART		7
1170Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF		8
1180Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON		9
1190Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON_IMMEDIATE	10
1200Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_STOP			11
1210Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_START			12
1220Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE		13
1230Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY		14
1240Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ADMIN_DISABLE		15
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate #define	RESTARTER_EVENT_ERROR			-1
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate #define	RESTARTER_EVENT_INSTANCE_DISABLED	0
1290Sstevel@tonic-gate #define	RESTARTER_EVENT_INSTANCE_ENABLED	1
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate typedef enum {
1320Sstevel@tonic-gate 	RESTARTER_STATE_NONE,
1330Sstevel@tonic-gate 	RESTARTER_STATE_UNINIT,
1340Sstevel@tonic-gate 	RESTARTER_STATE_MAINT,
1350Sstevel@tonic-gate 	RESTARTER_STATE_OFFLINE,
1360Sstevel@tonic-gate 	RESTARTER_STATE_DISABLED,
1370Sstevel@tonic-gate 	RESTARTER_STATE_ONLINE,
1380Sstevel@tonic-gate 	RESTARTER_STATE_DEGRADED
1390Sstevel@tonic-gate } restarter_instance_state_t;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate /*
1420Sstevel@tonic-gate  * These values are ordered by severity of required restart, as we use
1430Sstevel@tonic-gate  * integer comparisons to determine error flow.
1440Sstevel@tonic-gate  */
1450Sstevel@tonic-gate typedef enum {
1460Sstevel@tonic-gate 	RERR_UNSUPPORTED = -1,
1470Sstevel@tonic-gate 	RERR_NONE = 0,			/* no error, restart, refresh */
1480Sstevel@tonic-gate 	RERR_FAULT,			/* fault occurred */
1490Sstevel@tonic-gate 	RERR_RESTART,			/* transition due to restart */
1500Sstevel@tonic-gate 	RERR_REFRESH			/* transition due to refresh */
1510Sstevel@tonic-gate } restarter_error_t;
1520Sstevel@tonic-gate /*
1530Sstevel@tonic-gate  * restarter_store_contract() and restarter_remove_contract() types
1540Sstevel@tonic-gate  */
1550Sstevel@tonic-gate typedef enum {
1560Sstevel@tonic-gate 	RESTARTER_CONTRACT_PRIMARY,
1570Sstevel@tonic-gate 	RESTARTER_CONTRACT_TRANSIENT
1580Sstevel@tonic-gate } restarter_contract_type_t;
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate /*
1610Sstevel@tonic-gate  * restarter_bind_handle() registers a delegate with svc.startd to
1620Sstevel@tonic-gate  * begin consuming events.
1630Sstevel@tonic-gate  *
1640Sstevel@tonic-gate  * On initial bind, the delgated restarter receives an event for each
1650Sstevel@tonic-gate  * instance it is responsible for, as if that instance was new.
1660Sstevel@tonic-gate  *
1670Sstevel@tonic-gate  * callers must have superuser privileges
1680Sstevel@tonic-gate  *
1690Sstevel@tonic-gate  * The event handler can return 0 for success, or EAGAIN to request
1700Sstevel@tonic-gate  * retry of event delivery. EAGAIN may be returned 3 times before the
1710Sstevel@tonic-gate  * event is discarded.
1720Sstevel@tonic-gate  */
1730Sstevel@tonic-gate int restarter_bind_handle(uint32_t, const char *,
1740Sstevel@tonic-gate     int (*event_handler)(restarter_event_t *), int,
1750Sstevel@tonic-gate     restarter_event_handle_t **);
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate restarter_event_type_t restarter_event_get_type(restarter_event_t *);
1780Sstevel@tonic-gate uint64_t restarter_event_get_seq(restarter_event_t *);
1790Sstevel@tonic-gate void restarter_event_get_time(restarter_event_t *, hrtime_t *);
1800Sstevel@tonic-gate ssize_t restarter_event_get_instance(restarter_event_t *, char *, size_t);
1810Sstevel@tonic-gate restarter_event_handle_t *restarter_event_get_handle(restarter_event_t *);
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate /*
1840Sstevel@tonic-gate  * The following functions work only on certain types of events.
1850Sstevel@tonic-gate  * They fail with a return of -1 if they're called on an inappropriate event.
1860Sstevel@tonic-gate  */
1870Sstevel@tonic-gate int restarter_event_get_enabled(restarter_event_t *);
1880Sstevel@tonic-gate int restarter_event_get_current_states(restarter_event_t *,
1890Sstevel@tonic-gate     restarter_instance_state_t *, restarter_instance_state_t *);
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate /*
1920Sstevel@tonic-gate  * Functions for updating the repository.
1930Sstevel@tonic-gate  */
1948823STruong.Q.Nguyen@Sun.COM 
1958823STruong.Q.Nguyen@Sun.COM /*
1968823STruong.Q.Nguyen@Sun.COM  * When setting state to "maintenance", callers of restarter_set_states() can
1978823STruong.Q.Nguyen@Sun.COM  * set aux_state to "service_request" to communicate that another service has
1988823STruong.Q.Nguyen@Sun.COM  * requested maintenance state for the target service.
1998823STruong.Q.Nguyen@Sun.COM  *
2008823STruong.Q.Nguyen@Sun.COM  * Callers should use restarter_inst_validate_aux_fmri() to validate the fmri
2018823STruong.Q.Nguyen@Sun.COM  * of the requested service and pass "service_request" for aux_state when
2028823STruong.Q.Nguyen@Sun.COM  * calling restarter_set_states(). See inetd and startd for examples.
2038823STruong.Q.Nguyen@Sun.COM  */
2040Sstevel@tonic-gate int restarter_set_states(restarter_event_handle_t *, const char *,
2050Sstevel@tonic-gate     restarter_instance_state_t, restarter_instance_state_t,
2060Sstevel@tonic-gate     restarter_instance_state_t, restarter_instance_state_t, restarter_error_t,
2070Sstevel@tonic-gate     const char *);
2086045Srm88369 int restarter_event_publish_retry(evchan_t *, const char *, const char *,
2096045Srm88369     const char *, const char *, nvlist_t *, uint32_t);
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate int restarter_store_contract(scf_instance_t *, ctid_t,
2120Sstevel@tonic-gate     restarter_contract_type_t);
2130Sstevel@tonic-gate int restarter_remove_contract(scf_instance_t *, ctid_t,
2140Sstevel@tonic-gate     restarter_contract_type_t);
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate ssize_t restarter_state_to_string(restarter_instance_state_t, char *, size_t);
2170Sstevel@tonic-gate restarter_instance_state_t restarter_string_to_state(char *);
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate #define	RESTARTER_METHOD_CONTEXT_VERSION	6
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate struct method_context {
2220Sstevel@tonic-gate 	/* Stable */
2230Sstevel@tonic-gate 	uid_t		uid, euid;
2240Sstevel@tonic-gate 	gid_t		gid, egid;
2250Sstevel@tonic-gate 	int		ngroups;		/* -1 means use initgroups(). */
226*9263SSean.Wilcox@Sun.COM 	gid_t		groups[NGROUPS_MAX];
2270Sstevel@tonic-gate 	priv_set_t	*lpriv_set, *priv_set;
2280Sstevel@tonic-gate 	char		*corefile_pattern;	/* Optional. */
2290Sstevel@tonic-gate 	char		*project;		/* NULL for no change */
2300Sstevel@tonic-gate 	char		*resource_pool;		/* NULL for project default */
2310Sstevel@tonic-gate 	char		*working_dir;		/* NULL for :default */
2320Sstevel@tonic-gate 	char		**env;			/* NULL for no env */
2330Sstevel@tonic-gate 	size_t		env_sz;			/* size of env array */
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	/* Private */
2360Sstevel@tonic-gate 	char		*vbuf;
2370Sstevel@tonic-gate 	ssize_t		vbuf_sz;
2380Sstevel@tonic-gate 	struct passwd	pwd;
2390Sstevel@tonic-gate 	char		*pwbuf;
2400Sstevel@tonic-gate 	ssize_t		pwbufsz;
2410Sstevel@tonic-gate };
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate int restarter_rm_libs_loadable(void);
2440Sstevel@tonic-gate /* instance, restarter name, method name, command line, structure pointer */
2450Sstevel@tonic-gate const char *restarter_get_method_context(uint_t, scf_instance_t *,
2460Sstevel@tonic-gate     scf_snapshot_t *, const char *, const char *, struct method_context **);
2470Sstevel@tonic-gate int restarter_set_method_context(struct method_context *, const char **);
2480Sstevel@tonic-gate void restarter_free_method_context(struct method_context *);
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate int restarter_is_null_method(const char *);
2520Sstevel@tonic-gate int restarter_is_kill_method(const char *);
2530Sstevel@tonic-gate int restarter_is_kill_proc_method(const char *);
2540Sstevel@tonic-gate 
2558823STruong.Q.Nguyen@Sun.COM /* Validate the inst fmri specified in  restarter_actions/auxiliary_fmri */
2568823STruong.Q.Nguyen@Sun.COM int restarter_inst_validate_ractions_aux_fmri(scf_instance_t *);
2578823STruong.Q.Nguyen@Sun.COM 
2588823STruong.Q.Nguyen@Sun.COM /* Delete instance's restarter_actions/auxiliary_fmri property */
2598823STruong.Q.Nguyen@Sun.COM int restarter_inst_reset_ractions_aux_fmri(scf_instance_t *);
2608823STruong.Q.Nguyen@Sun.COM 
2618823STruong.Q.Nguyen@Sun.COM /* Get boolean value from instance's restarter_actions/auxiliary_tty */
2628823STruong.Q.Nguyen@Sun.COM int restarter_inst_ractions_from_tty(scf_instance_t *);
2638823STruong.Q.Nguyen@Sun.COM 
2648823STruong.Q.Nguyen@Sun.COM /* Delete instance's restarter/auxiliary_fmri property */
2658823STruong.Q.Nguyen@Sun.COM int restarter_inst_reset_aux_fmri(scf_instance_t *);
2668823STruong.Q.Nguyen@Sun.COM 
2678823STruong.Q.Nguyen@Sun.COM /*
2688823STruong.Q.Nguyen@Sun.COM  * Set instance's restarter/auxiliary_fmri, value come from
2698823STruong.Q.Nguyen@Sun.COM  * restarter_actions/auxliary_fmri
2708823STruong.Q.Nguyen@Sun.COM  */
2718823STruong.Q.Nguyen@Sun.COM int restarter_inst_set_aux_fmri(scf_instance_t *);
2728823STruong.Q.Nguyen@Sun.COM 
2730Sstevel@tonic-gate #ifdef	__cplusplus
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate #endif
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate #endif	/* _LIBRESTART_H */
278