xref: /illumos-gate/usr/src/cmd/nscd/server.c (revision cb5caa98)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2245916cd2Sjpk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * Simple doors name server cache daemon
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
347c478bd9Sstevel@tonic-gate #include <fcntl.h>
35*cb5caa98Sdjl #include <string.h>
36*cb5caa98Sdjl #include <errno.h>
37*cb5caa98Sdjl #include <stdarg.h>
38*cb5caa98Sdjl #include <locale.h>
3945916cd2Sjpk #include <tsol/label.h>
4045916cd2Sjpk #include <zone.h>
41*cb5caa98Sdjl #include "cache.h"
42*cb5caa98Sdjl #include "nscd_log.h"
43*cb5caa98Sdjl #include "nscd_selfcred.h"
44*cb5caa98Sdjl #include "nscd_frontend.h"
45*cb5caa98Sdjl #include "nscd_common.h"
46*cb5caa98Sdjl #include "nscd_admin.h"
47*cb5caa98Sdjl #include "nscd_door.h"
48*cb5caa98Sdjl #include "nscd_switch.h"
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate extern int 	optind;
517c478bd9Sstevel@tonic-gate extern int 	opterr;
527c478bd9Sstevel@tonic-gate extern int 	optopt;
537c478bd9Sstevel@tonic-gate extern char 	*optarg;
547c478bd9Sstevel@tonic-gate 
55*cb5caa98Sdjl #define	NSCDOPT	"S:Kf:c:ge:p:n:i:l:d:s:h:o:GFR"
56*cb5caa98Sdjl 
57*cb5caa98Sdjl /* assume this is a single nscd  or, if multiple, the main nscd */
58*cb5caa98Sdjl int		_whoami = NSCD_MAIN;
59*cb5caa98Sdjl int		_doorfd = -1;
60*cb5caa98Sdjl extern int	_logfd;
61*cb5caa98Sdjl static char	*cfgfile = NULL;
62*cb5caa98Sdjl 
63*cb5caa98Sdjl extern nsc_ctx_t *cache_ctx_p[];
64*cb5caa98Sdjl 
657c478bd9Sstevel@tonic-gate static void usage(char *);
667c478bd9Sstevel@tonic-gate static void detachfromtty(void);
677c478bd9Sstevel@tonic-gate 
68*cb5caa98Sdjl static int	debug_level = 0;
69*cb5caa98Sdjl static char	logfile[128] = { 0 };
707c478bd9Sstevel@tonic-gate static int	will_become_server;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate static char *
737c478bd9Sstevel@tonic-gate getcacheopt(char *s)
747c478bd9Sstevel@tonic-gate {
757c478bd9Sstevel@tonic-gate 	while (*s && *s != ',')
767c478bd9Sstevel@tonic-gate 		s++;
777c478bd9Sstevel@tonic-gate 	return ((*s == ',') ? (s + 1) : NULL);
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate  * declaring this causes the files backend to use hashing
827c478bd9Sstevel@tonic-gate  * this is of course an utter hack, but provides a nice
837c478bd9Sstevel@tonic-gate  * quiet back door to enable this feature for only the nscd.
847c478bd9Sstevel@tonic-gate  */
857c478bd9Sstevel@tonic-gate void
867c478bd9Sstevel@tonic-gate __nss_use_files_hash(void)
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
90*cb5caa98Sdjl static int	saved_argc = 0;
91*cb5caa98Sdjl static char	**saved_argv = NULL;
927c478bd9Sstevel@tonic-gate static char	saved_execname[MAXPATHLEN];
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate static void
957c478bd9Sstevel@tonic-gate save_execname()
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate 	const char *name = getexecname();
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	saved_execname[0] = 0;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	if (name[0] != '/') { /* started w/ relative path */
1027c478bd9Sstevel@tonic-gate 		(void) getcwd(saved_execname, MAXPATHLEN);
103*cb5caa98Sdjl 		(void) strlcat(saved_execname, "/", MAXPATHLEN);
1047c478bd9Sstevel@tonic-gate 	}
105*cb5caa98Sdjl 	(void) strlcat(saved_execname, name, MAXPATHLEN);
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate 
108f166393fSesolom int
1097c478bd9Sstevel@tonic-gate main(int argc, char ** argv)
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate 	int		opt;
1127c478bd9Sstevel@tonic-gate 	int		errflg = 0;
1137c478bd9Sstevel@tonic-gate 	int		showstats = 0;
1147c478bd9Sstevel@tonic-gate 	int		doset = 0;
115*cb5caa98Sdjl 	nscd_rc_t	rc;
116*cb5caa98Sdjl 	char		*me = "main()";
117*cb5caa98Sdjl 	char		*ret_locale;
118*cb5caa98Sdjl 	char		*ret_textdomain;
119*cb5caa98Sdjl 	char		msg[128];
120*cb5caa98Sdjl 
121*cb5caa98Sdjl 	ret_locale = setlocale(LC_ALL, "");
122*cb5caa98Sdjl 	if (ret_locale == NULL)
123*cb5caa98Sdjl 		(void) fprintf(stderr, gettext("Unable to set locale\n"));
124*cb5caa98Sdjl 
125*cb5caa98Sdjl 	ret_textdomain = textdomain(TEXT_DOMAIN);
126*cb5caa98Sdjl 	if (ret_textdomain == NULL)
127*cb5caa98Sdjl 		(void) fprintf(stderr, gettext("Unable to set textdomain\n"));
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	/*
13045916cd2Sjpk 	 * The admin model for TX is that labeled zones are managed
13145916cd2Sjpk 	 * in global zone where most trusted configuration database
13245916cd2Sjpk 	 * resides.
13345916cd2Sjpk 	 */
13445916cd2Sjpk 	if (is_system_labeled() && (getzoneid() != GLOBAL_ZONEID)) {
13545916cd2Sjpk 		(void) fprintf(stderr,
136*cb5caa98Sdjl gettext("With Trusted Extensions nscd runs only in the global zone.\n"));
13745916cd2Sjpk 		exit(1);
13845916cd2Sjpk 	}
13945916cd2Sjpk 
14045916cd2Sjpk 	/*
1417c478bd9Sstevel@tonic-gate 	 *  Special case non-root user here - he can just print stats
1427c478bd9Sstevel@tonic-gate 	 */
1437c478bd9Sstevel@tonic-gate 	if (geteuid()) {
144*cb5caa98Sdjl 		if (argc != 2 ||
145*cb5caa98Sdjl 			(strcmp(argv[1], "-g") && strcmp(argv[1], "-G"))) {
1467c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
147*cb5caa98Sdjl 	gettext("Must be root to use any option other than -g\n\n"));
1487c478bd9Sstevel@tonic-gate 			usage(argv[0]);
1497c478bd9Sstevel@tonic-gate 		}
1507c478bd9Sstevel@tonic-gate 
151*cb5caa98Sdjl 		if (_nscd_doorcall(NSCD_PING) != NSS_SUCCESS) {
1527c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
153*cb5caa98Sdjl 			gettext("%s doesn't appear to be running.\n"),
154*cb5caa98Sdjl 				argv[0]);
1557c478bd9Sstevel@tonic-gate 			exit(1);
1567c478bd9Sstevel@tonic-gate 		}
157*cb5caa98Sdjl 		if (_nscd_client_getadmin(argv[1][1]) != 0) {
158*cb5caa98Sdjl 			(void) fprintf(stderr,
159*cb5caa98Sdjl 	gettext("unable to get configuration and statistics data\n"));
160*cb5caa98Sdjl 			exit(1);
161*cb5caa98Sdjl 		}
162*cb5caa98Sdjl 
163*cb5caa98Sdjl 		_nscd_client_showstats();
1647c478bd9Sstevel@tonic-gate 		exit(0);
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 
167*cb5caa98Sdjl 	/*
168*cb5caa98Sdjl 	 *  Determine if there is already a daemon (main nscd) running.
169*cb5caa98Sdjl 	 *  If not, will start it. Forker NSCD will always become a
170*cb5caa98Sdjl 	 *  daemon.
171*cb5caa98Sdjl 	 */
172*cb5caa98Sdjl 	will_become_server = (_nscd_doorcall(NSCD_PING) != NSS_SUCCESS);
173*cb5caa98Sdjl 	if (argc >= 2 && strcmp(argv[1], "-F") == 0) {
174*cb5caa98Sdjl 		will_become_server = 1;
175*cb5caa98Sdjl 		_whoami = NSCD_FORKER;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 		/*
178*cb5caa98Sdjl 		 * allow time for the main nscd to get ready
179*cb5caa98Sdjl 		 * to receive the IMHERE door request this
180*cb5caa98Sdjl 		 * process will send later
1817c478bd9Sstevel@tonic-gate 		 */
182*cb5caa98Sdjl 		(void) usleep(100000);
183*cb5caa98Sdjl 	}
1847c478bd9Sstevel@tonic-gate 
185*cb5caa98Sdjl 	/*
186*cb5caa98Sdjl 	 * first get the config file path. Also detect
187*cb5caa98Sdjl 	 * invalid option as soon as possible.
188*cb5caa98Sdjl 	 */
189*cb5caa98Sdjl 	while ((opt = getopt(argc, argv, NSCDOPT)) != EOF) {
190*cb5caa98Sdjl 		switch (opt) {
191*cb5caa98Sdjl 
192*cb5caa98Sdjl 		case 'f':
193*cb5caa98Sdjl 			if ((cfgfile = strdup(optarg)) == NULL)
194*cb5caa98Sdjl 				exit(1);
195*cb5caa98Sdjl 			break;
196*cb5caa98Sdjl 		case 'g':
197*cb5caa98Sdjl 			if (will_become_server) {
198*cb5caa98Sdjl 				(void) fprintf(stderr,
199*cb5caa98Sdjl 		gettext("nscd not running, no statistics to show\n\n"));
200*cb5caa98Sdjl 				errflg++;
201*cb5caa98Sdjl 			}
202*cb5caa98Sdjl 			break;
203*cb5caa98Sdjl 		case 'i':
204*cb5caa98Sdjl 			if (will_become_server) {
205*cb5caa98Sdjl 				(void) fprintf(stderr,
206*cb5caa98Sdjl 		gettext("nscd not running, no cache to invalidate\n\n"));
207*cb5caa98Sdjl 				errflg++;
208*cb5caa98Sdjl 			}
209*cb5caa98Sdjl 			break;
210*cb5caa98Sdjl 
211*cb5caa98Sdjl 		case '?':
212*cb5caa98Sdjl 			errflg++;
213*cb5caa98Sdjl 			break;
214*cb5caa98Sdjl 		}
215*cb5caa98Sdjl 
216*cb5caa98Sdjl 	}
217*cb5caa98Sdjl 	if (errflg)
218*cb5caa98Sdjl 	    usage(argv[0]);
219*cb5caa98Sdjl 
220*cb5caa98Sdjl 	/*
221*cb5caa98Sdjl 	 *  perform more initialization and load configuration
222*cb5caa98Sdjl 	 * if to become server
223*cb5caa98Sdjl 	 */
224*cb5caa98Sdjl 	if (will_become_server) {
225*cb5caa98Sdjl 
226*cb5caa98Sdjl 		/* initialize switch engine and config/stats management */
227*cb5caa98Sdjl 		if ((rc = _nscd_init(cfgfile)) != NSCD_SUCCESS) {
228*cb5caa98Sdjl 			(void) fprintf(stderr,
229*cb5caa98Sdjl 		gettext("initialization of switch failed (rc = %d)\n"), rc);
230*cb5caa98Sdjl 			exit(1);
231*cb5caa98Sdjl 		}
232*cb5caa98Sdjl 
233*cb5caa98Sdjl 		/*
234*cb5caa98Sdjl 		 * initialize cache store
235*cb5caa98Sdjl 		 */
236*cb5caa98Sdjl 		if ((rc = init_cache(0)) != NSCD_SUCCESS) {
237*cb5caa98Sdjl 			(void) fprintf(stderr,
238*cb5caa98Sdjl 	gettext("initialization of cache store failed (rc = %d)\n"), rc);
239*cb5caa98Sdjl 			exit(1);
240*cb5caa98Sdjl 		}
241*cb5caa98Sdjl 	}
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	/*
2447c478bd9Sstevel@tonic-gate 	 * process usual options
2457c478bd9Sstevel@tonic-gate 	 */
246*cb5caa98Sdjl 	optind = 1; /* this is a rescan */
247*cb5caa98Sdjl 	*msg = '\0';
248*cb5caa98Sdjl 	while ((opt = getopt(argc, argv, NSCDOPT)) != EOF) {
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 		switch (opt) {
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 		case 'K':		/* undocumented feature */
253*cb5caa98Sdjl 			(void) _nscd_doorcall(NSCD_KILLSERVER);
2547c478bd9Sstevel@tonic-gate 			exit(0);
2557c478bd9Sstevel@tonic-gate 			break;
2567c478bd9Sstevel@tonic-gate 
257*cb5caa98Sdjl 		case 'G':
2587c478bd9Sstevel@tonic-gate 		case 'g':
2597c478bd9Sstevel@tonic-gate 			showstats++;
2607c478bd9Sstevel@tonic-gate 			break;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 		case 'p':
2637c478bd9Sstevel@tonic-gate 			doset++;
264*cb5caa98Sdjl 			if (_nscd_add_admin_mod(optarg, 'p',
265*cb5caa98Sdjl 				getcacheopt(optarg),
266*cb5caa98Sdjl 				msg, sizeof (msg)) == -1)
2677c478bd9Sstevel@tonic-gate 				errflg++;
2687c478bd9Sstevel@tonic-gate 			break;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 		case 'n':
2717c478bd9Sstevel@tonic-gate 			doset++;
272*cb5caa98Sdjl 			if (_nscd_add_admin_mod(optarg, 'n',
273*cb5caa98Sdjl 				getcacheopt(optarg),
274*cb5caa98Sdjl 				msg, sizeof (msg)) == -1)
2757c478bd9Sstevel@tonic-gate 				errflg++;
2767c478bd9Sstevel@tonic-gate 			break;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 		case 'c':
2797c478bd9Sstevel@tonic-gate 			doset++;
280*cb5caa98Sdjl 			if (_nscd_add_admin_mod(optarg, 'c',
281*cb5caa98Sdjl 				getcacheopt(optarg),
282*cb5caa98Sdjl 				msg, sizeof (msg)) == -1)
2837c478bd9Sstevel@tonic-gate 				errflg++;
2847c478bd9Sstevel@tonic-gate 			break;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 		case 'i':
2877c478bd9Sstevel@tonic-gate 			doset++;
288*cb5caa98Sdjl 			if (_nscd_add_admin_mod(optarg, 'i', NULL,
289*cb5caa98Sdjl 				msg, sizeof (msg)) == -1)
2907c478bd9Sstevel@tonic-gate 				errflg++;
2917c478bd9Sstevel@tonic-gate 			break;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 		case 'l':
2947c478bd9Sstevel@tonic-gate 			doset++;
295*cb5caa98Sdjl 			(void) strlcpy(logfile, optarg, 128);
296*cb5caa98Sdjl 			(void) _nscd_add_admin_mod(NULL, 'l', optarg,
297*cb5caa98Sdjl 				msg, sizeof (msg));
2987c478bd9Sstevel@tonic-gate 			break;
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 		case 'd':
3017c478bd9Sstevel@tonic-gate 			doset++;
302*cb5caa98Sdjl 			debug_level = atoi(optarg);
303*cb5caa98Sdjl 			(void) _nscd_add_admin_mod(NULL, 'd', optarg,
304*cb5caa98Sdjl 				msg, sizeof (msg));
305*cb5caa98Sdjl 			break;
306*cb5caa98Sdjl 
307*cb5caa98Sdjl 		case 'S':
308*cb5caa98Sdjl 			/* silently ignore secure-mode */
3097c478bd9Sstevel@tonic-gate 			break;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 		case 's':
312*cb5caa98Sdjl 			/* silently ignore suggested-size */
3137c478bd9Sstevel@tonic-gate 			break;
3147c478bd9Sstevel@tonic-gate 
315*cb5caa98Sdjl 		case 'o':
316*cb5caa98Sdjl 			/* silently ignore old-data-ok */
3177c478bd9Sstevel@tonic-gate 			break;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 		case 'h':
3207c478bd9Sstevel@tonic-gate 			doset++;
321*cb5caa98Sdjl 			if (_nscd_add_admin_mod(optarg, 'h',
322*cb5caa98Sdjl 				getcacheopt(optarg),
323*cb5caa98Sdjl 				msg, sizeof (msg)) == -1)
3247c478bd9Sstevel@tonic-gate 				errflg++;
3257c478bd9Sstevel@tonic-gate 			break;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 		case 'e':
3287c478bd9Sstevel@tonic-gate 			doset++;
329*cb5caa98Sdjl 			if (_nscd_add_admin_mod(optarg, 'e',
330*cb5caa98Sdjl 				getcacheopt(optarg),
331*cb5caa98Sdjl 				msg, sizeof (msg)) == -1)
3327c478bd9Sstevel@tonic-gate 				errflg++;
3337c478bd9Sstevel@tonic-gate 			break;
334*cb5caa98Sdjl 
335*cb5caa98Sdjl 		case 'F':
336*cb5caa98Sdjl 			_whoami = NSCD_FORKER;
3377c478bd9Sstevel@tonic-gate 			break;
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 		default:
3407c478bd9Sstevel@tonic-gate 			errflg++;
3417c478bd9Sstevel@tonic-gate 			break;
3427c478bd9Sstevel@tonic-gate 		}
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	}
3457c478bd9Sstevel@tonic-gate 
346*cb5caa98Sdjl 	if (errflg) {
347*cb5caa98Sdjl 	    if (*msg != '\0')
348*cb5caa98Sdjl 		(void) fprintf(stderr, "\n%s: %s\n\n", argv[0], msg);
3497c478bd9Sstevel@tonic-gate 	    usage(argv[0]);
350*cb5caa98Sdjl 	}
3517c478bd9Sstevel@tonic-gate 
352*cb5caa98Sdjl 	/*
353*cb5caa98Sdjl 	 * if main nscd already running and not forker nscd,
354*cb5caa98Sdjl 	 * can only do admin work
355*cb5caa98Sdjl 	 */
356*cb5caa98Sdjl 	if (_whoami == NSCD_MAIN) {
3577c478bd9Sstevel@tonic-gate 		if (!will_become_server) {
3587c478bd9Sstevel@tonic-gate 			if (showstats) {
359*cb5caa98Sdjl 				if (_nscd_client_getadmin('g')) {
360*cb5caa98Sdjl 					(void) fprintf(stderr,
361*cb5caa98Sdjl 			gettext("Cannot contact nscd properly(?)\n"));
362*cb5caa98Sdjl 					exit(1);
363*cb5caa98Sdjl 				}
364*cb5caa98Sdjl 				_nscd_client_showstats();
3657c478bd9Sstevel@tonic-gate 			}
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 			if (doset) {
368*cb5caa98Sdjl 				if (_nscd_client_setadmin() < 0) {
3697c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
370*cb5caa98Sdjl 				gettext("Error during admin call\n"));
3717c478bd9Sstevel@tonic-gate 					exit(1);
3727c478bd9Sstevel@tonic-gate 				}
3737c478bd9Sstevel@tonic-gate 			}
3747c478bd9Sstevel@tonic-gate 			if (!showstats && !doset) {
3757c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
376*cb5caa98Sdjl gettext("%s already running.... no administration option specified\n"),
3777c478bd9Sstevel@tonic-gate 					argv[0]);
3787c478bd9Sstevel@tonic-gate 			}
3797c478bd9Sstevel@tonic-gate 			exit(0);
3807c478bd9Sstevel@tonic-gate 		}
3817c478bd9Sstevel@tonic-gate 	}
3827c478bd9Sstevel@tonic-gate 
383*cb5caa98Sdjl 	/*
384*cb5caa98Sdjl 	 *   daemon from here on
385*cb5caa98Sdjl 	 */
386*cb5caa98Sdjl 
387*cb5caa98Sdjl 	if (_whoami == NSCD_MAIN) {
388*cb5caa98Sdjl 
389*cb5caa98Sdjl 		/* save enough info in case need to restart or fork */
390*cb5caa98Sdjl 		saved_argc = argc;
3917c478bd9Sstevel@tonic-gate 		saved_argv = argv;
3927c478bd9Sstevel@tonic-gate 		save_execname();
3937c478bd9Sstevel@tonic-gate 
394*cb5caa98Sdjl 		/*
395*cb5caa98Sdjl 		 * if a log file is not specified, set it to
396*cb5caa98Sdjl 		 * "stderr" or "/dev/null" based on debug level
397*cb5caa98Sdjl 		 */
398*cb5caa98Sdjl 		if (_logfd < 0 && *logfile == '\0') {
399*cb5caa98Sdjl 			if (debug_level != 0)
4007c478bd9Sstevel@tonic-gate 				/* we're debugging... */
401*cb5caa98Sdjl 				(void) strcpy(logfile, "stderr");
4027c478bd9Sstevel@tonic-gate 			else
403*cb5caa98Sdjl 				(void) strcpy(logfile, "/dev/null");
404*cb5caa98Sdjl 
405*cb5caa98Sdjl 			(void) _nscd_add_admin_mod(NULL, 'l', logfile,
406*cb5caa98Sdjl 				msg, sizeof (msg));
407*cb5caa98Sdjl 		}
408*cb5caa98Sdjl 
409*cb5caa98Sdjl 		/* activate command options */
410*cb5caa98Sdjl 		if (_nscd_server_setadmin(NULL) != NSCD_SUCCESS) {
411*cb5caa98Sdjl 			(void) fprintf(stderr,
412*cb5caa98Sdjl 			gettext("unable to set command line options\n"));
413*cb5caa98Sdjl 			exit(1);
414*cb5caa98Sdjl 		}
415*cb5caa98Sdjl 
416*cb5caa98Sdjl 		if (debug_level) {
417*cb5caa98Sdjl 			/* we're debugging, no forking of nscd */
418*cb5caa98Sdjl 
419*cb5caa98Sdjl 			/*
420*cb5caa98Sdjl 			 * forker nscd will be started if self credential
421*cb5caa98Sdjl 			 * is configured
422*cb5caa98Sdjl 			 */
423*cb5caa98Sdjl 			_nscd_start_forker(saved_execname, saved_argc,
424*cb5caa98Sdjl 				saved_argv);
4257c478bd9Sstevel@tonic-gate 		} else {
426*cb5caa98Sdjl 			/*
427*cb5caa98Sdjl 			 * daemonize the nscd (forker nscd will also
428*cb5caa98Sdjl 			 * be started if self credential is configured)
429*cb5caa98Sdjl 			 */
4307c478bd9Sstevel@tonic-gate 			detachfromtty();
4317c478bd9Sstevel@tonic-gate 		}
432*cb5caa98Sdjl 	} else { /* NSCD_FORKER */
433*cb5caa98Sdjl 		(void) open("/dev/null", O_RDWR, 0);
434*cb5caa98Sdjl 		(void) dup(0);
435*cb5caa98Sdjl 		if (_logfd != 2)
436*cb5caa98Sdjl 			(void) dup(0);
4377c478bd9Sstevel@tonic-gate 	}
4387c478bd9Sstevel@tonic-gate 
439*cb5caa98Sdjl 	/* set up door and establish our own server thread pool */
440*cb5caa98Sdjl 	if ((_doorfd = _nscd_setup_server(saved_execname, saved_argv)) == -1) {
441*cb5caa98Sdjl 		_NSCD_LOG(NSCD_LOG_FRONT_END, NSCD_LOG_LEVEL_ERROR)
442*cb5caa98Sdjl 		(me, "unable to set up door\n");
4437c478bd9Sstevel@tonic-gate 		exit(1);
4447c478bd9Sstevel@tonic-gate 	}
4457c478bd9Sstevel@tonic-gate 
446*cb5caa98Sdjl 	/* inform the main nscd that this forker is ready */
447*cb5caa98Sdjl 	if (_whoami == NSCD_FORKER) {
448*cb5caa98Sdjl 		int	ret;
4497c478bd9Sstevel@tonic-gate 
450*cb5caa98Sdjl 		for (ret = NSS_ALTRETRY; ret == NSS_ALTRETRY; )
451*cb5caa98Sdjl 			ret = _nscd_doorcall_sendfd(_doorfd,
452*cb5caa98Sdjl 				NSCD_IMHERE | (NSCD_FORKER & NSCD_WHOAMI),
453*cb5caa98Sdjl 				NULL, 0, NULL);
4547c478bd9Sstevel@tonic-gate 	}
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	for (;;) {
4577c478bd9Sstevel@tonic-gate 		(void) pause();
458*cb5caa98Sdjl 		(void) _nscd_doorcall(NSCD_REFRESH);
4597c478bd9Sstevel@tonic-gate 	}
4607c478bd9Sstevel@tonic-gate 
461*cb5caa98Sdjl 	/* NOTREACHED */
462*cb5caa98Sdjl 	/*LINTED E_FUNC_HAS_NO_RETURN_STMT*/
4637c478bd9Sstevel@tonic-gate }
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate static void
4667c478bd9Sstevel@tonic-gate usage(char *s)
4677c478bd9Sstevel@tonic-gate {
4687c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
4697c478bd9Sstevel@tonic-gate 		"Usage: %s [-d debug_level] [-l logfilename]\n", s);
4707c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
4717c478bd9Sstevel@tonic-gate 		"	[-p cachename,positive_time_to_live]\n");
4727c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
4737c478bd9Sstevel@tonic-gate 		"	[-n cachename,negative_time_to_live]\n");
4747c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
475*cb5caa98Sdjl 		"	[-i cachename]\n");
4767c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
477*cb5caa98Sdjl 		"	[-h cachename,keep_hot_count]\n");
4787c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
4797c478bd9Sstevel@tonic-gate 		"	[-e cachename,\"yes\"|\"no\"] [-g] " \
4807c478bd9Sstevel@tonic-gate 		"[-c cachename,\"yes\"|\"no\"]\n");
4817c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
4827c478bd9Sstevel@tonic-gate 		"	[-f configfilename] \n");
4837c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
484*cb5caa98Sdjl 		"\n	Supported caches:\n");
4857c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
486*cb5caa98Sdjl 		"	  audit_user, auth_attr, bootparams, ethers\n");
487*cb5caa98Sdjl 	(void) fprintf(stderr,
488*cb5caa98Sdjl 		"	  exec_attr, group, hosts, ipnodes, netmasks\n");
489*cb5caa98Sdjl 	(void) fprintf(stderr,
490*cb5caa98Sdjl 		"	  networks, passwd, printers, prof_attr, project\n");
491*cb5caa98Sdjl 	(void) fprintf(stderr,
492*cb5caa98Sdjl 		"	  protocols, rpc, services, tnrhtp, tnrhdb\n");
493*cb5caa98Sdjl 	(void) fprintf(stderr,
494*cb5caa98Sdjl 		"	  user_attr\n");
4957c478bd9Sstevel@tonic-gate 	exit(1);
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate /*
4997c478bd9Sstevel@tonic-gate  * detach from tty
5007c478bd9Sstevel@tonic-gate  */
5017c478bd9Sstevel@tonic-gate static void
5027c478bd9Sstevel@tonic-gate detachfromtty(void)
5037c478bd9Sstevel@tonic-gate {
504*cb5caa98Sdjl 	nscd_rc_t	rc;
505*cb5caa98Sdjl 	char		*me = "detachfromtty";
506*cb5caa98Sdjl 
507*cb5caa98Sdjl 	if (_logfd > 0) {
5087c478bd9Sstevel@tonic-gate 		int i;
509*cb5caa98Sdjl 		for (i = 0; i < _logfd; i++)
5107c478bd9Sstevel@tonic-gate 			(void) close(i);
511*cb5caa98Sdjl 		closefrom(_logfd + 1);
5127c478bd9Sstevel@tonic-gate 	} else
5137c478bd9Sstevel@tonic-gate 		closefrom(0);
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	(void) chdir("/");
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	switch (fork1()) {
5187c478bd9Sstevel@tonic-gate 	case (pid_t)-1:
519*cb5caa98Sdjl 
520*cb5caa98Sdjl 		_NSCD_LOG(NSCD_LOG_FRONT_END, NSCD_LOG_LEVEL_ERROR)
521*cb5caa98Sdjl 		(me, "unable to fork: pid = %d, %s\n",
522*cb5caa98Sdjl 		getpid(), strerror(errno));
523*cb5caa98Sdjl 
5247c478bd9Sstevel@tonic-gate 		exit(1);
5257c478bd9Sstevel@tonic-gate 		break;
5267c478bd9Sstevel@tonic-gate 	case 0:
527*cb5caa98Sdjl 		/* start the forker nscd if so configured */
528*cb5caa98Sdjl 		_nscd_start_forker(saved_execname, saved_argc, saved_argv);
5297c478bd9Sstevel@tonic-gate 		break;
5307c478bd9Sstevel@tonic-gate 	default:
5317c478bd9Sstevel@tonic-gate 		exit(0);
5327c478bd9Sstevel@tonic-gate 	}
5337c478bd9Sstevel@tonic-gate 	(void) setsid();
5347c478bd9Sstevel@tonic-gate 	(void) open("/dev/null", O_RDWR, 0);
5357c478bd9Sstevel@tonic-gate 	(void) dup(0);
536*cb5caa98Sdjl 	if (_logfd != 2)
5377c478bd9Sstevel@tonic-gate 		(void) dup(0);
538*cb5caa98Sdjl 
539*cb5caa98Sdjl 	/*
540*cb5caa98Sdjl 	 * start monitoring the states of the name service clients
541*cb5caa98Sdjl 	 */
542*cb5caa98Sdjl 	rc = _nscd_init_smf_monitor();
543*cb5caa98Sdjl 	if (rc != NSCD_SUCCESS) {
544*cb5caa98Sdjl 		_NSCD_LOG(NSCD_LOG_FRONT_END, NSCD_LOG_LEVEL_ERROR)
545*cb5caa98Sdjl 	(me, "unable to start the SMF monitor (rc = %d)\n", rc);
546*cb5caa98Sdjl 
547*cb5caa98Sdjl 		exit(-1);
548*cb5caa98Sdjl 	}
5497c478bd9Sstevel@tonic-gate }
550