2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
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.
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
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.
25 #include <common/common.h>
26 #include <common/defaults.h>
27 #include <common/compat/string.h>
30 #include "health-sessiond.h"
31 #include "kernel-consumer.h"
32 #include "notification-thread-commands.h"
34 #include "lttng-sessiond.h"
36 static char *create_channel_path(struct consumer_output
*consumer
,
40 char tmp_path
[PATH_MAX
];
41 char *pathname
= NULL
;
45 /* Get the right path name destination */
46 if (consumer
->type
== CONSUMER_DST_LOCAL
) {
47 /* Set application path to the destination path */
48 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s%s%s",
49 consumer
->dst
.session_root_path
,
53 PERROR("snprintf kernel channel path");
55 } else if (ret
>= sizeof(tmp_path
)) {
56 ERR("Kernel channel path exceeds the maximal allowed length of of %zu bytes (%i bytes required) with path \"%s%s%s\"",
57 sizeof(tmp_path
), ret
,
58 consumer
->dst
.session_root_path
,
63 pathname
= lttng_strndup(tmp_path
, sizeof(tmp_path
));
65 PERROR("lttng_strndup");
69 /* Create directory */
70 ret
= run_as_mkdir_recursive(pathname
, S_IRWXU
| S_IRWXG
, uid
, gid
);
72 if (errno
!= EEXIST
) {
73 ERR("Trace directory creation error");
77 DBG3("Kernel local consumer tracefile path: %s", pathname
);
79 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s%s",
80 consumer
->dst
.net
.base_dir
,
83 PERROR("snprintf kernel metadata path");
85 } else if (ret
>= sizeof(tmp_path
)) {
86 ERR("Kernel channel path exceeds the maximal allowed length of of %zu bytes (%i bytes required) with path \"%s%s\"",
87 sizeof(tmp_path
), ret
,
88 consumer
->dst
.net
.base_dir
,
92 pathname
= lttng_strndup(tmp_path
, sizeof(tmp_path
));
94 PERROR("lttng_strndup");
97 DBG3("Kernel network consumer subdir path: %s", pathname
);
108 * Sending a single channel to the consumer with command ADD_CHANNEL.
110 int kernel_consumer_add_channel(struct consumer_socket
*sock
,
111 struct ltt_kernel_channel
*channel
,
112 struct ltt_kernel_session
*ksession
,
113 unsigned int monitor
)
117 struct lttcomm_consumer_msg lkm
;
118 struct consumer_output
*consumer
;
119 enum lttng_error_code status
;
120 struct ltt_session
*session
;
121 struct lttng_channel_extended
*channel_attr_extended
;
126 assert(ksession
->consumer
);
128 consumer
= ksession
->consumer
;
129 channel_attr_extended
= (struct lttng_channel_extended
*)
130 channel
->channel
->attr
.extended
.ptr
;
132 DBG("Kernel consumer adding channel %s to kernel consumer",
133 channel
->channel
->name
);
136 pathname
= create_channel_path(consumer
, ksession
->uid
,
140 pathname
= strdup("");
147 /* Prep channel message structure */
148 consumer_init_add_channel_comm_msg(&lkm
,
154 consumer
->net_seq_index
,
155 channel
->channel
->name
,
156 channel
->stream_count
,
157 channel
->channel
->attr
.output
,
158 CONSUMER_CHANNEL_TYPE_DATA
,
159 channel
->channel
->attr
.tracefile_size
,
160 channel
->channel
->attr
.tracefile_count
,
162 channel
->channel
->attr
.live_timer_interval
,
163 channel_attr_extended
->monitor_timer_interval
);
165 health_code_update();
167 ret
= consumer_send_channel(sock
, &lkm
);
172 health_code_update();
174 session
= session_find_by_id(ksession
->id
);
177 status
= notification_thread_command_add_channel(
178 notification_thread_handle
, session
->name
,
179 ksession
->uid
, ksession
->gid
,
180 channel
->channel
->name
, channel
->key
,
182 channel
->channel
->attr
.subbuf_size
* channel
->channel
->attr
.num_subbuf
);
184 if (status
!= LTTNG_OK
) {
189 channel
->published_to_notification_thread
= true;
197 * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
199 * The consumer socket lock must be held by the caller.
201 int kernel_consumer_add_metadata(struct consumer_socket
*sock
,
202 struct ltt_kernel_session
*session
, unsigned int monitor
)
206 struct lttcomm_consumer_msg lkm
;
207 struct consumer_output
*consumer
;
211 assert(session
->consumer
);
214 DBG("Sending metadata %d to kernel consumer", session
->metadata_stream_fd
);
216 /* Get consumer output pointer */
217 consumer
= session
->consumer
;
220 pathname
= create_channel_path(consumer
, session
->uid
, session
->gid
);
223 pathname
= strdup("");
230 /* Prep channel message structure */
231 consumer_init_add_channel_comm_msg(&lkm
,
232 session
->metadata
->key
,
237 consumer
->net_seq_index
,
238 DEFAULT_METADATA_NAME
,
240 DEFAULT_KERNEL_CHANNEL_OUTPUT
,
241 CONSUMER_CHANNEL_TYPE_METADATA
,
245 health_code_update();
247 ret
= consumer_send_channel(sock
, &lkm
);
252 health_code_update();
254 /* Prep stream message structure */
255 consumer_init_stream_comm_msg(&lkm
,
256 LTTNG_CONSUMER_ADD_STREAM
,
257 session
->metadata
->key
,
258 session
->metadata_stream_fd
,
259 0); /* CPU: 0 for metadata. */
261 health_code_update();
263 /* Send stream and file descriptor */
264 ret
= consumer_send_stream(sock
, consumer
, &lkm
,
265 &session
->metadata_stream_fd
, 1);
270 health_code_update();
278 * Sending a single stream to the consumer with command ADD_STREAM.
281 int kernel_consumer_add_stream(struct consumer_socket
*sock
,
282 struct ltt_kernel_channel
*channel
, struct ltt_kernel_stream
*stream
,
283 struct ltt_kernel_session
*session
, unsigned int monitor
)
286 struct lttcomm_consumer_msg lkm
;
287 struct consumer_output
*consumer
;
292 assert(session
->consumer
);
295 DBG("Sending stream %d of channel %s to kernel consumer",
296 stream
->fd
, channel
->channel
->name
);
298 /* Get consumer output pointer */
299 consumer
= session
->consumer
;
301 /* Prep stream consumer message */
302 consumer_init_stream_comm_msg(&lkm
,
303 LTTNG_CONSUMER_ADD_STREAM
,
308 health_code_update();
310 /* Send stream and file descriptor */
311 ret
= consumer_send_stream(sock
, consumer
, &lkm
, &stream
->fd
, 1);
316 health_code_update();
323 * Sending the notification that all streams were sent with STREAMS_SENT.
325 int kernel_consumer_streams_sent(struct consumer_socket
*sock
,
326 struct ltt_kernel_session
*session
, uint64_t channel_key
)
329 struct lttcomm_consumer_msg lkm
;
330 struct consumer_output
*consumer
;
335 DBG("Sending streams_sent");
336 /* Get consumer output pointer */
337 consumer
= session
->consumer
;
339 /* Prep stream consumer message */
340 consumer_init_streams_sent_comm_msg(&lkm
,
341 LTTNG_CONSUMER_STREAMS_SENT
,
342 channel_key
, consumer
->net_seq_index
);
344 health_code_update();
346 /* Send stream and file descriptor */
347 ret
= consumer_send_msg(sock
, &lkm
);
357 * Send all stream fds of kernel channel to the consumer.
359 * The consumer socket lock must be held by the caller.
361 int kernel_consumer_send_channel_streams(struct consumer_socket
*sock
,
362 struct ltt_kernel_channel
*channel
, struct ltt_kernel_session
*session
,
363 unsigned int monitor
)
366 struct ltt_kernel_stream
*stream
;
371 assert(session
->consumer
);
374 /* Bail out if consumer is disabled */
375 if (!session
->consumer
->enabled
) {
380 DBG("Sending streams of channel %s to kernel consumer",
381 channel
->channel
->name
);
383 if (!channel
->sent_to_consumer
) {
384 ret
= kernel_consumer_add_channel(sock
, channel
, session
, monitor
);
388 channel
->sent_to_consumer
= true;
392 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
393 if (!stream
->fd
|| stream
->sent_to_consumer
) {
397 /* Add stream on the kernel consumer side. */
398 ret
= kernel_consumer_add_stream(sock
, channel
, stream
, session
,
403 stream
->sent_to_consumer
= true;
411 * Send all stream fds of the kernel session to the consumer.
413 * The consumer socket lock must be held by the caller.
415 int kernel_consumer_send_session(struct consumer_socket
*sock
,
416 struct ltt_kernel_session
*session
)
418 int ret
, monitor
= 0;
419 struct ltt_kernel_channel
*chan
;
423 assert(session
->consumer
);
426 /* Bail out if consumer is disabled */
427 if (!session
->consumer
->enabled
) {
432 /* Don't monitor the streams on the consumer if in flight recorder. */
433 if (session
->output_traces
) {
437 DBG("Sending session stream to kernel consumer");
439 if (session
->metadata_stream_fd
>= 0 && session
->metadata
) {
440 ret
= kernel_consumer_add_metadata(sock
, session
, monitor
);
446 /* Send channel and streams of it */
447 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
448 ret
= kernel_consumer_send_channel_streams(sock
, chan
, session
,
455 * Inform the relay that all the streams for the
458 ret
= kernel_consumer_streams_sent(sock
, session
, chan
->key
);
465 DBG("Kernel consumer FDs of metadata and channel streams sent");
467 session
->consumer_fds_sent
= 1;
474 int kernel_consumer_destroy_channel(struct consumer_socket
*socket
,
475 struct ltt_kernel_channel
*channel
)
478 struct lttcomm_consumer_msg msg
;
483 DBG("Sending kernel consumer destroy channel key %" PRIu64
, channel
->key
);
485 memset(&msg
, 0, sizeof(msg
));
486 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
487 msg
.u
.destroy_channel
.key
= channel
->key
;
489 pthread_mutex_lock(socket
->lock
);
490 health_code_update();
492 ret
= consumer_send_msg(socket
, &msg
);
498 health_code_update();
499 pthread_mutex_unlock(socket
->lock
);
503 int kernel_consumer_destroy_metadata(struct consumer_socket
*socket
,
504 struct ltt_kernel_metadata
*metadata
)
507 struct lttcomm_consumer_msg msg
;
512 DBG("Sending kernel consumer destroy channel key %" PRIu64
, metadata
->key
);
514 memset(&msg
, 0, sizeof(msg
));
515 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
516 msg
.u
.destroy_channel
.key
= metadata
->key
;
518 pthread_mutex_lock(socket
->lock
);
519 health_code_update();
521 ret
= consumer_send_msg(socket
, &msg
);
527 health_code_update();
528 pthread_mutex_unlock(socket
->lock
);