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.
111 int kernel_consumer_add_channel(struct consumer_socket
*sock
,
112 struct ltt_kernel_channel
*channel
,
113 struct ltt_kernel_session
*ksession
,
114 unsigned int monitor
)
118 struct lttcomm_consumer_msg lkm
;
119 struct consumer_output
*consumer
;
120 enum lttng_error_code status
;
121 struct ltt_session
*session
= NULL
;
122 struct lttng_channel_extended
*channel_attr_extended
;
127 assert(ksession
->consumer
);
129 consumer
= ksession
->consumer
;
130 channel_attr_extended
= (struct lttng_channel_extended
*)
131 channel
->channel
->attr
.extended
.ptr
;
133 DBG("Kernel consumer adding channel %s to kernel consumer",
134 channel
->channel
->name
);
137 pathname
= create_channel_path(consumer
, ksession
->uid
,
141 pathname
= strdup("");
148 /* Prep channel message structure */
149 consumer_init_add_channel_comm_msg(&lkm
,
155 consumer
->net_seq_index
,
156 channel
->channel
->name
,
157 channel
->stream_count
,
158 channel
->channel
->attr
.output
,
159 CONSUMER_CHANNEL_TYPE_DATA
,
160 channel
->channel
->attr
.tracefile_size
,
161 channel
->channel
->attr
.tracefile_count
,
163 channel
->channel
->attr
.live_timer_interval
,
164 channel_attr_extended
->monitor_timer_interval
);
166 health_code_update();
168 ret
= consumer_send_channel(sock
, &lkm
);
173 health_code_update();
175 session
= session_find_by_id(ksession
->id
);
177 assert(pthread_mutex_trylock(&session
->lock
));
178 assert(session_trylock_list());
180 status
= notification_thread_command_add_channel(
181 notification_thread_handle
, session
->name
,
182 ksession
->uid
, ksession
->gid
,
183 channel
->channel
->name
, channel
->key
,
185 channel
->channel
->attr
.subbuf_size
* channel
->channel
->attr
.num_subbuf
);
187 if (status
!= LTTNG_OK
) {
192 channel
->published_to_notification_thread
= true;
196 session_put(session
);
203 * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
205 * The consumer socket lock must be held by the caller.
207 int kernel_consumer_add_metadata(struct consumer_socket
*sock
,
208 struct ltt_kernel_session
*ksession
, unsigned int monitor
)
212 struct lttcomm_consumer_msg lkm
;
213 struct consumer_output
*consumer
;
214 struct ltt_session
*session
= NULL
;
220 assert(ksession
->consumer
);
223 DBG("Sending metadata %d to kernel consumer",
224 ksession
->metadata_stream_fd
);
226 /* Get consumer output pointer */
227 consumer
= ksession
->consumer
;
230 pathname
= create_channel_path(consumer
,
231 ksession
->uid
, ksession
->gid
);
234 pathname
= strdup("");
241 session
= session_find_by_id(ksession
->id
);
243 assert(pthread_mutex_trylock(&session
->lock
));
244 assert(session_trylock_list());
246 /* Prep channel message structure */
247 consumer_init_add_channel_comm_msg(&lkm
,
248 ksession
->metadata
->key
,
253 consumer
->net_seq_index
,
254 DEFAULT_METADATA_NAME
,
256 DEFAULT_KERNEL_CHANNEL_OUTPUT
,
257 CONSUMER_CHANNEL_TYPE_METADATA
,
261 health_code_update();
263 ret
= consumer_send_channel(sock
, &lkm
);
268 health_code_update();
270 /* Prep stream message structure */
271 consumer_init_add_stream_comm_msg(&lkm
,
272 ksession
->metadata
->key
,
273 ksession
->metadata_stream_fd
,
274 0 /* CPU: 0 for metadata. */,
275 session
->current_archive_id
);
277 health_code_update();
279 /* Send stream and file descriptor */
280 ret
= consumer_send_stream(sock
, consumer
, &lkm
,
281 &ksession
->metadata_stream_fd
, 1);
286 health_code_update();
292 session_put(session
);
298 * Sending a single stream to the consumer with command ADD_STREAM.
301 int kernel_consumer_add_stream(struct consumer_socket
*sock
,
302 struct ltt_kernel_channel
*channel
,
303 struct ltt_kernel_stream
*stream
,
304 struct ltt_kernel_session
*session
, unsigned int monitor
,
305 uint64_t trace_archive_id
)
308 struct lttcomm_consumer_msg lkm
;
309 struct consumer_output
*consumer
;
314 assert(session
->consumer
);
317 DBG("Sending stream %d of channel %s to kernel consumer",
318 stream
->fd
, channel
->channel
->name
);
320 /* Get consumer output pointer */
321 consumer
= session
->consumer
;
323 /* Prep stream consumer message */
324 consumer_init_add_stream_comm_msg(&lkm
,
330 health_code_update();
332 /* Send stream and file descriptor */
333 ret
= consumer_send_stream(sock
, consumer
, &lkm
, &stream
->fd
, 1);
338 health_code_update();
345 * Sending the notification that all streams were sent with STREAMS_SENT.
347 int kernel_consumer_streams_sent(struct consumer_socket
*sock
,
348 struct ltt_kernel_session
*session
, uint64_t channel_key
)
351 struct lttcomm_consumer_msg lkm
;
352 struct consumer_output
*consumer
;
357 DBG("Sending streams_sent");
358 /* Get consumer output pointer */
359 consumer
= session
->consumer
;
361 /* Prep stream consumer message */
362 consumer_init_streams_sent_comm_msg(&lkm
,
363 LTTNG_CONSUMER_STREAMS_SENT
,
364 channel_key
, consumer
->net_seq_index
);
366 health_code_update();
368 /* Send stream and file descriptor */
369 ret
= consumer_send_msg(sock
, &lkm
);
379 * Send all stream fds of kernel channel to the consumer.
381 * The consumer socket lock must be held by the caller.
383 int kernel_consumer_send_channel_streams(struct consumer_socket
*sock
,
384 struct ltt_kernel_channel
*channel
, struct ltt_kernel_session
*ksession
,
385 unsigned int monitor
)
388 struct ltt_kernel_stream
*stream
;
389 struct ltt_session
*session
= NULL
;
394 assert(ksession
->consumer
);
399 session
= session_find_by_id(ksession
->id
);
401 assert(pthread_mutex_trylock(&session
->lock
));
402 assert(session_trylock_list());
404 /* Bail out if consumer is disabled */
405 if (!ksession
->consumer
->enabled
) {
410 DBG("Sending streams of channel %s to kernel consumer",
411 channel
->channel
->name
);
413 if (!channel
->sent_to_consumer
) {
414 ret
= kernel_consumer_add_channel(sock
, channel
, ksession
, monitor
);
418 channel
->sent_to_consumer
= true;
422 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
423 if (!stream
->fd
|| stream
->sent_to_consumer
) {
427 /* Add stream on the kernel consumer side. */
428 ret
= kernel_consumer_add_stream(sock
, channel
, stream
,
429 ksession
, monitor
, session
->current_archive_id
);
433 stream
->sent_to_consumer
= true;
439 session_put(session
);
445 * Send all stream fds of the kernel session to the consumer.
447 * The consumer socket lock must be held by the caller.
449 int kernel_consumer_send_session(struct consumer_socket
*sock
,
450 struct ltt_kernel_session
*session
)
452 int ret
, monitor
= 0;
453 struct ltt_kernel_channel
*chan
;
457 assert(session
->consumer
);
460 /* Bail out if consumer is disabled */
461 if (!session
->consumer
->enabled
) {
466 /* Don't monitor the streams on the consumer if in flight recorder. */
467 if (session
->output_traces
) {
471 DBG("Sending session stream to kernel consumer");
473 if (session
->metadata_stream_fd
>= 0 && session
->metadata
) {
474 ret
= kernel_consumer_add_metadata(sock
, session
, monitor
);
480 /* Send channel and streams of it */
481 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
482 ret
= kernel_consumer_send_channel_streams(sock
, chan
, session
,
489 * Inform the relay that all the streams for the
492 ret
= kernel_consumer_streams_sent(sock
, session
, chan
->key
);
499 DBG("Kernel consumer FDs of metadata and channel streams sent");
501 session
->consumer_fds_sent
= 1;
508 int kernel_consumer_destroy_channel(struct consumer_socket
*socket
,
509 struct ltt_kernel_channel
*channel
)
512 struct lttcomm_consumer_msg msg
;
517 DBG("Sending kernel consumer destroy channel key %" PRIu64
, channel
->key
);
519 memset(&msg
, 0, sizeof(msg
));
520 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
521 msg
.u
.destroy_channel
.key
= channel
->key
;
523 pthread_mutex_lock(socket
->lock
);
524 health_code_update();
526 ret
= consumer_send_msg(socket
, &msg
);
532 health_code_update();
533 pthread_mutex_unlock(socket
->lock
);
537 int kernel_consumer_destroy_metadata(struct consumer_socket
*socket
,
538 struct ltt_kernel_metadata
*metadata
)
541 struct lttcomm_consumer_msg msg
;
546 DBG("Sending kernel consumer destroy channel key %" PRIu64
, metadata
->key
);
548 memset(&msg
, 0, sizeof(msg
));
549 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
550 msg
.u
.destroy_channel
.key
= metadata
->key
;
552 pthread_mutex_lock(socket
->lock
);
553 health_code_update();
555 ret
= consumer_send_msg(socket
, &msg
);
561 health_code_update();
562 pthread_mutex_unlock(socket
->lock
);