xref: /titanic_54/usr/src/lib/librestart/common/librestart.h (revision eb1a34638eba7c5add1421327f3eb225a8ea7518)
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 /*
22*eb1a3463STruong 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 
61*eb1a3463STruong Nguyen /*
62*eb1a3463STruong Nguyen  * Some of the functions have external contracted consumers, review contracts
63*eb1a3463STruong Nguyen  * when making incompatible changes.
64*eb1a3463STruong Nguyen  */
65*eb1a3463STruong 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 
817c478bd9Sstevel@tonic-gate /*
827c478bd9Sstevel@tonic-gate  * Event types
837c478bd9Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ADD_INSTANCE
847c478bd9Sstevel@tonic-gate  *		responsible for a new (stopped) instance
857c478bd9Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_REMOVE_INSTANCE
867c478bd9Sstevel@tonic-gate  *		no longer responsible for this instance; stop it and return
877c478bd9Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ENABLE
887c478bd9Sstevel@tonic-gate  *		no guarantee that dependencies are met; see
897c478bd9Sstevel@tonic-gate  *		RESTARTER_EVENT_TYPE_START
907c478bd9Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_DISABLE
917c478bd9Sstevel@tonic-gate  *		no guarantee that instance was running
927c478bd9Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ADMIN_DEGRADED
937c478bd9Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ADMIN_REFRESH
947c478bd9Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ADMIN_RESTART
957c478bd9Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF
967c478bd9Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON
977c478bd9Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON_IMMEDIATE
987c478bd9Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF
997c478bd9Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_STOP
1007c478bd9Sstevel@tonic-gate  *		dependencies are, or are becoming, unsatisfied
1017c478bd9Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_START
1027c478bd9Sstevel@tonic-gate  *		dependencies have become satisfied
1037c478bd9Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE
1047c478bd9Sstevel@tonic-gate  *		instance caused a dependency cycle
1057c478bd9Sstevel@tonic-gate  *	RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY
1067c478bd9Sstevel@tonic-gate  *		instance has an invalid dependency
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_INVALID			0
1107c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ADD_INSTANCE		1
1117c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_REMOVE_INSTANCE		2
1127c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ENABLE			3
1137c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_DISABLE			4
1147c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ADMIN_DEGRADED		5
1157c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ADMIN_REFRESH		6
1167c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ADMIN_RESTART		7
1177c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF		8
1187c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON		9
1197c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON_IMMEDIATE	10
1207c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_STOP			11
1217c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_START			12
1227c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE		13
1237c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY		14
1247c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_TYPE_ADMIN_DISABLE		15
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_ERROR			-1
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_INSTANCE_DISABLED	0
1297c478bd9Sstevel@tonic-gate #define	RESTARTER_EVENT_INSTANCE_ENABLED	1
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate typedef enum {
1327c478bd9Sstevel@tonic-gate 	RESTARTER_STATE_NONE,
1337c478bd9Sstevel@tonic-gate 	RESTARTER_STATE_UNINIT,
1347c478bd9Sstevel@tonic-gate 	RESTARTER_STATE_MAINT,
1357c478bd9Sstevel@tonic-gate 	RESTARTER_STATE_OFFLINE,
1367c478bd9Sstevel@tonic-gate 	RESTARTER_STATE_DISABLED,
1377c478bd9Sstevel@tonic-gate 	RESTARTER_STATE_ONLINE,
1387c478bd9Sstevel@tonic-gate 	RESTARTER_STATE_DEGRADED
1397c478bd9Sstevel@tonic-gate } restarter_instance_state_t;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate /*
1427c478bd9Sstevel@tonic-gate  * These values are ordered by severity of required restart, as we use
1437c478bd9Sstevel@tonic-gate  * integer comparisons to determine error flow.
1447c478bd9Sstevel@tonic-gate  */
1457c478bd9Sstevel@tonic-gate typedef enum {
1467c478bd9Sstevel@tonic-gate 	RERR_UNSUPPORTED = -1,
1477c478bd9Sstevel@tonic-gate 	RERR_NONE = 0,			/* no error, restart, refresh */
1487c478bd9Sstevel@tonic-gate 	RERR_FAULT,			/* fault occurred */
1497c478bd9Sstevel@tonic-gate 	RERR_RESTART,			/* transition due to restart */
1507c478bd9Sstevel@tonic-gate 	RERR_REFRESH			/* transition due to refresh */
1517c478bd9Sstevel@tonic-gate } restarter_error_t;
1527c478bd9Sstevel@tonic-gate /*
1537c478bd9Sstevel@tonic-gate  * restarter_store_contract() and restarter_remove_contract() types
1547c478bd9Sstevel@tonic-gate  */
1557c478bd9Sstevel@tonic-gate typedef enum {
1567c478bd9Sstevel@tonic-gate 	RESTARTER_CONTRACT_PRIMARY,
1577c478bd9Sstevel@tonic-gate 	RESTARTER_CONTRACT_TRANSIENT
1587c478bd9Sstevel@tonic-gate } restarter_contract_type_t;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate /*
1617c478bd9Sstevel@tonic-gate  * restarter_bind_handle() registers a delegate with svc.startd to
1627c478bd9Sstevel@tonic-gate  * begin consuming events.
1637c478bd9Sstevel@tonic-gate  *
1647c478bd9Sstevel@tonic-gate  * On initial bind, the delgated restarter receives an event for each
1657c478bd9Sstevel@tonic-gate  * instance it is responsible for, as if that instance was new.
1667c478bd9Sstevel@tonic-gate  *
1677c478bd9Sstevel@tonic-gate  * callers must have superuser privileges
1687c478bd9Sstevel@tonic-gate  *
1697c478bd9Sstevel@tonic-gate  * The event handler can return 0 for success, or EAGAIN to request
1707c478bd9Sstevel@tonic-gate  * retry of event delivery. EAGAIN may be returned 3 times before the
1717c478bd9Sstevel@tonic-gate  * event is discarded.
1727c478bd9Sstevel@tonic-gate  */
1737c478bd9Sstevel@tonic-gate int restarter_bind_handle(uint32_t, const char *,
1747c478bd9Sstevel@tonic-gate     int (*event_handler)(restarter_event_t *), int,
1757c478bd9Sstevel@tonic-gate     restarter_event_handle_t **);
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate restarter_event_type_t restarter_event_get_type(restarter_event_t *);
1787c478bd9Sstevel@tonic-gate uint64_t restarter_event_get_seq(restarter_event_t *);
1797c478bd9Sstevel@tonic-gate void restarter_event_get_time(restarter_event_t *, hrtime_t *);
1807c478bd9Sstevel@tonic-gate ssize_t restarter_event_get_instance(restarter_event_t *, char *, size_t);
1817c478bd9Sstevel@tonic-gate restarter_event_handle_t *restarter_event_get_handle(restarter_event_t *);
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate /*
1847c478bd9Sstevel@tonic-gate  * The following functions work only on certain types of events.
1857c478bd9Sstevel@tonic-gate  * They fail with a return of -1 if they're called on an inappropriate event.
1867c478bd9Sstevel@tonic-gate  */
1877c478bd9Sstevel@tonic-gate int restarter_event_get_enabled(restarter_event_t *);
1887c478bd9Sstevel@tonic-gate int restarter_event_get_current_states(restarter_event_t *,
1897c478bd9Sstevel@tonic-gate     restarter_instance_state_t *, restarter_instance_state_t *);
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate /*
1927c478bd9Sstevel@tonic-gate  * Functions for updating the repository.
1937c478bd9Sstevel@tonic-gate  */
194*eb1a3463STruong Nguyen 
195*eb1a3463STruong Nguyen /*
196*eb1a3463STruong Nguyen  * When setting state to "maintenance", callers of restarter_set_states() can
197*eb1a3463STruong Nguyen  * set aux_state to "service_request" to communicate that another service has
198*eb1a3463STruong Nguyen  * requested maintenance state for the target service.
199*eb1a3463STruong Nguyen  *
200*eb1a3463STruong Nguyen  * Callers should use restarter_inst_validate_aux_fmri() to validate the fmri
201*eb1a3463STruong Nguyen  * of the requested service and pass "service_request" for aux_state when
202*eb1a3463STruong Nguyen  * calling restarter_set_states(). See inetd and startd for examples.
203*eb1a3463STruong Nguyen  */
2047c478bd9Sstevel@tonic-gate int restarter_set_states(restarter_event_handle_t *, const char *,
2057c478bd9Sstevel@tonic-gate     restarter_instance_state_t, restarter_instance_state_t,
2067c478bd9Sstevel@tonic-gate     restarter_instance_state_t, restarter_instance_state_t, restarter_error_t,
2077c478bd9Sstevel@tonic-gate     const char *);
2082c65c8b0Srm88369 int restarter_event_publish_retry(evchan_t *, const char *, const char *,
2092c65c8b0Srm88369     const char *, const char *, nvlist_t *, uint32_t);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate int restarter_store_contract(scf_instance_t *, ctid_t,
2127c478bd9Sstevel@tonic-gate     restarter_contract_type_t);
2137c478bd9Sstevel@tonic-gate int restarter_remove_contract(scf_instance_t *, ctid_t,
2147c478bd9Sstevel@tonic-gate     restarter_contract_type_t);
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate ssize_t restarter_state_to_string(restarter_instance_state_t, char *, size_t);
2177c478bd9Sstevel@tonic-gate restarter_instance_state_t restarter_string_to_state(char *);
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate #define	RESTARTER_METHOD_CONTEXT_VERSION	6
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate struct method_context {
2227c478bd9Sstevel@tonic-gate 	/* Stable */
2237c478bd9Sstevel@tonic-gate 	uid_t		uid, euid;
2247c478bd9Sstevel@tonic-gate 	gid_t		gid, egid;
2257c478bd9Sstevel@tonic-gate 	int		ngroups;		/* -1 means use initgroups(). */
2267c478bd9Sstevel@tonic-gate 	gid_t		groups[NGROUPS_MAX-1];
2277c478bd9Sstevel@tonic-gate 	priv_set_t	*lpriv_set, *priv_set;
2287c478bd9Sstevel@tonic-gate 	char		*corefile_pattern;	/* Optional. */
2297c478bd9Sstevel@tonic-gate 	char		*project;		/* NULL for no change */
2307c478bd9Sstevel@tonic-gate 	char		*resource_pool;		/* NULL for project default */
2317c478bd9Sstevel@tonic-gate 	char		*working_dir;		/* NULL for :default */
2327c478bd9Sstevel@tonic-gate 	char		**env;			/* NULL for no env */
2337c478bd9Sstevel@tonic-gate 	size_t		env_sz;			/* size of env array */
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	/* Private */
2367c478bd9Sstevel@tonic-gate 	char		*vbuf;
2377c478bd9Sstevel@tonic-gate 	ssize_t		vbuf_sz;
2387c478bd9Sstevel@tonic-gate 	struct passwd	pwd;
2397c478bd9Sstevel@tonic-gate 	char		*pwbuf;
2407c478bd9Sstevel@tonic-gate 	ssize_t		pwbufsz;
2417c478bd9Sstevel@tonic-gate };
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate int restarter_rm_libs_loadable(void);
2447c478bd9Sstevel@tonic-gate /* instance, restarter name, method name, command line, structure pointer */
2457c478bd9Sstevel@tonic-gate const char *restarter_get_method_context(uint_t, scf_instance_t *,
2467c478bd9Sstevel@tonic-gate     scf_snapshot_t *, const char *, const char *, struct method_context **);
2477c478bd9Sstevel@tonic-gate int restarter_set_method_context(struct method_context *, const char **);
2487c478bd9Sstevel@tonic-gate void restarter_free_method_context(struct method_context *);
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate int restarter_is_null_method(const char *);
2527c478bd9Sstevel@tonic-gate int restarter_is_kill_method(const char *);
2537c478bd9Sstevel@tonic-gate int restarter_is_kill_proc_method(const char *);
2547c478bd9Sstevel@tonic-gate 
255*eb1a3463STruong Nguyen /* Validate the inst fmri specified in  restarter_actions/auxiliary_fmri */
256*eb1a3463STruong Nguyen int restarter_inst_validate_ractions_aux_fmri(scf_instance_t *);
257*eb1a3463STruong Nguyen 
258*eb1a3463STruong Nguyen /* Delete instance's restarter_actions/auxiliary_fmri property */
259*eb1a3463STruong Nguyen int restarter_inst_reset_ractions_aux_fmri(scf_instance_t *);
260*eb1a3463STruong Nguyen 
261*eb1a3463STruong Nguyen /* Get boolean value from instance's restarter_actions/auxiliary_tty */
262*eb1a3463STruong Nguyen int restarter_inst_ractions_from_tty(scf_instance_t *);
263*eb1a3463STruong Nguyen 
264*eb1a3463STruong Nguyen /* Delete instance's restarter/auxiliary_fmri property */
265*eb1a3463STruong Nguyen int restarter_inst_reset_aux_fmri(scf_instance_t *);
266*eb1a3463STruong Nguyen 
267*eb1a3463STruong Nguyen /*
268*eb1a3463STruong Nguyen  * Set instance's restarter/auxiliary_fmri, value come from
269*eb1a3463STruong Nguyen  * restarter_actions/auxliary_fmri
270*eb1a3463STruong Nguyen  */
271*eb1a3463STruong Nguyen int restarter_inst_set_aux_fmri(scf_instance_t *);
272*eb1a3463STruong Nguyen 
2737c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate #endif
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate #endif	/* _LIBRESTART_H */
278