Move runas.h to common/ directory
[lttng-tools.git] / liblttng-kconsumer / lttng-kconsumer.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
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>
22 #include <fcntl.h>
23 #include <poll.h>
24 #include <pthread.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/mman.h>
28 #include <sys/socket.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31
32 #include <lttng-kernel-ctl.h>
33 #include <lttng-sessiond-comm.h>
34 #include <lttng/lttng-kconsumer.h>
35 #include <lttngerr.h>
36
37 #include "common/runas.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
47 */
48 int 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) {
61 ret = -errno;
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) {
71 ret = -errno;
72 perror("Error in file write");
73 goto end;
74 }
75 /* This won't block, but will start writeout asynchronously */
76 sync_file_range(outfd, stream->out_fd_offset, ret,
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
85 end:
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 */
94 int 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) {
111 ret = errno;
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) {
120 ret = errno;
121 perror("Error in file splice");
122 goto splice_error;
123 }
124 len -= ret;
125 /* This won't block, but will start writeout asynchronously */
126 sync_file_range(outfd, stream->out_fd_offset, ret,
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
134 splice_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
151 end:
152 return ret;
153 }
154
155 /*
156 * Take a snapshot for a specific fd
157 *
158 * Returns 0 on success, < 0 on error
159 */
160 int 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) {
168 ret = errno;
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 */
180 int 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) {
190 ret = errno;
191 perror("kernctl_snapshot_get_produced");
192 }
193
194 return ret;
195 }
196
197 int 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)) {
205 lttng_consumer_send_error(ctx, CONSUMERD_ERROR_RECV_CMD);
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;
241 int fd;
242
243 /* block */
244 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
245 return -EINTR;
246 }
247 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
248 if (ret != sizeof(fd)) {
249 lttng_consumer_send_error(ctx, CONSUMERD_ERROR_RECV_FD);
250 return ret;
251 }
252
253 DBG("consumer_add_stream %s (%d)", msg.u.stream.path_name,
254 fd);
255 new_stream = consumer_allocate_stream(msg.u.stream.channel_key,
256 msg.u.stream.stream_key,
257 fd, fd,
258 msg.u.stream.state,
259 msg.u.stream.mmap_len,
260 msg.u.stream.output,
261 msg.u.stream.path_name,
262 msg.u.stream.uid,
263 msg.u.stream.gid);
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 }
298 end:
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 }
304 end_nosignal:
305 return 0;
306 }
307
308 /*
309 * Consume data on a file descriptor and write it on a trace file.
310 */
311 int 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) {
323 ret = errno;
324 /*
325 * This is a debug message even for single-threaded consumer,
326 * because poll() have more relaxed criterions than get subbuf,
327 * so get_subbuf may fail for short race windows where poll()
328 * would issue wakeups.
329 */
330 DBG("Reserving sub buffer failed (everything is normal, "
331 "it is due to concurrency)");
332 goto end;
333 }
334
335 switch (stream->output) {
336 case LTTNG_EVENT_SPLICE:
337 /* read the whole subbuffer */
338 err = kernctl_get_padded_subbuf_size(infd, &len);
339 if (err != 0) {
340 ret = errno;
341 perror("Getting sub-buffer len failed.");
342 goto end;
343 }
344
345 /* splice the subbuffer to the tracefile */
346 ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, len);
347 if (ret < 0) {
348 /*
349 * display the error but continue processing to try
350 * to release the subbuffer
351 */
352 ERR("Error splicing to tracefile");
353 }
354 break;
355 case LTTNG_EVENT_MMAP:
356 /* read the used subbuffer size */
357 err = kernctl_get_padded_subbuf_size(infd, &len);
358 if (err != 0) {
359 ret = errno;
360 perror("Getting sub-buffer len failed.");
361 goto end;
362 }
363 /* write the subbuffer to the tracefile */
364 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, len);
365 if (ret < 0) {
366 /*
367 * display the error but continue processing to try
368 * to release the subbuffer
369 */
370 ERR("Error writing to tracefile");
371 }
372 break;
373 default:
374 ERR("Unknown output method");
375 ret = -1;
376 }
377
378 err = kernctl_put_next_subbuf(infd);
379 if (err != 0) {
380 ret = errno;
381 if (errno == EFAULT) {
382 perror("Error in unreserving sub buffer\n");
383 } else if (errno == EIO) {
384 /* Should never happen with newer LTTng versions */
385 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
386 }
387 goto end;
388 }
389
390 end:
391 return ret;
392 }
393
394 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
395 {
396 int ret;
397
398 /* Opening the tracefile in write mode */
399 if (stream->path_name != NULL) {
400 ret = open_run_as(stream->path_name,
401 O_WRONLY|O_CREAT|O_TRUNC,
402 S_IRWXU|S_IRWXG|S_IRWXO,
403 stream->uid, stream->gid);
404 if (ret < 0) {
405 ERR("Opening %s", stream->path_name);
406 perror("open");
407 goto error;
408 }
409 stream->out_fd = ret;
410 }
411
412 if (stream->output == LTTNG_EVENT_MMAP) {
413 /* get the len of the mmap region */
414 unsigned long mmap_len;
415
416 ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len);
417 if (ret != 0) {
418 ret = errno;
419 perror("kernctl_get_mmap_len");
420 goto error_close_fd;
421 }
422 stream->mmap_len = (size_t) mmap_len;
423
424 stream->mmap_base = mmap(NULL, stream->mmap_len,
425 PROT_READ, MAP_PRIVATE, stream->wait_fd, 0);
426 if (stream->mmap_base == MAP_FAILED) {
427 perror("Error mmaping");
428 ret = -1;
429 goto error_close_fd;
430 }
431 }
432
433 /* we return 0 to let the library handle the FD internally */
434 return 0;
435
436 error_close_fd:
437 {
438 int err;
439
440 err = close(stream->out_fd);
441 assert(!err);
442 }
443 error:
444 return ret;
445 }
446
This page took 0.037725 seconds and 4 git commands to generate.