xref: /titanic_44/usr/src/cmd/fs.d/cachefs/cfsd/cfsd_subr.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * Various support routines.
31*7c478bd9Sstevel@tonic-gate  */
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <libintl.h>
34*7c478bd9Sstevel@tonic-gate #include <stdio.h>
35*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
36*7c478bd9Sstevel@tonic-gate #include <string.h>
37*7c478bd9Sstevel@tonic-gate #include <errno.h>
38*7c478bd9Sstevel@tonic-gate #include <dirent.h>
39*7c478bd9Sstevel@tonic-gate #include <wait.h>
40*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
41*7c478bd9Sstevel@tonic-gate #include <limits.h>
42*7c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
43*7c478bd9Sstevel@tonic-gate #include <rpc/pmap_clnt.h> /* for pmap_unset */
44*7c478bd9Sstevel@tonic-gate #include <string.h> /* strcmp */
45*7c478bd9Sstevel@tonic-gate #include <signal.h>
46*7c478bd9Sstevel@tonic-gate #include <unistd.h> /* setsid */
47*7c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/vfstab.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
52*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
53*7c478bd9Sstevel@tonic-gate #include <memory.h>
54*7c478bd9Sstevel@tonic-gate #include <stropts.h>
55*7c478bd9Sstevel@tonic-gate #include <netconfig.h>
56*7c478bd9Sstevel@tonic-gate #include <sys/resource.h> /* rlimit */
57*7c478bd9Sstevel@tonic-gate #include <thread.h>
58*7c478bd9Sstevel@tonic-gate #include <synch.h>
59*7c478bd9Sstevel@tonic-gate #include <mdbug/mdbug.h>
60*7c478bd9Sstevel@tonic-gate #include <sys/fs/cachefs_fs.h>
61*7c478bd9Sstevel@tonic-gate #include <sys/fs/cachefs_dlog.h>
62*7c478bd9Sstevel@tonic-gate #include <sys/fs/cachefs_ioctl.h>
63*7c478bd9Sstevel@tonic-gate #include "cfsd.h"
64*7c478bd9Sstevel@tonic-gate #include "cfsd_kmod.h"
65*7c478bd9Sstevel@tonic-gate #include "cfsd_maptbl.h"
66*7c478bd9Sstevel@tonic-gate #include "cfsd_logfile.h"
67*7c478bd9Sstevel@tonic-gate #include "cfsd_fscache.h"
68*7c478bd9Sstevel@tonic-gate #include "cfsd_cache.h"
69*7c478bd9Sstevel@tonic-gate #include "cfsd_all.h"
70*7c478bd9Sstevel@tonic-gate #include <common/cachefsd.h>
71*7c478bd9Sstevel@tonic-gate #include <common/subr.h>
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate /* forward references */
74*7c478bd9Sstevel@tonic-gate void *subr_mount_thread(void *datap);
75*7c478bd9Sstevel@tonic-gate int subr_fsck_cache(const char *cachedirp);
76*7c478bd9Sstevel@tonic-gate void subr_doexec(const char *fstype, char *newargv[], const char *progp);
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate /*
79*7c478bd9Sstevel@tonic-gate  *			subr_add_mount
80*7c478bd9Sstevel@tonic-gate  *
81*7c478bd9Sstevel@tonic-gate  * Description:
82*7c478bd9Sstevel@tonic-gate  *	Adds the specified file system to the data structures.
83*7c478bd9Sstevel@tonic-gate  * Arguments:
84*7c478bd9Sstevel@tonic-gate  *	allp	ptr to set of data structures
85*7c478bd9Sstevel@tonic-gate  *	dirp	ptr to name of cache directory
86*7c478bd9Sstevel@tonic-gate  *	idp	ptr to id of file system cache in dirp
87*7c478bd9Sstevel@tonic-gate  * Returns:
88*7c478bd9Sstevel@tonic-gate  * Preconditions:
89*7c478bd9Sstevel@tonic-gate  *	precond(allp)
90*7c478bd9Sstevel@tonic-gate  *	precond(dirp)
91*7c478bd9Sstevel@tonic-gate  *	precond(idp)
92*7c478bd9Sstevel@tonic-gate  */
93*7c478bd9Sstevel@tonic-gate void
subr_add_mount(cfsd_all_object_t * all_object_p,const char * dirp,const char * idp)94*7c478bd9Sstevel@tonic-gate subr_add_mount(cfsd_all_object_t *all_object_p,
95*7c478bd9Sstevel@tonic-gate 	const char *dirp,
96*7c478bd9Sstevel@tonic-gate 	const char *idp)
97*7c478bd9Sstevel@tonic-gate {
98*7c478bd9Sstevel@tonic-gate 	int xx;
99*7c478bd9Sstevel@tonic-gate 	thread_t new_thread;
100*7c478bd9Sstevel@tonic-gate 	cfsd_cache_object_t *cache_object_p;
101*7c478bd9Sstevel@tonic-gate 	cfsd_fscache_object_t *fscache_object_p;
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate 	dbug_enter("subr_add_mount");
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	dbug_precond(all_object_p);
106*7c478bd9Sstevel@tonic-gate 	dbug_precond(dirp);
107*7c478bd9Sstevel@tonic-gate 	dbug_precond(idp);
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	dbug_print(("info", "cachedir %s, cacheid %s", dirp, idp));
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 	/* find or create the cache object */
112*7c478bd9Sstevel@tonic-gate 	all_lock(all_object_p);
113*7c478bd9Sstevel@tonic-gate 	cache_object_p = all_cachelist_find(all_object_p, dirp);
114*7c478bd9Sstevel@tonic-gate 	if (cache_object_p == NULL) {
115*7c478bd9Sstevel@tonic-gate 		/* make the cache object */
116*7c478bd9Sstevel@tonic-gate 		cache_object_p = cfsd_cache_create();
117*7c478bd9Sstevel@tonic-gate 		xx = all_object_p->i_nextcacheid;
118*7c478bd9Sstevel@tonic-gate 		xx = cache_setup(cache_object_p, dirp, xx);
119*7c478bd9Sstevel@tonic-gate 		if (xx == 0) {
120*7c478bd9Sstevel@tonic-gate 			dbug_print(("error", "invalid cache %s", dirp));
121*7c478bd9Sstevel@tonic-gate 			cfsd_cache_destroy(cache_object_p);
122*7c478bd9Sstevel@tonic-gate 			all_unlock(all_object_p);
123*7c478bd9Sstevel@tonic-gate 			dbug_leave("subr_add_mount");
124*7c478bd9Sstevel@tonic-gate 			return;
125*7c478bd9Sstevel@tonic-gate 		}
126*7c478bd9Sstevel@tonic-gate 		all_cachelist_add(all_object_p, cache_object_p);
127*7c478bd9Sstevel@tonic-gate 		all_cachefstab_update(all_object_p);
128*7c478bd9Sstevel@tonic-gate 	}
129*7c478bd9Sstevel@tonic-gate 	cache_lock(cache_object_p);
130*7c478bd9Sstevel@tonic-gate 	cache_object_p->i_refcnt++;
131*7c478bd9Sstevel@tonic-gate 	cache_unlock(cache_object_p);
132*7c478bd9Sstevel@tonic-gate 	all_unlock(all_object_p);
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 	/* find or create the fscache object */
135*7c478bd9Sstevel@tonic-gate 	cache_lock(cache_object_p);
136*7c478bd9Sstevel@tonic-gate 	fscache_object_p = cache_fscachelist_find(cache_object_p, idp);
137*7c478bd9Sstevel@tonic-gate 	if (fscache_object_p == NULL) {
138*7c478bd9Sstevel@tonic-gate 		/* make the fscache object and add it to the list */
139*7c478bd9Sstevel@tonic-gate 		xx = cache_object_p->i_nextfscacheid;
140*7c478bd9Sstevel@tonic-gate 		fscache_object_p = cfsd_fscache_create(idp, dirp, xx);
141*7c478bd9Sstevel@tonic-gate 		cache_fscachelist_add(cache_object_p, fscache_object_p);
142*7c478bd9Sstevel@tonic-gate 	} else {
143*7c478bd9Sstevel@tonic-gate 		/* don't do any more if already mounted */
144*7c478bd9Sstevel@tonic-gate 		fscache_lock(fscache_object_p);
145*7c478bd9Sstevel@tonic-gate 		if (fscache_object_p->i_mounted) {
146*7c478bd9Sstevel@tonic-gate 			cache_object_p->i_refcnt--;
147*7c478bd9Sstevel@tonic-gate 			fscache_unlock(fscache_object_p);
148*7c478bd9Sstevel@tonic-gate 			cache_unlock(cache_object_p);
149*7c478bd9Sstevel@tonic-gate 			dbug_print(("info", "fscache already mounted"));
150*7c478bd9Sstevel@tonic-gate 			dbug_leave("subr_add_mount");
151*7c478bd9Sstevel@tonic-gate 			return;
152*7c478bd9Sstevel@tonic-gate 		}
153*7c478bd9Sstevel@tonic-gate 		fscache_unlock(fscache_object_p);
154*7c478bd9Sstevel@tonic-gate 	}
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	fscache_lock(fscache_object_p);
157*7c478bd9Sstevel@tonic-gate 	fscache_object_p->i_refcnt++;
158*7c478bd9Sstevel@tonic-gate 	fscache_unlock(fscache_object_p);
159*7c478bd9Sstevel@tonic-gate 	cache_unlock(cache_object_p);
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	/* init the fscache object with mount information */
162*7c478bd9Sstevel@tonic-gate 	fscache_lock(fscache_object_p);
163*7c478bd9Sstevel@tonic-gate 	fscache_setup(fscache_object_p);
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 	/* start the disconnect thread if necessary */
166*7c478bd9Sstevel@tonic-gate 	if (fscache_object_p->i_disconnectable &&
167*7c478bd9Sstevel@tonic-gate 	    fscache_object_p->i_mounted &&
168*7c478bd9Sstevel@tonic-gate 	    (fscache_object_p->i_threaded == 0) &&
169*7c478bd9Sstevel@tonic-gate 	    (strcmp(fscache_object_p->i_name, "rootcache") != 0)) {
170*7c478bd9Sstevel@tonic-gate 		fscache_object_p->i_refcnt++;
171*7c478bd9Sstevel@tonic-gate 		fscache_object_p->i_threaded = 1;
172*7c478bd9Sstevel@tonic-gate 		xx = thr_create(NULL, 0, subr_mount_thread, fscache_object_p,
173*7c478bd9Sstevel@tonic-gate 		    THR_DETACHED | THR_NEW_LWP, &new_thread);
174*7c478bd9Sstevel@tonic-gate 		if (xx) {
175*7c478bd9Sstevel@tonic-gate 			/* XXX cachefs kmod cannot allow transition */
176*7c478bd9Sstevel@tonic-gate 			dbug_print(("error", "mount thr_create failed %d", xx));
177*7c478bd9Sstevel@tonic-gate 			fscache_object_p->i_refcnt--;
178*7c478bd9Sstevel@tonic-gate 			fscache_object_p->i_threaded = 0;
179*7c478bd9Sstevel@tonic-gate 		}
180*7c478bd9Sstevel@tonic-gate 		fscache_object_p->i_threadid = new_thread;
181*7c478bd9Sstevel@tonic-gate 	}
182*7c478bd9Sstevel@tonic-gate 	fscache_object_p->i_refcnt--;
183*7c478bd9Sstevel@tonic-gate 	fscache_unlock(fscache_object_p);
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate 	cache_lock(cache_object_p);
186*7c478bd9Sstevel@tonic-gate 	cache_object_p->i_refcnt--;
187*7c478bd9Sstevel@tonic-gate 	cache_unlock(cache_object_p);
188*7c478bd9Sstevel@tonic-gate 	dbug_leave("subr_add_mount");
189*7c478bd9Sstevel@tonic-gate }
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate /*
192*7c478bd9Sstevel@tonic-gate  * ------------------------------------------------------------
193*7c478bd9Sstevel@tonic-gate  *			subr_mount_thread
194*7c478bd9Sstevel@tonic-gate  *
195*7c478bd9Sstevel@tonic-gate  * Description:
196*7c478bd9Sstevel@tonic-gate  *	Called when a thread is created via thr_create to process
197*7c478bd9Sstevel@tonic-gate  *	an fscache.
198*7c478bd9Sstevel@tonic-gate  * Arguments:
199*7c478bd9Sstevel@tonic-gate  *	datap	ptr to cfsd_fscache to process
200*7c478bd9Sstevel@tonic-gate  * Returns:
201*7c478bd9Sstevel@tonic-gate  *	Returns NULL.
202*7c478bd9Sstevel@tonic-gate  * Preconditions:
203*7c478bd9Sstevel@tonic-gate  *	precond(datap)
204*7c478bd9Sstevel@tonic-gate  */
205*7c478bd9Sstevel@tonic-gate void *
subr_mount_thread(void * datap)206*7c478bd9Sstevel@tonic-gate subr_mount_thread(void *datap)
207*7c478bd9Sstevel@tonic-gate {
208*7c478bd9Sstevel@tonic-gate 	cfsd_fscache_object_t *fscache_object_p;
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 	dbug_enter("subr_mount_thread");
211*7c478bd9Sstevel@tonic-gate 	dbug_precond(datap);
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate 	fscache_object_p = (cfsd_fscache_object_t *)datap;
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate 	fscache_process(fscache_object_p);
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate 	fscache_lock(fscache_object_p);
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate 	/* close down the message file descriptor */
220*7c478bd9Sstevel@tonic-gate 	if (fscache_object_p->i_ofd >= 0) {
221*7c478bd9Sstevel@tonic-gate 		if (close(fscache_object_p->i_ofd))
222*7c478bd9Sstevel@tonic-gate 			dbug_print(("error", "cannot close fscache fd error %d",
223*7c478bd9Sstevel@tonic-gate 			    errno));
224*7c478bd9Sstevel@tonic-gate 		fscache_object_p->i_ofd = -1;
225*7c478bd9Sstevel@tonic-gate 	}
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 	fscache_object_p->i_threaded = 0;
228*7c478bd9Sstevel@tonic-gate 	fscache_object_p->i_refcnt--;
229*7c478bd9Sstevel@tonic-gate 	fscache_unlock(fscache_object_p);
230*7c478bd9Sstevel@tonic-gate 
231*7c478bd9Sstevel@tonic-gate 	dbug_leave("subr_mount_thread");
232*7c478bd9Sstevel@tonic-gate 	return (NULL);
233*7c478bd9Sstevel@tonic-gate }
234*7c478bd9Sstevel@tonic-gate 
235*7c478bd9Sstevel@tonic-gate /*
236*7c478bd9Sstevel@tonic-gate  * ------------------------------------------------------------
237*7c478bd9Sstevel@tonic-gate  *			subr_cache_setup
238*7c478bd9Sstevel@tonic-gate  *
239*7c478bd9Sstevel@tonic-gate  * Description:
240*7c478bd9Sstevel@tonic-gate  *	Called once when the daemon starts up to get the current state
241*7c478bd9Sstevel@tonic-gate  *	of caches reflected in the daemon.
242*7c478bd9Sstevel@tonic-gate  * Arguments:
243*7c478bd9Sstevel@tonic-gate  *	allp
244*7c478bd9Sstevel@tonic-gate  * Returns:
245*7c478bd9Sstevel@tonic-gate  * Preconditions:
246*7c478bd9Sstevel@tonic-gate  *	precond(allp)
247*7c478bd9Sstevel@tonic-gate  */
248*7c478bd9Sstevel@tonic-gate void
subr_cache_setup(cfsd_all_object_t * all_object_p)249*7c478bd9Sstevel@tonic-gate subr_cache_setup(cfsd_all_object_t *all_object_p)
250*7c478bd9Sstevel@tonic-gate {
251*7c478bd9Sstevel@tonic-gate 	cfsd_cache_object_t *cache_object_p;
252*7c478bd9Sstevel@tonic-gate 	int fixcachefstab = 0;
253*7c478bd9Sstevel@tonic-gate 	int xx;
254*7c478bd9Sstevel@tonic-gate 	FILE *fin;
255*7c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN];
256*7c478bd9Sstevel@tonic-gate 	struct mnttab minfo;
257*7c478bd9Sstevel@tonic-gate 	struct mnttab mpref;
258*7c478bd9Sstevel@tonic-gate 	char *cp;
259*7c478bd9Sstevel@tonic-gate 	char *xcp;
260*7c478bd9Sstevel@tonic-gate 	struct vfstab vinfo;
261*7c478bd9Sstevel@tonic-gate 	struct vfstab vpref;
262*7c478bd9Sstevel@tonic-gate 	size_t index;
263*7c478bd9Sstevel@tonic-gate 	int lockfd;
264*7c478bd9Sstevel@tonic-gate 	DIR *dirp;
265*7c478bd9Sstevel@tonic-gate 	char pathname[MAXPATHLEN];
266*7c478bd9Sstevel@tonic-gate 	int len;
267*7c478bd9Sstevel@tonic-gate 	struct dirent64 *entp;
268*7c478bd9Sstevel@tonic-gate 	struct stat64 sinfo;
269*7c478bd9Sstevel@tonic-gate 
270*7c478bd9Sstevel@tonic-gate 	dbug_enter("subr_cache_setup");
271*7c478bd9Sstevel@tonic-gate 	dbug_precond(all_object_p);
272*7c478bd9Sstevel@tonic-gate 
273*7c478bd9Sstevel@tonic-gate 	all_lock(all_object_p);
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate 	/* find all the caches indicated in the CACHEFSTAB file */
276*7c478bd9Sstevel@tonic-gate 	fin = fopen(CACHEFSTAB, "r");
277*7c478bd9Sstevel@tonic-gate 	if (fin == NULL) {
278*7c478bd9Sstevel@tonic-gate 		dbug_print(("info", "%s does not exist", CACHEFSTAB));
279*7c478bd9Sstevel@tonic-gate 	} else {
280*7c478bd9Sstevel@tonic-gate 		while (fgets(buf, sizeof (buf), fin) != NULL) {
281*7c478bd9Sstevel@tonic-gate 			if (strlen(buf) == 1)
282*7c478bd9Sstevel@tonic-gate 				continue;
283*7c478bd9Sstevel@tonic-gate 			/*
284*7c478bd9Sstevel@tonic-gate 			 * if the line did not fit in the buffer
285*7c478bd9Sstevel@tonic-gate 			 * it is invalid (i.e. no newline char)
286*7c478bd9Sstevel@tonic-gate 			 */
287*7c478bd9Sstevel@tonic-gate 			dbug_precond(buf[(strlen(buf) - 1)] == '\n');
288*7c478bd9Sstevel@tonic-gate 			if (buf[(strlen(buf) - 1)] != '\n') {
289*7c478bd9Sstevel@tonic-gate #if 0
290*7c478bd9Sstevel@tonic-gate 				/*
291*7c478bd9Sstevel@tonic-gate 				 * if the line is invalid read until
292*7c478bd9Sstevel@tonic-gate 				 * you get to the next line.
293*7c478bd9Sstevel@tonic-gate 				 * we only need to do this if we are
294*7c478bd9Sstevel@tonic-gate 				 * going to continue
295*7c478bd9Sstevel@tonic-gate 				 */
296*7c478bd9Sstevel@tonic-gate 				do {
297*7c478bd9Sstevel@tonic-gate 					cp = fgets(buf, sizeof (buf), fin);
298*7c478bd9Sstevel@tonic-gate 				} while ((cp != NULL) &&
299*7c478bd9Sstevel@tonic-gate 				    (buf[(strlen(buf) - 1)] != '\n'));
300*7c478bd9Sstevel@tonic-gate #endif
301*7c478bd9Sstevel@tonic-gate 				break;
302*7c478bd9Sstevel@tonic-gate 			}
303*7c478bd9Sstevel@tonic-gate 			buf[strlen(buf) - 1] = '\0';
304*7c478bd9Sstevel@tonic-gate 			dbug_print(("info", "cachefstab cache \"%s\"", buf));
305*7c478bd9Sstevel@tonic-gate 			cache_object_p = all_cachelist_find(all_object_p, buf);
306*7c478bd9Sstevel@tonic-gate 			if (cache_object_p == NULL) {
307*7c478bd9Sstevel@tonic-gate 				/* make the cache object */
308*7c478bd9Sstevel@tonic-gate 				cache_object_p = cfsd_cache_create();
309*7c478bd9Sstevel@tonic-gate 				xx = all_object_p->i_nextcacheid;
310*7c478bd9Sstevel@tonic-gate 				xx = cache_setup(cache_object_p, buf, xx);
311*7c478bd9Sstevel@tonic-gate 				if (xx == 0) {
312*7c478bd9Sstevel@tonic-gate 					cfsd_cache_destroy(cache_object_p);
313*7c478bd9Sstevel@tonic-gate 					fixcachefstab++;
314*7c478bd9Sstevel@tonic-gate 				} else {
315*7c478bd9Sstevel@tonic-gate 					all_cachelist_add(all_object_p,
316*7c478bd9Sstevel@tonic-gate 					    cache_object_p);
317*7c478bd9Sstevel@tonic-gate 				}
318*7c478bd9Sstevel@tonic-gate 			} else {
319*7c478bd9Sstevel@tonic-gate 				fixcachefstab++;
320*7c478bd9Sstevel@tonic-gate 			}
321*7c478bd9Sstevel@tonic-gate 		}
322*7c478bd9Sstevel@tonic-gate 		if (fclose(fin))
323*7c478bd9Sstevel@tonic-gate 			dbug_print(("err", "cannot close %s, %d",
324*7c478bd9Sstevel@tonic-gate 			    CACHEFSTAB, errno));
325*7c478bd9Sstevel@tonic-gate 	}
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate 	/* read the mnttab file looking for caches we may have missed */
328*7c478bd9Sstevel@tonic-gate 	fin = fopen(MNTTAB, "r");
329*7c478bd9Sstevel@tonic-gate 	if (fin == NULL) {
330*7c478bd9Sstevel@tonic-gate 		dbug_print(("info", "%s does not exist", MNTTAB));
331*7c478bd9Sstevel@tonic-gate 	} else {
332*7c478bd9Sstevel@tonic-gate 		mpref.mnt_special = NULL;
333*7c478bd9Sstevel@tonic-gate 		mpref.mnt_mountp = NULL;
334*7c478bd9Sstevel@tonic-gate 		mpref.mnt_fstype = "cachefs";
335*7c478bd9Sstevel@tonic-gate 		mpref.mnt_mntopts = NULL;
336*7c478bd9Sstevel@tonic-gate 		mpref.mnt_time = NULL;
337*7c478bd9Sstevel@tonic-gate 		while ((xx = getmntany(fin, &minfo, &mpref)) != -1) {
338*7c478bd9Sstevel@tonic-gate 			if (xx != 0)
339*7c478bd9Sstevel@tonic-gate 				continue;
340*7c478bd9Sstevel@tonic-gate 			cp = hasmntopt(&minfo, "cachedir=");
341*7c478bd9Sstevel@tonic-gate 			if (cp == NULL)
342*7c478bd9Sstevel@tonic-gate 				cp = "/cache"; /* XXX define in mount.c */
343*7c478bd9Sstevel@tonic-gate 			else {
344*7c478bd9Sstevel@tonic-gate 				cp += 9;
345*7c478bd9Sstevel@tonic-gate 				xcp = strchr(cp, ',');
346*7c478bd9Sstevel@tonic-gate 				if (xcp)
347*7c478bd9Sstevel@tonic-gate 					*xcp = '\0';
348*7c478bd9Sstevel@tonic-gate 			}
349*7c478bd9Sstevel@tonic-gate 			dbug_print(("info", "mnttab cache \"%s\"", cp));
350*7c478bd9Sstevel@tonic-gate 			cache_object_p = all_cachelist_find(all_object_p, cp);
351*7c478bd9Sstevel@tonic-gate 			if (cache_object_p == NULL) {
352*7c478bd9Sstevel@tonic-gate 				/* make the cache object */
353*7c478bd9Sstevel@tonic-gate 				cache_object_p = cfsd_cache_create();
354*7c478bd9Sstevel@tonic-gate 				xx = all_object_p->i_nextcacheid;
355*7c478bd9Sstevel@tonic-gate 				xx = cache_setup(cache_object_p, cp, xx);
356*7c478bd9Sstevel@tonic-gate 				if (xx == 0) {
357*7c478bd9Sstevel@tonic-gate 					cfsd_cache_destroy(cache_object_p);
358*7c478bd9Sstevel@tonic-gate 					fixcachefstab++;
359*7c478bd9Sstevel@tonic-gate 				} else {
360*7c478bd9Sstevel@tonic-gate 					all_cachelist_add(all_object_p,
361*7c478bd9Sstevel@tonic-gate 					    cache_object_p);
362*7c478bd9Sstevel@tonic-gate 				}
363*7c478bd9Sstevel@tonic-gate 			} else {
364*7c478bd9Sstevel@tonic-gate 				fixcachefstab++;
365*7c478bd9Sstevel@tonic-gate 			}
366*7c478bd9Sstevel@tonic-gate 		}
367*7c478bd9Sstevel@tonic-gate 		if (fclose(fin))
368*7c478bd9Sstevel@tonic-gate 			dbug_print(("err", "cannot close %s, %d",
369*7c478bd9Sstevel@tonic-gate 			    MNTTAB, errno));
370*7c478bd9Sstevel@tonic-gate 	}
371*7c478bd9Sstevel@tonic-gate 
372*7c478bd9Sstevel@tonic-gate 	/* read the vfstab file looking for caches we may have missed */
373*7c478bd9Sstevel@tonic-gate 	fin = fopen(VFSTAB, "r");
374*7c478bd9Sstevel@tonic-gate 	if (fin == NULL) {
375*7c478bd9Sstevel@tonic-gate 		dbug_print(("info", "%s does not exist", VFSTAB));
376*7c478bd9Sstevel@tonic-gate 	} else {
377*7c478bd9Sstevel@tonic-gate 		vpref.vfs_special = NULL;
378*7c478bd9Sstevel@tonic-gate 		vpref.vfs_fsckdev = NULL;
379*7c478bd9Sstevel@tonic-gate 		vpref.vfs_mountp = NULL;
380*7c478bd9Sstevel@tonic-gate 		vpref.vfs_fstype = "cachefs";
381*7c478bd9Sstevel@tonic-gate 		vpref.vfs_fsckpass = NULL;
382*7c478bd9Sstevel@tonic-gate 		vpref.vfs_automnt = NULL;
383*7c478bd9Sstevel@tonic-gate 		vpref.vfs_mntopts = NULL;
384*7c478bd9Sstevel@tonic-gate 		while ((xx = getvfsany(fin, &vinfo, &vpref)) != -1) {
385*7c478bd9Sstevel@tonic-gate 			if (xx != 0)
386*7c478bd9Sstevel@tonic-gate 				continue;
387*7c478bd9Sstevel@tonic-gate 			cp = strstr(vinfo.vfs_mntopts, "cachedir=");
388*7c478bd9Sstevel@tonic-gate 			if (cp == NULL)
389*7c478bd9Sstevel@tonic-gate 				cp = "/cache"; /* XXX define in mount.c */
390*7c478bd9Sstevel@tonic-gate 			else {
391*7c478bd9Sstevel@tonic-gate 				cp += 9;
392*7c478bd9Sstevel@tonic-gate 				xcp = strchr(cp, ',');
393*7c478bd9Sstevel@tonic-gate 				if (xcp)
394*7c478bd9Sstevel@tonic-gate 					*xcp = '\0';
395*7c478bd9Sstevel@tonic-gate 			}
396*7c478bd9Sstevel@tonic-gate 			dbug_print(("info", "vfstab cache \"%s\"", cp));
397*7c478bd9Sstevel@tonic-gate 			cache_object_p = all_cachelist_find(all_object_p, cp);
398*7c478bd9Sstevel@tonic-gate 			if (cache_object_p == NULL) {
399*7c478bd9Sstevel@tonic-gate 				/* make the cache object */
400*7c478bd9Sstevel@tonic-gate 				cache_object_p = cfsd_cache_create();
401*7c478bd9Sstevel@tonic-gate 				xx = all_object_p->i_nextcacheid;
402*7c478bd9Sstevel@tonic-gate 				xx = cache_setup(cache_object_p, cp, xx);
403*7c478bd9Sstevel@tonic-gate 				if (xx == 0) {
404*7c478bd9Sstevel@tonic-gate 					cfsd_cache_destroy(cache_object_p);
405*7c478bd9Sstevel@tonic-gate 				} else {
406*7c478bd9Sstevel@tonic-gate 					all_cachelist_add(all_object_p,
407*7c478bd9Sstevel@tonic-gate 					    cache_object_p);
408*7c478bd9Sstevel@tonic-gate 					fixcachefstab++;
409*7c478bd9Sstevel@tonic-gate 				}
410*7c478bd9Sstevel@tonic-gate 			}
411*7c478bd9Sstevel@tonic-gate 		}
412*7c478bd9Sstevel@tonic-gate 		if (fclose(fin))
413*7c478bd9Sstevel@tonic-gate 			dbug_print(("err", "cannot close %s, %d",
414*7c478bd9Sstevel@tonic-gate 			    VFSTAB, errno));
415*7c478bd9Sstevel@tonic-gate 	}
416*7c478bd9Sstevel@tonic-gate 
417*7c478bd9Sstevel@tonic-gate 	/* fix up the CACHEFSTAB file if it is out of date */
418*7c478bd9Sstevel@tonic-gate 	if (fixcachefstab)
419*7c478bd9Sstevel@tonic-gate 		all_cachefstab_update(all_object_p);
420*7c478bd9Sstevel@tonic-gate 
421*7c478bd9Sstevel@tonic-gate 	/*
422*7c478bd9Sstevel@tonic-gate 	 * now for each cache we found,
423*7c478bd9Sstevel@tonic-gate 	 * find all the file systems in the cache
424*7c478bd9Sstevel@tonic-gate 	 */
425*7c478bd9Sstevel@tonic-gate 	for (index = 0; index < all_object_p->i_cachecount; index++) {
426*7c478bd9Sstevel@tonic-gate 		cache_object_p = all_cachelist_at(all_object_p, index);
427*7c478bd9Sstevel@tonic-gate 		dbug_assert(cache_object_p);
428*7c478bd9Sstevel@tonic-gate 		cache_lock(cache_object_p);
429*7c478bd9Sstevel@tonic-gate 		cache_object_p->i_refcnt++;
430*7c478bd9Sstevel@tonic-gate 		cache_unlock(cache_object_p);
431*7c478bd9Sstevel@tonic-gate 		all_unlock(all_object_p);
432*7c478bd9Sstevel@tonic-gate 
433*7c478bd9Sstevel@tonic-gate 		/* fix up the cache if necessary */
434*7c478bd9Sstevel@tonic-gate 		xx = subr_fsck_cache(cache_object_p->i_cachedir);
435*7c478bd9Sstevel@tonic-gate 		if (xx != 0) {
436*7c478bd9Sstevel@tonic-gate 			dbug_print(("error", "could not fix up cache %d",
437*7c478bd9Sstevel@tonic-gate 			    cache_object_p->i_cachedir));
438*7c478bd9Sstevel@tonic-gate 			all_lock(all_object_p);
439*7c478bd9Sstevel@tonic-gate 			cache_lock(cache_object_p);
440*7c478bd9Sstevel@tonic-gate 			cache_object_p->i_refcnt--;
441*7c478bd9Sstevel@tonic-gate 			cache_unlock(cache_object_p);
442*7c478bd9Sstevel@tonic-gate 			continue;
443*7c478bd9Sstevel@tonic-gate 		}
444*7c478bd9Sstevel@tonic-gate 
445*7c478bd9Sstevel@tonic-gate 		/* lock out activity on the cache */
446*7c478bd9Sstevel@tonic-gate 		lockfd = cachefs_dir_lock(cache_object_p->i_cachedir, 0);
447*7c478bd9Sstevel@tonic-gate 		if (lockfd < 0) {
448*7c478bd9Sstevel@tonic-gate 			dbug_print(("error", "cannot aquire cache lock on %s",
449*7c478bd9Sstevel@tonic-gate 			    cache_object_p->i_cachedir));
450*7c478bd9Sstevel@tonic-gate 			all_lock(all_object_p);
451*7c478bd9Sstevel@tonic-gate 			cache_lock(cache_object_p);
452*7c478bd9Sstevel@tonic-gate 			cache_object_p->i_refcnt--;
453*7c478bd9Sstevel@tonic-gate 			cache_unlock(cache_object_p);
454*7c478bd9Sstevel@tonic-gate 			continue;
455*7c478bd9Sstevel@tonic-gate 		}
456*7c478bd9Sstevel@tonic-gate 
457*7c478bd9Sstevel@tonic-gate 		/* open the cache directory */
458*7c478bd9Sstevel@tonic-gate 		dirp = opendir(cache_object_p->i_cachedir);
459*7c478bd9Sstevel@tonic-gate 		if (dirp == NULL) {
460*7c478bd9Sstevel@tonic-gate 			dbug_print(("error", "cannot open dir %s",
461*7c478bd9Sstevel@tonic-gate 			    cache_object_p->i_cachedir));
462*7c478bd9Sstevel@tonic-gate 			cachefs_dir_unlock(lockfd);
463*7c478bd9Sstevel@tonic-gate 			all_lock(all_object_p);
464*7c478bd9Sstevel@tonic-gate 			cache_lock(cache_object_p);
465*7c478bd9Sstevel@tonic-gate 			cache_object_p->i_refcnt--;
466*7c478bd9Sstevel@tonic-gate 			cache_unlock(cache_object_p);
467*7c478bd9Sstevel@tonic-gate 			continue;
468*7c478bd9Sstevel@tonic-gate 		}
469*7c478bd9Sstevel@tonic-gate 
470*7c478bd9Sstevel@tonic-gate 		strlcpy(pathname, cache_object_p->i_cachedir,
471*7c478bd9Sstevel@tonic-gate 		    sizeof (pathname));
472*7c478bd9Sstevel@tonic-gate 		strlcat(pathname, "/", sizeof (pathname));
473*7c478bd9Sstevel@tonic-gate 		len = strlen(pathname);
474*7c478bd9Sstevel@tonic-gate 
475*7c478bd9Sstevel@tonic-gate 		/* read the directory entries */
476*7c478bd9Sstevel@tonic-gate 		while ((entp = readdir64(dirp)) != NULL) {
477*7c478bd9Sstevel@tonic-gate 			/* skip . and .. */
478*7c478bd9Sstevel@tonic-gate 			if ((strcmp(entp->d_name, ".") == 0) ||
479*7c478bd9Sstevel@tonic-gate 			    (strcmp(entp->d_name, "..") == 0))
480*7c478bd9Sstevel@tonic-gate 				continue;
481*7c478bd9Sstevel@tonic-gate 
482*7c478bd9Sstevel@tonic-gate 			pathname[len] = '\0';
483*7c478bd9Sstevel@tonic-gate 			strlcat(pathname, entp->d_name, sizeof (pathname));
484*7c478bd9Sstevel@tonic-gate 
485*7c478bd9Sstevel@tonic-gate 			/* get info on the file */
486*7c478bd9Sstevel@tonic-gate 			xx = lstat64(pathname, &sinfo);
487*7c478bd9Sstevel@tonic-gate 			if (xx != 0) {
488*7c478bd9Sstevel@tonic-gate 				dbug_print(("error",
489*7c478bd9Sstevel@tonic-gate 				    "cannot stat %s %d", pathname, errno));
490*7c478bd9Sstevel@tonic-gate 				continue;
491*7c478bd9Sstevel@tonic-gate 			}
492*7c478bd9Sstevel@tonic-gate 
493*7c478bd9Sstevel@tonic-gate 			/* skip unless a symbolic link */
494*7c478bd9Sstevel@tonic-gate 			if (!S_ISLNK(sinfo.st_mode))
495*7c478bd9Sstevel@tonic-gate 				continue;
496*7c478bd9Sstevel@tonic-gate 
497*7c478bd9Sstevel@tonic-gate 			/* add this file system to the list */
498*7c478bd9Sstevel@tonic-gate 			subr_add_mount(all_object_p, cache_object_p->i_cachedir,
499*7c478bd9Sstevel@tonic-gate 			    entp->d_name);
500*7c478bd9Sstevel@tonic-gate 		}
501*7c478bd9Sstevel@tonic-gate 		if (closedir(dirp))
502*7c478bd9Sstevel@tonic-gate 			dbug_print(("err", "cannot close dir, %d", errno));
503*7c478bd9Sstevel@tonic-gate 		cachefs_dir_unlock(lockfd);
504*7c478bd9Sstevel@tonic-gate 		all_lock(all_object_p);
505*7c478bd9Sstevel@tonic-gate 		cache_lock(cache_object_p);
506*7c478bd9Sstevel@tonic-gate 		cache_object_p->i_refcnt--;
507*7c478bd9Sstevel@tonic-gate 		cache_unlock(cache_object_p);
508*7c478bd9Sstevel@tonic-gate 	}
509*7c478bd9Sstevel@tonic-gate 
510*7c478bd9Sstevel@tonic-gate 	all_unlock(all_object_p);
511*7c478bd9Sstevel@tonic-gate 	dbug_leave("subr_cache_setup");
512*7c478bd9Sstevel@tonic-gate }
513*7c478bd9Sstevel@tonic-gate 
514*7c478bd9Sstevel@tonic-gate /*
515*7c478bd9Sstevel@tonic-gate  * ------------------------------------------------------------
516*7c478bd9Sstevel@tonic-gate  *			subr_fsck_cache
517*7c478bd9Sstevel@tonic-gate  *
518*7c478bd9Sstevel@tonic-gate  * Description:
519*7c478bd9Sstevel@tonic-gate  *	Fixes the cache if necessary.
520*7c478bd9Sstevel@tonic-gate  * Arguments:
521*7c478bd9Sstevel@tonic-gate  *	cachedirp
522*7c478bd9Sstevel@tonic-gate  * Returns:
523*7c478bd9Sstevel@tonic-gate  *	Returns 0 for success !0 if the cache is not fixed.
524*7c478bd9Sstevel@tonic-gate  * Preconditions:
525*7c478bd9Sstevel@tonic-gate  *	precond(cachedirp)
526*7c478bd9Sstevel@tonic-gate  */
527*7c478bd9Sstevel@tonic-gate int
subr_fsck_cache(const char * cachedirp)528*7c478bd9Sstevel@tonic-gate subr_fsck_cache(const char *cachedirp)
529*7c478bd9Sstevel@tonic-gate {
530*7c478bd9Sstevel@tonic-gate 	char *fsck_argv[4];
531*7c478bd9Sstevel@tonic-gate 	int status = 0;
532*7c478bd9Sstevel@tonic-gate 	pid_t pid;
533*7c478bd9Sstevel@tonic-gate 
534*7c478bd9Sstevel@tonic-gate 	dbug_enter("subr_fsck_cache");
535*7c478bd9Sstevel@tonic-gate 
536*7c478bd9Sstevel@tonic-gate 	dbug_precond(cachedirp);
537*7c478bd9Sstevel@tonic-gate 
538*7c478bd9Sstevel@tonic-gate 	fsck_argv[1] = "fsck";
539*7c478bd9Sstevel@tonic-gate 	fsck_argv[2] = (char *)cachedirp;
540*7c478bd9Sstevel@tonic-gate 	fsck_argv[3] = NULL;
541*7c478bd9Sstevel@tonic-gate 
542*7c478bd9Sstevel@tonic-gate 	dbug_print(("info", "about to fsck %s", cachedirp));
543*7c478bd9Sstevel@tonic-gate 
544*7c478bd9Sstevel@tonic-gate 	/* fork */
545*7c478bd9Sstevel@tonic-gate 	if ((pid = fork()) == -1) {
546*7c478bd9Sstevel@tonic-gate 		dbug_print(("error", "could not fork fsck %d", errno));
547*7c478bd9Sstevel@tonic-gate 		dbug_leave("subr_fsck_cache");
548*7c478bd9Sstevel@tonic-gate 		return (1);
549*7c478bd9Sstevel@tonic-gate 	}
550*7c478bd9Sstevel@tonic-gate 
551*7c478bd9Sstevel@tonic-gate 	if (pid == 0) {
552*7c478bd9Sstevel@tonic-gate 		/* do the fsck */
553*7c478bd9Sstevel@tonic-gate 		subr_doexec("cachefs", fsck_argv, "fsck");
554*7c478bd9Sstevel@tonic-gate 	} else {
555*7c478bd9Sstevel@tonic-gate 		/* wait for the child to exit */
556*7c478bd9Sstevel@tonic-gate 		if (waitpid(pid, &status, 0) == -1) {
557*7c478bd9Sstevel@tonic-gate 			dbug_print(("error", "fsck wait failed %d", errno));
558*7c478bd9Sstevel@tonic-gate 			dbug_leave("subr_fsck_cache");
559*7c478bd9Sstevel@tonic-gate 			return (1);
560*7c478bd9Sstevel@tonic-gate 		}
561*7c478bd9Sstevel@tonic-gate 
562*7c478bd9Sstevel@tonic-gate 		if (!WIFEXITED(status)) {
563*7c478bd9Sstevel@tonic-gate 			dbug_print(("error", "fsck did not exit"));
564*7c478bd9Sstevel@tonic-gate 			dbug_leave("subr_fsck_cache");
565*7c478bd9Sstevel@tonic-gate 			return (1);
566*7c478bd9Sstevel@tonic-gate 		}
567*7c478bd9Sstevel@tonic-gate 
568*7c478bd9Sstevel@tonic-gate 		if (WEXITSTATUS(status) != 0) {
569*7c478bd9Sstevel@tonic-gate 			dbug_print(("error", "fsck failed"));
570*7c478bd9Sstevel@tonic-gate 			dbug_leave("subr_fsck_cache");
571*7c478bd9Sstevel@tonic-gate 			return (1);
572*7c478bd9Sstevel@tonic-gate 		}
573*7c478bd9Sstevel@tonic-gate 	}
574*7c478bd9Sstevel@tonic-gate 	dbug_leave("subr_fsck_cache");
575*7c478bd9Sstevel@tonic-gate 	return (0);
576*7c478bd9Sstevel@tonic-gate }
577*7c478bd9Sstevel@tonic-gate 
578*7c478bd9Sstevel@tonic-gate /*
579*7c478bd9Sstevel@tonic-gate  * ------------------------------------------------------------
580*7c478bd9Sstevel@tonic-gate  *			subr_doexec
581*7c478bd9Sstevel@tonic-gate  *
582*7c478bd9Sstevel@tonic-gate  * Description:
583*7c478bd9Sstevel@tonic-gate  *	Execs the specified program with the specified command line arguments.
584*7c478bd9Sstevel@tonic-gate  *	This function never returns.
585*7c478bd9Sstevel@tonic-gate  * Arguments:
586*7c478bd9Sstevel@tonic-gate  *	fstype	type of file system
587*7c478bd9Sstevel@tonic-gate  *	newargv	command line arguments
588*7c478bd9Sstevel@tonic-gate  *	progp	name of program to exec
589*7c478bd9Sstevel@tonic-gate  * Returns:
590*7c478bd9Sstevel@tonic-gate  * Preconditions:
591*7c478bd9Sstevel@tonic-gate  *	precond(fstype)
592*7c478bd9Sstevel@tonic-gate  *	precond(newargv)
593*7c478bd9Sstevel@tonic-gate  *	precond(progp)
594*7c478bd9Sstevel@tonic-gate  */
595*7c478bd9Sstevel@tonic-gate void
subr_doexec(const char * fstype,char * newargv[],const char * progp)596*7c478bd9Sstevel@tonic-gate subr_doexec(const char *fstype, char *newargv[], const char *progp)
597*7c478bd9Sstevel@tonic-gate {
598*7c478bd9Sstevel@tonic-gate #define	VFS_PATH	"/usr/lib/fs"
599*7c478bd9Sstevel@tonic-gate #define	ALT_PATH	"/etc/fs"
600*7c478bd9Sstevel@tonic-gate 
601*7c478bd9Sstevel@tonic-gate 	char	full_path[MAXPATHLEN];
602*7c478bd9Sstevel@tonic-gate 	char	alter_path[MAXPATHLEN];
603*7c478bd9Sstevel@tonic-gate 	char	*vfs_path = VFS_PATH;
604*7c478bd9Sstevel@tonic-gate 	char	*alt_path = ALT_PATH;
605*7c478bd9Sstevel@tonic-gate 
606*7c478bd9Sstevel@tonic-gate 	dbug_enter("subr_doexec");
607*7c478bd9Sstevel@tonic-gate 
608*7c478bd9Sstevel@tonic-gate 	dbug_precond(fstype);
609*7c478bd9Sstevel@tonic-gate 	dbug_precond(newargv);
610*7c478bd9Sstevel@tonic-gate 	dbug_precond(progp);
611*7c478bd9Sstevel@tonic-gate 
612*7c478bd9Sstevel@tonic-gate 	/* build the full pathname of the fstype dependent command. */
613*7c478bd9Sstevel@tonic-gate 	snprintf(full_path, sizeof (full_path), "%s/%s/%s", vfs_path,
614*7c478bd9Sstevel@tonic-gate 	    fstype, progp);
615*7c478bd9Sstevel@tonic-gate 	snprintf(alter_path, sizeof (alter_path), "%s/%s/%s", alt_path,
616*7c478bd9Sstevel@tonic-gate 	    fstype, progp);
617*7c478bd9Sstevel@tonic-gate 
618*7c478bd9Sstevel@tonic-gate 	/* if the program exists */
619*7c478bd9Sstevel@tonic-gate 	if (access(full_path, X_OK) == 0) {
620*7c478bd9Sstevel@tonic-gate 		/* invoke the program */
621*7c478bd9Sstevel@tonic-gate 		execv(full_path, &newargv[1]);
622*7c478bd9Sstevel@tonic-gate 
623*7c478bd9Sstevel@tonic-gate 		/* if wrong permissions */
624*7c478bd9Sstevel@tonic-gate 		if (errno == EACCES) {
625*7c478bd9Sstevel@tonic-gate 			dbug_print(("error", "cannot execute %s %s",
626*7c478bd9Sstevel@tonic-gate 			    full_path, strerror(errno)));
627*7c478bd9Sstevel@tonic-gate 		}
628*7c478bd9Sstevel@tonic-gate 
629*7c478bd9Sstevel@tonic-gate #ifdef OBSOLETE
630*7c478bd9Sstevel@tonic-gate 		/* if it did not work and the shell might make it */
631*7c478bd9Sstevel@tonic-gate 		if (errno == ENOEXEC) {
632*7c478bd9Sstevel@tonic-gate 			newargv[0] = "sh";
633*7c478bd9Sstevel@tonic-gate 			newargv[1] = full_path;
634*7c478bd9Sstevel@tonic-gate 			execv("/sbin/sh", &newargv[0]);
635*7c478bd9Sstevel@tonic-gate 		}
636*7c478bd9Sstevel@tonic-gate #endif
637*7c478bd9Sstevel@tonic-gate 	}
638*7c478bd9Sstevel@tonic-gate 
639*7c478bd9Sstevel@tonic-gate #ifdef OBSOLETE
640*7c478bd9Sstevel@tonic-gate 	/* try the alternate path */
641*7c478bd9Sstevel@tonic-gate 	execv(alter_path, &newargv[1]);
642*7c478bd9Sstevel@tonic-gate 
643*7c478bd9Sstevel@tonic-gate 	/* if wrong permissions */
644*7c478bd9Sstevel@tonic-gate 	if (errno == EACCES) {
645*7c478bd9Sstevel@tonic-gate 		dbug_print(("error", "cannot execute %s %s",
646*7c478bd9Sstevel@tonic-gate 		    alter_path, strerror(errno)));
647*7c478bd9Sstevel@tonic-gate 	}
648*7c478bd9Sstevel@tonic-gate 
649*7c478bd9Sstevel@tonic-gate 	/* if it did not work and the shell might make it */
650*7c478bd9Sstevel@tonic-gate 	if (errno == ENOEXEC) {
651*7c478bd9Sstevel@tonic-gate 		newargv[0] = "sh";
652*7c478bd9Sstevel@tonic-gate 		newargv[1] = alter_path;
653*7c478bd9Sstevel@tonic-gate 		execv("/sbin/sh", &newargv[0]);
654*7c478bd9Sstevel@tonic-gate 	}
655*7c478bd9Sstevel@tonic-gate 
656*7c478bd9Sstevel@tonic-gate 	dbug_print(("error", "operation not applicable to FSType %s", fstype));
657*7c478bd9Sstevel@tonic-gate #endif
658*7c478bd9Sstevel@tonic-gate 	dbug_leave("subr_doexec");
659*7c478bd9Sstevel@tonic-gate 	_exit(1);
660*7c478bd9Sstevel@tonic-gate }
661*7c478bd9Sstevel@tonic-gate 
662*7c478bd9Sstevel@tonic-gate /*
663*7c478bd9Sstevel@tonic-gate  * ------------------------------------------------------------
664*7c478bd9Sstevel@tonic-gate  *			pr_err
665*7c478bd9Sstevel@tonic-gate  *
666*7c478bd9Sstevel@tonic-gate  * Description:
667*7c478bd9Sstevel@tonic-gate  * Arguments:
668*7c478bd9Sstevel@tonic-gate  *	fmt
669*7c478bd9Sstevel@tonic-gate  * Returns:
670*7c478bd9Sstevel@tonic-gate  * Preconditions:
671*7c478bd9Sstevel@tonic-gate  *	precond(fmt)
672*7c478bd9Sstevel@tonic-gate  */
673*7c478bd9Sstevel@tonic-gate void
pr_err(char * fmt,...)674*7c478bd9Sstevel@tonic-gate pr_err(char *fmt, ...)
675*7c478bd9Sstevel@tonic-gate {
676*7c478bd9Sstevel@tonic-gate 	va_list ap;
677*7c478bd9Sstevel@tonic-gate 
678*7c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
679*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "cachefsd -F cachefs: ");
680*7c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
681*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
682*7c478bd9Sstevel@tonic-gate 	va_end(ap);
683*7c478bd9Sstevel@tonic-gate }
684*7c478bd9Sstevel@tonic-gate 
685*7c478bd9Sstevel@tonic-gate 
686*7c478bd9Sstevel@tonic-gate /*
687*7c478bd9Sstevel@tonic-gate  *			subr_strdup
688*7c478bd9Sstevel@tonic-gate  *
689*7c478bd9Sstevel@tonic-gate  * Description:
690*7c478bd9Sstevel@tonic-gate  *	Returns the string dupped.  Returns NULL if passed NULL.
691*7c478bd9Sstevel@tonic-gate  *	Calls new to allocate memory.
692*7c478bd9Sstevel@tonic-gate  * Arguments:
693*7c478bd9Sstevel@tonic-gate  *	strp
694*7c478bd9Sstevel@tonic-gate  * Returns:
695*7c478bd9Sstevel@tonic-gate  * Preconditions:
696*7c478bd9Sstevel@tonic-gate  */
697*7c478bd9Sstevel@tonic-gate char *
subr_strdup(const char * strp)698*7c478bd9Sstevel@tonic-gate subr_strdup(const char *strp)
699*7c478bd9Sstevel@tonic-gate {
700*7c478bd9Sstevel@tonic-gate 	char *retp = NULL;
701*7c478bd9Sstevel@tonic-gate 	int len;
702*7c478bd9Sstevel@tonic-gate 
703*7c478bd9Sstevel@tonic-gate 	if (strp) {
704*7c478bd9Sstevel@tonic-gate 		len = strlen(strp) + 1;
705*7c478bd9Sstevel@tonic-gate 		retp = cfsd_calloc(len);
706*7c478bd9Sstevel@tonic-gate 		if (retp)
707*7c478bd9Sstevel@tonic-gate 			strlcpy(retp, strp, len);
708*7c478bd9Sstevel@tonic-gate 	}
709*7c478bd9Sstevel@tonic-gate 	return (retp);
710*7c478bd9Sstevel@tonic-gate }
711*7c478bd9Sstevel@tonic-gate /*
712*7c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------
713*7c478bd9Sstevel@tonic-gate  *			cfsd_calloc
714*7c478bd9Sstevel@tonic-gate  *
715*7c478bd9Sstevel@tonic-gate  * Description:
716*7c478bd9Sstevel@tonic-gate  *	allocates memory of a given size, will retry if error
717*7c478bd9Sstevel@tonic-gate  * Arguments:
718*7c478bd9Sstevel@tonic-gate  *	size
719*7c478bd9Sstevel@tonic-gate  * Returns:
720*7c478bd9Sstevel@tonic-gate  *	pointer to memory
721*7c478bd9Sstevel@tonic-gate  * Preconditions:
722*7c478bd9Sstevel@tonic-gate  *	precond(size)
723*7c478bd9Sstevel@tonic-gate  */
724*7c478bd9Sstevel@tonic-gate 
725*7c478bd9Sstevel@tonic-gate void *
cfsd_calloc(int size)726*7c478bd9Sstevel@tonic-gate cfsd_calloc(int size)
727*7c478bd9Sstevel@tonic-gate {
728*7c478bd9Sstevel@tonic-gate 	void *alloc_ptr;
729*7c478bd9Sstevel@tonic-gate 
730*7c478bd9Sstevel@tonic-gate 	dbug_enter("cfsd_calloc");
731*7c478bd9Sstevel@tonic-gate 	dbug_precond(size);
732*7c478bd9Sstevel@tonic-gate 
733*7c478bd9Sstevel@tonic-gate 	/* allocate memory, if calloc fails sleep and retry */
734*7c478bd9Sstevel@tonic-gate 	while ((alloc_ptr = calloc(size, 1)) == NULL) {
735*7c478bd9Sstevel@tonic-gate 		cfsd_sleep(5);
736*7c478bd9Sstevel@tonic-gate 	}
737*7c478bd9Sstevel@tonic-gate 
738*7c478bd9Sstevel@tonic-gate 	dbug_leave("cfsd_calloc");
739*7c478bd9Sstevel@tonic-gate 	return (alloc_ptr);
740*7c478bd9Sstevel@tonic-gate }
741*7c478bd9Sstevel@tonic-gate /*
742*7c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------
743*7c478bd9Sstevel@tonic-gate  *			cfsd_free
744*7c478bd9Sstevel@tonic-gate  *
745*7c478bd9Sstevel@tonic-gate  * Description:
746*7c478bd9Sstevel@tonic-gate  *	frees memory allocated from cfsd_calloc
747*7c478bd9Sstevel@tonic-gate  * Arguments:
748*7c478bd9Sstevel@tonic-gate  *	pointer to memeory
749*7c478bd9Sstevel@tonic-gate  * Returns:
750*7c478bd9Sstevel@tonic-gate  *	none
751*7c478bd9Sstevel@tonic-gate  * Preconditions:
752*7c478bd9Sstevel@tonic-gate  *	precond(size)
753*7c478bd9Sstevel@tonic-gate  */
754*7c478bd9Sstevel@tonic-gate 
755*7c478bd9Sstevel@tonic-gate void
cfsd_free(void * free_ptr)756*7c478bd9Sstevel@tonic-gate cfsd_free(void *free_ptr)
757*7c478bd9Sstevel@tonic-gate {
758*7c478bd9Sstevel@tonic-gate 	dbug_enter("cfsd_free");
759*7c478bd9Sstevel@tonic-gate 	dbug_precond(free_ptr);
760*7c478bd9Sstevel@tonic-gate 
761*7c478bd9Sstevel@tonic-gate 	/* free memory */
762*7c478bd9Sstevel@tonic-gate 	if (free_ptr)
763*7c478bd9Sstevel@tonic-gate 		free(free_ptr);
764*7c478bd9Sstevel@tonic-gate 
765*7c478bd9Sstevel@tonic-gate 	dbug_leave("cfsd_free");
766*7c478bd9Sstevel@tonic-gate }
767*7c478bd9Sstevel@tonic-gate /*
768*7c478bd9Sstevel@tonic-gate  * -----------------------------------------------------------------
769*7c478bd9Sstevel@tonic-gate  *			cfsd_sleep
770*7c478bd9Sstevel@tonic-gate  *
771*7c478bd9Sstevel@tonic-gate  * Description:
772*7c478bd9Sstevel@tonic-gate  *	A reimplemenation of the sleep(3c) function call using
773*7c478bd9Sstevel@tonic-gate  *	cond_timedwait.
774*7c478bd9Sstevel@tonic-gate  *	Problem withe sleep(3c) hanging. May return early.
775*7c478bd9Sstevel@tonic-gate  * Arguments:
776*7c478bd9Sstevel@tonic-gate  *	sec	number of seconds to sleep for
777*7c478bd9Sstevel@tonic-gate  * Returns:
778*7c478bd9Sstevel@tonic-gate  * Preconditions:
779*7c478bd9Sstevel@tonic-gate  */
780*7c478bd9Sstevel@tonic-gate 
781*7c478bd9Sstevel@tonic-gate void
cfsd_sleep(int sec)782*7c478bd9Sstevel@tonic-gate cfsd_sleep(int sec)
783*7c478bd9Sstevel@tonic-gate {
784*7c478bd9Sstevel@tonic-gate 	cond_t cv;
785*7c478bd9Sstevel@tonic-gate 	mutex_t mt;
786*7c478bd9Sstevel@tonic-gate 	timestruc_t reltime;
787*7c478bd9Sstevel@tonic-gate 
788*7c478bd9Sstevel@tonic-gate 	dbug_enter("cfsd_sleep");
789*7c478bd9Sstevel@tonic-gate 
790*7c478bd9Sstevel@tonic-gate 	if (sec > 0) {
791*7c478bd9Sstevel@tonic-gate 		mutex_init(&mt, USYNC_THREAD, NULL);
792*7c478bd9Sstevel@tonic-gate 		cond_init(&cv, USYNC_THREAD, 0);
793*7c478bd9Sstevel@tonic-gate 
794*7c478bd9Sstevel@tonic-gate 		reltime.tv_sec = sec;
795*7c478bd9Sstevel@tonic-gate 		reltime.tv_nsec = 0;
796*7c478bd9Sstevel@tonic-gate 
797*7c478bd9Sstevel@tonic-gate 		mutex_lock(&mt);
798*7c478bd9Sstevel@tonic-gate 		cond_reltimedwait(&cv, &mt, &reltime);
799*7c478bd9Sstevel@tonic-gate 		mutex_unlock(&mt);
800*7c478bd9Sstevel@tonic-gate 
801*7c478bd9Sstevel@tonic-gate 		cond_destroy(&cv);
802*7c478bd9Sstevel@tonic-gate 		mutex_destroy(&mt);
803*7c478bd9Sstevel@tonic-gate 	}
804*7c478bd9Sstevel@tonic-gate 	dbug_leave("cfsd_sleep");
805*7c478bd9Sstevel@tonic-gate }
806