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