Commit | Line | Data |
---|---|---|
3bd1e081 MD |
1 | /* |
2 | * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca> | |
3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
4 | * | |
d14d33bf AM |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License, version 2 only, | |
7 | * as published by the Free Software Foundation. | |
3bd1e081 MD |
8 | * |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
d14d33bf AM |
14 | * You should have received a copy of the GNU General Public License along |
15 | * with this program; if not, write to the Free Software Foundation, Inc., | |
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
3bd1e081 MD |
17 | */ |
18 | ||
19 | #define _GNU_SOURCE | |
20 | #include <assert.h> | |
f02e1e8a | 21 | #include <lttng/ust-ctl.h> |
3bd1e081 MD |
22 | #include <poll.h> |
23 | #include <pthread.h> | |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
26 | #include <sys/mman.h> | |
27 | #include <sys/socket.h> | |
dbb5dfe6 | 28 | #include <sys/stat.h> |
3bd1e081 | 29 | #include <sys/types.h> |
77c7c900 | 30 | #include <inttypes.h> |
3bd1e081 | 31 | #include <unistd.h> |
0857097f | 32 | |
990570ed | 33 | #include <common/common.h> |
10a8a223 | 34 | #include <common/sessiond-comm/sessiond-comm.h> |
00e2e675 | 35 | #include <common/relayd/relayd.h> |
dbb5dfe6 | 36 | #include <common/compat/fcntl.h> |
10a8a223 DG |
37 | |
38 | #include "ust-consumer.h" | |
3bd1e081 MD |
39 | |
40 | extern struct lttng_consumer_global_data consumer_data; | |
41 | extern int consumer_poll_timeout; | |
42 | extern volatile int consumer_quit; | |
43 | ||
44 | /* | |
f02e1e8a DG |
45 | * Wrapper over the mmap() read offset from ust-ctl library. Since this can be |
46 | * compiled out, we isolate it in this library. | |
3bd1e081 | 47 | */ |
f02e1e8a DG |
48 | int lttng_ustctl_get_mmap_read_offset(struct lttng_ust_shm_handle *handle, |
49 | struct lttng_ust_lib_ring_buffer *buf, unsigned long *off) | |
3bd1e081 | 50 | { |
f02e1e8a DG |
51 | return ustctl_get_mmap_read_offset(handle, buf, off); |
52 | }; | |
3bd1e081 MD |
53 | |
54 | /* | |
55 | * Take a snapshot for a specific fd | |
56 | * | |
57 | * Returns 0 on success, < 0 on error | |
58 | */ | |
59 | int lttng_ustconsumer_take_snapshot(struct lttng_consumer_local_data *ctx, | |
60 | struct lttng_consumer_stream *stream) | |
61 | { | |
62 | int ret = 0; | |
63 | ||
64 | ret = ustctl_snapshot(stream->chan->handle, stream->buf); | |
65 | if (ret != 0) { | |
87dc6a9c | 66 | errno = -ret; |
4c462e79 | 67 | PERROR("Getting sub-buffer snapshot."); |
3bd1e081 MD |
68 | } |
69 | ||
70 | return ret; | |
71 | } | |
72 | ||
73 | /* | |
74 | * Get the produced position | |
75 | * | |
76 | * Returns 0 on success, < 0 on error | |
77 | */ | |
78 | int lttng_ustconsumer_get_produced_snapshot( | |
79 | struct lttng_consumer_local_data *ctx, | |
80 | struct lttng_consumer_stream *stream, | |
81 | unsigned long *pos) | |
82 | { | |
83 | int ret; | |
84 | ||
85 | ret = ustctl_snapshot_get_produced(stream->chan->handle, | |
86 | stream->buf, pos); | |
87 | if (ret != 0) { | |
87dc6a9c | 88 | errno = -ret; |
4c462e79 | 89 | PERROR("kernctl_snapshot_get_produced"); |
3bd1e081 MD |
90 | } |
91 | ||
92 | return ret; | |
93 | } | |
94 | ||
4cbc1a04 DG |
95 | /* |
96 | * Receive command from session daemon and process it. | |
97 | * | |
98 | * Return 1 on success else a negative value or 0. | |
99 | */ | |
3bd1e081 MD |
100 | int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data *ctx, |
101 | int sock, struct pollfd *consumer_sockpoll) | |
102 | { | |
103 | ssize_t ret; | |
104 | struct lttcomm_consumer_msg msg; | |
105 | ||
106 | ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg)); | |
107 | if (ret != sizeof(msg)) { | |
173af62f DG |
108 | DBG("Consumer received unexpected message size %zd (expects %zu)", |
109 | ret, sizeof(msg)); | |
f73fabfd | 110 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD); |
3bd1e081 MD |
111 | return ret; |
112 | } | |
113 | if (msg.cmd_type == LTTNG_CONSUMER_STOP) { | |
114 | return -ENOENT; | |
115 | } | |
116 | ||
3f8e211f | 117 | /* relayd needs RCU read-side lock */ |
b0b335c8 MD |
118 | rcu_read_lock(); |
119 | ||
3bd1e081 | 120 | switch (msg.cmd_type) { |
00e2e675 DG |
121 | case LTTNG_CONSUMER_ADD_RELAYD_SOCKET: |
122 | { | |
7735ef9e DG |
123 | ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index, |
124 | msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll, | |
125 | &msg.u.relayd_sock.sock); | |
00e2e675 DG |
126 | goto end_nosignal; |
127 | } | |
3bd1e081 MD |
128 | case LTTNG_CONSUMER_ADD_CHANNEL: |
129 | { | |
130 | struct lttng_consumer_channel *new_channel; | |
131 | int fds[1]; | |
132 | size_t nb_fd = 1; | |
133 | ||
173af62f DG |
134 | DBG("UST Consumer adding channel"); |
135 | ||
3bd1e081 MD |
136 | /* block */ |
137 | if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) { | |
3f8e211f | 138 | rcu_read_unlock(); |
3bd1e081 MD |
139 | return -EINTR; |
140 | } | |
141 | ret = lttcomm_recv_fds_unix_sock(sock, fds, nb_fd); | |
142 | if (ret != sizeof(fds)) { | |
f73fabfd | 143 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD); |
3f8e211f | 144 | rcu_read_unlock(); |
3bd1e081 MD |
145 | return ret; |
146 | } | |
147 | ||
148 | DBG("consumer_add_channel %d", msg.u.channel.channel_key); | |
149 | ||
150 | new_channel = consumer_allocate_channel(msg.u.channel.channel_key, | |
151 | fds[0], -1, | |
152 | msg.u.channel.mmap_len, | |
c30aaa51 MD |
153 | msg.u.channel.max_sb_size, |
154 | msg.u.channel.nb_init_streams); | |
3bd1e081 | 155 | if (new_channel == NULL) { |
f73fabfd | 156 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR); |
3bd1e081 MD |
157 | goto end_nosignal; |
158 | } | |
159 | if (ctx->on_recv_channel != NULL) { | |
160 | ret = ctx->on_recv_channel(new_channel); | |
161 | if (ret == 0) { | |
162 | consumer_add_channel(new_channel); | |
163 | } else if (ret < 0) { | |
164 | goto end_nosignal; | |
165 | } | |
166 | } else { | |
167 | consumer_add_channel(new_channel); | |
168 | } | |
169 | goto end_nosignal; | |
170 | } | |
171 | case LTTNG_CONSUMER_ADD_STREAM: | |
172 | { | |
173 | struct lttng_consumer_stream *new_stream; | |
50f8ae69 | 174 | int fds[2], stream_pipe; |
3bd1e081 | 175 | size_t nb_fd = 2; |
00e2e675 | 176 | struct consumer_relayd_sock_pair *relayd = NULL; |
c80048c6 | 177 | int alloc_ret = 0; |
3bd1e081 | 178 | |
173af62f DG |
179 | DBG("UST Consumer adding stream"); |
180 | ||
3bd1e081 MD |
181 | /* block */ |
182 | if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) { | |
3f8e211f | 183 | rcu_read_unlock(); |
3bd1e081 MD |
184 | return -EINTR; |
185 | } | |
186 | ret = lttcomm_recv_fds_unix_sock(sock, fds, nb_fd); | |
187 | if (ret != sizeof(fds)) { | |
f73fabfd | 188 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD); |
3f8e211f | 189 | rcu_read_unlock(); |
3bd1e081 MD |
190 | return ret; |
191 | } | |
192 | ||
7a57cf92 DG |
193 | DBG("Consumer command ADD_STREAM chan %d stream %d", |
194 | msg.u.stream.channel_key, msg.u.stream.stream_key); | |
173af62f | 195 | |
d41f73b7 | 196 | assert(msg.u.stream.output == LTTNG_EVENT_MMAP); |
173af62f | 197 | new_stream = consumer_allocate_stream(msg.u.stream.channel_key, |
3bd1e081 MD |
198 | msg.u.stream.stream_key, |
199 | fds[0], fds[1], | |
200 | msg.u.stream.state, | |
201 | msg.u.stream.mmap_len, | |
202 | msg.u.stream.output, | |
6df2e2c9 MD |
203 | msg.u.stream.path_name, |
204 | msg.u.stream.uid, | |
00e2e675 DG |
205 | msg.u.stream.gid, |
206 | msg.u.stream.net_index, | |
c80048c6 | 207 | msg.u.stream.metadata_flag, |
53632229 | 208 | msg.u.stream.session_id, |
c80048c6 | 209 | &alloc_ret); |
3bd1e081 | 210 | if (new_stream == NULL) { |
c80048c6 MD |
211 | switch (alloc_ret) { |
212 | case -ENOMEM: | |
213 | case -EINVAL: | |
214 | default: | |
215 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR); | |
216 | break; | |
217 | case -ENOENT: | |
218 | /* | |
219 | * We could not find the channel. Can happen if cpu hotplug | |
220 | * happens while tearing down. | |
221 | */ | |
222 | DBG3("Could not find channel"); | |
223 | break; | |
224 | } | |
3f8e211f | 225 | goto end_nosignal; |
3bd1e081 | 226 | } |
00e2e675 DG |
227 | |
228 | /* The stream is not metadata. Get relayd reference if exists. */ | |
229 | relayd = consumer_find_relayd(msg.u.stream.net_index); | |
230 | if (relayd != NULL) { | |
231 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); | |
232 | /* Add stream on the relayd */ | |
233 | ret = relayd_add_stream(&relayd->control_sock, | |
234 | msg.u.stream.name, msg.u.stream.path_name, | |
235 | &new_stream->relayd_stream_id); | |
236 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); | |
237 | if (ret < 0) { | |
e316aad5 | 238 | consumer_del_stream(new_stream, NULL); |
3f8e211f | 239 | goto end_nosignal; |
00e2e675 DG |
240 | } |
241 | } else if (msg.u.stream.net_index != -1) { | |
242 | ERR("Network sequence index %d unknown. Not adding stream.", | |
243 | msg.u.stream.net_index); | |
e316aad5 | 244 | consumer_del_stream(new_stream, NULL); |
3f8e211f | 245 | goto end_nosignal; |
00e2e675 DG |
246 | } |
247 | ||
633d0084 DG |
248 | /* Do actions once stream has been received. */ |
249 | if (ctx->on_recv_stream) { | |
250 | ret = ctx->on_recv_stream(new_stream); | |
251 | if (ret < 0) { | |
e316aad5 | 252 | consumer_del_stream(new_stream, NULL); |
633d0084 | 253 | goto end_nosignal; |
fb3a43a9 | 254 | } |
633d0084 | 255 | } |
fb3a43a9 | 256 | |
50f8ae69 | 257 | /* Get the right pipe where the stream will be sent. */ |
633d0084 | 258 | if (new_stream->metadata_flag) { |
50f8ae69 | 259 | stream_pipe = ctx->consumer_metadata_pipe[1]; |
3bd1e081 | 260 | } else { |
50f8ae69 DG |
261 | stream_pipe = ctx->consumer_data_pipe[1]; |
262 | } | |
263 | ||
264 | do { | |
265 | ret = write(stream_pipe, &new_stream, sizeof(new_stream)); | |
266 | } while (ret < 0 && errno == EINTR); | |
267 | if (ret < 0) { | |
268 | PERROR("Consumer write %s stream to pipe %d", | |
269 | new_stream->metadata_flag ? "metadata" : "data", | |
270 | stream_pipe); | |
271 | consumer_del_stream(new_stream, NULL); | |
272 | goto end_nosignal; | |
3bd1e081 | 273 | } |
00e2e675 | 274 | |
50f8ae69 | 275 | DBG("UST consumer ADD_STREAM %s (%d,%d) with relayd id %" PRIu64, |
00e2e675 DG |
276 | msg.u.stream.path_name, fds[0], fds[1], |
277 | new_stream->relayd_stream_id); | |
3bd1e081 MD |
278 | break; |
279 | } | |
173af62f DG |
280 | case LTTNG_CONSUMER_DESTROY_RELAYD: |
281 | { | |
a6ba4fe1 | 282 | uint64_t index = msg.u.destroy_relayd.net_seq_idx; |
173af62f DG |
283 | struct consumer_relayd_sock_pair *relayd; |
284 | ||
a6ba4fe1 | 285 | DBG("UST consumer destroying relayd %" PRIu64, index); |
173af62f DG |
286 | |
287 | /* Get relayd reference if exists. */ | |
a6ba4fe1 | 288 | relayd = consumer_find_relayd(index); |
173af62f | 289 | if (relayd == NULL) { |
a6ba4fe1 | 290 | ERR("Unable to find relayd %" PRIu64, index); |
3f8e211f | 291 | goto end_nosignal; |
173af62f DG |
292 | } |
293 | ||
a6ba4fe1 DG |
294 | /* |
295 | * Each relayd socket pair has a refcount of stream attached to it | |
296 | * which tells if the relayd is still active or not depending on the | |
297 | * refcount value. | |
298 | * | |
299 | * This will set the destroy flag of the relayd object and destroy it | |
300 | * if the refcount reaches zero when called. | |
301 | * | |
302 | * The destroy can happen either here or when a stream fd hangs up. | |
303 | */ | |
304 | consumer_flag_relayd_for_destroy(relayd); | |
173af62f | 305 | |
3f8e211f | 306 | goto end_nosignal; |
173af62f | 307 | } |
3bd1e081 MD |
308 | case LTTNG_CONSUMER_UPDATE_STREAM: |
309 | { | |
3f8e211f | 310 | rcu_read_unlock(); |
7ad0a0cb | 311 | return -ENOSYS; |
3bd1e081 | 312 | } |
53632229 DG |
313 | case LTTNG_CONSUMER_DATA_AVAILABLE: |
314 | { | |
ca22feea DG |
315 | int32_t ret; |
316 | uint64_t id = msg.u.data_available.session_id; | |
317 | ||
318 | DBG("UST consumer data available command for id %" PRIu64, id); | |
319 | ||
320 | ret = consumer_data_available(id); | |
321 | ||
322 | /* Send back returned value to session daemon */ | |
323 | ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret)); | |
324 | if (ret < 0) { | |
325 | PERROR("send data available ret code"); | |
326 | } | |
327 | break; | |
53632229 | 328 | } |
3bd1e081 MD |
329 | default: |
330 | break; | |
331 | } | |
3f8e211f | 332 | |
3bd1e081 | 333 | end_nosignal: |
b0b335c8 | 334 | rcu_read_unlock(); |
4cbc1a04 DG |
335 | |
336 | /* | |
337 | * Return 1 to indicate success since the 0 value can be a socket | |
338 | * shutdown during the recv() or send() call. | |
339 | */ | |
340 | return 1; | |
3bd1e081 MD |
341 | } |
342 | ||
343 | int lttng_ustconsumer_allocate_channel(struct lttng_consumer_channel *chan) | |
344 | { | |
13161846 | 345 | struct lttng_ust_object_data obj; |
3bd1e081 MD |
346 | |
347 | obj.handle = -1; | |
348 | obj.shm_fd = chan->shm_fd; | |
349 | obj.wait_fd = chan->wait_fd; | |
350 | obj.memory_map_size = chan->mmap_len; | |
351 | chan->handle = ustctl_map_channel(&obj); | |
352 | if (!chan->handle) { | |
353 | return -ENOMEM; | |
354 | } | |
b5c5fc29 | 355 | chan->wait_fd_is_copy = 1; |
2c1dd183 | 356 | chan->shm_fd = -1; |
b5c5fc29 | 357 | |
3bd1e081 MD |
358 | return 0; |
359 | } | |
360 | ||
d056b477 MD |
361 | void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream *stream) |
362 | { | |
363 | ustctl_flush_buffer(stream->chan->handle, stream->buf, 0); | |
effcf122 | 364 | stream->hangup_flush_done = 1; |
d056b477 MD |
365 | } |
366 | ||
3bd1e081 MD |
367 | void lttng_ustconsumer_del_channel(struct lttng_consumer_channel *chan) |
368 | { | |
369 | ustctl_unmap_channel(chan->handle); | |
370 | } | |
371 | ||
e316aad5 | 372 | int lttng_ustconsumer_add_stream(struct lttng_consumer_stream *stream) |
3bd1e081 | 373 | { |
13161846 | 374 | struct lttng_ust_object_data obj; |
3bd1e081 MD |
375 | int ret; |
376 | ||
377 | obj.handle = -1; | |
378 | obj.shm_fd = stream->shm_fd; | |
379 | obj.wait_fd = stream->wait_fd; | |
380 | obj.memory_map_size = stream->mmap_len; | |
381 | ret = ustctl_add_stream(stream->chan->handle, &obj); | |
7a57cf92 DG |
382 | if (ret) { |
383 | ERR("UST ctl add_stream failed with ret %d", ret); | |
e316aad5 | 384 | goto error; |
7a57cf92 DG |
385 | } |
386 | ||
3bd1e081 | 387 | stream->buf = ustctl_open_stream_read(stream->chan->handle, stream->cpu); |
7a57cf92 DG |
388 | if (!stream->buf) { |
389 | ERR("UST ctl open_stream_read failed"); | |
e316aad5 DG |
390 | ret = -EBUSY; |
391 | goto error; | |
7a57cf92 DG |
392 | } |
393 | ||
2c1dd183 MD |
394 | /* ustctl_open_stream_read has closed the shm fd. */ |
395 | stream->wait_fd_is_copy = 1; | |
396 | stream->shm_fd = -1; | |
397 | ||
3bd1e081 MD |
398 | stream->mmap_base = ustctl_get_mmap_base(stream->chan->handle, stream->buf); |
399 | if (!stream->mmap_base) { | |
7a57cf92 | 400 | ERR("UST ctl get_mmap_base failed"); |
e316aad5 DG |
401 | ret = -EINVAL; |
402 | goto mmap_error; | |
3bd1e081 | 403 | } |
ee77a7b0 | 404 | |
3bd1e081 | 405 | return 0; |
e316aad5 DG |
406 | |
407 | mmap_error: | |
408 | ustctl_close_stream_read(stream->chan->handle, stream->buf); | |
409 | error: | |
410 | return ret; | |
3bd1e081 MD |
411 | } |
412 | ||
413 | void lttng_ustconsumer_del_stream(struct lttng_consumer_stream *stream) | |
414 | { | |
415 | ustctl_close_stream_read(stream->chan->handle, stream->buf); | |
416 | } | |
d41f73b7 MD |
417 | |
418 | ||
419 | int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream *stream, | |
420 | struct lttng_consumer_local_data *ctx) | |
421 | { | |
1d4dfdef | 422 | unsigned long len, subbuf_size, padding; |
d41f73b7 MD |
423 | int err; |
424 | long ret = 0; | |
425 | struct lttng_ust_shm_handle *handle; | |
426 | struct lttng_ust_lib_ring_buffer *buf; | |
427 | char dummy; | |
428 | ssize_t readlen; | |
429 | ||
430 | DBG("In read_subbuffer (wait_fd: %d, stream key: %d)", | |
431 | stream->wait_fd, stream->key); | |
432 | ||
433 | /* We can consume the 1 byte written into the wait_fd by UST */ | |
effcf122 MD |
434 | if (!stream->hangup_flush_done) { |
435 | do { | |
436 | readlen = read(stream->wait_fd, &dummy, 1); | |
87dc6a9c | 437 | } while (readlen == -1 && errno == EINTR); |
effcf122 MD |
438 | if (readlen == -1) { |
439 | ret = readlen; | |
440 | goto end; | |
441 | } | |
d41f73b7 MD |
442 | } |
443 | ||
444 | buf = stream->buf; | |
445 | handle = stream->chan->handle; | |
446 | /* Get the next subbuffer */ | |
447 | err = ustctl_get_next_subbuf(handle, buf); | |
448 | if (err != 0) { | |
1d4dfdef | 449 | ret = err; /* ustctl_get_next_subbuf returns negative, caller expect positive. */ |
d41f73b7 MD |
450 | /* |
451 | * This is a debug message even for single-threaded consumer, | |
452 | * because poll() have more relaxed criterions than get subbuf, | |
453 | * so get_subbuf may fail for short race windows where poll() | |
454 | * would issue wakeups. | |
455 | */ | |
456 | DBG("Reserving sub buffer failed (everything is normal, " | |
457 | "it is due to concurrency)"); | |
458 | goto end; | |
459 | } | |
460 | assert(stream->output == LTTNG_EVENT_MMAP); | |
1d4dfdef | 461 | /* Get the full padded subbuffer size */ |
d41f73b7 | 462 | err = ustctl_get_padded_subbuf_size(handle, buf, &len); |
effcf122 | 463 | assert(err == 0); |
1d4dfdef DG |
464 | |
465 | /* Get subbuffer data size (without padding) */ | |
466 | err = ustctl_get_subbuf_size(handle, buf, &subbuf_size); | |
467 | assert(err == 0); | |
468 | ||
469 | /* Make sure we don't get a subbuffer size bigger than the padded */ | |
470 | assert(len >= subbuf_size); | |
471 | ||
472 | padding = len - subbuf_size; | |
d41f73b7 | 473 | /* write the subbuffer to the tracefile */ |
1d4dfdef | 474 | ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size, padding); |
91dfef6e DG |
475 | /* |
476 | * The mmap operation should write subbuf_size amount of data when network | |
477 | * streaming or the full padding (len) size when we are _not_ streaming. | |
478 | */ | |
479 | if ((ret != subbuf_size && stream->net_seq_idx != -1) || | |
480 | (ret != len && stream->net_seq_idx == -1)) { | |
d41f73b7 | 481 | /* |
91dfef6e DG |
482 | * Display the error but continue processing to try to release the |
483 | * subbuffer | |
d41f73b7 | 484 | */ |
91dfef6e DG |
485 | ERR("Error writing to tracefile " |
486 | "(ret: %zd != len: %lu != subbuf_size: %lu)", | |
487 | ret, len, subbuf_size); | |
d41f73b7 MD |
488 | } |
489 | err = ustctl_put_next_subbuf(handle, buf); | |
effcf122 | 490 | assert(err == 0); |
d41f73b7 MD |
491 | end: |
492 | return ret; | |
493 | } | |
494 | ||
495 | int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream *stream) | |
496 | { | |
497 | int ret; | |
498 | ||
499 | /* Opening the tracefile in write mode */ | |
00e2e675 | 500 | if (stream->path_name != NULL && stream->net_seq_idx == -1) { |
e11d277b | 501 | ret = run_as_open(stream->path_name, |
60b6c79c MD |
502 | O_WRONLY|O_CREAT|O_TRUNC, |
503 | S_IRWXU|S_IRWXG|S_IRWXO, | |
504 | stream->uid, stream->gid); | |
d41f73b7 MD |
505 | if (ret < 0) { |
506 | ERR("Opening %s", stream->path_name); | |
4c462e79 | 507 | PERROR("open"); |
d41f73b7 MD |
508 | goto error; |
509 | } | |
510 | stream->out_fd = ret; | |
511 | } | |
512 | ||
c869f647 DG |
513 | ret = lttng_ustconsumer_add_stream(stream); |
514 | if (ret) { | |
515 | consumer_del_stream(stream, NULL); | |
516 | ret = -1; | |
517 | goto error; | |
518 | } | |
519 | ||
d41f73b7 MD |
520 | /* we return 0 to let the library handle the FD internally */ |
521 | return 0; | |
522 | ||
523 | error: | |
524 | return ret; | |
525 | } | |
ca22feea DG |
526 | |
527 | /* | |
528 | * Check if data is still being extracted from the buffers for a specific | |
529 | * stream. Consumer data lock MUST be acquired before calling this function. | |
530 | * | |
531 | * Return 0 if the traced data are still getting read else 1 meaning that the | |
532 | * data is available for trace viewer reading. | |
533 | */ | |
534 | int lttng_ustconsumer_data_available(struct lttng_consumer_stream *stream) | |
535 | { | |
536 | int ret; | |
537 | ||
538 | assert(stream); | |
539 | ||
540 | /* | |
541 | * Try to lock the stream mutex. On failure, we know that the stream is | |
542 | * being used else where hence there is data still being extracted. | |
543 | */ | |
544 | ret = pthread_mutex_trylock(&stream->lock); | |
545 | if (ret == EBUSY) { | |
546 | goto data_not_available; | |
547 | } | |
548 | /* The stream is now locked so we can do our ustctl calls */ | |
549 | ||
550 | ret = ustctl_get_next_subbuf(stream->chan->handle, stream->buf); | |
551 | if (ret == 0) { | |
552 | /* There is still data so let's put back this subbuffer. */ | |
553 | ret = ustctl_put_subbuf(stream->chan->handle, stream->buf); | |
554 | assert(ret == 0); | |
555 | pthread_mutex_unlock(&stream->lock); | |
556 | goto data_not_available; | |
557 | } | |
558 | ||
559 | /* Data is available to be read for this stream. */ | |
560 | pthread_mutex_unlock(&stream->lock); | |
561 | return 1; | |
562 | ||
563 | data_not_available: | |
564 | return 0; | |
565 | } |