xref: /titanic_54/usr/src/cmd/ldapcachemgr/cachemgr.c (revision cb5caa98562cf06753163f558cbcfe30b8f4673a)
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
5*cb5caa98Sdjl  * Common Development and Distribution License (the "License").
6*cb5caa98Sdjl  * 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*cb5caa98Sdjl  * 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 ldap cache daemon
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
347c478bd9Sstevel@tonic-gate #include <signal.h>
357c478bd9Sstevel@tonic-gate #include <door.h>
367c478bd9Sstevel@tonic-gate #include <time.h>
377c478bd9Sstevel@tonic-gate #include <string.h>
387c478bd9Sstevel@tonic-gate #include <libintl.h>
397c478bd9Sstevel@tonic-gate #include <sys/stat.h>
407c478bd9Sstevel@tonic-gate #include <sys/time.h>
417c478bd9Sstevel@tonic-gate #include <sys/wait.h>
427c478bd9Sstevel@tonic-gate #include <stdlib.h>
437c478bd9Sstevel@tonic-gate #include <errno.h>
447c478bd9Sstevel@tonic-gate #include <pthread.h>
457c478bd9Sstevel@tonic-gate #include <thread.h>
467c478bd9Sstevel@tonic-gate #include <stdarg.h>
477c478bd9Sstevel@tonic-gate #include <fcntl.h>
487c478bd9Sstevel@tonic-gate #include <assert.h>
497c478bd9Sstevel@tonic-gate #include <unistd.h>
507c478bd9Sstevel@tonic-gate #include <memory.h>
517c478bd9Sstevel@tonic-gate #include <sys/types.h>
527c478bd9Sstevel@tonic-gate #include <syslog.h>
537c478bd9Sstevel@tonic-gate #include <locale.h>	/* LC_ALL */
547c478bd9Sstevel@tonic-gate #include "cachemgr.h"
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate static void	detachfromtty();
577c478bd9Sstevel@tonic-gate admin_t		current_admin;
587c478bd9Sstevel@tonic-gate static int	will_become_server;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate static void switcher(void *cookie, char *argp, size_t arg_size,
617c478bd9Sstevel@tonic-gate 			door_desc_t *dp, uint_t n_desc);
627c478bd9Sstevel@tonic-gate static void usage(char *s);
637c478bd9Sstevel@tonic-gate static int cachemgr_set_lf(admin_t *ptr, char *logfile);
647c478bd9Sstevel@tonic-gate static int client_getadmin(admin_t *ptr);
657c478bd9Sstevel@tonic-gate static int getadmin(ldap_return_t *out);
667c478bd9Sstevel@tonic-gate static int setadmin(ldap_return_t *out, ldap_call_t *ptr);
677c478bd9Sstevel@tonic-gate static  int client_setadmin(admin_t *ptr);
687c478bd9Sstevel@tonic-gate static int client_showstats(admin_t *ptr);
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #ifdef SLP
717c478bd9Sstevel@tonic-gate int			use_slp = 0;
727c478bd9Sstevel@tonic-gate static unsigned int	refresh = 10800;	/* dynamic discovery interval */
737c478bd9Sstevel@tonic-gate #endif /* SLP */
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate static ldap_stat_t *
767c478bd9Sstevel@tonic-gate getcacheptr(char *s)
777c478bd9Sstevel@tonic-gate {
787c478bd9Sstevel@tonic-gate 	static const char *caches[1] = {"ldap"};
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	if (strncmp(caches[0], s, strlen(caches[0])) == 0)
817c478bd9Sstevel@tonic-gate 		return (&current_admin.ldap_stat);
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	return (NULL);
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate char *
877c478bd9Sstevel@tonic-gate getcacheopt(char *s)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	while (*s && *s != ',')
907c478bd9Sstevel@tonic-gate 		s++;
917c478bd9Sstevel@tonic-gate 	return ((*s == ',') ? (s + 1) : NULL);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  *  This is here to prevent the ldap_cachemgr becomes
967c478bd9Sstevel@tonic-gate  *  daemonlized to early to soon during boot time.
977c478bd9Sstevel@tonic-gate  *  This causes problems during boot when automounter
987c478bd9Sstevel@tonic-gate  *  and others try to use libsldap before ldap_cachemgr
997c478bd9Sstevel@tonic-gate  *  finishes walking the server list.
1007c478bd9Sstevel@tonic-gate  */
1017c478bd9Sstevel@tonic-gate static void
1027c478bd9Sstevel@tonic-gate sig_ok_to_exit(int signo)
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate 	if (signo == SIGUSR1) {
1057c478bd9Sstevel@tonic-gate 		logit("sig_ok_to_exit(): parent exiting...\n");
1067c478bd9Sstevel@tonic-gate 		exit(0);
1077c478bd9Sstevel@tonic-gate 	} else {
1087c478bd9Sstevel@tonic-gate 		logit("sig_ok_to_exit(): invalid signal(%d) received.\n",
1097c478bd9Sstevel@tonic-gate 			signo);
1107c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, gettext("ldap_cachemgr: "
1117c478bd9Sstevel@tonic-gate 			"invalid signal(%d) received."), signo);
1127c478bd9Sstevel@tonic-gate 		exit(1);
1137c478bd9Sstevel@tonic-gate 	}
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate #define	LDAP_TABLES		1	/* ldap */
1167c478bd9Sstevel@tonic-gate #define	TABLE_THREADS		10
1177c478bd9Sstevel@tonic-gate #define	COMMON_THREADS		20
1187c478bd9Sstevel@tonic-gate #define	CACHE_MISS_THREADS	(COMMON_THREADS + LDAP_TABLES * TABLE_THREADS)
1197c478bd9Sstevel@tonic-gate #define	CACHE_HIT_THREADS	20
1207c478bd9Sstevel@tonic-gate #define	MAX_SERVER_THREADS	(CACHE_HIT_THREADS + CACHE_MISS_THREADS)
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate static sema_t common_sema;
1237c478bd9Sstevel@tonic-gate static sema_t ldap_sema;
1247c478bd9Sstevel@tonic-gate static thread_key_t lookup_state_key;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate static void
1277c478bd9Sstevel@tonic-gate initialize_lookup_clearance()
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate 	(void) thr_keycreate(&lookup_state_key, NULL);
1307c478bd9Sstevel@tonic-gate 	(void) sema_init(&common_sema, COMMON_THREADS, USYNC_THREAD, 0);
1317c478bd9Sstevel@tonic-gate 	(void) sema_init(&ldap_sema, TABLE_THREADS, USYNC_THREAD, 0);
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate int
1357c478bd9Sstevel@tonic-gate get_clearance(int callnumber)
1367c478bd9Sstevel@tonic-gate {
1377c478bd9Sstevel@tonic-gate 	sema_t	*table_sema = NULL;
1387c478bd9Sstevel@tonic-gate 	char	*tab;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	if (sema_trywait(&common_sema) == 0) {
1417c478bd9Sstevel@tonic-gate 		(void) thr_setspecific(lookup_state_key, NULL);
1427c478bd9Sstevel@tonic-gate 		return (0);
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	switch (callnumber) {
1467c478bd9Sstevel@tonic-gate 		case GETLDAPCONFIG:
1477c478bd9Sstevel@tonic-gate 			tab = "ldap";
1487c478bd9Sstevel@tonic-gate 			table_sema = &ldap_sema;
1497c478bd9Sstevel@tonic-gate 			break;
1507c478bd9Sstevel@tonic-gate 		default:
1517c478bd9Sstevel@tonic-gate 			logit("Internal Error: get_clearance\n");
1527c478bd9Sstevel@tonic-gate 			break;
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	if (sema_trywait(table_sema) == 0) {
1567c478bd9Sstevel@tonic-gate 		(void) thr_setspecific(lookup_state_key, (void*)1);
1577c478bd9Sstevel@tonic-gate 		return (0);
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_CANT_FIND) {
1617c478bd9Sstevel@tonic-gate 		logit("get_clearance: throttling load for %s table\n", tab);
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	return (-1);
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate int
1687c478bd9Sstevel@tonic-gate release_clearance(int callnumber)
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate 	int	which;
1717c478bd9Sstevel@tonic-gate 	sema_t	*table_sema = NULL;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	(void) thr_getspecific(lookup_state_key, (void**)&which);
1747c478bd9Sstevel@tonic-gate 	if (which == 0) /* from common pool */ {
1757c478bd9Sstevel@tonic-gate 		(void) sema_post(&common_sema);
1767c478bd9Sstevel@tonic-gate 		return (0);
1777c478bd9Sstevel@tonic-gate 	}
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	switch (callnumber) {
1807c478bd9Sstevel@tonic-gate 		case GETLDAPCONFIG:
1817c478bd9Sstevel@tonic-gate 			table_sema = &ldap_sema;
1827c478bd9Sstevel@tonic-gate 			break;
1837c478bd9Sstevel@tonic-gate 		default:
1847c478bd9Sstevel@tonic-gate 			logit("Internal Error: release_clearance\n");
1857c478bd9Sstevel@tonic-gate 			break;
1867c478bd9Sstevel@tonic-gate 	}
1877c478bd9Sstevel@tonic-gate 	(void) sema_post(table_sema);
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	return (0);
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate static mutex_t		create_lock;
1947c478bd9Sstevel@tonic-gate static int		num_servers = 0;
1957c478bd9Sstevel@tonic-gate static thread_key_t	server_key;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate /*
1997c478bd9Sstevel@tonic-gate  * Bind a TSD value to a server thread. This enables the destructor to
2007c478bd9Sstevel@tonic-gate  * be called if/when this thread exits.  This would be a programming error,
2017c478bd9Sstevel@tonic-gate  * but better safe than sorry.
2027c478bd9Sstevel@tonic-gate  */
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2057c478bd9Sstevel@tonic-gate static void *
2067c478bd9Sstevel@tonic-gate server_tsd_bind(void *arg)
2077c478bd9Sstevel@tonic-gate {
2087c478bd9Sstevel@tonic-gate 	static void	*value = 0;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	/*
2117c478bd9Sstevel@tonic-gate 	 * disable cancellation to prevent hangs when server
2127c478bd9Sstevel@tonic-gate 	 * threads disappear
2137c478bd9Sstevel@tonic-gate 	 */
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	(void) thr_setspecific(server_key, value);
216*cb5caa98Sdjl 	(void) door_return(NULL, 0, NULL, 0);
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	return (value);
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate /*
2227c478bd9Sstevel@tonic-gate  * Server threads are created here.
2237c478bd9Sstevel@tonic-gate  */
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2267c478bd9Sstevel@tonic-gate static void
2277c478bd9Sstevel@tonic-gate server_create(door_info_t *dip)
2287c478bd9Sstevel@tonic-gate {
2297c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&create_lock);
2307c478bd9Sstevel@tonic-gate 	if (++num_servers > MAX_SERVER_THREADS) {
2317c478bd9Sstevel@tonic-gate 		num_servers--;
2327c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&create_lock);
2337c478bd9Sstevel@tonic-gate 		return;
2347c478bd9Sstevel@tonic-gate 	}
2357c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&create_lock);
2367c478bd9Sstevel@tonic-gate 	(void) thr_create(NULL, 0, server_tsd_bind, NULL,
2377c478bd9Sstevel@tonic-gate 		THR_BOUND|THR_DETACHED, NULL);
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate /*
2417c478bd9Sstevel@tonic-gate  * Server thread are destroyed here
2427c478bd9Sstevel@tonic-gate  */
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2457c478bd9Sstevel@tonic-gate static void
2467c478bd9Sstevel@tonic-gate server_destroy(void *arg)
2477c478bd9Sstevel@tonic-gate {
2487c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&create_lock);
2497c478bd9Sstevel@tonic-gate 	num_servers--;
2507c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&create_lock);
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate 
253a506a34cSth160488 int
2547c478bd9Sstevel@tonic-gate main(int argc, char ** argv)
2557c478bd9Sstevel@tonic-gate {
2567c478bd9Sstevel@tonic-gate 	int			did;
2577c478bd9Sstevel@tonic-gate 	int			opt;
2587c478bd9Sstevel@tonic-gate 	int			errflg = 0;
2597c478bd9Sstevel@tonic-gate 	int			showstats = 0;
2607c478bd9Sstevel@tonic-gate 	int			doset = 0;
2617c478bd9Sstevel@tonic-gate 	int			dofg = 0;
2627c478bd9Sstevel@tonic-gate 	struct stat		buf;
2637c478bd9Sstevel@tonic-gate 	sigset_t		myset;
2647c478bd9Sstevel@tonic-gate 	struct sigaction	sighupaction;
2657c478bd9Sstevel@tonic-gate 	static void		client_killserver();
2667c478bd9Sstevel@tonic-gate 	int			debug_level = 0;
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	/* setup for localization */
2697c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2707c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	openlog("ldap_cachemgr", LOG_PID, LOG_DAEMON);
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	if (chdir(NSLDAPDIRECTORY) < 0) {
2757c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("chdir(\"%s\") failed: %s\n"),
2767c478bd9Sstevel@tonic-gate 			NSLDAPDIRECTORY, strerror(errno));
2777c478bd9Sstevel@tonic-gate 		exit(1);
2787c478bd9Sstevel@tonic-gate 	}
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	/*
2817c478bd9Sstevel@tonic-gate 	 * Correctly set file mode creation mask, so to make the new files
2827c478bd9Sstevel@tonic-gate 	 * created for door calls being readable by all.
2837c478bd9Sstevel@tonic-gate 	 */
2847c478bd9Sstevel@tonic-gate 	(void) umask(0);
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	/*
2877c478bd9Sstevel@tonic-gate 	 *  Special case non-root user here -	he/she/they/it can just print
2887c478bd9Sstevel@tonic-gate 	 *					stats
2897c478bd9Sstevel@tonic-gate 	 */
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	if (geteuid()) {
2927c478bd9Sstevel@tonic-gate 		if (argc != 2 || strcmp(argv[1], "-g")) {
2937c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2947c478bd9Sstevel@tonic-gate 			    gettext("Must be root to use any option "
2957c478bd9Sstevel@tonic-gate 			    "other than -g.\n\n"));
2967c478bd9Sstevel@tonic-gate 			usage(argv[0]);
2977c478bd9Sstevel@tonic-gate 		}
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 		if ((__ns_ldap_cache_ping() != SUCCESS) ||
3007c478bd9Sstevel@tonic-gate 		    (client_getadmin(&current_admin) != 0)) {
3017c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3027c478bd9Sstevel@tonic-gate 				gettext("%s doesn't appear to be running.\n"),
3037c478bd9Sstevel@tonic-gate 				argv[0]);
3047c478bd9Sstevel@tonic-gate 			exit(1);
3057c478bd9Sstevel@tonic-gate 		}
3067c478bd9Sstevel@tonic-gate 		(void) client_showstats(&current_admin);
3077c478bd9Sstevel@tonic-gate 		exit(0);
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	/*
3137c478bd9Sstevel@tonic-gate 	 *  Determine if there is already a daemon running
3147c478bd9Sstevel@tonic-gate 	 */
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	will_become_server = (__ns_ldap_cache_ping() != SUCCESS);
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	/*
3197c478bd9Sstevel@tonic-gate 	 *  load normal config file
3207c478bd9Sstevel@tonic-gate 	 */
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	if (will_become_server) {
3237c478bd9Sstevel@tonic-gate 		static const ldap_stat_t defaults = {
3247c478bd9Sstevel@tonic-gate 			0,		/* stat */
3257c478bd9Sstevel@tonic-gate 			DEFAULTTTL};	/* ttl */
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 		current_admin.ldap_stat = defaults;
3287c478bd9Sstevel@tonic-gate 		(void) strcpy(current_admin.logfile, LOGFILE);
3297c478bd9Sstevel@tonic-gate 	} else {
3307c478bd9Sstevel@tonic-gate 		if (client_getadmin(&current_admin)) {
3317c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("Cannot contact %s "
3327c478bd9Sstevel@tonic-gate 				"properly(?)\n"), argv[0]);
3337c478bd9Sstevel@tonic-gate 			exit(1);
3347c478bd9Sstevel@tonic-gate 		}
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate #ifndef SLP
3387c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "fKgl:r:d:")) != EOF) {
3397c478bd9Sstevel@tonic-gate #else
3407c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "fKgs:l:r:d:")) != EOF) {
3417c478bd9Sstevel@tonic-gate #endif /* SLP */
3427c478bd9Sstevel@tonic-gate 		ldap_stat_t	*cache;
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 		switch (opt) {
3457c478bd9Sstevel@tonic-gate 		case 'K':
3467c478bd9Sstevel@tonic-gate 			client_killserver();
3477c478bd9Sstevel@tonic-gate 			exit(0);
3487c478bd9Sstevel@tonic-gate 			break;
3497c478bd9Sstevel@tonic-gate 		case 'g':
3507c478bd9Sstevel@tonic-gate 			showstats++;
3517c478bd9Sstevel@tonic-gate 			break;
3527c478bd9Sstevel@tonic-gate 		case 'f':
3537c478bd9Sstevel@tonic-gate 			dofg++;
3547c478bd9Sstevel@tonic-gate 			break;
3557c478bd9Sstevel@tonic-gate 		case 'r':
3567c478bd9Sstevel@tonic-gate 			doset++;
3577c478bd9Sstevel@tonic-gate 			cache = getcacheptr("ldap");
3587c478bd9Sstevel@tonic-gate 			if (!optarg) {
3597c478bd9Sstevel@tonic-gate 				errflg++;
3607c478bd9Sstevel@tonic-gate 				break;
3617c478bd9Sstevel@tonic-gate 			}
3627c478bd9Sstevel@tonic-gate 			cache->ldap_ttl = atoi(optarg);
3637c478bd9Sstevel@tonic-gate 			break;
3647c478bd9Sstevel@tonic-gate 		case 'l':
3657c478bd9Sstevel@tonic-gate 			doset++;
3667c478bd9Sstevel@tonic-gate 			(void) strlcpy(current_admin.logfile,
3677c478bd9Sstevel@tonic-gate 				optarg, sizeof (current_admin.logfile));
3687c478bd9Sstevel@tonic-gate 			break;
3697c478bd9Sstevel@tonic-gate 		case 'd':
3707c478bd9Sstevel@tonic-gate 			doset++;
3717c478bd9Sstevel@tonic-gate 			debug_level = atoi(optarg);
3727c478bd9Sstevel@tonic-gate 			break;
3737c478bd9Sstevel@tonic-gate #ifdef SLP
3747c478bd9Sstevel@tonic-gate 		case 's':	/* undocumented: use dynamic (SLP) config */
3757c478bd9Sstevel@tonic-gate 			use_slp = 1;
3767c478bd9Sstevel@tonic-gate 			break;
3777c478bd9Sstevel@tonic-gate #endif /* SLP */
3787c478bd9Sstevel@tonic-gate 		default:
3797c478bd9Sstevel@tonic-gate 			errflg++;
3807c478bd9Sstevel@tonic-gate 			break;
3817c478bd9Sstevel@tonic-gate 		}
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	if (errflg)
3857c478bd9Sstevel@tonic-gate 	    usage(argv[0]);
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	/*
3887c478bd9Sstevel@tonic-gate 	 * will not show statistics if no daemon running
3897c478bd9Sstevel@tonic-gate 	 */
3907c478bd9Sstevel@tonic-gate 	if (will_become_server && showstats) {
3917c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3927c478bd9Sstevel@tonic-gate 			gettext("%s doesn't appear to be running.\n"),
3937c478bd9Sstevel@tonic-gate 				argv[0]);
3947c478bd9Sstevel@tonic-gate 		exit(1);
3957c478bd9Sstevel@tonic-gate 	}
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	if (!will_become_server) {
3987c478bd9Sstevel@tonic-gate 		if (showstats) {
3997c478bd9Sstevel@tonic-gate 			(void) client_showstats(&current_admin);
4007c478bd9Sstevel@tonic-gate 		}
4017c478bd9Sstevel@tonic-gate 		if (doset) {
4027c478bd9Sstevel@tonic-gate 			current_admin.debug_level = debug_level;
4037c478bd9Sstevel@tonic-gate 			if (client_setadmin(&current_admin) < 0) {
4047c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
4057c478bd9Sstevel@tonic-gate 					gettext("Error during admin call\n"));
4067c478bd9Sstevel@tonic-gate 				exit(1);
4077c478bd9Sstevel@tonic-gate 			}
4087c478bd9Sstevel@tonic-gate 		}
4097c478bd9Sstevel@tonic-gate 		if (!showstats && !doset) {
4107c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4117c478bd9Sstevel@tonic-gate 			gettext("%s already running....use '%s "
4127c478bd9Sstevel@tonic-gate 				"-K' to stop\n"), argv[0], argv[0]);
4137c478bd9Sstevel@tonic-gate 		}
4147c478bd9Sstevel@tonic-gate 		exit(0);
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	/*
4187c478bd9Sstevel@tonic-gate 	 *   daemon from here on
4197c478bd9Sstevel@tonic-gate 	 */
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	if (debug_level) {
4227c478bd9Sstevel@tonic-gate 		/*
4237c478bd9Sstevel@tonic-gate 		 * we're debugging...
4247c478bd9Sstevel@tonic-gate 		 */
4257c478bd9Sstevel@tonic-gate 		if (strlen(current_admin.logfile) == 0)
4267c478bd9Sstevel@tonic-gate 			/*
4277c478bd9Sstevel@tonic-gate 			 * no specified log file
4287c478bd9Sstevel@tonic-gate 			 */
4297c478bd9Sstevel@tonic-gate 			(void) strcpy(current_admin.logfile, LOGFILE);
4307c478bd9Sstevel@tonic-gate 		else
4317c478bd9Sstevel@tonic-gate 			(void) cachemgr_set_lf(&current_admin,
4327c478bd9Sstevel@tonic-gate 				current_admin.logfile);
4337c478bd9Sstevel@tonic-gate 		/*
4347c478bd9Sstevel@tonic-gate 		 * validate the range of debug level number
4357c478bd9Sstevel@tonic-gate 		 * and set the number to current_admin.debug_level
4367c478bd9Sstevel@tonic-gate 		 */
4377c478bd9Sstevel@tonic-gate 		if (cachemgr_set_dl(&current_admin, debug_level) < 0) {
4387c478bd9Sstevel@tonic-gate 				/*
4397c478bd9Sstevel@tonic-gate 				 * print error messages to the screen
4407c478bd9Sstevel@tonic-gate 				 * cachemgr_set_dl prints msgs to cachemgr.log
4417c478bd9Sstevel@tonic-gate 				 * only
4427c478bd9Sstevel@tonic-gate 				 */
4437c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
4447c478bd9Sstevel@tonic-gate 				gettext("Incorrect Debug Level: %d\n"
4457c478bd9Sstevel@tonic-gate 				"It should be between %d and %d\n"),
4467c478bd9Sstevel@tonic-gate 				debug_level, DBG_OFF, MAXDEBUG);
4477c478bd9Sstevel@tonic-gate 			exit(-1);
4487c478bd9Sstevel@tonic-gate 		}
4497c478bd9Sstevel@tonic-gate 	} else {
4507c478bd9Sstevel@tonic-gate 		if (strlen(current_admin.logfile) == 0)
4517c478bd9Sstevel@tonic-gate 			(void) strcpy(current_admin.logfile, "/dev/null");
4527c478bd9Sstevel@tonic-gate 			(void) cachemgr_set_lf(&current_admin,
4537c478bd9Sstevel@tonic-gate 				current_admin.logfile);
4547c478bd9Sstevel@tonic-gate 	}
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	if (dofg == 0)
4577c478bd9Sstevel@tonic-gate 		detachfromtty(argv[0]);
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	/*
4607c478bd9Sstevel@tonic-gate 	 * perform some initialization
4617c478bd9Sstevel@tonic-gate 	 */
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	initialize_lookup_clearance();
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	if (getldap_init() != 0)
4667c478bd9Sstevel@tonic-gate 		exit(-1);
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	/*
4697c478bd9Sstevel@tonic-gate 	 * Establish our own server thread pool
4707c478bd9Sstevel@tonic-gate 	 */
4717c478bd9Sstevel@tonic-gate 
472*cb5caa98Sdjl 	(void) door_server_create(server_create);
4737c478bd9Sstevel@tonic-gate 	if (thr_keycreate(&server_key, server_destroy) != 0) {
4747c478bd9Sstevel@tonic-gate 		logit("thr_keycreate() call failed\n");
4757c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
4767c478bd9Sstevel@tonic-gate 			gettext("ldap_cachemgr: thr_keycreate() call failed"));
4777c478bd9Sstevel@tonic-gate 		perror("thr_keycreate");
4787c478bd9Sstevel@tonic-gate 		exit(-1);
4797c478bd9Sstevel@tonic-gate 	}
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	/*
4827c478bd9Sstevel@tonic-gate 	 * Create a door
4837c478bd9Sstevel@tonic-gate 	 */
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	if ((did = door_create(switcher, LDAP_CACHE_DOOR_COOKIE,
4867c478bd9Sstevel@tonic-gate 	    DOOR_UNREF | DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) < 0) {
4877c478bd9Sstevel@tonic-gate 		logit("door_create() call failed\n");
4887c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, gettext(
4897c478bd9Sstevel@tonic-gate 			"ldap_cachemgr: door_create() call failed"));
4907c478bd9Sstevel@tonic-gate 		perror("door_create");
4917c478bd9Sstevel@tonic-gate 		exit(-1);
4927c478bd9Sstevel@tonic-gate 	}
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	/*
4957c478bd9Sstevel@tonic-gate 	 * bind to file system
4967c478bd9Sstevel@tonic-gate 	 */
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	if (stat(LDAP_CACHE_DOOR, &buf) < 0) {
4997c478bd9Sstevel@tonic-gate 		int	newfd;
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 		if ((newfd = creat(LDAP_CACHE_DOOR, 0444)) < 0) {
5027c478bd9Sstevel@tonic-gate 			logit("Cannot create %s:%s\n",
5037c478bd9Sstevel@tonic-gate 				LDAP_CACHE_DOOR,
5047c478bd9Sstevel@tonic-gate 				strerror(errno));
5057c478bd9Sstevel@tonic-gate 			exit(1);
5067c478bd9Sstevel@tonic-gate 		}
5077c478bd9Sstevel@tonic-gate 		(void) close(newfd);
5087c478bd9Sstevel@tonic-gate 	}
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	if (fattach(did, LDAP_CACHE_DOOR) < 0) {
5117c478bd9Sstevel@tonic-gate 		if ((errno != EBUSY) ||
5127c478bd9Sstevel@tonic-gate 		    (fdetach(LDAP_CACHE_DOOR) <  0) ||
5137c478bd9Sstevel@tonic-gate 		    (fattach(did, LDAP_CACHE_DOOR) < 0)) {
5147c478bd9Sstevel@tonic-gate 			logit("fattach() call failed\n");
5157c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, gettext(
5167c478bd9Sstevel@tonic-gate 				"ldap_cachemgr: fattach() call failed"));
5177c478bd9Sstevel@tonic-gate 			perror("fattach");
5187c478bd9Sstevel@tonic-gate 			exit(2);
5197c478bd9Sstevel@tonic-gate 		}
5207c478bd9Sstevel@tonic-gate 	}
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	/* catch SIGHUP revalid signals */
5237c478bd9Sstevel@tonic-gate 	sighupaction.sa_handler = getldap_revalidate;
5247c478bd9Sstevel@tonic-gate 	sighupaction.sa_flags = 0;
5257c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sighupaction.sa_mask);
5267c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&myset);
5277c478bd9Sstevel@tonic-gate 	(void) sigaddset(&myset, SIGHUP);
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	if (sigaction(SIGHUP, &sighupaction, NULL) < 0) {
5307c478bd9Sstevel@tonic-gate 		logit("sigaction() call failed\n");
5317c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
5327c478bd9Sstevel@tonic-gate 			gettext("ldap_cachemgr: sigaction() call failed"));
5337c478bd9Sstevel@tonic-gate 		perror("sigaction");
5347c478bd9Sstevel@tonic-gate 		exit(1);
5357c478bd9Sstevel@tonic-gate 	}
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	if (thr_sigsetmask(SIG_BLOCK, &myset, NULL) < 0) {
5387c478bd9Sstevel@tonic-gate 		logit("thr_sigsetmask() call failed\n");
5397c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
5407c478bd9Sstevel@tonic-gate 			gettext("ldap_cachemgr: thr_sigsetmask() call failed"));
5417c478bd9Sstevel@tonic-gate 		perror("thr_sigsetmask");
5427c478bd9Sstevel@tonic-gate 		exit(1);
5437c478bd9Sstevel@tonic-gate 	}
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	/*
5467c478bd9Sstevel@tonic-gate 	 *  kick off revalidate threads only if ttl != 0
5477c478bd9Sstevel@tonic-gate 	 */
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	if (thr_create(NULL, NULL, (void *(*)(void*))getldap_refresh,
5507c478bd9Sstevel@tonic-gate 		0, 0, NULL) != 0) {
5517c478bd9Sstevel@tonic-gate 		logit("thr_create() call failed\n");
5527c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
5537c478bd9Sstevel@tonic-gate 			gettext("ldap_cachemgr: thr_create() call failed"));
5547c478bd9Sstevel@tonic-gate 		perror("thr_create");
5557c478bd9Sstevel@tonic-gate 		exit(1);
5567c478bd9Sstevel@tonic-gate 	}
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	/*
5597c478bd9Sstevel@tonic-gate 	 *  kick off the thread which refreshes the server info
5607c478bd9Sstevel@tonic-gate 	 */
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	if (thr_create(NULL, NULL, (void *(*)(void*))getldap_serverInfo_refresh,
5637c478bd9Sstevel@tonic-gate 		0, 0, NULL) != 0) {
5647c478bd9Sstevel@tonic-gate 		logit("thr_create() call failed\n");
5657c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
5667c478bd9Sstevel@tonic-gate 			gettext("ldap_cachemgr: thr_create() call failed"));
5677c478bd9Sstevel@tonic-gate 		perror("thr_create");
5687c478bd9Sstevel@tonic-gate 		exit(1);
5697c478bd9Sstevel@tonic-gate 	}
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate #ifdef SLP
5727c478bd9Sstevel@tonic-gate 	if (use_slp) {
5737c478bd9Sstevel@tonic-gate 		/* kick off SLP discovery thread */
5747c478bd9Sstevel@tonic-gate 		if (thr_create(NULL, NULL, (void *(*)(void *))discover,
5757c478bd9Sstevel@tonic-gate 			(void *)&refresh, 0, NULL) != 0) {
5767c478bd9Sstevel@tonic-gate 			logit("thr_create() call failed\n");
5777c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, gettext("ldap_cachemgr: thr_create() "
5787c478bd9Sstevel@tonic-gate 				"call failed"));
5797c478bd9Sstevel@tonic-gate 			perror("thr_create");
5807c478bd9Sstevel@tonic-gate 			exit(1);
5817c478bd9Sstevel@tonic-gate 		}
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate #endif /* SLP */
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	if (thr_sigsetmask(SIG_UNBLOCK, &myset, NULL) < 0) {
5867c478bd9Sstevel@tonic-gate 		logit("thr_sigsetmask() call failed\n");
5877c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
5887c478bd9Sstevel@tonic-gate 			gettext("ldap_cachemgr: the_sigsetmask() call failed"));
5897c478bd9Sstevel@tonic-gate 		perror("thr_sigsetmask");
5907c478bd9Sstevel@tonic-gate 		exit(1);
5917c478bd9Sstevel@tonic-gate 	}
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	/*CONSTCOND*/
5947c478bd9Sstevel@tonic-gate 	while (1) {
5957c478bd9Sstevel@tonic-gate 		(void) pause();
5967c478bd9Sstevel@tonic-gate 	}
597*cb5caa98Sdjl 	/* NOTREACHED */
598*cb5caa98Sdjl 	/*LINTED E_FUNC_HAS_NO_RETURN_STMT*/
5997c478bd9Sstevel@tonic-gate }
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6037c478bd9Sstevel@tonic-gate static void
6047c478bd9Sstevel@tonic-gate switcher(void *cookie, char *argp, size_t arg_size,
6057c478bd9Sstevel@tonic-gate     door_desc_t *dp, uint_t n_desc)
6067c478bd9Sstevel@tonic-gate {
6077c478bd9Sstevel@tonic-gate 	dataunion		u;
6087c478bd9Sstevel@tonic-gate 	ldap_call_t	*ptr = (ldap_call_t *)argp;
6097c478bd9Sstevel@tonic-gate 	door_cred_t	dc;
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	if (argp == DOOR_UNREF_DATA) {
6127c478bd9Sstevel@tonic-gate 		logit("Door Slam... invalid door param\n");
6137c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, gettext("ldap_cachemgr: Door Slam... "
6147c478bd9Sstevel@tonic-gate 			"invalid door param"));
6157c478bd9Sstevel@tonic-gate 		(void) printf(gettext("Door Slam... invalid door param\n"));
6167c478bd9Sstevel@tonic-gate 		exit(0);
6177c478bd9Sstevel@tonic-gate 	}
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	if (ptr == NULL) { /* empty door call */
6207c478bd9Sstevel@tonic-gate 		(void) door_return(NULL, 0, 0, 0); /* return the favor */
6217c478bd9Sstevel@tonic-gate 	}
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	switch (ptr->ldap_callnumber) {
6247c478bd9Sstevel@tonic-gate 	case NULLCALL:
6257c478bd9Sstevel@tonic-gate 		u.data.ldap_ret.ldap_return_code = SUCCESS;
6267c478bd9Sstevel@tonic-gate 		u.data.ldap_ret.ldap_bufferbytesused = sizeof (ldap_return_t);
6277c478bd9Sstevel@tonic-gate 		break;
6287c478bd9Sstevel@tonic-gate 	case GETLDAPCONFIG:
6297c478bd9Sstevel@tonic-gate 		getldap_lookup(&u.data.ldap_ret, ptr);
6307c478bd9Sstevel@tonic-gate 		current_admin.ldap_stat.ldap_numbercalls++;
6317c478bd9Sstevel@tonic-gate 		break;
6327c478bd9Sstevel@tonic-gate 	case GETADMIN:
6337c478bd9Sstevel@tonic-gate 		(void) getadmin(&u.data.ldap_ret);
6347c478bd9Sstevel@tonic-gate 		break;
6357c478bd9Sstevel@tonic-gate 	case SETADMIN:
6367c478bd9Sstevel@tonic-gate 	case KILLSERVER:
6377c478bd9Sstevel@tonic-gate 		if (door_cred(&dc) < 0) {
6387c478bd9Sstevel@tonic-gate 			logit("door_cred() call failed\n");
6397c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, gettext("ldap_cachemgr: door_cred() "
6407c478bd9Sstevel@tonic-gate 				"call failed"));
6417c478bd9Sstevel@tonic-gate 			perror("door_cred");
6427c478bd9Sstevel@tonic-gate 			break;
6437c478bd9Sstevel@tonic-gate 		}
6447c478bd9Sstevel@tonic-gate 		if (dc.dc_euid != 0 && ptr->ldap_callnumber == SETADMIN) {
6457c478bd9Sstevel@tonic-gate 			logit("SETADMIN call failed (cred): caller "
6467c478bd9Sstevel@tonic-gate 			    "pid %ld, uid %ld, euid %ld\n",
6477c478bd9Sstevel@tonic-gate 			    dc.dc_pid, dc.dc_ruid, dc.dc_euid);
6487c478bd9Sstevel@tonic-gate 			u.data.ldap_ret.ldap_return_code = NOTFOUND;
6497c478bd9Sstevel@tonic-gate 			break;
6507c478bd9Sstevel@tonic-gate 		}
6517c478bd9Sstevel@tonic-gate 		if (ptr->ldap_callnumber == KILLSERVER) {
6527c478bd9Sstevel@tonic-gate 			logit("ldap_cachemgr received KILLSERVER cmd from "
6537c478bd9Sstevel@tonic-gate 			    "pid %ld, uid %ld, euid %ld\n",
6547c478bd9Sstevel@tonic-gate 			    dc.dc_pid, dc.dc_ruid, dc.dc_euid);
6557c478bd9Sstevel@tonic-gate 			exit(0);
6567c478bd9Sstevel@tonic-gate 		} else {
6577c478bd9Sstevel@tonic-gate 			(void) setadmin(&u.data.ldap_ret, ptr);
6587c478bd9Sstevel@tonic-gate 		}
6597c478bd9Sstevel@tonic-gate 		break;
6607c478bd9Sstevel@tonic-gate 	case GETLDAPSERVER:
6617c478bd9Sstevel@tonic-gate 		getldap_getserver(&u.data.ldap_ret, ptr);
6627c478bd9Sstevel@tonic-gate 		current_admin.ldap_stat.ldap_numbercalls++;
6637c478bd9Sstevel@tonic-gate 		break;
6647c478bd9Sstevel@tonic-gate 	case GETCACHE:
6657c478bd9Sstevel@tonic-gate 		getldap_get_cacheData(&u.data.ldap_ret, ptr);
6667c478bd9Sstevel@tonic-gate 		current_admin.ldap_stat.ldap_numbercalls++;
6677c478bd9Sstevel@tonic-gate 		break;
6687c478bd9Sstevel@tonic-gate 	case SETCACHE:
6697c478bd9Sstevel@tonic-gate 		getldap_set_cacheData(&u.data.ldap_ret, ptr);
6707c478bd9Sstevel@tonic-gate 		current_admin.ldap_stat.ldap_numbercalls++;
6717c478bd9Sstevel@tonic-gate 		break;
6727c478bd9Sstevel@tonic-gate 	case GETCACHESTAT:
6737c478bd9Sstevel@tonic-gate 		getldap_get_cacheStat(&u.data.ldap_ret);
6747c478bd9Sstevel@tonic-gate 		current_admin.ldap_stat.ldap_numbercalls++;
6757c478bd9Sstevel@tonic-gate 		break;
6767c478bd9Sstevel@tonic-gate 	default:
6777c478bd9Sstevel@tonic-gate 		logit("Unknown ldap service door call op %d\n",
6787c478bd9Sstevel@tonic-gate 		    ptr->ldap_callnumber);
6797c478bd9Sstevel@tonic-gate 		u.data.ldap_ret.ldap_return_code = -99;
6807c478bd9Sstevel@tonic-gate 		u.data.ldap_ret.ldap_bufferbytesused = sizeof (ldap_return_t);
6817c478bd9Sstevel@tonic-gate 		break;
6827c478bd9Sstevel@tonic-gate 	}
683*cb5caa98Sdjl 	(void) door_return((char *)&u.data,
6847c478bd9Sstevel@tonic-gate 		u.data.ldap_ret.ldap_bufferbytesused, NULL, 0);
6857c478bd9Sstevel@tonic-gate }
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate static void
6887c478bd9Sstevel@tonic-gate usage(char *s)
6897c478bd9Sstevel@tonic-gate {
6907c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
6917c478bd9Sstevel@tonic-gate 		gettext("Usage: %s [-d debug_level] [-l logfilename]\n"), s);
6927c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("	[-K] "
6937c478bd9Sstevel@tonic-gate 					"[-r revalidate_interval] "));
6947c478bd9Sstevel@tonic-gate #ifndef SLP
6957c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("	[-g]\n"));
6967c478bd9Sstevel@tonic-gate #else
6977c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("	[-g] [-s]\n"));
6987c478bd9Sstevel@tonic-gate #endif /* SLP */
6997c478bd9Sstevel@tonic-gate 	exit(1);
7007c478bd9Sstevel@tonic-gate }
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate static int	logfd = -1;
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate static int
7067c478bd9Sstevel@tonic-gate cachemgr_set_lf(admin_t *ptr, char *logfile)
7077c478bd9Sstevel@tonic-gate {
7087c478bd9Sstevel@tonic-gate 	int	newlogfd;
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	/*
7117c478bd9Sstevel@tonic-gate 	 *  we don't really want to try and open the log file
7127c478bd9Sstevel@tonic-gate 	 *  /dev/null since that will fail w/ our security fixes
7137c478bd9Sstevel@tonic-gate 	 */
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	if (logfile == NULL || *logfile == 0) {
7167c478bd9Sstevel@tonic-gate 		/*EMPTY*/;
7177c478bd9Sstevel@tonic-gate 	} else if (strcmp(logfile, "/dev/null") == 0) {
7187c478bd9Sstevel@tonic-gate 		(void) strcpy(current_admin.logfile, "/dev/null");
7197c478bd9Sstevel@tonic-gate 		(void) close(logfd);
7207c478bd9Sstevel@tonic-gate 		logfd = -1;
7217c478bd9Sstevel@tonic-gate 	} else {
7227c478bd9Sstevel@tonic-gate 		if ((newlogfd =
7237c478bd9Sstevel@tonic-gate 			open(logfile, O_EXCL|O_WRONLY|O_CREAT, 0644)) < 0) {
7247c478bd9Sstevel@tonic-gate 			/*
7257c478bd9Sstevel@tonic-gate 			 * File already exists... now we need to get cute
7267c478bd9Sstevel@tonic-gate 			 * since opening a file in a world-writeable directory
7277c478bd9Sstevel@tonic-gate 			 * safely is hard = it could be a hard link or a
7287c478bd9Sstevel@tonic-gate 			 * symbolic link to a system file.
7297c478bd9Sstevel@tonic-gate 			 *
7307c478bd9Sstevel@tonic-gate 			 */
7317c478bd9Sstevel@tonic-gate 			struct stat	before;
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 			if (lstat(logfile, &before) < 0) {
7347c478bd9Sstevel@tonic-gate 				logit("Cannot open new logfile \"%s\": %sn",
7357c478bd9Sstevel@tonic-gate 					logfile, strerror(errno));
7367c478bd9Sstevel@tonic-gate 				return (-1);
7377c478bd9Sstevel@tonic-gate 			}
7387c478bd9Sstevel@tonic-gate 			if (S_ISREG(before.st_mode) &&	/* no symbolic links */
7397c478bd9Sstevel@tonic-gate 			    (before.st_nlink == 1) &&	/* no hard links */
7407c478bd9Sstevel@tonic-gate 			    (before.st_uid == 0)) {	/* owned by root */
7417c478bd9Sstevel@tonic-gate 				if ((newlogfd =
7427c478bd9Sstevel@tonic-gate 				    open(logfile,
7437c478bd9Sstevel@tonic-gate 				    O_APPEND|O_WRONLY, 0644)) < 0) {
7447c478bd9Sstevel@tonic-gate 					logit("Cannot open new logfile "
7457c478bd9Sstevel@tonic-gate 						"\"%s\": %s\n",
7467c478bd9Sstevel@tonic-gate 						logfile, strerror(errno));
7477c478bd9Sstevel@tonic-gate 					return (-1);
7487c478bd9Sstevel@tonic-gate 				}
7497c478bd9Sstevel@tonic-gate 			} else {
7507c478bd9Sstevel@tonic-gate 				logit("Cannot use specified logfile "
7517c478bd9Sstevel@tonic-gate 				    "\"%s\": file is/has links or isn't "
7527c478bd9Sstevel@tonic-gate 				    "owned by root\n", logfile);
7537c478bd9Sstevel@tonic-gate 				return (-1);
7547c478bd9Sstevel@tonic-gate 			}
7557c478bd9Sstevel@tonic-gate 		}
7567c478bd9Sstevel@tonic-gate 		(void) strlcpy(ptr->logfile, logfile, sizeof (ptr->logfile));
7577c478bd9Sstevel@tonic-gate 		(void) close(logfd);
7587c478bd9Sstevel@tonic-gate 		logfd = newlogfd;
7597c478bd9Sstevel@tonic-gate 		logit("Starting ldap_cachemgr, logfile %s\n", logfile);
7607c478bd9Sstevel@tonic-gate 	}
7617c478bd9Sstevel@tonic-gate 	return (0);
7627c478bd9Sstevel@tonic-gate }
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
7657c478bd9Sstevel@tonic-gate void
7667c478bd9Sstevel@tonic-gate logit(char *format, ...)
7677c478bd9Sstevel@tonic-gate {
7687c478bd9Sstevel@tonic-gate 	static mutex_t	loglock;
7697c478bd9Sstevel@tonic-gate 	struct timeval	tv;
7707c478bd9Sstevel@tonic-gate 	char		buffer[BUFSIZ];
7717c478bd9Sstevel@tonic-gate 	va_list		ap;
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	va_start(ap, format);
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	if (logfd >= 0) {
7767c478bd9Sstevel@tonic-gate 		int	safechars;
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 		(void) gettimeofday(&tv, NULL);
7797c478bd9Sstevel@tonic-gate 		(void) ctime_r(&tv.tv_sec, buffer, BUFSIZ);
7807c478bd9Sstevel@tonic-gate 		(void) snprintf(buffer+19, BUFSIZE, ".%.4ld	",
7817c478bd9Sstevel@tonic-gate 			tv.tv_usec/100);
7827c478bd9Sstevel@tonic-gate 		safechars = sizeof (buffer) - 30;
7837c478bd9Sstevel@tonic-gate 		if (vsnprintf(buffer+25, safechars, format, ap) > safechars)
7847c478bd9Sstevel@tonic-gate 			(void) strcat(buffer, "...\n");
7857c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&loglock);
7867c478bd9Sstevel@tonic-gate 		(void) write(logfd, buffer, strlen(buffer));
7877c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&loglock);
7887c478bd9Sstevel@tonic-gate 	}
7897c478bd9Sstevel@tonic-gate 	va_end(ap);
7907c478bd9Sstevel@tonic-gate }
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate void
7947c478bd9Sstevel@tonic-gate do_update(ldap_call_t *in)
7957c478bd9Sstevel@tonic-gate {
7967c478bd9Sstevel@tonic-gate 	dataunion		u;
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	switch (in->ldap_callnumber) {
7997c478bd9Sstevel@tonic-gate 	case GETLDAPCONFIG:
8007c478bd9Sstevel@tonic-gate 		getldap_lookup(&u.data.ldap_ret, in);
8017c478bd9Sstevel@tonic-gate 		break;
8027c478bd9Sstevel@tonic-gate 	default:
8037c478bd9Sstevel@tonic-gate 		assert(0);
8047c478bd9Sstevel@tonic-gate 		break;
8057c478bd9Sstevel@tonic-gate 	}
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	free(in);
8087c478bd9Sstevel@tonic-gate }
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate static int
8127c478bd9Sstevel@tonic-gate client_getadmin(admin_t *ptr)
8137c478bd9Sstevel@tonic-gate {
8147c478bd9Sstevel@tonic-gate 	dataunion		u;
8157c478bd9Sstevel@tonic-gate 	ldap_data_t	*dptr;
8167c478bd9Sstevel@tonic-gate 	int		ndata;
8177c478bd9Sstevel@tonic-gate 	int		adata;
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	u.data.ldap_call.ldap_callnumber = GETADMIN;
8207c478bd9Sstevel@tonic-gate 	ndata = sizeof (u);
8217c478bd9Sstevel@tonic-gate 	adata = sizeof (u.data);
8227c478bd9Sstevel@tonic-gate 	dptr = &u.data;
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	if (__ns_ldap_trydoorcall(&dptr, &ndata, &adata) != SUCCESS) {
8257c478bd9Sstevel@tonic-gate 		return (-1);
8267c478bd9Sstevel@tonic-gate 	}
8277c478bd9Sstevel@tonic-gate 	(void) memcpy(ptr, dptr->ldap_ret.ldap_u.buff, sizeof (*ptr));
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	return (0);
8307c478bd9Sstevel@tonic-gate }
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate static int
8337c478bd9Sstevel@tonic-gate getadmin(ldap_return_t *out)
8347c478bd9Sstevel@tonic-gate {
8357c478bd9Sstevel@tonic-gate 	out->ldap_return_code = SUCCESS;
8367c478bd9Sstevel@tonic-gate 	out->ldap_bufferbytesused = sizeof (current_admin);
8377c478bd9Sstevel@tonic-gate 	(void) memcpy(out->ldap_u.buff, &current_admin, sizeof (current_admin));
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 	return (0);
8407c478bd9Sstevel@tonic-gate }
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate static int
8447c478bd9Sstevel@tonic-gate setadmin(ldap_return_t *out, ldap_call_t *ptr)
8457c478bd9Sstevel@tonic-gate {
8467c478bd9Sstevel@tonic-gate 	admin_t	*new;
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 	out->ldap_return_code = SUCCESS;
8497c478bd9Sstevel@tonic-gate 	out->ldap_bufferbytesused = sizeof (ldap_return_t);
8507c478bd9Sstevel@tonic-gate 	new = (admin_t *)ptr->ldap_u.domainname;
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 	/*
8537c478bd9Sstevel@tonic-gate 	 *  global admin stuff
8547c478bd9Sstevel@tonic-gate 	 */
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	if ((cachemgr_set_lf(&current_admin, new->logfile) < 0) ||
8577c478bd9Sstevel@tonic-gate 	    cachemgr_set_dl(&current_admin, new->debug_level) < 0) {
8587c478bd9Sstevel@tonic-gate 		out->ldap_return_code = NOTFOUND;
8597c478bd9Sstevel@tonic-gate 		return (-1);
8607c478bd9Sstevel@tonic-gate 	}
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	if (cachemgr_set_ttl(&current_admin.ldap_stat,
8637c478bd9Sstevel@tonic-gate 			"ldap",
8647c478bd9Sstevel@tonic-gate 			new->ldap_stat.ldap_ttl) < 0) {
8657c478bd9Sstevel@tonic-gate 		out->ldap_return_code = NOTFOUND;
8667c478bd9Sstevel@tonic-gate 		return (-1);
8677c478bd9Sstevel@tonic-gate 	}
8687c478bd9Sstevel@tonic-gate 	out->ldap_return_code = SUCCESS;
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	return (0);
8717c478bd9Sstevel@tonic-gate }
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate static void
8757c478bd9Sstevel@tonic-gate client_killserver()
8767c478bd9Sstevel@tonic-gate {
8777c478bd9Sstevel@tonic-gate 	dataunion		u;
8787c478bd9Sstevel@tonic-gate 	ldap_data_t		*dptr;
8797c478bd9Sstevel@tonic-gate 	int			ndata;
8807c478bd9Sstevel@tonic-gate 	int			adata;
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	u.data.ldap_call.ldap_callnumber = KILLSERVER;
8837c478bd9Sstevel@tonic-gate 	ndata = sizeof (u);
8847c478bd9Sstevel@tonic-gate 	adata = sizeof (ldap_call_t);
8857c478bd9Sstevel@tonic-gate 	dptr = &u.data;
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	__ns_ldap_trydoorcall(&dptr, &ndata, &adata);
8887c478bd9Sstevel@tonic-gate }
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate static int
8927c478bd9Sstevel@tonic-gate client_setadmin(admin_t *ptr)
8937c478bd9Sstevel@tonic-gate {
8947c478bd9Sstevel@tonic-gate 	dataunion		u;
8957c478bd9Sstevel@tonic-gate 	ldap_data_t		*dptr;
8967c478bd9Sstevel@tonic-gate 	int			ndata;
8977c478bd9Sstevel@tonic-gate 	int			adata;
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	u.data.ldap_call.ldap_callnumber = SETADMIN;
9007c478bd9Sstevel@tonic-gate 	(void) memcpy(u.data.ldap_call.ldap_u.domainname, ptr, sizeof (*ptr));
9017c478bd9Sstevel@tonic-gate 	ndata = sizeof (u);
9027c478bd9Sstevel@tonic-gate 	adata = sizeof (*ptr);
9037c478bd9Sstevel@tonic-gate 	dptr = &u.data;
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 	if (__ns_ldap_trydoorcall(&dptr, &ndata, &adata) != SUCCESS) {
9067c478bd9Sstevel@tonic-gate 		return (-1);
9077c478bd9Sstevel@tonic-gate 	}
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	return (0);
9107c478bd9Sstevel@tonic-gate }
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate static int
9137c478bd9Sstevel@tonic-gate client_showstats(admin_t *ptr)
9147c478bd9Sstevel@tonic-gate {
9157c478bd9Sstevel@tonic-gate 	dataunion	u;
9167c478bd9Sstevel@tonic-gate 	ldap_data_t	*dptr;
9177c478bd9Sstevel@tonic-gate 	int		ndata;
9187c478bd9Sstevel@tonic-gate 	int		adata;
9197c478bd9Sstevel@tonic-gate 	char		*rbuf, *sptr, *rest;
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	/*
9227c478bd9Sstevel@tonic-gate 	 * print admin data
9237c478bd9Sstevel@tonic-gate 	 */
9247c478bd9Sstevel@tonic-gate 	(void) printf(gettext("\ncachemgr configuration:\n"));
9257c478bd9Sstevel@tonic-gate 	(void) printf(gettext("server debug level %10d\n"), ptr->debug_level);
9267c478bd9Sstevel@tonic-gate 	(void) printf(gettext("server log file\t\"%s\"\n"), ptr->logfile);
9277c478bd9Sstevel@tonic-gate 	(void) printf(gettext("number of calls to ldapcachemgr %10d\n"),
9287c478bd9Sstevel@tonic-gate 		ptr->ldap_stat.ldap_numbercalls);
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	/*
9317c478bd9Sstevel@tonic-gate 	 * get cache data statistics
9327c478bd9Sstevel@tonic-gate 	 */
9337c478bd9Sstevel@tonic-gate 	u.data.ldap_call.ldap_callnumber = GETCACHESTAT;
9347c478bd9Sstevel@tonic-gate 	ndata = sizeof (u);
9357c478bd9Sstevel@tonic-gate 	adata = sizeof (ldap_call_t);
9367c478bd9Sstevel@tonic-gate 	dptr = &u.data;
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 	if (__ns_ldap_trydoorcall(&dptr, &ndata, &adata) != SUCCESS) {
9397c478bd9Sstevel@tonic-gate 		(void) printf(
9407c478bd9Sstevel@tonic-gate 			gettext("\nCache data statistics not available!\n"));
9417c478bd9Sstevel@tonic-gate 		return (0);
9427c478bd9Sstevel@tonic-gate 	}
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 	/*
9457c478bd9Sstevel@tonic-gate 	 * print cache data statistics line by line
9467c478bd9Sstevel@tonic-gate 	 */
9477c478bd9Sstevel@tonic-gate 	(void) printf(gettext("\ncachemgr cache data statistics:\n"));
9487c478bd9Sstevel@tonic-gate 	rbuf = dptr->ldap_ret.ldap_u.buff;
9497c478bd9Sstevel@tonic-gate 	sptr = strtok_r(rbuf, DOORLINESEP, &rest);
9507c478bd9Sstevel@tonic-gate 	for (;;) {
9517c478bd9Sstevel@tonic-gate 		(void) printf("%s\n", sptr);
9527c478bd9Sstevel@tonic-gate 		sptr = strtok_r(NULL, DOORLINESEP, &rest);
9537c478bd9Sstevel@tonic-gate 		if (sptr == NULL)
9547c478bd9Sstevel@tonic-gate 			break;
9557c478bd9Sstevel@tonic-gate 	}
9567c478bd9Sstevel@tonic-gate 	return (0);
9577c478bd9Sstevel@tonic-gate }
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate /*
9617c478bd9Sstevel@tonic-gate  * detach from tty
9627c478bd9Sstevel@tonic-gate  */
9637c478bd9Sstevel@tonic-gate static void
9647c478bd9Sstevel@tonic-gate detachfromtty(char *pgm)
9657c478bd9Sstevel@tonic-gate {
9667c478bd9Sstevel@tonic-gate 	int 	status;
9677c478bd9Sstevel@tonic-gate 	pid_t	pid, wret;
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	(void) close(0);
9707c478bd9Sstevel@tonic-gate 	(void) close(1);
9717c478bd9Sstevel@tonic-gate 	/*
9727c478bd9Sstevel@tonic-gate 	 * Block the SIGUSR1 signal
9737c478bd9Sstevel@tonic-gate 	 * just in case that the child
9747c478bd9Sstevel@tonic-gate 	 * process may run faster than
9757c478bd9Sstevel@tonic-gate 	 * the parent process and
9767c478bd9Sstevel@tonic-gate 	 * send this signal before
9777c478bd9Sstevel@tonic-gate 	 * the signal handler is ready
9787c478bd9Sstevel@tonic-gate 	 * in the parent process.
9797c478bd9Sstevel@tonic-gate 	 * This error will cause the parent
9807c478bd9Sstevel@tonic-gate 	 * to exit with the User Signal 1
9817c478bd9Sstevel@tonic-gate 	 * exit code (144).
9827c478bd9Sstevel@tonic-gate 	 */
9837c478bd9Sstevel@tonic-gate 	(void) sighold(SIGUSR1);
9847c478bd9Sstevel@tonic-gate 	pid = fork1();
9857c478bd9Sstevel@tonic-gate 	switch (pid) {
9867c478bd9Sstevel@tonic-gate 		case (pid_t)-1:
9877c478bd9Sstevel@tonic-gate 			logit("detachfromtty(): fork1() call failed\n");
9887c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
9897c478bd9Sstevel@tonic-gate 					gettext("%s: fork1() call failed.\n"),
9907c478bd9Sstevel@tonic-gate 					pgm);
9917c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
9927c478bd9Sstevel@tonic-gate 				gettext("ldap_cachemgr: fork1() call failed."));
9937c478bd9Sstevel@tonic-gate 			exit(1);
9947c478bd9Sstevel@tonic-gate 			break;
9957c478bd9Sstevel@tonic-gate 		case 0:
9967c478bd9Sstevel@tonic-gate 			/*
9977c478bd9Sstevel@tonic-gate 			 * child process does not
9987c478bd9Sstevel@tonic-gate 			 * need to worry about
9997c478bd9Sstevel@tonic-gate 			 * the SIGUSR1 signal
10007c478bd9Sstevel@tonic-gate 			 */
10017c478bd9Sstevel@tonic-gate 			(void) sigrelse(SIGUSR1);
10027c478bd9Sstevel@tonic-gate 			(void) close(2);
10037c478bd9Sstevel@tonic-gate 			break;
10047c478bd9Sstevel@tonic-gate 		default:
10057c478bd9Sstevel@tonic-gate 			/*
10067c478bd9Sstevel@tonic-gate 			 * Wait forever until the child process
10077c478bd9Sstevel@tonic-gate 			 * has exited, or has signalled that at
10087c478bd9Sstevel@tonic-gate 			 * least one server in the server list
10097c478bd9Sstevel@tonic-gate 			 * is up.
10107c478bd9Sstevel@tonic-gate 			 */
10117c478bd9Sstevel@tonic-gate 			if (signal(SIGUSR1, sig_ok_to_exit) == SIG_ERR) {
10127c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): "
10137c478bd9Sstevel@tonic-gate 					"can't set up signal handler to "
10147c478bd9Sstevel@tonic-gate 					" catch SIGUSR1.\n");
10157c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
10167c478bd9Sstevel@tonic-gate 					gettext("%s: signal() call failed.\n"),
10177c478bd9Sstevel@tonic-gate 					pgm);
10187c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, gettext("ldap_cachemgr: "
10197c478bd9Sstevel@tonic-gate 					"can't set up signal handler to "
10207c478bd9Sstevel@tonic-gate 					" catch SIGUSR1."));
10217c478bd9Sstevel@tonic-gate 				exit(1);
10227c478bd9Sstevel@tonic-gate 			}
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 			/*
10257c478bd9Sstevel@tonic-gate 			 * now unblock the SIGUSR1 signal
10267c478bd9Sstevel@tonic-gate 			 * to handle the pending or
10277c478bd9Sstevel@tonic-gate 			 * soon to arrive SIGUSR1 signal
10287c478bd9Sstevel@tonic-gate 			 */
10297c478bd9Sstevel@tonic-gate 			(void) sigrelse(SIGUSR1);
10307c478bd9Sstevel@tonic-gate 			wret = waitpid(pid, &status, 0);
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 			if (wret == -1) {
10337c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): "
10347c478bd9Sstevel@tonic-gate 					"waitpid() call failed\n");
10357c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
10367c478bd9Sstevel@tonic-gate 					gettext("%s: waitpid() call failed.\n"),
10377c478bd9Sstevel@tonic-gate 					pgm);
10387c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
10397c478bd9Sstevel@tonic-gate 					gettext("ldap_cachemgr: waitpid() "
10407c478bd9Sstevel@tonic-gate 						"call failed."));
10417c478bd9Sstevel@tonic-gate 				exit(1);
10427c478bd9Sstevel@tonic-gate 			}
10437c478bd9Sstevel@tonic-gate 			if (wret != pid) {
10447c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): "
10457c478bd9Sstevel@tonic-gate 					"waitpid() returned %ld when "
10467c478bd9Sstevel@tonic-gate 					"child pid was %ld\n",
10477c478bd9Sstevel@tonic-gate 					wret, pid);
10487c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
10497c478bd9Sstevel@tonic-gate 					gettext(
10507c478bd9Sstevel@tonic-gate 					"%s: waitpid() returned %ld when "
10517c478bd9Sstevel@tonic-gate 					"child pid was %ld.\n"),
10527c478bd9Sstevel@tonic-gate 					pgm, wret, pid);
10537c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
10547c478bd9Sstevel@tonic-gate 					gettext("ldap_cachemgr: waitpid() "
10557c478bd9Sstevel@tonic-gate 						"returned different "
10567c478bd9Sstevel@tonic-gate 						"child pid."));
10577c478bd9Sstevel@tonic-gate 				exit(1);
10587c478bd9Sstevel@tonic-gate 			}
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 			/* evaluate return status */
10617c478bd9Sstevel@tonic-gate 			if (WIFEXITED(status)) {
10627c478bd9Sstevel@tonic-gate 				if (WEXITSTATUS(status) == 0) {
10637c478bd9Sstevel@tonic-gate 					exit(0);
10647c478bd9Sstevel@tonic-gate 				}
10657c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): "
10667c478bd9Sstevel@tonic-gate 					"child failed (rc = %d).\n",
10677c478bd9Sstevel@tonic-gate 					WEXITSTATUS(status));
10687c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
10697c478bd9Sstevel@tonic-gate 					gettext("%s: failed. Please see "
10707c478bd9Sstevel@tonic-gate 					"syslog for details.\n"),
10717c478bd9Sstevel@tonic-gate 					pgm);
10727c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
10737c478bd9Sstevel@tonic-gate 					gettext("ldap_cachemgr: failed "
10747c478bd9Sstevel@tonic-gate 					"(rc = %d)."),
10757c478bd9Sstevel@tonic-gate 					WEXITSTATUS(status));
10767c478bd9Sstevel@tonic-gate 			} else if (WIFSIGNALED(status)) {
10777c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): "
10787c478bd9Sstevel@tonic-gate 					"child terminated by signal %d.\n",
10797c478bd9Sstevel@tonic-gate 					WTERMSIG(status));
10807c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
10817c478bd9Sstevel@tonic-gate 				gettext("%s: terminated by signal %d.\n"),
10827c478bd9Sstevel@tonic-gate 					pgm, WTERMSIG(status));
10837c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
10847c478bd9Sstevel@tonic-gate 					gettext("ldap_cachemgr: terminated by "
10857c478bd9Sstevel@tonic-gate 					"signal %d.\n"),
10867c478bd9Sstevel@tonic-gate 					WTERMSIG(status));
10877c478bd9Sstevel@tonic-gate 			} else if (WCOREDUMP(status)) {
10887c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): child core dumped.\n"),
10897c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
10907c478bd9Sstevel@tonic-gate 					gettext("%s: core dumped.\n"),
10917c478bd9Sstevel@tonic-gate 					pgm);
10927c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
10937c478bd9Sstevel@tonic-gate 					gettext("ldap_cachemgr: "
10947c478bd9Sstevel@tonic-gate 						"core dumped.\n"));
10957c478bd9Sstevel@tonic-gate 			}
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 			exit(1);
10987c478bd9Sstevel@tonic-gate 	}
10997c478bd9Sstevel@tonic-gate 	(void) setsid();
11007c478bd9Sstevel@tonic-gate 	if (open("/dev/null", O_RDWR, 0) != -1) {
11017c478bd9Sstevel@tonic-gate 		(void) dup(0);
11027c478bd9Sstevel@tonic-gate 		(void) dup(0);
11037c478bd9Sstevel@tonic-gate 	}
11047c478bd9Sstevel@tonic-gate }
1105