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*004388ebScasper  * Common Development and Distribution License (the "License").
6*004388ebScasper  * 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
20*004388ebScasper  */
21*004388ebScasper /*
22*004388ebScasper  * Copyright 2006 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 _RCM_IMPL_H
277c478bd9Sstevel@tonic-gate #define	_RCM_IMPL_H
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
307c478bd9Sstevel@tonic-gate extern "C" {
317c478bd9Sstevel@tonic-gate #endif
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <assert.h>
347c478bd9Sstevel@tonic-gate #include <stdarg.h>
357c478bd9Sstevel@tonic-gate #include <stdio.h>
36*004388ebScasper #include <stdio_ext.h>
377c478bd9Sstevel@tonic-gate #include <stdlib.h>
387c478bd9Sstevel@tonic-gate #include <dirent.h>
397c478bd9Sstevel@tonic-gate #include <dlfcn.h>
407c478bd9Sstevel@tonic-gate #include <errno.h>
417c478bd9Sstevel@tonic-gate #include <fcntl.h>
427c478bd9Sstevel@tonic-gate #include <limits.h>
437c478bd9Sstevel@tonic-gate #include <locale.h>
447c478bd9Sstevel@tonic-gate #include <poll.h>
457c478bd9Sstevel@tonic-gate #include <signal.h>
467c478bd9Sstevel@tonic-gate #include <strings.h>
477c478bd9Sstevel@tonic-gate #include <syslog.h>
487c478bd9Sstevel@tonic-gate #include <thread.h>
497c478bd9Sstevel@tonic-gate #include <unistd.h>
507c478bd9Sstevel@tonic-gate #include <sys/mman.h>
517c478bd9Sstevel@tonic-gate #include <sys/param.h>
527c478bd9Sstevel@tonic-gate #include <sys/stat.h>
537c478bd9Sstevel@tonic-gate #include <sys/types.h>
547c478bd9Sstevel@tonic-gate #include <librcm.h>
557c478bd9Sstevel@tonic-gate #include <librcm_impl.h>
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #include "rcm_module.h"
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate  * Daemon states for thread control
627c478bd9Sstevel@tonic-gate  */
637c478bd9Sstevel@tonic-gate #define	RCMD_INIT	1
647c478bd9Sstevel@tonic-gate #define	RCMD_NORMAL	2
657c478bd9Sstevel@tonic-gate #define	RCMD_CLEANUP	3
667c478bd9Sstevel@tonic-gate #define	RCMD_FINI	4
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /*
697c478bd9Sstevel@tonic-gate  * flags for node operation
707c478bd9Sstevel@tonic-gate  */
717c478bd9Sstevel@tonic-gate #define	RSRC_NODE_CREATE	1
727c478bd9Sstevel@tonic-gate #define	RSRC_NODE_REMOVE	2	/* not used */
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate  * Resource types
767c478bd9Sstevel@tonic-gate  */
777c478bd9Sstevel@tonic-gate #define	RSRC_TYPE_NORMAL	0
787c478bd9Sstevel@tonic-gate #define	RSRC_TYPE_DEVICE	1
797c478bd9Sstevel@tonic-gate #define	RSRC_TYPE_FILESYS	2
807c478bd9Sstevel@tonic-gate #define	RSRC_TYPE_ABSTRACT	3
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate  * lock conflict checking flags
847c478bd9Sstevel@tonic-gate  */
857c478bd9Sstevel@tonic-gate #define	LOCK_FOR_DR		0
867c478bd9Sstevel@tonic-gate #define	LOCK_FOR_USE		1
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate /*
897c478bd9Sstevel@tonic-gate  * Sequence number encoding constants
907c478bd9Sstevel@tonic-gate  */
917c478bd9Sstevel@tonic-gate #define	SEQ_NUM_SHIFT	8	/* lowest 8 bits indicate cascade operation */
927c478bd9Sstevel@tonic-gate #define	SEQ_NUM_MASK	((1 << SEQ_NUM_SHIFT) - 1)
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * RCM queuing structure
967c478bd9Sstevel@tonic-gate  */
977c478bd9Sstevel@tonic-gate typedef struct rcm_queue {
987c478bd9Sstevel@tonic-gate 	struct rcm_queue	*next;
997c478bd9Sstevel@tonic-gate 	struct rcm_queue	*prev;
1007c478bd9Sstevel@tonic-gate } rcm_queue_t;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate #define	RCM_STRUCT_BASE_ADDR(struct_type, x, y)		\
1037c478bd9Sstevel@tonic-gate 	((struct_type *) ((void *)(((char *)(x)) -	\
1047c478bd9Sstevel@tonic-gate 			(int)(&((struct_type *)0)->y))))
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate  * Struct for client loadable module
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate typedef struct module {
1107c478bd9Sstevel@tonic-gate 	struct module	*next;
1117c478bd9Sstevel@tonic-gate 	void		*dlhandle;
1127c478bd9Sstevel@tonic-gate 	struct rcm_mod_ops *(*init)();
1137c478bd9Sstevel@tonic-gate 	const char	*(*info)();
1147c478bd9Sstevel@tonic-gate 	int		(*fini)();
1157c478bd9Sstevel@tonic-gate 	struct rcm_mod_ops *modops;	/* ops vector */
1167c478bd9Sstevel@tonic-gate 	char		*name;		/* module name */
1177c478bd9Sstevel@tonic-gate 	rcm_handle_t	*rcmhandle;
1187c478bd9Sstevel@tonic-gate 	int		ref_count;
1197c478bd9Sstevel@tonic-gate 	rcm_queue_t	client_q;	/* list of module's clients */
1207c478bd9Sstevel@tonic-gate 	struct script_info *rsi;	/* scripting data */
1217c478bd9Sstevel@tonic-gate } module_t;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate /*
1247c478bd9Sstevel@tonic-gate  * Struct for describing a resource client
1257c478bd9Sstevel@tonic-gate  */
1267c478bd9Sstevel@tonic-gate typedef struct client {
1277c478bd9Sstevel@tonic-gate 	rcm_queue_t	queue;		/* per module queue */
1287c478bd9Sstevel@tonic-gate 	struct client	*next;		/* next client on rsrc node list */
1297c478bd9Sstevel@tonic-gate 	module_t	*module;	/* per-client module */
1307c478bd9Sstevel@tonic-gate 	char		*alias;		/* rsrc_name known to client */
1317c478bd9Sstevel@tonic-gate 	pid_t		pid;		/* pid of regis process */
1327c478bd9Sstevel@tonic-gate 	int		state;		/* rsrc state known to client */
1337c478bd9Sstevel@tonic-gate 	uint_t		flag;		/* flag specified for registration */
1347c478bd9Sstevel@tonic-gate 	uint_t		prv_flags;	/* currently used by rcm scripting */
1357c478bd9Sstevel@tonic-gate } client_t;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate /*
1387c478bd9Sstevel@tonic-gate  * defines for client_t:prv_flags (used by rcm scripting)
1397c478bd9Sstevel@tonic-gate  */
1407c478bd9Sstevel@tonic-gate #define	RCM_NEED_TO_UNREGISTER	1
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate /*
1437c478bd9Sstevel@tonic-gate  * Struct for a list of outstanding rcm requests
1447c478bd9Sstevel@tonic-gate  */
1457c478bd9Sstevel@tonic-gate typedef struct {
1467c478bd9Sstevel@tonic-gate 	int	n_req;
1477c478bd9Sstevel@tonic-gate 	int	n_req_max;	/* max entries in this block */
1487c478bd9Sstevel@tonic-gate 	struct {
1497c478bd9Sstevel@tonic-gate 		int	seq_num;		/* sequence number of request */
1507c478bd9Sstevel@tonic-gate 		int	state;			/* current state */
1517c478bd9Sstevel@tonic-gate 		id_t	id;			/* id of initiator */
1527c478bd9Sstevel@tonic-gate 		uint_t	flag;			/* request flags */
1537c478bd9Sstevel@tonic-gate 		int	type;			/* resource(device) type */
1547c478bd9Sstevel@tonic-gate 		char	device[MAXPATHLEN];	/* name of device or resource */
1557c478bd9Sstevel@tonic-gate 	} req[1];
1567c478bd9Sstevel@tonic-gate 	/* more entries may follow */
1577c478bd9Sstevel@tonic-gate } rcm_req_t;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate /*
1607c478bd9Sstevel@tonic-gate  * struct for describing resource tree node
1617c478bd9Sstevel@tonic-gate  */
1627c478bd9Sstevel@tonic-gate typedef struct rsrc_node {
1637c478bd9Sstevel@tonic-gate 	struct rsrc_node	*parent;
1647c478bd9Sstevel@tonic-gate 	struct rsrc_node	*sibling;
1657c478bd9Sstevel@tonic-gate 	struct rsrc_node	*child;
1667c478bd9Sstevel@tonic-gate 	char			*name;		/* phys path for devices */
1677c478bd9Sstevel@tonic-gate 	client_t		*users;		/* linked list of users */
1687c478bd9Sstevel@tonic-gate 	int			type;		/* resource type */
1697c478bd9Sstevel@tonic-gate } rsrc_node_t;
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate /*
1727c478bd9Sstevel@tonic-gate  * struct for tree action args
1737c478bd9Sstevel@tonic-gate  */
1747c478bd9Sstevel@tonic-gate typedef struct {
1757c478bd9Sstevel@tonic-gate 	int cmd;		/* command */
1767c478bd9Sstevel@tonic-gate 	int seq_num;		/* unique sequence number */
1777c478bd9Sstevel@tonic-gate 	int retcode;		/* return code */
1787c478bd9Sstevel@tonic-gate 	uint_t flag;		/* flag assoc. w command */
1797c478bd9Sstevel@tonic-gate 	timespec_t *interval;	/* for suspend command */
1807c478bd9Sstevel@tonic-gate 	nvlist_t *nvl;		/* for state changes */
1817c478bd9Sstevel@tonic-gate 	rcm_info_t **info;	/* info to be filled in */
1827c478bd9Sstevel@tonic-gate } tree_walk_arg_t;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate /*
1857c478bd9Sstevel@tonic-gate  * for synchrizing various threads
1867c478bd9Sstevel@tonic-gate  */
1877c478bd9Sstevel@tonic-gate typedef struct {
1887c478bd9Sstevel@tonic-gate 	int thr_count;
1897c478bd9Sstevel@tonic-gate 	short wanted;
1907c478bd9Sstevel@tonic-gate 	short state;
1917c478bd9Sstevel@tonic-gate 	time_t last_update;
1927c478bd9Sstevel@tonic-gate 	cond_t cv;
1937c478bd9Sstevel@tonic-gate 	mutex_t lock;
1947c478bd9Sstevel@tonic-gate } barrier_t;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate /*
1977c478bd9Sstevel@tonic-gate  * locks
1987c478bd9Sstevel@tonic-gate  */
1997c478bd9Sstevel@tonic-gate extern mutex_t rcm_req_lock;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate /*
2027c478bd9Sstevel@tonic-gate  * global variables
2037c478bd9Sstevel@tonic-gate  */
2047c478bd9Sstevel@tonic-gate extern librcm_ops_t rcm_ops;	/* ops for module callback */
2057c478bd9Sstevel@tonic-gate extern int need_cleanup;
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate /*
2087c478bd9Sstevel@tonic-gate  * comparison macros
2097c478bd9Sstevel@tonic-gate  *	EQUAL, AFTER, DESCENDENT
2107c478bd9Sstevel@tonic-gate  */
2117c478bd9Sstevel@tonic-gate #define	EQUAL(x, y)	(strcmp(x, y) == 0)
2127c478bd9Sstevel@tonic-gate #define	AFTER(x, y)	(strcmp(x, y) > 0)
2137c478bd9Sstevel@tonic-gate #define	DESCENDENT(x, y)			\
2147c478bd9Sstevel@tonic-gate 	((strlen(x) > strlen(y)) &&		\
2157c478bd9Sstevel@tonic-gate 	(strncmp(x, y, strlen(y)) == 0) &&	\
2167c478bd9Sstevel@tonic-gate 	((x[strlen(y)] == '/') ||		\
2177c478bd9Sstevel@tonic-gate 	(x[strlen(y)] == ':') ||		\
2187c478bd9Sstevel@tonic-gate 	(x[strlen(y) - 1] == '/')))
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate /*
2217c478bd9Sstevel@tonic-gate  * function prototypes
2227c478bd9Sstevel@tonic-gate  */
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate /* top level request handling routines */
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate void event_service(void **, size_t *);
2277c478bd9Sstevel@tonic-gate int process_resource_suspend(char **, pid_t, uint_t, int, timespec_t *,
2287c478bd9Sstevel@tonic-gate     rcm_info_t **);
2297c478bd9Sstevel@tonic-gate int notify_resource_resume(char **, pid_t, uint_t, int, rcm_info_t **);
2307c478bd9Sstevel@tonic-gate int process_resource_offline(char **, pid_t, uint_t, int, rcm_info_t **);
2317c478bd9Sstevel@tonic-gate int notify_resource_online(char **, pid_t, uint_t, int, rcm_info_t **);
2327c478bd9Sstevel@tonic-gate int notify_resource_remove(char **, pid_t, uint_t, int, rcm_info_t **);
2337c478bd9Sstevel@tonic-gate int add_resource_client(char *, char *, pid_t, uint_t, rcm_info_t **);
2347c478bd9Sstevel@tonic-gate int remove_resource_client(char *, char *, pid_t, uint_t);
2357c478bd9Sstevel@tonic-gate int get_resource_info(char **, uint_t, int, rcm_info_t **);
2367c478bd9Sstevel@tonic-gate int notify_resource_event(char *, pid_t, uint_t, int, nvlist_t *,
2377c478bd9Sstevel@tonic-gate     rcm_info_t **);
2387c478bd9Sstevel@tonic-gate int request_capacity_change(char *, pid_t, uint_t, int, nvlist_t *,
2397c478bd9Sstevel@tonic-gate     rcm_info_t **);
2407c478bd9Sstevel@tonic-gate int notify_capacity_change(char *, pid_t, uint_t, int, nvlist_t *,
2417c478bd9Sstevel@tonic-gate     rcm_info_t **);
2427c478bd9Sstevel@tonic-gate int get_resource_state(char *, pid_t, rcm_info_t **);
2437c478bd9Sstevel@tonic-gate rcm_info_t *rsrc_mod_info();
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate /* dr request list routines */
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate rcm_info_t *rsrc_dr_info();
2487c478bd9Sstevel@tonic-gate void clean_dr_list();
2497c478bd9Sstevel@tonic-gate int dr_req_add(char *, pid_t, uint_t, int, int, timespec_t *, rcm_info_t **);
2507c478bd9Sstevel@tonic-gate int dr_req_update(char *, pid_t, uint_t, int, int, rcm_info_t **);
2517c478bd9Sstevel@tonic-gate int dr_req_lookup(int, char *);
2527c478bd9Sstevel@tonic-gate void dr_req_remove(char *, uint_t);
2537c478bd9Sstevel@tonic-gate int info_req_add(char *, uint_t, int);
2547c478bd9Sstevel@tonic-gate void info_req_remove(int);
2557c478bd9Sstevel@tonic-gate int rsrc_check_lock_conflicts(char *, uint_t, int, rcm_info_t **);
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate /* node related routines */
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate int rsrc_get_type(const char *);
2607c478bd9Sstevel@tonic-gate int rsrc_node_find(char *, int, rsrc_node_t **);
2617c478bd9Sstevel@tonic-gate int rsrc_node_add_user(rsrc_node_t *, char *, char *, pid_t, uint_t);
2627c478bd9Sstevel@tonic-gate int rsrc_node_remove_user(rsrc_node_t *, char *, pid_t, uint_t);
2637c478bd9Sstevel@tonic-gate client_t *rsrc_client_find(char *, pid_t, client_t **);
2647c478bd9Sstevel@tonic-gate int rsrc_client_action_list(client_t *, int cmd, void *);
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate /* tree related routines */
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate int rsrc_usage_info(char **, uint_t, int, rcm_info_t **);
2697c478bd9Sstevel@tonic-gate int rsrc_tree_action(rsrc_node_t *, int, tree_walk_arg_t *);
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate /* database helpers and misc */
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate void rcmd_set_state(int);
2747c478bd9Sstevel@tonic-gate int rcmd_thr_incr(int);
2757c478bd9Sstevel@tonic-gate void rcmd_thr_decr(void);
2767c478bd9Sstevel@tonic-gate void rcmd_thr_signal(void);
2777c478bd9Sstevel@tonic-gate void rcmd_lock_init(void);
2787c478bd9Sstevel@tonic-gate void rcmd_db_init(void);
2797c478bd9Sstevel@tonic-gate void rcmd_db_sync(void);
2807c478bd9Sstevel@tonic-gate void rcmd_db_clean(void);
2817c478bd9Sstevel@tonic-gate void rcmd_start_timer(int);
2827c478bd9Sstevel@tonic-gate void rcmd_exit(int);
2837c478bd9Sstevel@tonic-gate void rcm_log_message(int, char *, ...);
2847c478bd9Sstevel@tonic-gate void rcm_log_msg(int, char *, ...);
2857c478bd9Sstevel@tonic-gate void add_busy_rsrc_to_list(char *, pid_t, int, int, char *, const char *,
2867c478bd9Sstevel@tonic-gate 	const char *, nvlist_t *, rcm_info_t **);
2877c478bd9Sstevel@tonic-gate char *resolve_name(char *);
2887c478bd9Sstevel@tonic-gate int proc_exist(pid_t);
2897c478bd9Sstevel@tonic-gate void *s_malloc(size_t);
2907c478bd9Sstevel@tonic-gate void *s_calloc(int, size_t);
2917c478bd9Sstevel@tonic-gate void *s_realloc(void *, size_t);
2927c478bd9Sstevel@tonic-gate char *s_strdup(const char *);
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate /*
2957c478bd9Sstevel@tonic-gate  * RCM queuing function prototypes
2967c478bd9Sstevel@tonic-gate  */
2977c478bd9Sstevel@tonic-gate void rcm_init_queue(rcm_queue_t *);
2987c478bd9Sstevel@tonic-gate void rcm_enqueue_head(rcm_queue_t *, rcm_queue_t *);
2997c478bd9Sstevel@tonic-gate void rcm_enqueue_tail(rcm_queue_t *, rcm_queue_t *);
3007c478bd9Sstevel@tonic-gate void rcm_enqueue(rcm_queue_t *, rcm_queue_t *);
3017c478bd9Sstevel@tonic-gate rcm_queue_t *rcm_dequeue_head(rcm_queue_t *);
3027c478bd9Sstevel@tonic-gate rcm_queue_t *rcm_dequeue_tail(rcm_queue_t *);
3037c478bd9Sstevel@tonic-gate void rcm_dequeue(rcm_queue_t *);
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate /*
3067c478bd9Sstevel@tonic-gate  * Function protoypes related to rcm scripting
3077c478bd9Sstevel@tonic-gate  */
3087c478bd9Sstevel@tonic-gate int script_main_init(void);
3097c478bd9Sstevel@tonic-gate int script_main_fini(void);
3107c478bd9Sstevel@tonic-gate struct rcm_mod_ops *script_init(module_t *);
3117c478bd9Sstevel@tonic-gate char *script_info(module_t *);
3127c478bd9Sstevel@tonic-gate int script_fini(module_t *);
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate #endif
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate #endif /* _RCM_IMPL_H */
320