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