Fix: agent thread poll set creation failure results in deadlock
[lttng-tools.git] / src / bin / lttng-sessiond / agent-thread.c
index d1bb122c171d9158b343f504497e5fba2fe64382..189e3ba6b671d38db58b19b6c998530ebcc3fae0 100644 (file)
@@ -15,7 +15,6 @@
  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#define _GNU_SOURCE
 #define _LGPL_SOURCE
 #include <assert.h>
 
@@ -33,6 +32,8 @@
 #include "session.h"
 #include "utils.h"
 
+static int agent_tracing_enabled = -1;
+
 /*
  * Note that there is not port here. It's set after this URI is parsed so we
  * can let the user define a custom one. However, localhost is ALWAYS the
@@ -224,6 +225,15 @@ error:
        return ret;
 }
 
+bool agent_tracing_is_enabled(void)
+{
+       int enabled;
+
+       enabled = uatomic_read(&agent_tracing_enabled);
+       assert(enabled != -1);
+       return enabled == 1;
+}
+
 /*
  * This thread manage application notify communication.
  */
@@ -245,10 +255,18 @@ void *agent_thread_manage_registration(void *data)
        /* Create pollset with size 2, quit pipe and socket. */
        ret = sessiond_set_thread_pollset(&events, 2);
        if (ret < 0) {
+               sessiond_notify_ready();
                goto error_poll_create;
        }
 
        reg_sock = init_tcp_socket();
+       uatomic_set(&agent_tracing_enabled, !!reg_sock);
+
+       /*
+        * Signal that the agent thread is ready. The command thread
+        * may start to query whether or not agent tracing is enabled.
+        */
+       sessiond_notify_ready();
        if (!reg_sock) {
                goto error_tcp_socket;
        }
@@ -296,35 +314,22 @@ restart:
                                goto exit;
                        }
 
-                       /*
-                        * Check first if this is a POLLERR since POLLIN is also included
-                        * in an error value thus checking first.
-                        */
-                       if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
-                               /* Removing from the poll set */
-                               ret = lttng_poll_del(&events, pollfd);
-                               if (ret < 0) {
-                                       goto error;
-                               }
-
-                               agent_destroy_app_by_sock(pollfd);
-                       } else if (revents & (LPOLLIN)) {
+                       if (revents & LPOLLIN) {
                                int new_fd;
                                struct agent_app *app = NULL;
 
-                               /* Pollin event of agent app socket should NEVER happen. */
                                assert(pollfd == reg_sock->fd);
-
                                new_fd = handle_registration(reg_sock, &app);
                                if (new_fd < 0) {
-                                       WARN("[agent-thread] agent registration failed. Ignoring.");
-                                       /* Somehow the communication failed. Just continue. */
                                        continue;
                                }
                                /* Should not have a NULL app on success. */
                                assert(app);
 
-                               /* Only add poll error event to only detect shutdown. */
+                               /*
+                                * Since this is a command socket (write then read),
+                                * only add poll error event to only detect shutdown.
+                                */
                                ret = lttng_poll_add(&events, new_fd,
                                                LPOLLERR | LPOLLHUP | LPOLLRDHUP);
                                if (ret < 0) {
@@ -336,15 +341,32 @@ restart:
                                update_agent_app(app);
 
                                /* On failure, the poll will detect it and clean it up. */
-                               (void) agent_send_registration_done(app);
+                               ret = agent_send_registration_done(app);
+                               if (ret < 0) {
+                                       /* Removing from the poll set */
+                                       ret = lttng_poll_del(&events, new_fd);
+                                       if (ret < 0) {
+                                               goto error;
+                                       }
+                                       agent_destroy_app_by_sock(new_fd);
+                                       continue;
+                               }
+                       } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
+                               /* Removing from the poll set */
+                               ret = lttng_poll_del(&events, pollfd);
+                               if (ret < 0) {
+                                       goto error;
+                               }
+                               agent_destroy_app_by_sock(pollfd);
                        } else {
-                               ERR("Unknown poll events %u for sock %d", revents, pollfd);
-                               continue;
+                               ERR("Unexpected poll events %u for sock %d", revents, pollfd);
+                               goto error;
                        }
                }
        }
 
 exit:
+       uatomic_set(&agent_tracing_enabled, 0);
        /* Whatever happens, try to delete it and exit. */
        (void) lttng_poll_del(&events, reg_sock->fd);
 error:
This page took 0.024554 seconds and 4 git commands to generate.