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_channel_comm_msg(&lkm
,
149 LTTNG_CONSUMER_ADD_CHANNEL
,
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
);
178 status
= notification_thread_command_add_channel(
179 notification_thread_handle
, session
->name
,
180 ksession
->uid
, ksession
->gid
,
181 channel
->channel
->name
, channel
->key
,
183 channel
->channel
->attr
.subbuf_size
* channel
->channel
->attr
.num_subbuf
);
185 if (status
!= LTTNG_OK
) {
190 channel
->published_to_notification_thread
= true;
198 * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
200 * The consumer socket lock must be held by the caller.
202 int kernel_consumer_add_metadata(struct consumer_socket
*sock
,
203 struct ltt_kernel_session
*session
, unsigned int monitor
)
207 struct lttcomm_consumer_msg lkm
;
208 struct consumer_output
*consumer
;
212 assert(session
->consumer
);
215 DBG("Sending metadata %d to kernel consumer", session
->metadata_stream_fd
);
217 /* Get consumer output pointer */
218 consumer
= session
->consumer
;
221 pathname
= create_channel_path(consumer
, session
->uid
, session
->gid
);
224 pathname
= strdup("");
231 /* Prep channel message structure */
232 consumer_init_channel_comm_msg(&lkm
,
233 LTTNG_CONSUMER_ADD_CHANNEL
,
234 session
->metadata
->key
,
239 consumer
->net_seq_index
,
240 DEFAULT_METADATA_NAME
,
242 DEFAULT_KERNEL_CHANNEL_OUTPUT
,
243 CONSUMER_CHANNEL_TYPE_METADATA
,
247 health_code_update();
249 ret
= consumer_send_channel(sock
, &lkm
);
254 health_code_update();
256 /* Prep stream message structure */
257 consumer_init_stream_comm_msg(&lkm
,
258 LTTNG_CONSUMER_ADD_STREAM
,
259 session
->metadata
->key
,
260 session
->metadata_stream_fd
,
261 0); /* CPU: 0 for metadata. */
263 health_code_update();
265 /* Send stream and file descriptor */
266 ret
= consumer_send_stream(sock
, consumer
, &lkm
,
267 &session
->metadata_stream_fd
, 1);
272 health_code_update();
280 * Sending a single stream to the consumer with command ADD_STREAM.
282 int kernel_consumer_add_stream(struct consumer_socket
*sock
,
283 struct ltt_kernel_channel
*channel
, struct ltt_kernel_stream
*stream
,
284 struct ltt_kernel_session
*session
, unsigned int monitor
)
287 struct lttcomm_consumer_msg lkm
;
288 struct consumer_output
*consumer
;
293 assert(session
->consumer
);
296 DBG("Sending stream %d of channel %s to kernel consumer",
297 stream
->fd
, channel
->channel
->name
);
299 /* Get consumer output pointer */
300 consumer
= session
->consumer
;
302 /* Prep stream consumer message */
303 consumer_init_stream_comm_msg(&lkm
,
304 LTTNG_CONSUMER_ADD_STREAM
,
309 health_code_update();
311 /* Send stream and file descriptor */
312 ret
= consumer_send_stream(sock
, consumer
, &lkm
, &stream
->fd
, 1);
317 health_code_update();
324 * Sending the notification that all streams were sent with STREAMS_SENT.
326 int kernel_consumer_streams_sent(struct consumer_socket
*sock
,
327 struct ltt_kernel_session
*session
, uint64_t channel_key
)
330 struct lttcomm_consumer_msg lkm
;
331 struct consumer_output
*consumer
;
336 DBG("Sending streams_sent");
337 /* Get consumer output pointer */
338 consumer
= session
->consumer
;
340 /* Prep stream consumer message */
341 consumer_init_streams_sent_comm_msg(&lkm
,
342 LTTNG_CONSUMER_STREAMS_SENT
,
343 channel_key
, consumer
->net_seq_index
);
345 health_code_update();
347 /* Send stream and file descriptor */
348 ret
= consumer_send_msg(sock
, &lkm
);
358 * Send all stream fds of kernel channel to the consumer.
360 * The consumer socket lock must be held by the caller.
362 int kernel_consumer_send_channel_stream(struct consumer_socket
*sock
,
363 struct ltt_kernel_channel
*channel
, struct ltt_kernel_session
*session
,
364 unsigned int monitor
)
367 struct ltt_kernel_stream
*stream
;
372 assert(session
->consumer
);
375 /* Bail out if consumer is disabled */
376 if (!session
->consumer
->enabled
) {
381 DBG("Sending streams of channel %s to kernel consumer",
382 channel
->channel
->name
);
384 if (!channel
->sent_to_consumer
) {
385 ret
= kernel_consumer_add_channel(sock
, channel
, session
, monitor
);
389 channel
->sent_to_consumer
= true;
393 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
394 if (!stream
->fd
|| stream
->sent_to_consumer
) {
398 /* Add stream on the kernel consumer side. */
399 ret
= kernel_consumer_add_stream(sock
, channel
, stream
, session
,
404 stream
->sent_to_consumer
= true;
412 * Send all stream fds of the kernel session to the consumer.
414 * The consumer socket lock must be held by the caller.
416 int kernel_consumer_send_session(struct consumer_socket
*sock
,
417 struct ltt_kernel_session
*session
)
419 int ret
, monitor
= 0;
420 struct ltt_kernel_channel
*chan
;
424 assert(session
->consumer
);
427 /* Bail out if consumer is disabled */
428 if (!session
->consumer
->enabled
) {
433 /* Don't monitor the streams on the consumer if in flight recorder. */
434 if (session
->output_traces
) {
438 DBG("Sending session stream to kernel consumer");
440 if (session
->metadata_stream_fd
>= 0 && session
->metadata
) {
441 ret
= kernel_consumer_add_metadata(sock
, session
, monitor
);
447 /* Send channel and streams of it */
448 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
449 ret
= kernel_consumer_send_channel_stream(sock
, chan
, session
,
456 * Inform the relay that all the streams for the
459 ret
= kernel_consumer_streams_sent(sock
, session
, chan
->key
);
466 DBG("Kernel consumer FDs of metadata and channel streams sent");
468 session
->consumer_fds_sent
= 1;
475 int kernel_consumer_destroy_channel(struct consumer_socket
*socket
,
476 struct ltt_kernel_channel
*channel
)
479 struct lttcomm_consumer_msg msg
;
484 DBG("Sending kernel consumer destroy channel key %" PRIu64
, channel
->key
);
486 memset(&msg
, 0, sizeof(msg
));
487 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
488 msg
.u
.destroy_channel
.key
= channel
->key
;
490 pthread_mutex_lock(socket
->lock
);
491 health_code_update();
493 ret
= consumer_send_msg(socket
, &msg
);
499 health_code_update();
500 pthread_mutex_unlock(socket
->lock
);
504 int kernel_consumer_destroy_metadata(struct consumer_socket
*socket
,
505 struct ltt_kernel_metadata
*metadata
)
508 struct lttcomm_consumer_msg msg
;
513 DBG("Sending kernel consumer destroy channel key %" PRIu64
, metadata
->key
);
515 memset(&msg
, 0, sizeof(msg
));
516 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
517 msg
.u
.destroy_channel
.key
= metadata
->key
;
519 pthread_mutex_lock(socket
->lock
);
520 health_code_update();
522 ret
= consumer_send_msg(socket
, &msg
);
528 health_code_update();
529 pthread_mutex_unlock(socket
->lock
);