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