sessiond: enforce mmap output type for kernel metadata channel
[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>
e1f3997a 23#include <inttypes.h>
f1e16794
DG
24
25#include <common/common.h>
26#include <common/defaults.h>
f5436bfc 27#include <common/compat/string.h>
f1e16794 28
00e2e675 29#include "consumer.h"
8782cc74 30#include "health-sessiond.h"
f1e16794 31#include "kernel-consumer.h"
e9404c27
JG
32#include "notification-thread-commands.h"
33#include "session.h"
34#include "lttng-sessiond.h"
f1e16794 35
e5148e25 36static char *create_channel_path(struct consumer_output *consumer)
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 44 /* Get the right path name destination */
9c96bc23
JG
45 if (consumer->type == CONSUMER_DST_LOCAL ||
46 (consumer->type == CONSUMER_DST_NET &&
47 consumer->relay_major_version == 2 &&
48 consumer->relay_minor_version >= 11)) {
e5148e25 49 pathname = strdup(consumer->domain_subdir);
bb3c4e70 50 if (!pathname) {
e5148e25
JG
51 PERROR("Failed to copy domain subdirectory string %s",
52 consumer->domain_subdir);
bb3c4e70
MD
53 goto error;
54 }
e5148e25
JG
55 DBG3("Kernel local consumer trace path relative to current trace chunk: \"%s\"",
56 pathname);
ffe60014 57 } else {
5feee503 58 /* Network output. */
2b29c638
JD
59 ret = snprintf(tmp_path, sizeof(tmp_path), "%s%s",
60 consumer->dst.net.base_dir,
5feee503 61 consumer->domain_subdir);
ffe60014 62 if (ret < 0) {
2bba9e53 63 PERROR("snprintf kernel metadata path");
ffe60014 64 goto error;
dba13f1d
JG
65 } else if (ret >= sizeof(tmp_path)) {
66 ERR("Kernel channel path exceeds the maximal allowed length of of %zu bytes (%i bytes required) with path \"%s%s\"",
67 sizeof(tmp_path), ret,
68 consumer->dst.net.base_dir,
5feee503 69 consumer->domain_subdir);
dba13f1d 70 goto error;
ffe60014 71 }
f5436bfc 72 pathname = lttng_strndup(tmp_path, sizeof(tmp_path));
bb3c4e70 73 if (!pathname) {
f5436bfc 74 PERROR("lttng_strndup");
bb3c4e70
MD
75 goto error;
76 }
ffe60014
DG
77 DBG3("Kernel network consumer subdir path: %s", pathname);
78 }
79
2bba9e53
DG
80 return pathname;
81
82error:
83 free(pathname);
84 return NULL;
85}
86
87/*
88 * Sending a single channel to the consumer with command ADD_CHANNEL.
89 */
606b643e 90static
2bba9e53 91int kernel_consumer_add_channel(struct consumer_socket *sock,
e9404c27
JG
92 struct ltt_kernel_channel *channel,
93 struct ltt_kernel_session *ksession,
2bba9e53
DG
94 unsigned int monitor)
95{
96 int ret;
e5148e25 97 char *pathname = NULL;
2bba9e53
DG
98 struct lttcomm_consumer_msg lkm;
99 struct consumer_output *consumer;
e9404c27 100 enum lttng_error_code status;
48a86f68 101 struct ltt_session *session = NULL;
e9404c27 102 struct lttng_channel_extended *channel_attr_extended;
e5148e25 103 bool is_local_trace;
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);
e5148e25 116 is_local_trace = consumer->net_seq_index == -1ULL;
2bba9e53 117
e5148e25 118 pathname = create_channel_path(consumer);
bb3c4e70
MD
119 if (!pathname) {
120 ret = -1;
121 goto error;
122 }
2bba9e53 123
e5148e25
JG
124 if (is_local_trace && ksession->current_trace_chunk) {
125 enum lttng_trace_chunk_status chunk_status;
126 char *pathname_index;
127
128 ret = asprintf(&pathname_index, "%s/" DEFAULT_INDEX_DIR,
129 pathname);
130 if (ret < 0) {
131 ERR("Failed to format channel index directory");
132 ret = -1;
133 goto error;
134 }
135
136 /*
137 * Create the index subdirectory which will take care
138 * of implicitly creating the channel's path.
139 */
140 chunk_status = lttng_trace_chunk_create_subdirectory(
141 ksession->current_trace_chunk, pathname_index);
142 free(pathname_index);
143 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
144 ret = -1;
145 goto error;
146 }
147 }
148
00e2e675 149 /* Prep channel message structure */
638e7b4e 150 consumer_init_add_channel_comm_msg(&lkm,
e1f3997a 151 channel->key,
e9404c27 152 ksession->id,
ffe60014 153 pathname,
e9404c27
JG
154 ksession->uid,
155 ksession->gid,
ffe60014 156 consumer->net_seq_index,
c30aaa51 157 channel->channel->name,
ffe60014
DG
158 channel->stream_count,
159 channel->channel->attr.output,
1624d5b7
JD
160 CONSUMER_CHANNEL_TYPE_DATA,
161 channel->channel->attr.tracefile_size,
2bba9e53 162 channel->channel->attr.tracefile_count,
ecc48a90 163 monitor,
e9404c27 164 channel->channel->attr.live_timer_interval,
3ef395a9 165 ksession->is_live_session,
e5148e25
JG
166 channel_attr_extended->monitor_timer_interval,
167 ksession->current_trace_chunk);
00e2e675 168
840cb59c 169 health_code_update();
ca03de58 170
00e2e675
DG
171 ret = consumer_send_channel(sock, &lkm);
172 if (ret < 0) {
173 goto error;
174 }
175
840cb59c 176 health_code_update();
e9404c27
JG
177 rcu_read_lock();
178 session = session_find_by_id(ksession->id);
179 assert(session);
e098433c
JG
180 assert(pthread_mutex_trylock(&session->lock));
181 assert(session_trylock_list());
ca03de58 182
e9404c27
JG
183 status = notification_thread_command_add_channel(
184 notification_thread_handle, session->name,
185 ksession->uid, ksession->gid,
e1f3997a 186 channel->channel->name, channel->key,
e9404c27
JG
187 LTTNG_DOMAIN_KERNEL,
188 channel->channel->attr.subbuf_size * channel->channel->attr.num_subbuf);
189 rcu_read_unlock();
190 if (status != LTTNG_OK) {
191 ret = -1;
192 goto error;
193 }
753873bf
JR
194
195 channel->published_to_notification_thread = true;
196
00e2e675 197error:
48a86f68
JG
198 if (session) {
199 session_put(session);
200 }
53efb85a 201 free(pathname);
00e2e675
DG
202 return ret;
203}
204
205/*
206 * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
9a318688
JG
207 *
208 * The consumer socket lock must be held by the caller.
00e2e675 209 */
f50f23d9 210int kernel_consumer_add_metadata(struct consumer_socket *sock,
e098433c 211 struct ltt_kernel_session *ksession, unsigned int monitor)
00e2e675
DG
212{
213 int ret;
00e2e675 214 struct lttcomm_consumer_msg lkm;
a7d9a3e7 215 struct consumer_output *consumer;
e098433c
JG
216
217 rcu_read_lock();
00e2e675
DG
218
219 /* Safety net */
e098433c
JG
220 assert(ksession);
221 assert(ksession->consumer);
f50f23d9 222 assert(sock);
00e2e675 223
e098433c
JG
224 DBG("Sending metadata %d to kernel consumer",
225 ksession->metadata_stream_fd);
00e2e675
DG
226
227 /* Get consumer output pointer */
e098433c 228 consumer = ksession->consumer;
00e2e675 229
00e2e675 230 /* Prep channel message structure */
17136d8c
JG
231 consumer_init_add_channel_comm_msg(&lkm,
232 ksession->metadata->key,
233 ksession->id,
234 "",
235 ksession->uid,
236 ksession->gid,
237 consumer->net_seq_index,
238 ksession->metadata->conf->name,
239 1,
240 ksession->metadata->conf->attr.output,
241 CONSUMER_CHANNEL_TYPE_METADATA,
242 ksession->metadata->conf->attr.tracefile_size,
243 ksession->metadata->conf->attr.tracefile_count,
244 monitor,
245 ksession->metadata->conf->attr.live_timer_interval,
246 ksession->is_live_session,
247 0,
3ef395a9 248 ksession->current_trace_chunk);
00e2e675 249
840cb59c 250 health_code_update();
ca03de58 251
00e2e675
DG
252 ret = consumer_send_channel(sock, &lkm);
253 if (ret < 0) {
254 goto error;
255 }
256
840cb59c 257 health_code_update();
ca03de58 258
00e2e675 259 /* Prep stream message structure */
e098433c
JG
260 consumer_init_add_stream_comm_msg(&lkm,
261 ksession->metadata->key,
262 ksession->metadata_stream_fd,
e5148e25 263 0 /* CPU: 0 for metadata. */);
00e2e675 264
840cb59c 265 health_code_update();
ca03de58 266
00e2e675 267 /* Send stream and file descriptor */
a7d9a3e7 268 ret = consumer_send_stream(sock, consumer, &lkm,
e098433c 269 &ksession->metadata_stream_fd, 1);
00e2e675
DG
270 if (ret < 0) {
271 goto error;
272 }
273
840cb59c 274 health_code_update();
ca03de58 275
00e2e675 276error:
e098433c 277 rcu_read_unlock();
00e2e675
DG
278 return ret;
279}
280
281/*
282 * Sending a single stream to the consumer with command ADD_STREAM.
283 */
5b0e3ccb 284static
f50f23d9 285int kernel_consumer_add_stream(struct consumer_socket *sock,
e098433c
JG
286 struct ltt_kernel_channel *channel,
287 struct ltt_kernel_stream *stream,
e5148e25 288 struct ltt_kernel_session *session, unsigned int monitor)
00e2e675
DG
289{
290 int ret;
00e2e675 291 struct lttcomm_consumer_msg lkm;
a7d9a3e7 292 struct consumer_output *consumer;
00e2e675
DG
293
294 assert(channel);
295 assert(stream);
296 assert(session);
297 assert(session->consumer);
f50f23d9 298 assert(sock);
00e2e675
DG
299
300 DBG("Sending stream %d of channel %s to kernel consumer",
301 stream->fd, channel->channel->name);
302
303 /* Get consumer output pointer */
a7d9a3e7 304 consumer = session->consumer;
00e2e675 305
00e2e675 306 /* Prep stream consumer message */
e098433c 307 consumer_init_add_stream_comm_msg(&lkm,
e1f3997a 308 channel->key,
00e2e675 309 stream->fd,
e5148e25 310 stream->cpu);
00e2e675 311
840cb59c 312 health_code_update();
ca03de58 313
00e2e675 314 /* Send stream and file descriptor */
a7d9a3e7 315 ret = consumer_send_stream(sock, consumer, &lkm, &stream->fd, 1);
00e2e675
DG
316 if (ret < 0) {
317 goto error;
318 }
319
840cb59c 320 health_code_update();
ca03de58 321
00e2e675
DG
322error:
323 return ret;
324}
325
a4baae1b
JD
326/*
327 * Sending the notification that all streams were sent with STREAMS_SENT.
328 */
329int kernel_consumer_streams_sent(struct consumer_socket *sock,
330 struct ltt_kernel_session *session, uint64_t channel_key)
331{
332 int ret;
333 struct lttcomm_consumer_msg lkm;
334 struct consumer_output *consumer;
335
336 assert(sock);
337 assert(session);
338
339 DBG("Sending streams_sent");
340 /* Get consumer output pointer */
341 consumer = session->consumer;
342
343 /* Prep stream consumer message */
344 consumer_init_streams_sent_comm_msg(&lkm,
345 LTTNG_CONSUMER_STREAMS_SENT,
346 channel_key, consumer->net_seq_index);
347
348 health_code_update();
349
350 /* Send stream and file descriptor */
351 ret = consumer_send_msg(sock, &lkm);
352 if (ret < 0) {
353 goto error;
354 }
355
356error:
357 return ret;
358}
359
f1e16794
DG
360/*
361 * Send all stream fds of kernel channel to the consumer.
9a318688
JG
362 *
363 * The consumer socket lock must be held by the caller.
f1e16794 364 */
1fc1b7c8 365int kernel_consumer_send_channel_streams(struct consumer_socket *sock,
e098433c 366 struct ltt_kernel_channel *channel, struct ltt_kernel_session *ksession,
2bba9e53 367 unsigned int monitor)
f1e16794 368{
e99f9447 369 int ret = LTTNG_OK;
f1e16794 370 struct ltt_kernel_stream *stream;
00e2e675
DG
371
372 /* Safety net */
373 assert(channel);
e098433c
JG
374 assert(ksession);
375 assert(ksession->consumer);
f50f23d9 376 assert(sock);
00e2e675 377
e098433c
JG
378 rcu_read_lock();
379
00e2e675 380 /* Bail out if consumer is disabled */
e098433c 381 if (!ksession->consumer->enabled) {
f73fabfd 382 ret = LTTNG_OK;
00e2e675
DG
383 goto error;
384 }
f1e16794
DG
385
386 DBG("Sending streams of channel %s to kernel consumer",
387 channel->channel->name);
388
e99f9447 389 if (!channel->sent_to_consumer) {
e098433c 390 ret = kernel_consumer_add_channel(sock, channel, ksession, monitor);
e99f9447
MD
391 if (ret < 0) {
392 goto error;
393 }
394 channel->sent_to_consumer = true;
f1e16794
DG
395 }
396
397 /* Send streams */
398 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
6986ab9b 399 if (!stream->fd || stream->sent_to_consumer) {
f1e16794
DG
400 continue;
401 }
00e2e675
DG
402
403 /* Add stream on the kernel consumer side. */
e098433c 404 ret = kernel_consumer_add_stream(sock, channel, stream,
e5148e25 405 ksession, monitor);
f1e16794 406 if (ret < 0) {
f1e16794
DG
407 goto error;
408 }
6986ab9b 409 stream->sent_to_consumer = true;
f1e16794
DG
410 }
411
f1e16794 412error:
e098433c 413 rcu_read_unlock();
f1e16794
DG
414 return ret;
415}
416
417/*
418 * Send all stream fds of the kernel session to the consumer.
9a318688
JG
419 *
420 * The consumer socket lock must be held by the caller.
f1e16794 421 */
f50f23d9
DG
422int kernel_consumer_send_session(struct consumer_socket *sock,
423 struct ltt_kernel_session *session)
f1e16794 424{
2bba9e53 425 int ret, monitor = 0;
f1e16794 426 struct ltt_kernel_channel *chan;
f1e16794 427
00e2e675
DG
428 /* Safety net */
429 assert(session);
430 assert(session->consumer);
f50f23d9 431 assert(sock);
f1e16794 432
00e2e675
DG
433 /* Bail out if consumer is disabled */
434 if (!session->consumer->enabled) {
f73fabfd 435 ret = LTTNG_OK;
00e2e675 436 goto error;
f1e16794
DG
437 }
438
2bba9e53
DG
439 /* Don't monitor the streams on the consumer if in flight recorder. */
440 if (session->output_traces) {
441 monitor = 1;
442 }
443
00e2e675
DG
444 DBG("Sending session stream to kernel consumer");
445
609af759 446 if (session->metadata_stream_fd >= 0 && session->metadata) {
2bba9e53 447 ret = kernel_consumer_add_metadata(sock, session, monitor);
f1e16794 448 if (ret < 0) {
f1e16794
DG
449 goto error;
450 }
f1e16794
DG
451 }
452
00e2e675 453 /* Send channel and streams of it */
f1e16794 454 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
1fc1b7c8 455 ret = kernel_consumer_send_channel_streams(sock, chan, session,
2bba9e53 456 monitor);
f1e16794
DG
457 if (ret < 0) {
458 goto error;
459 }
601262d6
JD
460 if (monitor) {
461 /*
462 * Inform the relay that all the streams for the
463 * channel were sent.
464 */
e1f3997a 465 ret = kernel_consumer_streams_sent(sock, session, chan->key);
601262d6
JD
466 if (ret < 0) {
467 goto error;
468 }
469 }
f1e16794
DG
470 }
471
00e2e675 472 DBG("Kernel consumer FDs of metadata and channel streams sent");
f1e16794 473
4ce9ff51 474 session->consumer_fds_sent = 1;
f1e16794
DG
475 return 0;
476
477error:
478 return ret;
479}
07b86b52
JD
480
481int kernel_consumer_destroy_channel(struct consumer_socket *socket,
482 struct ltt_kernel_channel *channel)
483{
484 int ret;
485 struct lttcomm_consumer_msg msg;
486
487 assert(channel);
488 assert(socket);
07b86b52 489
e1f3997a 490 DBG("Sending kernel consumer destroy channel key %" PRIu64, channel->key);
07b86b52 491
53efb85a 492 memset(&msg, 0, sizeof(msg));
07b86b52 493 msg.cmd_type = LTTNG_CONSUMER_DESTROY_CHANNEL;
e1f3997a 494 msg.u.destroy_channel.key = channel->key;
07b86b52
JD
495
496 pthread_mutex_lock(socket->lock);
497 health_code_update();
498
499 ret = consumer_send_msg(socket, &msg);
500 if (ret < 0) {
501 goto error;
502 }
503
504error:
505 health_code_update();
506 pthread_mutex_unlock(socket->lock);
507 return ret;
508}
509
510int kernel_consumer_destroy_metadata(struct consumer_socket *socket,
511 struct ltt_kernel_metadata *metadata)
512{
513 int ret;
514 struct lttcomm_consumer_msg msg;
515
516 assert(metadata);
517 assert(socket);
07b86b52 518
d40f0359 519 DBG("Sending kernel consumer destroy channel key %" PRIu64, metadata->key);
07b86b52 520
53efb85a 521 memset(&msg, 0, sizeof(msg));
07b86b52 522 msg.cmd_type = LTTNG_CONSUMER_DESTROY_CHANNEL;
d40f0359 523 msg.u.destroy_channel.key = metadata->key;
07b86b52
JD
524
525 pthread_mutex_lock(socket->lock);
526 health_code_update();
527
528 ret = consumer_send_msg(socket, &msg);
529 if (ret < 0) {
530 goto error;
531 }
532
533error:
534 health_code_update();
535 pthread_mutex_unlock(socket->lock);
536 return ret;
537}
This page took 0.077313 seconds and 4 git commands to generate.