Fix: work-around glibc __nptl_setxid vs clone hang
[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>
28#include <unistd.h>
dbb5dfe6 29#include <sys/stat.h>
3bd1e081 30
990570ed 31#include <common/common.h>
10a8a223 32#include <common/kernel-ctl/kernel-ctl.h>
10a8a223 33#include <common/sessiond-comm/sessiond-comm.h>
dbb5dfe6 34#include <common/compat/fcntl.h>
0857097f 35
10a8a223 36#include "kernel-consumer.h"
3bd1e081
MD
37
38extern struct lttng_consumer_global_data consumer_data;
39extern int consumer_poll_timeout;
40extern volatile int consumer_quit;
41
42/*
43 * Mmap the ring buffer, read it and write the data to the tracefile.
44 *
45 * Returns the number of bytes written
46 */
4078b776 47ssize_t lttng_kconsumer_on_read_subbuffer_mmap(
3bd1e081
MD
48 struct lttng_consumer_local_data *ctx,
49 struct lttng_consumer_stream *stream, unsigned long len)
50{
51 unsigned long mmap_offset;
4078b776 52 ssize_t ret = 0;
3bd1e081
MD
53 off_t orig_offset = stream->out_fd_offset;
54 int fd = stream->wait_fd;
55 int outfd = stream->out_fd;
56
57 /* get the offset inside the fd to mmap */
58 ret = kernctl_get_mmap_read_offset(fd, &mmap_offset);
59 if (ret != 0) {
87dc6a9c 60 errno = -ret;
3bd1e081
MD
61 perror("kernctl_get_mmap_read_offset");
62 goto end;
63 }
64
65 while (len > 0) {
66 ret = write(outfd, stream->mmap_base + mmap_offset, len);
67 if (ret >= len) {
68 len = 0;
69 } else if (ret < 0) {
87dc6a9c 70 errno = -ret;
3bd1e081
MD
71 perror("Error in file write");
72 goto end;
73 }
74 /* This won't block, but will start writeout asynchronously */
dbb5dfe6 75 lttng_sync_file_range(outfd, stream->out_fd_offset, ret,
3bd1e081
MD
76 SYNC_FILE_RANGE_WRITE);
77 stream->out_fd_offset += ret;
78 }
79
80 lttng_consumer_sync_trace_file(stream, orig_offset);
81
82 goto end;
83
84end:
85 return ret;
86}
87
88/*
89 * Splice the data from the ring buffer to the tracefile.
90 *
91 * Returns the number of bytes spliced.
92 */
4078b776 93ssize_t lttng_kconsumer_on_read_subbuffer_splice(
3bd1e081
MD
94 struct lttng_consumer_local_data *ctx,
95 struct lttng_consumer_stream *stream, unsigned long len)
96{
4078b776 97 ssize_t ret = 0;
3bd1e081
MD
98 loff_t offset = 0;
99 off_t orig_offset = stream->out_fd_offset;
100 int fd = stream->wait_fd;
101 int outfd = stream->out_fd;
102
103 while (len > 0) {
104 DBG("splice chan to pipe offset %lu (fd : %d)",
105 (unsigned long)offset, fd);
106 ret = splice(fd, &offset, ctx->consumer_thread_pipe[1], NULL, len,
107 SPLICE_F_MOVE | SPLICE_F_MORE);
630543f2 108 DBG("splice chan to pipe ret %zd", ret);
3bd1e081 109 if (ret < 0) {
87dc6a9c 110 errno = -ret;
3bd1e081
MD
111 perror("Error in relay splice");
112 goto splice_error;
113 }
114
115 ret = splice(ctx->consumer_thread_pipe[0], NULL, outfd, NULL, ret,
116 SPLICE_F_MOVE | SPLICE_F_MORE);
630543f2 117 DBG("splice pipe to file %zd", ret);
3bd1e081 118 if (ret < 0) {
87dc6a9c 119 errno = -ret;
3bd1e081
MD
120 perror("Error in file splice");
121 goto splice_error;
122 }
123 len -= ret;
124 /* This won't block, but will start writeout asynchronously */
dbb5dfe6 125 lttng_sync_file_range(outfd, stream->out_fd_offset, ret,
3bd1e081
MD
126 SYNC_FILE_RANGE_WRITE);
127 stream->out_fd_offset += ret;
128 }
129 lttng_consumer_sync_trace_file(stream, orig_offset);
130
131 goto end;
132
133splice_error:
134 /* send the appropriate error description to sessiond */
135 switch(ret) {
136 case EBADF:
137 lttng_consumer_send_error(ctx, CONSUMERD_SPLICE_EBADF);
138 break;
139 case EINVAL:
140 lttng_consumer_send_error(ctx, CONSUMERD_SPLICE_EINVAL);
141 break;
142 case ENOMEM:
143 lttng_consumer_send_error(ctx, CONSUMERD_SPLICE_ENOMEM);
144 break;
145 case ESPIPE:
146 lttng_consumer_send_error(ctx, CONSUMERD_SPLICE_ESPIPE);
147 break;
148 }
149
150end:
151 return ret;
152}
153
154/*
155 * Take a snapshot for a specific fd
156 *
157 * Returns 0 on success, < 0 on error
158 */
159int lttng_kconsumer_take_snapshot(struct lttng_consumer_local_data *ctx,
160 struct lttng_consumer_stream *stream)
161{
162 int ret = 0;
163 int infd = stream->wait_fd;
164
165 ret = kernctl_snapshot(infd);
166 if (ret != 0) {
87dc6a9c 167 errno = -ret;
3bd1e081
MD
168 perror("Getting sub-buffer snapshot.");
169 }
170
171 return ret;
172}
173
174/*
175 * Get the produced position
176 *
177 * Returns 0 on success, < 0 on error
178 */
179int lttng_kconsumer_get_produced_snapshot(
180 struct lttng_consumer_local_data *ctx,
181 struct lttng_consumer_stream *stream,
182 unsigned long *pos)
183{
184 int ret;
185 int infd = stream->wait_fd;
186
187 ret = kernctl_snapshot_get_produced(infd, pos);
188 if (ret != 0) {
87dc6a9c 189 errno = -ret;
3bd1e081
MD
190 perror("kernctl_snapshot_get_produced");
191 }
192
193 return ret;
194}
195
196int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
197 int sock, struct pollfd *consumer_sockpoll)
198{
199 ssize_t ret;
200 struct lttcomm_consumer_msg msg;
201
202 ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
203 if (ret != sizeof(msg)) {
f2fc6720 204 lttng_consumer_send_error(ctx, CONSUMERD_ERROR_RECV_CMD);
3bd1e081
MD
205 return ret;
206 }
207 if (msg.cmd_type == LTTNG_CONSUMER_STOP) {
208 return -ENOENT;
209 }
210
211 switch (msg.cmd_type) {
212 case LTTNG_CONSUMER_ADD_CHANNEL:
213 {
214 struct lttng_consumer_channel *new_channel;
215
216 DBG("consumer_add_channel %d", msg.u.channel.channel_key);
217 new_channel = consumer_allocate_channel(msg.u.channel.channel_key,
218 -1, -1,
219 msg.u.channel.mmap_len,
220 msg.u.channel.max_sb_size);
221 if (new_channel == NULL) {
222 lttng_consumer_send_error(ctx, CONSUMERD_OUTFD_ERROR);
223 goto end_nosignal;
224 }
225 if (ctx->on_recv_channel != NULL) {
226 ret = ctx->on_recv_channel(new_channel);
227 if (ret == 0) {
228 consumer_add_channel(new_channel);
229 } else if (ret < 0) {
230 goto end_nosignal;
231 }
232 } else {
233 consumer_add_channel(new_channel);
234 }
235 goto end_nosignal;
236 }
237 case LTTNG_CONSUMER_ADD_STREAM:
238 {
239 struct lttng_consumer_stream *new_stream;
f2fc6720 240 int fd;
3bd1e081
MD
241
242 /* block */
243 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
244 return -EINTR;
245 }
f2fc6720
MD
246 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
247 if (ret != sizeof(fd)) {
3bd1e081
MD
248 lttng_consumer_send_error(ctx, CONSUMERD_ERROR_RECV_FD);
249 return ret;
250 }
3bd1e081 251
f2fc6720
MD
252 DBG("consumer_add_stream %s (%d)", msg.u.stream.path_name,
253 fd);
3bd1e081
MD
254 new_stream = consumer_allocate_stream(msg.u.stream.channel_key,
255 msg.u.stream.stream_key,
f2fc6720 256 fd, fd,
3bd1e081
MD
257 msg.u.stream.state,
258 msg.u.stream.mmap_len,
259 msg.u.stream.output,
6df2e2c9
MD
260 msg.u.stream.path_name,
261 msg.u.stream.uid,
262 msg.u.stream.gid);
3bd1e081
MD
263 if (new_stream == NULL) {
264 lttng_consumer_send_error(ctx, CONSUMERD_OUTFD_ERROR);
265 goto end;
266 }
267 if (ctx->on_recv_stream != NULL) {
268 ret = ctx->on_recv_stream(new_stream);
269 if (ret == 0) {
270 consumer_add_stream(new_stream);
271 } else if (ret < 0) {
272 goto end;
273 }
274 } else {
275 consumer_add_stream(new_stream);
276 }
277 break;
278 }
279 case LTTNG_CONSUMER_UPDATE_STREAM:
280 {
281 if (ctx->on_update_stream != NULL) {
282 ret = ctx->on_update_stream(msg.u.stream.stream_key, msg.u.stream.state);
283 if (ret == 0) {
284 consumer_change_stream_state(msg.u.stream.stream_key, msg.u.stream.state);
285 } else if (ret < 0) {
286 goto end;
287 }
288 } else {
289 consumer_change_stream_state(msg.u.stream.stream_key,
290 msg.u.stream.state);
291 }
292 break;
293 }
294 default:
295 break;
296 }
297end:
298 /* signal the poll thread */
299 ret = write(ctx->consumer_poll_pipe[1], "4", 1);
300 if (ret < 0) {
301 perror("write consumer poll");
302 }
303end_nosignal:
304 return 0;
305}
d41f73b7
MD
306
307/*
308 * Consume data on a file descriptor and write it on a trace file.
309 */
4078b776 310ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
d41f73b7
MD
311 struct lttng_consumer_local_data *ctx)
312{
313 unsigned long len;
314 int err;
4078b776 315 ssize_t ret = 0;
d41f73b7
MD
316 int infd = stream->wait_fd;
317
318 DBG("In read_subbuffer (infd : %d)", infd);
319 /* Get the next subbuffer */
320 err = kernctl_get_next_subbuf(infd);
321 if (err != 0) {
d41f73b7
MD
322 /*
323 * This is a debug message even for single-threaded consumer,
324 * because poll() have more relaxed criterions than get subbuf,
325 * so get_subbuf may fail for short race windows where poll()
326 * would issue wakeups.
327 */
328 DBG("Reserving sub buffer failed (everything is normal, "
329 "it is due to concurrency)");
330 goto end;
331 }
332
333 switch (stream->output) {
334 case LTTNG_EVENT_SPLICE:
335 /* read the whole subbuffer */
336 err = kernctl_get_padded_subbuf_size(infd, &len);
337 if (err != 0) {
87dc6a9c 338 errno = -ret;
d41f73b7
MD
339 perror("Getting sub-buffer len failed.");
340 goto end;
341 }
342
343 /* splice the subbuffer to the tracefile */
344 ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, len);
345 if (ret < 0) {
346 /*
347 * display the error but continue processing to try
348 * to release the subbuffer
349 */
350 ERR("Error splicing to tracefile");
351 }
352 break;
353 case LTTNG_EVENT_MMAP:
354 /* read the used subbuffer size */
355 err = kernctl_get_padded_subbuf_size(infd, &len);
356 if (err != 0) {
87dc6a9c 357 errno = -ret;
d41f73b7
MD
358 perror("Getting sub-buffer len failed.");
359 goto end;
360 }
361 /* write the subbuffer to the tracefile */
362 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, len);
363 if (ret < 0) {
364 /*
365 * display the error but continue processing to try
366 * to release the subbuffer
367 */
368 ERR("Error writing to tracefile");
369 }
370 break;
371 default:
372 ERR("Unknown output method");
373 ret = -1;
374 }
375
376 err = kernctl_put_next_subbuf(infd);
377 if (err != 0) {
87dc6a9c 378 errno = -ret;
d41f73b7
MD
379 if (errno == EFAULT) {
380 perror("Error in unreserving sub buffer\n");
381 } else if (errno == EIO) {
382 /* Should never happen with newer LTTng versions */
383 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
384 }
385 goto end;
386 }
387
388end:
389 return ret;
390}
391
392int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
393{
394 int ret;
395
396 /* Opening the tracefile in write mode */
397 if (stream->path_name != NULL) {
e11d277b 398 ret = run_as_open(stream->path_name,
60b6c79c
MD
399 O_WRONLY|O_CREAT|O_TRUNC,
400 S_IRWXU|S_IRWXG|S_IRWXO,
401 stream->uid, stream->gid);
d41f73b7
MD
402 if (ret < 0) {
403 ERR("Opening %s", stream->path_name);
404 perror("open");
405 goto error;
406 }
407 stream->out_fd = ret;
408 }
409
410 if (stream->output == LTTNG_EVENT_MMAP) {
411 /* get the len of the mmap region */
412 unsigned long mmap_len;
413
414 ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len);
415 if (ret != 0) {
87dc6a9c 416 errno = -ret;
d41f73b7
MD
417 perror("kernctl_get_mmap_len");
418 goto error_close_fd;
419 }
420 stream->mmap_len = (size_t) mmap_len;
421
422 stream->mmap_base = mmap(NULL, stream->mmap_len,
423 PROT_READ, MAP_PRIVATE, stream->wait_fd, 0);
424 if (stream->mmap_base == MAP_FAILED) {
425 perror("Error mmaping");
426 ret = -1;
427 goto error_close_fd;
428 }
429 }
430
431 /* we return 0 to let the library handle the FD internally */
432 return 0;
433
434error_close_fd:
435 {
436 int err;
437
438 err = close(stream->out_fd);
439 assert(!err);
440 }
441error:
442 return ret;
443}
444
This page took 0.047412 seconds and 4 git commands to generate.