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; | |
f50f23d9 | 104 | enum lttng_error_code ret_code = LTTNG_OK; |
3bd1e081 MD |
105 | struct lttcomm_consumer_msg msg; |
106 | ||
107 | ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg)); | |
108 | if (ret != sizeof(msg)) { | |
173af62f DG |
109 | DBG("Consumer received unexpected message size %zd (expects %zu)", |
110 | ret, sizeof(msg)); | |
f73fabfd | 111 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD); |
3bd1e081 MD |
112 | return ret; |
113 | } | |
114 | if (msg.cmd_type == LTTNG_CONSUMER_STOP) { | |
f50f23d9 DG |
115 | /* |
116 | * Notify the session daemon that the command is completed. | |
117 | * | |
118 | * On transport layer error, the function call will print an error | |
119 | * message so handling the returned code is a bit useless since we | |
120 | * return an error code anyway. | |
121 | */ | |
122 | (void) consumer_send_status_msg(sock, ret_code); | |
3bd1e081 MD |
123 | return -ENOENT; |
124 | } | |
125 | ||
3f8e211f | 126 | /* relayd needs RCU read-side lock */ |
b0b335c8 MD |
127 | rcu_read_lock(); |
128 | ||
3bd1e081 | 129 | switch (msg.cmd_type) { |
00e2e675 DG |
130 | case LTTNG_CONSUMER_ADD_RELAYD_SOCKET: |
131 | { | |
f50f23d9 | 132 | /* Session daemon status message are handled in the following call. */ |
7735ef9e DG |
133 | ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index, |
134 | msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll, | |
46e6455f | 135 | &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id); |
00e2e675 DG |
136 | goto end_nosignal; |
137 | } | |
3bd1e081 MD |
138 | case LTTNG_CONSUMER_ADD_CHANNEL: |
139 | { | |
140 | struct lttng_consumer_channel *new_channel; | |
141 | int fds[1]; | |
142 | size_t nb_fd = 1; | |
143 | ||
173af62f DG |
144 | DBG("UST Consumer adding channel"); |
145 | ||
f50f23d9 DG |
146 | /* First send a status message before receiving the fds. */ |
147 | ret = consumer_send_status_msg(sock, ret_code); | |
148 | if (ret < 0) { | |
149 | /* Somehow, the session daemon is not responding anymore. */ | |
150 | goto end_nosignal; | |
151 | } | |
152 | ||
3bd1e081 MD |
153 | /* block */ |
154 | if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) { | |
3f8e211f | 155 | rcu_read_unlock(); |
3bd1e081 MD |
156 | return -EINTR; |
157 | } | |
158 | ret = lttcomm_recv_fds_unix_sock(sock, fds, nb_fd); | |
159 | if (ret != sizeof(fds)) { | |
f73fabfd | 160 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD); |
3f8e211f | 161 | rcu_read_unlock(); |
3bd1e081 MD |
162 | return ret; |
163 | } | |
164 | ||
f50f23d9 DG |
165 | /* |
166 | * Send status code to session daemon only if the recv works. If the | |
167 | * above recv() failed, the session daemon is notified through the | |
168 | * error socket and the teardown is eventually done. | |
169 | */ | |
170 | ret = consumer_send_status_msg(sock, ret_code); | |
171 | if (ret < 0) { | |
172 | /* Somehow, the session daemon is not responding anymore. */ | |
173 | goto end_nosignal; | |
174 | } | |
175 | ||
3bd1e081 MD |
176 | DBG("consumer_add_channel %d", msg.u.channel.channel_key); |
177 | ||
178 | new_channel = consumer_allocate_channel(msg.u.channel.channel_key, | |
179 | fds[0], -1, | |
180 | msg.u.channel.mmap_len, | |
c30aaa51 MD |
181 | msg.u.channel.max_sb_size, |
182 | msg.u.channel.nb_init_streams); | |
3bd1e081 | 183 | if (new_channel == NULL) { |
f73fabfd | 184 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR); |
3bd1e081 MD |
185 | goto end_nosignal; |
186 | } | |
187 | if (ctx->on_recv_channel != NULL) { | |
188 | ret = ctx->on_recv_channel(new_channel); | |
189 | if (ret == 0) { | |
190 | consumer_add_channel(new_channel); | |
191 | } else if (ret < 0) { | |
192 | goto end_nosignal; | |
193 | } | |
194 | } else { | |
195 | consumer_add_channel(new_channel); | |
196 | } | |
197 | goto end_nosignal; | |
198 | } | |
199 | case LTTNG_CONSUMER_ADD_STREAM: | |
200 | { | |
201 | struct lttng_consumer_stream *new_stream; | |
50f8ae69 | 202 | int fds[2], stream_pipe; |
3bd1e081 | 203 | size_t nb_fd = 2; |
00e2e675 | 204 | struct consumer_relayd_sock_pair *relayd = NULL; |
c80048c6 | 205 | int alloc_ret = 0; |
3bd1e081 | 206 | |
173af62f DG |
207 | DBG("UST Consumer adding stream"); |
208 | ||
f50f23d9 DG |
209 | /* First send a status message before receiving the fds. */ |
210 | ret = consumer_send_status_msg(sock, ret_code); | |
211 | if (ret < 0) { | |
212 | /* Somehow, the session daemon is not responding anymore. */ | |
213 | goto end_nosignal; | |
214 | } | |
215 | ||
3bd1e081 MD |
216 | /* block */ |
217 | if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) { | |
3f8e211f | 218 | rcu_read_unlock(); |
3bd1e081 MD |
219 | return -EINTR; |
220 | } | |
221 | ret = lttcomm_recv_fds_unix_sock(sock, fds, nb_fd); | |
222 | if (ret != sizeof(fds)) { | |
f73fabfd | 223 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD); |
3f8e211f | 224 | rcu_read_unlock(); |
3bd1e081 MD |
225 | return ret; |
226 | } | |
227 | ||
f50f23d9 DG |
228 | /* |
229 | * Send status code to session daemon only if the recv works. If the | |
230 | * above recv() failed, the session daemon is notified through the | |
231 | * error socket and the teardown is eventually done. | |
232 | */ | |
233 | ret = consumer_send_status_msg(sock, ret_code); | |
234 | if (ret < 0) { | |
235 | /* Somehow, the session daemon is not responding anymore. */ | |
236 | goto end_nosignal; | |
237 | } | |
238 | ||
7a57cf92 DG |
239 | DBG("Consumer command ADD_STREAM chan %d stream %d", |
240 | msg.u.stream.channel_key, msg.u.stream.stream_key); | |
173af62f | 241 | |
d41f73b7 | 242 | assert(msg.u.stream.output == LTTNG_EVENT_MMAP); |
173af62f | 243 | new_stream = consumer_allocate_stream(msg.u.stream.channel_key, |
3bd1e081 MD |
244 | msg.u.stream.stream_key, |
245 | fds[0], fds[1], | |
246 | msg.u.stream.state, | |
247 | msg.u.stream.mmap_len, | |
248 | msg.u.stream.output, | |
6df2e2c9 MD |
249 | msg.u.stream.path_name, |
250 | msg.u.stream.uid, | |
00e2e675 DG |
251 | msg.u.stream.gid, |
252 | msg.u.stream.net_index, | |
c80048c6 | 253 | msg.u.stream.metadata_flag, |
53632229 | 254 | msg.u.stream.session_id, |
c80048c6 | 255 | &alloc_ret); |
3bd1e081 | 256 | if (new_stream == NULL) { |
c80048c6 MD |
257 | switch (alloc_ret) { |
258 | case -ENOMEM: | |
259 | case -EINVAL: | |
260 | default: | |
261 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR); | |
262 | break; | |
263 | case -ENOENT: | |
264 | /* | |
265 | * We could not find the channel. Can happen if cpu hotplug | |
266 | * happens while tearing down. | |
267 | */ | |
268 | DBG3("Could not find channel"); | |
269 | break; | |
270 | } | |
3f8e211f | 271 | goto end_nosignal; |
3bd1e081 | 272 | } |
00e2e675 DG |
273 | |
274 | /* The stream is not metadata. Get relayd reference if exists. */ | |
275 | relayd = consumer_find_relayd(msg.u.stream.net_index); | |
276 | if (relayd != NULL) { | |
277 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); | |
278 | /* Add stream on the relayd */ | |
279 | ret = relayd_add_stream(&relayd->control_sock, | |
280 | msg.u.stream.name, msg.u.stream.path_name, | |
281 | &new_stream->relayd_stream_id); | |
282 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); | |
283 | if (ret < 0) { | |
e316aad5 | 284 | consumer_del_stream(new_stream, NULL); |
3f8e211f | 285 | goto end_nosignal; |
00e2e675 DG |
286 | } |
287 | } else if (msg.u.stream.net_index != -1) { | |
288 | ERR("Network sequence index %d unknown. Not adding stream.", | |
289 | msg.u.stream.net_index); | |
e316aad5 | 290 | consumer_del_stream(new_stream, NULL); |
3f8e211f | 291 | goto end_nosignal; |
00e2e675 DG |
292 | } |
293 | ||
633d0084 DG |
294 | /* Do actions once stream has been received. */ |
295 | if (ctx->on_recv_stream) { | |
296 | ret = ctx->on_recv_stream(new_stream); | |
297 | if (ret < 0) { | |
e316aad5 | 298 | consumer_del_stream(new_stream, NULL); |
633d0084 | 299 | goto end_nosignal; |
fb3a43a9 | 300 | } |
633d0084 | 301 | } |
fb3a43a9 | 302 | |
50f8ae69 | 303 | /* Get the right pipe where the stream will be sent. */ |
633d0084 | 304 | if (new_stream->metadata_flag) { |
50f8ae69 | 305 | stream_pipe = ctx->consumer_metadata_pipe[1]; |
3bd1e081 | 306 | } else { |
50f8ae69 DG |
307 | stream_pipe = ctx->consumer_data_pipe[1]; |
308 | } | |
309 | ||
310 | do { | |
311 | ret = write(stream_pipe, &new_stream, sizeof(new_stream)); | |
312 | } while (ret < 0 && errno == EINTR); | |
313 | if (ret < 0) { | |
314 | PERROR("Consumer write %s stream to pipe %d", | |
315 | new_stream->metadata_flag ? "metadata" : "data", | |
316 | stream_pipe); | |
317 | consumer_del_stream(new_stream, NULL); | |
318 | goto end_nosignal; | |
3bd1e081 | 319 | } |
00e2e675 | 320 | |
50f8ae69 | 321 | DBG("UST consumer ADD_STREAM %s (%d,%d) with relayd id %" PRIu64, |
00e2e675 DG |
322 | msg.u.stream.path_name, fds[0], fds[1], |
323 | new_stream->relayd_stream_id); | |
3bd1e081 MD |
324 | break; |
325 | } | |
173af62f DG |
326 | case LTTNG_CONSUMER_DESTROY_RELAYD: |
327 | { | |
a6ba4fe1 | 328 | uint64_t index = msg.u.destroy_relayd.net_seq_idx; |
173af62f DG |
329 | struct consumer_relayd_sock_pair *relayd; |
330 | ||
a6ba4fe1 | 331 | DBG("UST consumer destroying relayd %" PRIu64, index); |
173af62f DG |
332 | |
333 | /* Get relayd reference if exists. */ | |
a6ba4fe1 | 334 | relayd = consumer_find_relayd(index); |
173af62f | 335 | if (relayd == NULL) { |
3448e266 | 336 | DBG("Unable to find relayd %" PRIu64, index); |
f50f23d9 | 337 | ret_code = LTTNG_ERR_NO_CONSUMER; |
173af62f DG |
338 | } |
339 | ||
a6ba4fe1 DG |
340 | /* |
341 | * Each relayd socket pair has a refcount of stream attached to it | |
342 | * which tells if the relayd is still active or not depending on the | |
343 | * refcount value. | |
344 | * | |
345 | * This will set the destroy flag of the relayd object and destroy it | |
346 | * if the refcount reaches zero when called. | |
347 | * | |
348 | * The destroy can happen either here or when a stream fd hangs up. | |
349 | */ | |
f50f23d9 DG |
350 | if (relayd) { |
351 | consumer_flag_relayd_for_destroy(relayd); | |
352 | } | |
353 | ||
354 | ret = consumer_send_status_msg(sock, ret_code); | |
355 | if (ret < 0) { | |
356 | /* Somehow, the session daemon is not responding anymore. */ | |
357 | goto end_nosignal; | |
358 | } | |
173af62f | 359 | |
3f8e211f | 360 | goto end_nosignal; |
173af62f | 361 | } |
3bd1e081 MD |
362 | case LTTNG_CONSUMER_UPDATE_STREAM: |
363 | { | |
3f8e211f | 364 | rcu_read_unlock(); |
7ad0a0cb | 365 | return -ENOSYS; |
3bd1e081 | 366 | } |
6d805429 | 367 | case LTTNG_CONSUMER_DATA_PENDING: |
53632229 | 368 | { |
ca22feea | 369 | int32_t ret; |
6d805429 | 370 | uint64_t id = msg.u.data_pending.session_id; |
ca22feea | 371 | |
6d805429 | 372 | DBG("UST consumer data pending command for id %" PRIu64, id); |
ca22feea | 373 | |
6d805429 | 374 | ret = consumer_data_pending(id); |
ca22feea DG |
375 | |
376 | /* Send back returned value to session daemon */ | |
377 | ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret)); | |
378 | if (ret < 0) { | |
6d805429 | 379 | PERROR("send data pending ret code"); |
ca22feea | 380 | } |
f50f23d9 DG |
381 | |
382 | /* | |
383 | * No need to send back a status message since the data pending | |
384 | * returned value is the response. | |
385 | */ | |
ca22feea | 386 | break; |
53632229 | 387 | } |
3bd1e081 MD |
388 | default: |
389 | break; | |
390 | } | |
3f8e211f | 391 | |
3bd1e081 | 392 | end_nosignal: |
b0b335c8 | 393 | rcu_read_unlock(); |
4cbc1a04 DG |
394 | |
395 | /* | |
396 | * Return 1 to indicate success since the 0 value can be a socket | |
397 | * shutdown during the recv() or send() call. | |
398 | */ | |
399 | return 1; | |
3bd1e081 MD |
400 | } |
401 | ||
402 | int lttng_ustconsumer_allocate_channel(struct lttng_consumer_channel *chan) | |
403 | { | |
13161846 | 404 | struct lttng_ust_object_data obj; |
3bd1e081 MD |
405 | |
406 | obj.handle = -1; | |
407 | obj.shm_fd = chan->shm_fd; | |
408 | obj.wait_fd = chan->wait_fd; | |
409 | obj.memory_map_size = chan->mmap_len; | |
410 | chan->handle = ustctl_map_channel(&obj); | |
411 | if (!chan->handle) { | |
412 | return -ENOMEM; | |
413 | } | |
b5c5fc29 | 414 | chan->wait_fd_is_copy = 1; |
2c1dd183 | 415 | chan->shm_fd = -1; |
b5c5fc29 | 416 | |
3bd1e081 MD |
417 | return 0; |
418 | } | |
419 | ||
d056b477 MD |
420 | void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream *stream) |
421 | { | |
422 | ustctl_flush_buffer(stream->chan->handle, stream->buf, 0); | |
effcf122 | 423 | stream->hangup_flush_done = 1; |
d056b477 MD |
424 | } |
425 | ||
3bd1e081 MD |
426 | void lttng_ustconsumer_del_channel(struct lttng_consumer_channel *chan) |
427 | { | |
428 | ustctl_unmap_channel(chan->handle); | |
429 | } | |
430 | ||
e316aad5 | 431 | int lttng_ustconsumer_add_stream(struct lttng_consumer_stream *stream) |
3bd1e081 | 432 | { |
13161846 | 433 | struct lttng_ust_object_data obj; |
3bd1e081 MD |
434 | int ret; |
435 | ||
436 | obj.handle = -1; | |
437 | obj.shm_fd = stream->shm_fd; | |
438 | obj.wait_fd = stream->wait_fd; | |
439 | obj.memory_map_size = stream->mmap_len; | |
440 | ret = ustctl_add_stream(stream->chan->handle, &obj); | |
7a57cf92 DG |
441 | if (ret) { |
442 | ERR("UST ctl add_stream failed with ret %d", ret); | |
e316aad5 | 443 | goto error; |
7a57cf92 DG |
444 | } |
445 | ||
3bd1e081 | 446 | stream->buf = ustctl_open_stream_read(stream->chan->handle, stream->cpu); |
7a57cf92 DG |
447 | if (!stream->buf) { |
448 | ERR("UST ctl open_stream_read failed"); | |
e316aad5 DG |
449 | ret = -EBUSY; |
450 | goto error; | |
7a57cf92 DG |
451 | } |
452 | ||
2c1dd183 MD |
453 | /* ustctl_open_stream_read has closed the shm fd. */ |
454 | stream->wait_fd_is_copy = 1; | |
455 | stream->shm_fd = -1; | |
456 | ||
3bd1e081 MD |
457 | stream->mmap_base = ustctl_get_mmap_base(stream->chan->handle, stream->buf); |
458 | if (!stream->mmap_base) { | |
7a57cf92 | 459 | ERR("UST ctl get_mmap_base failed"); |
e316aad5 DG |
460 | ret = -EINVAL; |
461 | goto mmap_error; | |
3bd1e081 | 462 | } |
ee77a7b0 | 463 | |
3bd1e081 | 464 | return 0; |
e316aad5 DG |
465 | |
466 | mmap_error: | |
467 | ustctl_close_stream_read(stream->chan->handle, stream->buf); | |
468 | error: | |
469 | return ret; | |
3bd1e081 MD |
470 | } |
471 | ||
472 | void lttng_ustconsumer_del_stream(struct lttng_consumer_stream *stream) | |
473 | { | |
474 | ustctl_close_stream_read(stream->chan->handle, stream->buf); | |
475 | } | |
d41f73b7 MD |
476 | |
477 | ||
478 | int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream *stream, | |
479 | struct lttng_consumer_local_data *ctx) | |
480 | { | |
1d4dfdef | 481 | unsigned long len, subbuf_size, padding; |
d41f73b7 MD |
482 | int err; |
483 | long ret = 0; | |
484 | struct lttng_ust_shm_handle *handle; | |
485 | struct lttng_ust_lib_ring_buffer *buf; | |
486 | char dummy; | |
d41f73b7 MD |
487 | |
488 | DBG("In read_subbuffer (wait_fd: %d, stream key: %d)", | |
489 | stream->wait_fd, stream->key); | |
490 | ||
491 | /* We can consume the 1 byte written into the wait_fd by UST */ | |
effcf122 | 492 | if (!stream->hangup_flush_done) { |
c617c0c6 MD |
493 | ssize_t readlen; |
494 | ||
effcf122 MD |
495 | do { |
496 | readlen = read(stream->wait_fd, &dummy, 1); | |
87dc6a9c | 497 | } while (readlen == -1 && errno == EINTR); |
effcf122 MD |
498 | if (readlen == -1) { |
499 | ret = readlen; | |
500 | goto end; | |
501 | } | |
d41f73b7 MD |
502 | } |
503 | ||
504 | buf = stream->buf; | |
505 | handle = stream->chan->handle; | |
506 | /* Get the next subbuffer */ | |
507 | err = ustctl_get_next_subbuf(handle, buf); | |
508 | if (err != 0) { | |
1d4dfdef | 509 | ret = err; /* ustctl_get_next_subbuf returns negative, caller expect positive. */ |
d41f73b7 MD |
510 | /* |
511 | * This is a debug message even for single-threaded consumer, | |
512 | * because poll() have more relaxed criterions than get subbuf, | |
513 | * so get_subbuf may fail for short race windows where poll() | |
514 | * would issue wakeups. | |
515 | */ | |
516 | DBG("Reserving sub buffer failed (everything is normal, " | |
517 | "it is due to concurrency)"); | |
518 | goto end; | |
519 | } | |
520 | assert(stream->output == LTTNG_EVENT_MMAP); | |
1d4dfdef | 521 | /* Get the full padded subbuffer size */ |
d41f73b7 | 522 | err = ustctl_get_padded_subbuf_size(handle, buf, &len); |
effcf122 | 523 | assert(err == 0); |
1d4dfdef DG |
524 | |
525 | /* Get subbuffer data size (without padding) */ | |
526 | err = ustctl_get_subbuf_size(handle, buf, &subbuf_size); | |
527 | assert(err == 0); | |
528 | ||
529 | /* Make sure we don't get a subbuffer size bigger than the padded */ | |
530 | assert(len >= subbuf_size); | |
531 | ||
532 | padding = len - subbuf_size; | |
d41f73b7 | 533 | /* write the subbuffer to the tracefile */ |
1d4dfdef | 534 | ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size, padding); |
91dfef6e DG |
535 | /* |
536 | * The mmap operation should write subbuf_size amount of data when network | |
537 | * streaming or the full padding (len) size when we are _not_ streaming. | |
538 | */ | |
539 | if ((ret != subbuf_size && stream->net_seq_idx != -1) || | |
540 | (ret != len && stream->net_seq_idx == -1)) { | |
d41f73b7 | 541 | /* |
91dfef6e | 542 | * Display the error but continue processing to try to release the |
c5c45efa DG |
543 | * subbuffer. This is a DBG statement since any unexpected kill or |
544 | * signal, the application gets unregistered, relayd gets closed or | |
545 | * anything that affects the buffer lifetime will trigger this error. | |
546 | * So, for the sake of the user, don't print this error since it can | |
547 | * happen and it is OK with the code flow. | |
d41f73b7 | 548 | */ |
c5c45efa | 549 | DBG("Error writing to tracefile " |
91dfef6e DG |
550 | "(ret: %zd != len: %lu != subbuf_size: %lu)", |
551 | ret, len, subbuf_size); | |
d41f73b7 MD |
552 | } |
553 | err = ustctl_put_next_subbuf(handle, buf); | |
effcf122 | 554 | assert(err == 0); |
d41f73b7 MD |
555 | end: |
556 | return ret; | |
557 | } | |
558 | ||
559 | int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream *stream) | |
560 | { | |
561 | int ret; | |
562 | ||
563 | /* Opening the tracefile in write mode */ | |
00e2e675 | 564 | if (stream->path_name != NULL && stream->net_seq_idx == -1) { |
e11d277b | 565 | ret = run_as_open(stream->path_name, |
60b6c79c MD |
566 | O_WRONLY|O_CREAT|O_TRUNC, |
567 | S_IRWXU|S_IRWXG|S_IRWXO, | |
568 | stream->uid, stream->gid); | |
d41f73b7 MD |
569 | if (ret < 0) { |
570 | ERR("Opening %s", stream->path_name); | |
4c462e79 | 571 | PERROR("open"); |
d41f73b7 MD |
572 | goto error; |
573 | } | |
574 | stream->out_fd = ret; | |
575 | } | |
576 | ||
c869f647 DG |
577 | ret = lttng_ustconsumer_add_stream(stream); |
578 | if (ret) { | |
579 | consumer_del_stream(stream, NULL); | |
580 | ret = -1; | |
581 | goto error; | |
582 | } | |
583 | ||
d41f73b7 MD |
584 | /* we return 0 to let the library handle the FD internally */ |
585 | return 0; | |
586 | ||
587 | error: | |
588 | return ret; | |
589 | } | |
ca22feea DG |
590 | |
591 | /* | |
592 | * Check if data is still being extracted from the buffers for a specific | |
4e9a4686 DG |
593 | * stream. Consumer data lock MUST be acquired before calling this function |
594 | * and the stream lock. | |
ca22feea | 595 | * |
6d805429 | 596 | * Return 1 if the traced data are still getting read else 0 meaning that the |
ca22feea DG |
597 | * data is available for trace viewer reading. |
598 | */ | |
6d805429 | 599 | int lttng_ustconsumer_data_pending(struct lttng_consumer_stream *stream) |
ca22feea DG |
600 | { |
601 | int ret; | |
602 | ||
603 | assert(stream); | |
604 | ||
6d805429 | 605 | DBG("UST consumer checking data pending"); |
c8f59ee5 | 606 | |
ca22feea DG |
607 | ret = ustctl_get_next_subbuf(stream->chan->handle, stream->buf); |
608 | if (ret == 0) { | |
609 | /* There is still data so let's put back this subbuffer. */ | |
610 | ret = ustctl_put_subbuf(stream->chan->handle, stream->buf); | |
611 | assert(ret == 0); | |
6d805429 | 612 | ret = 1; /* Data is pending */ |
4e9a4686 | 613 | goto end; |
ca22feea DG |
614 | } |
615 | ||
6d805429 DG |
616 | /* Data is NOT pending so ready to be read. */ |
617 | ret = 0; | |
ca22feea | 618 | |
6efae65e DG |
619 | end: |
620 | return ret; | |
ca22feea | 621 | } |