X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=ltt-sessiond%2Fmain.c;h=f5431bc0ae0299102b1d52b7c42bbc4f4b68de30;hp=f2205e4b12edd2f39f407f8be74afae8aa0ee992;hb=471d16937e160d99382c633db37d89605f483556;hpb=d6f421500e248dee6c9b8ccac863a3840d4fbff6 diff --git a/ltt-sessiond/main.c b/ltt-sessiond/main.c index f2205e4b1..f5431bc0a 100644 --- a/ltt-sessiond/main.c +++ b/ltt-sessiond/main.c @@ -52,18 +52,18 @@ const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE; /* Static functions */ static int check_existing_daemon(void); -static int connect_app(pid_t pid); +static int ust_connect_app(pid_t pid); static int init_daemon_socket(void); static int notify_apps(const char* name); 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 set_signal_handler(void); static int set_permissions(void); -static int setup_data_buffer(char **buf, size_t size, struct lttcomm_lttng_msg *llm); +static int setup_data_buffer(char **buf, size_t size, struct lttcomm_lttng_header *llh); static int create_lttng_rundir(void); static int set_kconsumerd_sockets(void); static void cleanup(void); -static void copy_common_data(struct lttcomm_lttng_msg *llm, struct lttcomm_session_msg *lsm); +static void copy_common_data(struct lttcomm_lttng_header *llh, struct lttcomm_session_msg *lsm); static void sighandler(int sig); static void *thread_manage_clients(void *data); @@ -89,7 +89,8 @@ static int client_sock; static int apps_sock; static int kconsumerd_err_sock; -static struct ltt_session *current_session; +/* Extern in session.h */ +struct ltt_session *current_session; /* * thread_manage_kconsumerd @@ -232,7 +233,7 @@ static void *thread_manage_clients(void *data) /* This function dispatch the work to the LTTng or UST libs * and then sends back the response to the client. This is needed - * because there might be more then one lttcomm_lttng_msg to + * because there might be more then one lttcomm_lttng_header to * send out so process_client_msg do both jobs. */ ret = process_client_msg(sock, &lsm); @@ -264,7 +265,7 @@ static int send_unix_sock(int sock, void *buf, size_t len) } /* - * connect_app + * ust_connect_app * * Return a socket connected to the libust communication socket * of the application identified by the pid. @@ -272,7 +273,7 @@ static int send_unix_sock(int sock, void *buf, size_t len) * If the pid is not found in the traceable list, * return -1 to indicate error. */ -static int connect_app(pid_t pid) +static int ust_connect_app(pid_t pid) { int sock; struct ltt_traceable_app *lta; @@ -326,148 +327,23 @@ error: return ret; } -/* - * 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; - - DBG("Creating trace for pid %d", pid); - - trace = malloc(sizeof(struct ltt_ust_trace)); - if (trace == NULL) { - perror("malloc"); - ret = -1; - goto error; - } - - /* Init */ - trace->pid = pid; - trace->shmid = 0; - /* NOTE: to be removed. Trace name will no longer be - * required for LTTng userspace tracer. For now, we set it - * to 'auto' for API compliance. - */ - snprintf(trace->name, 5, "auto"); - - /* Connect to app using ustctl API */ - sock = connect_app(pid); - if (sock < 0) { - ret = LTTCOMM_NO_TRACEABLE; - goto error; - } - - ret = ustctl_create_trace(sock, trace->name); - 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); - current_session->ust_trace_count++; - } - -error: - return ret; -} - -/* - * ust_start_trace - * - * Start a trace. This trace, identified by the pid, must be - * in the current session ust_traces list. - */ -static int ust_start_trace(pid_t pid) -{ - int sock, ret; - struct ltt_ust_trace *trace; - - DBG("Starting trace for pid %d", pid); - - trace = find_session_ust_trace_by_pid(current_session, pid); - if (trace == NULL) { - ret = LTTCOMM_NO_TRACE; - goto error; - } - - /* Connect to app using ustctl API */ - sock = connect_app(pid); - if (sock < 0) { - ret = LTTCOMM_NO_TRACEABLE; - goto error; - } - - ret = ustctl_start_trace(sock, "auto"); - if (ret < 0) { - ret = LTTCOMM_START_FAIL; - goto error; - } - -error: - return ret; -} - -/* - * ust_stop_trace - * - * Stop a trace. This trace, identified by the pid, must be - * in the current session ust_traces list. - */ -static int ust_stop_trace(pid_t pid) -{ - int sock, ret; - struct ltt_ust_trace *trace; - - DBG("Stopping trace for pid %d", pid); - - trace = find_session_ust_trace_by_pid(current_session, pid); - if (trace == NULL) { - ret = LTTCOMM_NO_TRACE; - goto error; - } - - /* Connect to app using ustctl API */ - sock = connect_app(pid); - if (sock < 0) { - ret = LTTCOMM_NO_TRACEABLE; - goto error; - } - - ret = ustctl_stop_trace(sock, trace->name); - if (ret < 0) { - ret = LTTCOMM_STOP_FAIL; - goto error; - } - -error: - return ret; -} - /* * copy_common_data * - * Copy common data between lttcomm_lttng_msg and lttcomm_session_msg + * Copy common data between lttcomm_lttng_header and lttcomm_session_msg */ -static void copy_common_data(struct lttcomm_lttng_msg *llm, struct lttcomm_session_msg *lsm) +static void copy_common_data(struct lttcomm_lttng_header *llh, struct lttcomm_session_msg *lsm) { - llm->cmd_type = lsm->cmd_type; - llm->pid = lsm->pid; + llh->cmd_type = lsm->cmd_type; + llh->pid = lsm->pid; /* Manage uuid */ if (!uuid_is_null(lsm->session_id)) { - uuid_copy(llm->session_id, lsm->session_id); + uuid_copy(llh->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'; + strncpy(llh->trace_name, lsm->trace_name, strlen(llh->trace_name)); + llh->trace_name[strlen(llh->trace_name) - 1] = '\0'; } /* @@ -478,12 +354,12 @@ static void copy_common_data(struct lttcomm_lttng_msg *llm, struct lttcomm_sessi * * Return total size of the buffer pointed by buf. */ -static int setup_data_buffer(char **buf, size_t s_data, struct lttcomm_lttng_msg *llm) +static int setup_data_buffer(char **buf, size_t s_data, struct lttcomm_lttng_header *llh) { int ret = 0; size_t buf_size; - buf_size = sizeof(struct lttcomm_lttng_msg) + s_data; + buf_size = sizeof(struct lttcomm_lttng_header) + s_data; *buf = malloc(buf_size); if (*buf == NULL) { perror("malloc"); @@ -491,11 +367,11 @@ static int setup_data_buffer(char **buf, size_t s_data, struct lttcomm_lttng_msg goto error; } - /* Setup lttcomm_lttng_msg data and copy + /* Setup lttcomm_lttng_header data and copy * it to the newly allocated buffer. */ - llm->size_payload = s_data; - memcpy(*buf, llm, sizeof(struct lttcomm_lttng_msg)); + llh->payload_size = s_data; + memcpy(*buf, llh, sizeof(struct lttcomm_lttng_header)); return buf_size; @@ -514,18 +390,17 @@ error: */ static int process_client_msg(int sock, struct lttcomm_session_msg *lsm) { - int ret; - int buf_size; + int ust_sock, ret, buf_size; size_t header_size; char *send_buf = NULL; - struct lttcomm_lttng_msg llm; + struct lttcomm_lttng_header llh; DBG("Processing client message"); /* Copy common data to identify the response * on the lttng client side. */ - copy_common_data(&llm, lsm); + copy_common_data(&llh, lsm); /* Check command that needs a session */ if (lsm->cmd_type != LTTNG_CREATE_SESSION && @@ -542,15 +417,25 @@ static int process_client_msg(int sock, struct lttcomm_session_msg *lsm) /* Default return code. * In our world, everything is OK... right? ;) */ - llm.ret_code = LTTCOMM_OK; + llh.ret_code = LTTCOMM_OK; - header_size = sizeof(struct lttcomm_lttng_msg); + header_size = sizeof(struct lttcomm_lttng_header); + + /* Connect to ust apps if available pid */ + if (lsm->pid != 0) { + /* Connect to app using ustctl API */ + ust_sock = ust_connect_app(lsm->pid); + if (ust_sock < 0) { + ret = LTTCOMM_NO_TRACEABLE; + goto end; + } + } /* Process by command type */ switch (lsm->cmd_type) { case LTTNG_CREATE_SESSION: { - ret = create_session(lsm->session_name, &llm.session_id); + ret = create_session(lsm->session_name, &llh.session_id); if (ret < 0) { if (ret == -1) { ret = LTTCOMM_EXIST_SESS; @@ -560,7 +445,7 @@ static int process_client_msg(int sock, struct lttcomm_session_msg *lsm) goto end; } - buf_size = setup_data_buffer(&send_buf, 0, &llm); + buf_size = setup_data_buffer(&send_buf, 0, &llh); if (buf_size < 0) { ret = LTTCOMM_FATAL; goto end; @@ -577,7 +462,7 @@ static int process_client_msg(int sock, struct lttcomm_session_msg *lsm) ret = LTTCOMM_OK; } - /* No auxiliary data so only send the llm struct. */ + /* No auxiliary data so only send the llh struct. */ goto end; } case LTTNG_LIST_TRACES: @@ -590,7 +475,7 @@ static int process_client_msg(int sock, struct lttcomm_session_msg *lsm) } buf_size = setup_data_buffer(&send_buf, - sizeof(struct lttng_trace) * trace_count, &llm); + sizeof(struct lttng_trace) * trace_count, &llh); if (buf_size < 0) { ret = LTTCOMM_FATAL; goto end; @@ -601,7 +486,7 @@ static int process_client_msg(int sock, struct lttcomm_session_msg *lsm) } case UST_CREATE_TRACE: { - ret = ust_create_trace(lsm->pid); + ret = ust_create_trace(ust_sock, lsm->pid); if (ret < 0) { /* If -1 is returned from ust_create_trace, malloc * failed so it's pretty much a fatal error. @@ -610,7 +495,7 @@ static int process_client_msg(int sock, struct lttcomm_session_msg *lsm) goto end; } - /* No auxiliary data so only send the llm struct. */ + /* No auxiliary data so only send the llh struct. */ goto end; } case UST_LIST_APPS: @@ -624,7 +509,7 @@ static int process_client_msg(int sock, struct lttcomm_session_msg *lsm) /* Setup data buffer and details for transmission */ buf_size = setup_data_buffer(&send_buf, - sizeof(pid_t) * app_count, &llm); + sizeof(pid_t) * app_count, &llh); if (buf_size < 0) { ret = LTTCOMM_FATAL; goto end; @@ -636,16 +521,16 @@ static int process_client_msg(int sock, struct lttcomm_session_msg *lsm) } case UST_START_TRACE: { - ret = ust_start_trace(lsm->pid); + ret = ust_start_trace(ust_sock, lsm->pid); - /* No auxiliary data so only send the llm struct. */ + /* No auxiliary data so only send the llh struct. */ goto end; } case UST_STOP_TRACE: { - ret = ust_stop_trace(lsm->pid); + ret = ust_stop_trace(ust_sock, lsm->pid); - /* No auxiliary data so only send the llm struct. */ + /* No auxiliary data so only send the llh struct. */ goto end; } case LTTNG_LIST_SESSIONS: @@ -659,7 +544,7 @@ static int process_client_msg(int sock, struct lttcomm_session_msg *lsm) /* Setup data buffer and details for transmission */ buf_size = setup_data_buffer(&send_buf, - (sizeof(struct lttng_session) * session_count), &llm); + (sizeof(struct lttng_session) * session_count), &llh); if (buf_size < 0) { ret = LTTCOMM_FATAL; goto end; @@ -688,9 +573,9 @@ static int process_client_msg(int sock, struct lttcomm_session_msg *lsm) end: DBG("Return code to client %d", ret); /* Notify client of error */ - llm.ret_code = ret; - llm.size_payload = 0; - send_unix_sock(sock, (void*) &llm, sizeof(llm)); + llh.ret_code = ret; + llh.payload_size = 0; + send_unix_sock(sock, (void*) &llh, sizeof(llh)); return ret; }