xref: /onnv-gate/usr/src/lib/lvm/libmeta/common/meta_setup.c (revision 0:68f95e015346)
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 /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * Just in case we're not in a build environment, make sure that
31*0Sstevel@tonic-gate  * TEXT_DOMAIN gets set to something.
32*0Sstevel@tonic-gate  */
33*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
34*0Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
35*0Sstevel@tonic-gate #endif
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate /*
38*0Sstevel@tonic-gate  * setup utility
39*0Sstevel@tonic-gate  */
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #include "meta_set_prv.h"
42*0Sstevel@tonic-gate #include <sys/resource.h>
43*0Sstevel@tonic-gate #include <syslog.h>
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate /* globals */
47*0Sstevel@tonic-gate char		*myname = "";
48*0Sstevel@tonic-gate FILE		*metalogfp = NULL;
49*0Sstevel@tonic-gate int		metasyslog = 0;
50*0Sstevel@tonic-gate uint_t		verbosity = 0;
51*0Sstevel@tonic-gate hrtime_t	start_time = 0;
52*0Sstevel@tonic-gate sigset_t	allsigs;
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate /* locals */
55*0Sstevel@tonic-gate static	int	rb_signal_handling = FALSE;
56*0Sstevel@tonic-gate static	int	rb_signal_caught = FALSE;
57*0Sstevel@tonic-gate static	int	rb_signal_which = 0;
58*0Sstevel@tonic-gate static	size_t	metansig = 0;
59*0Sstevel@tonic-gate static	struct	sigaction	*metahandlers = NULL;
60*0Sstevel@tonic-gate #ifdef	_DEBUG_MALLOC_INC
61*0Sstevel@tonic-gate static	ulong_t	malloc_histid_begin;
62*0Sstevel@tonic-gate static	ulong_t	malloc_histid_end;
63*0Sstevel@tonic-gate static	ulong_t	malloc_inuse_begin;
64*0Sstevel@tonic-gate static	ulong_t	malloc_inuse_end;
65*0Sstevel@tonic-gate #endif	/* _DEBUG_MALLOC_INC */
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate /* forwards */
68*0Sstevel@tonic-gate static	void	md_catcher(int sig);
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate /*
71*0Sstevel@tonic-gate  * push/pop signal handlers
72*0Sstevel@tonic-gate  */
73*0Sstevel@tonic-gate static int
md_pushsig(unsigned sig,void (* handler)(int sig),md_error_t * ep)74*0Sstevel@tonic-gate md_pushsig(
75*0Sstevel@tonic-gate 	unsigned	sig,
76*0Sstevel@tonic-gate 	void		(*handler)(int sig),
77*0Sstevel@tonic-gate 	md_error_t	*ep
78*0Sstevel@tonic-gate )
79*0Sstevel@tonic-gate {
80*0Sstevel@tonic-gate 	struct	sigaction	newhandler;
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate 	/* expand vector as neccessary */
83*0Sstevel@tonic-gate 	if (sig >= metansig) {
84*0Sstevel@tonic-gate 		if (metahandlers == NULL) {
85*0Sstevel@tonic-gate 			metahandlers = Zalloc(
86*0Sstevel@tonic-gate 			    (sig + 1) * sizeof (metahandlers[0]));
87*0Sstevel@tonic-gate 		} else {
88*0Sstevel@tonic-gate 			metahandlers = Realloc(metahandlers,
89*0Sstevel@tonic-gate 			    ((sig + 1) * sizeof (metahandlers[0])));
90*0Sstevel@tonic-gate 			(void) memset(&metahandlers[metansig], 0,
91*0Sstevel@tonic-gate 			    ((sig - metansig) * sizeof (metahandlers[0])));
92*0Sstevel@tonic-gate 		}
93*0Sstevel@tonic-gate 		metansig = sig;
94*0Sstevel@tonic-gate 	}
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 	/* We need to have a seperate stack to handle rollback properly */
97*0Sstevel@tonic-gate 	newhandler.sa_flags = 0;
98*0Sstevel@tonic-gate 	if (sigfillset(&newhandler.sa_mask) < 0)
99*0Sstevel@tonic-gate 		return (mdsyserror(ep, errno,
100*0Sstevel@tonic-gate 		    "sigfillset(&newhandler.sa_mask)"));
101*0Sstevel@tonic-gate 	newhandler.sa_handler = handler;
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 	/* push handler */
104*0Sstevel@tonic-gate 	if (sigaction(sig, &newhandler, &metahandlers[sig]) < 0)
105*0Sstevel@tonic-gate 		return (mdsyserror(ep, errno, "sigaction(&newhandler)"));
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 	/* return success */
108*0Sstevel@tonic-gate 	return (0);
109*0Sstevel@tonic-gate }
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate static int
md_popsig(unsigned sig,md_error_t * ep)112*0Sstevel@tonic-gate md_popsig(
113*0Sstevel@tonic-gate 	unsigned	sig,
114*0Sstevel@tonic-gate 	md_error_t	*ep
115*0Sstevel@tonic-gate )
116*0Sstevel@tonic-gate {
117*0Sstevel@tonic-gate 	/* can't pop what isn't pushed */
118*0Sstevel@tonic-gate 	assert(sig <= metansig);
119*0Sstevel@tonic-gate 	assert(metahandlers[sig].sa_handler != md_catcher);
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	/* pop handler */
122*0Sstevel@tonic-gate 	if (sigaction(sig, &metahandlers[sig], NULL) < 0)
123*0Sstevel@tonic-gate 		return (mdsyserror(ep, errno, "sigaction(&metahandlers)"));
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 	/* return success */
126*0Sstevel@tonic-gate 	return (0);
127*0Sstevel@tonic-gate }
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate char *
meta_lock_name(set_t setno)130*0Sstevel@tonic-gate meta_lock_name(
131*0Sstevel@tonic-gate 	set_t	setno
132*0Sstevel@tonic-gate )
133*0Sstevel@tonic-gate {
134*0Sstevel@tonic-gate 	char	lockname[30];
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 	if (setno == MD_LOCAL_SET)
137*0Sstevel@tonic-gate 		return (strdup(METALOCK));
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 	(void) snprintf(lockname, sizeof (lockname), "%s.%ld", METALOCK, setno);
140*0Sstevel@tonic-gate 	return (strdup(lockname));
141*0Sstevel@tonic-gate }
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate #define	META_LOCK_FD(sp)	((sp)->lockfd)
144*0Sstevel@tonic-gate #define	META_LOCK_NAME(sp)	(meta_lock_name((sp)->setno))
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate /*
147*0Sstevel@tonic-gate  * open lock
148*0Sstevel@tonic-gate  */
149*0Sstevel@tonic-gate static int
meta_lock_open(mdsetname_t * sp,md_error_t * ep)150*0Sstevel@tonic-gate meta_lock_open(
151*0Sstevel@tonic-gate 	mdsetname_t	*sp,
152*0Sstevel@tonic-gate 	md_error_t	*ep
153*0Sstevel@tonic-gate )
154*0Sstevel@tonic-gate {
155*0Sstevel@tonic-gate 	int	lockfd = META_LOCK_FD(sp);
156*0Sstevel@tonic-gate 	char	*lockname = META_LOCK_NAME(sp);
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 	/* check for already open */
159*0Sstevel@tonic-gate 	if (lockfd >= 0)
160*0Sstevel@tonic-gate 		goto success;
161*0Sstevel@tonic-gate 	assert(lockfd == MD_NO_LOCK);
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 	/* open and/or create lock file */
164*0Sstevel@tonic-gate 	if ((lockfd = open(lockname, O_WRONLY, 0)) < 0) {
165*0Sstevel@tonic-gate 		if (errno == EROFS) {
166*0Sstevel@tonic-gate 			lockfd = MD_NO_LOCK;
167*0Sstevel@tonic-gate 			goto success;
168*0Sstevel@tonic-gate 		}
169*0Sstevel@tonic-gate 		if (errno != ENOENT) {
170*0Sstevel@tonic-gate 			(void) mdsyserror(ep, errno, lockname);
171*0Sstevel@tonic-gate 			goto failure;
172*0Sstevel@tonic-gate 		}
173*0Sstevel@tonic-gate 		if ((lockfd = open(lockname, (O_WRONLY|O_CREAT),
174*0Sstevel@tonic-gate 		    0644)) < 0) {
175*0Sstevel@tonic-gate 			(void) mdsyserror(ep, errno, lockname);
176*0Sstevel@tonic-gate 			goto failure;
177*0Sstevel@tonic-gate 		}
178*0Sstevel@tonic-gate 		if (fchmod(lockfd, 0644) != 0) {
179*0Sstevel@tonic-gate 			(void) mdsyserror(ep, errno, lockname);
180*0Sstevel@tonic-gate 			goto failure;
181*0Sstevel@tonic-gate 		}
182*0Sstevel@tonic-gate 	}
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 	/* return success */
185*0Sstevel@tonic-gate success:
186*0Sstevel@tonic-gate 	if (lockname != NULL)
187*0Sstevel@tonic-gate 		free(lockname);
188*0Sstevel@tonic-gate 	META_LOCK_FD(sp) = lockfd;
189*0Sstevel@tonic-gate 	return (0);
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 	/* flag failure */
192*0Sstevel@tonic-gate failure:
193*0Sstevel@tonic-gate 	if (lockname != NULL)
194*0Sstevel@tonic-gate 		free(lockname);
195*0Sstevel@tonic-gate 	if (lockfd >= 0)
196*0Sstevel@tonic-gate 		(void) close(lockfd);
197*0Sstevel@tonic-gate 	return (-1);
198*0Sstevel@tonic-gate }
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate static int
meta_lock_close(mdsetname_t * sp,md_error_t * ep)201*0Sstevel@tonic-gate meta_lock_close(
202*0Sstevel@tonic-gate 	mdsetname_t	*sp,
203*0Sstevel@tonic-gate 	md_error_t	*ep
204*0Sstevel@tonic-gate )
205*0Sstevel@tonic-gate {
206*0Sstevel@tonic-gate 	int	retval = 0;
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 	if (close(META_LOCK_FD(sp)) != 0) {
209*0Sstevel@tonic-gate 		if (ep != NULL) {
210*0Sstevel@tonic-gate 			char	*lockname = META_LOCK_NAME(sp);
211*0Sstevel@tonic-gate 			(void) mdsyserror(ep, errno, lockname);
212*0Sstevel@tonic-gate 			if (lockname != NULL)
213*0Sstevel@tonic-gate 				free(lockname);
214*0Sstevel@tonic-gate 		}
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate 		retval = -1;
217*0Sstevel@tonic-gate 	}
218*0Sstevel@tonic-gate 	META_LOCK_FD(sp) = MD_NO_LOCK;
219*0Sstevel@tonic-gate 	return (retval);
220*0Sstevel@tonic-gate }
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate /*
223*0Sstevel@tonic-gate  * unlock
224*0Sstevel@tonic-gate  */
225*0Sstevel@tonic-gate int
meta_unlock(mdsetname_t * sp,md_error_t * ep)226*0Sstevel@tonic-gate meta_unlock(
227*0Sstevel@tonic-gate 	mdsetname_t	*sp,
228*0Sstevel@tonic-gate 	md_error_t	*ep
229*0Sstevel@tonic-gate )
230*0Sstevel@tonic-gate {
231*0Sstevel@tonic-gate 	int	lockfd = META_LOCK_FD(sp);
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate 	/* ignore read-only filesystem */
234*0Sstevel@tonic-gate 	if (lockfd == MD_NO_LOCK)
235*0Sstevel@tonic-gate 		return (0);
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate 	assert(lockfd >= 0);
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate 	/* unlock and discard */
240*0Sstevel@tonic-gate 	if (lockf(lockfd, F_ULOCK, 0) != 0) {
241*0Sstevel@tonic-gate 		(void) mdsyserror(ep, errno, METALOCK);
242*0Sstevel@tonic-gate 		(void) meta_lock_close(sp, NULL);
243*0Sstevel@tonic-gate 		return (-1);
244*0Sstevel@tonic-gate 	}
245*0Sstevel@tonic-gate 	return (meta_lock_close(sp, ep));
246*0Sstevel@tonic-gate }
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate /*
249*0Sstevel@tonic-gate  * lock
250*0Sstevel@tonic-gate  */
251*0Sstevel@tonic-gate int
meta_lock(mdsetname_t * sp,int print_status,md_error_t * ep)252*0Sstevel@tonic-gate meta_lock(
253*0Sstevel@tonic-gate 	mdsetname_t	*sp,
254*0Sstevel@tonic-gate 	int		print_status,
255*0Sstevel@tonic-gate 	md_error_t	*ep
256*0Sstevel@tonic-gate )
257*0Sstevel@tonic-gate {
258*0Sstevel@tonic-gate 	int	lockfd;
259*0Sstevel@tonic-gate 	char	*lockname = NULL;
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate 	/* open lock file */
262*0Sstevel@tonic-gate 	if (meta_lock_open(sp, ep) != 0) {
263*0Sstevel@tonic-gate 		assert(META_LOCK_FD(sp) == MD_NO_LOCK);
264*0Sstevel@tonic-gate 		goto failure;
265*0Sstevel@tonic-gate 	}
266*0Sstevel@tonic-gate 
267*0Sstevel@tonic-gate 	/* ignore read-only filesystem */
268*0Sstevel@tonic-gate 	if ((lockfd = META_LOCK_FD(sp)) == MD_NO_LOCK)
269*0Sstevel@tonic-gate 		goto success;
270*0Sstevel@tonic-gate 	assert(lockfd >= 0);
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate 	lockname = META_LOCK_NAME(sp);
273*0Sstevel@tonic-gate 
274*0Sstevel@tonic-gate 	/* grab lock */
275*0Sstevel@tonic-gate 	if (lockf(lockfd, F_TLOCK, 0) != 0) {
276*0Sstevel@tonic-gate 		if ((errno != EACCES) && (errno != EAGAIN)) {
277*0Sstevel@tonic-gate 			(void) mdsyserror(ep, errno, lockname);
278*0Sstevel@tonic-gate 			goto failure;
279*0Sstevel@tonic-gate 		}
280*0Sstevel@tonic-gate 		if (print_status)
281*0Sstevel@tonic-gate 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
282*0Sstevel@tonic-gate 			    "%s: waiting on %s\n"),
283*0Sstevel@tonic-gate 			    myname, lockname);
284*0Sstevel@tonic-gate 		if (lockf(lockfd, F_LOCK, 0) != 0) {
285*0Sstevel@tonic-gate 			(void) mdsyserror(ep, errno, lockname);
286*0Sstevel@tonic-gate 			goto failure;
287*0Sstevel@tonic-gate 		}
288*0Sstevel@tonic-gate 	}
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate 	/* return success */
291*0Sstevel@tonic-gate success:
292*0Sstevel@tonic-gate 	if (lockname != NULL)
293*0Sstevel@tonic-gate 		free(lockname);
294*0Sstevel@tonic-gate 	return (0);
295*0Sstevel@tonic-gate 
296*0Sstevel@tonic-gate 	/* flag failure */
297*0Sstevel@tonic-gate failure:
298*0Sstevel@tonic-gate 	if (lockname != NULL)
299*0Sstevel@tonic-gate 		free(lockname);
300*0Sstevel@tonic-gate 	if (lockfd >= 0)
301*0Sstevel@tonic-gate 		(void) meta_lock_close(sp, ep);
302*0Sstevel@tonic-gate 	return (-1);
303*0Sstevel@tonic-gate }
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate int
meta_lock_nowait(mdsetname_t * sp,md_error_t * ep)306*0Sstevel@tonic-gate meta_lock_nowait(
307*0Sstevel@tonic-gate 	mdsetname_t	*sp,
308*0Sstevel@tonic-gate 	md_error_t	*ep
309*0Sstevel@tonic-gate )
310*0Sstevel@tonic-gate {
311*0Sstevel@tonic-gate 	int	lockfd;
312*0Sstevel@tonic-gate 	char	*lockname = NULL;
313*0Sstevel@tonic-gate 
314*0Sstevel@tonic-gate 	/* open lock file */
315*0Sstevel@tonic-gate 	if (meta_lock_open(sp, ep) != 0) {
316*0Sstevel@tonic-gate 		assert(META_LOCK_FD(sp) == MD_NO_LOCK);
317*0Sstevel@tonic-gate 		goto failure;
318*0Sstevel@tonic-gate 	}
319*0Sstevel@tonic-gate 
320*0Sstevel@tonic-gate 	/* ignore read-only filesystem */
321*0Sstevel@tonic-gate 	if ((lockfd = META_LOCK_FD(sp)) == MD_NO_LOCK)
322*0Sstevel@tonic-gate 		goto success;
323*0Sstevel@tonic-gate 	assert(lockfd >= 0);
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate 	lockname = META_LOCK_NAME(sp);
326*0Sstevel@tonic-gate 
327*0Sstevel@tonic-gate 	/* grab lock */
328*0Sstevel@tonic-gate 	if (lockf(lockfd, F_TLOCK, 0) != 0) {
329*0Sstevel@tonic-gate 		if ((errno != EACCES) && (errno != EAGAIN)) {
330*0Sstevel@tonic-gate 			(void) mdsyserror(ep, errno, lockname);
331*0Sstevel@tonic-gate 			goto failure;
332*0Sstevel@tonic-gate 		}
333*0Sstevel@tonic-gate 		(void) mdsyserror(ep, EAGAIN, lockname);
334*0Sstevel@tonic-gate 		goto failure;
335*0Sstevel@tonic-gate 	}
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate 	/* return success */
338*0Sstevel@tonic-gate success:
339*0Sstevel@tonic-gate 	if (lockname != NULL)
340*0Sstevel@tonic-gate 		free(lockname);
341*0Sstevel@tonic-gate 	return (0);
342*0Sstevel@tonic-gate 
343*0Sstevel@tonic-gate 	/* flag failure */
344*0Sstevel@tonic-gate failure:
345*0Sstevel@tonic-gate 	if (lockname != NULL)
346*0Sstevel@tonic-gate 		free(lockname);
347*0Sstevel@tonic-gate 	if (lockfd >= 0)
348*0Sstevel@tonic-gate 		(void) meta_lock_close(sp, ep);
349*0Sstevel@tonic-gate 	return (-1);
350*0Sstevel@tonic-gate }
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate /*
353*0Sstevel@tonic-gate  * lock status
354*0Sstevel@tonic-gate  */
355*0Sstevel@tonic-gate int
meta_lock_status(mdsetname_t * sp,md_error_t * ep)356*0Sstevel@tonic-gate meta_lock_status(
357*0Sstevel@tonic-gate 	mdsetname_t	*sp,
358*0Sstevel@tonic-gate 	md_error_t	*ep
359*0Sstevel@tonic-gate )
360*0Sstevel@tonic-gate {
361*0Sstevel@tonic-gate 	int lockfd;
362*0Sstevel@tonic-gate 
363*0Sstevel@tonic-gate 	/* open lock file */
364*0Sstevel@tonic-gate 	if (meta_lock_open(sp, ep) != 0) {
365*0Sstevel@tonic-gate 		assert(META_LOCK_FD(sp) == MD_NO_LOCK);
366*0Sstevel@tonic-gate 		return (-1);
367*0Sstevel@tonic-gate 	}
368*0Sstevel@tonic-gate 
369*0Sstevel@tonic-gate 	lockfd = META_LOCK_FD(sp);
370*0Sstevel@tonic-gate 	/* ignore read-only filesystem */
371*0Sstevel@tonic-gate 	if (lockfd == MD_NO_LOCK)
372*0Sstevel@tonic-gate 		return (0);
373*0Sstevel@tonic-gate 	assert(lockfd >= 0);
374*0Sstevel@tonic-gate 
375*0Sstevel@tonic-gate 	/* test lock */
376*0Sstevel@tonic-gate 	if (lockf(lockfd, F_TEST, 0) != 0) {
377*0Sstevel@tonic-gate 		char *lockname = META_LOCK_NAME(sp);
378*0Sstevel@tonic-gate 		(void) mdsyserror(ep, errno, lockname);
379*0Sstevel@tonic-gate 		if (lockname != NULL)
380*0Sstevel@tonic-gate 			free(lockname);
381*0Sstevel@tonic-gate 		return (-1);
382*0Sstevel@tonic-gate 	}
383*0Sstevel@tonic-gate 
384*0Sstevel@tonic-gate 	return (0);
385*0Sstevel@tonic-gate }
386*0Sstevel@tonic-gate 
387*0Sstevel@tonic-gate /*
388*0Sstevel@tonic-gate  * setup for syslog daemon output
389*0Sstevel@tonic-gate  */
390*0Sstevel@tonic-gate static void
md_syslog(char * name)391*0Sstevel@tonic-gate md_syslog(
392*0Sstevel@tonic-gate 	char	*name	/* name of program */
393*0Sstevel@tonic-gate )
394*0Sstevel@tonic-gate {
395*0Sstevel@tonic-gate 	if ((name == NULL) || (*name == '\0'))
396*0Sstevel@tonic-gate 		name = "md";
397*0Sstevel@tonic-gate 	openlog(name, LOG_CONS, LOG_DAEMON);
398*0Sstevel@tonic-gate 	metasyslog = 1;
399*0Sstevel@tonic-gate }
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate /*
402*0Sstevel@tonic-gate  * daemonize: put in background
403*0Sstevel@tonic-gate  */
404*0Sstevel@tonic-gate int
md_daemonize(mdsetname_t * sp,md_error_t * ep)405*0Sstevel@tonic-gate md_daemonize(
406*0Sstevel@tonic-gate 	mdsetname_t	*sp,
407*0Sstevel@tonic-gate 	md_error_t	*ep
408*0Sstevel@tonic-gate )
409*0Sstevel@tonic-gate {
410*0Sstevel@tonic-gate 	char		*p;
411*0Sstevel@tonic-gate 	struct rlimit	rlim;
412*0Sstevel@tonic-gate 	pid_t		pid;
413*0Sstevel@tonic-gate 	int		i;
414*0Sstevel@tonic-gate 
415*0Sstevel@tonic-gate 	/* debug */
416*0Sstevel@tonic-gate 	if (((p = getenv("MD_DEBUG")) != NULL) &&
417*0Sstevel@tonic-gate 	    (strstr(p, "NODAEMON") != NULL)) {
418*0Sstevel@tonic-gate 		return (0);	/* do nothing */
419*0Sstevel@tonic-gate 	}
420*0Sstevel@tonic-gate 
421*0Sstevel@tonic-gate 	/* get number of file descriptors */
422*0Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
423*0Sstevel@tonic-gate 		return (mdsyserror(ep, errno, "getrlimit(RLIMIT_NOFILE)"));
424*0Sstevel@tonic-gate 	}
425*0Sstevel@tonic-gate 
426*0Sstevel@tonic-gate 	/* fork and kill parent */
427*0Sstevel@tonic-gate 	if ((pid = fork()) == -1)
428*0Sstevel@tonic-gate 		return (mdsyserror(ep, errno, "fork"));
429*0Sstevel@tonic-gate 	else if (pid != 0)
430*0Sstevel@tonic-gate 		return (pid);
431*0Sstevel@tonic-gate 
432*0Sstevel@tonic-gate 	/*
433*0Sstevel@tonic-gate 	 * We need to close the admin device and reset the specialfd to force
434*0Sstevel@tonic-gate 	 * the child process to reopen it, since we are going to close all
435*0Sstevel@tonic-gate 	 * descriptors from 3 up to RLIMIT_NOFILE in the child.
436*0Sstevel@tonic-gate 	 */
437*0Sstevel@tonic-gate 	if (close_admin(ep) != 0)
438*0Sstevel@tonic-gate 		return (-1);
439*0Sstevel@tonic-gate 
440*0Sstevel@tonic-gate 	/* close RPC connections */
441*0Sstevel@tonic-gate 	metarpccloseall();
442*0Sstevel@tonic-gate 
443*0Sstevel@tonic-gate 	/* drop lock */
444*0Sstevel@tonic-gate 	if (meta_unlock(sp, ep) != 0)
445*0Sstevel@tonic-gate 		return (-1);
446*0Sstevel@tonic-gate 
447*0Sstevel@tonic-gate 	if (rlim.rlim_cur != RLIM_INFINITY) {
448*0Sstevel@tonic-gate 		/*
449*0Sstevel@tonic-gate 		 * close all but stdout, stderr, and metalogfp
450*0Sstevel@tonic-gate 		 */
451*0Sstevel@tonic-gate 
452*0Sstevel@tonic-gate 		for (i = 0; (i < rlim.rlim_cur); ++i) {
453*0Sstevel@tonic-gate 			if ((i == fileno(stdout)) ||
454*0Sstevel@tonic-gate 			    (i == fileno(stderr)) ||
455*0Sstevel@tonic-gate 			    ((metalogfp != NULL) &&
456*0Sstevel@tonic-gate 			    (i == fileno(metalogfp)))) {
457*0Sstevel@tonic-gate 				continue;
458*0Sstevel@tonic-gate 			}
459*0Sstevel@tonic-gate 			(void) close(i);
460*0Sstevel@tonic-gate 		}
461*0Sstevel@tonic-gate 	}
462*0Sstevel@tonic-gate 
463*0Sstevel@tonic-gate 	/* put in own process group */
464*0Sstevel@tonic-gate 	if (setsid() == -1)
465*0Sstevel@tonic-gate 		return (mdsyserror(ep, errno, "setsid"));
466*0Sstevel@tonic-gate 
467*0Sstevel@tonic-gate 	/* setup syslog */
468*0Sstevel@tonic-gate 	md_syslog(myname);
469*0Sstevel@tonic-gate 
470*0Sstevel@tonic-gate 	/* return success */
471*0Sstevel@tonic-gate 	return (0);
472*0Sstevel@tonic-gate }
473*0Sstevel@tonic-gate 
474*0Sstevel@tonic-gate /*
475*0Sstevel@tonic-gate  * flush and sync fp
476*0Sstevel@tonic-gate  */
477*0Sstevel@tonic-gate static void
flushfp(FILE * fp)478*0Sstevel@tonic-gate flushfp(
479*0Sstevel@tonic-gate 	FILE	*fp
480*0Sstevel@tonic-gate )
481*0Sstevel@tonic-gate {
482*0Sstevel@tonic-gate 	(void) fflush(fp);
483*0Sstevel@tonic-gate 	(void) fsync(fileno(fp));
484*0Sstevel@tonic-gate }
485*0Sstevel@tonic-gate 
486*0Sstevel@tonic-gate /*
487*0Sstevel@tonic-gate  * reset and exit utility
488*0Sstevel@tonic-gate  */
489*0Sstevel@tonic-gate void
md_exit(mdsetname_t * sp,int eval)490*0Sstevel@tonic-gate md_exit(
491*0Sstevel@tonic-gate 	mdsetname_t	*sp,
492*0Sstevel@tonic-gate 	int		eval
493*0Sstevel@tonic-gate )
494*0Sstevel@tonic-gate {
495*0Sstevel@tonic-gate 	md_error_t	status = mdnullerror;
496*0Sstevel@tonic-gate 	md_error_t	*ep = &status;
497*0Sstevel@tonic-gate 
498*0Sstevel@tonic-gate 
499*0Sstevel@tonic-gate 	/* close RPC connections */
500*0Sstevel@tonic-gate 	metarpccloseall();
501*0Sstevel@tonic-gate 
502*0Sstevel@tonic-gate 	if (sp != NULL) {
503*0Sstevel@tonic-gate 		if (meta_unlock(sp, ep) != 0) {
504*0Sstevel@tonic-gate 			mde_perror(ep, "");
505*0Sstevel@tonic-gate 			mdclrerror(ep);
506*0Sstevel@tonic-gate 			if (eval == 0)
507*0Sstevel@tonic-gate 				eval = 1;
508*0Sstevel@tonic-gate 		}
509*0Sstevel@tonic-gate 	}
510*0Sstevel@tonic-gate 
511*0Sstevel@tonic-gate 	/* flush name caches */
512*0Sstevel@tonic-gate #ifdef	DEBUG
513*0Sstevel@tonic-gate 	metaflushnames(1);
514*0Sstevel@tonic-gate #endif	/* DEBUG */
515*0Sstevel@tonic-gate 
516*0Sstevel@tonic-gate 	/* log exit */
517*0Sstevel@tonic-gate 	if (metalogfp != NULL) {
518*0Sstevel@tonic-gate 		md_logpfx(metalogfp);
519*0Sstevel@tonic-gate 		(void) fprintf(metalogfp, dgettext(TEXT_DOMAIN,
520*0Sstevel@tonic-gate 		    "exiting with %d\n"), eval);
521*0Sstevel@tonic-gate 		flushfp(metalogfp);
522*0Sstevel@tonic-gate 		(void) fclose(metalogfp);
523*0Sstevel@tonic-gate 		metalogfp = NULL;
524*0Sstevel@tonic-gate 	}
525*0Sstevel@tonic-gate 	if ((metasyslog) && (eval != 0)) {
526*0Sstevel@tonic-gate 		syslog(LOG_ERR, dgettext(TEXT_DOMAIN,
527*0Sstevel@tonic-gate 		    "exiting with %d\n"), eval);
528*0Sstevel@tonic-gate 		closelog();
529*0Sstevel@tonic-gate 		metasyslog = 0;
530*0Sstevel@tonic-gate 	}
531*0Sstevel@tonic-gate 
532*0Sstevel@tonic-gate 	/* check arena, print malloc usage */
533*0Sstevel@tonic-gate #ifdef	_DEBUG_MALLOC_INC
534*0Sstevel@tonic-gate 	(void) malloc_chain_check(1);
535*0Sstevel@tonic-gate 	{
536*0Sstevel@tonic-gate 		char	*p;
537*0Sstevel@tonic-gate 
538*0Sstevel@tonic-gate 		if (((p = getenv("MD_DEBUG")) != NULL) &&
539*0Sstevel@tonic-gate 		    (strstr(p, "MALLOC") != NULL)) {
540*0Sstevel@tonic-gate 			malloc_inuse_end = malloc_inuse(&malloc_histid_end);
541*0Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: end malloc_inuse %lu\n",
542*0Sstevel@tonic-gate 			    myname, malloc_inuse_end);
543*0Sstevel@tonic-gate 			if (malloc_inuse_end != malloc_inuse_begin) {
544*0Sstevel@tonic-gate 				malloc_list(fileno(stderr),
545*0Sstevel@tonic-gate 				    malloc_histid_begin, malloc_histid_end);
546*0Sstevel@tonic-gate 			}
547*0Sstevel@tonic-gate 		}
548*0Sstevel@tonic-gate 	}
549*0Sstevel@tonic-gate #endif	/* _DEBUG_MALLOC_INC */
550*0Sstevel@tonic-gate 
551*0Sstevel@tonic-gate 	/* exit with value */
552*0Sstevel@tonic-gate 	exit(eval);
553*0Sstevel@tonic-gate }
554*0Sstevel@tonic-gate 
555*0Sstevel@tonic-gate /*
556*0Sstevel@tonic-gate  * signal catcher
557*0Sstevel@tonic-gate  */
558*0Sstevel@tonic-gate static void
md_catcher(int sig)559*0Sstevel@tonic-gate md_catcher(
560*0Sstevel@tonic-gate 	int			sig
561*0Sstevel@tonic-gate )
562*0Sstevel@tonic-gate {
563*0Sstevel@tonic-gate 	char			buf[128];
564*0Sstevel@tonic-gate 	char			*msg;
565*0Sstevel@tonic-gate 	md_error_t		status = mdnullerror;
566*0Sstevel@tonic-gate 	md_error_t		*ep = &status;
567*0Sstevel@tonic-gate 	struct sigaction	defhandler;
568*0Sstevel@tonic-gate 
569*0Sstevel@tonic-gate 	/* log signal */
570*0Sstevel@tonic-gate 	if ((msg = strsignal(sig)) == NULL) {
571*0Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf),
572*0Sstevel@tonic-gate 		    dgettext(TEXT_DOMAIN, "unknown signal %d"), sig);
573*0Sstevel@tonic-gate 		msg = buf;
574*0Sstevel@tonic-gate 	}
575*0Sstevel@tonic-gate 	md_eprintf("%s\n", msg);
576*0Sstevel@tonic-gate 
577*0Sstevel@tonic-gate 	/*
578*0Sstevel@tonic-gate 	 * In roll_back crtical section handling, the first instance of a user
579*0Sstevel@tonic-gate 	 * generated signal is caught, a flag is set to allow preemption at a
580*0Sstevel@tonic-gate 	 * "convenient" point and md_catcher returns.  If the user continues
581*0Sstevel@tonic-gate 	 * generate the signal, the second instance will invoke the default
582*0Sstevel@tonic-gate 	 * handler and exit.
583*0Sstevel@tonic-gate 	 */
584*0Sstevel@tonic-gate 	if (rb_signal_handling == TRUE) {
585*0Sstevel@tonic-gate 		if (sig != SIGABRT && sig != SIGBUS && sig != SIGSEGV) {
586*0Sstevel@tonic-gate 			if (rb_signal_caught == FALSE) {
587*0Sstevel@tonic-gate 				rb_signal_caught = TRUE;
588*0Sstevel@tonic-gate 				rb_signal_which  = sig;
589*0Sstevel@tonic-gate 				return;
590*0Sstevel@tonic-gate 			}
591*0Sstevel@tonic-gate 		}
592*0Sstevel@tonic-gate 	}
593*0Sstevel@tonic-gate 
594*0Sstevel@tonic-gate 	/* let default handler do it's thing */
595*0Sstevel@tonic-gate 	if (md_popsig(sig, ep) != 0) {
596*0Sstevel@tonic-gate 		mde_perror(ep, "");
597*0Sstevel@tonic-gate 		mdclrerror(ep);
598*0Sstevel@tonic-gate 		defhandler.sa_flags = 0;
599*0Sstevel@tonic-gate 		if (sigfillset(&defhandler.sa_mask) < 0) {
600*0Sstevel@tonic-gate 			(void) mdsyserror(ep, errno,
601*0Sstevel@tonic-gate 			    "sigfillset(&defhandler.sa_mask)");
602*0Sstevel@tonic-gate 			mde_perror(ep, "");
603*0Sstevel@tonic-gate 			md_exit(NULL, 1);
604*0Sstevel@tonic-gate 		}
605*0Sstevel@tonic-gate 		defhandler.sa_handler = SIG_DFL;
606*0Sstevel@tonic-gate 		if (sigaction(sig, &defhandler, NULL) < 0) {
607*0Sstevel@tonic-gate 			(void) mdsyserror(ep, errno, "sigaction(&defhandler)");
608*0Sstevel@tonic-gate 			mde_perror(ep, "");
609*0Sstevel@tonic-gate 			md_exit(NULL, 1);
610*0Sstevel@tonic-gate 		}
611*0Sstevel@tonic-gate 	}
612*0Sstevel@tonic-gate 
613*0Sstevel@tonic-gate 	md_post_sig(sig);
614*0Sstevel@tonic-gate }
615*0Sstevel@tonic-gate 
616*0Sstevel@tonic-gate void
md_post_sig(int sig)617*0Sstevel@tonic-gate md_post_sig(int sig)
618*0Sstevel@tonic-gate {
619*0Sstevel@tonic-gate 	if (kill(getpid(), sig) != 0) {
620*0Sstevel@tonic-gate 		md_perror("kill(getpid())");
621*0Sstevel@tonic-gate 		md_exit(NULL, -sig);
622*0Sstevel@tonic-gate 	}
623*0Sstevel@tonic-gate }
624*0Sstevel@tonic-gate 
625*0Sstevel@tonic-gate int
md_got_sig(void)626*0Sstevel@tonic-gate md_got_sig(void)
627*0Sstevel@tonic-gate {
628*0Sstevel@tonic-gate 	return (rb_signal_caught);
629*0Sstevel@tonic-gate }
630*0Sstevel@tonic-gate 
631*0Sstevel@tonic-gate int
md_which_sig(void)632*0Sstevel@tonic-gate md_which_sig(void)
633*0Sstevel@tonic-gate {
634*0Sstevel@tonic-gate 	return (rb_signal_which);
635*0Sstevel@tonic-gate }
636*0Sstevel@tonic-gate 
637*0Sstevel@tonic-gate void
md_rb_sig_handling_on(void)638*0Sstevel@tonic-gate md_rb_sig_handling_on(void)
639*0Sstevel@tonic-gate {
640*0Sstevel@tonic-gate 	rb_signal_handling = TRUE;
641*0Sstevel@tonic-gate }
642*0Sstevel@tonic-gate 
643*0Sstevel@tonic-gate void
md_rb_sig_handling_off(int sig_seen,int sig)644*0Sstevel@tonic-gate md_rb_sig_handling_off(int sig_seen, int sig)
645*0Sstevel@tonic-gate {
646*0Sstevel@tonic-gate 	rb_signal_handling = FALSE;
647*0Sstevel@tonic-gate 	rb_signal_caught = FALSE;
648*0Sstevel@tonic-gate 	rb_signal_which  = 0;
649*0Sstevel@tonic-gate 	if (sig_seen)
650*0Sstevel@tonic-gate 		md_post_sig(sig);
651*0Sstevel@tonic-gate }
652*0Sstevel@tonic-gate 
653*0Sstevel@tonic-gate /*
654*0Sstevel@tonic-gate  * setup metaclust variables
655*0Sstevel@tonic-gate  */
656*0Sstevel@tonic-gate void
setup_mc_log(uint_t level)657*0Sstevel@tonic-gate setup_mc_log(
658*0Sstevel@tonic-gate 	uint_t	level
659*0Sstevel@tonic-gate )
660*0Sstevel@tonic-gate {
661*0Sstevel@tonic-gate 	/* initialise externals */
662*0Sstevel@tonic-gate 	verbosity = level;
663*0Sstevel@tonic-gate 	start_time = gethrtime();
664*0Sstevel@tonic-gate }
665*0Sstevel@tonic-gate 
666*0Sstevel@tonic-gate /*
667*0Sstevel@tonic-gate  * initilize utility
668*0Sstevel@tonic-gate  */
669*0Sstevel@tonic-gate int
md_init(int argc,char * argv[],int dosyslog,int doadmin,md_error_t * ep)670*0Sstevel@tonic-gate md_init(
671*0Sstevel@tonic-gate 	int		argc,
672*0Sstevel@tonic-gate 	char		*argv[],
673*0Sstevel@tonic-gate 	int		dosyslog,
674*0Sstevel@tonic-gate 	int		doadmin,
675*0Sstevel@tonic-gate 	md_error_t	*ep
676*0Sstevel@tonic-gate )
677*0Sstevel@tonic-gate {
678*0Sstevel@tonic-gate 	int ret = 0;
679*0Sstevel@tonic-gate 
680*0Sstevel@tonic-gate 	/* initialize everything but the signals */
681*0Sstevel@tonic-gate 	if ((ret = md_init_nosig(argc, argv, dosyslog,
682*0Sstevel@tonic-gate 			doadmin, ep)) != 0)
683*0Sstevel@tonic-gate 		return (ret);
684*0Sstevel@tonic-gate 
685*0Sstevel@tonic-gate 
686*0Sstevel@tonic-gate 	if (sigfillset(&allsigs) < 0)
687*0Sstevel@tonic-gate 		return (mdsyserror(ep, errno, "sigfillset(&allsigs)"));
688*0Sstevel@tonic-gate 
689*0Sstevel@tonic-gate 	/* catch common signals */
690*0Sstevel@tonic-gate 	if ((md_pushsig(SIGHUP, md_catcher, ep) != 0) ||
691*0Sstevel@tonic-gate 	    (md_pushsig(SIGINT, md_catcher, ep) != 0) ||
692*0Sstevel@tonic-gate 	    (md_pushsig(SIGQUIT, md_catcher, ep) != 0) ||
693*0Sstevel@tonic-gate 	    (md_pushsig(SIGABRT, md_catcher, ep) != 0) ||
694*0Sstevel@tonic-gate 	    (md_pushsig(SIGBUS, md_catcher, ep) != 0) ||
695*0Sstevel@tonic-gate 	    (md_pushsig(SIGSEGV, md_catcher, ep) != 0) ||
696*0Sstevel@tonic-gate 	    (md_pushsig(SIGPIPE, md_catcher, ep) != 0) ||
697*0Sstevel@tonic-gate 	    (md_pushsig(SIGTERM, md_catcher, ep) != 0)) {
698*0Sstevel@tonic-gate 		return (-1);
699*0Sstevel@tonic-gate 	}
700*0Sstevel@tonic-gate 
701*0Sstevel@tonic-gate 	/* return success */
702*0Sstevel@tonic-gate 	return (0);
703*0Sstevel@tonic-gate }
704*0Sstevel@tonic-gate 
705*0Sstevel@tonic-gate 
706*0Sstevel@tonic-gate /*
707*0Sstevel@tonic-gate  * initilize utility without setting up sighandlers
708*0Sstevel@tonic-gate  * setting up signal handlers in libmeta can affect others
709*0Sstevel@tonic-gate  * programs that link with libmeta but have their own handlers
710*0Sstevel@tonic-gate  */
711*0Sstevel@tonic-gate int
md_init_nosig(int argc,char * argv[],int dosyslog,int doadmin,md_error_t * ep)712*0Sstevel@tonic-gate md_init_nosig(
713*0Sstevel@tonic-gate 	int		argc,
714*0Sstevel@tonic-gate 	char		*argv[],
715*0Sstevel@tonic-gate 	int		dosyslog,
716*0Sstevel@tonic-gate 	int		doadmin,
717*0Sstevel@tonic-gate 	md_error_t	*ep
718*0Sstevel@tonic-gate )
719*0Sstevel@tonic-gate {
720*0Sstevel@tonic-gate 	/* setup myname */
721*0Sstevel@tonic-gate 	if ((myname = strrchr(argv[0], '/')) != NULL)
722*0Sstevel@tonic-gate 		++myname;
723*0Sstevel@tonic-gate 	else
724*0Sstevel@tonic-gate 		myname = argv[0];
725*0Sstevel@tonic-gate 
726*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
727*0Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
728*0Sstevel@tonic-gate #endif
729*0Sstevel@tonic-gate 
730*0Sstevel@tonic-gate 	/* print malloc usage */
731*0Sstevel@tonic-gate #ifdef	_DEBUG_MALLOC_INC
732*0Sstevel@tonic-gate 	{
733*0Sstevel@tonic-gate 		char	*p;
734*0Sstevel@tonic-gate 
735*0Sstevel@tonic-gate 		if (((p = getenv("MD_DEBUG")) != NULL) &&
736*0Sstevel@tonic-gate 		    (strstr(p, "MALLOC") != NULL)) {
737*0Sstevel@tonic-gate 			malloc_inuse_begin =
738*0Sstevel@tonic-gate 			    malloc_inuse(&malloc_histid_begin);
739*0Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: begin malloc_inuse %lu\n",
740*0Sstevel@tonic-gate 			    myname, malloc_inuse_begin);
741*0Sstevel@tonic-gate 		}
742*0Sstevel@tonic-gate 	}
743*0Sstevel@tonic-gate #endif	/* _DEBUG_MALLOC_INC */
744*0Sstevel@tonic-gate 
745*0Sstevel@tonic-gate 	/* open syslog */
746*0Sstevel@tonic-gate 	if (dosyslog)
747*0Sstevel@tonic-gate 		md_syslog(myname);
748*0Sstevel@tonic-gate 
749*0Sstevel@tonic-gate 	/* log command */
750*0Sstevel@tonic-gate 	if (getenv(METALOGENV) != NULL) {
751*0Sstevel@tonic-gate 		if ((metalogfp = fopen(METALOG, "a")) != NULL) {
752*0Sstevel@tonic-gate 			int	i;
753*0Sstevel@tonic-gate 
754*0Sstevel@tonic-gate 			(void) fchmod(fileno(metalogfp), 0664);
755*0Sstevel@tonic-gate 			md_logpfx(metalogfp);
756*0Sstevel@tonic-gate 			for (i = 1; (i < argc); ++i)
757*0Sstevel@tonic-gate 				(void) fprintf(metalogfp, " %s", argv[i]);
758*0Sstevel@tonic-gate 			(void) fprintf(metalogfp, "\n");
759*0Sstevel@tonic-gate 			flushfp(metalogfp);
760*0Sstevel@tonic-gate 		}
761*0Sstevel@tonic-gate 	}
762*0Sstevel@tonic-gate 
763*0Sstevel@tonic-gate 	/* make sure we can open the admin device before we do anything else */
764*0Sstevel@tonic-gate 	if (doadmin)
765*0Sstevel@tonic-gate 		if (open_admin(ep) < 0)
766*0Sstevel@tonic-gate 			return (-1);
767*0Sstevel@tonic-gate 
768*0Sstevel@tonic-gate 	/* flush name caches */
769*0Sstevel@tonic-gate 	metaflushnames(1);
770*0Sstevel@tonic-gate 
771*0Sstevel@tonic-gate 	/* return success */
772*0Sstevel@tonic-gate 	return (0);
773*0Sstevel@tonic-gate }
774*0Sstevel@tonic-gate 
775*0Sstevel@tonic-gate /*
776*0Sstevel@tonic-gate  * (re)initilize daemon
777*0Sstevel@tonic-gate  */
778*0Sstevel@tonic-gate int
md_init_daemon(char * name,md_error_t * ep)779*0Sstevel@tonic-gate md_init_daemon(
780*0Sstevel@tonic-gate 	char		*name,
781*0Sstevel@tonic-gate 	md_error_t	*ep
782*0Sstevel@tonic-gate )
783*0Sstevel@tonic-gate {
784*0Sstevel@tonic-gate 	static int	already = 0;
785*0Sstevel@tonic-gate 	int		dosyslog = 1;
786*0Sstevel@tonic-gate 	int		doadmin = 1;
787*0Sstevel@tonic-gate 
788*0Sstevel@tonic-gate 	/* setup */
789*0Sstevel@tonic-gate 	if (! already) {
790*0Sstevel@tonic-gate 		if (md_init(1, &name, dosyslog, doadmin, ep) != 0)
791*0Sstevel@tonic-gate 			return (-1);
792*0Sstevel@tonic-gate 		already = 1;
793*0Sstevel@tonic-gate 	}
794*0Sstevel@tonic-gate 
795*0Sstevel@tonic-gate 	/* return success */
796*0Sstevel@tonic-gate 	return (0);
797*0Sstevel@tonic-gate }
798*0Sstevel@tonic-gate 
799*0Sstevel@tonic-gate /*
800*0Sstevel@tonic-gate  * Roll back functions for handling sync and async cleanup.
801*0Sstevel@tonic-gate  */
802*0Sstevel@tonic-gate 
803*0Sstevel@tonic-gate int
procsigs(int block,sigset_t * oldsigs,md_error_t * ep)804*0Sstevel@tonic-gate procsigs(int block, sigset_t *oldsigs, md_error_t *ep)
805*0Sstevel@tonic-gate {
806*0Sstevel@tonic-gate 	if (block == TRUE) {
807*0Sstevel@tonic-gate 		if (sigprocmask(SIG_BLOCK, &allsigs, oldsigs) < 0) {
808*0Sstevel@tonic-gate 			(void) mdsyserror(ep, errno, "sigprocmask(SIG_BLOCK)");
809*0Sstevel@tonic-gate 			return (-1);
810*0Sstevel@tonic-gate 		}
811*0Sstevel@tonic-gate 	} else {
812*0Sstevel@tonic-gate 		if (sigprocmask(SIG_SETMASK, oldsigs, NULL) < 0) {
813*0Sstevel@tonic-gate 			(void) mdsyserror(ep, errno,
814*0Sstevel@tonic-gate 			    "sigprocmask(SIG_SETMASK)");
815*0Sstevel@tonic-gate 			return (-1);
816*0Sstevel@tonic-gate 		}
817*0Sstevel@tonic-gate 	}
818*0Sstevel@tonic-gate 	return (0);
819*0Sstevel@tonic-gate }
820*0Sstevel@tonic-gate 
821*0Sstevel@tonic-gate #ifdef DEBUG
822*0Sstevel@tonic-gate int
rb_test(int rbt_sel_tpt,char * rbt_sel_tag,md_error_t * ep)823*0Sstevel@tonic-gate rb_test(
824*0Sstevel@tonic-gate 	int		rbt_sel_tpt,
825*0Sstevel@tonic-gate 	char		*rbt_sel_tag,
826*0Sstevel@tonic-gate 	md_error_t	*ep
827*0Sstevel@tonic-gate )
828*0Sstevel@tonic-gate {
829*0Sstevel@tonic-gate 	char		*rbt_env_tpt = getenv("META_RBT_TPT");
830*0Sstevel@tonic-gate 	char		*rbt_env_tag = getenv("META_RBT_TAG");
831*0Sstevel@tonic-gate 	int		sig = 0;
832*0Sstevel@tonic-gate 	int		rbt_int_tpt;
833*0Sstevel@tonic-gate 	int		rbt_tag_match = 1;
834*0Sstevel@tonic-gate 	sigset_t	curmask;
835*0Sstevel@tonic-gate 	md_error_t	xep = mdnullerror;
836*0Sstevel@tonic-gate 
837*0Sstevel@tonic-gate 	if (rbt_env_tpt) {
838*0Sstevel@tonic-gate 		rbt_int_tpt = atoi(rbt_env_tpt);
839*0Sstevel@tonic-gate 		if (rbt_int_tpt < 0) {
840*0Sstevel@tonic-gate 			sig = 1;
841*0Sstevel@tonic-gate 			rbt_int_tpt = -1 * rbt_int_tpt;
842*0Sstevel@tonic-gate 		}
843*0Sstevel@tonic-gate 
844*0Sstevel@tonic-gate 		assert(rbt_sel_tpt != 0);
845*0Sstevel@tonic-gate 
846*0Sstevel@tonic-gate 		if (rbt_int_tpt == 0)
847*0Sstevel@tonic-gate 			return (0);
848*0Sstevel@tonic-gate 
849*0Sstevel@tonic-gate 		if (rbt_env_tag && rbt_sel_tag)
850*0Sstevel@tonic-gate 			if (strcmp(rbt_env_tag, rbt_sel_tag) != 0)
851*0Sstevel@tonic-gate 				rbt_tag_match = 0;
852*0Sstevel@tonic-gate 
853*0Sstevel@tonic-gate 		if (rbt_int_tpt == rbt_sel_tpt && rbt_tag_match) {
854*0Sstevel@tonic-gate 			md_eprintf(
855*0Sstevel@tonic-gate 			    "******************** RB_TEST(%s, %d, sig=%s)\n",
856*0Sstevel@tonic-gate 			    rbt_sel_tag, rbt_sel_tpt,
857*0Sstevel@tonic-gate 			    (sig != 0) ? "True" : "False");
858*0Sstevel@tonic-gate 			if (sig) {
859*0Sstevel@tonic-gate 				md_eprintf("********** sigsuspend()\n");
860*0Sstevel@tonic-gate 				if (sigprocmask(NULL, NULL, &curmask) < 0) {
861*0Sstevel@tonic-gate 					(void) mdsyserror(&xep, errno, NULL);
862*0Sstevel@tonic-gate 					mde_perror(&xep, "sigprocmask(GET)");
863*0Sstevel@tonic-gate 					md_exit(NULL, 1);
864*0Sstevel@tonic-gate 				}
865*0Sstevel@tonic-gate 
866*0Sstevel@tonic-gate 				if (sigsuspend(&curmask) < 0) {
867*0Sstevel@tonic-gate 					(void) mdsyserror(&xep, errno, NULL);
868*0Sstevel@tonic-gate 					mde_perror(&xep,
869*0Sstevel@tonic-gate 					    "sigsuspend(&curmask)");
870*0Sstevel@tonic-gate 					md_exit(NULL, 1);
871*0Sstevel@tonic-gate 				}
872*0Sstevel@tonic-gate 
873*0Sstevel@tonic-gate 				if (md_got_sig())
874*0Sstevel@tonic-gate 					return (-1);
875*0Sstevel@tonic-gate 			}
876*0Sstevel@tonic-gate 			(void) mderror(ep, MDE_TESTERROR,
877*0Sstevel@tonic-gate 			    "********** rb_test()");
878*0Sstevel@tonic-gate 			md_eprintf("******************** rollback\n");
879*0Sstevel@tonic-gate 			return (-1);
880*0Sstevel@tonic-gate 		}
881*0Sstevel@tonic-gate 	}
882*0Sstevel@tonic-gate 	return (0);
883*0Sstevel@tonic-gate }
884*0Sstevel@tonic-gate #else
885*0Sstevel@tonic-gate /* ARGSUSED */
886*0Sstevel@tonic-gate int
rb_test(int rbt_sel_tpt,char * rbt_sel_tag,md_error_t * ep)887*0Sstevel@tonic-gate rb_test(
888*0Sstevel@tonic-gate 	int		rbt_sel_tpt,
889*0Sstevel@tonic-gate 	char		*rbt_sel_tag,
890*0Sstevel@tonic-gate 	md_error_t	*ep
891*0Sstevel@tonic-gate )
892*0Sstevel@tonic-gate {
893*0Sstevel@tonic-gate 	(void) mderror(ep, MDE_TESTERROR, "******** rb_test:Not supported\n");
894*0Sstevel@tonic-gate 	return (-1);
895*0Sstevel@tonic-gate 
896*0Sstevel@tonic-gate }
897*0Sstevel@tonic-gate #endif	/* DEBUG */
898