Add the relayd create session command
[lttng-tools.git] / src / common / kernel-consumer / kernel-consumer.c
CommitLineData
3bd1e081
MD
1/*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
d14d33bf
AM
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
3bd1e081
MD
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
d14d33bf
AM
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
3bd1e081
MD
17 */
18
19#define _GNU_SOURCE
20#include <assert.h>
3bd1e081
MD
21#include <poll.h>
22#include <pthread.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/mman.h>
26#include <sys/socket.h>
27#include <sys/types.h>
77c7c900 28#include <inttypes.h>
3bd1e081 29#include <unistd.h>
dbb5dfe6 30#include <sys/stat.h>
3bd1e081 31
990570ed 32#include <common/common.h>
10a8a223 33#include <common/kernel-ctl/kernel-ctl.h>
10a8a223 34#include <common/sessiond-comm/sessiond-comm.h>
00e2e675 35#include <common/sessiond-comm/relayd.h>
dbb5dfe6 36#include <common/compat/fcntl.h>
00e2e675 37#include <common/relayd/relayd.h>
0857097f 38
10a8a223 39#include "kernel-consumer.h"
3bd1e081
MD
40
41extern struct lttng_consumer_global_data consumer_data;
42extern int consumer_poll_timeout;
43extern volatile int consumer_quit;
44
3bd1e081
MD
45/*
46 * Take a snapshot for a specific fd
47 *
48 * Returns 0 on success, < 0 on error
49 */
50int lttng_kconsumer_take_snapshot(struct lttng_consumer_local_data *ctx,
51 struct lttng_consumer_stream *stream)
52{
53 int ret = 0;
54 int infd = stream->wait_fd;
55
56 ret = kernctl_snapshot(infd);
57 if (ret != 0) {
87dc6a9c 58 errno = -ret;
3bd1e081
MD
59 perror("Getting sub-buffer snapshot.");
60 }
61
62 return ret;
63}
64
65/*
66 * Get the produced position
67 *
68 * Returns 0 on success, < 0 on error
69 */
70int lttng_kconsumer_get_produced_snapshot(
71 struct lttng_consumer_local_data *ctx,
72 struct lttng_consumer_stream *stream,
73 unsigned long *pos)
74{
75 int ret;
76 int infd = stream->wait_fd;
77
78 ret = kernctl_snapshot_get_produced(infd, pos);
79 if (ret != 0) {
87dc6a9c 80 errno = -ret;
3bd1e081
MD
81 perror("kernctl_snapshot_get_produced");
82 }
83
84 return ret;
85}
86
87int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
88 int sock, struct pollfd *consumer_sockpoll)
89{
90 ssize_t ret;
f50f23d9 91 enum lttng_error_code ret_code = LTTNG_OK;
3bd1e081
MD
92 struct lttcomm_consumer_msg msg;
93
94 ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
95 if (ret != sizeof(msg)) {
f73fabfd 96 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
3bd1e081
MD
97 return ret;
98 }
99 if (msg.cmd_type == LTTNG_CONSUMER_STOP) {
f50f23d9
DG
100 /*
101 * Notify the session daemon that the command is completed.
102 *
103 * On transport layer error, the function call will print an error
104 * message so handling the returned code is a bit useless since we
105 * return an error code anyway.
106 */
107 (void) consumer_send_status_msg(sock, ret_code);
3bd1e081
MD
108 return -ENOENT;
109 }
110
b0b335c8
MD
111 /* relayd needs RCU read-side protection */
112 rcu_read_lock();
113
3bd1e081 114 switch (msg.cmd_type) {
00e2e675
DG
115 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
116 {
f50f23d9 117 /* Session daemon status message are handled in the following call. */
7735ef9e
DG
118 ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
119 msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll,
120 &msg.u.relayd_sock.sock);
00e2e675
DG
121 goto end_nosignal;
122 }
3bd1e081
MD
123 case LTTNG_CONSUMER_ADD_CHANNEL:
124 {
125 struct lttng_consumer_channel *new_channel;
126
f50f23d9
DG
127 /* First send a status message before receiving the fds. */
128 ret = consumer_send_status_msg(sock, ret_code);
129 if (ret < 0) {
130 /* Somehow, the session daemon is not responding anymore. */
131 goto end_nosignal;
132 }
133
3bd1e081
MD
134 DBG("consumer_add_channel %d", msg.u.channel.channel_key);
135 new_channel = consumer_allocate_channel(msg.u.channel.channel_key,
136 -1, -1,
137 msg.u.channel.mmap_len,
c30aaa51
MD
138 msg.u.channel.max_sb_size,
139 msg.u.channel.nb_init_streams);
3bd1e081 140 if (new_channel == NULL) {
f73fabfd 141 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
3bd1e081
MD
142 goto end_nosignal;
143 }
144 if (ctx->on_recv_channel != NULL) {
145 ret = ctx->on_recv_channel(new_channel);
146 if (ret == 0) {
147 consumer_add_channel(new_channel);
148 } else if (ret < 0) {
149 goto end_nosignal;
150 }
151 } else {
152 consumer_add_channel(new_channel);
153 }
154 goto end_nosignal;
155 }
156 case LTTNG_CONSUMER_ADD_STREAM:
157 {
50f8ae69 158 int fd, stream_pipe;
00e2e675
DG
159 struct consumer_relayd_sock_pair *relayd = NULL;
160 struct lttng_consumer_stream *new_stream;
c80048c6 161 int alloc_ret = 0;
3bd1e081 162
f50f23d9
DG
163 /* First send a status message before receiving the fds. */
164 ret = consumer_send_status_msg(sock, ret_code);
165 if (ret < 0) {
166 /* Somehow, the session daemon is not responding anymore. */
167 goto end_nosignal;
168 }
169
3bd1e081
MD
170 /* block */
171 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
3f8e211f 172 rcu_read_unlock();
3bd1e081
MD
173 return -EINTR;
174 }
00e2e675
DG
175
176 /* Get stream file descriptor from socket */
f2fc6720
MD
177 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
178 if (ret != sizeof(fd)) {
f73fabfd 179 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
3f8e211f 180 rcu_read_unlock();
3bd1e081
MD
181 return ret;
182 }
3bd1e081 183
f50f23d9
DG
184 /*
185 * Send status code to session daemon only if the recv works. If the
186 * above recv() failed, the session daemon is notified through the
187 * error socket and the teardown is eventually done.
188 */
189 ret = consumer_send_status_msg(sock, ret_code);
190 if (ret < 0) {
191 /* Somehow, the session daemon is not responding anymore. */
192 goto end_nosignal;
193 }
194
3bd1e081
MD
195 new_stream = consumer_allocate_stream(msg.u.stream.channel_key,
196 msg.u.stream.stream_key,
f2fc6720 197 fd, fd,
3bd1e081
MD
198 msg.u.stream.state,
199 msg.u.stream.mmap_len,
200 msg.u.stream.output,
6df2e2c9
MD
201 msg.u.stream.path_name,
202 msg.u.stream.uid,
00e2e675
DG
203 msg.u.stream.gid,
204 msg.u.stream.net_index,
c80048c6 205 msg.u.stream.metadata_flag,
53632229 206 msg.u.stream.session_id,
c80048c6 207 &alloc_ret);
3bd1e081 208 if (new_stream == NULL) {
c80048c6
MD
209 switch (alloc_ret) {
210 case -ENOMEM:
211 case -EINVAL:
212 default:
213 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
214 break;
215 case -ENOENT:
216 /*
217 * We could not find the channel. Can happen if cpu hotplug
218 * happens while tearing down.
219 */
220 DBG3("Could not find channel");
221 break;
222 }
3f8e211f 223 goto end_nosignal;
3bd1e081 224 }
00e2e675 225
fb3a43a9
DG
226 /*
227 * The buffer flush is done on the session daemon side for the kernel
228 * so no need for the stream "hangup_flush_done" variable to be
229 * tracked. This is important for a kernel stream since we don't rely
230 * on the flush state of the stream to read data. It's not the case for
231 * user space tracing.
232 */
233 new_stream->hangup_flush_done = 0;
234
00e2e675
DG
235 /* The stream is not metadata. Get relayd reference if exists. */
236 relayd = consumer_find_relayd(msg.u.stream.net_index);
237 if (relayd != NULL) {
238 /* Add stream on the relayd */
239 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
240 ret = relayd_add_stream(&relayd->control_sock,
241 msg.u.stream.name, msg.u.stream.path_name,
242 &new_stream->relayd_stream_id);
243 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
244 if (ret < 0) {
e316aad5 245 consumer_del_stream(new_stream, NULL);
3f8e211f 246 goto end_nosignal;
00e2e675
DG
247 }
248 } else if (msg.u.stream.net_index != -1) {
249 ERR("Network sequence index %d unknown. Not adding stream.",
250 msg.u.stream.net_index);
e316aad5 251 consumer_del_stream(new_stream, NULL);
3f8e211f 252 goto end_nosignal;
00e2e675
DG
253 }
254
633d0084
DG
255 if (ctx->on_recv_stream) {
256 ret = ctx->on_recv_stream(new_stream);
257 if (ret < 0) {
e316aad5 258 consumer_del_stream(new_stream, NULL);
633d0084 259 goto end_nosignal;
fb3a43a9 260 }
633d0084 261 }
fb3a43a9 262
50f8ae69 263 /* Get the right pipe where the stream will be sent. */
633d0084 264 if (new_stream->metadata_flag) {
50f8ae69 265 stream_pipe = ctx->consumer_metadata_pipe[1];
3bd1e081 266 } else {
50f8ae69
DG
267 stream_pipe = ctx->consumer_data_pipe[1];
268 }
269
270 do {
271 ret = write(stream_pipe, &new_stream, sizeof(new_stream));
272 } while (ret < 0 && errno == EINTR);
273 if (ret < 0) {
274 PERROR("Consumer write %s stream to pipe %d",
275 new_stream->metadata_flag ? "metadata" : "data",
276 stream_pipe);
277 consumer_del_stream(new_stream, NULL);
278 goto end_nosignal;
3bd1e081 279 }
00e2e675 280
50f8ae69
DG
281 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64,
282 msg.u.stream.path_name, fd, new_stream->relayd_stream_id);
3bd1e081
MD
283 break;
284 }
285 case LTTNG_CONSUMER_UPDATE_STREAM:
286 {
3f8e211f
DG
287 rcu_read_unlock();
288 return -ENOSYS;
289 }
290 case LTTNG_CONSUMER_DESTROY_RELAYD:
291 {
a6ba4fe1 292 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
3f8e211f
DG
293 struct consumer_relayd_sock_pair *relayd;
294
a6ba4fe1 295 DBG("Kernel consumer destroying relayd %" PRIu64, index);
3f8e211f
DG
296
297 /* Get relayd reference if exists. */
a6ba4fe1 298 relayd = consumer_find_relayd(index);
3f8e211f 299 if (relayd == NULL) {
a6ba4fe1 300 ERR("Unable to find relayd %" PRIu64, index);
f50f23d9 301 ret_code = LTTNG_ERR_NO_CONSUMER;
3bd1e081 302 }
3f8e211f 303
a6ba4fe1
DG
304 /*
305 * Each relayd socket pair has a refcount of stream attached to it
306 * which tells if the relayd is still active or not depending on the
307 * refcount value.
308 *
309 * This will set the destroy flag of the relayd object and destroy it
310 * if the refcount reaches zero when called.
311 *
312 * The destroy can happen either here or when a stream fd hangs up.
313 */
f50f23d9
DG
314 if (relayd) {
315 consumer_flag_relayd_for_destroy(relayd);
316 }
317
318 ret = consumer_send_status_msg(sock, ret_code);
319 if (ret < 0) {
320 /* Somehow, the session daemon is not responding anymore. */
321 goto end_nosignal;
322 }
3f8e211f 323
3f8e211f 324 goto end_nosignal;
3bd1e081 325 }
6d805429 326 case LTTNG_CONSUMER_DATA_PENDING:
53632229 327 {
c8f59ee5 328 int32_t ret;
6d805429 329 uint64_t id = msg.u.data_pending.session_id;
c8f59ee5 330
6d805429 331 DBG("Kernel consumer data pending command for id %" PRIu64, id);
c8f59ee5 332
6d805429 333 ret = consumer_data_pending(id);
c8f59ee5
DG
334
335 /* Send back returned value to session daemon */
336 ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret));
337 if (ret < 0) {
6d805429 338 PERROR("send data pending ret code");
c8f59ee5 339 }
f50f23d9
DG
340
341 /*
342 * No need to send back a status message since the data pending
343 * returned value is the response.
344 */
c8f59ee5 345 break;
53632229 346 }
3bd1e081 347 default:
3f8e211f 348 goto end_nosignal;
3bd1e081 349 }
3f8e211f 350
3bd1e081 351end_nosignal:
b0b335c8 352 rcu_read_unlock();
4cbc1a04
DG
353
354 /*
355 * Return 1 to indicate success since the 0 value can be a socket
356 * shutdown during the recv() or send() call.
357 */
358 return 1;
3bd1e081 359}
d41f73b7
MD
360
361/*
362 * Consume data on a file descriptor and write it on a trace file.
363 */
4078b776 364ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
d41f73b7
MD
365 struct lttng_consumer_local_data *ctx)
366{
1d4dfdef 367 unsigned long len, subbuf_size, padding;
d41f73b7 368 int err;
4078b776 369 ssize_t ret = 0;
d41f73b7
MD
370 int infd = stream->wait_fd;
371
372 DBG("In read_subbuffer (infd : %d)", infd);
373 /* Get the next subbuffer */
374 err = kernctl_get_next_subbuf(infd);
375 if (err != 0) {
1d4dfdef 376 ret = err;
d41f73b7
MD
377 /*
378 * This is a debug message even for single-threaded consumer,
379 * because poll() have more relaxed criterions than get subbuf,
380 * so get_subbuf may fail for short race windows where poll()
381 * would issue wakeups.
382 */
383 DBG("Reserving sub buffer failed (everything is normal, "
384 "it is due to concurrency)");
385 goto end;
386 }
387
1d4dfdef
DG
388 /* Get the full subbuffer size including padding */
389 err = kernctl_get_padded_subbuf_size(infd, &len);
390 if (err != 0) {
391 errno = -err;
392 perror("Getting sub-buffer len failed.");
393 ret = err;
394 goto end;
395 }
396
d41f73b7 397 switch (stream->output) {
1d4dfdef 398 case LTTNG_EVENT_SPLICE:
d41f73b7 399
1d4dfdef
DG
400 /*
401 * XXX: The lttng-modules splice "actor" does not handle copying
402 * partial pages hence only using the subbuffer size without the
403 * padding makes the splice fail.
404 */
405 subbuf_size = len;
406 padding = 0;
407
408 /* splice the subbuffer to the tracefile */
91dfef6e
DG
409 ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, subbuf_size,
410 padding);
411 /*
412 * XXX: Splice does not support network streaming so the return value
413 * is simply checked against subbuf_size and not like the mmap() op.
414 */
1d4dfdef
DG
415 if (ret != subbuf_size) {
416 /*
417 * display the error but continue processing to try
418 * to release the subbuffer
419 */
420 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
421 ret, subbuf_size);
422 }
423 break;
424 case LTTNG_EVENT_MMAP:
425 /* Get subbuffer size without padding */
426 err = kernctl_get_subbuf_size(infd, &subbuf_size);
427 if (err != 0) {
428 errno = -err;
429 perror("Getting sub-buffer len failed.");
430 ret = err;
431 goto end;
432 }
47e81c02 433
1d4dfdef
DG
434 /* Make sure the tracer is not gone mad on us! */
435 assert(len >= subbuf_size);
436
437 padding = len - subbuf_size;
438
439 /* write the subbuffer to the tracefile */
91dfef6e
DG
440 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size,
441 padding);
442 /*
443 * The mmap operation should write subbuf_size amount of data when
444 * network streaming or the full padding (len) size when we are _not_
445 * streaming.
446 */
447 if ((ret != subbuf_size && stream->net_seq_idx != -1) ||
448 (ret != len && stream->net_seq_idx == -1)) {
1d4dfdef 449 /*
91dfef6e
DG
450 * Display the error but continue processing to try to release the
451 * subbuffer
1d4dfdef 452 */
91dfef6e
DG
453 ERR("Error writing to tracefile "
454 "(ret: %zd != len: %lu != subbuf_size: %lu)",
455 ret, len, subbuf_size);
1d4dfdef
DG
456 }
457 break;
458 default:
459 ERR("Unknown output method");
460 ret = -1;
d41f73b7
MD
461 }
462
463 err = kernctl_put_next_subbuf(infd);
464 if (err != 0) {
21073eaa 465 errno = -err;
d41f73b7
MD
466 if (errno == EFAULT) {
467 perror("Error in unreserving sub buffer\n");
468 } else if (errno == EIO) {
469 /* Should never happen with newer LTTng versions */
470 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
471 }
21073eaa
DG
472
473 ret = -err;
d41f73b7
MD
474 goto end;
475 }
476
477end:
478 return ret;
479}
480
481int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
482{
483 int ret;
484
485 /* Opening the tracefile in write mode */
00e2e675 486 if (strlen(stream->path_name) > 0 && stream->net_seq_idx == -1) {
e11d277b 487 ret = run_as_open(stream->path_name,
60b6c79c
MD
488 O_WRONLY|O_CREAT|O_TRUNC,
489 S_IRWXU|S_IRWXG|S_IRWXO,
490 stream->uid, stream->gid);
d41f73b7
MD
491 if (ret < 0) {
492 ERR("Opening %s", stream->path_name);
493 perror("open");
494 goto error;
495 }
496 stream->out_fd = ret;
497 }
498
499 if (stream->output == LTTNG_EVENT_MMAP) {
500 /* get the len of the mmap region */
501 unsigned long mmap_len;
502
503 ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len);
504 if (ret != 0) {
87dc6a9c 505 errno = -ret;
d41f73b7
MD
506 perror("kernctl_get_mmap_len");
507 goto error_close_fd;
508 }
509 stream->mmap_len = (size_t) mmap_len;
510
511 stream->mmap_base = mmap(NULL, stream->mmap_len,
512 PROT_READ, MAP_PRIVATE, stream->wait_fd, 0);
513 if (stream->mmap_base == MAP_FAILED) {
514 perror("Error mmaping");
515 ret = -1;
516 goto error_close_fd;
517 }
518 }
519
520 /* we return 0 to let the library handle the FD internally */
521 return 0;
522
523error_close_fd:
524 {
525 int err;
526
527 err = close(stream->out_fd);
528 assert(!err);
529 }
530error:
531 return ret;
532}
533
ca22feea
DG
534/*
535 * Check if data is still being extracted from the buffers for a specific
4e9a4686
DG
536 * stream. Consumer data lock MUST be acquired before calling this function
537 * and the stream lock.
ca22feea 538 *
6d805429 539 * Return 1 if the traced data are still getting read else 0 meaning that the
ca22feea
DG
540 * data is available for trace viewer reading.
541 */
6d805429 542int lttng_kconsumer_data_pending(struct lttng_consumer_stream *stream)
ca22feea
DG
543{
544 int ret;
545
546 assert(stream);
547
ca22feea
DG
548 ret = kernctl_get_next_subbuf(stream->wait_fd);
549 if (ret == 0) {
550 /* There is still data so let's put back this subbuffer. */
551 ret = kernctl_put_subbuf(stream->wait_fd);
552 assert(ret == 0);
6d805429 553 ret = 1; /* Data is pending */
4e9a4686 554 goto end;
ca22feea
DG
555 }
556
6d805429
DG
557 /* Data is NOT pending and ready to be read. */
558 ret = 0;
ca22feea 559
6efae65e
DG
560end:
561 return ret;
ca22feea 562}
This page took 0.059762 seconds and 4 git commands to generate.