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