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",
49 consumer
->dst
.trace_path
, consumer
->subdir
);
51 PERROR("snprintf kernel channel path");
54 pathname
= lttng_strndup(tmp_path
, sizeof(tmp_path
));
56 PERROR("lttng_strndup");
60 /* Create directory */
61 ret
= run_as_mkdir_recursive(pathname
, S_IRWXU
| S_IRWXG
, uid
, gid
);
63 if (errno
!= EEXIST
) {
64 ERR("Trace directory creation error");
68 DBG3("Kernel local consumer tracefile path: %s", pathname
);
70 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s", consumer
->subdir
);
72 PERROR("snprintf kernel metadata path");
75 pathname
= lttng_strndup(tmp_path
, sizeof(tmp_path
));
77 PERROR("lttng_strndup");
80 DBG3("Kernel network consumer subdir path: %s", pathname
);
91 * Sending a single channel to the consumer with command ADD_CHANNEL.
93 int kernel_consumer_add_channel(struct consumer_socket
*sock
,
94 struct ltt_kernel_channel
*channel
,
95 struct ltt_kernel_session
*ksession
,
100 struct lttcomm_consumer_msg lkm
;
101 struct consumer_output
*consumer
;
102 enum lttng_error_code status
;
103 struct ltt_session
*session
;
104 struct lttng_channel_extended
*channel_attr_extended
;
109 assert(ksession
->consumer
);
111 consumer
= ksession
->consumer
;
112 channel_attr_extended
= (struct lttng_channel_extended
*)
113 channel
->channel
->attr
.extended
.ptr
;
115 DBG("Kernel consumer adding channel %s to kernel consumer",
116 channel
->channel
->name
);
119 pathname
= create_channel_path(consumer
, ksession
->uid
,
123 pathname
= strdup("");
130 /* Prep channel message structure */
131 consumer_init_channel_comm_msg(&lkm
,
132 LTTNG_CONSUMER_ADD_CHANNEL
,
138 consumer
->net_seq_index
,
139 channel
->channel
->name
,
140 channel
->stream_count
,
141 channel
->channel
->attr
.output
,
142 CONSUMER_CHANNEL_TYPE_DATA
,
143 channel
->channel
->attr
.tracefile_size
,
144 channel
->channel
->attr
.tracefile_count
,
146 channel
->channel
->attr
.live_timer_interval
,
147 channel_attr_extended
->monitor_timer_interval
);
149 health_code_update();
151 ret
= consumer_send_channel(sock
, &lkm
);
156 health_code_update();
158 session
= session_find_by_id(ksession
->id
);
161 status
= notification_thread_command_add_channel(
162 notification_thread_handle
, session
->name
,
163 ksession
->uid
, ksession
->gid
,
164 channel
->channel
->name
, channel
->key
,
166 channel
->channel
->attr
.subbuf_size
* channel
->channel
->attr
.num_subbuf
);
168 if (status
!= LTTNG_OK
) {
173 channel
->published_to_notification_thread
= true;
181 * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
183 * The consumer socket lock must be held by the caller.
185 int kernel_consumer_add_metadata(struct consumer_socket
*sock
,
186 struct ltt_kernel_session
*session
, unsigned int monitor
)
190 struct lttcomm_consumer_msg lkm
;
191 struct consumer_output
*consumer
;
195 assert(session
->consumer
);
198 DBG("Sending metadata %d to kernel consumer", session
->metadata_stream_fd
);
200 /* Get consumer output pointer */
201 consumer
= session
->consumer
;
204 pathname
= create_channel_path(consumer
, session
->uid
, session
->gid
);
207 pathname
= strdup("");
214 /* Prep channel message structure */
215 consumer_init_channel_comm_msg(&lkm
,
216 LTTNG_CONSUMER_ADD_CHANNEL
,
217 session
->metadata
->fd
,
222 consumer
->net_seq_index
,
223 DEFAULT_METADATA_NAME
,
225 DEFAULT_KERNEL_CHANNEL_OUTPUT
,
226 CONSUMER_CHANNEL_TYPE_METADATA
,
230 health_code_update();
232 ret
= consumer_send_channel(sock
, &lkm
);
237 health_code_update();
239 /* Prep stream message structure */
240 consumer_init_stream_comm_msg(&lkm
,
241 LTTNG_CONSUMER_ADD_STREAM
,
242 session
->metadata
->fd
,
243 session
->metadata_stream_fd
,
244 0); /* CPU: 0 for metadata. */
246 health_code_update();
248 /* Send stream and file descriptor */
249 ret
= consumer_send_stream(sock
, consumer
, &lkm
,
250 &session
->metadata_stream_fd
, 1);
255 health_code_update();
263 * Sending a single stream to the consumer with command ADD_STREAM.
265 int kernel_consumer_add_stream(struct consumer_socket
*sock
,
266 struct ltt_kernel_channel
*channel
, struct ltt_kernel_stream
*stream
,
267 struct ltt_kernel_session
*session
, unsigned int monitor
)
270 struct lttcomm_consumer_msg lkm
;
271 struct consumer_output
*consumer
;
276 assert(session
->consumer
);
279 DBG("Sending stream %d of channel %s to kernel consumer",
280 stream
->fd
, channel
->channel
->name
);
282 /* Get consumer output pointer */
283 consumer
= session
->consumer
;
285 /* Prep stream consumer message */
286 consumer_init_stream_comm_msg(&lkm
,
287 LTTNG_CONSUMER_ADD_STREAM
,
292 health_code_update();
294 /* Send stream and file descriptor */
295 ret
= consumer_send_stream(sock
, consumer
, &lkm
, &stream
->fd
, 1);
300 health_code_update();
307 * Sending the notification that all streams were sent with STREAMS_SENT.
309 int kernel_consumer_streams_sent(struct consumer_socket
*sock
,
310 struct ltt_kernel_session
*session
, uint64_t channel_key
)
313 struct lttcomm_consumer_msg lkm
;
314 struct consumer_output
*consumer
;
319 DBG("Sending streams_sent");
320 /* Get consumer output pointer */
321 consumer
= session
->consumer
;
323 /* Prep stream consumer message */
324 consumer_init_streams_sent_comm_msg(&lkm
,
325 LTTNG_CONSUMER_STREAMS_SENT
,
326 channel_key
, consumer
->net_seq_index
);
328 health_code_update();
330 /* Send stream and file descriptor */
331 ret
= consumer_send_msg(sock
, &lkm
);
341 * Send all stream fds of kernel channel to the consumer.
343 * The consumer socket lock must be held by the caller.
345 int kernel_consumer_send_channel_stream(struct consumer_socket
*sock
,
346 struct ltt_kernel_channel
*channel
, struct ltt_kernel_session
*session
,
347 unsigned int monitor
)
350 struct ltt_kernel_stream
*stream
;
355 assert(session
->consumer
);
358 /* Bail out if consumer is disabled */
359 if (!session
->consumer
->enabled
) {
364 DBG("Sending streams of channel %s to kernel consumer",
365 channel
->channel
->name
);
367 if (!channel
->sent_to_consumer
) {
368 ret
= kernel_consumer_add_channel(sock
, channel
, session
, monitor
);
372 channel
->sent_to_consumer
= true;
376 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
377 if (!stream
->fd
|| stream
->sent_to_consumer
) {
381 /* Add stream on the kernel consumer side. */
382 ret
= kernel_consumer_add_stream(sock
, channel
, stream
, session
,
387 stream
->sent_to_consumer
= true;
395 * Send all stream fds of the kernel session to the consumer.
397 * The consumer socket lock must be held by the caller.
399 int kernel_consumer_send_session(struct consumer_socket
*sock
,
400 struct ltt_kernel_session
*session
)
402 int ret
, monitor
= 0;
403 struct ltt_kernel_channel
*chan
;
407 assert(session
->consumer
);
410 /* Bail out if consumer is disabled */
411 if (!session
->consumer
->enabled
) {
416 /* Don't monitor the streams on the consumer if in flight recorder. */
417 if (session
->output_traces
) {
421 DBG("Sending session stream to kernel consumer");
423 if (session
->metadata_stream_fd
>= 0 && session
->metadata
) {
424 ret
= kernel_consumer_add_metadata(sock
, session
, monitor
);
430 /* Send channel and streams of it */
431 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
432 ret
= kernel_consumer_send_channel_stream(sock
, chan
, session
,
439 * Inform the relay that all the streams for the
442 ret
= kernel_consumer_streams_sent(sock
, session
, chan
->key
);
449 DBG("Kernel consumer FDs of metadata and channel streams sent");
451 session
->consumer_fds_sent
= 1;
458 int kernel_consumer_destroy_channel(struct consumer_socket
*socket
,
459 struct ltt_kernel_channel
*channel
)
462 struct lttcomm_consumer_msg msg
;
467 DBG("Sending kernel consumer destroy channel key %" PRIu64
, channel
->key
);
469 memset(&msg
, 0, sizeof(msg
));
470 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
471 msg
.u
.destroy_channel
.key
= channel
->key
;
473 pthread_mutex_lock(socket
->lock
);
474 health_code_update();
476 ret
= consumer_send_msg(socket
, &msg
);
482 health_code_update();
483 pthread_mutex_unlock(socket
->lock
);
487 int kernel_consumer_destroy_metadata(struct consumer_socket
*socket
,
488 struct ltt_kernel_metadata
*metadata
)
491 struct lttcomm_consumer_msg msg
;
496 DBG("Sending kernel consumer destroy channel key %d", metadata
->fd
);
498 memset(&msg
, 0, sizeof(msg
));
499 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
500 msg
.u
.destroy_channel
.key
= metadata
->fd
;
502 pthread_mutex_lock(socket
->lock
);
503 health_code_update();
505 ret
= consumer_send_msg(socket
, &msg
);
511 health_code_update();
512 pthread_mutex_unlock(socket
->lock
);