Consumer daemon data available command support
[lttng-tools.git] / src / bin / lttng-sessiond / kernel-consumer.c
CommitLineData
f1e16794
DG
1/*
2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
3 *
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.
7 *
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
11 * more details.
12 *
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.
16 */
17
18#define _GNU_SOURCE
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <unistd.h>
24
25#include <common/common.h>
26#include <common/defaults.h>
f1e16794 27
00e2e675 28#include "consumer.h"
f1e16794
DG
29#include "kernel-consumer.h"
30
00e2e675
DG
31/*
32 * Sending a single channel to the consumer with command ADD_CHANNEL.
33 */
34int kernel_consumer_add_channel(int sock, struct ltt_kernel_channel *channel)
35{
36 int ret;
37 struct lttcomm_consumer_msg lkm;
38
39 /* Safety net */
40 assert(channel);
41
42 DBG("Kernel consumer adding channel %s to kernel consumer",
43 channel->channel->name);
44
45 /* Prep channel message structure */
46 consumer_init_channel_comm_msg(&lkm,
47 LTTNG_CONSUMER_ADD_CHANNEL,
48 channel->fd,
49 channel->channel->attr.subbuf_size,
50 0, /* Kernel */
c30aaa51
MD
51 channel->channel->name,
52 channel->stream_count);
00e2e675
DG
53
54 ret = consumer_send_channel(sock, &lkm);
55 if (ret < 0) {
56 goto error;
57 }
58
59error:
60 return ret;
61}
62
63/*
64 * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
65 */
66int kernel_consumer_add_metadata(int sock, struct ltt_kernel_session *session)
67{
68 int ret;
a7d9a3e7 69 char tmp_path[PATH_MAX];
00e2e675
DG
70 const char *pathname;
71 struct lttcomm_consumer_msg lkm;
a7d9a3e7 72 struct consumer_output *consumer;
00e2e675
DG
73
74 /* Safety net */
75 assert(session);
76 assert(session->consumer);
77
78 DBG("Sending metadata %d to kernel consumer", session->metadata_stream_fd);
79
80 /* Get consumer output pointer */
a7d9a3e7 81 consumer = session->consumer;
00e2e675 82
a7d9a3e7
DG
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);
88 if (ret < 0) {
89 PERROR("snprintf metadata path");
90 goto error;
91 }
92 pathname = tmp_path;
93
94 /* Create directory */
95 ret = run_as_mkdir_recursive(pathname, S_IRWXU | S_IRWXG,
96 session->uid, session->gid);
97 if (ret < 0) {
98 if (ret != -EEXIST) {
99 ERR("Trace directory creation error");
100 goto error;
101 }
102 }
103 DBG3("Kernel local consumer tracefile path: %s", pathname);
00e2e675 104 } else {
a7d9a3e7
DG
105 ret = snprintf(tmp_path, sizeof(tmp_path), "%s", consumer->subdir);
106 if (ret < 0) {
107 PERROR("snprintf metadata path");
108 goto error;
109 }
110 pathname = tmp_path;
111 DBG3("Kernel network consumer subdir path: %s", pathname);
00e2e675
DG
112 }
113
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,
119 0, /* for kernel */
c30aaa51
MD
120 "metadata",
121 1);
00e2e675
DG
122
123 ret = consumer_send_channel(sock, &lkm);
124 if (ret < 0) {
125 goto error;
126 }
127
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,
135 0, /* Kernel */
136 session->uid,
137 session->gid,
a7d9a3e7 138 consumer->net_seq_index,
00e2e675
DG
139 1, /* Metadata flag set */
140 "metadata",
ca22feea
DG
141 pathname,
142 session->id);
00e2e675
DG
143
144 /* Send stream and file descriptor */
a7d9a3e7 145 ret = consumer_send_stream(sock, consumer, &lkm,
00e2e675
DG
146 &session->metadata_stream_fd, 1);
147 if (ret < 0) {
148 goto error;
149 }
150
151error:
152 return ret;
153}
154
155/*
156 * Sending a single stream to the consumer with command ADD_STREAM.
157 */
158int kernel_consumer_add_stream(int sock, struct ltt_kernel_channel *channel,
159 struct ltt_kernel_stream *stream, struct ltt_kernel_session *session)
160{
161 int ret;
a7d9a3e7 162 char tmp_path[PATH_MAX];
00e2e675
DG
163 const char *pathname;
164 struct lttcomm_consumer_msg lkm;
a7d9a3e7 165 struct consumer_output *consumer;
00e2e675
DG
166
167 assert(channel);
168 assert(stream);
169 assert(session);
170 assert(session->consumer);
171
172 DBG("Sending stream %d of channel %s to kernel consumer",
173 stream->fd, channel->channel->name);
174
175 /* Get consumer output pointer */
a7d9a3e7 176 consumer = session->consumer;
00e2e675 177
a7d9a3e7
DG
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);
183 if (ret < 0) {
184 PERROR("snprintf stream path");
185 goto error;
186 }
187 pathname = tmp_path;
188 DBG3("Kernel local consumer tracefile path: %s", pathname);
00e2e675 189 } else {
a7d9a3e7
DG
190 ret = snprintf(tmp_path, sizeof(tmp_path), "%s", consumer->subdir);
191 if (ret < 0) {
192 PERROR("snprintf stream path");
193 goto error;
194 }
195 pathname = tmp_path;
196 DBG3("Kernel network consumer subdir path: %s", pathname);
00e2e675
DG
197 }
198
199 /* Prep stream consumer message */
200 consumer_init_stream_comm_msg(&lkm, LTTNG_CONSUMER_ADD_STREAM,
201 channel->fd,
202 stream->fd,
203 stream->state,
204 channel->channel->attr.output,
205 0, /* Kernel */
206 session->uid,
207 session->gid,
a7d9a3e7 208 consumer->net_seq_index,
00e2e675
DG
209 0, /* Metadata flag unset */
210 stream->name,
ca22feea
DG
211 pathname,
212 session->id);
00e2e675
DG
213
214 /* Send stream and file descriptor */
a7d9a3e7 215 ret = consumer_send_stream(sock, consumer, &lkm, &stream->fd, 1);
00e2e675
DG
216 if (ret < 0) {
217 goto error;
218 }
219
220error:
221 return ret;
222}
223
f1e16794
DG
224/*
225 * Send all stream fds of kernel channel to the consumer.
226 */
00e2e675
DG
227int kernel_consumer_send_channel_stream(int sock,
228 struct ltt_kernel_channel *channel, struct ltt_kernel_session *session)
f1e16794 229{
00e2e675 230 int ret;
f1e16794 231 struct ltt_kernel_stream *stream;
00e2e675
DG
232
233 /* Safety net */
234 assert(channel);
235 assert(session);
236 assert(session->consumer);
237
238 /* Bail out if consumer is disabled */
239 if (!session->consumer->enabled) {
f73fabfd 240 ret = LTTNG_OK;
00e2e675
DG
241 goto error;
242 }
f1e16794
DG
243
244 DBG("Sending streams of channel %s to kernel consumer",
245 channel->channel->name);
246
00e2e675 247 ret = kernel_consumer_add_channel(sock, channel);
f1e16794 248 if (ret < 0) {
f1e16794
DG
249 goto error;
250 }
251
252 /* Send streams */
253 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
254 if (!stream->fd) {
255 continue;
256 }
00e2e675
DG
257
258 /* Add stream on the kernel consumer side. */
259 ret = kernel_consumer_add_stream(sock, channel, stream, session);
f1e16794 260 if (ret < 0) {
f1e16794
DG
261 goto error;
262 }
263 }
264
f1e16794
DG
265error:
266 return ret;
267}
268
269/*
270 * Send all stream fds of the kernel session to the consumer.
271 */
00e2e675 272int kernel_consumer_send_session(int sock, struct ltt_kernel_session *session)
f1e16794
DG
273{
274 int ret;
275 struct ltt_kernel_channel *chan;
f1e16794 276
00e2e675
DG
277 /* Safety net */
278 assert(session);
279 assert(session->consumer);
f1e16794 280
00e2e675
DG
281 /* Bail out if consumer is disabled */
282 if (!session->consumer->enabled) {
f73fabfd 283 ret = LTTNG_OK;
00e2e675 284 goto error;
f1e16794
DG
285 }
286
00e2e675
DG
287 DBG("Sending session stream to kernel consumer");
288
f1e16794 289 if (session->metadata_stream_fd >= 0) {
00e2e675 290 ret = kernel_consumer_add_metadata(sock, session);
f1e16794 291 if (ret < 0) {
f1e16794
DG
292 goto error;
293 }
294
00e2e675
DG
295 /* Flag that at least the metadata has been sent to the consumer. */
296 session->consumer_fds_sent = 1;
f1e16794
DG
297 }
298
00e2e675 299 /* Send channel and streams of it */
f1e16794 300 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
00e2e675 301 ret = kernel_consumer_send_channel_stream(sock, chan, session);
f1e16794
DG
302 if (ret < 0) {
303 goto error;
304 }
305 }
306
00e2e675 307 DBG("Kernel consumer FDs of metadata and channel streams sent");
f1e16794
DG
308
309 return 0;
310
311error:
312 return ret;
313}
This page took 0.05463 seconds and 4 git commands to generate.