xref: /illumos-gate/usr/src/cmd/rcm_daemon/common/rcm_script_impl.h (revision 012e6ce759c490003aed29439cc47d3d73a99ad3)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2000-2001 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #ifndef _RCM_SCRIPT_IMPL_H
28 #define	_RCM_SCRIPT_IMPL_H
29 
30 #ifdef	__cplusplus
31 extern "C" {
32 #endif
33 
34 #define	TRUE	1
35 #define	FALSE	0
36 
37 /* Minimum and maximum rcm scripting API version supported. */
38 #define	SCRIPT_API_MIN_VER		1
39 #define	SCRIPT_API_MAX_VER		1
40 
41 /*
42  * Default maximum time (in seconds) allocated for an rcm command
43  * before SIGABRT is sent.
44  */
45 #define	SCRIPT_CMD_TIMEOUT		60
46 
47 /*
48  * Maximum time (in seconds) allocated after sending SIGABRT before
49  * the script is killed.
50  */
51 #define	SCRIPT_ABORT_TIMEOUT		10
52 
53 /*
54  * Maximum time (in seconds) for which the rcm daemon checks whether
55  * a script is killed or not after the rcm daemon kills the script.
56  */
57 #define	SCRIPT_KILL_TIMEOUT		3
58 
59 /* Maximum number of command line parameters passed to a script */
60 #define	MAX_ARGS			16
61 
62 /* Maximum number of environment parameters passed to a script */
63 #define	MAX_ENV_PARAMS			64
64 
65 #define	MAX_LINE_LEN			(4*1024)
66 #define	MAX_FLAGS_NAME_LEN		64
67 
68 /* exit codes */
69 typedef enum {
70 	E_SUCCESS,
71 	E_FAILURE,
72 	E_UNSUPPORTED_CMD,
73 	E_REFUSE
74 } script_exit_codes_t;
75 
76 /* This structure is used to maintain a list of current dr'ed resources */
77 typedef struct {
78 	rcm_queue_t queue;
79 	char *resource_name;
80 } drreq_t;
81 
82 /*
83  * Main data structure for rcm scripting. There will be one instance of
84  * this structure for every rcm script. A pointer to this structure is
85  * kept in module structure.
86  */
87 typedef struct script_info {
88 	/*
89 	 * Used to maintain a queue of script_info structures
90 	 * Global variable script_info_q is the head of the queue.
91 	 */
92 	rcm_queue_t queue;
93 
94 	rcm_queue_t drreq_q;	/* queue head for current dr'ed resources */
95 
96 	module_t *module;
97 	rcm_handle_t *hdl;
98 
99 	char *script_full_name;	/* name of the script including path */
100 	char *script_name;	/* name of the script without path component */
101 
102 	/*
103 	 * file descriptors used to communicate with the script
104 	 * pipe1 is used to capture script's stdout
105 	 * pipe2 is used to capture script's stderr
106 	 */
107 	int pipe1[2];
108 	int pipe2[2];
109 
110 	pid_t pid;		/* process id of the script process */
111 	thread_t tid;		/* thread id of the stderr reader thread */
112 
113 	/*
114 	 * Lock to protect the fileds in this structure and also to protect
115 	 * the communication channel to the script.
116 	 */
117 	mutex_t channel_lock;
118 
119 	int ver;		/* scripting api version of the script */
120 	int cmd;		/* current rcm scripting command */
121 	int cmd_timeout;	/* timeout value in seconds */
122 	int exit_status;	/* exit status of the script */
123 
124 	/* time stamp of the script when it was last run */
125 	time_t lastrun;
126 
127 	char *func_info_buf;
128 	char *func_info_buf_curptr;
129 	int func_info_buf_len;
130 
131 	char *resource_usage_info_buf;
132 	char *resource_usage_info_buf_curptr;
133 	int resource_usage_info_buf_len;
134 
135 	char *failure_reason_buf;
136 	char *failure_reason_buf_curptr;
137 	int failure_reason_buf_len;
138 	uint_t flags;
139 } script_info_t;
140 
141 /*
142  * script_info_t:flags
143  */
144 #define	STDERR_THREAD_CREATED	1
145 
146 #define	PARENT_END_OF_PIPE	0
147 #define	CHILD_END_OF_PIPE	1
148 
149 #define	PS_STATE_FILE_VER	1
150 
151 typedef struct state_element {
152 	uint32_t flags;
153 	uint32_t reserved;	/* for 64 bit alignment */
154 	/* followed by actual state element */
155 } state_element_t;
156 
157 /*
158  * state_element_t:flags
159  * The following flag when set indicates that the state element is
160  * currently in use. When not set indicates that the state element is free.
161  */
162 #define	STATE_ELEMENT_IN_USE	0x1
163 
164 /*
165  * This structure defines the layout of state file used by rcm scripting
166  */
167 typedef struct state_file {
168 	uint32_t version;
169 	uint32_t max_elements;	/* number of state elements */
170 	/* followed by an array of state elements of type state_element_t */
171 } state_file_t;
172 
173 typedef struct state_file_descr {
174 	uint32_t version;
175 	int fd;			/* file descriptor to the state file */
176 	size_t element_size;	/* size of one state element */
177 
178 	/*
179 	 * number of state elements to allocate at a time when the state file
180 	 * grows.
181 	 */
182 	int chunk_size;
183 
184 	/*
185 	 * index into the state element array where the next search will
186 	 * begin for an empty slot.
187 	 */
188 	int index;
189 
190 	/* pointer to mmapped state file */
191 	state_file_t *state_file;
192 } state_file_descr_t;
193 
194 /* round up to n byte boundary. n must be power of 2 for this macro to work */
195 #define	RSCR_ROUNDUP(x, n)	(((x) + ((n) - 1)) & (~((n) - 1)))
196 
197 typedef struct ps_state_element {
198 	pid_t pid;
199 	char script_name[MAXNAMELEN];
200 } ps_state_element_t;
201 
202 /* maximum number of additional env variables for capacity specific stuff */
203 #define	MAX_CAPACITY_PARAMS	10
204 
205 typedef struct capacity_descr {
206 	char *resource_name;
207 	int match_type;
208 	struct {
209 		char *nvname;
210 		char *envname;
211 	} param[MAX_CAPACITY_PARAMS];
212 } capacity_descr_t;
213 
214 /* capacity_descr_t:match_type */
215 #define	MATCH_INVALID		0
216 #define	MATCH_EXACT		1
217 #define	MATCH_PREFIX		2
218 
219 #ifdef	__cplusplus
220 }
221 #endif
222 
223 #endif /* _RCM_SCRIPT_IMPL_H */
224