Make code simpler for send buffer header size
[lttng-tools.git] / ltt-sessiond / ltt-sessiond.c
index 2dcf62f4518b620a31bd3b674dab166b143c67b3..4f9ca1bd4d2e45396edbbe367a165dca50af3830 100644 (file)
@@ -56,10 +56,15 @@ static void copy_common_data(struct lttcomm_lttng_msg *llm, struct lttcomm_sessi
 static int check_existing_daemon(void);
 static int notify_apps(const char* name);
 static int connect_app(pid_t pid);
+static int find_app_by_pid(pid_t pid);
 static int init_daemon_socket(void);
 static int process_client_msg(int sock, struct lttcomm_session_msg*);
 static int send_unix_sock(int sock, void *buf, size_t len);
 static int setup_data_buffer(char **buf, size_t size, struct lttcomm_lttng_msg *llm);
+static void add_traceable_app(struct ltt_traceable_app *lta);
+static void del_traceable_app(struct ltt_traceable_app *lta);
+static void add_session_list(struct ltt_session *ls);
+static void del_session_list(struct ltt_session *ls);
 
 /* Command function */
 static void get_list_apps(pid_t *pids);
@@ -69,9 +74,10 @@ static void *thread_manage_clients(void *data);
 static void *thread_manage_apps(void *data);
 
 static int create_session(char *name, uuid_t *session_id);
-static void destroy_session(uuid_t session_id);
+static int destroy_session(uuid_t *uuid);
 
-static struct ltt_session *find_session(uuid_t session_id);
+static struct ltt_session *find_session_by_uuid(uuid_t session_id);
+static struct ltt_session *find_session_by_name(char *name);
 
 /* Variables */
 const char *progname;
@@ -105,6 +111,9 @@ static struct ltt_traceable_app_list ltt_traceable_app_list = {
        .head = CDS_LIST_HEAD_INIT(ltt_traceable_app_list.head),
 };
 
+/* List mutex */
+pthread_mutex_t ltt_traceable_app_list_mutex;
+
 /*
  *     thread_manage_apps
  *
@@ -152,26 +161,16 @@ static void *thread_manage_apps(void *data)
                        lta = malloc(sizeof(struct ltt_traceable_app));
                        lta->pid = reg_msg.pid;
                        lta->uid = reg_msg.uid;
-                       cds_list_add(&lta->list, &ltt_traceable_app_list.head);
-                       traceable_app_count++;
+                       add_traceable_app(lta);
                } else {
                        /* Unregistering */
-                       lta = NULL;
                        cds_list_for_each_entry(lta, &ltt_traceable_app_list.head, list) {
                                if (lta->pid == reg_msg.pid && lta->uid == reg_msg.uid) {
-                                       cds_list_del(&lta->list);
-                                       /* Check to not overflow here */
-                                       if (traceable_app_count != 0) {
-                                               traceable_app_count--;
-                                       }
+                                       del_traceable_app(lta);
+                                       free(lta);
                                        break;
                                }
                        }
-
-                       /* If an item was found, free it from memory */
-                       if (lta) {
-                               free(lta);
-                       }
                }
        }
 
@@ -216,7 +215,7 @@ static void *thread_manage_clients(void *data)
                 * request of the client.
                 */
                ret = lttcomm_recv_unix_sock(sock, &lsm, sizeof(lsm));
-               if (ret < 0) {
+               if (ret <= 0) {
                        continue;
                }
 
@@ -236,6 +235,62 @@ error:
        return NULL;
 }
 
+/*
+ *  add_traceable_app
+ *
+ *  Add a traceable application structure to the global
+ *  list protected by a mutex.
+ */
+static void add_traceable_app(struct ltt_traceable_app *lta)
+{
+       pthread_mutex_lock(&ltt_traceable_app_list_mutex);
+       cds_list_add(&lta->list, &ltt_traceable_app_list.head);
+       traceable_app_count++;
+       pthread_mutex_unlock(&ltt_traceable_app_list_mutex);
+}
+
+/*
+ *  del_traceable_app
+ *
+ *  Delete a traceable application structure from the
+ *  global list protected by a mutex.
+ */
+static void del_traceable_app(struct ltt_traceable_app *lta)
+{
+       pthread_mutex_lock(&ltt_traceable_app_list_mutex);
+       cds_list_del(&lta->list);
+       /* Sanity check */
+       if (traceable_app_count != 0) {
+               traceable_app_count--;
+       }
+       pthread_mutex_unlock(&ltt_traceable_app_list_mutex);
+}
+
+/*
+ *  add_session_list
+ *
+ *  Add a ltt_session structure to the global list.
+ */
+static void add_session_list(struct ltt_session *ls)
+{
+       cds_list_add(&ls->list, &ltt_session_list.head);
+       session_count++;
+}
+
+/*
+ *  del_session_list
+ *
+ *  Delete a ltt_session structure to the global list.
+ */
+static void del_session_list(struct ltt_session *ls)
+{
+       cds_list_del(&ls->list);
+       /* Sanity check */
+       if (session_count != 0) {
+               session_count--;
+       }
+}
+
 /*
  *  send_unix_sock
  *
@@ -258,10 +313,18 @@ static int send_unix_sock(int sock, void *buf, size_t len)
  *
  *     Return a socket connected to the libust communication socket
  *     of the application identified by the pid.
+ *
+ *     If the pid is not found in the traceable list,
+ *     return -1 to indicate error.
  */
 static int connect_app(pid_t pid)
 {
-       int sock;
+       int sock, ret;
+
+       ret = find_app_by_pid(pid);
+       if (ret == 0) {
+               return -1;
+       }
 
        sock = ustctl_connect_pid(pid);
        if (sock < 0) {
@@ -302,13 +365,37 @@ error:
 }
 
 /*
- *     find_session
+ *  find_app_by_pid
+ *
+ *  Iterate over the traceable apps list.
+ *  On success, return 1, else return 0
+ */
+static int find_app_by_pid(pid_t pid)
+{
+       struct ltt_traceable_app *iter;
+
+       pthread_mutex_lock(&ltt_traceable_app_list_mutex);
+       cds_list_for_each_entry(iter, &ltt_traceable_app_list.head, list) {
+               if (iter->pid == pid) {
+                       pthread_mutex_unlock(&ltt_traceable_app_list_mutex);
+                       /* Found */
+                       return 1;
+               }
+       }
+       pthread_mutex_unlock(&ltt_traceable_app_list_mutex);
+
+       return 0;
+}
+
+/*
+ *     find_session_by_uuid
  *
  *     Return a ltt_session structure ptr that matches the uuid.
  */
-static struct ltt_session *find_session(uuid_t session_id)
+static struct ltt_session *find_session_by_uuid(uuid_t session_id)
 {
-       struct ltt_session *iter = NULL;
+       int found = 0;
+       struct ltt_session *iter;
 
        /* Sanity check for NULL session_id */
        if (uuid_is_null(session_id)) {
@@ -316,12 +403,41 @@ static struct ltt_session *find_session(uuid_t session_id)
        }
 
        cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
-               if (uuid_compare(iter->uuid, session_id)) {
+               if (uuid_compare(iter->uuid, session_id) == 0) {
+                       found = 1;
                        break;
                }
        }
 
 end:
+       if (!found) {
+               iter = NULL;
+       }
+       return iter;
+}
+
+/*
+ *     find_session_by_name
+ *
+ *     Return a ltt_session structure ptr that matches name.
+ *     If no session found, NULL is returned.
+ */
+static struct ltt_session *find_session_by_name(char *name)
+{
+       int found = 0;
+       struct ltt_session *iter;
+
+       cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
+               if (strncmp(iter->name, name, strlen(iter->name)) == 0) {
+                       found = 1;
+                       break;
+               }
+       }
+
+       if (!found) {
+               iter = NULL;
+       }
+
        return iter;
 }
 
@@ -330,22 +446,25 @@ end:
  *
  *  Delete session from the global session list
  *  and free the memory.
+ *
+ *  Return -1 if no session is found.
+ *  On success, return 1;
  */
-static void destroy_session(uuid_t session_id)
+static int destroy_session(uuid_t *uuid)
 {
-       struct ltt_session *iter = NULL;
+       int found = -1;
+       struct ltt_session *iter;
 
        cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
-               if (uuid_compare(iter->uuid, session_id)) {
-                       cds_list_del(&iter->list);
+               if (uuid_compare(iter->uuid, *uuid) == 0) {
+                       del_session_list(iter);
+                       free(iter);
+                       found = 1;
                        break;
                }
        }
 
-       if (iter) {
-               free(iter);
-               session_count--;
-       }
+       return found;
 }
 
 /*
@@ -358,21 +477,26 @@ static int create_session(char *name, uuid_t *session_id)
 {
        struct ltt_session *new_session;
 
+       new_session = find_session_by_name(name);
+       if (new_session != NULL) {
+               goto error;
+       }
+
        /* Allocate session data structure */
        new_session = malloc(sizeof(struct ltt_session));
        if (new_session == NULL) {
                perror("malloc");
-               goto error;
+               goto error_mem;
        }
 
        if (name != NULL) {
                if (asprintf(&new_session->name, "%s", name) < 0) {
-                       goto error;
+                       goto error_mem;
                }
        } else {
                /* Generate session name based on the session count */
-               if (asprintf(&new_session->name, "%s%d", "auto", session_count) < 0) {
-                       goto error;
+               if (asprintf(&new_session->name, "%s%d", "lttng-", session_count) < 0) {
+                       goto error_mem;
                }
        }
 
@@ -391,14 +515,60 @@ static int create_session(char *name, uuid_t *session_id)
        CDS_INIT_LIST_HEAD(&new_session->lttng_traces);
 
        /* Add new session to the global session list */
-       cds_list_add(&new_session->list, &ltt_session_list.head);
-
-       session_count++;
+       add_session_list(new_session);
 
        return 0;
 
 error:
        return -1;
+
+error_mem:
+       return -ENOMEM;
+}
+
+/*
+ *  ust_create_trace
+ *
+ *  Create an userspace trace using pid.
+ *  This trace is then appended to the current session
+ *  ust trace list.
+ */
+static int ust_create_trace(pid_t pid)
+{
+       int sock, ret;
+       struct ltt_ust_trace *trace;
+
+       trace = malloc(sizeof(struct ltt_ust_trace));
+       if (trace == NULL) {
+               perror("malloc");
+               ret = -1;
+               goto error;
+       }
+
+       /* Init */
+       trace->pid = pid;
+       trace->shmid = 0;
+
+       /* Connect to app using ustctl API */
+       sock = connect_app(pid);
+       if (sock < 0) {
+               ret = LTTCOMM_NO_TRACEABLE;
+               goto error;
+       }
+
+       ret = ustctl_create_trace(sock, "auto");
+       if (ret < 0) {
+               ret = LTTCOMM_CREATE_FAIL;
+               goto error;
+       }
+
+       /* Check if current session is valid */
+       if (current_session) {
+               cds_list_add(&trace->list, &current_session->ust_traces);
+       }
+
+error:
+       return ret;
 }
 
 /*
@@ -412,11 +582,15 @@ static void get_list_apps(pid_t *pids)
        int i = 0;
        struct ltt_traceable_app *iter;
 
-       /* TODO: Mutex needed to access this list */
+       /* Protected by a mutex here because the threads manage_client
+        * and manage_apps can access this list.
+        */
+       pthread_mutex_lock(&ltt_traceable_app_list_mutex);
        cds_list_for_each_entry(iter, &ltt_traceable_app_list.head, list) {
                pids[i] = iter->pid;
                i++;
        }
+       pthread_mutex_unlock(&ltt_traceable_app_list_mutex);
 }
 
 /*
@@ -456,11 +630,12 @@ static void copy_common_data(struct lttcomm_lttng_msg *llm, struct lttcomm_sessi
        llm->pid = lsm->pid;
 
        /* Manage uuid */
-       if (lsm->session_id != NULL) {
-               strncpy(llm->session_id, lsm->session_id, UUID_STR_LEN);
+       if (!uuid_is_null(lsm->session_id)) {
+               uuid_copy(llm->session_id, lsm->session_id);
        }
 
        strncpy(llm->trace_name, lsm->trace_name, strlen(llm->trace_name));
+       llm->trace_name[strlen(llm->trace_name) - 1] = '\0';
 }
 
 /*
@@ -509,6 +684,7 @@ static int process_client_msg(int sock, struct lttcomm_session_msg *lsm)
 {
        int ret;
        int buf_size;
+       size_t header_size;
        char *send_buf = NULL;
        struct lttcomm_lttng_msg llm;
 
@@ -517,38 +693,76 @@ static int process_client_msg(int sock, struct lttcomm_session_msg *lsm)
         */
        copy_common_data(&llm, lsm);
 
+       /* Check command that needs a session */
+       if (lsm->cmd_type != LTTNG_CREATE_SESSION &&
+               lsm->cmd_type != LTTNG_LIST_SESSIONS &&
+               lsm->cmd_type != UST_LIST_APPS)
+       {
+               current_session = find_session_by_uuid(lsm->session_id);
+               if (current_session == NULL) {
+                       ret = LTTCOMM_SELECT_SESS;
+                       goto end;
+               }
+       }
+
        /* Default return code.
         * In our world, everything is OK... right? ;)
         */
        llm.ret_code = LTTCOMM_OK;
 
+       header_size = sizeof(struct lttcomm_lttng_msg);
+
        /* Process by command type */
        switch (lsm->cmd_type) {
                case LTTNG_CREATE_SESSION:
                {
-                       uuid_t uuid;
-                       ret = create_session(lsm->session_name, &uuid);
+                       ret = create_session(lsm->session_name, &llm.session_id);
                        if (ret < 0) {
-                               goto error;
+                               if (ret == -1) {
+                                       ret = LTTCOMM_EXIST_SESS;
+                               } else {
+                                       ret = LTTCOMM_FATAL;
+                               }
+                               goto end;
                        }
 
-                       uuid_unparse(uuid, llm.session_id);
-
                        buf_size = setup_data_buffer(&send_buf, 0, &llm);
                        if (buf_size < 0) {
                                ret = LTTCOMM_FATAL;
-                               goto error;
+                               goto end;
                        }
 
-                       goto send;
                        break;
                }
+               case LTTNG_DESTROY_SESSION:
+               {
+                       ret = destroy_session(&lsm->session_id);
+                       if (ret < 0) {
+                               ret = LTTCOMM_NO_SESS;
+                       } else {
+                               ret = LTTCOMM_OK;
+                       }
+
+                       /* No auxiliary data so only send the llm struct. */
+                       goto end;
+               }
+               case UST_CREATE_TRACE:
+               {
+                       ret = ust_create_trace(lsm->pid);
+                       if (ret < 0) {
+                               ret = LTTCOMM_CREATE_FAIL;
+                               goto end;
+                       }
+
+                       /* No auxiliary data so only send the llm struct. */
+                       goto end;
+               }
                case UST_LIST_APPS:
                {
                        /* Stop right now if no apps */
                        if (traceable_app_count == 0) {
                                ret = LTTCOMM_NO_APPS;
-                               goto error;
+                               goto end;
                        }
 
                        /* Setup data buffer and details for transmission */
@@ -556,12 +770,11 @@ static int process_client_msg(int sock, struct lttcomm_session_msg *lsm)
                                        sizeof(pid_t) * traceable_app_count, &llm);
                        if (buf_size < 0) {
                                ret = LTTCOMM_FATAL;
-                               goto error;
+                               goto end;
                        }
 
-                       get_list_apps((pid_t *)(send_buf + sizeof(struct lttcomm_lttng_msg)));
+                       get_list_apps((pid_t *)(send_buf + header_size));
 
-                       goto send;
                        break;
                }
                case LTTNG_LIST_SESSIONS:
@@ -569,7 +782,7 @@ static int process_client_msg(int sock, struct lttcomm_session_msg *lsm)
                        /* Stop right now if no session */
                        if (session_count == 0) {
                                ret = LTTCOMM_NO_SESS;
-                               goto error;
+                               goto end;
                        }
 
                        /* Setup data buffer and details for transmission */
@@ -577,23 +790,21 @@ static int process_client_msg(int sock, struct lttcomm_session_msg *lsm)
                                        (sizeof(struct lttng_session) * session_count), &llm);
                        if (buf_size < 0) {
                                ret = LTTCOMM_FATAL;
-                               goto error;
+                               goto end;
                        }
 
-                       get_list_sessions((struct lttng_session *)(send_buf + sizeof(struct lttcomm_lttng_msg)));
+                       get_list_sessions((struct lttng_session *)(send_buf + header_size));
 
-                       goto send;
                        break;
                }
                default:
                {
                        /* Undefined command */
                        ret = LTTCOMM_UND;
-                       goto error;
+                       goto end;
                }
        }
 
-send:
        ret = send_unix_sock(sock, send_buf, buf_size);
 
        if (send_buf != NULL) {
@@ -602,13 +813,13 @@ send:
 
        return ret;
 
-error:
+end:
        /* Notify client of error */
        llm.ret_code = ret;
        llm.size_payload = 0;
        send_unix_sock(sock, (void*) &llm, sizeof(llm));
 
-       return -1;
+       return ret;
 }
 
 /*
@@ -616,16 +827,15 @@ error:
  */
 static void usage(void)
 {
-       fprintf(stderr, "Usage:\n%s OPTIONS\n\nOptions:\n"
-                       "\t-h, --help\t\tDisplay this usage.\n"
-                       "\t-c, --client-sock PATH\t\tSpecify path for the client unix socket\n"
-                       "\t-a, --apps-sock PATH\t\tSpecify path for apps unix socket.\n"
-                       "\t-d, --daemonize\t\tStart as a daemon.\n"
-                       "\t-g, --group NAME\t\tSpecify the tracing group name. (default: tracing)\n"
-                       "\t-V, --version\t\tShow version number.\n"
-                       "\t-S, --sig-parent\t\tSend SIGCHLD to parent pid to notify readiness.\n"
-                       "\t-q, --quiet\t\tNo output at all.\n",
-                       progname);
+       fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
+       fprintf(stderr, "  -h, --help                Display this usage.\n");
+       fprintf(stderr, "  -c, --client-sock PATH    Specify path for the client unix socket\n");
+       fprintf(stderr, "  -a, --apps-sock PATH      Specify path for apps unix socket.\n");
+       fprintf(stderr, "  -d, --daemonize           Start as a daemon.\n");
+       fprintf(stderr, "  -g, --group NAME          Specify the tracing group name. (default: tracing)\n");
+       fprintf(stderr, "  -V, --version             Show version number.\n");
+       fprintf(stderr, "  -S, --sig-parent          Send SIGCHLD to parent pid to notify readiness.\n");
+       fprintf(stderr, "  -q, --quiet               No output at all.\n");
 }
 
 /*
@@ -661,7 +871,7 @@ static int parse_args(int argc, char **argv)
                                fprintf(stderr, " with arg %s\n", optarg);
                        }
                        break;
-               case 's':
+               case 'c':
                        snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg);
                        break;
                case 'a':
@@ -856,6 +1066,7 @@ static void sighandler(int sig)
 {
        switch (sig) {
                case SIGPIPE:
+                       return;
                case SIGINT:
                case SIGTERM:
                        cleanup();
@@ -875,8 +1086,8 @@ static void sighandler(int sig)
 static void cleanup()
 {
        /* <fun> */
-       MSG("\n\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
-       MSG("%c[%d;%dm Matthew, BEET driven development works!%c[%dm\n",27,1,33,27,0);
+       MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
+       MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0);
        /* </fun> */
 
        unlink(client_unix_sock_path);
@@ -940,7 +1151,10 @@ int main(int argc, char **argv)
         */
        if ((ret = check_existing_daemon()) == 0) {
                ERR("Already running daemon.\n");
-               goto error;
+               /* We do not goto error because we must not
+                * cleanup() because a daemon is already working.
+                */
+               return EXIT_FAILURE;
        }
 
        if (set_signal_handler() < 0) {
This page took 0.030677 seconds and 4 git commands to generate.