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.
24 #include <common/common.h>
25 #include <common/defaults.h>
26 #include <common/compat/string.h>
29 #include "health-sessiond.h"
30 #include "kernel-consumer.h"
32 static char *create_channel_path(struct consumer_output
*consumer
,
36 char tmp_path
[PATH_MAX
];
37 char *pathname
= NULL
;
41 /* Get the right path name destination */
42 if (consumer
->type
== CONSUMER_DST_LOCAL
) {
43 /* Set application path to the destination path */
44 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s%s",
45 consumer
->dst
.trace_path
, consumer
->subdir
);
47 PERROR("snprintf kernel channel path");
50 pathname
= lttng_strndup(tmp_path
, sizeof(tmp_path
));
52 PERROR("lttng_strndup");
56 /* Create directory */
57 ret
= run_as_mkdir_recursive(pathname
, S_IRWXU
| S_IRWXG
, uid
, gid
);
59 if (errno
!= EEXIST
) {
60 ERR("Trace directory creation error");
64 DBG3("Kernel local consumer tracefile path: %s", pathname
);
66 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s", consumer
->subdir
);
68 PERROR("snprintf kernel metadata path");
71 pathname
= lttng_strndup(tmp_path
, sizeof(tmp_path
));
73 PERROR("lttng_strndup");
76 DBG3("Kernel network consumer subdir path: %s", pathname
);
87 * Sending a single channel to the consumer with command ADD_CHANNEL.
89 int kernel_consumer_add_channel(struct consumer_socket
*sock
,
90 struct ltt_kernel_channel
*channel
, struct ltt_kernel_session
*session
,
95 struct lttcomm_consumer_msg lkm
;
96 struct consumer_output
*consumer
;
101 assert(session
->consumer
);
103 consumer
= session
->consumer
;
105 DBG("Kernel consumer adding channel %s to kernel consumer",
106 channel
->channel
->name
);
109 pathname
= create_channel_path(consumer
, session
->uid
, session
->gid
);
112 pathname
= strdup("");
119 /* Prep channel message structure */
120 consumer_init_channel_comm_msg(&lkm
,
121 LTTNG_CONSUMER_ADD_CHANNEL
,
127 consumer
->net_seq_index
,
128 channel
->channel
->name
,
129 channel
->stream_count
,
130 channel
->channel
->attr
.output
,
131 CONSUMER_CHANNEL_TYPE_DATA
,
132 channel
->channel
->attr
.tracefile_size
,
133 channel
->channel
->attr
.tracefile_count
,
135 channel
->channel
->attr
.live_timer_interval
);
137 health_code_update();
139 ret
= consumer_send_channel(sock
, &lkm
);
144 health_code_update();
152 * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
154 * The consumer socket lock must be held by the caller.
156 int kernel_consumer_add_metadata(struct consumer_socket
*sock
,
157 struct ltt_kernel_session
*session
, unsigned int monitor
)
161 struct lttcomm_consumer_msg lkm
;
162 struct consumer_output
*consumer
;
166 assert(session
->consumer
);
169 DBG("Sending metadata %d to kernel consumer", session
->metadata_stream_fd
);
171 /* Get consumer output pointer */
172 consumer
= session
->consumer
;
175 pathname
= create_channel_path(consumer
, session
->uid
, session
->gid
);
178 pathname
= strdup("");
185 /* Prep channel message structure */
186 consumer_init_channel_comm_msg(&lkm
,
187 LTTNG_CONSUMER_ADD_CHANNEL
,
188 session
->metadata
->fd
,
193 consumer
->net_seq_index
,
194 DEFAULT_METADATA_NAME
,
196 DEFAULT_KERNEL_CHANNEL_OUTPUT
,
197 CONSUMER_CHANNEL_TYPE_METADATA
,
201 health_code_update();
203 ret
= consumer_send_channel(sock
, &lkm
);
208 health_code_update();
210 /* Prep stream message structure */
211 consumer_init_stream_comm_msg(&lkm
,
212 LTTNG_CONSUMER_ADD_STREAM
,
213 session
->metadata
->fd
,
214 session
->metadata_stream_fd
,
215 0); /* CPU: 0 for metadata. */
217 health_code_update();
219 /* Send stream and file descriptor */
220 ret
= consumer_send_stream(sock
, consumer
, &lkm
,
221 &session
->metadata_stream_fd
, 1);
226 health_code_update();
234 * Sending a single stream to the consumer with command ADD_STREAM.
236 int kernel_consumer_add_stream(struct consumer_socket
*sock
,
237 struct ltt_kernel_channel
*channel
, struct ltt_kernel_stream
*stream
,
238 struct ltt_kernel_session
*session
, unsigned int monitor
)
241 struct lttcomm_consumer_msg lkm
;
242 struct consumer_output
*consumer
;
247 assert(session
->consumer
);
250 DBG("Sending stream %d of channel %s to kernel consumer",
251 stream
->fd
, channel
->channel
->name
);
253 /* Get consumer output pointer */
254 consumer
= session
->consumer
;
256 /* Prep stream consumer message */
257 consumer_init_stream_comm_msg(&lkm
,
258 LTTNG_CONSUMER_ADD_STREAM
,
263 health_code_update();
265 /* Send stream and file descriptor */
266 ret
= consumer_send_stream(sock
, consumer
, &lkm
, &stream
->fd
, 1);
271 health_code_update();
278 * Sending the notification that all streams were sent with STREAMS_SENT.
280 int kernel_consumer_streams_sent(struct consumer_socket
*sock
,
281 struct ltt_kernel_session
*session
, uint64_t channel_key
)
284 struct lttcomm_consumer_msg lkm
;
285 struct consumer_output
*consumer
;
290 DBG("Sending streams_sent");
291 /* Get consumer output pointer */
292 consumer
= session
->consumer
;
294 /* Prep stream consumer message */
295 consumer_init_streams_sent_comm_msg(&lkm
,
296 LTTNG_CONSUMER_STREAMS_SENT
,
297 channel_key
, consumer
->net_seq_index
);
299 health_code_update();
301 /* Send stream and file descriptor */
302 ret
= consumer_send_msg(sock
, &lkm
);
312 * Send all stream fds of kernel channel to the consumer.
314 * The consumer socket lock must be held by the caller.
316 int kernel_consumer_send_channel_stream(struct consumer_socket
*sock
,
317 struct ltt_kernel_channel
*channel
, struct ltt_kernel_session
*session
,
318 unsigned int monitor
)
321 struct ltt_kernel_stream
*stream
;
326 assert(session
->consumer
);
329 /* Bail out if consumer is disabled */
330 if (!session
->consumer
->enabled
) {
335 DBG("Sending streams of channel %s to kernel consumer",
336 channel
->channel
->name
);
338 if (!channel
->sent_to_consumer
) {
339 ret
= kernel_consumer_add_channel(sock
, channel
, session
, monitor
);
343 channel
->sent_to_consumer
= true;
347 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
348 if (!stream
->fd
|| stream
->sent_to_consumer
) {
352 /* Add stream on the kernel consumer side. */
353 ret
= kernel_consumer_add_stream(sock
, channel
, stream
, session
,
358 stream
->sent_to_consumer
= true;
366 * Send all stream fds of the kernel session to the consumer.
368 * The consumer socket lock must be held by the caller.
370 int kernel_consumer_send_session(struct consumer_socket
*sock
,
371 struct ltt_kernel_session
*session
)
373 int ret
, monitor
= 0;
374 struct ltt_kernel_channel
*chan
;
378 assert(session
->consumer
);
381 /* Bail out if consumer is disabled */
382 if (!session
->consumer
->enabled
) {
387 /* Don't monitor the streams on the consumer if in flight recorder. */
388 if (session
->output_traces
) {
392 DBG("Sending session stream to kernel consumer");
394 if (session
->metadata_stream_fd
>= 0 && session
->metadata
) {
395 ret
= kernel_consumer_add_metadata(sock
, session
, monitor
);
401 /* Send channel and streams of it */
402 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
403 ret
= kernel_consumer_send_channel_stream(sock
, chan
, session
,
410 * Inform the relay that all the streams for the
413 ret
= kernel_consumer_streams_sent(sock
, session
, chan
->fd
);
420 DBG("Kernel consumer FDs of metadata and channel streams sent");
422 session
->consumer_fds_sent
= 1;
429 int kernel_consumer_destroy_channel(struct consumer_socket
*socket
,
430 struct ltt_kernel_channel
*channel
)
433 struct lttcomm_consumer_msg msg
;
438 DBG("Sending kernel consumer destroy channel key %d", channel
->fd
);
440 memset(&msg
, 0, sizeof(msg
));
441 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
442 msg
.u
.destroy_channel
.key
= channel
->fd
;
444 pthread_mutex_lock(socket
->lock
);
445 health_code_update();
447 ret
= consumer_send_msg(socket
, &msg
);
453 health_code_update();
454 pthread_mutex_unlock(socket
->lock
);
458 int kernel_consumer_destroy_metadata(struct consumer_socket
*socket
,
459 struct ltt_kernel_metadata
*metadata
)
462 struct lttcomm_consumer_msg msg
;
467 DBG("Sending kernel consumer destroy channel key %d", metadata
->fd
);
469 memset(&msg
, 0, sizeof(msg
));
470 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
471 msg
.u
.destroy_channel
.key
= metadata
->fd
;
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
);