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");
56 pathname
= lttng_strndup(tmp_path
, sizeof(tmp_path
));
58 PERROR("lttng_strndup");
62 /* Create directory */
63 ret
= run_as_mkdir_recursive(pathname
, S_IRWXU
| S_IRWXG
, uid
, gid
);
65 if (errno
!= EEXIST
) {
66 ERR("Trace directory creation error");
70 DBG3("Kernel local consumer tracefile path: %s", pathname
);
72 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s", consumer
->subdir
);
74 PERROR("snprintf kernel metadata path");
77 pathname
= lttng_strndup(tmp_path
, sizeof(tmp_path
));
79 PERROR("lttng_strndup");
82 DBG3("Kernel network consumer subdir path: %s", pathname
);
93 * Sending a single channel to the consumer with command ADD_CHANNEL.
95 int kernel_consumer_add_channel(struct consumer_socket
*sock
,
96 struct ltt_kernel_channel
*channel
,
97 struct ltt_kernel_session
*ksession
,
102 struct lttcomm_consumer_msg lkm
;
103 struct consumer_output
*consumer
;
104 enum lttng_error_code status
;
105 struct ltt_session
*session
;
106 struct lttng_channel_extended
*channel_attr_extended
;
111 assert(ksession
->consumer
);
113 consumer
= ksession
->consumer
;
114 channel_attr_extended
= (struct lttng_channel_extended
*)
115 channel
->channel
->attr
.extended
.ptr
;
117 DBG("Kernel consumer adding channel %s to kernel consumer",
118 channel
->channel
->name
);
121 pathname
= create_channel_path(consumer
, ksession
->uid
,
125 pathname
= strdup("");
132 /* Prep channel message structure */
133 consumer_init_channel_comm_msg(&lkm
,
134 LTTNG_CONSUMER_ADD_CHANNEL
,
140 consumer
->net_seq_index
,
141 channel
->channel
->name
,
142 channel
->stream_count
,
143 channel
->channel
->attr
.output
,
144 CONSUMER_CHANNEL_TYPE_DATA
,
145 channel
->channel
->attr
.tracefile_size
,
146 channel
->channel
->attr
.tracefile_count
,
148 channel
->channel
->attr
.live_timer_interval
,
149 channel_attr_extended
->monitor_timer_interval
);
151 health_code_update();
153 ret
= consumer_send_channel(sock
, &lkm
);
158 health_code_update();
160 session
= session_find_by_id(ksession
->id
);
163 status
= notification_thread_command_add_channel(
164 notification_thread_handle
, session
->name
,
165 ksession
->uid
, ksession
->gid
,
166 channel
->channel
->name
, channel
->key
,
168 channel
->channel
->attr
.subbuf_size
* channel
->channel
->attr
.num_subbuf
);
170 if (status
!= LTTNG_OK
) {
175 channel
->published_to_notification_thread
= true;
183 * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
185 * The consumer socket lock must be held by the caller.
187 int kernel_consumer_add_metadata(struct consumer_socket
*sock
,
188 struct ltt_kernel_session
*session
, unsigned int monitor
)
192 struct lttcomm_consumer_msg lkm
;
193 struct consumer_output
*consumer
;
197 assert(session
->consumer
);
200 DBG("Sending metadata %d to kernel consumer", session
->metadata_stream_fd
);
202 /* Get consumer output pointer */
203 consumer
= session
->consumer
;
206 pathname
= create_channel_path(consumer
, session
->uid
, session
->gid
);
209 pathname
= strdup("");
216 /* Prep channel message structure */
217 consumer_init_channel_comm_msg(&lkm
,
218 LTTNG_CONSUMER_ADD_CHANNEL
,
219 session
->metadata
->fd
,
224 consumer
->net_seq_index
,
225 DEFAULT_METADATA_NAME
,
227 DEFAULT_KERNEL_CHANNEL_OUTPUT
,
228 CONSUMER_CHANNEL_TYPE_METADATA
,
232 health_code_update();
234 ret
= consumer_send_channel(sock
, &lkm
);
239 health_code_update();
241 /* Prep stream message structure */
242 consumer_init_stream_comm_msg(&lkm
,
243 LTTNG_CONSUMER_ADD_STREAM
,
244 session
->metadata
->fd
,
245 session
->metadata_stream_fd
,
246 0); /* CPU: 0 for metadata. */
248 health_code_update();
250 /* Send stream and file descriptor */
251 ret
= consumer_send_stream(sock
, consumer
, &lkm
,
252 &session
->metadata_stream_fd
, 1);
257 health_code_update();
265 * Sending a single stream to the consumer with command ADD_STREAM.
267 int kernel_consumer_add_stream(struct consumer_socket
*sock
,
268 struct ltt_kernel_channel
*channel
, struct ltt_kernel_stream
*stream
,
269 struct ltt_kernel_session
*session
, unsigned int monitor
)
272 struct lttcomm_consumer_msg lkm
;
273 struct consumer_output
*consumer
;
278 assert(session
->consumer
);
281 DBG("Sending stream %d of channel %s to kernel consumer",
282 stream
->fd
, channel
->channel
->name
);
284 /* Get consumer output pointer */
285 consumer
= session
->consumer
;
287 /* Prep stream consumer message */
288 consumer_init_stream_comm_msg(&lkm
,
289 LTTNG_CONSUMER_ADD_STREAM
,
294 health_code_update();
296 /* Send stream and file descriptor */
297 ret
= consumer_send_stream(sock
, consumer
, &lkm
, &stream
->fd
, 1);
302 health_code_update();
309 * Sending the notification that all streams were sent with STREAMS_SENT.
311 int kernel_consumer_streams_sent(struct consumer_socket
*sock
,
312 struct ltt_kernel_session
*session
, uint64_t channel_key
)
315 struct lttcomm_consumer_msg lkm
;
316 struct consumer_output
*consumer
;
321 DBG("Sending streams_sent");
322 /* Get consumer output pointer */
323 consumer
= session
->consumer
;
325 /* Prep stream consumer message */
326 consumer_init_streams_sent_comm_msg(&lkm
,
327 LTTNG_CONSUMER_STREAMS_SENT
,
328 channel_key
, consumer
->net_seq_index
);
330 health_code_update();
332 /* Send stream and file descriptor */
333 ret
= consumer_send_msg(sock
, &lkm
);
343 * Send all stream fds of kernel channel to the consumer.
345 * The consumer socket lock must be held by the caller.
347 int kernel_consumer_send_channel_stream(struct consumer_socket
*sock
,
348 struct ltt_kernel_channel
*channel
, struct ltt_kernel_session
*session
,
349 unsigned int monitor
)
352 struct ltt_kernel_stream
*stream
;
357 assert(session
->consumer
);
360 /* Bail out if consumer is disabled */
361 if (!session
->consumer
->enabled
) {
366 DBG("Sending streams of channel %s to kernel consumer",
367 channel
->channel
->name
);
369 if (!channel
->sent_to_consumer
) {
370 ret
= kernel_consumer_add_channel(sock
, channel
, session
, monitor
);
374 channel
->sent_to_consumer
= true;
378 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
379 if (!stream
->fd
|| stream
->sent_to_consumer
) {
383 /* Add stream on the kernel consumer side. */
384 ret
= kernel_consumer_add_stream(sock
, channel
, stream
, session
,
389 stream
->sent_to_consumer
= true;
397 * Send all stream fds of the kernel session to the consumer.
399 * The consumer socket lock must be held by the caller.
401 int kernel_consumer_send_session(struct consumer_socket
*sock
,
402 struct ltt_kernel_session
*session
)
404 int ret
, monitor
= 0;
405 struct ltt_kernel_channel
*chan
;
409 assert(session
->consumer
);
412 /* Bail out if consumer is disabled */
413 if (!session
->consumer
->enabled
) {
418 /* Don't monitor the streams on the consumer if in flight recorder. */
419 if (session
->output_traces
) {
423 DBG("Sending session stream to kernel consumer");
425 if (session
->metadata_stream_fd
>= 0 && session
->metadata
) {
426 ret
= kernel_consumer_add_metadata(sock
, session
, monitor
);
432 /* Send channel and streams of it */
433 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
434 ret
= kernel_consumer_send_channel_stream(sock
, chan
, session
,
441 * Inform the relay that all the streams for the
444 ret
= kernel_consumer_streams_sent(sock
, session
, chan
->key
);
451 DBG("Kernel consumer FDs of metadata and channel streams sent");
453 session
->consumer_fds_sent
= 1;
460 int kernel_consumer_destroy_channel(struct consumer_socket
*socket
,
461 struct ltt_kernel_channel
*channel
)
464 struct lttcomm_consumer_msg msg
;
469 DBG("Sending kernel consumer destroy channel key %" PRIu64
, channel
->key
);
471 memset(&msg
, 0, sizeof(msg
));
472 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
473 msg
.u
.destroy_channel
.key
= channel
->key
;
475 pthread_mutex_lock(socket
->lock
);
476 health_code_update();
478 ret
= consumer_send_msg(socket
, &msg
);
484 health_code_update();
485 pthread_mutex_unlock(socket
->lock
);
489 int kernel_consumer_destroy_metadata(struct consumer_socket
*socket
,
490 struct ltt_kernel_metadata
*metadata
)
493 struct lttcomm_consumer_msg msg
;
498 DBG("Sending kernel consumer destroy channel key %d", metadata
->fd
);
500 memset(&msg
, 0, sizeof(msg
));
501 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
502 msg
.u
.destroy_channel
.key
= metadata
->fd
;
504 pthread_mutex_lock(socket
->lock
);
505 health_code_update();
507 ret
= consumer_send_msg(socket
, &msg
);
513 health_code_update();
514 pthread_mutex_unlock(socket
->lock
);