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(struct consumer_socket
*sock
,
35 struct ltt_kernel_channel
*channel
)
38 struct lttcomm_consumer_msg lkm
;
43 DBG("Kernel consumer adding channel %s to kernel consumer",
44 channel
->channel
->name
);
46 /* Prep channel message structure */
47 consumer_init_channel_comm_msg(&lkm
,
48 LTTNG_CONSUMER_ADD_CHANNEL
,
50 channel
->channel
->attr
.subbuf_size
,
52 channel
->channel
->name
,
53 channel
->stream_count
);
55 ret
= consumer_send_channel(sock
, &lkm
);
65 * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
67 int kernel_consumer_add_metadata(struct consumer_socket
*sock
,
68 struct ltt_kernel_session
*session
)
71 char tmp_path
[PATH_MAX
];
73 struct lttcomm_consumer_msg lkm
;
74 struct consumer_output
*consumer
;
78 assert(session
->consumer
);
81 DBG("Sending metadata %d to kernel consumer", session
->metadata_stream_fd
);
83 /* Get consumer output pointer */
84 consumer
= session
->consumer
;
86 /* Get the right path name destination */
87 if (consumer
->type
== CONSUMER_DST_LOCAL
) {
88 /* Set application path to the destination path */
89 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
90 consumer
->dst
.trace_path
, consumer
->subdir
);
92 PERROR("snprintf metadata path");
97 /* Create directory */
98 ret
= run_as_mkdir_recursive(pathname
, S_IRWXU
| S_IRWXG
,
99 session
->uid
, session
->gid
);
101 if (ret
!= -EEXIST
) {
102 ERR("Trace directory creation error");
106 DBG3("Kernel local consumer tracefile path: %s", pathname
);
108 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s", consumer
->subdir
);
110 PERROR("snprintf metadata path");
114 DBG3("Kernel network consumer subdir path: %s", pathname
);
117 /* Prep channel message structure */
118 consumer_init_channel_comm_msg(&lkm
,
119 LTTNG_CONSUMER_ADD_CHANNEL
,
120 session
->metadata
->fd
,
121 session
->metadata
->conf
->attr
.subbuf_size
,
126 ret
= consumer_send_channel(sock
, &lkm
);
131 /* Prep stream message structure */
132 consumer_init_stream_comm_msg(&lkm
,
133 LTTNG_CONSUMER_ADD_STREAM
,
134 session
->metadata
->fd
,
135 session
->metadata_stream_fd
,
136 LTTNG_CONSUMER_ACTIVE_STREAM
,
137 DEFAULT_KERNEL_CHANNEL_OUTPUT
,
141 consumer
->net_seq_index
,
142 1, /* Metadata flag set */
147 /* Send stream and file descriptor */
148 ret
= consumer_send_stream(sock
, consumer
, &lkm
,
149 &session
->metadata_stream_fd
, 1);
159 * Sending a single stream to the consumer with command ADD_STREAM.
161 int kernel_consumer_add_stream(struct consumer_socket
*sock
,
162 struct ltt_kernel_channel
*channel
, struct ltt_kernel_stream
*stream
,
163 struct ltt_kernel_session
*session
)
166 char tmp_path
[PATH_MAX
];
167 const char *pathname
;
168 struct lttcomm_consumer_msg lkm
;
169 struct consumer_output
*consumer
;
174 assert(session
->consumer
);
177 DBG("Sending stream %d of channel %s to kernel consumer",
178 stream
->fd
, channel
->channel
->name
);
180 /* Get consumer output pointer */
181 consumer
= session
->consumer
;
183 /* Get the right path name destination */
184 if (consumer
->type
== CONSUMER_DST_LOCAL
) {
185 /* Set application path to the destination path */
186 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
187 consumer
->dst
.trace_path
, consumer
->subdir
);
189 PERROR("snprintf stream path");
193 DBG3("Kernel local consumer tracefile path: %s", pathname
);
195 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s", consumer
->subdir
);
197 PERROR("snprintf stream path");
201 DBG3("Kernel network consumer subdir path: %s", pathname
);
204 /* Prep stream consumer message */
205 consumer_init_stream_comm_msg(&lkm
, LTTNG_CONSUMER_ADD_STREAM
,
209 channel
->channel
->attr
.output
,
213 consumer
->net_seq_index
,
214 0, /* Metadata flag unset */
219 /* Send stream and file descriptor */
220 ret
= consumer_send_stream(sock
, consumer
, &lkm
, &stream
->fd
, 1);
230 * Send all stream fds of kernel channel to the consumer.
232 int kernel_consumer_send_channel_stream(struct consumer_socket
*sock
,
233 struct ltt_kernel_channel
*channel
, struct ltt_kernel_session
*session
)
236 struct ltt_kernel_stream
*stream
;
241 assert(session
->consumer
);
244 /* Bail out if consumer is disabled */
245 if (!session
->consumer
->enabled
) {
250 DBG("Sending streams of channel %s to kernel consumer",
251 channel
->channel
->name
);
253 ret
= kernel_consumer_add_channel(sock
, channel
);
259 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
264 /* Add stream on the kernel consumer side. */
265 ret
= kernel_consumer_add_stream(sock
, channel
, stream
, session
);
276 * Send all stream fds of the kernel session to the consumer.
278 int kernel_consumer_send_session(struct consumer_socket
*sock
,
279 struct ltt_kernel_session
*session
)
282 struct ltt_kernel_channel
*chan
;
286 assert(session
->consumer
);
289 /* Bail out if consumer is disabled */
290 if (!session
->consumer
->enabled
) {
295 DBG("Sending session stream to kernel consumer");
297 if (session
->metadata_stream_fd
>= 0) {
298 ret
= kernel_consumer_add_metadata(sock
, session
);
303 /* Flag that at least the metadata has been sent to the consumer. */
304 session
->consumer_fds_sent
= 1;
307 /* Send channel and streams of it */
308 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
309 ret
= kernel_consumer_send_channel_stream(sock
, chan
, session
);
315 DBG("Kernel consumer FDs of metadata and channel streams sent");