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> |
acdb9057 | 37 | #include <common/pipe.h> |
00e2e675 | 38 | #include <common/relayd/relayd.h> |
fe4477ee | 39 | #include <common/utils.h> |
07b86b52 | 40 | #include <common/consumer-stream.h> |
0857097f | 41 | |
10a8a223 | 42 | #include "kernel-consumer.h" |
3bd1e081 MD |
43 | |
44 | extern struct lttng_consumer_global_data consumer_data; | |
45 | extern int consumer_poll_timeout; | |
46 | extern volatile int consumer_quit; | |
47 | ||
3bd1e081 MD |
48 | /* |
49 | * Take a snapshot for a specific fd | |
50 | * | |
51 | * Returns 0 on success, < 0 on error | |
52 | */ | |
ffe60014 | 53 | int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream *stream) |
3bd1e081 MD |
54 | { |
55 | int ret = 0; | |
56 | int infd = stream->wait_fd; | |
57 | ||
58 | ret = kernctl_snapshot(infd); | |
59 | if (ret != 0) { | |
87dc6a9c | 60 | errno = -ret; |
3bd1e081 MD |
61 | perror("Getting sub-buffer snapshot."); |
62 | } | |
63 | ||
64 | return ret; | |
65 | } | |
66 | ||
67 | /* | |
68 | * Get the produced position | |
69 | * | |
70 | * Returns 0 on success, < 0 on error | |
71 | */ | |
ffe60014 | 72 | int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream *stream, |
3bd1e081 MD |
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 | ||
07b86b52 JD |
87 | /* |
88 | * Get the consumerd position | |
89 | * | |
90 | * Returns 0 on success, < 0 on error | |
91 | */ | |
92 | int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream *stream, | |
93 | unsigned long *pos) | |
94 | { | |
95 | int ret; | |
96 | int infd = stream->wait_fd; | |
97 | ||
98 | ret = kernctl_snapshot_get_consumed(infd, pos); | |
99 | if (ret != 0) { | |
100 | errno = -ret; | |
101 | perror("kernctl_snapshot_get_consumed"); | |
102 | } | |
103 | ||
104 | return ret; | |
105 | } | |
106 | ||
07b86b52 JD |
107 | /* |
108 | * Take a snapshot of all the stream of a channel | |
109 | * | |
110 | * Returns 0 on success, < 0 on error | |
111 | */ | |
112 | int lttng_kconsumer_snapshot_channel(uint64_t key, char *path, | |
5c786ded JD |
113 | uint64_t relayd_id, uint64_t max_stream_size, |
114 | struct lttng_consumer_local_data *ctx) | |
07b86b52 JD |
115 | { |
116 | int ret; | |
117 | unsigned long consumed_pos, produced_pos; | |
118 | struct lttng_consumer_channel *channel; | |
119 | struct lttng_consumer_stream *stream; | |
120 | ||
121 | DBG("Kernel consumer snapshot channel %lu", key); | |
122 | ||
123 | rcu_read_lock(); | |
124 | ||
125 | channel = consumer_find_channel(key); | |
126 | if (!channel) { | |
127 | ERR("No channel found for key %lu", key); | |
128 | ret = -1; | |
129 | goto end; | |
130 | } | |
131 | ||
132 | /* Splice is not supported yet for channel snapshot. */ | |
133 | if (channel->output != CONSUMER_CHANNEL_MMAP) { | |
134 | ERR("Unsupported output %d", channel->output); | |
135 | ret = -1; | |
136 | goto end; | |
137 | } | |
138 | ||
10a50311 | 139 | cds_list_for_each_entry(stream, &channel->streams.head, send_node) { |
07b86b52 JD |
140 | /* |
141 | * Lock stream because we are about to change its state. | |
142 | */ | |
143 | pthread_mutex_lock(&stream->lock); | |
144 | ||
29decac3 DG |
145 | /* |
146 | * Assign the received relayd ID so we can use it for streaming. The streams | |
147 | * are not visible to anyone so this is OK to change it. | |
148 | */ | |
07b86b52 JD |
149 | stream->net_seq_idx = relayd_id; |
150 | channel->relayd_id = relayd_id; | |
151 | if (relayd_id != (uint64_t) -1ULL) { | |
10a50311 | 152 | ret = consumer_send_relayd_stream(stream, path); |
07b86b52 JD |
153 | if (ret < 0) { |
154 | ERR("sending stream to relayd"); | |
155 | goto end_unlock; | |
156 | } | |
07b86b52 JD |
157 | } else { |
158 | ret = utils_create_stream_file(path, stream->name, | |
10a50311 JD |
159 | stream->chan->tracefile_size, |
160 | stream->tracefile_count_current, | |
07b86b52 JD |
161 | stream->uid, stream->gid); |
162 | if (ret < 0) { | |
163 | ERR("utils_create_stream_file"); | |
164 | goto end_unlock; | |
165 | } | |
166 | ||
167 | stream->out_fd = ret; | |
168 | stream->tracefile_size_current = 0; | |
169 | ||
170 | DBG("Kernel consumer snapshot stream %s/%s (%lu)", path, | |
171 | stream->name, stream->key); | |
172 | } | |
173 | ||
174 | ret = kernctl_buffer_flush(stream->wait_fd); | |
175 | if (ret < 0) { | |
10a50311 | 176 | ERR("Failed to flush kernel stream"); |
07b86b52 JD |
177 | goto end_unlock; |
178 | } | |
179 | ||
180 | ret = lttng_kconsumer_take_snapshot(stream); | |
181 | if (ret < 0) { | |
182 | ERR("Taking kernel snapshot"); | |
183 | goto end_unlock; | |
184 | } | |
185 | ||
186 | ret = lttng_kconsumer_get_produced_snapshot(stream, &produced_pos); | |
187 | if (ret < 0) { | |
188 | ERR("Produced kernel snapshot position"); | |
189 | goto end_unlock; | |
190 | } | |
191 | ||
192 | ret = lttng_kconsumer_get_consumed_snapshot(stream, &consumed_pos); | |
193 | if (ret < 0) { | |
194 | ERR("Consumerd kernel snapshot position"); | |
195 | goto end_unlock; | |
196 | } | |
197 | ||
198 | if (stream->max_sb_size == 0) { | |
199 | ret = kernctl_get_max_subbuf_size(stream->wait_fd, | |
200 | &stream->max_sb_size); | |
201 | if (ret < 0) { | |
202 | ERR("Getting kernel max_sb_size"); | |
203 | goto end_unlock; | |
204 | } | |
205 | } | |
206 | ||
5c786ded JD |
207 | /* |
208 | * The original value is sent back if max stream size is larger than | |
209 | * the possible size of the snapshot. Also, we asume that the session | |
210 | * daemon should never send a maximum stream size that is lower than | |
211 | * subbuffer size. | |
212 | */ | |
213 | consumed_pos = consumer_get_consumed_maxsize(consumed_pos, | |
214 | produced_pos, max_stream_size); | |
215 | ||
07b86b52 JD |
216 | while (consumed_pos < produced_pos) { |
217 | ssize_t read_len; | |
218 | unsigned long len, padded_len; | |
219 | ||
220 | DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos); | |
221 | ||
222 | ret = kernctl_get_subbuf(stream->wait_fd, &consumed_pos); | |
223 | if (ret < 0) { | |
224 | if (errno != EAGAIN) { | |
225 | PERROR("kernctl_get_subbuf snapshot"); | |
226 | goto end_unlock; | |
227 | } | |
228 | DBG("Kernel consumer get subbuf failed. Skipping it."); | |
229 | consumed_pos += stream->max_sb_size; | |
230 | continue; | |
231 | } | |
232 | ||
233 | ret = kernctl_get_subbuf_size(stream->wait_fd, &len); | |
234 | if (ret < 0) { | |
235 | ERR("Snapshot kernctl_get_subbuf_size"); | |
29decac3 | 236 | goto error_put_subbuf; |
07b86b52 JD |
237 | } |
238 | ||
239 | ret = kernctl_get_padded_subbuf_size(stream->wait_fd, &padded_len); | |
240 | if (ret < 0) { | |
241 | ERR("Snapshot kernctl_get_padded_subbuf_size"); | |
29decac3 | 242 | goto error_put_subbuf; |
07b86b52 JD |
243 | } |
244 | ||
245 | read_len = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, len, | |
246 | padded_len - len); | |
247 | /* | |
29decac3 DG |
248 | * We write the padded len in local tracefiles but the data len |
249 | * when using a relay. Display the error but continue processing | |
250 | * to try to release the subbuffer. | |
07b86b52 JD |
251 | */ |
252 | if (relayd_id != (uint64_t) -1ULL) { | |
253 | if (read_len != len) { | |
254 | ERR("Error sending to the relay (ret: %zd != len: %lu)", | |
255 | read_len, len); | |
256 | } | |
257 | } else { | |
258 | if (read_len != padded_len) { | |
259 | ERR("Error writing to tracefile (ret: %zd != len: %lu)", | |
260 | read_len, padded_len); | |
261 | } | |
262 | } | |
263 | ||
264 | ret = kernctl_put_subbuf(stream->wait_fd); | |
265 | if (ret < 0) { | |
266 | ERR("Snapshot kernctl_put_subbuf"); | |
267 | goto end_unlock; | |
268 | } | |
269 | consumed_pos += stream->max_sb_size; | |
270 | } | |
271 | ||
272 | if (relayd_id == (uint64_t) -1ULL) { | |
fdf9986c MD |
273 | if (stream->out_fd >= 0) { |
274 | ret = close(stream->out_fd); | |
275 | if (ret < 0) { | |
276 | PERROR("Kernel consumer snapshot close out_fd"); | |
277 | goto end_unlock; | |
278 | } | |
279 | stream->out_fd = -1; | |
07b86b52 | 280 | } |
07b86b52 JD |
281 | } else { |
282 | close_relayd_stream(stream); | |
283 | stream->net_seq_idx = (uint64_t) -1ULL; | |
284 | } | |
285 | pthread_mutex_unlock(&stream->lock); | |
286 | } | |
287 | ||
288 | /* All good! */ | |
289 | ret = 0; | |
290 | goto end; | |
291 | ||
29decac3 DG |
292 | error_put_subbuf: |
293 | ret = kernctl_put_subbuf(stream->wait_fd); | |
294 | if (ret < 0) { | |
295 | ERR("Snapshot kernctl_put_subbuf error path"); | |
296 | } | |
07b86b52 JD |
297 | end_unlock: |
298 | pthread_mutex_unlock(&stream->lock); | |
299 | end: | |
300 | rcu_read_unlock(); | |
301 | return ret; | |
302 | } | |
303 | ||
304 | /* | |
305 | * Read the whole metadata available for a snapshot. | |
306 | * | |
307 | * Returns 0 on success, < 0 on error | |
308 | */ | |
309 | int lttng_kconsumer_snapshot_metadata(uint64_t key, char *path, | |
e2039c7a | 310 | uint64_t relayd_id, struct lttng_consumer_local_data *ctx) |
07b86b52 | 311 | { |
d771f832 DG |
312 | int ret, use_relayd = 0; |
313 | ssize_t ret_read; | |
07b86b52 JD |
314 | struct lttng_consumer_channel *metadata_channel; |
315 | struct lttng_consumer_stream *metadata_stream; | |
d771f832 DG |
316 | |
317 | assert(ctx); | |
07b86b52 JD |
318 | |
319 | DBG("Kernel consumer snapshot metadata with key %" PRIu64 " at path %s", | |
320 | key, path); | |
321 | ||
322 | rcu_read_lock(); | |
323 | ||
324 | metadata_channel = consumer_find_channel(key); | |
325 | if (!metadata_channel) { | |
d771f832 | 326 | ERR("Kernel snapshot metadata not found for key %" PRIu64, key); |
07b86b52 | 327 | ret = -1; |
d771f832 | 328 | goto error; |
07b86b52 JD |
329 | } |
330 | ||
331 | metadata_stream = metadata_channel->metadata_stream; | |
332 | assert(metadata_stream); | |
333 | ||
d771f832 | 334 | /* Flag once that we have a valid relayd for the stream. */ |
e2039c7a | 335 | if (relayd_id != (uint64_t) -1ULL) { |
d771f832 DG |
336 | use_relayd = 1; |
337 | } | |
338 | ||
339 | if (use_relayd) { | |
10a50311 | 340 | ret = consumer_send_relayd_stream(metadata_stream, path); |
e2039c7a | 341 | if (ret < 0) { |
d771f832 | 342 | goto error; |
e2039c7a | 343 | } |
e2039c7a JD |
344 | } else { |
345 | ret = utils_create_stream_file(path, metadata_stream->name, | |
346 | metadata_stream->chan->tracefile_size, | |
347 | metadata_stream->tracefile_count_current, | |
348 | metadata_stream->uid, metadata_stream->gid); | |
349 | if (ret < 0) { | |
d771f832 | 350 | goto error; |
e2039c7a JD |
351 | } |
352 | metadata_stream->out_fd = ret; | |
07b86b52 | 353 | } |
07b86b52 | 354 | |
d771f832 DG |
355 | do { |
356 | ret_read = lttng_kconsumer_read_subbuffer(metadata_stream, ctx); | |
357 | if (ret_read < 0) { | |
358 | if (ret_read != -EPERM) { | |
359 | ERR("Kernel snapshot reading metadata subbuffer (ret: %ld)", | |
360 | ret_read); | |
361 | goto error; | |
07b86b52 | 362 | } |
d771f832 | 363 | /* ret_read is negative at this point so we will exit the loop. */ |
07b86b52 JD |
364 | continue; |
365 | } | |
d771f832 | 366 | } while (ret_read >= 0); |
07b86b52 | 367 | |
d771f832 DG |
368 | if (use_relayd) { |
369 | close_relayd_stream(metadata_stream); | |
370 | metadata_stream->net_seq_idx = (uint64_t) -1ULL; | |
371 | } else { | |
fdf9986c MD |
372 | if (metadata_stream->out_fd >= 0) { |
373 | ret = close(metadata_stream->out_fd); | |
374 | if (ret < 0) { | |
375 | PERROR("Kernel consumer snapshot metadata close out_fd"); | |
376 | /* | |
377 | * Don't go on error here since the snapshot was successful at this | |
378 | * point but somehow the close failed. | |
379 | */ | |
380 | } | |
381 | metadata_stream->out_fd = -1; | |
e2039c7a | 382 | } |
e2039c7a JD |
383 | } |
384 | ||
07b86b52 | 385 | ret = 0; |
d771f832 | 386 | |
cf53a8a6 JD |
387 | cds_list_del(&metadata_stream->send_node); |
388 | consumer_stream_destroy(metadata_stream, NULL); | |
389 | metadata_channel->metadata_stream = NULL; | |
d771f832 | 390 | error: |
07b86b52 JD |
391 | rcu_read_unlock(); |
392 | return ret; | |
393 | } | |
394 | ||
1803a064 MD |
395 | /* |
396 | * Receive command from session daemon and process it. | |
397 | * | |
398 | * Return 1 on success else a negative value or 0. | |
399 | */ | |
3bd1e081 MD |
400 | int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx, |
401 | int sock, struct pollfd *consumer_sockpoll) | |
402 | { | |
403 | ssize_t ret; | |
f50f23d9 | 404 | enum lttng_error_code ret_code = LTTNG_OK; |
3bd1e081 MD |
405 | struct lttcomm_consumer_msg msg; |
406 | ||
407 | ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg)); | |
408 | if (ret != sizeof(msg)) { | |
1803a064 | 409 | if (ret > 0) { |
c6857fcf | 410 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD); |
1803a064 MD |
411 | ret = -1; |
412 | } | |
3bd1e081 MD |
413 | return ret; |
414 | } | |
415 | if (msg.cmd_type == LTTNG_CONSUMER_STOP) { | |
f50f23d9 DG |
416 | /* |
417 | * Notify the session daemon that the command is completed. | |
418 | * | |
419 | * On transport layer error, the function call will print an error | |
420 | * message so handling the returned code is a bit useless since we | |
421 | * return an error code anyway. | |
422 | */ | |
423 | (void) consumer_send_status_msg(sock, ret_code); | |
3bd1e081 MD |
424 | return -ENOENT; |
425 | } | |
426 | ||
b0b335c8 MD |
427 | /* relayd needs RCU read-side protection */ |
428 | rcu_read_lock(); | |
429 | ||
3bd1e081 | 430 | switch (msg.cmd_type) { |
00e2e675 DG |
431 | case LTTNG_CONSUMER_ADD_RELAYD_SOCKET: |
432 | { | |
f50f23d9 | 433 | /* Session daemon status message are handled in the following call. */ |
7735ef9e DG |
434 | ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index, |
435 | msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll, | |
46e6455f | 436 | &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id); |
00e2e675 DG |
437 | goto end_nosignal; |
438 | } | |
3bd1e081 MD |
439 | case LTTNG_CONSUMER_ADD_CHANNEL: |
440 | { | |
441 | struct lttng_consumer_channel *new_channel; | |
e43c41c5 | 442 | int ret_recv; |
3bd1e081 | 443 | |
f50f23d9 DG |
444 | /* First send a status message before receiving the fds. */ |
445 | ret = consumer_send_status_msg(sock, ret_code); | |
446 | if (ret < 0) { | |
447 | /* Somehow, the session daemon is not responding anymore. */ | |
1803a064 | 448 | goto error_fatal; |
f50f23d9 | 449 | } |
d88aee68 | 450 | DBG("consumer_add_channel %" PRIu64, msg.u.channel.channel_key); |
3bd1e081 | 451 | new_channel = consumer_allocate_channel(msg.u.channel.channel_key, |
ffe60014 DG |
452 | msg.u.channel.session_id, msg.u.channel.pathname, |
453 | msg.u.channel.name, msg.u.channel.uid, msg.u.channel.gid, | |
1624d5b7 JD |
454 | msg.u.channel.relayd_id, msg.u.channel.output, |
455 | msg.u.channel.tracefile_size, | |
1950109e | 456 | msg.u.channel.tracefile_count, 0, |
2bba9e53 | 457 | msg.u.channel.monitor); |
3bd1e081 | 458 | if (new_channel == NULL) { |
f73fabfd | 459 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR); |
3bd1e081 MD |
460 | goto end_nosignal; |
461 | } | |
ffe60014 | 462 | new_channel->nb_init_stream_left = msg.u.channel.nb_init_streams; |
95a1109b JD |
463 | switch (msg.u.channel.output) { |
464 | case LTTNG_EVENT_SPLICE: | |
465 | new_channel->output = CONSUMER_CHANNEL_SPLICE; | |
466 | break; | |
467 | case LTTNG_EVENT_MMAP: | |
468 | new_channel->output = CONSUMER_CHANNEL_MMAP; | |
469 | break; | |
470 | default: | |
471 | ERR("Channel output unknown %d", msg.u.channel.output); | |
472 | goto end_nosignal; | |
473 | } | |
ffe60014 DG |
474 | |
475 | /* Translate and save channel type. */ | |
476 | switch (msg.u.channel.type) { | |
477 | case CONSUMER_CHANNEL_TYPE_DATA: | |
478 | case CONSUMER_CHANNEL_TYPE_METADATA: | |
479 | new_channel->type = msg.u.channel.type; | |
480 | break; | |
481 | default: | |
482 | assert(0); | |
483 | goto end_nosignal; | |
484 | }; | |
485 | ||
3bd1e081 | 486 | if (ctx->on_recv_channel != NULL) { |
e43c41c5 JD |
487 | ret_recv = ctx->on_recv_channel(new_channel); |
488 | if (ret_recv == 0) { | |
489 | ret = consumer_add_channel(new_channel, ctx); | |
490 | } else if (ret_recv < 0) { | |
3bd1e081 MD |
491 | goto end_nosignal; |
492 | } | |
493 | } else { | |
e43c41c5 | 494 | ret = consumer_add_channel(new_channel, ctx); |
3bd1e081 | 495 | } |
e43c41c5 JD |
496 | |
497 | /* If we received an error in add_channel, we need to report it. */ | |
821fffb2 | 498 | if (ret < 0) { |
1803a064 MD |
499 | ret = consumer_send_status_msg(sock, ret); |
500 | if (ret < 0) { | |
501 | goto error_fatal; | |
502 | } | |
e43c41c5 JD |
503 | goto end_nosignal; |
504 | } | |
505 | ||
3bd1e081 MD |
506 | goto end_nosignal; |
507 | } | |
508 | case LTTNG_CONSUMER_ADD_STREAM: | |
509 | { | |
dae10966 DG |
510 | int fd; |
511 | struct lttng_pipe *stream_pipe; | |
00e2e675 | 512 | struct lttng_consumer_stream *new_stream; |
ffe60014 | 513 | struct lttng_consumer_channel *channel; |
c80048c6 | 514 | int alloc_ret = 0; |
3bd1e081 | 515 | |
ffe60014 DG |
516 | /* |
517 | * Get stream's channel reference. Needed when adding the stream to the | |
518 | * global hash table. | |
519 | */ | |
520 | channel = consumer_find_channel(msg.u.stream.channel_key); | |
521 | if (!channel) { | |
522 | /* | |
523 | * We could not find the channel. Can happen if cpu hotplug | |
524 | * happens while tearing down. | |
525 | */ | |
d88aee68 | 526 | ERR("Unable to find channel key %" PRIu64, msg.u.stream.channel_key); |
ffe60014 DG |
527 | ret_code = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
528 | } | |
529 | ||
f50f23d9 DG |
530 | /* First send a status message before receiving the fds. */ |
531 | ret = consumer_send_status_msg(sock, ret_code); | |
1803a064 | 532 | if (ret < 0) { |
d771f832 | 533 | /* Somehow, the session daemon is not responding anymore. */ |
1803a064 MD |
534 | goto error_fatal; |
535 | } | |
536 | if (ret_code != LTTNG_OK) { | |
d771f832 | 537 | /* Channel was not found. */ |
f50f23d9 DG |
538 | goto end_nosignal; |
539 | } | |
540 | ||
d771f832 | 541 | /* Blocking call */ |
3bd1e081 | 542 | if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) { |
3f8e211f | 543 | rcu_read_unlock(); |
3bd1e081 MD |
544 | return -EINTR; |
545 | } | |
00e2e675 DG |
546 | |
547 | /* Get stream file descriptor from socket */ | |
f2fc6720 MD |
548 | ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1); |
549 | if (ret != sizeof(fd)) { | |
f73fabfd | 550 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD); |
3f8e211f | 551 | rcu_read_unlock(); |
3bd1e081 MD |
552 | return ret; |
553 | } | |
3bd1e081 | 554 | |
f50f23d9 DG |
555 | /* |
556 | * Send status code to session daemon only if the recv works. If the | |
557 | * above recv() failed, the session daemon is notified through the | |
558 | * error socket and the teardown is eventually done. | |
559 | */ | |
560 | ret = consumer_send_status_msg(sock, ret_code); | |
561 | if (ret < 0) { | |
562 | /* Somehow, the session daemon is not responding anymore. */ | |
563 | goto end_nosignal; | |
564 | } | |
565 | ||
ffe60014 DG |
566 | new_stream = consumer_allocate_stream(channel->key, |
567 | fd, | |
568 | LTTNG_CONSUMER_ACTIVE_STREAM, | |
569 | channel->name, | |
570 | channel->uid, | |
571 | channel->gid, | |
572 | channel->relayd_id, | |
573 | channel->session_id, | |
574 | msg.u.stream.cpu, | |
575 | &alloc_ret, | |
4891ece8 DG |
576 | channel->type, |
577 | channel->monitor); | |
3bd1e081 | 578 | if (new_stream == NULL) { |
c80048c6 MD |
579 | switch (alloc_ret) { |
580 | case -ENOMEM: | |
581 | case -EINVAL: | |
582 | default: | |
583 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR); | |
584 | break; | |
c80048c6 | 585 | } |
3f8e211f | 586 | goto end_nosignal; |
3bd1e081 | 587 | } |
d771f832 | 588 | |
ffe60014 DG |
589 | new_stream->chan = channel; |
590 | new_stream->wait_fd = fd; | |
07b86b52 JD |
591 | switch (channel->output) { |
592 | case CONSUMER_CHANNEL_SPLICE: | |
593 | new_stream->output = LTTNG_EVENT_SPLICE; | |
594 | break; | |
595 | case CONSUMER_CHANNEL_MMAP: | |
596 | new_stream->output = LTTNG_EVENT_MMAP; | |
597 | break; | |
598 | default: | |
599 | ERR("Stream output unknown %d", channel->output); | |
600 | goto end_nosignal; | |
601 | } | |
00e2e675 | 602 | |
a0c83db9 DG |
603 | /* |
604 | * We've just assigned the channel to the stream so increment the | |
07b86b52 JD |
605 | * refcount right now. We don't need to increment the refcount for |
606 | * streams in no monitor because we handle manually the cleanup of | |
607 | * those. It is very important to make sure there is NO prior | |
608 | * consumer_del_stream() calls or else the refcount will be unbalanced. | |
a0c83db9 | 609 | */ |
07b86b52 JD |
610 | if (channel->monitor) { |
611 | uatomic_inc(&new_stream->chan->refcount); | |
612 | } | |
9d9353f9 | 613 | |
fb3a43a9 DG |
614 | /* |
615 | * The buffer flush is done on the session daemon side for the kernel | |
616 | * so no need for the stream "hangup_flush_done" variable to be | |
617 | * tracked. This is important for a kernel stream since we don't rely | |
618 | * on the flush state of the stream to read data. It's not the case for | |
619 | * user space tracing. | |
620 | */ | |
621 | new_stream->hangup_flush_done = 0; | |
622 | ||
633d0084 DG |
623 | if (ctx->on_recv_stream) { |
624 | ret = ctx->on_recv_stream(new_stream); | |
625 | if (ret < 0) { | |
d771f832 | 626 | consumer_stream_free(new_stream); |
633d0084 | 627 | goto end_nosignal; |
fb3a43a9 | 628 | } |
633d0084 | 629 | } |
fb3a43a9 | 630 | |
07b86b52 JD |
631 | if (new_stream->metadata_flag) { |
632 | channel->metadata_stream = new_stream; | |
633 | } | |
634 | ||
2bba9e53 DG |
635 | /* Do not monitor this stream. */ |
636 | if (!channel->monitor) { | |
5eecee74 | 637 | DBG("Kernel consumer add stream %s in no monitor mode with " |
6dc3064a | 638 | "relayd id %" PRIu64, new_stream->name, |
5eecee74 | 639 | new_stream->net_seq_idx); |
10a50311 | 640 | cds_list_add(&new_stream->send_node, &channel->streams.head); |
6dc3064a DG |
641 | break; |
642 | } | |
643 | ||
e1b71bdc DG |
644 | /* Send stream to relayd if the stream has an ID. */ |
645 | if (new_stream->net_seq_idx != (uint64_t) -1ULL) { | |
194ee077 DG |
646 | ret = consumer_send_relayd_stream(new_stream, |
647 | new_stream->chan->pathname); | |
e1b71bdc DG |
648 | if (ret < 0) { |
649 | consumer_stream_free(new_stream); | |
650 | goto end_nosignal; | |
651 | } | |
e2039c7a JD |
652 | } |
653 | ||
50f8ae69 | 654 | /* Get the right pipe where the stream will be sent. */ |
633d0084 | 655 | if (new_stream->metadata_flag) { |
5ab66908 MD |
656 | ret = consumer_add_metadata_stream(new_stream); |
657 | if (ret) { | |
658 | ERR("Consumer add metadata stream %" PRIu64 " failed. Continuing", | |
659 | new_stream->key); | |
660 | consumer_stream_free(new_stream); | |
661 | goto end_nosignal; | |
662 | } | |
dae10966 | 663 | stream_pipe = ctx->consumer_metadata_pipe; |
3bd1e081 | 664 | } else { |
5ab66908 MD |
665 | ret = consumer_add_data_stream(new_stream); |
666 | if (ret) { | |
667 | ERR("Consumer add stream %" PRIu64 " failed. Continuing", | |
668 | new_stream->key); | |
669 | consumer_stream_free(new_stream); | |
670 | goto end_nosignal; | |
671 | } | |
dae10966 | 672 | stream_pipe = ctx->consumer_data_pipe; |
50f8ae69 DG |
673 | } |
674 | ||
5ab66908 MD |
675 | /* Vitible to other threads */ |
676 | new_stream->globally_visible = 1; | |
677 | ||
dae10966 | 678 | ret = lttng_pipe_write(stream_pipe, &new_stream, sizeof(new_stream)); |
50f8ae69 | 679 | if (ret < 0) { |
dae10966 | 680 | ERR("Consumer write %s stream to pipe %d", |
50f8ae69 | 681 | new_stream->metadata_flag ? "metadata" : "data", |
dae10966 | 682 | lttng_pipe_get_writefd(stream_pipe)); |
5ab66908 MD |
683 | if (new_stream->metadata_flag) { |
684 | consumer_del_stream_for_metadata(new_stream); | |
685 | } else { | |
686 | consumer_del_stream_for_data(new_stream); | |
687 | } | |
50f8ae69 | 688 | goto end_nosignal; |
3bd1e081 | 689 | } |
00e2e675 | 690 | |
50f8ae69 | 691 | DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64, |
ffe60014 | 692 | new_stream->name, fd, new_stream->relayd_stream_id); |
3bd1e081 MD |
693 | break; |
694 | } | |
695 | case LTTNG_CONSUMER_UPDATE_STREAM: | |
696 | { | |
3f8e211f DG |
697 | rcu_read_unlock(); |
698 | return -ENOSYS; | |
699 | } | |
700 | case LTTNG_CONSUMER_DESTROY_RELAYD: | |
701 | { | |
a6ba4fe1 | 702 | uint64_t index = msg.u.destroy_relayd.net_seq_idx; |
3f8e211f DG |
703 | struct consumer_relayd_sock_pair *relayd; |
704 | ||
a6ba4fe1 | 705 | DBG("Kernel consumer destroying relayd %" PRIu64, index); |
3f8e211f DG |
706 | |
707 | /* Get relayd reference if exists. */ | |
a6ba4fe1 | 708 | relayd = consumer_find_relayd(index); |
3f8e211f | 709 | if (relayd == NULL) { |
3448e266 | 710 | DBG("Unable to find relayd %" PRIu64, index); |
f50f23d9 | 711 | ret_code = LTTNG_ERR_NO_CONSUMER; |
3bd1e081 | 712 | } |
3f8e211f | 713 | |
a6ba4fe1 DG |
714 | /* |
715 | * Each relayd socket pair has a refcount of stream attached to it | |
716 | * which tells if the relayd is still active or not depending on the | |
717 | * refcount value. | |
718 | * | |
719 | * This will set the destroy flag of the relayd object and destroy it | |
720 | * if the refcount reaches zero when called. | |
721 | * | |
722 | * The destroy can happen either here or when a stream fd hangs up. | |
723 | */ | |
f50f23d9 DG |
724 | if (relayd) { |
725 | consumer_flag_relayd_for_destroy(relayd); | |
726 | } | |
727 | ||
728 | ret = consumer_send_status_msg(sock, ret_code); | |
729 | if (ret < 0) { | |
730 | /* Somehow, the session daemon is not responding anymore. */ | |
1803a064 | 731 | goto error_fatal; |
f50f23d9 | 732 | } |
3f8e211f | 733 | |
3f8e211f | 734 | goto end_nosignal; |
3bd1e081 | 735 | } |
6d805429 | 736 | case LTTNG_CONSUMER_DATA_PENDING: |
53632229 | 737 | { |
c8f59ee5 | 738 | int32_t ret; |
6d805429 | 739 | uint64_t id = msg.u.data_pending.session_id; |
c8f59ee5 | 740 | |
6d805429 | 741 | DBG("Kernel consumer data pending command for id %" PRIu64, id); |
c8f59ee5 | 742 | |
6d805429 | 743 | ret = consumer_data_pending(id); |
c8f59ee5 DG |
744 | |
745 | /* Send back returned value to session daemon */ | |
746 | ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret)); | |
747 | if (ret < 0) { | |
6d805429 | 748 | PERROR("send data pending ret code"); |
1803a064 | 749 | goto error_fatal; |
c8f59ee5 | 750 | } |
f50f23d9 DG |
751 | |
752 | /* | |
753 | * No need to send back a status message since the data pending | |
754 | * returned value is the response. | |
755 | */ | |
c8f59ee5 | 756 | break; |
53632229 | 757 | } |
6dc3064a DG |
758 | case LTTNG_CONSUMER_SNAPSHOT_CHANNEL: |
759 | { | |
07b86b52 JD |
760 | if (msg.u.snapshot_channel.metadata == 1) { |
761 | ret = lttng_kconsumer_snapshot_metadata(msg.u.snapshot_channel.key, | |
e2039c7a JD |
762 | msg.u.snapshot_channel.pathname, |
763 | msg.u.snapshot_channel.relayd_id, ctx); | |
07b86b52 JD |
764 | if (ret < 0) { |
765 | ERR("Snapshot metadata failed"); | |
766 | ret_code = LTTNG_ERR_KERN_META_FAIL; | |
767 | } | |
768 | } else { | |
769 | ret = lttng_kconsumer_snapshot_channel(msg.u.snapshot_channel.key, | |
770 | msg.u.snapshot_channel.pathname, | |
5c786ded JD |
771 | msg.u.snapshot_channel.relayd_id, |
772 | msg.u.snapshot_channel.max_stream_size, | |
773 | ctx); | |
07b86b52 JD |
774 | if (ret < 0) { |
775 | ERR("Snapshot channel failed"); | |
776 | ret_code = LTTNG_ERR_KERN_CHAN_FAIL; | |
777 | } | |
778 | } | |
779 | ||
6dc3064a DG |
780 | ret = consumer_send_status_msg(sock, ret_code); |
781 | if (ret < 0) { | |
782 | /* Somehow, the session daemon is not responding anymore. */ | |
783 | goto end_nosignal; | |
784 | } | |
785 | break; | |
786 | } | |
07b86b52 JD |
787 | case LTTNG_CONSUMER_DESTROY_CHANNEL: |
788 | { | |
789 | uint64_t key = msg.u.destroy_channel.key; | |
790 | struct lttng_consumer_channel *channel; | |
791 | ||
792 | channel = consumer_find_channel(key); | |
793 | if (!channel) { | |
794 | ERR("Kernel consumer destroy channel %" PRIu64 " not found", key); | |
795 | ret_code = LTTNG_ERR_KERN_CHAN_NOT_FOUND; | |
796 | } | |
797 | ||
798 | ret = consumer_send_status_msg(sock, ret_code); | |
799 | if (ret < 0) { | |
800 | /* Somehow, the session daemon is not responding anymore. */ | |
801 | goto end_nosignal; | |
802 | } | |
803 | ||
804 | /* | |
805 | * This command should ONLY be issued for channel with streams set in | |
806 | * no monitor mode. | |
807 | */ | |
808 | assert(!channel->monitor); | |
809 | ||
810 | /* | |
811 | * The refcount should ALWAYS be 0 in the case of a channel in no | |
812 | * monitor mode. | |
813 | */ | |
814 | assert(!uatomic_sub_return(&channel->refcount, 1)); | |
815 | ||
816 | consumer_del_channel(channel); | |
817 | ||
818 | goto end_nosignal; | |
819 | } | |
3bd1e081 | 820 | default: |
3f8e211f | 821 | goto end_nosignal; |
3bd1e081 | 822 | } |
3f8e211f | 823 | |
3bd1e081 | 824 | end_nosignal: |
b0b335c8 | 825 | rcu_read_unlock(); |
4cbc1a04 DG |
826 | |
827 | /* | |
828 | * Return 1 to indicate success since the 0 value can be a socket | |
829 | * shutdown during the recv() or send() call. | |
830 | */ | |
831 | return 1; | |
1803a064 MD |
832 | |
833 | error_fatal: | |
834 | rcu_read_unlock(); | |
835 | /* This will issue a consumer stop. */ | |
836 | return -1; | |
3bd1e081 | 837 | } |
d41f73b7 MD |
838 | |
839 | /* | |
840 | * Consume data on a file descriptor and write it on a trace file. | |
841 | */ | |
4078b776 | 842 | ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream, |
d41f73b7 MD |
843 | struct lttng_consumer_local_data *ctx) |
844 | { | |
1d4dfdef | 845 | unsigned long len, subbuf_size, padding; |
d41f73b7 | 846 | int err; |
4078b776 | 847 | ssize_t ret = 0; |
d41f73b7 MD |
848 | int infd = stream->wait_fd; |
849 | ||
850 | DBG("In read_subbuffer (infd : %d)", infd); | |
851 | /* Get the next subbuffer */ | |
852 | err = kernctl_get_next_subbuf(infd); | |
853 | if (err != 0) { | |
1d4dfdef | 854 | ret = err; |
d41f73b7 MD |
855 | /* |
856 | * This is a debug message even for single-threaded consumer, | |
857 | * because poll() have more relaxed criterions than get subbuf, | |
858 | * so get_subbuf may fail for short race windows where poll() | |
859 | * would issue wakeups. | |
860 | */ | |
861 | DBG("Reserving sub buffer failed (everything is normal, " | |
862 | "it is due to concurrency)"); | |
863 | goto end; | |
864 | } | |
865 | ||
1d4dfdef DG |
866 | /* Get the full subbuffer size including padding */ |
867 | err = kernctl_get_padded_subbuf_size(infd, &len); | |
868 | if (err != 0) { | |
869 | errno = -err; | |
870 | perror("Getting sub-buffer len failed."); | |
871 | ret = err; | |
872 | goto end; | |
873 | } | |
874 | ||
ffe60014 | 875 | switch (stream->chan->output) { |
07b86b52 | 876 | case CONSUMER_CHANNEL_SPLICE: |
1d4dfdef DG |
877 | /* |
878 | * XXX: The lttng-modules splice "actor" does not handle copying | |
879 | * partial pages hence only using the subbuffer size without the | |
880 | * padding makes the splice fail. | |
881 | */ | |
882 | subbuf_size = len; | |
883 | padding = 0; | |
884 | ||
885 | /* splice the subbuffer to the tracefile */ | |
91dfef6e DG |
886 | ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, subbuf_size, |
887 | padding); | |
888 | /* | |
889 | * XXX: Splice does not support network streaming so the return value | |
890 | * is simply checked against subbuf_size and not like the mmap() op. | |
891 | */ | |
1d4dfdef DG |
892 | if (ret != subbuf_size) { |
893 | /* | |
894 | * display the error but continue processing to try | |
895 | * to release the subbuffer | |
896 | */ | |
897 | ERR("Error splicing to tracefile (ret: %zd != len: %lu)", | |
898 | ret, subbuf_size); | |
899 | } | |
900 | break; | |
07b86b52 | 901 | case CONSUMER_CHANNEL_MMAP: |
1d4dfdef DG |
902 | /* Get subbuffer size without padding */ |
903 | err = kernctl_get_subbuf_size(infd, &subbuf_size); | |
904 | if (err != 0) { | |
905 | errno = -err; | |
906 | perror("Getting sub-buffer len failed."); | |
907 | ret = err; | |
908 | goto end; | |
909 | } | |
47e81c02 | 910 | |
1d4dfdef DG |
911 | /* Make sure the tracer is not gone mad on us! */ |
912 | assert(len >= subbuf_size); | |
913 | ||
914 | padding = len - subbuf_size; | |
915 | ||
916 | /* write the subbuffer to the tracefile */ | |
91dfef6e DG |
917 | ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size, |
918 | padding); | |
919 | /* | |
920 | * The mmap operation should write subbuf_size amount of data when | |
921 | * network streaming or the full padding (len) size when we are _not_ | |
922 | * streaming. | |
923 | */ | |
d88aee68 DG |
924 | if ((ret != subbuf_size && stream->net_seq_idx != (uint64_t) -1ULL) || |
925 | (ret != len && stream->net_seq_idx == (uint64_t) -1ULL)) { | |
1d4dfdef | 926 | /* |
91dfef6e DG |
927 | * Display the error but continue processing to try to release the |
928 | * subbuffer | |
1d4dfdef | 929 | */ |
91dfef6e DG |
930 | ERR("Error writing to tracefile " |
931 | "(ret: %zd != len: %lu != subbuf_size: %lu)", | |
932 | ret, len, subbuf_size); | |
1d4dfdef DG |
933 | } |
934 | break; | |
935 | default: | |
936 | ERR("Unknown output method"); | |
937 | ret = -1; | |
d41f73b7 MD |
938 | } |
939 | ||
940 | err = kernctl_put_next_subbuf(infd); | |
941 | if (err != 0) { | |
21073eaa | 942 | errno = -err; |
d41f73b7 MD |
943 | if (errno == EFAULT) { |
944 | perror("Error in unreserving sub buffer\n"); | |
945 | } else if (errno == EIO) { | |
946 | /* Should never happen with newer LTTng versions */ | |
947 | perror("Reader has been pushed by the writer, last sub-buffer corrupted."); | |
948 | } | |
21073eaa DG |
949 | |
950 | ret = -err; | |
d41f73b7 MD |
951 | goto end; |
952 | } | |
953 | ||
954 | end: | |
955 | return ret; | |
956 | } | |
957 | ||
958 | int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream) | |
959 | { | |
960 | int ret; | |
ffe60014 DG |
961 | |
962 | assert(stream); | |
963 | ||
2bba9e53 DG |
964 | /* |
965 | * Don't create anything if this is set for streaming or should not be | |
966 | * monitored. | |
967 | */ | |
968 | if (stream->net_seq_idx == (uint64_t) -1ULL && stream->chan->monitor) { | |
fe4477ee JD |
969 | ret = utils_create_stream_file(stream->chan->pathname, stream->name, |
970 | stream->chan->tracefile_size, stream->tracefile_count_current, | |
971 | stream->uid, stream->gid); | |
972 | if (ret < 0) { | |
973 | goto error; | |
974 | } | |
975 | stream->out_fd = ret; | |
976 | stream->tracefile_size_current = 0; | |
ffe60014 | 977 | } |
d41f73b7 | 978 | |
d41f73b7 MD |
979 | if (stream->output == LTTNG_EVENT_MMAP) { |
980 | /* get the len of the mmap region */ | |
981 | unsigned long mmap_len; | |
982 | ||
983 | ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len); | |
984 | if (ret != 0) { | |
87dc6a9c | 985 | errno = -ret; |
ffe60014 | 986 | PERROR("kernctl_get_mmap_len"); |
d41f73b7 MD |
987 | goto error_close_fd; |
988 | } | |
989 | stream->mmap_len = (size_t) mmap_len; | |
990 | ||
ffe60014 DG |
991 | stream->mmap_base = mmap(NULL, stream->mmap_len, PROT_READ, |
992 | MAP_PRIVATE, stream->wait_fd, 0); | |
d41f73b7 | 993 | if (stream->mmap_base == MAP_FAILED) { |
ffe60014 | 994 | PERROR("Error mmaping"); |
d41f73b7 MD |
995 | ret = -1; |
996 | goto error_close_fd; | |
997 | } | |
998 | } | |
999 | ||
1000 | /* we return 0 to let the library handle the FD internally */ | |
1001 | return 0; | |
1002 | ||
1003 | error_close_fd: | |
2f225ce2 | 1004 | if (stream->out_fd >= 0) { |
d41f73b7 MD |
1005 | int err; |
1006 | ||
1007 | err = close(stream->out_fd); | |
1008 | assert(!err); | |
2f225ce2 | 1009 | stream->out_fd = -1; |
d41f73b7 MD |
1010 | } |
1011 | error: | |
1012 | return ret; | |
1013 | } | |
1014 | ||
ca22feea DG |
1015 | /* |
1016 | * Check if data is still being extracted from the buffers for a specific | |
4e9a4686 DG |
1017 | * stream. Consumer data lock MUST be acquired before calling this function |
1018 | * and the stream lock. | |
ca22feea | 1019 | * |
6d805429 | 1020 | * Return 1 if the traced data are still getting read else 0 meaning that the |
ca22feea DG |
1021 | * data is available for trace viewer reading. |
1022 | */ | |
6d805429 | 1023 | int lttng_kconsumer_data_pending(struct lttng_consumer_stream *stream) |
ca22feea DG |
1024 | { |
1025 | int ret; | |
1026 | ||
1027 | assert(stream); | |
1028 | ||
873b9e9a MD |
1029 | if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) { |
1030 | ret = 0; | |
1031 | goto end; | |
1032 | } | |
1033 | ||
ca22feea DG |
1034 | ret = kernctl_get_next_subbuf(stream->wait_fd); |
1035 | if (ret == 0) { | |
1036 | /* There is still data so let's put back this subbuffer. */ | |
1037 | ret = kernctl_put_subbuf(stream->wait_fd); | |
1038 | assert(ret == 0); | |
6d805429 | 1039 | ret = 1; /* Data is pending */ |
4e9a4686 | 1040 | goto end; |
ca22feea DG |
1041 | } |
1042 | ||
6d805429 DG |
1043 | /* Data is NOT pending and ready to be read. */ |
1044 | ret = 0; | |
ca22feea | 1045 | |
6efae65e DG |
1046 | end: |
1047 | return ret; | |
ca22feea | 1048 | } |