Merge branch 'master' into compat-freebsd
[lttng-tools.git] / src / bin / lttng-sessiond / main.c
index bf20850003ece493934432f876af883a59979f58..8654031f1becce5d4fa497ddba1c70b35850d25f 100644 (file)
@@ -17,7 +17,6 @@
  */
 
 #define _GNU_SOURCE
-#include <fcntl.h>
 #include <getopt.h>
 #include <grp.h>
 #include <limits.h>
@@ -40,6 +39,7 @@
 
 #include <common/common.h>
 #include <common/compat/poll.h>
+#include <common/compat/socket.h>
 #include <common/defaults.h>
 #include <common/kernel-consumer/kernel-consumer.h>
 #include <common/ust-consumer/ust-consumer.h>
@@ -295,14 +295,22 @@ static gid_t allowed_group(void)
  */
 static int init_thread_quit_pipe(void)
 {
-       int ret;
+       int ret, i;
 
-       ret = pipe2(thread_quit_pipe, O_CLOEXEC);
+       ret = pipe(thread_quit_pipe);
        if (ret < 0) {
-               perror("thread quit pipe");
+               PERROR("thread quit pipe");
                goto error;
        }
 
+       for (i = 0; i < 2; i++) {
+               ret = fcntl(thread_quit_pipe[i], F_SETFD, FD_CLOEXEC);
+               if (ret < 0) {
+                       PERROR("fcntl");
+                       goto error;
+               }
+       }
+
 error:
        return ret;
 }
@@ -769,12 +777,18 @@ static void update_ust_app(int app_sock)
 {
        struct ltt_session *sess, *stmp;
 
+       session_lock_list();
+
        /* For all tracing session(s) */
        cds_list_for_each_entry_safe(sess, stmp, &session_list_ptr->head, list) {
+               session_lock(sess);
                if (sess->ust_session) {
                        ust_app_global_update(sess->ust_session, app_sock);
                }
+               session_unlock(sess);
        }
+
+       session_unlock_list();
 }
 
 /*
@@ -825,8 +839,15 @@ static void *thread_manage_kernel(void *data)
                lttng_poll_reset(&events);
 
                /* Poll infinite value of time */
+       restart:
                ret = lttng_poll_wait(&events, -1);
                if (ret < 0) {
+                       /*
+                        * Restart interrupted system call.
+                        */
+                       if (errno == EINTR) {
+                               goto restart;
+                       }
                        goto error;
                } else if (ret == 0) {
                        /* Should not happen since timeout is infinite */
@@ -916,8 +937,15 @@ static void *thread_manage_consumer(void *data)
        nb_fd = LTTNG_POLL_GETNB(&events);
 
        /* Inifinite blocking call, waiting for transmission */
+restart:
        ret = lttng_poll_wait(&events, -1);
        if (ret < 0) {
+               /*
+                * Restart interrupted system call.
+                */
+               if (errno == EINTR) {
+                       goto restart;
+               }
                goto error;
        }
 
@@ -987,8 +1015,15 @@ static void *thread_manage_consumer(void *data)
        nb_fd = LTTNG_POLL_GETNB(&events);
 
        /* Inifinite blocking call, waiting for transmission */
+restart_poll:
        ret = lttng_poll_wait(&events, -1);
        if (ret < 0) {
+               /*
+                * Restart interrupted system call.
+                */
+               if (errno == EINTR) {
+                       goto restart_poll;
+               }
                goto error;
        }
 
@@ -1071,8 +1106,15 @@ static void *thread_manage_apps(void *data)
                DBG("Apps thread polling on %d fds", nb_fd);
 
                /* Inifinite blocking call, waiting for transmission */
+       restart:
                ret = lttng_poll_wait(&events, -1);
                if (ret < 0) {
+                       /*
+                        * Restart interrupted system call.
+                        */
+                       if (errno == EINTR) {
+                               goto restart;
+                       }
                        goto error;
                }
 
@@ -1132,8 +1174,10 @@ static void *thread_manage_apps(void *data)
                                                /*
                                                 * We just need here to monitor the close of the UST
                                                 * socket and poll set monitor those by default.
+                                                * Listen on POLLIN (even if we never expect any
+                                                * data) to ensure that hangup wakes us.
                                                 */
-                                               ret = lttng_poll_add(&events, ust_cmd.sock, 0);
+                                               ret = lttng_poll_add(&events, ust_cmd.sock, LPOLLIN);
                                                if (ret < 0) {
                                                        goto error;
                                                }
@@ -1290,8 +1334,15 @@ static void *thread_registration_apps(void *data)
                nb_fd = LTTNG_POLL_GETNB(&events);
 
                /* Inifinite blocking call, waiting for transmission */
+       restart:
                ret = lttng_poll_wait(&events, -1);
                if (ret < 0) {
+                       /*
+                        * Restart interrupted system call.
+                        */
+                       if (errno == EINTR) {
+                               goto restart;
+                       }
                        goto error;
                }
 
@@ -2866,11 +2917,12 @@ error:
 /*
  * Command LTTNG_CREATE_SESSION processed by the client thread.
  */
-static int cmd_create_session(char *name, char *path, struct ucred *creds)
+static int cmd_create_session(char *name, char *path, lttng_sock_cred *creds)
 {
        int ret;
 
-       ret = session_create(name, path, creds->uid, creds->gid);
+       ret = session_create(name, path, LTTNG_SOCK_GET_UID_CRED(creds),
+                       LTTNG_SOCK_GET_GID_CRED(creds));
        if (ret != LTTCOMM_OK) {
                goto error;
        }
@@ -3120,10 +3172,25 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
 {
        int ret = LTTCOMM_OK;
        int need_tracing_session = 1;
+       int need_domain;
 
        DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
 
-       if (opt_no_kernel && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) {
+       switch (cmd_ctx->lsm->cmd_type) {
+       case LTTNG_CREATE_SESSION:
+       case LTTNG_DESTROY_SESSION:
+       case LTTNG_LIST_SESSIONS:
+       case LTTNG_LIST_DOMAINS:
+       case LTTNG_START_TRACE:
+       case LTTNG_STOP_TRACE:
+               need_domain = 0;
+               break;
+       default:
+               need_domain = 1;
+       }
+
+       if (opt_no_kernel && need_domain
+                       && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) {
                ret = LTTCOMM_KERN_NA;
                goto error;
        }
@@ -3151,8 +3218,8 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
 
        /* Commands that DO NOT need a session. */
        switch (cmd_ctx->lsm->cmd_type) {
-       case LTTNG_CALIBRATE:
        case LTTNG_CREATE_SESSION:
+       case LTTNG_CALIBRATE:
        case LTTNG_LIST_SESSIONS:
        case LTTNG_LIST_TRACEPOINTS:
                need_tracing_session = 0;
@@ -3177,6 +3244,9 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
                break;
        }
 
+       if (!need_domain) {
+               goto skip_domain;
+       }
        /*
         * Check domain type for specific "pre-action".
         */
@@ -3270,6 +3340,7 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
        default:
                break;
        }
+skip_domain:
 
        /*
         * Check that the UID or GID match that of the tracing session.
@@ -3277,7 +3348,8 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
         */
        if (need_tracing_session) {
                if (!session_access_ok(cmd_ctx->session,
-                               cmd_ctx->creds.uid, cmd_ctx->creds.gid)) {
+                               LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
+                               LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds))) {
                        ret = LTTCOMM_EPERM;
                        goto error;
                }
@@ -3470,12 +3542,10 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
                unsigned int nr_sessions;
 
                session_lock_list();
-               nr_sessions = lttng_sessions_count(cmd_ctx->creds.uid, cmd_ctx->creds.gid);
-               if (nr_sessions == 0) {
-                       ret = LTTCOMM_NO_SESSION;
-                       session_unlock_list();
-                       goto error;
-               }
+               nr_sessions = lttng_sessions_count(
+                               LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
+                               LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
+
                ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * nr_sessions);
                if (ret < 0) {
                        session_unlock_list();
@@ -3484,7 +3554,8 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
 
                /* Filled the session array */
                list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload),
-                       cmd_ctx->creds.uid, cmd_ctx->creds.gid);
+                       LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
+                       LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
 
                session_unlock_list();
 
@@ -3573,8 +3644,15 @@ static void *thread_manage_clients(void *data)
                nb_fd = LTTNG_POLL_GETNB(&events);
 
                /* Inifinite blocking call, waiting for transmission */
+       restart:
                ret = lttng_poll_wait(&events, -1);
                if (ret < 0) {
+                       /*
+                        * Restart interrupted system call.
+                        */
+                       if (errno == EINTR) {
+                               goto restart;
+                       }
                        goto error;
                }
 
@@ -3899,24 +3977,19 @@ end:
  */
 static int check_existing_daemon(void)
 {
-       if (access(client_unix_sock_path, F_OK) < 0 &&
-                       access(apps_unix_sock_path, F_OK) < 0) {
-               return 0;
-       }
-
        /* Is there anybody out there ? */
        if (lttng_session_daemon_alive()) {
                return -EEXIST;
-       } else {
-               return 0;
        }
+
+       return 0;
 }
 
 /*
  * Set the tracing group gid onto the client socket.
  *
  * Race window between mkdir and chown is OK because we are going from more
- * permissive (root.root) to les permissive (root.tracing).
+ * permissive (root.root) to less permissive (root.tracing).
  */
 static int set_permissions(char *rundir)
 {
@@ -3937,6 +4010,13 @@ static int set_permissions(char *rundir)
                perror("chown");
        }
 
+       /* Ensure tracing group can search the run dir */
+       ret = chmod(rundir, S_IRWXU | S_IXGRP | S_IXOTH);
+       if (ret < 0) {
+               ERR("Unable to set permissions on %s", rundir);
+               perror("chmod");
+       }
+
        /* lttng client socket path */
        ret = chown(client_unix_sock_path, 0, gid);
        if (ret < 0) {
@@ -3976,7 +4056,24 @@ end:
  */
 static int create_kernel_poll_pipe(void)
 {
-       return pipe2(kernel_poll_pipe, O_CLOEXEC);
+       int ret, i;
+
+       ret = pipe(kernel_poll_pipe);
+       if (ret < 0) {
+               PERROR("kernel poll pipe");
+               goto error;
+       }
+
+       for (i = 0; i < 2; i++) {
+               ret = fcntl(kernel_poll_pipe[i], F_SETFD, FD_CLOEXEC);
+               if (ret < 0) {
+                       PERROR("fcntl kernel_poll_pipe");
+                       goto error;
+               }
+       }
+
+error:
+       return ret;
 }
 
 /*
@@ -3984,7 +4081,24 @@ static int create_kernel_poll_pipe(void)
  */
 static int create_apps_cmd_pipe(void)
 {
-       return pipe2(apps_cmd_pipe, O_CLOEXEC);
+       int ret, i;
+
+       ret = pipe(apps_cmd_pipe);
+       if (ret < 0) {
+               PERROR("apps cmd pipe");
+               goto error;
+       }
+
+       for (i = 0; i < 2; i++) {
+               ret = fcntl(apps_cmd_pipe[i], F_SETFD, FD_CLOEXEC);
+               if (ret < 0) {
+                       PERROR("fcntl apps_cmd_pipe");
+                       goto error;
+               }
+       }
+
+error:
+       return ret;
 }
 
 /*
@@ -3996,7 +4110,7 @@ static int create_lttng_rundir(const char *rundir)
 
        DBG3("Creating LTTng run directory: %s", rundir);
 
-       ret = mkdir(rundir, S_IRWXU | S_IRWXG );
+       ret = mkdir(rundir, S_IRWXU);
        if (ret < 0) {
                if (errno != EEXIST) {
                        ERR("Unable to create %s", rundir);
@@ -4038,7 +4152,7 @@ static int set_consumer_sockets(struct consumer_data *consumer_data,
 
        DBG2("Creating consumer directory: %s", path);
 
-       ret = mkdir(path, S_IRWXU | S_IRWXG);
+       ret = mkdir(path, S_IRWXU);
        if (ret < 0) {
                if (errno != EEXIST) {
                        ERR("Failed to create %s", path);
This page took 0.028367 seconds and 4 git commands to generate.