xref: /onnv-gate/usr/src/cmd/smbsrv/smbd/smbd_main.c (revision 13082:81ec56bf6147)
15331Samw /*
25331Samw  * CDDL HEADER START
35331Samw  *
45331Samw  * The contents of this file are subject to the terms of the
55331Samw  * Common Development and Distribution License (the "License").
65331Samw  * You may not use this file except in compliance with the License.
75331Samw  *
85331Samw  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95331Samw  * or http://www.opensolaris.org/os/licensing.
105331Samw  * See the License for the specific language governing permissions
115331Samw  * and limitations under the License.
125331Samw  *
135331Samw  * When distributing Covered Code, include this CDDL HEADER in each
145331Samw  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155331Samw  * If applicable, add the following below this CDDL HEADER, with the
165331Samw  * fields enclosed by brackets "[]" replaced with your own identifying
175331Samw  * information: Portions Copyright [yyyy] [name of copyright owner]
185331Samw  *
195331Samw  * CDDL HEADER END
205331Samw  */
215331Samw /*
2212508Samw@Sun.COM  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
235331Samw  */
245331Samw 
255331Samw #include <sys/types.h>
265331Samw #include <sys/stat.h>
275331Samw #include <sys/ioccom.h>
289832Samw@Sun.COM #include <sys/corectl.h>
295331Samw #include <stdio.h>
305331Samw #include <string.h>
315331Samw #include <strings.h>
325331Samw #include <stdlib.h>
335331Samw #include <unistd.h>
345331Samw #include <stdarg.h>
355331Samw #include <fcntl.h>
365331Samw #include <wait.h>
375331Samw #include <signal.h>
388334SJose.Borrego@Sun.COM #include <atomic.h>
395331Samw #include <libscf.h>
405331Samw #include <limits.h>
415331Samw #include <priv_utils.h>
425331Samw #include <door.h>
435331Samw #include <errno.h>
445331Samw #include <pthread.h>
455331Samw #include <time.h>
465331Samw #include <libscf.h>
475331Samw #include <zone.h>
485331Samw #include <libgen.h>
495331Samw #include <pwd.h>
505331Samw #include <grp.h>
5112890SJoyce.McIntosh@Sun.COM #include <cups/cups.h>
525331Samw 
5311963SAfshin.Ardakani@Sun.COM #include <smbsrv/smb_door.h>
545331Samw #include <smbsrv/smb_ioctl.h>
5510966SJordan.Brown@Sun.COM #include <smbsrv/string.h>
565331Samw #include <smbsrv/libsmb.h>
575331Samw #include <smbsrv/libsmbns.h>
585331Samw #include <smbsrv/libmlsvc.h>
595331Samw #include "smbd.h"
605331Samw 
6112914SJoyce.McIntosh@Sun.COM #define	SMBD_ONLINE_WAIT_INTERVAL	10
62*13082SJoyce.McIntosh@Sun.COM #define	SMBD_REFRESH_INTERVAL		10
635331Samw #define	DRV_DEVICE_PATH	"/devices/pseudo/smbsrv@0:smbsrv"
645331Samw #define	SMB_DBDIR "/var/smb"
655331Samw 
669832Samw@Sun.COM static void *smbd_nbt_listener(void *);
679832Samw@Sun.COM static void *smbd_tcp_listener(void *);
689832Samw@Sun.COM static void *smbd_nbt_receiver(void *);
699832Samw@Sun.COM static void *smbd_tcp_receiver(void *);
706139Sjb150015 
715331Samw static int smbd_daemonize_init(void);
725331Samw static void smbd_daemonize_fini(int, int);
739832Samw@Sun.COM static int smb_init_daemon_priv(int, uid_t, gid_t);
745331Samw 
756432Sas200622 static int smbd_kernel_bind(void);
765331Samw static void smbd_kernel_unbind(void);
775331Samw static int smbd_already_running(void);
785331Samw 
795331Samw static int smbd_service_init(void);
805331Samw static void smbd_service_fini(void);
815331Samw 
825331Samw static int smbd_setup_options(int argc, char *argv[]);
835331Samw static void smbd_usage(FILE *fp);
845331Samw static void smbd_report(const char *fmt, ...);
855331Samw 
865331Samw static void smbd_sig_handler(int sig);
875331Samw 
887348SJose.Borrego@Sun.COM static int32_t smbd_gmtoff(void);
89*13082SJoyce.McIntosh@Sun.COM static void smbd_localtime_init(void);
905331Samw static void *smbd_localtime_monitor(void *arg);
915331Samw 
92*13082SJoyce.McIntosh@Sun.COM static void smbd_dyndns_init(void);
93*13082SJoyce.McIntosh@Sun.COM static void smbd_load_shares(void);
9412890SJoyce.McIntosh@Sun.COM 
955331Samw static int smbd_refresh_init(void);
965331Samw static void smbd_refresh_fini(void);
975331Samw static void *smbd_refresh_monitor(void *);
988670SJose.Borrego@Sun.COM 
999046SJose.Borrego@Sun.COM static int smbd_start_listeners(void);
1009046SJose.Borrego@Sun.COM static void smbd_stop_listeners(void);
1019832Samw@Sun.COM static int smbd_kernel_start(void);
1029832Samw@Sun.COM 
1039832Samw@Sun.COM static void smbd_fatal_error(const char *);
1049046SJose.Borrego@Sun.COM 
1055331Samw static pthread_cond_t refresh_cond;
1065331Samw static pthread_mutex_t refresh_mutex;
1075331Samw 
10811963SAfshin.Ardakani@Sun.COM static cond_t listener_cv;
10911963SAfshin.Ardakani@Sun.COM static mutex_t listener_mutex;
11011963SAfshin.Ardakani@Sun.COM 
11111963SAfshin.Ardakani@Sun.COM /*
11211963SAfshin.Ardakani@Sun.COM  * Mutex to ensure that smbd_service_fini() and smbd_service_init()
11311963SAfshin.Ardakani@Sun.COM  * are atomic w.r.t. one another.  Otherwise, if a shutdown begins
11411963SAfshin.Ardakani@Sun.COM  * before initialization is complete, resources can get deallocated
11511963SAfshin.Ardakani@Sun.COM  * while initialization threads are still using them.
11611963SAfshin.Ardakani@Sun.COM  */
11711963SAfshin.Ardakani@Sun.COM static mutex_t smbd_service_mutex;
11811963SAfshin.Ardakani@Sun.COM static cond_t smbd_service_cv;
11911963SAfshin.Ardakani@Sun.COM 
1206139Sjb150015 smbd_t smbd;
1215331Samw 
1225331Samw /*
1235331Samw  * Use SMF error codes only on return or exit.
1245331Samw  */
1255331Samw int
1265331Samw main(int argc, char *argv[])
1275331Samw {
1286432Sas200622 	struct sigaction	act;
1296432Sas200622 	sigset_t		set;
1306432Sas200622 	uid_t			uid;
1316432Sas200622 	int			pfd = -1;
1328334SJose.Borrego@Sun.COM 	uint_t			sigval;
13312890SJoyce.McIntosh@Sun.COM 	struct rlimit		rl;
13412890SJoyce.McIntosh@Sun.COM 	int			orig_limit;
1355331Samw 
1365331Samw 	smbd.s_pname = basename(argv[0]);
1375331Samw 	openlog(smbd.s_pname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
1385331Samw 
1395331Samw 	if (smbd_setup_options(argc, argv) != 0)
1405331Samw 		return (SMF_EXIT_ERR_FATAL);
1415331Samw 
1425331Samw 	if ((uid = getuid()) != smbd.s_uid) {
1435331Samw 		smbd_report("user %d: %s", uid, strerror(EPERM));
1445331Samw 		return (SMF_EXIT_ERR_FATAL);
1455331Samw 	}
1465331Samw 
1475331Samw 	if (getzoneid() != GLOBAL_ZONEID) {
1485331Samw 		smbd_report("non-global zones are not supported");
1495331Samw 		return (SMF_EXIT_ERR_FATAL);
1505331Samw 	}
1515331Samw 
1525331Samw 	if (is_system_labeled()) {
1535331Samw 		smbd_report("Trusted Extensions not supported");
1545331Samw 		return (SMF_EXIT_ERR_FATAL);
1555331Samw 	}
1565331Samw 
1575331Samw 	if (smbd_already_running())
1585331Samw 		return (SMF_EXIT_OK);
1595331Samw 
16012890SJoyce.McIntosh@Sun.COM 	/*
16112890SJoyce.McIntosh@Sun.COM 	 * Raise the file descriptor limit to accommodate simultaneous user
16212890SJoyce.McIntosh@Sun.COM 	 * authentications/file access.
16312890SJoyce.McIntosh@Sun.COM 	 */
16412890SJoyce.McIntosh@Sun.COM 	if ((getrlimit(RLIMIT_NOFILE, &rl) == 0) &&
16512890SJoyce.McIntosh@Sun.COM 	    (rl.rlim_cur < rl.rlim_max)) {
16612890SJoyce.McIntosh@Sun.COM 		orig_limit = rl.rlim_cur;
16712890SJoyce.McIntosh@Sun.COM 		rl.rlim_cur = rl.rlim_max;
16812890SJoyce.McIntosh@Sun.COM 		if (setrlimit(RLIMIT_NOFILE, &rl) != 0)
16912890SJoyce.McIntosh@Sun.COM 			smbd_report("Failed to raise file descriptor limit"
17012890SJoyce.McIntosh@Sun.COM 			    " from %d to %d", orig_limit, rl.rlim_cur);
17112890SJoyce.McIntosh@Sun.COM 	}
17212890SJoyce.McIntosh@Sun.COM 
1735331Samw 	(void) sigfillset(&set);
1745331Samw 	(void) sigdelset(&set, SIGABRT);
1755331Samw 
1765331Samw 	(void) sigfillset(&act.sa_mask);
1775331Samw 	act.sa_handler = smbd_sig_handler;
1785331Samw 	act.sa_flags = 0;
1795331Samw 
18011963SAfshin.Ardakani@Sun.COM 	(void) sigaction(SIGABRT, &act, NULL);
1815331Samw 	(void) sigaction(SIGTERM, &act, NULL);
1825331Samw 	(void) sigaction(SIGHUP, &act, NULL);
1835331Samw 	(void) sigaction(SIGINT, &act, NULL);
1845331Samw 	(void) sigaction(SIGPIPE, &act, NULL);
18512508Samw@Sun.COM 	(void) sigaction(SIGUSR1, &act, NULL);
1865331Samw 
1875331Samw 	(void) sigdelset(&set, SIGTERM);
1885331Samw 	(void) sigdelset(&set, SIGHUP);
1895331Samw 	(void) sigdelset(&set, SIGINT);
1905331Samw 	(void) sigdelset(&set, SIGPIPE);
19112508Samw@Sun.COM 	(void) sigdelset(&set, SIGUSR1);
1925331Samw 
1935331Samw 	if (smbd.s_fg) {
1945331Samw 		(void) sigdelset(&set, SIGTSTP);
1955331Samw 		(void) sigdelset(&set, SIGTTIN);
1965331Samw 		(void) sigdelset(&set, SIGTTOU);
1975331Samw 
1985331Samw 		if (smbd_service_init() != 0) {
1995331Samw 			smbd_report("service initialization failed");
2005331Samw 			exit(SMF_EXIT_ERR_FATAL);
2015331Samw 		}
2025331Samw 	} else {
2035331Samw 		/*
2045331Samw 		 * "pfd" is a pipe descriptor -- any fatal errors
2055331Samw 		 * during subsequent initialization of the child
2065331Samw 		 * process should be written to this pipe and the
2075331Samw 		 * parent will report this error as the exit status.
2085331Samw 		 */
2095331Samw 		pfd = smbd_daemonize_init();
2105331Samw 
2115331Samw 		if (smbd_service_init() != 0) {
2125331Samw 			smbd_report("daemon initialization failed");
2135331Samw 			exit(SMF_EXIT_ERR_FATAL);
2145331Samw 		}
2155331Samw 
2165331Samw 		smbd_daemonize_fini(pfd, SMF_EXIT_OK);
2175331Samw 	}
2185331Samw 
21911963SAfshin.Ardakani@Sun.COM 	(void) atexit(smb_kmod_stop);
2205331Samw 
2218334SJose.Borrego@Sun.COM 	while (!smbd.s_shutting_down) {
2228334SJose.Borrego@Sun.COM 		if (smbd.s_sigval == 0 && smbd.s_refreshes == 0)
2237052Samw 			(void) sigsuspend(&set);
2245331Samw 
2258334SJose.Borrego@Sun.COM 		sigval = atomic_swap_uint(&smbd.s_sigval, 0);
2267052Samw 
2277052Samw 		switch (sigval) {
2285331Samw 		case 0:
2295331Samw 		case SIGPIPE:
23011963SAfshin.Ardakani@Sun.COM 		case SIGABRT:
2315331Samw 			break;
2325331Samw 
2335331Samw 		case SIGHUP:
2348334SJose.Borrego@Sun.COM 			syslog(LOG_DEBUG, "refresh requested");
2355331Samw 			(void) pthread_cond_signal(&refresh_cond);
2365331Samw 			break;
2375331Samw 
23812508Samw@Sun.COM 		case SIGUSR1:
23912508Samw@Sun.COM 			smb_log_dumpall();
24012508Samw@Sun.COM 			break;
24112508Samw@Sun.COM 
2425331Samw 		default:
2435331Samw 			/*
2445331Samw 			 * Typically SIGINT or SIGTERM.
2455331Samw 			 */
2468334SJose.Borrego@Sun.COM 			smbd.s_shutting_down = B_TRUE;
2475331Samw 			break;
2485331Samw 		}
2495331Samw 	}
2505331Samw 
2515331Samw 	smbd_service_fini();
2525331Samw 	closelog();
2539832Samw@Sun.COM 	return ((smbd.s_fatal_error) ? SMF_EXIT_ERR_FATAL : SMF_EXIT_OK);
2545331Samw }
2555331Samw 
2565331Samw /*
2575331Samw  * This function will fork off a child process,
2585331Samw  * from which only the child will return.
2595331Samw  *
2605331Samw  * Use SMF error codes only on exit.
2615331Samw  */
2625331Samw static int
2635331Samw smbd_daemonize_init(void)
2645331Samw {
2655331Samw 	int status, pfds[2];
2665331Samw 	sigset_t set, oset;
2675331Samw 	pid_t pid;
2685331Samw 	int rc;
2695331Samw 
2705331Samw 	/*
2715331Samw 	 * Reset privileges to the minimum set required. We continue
2725331Samw 	 * to run as root to create and access files in /var.
2735331Samw 	 */
2749832Samw@Sun.COM 	rc = smb_init_daemon_priv(PU_RESETGROUPS, smbd.s_uid, smbd.s_gid);
2755331Samw 
2765331Samw 	if (rc != 0) {
2775331Samw 		smbd_report("insufficient privileges");
2785331Samw 		exit(SMF_EXIT_ERR_FATAL);
2795331Samw 	}
2805331Samw 
2815331Samw 	/*
2825331Samw 	 * Block all signals prior to the fork and leave them blocked in the
2835331Samw 	 * parent so we don't get in a situation where the parent gets SIGINT
2845331Samw 	 * and returns non-zero exit status and the child is actually running.
2855331Samw 	 * In the child, restore the signal mask once we've done our setsid().
2865331Samw 	 */
2875331Samw 	(void) sigfillset(&set);
2885331Samw 	(void) sigdelset(&set, SIGABRT);
2895331Samw 	(void) sigprocmask(SIG_BLOCK, &set, &oset);
2905331Samw 
2915331Samw 	if (pipe(pfds) == -1) {
2925331Samw 		smbd_report("unable to create pipe");
2935331Samw 		exit(SMF_EXIT_ERR_FATAL);
2945331Samw 	}
2955331Samw 
2965331Samw 	closelog();
2975331Samw 
2985331Samw 	if ((pid = fork()) == -1) {
2995331Samw 		openlog(smbd.s_pname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
3005331Samw 		smbd_report("unable to fork");
3015331Samw 		closelog();
3025331Samw 		exit(SMF_EXIT_ERR_FATAL);
3035331Samw 	}
3045331Samw 
3055331Samw 	/*
3065331Samw 	 * If we're the parent process, wait for either the child to send us
3075331Samw 	 * the appropriate exit status over the pipe or for the read to fail
3085331Samw 	 * (presumably with 0 for EOF if our child terminated abnormally).
3095331Samw 	 * If the read fails, exit with either the child's exit status if it
3105331Samw 	 * exited or with SMF_EXIT_ERR_FATAL if it died from a fatal signal.
3115331Samw 	 */
3125331Samw 	if (pid != 0) {
3135331Samw 		(void) close(pfds[1]);
3145331Samw 
3155331Samw 		if (read(pfds[0], &status, sizeof (status)) == sizeof (status))
3165331Samw 			_exit(status);
3175331Samw 
3185331Samw 		if (waitpid(pid, &status, 0) == pid && WIFEXITED(status))
3195331Samw 			_exit(WEXITSTATUS(status));
3205331Samw 
3215331Samw 		_exit(SMF_EXIT_ERR_FATAL);
3225331Samw 	}
3235331Samw 
3245331Samw 	openlog(smbd.s_pname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
3255331Samw 	(void) setsid();
3265331Samw 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
3275331Samw 	(void) chdir("/");
3285331Samw 	(void) umask(022);
3295331Samw 	(void) close(pfds[0]);
3305331Samw 
3315331Samw 	return (pfds[1]);
3325331Samw }
3335331Samw 
3349832Samw@Sun.COM /*
3359832Samw@Sun.COM  * This function is based on __init_daemon_priv() and replaces
3369832Samw@Sun.COM  * __init_daemon_priv() since we want smbd to have all privileges so that it
3379832Samw@Sun.COM  * can execute map/unmap commands with all privileges during share
3389832Samw@Sun.COM  * connection/disconnection.  Unused privileges are disabled until command
3399832Samw@Sun.COM  * execution.  The permitted and the limit set contains all privileges.  The
3409832Samw@Sun.COM  * inheritable set contains no privileges.
3419832Samw@Sun.COM  */
3429832Samw@Sun.COM 
3439832Samw@Sun.COM static const char root_cp[] = "/core.%f.%t";
3449832Samw@Sun.COM static const char daemon_cp[] = "/var/tmp/core.%f.%t";
3459832Samw@Sun.COM 
3469832Samw@Sun.COM static int
3479832Samw@Sun.COM smb_init_daemon_priv(int flags, uid_t uid, gid_t gid)
3489832Samw@Sun.COM {
3499832Samw@Sun.COM 	priv_set_t *perm = NULL;
3509832Samw@Sun.COM 	int ret = -1;
3519832Samw@Sun.COM 	char buf[1024];
3529832Samw@Sun.COM 
3539832Samw@Sun.COM 	/*
3549832Samw@Sun.COM 	 * This is not a significant failure: it allows us to start programs
3559832Samw@Sun.COM 	 * with sufficient privileges and with the proper uid.   We don't
3569832Samw@Sun.COM 	 * care enough about the extra groups in that case.
3579832Samw@Sun.COM 	 */
3589832Samw@Sun.COM 	if (flags & PU_RESETGROUPS)
3599832Samw@Sun.COM 		(void) setgroups(0, NULL);
3609832Samw@Sun.COM 
3619832Samw@Sun.COM 	if (gid != (gid_t)-1 && setgid(gid) != 0)
3629832Samw@Sun.COM 		goto end;
3639832Samw@Sun.COM 
3649832Samw@Sun.COM 	perm = priv_allocset();
3659832Samw@Sun.COM 	if (perm == NULL)
3669832Samw@Sun.COM 		goto end;
3679832Samw@Sun.COM 
3689832Samw@Sun.COM 	/* E = P */
3699832Samw@Sun.COM 	(void) getppriv(PRIV_PERMITTED, perm);
3709832Samw@Sun.COM 	(void) setppriv(PRIV_SET, PRIV_EFFECTIVE, perm);
3719832Samw@Sun.COM 
3729832Samw@Sun.COM 	/* Now reset suid and euid */
3739832Samw@Sun.COM 	if (uid != (uid_t)-1 && setreuid(uid, uid) != 0)
3749832Samw@Sun.COM 		goto end;
3759832Samw@Sun.COM 
3769832Samw@Sun.COM 	/* I = 0 */
3779832Samw@Sun.COM 	priv_emptyset(perm);
3789832Samw@Sun.COM 	ret = setppriv(PRIV_SET, PRIV_INHERITABLE, perm);
3799832Samw@Sun.COM end:
3809832Samw@Sun.COM 	priv_freeset(perm);
3819832Samw@Sun.COM 
3829832Samw@Sun.COM 	if (core_get_process_path(buf, sizeof (buf), getpid()) == 0 &&
3839832Samw@Sun.COM 	    strcmp(buf, "core") == 0) {
3849832Samw@Sun.COM 
3859832Samw@Sun.COM 		if ((uid == (uid_t)-1 ? geteuid() : uid) == 0) {
3869832Samw@Sun.COM 			(void) core_set_process_path(root_cp, sizeof (root_cp),
3879832Samw@Sun.COM 			    getpid());
3889832Samw@Sun.COM 		} else {
3899832Samw@Sun.COM 			(void) core_set_process_path(daemon_cp,
3909832Samw@Sun.COM 			    sizeof (daemon_cp), getpid());
3919832Samw@Sun.COM 		}
3929832Samw@Sun.COM 	}
3939832Samw@Sun.COM 	(void) setpflags(__PROC_PROTECT, 0);
3949832Samw@Sun.COM 
3959832Samw@Sun.COM 	return (ret);
3969832Samw@Sun.COM }
3979832Samw@Sun.COM 
3989832Samw@Sun.COM /*
3999832Samw@Sun.COM  * Most privileges, except the ones that are required for smbd, are turn off
4009832Samw@Sun.COM  * in the effective set.  They will be turn on when needed for command
4019832Samw@Sun.COM  * execution during share connection/disconnection.
4029832Samw@Sun.COM  */
4035331Samw static void
4045331Samw smbd_daemonize_fini(int fd, int exit_status)
4055331Samw {
4069832Samw@Sun.COM 	priv_set_t *pset;
4079832Samw@Sun.COM 
4085331Samw 	/*
4095331Samw 	 * Now that we're running, if a pipe fd was specified, write an exit
4105331Samw 	 * status to it to indicate that our parent process can safely detach.
4115331Samw 	 * Then proceed to loading the remaining non-built-in modules.
4125331Samw 	 */
4135331Samw 	if (fd >= 0)
4145331Samw 		(void) write(fd, &exit_status, sizeof (exit_status));
4155331Samw 
4165331Samw 	(void) close(fd);
4175331Samw 
4189832Samw@Sun.COM 	pset = priv_allocset();
4199832Samw@Sun.COM 	if (pset == NULL)
4209832Samw@Sun.COM 		return;
4219832Samw@Sun.COM 
42211537SCasper.Dik@Sun.COM 	priv_basicset(pset);
4239832Samw@Sun.COM 
4249832Samw@Sun.COM 	/* list of privileges for smbd */
4259832Samw@Sun.COM 	(void) priv_addset(pset, PRIV_NET_MAC_AWARE);
4269832Samw@Sun.COM 	(void) priv_addset(pset, PRIV_NET_PRIVADDR);
4279832Samw@Sun.COM 	(void) priv_addset(pset, PRIV_PROC_AUDIT);
4289832Samw@Sun.COM 	(void) priv_addset(pset, PRIV_SYS_DEVICES);
4299832Samw@Sun.COM 	(void) priv_addset(pset, PRIV_SYS_SMB);
43011963SAfshin.Ardakani@Sun.COM 	(void) priv_addset(pset, PRIV_SYS_MOUNT);
4319832Samw@Sun.COM 
4329832Samw@Sun.COM 	priv_inverse(pset);
4339832Samw@Sun.COM 
4349832Samw@Sun.COM 	/* turn off unneeded privileges */
4359832Samw@Sun.COM 	(void) setppriv(PRIV_OFF, PRIV_EFFECTIVE, pset);
4369832Samw@Sun.COM 
4379832Samw@Sun.COM 	priv_freeset(pset);
4389832Samw@Sun.COM 
4399832Samw@Sun.COM 	/* reenable core dumps */
4409832Samw@Sun.COM 	__fini_daemon_priv(NULL);
4415331Samw }
4425331Samw 
4436139Sjb150015 /*
4446139Sjb150015  * smbd_service_init
4456139Sjb150015  */
4465331Samw static int
4475331Samw smbd_service_init(void)
4485331Samw {
44911963SAfshin.Ardakani@Sun.COM 	static struct dir {
45011963SAfshin.Ardakani@Sun.COM 		char	*name;
45111963SAfshin.Ardakani@Sun.COM 		int	perm;
45211963SAfshin.Ardakani@Sun.COM 	} dir[] = {
45311963SAfshin.Ardakani@Sun.COM 		{ SMB_DBDIR,	0700 },
45411963SAfshin.Ardakani@Sun.COM 		{ SMB_CVOL,	0755 },
45511963SAfshin.Ardakani@Sun.COM 		{ SMB_SYSROOT,	0755 },
45611963SAfshin.Ardakani@Sun.COM 		{ SMB_SYSTEM32,	0755 },
45711963SAfshin.Ardakani@Sun.COM 		{ SMB_VSS,	0755 }
45811963SAfshin.Ardakani@Sun.COM 	};
45911963SAfshin.Ardakani@Sun.COM 	int	rc, i;
46011963SAfshin.Ardakani@Sun.COM 
46111963SAfshin.Ardakani@Sun.COM 	(void) mutex_lock(&smbd_service_mutex);
4626432Sas200622 
4639832Samw@Sun.COM 	smbd.s_pid = getpid();
46411963SAfshin.Ardakani@Sun.COM 	for (i = 0; i < sizeof (dir)/sizeof (dir[0]); ++i) {
46511963SAfshin.Ardakani@Sun.COM 		if ((mkdir(dir[i].name, dir[i].perm) < 0) &&
46611963SAfshin.Ardakani@Sun.COM 		    (errno != EEXIST)) {
46711963SAfshin.Ardakani@Sun.COM 			smbd_report("mkdir %s: %s", dir[i].name,
46811963SAfshin.Ardakani@Sun.COM 			    strerror(errno));
46911963SAfshin.Ardakani@Sun.COM 			(void) mutex_unlock(&smbd_service_mutex);
47011963SAfshin.Ardakani@Sun.COM 			return (-1);
47111963SAfshin.Ardakani@Sun.COM 		}
4725331Samw 	}
4735331Samw 
4746139Sjb150015 	if ((rc = smb_ccache_init(SMB_VARRUN_DIR, SMB_CCACHE_FILE)) != 0) {
4756139Sjb150015 		if (rc == -1)
4766139Sjb150015 			smbd_report("mkdir %s: %s", SMB_VARRUN_DIR,
4776139Sjb150015 			    strerror(errno));
4786139Sjb150015 		else
4796139Sjb150015 			smbd_report("unable to set KRB5CCNAME");
48011963SAfshin.Ardakani@Sun.COM 		(void) mutex_unlock(&smbd_service_mutex);
48111963SAfshin.Ardakani@Sun.COM 		return (-1);
4825331Samw 	}
4835331Samw 
48412508Samw@Sun.COM 	smbd.s_loghd = smb_log_create(SMBD_LOGSIZE, SMBD_LOGNAME);
48510966SJordan.Brown@Sun.COM 	smb_codepage_init();
4865331Samw 
487*13082SJoyce.McIntosh@Sun.COM 	rc = smbd_cups_init();
488*13082SJoyce.McIntosh@Sun.COM 	if (smb_config_getbool(SMB_CI_PRINT_ENABLE))
489*13082SJoyce.McIntosh@Sun.COM 		smbd_report("print service %savailable", (rc == 0) ? "" : "un");
490*13082SJoyce.McIntosh@Sun.COM 
49112508Samw@Sun.COM 	if (smbd_nicmon_start(SMBD_DEFAULT_INSTANCE_FMRI) != 0)
49212508Samw@Sun.COM 		smbd_report("NIC monitor failed to start");
4936432Sas200622 
494*13082SJoyce.McIntosh@Sun.COM 	smbd_dyndns_init();
49510717Samw@Sun.COM 	smb_ipc_init();
4965331Samw 
4975772Sas200622 	if (smb_netbios_start() != 0)
4985772Sas200622 		smbd_report("NetBIOS services failed to start");
4995772Sas200622 	else
5005772Sas200622 		smbd_report("NetBIOS services started");
5015772Sas200622 
5026432Sas200622 	smbd.s_secmode = smb_config_get_secmode();
50310717Samw@Sun.COM 	if ((rc = smb_domain_init(smbd.s_secmode)) != 0) {
5045772Sas200622 		if (rc == SMB_DOMAIN_NOMACHINE_SID) {
5055772Sas200622 			smbd_report(
5065772Sas200622 			    "no machine SID: check idmap configuration");
50711963SAfshin.Ardakani@Sun.COM 			(void) mutex_unlock(&smbd_service_mutex);
50811963SAfshin.Ardakani@Sun.COM 			return (-1);
5095772Sas200622 		}
5105331Samw 	}
5115331Samw 
51212914SJoyce.McIntosh@Sun.COM 	if (smbd_dc_monitor_init() != 0)
51312914SJoyce.McIntosh@Sun.COM 		smbd_report("DC monitor initialization failed %s",
51412914SJoyce.McIntosh@Sun.COM 		    strerror(errno));
51512914SJoyce.McIntosh@Sun.COM 
51611963SAfshin.Ardakani@Sun.COM 	if (mlsvc_init() != 0) {
5175331Samw 		smbd_report("msrpc initialization failed");
51811963SAfshin.Ardakani@Sun.COM 		(void) mutex_unlock(&smbd_service_mutex);
51911963SAfshin.Ardakani@Sun.COM 		return (-1);
5205331Samw 	}
5215331Samw 
52211963SAfshin.Ardakani@Sun.COM 	smbd.s_door_srv = smbd_door_start();
52311963SAfshin.Ardakani@Sun.COM 	smbd.s_door_opipe = smbd_opipe_start();
52411963SAfshin.Ardakani@Sun.COM 	if (smbd.s_door_srv < 0 || smbd.s_door_opipe < 0) {
52511963SAfshin.Ardakani@Sun.COM 		smbd_report("door initialization failed %s", strerror(errno));
52611963SAfshin.Ardakani@Sun.COM 		(void) mutex_unlock(&smbd_service_mutex);
52711963SAfshin.Ardakani@Sun.COM 		return (-1);
52811963SAfshin.Ardakani@Sun.COM 	}
5295331Samw 
53011963SAfshin.Ardakani@Sun.COM 	if (smbd_refresh_init() != 0) {
53111963SAfshin.Ardakani@Sun.COM 		(void) mutex_unlock(&smbd_service_mutex);
53211963SAfshin.Ardakani@Sun.COM 		return (-1);
53311963SAfshin.Ardakani@Sun.COM 	}
5345331Samw 
5358334SJose.Borrego@Sun.COM 	dyndns_update_zones();
536*13082SJoyce.McIntosh@Sun.COM 	smbd_localtime_init();
5375772Sas200622 	(void) smb_lgrp_start();
5387052Samw 	smb_pwd_init(B_TRUE);
5396030Sjb150015 
54011963SAfshin.Ardakani@Sun.COM 	if (smb_shr_start() != 0) {
5417961SNatalie.Li@Sun.COM 		smbd_report("share initialization failed: %s", strerror(errno));
54211963SAfshin.Ardakani@Sun.COM 		(void) mutex_unlock(&smbd_service_mutex);
54311963SAfshin.Ardakani@Sun.COM 		return (-1);
5447961SNatalie.Li@Sun.COM 	}
5457961SNatalie.Li@Sun.COM 
54611963SAfshin.Ardakani@Sun.COM 	smbd.s_door_lmshr = smbd_share_start();
54711963SAfshin.Ardakani@Sun.COM 	if (smbd.s_door_lmshr < 0)
54811963SAfshin.Ardakani@Sun.COM 		smbd_report("share initialization failed");
54911963SAfshin.Ardakani@Sun.COM 
55011963SAfshin.Ardakani@Sun.COM 	if (smbd_kernel_bind() != 0) {
55111963SAfshin.Ardakani@Sun.COM 		(void) mutex_unlock(&smbd_service_mutex);
55211963SAfshin.Ardakani@Sun.COM 		return (-1);
5536432Sas200622 	}
5545331Samw 
555*13082SJoyce.McIntosh@Sun.COM 	smbd_load_shares();
556*13082SJoyce.McIntosh@Sun.COM 	smbd_load_printers();
55712890SJoyce.McIntosh@Sun.COM 
55811963SAfshin.Ardakani@Sun.COM 	smbd.s_initialized = B_TRUE;
55911963SAfshin.Ardakani@Sun.COM 	smbd_report("service initialized");
56011963SAfshin.Ardakani@Sun.COM 	(void) cond_signal(&smbd_service_cv);
56111963SAfshin.Ardakani@Sun.COM 	(void) mutex_unlock(&smbd_service_mutex);
5627348SJose.Borrego@Sun.COM 	return (0);
5635521Sas200622 }
5645521Sas200622 
5655331Samw /*
56611963SAfshin.Ardakani@Sun.COM  * Shutdown smbd and smbsrv kernel services.
56711963SAfshin.Ardakani@Sun.COM  *
56811963SAfshin.Ardakani@Sun.COM  * Shutdown will not begin until initialization has completed.
56911963SAfshin.Ardakani@Sun.COM  * Only one thread is allowed to perform the shutdown.  Other
57011963SAfshin.Ardakani@Sun.COM  * threads will be blocked on fini_in_progress until the process
57111963SAfshin.Ardakani@Sun.COM  * has exited.
5725331Samw  */
5735331Samw static void
5745331Samw smbd_service_fini(void)
5755331Samw {
57611963SAfshin.Ardakani@Sun.COM 	static uint_t	fini_in_progress;
57711963SAfshin.Ardakani@Sun.COM 
57811963SAfshin.Ardakani@Sun.COM 	(void) mutex_lock(&smbd_service_mutex);
57911963SAfshin.Ardakani@Sun.COM 
58011963SAfshin.Ardakani@Sun.COM 	while (!smbd.s_initialized)
58111963SAfshin.Ardakani@Sun.COM 		(void) cond_wait(&smbd_service_cv, &smbd_service_mutex);
58211963SAfshin.Ardakani@Sun.COM 
58311963SAfshin.Ardakani@Sun.COM 	if (atomic_swap_uint(&fini_in_progress, 1) != 0) {
58411963SAfshin.Ardakani@Sun.COM 		while (fini_in_progress)
58511963SAfshin.Ardakani@Sun.COM 			(void) cond_wait(&smbd_service_cv, &smbd_service_mutex);
58611963SAfshin.Ardakani@Sun.COM 		/*NOTREACHED*/
58711963SAfshin.Ardakani@Sun.COM 	}
58811963SAfshin.Ardakani@Sun.COM 
58911963SAfshin.Ardakani@Sun.COM 	smbd.s_shutting_down = B_TRUE;
59011963SAfshin.Ardakani@Sun.COM 	smbd_report("service shutting down");
59111963SAfshin.Ardakani@Sun.COM 
59211963SAfshin.Ardakani@Sun.COM 	smb_kmod_stop();
59311963SAfshin.Ardakani@Sun.COM 	smb_logon_abort();
59411963SAfshin.Ardakani@Sun.COM 	smb_lgrp_stop();
59511963SAfshin.Ardakani@Sun.COM 	smbd_opipe_stop();
59611963SAfshin.Ardakani@Sun.COM 	smbd_door_stop();
5975331Samw 	smbd_refresh_fini();
5985331Samw 	smbd_kernel_unbind();
59911963SAfshin.Ardakani@Sun.COM 	smbd_share_stop();
6007052Samw 	smb_shr_stop();
6018334SJose.Borrego@Sun.COM 	dyndns_stop();
60212508Samw@Sun.COM 	smbd_nicmon_stop();
6036139Sjb150015 	smb_ccache_remove(SMB_CCACHE_PATH);
6046030Sjb150015 	smb_pwd_fini();
60510717Samw@Sun.COM 	smb_domain_fini();
6069832Samw@Sun.COM 	mlsvc_fini();
60710717Samw@Sun.COM 	smb_netbios_stop();
608*13082SJoyce.McIntosh@Sun.COM 	smbd_cups_fini();
60911963SAfshin.Ardakani@Sun.COM 
61011963SAfshin.Ardakani@Sun.COM 	smbd.s_initialized = B_FALSE;
61111963SAfshin.Ardakani@Sun.COM 	smbd_report("service terminated");
61211963SAfshin.Ardakani@Sun.COM 	(void) mutex_unlock(&smbd_service_mutex);
61311963SAfshin.Ardakani@Sun.COM 	exit((smbd.s_fatal_error) ? SMF_EXIT_ERR_FATAL : SMF_EXIT_OK);
6145331Samw }
6155331Samw 
6165331Samw /*
6175331Samw  * smbd_refresh_init()
6185331Samw  *
6195331Samw  * SMB service refresh thread initialization.  This thread waits for a
6205331Samw  * refresh event and updates the daemon's view of the configuration
6215331Samw  * before going back to sleep.
6225331Samw  */
6235331Samw static int
6245331Samw smbd_refresh_init()
6255331Samw {
6266432Sas200622 	pthread_attr_t		tattr;
6276432Sas200622 	pthread_condattr_t	cattr;
6286432Sas200622 	int			rc;
6295331Samw 
6305331Samw 	(void) pthread_condattr_init(&cattr);
6315331Samw 	(void) pthread_cond_init(&refresh_cond, &cattr);
6325331Samw 	(void) pthread_condattr_destroy(&cattr);
6335331Samw 
6345331Samw 	(void) pthread_mutex_init(&refresh_mutex, NULL);
6355331Samw 
6365331Samw 	(void) pthread_attr_init(&tattr);
6375331Samw 	(void) pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
63812914SJoyce.McIntosh@Sun.COM 	rc = pthread_create(&smbd.s_refresh_tid, &tattr, smbd_refresh_monitor,
63912914SJoyce.McIntosh@Sun.COM 	    NULL);
6405331Samw 	(void) pthread_attr_destroy(&tattr);
6416432Sas200622 
642*13082SJoyce.McIntosh@Sun.COM 	if (rc != 0)
643*13082SJoyce.McIntosh@Sun.COM 		smbd_report("unable to start refresh monitor: %s",
644*13082SJoyce.McIntosh@Sun.COM 		    strerror(errno));
6455331Samw 	return (rc);
6465331Samw }
6475331Samw 
6485331Samw /*
6495331Samw  * smbd_refresh_fini()
6505331Samw  *
6515331Samw  * Stop the refresh thread.
6525331Samw  */
6535331Samw static void
6545331Samw smbd_refresh_fini()
6555331Samw {
656*13082SJoyce.McIntosh@Sun.COM 	if ((pthread_self() != smbd.s_refresh_tid) &&
657*13082SJoyce.McIntosh@Sun.COM 	    (smbd.s_refresh_tid != 0)) {
65812914SJoyce.McIntosh@Sun.COM 		(void) pthread_cancel(smbd.s_refresh_tid);
65911963SAfshin.Ardakani@Sun.COM 		(void) pthread_cond_destroy(&refresh_cond);
66011963SAfshin.Ardakani@Sun.COM 		(void) pthread_mutex_destroy(&refresh_mutex);
66111963SAfshin.Ardakani@Sun.COM 	}
6625331Samw }
6635331Samw 
6645331Samw /*
665*13082SJoyce.McIntosh@Sun.COM  * Wait for refresh events.  When woken up, update the smbd configuration
666*13082SJoyce.McIntosh@Sun.COM  * from SMF and check for changes that require service reconfiguration.
667*13082SJoyce.McIntosh@Sun.COM  * Throttling is applied to coallesce multiple refresh events when the
668*13082SJoyce.McIntosh@Sun.COM  * service is being refreshed repeatedly.
6695331Samw  */
6705331Samw /*ARGSUSED*/
6715331Samw static void *
6725331Samw smbd_refresh_monitor(void *arg)
6735331Samw {
674*13082SJoyce.McIntosh@Sun.COM 	smbd_online_wait("smbd_refresh_monitor");
6756771Sjb150015 
6768334SJose.Borrego@Sun.COM 	while (!smbd.s_shutting_down) {
677*13082SJoyce.McIntosh@Sun.COM 		(void) sleep(SMBD_REFRESH_INTERVAL);
678*13082SJoyce.McIntosh@Sun.COM 
6798334SJose.Borrego@Sun.COM 		(void) pthread_mutex_lock(&refresh_mutex);
6808334SJose.Borrego@Sun.COM 		while ((atomic_swap_uint(&smbd.s_refreshes, 0) == 0) &&
6818334SJose.Borrego@Sun.COM 		    (!smbd.s_shutting_down))
6828334SJose.Borrego@Sun.COM 			(void) pthread_cond_wait(&refresh_cond, &refresh_mutex);
6838334SJose.Borrego@Sun.COM 		(void) pthread_mutex_unlock(&refresh_mutex);
6848334SJose.Borrego@Sun.COM 
6858334SJose.Borrego@Sun.COM 		if (smbd.s_shutting_down) {
68611963SAfshin.Ardakani@Sun.COM 			smbd_service_fini();
68711963SAfshin.Ardakani@Sun.COM 			/*NOTREACHED*/
6888334SJose.Borrego@Sun.COM 		}
6898334SJose.Borrego@Sun.COM 
69011963SAfshin.Ardakani@Sun.COM 		(void) mutex_lock(&smbd_service_mutex);
69111963SAfshin.Ardakani@Sun.COM 
692*13082SJoyce.McIntosh@Sun.COM 		smbd_dc_monitor_refresh();
6936139Sjb150015 		smb_ccache_remove(SMB_CCACHE_PATH);
6946432Sas200622 
6958334SJose.Borrego@Sun.COM 		/*
6968334SJose.Borrego@Sun.COM 		 * Clear the DNS zones for the existing interfaces
6978334SJose.Borrego@Sun.COM 		 * before updating the NIC interface list.
6988334SJose.Borrego@Sun.COM 		 */
6998334SJose.Borrego@Sun.COM 		dyndns_clear_zones();
7006432Sas200622 
70112508Samw@Sun.COM 		if (smbd_nicmon_refresh() != 0)
70212508Samw@Sun.COM 			smbd_report("NIC monitor refresh failed");
703*13082SJoyce.McIntosh@Sun.COM 
7046432Sas200622 		smb_netbios_name_reconfig();
7056432Sas200622 		smb_browser_reconfig();
7068334SJose.Borrego@Sun.COM 		dyndns_update_zones();
707*13082SJoyce.McIntosh@Sun.COM 		(void) smbd_kernel_bind();
708*13082SJoyce.McIntosh@Sun.COM 		smbd_load_shares();
709*13082SJoyce.McIntosh@Sun.COM 		smbd_load_printers();
7106432Sas200622 
71111963SAfshin.Ardakani@Sun.COM 		(void) mutex_unlock(&smbd_service_mutex);
7125331Samw 	}
7138334SJose.Borrego@Sun.COM 
714*13082SJoyce.McIntosh@Sun.COM 	smbd.s_refresh_tid = 0;
7155331Samw 	return (NULL);
7165331Samw }
7175331Samw 
7188334SJose.Borrego@Sun.COM void
7198334SJose.Borrego@Sun.COM smbd_set_secmode(int secmode)
7208334SJose.Borrego@Sun.COM {
7218334SJose.Borrego@Sun.COM 	switch (secmode) {
7228334SJose.Borrego@Sun.COM 	case SMB_SECMODE_WORKGRP:
7238334SJose.Borrego@Sun.COM 	case SMB_SECMODE_DOMAIN:
7248334SJose.Borrego@Sun.COM 		(void) smb_config_set_secmode(secmode);
7258334SJose.Borrego@Sun.COM 		smbd.s_secmode = secmode;
7268334SJose.Borrego@Sun.COM 		break;
7278334SJose.Borrego@Sun.COM 
7288334SJose.Borrego@Sun.COM 	default:
7298334SJose.Borrego@Sun.COM 		syslog(LOG_ERR, "invalid security mode: %d", secmode);
7308334SJose.Borrego@Sun.COM 		syslog(LOG_ERR, "entering maintenance mode");
7318334SJose.Borrego@Sun.COM 		(void) smb_smf_maintenance_mode();
7328334SJose.Borrego@Sun.COM 	}
7338334SJose.Borrego@Sun.COM }
7345331Samw 
7355331Samw /*
73611963SAfshin.Ardakani@Sun.COM  * The service is online if initialization is complete and shutdown
73711963SAfshin.Ardakani@Sun.COM  * has not begun.
73811963SAfshin.Ardakani@Sun.COM  */
73911963SAfshin.Ardakani@Sun.COM boolean_t
74011963SAfshin.Ardakani@Sun.COM smbd_online(void)
74111963SAfshin.Ardakani@Sun.COM {
74211963SAfshin.Ardakani@Sun.COM 	return (smbd.s_initialized && !smbd.s_shutting_down);
74311963SAfshin.Ardakani@Sun.COM }
74411963SAfshin.Ardakani@Sun.COM 
74511963SAfshin.Ardakani@Sun.COM /*
74612914SJoyce.McIntosh@Sun.COM  * Wait until the service is online.  Provided for threads that
74712914SJoyce.McIntosh@Sun.COM  * should wait until the service has been fully initialized before
74812914SJoyce.McIntosh@Sun.COM  * they start performing operations.
74912914SJoyce.McIntosh@Sun.COM  */
75012914SJoyce.McIntosh@Sun.COM void
75112914SJoyce.McIntosh@Sun.COM smbd_online_wait(const char *text)
75212914SJoyce.McIntosh@Sun.COM {
753*13082SJoyce.McIntosh@Sun.COM 	while (!smbd_online())
754*13082SJoyce.McIntosh@Sun.COM 		(void) sleep(SMBD_ONLINE_WAIT_INTERVAL);
75512914SJoyce.McIntosh@Sun.COM 
756*13082SJoyce.McIntosh@Sun.COM 	if (text != NULL) {
757*13082SJoyce.McIntosh@Sun.COM 		smb_log(smbd.s_loghd, LOG_DEBUG, "%s: online", text);
758*13082SJoyce.McIntosh@Sun.COM 		(void) fprintf(stderr, "%s: online\n", text);
75912914SJoyce.McIntosh@Sun.COM 	}
76012914SJoyce.McIntosh@Sun.COM }
76112914SJoyce.McIntosh@Sun.COM 
76212914SJoyce.McIntosh@Sun.COM /*
7635331Samw  * If the door has already been opened by another process (non-zero pid
7645331Samw  * in target), we assume that another smbd is already running.  If there
7655331Samw  * is a race here, it will be caught later when smbsrv is opened because
7665331Samw  * only one process is allowed to open the device at a time.
7675331Samw  */
7685331Samw static int
7695331Samw smbd_already_running(void)
7705331Samw {
7715331Samw 	door_info_t info;
7725331Samw 	int door;
7735331Samw 
77411963SAfshin.Ardakani@Sun.COM 	if ((door = open(SMBD_DOOR_NAME, O_RDONLY)) < 0)
7755331Samw 		return (0);
7765331Samw 
7775331Samw 	if (door_info(door, &info) < 0)
7785331Samw 		return (0);
7795331Samw 
7805331Samw 	if (info.di_target > 0) {
7815331Samw 		smbd_report("already running: pid %ld\n", info.di_target);
7825331Samw 		(void) close(door);
7835331Samw 		return (1);
7845331Samw 	}
7855331Samw 
7865331Samw 	(void) close(door);
7875331Samw 	return (0);
7885331Samw }
7895331Samw 
7906139Sjb150015 /*
7916139Sjb150015  * smbd_kernel_bind
7926432Sas200622  *
793*13082SJoyce.McIntosh@Sun.COM  * If smbsrv is already bound, reload the configuration and update smbsrv.
794*13082SJoyce.McIntosh@Sun.COM  * Otherwise, open the smbsrv device and start the kernel service.
7956139Sjb150015  */
7965331Samw static int
7976432Sas200622 smbd_kernel_bind(void)
7985331Samw {
799*13082SJoyce.McIntosh@Sun.COM 	smb_kmod_cfg_t	cfg;
800*13082SJoyce.McIntosh@Sun.COM 	int		rc;
8016139Sjb150015 
802*13082SJoyce.McIntosh@Sun.COM 	if (smbd.s_kbound) {
803*13082SJoyce.McIntosh@Sun.COM 		smb_load_kconfig(&cfg);
804*13082SJoyce.McIntosh@Sun.COM 		rc = smb_kmod_setcfg(&cfg);
805*13082SJoyce.McIntosh@Sun.COM 		if (rc < 0)
806*13082SJoyce.McIntosh@Sun.COM 			smbd_report("kernel configuration update failed: %s",
807*13082SJoyce.McIntosh@Sun.COM 			    strerror(errno));
808*13082SJoyce.McIntosh@Sun.COM 		return (rc);
809*13082SJoyce.McIntosh@Sun.COM 	}
810*13082SJoyce.McIntosh@Sun.COM 
811*13082SJoyce.McIntosh@Sun.COM 	if (smb_kmod_isbound())
812*13082SJoyce.McIntosh@Sun.COM 		smbd_kernel_unbind();
8135331Samw 
81411963SAfshin.Ardakani@Sun.COM 	if ((rc = smb_kmod_bind()) == 0) {
8159832Samw@Sun.COM 		rc = smbd_kernel_start();
8169832Samw@Sun.COM 		if (rc != 0)
8179832Samw@Sun.COM 			smb_kmod_unbind();
8189832Samw@Sun.COM 		else
8199832Samw@Sun.COM 			smbd.s_kbound = B_TRUE;
8206139Sjb150015 	}
82111963SAfshin.Ardakani@Sun.COM 
82211963SAfshin.Ardakani@Sun.COM 	if (rc != 0)
82311963SAfshin.Ardakani@Sun.COM 		smbd_report("kernel bind error: %s", strerror(errno));
8249832Samw@Sun.COM 	return (rc);
8259832Samw@Sun.COM }
8269832Samw@Sun.COM 
8279832Samw@Sun.COM static int
8289832Samw@Sun.COM smbd_kernel_start(void)
8299832Samw@Sun.COM {
8309832Samw@Sun.COM 	smb_kmod_cfg_t	cfg;
8319832Samw@Sun.COM 	int		rc;
8329832Samw@Sun.COM 
8339832Samw@Sun.COM 	smb_load_kconfig(&cfg);
8349832Samw@Sun.COM 	rc = smb_kmod_setcfg(&cfg);
8359832Samw@Sun.COM 	if (rc != 0)
8369832Samw@Sun.COM 		return (rc);
8379832Samw@Sun.COM 
8389832Samw@Sun.COM 	rc = smb_kmod_setgmtoff(smbd_gmtoff());
8399832Samw@Sun.COM 	if (rc != 0)
8409832Samw@Sun.COM 		return (rc);
8419832Samw@Sun.COM 
8429832Samw@Sun.COM 	rc = smb_kmod_start(smbd.s_door_opipe, smbd.s_door_lmshr,
8439832Samw@Sun.COM 	    smbd.s_door_srv);
8449832Samw@Sun.COM 	if (rc != 0)
8459832Samw@Sun.COM 		return (rc);
8466139Sjb150015 
8479046SJose.Borrego@Sun.COM 	rc = smbd_start_listeners();
8489832Samw@Sun.COM 	if (rc != 0)
8499832Samw@Sun.COM 		return (rc);
8509832Samw@Sun.COM 
851*13082SJoyce.McIntosh@Sun.COM 	smbd_spool_init();
8529832Samw@Sun.COM 	return (0);
8535331Samw }
8545331Samw 
8556139Sjb150015 /*
8566139Sjb150015  * smbd_kernel_unbind
8576139Sjb150015  */
8586139Sjb150015 static void
8596139Sjb150015 smbd_kernel_unbind(void)
8606139Sjb150015 {
861*13082SJoyce.McIntosh@Sun.COM 	smbd_spool_fini();
8629832Samw@Sun.COM 	smbd_stop_listeners();
8639832Samw@Sun.COM 	smb_kmod_unbind();
8649832Samw@Sun.COM 	smbd.s_kbound = B_FALSE;
8658167Samw@Sun.COM }
8668167Samw@Sun.COM 
8675331Samw /*
868*13082SJoyce.McIntosh@Sun.COM  * Create the Dynamic DNS publisher thread.
86912890SJoyce.McIntosh@Sun.COM  */
870*13082SJoyce.McIntosh@Sun.COM static void
871*13082SJoyce.McIntosh@Sun.COM smbd_dyndns_init(void)
87212890SJoyce.McIntosh@Sun.COM {
873*13082SJoyce.McIntosh@Sun.COM 	pthread_t	tid;
874*13082SJoyce.McIntosh@Sun.COM 	pthread_attr_t	attr;
875*13082SJoyce.McIntosh@Sun.COM 	int		rc;
876*13082SJoyce.McIntosh@Sun.COM 
877*13082SJoyce.McIntosh@Sun.COM 	dyndns_start();
87812890SJoyce.McIntosh@Sun.COM 
879*13082SJoyce.McIntosh@Sun.COM 	(void) pthread_attr_init(&attr);
880*13082SJoyce.McIntosh@Sun.COM 	(void) pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
881*13082SJoyce.McIntosh@Sun.COM 	rc = pthread_create(&tid, &attr, dyndns_publisher, NULL);
882*13082SJoyce.McIntosh@Sun.COM 	(void) pthread_attr_destroy(&attr);
88312890SJoyce.McIntosh@Sun.COM 
884*13082SJoyce.McIntosh@Sun.COM 	if (rc != 0)
885*13082SJoyce.McIntosh@Sun.COM 		smbd_report("unable to start dyndns publisher: %s",
886*13082SJoyce.McIntosh@Sun.COM 		    strerror(errno));
88712890SJoyce.McIntosh@Sun.COM }
88812890SJoyce.McIntosh@Sun.COM 
88912890SJoyce.McIntosh@Sun.COM /*
890*13082SJoyce.McIntosh@Sun.COM  * Launches a thread to populate the share cache by share information
891*13082SJoyce.McIntosh@Sun.COM  * stored in sharemgr
89212890SJoyce.McIntosh@Sun.COM  */
893*13082SJoyce.McIntosh@Sun.COM static void
894*13082SJoyce.McIntosh@Sun.COM smbd_load_shares(void)
89512890SJoyce.McIntosh@Sun.COM {
896*13082SJoyce.McIntosh@Sun.COM 	pthread_t	tid;
897*13082SJoyce.McIntosh@Sun.COM 	pthread_attr_t	attr;
898*13082SJoyce.McIntosh@Sun.COM 	int		rc;
89912890SJoyce.McIntosh@Sun.COM 
900*13082SJoyce.McIntosh@Sun.COM 	(void) pthread_attr_init(&attr);
901*13082SJoyce.McIntosh@Sun.COM 	(void) pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
902*13082SJoyce.McIntosh@Sun.COM 	rc = pthread_create(&tid, &attr, smb_shr_load, NULL);
903*13082SJoyce.McIntosh@Sun.COM 	(void) pthread_attr_destroy(&attr);
90412890SJoyce.McIntosh@Sun.COM 
905*13082SJoyce.McIntosh@Sun.COM 	if (rc != 0)
906*13082SJoyce.McIntosh@Sun.COM 		smbd_report("unable to load disk shares: %s", strerror(errno));
90712890SJoyce.McIntosh@Sun.COM }
90812890SJoyce.McIntosh@Sun.COM 
90912890SJoyce.McIntosh@Sun.COM /*
9105331Samw  * Initialization of the localtime thread.
9115331Samw  * Returns 0 on success, an error number if thread creation fails.
9125331Samw  */
9135331Samw 
914*13082SJoyce.McIntosh@Sun.COM static void
9155331Samw smbd_localtime_init(void)
9165331Samw {
917*13082SJoyce.McIntosh@Sun.COM 	pthread_attr_t	attr;
918*13082SJoyce.McIntosh@Sun.COM 	int		rc;
9195331Samw 
920*13082SJoyce.McIntosh@Sun.COM 	(void) pthread_attr_init(&attr);
921*13082SJoyce.McIntosh@Sun.COM 	(void) pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
922*13082SJoyce.McIntosh@Sun.COM 	rc = pthread_create(&smbd.s_localtime_tid, &attr,
92312914SJoyce.McIntosh@Sun.COM 	    smbd_localtime_monitor, NULL);
924*13082SJoyce.McIntosh@Sun.COM 	(void) pthread_attr_destroy(&attr);
925*13082SJoyce.McIntosh@Sun.COM 
926*13082SJoyce.McIntosh@Sun.COM 	if (rc != 0)
927*13082SJoyce.McIntosh@Sun.COM 		smbd_report("unable to monitor localtime: %s", strerror(errno));
9285331Samw }
9295331Samw 
9305331Samw /*
931*13082SJoyce.McIntosh@Sun.COM  * Send local gmtoff to the kernel module one time at startup and each
932*13082SJoyce.McIntosh@Sun.COM  * time it changes (up to twice a year).
933*13082SJoyce.McIntosh@Sun.COM  * Local gmtoff is checked once every 15 minutes since some timezones
934*13082SJoyce.McIntosh@Sun.COM  * are aligned on half and quarter hour boundaries.
9355331Samw  */
9365331Samw /*ARGSUSED*/
9375331Samw static void *
9385331Samw smbd_localtime_monitor(void *arg)
9395331Samw {
9405331Samw 	struct tm local_tm;
9417348SJose.Borrego@Sun.COM 	time_t secs;
9427348SJose.Borrego@Sun.COM 	int32_t gmtoff, last_gmtoff = -1;
9435331Samw 	int timeout;
9449832Samw@Sun.COM 	int error;
9458167Samw@Sun.COM 
94612914SJoyce.McIntosh@Sun.COM 	smbd_online_wait("smbd_localtime_monitor");
94712914SJoyce.McIntosh@Sun.COM 
9485331Samw 	for (;;) {
9497348SJose.Borrego@Sun.COM 		gmtoff = smbd_gmtoff();
9505331Samw 
9519832Samw@Sun.COM 		if ((last_gmtoff != gmtoff) && smbd.s_kbound) {
9529832Samw@Sun.COM 			error = smb_kmod_setgmtoff(gmtoff);
9539832Samw@Sun.COM 			if (error != 0)
9549832Samw@Sun.COM 				smbd_report("localtime set failed: %s",
9559832Samw@Sun.COM 				    strerror(error));
9565331Samw 		}
9575331Samw 
9585331Samw 		/*
9595331Samw 		 * Align the next iteration on a fifteen minute boundary.
9605331Samw 		 */
9615331Samw 		secs = time(0);
9625331Samw 		(void) localtime_r(&secs, &local_tm);
9635331Samw 		timeout = ((15 - (local_tm.tm_min % 15)) * SECSPERMIN);
9645331Samw 		(void) sleep(timeout);
9655331Samw 
9665331Samw 		last_gmtoff = gmtoff;
9675331Samw 	}
9685331Samw 
9695331Samw 	/*NOTREACHED*/
9705331Samw 	return (NULL);
9715331Samw }
9725331Samw 
9737348SJose.Borrego@Sun.COM /*
9747348SJose.Borrego@Sun.COM  * smbd_gmtoff
9757348SJose.Borrego@Sun.COM  *
9767348SJose.Borrego@Sun.COM  * Determine offset from GMT. If daylight saving time use altzone,
9777348SJose.Borrego@Sun.COM  * otherwise use timezone.
9787348SJose.Borrego@Sun.COM  */
9797348SJose.Borrego@Sun.COM static int32_t
9807348SJose.Borrego@Sun.COM smbd_gmtoff(void)
9817348SJose.Borrego@Sun.COM {
9827348SJose.Borrego@Sun.COM 	time_t clock_val;
9837348SJose.Borrego@Sun.COM 	struct tm *atm;
9847348SJose.Borrego@Sun.COM 	int32_t gmtoff;
9857348SJose.Borrego@Sun.COM 
9867348SJose.Borrego@Sun.COM 	(void) time(&clock_val);
9877348SJose.Borrego@Sun.COM 	atm = localtime(&clock_val);
9887348SJose.Borrego@Sun.COM 
9897348SJose.Borrego@Sun.COM 	gmtoff = (atm->tm_isdst) ? altzone : timezone;
9907348SJose.Borrego@Sun.COM 
9917348SJose.Borrego@Sun.COM 	return (gmtoff);
9927348SJose.Borrego@Sun.COM }
9937348SJose.Borrego@Sun.COM 
9945331Samw static void
9955331Samw smbd_sig_handler(int sigval)
9965331Samw {
9975331Samw 	if (smbd.s_sigval == 0)
9988334SJose.Borrego@Sun.COM 		(void) atomic_swap_uint(&smbd.s_sigval, sigval);
9998334SJose.Borrego@Sun.COM 
10008334SJose.Borrego@Sun.COM 	if (sigval == SIGHUP) {
10018334SJose.Borrego@Sun.COM 		atomic_inc_uint(&smbd.s_refreshes);
10028334SJose.Borrego@Sun.COM 		(void) pthread_cond_signal(&refresh_cond);
10038334SJose.Borrego@Sun.COM 	}
10048334SJose.Borrego@Sun.COM 
10058334SJose.Borrego@Sun.COM 	if (sigval == SIGINT || sigval == SIGTERM) {
10068334SJose.Borrego@Sun.COM 		smbd.s_shutting_down = B_TRUE;
10078334SJose.Borrego@Sun.COM 		(void) pthread_cond_signal(&refresh_cond);
10088334SJose.Borrego@Sun.COM 	}
10095331Samw }
10105331Samw 
10115331Samw /*
10125331Samw  * Set up configuration options and parse the command line.
10135331Samw  * This function will determine if we will run as a daemon
10145331Samw  * or in the foreground.
10155331Samw  *
10165331Samw  * Failure to find a uid or gid results in using the default (0).
10175331Samw  */
10185331Samw static int
10195331Samw smbd_setup_options(int argc, char *argv[])
10205331Samw {
10215331Samw 	struct passwd *pwd;
10225331Samw 	struct group *grp;
10235331Samw 	int c;
10245331Samw 
10255331Samw 	if ((pwd = getpwnam("root")) != NULL)
10265331Samw 		smbd.s_uid = pwd->pw_uid;
10275331Samw 
10285331Samw 	if ((grp = getgrnam("sys")) != NULL)
10295331Samw 		smbd.s_gid = grp->gr_gid;
10305331Samw 
10315772Sas200622 	smbd.s_fg = smb_config_get_fg_flag();
10325331Samw 
10335331Samw 	while ((c = getopt(argc, argv, ":f")) != -1) {
10345331Samw 		switch (c) {
10355331Samw 		case 'f':
10365331Samw 			smbd.s_fg = 1;
10375331Samw 			break;
10385331Samw 
10395331Samw 		case ':':
10405331Samw 		case '?':
10415331Samw 		default:
10425331Samw 			smbd_usage(stderr);
10435331Samw 			return (-1);
10445331Samw 		}
10455331Samw 	}
10465331Samw 
10475331Samw 	return (0);
10485331Samw }
10495331Samw 
10505331Samw static void
10515331Samw smbd_usage(FILE *fp)
10525331Samw {
10535331Samw 	static char *help[] = {
10545331Samw 		"-f  run program in foreground"
10555331Samw 	};
10565331Samw 
10575331Samw 	int i;
10585331Samw 
10595331Samw 	(void) fprintf(fp, "Usage: %s [-f]\n", smbd.s_pname);
10605331Samw 
10615331Samw 	for (i = 0; i < sizeof (help)/sizeof (help[0]); ++i)
10625331Samw 		(void) fprintf(fp, "    %s\n", help[i]);
10635331Samw }
10645331Samw 
10655331Samw static void
10665331Samw smbd_report(const char *fmt, ...)
10675331Samw {
10685331Samw 	char buf[128];
10695331Samw 	va_list ap;
10705331Samw 
10715331Samw 	if (fmt == NULL)
10725331Samw 		return;
10735331Samw 
10745331Samw 	va_start(ap, fmt);
10755331Samw 	(void) vsnprintf(buf, 128, fmt, ap);
10765331Samw 	va_end(ap);
10775331Samw 
10785331Samw 	(void) fprintf(stderr, "smbd: %s\n", buf);
10795331Samw }
10805331Samw 
10819046SJose.Borrego@Sun.COM static int
10829046SJose.Borrego@Sun.COM smbd_start_listeners(void)
10839046SJose.Borrego@Sun.COM {
10849046SJose.Borrego@Sun.COM 	int		rc1;
10859046SJose.Borrego@Sun.COM 	int		rc2;
10869046SJose.Borrego@Sun.COM 	pthread_attr_t	tattr;
10879046SJose.Borrego@Sun.COM 
10889046SJose.Borrego@Sun.COM 	(void) pthread_attr_init(&tattr);
10899046SJose.Borrego@Sun.COM 
10909046SJose.Borrego@Sun.COM 	if (!smbd.s_nbt_listener_running) {
10919046SJose.Borrego@Sun.COM 		rc1 = pthread_create(&smbd.s_nbt_listener_id, &tattr,
10929046SJose.Borrego@Sun.COM 		    smbd_nbt_listener, NULL);
10939046SJose.Borrego@Sun.COM 		if (rc1 != 0)
10949046SJose.Borrego@Sun.COM 			smbd_report("unable to start NBT service");
10959046SJose.Borrego@Sun.COM 		else
10969046SJose.Borrego@Sun.COM 			smbd.s_nbt_listener_running = B_TRUE;
10979046SJose.Borrego@Sun.COM 	}
10989046SJose.Borrego@Sun.COM 
10999046SJose.Borrego@Sun.COM 	if (!smbd.s_tcp_listener_running) {
11009046SJose.Borrego@Sun.COM 		rc2 = pthread_create(&smbd.s_tcp_listener_id, &tattr,
11019046SJose.Borrego@Sun.COM 		    smbd_tcp_listener, NULL);
11029046SJose.Borrego@Sun.COM 		if (rc2 != 0)
11039046SJose.Borrego@Sun.COM 			smbd_report("unable to start TCP service");
11049046SJose.Borrego@Sun.COM 		else
11059046SJose.Borrego@Sun.COM 			smbd.s_tcp_listener_running = B_TRUE;
11069046SJose.Borrego@Sun.COM 	}
11079046SJose.Borrego@Sun.COM 
11089046SJose.Borrego@Sun.COM 	(void) pthread_attr_destroy(&tattr);
11099046SJose.Borrego@Sun.COM 
11109046SJose.Borrego@Sun.COM 	if (rc1 != 0)
11119046SJose.Borrego@Sun.COM 		return (rc1);
11129046SJose.Borrego@Sun.COM 	return (rc2);
11139046SJose.Borrego@Sun.COM }
11149046SJose.Borrego@Sun.COM 
111511963SAfshin.Ardakani@Sun.COM /*
111611963SAfshin.Ardakani@Sun.COM  * Stop the listener threads.  In an attempt to ensure that the listener
111711963SAfshin.Ardakani@Sun.COM  * threads get the signal, we use the timed wait loop to harass the
111811963SAfshin.Ardakani@Sun.COM  * threads into terminating.  Then, if they are still running, we make
111911963SAfshin.Ardakani@Sun.COM  * one final attempt to deliver the signal before calling thread join
112011963SAfshin.Ardakani@Sun.COM  * to wait for them.  Note: if these threads don't terminate, smbd will
112111963SAfshin.Ardakani@Sun.COM  * hang here and SMF will probably end up killing the contract.
112211963SAfshin.Ardakani@Sun.COM  */
11239046SJose.Borrego@Sun.COM static void
11249046SJose.Borrego@Sun.COM smbd_stop_listeners(void)
11259046SJose.Borrego@Sun.COM {
112611963SAfshin.Ardakani@Sun.COM 	void		*status;
112711963SAfshin.Ardakani@Sun.COM 	timestruc_t	delay;
112811963SAfshin.Ardakani@Sun.COM 	int		rc = 0;
112911963SAfshin.Ardakani@Sun.COM 
113011963SAfshin.Ardakani@Sun.COM 	(void) mutex_lock(&listener_mutex);
113111963SAfshin.Ardakani@Sun.COM 
113211963SAfshin.Ardakani@Sun.COM 	while ((smbd.s_nbt_listener_running || smbd.s_tcp_listener_running) &&
113311963SAfshin.Ardakani@Sun.COM 	    (rc != ETIME)) {
113411963SAfshin.Ardakani@Sun.COM 		if (smbd.s_nbt_listener_running)
113511963SAfshin.Ardakani@Sun.COM 			(void) pthread_kill(smbd.s_nbt_listener_id, SIGTERM);
113611963SAfshin.Ardakani@Sun.COM 
113711963SAfshin.Ardakani@Sun.COM 		if (smbd.s_tcp_listener_running)
113811963SAfshin.Ardakani@Sun.COM 			(void) pthread_kill(smbd.s_tcp_listener_id, SIGTERM);
113911963SAfshin.Ardakani@Sun.COM 
114011963SAfshin.Ardakani@Sun.COM 		delay.tv_sec = 3;
114111963SAfshin.Ardakani@Sun.COM 		delay.tv_nsec = 0;
114211963SAfshin.Ardakani@Sun.COM 		rc = cond_reltimedwait(&listener_cv, &listener_mutex, &delay);
114311963SAfshin.Ardakani@Sun.COM 	}
114411963SAfshin.Ardakani@Sun.COM 
114511963SAfshin.Ardakani@Sun.COM 	(void) mutex_unlock(&listener_mutex);
11469046SJose.Borrego@Sun.COM 
11479046SJose.Borrego@Sun.COM 	if (smbd.s_nbt_listener_running) {
114811963SAfshin.Ardakani@Sun.COM 		syslog(LOG_WARNING, "NBT listener still running");
11499046SJose.Borrego@Sun.COM 		(void) pthread_kill(smbd.s_nbt_listener_id, SIGTERM);
11509046SJose.Borrego@Sun.COM 		(void) pthread_join(smbd.s_nbt_listener_id, &status);
11519046SJose.Borrego@Sun.COM 		smbd.s_nbt_listener_running = B_FALSE;
11529046SJose.Borrego@Sun.COM 	}
11539046SJose.Borrego@Sun.COM 
11549046SJose.Borrego@Sun.COM 	if (smbd.s_tcp_listener_running) {
115511963SAfshin.Ardakani@Sun.COM 		syslog(LOG_WARNING, "TCP listener still running");
11569046SJose.Borrego@Sun.COM 		(void) pthread_kill(smbd.s_tcp_listener_id, SIGTERM);
11579046SJose.Borrego@Sun.COM 		(void) pthread_join(smbd.s_tcp_listener_id, &status);
11589046SJose.Borrego@Sun.COM 		smbd.s_tcp_listener_running = B_FALSE;
11599046SJose.Borrego@Sun.COM 	}
11609046SJose.Borrego@Sun.COM }
11619046SJose.Borrego@Sun.COM 
11625331Samw /*
11639832Samw@Sun.COM  * Perform fatal error exit.
11649832Samw@Sun.COM  */
11659832Samw@Sun.COM static void
11669832Samw@Sun.COM smbd_fatal_error(const char *msg)
11679832Samw@Sun.COM {
11689832Samw@Sun.COM 	if (msg == NULL)
11699832Samw@Sun.COM 		msg = "Fatal error";
11709832Samw@Sun.COM 
11719832Samw@Sun.COM 	smbd_report("%s", msg);
11729832Samw@Sun.COM 	smbd.s_fatal_error = B_TRUE;
11739832Samw@Sun.COM 	(void) kill(smbd.s_pid, SIGTERM);
11749832Samw@Sun.COM }
11759832Samw@Sun.COM 
11769832Samw@Sun.COM /*ARGSUSED*/
11779832Samw@Sun.COM static void *
11789832Samw@Sun.COM smbd_nbt_receiver(void *arg)
11799832Samw@Sun.COM {
11809832Samw@Sun.COM 	(void) smb_kmod_nbtreceive();
11819832Samw@Sun.COM 	return (NULL);
11829832Samw@Sun.COM }
11839832Samw@Sun.COM 
11849832Samw@Sun.COM /*ARGSUSED*/
11859832Samw@Sun.COM static void *
11869832Samw@Sun.COM smbd_nbt_listener(void *arg)
11879832Samw@Sun.COM {
11889832Samw@Sun.COM 	pthread_attr_t	tattr;
11899832Samw@Sun.COM 	sigset_t	set;
11909832Samw@Sun.COM 	sigset_t	oset;
11919832Samw@Sun.COM 	pthread_t	tid;
11929832Samw@Sun.COM 	int		error = 0;
11939832Samw@Sun.COM 
11949832Samw@Sun.COM 	(void) sigfillset(&set);
11959832Samw@Sun.COM 	(void) sigdelset(&set, SIGTERM);
11969832Samw@Sun.COM 	(void) sigdelset(&set, SIGINT);
11979832Samw@Sun.COM 	(void) pthread_sigmask(SIG_SETMASK, &set, &oset);
11989832Samw@Sun.COM 	(void) pthread_attr_init(&tattr);
11999832Samw@Sun.COM 	(void) pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
12009832Samw@Sun.COM 
12019832Samw@Sun.COM 	while (smb_kmod_nbtlisten(error) == 0)
12029832Samw@Sun.COM 		error = pthread_create(&tid, &tattr, smbd_nbt_receiver, NULL);
12039832Samw@Sun.COM 
12049832Samw@Sun.COM 	(void) pthread_attr_destroy(&tattr);
12059832Samw@Sun.COM 
12069832Samw@Sun.COM 	if (!smbd.s_shutting_down)
12079832Samw@Sun.COM 		smbd_fatal_error("NBT listener thread terminated unexpectedly");
12089832Samw@Sun.COM 
120911963SAfshin.Ardakani@Sun.COM 	(void) mutex_lock(&listener_mutex);
121011963SAfshin.Ardakani@Sun.COM 	smbd.s_nbt_listener_running = B_FALSE;
121111963SAfshin.Ardakani@Sun.COM 	(void) cond_broadcast(&listener_cv);
121211963SAfshin.Ardakani@Sun.COM 	(void) mutex_unlock(&listener_mutex);
12139832Samw@Sun.COM 	return (NULL);
12149832Samw@Sun.COM }
12159832Samw@Sun.COM 
12169832Samw@Sun.COM /*ARGSUSED*/
12179832Samw@Sun.COM static void *
12189832Samw@Sun.COM smbd_tcp_receiver(void *arg)
12199832Samw@Sun.COM {
12209832Samw@Sun.COM 	(void) smb_kmod_tcpreceive();
12219832Samw@Sun.COM 	return (NULL);
12229832Samw@Sun.COM }
12239832Samw@Sun.COM 
12249832Samw@Sun.COM /*ARGSUSED*/
12259832Samw@Sun.COM static void *
12269832Samw@Sun.COM smbd_tcp_listener(void *arg)
12279832Samw@Sun.COM {
12289832Samw@Sun.COM 	pthread_attr_t	tattr;
12299832Samw@Sun.COM 	sigset_t	set;
12309832Samw@Sun.COM 	sigset_t	oset;
12319832Samw@Sun.COM 	pthread_t	tid;
12329832Samw@Sun.COM 	int		error = 0;
12339832Samw@Sun.COM 
12349832Samw@Sun.COM 	(void) sigfillset(&set);
12359832Samw@Sun.COM 	(void) sigdelset(&set, SIGTERM);
12369832Samw@Sun.COM 	(void) sigdelset(&set, SIGINT);
12379832Samw@Sun.COM 	(void) pthread_sigmask(SIG_SETMASK, &set, &oset);
12389832Samw@Sun.COM 	(void) pthread_attr_init(&tattr);
12399832Samw@Sun.COM 	(void) pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
12409832Samw@Sun.COM 
12419832Samw@Sun.COM 	while (smb_kmod_tcplisten(error) == 0)
12429832Samw@Sun.COM 		error = pthread_create(&tid, &tattr, smbd_tcp_receiver, NULL);
12439832Samw@Sun.COM 
12449832Samw@Sun.COM 	(void) pthread_attr_destroy(&tattr);
12459832Samw@Sun.COM 
12469832Samw@Sun.COM 	if (!smbd.s_shutting_down)
12479832Samw@Sun.COM 		smbd_fatal_error("TCP listener thread terminated unexpectedly");
12489832Samw@Sun.COM 
124911963SAfshin.Ardakani@Sun.COM 	(void) mutex_lock(&listener_mutex);
125011963SAfshin.Ardakani@Sun.COM 	smbd.s_tcp_listener_running = B_FALSE;
125111963SAfshin.Ardakani@Sun.COM 	(void) cond_broadcast(&listener_cv);
125211963SAfshin.Ardakani@Sun.COM 	(void) mutex_unlock(&listener_mutex);
12539832Samw@Sun.COM 	return (NULL);
12549832Samw@Sun.COM }
12559832Samw@Sun.COM 
12569832Samw@Sun.COM /*
12575331Samw  * Enable libumem debugging by default on DEBUG builds.
12585331Samw  */
12595331Samw #ifdef DEBUG
12605331Samw const char *
12615331Samw _umem_debug_init(void)
12625331Samw {
12635331Samw 	return ("default,verbose"); /* $UMEM_DEBUG setting */
12645331Samw }
12655331Samw 
12665331Samw const char *
12675331Samw _umem_logging_init(void)
12685331Samw {
12695331Samw 	return ("fail,contents"); /* $UMEM_LOGGING setting */
12705331Samw }
12715331Samw #endif
1272