Fix bug #7067 - Linux asynchronous IO (aio) can cause smbd to fail to respond to...
[ira/wip.git] / source3 / smbd / server.c
index 67836f785b489c8e022d10e1aa8ded2f78a8dd15..fb0efd2ae5202ed5315e3cd4b972e8783c89e6fb 100644 (file)
@@ -94,6 +94,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);
 }
 
@@ -180,19 +181,33 @@ static void msg_inject_fault(struct messaging_context *msg,
 }
 #endif /* DEVELOPER */
 
-struct child_pid {
-       struct child_pid *prev, *next;
-       pid_t pid;
-};
-
-static void add_child_pid(pid_t pid)
+/*
+ * Parent smbd process sets its own debug level first and then
+ * sends a message to all the smbd children to adjust their debug
+ * level to that of the parent.
+ */
+
+static void smbd_msg_debug(struct messaging_context *msg_ctx,
+                          void *private_data,
+                          uint32_t msg_type,
+                          struct server_id server_id,
+                          DATA_BLOB *data)
 {
        struct child_pid *child;
 
-       if (lp_max_smbd_processes() == 0) {
-               /* Don't bother with the child list if we don't care anyway */
-               return;
+       debug_message(msg_ctx, private_data, MSG_DEBUG, server_id, data);
+
+       for (child = children; child != NULL; child = child->next) {
+               messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
+                                  MSG_DEBUG,
+                                  data->data,
+                                  strlen((char *) data->data) + 1);
        }
+}
+
+static void add_child_pid(pid_t pid)
+{
+       struct child_pid *child;
 
        child = SMB_MALLOC_P(struct child_pid);
        if (child == NULL) {
@@ -219,11 +234,6 @@ static void remove_child_pid(pid_t pid, bool unclean_shutdown)
                                 MSG_SMB_UNLOCK, NULL, 0, NULL);
        }
 
-       if (lp_max_smbd_processes() == 0) {
-               /* Don't bother with the child list if we don't care anyway */
-               return;
-       }
-
        for (child = children; child != NULL; child = child->next) {
                if (child->pid == pid) {
                        struct child_pid *tmp = child;
@@ -356,6 +366,7 @@ static void smbd_accept_connection(struct tevent_context *ev,
 
        pid = sys_fork();
        if (pid == 0) {
+               NTSTATUS status = NT_STATUS_OK;
                /* Child code ... */
                am_parent = 0;
 
@@ -374,10 +385,15 @@ static void smbd_accept_connection(struct tevent_context *ev,
                talloc_free(s->parent);
                s = NULL;
 
-               if (!reinit_after_fork(
-                           smbd_messaging_context(),
-                           smbd_event_context(),
-                           true)) {
+               status = reinit_after_fork(smbd_messaging_context(),
+                                          smbd_event_context(), true);
+               if (!NT_STATUS_IS_OK(status)) {
+                       if (NT_STATUS_EQUAL(status,
+                                           NT_STATUS_TOO_MANY_OPENED_FILES)) {
+                               DEBUG(0,("child process cannot initialize "
+                                        "because too many files are open\n"));
+                               goto exit;
+                       }
                        DEBUG(0,("reinit_after_fork() failed\n"));
                        smb_panic("reinit_after_fork() failed");
                }
@@ -386,6 +402,7 @@ static void smbd_accept_connection(struct tevent_context *ev,
                smbd_setup_sig_hup_handler();
 
                smbd_process();
+        exit:
                exit_server_cleanly("end of child");
                return;
        } else if (pid < 0) {
@@ -629,6 +646,8 @@ static bool open_sockets_smbd(struct smbd_parent_context *parent,
                           MSG_SMB_CONF_UPDATED, smb_conf_updated);
        messaging_register(smbd_messaging_context(), NULL,
                           MSG_SMB_STAT_CACHE_DELETE, smb_stat_cache_delete);
+       messaging_register(smbd_messaging_context(), NULL,
+                          MSG_DEBUG, smbd_msg_debug);
        brl_register_msgs(smbd_messaging_context());
 
 #ifdef CLUSTER_SUPPORT
@@ -777,7 +796,8 @@ static void exit_server_common(enum server_exit_reason how,
 static void exit_server_common(enum server_exit_reason how,
        const char *const reason)
 {
-       bool had_open_conn;
+       bool had_open_conn = false;
+       struct smbd_server_connection *sconn = smbd_server_conn;
 
        if (!exit_firsttime)
                exit(0);
@@ -785,13 +805,15 @@ static void exit_server_common(enum server_exit_reason how,
 
        change_to_root_user();
 
-       if (negprot_global_auth_context) {
-               (negprot_global_auth_context->free)(&negprot_global_auth_context);
+       if (sconn && sconn->smb1.negprot.auth_context) {
+               struct auth_context *a = sconn->smb1.negprot.auth_context;
+               a->free(&sconn->smb1.negprot.auth_context);
        }
 
-       had_open_conn = conn_close_all();
-
-       invalidate_all_vuids();
+       if (sconn) {
+               had_open_conn = conn_close_all(sconn);
+               invalidate_all_vuids(sconn);
+       }
 
        /* 3 second timeout. */
        print_notify_send_messages(smbd_messaging_context(), 3);
@@ -817,6 +839,15 @@ static void exit_server_common(enum server_exit_reason how,
        locking_end();
        printing_end();
 
+       /*
+        * we need to force the order of freeing the following,
+        * because smbd_msg_ctx is not a talloc child of smbd_server_conn.
+        */
+       sconn = NULL;
+       TALLOC_FREE(smbd_server_conn);
+       TALLOC_FREE(smbd_msg_ctx);
+       TALLOC_FREE(smbd_event_ctx);
+
        if (how != SERVER_EXIT_NORMAL) {
                int oldlevel = DEBUGLEVEL;
 
@@ -835,6 +866,10 @@ static void exit_server_common(enum server_exit_reason how,
        } else {    
                DEBUG(3,("Server exit (%s)\n",
                        (reason ? reason : "normal exit")));
+               if (am_parent) {
+                       pidfile_unlink();
+               }
+               gencache_stabilize();
        }
 
        /* if we had any open SMB connections when we exited then we
@@ -877,12 +912,8 @@ static bool init_structs(void )
        if (!init_names())
                return False;
 
-       conn_init();
-
        file_init();
 
-       init_dptrs();
-
        if (!secrets_init())
                return False;
 
@@ -1016,6 +1047,14 @@ extern void build_options(bool screen);
        gain_root_privilege();
        gain_root_group_privilege();
 
+       /*
+        * Ensure we have CAP_KILL capability set on Linux,
+        * where we need this to communicate with threads.
+        * This is inherited by new threads, but not by new
+        * processes across exec().
+        */
+       set_effective_capability(KILL_CAPABILITY);
+
        fault_setup((void (*)(void *))exit_server_fault);
        dump_core_setup("smbd");
 
@@ -1038,6 +1077,11 @@ extern void build_options(bool screen);
        BlockSignals(False, SIGUSR1);
        BlockSignals(False, SIGTERM);
 
+       /* Ensure we leave no zombies until we
+        * correctly set up child handling below. */
+
+       CatchChild();
+
        /* we want total control over the permissions on created files,
           so set our umask to 0 */
        umask(0);
@@ -1122,8 +1166,8 @@ extern void build_options(bool screen);
        if (is_daemon)
                pidfile_create("smbd");
 
-       if (!reinit_after_fork(smbd_messaging_context(),
-                              smbd_event_context(), false)) {
+       if (!NT_STATUS_IS_OK(reinit_after_fork(smbd_messaging_context(),
+                            smbd_event_context(), false))) {
                DEBUG(0,("reinit_after_fork() failed\n"));
                exit(1);
        }
@@ -1182,6 +1226,15 @@ extern void build_options(bool screen);
                return -1;
        }
 
+       /* Open the share_info.tdb here, so we don't have to open
+          after the fork on every single connection.  This is a small
+          performance improvment and reduces the total number of system
+          fds used. */
+       if (!share_info_db_init()) {
+               DEBUG(0,("ERROR: failed to load share info db.\n"));
+               exit(1);
+       }
+
        /* 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 
@@ -1204,6 +1257,13 @@ extern void build_options(bool screen);
                /* close our standard file descriptors */
                close_low_fds(False); /* Don't close stderr */
 
+#ifdef HAVE_ATEXIT
+               atexit(killkids);
+#endif
+
+               /* Stop zombies */
+               smbd_setup_sig_chld_handler();
+
                smbd_process();
 
                exit_server_cleanly(NULL);