X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Flib%2Flttng-ctl%2Flttng-ctl.c;h=ff39b88416e4d4410cd5af453a6e92baf4226a10;hp=bbd9e85753e4d30ca708f437b3637143d1296836;hb=780d4bb8ec9b4fa0ffd6d1ba2a2aa5d2f812611f;hpb=b178f53e90c376dd44b020535c32649edef8f80e diff --git a/src/lib/lttng-ctl/lttng-ctl.c b/src/lib/lttng-ctl/lttng-ctl.c index bbd9e8575..ff39b8841 100644 --- a/src/lib/lttng-ctl/lttng-ctl.c +++ b/src/lib/lttng-ctl/lttng-ctl.c @@ -45,6 +45,7 @@ #include #include #include +#include #include "filter/filter-ast.h" #include "filter/filter-parser.h" @@ -68,7 +69,7 @@ do { \ /* Socket to session daemon for communication */ -static int sessiond_socket; +static int sessiond_socket = -1; static char sessiond_sock_path[PATH_MAX]; /* Variables */ @@ -243,15 +244,13 @@ end: LTTNG_HIDDEN int lttng_check_tracing_group(void) { - struct group *grp_tracing; /* no free(). See getgrnam(3) */ - gid_t *grp_list; + gid_t *grp_list, tracing_gid; int grp_list_size, grp_id, i; int ret = -1; const char *grp_name = tracing_group; /* Get GID of group 'tracing' */ - grp_tracing = getgrnam(grp_name); - if (!grp_tracing) { + if (utils_get_group_id(grp_name, false, &tracing_gid)) { /* If grp_tracing is NULL, the group does not exist. */ goto end; } @@ -276,7 +275,7 @@ int lttng_check_tracing_group(void) } for (i = 0; i < grp_list_size; i++) { - if (grp_list[i] == grp_tracing->gr_gid) { + if (grp_list[i] == tracing_gid) { ret = 1; break; } @@ -425,17 +424,12 @@ error: /* * Connect to the LTTng session daemon. * - * On success, return 0. On error, return -1. + * On success, return the socket's file descriptor. On error, return -1. */ -static int connect_sessiond(void) +LTTNG_HIDDEN int connect_sessiond(void) { int ret; - /* Don't try to connect if already connected. */ - if (connected) { - return 0; - } - ret = set_session_daemon_path(); if (ret < 0) { goto error; @@ -447,15 +441,18 @@ static int connect_sessiond(void) goto error; } - sessiond_socket = ret; - connected = 1; - - return 0; + return ret; error: return -1; } +static void reset_global_sessiond_connection_state(void) +{ + sessiond_socket = -1; + connected = 0; +} + /* * Clean disconnect from the session daemon. * @@ -467,8 +464,7 @@ static int disconnect_sessiond(void) if (connected) { ret = lttcomm_close_unix_sock(sessiond_socket); - sessiond_socket = 0; - connected = 0; + reset_global_sessiond_connection_state(); } return ret; @@ -543,6 +539,9 @@ int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg *lsm, if (ret < 0) { ret = -LTTNG_ERR_NO_SESSIOND; goto end; + } else { + sessiond_socket = ret; + connected = 1; } /* Send command to session daemon */ @@ -725,7 +724,7 @@ static int _lttng_stop_tracing(const char *session_name, int wait) * call returned value indicates availability. */ if (data_ret) { - usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME); + usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US); } } while (data_ret != 0); @@ -1080,7 +1079,11 @@ int lttng_enable_event_with_exclusions(struct lttng_handle *handle, unsigned int free_filter_expression = 0; struct filter_parser_ctx *ctx = NULL; - memset(&send_buffer, 0, sizeof(send_buffer)); + /* + * We have either a filter or some exclusions, so we need to set up + * a variable-length memory block from where to send the data. + */ + lttng_dynamic_buffer_init(&send_buffer); /* * Cast as non-const since we may replace the filter expression @@ -1130,12 +1133,6 @@ int lttng_enable_event_with_exclusions(struct lttng_handle *handle, lsm.u.enable.exclusion_count = exclusion_count; lsm.u.enable.bytecode_len = 0; - /* - * We have either a filter or some exclusions, so we need to set up - * a variable-length memory block from where to send the data. - */ - lttng_dynamic_buffer_init(&send_buffer); - /* Parse filter expression. */ if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL || handle->domain.type == LTTNG_DOMAIN_LOG4J @@ -1834,7 +1831,7 @@ end: /* * Create a new session using name and url for destination. * - * Returns LTTNG_OK on success or a negative error code. + * Return 0 on success else a negative LTTng error code. */ int lttng_create_session(const char *name, const char *url) { @@ -1889,7 +1886,7 @@ end: /* * Create a session exclusively used for snapshot. * - * Returns LTTNG_OK on success or a negative error code. + * Return 0 on success else a negative LTTng error code. */ int lttng_create_session_snapshot(const char *name, const char *snapshot_url) { @@ -1965,7 +1962,7 @@ end: /* * Create a session exclusively used for live. * - * Returns LTTNG_OK on success or a negative error code. + * Return 0 on success else a negative LTTng error code. */ int lttng_create_session_live(const char *name, const char *url, unsigned int timer_interval) @@ -1997,45 +1994,49 @@ end: return ret; } -/* - * Destroy session using name. - * Returns size of returned session payload data or a negative error code. - */ -static -int _lttng_destroy_session(const char *session_name) -{ - struct lttcomm_session_msg lsm; - - if (session_name == NULL) { - return -LTTNG_ERR_INVALID; - } - - memset(&lsm, 0, sizeof(lsm)); - lsm.cmd_type = LTTNG_DESTROY_SESSION; - - lttng_ctl_copy_string(lsm.session.name, session_name, - sizeof(lsm.session.name)); - - return lttng_ctl_ask_sessiond(&lsm, NULL); -} - /* * Stop the session and wait for the data before destroying it + * + * Return 0 on success else a negative LTTng error code. */ int lttng_destroy_session(const char *session_name) { int ret; + enum lttng_error_code ret_code; + enum lttng_destruction_handle_status status; + struct lttng_destruction_handle *handle = NULL; /* - * Stop the tracing and wait for the data. + * Stop the tracing and wait for the data to be + * consumed. */ ret = _lttng_stop_tracing(session_name, 1); if (ret && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) { goto end; } - ret = _lttng_destroy_session(session_name); + ret_code = lttng_destroy_session_ext(session_name, &handle); + if (ret_code != LTTNG_OK) { + ret = (int) -ret_code; + goto end; + } + assert(handle); + + /* Block until the completion of the destruction of the session. */ + status = lttng_destruction_handle_wait_for_completion(handle, -1); + if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED) { + ret = -LTTNG_ERR_UNK; + goto end; + } + + status = lttng_destruction_handle_get_result(handle, &ret_code); + if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_OK) { + ret = -LTTNG_ERR_UNK; + goto end; + } + ret = ret_code == LTTNG_OK ? 0 : -ret_code; end: + lttng_destruction_handle_destroy(handle); return ret; } @@ -2044,21 +2045,10 @@ end: */ int lttng_destroy_session_no_wait(const char *session_name) { - int ret; - - /* - * Stop the tracing without waiting for the data. - * The session might already have been stopped, so just - * skip this error. - */ - ret = _lttng_stop_tracing(session_name, 0); - if (ret && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) { - goto end; - } + enum lttng_error_code ret_code; - ret = _lttng_destroy_session(session_name); -end: - return ret; + ret_code = lttng_destroy_session_ext(session_name, NULL); + return ret_code == LTTNG_OK ? ret_code : -ret_code; } /* @@ -2081,7 +2071,6 @@ int lttng_list_sessions(struct lttng_session **out_sessions) lsm.cmd_type = LTTNG_LIST_SESSIONS; ret = lttng_ctl_ask_sessiond(&lsm, (void**) &sessions); if (ret <= 0) { - ret = ret == 0 ? -LTTNG_ERR_FATAL : ret; goto end; } if (!sessions) {