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> | |
f02e1e8a | 21 | #include <lttng/ust-ctl.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> | |
dbb5dfe6 | 28 | #include <sys/stat.h> |
3bd1e081 | 29 | #include <sys/types.h> |
77c7c900 | 30 | #include <inttypes.h> |
3bd1e081 | 31 | #include <unistd.h> |
ffe60014 | 32 | #include <urcu/list.h> |
331744e3 | 33 | #include <signal.h> |
0857097f | 34 | |
990570ed | 35 | #include <common/common.h> |
10a8a223 | 36 | #include <common/sessiond-comm/sessiond-comm.h> |
00e2e675 | 37 | #include <common/relayd/relayd.h> |
dbb5dfe6 | 38 | #include <common/compat/fcntl.h> |
331744e3 JD |
39 | #include <common/consumer-metadata-cache.h> |
40 | #include <common/consumer-timer.h> | |
fe4477ee | 41 | #include <common/utils.h> |
10a8a223 DG |
42 | |
43 | #include "ust-consumer.h" | |
3bd1e081 MD |
44 | |
45 | extern struct lttng_consumer_global_data consumer_data; | |
46 | extern int consumer_poll_timeout; | |
47 | extern volatile int consumer_quit; | |
48 | ||
49 | /* | |
ffe60014 DG |
50 | * Free channel object and all streams associated with it. This MUST be used |
51 | * only and only if the channel has _NEVER_ been added to the global channel | |
52 | * hash table. | |
3bd1e081 | 53 | */ |
ffe60014 | 54 | static void destroy_channel(struct lttng_consumer_channel *channel) |
3bd1e081 | 55 | { |
ffe60014 DG |
56 | struct lttng_consumer_stream *stream, *stmp; |
57 | ||
58 | assert(channel); | |
59 | ||
60 | DBG("UST consumer cleaning stream list"); | |
61 | ||
62 | cds_list_for_each_entry_safe(stream, stmp, &channel->streams.head, | |
63 | send_node) { | |
64 | cds_list_del(&stream->send_node); | |
65 | ustctl_destroy_stream(stream->ustream); | |
66 | free(stream); | |
67 | } | |
68 | ||
69 | /* | |
70 | * If a channel is available meaning that was created before the streams | |
71 | * were, delete it. | |
72 | */ | |
73 | if (channel->uchan) { | |
74 | lttng_ustconsumer_del_channel(channel); | |
75 | } | |
76 | free(channel); | |
77 | } | |
3bd1e081 MD |
78 | |
79 | /* | |
ffe60014 | 80 | * Add channel to internal consumer state. |
3bd1e081 | 81 | * |
ffe60014 | 82 | * Returns 0 on success or else a negative value. |
3bd1e081 | 83 | */ |
ffe60014 DG |
84 | static int add_channel(struct lttng_consumer_channel *channel, |
85 | struct lttng_consumer_local_data *ctx) | |
3bd1e081 MD |
86 | { |
87 | int ret = 0; | |
88 | ||
ffe60014 DG |
89 | assert(channel); |
90 | assert(ctx); | |
91 | ||
92 | if (ctx->on_recv_channel != NULL) { | |
93 | ret = ctx->on_recv_channel(channel); | |
94 | if (ret == 0) { | |
d8ef542d | 95 | ret = consumer_add_channel(channel, ctx); |
ffe60014 DG |
96 | } else if (ret < 0) { |
97 | /* Most likely an ENOMEM. */ | |
98 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR); | |
99 | goto error; | |
100 | } | |
101 | } else { | |
d8ef542d | 102 | ret = consumer_add_channel(channel, ctx); |
3bd1e081 MD |
103 | } |
104 | ||
d88aee68 | 105 | DBG("UST consumer channel added (key: %" PRIu64 ")", channel->key); |
ffe60014 DG |
106 | |
107 | error: | |
3bd1e081 MD |
108 | return ret; |
109 | } | |
110 | ||
111 | /* | |
ffe60014 DG |
112 | * Allocate and return a consumer channel object. |
113 | */ | |
114 | static struct lttng_consumer_channel *allocate_channel(uint64_t session_id, | |
115 | const char *pathname, const char *name, uid_t uid, gid_t gid, | |
1624d5b7 JD |
116 | int relayd_id, uint64_t key, enum lttng_event_output output, |
117 | uint64_t tracefile_size, uint64_t tracefile_count) | |
ffe60014 DG |
118 | { |
119 | assert(pathname); | |
120 | assert(name); | |
121 | ||
122 | return consumer_allocate_channel(key, session_id, pathname, name, uid, gid, | |
1624d5b7 | 123 | relayd_id, output, tracefile_size, tracefile_count); |
ffe60014 DG |
124 | } |
125 | ||
126 | /* | |
127 | * Allocate and return a consumer stream object. If _alloc_ret is not NULL, the | |
128 | * error value if applicable is set in it else it is kept untouched. | |
3bd1e081 | 129 | * |
ffe60014 | 130 | * Return NULL on error else the newly allocated stream object. |
3bd1e081 | 131 | */ |
ffe60014 DG |
132 | static struct lttng_consumer_stream *allocate_stream(int cpu, int key, |
133 | struct lttng_consumer_channel *channel, | |
134 | struct lttng_consumer_local_data *ctx, int *_alloc_ret) | |
135 | { | |
136 | int alloc_ret; | |
137 | struct lttng_consumer_stream *stream = NULL; | |
138 | ||
139 | assert(channel); | |
140 | assert(ctx); | |
141 | ||
142 | stream = consumer_allocate_stream(channel->key, | |
143 | key, | |
144 | LTTNG_CONSUMER_ACTIVE_STREAM, | |
145 | channel->name, | |
146 | channel->uid, | |
147 | channel->gid, | |
148 | channel->relayd_id, | |
149 | channel->session_id, | |
150 | cpu, | |
151 | &alloc_ret, | |
152 | channel->type); | |
153 | if (stream == NULL) { | |
154 | switch (alloc_ret) { | |
155 | case -ENOENT: | |
156 | /* | |
157 | * We could not find the channel. Can happen if cpu hotplug | |
158 | * happens while tearing down. | |
159 | */ | |
160 | DBG3("Could not find channel"); | |
161 | break; | |
162 | case -ENOMEM: | |
163 | case -EINVAL: | |
164 | default: | |
165 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR); | |
166 | break; | |
167 | } | |
168 | goto error; | |
169 | } | |
170 | ||
171 | stream->chan = channel; | |
172 | ||
173 | error: | |
174 | if (_alloc_ret) { | |
175 | *_alloc_ret = alloc_ret; | |
176 | } | |
177 | return stream; | |
178 | } | |
179 | ||
180 | /* | |
181 | * Send the given stream pointer to the corresponding thread. | |
182 | * | |
183 | * Returns 0 on success else a negative value. | |
184 | */ | |
185 | static int send_stream_to_thread(struct lttng_consumer_stream *stream, | |
186 | struct lttng_consumer_local_data *ctx) | |
187 | { | |
dae10966 DG |
188 | int ret; |
189 | struct lttng_pipe *stream_pipe; | |
ffe60014 DG |
190 | |
191 | /* Get the right pipe where the stream will be sent. */ | |
192 | if (stream->metadata_flag) { | |
dae10966 | 193 | stream_pipe = ctx->consumer_metadata_pipe; |
ffe60014 | 194 | } else { |
dae10966 | 195 | stream_pipe = ctx->consumer_data_pipe; |
ffe60014 DG |
196 | } |
197 | ||
dae10966 | 198 | ret = lttng_pipe_write(stream_pipe, &stream, sizeof(stream)); |
ffe60014 | 199 | if (ret < 0) { |
dae10966 DG |
200 | ERR("Consumer write %s stream to pipe %d", |
201 | stream->metadata_flag ? "metadata" : "data", | |
202 | lttng_pipe_get_writefd(stream_pipe)); | |
ffe60014 DG |
203 | } |
204 | ||
205 | return ret; | |
206 | } | |
207 | ||
208 | /* | |
209 | * Search for a relayd object related to the stream. If found, send the stream | |
210 | * to the relayd. | |
211 | * | |
212 | * On success, returns 0 else a negative value. | |
213 | */ | |
214 | static int send_stream_to_relayd(struct lttng_consumer_stream *stream) | |
215 | { | |
216 | int ret = 0; | |
217 | struct consumer_relayd_sock_pair *relayd; | |
218 | ||
219 | assert(stream); | |
220 | ||
221 | relayd = consumer_find_relayd(stream->net_seq_idx); | |
222 | if (relayd != NULL) { | |
223 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); | |
224 | /* Add stream on the relayd */ | |
225 | ret = relayd_add_stream(&relayd->control_sock, stream->name, | |
0f907de1 JD |
226 | stream->chan->pathname, &stream->relayd_stream_id, |
227 | stream->chan->tracefile_size, | |
228 | stream->chan->tracefile_count); | |
ffe60014 DG |
229 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); |
230 | if (ret < 0) { | |
231 | goto error; | |
232 | } | |
d88aee68 DG |
233 | } else if (stream->net_seq_idx != (uint64_t) -1ULL) { |
234 | ERR("Network sequence index %" PRIu64 " unknown. Not adding stream.", | |
ffe60014 DG |
235 | stream->net_seq_idx); |
236 | ret = -1; | |
237 | goto error; | |
238 | } | |
239 | ||
240 | error: | |
241 | return ret; | |
242 | } | |
243 | ||
d88aee68 DG |
244 | /* |
245 | * Create streams for the given channel using liblttng-ust-ctl. | |
246 | * | |
247 | * Return 0 on success else a negative value. | |
248 | */ | |
ffe60014 DG |
249 | static int create_ust_streams(struct lttng_consumer_channel *channel, |
250 | struct lttng_consumer_local_data *ctx) | |
251 | { | |
252 | int ret, cpu = 0; | |
253 | struct ustctl_consumer_stream *ustream; | |
254 | struct lttng_consumer_stream *stream; | |
255 | ||
256 | assert(channel); | |
257 | assert(ctx); | |
258 | ||
259 | /* | |
260 | * While a stream is available from ustctl. When NULL is returned, we've | |
261 | * reached the end of the possible stream for the channel. | |
262 | */ | |
263 | while ((ustream = ustctl_create_stream(channel->uchan, cpu))) { | |
264 | int wait_fd; | |
265 | ||
749d339a | 266 | wait_fd = ustctl_stream_get_wait_fd(ustream); |
ffe60014 DG |
267 | |
268 | /* Allocate consumer stream object. */ | |
269 | stream = allocate_stream(cpu, wait_fd, channel, ctx, &ret); | |
270 | if (!stream) { | |
271 | goto error_alloc; | |
272 | } | |
273 | stream->ustream = ustream; | |
274 | /* | |
275 | * Store it so we can save multiple function calls afterwards since | |
276 | * this value is used heavily in the stream threads. This is UST | |
277 | * specific so this is why it's done after allocation. | |
278 | */ | |
279 | stream->wait_fd = wait_fd; | |
280 | ||
b31398bb DG |
281 | /* |
282 | * Increment channel refcount since the channel reference has now been | |
283 | * assigned in the allocation process above. | |
284 | */ | |
285 | uatomic_inc(&stream->chan->refcount); | |
286 | ||
ffe60014 DG |
287 | /* |
288 | * Order is important this is why a list is used. On error, the caller | |
289 | * should clean this list. | |
290 | */ | |
291 | cds_list_add_tail(&stream->send_node, &channel->streams.head); | |
292 | ||
293 | ret = ustctl_get_max_subbuf_size(stream->ustream, | |
294 | &stream->max_sb_size); | |
295 | if (ret < 0) { | |
296 | ERR("ustctl_get_max_subbuf_size failed for stream %s", | |
297 | stream->name); | |
298 | goto error; | |
299 | } | |
300 | ||
301 | /* Do actions once stream has been received. */ | |
302 | if (ctx->on_recv_stream) { | |
303 | ret = ctx->on_recv_stream(stream); | |
304 | if (ret < 0) { | |
305 | goto error; | |
306 | } | |
307 | } | |
308 | ||
d88aee68 | 309 | DBG("UST consumer add stream %s (key: %" PRIu64 ") with relayd id %" PRIu64, |
ffe60014 DG |
310 | stream->name, stream->key, stream->relayd_stream_id); |
311 | ||
312 | /* Set next CPU stream. */ | |
313 | channel->streams.count = ++cpu; | |
d88aee68 DG |
314 | |
315 | /* Keep stream reference when creating metadata. */ | |
316 | if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA) { | |
317 | channel->metadata_stream = stream; | |
318 | } | |
ffe60014 DG |
319 | } |
320 | ||
321 | return 0; | |
322 | ||
323 | error: | |
324 | error_alloc: | |
325 | return ret; | |
326 | } | |
327 | ||
328 | /* | |
329 | * Create an UST channel with the given attributes and send it to the session | |
330 | * daemon using the ust ctl API. | |
331 | * | |
332 | * Return 0 on success or else a negative value. | |
333 | */ | |
334 | static int create_ust_channel(struct ustctl_consumer_channel_attr *attr, | |
335 | struct ustctl_consumer_channel **chanp) | |
336 | { | |
337 | int ret; | |
338 | struct ustctl_consumer_channel *channel; | |
339 | ||
340 | assert(attr); | |
341 | assert(chanp); | |
342 | ||
343 | DBG3("Creating channel to ustctl with attr: [overwrite: %d, " | |
344 | "subbuf_size: %" PRIu64 ", num_subbuf: %" PRIu64 ", " | |
345 | "switch_timer_interval: %u, read_timer_interval: %u, " | |
346 | "output: %d, type: %d", attr->overwrite, attr->subbuf_size, | |
347 | attr->num_subbuf, attr->switch_timer_interval, | |
348 | attr->read_timer_interval, attr->output, attr->type); | |
349 | ||
350 | channel = ustctl_create_channel(attr); | |
351 | if (!channel) { | |
352 | ret = -1; | |
353 | goto error_create; | |
354 | } | |
355 | ||
356 | *chanp = channel; | |
357 | ||
358 | return 0; | |
359 | ||
360 | error_create: | |
361 | return ret; | |
362 | } | |
363 | ||
d88aee68 DG |
364 | /* |
365 | * Send a single given stream to the session daemon using the sock. | |
366 | * | |
367 | * Return 0 on success else a negative value. | |
368 | */ | |
ffe60014 DG |
369 | static int send_sessiond_stream(int sock, struct lttng_consumer_stream *stream) |
370 | { | |
371 | int ret; | |
372 | ||
373 | assert(stream); | |
374 | assert(sock >= 0); | |
375 | ||
d88aee68 | 376 | DBG2("UST consumer sending stream %" PRIu64 " to sessiond", stream->key); |
ffe60014 DG |
377 | |
378 | /* Send stream to session daemon. */ | |
379 | ret = ustctl_send_stream_to_sessiond(sock, stream->ustream); | |
380 | if (ret < 0) { | |
381 | goto error; | |
382 | } | |
383 | ||
ffe60014 DG |
384 | error: |
385 | return ret; | |
386 | } | |
387 | ||
388 | /* | |
389 | * Send channel to sessiond. | |
390 | * | |
d88aee68 | 391 | * Return 0 on success or else a negative value. |
ffe60014 DG |
392 | */ |
393 | static int send_sessiond_channel(int sock, | |
394 | struct lttng_consumer_channel *channel, | |
395 | struct lttng_consumer_local_data *ctx, int *relayd_error) | |
396 | { | |
f2a444f1 | 397 | int ret, ret_code = LTTNG_OK; |
ffe60014 DG |
398 | struct lttng_consumer_stream *stream; |
399 | ||
400 | assert(channel); | |
401 | assert(ctx); | |
402 | assert(sock >= 0); | |
403 | ||
404 | DBG("UST consumer sending channel %s to sessiond", channel->name); | |
405 | ||
ffe60014 DG |
406 | cds_list_for_each_entry(stream, &channel->streams.head, send_node) { |
407 | /* Try to send the stream to the relayd if one is available. */ | |
408 | ret = send_stream_to_relayd(stream); | |
409 | if (ret < 0) { | |
410 | /* | |
411 | * Flag that the relayd was the problem here probably due to a | |
412 | * communicaton error on the socket. | |
413 | */ | |
414 | if (relayd_error) { | |
415 | *relayd_error = 1; | |
416 | } | |
f2a444f1 | 417 | ret_code = LTTNG_ERR_RELAYD_CONNECT_FAIL; |
ffe60014 | 418 | } |
f2a444f1 | 419 | } |
ffe60014 | 420 | |
f2a444f1 DG |
421 | /* Inform sessiond that we are about to send channel and streams. */ |
422 | ret = consumer_send_status_msg(sock, ret_code); | |
423 | if (ret < 0 || ret_code != LTTNG_OK) { | |
424 | /* | |
425 | * Either the session daemon is not responding or the relayd died so we | |
426 | * stop now. | |
427 | */ | |
428 | goto error; | |
429 | } | |
430 | ||
431 | /* Send channel to sessiond. */ | |
432 | ret = ustctl_send_channel_to_sessiond(sock, channel->uchan); | |
433 | if (ret < 0) { | |
434 | goto error; | |
435 | } | |
436 | ||
437 | ret = ustctl_channel_close_wakeup_fd(channel->uchan); | |
438 | if (ret < 0) { | |
439 | goto error; | |
440 | } | |
441 | ||
442 | /* The channel was sent successfully to the sessiond at this point. */ | |
443 | cds_list_for_each_entry(stream, &channel->streams.head, send_node) { | |
ffe60014 DG |
444 | /* Send stream to session daemon. */ |
445 | ret = send_sessiond_stream(sock, stream); | |
446 | if (ret < 0) { | |
447 | goto error; | |
448 | } | |
449 | } | |
450 | ||
451 | /* Tell sessiond there is no more stream. */ | |
452 | ret = ustctl_send_stream_to_sessiond(sock, NULL); | |
453 | if (ret < 0) { | |
454 | goto error; | |
455 | } | |
456 | ||
457 | DBG("UST consumer NULL stream sent to sessiond"); | |
458 | ||
459 | return 0; | |
460 | ||
461 | error: | |
f2a444f1 DG |
462 | if (ret_code != LTTNG_OK) { |
463 | ret = -1; | |
464 | } | |
ffe60014 DG |
465 | return ret; |
466 | } | |
467 | ||
468 | /* | |
469 | * Creates a channel and streams and add the channel it to the channel internal | |
470 | * state. The created stream must ONLY be sent once the GET_CHANNEL command is | |
471 | * received. | |
472 | * | |
473 | * Return 0 on success or else, a negative value is returned and the channel | |
474 | * MUST be destroyed by consumer_del_channel(). | |
475 | */ | |
476 | static int ask_channel(struct lttng_consumer_local_data *ctx, int sock, | |
477 | struct lttng_consumer_channel *channel, | |
478 | struct ustctl_consumer_channel_attr *attr) | |
3bd1e081 MD |
479 | { |
480 | int ret; | |
481 | ||
ffe60014 DG |
482 | assert(ctx); |
483 | assert(channel); | |
484 | assert(attr); | |
485 | ||
486 | /* | |
487 | * This value is still used by the kernel consumer since for the kernel, | |
488 | * the stream ownership is not IN the consumer so we need to have the | |
489 | * number of left stream that needs to be initialized so we can know when | |
490 | * to delete the channel (see consumer.c). | |
491 | * | |
492 | * As for the user space tracer now, the consumer creates and sends the | |
493 | * stream to the session daemon which only sends them to the application | |
494 | * once every stream of a channel is received making this value useless | |
495 | * because we they will be added to the poll thread before the application | |
496 | * receives them. This ensures that a stream can not hang up during | |
497 | * initilization of a channel. | |
498 | */ | |
499 | channel->nb_init_stream_left = 0; | |
500 | ||
501 | /* The reply msg status is handled in the following call. */ | |
502 | ret = create_ust_channel(attr, &channel->uchan); | |
503 | if (ret < 0) { | |
504 | goto error; | |
3bd1e081 MD |
505 | } |
506 | ||
d8ef542d MD |
507 | channel->wait_fd = ustctl_channel_get_wait_fd(channel->uchan); |
508 | ||
ffe60014 DG |
509 | /* Open all streams for this channel. */ |
510 | ret = create_ust_streams(channel, ctx); | |
511 | if (ret < 0) { | |
512 | goto error; | |
513 | } | |
514 | ||
515 | error: | |
3bd1e081 MD |
516 | return ret; |
517 | } | |
518 | ||
d88aee68 DG |
519 | /* |
520 | * Send all stream of a channel to the right thread handling it. | |
521 | * | |
522 | * On error, return a negative value else 0 on success. | |
523 | */ | |
524 | static int send_streams_to_thread(struct lttng_consumer_channel *channel, | |
525 | struct lttng_consumer_local_data *ctx) | |
526 | { | |
527 | int ret = 0; | |
528 | struct lttng_consumer_stream *stream, *stmp; | |
529 | ||
530 | assert(channel); | |
531 | assert(ctx); | |
532 | ||
533 | /* Send streams to the corresponding thread. */ | |
534 | cds_list_for_each_entry_safe(stream, stmp, &channel->streams.head, | |
535 | send_node) { | |
536 | /* Sending the stream to the thread. */ | |
537 | ret = send_stream_to_thread(stream, ctx); | |
538 | if (ret < 0) { | |
539 | /* | |
540 | * If we are unable to send the stream to the thread, there is | |
541 | * a big problem so just stop everything. | |
542 | */ | |
543 | goto error; | |
544 | } | |
545 | ||
546 | /* Remove node from the channel stream list. */ | |
547 | cds_list_del(&stream->send_node); | |
548 | } | |
549 | ||
550 | error: | |
551 | return ret; | |
552 | } | |
553 | ||
554 | /* | |
555 | * Write metadata to the given channel using ustctl to convert the string to | |
556 | * the ringbuffer. | |
331744e3 JD |
557 | * Called only from consumer_metadata_cache_write. |
558 | * The metadata cache lock MUST be acquired to write in the cache. | |
d88aee68 DG |
559 | * |
560 | * Return 0 on success else a negative value. | |
561 | */ | |
331744e3 | 562 | int lttng_ustconsumer_push_metadata(struct lttng_consumer_channel *metadata, |
d88aee68 DG |
563 | const char *metadata_str, uint64_t target_offset, uint64_t len) |
564 | { | |
565 | int ret; | |
566 | ||
567 | assert(metadata); | |
568 | assert(metadata_str); | |
569 | ||
570 | DBG("UST consumer writing metadata to channel %s", metadata->name); | |
571 | ||
73811ecc DG |
572 | if (!metadata->metadata_stream) { |
573 | ret = 0; | |
574 | goto error; | |
575 | } | |
576 | ||
331744e3 JD |
577 | assert(target_offset <= metadata->metadata_cache->max_offset); |
578 | ret = ustctl_write_metadata_to_channel(metadata->uchan, | |
579 | metadata_str + target_offset, len); | |
d88aee68 | 580 | if (ret < 0) { |
8fd623e0 | 581 | ERR("ustctl write metadata fail with ret %d, len %" PRIu64, ret, len); |
d88aee68 DG |
582 | goto error; |
583 | } | |
d88aee68 DG |
584 | |
585 | ustctl_flush_buffer(metadata->metadata_stream->ustream, 1); | |
586 | ||
587 | error: | |
588 | return ret; | |
589 | } | |
590 | ||
7972aab2 DG |
591 | /* |
592 | * Flush channel's streams using the given key to retrieve the channel. | |
593 | * | |
594 | * Return 0 on success else an LTTng error code. | |
595 | */ | |
596 | static int flush_channel(uint64_t chan_key) | |
597 | { | |
598 | int ret = 0; | |
599 | struct lttng_consumer_channel *channel; | |
600 | struct lttng_consumer_stream *stream; | |
601 | struct lttng_ht *ht; | |
602 | struct lttng_ht_iter iter; | |
603 | ||
8fd623e0 | 604 | DBG("UST consumer flush channel key %" PRIu64, chan_key); |
7972aab2 | 605 | |
a500c257 | 606 | rcu_read_lock(); |
7972aab2 DG |
607 | channel = consumer_find_channel(chan_key); |
608 | if (!channel) { | |
8fd623e0 | 609 | ERR("UST consumer flush channel %" PRIu64 " not found", chan_key); |
7972aab2 DG |
610 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
611 | goto error; | |
612 | } | |
613 | ||
614 | ht = consumer_data.stream_per_chan_id_ht; | |
615 | ||
616 | /* For each stream of the channel id, flush it. */ | |
7972aab2 DG |
617 | cds_lfht_for_each_entry_duplicate(ht->ht, |
618 | ht->hash_fct(&channel->key, lttng_ht_seed), ht->match_fct, | |
619 | &channel->key, &iter.iter, stream, node_channel_id.node) { | |
620 | ustctl_flush_buffer(stream->ustream, 1); | |
621 | } | |
7972aab2 | 622 | error: |
a500c257 | 623 | rcu_read_unlock(); |
7972aab2 DG |
624 | return ret; |
625 | } | |
626 | ||
d88aee68 DG |
627 | /* |
628 | * Close metadata stream wakeup_fd using the given key to retrieve the channel. | |
a500c257 | 629 | * RCU read side lock MUST be acquired before calling this function. |
d88aee68 DG |
630 | * |
631 | * Return 0 on success else an LTTng error code. | |
632 | */ | |
633 | static int close_metadata(uint64_t chan_key) | |
634 | { | |
ea88ca2a | 635 | int ret = 0; |
d88aee68 DG |
636 | struct lttng_consumer_channel *channel; |
637 | ||
8fd623e0 | 638 | DBG("UST consumer close metadata key %" PRIu64, chan_key); |
d88aee68 DG |
639 | |
640 | channel = consumer_find_channel(chan_key); | |
641 | if (!channel) { | |
84cc9aa0 DG |
642 | /* |
643 | * This is possible if the metadata thread has issue a delete because | |
644 | * the endpoint point of the stream hung up. There is no way the | |
645 | * session daemon can know about it thus use a DBG instead of an actual | |
646 | * error. | |
647 | */ | |
648 | DBG("UST consumer close metadata %" PRIu64 " not found", chan_key); | |
d88aee68 DG |
649 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
650 | goto error; | |
651 | } | |
652 | ||
ea88ca2a | 653 | pthread_mutex_lock(&consumer_data.lock); |
73811ecc DG |
654 | |
655 | if (cds_lfht_is_node_deleted(&channel->node.node)) { | |
656 | goto error_unlock; | |
657 | } | |
658 | ||
659 | if (channel->switch_timer_enabled == 1) { | |
660 | DBG("Deleting timer on metadata channel"); | |
661 | consumer_timer_switch_stop(channel); | |
662 | } | |
663 | ||
664 | if (channel->metadata_stream) { | |
ea88ca2a MD |
665 | ret = ustctl_stream_close_wakeup_fd(channel->metadata_stream->ustream); |
666 | if (ret < 0) { | |
667 | ERR("UST consumer unable to close fd of metadata (ret: %d)", ret); | |
668 | ret = LTTCOMM_CONSUMERD_ERROR_METADATA; | |
669 | goto error_unlock; | |
670 | } | |
331744e3 | 671 | } |
d88aee68 | 672 | |
ea88ca2a MD |
673 | error_unlock: |
674 | pthread_mutex_unlock(&consumer_data.lock); | |
d88aee68 DG |
675 | error: |
676 | return ret; | |
677 | } | |
678 | ||
679 | /* | |
680 | * RCU read side lock MUST be acquired before calling this function. | |
681 | * | |
682 | * Return 0 on success else an LTTng error code. | |
683 | */ | |
684 | static int setup_metadata(struct lttng_consumer_local_data *ctx, uint64_t key) | |
685 | { | |
686 | int ret; | |
687 | struct lttng_consumer_channel *metadata; | |
688 | ||
8fd623e0 | 689 | DBG("UST consumer setup metadata key %" PRIu64, key); |
d88aee68 DG |
690 | |
691 | metadata = consumer_find_channel(key); | |
692 | if (!metadata) { | |
693 | ERR("UST consumer push metadata %" PRIu64 " not found", key); | |
694 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; | |
f2a444f1 | 695 | goto error_find; |
d88aee68 DG |
696 | } |
697 | ||
698 | /* | |
699 | * Send metadata stream to relayd if one available. Availability is | |
700 | * known if the stream is still in the list of the channel. | |
701 | */ | |
702 | if (cds_list_empty(&metadata->streams.head)) { | |
703 | ERR("Metadata channel key %" PRIu64 ", no stream available.", key); | |
704 | ret = LTTCOMM_CONSUMERD_ERROR_METADATA; | |
705 | goto error; | |
706 | } | |
707 | ||
708 | /* Send metadata stream to relayd if needed. */ | |
709 | ret = send_stream_to_relayd(metadata->metadata_stream); | |
710 | if (ret < 0) { | |
711 | ret = LTTCOMM_CONSUMERD_ERROR_METADATA; | |
712 | goto error; | |
713 | } | |
714 | ||
715 | ret = send_streams_to_thread(metadata, ctx); | |
716 | if (ret < 0) { | |
717 | /* | |
718 | * If we are unable to send the stream to the thread, there is | |
719 | * a big problem so just stop everything. | |
720 | */ | |
721 | ret = LTTCOMM_CONSUMERD_FATAL; | |
722 | goto error; | |
723 | } | |
724 | /* List MUST be empty after or else it could be reused. */ | |
725 | assert(cds_list_empty(&metadata->streams.head)); | |
726 | ||
f2a444f1 | 727 | return 0; |
d88aee68 DG |
728 | |
729 | error: | |
f2a444f1 DG |
730 | /* |
731 | * Delete metadata channel on error. At this point, the metadata stream can | |
732 | * NOT be monitored by the metadata thread thus having the guarantee that | |
733 | * the stream is still in the local stream list of the channel. This call | |
734 | * will make sure to clean that list. | |
735 | */ | |
736 | consumer_del_channel(metadata); | |
737 | error_find: | |
d88aee68 DG |
738 | return ret; |
739 | } | |
740 | ||
331744e3 JD |
741 | /* |
742 | * Receive the metadata updates from the sessiond. | |
743 | */ | |
744 | int lttng_ustconsumer_recv_metadata(int sock, uint64_t key, uint64_t offset, | |
745 | uint64_t len, struct lttng_consumer_channel *channel) | |
746 | { | |
747 | int ret, ret_code = LTTNG_OK; | |
748 | char *metadata_str; | |
749 | ||
8fd623e0 | 750 | DBG("UST consumer push metadata key %" PRIu64 " of len %" PRIu64, key, len); |
331744e3 JD |
751 | |
752 | metadata_str = zmalloc(len * sizeof(char)); | |
753 | if (!metadata_str) { | |
754 | PERROR("zmalloc metadata string"); | |
755 | ret_code = LTTCOMM_CONSUMERD_ENOMEM; | |
756 | goto end; | |
757 | } | |
758 | ||
759 | /* Receive metadata string. */ | |
760 | ret = lttcomm_recv_unix_sock(sock, metadata_str, len); | |
761 | if (ret < 0) { | |
762 | /* Session daemon is dead so return gracefully. */ | |
763 | ret_code = ret; | |
764 | goto end_free; | |
765 | } | |
766 | ||
73811ecc DG |
767 | /* |
768 | * XXX: The consumer data lock is acquired before calling metadata cache | |
769 | * write which calls push metadata that MUST be protected by the consumer | |
770 | * lock in order to be able to check the validity of the metadata stream of | |
771 | * the channel. | |
772 | * | |
773 | * Note that this will be subject to change to better fine grained locking | |
774 | * and ultimately try to get rid of this global consumer data lock. | |
775 | */ | |
776 | pthread_mutex_lock(&consumer_data.lock); | |
777 | ||
331744e3 JD |
778 | pthread_mutex_lock(&channel->metadata_cache->lock); |
779 | ret = consumer_metadata_cache_write(channel, offset, len, metadata_str); | |
780 | if (ret < 0) { | |
781 | /* Unable to handle metadata. Notify session daemon. */ | |
782 | ret_code = LTTCOMM_CONSUMERD_ERROR_METADATA; | |
a32bd775 DG |
783 | /* |
784 | * Skip metadata flush on write error since the offset and len might | |
785 | * not have been updated which could create an infinite loop below when | |
786 | * waiting for the metadata cache to be flushed. | |
787 | */ | |
788 | pthread_mutex_unlock(&channel->metadata_cache->lock); | |
789 | pthread_mutex_unlock(&consumer_data.lock); | |
790 | goto end_free; | |
331744e3 JD |
791 | } |
792 | pthread_mutex_unlock(&channel->metadata_cache->lock); | |
73811ecc | 793 | pthread_mutex_unlock(&consumer_data.lock); |
331744e3 JD |
794 | |
795 | while (consumer_metadata_cache_flushed(channel, offset + len)) { | |
796 | DBG("Waiting for metadata to be flushed"); | |
797 | usleep(DEFAULT_METADATA_AVAILABILITY_WAIT_TIME); | |
798 | } | |
799 | ||
800 | end_free: | |
801 | free(metadata_str); | |
802 | end: | |
803 | return ret_code; | |
804 | } | |
805 | ||
4cbc1a04 DG |
806 | /* |
807 | * Receive command from session daemon and process it. | |
808 | * | |
809 | * Return 1 on success else a negative value or 0. | |
810 | */ | |
3bd1e081 MD |
811 | int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data *ctx, |
812 | int sock, struct pollfd *consumer_sockpoll) | |
813 | { | |
814 | ssize_t ret; | |
f50f23d9 | 815 | enum lttng_error_code ret_code = LTTNG_OK; |
3bd1e081 | 816 | struct lttcomm_consumer_msg msg; |
ffe60014 | 817 | struct lttng_consumer_channel *channel = NULL; |
3bd1e081 MD |
818 | |
819 | ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg)); | |
820 | if (ret != sizeof(msg)) { | |
173af62f DG |
821 | DBG("Consumer received unexpected message size %zd (expects %zu)", |
822 | ret, sizeof(msg)); | |
ffe60014 | 823 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD); |
3be74084 DG |
824 | /* |
825 | * The ret value might 0 meaning an orderly shutdown but this is ok | |
826 | * since the caller handles this. | |
827 | */ | |
489f70e9 MD |
828 | if (ret > 0) { |
829 | ret = -1; | |
830 | } | |
3bd1e081 MD |
831 | return ret; |
832 | } | |
833 | if (msg.cmd_type == LTTNG_CONSUMER_STOP) { | |
f50f23d9 DG |
834 | /* |
835 | * Notify the session daemon that the command is completed. | |
836 | * | |
837 | * On transport layer error, the function call will print an error | |
838 | * message so handling the returned code is a bit useless since we | |
839 | * return an error code anyway. | |
840 | */ | |
841 | (void) consumer_send_status_msg(sock, ret_code); | |
3bd1e081 MD |
842 | return -ENOENT; |
843 | } | |
844 | ||
3f8e211f | 845 | /* relayd needs RCU read-side lock */ |
b0b335c8 MD |
846 | rcu_read_lock(); |
847 | ||
3bd1e081 | 848 | switch (msg.cmd_type) { |
00e2e675 DG |
849 | case LTTNG_CONSUMER_ADD_RELAYD_SOCKET: |
850 | { | |
f50f23d9 | 851 | /* Session daemon status message are handled in the following call. */ |
7735ef9e DG |
852 | ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index, |
853 | msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll, | |
46e6455f | 854 | &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id); |
00e2e675 DG |
855 | goto end_nosignal; |
856 | } | |
173af62f DG |
857 | case LTTNG_CONSUMER_DESTROY_RELAYD: |
858 | { | |
a6ba4fe1 | 859 | uint64_t index = msg.u.destroy_relayd.net_seq_idx; |
173af62f DG |
860 | struct consumer_relayd_sock_pair *relayd; |
861 | ||
a6ba4fe1 | 862 | DBG("UST consumer destroying relayd %" PRIu64, index); |
173af62f DG |
863 | |
864 | /* Get relayd reference if exists. */ | |
a6ba4fe1 | 865 | relayd = consumer_find_relayd(index); |
173af62f | 866 | if (relayd == NULL) { |
3448e266 | 867 | DBG("Unable to find relayd %" PRIu64, index); |
f50f23d9 | 868 | ret_code = LTTNG_ERR_NO_CONSUMER; |
173af62f DG |
869 | } |
870 | ||
a6ba4fe1 DG |
871 | /* |
872 | * Each relayd socket pair has a refcount of stream attached to it | |
873 | * which tells if the relayd is still active or not depending on the | |
874 | * refcount value. | |
875 | * | |
876 | * This will set the destroy flag of the relayd object and destroy it | |
877 | * if the refcount reaches zero when called. | |
878 | * | |
879 | * The destroy can happen either here or when a stream fd hangs up. | |
880 | */ | |
f50f23d9 DG |
881 | if (relayd) { |
882 | consumer_flag_relayd_for_destroy(relayd); | |
883 | } | |
884 | ||
d88aee68 | 885 | goto end_msg_sessiond; |
173af62f | 886 | } |
3bd1e081 MD |
887 | case LTTNG_CONSUMER_UPDATE_STREAM: |
888 | { | |
3f8e211f | 889 | rcu_read_unlock(); |
7ad0a0cb | 890 | return -ENOSYS; |
3bd1e081 | 891 | } |
6d805429 | 892 | case LTTNG_CONSUMER_DATA_PENDING: |
53632229 | 893 | { |
3be74084 | 894 | int ret, is_data_pending; |
6d805429 | 895 | uint64_t id = msg.u.data_pending.session_id; |
ca22feea | 896 | |
6d805429 | 897 | DBG("UST consumer data pending command for id %" PRIu64, id); |
ca22feea | 898 | |
3be74084 | 899 | is_data_pending = consumer_data_pending(id); |
ca22feea DG |
900 | |
901 | /* Send back returned value to session daemon */ | |
3be74084 DG |
902 | ret = lttcomm_send_unix_sock(sock, &is_data_pending, |
903 | sizeof(is_data_pending)); | |
ca22feea | 904 | if (ret < 0) { |
3be74084 | 905 | DBG("Error when sending the data pending ret code: %d", ret); |
489f70e9 | 906 | goto error_fatal; |
ca22feea | 907 | } |
f50f23d9 DG |
908 | |
909 | /* | |
910 | * No need to send back a status message since the data pending | |
911 | * returned value is the response. | |
912 | */ | |
ca22feea | 913 | break; |
53632229 | 914 | } |
ffe60014 DG |
915 | case LTTNG_CONSUMER_ASK_CHANNEL_CREATION: |
916 | { | |
917 | int ret; | |
918 | struct ustctl_consumer_channel_attr attr; | |
919 | ||
920 | /* Create a plain object and reserve a channel key. */ | |
921 | channel = allocate_channel(msg.u.ask_channel.session_id, | |
922 | msg.u.ask_channel.pathname, msg.u.ask_channel.name, | |
923 | msg.u.ask_channel.uid, msg.u.ask_channel.gid, | |
924 | msg.u.ask_channel.relayd_id, msg.u.ask_channel.key, | |
1624d5b7 JD |
925 | (enum lttng_event_output) msg.u.ask_channel.output, |
926 | msg.u.ask_channel.tracefile_size, | |
927 | msg.u.ask_channel.tracefile_count); | |
ffe60014 DG |
928 | if (!channel) { |
929 | goto end_channel_error; | |
930 | } | |
931 | ||
932 | /* Build channel attributes from received message. */ | |
933 | attr.subbuf_size = msg.u.ask_channel.subbuf_size; | |
934 | attr.num_subbuf = msg.u.ask_channel.num_subbuf; | |
935 | attr.overwrite = msg.u.ask_channel.overwrite; | |
936 | attr.switch_timer_interval = msg.u.ask_channel.switch_timer_interval; | |
937 | attr.read_timer_interval = msg.u.ask_channel.read_timer_interval; | |
7972aab2 | 938 | attr.chan_id = msg.u.ask_channel.chan_id; |
ffe60014 DG |
939 | memcpy(attr.uuid, msg.u.ask_channel.uuid, sizeof(attr.uuid)); |
940 | ||
941 | /* Translate the event output type to UST. */ | |
942 | switch (channel->output) { | |
943 | case LTTNG_EVENT_SPLICE: | |
944 | /* Splice not supported so fallback on mmap(). */ | |
945 | case LTTNG_EVENT_MMAP: | |
946 | default: | |
947 | attr.output = CONSUMER_CHANNEL_MMAP; | |
948 | break; | |
949 | }; | |
950 | ||
951 | /* Translate and save channel type. */ | |
952 | switch (msg.u.ask_channel.type) { | |
953 | case LTTNG_UST_CHAN_PER_CPU: | |
954 | channel->type = CONSUMER_CHANNEL_TYPE_DATA; | |
955 | attr.type = LTTNG_UST_CHAN_PER_CPU; | |
8633d6e3 MD |
956 | /* |
957 | * Set refcount to 1 for owner. Below, we will | |
958 | * pass ownership to the | |
959 | * consumer_thread_channel_poll() thread. | |
960 | */ | |
961 | channel->refcount = 1; | |
ffe60014 DG |
962 | break; |
963 | case LTTNG_UST_CHAN_METADATA: | |
964 | channel->type = CONSUMER_CHANNEL_TYPE_METADATA; | |
965 | attr.type = LTTNG_UST_CHAN_METADATA; | |
966 | break; | |
967 | default: | |
968 | assert(0); | |
969 | goto error_fatal; | |
970 | }; | |
971 | ||
972 | ret = ask_channel(ctx, sock, channel, &attr); | |
973 | if (ret < 0) { | |
974 | goto end_channel_error; | |
975 | } | |
976 | ||
fc643247 MD |
977 | if (msg.u.ask_channel.type == LTTNG_UST_CHAN_METADATA) { |
978 | ret = consumer_metadata_cache_allocate(channel); | |
979 | if (ret < 0) { | |
980 | ERR("Allocating metadata cache"); | |
981 | goto end_channel_error; | |
982 | } | |
983 | consumer_timer_switch_start(channel, attr.switch_timer_interval); | |
984 | attr.switch_timer_interval = 0; | |
985 | } | |
986 | ||
ffe60014 DG |
987 | /* |
988 | * Add the channel to the internal state AFTER all streams were created | |
989 | * and successfully sent to session daemon. This way, all streams must | |
990 | * be ready before this channel is visible to the threads. | |
fc643247 MD |
991 | * If add_channel succeeds, ownership of the channel is |
992 | * passed to consumer_thread_channel_poll(). | |
ffe60014 DG |
993 | */ |
994 | ret = add_channel(channel, ctx); | |
995 | if (ret < 0) { | |
ea88ca2a MD |
996 | if (msg.u.ask_channel.type == LTTNG_UST_CHAN_METADATA) { |
997 | if (channel->switch_timer_enabled == 1) { | |
998 | consumer_timer_switch_stop(channel); | |
999 | } | |
1000 | consumer_metadata_cache_destroy(channel); | |
1001 | } | |
ffe60014 DG |
1002 | goto end_channel_error; |
1003 | } | |
1004 | ||
1005 | /* | |
1006 | * Channel and streams are now created. Inform the session daemon that | |
1007 | * everything went well and should wait to receive the channel and | |
1008 | * streams with ustctl API. | |
1009 | */ | |
1010 | ret = consumer_send_status_channel(sock, channel); | |
1011 | if (ret < 0) { | |
1012 | /* | |
489f70e9 | 1013 | * There is probably a problem on the socket. |
ffe60014 | 1014 | */ |
489f70e9 | 1015 | goto error_fatal; |
ffe60014 DG |
1016 | } |
1017 | ||
1018 | break; | |
1019 | } | |
1020 | case LTTNG_CONSUMER_GET_CHANNEL: | |
1021 | { | |
1022 | int ret, relayd_err = 0; | |
d88aee68 | 1023 | uint64_t key = msg.u.get_channel.key; |
ffe60014 | 1024 | struct lttng_consumer_channel *channel; |
ffe60014 DG |
1025 | |
1026 | channel = consumer_find_channel(key); | |
1027 | if (!channel) { | |
8fd623e0 | 1028 | ERR("UST consumer get channel key %" PRIu64 " not found", key); |
ffe60014 DG |
1029 | ret_code = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
1030 | goto end_msg_sessiond; | |
1031 | } | |
1032 | ||
ffe60014 DG |
1033 | /* Send everything to sessiond. */ |
1034 | ret = send_sessiond_channel(sock, channel, ctx, &relayd_err); | |
1035 | if (ret < 0) { | |
1036 | if (relayd_err) { | |
1037 | /* | |
1038 | * We were unable to send to the relayd the stream so avoid | |
1039 | * sending back a fatal error to the thread since this is OK | |
f2a444f1 DG |
1040 | * and the consumer can continue its work. The above call |
1041 | * has sent the error status message to the sessiond. | |
ffe60014 | 1042 | */ |
f2a444f1 | 1043 | goto end_nosignal; |
ffe60014 DG |
1044 | } |
1045 | /* | |
1046 | * The communicaton was broken hence there is a bad state between | |
1047 | * the consumer and sessiond so stop everything. | |
1048 | */ | |
1049 | goto error_fatal; | |
1050 | } | |
1051 | ||
d88aee68 DG |
1052 | ret = send_streams_to_thread(channel, ctx); |
1053 | if (ret < 0) { | |
1054 | /* | |
1055 | * If we are unable to send the stream to the thread, there is | |
1056 | * a big problem so just stop everything. | |
1057 | */ | |
1058 | goto error_fatal; | |
ffe60014 | 1059 | } |
ffe60014 DG |
1060 | /* List MUST be empty after or else it could be reused. */ |
1061 | assert(cds_list_empty(&channel->streams.head)); | |
1062 | ||
d88aee68 DG |
1063 | goto end_msg_sessiond; |
1064 | } | |
1065 | case LTTNG_CONSUMER_DESTROY_CHANNEL: | |
1066 | { | |
1067 | uint64_t key = msg.u.destroy_channel.key; | |
d88aee68 | 1068 | |
a0cbdd2e MD |
1069 | /* |
1070 | * Only called if streams have not been sent to stream | |
1071 | * manager thread. However, channel has been sent to | |
1072 | * channel manager thread. | |
1073 | */ | |
1074 | notify_thread_del_channel(ctx, key); | |
d88aee68 | 1075 | goto end_msg_sessiond; |
ffe60014 | 1076 | } |
d88aee68 DG |
1077 | case LTTNG_CONSUMER_CLOSE_METADATA: |
1078 | { | |
1079 | int ret; | |
1080 | ||
1081 | ret = close_metadata(msg.u.close_metadata.key); | |
1082 | if (ret != 0) { | |
1083 | ret_code = ret; | |
1084 | } | |
1085 | ||
1086 | goto end_msg_sessiond; | |
1087 | } | |
7972aab2 DG |
1088 | case LTTNG_CONSUMER_FLUSH_CHANNEL: |
1089 | { | |
1090 | int ret; | |
1091 | ||
1092 | ret = flush_channel(msg.u.flush_channel.key); | |
1093 | if (ret != 0) { | |
1094 | ret_code = ret; | |
1095 | } | |
1096 | ||
1097 | goto end_msg_sessiond; | |
1098 | } | |
d88aee68 | 1099 | case LTTNG_CONSUMER_PUSH_METADATA: |
ffe60014 DG |
1100 | { |
1101 | int ret; | |
d88aee68 | 1102 | uint64_t len = msg.u.push_metadata.len; |
d88aee68 | 1103 | uint64_t key = msg.u.push_metadata.key; |
331744e3 | 1104 | uint64_t offset = msg.u.push_metadata.target_offset; |
ffe60014 DG |
1105 | struct lttng_consumer_channel *channel; |
1106 | ||
8fd623e0 DG |
1107 | DBG("UST consumer push metadata key %" PRIu64 " of len %" PRIu64, key, |
1108 | len); | |
ffe60014 DG |
1109 | |
1110 | channel = consumer_find_channel(key); | |
1111 | if (!channel) { | |
8fd623e0 | 1112 | ERR("UST consumer push metadata %" PRIu64 " not found", key); |
ffe60014 | 1113 | ret_code = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
4a2eb0ca | 1114 | goto end_msg_sessiond; |
d88aee68 DG |
1115 | } |
1116 | ||
1117 | /* Tell session daemon we are ready to receive the metadata. */ | |
ffe60014 DG |
1118 | ret = consumer_send_status_msg(sock, LTTNG_OK); |
1119 | if (ret < 0) { | |
1120 | /* Somehow, the session daemon is not responding anymore. */ | |
d88aee68 DG |
1121 | goto error_fatal; |
1122 | } | |
1123 | ||
1124 | /* Wait for more data. */ | |
1125 | if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) { | |
489f70e9 | 1126 | goto error_fatal; |
d88aee68 DG |
1127 | } |
1128 | ||
331744e3 JD |
1129 | ret = lttng_ustconsumer_recv_metadata(sock, key, offset, |
1130 | len, channel); | |
d88aee68 | 1131 | if (ret < 0) { |
331744e3 | 1132 | /* error receiving from sessiond */ |
489f70e9 | 1133 | goto error_fatal; |
331744e3 JD |
1134 | } else { |
1135 | ret_code = ret; | |
d88aee68 DG |
1136 | goto end_msg_sessiond; |
1137 | } | |
d88aee68 DG |
1138 | } |
1139 | case LTTNG_CONSUMER_SETUP_METADATA: | |
1140 | { | |
1141 | int ret; | |
1142 | ||
1143 | ret = setup_metadata(ctx, msg.u.setup_metadata.key); | |
1144 | if (ret) { | |
1145 | ret_code = ret; | |
1146 | } | |
1147 | goto end_msg_sessiond; | |
ffe60014 | 1148 | } |
3bd1e081 MD |
1149 | default: |
1150 | break; | |
1151 | } | |
3f8e211f | 1152 | |
3bd1e081 | 1153 | end_nosignal: |
b0b335c8 | 1154 | rcu_read_unlock(); |
4cbc1a04 DG |
1155 | |
1156 | /* | |
1157 | * Return 1 to indicate success since the 0 value can be a socket | |
1158 | * shutdown during the recv() or send() call. | |
1159 | */ | |
1160 | return 1; | |
ffe60014 DG |
1161 | |
1162 | end_msg_sessiond: | |
1163 | /* | |
1164 | * The returned value here is not useful since either way we'll return 1 to | |
1165 | * the caller because the session daemon socket management is done | |
1166 | * elsewhere. Returning a negative code or 0 will shutdown the consumer. | |
1167 | */ | |
489f70e9 MD |
1168 | ret = consumer_send_status_msg(sock, ret_code); |
1169 | if (ret < 0) { | |
1170 | goto error_fatal; | |
1171 | } | |
ffe60014 DG |
1172 | rcu_read_unlock(); |
1173 | return 1; | |
1174 | end_channel_error: | |
1175 | if (channel) { | |
1176 | /* | |
1177 | * Free channel here since no one has a reference to it. We don't | |
1178 | * free after that because a stream can store this pointer. | |
1179 | */ | |
1180 | destroy_channel(channel); | |
1181 | } | |
1182 | /* We have to send a status channel message indicating an error. */ | |
1183 | ret = consumer_send_status_channel(sock, NULL); | |
1184 | if (ret < 0) { | |
1185 | /* Stop everything if session daemon can not be notified. */ | |
1186 | goto error_fatal; | |
1187 | } | |
1188 | rcu_read_unlock(); | |
1189 | return 1; | |
1190 | error_fatal: | |
1191 | rcu_read_unlock(); | |
1192 | /* This will issue a consumer stop. */ | |
1193 | return -1; | |
3bd1e081 MD |
1194 | } |
1195 | ||
ffe60014 DG |
1196 | /* |
1197 | * Wrapper over the mmap() read offset from ust-ctl library. Since this can be | |
1198 | * compiled out, we isolate it in this library. | |
1199 | */ | |
1200 | int lttng_ustctl_get_mmap_read_offset(struct lttng_consumer_stream *stream, | |
1201 | unsigned long *off) | |
3bd1e081 | 1202 | { |
ffe60014 DG |
1203 | assert(stream); |
1204 | assert(stream->ustream); | |
b5c5fc29 | 1205 | |
ffe60014 | 1206 | return ustctl_get_mmap_read_offset(stream->ustream, off); |
3bd1e081 MD |
1207 | } |
1208 | ||
ffe60014 DG |
1209 | /* |
1210 | * Wrapper over the mmap() read offset from ust-ctl library. Since this can be | |
1211 | * compiled out, we isolate it in this library. | |
1212 | */ | |
1213 | void *lttng_ustctl_get_mmap_base(struct lttng_consumer_stream *stream) | |
d056b477 | 1214 | { |
ffe60014 DG |
1215 | assert(stream); |
1216 | assert(stream->ustream); | |
1217 | ||
1218 | return ustctl_get_mmap_base(stream->ustream); | |
d056b477 MD |
1219 | } |
1220 | ||
ffe60014 DG |
1221 | /* |
1222 | * Take a snapshot for a specific fd | |
1223 | * | |
1224 | * Returns 0 on success, < 0 on error | |
1225 | */ | |
1226 | int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream *stream) | |
3bd1e081 | 1227 | { |
ffe60014 DG |
1228 | assert(stream); |
1229 | assert(stream->ustream); | |
1230 | ||
1231 | return ustctl_snapshot(stream->ustream); | |
3bd1e081 MD |
1232 | } |
1233 | ||
ffe60014 DG |
1234 | /* |
1235 | * Get the produced position | |
1236 | * | |
1237 | * Returns 0 on success, < 0 on error | |
1238 | */ | |
1239 | int lttng_ustconsumer_get_produced_snapshot( | |
1240 | struct lttng_consumer_stream *stream, unsigned long *pos) | |
3bd1e081 | 1241 | { |
ffe60014 DG |
1242 | assert(stream); |
1243 | assert(stream->ustream); | |
1244 | assert(pos); | |
7a57cf92 | 1245 | |
ffe60014 DG |
1246 | return ustctl_snapshot_get_produced(stream->ustream, pos); |
1247 | } | |
7a57cf92 | 1248 | |
ffe60014 DG |
1249 | /* |
1250 | * Called when the stream signal the consumer that it has hang up. | |
1251 | */ | |
1252 | void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream *stream) | |
1253 | { | |
1254 | assert(stream); | |
1255 | assert(stream->ustream); | |
2c1dd183 | 1256 | |
ffe60014 DG |
1257 | ustctl_flush_buffer(stream->ustream, 0); |
1258 | stream->hangup_flush_done = 1; | |
1259 | } | |
ee77a7b0 | 1260 | |
ffe60014 DG |
1261 | void lttng_ustconsumer_del_channel(struct lttng_consumer_channel *chan) |
1262 | { | |
1263 | assert(chan); | |
1264 | assert(chan->uchan); | |
e316aad5 | 1265 | |
ea88ca2a MD |
1266 | if (chan->switch_timer_enabled == 1) { |
1267 | consumer_timer_switch_stop(chan); | |
1268 | } | |
1269 | consumer_metadata_cache_destroy(chan); | |
ffe60014 | 1270 | ustctl_destroy_channel(chan->uchan); |
3bd1e081 MD |
1271 | } |
1272 | ||
1273 | void lttng_ustconsumer_del_stream(struct lttng_consumer_stream *stream) | |
1274 | { | |
ffe60014 DG |
1275 | assert(stream); |
1276 | assert(stream->ustream); | |
d41f73b7 | 1277 | |
ea88ca2a MD |
1278 | if (stream->chan->switch_timer_enabled == 1) { |
1279 | consumer_timer_switch_stop(stream->chan); | |
1280 | } | |
ffe60014 DG |
1281 | ustctl_destroy_stream(stream->ustream); |
1282 | } | |
d41f73b7 MD |
1283 | |
1284 | int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream *stream, | |
1285 | struct lttng_consumer_local_data *ctx) | |
1286 | { | |
1d4dfdef | 1287 | unsigned long len, subbuf_size, padding; |
d41f73b7 MD |
1288 | int err; |
1289 | long ret = 0; | |
d41f73b7 | 1290 | char dummy; |
ffe60014 DG |
1291 | struct ustctl_consumer_stream *ustream; |
1292 | ||
1293 | assert(stream); | |
1294 | assert(stream->ustream); | |
1295 | assert(ctx); | |
d41f73b7 | 1296 | |
ffe60014 DG |
1297 | DBG2("In UST read_subbuffer (wait_fd: %d, name: %s)", stream->wait_fd, |
1298 | stream->name); | |
1299 | ||
1300 | /* Ease our life for what's next. */ | |
1301 | ustream = stream->ustream; | |
d41f73b7 MD |
1302 | |
1303 | /* We can consume the 1 byte written into the wait_fd by UST */ | |
effcf122 | 1304 | if (!stream->hangup_flush_done) { |
c617c0c6 MD |
1305 | ssize_t readlen; |
1306 | ||
effcf122 MD |
1307 | do { |
1308 | readlen = read(stream->wait_fd, &dummy, 1); | |
87dc6a9c | 1309 | } while (readlen == -1 && errno == EINTR); |
effcf122 MD |
1310 | if (readlen == -1) { |
1311 | ret = readlen; | |
1312 | goto end; | |
1313 | } | |
d41f73b7 MD |
1314 | } |
1315 | ||
d41f73b7 | 1316 | /* Get the next subbuffer */ |
ffe60014 | 1317 | err = ustctl_get_next_subbuf(ustream); |
d41f73b7 | 1318 | if (err != 0) { |
1d4dfdef | 1319 | ret = err; /* ustctl_get_next_subbuf returns negative, caller expect positive. */ |
d41f73b7 MD |
1320 | /* |
1321 | * This is a debug message even for single-threaded consumer, | |
1322 | * because poll() have more relaxed criterions than get subbuf, | |
1323 | * so get_subbuf may fail for short race windows where poll() | |
1324 | * would issue wakeups. | |
1325 | */ | |
1326 | DBG("Reserving sub buffer failed (everything is normal, " | |
ffe60014 | 1327 | "it is due to concurrency) [ret: %d]", err); |
d41f73b7 MD |
1328 | goto end; |
1329 | } | |
ffe60014 | 1330 | assert(stream->chan->output == CONSUMER_CHANNEL_MMAP); |
1d4dfdef | 1331 | /* Get the full padded subbuffer size */ |
ffe60014 | 1332 | err = ustctl_get_padded_subbuf_size(ustream, &len); |
effcf122 | 1333 | assert(err == 0); |
1d4dfdef DG |
1334 | |
1335 | /* Get subbuffer data size (without padding) */ | |
ffe60014 | 1336 | err = ustctl_get_subbuf_size(ustream, &subbuf_size); |
1d4dfdef DG |
1337 | assert(err == 0); |
1338 | ||
1339 | /* Make sure we don't get a subbuffer size bigger than the padded */ | |
1340 | assert(len >= subbuf_size); | |
1341 | ||
1342 | padding = len - subbuf_size; | |
d41f73b7 | 1343 | /* write the subbuffer to the tracefile */ |
1d4dfdef | 1344 | ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size, padding); |
91dfef6e DG |
1345 | /* |
1346 | * The mmap operation should write subbuf_size amount of data when network | |
1347 | * streaming or the full padding (len) size when we are _not_ streaming. | |
1348 | */ | |
d88aee68 DG |
1349 | if ((ret != subbuf_size && stream->net_seq_idx != (uint64_t) -1ULL) || |
1350 | (ret != len && stream->net_seq_idx == (uint64_t) -1ULL)) { | |
d41f73b7 | 1351 | /* |
91dfef6e | 1352 | * Display the error but continue processing to try to release the |
c5c45efa DG |
1353 | * subbuffer. This is a DBG statement since any unexpected kill or |
1354 | * signal, the application gets unregistered, relayd gets closed or | |
1355 | * anything that affects the buffer lifetime will trigger this error. | |
1356 | * So, for the sake of the user, don't print this error since it can | |
1357 | * happen and it is OK with the code flow. | |
d41f73b7 | 1358 | */ |
c5c45efa | 1359 | DBG("Error writing to tracefile " |
8fd623e0 | 1360 | "(ret: %ld != len: %lu != subbuf_size: %lu)", |
91dfef6e | 1361 | ret, len, subbuf_size); |
d41f73b7 | 1362 | } |
ffe60014 | 1363 | err = ustctl_put_next_subbuf(ustream); |
effcf122 | 1364 | assert(err == 0); |
331744e3 | 1365 | |
d41f73b7 MD |
1366 | end: |
1367 | return ret; | |
1368 | } | |
1369 | ||
ffe60014 DG |
1370 | /* |
1371 | * Called when a stream is created. | |
fe4477ee JD |
1372 | * |
1373 | * Return 0 on success or else a negative value. | |
ffe60014 | 1374 | */ |
d41f73b7 MD |
1375 | int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream *stream) |
1376 | { | |
fe4477ee JD |
1377 | int ret; |
1378 | ||
1379 | /* Don't create anything if this is set for streaming. */ | |
1380 | if (stream->net_seq_idx == (uint64_t) -1ULL) { | |
1381 | ret = utils_create_stream_file(stream->chan->pathname, stream->name, | |
1382 | stream->chan->tracefile_size, stream->tracefile_count_current, | |
1383 | stream->uid, stream->gid); | |
1384 | if (ret < 0) { | |
1385 | goto error; | |
1386 | } | |
1387 | stream->out_fd = ret; | |
1388 | stream->tracefile_size_current = 0; | |
1389 | } | |
1390 | ret = 0; | |
1391 | ||
1392 | error: | |
1393 | return ret; | |
d41f73b7 | 1394 | } |
ca22feea DG |
1395 | |
1396 | /* | |
1397 | * Check if data is still being extracted from the buffers for a specific | |
4e9a4686 DG |
1398 | * stream. Consumer data lock MUST be acquired before calling this function |
1399 | * and the stream lock. | |
ca22feea | 1400 | * |
6d805429 | 1401 | * Return 1 if the traced data are still getting read else 0 meaning that the |
ca22feea DG |
1402 | * data is available for trace viewer reading. |
1403 | */ | |
6d805429 | 1404 | int lttng_ustconsumer_data_pending(struct lttng_consumer_stream *stream) |
ca22feea DG |
1405 | { |
1406 | int ret; | |
1407 | ||
1408 | assert(stream); | |
ffe60014 | 1409 | assert(stream->ustream); |
ca22feea | 1410 | |
6d805429 | 1411 | DBG("UST consumer checking data pending"); |
c8f59ee5 | 1412 | |
ffe60014 | 1413 | ret = ustctl_get_next_subbuf(stream->ustream); |
ca22feea DG |
1414 | if (ret == 0) { |
1415 | /* There is still data so let's put back this subbuffer. */ | |
ffe60014 | 1416 | ret = ustctl_put_subbuf(stream->ustream); |
ca22feea | 1417 | assert(ret == 0); |
6d805429 | 1418 | ret = 1; /* Data is pending */ |
4e9a4686 | 1419 | goto end; |
ca22feea DG |
1420 | } |
1421 | ||
6d805429 DG |
1422 | /* Data is NOT pending so ready to be read. */ |
1423 | ret = 0; | |
ca22feea | 1424 | |
6efae65e DG |
1425 | end: |
1426 | return ret; | |
ca22feea | 1427 | } |
d88aee68 DG |
1428 | |
1429 | /* | |
1430 | * Close every metadata stream wait fd of the metadata hash table. This | |
1431 | * function MUST be used very carefully so not to run into a race between the | |
1432 | * metadata thread handling streams and this function closing their wait fd. | |
1433 | * | |
1434 | * For UST, this is used when the session daemon hangs up. Its the metadata | |
1435 | * producer so calling this is safe because we are assured that no state change | |
1436 | * can occur in the metadata thread for the streams in the hash table. | |
1437 | */ | |
1438 | void lttng_ustconsumer_close_metadata(struct lttng_ht *metadata_ht) | |
1439 | { | |
1440 | int ret; | |
1441 | struct lttng_ht_iter iter; | |
1442 | struct lttng_consumer_stream *stream; | |
1443 | ||
1444 | assert(metadata_ht); | |
1445 | assert(metadata_ht->ht); | |
1446 | ||
1447 | DBG("UST consumer closing all metadata streams"); | |
1448 | ||
1449 | rcu_read_lock(); | |
1450 | cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream, | |
1451 | node.node) { | |
1452 | int fd = stream->wait_fd; | |
1453 | ||
1454 | /* | |
1455 | * Whatever happens here we have to continue to try to close every | |
1456 | * streams. Let's report at least the error on failure. | |
1457 | */ | |
1458 | ret = ustctl_stream_close_wakeup_fd(stream->ustream); | |
1459 | if (ret) { | |
1460 | ERR("Unable to close metadata stream fd %d ret %d", fd, ret); | |
1461 | } | |
1462 | DBG("Metadata wait fd %d closed", fd); | |
1463 | } | |
1464 | rcu_read_unlock(); | |
1465 | } | |
d8ef542d MD |
1466 | |
1467 | void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream *stream) | |
1468 | { | |
1469 | int ret; | |
1470 | ||
1471 | ret = ustctl_stream_close_wakeup_fd(stream->ustream); | |
1472 | if (ret < 0) { | |
1473 | ERR("Unable to close wakeup fd"); | |
1474 | } | |
1475 | } | |
331744e3 JD |
1476 | |
1477 | int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data *ctx, | |
1478 | struct lttng_consumer_channel *channel) | |
1479 | { | |
1480 | struct lttcomm_metadata_request_msg request; | |
1481 | struct lttcomm_consumer_msg msg; | |
1482 | enum lttng_error_code ret_code = LTTNG_OK; | |
1483 | uint64_t len, key, offset; | |
1484 | int ret; | |
1485 | ||
1486 | assert(channel); | |
1487 | assert(channel->metadata_cache); | |
1488 | ||
1489 | /* send the metadata request to sessiond */ | |
1490 | switch (consumer_data.type) { | |
1491 | case LTTNG_CONSUMER64_UST: | |
1492 | request.bits_per_long = 64; | |
1493 | break; | |
1494 | case LTTNG_CONSUMER32_UST: | |
1495 | request.bits_per_long = 32; | |
1496 | break; | |
1497 | default: | |
1498 | request.bits_per_long = 0; | |
1499 | break; | |
1500 | } | |
1501 | ||
1502 | request.session_id = channel->session_id; | |
1503 | request.uid = channel->uid; | |
1504 | request.key = channel->key; | |
1505 | DBG("Sending metadata request to sessiond, session %" PRIu64, | |
1506 | channel->session_id); | |
1507 | ||
1508 | ret = lttcomm_send_unix_sock(ctx->consumer_metadata_socket, &request, | |
1509 | sizeof(request)); | |
1510 | if (ret < 0) { | |
1511 | ERR("Asking metadata to sessiond"); | |
1512 | goto end; | |
1513 | } | |
1514 | ||
1515 | /* Receive the metadata from sessiond */ | |
1516 | ret = lttcomm_recv_unix_sock(ctx->consumer_metadata_socket, &msg, | |
1517 | sizeof(msg)); | |
1518 | if (ret != sizeof(msg)) { | |
8fd623e0 | 1519 | DBG("Consumer received unexpected message size %d (expects %zu)", |
331744e3 JD |
1520 | ret, sizeof(msg)); |
1521 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD); | |
1522 | /* | |
1523 | * The ret value might 0 meaning an orderly shutdown but this is ok | |
1524 | * since the caller handles this. | |
1525 | */ | |
1526 | goto end; | |
1527 | } | |
1528 | ||
1529 | if (msg.cmd_type == LTTNG_ERR_UND) { | |
1530 | /* No registry found */ | |
1531 | (void) consumer_send_status_msg(ctx->consumer_metadata_socket, | |
1532 | ret_code); | |
1533 | ret = 0; | |
1534 | goto end; | |
1535 | } else if (msg.cmd_type != LTTNG_CONSUMER_PUSH_METADATA) { | |
1536 | ERR("Unexpected cmd_type received %d", msg.cmd_type); | |
1537 | ret = -1; | |
1538 | goto end; | |
1539 | } | |
1540 | ||
1541 | len = msg.u.push_metadata.len; | |
1542 | key = msg.u.push_metadata.key; | |
1543 | offset = msg.u.push_metadata.target_offset; | |
1544 | ||
1545 | assert(key == channel->key); | |
1546 | if (len == 0) { | |
1547 | DBG("No new metadata to receive for key %" PRIu64, key); | |
1548 | } | |
1549 | ||
1550 | /* Tell session daemon we are ready to receive the metadata. */ | |
1551 | ret = consumer_send_status_msg(ctx->consumer_metadata_socket, | |
1552 | LTTNG_OK); | |
1553 | if (ret < 0 || len == 0) { | |
1554 | /* | |
1555 | * Somehow, the session daemon is not responding anymore or there is | |
1556 | * nothing to receive. | |
1557 | */ | |
1558 | goto end; | |
1559 | } | |
1560 | ||
1561 | ret_code = lttng_ustconsumer_recv_metadata(ctx->consumer_metadata_socket, | |
1562 | key, offset, len, channel); | |
f2a444f1 DG |
1563 | if (ret_code >= 0) { |
1564 | /* | |
1565 | * Only send the status msg if the sessiond is alive meaning a positive | |
1566 | * ret code. | |
1567 | */ | |
1568 | (void) consumer_send_status_msg(ctx->consumer_metadata_socket, ret_code); | |
1569 | } | |
331744e3 JD |
1570 | ret = 0; |
1571 | ||
1572 | end: | |
1573 | return ret; | |
1574 | } |