Cleanup: symbols in parser protected -> hidden
[lttng-tools.git] / src / common / ust-consumer / ust-consumer.c
CommitLineData
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
MD
29#include <sys/types.h>
30#include <unistd.h>
0857097f 31
990570ed 32#include <common/common.h>
10a8a223 33#include <common/sessiond-comm/sessiond-comm.h>
00e2e675 34#include <common/relayd/relayd.h>
dbb5dfe6 35#include <common/compat/fcntl.h>
10a8a223
DG
36
37#include "ust-consumer.h"
3bd1e081
MD
38
39extern struct lttng_consumer_global_data consumer_data;
40extern int consumer_poll_timeout;
41extern volatile int consumer_quit;
42
43/*
f02e1e8a
DG
44 * Wrapper over the mmap() read offset from ust-ctl library. Since this can be
45 * compiled out, we isolate it in this library.
3bd1e081 46 */
f02e1e8a
DG
47int lttng_ustctl_get_mmap_read_offset(struct lttng_ust_shm_handle *handle,
48 struct lttng_ust_lib_ring_buffer *buf, unsigned long *off)
3bd1e081 49{
f02e1e8a
DG
50 return ustctl_get_mmap_read_offset(handle, buf, off);
51};
3bd1e081
MD
52
53/*
54 * Take a snapshot for a specific fd
55 *
56 * Returns 0 on success, < 0 on error
57 */
58int lttng_ustconsumer_take_snapshot(struct lttng_consumer_local_data *ctx,
59 struct lttng_consumer_stream *stream)
60{
61 int ret = 0;
62
63 ret = ustctl_snapshot(stream->chan->handle, stream->buf);
64 if (ret != 0) {
87dc6a9c 65 errno = -ret;
4c462e79 66 PERROR("Getting sub-buffer snapshot.");
3bd1e081
MD
67 }
68
69 return ret;
70}
71
72/*
73 * Get the produced position
74 *
75 * Returns 0 on success, < 0 on error
76 */
77int lttng_ustconsumer_get_produced_snapshot(
78 struct lttng_consumer_local_data *ctx,
79 struct lttng_consumer_stream *stream,
80 unsigned long *pos)
81{
82 int ret;
83
84 ret = ustctl_snapshot_get_produced(stream->chan->handle,
85 stream->buf, pos);
86 if (ret != 0) {
87dc6a9c 87 errno = -ret;
4c462e79 88 PERROR("kernctl_snapshot_get_produced");
3bd1e081
MD
89 }
90
91 return ret;
92}
93
94int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
95 int sock, struct pollfd *consumer_sockpoll)
96{
97 ssize_t ret;
98 struct lttcomm_consumer_msg msg;
99
100 ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
101 if (ret != sizeof(msg)) {
173af62f
DG
102 DBG("Consumer received unexpected message size %zd (expects %zu)",
103 ret, sizeof(msg));
3bd1e081
MD
104 lttng_consumer_send_error(ctx, CONSUMERD_ERROR_RECV_FD);
105 return ret;
106 }
107 if (msg.cmd_type == LTTNG_CONSUMER_STOP) {
108 return -ENOENT;
109 }
110
3f8e211f 111 /* relayd needs RCU read-side lock */
b0b335c8
MD
112 rcu_read_lock();
113
3bd1e081 114 switch (msg.cmd_type) {
00e2e675
DG
115 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
116 {
117 int fd;
118 struct consumer_relayd_sock_pair *relayd;
119
120 DBG("UST Consumer adding relayd socket");
121
122 /* Get relayd reference if exists. */
123 relayd = consumer_find_relayd(msg.u.relayd_sock.net_index);
124 if (relayd == NULL) {
125 /* Not found. Allocate one. */
126 relayd = consumer_allocate_relayd_sock_pair(
127 msg.u.relayd_sock.net_index);
128 if (relayd == NULL) {
129 lttng_consumer_send_error(ctx, CONSUMERD_OUTFD_ERROR);
130 goto end_nosignal;
131 }
132 }
133
134 /* Poll on consumer socket. */
135 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
3f8e211f 136 rcu_read_unlock();
00e2e675
DG
137 return -EINTR;
138 }
139
140 /* Get relayd socket from session daemon */
141 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
142 if (ret != sizeof(fd)) {
143 lttng_consumer_send_error(ctx, CONSUMERD_ERROR_RECV_FD);
144 goto end_nosignal;
145 }
146
147 /* Copy socket information and received FD */
148 switch (msg.u.relayd_sock.type) {
149 case LTTNG_STREAM_CONTROL:
150 /* Copy received lttcomm socket */
151 lttcomm_copy_sock(&relayd->control_sock, &msg.u.relayd_sock.sock);
152 ret = lttcomm_create_sock(&relayd->control_sock);
153 if (ret < 0) {
154 goto end_nosignal;
155 }
156
157 /* Close the created socket fd which is useless */
158 close(relayd->control_sock.fd);
159
160 /* Assign new file descriptor */
161 relayd->control_sock.fd = fd;
162 break;
163 case LTTNG_STREAM_DATA:
164 /* Copy received lttcomm socket */
165 lttcomm_copy_sock(&relayd->data_sock, &msg.u.relayd_sock.sock);
166 ret = lttcomm_create_sock(&relayd->data_sock);
167 if (ret < 0) {
168 goto end_nosignal;
169 }
170
171 /* Close the created socket fd which is useless */
172 close(relayd->data_sock.fd);
173
174 /* Assign new file descriptor */
175 relayd->data_sock.fd = fd;
176 break;
177 default:
178 ERR("Unknown relayd socket type");
179 goto end_nosignal;
180 }
181
182 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
183 msg.u.relayd_sock.type == LTTNG_STREAM_CONTROL ? "control" : "data",
184 relayd->net_seq_idx, fd);
185
186 /*
187 * Add relayd socket pair to consumer data hashtable. If object already
188 * exists or on error, the function gracefully returns.
189 */
190 consumer_add_relayd(relayd);
191
192 goto end_nosignal;
193 }
3bd1e081
MD
194 case LTTNG_CONSUMER_ADD_CHANNEL:
195 {
196 struct lttng_consumer_channel *new_channel;
197 int fds[1];
198 size_t nb_fd = 1;
199
173af62f
DG
200 DBG("UST Consumer adding channel");
201
3bd1e081
MD
202 /* block */
203 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
3f8e211f 204 rcu_read_unlock();
3bd1e081
MD
205 return -EINTR;
206 }
207 ret = lttcomm_recv_fds_unix_sock(sock, fds, nb_fd);
208 if (ret != sizeof(fds)) {
209 lttng_consumer_send_error(ctx, CONSUMERD_ERROR_RECV_FD);
3f8e211f 210 rcu_read_unlock();
3bd1e081
MD
211 return ret;
212 }
213
214 DBG("consumer_add_channel %d", msg.u.channel.channel_key);
215
216 new_channel = consumer_allocate_channel(msg.u.channel.channel_key,
217 fds[0], -1,
218 msg.u.channel.mmap_len,
219 msg.u.channel.max_sb_size);
220 if (new_channel == NULL) {
221 lttng_consumer_send_error(ctx, CONSUMERD_OUTFD_ERROR);
222 goto end_nosignal;
223 }
224 if (ctx->on_recv_channel != NULL) {
225 ret = ctx->on_recv_channel(new_channel);
226 if (ret == 0) {
227 consumer_add_channel(new_channel);
228 } else if (ret < 0) {
229 goto end_nosignal;
230 }
231 } else {
232 consumer_add_channel(new_channel);
233 }
234 goto end_nosignal;
235 }
236 case LTTNG_CONSUMER_ADD_STREAM:
237 {
238 struct lttng_consumer_stream *new_stream;
239 int fds[2];
240 size_t nb_fd = 2;
00e2e675 241 struct consumer_relayd_sock_pair *relayd = NULL;
3bd1e081 242
173af62f
DG
243 DBG("UST Consumer adding stream");
244
3bd1e081
MD
245 /* block */
246 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
3f8e211f 247 rcu_read_unlock();
3bd1e081
MD
248 return -EINTR;
249 }
250 ret = lttcomm_recv_fds_unix_sock(sock, fds, nb_fd);
251 if (ret != sizeof(fds)) {
252 lttng_consumer_send_error(ctx, CONSUMERD_ERROR_RECV_FD);
3f8e211f 253 rcu_read_unlock();
3bd1e081
MD
254 return ret;
255 }
256
173af62f
DG
257 DBG("consumer_add_stream chan %d stream %d",
258 msg.u.stream.channel_key,
259 msg.u.stream.stream_key);
260
d41f73b7 261 assert(msg.u.stream.output == LTTNG_EVENT_MMAP);
173af62f 262 new_stream = consumer_allocate_stream(msg.u.stream.channel_key,
3bd1e081
MD
263 msg.u.stream.stream_key,
264 fds[0], fds[1],
265 msg.u.stream.state,
266 msg.u.stream.mmap_len,
267 msg.u.stream.output,
6df2e2c9
MD
268 msg.u.stream.path_name,
269 msg.u.stream.uid,
00e2e675
DG
270 msg.u.stream.gid,
271 msg.u.stream.net_index,
272 msg.u.stream.metadata_flag);
3bd1e081
MD
273 if (new_stream == NULL) {
274 lttng_consumer_send_error(ctx, CONSUMERD_OUTFD_ERROR);
3f8e211f 275 goto end_nosignal;
3bd1e081 276 }
00e2e675
DG
277
278 /* The stream is not metadata. Get relayd reference if exists. */
279 relayd = consumer_find_relayd(msg.u.stream.net_index);
280 if (relayd != NULL) {
281 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
282 /* Add stream on the relayd */
283 ret = relayd_add_stream(&relayd->control_sock,
284 msg.u.stream.name, msg.u.stream.path_name,
285 &new_stream->relayd_stream_id);
286 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
287 if (ret < 0) {
3f8e211f 288 goto end_nosignal;
00e2e675
DG
289 }
290 } else if (msg.u.stream.net_index != -1) {
291 ERR("Network sequence index %d unknown. Not adding stream.",
292 msg.u.stream.net_index);
293 free(new_stream);
3f8e211f 294 goto end_nosignal;
00e2e675
DG
295 }
296
3bd1e081
MD
297 if (ctx->on_recv_stream != NULL) {
298 ret = ctx->on_recv_stream(new_stream);
299 if (ret == 0) {
300 consumer_add_stream(new_stream);
301 } else if (ret < 0) {
3f8e211f 302 goto end_nosignal;
3bd1e081
MD
303 }
304 } else {
305 consumer_add_stream(new_stream);
306 }
00e2e675
DG
307
308 DBG("UST consumer_add_stream %s (%d,%d) with relayd id %lu",
309 msg.u.stream.path_name, fds[0], fds[1],
310 new_stream->relayd_stream_id);
3bd1e081
MD
311 break;
312 }
173af62f
DG
313 case LTTNG_CONSUMER_DESTROY_RELAYD:
314 {
315 struct consumer_relayd_sock_pair *relayd;
316
317 DBG("UST consumer destroying relayd %zu",
318 msg.u.destroy_relayd.net_seq_idx);
319
320 /* Get relayd reference if exists. */
321 relayd = consumer_find_relayd(msg.u.destroy_relayd.net_seq_idx);
322 if (relayd == NULL) {
3f8e211f
DG
323 ERR("Unable to find relayd %zu", msg.u.destroy_relayd.net_seq_idx);
324 goto end_nosignal;
173af62f
DG
325 }
326
327 /* Set destroy flag for this object */
328 uatomic_set(&relayd->destroy_flag, 1);
329
330 /* Destroy the relayd if refcount is 0 else set the destroy flag. */
331 if (uatomic_read(&relayd->refcount) == 0) {
332 consumer_destroy_relayd(relayd);
333 }
3f8e211f 334 goto end_nosignal;
173af62f 335 }
3bd1e081
MD
336 case LTTNG_CONSUMER_UPDATE_STREAM:
337 {
3f8e211f 338 rcu_read_unlock();
7ad0a0cb
MD
339 return -ENOSYS;
340#if 0
3bd1e081
MD
341 if (ctx->on_update_stream != NULL) {
342 ret = ctx->on_update_stream(msg.u.stream.stream_key, msg.u.stream.state);
343 if (ret == 0) {
344 consumer_change_stream_state(msg.u.stream.stream_key, msg.u.stream.state);
345 } else if (ret < 0) {
346 goto end;
347 }
348 } else {
349 consumer_change_stream_state(msg.u.stream.stream_key,
350 msg.u.stream.state);
351 }
352 break;
3f8e211f 353#endif
3bd1e081
MD
354 }
355 default:
356 break;
357 }
3f8e211f 358
04fdd819 359 /*
3f8e211f
DG
360 * Wake-up the other end by writing a null byte in the pipe (non-blocking).
361 * Important note: Because writing into the pipe is non-blocking (and
362 * therefore we allow dropping wakeup data, as long as there is wakeup data
363 * present in the pipe buffer to wake up the other end), the other end
364 * should perform the following sequence for waiting:
365 *
04fdd819
MD
366 * 1) empty the pipe (reads).
367 * 2) perform update operation.
368 * 3) wait on the pipe (poll).
369 */
370 do {
371 ret = write(ctx->consumer_poll_pipe[1], "", 1);
6f94560a 372 } while (ret < 0 && errno == EINTR);
3bd1e081 373end_nosignal:
b0b335c8 374 rcu_read_unlock();
3bd1e081
MD
375 return 0;
376}
377
378int lttng_ustconsumer_allocate_channel(struct lttng_consumer_channel *chan)
379{
13161846 380 struct lttng_ust_object_data obj;
3bd1e081
MD
381
382 obj.handle = -1;
383 obj.shm_fd = chan->shm_fd;
384 obj.wait_fd = chan->wait_fd;
385 obj.memory_map_size = chan->mmap_len;
386 chan->handle = ustctl_map_channel(&obj);
387 if (!chan->handle) {
388 return -ENOMEM;
389 }
b5c5fc29 390 chan->wait_fd_is_copy = 1;
2c1dd183 391 chan->shm_fd = -1;
b5c5fc29 392
3bd1e081
MD
393 return 0;
394}
395
d056b477
MD
396void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream *stream)
397{
398 ustctl_flush_buffer(stream->chan->handle, stream->buf, 0);
effcf122 399 stream->hangup_flush_done = 1;
d056b477
MD
400}
401
3bd1e081
MD
402void lttng_ustconsumer_del_channel(struct lttng_consumer_channel *chan)
403{
404 ustctl_unmap_channel(chan->handle);
405}
406
407int lttng_ustconsumer_allocate_stream(struct lttng_consumer_stream *stream)
408{
13161846 409 struct lttng_ust_object_data obj;
3bd1e081
MD
410 int ret;
411
412 obj.handle = -1;
413 obj.shm_fd = stream->shm_fd;
414 obj.wait_fd = stream->wait_fd;
415 obj.memory_map_size = stream->mmap_len;
416 ret = ustctl_add_stream(stream->chan->handle, &obj);
417 if (ret)
418 return ret;
419 stream->buf = ustctl_open_stream_read(stream->chan->handle, stream->cpu);
420 if (!stream->buf)
421 return -EBUSY;
2c1dd183
MD
422 /* ustctl_open_stream_read has closed the shm fd. */
423 stream->wait_fd_is_copy = 1;
424 stream->shm_fd = -1;
425
3bd1e081
MD
426 stream->mmap_base = ustctl_get_mmap_base(stream->chan->handle, stream->buf);
427 if (!stream->mmap_base) {
428 return -EINVAL;
429 }
ee77a7b0 430
3bd1e081
MD
431 return 0;
432}
433
434void lttng_ustconsumer_del_stream(struct lttng_consumer_stream *stream)
435{
436 ustctl_close_stream_read(stream->chan->handle, stream->buf);
437}
d41f73b7
MD
438
439
440int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
441 struct lttng_consumer_local_data *ctx)
442{
443 unsigned long len;
444 int err;
445 long ret = 0;
446 struct lttng_ust_shm_handle *handle;
447 struct lttng_ust_lib_ring_buffer *buf;
448 char dummy;
449 ssize_t readlen;
450
451 DBG("In read_subbuffer (wait_fd: %d, stream key: %d)",
452 stream->wait_fd, stream->key);
453
454 /* We can consume the 1 byte written into the wait_fd by UST */
effcf122
MD
455 if (!stream->hangup_flush_done) {
456 do {
457 readlen = read(stream->wait_fd, &dummy, 1);
87dc6a9c 458 } while (readlen == -1 && errno == EINTR);
effcf122
MD
459 if (readlen == -1) {
460 ret = readlen;
461 goto end;
462 }
d41f73b7
MD
463 }
464
465 buf = stream->buf;
466 handle = stream->chan->handle;
467 /* Get the next subbuffer */
468 err = ustctl_get_next_subbuf(handle, buf);
469 if (err != 0) {
effcf122 470 ret = -ret; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
d41f73b7
MD
471 /*
472 * This is a debug message even for single-threaded consumer,
473 * because poll() have more relaxed criterions than get subbuf,
474 * so get_subbuf may fail for short race windows where poll()
475 * would issue wakeups.
476 */
477 DBG("Reserving sub buffer failed (everything is normal, "
478 "it is due to concurrency)");
479 goto end;
480 }
481 assert(stream->output == LTTNG_EVENT_MMAP);
482 /* read the used subbuffer size */
483 err = ustctl_get_padded_subbuf_size(handle, buf, &len);
effcf122 484 assert(err == 0);
d41f73b7
MD
485 /* write the subbuffer to the tracefile */
486 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, len);
47e81c02 487 if (ret != len) {
d41f73b7
MD
488 /*
489 * display the error but continue processing to try
490 * to release the subbuffer
491 */
a4b92340 492 ERR("Error writing to tracefile (expected: %ld, got: %ld)", ret, len);
d41f73b7
MD
493 }
494 err = ustctl_put_next_subbuf(handle, buf);
effcf122 495 assert(err == 0);
d41f73b7
MD
496end:
497 return ret;
498}
499
500int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
501{
502 int ret;
503
504 /* Opening the tracefile in write mode */
00e2e675 505 if (stream->path_name != NULL && stream->net_seq_idx == -1) {
e11d277b 506 ret = run_as_open(stream->path_name,
60b6c79c
MD
507 O_WRONLY|O_CREAT|O_TRUNC,
508 S_IRWXU|S_IRWXG|S_IRWXO,
509 stream->uid, stream->gid);
d41f73b7
MD
510 if (ret < 0) {
511 ERR("Opening %s", stream->path_name);
4c462e79 512 PERROR("open");
d41f73b7
MD
513 goto error;
514 }
515 stream->out_fd = ret;
516 }
517
518 /* we return 0 to let the library handle the FD internally */
519 return 0;
520
521error:
522 return ret;
523}
This page took 0.055692 seconds and 4 git commands to generate.