r3708: BUG 1838: patch from Gavrie Philipson <gavrie@disksites.com> to remove stale...
[ira/wip.git] / source / smbd / server.c
index edc7b57ba3988f32c84fc2fe2dc3648af00ae488..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);
 }
@@ -634,6 +648,12 @@ static BOOL init_structs(void )
  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 */
@@ -699,7 +719,7 @@ static BOOL init_structs(void )
 
        /* 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 */
@@ -738,8 +758,8 @@ static BOOL init_structs(void )
 
        reopen_logs();
 
-       DEBUG(0,( "smbd version %s started.\n", VERSION));
-       DEBUGADD(0,( "Copyright Andrew Tridgell and the Samba Team 1992-2003\n"));
+       DEBUG(0,( "smbd version %s started.\n", SAMBA_VERSION_STRING));
+       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()));
@@ -761,6 +781,9 @@ static BOOL init_structs(void )
 
        init_structs();
 
+       if (!init_guest_info())
+               return -1;
+
 #ifdef WITH_PROFILE
        if (!profile_setup(False)) {
                DEBUG(0,("ERROR: failed to setup profiling\n"));
@@ -802,21 +825,42 @@ static BOOL init_structs(void )
        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 (!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);
 
-       /* 
-          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(); 
-       */
+       /* 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);
@@ -825,40 +869,24 @@ static BOOL init_structs(void )
         * everything after this point is run after the fork()
         */ 
 
-       namecache_enable();
-
-       if (!locking_init(0))
-               exit(1);
-
-       if (!share_info_db_init())
-               exit(1);
-
-       if (!init_registry())
-               exit(1);
+       /* 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 (!idmap_init())
-               exit(1);
-
-       if (!idmap_init_wellknown_sids())
+       if(!get_global_sam_sid()) {
+               DEBUG(0,("ERROR: Samba cannot create a SAM SID.\n"));
                exit(1);
+       }
 
        static_init_rpc;
 
        init_modules();
 
-       uni_group_cache_init(); /* Non-critical */
-       
        /* possibly reload the services file. */
        reload_services(True);
 
-       if(!get_global_sam_sid()) {
-               DEBUG(0,("ERROR: Samba cannot create a SAM SID.\n"));
-               exit(1);
-       }
-
        if (!init_account_policy()) {
                DEBUG(0,("Could not open account policy tdb.\n"));
                exit(1);
@@ -885,8 +913,16 @@ static BOOL init_structs(void )
 
        smbd_process();
        
-       uni_group_cache_shutdown();
        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);
 }