xref: /titanic_52/usr/src/lib/librestart/common/librestart.h (revision 2c65c8b07fc9eb4a059c4c47b7d637cd6905909e)
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
5*2c65c8b0Srm88369  * Common Development and Distribution License (the "License").
6*2c65c8b0Srm88369  * 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*2c65c8b0Srm88369  * Copyright 2008 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 
297c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
307c478bd9Sstevel@tonic-gate 
31*2c65c8b0Srm88369 #include <libsysevent.h>
327c478bd9Sstevel@tonic-gate #include <libcontract.h>
337c478bd9Sstevel@tonic-gate #include <libscf.h>
347c478bd9Sstevel@tonic-gate #include <limits.h>
357c478bd9Sstevel@tonic-gate #include <priv.h>
367c478bd9Sstevel@tonic-gate #include <pwd.h>
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
407c478bd9Sstevel@tonic-gate extern "C" {
417c478bd9Sstevel@tonic-gate #endif
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  * There are 3 parts to librestart.
457c478bd9Sstevel@tonic-gate  *	1) The event protocol from the master restarter to its delegates.
467c478bd9Sstevel@tonic-gate  *	2) A functional interface for updating the repository.
477c478bd9Sstevel@tonic-gate  *	3) Convenience functions for common restarter tasks.
487c478bd9Sstevel@tonic-gate  *
497c478bd9Sstevel@tonic-gate  * Event protocol
507c478bd9Sstevel@tonic-gate  *	We need a reliable event protocol, as there's no way to define
517c478bd9Sstevel@tonic-gate  *	restarter events as idempotent.
527c478bd9Sstevel@tonic-gate  *
537c478bd9Sstevel@tonic-gate  *	Currently using sysevent channels as the reliable event implementation.
547c478bd9Sstevel@tonic-gate  *	This could change if the implementation proves unsuitable, but
557c478bd9Sstevel@tonic-gate  *	the API defined here should abstract anything but a change in
567c478bd9Sstevel@tonic-gate  *	the fundamental event model.
577c478bd9Sstevel@tonic-gate  *
587c478bd9Sstevel@tonic-gate  *	We offer functions to tease apart the event rather than generic
597c478bd9Sstevel@tonic-gate  *	nvpair interfaces. This is because each event type has a well-
607c478bd9Sstevel@tonic-gate  *	defined set of fields.
617c478bd9Sstevel@tonic-gate  */
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate typedef struct restarter_event_handle restarter_event_handle_t;
647c478bd9Sstevel@tonic-gate typedef struct restarter_event restarter_event_t;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate typedef uint32_t restarter_event_type_t;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /*
697c478bd9Sstevel@tonic-gate  * Define an event protocol version. In theory, we could use this in
707c478bd9Sstevel@tonic-gate  * the future to support delegated restarters which use an older
717c478bd9Sstevel@tonic-gate  * protocol. In practice, increment RESTARTER_EVENT_VERSION whenever the
727c478bd9Sstevel@tonic-gate  * protocol might have changed.
737c478bd9Sstevel@tonic-gate  */
747c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_VERSION		4
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #define	RESTARTER_FLAG_DEBUG		1
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate  * Event types
807c478bd9Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ADD_INSTANCE
817c478bd9Sstevel@tonic-gate  *		responsible for a new (stopped) instance
827c478bd9Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_REMOVE_INSTANCE
837c478bd9Sstevel@tonic-gate  *		no longer responsible for this instance; stop it and return
847c478bd9Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ENABLE
857c478bd9Sstevel@tonic-gate  *		no guarantee that dependencies are met; see
867c478bd9Sstevel@tonic-gate  *		RESTARTER_EVENT_TYPE_START
877c478bd9Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_DISABLE
887c478bd9Sstevel@tonic-gate  *		no guarantee that instance was running
897c478bd9Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ADMIN_DEGRADED
907c478bd9Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ADMIN_REFRESH
917c478bd9Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ADMIN_RESTART
927c478bd9Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF
937c478bd9Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON
947c478bd9Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON_IMMEDIATE
957c478bd9Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF
967c478bd9Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_STOP
977c478bd9Sstevel@tonic-gate  *		dependencies are, or are becoming, unsatisfied
987c478bd9Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_START
997c478bd9Sstevel@tonic-gate  *		dependencies have become satisfied
1007c478bd9Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE
1017c478bd9Sstevel@tonic-gate  *		instance caused a dependency cycle
1027c478bd9Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY
1037c478bd9Sstevel@tonic-gate  *		instance has an invalid dependency
1047c478bd9Sstevel@tonic-gate  */
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_INVALID			0
1077c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ADD_INSTANCE		1
1087c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_REMOVE_INSTANCE		2
1097c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ENABLE			3
1107c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_DISABLE			4
1117c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ADMIN_DEGRADED		5
1127c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ADMIN_REFRESH		6
1137c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ADMIN_RESTART		7
1147c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF		8
1157c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON		9
1167c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON_IMMEDIATE	10
1177c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_STOP			11
1187c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_START			12
1197c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE		13
1207c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY		14
1217c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ADMIN_DISABLE		15
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_ERROR			-1
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_INSTANCE_DISABLED	0
1267c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_INSTANCE_ENABLED	1
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate typedef enum {
1297c478bd9Sstevel@tonic-gate 	RESTARTER_STATE_NONE,
1307c478bd9Sstevel@tonic-gate 	RESTARTER_STATE_UNINIT,
1317c478bd9Sstevel@tonic-gate 	RESTARTER_STATE_MAINT,
1327c478bd9Sstevel@tonic-gate 	RESTARTER_STATE_OFFLINE,
1337c478bd9Sstevel@tonic-gate 	RESTARTER_STATE_DISABLED,
1347c478bd9Sstevel@tonic-gate 	RESTARTER_STATE_ONLINE,
1357c478bd9Sstevel@tonic-gate 	RESTARTER_STATE_DEGRADED
1367c478bd9Sstevel@tonic-gate } restarter_instance_state_t;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate  * These values are ordered by severity of required restart, as we use
1407c478bd9Sstevel@tonic-gate  * integer comparisons to determine error flow.
1417c478bd9Sstevel@tonic-gate  */
1427c478bd9Sstevel@tonic-gate typedef enum {
1437c478bd9Sstevel@tonic-gate 	RERR_UNSUPPORTED = -1,
1447c478bd9Sstevel@tonic-gate 	RERR_NONE = 0,			/* no error, restart, refresh */
1457c478bd9Sstevel@tonic-gate 	RERR_FAULT,			/* fault occurred */
1467c478bd9Sstevel@tonic-gate 	RERR_RESTART,			/* transition due to restart */
1477c478bd9Sstevel@tonic-gate 	RERR_REFRESH			/* transition due to refresh */
1487c478bd9Sstevel@tonic-gate } restarter_error_t;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate  * restarter_store_contract() and restarter_remove_contract() types
1527c478bd9Sstevel@tonic-gate  */
1537c478bd9Sstevel@tonic-gate typedef enum {
1547c478bd9Sstevel@tonic-gate 	RESTARTER_CONTRACT_PRIMARY,
1557c478bd9Sstevel@tonic-gate 	RESTARTER_CONTRACT_TRANSIENT
1567c478bd9Sstevel@tonic-gate } restarter_contract_type_t;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate /*
1597c478bd9Sstevel@tonic-gate  * restarter_bind_handle() registers a delegate with svc.startd to
1607c478bd9Sstevel@tonic-gate  * begin consuming events.
1617c478bd9Sstevel@tonic-gate  *
1627c478bd9Sstevel@tonic-gate  * On initial bind, the delgated restarter receives an event for each
1637c478bd9Sstevel@tonic-gate  * instance it is responsible for, as if that instance was new.
1647c478bd9Sstevel@tonic-gate  *
1657c478bd9Sstevel@tonic-gate  * callers must have superuser privileges
1667c478bd9Sstevel@tonic-gate  *
1677c478bd9Sstevel@tonic-gate  * The event handler can return 0 for success, or EAGAIN to request
1687c478bd9Sstevel@tonic-gate  * retry of event delivery. EAGAIN may be returned 3 times before the
1697c478bd9Sstevel@tonic-gate  * event is discarded.
1707c478bd9Sstevel@tonic-gate  */
1717c478bd9Sstevel@tonic-gate int restarter_bind_handle(uint32_t, const char *,
1727c478bd9Sstevel@tonic-gate     int (*event_handler)(restarter_event_t *), int,
1737c478bd9Sstevel@tonic-gate     restarter_event_handle_t **);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate restarter_event_type_t restarter_event_get_type(restarter_event_t *);
1767c478bd9Sstevel@tonic-gate uint64_t restarter_event_get_seq(restarter_event_t *);
1777c478bd9Sstevel@tonic-gate void restarter_event_get_time(restarter_event_t *, hrtime_t *);
1787c478bd9Sstevel@tonic-gate ssize_t restarter_event_get_instance(restarter_event_t *, char *, size_t);
1797c478bd9Sstevel@tonic-gate restarter_event_handle_t *restarter_event_get_handle(restarter_event_t *);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate  * The following functions work only on certain types of events.
1837c478bd9Sstevel@tonic-gate  * They fail with a return of -1 if they're called on an inappropriate event.
1847c478bd9Sstevel@tonic-gate  */
1857c478bd9Sstevel@tonic-gate int restarter_event_get_enabled(restarter_event_t *);
1867c478bd9Sstevel@tonic-gate int restarter_event_get_current_states(restarter_event_t *,
1877c478bd9Sstevel@tonic-gate     restarter_instance_state_t *, restarter_instance_state_t *);
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate /*
1907c478bd9Sstevel@tonic-gate  * Functions for updating the repository.
1917c478bd9Sstevel@tonic-gate  */
1927c478bd9Sstevel@tonic-gate int restarter_set_states(restarter_event_handle_t *, const char *,
1937c478bd9Sstevel@tonic-gate     restarter_instance_state_t, restarter_instance_state_t,
1947c478bd9Sstevel@tonic-gate     restarter_instance_state_t, restarter_instance_state_t, restarter_error_t,
1957c478bd9Sstevel@tonic-gate     const char *);
196*2c65c8b0Srm88369 int restarter_event_publish_retry(evchan_t *, const char *, const char *,
197*2c65c8b0Srm88369     const char *, const char *, nvlist_t *, uint32_t);
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate int restarter_store_contract(scf_instance_t *, ctid_t,
2007c478bd9Sstevel@tonic-gate     restarter_contract_type_t);
2017c478bd9Sstevel@tonic-gate int restarter_remove_contract(scf_instance_t *, ctid_t,
2027c478bd9Sstevel@tonic-gate     restarter_contract_type_t);
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate ssize_t restarter_state_to_string(restarter_instance_state_t, char *, size_t);
2057c478bd9Sstevel@tonic-gate restarter_instance_state_t restarter_string_to_state(char *);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate #define	RESTARTER_METHOD_CONTEXT_VERSION	6
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate struct method_context {
2107c478bd9Sstevel@tonic-gate 	/* Stable */
2117c478bd9Sstevel@tonic-gate 	uid_t		uid, euid;
2127c478bd9Sstevel@tonic-gate 	gid_t		gid, egid;
2137c478bd9Sstevel@tonic-gate 	int		ngroups;		/* -1 means use initgroups(). */
2147c478bd9Sstevel@tonic-gate 	gid_t		groups[NGROUPS_MAX-1];
2157c478bd9Sstevel@tonic-gate 	priv_set_t	*lpriv_set, *priv_set;
2167c478bd9Sstevel@tonic-gate 	char		*corefile_pattern;	/* Optional. */
2177c478bd9Sstevel@tonic-gate 	char		*project;		/* NULL for no change */
2187c478bd9Sstevel@tonic-gate 	char		*resource_pool;		/* NULL for project default */
2197c478bd9Sstevel@tonic-gate 	char		*working_dir;		/* NULL for :default */
2207c478bd9Sstevel@tonic-gate 	char		**env;			/* NULL for no env */
2217c478bd9Sstevel@tonic-gate 	size_t		env_sz;			/* size of env array */
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	/* Private */
2247c478bd9Sstevel@tonic-gate 	char		*vbuf;
2257c478bd9Sstevel@tonic-gate 	ssize_t		vbuf_sz;
2267c478bd9Sstevel@tonic-gate 	struct passwd	pwd;
2277c478bd9Sstevel@tonic-gate 	char		*pwbuf;
2287c478bd9Sstevel@tonic-gate 	ssize_t		pwbufsz;
2297c478bd9Sstevel@tonic-gate };
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate int restarter_rm_libs_loadable(void);
2327c478bd9Sstevel@tonic-gate /* instance, restarter name, method name, command line, structure pointer */
2337c478bd9Sstevel@tonic-gate const char *restarter_get_method_context(uint_t, scf_instance_t *,
2347c478bd9Sstevel@tonic-gate     scf_snapshot_t *, const char *, const char *, struct method_context **);
2357c478bd9Sstevel@tonic-gate int restarter_set_method_context(struct method_context *, const char **);
2367c478bd9Sstevel@tonic-gate void restarter_free_method_context(struct method_context *);
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate int restarter_is_null_method(const char *);
2407c478bd9Sstevel@tonic-gate int restarter_is_kill_method(const char *);
2417c478bd9Sstevel@tonic-gate int restarter_is_kill_proc_method(const char *);
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate #endif
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate #endif	/* _LIBRESTART_H */
248