r3708: BUG 1838: patch from Gavrie Philipson <gavrie@disksites.com> to remove stale...
[ira/wip.git] / source / smbd / server.c
index 8b890549ea34595b122e727fdedcd1f88f1f83e3..e40d1f8bc7fb753b967d2b3934adf0e875eeac71 100644 (file)
@@ -22,7 +22,7 @@
 
 #include "includes.h"
 
-int am_parent = 1;
+static int am_parent = 1;
 
 /* the last message the was processed */
 int last_message = -1;
@@ -145,7 +145,7 @@ static void msg_exit_server(int msg_type, pid_t src, void *buf, size_t len)
  Have we reached the process limit ?
 ****************************************************************************/
 
-BOOL allowable_number_of_smbd_processes(void)
+static BOOL allowable_number_of_smbd_processes(void)
 {
        int max_processes = lp_max_smbd_processes();
 
@@ -249,8 +249,11 @@ static BOOL open_sockets_smbd(BOOL is_daemon, BOOL interactive, const char *smb_
                                /* ready to listen */
                                set_socket_options(s,"SO_KEEPALIVE"); 
                                set_socket_options(s,user_socket_options);
-      
-                               if (listen(s, 5) == -1) {
+     
+                               /* Set server socket to non-blocking for the accept. */
+                               set_blocking(s,False); 
+                               if (listen(s, SMBD_LISTEN_BACKLOG) == -1) {
                                        DEBUG(0,("listen: %s\n",strerror(errno)));
                                        close(s);
                                        return False;
@@ -286,7 +289,10 @@ static BOOL open_sockets_smbd(BOOL is_daemon, BOOL interactive, const char *smb_
                        set_socket_options(s,"SO_KEEPALIVE"); 
                        set_socket_options(s,user_socket_options);
                        
-                       if (listen(s, 5) == -1) {
+                       /* Set server socket to non-blocking for the accept. */
+                       set_blocking(s,False); 
+                       if (listen(s, SMBD_LISTEN_BACKLOG) == -1) {
                                DEBUG(0,("open_sockets_smbd: listen: %s\n",
                                         strerror(errno)));
                                close(s);
@@ -378,6 +384,9 @@ static BOOL open_sockets_smbd(BOOL is_daemon, BOOL interactive, const char *smb_
                                continue;
                        }
 
+                       /* Ensure child is set to blocking mode */
+                       set_blocking(smbd_server_fd(),True);
+
                        if (smbd_server_fd() != -1 && interactive)
                                return True;
                        
@@ -398,17 +407,18 @@ static BOOL open_sockets_smbd(BOOL is_daemon, BOOL interactive, const char *smb_
                                
                                /* this is needed so that we get decent entries
                                   in smbstatus for port 445 connects */
-                               set_remote_machine_name(get_socket_addr(smbd_server_fd()), False);
+                               set_remote_machine_name(get_peer_addr(smbd_server_fd()), False);
                                
-                               /* Reset global variables in util.c so
-                                  that client substitutions will be
-                                  done correctly in the process.  */
-                               reset_globals_after_fork();
+                               /* Reset the state of the random
+                                * number generation system, so
+                                * children do not get the same random
+                                * numbers as each other */
 
-                               /* tdb needs special fork handling */
+                               set_need_random_reseed();
+                               /* tdb needs special fork handling - remove CLEAR_IF_FIRST flags */
                                if (tdb_reopen_all() == -1) {
                                        DEBUG(0,("tdb_reopen_all failed.\n"));
-                                       return False;
+                                       smb_panic("tdb_reopen_all failed.");
                                }
 
                                return True; 
@@ -472,9 +482,10 @@ BOOL reload_services(BOOL test)
                return(True);
 
        lp_killunused(conn_snum_used);
-       
+
        ret = lp_load(dyn_CONFIGFILE, False, False, True);
 
+       remove_stale_printers();
        load_printers();
 
        /* perhaps the config filename is now set */
@@ -485,22 +496,21 @@ BOOL reload_services(BOOL test)
 
        load_interfaces();
 
-       {
-               if (smbd_server_fd() != -1) {      
-                       set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
-                       set_socket_options(smbd_server_fd(), user_socket_options);
-               }
+       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,True);
+       set_current_service(NULL,0,True);
 
        return(ret);
 }
 
+
 #if DUMP_CORE
 /*******************************************************************
 prepare to dump a core file - carefully!
@@ -535,6 +545,10 @@ static BOOL dump_core(void)
 
 
        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);
 }
@@ -705,7 +719,7 @@ void build_options(BOOL screen);
 
        /* we want to re-seed early to prevent time delays causing
            client problems at a later date. (tridge) */
-       generate_random_buffer(NULL, 0, False);
+       generate_random_buffer(NULL, 0);
 
        /* make absolutely sure we run as root - to handle cases where people
           are crazy enough to have it setuid */
@@ -745,7 +759,7 @@ void build_options(BOOL screen);
        reopen_logs();
 
        DEBUG(0,( "smbd version %s started.\n", SAMBA_VERSION_STRING));
-       DEBUGADD(0,( "Copyright Andrew Tridgell and the Samba Team 1992-2003\n"));
+       DEBUGADD(0,( "Copyright Andrew Tridgell and the Samba Team 1992-2004\n"));
 
        DEBUG(2,("uid=%d gid=%d euid=%d egid=%d\n",
                 (int)getuid(),(int)getgid(),(int)geteuid(),(int)getegid()));
@@ -767,6 +781,9 @@ void build_options(BOOL screen);
 
        init_structs();
 
+       if (!init_guest_info())
+               return -1;
+
 #ifdef WITH_PROFILE
        if (!profile_setup(False)) {
                DEBUG(0,("ERROR: failed to setup profiling\n"));
@@ -808,40 +825,50 @@ void build_options(BOOL screen);
        if (is_daemon)
                pidfile_create("smbd");
 
+       /* Setup all the TDB's - including CLEAR_IF_FIRST tdb's. */
        if (!message_init())
                exit(1);
 
-       if (!print_backend_init())
+       if (!session_init())
                exit(1);
 
-       /* Setup the main smbd so that we can get messages. */
-       claim_connection(NULL,"",0,True,FLAG_MSG_GENERAL|FLAG_MSG_SMBD);
-
-       /* 
-          DO NOT ENABLE THIS TILL YOU COPE WITH KILLING THESE TASKS AND INETD
-          THIS *killed* LOTS OF BUILD FARM MACHINES. IT CREATED HUNDREDS OF 
-          smbd PROCESSES THAT NEVER DIE
-          start_background_queue(); 
-       */
+       if (conn_tdb_ctx() == NULL)
+               exit(1);
 
-       if (!open_sockets_smbd(is_daemon, interactive, ports))
+       if (!locking_init(0))
                exit(1);
 
-       /*
-        * everything after this point is run after the fork()
-        */ 
+       if (!share_info_db_init())
+               exit(1);
 
        namecache_enable();
 
-       if (!locking_init(0))
+       if (!init_registry())
                exit(1);
 
-       if (!share_info_db_init())
+       if (!print_backend_init())
                exit(1);
 
-       if (!init_registry())
+       /* 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 )
+               start_background_queue(); 
+
+       if (!open_sockets_smbd(is_daemon, interactive, ports))
                exit(1);
 
+       /*
+        * everything after this point is run after the fork()
+        */ 
+
        /* Initialise the password backed before the global_sam_sid
           to ensure that we fetch from ldap before we make a domain sid up */
 
@@ -887,6 +914,15 @@ void build_options(BOOL screen);
        smbd_process();
        
        namecache_shutdown();
+
+       if (interactive) {
+               TALLOC_CTX *mem_ctx = talloc_init("end_description");
+               char *description = talloc_describe_all(mem_ctx);
+
+               DEBUG(3, ("tallocs left:\n%s\n", description));
+               talloc_destroy(mem_ctx);
+       }
+
        exit_server("normal exit");
        return(0);
 }