c0999b613b496446c3c0de472af7f76cad6085d1
[lttng-tools.git] / src / common / ust-consumer / ust-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 <lttng/ust-ctl.h>
22 #include <poll.h>
23 #include <pthread.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/mman.h>
27 #include <sys/socket.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <inttypes.h>
31 #include <unistd.h>
32
33 #include <common/common.h>
34 #include <common/sessiond-comm/sessiond-comm.h>
35 #include <common/relayd/relayd.h>
36 #include <common/compat/fcntl.h>
37
38 #include "ust-consumer.h"
39
40 extern struct lttng_consumer_global_data consumer_data;
41 extern int consumer_poll_timeout;
42 extern volatile int consumer_quit;
43
44 /*
45 * Wrapper over the mmap() read offset from ust-ctl library. Since this can be
46 * compiled out, we isolate it in this library.
47 */
48 int lttng_ustctl_get_mmap_read_offset(struct lttng_ust_shm_handle *handle,
49 struct lttng_ust_lib_ring_buffer *buf, unsigned long *off)
50 {
51 return ustctl_get_mmap_read_offset(handle, buf, off);
52 };
53
54 /*
55 * Take a snapshot for a specific fd
56 *
57 * Returns 0 on success, < 0 on error
58 */
59 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_local_data *ctx,
60 struct lttng_consumer_stream *stream)
61 {
62 int ret = 0;
63
64 ret = ustctl_snapshot(stream->chan->handle, stream->buf);
65 if (ret != 0) {
66 errno = -ret;
67 PERROR("Getting sub-buffer snapshot.");
68 }
69
70 return ret;
71 }
72
73 /*
74 * Get the produced position
75 *
76 * Returns 0 on success, < 0 on error
77 */
78 int lttng_ustconsumer_get_produced_snapshot(
79 struct lttng_consumer_local_data *ctx,
80 struct lttng_consumer_stream *stream,
81 unsigned long *pos)
82 {
83 int ret;
84
85 ret = ustctl_snapshot_get_produced(stream->chan->handle,
86 stream->buf, pos);
87 if (ret != 0) {
88 errno = -ret;
89 PERROR("kernctl_snapshot_get_produced");
90 }
91
92 return ret;
93 }
94
95 /*
96 * Receive command from session daemon and process it.
97 *
98 * Return 1 on success else a negative value or 0.
99 */
100 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
101 int sock, struct pollfd *consumer_sockpoll)
102 {
103 ssize_t ret;
104 enum lttng_error_code ret_code = LTTNG_OK;
105 struct lttcomm_consumer_msg msg;
106
107 ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
108 if (ret != sizeof(msg)) {
109 DBG("Consumer received unexpected message size %zd (expects %zu)",
110 ret, sizeof(msg));
111 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
112 return ret;
113 }
114 if (msg.cmd_type == LTTNG_CONSUMER_STOP) {
115 /*
116 * Notify the session daemon that the command is completed.
117 *
118 * On transport layer error, the function call will print an error
119 * message so handling the returned code is a bit useless since we
120 * return an error code anyway.
121 */
122 (void) consumer_send_status_msg(sock, ret_code);
123 return -ENOENT;
124 }
125
126 /* relayd needs RCU read-side lock */
127 rcu_read_lock();
128
129 switch (msg.cmd_type) {
130 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
131 {
132 /* Session daemon status message are handled in the following call. */
133 ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
134 msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll,
135 &msg.u.relayd_sock.sock);
136 goto end_nosignal;
137 }
138 case LTTNG_CONSUMER_ADD_CHANNEL:
139 {
140 struct lttng_consumer_channel *new_channel;
141 int fds[1];
142 size_t nb_fd = 1;
143
144 DBG("UST Consumer adding channel");
145
146 /* First send a status message before receiving the fds. */
147 ret = consumer_send_status_msg(sock, ret_code);
148 if (ret < 0) {
149 /* Somehow, the session daemon is not responding anymore. */
150 goto end_nosignal;
151 }
152
153 /* block */
154 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
155 rcu_read_unlock();
156 return -EINTR;
157 }
158 ret = lttcomm_recv_fds_unix_sock(sock, fds, nb_fd);
159 if (ret != sizeof(fds)) {
160 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
161 rcu_read_unlock();
162 return ret;
163 }
164
165 /*
166 * Send status code to session daemon only if the recv works. If the
167 * above recv() failed, the session daemon is notified through the
168 * error socket and the teardown is eventually done.
169 */
170 ret = consumer_send_status_msg(sock, ret_code);
171 if (ret < 0) {
172 /* Somehow, the session daemon is not responding anymore. */
173 goto end_nosignal;
174 }
175
176 DBG("consumer_add_channel %d", msg.u.channel.channel_key);
177
178 new_channel = consumer_allocate_channel(msg.u.channel.channel_key,
179 fds[0], -1,
180 msg.u.channel.mmap_len,
181 msg.u.channel.max_sb_size,
182 msg.u.channel.nb_init_streams);
183 if (new_channel == NULL) {
184 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
185 goto end_nosignal;
186 }
187 if (ctx->on_recv_channel != NULL) {
188 ret = ctx->on_recv_channel(new_channel);
189 if (ret == 0) {
190 consumer_add_channel(new_channel);
191 } else if (ret < 0) {
192 goto end_nosignal;
193 }
194 } else {
195 consumer_add_channel(new_channel);
196 }
197 goto end_nosignal;
198 }
199 case LTTNG_CONSUMER_ADD_STREAM:
200 {
201 struct lttng_consumer_stream *new_stream;
202 int fds[2], stream_pipe;
203 size_t nb_fd = 2;
204 struct consumer_relayd_sock_pair *relayd = NULL;
205 int alloc_ret = 0;
206
207 DBG("UST Consumer adding stream");
208
209 /* First send a status message before receiving the fds. */
210 ret = consumer_send_status_msg(sock, ret_code);
211 if (ret < 0) {
212 /* Somehow, the session daemon is not responding anymore. */
213 goto end_nosignal;
214 }
215
216 /* block */
217 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
218 rcu_read_unlock();
219 return -EINTR;
220 }
221 ret = lttcomm_recv_fds_unix_sock(sock, fds, nb_fd);
222 if (ret != sizeof(fds)) {
223 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
224 rcu_read_unlock();
225 return ret;
226 }
227
228 /*
229 * Send status code to session daemon only if the recv works. If the
230 * above recv() failed, the session daemon is notified through the
231 * error socket and the teardown is eventually done.
232 */
233 ret = consumer_send_status_msg(sock, ret_code);
234 if (ret < 0) {
235 /* Somehow, the session daemon is not responding anymore. */
236 goto end_nosignal;
237 }
238
239 DBG("Consumer command ADD_STREAM chan %d stream %d",
240 msg.u.stream.channel_key, msg.u.stream.stream_key);
241
242 assert(msg.u.stream.output == LTTNG_EVENT_MMAP);
243 new_stream = consumer_allocate_stream(msg.u.stream.channel_key,
244 msg.u.stream.stream_key,
245 fds[0], fds[1],
246 msg.u.stream.state,
247 msg.u.stream.mmap_len,
248 msg.u.stream.output,
249 msg.u.stream.path_name,
250 msg.u.stream.uid,
251 msg.u.stream.gid,
252 msg.u.stream.net_index,
253 msg.u.stream.metadata_flag,
254 msg.u.stream.session_id,
255 &alloc_ret);
256 if (new_stream == NULL) {
257 switch (alloc_ret) {
258 case -ENOMEM:
259 case -EINVAL:
260 default:
261 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
262 break;
263 case -ENOENT:
264 /*
265 * We could not find the channel. Can happen if cpu hotplug
266 * happens while tearing down.
267 */
268 DBG3("Could not find channel");
269 break;
270 }
271 goto end_nosignal;
272 }
273
274 /* The stream is not metadata. Get relayd reference if exists. */
275 relayd = consumer_find_relayd(msg.u.stream.net_index);
276 if (relayd != NULL) {
277 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
278 /* Add stream on the relayd */
279 ret = relayd_add_stream(&relayd->control_sock,
280 msg.u.stream.name, msg.u.stream.path_name,
281 &new_stream->relayd_stream_id);
282 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
283 if (ret < 0) {
284 consumer_del_stream(new_stream, NULL);
285 goto end_nosignal;
286 }
287 } else if (msg.u.stream.net_index != -1) {
288 ERR("Network sequence index %d unknown. Not adding stream.",
289 msg.u.stream.net_index);
290 consumer_del_stream(new_stream, NULL);
291 goto end_nosignal;
292 }
293
294 /* Do actions once stream has been received. */
295 if (ctx->on_recv_stream) {
296 ret = ctx->on_recv_stream(new_stream);
297 if (ret < 0) {
298 consumer_del_stream(new_stream, NULL);
299 goto end_nosignal;
300 }
301 }
302
303 /* Get the right pipe where the stream will be sent. */
304 if (new_stream->metadata_flag) {
305 stream_pipe = ctx->consumer_metadata_pipe[1];
306 } else {
307 stream_pipe = ctx->consumer_data_pipe[1];
308 }
309
310 do {
311 ret = write(stream_pipe, &new_stream, sizeof(new_stream));
312 } while (ret < 0 && errno == EINTR);
313 if (ret < 0) {
314 PERROR("Consumer write %s stream to pipe %d",
315 new_stream->metadata_flag ? "metadata" : "data",
316 stream_pipe);
317 consumer_del_stream(new_stream, NULL);
318 goto end_nosignal;
319 }
320
321 DBG("UST consumer ADD_STREAM %s (%d,%d) with relayd id %" PRIu64,
322 msg.u.stream.path_name, fds[0], fds[1],
323 new_stream->relayd_stream_id);
324 break;
325 }
326 case LTTNG_CONSUMER_DESTROY_RELAYD:
327 {
328 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
329 struct consumer_relayd_sock_pair *relayd;
330
331 DBG("UST consumer destroying relayd %" PRIu64, index);
332
333 /* Get relayd reference if exists. */
334 relayd = consumer_find_relayd(index);
335 if (relayd == NULL) {
336 ERR("Unable to find relayd %" PRIu64, index);
337 ret_code = LTTNG_ERR_NO_CONSUMER;
338 }
339
340 /*
341 * Each relayd socket pair has a refcount of stream attached to it
342 * which tells if the relayd is still active or not depending on the
343 * refcount value.
344 *
345 * This will set the destroy flag of the relayd object and destroy it
346 * if the refcount reaches zero when called.
347 *
348 * The destroy can happen either here or when a stream fd hangs up.
349 */
350 if (relayd) {
351 consumer_flag_relayd_for_destroy(relayd);
352 }
353
354 ret = consumer_send_status_msg(sock, ret_code);
355 if (ret < 0) {
356 /* Somehow, the session daemon is not responding anymore. */
357 goto end_nosignal;
358 }
359
360 goto end_nosignal;
361 }
362 case LTTNG_CONSUMER_UPDATE_STREAM:
363 {
364 rcu_read_unlock();
365 return -ENOSYS;
366 }
367 case LTTNG_CONSUMER_DATA_PENDING:
368 {
369 int32_t ret;
370 uint64_t id = msg.u.data_pending.session_id;
371
372 DBG("UST consumer data pending command for id %" PRIu64, id);
373
374 ret = consumer_data_pending(id);
375
376 /* Send back returned value to session daemon */
377 ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret));
378 if (ret < 0) {
379 PERROR("send data pending ret code");
380 }
381
382 /*
383 * No need to send back a status message since the data pending
384 * returned value is the response.
385 */
386 break;
387 }
388 default:
389 break;
390 }
391
392 end_nosignal:
393 rcu_read_unlock();
394
395 /*
396 * Return 1 to indicate success since the 0 value can be a socket
397 * shutdown during the recv() or send() call.
398 */
399 return 1;
400 }
401
402 int lttng_ustconsumer_allocate_channel(struct lttng_consumer_channel *chan)
403 {
404 struct lttng_ust_object_data obj;
405
406 obj.handle = -1;
407 obj.shm_fd = chan->shm_fd;
408 obj.wait_fd = chan->wait_fd;
409 obj.memory_map_size = chan->mmap_len;
410 chan->handle = ustctl_map_channel(&obj);
411 if (!chan->handle) {
412 return -ENOMEM;
413 }
414 chan->wait_fd_is_copy = 1;
415 chan->shm_fd = -1;
416
417 return 0;
418 }
419
420 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream *stream)
421 {
422 ustctl_flush_buffer(stream->chan->handle, stream->buf, 0);
423 stream->hangup_flush_done = 1;
424 }
425
426 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel *chan)
427 {
428 ustctl_unmap_channel(chan->handle);
429 }
430
431 int lttng_ustconsumer_add_stream(struct lttng_consumer_stream *stream)
432 {
433 struct lttng_ust_object_data obj;
434 int ret;
435
436 obj.handle = -1;
437 obj.shm_fd = stream->shm_fd;
438 obj.wait_fd = stream->wait_fd;
439 obj.memory_map_size = stream->mmap_len;
440 ret = ustctl_add_stream(stream->chan->handle, &obj);
441 if (ret) {
442 ERR("UST ctl add_stream failed with ret %d", ret);
443 goto error;
444 }
445
446 stream->buf = ustctl_open_stream_read(stream->chan->handle, stream->cpu);
447 if (!stream->buf) {
448 ERR("UST ctl open_stream_read failed");
449 ret = -EBUSY;
450 goto error;
451 }
452
453 /* ustctl_open_stream_read has closed the shm fd. */
454 stream->wait_fd_is_copy = 1;
455 stream->shm_fd = -1;
456
457 stream->mmap_base = ustctl_get_mmap_base(stream->chan->handle, stream->buf);
458 if (!stream->mmap_base) {
459 ERR("UST ctl get_mmap_base failed");
460 ret = -EINVAL;
461 goto mmap_error;
462 }
463
464 return 0;
465
466 mmap_error:
467 ustctl_close_stream_read(stream->chan->handle, stream->buf);
468 error:
469 return ret;
470 }
471
472 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream *stream)
473 {
474 ustctl_close_stream_read(stream->chan->handle, stream->buf);
475 }
476
477
478 int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
479 struct lttng_consumer_local_data *ctx)
480 {
481 unsigned long len, subbuf_size, padding;
482 int err;
483 long ret = 0;
484 struct lttng_ust_shm_handle *handle;
485 struct lttng_ust_lib_ring_buffer *buf;
486 char dummy;
487 ssize_t readlen;
488
489 DBG("In read_subbuffer (wait_fd: %d, stream key: %d)",
490 stream->wait_fd, stream->key);
491
492 /* We can consume the 1 byte written into the wait_fd by UST */
493 if (!stream->hangup_flush_done) {
494 do {
495 readlen = read(stream->wait_fd, &dummy, 1);
496 } while (readlen == -1 && errno == EINTR);
497 if (readlen == -1) {
498 ret = readlen;
499 goto end;
500 }
501 }
502
503 buf = stream->buf;
504 handle = stream->chan->handle;
505 /* Get the next subbuffer */
506 err = ustctl_get_next_subbuf(handle, buf);
507 if (err != 0) {
508 ret = err; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
509 /*
510 * This is a debug message even for single-threaded consumer,
511 * because poll() have more relaxed criterions than get subbuf,
512 * so get_subbuf may fail for short race windows where poll()
513 * would issue wakeups.
514 */
515 DBG("Reserving sub buffer failed (everything is normal, "
516 "it is due to concurrency)");
517 goto end;
518 }
519 assert(stream->output == LTTNG_EVENT_MMAP);
520 /* Get the full padded subbuffer size */
521 err = ustctl_get_padded_subbuf_size(handle, buf, &len);
522 assert(err == 0);
523
524 /* Get subbuffer data size (without padding) */
525 err = ustctl_get_subbuf_size(handle, buf, &subbuf_size);
526 assert(err == 0);
527
528 /* Make sure we don't get a subbuffer size bigger than the padded */
529 assert(len >= subbuf_size);
530
531 padding = len - subbuf_size;
532 /* write the subbuffer to the tracefile */
533 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size, padding);
534 /*
535 * The mmap operation should write subbuf_size amount of data when network
536 * streaming or the full padding (len) size when we are _not_ streaming.
537 */
538 if ((ret != subbuf_size && stream->net_seq_idx != -1) ||
539 (ret != len && stream->net_seq_idx == -1)) {
540 /*
541 * Display the error but continue processing to try to release the
542 * subbuffer
543 */
544 ERR("Error writing to tracefile "
545 "(ret: %zd != len: %lu != subbuf_size: %lu)",
546 ret, len, subbuf_size);
547 }
548 err = ustctl_put_next_subbuf(handle, buf);
549 assert(err == 0);
550 end:
551 return ret;
552 }
553
554 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
555 {
556 int ret;
557
558 /* Opening the tracefile in write mode */
559 if (stream->path_name != NULL && stream->net_seq_idx == -1) {
560 ret = run_as_open(stream->path_name,
561 O_WRONLY|O_CREAT|O_TRUNC,
562 S_IRWXU|S_IRWXG|S_IRWXO,
563 stream->uid, stream->gid);
564 if (ret < 0) {
565 ERR("Opening %s", stream->path_name);
566 PERROR("open");
567 goto error;
568 }
569 stream->out_fd = ret;
570 }
571
572 ret = lttng_ustconsumer_add_stream(stream);
573 if (ret) {
574 consumer_del_stream(stream, NULL);
575 ret = -1;
576 goto error;
577 }
578
579 /* we return 0 to let the library handle the FD internally */
580 return 0;
581
582 error:
583 return ret;
584 }
585
586 /*
587 * Check if data is still being extracted from the buffers for a specific
588 * stream. Consumer data lock MUST be acquired before calling this function
589 * and the stream lock.
590 *
591 * Return 1 if the traced data are still getting read else 0 meaning that the
592 * data is available for trace viewer reading.
593 */
594 int lttng_ustconsumer_data_pending(struct lttng_consumer_stream *stream)
595 {
596 int ret;
597
598 assert(stream);
599
600 DBG("UST consumer checking data pending");
601
602 ret = ustctl_get_next_subbuf(stream->chan->handle, stream->buf);
603 if (ret == 0) {
604 /* There is still data so let's put back this subbuffer. */
605 ret = ustctl_put_subbuf(stream->chan->handle, stream->buf);
606 assert(ret == 0);
607 ret = 1; /* Data is pending */
608 goto end;
609 }
610
611 /* Data is NOT pending so ready to be read. */
612 ret = 0;
613
614 end:
615 return ret;
616 }
This page took 0.040057 seconds and 3 git commands to generate.