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
);
53 ret
= consumer_send_channel(sock
, &lkm
);
63 * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
65 int kernel_consumer_add_metadata(int sock
, struct ltt_kernel_session
*session
)
69 struct lttcomm_consumer_msg lkm
;
70 struct consumer_output
*output
;
74 assert(session
->consumer
);
76 DBG("Sending metadata %d to kernel consumer", session
->metadata_stream_fd
);
78 /* Get consumer output pointer */
79 output
= session
->consumer
;
81 /* Get correct path name destination */
82 if (output
->type
== CONSUMER_DST_LOCAL
) {
83 pathname
= output
->dst
.trace_path
;
85 pathname
= output
->subdir
;
88 /* Prep channel message structure */
89 consumer_init_channel_comm_msg(&lkm
,
90 LTTNG_CONSUMER_ADD_CHANNEL
,
91 session
->metadata
->fd
,
92 session
->metadata
->conf
->attr
.subbuf_size
,
96 ret
= consumer_send_channel(sock
, &lkm
);
101 /* Prep stream message structure */
102 consumer_init_stream_comm_msg(&lkm
,
103 LTTNG_CONSUMER_ADD_STREAM
,
104 session
->metadata
->fd
,
105 session
->metadata_stream_fd
,
106 LTTNG_CONSUMER_ACTIVE_STREAM
,
107 DEFAULT_KERNEL_CHANNEL_OUTPUT
,
111 output
->net_seq_index
,
112 1, /* Metadata flag set */
116 /* Send stream and file descriptor */
117 ret
= consumer_send_stream(sock
, output
, &lkm
,
118 &session
->metadata_stream_fd
, 1);
128 * Sending a single stream to the consumer with command ADD_STREAM.
130 int kernel_consumer_add_stream(int sock
, struct ltt_kernel_channel
*channel
,
131 struct ltt_kernel_stream
*stream
, struct ltt_kernel_session
*session
)
134 const char *pathname
;
135 struct lttcomm_consumer_msg lkm
;
136 struct consumer_output
*output
;
141 assert(session
->consumer
);
143 DBG("Sending stream %d of channel %s to kernel consumer",
144 stream
->fd
, channel
->channel
->name
);
146 /* Get consumer output pointer */
147 output
= session
->consumer
;
149 /* Get correct path name destination */
150 if (output
->type
== CONSUMER_DST_LOCAL
) {
151 pathname
= output
->dst
.trace_path
;
152 DBG3("Consumer is local to %s", pathname
);
154 pathname
= output
->subdir
;
155 DBG3("Consumer is network to subdir %s", pathname
);
158 /* Prep stream consumer message */
159 consumer_init_stream_comm_msg(&lkm
, LTTNG_CONSUMER_ADD_STREAM
,
163 channel
->channel
->attr
.output
,
167 output
->net_seq_index
,
168 0, /* Metadata flag unset */
172 /* Send stream and file descriptor */
173 ret
= consumer_send_stream(sock
, output
, &lkm
, &stream
->fd
, 1);
183 * Send all stream fds of kernel channel to the consumer.
185 int kernel_consumer_send_channel_stream(int sock
,
186 struct ltt_kernel_channel
*channel
, struct ltt_kernel_session
*session
)
189 struct ltt_kernel_stream
*stream
;
194 assert(session
->consumer
);
196 /* Bail out if consumer is disabled */
197 if (!session
->consumer
->enabled
) {
202 DBG("Sending streams of channel %s to kernel consumer",
203 channel
->channel
->name
);
205 ret
= kernel_consumer_add_channel(sock
, channel
);
211 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
216 /* Add stream on the kernel consumer side. */
217 ret
= kernel_consumer_add_stream(sock
, channel
, stream
, session
);
228 * Send all stream fds of the kernel session to the consumer.
230 int kernel_consumer_send_session(int sock
, struct ltt_kernel_session
*session
)
233 struct ltt_kernel_channel
*chan
;
237 assert(session
->consumer
);
239 /* Bail out if consumer is disabled */
240 if (!session
->consumer
->enabled
) {
245 DBG("Sending session stream to kernel consumer");
247 if (session
->metadata_stream_fd
>= 0) {
248 ret
= kernel_consumer_add_metadata(sock
, session
);
253 /* Flag that at least the metadata has been sent to the consumer. */
254 session
->consumer_fds_sent
= 1;
257 /* Send channel and streams of it */
258 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
259 ret
= kernel_consumer_send_channel_stream(sock
, chan
, session
);
265 DBG("Kernel consumer FDs of metadata and channel streams sent");
274 * Send relayd socket to consumer associated with a session name.
276 * On success return positive value. On error, negative value.
278 int kernel_consumer_send_relayd_socket(int consumer_sock
,
279 struct lttcomm_sock
*sock
, struct consumer_output
*consumer
,
280 enum lttng_stream_type type
)
283 struct lttcomm_consumer_msg msg
;
285 /* Code flow error. Safety net. */
289 /* Bail out if consumer is disabled */
290 if (!consumer
->enabled
) {
295 msg
.cmd_type
= LTTNG_CONSUMER_ADD_RELAYD_SOCKET
;
297 * Assign network consumer output index using the temporary consumer since
298 * this call should only be made from within a set_consumer_uri() function
299 * call in the session daemon.
301 msg
.u
.relayd_sock
.net_index
= consumer
->net_seq_index
;
302 msg
.u
.relayd_sock
.type
= type
;
303 memcpy(&msg
.u
.relayd_sock
.sock
, sock
, sizeof(msg
.u
.relayd_sock
.sock
));
305 DBG2("Sending relayd sock info to consumer");
306 ret
= lttcomm_send_unix_sock(consumer_sock
, &msg
, sizeof(msg
));
308 PERROR("send consumer relayd socket info");
312 DBG2("Sending relayd socket file descriptor to consumer");
313 ret
= consumer_send_fds(consumer_sock
, &sock
->fd
, 1);
318 DBG("Kernel consumer relayd socket sent");