Add consumer socket object and relayd commands
[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 <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/stat.h>
28 #include <sys/types.h>
29 #include <unistd.h>
30 #include <lttng/ust-ctl.h>
31
32 #include <common/common.h>
33 #include <common/sessiond-comm/sessiond-comm.h>
34 #include <common/relayd/relayd.h>
35 #include <common/compat/fcntl.h>
36
37 #include "ust-consumer.h"
38
39 extern struct lttng_consumer_global_data consumer_data;
40 extern int consumer_poll_timeout;
41 extern volatile int consumer_quit;
42
43 /*
44 * Mmap the ring buffer, read it and write the data to the tracefile.
45 *
46 * Returns the number of bytes written, else negative value on error.
47 */
48 ssize_t lttng_ustconsumer_on_read_subbuffer_mmap(
49 struct lttng_consumer_local_data *ctx,
50 struct lttng_consumer_stream *stream, unsigned long len)
51 {
52 unsigned long mmap_offset;
53 long ret = 0, written = 0;
54 off_t orig_offset = stream->out_fd_offset;
55 int outfd = stream->out_fd;
56 uint64_t metadata_id;
57 struct consumer_relayd_sock_pair *relayd = NULL;
58
59 /* RCU lock for the relayd pointer */
60 rcu_read_lock();
61
62 /* Flag that the current stream if set for network streaming. */
63 if (stream->net_seq_idx != -1) {
64 relayd = consumer_find_relayd(stream->net_seq_idx);
65 if (relayd == NULL) {
66 ERR("UST consumer mmap(), unable to find relay for index %d",
67 stream->net_seq_idx);
68 goto end;
69 }
70 }
71
72 /* get the offset inside the fd to mmap */
73 ret = ustctl_get_mmap_read_offset(stream->chan->handle,
74 stream->buf, &mmap_offset);
75 if (ret != 0) {
76 errno = -ret;
77 PERROR("ustctl_get_mmap_read_offset");
78 written = ret;
79 goto end;
80 }
81
82 /* Handle stream on the relayd if the output is on the network */
83 if (relayd) {
84 unsigned long netlen = len;
85
86 if (stream->metadata_flag) {
87 /* Only lock if metadata since we use the control socket. */
88 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
89 netlen += sizeof(stream->relayd_stream_id);
90 }
91
92 ret = consumer_handle_stream_before_relayd(stream, netlen);
93 if (ret >= 0) {
94 outfd = ret;
95
96 /* Write metadata stream id before payload */
97 if (stream->metadata_flag) {
98 metadata_id = htobe64(stream->relayd_stream_id);
99 do {
100 ret = write(outfd, (void *) &metadata_id,
101 sizeof(stream->relayd_stream_id));
102 } while (ret < 0 && errno == EINTR);
103 if (ret < 0) {
104 PERROR("write metadata stream id");
105 written = ret;
106 goto end;
107 }
108 DBG("Metadata stream id %zu written before data",
109 stream->relayd_stream_id);
110 }
111 }
112 /* Else, use the default set before which is the filesystem. */
113 }
114
115 while (len > 0) {
116 do {
117 ret = write(outfd, stream->mmap_base + mmap_offset, len);
118 } while (ret < 0 && errno == EINTR);
119 if (ret < 0) {
120 PERROR("Error in file write");
121 if (written == 0) {
122 written = ret;
123 }
124 goto end;
125 } else if (ret > len) {
126 PERROR("ret %ld > len %lu", ret, len);
127 written += ret;
128 goto end;
129 } else {
130 len -= ret;
131 mmap_offset += ret;
132 }
133 DBG("UST mmap write() ret %ld (len %lu)", ret, len);
134
135 /* This call is useless on a socket so better save a syscall. */
136 if (!relayd) {
137 /* This won't block, but will start writeout asynchronously */
138 lttng_sync_file_range(outfd, stream->out_fd_offset, ret,
139 SYNC_FILE_RANGE_WRITE);
140 stream->out_fd_offset += ret;
141 }
142 written += ret;
143 }
144 lttng_consumer_sync_trace_file(stream, orig_offset);
145
146 end:
147 if (relayd && stream->metadata_flag) {
148 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
149 }
150 rcu_read_unlock();
151 return written;
152 }
153
154 /*
155 * Splice the data from the ring buffer to the tracefile.
156 *
157 * Returns the number of bytes spliced.
158 */
159 ssize_t lttng_ustconsumer_on_read_subbuffer_splice(
160 struct lttng_consumer_local_data *ctx,
161 struct lttng_consumer_stream *stream, unsigned long len)
162 {
163 return -ENOSYS;
164 }
165
166 /*
167 * Take a snapshot for a specific fd
168 *
169 * Returns 0 on success, < 0 on error
170 */
171 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_local_data *ctx,
172 struct lttng_consumer_stream *stream)
173 {
174 int ret = 0;
175
176 ret = ustctl_snapshot(stream->chan->handle, stream->buf);
177 if (ret != 0) {
178 errno = -ret;
179 PERROR("Getting sub-buffer snapshot.");
180 }
181
182 return ret;
183 }
184
185 /*
186 * Get the produced position
187 *
188 * Returns 0 on success, < 0 on error
189 */
190 int lttng_ustconsumer_get_produced_snapshot(
191 struct lttng_consumer_local_data *ctx,
192 struct lttng_consumer_stream *stream,
193 unsigned long *pos)
194 {
195 int ret;
196
197 ret = ustctl_snapshot_get_produced(stream->chan->handle,
198 stream->buf, pos);
199 if (ret != 0) {
200 errno = -ret;
201 PERROR("kernctl_snapshot_get_produced");
202 }
203
204 return ret;
205 }
206
207 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
208 int sock, struct pollfd *consumer_sockpoll)
209 {
210 ssize_t ret;
211 struct lttcomm_consumer_msg msg;
212
213 ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
214 if (ret != sizeof(msg)) {
215 DBG("Consumer received unexpected message size %zd (expects %zu)",
216 ret, sizeof(msg));
217 lttng_consumer_send_error(ctx, CONSUMERD_ERROR_RECV_FD);
218 return ret;
219 }
220 if (msg.cmd_type == LTTNG_CONSUMER_STOP) {
221 return -ENOENT;
222 }
223
224 /* relayd need RCU read-side lock */
225 rcu_read_lock();
226
227 switch (msg.cmd_type) {
228 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
229 {
230 int fd;
231 struct consumer_relayd_sock_pair *relayd;
232
233 DBG("UST Consumer adding relayd socket");
234
235 /* Get relayd reference if exists. */
236 relayd = consumer_find_relayd(msg.u.relayd_sock.net_index);
237 if (relayd == NULL) {
238 /* Not found. Allocate one. */
239 relayd = consumer_allocate_relayd_sock_pair(
240 msg.u.relayd_sock.net_index);
241 if (relayd == NULL) {
242 lttng_consumer_send_error(ctx, CONSUMERD_OUTFD_ERROR);
243 goto end_nosignal;
244 }
245 }
246
247 /* Poll on consumer socket. */
248 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
249 return -EINTR;
250 }
251
252 /* Get relayd socket from session daemon */
253 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
254 if (ret != sizeof(fd)) {
255 lttng_consumer_send_error(ctx, CONSUMERD_ERROR_RECV_FD);
256 goto end_nosignal;
257 }
258
259 /* Copy socket information and received FD */
260 switch (msg.u.relayd_sock.type) {
261 case LTTNG_STREAM_CONTROL:
262 /* Copy received lttcomm socket */
263 lttcomm_copy_sock(&relayd->control_sock, &msg.u.relayd_sock.sock);
264 ret = lttcomm_create_sock(&relayd->control_sock);
265 if (ret < 0) {
266 goto end_nosignal;
267 }
268
269 /* Close the created socket fd which is useless */
270 close(relayd->control_sock.fd);
271
272 /* Assign new file descriptor */
273 relayd->control_sock.fd = fd;
274 break;
275 case LTTNG_STREAM_DATA:
276 /* Copy received lttcomm socket */
277 lttcomm_copy_sock(&relayd->data_sock, &msg.u.relayd_sock.sock);
278 ret = lttcomm_create_sock(&relayd->data_sock);
279 if (ret < 0) {
280 goto end_nosignal;
281 }
282
283 /* Close the created socket fd which is useless */
284 close(relayd->data_sock.fd);
285
286 /* Assign new file descriptor */
287 relayd->data_sock.fd = fd;
288 break;
289 default:
290 ERR("Unknown relayd socket type");
291 goto end_nosignal;
292 }
293
294 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
295 msg.u.relayd_sock.type == LTTNG_STREAM_CONTROL ? "control" : "data",
296 relayd->net_seq_idx, fd);
297
298 /*
299 * Add relayd socket pair to consumer data hashtable. If object already
300 * exists or on error, the function gracefully returns.
301 */
302 consumer_add_relayd(relayd);
303
304 goto end_nosignal;
305 }
306 case LTTNG_CONSUMER_ADD_CHANNEL:
307 {
308 struct lttng_consumer_channel *new_channel;
309 int fds[1];
310 size_t nb_fd = 1;
311
312 DBG("UST Consumer adding channel");
313
314 /* block */
315 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
316 return -EINTR;
317 }
318 ret = lttcomm_recv_fds_unix_sock(sock, fds, nb_fd);
319 if (ret != sizeof(fds)) {
320 lttng_consumer_send_error(ctx, CONSUMERD_ERROR_RECV_FD);
321 return ret;
322 }
323
324 DBG("consumer_add_channel %d", msg.u.channel.channel_key);
325
326 new_channel = consumer_allocate_channel(msg.u.channel.channel_key,
327 fds[0], -1,
328 msg.u.channel.mmap_len,
329 msg.u.channel.max_sb_size);
330 if (new_channel == NULL) {
331 lttng_consumer_send_error(ctx, CONSUMERD_OUTFD_ERROR);
332 goto end_nosignal;
333 }
334 if (ctx->on_recv_channel != NULL) {
335 ret = ctx->on_recv_channel(new_channel);
336 if (ret == 0) {
337 consumer_add_channel(new_channel);
338 } else if (ret < 0) {
339 goto end_nosignal;
340 }
341 } else {
342 consumer_add_channel(new_channel);
343 }
344 goto end_nosignal;
345 }
346 case LTTNG_CONSUMER_ADD_STREAM:
347 {
348 struct lttng_consumer_stream *new_stream;
349 int fds[2];
350 size_t nb_fd = 2;
351 struct consumer_relayd_sock_pair *relayd = NULL;
352
353 DBG("UST Consumer adding stream");
354
355 /* block */
356 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
357 return -EINTR;
358 }
359 ret = lttcomm_recv_fds_unix_sock(sock, fds, nb_fd);
360 if (ret != sizeof(fds)) {
361 lttng_consumer_send_error(ctx, CONSUMERD_ERROR_RECV_FD);
362 return ret;
363 }
364
365 DBG("consumer_add_stream chan %d stream %d",
366 msg.u.stream.channel_key,
367 msg.u.stream.stream_key);
368
369 assert(msg.u.stream.output == LTTNG_EVENT_MMAP);
370 new_stream = consumer_allocate_stream(msg.u.stream.channel_key,
371 msg.u.stream.stream_key,
372 fds[0], fds[1],
373 msg.u.stream.state,
374 msg.u.stream.mmap_len,
375 msg.u.stream.output,
376 msg.u.stream.path_name,
377 msg.u.stream.uid,
378 msg.u.stream.gid,
379 msg.u.stream.net_index,
380 msg.u.stream.metadata_flag);
381 if (new_stream == NULL) {
382 lttng_consumer_send_error(ctx, CONSUMERD_OUTFD_ERROR);
383 goto end;
384 }
385
386 /* The stream is not metadata. Get relayd reference if exists. */
387 relayd = consumer_find_relayd(msg.u.stream.net_index);
388 if (relayd != NULL) {
389 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
390 /* Add stream on the relayd */
391 ret = relayd_add_stream(&relayd->control_sock,
392 msg.u.stream.name, msg.u.stream.path_name,
393 &new_stream->relayd_stream_id);
394 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
395 if (ret < 0) {
396 goto end;
397 }
398 } else if (msg.u.stream.net_index != -1) {
399 ERR("Network sequence index %d unknown. Not adding stream.",
400 msg.u.stream.net_index);
401 free(new_stream);
402 goto end;
403 }
404
405 if (ctx->on_recv_stream != NULL) {
406 ret = ctx->on_recv_stream(new_stream);
407 if (ret == 0) {
408 consumer_add_stream(new_stream);
409 } else if (ret < 0) {
410 goto end;
411 }
412 } else {
413 consumer_add_stream(new_stream);
414 }
415
416 DBG("UST consumer_add_stream %s (%d,%d) with relayd id %lu",
417 msg.u.stream.path_name, fds[0], fds[1],
418 new_stream->relayd_stream_id);
419 break;
420 }
421 case LTTNG_CONSUMER_DESTROY_RELAYD:
422 {
423 struct consumer_relayd_sock_pair *relayd;
424
425 DBG("UST consumer destroying relayd %zu",
426 msg.u.destroy_relayd.net_seq_idx);
427
428 /* Get relayd reference if exists. */
429 relayd = consumer_find_relayd(msg.u.destroy_relayd.net_seq_idx);
430 if (relayd == NULL) {
431 ERR("Unable to find relayd %zu",
432 msg.u.destroy_relayd.net_seq_idx);
433 }
434
435 /* Set destroy flag for this object */
436 uatomic_set(&relayd->destroy_flag, 1);
437
438 /* Destroy the relayd if refcount is 0 else set the destroy flag. */
439 if (uatomic_read(&relayd->refcount) == 0) {
440 consumer_destroy_relayd(relayd);
441 }
442 break;
443 }
444 case LTTNG_CONSUMER_UPDATE_STREAM:
445 {
446 return -ENOSYS;
447 #if 0
448 if (ctx->on_update_stream != NULL) {
449 ret = ctx->on_update_stream(msg.u.stream.stream_key, msg.u.stream.state);
450 if (ret == 0) {
451 consumer_change_stream_state(msg.u.stream.stream_key, msg.u.stream.state);
452 } else if (ret < 0) {
453 goto end;
454 }
455 } else {
456 consumer_change_stream_state(msg.u.stream.stream_key,
457 msg.u.stream.state);
458 }
459 #endif
460 break;
461 }
462 default:
463 break;
464 }
465 end:
466 /*
467 * Wake-up the other end by writing a null byte in the pipe
468 * (non-blocking). Important note: Because writing into the
469 * pipe is non-blocking (and therefore we allow dropping wakeup
470 * data, as long as there is wakeup data present in the pipe
471 * buffer to wake up the other end), the other end should
472 * perform the following sequence for waiting:
473 * 1) empty the pipe (reads).
474 * 2) perform update operation.
475 * 3) wait on the pipe (poll).
476 */
477 do {
478 ret = write(ctx->consumer_poll_pipe[1], "", 1);
479 } while (ret < 0 && errno == EINTR);
480 end_nosignal:
481 rcu_read_unlock();
482 return 0;
483 }
484
485 int lttng_ustconsumer_allocate_channel(struct lttng_consumer_channel *chan)
486 {
487 struct lttng_ust_object_data obj;
488
489 obj.handle = -1;
490 obj.shm_fd = chan->shm_fd;
491 obj.wait_fd = chan->wait_fd;
492 obj.memory_map_size = chan->mmap_len;
493 chan->handle = ustctl_map_channel(&obj);
494 if (!chan->handle) {
495 return -ENOMEM;
496 }
497 chan->wait_fd_is_copy = 1;
498 chan->shm_fd = -1;
499
500 return 0;
501 }
502
503 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream *stream)
504 {
505 ustctl_flush_buffer(stream->chan->handle, stream->buf, 0);
506 stream->hangup_flush_done = 1;
507 }
508
509 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel *chan)
510 {
511 ustctl_unmap_channel(chan->handle);
512 }
513
514 int lttng_ustconsumer_allocate_stream(struct lttng_consumer_stream *stream)
515 {
516 struct lttng_ust_object_data obj;
517 int ret;
518
519 obj.handle = -1;
520 obj.shm_fd = stream->shm_fd;
521 obj.wait_fd = stream->wait_fd;
522 obj.memory_map_size = stream->mmap_len;
523 ret = ustctl_add_stream(stream->chan->handle, &obj);
524 if (ret)
525 return ret;
526 stream->buf = ustctl_open_stream_read(stream->chan->handle, stream->cpu);
527 if (!stream->buf)
528 return -EBUSY;
529 /* ustctl_open_stream_read has closed the shm fd. */
530 stream->wait_fd_is_copy = 1;
531 stream->shm_fd = -1;
532
533 stream->mmap_base = ustctl_get_mmap_base(stream->chan->handle, stream->buf);
534 if (!stream->mmap_base) {
535 return -EINVAL;
536 }
537
538 return 0;
539 }
540
541 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream *stream)
542 {
543 ustctl_close_stream_read(stream->chan->handle, stream->buf);
544 }
545
546
547 int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
548 struct lttng_consumer_local_data *ctx)
549 {
550 unsigned long len;
551 int err;
552 long ret = 0;
553 struct lttng_ust_shm_handle *handle;
554 struct lttng_ust_lib_ring_buffer *buf;
555 char dummy;
556 ssize_t readlen;
557
558 DBG("In read_subbuffer (wait_fd: %d, stream key: %d)",
559 stream->wait_fd, stream->key);
560
561 /* We can consume the 1 byte written into the wait_fd by UST */
562 if (!stream->hangup_flush_done) {
563 do {
564 readlen = read(stream->wait_fd, &dummy, 1);
565 } while (readlen == -1 && errno == EINTR);
566 if (readlen == -1) {
567 ret = readlen;
568 goto end;
569 }
570 }
571
572 buf = stream->buf;
573 handle = stream->chan->handle;
574 /* Get the next subbuffer */
575 err = ustctl_get_next_subbuf(handle, buf);
576 if (err != 0) {
577 ret = -ret; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
578 /*
579 * This is a debug message even for single-threaded consumer,
580 * because poll() have more relaxed criterions than get subbuf,
581 * so get_subbuf may fail for short race windows where poll()
582 * would issue wakeups.
583 */
584 DBG("Reserving sub buffer failed (everything is normal, "
585 "it is due to concurrency)");
586 goto end;
587 }
588 assert(stream->output == LTTNG_EVENT_MMAP);
589 /* read the used subbuffer size */
590 err = ustctl_get_padded_subbuf_size(handle, buf, &len);
591 assert(err == 0);
592 /* write the subbuffer to the tracefile */
593 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, len);
594 if (ret != len) {
595 /*
596 * display the error but continue processing to try
597 * to release the subbuffer
598 */
599 ERR("Error writing to tracefile");
600 }
601 err = ustctl_put_next_subbuf(handle, buf);
602 assert(err == 0);
603 end:
604 return ret;
605 }
606
607 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
608 {
609 int ret;
610
611 /* Opening the tracefile in write mode */
612 if (stream->path_name != NULL && stream->net_seq_idx == -1) {
613 ret = run_as_open(stream->path_name,
614 O_WRONLY|O_CREAT|O_TRUNC,
615 S_IRWXU|S_IRWXG|S_IRWXO,
616 stream->uid, stream->gid);
617 if (ret < 0) {
618 ERR("Opening %s", stream->path_name);
619 PERROR("open");
620 goto error;
621 }
622 stream->out_fd = ret;
623 }
624
625 /* we return 0 to let the library handle the FD internally */
626 return 0;
627
628 error:
629 return ret;
630 }
This page took 0.041888 seconds and 5 git commands to generate.