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>
29 #include "kernel-consumer.h"
32 * Sending a single channel to the consumer with command ADD_CHANNEL.
34 int kernel_consumer_add_channel(int sock
, struct ltt_kernel_channel
*channel
)
37 struct lttcomm_consumer_msg lkm
;
42 DBG("Kernel consumer adding channel %s to kernel consumer",
43 channel
->channel
->name
);
45 /* Prep channel message structure */
46 consumer_init_channel_comm_msg(&lkm
,
47 LTTNG_CONSUMER_ADD_CHANNEL
,
49 channel
->channel
->attr
.subbuf_size
,
51 channel
->channel
->name
,
52 channel
->stream_count
);
54 ret
= consumer_send_channel(sock
, &lkm
);
64 * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
66 int kernel_consumer_add_metadata(int sock
, struct ltt_kernel_session
*session
)
69 char tmp_path
[PATH_MAX
];
71 struct lttcomm_consumer_msg lkm
;
72 struct consumer_output
*consumer
;
76 assert(session
->consumer
);
78 DBG("Sending metadata %d to kernel consumer", session
->metadata_stream_fd
);
80 /* Get consumer output pointer */
81 consumer
= session
->consumer
;
83 /* Get the right path name destination */
84 if (consumer
->type
== CONSUMER_DST_LOCAL
) {
85 /* Set application path to the destination path */
86 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
87 consumer
->dst
.trace_path
, consumer
->subdir
);
89 PERROR("snprintf metadata path");
94 /* Create directory */
95 ret
= run_as_mkdir_recursive(pathname
, S_IRWXU
| S_IRWXG
,
96 session
->uid
, session
->gid
);
99 ERR("Trace directory creation error");
103 DBG3("Kernel local consumer tracefile path: %s", pathname
);
105 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s", consumer
->subdir
);
107 PERROR("snprintf metadata path");
111 DBG3("Kernel network consumer subdir path: %s", pathname
);
114 /* Prep channel message structure */
115 consumer_init_channel_comm_msg(&lkm
,
116 LTTNG_CONSUMER_ADD_CHANNEL
,
117 session
->metadata
->fd
,
118 session
->metadata
->conf
->attr
.subbuf_size
,
123 ret
= consumer_send_channel(sock
, &lkm
);
128 /* Prep stream message structure */
129 consumer_init_stream_comm_msg(&lkm
,
130 LTTNG_CONSUMER_ADD_STREAM
,
131 session
->metadata
->fd
,
132 session
->metadata_stream_fd
,
133 LTTNG_CONSUMER_ACTIVE_STREAM
,
134 DEFAULT_KERNEL_CHANNEL_OUTPUT
,
138 consumer
->net_seq_index
,
139 1, /* Metadata flag set */
144 /* Send stream and file descriptor */
145 ret
= consumer_send_stream(sock
, consumer
, &lkm
,
146 &session
->metadata_stream_fd
, 1);
156 * Sending a single stream to the consumer with command ADD_STREAM.
158 int kernel_consumer_add_stream(int sock
, struct ltt_kernel_channel
*channel
,
159 struct ltt_kernel_stream
*stream
, struct ltt_kernel_session
*session
)
162 char tmp_path
[PATH_MAX
];
163 const char *pathname
;
164 struct lttcomm_consumer_msg lkm
;
165 struct consumer_output
*consumer
;
170 assert(session
->consumer
);
172 DBG("Sending stream %d of channel %s to kernel consumer",
173 stream
->fd
, channel
->channel
->name
);
175 /* Get consumer output pointer */
176 consumer
= session
->consumer
;
178 /* Get the right path name destination */
179 if (consumer
->type
== CONSUMER_DST_LOCAL
) {
180 /* Set application path to the destination path */
181 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
182 consumer
->dst
.trace_path
, consumer
->subdir
);
184 PERROR("snprintf stream path");
188 DBG3("Kernel local consumer tracefile path: %s", pathname
);
190 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s", consumer
->subdir
);
192 PERROR("snprintf stream path");
196 DBG3("Kernel network consumer subdir path: %s", pathname
);
199 /* Prep stream consumer message */
200 consumer_init_stream_comm_msg(&lkm
, LTTNG_CONSUMER_ADD_STREAM
,
204 channel
->channel
->attr
.output
,
208 consumer
->net_seq_index
,
209 0, /* Metadata flag unset */
214 /* Send stream and file descriptor */
215 ret
= consumer_send_stream(sock
, consumer
, &lkm
, &stream
->fd
, 1);
225 * Send all stream fds of kernel channel to the consumer.
227 int kernel_consumer_send_channel_stream(int sock
,
228 struct ltt_kernel_channel
*channel
, struct ltt_kernel_session
*session
)
231 struct ltt_kernel_stream
*stream
;
236 assert(session
->consumer
);
238 /* Bail out if consumer is disabled */
239 if (!session
->consumer
->enabled
) {
244 DBG("Sending streams of channel %s to kernel consumer",
245 channel
->channel
->name
);
247 ret
= kernel_consumer_add_channel(sock
, channel
);
253 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
258 /* Add stream on the kernel consumer side. */
259 ret
= kernel_consumer_add_stream(sock
, channel
, stream
, session
);
270 * Send all stream fds of the kernel session to the consumer.
272 int kernel_consumer_send_session(int sock
, struct ltt_kernel_session
*session
)
275 struct ltt_kernel_channel
*chan
;
279 assert(session
->consumer
);
281 /* Bail out if consumer is disabled */
282 if (!session
->consumer
->enabled
) {
287 DBG("Sending session stream to kernel consumer");
289 if (session
->metadata_stream_fd
>= 0) {
290 ret
= kernel_consumer_add_metadata(sock
, session
);
295 /* Flag that at least the metadata has been sent to the consumer. */
296 session
->consumer_fds_sent
= 1;
299 /* Send channel and streams of it */
300 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
301 ret
= kernel_consumer_send_channel_stream(sock
, chan
, session
);
307 DBG("Kernel consumer FDs of metadata and channel streams sent");