X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=src%2Fbin%2Flttng-relayd%2Flive.c;h=9bb719ef607bc1d2c0777dda1a8f557bddd0afb9;hb=f465e9787f5b70d4a8f3761c60b8d6e4161e9628;hp=bafdf35ce90034a90e8ee0e9eaa1a8996103de9e;hpb=9237e6a108fdba7acc014f739d0569565552bdec;p=lttng-tools.git diff --git a/src/bin/lttng-relayd/live.c b/src/bin/lttng-relayd/live.c index bafdf35ce..9bb719ef6 100644 --- a/src/bin/lttng-relayd/live.c +++ b/src/bin/lttng-relayd/live.c @@ -17,7 +17,6 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#define _GNU_SOURCE #define _LGPL_SOURCE #include #include @@ -40,7 +39,6 @@ #include #include #include -#include #include #include @@ -49,12 +47,12 @@ #include #include #include -#include #include #include #include #include #include +#include #include "cmd.h" #include "live.h" @@ -68,6 +66,7 @@ #include "ctf-trace.h" #include "connection.h" #include "viewer-session.h" +#include "index-file.h" #define SESSION_BUF_DEFAULT_COUNT 16 @@ -232,10 +231,21 @@ ssize_t send_viewer_streams(struct lttcomm_sock *sock, send_stream.ctf_trace_id = htobe64(ctf_trace->id); send_stream.metadata_flag = htobe32( vstream->stream->is_metadata); - strncpy(send_stream.path_name, vstream->path_name, - sizeof(send_stream.path_name)); - strncpy(send_stream.channel_name, vstream->channel_name, - sizeof(send_stream.channel_name)); + if (lttng_strncpy(send_stream.path_name, vstream->path_name, + sizeof(send_stream.path_name))) { + pthread_mutex_unlock(&vstream->stream->lock); + viewer_stream_put(vstream); + ret = -1; /* Error. */ + goto end_unlock; + } + if (lttng_strncpy(send_stream.channel_name, + vstream->channel_name, + sizeof(send_stream.channel_name))) { + pthread_mutex_unlock(&vstream->stream->lock); + viewer_stream_put(vstream); + ret = -1; /* Error. */ + goto end_unlock; + } DBG("Sending stream %" PRIu64 " to viewer", vstream->stream->stream_handle); @@ -307,24 +317,11 @@ int make_viewer_streams(struct relay_session *session, continue; } /* - * stream published is protected by the session - * lock. + * stream published is protected by the session lock. */ if (!stream->published) { goto next; } - /* - * Stream has no data, don't consider it yet. - */ - if (stream->is_metadata) { - if (!stream->metadata_received) { - goto next; - } - } else { - if (stream->prev_seq == -1ULL) { - goto next; - } - } vstream = viewer_stream_get_by_id(stream->stream_handle); if (!vstream) { vstream = viewer_stream_create(stream, seek_t); @@ -343,7 +340,10 @@ int make_viewer_streams(struct relay_session *session, * Ensure a self-reference is preserved even * after we have put our local reference. */ - viewer_stream_get(vstream); + if (!viewer_stream_get(vstream)) { + ERR("Unable to get self-reference on viewer stream, logic error."); + abort(); + } } else { if (!vstream->sent_flag && nb_unsent) { /* Update number of unsent stream counter. */ @@ -393,7 +393,8 @@ int relayd_live_stop(void) * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set. */ static -int create_thread_poll_set(struct lttng_poll_event *events, int size) +int create_named_thread_poll_set(struct lttng_poll_event *events, + int size, const char *name) { int ret; @@ -402,10 +403,8 @@ int create_thread_poll_set(struct lttng_poll_event *events, int size) goto error; } - ret = lttng_poll_create(events, size, LTTNG_CLOEXEC); - if (ret < 0) { - goto error; - } + ret = fd_tracker_util_poll_create(the_fd_tracker, + name, events, 1, LTTNG_CLOEXEC); /* Add quit pipe */ ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN | LPOLLERR); @@ -434,14 +433,78 @@ int check_thread_quit_pipe(int fd, uint32_t events) return 0; } +static +int create_sock(void *data, int *out_fd) +{ + int ret; + struct lttcomm_sock *sock = data; + + ret = lttcomm_create_sock(sock); + if (ret < 0) { + goto end; + } + + *out_fd = sock->fd; +end: + return ret; +} + +static +int close_sock(void *data, int *in_fd) +{ + struct lttcomm_sock *sock = data; + + return sock->ops->close(sock); +} + +static int accept_sock(void *data, int *out_fd) +{ + int ret = 0; + /* Socks is an array of in_sock, out_sock. */ + struct lttcomm_sock **socks = data; + struct lttcomm_sock *in_sock = socks[0]; + + socks[1] = in_sock->ops->accept(in_sock); + if (!socks[1]) { + ret = -1; + goto end; + } + *out_fd = socks[1]->fd; +end: + return ret; +} + +static +struct lttcomm_sock *accept_live_sock(struct lttcomm_sock *listening_sock, + const char *name) +{ + int out_fd, ret; + struct lttcomm_sock *socks[2] = { listening_sock, NULL }; + struct lttcomm_sock *new_sock = NULL; + + ret = fd_tracker_open_unsuspendable_fd( + the_fd_tracker, &out_fd, + (const char **) &name, + 1, accept_sock, &socks); + if (ret) { + goto end; + } + new_sock = socks[1]; + DBG("%s accepted, socket %d", name, new_sock->fd); +end: + return new_sock; +} + /* * Create and init socket from uri. */ static -struct lttcomm_sock *init_socket(struct lttng_uri *uri) +struct lttcomm_sock *init_socket(struct lttng_uri *uri, const char *name) { - int ret; + int ret, sock_fd; struct lttcomm_sock *sock = NULL; + char uri_str[PATH_MAX]; + char *formated_name = NULL; sock = lttcomm_alloc_sock_from_uri(uri); if (sock == NULL) { @@ -449,11 +512,25 @@ struct lttcomm_sock *init_socket(struct lttng_uri *uri) goto error; } - ret = lttcomm_create_sock(sock); - if (ret < 0) { - goto error; + /* + * Don't fail to create the socket if the name can't be built as it is + * only used for debugging purposes. + */ + ret = uri_to_str_url(uri, uri_str, sizeof(uri_str)); + uri_str[sizeof(uri_str) - 1] = '\0'; + if (ret >= 0) { + ret = asprintf(&formated_name, "%s socket @ %s", name, + uri_str); + if (ret < 0) { + formated_name = NULL; + } } - DBG("Listening on sock %d for live", sock->fd); + + ret = fd_tracker_open_unsuspendable_fd(the_fd_tracker, &sock_fd, + (const char **) (formated_name ? &formated_name : NULL), + 1, create_sock, sock); + free(formated_name); + DBG("Listening on %s socket %d", name, sock->fd); ret = sock->ops->bind(sock); if (ret < 0) { @@ -492,13 +569,14 @@ void *thread_listener(void *data) health_code_update(); - live_control_sock = init_socket(live_uri); + live_control_sock = init_socket(live_uri, "Live listener"); if (!live_control_sock) { goto error_sock_control; } /* Pass 2 as size here for the thread quit pipe and control sockets. */ - ret = create_thread_poll_set(&events, 2); + ret = create_named_thread_poll_set(&events, 2, + "Live listener thread epoll"); if (ret < 0) { goto error_create_poll; } @@ -555,10 +633,7 @@ restart: goto exit; } - if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { - ERR("socket poll error"); - goto error; - } else if (revents & LPOLLIN) { + if (revents & LPOLLIN) { /* * A new connection is requested, therefore a * viewer connection is allocated in this @@ -569,7 +644,8 @@ restart: struct relay_connection *new_conn; struct lttcomm_sock *newsock; - newsock = live_control_sock->ops->accept(live_control_sock); + newsock = accept_live_sock(live_control_sock, + "Live socket to client"); if (!newsock) { PERROR("accepting control sock"); goto error; @@ -601,6 +677,12 @@ restart: * exchange in cds_wfcq_enqueue. */ futex_nto1_wake(&viewer_conn_queue.futex); + } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { + ERR("socket poll error"); + goto error; + } else { + ERR("Unexpected poll events %u for sock %d", revents, pollfd); + goto error; } } } @@ -609,10 +691,12 @@ exit: error: error_poll_add: error_testpoint: - lttng_poll_clean(&events); + (void) fd_tracker_util_poll_clean(the_fd_tracker, &events); error_create_poll: if (live_control_sock->fd >= 0) { - ret = live_control_sock->ops->close(live_control_sock); + ret = fd_tracker_close_unsuspendable_fd(the_fd_tracker, + &live_control_sock->fd, 1, close_sock, + live_control_sock); if (ret) { PERROR("close"); } @@ -652,12 +736,16 @@ void *thread_dispatcher(void *data) health_code_update(); - while (!CMM_LOAD_SHARED(live_dispatch_thread_exit)) { + for (;;) { health_code_update(); /* Atomically prepare the queue futex */ futex_nto1_prepare(&viewer_conn_queue.futex); + if (CMM_LOAD_SHARED(live_dispatch_thread_exit)) { + break; + } + do { health_code_update(); @@ -806,7 +894,7 @@ end: static int viewer_list_sessions(struct relay_connection *conn) { - int ret; + int ret = 0; struct lttng_viewer_list_sessions session_list; struct lttng_ht_iter iter; struct relay_session *session; @@ -836,17 +924,23 @@ int viewer_list_sessions(struct relay_connection *conn) new_buf_count * sizeof(*send_session_buf)); if (!newbuf) { ret = -1; - rcu_read_unlock(); - goto end_free; + break; } send_session_buf = newbuf; buf_count = new_buf_count; } send_session = &send_session_buf[count]; - strncpy(send_session->session_name, session->session_name, - sizeof(send_session->session_name)); - strncpy(send_session->hostname, session->hostname, - sizeof(send_session->hostname)); + if (lttng_strncpy(send_session->session_name, + session->session_name, + sizeof(send_session->session_name))) { + ret = -1; + break; + } + if (lttng_strncpy(send_session->hostname, session->hostname, + sizeof(send_session->hostname))) { + ret = -1; + break; + } send_session->id = htobe64(session->id); send_session->live_timer = htobe32(session->live_timer); if (session->viewer_attached) { @@ -858,6 +952,9 @@ int viewer_list_sessions(struct relay_connection *conn) count++; } rcu_read_unlock(); + if (ret < 0) { + goto end_free; + } session_list.sessions_count = htobe32(count); @@ -1023,12 +1120,12 @@ int viewer_attach_session(struct relay_connection *conn) session = session_get_by_id(be64toh(request.session_id)); if (!session) { DBG("Relay session %" PRIu64 " not found", - be64toh(request.session_id)); + (uint64_t) be64toh(request.session_id)); response.status = htobe32(LTTNG_VIEWER_ATTACH_UNK); goto send_reply; } DBG("Attach session ID %" PRIu64 " received", - be64toh(request.session_id)); + (uint64_t) be64toh(request.session_id)); if (session->live_timer == 0) { DBG("Not live session"); @@ -1111,8 +1208,8 @@ error: /* * Open the index file if needed for the given vstream. * - * If an index file is successfully opened, the vstream index_fd set with - * it. + * If an index file is successfully opened, the vstream will set it as its + * current index file. * * Return 0 on success, a negative value on error (-ENOENT if not ready yet). * @@ -1123,31 +1220,23 @@ static int try_open_index(struct relay_viewer_stream *vstream, { int ret = 0; - if (vstream->index_fd) { + if (vstream->index_file) { goto end; } /* * First time, we open the index file and at least one index is ready. */ - if (rstream->total_index_received == 0) { + if (rstream->index_received_seqcount == 0) { ret = -ENOENT; goto end; } - ret = index_open(vstream->path_name, vstream->channel_name, + vstream->index_file = relay_index_file_open(vstream->path_name, + vstream->channel_name, vstream->stream->tracefile_count, vstream->current_tracefile_id); - if (ret >= 0) { - vstream->index_fd = stream_fd_create(ret); - if (!vstream->index_fd) { - if (close(ret)) { - PERROR("close"); - } - ret = -1; - } else { - ret = 0; - } - goto end; + if (!vstream->index_file) { + ret = -1; } end: @@ -1172,14 +1261,14 @@ static int check_index_status(struct relay_viewer_stream *vstream, int ret; if (trace->session->connection_closed - && rstream->total_index_received - == vstream->last_sent_index) { + && rstream->index_received_seqcount + == vstream->index_sent_seqcount) { /* Last index sent and session connection is closed. */ index->status = htobe32(LTTNG_VIEWER_INDEX_HUP); goto hup; } else if (rstream->beacon_ts_end != -1ULL && - rstream->total_index_received - == vstream->last_sent_index) { + rstream->index_received_seqcount + == vstream->index_sent_seqcount) { /* * We've received a synchronization beacon and the last index * available has been sent, the index for now is inactive. @@ -1193,21 +1282,24 @@ static int check_index_status(struct relay_viewer_stream *vstream, index->timestamp_end = htobe64(rstream->beacon_ts_end); index->stream_id = htobe64(rstream->ctf_stream_id); goto index_ready; - } else if (rstream->total_index_received <= vstream->last_sent_index) { + } else if (rstream->index_received_seqcount + == vstream->index_sent_seqcount) { /* - * This actually checks the case where recv == last_sent. - * In this case, we have not received a beacon. Therefore, we - * can only ask the client to retry later. + * This checks whether received == sent seqcount. In + * this case, we have not received a beacon. Therefore, + * we can only ask the client to retry later. */ index->status = htobe32(LTTNG_VIEWER_INDEX_RETRY); goto index_ready; - } else if (!viewer_stream_is_tracefile_seq_readable(vstream, - vstream->current_tracefile_seq)) { + } else if (!tracefile_array_seq_in_file(rstream->tfa, + vstream->current_tracefile_id, + vstream->index_sent_seqcount)) { /* - * The producer has overwritten our current file. We - * need to rotate. + * The next index we want to send cannot be read either + * because we need to perform a rotation, or due to + * the producer having overwritten its trace file. */ - DBG("Viewer stream %" PRIu64 " rotation due to overwrite", + DBG("Viewer stream %" PRIu64 " rotation", vstream->stream->stream_handle); ret = viewer_stream_rotate(vstream); if (ret < 0) { @@ -1217,50 +1309,34 @@ static int check_index_status(struct relay_viewer_stream *vstream, index->status = htobe32(LTTNG_VIEWER_INDEX_HUP); goto hup; } - assert(viewer_stream_is_tracefile_seq_readable(vstream, - vstream->current_tracefile_seq)); - /* ret == 0 means successful so we continue. */ - ret = 0; - } else { - ssize_t read_ret; - char tmp[1]; - /* - * Use EOF on current index file to find out when we - * need to rotate. + * If we have been pushed due to overwrite, it + * necessarily means there is data that can be read in + * the stream. If we rotated because we reached the end + * of a tracefile, it means the following tracefile + * needs to contain at least one index, else we would + * have already returned LTTNG_VIEWER_INDEX_RETRY to the + * viewer. The updated index_sent_seqcount needs to + * point to a readable index entry now. + * + * In the case where we "rotate" on a single file, we + * can end up in a case where the requested index is + * still unavailable. */ - read_ret = lttng_read(vstream->index_fd->fd, tmp, 1); - if (read_ret == 1) { - off_t seek_ret; - - /* There is still data to read. Rewind position. */ - seek_ret = lseek(vstream->index_fd->fd, -1, SEEK_CUR); - if (seek_ret < 0) { - ret = -1; - goto end; - } - ret = 0; - } else if (read_ret == 0) { - /* EOF. We need to rotate. */ - DBG("Viewer stream %" PRIu64 " rotation due to EOF", - vstream->stream->stream_handle); - ret = viewer_stream_rotate(vstream); - if (ret < 0) { - goto end; - } else if (ret == 1) { - /* EOF across entire stream. */ - index->status = htobe32(LTTNG_VIEWER_INDEX_HUP); - goto hup; - } - assert(viewer_stream_is_tracefile_seq_readable(vstream, - vstream->current_tracefile_seq)); - /* ret == 0 means successful so we continue. */ - ret = 0; - } else { - /* Error reading index. */ - ret = -1; + if (rstream->tracefile_count == 1 && + !tracefile_array_seq_in_file( + rstream->tfa, + vstream->current_tracefile_id, + vstream->index_sent_seqcount)) { + index->status = htobe32(LTTNG_VIEWER_INDEX_RETRY); + goto index_ready; } + assert(tracefile_array_seq_in_file(rstream->tfa, + vstream->current_tracefile_id, + vstream->index_sent_seqcount)); } + /* ret == 0 means successful so we continue. */ + ret = 0; end: return ret; @@ -1279,7 +1355,6 @@ static int viewer_get_next_index(struct relay_connection *conn) { int ret; - ssize_t read_ret; struct lttng_viewer_get_next_index request_index; struct lttng_viewer_index viewer_index; struct ctf_packet_index packet_index; @@ -1303,6 +1378,8 @@ int viewer_get_next_index(struct relay_connection *conn) vstream = viewer_stream_get_by_id(be64toh(request_index.stream_id)); if (!vstream) { + DBG("Client requested index of unknown stream id %" PRIu64, + (uint64_t) be64toh(request_index.stream_id)); viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR); goto send_reply; } @@ -1378,12 +1455,8 @@ int viewer_get_next_index(struct relay_connection *conn) if (ret < 0) { goto error_put; } - ret = open(fullpath, O_RDONLY); - if (ret < 0) { - PERROR("Relay opening trace file"); - goto error_put; - } - vstream->stream_fd = stream_fd_create(ret); + + vstream->stream_fd = stream_fd_open(fullpath); if (!vstream->stream_fd) { if (close(ret)) { PERROR("close"); @@ -1400,16 +1473,14 @@ int viewer_get_next_index(struct relay_connection *conn) viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_STREAM; } - read_ret = lttng_read(vstream->index_fd->fd, &packet_index, - sizeof(packet_index)); - if (read_ret < sizeof(packet_index)) { - ERR("Relay reading index file %d returned %zd", - vstream->index_fd->fd, read_ret); + ret = relay_index_file_read(vstream->index_file, &packet_index); + if (ret) { + ERR("Relay error reading index file"); viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR); goto send_reply; } else { viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_OK); - vstream->last_sent_index++; + vstream->index_sent_seqcount++; } /* @@ -1417,7 +1488,7 @@ int viewer_get_next_index(struct relay_connection *conn) */ DBG("Sending viewer index for stream %" PRIu64 " offset %" PRIu64, rstream->stream_handle, - be64toh(packet_index.offset)); + (uint64_t) be64toh(packet_index.offset)); viewer_index.offset = packet_index.offset; viewer_index.packet_size = packet_index.packet_size; viewer_index.content_size = packet_index.content_size; @@ -1456,7 +1527,7 @@ send_reply: if (vstream) { DBG("Index %" PRIu64 " for stream %" PRIu64 " sent", - vstream->last_sent_index, + vstream->index_sent_seqcount, vstream->stream->stream_handle); } end: @@ -1485,13 +1556,15 @@ error_put: static int viewer_get_packet(struct relay_connection *conn) { - int ret, send_data = 0; - char *data = NULL; - uint32_t len = 0; - ssize_t read_len; + int ret, stream_fd; + off_t lseek_ret; + char *reply = NULL; struct lttng_viewer_get_packet get_packet_info; - struct lttng_viewer_trace_packet reply; + struct lttng_viewer_trace_packet reply_header; struct relay_viewer_stream *vstream = NULL; + uint32_t reply_size = sizeof(reply_header); + uint32_t packet_data_len = 0; + ssize_t read_len; DBG2("Relay get data packet"); @@ -1505,74 +1578,84 @@ int viewer_get_packet(struct relay_connection *conn) health_code_update(); /* From this point on, the error label can be reached. */ - memset(&reply, 0, sizeof(reply)); + memset(&reply_header, 0, sizeof(reply_header)); vstream = viewer_stream_get_by_id(be64toh(get_packet_info.stream_id)); if (!vstream) { - reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_ERR); + DBG("Client requested packet of unknown stream id %" PRIu64, + (uint64_t) be64toh(get_packet_info.stream_id)); + reply_header.status = htobe32(LTTNG_VIEWER_GET_PACKET_ERR); goto send_reply_nolock; + } else { + packet_data_len = be32toh(get_packet_info.len); + reply_size += packet_data_len; } - pthread_mutex_lock(&vstream->stream->lock); - - len = be32toh(get_packet_info.len); - data = zmalloc(len); - if (!data) { - PERROR("relay data zmalloc"); + reply = zmalloc(reply_size); + if (!reply) { + PERROR("packet reply zmalloc"); + reply_size = sizeof(reply_header); goto error; } - ret = lseek(vstream->stream_fd->fd, be64toh(get_packet_info.offset), - SEEK_SET); - if (ret < 0) { - PERROR("lseek fd %d to offset %" PRIu64, vstream->stream_fd->fd, - be64toh(get_packet_info.offset)); - goto error; - } - read_len = lttng_read(vstream->stream_fd->fd, data, len); - if (read_len < len) { + pthread_mutex_lock(&vstream->stream->lock); + stream_fd = stream_fd_get_fd(vstream->stream_fd); + if (stream_fd < 0) { + ERR("Failed to get viewer stream file descriptor"); + goto error_put_fd; + } + lseek_ret = lseek(stream_fd, be64toh(get_packet_info.offset), SEEK_SET); + if (lseek_ret < 0) { + PERROR("lseek fd %d to offset %" PRIu64, stream_fd, + (uint64_t) be64toh(get_packet_info.offset)); + goto error_put_fd; + } + read_len = lttng_read(stream_fd, + reply + sizeof(reply_header), + packet_data_len); + if (read_len < packet_data_len) { PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64, - vstream->stream_fd->fd, - be64toh(get_packet_info.offset)); - goto error; + stream_fd, + (uint64_t) be64toh(get_packet_info.offset)); + goto error_put_fd; } - reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_OK); - reply.len = htobe32(len); - send_data = 1; + reply_header.status = htobe32(LTTNG_VIEWER_GET_PACKET_OK); + reply_header.len = htobe32(packet_data_len); goto send_reply; +error_put_fd: + stream_fd_put_fd(vstream->stream_fd); error: - reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_ERR); + reply_header.status = htobe32(LTTNG_VIEWER_GET_PACKET_ERR); send_reply: if (vstream) { pthread_mutex_unlock(&vstream->stream->lock); } send_reply_nolock: - reply.flags = htobe32(reply.flags); health_code_update(); - ret = send_response(conn->sock, &reply, sizeof(reply)); - if (ret < 0) { - goto end_free; + if (reply) { + memcpy(reply, &reply_header, sizeof(reply_header)); + ret = send_response(conn->sock, reply, reply_size); + } else { + /* No reply to send. */ + ret = send_response(conn->sock, &reply_header, + reply_size); } - health_code_update(); - if (send_data) { - health_code_update(); - ret = send_response(conn->sock, data, len); - if (ret < 0) { - goto end_free; - } - health_code_update(); + health_code_update(); + if (ret < 0) { + PERROR("sendmsg of packet data failed"); + goto end_free; } - DBG("Sent %u bytes for stream %" PRIu64, len, - be64toh(get_packet_info.stream_id)); + DBG("Sent %u bytes for stream %" PRIu64, reply_size, + (uint64_t) be64toh(get_packet_info.stream_id)); end_free: - free(data); + free(reply); end: if (vstream) { viewer_stream_put(vstream); @@ -1588,7 +1671,7 @@ end: static int viewer_get_metadata(struct relay_connection *conn) { - int ret = 0; + int ret = 0, stream_fd = -1; ssize_t read_len; uint64_t len = 0; char *data = NULL; @@ -1620,6 +1703,8 @@ int viewer_get_metadata(struct relay_connection *conn) * Reply back to the client with an error if we cannot * find it. */ + DBG("Client requested metadata of unknown stream id %" PRIu64, + (uint64_t) be64toh(request.stream_id)); reply.status = htobe32(LTTNG_VIEWER_METADATA_ERR); goto send_reply; } @@ -1646,16 +1731,9 @@ int viewer_get_metadata(struct relay_connection *conn) if (ret < 0) { goto error; } - ret = open(fullpath, O_RDONLY); - if (ret < 0) { - PERROR("Relay opening metadata file"); - goto error; - } - vstream->stream_fd = stream_fd_create(ret); + vstream->stream_fd = stream_fd_open(fullpath); if (!vstream->stream_fd) { - if (close(ret)) { - PERROR("close"); - } + PERROR("Failed to open viewer stream file at %s", fullpath); goto error; } } @@ -1667,10 +1745,14 @@ int viewer_get_metadata(struct relay_connection *conn) goto error; } - read_len = lttng_read(vstream->stream_fd->fd, data, len); + stream_fd = stream_fd_get_fd(vstream->stream_fd); + if (stream_fd < 0) { + goto error_put_fd; + } + read_len = lttng_read(stream_fd, data, len); if (read_len < len) { PERROR("Relay reading metadata file"); - goto error; + goto error_put_fd; } vstream->metadata_sent += read_len; if (vstream->metadata_sent == vstream->stream->metadata_received @@ -1682,7 +1764,8 @@ int viewer_get_metadata(struct relay_connection *conn) reply.status = htobe32(LTTNG_VIEWER_METADATA_OK); goto send_reply; - +error_put_fd: + (void) stream_fd_put_fd(vstream->stream_fd); error: reply.status = htobe32(LTTNG_VIEWER_METADATA_ERR); @@ -1705,7 +1788,7 @@ send_reply: } DBG("Sent %" PRIu64 " bytes of metadata for stream %" PRIu64, len, - be64toh(request.stream_id)); + (uint64_t) be64toh(request.stream_id)); DBG("Metadata sent"); @@ -1753,6 +1836,78 @@ end: return ret; } +/* + * Detach a viewer session. + * + * Return 0 on success or else a negative value. + */ +static +int viewer_detach_session(struct relay_connection *conn) +{ + int ret; + struct lttng_viewer_detach_session_response response; + struct lttng_viewer_detach_session_request request; + struct relay_session *session = NULL; + uint64_t viewer_session_to_close; + + DBG("Viewer detach session received"); + + assert(conn); + + health_code_update(); + + /* Receive the request from the connected client. */ + ret = recv_request(conn->sock, &request, sizeof(request)); + if (ret < 0) { + goto end; + } + viewer_session_to_close = be64toh(request.session_id); + + if (!conn->viewer_session) { + DBG("Client trying to detach before creating a live viewer session"); + response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR); + goto send_reply; + } + + health_code_update(); + + memset(&response, 0, sizeof(response)); + DBG("Detaching from session ID %" PRIu64, viewer_session_to_close); + + session = session_get_by_id(be64toh(request.session_id)); + if (!session) { + DBG("Relay session %" PRIu64 " not found", + (uint64_t) be64toh(request.session_id)); + response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_UNK); + goto send_reply; + } + + ret = viewer_session_is_attached(conn->viewer_session, session); + if (ret != 1) { + DBG("Not attached to this session"); + response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR); + goto send_reply_put; + } + + viewer_session_close_one_session(conn->viewer_session, session); + response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_OK); + DBG("Session %" PRIu64 " detached.", viewer_session_to_close); + +send_reply_put: + session_put(session); + +send_reply: + health_code_update(); + ret = send_response(conn->sock, &response, sizeof(response)); + if (ret < 0) { + goto end; + } + health_code_update(); + ret = 0; + +end: + return ret; +} /* * live_relay_unknown_command: send -1 if received unknown command @@ -1814,6 +1969,9 @@ int process_control(struct lttng_viewer_cmd *recv_hdr, case LTTNG_VIEWER_CREATE_SESSION: ret = viewer_create_session(conn); break; + case LTTNG_VIEWER_DETACH_SESSION: + ret = viewer_detach_session(conn); + break; default: ERR("Received unknown viewer command (%u)", be32toh(recv_hdr->cmd)); @@ -1833,7 +1991,8 @@ void cleanup_connection_pollfd(struct lttng_poll_event *events, int pollfd) (void) lttng_poll_del(events, pollfd); - ret = close(pollfd); + ret = fd_tracker_close_unsuspendable_fd(the_fd_tracker, &pollfd, 1, + fd_tracker_util_close_fd, NULL); if (ret < 0) { ERR("Closing pollfd %d", pollfd); } @@ -1869,7 +2028,8 @@ void *thread_worker(void *data) goto viewer_connections_ht_error; } - ret = create_thread_poll_set(&events, 2); + ret = create_named_thread_poll_set(&events, 2, + "Live viewer worker thread epoll"); if (ret < 0) { goto error_poll_create; } @@ -1928,10 +2088,7 @@ restart: /* Inspect the relay conn pipe for new connection. */ if (pollfd == live_conn_pipe[0]) { - if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { - ERR("Relay live pipe error"); - goto error; - } else if (revents & LPOLLIN) { + if (revents & LPOLLIN) { struct relay_connection *conn; ret = lttng_read(live_conn_pipe[0], @@ -1943,6 +2100,12 @@ restart: LPOLLIN | LPOLLRDHUP); connection_ht_add(viewer_connections_ht, conn); DBG("Connection socket %d added to poll", conn->sock->fd); + } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { + ERR("Relay live pipe error"); + goto error; + } else { + ERR("Unexpected poll events %u for sock %d", revents, pollfd); + goto error; } } else { /* Connection activity. */ @@ -1953,11 +2116,7 @@ restart: continue; } - if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { - cleanup_connection_pollfd(&events, pollfd); - /* Put "create" ownership reference. */ - connection_put(conn); - } else if (revents & LPOLLIN) { + if (revents & LPOLLIN) { ret = conn->sock->ops->recvmsg(conn->sock, &recv_hdr, sizeof(recv_hdr), 0); if (ret <= 0) { @@ -1976,6 +2135,14 @@ restart: DBG("Viewer connection closed with %d", pollfd); } } + } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { + cleanup_connection_pollfd(&events, pollfd); + /* Put "create" ownership reference. */ + connection_put(conn); + } else { + ERR("Unexpected poll events %u for sock %d", revents, pollfd); + connection_put(conn); + goto error; } /* Put local "get_by_sock" reference. */ connection_put(conn); @@ -1985,7 +2152,7 @@ restart: exit: error: - lttng_poll_clean(&events); + (void) fd_tracker_util_poll_clean(the_fd_tracker, &events); /* Cleanup reamaining connection object. */ rcu_read_lock(); @@ -2000,7 +2167,7 @@ error_poll_create: lttng_ht_destroy(viewer_connections_ht); viewer_connections_ht_error: /* Close relay conn pipes */ - utils_close_pipe(live_conn_pipe); + (void) fd_tracker_util_pipe_close(the_fd_tracker, live_conn_pipe); if (err) { DBG("Viewer worker thread exited with error"); } @@ -2024,7 +2191,8 @@ error_testpoint: */ static int create_conn_pipe(void) { - return utils_create_pipe_cloexec(live_conn_pipe); + return fd_tracker_util_pipe_open_cloexec(the_fd_tracker, + "Live connection pipe", live_conn_pipe); } int relayd_live_join(void) @@ -2100,7 +2268,7 @@ int relayd_live_create(struct lttng_uri *uri) } /* Setup the dispatcher thread */ - ret = pthread_create(&live_dispatcher_thread, NULL, + ret = pthread_create(&live_dispatcher_thread, default_pthread_attr(), thread_dispatcher, (void *) NULL); if (ret) { errno = ret; @@ -2110,7 +2278,7 @@ int relayd_live_create(struct lttng_uri *uri) } /* Setup the worker thread */ - ret = pthread_create(&live_worker_thread, NULL, + ret = pthread_create(&live_worker_thread, default_pthread_attr(), thread_worker, NULL); if (ret) { errno = ret; @@ -2120,7 +2288,7 @@ int relayd_live_create(struct lttng_uri *uri) } /* Setup the listener thread */ - ret = pthread_create(&live_listener_thread, NULL, + ret = pthread_create(&live_listener_thread, default_pthread_attr(), thread_listener, (void *) NULL); if (ret) { errno = ret;