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
= NULL
;
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
);
176 assert(pthread_mutex_trylock(&session
->lock
));
177 assert(session_trylock_list());
179 status
= notification_thread_command_add_channel(
180 notification_thread_handle
, session
->name
,
181 ksession
->uid
, ksession
->gid
,
182 channel
->channel
->name
, channel
->key
,
184 channel
->channel
->attr
.subbuf_size
* channel
->channel
->attr
.num_subbuf
);
186 if (status
!= LTTNG_OK
) {
191 channel
->published_to_notification_thread
= true;
195 session_put(session
);
202 * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
204 * The consumer socket lock must be held by the caller.
206 int kernel_consumer_add_metadata(struct consumer_socket
*sock
,
207 struct ltt_kernel_session
*ksession
, unsigned int monitor
)
211 struct lttcomm_consumer_msg lkm
;
212 struct consumer_output
*consumer
;
213 struct ltt_session
*session
= NULL
;
219 assert(ksession
->consumer
);
222 DBG("Sending metadata %d to kernel consumer",
223 ksession
->metadata_stream_fd
);
225 /* Get consumer output pointer */
226 consumer
= ksession
->consumer
;
229 pathname
= create_channel_path(consumer
,
230 ksession
->uid
, ksession
->gid
);
233 pathname
= strdup("");
240 session
= session_find_by_id(ksession
->id
);
242 assert(pthread_mutex_trylock(&session
->lock
));
243 assert(session_trylock_list());
245 /* Prep channel message structure */
246 consumer_init_add_channel_comm_msg(&lkm
,
247 ksession
->metadata
->key
,
252 consumer
->net_seq_index
,
253 DEFAULT_METADATA_NAME
,
255 DEFAULT_KERNEL_CHANNEL_OUTPUT
,
256 CONSUMER_CHANNEL_TYPE_METADATA
,
260 health_code_update();
262 ret
= consumer_send_channel(sock
, &lkm
);
267 health_code_update();
269 /* Prep stream message structure */
270 consumer_init_add_stream_comm_msg(&lkm
,
271 ksession
->metadata
->key
,
272 ksession
->metadata_stream_fd
,
273 0 /* CPU: 0 for metadata. */,
274 session
->current_archive_id
);
276 health_code_update();
278 /* Send stream and file descriptor */
279 ret
= consumer_send_stream(sock
, consumer
, &lkm
,
280 &ksession
->metadata_stream_fd
, 1);
285 health_code_update();
291 session_put(session
);
297 * Sending a single stream to the consumer with command ADD_STREAM.
300 int kernel_consumer_add_stream(struct consumer_socket
*sock
,
301 struct ltt_kernel_channel
*channel
,
302 struct ltt_kernel_stream
*stream
,
303 struct ltt_kernel_session
*session
, unsigned int monitor
,
304 uint64_t trace_archive_id
)
307 struct lttcomm_consumer_msg lkm
;
308 struct consumer_output
*consumer
;
313 assert(session
->consumer
);
316 DBG("Sending stream %d of channel %s to kernel consumer",
317 stream
->fd
, channel
->channel
->name
);
319 /* Get consumer output pointer */
320 consumer
= session
->consumer
;
322 /* Prep stream consumer message */
323 consumer_init_add_stream_comm_msg(&lkm
,
329 health_code_update();
331 /* Send stream and file descriptor */
332 ret
= consumer_send_stream(sock
, consumer
, &lkm
, &stream
->fd
, 1);
337 health_code_update();
344 * Sending the notification that all streams were sent with STREAMS_SENT.
346 int kernel_consumer_streams_sent(struct consumer_socket
*sock
,
347 struct ltt_kernel_session
*session
, uint64_t channel_key
)
350 struct lttcomm_consumer_msg lkm
;
351 struct consumer_output
*consumer
;
356 DBG("Sending streams_sent");
357 /* Get consumer output pointer */
358 consumer
= session
->consumer
;
360 /* Prep stream consumer message */
361 consumer_init_streams_sent_comm_msg(&lkm
,
362 LTTNG_CONSUMER_STREAMS_SENT
,
363 channel_key
, consumer
->net_seq_index
);
365 health_code_update();
367 /* Send stream and file descriptor */
368 ret
= consumer_send_msg(sock
, &lkm
);
378 * Send all stream fds of kernel channel to the consumer.
380 * The consumer socket lock must be held by the caller.
382 int kernel_consumer_send_channel_streams(struct consumer_socket
*sock
,
383 struct ltt_kernel_channel
*channel
, struct ltt_kernel_session
*ksession
,
384 unsigned int monitor
)
387 struct ltt_kernel_stream
*stream
;
388 struct ltt_session
*session
= NULL
;
393 assert(ksession
->consumer
);
398 session
= session_find_by_id(ksession
->id
);
400 assert(pthread_mutex_trylock(&session
->lock
));
401 assert(session_trylock_list());
403 /* Bail out if consumer is disabled */
404 if (!ksession
->consumer
->enabled
) {
409 DBG("Sending streams of channel %s to kernel consumer",
410 channel
->channel
->name
);
412 if (!channel
->sent_to_consumer
) {
413 ret
= kernel_consumer_add_channel(sock
, channel
, ksession
, monitor
);
417 channel
->sent_to_consumer
= true;
421 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
422 if (!stream
->fd
|| stream
->sent_to_consumer
) {
426 /* Add stream on the kernel consumer side. */
427 ret
= kernel_consumer_add_stream(sock
, channel
, stream
,
428 ksession
, monitor
, session
->current_archive_id
);
432 stream
->sent_to_consumer
= true;
438 session_put(session
);
444 * Send all stream fds of the kernel session to the consumer.
446 * The consumer socket lock must be held by the caller.
448 int kernel_consumer_send_session(struct consumer_socket
*sock
,
449 struct ltt_kernel_session
*session
)
451 int ret
, monitor
= 0;
452 struct ltt_kernel_channel
*chan
;
456 assert(session
->consumer
);
459 /* Bail out if consumer is disabled */
460 if (!session
->consumer
->enabled
) {
465 /* Don't monitor the streams on the consumer if in flight recorder. */
466 if (session
->output_traces
) {
470 DBG("Sending session stream to kernel consumer");
472 if (session
->metadata_stream_fd
>= 0 && session
->metadata
) {
473 ret
= kernel_consumer_add_metadata(sock
, session
, monitor
);
479 /* Send channel and streams of it */
480 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
481 ret
= kernel_consumer_send_channel_streams(sock
, chan
, session
,
488 * Inform the relay that all the streams for the
491 ret
= kernel_consumer_streams_sent(sock
, session
, chan
->key
);
498 DBG("Kernel consumer FDs of metadata and channel streams sent");
500 session
->consumer_fds_sent
= 1;
507 int kernel_consumer_destroy_channel(struct consumer_socket
*socket
,
508 struct ltt_kernel_channel
*channel
)
511 struct lttcomm_consumer_msg msg
;
516 DBG("Sending kernel consumer destroy channel key %" PRIu64
, channel
->key
);
518 memset(&msg
, 0, sizeof(msg
));
519 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
520 msg
.u
.destroy_channel
.key
= channel
->key
;
522 pthread_mutex_lock(socket
->lock
);
523 health_code_update();
525 ret
= consumer_send_msg(socket
, &msg
);
531 health_code_update();
532 pthread_mutex_unlock(socket
->lock
);
536 int kernel_consumer_destroy_metadata(struct consumer_socket
*socket
,
537 struct ltt_kernel_metadata
*metadata
)
540 struct lttcomm_consumer_msg msg
;
545 DBG("Sending kernel consumer destroy channel key %" PRIu64
, metadata
->key
);
547 memset(&msg
, 0, sizeof(msg
));
548 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
549 msg
.u
.destroy_channel
.key
= metadata
->key
;
551 pthread_mutex_lock(socket
->lock
);
552 health_code_update();
554 ret
= consumer_send_msg(socket
, &msg
);
560 health_code_update();
561 pthread_mutex_unlock(socket
->lock
);