X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fbin%2Flttng-sessiond%2Fust-consumer.c;h=e804f4b5ac74284c1c3b0b9ea23a9317b505649f;hp=ee54e79850560d6f4866c177716aeddae3b542df;hb=ca03de585e016f441dfe373420545a6fb047531a;hpb=e71aad1fa4b06a5f91ddceace42366f3d79bd77e diff --git a/src/bin/lttng-sessiond/ust-consumer.c b/src/bin/lttng-sessiond/ust-consumer.c index ee54e7985..e804f4b5a 100644 --- a/src/bin/lttng-sessiond/ust-consumer.c +++ b/src/bin/lttng-sessiond/ust-consumer.c @@ -1,18 +1,18 @@ /* * Copyright (C) 2011 - David Goulet * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the Free - * Software Foundation; only version 2 of the License. + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License, version 2 only, + * as published by the Free Software Foundation. * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License along with - * this program; if not, write to the Free Software Foundation, Inc., 59 Temple - * Place - Suite 330, Boston, MA 02111-1307, USA. + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #define _GNU_SOURCE @@ -25,86 +25,167 @@ #include #include #include -#include +#include "consumer.h" #include "ust-consumer.h" /* - * Send all stream fds of UST channel to the consumer. + * Send a single channel to the consumer using command ADD_CHANNEL. */ -static int send_channel_streams(int sock, - struct ust_app_channel *uchan, - uid_t uid, gid_t gid) +static int send_channel(struct consumer_socket *sock, + struct ust_app_channel *uchan) { int ret, fd; - struct lttcomm_consumer_msg lum; - struct ltt_ust_stream *stream, *tmp; + struct lttcomm_consumer_msg msg; - DBG("Sending streams of channel %s to UST consumer", uchan->name); + /* Safety net */ + assert(uchan); + assert(sock); + + if (sock->fd < 0) { + ret = -EINVAL; + goto error; + } + + DBG2("Sending channel %s to UST consumer", uchan->name); - /* Send channel */ - lum.cmd_type = LTTNG_CONSUMER_ADD_CHANNEL; - - /* - * We need to keep shm_fd open while we transfer the stream file - * descriptors to make sure this key stays unique within the - * session daemon. We can free the channel shm_fd without - * problem after we finished sending stream fds for that - * channel. - */ - lum.u.channel.channel_key = uchan->obj->shm_fd; - lum.u.channel.max_sb_size = uchan->attr.subbuf_size; - lum.u.channel.mmap_len = uchan->obj->memory_map_size; - DBG("Sending channel %d to consumer", lum.u.channel.channel_key); - ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum)); + consumer_init_channel_comm_msg(&msg, + LTTNG_CONSUMER_ADD_CHANNEL, + uchan->obj->shm_fd, + uchan->attr.subbuf_size, + uchan->obj->memory_map_size, + uchan->name, + uchan->streams.count); + + health_code_update(&health_thread_cmd); + + ret = consumer_send_channel(sock, &msg); if (ret < 0) { - PERROR("send consumer channel"); goto error; } + + health_code_update(&health_thread_cmd); + fd = uchan->obj->shm_fd; - ret = lttcomm_send_fds_unix_sock(sock, &fd, 1); + ret = consumer_send_fds(sock, &fd, 1); if (ret < 0) { - PERROR("send consumer channel ancillary data"); goto error; } - cds_list_for_each_entry_safe(stream, tmp, &uchan->streams.head, list) { - int fds[2]; + health_code_update(&health_thread_cmd); - if (!stream->obj->shm_fd) { - continue; +error: + return ret; +} + +/* + * Send a single stream to the consumer using ADD_STREAM command. + */ +static int send_channel_stream(struct consumer_socket *sock, + struct ust_app_channel *uchan, struct ust_app_session *usess, + struct ltt_ust_stream *stream, struct consumer_output *consumer, + const char *pathname) +{ + int ret, fds[2]; + struct lttcomm_consumer_msg msg; + + /* Safety net */ + assert(uchan); + assert(usess); + assert(stream); + assert(consumer); + assert(sock); + + DBG2("Sending stream %d of channel %s to kernel consumer", + stream->obj->shm_fd, uchan->name); + + consumer_init_stream_comm_msg(&msg, + LTTNG_CONSUMER_ADD_STREAM, + uchan->obj->shm_fd, + stream->obj->shm_fd, + LTTNG_CONSUMER_ACTIVE_STREAM, + DEFAULT_UST_CHANNEL_OUTPUT, + stream->obj->memory_map_size, + usess->uid, + usess->gid, + consumer->net_seq_index, + 0, /* Metadata flag unset */ + stream->name, + pathname, + usess->id); + + health_code_update(&health_thread_cmd); + + /* Send stream and file descriptor */ + fds[0] = stream->obj->shm_fd; + fds[1] = stream->obj->wait_fd; + ret = consumer_send_stream(sock, consumer, &msg, fds, 2); + if (ret < 0) { + goto error; + } + + health_code_update(&health_thread_cmd); + +error: + return ret; +} + +/* + * Send all stream fds of UST channel to the consumer. + */ +static int send_channel_streams(struct consumer_socket *sock, + struct ust_app_channel *uchan, struct ust_app_session *usess, + struct consumer_output *consumer) +{ + int ret; + char tmp_path[PATH_MAX]; + const char *pathname; + struct ltt_ust_stream *stream, *tmp; + + assert(sock); + + DBG("Sending streams of channel %s to UST consumer", uchan->name); + + ret = send_channel(sock, uchan); + if (ret < 0) { + goto error; + } + + /* Get the right path name destination */ + if (consumer->type == CONSUMER_DST_LOCAL) { + /* Set application path to the destination path */ + ret = snprintf(tmp_path, sizeof(tmp_path), "%s/%s/%s", + consumer->dst.trace_path, consumer->subdir, usess->path); + if (ret < 0) { + PERROR("snprintf stream path"); + goto error; } - lum.cmd_type = LTTNG_CONSUMER_ADD_STREAM; - lum.u.stream.channel_key = uchan->obj->shm_fd; - lum.u.stream.stream_key = stream->obj->shm_fd; - lum.u.stream.state = LTTNG_CONSUMER_ACTIVE_STREAM; - /* - * FIXME Hack alert! we force MMAP for now. Mixup - * between EVENT and UST enums elsewhere. - */ - lum.u.stream.output = DEFAULT_UST_CHANNEL_OUTPUT; - lum.u.stream.mmap_len = stream->obj->memory_map_size; - lum.u.stream.uid = uid; - lum.u.stream.gid = gid; - strncpy(lum.u.stream.path_name, stream->pathname, PATH_MAX - 1); - lum.u.stream.path_name[PATH_MAX - 1] = '\0'; - DBG("Sending stream %d to consumer", lum.u.stream.stream_key); - ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum)); + pathname = tmp_path; + DBG3("UST local consumer tracefile path: %s", pathname); + } else { + ret = snprintf(tmp_path, sizeof(tmp_path), "%s/%s", + consumer->subdir, usess->path); if (ret < 0) { - PERROR("send consumer stream"); + PERROR("snprintf stream path"); goto error; } + pathname = tmp_path; + DBG3("UST network consumer subdir path: %s", pathname); + } + + cds_list_for_each_entry_safe(stream, tmp, &uchan->streams.head, list) { + if (!stream->obj->shm_fd) { + continue; + } - fds[0] = stream->obj->shm_fd; - fds[1] = stream->obj->wait_fd; - ret = lttcomm_send_fds_unix_sock(sock, fds, 2); + ret = send_channel_stream(sock, uchan, usess, stream, consumer, + pathname); if (ret < 0) { - PERROR("send consumer stream ancillary data"); goto error; } } - DBG("consumer channel streams sent"); + DBG("UST consumer channel streams sent"); return 0; @@ -113,69 +194,147 @@ error: } /* - * Send all stream fds of the UST session to the consumer. + * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM. */ -int ust_consumer_send_session(int consumer_fd, struct ust_app_session *usess) +static int send_metadata(struct consumer_socket *sock, + struct ust_app_session *usess, struct consumer_output *consumer) { - int ret = 0; - int sock = consumer_fd; - struct lttng_ht_iter iter; - struct lttcomm_consumer_msg lum; - struct ust_app_channel *ua_chan; + int ret, fd, fds[2]; + char tmp_path[PATH_MAX]; + const char *pathname; + struct lttcomm_consumer_msg msg; - DBG("Sending metadata stream fd"); + /* Safety net */ + assert(usess); + assert(consumer); + assert(sock); - if (consumer_fd < 0) { - ERR("Consumer has negative file descriptor"); + if (sock->fd < 0) { + ERR("Consumer socket is negative (%d)", sock->fd); return -EINVAL; } - if (usess->metadata->obj->shm_fd != 0) { - int fd; - int fds[2]; + if (usess->metadata->obj->shm_fd == 0) { + ERR("Metadata obj shm_fd is 0"); + ret = -1; + goto error; + } - /* Send metadata channel fd */ - lum.cmd_type = LTTNG_CONSUMER_ADD_CHANNEL; - lum.u.channel.channel_key = usess->metadata->obj->shm_fd; - lum.u.channel.max_sb_size = usess->metadata->attr.subbuf_size; - lum.u.channel.mmap_len = usess->metadata->obj->memory_map_size; - DBG("Sending metadata channel %d to consumer", lum.u.channel.channel_key); - ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum)); - if (ret < 0) { - PERROR("send consumer channel"); - goto error; - } - fd = usess->metadata->obj->shm_fd; - ret = lttcomm_send_fds_unix_sock(sock, &fd, 1); + DBG("UST consumer sending metadata stream fd"); + + consumer_init_channel_comm_msg(&msg, + LTTNG_CONSUMER_ADD_CHANNEL, + usess->metadata->obj->shm_fd, + usess->metadata->attr.subbuf_size, + usess->metadata->obj->memory_map_size, + "metadata", + 1); + + health_code_update(&health_thread_cmd); + + ret = consumer_send_channel(sock, &msg); + if (ret < 0) { + goto error; + } + + health_code_update(&health_thread_cmd); + + /* Sending metadata shared memory fd */ + fd = usess->metadata->obj->shm_fd; + ret = consumer_send_fds(sock, &fd, 1); + if (ret < 0) { + goto error; + } + + health_code_update(&health_thread_cmd); + + /* Get correct path name destination */ + if (consumer->type == CONSUMER_DST_LOCAL) { + /* Set application path to the destination path */ + ret = snprintf(tmp_path, sizeof(tmp_path), "%s/%s/%s", + consumer->dst.trace_path, consumer->subdir, usess->path); if (ret < 0) { - PERROR("send consumer metadata channel"); + PERROR("snprintf stream path"); goto error; } + pathname = tmp_path; - /* Send metadata stream fd */ - lum.cmd_type = LTTNG_CONSUMER_ADD_STREAM; - lum.u.stream.channel_key = usess->metadata->obj->shm_fd; - lum.u.stream.stream_key = usess->metadata->stream_obj->shm_fd; - lum.u.stream.state = LTTNG_CONSUMER_ACTIVE_STREAM; - lum.u.stream.output = DEFAULT_UST_CHANNEL_OUTPUT; - lum.u.stream.mmap_len = usess->metadata->stream_obj->memory_map_size; - lum.u.stream.uid = usess->uid; - lum.u.stream.gid = usess->gid; - strncpy(lum.u.stream.path_name, usess->metadata->pathname, PATH_MAX - 1); - lum.u.stream.path_name[PATH_MAX - 1] = '\0'; - DBG("Sending metadata stream %d to consumer", lum.u.stream.stream_key); - ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum)); + /* Create directory */ + ret = run_as_mkdir_recursive(pathname, S_IRWXU | S_IRWXG, + usess->uid, usess->gid); if (ret < 0) { - PERROR("send consumer metadata stream"); - goto error; + if (ret != -EEXIST) { + ERR("Trace directory creation error"); + goto error; + } } - fds[0] = usess->metadata->stream_obj->shm_fd; - fds[1] = usess->metadata->stream_obj->wait_fd; - ret = lttcomm_send_fds_unix_sock(sock, fds, 2); + } else { + ret = snprintf(tmp_path, sizeof(tmp_path), "%s/%s", + consumer->subdir, usess->path); if (ret < 0) { - PERROR("send consumer stream"); + PERROR("snprintf metadata path"); goto error; } + pathname = tmp_path; + } + + consumer_init_stream_comm_msg(&msg, + LTTNG_CONSUMER_ADD_STREAM, + usess->metadata->obj->shm_fd, + usess->metadata->stream_obj->shm_fd, + LTTNG_CONSUMER_ACTIVE_STREAM, + DEFAULT_UST_CHANNEL_OUTPUT, + usess->metadata->stream_obj->memory_map_size, + usess->uid, + usess->gid, + consumer->net_seq_index, + 1, /* Flag metadata set */ + "metadata", + pathname, + usess->id); + + health_code_update(&health_thread_cmd); + + /* Send stream and file descriptor */ + fds[0] = usess->metadata->stream_obj->shm_fd; + fds[1] = usess->metadata->stream_obj->wait_fd; + ret = consumer_send_stream(sock, consumer, &msg, fds, 2); + if (ret < 0) { + goto error; + } + + health_code_update(&health_thread_cmd); + +error: + return ret; +} + +/* + * Send all stream fds of the UST session to the consumer. + */ +int ust_consumer_send_session(struct ust_app_session *usess, + struct consumer_output *consumer, struct consumer_socket *sock) +{ + int ret = 0; + struct lttng_ht_iter iter; + struct ust_app_channel *ua_chan; + + assert(usess); + + if (consumer == NULL || sock == NULL) { + /* There is no consumer so just ignoring the command. */ + DBG("UST consumer does not exist. Not sending streams"); + return 0; + } + + DBG("Sending metadata stream fd to consumer on %d", sock->fd); + + pthread_mutex_lock(sock->lock); + + /* Sending metadata information to the consumer */ + ret = send_metadata(sock, usess, consumer); + if (ret < 0) { + goto error; } /* Send each channel fd streams of session */ @@ -190,7 +349,7 @@ int ust_consumer_send_session(int consumer_fd, struct ust_app_session *usess) continue; } - ret = send_channel_streams(sock, ua_chan, usess->uid, usess->gid); + ret = send_channel_streams(sock, ua_chan, usess, consumer); if (ret < 0) { rcu_read_unlock(); goto error; @@ -200,8 +359,10 @@ int ust_consumer_send_session(int consumer_fd, struct ust_app_session *usess) DBG("consumer fds (metadata and channel streams) sent"); - return 0; + /* All good! */ + ret = 0; error: + pthread_mutex_unlock(sock->lock); return ret; }