From 379473d2e442e5273e3afb7f71b1d3fb37641d0c Mon Sep 17 00:00:00 2001 From: David Goulet Date: Wed, 27 Apr 2011 14:53:49 -0400 Subject: [PATCH] Fix create trace using the current session Better create trace internal function for the session daemon. Add a check for command that needs a session. Add lttcomm error message for missing session. Signed-off-by: David Goulet --- liblttsessiondcomm/liblttsessiondcomm.c | 3 + liblttsessiondcomm/liblttsessiondcomm.h | 2 + ltt-sessiond/ltt-sessiond.c | 105 +++++++++++++++++++++--- ltt-sessiond/ltt-sessiond.h | 2 +- 4 files changed, 98 insertions(+), 14 deletions(-) diff --git a/liblttsessiondcomm/liblttsessiondcomm.c b/liblttsessiondcomm/liblttsessiondcomm.c index bc74cc9e8..2ab185ce9 100644 --- a/liblttsessiondcomm/liblttsessiondcomm.c +++ b/liblttsessiondcomm/liblttsessiondcomm.c @@ -42,6 +42,9 @@ static const char *lttcomm_readable_code[] = { [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_SESS) ] = "No session found", [ LTTCOMM_ERR_INDEX(LTTCOMM_FATAL) ] = "Fatal error of the session daemon", [ LTTCOMM_ERR_INDEX(LTTCOMM_CREATE_FAIL) ] = "Create trace failed", + [ LTTCOMM_ERR_INDEX(LTTCOMM_START_FAIL) ] = "Start trace failed", + [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_TRACEABLE) ] = "App is not traceable", + [ LTTCOMM_ERR_INDEX(LTTCOMM_SELECT_SESS) ] = "A session MUST be selected", }; /* diff --git a/liblttsessiondcomm/liblttsessiondcomm.h b/liblttsessiondcomm/liblttsessiondcomm.h index 83dcdc377..540241a62 100644 --- a/liblttsessiondcomm/liblttsessiondcomm.h +++ b/liblttsessiondcomm/liblttsessiondcomm.h @@ -76,6 +76,8 @@ enum lttcomm_return_code { LTTCOMM_NO_APPS, /* No traceable application */ LTTCOMM_NO_SESS, /* No sessions available */ LTTCOMM_FATAL, /* Session daemon had a fatal error */ + LTTCOMM_NO_TRACEABLE, /* Error for non traceable app */ + LTTCOMM_SELECT_SESS, /* Must select a session */ LTTCOMM_NR, /* Last element */ }; diff --git a/ltt-sessiond/ltt-sessiond.c b/ltt-sessiond/ltt-sessiond.c index 6df78d8e3..a4baafc07 100644 --- a/ltt-sessiond/ltt-sessiond.c +++ b/ltt-sessiond/ltt-sessiond.c @@ -56,6 +56,7 @@ 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); @@ -157,10 +158,10 @@ static void *thread_manage_apps(void *data) traceable_app_count++; } else { /* Unregistering */ - lta = NULL; cds_list_for_each_entry(lta, <t_traceable_app_list.head, list) { if (lta->pid == reg_msg.pid && lta->uid == reg_msg.uid) { cds_list_del(<a->list); + free(lta); /* Check to not overflow here */ if (traceable_app_count != 0) { traceable_app_count--; @@ -168,11 +169,6 @@ static void *thread_manage_apps(void *data) break; } } - - /* If an item was found, free it from memory */ - if (lta) { - free(lta); - } } } @@ -259,10 +255,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,6 +306,26 @@ error: return ret; } +/* + * 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; + + cds_list_for_each_entry(iter, <t_traceable_app_list.head, list) { + if (iter->pid == pid) { + /* Found */ + return 1; + } + } + + return 0; +} + /* * find_session_by_uuid * @@ -436,6 +460,51 @@ error: return -1; } +/* + * 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, ¤t_session->ust_traces); + } + +error: + return ret; +} + /* * get_list_apps * @@ -553,6 +622,19 @@ 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? ;) */ @@ -589,16 +671,13 @@ static int process_client_msg(int sock, struct lttcomm_session_msg *lsm) } case UST_CREATE_TRACE: { - int sock; - sock = connect_app(lsm->pid); - - ret = ustctl_create_trace(sock, "auto"); + ret = ust_create_trace(lsm->pid); if (ret < 0) { ret = LTTCOMM_CREATE_FAIL; - } else { - ret = LTTCOMM_OK; + goto end; } + /* No auxiliary data so only send the llm struct. */ goto end; } case UST_LIST_APPS: diff --git a/ltt-sessiond/ltt-sessiond.h b/ltt-sessiond/ltt-sessiond.h index 97f8658e1..6c0824b80 100644 --- a/ltt-sessiond/ltt-sessiond.h +++ b/ltt-sessiond/ltt-sessiond.h @@ -39,7 +39,7 @@ struct ltt_lttng_trace { struct ltt_ust_trace { struct cds_list_head list; int shmid; - char trace_name[NAME_MAX]; + pid_t pid; struct cds_list_head markers; }; -- 2.34.1