xref: /titanic_44/usr/src/cmd/nscd/server.c (revision 6a634c9dca3093f3922e4b7ab826d7bdf17bf78e)
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 /*
22*07925104Sgww  * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
267c478bd9Sstevel@tonic-gate  * Simple doors name server cache daemon
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <fcntl.h>
32cb5caa98Sdjl #include <string.h>
33cb5caa98Sdjl #include <errno.h>
34cb5caa98Sdjl #include <stdarg.h>
35cb5caa98Sdjl #include <locale.h>
36bf1e3beeSmichen #include <sys/stat.h>
3745916cd2Sjpk #include <tsol/label.h>
3845916cd2Sjpk #include <zone.h>
3909480fbfSSreedhar Chalamalasetti - Sun Microsystems - Bangalore India #include <signal.h>
4049959095SJulian Pullen #include <sys/resource.h>
41cb5caa98Sdjl #include "cache.h"
42cb5caa98Sdjl #include "nscd_log.h"
43cb5caa98Sdjl #include "nscd_selfcred.h"
44cb5caa98Sdjl #include "nscd_frontend.h"
45cb5caa98Sdjl #include "nscd_common.h"
46cb5caa98Sdjl #include "nscd_admin.h"
47cb5caa98Sdjl #include "nscd_door.h"
48cb5caa98Sdjl #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 
55cb5caa98Sdjl #define	NSCDOPT	"S:Kf:c:ge:p:n:i:l:d:s:h:o:GFR"
56cb5caa98Sdjl 
57cb5caa98Sdjl /* assume this is a single nscd  or, if multiple, the main nscd */
58cb5caa98Sdjl int		_whoami = NSCD_MAIN;
59cb5caa98Sdjl int		_doorfd = -1;
60cb5caa98Sdjl extern int	_logfd;
61cb5caa98Sdjl static char	*cfgfile = NULL;
62cb5caa98Sdjl 
63cb5caa98Sdjl extern nsc_ctx_t *cache_ctx_p[];
64cb5caa98Sdjl 
657c478bd9Sstevel@tonic-gate static void usage(char *);
667c478bd9Sstevel@tonic-gate static void detachfromtty(void);
677c478bd9Sstevel@tonic-gate 
6818bdb8a7Smichen static char	debug_level[32] = { 0 };
69cb5caa98Sdjl static char	logfile[128] = { 0 };
707c478bd9Sstevel@tonic-gate static int	will_become_server;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate static char *
getcacheopt(char * s)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
__nss_use_files_hash(void)867c478bd9Sstevel@tonic-gate __nss_use_files_hash(void)
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
90cb5caa98Sdjl static int	saved_argc = 0;
91cb5caa98Sdjl static char	**saved_argv = NULL;
927c478bd9Sstevel@tonic-gate static char	saved_execname[MAXPATHLEN];
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate static void
save_execname()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);
103cb5caa98Sdjl 		(void) strlcat(saved_execname, "/", MAXPATHLEN);
1047c478bd9Sstevel@tonic-gate 	}
105cb5caa98Sdjl 	(void) strlcat(saved_execname, name, MAXPATHLEN);
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate 
108f166393fSesolom int
main(int argc,char ** argv)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;
115cb5caa98Sdjl 	nscd_rc_t	rc;
116cb5caa98Sdjl 	char		*me = "main()";
117cb5caa98Sdjl 	char		*ret_locale;
118cb5caa98Sdjl 	char		*ret_textdomain;
119cb5caa98Sdjl 	char		msg[128];
12049959095SJulian Pullen 	struct		rlimit rl;
121cb5caa98Sdjl 
122cb5caa98Sdjl 	ret_locale = setlocale(LC_ALL, "");
123cb5caa98Sdjl 	if (ret_locale == NULL)
124cb5caa98Sdjl 		(void) fprintf(stderr, gettext("Unable to set locale\n"));
125cb5caa98Sdjl 
126cb5caa98Sdjl 	ret_textdomain = textdomain(TEXT_DOMAIN);
127cb5caa98Sdjl 	if (ret_textdomain == NULL)
128cb5caa98Sdjl 		(void) fprintf(stderr, gettext("Unable to set textdomain\n"));
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	/*
13145916cd2Sjpk 	 * The admin model for TX is that labeled zones are managed
13245916cd2Sjpk 	 * in global zone where most trusted configuration database
133bf1e3beeSmichen 	 * resides. However, nscd will run in any labeled zone if
134bf1e3beeSmichen 	 * file /var/tsol/doors/nscd_per_label exists.
13545916cd2Sjpk 	 */
13645916cd2Sjpk 	if (is_system_labeled() && (getzoneid() != GLOBAL_ZONEID)) {
137bf1e3beeSmichen 		struct stat sbuf;
138bf1e3beeSmichen 		if (stat(TSOL_NSCD_PER_LABEL_FILE, &sbuf) < 0) {
13945916cd2Sjpk 			(void) fprintf(stderr,
140bf1e3beeSmichen 			gettext("With Trusted Extensions nscd runs only in the "
141bf1e3beeSmichen 			    "global zone (if nscd_per_label flag not set)\n"));
14245916cd2Sjpk 			exit(1);
14345916cd2Sjpk 		}
144bf1e3beeSmichen 	}
14545916cd2Sjpk 
14645916cd2Sjpk 	/*
1477c478bd9Sstevel@tonic-gate 	 *  Special case non-root user here - he can just print stats
1487c478bd9Sstevel@tonic-gate 	 */
1497c478bd9Sstevel@tonic-gate 	if (geteuid()) {
150cb5caa98Sdjl 		if (argc != 2 ||
151cb5caa98Sdjl 		    (strcmp(argv[1], "-g") && strcmp(argv[1], "-G"))) {
1527c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
153cb5caa98Sdjl 	gettext("Must be root to use any option other than -g\n\n"));
1547c478bd9Sstevel@tonic-gate 			usage(argv[0]);
1557c478bd9Sstevel@tonic-gate 		}
1567c478bd9Sstevel@tonic-gate 
157cb5caa98Sdjl 		if (_nscd_doorcall(NSCD_PING) != NSS_SUCCESS) {
1587c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
159cb5caa98Sdjl 			gettext("%s doesn't appear to be running.\n"),
160cb5caa98Sdjl 			    argv[0]);
1617c478bd9Sstevel@tonic-gate 			exit(1);
1627c478bd9Sstevel@tonic-gate 		}
163cb5caa98Sdjl 		if (_nscd_client_getadmin(argv[1][1]) != 0) {
164cb5caa98Sdjl 			(void) fprintf(stderr,
165cb5caa98Sdjl 	gettext("unable to get configuration and statistics data\n"));
166cb5caa98Sdjl 			exit(1);
167cb5caa98Sdjl 		}
168cb5caa98Sdjl 
169cb5caa98Sdjl 		_nscd_client_showstats();
1707c478bd9Sstevel@tonic-gate 		exit(0);
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 
173cb5caa98Sdjl 	/*
174cb5caa98Sdjl 	 *  Determine if there is already a daemon (main nscd) running.
175cb5caa98Sdjl 	 *  If not, will start it. Forker NSCD will always become a
176cb5caa98Sdjl 	 *  daemon.
177cb5caa98Sdjl 	 */
178cb5caa98Sdjl 	will_become_server = (_nscd_doorcall(NSCD_PING) != NSS_SUCCESS);
179cb5caa98Sdjl 	if (argc >= 2 && strcmp(argv[1], "-F") == 0) {
180cb5caa98Sdjl 		will_become_server = 1;
181cb5caa98Sdjl 		_whoami = NSCD_FORKER;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 		/*
184cb5caa98Sdjl 		 * allow time for the main nscd to get ready
185cb5caa98Sdjl 		 * to receive the IMHERE door request this
186cb5caa98Sdjl 		 * process will send later
1877c478bd9Sstevel@tonic-gate 		 */
188cb5caa98Sdjl 		(void) usleep(100000);
189cb5caa98Sdjl 	}
1907c478bd9Sstevel@tonic-gate 
191cb5caa98Sdjl 	/*
192cb5caa98Sdjl 	 * first get the config file path. Also detect
193cb5caa98Sdjl 	 * invalid option as soon as possible.
194cb5caa98Sdjl 	 */
195cb5caa98Sdjl 	while ((opt = getopt(argc, argv, NSCDOPT)) != EOF) {
196cb5caa98Sdjl 		switch (opt) {
197cb5caa98Sdjl 
198cb5caa98Sdjl 		case 'f':
199cb5caa98Sdjl 			if ((cfgfile = strdup(optarg)) == NULL)
200cb5caa98Sdjl 				exit(1);
201cb5caa98Sdjl 			break;
202cb5caa98Sdjl 		case 'g':
203cb5caa98Sdjl 			if (will_become_server) {
204cb5caa98Sdjl 				(void) fprintf(stderr,
205cb5caa98Sdjl 		gettext("nscd not running, no statistics to show\n\n"));
206cb5caa98Sdjl 				errflg++;
207cb5caa98Sdjl 			}
208cb5caa98Sdjl 			break;
209cb5caa98Sdjl 		case 'i':
210cb5caa98Sdjl 			if (will_become_server) {
211cb5caa98Sdjl 				(void) fprintf(stderr,
212cb5caa98Sdjl 		gettext("nscd not running, no cache to invalidate\n\n"));
213cb5caa98Sdjl 				errflg++;
214cb5caa98Sdjl 			}
215cb5caa98Sdjl 			break;
216cb5caa98Sdjl 
217cb5caa98Sdjl 		case '?':
218cb5caa98Sdjl 			errflg++;
219cb5caa98Sdjl 			break;
220cb5caa98Sdjl 		}
221cb5caa98Sdjl 
222cb5caa98Sdjl 	}
223cb5caa98Sdjl 	if (errflg)
224cb5caa98Sdjl 		usage(argv[0]);
225cb5caa98Sdjl 
226cb5caa98Sdjl 	/*
227cb5caa98Sdjl 	 *  perform more initialization and load configuration
228cb5caa98Sdjl 	 * if to become server
229cb5caa98Sdjl 	 */
230cb5caa98Sdjl 	if (will_become_server) {
231cb5caa98Sdjl 
232cb5caa98Sdjl 		/* initialize switch engine and config/stats management */
233cb5caa98Sdjl 		if ((rc = _nscd_init(cfgfile)) != NSCD_SUCCESS) {
234cb5caa98Sdjl 			(void) fprintf(stderr,
235cb5caa98Sdjl 		gettext("initialization of switch failed (rc = %d)\n"), rc);
236cb5caa98Sdjl 			exit(1);
237cb5caa98Sdjl 		}
23818bdb8a7Smichen 		_nscd_get_log_info(debug_level, sizeof (debug_level),
23918bdb8a7Smichen 		    logfile, sizeof (logfile));
240cb5caa98Sdjl 
241cb5caa98Sdjl 		/*
242cb5caa98Sdjl 		 * initialize cache store
243cb5caa98Sdjl 		 */
244cb5caa98Sdjl 		if ((rc = init_cache(0)) != NSCD_SUCCESS) {
245cb5caa98Sdjl 			(void) fprintf(stderr,
246cb5caa98Sdjl 	gettext("initialization of cache store failed (rc = %d)\n"), rc);
247cb5caa98Sdjl 			exit(1);
248cb5caa98Sdjl 		}
249cb5caa98Sdjl 	}
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	/*
2527c478bd9Sstevel@tonic-gate 	 * process usual options
2537c478bd9Sstevel@tonic-gate 	 */
254cb5caa98Sdjl 	optind = 1; /* this is a rescan */
255cb5caa98Sdjl 	*msg = '\0';
256cb5caa98Sdjl 	while ((opt = getopt(argc, argv, NSCDOPT)) != EOF) {
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 		switch (opt) {
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 		case 'K':		/* undocumented feature */
261cb5caa98Sdjl 			(void) _nscd_doorcall(NSCD_KILLSERVER);
2627c478bd9Sstevel@tonic-gate 			exit(0);
2637c478bd9Sstevel@tonic-gate 			break;
2647c478bd9Sstevel@tonic-gate 
265cb5caa98Sdjl 		case 'G':
2667c478bd9Sstevel@tonic-gate 		case 'g':
2677c478bd9Sstevel@tonic-gate 			showstats++;
2687c478bd9Sstevel@tonic-gate 			break;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 		case 'p':
2717c478bd9Sstevel@tonic-gate 			doset++;
272cb5caa98Sdjl 			if (_nscd_add_admin_mod(optarg, 'p',
273cb5caa98Sdjl 			    getcacheopt(optarg),
274cb5caa98Sdjl 			    msg, sizeof (msg)) == -1)
2757c478bd9Sstevel@tonic-gate 				errflg++;
2767c478bd9Sstevel@tonic-gate 			break;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 		case 'n':
2797c478bd9Sstevel@tonic-gate 			doset++;
280cb5caa98Sdjl 			if (_nscd_add_admin_mod(optarg, 'n',
281cb5caa98Sdjl 			    getcacheopt(optarg),
282cb5caa98Sdjl 			    msg, sizeof (msg)) == -1)
2837c478bd9Sstevel@tonic-gate 				errflg++;
2847c478bd9Sstevel@tonic-gate 			break;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 		case 'c':
2877c478bd9Sstevel@tonic-gate 			doset++;
288cb5caa98Sdjl 			if (_nscd_add_admin_mod(optarg, 'c',
289cb5caa98Sdjl 			    getcacheopt(optarg),
290cb5caa98Sdjl 			    msg, sizeof (msg)) == -1)
2917c478bd9Sstevel@tonic-gate 				errflg++;
2927c478bd9Sstevel@tonic-gate 			break;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 		case 'i':
2957c478bd9Sstevel@tonic-gate 			doset++;
296cb5caa98Sdjl 			if (_nscd_add_admin_mod(optarg, 'i', NULL,
297cb5caa98Sdjl 			    msg, sizeof (msg)) == -1)
2987c478bd9Sstevel@tonic-gate 				errflg++;
2997c478bd9Sstevel@tonic-gate 			break;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 		case 'l':
3027c478bd9Sstevel@tonic-gate 			doset++;
30318bdb8a7Smichen 			(void) strlcpy(logfile, optarg, sizeof (logfile));
3047c478bd9Sstevel@tonic-gate 			break;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 		case 'd':
3077c478bd9Sstevel@tonic-gate 			doset++;
30818bdb8a7Smichen 			(void) strlcpy(debug_level, optarg,
30918bdb8a7Smichen 			    sizeof (debug_level));
310cb5caa98Sdjl 			break;
311cb5caa98Sdjl 
312cb5caa98Sdjl 		case 'S':
313cb5caa98Sdjl 			/* silently ignore secure-mode */
3147c478bd9Sstevel@tonic-gate 			break;
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 		case 's':
317cb5caa98Sdjl 			/* silently ignore suggested-size */
3187c478bd9Sstevel@tonic-gate 			break;
3197c478bd9Sstevel@tonic-gate 
320cb5caa98Sdjl 		case 'o':
321cb5caa98Sdjl 			/* silently ignore old-data-ok */
3227c478bd9Sstevel@tonic-gate 			break;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 		case 'h':
3257c478bd9Sstevel@tonic-gate 			doset++;
326cb5caa98Sdjl 			if (_nscd_add_admin_mod(optarg, 'h',
327cb5caa98Sdjl 			    getcacheopt(optarg),
328cb5caa98Sdjl 			    msg, sizeof (msg)) == -1)
3297c478bd9Sstevel@tonic-gate 				errflg++;
3307c478bd9Sstevel@tonic-gate 			break;
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 		case 'e':
3337c478bd9Sstevel@tonic-gate 			doset++;
334cb5caa98Sdjl 			if (_nscd_add_admin_mod(optarg, 'e',
335cb5caa98Sdjl 			    getcacheopt(optarg),
336cb5caa98Sdjl 			    msg, sizeof (msg)) == -1)
3377c478bd9Sstevel@tonic-gate 				errflg++;
3387c478bd9Sstevel@tonic-gate 			break;
339cb5caa98Sdjl 
340cb5caa98Sdjl 		case 'F':
341cb5caa98Sdjl 			_whoami = NSCD_FORKER;
3427c478bd9Sstevel@tonic-gate 			break;
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 		default:
3457c478bd9Sstevel@tonic-gate 			errflg++;
3467c478bd9Sstevel@tonic-gate 			break;
3477c478bd9Sstevel@tonic-gate 		}
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate 
351cb5caa98Sdjl 	if (errflg) {
352cb5caa98Sdjl 		if (*msg != '\0')
353cb5caa98Sdjl 			(void) fprintf(stderr, "\n%s: %s\n\n", argv[0], msg);
3547c478bd9Sstevel@tonic-gate 		usage(argv[0]);
355cb5caa98Sdjl 	}
3567c478bd9Sstevel@tonic-gate 
357cb5caa98Sdjl 	/*
358cb5caa98Sdjl 	 * if main nscd already running and not forker nscd,
359cb5caa98Sdjl 	 * can only do admin work
360cb5caa98Sdjl 	 */
361cb5caa98Sdjl 	if (_whoami == NSCD_MAIN) {
3627c478bd9Sstevel@tonic-gate 		if (!will_become_server) {
3637c478bd9Sstevel@tonic-gate 			if (showstats) {
364cb5caa98Sdjl 				if (_nscd_client_getadmin('g')) {
365cb5caa98Sdjl 					(void) fprintf(stderr,
366cb5caa98Sdjl 			gettext("Cannot contact nscd properly(?)\n"));
367cb5caa98Sdjl 					exit(1);
368cb5caa98Sdjl 				}
369cb5caa98Sdjl 				_nscd_client_showstats();
3707c478bd9Sstevel@tonic-gate 			}
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 			if (doset) {
373cb5caa98Sdjl 				if (_nscd_client_setadmin() < 0) {
3747c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
375cb5caa98Sdjl 				gettext("Error during admin call\n"));
3767c478bd9Sstevel@tonic-gate 					exit(1);
3777c478bd9Sstevel@tonic-gate 				}
3787c478bd9Sstevel@tonic-gate 			}
3797c478bd9Sstevel@tonic-gate 			if (!showstats && !doset) {
3807c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
381cb5caa98Sdjl gettext("%s already running.... no administration option specified\n"),
3827c478bd9Sstevel@tonic-gate 				    argv[0]);
3837c478bd9Sstevel@tonic-gate 			}
3847c478bd9Sstevel@tonic-gate 			exit(0);
3857c478bd9Sstevel@tonic-gate 		}
3867c478bd9Sstevel@tonic-gate 	}
3877c478bd9Sstevel@tonic-gate 
388cb5caa98Sdjl 	/*
389cb5caa98Sdjl 	 *   daemon from here on
390cb5caa98Sdjl 	 */
391cb5caa98Sdjl 
392cb5caa98Sdjl 	if (_whoami == NSCD_MAIN) {
393cb5caa98Sdjl 
394cb5caa98Sdjl 		/* save enough info in case need to restart or fork */
395cb5caa98Sdjl 		saved_argc = argc;
3967c478bd9Sstevel@tonic-gate 		saved_argv = argv;
3977c478bd9Sstevel@tonic-gate 		save_execname();
3987c478bd9Sstevel@tonic-gate 
399cb5caa98Sdjl 		/*
400cb5caa98Sdjl 		 * if a log file is not specified, set it to
401cb5caa98Sdjl 		 * "stderr" or "/dev/null" based on debug level
402cb5caa98Sdjl 		 */
40318bdb8a7Smichen 		if (*logfile == '\0') {
40418bdb8a7Smichen 			if (*debug_level != '\0')
4057c478bd9Sstevel@tonic-gate 				/* we're debugging... */
406cb5caa98Sdjl 				(void) strcpy(logfile, "stderr");
4077c478bd9Sstevel@tonic-gate 			else
408cb5caa98Sdjl 				(void) strcpy(logfile, "/dev/null");
40918bdb8a7Smichen 		}
410cb5caa98Sdjl 		(void) _nscd_add_admin_mod(NULL, 'l', logfile,
411cb5caa98Sdjl 		    msg, sizeof (msg));
41218bdb8a7Smichen 		(void) _nscd_add_admin_mod(NULL, 'd', debug_level,
41318bdb8a7Smichen 		    msg, sizeof (msg));
414cb5caa98Sdjl 
415cb5caa98Sdjl 		/* activate command options */
416cb5caa98Sdjl 		if (_nscd_server_setadmin(NULL) != NSCD_SUCCESS) {
417cb5caa98Sdjl 			(void) fprintf(stderr,
418cb5caa98Sdjl 			gettext("unable to set command line options\n"));
419cb5caa98Sdjl 			exit(1);
420cb5caa98Sdjl 		}
421cb5caa98Sdjl 
42218bdb8a7Smichen 		if (*debug_level != '\0') {
423cb5caa98Sdjl 			/* we're debugging, no forking of nscd */
424cb5caa98Sdjl 
425cb5caa98Sdjl 			/*
426cb5caa98Sdjl 			 * forker nscd will be started if self credential
427cb5caa98Sdjl 			 * is configured
428cb5caa98Sdjl 			 */
429cb5caa98Sdjl 			_nscd_start_forker(saved_execname, saved_argc,
430cb5caa98Sdjl 			    saved_argv);
4317c478bd9Sstevel@tonic-gate 		} else {
432cb5caa98Sdjl 			/*
433cb5caa98Sdjl 			 * daemonize the nscd (forker nscd will also
434cb5caa98Sdjl 			 * be started if self credential is configured)
435cb5caa98Sdjl 			 */
4367c478bd9Sstevel@tonic-gate 			detachfromtty();
4377c478bd9Sstevel@tonic-gate 		}
438cb5caa98Sdjl 	} else { /* NSCD_FORKER */
43909480fbfSSreedhar Chalamalasetti - Sun Microsystems - Bangalore India 		/*
44009480fbfSSreedhar Chalamalasetti - Sun Microsystems - Bangalore India 		 * To avoid PUN (Per User Nscd) processes from becoming
44109480fbfSSreedhar Chalamalasetti - Sun Microsystems - Bangalore India 		 * zombies after they exit, the forking nscd should
44209480fbfSSreedhar Chalamalasetti - Sun Microsystems - Bangalore India 		 * ignore the SIGCLD signal so that it does not
44309480fbfSSreedhar Chalamalasetti - Sun Microsystems - Bangalore India 		 * need to wait for every child PUN to exit.
44409480fbfSSreedhar Chalamalasetti - Sun Microsystems - Bangalore India 		 */
44509480fbfSSreedhar Chalamalasetti - Sun Microsystems - Bangalore India 		(void) signal(SIGCLD, SIG_IGN);
446cb5caa98Sdjl 		(void) open("/dev/null", O_RDWR, 0);
447cb5caa98Sdjl 		(void) dup(0);
448cb5caa98Sdjl 		if (_logfd != 2)
449cb5caa98Sdjl 			(void) dup(0);
4507c478bd9Sstevel@tonic-gate 	}
4517c478bd9Sstevel@tonic-gate 
45249959095SJulian Pullen 	/* set NOFILE to unlimited */
45349959095SJulian Pullen 	rl.rlim_cur = rl.rlim_max = RLIM_INFINITY;
45449959095SJulian Pullen 	if (setrlimit(RLIMIT_NOFILE, &rl) < 0) {
45549959095SJulian Pullen 		_NSCD_LOG(NSCD_LOG_FRONT_END, NSCD_LOG_LEVEL_ERROR)
45649959095SJulian Pullen 		(me, "Cannot set open file limit: %s\n", strerror(errno));
45749959095SJulian Pullen 		exit(1);
45849959095SJulian Pullen 	}
45949959095SJulian Pullen 
460cb5caa98Sdjl 	/* set up door and establish our own server thread pool */
461cb5caa98Sdjl 	if ((_doorfd = _nscd_setup_server(saved_execname, saved_argv)) == -1) {
462cb5caa98Sdjl 		_NSCD_LOG(NSCD_LOG_FRONT_END, NSCD_LOG_LEVEL_ERROR)
463cb5caa98Sdjl 		(me, "unable to set up door\n");
4647c478bd9Sstevel@tonic-gate 		exit(1);
4657c478bd9Sstevel@tonic-gate 	}
4667c478bd9Sstevel@tonic-gate 
467cb5caa98Sdjl 	/* inform the main nscd that this forker is ready */
468cb5caa98Sdjl 	if (_whoami == NSCD_FORKER) {
469cb5caa98Sdjl 		int	ret;
4707c478bd9Sstevel@tonic-gate 
471cb5caa98Sdjl 		for (ret = NSS_ALTRETRY; ret == NSS_ALTRETRY; )
472cb5caa98Sdjl 			ret = _nscd_doorcall_sendfd(_doorfd,
473cb5caa98Sdjl 			    NSCD_IMHERE | (NSCD_FORKER & NSCD_WHOAMI),
474cb5caa98Sdjl 			    NULL, 0, NULL);
4757c478bd9Sstevel@tonic-gate 	}
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	for (;;) {
4787c478bd9Sstevel@tonic-gate 		(void) pause();
479cb5caa98Sdjl 		(void) _nscd_doorcall(NSCD_REFRESH);
4807c478bd9Sstevel@tonic-gate 	}
4817c478bd9Sstevel@tonic-gate 
482cb5caa98Sdjl 	/* NOTREACHED */
483cb5caa98Sdjl 	/*LINTED E_FUNC_HAS_NO_RETURN_STMT*/
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate static void
usage(char * s)4877c478bd9Sstevel@tonic-gate usage(char *s)
4887c478bd9Sstevel@tonic-gate {
4897c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
4907c478bd9Sstevel@tonic-gate 	    "Usage: %s [-d debug_level] [-l logfilename]\n", s);
4917c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
4927c478bd9Sstevel@tonic-gate 	    "	[-p cachename,positive_time_to_live]\n");
4937c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
4947c478bd9Sstevel@tonic-gate 	    "	[-n cachename,negative_time_to_live]\n");
4957c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
496cb5caa98Sdjl 	    "	[-i cachename]\n");
4977c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
498cb5caa98Sdjl 	    "	[-h cachename,keep_hot_count]\n");
4997c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
5007c478bd9Sstevel@tonic-gate 	    "	[-e cachename,\"yes\"|\"no\"] [-g] " \
5017c478bd9Sstevel@tonic-gate 	    "[-c cachename,\"yes\"|\"no\"]\n");
5027c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
5037c478bd9Sstevel@tonic-gate 	    "	[-f configfilename] \n");
5047c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
505cb5caa98Sdjl 	    "\n	Supported caches:\n");
5067c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
507*07925104Sgww 	    "	  auth_attr, bootparams, ethers\n");
508cb5caa98Sdjl 	(void) fprintf(stderr,
509cb5caa98Sdjl 	    "	  exec_attr, group, hosts, ipnodes, netmasks\n");
510cb5caa98Sdjl 	(void) fprintf(stderr,
511cb5caa98Sdjl 	    "	  networks, passwd, printers, prof_attr, project\n");
512cb5caa98Sdjl 	(void) fprintf(stderr,
513cb5caa98Sdjl 	    "	  protocols, rpc, services, tnrhtp, tnrhdb\n");
514cb5caa98Sdjl 	(void) fprintf(stderr,
515cb5caa98Sdjl 	    "	  user_attr\n");
5167c478bd9Sstevel@tonic-gate 	exit(1);
5177c478bd9Sstevel@tonic-gate }
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate /*
5207c478bd9Sstevel@tonic-gate  * detach from tty
5217c478bd9Sstevel@tonic-gate  */
5227c478bd9Sstevel@tonic-gate static void
detachfromtty(void)5237c478bd9Sstevel@tonic-gate detachfromtty(void)
5247c478bd9Sstevel@tonic-gate {
525cb5caa98Sdjl 	nscd_rc_t	rc;
526cb5caa98Sdjl 	char		*me = "detachfromtty";
527cb5caa98Sdjl 
528cb5caa98Sdjl 	if (_logfd > 0) {
5297c478bd9Sstevel@tonic-gate 		int i;
530cb5caa98Sdjl 		for (i = 0; i < _logfd; i++)
5317c478bd9Sstevel@tonic-gate 			(void) close(i);
532cb5caa98Sdjl 		closefrom(_logfd + 1);
5337c478bd9Sstevel@tonic-gate 	} else
5347c478bd9Sstevel@tonic-gate 		closefrom(0);
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	(void) chdir("/");
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	switch (fork1()) {
5397c478bd9Sstevel@tonic-gate 	case (pid_t)-1:
540cb5caa98Sdjl 
541cb5caa98Sdjl 		_NSCD_LOG(NSCD_LOG_FRONT_END, NSCD_LOG_LEVEL_ERROR)
542cb5caa98Sdjl 		(me, "unable to fork: pid = %d, %s\n",
543cb5caa98Sdjl 		    getpid(), strerror(errno));
544cb5caa98Sdjl 
5457c478bd9Sstevel@tonic-gate 		exit(1);
5467c478bd9Sstevel@tonic-gate 		break;
5477c478bd9Sstevel@tonic-gate 	case 0:
548cb5caa98Sdjl 		/* start the forker nscd if so configured */
549cb5caa98Sdjl 		_nscd_start_forker(saved_execname, saved_argc, saved_argv);
5507c478bd9Sstevel@tonic-gate 		break;
5517c478bd9Sstevel@tonic-gate 	default:
5527c478bd9Sstevel@tonic-gate 		exit(0);
5537c478bd9Sstevel@tonic-gate 	}
55449959095SJulian Pullen 
5557c478bd9Sstevel@tonic-gate 	(void) setsid();
5567c478bd9Sstevel@tonic-gate 	(void) open("/dev/null", O_RDWR, 0);
5577c478bd9Sstevel@tonic-gate 	(void) dup(0);
558cb5caa98Sdjl 	if (_logfd != 2)
5597c478bd9Sstevel@tonic-gate 		(void) dup(0);
560cb5caa98Sdjl 
561cb5caa98Sdjl 	/*
562cb5caa98Sdjl 	 * start monitoring the states of the name service clients
563cb5caa98Sdjl 	 */
564cb5caa98Sdjl 	rc = _nscd_init_smf_monitor();
565cb5caa98Sdjl 	if (rc != NSCD_SUCCESS) {
566cb5caa98Sdjl 		_NSCD_LOG(NSCD_LOG_FRONT_END, NSCD_LOG_LEVEL_ERROR)
567cb5caa98Sdjl 	(me, "unable to start the SMF monitor (rc = %d)\n", rc);
568cb5caa98Sdjl 
569cb5caa98Sdjl 		exit(-1);
570cb5caa98Sdjl 	}
5717c478bd9Sstevel@tonic-gate }
572