xref: /titanic_44/usr/src/cmd/nscd/getexec.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 2003 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  * Routines to handle getexec* calls in nscd
31*7c478bd9Sstevel@tonic-gate  */
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <assert.h>
34*7c478bd9Sstevel@tonic-gate #include <errno.h>
35*7c478bd9Sstevel@tonic-gate #include <memory.h>
36*7c478bd9Sstevel@tonic-gate #include <signal.h>
37*7c478bd9Sstevel@tonic-gate #include <stdio.h>
38*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
39*7c478bd9Sstevel@tonic-gate #include <string.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/door.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/wait.h>
45*7c478bd9Sstevel@tonic-gate #include <thread.h>
46*7c478bd9Sstevel@tonic-gate #include <unistd.h>
47*7c478bd9Sstevel@tonic-gate #include <ucred.h>
48*7c478bd9Sstevel@tonic-gate #include <nss_common.h>
49*7c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h>
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate #include <exec_attr.h>
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate #include <getxby_door.h>
54*7c478bd9Sstevel@tonic-gate #include "server_door.h"
55*7c478bd9Sstevel@tonic-gate #include "nscd.h"
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate extern execstr_t *_getexecprof(char *, char *, char *, int, execstr_t *,
58*7c478bd9Sstevel@tonic-gate     char *, int, int *);
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate static hash_t *nam_hash;
61*7c478bd9Sstevel@tonic-gate static mutex_t  db_lock = DEFAULTMUTEX;
62*7c478bd9Sstevel@tonic-gate static waiter_t db_wait;
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate static getexec_namekeepalive(int keep, int interval);
65*7c478bd9Sstevel@tonic-gate static int update_exec_bucket(nsc_bucket_t **old, nsc_bucket_t *new,
66*7c478bd9Sstevel@tonic-gate     int callnumber);
67*7c478bd9Sstevel@tonic-gate static nsc_bucket_t *fixbuffer(nsc_return_t *in, int maxlen);
68*7c478bd9Sstevel@tonic-gate static void do_findgnams(nsc_bucket_t *ptr, int *table, char *gnam);
69*7c478bd9Sstevel@tonic-gate static void do_invalidate(nsc_bucket_t **ptr, int callnumber);
70*7c478bd9Sstevel@tonic-gate static void getexec_invalidate_unlocked(void);
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate void
74*7c478bd9Sstevel@tonic-gate getexec_init(void)
75*7c478bd9Sstevel@tonic-gate {
76*7c478bd9Sstevel@tonic-gate 	nam_hash = make_hash(current_admin.exec.nsc_suggestedsize);
77*7c478bd9Sstevel@tonic-gate }
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate static void
80*7c478bd9Sstevel@tonic-gate do_invalidate(nsc_bucket_t ** ptr, int callnumber)
81*7c478bd9Sstevel@tonic-gate {
82*7c478bd9Sstevel@tonic-gate 	if (*ptr != NULL && *ptr != (nsc_bucket_t *)-1) {
83*7c478bd9Sstevel@tonic-gate 		/* leave pending calls alone */
84*7c478bd9Sstevel@tonic-gate 		update_exec_bucket(ptr, NULL, callnumber);
85*7c478bd9Sstevel@tonic-gate 	}
86*7c478bd9Sstevel@tonic-gate }
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate static void
89*7c478bd9Sstevel@tonic-gate do_findgnams(nsc_bucket_t *ptr, int *table, char *gnam)
90*7c478bd9Sstevel@tonic-gate {
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	/*
93*7c478bd9Sstevel@tonic-gate 	 * be careful with ptr - it may be -1 or NULL.
94*7c478bd9Sstevel@tonic-gate 	 */
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate 	if (ptr != NULL && ptr != (nsc_bucket_t *)-1) {
97*7c478bd9Sstevel@tonic-gate 		char *tmp = (char *)insertn(table, ptr->nsc_hits,
98*7c478bd9Sstevel@tonic-gate 		    (int)strdup(gnam));
99*7c478bd9Sstevel@tonic-gate 		if (tmp != (char *)-1)
100*7c478bd9Sstevel@tonic-gate 			free(tmp);
101*7c478bd9Sstevel@tonic-gate 	}
102*7c478bd9Sstevel@tonic-gate }
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate void
105*7c478bd9Sstevel@tonic-gate getexec_revalidate(void)
106*7c478bd9Sstevel@tonic-gate {
107*7c478bd9Sstevel@tonic-gate 	for (;;) {
108*7c478bd9Sstevel@tonic-gate 		int slp;
109*7c478bd9Sstevel@tonic-gate 		int interval;
110*7c478bd9Sstevel@tonic-gate 		int count;
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 		slp = current_admin.exec.nsc_pos_ttl;
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 		if (slp < 60) {
115*7c478bd9Sstevel@tonic-gate 			slp = 60;
116*7c478bd9Sstevel@tonic-gate 		}
117*7c478bd9Sstevel@tonic-gate 		if ((count = current_admin.exec.nsc_keephot) != 0) {
118*7c478bd9Sstevel@tonic-gate 			interval = (slp / 2)/count;
119*7c478bd9Sstevel@tonic-gate 			if (interval == 0) interval = 1;
120*7c478bd9Sstevel@tonic-gate 			sleep(slp * 2 / 3);
121*7c478bd9Sstevel@tonic-gate 			getexec_namekeepalive(count, interval);
122*7c478bd9Sstevel@tonic-gate 		} else {
123*7c478bd9Sstevel@tonic-gate 			sleep(slp);
124*7c478bd9Sstevel@tonic-gate 		}
125*7c478bd9Sstevel@tonic-gate 	}
126*7c478bd9Sstevel@tonic-gate }
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate static
129*7c478bd9Sstevel@tonic-gate getexec_namekeepalive(int keep, int interval)
130*7c478bd9Sstevel@tonic-gate {
131*7c478bd9Sstevel@tonic-gate 	int *table;
132*7c478bd9Sstevel@tonic-gate 	union {
133*7c478bd9Sstevel@tonic-gate 		nsc_data_t  ping;
134*7c478bd9Sstevel@tonic-gate 		char space[sizeof (nsc_data_t) + NSCDMAXNAMELEN];
135*7c478bd9Sstevel@tonic-gate 	} u;
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	int i;
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	if (!keep)
140*7c478bd9Sstevel@tonic-gate 		return (0);
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 	table = maken(keep);
143*7c478bd9Sstevel@tonic-gate 	mutex_lock(&db_lock);
144*7c478bd9Sstevel@tonic-gate 	operate_hash(nam_hash, do_findgnams, (char *)table);
145*7c478bd9Sstevel@tonic-gate 	mutex_unlock(&db_lock);
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 	for (i = 1; i <= keep; i++) {
148*7c478bd9Sstevel@tonic-gate 		char *tmp;
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 		u.ping.nsc_call.nsc_callnumber = GETEXECID;
151*7c478bd9Sstevel@tonic-gate 		if ((tmp = (char *)table[keep + 1 + i]) == (char *)-1)
152*7c478bd9Sstevel@tonic-gate 			continue; /* unused slot in table */
153*7c478bd9Sstevel@tonic-gate 		strcpy(u.ping.nsc_call.nsc_u.name, tmp);
154*7c478bd9Sstevel@tonic-gate 		launch_update(&u.ping.nsc_call);
155*7c478bd9Sstevel@tonic-gate 		sleep(interval);
156*7c478bd9Sstevel@tonic-gate 	}
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 	for (i = 1; i <= keep; i++) {
159*7c478bd9Sstevel@tonic-gate 		char *tmp;
160*7c478bd9Sstevel@tonic-gate 		if ((tmp = (char *)table[keep + 1 + i]) != (char *)-1)
161*7c478bd9Sstevel@tonic-gate 			free(tmp);
162*7c478bd9Sstevel@tonic-gate 	}
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate 	free(table);
165*7c478bd9Sstevel@tonic-gate 	return (0);
166*7c478bd9Sstevel@tonic-gate }
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate /*
170*7c478bd9Sstevel@tonic-gate  *   This routine marks all entries as invalid
171*7c478bd9Sstevel@tonic-gate  *
172*7c478bd9Sstevel@tonic-gate  */
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate void
175*7c478bd9Sstevel@tonic-gate getexec_invalidate(void)
176*7c478bd9Sstevel@tonic-gate {
177*7c478bd9Sstevel@tonic-gate 	mutex_lock(&db_lock);
178*7c478bd9Sstevel@tonic-gate 	getexec_invalidate_unlocked();
179*7c478bd9Sstevel@tonic-gate 	mutex_unlock(&db_lock);
180*7c478bd9Sstevel@tonic-gate }
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate static void
183*7c478bd9Sstevel@tonic-gate getexec_invalidate_unlocked(void)
184*7c478bd9Sstevel@tonic-gate {
185*7c478bd9Sstevel@tonic-gate 	operate_hash_addr(nam_hash, do_invalidate, (char *)GETEXECID);
186*7c478bd9Sstevel@tonic-gate 	current_admin.exec.nsc_invalidate_count++;
187*7c478bd9Sstevel@tonic-gate }
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate void
190*7c478bd9Sstevel@tonic-gate getexec_lookup(nsc_return_t *out, int maxsize, nsc_call_t *in, time_t now)
191*7c478bd9Sstevel@tonic-gate {
192*7c478bd9Sstevel@tonic-gate 	int		out_of_date;
193*7c478bd9Sstevel@tonic-gate 	nsc_bucket_t	*retb;
194*7c478bd9Sstevel@tonic-gate 	char 		**bucket;
195*7c478bd9Sstevel@tonic-gate 	char		*p_name;
196*7c478bd9Sstevel@tonic-gate 	char		*p_type;
197*7c478bd9Sstevel@tonic-gate 	char		*p_id;
198*7c478bd9Sstevel@tonic-gate 	char		*unique;
199*7c478bd9Sstevel@tonic-gate 	char 		*lasts;
200*7c478bd9Sstevel@tonic-gate 	char		*sep = ":";
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate 	static time_t	lastmod;
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate 	int bufferspace = maxsize - sizeof (nsc_return_t);
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	if (current_admin.exec.nsc_enabled == 0) {
207*7c478bd9Sstevel@tonic-gate 		out->nsc_return_code = NOSERVER;
208*7c478bd9Sstevel@tonic-gate 		out->nsc_bufferbytesused = sizeof (*out);
209*7c478bd9Sstevel@tonic-gate 		return;
210*7c478bd9Sstevel@tonic-gate 	}
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 	mutex_lock(&db_lock);
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate 	if (current_admin.exec.nsc_check_files) {
215*7c478bd9Sstevel@tonic-gate 		struct stat buf;
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate 		if (stat(EXECATTR_FILENAME, &buf) < 0) {
218*7c478bd9Sstevel@tonic-gate 			/*EMPTY*/;
219*7c478bd9Sstevel@tonic-gate 		} else if (lastmod == 0) {
220*7c478bd9Sstevel@tonic-gate 			lastmod = buf.st_mtime;
221*7c478bd9Sstevel@tonic-gate 		} else if (lastmod < buf.st_mtime) {
222*7c478bd9Sstevel@tonic-gate 			getexec_invalidate_unlocked();
223*7c478bd9Sstevel@tonic-gate 			lastmod = buf.st_mtime;
224*7c478bd9Sstevel@tonic-gate 		}
225*7c478bd9Sstevel@tonic-gate 	}
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_ALL) {
228*7c478bd9Sstevel@tonic-gate 		logit("getexec_lookup: looking for name %s\n",
229*7c478bd9Sstevel@tonic-gate 				in->nsc_u.name);
230*7c478bd9Sstevel@tonic-gate 	}
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate 	for (;;) {
233*7c478bd9Sstevel@tonic-gate 		if (attr_strlen(in->nsc_u.name) > NSCDMAXNAMELEN) {
234*7c478bd9Sstevel@tonic-gate 			ucred_t *uc = NULL;
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate 			if (door_ucred(&uc) != 0) {
237*7c478bd9Sstevel@tonic-gate 				logit("getexec_lookup: Name too long, "
238*7c478bd9Sstevel@tonic-gate 				    "but no user credential: %s\n",
239*7c478bd9Sstevel@tonic-gate 				    strerror(errno));
240*7c478bd9Sstevel@tonic-gate 			} else {
241*7c478bd9Sstevel@tonic-gate 				logit("getexec_lookup: Name too long from pid "
242*7c478bd9Sstevel@tonic-gate 				    "%d uid %d\n", ucred_getpid(uc),
243*7c478bd9Sstevel@tonic-gate 				    ucred_getruid(uc));
244*7c478bd9Sstevel@tonic-gate 				ucred_free(uc);
245*7c478bd9Sstevel@tonic-gate 			}
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate 			out->nsc_errno = NSS_NOTFOUND;
248*7c478bd9Sstevel@tonic-gate 			out->nsc_return_code = NOTFOUND;
249*7c478bd9Sstevel@tonic-gate 			out->nsc_bufferbytesused = sizeof (*out);
250*7c478bd9Sstevel@tonic-gate 			goto getout;
251*7c478bd9Sstevel@tonic-gate 		}
252*7c478bd9Sstevel@tonic-gate 		bucket = get_hash(nam_hash, in->nsc_u.name);
253*7c478bd9Sstevel@tonic-gate 
254*7c478bd9Sstevel@tonic-gate 		if (*bucket == (char *)-1) {	/* pending lookup */
255*7c478bd9Sstevel@tonic-gate 			if (get_clearance(in->nsc_callnumber) != 0) {
256*7c478bd9Sstevel@tonic-gate 			    /* no threads available */
257*7c478bd9Sstevel@tonic-gate 				out->nsc_return_code = NOSERVER;
258*7c478bd9Sstevel@tonic-gate 				/* cannot process now */
259*7c478bd9Sstevel@tonic-gate 				out->nsc_bufferbytesused =
260*7c478bd9Sstevel@tonic-gate 				    sizeof (*out);
261*7c478bd9Sstevel@tonic-gate 				current_admin.exec.nsc_throttle_count++;
262*7c478bd9Sstevel@tonic-gate 				goto getout;
263*7c478bd9Sstevel@tonic-gate 			}
264*7c478bd9Sstevel@tonic-gate 			nscd_wait(&db_wait, &db_lock, bucket);
265*7c478bd9Sstevel@tonic-gate 			release_clearance(in->nsc_callnumber);
266*7c478bd9Sstevel@tonic-gate 			continue; /* go back and relookup hash bucket */
267*7c478bd9Sstevel@tonic-gate 		}
268*7c478bd9Sstevel@tonic-gate 		break;
269*7c478bd9Sstevel@tonic-gate 	}
270*7c478bd9Sstevel@tonic-gate 
271*7c478bd9Sstevel@tonic-gate 	/*
272*7c478bd9Sstevel@tonic-gate 	 * check for no name_service mode
273*7c478bd9Sstevel@tonic-gate 	 */
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate 	if (*bucket == NULL && current_admin.avoid_nameservice) {
276*7c478bd9Sstevel@tonic-gate 		out->nsc_return_code = NOTFOUND;
277*7c478bd9Sstevel@tonic-gate 		out->nsc_bufferbytesused = sizeof (*out);
278*7c478bd9Sstevel@tonic-gate 	} else if ((*bucket == NULL) || /* New entry in name service */
279*7c478bd9Sstevel@tonic-gate 	    (in->nsc_callnumber & UPDATEBIT) || /* needs updating */
280*7c478bd9Sstevel@tonic-gate 	    (out_of_date = (!current_admin.avoid_nameservice &&
281*7c478bd9Sstevel@tonic-gate 	    (current_admin.exec.nsc_old_data_ok == 0) &&
282*7c478bd9Sstevel@tonic-gate 	    (((nsc_bucket_t *)*bucket)->nsc_timestamp < now)))) {
283*7c478bd9Sstevel@tonic-gate 		/* time has expired */
284*7c478bd9Sstevel@tonic-gate 		int saved_errno;
285*7c478bd9Sstevel@tonic-gate 		int saved_hits = 0;
286*7c478bd9Sstevel@tonic-gate 		execstr_t *p;
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate 		if (get_clearance(in->nsc_callnumber) != 0) {
289*7c478bd9Sstevel@tonic-gate 		    /* no threads available */
290*7c478bd9Sstevel@tonic-gate 			out->nsc_return_code = NOSERVER;
291*7c478bd9Sstevel@tonic-gate 			    /* cannot process now */
292*7c478bd9Sstevel@tonic-gate 			out->nsc_bufferbytesused = sizeof (*out);
293*7c478bd9Sstevel@tonic-gate 			current_admin.exec.nsc_throttle_count++;
294*7c478bd9Sstevel@tonic-gate 			goto getout;
295*7c478bd9Sstevel@tonic-gate 		}
296*7c478bd9Sstevel@tonic-gate 
297*7c478bd9Sstevel@tonic-gate 		if (*bucket != NULL) {
298*7c478bd9Sstevel@tonic-gate 			saved_hits =
299*7c478bd9Sstevel@tonic-gate 			    ((nsc_bucket_t *)*bucket)->nsc_hits;
300*7c478bd9Sstevel@tonic-gate 		}
301*7c478bd9Sstevel@tonic-gate 
302*7c478bd9Sstevel@tonic-gate 		/*
303*7c478bd9Sstevel@tonic-gate 		 *  block any threads accessing this bucket if data is
304*7c478bd9Sstevel@tonic-gate 		 *  non-existent out of date
305*7c478bd9Sstevel@tonic-gate 		 */
306*7c478bd9Sstevel@tonic-gate 
307*7c478bd9Sstevel@tonic-gate 		if (*bucket == NULL || out_of_date) {
308*7c478bd9Sstevel@tonic-gate 			update_exec_bucket((nsc_bucket_t **)bucket,
309*7c478bd9Sstevel@tonic-gate 			    (nsc_bucket_t *)-1,
310*7c478bd9Sstevel@tonic-gate 			    in->nsc_callnumber);
311*7c478bd9Sstevel@tonic-gate 		} else {
312*7c478bd9Sstevel@tonic-gate 		/*
313*7c478bd9Sstevel@tonic-gate 		 * if still not -1 bucket we are doing update...
314*7c478bd9Sstevel@tonic-gate 		 * mark to prevent
315*7c478bd9Sstevel@tonic-gate 		 * pileups of threads if the name service is hanging....
316*7c478bd9Sstevel@tonic-gate 		 */
317*7c478bd9Sstevel@tonic-gate 			((nsc_bucket_t *)(*bucket))->nsc_status |=
318*7c478bd9Sstevel@tonic-gate 			    ST_UPDATE_PENDING;
319*7c478bd9Sstevel@tonic-gate 			/* cleared by deletion of old data */
320*7c478bd9Sstevel@tonic-gate 		}
321*7c478bd9Sstevel@tonic-gate 		mutex_unlock(&db_lock);
322*7c478bd9Sstevel@tonic-gate 
323*7c478bd9Sstevel@tonic-gate 		/*
324*7c478bd9Sstevel@tonic-gate 		 * Call non-caching version in libnsl.
325*7c478bd9Sstevel@tonic-gate 		 */
326*7c478bd9Sstevel@tonic-gate 		unique = strdup(in->nsc_u.name);
327*7c478bd9Sstevel@tonic-gate 		p_name = strtok_r(unique, sep, &lasts);
328*7c478bd9Sstevel@tonic-gate 		p_type = strtok_r(NULL, sep, &lasts);
329*7c478bd9Sstevel@tonic-gate 		p_id = strtok_r(NULL, sep, &lasts);
330*7c478bd9Sstevel@tonic-gate 		p = _getexecprof(p_name,
331*7c478bd9Sstevel@tonic-gate 		    p_type,
332*7c478bd9Sstevel@tonic-gate 		    p_id,
333*7c478bd9Sstevel@tonic-gate 		    GET_ONE,
334*7c478bd9Sstevel@tonic-gate 		    &out->nsc_u.exec,
335*7c478bd9Sstevel@tonic-gate 		    out->nsc_u.buff + sizeof (execstr_t),
336*7c478bd9Sstevel@tonic-gate 		    bufferspace,
337*7c478bd9Sstevel@tonic-gate 		    &saved_errno);
338*7c478bd9Sstevel@tonic-gate 
339*7c478bd9Sstevel@tonic-gate 		free(unique);
340*7c478bd9Sstevel@tonic-gate 		mutex_lock(&db_lock);
341*7c478bd9Sstevel@tonic-gate 
342*7c478bd9Sstevel@tonic-gate 		release_clearance(in->nsc_callnumber);
343*7c478bd9Sstevel@tonic-gate 
344*7c478bd9Sstevel@tonic-gate 		if (p == NULL) { /* data not found */
345*7c478bd9Sstevel@tonic-gate 
346*7c478bd9Sstevel@tonic-gate 			if (current_admin.debug_level >= DBG_CANT_FIND) {
347*7c478bd9Sstevel@tonic-gate 		logit("getexec_lookup: nscd COULDN'T FIND exec_attr name %s\n",
348*7c478bd9Sstevel@tonic-gate 						in->nsc_u.name);
349*7c478bd9Sstevel@tonic-gate 			}
350*7c478bd9Sstevel@tonic-gate 
351*7c478bd9Sstevel@tonic-gate 			if (!(UPDATEBIT & in->nsc_callnumber))
352*7c478bd9Sstevel@tonic-gate 			    current_admin.exec.nsc_neg_cache_misses++;
353*7c478bd9Sstevel@tonic-gate 
354*7c478bd9Sstevel@tonic-gate 			retb = (nsc_bucket_t *)malloc(sizeof (nsc_bucket_t));
355*7c478bd9Sstevel@tonic-gate 
356*7c478bd9Sstevel@tonic-gate 			retb->nsc_refcount = 1;
357*7c478bd9Sstevel@tonic-gate 			retb->nsc_data.nsc_bufferbytesused =
358*7c478bd9Sstevel@tonic-gate 				sizeof (nsc_return_t);
359*7c478bd9Sstevel@tonic-gate 			retb->nsc_data.nsc_return_code = NOTFOUND;
360*7c478bd9Sstevel@tonic-gate 			retb->nsc_data.nsc_errno = saved_errno;
361*7c478bd9Sstevel@tonic-gate 			memcpy(out, &retb->nsc_data,
362*7c478bd9Sstevel@tonic-gate 			    retb->nsc_data.nsc_bufferbytesused);
363*7c478bd9Sstevel@tonic-gate 			update_exec_bucket((nsc_bucket_t **)bucket,
364*7c478bd9Sstevel@tonic-gate 			    retb, in->nsc_callnumber);
365*7c478bd9Sstevel@tonic-gate 			goto getout;
366*7c478bd9Sstevel@tonic-gate 		} else {
367*7c478bd9Sstevel@tonic-gate 			if (current_admin.debug_level >= DBG_ALL) {
368*7c478bd9Sstevel@tonic-gate 		logit("getexec_lookup: nscd FOUND exec_attr name %s\n",
369*7c478bd9Sstevel@tonic-gate 						in->nsc_u.name);
370*7c478bd9Sstevel@tonic-gate 			}
371*7c478bd9Sstevel@tonic-gate 			if (!(UPDATEBIT & in->nsc_callnumber))
372*7c478bd9Sstevel@tonic-gate 			    current_admin.exec.nsc_pos_cache_misses++;
373*7c478bd9Sstevel@tonic-gate 			retb = fixbuffer(out, bufferspace);
374*7c478bd9Sstevel@tonic-gate 			update_exec_bucket((nsc_bucket_t **)bucket,
375*7c478bd9Sstevel@tonic-gate 			    retb, in->nsc_callnumber);
376*7c478bd9Sstevel@tonic-gate 			if (saved_hits)
377*7c478bd9Sstevel@tonic-gate 				retb->nsc_hits = saved_hits;
378*7c478bd9Sstevel@tonic-gate 		}
379*7c478bd9Sstevel@tonic-gate 	} else { 	/* found entry in cache */
380*7c478bd9Sstevel@tonic-gate 		retb = (nsc_bucket_t *)*bucket;
381*7c478bd9Sstevel@tonic-gate 		retb->nsc_hits++;
382*7c478bd9Sstevel@tonic-gate 		memcpy(out, &(retb->nsc_data),
383*7c478bd9Sstevel@tonic-gate 		    retb->nsc_data.nsc_bufferbytesused);
384*7c478bd9Sstevel@tonic-gate 		if (out->nsc_return_code == SUCCESS) {
385*7c478bd9Sstevel@tonic-gate 			if (!(UPDATEBIT & in->nsc_callnumber))
386*7c478bd9Sstevel@tonic-gate 			    current_admin.exec.nsc_pos_cache_hits++;
387*7c478bd9Sstevel@tonic-gate 			if (current_admin.debug_level >= DBG_ALL) {
388*7c478bd9Sstevel@tonic-gate 			logit("getexec_lookup: found name %s in cache\n",
389*7c478bd9Sstevel@tonic-gate 						in->nsc_u.name);
390*7c478bd9Sstevel@tonic-gate 			}
391*7c478bd9Sstevel@tonic-gate 		} else {
392*7c478bd9Sstevel@tonic-gate 			if (!(UPDATEBIT & in->nsc_callnumber))
393*7c478bd9Sstevel@tonic-gate 			    current_admin.exec.nsc_neg_cache_hits++;
394*7c478bd9Sstevel@tonic-gate 			if (current_admin.debug_level >= DBG_ALL) {
395*7c478bd9Sstevel@tonic-gate 		logit("getexec_lookup: %s marked as NOT FOUND in cache.\n",
396*7c478bd9Sstevel@tonic-gate 						in->nsc_u.name);
397*7c478bd9Sstevel@tonic-gate 			}
398*7c478bd9Sstevel@tonic-gate 		}
399*7c478bd9Sstevel@tonic-gate 
400*7c478bd9Sstevel@tonic-gate 		if ((retb->nsc_timestamp < now) &&
401*7c478bd9Sstevel@tonic-gate 		    !(in->nsc_callnumber & UPDATEBIT) &&
402*7c478bd9Sstevel@tonic-gate 		    !(retb->nsc_status & ST_UPDATE_PENDING)) {
403*7c478bd9Sstevel@tonic-gate 		logit("launch update since time = %d\n", retb->nsc_timestamp);
404*7c478bd9Sstevel@tonic-gate 			retb->nsc_status |= ST_UPDATE_PENDING;
405*7c478bd9Sstevel@tonic-gate 			/* cleared by deletion of old data */
406*7c478bd9Sstevel@tonic-gate 			launch_update(in);
407*7c478bd9Sstevel@tonic-gate 		}
408*7c478bd9Sstevel@tonic-gate 	}
409*7c478bd9Sstevel@tonic-gate 
410*7c478bd9Sstevel@tonic-gate getout:
411*7c478bd9Sstevel@tonic-gate 	mutex_unlock(&db_lock);
412*7c478bd9Sstevel@tonic-gate }
413*7c478bd9Sstevel@tonic-gate 
414*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
415*7c478bd9Sstevel@tonic-gate static int
416*7c478bd9Sstevel@tonic-gate update_exec_bucket(nsc_bucket_t **old, nsc_bucket_t *new, int callnumber)
417*7c478bd9Sstevel@tonic-gate {
418*7c478bd9Sstevel@tonic-gate 	if (*old != NULL && *old != (nsc_bucket_t *)-1) { /* old data exists */
419*7c478bd9Sstevel@tonic-gate 		free(*old);
420*7c478bd9Sstevel@tonic-gate 		current_admin.exec.nsc_entries--;
421*7c478bd9Sstevel@tonic-gate 	}
422*7c478bd9Sstevel@tonic-gate 
423*7c478bd9Sstevel@tonic-gate 	/*
424*7c478bd9Sstevel@tonic-gate 	 *  we can do this before reseting *old since we're holding the lock
425*7c478bd9Sstevel@tonic-gate 	 */
426*7c478bd9Sstevel@tonic-gate 
427*7c478bd9Sstevel@tonic-gate 	else if (*old == (nsc_bucket_t *)-1) {
428*7c478bd9Sstevel@tonic-gate 		nscd_signal(&db_wait, (char **)old);
429*7c478bd9Sstevel@tonic-gate 	}
430*7c478bd9Sstevel@tonic-gate 
431*7c478bd9Sstevel@tonic-gate 	*old = new;
432*7c478bd9Sstevel@tonic-gate 
433*7c478bd9Sstevel@tonic-gate 	if ((new != NULL) && (new != (nsc_bucket_t *)-1)) {
434*7c478bd9Sstevel@tonic-gate 		/* real data, not just update pending or invalidate */
435*7c478bd9Sstevel@tonic-gate 		new->nsc_hits = 1;
436*7c478bd9Sstevel@tonic-gate 		new->nsc_status = 0;
437*7c478bd9Sstevel@tonic-gate 		new->nsc_refcount = 1;
438*7c478bd9Sstevel@tonic-gate 		current_admin.exec.nsc_entries++;
439*7c478bd9Sstevel@tonic-gate 
440*7c478bd9Sstevel@tonic-gate 		if (new->nsc_data.nsc_return_code == SUCCESS) {
441*7c478bd9Sstevel@tonic-gate 			new->nsc_timestamp = time(NULL) +
442*7c478bd9Sstevel@tonic-gate 			    current_admin.exec.nsc_pos_ttl;
443*7c478bd9Sstevel@tonic-gate 		} else {
444*7c478bd9Sstevel@tonic-gate 			new->nsc_timestamp = time(NULL) +
445*7c478bd9Sstevel@tonic-gate 			    current_admin.exec.nsc_neg_ttl;
446*7c478bd9Sstevel@tonic-gate 		}
447*7c478bd9Sstevel@tonic-gate 	}
448*7c478bd9Sstevel@tonic-gate 	return (0);
449*7c478bd9Sstevel@tonic-gate }
450*7c478bd9Sstevel@tonic-gate 
451*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
452*7c478bd9Sstevel@tonic-gate static nsc_bucket_t *
453*7c478bd9Sstevel@tonic-gate fixbuffer(nsc_return_t *in, int maxlen)
454*7c478bd9Sstevel@tonic-gate {
455*7c478bd9Sstevel@tonic-gate 	nsc_bucket_t *retb;
456*7c478bd9Sstevel@tonic-gate 	nsc_return_t *out;
457*7c478bd9Sstevel@tonic-gate 	char 	*dest;
458*7c478bd9Sstevel@tonic-gate 	int 	offset;
459*7c478bd9Sstevel@tonic-gate 	int 	strs;
460*7c478bd9Sstevel@tonic-gate 
461*7c478bd9Sstevel@tonic-gate 	/*
462*7c478bd9Sstevel@tonic-gate 	 * find out the size of the data block we're going to need
463*7c478bd9Sstevel@tonic-gate 	 */
464*7c478bd9Sstevel@tonic-gate 
465*7c478bd9Sstevel@tonic-gate 	strs = attr_strlen(in->nsc_u.exec.name) +
466*7c478bd9Sstevel@tonic-gate 	    attr_strlen(in->nsc_u.exec.policy) +
467*7c478bd9Sstevel@tonic-gate 	    attr_strlen(in->nsc_u.exec.type) +
468*7c478bd9Sstevel@tonic-gate 	    attr_strlen(in->nsc_u.exec.res1) +
469*7c478bd9Sstevel@tonic-gate 	    attr_strlen(in->nsc_u.exec.res2) +
470*7c478bd9Sstevel@tonic-gate 	    attr_strlen(in->nsc_u.exec.id) +
471*7c478bd9Sstevel@tonic-gate 	    attr_strlen(in->nsc_u.exec.attr) + EXECATTR_DB_NCOL;
472*7c478bd9Sstevel@tonic-gate 
473*7c478bd9Sstevel@tonic-gate 	/*
474*7c478bd9Sstevel@tonic-gate 	 * allocate it and copy it in
475*7c478bd9Sstevel@tonic-gate 	 * code doesn't assume packing order in original buffer
476*7c478bd9Sstevel@tonic-gate 	 */
477*7c478bd9Sstevel@tonic-gate 
478*7c478bd9Sstevel@tonic-gate 	if ((retb = (nsc_bucket_t *)malloc(sizeof (*retb) + strs)) == NULL) {
479*7c478bd9Sstevel@tonic-gate 		return (NULL);
480*7c478bd9Sstevel@tonic-gate 	}
481*7c478bd9Sstevel@tonic-gate 
482*7c478bd9Sstevel@tonic-gate 	out = &(retb->nsc_data);
483*7c478bd9Sstevel@tonic-gate 	out->nsc_bufferbytesused = strs + ((int)&out->nsc_u.exec - (int)out) +
484*7c478bd9Sstevel@tonic-gate 	    sizeof (execstr_t);
485*7c478bd9Sstevel@tonic-gate 	out->nsc_return_code 	= SUCCESS;
486*7c478bd9Sstevel@tonic-gate 	out->nsc_errno 		= 0;
487*7c478bd9Sstevel@tonic-gate 
488*7c478bd9Sstevel@tonic-gate 	dest = retb->nsc_data.nsc_u.buff + sizeof (execstr_t);
489*7c478bd9Sstevel@tonic-gate 	offset = (int)dest;
490*7c478bd9Sstevel@tonic-gate 
491*7c478bd9Sstevel@tonic-gate 	attr_strcpy(dest, in->nsc_u.exec.name);
492*7c478bd9Sstevel@tonic-gate 	strs = 1 + attr_strlen(in->nsc_u.exec.name);
493*7c478bd9Sstevel@tonic-gate 	out->nsc_u.exec.name = dest - offset;
494*7c478bd9Sstevel@tonic-gate 	dest += strs;
495*7c478bd9Sstevel@tonic-gate 
496*7c478bd9Sstevel@tonic-gate 	attr_strcpy(dest, in->nsc_u.exec.policy);
497*7c478bd9Sstevel@tonic-gate 	strs = 1 + attr_strlen(in->nsc_u.exec.policy);
498*7c478bd9Sstevel@tonic-gate 	out->nsc_u.exec.policy = dest - offset;
499*7c478bd9Sstevel@tonic-gate 	dest += strs;
500*7c478bd9Sstevel@tonic-gate 
501*7c478bd9Sstevel@tonic-gate 	attr_strcpy(dest, in->nsc_u.exec.type);
502*7c478bd9Sstevel@tonic-gate 	strs = 1 + attr_strlen(in->nsc_u.exec.type);
503*7c478bd9Sstevel@tonic-gate 	out->nsc_u.exec.type = dest - offset;
504*7c478bd9Sstevel@tonic-gate 	dest += strs;
505*7c478bd9Sstevel@tonic-gate 
506*7c478bd9Sstevel@tonic-gate 	attr_strcpy(dest, in->nsc_u.exec.res1);
507*7c478bd9Sstevel@tonic-gate 	strs = 1 + attr_strlen(in->nsc_u.exec.res1);
508*7c478bd9Sstevel@tonic-gate 	out->nsc_u.exec.res1 = dest - offset;
509*7c478bd9Sstevel@tonic-gate 	dest += strs;
510*7c478bd9Sstevel@tonic-gate 
511*7c478bd9Sstevel@tonic-gate 	attr_strcpy(dest, in->nsc_u.exec.res2);
512*7c478bd9Sstevel@tonic-gate 	strs = 1 + attr_strlen(in->nsc_u.exec.res2);
513*7c478bd9Sstevel@tonic-gate 	out->nsc_u.exec.res2 = dest - offset;
514*7c478bd9Sstevel@tonic-gate 	dest += strs;
515*7c478bd9Sstevel@tonic-gate 
516*7c478bd9Sstevel@tonic-gate 	attr_strcpy(dest, in->nsc_u.exec.id);
517*7c478bd9Sstevel@tonic-gate 	strs = 1 + attr_strlen(in->nsc_u.exec.id);
518*7c478bd9Sstevel@tonic-gate 	out->nsc_u.exec.id = dest - offset;
519*7c478bd9Sstevel@tonic-gate 	dest += strs;
520*7c478bd9Sstevel@tonic-gate 
521*7c478bd9Sstevel@tonic-gate 	attr_strcpy(dest, in->nsc_u.exec.attr);
522*7c478bd9Sstevel@tonic-gate 	out->nsc_u.exec.attr = dest - offset;
523*7c478bd9Sstevel@tonic-gate 
524*7c478bd9Sstevel@tonic-gate 	memcpy(in, out, out->nsc_bufferbytesused);
525*7c478bd9Sstevel@tonic-gate 
526*7c478bd9Sstevel@tonic-gate 	return (retb);
527*7c478bd9Sstevel@tonic-gate }
528*7c478bd9Sstevel@tonic-gate 
529*7c478bd9Sstevel@tonic-gate void
530*7c478bd9Sstevel@tonic-gate getexec_reaper(void)
531*7c478bd9Sstevel@tonic-gate {
532*7c478bd9Sstevel@tonic-gate 	nsc_reaper("getexec", nam_hash, &current_admin.exec, &db_lock);
533*7c478bd9Sstevel@tonic-gate }
534