Add lttng_waiter utils
[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
6c1c0768 18#define _LGPL_SOURCE
f1e16794
DG
19#include <stdio.h>
20#include <stdlib.h>
f1e16794
DG
21#include <sys/stat.h>
22#include <unistd.h>
23
24#include <common/common.h>
25#include <common/defaults.h>
f5436bfc 26#include <common/compat/string.h>
f1e16794 27
00e2e675 28#include "consumer.h"
8782cc74 29#include "health-sessiond.h"
f1e16794 30#include "kernel-consumer.h"
e9404c27
JG
31#include "notification-thread-commands.h"
32#include "session.h"
33#include "lttng-sessiond.h"
f1e16794 34
2bba9e53
DG
35static char *create_channel_path(struct consumer_output *consumer,
36 uid_t uid, gid_t gid)
00e2e675
DG
37{
38 int ret;
ffe60014 39 char tmp_path[PATH_MAX];
2bba9e53 40 char *pathname = NULL;
00e2e675 41
2bba9e53 42 assert(consumer);
00e2e675 43
ffe60014
DG
44 /* Get the right path name destination */
45 if (consumer->type == CONSUMER_DST_LOCAL) {
46 /* Set application path to the destination path */
dec56f6c 47 ret = snprintf(tmp_path, sizeof(tmp_path), "%s%s",
ffe60014
DG
48 consumer->dst.trace_path, consumer->subdir);
49 if (ret < 0) {
2bba9e53 50 PERROR("snprintf kernel channel path");
ffe60014
DG
51 goto error;
52 }
f5436bfc 53 pathname = lttng_strndup(tmp_path, sizeof(tmp_path));
bb3c4e70 54 if (!pathname) {
f5436bfc 55 PERROR("lttng_strndup");
bb3c4e70
MD
56 goto error;
57 }
ffe60014
DG
58
59 /* Create directory */
2bba9e53 60 ret = run_as_mkdir_recursive(pathname, S_IRWXU | S_IRWXG, uid, gid);
ffe60014 61 if (ret < 0) {
df5b86c8 62 if (errno != EEXIST) {
ffe60014
DG
63 ERR("Trace directory creation error");
64 goto error;
65 }
66 }
67 DBG3("Kernel local consumer tracefile path: %s", pathname);
68 } else {
69 ret = snprintf(tmp_path, sizeof(tmp_path), "%s", consumer->subdir);
70 if (ret < 0) {
2bba9e53 71 PERROR("snprintf kernel metadata path");
ffe60014
DG
72 goto error;
73 }
f5436bfc 74 pathname = lttng_strndup(tmp_path, sizeof(tmp_path));
bb3c4e70 75 if (!pathname) {
f5436bfc 76 PERROR("lttng_strndup");
bb3c4e70
MD
77 goto error;
78 }
ffe60014
DG
79 DBG3("Kernel network consumer subdir path: %s", pathname);
80 }
81
2bba9e53
DG
82 return pathname;
83
84error:
85 free(pathname);
86 return NULL;
87}
88
89/*
90 * Sending a single channel to the consumer with command ADD_CHANNEL.
91 */
92int kernel_consumer_add_channel(struct consumer_socket *sock,
e9404c27
JG
93 struct ltt_kernel_channel *channel,
94 struct ltt_kernel_session *ksession,
2bba9e53
DG
95 unsigned int monitor)
96{
97 int ret;
98 char *pathname;
99 struct lttcomm_consumer_msg lkm;
100 struct consumer_output *consumer;
e9404c27
JG
101 enum lttng_error_code status;
102 struct ltt_session *session;
103 struct lttng_channel_extended *channel_attr_extended;
2bba9e53
DG
104
105 /* Safety net */
106 assert(channel);
e9404c27
JG
107 assert(ksession);
108 assert(ksession->consumer);
2bba9e53 109
e9404c27
JG
110 consumer = ksession->consumer;
111 channel_attr_extended = (struct lttng_channel_extended *)
112 channel->channel->attr.extended.ptr;
2bba9e53
DG
113
114 DBG("Kernel consumer adding channel %s to kernel consumer",
115 channel->channel->name);
116
117 if (monitor) {
e9404c27
JG
118 pathname = create_channel_path(consumer, ksession->uid,
119 ksession->gid);
2bba9e53
DG
120 } else {
121 /* Empty path. */
53efb85a 122 pathname = strdup("");
2bba9e53 123 }
bb3c4e70
MD
124 if (!pathname) {
125 ret = -1;
126 goto error;
127 }
2bba9e53 128
00e2e675
DG
129 /* Prep channel message structure */
130 consumer_init_channel_comm_msg(&lkm,
131 LTTNG_CONSUMER_ADD_CHANNEL,
132 channel->fd,
e9404c27 133 ksession->id,
ffe60014 134 pathname,
e9404c27
JG
135 ksession->uid,
136 ksession->gid,
ffe60014 137 consumer->net_seq_index,
c30aaa51 138 channel->channel->name,
ffe60014
DG
139 channel->stream_count,
140 channel->channel->attr.output,
1624d5b7
JD
141 CONSUMER_CHANNEL_TYPE_DATA,
142 channel->channel->attr.tracefile_size,
2bba9e53 143 channel->channel->attr.tracefile_count,
ecc48a90 144 monitor,
e9404c27
JG
145 channel->channel->attr.live_timer_interval,
146 channel_attr_extended->monitor_timer_interval);
00e2e675 147
840cb59c 148 health_code_update();
ca03de58 149
00e2e675
DG
150 ret = consumer_send_channel(sock, &lkm);
151 if (ret < 0) {
152 goto error;
153 }
154
840cb59c 155 health_code_update();
e9404c27
JG
156 rcu_read_lock();
157 session = session_find_by_id(ksession->id);
158 assert(session);
ca03de58 159
e9404c27
JG
160 status = notification_thread_command_add_channel(
161 notification_thread_handle, session->name,
162 ksession->uid, ksession->gid,
163 channel->channel->name, channel->fd,
164 LTTNG_DOMAIN_KERNEL,
165 channel->channel->attr.subbuf_size * channel->channel->attr.num_subbuf);
166 rcu_read_unlock();
167 if (status != LTTNG_OK) {
168 ret = -1;
169 goto error;
170 }
00e2e675 171error:
53efb85a 172 free(pathname);
00e2e675
DG
173 return ret;
174}
175
176/*
177 * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
178 */
f50f23d9 179int kernel_consumer_add_metadata(struct consumer_socket *sock,
2bba9e53 180 struct ltt_kernel_session *session, unsigned int monitor)
00e2e675
DG
181{
182 int ret;
2bba9e53 183 char *pathname;
00e2e675 184 struct lttcomm_consumer_msg lkm;
a7d9a3e7 185 struct consumer_output *consumer;
00e2e675
DG
186
187 /* Safety net */
188 assert(session);
189 assert(session->consumer);
f50f23d9 190 assert(sock);
00e2e675
DG
191
192 DBG("Sending metadata %d to kernel consumer", session->metadata_stream_fd);
193
194 /* Get consumer output pointer */
a7d9a3e7 195 consumer = session->consumer;
00e2e675 196
2bba9e53
DG
197 if (monitor) {
198 pathname = create_channel_path(consumer, session->uid, session->gid);
00e2e675 199 } else {
2bba9e53 200 /* Empty path. */
53efb85a 201 pathname = strdup("");
00e2e675 202 }
bb3c4e70
MD
203 if (!pathname) {
204 ret = -1;
205 goto error;
206 }
00e2e675
DG
207
208 /* Prep channel message structure */
209 consumer_init_channel_comm_msg(&lkm,
210 LTTNG_CONSUMER_ADD_CHANNEL,
211 session->metadata->fd,
ffe60014
DG
212 session->id,
213 pathname,
214 session->uid,
215 session->gid,
216 consumer->net_seq_index,
30079b6b 217 DEFAULT_METADATA_NAME,
ffe60014
DG
218 1,
219 DEFAULT_KERNEL_CHANNEL_OUTPUT,
1624d5b7 220 CONSUMER_CHANNEL_TYPE_METADATA,
2bba9e53 221 0, 0,
e9404c27 222 monitor, 0, 0);
00e2e675 223
840cb59c 224 health_code_update();
ca03de58 225
00e2e675
DG
226 ret = consumer_send_channel(sock, &lkm);
227 if (ret < 0) {
228 goto error;
229 }
230
840cb59c 231 health_code_update();
ca03de58 232
00e2e675
DG
233 /* Prep stream message structure */
234 consumer_init_stream_comm_msg(&lkm,
235 LTTNG_CONSUMER_ADD_STREAM,
236 session->metadata->fd,
237 session->metadata_stream_fd,
1624d5b7 238 0); /* CPU: 0 for metadata. */
00e2e675 239
840cb59c 240 health_code_update();
ca03de58 241
00e2e675 242 /* Send stream and file descriptor */
a7d9a3e7 243 ret = consumer_send_stream(sock, consumer, &lkm,
00e2e675
DG
244 &session->metadata_stream_fd, 1);
245 if (ret < 0) {
246 goto error;
247 }
248
840cb59c 249 health_code_update();
ca03de58 250
00e2e675 251error:
53efb85a 252 free(pathname);
00e2e675
DG
253 return ret;
254}
255
256/*
257 * Sending a single stream to the consumer with command ADD_STREAM.
258 */
f50f23d9
DG
259int kernel_consumer_add_stream(struct consumer_socket *sock,
260 struct ltt_kernel_channel *channel, struct ltt_kernel_stream *stream,
2bba9e53 261 struct ltt_kernel_session *session, unsigned int monitor)
00e2e675
DG
262{
263 int ret;
00e2e675 264 struct lttcomm_consumer_msg lkm;
a7d9a3e7 265 struct consumer_output *consumer;
00e2e675
DG
266
267 assert(channel);
268 assert(stream);
269 assert(session);
270 assert(session->consumer);
f50f23d9 271 assert(sock);
00e2e675
DG
272
273 DBG("Sending stream %d of channel %s to kernel consumer",
274 stream->fd, channel->channel->name);
275
276 /* Get consumer output pointer */
a7d9a3e7 277 consumer = session->consumer;
00e2e675 278
00e2e675 279 /* Prep stream consumer message */
ffe60014
DG
280 consumer_init_stream_comm_msg(&lkm,
281 LTTNG_CONSUMER_ADD_STREAM,
00e2e675
DG
282 channel->fd,
283 stream->fd,
ffe60014 284 stream->cpu);
00e2e675 285
840cb59c 286 health_code_update();
ca03de58 287
00e2e675 288 /* Send stream and file descriptor */
a7d9a3e7 289 ret = consumer_send_stream(sock, consumer, &lkm, &stream->fd, 1);
00e2e675
DG
290 if (ret < 0) {
291 goto error;
292 }
293
840cb59c 294 health_code_update();
ca03de58 295
00e2e675
DG
296error:
297 return ret;
298}
299
a4baae1b
JD
300/*
301 * Sending the notification that all streams were sent with STREAMS_SENT.
302 */
303int kernel_consumer_streams_sent(struct consumer_socket *sock,
304 struct ltt_kernel_session *session, uint64_t channel_key)
305{
306 int ret;
307 struct lttcomm_consumer_msg lkm;
308 struct consumer_output *consumer;
309
310 assert(sock);
311 assert(session);
312
313 DBG("Sending streams_sent");
314 /* Get consumer output pointer */
315 consumer = session->consumer;
316
317 /* Prep stream consumer message */
318 consumer_init_streams_sent_comm_msg(&lkm,
319 LTTNG_CONSUMER_STREAMS_SENT,
320 channel_key, consumer->net_seq_index);
321
322 health_code_update();
323
324 /* Send stream and file descriptor */
325 ret = consumer_send_msg(sock, &lkm);
326 if (ret < 0) {
327 goto error;
328 }
329
330error:
331 return ret;
332}
333
f1e16794
DG
334/*
335 * Send all stream fds of kernel channel to the consumer.
336 */
f50f23d9 337int kernel_consumer_send_channel_stream(struct consumer_socket *sock,
2bba9e53
DG
338 struct ltt_kernel_channel *channel, struct ltt_kernel_session *session,
339 unsigned int monitor)
f1e16794 340{
00e2e675 341 int ret;
f1e16794 342 struct ltt_kernel_stream *stream;
00e2e675
DG
343
344 /* Safety net */
345 assert(channel);
346 assert(session);
347 assert(session->consumer);
f50f23d9 348 assert(sock);
00e2e675
DG
349
350 /* Bail out if consumer is disabled */
351 if (!session->consumer->enabled) {
f73fabfd 352 ret = LTTNG_OK;
00e2e675
DG
353 goto error;
354 }
f1e16794
DG
355
356 DBG("Sending streams of channel %s to kernel consumer",
357 channel->channel->name);
358
2bba9e53 359 ret = kernel_consumer_add_channel(sock, channel, session, monitor);
f1e16794 360 if (ret < 0) {
f1e16794
DG
361 goto error;
362 }
363
364 /* Send streams */
365 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
6986ab9b 366 if (!stream->fd || stream->sent_to_consumer) {
f1e16794
DG
367 continue;
368 }
00e2e675
DG
369
370 /* Add stream on the kernel consumer side. */
2bba9e53
DG
371 ret = kernel_consumer_add_stream(sock, channel, stream, session,
372 monitor);
f1e16794 373 if (ret < 0) {
f1e16794
DG
374 goto error;
375 }
6986ab9b 376 stream->sent_to_consumer = true;
f1e16794
DG
377 }
378
f1e16794
DG
379error:
380 return ret;
381}
382
383/*
384 * Send all stream fds of the kernel session to the consumer.
385 */
f50f23d9
DG
386int kernel_consumer_send_session(struct consumer_socket *sock,
387 struct ltt_kernel_session *session)
f1e16794 388{
2bba9e53 389 int ret, monitor = 0;
f1e16794 390 struct ltt_kernel_channel *chan;
f1e16794 391
00e2e675
DG
392 /* Safety net */
393 assert(session);
394 assert(session->consumer);
f50f23d9 395 assert(sock);
f1e16794 396
00e2e675
DG
397 /* Bail out if consumer is disabled */
398 if (!session->consumer->enabled) {
f73fabfd 399 ret = LTTNG_OK;
00e2e675 400 goto error;
f1e16794
DG
401 }
402
2bba9e53
DG
403 /* Don't monitor the streams on the consumer if in flight recorder. */
404 if (session->output_traces) {
405 monitor = 1;
406 }
407
00e2e675
DG
408 DBG("Sending session stream to kernel consumer");
409
609af759 410 if (session->metadata_stream_fd >= 0 && session->metadata) {
2bba9e53 411 ret = kernel_consumer_add_metadata(sock, session, monitor);
f1e16794 412 if (ret < 0) {
f1e16794
DG
413 goto error;
414 }
f1e16794
DG
415 }
416
00e2e675 417 /* Send channel and streams of it */
f1e16794 418 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
2bba9e53
DG
419 ret = kernel_consumer_send_channel_stream(sock, chan, session,
420 monitor);
f1e16794
DG
421 if (ret < 0) {
422 goto error;
423 }
601262d6
JD
424 if (monitor) {
425 /*
426 * Inform the relay that all the streams for the
427 * channel were sent.
428 */
429 ret = kernel_consumer_streams_sent(sock, session, chan->fd);
430 if (ret < 0) {
431 goto error;
432 }
433 }
f1e16794
DG
434 }
435
00e2e675 436 DBG("Kernel consumer FDs of metadata and channel streams sent");
f1e16794 437
4ce9ff51 438 session->consumer_fds_sent = 1;
f1e16794
DG
439 return 0;
440
441error:
442 return ret;
443}
07b86b52
JD
444
445int kernel_consumer_destroy_channel(struct consumer_socket *socket,
446 struct ltt_kernel_channel *channel)
447{
448 int ret;
449 struct lttcomm_consumer_msg msg;
450
451 assert(channel);
452 assert(socket);
07b86b52
JD
453
454 DBG("Sending kernel consumer destroy channel key %d", channel->fd);
455
53efb85a 456 memset(&msg, 0, sizeof(msg));
07b86b52
JD
457 msg.cmd_type = LTTNG_CONSUMER_DESTROY_CHANNEL;
458 msg.u.destroy_channel.key = channel->fd;
459
460 pthread_mutex_lock(socket->lock);
461 health_code_update();
462
463 ret = consumer_send_msg(socket, &msg);
464 if (ret < 0) {
465 goto error;
466 }
467
468error:
469 health_code_update();
470 pthread_mutex_unlock(socket->lock);
471 return ret;
472}
473
474int kernel_consumer_destroy_metadata(struct consumer_socket *socket,
475 struct ltt_kernel_metadata *metadata)
476{
477 int ret;
478 struct lttcomm_consumer_msg msg;
479
480 assert(metadata);
481 assert(socket);
07b86b52
JD
482
483 DBG("Sending kernel consumer destroy channel key %d", metadata->fd);
484
53efb85a 485 memset(&msg, 0, sizeof(msg));
07b86b52
JD
486 msg.cmd_type = LTTNG_CONSUMER_DESTROY_CHANNEL;
487 msg.u.destroy_channel.key = metadata->fd;
488
489 pthread_mutex_lock(socket->lock);
490 health_code_update();
491
492 ret = consumer_send_msg(socket, &msg);
493 if (ret < 0) {
494 goto error;
495 }
496
497error:
498 health_code_update();
499 pthread_mutex_unlock(socket->lock);
500 return ret;
501}
This page took 0.06543 seconds and 4 git commands to generate.