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  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
23*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24*7c478bd9Sstevel@tonic-gate  */
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate #include "rcm_impl.h"
27*7c478bd9Sstevel@tonic-gate #include "rcm_module.h"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * Global locks
31*7c478bd9Sstevel@tonic-gate  */
32*7c478bd9Sstevel@tonic-gate mutex_t rcm_req_lock;	/* protects global dr & info request list */
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate /*
35*7c478bd9Sstevel@tonic-gate  * Daemon state file
36*7c478bd9Sstevel@tonic-gate  */
37*7c478bd9Sstevel@tonic-gate static int state_fd;
38*7c478bd9Sstevel@tonic-gate #define	RCM_STATE_FILE	"/var/run/rcm_daemon_state"
39*7c478bd9Sstevel@tonic-gate #define	N_REQ_CHUNK	10	/* grow 10 entries at a time */
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate /*
42*7c478bd9Sstevel@tonic-gate  * Daemon timeout value
43*7c478bd9Sstevel@tonic-gate  */
44*7c478bd9Sstevel@tonic-gate #define	RCM_DAEMON_TIMEOUT	300	/* 5 minutes idle time */
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate /*
47*7c478bd9Sstevel@tonic-gate  * Struct for a list of outstanding rcm requests
48*7c478bd9Sstevel@tonic-gate  */
49*7c478bd9Sstevel@tonic-gate typedef struct {
50*7c478bd9Sstevel@tonic-gate 	int	seq_num;		/* sequence number of request */
51*7c478bd9Sstevel@tonic-gate 	int	state;			/* current state */
52*7c478bd9Sstevel@tonic-gate 	pid_t	pid;			/* pid of initiator */
53*7c478bd9Sstevel@tonic-gate 	uint_t	flag;			/* request flags */
54*7c478bd9Sstevel@tonic-gate 	int	type;			/* resource(device) type */
55*7c478bd9Sstevel@tonic-gate 	timespec_t interval;		/* suspend interval */
56*7c478bd9Sstevel@tonic-gate 	char	device[MAXPATHLEN];	/* name of device or resource */
57*7c478bd9Sstevel@tonic-gate } req_t;
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate typedef struct {
60*7c478bd9Sstevel@tonic-gate 	int	n_req;
61*7c478bd9Sstevel@tonic-gate 	int	n_req_max;	/* number of req_t's to follow */
62*7c478bd9Sstevel@tonic-gate 	int	n_seq_max;	/* last sequence number */
63*7c478bd9Sstevel@tonic-gate 	int	idle_timeout;	/* persist idle timeout value */
64*7c478bd9Sstevel@tonic-gate 	req_t	req[1];
65*7c478bd9Sstevel@tonic-gate 	/* more req_t follows */
66*7c478bd9Sstevel@tonic-gate } req_list_t;
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate static req_list_t *dr_req_list;
69*7c478bd9Sstevel@tonic-gate static req_list_t *info_req_list;
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate static const char *locked_info = "DR operation in progress";
72*7c478bd9Sstevel@tonic-gate static const char *locked_err = "Resource is busy";
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate static int rcmd_get_state();
75*7c478bd9Sstevel@tonic-gate static void add_to_polling_list(pid_t);
76*7c478bd9Sstevel@tonic-gate static void remove_from_polling_list(pid_t);
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate void start_polling_thread();
79*7c478bd9Sstevel@tonic-gate static void stop_polling_thread();
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate /*
82*7c478bd9Sstevel@tonic-gate  * Initialize request lists required for locking
83*7c478bd9Sstevel@tonic-gate  */
84*7c478bd9Sstevel@tonic-gate void
rcmd_lock_init(void)85*7c478bd9Sstevel@tonic-gate rcmd_lock_init(void)
86*7c478bd9Sstevel@tonic-gate {
87*7c478bd9Sstevel@tonic-gate 	int size;
88*7c478bd9Sstevel@tonic-gate 	struct stat fbuf;
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 	/*
91*7c478bd9Sstevel@tonic-gate 	 * Start info list with one slot, then grow on demand.
92*7c478bd9Sstevel@tonic-gate 	 */
93*7c478bd9Sstevel@tonic-gate 	info_req_list = s_calloc(1, sizeof (req_list_t));
94*7c478bd9Sstevel@tonic-gate 	info_req_list->n_req_max = 1;
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate 	/*
97*7c478bd9Sstevel@tonic-gate 	 * Open daemon state file and map in contents
98*7c478bd9Sstevel@tonic-gate 	 */
99*7c478bd9Sstevel@tonic-gate 	state_fd = open(RCM_STATE_FILE, O_CREAT|O_RDWR, 0600);
100*7c478bd9Sstevel@tonic-gate 	if (state_fd == -1) {
101*7c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, gettext("cannot open %s: %s\n"),
102*7c478bd9Sstevel@tonic-gate 		    RCM_STATE_FILE, strerror(errno));
103*7c478bd9Sstevel@tonic-gate 		rcmd_exit(errno);
104*7c478bd9Sstevel@tonic-gate 	}
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 	if (fstat(state_fd, &fbuf) != 0) {
107*7c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, gettext("cannot stat %s: %s\n"),
108*7c478bd9Sstevel@tonic-gate 		    RCM_STATE_FILE, strerror(errno));
109*7c478bd9Sstevel@tonic-gate 		rcmd_exit(errno);
110*7c478bd9Sstevel@tonic-gate 	}
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 	size = fbuf.st_size;
113*7c478bd9Sstevel@tonic-gate 	if (size == 0) {
114*7c478bd9Sstevel@tonic-gate 		size = sizeof (req_list_t);
115*7c478bd9Sstevel@tonic-gate 		if (ftruncate(state_fd, size) != 0) {
116*7c478bd9Sstevel@tonic-gate 			rcm_log_message(RCM_ERROR,
117*7c478bd9Sstevel@tonic-gate 			    gettext("cannot truncate %s: %s\n"),
118*7c478bd9Sstevel@tonic-gate 			    RCM_STATE_FILE, strerror(errno));
119*7c478bd9Sstevel@tonic-gate 			rcmd_exit(errno);
120*7c478bd9Sstevel@tonic-gate 		}
121*7c478bd9Sstevel@tonic-gate 	}
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 	/*LINTED*/
124*7c478bd9Sstevel@tonic-gate 	dr_req_list = (req_list_t *)mmap(NULL, size, PROT_READ|PROT_WRITE,
125*7c478bd9Sstevel@tonic-gate 	    MAP_SHARED, state_fd, 0);
126*7c478bd9Sstevel@tonic-gate 	if (dr_req_list == MAP_FAILED) {
127*7c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, gettext("cannot mmap %s: %s\n"),
128*7c478bd9Sstevel@tonic-gate 		    RCM_STATE_FILE, strerror(errno));
129*7c478bd9Sstevel@tonic-gate 		rcmd_exit(errno);
130*7c478bd9Sstevel@tonic-gate 	}
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 	/*
133*7c478bd9Sstevel@tonic-gate 	 * Initial size is one entry
134*7c478bd9Sstevel@tonic-gate 	 */
135*7c478bd9Sstevel@tonic-gate 	if (dr_req_list->n_req_max == 0) {
136*7c478bd9Sstevel@tonic-gate 		dr_req_list->n_req_max = 1;
137*7c478bd9Sstevel@tonic-gate 		(void) fsync(state_fd);
138*7c478bd9Sstevel@tonic-gate 		return;
139*7c478bd9Sstevel@tonic-gate 	}
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	rcm_log_message(RCM_DEBUG, "n_req = %d, n_req_max = %d\n",
142*7c478bd9Sstevel@tonic-gate 	    dr_req_list->n_req, dr_req_list->n_req_max);
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 	/*
145*7c478bd9Sstevel@tonic-gate 	 * Recover the daemon state
146*7c478bd9Sstevel@tonic-gate 	 */
147*7c478bd9Sstevel@tonic-gate 	clean_dr_list();
148*7c478bd9Sstevel@tonic-gate }
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate /*
151*7c478bd9Sstevel@tonic-gate  * Get a unique sequence number--to be called with rcm_req_lock held.
152*7c478bd9Sstevel@tonic-gate  */
153*7c478bd9Sstevel@tonic-gate static int
get_seq_number()154*7c478bd9Sstevel@tonic-gate get_seq_number()
155*7c478bd9Sstevel@tonic-gate {
156*7c478bd9Sstevel@tonic-gate 	int number;
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 	if (dr_req_list == NULL)
159*7c478bd9Sstevel@tonic-gate 		return (0);
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	dr_req_list->n_seq_max++;
162*7c478bd9Sstevel@tonic-gate 	number  = (dr_req_list->n_seq_max << SEQ_NUM_SHIFT);
163*7c478bd9Sstevel@tonic-gate 	(void) fsync(state_fd);
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 	return (number);
166*7c478bd9Sstevel@tonic-gate }
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate /*
169*7c478bd9Sstevel@tonic-gate  * Find entry in list with the same resource name and sequence number.
170*7c478bd9Sstevel@tonic-gate  * If seq_num == -1, no seq_num matching is required.
171*7c478bd9Sstevel@tonic-gate  */
172*7c478bd9Sstevel@tonic-gate static req_t *
find_req_entry(char * device,uint_t flag,int seq_num,req_list_t * list)173*7c478bd9Sstevel@tonic-gate find_req_entry(char *device, uint_t flag, int seq_num, req_list_t *list)
174*7c478bd9Sstevel@tonic-gate {
175*7c478bd9Sstevel@tonic-gate 	int i;
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate 	/*
178*7c478bd9Sstevel@tonic-gate 	 * Look for entry with the same resource and seq_num.
179*7c478bd9Sstevel@tonic-gate 	 * Also match RCM_FILESYS field in flag.
180*7c478bd9Sstevel@tonic-gate 	 */
181*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < list->n_req_max; i++) {
182*7c478bd9Sstevel@tonic-gate 		if (list->req[i].state == RCM_STATE_REMOVE)
183*7c478bd9Sstevel@tonic-gate 			/* stale entry */
184*7c478bd9Sstevel@tonic-gate 			continue;
185*7c478bd9Sstevel@tonic-gate 		/*
186*7c478bd9Sstevel@tonic-gate 		 * We need to distiguish a file system root from the directory
187*7c478bd9Sstevel@tonic-gate 		 * it is mounted on.
188*7c478bd9Sstevel@tonic-gate 		 *
189*7c478bd9Sstevel@tonic-gate 		 * Applications are not aware of any difference between the
190*7c478bd9Sstevel@tonic-gate 		 * two, but the system keeps track of it internally by
191*7c478bd9Sstevel@tonic-gate 		 * checking for mount points while traversing file path.
192*7c478bd9Sstevel@tonic-gate 		 * In a similar spirit, RCM is keeping this difference as
193*7c478bd9Sstevel@tonic-gate 		 * an implementation detail.
194*7c478bd9Sstevel@tonic-gate 		 */
195*7c478bd9Sstevel@tonic-gate 		if ((strcmp(device, list->req[i].device) != 0) ||
196*7c478bd9Sstevel@tonic-gate 		    (list->req[i].flag & RCM_FILESYS) != (flag & RCM_FILESYS))
197*7c478bd9Sstevel@tonic-gate 			/* different resource */
198*7c478bd9Sstevel@tonic-gate 			continue;
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate 		if ((seq_num != -1) && ((seq_num >> SEQ_NUM_SHIFT) !=
201*7c478bd9Sstevel@tonic-gate 		    (list->req[i].seq_num >> SEQ_NUM_SHIFT)))
202*7c478bd9Sstevel@tonic-gate 			/* different base seqnum */
203*7c478bd9Sstevel@tonic-gate 			continue;
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 		return (&list->req[i]);
206*7c478bd9Sstevel@tonic-gate 	}
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 	return (NULL);
209*7c478bd9Sstevel@tonic-gate }
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate /*
212*7c478bd9Sstevel@tonic-gate  * Get the next empty req_t entry. If no entry exists, grow the list.
213*7c478bd9Sstevel@tonic-gate  */
214*7c478bd9Sstevel@tonic-gate static req_t *
get_req_entry(req_list_t ** listp)215*7c478bd9Sstevel@tonic-gate get_req_entry(req_list_t **listp)
216*7c478bd9Sstevel@tonic-gate {
217*7c478bd9Sstevel@tonic-gate 	int i;
218*7c478bd9Sstevel@tonic-gate 	int n_req = (*listp)->n_req;
219*7c478bd9Sstevel@tonic-gate 	int n_req_max = (*listp)->n_req_max;
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate 	/*
222*7c478bd9Sstevel@tonic-gate 	 * If the list is full, grow the list and return the first
223*7c478bd9Sstevel@tonic-gate 	 * entry in the new portion.
224*7c478bd9Sstevel@tonic-gate 	 */
225*7c478bd9Sstevel@tonic-gate 	if (n_req == n_req_max) {
226*7c478bd9Sstevel@tonic-gate 		int newsize;
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate 		n_req_max += N_REQ_CHUNK;
229*7c478bd9Sstevel@tonic-gate 		newsize = sizeof (req_list_t) + (n_req_max - 1) *
230*7c478bd9Sstevel@tonic-gate 		    sizeof (req_t);
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate 		if (listp == &info_req_list) {
233*7c478bd9Sstevel@tonic-gate 			*listp = s_realloc(*listp, newsize);
234*7c478bd9Sstevel@tonic-gate 		} else if (ftruncate(state_fd, newsize) != 0) {
235*7c478bd9Sstevel@tonic-gate 			rcm_log_message(RCM_ERROR,
236*7c478bd9Sstevel@tonic-gate 			    gettext("cannot truncate %s: %s\n"),
237*7c478bd9Sstevel@tonic-gate 			    RCM_STATE_FILE, strerror(errno));
238*7c478bd9Sstevel@tonic-gate 			rcmd_exit(errno);
239*7c478bd9Sstevel@tonic-gate 		/*LINTED*/
240*7c478bd9Sstevel@tonic-gate 		} else if ((*listp = (req_list_t *)mmap(NULL, newsize,
241*7c478bd9Sstevel@tonic-gate 		    PROT_READ|PROT_WRITE, MAP_SHARED, state_fd, 0)) ==
242*7c478bd9Sstevel@tonic-gate 		    MAP_FAILED) {
243*7c478bd9Sstevel@tonic-gate 			rcm_log_message(RCM_ERROR,
244*7c478bd9Sstevel@tonic-gate 			    gettext("cannot mmap %s: %s\n"),
245*7c478bd9Sstevel@tonic-gate 			    RCM_STATE_FILE, strerror(errno));
246*7c478bd9Sstevel@tonic-gate 			rcmd_exit(errno);
247*7c478bd9Sstevel@tonic-gate 		}
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate 		/* Initialize the new entries */
250*7c478bd9Sstevel@tonic-gate 		for (i = (*listp)->n_req_max; i < n_req_max; i++) {
251*7c478bd9Sstevel@tonic-gate 			(*listp)->req[i].state = RCM_STATE_REMOVE;
252*7c478bd9Sstevel@tonic-gate 			(void) strcpy((*listp)->req[i].device, "");
253*7c478bd9Sstevel@tonic-gate 		}
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 		(*listp)->n_req_max = n_req_max;
256*7c478bd9Sstevel@tonic-gate 		(*listp)->n_req++;
257*7c478bd9Sstevel@tonic-gate 		return (&(*listp)->req[n_req]);
258*7c478bd9Sstevel@tonic-gate 	}
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate 	/*
261*7c478bd9Sstevel@tonic-gate 	 * List contains empty slots, find it.
262*7c478bd9Sstevel@tonic-gate 	 */
263*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < n_req_max; i++) {
264*7c478bd9Sstevel@tonic-gate 		if (((*listp)->req[i].device[0] == '\0') ||
265*7c478bd9Sstevel@tonic-gate 		    ((*listp)->req[i].state == RCM_STATE_REMOVE)) {
266*7c478bd9Sstevel@tonic-gate 			break;
267*7c478bd9Sstevel@tonic-gate 		}
268*7c478bd9Sstevel@tonic-gate 	}
269*7c478bd9Sstevel@tonic-gate 
270*7c478bd9Sstevel@tonic-gate 	assert(i < n_req_max);	/* empty slot must exist */
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate 	(*listp)->n_req++;
273*7c478bd9Sstevel@tonic-gate 	return (&(*listp)->req[i]);
274*7c478bd9Sstevel@tonic-gate }
275*7c478bd9Sstevel@tonic-gate 
276*7c478bd9Sstevel@tonic-gate /*
277*7c478bd9Sstevel@tonic-gate  * When one resource depends on multiple resources, it's possible that
278*7c478bd9Sstevel@tonic-gate  * rcm_get_info can be called multiple times on the resource, resulting
279*7c478bd9Sstevel@tonic-gate  * in duplicate information. By assigning a unique sequence number to
280*7c478bd9Sstevel@tonic-gate  * each rcm_get_info operation, this duplication can be eliminated.
281*7c478bd9Sstevel@tonic-gate  *
282*7c478bd9Sstevel@tonic-gate  * Insert a dr entry in info_req_list
283*7c478bd9Sstevel@tonic-gate  */
284*7c478bd9Sstevel@tonic-gate int
info_req_add(char * rsrcname,uint_t flag,int seq_num)285*7c478bd9Sstevel@tonic-gate info_req_add(char *rsrcname, uint_t flag, int seq_num)
286*7c478bd9Sstevel@tonic-gate {
287*7c478bd9Sstevel@tonic-gate 	int error = 0;
288*7c478bd9Sstevel@tonic-gate 	char *device;
289*7c478bd9Sstevel@tonic-gate 	req_t *req;
290*7c478bd9Sstevel@tonic-gate 
291*7c478bd9Sstevel@tonic-gate 	rcm_log_message(RCM_TRACE2, "info_req_add(%s, %d)\n",
292*7c478bd9Sstevel@tonic-gate 	    rsrcname, seq_num);
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate 	device = resolve_name(rsrcname);
295*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&rcm_req_lock);
296*7c478bd9Sstevel@tonic-gate 
297*7c478bd9Sstevel@tonic-gate 	/*
298*7c478bd9Sstevel@tonic-gate 	 * Look for entry with the same resource and seq_num.
299*7c478bd9Sstevel@tonic-gate 	 * If it exists, we return an error so that such
300*7c478bd9Sstevel@tonic-gate 	 * information is not gathered more than once.
301*7c478bd9Sstevel@tonic-gate 	 */
302*7c478bd9Sstevel@tonic-gate 	if (find_req_entry(device, flag, seq_num, info_req_list) != NULL) {
303*7c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_DEBUG, "getinfo cycle: %s %d \n",
304*7c478bd9Sstevel@tonic-gate 		    device, seq_num);
305*7c478bd9Sstevel@tonic-gate 		error = -1;
306*7c478bd9Sstevel@tonic-gate 		goto out;
307*7c478bd9Sstevel@tonic-gate 	}
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate 	/*
310*7c478bd9Sstevel@tonic-gate 	 * Get empty entry and fill in seq_num and device.
311*7c478bd9Sstevel@tonic-gate 	 */
312*7c478bd9Sstevel@tonic-gate 	req = get_req_entry(&info_req_list);
313*7c478bd9Sstevel@tonic-gate 	req->seq_num = seq_num;
314*7c478bd9Sstevel@tonic-gate 	req->state = RCM_STATE_ONLINE;  /* mark that the entry is in use */
315*7c478bd9Sstevel@tonic-gate 	req->flag = flag;
316*7c478bd9Sstevel@tonic-gate 	(void) strcpy(req->device, device);
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate out:
319*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&rcm_req_lock);
320*7c478bd9Sstevel@tonic-gate 	free(device);
321*7c478bd9Sstevel@tonic-gate 
322*7c478bd9Sstevel@tonic-gate 	return (error);
323*7c478bd9Sstevel@tonic-gate }
324*7c478bd9Sstevel@tonic-gate 
325*7c478bd9Sstevel@tonic-gate /*
326*7c478bd9Sstevel@tonic-gate  * Remove all entries associated with seq_num from info_req_list
327*7c478bd9Sstevel@tonic-gate  */
328*7c478bd9Sstevel@tonic-gate void
info_req_remove(int seq_num)329*7c478bd9Sstevel@tonic-gate info_req_remove(int seq_num)
330*7c478bd9Sstevel@tonic-gate {
331*7c478bd9Sstevel@tonic-gate 	int i;
332*7c478bd9Sstevel@tonic-gate 
333*7c478bd9Sstevel@tonic-gate 	rcm_log_message(RCM_TRACE3, "info_req_remove(%d)\n", seq_num);
334*7c478bd9Sstevel@tonic-gate 
335*7c478bd9Sstevel@tonic-gate 	seq_num >>= SEQ_NUM_SHIFT;
336*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&rcm_req_lock);
337*7c478bd9Sstevel@tonic-gate 
338*7c478bd9Sstevel@tonic-gate 	/* remove all entries with seq_num */
339*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < info_req_list->n_req_max; i++) {
340*7c478bd9Sstevel@tonic-gate 		if (info_req_list->req[i].state == RCM_STATE_REMOVE)
341*7c478bd9Sstevel@tonic-gate 			continue;
342*7c478bd9Sstevel@tonic-gate 
343*7c478bd9Sstevel@tonic-gate 		if ((info_req_list->req[i].seq_num >> SEQ_NUM_SHIFT) != seq_num)
344*7c478bd9Sstevel@tonic-gate 			continue;
345*7c478bd9Sstevel@tonic-gate 
346*7c478bd9Sstevel@tonic-gate 		info_req_list->req[i].state = RCM_STATE_REMOVE;
347*7c478bd9Sstevel@tonic-gate 		info_req_list->n_req--;
348*7c478bd9Sstevel@tonic-gate 	}
349*7c478bd9Sstevel@tonic-gate 
350*7c478bd9Sstevel@tonic-gate 	/*
351*7c478bd9Sstevel@tonic-gate 	 * We don't shrink the info_req_list size for now.
352*7c478bd9Sstevel@tonic-gate 	 */
353*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&rcm_req_lock);
354*7c478bd9Sstevel@tonic-gate }
355*7c478bd9Sstevel@tonic-gate 
356*7c478bd9Sstevel@tonic-gate /*
357*7c478bd9Sstevel@tonic-gate  * Checking lock conflicts. There is a conflict if:
358*7c478bd9Sstevel@tonic-gate  * - attempt to DR a node when either its ancester or descendent
359*7c478bd9Sstevel@tonic-gate  *	is in the process of DR
360*7c478bd9Sstevel@tonic-gate  * - attempt to register for a node when its ancester is locked for DR
361*7c478bd9Sstevel@tonic-gate  */
362*7c478bd9Sstevel@tonic-gate static int
check_lock(char * device,uint_t flag,int cflag,rcm_info_t ** info)363*7c478bd9Sstevel@tonic-gate check_lock(char *device, uint_t flag, int cflag, rcm_info_t **info)
364*7c478bd9Sstevel@tonic-gate {
365*7c478bd9Sstevel@tonic-gate 	int i, ret = RCM_SUCCESS;
366*7c478bd9Sstevel@tonic-gate 
367*7c478bd9Sstevel@tonic-gate 	if (info)
368*7c478bd9Sstevel@tonic-gate 		*info = NULL;
369*7c478bd9Sstevel@tonic-gate 
370*7c478bd9Sstevel@tonic-gate 	/*
371*7c478bd9Sstevel@tonic-gate 	 * During daemon initialization, don't check locks
372*7c478bd9Sstevel@tonic-gate 	 */
373*7c478bd9Sstevel@tonic-gate 	if (dr_req_list == NULL)
374*7c478bd9Sstevel@tonic-gate 		return (ret);
375*7c478bd9Sstevel@tonic-gate 
376*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < dr_req_list->n_req; i++) {
377*7c478bd9Sstevel@tonic-gate 		req_t *req = &dr_req_list->req[i];
378*7c478bd9Sstevel@tonic-gate 		char *dr_dev = req->device;
379*7c478bd9Sstevel@tonic-gate 
380*7c478bd9Sstevel@tonic-gate 		/*
381*7c478bd9Sstevel@tonic-gate 		 * Skip empty entries
382*7c478bd9Sstevel@tonic-gate 		 */
383*7c478bd9Sstevel@tonic-gate 		if ((req->state == RCM_STATE_REMOVE) || (dr_dev[0] == '\0'))
384*7c478bd9Sstevel@tonic-gate 			continue;
385*7c478bd9Sstevel@tonic-gate 
386*7c478bd9Sstevel@tonic-gate 		/*
387*7c478bd9Sstevel@tonic-gate 		 * Make sure that none of the ancestors of dr_dev is
388*7c478bd9Sstevel@tonic-gate 		 * being operated upon.
389*7c478bd9Sstevel@tonic-gate 		 */
390*7c478bd9Sstevel@tonic-gate 		if (EQUAL(device, dr_dev) || DESCENDENT(device, dr_dev)) {
391*7c478bd9Sstevel@tonic-gate 			/*
392*7c478bd9Sstevel@tonic-gate 			 * An exception to this is the filesystem.
393*7c478bd9Sstevel@tonic-gate 			 * We should allowed a filesystem rooted at a
394*7c478bd9Sstevel@tonic-gate 			 * child directory to be unmounted.
395*7c478bd9Sstevel@tonic-gate 			 */
396*7c478bd9Sstevel@tonic-gate 			if ((flag & RCM_FILESYS) && (!EQUAL(device, dr_dev) ||
397*7c478bd9Sstevel@tonic-gate 			    ((dr_req_list->req[i].flag & RCM_FILESYS) == 0)))
398*7c478bd9Sstevel@tonic-gate 				continue;
399*7c478bd9Sstevel@tonic-gate 
400*7c478bd9Sstevel@tonic-gate 			assert(info != 0);
401*7c478bd9Sstevel@tonic-gate 
402*7c478bd9Sstevel@tonic-gate 			add_busy_rsrc_to_list(dr_dev, dr_req_list->req[i].pid,
403*7c478bd9Sstevel@tonic-gate 			    dr_req_list->req[i].state,
404*7c478bd9Sstevel@tonic-gate 			    dr_req_list->req[i].seq_num, NULL, locked_info,
405*7c478bd9Sstevel@tonic-gate 			    locked_err, NULL, info);
406*7c478bd9Sstevel@tonic-gate 			ret = RCM_CONFLICT;
407*7c478bd9Sstevel@tonic-gate 			break;
408*7c478bd9Sstevel@tonic-gate 		}
409*7c478bd9Sstevel@tonic-gate 
410*7c478bd9Sstevel@tonic-gate 		if ((cflag == LOCK_FOR_DR) && DESCENDENT(dr_dev, device)) {
411*7c478bd9Sstevel@tonic-gate 			/*
412*7c478bd9Sstevel@tonic-gate 			 * Check descendents only for DR request.
413*7c478bd9Sstevel@tonic-gate 			 *
414*7c478bd9Sstevel@tonic-gate 			 * Could have multiple descendents doing DR,
415*7c478bd9Sstevel@tonic-gate 			 * we want to find them all.
416*7c478bd9Sstevel@tonic-gate 			 */
417*7c478bd9Sstevel@tonic-gate 			assert(info != 0);
418*7c478bd9Sstevel@tonic-gate 
419*7c478bd9Sstevel@tonic-gate 			add_busy_rsrc_to_list(dr_dev, dr_req_list->req[i].pid,
420*7c478bd9Sstevel@tonic-gate 			    dr_req_list->req[i].state,
421*7c478bd9Sstevel@tonic-gate 			    dr_req_list->req[i].seq_num, NULL, locked_info,
422*7c478bd9Sstevel@tonic-gate 			    locked_err, NULL, info);
423*7c478bd9Sstevel@tonic-gate 			ret = RCM_CONFLICT;
424*7c478bd9Sstevel@tonic-gate 			/* don't break here, need to find all conflicts */
425*7c478bd9Sstevel@tonic-gate 		}
426*7c478bd9Sstevel@tonic-gate 	}
427*7c478bd9Sstevel@tonic-gate 
428*7c478bd9Sstevel@tonic-gate 	return (ret);
429*7c478bd9Sstevel@tonic-gate }
430*7c478bd9Sstevel@tonic-gate 
431*7c478bd9Sstevel@tonic-gate /*
432*7c478bd9Sstevel@tonic-gate  * Check for lock conflicts for DR operation or client registration
433*7c478bd9Sstevel@tonic-gate  */
434*7c478bd9Sstevel@tonic-gate int
rsrc_check_lock_conflicts(char * rsrcname,uint_t flag,int cflag,rcm_info_t ** info)435*7c478bd9Sstevel@tonic-gate rsrc_check_lock_conflicts(char *rsrcname, uint_t flag, int cflag,
436*7c478bd9Sstevel@tonic-gate     rcm_info_t **info)
437*7c478bd9Sstevel@tonic-gate {
438*7c478bd9Sstevel@tonic-gate 	int result;
439*7c478bd9Sstevel@tonic-gate 	char *device;
440*7c478bd9Sstevel@tonic-gate 
441*7c478bd9Sstevel@tonic-gate 	device = resolve_name(rsrcname);
442*7c478bd9Sstevel@tonic-gate 	result = check_lock(device, flag, cflag, info);
443*7c478bd9Sstevel@tonic-gate 	free(device);
444*7c478bd9Sstevel@tonic-gate 
445*7c478bd9Sstevel@tonic-gate 	return (result);
446*7c478bd9Sstevel@tonic-gate }
447*7c478bd9Sstevel@tonic-gate 
448*7c478bd9Sstevel@tonic-gate static int
transition_state(int state)449*7c478bd9Sstevel@tonic-gate transition_state(int state)
450*7c478bd9Sstevel@tonic-gate {
451*7c478bd9Sstevel@tonic-gate 	/*
452*7c478bd9Sstevel@tonic-gate 	 * If the resource state is in transition, ask caller to
453*7c478bd9Sstevel@tonic-gate 	 * try again.
454*7c478bd9Sstevel@tonic-gate 	 */
455*7c478bd9Sstevel@tonic-gate 	switch (state) {
456*7c478bd9Sstevel@tonic-gate 	case RCM_STATE_OFFLINING:
457*7c478bd9Sstevel@tonic-gate 	case RCM_STATE_SUSPENDING:
458*7c478bd9Sstevel@tonic-gate 	case RCM_STATE_RESUMING:
459*7c478bd9Sstevel@tonic-gate 	case RCM_STATE_ONLINING:
460*7c478bd9Sstevel@tonic-gate 	case RCM_STATE_REMOVING:
461*7c478bd9Sstevel@tonic-gate 
462*7c478bd9Sstevel@tonic-gate 		return (1);
463*7c478bd9Sstevel@tonic-gate 
464*7c478bd9Sstevel@tonic-gate 	default:
465*7c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
466*7c478bd9Sstevel@tonic-gate 		break;
467*7c478bd9Sstevel@tonic-gate 	}
468*7c478bd9Sstevel@tonic-gate 	return (0);
469*7c478bd9Sstevel@tonic-gate }
470*7c478bd9Sstevel@tonic-gate 
471*7c478bd9Sstevel@tonic-gate /*
472*7c478bd9Sstevel@tonic-gate  * Update a dr entry in dr_req_list
473*7c478bd9Sstevel@tonic-gate  */
474*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
475*7c478bd9Sstevel@tonic-gate static int
dr_req_update_entry(char * device,pid_t pid,uint_t flag,int state,int seq_num,timespec_t * interval,rcm_info_t ** infop)476*7c478bd9Sstevel@tonic-gate dr_req_update_entry(char *device, pid_t pid, uint_t flag, int state,
477*7c478bd9Sstevel@tonic-gate     int seq_num, timespec_t *interval, rcm_info_t **infop)
478*7c478bd9Sstevel@tonic-gate {
479*7c478bd9Sstevel@tonic-gate 	req_t *req;
480*7c478bd9Sstevel@tonic-gate 
481*7c478bd9Sstevel@tonic-gate 	/*
482*7c478bd9Sstevel@tonic-gate 	 * Find request entry. If not found, return RCM_FAILURE
483*7c478bd9Sstevel@tonic-gate 	 */
484*7c478bd9Sstevel@tonic-gate 	req = find_req_entry(device, flag, -1, dr_req_list);
485*7c478bd9Sstevel@tonic-gate 
486*7c478bd9Sstevel@tonic-gate 	if (req == NULL) {
487*7c478bd9Sstevel@tonic-gate 		switch (state) {
488*7c478bd9Sstevel@tonic-gate 		case RCM_STATE_OFFLINE_QUERYING:
489*7c478bd9Sstevel@tonic-gate 		case RCM_STATE_SUSPEND_QUERYING:
490*7c478bd9Sstevel@tonic-gate 		case RCM_STATE_OFFLINING:
491*7c478bd9Sstevel@tonic-gate 		case RCM_STATE_SUSPENDING:
492*7c478bd9Sstevel@tonic-gate 			/* could be re-do operation, no error message */
493*7c478bd9Sstevel@tonic-gate 			break;
494*7c478bd9Sstevel@tonic-gate 
495*7c478bd9Sstevel@tonic-gate 		default:
496*7c478bd9Sstevel@tonic-gate 			rcm_log_message(RCM_DEBUG,
497*7c478bd9Sstevel@tonic-gate 			    "update non-existing resource %s\n", device);
498*7c478bd9Sstevel@tonic-gate 		}
499*7c478bd9Sstevel@tonic-gate 		return (RCM_FAILURE);
500*7c478bd9Sstevel@tonic-gate 	}
501*7c478bd9Sstevel@tonic-gate 
502*7c478bd9Sstevel@tonic-gate 	/*
503*7c478bd9Sstevel@tonic-gate 	 * During initialization, update is unconditional (forced)
504*7c478bd9Sstevel@tonic-gate 	 * in order to bring the daemon up in a sane state.
505*7c478bd9Sstevel@tonic-gate 	 */
506*7c478bd9Sstevel@tonic-gate 	if (rcmd_get_state() == RCMD_INIT)
507*7c478bd9Sstevel@tonic-gate 		goto update;
508*7c478bd9Sstevel@tonic-gate 
509*7c478bd9Sstevel@tonic-gate 	/*
510*7c478bd9Sstevel@tonic-gate 	 * Don't allow update with mismatched initiator pid. This could happen
511*7c478bd9Sstevel@tonic-gate 	 * as part of normal operation.
512*7c478bd9Sstevel@tonic-gate 	 */
513*7c478bd9Sstevel@tonic-gate 	if (pid != req->pid) {
514*7c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_INFO,
515*7c478bd9Sstevel@tonic-gate 		    gettext("mismatched dr initiator pid: %ld %ld\n"),
516*7c478bd9Sstevel@tonic-gate 		    req->pid, pid);
517*7c478bd9Sstevel@tonic-gate 		goto failure;
518*7c478bd9Sstevel@tonic-gate 	}
519*7c478bd9Sstevel@tonic-gate 
520*7c478bd9Sstevel@tonic-gate 	rcm_log_message(RCM_TRACE4,
521*7c478bd9Sstevel@tonic-gate 	    "dr_req_update_entry: state=%d, device=%s\n",
522*7c478bd9Sstevel@tonic-gate 	    req->state, req->device);
523*7c478bd9Sstevel@tonic-gate 
524*7c478bd9Sstevel@tonic-gate 	/*
525*7c478bd9Sstevel@tonic-gate 	 * Check that the state transition is valid
526*7c478bd9Sstevel@tonic-gate 	 */
527*7c478bd9Sstevel@tonic-gate 	switch (state) {
528*7c478bd9Sstevel@tonic-gate 	case RCM_STATE_OFFLINE_QUERYING:
529*7c478bd9Sstevel@tonic-gate 	case RCM_STATE_OFFLINING:
530*7c478bd9Sstevel@tonic-gate 		/*
531*7c478bd9Sstevel@tonic-gate 		 * This is the case of re-offlining, which applies only
532*7c478bd9Sstevel@tonic-gate 		 * if a previous attempt failed.
533*7c478bd9Sstevel@tonic-gate 		 */
534*7c478bd9Sstevel@tonic-gate 		if ((req->state != RCM_STATE_OFFLINE_FAIL) &&
535*7c478bd9Sstevel@tonic-gate 		    (req->state != RCM_STATE_OFFLINE_QUERYING) &&
536*7c478bd9Sstevel@tonic-gate 		    (req->state != RCM_STATE_OFFLINE_QUERY) &&
537*7c478bd9Sstevel@tonic-gate 		    (req->state != RCM_STATE_OFFLINE_QUERY_FAIL) &&
538*7c478bd9Sstevel@tonic-gate 		    (req->state != RCM_STATE_OFFLINE)) {
539*7c478bd9Sstevel@tonic-gate 			rcm_log_message(RCM_WARNING,
540*7c478bd9Sstevel@tonic-gate 			    gettext("%s: invalid offlining from state %d\n"),
541*7c478bd9Sstevel@tonic-gate 			    device, req->state);
542*7c478bd9Sstevel@tonic-gate 			goto failure;
543*7c478bd9Sstevel@tonic-gate 		}
544*7c478bd9Sstevel@tonic-gate 		break;
545*7c478bd9Sstevel@tonic-gate 
546*7c478bd9Sstevel@tonic-gate 	case RCM_STATE_SUSPEND_QUERYING:
547*7c478bd9Sstevel@tonic-gate 	case RCM_STATE_SUSPENDING:
548*7c478bd9Sstevel@tonic-gate 		/*
549*7c478bd9Sstevel@tonic-gate 		 * This is the case of re-suspending, which applies only
550*7c478bd9Sstevel@tonic-gate 		 * if a previous attempt failed.
551*7c478bd9Sstevel@tonic-gate 		 */
552*7c478bd9Sstevel@tonic-gate 		if ((req->state != RCM_STATE_SUSPEND_FAIL) &&
553*7c478bd9Sstevel@tonic-gate 		    (req->state != RCM_STATE_SUSPEND_QUERYING) &&
554*7c478bd9Sstevel@tonic-gate 		    (req->state != RCM_STATE_SUSPEND_QUERY) &&
555*7c478bd9Sstevel@tonic-gate 		    (req->state != RCM_STATE_SUSPEND_QUERY_FAIL) &&
556*7c478bd9Sstevel@tonic-gate 		    (req->state != RCM_STATE_SUSPEND)) {
557*7c478bd9Sstevel@tonic-gate 			rcm_log_message(RCM_WARNING,
558*7c478bd9Sstevel@tonic-gate 			    gettext("%s: invalid suspending from state %d\n"),
559*7c478bd9Sstevel@tonic-gate 			    device, req->state);
560*7c478bd9Sstevel@tonic-gate 			goto failure;
561*7c478bd9Sstevel@tonic-gate 		}
562*7c478bd9Sstevel@tonic-gate 		break;
563*7c478bd9Sstevel@tonic-gate 
564*7c478bd9Sstevel@tonic-gate 	case RCM_STATE_RESUMING:
565*7c478bd9Sstevel@tonic-gate 		if ((req->state != RCM_STATE_SUSPEND) &&
566*7c478bd9Sstevel@tonic-gate 		    (req->state != RCM_STATE_SUSPEND_QUERYING) &&
567*7c478bd9Sstevel@tonic-gate 		    (req->state != RCM_STATE_SUSPEND_QUERY) &&
568*7c478bd9Sstevel@tonic-gate 		    (req->state != RCM_STATE_SUSPEND_QUERY_FAIL) &&
569*7c478bd9Sstevel@tonic-gate 		    (req->state != RCM_STATE_SUSPEND_FAIL)) {
570*7c478bd9Sstevel@tonic-gate 			rcm_log_message(RCM_DEBUG,
571*7c478bd9Sstevel@tonic-gate 			    "%s: invalid resuming from state %d\n",
572*7c478bd9Sstevel@tonic-gate 			    device, req->state);
573*7c478bd9Sstevel@tonic-gate 			goto failure;
574*7c478bd9Sstevel@tonic-gate 		}
575*7c478bd9Sstevel@tonic-gate 		break;
576*7c478bd9Sstevel@tonic-gate 
577*7c478bd9Sstevel@tonic-gate 	case RCM_STATE_ONLINING:
578*7c478bd9Sstevel@tonic-gate 		if ((req->state != RCM_STATE_OFFLINE) &&
579*7c478bd9Sstevel@tonic-gate 		    (req->state != RCM_STATE_OFFLINE_QUERYING) &&
580*7c478bd9Sstevel@tonic-gate 		    (req->state != RCM_STATE_OFFLINE_QUERY) &&
581*7c478bd9Sstevel@tonic-gate 		    (req->state != RCM_STATE_OFFLINE_QUERY_FAIL) &&
582*7c478bd9Sstevel@tonic-gate 		    (req->state != RCM_STATE_OFFLINE_FAIL)) {
583*7c478bd9Sstevel@tonic-gate 			rcm_log_message(RCM_INFO,
584*7c478bd9Sstevel@tonic-gate 			    gettext("%s: invalid onlining from state %d\n"),
585*7c478bd9Sstevel@tonic-gate 			    device, req->state);
586*7c478bd9Sstevel@tonic-gate 			goto failure;
587*7c478bd9Sstevel@tonic-gate 		}
588*7c478bd9Sstevel@tonic-gate 		break;
589*7c478bd9Sstevel@tonic-gate 
590*7c478bd9Sstevel@tonic-gate 	case RCM_STATE_REMOVING:
591*7c478bd9Sstevel@tonic-gate 		if ((req->state != RCM_STATE_OFFLINE) &&
592*7c478bd9Sstevel@tonic-gate 		    (req->state != RCM_STATE_OFFLINE_FAIL)) {
593*7c478bd9Sstevel@tonic-gate 			rcm_log_message(RCM_INFO,
594*7c478bd9Sstevel@tonic-gate 			    gettext("%s: invalid removing from state %d\n"),
595*7c478bd9Sstevel@tonic-gate 			    device, req->state);
596*7c478bd9Sstevel@tonic-gate 			goto failure;
597*7c478bd9Sstevel@tonic-gate 		}
598*7c478bd9Sstevel@tonic-gate 		break;
599*7c478bd9Sstevel@tonic-gate 
600*7c478bd9Sstevel@tonic-gate 	case RCM_STATE_SUSPEND_FAIL:
601*7c478bd9Sstevel@tonic-gate 		assert(req->state == RCM_STATE_SUSPENDING);
602*7c478bd9Sstevel@tonic-gate 		break;
603*7c478bd9Sstevel@tonic-gate 
604*7c478bd9Sstevel@tonic-gate 	case RCM_STATE_OFFLINE_FAIL:
605*7c478bd9Sstevel@tonic-gate 		assert(req->state == RCM_STATE_OFFLINING);
606*7c478bd9Sstevel@tonic-gate 		break;
607*7c478bd9Sstevel@tonic-gate 
608*7c478bd9Sstevel@tonic-gate 	case RCM_STATE_SUSPEND:
609*7c478bd9Sstevel@tonic-gate 		assert(req->state == RCM_STATE_SUSPENDING);
610*7c478bd9Sstevel@tonic-gate 		break;
611*7c478bd9Sstevel@tonic-gate 
612*7c478bd9Sstevel@tonic-gate 	case RCM_STATE_OFFLINE:
613*7c478bd9Sstevel@tonic-gate 		assert(req->state == RCM_STATE_OFFLINING);
614*7c478bd9Sstevel@tonic-gate 		break;
615*7c478bd9Sstevel@tonic-gate 
616*7c478bd9Sstevel@tonic-gate 	case RCM_STATE_ONLINE:
617*7c478bd9Sstevel@tonic-gate 		assert((req->state == RCM_STATE_RESUMING) ||
618*7c478bd9Sstevel@tonic-gate 		    (req->state == RCM_STATE_ONLINING));
619*7c478bd9Sstevel@tonic-gate 		break;
620*7c478bd9Sstevel@tonic-gate 
621*7c478bd9Sstevel@tonic-gate 	default:	/* shouldn't be here */
622*7c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR,
623*7c478bd9Sstevel@tonic-gate 		    gettext("invalid update to dr state: %d\n"), state);
624*7c478bd9Sstevel@tonic-gate 		return (RCM_FAILURE);
625*7c478bd9Sstevel@tonic-gate 	}
626*7c478bd9Sstevel@tonic-gate 
627*7c478bd9Sstevel@tonic-gate update:
628*7c478bd9Sstevel@tonic-gate 	/*
629*7c478bd9Sstevel@tonic-gate 	 * update the state, interval, and sequence number; sync state file
630*7c478bd9Sstevel@tonic-gate 	 */
631*7c478bd9Sstevel@tonic-gate 	req->state = state;
632*7c478bd9Sstevel@tonic-gate 	req->seq_num = seq_num;
633*7c478bd9Sstevel@tonic-gate 
634*7c478bd9Sstevel@tonic-gate 	if (interval)
635*7c478bd9Sstevel@tonic-gate 		req->interval = *interval;
636*7c478bd9Sstevel@tonic-gate 	else
637*7c478bd9Sstevel@tonic-gate 		bzero(&req->interval, sizeof (timespec_t));
638*7c478bd9Sstevel@tonic-gate 
639*7c478bd9Sstevel@tonic-gate 	(void) fsync(state_fd);
640*7c478bd9Sstevel@tonic-gate 	return (RCM_SUCCESS);
641*7c478bd9Sstevel@tonic-gate 
642*7c478bd9Sstevel@tonic-gate failure:
643*7c478bd9Sstevel@tonic-gate 	if (infop != NULL) {
644*7c478bd9Sstevel@tonic-gate 		add_busy_rsrc_to_list(req->device, req->pid, req->state,
645*7c478bd9Sstevel@tonic-gate 		    req->seq_num, NULL, locked_info, locked_err, NULL, infop);
646*7c478bd9Sstevel@tonic-gate 	}
647*7c478bd9Sstevel@tonic-gate 
648*7c478bd9Sstevel@tonic-gate 	/*
649*7c478bd9Sstevel@tonic-gate 	 * A request may be left in a transition state because the operator
650*7c478bd9Sstevel@tonic-gate 	 * typed ctrl-C. In this case, the daemon thread continues to run
651*7c478bd9Sstevel@tonic-gate 	 * and will eventually put the state in a non-transitional state.
652*7c478bd9Sstevel@tonic-gate 	 *
653*7c478bd9Sstevel@tonic-gate 	 * To be safe, we return EAGAIN to allow librcm to loop and retry.
654*7c478bd9Sstevel@tonic-gate 	 * If we are called from a module, loop & retry could result in a
655*7c478bd9Sstevel@tonic-gate 	 * deadlock. The called will check for this case and turn EAGAIN
656*7c478bd9Sstevel@tonic-gate 	 * into RCM_CONFLICT.
657*7c478bd9Sstevel@tonic-gate 	 */
658*7c478bd9Sstevel@tonic-gate 	if (transition_state(req->state)) {
659*7c478bd9Sstevel@tonic-gate 		return (EAGAIN);
660*7c478bd9Sstevel@tonic-gate 	}
661*7c478bd9Sstevel@tonic-gate 
662*7c478bd9Sstevel@tonic-gate 	return (RCM_CONFLICT);
663*7c478bd9Sstevel@tonic-gate }
664*7c478bd9Sstevel@tonic-gate 
665*7c478bd9Sstevel@tonic-gate /*
666*7c478bd9Sstevel@tonic-gate  * Insert a dr entry in dr_req_list
667*7c478bd9Sstevel@tonic-gate  */
668*7c478bd9Sstevel@tonic-gate int
dr_req_add(char * rsrcname,pid_t pid,uint_t flag,int state,int seq_num,timespec_t * interval,rcm_info_t ** info)669*7c478bd9Sstevel@tonic-gate dr_req_add(char *rsrcname, pid_t pid, uint_t flag, int state, int seq_num,
670*7c478bd9Sstevel@tonic-gate     timespec_t *interval, rcm_info_t **info)
671*7c478bd9Sstevel@tonic-gate {
672*7c478bd9Sstevel@tonic-gate 	int error;
673*7c478bd9Sstevel@tonic-gate 	char *device;
674*7c478bd9Sstevel@tonic-gate 	req_t *req;
675*7c478bd9Sstevel@tonic-gate 
676*7c478bd9Sstevel@tonic-gate 	rcm_log_message(RCM_TRACE3, "dr_req_add(%s, %ld, 0x%x, %d, %d, %p)\n",
677*7c478bd9Sstevel@tonic-gate 	    rsrcname, pid, flag, state, seq_num, (void *)info);
678*7c478bd9Sstevel@tonic-gate 
679*7c478bd9Sstevel@tonic-gate 	device = resolve_name(rsrcname);
680*7c478bd9Sstevel@tonic-gate 	if (device == NULL)
681*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
682*7c478bd9Sstevel@tonic-gate 
683*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&rcm_req_lock);
684*7c478bd9Sstevel@tonic-gate 
685*7c478bd9Sstevel@tonic-gate 	/*
686*7c478bd9Sstevel@tonic-gate 	 * In the re-offline/suspend case, attempt to update dr request.
687*7c478bd9Sstevel@tonic-gate 	 *
688*7c478bd9Sstevel@tonic-gate 	 * If this succeeds, return success;
689*7c478bd9Sstevel@tonic-gate 	 * If this fails because of a conflict, return error;
690*7c478bd9Sstevel@tonic-gate 	 * If this this fails because no entry exists, add a new entry.
691*7c478bd9Sstevel@tonic-gate 	 */
692*7c478bd9Sstevel@tonic-gate 	error = dr_req_update_entry(device, pid, flag, state, seq_num, interval,
693*7c478bd9Sstevel@tonic-gate 	    info);
694*7c478bd9Sstevel@tonic-gate 
695*7c478bd9Sstevel@tonic-gate 	switch (error) {
696*7c478bd9Sstevel@tonic-gate 	case RCM_FAILURE:
697*7c478bd9Sstevel@tonic-gate 		/* proceed to add a new entry */
698*7c478bd9Sstevel@tonic-gate 		break;
699*7c478bd9Sstevel@tonic-gate 
700*7c478bd9Sstevel@tonic-gate 	case RCM_CONFLICT:
701*7c478bd9Sstevel@tonic-gate 	case RCM_SUCCESS:
702*7c478bd9Sstevel@tonic-gate 	case EAGAIN:
703*7c478bd9Sstevel@tonic-gate 	default:
704*7c478bd9Sstevel@tonic-gate 		goto out;
705*7c478bd9Sstevel@tonic-gate 	}
706*7c478bd9Sstevel@tonic-gate 
707*7c478bd9Sstevel@tonic-gate 	/*
708*7c478bd9Sstevel@tonic-gate 	 * Check for lock conflicts
709*7c478bd9Sstevel@tonic-gate 	 */
710*7c478bd9Sstevel@tonic-gate 	error = check_lock(device, flag, LOCK_FOR_DR, info);
711*7c478bd9Sstevel@tonic-gate 	if (error != RCM_SUCCESS) {
712*7c478bd9Sstevel@tonic-gate 		error = RCM_CONFLICT;
713*7c478bd9Sstevel@tonic-gate 		goto out;
714*7c478bd9Sstevel@tonic-gate 	}
715*7c478bd9Sstevel@tonic-gate 
716*7c478bd9Sstevel@tonic-gate 	/*
717*7c478bd9Sstevel@tonic-gate 	 * Get empty request entry, fill in values and sync state file
718*7c478bd9Sstevel@tonic-gate 	 */
719*7c478bd9Sstevel@tonic-gate 	req = get_req_entry(&dr_req_list);
720*7c478bd9Sstevel@tonic-gate 
721*7c478bd9Sstevel@tonic-gate 	req->seq_num = seq_num;
722*7c478bd9Sstevel@tonic-gate 	req->pid = pid;
723*7c478bd9Sstevel@tonic-gate 	req->flag = flag;
724*7c478bd9Sstevel@tonic-gate 	req->state = state;
725*7c478bd9Sstevel@tonic-gate 	req->type = rsrc_get_type(device);
726*7c478bd9Sstevel@tonic-gate 	(void) strcpy(req->device, device);
727*7c478bd9Sstevel@tonic-gate 
728*7c478bd9Sstevel@tonic-gate 	/* cache interval for failure recovery */
729*7c478bd9Sstevel@tonic-gate 	if (interval)
730*7c478bd9Sstevel@tonic-gate 		req->interval = *interval;
731*7c478bd9Sstevel@tonic-gate 	else
732*7c478bd9Sstevel@tonic-gate 		bzero(&req->interval, sizeof (timespec_t));
733*7c478bd9Sstevel@tonic-gate 
734*7c478bd9Sstevel@tonic-gate 	(void) fsync(state_fd);
735*7c478bd9Sstevel@tonic-gate 
736*7c478bd9Sstevel@tonic-gate 	/*
737*7c478bd9Sstevel@tonic-gate 	 * Add initiator pid to polling list
738*7c478bd9Sstevel@tonic-gate 	 */
739*7c478bd9Sstevel@tonic-gate 	add_to_polling_list(req->pid);
740*7c478bd9Sstevel@tonic-gate 
741*7c478bd9Sstevel@tonic-gate out:
742*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&rcm_req_lock);
743*7c478bd9Sstevel@tonic-gate 	free(device);
744*7c478bd9Sstevel@tonic-gate 
745*7c478bd9Sstevel@tonic-gate 	return (error);
746*7c478bd9Sstevel@tonic-gate }
747*7c478bd9Sstevel@tonic-gate 
748*7c478bd9Sstevel@tonic-gate /*
749*7c478bd9Sstevel@tonic-gate  * Update a dr entry in dr_req_list
750*7c478bd9Sstevel@tonic-gate  */
751*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
752*7c478bd9Sstevel@tonic-gate int
dr_req_update(char * rsrcname,pid_t pid,uint_t flag,int state,int seq_num,rcm_info_t ** info)753*7c478bd9Sstevel@tonic-gate dr_req_update(char *rsrcname, pid_t pid, uint_t flag, int state, int seq_num,
754*7c478bd9Sstevel@tonic-gate     rcm_info_t **info)
755*7c478bd9Sstevel@tonic-gate {
756*7c478bd9Sstevel@tonic-gate 	int error;
757*7c478bd9Sstevel@tonic-gate 	char *device = resolve_name(rsrcname);
758*7c478bd9Sstevel@tonic-gate 
759*7c478bd9Sstevel@tonic-gate 	rcm_log_message(RCM_TRACE3, "dr_req_update(%s, %ld, 0x%x, %d, %d)\n",
760*7c478bd9Sstevel@tonic-gate 	    rsrcname, pid, flag, state, seq_num);
761*7c478bd9Sstevel@tonic-gate 
762*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&rcm_req_lock);
763*7c478bd9Sstevel@tonic-gate 	error = dr_req_update_entry(device, pid, flag, state, seq_num, NULL,
764*7c478bd9Sstevel@tonic-gate 	    info);
765*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&rcm_req_lock);
766*7c478bd9Sstevel@tonic-gate 	free(device);
767*7c478bd9Sstevel@tonic-gate 
768*7c478bd9Sstevel@tonic-gate 	return (error);
769*7c478bd9Sstevel@tonic-gate }
770*7c478bd9Sstevel@tonic-gate 
771*7c478bd9Sstevel@tonic-gate /*
772*7c478bd9Sstevel@tonic-gate  * This function scans the DR request list for the next, non-removed
773*7c478bd9Sstevel@tonic-gate  * entry that is part of the specified sequence.  The 'device' name
774*7c478bd9Sstevel@tonic-gate  * of the entry is copied into the provided 'rsrc' buffer.
775*7c478bd9Sstevel@tonic-gate  *
776*7c478bd9Sstevel@tonic-gate  * The 'rsrc' buffer is required because the DR request list is only
777*7c478bd9Sstevel@tonic-gate  * locked during the duration of this lookup.  Giving a direct pointer
778*7c478bd9Sstevel@tonic-gate  * to something in the list would be unsafe.
779*7c478bd9Sstevel@tonic-gate  */
780*7c478bd9Sstevel@tonic-gate int
dr_req_lookup(int seq_num,char * rsrc)781*7c478bd9Sstevel@tonic-gate dr_req_lookup(int seq_num, char *rsrc)
782*7c478bd9Sstevel@tonic-gate {
783*7c478bd9Sstevel@tonic-gate 	int	i;
784*7c478bd9Sstevel@tonic-gate 	int	len;
785*7c478bd9Sstevel@tonic-gate 	int	base = (seq_num >> SEQ_NUM_SHIFT);
786*7c478bd9Sstevel@tonic-gate 	int	retval = RCM_FAILURE;
787*7c478bd9Sstevel@tonic-gate 
788*7c478bd9Sstevel@tonic-gate 	if (rsrc == NULL) {
789*7c478bd9Sstevel@tonic-gate 		return (RCM_FAILURE);
790*7c478bd9Sstevel@tonic-gate 	}
791*7c478bd9Sstevel@tonic-gate 
792*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&rcm_req_lock);
793*7c478bd9Sstevel@tonic-gate 
794*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < dr_req_list->n_req_max; i++) {
795*7c478bd9Sstevel@tonic-gate 
796*7c478bd9Sstevel@tonic-gate 		/* Skip removed or non-matching entries */
797*7c478bd9Sstevel@tonic-gate 		if ((dr_req_list->req[i].state == RCM_STATE_REMOVE) ||
798*7c478bd9Sstevel@tonic-gate 		    ((dr_req_list->req[i].seq_num >> SEQ_NUM_SHIFT) != base)) {
799*7c478bd9Sstevel@tonic-gate 			continue;
800*7c478bd9Sstevel@tonic-gate 		}
801*7c478bd9Sstevel@tonic-gate 
802*7c478bd9Sstevel@tonic-gate 		/* Copy the next-matching 'device' name into 'rsrc' */
803*7c478bd9Sstevel@tonic-gate 		len = strlcpy(rsrc, dr_req_list->req[i].device, MAXPATHLEN);
804*7c478bd9Sstevel@tonic-gate 		if (len < MAXPATHLEN) {
805*7c478bd9Sstevel@tonic-gate 			retval = RCM_SUCCESS;
806*7c478bd9Sstevel@tonic-gate 		}
807*7c478bd9Sstevel@tonic-gate 		break;
808*7c478bd9Sstevel@tonic-gate 	}
809*7c478bd9Sstevel@tonic-gate 
810*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&rcm_req_lock);
811*7c478bd9Sstevel@tonic-gate 
812*7c478bd9Sstevel@tonic-gate 	return (retval);
813*7c478bd9Sstevel@tonic-gate }
814*7c478bd9Sstevel@tonic-gate 
815*7c478bd9Sstevel@tonic-gate /*
816*7c478bd9Sstevel@tonic-gate  * Remove a dr entry in dr_req_list
817*7c478bd9Sstevel@tonic-gate  */
818*7c478bd9Sstevel@tonic-gate void
dr_req_remove(char * rsrcname,uint_t flag)819*7c478bd9Sstevel@tonic-gate dr_req_remove(char *rsrcname, uint_t flag)
820*7c478bd9Sstevel@tonic-gate {
821*7c478bd9Sstevel@tonic-gate 	req_t *req;
822*7c478bd9Sstevel@tonic-gate 	char *device = resolve_name(rsrcname);
823*7c478bd9Sstevel@tonic-gate 
824*7c478bd9Sstevel@tonic-gate 	rcm_log_message(RCM_TRACE3, "dr_req_remove(%s)\n", rsrcname);
825*7c478bd9Sstevel@tonic-gate 
826*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&rcm_req_lock);
827*7c478bd9Sstevel@tonic-gate 
828*7c478bd9Sstevel@tonic-gate 	/* find entry */
829*7c478bd9Sstevel@tonic-gate 	req = find_req_entry(device, flag, -1, dr_req_list);
830*7c478bd9Sstevel@tonic-gate 	free(device);
831*7c478bd9Sstevel@tonic-gate 
832*7c478bd9Sstevel@tonic-gate 	if (req == NULL) {
833*7c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&rcm_req_lock);
834*7c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_WARNING,
835*7c478bd9Sstevel@tonic-gate 		    gettext("dr_req entry %s not found\n"), rsrcname);
836*7c478bd9Sstevel@tonic-gate 		return;
837*7c478bd9Sstevel@tonic-gate 	}
838*7c478bd9Sstevel@tonic-gate 
839*7c478bd9Sstevel@tonic-gate 	req->state = RCM_STATE_REMOVE;
840*7c478bd9Sstevel@tonic-gate 	dr_req_list->n_req--;
841*7c478bd9Sstevel@tonic-gate 	(void) fsync(state_fd);
842*7c478bd9Sstevel@tonic-gate 
843*7c478bd9Sstevel@tonic-gate 	/*
844*7c478bd9Sstevel@tonic-gate 	 * remove pid from polling list
845*7c478bd9Sstevel@tonic-gate 	 */
846*7c478bd9Sstevel@tonic-gate 	remove_from_polling_list(req->pid);
847*7c478bd9Sstevel@tonic-gate 
848*7c478bd9Sstevel@tonic-gate 	/*
849*7c478bd9Sstevel@tonic-gate 	 * We don't shrink the dr_req_list size for now.
850*7c478bd9Sstevel@tonic-gate 	 * Shouldn't cause big memory leaks.
851*7c478bd9Sstevel@tonic-gate 	 */
852*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&rcm_req_lock);
853*7c478bd9Sstevel@tonic-gate }
854*7c478bd9Sstevel@tonic-gate 
855*7c478bd9Sstevel@tonic-gate /*
856*7c478bd9Sstevel@tonic-gate  * Return the list of ongoing dr operation requests
857*7c478bd9Sstevel@tonic-gate  */
858*7c478bd9Sstevel@tonic-gate rcm_info_t *
rsrc_dr_info()859*7c478bd9Sstevel@tonic-gate rsrc_dr_info()
860*7c478bd9Sstevel@tonic-gate {
861*7c478bd9Sstevel@tonic-gate 	int i;
862*7c478bd9Sstevel@tonic-gate 	rcm_info_t *info;
863*7c478bd9Sstevel@tonic-gate 	rcm_info_t *result = NULL;
864*7c478bd9Sstevel@tonic-gate 	char *rsrc;
865*7c478bd9Sstevel@tonic-gate 	int len;
866*7c478bd9Sstevel@tonic-gate 
867*7c478bd9Sstevel@tonic-gate 	rcm_log_message(RCM_TRACE2, "rsrc_dr_info()\n");
868*7c478bd9Sstevel@tonic-gate 
869*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&rcm_req_lock);
870*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < dr_req_list->n_req_max; i++) {
871*7c478bd9Sstevel@tonic-gate 		if (dr_req_list->req[i].state == RCM_STATE_REMOVE)
872*7c478bd9Sstevel@tonic-gate 			continue;
873*7c478bd9Sstevel@tonic-gate 
874*7c478bd9Sstevel@tonic-gate 		if (dr_req_list->req[i].device[0] == '\0')
875*7c478bd9Sstevel@tonic-gate 			continue;
876*7c478bd9Sstevel@tonic-gate 
877*7c478bd9Sstevel@tonic-gate 		if (dr_req_list->req[i].flag & RCM_FILESYS) {
878*7c478bd9Sstevel@tonic-gate 			len = strlen(dr_req_list->req[i].device) + 5;
879*7c478bd9Sstevel@tonic-gate 			rsrc = s_malloc(len);
880*7c478bd9Sstevel@tonic-gate 			(void) snprintf(rsrc, len, "%s(fs)",
881*7c478bd9Sstevel@tonic-gate 			    dr_req_list->req[i].device);
882*7c478bd9Sstevel@tonic-gate 		} else {
883*7c478bd9Sstevel@tonic-gate 			rsrc = s_strdup(dr_req_list->req[i].device);
884*7c478bd9Sstevel@tonic-gate 		}
885*7c478bd9Sstevel@tonic-gate 
886*7c478bd9Sstevel@tonic-gate 		info = s_calloc(1, sizeof (*info));
887*7c478bd9Sstevel@tonic-gate 		if (errno = nvlist_alloc(&(info->info), NV_UNIQUE_NAME, 0)) {
888*7c478bd9Sstevel@tonic-gate 			rcm_log_message(RCM_ERROR,
889*7c478bd9Sstevel@tonic-gate 			    gettext("failed (nvlist_alloc=%s).\n"),
890*7c478bd9Sstevel@tonic-gate 			    strerror(errno));
891*7c478bd9Sstevel@tonic-gate 			rcmd_exit(errno);
892*7c478bd9Sstevel@tonic-gate 		}
893*7c478bd9Sstevel@tonic-gate 
894*7c478bd9Sstevel@tonic-gate 		if (errno = nvlist_add_string(info->info, RCM_RSRCNAME, rsrc)) {
895*7c478bd9Sstevel@tonic-gate 			rcm_log_message(RCM_ERROR,
896*7c478bd9Sstevel@tonic-gate 			    gettext("failed (nvlist_add=%s).\n"),
897*7c478bd9Sstevel@tonic-gate 			    strerror(errno));
898*7c478bd9Sstevel@tonic-gate 			rcmd_exit(errno);
899*7c478bd9Sstevel@tonic-gate 		}
900*7c478bd9Sstevel@tonic-gate 		(void) free(rsrc);
901*7c478bd9Sstevel@tonic-gate 
902*7c478bd9Sstevel@tonic-gate 		if (errno = nvlist_add_int64(info->info, RCM_CLIENT_ID,
903*7c478bd9Sstevel@tonic-gate 		    dr_req_list->req[i].pid)) {
904*7c478bd9Sstevel@tonic-gate 			rcm_log_message(RCM_ERROR,
905*7c478bd9Sstevel@tonic-gate 			    gettext("failed (nvlist_add=%s).\n"),
906*7c478bd9Sstevel@tonic-gate 			    strerror(errno));
907*7c478bd9Sstevel@tonic-gate 			rcmd_exit(errno);
908*7c478bd9Sstevel@tonic-gate 		}
909*7c478bd9Sstevel@tonic-gate 
910*7c478bd9Sstevel@tonic-gate 		if (errno = nvlist_add_int32(info->info, RCM_SEQ_NUM,
911*7c478bd9Sstevel@tonic-gate 		    dr_req_list->req[i].seq_num)) {
912*7c478bd9Sstevel@tonic-gate 			rcm_log_message(RCM_ERROR,
913*7c478bd9Sstevel@tonic-gate 			    gettext("failed (nvlist_add=%s).\n"),
914*7c478bd9Sstevel@tonic-gate 			    strerror(errno));
915*7c478bd9Sstevel@tonic-gate 			rcmd_exit(errno);
916*7c478bd9Sstevel@tonic-gate 		}
917*7c478bd9Sstevel@tonic-gate 
918*7c478bd9Sstevel@tonic-gate 		if (errno = nvlist_add_int32(info->info, RCM_RSRCSTATE,
919*7c478bd9Sstevel@tonic-gate 		    dr_req_list->req[i].state)) {
920*7c478bd9Sstevel@tonic-gate 			rcm_log_message(RCM_ERROR,
921*7c478bd9Sstevel@tonic-gate 			    gettext("failed (nvlist_add=%s).\n"),
922*7c478bd9Sstevel@tonic-gate 			    strerror(errno));
923*7c478bd9Sstevel@tonic-gate 			rcmd_exit(errno);
924*7c478bd9Sstevel@tonic-gate 		}
925*7c478bd9Sstevel@tonic-gate 
926*7c478bd9Sstevel@tonic-gate 		if (errno = nvlist_add_string(info->info, RCM_CLIENT_INFO,
927*7c478bd9Sstevel@tonic-gate 		    (char *)locked_info)) {
928*7c478bd9Sstevel@tonic-gate 			rcm_log_message(RCM_ERROR,
929*7c478bd9Sstevel@tonic-gate 			    gettext("failed (nvlist_add=%s).\n"),
930*7c478bd9Sstevel@tonic-gate 			    strerror(errno));
931*7c478bd9Sstevel@tonic-gate 			rcmd_exit(errno);
932*7c478bd9Sstevel@tonic-gate 		}
933*7c478bd9Sstevel@tonic-gate 
934*7c478bd9Sstevel@tonic-gate 		info->next = result;
935*7c478bd9Sstevel@tonic-gate 		result = info;
936*7c478bd9Sstevel@tonic-gate 	}
937*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&rcm_req_lock);
938*7c478bd9Sstevel@tonic-gate 
939*7c478bd9Sstevel@tonic-gate 	return (result);
940*7c478bd9Sstevel@tonic-gate }
941*7c478bd9Sstevel@tonic-gate 
942*7c478bd9Sstevel@tonic-gate /*
943*7c478bd9Sstevel@tonic-gate  * Eliminate entries whose dr initiator is no longer running
944*7c478bd9Sstevel@tonic-gate  * and recover daemon state during daemon restart.
945*7c478bd9Sstevel@tonic-gate  *
946*7c478bd9Sstevel@tonic-gate  * This routine is called from either during daemon initialization
947*7c478bd9Sstevel@tonic-gate  * after all modules have registered resources or from the cleanup
948*7c478bd9Sstevel@tonic-gate  * thread. In either case, it is the only thread running in the
949*7c478bd9Sstevel@tonic-gate  * daemon.
950*7c478bd9Sstevel@tonic-gate  */
951*7c478bd9Sstevel@tonic-gate void
clean_dr_list()952*7c478bd9Sstevel@tonic-gate clean_dr_list()
953*7c478bd9Sstevel@tonic-gate {
954*7c478bd9Sstevel@tonic-gate 	int i;
955*7c478bd9Sstevel@tonic-gate 	struct clean_list {
956*7c478bd9Sstevel@tonic-gate 		struct clean_list *next;
957*7c478bd9Sstevel@tonic-gate 		char *rsrcname;
958*7c478bd9Sstevel@tonic-gate 		pid_t pid;
959*7c478bd9Sstevel@tonic-gate 		int seq_num;
960*7c478bd9Sstevel@tonic-gate 		int state;
961*7c478bd9Sstevel@tonic-gate 		timespec_t interval;
962*7c478bd9Sstevel@tonic-gate 	} *tmp, *list = NULL;
963*7c478bd9Sstevel@tonic-gate 	char *rsrcnames[2];
964*7c478bd9Sstevel@tonic-gate 
965*7c478bd9Sstevel@tonic-gate 	rcm_log_message(RCM_TRACE3,
966*7c478bd9Sstevel@tonic-gate 	    "clean_dr_list(): look for stale dr initiators\n");
967*7c478bd9Sstevel@tonic-gate 
968*7c478bd9Sstevel@tonic-gate 	rsrcnames[1] = NULL;
969*7c478bd9Sstevel@tonic-gate 
970*7c478bd9Sstevel@tonic-gate 	/*
971*7c478bd9Sstevel@tonic-gate 	 * Make a list of entries to recover. This is necessary because
972*7c478bd9Sstevel@tonic-gate 	 * the recovery operation will modify dr_req_list.
973*7c478bd9Sstevel@tonic-gate 	 */
974*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&rcm_req_lock);
975*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < dr_req_list->n_req_max; i++) {
976*7c478bd9Sstevel@tonic-gate 		/* skip empty entries */
977*7c478bd9Sstevel@tonic-gate 		if (dr_req_list->req[i].state == RCM_STATE_REMOVE)
978*7c478bd9Sstevel@tonic-gate 			continue;
979*7c478bd9Sstevel@tonic-gate 
980*7c478bd9Sstevel@tonic-gate 		if (dr_req_list->req[i].device[0] == '\0')
981*7c478bd9Sstevel@tonic-gate 			continue;
982*7c478bd9Sstevel@tonic-gate 
983*7c478bd9Sstevel@tonic-gate 		/* skip cascade operations */
984*7c478bd9Sstevel@tonic-gate 		if (dr_req_list->req[i].seq_num & SEQ_NUM_MASK)
985*7c478bd9Sstevel@tonic-gate 			continue;
986*7c478bd9Sstevel@tonic-gate 
987*7c478bd9Sstevel@tonic-gate 		/*
988*7c478bd9Sstevel@tonic-gate 		 * In the cleanup case, ignore entries with initiators alive
989*7c478bd9Sstevel@tonic-gate 		 */
990*7c478bd9Sstevel@tonic-gate 		if ((rcmd_get_state() == RCMD_CLEANUP) &&
991*7c478bd9Sstevel@tonic-gate 		    proc_exist(dr_req_list->req[i].pid))
992*7c478bd9Sstevel@tonic-gate 			continue;
993*7c478bd9Sstevel@tonic-gate 
994*7c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_TRACE1,
995*7c478bd9Sstevel@tonic-gate 		    "found stale entry: %s\n", dr_req_list->req[i].device);
996*7c478bd9Sstevel@tonic-gate 
997*7c478bd9Sstevel@tonic-gate 		tmp = s_malloc(sizeof (*tmp));
998*7c478bd9Sstevel@tonic-gate 		tmp->rsrcname = s_strdup(dr_req_list->req[i].device);
999*7c478bd9Sstevel@tonic-gate 		tmp->state = dr_req_list->req[i].state;
1000*7c478bd9Sstevel@tonic-gate 		tmp->pid = dr_req_list->req[i].pid;
1001*7c478bd9Sstevel@tonic-gate 		tmp->seq_num = dr_req_list->req[i].seq_num;
1002*7c478bd9Sstevel@tonic-gate 		tmp->interval = dr_req_list->req[i].interval;
1003*7c478bd9Sstevel@tonic-gate 		tmp->next = list;
1004*7c478bd9Sstevel@tonic-gate 		list = tmp;
1005*7c478bd9Sstevel@tonic-gate 	}
1006*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&rcm_req_lock);
1007*7c478bd9Sstevel@tonic-gate 
1008*7c478bd9Sstevel@tonic-gate 	if (list == NULL)
1009*7c478bd9Sstevel@tonic-gate 		return;
1010*7c478bd9Sstevel@tonic-gate 
1011*7c478bd9Sstevel@tonic-gate 	/*
1012*7c478bd9Sstevel@tonic-gate 	 * If everything worked normally, we shouldn't be here.
1013*7c478bd9Sstevel@tonic-gate 	 * Since we are here, something went wrong, so say something.
1014*7c478bd9Sstevel@tonic-gate 	 */
1015*7c478bd9Sstevel@tonic-gate 	if (rcmd_get_state() == RCMD_INIT) {
1016*7c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_NOTICE, gettext("rcm_daemon died "
1017*7c478bd9Sstevel@tonic-gate 		    "unexpectedly, recovering previous daemon state\n"));
1018*7c478bd9Sstevel@tonic-gate 	} else {
1019*7c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_INFO, gettext("one or more dr initiator "
1020*7c478bd9Sstevel@tonic-gate 		    "died, attempting automatic recovery\n"));
1021*7c478bd9Sstevel@tonic-gate 	}
1022*7c478bd9Sstevel@tonic-gate 
1023*7c478bd9Sstevel@tonic-gate 	while (list) {
1024*7c478bd9Sstevel@tonic-gate 		tmp = list;
1025*7c478bd9Sstevel@tonic-gate 		list = tmp->next;
1026*7c478bd9Sstevel@tonic-gate 
1027*7c478bd9Sstevel@tonic-gate 		switch (tmp->state) {
1028*7c478bd9Sstevel@tonic-gate 		case RCM_STATE_OFFLINE_QUERY:
1029*7c478bd9Sstevel@tonic-gate 		case RCM_STATE_OFFLINE_QUERY_FAIL:
1030*7c478bd9Sstevel@tonic-gate 			rsrcnames[0] = tmp->rsrcname;
1031*7c478bd9Sstevel@tonic-gate 			if (proc_exist(tmp->pid)) {
1032*7c478bd9Sstevel@tonic-gate 				/* redo */
1033*7c478bd9Sstevel@tonic-gate 				(void) process_resource_offline(rsrcnames,
1034*7c478bd9Sstevel@tonic-gate 				    tmp->pid, RCM_QUERY, tmp->seq_num, NULL);
1035*7c478bd9Sstevel@tonic-gate 			} else {
1036*7c478bd9Sstevel@tonic-gate 				/* undo */
1037*7c478bd9Sstevel@tonic-gate 				(void) notify_resource_online(rsrcnames,
1038*7c478bd9Sstevel@tonic-gate 				    tmp->pid, 0, tmp->seq_num, NULL);
1039*7c478bd9Sstevel@tonic-gate 			}
1040*7c478bd9Sstevel@tonic-gate 			break;
1041*7c478bd9Sstevel@tonic-gate 
1042*7c478bd9Sstevel@tonic-gate 		case RCM_STATE_OFFLINE:
1043*7c478bd9Sstevel@tonic-gate 		case RCM_STATE_OFFLINE_FAIL:
1044*7c478bd9Sstevel@tonic-gate 			rsrcnames[0] = tmp->rsrcname;
1045*7c478bd9Sstevel@tonic-gate 			if (proc_exist(tmp->pid)) {
1046*7c478bd9Sstevel@tonic-gate 				/* redo */
1047*7c478bd9Sstevel@tonic-gate 				(void) process_resource_offline(rsrcnames,
1048*7c478bd9Sstevel@tonic-gate 				    tmp->pid, 0, tmp->seq_num, NULL);
1049*7c478bd9Sstevel@tonic-gate 			} else {
1050*7c478bd9Sstevel@tonic-gate 				/* undo */
1051*7c478bd9Sstevel@tonic-gate 				(void) notify_resource_online(rsrcnames,
1052*7c478bd9Sstevel@tonic-gate 				    tmp->pid, 0, tmp->seq_num, NULL);
1053*7c478bd9Sstevel@tonic-gate 			}
1054*7c478bd9Sstevel@tonic-gate 			break;
1055*7c478bd9Sstevel@tonic-gate 
1056*7c478bd9Sstevel@tonic-gate 		case RCM_STATE_SUSPEND_QUERY:
1057*7c478bd9Sstevel@tonic-gate 		case RCM_STATE_SUSPEND_QUERY_FAIL:
1058*7c478bd9Sstevel@tonic-gate 			rsrcnames[0] = tmp->rsrcname;
1059*7c478bd9Sstevel@tonic-gate 			if (proc_exist(tmp->pid)) {
1060*7c478bd9Sstevel@tonic-gate 				/* redo */
1061*7c478bd9Sstevel@tonic-gate 				(void) process_resource_suspend(rsrcnames,
1062*7c478bd9Sstevel@tonic-gate 				    tmp->pid, RCM_QUERY, tmp->seq_num,
1063*7c478bd9Sstevel@tonic-gate 				    &tmp->interval, NULL);
1064*7c478bd9Sstevel@tonic-gate 			} else {
1065*7c478bd9Sstevel@tonic-gate 				/* undo */
1066*7c478bd9Sstevel@tonic-gate 				(void) notify_resource_resume(rsrcnames,
1067*7c478bd9Sstevel@tonic-gate 				    tmp->pid, 0, tmp->seq_num, NULL);
1068*7c478bd9Sstevel@tonic-gate 			}
1069*7c478bd9Sstevel@tonic-gate 			break;
1070*7c478bd9Sstevel@tonic-gate 
1071*7c478bd9Sstevel@tonic-gate 		case RCM_STATE_SUSPEND:
1072*7c478bd9Sstevel@tonic-gate 		case RCM_STATE_SUSPEND_FAIL:
1073*7c478bd9Sstevel@tonic-gate 			rsrcnames[0] = tmp->rsrcname;
1074*7c478bd9Sstevel@tonic-gate 			if (proc_exist(tmp->pid)) {
1075*7c478bd9Sstevel@tonic-gate 				/* redo */
1076*7c478bd9Sstevel@tonic-gate 				(void) process_resource_suspend(rsrcnames,
1077*7c478bd9Sstevel@tonic-gate 				    tmp->pid, 0, tmp->seq_num, &tmp->interval,
1078*7c478bd9Sstevel@tonic-gate 				    NULL);
1079*7c478bd9Sstevel@tonic-gate 			} else {
1080*7c478bd9Sstevel@tonic-gate 				/* undo */
1081*7c478bd9Sstevel@tonic-gate 				(void) notify_resource_resume(rsrcnames,
1082*7c478bd9Sstevel@tonic-gate 				    tmp->pid, 0, tmp->seq_num, NULL);
1083*7c478bd9Sstevel@tonic-gate 			}
1084*7c478bd9Sstevel@tonic-gate 			break;
1085*7c478bd9Sstevel@tonic-gate 
1086*7c478bd9Sstevel@tonic-gate 		case RCM_STATE_OFFLINING:
1087*7c478bd9Sstevel@tonic-gate 		case RCM_STATE_ONLINING:
1088*7c478bd9Sstevel@tonic-gate 			rsrcnames[0] = tmp->rsrcname;
1089*7c478bd9Sstevel@tonic-gate 			(void) notify_resource_online(rsrcnames, tmp->pid, 0,
1090*7c478bd9Sstevel@tonic-gate 			    tmp->seq_num, NULL);
1091*7c478bd9Sstevel@tonic-gate 			break;
1092*7c478bd9Sstevel@tonic-gate 
1093*7c478bd9Sstevel@tonic-gate 		case RCM_STATE_SUSPENDING:
1094*7c478bd9Sstevel@tonic-gate 		case RCM_STATE_RESUMING:
1095*7c478bd9Sstevel@tonic-gate 			rsrcnames[0] = tmp->rsrcname;
1096*7c478bd9Sstevel@tonic-gate 			(void) notify_resource_resume(rsrcnames, tmp->pid, 0,
1097*7c478bd9Sstevel@tonic-gate 			    tmp->seq_num, NULL);
1098*7c478bd9Sstevel@tonic-gate 			break;
1099*7c478bd9Sstevel@tonic-gate 
1100*7c478bd9Sstevel@tonic-gate 		case RCM_STATE_REMOVING:
1101*7c478bd9Sstevel@tonic-gate 			rsrcnames[0] = tmp->rsrcname;
1102*7c478bd9Sstevel@tonic-gate 			(void) notify_resource_remove(rsrcnames, tmp->pid, 0,
1103*7c478bd9Sstevel@tonic-gate 			    tmp->seq_num, NULL);
1104*7c478bd9Sstevel@tonic-gate 			break;
1105*7c478bd9Sstevel@tonic-gate 
1106*7c478bd9Sstevel@tonic-gate 		default:
1107*7c478bd9Sstevel@tonic-gate 			rcm_log_message(RCM_WARNING,
1108*7c478bd9Sstevel@tonic-gate 			    gettext("%s in unknown state %d\n"),
1109*7c478bd9Sstevel@tonic-gate 			    tmp->rsrcname, tmp->state);
1110*7c478bd9Sstevel@tonic-gate 			break;
1111*7c478bd9Sstevel@tonic-gate 		}
1112*7c478bd9Sstevel@tonic-gate 		free(tmp->rsrcname);
1113*7c478bd9Sstevel@tonic-gate 		free(tmp);
1114*7c478bd9Sstevel@tonic-gate 	}
1115*7c478bd9Sstevel@tonic-gate }
1116*7c478bd9Sstevel@tonic-gate 
1117*7c478bd9Sstevel@tonic-gate /*
1118*7c478bd9Sstevel@tonic-gate  * Selected thread blocking based on event type
1119*7c478bd9Sstevel@tonic-gate  */
1120*7c478bd9Sstevel@tonic-gate barrier_t barrier;
1121*7c478bd9Sstevel@tonic-gate 
1122*7c478bd9Sstevel@tonic-gate /*
1123*7c478bd9Sstevel@tonic-gate  * Change barrier state:
1124*7c478bd9Sstevel@tonic-gate  *	RCMD_INIT - daemon is intializing, only register allowed
1125*7c478bd9Sstevel@tonic-gate  *	RCMD_NORMAL - normal daemon processing
1126*7c478bd9Sstevel@tonic-gate  *	RCMD_CLEANUP - cleanup thread is waiting or running
1127*7c478bd9Sstevel@tonic-gate  */
1128*7c478bd9Sstevel@tonic-gate int
rcmd_get_state()1129*7c478bd9Sstevel@tonic-gate rcmd_get_state()
1130*7c478bd9Sstevel@tonic-gate {
1131*7c478bd9Sstevel@tonic-gate 	return (barrier.state);
1132*7c478bd9Sstevel@tonic-gate }
1133*7c478bd9Sstevel@tonic-gate 
1134*7c478bd9Sstevel@tonic-gate void
rcmd_set_state(int state)1135*7c478bd9Sstevel@tonic-gate rcmd_set_state(int state)
1136*7c478bd9Sstevel@tonic-gate {
1137*7c478bd9Sstevel@tonic-gate 	/*
1138*7c478bd9Sstevel@tonic-gate 	 * The state transition is as follows:
1139*7c478bd9Sstevel@tonic-gate 	 *	INIT --> NORMAL <---> CLEANUP
1140*7c478bd9Sstevel@tonic-gate 	 * The implementation favors the cleanup thread
1141*7c478bd9Sstevel@tonic-gate 	 */
1142*7c478bd9Sstevel@tonic-gate 
1143*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&barrier.lock);
1144*7c478bd9Sstevel@tonic-gate 	barrier.state = state;
1145*7c478bd9Sstevel@tonic-gate 
1146*7c478bd9Sstevel@tonic-gate 	switch (state) {
1147*7c478bd9Sstevel@tonic-gate 	case RCMD_CLEANUP:
1148*7c478bd9Sstevel@tonic-gate 		/*
1149*7c478bd9Sstevel@tonic-gate 		 * Wait for existing threads to exit
1150*7c478bd9Sstevel@tonic-gate 		 */
1151*7c478bd9Sstevel@tonic-gate 		barrier.wanted++;
1152*7c478bd9Sstevel@tonic-gate 		while (barrier.thr_count != 0)
1153*7c478bd9Sstevel@tonic-gate 			(void) cond_wait(&barrier.cv, &barrier.lock);
1154*7c478bd9Sstevel@tonic-gate 		barrier.wanted--;
1155*7c478bd9Sstevel@tonic-gate 		barrier.thr_count = -1;
1156*7c478bd9Sstevel@tonic-gate 		break;
1157*7c478bd9Sstevel@tonic-gate 
1158*7c478bd9Sstevel@tonic-gate 	case RCMD_INIT:
1159*7c478bd9Sstevel@tonic-gate 	case RCMD_NORMAL:
1160*7c478bd9Sstevel@tonic-gate 	default:
1161*7c478bd9Sstevel@tonic-gate 		if (barrier.thr_count == -1)
1162*7c478bd9Sstevel@tonic-gate 			barrier.thr_count = 0;
1163*7c478bd9Sstevel@tonic-gate 		if (barrier.wanted)
1164*7c478bd9Sstevel@tonic-gate 			(void) cond_broadcast(&barrier.cv);
1165*7c478bd9Sstevel@tonic-gate 		break;
1166*7c478bd9Sstevel@tonic-gate 	}
1167*7c478bd9Sstevel@tonic-gate 
1168*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&barrier.lock);
1169*7c478bd9Sstevel@tonic-gate }
1170*7c478bd9Sstevel@tonic-gate 
1171*7c478bd9Sstevel@tonic-gate /*
1172*7c478bd9Sstevel@tonic-gate  * Increment daemon thread count
1173*7c478bd9Sstevel@tonic-gate  */
1174*7c478bd9Sstevel@tonic-gate int
rcmd_thr_incr(int cmd)1175*7c478bd9Sstevel@tonic-gate rcmd_thr_incr(int cmd)
1176*7c478bd9Sstevel@tonic-gate {
1177*7c478bd9Sstevel@tonic-gate 	int seq_num;
1178*7c478bd9Sstevel@tonic-gate 
1179*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&barrier.lock);
1180*7c478bd9Sstevel@tonic-gate 	/*
1181*7c478bd9Sstevel@tonic-gate 	 * Set wanted flag
1182*7c478bd9Sstevel@tonic-gate 	 */
1183*7c478bd9Sstevel@tonic-gate 	barrier.wanted++;
1184*7c478bd9Sstevel@tonic-gate 
1185*7c478bd9Sstevel@tonic-gate 	/*
1186*7c478bd9Sstevel@tonic-gate 	 * Wait till it is safe for daemon to perform the operation
1187*7c478bd9Sstevel@tonic-gate 	 *
1188*7c478bd9Sstevel@tonic-gate 	 * NOTE: if a module registers by passing a request to the
1189*7c478bd9Sstevel@tonic-gate 	 *	client proccess, we may need to allow register
1190*7c478bd9Sstevel@tonic-gate 	 *	to come through during daemon initialization.
1191*7c478bd9Sstevel@tonic-gate 	 */
1192*7c478bd9Sstevel@tonic-gate 	while (barrier.state != RCMD_NORMAL)
1193*7c478bd9Sstevel@tonic-gate 		(void) cond_wait(&barrier.cv, &barrier.lock);
1194*7c478bd9Sstevel@tonic-gate 
1195*7c478bd9Sstevel@tonic-gate 	if ((cmd == CMD_EVENT) ||
1196*7c478bd9Sstevel@tonic-gate 	    (cmd == CMD_REGISTER) ||
1197*7c478bd9Sstevel@tonic-gate 	    (cmd == CMD_UNREGISTER)) {
1198*7c478bd9Sstevel@tonic-gate 		/*
1199*7c478bd9Sstevel@tonic-gate 		 * Event passthru and register ops don't need sequence number
1200*7c478bd9Sstevel@tonic-gate 		 */
1201*7c478bd9Sstevel@tonic-gate 		seq_num = -1;
1202*7c478bd9Sstevel@tonic-gate 	} else {
1203*7c478bd9Sstevel@tonic-gate 		/*
1204*7c478bd9Sstevel@tonic-gate 		 * Non register operation gets a sequence number
1205*7c478bd9Sstevel@tonic-gate 		 */
1206*7c478bd9Sstevel@tonic-gate 		seq_num = get_seq_number();
1207*7c478bd9Sstevel@tonic-gate 	}
1208*7c478bd9Sstevel@tonic-gate 	barrier.wanted--;
1209*7c478bd9Sstevel@tonic-gate 	barrier.thr_count++;
1210*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&barrier.lock);
1211*7c478bd9Sstevel@tonic-gate 
1212*7c478bd9Sstevel@tonic-gate 	if ((cmd == CMD_OFFLINE) ||
1213*7c478bd9Sstevel@tonic-gate 	    (cmd == CMD_SUSPEND) ||
1214*7c478bd9Sstevel@tonic-gate 	    (cmd == CMD_GETINFO)) {
1215*7c478bd9Sstevel@tonic-gate 		/*
1216*7c478bd9Sstevel@tonic-gate 		 * For these operations, need to ask modules to
1217*7c478bd9Sstevel@tonic-gate 		 * register any new resources that came online.
1218*7c478bd9Sstevel@tonic-gate 		 *
1219*7c478bd9Sstevel@tonic-gate 		 * This is because mount/umount are not instrumented
1220*7c478bd9Sstevel@tonic-gate 		 * to register with rcm before using system resources.
1221*7c478bd9Sstevel@tonic-gate 		 * Certain registration ops may fail during sync, which
1222*7c478bd9Sstevel@tonic-gate 		 * indicates race conditions. This cannot be avoided
1223*7c478bd9Sstevel@tonic-gate 		 * without changing mount/umount.
1224*7c478bd9Sstevel@tonic-gate 		 */
1225*7c478bd9Sstevel@tonic-gate 		rcmd_db_sync();
1226*7c478bd9Sstevel@tonic-gate 	}
1227*7c478bd9Sstevel@tonic-gate 
1228*7c478bd9Sstevel@tonic-gate 	return (seq_num);
1229*7c478bd9Sstevel@tonic-gate }
1230*7c478bd9Sstevel@tonic-gate 
1231*7c478bd9Sstevel@tonic-gate /*
1232*7c478bd9Sstevel@tonic-gate  * Decrement thread count
1233*7c478bd9Sstevel@tonic-gate  */
1234*7c478bd9Sstevel@tonic-gate void
rcmd_thr_decr()1235*7c478bd9Sstevel@tonic-gate rcmd_thr_decr()
1236*7c478bd9Sstevel@tonic-gate {
1237*7c478bd9Sstevel@tonic-gate 	/*
1238*7c478bd9Sstevel@tonic-gate 	 * Decrement thread count and wake up reload/cleanup thread.
1239*7c478bd9Sstevel@tonic-gate 	 */
1240*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&barrier.lock);
1241*7c478bd9Sstevel@tonic-gate 	barrier.last_update = time(NULL);
1242*7c478bd9Sstevel@tonic-gate 	if (--barrier.thr_count == 0)
1243*7c478bd9Sstevel@tonic-gate 		(void) cond_broadcast(&barrier.cv);
1244*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&barrier.lock);
1245*7c478bd9Sstevel@tonic-gate }
1246*7c478bd9Sstevel@tonic-gate 
1247*7c478bd9Sstevel@tonic-gate /*
1248*7c478bd9Sstevel@tonic-gate  * Wakeup all waiting threads as a result of SIGHUP
1249*7c478bd9Sstevel@tonic-gate  */
1250*7c478bd9Sstevel@tonic-gate static int sighup_received = 0;
1251*7c478bd9Sstevel@tonic-gate 
1252*7c478bd9Sstevel@tonic-gate void
rcmd_thr_signal()1253*7c478bd9Sstevel@tonic-gate rcmd_thr_signal()
1254*7c478bd9Sstevel@tonic-gate {
1255*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&barrier.lock);
1256*7c478bd9Sstevel@tonic-gate 	sighup_received = 1;
1257*7c478bd9Sstevel@tonic-gate 	(void) cond_broadcast(&barrier.cv);
1258*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&barrier.lock);
1259*7c478bd9Sstevel@tonic-gate }
1260*7c478bd9Sstevel@tonic-gate 
1261*7c478bd9Sstevel@tonic-gate void
rcmd_start_timer(int timeout)1262*7c478bd9Sstevel@tonic-gate rcmd_start_timer(int timeout)
1263*7c478bd9Sstevel@tonic-gate {
1264*7c478bd9Sstevel@tonic-gate 	timestruc_t abstime;
1265*7c478bd9Sstevel@tonic-gate 
1266*7c478bd9Sstevel@tonic-gate 	if (timeout == 0)
1267*7c478bd9Sstevel@tonic-gate 		timeout = RCM_DAEMON_TIMEOUT;	/* default to 5 minutes */
1268*7c478bd9Sstevel@tonic-gate 	else
1269*7c478bd9Sstevel@tonic-gate 		dr_req_list->idle_timeout = timeout;	/* persist timeout */
1270*7c478bd9Sstevel@tonic-gate 
1271*7c478bd9Sstevel@tonic-gate 	if (timeout > 0) {
1272*7c478bd9Sstevel@tonic-gate 		abstime.tv_sec = time(NULL) + timeout;
1273*7c478bd9Sstevel@tonic-gate 	}
1274*7c478bd9Sstevel@tonic-gate 
1275*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&barrier.lock);
1276*7c478bd9Sstevel@tonic-gate 	for (;;) {
1277*7c478bd9Sstevel@tonic-gate 		int idletime;
1278*7c478bd9Sstevel@tonic-gate 		int is_active;
1279*7c478bd9Sstevel@tonic-gate 
1280*7c478bd9Sstevel@tonic-gate 		if (timeout > 0)
1281*7c478bd9Sstevel@tonic-gate 			(void) cond_timedwait(&barrier.cv, &barrier.lock,
1282*7c478bd9Sstevel@tonic-gate 			    &abstime);
1283*7c478bd9Sstevel@tonic-gate 		else
1284*7c478bd9Sstevel@tonic-gate 			(void) cond_wait(&barrier.cv, &barrier.lock);
1285*7c478bd9Sstevel@tonic-gate 
1286*7c478bd9Sstevel@tonic-gate 		/*
1287*7c478bd9Sstevel@tonic-gate 		 * If sighup received, change timeout to 0 so the daemon is
1288*7c478bd9Sstevel@tonic-gate 		 * shut down at the first possible moment
1289*7c478bd9Sstevel@tonic-gate 		 */
1290*7c478bd9Sstevel@tonic-gate 		if (sighup_received)
1291*7c478bd9Sstevel@tonic-gate 			timeout = 0;
1292*7c478bd9Sstevel@tonic-gate 
1293*7c478bd9Sstevel@tonic-gate 		/*
1294*7c478bd9Sstevel@tonic-gate 		 * If timeout is negative, never shutdown the daemon
1295*7c478bd9Sstevel@tonic-gate 		 */
1296*7c478bd9Sstevel@tonic-gate 		if (timeout < 0)
1297*7c478bd9Sstevel@tonic-gate 			continue;
1298*7c478bd9Sstevel@tonic-gate 
1299*7c478bd9Sstevel@tonic-gate 		/*
1300*7c478bd9Sstevel@tonic-gate 		 * Check for ongoing/pending activity
1301*7c478bd9Sstevel@tonic-gate 		 */
1302*7c478bd9Sstevel@tonic-gate 		is_active = (barrier.thr_count || barrier.wanted ||
1303*7c478bd9Sstevel@tonic-gate 		    (dr_req_list->n_req != 0));
1304*7c478bd9Sstevel@tonic-gate 		if (is_active) {
1305*7c478bd9Sstevel@tonic-gate 			abstime.tv_sec = time(NULL) + timeout;
1306*7c478bd9Sstevel@tonic-gate 			continue;
1307*7c478bd9Sstevel@tonic-gate 		}
1308*7c478bd9Sstevel@tonic-gate 
1309*7c478bd9Sstevel@tonic-gate 		/*
1310*7c478bd9Sstevel@tonic-gate 		 * If idletime is less than timeout, continue to wait
1311*7c478bd9Sstevel@tonic-gate 		 */
1312*7c478bd9Sstevel@tonic-gate 		idletime = time(NULL) - barrier.last_update;
1313*7c478bd9Sstevel@tonic-gate 		if (idletime < timeout) {
1314*7c478bd9Sstevel@tonic-gate 			abstime.tv_sec = barrier.last_update + timeout;
1315*7c478bd9Sstevel@tonic-gate 			continue;
1316*7c478bd9Sstevel@tonic-gate 		}
1317*7c478bd9Sstevel@tonic-gate 		break;
1318*7c478bd9Sstevel@tonic-gate 	}
1319*7c478bd9Sstevel@tonic-gate 
1320*7c478bd9Sstevel@tonic-gate 	(void) script_main_fini();
1321*7c478bd9Sstevel@tonic-gate 
1322*7c478bd9Sstevel@tonic-gate 	rcm_log_message(RCM_INFO, gettext("rcm_daemon is shut down.\n"));
1323*7c478bd9Sstevel@tonic-gate }
1324*7c478bd9Sstevel@tonic-gate 
1325*7c478bd9Sstevel@tonic-gate /*
1326*7c478bd9Sstevel@tonic-gate  * Code related to polling client pid's
1327*7c478bd9Sstevel@tonic-gate  * Not declared as static so that we can find this structure easily
1328*7c478bd9Sstevel@tonic-gate  * in the core file.
1329*7c478bd9Sstevel@tonic-gate  */
1330*7c478bd9Sstevel@tonic-gate struct {
1331*7c478bd9Sstevel@tonic-gate 	int		n_pids;
1332*7c478bd9Sstevel@tonic-gate 	int		n_max_pids;
1333*7c478bd9Sstevel@tonic-gate 	thread_t	poll_tid;	/* poll thread id */
1334*7c478bd9Sstevel@tonic-gate 	int		signaled;
1335*7c478bd9Sstevel@tonic-gate 	pid_t		*pids;
1336*7c478bd9Sstevel@tonic-gate 	int		*refcnt;
1337*7c478bd9Sstevel@tonic-gate 	struct pollfd	*fds;
1338*7c478bd9Sstevel@tonic-gate 	cond_t		cv;	/* the associated lock is rcm_req_lock */
1339*7c478bd9Sstevel@tonic-gate } polllist;
1340*7c478bd9Sstevel@tonic-gate 
1341*7c478bd9Sstevel@tonic-gate static int
find_pid_index(pid_t pid)1342*7c478bd9Sstevel@tonic-gate find_pid_index(pid_t pid)
1343*7c478bd9Sstevel@tonic-gate {
1344*7c478bd9Sstevel@tonic-gate 	int i;
1345*7c478bd9Sstevel@tonic-gate 
1346*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < polllist.n_pids; i++) {
1347*7c478bd9Sstevel@tonic-gate 		if (polllist.pids[i] == pid) {
1348*7c478bd9Sstevel@tonic-gate 			return (i);
1349*7c478bd9Sstevel@tonic-gate 		}
1350*7c478bd9Sstevel@tonic-gate 	}
1351*7c478bd9Sstevel@tonic-gate 	return (-1);
1352*7c478bd9Sstevel@tonic-gate }
1353*7c478bd9Sstevel@tonic-gate 
1354*7c478bd9Sstevel@tonic-gate /*
1355*7c478bd9Sstevel@tonic-gate  * Resize buffer for new pids
1356*7c478bd9Sstevel@tonic-gate  */
1357*7c478bd9Sstevel@tonic-gate static int
get_pid_index()1358*7c478bd9Sstevel@tonic-gate get_pid_index()
1359*7c478bd9Sstevel@tonic-gate {
1360*7c478bd9Sstevel@tonic-gate 	const int n_chunk = 10;
1361*7c478bd9Sstevel@tonic-gate 
1362*7c478bd9Sstevel@tonic-gate 	int n_max;
1363*7c478bd9Sstevel@tonic-gate 	int index = polllist.n_pids;
1364*7c478bd9Sstevel@tonic-gate 
1365*7c478bd9Sstevel@tonic-gate 	if (polllist.n_pids < polllist.n_max_pids) {
1366*7c478bd9Sstevel@tonic-gate 		polllist.n_pids++;
1367*7c478bd9Sstevel@tonic-gate 		return (index);
1368*7c478bd9Sstevel@tonic-gate 	}
1369*7c478bd9Sstevel@tonic-gate 
1370*7c478bd9Sstevel@tonic-gate 	if (polllist.n_max_pids == 0) {
1371*7c478bd9Sstevel@tonic-gate 		n_max = n_chunk;
1372*7c478bd9Sstevel@tonic-gate 		polllist.pids = s_calloc(n_max, sizeof (pid_t));
1373*7c478bd9Sstevel@tonic-gate 		polllist.refcnt = s_calloc(n_max, sizeof (int));
1374*7c478bd9Sstevel@tonic-gate 		polllist.fds = s_calloc(n_max, sizeof (struct pollfd));
1375*7c478bd9Sstevel@tonic-gate 	} else {
1376*7c478bd9Sstevel@tonic-gate 		n_max = polllist.n_max_pids + n_chunk;
1377*7c478bd9Sstevel@tonic-gate 		polllist.pids = s_realloc(polllist.pids,
1378*7c478bd9Sstevel@tonic-gate 		    n_max * sizeof (pid_t));
1379*7c478bd9Sstevel@tonic-gate 		polllist.refcnt = s_realloc(polllist.refcnt,
1380*7c478bd9Sstevel@tonic-gate 		    n_max * sizeof (int));
1381*7c478bd9Sstevel@tonic-gate 		polllist.fds = s_realloc(polllist.fds,
1382*7c478bd9Sstevel@tonic-gate 		    n_max * sizeof (struct pollfd));
1383*7c478bd9Sstevel@tonic-gate 	}
1384*7c478bd9Sstevel@tonic-gate 	polllist.n_max_pids = n_max;
1385*7c478bd9Sstevel@tonic-gate 	polllist.n_pids++;
1386*7c478bd9Sstevel@tonic-gate 	return (index);
1387*7c478bd9Sstevel@tonic-gate }
1388*7c478bd9Sstevel@tonic-gate 
1389*7c478bd9Sstevel@tonic-gate /*
1390*7c478bd9Sstevel@tonic-gate  * rcm_req_lock must be held
1391*7c478bd9Sstevel@tonic-gate  */
1392*7c478bd9Sstevel@tonic-gate static void
add_to_polling_list(pid_t pid)1393*7c478bd9Sstevel@tonic-gate add_to_polling_list(pid_t pid)
1394*7c478bd9Sstevel@tonic-gate {
1395*7c478bd9Sstevel@tonic-gate 	int fd, index;
1396*7c478bd9Sstevel@tonic-gate 	char procfile[MAXPATHLEN];
1397*7c478bd9Sstevel@tonic-gate 
1398*7c478bd9Sstevel@tonic-gate 	if (pid == (pid_t)0)
1399*7c478bd9Sstevel@tonic-gate 		return;
1400*7c478bd9Sstevel@tonic-gate 
1401*7c478bd9Sstevel@tonic-gate 	rcm_log_message(RCM_TRACE1, "add_to_polling_list(%ld)\n", pid);
1402*7c478bd9Sstevel@tonic-gate 
1403*7c478bd9Sstevel@tonic-gate 	/*
1404*7c478bd9Sstevel@tonic-gate 	 * Need to stop the poll thread before manipulating the polllist
1405*7c478bd9Sstevel@tonic-gate 	 * since poll thread may possibly be using polllist.fds[] and
1406*7c478bd9Sstevel@tonic-gate 	 * polllist.n_pids. As an optimization, first check if the pid
1407*7c478bd9Sstevel@tonic-gate 	 * is already in the polllist. If it is, there is no need to
1408*7c478bd9Sstevel@tonic-gate 	 * stop the poll thread. Just increment the pid reference count
1409*7c478bd9Sstevel@tonic-gate 	 * and return;
1410*7c478bd9Sstevel@tonic-gate 	 */
1411*7c478bd9Sstevel@tonic-gate 	index = find_pid_index(pid);
1412*7c478bd9Sstevel@tonic-gate 	if (index != -1) {
1413*7c478bd9Sstevel@tonic-gate 		polllist.refcnt[index]++;
1414*7c478bd9Sstevel@tonic-gate 		return;
1415*7c478bd9Sstevel@tonic-gate 	}
1416*7c478bd9Sstevel@tonic-gate 
1417*7c478bd9Sstevel@tonic-gate 	stop_polling_thread();
1418*7c478bd9Sstevel@tonic-gate 
1419*7c478bd9Sstevel@tonic-gate 	/*
1420*7c478bd9Sstevel@tonic-gate 	 * In an attempt to stop the poll thread we may have released
1421*7c478bd9Sstevel@tonic-gate 	 * and reacquired rcm_req_lock. So find the index again.
1422*7c478bd9Sstevel@tonic-gate 	 */
1423*7c478bd9Sstevel@tonic-gate 	index = find_pid_index(pid);
1424*7c478bd9Sstevel@tonic-gate 	if (index != -1) {
1425*7c478bd9Sstevel@tonic-gate 		polllist.refcnt[index]++;
1426*7c478bd9Sstevel@tonic-gate 		goto done;
1427*7c478bd9Sstevel@tonic-gate 	}
1428*7c478bd9Sstevel@tonic-gate 
1429*7c478bd9Sstevel@tonic-gate 	/*
1430*7c478bd9Sstevel@tonic-gate 	 * Open a /proc file
1431*7c478bd9Sstevel@tonic-gate 	 */
1432*7c478bd9Sstevel@tonic-gate 	(void) sprintf(procfile, "/proc/%ld/as", pid);
1433*7c478bd9Sstevel@tonic-gate 	if ((fd = open(procfile, O_RDONLY)) == -1) {
1434*7c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_NOTICE, gettext("open(%s): %s\n"),
1435*7c478bd9Sstevel@tonic-gate 		    procfile, strerror(errno));
1436*7c478bd9Sstevel@tonic-gate 		goto done;
1437*7c478bd9Sstevel@tonic-gate 	}
1438*7c478bd9Sstevel@tonic-gate 
1439*7c478bd9Sstevel@tonic-gate 	/*
1440*7c478bd9Sstevel@tonic-gate 	 * add pid to polllist
1441*7c478bd9Sstevel@tonic-gate 	 */
1442*7c478bd9Sstevel@tonic-gate 	index = get_pid_index();
1443*7c478bd9Sstevel@tonic-gate 	polllist.pids[index] = pid;
1444*7c478bd9Sstevel@tonic-gate 	polllist.refcnt[index] = 1;
1445*7c478bd9Sstevel@tonic-gate 	polllist.fds[index].fd = fd;
1446*7c478bd9Sstevel@tonic-gate 	polllist.fds[index].events = 0;
1447*7c478bd9Sstevel@tonic-gate 	polllist.fds[index].revents = 0;
1448*7c478bd9Sstevel@tonic-gate 
1449*7c478bd9Sstevel@tonic-gate 	rcm_log_message(RCM_DEBUG, "add pid %ld at index %ld\n", pid, index);
1450*7c478bd9Sstevel@tonic-gate 
1451*7c478bd9Sstevel@tonic-gate done:
1452*7c478bd9Sstevel@tonic-gate 	start_polling_thread();
1453*7c478bd9Sstevel@tonic-gate }
1454*7c478bd9Sstevel@tonic-gate 
1455*7c478bd9Sstevel@tonic-gate /*
1456*7c478bd9Sstevel@tonic-gate  * rcm_req_lock must be held
1457*7c478bd9Sstevel@tonic-gate  */
1458*7c478bd9Sstevel@tonic-gate static void
remove_from_polling_list(pid_t pid)1459*7c478bd9Sstevel@tonic-gate remove_from_polling_list(pid_t pid)
1460*7c478bd9Sstevel@tonic-gate {
1461*7c478bd9Sstevel@tonic-gate 	int i, index;
1462*7c478bd9Sstevel@tonic-gate 
1463*7c478bd9Sstevel@tonic-gate 	if (pid == (pid_t)0)
1464*7c478bd9Sstevel@tonic-gate 		return;
1465*7c478bd9Sstevel@tonic-gate 
1466*7c478bd9Sstevel@tonic-gate 	rcm_log_message(RCM_TRACE1, "remove_from_polling_list(%ld)\n", pid);
1467*7c478bd9Sstevel@tonic-gate 
1468*7c478bd9Sstevel@tonic-gate 	/*
1469*7c478bd9Sstevel@tonic-gate 	 * Need to stop the poll thread before manipulating the polllist
1470*7c478bd9Sstevel@tonic-gate 	 * since poll thread may possibly be using polllist.fds[] and
1471*7c478bd9Sstevel@tonic-gate 	 * polllist.n_pids. As an optimization, first check the pid
1472*7c478bd9Sstevel@tonic-gate 	 * reference count. If the pid reference count is greater than 1
1473*7c478bd9Sstevel@tonic-gate 	 * there is no need to stop the polling thread.
1474*7c478bd9Sstevel@tonic-gate 	 */
1475*7c478bd9Sstevel@tonic-gate 
1476*7c478bd9Sstevel@tonic-gate 	index = find_pid_index(pid);
1477*7c478bd9Sstevel@tonic-gate 	if (index == -1) {
1478*7c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_NOTICE,
1479*7c478bd9Sstevel@tonic-gate 		    gettext("error removing pid %ld from polling list\n"), pid);
1480*7c478bd9Sstevel@tonic-gate 		return;
1481*7c478bd9Sstevel@tonic-gate 	}
1482*7c478bd9Sstevel@tonic-gate 
1483*7c478bd9Sstevel@tonic-gate 	/*
1484*7c478bd9Sstevel@tonic-gate 	 * decrement the pid refcnt
1485*7c478bd9Sstevel@tonic-gate 	 */
1486*7c478bd9Sstevel@tonic-gate 	if (polllist.refcnt[index] > 1) {
1487*7c478bd9Sstevel@tonic-gate 		polllist.refcnt[index]--;
1488*7c478bd9Sstevel@tonic-gate 		return;
1489*7c478bd9Sstevel@tonic-gate 	}
1490*7c478bd9Sstevel@tonic-gate 
1491*7c478bd9Sstevel@tonic-gate 	stop_polling_thread();
1492*7c478bd9Sstevel@tonic-gate 
1493*7c478bd9Sstevel@tonic-gate 	/*
1494*7c478bd9Sstevel@tonic-gate 	 * In an attempt to stop the poll thread we may have released
1495*7c478bd9Sstevel@tonic-gate 	 * and reacquired rcm_req_lock. So find the index again.
1496*7c478bd9Sstevel@tonic-gate 	 */
1497*7c478bd9Sstevel@tonic-gate 	index = find_pid_index(pid);
1498*7c478bd9Sstevel@tonic-gate 	if (index == -1) {
1499*7c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_NOTICE,
1500*7c478bd9Sstevel@tonic-gate 		    gettext("error removing pid %ld from polling list\n"), pid);
1501*7c478bd9Sstevel@tonic-gate 		goto done;
1502*7c478bd9Sstevel@tonic-gate 	}
1503*7c478bd9Sstevel@tonic-gate 
1504*7c478bd9Sstevel@tonic-gate 	if (--polllist.refcnt[index] > 0)
1505*7c478bd9Sstevel@tonic-gate 		goto done;
1506*7c478bd9Sstevel@tonic-gate 
1507*7c478bd9Sstevel@tonic-gate 	/*
1508*7c478bd9Sstevel@tonic-gate 	 * refcnt down to zero, delete pid from polling list
1509*7c478bd9Sstevel@tonic-gate 	 */
1510*7c478bd9Sstevel@tonic-gate 	(void) close(polllist.fds[index].fd);
1511*7c478bd9Sstevel@tonic-gate 	polllist.n_pids--;
1512*7c478bd9Sstevel@tonic-gate 
1513*7c478bd9Sstevel@tonic-gate 	for (i = index; i < polllist.n_pids; i++) {
1514*7c478bd9Sstevel@tonic-gate 		polllist.pids[i] = polllist.pids[i + 1];
1515*7c478bd9Sstevel@tonic-gate 		polllist.refcnt[i] = polllist.refcnt[i + 1];
1516*7c478bd9Sstevel@tonic-gate 		bcopy(&polllist.fds[i + 1], &polllist.fds[i],
1517*7c478bd9Sstevel@tonic-gate 		    sizeof (struct pollfd));
1518*7c478bd9Sstevel@tonic-gate 	}
1519*7c478bd9Sstevel@tonic-gate 
1520*7c478bd9Sstevel@tonic-gate 	rcm_log_message(RCM_DEBUG, "remove pid %ld at index %d\n", pid, index);
1521*7c478bd9Sstevel@tonic-gate 
1522*7c478bd9Sstevel@tonic-gate done:
1523*7c478bd9Sstevel@tonic-gate 	start_polling_thread();
1524*7c478bd9Sstevel@tonic-gate }
1525*7c478bd9Sstevel@tonic-gate 
1526*7c478bd9Sstevel@tonic-gate void
init_poll_thread()1527*7c478bd9Sstevel@tonic-gate init_poll_thread()
1528*7c478bd9Sstevel@tonic-gate {
1529*7c478bd9Sstevel@tonic-gate 	polllist.poll_tid = (thread_t)-1;
1530*7c478bd9Sstevel@tonic-gate }
1531*7c478bd9Sstevel@tonic-gate 
1532*7c478bd9Sstevel@tonic-gate void
cleanup_poll_thread()1533*7c478bd9Sstevel@tonic-gate cleanup_poll_thread()
1534*7c478bd9Sstevel@tonic-gate {
1535*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&rcm_req_lock);
1536*7c478bd9Sstevel@tonic-gate 	if (polllist.poll_tid == thr_self()) {
1537*7c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_TRACE2,
1538*7c478bd9Sstevel@tonic-gate 		    "cleanup_poll_thread: n_pids = %d\n", polllist.n_pids);
1539*7c478bd9Sstevel@tonic-gate 		polllist.poll_tid = (thread_t)-1;
1540*7c478bd9Sstevel@tonic-gate 		(void) cond_broadcast(&polllist.cv);
1541*7c478bd9Sstevel@tonic-gate 	}
1542*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&rcm_req_lock);
1543*7c478bd9Sstevel@tonic-gate }
1544*7c478bd9Sstevel@tonic-gate 
1545*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1546*7c478bd9Sstevel@tonic-gate static void *
pollfunc(void * arg)1547*7c478bd9Sstevel@tonic-gate pollfunc(void *arg)
1548*7c478bd9Sstevel@tonic-gate {
1549*7c478bd9Sstevel@tonic-gate 	sigset_t mask;
1550*7c478bd9Sstevel@tonic-gate 
1551*7c478bd9Sstevel@tonic-gate 	rcm_log_message(RCM_TRACE2, "poll thread started. n_pids = %d\n",
1552*7c478bd9Sstevel@tonic-gate 	    polllist.n_pids);
1553*7c478bd9Sstevel@tonic-gate 
1554*7c478bd9Sstevel@tonic-gate 	/*
1555*7c478bd9Sstevel@tonic-gate 	 * Unblock SIGUSR1 to allow polling thread to be killed
1556*7c478bd9Sstevel@tonic-gate 	 */
1557*7c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&mask);
1558*7c478bd9Sstevel@tonic-gate 	(void) sigaddset(&mask, SIGUSR1);
1559*7c478bd9Sstevel@tonic-gate 	(void) thr_sigsetmask(SIG_UNBLOCK, &mask, NULL);
1560*7c478bd9Sstevel@tonic-gate 
1561*7c478bd9Sstevel@tonic-gate 	(void) poll(polllist.fds, polllist.n_pids, (time_t)-1);
1562*7c478bd9Sstevel@tonic-gate 
1563*7c478bd9Sstevel@tonic-gate 	/*
1564*7c478bd9Sstevel@tonic-gate 	 * block SIGUSR1 to avoid being killed while holding a lock
1565*7c478bd9Sstevel@tonic-gate 	 */
1566*7c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&mask);
1567*7c478bd9Sstevel@tonic-gate 	(void) sigaddset(&mask, SIGUSR1);
1568*7c478bd9Sstevel@tonic-gate 	(void) thr_sigsetmask(SIG_BLOCK, &mask, NULL);
1569*7c478bd9Sstevel@tonic-gate 
1570*7c478bd9Sstevel@tonic-gate 	rcm_log_message(RCM_TRACE2, "returned from poll()\n");
1571*7c478bd9Sstevel@tonic-gate 
1572*7c478bd9Sstevel@tonic-gate 	cleanup_poll_thread();
1573*7c478bd9Sstevel@tonic-gate 
1574*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&barrier.lock);
1575*7c478bd9Sstevel@tonic-gate 	need_cleanup = 1;
1576*7c478bd9Sstevel@tonic-gate 	(void) cond_broadcast(&barrier.cv);
1577*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&barrier.lock);
1578*7c478bd9Sstevel@tonic-gate 
1579*7c478bd9Sstevel@tonic-gate 	return (NULL);
1580*7c478bd9Sstevel@tonic-gate }
1581*7c478bd9Sstevel@tonic-gate 
1582*7c478bd9Sstevel@tonic-gate /*
1583*7c478bd9Sstevel@tonic-gate  * rcm_req_lock must be held
1584*7c478bd9Sstevel@tonic-gate  */
1585*7c478bd9Sstevel@tonic-gate void
start_polling_thread()1586*7c478bd9Sstevel@tonic-gate start_polling_thread()
1587*7c478bd9Sstevel@tonic-gate {
1588*7c478bd9Sstevel@tonic-gate 	int err;
1589*7c478bd9Sstevel@tonic-gate 
1590*7c478bd9Sstevel@tonic-gate 	if (rcmd_get_state() != RCMD_NORMAL)
1591*7c478bd9Sstevel@tonic-gate 		return;
1592*7c478bd9Sstevel@tonic-gate 
1593*7c478bd9Sstevel@tonic-gate 	if (polllist.poll_tid != (thread_t)-1 || polllist.n_pids == 0)
1594*7c478bd9Sstevel@tonic-gate 		return;
1595*7c478bd9Sstevel@tonic-gate 
1596*7c478bd9Sstevel@tonic-gate 	if ((err = thr_create(NULL, 0, pollfunc, NULL, THR_DETACHED,
1597*7c478bd9Sstevel@tonic-gate 	    &polllist.poll_tid)) == 0)
1598*7c478bd9Sstevel@tonic-gate 		polllist.signaled = 0;
1599*7c478bd9Sstevel@tonic-gate 	else
1600*7c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR,
1601*7c478bd9Sstevel@tonic-gate 		    gettext("failed to create polling thread: %s\n"),
1602*7c478bd9Sstevel@tonic-gate 		    strerror(err));
1603*7c478bd9Sstevel@tonic-gate }
1604*7c478bd9Sstevel@tonic-gate 
1605*7c478bd9Sstevel@tonic-gate /*
1606*7c478bd9Sstevel@tonic-gate  * rcm_req_lock must be held
1607*7c478bd9Sstevel@tonic-gate  */
1608*7c478bd9Sstevel@tonic-gate static void
stop_polling_thread()1609*7c478bd9Sstevel@tonic-gate stop_polling_thread()
1610*7c478bd9Sstevel@tonic-gate {
1611*7c478bd9Sstevel@tonic-gate 	int err;
1612*7c478bd9Sstevel@tonic-gate 
1613*7c478bd9Sstevel@tonic-gate 	while (polllist.poll_tid != (thread_t)-1) {
1614*7c478bd9Sstevel@tonic-gate 		if (polllist.signaled == 0) {
1615*7c478bd9Sstevel@tonic-gate 			if ((err = thr_kill(polllist.poll_tid, SIGUSR1)) == 0)
1616*7c478bd9Sstevel@tonic-gate 				polllist.signaled = 1;
1617*7c478bd9Sstevel@tonic-gate 			else
1618*7c478bd9Sstevel@tonic-gate 				/*
1619*7c478bd9Sstevel@tonic-gate 				 * thr_kill shouldn't have failed since the
1620*7c478bd9Sstevel@tonic-gate 				 * poll thread id and the signal are valid.
1621*7c478bd9Sstevel@tonic-gate 				 * So log an error. Since when thr_kill
1622*7c478bd9Sstevel@tonic-gate 				 * fails no signal is sent (as per man page),
1623*7c478bd9Sstevel@tonic-gate 				 * the cond_wait below will wait until the
1624*7c478bd9Sstevel@tonic-gate 				 * the poll thread exits by some other means.
1625*7c478bd9Sstevel@tonic-gate 				 * The poll thread, for example, exits on its
1626*7c478bd9Sstevel@tonic-gate 				 * own when any DR initiator process that it
1627*7c478bd9Sstevel@tonic-gate 				 * is currently polling exits.
1628*7c478bd9Sstevel@tonic-gate 				 */
1629*7c478bd9Sstevel@tonic-gate 				rcm_log_message(RCM_ERROR,
1630*7c478bd9Sstevel@tonic-gate 				    gettext(
1631*7c478bd9Sstevel@tonic-gate 				    "fail to kill polling thread %d: %s\n"),
1632*7c478bd9Sstevel@tonic-gate 				    polllist.poll_tid, strerror(err));
1633*7c478bd9Sstevel@tonic-gate 		}
1634*7c478bd9Sstevel@tonic-gate 		(void) cond_wait(&polllist.cv, &rcm_req_lock);
1635*7c478bd9Sstevel@tonic-gate 	}
1636*7c478bd9Sstevel@tonic-gate }
1637