Cleanups!
[kai/samba.git] / source3 / smbd / server.c
index 4d4c612b521f5a92c7b2909b5b3df5720e2edd9c..5f8f7044a6a999fe6a7dc82a17e7517748250e12 100644 (file)
@@ -1,5 +1,5 @@
 /* 
-   Unix SMB/Netbios implementation.
+   Unix SMB/CIFS implementation.
    Main SMB server routines
    Copyright (C) Andrew Tridgell               1992-1998
    Copyright (C) Martin Pool                   2002
@@ -53,33 +53,50 @@ int smbd_server_fd(void)
        return server_fd;
 }
 
-void smbd_set_server_fd(int fd)
+static void smbd_set_server_fd(int fd)
 {
        server_fd = fd;
        client_setfd(fd);
 }
 
 /****************************************************************************
-  when exiting, take the whole family
+ Terminate signal.
 ****************************************************************************/
-static void *dflt_sig(void)
+
+VOLATILE sig_atomic_t got_sig_term = 0;
+
+static void sig_term(void)
+{
+       got_sig_term = 1;
+       sys_select_signal();
+}
+
+/****************************************************************************
+ Catch a sighup.
+****************************************************************************/
+
+VOLATILE sig_atomic_t reload_after_sighup = 0;
+
+static void sig_hup(int sig)
 {
-       exit_server("caught signal");
-       return NULL;
+       reload_after_sighup = 1;
+       sys_select_signal();
 }
 
 /****************************************************************************
   Send a SIGTERM to our process group.
 *****************************************************************************/
+
 static void  killkids(void)
 {
        if(am_parent) kill(0,SIGTERM);
 }
 
 /****************************************************************************
 process a sam sync message - not sure whether to do this here or
-  somewhere else
Process a sam sync message - not sure whether to do this here or
+ somewhere else.
 ****************************************************************************/
+
 static void msg_sam_sync(int UNUSED(msg_type), pid_t UNUSED(pid),
                         void *UNUSED(buf), size_t UNUSED(len))
 {
@@ -87,9 +104,10 @@ static void msg_sam_sync(int UNUSED(msg_type), pid_t UNUSED(pid),
 }
 
 /****************************************************************************
 process a sam sync replicate message - not sure whether to do this here or
-  somewhere else
Process a sam sync replicate message - not sure whether to do this here or
+ somewhere else.
 ****************************************************************************/
+
 static void msg_sam_repl(int msg_type, pid_t pid, void *buf, size_t len)
 {
         uint32 low_serial;
@@ -104,8 +122,9 @@ static void msg_sam_repl(int msg_type, pid_t pid, void *buf, size_t len)
 }
 
 /****************************************************************************
-  open the socket communication
+ Open the socket communication - inetd.
 ****************************************************************************/
+
 static BOOL open_sockets_inetd(void)
 {
        /* Started from inetd. fd 0 is the socket. */
@@ -122,10 +141,16 @@ static BOOL open_sockets_inetd(void)
        return True;
 }
 
+static void msg_exit_server(int msg_type, pid_t src, void *buf, size_t len)
+{
+       exit_server("Got a SHUTDOWN message");
+}
+
 
 /****************************************************************************
-  open the socket communication
+ Open the socket communication.
 ****************************************************************************/
+
 static BOOL open_sockets(BOOL is_daemon,int port)
 {
        int num_interfaces = iface_count();
@@ -222,6 +247,7 @@ max can be %d\n",
 
         message_register(MSG_SMB_SAM_SYNC, msg_sam_sync);
         message_register(MSG_SMB_SAM_REPL, msg_sam_repl);
+        message_register(MSG_SHUTDOWN, msg_exit_server);
 
        /* now accept incoming connections - forking a new process
           for each incoming connection */
@@ -239,17 +265,19 @@ max can be %d\n",
                memcpy((char *)&lfds, (char *)&listen_set, 
                       sizeof(listen_set));
                
-               num = sys_select(FD_SETSIZE,&lfds,NULL);
+               num = sys_select(FD_SETSIZE,&lfds,NULL,NULL,NULL);
                
                if (num == -1 && errno == EINTR) {
-                       extern VOLATILE sig_atomic_t reload_after_sighup;
+                       if (got_sig_term) {
+                               exit_server("Caught TERM signal");
+                       }
 
                        /* check for sighup processing */
                        if (reload_after_sighup) {
                                change_to_root_user();
                                DEBUG(1,("Reloading services after SIGHUP\n"));
                                reload_services(False);
-                               reload_after_sighup = False;
+                               reload_after_sighup = 0;
                        }
 
                        continue;
@@ -347,8 +375,9 @@ max can be %d\n",
 }
 
 /****************************************************************************
-  reload the services file
-  **************************************************************************/
+ Reload the services file.
+**************************************************************************/
+
 BOOL reload_services(BOOL test)
 {
        BOOL ret;
@@ -389,7 +418,7 @@ BOOL reload_services(BOOL test)
                }
        }
 
-       reset_mangled_cache();
+       mangle_reset_cache();
        reset_stat_cache();
 
        /* this forces service parameters to be flushed */
@@ -398,26 +427,6 @@ BOOL reload_services(BOOL test)
        return(ret);
 }
 
-
-
-/****************************************************************************
- Catch a sighup.
-****************************************************************************/
-
-VOLATILE sig_atomic_t reload_after_sighup = False;
-
-static void sig_hup(int sig)
-{
-       BlockSignals(True,SIGHUP);
-       DEBUG(0,("Got SIGHUP\n"));
-
-       sys_select_signal();
-       reload_after_sighup = True;
-       BlockSignals(False,SIGHUP);
-}
-
-
-
 #if DUMP_CORE
 /*******************************************************************
 prepare to dump a core file - carefully!
@@ -472,8 +481,9 @@ static void decrement_smbd_process_count(void)
 }
 
 /****************************************************************************
-exit the server
+ Exit the server.
 ****************************************************************************/
+
 void exit_server(char *reason)
 {
        static int firsttime=1;
@@ -527,8 +537,9 @@ void exit_server(char *reason)
 }
 
 /****************************************************************************
-  initialise connect, service and file structs
+ Initialise connect, service and file structs.
 ****************************************************************************/
+
 static void init_structs(void )
 {
        /*
@@ -560,8 +571,9 @@ static void init_structs(void )
 }
 
 /****************************************************************************
-usage on the program
+ Usage on the program.
 ****************************************************************************/
+
 static void usage(char *pname)
 {
 
@@ -584,11 +596,13 @@ static void usage(char *pname)
 }
 
 /****************************************************************************
-  main program
+ main program.
 ****************************************************************************/
+
  int main(int argc,char *argv[])
 {
        extern BOOL append_log;
+       extern BOOL AllowDebugChange;
        extern char *optarg;
        /* shall I run as a daemon */
        BOOL is_daemon = False;
@@ -645,6 +659,7 @@ static void usage(char *pname)
                                DEBUGLEVEL = 10000;
                        else
                                DEBUGLEVEL = atoi(optarg);
+                       AllowDebugChange = False;
                        break;
 
                case 'p':
@@ -702,8 +717,9 @@ static void usage(char *pname)
        gain_root_group_privilege();
 
        fault_setup((void (*)(void *))exit_server);
-       CatchSignal(SIGTERM , SIGNAL_CAST dflt_sig);
-
+       CatchSignal(SIGTERM , SIGNAL_CAST sig_term);
+       CatchSignal(SIGHUP,SIGNAL_CAST sig_hup);
+       
        /* we are never interested in SIGPIPE */
        BlockSignals(True,SIGPIPE);
 
@@ -721,6 +737,7 @@ static void usage(char *pname)
         * 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 */
@@ -765,23 +782,19 @@ static void usage(char *pname)
        }
 #endif
 
-#ifdef WITH_SSL
-       {
-               extern BOOL sslEnabled;
-               sslEnabled = lp_ssl_enabled();
-               if(sslEnabled)
-                       sslutil_init(True);
-       }
-#endif        /* WITH_SSL */
-
        fstrcpy(global_myworkgroup, lp_workgroup());
 
-       CatchSignal(SIGHUP,SIGNAL_CAST sig_hup);
-       
        DEBUG(3,( "loaded services\n"));
 
        if (!is_daemon && !is_a_socket(0)) {
-               DEBUG(0,("standard input is not a socket, assuming -D option\n"));
+               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;
        }
 
@@ -866,10 +879,6 @@ static void usage(char *pname)
        if (!init_oplocks())
                exit(1);
        
-       /* Setup mangle */
-       if (!init_mangle_tdb())
-               exit(1);
-
        /* Setup change notify */
        if (!init_change_notify())
                exit(1);