server.c

来自「samba-3.0.22.tar.gz 编译smb服务器的源码」· C语言 代码 · 共 983 行 · 第 1/2 页

C
983
字号
		if (!pcap_printername_ok(pname)) {			DEBUG(3, ("removing stale printer %s\n", pname));			if (is_printer_published(NULL, snum, NULL))				nt_printer_publish(NULL, snum, SPOOL_DS_UNPUBLISH);			del_a_printer(pname);			lp_killservice(snum);		}	}	load_printers();}/**************************************************************************** Reload the services file.**************************************************************************/BOOL reload_services(BOOL test){	BOOL ret;		if (lp_loaded()) {		pstring fname;		pstrcpy(fname,lp_configfile());		if (file_exist(fname, NULL) &&		    !strcsequal(fname, dyn_CONFIGFILE)) {			pstrcpy(dyn_CONFIGFILE, fname);			test = False;		}	}	reopen_logs();	if (test && !lp_file_list_changed())		return(True);	lp_killunused(conn_snum_used);	ret = lp_load(dyn_CONFIGFILE, False, False, True);	reload_printers();	/* perhaps the config filename is now set */	if (!test)		reload_services(True);	reopen_logs();	load_interfaces();	if (smbd_server_fd() != -1) {      		set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");		set_socket_options(smbd_server_fd(), user_socket_options);	}	mangle_reset_cache();	reset_stat_cache();	/* this forces service parameters to be flushed */	set_current_service(NULL,0,True);	return(ret);}#if DUMP_CORE/*******************************************************************prepare to dump a core file - carefully!********************************************************************/static BOOL dump_core(void){	char *p;	pstring dname;		pstrcpy(dname,lp_logfile());	if ((p=strrchr_m(dname,'/'))) *p=0;	pstrcat(dname,"/corefiles");	mkdir(dname,0700);	sys_chown(dname,getuid(),getgid());	chmod(dname,0700);	if (chdir(dname)) return(False);	umask(~(0700));#ifdef HAVE_GETRLIMIT#ifdef RLIMIT_CORE	{		struct rlimit rlp;		getrlimit(RLIMIT_CORE, &rlp);		rlp.rlim_cur = MAX(4*1024*1024,rlp.rlim_cur);		setrlimit(RLIMIT_CORE, &rlp);		getrlimit(RLIMIT_CORE, &rlp);		DEBUG(3,("Core limits now %d %d\n",			 (int)rlp.rlim_cur,(int)rlp.rlim_max));	}#endif#endif	DEBUG(0,("Dumping core in %s\n", dname));	/* Ensure we don't have a signal handler for abort. */#ifdef SIGABRT	CatchSignal(SIGABRT,SIGNAL_CAST SIG_DFL);#endif	abort();	return(True);}#endif/**************************************************************************** Exit the server.****************************************************************************/void exit_server(const char *reason){	static int firsttime=1;	if (!firsttime)		exit(0);	firsttime = 0;	change_to_root_user();	DEBUG(2,("Closing connections\n"));	if (negprot_global_auth_context) {		(negprot_global_auth_context->free)(&negprot_global_auth_context);	}	conn_close_all();	invalidate_all_vuids();	print_notify_send_messages(3); /* 3 second timeout. */	/* delete our entry in the connections database. */	yield_connection(NULL,"");	respond_to_all_remaining_local_messages();	decrement_smbd_process_count();#ifdef WITH_DFS	if (dcelogin_atmost_once) {		dfs_unlogin();	}#endif	if (!reason) {   		int oldlevel = DEBUGLEVEL;		char *last_inbuf = get_InBuffer();		DEBUGLEVEL = 10;		DEBUG(0,("Last message was %s\n",smb_fn_name(last_message)));		if (last_inbuf)			show_msg(last_inbuf);		DEBUGLEVEL = oldlevel;		DEBUG(0,("===============================================================\n"));#if DUMP_CORE		if (dump_core()) return;#endif	}    	locking_end();	printing_end();	DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));	exit(0);}/**************************************************************************** Initialise connect, service and file structs.****************************************************************************/static BOOL init_structs(void ){	/*	 * Set the machine NETBIOS name if not already	 * set from the config file.	 */	if (!init_names())		return False;	conn_init();	file_init();	/* for RPC pipes */	init_rpc_pipe_hnd();	init_dptrs();	secrets_init();	return True;}/**************************************************************************** main program.****************************************************************************//* Declare prototype for build_options() to avoid having to run it through   mkproto.h.  Mixing $(builddir) and $(srcdir) source files in the current   prototype generation system is too complicated. */void build_options(BOOL screen); int main(int argc,const char *argv[]){	/* shall I run as a daemon */	static BOOL is_daemon = False;	static BOOL interactive = False;	static BOOL Fork = True;	static BOOL log_stdout = False;	static char *ports = NULL;	int opt;	poptContext pc;	struct poptOption long_options[] = {		POPT_AUTOHELP	{"daemon", 'D', POPT_ARG_VAL, &is_daemon, True, "Become a daemon (default)" },	{"interactive", 'i', POPT_ARG_VAL, &interactive, True, "Run interactive (not a daemon)"},	{"foreground", 'F', POPT_ARG_VAL, &Fork, False, "Run daemon in foreground (for daemontools & etc)" },	{"log-stdout", 'S', POPT_ARG_VAL, &log_stdout, True, "Log to stdout" },	{"build-options", 'b', POPT_ARG_NONE, NULL, 'b', "Print build options" },	{"port", 'p', POPT_ARG_STRING, &ports, 0, "Listen on the specified ports"},	POPT_COMMON_SAMBA	{ NULL }	};	load_case_tables();#ifdef HAVE_SET_AUTH_PARAMETERS	set_auth_parameters(argc,argv);#endif	pc = poptGetContext("smbd", argc, argv, long_options, 0);		while((opt = poptGetNextOpt(pc)) != -1) {		switch (opt)  {		case 'b':			build_options(True); /* Display output to screen as well as debug */ 			exit(0);			break;		}	}	poptFreeContext(pc);#ifdef HAVE_SETLUID	/* needed for SecureWare on SCO */	setluid(0);#endif	sec_init();	set_remote_machine_name("smbd", False);	if (interactive) {		Fork = False;		log_stdout = True;	}	if (interactive && (DEBUGLEVEL >= 9)) {		talloc_enable_leak_report();	}	if (log_stdout && Fork) {		DEBUG(0,("ERROR: Can't log to stdout (-S) unless daemon is in foreground (-F) or interactive (-i)\n"));		exit(1);	}	setup_logging(argv[0],log_stdout);	/* we want to re-seed early to prevent time delays causing           client problems at a later date. (tridge) */	generate_random_buffer(NULL, 0);	/* make absolutely sure we run as root - to handle cases where people	   are crazy enough to have it setuid */	gain_root_privilege();	gain_root_group_privilege();	fault_setup((void (*)(void *))exit_server);	CatchSignal(SIGTERM , SIGNAL_CAST sig_term);	CatchSignal(SIGHUP,SIGNAL_CAST sig_hup);		/* we are never interested in SIGPIPE */	BlockSignals(True,SIGPIPE);#if defined(SIGFPE)	/* we are never interested in SIGFPE */	BlockSignals(True,SIGFPE);#endif#if defined(SIGUSR2)	/* We are no longer interested in USR2 */	BlockSignals(True,SIGUSR2);#endif	/* POSIX demands that signals are inherited. If the invoking process has	 * these signals masked, we will have problems, as we won't recieve them. */	BlockSignals(False, SIGHUP);	BlockSignals(False, SIGUSR1);	BlockSignals(False, SIGTERM);	/* we want total control over the permissions on created files,	   so set our umask to 0 */	umask(0);	init_sec_ctx();	reopen_logs();	DEBUG(0,( "smbd version %s started.\n", SAMBA_VERSION_STRING));	DEBUGADD( 0, ( "%s\n", COPYRIGHT_STARTUP_MESSAGE ) );	DEBUG(2,("uid=%d gid=%d euid=%d egid=%d\n",		 (int)getuid(),(int)getgid(),(int)geteuid(),(int)getegid()));	/* Output the build options to the debug log */ 	build_options(False);	if (sizeof(uint16) < 2 || sizeof(uint32) < 4) {		DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));		exit(1);	}	/*	 * Do this before reload_services.	 */	if (!reload_services(False))		return(-1);		init_structs();	if (!init_guest_info()) {		DEBUG(0,("ERROR: failed to setup guest info.\n"));		return -1;	}#ifdef WITH_PROFILE	if (!profile_setup(False)) {		DEBUG(0,("ERROR: failed to setup profiling\n"));		return -1;	}#endif	DEBUG(3,( "loaded services\n"));	if (!is_daemon && !is_a_socket(0)) {		if (!interactive)			DEBUG(0,("standard input is not a socket, assuming -D option\n"));		/*		 * Setting is_daemon here prevents us from eventually calling		 * the open_sockets_inetd()		 */		is_daemon = True;	}	if (is_daemon && !interactive) {		DEBUG( 3, ( "Becoming a daemon.\n" ) );		become_daemon(Fork);	}#if HAVE_SETPGID	/*	 * If we're interactive we want to set our own process group for	 * signal management.	 */	if (interactive)		setpgid( (pid_t)0, (pid_t)0);#endif	if (!directory_exist(lp_lockdir(), NULL))		mkdir(lp_lockdir(), 0755);	if (is_daemon)		pidfile_create("smbd");	/* Setup all the TDB's - including CLEAR_IF_FIRST tdb's. */	if (!message_init())		exit(1);	if (!session_init())		exit(1);	if (conn_tdb_ctx() == NULL)		exit(1);	if (!locking_init(0))		exit(1);	if (!share_info_db_init())		exit(1);	namecache_enable();	if (!init_registry())		exit(1);#if 0	if (!init_svcctl_db())                exit(1);#endif	if (!print_backend_init())		exit(1);	/* Setup the main smbd so that we can get messages. */	/* don't worry about general printing messages here */	claim_connection(NULL,"",0,True,FLAG_MSG_GENERAL|FLAG_MSG_SMBD);	/* only start the background queue daemon if we are 	   running as a daemon -- bad things will happen if	   smbd is launched via inetd and we fork a copy of 	   ourselves here */	if ( is_daemon && !interactive )		start_background_queue(); 	if (!open_sockets_smbd(is_daemon, interactive, ports))		exit(1);	/*	 * everything after this point is run after the fork()	 */ #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)	/* On Linux we lose the ability to dump core when we change our user	 * ID. We know how to dump core safely, so let's make sure we have our	 * dumpable flag set.	 */	prctl(PR_SET_DUMPABLE, 1);#endif	/* Initialise the password backed before the global_sam_sid	   to ensure that we fetch from ldap before we make a domain sid up */	if(!initialize_password_db(False))		exit(1);	if(!get_global_sam_sid()) {		DEBUG(0,("ERROR: Samba cannot create a SAM SID.\n"));		exit(1);	}	static_init_rpc;	init_modules();	/* possibly reload the services file. */	reload_services(True);	if (!init_account_policy()) {		DEBUG(0,("Could not open account policy tdb.\n"));		exit(1);	}	if (*lp_rootdir()) {		if (sys_chroot(lp_rootdir()) == 0)			DEBUG(2,("Changed root to %s\n", lp_rootdir()));	}	/* Setup oplocks */	if (!init_oplocks())		exit(1);		/* Setup change notify */	if (!init_change_notify())		exit(1);	/* Setup aio signal handler. */	initialize_async_io_handler();	/* re-initialise the timezone */	TimeInit();	/* register our message handlers */	message_register(MSG_SMB_FORCE_TDIS, msg_force_tdis);	smbd_process();		namecache_shutdown();	exit_server("normal exit");	return(0);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?