1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*
28*7c478bd9Sstevel@tonic-gate  * rcm scripting module:
29*7c478bd9Sstevel@tonic-gate  *
30*7c478bd9Sstevel@tonic-gate  * This module implements rcm scripting interfaces.
31*7c478bd9Sstevel@tonic-gate  * It translates rcm module based interfaces to rcm script based
32*7c478bd9Sstevel@tonic-gate  * interfaces.
33*7c478bd9Sstevel@tonic-gate  *
34*7c478bd9Sstevel@tonic-gate  * Entry points:
35*7c478bd9Sstevel@tonic-gate  *
36*7c478bd9Sstevel@tonic-gate  *   int script_main_init()
37*7c478bd9Sstevel@tonic-gate  *	Initialize the rcm scripting framework.
38*7c478bd9Sstevel@tonic-gate  *	Called during the rcm daemon initialization
39*7c478bd9Sstevel@tonic-gate  *
40*7c478bd9Sstevel@tonic-gate  *   int script_main_fini()
41*7c478bd9Sstevel@tonic-gate  *	Called at the time of the rcm daemon exit.
42*7c478bd9Sstevel@tonic-gate  *
43*7c478bd9Sstevel@tonic-gate  *   struct rcm_mod_ops *script_init(module_t *module)
44*7c478bd9Sstevel@tonic-gate  *	Initialize the given script.
45*7c478bd9Sstevel@tonic-gate  *	module->name contains the name of the script.
46*7c478bd9Sstevel@tonic-gate  *	Called at the time of loading scripts.
47*7c478bd9Sstevel@tonic-gate  *	Semantics are similar to module init.
48*7c478bd9Sstevel@tonic-gate  *
49*7c478bd9Sstevel@tonic-gate  *   char *script_info(module_t *module)
50*7c478bd9Sstevel@tonic-gate  *	Called when the rcm daemon wishes to get the script information.
51*7c478bd9Sstevel@tonic-gate  *	module->name contains the name of the script.
52*7c478bd9Sstevel@tonic-gate  *	Semantics are similar to module info.
53*7c478bd9Sstevel@tonic-gate  *
54*7c478bd9Sstevel@tonic-gate  *   int script_fini(module_t *module)
55*7c478bd9Sstevel@tonic-gate  *	Called before removing the script.
56*7c478bd9Sstevel@tonic-gate  *	module->name contains the name of the script.
57*7c478bd9Sstevel@tonic-gate  *	Semantics are similar to module fini.
58*7c478bd9Sstevel@tonic-gate  *
59*7c478bd9Sstevel@tonic-gate  * In addition to the above entry points rcm_mod_ops structure contains
60*7c478bd9Sstevel@tonic-gate  * the other entry points. A pointer to this structure is returned when
61*7c478bd9Sstevel@tonic-gate  * script_init() is called.
62*7c478bd9Sstevel@tonic-gate  */
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate #include "rcm_impl.h"
65*7c478bd9Sstevel@tonic-gate #include "rcm_script_impl.h"
66*7c478bd9Sstevel@tonic-gate #include <sys/resource.h>
67*7c478bd9Sstevel@tonic-gate #include <procfs.h>
68*7c478bd9Sstevel@tonic-gate #include <sys/proc.h>
69*7c478bd9Sstevel@tonic-gate #include <ctype.h>
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate /*
72*7c478bd9Sstevel@tonic-gate  * All rcm scripting commands are enumerated here.
73*7c478bd9Sstevel@tonic-gate  * NOTE: command positions in script_cmd_id_t and script_cmd_name must match.
74*7c478bd9Sstevel@tonic-gate  */
75*7c478bd9Sstevel@tonic-gate typedef enum {
76*7c478bd9Sstevel@tonic-gate 	C_SCRIPTINFO,
77*7c478bd9Sstevel@tonic-gate 	C_RESOURCEINFO,
78*7c478bd9Sstevel@tonic-gate 	C_REGISTER,
79*7c478bd9Sstevel@tonic-gate 	C_QUERYREMOVE,
80*7c478bd9Sstevel@tonic-gate 	C_PREREMOVE,
81*7c478bd9Sstevel@tonic-gate 	C_POSTREMOVE,
82*7c478bd9Sstevel@tonic-gate 	C_UNDOREMOVE,
83*7c478bd9Sstevel@tonic-gate 	C_QUERYCAPACITY,
84*7c478bd9Sstevel@tonic-gate 	C_PRECAPACITY,
85*7c478bd9Sstevel@tonic-gate 	C_POSTCAPACITY,
86*7c478bd9Sstevel@tonic-gate 	C_QUERYSUSPEND,
87*7c478bd9Sstevel@tonic-gate 	C_PRESUSPEND,
88*7c478bd9Sstevel@tonic-gate 	C_POSTRESUME,
89*7c478bd9Sstevel@tonic-gate 	C_CANCELSUSPEND
90*7c478bd9Sstevel@tonic-gate } script_cmd_id_t;
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate /* NOTE: command positions in script_cmd_id_t and script_cmd_name must match */
93*7c478bd9Sstevel@tonic-gate static char *script_cmd_name[] = {
94*7c478bd9Sstevel@tonic-gate 	"scriptinfo",
95*7c478bd9Sstevel@tonic-gate 	"resourceinfo",
96*7c478bd9Sstevel@tonic-gate 	"register",
97*7c478bd9Sstevel@tonic-gate 	"queryremove",
98*7c478bd9Sstevel@tonic-gate 	"preremove",
99*7c478bd9Sstevel@tonic-gate 	"postremove",
100*7c478bd9Sstevel@tonic-gate 	"undoremove",
101*7c478bd9Sstevel@tonic-gate 	"querycapacity",
102*7c478bd9Sstevel@tonic-gate 	"precapacity",
103*7c478bd9Sstevel@tonic-gate 	"postcapacity",
104*7c478bd9Sstevel@tonic-gate 	"querysuspend",
105*7c478bd9Sstevel@tonic-gate 	"presuspend",
106*7c478bd9Sstevel@tonic-gate 	"postresume",
107*7c478bd9Sstevel@tonic-gate 	"cancelsuspend",
108*7c478bd9Sstevel@tonic-gate 	NULL
109*7c478bd9Sstevel@tonic-gate };
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate /*
112*7c478bd9Sstevel@tonic-gate  * All rcm scripting data items are enumerated here.
113*7c478bd9Sstevel@tonic-gate  * NOTE: data item positions in script_data_item_id_t and
114*7c478bd9Sstevel@tonic-gate  * script_data_item_name must match.
115*7c478bd9Sstevel@tonic-gate  */
116*7c478bd9Sstevel@tonic-gate typedef	enum {
117*7c478bd9Sstevel@tonic-gate 	D_SCRIPT_VERSION,
118*7c478bd9Sstevel@tonic-gate 	D_SCRIPT_FUNC_INFO,
119*7c478bd9Sstevel@tonic-gate 	D_CMD_TIMEOUT,
120*7c478bd9Sstevel@tonic-gate 	D_RESOURCE_NAME,
121*7c478bd9Sstevel@tonic-gate 	D_RESOURCE_USAGE_INFO,
122*7c478bd9Sstevel@tonic-gate 	D_FAILURE_REASON,
123*7c478bd9Sstevel@tonic-gate 	D_LOG_ERR,
124*7c478bd9Sstevel@tonic-gate 	D_LOG_WARN,
125*7c478bd9Sstevel@tonic-gate 	D_LOG_INFO,
126*7c478bd9Sstevel@tonic-gate 	D_LOG_DEBUG
127*7c478bd9Sstevel@tonic-gate } script_data_item_id_t;
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate /*
130*7c478bd9Sstevel@tonic-gate  * NOTE: data item positions in script_data_item_id_t and
131*7c478bd9Sstevel@tonic-gate  * script_data_item_name must match.
132*7c478bd9Sstevel@tonic-gate  */
133*7c478bd9Sstevel@tonic-gate static const char *script_data_item_name[] = {
134*7c478bd9Sstevel@tonic-gate 	"rcm_script_version",
135*7c478bd9Sstevel@tonic-gate 	"rcm_script_func_info",
136*7c478bd9Sstevel@tonic-gate 	"rcm_cmd_timeout",
137*7c478bd9Sstevel@tonic-gate 	"rcm_resource_name",
138*7c478bd9Sstevel@tonic-gate 	"rcm_resource_usage_info",
139*7c478bd9Sstevel@tonic-gate 	"rcm_failure_reason",
140*7c478bd9Sstevel@tonic-gate 	"rcm_log_err",
141*7c478bd9Sstevel@tonic-gate 	"rcm_log_warn",
142*7c478bd9Sstevel@tonic-gate 	"rcm_log_info",
143*7c478bd9Sstevel@tonic-gate 	"rcm_log_debug",
144*7c478bd9Sstevel@tonic-gate 	NULL
145*7c478bd9Sstevel@tonic-gate };
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate /*
148*7c478bd9Sstevel@tonic-gate  * Maximum number of rcm scripts that can run in parallel.
149*7c478bd9Sstevel@tonic-gate  * RCM daemon has no limit on the number of scripts supported. But
150*7c478bd9Sstevel@tonic-gate  * at most it runs script_max_parallelism number of scripts in parallel.
151*7c478bd9Sstevel@tonic-gate  * For each running script rcm daemon consumes two file descriptors
152*7c478bd9Sstevel@tonic-gate  * in order to communicate with the script via pipes.
153*7c478bd9Sstevel@tonic-gate  * So maximum number of file descriptor entries consumed by rcm daemon
154*7c478bd9Sstevel@tonic-gate  * on behalf of rcm scripts is "script_max_parallelism * 2"
155*7c478bd9Sstevel@tonic-gate  */
156*7c478bd9Sstevel@tonic-gate static const int script_max_parallelism = 64;
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate /*
159*7c478bd9Sstevel@tonic-gate  * semaphore to limit the number of rcm script processes running in
160*7c478bd9Sstevel@tonic-gate  * parallel to script_max_parallelism.
161*7c478bd9Sstevel@tonic-gate  */
162*7c478bd9Sstevel@tonic-gate static sema_t script_process_sema;
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate /* mutex to protect the any global data */
165*7c478bd9Sstevel@tonic-gate static mutex_t script_lock;
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate /* contains head to a queue of script_info structures */
168*7c478bd9Sstevel@tonic-gate static rcm_queue_t script_info_q;
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate /*
171*7c478bd9Sstevel@tonic-gate  * This mmapped state file is used to store the process id and
172*7c478bd9Sstevel@tonic-gate  * rcm script name of all currently running rcm scripts.
173*7c478bd9Sstevel@tonic-gate  */
174*7c478bd9Sstevel@tonic-gate static const char *script_ps_state_file = "/var/run/rcm_script_state";
175*7c478bd9Sstevel@tonic-gate static state_file_descr_t script_ps_statefd;
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate static char *script_env_noforce = "RCM_ENV_FORCE=FALSE";
178*7c478bd9Sstevel@tonic-gate static char *script_env_force = "RCM_ENV_FORCE=TRUE";
179*7c478bd9Sstevel@tonic-gate static char *script_env_interval = "RCM_ENV_INTERVAL=%ld";
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate #define	RSCR_TRACE		RCM_TRACE1
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate /* rcm script base environment */
184*7c478bd9Sstevel@tonic-gate static char *script_env[MAX_ENV_PARAMS];
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate struct rlimit file_limit;
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate /* function prototypes */
189*7c478bd9Sstevel@tonic-gate static void build_env(void);
190*7c478bd9Sstevel@tonic-gate static void copy_env(char *[], char *[]);
191*7c478bd9Sstevel@tonic-gate static void open_state_file(const char *, state_file_descr_t *, size_t, int,
192*7c478bd9Sstevel@tonic-gate 	uint32_t);
193*7c478bd9Sstevel@tonic-gate static void truncate_state_file(state_file_descr_t *);
194*7c478bd9Sstevel@tonic-gate static void close_state_file(const char *, state_file_descr_t *);
195*7c478bd9Sstevel@tonic-gate static void grow_state_file(state_file_descr_t *);
196*7c478bd9Sstevel@tonic-gate static void *get_state_element(state_file_descr_t *, int, int *);
197*7c478bd9Sstevel@tonic-gate static void *allocate_state_element(state_file_descr_t *, int *);
198*7c478bd9Sstevel@tonic-gate static void free_state_element(void *);
199*7c478bd9Sstevel@tonic-gate static void script_ps_state_file_kill_pids(void);
200*7c478bd9Sstevel@tonic-gate static void script_ps_state_file_add_entry(pid_t, char *);
201*7c478bd9Sstevel@tonic-gate static void script_ps_state_file_remove_entry(pid_t);
202*7c478bd9Sstevel@tonic-gate static int dname_to_id(char *);
203*7c478bd9Sstevel@tonic-gate static void script_process_sema_wait(void);
204*7c478bd9Sstevel@tonic-gate static int run_script(script_info_t *, char *[], char *[], char **);
205*7c478bd9Sstevel@tonic-gate static int get_line(int fd, char *, char *, int, size_t *, time_t, int *);
206*7c478bd9Sstevel@tonic-gate static void script_exited(script_info_t *);
207*7c478bd9Sstevel@tonic-gate static int kill_pid(pid_t);
208*7c478bd9Sstevel@tonic-gate static void kill_script(script_info_t *);
209*7c478bd9Sstevel@tonic-gate static char *flags_to_name(int, char *, int);
210*7c478bd9Sstevel@tonic-gate static void fill_argv(script_info_t *, char *[], char *);
211*7c478bd9Sstevel@tonic-gate static void *read_stderr(script_info_t *);
212*7c478bd9Sstevel@tonic-gate static int process_dataitem(script_info_t *, int, char *, char **);
213*7c478bd9Sstevel@tonic-gate static int do_cmd(script_info_t *, char *[], char *[], char **);
214*7c478bd9Sstevel@tonic-gate static int do_script_info(script_info_t *);
215*7c478bd9Sstevel@tonic-gate static int do_dr(script_info_t *, char *[], char *[], char **);
216*7c478bd9Sstevel@tonic-gate static int script_get_info(rcm_handle_t *, char *, pid_t, uint_t, char **,
217*7c478bd9Sstevel@tonic-gate 	char **, nvlist_t *, rcm_info_t **);
218*7c478bd9Sstevel@tonic-gate static void add_for_unregister(script_info_t *);
219*7c478bd9Sstevel@tonic-gate static void remove_from_unregister(script_info_t *, char *);
220*7c478bd9Sstevel@tonic-gate static void complete_unregister(script_info_t *);
221*7c478bd9Sstevel@tonic-gate static int script_register_interest(rcm_handle_t *);
222*7c478bd9Sstevel@tonic-gate static void add_drreq(script_info_t *, char *);
223*7c478bd9Sstevel@tonic-gate static void remove_drreq(script_info_t *, char *);
224*7c478bd9Sstevel@tonic-gate static void remove_drreq_all(script_info_t *);
225*7c478bd9Sstevel@tonic-gate static int script_request_offline(rcm_handle_t *, char *, pid_t, uint_t,
226*7c478bd9Sstevel@tonic-gate 	char **, rcm_info_t **);
227*7c478bd9Sstevel@tonic-gate static int script_notify_online(rcm_handle_t *, char *, pid_t, uint_t,
228*7c478bd9Sstevel@tonic-gate 	char **, rcm_info_t **);
229*7c478bd9Sstevel@tonic-gate static int script_notify_remove(rcm_handle_t *, char *, pid_t, uint_t,
230*7c478bd9Sstevel@tonic-gate 	char **, rcm_info_t **);
231*7c478bd9Sstevel@tonic-gate static int script_request_suspend(rcm_handle_t *, char *, pid_t, timespec_t *,
232*7c478bd9Sstevel@tonic-gate 	uint_t, char **, rcm_info_t **);
233*7c478bd9Sstevel@tonic-gate static int script_notify_resume(rcm_handle_t *, char *, pid_t, uint_t,
234*7c478bd9Sstevel@tonic-gate 	char **, rcm_info_t **);
235*7c478bd9Sstevel@tonic-gate static capacity_descr_t *get_capacity_descr(char *);
236*7c478bd9Sstevel@tonic-gate static int build_env_for_capacity(script_info_t *, char *, uint_t, nvlist_t *,
237*7c478bd9Sstevel@tonic-gate 	char *[], int *, char **);
238*7c478bd9Sstevel@tonic-gate static int script_request_capacity_change(rcm_handle_t *, char *, pid_t,
239*7c478bd9Sstevel@tonic-gate 	uint_t, nvlist_t *, char **, rcm_info_t **);
240*7c478bd9Sstevel@tonic-gate static int script_notify_capacity_change(rcm_handle_t *, char *, pid_t,
241*7c478bd9Sstevel@tonic-gate 	uint_t, nvlist_t *, char **, rcm_info_t **);
242*7c478bd9Sstevel@tonic-gate static void log_msg(script_info_t *, int, char *);
243*7c478bd9Sstevel@tonic-gate static char *dup_err(int, char *, ...);
244*7c478bd9Sstevel@tonic-gate static void rcmscript_snprintf(char **, int *, char **, char *, ...);
245*7c478bd9Sstevel@tonic-gate static char *rcmscript_strdup(char *);
246*7c478bd9Sstevel@tonic-gate static void *rcmscript_malloc(size_t);
247*7c478bd9Sstevel@tonic-gate static void *rcmscript_calloc(size_t, size_t);
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate static struct rcm_mod_ops script_ops =
251*7c478bd9Sstevel@tonic-gate {
252*7c478bd9Sstevel@tonic-gate 	RCM_MOD_OPS_VERSION,
253*7c478bd9Sstevel@tonic-gate 	script_register_interest, /* register */
254*7c478bd9Sstevel@tonic-gate 	script_register_interest, /* unregister */
255*7c478bd9Sstevel@tonic-gate 	script_get_info,
256*7c478bd9Sstevel@tonic-gate 	script_request_suspend,
257*7c478bd9Sstevel@tonic-gate 	script_notify_resume,
258*7c478bd9Sstevel@tonic-gate 	script_request_offline,
259*7c478bd9Sstevel@tonic-gate 	script_notify_online,
260*7c478bd9Sstevel@tonic-gate 	script_notify_remove,
261*7c478bd9Sstevel@tonic-gate 	script_request_capacity_change,
262*7c478bd9Sstevel@tonic-gate 	script_notify_capacity_change,
263*7c478bd9Sstevel@tonic-gate 	NULL
264*7c478bd9Sstevel@tonic-gate };
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate /*
267*7c478bd9Sstevel@tonic-gate  * Messages fall into two categories:
268*7c478bd9Sstevel@tonic-gate  *   framework messages (MF_..)
269*7c478bd9Sstevel@tonic-gate  *   errors directly attributable to scripts (MS_..)
270*7c478bd9Sstevel@tonic-gate  */
271*7c478bd9Sstevel@tonic-gate #define	MF_MEMORY_ALLOCATION_ERR \
272*7c478bd9Sstevel@tonic-gate 	gettext("rcm: failed to allocate memory: %1$s\n")
273*7c478bd9Sstevel@tonic-gate #define	MF_STATE_FILE_ERR \
274*7c478bd9Sstevel@tonic-gate 	gettext("rcm: state file error: %1$s: %2$s\n")
275*7c478bd9Sstevel@tonic-gate #define	MF_FUNC_CALL_ERR \
276*7c478bd9Sstevel@tonic-gate 	gettext("rcm: %1$s: %2$s\n")
277*7c478bd9Sstevel@tonic-gate #define	MF_NV_ERR \
278*7c478bd9Sstevel@tonic-gate 	gettext("rcm: required name-value parameters missing (%1$s)\n")
279*7c478bd9Sstevel@tonic-gate #define	MF_UNKNOWN_RSRC_ERR \
280*7c478bd9Sstevel@tonic-gate 	gettext("rcm: unknown resource name %1$s (%2$s)\n")
281*7c478bd9Sstevel@tonic-gate #define	MS_REGISTER_RSRC_ERR \
282*7c478bd9Sstevel@tonic-gate 	gettext("rcm script %1$s: failed to register %2$s\n")
283*7c478bd9Sstevel@tonic-gate #define	MS_REGISTER_ERR \
284*7c478bd9Sstevel@tonic-gate 	gettext("rcm script %1$s: register: %2$s\n")
285*7c478bd9Sstevel@tonic-gate #define	MS_SCRIPTINFO_ERR \
286*7c478bd9Sstevel@tonic-gate 	gettext("rcm script %1$s: scriptinfo: %2$s\n")
287*7c478bd9Sstevel@tonic-gate #define	MS_PROTOCOL_ERR \
288*7c478bd9Sstevel@tonic-gate 	gettext("rcm script %1$s: scripting protocol error\n")
289*7c478bd9Sstevel@tonic-gate #define	MS_TIMEOUT_ERR \
290*7c478bd9Sstevel@tonic-gate 	gettext("rcm script %1$s: timeout error\n")
291*7c478bd9Sstevel@tonic-gate #define	MS_UNSUPPORTED_VER \
292*7c478bd9Sstevel@tonic-gate 	gettext("rcm script %1$s: unsupported version %2$d\n")
293*7c478bd9Sstevel@tonic-gate #define	MS_SCRIPT_ERR \
294*7c478bd9Sstevel@tonic-gate 	gettext("rcm script %1$s: error: %2$s\n")
295*7c478bd9Sstevel@tonic-gate #define	MS_UNKNOWN_ERR \
296*7c478bd9Sstevel@tonic-gate 	gettext("rcm script %1$s: unknown error\n")
297*7c478bd9Sstevel@tonic-gate #define	MS_LOG_MSG \
298*7c478bd9Sstevel@tonic-gate 	gettext("rcm script %1$s: %2$s\n")
299*7c478bd9Sstevel@tonic-gate 
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate /*
302*7c478bd9Sstevel@tonic-gate  * Initialize rcm scripting framework.
303*7c478bd9Sstevel@tonic-gate  * Called during initialization of rcm daemon.
304*7c478bd9Sstevel@tonic-gate  */
305*7c478bd9Sstevel@tonic-gate int
script_main_init(void)306*7c478bd9Sstevel@tonic-gate script_main_init(void)
307*7c478bd9Sstevel@tonic-gate {
308*7c478bd9Sstevel@tonic-gate #define	PS_STATE_FILE_CHUNK_SIZE	32
309*7c478bd9Sstevel@tonic-gate 
310*7c478bd9Sstevel@tonic-gate 	/* set base script environment */
311*7c478bd9Sstevel@tonic-gate 	build_env();
312*7c478bd9Sstevel@tonic-gate 
313*7c478bd9Sstevel@tonic-gate 	rcm_init_queue(&script_info_q);
314*7c478bd9Sstevel@tonic-gate 
315*7c478bd9Sstevel@tonic-gate 	/*
316*7c478bd9Sstevel@tonic-gate 	 * Initialize the semaphore to limit the number of rcm script
317*7c478bd9Sstevel@tonic-gate 	 * process running in parallel to script_max_parallelism.
318*7c478bd9Sstevel@tonic-gate 	 */
319*7c478bd9Sstevel@tonic-gate 	(void) sema_init(&script_process_sema, script_max_parallelism,
320*7c478bd9Sstevel@tonic-gate 			USYNC_THREAD, NULL);
321*7c478bd9Sstevel@tonic-gate 
322*7c478bd9Sstevel@tonic-gate 	(void) mutex_init(&script_lock, USYNC_THREAD, NULL);
323*7c478bd9Sstevel@tonic-gate 
324*7c478bd9Sstevel@tonic-gate 	/* save original file limit */
325*7c478bd9Sstevel@tonic-gate 	(void) getrlimit(RLIMIT_NOFILE, &file_limit);
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate 	open_state_file(script_ps_state_file, &script_ps_statefd,
328*7c478bd9Sstevel@tonic-gate 		sizeof (ps_state_element_t),
329*7c478bd9Sstevel@tonic-gate 		PS_STATE_FILE_CHUNK_SIZE,
330*7c478bd9Sstevel@tonic-gate 		PS_STATE_FILE_VER);
331*7c478bd9Sstevel@tonic-gate 
332*7c478bd9Sstevel@tonic-gate 	/*
333*7c478bd9Sstevel@tonic-gate 	 * If any pids exist in the ps state file since the last incarnation of
334*7c478bd9Sstevel@tonic-gate 	 * the rcm daemon, kill the pids.
335*7c478bd9Sstevel@tonic-gate 	 * On a normal daemon exit no pids should exist in the ps state file.
336*7c478bd9Sstevel@tonic-gate 	 * But on an abnormal daemon exit pids may exist in the ps state file.
337*7c478bd9Sstevel@tonic-gate 	 */
338*7c478bd9Sstevel@tonic-gate 	if (script_ps_statefd.state_file) {
339*7c478bd9Sstevel@tonic-gate 		script_ps_state_file_kill_pids();
340*7c478bd9Sstevel@tonic-gate 		truncate_state_file(&script_ps_statefd);
341*7c478bd9Sstevel@tonic-gate 	}
342*7c478bd9Sstevel@tonic-gate 
343*7c478bd9Sstevel@tonic-gate 	return (0);
344*7c478bd9Sstevel@tonic-gate }
345*7c478bd9Sstevel@tonic-gate 
346*7c478bd9Sstevel@tonic-gate /*
347*7c478bd9Sstevel@tonic-gate  * Do any cleanup.
348*7c478bd9Sstevel@tonic-gate  * Called at the time of normal rcm daemon exit.
349*7c478bd9Sstevel@tonic-gate  */
350*7c478bd9Sstevel@tonic-gate int
script_main_fini(void)351*7c478bd9Sstevel@tonic-gate script_main_fini(void)
352*7c478bd9Sstevel@tonic-gate {
353*7c478bd9Sstevel@tonic-gate 	script_ps_state_file_kill_pids();
354*7c478bd9Sstevel@tonic-gate 	close_state_file(script_ps_state_file, &script_ps_statefd);
355*7c478bd9Sstevel@tonic-gate 	return (0);
356*7c478bd9Sstevel@tonic-gate }
357*7c478bd9Sstevel@tonic-gate 
358*7c478bd9Sstevel@tonic-gate /*
359*7c478bd9Sstevel@tonic-gate  * Initialize the given rcm script.
360*7c478bd9Sstevel@tonic-gate  * module->name contains the name of the rcm script.
361*7c478bd9Sstevel@tonic-gate  */
362*7c478bd9Sstevel@tonic-gate struct rcm_mod_ops *
script_init(module_t * module)363*7c478bd9Sstevel@tonic-gate script_init(module_t *module)
364*7c478bd9Sstevel@tonic-gate {
365*7c478bd9Sstevel@tonic-gate 	script_info_t *rsi;
366*7c478bd9Sstevel@tonic-gate 	size_t len;
367*7c478bd9Sstevel@tonic-gate 	char *script_path;
368*7c478bd9Sstevel@tonic-gate 
369*7c478bd9Sstevel@tonic-gate 	rcm_log_message(RSCR_TRACE, "script_init: script name = %s\n",
370*7c478bd9Sstevel@tonic-gate 						module->name);
371*7c478bd9Sstevel@tonic-gate 
372*7c478bd9Sstevel@tonic-gate 	module->rsi = NULL;
373*7c478bd9Sstevel@tonic-gate 
374*7c478bd9Sstevel@tonic-gate 	if ((script_path = rcm_get_script_dir(module->name)) == NULL)
375*7c478bd9Sstevel@tonic-gate 		return (NULL);
376*7c478bd9Sstevel@tonic-gate 
377*7c478bd9Sstevel@tonic-gate 	len = strlen(script_path) + strlen(module->name) + 2;
378*7c478bd9Sstevel@tonic-gate 
379*7c478bd9Sstevel@tonic-gate 	/* calloc also zeros the contents */
380*7c478bd9Sstevel@tonic-gate 	rsi = (script_info_t *)rcmscript_calloc(1, sizeof (script_info_t));
381*7c478bd9Sstevel@tonic-gate 	rsi->script_full_name = (char *)rcmscript_calloc(1, len);
382*7c478bd9Sstevel@tonic-gate 
383*7c478bd9Sstevel@tonic-gate 	rsi->module = module;
384*7c478bd9Sstevel@tonic-gate 	rcm_init_queue(&rsi->drreq_q);
385*7c478bd9Sstevel@tonic-gate 
386*7c478bd9Sstevel@tonic-gate 	(void) mutex_init(&rsi->channel_lock, USYNC_THREAD, NULL);
387*7c478bd9Sstevel@tonic-gate 
388*7c478bd9Sstevel@tonic-gate 	(void) snprintf(rsi->script_full_name, len, "%s%s", script_path,
389*7c478bd9Sstevel@tonic-gate 			module->name);
390*7c478bd9Sstevel@tonic-gate 	rsi->script_name = strrchr(rsi->script_full_name, '/') + 1;
391*7c478bd9Sstevel@tonic-gate 
392*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&rsi->channel_lock);
393*7c478bd9Sstevel@tonic-gate 
394*7c478bd9Sstevel@tonic-gate 	rsi->cmd_timeout = -1; /* don't time scriptinfo command */
395*7c478bd9Sstevel@tonic-gate 	if (do_script_info(rsi) == RCM_SUCCESS) {
396*7c478bd9Sstevel@tonic-gate 		/*
397*7c478bd9Sstevel@tonic-gate 		 * if the script hasn't specified a timeout value set it to
398*7c478bd9Sstevel@tonic-gate 		 * default
399*7c478bd9Sstevel@tonic-gate 		 */
400*7c478bd9Sstevel@tonic-gate 		if (rsi->cmd_timeout == -1)
401*7c478bd9Sstevel@tonic-gate 			rsi->cmd_timeout = SCRIPT_CMD_TIMEOUT;
402*7c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&rsi->channel_lock);
403*7c478bd9Sstevel@tonic-gate 
404*7c478bd9Sstevel@tonic-gate 		/* put rsi on script_info_q */
405*7c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&script_lock);
406*7c478bd9Sstevel@tonic-gate 		rcm_enqueue_tail(&script_info_q, &rsi->queue);
407*7c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&script_lock);
408*7c478bd9Sstevel@tonic-gate 
409*7c478bd9Sstevel@tonic-gate 		module->rsi = rsi;
410*7c478bd9Sstevel@tonic-gate 		return (&script_ops);
411*7c478bd9Sstevel@tonic-gate 	}
412*7c478bd9Sstevel@tonic-gate 
413*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&rsi->channel_lock);
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate 	free(rsi->script_full_name);
416*7c478bd9Sstevel@tonic-gate 	free(rsi);
417*7c478bd9Sstevel@tonic-gate 	return (NULL);
418*7c478bd9Sstevel@tonic-gate }
419*7c478bd9Sstevel@tonic-gate 
420*7c478bd9Sstevel@tonic-gate /*
421*7c478bd9Sstevel@tonic-gate  * Returns a string describing the script's functionality.
422*7c478bd9Sstevel@tonic-gate  * module->name contains the name of the rcm script for which information
423*7c478bd9Sstevel@tonic-gate  * is requested.
424*7c478bd9Sstevel@tonic-gate  */
425*7c478bd9Sstevel@tonic-gate char *
script_info(module_t * module)426*7c478bd9Sstevel@tonic-gate script_info(module_t *module)
427*7c478bd9Sstevel@tonic-gate {
428*7c478bd9Sstevel@tonic-gate 	script_info_t *rsi = module->rsi;
429*7c478bd9Sstevel@tonic-gate 
430*7c478bd9Sstevel@tonic-gate 	rcm_log_message(RSCR_TRACE, "script_info: script name = %s\n",
431*7c478bd9Sstevel@tonic-gate 						rsi->script_name);
432*7c478bd9Sstevel@tonic-gate 	return (rsi->func_info_buf);
433*7c478bd9Sstevel@tonic-gate }
434*7c478bd9Sstevel@tonic-gate 
435*7c478bd9Sstevel@tonic-gate /*
436*7c478bd9Sstevel@tonic-gate  * Called before unloading the script.
437*7c478bd9Sstevel@tonic-gate  * module->name contains the name of the rcm script which is being unloaded.
438*7c478bd9Sstevel@tonic-gate  * Do any cleanup.
439*7c478bd9Sstevel@tonic-gate  */
440*7c478bd9Sstevel@tonic-gate int
script_fini(module_t * module)441*7c478bd9Sstevel@tonic-gate script_fini(module_t *module)
442*7c478bd9Sstevel@tonic-gate {
443*7c478bd9Sstevel@tonic-gate 	script_info_t *rsi = module->rsi;
444*7c478bd9Sstevel@tonic-gate 
445*7c478bd9Sstevel@tonic-gate 	rcm_log_message(RSCR_TRACE, "script_fini: script name = %s\n",
446*7c478bd9Sstevel@tonic-gate 						rsi->script_name);
447*7c478bd9Sstevel@tonic-gate 
448*7c478bd9Sstevel@tonic-gate 	/* remove rsi from script_info_q */
449*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&script_lock);
450*7c478bd9Sstevel@tonic-gate 	rcm_dequeue(&rsi->queue);
451*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&script_lock);
452*7c478bd9Sstevel@tonic-gate 
453*7c478bd9Sstevel@tonic-gate 	remove_drreq_all(rsi);
454*7c478bd9Sstevel@tonic-gate 
455*7c478bd9Sstevel@tonic-gate 	if (rsi->func_info_buf)
456*7c478bd9Sstevel@tonic-gate 		free(rsi->func_info_buf);
457*7c478bd9Sstevel@tonic-gate 
458*7c478bd9Sstevel@tonic-gate 	free(rsi->script_full_name);
459*7c478bd9Sstevel@tonic-gate 	free(rsi);
460*7c478bd9Sstevel@tonic-gate 
461*7c478bd9Sstevel@tonic-gate 	module->rsi = NULL;
462*7c478bd9Sstevel@tonic-gate 
463*7c478bd9Sstevel@tonic-gate 	return (RCM_SUCCESS);
464*7c478bd9Sstevel@tonic-gate }
465*7c478bd9Sstevel@tonic-gate 
466*7c478bd9Sstevel@tonic-gate /* build base environment for scripts */
467*7c478bd9Sstevel@tonic-gate static void
build_env(void)468*7c478bd9Sstevel@tonic-gate build_env(void)
469*7c478bd9Sstevel@tonic-gate {
470*7c478bd9Sstevel@tonic-gate 	const char *env_list[] = { "LANG", "LC_COLLATE", "LC_CTYPE",
471*7c478bd9Sstevel@tonic-gate 		"LC_MESSAGES", "LC_MONETARY", "LC_NUMERIC", "LC_TIME",
472*7c478bd9Sstevel@tonic-gate 		"LC_ALL", "TZ", NULL };
473*7c478bd9Sstevel@tonic-gate 	char *x;
474*7c478bd9Sstevel@tonic-gate 	int len;
475*7c478bd9Sstevel@tonic-gate 	int i, j = 0;
476*7c478bd9Sstevel@tonic-gate 	int d;
477*7c478bd9Sstevel@tonic-gate 	extern int debug_level;
478*7c478bd9Sstevel@tonic-gate 
479*7c478bd9Sstevel@tonic-gate 	script_env[j++] = rcmscript_strdup("PATH=/usr/sbin:/usr/bin");
480*7c478bd9Sstevel@tonic-gate 
481*7c478bd9Sstevel@tonic-gate 	for (i = 0; env_list[i] != NULL; i++) {
482*7c478bd9Sstevel@tonic-gate 		x = getenv(env_list[i]);
483*7c478bd9Sstevel@tonic-gate 		if (x) {
484*7c478bd9Sstevel@tonic-gate 			len = strlen(env_list[i]) + strlen(x) + 2;
485*7c478bd9Sstevel@tonic-gate 			script_env[j] = (char *)rcmscript_malloc(len);
486*7c478bd9Sstevel@tonic-gate 
487*7c478bd9Sstevel@tonic-gate 			(void) snprintf(script_env[j++], len, "%s=%s",
488*7c478bd9Sstevel@tonic-gate 				env_list[i], x);
489*7c478bd9Sstevel@tonic-gate 		}
490*7c478bd9Sstevel@tonic-gate 	}
491*7c478bd9Sstevel@tonic-gate 
492*7c478bd9Sstevel@tonic-gate 	len = strlen("RCM_ENV_DEBUG_LEVEL") + 3;
493*7c478bd9Sstevel@tonic-gate 	script_env[j] = (char *)rcmscript_malloc(len);
494*7c478bd9Sstevel@tonic-gate 
495*7c478bd9Sstevel@tonic-gate 	if (debug_level < 0)
496*7c478bd9Sstevel@tonic-gate 		d = 0;
497*7c478bd9Sstevel@tonic-gate 	else if (debug_level > 9)
498*7c478bd9Sstevel@tonic-gate 		d = 9;
499*7c478bd9Sstevel@tonic-gate 	else
500*7c478bd9Sstevel@tonic-gate 		d = debug_level;
501*7c478bd9Sstevel@tonic-gate 
502*7c478bd9Sstevel@tonic-gate 	(void) snprintf(script_env[j++], len, "RCM_ENV_DEBUG_LEVEL=%d", d);
503*7c478bd9Sstevel@tonic-gate 
504*7c478bd9Sstevel@tonic-gate 	script_env[j] = NULL;
505*7c478bd9Sstevel@tonic-gate }
506*7c478bd9Sstevel@tonic-gate 
507*7c478bd9Sstevel@tonic-gate static void
copy_env(char * src[],char * dst[])508*7c478bd9Sstevel@tonic-gate copy_env(char *src[], char *dst[])
509*7c478bd9Sstevel@tonic-gate {
510*7c478bd9Sstevel@tonic-gate 	int i;
511*7c478bd9Sstevel@tonic-gate 
512*7c478bd9Sstevel@tonic-gate 	for (i = 0; src[i] != NULL; i++)
513*7c478bd9Sstevel@tonic-gate 		dst[i] = src[i];
514*7c478bd9Sstevel@tonic-gate 
515*7c478bd9Sstevel@tonic-gate 	dst[i] = NULL;
516*7c478bd9Sstevel@tonic-gate }
517*7c478bd9Sstevel@tonic-gate 
518*7c478bd9Sstevel@tonic-gate /*
519*7c478bd9Sstevel@tonic-gate  * Open (or create if the file does not exist) the given state file
520*7c478bd9Sstevel@tonic-gate  * and mmap it.
521*7c478bd9Sstevel@tonic-gate  */
522*7c478bd9Sstevel@tonic-gate static void
open_state_file(const char * filename,state_file_descr_t * statefd,size_t element_size,int chunk_size,uint32_t version)523*7c478bd9Sstevel@tonic-gate open_state_file(const char *filename,
524*7c478bd9Sstevel@tonic-gate 	state_file_descr_t *statefd,
525*7c478bd9Sstevel@tonic-gate 	size_t element_size,
526*7c478bd9Sstevel@tonic-gate 	int chunk_size,
527*7c478bd9Sstevel@tonic-gate 	uint32_t version)
528*7c478bd9Sstevel@tonic-gate {
529*7c478bd9Sstevel@tonic-gate 	struct stat stats;
530*7c478bd9Sstevel@tonic-gate 	int error_num;
531*7c478bd9Sstevel@tonic-gate 
532*7c478bd9Sstevel@tonic-gate 	if ((statefd->fd = open(filename, O_CREAT|O_RDWR, 0600)) ==
533*7c478bd9Sstevel@tonic-gate 			-1) {
534*7c478bd9Sstevel@tonic-gate 		error_num = errno;
535*7c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, MF_STATE_FILE_ERR,
536*7c478bd9Sstevel@tonic-gate 			"open", strerror(error_num));
537*7c478bd9Sstevel@tonic-gate 		rcmd_exit(error_num);
538*7c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
539*7c478bd9Sstevel@tonic-gate 	}
540*7c478bd9Sstevel@tonic-gate 
541*7c478bd9Sstevel@tonic-gate 	if (fstat(statefd->fd, &stats) != 0) {
542*7c478bd9Sstevel@tonic-gate 		error_num = errno;
543*7c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, MF_STATE_FILE_ERR,
544*7c478bd9Sstevel@tonic-gate 			"fstat", strerror(error_num));
545*7c478bd9Sstevel@tonic-gate 		rcmd_exit(error_num);
546*7c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
547*7c478bd9Sstevel@tonic-gate 	}
548*7c478bd9Sstevel@tonic-gate 
549*7c478bd9Sstevel@tonic-gate 	if (stats.st_size != 0) {
550*7c478bd9Sstevel@tonic-gate 		/* LINTED */
551*7c478bd9Sstevel@tonic-gate 		statefd->state_file = (state_file_t *)mmap(NULL,
552*7c478bd9Sstevel@tonic-gate 			stats.st_size, PROT_READ|PROT_WRITE, MAP_SHARED,
553*7c478bd9Sstevel@tonic-gate 			statefd->fd, 0);
554*7c478bd9Sstevel@tonic-gate 
555*7c478bd9Sstevel@tonic-gate 		if (statefd->state_file == MAP_FAILED) {
556*7c478bd9Sstevel@tonic-gate 			error_num = errno;
557*7c478bd9Sstevel@tonic-gate 			rcm_log_message(RCM_ERROR, MF_STATE_FILE_ERR,
558*7c478bd9Sstevel@tonic-gate 				"mmap", strerror(error_num));
559*7c478bd9Sstevel@tonic-gate 			rcmd_exit(error_num);
560*7c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
561*7c478bd9Sstevel@tonic-gate 		}
562*7c478bd9Sstevel@tonic-gate 
563*7c478bd9Sstevel@tonic-gate 		if (statefd->state_file->version != version) {
564*7c478bd9Sstevel@tonic-gate 			(void) munmap((void *)statefd->state_file,
565*7c478bd9Sstevel@tonic-gate 					stats.st_size);
566*7c478bd9Sstevel@tonic-gate 			statefd->state_file = NULL;
567*7c478bd9Sstevel@tonic-gate 			(void) ftruncate(statefd->fd, 0);
568*7c478bd9Sstevel@tonic-gate 		}
569*7c478bd9Sstevel@tonic-gate 	} else {
570*7c478bd9Sstevel@tonic-gate 		statefd->state_file = NULL;
571*7c478bd9Sstevel@tonic-gate 	}
572*7c478bd9Sstevel@tonic-gate 
573*7c478bd9Sstevel@tonic-gate 	statefd->version = version;
574*7c478bd9Sstevel@tonic-gate 	statefd->element_size = sizeof (state_element_t) +
575*7c478bd9Sstevel@tonic-gate 				RSCR_ROUNDUP(element_size, 8);
576*7c478bd9Sstevel@tonic-gate 	statefd->chunk_size = chunk_size;
577*7c478bd9Sstevel@tonic-gate 	statefd->index = 0;
578*7c478bd9Sstevel@tonic-gate }
579*7c478bd9Sstevel@tonic-gate 
580*7c478bd9Sstevel@tonic-gate static void
truncate_state_file(state_file_descr_t * statefd)581*7c478bd9Sstevel@tonic-gate truncate_state_file(state_file_descr_t *statefd)
582*7c478bd9Sstevel@tonic-gate {
583*7c478bd9Sstevel@tonic-gate 	size_t size;
584*7c478bd9Sstevel@tonic-gate 
585*7c478bd9Sstevel@tonic-gate 	if (statefd->state_file) {
586*7c478bd9Sstevel@tonic-gate 		size = sizeof (state_file_t) + statefd->element_size *
587*7c478bd9Sstevel@tonic-gate 			statefd->state_file->max_elements;
588*7c478bd9Sstevel@tonic-gate 
589*7c478bd9Sstevel@tonic-gate 		(void) munmap((void *)statefd->state_file, size);
590*7c478bd9Sstevel@tonic-gate 		statefd->state_file = NULL;
591*7c478bd9Sstevel@tonic-gate 	}
592*7c478bd9Sstevel@tonic-gate 	(void) ftruncate(statefd->fd, 0);
593*7c478bd9Sstevel@tonic-gate }
594*7c478bd9Sstevel@tonic-gate 
595*7c478bd9Sstevel@tonic-gate static void
close_state_file(const char * filename,state_file_descr_t * statefd)596*7c478bd9Sstevel@tonic-gate close_state_file(const char *filename, state_file_descr_t *statefd)
597*7c478bd9Sstevel@tonic-gate {
598*7c478bd9Sstevel@tonic-gate 	truncate_state_file(statefd);
599*7c478bd9Sstevel@tonic-gate 	(void) close(statefd->fd);
600*7c478bd9Sstevel@tonic-gate 	(void) unlink(filename);
601*7c478bd9Sstevel@tonic-gate }
602*7c478bd9Sstevel@tonic-gate 
603*7c478bd9Sstevel@tonic-gate /*
604*7c478bd9Sstevel@tonic-gate  * Grow the state file by the chunk size specified in statefd
605*7c478bd9Sstevel@tonic-gate  * and mmap it.
606*7c478bd9Sstevel@tonic-gate  */
607*7c478bd9Sstevel@tonic-gate static void
grow_state_file(state_file_descr_t * statefd)608*7c478bd9Sstevel@tonic-gate grow_state_file(state_file_descr_t *statefd)
609*7c478bd9Sstevel@tonic-gate {
610*7c478bd9Sstevel@tonic-gate 	size_t size;
611*7c478bd9Sstevel@tonic-gate 	int max_elements;
612*7c478bd9Sstevel@tonic-gate 	int error_num;
613*7c478bd9Sstevel@tonic-gate 
614*7c478bd9Sstevel@tonic-gate 	max_elements = statefd->chunk_size;
615*7c478bd9Sstevel@tonic-gate 	if (statefd->state_file)
616*7c478bd9Sstevel@tonic-gate 		max_elements += statefd->state_file->max_elements;
617*7c478bd9Sstevel@tonic-gate 
618*7c478bd9Sstevel@tonic-gate 	size = sizeof (state_file_t) +
619*7c478bd9Sstevel@tonic-gate 		statefd->element_size * max_elements;
620*7c478bd9Sstevel@tonic-gate 
621*7c478bd9Sstevel@tonic-gate 	if (ftruncate(statefd->fd, size) != 0) {
622*7c478bd9Sstevel@tonic-gate 		error_num = errno;
623*7c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, MF_STATE_FILE_ERR,
624*7c478bd9Sstevel@tonic-gate 			"ftruncate", strerror(error_num));
625*7c478bd9Sstevel@tonic-gate 		rcmd_exit(error_num);
626*7c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
627*7c478bd9Sstevel@tonic-gate 	}
628*7c478bd9Sstevel@tonic-gate 
629*7c478bd9Sstevel@tonic-gate 	/* LINTED */
630*7c478bd9Sstevel@tonic-gate 	statefd->state_file = (state_file_t *)mmap(NULL, size,
631*7c478bd9Sstevel@tonic-gate 		PROT_READ|PROT_WRITE, MAP_SHARED, statefd->fd, 0);
632*7c478bd9Sstevel@tonic-gate 
633*7c478bd9Sstevel@tonic-gate 	if (statefd->state_file == MAP_FAILED) {
634*7c478bd9Sstevel@tonic-gate 		error_num = errno;
635*7c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, MF_STATE_FILE_ERR,
636*7c478bd9Sstevel@tonic-gate 			"mmap", strerror(error_num));
637*7c478bd9Sstevel@tonic-gate 		rcmd_exit(error_num);
638*7c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
639*7c478bd9Sstevel@tonic-gate 	}
640*7c478bd9Sstevel@tonic-gate 
641*7c478bd9Sstevel@tonic-gate 	statefd->index = statefd->state_file->max_elements;
642*7c478bd9Sstevel@tonic-gate 	statefd->state_file->max_elements = max_elements;
643*7c478bd9Sstevel@tonic-gate 	statefd->state_file->version = statefd->version;
644*7c478bd9Sstevel@tonic-gate }
645*7c478bd9Sstevel@tonic-gate 
646*7c478bd9Sstevel@tonic-gate /*
647*7c478bd9Sstevel@tonic-gate  * Given index into state element array, get the pointer to the actual
648*7c478bd9Sstevel@tonic-gate  * state element.
649*7c478bd9Sstevel@tonic-gate  * If flag is non-null set *flag to
650*7c478bd9Sstevel@tonic-gate  *	TRUE if the state element is currently is use.
651*7c478bd9Sstevel@tonic-gate  *	FALSE if the state element is free.
652*7c478bd9Sstevel@tonic-gate  */
653*7c478bd9Sstevel@tonic-gate static void *
get_state_element(state_file_descr_t * statefd,int index,int * flag)654*7c478bd9Sstevel@tonic-gate get_state_element(state_file_descr_t *statefd, int index, int *flag)
655*7c478bd9Sstevel@tonic-gate {
656*7c478bd9Sstevel@tonic-gate 	char *ptr;
657*7c478bd9Sstevel@tonic-gate 
658*7c478bd9Sstevel@tonic-gate 	if (statefd->state_file &&
659*7c478bd9Sstevel@tonic-gate 	    (index < statefd->state_file->max_elements)) {
660*7c478bd9Sstevel@tonic-gate 
661*7c478bd9Sstevel@tonic-gate 		ptr = (char *)(statefd->state_file);
662*7c478bd9Sstevel@tonic-gate 		ptr += sizeof (state_file_t) +
663*7c478bd9Sstevel@tonic-gate 			index * statefd->element_size;
664*7c478bd9Sstevel@tonic-gate 
665*7c478bd9Sstevel@tonic-gate 		if (flag) {
666*7c478bd9Sstevel@tonic-gate 			*flag = (((state_element_t *)((void *)ptr))->flags &
667*7c478bd9Sstevel@tonic-gate 				STATE_ELEMENT_IN_USE) ? 1 : 0;
668*7c478bd9Sstevel@tonic-gate 		}
669*7c478bd9Sstevel@tonic-gate 
670*7c478bd9Sstevel@tonic-gate 		ptr += sizeof (state_element_t);
671*7c478bd9Sstevel@tonic-gate 	} else
672*7c478bd9Sstevel@tonic-gate 		ptr = NULL;
673*7c478bd9Sstevel@tonic-gate 
674*7c478bd9Sstevel@tonic-gate 	return ((void *)ptr);
675*7c478bd9Sstevel@tonic-gate }
676*7c478bd9Sstevel@tonic-gate 
677*7c478bd9Sstevel@tonic-gate /*
678*7c478bd9Sstevel@tonic-gate  * Allocate a state element entry in the state file and return a pointer
679*7c478bd9Sstevel@tonic-gate  * to the allocated entry.
680*7c478bd9Sstevel@tonic-gate  * If index is non-null set *index to index into the state element array
681*7c478bd9Sstevel@tonic-gate  * of the allocated entry.
682*7c478bd9Sstevel@tonic-gate  */
683*7c478bd9Sstevel@tonic-gate static void *
allocate_state_element(state_file_descr_t * statefd,int * index)684*7c478bd9Sstevel@tonic-gate allocate_state_element(state_file_descr_t *statefd, int *index)
685*7c478bd9Sstevel@tonic-gate {
686*7c478bd9Sstevel@tonic-gate 	void *x;
687*7c478bd9Sstevel@tonic-gate 	int i;
688*7c478bd9Sstevel@tonic-gate 	int flag;
689*7c478bd9Sstevel@tonic-gate 
690*7c478bd9Sstevel@tonic-gate 	if (statefd->state_file) {
691*7c478bd9Sstevel@tonic-gate 		/* find an empty slot */
692*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < statefd->state_file->max_elements; i++) {
693*7c478bd9Sstevel@tonic-gate 			x = get_state_element(statefd, statefd->index,
694*7c478bd9Sstevel@tonic-gate 						&flag);
695*7c478bd9Sstevel@tonic-gate 			assert(x != NULL);
696*7c478bd9Sstevel@tonic-gate 
697*7c478bd9Sstevel@tonic-gate 			if (flag == 0)
698*7c478bd9Sstevel@tonic-gate 				/* entry is free */
699*7c478bd9Sstevel@tonic-gate 				break;
700*7c478bd9Sstevel@tonic-gate 
701*7c478bd9Sstevel@tonic-gate 			statefd->index++;
702*7c478bd9Sstevel@tonic-gate 			if (statefd->index >= statefd->state_file->max_elements)
703*7c478bd9Sstevel@tonic-gate 				statefd->index = 0;
704*7c478bd9Sstevel@tonic-gate 		}
705*7c478bd9Sstevel@tonic-gate 	}
706*7c478bd9Sstevel@tonic-gate 
707*7c478bd9Sstevel@tonic-gate 	if (statefd->state_file == NULL ||
708*7c478bd9Sstevel@tonic-gate 		i == statefd->state_file->max_elements) {
709*7c478bd9Sstevel@tonic-gate 
710*7c478bd9Sstevel@tonic-gate 		/* All entries are in use. Grow the list */
711*7c478bd9Sstevel@tonic-gate 		grow_state_file(statefd);
712*7c478bd9Sstevel@tonic-gate 		x = get_state_element(statefd, statefd->index, &flag);
713*7c478bd9Sstevel@tonic-gate 		assert(flag == 0);
714*7c478bd9Sstevel@tonic-gate 	}
715*7c478bd9Sstevel@tonic-gate 
716*7c478bd9Sstevel@tonic-gate 	if (index != NULL)
717*7c478bd9Sstevel@tonic-gate 		*index = statefd->index;
718*7c478bd9Sstevel@tonic-gate 
719*7c478bd9Sstevel@tonic-gate 	statefd->index++;
720*7c478bd9Sstevel@tonic-gate 	if (statefd->index >= statefd->state_file->max_elements)
721*7c478bd9Sstevel@tonic-gate 		statefd->index = 0;
722*7c478bd9Sstevel@tonic-gate 
723*7c478bd9Sstevel@tonic-gate 	((state_element_t *)x - 1)->flags |= STATE_ELEMENT_IN_USE;
724*7c478bd9Sstevel@tonic-gate 	return (x);
725*7c478bd9Sstevel@tonic-gate }
726*7c478bd9Sstevel@tonic-gate 
727*7c478bd9Sstevel@tonic-gate static void
free_state_element(void * x)728*7c478bd9Sstevel@tonic-gate free_state_element(void *x)
729*7c478bd9Sstevel@tonic-gate {
730*7c478bd9Sstevel@tonic-gate 	((state_element_t *)x - 1)->flags &= ~STATE_ELEMENT_IN_USE;
731*7c478bd9Sstevel@tonic-gate }
732*7c478bd9Sstevel@tonic-gate 
733*7c478bd9Sstevel@tonic-gate /*
734*7c478bd9Sstevel@tonic-gate  * Kill the pids contained in ps state file.
735*7c478bd9Sstevel@tonic-gate  */
736*7c478bd9Sstevel@tonic-gate static void
script_ps_state_file_kill_pids(void)737*7c478bd9Sstevel@tonic-gate script_ps_state_file_kill_pids(void)
738*7c478bd9Sstevel@tonic-gate {
739*7c478bd9Sstevel@tonic-gate 	ps_state_element_t *x;
740*7c478bd9Sstevel@tonic-gate 	char procfile[80];
741*7c478bd9Sstevel@tonic-gate 	psinfo_t psi;
742*7c478bd9Sstevel@tonic-gate 	int fd, i, flag;
743*7c478bd9Sstevel@tonic-gate 
744*7c478bd9Sstevel@tonic-gate 	/* LINTED */
745*7c478bd9Sstevel@tonic-gate 	for (i = 0; 1; i++) {
746*7c478bd9Sstevel@tonic-gate 		if ((x = (ps_state_element_t *)get_state_element(
747*7c478bd9Sstevel@tonic-gate 					&script_ps_statefd, i, &flag)) == NULL)
748*7c478bd9Sstevel@tonic-gate 			break;
749*7c478bd9Sstevel@tonic-gate 
750*7c478bd9Sstevel@tonic-gate 		if (flag == 1) { /* the entry is in use */
751*7c478bd9Sstevel@tonic-gate 			(void) snprintf(procfile, 80, "/proc/%ld/psinfo",
752*7c478bd9Sstevel@tonic-gate 					(long)x->pid);
753*7c478bd9Sstevel@tonic-gate 			if ((fd = open(procfile, O_RDONLY)) != -1 &&
754*7c478bd9Sstevel@tonic-gate 				read(fd, &psi, sizeof (psi)) == sizeof (psi) &&
755*7c478bd9Sstevel@tonic-gate 				strcmp(psi.pr_fname,
756*7c478bd9Sstevel@tonic-gate 				x->script_name) == 0) {
757*7c478bd9Sstevel@tonic-gate 
758*7c478bd9Sstevel@tonic-gate 				(void) close(fd);
759*7c478bd9Sstevel@tonic-gate 
760*7c478bd9Sstevel@tonic-gate 				/*
761*7c478bd9Sstevel@tonic-gate 				 * just a safety check to not to blow up
762*7c478bd9Sstevel@tonic-gate 				 * system processes if the file is ever corrupt
763*7c478bd9Sstevel@tonic-gate 				 */
764*7c478bd9Sstevel@tonic-gate 				if (x->pid > 1) {
765*7c478bd9Sstevel@tonic-gate 					rcm_log_message(RCM_DEBUG,
766*7c478bd9Sstevel@tonic-gate 					"script_ps_state_file_kill_pids: "
767*7c478bd9Sstevel@tonic-gate 					"killing script_name = %s pid = %ld\n",
768*7c478bd9Sstevel@tonic-gate 					x->script_name, x->pid);
769*7c478bd9Sstevel@tonic-gate 
770*7c478bd9Sstevel@tonic-gate 					/* kill the process group */
771*7c478bd9Sstevel@tonic-gate 					(void) kill(-(x->pid), SIGKILL);
772*7c478bd9Sstevel@tonic-gate 				}
773*7c478bd9Sstevel@tonic-gate 			} else {
774*7c478bd9Sstevel@tonic-gate 				if (fd != -1)
775*7c478bd9Sstevel@tonic-gate 					(void) close(fd);
776*7c478bd9Sstevel@tonic-gate 			}
777*7c478bd9Sstevel@tonic-gate 			free_state_element((void *)x);
778*7c478bd9Sstevel@tonic-gate 		}
779*7c478bd9Sstevel@tonic-gate 	}
780*7c478bd9Sstevel@tonic-gate }
781*7c478bd9Sstevel@tonic-gate 
782*7c478bd9Sstevel@tonic-gate /*
783*7c478bd9Sstevel@tonic-gate  * Add a state element entry to ps state file.
784*7c478bd9Sstevel@tonic-gate  */
785*7c478bd9Sstevel@tonic-gate static void
script_ps_state_file_add_entry(pid_t pid,char * script_name)786*7c478bd9Sstevel@tonic-gate script_ps_state_file_add_entry(pid_t pid, char *script_name)
787*7c478bd9Sstevel@tonic-gate {
788*7c478bd9Sstevel@tonic-gate 	ps_state_element_t *x;
789*7c478bd9Sstevel@tonic-gate 
790*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&script_lock);
791*7c478bd9Sstevel@tonic-gate 
792*7c478bd9Sstevel@tonic-gate 	x = (ps_state_element_t *)allocate_state_element(
793*7c478bd9Sstevel@tonic-gate 		&script_ps_statefd, NULL);
794*7c478bd9Sstevel@tonic-gate 
795*7c478bd9Sstevel@tonic-gate 	x->pid = pid;
796*7c478bd9Sstevel@tonic-gate 	(void) strlcpy(x->script_name, script_name, MAXNAMELEN);
797*7c478bd9Sstevel@tonic-gate 
798*7c478bd9Sstevel@tonic-gate 	(void) fsync(script_ps_statefd.fd);
799*7c478bd9Sstevel@tonic-gate 
800*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&script_lock);
801*7c478bd9Sstevel@tonic-gate }
802*7c478bd9Sstevel@tonic-gate 
803*7c478bd9Sstevel@tonic-gate /*
804*7c478bd9Sstevel@tonic-gate  * Remove the state element entry corresponding to pid from the
805*7c478bd9Sstevel@tonic-gate  * ps state file.
806*7c478bd9Sstevel@tonic-gate  */
807*7c478bd9Sstevel@tonic-gate static void
script_ps_state_file_remove_entry(pid_t pid)808*7c478bd9Sstevel@tonic-gate script_ps_state_file_remove_entry(pid_t pid)
809*7c478bd9Sstevel@tonic-gate {
810*7c478bd9Sstevel@tonic-gate 	ps_state_element_t *x;
811*7c478bd9Sstevel@tonic-gate 	int flag, i;
812*7c478bd9Sstevel@tonic-gate 
813*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&script_lock);
814*7c478bd9Sstevel@tonic-gate 
815*7c478bd9Sstevel@tonic-gate 	/* LINTED */
816*7c478bd9Sstevel@tonic-gate 	for (i = 0; 1; i++) {
817*7c478bd9Sstevel@tonic-gate 		if ((x = (ps_state_element_t *)get_state_element(
818*7c478bd9Sstevel@tonic-gate 					&script_ps_statefd, i, &flag)) == NULL)
819*7c478bd9Sstevel@tonic-gate 			break;
820*7c478bd9Sstevel@tonic-gate 
821*7c478bd9Sstevel@tonic-gate 		/* if the state element entry is in use and pid matches */
822*7c478bd9Sstevel@tonic-gate 		if (flag == 1 && x->pid == pid) {
823*7c478bd9Sstevel@tonic-gate 			free_state_element((void *)x);
824*7c478bd9Sstevel@tonic-gate 			break;
825*7c478bd9Sstevel@tonic-gate 		}
826*7c478bd9Sstevel@tonic-gate 	}
827*7c478bd9Sstevel@tonic-gate 
828*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&script_lock);
829*7c478bd9Sstevel@tonic-gate }
830*7c478bd9Sstevel@tonic-gate 
831*7c478bd9Sstevel@tonic-gate /*
832*7c478bd9Sstevel@tonic-gate  * Get data item id given data item name
833*7c478bd9Sstevel@tonic-gate  */
834*7c478bd9Sstevel@tonic-gate static int
dname_to_id(char * dname)835*7c478bd9Sstevel@tonic-gate dname_to_id(char *dname)
836*7c478bd9Sstevel@tonic-gate {
837*7c478bd9Sstevel@tonic-gate 	int i;
838*7c478bd9Sstevel@tonic-gate 
839*7c478bd9Sstevel@tonic-gate 	for (i = 0; script_data_item_name[i] != NULL; i++) {
840*7c478bd9Sstevel@tonic-gate 		if (strcmp(dname, script_data_item_name[i]) == 0)
841*7c478bd9Sstevel@tonic-gate 			return (i);
842*7c478bd9Sstevel@tonic-gate 	}
843*7c478bd9Sstevel@tonic-gate 
844*7c478bd9Sstevel@tonic-gate 	return (-1);
845*7c478bd9Sstevel@tonic-gate }
846*7c478bd9Sstevel@tonic-gate 
847*7c478bd9Sstevel@tonic-gate /*
848*7c478bd9Sstevel@tonic-gate  * Called before running any script.
849*7c478bd9Sstevel@tonic-gate  * This routine waits until the number of script processes running in
850*7c478bd9Sstevel@tonic-gate  * parallel drops down below to script_max_parallelism.
851*7c478bd9Sstevel@tonic-gate  */
852*7c478bd9Sstevel@tonic-gate static void
script_process_sema_wait(void)853*7c478bd9Sstevel@tonic-gate script_process_sema_wait(void)
854*7c478bd9Sstevel@tonic-gate {
855*7c478bd9Sstevel@tonic-gate 	int error_num;
856*7c478bd9Sstevel@tonic-gate 
857*7c478bd9Sstevel@tonic-gate 	/* LINTED */
858*7c478bd9Sstevel@tonic-gate 	while (1) {
859*7c478bd9Sstevel@tonic-gate 		if (sema_wait(&script_process_sema) == 0)
860*7c478bd9Sstevel@tonic-gate 			return;
861*7c478bd9Sstevel@tonic-gate 
862*7c478bd9Sstevel@tonic-gate 		if (errno != EINTR && errno != EAGAIN) {
863*7c478bd9Sstevel@tonic-gate 			error_num = errno;
864*7c478bd9Sstevel@tonic-gate 			rcm_log_message(RCM_ERROR, MF_FUNC_CALL_ERR,
865*7c478bd9Sstevel@tonic-gate 				"sema_wait", strerror(error_num));
866*7c478bd9Sstevel@tonic-gate 			rcmd_exit(error_num);
867*7c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
868*7c478bd9Sstevel@tonic-gate 		}
869*7c478bd9Sstevel@tonic-gate 	}
870*7c478bd9Sstevel@tonic-gate 
871*7c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
872*7c478bd9Sstevel@tonic-gate }
873*7c478bd9Sstevel@tonic-gate 
874*7c478bd9Sstevel@tonic-gate /*
875*7c478bd9Sstevel@tonic-gate  * Fork and execute the script.
876*7c478bd9Sstevel@tonic-gate  */
877*7c478bd9Sstevel@tonic-gate static int
run_script(script_info_t * rsi,char * argv[],char * envp[],char ** errmsg)878*7c478bd9Sstevel@tonic-gate run_script(script_info_t *rsi, char *argv[], char *envp[], char **errmsg)
879*7c478bd9Sstevel@tonic-gate {
880*7c478bd9Sstevel@tonic-gate 	int i, p1 = -1, p2 = -1;
881*7c478bd9Sstevel@tonic-gate 	struct rlimit rlp;
882*7c478bd9Sstevel@tonic-gate 	struct stat stats;
883*7c478bd9Sstevel@tonic-gate 
884*7c478bd9Sstevel@tonic-gate 	rcm_log_message(RSCR_TRACE, "run_script: script name = %s\n",
885*7c478bd9Sstevel@tonic-gate 					rsi->script_full_name);
886*7c478bd9Sstevel@tonic-gate 
887*7c478bd9Sstevel@tonic-gate 	for (i = 0; argv[i] != NULL; i++)
888*7c478bd9Sstevel@tonic-gate 		rcm_log_message(RSCR_TRACE, "run_script: argv[%d] = %s\n",
889*7c478bd9Sstevel@tonic-gate 					i, argv[i]);
890*7c478bd9Sstevel@tonic-gate 
891*7c478bd9Sstevel@tonic-gate 	*errmsg = NULL;
892*7c478bd9Sstevel@tonic-gate 
893*7c478bd9Sstevel@tonic-gate 	/* check that the script exists */
894*7c478bd9Sstevel@tonic-gate 	if (stat(rsi->script_full_name, &stats) != 0)
895*7c478bd9Sstevel@tonic-gate 		goto error;
896*7c478bd9Sstevel@tonic-gate 
897*7c478bd9Sstevel@tonic-gate 	/*
898*7c478bd9Sstevel@tonic-gate 	 * If the syscall pipe fails because of reaching the max open file
899*7c478bd9Sstevel@tonic-gate 	 * count per process then dynamically increase the limit on the max
900*7c478bd9Sstevel@tonic-gate 	 * open file count.
901*7c478bd9Sstevel@tonic-gate 	 *
902*7c478bd9Sstevel@tonic-gate 	 * At present the rcm_daemon consumes file descriptor
903*7c478bd9Sstevel@tonic-gate 	 * entries for the following files.
904*7c478bd9Sstevel@tonic-gate 	 *   RCM_STATE_FILE   - /var/run/rcm_daemon_state
905*7c478bd9Sstevel@tonic-gate 	 *   DAEMON_LOCK_FILE - /var/run/rcm_daemon_lock
906*7c478bd9Sstevel@tonic-gate 	 *   RCM_SERVICE_DOOR - /var/run/rcm_daemon_door
907*7c478bd9Sstevel@tonic-gate 	 *   proc files in the format "/proc/pid/as" for each pid
908*7c478bd9Sstevel@tonic-gate 	 *	communicating with the rcm_daemon via doors
909*7c478bd9Sstevel@tonic-gate 	 *   dlopen for each rcm module
910*7c478bd9Sstevel@tonic-gate 	 *   When in daemon mode stdin, stdout and stderr are closed;
911*7c478bd9Sstevel@tonic-gate 	 *	/dev/null opened and duped to stdout, and stderr
912*7c478bd9Sstevel@tonic-gate 	 *   openlog
913*7c478bd9Sstevel@tonic-gate 	 *   Some files which are opened briefly and closed such as
914*7c478bd9Sstevel@tonic-gate 	 *	directory files.
915*7c478bd9Sstevel@tonic-gate 	 *   Two file descriptors for each script in running state.
916*7c478bd9Sstevel@tonic-gate 	 *	Note that the constant script_max_parallelism sets an
917*7c478bd9Sstevel@tonic-gate 	 *	upper cap on how many rcm scripts can run in
918*7c478bd9Sstevel@tonic-gate 	 *	parallel.
919*7c478bd9Sstevel@tonic-gate 	 */
920*7c478bd9Sstevel@tonic-gate 	if ((p1 = pipe(rsi->pipe1)) == -1 || (p2 = pipe(rsi->pipe2)) == -1) {
921*7c478bd9Sstevel@tonic-gate 		if ((errno == EMFILE) &&
922*7c478bd9Sstevel@tonic-gate 			(getrlimit(RLIMIT_NOFILE, &rlp) == 0)) {
923*7c478bd9Sstevel@tonic-gate 
924*7c478bd9Sstevel@tonic-gate 			rlp.rlim_cur += 16;
925*7c478bd9Sstevel@tonic-gate 			if (rlp.rlim_max < rlp.rlim_cur)
926*7c478bd9Sstevel@tonic-gate 				rlp.rlim_max = rlp.rlim_cur;
927*7c478bd9Sstevel@tonic-gate 			(void) setrlimit(RLIMIT_NOFILE, &rlp);
928*7c478bd9Sstevel@tonic-gate 
929*7c478bd9Sstevel@tonic-gate 			if (p1 == -1) {
930*7c478bd9Sstevel@tonic-gate 				if ((p1 = pipe(rsi->pipe1)) == -1)
931*7c478bd9Sstevel@tonic-gate 					goto error;
932*7c478bd9Sstevel@tonic-gate 			}
933*7c478bd9Sstevel@tonic-gate 			if ((p2 = pipe(rsi->pipe2)) == -1)
934*7c478bd9Sstevel@tonic-gate 				goto error;
935*7c478bd9Sstevel@tonic-gate 		} else
936*7c478bd9Sstevel@tonic-gate 			goto error;
937*7c478bd9Sstevel@tonic-gate 	}
938*7c478bd9Sstevel@tonic-gate 
939*7c478bd9Sstevel@tonic-gate forkagain:
940*7c478bd9Sstevel@tonic-gate 	if ((rsi->pid = fork1()) == (pid_t)-1) {
941*7c478bd9Sstevel@tonic-gate 		if (errno == EINTR || errno == EAGAIN)
942*7c478bd9Sstevel@tonic-gate 			goto forkagain;
943*7c478bd9Sstevel@tonic-gate 
944*7c478bd9Sstevel@tonic-gate 		goto error;
945*7c478bd9Sstevel@tonic-gate 	}
946*7c478bd9Sstevel@tonic-gate 
947*7c478bd9Sstevel@tonic-gate 	if (rsi->pid == 0) {
948*7c478bd9Sstevel@tonic-gate 		/* child process */
949*7c478bd9Sstevel@tonic-gate 
950*7c478bd9Sstevel@tonic-gate 		(void) setsid();
951*7c478bd9Sstevel@tonic-gate 
952*7c478bd9Sstevel@tonic-gate 		/* close stdin, stdout and stderr */
953*7c478bd9Sstevel@tonic-gate 		(void) close(0);
954*7c478bd9Sstevel@tonic-gate 		(void) close(1);
955*7c478bd9Sstevel@tonic-gate 		(void) close(2);
956*7c478bd9Sstevel@tonic-gate 
957*7c478bd9Sstevel@tonic-gate 		/* set stdin to /dev/null */
958*7c478bd9Sstevel@tonic-gate 		(void) open("/dev/null", O_RDWR, 0);
959*7c478bd9Sstevel@tonic-gate 
960*7c478bd9Sstevel@tonic-gate 		/* redirect stdout and stderr to pipe */
961*7c478bd9Sstevel@tonic-gate 		(void) dup2(rsi->pipe1[CHILD_END_OF_PIPE], 1);
962*7c478bd9Sstevel@tonic-gate 		(void) dup2(rsi->pipe2[CHILD_END_OF_PIPE], 2);
963*7c478bd9Sstevel@tonic-gate 
964*7c478bd9Sstevel@tonic-gate 		/* close all other file descriptors */
965*7c478bd9Sstevel@tonic-gate 		closefrom(3);
966*7c478bd9Sstevel@tonic-gate 
967*7c478bd9Sstevel@tonic-gate 		/* restore original file limit */
968*7c478bd9Sstevel@tonic-gate 		(void) setrlimit(RLIMIT_NOFILE, &file_limit);
969*7c478bd9Sstevel@tonic-gate 
970*7c478bd9Sstevel@tonic-gate 		/* set current working dir */
971*7c478bd9Sstevel@tonic-gate 		if (stats.st_uid == 0) {
972*7c478bd9Sstevel@tonic-gate 			/* root */
973*7c478bd9Sstevel@tonic-gate 			if (chdir("/var/run") == -1)
974*7c478bd9Sstevel@tonic-gate 				_exit(127);
975*7c478bd9Sstevel@tonic-gate 		} else {
976*7c478bd9Sstevel@tonic-gate 			if (chdir("/tmp") == -1)
977*7c478bd9Sstevel@tonic-gate 				_exit(127);
978*7c478bd9Sstevel@tonic-gate 		}
979*7c478bd9Sstevel@tonic-gate 
980*7c478bd9Sstevel@tonic-gate 		/*
981*7c478bd9Sstevel@tonic-gate 		 * setuid sets real, effective and saved user ids to the
982*7c478bd9Sstevel@tonic-gate 		 * given id.
983*7c478bd9Sstevel@tonic-gate 		 * setgid sets real, effective and saved group ids to the
984*7c478bd9Sstevel@tonic-gate 		 * given id.
985*7c478bd9Sstevel@tonic-gate 		 */
986*7c478bd9Sstevel@tonic-gate 		(void) setgid(stats.st_gid);
987*7c478bd9Sstevel@tonic-gate 		(void) setuid(stats.st_uid);
988*7c478bd9Sstevel@tonic-gate 
989*7c478bd9Sstevel@tonic-gate 		(void) execve(rsi->script_full_name, argv, envp);
990*7c478bd9Sstevel@tonic-gate 		_exit(127);
991*7c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
992*7c478bd9Sstevel@tonic-gate 	}
993*7c478bd9Sstevel@tonic-gate 
994*7c478bd9Sstevel@tonic-gate 	(void) close(rsi->pipe1[CHILD_END_OF_PIPE]);
995*7c478bd9Sstevel@tonic-gate 	(void) close(rsi->pipe2[CHILD_END_OF_PIPE]);
996*7c478bd9Sstevel@tonic-gate 
997*7c478bd9Sstevel@tonic-gate 	script_ps_state_file_add_entry(rsi->pid, rsi->script_name);
998*7c478bd9Sstevel@tonic-gate 
999*7c478bd9Sstevel@tonic-gate 	return (0);
1000*7c478bd9Sstevel@tonic-gate 
1001*7c478bd9Sstevel@tonic-gate error:
1002*7c478bd9Sstevel@tonic-gate 	*errmsg = dup_err(RCM_ERROR, MS_SCRIPT_ERR,
1003*7c478bd9Sstevel@tonic-gate 			rsi->script_name, strerror(errno));
1004*7c478bd9Sstevel@tonic-gate 
1005*7c478bd9Sstevel@tonic-gate 	if (p1 != -1) {
1006*7c478bd9Sstevel@tonic-gate 		(void) close(rsi->pipe1[PARENT_END_OF_PIPE]);
1007*7c478bd9Sstevel@tonic-gate 		(void) close(rsi->pipe1[CHILD_END_OF_PIPE]);
1008*7c478bd9Sstevel@tonic-gate 	}
1009*7c478bd9Sstevel@tonic-gate 
1010*7c478bd9Sstevel@tonic-gate 	if (p2 != -1) {
1011*7c478bd9Sstevel@tonic-gate 		(void) close(rsi->pipe2[PARENT_END_OF_PIPE]);
1012*7c478bd9Sstevel@tonic-gate 		(void) close(rsi->pipe2[CHILD_END_OF_PIPE]);
1013*7c478bd9Sstevel@tonic-gate 	}
1014*7c478bd9Sstevel@tonic-gate 
1015*7c478bd9Sstevel@tonic-gate 	return (-1);
1016*7c478bd9Sstevel@tonic-gate }
1017*7c478bd9Sstevel@tonic-gate 
1018*7c478bd9Sstevel@tonic-gate /*
1019*7c478bd9Sstevel@tonic-gate  * Reads one line of input (including the newline character) from the
1020*7c478bd9Sstevel@tonic-gate  * given file descriptor "fd" to buf.
1021*7c478bd9Sstevel@tonic-gate  * maxbuflen specifies the size of memory allocated for buf.
1022*7c478bd9Sstevel@tonic-gate  * Timeoutval is the max timeout value in seconds for the script to supply
1023*7c478bd9Sstevel@tonic-gate  * input. A timeoutval of 0 implies no timeout.
1024*7c478bd9Sstevel@tonic-gate  *
1025*7c478bd9Sstevel@tonic-gate  * Upon return *buflen contains the number of bytes read.
1026*7c478bd9Sstevel@tonic-gate  *
1027*7c478bd9Sstevel@tonic-gate  * Return values:
1028*7c478bd9Sstevel@tonic-gate  *   0  success
1029*7c478bd9Sstevel@tonic-gate  *   -1 an error occured
1030*7c478bd9Sstevel@tonic-gate  *   -2 timeout occurred
1031*7c478bd9Sstevel@tonic-gate  *   -3 script exited
1032*7c478bd9Sstevel@tonic-gate  */
1033*7c478bd9Sstevel@tonic-gate static int
get_line(int fd,char * fdname,char * buf,int maxbuflen,size_t * buflen,time_t timeoutval,int * error_num)1034*7c478bd9Sstevel@tonic-gate get_line(int fd,
1035*7c478bd9Sstevel@tonic-gate 	char *fdname,
1036*7c478bd9Sstevel@tonic-gate 	char *buf,
1037*7c478bd9Sstevel@tonic-gate 	int maxbuflen,
1038*7c478bd9Sstevel@tonic-gate 	size_t *buflen,
1039*7c478bd9Sstevel@tonic-gate 	time_t timeoutval,
1040*7c478bd9Sstevel@tonic-gate 	int *error_num)
1041*7c478bd9Sstevel@tonic-gate {
1042*7c478bd9Sstevel@tonic-gate 	char c = '\0';
1043*7c478bd9Sstevel@tonic-gate 	struct pollfd fds[1];
1044*7c478bd9Sstevel@tonic-gate 	int x;
1045*7c478bd9Sstevel@tonic-gate 	size_t len = 0;
1046*7c478bd9Sstevel@tonic-gate 	char *ptr;
1047*7c478bd9Sstevel@tonic-gate 	int timeit;
1048*7c478bd9Sstevel@tonic-gate 	time_t deadline;
1049*7c478bd9Sstevel@tonic-gate 	int rval = 0;
1050*7c478bd9Sstevel@tonic-gate 
1051*7c478bd9Sstevel@tonic-gate 	if (timeoutval) {
1052*7c478bd9Sstevel@tonic-gate 		timeit = TRUE;
1053*7c478bd9Sstevel@tonic-gate 		deadline = time(NULL) + timeoutval;
1054*7c478bd9Sstevel@tonic-gate 		fds[0].fd = fd;
1055*7c478bd9Sstevel@tonic-gate 		fds[0].events = POLLIN;
1056*7c478bd9Sstevel@tonic-gate 	} else
1057*7c478bd9Sstevel@tonic-gate 		timeit = FALSE;
1058*7c478bd9Sstevel@tonic-gate 
1059*7c478bd9Sstevel@tonic-gate 	ptr = buf;
1060*7c478bd9Sstevel@tonic-gate 
1061*7c478bd9Sstevel@tonic-gate 	while (c != '\n' && len < (maxbuflen -1)) {
1062*7c478bd9Sstevel@tonic-gate 		if (timeit) {
1063*7c478bd9Sstevel@tonic-gate pollagain:
1064*7c478bd9Sstevel@tonic-gate 			fds[0].revents = 0;
1065*7c478bd9Sstevel@tonic-gate 			timeoutval = deadline - time(NULL);
1066*7c478bd9Sstevel@tonic-gate 			if (timeoutval <= 0) {
1067*7c478bd9Sstevel@tonic-gate 				rval = -2;
1068*7c478bd9Sstevel@tonic-gate 				break;
1069*7c478bd9Sstevel@tonic-gate 			}
1070*7c478bd9Sstevel@tonic-gate 			x = poll(fds, 1, timeoutval*1000);
1071*7c478bd9Sstevel@tonic-gate 			if (x <= 0) {
1072*7c478bd9Sstevel@tonic-gate 				if (x == 0)
1073*7c478bd9Sstevel@tonic-gate 					/* poll timedout */
1074*7c478bd9Sstevel@tonic-gate 					rval = -2;
1075*7c478bd9Sstevel@tonic-gate 				else {
1076*7c478bd9Sstevel@tonic-gate 					if (errno == EINTR || errno == EAGAIN)
1077*7c478bd9Sstevel@tonic-gate 						goto pollagain;
1078*7c478bd9Sstevel@tonic-gate 					*error_num = errno;
1079*7c478bd9Sstevel@tonic-gate 					rval = -1;
1080*7c478bd9Sstevel@tonic-gate 				}
1081*7c478bd9Sstevel@tonic-gate 				break;
1082*7c478bd9Sstevel@tonic-gate 			}
1083*7c478bd9Sstevel@tonic-gate 		}
1084*7c478bd9Sstevel@tonic-gate readagain:
1085*7c478bd9Sstevel@tonic-gate 		if ((x = read(fd, &c, 1)) != 1) {
1086*7c478bd9Sstevel@tonic-gate 			if (x == 0)
1087*7c478bd9Sstevel@tonic-gate 				/*
1088*7c478bd9Sstevel@tonic-gate 				 * Script exited. Or more specifically the
1089*7c478bd9Sstevel@tonic-gate 				 * script has closed its end of the pipe.
1090*7c478bd9Sstevel@tonic-gate 				 */
1091*7c478bd9Sstevel@tonic-gate 				rval = -3;
1092*7c478bd9Sstevel@tonic-gate 			else {
1093*7c478bd9Sstevel@tonic-gate 				if (errno == EINTR || errno == EAGAIN)
1094*7c478bd9Sstevel@tonic-gate 					goto readagain;
1095*7c478bd9Sstevel@tonic-gate 				*error_num = errno;
1096*7c478bd9Sstevel@tonic-gate 				rval = -1;
1097*7c478bd9Sstevel@tonic-gate 			}
1098*7c478bd9Sstevel@tonic-gate 			break;
1099*7c478bd9Sstevel@tonic-gate 		}
1100*7c478bd9Sstevel@tonic-gate 
1101*7c478bd9Sstevel@tonic-gate 		*ptr++ = c;
1102*7c478bd9Sstevel@tonic-gate 		len++;
1103*7c478bd9Sstevel@tonic-gate 	}
1104*7c478bd9Sstevel@tonic-gate 
1105*7c478bd9Sstevel@tonic-gate 	*ptr = '\0';
1106*7c478bd9Sstevel@tonic-gate 	*buflen = len;
1107*7c478bd9Sstevel@tonic-gate 
1108*7c478bd9Sstevel@tonic-gate 	rcm_log_message(RSCR_TRACE,
1109*7c478bd9Sstevel@tonic-gate 		"get_line(%s): rval = %d buflen = %d line = %s\n",
1110*7c478bd9Sstevel@tonic-gate 		fdname, rval, *buflen, buf);
1111*7c478bd9Sstevel@tonic-gate 	return (rval);
1112*7c478bd9Sstevel@tonic-gate }
1113*7c478bd9Sstevel@tonic-gate 
1114*7c478bd9Sstevel@tonic-gate static void
script_exited(script_info_t * rsi)1115*7c478bd9Sstevel@tonic-gate script_exited(script_info_t *rsi)
1116*7c478bd9Sstevel@tonic-gate {
1117*7c478bd9Sstevel@tonic-gate 	if (rsi->flags & STDERR_THREAD_CREATED) {
1118*7c478bd9Sstevel@tonic-gate 		rcm_log_message(RSCR_TRACE,
1119*7c478bd9Sstevel@tonic-gate 		    "script_exited: doing thr_join (%s)\n", rsi->script_name);
1120*7c478bd9Sstevel@tonic-gate 		(void) thr_join(rsi->tid, NULL, NULL);
1121*7c478bd9Sstevel@tonic-gate 		rsi->flags &= ~STDERR_THREAD_CREATED;
1122*7c478bd9Sstevel@tonic-gate 	}
1123*7c478bd9Sstevel@tonic-gate 
1124*7c478bd9Sstevel@tonic-gate 	(void) close(rsi->pipe1[PARENT_END_OF_PIPE]);
1125*7c478bd9Sstevel@tonic-gate 	(void) close(rsi->pipe2[PARENT_END_OF_PIPE]);
1126*7c478bd9Sstevel@tonic-gate 	rsi->pipe1[PARENT_END_OF_PIPE] = -1;
1127*7c478bd9Sstevel@tonic-gate 	rsi->pipe2[PARENT_END_OF_PIPE] = -1;
1128*7c478bd9Sstevel@tonic-gate 
1129*7c478bd9Sstevel@tonic-gate 	script_ps_state_file_remove_entry(rsi->pid);
1130*7c478bd9Sstevel@tonic-gate 	rsi->pid = 0;
1131*7c478bd9Sstevel@tonic-gate 	(void) sema_post(&script_process_sema);
1132*7c478bd9Sstevel@tonic-gate }
1133*7c478bd9Sstevel@tonic-gate 
1134*7c478bd9Sstevel@tonic-gate /*
1135*7c478bd9Sstevel@tonic-gate  * Kill the specified process group
1136*7c478bd9Sstevel@tonic-gate  */
1137*7c478bd9Sstevel@tonic-gate static int
kill_pid(pid_t pid)1138*7c478bd9Sstevel@tonic-gate kill_pid(pid_t pid)
1139*7c478bd9Sstevel@tonic-gate {
1140*7c478bd9Sstevel@tonic-gate 	time_t deadline, timeleft;
1141*7c478bd9Sstevel@tonic-gate 	int child_status;
1142*7c478bd9Sstevel@tonic-gate 
1143*7c478bd9Sstevel@tonic-gate 	/* kill the entire process group */
1144*7c478bd9Sstevel@tonic-gate 	(void) kill(-(pid), SIGKILL);
1145*7c478bd9Sstevel@tonic-gate 
1146*7c478bd9Sstevel@tonic-gate 	/* give some time for the script to be killed */
1147*7c478bd9Sstevel@tonic-gate 	deadline = time(NULL) + SCRIPT_KILL_TIMEOUT;
1148*7c478bd9Sstevel@tonic-gate 	do {
1149*7c478bd9Sstevel@tonic-gate 		if (waitpid(pid, &child_status, WNOHANG) == pid)
1150*7c478bd9Sstevel@tonic-gate 			return (0);
1151*7c478bd9Sstevel@tonic-gate 
1152*7c478bd9Sstevel@tonic-gate 		/* wait for 100 ms */
1153*7c478bd9Sstevel@tonic-gate 		(void) poll(NULL, 0, 100);
1154*7c478bd9Sstevel@tonic-gate 
1155*7c478bd9Sstevel@tonic-gate 		timeleft = deadline - time(NULL);
1156*7c478bd9Sstevel@tonic-gate 	} while (timeleft > 0);
1157*7c478bd9Sstevel@tonic-gate 
1158*7c478bd9Sstevel@tonic-gate 	/* script process was not killed successfully */
1159*7c478bd9Sstevel@tonic-gate 	return (-1);
1160*7c478bd9Sstevel@tonic-gate }
1161*7c478bd9Sstevel@tonic-gate 
1162*7c478bd9Sstevel@tonic-gate /*
1163*7c478bd9Sstevel@tonic-gate  * Kill the specified script.
1164*7c478bd9Sstevel@tonic-gate  */
1165*7c478bd9Sstevel@tonic-gate static void
kill_script(script_info_t * rsi)1166*7c478bd9Sstevel@tonic-gate kill_script(script_info_t *rsi)
1167*7c478bd9Sstevel@tonic-gate {
1168*7c478bd9Sstevel@tonic-gate 	if (rsi->pid > 1) {
1169*7c478bd9Sstevel@tonic-gate 		(void) kill_pid(rsi->pid);
1170*7c478bd9Sstevel@tonic-gate 		script_exited(rsi);
1171*7c478bd9Sstevel@tonic-gate 		remove_drreq_all(rsi);
1172*7c478bd9Sstevel@tonic-gate 	}
1173*7c478bd9Sstevel@tonic-gate }
1174*7c478bd9Sstevel@tonic-gate 
1175*7c478bd9Sstevel@tonic-gate /*
1176*7c478bd9Sstevel@tonic-gate  * Convert rcm flags parameter to a string.
1177*7c478bd9Sstevel@tonic-gate  * Used for debug prints.
1178*7c478bd9Sstevel@tonic-gate  */
1179*7c478bd9Sstevel@tonic-gate static char *
flags_to_name(int flags,char * buf,int maxbuflen)1180*7c478bd9Sstevel@tonic-gate flags_to_name(int flags, char *buf, int maxbuflen)
1181*7c478bd9Sstevel@tonic-gate {
1182*7c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, maxbuflen, "%s%s",
1183*7c478bd9Sstevel@tonic-gate 		(flags & RCM_QUERY) ? "RCM_QUERY " : "",
1184*7c478bd9Sstevel@tonic-gate 		(flags & RCM_FORCE) ? "RCM_FORCE" : "");
1185*7c478bd9Sstevel@tonic-gate 
1186*7c478bd9Sstevel@tonic-gate 	return (buf);
1187*7c478bd9Sstevel@tonic-gate }
1188*7c478bd9Sstevel@tonic-gate 
1189*7c478bd9Sstevel@tonic-gate static void
fill_argv(script_info_t * rsi,char * argv[],char * resource_name)1190*7c478bd9Sstevel@tonic-gate fill_argv(script_info_t *rsi, char *argv[], char *resource_name)
1191*7c478bd9Sstevel@tonic-gate {
1192*7c478bd9Sstevel@tonic-gate 	argv[0] = rsi->script_full_name;
1193*7c478bd9Sstevel@tonic-gate 	argv[1] = script_cmd_name[rsi->cmd];
1194*7c478bd9Sstevel@tonic-gate 	if (resource_name) {
1195*7c478bd9Sstevel@tonic-gate 		argv[2] = resource_name;
1196*7c478bd9Sstevel@tonic-gate 		argv[3] = NULL;
1197*7c478bd9Sstevel@tonic-gate 	} else
1198*7c478bd9Sstevel@tonic-gate 		argv[2] = NULL;
1199*7c478bd9Sstevel@tonic-gate }
1200*7c478bd9Sstevel@tonic-gate 
1201*7c478bd9Sstevel@tonic-gate /*
1202*7c478bd9Sstevel@tonic-gate  * stderr thread:
1203*7c478bd9Sstevel@tonic-gate  * Reads stderr and logs to syslog.
1204*7c478bd9Sstevel@tonic-gate  * Runs as a separate thread.
1205*7c478bd9Sstevel@tonic-gate  */
1206*7c478bd9Sstevel@tonic-gate static void *
read_stderr(script_info_t * rsi)1207*7c478bd9Sstevel@tonic-gate read_stderr(script_info_t *rsi)
1208*7c478bd9Sstevel@tonic-gate {
1209*7c478bd9Sstevel@tonic-gate 	char buf[MAX_LINE_LEN];
1210*7c478bd9Sstevel@tonic-gate 	size_t buflen;
1211*7c478bd9Sstevel@tonic-gate 	int error_num;
1212*7c478bd9Sstevel@tonic-gate 
1213*7c478bd9Sstevel@tonic-gate 	while ((get_line(rsi->pipe2[PARENT_END_OF_PIPE], "stderr",
1214*7c478bd9Sstevel@tonic-gate 		buf, MAX_LINE_LEN, &buflen, 0, &error_num)) == 0) {
1215*7c478bd9Sstevel@tonic-gate 		log_msg(rsi, RCM_ERROR, buf);
1216*7c478bd9Sstevel@tonic-gate 	}
1217*7c478bd9Sstevel@tonic-gate 
1218*7c478bd9Sstevel@tonic-gate 	if (buflen)
1219*7c478bd9Sstevel@tonic-gate 		log_msg(rsi, RCM_ERROR, buf);
1220*7c478bd9Sstevel@tonic-gate 
1221*7c478bd9Sstevel@tonic-gate 	return (NULL);
1222*7c478bd9Sstevel@tonic-gate }
1223*7c478bd9Sstevel@tonic-gate 
1224*7c478bd9Sstevel@tonic-gate /* process return data items passed by scripts to the framework */
1225*7c478bd9Sstevel@tonic-gate static int
process_dataitem(script_info_t * rsi,int token,char * value,char ** errmsg)1226*7c478bd9Sstevel@tonic-gate process_dataitem(script_info_t *rsi, int token, char *value, char **errmsg)
1227*7c478bd9Sstevel@tonic-gate {
1228*7c478bd9Sstevel@tonic-gate 	char *ptr;
1229*7c478bd9Sstevel@tonic-gate 	int status;
1230*7c478bd9Sstevel@tonic-gate 
1231*7c478bd9Sstevel@tonic-gate 	*errmsg = NULL;
1232*7c478bd9Sstevel@tonic-gate 
1233*7c478bd9Sstevel@tonic-gate 	if (*value == '\0')
1234*7c478bd9Sstevel@tonic-gate 		goto error;
1235*7c478bd9Sstevel@tonic-gate 
1236*7c478bd9Sstevel@tonic-gate 	switch (token) {
1237*7c478bd9Sstevel@tonic-gate 	case D_SCRIPT_VERSION:
1238*7c478bd9Sstevel@tonic-gate 		if (rsi->cmd != C_SCRIPTINFO)
1239*7c478bd9Sstevel@tonic-gate 			goto error;
1240*7c478bd9Sstevel@tonic-gate 
1241*7c478bd9Sstevel@tonic-gate 		/* check that value contains only digits */
1242*7c478bd9Sstevel@tonic-gate 		for (ptr = value; *ptr != '\0'; ptr++)
1243*7c478bd9Sstevel@tonic-gate 			if (isdigit((int)(*ptr)) == 0)
1244*7c478bd9Sstevel@tonic-gate 				break;
1245*7c478bd9Sstevel@tonic-gate 
1246*7c478bd9Sstevel@tonic-gate 		if (*ptr == '\0')
1247*7c478bd9Sstevel@tonic-gate 			rsi->ver = atoi(value);
1248*7c478bd9Sstevel@tonic-gate 		else
1249*7c478bd9Sstevel@tonic-gate 			goto error;
1250*7c478bd9Sstevel@tonic-gate 
1251*7c478bd9Sstevel@tonic-gate 		break;
1252*7c478bd9Sstevel@tonic-gate 
1253*7c478bd9Sstevel@tonic-gate 	case D_SCRIPT_FUNC_INFO:
1254*7c478bd9Sstevel@tonic-gate 		if (rsi->cmd != C_SCRIPTINFO)
1255*7c478bd9Sstevel@tonic-gate 			goto error;
1256*7c478bd9Sstevel@tonic-gate 
1257*7c478bd9Sstevel@tonic-gate 		rcmscript_snprintf(&rsi->func_info_buf,
1258*7c478bd9Sstevel@tonic-gate 			&rsi->func_info_buf_len,
1259*7c478bd9Sstevel@tonic-gate 			&rsi->func_info_buf_curptr,
1260*7c478bd9Sstevel@tonic-gate 			"%s", value);
1261*7c478bd9Sstevel@tonic-gate 		break;
1262*7c478bd9Sstevel@tonic-gate 
1263*7c478bd9Sstevel@tonic-gate 	case D_CMD_TIMEOUT:
1264*7c478bd9Sstevel@tonic-gate 		if (rsi->cmd != C_SCRIPTINFO)
1265*7c478bd9Sstevel@tonic-gate 			goto error;
1266*7c478bd9Sstevel@tonic-gate 
1267*7c478bd9Sstevel@tonic-gate 		/* check that value contains only digits */
1268*7c478bd9Sstevel@tonic-gate 		for (ptr = value; *ptr != '\0'; ptr++)
1269*7c478bd9Sstevel@tonic-gate 			if (isdigit((int)(*ptr)) == 0)
1270*7c478bd9Sstevel@tonic-gate 				break;
1271*7c478bd9Sstevel@tonic-gate 
1272*7c478bd9Sstevel@tonic-gate 		if (*ptr == '\0')
1273*7c478bd9Sstevel@tonic-gate 			rsi->cmd_timeout = atoi(value);
1274*7c478bd9Sstevel@tonic-gate 		else
1275*7c478bd9Sstevel@tonic-gate 			goto error;
1276*7c478bd9Sstevel@tonic-gate 		break;
1277*7c478bd9Sstevel@tonic-gate 
1278*7c478bd9Sstevel@tonic-gate 	case D_RESOURCE_NAME:
1279*7c478bd9Sstevel@tonic-gate 		if (rsi->cmd != C_REGISTER)
1280*7c478bd9Sstevel@tonic-gate 			goto error;
1281*7c478bd9Sstevel@tonic-gate 
1282*7c478bd9Sstevel@tonic-gate 		if (get_capacity_descr(value) != NULL)
1283*7c478bd9Sstevel@tonic-gate 			status = rcm_register_capacity(rsi->hdl, value,
1284*7c478bd9Sstevel@tonic-gate 					0, NULL);
1285*7c478bd9Sstevel@tonic-gate 		else
1286*7c478bd9Sstevel@tonic-gate 			status = rcm_register_interest(rsi->hdl, value, 0,
1287*7c478bd9Sstevel@tonic-gate 					NULL);
1288*7c478bd9Sstevel@tonic-gate 
1289*7c478bd9Sstevel@tonic-gate 		if (status == RCM_FAILURE && errno == EALREADY)
1290*7c478bd9Sstevel@tonic-gate 			status = RCM_SUCCESS;
1291*7c478bd9Sstevel@tonic-gate 
1292*7c478bd9Sstevel@tonic-gate 		if (status != RCM_SUCCESS) {
1293*7c478bd9Sstevel@tonic-gate 			rcm_log_message(RCM_ERROR, MS_REGISTER_RSRC_ERR,
1294*7c478bd9Sstevel@tonic-gate 				rsi->script_name, value);
1295*7c478bd9Sstevel@tonic-gate 		}
1296*7c478bd9Sstevel@tonic-gate 
1297*7c478bd9Sstevel@tonic-gate 		remove_from_unregister(rsi, value);
1298*7c478bd9Sstevel@tonic-gate 		break;
1299*7c478bd9Sstevel@tonic-gate 
1300*7c478bd9Sstevel@tonic-gate 	case D_RESOURCE_USAGE_INFO:
1301*7c478bd9Sstevel@tonic-gate 		if (rsi->cmd != C_RESOURCEINFO)
1302*7c478bd9Sstevel@tonic-gate 			goto error;
1303*7c478bd9Sstevel@tonic-gate 
1304*7c478bd9Sstevel@tonic-gate 		rcmscript_snprintf(&rsi->resource_usage_info_buf,
1305*7c478bd9Sstevel@tonic-gate 			&rsi->resource_usage_info_buf_len,
1306*7c478bd9Sstevel@tonic-gate 			&rsi->resource_usage_info_buf_curptr,
1307*7c478bd9Sstevel@tonic-gate 			"%s", value);
1308*7c478bd9Sstevel@tonic-gate 		break;
1309*7c478bd9Sstevel@tonic-gate 
1310*7c478bd9Sstevel@tonic-gate 	case D_FAILURE_REASON:
1311*7c478bd9Sstevel@tonic-gate 		rcmscript_snprintf(&rsi->failure_reason_buf,
1312*7c478bd9Sstevel@tonic-gate 			&rsi->failure_reason_buf_len,
1313*7c478bd9Sstevel@tonic-gate 			&rsi->failure_reason_buf_curptr,
1314*7c478bd9Sstevel@tonic-gate 			"%s", value);
1315*7c478bd9Sstevel@tonic-gate 		break;
1316*7c478bd9Sstevel@tonic-gate 
1317*7c478bd9Sstevel@tonic-gate 	default:
1318*7c478bd9Sstevel@tonic-gate 		goto error;
1319*7c478bd9Sstevel@tonic-gate 	}
1320*7c478bd9Sstevel@tonic-gate 
1321*7c478bd9Sstevel@tonic-gate 	return (0);
1322*7c478bd9Sstevel@tonic-gate 
1323*7c478bd9Sstevel@tonic-gate error:
1324*7c478bd9Sstevel@tonic-gate 	*errmsg = dup_err(RCM_ERROR, MS_PROTOCOL_ERR, rsi->script_name);
1325*7c478bd9Sstevel@tonic-gate 	return (-1);
1326*7c478bd9Sstevel@tonic-gate }
1327*7c478bd9Sstevel@tonic-gate 
1328*7c478bd9Sstevel@tonic-gate /* Send the given command to the script and process return data */
1329*7c478bd9Sstevel@tonic-gate static int
do_cmd(script_info_t * rsi,char * argv[],char * envp[],char ** errmsg)1330*7c478bd9Sstevel@tonic-gate do_cmd(script_info_t *rsi, char *argv[], char *envp[], char **errmsg)
1331*7c478bd9Sstevel@tonic-gate {
1332*7c478bd9Sstevel@tonic-gate 	char buf[MAX_LINE_LEN];
1333*7c478bd9Sstevel@tonic-gate 	size_t buflen;
1334*7c478bd9Sstevel@tonic-gate 	int loglevel = -1, continuelog = 0;
1335*7c478bd9Sstevel@tonic-gate 	char *ptr, *dname, *value;
1336*7c478bd9Sstevel@tonic-gate 	time_t maxsecs;
1337*7c478bd9Sstevel@tonic-gate 	time_t deadline;
1338*7c478bd9Sstevel@tonic-gate 	int sigaborted = 0;
1339*7c478bd9Sstevel@tonic-gate 	int rval, child_status, token;
1340*7c478bd9Sstevel@tonic-gate 	int error_num;
1341*7c478bd9Sstevel@tonic-gate 	int cmd_timeout = rsi->cmd_timeout;
1342*7c478bd9Sstevel@tonic-gate 
1343*7c478bd9Sstevel@tonic-gate 	*errmsg = NULL;
1344*7c478bd9Sstevel@tonic-gate 
1345*7c478bd9Sstevel@tonic-gate 	script_process_sema_wait();
1346*7c478bd9Sstevel@tonic-gate 
1347*7c478bd9Sstevel@tonic-gate 	if (run_script(rsi, argv, envp, errmsg) == -1) {
1348*7c478bd9Sstevel@tonic-gate 		(void) sema_post(&script_process_sema);
1349*7c478bd9Sstevel@tonic-gate 		goto error2;
1350*7c478bd9Sstevel@tonic-gate 	}
1351*7c478bd9Sstevel@tonic-gate 
1352*7c478bd9Sstevel@tonic-gate 	(void) time(&rsi->lastrun);
1353*7c478bd9Sstevel@tonic-gate 	deadline = rsi->lastrun + cmd_timeout;
1354*7c478bd9Sstevel@tonic-gate 
1355*7c478bd9Sstevel@tonic-gate 	if (thr_create(NULL, 0, (void *(*)(void *))read_stderr, rsi,
1356*7c478bd9Sstevel@tonic-gate 	    0, &rsi->tid) != 0) {
1357*7c478bd9Sstevel@tonic-gate 		*errmsg = dup_err(RCM_ERROR, MF_FUNC_CALL_ERR,
1358*7c478bd9Sstevel@tonic-gate 				"thr_create", strerror(errno));
1359*7c478bd9Sstevel@tonic-gate 		goto error1;
1360*7c478bd9Sstevel@tonic-gate 	}
1361*7c478bd9Sstevel@tonic-gate 	rsi->flags |= STDERR_THREAD_CREATED;
1362*7c478bd9Sstevel@tonic-gate 
1363*7c478bd9Sstevel@tonic-gate 	/* LINTED */
1364*7c478bd9Sstevel@tonic-gate 	while (1) {
1365*7c478bd9Sstevel@tonic-gate 		if (cmd_timeout > 0) {
1366*7c478bd9Sstevel@tonic-gate 			maxsecs = deadline - time(NULL);
1367*7c478bd9Sstevel@tonic-gate 			if (maxsecs <= 0)
1368*7c478bd9Sstevel@tonic-gate 				goto timedout;
1369*7c478bd9Sstevel@tonic-gate 		} else
1370*7c478bd9Sstevel@tonic-gate 			maxsecs = 0;
1371*7c478bd9Sstevel@tonic-gate 
1372*7c478bd9Sstevel@tonic-gate 		rval = get_line(rsi->pipe1[PARENT_END_OF_PIPE],
1373*7c478bd9Sstevel@tonic-gate 				"stdout", buf, MAX_LINE_LEN, &buflen,
1374*7c478bd9Sstevel@tonic-gate 				maxsecs, &error_num);
1375*7c478bd9Sstevel@tonic-gate 
1376*7c478bd9Sstevel@tonic-gate 		if (buflen) {
1377*7c478bd9Sstevel@tonic-gate 			if (continuelog)
1378*7c478bd9Sstevel@tonic-gate 				log_msg(rsi, loglevel, buf);
1379*7c478bd9Sstevel@tonic-gate 			else {
1380*7c478bd9Sstevel@tonic-gate 				if ((ptr = strchr(buf, '=')) == NULL)
1381*7c478bd9Sstevel@tonic-gate 					goto error;
1382*7c478bd9Sstevel@tonic-gate 
1383*7c478bd9Sstevel@tonic-gate 				*ptr = '\0';
1384*7c478bd9Sstevel@tonic-gate 				dname = buf;
1385*7c478bd9Sstevel@tonic-gate 				value = ptr + 1;
1386*7c478bd9Sstevel@tonic-gate 				if ((token = dname_to_id(dname)) == -1)
1387*7c478bd9Sstevel@tonic-gate 					goto error;
1388*7c478bd9Sstevel@tonic-gate 
1389*7c478bd9Sstevel@tonic-gate 				switch (token) {
1390*7c478bd9Sstevel@tonic-gate 				case D_LOG_ERR:
1391*7c478bd9Sstevel@tonic-gate 					loglevel = RCM_ERROR;
1392*7c478bd9Sstevel@tonic-gate 					break;
1393*7c478bd9Sstevel@tonic-gate 
1394*7c478bd9Sstevel@tonic-gate 				case D_LOG_WARN:
1395*7c478bd9Sstevel@tonic-gate 					loglevel = RCM_WARNING;
1396*7c478bd9Sstevel@tonic-gate 					break;
1397*7c478bd9Sstevel@tonic-gate 
1398*7c478bd9Sstevel@tonic-gate 				case D_LOG_INFO:
1399*7c478bd9Sstevel@tonic-gate 					loglevel = RCM_INFO;
1400*7c478bd9Sstevel@tonic-gate 					break;
1401*7c478bd9Sstevel@tonic-gate 
1402*7c478bd9Sstevel@tonic-gate 				case D_LOG_DEBUG:
1403*7c478bd9Sstevel@tonic-gate 					loglevel = RCM_DEBUG;
1404*7c478bd9Sstevel@tonic-gate 					break;
1405*7c478bd9Sstevel@tonic-gate 
1406*7c478bd9Sstevel@tonic-gate 				default:
1407*7c478bd9Sstevel@tonic-gate 					loglevel = -1;
1408*7c478bd9Sstevel@tonic-gate 					break;
1409*7c478bd9Sstevel@tonic-gate 				}
1410*7c478bd9Sstevel@tonic-gate 
1411*7c478bd9Sstevel@tonic-gate 				if (loglevel != -1) {
1412*7c478bd9Sstevel@tonic-gate 					log_msg(rsi, loglevel, value);
1413*7c478bd9Sstevel@tonic-gate 					if (buf[buflen - 1] == '\n')
1414*7c478bd9Sstevel@tonic-gate 						continuelog = 0;
1415*7c478bd9Sstevel@tonic-gate 					else
1416*7c478bd9Sstevel@tonic-gate 						continuelog = 1;
1417*7c478bd9Sstevel@tonic-gate 				} else {
1418*7c478bd9Sstevel@tonic-gate 					if (buf[buflen - 1] != '\n')
1419*7c478bd9Sstevel@tonic-gate 						goto error;
1420*7c478bd9Sstevel@tonic-gate 
1421*7c478bd9Sstevel@tonic-gate 					buf[buflen - 1] = '\0';
1422*7c478bd9Sstevel@tonic-gate 					if (process_dataitem(rsi, token,
1423*7c478bd9Sstevel@tonic-gate 						value, errmsg) != 0)
1424*7c478bd9Sstevel@tonic-gate 						goto error1;
1425*7c478bd9Sstevel@tonic-gate 				}
1426*7c478bd9Sstevel@tonic-gate 			}
1427*7c478bd9Sstevel@tonic-gate 		}
1428*7c478bd9Sstevel@tonic-gate 
1429*7c478bd9Sstevel@tonic-gate 		if (rval == -3) {
1430*7c478bd9Sstevel@tonic-gate 			/* script exited */
1431*7c478bd9Sstevel@tonic-gate waitagain:
1432*7c478bd9Sstevel@tonic-gate 			if (waitpid(rsi->pid, &child_status, 0)
1433*7c478bd9Sstevel@tonic-gate 					!= rsi->pid) {
1434*7c478bd9Sstevel@tonic-gate 				if (errno == EINTR || errno == EAGAIN)
1435*7c478bd9Sstevel@tonic-gate 					goto waitagain;
1436*7c478bd9Sstevel@tonic-gate 				*errmsg = dup_err(RCM_ERROR, MS_SCRIPT_ERR,
1437*7c478bd9Sstevel@tonic-gate 					rsi->script_name, strerror(errno));
1438*7c478bd9Sstevel@tonic-gate 				goto error1;
1439*7c478bd9Sstevel@tonic-gate 			}
1440*7c478bd9Sstevel@tonic-gate 
1441*7c478bd9Sstevel@tonic-gate 			if (WIFEXITED(child_status)) {
1442*7c478bd9Sstevel@tonic-gate 				script_exited(rsi);
1443*7c478bd9Sstevel@tonic-gate 				rsi->exit_status = WEXITSTATUS(child_status);
1444*7c478bd9Sstevel@tonic-gate 			} else {
1445*7c478bd9Sstevel@tonic-gate 				if (sigaborted)
1446*7c478bd9Sstevel@tonic-gate 					*errmsg = dup_err(RCM_ERROR,
1447*7c478bd9Sstevel@tonic-gate 					MS_TIMEOUT_ERR, rsi->script_name);
1448*7c478bd9Sstevel@tonic-gate 				else
1449*7c478bd9Sstevel@tonic-gate 					*errmsg = dup_err(RCM_ERROR,
1450*7c478bd9Sstevel@tonic-gate 					MS_UNKNOWN_ERR, rsi->script_name);
1451*7c478bd9Sstevel@tonic-gate 
1452*7c478bd9Sstevel@tonic-gate 				/* kill any remaining processes in the pgrp */
1453*7c478bd9Sstevel@tonic-gate 				(void) kill(-(rsi->pid), SIGKILL);
1454*7c478bd9Sstevel@tonic-gate 				script_exited(rsi);
1455*7c478bd9Sstevel@tonic-gate 				goto error2;
1456*7c478bd9Sstevel@tonic-gate 			}
1457*7c478bd9Sstevel@tonic-gate 
1458*7c478bd9Sstevel@tonic-gate 			break;
1459*7c478bd9Sstevel@tonic-gate 		}
1460*7c478bd9Sstevel@tonic-gate 
1461*7c478bd9Sstevel@tonic-gate 		if (rval == -1) {
1462*7c478bd9Sstevel@tonic-gate 			*errmsg = dup_err(RCM_ERROR, MS_SCRIPT_ERR,
1463*7c478bd9Sstevel@tonic-gate 				rsi->script_name, strerror(errno));
1464*7c478bd9Sstevel@tonic-gate 			goto error1;
1465*7c478bd9Sstevel@tonic-gate 		}
1466*7c478bd9Sstevel@tonic-gate 
1467*7c478bd9Sstevel@tonic-gate 		if (rval == -2) {
1468*7c478bd9Sstevel@tonic-gate timedout:
1469*7c478bd9Sstevel@tonic-gate 			/* timeout occurred */
1470*7c478bd9Sstevel@tonic-gate 			if (sigaborted == 0) {
1471*7c478bd9Sstevel@tonic-gate 				(void) kill(rsi->pid, SIGABRT);
1472*7c478bd9Sstevel@tonic-gate 				sigaborted = 1;
1473*7c478bd9Sstevel@tonic-gate 				/* extend deadline */
1474*7c478bd9Sstevel@tonic-gate 				deadline += SCRIPT_ABORT_TIMEOUT;
1475*7c478bd9Sstevel@tonic-gate 			} else {
1476*7c478bd9Sstevel@tonic-gate 				*errmsg = dup_err(RCM_ERROR,
1477*7c478bd9Sstevel@tonic-gate 					MS_TIMEOUT_ERR, rsi->script_name);
1478*7c478bd9Sstevel@tonic-gate 				goto error1;
1479*7c478bd9Sstevel@tonic-gate 			}
1480*7c478bd9Sstevel@tonic-gate 		}
1481*7c478bd9Sstevel@tonic-gate 	}
1482*7c478bd9Sstevel@tonic-gate 
1483*7c478bd9Sstevel@tonic-gate 	return (0);
1484*7c478bd9Sstevel@tonic-gate 
1485*7c478bd9Sstevel@tonic-gate error:
1486*7c478bd9Sstevel@tonic-gate 	*errmsg = dup_err(RCM_ERROR, MS_PROTOCOL_ERR, rsi->script_name);
1487*7c478bd9Sstevel@tonic-gate 
1488*7c478bd9Sstevel@tonic-gate error1:
1489*7c478bd9Sstevel@tonic-gate 	kill_script(rsi);
1490*7c478bd9Sstevel@tonic-gate 
1491*7c478bd9Sstevel@tonic-gate error2:
1492*7c478bd9Sstevel@tonic-gate 	return (-1);
1493*7c478bd9Sstevel@tonic-gate }
1494*7c478bd9Sstevel@tonic-gate 
1495*7c478bd9Sstevel@tonic-gate static int
do_script_info(script_info_t * rsi)1496*7c478bd9Sstevel@tonic-gate do_script_info(script_info_t *rsi)
1497*7c478bd9Sstevel@tonic-gate {
1498*7c478bd9Sstevel@tonic-gate 	char *argv[MAX_ARGS];
1499*7c478bd9Sstevel@tonic-gate 	int status = RCM_FAILURE;
1500*7c478bd9Sstevel@tonic-gate 	int err = 0;
1501*7c478bd9Sstevel@tonic-gate 	char *errmsg = NULL;
1502*7c478bd9Sstevel@tonic-gate 
1503*7c478bd9Sstevel@tonic-gate 	rcm_log_message(RSCR_TRACE, "do_script_info: script name = %s\n",
1504*7c478bd9Sstevel@tonic-gate 						rsi->script_name);
1505*7c478bd9Sstevel@tonic-gate 
1506*7c478bd9Sstevel@tonic-gate 	rsi->cmd = C_SCRIPTINFO;
1507*7c478bd9Sstevel@tonic-gate 	rsi->func_info_buf = NULL;
1508*7c478bd9Sstevel@tonic-gate 	rsi->failure_reason_buf = NULL;
1509*7c478bd9Sstevel@tonic-gate 	fill_argv(rsi, argv, NULL);
1510*7c478bd9Sstevel@tonic-gate 
1511*7c478bd9Sstevel@tonic-gate 	if (do_cmd(rsi, argv, script_env, &errmsg) == 0) {
1512*7c478bd9Sstevel@tonic-gate 		switch (rsi->exit_status) {
1513*7c478bd9Sstevel@tonic-gate 		case E_SUCCESS:
1514*7c478bd9Sstevel@tonic-gate 			if (rsi->func_info_buf != NULL &&
1515*7c478bd9Sstevel@tonic-gate 				rsi->failure_reason_buf == NULL) {
1516*7c478bd9Sstevel@tonic-gate 
1517*7c478bd9Sstevel@tonic-gate 				if (rsi->ver >= SCRIPT_API_MIN_VER &&
1518*7c478bd9Sstevel@tonic-gate 					rsi->ver <= SCRIPT_API_MAX_VER)
1519*7c478bd9Sstevel@tonic-gate 					status = RCM_SUCCESS;
1520*7c478bd9Sstevel@tonic-gate 				else
1521*7c478bd9Sstevel@tonic-gate 					rcm_log_message(RCM_ERROR,
1522*7c478bd9Sstevel@tonic-gate 					MS_UNSUPPORTED_VER, rsi->script_name,
1523*7c478bd9Sstevel@tonic-gate 					rsi->ver);
1524*7c478bd9Sstevel@tonic-gate 			} else
1525*7c478bd9Sstevel@tonic-gate 				err = 1;
1526*7c478bd9Sstevel@tonic-gate 			break;
1527*7c478bd9Sstevel@tonic-gate 
1528*7c478bd9Sstevel@tonic-gate 		case E_FAILURE:
1529*7c478bd9Sstevel@tonic-gate 			if (rsi->failure_reason_buf != NULL) {
1530*7c478bd9Sstevel@tonic-gate 				rcm_log_message(RCM_ERROR, MS_SCRIPTINFO_ERR,
1531*7c478bd9Sstevel@tonic-gate 					rsi->script_name,
1532*7c478bd9Sstevel@tonic-gate 					rsi->failure_reason_buf);
1533*7c478bd9Sstevel@tonic-gate 			} else
1534*7c478bd9Sstevel@tonic-gate 				err = 1;
1535*7c478bd9Sstevel@tonic-gate 			break;
1536*7c478bd9Sstevel@tonic-gate 
1537*7c478bd9Sstevel@tonic-gate 		default:
1538*7c478bd9Sstevel@tonic-gate 			err = 1;
1539*7c478bd9Sstevel@tonic-gate 			break;
1540*7c478bd9Sstevel@tonic-gate 		}
1541*7c478bd9Sstevel@tonic-gate 		if (err)
1542*7c478bd9Sstevel@tonic-gate 			rcm_log_message(RCM_ERROR, MS_PROTOCOL_ERR,
1543*7c478bd9Sstevel@tonic-gate 				rsi->script_name);
1544*7c478bd9Sstevel@tonic-gate 	} else if (errmsg)
1545*7c478bd9Sstevel@tonic-gate 		(void) free(errmsg);
1546*7c478bd9Sstevel@tonic-gate 
1547*7c478bd9Sstevel@tonic-gate 	if (status != RCM_SUCCESS && rsi->func_info_buf != NULL)
1548*7c478bd9Sstevel@tonic-gate 		free(rsi->func_info_buf);
1549*7c478bd9Sstevel@tonic-gate 
1550*7c478bd9Sstevel@tonic-gate 	if (rsi->failure_reason_buf)
1551*7c478bd9Sstevel@tonic-gate 		free(rsi->failure_reason_buf);
1552*7c478bd9Sstevel@tonic-gate 
1553*7c478bd9Sstevel@tonic-gate 	return (status);
1554*7c478bd9Sstevel@tonic-gate }
1555*7c478bd9Sstevel@tonic-gate 
1556*7c478bd9Sstevel@tonic-gate static int
do_dr(script_info_t * rsi,char * argv[],char * envp[],char ** info)1557*7c478bd9Sstevel@tonic-gate do_dr(script_info_t *rsi, char *argv[], char *envp[], char **info)
1558*7c478bd9Sstevel@tonic-gate {
1559*7c478bd9Sstevel@tonic-gate 	int status = RCM_FAILURE;
1560*7c478bd9Sstevel@tonic-gate 	int err = 0;
1561*7c478bd9Sstevel@tonic-gate 
1562*7c478bd9Sstevel@tonic-gate 	rsi->failure_reason_buf = NULL;
1563*7c478bd9Sstevel@tonic-gate 
1564*7c478bd9Sstevel@tonic-gate 	if (do_cmd(rsi, argv, envp, info) == 0) {
1565*7c478bd9Sstevel@tonic-gate 		switch (rsi->exit_status) {
1566*7c478bd9Sstevel@tonic-gate 		case E_SUCCESS:
1567*7c478bd9Sstevel@tonic-gate 		case E_UNSUPPORTED_CMD:
1568*7c478bd9Sstevel@tonic-gate 			if (rsi->failure_reason_buf == NULL)
1569*7c478bd9Sstevel@tonic-gate 				status = RCM_SUCCESS;
1570*7c478bd9Sstevel@tonic-gate 			else
1571*7c478bd9Sstevel@tonic-gate 				err = 1;
1572*7c478bd9Sstevel@tonic-gate 			break;
1573*7c478bd9Sstevel@tonic-gate 
1574*7c478bd9Sstevel@tonic-gate 		case E_FAILURE:
1575*7c478bd9Sstevel@tonic-gate 		case E_REFUSE:
1576*7c478bd9Sstevel@tonic-gate 			if (rsi->failure_reason_buf != NULL) {
1577*7c478bd9Sstevel@tonic-gate 				*info = rsi->failure_reason_buf;
1578*7c478bd9Sstevel@tonic-gate 				rsi->failure_reason_buf = NULL;
1579*7c478bd9Sstevel@tonic-gate 			} else
1580*7c478bd9Sstevel@tonic-gate 				err = 1;
1581*7c478bd9Sstevel@tonic-gate 			break;
1582*7c478bd9Sstevel@tonic-gate 
1583*7c478bd9Sstevel@tonic-gate 		default:
1584*7c478bd9Sstevel@tonic-gate 			err = 1;
1585*7c478bd9Sstevel@tonic-gate 			break;
1586*7c478bd9Sstevel@tonic-gate 		}
1587*7c478bd9Sstevel@tonic-gate 
1588*7c478bd9Sstevel@tonic-gate 		if (err)
1589*7c478bd9Sstevel@tonic-gate 			*info = dup_err(RCM_ERROR, MS_PROTOCOL_ERR,
1590*7c478bd9Sstevel@tonic-gate 				rsi->script_name);
1591*7c478bd9Sstevel@tonic-gate 	}
1592*7c478bd9Sstevel@tonic-gate 
1593*7c478bd9Sstevel@tonic-gate 	if (rsi->failure_reason_buf)
1594*7c478bd9Sstevel@tonic-gate 		free(rsi->failure_reason_buf);
1595*7c478bd9Sstevel@tonic-gate 
1596*7c478bd9Sstevel@tonic-gate 	return (status);
1597*7c478bd9Sstevel@tonic-gate }
1598*7c478bd9Sstevel@tonic-gate 
1599*7c478bd9Sstevel@tonic-gate /*
1600*7c478bd9Sstevel@tonic-gate  * get_info entry point
1601*7c478bd9Sstevel@tonic-gate  */
1602*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
1603*7c478bd9Sstevel@tonic-gate static int
script_get_info(rcm_handle_t * hdl,char * resource_name,pid_t pid,uint_t flag,char ** info,char ** error,nvlist_t * props,rcm_info_t ** dependent_info)1604*7c478bd9Sstevel@tonic-gate script_get_info(rcm_handle_t *hdl,
1605*7c478bd9Sstevel@tonic-gate 	char *resource_name,
1606*7c478bd9Sstevel@tonic-gate 	pid_t pid,
1607*7c478bd9Sstevel@tonic-gate 	uint_t flag,
1608*7c478bd9Sstevel@tonic-gate 	char **info,
1609*7c478bd9Sstevel@tonic-gate 	char **error,
1610*7c478bd9Sstevel@tonic-gate 	nvlist_t *props,
1611*7c478bd9Sstevel@tonic-gate 	rcm_info_t **dependent_info)
1612*7c478bd9Sstevel@tonic-gate {
1613*7c478bd9Sstevel@tonic-gate 	script_info_t *rsi = hdl->module->rsi;
1614*7c478bd9Sstevel@tonic-gate 	char *argv[MAX_ARGS];
1615*7c478bd9Sstevel@tonic-gate 	int status = RCM_FAILURE;
1616*7c478bd9Sstevel@tonic-gate 	int err = 0;
1617*7c478bd9Sstevel@tonic-gate 
1618*7c478bd9Sstevel@tonic-gate 	rcm_log_message(RSCR_TRACE, "script_get_info: resource = %s\n",
1619*7c478bd9Sstevel@tonic-gate 				resource_name);
1620*7c478bd9Sstevel@tonic-gate 
1621*7c478bd9Sstevel@tonic-gate 	*info = NULL;
1622*7c478bd9Sstevel@tonic-gate 	*error = NULL;
1623*7c478bd9Sstevel@tonic-gate 
1624*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&rsi->channel_lock);
1625*7c478bd9Sstevel@tonic-gate 
1626*7c478bd9Sstevel@tonic-gate 	rsi->hdl = hdl;
1627*7c478bd9Sstevel@tonic-gate 	rsi->cmd = C_RESOURCEINFO;
1628*7c478bd9Sstevel@tonic-gate 	rsi->resource_usage_info_buf = NULL;
1629*7c478bd9Sstevel@tonic-gate 	rsi->failure_reason_buf = NULL;
1630*7c478bd9Sstevel@tonic-gate 	fill_argv(rsi, argv, resource_name);
1631*7c478bd9Sstevel@tonic-gate 
1632*7c478bd9Sstevel@tonic-gate 	if (do_cmd(rsi, argv, script_env, error) == 0) {
1633*7c478bd9Sstevel@tonic-gate 		switch (rsi->exit_status) {
1634*7c478bd9Sstevel@tonic-gate 		case E_SUCCESS:
1635*7c478bd9Sstevel@tonic-gate 			if (rsi->resource_usage_info_buf != NULL &&
1636*7c478bd9Sstevel@tonic-gate 				rsi->failure_reason_buf == NULL) {
1637*7c478bd9Sstevel@tonic-gate 
1638*7c478bd9Sstevel@tonic-gate 				*info = rsi->resource_usage_info_buf;
1639*7c478bd9Sstevel@tonic-gate 				rsi->resource_usage_info_buf = NULL;
1640*7c478bd9Sstevel@tonic-gate 				status = RCM_SUCCESS;
1641*7c478bd9Sstevel@tonic-gate 			} else
1642*7c478bd9Sstevel@tonic-gate 				err = 1;
1643*7c478bd9Sstevel@tonic-gate 			break;
1644*7c478bd9Sstevel@tonic-gate 
1645*7c478bd9Sstevel@tonic-gate 		case E_FAILURE:
1646*7c478bd9Sstevel@tonic-gate 			if (rsi->failure_reason_buf != NULL) {
1647*7c478bd9Sstevel@tonic-gate 				*error = rsi->failure_reason_buf;
1648*7c478bd9Sstevel@tonic-gate 				rsi->failure_reason_buf = NULL;
1649*7c478bd9Sstevel@tonic-gate 			} else
1650*7c478bd9Sstevel@tonic-gate 				err = 1;
1651*7c478bd9Sstevel@tonic-gate 			break;
1652*7c478bd9Sstevel@tonic-gate 
1653*7c478bd9Sstevel@tonic-gate 		default:
1654*7c478bd9Sstevel@tonic-gate 			err = 1;
1655*7c478bd9Sstevel@tonic-gate 			break;
1656*7c478bd9Sstevel@tonic-gate 		}
1657*7c478bd9Sstevel@tonic-gate 		if (err)
1658*7c478bd9Sstevel@tonic-gate 			*error = dup_err(RCM_ERROR, MS_PROTOCOL_ERR,
1659*7c478bd9Sstevel@tonic-gate 				rsi->script_name);
1660*7c478bd9Sstevel@tonic-gate 	}
1661*7c478bd9Sstevel@tonic-gate 
1662*7c478bd9Sstevel@tonic-gate 	if (rsi->resource_usage_info_buf)
1663*7c478bd9Sstevel@tonic-gate 		free(rsi->resource_usage_info_buf);
1664*7c478bd9Sstevel@tonic-gate 
1665*7c478bd9Sstevel@tonic-gate 	if (rsi->failure_reason_buf)
1666*7c478bd9Sstevel@tonic-gate 		free(rsi->failure_reason_buf);
1667*7c478bd9Sstevel@tonic-gate 
1668*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&rsi->channel_lock);
1669*7c478bd9Sstevel@tonic-gate 
1670*7c478bd9Sstevel@tonic-gate 	return (status);
1671*7c478bd9Sstevel@tonic-gate }
1672*7c478bd9Sstevel@tonic-gate 
1673*7c478bd9Sstevel@tonic-gate static void
add_for_unregister(script_info_t * rsi)1674*7c478bd9Sstevel@tonic-gate add_for_unregister(script_info_t *rsi)
1675*7c478bd9Sstevel@tonic-gate {
1676*7c478bd9Sstevel@tonic-gate 	module_t *module = rsi->module;
1677*7c478bd9Sstevel@tonic-gate 	client_t *client;
1678*7c478bd9Sstevel@tonic-gate 	rcm_queue_t *head;
1679*7c478bd9Sstevel@tonic-gate 	rcm_queue_t *q;
1680*7c478bd9Sstevel@tonic-gate 
1681*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&rcm_req_lock);
1682*7c478bd9Sstevel@tonic-gate 
1683*7c478bd9Sstevel@tonic-gate 	head = &module->client_q;
1684*7c478bd9Sstevel@tonic-gate 
1685*7c478bd9Sstevel@tonic-gate 	for (q = head->next; q != head; q = q->next) {
1686*7c478bd9Sstevel@tonic-gate 		client = RCM_STRUCT_BASE_ADDR(client_t, q, queue);
1687*7c478bd9Sstevel@tonic-gate 		client->prv_flags |= RCM_NEED_TO_UNREGISTER;
1688*7c478bd9Sstevel@tonic-gate 	}
1689*7c478bd9Sstevel@tonic-gate 
1690*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&rcm_req_lock);
1691*7c478bd9Sstevel@tonic-gate }
1692*7c478bd9Sstevel@tonic-gate 
1693*7c478bd9Sstevel@tonic-gate static void
remove_from_unregister(script_info_t * rsi,char * resource_name)1694*7c478bd9Sstevel@tonic-gate remove_from_unregister(script_info_t *rsi, char *resource_name)
1695*7c478bd9Sstevel@tonic-gate {
1696*7c478bd9Sstevel@tonic-gate 	module_t *module = rsi->module;
1697*7c478bd9Sstevel@tonic-gate 	client_t *client;
1698*7c478bd9Sstevel@tonic-gate 	rcm_queue_t *head;
1699*7c478bd9Sstevel@tonic-gate 	rcm_queue_t *q;
1700*7c478bd9Sstevel@tonic-gate 
1701*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&rcm_req_lock);
1702*7c478bd9Sstevel@tonic-gate 
1703*7c478bd9Sstevel@tonic-gate 	head = &module->client_q;
1704*7c478bd9Sstevel@tonic-gate 
1705*7c478bd9Sstevel@tonic-gate 	for (q = head->next; q != head; q = q->next) {
1706*7c478bd9Sstevel@tonic-gate 		client = RCM_STRUCT_BASE_ADDR(client_t, q, queue);
1707*7c478bd9Sstevel@tonic-gate 		if (strcmp(client->alias, resource_name) == 0) {
1708*7c478bd9Sstevel@tonic-gate 			client->prv_flags &= ~RCM_NEED_TO_UNREGISTER;
1709*7c478bd9Sstevel@tonic-gate 			break;
1710*7c478bd9Sstevel@tonic-gate 		}
1711*7c478bd9Sstevel@tonic-gate 	}
1712*7c478bd9Sstevel@tonic-gate 
1713*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&rcm_req_lock);
1714*7c478bd9Sstevel@tonic-gate }
1715*7c478bd9Sstevel@tonic-gate 
1716*7c478bd9Sstevel@tonic-gate static void
complete_unregister(script_info_t * rsi)1717*7c478bd9Sstevel@tonic-gate complete_unregister(script_info_t *rsi)
1718*7c478bd9Sstevel@tonic-gate {
1719*7c478bd9Sstevel@tonic-gate 	module_t *module = rsi->module;
1720*7c478bd9Sstevel@tonic-gate 	client_t *client;
1721*7c478bd9Sstevel@tonic-gate 	rcm_queue_t *head;
1722*7c478bd9Sstevel@tonic-gate 	rcm_queue_t *q;
1723*7c478bd9Sstevel@tonic-gate 
1724*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&rcm_req_lock);
1725*7c478bd9Sstevel@tonic-gate 
1726*7c478bd9Sstevel@tonic-gate 	head = &module->client_q;
1727*7c478bd9Sstevel@tonic-gate 
1728*7c478bd9Sstevel@tonic-gate 	for (q = head->next; q != head; q = q->next) {
1729*7c478bd9Sstevel@tonic-gate 		client = RCM_STRUCT_BASE_ADDR(client_t, q, queue);
1730*7c478bd9Sstevel@tonic-gate 		if (client->prv_flags & RCM_NEED_TO_UNREGISTER) {
1731*7c478bd9Sstevel@tonic-gate 			client->prv_flags &= ~RCM_NEED_TO_UNREGISTER;
1732*7c478bd9Sstevel@tonic-gate 			client->state = RCM_STATE_REMOVE;
1733*7c478bd9Sstevel@tonic-gate 		}
1734*7c478bd9Sstevel@tonic-gate 	}
1735*7c478bd9Sstevel@tonic-gate 
1736*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&rcm_req_lock);
1737*7c478bd9Sstevel@tonic-gate }
1738*7c478bd9Sstevel@tonic-gate 
1739*7c478bd9Sstevel@tonic-gate /*
1740*7c478bd9Sstevel@tonic-gate  * register_interest entry point
1741*7c478bd9Sstevel@tonic-gate  */
1742*7c478bd9Sstevel@tonic-gate static int
script_register_interest(rcm_handle_t * hdl)1743*7c478bd9Sstevel@tonic-gate script_register_interest(rcm_handle_t *hdl)
1744*7c478bd9Sstevel@tonic-gate {
1745*7c478bd9Sstevel@tonic-gate 	script_info_t *rsi = hdl->module->rsi;
1746*7c478bd9Sstevel@tonic-gate 	char *argv[MAX_ARGS];
1747*7c478bd9Sstevel@tonic-gate 	int status = RCM_FAILURE;
1748*7c478bd9Sstevel@tonic-gate 	int err = 0;
1749*7c478bd9Sstevel@tonic-gate 	char *errmsg = NULL;
1750*7c478bd9Sstevel@tonic-gate 
1751*7c478bd9Sstevel@tonic-gate 	rcm_log_message(RSCR_TRACE,
1752*7c478bd9Sstevel@tonic-gate 		"script_register_interest: script name = %s\n",
1753*7c478bd9Sstevel@tonic-gate 		rsi->script_name);
1754*7c478bd9Sstevel@tonic-gate 
1755*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&rsi->channel_lock);
1756*7c478bd9Sstevel@tonic-gate 
1757*7c478bd9Sstevel@tonic-gate 	if (rsi->drreq_q.next != &rsi->drreq_q) {
1758*7c478bd9Sstevel@tonic-gate 		/* if DR is already in progress no need to register again */
1759*7c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&rsi->channel_lock);
1760*7c478bd9Sstevel@tonic-gate 		return (RCM_SUCCESS);
1761*7c478bd9Sstevel@tonic-gate 	}
1762*7c478bd9Sstevel@tonic-gate 
1763*7c478bd9Sstevel@tonic-gate 	rsi->hdl = hdl;
1764*7c478bd9Sstevel@tonic-gate 	rsi->cmd = C_REGISTER;
1765*7c478bd9Sstevel@tonic-gate 	rsi->failure_reason_buf = NULL;
1766*7c478bd9Sstevel@tonic-gate 	fill_argv(rsi, argv, NULL);
1767*7c478bd9Sstevel@tonic-gate 
1768*7c478bd9Sstevel@tonic-gate 	add_for_unregister(rsi);
1769*7c478bd9Sstevel@tonic-gate 
1770*7c478bd9Sstevel@tonic-gate 	if (do_cmd(rsi, argv, script_env, &errmsg) == 0) {
1771*7c478bd9Sstevel@tonic-gate 		switch (rsi->exit_status) {
1772*7c478bd9Sstevel@tonic-gate 		case E_SUCCESS:
1773*7c478bd9Sstevel@tonic-gate 			status = RCM_SUCCESS;
1774*7c478bd9Sstevel@tonic-gate 			break;
1775*7c478bd9Sstevel@tonic-gate 
1776*7c478bd9Sstevel@tonic-gate 		case E_FAILURE:
1777*7c478bd9Sstevel@tonic-gate 			if (rsi->failure_reason_buf != NULL) {
1778*7c478bd9Sstevel@tonic-gate 				rcm_log_message(RCM_ERROR, MS_REGISTER_ERR,
1779*7c478bd9Sstevel@tonic-gate 					rsi->script_name,
1780*7c478bd9Sstevel@tonic-gate 					rsi->failure_reason_buf);
1781*7c478bd9Sstevel@tonic-gate 			} else
1782*7c478bd9Sstevel@tonic-gate 				err = 1;
1783*7c478bd9Sstevel@tonic-gate 			break;
1784*7c478bd9Sstevel@tonic-gate 
1785*7c478bd9Sstevel@tonic-gate 		default:
1786*7c478bd9Sstevel@tonic-gate 			err = 1;
1787*7c478bd9Sstevel@tonic-gate 			break;
1788*7c478bd9Sstevel@tonic-gate 		}
1789*7c478bd9Sstevel@tonic-gate 		if (err)
1790*7c478bd9Sstevel@tonic-gate 			rcm_log_message(RCM_ERROR, MS_PROTOCOL_ERR,
1791*7c478bd9Sstevel@tonic-gate 				rsi->script_name);
1792*7c478bd9Sstevel@tonic-gate 	} else if (errmsg)
1793*7c478bd9Sstevel@tonic-gate 		(void) free(errmsg);
1794*7c478bd9Sstevel@tonic-gate 
1795*7c478bd9Sstevel@tonic-gate 	complete_unregister(rsi);
1796*7c478bd9Sstevel@tonic-gate 
1797*7c478bd9Sstevel@tonic-gate 	if (rsi->failure_reason_buf)
1798*7c478bd9Sstevel@tonic-gate 		free(rsi->failure_reason_buf);
1799*7c478bd9Sstevel@tonic-gate 
1800*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&rsi->channel_lock);
1801*7c478bd9Sstevel@tonic-gate 
1802*7c478bd9Sstevel@tonic-gate 	return (status);
1803*7c478bd9Sstevel@tonic-gate }
1804*7c478bd9Sstevel@tonic-gate 
1805*7c478bd9Sstevel@tonic-gate /*
1806*7c478bd9Sstevel@tonic-gate  * Add the specified resource name to the drreq_q.
1807*7c478bd9Sstevel@tonic-gate  */
1808*7c478bd9Sstevel@tonic-gate static void
add_drreq(script_info_t * rsi,char * resource_name)1809*7c478bd9Sstevel@tonic-gate add_drreq(script_info_t *rsi, char *resource_name)
1810*7c478bd9Sstevel@tonic-gate {
1811*7c478bd9Sstevel@tonic-gate 	rcm_queue_t *head = &rsi->drreq_q;
1812*7c478bd9Sstevel@tonic-gate 	rcm_queue_t *q;
1813*7c478bd9Sstevel@tonic-gate 	drreq_t *drreq;
1814*7c478bd9Sstevel@tonic-gate 
1815*7c478bd9Sstevel@tonic-gate 	/* check if the dr req is already in the list */
1816*7c478bd9Sstevel@tonic-gate 	for (q = head->next; q != head; q = q->next) {
1817*7c478bd9Sstevel@tonic-gate 		drreq = RCM_STRUCT_BASE_ADDR(drreq_t, q, queue);
1818*7c478bd9Sstevel@tonic-gate 		if (strcmp(drreq->resource_name, resource_name) == 0)
1819*7c478bd9Sstevel@tonic-gate 			/* dr req is already present in the queue */
1820*7c478bd9Sstevel@tonic-gate 			return;
1821*7c478bd9Sstevel@tonic-gate 	}
1822*7c478bd9Sstevel@tonic-gate 
1823*7c478bd9Sstevel@tonic-gate 	drreq = (drreq_t *)rcmscript_calloc(1, sizeof (drreq_t));
1824*7c478bd9Sstevel@tonic-gate 	drreq->resource_name = rcmscript_strdup(resource_name);
1825*7c478bd9Sstevel@tonic-gate 
1826*7c478bd9Sstevel@tonic-gate 	rcm_enqueue_tail(&rsi->drreq_q, &drreq->queue);
1827*7c478bd9Sstevel@tonic-gate }
1828*7c478bd9Sstevel@tonic-gate 
1829*7c478bd9Sstevel@tonic-gate /*
1830*7c478bd9Sstevel@tonic-gate  * Remove the dr req for the specified resource name from the drreq_q.
1831*7c478bd9Sstevel@tonic-gate  */
1832*7c478bd9Sstevel@tonic-gate static void
remove_drreq(script_info_t * rsi,char * resource_name)1833*7c478bd9Sstevel@tonic-gate remove_drreq(script_info_t *rsi, char *resource_name)
1834*7c478bd9Sstevel@tonic-gate {
1835*7c478bd9Sstevel@tonic-gate 	rcm_queue_t *head = &rsi->drreq_q;
1836*7c478bd9Sstevel@tonic-gate 	rcm_queue_t *q;
1837*7c478bd9Sstevel@tonic-gate 	drreq_t *drreq;
1838*7c478bd9Sstevel@tonic-gate 
1839*7c478bd9Sstevel@tonic-gate 	/* search for dr req and remove from the list */
1840*7c478bd9Sstevel@tonic-gate 	for (q = head->next; q != head; q = q->next) {
1841*7c478bd9Sstevel@tonic-gate 		drreq = RCM_STRUCT_BASE_ADDR(drreq_t, q, queue);
1842*7c478bd9Sstevel@tonic-gate 		if (strcmp(drreq->resource_name, resource_name) == 0)
1843*7c478bd9Sstevel@tonic-gate 			break;
1844*7c478bd9Sstevel@tonic-gate 	}
1845*7c478bd9Sstevel@tonic-gate 
1846*7c478bd9Sstevel@tonic-gate 	if (q != head) {
1847*7c478bd9Sstevel@tonic-gate 		/* found drreq on the queue */
1848*7c478bd9Sstevel@tonic-gate 		rcm_dequeue(&drreq->queue);
1849*7c478bd9Sstevel@tonic-gate 		free(drreq->resource_name);
1850*7c478bd9Sstevel@tonic-gate 		free(drreq);
1851*7c478bd9Sstevel@tonic-gate 	}
1852*7c478bd9Sstevel@tonic-gate }
1853*7c478bd9Sstevel@tonic-gate 
1854*7c478bd9Sstevel@tonic-gate /*
1855*7c478bd9Sstevel@tonic-gate  * Remove all dr req's.
1856*7c478bd9Sstevel@tonic-gate  */
1857*7c478bd9Sstevel@tonic-gate static void
remove_drreq_all(script_info_t * rsi)1858*7c478bd9Sstevel@tonic-gate remove_drreq_all(script_info_t *rsi)
1859*7c478bd9Sstevel@tonic-gate {
1860*7c478bd9Sstevel@tonic-gate 	drreq_t *drreq;
1861*7c478bd9Sstevel@tonic-gate 
1862*7c478bd9Sstevel@tonic-gate 	while (rsi->drreq_q.next != &rsi->drreq_q) {
1863*7c478bd9Sstevel@tonic-gate 		drreq = RCM_STRUCT_BASE_ADDR(drreq_t,
1864*7c478bd9Sstevel@tonic-gate 					rsi->drreq_q.next, queue);
1865*7c478bd9Sstevel@tonic-gate 		remove_drreq(rsi, drreq->resource_name);
1866*7c478bd9Sstevel@tonic-gate 	}
1867*7c478bd9Sstevel@tonic-gate }
1868*7c478bd9Sstevel@tonic-gate 
1869*7c478bd9Sstevel@tonic-gate /*
1870*7c478bd9Sstevel@tonic-gate  * request_offline entry point
1871*7c478bd9Sstevel@tonic-gate  */
1872*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
1873*7c478bd9Sstevel@tonic-gate static int
script_request_offline(rcm_handle_t * hdl,char * resource_name,pid_t pid,uint_t flag,char ** info,rcm_info_t ** dependent_info)1874*7c478bd9Sstevel@tonic-gate script_request_offline(rcm_handle_t *hdl,
1875*7c478bd9Sstevel@tonic-gate 	char *resource_name,
1876*7c478bd9Sstevel@tonic-gate 	pid_t pid,
1877*7c478bd9Sstevel@tonic-gate 	uint_t flag,
1878*7c478bd9Sstevel@tonic-gate 	char **info,
1879*7c478bd9Sstevel@tonic-gate 	rcm_info_t **dependent_info)
1880*7c478bd9Sstevel@tonic-gate {
1881*7c478bd9Sstevel@tonic-gate 	script_info_t *rsi = hdl->module->rsi;
1882*7c478bd9Sstevel@tonic-gate 	char *argv[MAX_ARGS];
1883*7c478bd9Sstevel@tonic-gate 	char *envp[MAX_ENV_PARAMS];
1884*7c478bd9Sstevel@tonic-gate 	char flags_name[MAX_FLAGS_NAME_LEN];
1885*7c478bd9Sstevel@tonic-gate 	int status;
1886*7c478bd9Sstevel@tonic-gate 	int i;
1887*7c478bd9Sstevel@tonic-gate 
1888*7c478bd9Sstevel@tonic-gate 	rcm_log_message(RSCR_TRACE,
1889*7c478bd9Sstevel@tonic-gate 		"script_request_offline: resource = %s flags = %s\n",
1890*7c478bd9Sstevel@tonic-gate 			resource_name,
1891*7c478bd9Sstevel@tonic-gate 			flags_to_name(flag, flags_name, MAX_FLAGS_NAME_LEN));
1892*7c478bd9Sstevel@tonic-gate 
1893*7c478bd9Sstevel@tonic-gate 	*info = NULL;
1894*7c478bd9Sstevel@tonic-gate 
1895*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&rsi->channel_lock);
1896*7c478bd9Sstevel@tonic-gate 
1897*7c478bd9Sstevel@tonic-gate 	rsi->hdl = hdl;
1898*7c478bd9Sstevel@tonic-gate 	rsi->cmd = (flag & RCM_QUERY) ? C_QUERYREMOVE : C_PREREMOVE;
1899*7c478bd9Sstevel@tonic-gate 
1900*7c478bd9Sstevel@tonic-gate 	if (rsi->cmd == C_PREREMOVE)
1901*7c478bd9Sstevel@tonic-gate 		add_drreq(rsi, resource_name);
1902*7c478bd9Sstevel@tonic-gate 
1903*7c478bd9Sstevel@tonic-gate 	fill_argv(rsi, argv, resource_name);
1904*7c478bd9Sstevel@tonic-gate 	copy_env(script_env, envp);
1905*7c478bd9Sstevel@tonic-gate 	for (i = 0; envp[i] != NULL; i++)
1906*7c478bd9Sstevel@tonic-gate 		;
1907*7c478bd9Sstevel@tonic-gate 	envp[i++] = (flag & RCM_FORCE) ? script_env_force : script_env_noforce;
1908*7c478bd9Sstevel@tonic-gate 	envp[i] = NULL;
1909*7c478bd9Sstevel@tonic-gate 
1910*7c478bd9Sstevel@tonic-gate 	status = do_dr(rsi, argv, envp, info);
1911*7c478bd9Sstevel@tonic-gate 
1912*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&rsi->channel_lock);
1913*7c478bd9Sstevel@tonic-gate 	return (status);
1914*7c478bd9Sstevel@tonic-gate }
1915*7c478bd9Sstevel@tonic-gate 
1916*7c478bd9Sstevel@tonic-gate /*
1917*7c478bd9Sstevel@tonic-gate  * notify_online entry point
1918*7c478bd9Sstevel@tonic-gate  */
1919*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
1920*7c478bd9Sstevel@tonic-gate static int
script_notify_online(rcm_handle_t * hdl,char * resource_name,pid_t pid,uint_t flag,char ** info,rcm_info_t ** dependent_info)1921*7c478bd9Sstevel@tonic-gate script_notify_online(rcm_handle_t *hdl,
1922*7c478bd9Sstevel@tonic-gate 	char *resource_name,
1923*7c478bd9Sstevel@tonic-gate 	pid_t pid,
1924*7c478bd9Sstevel@tonic-gate 	uint_t flag,
1925*7c478bd9Sstevel@tonic-gate 	char **info,
1926*7c478bd9Sstevel@tonic-gate 	rcm_info_t **dependent_info)
1927*7c478bd9Sstevel@tonic-gate {
1928*7c478bd9Sstevel@tonic-gate 	script_info_t *rsi = hdl->module->rsi;
1929*7c478bd9Sstevel@tonic-gate 	char *argv[MAX_ARGS];
1930*7c478bd9Sstevel@tonic-gate 	int status;
1931*7c478bd9Sstevel@tonic-gate 
1932*7c478bd9Sstevel@tonic-gate 	rcm_log_message(RSCR_TRACE, "script_notify_online: resource = %s\n",
1933*7c478bd9Sstevel@tonic-gate 				resource_name);
1934*7c478bd9Sstevel@tonic-gate 
1935*7c478bd9Sstevel@tonic-gate 	*info = NULL;
1936*7c478bd9Sstevel@tonic-gate 
1937*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&rsi->channel_lock);
1938*7c478bd9Sstevel@tonic-gate 
1939*7c478bd9Sstevel@tonic-gate 	rsi->hdl = hdl;
1940*7c478bd9Sstevel@tonic-gate 	rsi->cmd = C_UNDOREMOVE;
1941*7c478bd9Sstevel@tonic-gate 	fill_argv(rsi, argv, resource_name);
1942*7c478bd9Sstevel@tonic-gate 
1943*7c478bd9Sstevel@tonic-gate 	status = do_dr(rsi, argv, script_env, info);
1944*7c478bd9Sstevel@tonic-gate 
1945*7c478bd9Sstevel@tonic-gate 	remove_drreq(rsi, resource_name);
1946*7c478bd9Sstevel@tonic-gate 
1947*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&rsi->channel_lock);
1948*7c478bd9Sstevel@tonic-gate 	return (status);
1949*7c478bd9Sstevel@tonic-gate }
1950*7c478bd9Sstevel@tonic-gate 
1951*7c478bd9Sstevel@tonic-gate /*
1952*7c478bd9Sstevel@tonic-gate  * notify_remove entry point
1953*7c478bd9Sstevel@tonic-gate  */
1954*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
1955*7c478bd9Sstevel@tonic-gate static int
script_notify_remove(rcm_handle_t * hdl,char * resource_name,pid_t pid,uint_t flag,char ** info,rcm_info_t ** dependent_info)1956*7c478bd9Sstevel@tonic-gate script_notify_remove(rcm_handle_t *hdl,
1957*7c478bd9Sstevel@tonic-gate 	char *resource_name,
1958*7c478bd9Sstevel@tonic-gate 	pid_t pid,
1959*7c478bd9Sstevel@tonic-gate 	uint_t flag,
1960*7c478bd9Sstevel@tonic-gate 	char **info,
1961*7c478bd9Sstevel@tonic-gate 	rcm_info_t **dependent_info)
1962*7c478bd9Sstevel@tonic-gate {
1963*7c478bd9Sstevel@tonic-gate 	script_info_t *rsi = hdl->module->rsi;
1964*7c478bd9Sstevel@tonic-gate 	char *argv[MAX_ARGS];
1965*7c478bd9Sstevel@tonic-gate 	int status;
1966*7c478bd9Sstevel@tonic-gate 
1967*7c478bd9Sstevel@tonic-gate 	rcm_log_message(RSCR_TRACE, "script_notify_remove: resource = %s\n",
1968*7c478bd9Sstevel@tonic-gate 				resource_name);
1969*7c478bd9Sstevel@tonic-gate 
1970*7c478bd9Sstevel@tonic-gate 	*info = NULL;
1971*7c478bd9Sstevel@tonic-gate 
1972*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&rsi->channel_lock);
1973*7c478bd9Sstevel@tonic-gate 
1974*7c478bd9Sstevel@tonic-gate 	rsi->hdl = hdl;
1975*7c478bd9Sstevel@tonic-gate 	rsi->cmd = C_POSTREMOVE;
1976*7c478bd9Sstevel@tonic-gate 	fill_argv(rsi, argv, resource_name);
1977*7c478bd9Sstevel@tonic-gate 
1978*7c478bd9Sstevel@tonic-gate 	status = do_dr(rsi, argv, script_env, info);
1979*7c478bd9Sstevel@tonic-gate 
1980*7c478bd9Sstevel@tonic-gate 	remove_drreq(rsi, resource_name);
1981*7c478bd9Sstevel@tonic-gate 
1982*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&rsi->channel_lock);
1983*7c478bd9Sstevel@tonic-gate 	return (status);
1984*7c478bd9Sstevel@tonic-gate }
1985*7c478bd9Sstevel@tonic-gate 
1986*7c478bd9Sstevel@tonic-gate /*
1987*7c478bd9Sstevel@tonic-gate  * request_suspend entry point
1988*7c478bd9Sstevel@tonic-gate  */
1989*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
1990*7c478bd9Sstevel@tonic-gate static int
script_request_suspend(rcm_handle_t * hdl,char * resource_name,pid_t pid,timespec_t * interval,uint_t flag,char ** info,rcm_info_t ** dependent_info)1991*7c478bd9Sstevel@tonic-gate script_request_suspend(rcm_handle_t *hdl,
1992*7c478bd9Sstevel@tonic-gate 	char *resource_name,
1993*7c478bd9Sstevel@tonic-gate 	pid_t pid,
1994*7c478bd9Sstevel@tonic-gate 	timespec_t *interval,
1995*7c478bd9Sstevel@tonic-gate 	uint_t flag,
1996*7c478bd9Sstevel@tonic-gate 	char **info,
1997*7c478bd9Sstevel@tonic-gate 	rcm_info_t **dependent_info)
1998*7c478bd9Sstevel@tonic-gate {
1999*7c478bd9Sstevel@tonic-gate 	script_info_t *rsi = hdl->module->rsi;
2000*7c478bd9Sstevel@tonic-gate 	char *buf = NULL;
2001*7c478bd9Sstevel@tonic-gate 	char *curptr = NULL;
2002*7c478bd9Sstevel@tonic-gate 	char *argv[MAX_ARGS];
2003*7c478bd9Sstevel@tonic-gate 	char *envp[MAX_ENV_PARAMS];
2004*7c478bd9Sstevel@tonic-gate 	char flags_name[MAX_FLAGS_NAME_LEN];
2005*7c478bd9Sstevel@tonic-gate 	int buflen = 0;
2006*7c478bd9Sstevel@tonic-gate 	long seconds;
2007*7c478bd9Sstevel@tonic-gate 	int status;
2008*7c478bd9Sstevel@tonic-gate 	int i;
2009*7c478bd9Sstevel@tonic-gate 
2010*7c478bd9Sstevel@tonic-gate 	rcm_log_message(RSCR_TRACE,
2011*7c478bd9Sstevel@tonic-gate 	    "script_request_suspend: resource = %s flags = %s\n", resource_name,
2012*7c478bd9Sstevel@tonic-gate 	    flags_to_name(flag, flags_name, MAX_FLAGS_NAME_LEN));
2013*7c478bd9Sstevel@tonic-gate 
2014*7c478bd9Sstevel@tonic-gate 	*info = NULL;
2015*7c478bd9Sstevel@tonic-gate 
2016*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&rsi->channel_lock);
2017*7c478bd9Sstevel@tonic-gate 
2018*7c478bd9Sstevel@tonic-gate 	rsi->hdl = hdl;
2019*7c478bd9Sstevel@tonic-gate 	rsi->cmd = (flag & RCM_QUERY) ? C_QUERYSUSPEND : C_PRESUSPEND;
2020*7c478bd9Sstevel@tonic-gate 
2021*7c478bd9Sstevel@tonic-gate 	if (rsi->cmd == C_PRESUSPEND)
2022*7c478bd9Sstevel@tonic-gate 		add_drreq(rsi, resource_name);
2023*7c478bd9Sstevel@tonic-gate 
2024*7c478bd9Sstevel@tonic-gate 	fill_argv(rsi, argv, resource_name);
2025*7c478bd9Sstevel@tonic-gate 
2026*7c478bd9Sstevel@tonic-gate 	copy_env(script_env, envp);
2027*7c478bd9Sstevel@tonic-gate 	for (i = 0; envp[i] != NULL; i++);
2028*7c478bd9Sstevel@tonic-gate 
2029*7c478bd9Sstevel@tonic-gate 	envp[i++] = (flag & RCM_FORCE) ? script_env_force : script_env_noforce;
2030*7c478bd9Sstevel@tonic-gate 
2031*7c478bd9Sstevel@tonic-gate 	if (interval) {
2032*7c478bd9Sstevel@tonic-gate 		/*
2033*7c478bd9Sstevel@tonic-gate 		 * Merge the seconds and nanoseconds, rounding up if there
2034*7c478bd9Sstevel@tonic-gate 		 * are any remainder nanoseconds.
2035*7c478bd9Sstevel@tonic-gate 		 */
2036*7c478bd9Sstevel@tonic-gate 		seconds = interval->tv_sec + (interval->tv_nsec / 1000000000L);
2037*7c478bd9Sstevel@tonic-gate 		if (interval->tv_nsec % 1000000000L)
2038*7c478bd9Sstevel@tonic-gate 			seconds += (interval->tv_sec > 0) ? 1L : -1L;
2039*7c478bd9Sstevel@tonic-gate 		rcmscript_snprintf(&buf, &buflen, &curptr, script_env_interval,
2040*7c478bd9Sstevel@tonic-gate 		    seconds);
2041*7c478bd9Sstevel@tonic-gate 		envp[i++] = buf;
2042*7c478bd9Sstevel@tonic-gate 	}
2043*7c478bd9Sstevel@tonic-gate 
2044*7c478bd9Sstevel@tonic-gate 	envp[i] = NULL;
2045*7c478bd9Sstevel@tonic-gate 
2046*7c478bd9Sstevel@tonic-gate 	status = do_dr(rsi, argv, envp, info);
2047*7c478bd9Sstevel@tonic-gate 
2048*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&rsi->channel_lock);
2049*7c478bd9Sstevel@tonic-gate 	if (buf)
2050*7c478bd9Sstevel@tonic-gate 		free(buf);
2051*7c478bd9Sstevel@tonic-gate 	return (status);
2052*7c478bd9Sstevel@tonic-gate }
2053*7c478bd9Sstevel@tonic-gate 
2054*7c478bd9Sstevel@tonic-gate /*
2055*7c478bd9Sstevel@tonic-gate  * notify_resume entry point
2056*7c478bd9Sstevel@tonic-gate  */
2057*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
2058*7c478bd9Sstevel@tonic-gate static int
script_notify_resume(rcm_handle_t * hdl,char * resource_name,pid_t pid,uint_t flag,char ** info,rcm_info_t ** dependent_info)2059*7c478bd9Sstevel@tonic-gate script_notify_resume(rcm_handle_t *hdl,
2060*7c478bd9Sstevel@tonic-gate 	char *resource_name,
2061*7c478bd9Sstevel@tonic-gate 	pid_t pid,
2062*7c478bd9Sstevel@tonic-gate 	uint_t flag,
2063*7c478bd9Sstevel@tonic-gate 	char **info,
2064*7c478bd9Sstevel@tonic-gate 	rcm_info_t **dependent_info)
2065*7c478bd9Sstevel@tonic-gate {
2066*7c478bd9Sstevel@tonic-gate 	script_info_t *rsi = hdl->module->rsi;
2067*7c478bd9Sstevel@tonic-gate 	char *argv[MAX_ARGS];
2068*7c478bd9Sstevel@tonic-gate 	int status;
2069*7c478bd9Sstevel@tonic-gate 
2070*7c478bd9Sstevel@tonic-gate 	rcm_log_message(RSCR_TRACE, "script_notify_resume: resource = %s\n",
2071*7c478bd9Sstevel@tonic-gate 	    resource_name);
2072*7c478bd9Sstevel@tonic-gate 
2073*7c478bd9Sstevel@tonic-gate 	*info = NULL;
2074*7c478bd9Sstevel@tonic-gate 
2075*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&rsi->channel_lock);
2076*7c478bd9Sstevel@tonic-gate 
2077*7c478bd9Sstevel@tonic-gate 	rsi->hdl = hdl;
2078*7c478bd9Sstevel@tonic-gate 	rsi->cmd = (flag & RCM_SUSPENDED) ? C_POSTRESUME : C_CANCELSUSPEND;
2079*7c478bd9Sstevel@tonic-gate 	fill_argv(rsi, argv, resource_name);
2080*7c478bd9Sstevel@tonic-gate 
2081*7c478bd9Sstevel@tonic-gate 	status = do_dr(rsi, argv, script_env, info);
2082*7c478bd9Sstevel@tonic-gate 
2083*7c478bd9Sstevel@tonic-gate 	remove_drreq(rsi, resource_name);
2084*7c478bd9Sstevel@tonic-gate 
2085*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&rsi->channel_lock);
2086*7c478bd9Sstevel@tonic-gate 	return (status);
2087*7c478bd9Sstevel@tonic-gate }
2088*7c478bd9Sstevel@tonic-gate 
2089*7c478bd9Sstevel@tonic-gate static capacity_descr_t capacity_type[] = {
2090*7c478bd9Sstevel@tonic-gate 	{ "SUNW_memory", MATCH_EXACT,
2091*7c478bd9Sstevel@tonic-gate 		"new_pages", "RCM_ENV_CAPACITY",
2092*7c478bd9Sstevel@tonic-gate 		"page_size", "RCM_ENV_UNIT_SIZE",
2093*7c478bd9Sstevel@tonic-gate 		"", ""},
2094*7c478bd9Sstevel@tonic-gate 	{ "SUNW_cpu", MATCH_EXACT,
2095*7c478bd9Sstevel@tonic-gate 		"new_total", "RCM_ENV_CAPACITY",
2096*7c478bd9Sstevel@tonic-gate 		"new_cpu_list", "RCM_ENV_CPU_IDS",
2097*7c478bd9Sstevel@tonic-gate 		"", ""},
2098*7c478bd9Sstevel@tonic-gate 	{ "SUNW_cpu/set", MATCH_PREFIX,
2099*7c478bd9Sstevel@tonic-gate 		"new_total", "RCM_ENV_CAPACITY",
2100*7c478bd9Sstevel@tonic-gate 		"new_cpu_list", "RCM_ENV_CPU_IDS",
2101*7c478bd9Sstevel@tonic-gate 		"", ""},
2102*7c478bd9Sstevel@tonic-gate 	{ "", MATCH_INVALID, "", "" }
2103*7c478bd9Sstevel@tonic-gate };
2104*7c478bd9Sstevel@tonic-gate 
2105*7c478bd9Sstevel@tonic-gate static capacity_descr_t *
get_capacity_descr(char * resource_name)2106*7c478bd9Sstevel@tonic-gate get_capacity_descr(char *resource_name)
2107*7c478bd9Sstevel@tonic-gate {
2108*7c478bd9Sstevel@tonic-gate 	int i;
2109*7c478bd9Sstevel@tonic-gate 
2110*7c478bd9Sstevel@tonic-gate 	for (i = 0; *capacity_type[i].resource_name != '\0'; i++) {
2111*7c478bd9Sstevel@tonic-gate 		if ((capacity_type[i].match_type == MATCH_EXACT &&
2112*7c478bd9Sstevel@tonic-gate 			strcmp(capacity_type[i].resource_name,
2113*7c478bd9Sstevel@tonic-gate 			resource_name) == 0) ||
2114*7c478bd9Sstevel@tonic-gate 			(capacity_type[i].match_type == MATCH_PREFIX &&
2115*7c478bd9Sstevel@tonic-gate 			strncmp(capacity_type[i].resource_name,
2116*7c478bd9Sstevel@tonic-gate 			resource_name,
2117*7c478bd9Sstevel@tonic-gate 			strlen(capacity_type[i].resource_name)) == 0))
2118*7c478bd9Sstevel@tonic-gate 
2119*7c478bd9Sstevel@tonic-gate 			return (&capacity_type[i]);
2120*7c478bd9Sstevel@tonic-gate 	}
2121*7c478bd9Sstevel@tonic-gate 
2122*7c478bd9Sstevel@tonic-gate 	return (NULL);
2123*7c478bd9Sstevel@tonic-gate }
2124*7c478bd9Sstevel@tonic-gate 
2125*7c478bd9Sstevel@tonic-gate static int
build_env_for_capacity(script_info_t * rsi,char * resource_name,uint_t flag,nvlist_t * capacity_info,char * envp[],int * dynamic_env_index,char ** errmsg)2126*7c478bd9Sstevel@tonic-gate build_env_for_capacity(script_info_t *rsi,
2127*7c478bd9Sstevel@tonic-gate 	char *resource_name,
2128*7c478bd9Sstevel@tonic-gate 	uint_t flag,
2129*7c478bd9Sstevel@tonic-gate 	nvlist_t *capacity_info,
2130*7c478bd9Sstevel@tonic-gate 	char *envp[],
2131*7c478bd9Sstevel@tonic-gate 	int *dynamic_env_index,
2132*7c478bd9Sstevel@tonic-gate 	char **errmsg)
2133*7c478bd9Sstevel@tonic-gate {
2134*7c478bd9Sstevel@tonic-gate 	int p, i;
2135*7c478bd9Sstevel@tonic-gate 	capacity_descr_t *capa = NULL;
2136*7c478bd9Sstevel@tonic-gate 	nvpair_t *nvpair;
2137*7c478bd9Sstevel@tonic-gate 	char *buf;
2138*7c478bd9Sstevel@tonic-gate 	char *curptr;
2139*7c478bd9Sstevel@tonic-gate 	int buflen;
2140*7c478bd9Sstevel@tonic-gate 	int error;
2141*7c478bd9Sstevel@tonic-gate 	uint_t n;
2142*7c478bd9Sstevel@tonic-gate 
2143*7c478bd9Sstevel@tonic-gate 	copy_env(script_env, envp);
2144*7c478bd9Sstevel@tonic-gate 	for (p = 0; envp[p] != NULL; p++)
2145*7c478bd9Sstevel@tonic-gate 		;
2146*7c478bd9Sstevel@tonic-gate 
2147*7c478bd9Sstevel@tonic-gate 	if (rsi->cmd == C_QUERYCAPACITY || rsi->cmd == C_PRECAPACITY)
2148*7c478bd9Sstevel@tonic-gate 		envp[p++] = (flag & RCM_FORCE) ? script_env_force :
2149*7c478bd9Sstevel@tonic-gate 						script_env_noforce;
2150*7c478bd9Sstevel@tonic-gate 
2151*7c478bd9Sstevel@tonic-gate 	envp[p] = NULL;
2152*7c478bd9Sstevel@tonic-gate 	*dynamic_env_index = p;
2153*7c478bd9Sstevel@tonic-gate 
2154*7c478bd9Sstevel@tonic-gate 	if ((capa = get_capacity_descr(resource_name)) == NULL) {
2155*7c478bd9Sstevel@tonic-gate 		*errmsg = dup_err(RCM_ERROR, MF_UNKNOWN_RSRC_ERR,
2156*7c478bd9Sstevel@tonic-gate 			resource_name, rsi->script_name);
2157*7c478bd9Sstevel@tonic-gate 		return (-1);
2158*7c478bd9Sstevel@tonic-gate 	}
2159*7c478bd9Sstevel@tonic-gate 
2160*7c478bd9Sstevel@tonic-gate 	for (i = 0; *capa->param[i].nvname != '\0'; i++) {
2161*7c478bd9Sstevel@tonic-gate 		nvpair = NULL;
2162*7c478bd9Sstevel@tonic-gate 		while ((nvpair = nvlist_next_nvpair(capacity_info, nvpair))
2163*7c478bd9Sstevel@tonic-gate 				!= NULL) {
2164*7c478bd9Sstevel@tonic-gate 			if (strcmp(nvpair_name(nvpair),
2165*7c478bd9Sstevel@tonic-gate 					capa->param[i].nvname) == 0)
2166*7c478bd9Sstevel@tonic-gate 				break;
2167*7c478bd9Sstevel@tonic-gate 		}
2168*7c478bd9Sstevel@tonic-gate 
2169*7c478bd9Sstevel@tonic-gate 		if (nvpair == NULL) {
2170*7c478bd9Sstevel@tonic-gate 			*errmsg = dup_err(RCM_ERROR, MF_NV_ERR,
2171*7c478bd9Sstevel@tonic-gate 				rsi->script_name);
2172*7c478bd9Sstevel@tonic-gate 			return (-1);
2173*7c478bd9Sstevel@tonic-gate 		}
2174*7c478bd9Sstevel@tonic-gate 
2175*7c478bd9Sstevel@tonic-gate 		error = 0;
2176*7c478bd9Sstevel@tonic-gate 		buf = NULL;
2177*7c478bd9Sstevel@tonic-gate 
2178*7c478bd9Sstevel@tonic-gate 		rcmscript_snprintf(&buf, &buflen, &curptr, "%s=",
2179*7c478bd9Sstevel@tonic-gate 				capa->param[i].envname);
2180*7c478bd9Sstevel@tonic-gate 
2181*7c478bd9Sstevel@tonic-gate 		switch (nvpair_type(nvpair)) {
2182*7c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT16:
2183*7c478bd9Sstevel@tonic-gate 		{
2184*7c478bd9Sstevel@tonic-gate 			int16_t x;
2185*7c478bd9Sstevel@tonic-gate 
2186*7c478bd9Sstevel@tonic-gate 			if (nvpair_value_int16(nvpair, &x) == 0) {
2187*7c478bd9Sstevel@tonic-gate 				rcmscript_snprintf(&buf, &buflen, &curptr,
2188*7c478bd9Sstevel@tonic-gate 						"%hd", (short)x);
2189*7c478bd9Sstevel@tonic-gate 			} else
2190*7c478bd9Sstevel@tonic-gate 				error = 1;
2191*7c478bd9Sstevel@tonic-gate 			break;
2192*7c478bd9Sstevel@tonic-gate 		}
2193*7c478bd9Sstevel@tonic-gate 
2194*7c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT16:
2195*7c478bd9Sstevel@tonic-gate 		{
2196*7c478bd9Sstevel@tonic-gate 			uint16_t x;
2197*7c478bd9Sstevel@tonic-gate 
2198*7c478bd9Sstevel@tonic-gate 			if (nvpair_value_uint16(nvpair, &x) == 0) {
2199*7c478bd9Sstevel@tonic-gate 				rcmscript_snprintf(&buf, &buflen, &curptr,
2200*7c478bd9Sstevel@tonic-gate 						"%hu", (unsigned short)x);
2201*7c478bd9Sstevel@tonic-gate 			} else
2202*7c478bd9Sstevel@tonic-gate 				error = 1;
2203*7c478bd9Sstevel@tonic-gate 			break;
2204*7c478bd9Sstevel@tonic-gate 		}
2205*7c478bd9Sstevel@tonic-gate 
2206*7c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT32:
2207*7c478bd9Sstevel@tonic-gate 		{
2208*7c478bd9Sstevel@tonic-gate 			int32_t x;
2209*7c478bd9Sstevel@tonic-gate 
2210*7c478bd9Sstevel@tonic-gate 			if (nvpair_value_int32(nvpair, &x) == 0) {
2211*7c478bd9Sstevel@tonic-gate 				rcmscript_snprintf(&buf, &buflen, &curptr,
2212*7c478bd9Sstevel@tonic-gate 						"%d", (int)x);
2213*7c478bd9Sstevel@tonic-gate 			} else
2214*7c478bd9Sstevel@tonic-gate 				error = 1;
2215*7c478bd9Sstevel@tonic-gate 			break;
2216*7c478bd9Sstevel@tonic-gate 		}
2217*7c478bd9Sstevel@tonic-gate 
2218*7c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT32:
2219*7c478bd9Sstevel@tonic-gate 		{
2220*7c478bd9Sstevel@tonic-gate 			uint32_t x;
2221*7c478bd9Sstevel@tonic-gate 
2222*7c478bd9Sstevel@tonic-gate 			if (nvpair_value_uint32(nvpair, &x) == 0) {
2223*7c478bd9Sstevel@tonic-gate 				rcmscript_snprintf(&buf, &buflen, &curptr,
2224*7c478bd9Sstevel@tonic-gate 						"%u", (uint_t)x);
2225*7c478bd9Sstevel@tonic-gate 			} else
2226*7c478bd9Sstevel@tonic-gate 				error = 1;
2227*7c478bd9Sstevel@tonic-gate 			break;
2228*7c478bd9Sstevel@tonic-gate 		}
2229*7c478bd9Sstevel@tonic-gate 
2230*7c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT64:
2231*7c478bd9Sstevel@tonic-gate 		{
2232*7c478bd9Sstevel@tonic-gate 			int64_t x;
2233*7c478bd9Sstevel@tonic-gate 
2234*7c478bd9Sstevel@tonic-gate 			if (nvpair_value_int64(nvpair, &x) == 0) {
2235*7c478bd9Sstevel@tonic-gate 				rcmscript_snprintf(&buf, &buflen, &curptr,
2236*7c478bd9Sstevel@tonic-gate 						"%lld", (long long)x);
2237*7c478bd9Sstevel@tonic-gate 			} else
2238*7c478bd9Sstevel@tonic-gate 				error = 1;
2239*7c478bd9Sstevel@tonic-gate 			break;
2240*7c478bd9Sstevel@tonic-gate 		}
2241*7c478bd9Sstevel@tonic-gate 
2242*7c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT64:
2243*7c478bd9Sstevel@tonic-gate 		{
2244*7c478bd9Sstevel@tonic-gate 			uint64_t x;
2245*7c478bd9Sstevel@tonic-gate 
2246*7c478bd9Sstevel@tonic-gate 			if (nvpair_value_uint64(nvpair, &x) == 0) {
2247*7c478bd9Sstevel@tonic-gate 				rcmscript_snprintf(&buf, &buflen, &curptr,
2248*7c478bd9Sstevel@tonic-gate 					"%llu", (unsigned long long)x);
2249*7c478bd9Sstevel@tonic-gate 			} else
2250*7c478bd9Sstevel@tonic-gate 				error = 1;
2251*7c478bd9Sstevel@tonic-gate 			break;
2252*7c478bd9Sstevel@tonic-gate 		}
2253*7c478bd9Sstevel@tonic-gate 
2254*7c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT16_ARRAY:
2255*7c478bd9Sstevel@tonic-gate 		{
2256*7c478bd9Sstevel@tonic-gate 			int16_t *x;
2257*7c478bd9Sstevel@tonic-gate 
2258*7c478bd9Sstevel@tonic-gate 			if (nvpair_value_int16_array(nvpair, &x, &n) == 0) {
2259*7c478bd9Sstevel@tonic-gate 				while (n--) {
2260*7c478bd9Sstevel@tonic-gate 					rcmscript_snprintf(&buf, &buflen,
2261*7c478bd9Sstevel@tonic-gate 						&curptr, "%hd%s",
2262*7c478bd9Sstevel@tonic-gate 						(short)(*x),
2263*7c478bd9Sstevel@tonic-gate 						(n == 0) ? "" : " ");
2264*7c478bd9Sstevel@tonic-gate 					x++;
2265*7c478bd9Sstevel@tonic-gate 				}
2266*7c478bd9Sstevel@tonic-gate 			} else
2267*7c478bd9Sstevel@tonic-gate 				error = 1;
2268*7c478bd9Sstevel@tonic-gate 			break;
2269*7c478bd9Sstevel@tonic-gate 		}
2270*7c478bd9Sstevel@tonic-gate 
2271*7c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT16_ARRAY:
2272*7c478bd9Sstevel@tonic-gate 		{
2273*7c478bd9Sstevel@tonic-gate 			uint16_t *x;
2274*7c478bd9Sstevel@tonic-gate 
2275*7c478bd9Sstevel@tonic-gate 			if (nvpair_value_uint16_array(nvpair, &x, &n) == 0) {
2276*7c478bd9Sstevel@tonic-gate 				while (n--) {
2277*7c478bd9Sstevel@tonic-gate 					rcmscript_snprintf(&buf, &buflen,
2278*7c478bd9Sstevel@tonic-gate 						&curptr, "%hu%s",
2279*7c478bd9Sstevel@tonic-gate 						(unsigned short)(*x),
2280*7c478bd9Sstevel@tonic-gate 						(n == 0) ? "" : " ");
2281*7c478bd9Sstevel@tonic-gate 					x++;
2282*7c478bd9Sstevel@tonic-gate 				}
2283*7c478bd9Sstevel@tonic-gate 			} else
2284*7c478bd9Sstevel@tonic-gate 				error = 1;
2285*7c478bd9Sstevel@tonic-gate 			break;
2286*7c478bd9Sstevel@tonic-gate 		}
2287*7c478bd9Sstevel@tonic-gate 
2288*7c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT32_ARRAY:
2289*7c478bd9Sstevel@tonic-gate 		{
2290*7c478bd9Sstevel@tonic-gate 			int32_t *x;
2291*7c478bd9Sstevel@tonic-gate 
2292*7c478bd9Sstevel@tonic-gate 			if (nvpair_value_int32_array(nvpair, &x, &n) == 0) {
2293*7c478bd9Sstevel@tonic-gate 				while (n--) {
2294*7c478bd9Sstevel@tonic-gate 					rcmscript_snprintf(&buf, &buflen,
2295*7c478bd9Sstevel@tonic-gate 						&curptr, "%d%s",
2296*7c478bd9Sstevel@tonic-gate 						(int)(*x),
2297*7c478bd9Sstevel@tonic-gate 						(n == 0) ? "" : " ");
2298*7c478bd9Sstevel@tonic-gate 					x++;
2299*7c478bd9Sstevel@tonic-gate 				}
2300*7c478bd9Sstevel@tonic-gate 			} else
2301*7c478bd9Sstevel@tonic-gate 				error = 1;
2302*7c478bd9Sstevel@tonic-gate 			break;
2303*7c478bd9Sstevel@tonic-gate 		}
2304*7c478bd9Sstevel@tonic-gate 
2305*7c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT32_ARRAY:
2306*7c478bd9Sstevel@tonic-gate 		{
2307*7c478bd9Sstevel@tonic-gate 			uint32_t *x;
2308*7c478bd9Sstevel@tonic-gate 
2309*7c478bd9Sstevel@tonic-gate 			if (nvpair_value_uint32_array(nvpair, &x, &n) == 0) {
2310*7c478bd9Sstevel@tonic-gate 				while (n--) {
2311*7c478bd9Sstevel@tonic-gate 					rcmscript_snprintf(&buf, &buflen,
2312*7c478bd9Sstevel@tonic-gate 						&curptr, "%u%s",
2313*7c478bd9Sstevel@tonic-gate 						(uint_t)(*x),
2314*7c478bd9Sstevel@tonic-gate 						(n == 0) ? "" : " ");
2315*7c478bd9Sstevel@tonic-gate 					x++;
2316*7c478bd9Sstevel@tonic-gate 				}
2317*7c478bd9Sstevel@tonic-gate 			} else
2318*7c478bd9Sstevel@tonic-gate 				error = 1;
2319*7c478bd9Sstevel@tonic-gate 			break;
2320*7c478bd9Sstevel@tonic-gate 		}
2321*7c478bd9Sstevel@tonic-gate 
2322*7c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT64_ARRAY:
2323*7c478bd9Sstevel@tonic-gate 		{
2324*7c478bd9Sstevel@tonic-gate 			int64_t *x;
2325*7c478bd9Sstevel@tonic-gate 
2326*7c478bd9Sstevel@tonic-gate 			if (nvpair_value_int64_array(nvpair, &x, &n) == 0) {
2327*7c478bd9Sstevel@tonic-gate 				while (n--) {
2328*7c478bd9Sstevel@tonic-gate 					rcmscript_snprintf(&buf, &buflen,
2329*7c478bd9Sstevel@tonic-gate 						&curptr, "%lld%s",
2330*7c478bd9Sstevel@tonic-gate 						(long long)(*x),
2331*7c478bd9Sstevel@tonic-gate 						(n == 0) ? "" : " ");
2332*7c478bd9Sstevel@tonic-gate 					x++;
2333*7c478bd9Sstevel@tonic-gate 				}
2334*7c478bd9Sstevel@tonic-gate 			} else
2335*7c478bd9Sstevel@tonic-gate 				error = 1;
2336*7c478bd9Sstevel@tonic-gate 			break;
2337*7c478bd9Sstevel@tonic-gate 		}
2338*7c478bd9Sstevel@tonic-gate 
2339*7c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT64_ARRAY:
2340*7c478bd9Sstevel@tonic-gate 		{
2341*7c478bd9Sstevel@tonic-gate 			uint64_t *x;
2342*7c478bd9Sstevel@tonic-gate 
2343*7c478bd9Sstevel@tonic-gate 			if (nvpair_value_uint64_array(nvpair, &x, &n) == 0) {
2344*7c478bd9Sstevel@tonic-gate 				while (n--) {
2345*7c478bd9Sstevel@tonic-gate 					rcmscript_snprintf(&buf, &buflen,
2346*7c478bd9Sstevel@tonic-gate 						&curptr, "%llu%s",
2347*7c478bd9Sstevel@tonic-gate 						(unsigned long long)(*x),
2348*7c478bd9Sstevel@tonic-gate 						(n == 0) ? "" : " ");
2349*7c478bd9Sstevel@tonic-gate 					x++;
2350*7c478bd9Sstevel@tonic-gate 				}
2351*7c478bd9Sstevel@tonic-gate 			} else
2352*7c478bd9Sstevel@tonic-gate 				error = 1;
2353*7c478bd9Sstevel@tonic-gate 			break;
2354*7c478bd9Sstevel@tonic-gate 		}
2355*7c478bd9Sstevel@tonic-gate 
2356*7c478bd9Sstevel@tonic-gate 		case DATA_TYPE_STRING:
2357*7c478bd9Sstevel@tonic-gate 		{
2358*7c478bd9Sstevel@tonic-gate 			char *x;
2359*7c478bd9Sstevel@tonic-gate 
2360*7c478bd9Sstevel@tonic-gate 			if (nvpair_value_string(nvpair, &x) == 0) {
2361*7c478bd9Sstevel@tonic-gate 				rcmscript_snprintf(&buf, &buflen, &curptr,
2362*7c478bd9Sstevel@tonic-gate 						"%s", x);
2363*7c478bd9Sstevel@tonic-gate 			} else
2364*7c478bd9Sstevel@tonic-gate 				error = 1;
2365*7c478bd9Sstevel@tonic-gate 			break;
2366*7c478bd9Sstevel@tonic-gate 		}
2367*7c478bd9Sstevel@tonic-gate 
2368*7c478bd9Sstevel@tonic-gate 
2369*7c478bd9Sstevel@tonic-gate 		default:
2370*7c478bd9Sstevel@tonic-gate 			error = 1;
2371*7c478bd9Sstevel@tonic-gate 			break;
2372*7c478bd9Sstevel@tonic-gate 		}
2373*7c478bd9Sstevel@tonic-gate 
2374*7c478bd9Sstevel@tonic-gate 		envp[p++] = buf;
2375*7c478bd9Sstevel@tonic-gate 
2376*7c478bd9Sstevel@tonic-gate 		if (error) {
2377*7c478bd9Sstevel@tonic-gate 			envp[p] = NULL;
2378*7c478bd9Sstevel@tonic-gate 			for (p = *dynamic_env_index; envp[p] != NULL; p++)
2379*7c478bd9Sstevel@tonic-gate 				free(envp[p]);
2380*7c478bd9Sstevel@tonic-gate 			*errmsg = dup_err(RCM_ERROR, MF_NV_ERR,
2381*7c478bd9Sstevel@tonic-gate 				rsi->script_name);
2382*7c478bd9Sstevel@tonic-gate 			return (-1);
2383*7c478bd9Sstevel@tonic-gate 		}
2384*7c478bd9Sstevel@tonic-gate 	}
2385*7c478bd9Sstevel@tonic-gate 
2386*7c478bd9Sstevel@tonic-gate 	envp[p] = NULL;
2387*7c478bd9Sstevel@tonic-gate 
2388*7c478bd9Sstevel@tonic-gate 	return (0);
2389*7c478bd9Sstevel@tonic-gate }
2390*7c478bd9Sstevel@tonic-gate 
2391*7c478bd9Sstevel@tonic-gate /*
2392*7c478bd9Sstevel@tonic-gate  * request_capacity_change entry point
2393*7c478bd9Sstevel@tonic-gate  */
2394*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
2395*7c478bd9Sstevel@tonic-gate static int
script_request_capacity_change(rcm_handle_t * hdl,char * resource_name,pid_t pid,uint_t flag,nvlist_t * capacity_info,char ** info,rcm_info_t ** dependent_info)2396*7c478bd9Sstevel@tonic-gate script_request_capacity_change(rcm_handle_t *hdl,
2397*7c478bd9Sstevel@tonic-gate 	char *resource_name,
2398*7c478bd9Sstevel@tonic-gate 	pid_t pid,
2399*7c478bd9Sstevel@tonic-gate 	uint_t flag,
2400*7c478bd9Sstevel@tonic-gate 	nvlist_t *capacity_info,
2401*7c478bd9Sstevel@tonic-gate 	char **info,
2402*7c478bd9Sstevel@tonic-gate 	rcm_info_t **dependent_info)
2403*7c478bd9Sstevel@tonic-gate {
2404*7c478bd9Sstevel@tonic-gate 	script_info_t *rsi = hdl->module->rsi;
2405*7c478bd9Sstevel@tonic-gate 	char *argv[MAX_ARGS];
2406*7c478bd9Sstevel@tonic-gate 	char *envp[MAX_ENV_PARAMS];
2407*7c478bd9Sstevel@tonic-gate 	char flags_name[MAX_FLAGS_NAME_LEN];
2408*7c478bd9Sstevel@tonic-gate 	int status;
2409*7c478bd9Sstevel@tonic-gate 	int dynamic_env_index;
2410*7c478bd9Sstevel@tonic-gate 
2411*7c478bd9Sstevel@tonic-gate 	rcm_log_message(RSCR_TRACE,
2412*7c478bd9Sstevel@tonic-gate 		"script_request_capacity_change: resource = %s flags = %s\n",
2413*7c478bd9Sstevel@tonic-gate 			resource_name,
2414*7c478bd9Sstevel@tonic-gate 			flags_to_name(flag, flags_name, MAX_FLAGS_NAME_LEN));
2415*7c478bd9Sstevel@tonic-gate 
2416*7c478bd9Sstevel@tonic-gate 	*info = NULL;
2417*7c478bd9Sstevel@tonic-gate 
2418*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&rsi->channel_lock);
2419*7c478bd9Sstevel@tonic-gate 
2420*7c478bd9Sstevel@tonic-gate 	rsi->hdl = hdl;
2421*7c478bd9Sstevel@tonic-gate 	rsi->cmd = (flag & RCM_QUERY) ? C_QUERYCAPACITY : C_PRECAPACITY;
2422*7c478bd9Sstevel@tonic-gate 	fill_argv(rsi, argv, resource_name);
2423*7c478bd9Sstevel@tonic-gate 
2424*7c478bd9Sstevel@tonic-gate 	if (build_env_for_capacity(rsi, resource_name, flag,
2425*7c478bd9Sstevel@tonic-gate 			capacity_info, envp, &dynamic_env_index, info) == 0) {
2426*7c478bd9Sstevel@tonic-gate 
2427*7c478bd9Sstevel@tonic-gate 		status = do_dr(rsi, argv, envp, info);
2428*7c478bd9Sstevel@tonic-gate 
2429*7c478bd9Sstevel@tonic-gate 		while (envp[dynamic_env_index] != NULL) {
2430*7c478bd9Sstevel@tonic-gate 			free(envp[dynamic_env_index]);
2431*7c478bd9Sstevel@tonic-gate 			dynamic_env_index++;
2432*7c478bd9Sstevel@tonic-gate 		}
2433*7c478bd9Sstevel@tonic-gate 	} else
2434*7c478bd9Sstevel@tonic-gate 		status = RCM_FAILURE;
2435*7c478bd9Sstevel@tonic-gate 
2436*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&rsi->channel_lock);
2437*7c478bd9Sstevel@tonic-gate 	return (status);
2438*7c478bd9Sstevel@tonic-gate }
2439*7c478bd9Sstevel@tonic-gate 
2440*7c478bd9Sstevel@tonic-gate /*
2441*7c478bd9Sstevel@tonic-gate  * notify_capacity_change entry point
2442*7c478bd9Sstevel@tonic-gate  */
2443*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
2444*7c478bd9Sstevel@tonic-gate static int
script_notify_capacity_change(rcm_handle_t * hdl,char * resource_name,pid_t pid,uint_t flag,nvlist_t * capacity_info,char ** info,rcm_info_t ** dependent_info)2445*7c478bd9Sstevel@tonic-gate script_notify_capacity_change(rcm_handle_t *hdl,
2446*7c478bd9Sstevel@tonic-gate 	char *resource_name,
2447*7c478bd9Sstevel@tonic-gate 	pid_t pid,
2448*7c478bd9Sstevel@tonic-gate 	uint_t flag,
2449*7c478bd9Sstevel@tonic-gate 	nvlist_t *capacity_info,
2450*7c478bd9Sstevel@tonic-gate 	char **info,
2451*7c478bd9Sstevel@tonic-gate 	rcm_info_t **dependent_info)
2452*7c478bd9Sstevel@tonic-gate {
2453*7c478bd9Sstevel@tonic-gate 	script_info_t *rsi = hdl->module->rsi;
2454*7c478bd9Sstevel@tonic-gate 	char *argv[MAX_ARGS];
2455*7c478bd9Sstevel@tonic-gate 	char *envp[MAX_ENV_PARAMS];
2456*7c478bd9Sstevel@tonic-gate 	int status;
2457*7c478bd9Sstevel@tonic-gate 	int dynamic_env_index;
2458*7c478bd9Sstevel@tonic-gate 
2459*7c478bd9Sstevel@tonic-gate 	rcm_log_message(RSCR_TRACE,
2460*7c478bd9Sstevel@tonic-gate 	"script_notify_capacity_change: resource = %s\n", resource_name);
2461*7c478bd9Sstevel@tonic-gate 
2462*7c478bd9Sstevel@tonic-gate 	*info = NULL;
2463*7c478bd9Sstevel@tonic-gate 
2464*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&rsi->channel_lock);
2465*7c478bd9Sstevel@tonic-gate 
2466*7c478bd9Sstevel@tonic-gate 	rsi->hdl = hdl;
2467*7c478bd9Sstevel@tonic-gate 	rsi->cmd = C_POSTCAPACITY;
2468*7c478bd9Sstevel@tonic-gate 	fill_argv(rsi, argv, resource_name);
2469*7c478bd9Sstevel@tonic-gate 
2470*7c478bd9Sstevel@tonic-gate 	if (build_env_for_capacity(rsi, resource_name, flag,
2471*7c478bd9Sstevel@tonic-gate 			capacity_info, envp, &dynamic_env_index, info) == 0) {
2472*7c478bd9Sstevel@tonic-gate 
2473*7c478bd9Sstevel@tonic-gate 		status = do_dr(rsi, argv, envp, info);
2474*7c478bd9Sstevel@tonic-gate 
2475*7c478bd9Sstevel@tonic-gate 		while (envp[dynamic_env_index] != NULL) {
2476*7c478bd9Sstevel@tonic-gate 			free(envp[dynamic_env_index]);
2477*7c478bd9Sstevel@tonic-gate 			dynamic_env_index++;
2478*7c478bd9Sstevel@tonic-gate 		}
2479*7c478bd9Sstevel@tonic-gate 	} else
2480*7c478bd9Sstevel@tonic-gate 		status = RCM_FAILURE;
2481*7c478bd9Sstevel@tonic-gate 
2482*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&rsi->channel_lock);
2483*7c478bd9Sstevel@tonic-gate 	return (status);
2484*7c478bd9Sstevel@tonic-gate }
2485*7c478bd9Sstevel@tonic-gate 
2486*7c478bd9Sstevel@tonic-gate /* Log the message to syslog */
2487*7c478bd9Sstevel@tonic-gate static void
log_msg(script_info_t * rsi,int level,char * msg)2488*7c478bd9Sstevel@tonic-gate log_msg(script_info_t *rsi, int level, char *msg)
2489*7c478bd9Sstevel@tonic-gate {
2490*7c478bd9Sstevel@tonic-gate 	rcm_log_msg(level, MS_LOG_MSG, rsi->script_name, msg);
2491*7c478bd9Sstevel@tonic-gate }
2492*7c478bd9Sstevel@tonic-gate 
2493*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
2494*7c478bd9Sstevel@tonic-gate static char *
dup_err(int level,char * format,...)2495*7c478bd9Sstevel@tonic-gate dup_err(int level, char *format, ...)
2496*7c478bd9Sstevel@tonic-gate {
2497*7c478bd9Sstevel@tonic-gate 	va_list ap;
2498*7c478bd9Sstevel@tonic-gate 	char buf1[1];
2499*7c478bd9Sstevel@tonic-gate 	char *buf2;
2500*7c478bd9Sstevel@tonic-gate 	int n;
2501*7c478bd9Sstevel@tonic-gate 
2502*7c478bd9Sstevel@tonic-gate 	va_start(ap, format);
2503*7c478bd9Sstevel@tonic-gate 	n = vsnprintf(buf1, 1, format, ap);
2504*7c478bd9Sstevel@tonic-gate 	va_end(ap);
2505*7c478bd9Sstevel@tonic-gate 
2506*7c478bd9Sstevel@tonic-gate 	if (n > 0) {
2507*7c478bd9Sstevel@tonic-gate 		n++;
2508*7c478bd9Sstevel@tonic-gate 		if (buf2 = (char *)malloc(n)) {
2509*7c478bd9Sstevel@tonic-gate 			va_start(ap, format);
2510*7c478bd9Sstevel@tonic-gate 			n = vsnprintf(buf2, n, format, ap);
2511*7c478bd9Sstevel@tonic-gate 			va_end(ap);
2512*7c478bd9Sstevel@tonic-gate 			if (n > 0) {
2513*7c478bd9Sstevel@tonic-gate 				if (level != -1)
2514*7c478bd9Sstevel@tonic-gate 					rcm_log_message(level, buf2);
2515*7c478bd9Sstevel@tonic-gate 				return (buf2);
2516*7c478bd9Sstevel@tonic-gate 			}
2517*7c478bd9Sstevel@tonic-gate 			free(buf2);
2518*7c478bd9Sstevel@tonic-gate 		}
2519*7c478bd9Sstevel@tonic-gate 	}
2520*7c478bd9Sstevel@tonic-gate 
2521*7c478bd9Sstevel@tonic-gate 	return (NULL);
2522*7c478bd9Sstevel@tonic-gate }
2523*7c478bd9Sstevel@tonic-gate 
2524*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE4*/
2525*7c478bd9Sstevel@tonic-gate static void
rcmscript_snprintf(char ** buf,int * buflen,char ** curptr,char * format,...)2526*7c478bd9Sstevel@tonic-gate rcmscript_snprintf(char **buf, int *buflen, char **curptr, char *format, ...)
2527*7c478bd9Sstevel@tonic-gate {
2528*7c478bd9Sstevel@tonic-gate /* must be power of 2 otherwise RSCR_ROUNDUP would break */
2529*7c478bd9Sstevel@tonic-gate #define	SPRINTF_CHUNK_LEN	512
2530*7c478bd9Sstevel@tonic-gate #define	SPRINTF_MIN_CHUNK_LEN	64
2531*7c478bd9Sstevel@tonic-gate 
2532*7c478bd9Sstevel@tonic-gate 	va_list ap;
2533*7c478bd9Sstevel@tonic-gate 	int offset, bytesneeded, bytesleft, error_num;
2534*7c478bd9Sstevel@tonic-gate 
2535*7c478bd9Sstevel@tonic-gate 	if (*buf == NULL) {
2536*7c478bd9Sstevel@tonic-gate 		*buflen = 0;
2537*7c478bd9Sstevel@tonic-gate 		*curptr = NULL;
2538*7c478bd9Sstevel@tonic-gate 	}
2539*7c478bd9Sstevel@tonic-gate 
2540*7c478bd9Sstevel@tonic-gate 	offset = *curptr - *buf;
2541*7c478bd9Sstevel@tonic-gate 	bytesneeded = SPRINTF_MIN_CHUNK_LEN;
2542*7c478bd9Sstevel@tonic-gate 	bytesleft = *buflen - offset;
2543*7c478bd9Sstevel@tonic-gate 
2544*7c478bd9Sstevel@tonic-gate 	/* LINTED */
2545*7c478bd9Sstevel@tonic-gate 	while (1) {
2546*7c478bd9Sstevel@tonic-gate 		if (bytesneeded > bytesleft) {
2547*7c478bd9Sstevel@tonic-gate 			*buflen += RSCR_ROUNDUP(bytesneeded - bytesleft,
2548*7c478bd9Sstevel@tonic-gate 					SPRINTF_CHUNK_LEN);
2549*7c478bd9Sstevel@tonic-gate 			if ((*buf = (char *)realloc(*buf, *buflen)) == NULL) {
2550*7c478bd9Sstevel@tonic-gate 				error_num = errno;
2551*7c478bd9Sstevel@tonic-gate 				rcm_log_message(RCM_ERROR,
2552*7c478bd9Sstevel@tonic-gate 					MF_MEMORY_ALLOCATION_ERR,
2553*7c478bd9Sstevel@tonic-gate 					strerror(error_num));
2554*7c478bd9Sstevel@tonic-gate 				rcmd_exit(error_num);
2555*7c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
2556*7c478bd9Sstevel@tonic-gate 			}
2557*7c478bd9Sstevel@tonic-gate 			*curptr = *buf + offset;
2558*7c478bd9Sstevel@tonic-gate 			bytesleft = *buflen - offset;
2559*7c478bd9Sstevel@tonic-gate 		}
2560*7c478bd9Sstevel@tonic-gate 
2561*7c478bd9Sstevel@tonic-gate 		va_start(ap, format);
2562*7c478bd9Sstevel@tonic-gate 		bytesneeded = vsnprintf(*curptr, bytesleft, format, ap);
2563*7c478bd9Sstevel@tonic-gate 		va_end(ap);
2564*7c478bd9Sstevel@tonic-gate 
2565*7c478bd9Sstevel@tonic-gate 		if (bytesneeded < 0)  {
2566*7c478bd9Sstevel@tonic-gate 			/* vsnprintf encountered an error */
2567*7c478bd9Sstevel@tonic-gate 			error_num = errno;
2568*7c478bd9Sstevel@tonic-gate 			rcm_log_message(RCM_ERROR, MF_FUNC_CALL_ERR,
2569*7c478bd9Sstevel@tonic-gate 				"vsnprintf", strerror(error_num));
2570*7c478bd9Sstevel@tonic-gate 			rcmd_exit(error_num);
2571*7c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
2572*7c478bd9Sstevel@tonic-gate 
2573*7c478bd9Sstevel@tonic-gate 		} else if (bytesneeded < bytesleft) {
2574*7c478bd9Sstevel@tonic-gate 			/* vsnprintf succeeded */
2575*7c478bd9Sstevel@tonic-gate 			*curptr += bytesneeded;
2576*7c478bd9Sstevel@tonic-gate 			return;
2577*7c478bd9Sstevel@tonic-gate 
2578*7c478bd9Sstevel@tonic-gate 		} else {
2579*7c478bd9Sstevel@tonic-gate 			bytesneeded++; /* to account for storage for '\0' */
2580*7c478bd9Sstevel@tonic-gate 		}
2581*7c478bd9Sstevel@tonic-gate 	}
2582*7c478bd9Sstevel@tonic-gate }
2583*7c478bd9Sstevel@tonic-gate 
2584*7c478bd9Sstevel@tonic-gate static char *
rcmscript_strdup(char * str)2585*7c478bd9Sstevel@tonic-gate rcmscript_strdup(char *str)
2586*7c478bd9Sstevel@tonic-gate {
2587*7c478bd9Sstevel@tonic-gate 	char *dupstr;
2588*7c478bd9Sstevel@tonic-gate 
2589*7c478bd9Sstevel@tonic-gate 	if ((dupstr = strdup(str)) == NULL) {
2590*7c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, MF_MEMORY_ALLOCATION_ERR,
2591*7c478bd9Sstevel@tonic-gate 			strerror(errno));
2592*7c478bd9Sstevel@tonic-gate 		rcmd_exit(errno);
2593*7c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
2594*7c478bd9Sstevel@tonic-gate 	}
2595*7c478bd9Sstevel@tonic-gate 
2596*7c478bd9Sstevel@tonic-gate 	return (dupstr);
2597*7c478bd9Sstevel@tonic-gate }
2598*7c478bd9Sstevel@tonic-gate 
2599*7c478bd9Sstevel@tonic-gate static void *
rcmscript_malloc(size_t len)2600*7c478bd9Sstevel@tonic-gate rcmscript_malloc(size_t len)
2601*7c478bd9Sstevel@tonic-gate {
2602*7c478bd9Sstevel@tonic-gate 	void *ptr;
2603*7c478bd9Sstevel@tonic-gate 
2604*7c478bd9Sstevel@tonic-gate 	if ((ptr = malloc(len)) == NULL) {
2605*7c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, MF_MEMORY_ALLOCATION_ERR,
2606*7c478bd9Sstevel@tonic-gate 			strerror(errno));
2607*7c478bd9Sstevel@tonic-gate 		rcmd_exit(errno);
2608*7c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
2609*7c478bd9Sstevel@tonic-gate 	}
2610*7c478bd9Sstevel@tonic-gate 
2611*7c478bd9Sstevel@tonic-gate 	return (ptr);
2612*7c478bd9Sstevel@tonic-gate }
2613*7c478bd9Sstevel@tonic-gate 
2614*7c478bd9Sstevel@tonic-gate static void *
rcmscript_calloc(size_t nelem,size_t elsize)2615*7c478bd9Sstevel@tonic-gate rcmscript_calloc(size_t nelem, size_t elsize)
2616*7c478bd9Sstevel@tonic-gate {
2617*7c478bd9Sstevel@tonic-gate 	void *ptr;
2618*7c478bd9Sstevel@tonic-gate 
2619*7c478bd9Sstevel@tonic-gate 	if ((ptr = calloc(nelem, elsize)) == NULL) {
2620*7c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, MF_MEMORY_ALLOCATION_ERR,
2621*7c478bd9Sstevel@tonic-gate 			strerror(errno));
2622*7c478bd9Sstevel@tonic-gate 		rcmd_exit(errno);
2623*7c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
2624*7c478bd9Sstevel@tonic-gate 	}
2625*7c478bd9Sstevel@tonic-gate 
2626*7c478bd9Sstevel@tonic-gate 	return (ptr);
2627*7c478bd9Sstevel@tonic-gate }
2628