xref: /illumos-gate/usr/src/cmd/nscd/server.c (revision 48bbca81)
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 /*
2207925104Sgww  * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
23*48bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Simple doors name server cache daemon
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <fcntl.h>
33cb5caa98Sdjl #include <string.h>
34cb5caa98Sdjl #include <errno.h>
35cb5caa98Sdjl #include <stdarg.h>
36cb5caa98Sdjl #include <locale.h>
37bf1e3beeSmichen #include <sys/stat.h>
3845916cd2Sjpk #include <tsol/label.h>
3945916cd2Sjpk #include <zone.h>
4009480fbfSSreedhar Chalamalasetti - Sun Microsystems - Bangalore India #include <signal.h>
4149959095SJulian Pullen #include <sys/resource.h>
42cb5caa98Sdjl #include "cache.h"
43cb5caa98Sdjl #include "nscd_log.h"
44cb5caa98Sdjl #include "nscd_selfcred.h"
45cb5caa98Sdjl #include "nscd_frontend.h"
46cb5caa98Sdjl #include "nscd_common.h"
47cb5caa98Sdjl #include "nscd_admin.h"
48cb5caa98Sdjl #include "nscd_door.h"
49cb5caa98Sdjl #include "nscd_switch.h"
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate extern int 	optind;
527c478bd9Sstevel@tonic-gate extern int 	opterr;
537c478bd9Sstevel@tonic-gate extern int 	optopt;
547c478bd9Sstevel@tonic-gate extern char 	*optarg;
557c478bd9Sstevel@tonic-gate 
56cb5caa98Sdjl #define	NSCDOPT	"S:Kf:c:ge:p:n:i:l:d:s:h:o:GFR"
57cb5caa98Sdjl 
58cb5caa98Sdjl /* assume this is a single nscd  or, if multiple, the main nscd */
59cb5caa98Sdjl int		_whoami = NSCD_MAIN;
60cb5caa98Sdjl int		_doorfd = -1;
61cb5caa98Sdjl extern int	_logfd;
62cb5caa98Sdjl static char	*cfgfile = NULL;
63cb5caa98Sdjl 
64cb5caa98Sdjl extern nsc_ctx_t *cache_ctx_p[];
65cb5caa98Sdjl 
667c478bd9Sstevel@tonic-gate static void usage(char *);
677c478bd9Sstevel@tonic-gate static void detachfromtty(void);
687c478bd9Sstevel@tonic-gate 
6918bdb8a7Smichen static char	debug_level[32] = { 0 };
70cb5caa98Sdjl static char	logfile[128] = { 0 };
717c478bd9Sstevel@tonic-gate static int	will_become_server;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate static char *
getcacheopt(char * s)747c478bd9Sstevel@tonic-gate getcacheopt(char *s)
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate 	while (*s && *s != ',')
777c478bd9Sstevel@tonic-gate 		s++;
787c478bd9Sstevel@tonic-gate 	return ((*s == ',') ? (s + 1) : NULL);
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /*
827c478bd9Sstevel@tonic-gate  * declaring this causes the files backend to use hashing
837c478bd9Sstevel@tonic-gate  * this is of course an utter hack, but provides a nice
847c478bd9Sstevel@tonic-gate  * quiet back door to enable this feature for only the nscd.
857c478bd9Sstevel@tonic-gate  */
867c478bd9Sstevel@tonic-gate void
__nss_use_files_hash(void)877c478bd9Sstevel@tonic-gate __nss_use_files_hash(void)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate 
91cb5caa98Sdjl static int	saved_argc = 0;
92cb5caa98Sdjl static char	**saved_argv = NULL;
937c478bd9Sstevel@tonic-gate static char	saved_execname[MAXPATHLEN];
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate static void
save_execname()967c478bd9Sstevel@tonic-gate save_execname()
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate 	const char *name = getexecname();
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	saved_execname[0] = 0;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	if (name[0] != '/') { /* started w/ relative path */
1037c478bd9Sstevel@tonic-gate 		(void) getcwd(saved_execname, MAXPATHLEN);
104cb5caa98Sdjl 		(void) strlcat(saved_execname, "/", MAXPATHLEN);
1057c478bd9Sstevel@tonic-gate 	}
106cb5caa98Sdjl 	(void) strlcat(saved_execname, name, MAXPATHLEN);
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate 
109f166393fSesolom int
main(int argc,char ** argv)1107c478bd9Sstevel@tonic-gate main(int argc, char ** argv)
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate 	int		opt;
1137c478bd9Sstevel@tonic-gate 	int		errflg = 0;
1147c478bd9Sstevel@tonic-gate 	int		showstats = 0;
1157c478bd9Sstevel@tonic-gate 	int		doset = 0;
116cb5caa98Sdjl 	nscd_rc_t	rc;
117cb5caa98Sdjl 	char		*me = "main()";
118cb5caa98Sdjl 	char		*ret_locale;
119cb5caa98Sdjl 	char		*ret_textdomain;
120cb5caa98Sdjl 	char		msg[128];
12149959095SJulian Pullen 	struct		rlimit rl;
122cb5caa98Sdjl 
123cb5caa98Sdjl 	ret_locale = setlocale(LC_ALL, "");
124cb5caa98Sdjl 	if (ret_locale == NULL)
125cb5caa98Sdjl 		(void) fprintf(stderr, gettext("Unable to set locale\n"));
126cb5caa98Sdjl 
127cb5caa98Sdjl 	ret_textdomain = textdomain(TEXT_DOMAIN);
128cb5caa98Sdjl 	if (ret_textdomain == NULL)
129cb5caa98Sdjl 		(void) fprintf(stderr, gettext("Unable to set textdomain\n"));
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	/*
13245916cd2Sjpk 	 * The admin model for TX is that labeled zones are managed
13345916cd2Sjpk 	 * in global zone where most trusted configuration database
134bf1e3beeSmichen 	 * resides. However, nscd will run in any labeled zone if
135bf1e3beeSmichen 	 * file /var/tsol/doors/nscd_per_label exists.
13645916cd2Sjpk 	 */
13745916cd2Sjpk 	if (is_system_labeled() && (getzoneid() != GLOBAL_ZONEID)) {
138bf1e3beeSmichen 		struct stat sbuf;
139bf1e3beeSmichen 		if (stat(TSOL_NSCD_PER_LABEL_FILE, &sbuf) < 0) {
14045916cd2Sjpk 			(void) fprintf(stderr,
141bf1e3beeSmichen 			gettext("With Trusted Extensions nscd runs only in the "
142bf1e3beeSmichen 			    "global zone (if nscd_per_label flag not set)\n"));
14345916cd2Sjpk 			exit(1);
14445916cd2Sjpk 		}
145bf1e3beeSmichen 	}
14645916cd2Sjpk 
14745916cd2Sjpk 	/*
148*48bbca81SDaniel Hoffman 	 *  Special case non-root user here - they can only print stats
1497c478bd9Sstevel@tonic-gate 	 */
1507c478bd9Sstevel@tonic-gate 	if (geteuid()) {
151cb5caa98Sdjl 		if (argc != 2 ||
152cb5caa98Sdjl 		    (strcmp(argv[1], "-g") && strcmp(argv[1], "-G"))) {
1537c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
154cb5caa98Sdjl 	gettext("Must be root to use any option other than -g\n\n"));
1557c478bd9Sstevel@tonic-gate 			usage(argv[0]);
1567c478bd9Sstevel@tonic-gate 		}
1577c478bd9Sstevel@tonic-gate 
158cb5caa98Sdjl 		if (_nscd_doorcall(NSCD_PING) != NSS_SUCCESS) {
1597c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
160cb5caa98Sdjl 			gettext("%s doesn't appear to be running.\n"),
161cb5caa98Sdjl 			    argv[0]);
1627c478bd9Sstevel@tonic-gate 			exit(1);
1637c478bd9Sstevel@tonic-gate 		}
164cb5caa98Sdjl 		if (_nscd_client_getadmin(argv[1][1]) != 0) {
165cb5caa98Sdjl 			(void) fprintf(stderr,
166cb5caa98Sdjl 	gettext("unable to get configuration and statistics data\n"));
167cb5caa98Sdjl 			exit(1);
168cb5caa98Sdjl 		}
169cb5caa98Sdjl 
170cb5caa98Sdjl 		_nscd_client_showstats();
1717c478bd9Sstevel@tonic-gate 		exit(0);
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 
174cb5caa98Sdjl 	/*
175cb5caa98Sdjl 	 *  Determine if there is already a daemon (main nscd) running.
176cb5caa98Sdjl 	 *  If not, will start it. Forker NSCD will always become a
177cb5caa98Sdjl 	 *  daemon.
178cb5caa98Sdjl 	 */
179cb5caa98Sdjl 	will_become_server = (_nscd_doorcall(NSCD_PING) != NSS_SUCCESS);
180cb5caa98Sdjl 	if (argc >= 2 && strcmp(argv[1], "-F") == 0) {
181cb5caa98Sdjl 		will_become_server = 1;
182cb5caa98Sdjl 		_whoami = NSCD_FORKER;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 		/*
185cb5caa98Sdjl 		 * allow time for the main nscd to get ready
186cb5caa98Sdjl 		 * to receive the IMHERE door request this
187cb5caa98Sdjl 		 * process will send later
1887c478bd9Sstevel@tonic-gate 		 */
189cb5caa98Sdjl 		(void) usleep(100000);
190cb5caa98Sdjl 	}
1917c478bd9Sstevel@tonic-gate 
192cb5caa98Sdjl 	/*
193cb5caa98Sdjl 	 * first get the config file path. Also detect
194cb5caa98Sdjl 	 * invalid option as soon as possible.
195cb5caa98Sdjl 	 */
196cb5caa98Sdjl 	while ((opt = getopt(argc, argv, NSCDOPT)) != EOF) {
197cb5caa98Sdjl 		switch (opt) {
198cb5caa98Sdjl 
199cb5caa98Sdjl 		case 'f':
200cb5caa98Sdjl 			if ((cfgfile = strdup(optarg)) == NULL)
201cb5caa98Sdjl 				exit(1);
202cb5caa98Sdjl 			break;
203cb5caa98Sdjl 		case 'g':
204cb5caa98Sdjl 			if (will_become_server) {
205cb5caa98Sdjl 				(void) fprintf(stderr,
206cb5caa98Sdjl 		gettext("nscd not running, no statistics to show\n\n"));
207cb5caa98Sdjl 				errflg++;
208cb5caa98Sdjl 			}
209cb5caa98Sdjl 			break;
210cb5caa98Sdjl 		case 'i':
211cb5caa98Sdjl 			if (will_become_server) {
212cb5caa98Sdjl 				(void) fprintf(stderr,
213cb5caa98Sdjl 		gettext("nscd not running, no cache to invalidate\n\n"));
214cb5caa98Sdjl 				errflg++;
215cb5caa98Sdjl 			}
216cb5caa98Sdjl 			break;
217cb5caa98Sdjl 
218cb5caa98Sdjl 		case '?':
219cb5caa98Sdjl 			errflg++;
220cb5caa98Sdjl 			break;
221cb5caa98Sdjl 		}
222cb5caa98Sdjl 
223cb5caa98Sdjl 	}
224cb5caa98Sdjl 	if (errflg)
225cb5caa98Sdjl 		usage(argv[0]);
226cb5caa98Sdjl 
227cb5caa98Sdjl 	/*
228cb5caa98Sdjl 	 *  perform more initialization and load configuration
229cb5caa98Sdjl 	 * if to become server
230cb5caa98Sdjl 	 */
231cb5caa98Sdjl 	if (will_become_server) {
232cb5caa98Sdjl 
233cb5caa98Sdjl 		/* initialize switch engine and config/stats management */
234cb5caa98Sdjl 		if ((rc = _nscd_init(cfgfile)) != NSCD_SUCCESS) {
235cb5caa98Sdjl 			(void) fprintf(stderr,
236cb5caa98Sdjl 		gettext("initialization of switch failed (rc = %d)\n"), rc);
237cb5caa98Sdjl 			exit(1);
238cb5caa98Sdjl 		}
23918bdb8a7Smichen 		_nscd_get_log_info(debug_level, sizeof (debug_level),
24018bdb8a7Smichen 		    logfile, sizeof (logfile));
241cb5caa98Sdjl 
242cb5caa98Sdjl 		/*
243cb5caa98Sdjl 		 * initialize cache store
244cb5caa98Sdjl 		 */
245cb5caa98Sdjl 		if ((rc = init_cache(0)) != NSCD_SUCCESS) {
246cb5caa98Sdjl 			(void) fprintf(stderr,
247cb5caa98Sdjl 	gettext("initialization of cache store failed (rc = %d)\n"), rc);
248cb5caa98Sdjl 			exit(1);
249cb5caa98Sdjl 		}
250cb5caa98Sdjl 	}
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	/*
2537c478bd9Sstevel@tonic-gate 	 * process usual options
2547c478bd9Sstevel@tonic-gate 	 */
255cb5caa98Sdjl 	optind = 1; /* this is a rescan */
256cb5caa98Sdjl 	*msg = '\0';
257cb5caa98Sdjl 	while ((opt = getopt(argc, argv, NSCDOPT)) != EOF) {
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 		switch (opt) {
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 		case 'K':		/* undocumented feature */
262cb5caa98Sdjl 			(void) _nscd_doorcall(NSCD_KILLSERVER);
2637c478bd9Sstevel@tonic-gate 			exit(0);
2647c478bd9Sstevel@tonic-gate 			break;
2657c478bd9Sstevel@tonic-gate 
266cb5caa98Sdjl 		case 'G':
2677c478bd9Sstevel@tonic-gate 		case 'g':
2687c478bd9Sstevel@tonic-gate 			showstats++;
2697c478bd9Sstevel@tonic-gate 			break;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 		case 'p':
2727c478bd9Sstevel@tonic-gate 			doset++;
273cb5caa98Sdjl 			if (_nscd_add_admin_mod(optarg, 'p',
274cb5caa98Sdjl 			    getcacheopt(optarg),
275cb5caa98Sdjl 			    msg, sizeof (msg)) == -1)
2767c478bd9Sstevel@tonic-gate 				errflg++;
2777c478bd9Sstevel@tonic-gate 			break;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 		case 'n':
2807c478bd9Sstevel@tonic-gate 			doset++;
281cb5caa98Sdjl 			if (_nscd_add_admin_mod(optarg, 'n',
282cb5caa98Sdjl 			    getcacheopt(optarg),
283cb5caa98Sdjl 			    msg, sizeof (msg)) == -1)
2847c478bd9Sstevel@tonic-gate 				errflg++;
2857c478bd9Sstevel@tonic-gate 			break;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 		case 'c':
2887c478bd9Sstevel@tonic-gate 			doset++;
289cb5caa98Sdjl 			if (_nscd_add_admin_mod(optarg, 'c',
290cb5caa98Sdjl 			    getcacheopt(optarg),
291cb5caa98Sdjl 			    msg, sizeof (msg)) == -1)
2927c478bd9Sstevel@tonic-gate 				errflg++;
2937c478bd9Sstevel@tonic-gate 			break;
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 		case 'i':
2967c478bd9Sstevel@tonic-gate 			doset++;
297cb5caa98Sdjl 			if (_nscd_add_admin_mod(optarg, 'i', NULL,
298cb5caa98Sdjl 			    msg, sizeof (msg)) == -1)
2997c478bd9Sstevel@tonic-gate 				errflg++;
3007c478bd9Sstevel@tonic-gate 			break;
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 		case 'l':
3037c478bd9Sstevel@tonic-gate 			doset++;
30418bdb8a7Smichen 			(void) strlcpy(logfile, optarg, sizeof (logfile));
3057c478bd9Sstevel@tonic-gate 			break;
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 		case 'd':
3087c478bd9Sstevel@tonic-gate 			doset++;
30918bdb8a7Smichen 			(void) strlcpy(debug_level, optarg,
31018bdb8a7Smichen 			    sizeof (debug_level));
311cb5caa98Sdjl 			break;
312cb5caa98Sdjl 
313cb5caa98Sdjl 		case 'S':
314cb5caa98Sdjl 			/* silently ignore secure-mode */
3157c478bd9Sstevel@tonic-gate 			break;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 		case 's':
318cb5caa98Sdjl 			/* silently ignore suggested-size */
3197c478bd9Sstevel@tonic-gate 			break;
3207c478bd9Sstevel@tonic-gate 
321cb5caa98Sdjl 		case 'o':
322cb5caa98Sdjl 			/* silently ignore old-data-ok */
3237c478bd9Sstevel@tonic-gate 			break;
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 		case 'h':
3267c478bd9Sstevel@tonic-gate 			doset++;
327cb5caa98Sdjl 			if (_nscd_add_admin_mod(optarg, 'h',
328cb5caa98Sdjl 			    getcacheopt(optarg),
329cb5caa98Sdjl 			    msg, sizeof (msg)) == -1)
3307c478bd9Sstevel@tonic-gate 				errflg++;
3317c478bd9Sstevel@tonic-gate 			break;
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 		case 'e':
3347c478bd9Sstevel@tonic-gate 			doset++;
335cb5caa98Sdjl 			if (_nscd_add_admin_mod(optarg, 'e',
336cb5caa98Sdjl 			    getcacheopt(optarg),
337cb5caa98Sdjl 			    msg, sizeof (msg)) == -1)
3387c478bd9Sstevel@tonic-gate 				errflg++;
3397c478bd9Sstevel@tonic-gate 			break;
340cb5caa98Sdjl 
341cb5caa98Sdjl 		case 'F':
342cb5caa98Sdjl 			_whoami = NSCD_FORKER;
3437c478bd9Sstevel@tonic-gate 			break;
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 		default:
3467c478bd9Sstevel@tonic-gate 			errflg++;
3477c478bd9Sstevel@tonic-gate 			break;
3487c478bd9Sstevel@tonic-gate 		}
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	}
3517c478bd9Sstevel@tonic-gate 
352cb5caa98Sdjl 	if (errflg) {
353cb5caa98Sdjl 		if (*msg != '\0')
354cb5caa98Sdjl 			(void) fprintf(stderr, "\n%s: %s\n\n", argv[0], msg);
3557c478bd9Sstevel@tonic-gate 		usage(argv[0]);
356cb5caa98Sdjl 	}
3577c478bd9Sstevel@tonic-gate 
358cb5caa98Sdjl 	/*
359cb5caa98Sdjl 	 * if main nscd already running and not forker nscd,
360cb5caa98Sdjl 	 * can only do admin work
361cb5caa98Sdjl 	 */
362cb5caa98Sdjl 	if (_whoami == NSCD_MAIN) {
3637c478bd9Sstevel@tonic-gate 		if (!will_become_server) {
3647c478bd9Sstevel@tonic-gate 			if (showstats) {
365cb5caa98Sdjl 				if (_nscd_client_getadmin('g')) {
366cb5caa98Sdjl 					(void) fprintf(stderr,
367cb5caa98Sdjl 			gettext("Cannot contact nscd properly(?)\n"));
368cb5caa98Sdjl 					exit(1);
369cb5caa98Sdjl 				}
370cb5caa98Sdjl 				_nscd_client_showstats();
3717c478bd9Sstevel@tonic-gate 			}
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 			if (doset) {
374cb5caa98Sdjl 				if (_nscd_client_setadmin() < 0) {
3757c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
376cb5caa98Sdjl 				gettext("Error during admin call\n"));
3777c478bd9Sstevel@tonic-gate 					exit(1);
3787c478bd9Sstevel@tonic-gate 				}
3797c478bd9Sstevel@tonic-gate 			}
3807c478bd9Sstevel@tonic-gate 			if (!showstats && !doset) {
3817c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
382cb5caa98Sdjl gettext("%s already running.... no administration option specified\n"),
3837c478bd9Sstevel@tonic-gate 				    argv[0]);
3847c478bd9Sstevel@tonic-gate 			}
3857c478bd9Sstevel@tonic-gate 			exit(0);
3867c478bd9Sstevel@tonic-gate 		}
3877c478bd9Sstevel@tonic-gate 	}
3887c478bd9Sstevel@tonic-gate 
389cb5caa98Sdjl 	/*
390cb5caa98Sdjl 	 *   daemon from here on
391cb5caa98Sdjl 	 */
392cb5caa98Sdjl 
393cb5caa98Sdjl 	if (_whoami == NSCD_MAIN) {
394cb5caa98Sdjl 
395cb5caa98Sdjl 		/* save enough info in case need to restart or fork */
396cb5caa98Sdjl 		saved_argc = argc;
3977c478bd9Sstevel@tonic-gate 		saved_argv = argv;
3987c478bd9Sstevel@tonic-gate 		save_execname();
3997c478bd9Sstevel@tonic-gate 
400cb5caa98Sdjl 		/*
401cb5caa98Sdjl 		 * if a log file is not specified, set it to
402cb5caa98Sdjl 		 * "stderr" or "/dev/null" based on debug level
403cb5caa98Sdjl 		 */
40418bdb8a7Smichen 		if (*logfile == '\0') {
40518bdb8a7Smichen 			if (*debug_level != '\0')
4067c478bd9Sstevel@tonic-gate 				/* we're debugging... */
407cb5caa98Sdjl 				(void) strcpy(logfile, "stderr");
4087c478bd9Sstevel@tonic-gate 			else
409cb5caa98Sdjl 				(void) strcpy(logfile, "/dev/null");
41018bdb8a7Smichen 		}
411cb5caa98Sdjl 		(void) _nscd_add_admin_mod(NULL, 'l', logfile,
412cb5caa98Sdjl 		    msg, sizeof (msg));
41318bdb8a7Smichen 		(void) _nscd_add_admin_mod(NULL, 'd', debug_level,
41418bdb8a7Smichen 		    msg, sizeof (msg));
415cb5caa98Sdjl 
416cb5caa98Sdjl 		/* activate command options */
417cb5caa98Sdjl 		if (_nscd_server_setadmin(NULL) != NSCD_SUCCESS) {
418cb5caa98Sdjl 			(void) fprintf(stderr,
419cb5caa98Sdjl 			gettext("unable to set command line options\n"));
420cb5caa98Sdjl 			exit(1);
421cb5caa98Sdjl 		}
422cb5caa98Sdjl 
42318bdb8a7Smichen 		if (*debug_level != '\0') {
424cb5caa98Sdjl 			/* we're debugging, no forking of nscd */
425cb5caa98Sdjl 
426cb5caa98Sdjl 			/*
427cb5caa98Sdjl 			 * forker nscd will be started if self credential
428cb5caa98Sdjl 			 * is configured
429cb5caa98Sdjl 			 */
430cb5caa98Sdjl 			_nscd_start_forker(saved_execname, saved_argc,
431cb5caa98Sdjl 			    saved_argv);
4327c478bd9Sstevel@tonic-gate 		} else {
433cb5caa98Sdjl 			/*
434cb5caa98Sdjl 			 * daemonize the nscd (forker nscd will also
435cb5caa98Sdjl 			 * be started if self credential is configured)
436cb5caa98Sdjl 			 */
4377c478bd9Sstevel@tonic-gate 			detachfromtty();
4387c478bd9Sstevel@tonic-gate 		}
439cb5caa98Sdjl 	} else { /* NSCD_FORKER */
44009480fbfSSreedhar Chalamalasetti - Sun Microsystems - Bangalore India 		/*
44109480fbfSSreedhar Chalamalasetti - Sun Microsystems - Bangalore India 		 * To avoid PUN (Per User Nscd) processes from becoming
44209480fbfSSreedhar Chalamalasetti - Sun Microsystems - Bangalore India 		 * zombies after they exit, the forking nscd should
44309480fbfSSreedhar Chalamalasetti - Sun Microsystems - Bangalore India 		 * ignore the SIGCLD signal so that it does not
44409480fbfSSreedhar Chalamalasetti - Sun Microsystems - Bangalore India 		 * need to wait for every child PUN to exit.
44509480fbfSSreedhar Chalamalasetti - Sun Microsystems - Bangalore India 		 */
44609480fbfSSreedhar Chalamalasetti - Sun Microsystems - Bangalore India 		(void) signal(SIGCLD, SIG_IGN);
447cb5caa98Sdjl 		(void) open("/dev/null", O_RDWR, 0);
448cb5caa98Sdjl 		(void) dup(0);
449cb5caa98Sdjl 		if (_logfd != 2)
450cb5caa98Sdjl 			(void) dup(0);
4517c478bd9Sstevel@tonic-gate 	}
4527c478bd9Sstevel@tonic-gate 
45349959095SJulian Pullen 	/* set NOFILE to unlimited */
45449959095SJulian Pullen 	rl.rlim_cur = rl.rlim_max = RLIM_INFINITY;
45549959095SJulian Pullen 	if (setrlimit(RLIMIT_NOFILE, &rl) < 0) {
45649959095SJulian Pullen 		_NSCD_LOG(NSCD_LOG_FRONT_END, NSCD_LOG_LEVEL_ERROR)
45749959095SJulian Pullen 		(me, "Cannot set open file limit: %s\n", strerror(errno));
45849959095SJulian Pullen 		exit(1);
45949959095SJulian Pullen 	}
46049959095SJulian Pullen 
461cb5caa98Sdjl 	/* set up door and establish our own server thread pool */
462cb5caa98Sdjl 	if ((_doorfd = _nscd_setup_server(saved_execname, saved_argv)) == -1) {
463cb5caa98Sdjl 		_NSCD_LOG(NSCD_LOG_FRONT_END, NSCD_LOG_LEVEL_ERROR)
464cb5caa98Sdjl 		(me, "unable to set up door\n");
4657c478bd9Sstevel@tonic-gate 		exit(1);
4667c478bd9Sstevel@tonic-gate 	}
4677c478bd9Sstevel@tonic-gate 
468cb5caa98Sdjl 	/* inform the main nscd that this forker is ready */
469cb5caa98Sdjl 	if (_whoami == NSCD_FORKER) {
470cb5caa98Sdjl 		int	ret;
4717c478bd9Sstevel@tonic-gate 
472cb5caa98Sdjl 		for (ret = NSS_ALTRETRY; ret == NSS_ALTRETRY; )
473cb5caa98Sdjl 			ret = _nscd_doorcall_sendfd(_doorfd,
474cb5caa98Sdjl 			    NSCD_IMHERE | (NSCD_FORKER & NSCD_WHOAMI),
475cb5caa98Sdjl 			    NULL, 0, NULL);
4767c478bd9Sstevel@tonic-gate 	}
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	for (;;) {
4797c478bd9Sstevel@tonic-gate 		(void) pause();
480cb5caa98Sdjl 		(void) _nscd_doorcall(NSCD_REFRESH);
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate 
483cb5caa98Sdjl 	/* NOTREACHED */
484cb5caa98Sdjl 	/*LINTED E_FUNC_HAS_NO_RETURN_STMT*/
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate static void
usage(char * s)4887c478bd9Sstevel@tonic-gate usage(char *s)
4897c478bd9Sstevel@tonic-gate {
4907c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
4917c478bd9Sstevel@tonic-gate 	    "Usage: %s [-d debug_level] [-l logfilename]\n", s);
4927c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
4937c478bd9Sstevel@tonic-gate 	    "	[-p cachename,positive_time_to_live]\n");
4947c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
4957c478bd9Sstevel@tonic-gate 	    "	[-n cachename,negative_time_to_live]\n");
4967c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
497cb5caa98Sdjl 	    "	[-i cachename]\n");
4987c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
499cb5caa98Sdjl 	    "	[-h cachename,keep_hot_count]\n");
5007c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
5017c478bd9Sstevel@tonic-gate 	    "	[-e cachename,\"yes\"|\"no\"] [-g] " \
5027c478bd9Sstevel@tonic-gate 	    "[-c cachename,\"yes\"|\"no\"]\n");
5037c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
5047c478bd9Sstevel@tonic-gate 	    "	[-f configfilename] \n");
5057c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
506cb5caa98Sdjl 	    "\n	Supported caches:\n");
5077c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
50807925104Sgww 	    "	  auth_attr, bootparams, ethers\n");
509cb5caa98Sdjl 	(void) fprintf(stderr,
510cb5caa98Sdjl 	    "	  exec_attr, group, hosts, ipnodes, netmasks\n");
511cb5caa98Sdjl 	(void) fprintf(stderr,
512cb5caa98Sdjl 	    "	  networks, passwd, printers, prof_attr, project\n");
513cb5caa98Sdjl 	(void) fprintf(stderr,
514cb5caa98Sdjl 	    "	  protocols, rpc, services, tnrhtp, tnrhdb\n");
515cb5caa98Sdjl 	(void) fprintf(stderr,
516cb5caa98Sdjl 	    "	  user_attr\n");
5177c478bd9Sstevel@tonic-gate 	exit(1);
5187c478bd9Sstevel@tonic-gate }
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate /*
5217c478bd9Sstevel@tonic-gate  * detach from tty
5227c478bd9Sstevel@tonic-gate  */
5237c478bd9Sstevel@tonic-gate static void
detachfromtty(void)5247c478bd9Sstevel@tonic-gate detachfromtty(void)
5257c478bd9Sstevel@tonic-gate {
526cb5caa98Sdjl 	nscd_rc_t	rc;
527cb5caa98Sdjl 	char		*me = "detachfromtty";
528cb5caa98Sdjl 
529cb5caa98Sdjl 	if (_logfd > 0) {
5307c478bd9Sstevel@tonic-gate 		int i;
531cb5caa98Sdjl 		for (i = 0; i < _logfd; i++)
5327c478bd9Sstevel@tonic-gate 			(void) close(i);
533cb5caa98Sdjl 		closefrom(_logfd + 1);
5347c478bd9Sstevel@tonic-gate 	} else
5357c478bd9Sstevel@tonic-gate 		closefrom(0);
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	(void) chdir("/");
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	switch (fork1()) {
5407c478bd9Sstevel@tonic-gate 	case (pid_t)-1:
541cb5caa98Sdjl 
542cb5caa98Sdjl 		_NSCD_LOG(NSCD_LOG_FRONT_END, NSCD_LOG_LEVEL_ERROR)
543cb5caa98Sdjl 		(me, "unable to fork: pid = %d, %s\n",
544cb5caa98Sdjl 		    getpid(), strerror(errno));
545cb5caa98Sdjl 
5467c478bd9Sstevel@tonic-gate 		exit(1);
5477c478bd9Sstevel@tonic-gate 		break;
5487c478bd9Sstevel@tonic-gate 	case 0:
549cb5caa98Sdjl 		/* start the forker nscd if so configured */
550cb5caa98Sdjl 		_nscd_start_forker(saved_execname, saved_argc, saved_argv);
5517c478bd9Sstevel@tonic-gate 		break;
5527c478bd9Sstevel@tonic-gate 	default:
5537c478bd9Sstevel@tonic-gate 		exit(0);
5547c478bd9Sstevel@tonic-gate 	}
55549959095SJulian Pullen 
5567c478bd9Sstevel@tonic-gate 	(void) setsid();
5577c478bd9Sstevel@tonic-gate 	(void) open("/dev/null", O_RDWR, 0);
5587c478bd9Sstevel@tonic-gate 	(void) dup(0);
559cb5caa98Sdjl 	if (_logfd != 2)
5607c478bd9Sstevel@tonic-gate 		(void) dup(0);
561cb5caa98Sdjl 
562cb5caa98Sdjl 	/*
563cb5caa98Sdjl 	 * start monitoring the states of the name service clients
564cb5caa98Sdjl 	 */
565cb5caa98Sdjl 	rc = _nscd_init_smf_monitor();
566cb5caa98Sdjl 	if (rc != NSCD_SUCCESS) {
567cb5caa98Sdjl 		_NSCD_LOG(NSCD_LOG_FRONT_END, NSCD_LOG_LEVEL_ERROR)
568cb5caa98Sdjl 	(me, "unable to start the SMF monitor (rc = %d)\n", rc);
569cb5caa98Sdjl 
570cb5caa98Sdjl 		exit(-1);
571cb5caa98Sdjl 	}
5727c478bd9Sstevel@tonic-gate }
573