Fix: reintroduce lazy kernel modules load, fix empty syscall list
[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
d2956687 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 */
a5ba6fdd
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)) {
d2956687 49 pathname = strdup(consumer->domain_subdir);
bb3c4e70 50 if (!pathname) {
d2956687
JG
51 PERROR("Failed to copy domain subdirectory string %s",
52 consumer->domain_subdir);
bb3c4e70
MD
53 goto error;
54 }
d2956687
JG
55 DBG3("Kernel local consumer trace path relative to current trace chunk: \"%s\"",
56 pathname);
ffe60014 57 } else {
b178f53e 58 /* Network output. */
2b29c638
JD
59 ret = snprintf(tmp_path, sizeof(tmp_path), "%s%s",
60 consumer->dst.net.base_dir,
b178f53e 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,
b178f53e 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 */
ba27cd00 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;
d2956687 97 char *pathname = NULL;
2bba9e53
DG
98 struct lttcomm_consumer_msg lkm;
99 struct consumer_output *consumer;
e9404c27 100 enum lttng_error_code status;
e32d7f27 101 struct ltt_session *session = NULL;
e9404c27 102 struct lttng_channel_extended *channel_attr_extended;
d2956687 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);
d2956687 116 is_local_trace = consumer->net_seq_index == -1ULL;
2bba9e53 117
d2956687 118 pathname = create_channel_path(consumer);
bb3c4e70
MD
119 if (!pathname) {
120 ret = -1;
121 goto error;
122 }
2bba9e53 123
d2956687
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,
d2956687
JG
165 channel_attr_extended->monitor_timer_interval,
166 ksession->current_trace_chunk);
00e2e675 167
840cb59c 168 health_code_update();
ca03de58 169
00e2e675
DG
170 ret = consumer_send_channel(sock, &lkm);
171 if (ret < 0) {
172 goto error;
173 }
174
840cb59c 175 health_code_update();
e9404c27
JG
176 rcu_read_lock();
177 session = session_find_by_id(ksession->id);
178 assert(session);
e098433c
JG
179 assert(pthread_mutex_trylock(&session->lock));
180 assert(session_trylock_list());
ca03de58 181
e9404c27
JG
182 status = notification_thread_command_add_channel(
183 notification_thread_handle, session->name,
184 ksession->uid, ksession->gid,
e1f3997a 185 channel->channel->name, channel->key,
e9404c27
JG
186 LTTNG_DOMAIN_KERNEL,
187 channel->channel->attr.subbuf_size * channel->channel->attr.num_subbuf);
188 rcu_read_unlock();
189 if (status != LTTNG_OK) {
190 ret = -1;
191 goto error;
192 }
753873bf
JR
193
194 channel->published_to_notification_thread = true;
195
00e2e675 196error:
e32d7f27
JG
197 if (session) {
198 session_put(session);
199 }
53efb85a 200 free(pathname);
00e2e675
DG
201 return ret;
202}
203
204/*
205 * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
9a318688
JG
206 *
207 * The consumer socket lock must be held by the caller.
00e2e675 208 */
f50f23d9 209int kernel_consumer_add_metadata(struct consumer_socket *sock,
e098433c 210 struct ltt_kernel_session *ksession, unsigned int monitor)
00e2e675
DG
211{
212 int ret;
00e2e675 213 struct lttcomm_consumer_msg lkm;
a7d9a3e7 214 struct consumer_output *consumer;
e098433c
JG
215
216 rcu_read_lock();
00e2e675
DG
217
218 /* Safety net */
e098433c
JG
219 assert(ksession);
220 assert(ksession->consumer);
f50f23d9 221 assert(sock);
00e2e675 222
e098433c
JG
223 DBG("Sending metadata %d to kernel consumer",
224 ksession->metadata_stream_fd);
00e2e675
DG
225
226 /* Get consumer output pointer */
e098433c 227 consumer = ksession->consumer;
00e2e675 228
00e2e675 229 /* Prep channel message structure */
638e7b4e 230 consumer_init_add_channel_comm_msg(&lkm,
e098433c
JG
231 ksession->metadata->key,
232 ksession->id,
d2956687 233 DEFAULT_KERNEL_TRACE_DIR,
e098433c
JG
234 ksession->uid,
235 ksession->gid,
ffe60014 236 consumer->net_seq_index,
30079b6b 237 DEFAULT_METADATA_NAME,
ffe60014
DG
238 1,
239 DEFAULT_KERNEL_CHANNEL_OUTPUT,
1624d5b7 240 CONSUMER_CHANNEL_TYPE_METADATA,
2bba9e53 241 0, 0,
d2956687 242 monitor, 0, 0, ksession->current_trace_chunk);
00e2e675 243
840cb59c 244 health_code_update();
ca03de58 245
00e2e675
DG
246 ret = consumer_send_channel(sock, &lkm);
247 if (ret < 0) {
248 goto error;
249 }
250
840cb59c 251 health_code_update();
ca03de58 252
00e2e675 253 /* Prep stream message structure */
e098433c
JG
254 consumer_init_add_stream_comm_msg(&lkm,
255 ksession->metadata->key,
256 ksession->metadata_stream_fd,
d2956687 257 0 /* CPU: 0 for metadata. */);
00e2e675 258
840cb59c 259 health_code_update();
ca03de58 260
00e2e675 261 /* Send stream and file descriptor */
a7d9a3e7 262 ret = consumer_send_stream(sock, consumer, &lkm,
e098433c 263 &ksession->metadata_stream_fd, 1);
00e2e675
DG
264 if (ret < 0) {
265 goto error;
266 }
267
840cb59c 268 health_code_update();
ca03de58 269
00e2e675 270error:
e098433c 271 rcu_read_unlock();
00e2e675
DG
272 return ret;
273}
274
275/*
276 * Sending a single stream to the consumer with command ADD_STREAM.
277 */
5b0e3ccb 278static
f50f23d9 279int kernel_consumer_add_stream(struct consumer_socket *sock,
e098433c
JG
280 struct ltt_kernel_channel *channel,
281 struct ltt_kernel_stream *stream,
d2956687 282 struct ltt_kernel_session *session, unsigned int monitor)
00e2e675
DG
283{
284 int ret;
00e2e675 285 struct lttcomm_consumer_msg lkm;
a7d9a3e7 286 struct consumer_output *consumer;
00e2e675
DG
287
288 assert(channel);
289 assert(stream);
290 assert(session);
291 assert(session->consumer);
f50f23d9 292 assert(sock);
00e2e675
DG
293
294 DBG("Sending stream %d of channel %s to kernel consumer",
295 stream->fd, channel->channel->name);
296
297 /* Get consumer output pointer */
a7d9a3e7 298 consumer = session->consumer;
00e2e675 299
00e2e675 300 /* Prep stream consumer message */
e098433c 301 consumer_init_add_stream_comm_msg(&lkm,
e1f3997a 302 channel->key,
00e2e675 303 stream->fd,
d2956687 304 stream->cpu);
00e2e675 305
840cb59c 306 health_code_update();
ca03de58 307
00e2e675 308 /* Send stream and file descriptor */
a7d9a3e7 309 ret = consumer_send_stream(sock, consumer, &lkm, &stream->fd, 1);
00e2e675
DG
310 if (ret < 0) {
311 goto error;
312 }
313
840cb59c 314 health_code_update();
ca03de58 315
00e2e675
DG
316error:
317 return ret;
318}
319
a4baae1b
JD
320/*
321 * Sending the notification that all streams were sent with STREAMS_SENT.
322 */
323int kernel_consumer_streams_sent(struct consumer_socket *sock,
324 struct ltt_kernel_session *session, uint64_t channel_key)
325{
326 int ret;
327 struct lttcomm_consumer_msg lkm;
328 struct consumer_output *consumer;
329
330 assert(sock);
331 assert(session);
332
333 DBG("Sending streams_sent");
334 /* Get consumer output pointer */
335 consumer = session->consumer;
336
337 /* Prep stream consumer message */
338 consumer_init_streams_sent_comm_msg(&lkm,
339 LTTNG_CONSUMER_STREAMS_SENT,
340 channel_key, consumer->net_seq_index);
341
342 health_code_update();
343
344 /* Send stream and file descriptor */
345 ret = consumer_send_msg(sock, &lkm);
346 if (ret < 0) {
347 goto error;
348 }
349
350error:
351 return ret;
352}
353
f1e16794
DG
354/*
355 * Send all stream fds of kernel channel to the consumer.
9a318688
JG
356 *
357 * The consumer socket lock must be held by the caller.
f1e16794 358 */
1fc1b7c8 359int kernel_consumer_send_channel_streams(struct consumer_socket *sock,
e098433c 360 struct ltt_kernel_channel *channel, struct ltt_kernel_session *ksession,
2bba9e53 361 unsigned int monitor)
f1e16794 362{
e99f9447 363 int ret = LTTNG_OK;
f1e16794 364 struct ltt_kernel_stream *stream;
00e2e675
DG
365
366 /* Safety net */
367 assert(channel);
e098433c
JG
368 assert(ksession);
369 assert(ksession->consumer);
f50f23d9 370 assert(sock);
00e2e675 371
e098433c
JG
372 rcu_read_lock();
373
00e2e675 374 /* Bail out if consumer is disabled */
e098433c 375 if (!ksession->consumer->enabled) {
f73fabfd 376 ret = LTTNG_OK;
00e2e675
DG
377 goto error;
378 }
f1e16794
DG
379
380 DBG("Sending streams of channel %s to kernel consumer",
381 channel->channel->name);
382
e99f9447 383 if (!channel->sent_to_consumer) {
e098433c 384 ret = kernel_consumer_add_channel(sock, channel, ksession, monitor);
e99f9447
MD
385 if (ret < 0) {
386 goto error;
387 }
388 channel->sent_to_consumer = true;
f1e16794
DG
389 }
390
391 /* Send streams */
392 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
6986ab9b 393 if (!stream->fd || stream->sent_to_consumer) {
f1e16794
DG
394 continue;
395 }
00e2e675
DG
396
397 /* Add stream on the kernel consumer side. */
e098433c 398 ret = kernel_consumer_add_stream(sock, channel, stream,
d2956687 399 ksession, monitor);
f1e16794 400 if (ret < 0) {
f1e16794
DG
401 goto error;
402 }
6986ab9b 403 stream->sent_to_consumer = true;
f1e16794
DG
404 }
405
f1e16794 406error:
e098433c 407 rcu_read_unlock();
f1e16794
DG
408 return ret;
409}
410
411/*
412 * Send all stream fds of the kernel session to the consumer.
9a318688
JG
413 *
414 * The consumer socket lock must be held by the caller.
f1e16794 415 */
f50f23d9
DG
416int kernel_consumer_send_session(struct consumer_socket *sock,
417 struct ltt_kernel_session *session)
f1e16794 418{
2bba9e53 419 int ret, monitor = 0;
f1e16794 420 struct ltt_kernel_channel *chan;
f1e16794 421
00e2e675
DG
422 /* Safety net */
423 assert(session);
424 assert(session->consumer);
f50f23d9 425 assert(sock);
f1e16794 426
00e2e675
DG
427 /* Bail out if consumer is disabled */
428 if (!session->consumer->enabled) {
f73fabfd 429 ret = LTTNG_OK;
00e2e675 430 goto error;
f1e16794
DG
431 }
432
2bba9e53
DG
433 /* Don't monitor the streams on the consumer if in flight recorder. */
434 if (session->output_traces) {
435 monitor = 1;
436 }
437
00e2e675
DG
438 DBG("Sending session stream to kernel consumer");
439
609af759 440 if (session->metadata_stream_fd >= 0 && session->metadata) {
2bba9e53 441 ret = kernel_consumer_add_metadata(sock, session, monitor);
f1e16794 442 if (ret < 0) {
f1e16794
DG
443 goto error;
444 }
f1e16794
DG
445 }
446
00e2e675 447 /* Send channel and streams of it */
f1e16794 448 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
1fc1b7c8 449 ret = kernel_consumer_send_channel_streams(sock, chan, session,
2bba9e53 450 monitor);
f1e16794
DG
451 if (ret < 0) {
452 goto error;
453 }
601262d6
JD
454 if (monitor) {
455 /*
456 * Inform the relay that all the streams for the
457 * channel were sent.
458 */
e1f3997a 459 ret = kernel_consumer_streams_sent(sock, session, chan->key);
601262d6
JD
460 if (ret < 0) {
461 goto error;
462 }
463 }
f1e16794
DG
464 }
465
00e2e675 466 DBG("Kernel consumer FDs of metadata and channel streams sent");
f1e16794 467
4ce9ff51 468 session->consumer_fds_sent = 1;
f1e16794
DG
469 return 0;
470
471error:
472 return ret;
473}
07b86b52
JD
474
475int kernel_consumer_destroy_channel(struct consumer_socket *socket,
476 struct ltt_kernel_channel *channel)
477{
478 int ret;
479 struct lttcomm_consumer_msg msg;
480
481 assert(channel);
482 assert(socket);
07b86b52 483
e1f3997a 484 DBG("Sending kernel consumer destroy channel key %" PRIu64, channel->key);
07b86b52 485
53efb85a 486 memset(&msg, 0, sizeof(msg));
07b86b52 487 msg.cmd_type = LTTNG_CONSUMER_DESTROY_CHANNEL;
e1f3997a 488 msg.u.destroy_channel.key = channel->key;
07b86b52
JD
489
490 pthread_mutex_lock(socket->lock);
491 health_code_update();
492
493 ret = consumer_send_msg(socket, &msg);
494 if (ret < 0) {
495 goto error;
496 }
497
498error:
499 health_code_update();
500 pthread_mutex_unlock(socket->lock);
501 return ret;
502}
503
504int kernel_consumer_destroy_metadata(struct consumer_socket *socket,
505 struct ltt_kernel_metadata *metadata)
506{
507 int ret;
508 struct lttcomm_consumer_msg msg;
509
510 assert(metadata);
511 assert(socket);
07b86b52 512
d40f0359 513 DBG("Sending kernel consumer destroy channel key %" PRIu64, metadata->key);
07b86b52 514
53efb85a 515 memset(&msg, 0, sizeof(msg));
07b86b52 516 msg.cmd_type = LTTNG_CONSUMER_DESTROY_CHANNEL;
d40f0359 517 msg.u.destroy_channel.key = metadata->key;
07b86b52
JD
518
519 pthread_mutex_lock(socket->lock);
520 health_code_update();
521
522 ret = consumer_send_msg(socket, &msg);
523 if (ret < 0) {
524 goto error;
525 }
526
527error:
528 health_code_update();
529 pthread_mutex_unlock(socket->lock);
530 return ret;
531}
This page took 0.09368 seconds and 5 git commands to generate.