s3-smbd: Cleanup the order of the init functions.
[amitay/samba.git] / source3 / smbd / server.c
index a1250790cee2109d012415d4f7fc2d73450d1e1a..c41ad6fbafd82979ef1badcfe40ea493de59bf52 100644 (file)
 */
 
 #include "includes.h"
+#include "popt_common.h"
 #include "smbd/globals.h"
 #include "librpc/gen_ndr/messaging.h"
 #include "registry.h"
 #include "libcli/auth/schannel.h"
+#include "secrets.h"
 
 static_decl_rpc;
 
@@ -66,7 +68,7 @@ static void smb_conf_updated(struct messaging_context *msg,
        DEBUG(10,("smb_conf_updated: Got message saying smb.conf was "
                  "updated. Reloading.\n"));
        change_to_root_user();
-       reload_services(False);
+       reload_services(msg, False);
 }
 
 
@@ -367,7 +369,7 @@ static void smbd_accept_connection(struct tevent_context *ev,
        }
 
        if (s->parent->interactive) {
-               smbd_process();
+               smbd_process(smbd_server_conn);
                exit_server_cleanly("end of interactive mode");
                return;
        }
@@ -409,7 +411,8 @@ static void smbd_accept_connection(struct tevent_context *ev,
                s = NULL;
 
                status = reinit_after_fork(smbd_messaging_context(),
-                                          smbd_event_context(), true);
+                                          smbd_event_context(), procid_self(),
+                                          true);
                if (!NT_STATUS_IS_OK(status)) {
                        if (NT_STATUS_EQUAL(status,
                                            NT_STATUS_TOO_MANY_OPENED_FILES)) {
@@ -422,7 +425,8 @@ static void smbd_accept_connection(struct tevent_context *ev,
                }
 
                smbd_setup_sig_term_handler();
-               smbd_setup_sig_hup_handler();
+               smbd_setup_sig_hup_handler(server_event_context(),
+                                          server_messaging_context());
 
                if (!serverid_register(procid_self(),
                                       FLAG_MSG_GENERAL|FLAG_MSG_SMBD
@@ -432,11 +436,13 @@ static void smbd_accept_connection(struct tevent_context *ev,
                                            "serverid.tdb");
                }
 
-               smbd_process();
+               smbd_process(smbd_server_conn);
         exit:
                exit_server_cleanly("end of child");
                return;
-       } else if (pid < 0) {
+       }
+
+       if (pid < 0) {
                DEBUG(0,("smbd_accept_connection: sys_fork() failed: %s\n",
                         strerror(errno)));
        }
@@ -689,7 +695,8 @@ static bool open_sockets_smbd(struct smbd_parent_context *parent,
 
 #ifdef CLUSTER_SUPPORT
        if (lp_clustering()) {
-               ctdbd_register_reconfigure(messaging_ctdbd_connection());
+               ctdbd_register_reconfigure(
+                       messaging_ctdbd_connection(procid_self()));
        }
 #endif
 
@@ -806,6 +813,11 @@ extern void build_options(bool screen);
        };
        struct smbd_parent_context *parent = NULL;
        TALLOC_CTX *frame = talloc_stackframe(); /* Setup tos. */
+       NTSTATUS status;
+       uint64_t unique_id;
+
+       /* Initialize the event context, it will panic on error */
+       smbd_event_context();
 
        smbd_init_globals();
 
@@ -864,8 +876,6 @@ extern void build_options(bool screen);
        setluid(0);
 #endif
 
-       sec_init();
-
        set_remote_machine_name("smbd", False);
 
        if (interactive && (DEBUGLEVEL >= 9)) {
@@ -881,9 +891,11 @@ extern void build_options(bool screen);
            client problems at a later date. (tridge) */
        generate_random_buffer(NULL, 0);
 
+       /* get initial effective uid and gid */
+       sec_init();
+
        /* 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();
 
@@ -918,8 +930,6 @@ extern void build_options(bool screen);
           so set our umask to 0 */
        umask(0);
 
-       init_sec_ctx();
-
        reopen_logs();
 
        DEBUG(0,("smbd version %s started.\n", samba_version_string()));
@@ -941,10 +951,13 @@ extern void build_options(bool screen);
                exit(1);
        }
 
+       /* Init the security context and global current_user */
+       init_sec_ctx();
+
        if (smbd_messaging_context() == NULL)
                exit(1);
 
-       if (!reload_services(False))
+       if (!reload_services(smbd_messaging_context(), False))
                return(-1);     
 
        init_structs();
@@ -983,6 +996,9 @@ extern void build_options(bool screen);
                become_daemon(Fork, no_process_group, log_stdout);
        }
 
+        generate_random_buffer((uint8_t *)&unique_id, sizeof(unique_id));
+        set_my_unique_id(unique_id);
+
 #if HAVE_SETPGID
        /*
         * If we're interactive we want to set our own process group for
@@ -998,14 +1014,19 @@ extern void build_options(bool screen);
        if (is_daemon)
                pidfile_create("smbd");
 
-       if (!NT_STATUS_IS_OK(reinit_after_fork(smbd_messaging_context(),
-                            smbd_event_context(), false))) {
+       status = reinit_after_fork(smbd_messaging_context(),
+                                  smbd_event_context(),
+                                  procid_self(), false);
+       if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0,("reinit_after_fork() failed\n"));
                exit(1);
        }
 
+       smbd_server_conn->msg_ctx = smbd_messaging_context();
+
        smbd_setup_sig_term_handler();
-       smbd_setup_sig_hup_handler();
+       smbd_setup_sig_hup_handler(smbd_event_context(),
+                                  smbd_server_conn->msg_ctx);
 
        /* Setup all the TDB's - including CLEAR_IF_FIRST tdb's. */
 
@@ -1097,7 +1118,8 @@ extern void build_options(bool screen);
 
        if (is_daemon && !interactive
            && lp_parm_bool(-1, "smbd", "backgroundqueue", true)) {
-               start_background_queue();
+               start_background_queue(smbd_event_context(),
+                                      smbd_messaging_context());
        }
 
        if (!is_daemon) {
@@ -1119,7 +1141,7 @@ extern void build_options(bool screen);
                /* Stop zombies */
                smbd_setup_sig_chld_handler();
 
-               smbd_process();
+               smbd_process(smbd_server_conn);
 
                exit_server_cleanly(NULL);
                return(0);