Map session id of relayd and sessiond in consumer
[lttng-tools.git] / src / common / kernel-consumer / kernel-consumer.c
1 /*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
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.
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 *
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.
17 */
18
19 #define _GNU_SOURCE
20 #include <assert.h>
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>
28 #include <inttypes.h>
29 #include <unistd.h>
30 #include <sys/stat.h>
31
32 #include <common/common.h>
33 #include <common/kernel-ctl/kernel-ctl.h>
34 #include <common/sessiond-comm/sessiond-comm.h>
35 #include <common/sessiond-comm/relayd.h>
36 #include <common/compat/fcntl.h>
37 #include <common/relayd/relayd.h>
38
39 #include "kernel-consumer.h"
40
41 extern struct lttng_consumer_global_data consumer_data;
42 extern int consumer_poll_timeout;
43 extern volatile int consumer_quit;
44
45 /*
46 * Take a snapshot for a specific fd
47 *
48 * Returns 0 on success, < 0 on error
49 */
50 int 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) {
58 errno = -ret;
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 */
70 int 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) {
80 errno = -ret;
81 perror("kernctl_snapshot_get_produced");
82 }
83
84 return ret;
85 }
86
87 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
88 int sock, struct pollfd *consumer_sockpoll)
89 {
90 ssize_t ret;
91 enum lttng_error_code ret_code = LTTNG_OK;
92 struct lttcomm_consumer_msg msg;
93
94 ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
95 if (ret != sizeof(msg)) {
96 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
97 return ret;
98 }
99 if (msg.cmd_type == LTTNG_CONSUMER_STOP) {
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);
108 return -ENOENT;
109 }
110
111 /* relayd needs RCU read-side protection */
112 rcu_read_lock();
113
114 switch (msg.cmd_type) {
115 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
116 {
117 /* Session daemon status message are handled in the following call. */
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, msg.u.relayd_sock.session_id);
121 goto end_nosignal;
122 }
123 case LTTNG_CONSUMER_ADD_CHANNEL:
124 {
125 struct lttng_consumer_channel *new_channel;
126
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
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,
138 msg.u.channel.max_sb_size,
139 msg.u.channel.nb_init_streams);
140 if (new_channel == NULL) {
141 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
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 {
158 int fd, stream_pipe;
159 struct consumer_relayd_sock_pair *relayd = NULL;
160 struct lttng_consumer_stream *new_stream;
161 int alloc_ret = 0;
162
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
170 /* block */
171 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
172 rcu_read_unlock();
173 return -EINTR;
174 }
175
176 /* Get stream file descriptor from socket */
177 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
178 if (ret != sizeof(fd)) {
179 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
180 rcu_read_unlock();
181 return ret;
182 }
183
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
195 new_stream = consumer_allocate_stream(msg.u.stream.channel_key,
196 msg.u.stream.stream_key,
197 fd, fd,
198 msg.u.stream.state,
199 msg.u.stream.mmap_len,
200 msg.u.stream.output,
201 msg.u.stream.path_name,
202 msg.u.stream.uid,
203 msg.u.stream.gid,
204 msg.u.stream.net_index,
205 msg.u.stream.metadata_flag,
206 msg.u.stream.session_id,
207 &alloc_ret);
208 if (new_stream == NULL) {
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 }
223 goto end_nosignal;
224 }
225
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
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) {
245 consumer_del_stream(new_stream, NULL);
246 goto end_nosignal;
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);
251 consumer_del_stream(new_stream, NULL);
252 goto end_nosignal;
253 }
254
255 if (ctx->on_recv_stream) {
256 ret = ctx->on_recv_stream(new_stream);
257 if (ret < 0) {
258 consumer_del_stream(new_stream, NULL);
259 goto end_nosignal;
260 }
261 }
262
263 /* Get the right pipe where the stream will be sent. */
264 if (new_stream->metadata_flag) {
265 stream_pipe = ctx->consumer_metadata_pipe[1];
266 } else {
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;
279 }
280
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);
283 break;
284 }
285 case LTTNG_CONSUMER_UPDATE_STREAM:
286 {
287 rcu_read_unlock();
288 return -ENOSYS;
289 }
290 case LTTNG_CONSUMER_DESTROY_RELAYD:
291 {
292 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
293 struct consumer_relayd_sock_pair *relayd;
294
295 DBG("Kernel consumer destroying relayd %" PRIu64, index);
296
297 /* Get relayd reference if exists. */
298 relayd = consumer_find_relayd(index);
299 if (relayd == NULL) {
300 ERR("Unable to find relayd %" PRIu64, index);
301 ret_code = LTTNG_ERR_NO_CONSUMER;
302 }
303
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 */
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 }
323
324 goto end_nosignal;
325 }
326 case LTTNG_CONSUMER_DATA_PENDING:
327 {
328 int32_t ret;
329 uint64_t id = msg.u.data_pending.session_id;
330
331 DBG("Kernel consumer data pending command for id %" PRIu64, id);
332
333 ret = consumer_data_pending(id);
334
335 /* Send back returned value to session daemon */
336 ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret));
337 if (ret < 0) {
338 PERROR("send data pending ret code");
339 }
340
341 /*
342 * No need to send back a status message since the data pending
343 * returned value is the response.
344 */
345 break;
346 }
347 default:
348 goto end_nosignal;
349 }
350
351 end_nosignal:
352 rcu_read_unlock();
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;
359 }
360
361 /*
362 * Consume data on a file descriptor and write it on a trace file.
363 */
364 ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
365 struct lttng_consumer_local_data *ctx)
366 {
367 unsigned long len, subbuf_size, padding;
368 int err;
369 ssize_t ret = 0;
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) {
376 ret = err;
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
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
397 switch (stream->output) {
398 case LTTNG_EVENT_SPLICE:
399
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 */
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 */
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 }
433
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 */
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)) {
449 /*
450 * Display the error but continue processing to try to release the
451 * subbuffer
452 */
453 ERR("Error writing to tracefile "
454 "(ret: %zd != len: %lu != subbuf_size: %lu)",
455 ret, len, subbuf_size);
456 }
457 break;
458 default:
459 ERR("Unknown output method");
460 ret = -1;
461 }
462
463 err = kernctl_put_next_subbuf(infd);
464 if (err != 0) {
465 errno = -err;
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 }
472
473 ret = -err;
474 goto end;
475 }
476
477 end:
478 return ret;
479 }
480
481 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
482 {
483 int ret;
484
485 /* Opening the tracefile in write mode */
486 if (strlen(stream->path_name) > 0 && stream->net_seq_idx == -1) {
487 ret = run_as_open(stream->path_name,
488 O_WRONLY|O_CREAT|O_TRUNC,
489 S_IRWXU|S_IRWXG|S_IRWXO,
490 stream->uid, stream->gid);
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) {
505 errno = -ret;
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
523 error_close_fd:
524 {
525 int err;
526
527 err = close(stream->out_fd);
528 assert(!err);
529 }
530 error:
531 return ret;
532 }
533
534 /*
535 * Check if data is still being extracted from the buffers for a specific
536 * stream. Consumer data lock MUST be acquired before calling this function
537 * and the stream lock.
538 *
539 * Return 1 if the traced data are still getting read else 0 meaning that the
540 * data is available for trace viewer reading.
541 */
542 int lttng_kconsumer_data_pending(struct lttng_consumer_stream *stream)
543 {
544 int ret;
545
546 assert(stream);
547
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);
553 ret = 1; /* Data is pending */
554 goto end;
555 }
556
557 /* Data is NOT pending and ready to be read. */
558 ret = 0;
559
560 end:
561 return ret;
562 }
This page took 0.041022 seconds and 5 git commands to generate.