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