Launch the application notification thread using lttng_thread
[lttng-tools.git] / src / bin / lttng-sessiond / main.c
index b211370d538f03a0de8ba00121a4333160053b29..39800b51ccba4cefaec88d13e803620da6a02b10 100644 (file)
@@ -85,6 +85,7 @@
 #include "client.h"
 #include "dispatch.h"
 #include "register.h"
+#include "manage-apps.h"
 
 static const char *help_msg =
 #ifdef LTTNG_EMBED_HELP
@@ -141,10 +142,9 @@ static const char *config_ignore_options[] = { "help", "version", "config" };
  * that a command is queued and ready to be processed.
  */
 static int apps_cmd_pipe[2] = { -1, -1 };
+static int apps_cmd_notify_pipe[2] = { -1, -1 };
 
 /* Pthread, Mutexes and Semaphores */
-static pthread_t apps_thread;
-static pthread_t apps_notify_thread;
 static pthread_t kernel_thread;
 static pthread_t agent_reg_thread;
 static pthread_t load_session_thread;
@@ -293,6 +293,8 @@ static void sessiond_cleanup(void)
         * since we are now called.
         */
        sessiond_close_quit_pipe();
+       utils_close_pipe(apps_cmd_pipe);
+       utils_close_pipe(apps_cmd_notify_pipe);
 
        ret = remove(config.pid_file_path.value);
        if (ret < 0) {
@@ -1050,177 +1052,6 @@ error_poll:
        return NULL;
 }
 
-/*
- * This thread receives application command sockets (FDs) on the
- * apps_cmd_pipe and waits (polls) on them until they are closed
- * or an error occurs.
- *
- * At that point, it flushes the data (tracing and metadata) associated
- * with this application and tears down ust app sessions and other
- * associated data structures through ust_app_unregister().
- *
- * Note that this thread never sends commands to the applications
- * through the command sockets; it merely listens for hang-ups
- * and errors on those sockets and cleans-up as they occur.
- */
-static void *thread_manage_apps(void *data)
-{
-       int i, ret, pollfd, err = -1;
-       ssize_t size_ret;
-       uint32_t revents, nb_fd;
-       struct lttng_poll_event events;
-
-       DBG("[thread] Manage application started");
-
-       rcu_register_thread();
-       rcu_thread_online();
-
-       health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_MANAGE);
-
-       if (testpoint(sessiond_thread_manage_apps)) {
-               goto error_testpoint;
-       }
-
-       health_code_update();
-
-       ret = sessiond_set_thread_pollset(&events, 2);
-       if (ret < 0) {
-               goto error_poll_create;
-       }
-
-       ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
-       if (ret < 0) {
-               goto error;
-       }
-
-       if (testpoint(sessiond_thread_manage_apps_before_loop)) {
-               goto error;
-       }
-
-       health_code_update();
-
-       while (1) {
-               DBG("Apps thread polling");
-
-               /* Inifinite blocking call, waiting for transmission */
-       restart:
-               health_poll_entry();
-               ret = lttng_poll_wait(&events, -1);
-               DBG("Apps thread return from poll on %d fds",
-                               LTTNG_POLL_GETNB(&events));
-               health_poll_exit();
-               if (ret < 0) {
-                       /*
-                        * Restart interrupted system call.
-                        */
-                       if (errno == EINTR) {
-                               goto restart;
-                       }
-                       goto error;
-               }
-
-               nb_fd = ret;
-
-               for (i = 0; i < nb_fd; i++) {
-                       /* Fetch once the poll data */
-                       revents = LTTNG_POLL_GETEV(&events, i);
-                       pollfd = LTTNG_POLL_GETFD(&events, i);
-
-                       health_code_update();
-
-                       if (!revents) {
-                               /* No activity for this FD (poll implementation). */
-                               continue;
-                       }
-
-                       /* Thread quit pipe has been closed. Killing thread. */
-                       ret = sessiond_check_thread_quit_pipe(pollfd, revents);
-                       if (ret) {
-                               err = 0;
-                               goto exit;
-                       }
-
-                       /* Inspect the apps cmd pipe */
-                       if (pollfd == apps_cmd_pipe[0]) {
-                               if (revents & LPOLLIN) {
-                                       int sock;
-
-                                       /* Empty pipe */
-                                       size_ret = lttng_read(apps_cmd_pipe[0], &sock, sizeof(sock));
-                                       if (size_ret < sizeof(sock)) {
-                                               PERROR("read apps cmd pipe");
-                                               goto error;
-                                       }
-
-                                       health_code_update();
-
-                                       /*
-                                        * Since this is a command socket (write then read),
-                                        * we only monitor the error events of the socket.
-                                        */
-                                       ret = lttng_poll_add(&events, sock,
-                                                       LPOLLERR | LPOLLHUP | LPOLLRDHUP);
-                                       if (ret < 0) {
-                                               goto error;
-                                       }
-
-                                       DBG("Apps with sock %d added to poll set", sock);
-                               } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
-                                       ERR("Apps command pipe error");
-                                       goto error;
-                               } else {
-                                       ERR("Unknown poll events %u for sock %d", revents, pollfd);
-                                       goto error;
-                               }
-                       } else {
-                               /*
-                                * At this point, we know that a registered application made
-                                * the event at poll_wait.
-                                */
-                               if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
-                                       /* Removing from the poll set */
-                                       ret = lttng_poll_del(&events, pollfd);
-                                       if (ret < 0) {
-                                               goto error;
-                                       }
-
-                                       /* Socket closed on remote end. */
-                                       ust_app_unregister(pollfd);
-                               } else {
-                                       ERR("Unexpected poll events %u for sock %d", revents, pollfd);
-                                       goto error;
-                               }
-                       }
-
-                       health_code_update();
-               }
-       }
-
-exit:
-error:
-       lttng_poll_clean(&events);
-error_poll_create:
-error_testpoint:
-       utils_close_pipe(apps_cmd_pipe);
-       apps_cmd_pipe[0] = apps_cmd_pipe[1] = -1;
-
-       /*
-        * We don't clean the UST app hash table here since already registered
-        * applications can still be controlled so let them be until the session
-        * daemon dies or the applications stop.
-        */
-
-       if (err) {
-               health_error();
-               ERR("Health error occurred in %s", __func__);
-       }
-       health_unregister(health_sessiond);
-       DBG("Application communication apps thread cleanup complete");
-       rcu_thread_offline();
-       rcu_unregister_thread();
-       return NULL;
-}
-
 /*
  * Setup necessary data for kernel tracer action.
  */
@@ -2605,24 +2436,14 @@ int main(int argc, char **argv)
        }
 
        /* Create thread to manage application socket */
-       ret = pthread_create(&apps_thread, default_pthread_attr(),
-                       thread_manage_apps, (void *) NULL);
-       if (ret) {
-               errno = ret;
-               PERROR("pthread_create apps");
+       if (!launch_application_management_thread(apps_cmd_pipe[0])) {
                retval = -1;
-               stop_threads();
                goto exit_apps;
        }
 
        /* Create thread to manage application notify socket */
-       ret = pthread_create(&apps_notify_thread, default_pthread_attr(),
-                       ust_thread_manage_notify, (void *) NULL);
-       if (ret) {
-               errno = ret;
-               PERROR("pthread_create notify");
+       if (!launch_application_notification_thread(apps_cmd_notify_pipe[0])) {
                retval = -1;
-               stop_threads();
                goto exit_apps_notify;
        }
 
@@ -2707,21 +2528,7 @@ exit_kernel:
                retval = -1;
        }
 exit_agent_reg:
-
-       ret = pthread_join(apps_notify_thread, &status);
-       if (ret) {
-               errno = ret;
-               PERROR("pthread_join apps notify");
-               retval = -1;
-       }
 exit_apps_notify:
-
-       ret = pthread_join(apps_thread, &status);
-       if (ret) {
-               errno = ret;
-               PERROR("pthread_join apps");
-               retval = -1;
-       }
 exit_apps:
 exit_reg_apps:
 exit_dispatch:
This page took 0.027287 seconds and 5 git commands to generate.