Commit | Line | Data |
---|---|---|
3bd1e081 | 1 | /* |
21cf9b6b | 2 | * Copyright (C) 2011 EfficiOS Inc. |
ab5be9fa MJ |
3 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
4 | * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
3bd1e081 | 5 | * |
ab5be9fa | 6 | * SPDX-License-Identifier: GPL-2.0-only |
3bd1e081 | 7 | * |
3bd1e081 MD |
8 | */ |
9 | ||
6c1c0768 | 10 | #define _LGPL_SOURCE |
f02e1e8a | 11 | #include <lttng/ust-ctl.h> |
881fc67f | 12 | #include <lttng/ust-sigbus.h> |
3bd1e081 MD |
13 | #include <poll.h> |
14 | #include <pthread.h> | |
15 | #include <stdlib.h> | |
16 | #include <string.h> | |
17 | #include <sys/mman.h> | |
18 | #include <sys/socket.h> | |
dbb5dfe6 | 19 | #include <sys/stat.h> |
3bd1e081 | 20 | #include <sys/types.h> |
77c7c900 | 21 | #include <inttypes.h> |
3bd1e081 | 22 | #include <unistd.h> |
ffe60014 | 23 | #include <urcu/list.h> |
331744e3 | 24 | #include <signal.h> |
02d02e31 | 25 | #include <stdbool.h> |
6f9449c2 | 26 | #include <stdint.h> |
0857097f | 27 | |
c9e313bc SM |
28 | #include <bin/lttng-consumerd/health-consumerd.hpp> |
29 | #include <common/common.hpp> | |
30 | #include <common/sessiond-comm/sessiond-comm.hpp> | |
31 | #include <common/relayd/relayd.hpp> | |
32 | #include <common/compat/fcntl.hpp> | |
33 | #include <common/compat/endian.hpp> | |
34 | #include <common/consumer/consumer-metadata-cache.hpp> | |
35 | #include <common/consumer/consumer-stream.hpp> | |
36 | #include <common/consumer/consumer-timer.hpp> | |
37 | #include <common/utils.hpp> | |
38 | #include <common/index/index.hpp> | |
39 | #include <common/consumer/consumer.hpp> | |
40 | #include <common/shm.hpp> | |
41 | #include <common/optional.hpp> | |
10a8a223 | 42 | |
c9e313bc | 43 | #include "ust-consumer.hpp" |
3bd1e081 | 44 | |
45863397 | 45 | #define INT_MAX_STR_LEN 12 /* includes \0 */ |
4628484a | 46 | |
fa29bfbf | 47 | extern struct lttng_consumer_global_data the_consumer_data; |
3bd1e081 | 48 | extern int consumer_poll_timeout; |
3bd1e081 | 49 | |
4bd69c5f | 50 | LTTNG_EXPORT DEFINE_LTTNG_UST_SIGBUS_STATE(); |
881fc67f | 51 | |
3bd1e081 | 52 | /* |
ffe60014 | 53 | * Add channel to internal consumer state. |
3bd1e081 | 54 | * |
ffe60014 | 55 | * Returns 0 on success or else a negative value. |
3bd1e081 | 56 | */ |
ffe60014 DG |
57 | static int add_channel(struct lttng_consumer_channel *channel, |
58 | struct lttng_consumer_local_data *ctx) | |
3bd1e081 MD |
59 | { |
60 | int ret = 0; | |
61 | ||
a0377dfe FD |
62 | LTTNG_ASSERT(channel); |
63 | LTTNG_ASSERT(ctx); | |
ffe60014 DG |
64 | |
65 | if (ctx->on_recv_channel != NULL) { | |
66 | ret = ctx->on_recv_channel(channel); | |
67 | if (ret == 0) { | |
d8ef542d | 68 | ret = consumer_add_channel(channel, ctx); |
ffe60014 DG |
69 | } else if (ret < 0) { |
70 | /* Most likely an ENOMEM. */ | |
71 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR); | |
72 | goto error; | |
73 | } | |
74 | } else { | |
d8ef542d | 75 | ret = consumer_add_channel(channel, ctx); |
3bd1e081 MD |
76 | } |
77 | ||
d88aee68 | 78 | DBG("UST consumer channel added (key: %" PRIu64 ")", channel->key); |
ffe60014 DG |
79 | |
80 | error: | |
3bd1e081 MD |
81 | return ret; |
82 | } | |
83 | ||
ffe60014 DG |
84 | /* |
85 | * Allocate and return a consumer stream object. If _alloc_ret is not NULL, the | |
86 | * error value if applicable is set in it else it is kept untouched. | |
3bd1e081 | 87 | * |
ffe60014 | 88 | * Return NULL on error else the newly allocated stream object. |
3bd1e081 | 89 | */ |
ffe60014 DG |
90 | static struct lttng_consumer_stream *allocate_stream(int cpu, int key, |
91 | struct lttng_consumer_channel *channel, | |
d2956687 | 92 | struct lttng_consumer_local_data *ctx, int *_alloc_ret) |
ffe60014 DG |
93 | { |
94 | int alloc_ret; | |
95 | struct lttng_consumer_stream *stream = NULL; | |
96 | ||
a0377dfe FD |
97 | LTTNG_ASSERT(channel); |
98 | LTTNG_ASSERT(ctx); | |
ffe60014 | 99 | |
6f9449c2 | 100 | stream = consumer_stream_create( |
49f45573 JG |
101 | channel, |
102 | channel->key, | |
ffe60014 | 103 | key, |
ffe60014 | 104 | channel->name, |
ffe60014 DG |
105 | channel->relayd_id, |
106 | channel->session_id, | |
d2956687 | 107 | channel->trace_chunk, |
ffe60014 DG |
108 | cpu, |
109 | &alloc_ret, | |
4891ece8 | 110 | channel->type, |
d2956687 | 111 | channel->monitor); |
ffe60014 DG |
112 | if (stream == NULL) { |
113 | switch (alloc_ret) { | |
114 | case -ENOENT: | |
115 | /* | |
116 | * We could not find the channel. Can happen if cpu hotplug | |
117 | * happens while tearing down. | |
118 | */ | |
119 | DBG3("Could not find channel"); | |
120 | break; | |
121 | case -ENOMEM: | |
122 | case -EINVAL: | |
123 | default: | |
124 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR); | |
125 | break; | |
126 | } | |
127 | goto error; | |
128 | } | |
129 | ||
d9a2e16e | 130 | consumer_stream_update_channel_attributes(stream, channel); |
ffe60014 DG |
131 | |
132 | error: | |
133 | if (_alloc_ret) { | |
134 | *_alloc_ret = alloc_ret; | |
135 | } | |
136 | return stream; | |
137 | } | |
138 | ||
139 | /* | |
140 | * Send the given stream pointer to the corresponding thread. | |
141 | * | |
142 | * Returns 0 on success else a negative value. | |
143 | */ | |
144 | static int send_stream_to_thread(struct lttng_consumer_stream *stream, | |
145 | struct lttng_consumer_local_data *ctx) | |
146 | { | |
dae10966 DG |
147 | int ret; |
148 | struct lttng_pipe *stream_pipe; | |
ffe60014 DG |
149 | |
150 | /* Get the right pipe where the stream will be sent. */ | |
151 | if (stream->metadata_flag) { | |
66d583dc | 152 | consumer_add_metadata_stream(stream); |
dae10966 | 153 | stream_pipe = ctx->consumer_metadata_pipe; |
ffe60014 | 154 | } else { |
66d583dc | 155 | consumer_add_data_stream(stream); |
dae10966 | 156 | stream_pipe = ctx->consumer_data_pipe; |
ffe60014 DG |
157 | } |
158 | ||
5ab66908 MD |
159 | /* |
160 | * From this point on, the stream's ownership has been moved away from | |
a8086cf4 JR |
161 | * the channel and it becomes globally visible. Hence, remove it from |
162 | * the local stream list to prevent the stream from being both local and | |
163 | * global. | |
5ab66908 MD |
164 | */ |
165 | stream->globally_visible = 1; | |
5c5e3d71 | 166 | cds_list_del_init(&stream->send_node); |
5ab66908 | 167 | |
dae10966 | 168 | ret = lttng_pipe_write(stream_pipe, &stream, sizeof(stream)); |
ffe60014 | 169 | if (ret < 0) { |
dae10966 DG |
170 | ERR("Consumer write %s stream to pipe %d", |
171 | stream->metadata_flag ? "metadata" : "data", | |
172 | lttng_pipe_get_writefd(stream_pipe)); | |
5ab66908 MD |
173 | if (stream->metadata_flag) { |
174 | consumer_del_stream_for_metadata(stream); | |
175 | } else { | |
176 | consumer_del_stream_for_data(stream); | |
177 | } | |
a8086cf4 | 178 | goto error; |
ffe60014 | 179 | } |
a8086cf4 | 180 | |
5ab66908 | 181 | error: |
ffe60014 DG |
182 | return ret; |
183 | } | |
184 | ||
4628484a MD |
185 | static |
186 | int get_stream_shm_path(char *stream_shm_path, const char *shm_path, int cpu) | |
187 | { | |
45863397 | 188 | char cpu_nr[INT_MAX_STR_LEN]; /* int max len */ |
4628484a MD |
189 | int ret; |
190 | ||
191 | strncpy(stream_shm_path, shm_path, PATH_MAX); | |
192 | stream_shm_path[PATH_MAX - 1] = '\0'; | |
45863397 | 193 | ret = snprintf(cpu_nr, INT_MAX_STR_LEN, "%i", cpu); |
67f8cb8d MD |
194 | if (ret < 0) { |
195 | PERROR("snprintf"); | |
4628484a MD |
196 | goto end; |
197 | } | |
198 | strncat(stream_shm_path, cpu_nr, | |
199 | PATH_MAX - strlen(stream_shm_path) - 1); | |
200 | ret = 0; | |
201 | end: | |
202 | return ret; | |
203 | } | |
204 | ||
d88aee68 DG |
205 | /* |
206 | * Create streams for the given channel using liblttng-ust-ctl. | |
d2956687 | 207 | * The channel lock must be acquired by the caller. |
d88aee68 DG |
208 | * |
209 | * Return 0 on success else a negative value. | |
210 | */ | |
ffe60014 | 211 | static int create_ust_streams(struct lttng_consumer_channel *channel, |
d2956687 | 212 | struct lttng_consumer_local_data *ctx) |
ffe60014 DG |
213 | { |
214 | int ret, cpu = 0; | |
b623cb6a | 215 | struct lttng_ust_ctl_consumer_stream *ustream; |
ffe60014 | 216 | struct lttng_consumer_stream *stream; |
d2956687 | 217 | pthread_mutex_t *current_stream_lock = NULL; |
ffe60014 | 218 | |
a0377dfe FD |
219 | LTTNG_ASSERT(channel); |
220 | LTTNG_ASSERT(ctx); | |
ffe60014 DG |
221 | |
222 | /* | |
223 | * While a stream is available from ustctl. When NULL is returned, we've | |
224 | * reached the end of the possible stream for the channel. | |
225 | */ | |
b623cb6a | 226 | while ((ustream = lttng_ust_ctl_create_stream(channel->uchan, cpu))) { |
ffe60014 | 227 | int wait_fd; |
04ef1097 | 228 | int ust_metadata_pipe[2]; |
ffe60014 | 229 | |
9ce5646a MD |
230 | health_code_update(); |
231 | ||
04ef1097 MD |
232 | if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA && channel->monitor) { |
233 | ret = utils_create_pipe_cloexec_nonblock(ust_metadata_pipe); | |
234 | if (ret < 0) { | |
235 | ERR("Create ust metadata poll pipe"); | |
236 | goto error; | |
237 | } | |
238 | wait_fd = ust_metadata_pipe[0]; | |
239 | } else { | |
b623cb6a | 240 | wait_fd = lttng_ust_ctl_stream_get_wait_fd(ustream); |
04ef1097 | 241 | } |
ffe60014 DG |
242 | |
243 | /* Allocate consumer stream object. */ | |
d2956687 | 244 | stream = allocate_stream(cpu, wait_fd, channel, ctx, &ret); |
ffe60014 DG |
245 | if (!stream) { |
246 | goto error_alloc; | |
247 | } | |
248 | stream->ustream = ustream; | |
249 | /* | |
250 | * Store it so we can save multiple function calls afterwards since | |
251 | * this value is used heavily in the stream threads. This is UST | |
252 | * specific so this is why it's done after allocation. | |
253 | */ | |
254 | stream->wait_fd = wait_fd; | |
255 | ||
b31398bb DG |
256 | /* |
257 | * Increment channel refcount since the channel reference has now been | |
258 | * assigned in the allocation process above. | |
259 | */ | |
10a50311 JD |
260 | if (stream->chan->monitor) { |
261 | uatomic_inc(&stream->chan->refcount); | |
262 | } | |
b31398bb | 263 | |
d2956687 JG |
264 | pthread_mutex_lock(&stream->lock); |
265 | current_stream_lock = &stream->lock; | |
ffe60014 DG |
266 | /* |
267 | * Order is important this is why a list is used. On error, the caller | |
268 | * should clean this list. | |
269 | */ | |
270 | cds_list_add_tail(&stream->send_node, &channel->streams.head); | |
271 | ||
b623cb6a | 272 | ret = lttng_ust_ctl_get_max_subbuf_size(stream->ustream, |
ffe60014 DG |
273 | &stream->max_sb_size); |
274 | if (ret < 0) { | |
b623cb6a | 275 | ERR("lttng_ust_ctl_get_max_subbuf_size failed for stream %s", |
ffe60014 DG |
276 | stream->name); |
277 | goto error; | |
278 | } | |
279 | ||
280 | /* Do actions once stream has been received. */ | |
281 | if (ctx->on_recv_stream) { | |
282 | ret = ctx->on_recv_stream(stream); | |
283 | if (ret < 0) { | |
284 | goto error; | |
285 | } | |
286 | } | |
287 | ||
d88aee68 | 288 | DBG("UST consumer add stream %s (key: %" PRIu64 ") with relayd id %" PRIu64, |
ffe60014 DG |
289 | stream->name, stream->key, stream->relayd_stream_id); |
290 | ||
291 | /* Set next CPU stream. */ | |
292 | channel->streams.count = ++cpu; | |
d88aee68 DG |
293 | |
294 | /* Keep stream reference when creating metadata. */ | |
295 | if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA) { | |
296 | channel->metadata_stream = stream; | |
8de4f941 JG |
297 | if (channel->monitor) { |
298 | /* Set metadata poll pipe if we created one */ | |
299 | memcpy(stream->ust_metadata_poll_pipe, | |
300 | ust_metadata_pipe, | |
301 | sizeof(ust_metadata_pipe)); | |
302 | } | |
d88aee68 | 303 | } |
d2956687 JG |
304 | pthread_mutex_unlock(&stream->lock); |
305 | current_stream_lock = NULL; | |
ffe60014 DG |
306 | } |
307 | ||
308 | return 0; | |
309 | ||
310 | error: | |
311 | error_alloc: | |
d2956687 JG |
312 | if (current_stream_lock) { |
313 | pthread_mutex_unlock(current_stream_lock); | |
314 | } | |
ffe60014 DG |
315 | return ret; |
316 | } | |
317 | ||
d2956687 JG |
318 | static int open_ust_stream_fd(struct lttng_consumer_channel *channel, int cpu, |
319 | const struct lttng_credentials *session_credentials) | |
4628484a MD |
320 | { |
321 | char shm_path[PATH_MAX]; | |
322 | int ret; | |
323 | ||
324 | if (!channel->shm_path[0]) { | |
b7fc068d | 325 | return shm_create_anonymous("ust-consumer"); |
4628484a MD |
326 | } |
327 | ret = get_stream_shm_path(shm_path, channel->shm_path, cpu); | |
328 | if (ret) { | |
329 | goto error_shm_path; | |
330 | } | |
331 | return run_as_open(shm_path, | |
332 | O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, | |
ff588497 JR |
333 | lttng_credentials_get_uid(session_credentials), |
334 | lttng_credentials_get_gid(session_credentials)); | |
4628484a MD |
335 | |
336 | error_shm_path: | |
337 | return -1; | |
338 | } | |
339 | ||
ffe60014 DG |
340 | /* |
341 | * Create an UST channel with the given attributes and send it to the session | |
342 | * daemon using the ust ctl API. | |
343 | * | |
344 | * Return 0 on success or else a negative value. | |
345 | */ | |
4628484a | 346 | static int create_ust_channel(struct lttng_consumer_channel *channel, |
b623cb6a MJ |
347 | struct lttng_ust_ctl_consumer_channel_attr *attr, |
348 | struct lttng_ust_ctl_consumer_channel **ust_chanp) | |
ffe60014 | 349 | { |
4628484a MD |
350 | int ret, nr_stream_fds, i, j; |
351 | int *stream_fds; | |
b623cb6a | 352 | struct lttng_ust_ctl_consumer_channel *ust_channel; |
ffe60014 | 353 | |
a0377dfe FD |
354 | LTTNG_ASSERT(channel); |
355 | LTTNG_ASSERT(attr); | |
356 | LTTNG_ASSERT(ust_chanp); | |
357 | LTTNG_ASSERT(channel->buffer_credentials.is_set); | |
ffe60014 DG |
358 | |
359 | DBG3("Creating channel to ustctl with attr: [overwrite: %d, " | |
360 | "subbuf_size: %" PRIu64 ", num_subbuf: %" PRIu64 ", " | |
361 | "switch_timer_interval: %u, read_timer_interval: %u, " | |
362 | "output: %d, type: %d", attr->overwrite, attr->subbuf_size, | |
363 | attr->num_subbuf, attr->switch_timer_interval, | |
364 | attr->read_timer_interval, attr->output, attr->type); | |
365 | ||
4628484a MD |
366 | if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA) |
367 | nr_stream_fds = 1; | |
368 | else | |
b623cb6a | 369 | nr_stream_fds = lttng_ust_ctl_get_nr_stream_per_channel(); |
64803277 | 370 | stream_fds = calloc<int>(nr_stream_fds); |
4628484a MD |
371 | if (!stream_fds) { |
372 | ret = -1; | |
373 | goto error_alloc; | |
374 | } | |
375 | for (i = 0; i < nr_stream_fds; i++) { | |
d2956687 JG |
376 | stream_fds[i] = open_ust_stream_fd(channel, i, |
377 | &channel->buffer_credentials.value); | |
4628484a MD |
378 | if (stream_fds[i] < 0) { |
379 | ret = -1; | |
380 | goto error_open; | |
381 | } | |
382 | } | |
b623cb6a | 383 | ust_channel = lttng_ust_ctl_create_channel(attr, stream_fds, nr_stream_fds); |
4628484a | 384 | if (!ust_channel) { |
ffe60014 DG |
385 | ret = -1; |
386 | goto error_create; | |
387 | } | |
4628484a MD |
388 | channel->nr_stream_fds = nr_stream_fds; |
389 | channel->stream_fds = stream_fds; | |
390 | *ust_chanp = ust_channel; | |
ffe60014 DG |
391 | |
392 | return 0; | |
393 | ||
394 | error_create: | |
4628484a MD |
395 | error_open: |
396 | for (j = i - 1; j >= 0; j--) { | |
397 | int closeret; | |
398 | ||
399 | closeret = close(stream_fds[j]); | |
400 | if (closeret) { | |
401 | PERROR("close"); | |
402 | } | |
403 | if (channel->shm_path[0]) { | |
404 | char shm_path[PATH_MAX]; | |
405 | ||
406 | closeret = get_stream_shm_path(shm_path, | |
407 | channel->shm_path, j); | |
408 | if (closeret) { | |
409 | ERR("Cannot get stream shm path"); | |
410 | } | |
411 | closeret = run_as_unlink(shm_path, | |
ff588497 JR |
412 | lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR( |
413 | channel->buffer_credentials)), | |
414 | lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR( | |
415 | channel->buffer_credentials))); | |
4628484a | 416 | if (closeret) { |
4628484a MD |
417 | PERROR("unlink %s", shm_path); |
418 | } | |
419 | } | |
420 | } | |
421 | /* Try to rmdir all directories under shm_path root. */ | |
422 | if (channel->root_shm_path[0]) { | |
602766ec | 423 | (void) run_as_rmdir_recursive(channel->root_shm_path, |
ff588497 JR |
424 | lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR( |
425 | channel->buffer_credentials)), | |
426 | lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR( | |
427 | channel->buffer_credentials)), | |
f75c5439 | 428 | LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG); |
4628484a MD |
429 | } |
430 | free(stream_fds); | |
431 | error_alloc: | |
ffe60014 DG |
432 | return ret; |
433 | } | |
434 | ||
d88aee68 DG |
435 | /* |
436 | * Send a single given stream to the session daemon using the sock. | |
437 | * | |
438 | * Return 0 on success else a negative value. | |
439 | */ | |
ffe60014 DG |
440 | static int send_sessiond_stream(int sock, struct lttng_consumer_stream *stream) |
441 | { | |
442 | int ret; | |
443 | ||
a0377dfe FD |
444 | LTTNG_ASSERT(stream); |
445 | LTTNG_ASSERT(sock >= 0); | |
ffe60014 | 446 | |
3eb914c0 | 447 | DBG("UST consumer sending stream %" PRIu64 " to sessiond", stream->key); |
ffe60014 DG |
448 | |
449 | /* Send stream to session daemon. */ | |
b623cb6a | 450 | ret = lttng_ust_ctl_send_stream_to_sessiond(sock, stream->ustream); |
ffe60014 DG |
451 | if (ret < 0) { |
452 | goto error; | |
453 | } | |
454 | ||
ffe60014 DG |
455 | error: |
456 | return ret; | |
457 | } | |
458 | ||
459 | /* | |
a3a86f35 | 460 | * Send channel to sessiond and relayd if applicable. |
ffe60014 | 461 | * |
d88aee68 | 462 | * Return 0 on success or else a negative value. |
ffe60014 | 463 | */ |
a3a86f35 | 464 | static int send_channel_to_sessiond_and_relayd(int sock, |
ffe60014 DG |
465 | struct lttng_consumer_channel *channel, |
466 | struct lttng_consumer_local_data *ctx, int *relayd_error) | |
467 | { | |
0c759fc9 | 468 | int ret, ret_code = LTTCOMM_CONSUMERD_SUCCESS; |
ffe60014 | 469 | struct lttng_consumer_stream *stream; |
a4baae1b | 470 | uint64_t net_seq_idx = -1ULL; |
ffe60014 | 471 | |
a0377dfe FD |
472 | LTTNG_ASSERT(channel); |
473 | LTTNG_ASSERT(ctx); | |
474 | LTTNG_ASSERT(sock >= 0); | |
ffe60014 DG |
475 | |
476 | DBG("UST consumer sending channel %s to sessiond", channel->name); | |
477 | ||
62285ea4 DG |
478 | if (channel->relayd_id != (uint64_t) -1ULL) { |
479 | cds_list_for_each_entry(stream, &channel->streams.head, send_node) { | |
9ce5646a MD |
480 | |
481 | health_code_update(); | |
482 | ||
62285ea4 | 483 | /* Try to send the stream to the relayd if one is available. */ |
a3a86f35 JG |
484 | DBG("Sending stream %" PRIu64 " of channel \"%s\" to relayd", |
485 | stream->key, channel->name); | |
62285ea4 DG |
486 | ret = consumer_send_relayd_stream(stream, stream->chan->pathname); |
487 | if (ret < 0) { | |
488 | /* | |
489 | * Flag that the relayd was the problem here probably due to a | |
490 | * communicaton error on the socket. | |
491 | */ | |
492 | if (relayd_error) { | |
493 | *relayd_error = 1; | |
494 | } | |
725d28b2 | 495 | ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL; |
ffe60014 | 496 | } |
a4baae1b JD |
497 | if (net_seq_idx == -1ULL) { |
498 | net_seq_idx = stream->net_seq_idx; | |
499 | } | |
500 | } | |
f2a444f1 | 501 | } |
ffe60014 | 502 | |
f2a444f1 DG |
503 | /* Inform sessiond that we are about to send channel and streams. */ |
504 | ret = consumer_send_status_msg(sock, ret_code); | |
0c759fc9 | 505 | if (ret < 0 || ret_code != LTTCOMM_CONSUMERD_SUCCESS) { |
f2a444f1 DG |
506 | /* |
507 | * Either the session daemon is not responding or the relayd died so we | |
508 | * stop now. | |
509 | */ | |
510 | goto error; | |
511 | } | |
512 | ||
513 | /* Send channel to sessiond. */ | |
b623cb6a | 514 | ret = lttng_ust_ctl_send_channel_to_sessiond(sock, channel->uchan); |
f2a444f1 DG |
515 | if (ret < 0) { |
516 | goto error; | |
517 | } | |
518 | ||
b623cb6a | 519 | ret = lttng_ust_ctl_channel_close_wakeup_fd(channel->uchan); |
f2a444f1 DG |
520 | if (ret < 0) { |
521 | goto error; | |
522 | } | |
523 | ||
524 | /* The channel was sent successfully to the sessiond at this point. */ | |
525 | cds_list_for_each_entry(stream, &channel->streams.head, send_node) { | |
9ce5646a MD |
526 | |
527 | health_code_update(); | |
528 | ||
ffe60014 DG |
529 | /* Send stream to session daemon. */ |
530 | ret = send_sessiond_stream(sock, stream); | |
531 | if (ret < 0) { | |
532 | goto error; | |
533 | } | |
534 | } | |
535 | ||
536 | /* Tell sessiond there is no more stream. */ | |
b623cb6a | 537 | ret = lttng_ust_ctl_send_stream_to_sessiond(sock, NULL); |
ffe60014 DG |
538 | if (ret < 0) { |
539 | goto error; | |
540 | } | |
541 | ||
542 | DBG("UST consumer NULL stream sent to sessiond"); | |
543 | ||
544 | return 0; | |
545 | ||
546 | error: | |
0c759fc9 | 547 | if (ret_code != LTTCOMM_CONSUMERD_SUCCESS) { |
f2a444f1 DG |
548 | ret = -1; |
549 | } | |
ffe60014 DG |
550 | return ret; |
551 | } | |
552 | ||
553 | /* | |
554 | * Creates a channel and streams and add the channel it to the channel internal | |
555 | * state. The created stream must ONLY be sent once the GET_CHANNEL command is | |
556 | * received. | |
557 | * | |
558 | * Return 0 on success or else, a negative value is returned and the channel | |
559 | * MUST be destroyed by consumer_del_channel(). | |
560 | */ | |
75cfe9e6 | 561 | static int ask_channel(struct lttng_consumer_local_data *ctx, |
ffe60014 | 562 | struct lttng_consumer_channel *channel, |
b623cb6a | 563 | struct lttng_ust_ctl_consumer_channel_attr *attr) |
3bd1e081 MD |
564 | { |
565 | int ret; | |
566 | ||
a0377dfe FD |
567 | LTTNG_ASSERT(ctx); |
568 | LTTNG_ASSERT(channel); | |
569 | LTTNG_ASSERT(attr); | |
ffe60014 DG |
570 | |
571 | /* | |
572 | * This value is still used by the kernel consumer since for the kernel, | |
573 | * the stream ownership is not IN the consumer so we need to have the | |
574 | * number of left stream that needs to be initialized so we can know when | |
575 | * to delete the channel (see consumer.c). | |
576 | * | |
577 | * As for the user space tracer now, the consumer creates and sends the | |
578 | * stream to the session daemon which only sends them to the application | |
579 | * once every stream of a channel is received making this value useless | |
580 | * because we they will be added to the poll thread before the application | |
581 | * receives them. This ensures that a stream can not hang up during | |
582 | * initilization of a channel. | |
583 | */ | |
584 | channel->nb_init_stream_left = 0; | |
585 | ||
586 | /* The reply msg status is handled in the following call. */ | |
4628484a | 587 | ret = create_ust_channel(channel, attr, &channel->uchan); |
ffe60014 | 588 | if (ret < 0) { |
10a50311 | 589 | goto end; |
3bd1e081 MD |
590 | } |
591 | ||
b623cb6a | 592 | channel->wait_fd = lttng_ust_ctl_channel_get_wait_fd(channel->uchan); |
d8ef542d | 593 | |
10a50311 JD |
594 | /* |
595 | * For the snapshots (no monitor), we create the metadata streams | |
596 | * on demand, not during the channel creation. | |
597 | */ | |
598 | if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA && !channel->monitor) { | |
599 | ret = 0; | |
600 | goto end; | |
601 | } | |
602 | ||
ffe60014 | 603 | /* Open all streams for this channel. */ |
d2956687 JG |
604 | pthread_mutex_lock(&channel->lock); |
605 | ret = create_ust_streams(channel, ctx); | |
606 | pthread_mutex_unlock(&channel->lock); | |
ffe60014 | 607 | if (ret < 0) { |
10a50311 | 608 | goto end; |
ffe60014 DG |
609 | } |
610 | ||
10a50311 | 611 | end: |
3bd1e081 MD |
612 | return ret; |
613 | } | |
614 | ||
d88aee68 DG |
615 | /* |
616 | * Send all stream of a channel to the right thread handling it. | |
617 | * | |
618 | * On error, return a negative value else 0 on success. | |
619 | */ | |
620 | static int send_streams_to_thread(struct lttng_consumer_channel *channel, | |
621 | struct lttng_consumer_local_data *ctx) | |
622 | { | |
623 | int ret = 0; | |
624 | struct lttng_consumer_stream *stream, *stmp; | |
625 | ||
a0377dfe FD |
626 | LTTNG_ASSERT(channel); |
627 | LTTNG_ASSERT(ctx); | |
d88aee68 DG |
628 | |
629 | /* Send streams to the corresponding thread. */ | |
630 | cds_list_for_each_entry_safe(stream, stmp, &channel->streams.head, | |
631 | send_node) { | |
9ce5646a MD |
632 | |
633 | health_code_update(); | |
634 | ||
d88aee68 DG |
635 | /* Sending the stream to the thread. */ |
636 | ret = send_stream_to_thread(stream, ctx); | |
637 | if (ret < 0) { | |
638 | /* | |
639 | * If we are unable to send the stream to the thread, there is | |
640 | * a big problem so just stop everything. | |
641 | */ | |
642 | goto error; | |
643 | } | |
d88aee68 DG |
644 | } |
645 | ||
646 | error: | |
647 | return ret; | |
648 | } | |
649 | ||
7972aab2 DG |
650 | /* |
651 | * Flush channel's streams using the given key to retrieve the channel. | |
652 | * | |
653 | * Return 0 on success else an LTTng error code. | |
654 | */ | |
655 | static int flush_channel(uint64_t chan_key) | |
656 | { | |
657 | int ret = 0; | |
658 | struct lttng_consumer_channel *channel; | |
659 | struct lttng_consumer_stream *stream; | |
660 | struct lttng_ht *ht; | |
661 | struct lttng_ht_iter iter; | |
662 | ||
8fd623e0 | 663 | DBG("UST consumer flush channel key %" PRIu64, chan_key); |
7972aab2 | 664 | |
a500c257 | 665 | rcu_read_lock(); |
7972aab2 DG |
666 | channel = consumer_find_channel(chan_key); |
667 | if (!channel) { | |
8fd623e0 | 668 | ERR("UST consumer flush channel %" PRIu64 " not found", chan_key); |
7972aab2 DG |
669 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
670 | goto error; | |
671 | } | |
672 | ||
fa29bfbf | 673 | ht = the_consumer_data.stream_per_chan_id_ht; |
7972aab2 DG |
674 | |
675 | /* For each stream of the channel id, flush it. */ | |
7972aab2 DG |
676 | cds_lfht_for_each_entry_duplicate(ht->ht, |
677 | ht->hash_fct(&channel->key, lttng_ht_seed), ht->match_fct, | |
678 | &channel->key, &iter.iter, stream, node_channel_id.node) { | |
9ce5646a MD |
679 | |
680 | health_code_update(); | |
681 | ||
0dd01979 | 682 | pthread_mutex_lock(&stream->lock); |
5cfcab67 JR |
683 | |
684 | /* | |
685 | * Protect against concurrent teardown of a stream. | |
686 | */ | |
687 | if (cds_lfht_is_node_deleted(&stream->node.node)) { | |
688 | goto next; | |
689 | } | |
690 | ||
0dd01979 | 691 | if (!stream->quiescent) { |
881fc67f MD |
692 | ret = lttng_ust_ctl_flush_buffer(stream->ustream, 0); |
693 | if (ret) { | |
694 | ERR("Failed to flush buffer while flushing channel: channel key = %" PRIu64 ", channel name = '%s'", | |
695 | chan_key, channel->name); | |
696 | ret = LTTNG_ERR_BUFFER_FLUSH_FAILED; | |
697 | pthread_mutex_unlock(&stream->lock); | |
698 | goto error; | |
699 | } | |
0dd01979 MD |
700 | stream->quiescent = true; |
701 | } | |
5cfcab67 | 702 | next: |
0dd01979 MD |
703 | pthread_mutex_unlock(&stream->lock); |
704 | } | |
705 | error: | |
706 | rcu_read_unlock(); | |
707 | return ret; | |
708 | } | |
709 | ||
710 | /* | |
711 | * Clear quiescent state from channel's streams using the given key to | |
712 | * retrieve the channel. | |
713 | * | |
714 | * Return 0 on success else an LTTng error code. | |
715 | */ | |
716 | static int clear_quiescent_channel(uint64_t chan_key) | |
717 | { | |
718 | int ret = 0; | |
719 | struct lttng_consumer_channel *channel; | |
720 | struct lttng_consumer_stream *stream; | |
721 | struct lttng_ht *ht; | |
722 | struct lttng_ht_iter iter; | |
723 | ||
724 | DBG("UST consumer clear quiescent channel key %" PRIu64, chan_key); | |
725 | ||
726 | rcu_read_lock(); | |
727 | channel = consumer_find_channel(chan_key); | |
728 | if (!channel) { | |
729 | ERR("UST consumer clear quiescent channel %" PRIu64 " not found", chan_key); | |
730 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; | |
731 | goto error; | |
732 | } | |
733 | ||
fa29bfbf | 734 | ht = the_consumer_data.stream_per_chan_id_ht; |
0dd01979 MD |
735 | |
736 | /* For each stream of the channel id, clear quiescent state. */ | |
737 | cds_lfht_for_each_entry_duplicate(ht->ht, | |
738 | ht->hash_fct(&channel->key, lttng_ht_seed), ht->match_fct, | |
739 | &channel->key, &iter.iter, stream, node_channel_id.node) { | |
740 | ||
741 | health_code_update(); | |
742 | ||
743 | pthread_mutex_lock(&stream->lock); | |
744 | stream->quiescent = false; | |
745 | pthread_mutex_unlock(&stream->lock); | |
7972aab2 | 746 | } |
7972aab2 | 747 | error: |
a500c257 | 748 | rcu_read_unlock(); |
7972aab2 DG |
749 | return ret; |
750 | } | |
751 | ||
d88aee68 DG |
752 | /* |
753 | * Close metadata stream wakeup_fd using the given key to retrieve the channel. | |
754 | * | |
755 | * Return 0 on success else an LTTng error code. | |
756 | */ | |
757 | static int close_metadata(uint64_t chan_key) | |
758 | { | |
ea88ca2a | 759 | int ret = 0; |
d88aee68 | 760 | struct lttng_consumer_channel *channel; |
f65a74be | 761 | unsigned int channel_monitor; |
d88aee68 | 762 | |
8fd623e0 | 763 | DBG("UST consumer close metadata key %" PRIu64, chan_key); |
d88aee68 DG |
764 | |
765 | channel = consumer_find_channel(chan_key); | |
766 | if (!channel) { | |
84cc9aa0 DG |
767 | /* |
768 | * This is possible if the metadata thread has issue a delete because | |
769 | * the endpoint point of the stream hung up. There is no way the | |
770 | * session daemon can know about it thus use a DBG instead of an actual | |
771 | * error. | |
772 | */ | |
773 | DBG("UST consumer close metadata %" PRIu64 " not found", chan_key); | |
d88aee68 DG |
774 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
775 | goto error; | |
776 | } | |
777 | ||
fa29bfbf | 778 | pthread_mutex_lock(&the_consumer_data.lock); |
a9838785 | 779 | pthread_mutex_lock(&channel->lock); |
f65a74be | 780 | channel_monitor = channel->monitor; |
73811ecc DG |
781 | if (cds_lfht_is_node_deleted(&channel->node.node)) { |
782 | goto error_unlock; | |
783 | } | |
784 | ||
6d574024 | 785 | lttng_ustconsumer_close_metadata(channel); |
f65a74be | 786 | pthread_mutex_unlock(&channel->lock); |
fa29bfbf | 787 | pthread_mutex_unlock(&the_consumer_data.lock); |
d88aee68 | 788 | |
f65a74be JG |
789 | /* |
790 | * The ownership of a metadata channel depends on the type of | |
791 | * session to which it belongs. In effect, the monitor flag is checked | |
792 | * to determine if this metadata channel is in "snapshot" mode or not. | |
793 | * | |
794 | * In the non-snapshot case, the metadata channel is created along with | |
795 | * a single stream which will remain present until the metadata channel | |
796 | * is destroyed (on the destruction of its session). In this case, the | |
797 | * metadata stream in "monitored" by the metadata poll thread and holds | |
798 | * the ownership of its channel. | |
799 | * | |
800 | * Closing the metadata will cause the metadata stream's "metadata poll | |
801 | * pipe" to be closed. Closing this pipe will wake-up the metadata poll | |
802 | * thread which will teardown the metadata stream which, in return, | |
803 | * deletes the metadata channel. | |
804 | * | |
805 | * In the snapshot case, the metadata stream is created and destroyed | |
806 | * on every snapshot record. Since the channel doesn't have an owner | |
807 | * other than the session daemon, it is safe to destroy it immediately | |
808 | * on reception of the CLOSE_METADATA command. | |
809 | */ | |
810 | if (!channel_monitor) { | |
811 | /* | |
812 | * The channel and consumer_data locks must be | |
813 | * released before this call since consumer_del_channel | |
814 | * re-acquires the channel and consumer_data locks to teardown | |
815 | * the channel and queue its reclamation by the "call_rcu" | |
816 | * worker thread. | |
817 | */ | |
818 | consumer_del_channel(channel); | |
819 | } | |
820 | ||
821 | return ret; | |
ea88ca2a | 822 | error_unlock: |
a9838785 | 823 | pthread_mutex_unlock(&channel->lock); |
fa29bfbf | 824 | pthread_mutex_unlock(&the_consumer_data.lock); |
d88aee68 DG |
825 | error: |
826 | return ret; | |
827 | } | |
828 | ||
829 | /* | |
830 | * RCU read side lock MUST be acquired before calling this function. | |
831 | * | |
832 | * Return 0 on success else an LTTng error code. | |
833 | */ | |
834 | static int setup_metadata(struct lttng_consumer_local_data *ctx, uint64_t key) | |
835 | { | |
836 | int ret; | |
837 | struct lttng_consumer_channel *metadata; | |
838 | ||
48b7cdc2 FD |
839 | ASSERT_RCU_READ_LOCKED(); |
840 | ||
8fd623e0 | 841 | DBG("UST consumer setup metadata key %" PRIu64, key); |
d88aee68 DG |
842 | |
843 | metadata = consumer_find_channel(key); | |
844 | if (!metadata) { | |
845 | ERR("UST consumer push metadata %" PRIu64 " not found", key); | |
846 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; | |
10a50311 JD |
847 | goto end; |
848 | } | |
849 | ||
850 | /* | |
851 | * In no monitor mode, the metadata channel has no stream(s) so skip the | |
852 | * ownership transfer to the metadata thread. | |
853 | */ | |
854 | if (!metadata->monitor) { | |
855 | DBG("Metadata channel in no monitor"); | |
856 | ret = 0; | |
857 | goto end; | |
d88aee68 DG |
858 | } |
859 | ||
860 | /* | |
861 | * Send metadata stream to relayd if one available. Availability is | |
862 | * known if the stream is still in the list of the channel. | |
863 | */ | |
864 | if (cds_list_empty(&metadata->streams.head)) { | |
865 | ERR("Metadata channel key %" PRIu64 ", no stream available.", key); | |
866 | ret = LTTCOMM_CONSUMERD_ERROR_METADATA; | |
f5a0c9cf | 867 | goto error_no_stream; |
d88aee68 DG |
868 | } |
869 | ||
870 | /* Send metadata stream to relayd if needed. */ | |
62285ea4 DG |
871 | if (metadata->metadata_stream->net_seq_idx != (uint64_t) -1ULL) { |
872 | ret = consumer_send_relayd_stream(metadata->metadata_stream, | |
873 | metadata->pathname); | |
874 | if (ret < 0) { | |
875 | ret = LTTCOMM_CONSUMERD_ERROR_METADATA; | |
876 | goto error; | |
877 | } | |
601262d6 JD |
878 | ret = consumer_send_relayd_streams_sent( |
879 | metadata->metadata_stream->net_seq_idx); | |
880 | if (ret < 0) { | |
881 | ret = LTTCOMM_CONSUMERD_RELAYD_FAIL; | |
882 | goto error; | |
883 | } | |
d88aee68 DG |
884 | } |
885 | ||
a8086cf4 JR |
886 | /* |
887 | * Ownership of metadata stream is passed along. Freeing is handled by | |
888 | * the callee. | |
889 | */ | |
d88aee68 DG |
890 | ret = send_streams_to_thread(metadata, ctx); |
891 | if (ret < 0) { | |
892 | /* | |
893 | * If we are unable to send the stream to the thread, there is | |
894 | * a big problem so just stop everything. | |
895 | */ | |
896 | ret = LTTCOMM_CONSUMERD_FATAL; | |
a8086cf4 | 897 | goto send_streams_error; |
d88aee68 DG |
898 | } |
899 | /* List MUST be empty after or else it could be reused. */ | |
a0377dfe | 900 | LTTNG_ASSERT(cds_list_empty(&metadata->streams.head)); |
d88aee68 | 901 | |
10a50311 JD |
902 | ret = 0; |
903 | goto end; | |
d88aee68 DG |
904 | |
905 | error: | |
f2a444f1 DG |
906 | /* |
907 | * Delete metadata channel on error. At this point, the metadata stream can | |
908 | * NOT be monitored by the metadata thread thus having the guarantee that | |
909 | * the stream is still in the local stream list of the channel. This call | |
910 | * will make sure to clean that list. | |
911 | */ | |
f5a0c9cf | 912 | consumer_stream_destroy(metadata->metadata_stream, NULL); |
212d67a2 | 913 | metadata->metadata_stream = NULL; |
a8086cf4 | 914 | send_streams_error: |
f5a0c9cf | 915 | error_no_stream: |
10a50311 JD |
916 | end: |
917 | return ret; | |
918 | } | |
919 | ||
920 | /* | |
921 | * Snapshot the whole metadata. | |
d2956687 | 922 | * RCU read-side lock must be held by the caller. |
10a50311 JD |
923 | * |
924 | * Returns 0 on success, < 0 on error | |
925 | */ | |
3eb928aa MD |
926 | static int snapshot_metadata(struct lttng_consumer_channel *metadata_channel, |
927 | uint64_t key, char *path, uint64_t relayd_id, | |
d2956687 | 928 | struct lttng_consumer_local_data *ctx) |
10a50311 JD |
929 | { |
930 | int ret = 0; | |
10a50311 JD |
931 | struct lttng_consumer_stream *metadata_stream; |
932 | ||
a0377dfe FD |
933 | LTTNG_ASSERT(path); |
934 | LTTNG_ASSERT(ctx); | |
48b7cdc2 | 935 | ASSERT_RCU_READ_LOCKED(); |
10a50311 JD |
936 | |
937 | DBG("UST consumer snapshot metadata with key %" PRIu64 " at path %s", | |
938 | key, path); | |
939 | ||
940 | rcu_read_lock(); | |
941 | ||
a0377dfe | 942 | LTTNG_ASSERT(!metadata_channel->monitor); |
10a50311 | 943 | |
9ce5646a MD |
944 | health_code_update(); |
945 | ||
10a50311 JD |
946 | /* |
947 | * Ask the sessiond if we have new metadata waiting and update the | |
948 | * consumer metadata cache. | |
949 | */ | |
94d49140 | 950 | ret = lttng_ustconsumer_request_metadata(ctx, metadata_channel, 0, 1); |
10a50311 JD |
951 | if (ret < 0) { |
952 | goto error; | |
953 | } | |
954 | ||
9ce5646a MD |
955 | health_code_update(); |
956 | ||
10a50311 JD |
957 | /* |
958 | * The metadata stream is NOT created in no monitor mode when the channel | |
959 | * is created on a sessiond ask channel command. | |
960 | */ | |
d2956687 | 961 | ret = create_ust_streams(metadata_channel, ctx); |
10a50311 JD |
962 | if (ret < 0) { |
963 | goto error; | |
964 | } | |
965 | ||
966 | metadata_stream = metadata_channel->metadata_stream; | |
a0377dfe | 967 | LTTNG_ASSERT(metadata_stream); |
10a50311 | 968 | |
947bd097 | 969 | metadata_stream->read_subbuffer_ops.lock(metadata_stream); |
10a50311 JD |
970 | if (relayd_id != (uint64_t) -1ULL) { |
971 | metadata_stream->net_seq_idx = relayd_id; | |
972 | ret = consumer_send_relayd_stream(metadata_stream, path); | |
10a50311 | 973 | } else { |
d2956687 JG |
974 | ret = consumer_stream_create_output_files(metadata_stream, |
975 | false); | |
976 | } | |
d2956687 JG |
977 | if (ret < 0) { |
978 | goto error_stream; | |
10a50311 JD |
979 | } |
980 | ||
04ef1097 | 981 | do { |
9ce5646a | 982 | health_code_update(); |
6f9449c2 | 983 | ret = lttng_consumer_read_subbuffer(metadata_stream, ctx, true); |
10a50311 | 984 | if (ret < 0) { |
94d49140 | 985 | goto error_stream; |
10a50311 | 986 | } |
04ef1097 | 987 | } while (ret > 0); |
10a50311 | 988 | |
10a50311 | 989 | error_stream: |
947bd097 | 990 | metadata_stream->read_subbuffer_ops.unlock(metadata_stream); |
10a50311 | 991 | /* |
947bd097 JR |
992 | * Clean up the stream completely because the next snapshot will use a |
993 | * new metadata stream. | |
10a50311 | 994 | */ |
10a50311 JD |
995 | consumer_stream_destroy(metadata_stream, NULL); |
996 | metadata_channel->metadata_stream = NULL; | |
997 | ||
998 | error: | |
999 | rcu_read_unlock(); | |
1000 | return ret; | |
1001 | } | |
1002 | ||
128708c3 JG |
1003 | static |
1004 | int get_current_subbuf_addr(struct lttng_consumer_stream *stream, | |
1005 | const char **addr) | |
1006 | { | |
1007 | int ret; | |
1008 | unsigned long mmap_offset; | |
1009 | const char *mmap_base; | |
1010 | ||
97535efa | 1011 | mmap_base = (const char *) lttng_ust_ctl_get_mmap_base(stream->ustream); |
128708c3 JG |
1012 | if (!mmap_base) { |
1013 | ERR("Failed to get mmap base for stream `%s`", | |
1014 | stream->name); | |
1015 | ret = -EPERM; | |
1016 | goto error; | |
1017 | } | |
1018 | ||
b623cb6a | 1019 | ret = lttng_ust_ctl_get_mmap_read_offset(stream->ustream, &mmap_offset); |
128708c3 JG |
1020 | if (ret != 0) { |
1021 | ERR("Failed to get mmap offset for stream `%s`", stream->name); | |
1022 | ret = -EINVAL; | |
1023 | goto error; | |
1024 | } | |
1025 | ||
1026 | *addr = mmap_base + mmap_offset; | |
1027 | error: | |
1028 | return ret; | |
1029 | ||
1030 | } | |
1031 | ||
10a50311 JD |
1032 | /* |
1033 | * Take a snapshot of all the stream of a channel. | |
d2956687 | 1034 | * RCU read-side lock and the channel lock must be held by the caller. |
10a50311 JD |
1035 | * |
1036 | * Returns 0 on success, < 0 on error | |
1037 | */ | |
3eb928aa MD |
1038 | static int snapshot_channel(struct lttng_consumer_channel *channel, |
1039 | uint64_t key, char *path, uint64_t relayd_id, | |
e098433c JG |
1040 | uint64_t nb_packets_per_stream, |
1041 | struct lttng_consumer_local_data *ctx) | |
10a50311 JD |
1042 | { |
1043 | int ret; | |
1044 | unsigned use_relayd = 0; | |
1045 | unsigned long consumed_pos, produced_pos; | |
10a50311 JD |
1046 | struct lttng_consumer_stream *stream; |
1047 | ||
a0377dfe FD |
1048 | LTTNG_ASSERT(path); |
1049 | LTTNG_ASSERT(ctx); | |
48b7cdc2 | 1050 | ASSERT_RCU_READ_LOCKED(); |
10a50311 JD |
1051 | |
1052 | rcu_read_lock(); | |
1053 | ||
1054 | if (relayd_id != (uint64_t) -1ULL) { | |
1055 | use_relayd = 1; | |
1056 | } | |
1057 | ||
a0377dfe | 1058 | LTTNG_ASSERT(!channel->monitor); |
6a00837f | 1059 | DBG("UST consumer snapshot channel %" PRIu64, key); |
10a50311 JD |
1060 | |
1061 | cds_list_for_each_entry(stream, &channel->streams.head, send_node) { | |
9ce5646a MD |
1062 | health_code_update(); |
1063 | ||
10a50311 JD |
1064 | /* Lock stream because we are about to change its state. */ |
1065 | pthread_mutex_lock(&stream->lock); | |
a0377dfe | 1066 | LTTNG_ASSERT(channel->trace_chunk); |
d2956687 JG |
1067 | if (!lttng_trace_chunk_get(channel->trace_chunk)) { |
1068 | /* | |
1069 | * Can't happen barring an internal error as the channel | |
1070 | * holds a reference to the trace chunk. | |
1071 | */ | |
1072 | ERR("Failed to acquire reference to channel's trace chunk"); | |
1073 | ret = -1; | |
1074 | goto error_unlock; | |
1075 | } | |
a0377dfe | 1076 | LTTNG_ASSERT(!stream->trace_chunk); |
d2956687 JG |
1077 | stream->trace_chunk = channel->trace_chunk; |
1078 | ||
10a50311 JD |
1079 | stream->net_seq_idx = relayd_id; |
1080 | ||
1081 | if (use_relayd) { | |
1082 | ret = consumer_send_relayd_stream(stream, path); | |
1083 | if (ret < 0) { | |
1084 | goto error_unlock; | |
1085 | } | |
1086 | } else { | |
d2956687 JG |
1087 | ret = consumer_stream_create_output_files(stream, |
1088 | false); | |
10a50311 JD |
1089 | if (ret < 0) { |
1090 | goto error_unlock; | |
1091 | } | |
d2956687 JG |
1092 | DBG("UST consumer snapshot stream (%" PRIu64 ")", |
1093 | stream->key); | |
10a50311 JD |
1094 | } |
1095 | ||
d4d80f77 MD |
1096 | /* |
1097 | * If tracing is active, we want to perform a "full" buffer flush. | |
1098 | * Else, if quiescent, it has already been done by the prior stop. | |
1099 | */ | |
1100 | if (!stream->quiescent) { | |
881fc67f MD |
1101 | ret = lttng_ust_ctl_flush_buffer(stream->ustream, 0); |
1102 | if (ret < 0) { | |
1103 | ERR("Failed to flush buffer during snapshot of channel: channel key = %" PRIu64 ", channel name = '%s'", | |
1104 | channel->key, channel->name); | |
1105 | goto error_unlock; | |
1106 | } | |
d4d80f77 | 1107 | } |
10a50311 JD |
1108 | |
1109 | ret = lttng_ustconsumer_take_snapshot(stream); | |
1110 | if (ret < 0) { | |
1111 | ERR("Taking UST snapshot"); | |
1112 | goto error_unlock; | |
1113 | } | |
1114 | ||
1115 | ret = lttng_ustconsumer_get_produced_snapshot(stream, &produced_pos); | |
1116 | if (ret < 0) { | |
1117 | ERR("Produced UST snapshot position"); | |
1118 | goto error_unlock; | |
1119 | } | |
1120 | ||
1121 | ret = lttng_ustconsumer_get_consumed_snapshot(stream, &consumed_pos); | |
1122 | if (ret < 0) { | |
1123 | ERR("Consumerd UST snapshot position"); | |
1124 | goto error_unlock; | |
1125 | } | |
1126 | ||
5c786ded JD |
1127 | /* |
1128 | * The original value is sent back if max stream size is larger than | |
d07ceecd | 1129 | * the possible size of the snapshot. Also, we assume that the session |
5c786ded JD |
1130 | * daemon should never send a maximum stream size that is lower than |
1131 | * subbuffer size. | |
1132 | */ | |
d07ceecd MD |
1133 | consumed_pos = consumer_get_consume_start_pos(consumed_pos, |
1134 | produced_pos, nb_packets_per_stream, | |
1135 | stream->max_sb_size); | |
5c786ded | 1136 | |
9377d830 | 1137 | while ((long) (consumed_pos - produced_pos) < 0) { |
10a50311 JD |
1138 | ssize_t read_len; |
1139 | unsigned long len, padded_len; | |
128708c3 | 1140 | const char *subbuf_addr; |
fd424d99 | 1141 | struct lttng_buffer_view subbuf_view; |
10a50311 | 1142 | |
9ce5646a MD |
1143 | health_code_update(); |
1144 | ||
10a50311 JD |
1145 | DBG("UST consumer taking snapshot at pos %lu", consumed_pos); |
1146 | ||
b623cb6a | 1147 | ret = lttng_ust_ctl_get_subbuf(stream->ustream, &consumed_pos); |
10a50311 JD |
1148 | if (ret < 0) { |
1149 | if (ret != -EAGAIN) { | |
b623cb6a | 1150 | PERROR("lttng_ust_ctl_get_subbuf snapshot"); |
10a50311 JD |
1151 | goto error_close_stream; |
1152 | } | |
1153 | DBG("UST consumer get subbuf failed. Skipping it."); | |
1154 | consumed_pos += stream->max_sb_size; | |
ddc93ee4 | 1155 | stream->chan->lost_packets++; |
10a50311 JD |
1156 | continue; |
1157 | } | |
1158 | ||
b623cb6a | 1159 | ret = lttng_ust_ctl_get_subbuf_size(stream->ustream, &len); |
10a50311 | 1160 | if (ret < 0) { |
b623cb6a | 1161 | ERR("Snapshot lttng_ust_ctl_get_subbuf_size"); |
10a50311 JD |
1162 | goto error_put_subbuf; |
1163 | } | |
1164 | ||
b623cb6a | 1165 | ret = lttng_ust_ctl_get_padded_subbuf_size(stream->ustream, &padded_len); |
10a50311 | 1166 | if (ret < 0) { |
b623cb6a | 1167 | ERR("Snapshot lttng_ust_ctl_get_padded_subbuf_size"); |
10a50311 JD |
1168 | goto error_put_subbuf; |
1169 | } | |
1170 | ||
128708c3 JG |
1171 | ret = get_current_subbuf_addr(stream, &subbuf_addr); |
1172 | if (ret) { | |
1173 | goto error_put_subbuf; | |
1174 | } | |
1175 | ||
fd424d99 JG |
1176 | subbuf_view = lttng_buffer_view_init( |
1177 | subbuf_addr, 0, padded_len); | |
f5ba75b4 | 1178 | read_len = lttng_consumer_on_read_subbuffer_mmap( |
6f9449c2 | 1179 | stream, &subbuf_view, padded_len - len); |
10a50311 JD |
1180 | if (use_relayd) { |
1181 | if (read_len != len) { | |
56591bac | 1182 | ret = -EPERM; |
10a50311 JD |
1183 | goto error_put_subbuf; |
1184 | } | |
1185 | } else { | |
1186 | if (read_len != padded_len) { | |
56591bac | 1187 | ret = -EPERM; |
10a50311 JD |
1188 | goto error_put_subbuf; |
1189 | } | |
1190 | } | |
1191 | ||
b623cb6a | 1192 | ret = lttng_ust_ctl_put_subbuf(stream->ustream); |
10a50311 | 1193 | if (ret < 0) { |
b623cb6a | 1194 | ERR("Snapshot lttng_ust_ctl_put_subbuf"); |
10a50311 JD |
1195 | goto error_close_stream; |
1196 | } | |
1197 | consumed_pos += stream->max_sb_size; | |
1198 | } | |
1199 | ||
1200 | /* Simply close the stream so we can use it on the next snapshot. */ | |
1201 | consumer_stream_close(stream); | |
1202 | pthread_mutex_unlock(&stream->lock); | |
1203 | } | |
1204 | ||
1205 | rcu_read_unlock(); | |
1206 | return 0; | |
1207 | ||
1208 | error_put_subbuf: | |
b623cb6a MJ |
1209 | if (lttng_ust_ctl_put_subbuf(stream->ustream) < 0) { |
1210 | ERR("Snapshot lttng_ust_ctl_put_subbuf"); | |
10a50311 JD |
1211 | } |
1212 | error_close_stream: | |
1213 | consumer_stream_close(stream); | |
1214 | error_unlock: | |
1215 | pthread_mutex_unlock(&stream->lock); | |
10a50311 | 1216 | rcu_read_unlock(); |
d88aee68 DG |
1217 | return ret; |
1218 | } | |
1219 | ||
b1316da1 JG |
1220 | static |
1221 | void metadata_stream_reset_cache_consumed_position( | |
1222 | struct lttng_consumer_stream *stream) | |
1223 | { | |
1224 | ASSERT_LOCKED(stream->lock); | |
1225 | ||
1226 | DBG("Reset metadata cache of session %" PRIu64, | |
1227 | stream->chan->session_id); | |
1228 | stream->ust_metadata_pushed = 0; | |
1229 | } | |
1230 | ||
331744e3 | 1231 | /* |
c585821b MD |
1232 | * Receive the metadata updates from the sessiond. Supports receiving |
1233 | * overlapping metadata, but is needs to always belong to a contiguous | |
1234 | * range starting from 0. | |
1235 | * Be careful about the locks held when calling this function: it needs | |
1236 | * the metadata cache flush to concurrently progress in order to | |
1237 | * complete. | |
331744e3 JD |
1238 | */ |
1239 | int lttng_ustconsumer_recv_metadata(int sock, uint64_t key, uint64_t offset, | |
93ec662e JD |
1240 | uint64_t len, uint64_t version, |
1241 | struct lttng_consumer_channel *channel, int timer, int wait) | |
331744e3 | 1242 | { |
0c759fc9 | 1243 | int ret, ret_code = LTTCOMM_CONSUMERD_SUCCESS; |
331744e3 | 1244 | char *metadata_str; |
b1316da1 | 1245 | enum consumer_metadata_cache_write_status cache_write_status; |
331744e3 | 1246 | |
8fd623e0 | 1247 | DBG("UST consumer push metadata key %" PRIu64 " of len %" PRIu64, key, len); |
331744e3 | 1248 | |
64803277 | 1249 | metadata_str = calloc<char>(len); |
331744e3 JD |
1250 | if (!metadata_str) { |
1251 | PERROR("zmalloc metadata string"); | |
1252 | ret_code = LTTCOMM_CONSUMERD_ENOMEM; | |
1253 | goto end; | |
1254 | } | |
1255 | ||
9ce5646a MD |
1256 | health_code_update(); |
1257 | ||
331744e3 JD |
1258 | /* Receive metadata string. */ |
1259 | ret = lttcomm_recv_unix_sock(sock, metadata_str, len); | |
1260 | if (ret < 0) { | |
1261 | /* Session daemon is dead so return gracefully. */ | |
1262 | ret_code = ret; | |
1263 | goto end_free; | |
1264 | } | |
1265 | ||
9ce5646a MD |
1266 | health_code_update(); |
1267 | ||
331744e3 | 1268 | pthread_mutex_lock(&channel->metadata_cache->lock); |
b1316da1 | 1269 | cache_write_status = consumer_metadata_cache_write( |
a25d34bc JG |
1270 | channel->metadata_cache, offset, len, version, |
1271 | metadata_str); | |
3bdc49f3 | 1272 | pthread_mutex_unlock(&channel->metadata_cache->lock); |
b1316da1 JG |
1273 | switch (cache_write_status) { |
1274 | case CONSUMER_METADATA_CACHE_WRITE_STATUS_NO_CHANGE: | |
1275 | /* | |
1276 | * The write entirely overlapped with existing contents of the | |
1277 | * same metadata version (same content); there is nothing to do. | |
1278 | */ | |
1279 | break; | |
1280 | case CONSUMER_METADATA_CACHE_WRITE_STATUS_INVALIDATED: | |
1281 | /* | |
1282 | * The metadata cache was invalidated (previously pushed | |
1283 | * content has been overwritten). Reset the stream's consumed | |
1284 | * metadata position to ensure the metadata poll thread consumes | |
1285 | * the whole cache. | |
1286 | */ | |
947bd097 JR |
1287 | |
1288 | /* | |
1289 | * channel::metadata_stream can be null when the metadata | |
1290 | * channel is under a snapshot session type. No need to update | |
1291 | * the stream position in that scenario. | |
1292 | */ | |
1293 | if (channel->metadata_stream != NULL) { | |
1294 | pthread_mutex_lock(&channel->metadata_stream->lock); | |
1295 | metadata_stream_reset_cache_consumed_position( | |
1296 | channel->metadata_stream); | |
1297 | pthread_mutex_unlock(&channel->metadata_stream->lock); | |
1298 | } else { | |
1299 | /* Validate we are in snapshot mode. */ | |
1300 | LTTNG_ASSERT(!channel->monitor); | |
1301 | } | |
b1316da1 JG |
1302 | /* Fall-through. */ |
1303 | case CONSUMER_METADATA_CACHE_WRITE_STATUS_APPENDED_CONTENT: | |
1304 | /* | |
1305 | * In both cases, the metadata poll thread has new data to | |
1306 | * consume. | |
1307 | */ | |
1308 | ret = consumer_metadata_wakeup_pipe(channel); | |
1309 | if (ret) { | |
1310 | ret_code = LTTCOMM_CONSUMERD_ERROR_METADATA; | |
1311 | goto end_free; | |
1312 | } | |
1313 | break; | |
1314 | case CONSUMER_METADATA_CACHE_WRITE_STATUS_ERROR: | |
331744e3 JD |
1315 | /* Unable to handle metadata. Notify session daemon. */ |
1316 | ret_code = LTTCOMM_CONSUMERD_ERROR_METADATA; | |
a32bd775 DG |
1317 | /* |
1318 | * Skip metadata flush on write error since the offset and len might | |
1319 | * not have been updated which could create an infinite loop below when | |
1320 | * waiting for the metadata cache to be flushed. | |
1321 | */ | |
a32bd775 | 1322 | goto end_free; |
b1316da1 JG |
1323 | default: |
1324 | abort(); | |
331744e3 | 1325 | } |
331744e3 | 1326 | |
94d49140 JD |
1327 | if (!wait) { |
1328 | goto end_free; | |
1329 | } | |
5e41ebe1 | 1330 | while (consumer_metadata_cache_flushed(channel, offset + len, timer)) { |
331744e3 | 1331 | DBG("Waiting for metadata to be flushed"); |
9ce5646a MD |
1332 | |
1333 | health_code_update(); | |
1334 | ||
331744e3 JD |
1335 | usleep(DEFAULT_METADATA_AVAILABILITY_WAIT_TIME); |
1336 | } | |
1337 | ||
1338 | end_free: | |
1339 | free(metadata_str); | |
1340 | end: | |
1341 | return ret_code; | |
1342 | } | |
1343 | ||
4cbc1a04 DG |
1344 | /* |
1345 | * Receive command from session daemon and process it. | |
1346 | * | |
1347 | * Return 1 on success else a negative value or 0. | |
1348 | */ | |
3bd1e081 MD |
1349 | int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data *ctx, |
1350 | int sock, struct pollfd *consumer_sockpoll) | |
1351 | { | |
594c7c00 | 1352 | int ret_func; |
0c759fc9 | 1353 | enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS; |
3bd1e081 | 1354 | struct lttcomm_consumer_msg msg; |
ffe60014 | 1355 | struct lttng_consumer_channel *channel = NULL; |
3bd1e081 | 1356 | |
9ce5646a MD |
1357 | health_code_update(); |
1358 | ||
594c7c00 SM |
1359 | { |
1360 | ssize_t ret_recv; | |
1361 | ||
1362 | ret_recv = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg)); | |
1363 | if (ret_recv != sizeof(msg)) { | |
1364 | DBG("Consumer received unexpected message size %zd (expects %zu)", | |
1365 | ret_recv, sizeof(msg)); | |
1366 | /* | |
1367 | * The ret value might 0 meaning an orderly shutdown but this is ok | |
1368 | * since the caller handles this. | |
1369 | */ | |
1370 | if (ret_recv > 0) { | |
1371 | lttng_consumer_send_error(ctx, | |
1372 | LTTCOMM_CONSUMERD_ERROR_RECV_CMD); | |
1373 | ret_recv = -1; | |
1374 | } | |
1375 | return ret_recv; | |
489f70e9 | 1376 | } |
3bd1e081 | 1377 | } |
9ce5646a MD |
1378 | |
1379 | health_code_update(); | |
1380 | ||
84382d49 | 1381 | /* deprecated */ |
a0377dfe | 1382 | LTTNG_ASSERT(msg.cmd_type != LTTNG_CONSUMER_STOP); |
3bd1e081 | 1383 | |
9ce5646a MD |
1384 | health_code_update(); |
1385 | ||
3f8e211f | 1386 | /* relayd needs RCU read-side lock */ |
b0b335c8 MD |
1387 | rcu_read_lock(); |
1388 | ||
3bd1e081 | 1389 | switch (msg.cmd_type) { |
00e2e675 DG |
1390 | case LTTNG_CONSUMER_ADD_RELAYD_SOCKET: |
1391 | { | |
4222116f JR |
1392 | uint32_t major = msg.u.relayd_sock.major; |
1393 | uint32_t minor = msg.u.relayd_sock.minor; | |
1394 | enum lttcomm_sock_proto protocol = | |
1395 | (enum lttcomm_sock_proto) msg.u.relayd_sock | |
1396 | .relayd_socket_protocol; | |
1397 | ||
f50f23d9 | 1398 | /* Session daemon status message are handled in the following call. */ |
2527bf85 | 1399 | consumer_add_relayd_socket(msg.u.relayd_sock.net_index, |
4222116f JR |
1400 | msg.u.relayd_sock.type, ctx, sock, |
1401 | consumer_sockpoll, msg.u.relayd_sock.session_id, | |
1402 | msg.u.relayd_sock.relayd_session_id, major, | |
1403 | minor, protocol); | |
00e2e675 DG |
1404 | goto end_nosignal; |
1405 | } | |
173af62f DG |
1406 | case LTTNG_CONSUMER_DESTROY_RELAYD: |
1407 | { | |
a6ba4fe1 | 1408 | uint64_t index = msg.u.destroy_relayd.net_seq_idx; |
173af62f DG |
1409 | struct consumer_relayd_sock_pair *relayd; |
1410 | ||
a6ba4fe1 | 1411 | DBG("UST consumer destroying relayd %" PRIu64, index); |
173af62f DG |
1412 | |
1413 | /* Get relayd reference if exists. */ | |
a6ba4fe1 | 1414 | relayd = consumer_find_relayd(index); |
173af62f | 1415 | if (relayd == NULL) { |
3448e266 | 1416 | DBG("Unable to find relayd %" PRIu64, index); |
e462382a | 1417 | ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL; |
173af62f DG |
1418 | } |
1419 | ||
a6ba4fe1 DG |
1420 | /* |
1421 | * Each relayd socket pair has a refcount of stream attached to it | |
1422 | * which tells if the relayd is still active or not depending on the | |
1423 | * refcount value. | |
1424 | * | |
1425 | * This will set the destroy flag of the relayd object and destroy it | |
1426 | * if the refcount reaches zero when called. | |
1427 | * | |
1428 | * The destroy can happen either here or when a stream fd hangs up. | |
1429 | */ | |
f50f23d9 DG |
1430 | if (relayd) { |
1431 | consumer_flag_relayd_for_destroy(relayd); | |
1432 | } | |
1433 | ||
d88aee68 | 1434 | goto end_msg_sessiond; |
173af62f | 1435 | } |
3bd1e081 MD |
1436 | case LTTNG_CONSUMER_UPDATE_STREAM: |
1437 | { | |
3f8e211f | 1438 | rcu_read_unlock(); |
7ad0a0cb | 1439 | return -ENOSYS; |
3bd1e081 | 1440 | } |
6d805429 | 1441 | case LTTNG_CONSUMER_DATA_PENDING: |
53632229 | 1442 | { |
594c7c00 SM |
1443 | int is_data_pending; |
1444 | ssize_t ret_send; | |
6d805429 | 1445 | uint64_t id = msg.u.data_pending.session_id; |
ca22feea | 1446 | |
6d805429 | 1447 | DBG("UST consumer data pending command for id %" PRIu64, id); |
ca22feea | 1448 | |
3be74084 | 1449 | is_data_pending = consumer_data_pending(id); |
ca22feea DG |
1450 | |
1451 | /* Send back returned value to session daemon */ | |
594c7c00 | 1452 | ret_send = lttcomm_send_unix_sock(sock, &is_data_pending, |
3be74084 | 1453 | sizeof(is_data_pending)); |
594c7c00 SM |
1454 | if (ret_send < 0) { |
1455 | DBG("Error when sending the data pending ret code: %zd", | |
1456 | ret_send); | |
489f70e9 | 1457 | goto error_fatal; |
ca22feea | 1458 | } |
f50f23d9 DG |
1459 | |
1460 | /* | |
1461 | * No need to send back a status message since the data pending | |
1462 | * returned value is the response. | |
1463 | */ | |
ca22feea | 1464 | break; |
53632229 | 1465 | } |
ffe60014 DG |
1466 | case LTTNG_CONSUMER_ASK_CHANNEL_CREATION: |
1467 | { | |
594c7c00 | 1468 | int ret_ask_channel, ret_add_channel, ret_send; |
b623cb6a | 1469 | struct lttng_ust_ctl_consumer_channel_attr attr; |
d2956687 JG |
1470 | const uint64_t chunk_id = msg.u.ask_channel.chunk_id.value; |
1471 | const struct lttng_credentials buffer_credentials = { | |
ff588497 JR |
1472 | .uid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.ask_channel.buffer_credentials.uid), |
1473 | .gid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.ask_channel.buffer_credentials.gid), | |
d2956687 | 1474 | }; |
ffe60014 DG |
1475 | |
1476 | /* Create a plain object and reserve a channel key. */ | |
a2814ea7 JG |
1477 | channel = consumer_allocate_channel( |
1478 | msg.u.ask_channel.key, | |
1479 | msg.u.ask_channel.session_id, | |
d2956687 JG |
1480 | msg.u.ask_channel.chunk_id.is_set ? |
1481 | &chunk_id : NULL, | |
1482 | msg.u.ask_channel.pathname, | |
1483 | msg.u.ask_channel.name, | |
1484 | msg.u.ask_channel.relayd_id, | |
1624d5b7 JD |
1485 | (enum lttng_event_output) msg.u.ask_channel.output, |
1486 | msg.u.ask_channel.tracefile_size, | |
2bba9e53 | 1487 | msg.u.ask_channel.tracefile_count, |
1950109e | 1488 | msg.u.ask_channel.session_id_per_pid, |
ecc48a90 | 1489 | msg.u.ask_channel.monitor, |
d7ba1388 | 1490 | msg.u.ask_channel.live_timer_interval, |
a2814ea7 | 1491 | msg.u.ask_channel.is_live, |
3d071855 | 1492 | msg.u.ask_channel.root_shm_path, |
d7ba1388 | 1493 | msg.u.ask_channel.shm_path); |
ffe60014 DG |
1494 | if (!channel) { |
1495 | goto end_channel_error; | |
1496 | } | |
1497 | ||
d2956687 JG |
1498 | LTTNG_OPTIONAL_SET(&channel->buffer_credentials, |
1499 | buffer_credentials); | |
1500 | ||
567eb353 DG |
1501 | /* |
1502 | * Assign UST application UID to the channel. This value is ignored for | |
1503 | * per PID buffers. This is specific to UST thus setting this after the | |
1504 | * allocation. | |
1505 | */ | |
1506 | channel->ust_app_uid = msg.u.ask_channel.ust_app_uid; | |
1507 | ||
ffe60014 DG |
1508 | /* Build channel attributes from received message. */ |
1509 | attr.subbuf_size = msg.u.ask_channel.subbuf_size; | |
1510 | attr.num_subbuf = msg.u.ask_channel.num_subbuf; | |
1511 | attr.overwrite = msg.u.ask_channel.overwrite; | |
1512 | attr.switch_timer_interval = msg.u.ask_channel.switch_timer_interval; | |
1513 | attr.read_timer_interval = msg.u.ask_channel.read_timer_interval; | |
7972aab2 | 1514 | attr.chan_id = msg.u.ask_channel.chan_id; |
ffe60014 | 1515 | memcpy(attr.uuid, msg.u.ask_channel.uuid, sizeof(attr.uuid)); |
491d1539 | 1516 | attr.blocking_timeout= msg.u.ask_channel.blocking_timeout; |
ffe60014 | 1517 | |
0c759fc9 DG |
1518 | /* Match channel buffer type to the UST abi. */ |
1519 | switch (msg.u.ask_channel.output) { | |
1520 | case LTTNG_EVENT_MMAP: | |
1521 | default: | |
fc4b93fa | 1522 | attr.output = LTTNG_UST_ABI_MMAP; |
0c759fc9 DG |
1523 | break; |
1524 | } | |
1525 | ||
ffe60014 DG |
1526 | /* Translate and save channel type. */ |
1527 | switch (msg.u.ask_channel.type) { | |
fc4b93fa | 1528 | case LTTNG_UST_ABI_CHAN_PER_CPU: |
ffe60014 | 1529 | channel->type = CONSUMER_CHANNEL_TYPE_DATA; |
fc4b93fa | 1530 | attr.type = LTTNG_UST_ABI_CHAN_PER_CPU; |
8633d6e3 MD |
1531 | /* |
1532 | * Set refcount to 1 for owner. Below, we will | |
1533 | * pass ownership to the | |
1534 | * consumer_thread_channel_poll() thread. | |
1535 | */ | |
1536 | channel->refcount = 1; | |
ffe60014 | 1537 | break; |
fc4b93fa | 1538 | case LTTNG_UST_ABI_CHAN_METADATA: |
ffe60014 | 1539 | channel->type = CONSUMER_CHANNEL_TYPE_METADATA; |
fc4b93fa | 1540 | attr.type = LTTNG_UST_ABI_CHAN_METADATA; |
ffe60014 DG |
1541 | break; |
1542 | default: | |
a0377dfe | 1543 | abort(); |
ffe60014 DG |
1544 | goto error_fatal; |
1545 | }; | |
1546 | ||
9ce5646a MD |
1547 | health_code_update(); |
1548 | ||
594c7c00 SM |
1549 | ret_ask_channel = ask_channel(ctx, channel, &attr); |
1550 | if (ret_ask_channel < 0) { | |
ffe60014 DG |
1551 | goto end_channel_error; |
1552 | } | |
1553 | ||
fc4b93fa | 1554 | if (msg.u.ask_channel.type == LTTNG_UST_ABI_CHAN_METADATA) { |
594c7c00 SM |
1555 | int ret_allocate; |
1556 | ||
1557 | ret_allocate = consumer_metadata_cache_allocate( | |
1558 | channel); | |
1559 | if (ret_allocate < 0) { | |
fc643247 MD |
1560 | ERR("Allocating metadata cache"); |
1561 | goto end_channel_error; | |
1562 | } | |
1563 | consumer_timer_switch_start(channel, attr.switch_timer_interval); | |
1564 | attr.switch_timer_interval = 0; | |
94d49140 | 1565 | } else { |
e9404c27 JG |
1566 | int monitor_start_ret; |
1567 | ||
94d49140 JD |
1568 | consumer_timer_live_start(channel, |
1569 | msg.u.ask_channel.live_timer_interval); | |
e9404c27 JG |
1570 | monitor_start_ret = consumer_timer_monitor_start( |
1571 | channel, | |
1572 | msg.u.ask_channel.monitor_timer_interval); | |
1573 | if (monitor_start_ret < 0) { | |
1574 | ERR("Starting channel monitoring timer failed"); | |
1575 | goto end_channel_error; | |
1576 | } | |
fc643247 MD |
1577 | } |
1578 | ||
9ce5646a MD |
1579 | health_code_update(); |
1580 | ||
ffe60014 DG |
1581 | /* |
1582 | * Add the channel to the internal state AFTER all streams were created | |
1583 | * and successfully sent to session daemon. This way, all streams must | |
1584 | * be ready before this channel is visible to the threads. | |
fc643247 MD |
1585 | * If add_channel succeeds, ownership of the channel is |
1586 | * passed to consumer_thread_channel_poll(). | |
ffe60014 | 1587 | */ |
594c7c00 SM |
1588 | ret_add_channel = add_channel(channel, ctx); |
1589 | if (ret_add_channel < 0) { | |
fc4b93fa | 1590 | if (msg.u.ask_channel.type == LTTNG_UST_ABI_CHAN_METADATA) { |
ea88ca2a MD |
1591 | if (channel->switch_timer_enabled == 1) { |
1592 | consumer_timer_switch_stop(channel); | |
1593 | } | |
1594 | consumer_metadata_cache_destroy(channel); | |
1595 | } | |
d3e2ba59 JD |
1596 | if (channel->live_timer_enabled == 1) { |
1597 | consumer_timer_live_stop(channel); | |
1598 | } | |
e9404c27 JG |
1599 | if (channel->monitor_timer_enabled == 1) { |
1600 | consumer_timer_monitor_stop(channel); | |
1601 | } | |
ffe60014 DG |
1602 | goto end_channel_error; |
1603 | } | |
1604 | ||
9ce5646a MD |
1605 | health_code_update(); |
1606 | ||
ffe60014 DG |
1607 | /* |
1608 | * Channel and streams are now created. Inform the session daemon that | |
1609 | * everything went well and should wait to receive the channel and | |
1610 | * streams with ustctl API. | |
1611 | */ | |
594c7c00 SM |
1612 | ret_send = consumer_send_status_channel(sock, channel); |
1613 | if (ret_send < 0) { | |
ffe60014 | 1614 | /* |
489f70e9 | 1615 | * There is probably a problem on the socket. |
ffe60014 | 1616 | */ |
489f70e9 | 1617 | goto error_fatal; |
ffe60014 DG |
1618 | } |
1619 | ||
1620 | break; | |
1621 | } | |
1622 | case LTTNG_CONSUMER_GET_CHANNEL: | |
1623 | { | |
1624 | int ret, relayd_err = 0; | |
d88aee68 | 1625 | uint64_t key = msg.u.get_channel.key; |
594c7c00 | 1626 | struct lttng_consumer_channel *found_channel; |
ffe60014 | 1627 | |
594c7c00 SM |
1628 | found_channel = consumer_find_channel(key); |
1629 | if (!found_channel) { | |
8fd623e0 | 1630 | ERR("UST consumer get channel key %" PRIu64 " not found", key); |
e462382a | 1631 | ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND; |
f3a1fc7b | 1632 | goto end_get_channel; |
ffe60014 DG |
1633 | } |
1634 | ||
9ce5646a MD |
1635 | health_code_update(); |
1636 | ||
a3a86f35 | 1637 | /* Send the channel to sessiond (and relayd, if applicable). */ |
594c7c00 SM |
1638 | ret = send_channel_to_sessiond_and_relayd( |
1639 | sock, found_channel, ctx, &relayd_err); | |
ffe60014 DG |
1640 | if (ret < 0) { |
1641 | if (relayd_err) { | |
1642 | /* | |
1643 | * We were unable to send to the relayd the stream so avoid | |
1644 | * sending back a fatal error to the thread since this is OK | |
f2a444f1 DG |
1645 | * and the consumer can continue its work. The above call |
1646 | * has sent the error status message to the sessiond. | |
ffe60014 | 1647 | */ |
f3a1fc7b | 1648 | goto end_get_channel_nosignal; |
ffe60014 DG |
1649 | } |
1650 | /* | |
1651 | * The communicaton was broken hence there is a bad state between | |
1652 | * the consumer and sessiond so stop everything. | |
1653 | */ | |
f3a1fc7b | 1654 | goto error_get_channel_fatal; |
ffe60014 DG |
1655 | } |
1656 | ||
9ce5646a MD |
1657 | health_code_update(); |
1658 | ||
10a50311 JD |
1659 | /* |
1660 | * In no monitor mode, the streams ownership is kept inside the channel | |
1661 | * so don't send them to the data thread. | |
1662 | */ | |
594c7c00 | 1663 | if (!found_channel->monitor) { |
f3a1fc7b | 1664 | goto end_get_channel; |
10a50311 JD |
1665 | } |
1666 | ||
594c7c00 | 1667 | ret = send_streams_to_thread(found_channel, ctx); |
d88aee68 DG |
1668 | if (ret < 0) { |
1669 | /* | |
1670 | * If we are unable to send the stream to the thread, there is | |
1671 | * a big problem so just stop everything. | |
1672 | */ | |
f3a1fc7b | 1673 | goto error_get_channel_fatal; |
ffe60014 | 1674 | } |
ffe60014 | 1675 | /* List MUST be empty after or else it could be reused. */ |
a0377dfe | 1676 | LTTNG_ASSERT(cds_list_empty(&found_channel->streams.head)); |
f3a1fc7b | 1677 | end_get_channel: |
d88aee68 | 1678 | goto end_msg_sessiond; |
f3a1fc7b JG |
1679 | error_get_channel_fatal: |
1680 | goto error_fatal; | |
1681 | end_get_channel_nosignal: | |
1682 | goto end_nosignal; | |
d88aee68 DG |
1683 | } |
1684 | case LTTNG_CONSUMER_DESTROY_CHANNEL: | |
1685 | { | |
1686 | uint64_t key = msg.u.destroy_channel.key; | |
d88aee68 | 1687 | |
a0cbdd2e MD |
1688 | /* |
1689 | * Only called if streams have not been sent to stream | |
1690 | * manager thread. However, channel has been sent to | |
1691 | * channel manager thread. | |
1692 | */ | |
1693 | notify_thread_del_channel(ctx, key); | |
d88aee68 | 1694 | goto end_msg_sessiond; |
ffe60014 | 1695 | } |
d88aee68 DG |
1696 | case LTTNG_CONSUMER_CLOSE_METADATA: |
1697 | { | |
1698 | int ret; | |
1699 | ||
1700 | ret = close_metadata(msg.u.close_metadata.key); | |
1701 | if (ret != 0) { | |
97535efa | 1702 | ret_code = (lttcomm_return_code) ret; |
d88aee68 DG |
1703 | } |
1704 | ||
1705 | goto end_msg_sessiond; | |
1706 | } | |
7972aab2 DG |
1707 | case LTTNG_CONSUMER_FLUSH_CHANNEL: |
1708 | { | |
1709 | int ret; | |
1710 | ||
1711 | ret = flush_channel(msg.u.flush_channel.key); | |
1712 | if (ret != 0) { | |
97535efa | 1713 | ret_code = (lttcomm_return_code) ret; |
7972aab2 DG |
1714 | } |
1715 | ||
1716 | goto end_msg_sessiond; | |
1717 | } | |
0dd01979 MD |
1718 | case LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL: |
1719 | { | |
1720 | int ret; | |
1721 | ||
1722 | ret = clear_quiescent_channel( | |
1723 | msg.u.clear_quiescent_channel.key); | |
1724 | if (ret != 0) { | |
97535efa | 1725 | ret_code = (lttcomm_return_code) ret; |
0dd01979 MD |
1726 | } |
1727 | ||
1728 | goto end_msg_sessiond; | |
1729 | } | |
d88aee68 | 1730 | case LTTNG_CONSUMER_PUSH_METADATA: |
ffe60014 DG |
1731 | { |
1732 | int ret; | |
d88aee68 | 1733 | uint64_t len = msg.u.push_metadata.len; |
d88aee68 | 1734 | uint64_t key = msg.u.push_metadata.key; |
331744e3 | 1735 | uint64_t offset = msg.u.push_metadata.target_offset; |
93ec662e | 1736 | uint64_t version = msg.u.push_metadata.version; |
594c7c00 | 1737 | struct lttng_consumer_channel *found_channel; |
ffe60014 | 1738 | |
8fd623e0 DG |
1739 | DBG("UST consumer push metadata key %" PRIu64 " of len %" PRIu64, key, |
1740 | len); | |
ffe60014 | 1741 | |
594c7c00 SM |
1742 | found_channel = consumer_find_channel(key); |
1743 | if (!found_channel) { | |
000baf6a DG |
1744 | /* |
1745 | * This is possible if the metadata creation on the consumer side | |
1746 | * is in flight vis-a-vis a concurrent push metadata from the | |
1747 | * session daemon. Simply return that the channel failed and the | |
1748 | * session daemon will handle that message correctly considering | |
1749 | * that this race is acceptable thus the DBG() statement here. | |
1750 | */ | |
1751 | DBG("UST consumer push metadata %" PRIu64 " not found", key); | |
1752 | ret_code = LTTCOMM_CONSUMERD_CHANNEL_FAIL; | |
a8ffe244 | 1753 | goto end_push_metadata_msg_sessiond; |
d88aee68 DG |
1754 | } |
1755 | ||
9ce5646a MD |
1756 | health_code_update(); |
1757 | ||
c585821b MD |
1758 | if (!len) { |
1759 | /* | |
1760 | * There is nothing to receive. We have simply | |
1761 | * checked whether the channel can be found. | |
1762 | */ | |
1763 | ret_code = LTTCOMM_CONSUMERD_SUCCESS; | |
a8ffe244 | 1764 | goto end_push_metadata_msg_sessiond; |
c585821b MD |
1765 | } |
1766 | ||
d88aee68 | 1767 | /* Tell session daemon we are ready to receive the metadata. */ |
0c759fc9 | 1768 | ret = consumer_send_status_msg(sock, LTTCOMM_CONSUMERD_SUCCESS); |
ffe60014 DG |
1769 | if (ret < 0) { |
1770 | /* Somehow, the session daemon is not responding anymore. */ | |
a8ffe244 | 1771 | goto error_push_metadata_fatal; |
d88aee68 DG |
1772 | } |
1773 | ||
9ce5646a MD |
1774 | health_code_update(); |
1775 | ||
d88aee68 | 1776 | /* Wait for more data. */ |
9ce5646a MD |
1777 | health_poll_entry(); |
1778 | ret = lttng_consumer_poll_socket(consumer_sockpoll); | |
1779 | health_poll_exit(); | |
84382d49 | 1780 | if (ret) { |
a8ffe244 | 1781 | goto error_push_metadata_fatal; |
d88aee68 DG |
1782 | } |
1783 | ||
9ce5646a MD |
1784 | health_code_update(); |
1785 | ||
594c7c00 SM |
1786 | ret = lttng_ustconsumer_recv_metadata(sock, key, offset, len, |
1787 | version, found_channel, 0, 1); | |
d88aee68 | 1788 | if (ret < 0) { |
331744e3 | 1789 | /* error receiving from sessiond */ |
a8ffe244 | 1790 | goto error_push_metadata_fatal; |
331744e3 | 1791 | } else { |
97535efa | 1792 | ret_code = (lttcomm_return_code) ret; |
a8ffe244 | 1793 | goto end_push_metadata_msg_sessiond; |
d88aee68 | 1794 | } |
a8ffe244 JG |
1795 | end_push_metadata_msg_sessiond: |
1796 | goto end_msg_sessiond; | |
1797 | error_push_metadata_fatal: | |
1798 | goto error_fatal; | |
d88aee68 DG |
1799 | } |
1800 | case LTTNG_CONSUMER_SETUP_METADATA: | |
1801 | { | |
1802 | int ret; | |
1803 | ||
1804 | ret = setup_metadata(ctx, msg.u.setup_metadata.key); | |
1805 | if (ret) { | |
97535efa | 1806 | ret_code = (lttcomm_return_code) ret; |
d88aee68 DG |
1807 | } |
1808 | goto end_msg_sessiond; | |
ffe60014 | 1809 | } |
6dc3064a DG |
1810 | case LTTNG_CONSUMER_SNAPSHOT_CHANNEL: |
1811 | { | |
594c7c00 | 1812 | struct lttng_consumer_channel *found_channel; |
3eb928aa | 1813 | uint64_t key = msg.u.snapshot_channel.key; |
594c7c00 | 1814 | int ret_send; |
3eb928aa | 1815 | |
594c7c00 SM |
1816 | found_channel = consumer_find_channel(key); |
1817 | if (!found_channel) { | |
3eb928aa MD |
1818 | DBG("UST snapshot channel not found for key %" PRIu64, key); |
1819 | ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND; | |
10a50311 | 1820 | } else { |
3eb928aa | 1821 | if (msg.u.snapshot_channel.metadata) { |
594c7c00 SM |
1822 | int ret_snapshot; |
1823 | ||
1824 | ret_snapshot = snapshot_metadata(found_channel, | |
1825 | key, | |
3eb928aa MD |
1826 | msg.u.snapshot_channel.pathname, |
1827 | msg.u.snapshot_channel.relayd_id, | |
d2956687 | 1828 | ctx); |
594c7c00 | 1829 | if (ret_snapshot < 0) { |
3eb928aa MD |
1830 | ERR("Snapshot metadata failed"); |
1831 | ret_code = LTTCOMM_CONSUMERD_SNAPSHOT_FAILED; | |
1832 | } | |
1833 | } else { | |
594c7c00 SM |
1834 | int ret_snapshot; |
1835 | ||
1836 | ret_snapshot = snapshot_channel(found_channel, | |
1837 | key, | |
3eb928aa MD |
1838 | msg.u.snapshot_channel.pathname, |
1839 | msg.u.snapshot_channel.relayd_id, | |
594c7c00 SM |
1840 | msg.u.snapshot_channel |
1841 | .nb_packets_per_stream, | |
3eb928aa | 1842 | ctx); |
594c7c00 | 1843 | if (ret_snapshot < 0) { |
3eb928aa MD |
1844 | ERR("Snapshot channel failed"); |
1845 | ret_code = LTTCOMM_CONSUMERD_SNAPSHOT_FAILED; | |
1846 | } | |
10a50311 JD |
1847 | } |
1848 | } | |
9ce5646a | 1849 | health_code_update(); |
594c7c00 SM |
1850 | ret_send = consumer_send_status_msg(sock, ret_code); |
1851 | if (ret_send < 0) { | |
6dc3064a DG |
1852 | /* Somehow, the session daemon is not responding anymore. */ |
1853 | goto end_nosignal; | |
1854 | } | |
9ce5646a | 1855 | health_code_update(); |
6dc3064a DG |
1856 | break; |
1857 | } | |
fb83fe64 JD |
1858 | case LTTNG_CONSUMER_DISCARDED_EVENTS: |
1859 | { | |
beb59458 MJ |
1860 | int ret = 0; |
1861 | uint64_t discarded_events; | |
fb83fe64 JD |
1862 | struct lttng_ht_iter iter; |
1863 | struct lttng_ht *ht; | |
1864 | struct lttng_consumer_stream *stream; | |
1865 | uint64_t id = msg.u.discarded_events.session_id; | |
1866 | uint64_t key = msg.u.discarded_events.channel_key; | |
1867 | ||
1868 | DBG("UST consumer discarded events command for session id %" | |
1869 | PRIu64, id); | |
1870 | rcu_read_lock(); | |
fa29bfbf | 1871 | pthread_mutex_lock(&the_consumer_data.lock); |
fb83fe64 | 1872 | |
fa29bfbf | 1873 | ht = the_consumer_data.stream_list_ht; |
fb83fe64 JD |
1874 | |
1875 | /* | |
1876 | * We only need a reference to the channel, but they are not | |
1877 | * directly indexed, so we just use the first matching stream | |
1878 | * to extract the information we need, we default to 0 if not | |
1879 | * found (no events are dropped if the channel is not yet in | |
1880 | * use). | |
1881 | */ | |
beb59458 | 1882 | discarded_events = 0; |
fb83fe64 JD |
1883 | cds_lfht_for_each_entry_duplicate(ht->ht, |
1884 | ht->hash_fct(&id, lttng_ht_seed), | |
1885 | ht->match_fct, &id, | |
1886 | &iter.iter, stream, node_session_id.node) { | |
1887 | if (stream->chan->key == key) { | |
beb59458 | 1888 | discarded_events = stream->chan->discarded_events; |
fb83fe64 JD |
1889 | break; |
1890 | } | |
1891 | } | |
fa29bfbf | 1892 | pthread_mutex_unlock(&the_consumer_data.lock); |
fb83fe64 JD |
1893 | rcu_read_unlock(); |
1894 | ||
1895 | DBG("UST consumer discarded events command for session id %" | |
1896 | PRIu64 ", channel key %" PRIu64, id, key); | |
1897 | ||
1898 | health_code_update(); | |
1899 | ||
1900 | /* Send back returned value to session daemon */ | |
beb59458 | 1901 | ret = lttcomm_send_unix_sock(sock, &discarded_events, sizeof(discarded_events)); |
fb83fe64 JD |
1902 | if (ret < 0) { |
1903 | PERROR("send discarded events"); | |
1904 | goto error_fatal; | |
1905 | } | |
1906 | ||
1907 | break; | |
1908 | } | |
1909 | case LTTNG_CONSUMER_LOST_PACKETS: | |
1910 | { | |
9a06e8d4 JG |
1911 | int ret; |
1912 | uint64_t lost_packets; | |
fb83fe64 JD |
1913 | struct lttng_ht_iter iter; |
1914 | struct lttng_ht *ht; | |
1915 | struct lttng_consumer_stream *stream; | |
1916 | uint64_t id = msg.u.lost_packets.session_id; | |
1917 | uint64_t key = msg.u.lost_packets.channel_key; | |
1918 | ||
1919 | DBG("UST consumer lost packets command for session id %" | |
1920 | PRIu64, id); | |
1921 | rcu_read_lock(); | |
fa29bfbf | 1922 | pthread_mutex_lock(&the_consumer_data.lock); |
fb83fe64 | 1923 | |
fa29bfbf | 1924 | ht = the_consumer_data.stream_list_ht; |
fb83fe64 JD |
1925 | |
1926 | /* | |
1927 | * We only need a reference to the channel, but they are not | |
1928 | * directly indexed, so we just use the first matching stream | |
1929 | * to extract the information we need, we default to 0 if not | |
1930 | * found (no packets lost if the channel is not yet in use). | |
1931 | */ | |
9a06e8d4 | 1932 | lost_packets = 0; |
fb83fe64 JD |
1933 | cds_lfht_for_each_entry_duplicate(ht->ht, |
1934 | ht->hash_fct(&id, lttng_ht_seed), | |
1935 | ht->match_fct, &id, | |
1936 | &iter.iter, stream, node_session_id.node) { | |
1937 | if (stream->chan->key == key) { | |
9a06e8d4 | 1938 | lost_packets = stream->chan->lost_packets; |
fb83fe64 JD |
1939 | break; |
1940 | } | |
1941 | } | |
fa29bfbf | 1942 | pthread_mutex_unlock(&the_consumer_data.lock); |
fb83fe64 JD |
1943 | rcu_read_unlock(); |
1944 | ||
1945 | DBG("UST consumer lost packets command for session id %" | |
1946 | PRIu64 ", channel key %" PRIu64, id, key); | |
1947 | ||
1948 | health_code_update(); | |
1949 | ||
1950 | /* Send back returned value to session daemon */ | |
9a06e8d4 JG |
1951 | ret = lttcomm_send_unix_sock(sock, &lost_packets, |
1952 | sizeof(lost_packets)); | |
fb83fe64 JD |
1953 | if (ret < 0) { |
1954 | PERROR("send lost packets"); | |
1955 | goto error_fatal; | |
1956 | } | |
1957 | ||
1958 | break; | |
1959 | } | |
b3530820 JG |
1960 | case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE: |
1961 | { | |
594c7c00 SM |
1962 | int channel_monitor_pipe, ret_send, |
1963 | ret_set_channel_monitor_pipe; | |
1964 | ssize_t ret_recv; | |
b3530820 JG |
1965 | |
1966 | ret_code = LTTCOMM_CONSUMERD_SUCCESS; | |
1967 | /* Successfully received the command's type. */ | |
594c7c00 SM |
1968 | ret_send = consumer_send_status_msg(sock, ret_code); |
1969 | if (ret_send < 0) { | |
b3530820 JG |
1970 | goto error_fatal; |
1971 | } | |
1972 | ||
594c7c00 SM |
1973 | ret_recv = lttcomm_recv_fds_unix_sock( |
1974 | sock, &channel_monitor_pipe, 1); | |
1975 | if (ret_recv != sizeof(channel_monitor_pipe)) { | |
b3530820 JG |
1976 | ERR("Failed to receive channel monitor pipe"); |
1977 | goto error_fatal; | |
1978 | } | |
1979 | ||
1980 | DBG("Received channel monitor pipe (%d)", channel_monitor_pipe); | |
594c7c00 SM |
1981 | ret_set_channel_monitor_pipe = |
1982 | consumer_timer_thread_set_channel_monitor_pipe( | |
1983 | channel_monitor_pipe); | |
1984 | if (!ret_set_channel_monitor_pipe) { | |
b3530820 | 1985 | int flags; |
594c7c00 | 1986 | int ret_fcntl; |
b3530820 JG |
1987 | |
1988 | ret_code = LTTCOMM_CONSUMERD_SUCCESS; | |
1989 | /* Set the pipe as non-blocking. */ | |
594c7c00 SM |
1990 | ret_fcntl = fcntl(channel_monitor_pipe, F_GETFL, 0); |
1991 | if (ret_fcntl == -1) { | |
b3530820 JG |
1992 | PERROR("fcntl get flags of the channel monitoring pipe"); |
1993 | goto error_fatal; | |
1994 | } | |
594c7c00 | 1995 | flags = ret_fcntl; |
b3530820 | 1996 | |
594c7c00 | 1997 | ret_fcntl = fcntl(channel_monitor_pipe, F_SETFL, |
b3530820 | 1998 | flags | O_NONBLOCK); |
594c7c00 | 1999 | if (ret_fcntl == -1) { |
b3530820 JG |
2000 | PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe"); |
2001 | goto error_fatal; | |
2002 | } | |
2003 | DBG("Channel monitor pipe set as non-blocking"); | |
2004 | } else { | |
2005 | ret_code = LTTCOMM_CONSUMERD_ALREADY_SET; | |
2006 | } | |
2007 | goto end_msg_sessiond; | |
2008 | } | |
b99a8d42 JD |
2009 | case LTTNG_CONSUMER_ROTATE_CHANNEL: |
2010 | { | |
594c7c00 | 2011 | struct lttng_consumer_channel *found_channel; |
92b7a7f8 | 2012 | uint64_t key = msg.u.rotate_channel.key; |
594c7c00 | 2013 | int ret_send_status; |
b99a8d42 | 2014 | |
594c7c00 SM |
2015 | found_channel = consumer_find_channel(key); |
2016 | if (!found_channel) { | |
92b7a7f8 MD |
2017 | DBG("Channel %" PRIu64 " not found", key); |
2018 | ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND; | |
2019 | } else { | |
594c7c00 SM |
2020 | int rotate_channel; |
2021 | ||
92b7a7f8 MD |
2022 | /* |
2023 | * Sample the rotate position of all the streams in | |
2024 | * this channel. | |
2025 | */ | |
594c7c00 SM |
2026 | rotate_channel = lttng_consumer_rotate_channel( |
2027 | found_channel, key, | |
f46376a1 | 2028 | msg.u.rotate_channel.relayd_id); |
594c7c00 | 2029 | if (rotate_channel < 0) { |
92b7a7f8 MD |
2030 | ERR("Rotate channel failed"); |
2031 | ret_code = LTTCOMM_CONSUMERD_ROTATION_FAIL; | |
2032 | } | |
b99a8d42 | 2033 | |
92b7a7f8 MD |
2034 | health_code_update(); |
2035 | } | |
594c7c00 SM |
2036 | |
2037 | ret_send_status = consumer_send_status_msg(sock, ret_code); | |
2038 | if (ret_send_status < 0) { | |
b99a8d42 | 2039 | /* Somehow, the session daemon is not responding anymore. */ |
41c7a76d | 2040 | goto end_rotate_channel_nosignal; |
b99a8d42 JD |
2041 | } |
2042 | ||
2043 | /* | |
2044 | * Rotate the streams that are ready right now. | |
2045 | * FIXME: this is a second consecutive iteration over the | |
2046 | * streams in a channel, there is probably a better way to | |
2047 | * handle this, but it needs to be after the | |
2048 | * consumer_send_status_msg() call. | |
2049 | */ | |
594c7c00 SM |
2050 | if (found_channel) { |
2051 | int ret_rotate_read_streams; | |
2052 | ||
2053 | ret_rotate_read_streams = | |
2054 | lttng_consumer_rotate_ready_streams( | |
f46376a1 | 2055 | found_channel, key); |
594c7c00 | 2056 | if (ret_rotate_read_streams < 0) { |
92b7a7f8 MD |
2057 | ERR("Rotate channel failed"); |
2058 | } | |
b99a8d42 JD |
2059 | } |
2060 | break; | |
41c7a76d JG |
2061 | end_rotate_channel_nosignal: |
2062 | goto end_nosignal; | |
b99a8d42 | 2063 | } |
5f3aff8b MD |
2064 | case LTTNG_CONSUMER_CLEAR_CHANNEL: |
2065 | { | |
594c7c00 | 2066 | struct lttng_consumer_channel *found_channel; |
5f3aff8b | 2067 | uint64_t key = msg.u.clear_channel.key; |
594c7c00 | 2068 | int ret_send_status; |
5f3aff8b | 2069 | |
594c7c00 SM |
2070 | found_channel = consumer_find_channel(key); |
2071 | if (!found_channel) { | |
5f3aff8b MD |
2072 | DBG("Channel %" PRIu64 " not found", key); |
2073 | ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND; | |
2074 | } else { | |
594c7c00 SM |
2075 | int ret_clear_channel; |
2076 | ||
2077 | ret_clear_channel = lttng_consumer_clear_channel( | |
2078 | found_channel); | |
2079 | if (ret_clear_channel) { | |
5f3aff8b | 2080 | ERR("Clear channel failed key %" PRIu64, key); |
97535efa | 2081 | ret_code = (lttcomm_return_code) ret_clear_channel; |
5f3aff8b MD |
2082 | } |
2083 | ||
2084 | health_code_update(); | |
2085 | } | |
594c7c00 SM |
2086 | ret_send_status = consumer_send_status_msg(sock, ret_code); |
2087 | if (ret_send_status < 0) { | |
5f3aff8b MD |
2088 | /* Somehow, the session daemon is not responding anymore. */ |
2089 | goto end_nosignal; | |
2090 | } | |
2091 | break; | |
2092 | } | |
d2956687 | 2093 | case LTTNG_CONSUMER_INIT: |
00fb02ac | 2094 | { |
594c7c00 | 2095 | int ret_send_status; |
328c2fe7 | 2096 | lttng_uuid sessiond_uuid; |
594c7c00 | 2097 | |
328c2fe7 JG |
2098 | std::copy(std::begin(msg.u.init.sessiond_uuid), std::end(msg.u.init.sessiond_uuid), |
2099 | sessiond_uuid.begin()); | |
2100 | ret_code = lttng_consumer_init_command(ctx, sessiond_uuid); | |
d88744a4 | 2101 | health_code_update(); |
594c7c00 SM |
2102 | ret_send_status = consumer_send_status_msg(sock, ret_code); |
2103 | if (ret_send_status < 0) { | |
d88744a4 JD |
2104 | /* Somehow, the session daemon is not responding anymore. */ |
2105 | goto end_nosignal; | |
2106 | } | |
2107 | break; | |
2108 | } | |
d2956687 | 2109 | case LTTNG_CONSUMER_CREATE_TRACE_CHUNK: |
d88744a4 | 2110 | { |
d2956687 | 2111 | const struct lttng_credentials credentials = { |
ff588497 JR |
2112 | .uid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.create_trace_chunk.credentials.value.uid), |
2113 | .gid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.create_trace_chunk.credentials.value.gid), | |
d2956687 JG |
2114 | }; |
2115 | const bool is_local_trace = | |
2116 | !msg.u.create_trace_chunk.relayd_id.is_set; | |
2117 | const uint64_t relayd_id = | |
2118 | msg.u.create_trace_chunk.relayd_id.value; | |
2119 | const char *chunk_override_name = | |
2120 | *msg.u.create_trace_chunk.override_name ? | |
2121 | msg.u.create_trace_chunk.override_name : | |
2122 | NULL; | |
cbf53d23 | 2123 | struct lttng_directory_handle *chunk_directory_handle = NULL; |
d88744a4 | 2124 | |
d2956687 JG |
2125 | /* |
2126 | * The session daemon will only provide a chunk directory file | |
2127 | * descriptor for local traces. | |
2128 | */ | |
2129 | if (is_local_trace) { | |
2130 | int chunk_dirfd; | |
594c7c00 SM |
2131 | int ret_send_status; |
2132 | ssize_t ret_recv; | |
19990ed5 | 2133 | |
d2956687 | 2134 | /* Acnowledge the reception of the command. */ |
594c7c00 SM |
2135 | ret_send_status = consumer_send_status_msg( |
2136 | sock, LTTCOMM_CONSUMERD_SUCCESS); | |
2137 | if (ret_send_status < 0) { | |
d2956687 JG |
2138 | /* Somehow, the session daemon is not responding anymore. */ |
2139 | goto end_nosignal; | |
2140 | } | |
92816cc3 | 2141 | |
5da88b0f MD |
2142 | /* |
2143 | * Receive trace chunk domain dirfd. | |
2144 | */ | |
594c7c00 SM |
2145 | ret_recv = lttcomm_recv_fds_unix_sock( |
2146 | sock, &chunk_dirfd, 1); | |
2147 | if (ret_recv != sizeof(chunk_dirfd)) { | |
5da88b0f | 2148 | ERR("Failed to receive trace chunk domain directory file descriptor"); |
d2956687 JG |
2149 | goto error_fatal; |
2150 | } | |
92816cc3 | 2151 | |
5da88b0f | 2152 | DBG("Received trace chunk domain directory fd (%d)", |
d2956687 | 2153 | chunk_dirfd); |
cbf53d23 | 2154 | chunk_directory_handle = lttng_directory_handle_create_from_dirfd( |
d2956687 | 2155 | chunk_dirfd); |
cbf53d23 | 2156 | if (!chunk_directory_handle) { |
5da88b0f | 2157 | ERR("Failed to initialize chunk domain directory handle from directory file descriptor"); |
d2956687 JG |
2158 | if (close(chunk_dirfd)) { |
2159 | PERROR("Failed to close chunk directory file descriptor"); | |
2160 | } | |
2161 | goto error_fatal; | |
2162 | } | |
92816cc3 JG |
2163 | } |
2164 | ||
d2956687 JG |
2165 | ret_code = lttng_consumer_create_trace_chunk( |
2166 | !is_local_trace ? &relayd_id : NULL, | |
2167 | msg.u.create_trace_chunk.session_id, | |
2168 | msg.u.create_trace_chunk.chunk_id, | |
e5add6d0 JG |
2169 | (time_t) msg.u.create_trace_chunk |
2170 | .creation_timestamp, | |
d2956687 | 2171 | chunk_override_name, |
e5add6d0 JG |
2172 | msg.u.create_trace_chunk.credentials.is_set ? |
2173 | &credentials : | |
2174 | NULL, | |
cbf53d23 JG |
2175 | chunk_directory_handle); |
2176 | lttng_directory_handle_put(chunk_directory_handle); | |
d2956687 | 2177 | goto end_msg_sessiond; |
00fb02ac | 2178 | } |
d2956687 | 2179 | case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK: |
a1ae2ea5 | 2180 | { |
bbc4768c | 2181 | enum lttng_trace_chunk_command_type close_command = |
97535efa | 2182 | (lttng_trace_chunk_command_type) |
bbc4768c | 2183 | msg.u.close_trace_chunk.close_command.value; |
d2956687 JG |
2184 | const uint64_t relayd_id = |
2185 | msg.u.close_trace_chunk.relayd_id.value; | |
ecd1a12f | 2186 | struct lttcomm_consumer_close_trace_chunk_reply reply; |
d00fb490 | 2187 | char closed_trace_chunk_path[LTTNG_PATH_MAX] = {}; |
ecd1a12f | 2188 | int ret; |
d2956687 JG |
2189 | |
2190 | ret_code = lttng_consumer_close_trace_chunk( | |
2191 | msg.u.close_trace_chunk.relayd_id.is_set ? | |
bbc4768c JG |
2192 | &relayd_id : |
2193 | NULL, | |
d2956687 JG |
2194 | msg.u.close_trace_chunk.session_id, |
2195 | msg.u.close_trace_chunk.chunk_id, | |
bbc4768c JG |
2196 | (time_t) msg.u.close_trace_chunk.close_timestamp, |
2197 | msg.u.close_trace_chunk.close_command.is_set ? | |
2198 | &close_command : | |
ecd1a12f MD |
2199 | NULL, closed_trace_chunk_path); |
2200 | reply.ret_code = ret_code; | |
2201 | reply.path_length = strlen(closed_trace_chunk_path) + 1; | |
2202 | ret = lttcomm_send_unix_sock(sock, &reply, sizeof(reply)); | |
2203 | if (ret != sizeof(reply)) { | |
2204 | goto error_fatal; | |
2205 | } | |
2206 | ret = lttcomm_send_unix_sock(sock, closed_trace_chunk_path, | |
2207 | reply.path_length); | |
2208 | if (ret != reply.path_length) { | |
2209 | goto error_fatal; | |
2210 | } | |
2211 | goto end_nosignal; | |
a1ae2ea5 | 2212 | } |
d2956687 | 2213 | case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS: |
3654ed19 | 2214 | { |
d2956687 JG |
2215 | const uint64_t relayd_id = |
2216 | msg.u.trace_chunk_exists.relayd_id.value; | |
2217 | ||
2218 | ret_code = lttng_consumer_trace_chunk_exists( | |
2219 | msg.u.trace_chunk_exists.relayd_id.is_set ? | |
2220 | &relayd_id : NULL, | |
2221 | msg.u.trace_chunk_exists.session_id, | |
2222 | msg.u.trace_chunk_exists.chunk_id); | |
2223 | goto end_msg_sessiond; | |
04ed9e10 JG |
2224 | } |
2225 | case LTTNG_CONSUMER_OPEN_CHANNEL_PACKETS: | |
2226 | { | |
2227 | const uint64_t key = msg.u.open_channel_packets.key; | |
594c7c00 | 2228 | struct lttng_consumer_channel *found_channel = |
04ed9e10 JG |
2229 | consumer_find_channel(key); |
2230 | ||
594c7c00 SM |
2231 | if (found_channel) { |
2232 | pthread_mutex_lock(&found_channel->lock); | |
2233 | ret_code = lttng_consumer_open_channel_packets( | |
2234 | found_channel); | |
2235 | pthread_mutex_unlock(&found_channel->lock); | |
04ed9e10 JG |
2236 | } else { |
2237 | /* | |
2238 | * The channel could have disappeared in per-pid | |
2239 | * buffering mode. | |
2240 | */ | |
2241 | DBG("Channel %" PRIu64 " not found", key); | |
2242 | ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND; | |
2243 | } | |
2244 | ||
2245 | health_code_update(); | |
2246 | goto end_msg_sessiond; | |
3654ed19 | 2247 | } |
3bd1e081 MD |
2248 | default: |
2249 | break; | |
2250 | } | |
3f8e211f | 2251 | |
3bd1e081 | 2252 | end_nosignal: |
4cbc1a04 DG |
2253 | /* |
2254 | * Return 1 to indicate success since the 0 value can be a socket | |
2255 | * shutdown during the recv() or send() call. | |
2256 | */ | |
594c7c00 | 2257 | ret_func = 1; |
f3a1fc7b | 2258 | goto end; |
ffe60014 DG |
2259 | |
2260 | end_msg_sessiond: | |
2261 | /* | |
2262 | * The returned value here is not useful since either way we'll return 1 to | |
2263 | * the caller because the session daemon socket management is done | |
2264 | * elsewhere. Returning a negative code or 0 will shutdown the consumer. | |
2265 | */ | |
594c7c00 SM |
2266 | { |
2267 | int ret_send_status; | |
2268 | ||
2269 | ret_send_status = consumer_send_status_msg(sock, ret_code); | |
2270 | if (ret_send_status < 0) { | |
2271 | goto error_fatal; | |
2272 | } | |
489f70e9 | 2273 | } |
594c7c00 SM |
2274 | |
2275 | ret_func = 1; | |
f3a1fc7b | 2276 | goto end; |
9ce5646a | 2277 | |
ffe60014 DG |
2278 | end_channel_error: |
2279 | if (channel) { | |
a00932c8 | 2280 | consumer_del_channel(channel); |
ffe60014 DG |
2281 | } |
2282 | /* We have to send a status channel message indicating an error. */ | |
594c7c00 SM |
2283 | { |
2284 | int ret_send_status; | |
2285 | ||
2286 | ret_send_status = consumer_send_status_channel(sock, NULL); | |
2287 | if (ret_send_status < 0) { | |
2288 | /* Stop everything if session daemon can not be notified. */ | |
2289 | goto error_fatal; | |
2290 | } | |
ffe60014 | 2291 | } |
594c7c00 SM |
2292 | |
2293 | ret_func = 1; | |
f3a1fc7b | 2294 | goto end; |
9ce5646a | 2295 | |
ffe60014 | 2296 | error_fatal: |
ffe60014 | 2297 | /* This will issue a consumer stop. */ |
594c7c00 | 2298 | ret_func = -1; |
f3a1fc7b JG |
2299 | goto end; |
2300 | ||
2301 | end: | |
2302 | rcu_read_unlock(); | |
2303 | health_code_update(); | |
594c7c00 | 2304 | return ret_func; |
3bd1e081 MD |
2305 | } |
2306 | ||
881fc67f MD |
2307 | int lttng_ust_flush_buffer(struct lttng_consumer_stream *stream, |
2308 | int producer_active) | |
fc6d7a51 | 2309 | { |
a0377dfe FD |
2310 | LTTNG_ASSERT(stream); |
2311 | LTTNG_ASSERT(stream->ustream); | |
fc6d7a51 | 2312 | |
881fc67f | 2313 | return lttng_ust_ctl_flush_buffer(stream->ustream, producer_active); |
fc6d7a51 JD |
2314 | } |
2315 | ||
ffe60014 | 2316 | /* |
e9404c27 | 2317 | * Take a snapshot for a specific stream. |
ffe60014 DG |
2318 | * |
2319 | * Returns 0 on success, < 0 on error | |
2320 | */ | |
2321 | int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream *stream) | |
3bd1e081 | 2322 | { |
a0377dfe FD |
2323 | LTTNG_ASSERT(stream); |
2324 | LTTNG_ASSERT(stream->ustream); | |
ffe60014 | 2325 | |
b623cb6a | 2326 | return lttng_ust_ctl_snapshot(stream->ustream); |
3bd1e081 MD |
2327 | } |
2328 | ||
e9404c27 JG |
2329 | /* |
2330 | * Sample consumed and produced positions for a specific stream. | |
2331 | * | |
2332 | * Returns 0 on success, < 0 on error. | |
2333 | */ | |
2334 | int lttng_ustconsumer_sample_snapshot_positions( | |
2335 | struct lttng_consumer_stream *stream) | |
2336 | { | |
a0377dfe FD |
2337 | LTTNG_ASSERT(stream); |
2338 | LTTNG_ASSERT(stream->ustream); | |
e9404c27 | 2339 | |
b623cb6a | 2340 | return lttng_ust_ctl_snapshot_sample_positions(stream->ustream); |
e9404c27 JG |
2341 | } |
2342 | ||
ffe60014 DG |
2343 | /* |
2344 | * Get the produced position | |
2345 | * | |
2346 | * Returns 0 on success, < 0 on error | |
2347 | */ | |
2348 | int lttng_ustconsumer_get_produced_snapshot( | |
2349 | struct lttng_consumer_stream *stream, unsigned long *pos) | |
3bd1e081 | 2350 | { |
a0377dfe FD |
2351 | LTTNG_ASSERT(stream); |
2352 | LTTNG_ASSERT(stream->ustream); | |
2353 | LTTNG_ASSERT(pos); | |
7a57cf92 | 2354 | |
b623cb6a | 2355 | return lttng_ust_ctl_snapshot_get_produced(stream->ustream, pos); |
ffe60014 | 2356 | } |
7a57cf92 | 2357 | |
10a50311 JD |
2358 | /* |
2359 | * Get the consumed position | |
2360 | * | |
2361 | * Returns 0 on success, < 0 on error | |
2362 | */ | |
2363 | int lttng_ustconsumer_get_consumed_snapshot( | |
2364 | struct lttng_consumer_stream *stream, unsigned long *pos) | |
2365 | { | |
a0377dfe FD |
2366 | LTTNG_ASSERT(stream); |
2367 | LTTNG_ASSERT(stream->ustream); | |
2368 | LTTNG_ASSERT(pos); | |
10a50311 | 2369 | |
b623cb6a | 2370 | return lttng_ust_ctl_snapshot_get_consumed(stream->ustream, pos); |
10a50311 JD |
2371 | } |
2372 | ||
881fc67f | 2373 | int lttng_ustconsumer_flush_buffer(struct lttng_consumer_stream *stream, |
84a182ce DG |
2374 | int producer) |
2375 | { | |
a0377dfe FD |
2376 | LTTNG_ASSERT(stream); |
2377 | LTTNG_ASSERT(stream->ustream); | |
84a182ce | 2378 | |
881fc67f | 2379 | return lttng_ust_ctl_flush_buffer(stream->ustream, producer); |
84a182ce DG |
2380 | } |
2381 | ||
881fc67f | 2382 | int lttng_ustconsumer_clear_buffer(struct lttng_consumer_stream *stream) |
214f70e0 | 2383 | { |
a0377dfe FD |
2384 | LTTNG_ASSERT(stream); |
2385 | LTTNG_ASSERT(stream->ustream); | |
214f70e0 | 2386 | |
881fc67f | 2387 | return lttng_ust_ctl_clear_buffer(stream->ustream); |
214f70e0 JR |
2388 | } |
2389 | ||
84a182ce DG |
2390 | int lttng_ustconsumer_get_current_timestamp( |
2391 | struct lttng_consumer_stream *stream, uint64_t *ts) | |
2392 | { | |
a0377dfe FD |
2393 | LTTNG_ASSERT(stream); |
2394 | LTTNG_ASSERT(stream->ustream); | |
2395 | LTTNG_ASSERT(ts); | |
84a182ce | 2396 | |
b623cb6a | 2397 | return lttng_ust_ctl_get_current_timestamp(stream->ustream, ts); |
84a182ce DG |
2398 | } |
2399 | ||
fb83fe64 JD |
2400 | int lttng_ustconsumer_get_sequence_number( |
2401 | struct lttng_consumer_stream *stream, uint64_t *seq) | |
2402 | { | |
a0377dfe FD |
2403 | LTTNG_ASSERT(stream); |
2404 | LTTNG_ASSERT(stream->ustream); | |
2405 | LTTNG_ASSERT(seq); | |
fb83fe64 | 2406 | |
b623cb6a | 2407 | return lttng_ust_ctl_get_sequence_number(stream->ustream, seq); |
fb83fe64 JD |
2408 | } |
2409 | ||
ffe60014 | 2410 | /* |
0dd01979 | 2411 | * Called when the stream signals the consumer that it has hung up. |
ffe60014 DG |
2412 | */ |
2413 | void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream *stream) | |
2414 | { | |
a0377dfe FD |
2415 | LTTNG_ASSERT(stream); |
2416 | LTTNG_ASSERT(stream->ustream); | |
2c1dd183 | 2417 | |
0dd01979 MD |
2418 | pthread_mutex_lock(&stream->lock); |
2419 | if (!stream->quiescent) { | |
881fc67f MD |
2420 | if (lttng_ust_ctl_flush_buffer(stream->ustream, 0) < 0) { |
2421 | ERR("Failed to flush buffer on stream hang-up"); | |
2422 | } else { | |
2423 | stream->quiescent = true; | |
2424 | } | |
0dd01979 MD |
2425 | } |
2426 | pthread_mutex_unlock(&stream->lock); | |
ffe60014 DG |
2427 | stream->hangup_flush_done = 1; |
2428 | } | |
ee77a7b0 | 2429 | |
ffe60014 DG |
2430 | void lttng_ustconsumer_del_channel(struct lttng_consumer_channel *chan) |
2431 | { | |
4628484a MD |
2432 | int i; |
2433 | ||
a0377dfe FD |
2434 | LTTNG_ASSERT(chan); |
2435 | LTTNG_ASSERT(chan->uchan); | |
2436 | LTTNG_ASSERT(chan->buffer_credentials.is_set); | |
e316aad5 | 2437 | |
ea88ca2a MD |
2438 | if (chan->switch_timer_enabled == 1) { |
2439 | consumer_timer_switch_stop(chan); | |
2440 | } | |
4628484a MD |
2441 | for (i = 0; i < chan->nr_stream_fds; i++) { |
2442 | int ret; | |
2443 | ||
2444 | ret = close(chan->stream_fds[i]); | |
2445 | if (ret) { | |
2446 | PERROR("close"); | |
2447 | } | |
2448 | if (chan->shm_path[0]) { | |
2449 | char shm_path[PATH_MAX]; | |
2450 | ||
2451 | ret = get_stream_shm_path(shm_path, chan->shm_path, i); | |
2452 | if (ret) { | |
2453 | ERR("Cannot get stream shm path"); | |
2454 | } | |
d2956687 | 2455 | ret = run_as_unlink(shm_path, |
ff588497 JR |
2456 | lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR( |
2457 | chan->buffer_credentials)), | |
2458 | lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR( | |
2459 | chan->buffer_credentials))); | |
4628484a | 2460 | if (ret) { |
4628484a MD |
2461 | PERROR("unlink %s", shm_path); |
2462 | } | |
2463 | } | |
2464 | } | |
3bd1e081 MD |
2465 | } |
2466 | ||
b83e03c4 MD |
2467 | void lttng_ustconsumer_free_channel(struct lttng_consumer_channel *chan) |
2468 | { | |
a0377dfe FD |
2469 | LTTNG_ASSERT(chan); |
2470 | LTTNG_ASSERT(chan->uchan); | |
2471 | LTTNG_ASSERT(chan->buffer_credentials.is_set); | |
b83e03c4 MD |
2472 | |
2473 | consumer_metadata_cache_destroy(chan); | |
b623cb6a | 2474 | lttng_ust_ctl_destroy_channel(chan->uchan); |
ea853771 JR |
2475 | /* Try to rmdir all directories under shm_path root. */ |
2476 | if (chan->root_shm_path[0]) { | |
602766ec | 2477 | (void) run_as_rmdir_recursive(chan->root_shm_path, |
ff588497 JR |
2478 | lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR( |
2479 | chan->buffer_credentials)), | |
2480 | lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR( | |
2481 | chan->buffer_credentials)), | |
f75c5439 | 2482 | LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG); |
ea853771 | 2483 | } |
b83e03c4 MD |
2484 | free(chan->stream_fds); |
2485 | } | |
2486 | ||
3bd1e081 MD |
2487 | void lttng_ustconsumer_del_stream(struct lttng_consumer_stream *stream) |
2488 | { | |
a0377dfe FD |
2489 | LTTNG_ASSERT(stream); |
2490 | LTTNG_ASSERT(stream->ustream); | |
d41f73b7 | 2491 | |
ea88ca2a MD |
2492 | if (stream->chan->switch_timer_enabled == 1) { |
2493 | consumer_timer_switch_stop(stream->chan); | |
2494 | } | |
b623cb6a | 2495 | lttng_ust_ctl_destroy_stream(stream->ustream); |
ffe60014 | 2496 | } |
d41f73b7 | 2497 | |
6d574024 DG |
2498 | int lttng_ustconsumer_get_wakeup_fd(struct lttng_consumer_stream *stream) |
2499 | { | |
a0377dfe FD |
2500 | LTTNG_ASSERT(stream); |
2501 | LTTNG_ASSERT(stream->ustream); | |
6d574024 | 2502 | |
b623cb6a | 2503 | return lttng_ust_ctl_stream_get_wakeup_fd(stream->ustream); |
6d574024 DG |
2504 | } |
2505 | ||
2506 | int lttng_ustconsumer_close_wakeup_fd(struct lttng_consumer_stream *stream) | |
2507 | { | |
a0377dfe FD |
2508 | LTTNG_ASSERT(stream); |
2509 | LTTNG_ASSERT(stream->ustream); | |
6d574024 | 2510 | |
b623cb6a | 2511 | return lttng_ust_ctl_stream_close_wakeup_fd(stream->ustream); |
6d574024 DG |
2512 | } |
2513 | ||
94d49140 JD |
2514 | /* |
2515 | * Write up to one packet from the metadata cache to the channel. | |
2516 | * | |
577eea73 JG |
2517 | * Returns the number of bytes pushed from the cache into the ring buffer, or a |
2518 | * negative value on error. | |
94d49140 JD |
2519 | */ |
2520 | static | |
2521 | int commit_one_metadata_packet(struct lttng_consumer_stream *stream) | |
2522 | { | |
2523 | ssize_t write_len; | |
2524 | int ret; | |
2525 | ||
2526 | pthread_mutex_lock(&stream->chan->metadata_cache->lock); | |
9eac9828 JG |
2527 | if (stream->chan->metadata_cache->contents.size == |
2528 | stream->ust_metadata_pushed) { | |
55954e07 JG |
2529 | /* |
2530 | * In the context of a user space metadata channel, a | |
2531 | * change in version can be detected in two ways: | |
2532 | * 1) During the pre-consume of the `read_subbuffer` loop, | |
2533 | * 2) When populating the metadata ring buffer (i.e. here). | |
2534 | * | |
2535 | * This function is invoked when there is no metadata | |
2536 | * available in the ring-buffer. If all data was consumed | |
2537 | * up to the size of the metadata cache, there is no metadata | |
2538 | * to insert in the ring-buffer. | |
2539 | * | |
2540 | * However, the metadata version could still have changed (a | |
2541 | * regeneration without any new data will yield the same cache | |
2542 | * size). | |
2543 | * | |
2544 | * The cache's version is checked for a version change and the | |
2545 | * consumed position is reset if one occurred. | |
2546 | * | |
2547 | * This check is only necessary for the user space domain as | |
2548 | * it has to manage the cache explicitly. If this reset was not | |
2549 | * performed, no metadata would be consumed (and no reset would | |
2550 | * occur as part of the pre-consume) until the metadata size | |
2551 | * exceeded the cache size. | |
2552 | */ | |
2553 | if (stream->metadata_version != | |
2554 | stream->chan->metadata_cache->version) { | |
2555 | metadata_stream_reset_cache_consumed_position(stream); | |
2556 | consumer_stream_metadata_set_version(stream, | |
2557 | stream->chan->metadata_cache->version); | |
2558 | } else { | |
2559 | ret = 0; | |
2560 | goto end; | |
2561 | } | |
94d49140 JD |
2562 | } |
2563 | ||
b623cb6a | 2564 | write_len = lttng_ust_ctl_write_one_packet_to_channel(stream->chan->uchan, |
9eac9828 JG |
2565 | &stream->chan->metadata_cache->contents.data[stream->ust_metadata_pushed], |
2566 | stream->chan->metadata_cache->contents.size - | |
2567 | stream->ust_metadata_pushed); | |
a0377dfe | 2568 | LTTNG_ASSERT(write_len != 0); |
94d49140 JD |
2569 | if (write_len < 0) { |
2570 | ERR("Writing one metadata packet"); | |
f5ba75b4 | 2571 | ret = write_len; |
94d49140 JD |
2572 | goto end; |
2573 | } | |
2574 | stream->ust_metadata_pushed += write_len; | |
2575 | ||
a0377dfe | 2576 | LTTNG_ASSERT(stream->chan->metadata_cache->contents.size >= |
94d49140 JD |
2577 | stream->ust_metadata_pushed); |
2578 | ret = write_len; | |
2579 | ||
0d88e046 JG |
2580 | /* |
2581 | * Switch packet (but don't open the next one) on every commit of | |
2582 | * a metadata packet. Since the subbuffer is fully filled (with padding, | |
2583 | * if needed), the stream is "quiescent" after this commit. | |
2584 | */ | |
881fc67f | 2585 | if (lttng_ust_ctl_flush_buffer(stream->ustream, 1)) { |
edb555b5 | 2586 | ERR("Failed to flush buffer while committing one metadata packet"); |
881fc67f MD |
2587 | ret = -EIO; |
2588 | } else { | |
2589 | stream->quiescent = true; | |
2590 | } | |
94d49140 JD |
2591 | end: |
2592 | pthread_mutex_unlock(&stream->chan->metadata_cache->lock); | |
2593 | return ret; | |
2594 | } | |
2595 | ||
309167d2 | 2596 | |
94d49140 JD |
2597 | /* |
2598 | * Sync metadata meaning request them to the session daemon and snapshot to the | |
2599 | * metadata thread can consumer them. | |
2600 | * | |
c585821b MD |
2601 | * Metadata stream lock is held here, but we need to release it when |
2602 | * interacting with sessiond, else we cause a deadlock with live | |
2603 | * awaiting on metadata to be pushed out. | |
94d49140 | 2604 | * |
cdb72e4e | 2605 | * The RCU read side lock must be held by the caller. |
94d49140 | 2606 | */ |
577eea73 JG |
2607 | enum sync_metadata_status lttng_ustconsumer_sync_metadata( |
2608 | struct lttng_consumer_local_data *ctx, | |
cdb72e4e | 2609 | struct lttng_consumer_stream *metadata_stream) |
94d49140 JD |
2610 | { |
2611 | int ret; | |
577eea73 | 2612 | enum sync_metadata_status status; |
cdb72e4e | 2613 | struct lttng_consumer_channel *metadata_channel; |
94d49140 | 2614 | |
a0377dfe FD |
2615 | LTTNG_ASSERT(ctx); |
2616 | LTTNG_ASSERT(metadata_stream); | |
48b7cdc2 | 2617 | ASSERT_RCU_READ_LOCKED(); |
94d49140 | 2618 | |
cdb72e4e JG |
2619 | metadata_channel = metadata_stream->chan; |
2620 | pthread_mutex_unlock(&metadata_stream->lock); | |
94d49140 JD |
2621 | /* |
2622 | * Request metadata from the sessiond, but don't wait for the flush | |
2623 | * because we locked the metadata thread. | |
2624 | */ | |
cdb72e4e JG |
2625 | ret = lttng_ustconsumer_request_metadata(ctx, metadata_channel, 0, 0); |
2626 | pthread_mutex_lock(&metadata_stream->lock); | |
94d49140 | 2627 | if (ret < 0) { |
577eea73 | 2628 | status = SYNC_METADATA_STATUS_ERROR; |
94d49140 JD |
2629 | goto end; |
2630 | } | |
2631 | ||
cdb72e4e JG |
2632 | /* |
2633 | * The metadata stream and channel can be deleted while the | |
2634 | * metadata stream lock was released. The streamed is checked | |
2635 | * for deletion before we use it further. | |
2636 | * | |
2637 | * Note that it is safe to access a logically-deleted stream since its | |
2638 | * existence is still guaranteed by the RCU read side lock. However, | |
2639 | * it should no longer be used. The close/deletion of the metadata | |
2640 | * channel and stream already guarantees that all metadata has been | |
2641 | * consumed. Therefore, there is nothing left to do in this function. | |
2642 | */ | |
2643 | if (consumer_stream_is_deleted(metadata_stream)) { | |
2644 | DBG("Metadata stream %" PRIu64 " was deleted during the metadata synchronization", | |
2645 | metadata_stream->key); | |
577eea73 | 2646 | status = SYNC_METADATA_STATUS_NO_DATA; |
cdb72e4e JG |
2647 | goto end; |
2648 | } | |
2649 | ||
2650 | ret = commit_one_metadata_packet(metadata_stream); | |
577eea73 JG |
2651 | if (ret < 0) { |
2652 | status = SYNC_METADATA_STATUS_ERROR; | |
94d49140 JD |
2653 | goto end; |
2654 | } else if (ret > 0) { | |
577eea73 JG |
2655 | status = SYNC_METADATA_STATUS_NEW_DATA; |
2656 | } else /* ret == 0 */ { | |
2657 | status = SYNC_METADATA_STATUS_NO_DATA; | |
2658 | goto end; | |
94d49140 JD |
2659 | } |
2660 | ||
b623cb6a | 2661 | ret = lttng_ust_ctl_snapshot(metadata_stream->ustream); |
94d49140 | 2662 | if (ret < 0) { |
577eea73 JG |
2663 | ERR("Failed to take a snapshot of the metadata ring-buffer positions, ret = %d", ret); |
2664 | status = SYNC_METADATA_STATUS_ERROR; | |
94d49140 JD |
2665 | goto end; |
2666 | } | |
2667 | ||
94d49140 | 2668 | end: |
577eea73 | 2669 | return status; |
94d49140 JD |
2670 | } |
2671 | ||
02b3d176 DG |
2672 | /* |
2673 | * Return 0 on success else a negative value. | |
2674 | */ | |
2675 | static int notify_if_more_data(struct lttng_consumer_stream *stream, | |
2676 | struct lttng_consumer_local_data *ctx) | |
2677 | { | |
2678 | int ret; | |
b623cb6a | 2679 | struct lttng_ust_ctl_consumer_stream *ustream; |
02b3d176 | 2680 | |
a0377dfe FD |
2681 | LTTNG_ASSERT(stream); |
2682 | LTTNG_ASSERT(ctx); | |
02b3d176 DG |
2683 | |
2684 | ustream = stream->ustream; | |
2685 | ||
2686 | /* | |
2687 | * First, we are going to check if there is a new subbuffer available | |
2688 | * before reading the stream wait_fd. | |
2689 | */ | |
2690 | /* Get the next subbuffer */ | |
b623cb6a | 2691 | ret = lttng_ust_ctl_get_next_subbuf(ustream); |
02b3d176 DG |
2692 | if (ret) { |
2693 | /* No more data found, flag the stream. */ | |
2694 | stream->has_data = 0; | |
2695 | ret = 0; | |
2696 | goto end; | |
2697 | } | |
2698 | ||
b623cb6a | 2699 | ret = lttng_ust_ctl_put_subbuf(ustream); |
a0377dfe | 2700 | LTTNG_ASSERT(!ret); |
02b3d176 DG |
2701 | |
2702 | /* This stream still has data. Flag it and wake up the data thread. */ | |
2703 | stream->has_data = 1; | |
2704 | ||
2705 | if (stream->monitor && !stream->hangup_flush_done && !ctx->has_wakeup) { | |
2706 | ssize_t writelen; | |
2707 | ||
2708 | writelen = lttng_pipe_write(ctx->consumer_wakeup_pipe, "!", 1); | |
2709 | if (writelen < 0 && errno != EAGAIN && errno != EWOULDBLOCK) { | |
2710 | ret = writelen; | |
2711 | goto end; | |
2712 | } | |
2713 | ||
2714 | /* The wake up pipe has been notified. */ | |
2715 | ctx->has_wakeup = 1; | |
2716 | } | |
2717 | ret = 0; | |
2718 | ||
2719 | end: | |
2720 | return ret; | |
2721 | } | |
2722 | ||
6f9449c2 | 2723 | static int consumer_stream_ust_on_wake_up(struct lttng_consumer_stream *stream) |
fb83fe64 | 2724 | { |
6f9449c2 | 2725 | int ret = 0; |
fb83fe64 | 2726 | |
fb83fe64 | 2727 | /* |
6f9449c2 JG |
2728 | * We can consume the 1 byte written into the wait_fd by |
2729 | * UST. Don't trigger error if we cannot read this one byte | |
2730 | * (read returns 0), or if the error is EAGAIN or EWOULDBLOCK. | |
2731 | * | |
2732 | * This is only done when the stream is monitored by a thread, | |
2733 | * before the flush is done after a hangup and if the stream | |
2734 | * is not flagged with data since there might be nothing to | |
2735 | * consume in the wait fd but still have data available | |
2736 | * flagged by the consumer wake up pipe. | |
fb83fe64 | 2737 | */ |
6f9449c2 JG |
2738 | if (stream->monitor && !stream->hangup_flush_done && !stream->has_data) { |
2739 | char dummy; | |
2740 | ssize_t readlen; | |
2741 | ||
2742 | readlen = lttng_read(stream->wait_fd, &dummy, 1); | |
2743 | if (readlen < 0 && errno != EAGAIN && errno != EWOULDBLOCK) { | |
2744 | ret = readlen; | |
2745 | } | |
fb83fe64 | 2746 | } |
fb83fe64 | 2747 | |
6f9449c2 JG |
2748 | return ret; |
2749 | } | |
2750 | ||
2751 | static int extract_common_subbuffer_info(struct lttng_consumer_stream *stream, | |
2752 | struct stream_subbuffer *subbuf) | |
2753 | { | |
2754 | int ret; | |
2755 | ||
b623cb6a | 2756 | ret = lttng_ust_ctl_get_subbuf_size( |
6f9449c2 JG |
2757 | stream->ustream, &subbuf->info.data.subbuf_size); |
2758 | if (ret) { | |
fb83fe64 JD |
2759 | goto end; |
2760 | } | |
6f9449c2 | 2761 | |
b623cb6a | 2762 | ret = lttng_ust_ctl_get_padded_subbuf_size( |
6f9449c2 JG |
2763 | stream->ustream, &subbuf->info.data.padded_subbuf_size); |
2764 | if (ret) { | |
2765 | goto end; | |
fb83fe64 | 2766 | } |
fb83fe64 JD |
2767 | |
2768 | end: | |
2769 | return ret; | |
2770 | } | |
2771 | ||
6f9449c2 JG |
2772 | static int extract_metadata_subbuffer_info(struct lttng_consumer_stream *stream, |
2773 | struct stream_subbuffer *subbuf) | |
d41f73b7 | 2774 | { |
6f9449c2 | 2775 | int ret; |
ffe60014 | 2776 | |
6f9449c2 JG |
2777 | ret = extract_common_subbuffer_info(stream, subbuf); |
2778 | if (ret) { | |
2779 | goto end; | |
2780 | } | |
d41f73b7 | 2781 | |
55954e07 | 2782 | subbuf->info.metadata.version = stream->metadata_version; |
ffe60014 | 2783 | |
6f9449c2 JG |
2784 | end: |
2785 | return ret; | |
2786 | } | |
d41f73b7 | 2787 | |
6f9449c2 JG |
2788 | static int extract_data_subbuffer_info(struct lttng_consumer_stream *stream, |
2789 | struct stream_subbuffer *subbuf) | |
2790 | { | |
2791 | int ret; | |
c617c0c6 | 2792 | |
6f9449c2 JG |
2793 | ret = extract_common_subbuffer_info(stream, subbuf); |
2794 | if (ret) { | |
2795 | goto end; | |
02d02e31 JD |
2796 | } |
2797 | ||
b623cb6a | 2798 | ret = lttng_ust_ctl_get_packet_size( |
6f9449c2 JG |
2799 | stream->ustream, &subbuf->info.data.packet_size); |
2800 | if (ret < 0) { | |
2801 | PERROR("Failed to get sub-buffer packet size"); | |
2802 | goto end; | |
2803 | } | |
04ef1097 | 2804 | |
b623cb6a | 2805 | ret = lttng_ust_ctl_get_content_size( |
6f9449c2 JG |
2806 | stream->ustream, &subbuf->info.data.content_size); |
2807 | if (ret < 0) { | |
2808 | PERROR("Failed to get sub-buffer content size"); | |
2809 | goto end; | |
d41f73b7 | 2810 | } |
309167d2 | 2811 | |
b623cb6a | 2812 | ret = lttng_ust_ctl_get_timestamp_begin( |
6f9449c2 JG |
2813 | stream->ustream, &subbuf->info.data.timestamp_begin); |
2814 | if (ret < 0) { | |
2815 | PERROR("Failed to get sub-buffer begin timestamp"); | |
2816 | goto end; | |
2817 | } | |
fb83fe64 | 2818 | |
b623cb6a | 2819 | ret = lttng_ust_ctl_get_timestamp_end( |
6f9449c2 JG |
2820 | stream->ustream, &subbuf->info.data.timestamp_end); |
2821 | if (ret < 0) { | |
2822 | PERROR("Failed to get sub-buffer end timestamp"); | |
2823 | goto end; | |
2824 | } | |
2825 | ||
b623cb6a | 2826 | ret = lttng_ust_ctl_get_events_discarded( |
6f9449c2 JG |
2827 | stream->ustream, &subbuf->info.data.events_discarded); |
2828 | if (ret) { | |
2829 | PERROR("Failed to get sub-buffer events discarded count"); | |
2830 | goto end; | |
2831 | } | |
2832 | ||
b623cb6a | 2833 | ret = lttng_ust_ctl_get_sequence_number(stream->ustream, |
6f9449c2 JG |
2834 | &subbuf->info.data.sequence_number.value); |
2835 | if (ret) { | |
2836 | /* May not be supported by older LTTng-modules. */ | |
2837 | if (ret != -ENOTTY) { | |
2838 | PERROR("Failed to get sub-buffer sequence number"); | |
2839 | goto end; | |
fb83fe64 | 2840 | } |
1c20f0e2 | 2841 | } else { |
6f9449c2 | 2842 | subbuf->info.data.sequence_number.is_set = true; |
309167d2 JD |
2843 | } |
2844 | ||
b623cb6a | 2845 | ret = lttng_ust_ctl_get_stream_id( |
6f9449c2 JG |
2846 | stream->ustream, &subbuf->info.data.stream_id); |
2847 | if (ret < 0) { | |
2848 | PERROR("Failed to get stream id"); | |
2849 | goto end; | |
2850 | } | |
1d4dfdef | 2851 | |
b623cb6a | 2852 | ret = lttng_ust_ctl_get_instance_id(stream->ustream, |
6f9449c2 JG |
2853 | &subbuf->info.data.stream_instance_id.value); |
2854 | if (ret) { | |
2855 | /* May not be supported by older LTTng-modules. */ | |
2856 | if (ret != -ENOTTY) { | |
2857 | PERROR("Failed to get stream instance id"); | |
2858 | goto end; | |
2859 | } | |
2860 | } else { | |
2861 | subbuf->info.data.stream_instance_id.is_set = true; | |
2862 | } | |
2863 | end: | |
2864 | return ret; | |
2865 | } | |
1d4dfdef | 2866 | |
6f9449c2 JG |
2867 | static int get_next_subbuffer_common(struct lttng_consumer_stream *stream, |
2868 | struct stream_subbuffer *subbuffer) | |
2869 | { | |
2870 | int ret; | |
2871 | const char *addr; | |
1d4dfdef | 2872 | |
6f9449c2 JG |
2873 | ret = stream->read_subbuffer_ops.extract_subbuffer_info( |
2874 | stream, subbuffer); | |
2875 | if (ret) { | |
2876 | goto end; | |
2877 | } | |
02d02e31 | 2878 | |
6f9449c2 | 2879 | ret = get_current_subbuf_addr(stream, &addr); |
128708c3 | 2880 | if (ret) { |
6f9449c2 | 2881 | goto end; |
128708c3 JG |
2882 | } |
2883 | ||
6f9449c2 JG |
2884 | subbuffer->buffer.buffer = lttng_buffer_view_init( |
2885 | addr, 0, subbuffer->info.data.padded_subbuf_size); | |
a0377dfe | 2886 | LTTNG_ASSERT(subbuffer->buffer.buffer.data != NULL); |
6f9449c2 JG |
2887 | end: |
2888 | return ret; | |
2889 | } | |
fd424d99 | 2890 | |
b6797c8e JG |
2891 | static enum get_next_subbuffer_status get_next_subbuffer( |
2892 | struct lttng_consumer_stream *stream, | |
6f9449c2 JG |
2893 | struct stream_subbuffer *subbuffer) |
2894 | { | |
2895 | int ret; | |
b6797c8e | 2896 | enum get_next_subbuffer_status status; |
331744e3 | 2897 | |
b623cb6a | 2898 | ret = lttng_ust_ctl_get_next_subbuf(stream->ustream); |
b6797c8e JG |
2899 | switch (ret) { |
2900 | case 0: | |
2901 | status = GET_NEXT_SUBBUFFER_STATUS_OK; | |
2902 | break; | |
2903 | case -ENODATA: | |
2904 | case -EAGAIN: | |
2905 | /* | |
2906 | * The caller only expects -ENODATA when there is no data to | |
2907 | * read, but the kernel tracer returns -EAGAIN when there is | |
2908 | * currently no data for a non-finalized stream, and -ENODATA | |
2909 | * when there is no data for a finalized stream. Those can be | |
2910 | * combined into a -ENODATA return value. | |
2911 | */ | |
2912 | status = GET_NEXT_SUBBUFFER_STATUS_NO_DATA; | |
2913 | goto end; | |
2914 | default: | |
2915 | status = GET_NEXT_SUBBUFFER_STATUS_ERROR; | |
6f9449c2 | 2916 | goto end; |
02b3d176 DG |
2917 | } |
2918 | ||
6f9449c2 JG |
2919 | ret = get_next_subbuffer_common(stream, subbuffer); |
2920 | if (ret) { | |
b6797c8e | 2921 | status = GET_NEXT_SUBBUFFER_STATUS_ERROR; |
23d56598 | 2922 | goto end; |
1c20f0e2 | 2923 | } |
6f9449c2 | 2924 | end: |
b6797c8e | 2925 | return status; |
6f9449c2 | 2926 | } |
1c20f0e2 | 2927 | |
b6797c8e JG |
2928 | static enum get_next_subbuffer_status get_next_subbuffer_metadata( |
2929 | struct lttng_consumer_stream *stream, | |
6f9449c2 JG |
2930 | struct stream_subbuffer *subbuffer) |
2931 | { | |
2932 | int ret; | |
f5ba75b4 JG |
2933 | bool cache_empty; |
2934 | bool got_subbuffer; | |
2935 | bool coherent; | |
2936 | bool buffer_empty; | |
2937 | unsigned long consumed_pos, produced_pos; | |
b6797c8e | 2938 | enum get_next_subbuffer_status status; |
6f9449c2 | 2939 | |
f5ba75b4 | 2940 | do { |
b623cb6a | 2941 | ret = lttng_ust_ctl_get_next_subbuf(stream->ustream); |
f5ba75b4 JG |
2942 | if (ret == 0) { |
2943 | got_subbuffer = true; | |
2944 | } else { | |
2945 | got_subbuffer = false; | |
2946 | if (ret != -EAGAIN) { | |
2947 | /* Fatal error. */ | |
b6797c8e | 2948 | status = GET_NEXT_SUBBUFFER_STATUS_ERROR; |
f5ba75b4 JG |
2949 | goto end; |
2950 | } | |
c585821b MD |
2951 | } |
2952 | ||
f5ba75b4 JG |
2953 | /* |
2954 | * Determine if the cache is empty and ensure that a sub-buffer | |
2955 | * is made available if the cache is not empty. | |
2956 | */ | |
2957 | if (!got_subbuffer) { | |
2958 | ret = commit_one_metadata_packet(stream); | |
2959 | if (ret < 0 && ret != -ENOBUFS) { | |
b6797c8e | 2960 | status = GET_NEXT_SUBBUFFER_STATUS_ERROR; |
f5ba75b4 JG |
2961 | goto end; |
2962 | } else if (ret == 0) { | |
2963 | /* Not an error, the cache is empty. */ | |
2964 | cache_empty = true; | |
b6797c8e | 2965 | status = GET_NEXT_SUBBUFFER_STATUS_NO_DATA; |
f5ba75b4 JG |
2966 | goto end; |
2967 | } else { | |
2968 | cache_empty = false; | |
2969 | } | |
2970 | } else { | |
2971 | pthread_mutex_lock(&stream->chan->metadata_cache->lock); | |
9eac9828 JG |
2972 | cache_empty = stream->chan->metadata_cache->contents.size == |
2973 | stream->ust_metadata_pushed; | |
f5ba75b4 | 2974 | pthread_mutex_unlock(&stream->chan->metadata_cache->lock); |
94d49140 | 2975 | } |
f5ba75b4 | 2976 | } while (!got_subbuffer); |
94d49140 | 2977 | |
f5ba75b4 | 2978 | /* Populate sub-buffer infos and view. */ |
6f9449c2 JG |
2979 | ret = get_next_subbuffer_common(stream, subbuffer); |
2980 | if (ret) { | |
b6797c8e | 2981 | status = GET_NEXT_SUBBUFFER_STATUS_ERROR; |
6f9449c2 | 2982 | goto end; |
309167d2 | 2983 | } |
f5ba75b4 JG |
2984 | |
2985 | ret = lttng_ustconsumer_sample_snapshot_positions(stream); | |
2986 | if (ret < 0) { | |
2987 | /* | |
2988 | * -EAGAIN is not expected since we got a sub-buffer and haven't | |
2989 | * pushed the consumption position yet (on put_next). | |
2990 | */ | |
2991 | PERROR("Failed to take a snapshot of metadata buffer positions"); | |
b6797c8e | 2992 | status = GET_NEXT_SUBBUFFER_STATUS_ERROR; |
f5ba75b4 JG |
2993 | goto end; |
2994 | } | |
2995 | ||
2996 | ret = lttng_ustconsumer_get_consumed_snapshot(stream, &consumed_pos); | |
2997 | if (ret) { | |
2998 | PERROR("Failed to get metadata consumed position"); | |
b6797c8e | 2999 | status = GET_NEXT_SUBBUFFER_STATUS_ERROR; |
f5ba75b4 JG |
3000 | goto end; |
3001 | } | |
3002 | ||
3003 | ret = lttng_ustconsumer_get_produced_snapshot(stream, &produced_pos); | |
3004 | if (ret) { | |
3005 | PERROR("Failed to get metadata produced position"); | |
b6797c8e | 3006 | status = GET_NEXT_SUBBUFFER_STATUS_ERROR; |
f5ba75b4 JG |
3007 | goto end; |
3008 | } | |
3009 | ||
3010 | /* Last sub-buffer of the ring buffer ? */ | |
3011 | buffer_empty = (consumed_pos + stream->max_sb_size) == produced_pos; | |
3012 | ||
3013 | /* | |
3014 | * The sessiond registry lock ensures that coherent units of metadata | |
3015 | * are pushed to the consumer daemon at once. Hence, if a sub-buffer is | |
3016 | * acquired, the cache is empty, and it is the only available sub-buffer | |
3017 | * available, it is safe to assume that it is "coherent". | |
3018 | */ | |
3019 | coherent = got_subbuffer && cache_empty && buffer_empty; | |
3020 | ||
3021 | LTTNG_OPTIONAL_SET(&subbuffer->info.metadata.coherent, coherent); | |
b6797c8e | 3022 | status = GET_NEXT_SUBBUFFER_STATUS_OK; |
23d56598 | 3023 | end: |
b6797c8e | 3024 | return status; |
d41f73b7 MD |
3025 | } |
3026 | ||
6f9449c2 | 3027 | static int put_next_subbuffer(struct lttng_consumer_stream *stream, |
f46376a1 | 3028 | struct stream_subbuffer *subbuffer __attribute__((unused))) |
6f9449c2 | 3029 | { |
b623cb6a | 3030 | const int ret = lttng_ust_ctl_put_next_subbuf(stream->ustream); |
6f9449c2 | 3031 | |
a0377dfe | 3032 | LTTNG_ASSERT(ret == 0); |
6f9449c2 JG |
3033 | return ret; |
3034 | } | |
3035 | ||
3036 | static int signal_metadata(struct lttng_consumer_stream *stream, | |
f46376a1 | 3037 | struct lttng_consumer_local_data *ctx __attribute__((unused))) |
6f9449c2 | 3038 | { |
8db3acaf | 3039 | ASSERT_LOCKED(stream->metadata_rdv_lock); |
6f9449c2 JG |
3040 | return pthread_cond_broadcast(&stream->metadata_rdv) ? -errno : 0; |
3041 | } | |
3042 | ||
f5ba75b4 | 3043 | static int lttng_ustconsumer_set_stream_ops( |
6f9449c2 JG |
3044 | struct lttng_consumer_stream *stream) |
3045 | { | |
f5ba75b4 JG |
3046 | int ret = 0; |
3047 | ||
6f9449c2 JG |
3048 | stream->read_subbuffer_ops.on_wake_up = consumer_stream_ust_on_wake_up; |
3049 | if (stream->metadata_flag) { | |
3050 | stream->read_subbuffer_ops.get_next_subbuffer = | |
3051 | get_next_subbuffer_metadata; | |
3052 | stream->read_subbuffer_ops.extract_subbuffer_info = | |
3053 | extract_metadata_subbuffer_info; | |
3054 | stream->read_subbuffer_ops.reset_metadata = | |
55954e07 | 3055 | metadata_stream_reset_cache_consumed_position; |
f5ba75b4 JG |
3056 | if (stream->chan->is_live) { |
3057 | stream->read_subbuffer_ops.on_sleep = signal_metadata; | |
3058 | ret = consumer_stream_enable_metadata_bucketization( | |
3059 | stream); | |
3060 | if (ret) { | |
3061 | goto end; | |
3062 | } | |
3063 | } | |
6f9449c2 JG |
3064 | } else { |
3065 | stream->read_subbuffer_ops.get_next_subbuffer = | |
3066 | get_next_subbuffer; | |
3067 | stream->read_subbuffer_ops.extract_subbuffer_info = | |
3068 | extract_data_subbuffer_info; | |
3069 | stream->read_subbuffer_ops.on_sleep = notify_if_more_data; | |
3070 | if (stream->chan->is_live) { | |
3071 | stream->read_subbuffer_ops.send_live_beacon = | |
3072 | consumer_flush_ust_index; | |
3073 | } | |
3074 | } | |
3075 | ||
3076 | stream->read_subbuffer_ops.put_next_subbuffer = put_next_subbuffer; | |
f5ba75b4 JG |
3077 | end: |
3078 | return ret; | |
6f9449c2 JG |
3079 | } |
3080 | ||
ffe60014 DG |
3081 | /* |
3082 | * Called when a stream is created. | |
fe4477ee JD |
3083 | * |
3084 | * Return 0 on success or else a negative value. | |
ffe60014 | 3085 | */ |
d41f73b7 MD |
3086 | int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream *stream) |
3087 | { | |
fe4477ee JD |
3088 | int ret; |
3089 | ||
a0377dfe | 3090 | LTTNG_ASSERT(stream); |
10a50311 | 3091 | |
d2956687 JG |
3092 | /* |
3093 | * Don't create anything if this is set for streaming or if there is | |
3094 | * no current trace chunk on the parent channel. | |
3095 | */ | |
3096 | if (stream->net_seq_idx == (uint64_t) -1ULL && stream->chan->monitor && | |
3097 | stream->chan->trace_chunk) { | |
3098 | ret = consumer_stream_create_output_files(stream, true); | |
3099 | if (ret) { | |
fe4477ee JD |
3100 | goto error; |
3101 | } | |
fe4477ee | 3102 | } |
6f9449c2 JG |
3103 | |
3104 | lttng_ustconsumer_set_stream_ops(stream); | |
fe4477ee JD |
3105 | ret = 0; |
3106 | ||
3107 | error: | |
3108 | return ret; | |
d41f73b7 | 3109 | } |
ca22feea DG |
3110 | |
3111 | /* | |
3112 | * Check if data is still being extracted from the buffers for a specific | |
4e9a4686 DG |
3113 | * stream. Consumer data lock MUST be acquired before calling this function |
3114 | * and the stream lock. | |
ca22feea | 3115 | * |
6d805429 | 3116 | * Return 1 if the traced data are still getting read else 0 meaning that the |
ca22feea DG |
3117 | * data is available for trace viewer reading. |
3118 | */ | |
6d805429 | 3119 | int lttng_ustconsumer_data_pending(struct lttng_consumer_stream *stream) |
ca22feea DG |
3120 | { |
3121 | int ret; | |
3122 | ||
a0377dfe FD |
3123 | LTTNG_ASSERT(stream); |
3124 | LTTNG_ASSERT(stream->ustream); | |
b1316da1 | 3125 | ASSERT_LOCKED(stream->lock); |
ca22feea | 3126 | |
6d805429 | 3127 | DBG("UST consumer checking data pending"); |
c8f59ee5 | 3128 | |
ca6b395f MD |
3129 | if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) { |
3130 | ret = 0; | |
3131 | goto end; | |
3132 | } | |
3133 | ||
04ef1097 | 3134 | if (stream->chan->type == CONSUMER_CHANNEL_TYPE_METADATA) { |
e6ee4eab DG |
3135 | uint64_t contiguous, pushed; |
3136 | ||
3137 | /* Ease our life a bit. */ | |
934ba8fd | 3138 | pthread_mutex_lock(&stream->chan->metadata_cache->lock); |
9eac9828 | 3139 | contiguous = stream->chan->metadata_cache->contents.size; |
934ba8fd | 3140 | pthread_mutex_unlock(&stream->chan->metadata_cache->lock); |
e6ee4eab DG |
3141 | pushed = stream->ust_metadata_pushed; |
3142 | ||
04ef1097 MD |
3143 | /* |
3144 | * We can simply check whether all contiguously available data | |
3145 | * has been pushed to the ring buffer, since the push operation | |
3146 | * is performed within get_next_subbuf(), and because both | |
3147 | * get_next_subbuf() and put_next_subbuf() are issued atomically | |
3148 | * thanks to the stream lock within | |
3149 | * lttng_ustconsumer_read_subbuffer(). This basically means that | |
3150 | * whetnever ust_metadata_pushed is incremented, the associated | |
3151 | * metadata has been consumed from the metadata stream. | |
3152 | */ | |
3153 | DBG("UST consumer metadata pending check: contiguous %" PRIu64 " vs pushed %" PRIu64, | |
e6ee4eab | 3154 | contiguous, pushed); |
a0377dfe | 3155 | LTTNG_ASSERT(((int64_t) (contiguous - pushed)) >= 0); |
e6ee4eab | 3156 | if ((contiguous != pushed) || |
6acdf328 | 3157 | (((int64_t) contiguous - pushed) > 0 || contiguous == 0)) { |
04ef1097 MD |
3158 | ret = 1; /* Data is pending */ |
3159 | goto end; | |
3160 | } | |
3161 | } else { | |
b623cb6a | 3162 | ret = lttng_ust_ctl_get_next_subbuf(stream->ustream); |
04ef1097 MD |
3163 | if (ret == 0) { |
3164 | /* | |
3165 | * There is still data so let's put back this | |
3166 | * subbuffer. | |
3167 | */ | |
b623cb6a | 3168 | ret = lttng_ust_ctl_put_subbuf(stream->ustream); |
a0377dfe | 3169 | LTTNG_ASSERT(ret == 0); |
04ef1097 MD |
3170 | ret = 1; /* Data is pending */ |
3171 | goto end; | |
3172 | } | |
ca22feea DG |
3173 | } |
3174 | ||
6d805429 DG |
3175 | /* Data is NOT pending so ready to be read. */ |
3176 | ret = 0; | |
ca22feea | 3177 | |
6efae65e DG |
3178 | end: |
3179 | return ret; | |
ca22feea | 3180 | } |
d88aee68 | 3181 | |
6d574024 DG |
3182 | /* |
3183 | * Stop a given metadata channel timer if enabled and close the wait fd which | |
3184 | * is the poll pipe of the metadata stream. | |
3185 | * | |
d2c82a5a | 3186 | * This MUST be called with the metadata channel lock acquired. |
6d574024 DG |
3187 | */ |
3188 | void lttng_ustconsumer_close_metadata(struct lttng_consumer_channel *metadata) | |
3189 | { | |
3190 | int ret; | |
3191 | ||
a0377dfe FD |
3192 | LTTNG_ASSERT(metadata); |
3193 | LTTNG_ASSERT(metadata->type == CONSUMER_CHANNEL_TYPE_METADATA); | |
6d574024 DG |
3194 | |
3195 | DBG("Closing metadata channel key %" PRIu64, metadata->key); | |
3196 | ||
3197 | if (metadata->switch_timer_enabled == 1) { | |
3198 | consumer_timer_switch_stop(metadata); | |
3199 | } | |
3200 | ||
3201 | if (!metadata->metadata_stream) { | |
3202 | goto end; | |
3203 | } | |
3204 | ||
3205 | /* | |
3206 | * Closing write side so the thread monitoring the stream wakes up if any | |
3207 | * and clean the metadata stream. | |
3208 | */ | |
3209 | if (metadata->metadata_stream->ust_metadata_poll_pipe[1] >= 0) { | |
3210 | ret = close(metadata->metadata_stream->ust_metadata_poll_pipe[1]); | |
3211 | if (ret < 0) { | |
3212 | PERROR("closing metadata pipe write side"); | |
3213 | } | |
3214 | metadata->metadata_stream->ust_metadata_poll_pipe[1] = -1; | |
3215 | } | |
3216 | ||
3217 | end: | |
3218 | return; | |
3219 | } | |
3220 | ||
d88aee68 DG |
3221 | /* |
3222 | * Close every metadata stream wait fd of the metadata hash table. This | |
3223 | * function MUST be used very carefully so not to run into a race between the | |
3224 | * metadata thread handling streams and this function closing their wait fd. | |
3225 | * | |
3226 | * For UST, this is used when the session daemon hangs up. Its the metadata | |
3227 | * producer so calling this is safe because we are assured that no state change | |
3228 | * can occur in the metadata thread for the streams in the hash table. | |
3229 | */ | |
6d574024 | 3230 | void lttng_ustconsumer_close_all_metadata(struct lttng_ht *metadata_ht) |
d88aee68 | 3231 | { |
d88aee68 DG |
3232 | struct lttng_ht_iter iter; |
3233 | struct lttng_consumer_stream *stream; | |
3234 | ||
a0377dfe FD |
3235 | LTTNG_ASSERT(metadata_ht); |
3236 | LTTNG_ASSERT(metadata_ht->ht); | |
d88aee68 DG |
3237 | |
3238 | DBG("UST consumer closing all metadata streams"); | |
3239 | ||
3240 | rcu_read_lock(); | |
3241 | cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream, | |
3242 | node.node) { | |
9ce5646a MD |
3243 | |
3244 | health_code_update(); | |
3245 | ||
be2b50c7 | 3246 | pthread_mutex_lock(&stream->chan->lock); |
6d574024 | 3247 | lttng_ustconsumer_close_metadata(stream->chan); |
be2b50c7 DG |
3248 | pthread_mutex_unlock(&stream->chan->lock); |
3249 | ||
d88aee68 DG |
3250 | } |
3251 | rcu_read_unlock(); | |
3252 | } | |
d8ef542d MD |
3253 | |
3254 | void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream *stream) | |
3255 | { | |
3256 | int ret; | |
3257 | ||
b623cb6a | 3258 | ret = lttng_ust_ctl_stream_close_wakeup_fd(stream->ustream); |
d8ef542d MD |
3259 | if (ret < 0) { |
3260 | ERR("Unable to close wakeup fd"); | |
3261 | } | |
3262 | } | |
331744e3 | 3263 | |
f666ae70 MD |
3264 | /* |
3265 | * Please refer to consumer-timer.c before adding any lock within this | |
3266 | * function or any of its callees. Timers have a very strict locking | |
3267 | * semantic with respect to teardown. Failure to respect this semantic | |
3268 | * introduces deadlocks. | |
c585821b MD |
3269 | * |
3270 | * DON'T hold the metadata lock when calling this function, else this | |
3271 | * can cause deadlock involving consumer awaiting for metadata to be | |
3272 | * pushed out due to concurrent interaction with the session daemon. | |
f666ae70 | 3273 | */ |
331744e3 | 3274 | int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data *ctx, |
94d49140 | 3275 | struct lttng_consumer_channel *channel, int timer, int wait) |
331744e3 JD |
3276 | { |
3277 | struct lttcomm_metadata_request_msg request; | |
3278 | struct lttcomm_consumer_msg msg; | |
0c759fc9 | 3279 | enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS; |
93ec662e | 3280 | uint64_t len, key, offset, version; |
331744e3 JD |
3281 | int ret; |
3282 | ||
a0377dfe FD |
3283 | LTTNG_ASSERT(channel); |
3284 | LTTNG_ASSERT(channel->metadata_cache); | |
331744e3 | 3285 | |
53efb85a MD |
3286 | memset(&request, 0, sizeof(request)); |
3287 | ||
331744e3 | 3288 | /* send the metadata request to sessiond */ |
fa29bfbf | 3289 | switch (the_consumer_data.type) { |
331744e3 JD |
3290 | case LTTNG_CONSUMER64_UST: |
3291 | request.bits_per_long = 64; | |
3292 | break; | |
3293 | case LTTNG_CONSUMER32_UST: | |
3294 | request.bits_per_long = 32; | |
3295 | break; | |
3296 | default: | |
3297 | request.bits_per_long = 0; | |
3298 | break; | |
3299 | } | |
3300 | ||
3301 | request.session_id = channel->session_id; | |
1950109e | 3302 | request.session_id_per_pid = channel->session_id_per_pid; |
567eb353 DG |
3303 | /* |
3304 | * Request the application UID here so the metadata of that application can | |
3305 | * be sent back. The channel UID corresponds to the user UID of the session | |
3306 | * used for the rights on the stream file(s). | |
3307 | */ | |
3308 | request.uid = channel->ust_app_uid; | |
331744e3 | 3309 | request.key = channel->key; |
567eb353 | 3310 | |
1950109e | 3311 | DBG("Sending metadata request to sessiond, session id %" PRIu64 |
3e25d926 | 3312 | ", per-pid %" PRIu64 ", app UID %u and channel key %" PRIu64, |
567eb353 DG |
3313 | request.session_id, request.session_id_per_pid, request.uid, |
3314 | request.key); | |
331744e3 | 3315 | |
75d83e50 | 3316 | pthread_mutex_lock(&ctx->metadata_socket_lock); |
9ce5646a MD |
3317 | |
3318 | health_code_update(); | |
3319 | ||
331744e3 JD |
3320 | ret = lttcomm_send_unix_sock(ctx->consumer_metadata_socket, &request, |
3321 | sizeof(request)); | |
3322 | if (ret < 0) { | |
3323 | ERR("Asking metadata to sessiond"); | |
3324 | goto end; | |
3325 | } | |
3326 | ||
9ce5646a MD |
3327 | health_code_update(); |
3328 | ||
331744e3 JD |
3329 | /* Receive the metadata from sessiond */ |
3330 | ret = lttcomm_recv_unix_sock(ctx->consumer_metadata_socket, &msg, | |
3331 | sizeof(msg)); | |
3332 | if (ret != sizeof(msg)) { | |
8fd623e0 | 3333 | DBG("Consumer received unexpected message size %d (expects %zu)", |
331744e3 JD |
3334 | ret, sizeof(msg)); |
3335 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD); | |
3336 | /* | |
3337 | * The ret value might 0 meaning an orderly shutdown but this is ok | |
3338 | * since the caller handles this. | |
3339 | */ | |
3340 | goto end; | |
3341 | } | |
3342 | ||
9ce5646a MD |
3343 | health_code_update(); |
3344 | ||
331744e3 JD |
3345 | if (msg.cmd_type == LTTNG_ERR_UND) { |
3346 | /* No registry found */ | |
3347 | (void) consumer_send_status_msg(ctx->consumer_metadata_socket, | |
3348 | ret_code); | |
3349 | ret = 0; | |
3350 | goto end; | |
3351 | } else if (msg.cmd_type != LTTNG_CONSUMER_PUSH_METADATA) { | |
3352 | ERR("Unexpected cmd_type received %d", msg.cmd_type); | |
3353 | ret = -1; | |
3354 | goto end; | |
3355 | } | |
3356 | ||
3357 | len = msg.u.push_metadata.len; | |
3358 | key = msg.u.push_metadata.key; | |
3359 | offset = msg.u.push_metadata.target_offset; | |
93ec662e | 3360 | version = msg.u.push_metadata.version; |
331744e3 | 3361 | |
a0377dfe | 3362 | LTTNG_ASSERT(key == channel->key); |
331744e3 JD |
3363 | if (len == 0) { |
3364 | DBG("No new metadata to receive for key %" PRIu64, key); | |
3365 | } | |
3366 | ||
9ce5646a MD |
3367 | health_code_update(); |
3368 | ||
331744e3 JD |
3369 | /* Tell session daemon we are ready to receive the metadata. */ |
3370 | ret = consumer_send_status_msg(ctx->consumer_metadata_socket, | |
0c759fc9 | 3371 | LTTCOMM_CONSUMERD_SUCCESS); |
331744e3 JD |
3372 | if (ret < 0 || len == 0) { |
3373 | /* | |
3374 | * Somehow, the session daemon is not responding anymore or there is | |
3375 | * nothing to receive. | |
3376 | */ | |
3377 | goto end; | |
3378 | } | |
3379 | ||
9ce5646a MD |
3380 | health_code_update(); |
3381 | ||
1eb682be | 3382 | ret = lttng_ustconsumer_recv_metadata(ctx->consumer_metadata_socket, |
93ec662e | 3383 | key, offset, len, version, channel, timer, wait); |
1eb682be | 3384 | if (ret >= 0) { |
f2a444f1 DG |
3385 | /* |
3386 | * Only send the status msg if the sessiond is alive meaning a positive | |
3387 | * ret code. | |
3388 | */ | |
1eb682be | 3389 | (void) consumer_send_status_msg(ctx->consumer_metadata_socket, ret); |
f2a444f1 | 3390 | } |
331744e3 JD |
3391 | ret = 0; |
3392 | ||
3393 | end: | |
9ce5646a MD |
3394 | health_code_update(); |
3395 | ||
75d83e50 | 3396 | pthread_mutex_unlock(&ctx->metadata_socket_lock); |
331744e3 JD |
3397 | return ret; |
3398 | } | |
70190e1c DG |
3399 | |
3400 | /* | |
3401 | * Return the ustctl call for the get stream id. | |
3402 | */ | |
3403 | int lttng_ustconsumer_get_stream_id(struct lttng_consumer_stream *stream, | |
3404 | uint64_t *stream_id) | |
3405 | { | |
a0377dfe FD |
3406 | LTTNG_ASSERT(stream); |
3407 | LTTNG_ASSERT(stream_id); | |
70190e1c | 3408 | |
b623cb6a | 3409 | return lttng_ust_ctl_get_stream_id(stream->ustream, stream_id); |
70190e1c | 3410 | } |
881fc67f MD |
3411 | |
3412 | void lttng_ustconsumer_sigbus_handle(void *addr) | |
3413 | { | |
3414 | lttng_ust_ctl_sigbus_handle(addr); | |
3415 | } |