Fix: Send remove channel to notification thread only when necessary
[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 }
753873bf
JR
171
172 channel->published_to_notification_thread = true;
173
00e2e675 174error:
53efb85a 175 free(pathname);
00e2e675
DG
176 return ret;
177}
178
179/*
180 * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
181 */
f50f23d9 182int kernel_consumer_add_metadata(struct consumer_socket *sock,
2bba9e53 183 struct ltt_kernel_session *session, unsigned int monitor)
00e2e675
DG
184{
185 int ret;
2bba9e53 186 char *pathname;
00e2e675 187 struct lttcomm_consumer_msg lkm;
a7d9a3e7 188 struct consumer_output *consumer;
00e2e675
DG
189
190 /* Safety net */
191 assert(session);
192 assert(session->consumer);
f50f23d9 193 assert(sock);
00e2e675
DG
194
195 DBG("Sending metadata %d to kernel consumer", session->metadata_stream_fd);
196
197 /* Get consumer output pointer */
a7d9a3e7 198 consumer = session->consumer;
00e2e675 199
2bba9e53
DG
200 if (monitor) {
201 pathname = create_channel_path(consumer, session->uid, session->gid);
00e2e675 202 } else {
2bba9e53 203 /* Empty path. */
53efb85a 204 pathname = strdup("");
00e2e675 205 }
bb3c4e70
MD
206 if (!pathname) {
207 ret = -1;
208 goto error;
209 }
00e2e675
DG
210
211 /* Prep channel message structure */
212 consumer_init_channel_comm_msg(&lkm,
213 LTTNG_CONSUMER_ADD_CHANNEL,
214 session->metadata->fd,
ffe60014
DG
215 session->id,
216 pathname,
217 session->uid,
218 session->gid,
219 consumer->net_seq_index,
30079b6b 220 DEFAULT_METADATA_NAME,
ffe60014
DG
221 1,
222 DEFAULT_KERNEL_CHANNEL_OUTPUT,
1624d5b7 223 CONSUMER_CHANNEL_TYPE_METADATA,
2bba9e53 224 0, 0,
e9404c27 225 monitor, 0, 0);
00e2e675 226
840cb59c 227 health_code_update();
ca03de58 228
00e2e675
DG
229 ret = consumer_send_channel(sock, &lkm);
230 if (ret < 0) {
231 goto error;
232 }
233
840cb59c 234 health_code_update();
ca03de58 235
00e2e675
DG
236 /* Prep stream message structure */
237 consumer_init_stream_comm_msg(&lkm,
238 LTTNG_CONSUMER_ADD_STREAM,
239 session->metadata->fd,
240 session->metadata_stream_fd,
1624d5b7 241 0); /* CPU: 0 for metadata. */
00e2e675 242
840cb59c 243 health_code_update();
ca03de58 244
00e2e675 245 /* Send stream and file descriptor */
a7d9a3e7 246 ret = consumer_send_stream(sock, consumer, &lkm,
00e2e675
DG
247 &session->metadata_stream_fd, 1);
248 if (ret < 0) {
249 goto error;
250 }
251
840cb59c 252 health_code_update();
ca03de58 253
00e2e675 254error:
53efb85a 255 free(pathname);
00e2e675
DG
256 return ret;
257}
258
259/*
260 * Sending a single stream to the consumer with command ADD_STREAM.
261 */
f50f23d9
DG
262int kernel_consumer_add_stream(struct consumer_socket *sock,
263 struct ltt_kernel_channel *channel, struct ltt_kernel_stream *stream,
2bba9e53 264 struct ltt_kernel_session *session, unsigned int monitor)
00e2e675
DG
265{
266 int ret;
00e2e675 267 struct lttcomm_consumer_msg lkm;
a7d9a3e7 268 struct consumer_output *consumer;
00e2e675
DG
269
270 assert(channel);
271 assert(stream);
272 assert(session);
273 assert(session->consumer);
f50f23d9 274 assert(sock);
00e2e675
DG
275
276 DBG("Sending stream %d of channel %s to kernel consumer",
277 stream->fd, channel->channel->name);
278
279 /* Get consumer output pointer */
a7d9a3e7 280 consumer = session->consumer;
00e2e675 281
00e2e675 282 /* Prep stream consumer message */
ffe60014
DG
283 consumer_init_stream_comm_msg(&lkm,
284 LTTNG_CONSUMER_ADD_STREAM,
00e2e675
DG
285 channel->fd,
286 stream->fd,
ffe60014 287 stream->cpu);
00e2e675 288
840cb59c 289 health_code_update();
ca03de58 290
00e2e675 291 /* Send stream and file descriptor */
a7d9a3e7 292 ret = consumer_send_stream(sock, consumer, &lkm, &stream->fd, 1);
00e2e675
DG
293 if (ret < 0) {
294 goto error;
295 }
296
840cb59c 297 health_code_update();
ca03de58 298
00e2e675
DG
299error:
300 return ret;
301}
302
a4baae1b
JD
303/*
304 * Sending the notification that all streams were sent with STREAMS_SENT.
305 */
306int kernel_consumer_streams_sent(struct consumer_socket *sock,
307 struct ltt_kernel_session *session, uint64_t channel_key)
308{
309 int ret;
310 struct lttcomm_consumer_msg lkm;
311 struct consumer_output *consumer;
312
313 assert(sock);
314 assert(session);
315
316 DBG("Sending streams_sent");
317 /* Get consumer output pointer */
318 consumer = session->consumer;
319
320 /* Prep stream consumer message */
321 consumer_init_streams_sent_comm_msg(&lkm,
322 LTTNG_CONSUMER_STREAMS_SENT,
323 channel_key, consumer->net_seq_index);
324
325 health_code_update();
326
327 /* Send stream and file descriptor */
328 ret = consumer_send_msg(sock, &lkm);
329 if (ret < 0) {
330 goto error;
331 }
332
333error:
334 return ret;
335}
336
f1e16794
DG
337/*
338 * Send all stream fds of kernel channel to the consumer.
339 */
f50f23d9 340int kernel_consumer_send_channel_stream(struct consumer_socket *sock,
2bba9e53
DG
341 struct ltt_kernel_channel *channel, struct ltt_kernel_session *session,
342 unsigned int monitor)
f1e16794 343{
e99f9447 344 int ret = LTTNG_OK;
f1e16794 345 struct ltt_kernel_stream *stream;
00e2e675
DG
346
347 /* Safety net */
348 assert(channel);
349 assert(session);
350 assert(session->consumer);
f50f23d9 351 assert(sock);
00e2e675
DG
352
353 /* Bail out if consumer is disabled */
354 if (!session->consumer->enabled) {
f73fabfd 355 ret = LTTNG_OK;
00e2e675
DG
356 goto error;
357 }
f1e16794
DG
358
359 DBG("Sending streams of channel %s to kernel consumer",
360 channel->channel->name);
361
e99f9447
MD
362 if (!channel->sent_to_consumer) {
363 ret = kernel_consumer_add_channel(sock, channel, session, monitor);
364 if (ret < 0) {
365 goto error;
366 }
367 channel->sent_to_consumer = true;
f1e16794
DG
368 }
369
370 /* Send streams */
371 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
6986ab9b 372 if (!stream->fd || stream->sent_to_consumer) {
f1e16794
DG
373 continue;
374 }
00e2e675
DG
375
376 /* Add stream on the kernel consumer side. */
2bba9e53
DG
377 ret = kernel_consumer_add_stream(sock, channel, stream, session,
378 monitor);
f1e16794 379 if (ret < 0) {
f1e16794
DG
380 goto error;
381 }
6986ab9b 382 stream->sent_to_consumer = true;
f1e16794
DG
383 }
384
f1e16794
DG
385error:
386 return ret;
387}
388
389/*
390 * Send all stream fds of the kernel session to the consumer.
391 */
f50f23d9
DG
392int kernel_consumer_send_session(struct consumer_socket *sock,
393 struct ltt_kernel_session *session)
f1e16794 394{
2bba9e53 395 int ret, monitor = 0;
f1e16794 396 struct ltt_kernel_channel *chan;
f1e16794 397
00e2e675
DG
398 /* Safety net */
399 assert(session);
400 assert(session->consumer);
f50f23d9 401 assert(sock);
f1e16794 402
00e2e675
DG
403 /* Bail out if consumer is disabled */
404 if (!session->consumer->enabled) {
f73fabfd 405 ret = LTTNG_OK;
00e2e675 406 goto error;
f1e16794
DG
407 }
408
2bba9e53
DG
409 /* Don't monitor the streams on the consumer if in flight recorder. */
410 if (session->output_traces) {
411 monitor = 1;
412 }
413
00e2e675
DG
414 DBG("Sending session stream to kernel consumer");
415
609af759 416 if (session->metadata_stream_fd >= 0 && session->metadata) {
2bba9e53 417 ret = kernel_consumer_add_metadata(sock, session, monitor);
f1e16794 418 if (ret < 0) {
f1e16794
DG
419 goto error;
420 }
f1e16794
DG
421 }
422
00e2e675 423 /* Send channel and streams of it */
f1e16794 424 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
2bba9e53
DG
425 ret = kernel_consumer_send_channel_stream(sock, chan, session,
426 monitor);
f1e16794
DG
427 if (ret < 0) {
428 goto error;
429 }
601262d6
JD
430 if (monitor) {
431 /*
432 * Inform the relay that all the streams for the
433 * channel were sent.
434 */
435 ret = kernel_consumer_streams_sent(sock, session, chan->fd);
436 if (ret < 0) {
437 goto error;
438 }
439 }
f1e16794
DG
440 }
441
00e2e675 442 DBG("Kernel consumer FDs of metadata and channel streams sent");
f1e16794 443
4ce9ff51 444 session->consumer_fds_sent = 1;
f1e16794
DG
445 return 0;
446
447error:
448 return ret;
449}
07b86b52
JD
450
451int kernel_consumer_destroy_channel(struct consumer_socket *socket,
452 struct ltt_kernel_channel *channel)
453{
454 int ret;
455 struct lttcomm_consumer_msg msg;
456
457 assert(channel);
458 assert(socket);
07b86b52
JD
459
460 DBG("Sending kernel consumer destroy channel key %d", channel->fd);
461
53efb85a 462 memset(&msg, 0, sizeof(msg));
07b86b52
JD
463 msg.cmd_type = LTTNG_CONSUMER_DESTROY_CHANNEL;
464 msg.u.destroy_channel.key = channel->fd;
465
466 pthread_mutex_lock(socket->lock);
467 health_code_update();
468
469 ret = consumer_send_msg(socket, &msg);
470 if (ret < 0) {
471 goto error;
472 }
473
474error:
475 health_code_update();
476 pthread_mutex_unlock(socket->lock);
477 return ret;
478}
479
480int kernel_consumer_destroy_metadata(struct consumer_socket *socket,
481 struct ltt_kernel_metadata *metadata)
482{
483 int ret;
484 struct lttcomm_consumer_msg msg;
485
486 assert(metadata);
487 assert(socket);
07b86b52
JD
488
489 DBG("Sending kernel consumer destroy channel key %d", metadata->fd);
490
53efb85a 491 memset(&msg, 0, sizeof(msg));
07b86b52
JD
492 msg.cmd_type = LTTNG_CONSUMER_DESTROY_CHANNEL;
493 msg.u.destroy_channel.key = metadata->fd;
494
495 pthread_mutex_lock(socket->lock);
496 health_code_update();
497
498 ret = consumer_send_msg(socket, &msg);
499 if (ret < 0) {
500 goto error;
501 }
502
503error:
504 health_code_update();
505 pthread_mutex_unlock(socket->lock);
506 return ret;
507}
This page took 0.065109 seconds and 4 git commands to generate.