1d725c2318b74029feb878289cfdfde55fa082fd
[lttng-tools.git] / src / common / kernel-consumer / kernel-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/types.h>
28 #include <inttypes.h>
29 #include <unistd.h>
30 #include <sys/stat.h>
31
32 #include <common/common.h>
33 #include <common/kernel-ctl/kernel-ctl.h>
34 #include <common/sessiond-comm/sessiond-comm.h>
35 #include <common/sessiond-comm/relayd.h>
36 #include <common/compat/fcntl.h>
37 #include <common/relayd/relayd.h>
38
39 #include "kernel-consumer.h"
40
41 extern struct lttng_consumer_global_data consumer_data;
42 extern int consumer_poll_timeout;
43 extern volatile int consumer_quit;
44
45 /*
46 * Take a snapshot for a specific fd
47 *
48 * Returns 0 on success, < 0 on error
49 */
50 int lttng_kconsumer_take_snapshot(struct lttng_consumer_local_data *ctx,
51 struct lttng_consumer_stream *stream)
52 {
53 int ret = 0;
54 int infd = stream->wait_fd;
55
56 ret = kernctl_snapshot(infd);
57 if (ret != 0) {
58 errno = -ret;
59 perror("Getting sub-buffer snapshot.");
60 }
61
62 return ret;
63 }
64
65 /*
66 * Get the produced position
67 *
68 * Returns 0 on success, < 0 on error
69 */
70 int lttng_kconsumer_get_produced_snapshot(
71 struct lttng_consumer_local_data *ctx,
72 struct lttng_consumer_stream *stream,
73 unsigned long *pos)
74 {
75 int ret;
76 int infd = stream->wait_fd;
77
78 ret = kernctl_snapshot_get_produced(infd, pos);
79 if (ret != 0) {
80 errno = -ret;
81 perror("kernctl_snapshot_get_produced");
82 }
83
84 return ret;
85 }
86
87 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
88 int sock, struct pollfd *consumer_sockpoll)
89 {
90 ssize_t ret;
91 struct lttcomm_consumer_msg msg;
92
93 ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
94 if (ret != sizeof(msg)) {
95 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
96 return ret;
97 }
98 if (msg.cmd_type == LTTNG_CONSUMER_STOP) {
99 return -ENOENT;
100 }
101
102 /* relayd needs RCU read-side protection */
103 rcu_read_lock();
104
105 switch (msg.cmd_type) {
106 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
107 {
108 ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
109 msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll,
110 &msg.u.relayd_sock.sock);
111 goto end_nosignal;
112 }
113 case LTTNG_CONSUMER_ADD_CHANNEL:
114 {
115 struct lttng_consumer_channel *new_channel;
116
117 DBG("consumer_add_channel %d", msg.u.channel.channel_key);
118 new_channel = consumer_allocate_channel(msg.u.channel.channel_key,
119 -1, -1,
120 msg.u.channel.mmap_len,
121 msg.u.channel.max_sb_size,
122 msg.u.channel.nb_init_streams);
123 if (new_channel == NULL) {
124 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
125 goto end_nosignal;
126 }
127 if (ctx->on_recv_channel != NULL) {
128 ret = ctx->on_recv_channel(new_channel);
129 if (ret == 0) {
130 consumer_add_channel(new_channel);
131 } else if (ret < 0) {
132 goto end_nosignal;
133 }
134 } else {
135 consumer_add_channel(new_channel);
136 }
137 goto end_nosignal;
138 }
139 case LTTNG_CONSUMER_ADD_STREAM:
140 {
141 int fd;
142 struct consumer_relayd_sock_pair *relayd = NULL;
143 struct lttng_consumer_stream *new_stream;
144 int alloc_ret = 0;
145
146 /* block */
147 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
148 rcu_read_unlock();
149 return -EINTR;
150 }
151
152 /* Get stream file descriptor from socket */
153 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
154 if (ret != sizeof(fd)) {
155 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
156 rcu_read_unlock();
157 return ret;
158 }
159
160 new_stream = consumer_allocate_stream(msg.u.stream.channel_key,
161 msg.u.stream.stream_key,
162 fd, fd,
163 msg.u.stream.state,
164 msg.u.stream.mmap_len,
165 msg.u.stream.output,
166 msg.u.stream.path_name,
167 msg.u.stream.uid,
168 msg.u.stream.gid,
169 msg.u.stream.net_index,
170 msg.u.stream.metadata_flag,
171 &alloc_ret);
172 if (new_stream == NULL) {
173 switch (alloc_ret) {
174 case -ENOMEM:
175 case -EINVAL:
176 default:
177 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
178 break;
179 case -ENOENT:
180 /*
181 * We could not find the channel. Can happen if cpu hotplug
182 * happens while tearing down.
183 */
184 DBG3("Could not find channel");
185 break;
186 }
187 goto end_nosignal;
188 }
189
190 /*
191 * The buffer flush is done on the session daemon side for the kernel
192 * so no need for the stream "hangup_flush_done" variable to be
193 * tracked. This is important for a kernel stream since we don't rely
194 * on the flush state of the stream to read data. It's not the case for
195 * user space tracing.
196 */
197 new_stream->hangup_flush_done = 0;
198
199 /* The stream is not metadata. Get relayd reference if exists. */
200 relayd = consumer_find_relayd(msg.u.stream.net_index);
201 if (relayd != NULL) {
202 /* Add stream on the relayd */
203 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
204 ret = relayd_add_stream(&relayd->control_sock,
205 msg.u.stream.name, msg.u.stream.path_name,
206 &new_stream->relayd_stream_id);
207 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
208 if (ret < 0) {
209 consumer_del_stream(new_stream, NULL);
210 goto end_nosignal;
211 }
212 } else if (msg.u.stream.net_index != -1) {
213 ERR("Network sequence index %d unknown. Not adding stream.",
214 msg.u.stream.net_index);
215 consumer_del_stream(new_stream, NULL);
216 goto end_nosignal;
217 }
218
219 if (ctx->on_recv_stream) {
220 ret = ctx->on_recv_stream(new_stream);
221 if (ret < 0) {
222 consumer_del_stream(new_stream, NULL);
223 goto end_nosignal;
224 }
225 }
226
227 /* Send stream to the metadata thread */
228 if (new_stream->metadata_flag) {
229 do {
230 ret = write(ctx->consumer_metadata_pipe[1], &new_stream,
231 sizeof(new_stream));
232 } while (ret < 0 && errno == EINTR);
233 if (ret < 0) {
234 PERROR("write metadata pipe");
235 consumer_del_stream(new_stream, NULL);
236 goto end_nosignal;
237 }
238 } else {
239 do {
240 ret = write(ctx->consumer_poll_pipe[1], &new_stream,
241 sizeof(new_stream));
242 } while (ret < 0 && errno == EINTR);
243 if (ret < 0) {
244 PERROR("write data pipe");
245 consumer_del_stream(new_stream, NULL);
246 goto end_nosignal;
247 }
248 }
249
250 DBG("Kernel consumer_add_stream (%d)", fd);
251 break;
252 }
253 case LTTNG_CONSUMER_UPDATE_STREAM:
254 {
255 rcu_read_unlock();
256 return -ENOSYS;
257 }
258 case LTTNG_CONSUMER_DESTROY_RELAYD:
259 {
260 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
261 struct consumer_relayd_sock_pair *relayd;
262
263 DBG("Kernel consumer destroying relayd %" PRIu64, index);
264
265 /* Get relayd reference if exists. */
266 relayd = consumer_find_relayd(index);
267 if (relayd == NULL) {
268 ERR("Unable to find relayd %" PRIu64, index);
269 goto end_nosignal;
270 }
271
272 /*
273 * Each relayd socket pair has a refcount of stream attached to it
274 * which tells if the relayd is still active or not depending on the
275 * refcount value.
276 *
277 * This will set the destroy flag of the relayd object and destroy it
278 * if the refcount reaches zero when called.
279 *
280 * The destroy can happen either here or when a stream fd hangs up.
281 */
282 consumer_flag_relayd_for_destroy(relayd);
283
284 goto end_nosignal;
285 }
286 default:
287 goto end_nosignal;
288 }
289
290 end_nosignal:
291 rcu_read_unlock();
292
293 /*
294 * Return 1 to indicate success since the 0 value can be a socket
295 * shutdown during the recv() or send() call.
296 */
297 return 1;
298 }
299
300 /*
301 * Consume data on a file descriptor and write it on a trace file.
302 */
303 ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
304 struct lttng_consumer_local_data *ctx)
305 {
306 unsigned long len, subbuf_size, padding;
307 int err;
308 ssize_t ret = 0;
309 int infd = stream->wait_fd;
310
311 DBG("In read_subbuffer (infd : %d)", infd);
312 /* Get the next subbuffer */
313 err = kernctl_get_next_subbuf(infd);
314 if (err != 0) {
315 ret = err;
316 /*
317 * This is a debug message even for single-threaded consumer,
318 * because poll() have more relaxed criterions than get subbuf,
319 * so get_subbuf may fail for short race windows where poll()
320 * would issue wakeups.
321 */
322 DBG("Reserving sub buffer failed (everything is normal, "
323 "it is due to concurrency)");
324 goto end;
325 }
326
327 /* Get the full subbuffer size including padding */
328 err = kernctl_get_padded_subbuf_size(infd, &len);
329 if (err != 0) {
330 errno = -err;
331 perror("Getting sub-buffer len failed.");
332 ret = err;
333 goto end;
334 }
335
336 switch (stream->output) {
337 case LTTNG_EVENT_SPLICE:
338
339 /*
340 * XXX: The lttng-modules splice "actor" does not handle copying
341 * partial pages hence only using the subbuffer size without the
342 * padding makes the splice fail.
343 */
344 subbuf_size = len;
345 padding = 0;
346
347 /* splice the subbuffer to the tracefile */
348 ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, subbuf_size,
349 padding);
350 /*
351 * XXX: Splice does not support network streaming so the return value
352 * is simply checked against subbuf_size and not like the mmap() op.
353 */
354 if (ret != subbuf_size) {
355 /*
356 * display the error but continue processing to try
357 * to release the subbuffer
358 */
359 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
360 ret, subbuf_size);
361 }
362 break;
363 case LTTNG_EVENT_MMAP:
364 /* Get subbuffer size without padding */
365 err = kernctl_get_subbuf_size(infd, &subbuf_size);
366 if (err != 0) {
367 errno = -err;
368 perror("Getting sub-buffer len failed.");
369 ret = err;
370 goto end;
371 }
372
373 /* Make sure the tracer is not gone mad on us! */
374 assert(len >= subbuf_size);
375
376 padding = len - subbuf_size;
377
378 /* write the subbuffer to the tracefile */
379 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size,
380 padding);
381 /*
382 * The mmap operation should write subbuf_size amount of data when
383 * network streaming or the full padding (len) size when we are _not_
384 * streaming.
385 */
386 if ((ret != subbuf_size && stream->net_seq_idx != -1) ||
387 (ret != len && stream->net_seq_idx == -1)) {
388 /*
389 * Display the error but continue processing to try to release the
390 * subbuffer
391 */
392 ERR("Error writing to tracefile "
393 "(ret: %zd != len: %lu != subbuf_size: %lu)",
394 ret, len, subbuf_size);
395 }
396 break;
397 default:
398 ERR("Unknown output method");
399 ret = -1;
400 }
401
402 err = kernctl_put_next_subbuf(infd);
403 if (err != 0) {
404 errno = -err;
405 if (errno == EFAULT) {
406 perror("Error in unreserving sub buffer\n");
407 } else if (errno == EIO) {
408 /* Should never happen with newer LTTng versions */
409 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
410 }
411
412 ret = -err;
413 goto end;
414 }
415
416 end:
417 return ret;
418 }
419
420 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
421 {
422 int ret;
423
424 /* Opening the tracefile in write mode */
425 if (strlen(stream->path_name) > 0 && stream->net_seq_idx == -1) {
426 ret = run_as_open(stream->path_name,
427 O_WRONLY|O_CREAT|O_TRUNC,
428 S_IRWXU|S_IRWXG|S_IRWXO,
429 stream->uid, stream->gid);
430 if (ret < 0) {
431 ERR("Opening %s", stream->path_name);
432 perror("open");
433 goto error;
434 }
435 stream->out_fd = ret;
436 }
437
438 if (stream->output == LTTNG_EVENT_MMAP) {
439 /* get the len of the mmap region */
440 unsigned long mmap_len;
441
442 ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len);
443 if (ret != 0) {
444 errno = -ret;
445 perror("kernctl_get_mmap_len");
446 goto error_close_fd;
447 }
448 stream->mmap_len = (size_t) mmap_len;
449
450 stream->mmap_base = mmap(NULL, stream->mmap_len,
451 PROT_READ, MAP_PRIVATE, stream->wait_fd, 0);
452 if (stream->mmap_base == MAP_FAILED) {
453 perror("Error mmaping");
454 ret = -1;
455 goto error_close_fd;
456 }
457 }
458
459 /* we return 0 to let the library handle the FD internally */
460 return 0;
461
462 error_close_fd:
463 {
464 int err;
465
466 err = close(stream->out_fd);
467 assert(!err);
468 }
469 error:
470 return ret;
471 }
472
This page took 0.039763 seconds and 4 git commands to generate.