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