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
,
136 session
->is_live_session
);
138 health_code_update();
140 ret
= consumer_send_channel(sock
, &lkm
);
145 health_code_update();
153 * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
155 * The consumer socket lock must be held by the caller.
157 int kernel_consumer_add_metadata(struct consumer_socket
*sock
,
158 struct ltt_kernel_session
*session
, unsigned int monitor
)
162 struct lttcomm_consumer_msg lkm
;
163 struct consumer_output
*consumer
;
167 assert(session
->consumer
);
170 DBG("Sending metadata %d to kernel consumer", session
->metadata_stream_fd
);
172 /* Get consumer output pointer */
173 consumer
= session
->consumer
;
176 pathname
= create_channel_path(consumer
, session
->uid
, session
->gid
);
179 pathname
= strdup("");
186 /* Prep channel message structure */
187 consumer_init_channel_comm_msg(&lkm
,
188 LTTNG_CONSUMER_ADD_CHANNEL
,
189 session
->metadata
->fd
,
194 consumer
->net_seq_index
,
195 DEFAULT_METADATA_NAME
,
197 DEFAULT_KERNEL_CHANNEL_OUTPUT
,
198 CONSUMER_CHANNEL_TYPE_METADATA
,
200 monitor
, 0, session
->is_live_session
);
202 health_code_update();
204 ret
= consumer_send_channel(sock
, &lkm
);
209 health_code_update();
211 /* Prep stream message structure */
212 consumer_init_stream_comm_msg(&lkm
,
213 LTTNG_CONSUMER_ADD_STREAM
,
214 session
->metadata
->fd
,
215 session
->metadata_stream_fd
,
216 0); /* CPU: 0 for metadata. */
218 health_code_update();
220 /* Send stream and file descriptor */
221 ret
= consumer_send_stream(sock
, consumer
, &lkm
,
222 &session
->metadata_stream_fd
, 1);
227 health_code_update();
235 * Sending a single stream to the consumer with command ADD_STREAM.
237 int kernel_consumer_add_stream(struct consumer_socket
*sock
,
238 struct ltt_kernel_channel
*channel
, struct ltt_kernel_stream
*stream
,
239 struct ltt_kernel_session
*session
, unsigned int monitor
)
242 struct lttcomm_consumer_msg lkm
;
243 struct consumer_output
*consumer
;
248 assert(session
->consumer
);
251 DBG("Sending stream %d of channel %s to kernel consumer",
252 stream
->fd
, channel
->channel
->name
);
254 /* Get consumer output pointer */
255 consumer
= session
->consumer
;
257 /* Prep stream consumer message */
258 consumer_init_stream_comm_msg(&lkm
,
259 LTTNG_CONSUMER_ADD_STREAM
,
264 health_code_update();
266 /* Send stream and file descriptor */
267 ret
= consumer_send_stream(sock
, consumer
, &lkm
, &stream
->fd
, 1);
272 health_code_update();
279 * Sending the notification that all streams were sent with STREAMS_SENT.
281 int kernel_consumer_streams_sent(struct consumer_socket
*sock
,
282 struct ltt_kernel_session
*session
, uint64_t channel_key
)
285 struct lttcomm_consumer_msg lkm
;
286 struct consumer_output
*consumer
;
291 DBG("Sending streams_sent");
292 /* Get consumer output pointer */
293 consumer
= session
->consumer
;
295 /* Prep stream consumer message */
296 consumer_init_streams_sent_comm_msg(&lkm
,
297 LTTNG_CONSUMER_STREAMS_SENT
,
298 channel_key
, consumer
->net_seq_index
);
300 health_code_update();
302 /* Send stream and file descriptor */
303 ret
= consumer_send_msg(sock
, &lkm
);
313 * Send all stream fds of kernel channel to the consumer.
315 * The consumer socket lock must be held by the caller.
317 int kernel_consumer_send_channel_stream(struct consumer_socket
*sock
,
318 struct ltt_kernel_channel
*channel
, struct ltt_kernel_session
*session
,
319 unsigned int monitor
)
322 struct ltt_kernel_stream
*stream
;
327 assert(session
->consumer
);
330 /* Bail out if consumer is disabled */
331 if (!session
->consumer
->enabled
) {
336 DBG("Sending streams of channel %s to kernel consumer",
337 channel
->channel
->name
);
339 if (!channel
->sent_to_consumer
) {
340 ret
= kernel_consumer_add_channel(sock
, channel
, session
, monitor
);
344 channel
->sent_to_consumer
= true;
348 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
349 if (!stream
->fd
|| stream
->sent_to_consumer
) {
353 /* Add stream on the kernel consumer side. */
354 ret
= kernel_consumer_add_stream(sock
, channel
, stream
, session
,
359 stream
->sent_to_consumer
= true;
367 * Send all stream fds of the kernel session to the consumer.
369 * The consumer socket lock must be held by the caller.
371 int kernel_consumer_send_session(struct consumer_socket
*sock
,
372 struct ltt_kernel_session
*session
)
374 int ret
, monitor
= 0;
375 struct ltt_kernel_channel
*chan
;
379 assert(session
->consumer
);
382 /* Bail out if consumer is disabled */
383 if (!session
->consumer
->enabled
) {
388 /* Don't monitor the streams on the consumer if in flight recorder. */
389 if (session
->output_traces
) {
393 DBG("Sending session stream to kernel consumer");
395 if (session
->metadata_stream_fd
>= 0 && session
->metadata
) {
396 ret
= kernel_consumer_add_metadata(sock
, session
, monitor
);
402 /* Send channel and streams of it */
403 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
404 ret
= kernel_consumer_send_channel_stream(sock
, chan
, session
,
411 * Inform the relay that all the streams for the
414 ret
= kernel_consumer_streams_sent(sock
, session
, chan
->fd
);
421 DBG("Kernel consumer FDs of metadata and channel streams sent");
423 session
->consumer_fds_sent
= 1;
430 int kernel_consumer_destroy_channel(struct consumer_socket
*socket
,
431 struct ltt_kernel_channel
*channel
)
434 struct lttcomm_consumer_msg msg
;
439 DBG("Sending kernel consumer destroy channel key %d", channel
->fd
);
441 memset(&msg
, 0, sizeof(msg
));
442 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
443 msg
.u
.destroy_channel
.key
= channel
->fd
;
445 pthread_mutex_lock(socket
->lock
);
446 health_code_update();
448 ret
= consumer_send_msg(socket
, &msg
);
454 health_code_update();
455 pthread_mutex_unlock(socket
->lock
);
459 int kernel_consumer_destroy_metadata(struct consumer_socket
*socket
,
460 struct ltt_kernel_metadata
*metadata
)
463 struct lttcomm_consumer_msg msg
;
468 DBG("Sending kernel consumer destroy channel key %d", metadata
->fd
);
470 memset(&msg
, 0, sizeof(msg
));
471 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
472 msg
.u
.destroy_channel
.key
= metadata
->fd
;
474 pthread_mutex_lock(socket
->lock
);
475 health_code_update();
477 ret
= consumer_send_msg(socket
, &msg
);
483 health_code_update();
484 pthread_mutex_unlock(socket
->lock
);