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