Fix: remove the rundir at the end of the cleanup
[lttng-tools.git] / src / bin / lttng-sessiond / main.c
index 5ec39649969f9fef523e10b7e94896149d5812c1..35007b728a10eb9df8c2200167ef5885c99f95ff 100644 (file)
@@ -63,6 +63,7 @@
 #include "health-sessiond.h"
 #include "testpoint.h"
 #include "ust-thread.h"
+#include "jul-thread.h"
 
 #define CONSUMERD_FILE "lttng-consumerd"
 
@@ -156,6 +157,7 @@ static pthread_t kernel_thread;
 static pthread_t dispatch_thread;
 static pthread_t health_thread;
 static pthread_t ht_cleanup_thread;
+static pthread_t jul_reg_thread;
 
 /*
  * UST registration command queue. This queue is tied with a futex and uses a N
@@ -233,6 +235,9 @@ long page_size;
 /* Application health monitoring */
 struct health_app *health_sessiond;
 
+/* JUL TCP port for registration. Used by the JUL thread. */
+unsigned int jul_tcp_port = DEFAULT_JUL_TCP_PORT;
+
 static
 void setup_consumerd_path(void)
 {
@@ -419,8 +424,8 @@ static void close_consumer_sockets(void)
 static void cleanup(void)
 {
        int ret;
-       char *cmd = NULL;
        struct ltt_session *sess, *stmp;
+       char path[PATH_MAX];
 
        DBG("Cleaning up");
 
@@ -441,18 +446,60 @@ static void cleanup(void)
                }
        }
 
-       DBG("Removing %s directory", rundir);
-       ret = asprintf(&cmd, "rm -rf %s", rundir);
-       if (ret < 0) {
-               ERR("asprintf failed. Something is really wrong!");
-       }
-
-       /* Remove lttng run directory */
-       ret = system(cmd);
-       if (ret < 0) {
-               ERR("Unable to clean %s", rundir);
-       }
-       free(cmd);
+       DBG("Removing sessiond and consumerd content of directory %s", rundir);
+
+       /* sessiond */
+       snprintf(path, PATH_MAX,
+               "%s/%s",
+               rundir, DEFAULT_LTTNG_SESSIOND_PIDFILE);
+       DBG("Removing %s", path);
+       (void) unlink(path);
+
+       snprintf(path, PATH_MAX, "%s/%s", rundir,
+                       DEFAULT_LTTNG_SESSIOND_JULPORT_FILE);
+       DBG("Removing %s", path);
+       (void) unlink(path);
+
+       /* kconsumerd */
+       snprintf(path, PATH_MAX,
+               DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
+               rundir);
+       DBG("Removing %s", path);
+       (void) unlink(path);
+
+       snprintf(path, PATH_MAX,
+               DEFAULT_KCONSUMERD_PATH,
+               rundir);
+       DBG("Removing directory %s", path);
+       (void) rmdir(path);
+
+       /* ust consumerd 32 */
+       snprintf(path, PATH_MAX,
+               DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH,
+               rundir);
+       DBG("Removing %s", path);
+       (void) unlink(path);
+
+       snprintf(path, PATH_MAX,
+               DEFAULT_USTCONSUMERD32_PATH,
+               rundir);
+       DBG("Removing directory %s", path);
+       (void) rmdir(path);
+
+       /* ust consumerd 64 */
+       snprintf(path, PATH_MAX,
+               DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH,
+               rundir);
+       DBG("Removing %s", path);
+       (void) unlink(path);
+
+       snprintf(path, PATH_MAX,
+               DEFAULT_USTCONSUMERD64_PATH,
+               rundir);
+       DBG("Removing directory %s", path);
+       (void) rmdir(path);
+
+       (void) rmdir(rundir);
        free(rundir);
 
        DBG("Cleaning up all sessions");
@@ -1289,18 +1336,6 @@ static void *thread_manage_apps(void *data)
                                                goto error;
                                        }
 
-                                       /*
-                                        * Set socket timeout for both receiving and ending.
-                                        * app_socket_timeout is in seconds, whereas
-                                        * lttcomm_setsockopt_rcv_timeout and
-                                        * lttcomm_setsockopt_snd_timeout expect msec as
-                                        * parameter.
-                                        */
-                                       (void) lttcomm_setsockopt_rcv_timeout(sock,
-                                                       app_socket_timeout * 1000);
-                                       (void) lttcomm_setsockopt_snd_timeout(sock,
-                                                       app_socket_timeout * 1000);
-
                                        DBG("Apps with sock %d added to poll set", sock);
 
                                        health_code_update();
@@ -1544,7 +1579,7 @@ static void *thread_dispatch_ust_registration(void *data)
                                        if (ret < 0) {
                                                PERROR("close ust sock dispatch %d", ust_cmd->sock);
                                        }
-                                       lttng_fd_put(1, LTTNG_FD_APPS);
+                                       lttng_fd_put(LTTNG_FD_APPS, 1);
                                        free(ust_cmd);
                                        goto error;
                                }
@@ -1558,7 +1593,7 @@ static void *thread_dispatch_ust_registration(void *data)
                                        if (ret < 0) {
                                                PERROR("close ust sock dispatch %d", ust_cmd->sock);
                                        }
-                                       lttng_fd_put(1, LTTNG_FD_APPS);
+                                       lttng_fd_put(LTTNG_FD_APPS, 1);
                                        free(wait_node);
                                        free(ust_cmd);
                                        continue;
@@ -1606,7 +1641,7 @@ static void *thread_dispatch_ust_registration(void *data)
                                        if (ret < 0) {
                                                PERROR("close ust sock dispatch %d", ust_cmd->sock);
                                        }
-                                       lttng_fd_put(1, LTTNG_FD_APPS);
+                                       lttng_fd_put(LTTNG_FD_APPS, 1);
                                }
                                free(ust_cmd);
                        }
@@ -1803,6 +1838,18 @@ static void *thread_registration_apps(void *data)
                                                goto error;
                                        }
 
+                                       /*
+                                        * Set socket timeout for both receiving and ending.
+                                        * app_socket_timeout is in seconds, whereas
+                                        * lttcomm_setsockopt_rcv_timeout and
+                                        * lttcomm_setsockopt_snd_timeout expect msec as
+                                        * parameter.
+                                        */
+                                       (void) lttcomm_setsockopt_rcv_timeout(sock,
+                                                       app_socket_timeout * 1000);
+                                       (void) lttcomm_setsockopt_snd_timeout(sock,
+                                                       app_socket_timeout * 1000);
+
                                        /*
                                         * Set the CLOEXEC flag. Return code is useless because
                                         * either way, the show must go on.
@@ -2374,6 +2421,7 @@ static int copy_session_consumer(int domain, struct ltt_session *session)
                consumer = session->kernel_session->consumer;
                dir_name = DEFAULT_KERNEL_TRACE_DIR;
                break;
+       case LTTNG_DOMAIN_JUL:
        case LTTNG_DOMAIN_UST:
                DBG3("Copying tracing session consumer output in UST session");
                if (session->ust_session->consumer) {
@@ -2417,6 +2465,7 @@ static int create_ust_session(struct ltt_session *session,
        assert(session->consumer);
 
        switch (domain->type) {
+       case LTTNG_DOMAIN_JUL:
        case LTTNG_DOMAIN_UST:
                break;
        default:
@@ -2708,6 +2757,7 @@ static int process_client_msg(struct command_ctx *cmd_ctx, int sock,
                }
 
                break;
+       case LTTNG_DOMAIN_JUL:
        case LTTNG_DOMAIN_UST:
        {
                if (!ust_app_supported()) {
@@ -2799,6 +2849,7 @@ skip_domain:
        if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE ||
                        cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) {
                switch (cmd_ctx->lsm->domain.type) {
+               case LTTNG_DOMAIN_JUL:
                case LTTNG_DOMAIN_UST:
                        if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) {
                                ret = LTTNG_ERR_NO_USTCONSUMERD;
@@ -3875,6 +3926,7 @@ static void usage(void)
        fprintf(stderr, "  -p, --pidfile FILE                 Write a pid to FILE name overriding the default value.\n");
        fprintf(stderr, "      --verbose-consumer             Verbose mode for consumer. Activate DBG() macro.\n");
        fprintf(stderr, "      --no-kernel                    Disable kernel tracer\n");
+       fprintf(stderr, "      --jul-tcp-port                 JUL application registration TCP port\n");
 }
 
 /*
@@ -3907,12 +3959,13 @@ static int parse_args(int argc, char **argv)
                { "verbose-consumer", 0, 0, 'Z' },
                { "no-kernel", 0, 0, 'N' },
                { "pidfile", 1, 0, 'p' },
+               { "jul-tcp-port", 1, 0, 'J' },
                { NULL, 0, 0, 0 }
        };
 
        while (1) {
                int option_index = 0;
-               c = getopt_long(argc, argv, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t:p:",
+               c = getopt_long(argc, argv, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t:p:J:",
                                long_options, &option_index);
                if (c == -1) {
                        break;
@@ -3992,6 +4045,24 @@ static int parse_args(int argc, char **argv)
                case 'p':
                        opt_pidfile = optarg;
                        break;
+               case 'J': /* JUL TCP port. */
+               {
+                       unsigned long v;
+
+                       errno = 0;
+                       v = strtoul(optarg, NULL, 0);
+                       if (errno != 0 || !isdigit(optarg[0])) {
+                               ERR("Wrong value in --jul-tcp-port parameter: %s", optarg);
+                               return -1;
+                       }
+                       if (v == 0 || v >= 65535) {
+                               ERR("Port overflow in --jul-tcp-port parameter: %s", optarg);
+                               return -1;
+                       }
+                       jul_tcp_port = (uint32_t) v;
+                       DBG3("JUL TCP port set to non default: %u", jul_tcp_port);
+                       break;
+               }
                default:
                        /* Unknown option or other error.
                         * Error is printed by getopt, just return */
@@ -4364,6 +4435,34 @@ error:
        return;
 }
 
+/*
+ * Write JUL TCP port using the rundir.
+ */
+static void write_julport(void)
+{
+       int ret;
+       char path[PATH_MAX];
+
+       assert(rundir);
+
+       ret = snprintf(path, sizeof(path), "%s/"
+                       DEFAULT_LTTNG_SESSIOND_JULPORT_FILE, rundir);
+       if (ret < 0) {
+               PERROR("snprintf julport path");
+               goto error;
+       }
+
+       /*
+        * Create TCP JUL port file in rundir. Return value is of no importance.
+        * The execution will continue even though we are not able to write the
+        * file.
+        */
+       (void) utils_create_pid_file(jul_tcp_port, path);
+
+error:
+       return;
+}
+
 /*
  * main
  */
@@ -4561,6 +4660,12 @@ int main(int argc, char **argv)
         */
        ust_app_ht_alloc();
 
+       /* Initialize JUL domain subsystem. */
+       if ((ret = jul_init()) < 0) {
+               /* ENOMEM at this point. */
+               goto error;
+       }
+
        /* After this point, we can safely call cleanup() with "goto exit" */
 
        /*
@@ -4664,6 +4769,7 @@ int main(int argc, char **argv)
        }
 
        write_pidfile();
+       write_julport();
 
        /* Initialize communication library */
        lttcomm_init();
@@ -4736,6 +4842,14 @@ int main(int argc, char **argv)
                goto exit_apps_notify;
        }
 
+       /* Create JUL registration thread. */
+       ret = pthread_create(&jul_reg_thread, NULL,
+                       jul_thread_manage_registration, (void *) NULL);
+       if (ret != 0) {
+               PERROR("pthread_create apps");
+               goto exit_jul_reg;
+       }
+
        /* Don't start this thread if kernel tracing is not requested nor root */
        if (is_root && !opt_no_kernel) {
                /* Create kernel thread to manage kernel event */
@@ -4754,6 +4868,13 @@ int main(int argc, char **argv)
        }
 
 exit_kernel:
+       ret = pthread_join(jul_reg_thread, &status);
+       if (ret != 0) {
+               PERROR("pthread_join JUL");
+               goto error;     /* join error, exit without cleanup */
+       }
+
+exit_jul_reg:
        ret = pthread_join(apps_notify_thread, &status);
        if (ret != 0) {
                PERROR("pthread_join apps notify");
This page took 0.028457 seconds and 5 git commands to generate.