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, | |
153 | msg.u.channel.max_sb_size); | |
154 | if (new_channel == NULL) { | |
f73fabfd | 155 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR); |
3bd1e081 MD |
156 | goto end_nosignal; |
157 | } | |
158 | if (ctx->on_recv_channel != NULL) { | |
159 | ret = ctx->on_recv_channel(new_channel); | |
160 | if (ret == 0) { | |
161 | consumer_add_channel(new_channel); | |
162 | } else if (ret < 0) { | |
163 | goto end_nosignal; | |
164 | } | |
165 | } else { | |
166 | consumer_add_channel(new_channel); | |
167 | } | |
168 | goto end_nosignal; | |
169 | } | |
170 | case LTTNG_CONSUMER_ADD_STREAM: | |
171 | { | |
172 | struct lttng_consumer_stream *new_stream; | |
173 | int fds[2]; | |
174 | size_t nb_fd = 2; | |
00e2e675 | 175 | struct consumer_relayd_sock_pair *relayd = NULL; |
3bd1e081 | 176 | |
173af62f DG |
177 | DBG("UST Consumer adding stream"); |
178 | ||
3bd1e081 MD |
179 | /* block */ |
180 | if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) { | |
3f8e211f | 181 | rcu_read_unlock(); |
3bd1e081 MD |
182 | return -EINTR; |
183 | } | |
184 | ret = lttcomm_recv_fds_unix_sock(sock, fds, nb_fd); | |
185 | if (ret != sizeof(fds)) { | |
f73fabfd | 186 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD); |
3f8e211f | 187 | rcu_read_unlock(); |
3bd1e081 MD |
188 | return ret; |
189 | } | |
190 | ||
173af62f DG |
191 | DBG("consumer_add_stream chan %d stream %d", |
192 | msg.u.stream.channel_key, | |
193 | msg.u.stream.stream_key); | |
194 | ||
d41f73b7 | 195 | assert(msg.u.stream.output == LTTNG_EVENT_MMAP); |
173af62f | 196 | new_stream = consumer_allocate_stream(msg.u.stream.channel_key, |
3bd1e081 MD |
197 | msg.u.stream.stream_key, |
198 | fds[0], fds[1], | |
199 | msg.u.stream.state, | |
200 | msg.u.stream.mmap_len, | |
201 | msg.u.stream.output, | |
6df2e2c9 MD |
202 | msg.u.stream.path_name, |
203 | msg.u.stream.uid, | |
00e2e675 DG |
204 | msg.u.stream.gid, |
205 | msg.u.stream.net_index, | |
206 | msg.u.stream.metadata_flag); | |
3bd1e081 | 207 | if (new_stream == NULL) { |
f73fabfd | 208 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR); |
3f8e211f | 209 | goto end_nosignal; |
3bd1e081 | 210 | } |
00e2e675 DG |
211 | |
212 | /* The stream is not metadata. Get relayd reference if exists. */ | |
213 | relayd = consumer_find_relayd(msg.u.stream.net_index); | |
214 | if (relayd != NULL) { | |
215 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); | |
216 | /* Add stream on the relayd */ | |
217 | ret = relayd_add_stream(&relayd->control_sock, | |
218 | msg.u.stream.name, msg.u.stream.path_name, | |
219 | &new_stream->relayd_stream_id); | |
220 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); | |
221 | if (ret < 0) { | |
3f8e211f | 222 | goto end_nosignal; |
00e2e675 DG |
223 | } |
224 | } else if (msg.u.stream.net_index != -1) { | |
225 | ERR("Network sequence index %d unknown. Not adding stream.", | |
226 | msg.u.stream.net_index); | |
227 | free(new_stream); | |
3f8e211f | 228 | goto end_nosignal; |
00e2e675 DG |
229 | } |
230 | ||
3bd1e081 MD |
231 | if (ctx->on_recv_stream != NULL) { |
232 | ret = ctx->on_recv_stream(new_stream); | |
233 | if (ret == 0) { | |
234 | consumer_add_stream(new_stream); | |
235 | } else if (ret < 0) { | |
3f8e211f | 236 | goto end_nosignal; |
3bd1e081 MD |
237 | } |
238 | } else { | |
239 | consumer_add_stream(new_stream); | |
240 | } | |
00e2e675 | 241 | |
77c7c900 | 242 | DBG("UST consumer_add_stream %s (%d,%d) with relayd id %" PRIu64, |
00e2e675 DG |
243 | msg.u.stream.path_name, fds[0], fds[1], |
244 | new_stream->relayd_stream_id); | |
3bd1e081 MD |
245 | break; |
246 | } | |
173af62f DG |
247 | case LTTNG_CONSUMER_DESTROY_RELAYD: |
248 | { | |
a6ba4fe1 | 249 | uint64_t index = msg.u.destroy_relayd.net_seq_idx; |
173af62f DG |
250 | struct consumer_relayd_sock_pair *relayd; |
251 | ||
a6ba4fe1 | 252 | DBG("UST consumer destroying relayd %" PRIu64, index); |
173af62f DG |
253 | |
254 | /* Get relayd reference if exists. */ | |
a6ba4fe1 | 255 | relayd = consumer_find_relayd(index); |
173af62f | 256 | if (relayd == NULL) { |
a6ba4fe1 | 257 | ERR("Unable to find relayd %" PRIu64, index); |
3f8e211f | 258 | goto end_nosignal; |
173af62f DG |
259 | } |
260 | ||
a6ba4fe1 DG |
261 | /* |
262 | * Each relayd socket pair has a refcount of stream attached to it | |
263 | * which tells if the relayd is still active or not depending on the | |
264 | * refcount value. | |
265 | * | |
266 | * This will set the destroy flag of the relayd object and destroy it | |
267 | * if the refcount reaches zero when called. | |
268 | * | |
269 | * The destroy can happen either here or when a stream fd hangs up. | |
270 | */ | |
271 | consumer_flag_relayd_for_destroy(relayd); | |
173af62f | 272 | |
3f8e211f | 273 | goto end_nosignal; |
173af62f | 274 | } |
3bd1e081 MD |
275 | case LTTNG_CONSUMER_UPDATE_STREAM: |
276 | { | |
3f8e211f | 277 | rcu_read_unlock(); |
7ad0a0cb MD |
278 | return -ENOSYS; |
279 | #if 0 | |
3bd1e081 MD |
280 | if (ctx->on_update_stream != NULL) { |
281 | ret = ctx->on_update_stream(msg.u.stream.stream_key, msg.u.stream.state); | |
282 | if (ret == 0) { | |
283 | consumer_change_stream_state(msg.u.stream.stream_key, msg.u.stream.state); | |
284 | } else if (ret < 0) { | |
285 | goto end; | |
286 | } | |
287 | } else { | |
288 | consumer_change_stream_state(msg.u.stream.stream_key, | |
289 | msg.u.stream.state); | |
290 | } | |
291 | break; | |
3f8e211f | 292 | #endif |
3bd1e081 MD |
293 | } |
294 | default: | |
295 | break; | |
296 | } | |
3f8e211f | 297 | |
04fdd819 | 298 | /* |
3f8e211f DG |
299 | * Wake-up the other end by writing a null byte in the pipe (non-blocking). |
300 | * Important note: Because writing into the pipe is non-blocking (and | |
301 | * therefore we allow dropping wakeup data, as long as there is wakeup data | |
302 | * present in the pipe buffer to wake up the other end), the other end | |
303 | * should perform the following sequence for waiting: | |
304 | * | |
04fdd819 MD |
305 | * 1) empty the pipe (reads). |
306 | * 2) perform update operation. | |
307 | * 3) wait on the pipe (poll). | |
308 | */ | |
309 | do { | |
310 | ret = write(ctx->consumer_poll_pipe[1], "", 1); | |
6f94560a | 311 | } while (ret < 0 && errno == EINTR); |
3bd1e081 | 312 | end_nosignal: |
b0b335c8 | 313 | rcu_read_unlock(); |
4cbc1a04 DG |
314 | |
315 | /* | |
316 | * Return 1 to indicate success since the 0 value can be a socket | |
317 | * shutdown during the recv() or send() call. | |
318 | */ | |
319 | return 1; | |
3bd1e081 MD |
320 | } |
321 | ||
322 | int lttng_ustconsumer_allocate_channel(struct lttng_consumer_channel *chan) | |
323 | { | |
13161846 | 324 | struct lttng_ust_object_data obj; |
3bd1e081 MD |
325 | |
326 | obj.handle = -1; | |
327 | obj.shm_fd = chan->shm_fd; | |
328 | obj.wait_fd = chan->wait_fd; | |
329 | obj.memory_map_size = chan->mmap_len; | |
330 | chan->handle = ustctl_map_channel(&obj); | |
331 | if (!chan->handle) { | |
332 | return -ENOMEM; | |
333 | } | |
b5c5fc29 | 334 | chan->wait_fd_is_copy = 1; |
2c1dd183 | 335 | chan->shm_fd = -1; |
b5c5fc29 | 336 | |
3bd1e081 MD |
337 | return 0; |
338 | } | |
339 | ||
d056b477 MD |
340 | void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream *stream) |
341 | { | |
342 | ustctl_flush_buffer(stream->chan->handle, stream->buf, 0); | |
effcf122 | 343 | stream->hangup_flush_done = 1; |
d056b477 MD |
344 | } |
345 | ||
3bd1e081 MD |
346 | void lttng_ustconsumer_del_channel(struct lttng_consumer_channel *chan) |
347 | { | |
348 | ustctl_unmap_channel(chan->handle); | |
349 | } | |
350 | ||
351 | int lttng_ustconsumer_allocate_stream(struct lttng_consumer_stream *stream) | |
352 | { | |
13161846 | 353 | struct lttng_ust_object_data obj; |
3bd1e081 MD |
354 | int ret; |
355 | ||
356 | obj.handle = -1; | |
357 | obj.shm_fd = stream->shm_fd; | |
358 | obj.wait_fd = stream->wait_fd; | |
359 | obj.memory_map_size = stream->mmap_len; | |
360 | ret = ustctl_add_stream(stream->chan->handle, &obj); | |
361 | if (ret) | |
362 | return ret; | |
363 | stream->buf = ustctl_open_stream_read(stream->chan->handle, stream->cpu); | |
364 | if (!stream->buf) | |
365 | return -EBUSY; | |
2c1dd183 MD |
366 | /* ustctl_open_stream_read has closed the shm fd. */ |
367 | stream->wait_fd_is_copy = 1; | |
368 | stream->shm_fd = -1; | |
369 | ||
3bd1e081 MD |
370 | stream->mmap_base = ustctl_get_mmap_base(stream->chan->handle, stream->buf); |
371 | if (!stream->mmap_base) { | |
372 | return -EINVAL; | |
373 | } | |
ee77a7b0 | 374 | |
3bd1e081 MD |
375 | return 0; |
376 | } | |
377 | ||
378 | void lttng_ustconsumer_del_stream(struct lttng_consumer_stream *stream) | |
379 | { | |
380 | ustctl_close_stream_read(stream->chan->handle, stream->buf); | |
381 | } | |
d41f73b7 MD |
382 | |
383 | ||
384 | int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream *stream, | |
385 | struct lttng_consumer_local_data *ctx) | |
386 | { | |
387 | unsigned long len; | |
388 | int err; | |
389 | long ret = 0; | |
390 | struct lttng_ust_shm_handle *handle; | |
391 | struct lttng_ust_lib_ring_buffer *buf; | |
392 | char dummy; | |
393 | ssize_t readlen; | |
394 | ||
395 | DBG("In read_subbuffer (wait_fd: %d, stream key: %d)", | |
396 | stream->wait_fd, stream->key); | |
397 | ||
398 | /* We can consume the 1 byte written into the wait_fd by UST */ | |
effcf122 MD |
399 | if (!stream->hangup_flush_done) { |
400 | do { | |
401 | readlen = read(stream->wait_fd, &dummy, 1); | |
87dc6a9c | 402 | } while (readlen == -1 && errno == EINTR); |
effcf122 MD |
403 | if (readlen == -1) { |
404 | ret = readlen; | |
405 | goto end; | |
406 | } | |
d41f73b7 MD |
407 | } |
408 | ||
409 | buf = stream->buf; | |
410 | handle = stream->chan->handle; | |
411 | /* Get the next subbuffer */ | |
412 | err = ustctl_get_next_subbuf(handle, buf); | |
413 | if (err != 0) { | |
effcf122 | 414 | ret = -ret; /* ustctl_get_next_subbuf returns negative, caller expect positive. */ |
d41f73b7 MD |
415 | /* |
416 | * This is a debug message even for single-threaded consumer, | |
417 | * because poll() have more relaxed criterions than get subbuf, | |
418 | * so get_subbuf may fail for short race windows where poll() | |
419 | * would issue wakeups. | |
420 | */ | |
421 | DBG("Reserving sub buffer failed (everything is normal, " | |
422 | "it is due to concurrency)"); | |
423 | goto end; | |
424 | } | |
425 | assert(stream->output == LTTNG_EVENT_MMAP); | |
426 | /* read the used subbuffer size */ | |
427 | err = ustctl_get_padded_subbuf_size(handle, buf, &len); | |
effcf122 | 428 | assert(err == 0); |
d41f73b7 MD |
429 | /* write the subbuffer to the tracefile */ |
430 | ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, len); | |
47e81c02 | 431 | if (ret != len) { |
d41f73b7 MD |
432 | /* |
433 | * display the error but continue processing to try | |
434 | * to release the subbuffer | |
435 | */ | |
a4b92340 | 436 | ERR("Error writing to tracefile (expected: %ld, got: %ld)", ret, len); |
d41f73b7 MD |
437 | } |
438 | err = ustctl_put_next_subbuf(handle, buf); | |
effcf122 | 439 | assert(err == 0); |
d41f73b7 MD |
440 | end: |
441 | return ret; | |
442 | } | |
443 | ||
444 | int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream *stream) | |
445 | { | |
446 | int ret; | |
447 | ||
448 | /* Opening the tracefile in write mode */ | |
00e2e675 | 449 | if (stream->path_name != NULL && stream->net_seq_idx == -1) { |
e11d277b | 450 | ret = run_as_open(stream->path_name, |
60b6c79c MD |
451 | O_WRONLY|O_CREAT|O_TRUNC, |
452 | S_IRWXU|S_IRWXG|S_IRWXO, | |
453 | stream->uid, stream->gid); | |
d41f73b7 MD |
454 | if (ret < 0) { |
455 | ERR("Opening %s", stream->path_name); | |
4c462e79 | 456 | PERROR("open"); |
d41f73b7 MD |
457 | goto error; |
458 | } | |
459 | stream->out_fd = ret; | |
460 | } | |
461 | ||
462 | /* we return 0 to let the library handle the FD internally */ | |
463 | return 0; | |
464 | ||
465 | error: | |
466 | return ret; | |
467 | } |