| 1 | /* |
| 2 | * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License, version 2 only, as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along with |
| 14 | * this program; if not, write to the Free Software Foundation, Inc., 51 |
| 15 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | */ |
| 17 | |
| 18 | #define _GNU_SOURCE |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | #include <common/common.h> |
| 26 | #include <common/defaults.h> |
| 27 | |
| 28 | #include "consumer.h" |
| 29 | #include "kernel-consumer.h" |
| 30 | |
| 31 | /* |
| 32 | * Sending a single channel to the consumer with command ADD_CHANNEL. |
| 33 | */ |
| 34 | int kernel_consumer_add_channel(struct consumer_socket *sock, |
| 35 | struct ltt_kernel_channel *channel) |
| 36 | { |
| 37 | int ret; |
| 38 | struct lttcomm_consumer_msg lkm; |
| 39 | |
| 40 | /* Safety net */ |
| 41 | assert(channel); |
| 42 | |
| 43 | DBG("Kernel consumer adding channel %s to kernel consumer", |
| 44 | channel->channel->name); |
| 45 | |
| 46 | /* Prep channel message structure */ |
| 47 | consumer_init_channel_comm_msg(&lkm, |
| 48 | LTTNG_CONSUMER_ADD_CHANNEL, |
| 49 | channel->fd, |
| 50 | channel->channel->attr.subbuf_size, |
| 51 | 0, /* Kernel */ |
| 52 | channel->channel->name, |
| 53 | channel->stream_count); |
| 54 | |
| 55 | health_code_update(&health_thread_kernel); |
| 56 | |
| 57 | ret = consumer_send_channel(sock, &lkm); |
| 58 | if (ret < 0) { |
| 59 | goto error; |
| 60 | } |
| 61 | |
| 62 | health_code_update(&health_thread_kernel); |
| 63 | |
| 64 | error: |
| 65 | return ret; |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM. |
| 70 | */ |
| 71 | int kernel_consumer_add_metadata(struct consumer_socket *sock, |
| 72 | struct ltt_kernel_session *session) |
| 73 | { |
| 74 | int ret; |
| 75 | char tmp_path[PATH_MAX]; |
| 76 | const char *pathname; |
| 77 | struct lttcomm_consumer_msg lkm; |
| 78 | struct consumer_output *consumer; |
| 79 | |
| 80 | /* Safety net */ |
| 81 | assert(session); |
| 82 | assert(session->consumer); |
| 83 | assert(sock); |
| 84 | |
| 85 | DBG("Sending metadata %d to kernel consumer", session->metadata_stream_fd); |
| 86 | |
| 87 | /* Get consumer output pointer */ |
| 88 | consumer = session->consumer; |
| 89 | |
| 90 | /* Get the right path name destination */ |
| 91 | if (consumer->type == CONSUMER_DST_LOCAL) { |
| 92 | /* Set application path to the destination path */ |
| 93 | ret = snprintf(tmp_path, sizeof(tmp_path), "%s/%s", |
| 94 | consumer->dst.trace_path, consumer->subdir); |
| 95 | if (ret < 0) { |
| 96 | PERROR("snprintf metadata path"); |
| 97 | goto error; |
| 98 | } |
| 99 | pathname = tmp_path; |
| 100 | |
| 101 | /* Create directory */ |
| 102 | ret = run_as_mkdir_recursive(pathname, S_IRWXU | S_IRWXG, |
| 103 | session->uid, session->gid); |
| 104 | if (ret < 0) { |
| 105 | if (ret != -EEXIST) { |
| 106 | ERR("Trace directory creation error"); |
| 107 | goto error; |
| 108 | } |
| 109 | } |
| 110 | DBG3("Kernel local consumer tracefile path: %s", pathname); |
| 111 | } else { |
| 112 | ret = snprintf(tmp_path, sizeof(tmp_path), "%s", consumer->subdir); |
| 113 | if (ret < 0) { |
| 114 | PERROR("snprintf metadata path"); |
| 115 | goto error; |
| 116 | } |
| 117 | pathname = tmp_path; |
| 118 | DBG3("Kernel network consumer subdir path: %s", pathname); |
| 119 | } |
| 120 | |
| 121 | /* Prep channel message structure */ |
| 122 | consumer_init_channel_comm_msg(&lkm, |
| 123 | LTTNG_CONSUMER_ADD_CHANNEL, |
| 124 | session->metadata->fd, |
| 125 | session->metadata->conf->attr.subbuf_size, |
| 126 | 0, /* for kernel */ |
| 127 | "metadata", |
| 128 | 1); |
| 129 | |
| 130 | health_code_update(&health_thread_kernel); |
| 131 | |
| 132 | ret = consumer_send_channel(sock, &lkm); |
| 133 | if (ret < 0) { |
| 134 | goto error; |
| 135 | } |
| 136 | |
| 137 | health_code_update(&health_thread_kernel); |
| 138 | |
| 139 | /* Prep stream message structure */ |
| 140 | consumer_init_stream_comm_msg(&lkm, |
| 141 | LTTNG_CONSUMER_ADD_STREAM, |
| 142 | session->metadata->fd, |
| 143 | session->metadata_stream_fd, |
| 144 | LTTNG_CONSUMER_ACTIVE_STREAM, |
| 145 | DEFAULT_KERNEL_CHANNEL_OUTPUT, |
| 146 | 0, /* Kernel */ |
| 147 | session->uid, |
| 148 | session->gid, |
| 149 | consumer->net_seq_index, |
| 150 | 1, /* Metadata flag set */ |
| 151 | "metadata", |
| 152 | pathname, |
| 153 | session->id); |
| 154 | |
| 155 | health_code_update(&health_thread_kernel); |
| 156 | |
| 157 | /* Send stream and file descriptor */ |
| 158 | ret = consumer_send_stream(sock, consumer, &lkm, |
| 159 | &session->metadata_stream_fd, 1); |
| 160 | if (ret < 0) { |
| 161 | goto error; |
| 162 | } |
| 163 | |
| 164 | health_code_update(&health_thread_kernel); |
| 165 | |
| 166 | error: |
| 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | * Sending a single stream to the consumer with command ADD_STREAM. |
| 172 | */ |
| 173 | int kernel_consumer_add_stream(struct consumer_socket *sock, |
| 174 | struct ltt_kernel_channel *channel, struct ltt_kernel_stream *stream, |
| 175 | struct ltt_kernel_session *session) |
| 176 | { |
| 177 | int ret; |
| 178 | char tmp_path[PATH_MAX]; |
| 179 | const char *pathname; |
| 180 | struct lttcomm_consumer_msg lkm; |
| 181 | struct consumer_output *consumer; |
| 182 | |
| 183 | assert(channel); |
| 184 | assert(stream); |
| 185 | assert(session); |
| 186 | assert(session->consumer); |
| 187 | assert(sock); |
| 188 | |
| 189 | DBG("Sending stream %d of channel %s to kernel consumer", |
| 190 | stream->fd, channel->channel->name); |
| 191 | |
| 192 | /* Get consumer output pointer */ |
| 193 | consumer = session->consumer; |
| 194 | |
| 195 | /* Get the right path name destination */ |
| 196 | if (consumer->type == CONSUMER_DST_LOCAL) { |
| 197 | /* Set application path to the destination path */ |
| 198 | ret = snprintf(tmp_path, sizeof(tmp_path), "%s/%s", |
| 199 | consumer->dst.trace_path, consumer->subdir); |
| 200 | if (ret < 0) { |
| 201 | PERROR("snprintf stream path"); |
| 202 | goto error; |
| 203 | } |
| 204 | pathname = tmp_path; |
| 205 | DBG3("Kernel local consumer tracefile path: %s", pathname); |
| 206 | } else { |
| 207 | ret = snprintf(tmp_path, sizeof(tmp_path), "%s", consumer->subdir); |
| 208 | if (ret < 0) { |
| 209 | PERROR("snprintf stream path"); |
| 210 | goto error; |
| 211 | } |
| 212 | pathname = tmp_path; |
| 213 | DBG3("Kernel network consumer subdir path: %s", pathname); |
| 214 | } |
| 215 | |
| 216 | /* Prep stream consumer message */ |
| 217 | consumer_init_stream_comm_msg(&lkm, LTTNG_CONSUMER_ADD_STREAM, |
| 218 | channel->fd, |
| 219 | stream->fd, |
| 220 | stream->state, |
| 221 | channel->channel->attr.output, |
| 222 | 0, /* Kernel */ |
| 223 | session->uid, |
| 224 | session->gid, |
| 225 | consumer->net_seq_index, |
| 226 | 0, /* Metadata flag unset */ |
| 227 | stream->name, |
| 228 | pathname, |
| 229 | session->id); |
| 230 | |
| 231 | health_code_update(&health_thread_kernel); |
| 232 | |
| 233 | /* Send stream and file descriptor */ |
| 234 | ret = consumer_send_stream(sock, consumer, &lkm, &stream->fd, 1); |
| 235 | if (ret < 0) { |
| 236 | goto error; |
| 237 | } |
| 238 | |
| 239 | health_code_update(&health_thread_kernel); |
| 240 | |
| 241 | error: |
| 242 | return ret; |
| 243 | } |
| 244 | |
| 245 | /* |
| 246 | * Send all stream fds of kernel channel to the consumer. |
| 247 | */ |
| 248 | int kernel_consumer_send_channel_stream(struct consumer_socket *sock, |
| 249 | struct ltt_kernel_channel *channel, struct ltt_kernel_session *session) |
| 250 | { |
| 251 | int ret; |
| 252 | struct ltt_kernel_stream *stream; |
| 253 | |
| 254 | /* Safety net */ |
| 255 | assert(channel); |
| 256 | assert(session); |
| 257 | assert(session->consumer); |
| 258 | assert(sock); |
| 259 | |
| 260 | /* Bail out if consumer is disabled */ |
| 261 | if (!session->consumer->enabled) { |
| 262 | ret = LTTNG_OK; |
| 263 | goto error; |
| 264 | } |
| 265 | |
| 266 | DBG("Sending streams of channel %s to kernel consumer", |
| 267 | channel->channel->name); |
| 268 | |
| 269 | ret = kernel_consumer_add_channel(sock, channel); |
| 270 | if (ret < 0) { |
| 271 | goto error; |
| 272 | } |
| 273 | |
| 274 | /* Send streams */ |
| 275 | cds_list_for_each_entry(stream, &channel->stream_list.head, list) { |
| 276 | if (!stream->fd) { |
| 277 | continue; |
| 278 | } |
| 279 | |
| 280 | /* Add stream on the kernel consumer side. */ |
| 281 | ret = kernel_consumer_add_stream(sock, channel, stream, session); |
| 282 | if (ret < 0) { |
| 283 | goto error; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | error: |
| 288 | return ret; |
| 289 | } |
| 290 | |
| 291 | /* |
| 292 | * Send all stream fds of the kernel session to the consumer. |
| 293 | */ |
| 294 | int kernel_consumer_send_session(struct consumer_socket *sock, |
| 295 | struct ltt_kernel_session *session) |
| 296 | { |
| 297 | int ret; |
| 298 | struct ltt_kernel_channel *chan; |
| 299 | |
| 300 | /* Safety net */ |
| 301 | assert(session); |
| 302 | assert(session->consumer); |
| 303 | assert(sock); |
| 304 | |
| 305 | /* Bail out if consumer is disabled */ |
| 306 | if (!session->consumer->enabled) { |
| 307 | ret = LTTNG_OK; |
| 308 | goto error; |
| 309 | } |
| 310 | |
| 311 | DBG("Sending session stream to kernel consumer"); |
| 312 | |
| 313 | if (session->metadata_stream_fd >= 0) { |
| 314 | ret = kernel_consumer_add_metadata(sock, session); |
| 315 | if (ret < 0) { |
| 316 | goto error; |
| 317 | } |
| 318 | |
| 319 | /* Flag that at least the metadata has been sent to the consumer. */ |
| 320 | session->consumer_fds_sent = 1; |
| 321 | } |
| 322 | |
| 323 | /* Send channel and streams of it */ |
| 324 | cds_list_for_each_entry(chan, &session->channel_list.head, list) { |
| 325 | ret = kernel_consumer_send_channel_stream(sock, chan, session); |
| 326 | if (ret < 0) { |
| 327 | goto error; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | DBG("Kernel consumer FDs of metadata and channel streams sent"); |
| 332 | |
| 333 | return 0; |
| 334 | |
| 335 | error: |
| 336 | return ret; |
| 337 | } |