Commit | Line | Data |
---|---|---|
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> | |
77c7c900 | 28 | #include <inttypes.h> |
3bd1e081 | 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> |
00e2e675 | 35 | #include <common/sessiond-comm/relayd.h> |
dbb5dfe6 | 36 | #include <common/compat/fcntl.h> |
00e2e675 | 37 | #include <common/relayd/relayd.h> |
0857097f | 38 | |
10a8a223 | 39 | #include "kernel-consumer.h" |
3bd1e081 MD |
40 | |
41 | extern struct lttng_consumer_global_data consumer_data; | |
42 | extern int consumer_poll_timeout; | |
43 | extern volatile int consumer_quit; | |
44 | ||
3bd1e081 MD |
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) { | |
87dc6a9c | 58 | errno = -ret; |
3bd1e081 MD |
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) { | |
87dc6a9c | 80 | errno = -ret; |
3bd1e081 MD |
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)) { | |
f73fabfd | 95 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD); |
3bd1e081 MD |
96 | return ret; |
97 | } | |
98 | if (msg.cmd_type == LTTNG_CONSUMER_STOP) { | |
99 | return -ENOENT; | |
100 | } | |
101 | ||
b0b335c8 MD |
102 | /* relayd needs RCU read-side protection */ |
103 | rcu_read_lock(); | |
104 | ||
3bd1e081 | 105 | switch (msg.cmd_type) { |
00e2e675 DG |
106 | case LTTNG_CONSUMER_ADD_RELAYD_SOCKET: |
107 | { | |
7735ef9e DG |
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); | |
00e2e675 DG |
111 | goto end_nosignal; |
112 | } | |
3bd1e081 MD |
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, | |
c30aaa51 MD |
121 | msg.u.channel.max_sb_size, |
122 | msg.u.channel.nb_init_streams); | |
3bd1e081 | 123 | if (new_channel == NULL) { |
f73fabfd | 124 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR); |
3bd1e081 MD |
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 | { | |
f2fc6720 | 141 | int fd; |
00e2e675 DG |
142 | struct consumer_relayd_sock_pair *relayd = NULL; |
143 | struct lttng_consumer_stream *new_stream; | |
c80048c6 | 144 | int alloc_ret = 0; |
3bd1e081 MD |
145 | |
146 | /* block */ | |
147 | if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) { | |
3f8e211f | 148 | rcu_read_unlock(); |
3bd1e081 MD |
149 | return -EINTR; |
150 | } | |
00e2e675 DG |
151 | |
152 | /* Get stream file descriptor from socket */ | |
f2fc6720 MD |
153 | ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1); |
154 | if (ret != sizeof(fd)) { | |
f73fabfd | 155 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD); |
3f8e211f | 156 | rcu_read_unlock(); |
3bd1e081 MD |
157 | return ret; |
158 | } | |
3bd1e081 | 159 | |
3bd1e081 MD |
160 | new_stream = consumer_allocate_stream(msg.u.stream.channel_key, |
161 | msg.u.stream.stream_key, | |
f2fc6720 | 162 | fd, fd, |
3bd1e081 MD |
163 | msg.u.stream.state, |
164 | msg.u.stream.mmap_len, | |
165 | msg.u.stream.output, | |
6df2e2c9 MD |
166 | msg.u.stream.path_name, |
167 | msg.u.stream.uid, | |
00e2e675 DG |
168 | msg.u.stream.gid, |
169 | msg.u.stream.net_index, | |
c80048c6 MD |
170 | msg.u.stream.metadata_flag, |
171 | &alloc_ret); | |
3bd1e081 | 172 | if (new_stream == NULL) { |
c80048c6 MD |
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 | } | |
3f8e211f | 187 | goto end_nosignal; |
3bd1e081 | 188 | } |
00e2e675 | 189 | |
fb3a43a9 DG |
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 | ||
00e2e675 DG |
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) { | |
3f8e211f | 209 | goto end_nosignal; |
00e2e675 DG |
210 | } |
211 | } else if (msg.u.stream.net_index != -1) { | |
212 | ERR("Network sequence index %d unknown. Not adding stream.", | |
213 | msg.u.stream.net_index); | |
214 | free(new_stream); | |
3f8e211f | 215 | goto end_nosignal; |
00e2e675 DG |
216 | } |
217 | ||
fb3a43a9 DG |
218 | /* Send stream to the metadata thread */ |
219 | if (new_stream->metadata_flag) { | |
220 | if (ctx->on_recv_stream) { | |
221 | ret = ctx->on_recv_stream(new_stream); | |
222 | if (ret < 0) { | |
223 | goto end_nosignal; | |
224 | } | |
225 | } | |
226 | ||
227 | do { | |
228 | ret = write(ctx->consumer_metadata_pipe[1], new_stream, | |
229 | sizeof(struct lttng_consumer_stream)); | |
230 | } while (ret < 0 && errno == EINTR); | |
231 | if (ret < 0) { | |
232 | PERROR("write metadata pipe"); | |
3bd1e081 MD |
233 | } |
234 | } else { | |
fb3a43a9 DG |
235 | if (ctx->on_recv_stream) { |
236 | ret = ctx->on_recv_stream(new_stream); | |
237 | if (ret < 0) { | |
238 | goto end_nosignal; | |
239 | } | |
240 | } | |
3bd1e081 MD |
241 | consumer_add_stream(new_stream); |
242 | } | |
00e2e675 DG |
243 | |
244 | DBG("Kernel consumer_add_stream (%d)", fd); | |
3bd1e081 MD |
245 | break; |
246 | } | |
247 | case LTTNG_CONSUMER_UPDATE_STREAM: | |
248 | { | |
3f8e211f DG |
249 | rcu_read_unlock(); |
250 | return -ENOSYS; | |
251 | } | |
252 | case LTTNG_CONSUMER_DESTROY_RELAYD: | |
253 | { | |
a6ba4fe1 | 254 | uint64_t index = msg.u.destroy_relayd.net_seq_idx; |
3f8e211f DG |
255 | struct consumer_relayd_sock_pair *relayd; |
256 | ||
a6ba4fe1 | 257 | DBG("Kernel consumer destroying relayd %" PRIu64, index); |
3f8e211f DG |
258 | |
259 | /* Get relayd reference if exists. */ | |
a6ba4fe1 | 260 | relayd = consumer_find_relayd(index); |
3f8e211f | 261 | if (relayd == NULL) { |
a6ba4fe1 | 262 | ERR("Unable to find relayd %" PRIu64, index); |
3f8e211f | 263 | goto end_nosignal; |
3bd1e081 | 264 | } |
3f8e211f | 265 | |
a6ba4fe1 DG |
266 | /* |
267 | * Each relayd socket pair has a refcount of stream attached to it | |
268 | * which tells if the relayd is still active or not depending on the | |
269 | * refcount value. | |
270 | * | |
271 | * This will set the destroy flag of the relayd object and destroy it | |
272 | * if the refcount reaches zero when called. | |
273 | * | |
274 | * The destroy can happen either here or when a stream fd hangs up. | |
275 | */ | |
276 | consumer_flag_relayd_for_destroy(relayd); | |
3f8e211f | 277 | |
3f8e211f | 278 | goto end_nosignal; |
3bd1e081 MD |
279 | } |
280 | default: | |
3f8e211f | 281 | goto end_nosignal; |
3bd1e081 | 282 | } |
3f8e211f | 283 | |
04fdd819 | 284 | /* |
3f8e211f DG |
285 | * Wake-up the other end by writing a null byte in the pipe (non-blocking). |
286 | * Important note: Because writing into the pipe is non-blocking (and | |
287 | * therefore we allow dropping wakeup data, as long as there is wakeup data | |
288 | * present in the pipe buffer to wake up the other end), the other end | |
289 | * should perform the following sequence for waiting: | |
290 | * | |
04fdd819 MD |
291 | * 1) empty the pipe (reads). |
292 | * 2) perform update operation. | |
293 | * 3) wait on the pipe (poll). | |
294 | */ | |
295 | do { | |
296 | ret = write(ctx->consumer_poll_pipe[1], "", 1); | |
6f94560a | 297 | } while (ret < 0 && errno == EINTR); |
3bd1e081 | 298 | end_nosignal: |
b0b335c8 | 299 | rcu_read_unlock(); |
4cbc1a04 DG |
300 | |
301 | /* | |
302 | * Return 1 to indicate success since the 0 value can be a socket | |
303 | * shutdown during the recv() or send() call. | |
304 | */ | |
305 | return 1; | |
3bd1e081 | 306 | } |
d41f73b7 MD |
307 | |
308 | /* | |
309 | * Consume data on a file descriptor and write it on a trace file. | |
310 | */ | |
4078b776 | 311 | ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream, |
d41f73b7 MD |
312 | struct lttng_consumer_local_data *ctx) |
313 | { | |
1d4dfdef | 314 | unsigned long len, subbuf_size, padding; |
d41f73b7 | 315 | int err; |
4078b776 | 316 | ssize_t ret = 0; |
d41f73b7 MD |
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) { | |
1d4dfdef | 323 | ret = err; |
d41f73b7 MD |
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 | ||
1d4dfdef DG |
335 | /* Get the full subbuffer size including padding */ |
336 | err = kernctl_get_padded_subbuf_size(infd, &len); | |
337 | if (err != 0) { | |
338 | errno = -err; | |
339 | perror("Getting sub-buffer len failed."); | |
340 | ret = err; | |
341 | goto end; | |
342 | } | |
343 | ||
d41f73b7 | 344 | switch (stream->output) { |
1d4dfdef | 345 | case LTTNG_EVENT_SPLICE: |
d41f73b7 | 346 | |
1d4dfdef DG |
347 | /* |
348 | * XXX: The lttng-modules splice "actor" does not handle copying | |
349 | * partial pages hence only using the subbuffer size without the | |
350 | * padding makes the splice fail. | |
351 | */ | |
352 | subbuf_size = len; | |
353 | padding = 0; | |
354 | ||
355 | /* splice the subbuffer to the tracefile */ | |
91dfef6e DG |
356 | ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, subbuf_size, |
357 | padding); | |
358 | /* | |
359 | * XXX: Splice does not support network streaming so the return value | |
360 | * is simply checked against subbuf_size and not like the mmap() op. | |
361 | */ | |
1d4dfdef DG |
362 | if (ret != subbuf_size) { |
363 | /* | |
364 | * display the error but continue processing to try | |
365 | * to release the subbuffer | |
366 | */ | |
367 | ERR("Error splicing to tracefile (ret: %zd != len: %lu)", | |
368 | ret, subbuf_size); | |
369 | } | |
370 | break; | |
371 | case LTTNG_EVENT_MMAP: | |
372 | /* Get subbuffer size without padding */ | |
373 | err = kernctl_get_subbuf_size(infd, &subbuf_size); | |
374 | if (err != 0) { | |
375 | errno = -err; | |
376 | perror("Getting sub-buffer len failed."); | |
377 | ret = err; | |
378 | goto end; | |
379 | } | |
47e81c02 | 380 | |
1d4dfdef DG |
381 | /* Make sure the tracer is not gone mad on us! */ |
382 | assert(len >= subbuf_size); | |
383 | ||
384 | padding = len - subbuf_size; | |
385 | ||
386 | /* write the subbuffer to the tracefile */ | |
91dfef6e DG |
387 | ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size, |
388 | padding); | |
389 | /* | |
390 | * The mmap operation should write subbuf_size amount of data when | |
391 | * network streaming or the full padding (len) size when we are _not_ | |
392 | * streaming. | |
393 | */ | |
394 | if ((ret != subbuf_size && stream->net_seq_idx != -1) || | |
395 | (ret != len && stream->net_seq_idx == -1)) { | |
1d4dfdef | 396 | /* |
91dfef6e DG |
397 | * Display the error but continue processing to try to release the |
398 | * subbuffer | |
1d4dfdef | 399 | */ |
91dfef6e DG |
400 | ERR("Error writing to tracefile " |
401 | "(ret: %zd != len: %lu != subbuf_size: %lu)", | |
402 | ret, len, subbuf_size); | |
1d4dfdef DG |
403 | } |
404 | break; | |
405 | default: | |
406 | ERR("Unknown output method"); | |
407 | ret = -1; | |
d41f73b7 MD |
408 | } |
409 | ||
410 | err = kernctl_put_next_subbuf(infd); | |
411 | if (err != 0) { | |
21073eaa | 412 | errno = -err; |
d41f73b7 MD |
413 | if (errno == EFAULT) { |
414 | perror("Error in unreserving sub buffer\n"); | |
415 | } else if (errno == EIO) { | |
416 | /* Should never happen with newer LTTng versions */ | |
417 | perror("Reader has been pushed by the writer, last sub-buffer corrupted."); | |
418 | } | |
21073eaa DG |
419 | |
420 | ret = -err; | |
d41f73b7 MD |
421 | goto end; |
422 | } | |
423 | ||
424 | end: | |
425 | return ret; | |
426 | } | |
427 | ||
428 | int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream) | |
429 | { | |
430 | int ret; | |
431 | ||
432 | /* Opening the tracefile in write mode */ | |
00e2e675 | 433 | if (strlen(stream->path_name) > 0 && stream->net_seq_idx == -1) { |
e11d277b | 434 | ret = run_as_open(stream->path_name, |
60b6c79c MD |
435 | O_WRONLY|O_CREAT|O_TRUNC, |
436 | S_IRWXU|S_IRWXG|S_IRWXO, | |
437 | stream->uid, stream->gid); | |
d41f73b7 MD |
438 | if (ret < 0) { |
439 | ERR("Opening %s", stream->path_name); | |
440 | perror("open"); | |
441 | goto error; | |
442 | } | |
443 | stream->out_fd = ret; | |
444 | } | |
445 | ||
446 | if (stream->output == LTTNG_EVENT_MMAP) { | |
447 | /* get the len of the mmap region */ | |
448 | unsigned long mmap_len; | |
449 | ||
450 | ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len); | |
451 | if (ret != 0) { | |
87dc6a9c | 452 | errno = -ret; |
d41f73b7 MD |
453 | perror("kernctl_get_mmap_len"); |
454 | goto error_close_fd; | |
455 | } | |
456 | stream->mmap_len = (size_t) mmap_len; | |
457 | ||
458 | stream->mmap_base = mmap(NULL, stream->mmap_len, | |
459 | PROT_READ, MAP_PRIVATE, stream->wait_fd, 0); | |
460 | if (stream->mmap_base == MAP_FAILED) { | |
461 | perror("Error mmaping"); | |
462 | ret = -1; | |
463 | goto error_close_fd; | |
464 | } | |
465 | } | |
466 | ||
467 | /* we return 0 to let the library handle the FD internally */ | |
468 | return 0; | |
469 | ||
470 | error_close_fd: | |
471 | { | |
472 | int err; | |
473 | ||
474 | err = close(stream->out_fd); | |
475 | assert(!err); | |
476 | } | |
477 | error: | |
478 | return ret; | |
479 | } | |
480 |