Only flush when there are readers active
[lttng-ust.git] / liblttng-ust-ctl / ustctl.c
CommitLineData
57773204
MD
1/*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
74d81a6c 3 * Copyright (C) 2011-2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
57773204 4 *
e92f3e28
MD
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License only.
57773204
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 *
e92f3e28
MD
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.
57773204
MD
17 */
18
9d335227 19#define _GNU_SOURCE
57773204 20#include <string.h>
4318ae1b
MD
21#include <lttng/ust-ctl.h>
22#include <lttng/ust-abi.h>
c1fca457 23#include <lttng/ust-events.h>
7a784989 24#include <sys/mman.h>
32ce8569 25#include <byteswap.h>
44c72f10
MD
26
27#include <usterr-signal-safe.h>
b728d87e 28#include <ust-comm.h>
74d81a6c 29#include <helper.h>
57773204
MD
30
31#include "../libringbuffer/backend.h"
32#include "../libringbuffer/frontend.h"
c9023c93
MD
33#include "../liblttng-ust/wait.h"
34
35/*
36 * Number of milliseconds to retry before failing metadata writes on
37 * buffer full condition. (10 seconds)
38 */
39#define LTTNG_METADATA_TIMEOUT_MSEC 10000
57773204 40
74d81a6c
MD
41/*
42 * Channel representation within consumer.
43 */
44struct ustctl_consumer_channel {
45 struct lttng_channel *chan; /* lttng channel buffers */
6b120308 46
74d81a6c
MD
47 /* initial attributes */
48 struct ustctl_consumer_channel_attr attr;
ff0f5728
MD
49 int wait_fd; /* monitor close() */
50 int wakeup_fd; /* monitor close() */
74d81a6c
MD
51};
52
53/*
54 * Stream representation within consumer.
55 */
56struct ustctl_consumer_stream {
57 struct lttng_ust_shm_handle *handle; /* shared-memory handle */
58 struct lttng_ust_lib_ring_buffer *buf;
59 struct ustctl_consumer_channel *chan;
60 int shm_fd, wait_fd, wakeup_fd;
61 int cpu;
62 uint64_t memory_map_size;
63};
64
65extern void lttng_ring_buffer_client_overwrite_init(void);
66extern void lttng_ring_buffer_client_discard_init(void);
67extern void lttng_ring_buffer_metadata_client_init(void);
68extern void lttng_ring_buffer_client_overwrite_exit(void);
69extern void lttng_ring_buffer_client_discard_exit(void);
70extern void lttng_ring_buffer_metadata_client_exit(void);
71
72volatile enum ust_loglevel ust_loglevel;
57773204 73
2be0e72c
MD
74int ustctl_release_handle(int sock, int handle)
75{
76 struct ustcomm_ust_msg lum;
77 struct ustcomm_ust_reply lur;
2be0e72c 78
74d81a6c
MD
79 if (sock < 0 || handle < 0)
80 return 0;
81 memset(&lum, 0, sizeof(lum));
82 lum.handle = handle;
83 lum.cmd = LTTNG_UST_RELEASE;
84 return ustcomm_send_app_cmd(sock, &lum, &lur);
2be0e72c 85}
74d81a6c 86
12388166
MD
87/*
88 * If sock is negative, it means we don't have to notify the other side
89 * (e.g. application has already vanished).
90 */
d26228ae 91int ustctl_release_object(int sock, struct lttng_ust_object_data *data)
57773204 92{
57773204
MD
93 int ret;
94
9bfc503d
MD
95 if (!data)
96 return -EINVAL;
97
74d81a6c
MD
98 switch (data->type) {
99 case LTTNG_UST_OBJECT_TYPE_CHANNEL:
ff0f5728
MD
100 if (data->u.channel.wakeup_fd >= 0) {
101 ret = close(data->u.channel.wakeup_fd);
102 if (ret < 0) {
103 ret = -errno;
104 return ret;
105 }
106 }
74d81a6c
MD
107 free(data->u.channel.data);
108 break;
109 case LTTNG_UST_OBJECT_TYPE_STREAM:
110 if (data->u.stream.shm_fd >= 0) {
111 ret = close(data->u.stream.shm_fd);
112 if (ret < 0) {
113 ret = -errno;
114 return ret;
115 }
d26228ae 116 }
74d81a6c
MD
117 if (data->u.stream.wakeup_fd >= 0) {
118 ret = close(data->u.stream.wakeup_fd);
119 if (ret < 0) {
120 ret = -errno;
121 return ret;
122 }
d26228ae 123 }
74d81a6c 124 break;
32ce8569
MD
125 case LTTNG_UST_OBJECT_TYPE_EVENT:
126 case LTTNG_UST_OBJECT_TYPE_CONTEXT:
127 break;
74d81a6c
MD
128 default:
129 assert(0);
d26228ae 130 }
2be0e72c 131 return ustctl_release_handle(sock, data->handle);
57773204
MD
132}
133
1c5e467e
MD
134/*
135 * Send registration done packet to the application.
136 */
137int ustctl_register_done(int sock)
138{
139 struct ustcomm_ust_msg lum;
140 struct ustcomm_ust_reply lur;
141 int ret;
142
143 DBG("Sending register done command to %d", sock);
144 memset(&lum, 0, sizeof(lum));
145 lum.handle = LTTNG_UST_ROOT_HANDLE;
146 lum.cmd = LTTNG_UST_REGISTER_DONE;
147 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
148 if (ret)
149 return ret;
1c5e467e 150 return 0;
1c5e467e
MD
151}
152
57773204
MD
153/*
154 * returns session handle.
155 */
156int ustctl_create_session(int sock)
157{
158 struct ustcomm_ust_msg lum;
159 struct ustcomm_ust_reply lur;
160 int ret, session_handle;
161
162 /* Create session */
163 memset(&lum, 0, sizeof(lum));
164 lum.handle = LTTNG_UST_ROOT_HANDLE;
165 lum.cmd = LTTNG_UST_SESSION;
166 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
167 if (ret)
168 return ret;
169 session_handle = lur.ret_val;
170 DBG("received session handle %u", session_handle);
171 return session_handle;
172}
173
57773204 174int ustctl_create_event(int sock, struct lttng_ust_event *ev,
61f02aea
MD
175 struct lttng_ust_object_data *channel_data,
176 struct lttng_ust_object_data **_event_data)
57773204
MD
177{
178 struct ustcomm_ust_msg lum;
179 struct ustcomm_ust_reply lur;
61f02aea 180 struct lttng_ust_object_data *event_data;
57773204
MD
181 int ret;
182
9bfc503d
MD
183 if (!channel_data || !_event_data)
184 return -EINVAL;
185
74d81a6c 186 event_data = zmalloc(sizeof(*event_data));
57773204
MD
187 if (!event_data)
188 return -ENOMEM;
32ce8569 189 event_data->type = LTTNG_UST_OBJECT_TYPE_EVENT;
57773204
MD
190 memset(&lum, 0, sizeof(lum));
191 lum.handle = channel_data->handle;
192 lum.cmd = LTTNG_UST_EVENT;
193 strncpy(lum.u.event.name, ev->name,
194 LTTNG_UST_SYM_NAME_LEN);
195 lum.u.event.instrumentation = ev->instrumentation;
457a6b58
MD
196 lum.u.event.loglevel_type = ev->loglevel_type;
197 lum.u.event.loglevel = ev->loglevel;
57773204
MD
198 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
199 if (ret) {
200 free(event_data);
201 return ret;
202 }
203 event_data->handle = lur.ret_val;
204 DBG("received event handle %u", event_data->handle);
205 *_event_data = event_data;
206 return 0;
207}
208
209int ustctl_add_context(int sock, struct lttng_ust_context *ctx,
61f02aea
MD
210 struct lttng_ust_object_data *obj_data,
211 struct lttng_ust_object_data **_context_data)
57773204
MD
212{
213 struct ustcomm_ust_msg lum;
214 struct ustcomm_ust_reply lur;
61f02aea 215 struct lttng_ust_object_data *context_data;
57773204
MD
216 int ret;
217
9bfc503d
MD
218 if (!obj_data || !_context_data)
219 return -EINVAL;
220
74d81a6c 221 context_data = zmalloc(sizeof(*context_data));
57773204
MD
222 if (!context_data)
223 return -ENOMEM;
32ce8569 224 context_data->type = LTTNG_UST_OBJECT_TYPE_CONTEXT;
57773204 225 memset(&lum, 0, sizeof(lum));
3039d8ed 226 lum.handle = obj_data->handle;
57773204
MD
227 lum.cmd = LTTNG_UST_CONTEXT;
228 lum.u.context.ctx = ctx->ctx;
229 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
230 if (ret) {
231 free(context_data);
232 return ret;
233 }
32ce8569
MD
234 context_data->handle = -1;
235 DBG("Context created successfully");
57773204
MD
236 *_context_data = context_data;
237 return ret;
238}
239
cd54f6d9
MD
240int ustctl_set_filter(int sock, struct lttng_ust_filter_bytecode *bytecode,
241 struct lttng_ust_object_data *obj_data)
242{
243 struct ustcomm_ust_msg lum;
244 struct ustcomm_ust_reply lur;
245 int ret;
246
247 if (!obj_data)
248 return -EINVAL;
249
250 memset(&lum, 0, sizeof(lum));
251 lum.handle = obj_data->handle;
252 lum.cmd = LTTNG_UST_FILTER;
253 lum.u.filter.data_size = bytecode->len;
254 lum.u.filter.reloc_offset = bytecode->reloc_offset;
e695af51 255 lum.u.filter.seqnum = bytecode->seqnum;
cd54f6d9
MD
256
257 ret = ustcomm_send_app_msg(sock, &lum);
258 if (ret)
259 return ret;
cd54f6d9
MD
260 /* send var len bytecode */
261 ret = ustcomm_send_unix_sock(sock, bytecode->data,
262 bytecode->len);
263 if (ret < 0) {
264 return ret;
265 }
7bc53e94
MD
266 if (ret != bytecode->len)
267 return -EINVAL;
268 return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
cd54f6d9
MD
269}
270
57773204 271/* Enable event, channel and session ioctl */
61f02aea 272int ustctl_enable(int sock, struct lttng_ust_object_data *object)
57773204
MD
273{
274 struct ustcomm_ust_msg lum;
275 struct ustcomm_ust_reply lur;
276 int ret;
277
9bfc503d
MD
278 if (!object)
279 return -EINVAL;
280
57773204
MD
281 memset(&lum, 0, sizeof(lum));
282 lum.handle = object->handle;
283 lum.cmd = LTTNG_UST_ENABLE;
284 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
285 if (ret)
286 return ret;
287 DBG("enabled handle %u", object->handle);
288 return 0;
289}
290
291/* Disable event, channel and session ioctl */
61f02aea 292int ustctl_disable(int sock, struct lttng_ust_object_data *object)
57773204
MD
293{
294 struct ustcomm_ust_msg lum;
295 struct ustcomm_ust_reply lur;
296 int ret;
297
9bfc503d
MD
298 if (!object)
299 return -EINVAL;
300
57773204
MD
301 memset(&lum, 0, sizeof(lum));
302 lum.handle = object->handle;
303 lum.cmd = LTTNG_UST_DISABLE;
304 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
305 if (ret)
306 return ret;
307 DBG("disable handle %u", object->handle);
308 return 0;
309}
310
4a6ca058 311int ustctl_start_session(int sock, int handle)
57773204 312{
61f02aea 313 struct lttng_ust_object_data obj;
4a6ca058
MD
314
315 obj.handle = handle;
316 return ustctl_enable(sock, &obj);
57773204
MD
317}
318
4a6ca058 319int ustctl_stop_session(int sock, int handle)
57773204 320{
61f02aea 321 struct lttng_ust_object_data obj;
4a6ca058
MD
322
323 obj.handle = handle;
324 return ustctl_disable(sock, &obj);
57773204
MD
325}
326
57773204
MD
327int ustctl_tracepoint_list(int sock)
328{
b115631f
MD
329 struct ustcomm_ust_msg lum;
330 struct ustcomm_ust_reply lur;
331 int ret, tp_list_handle;
332
333 memset(&lum, 0, sizeof(lum));
334 lum.handle = LTTNG_UST_ROOT_HANDLE;
335 lum.cmd = LTTNG_UST_TRACEPOINT_LIST;
336 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
337 if (ret)
338 return ret;
339 tp_list_handle = lur.ret_val;
340 DBG("received tracepoint list handle %u", tp_list_handle);
341 return tp_list_handle;
342}
343
344int ustctl_tracepoint_list_get(int sock, int tp_list_handle,
cbef6901 345 struct lttng_ust_tracepoint_iter *iter)
b115631f
MD
346{
347 struct ustcomm_ust_msg lum;
348 struct ustcomm_ust_reply lur;
349 int ret;
350
9bfc503d
MD
351 if (!iter)
352 return -EINVAL;
353
b115631f
MD
354 memset(&lum, 0, sizeof(lum));
355 lum.handle = tp_list_handle;
356 lum.cmd = LTTNG_UST_TRACEPOINT_LIST_GET;
357 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
358 if (ret)
359 return ret;
882a56d7 360 DBG("received tracepoint list entry name %s loglevel %d",
cbef6901 361 lur.u.tracepoint.name,
882a56d7 362 lur.u.tracepoint.loglevel);
cbef6901 363 memcpy(iter, &lur.u.tracepoint, sizeof(*iter));
b115631f 364 return 0;
57773204
MD
365}
366
40003310
MD
367int ustctl_tracepoint_field_list(int sock)
368{
369 struct ustcomm_ust_msg lum;
370 struct ustcomm_ust_reply lur;
371 int ret, tp_field_list_handle;
372
373 memset(&lum, 0, sizeof(lum));
374 lum.handle = LTTNG_UST_ROOT_HANDLE;
375 lum.cmd = LTTNG_UST_TRACEPOINT_FIELD_LIST;
376 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
377 if (ret)
378 return ret;
379 tp_field_list_handle = lur.ret_val;
380 DBG("received tracepoint field list handle %u", tp_field_list_handle);
381 return tp_field_list_handle;
382}
383
384int ustctl_tracepoint_field_list_get(int sock, int tp_field_list_handle,
385 struct lttng_ust_field_iter *iter)
386{
387 struct ustcomm_ust_msg lum;
388 struct ustcomm_ust_reply lur;
389 int ret;
390 ssize_t len;
391
392 if (!iter)
393 return -EINVAL;
394
395 memset(&lum, 0, sizeof(lum));
396 lum.handle = tp_field_list_handle;
397 lum.cmd = LTTNG_UST_TRACEPOINT_FIELD_LIST_GET;
398 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
399 if (ret)
400 return ret;
401 len = ustcomm_recv_unix_sock(sock, iter, sizeof(*iter));
402 if (len != sizeof(*iter)) {
403 return -EINVAL;
404 }
405 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
406 iter->event_name,
407 iter->loglevel,
408 iter->field_name,
409 iter->type);
410 return 0;
411}
412
57773204
MD
413int ustctl_tracer_version(int sock, struct lttng_ust_tracer_version *v)
414{
415 struct ustcomm_ust_msg lum;
416 struct ustcomm_ust_reply lur;
417 int ret;
418
9bfc503d
MD
419 if (!v)
420 return -EINVAL;
421
57773204
MD
422 memset(&lum, 0, sizeof(lum));
423 lum.handle = LTTNG_UST_ROOT_HANDLE;
424 lum.cmd = LTTNG_UST_TRACER_VERSION;
425 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
426 if (ret)
427 return ret;
428 memcpy(v, &lur.u.version, sizeof(*v));
429 DBG("received tracer version");
430 return 0;
431}
432
433int ustctl_wait_quiescent(int sock)
434{
435 struct ustcomm_ust_msg lum;
436 struct ustcomm_ust_reply lur;
437 int ret;
438
439 memset(&lum, 0, sizeof(lum));
440 lum.handle = LTTNG_UST_ROOT_HANDLE;
441 lum.cmd = LTTNG_UST_WAIT_QUIESCENT;
442 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
443 if (ret)
444 return ret;
445 DBG("waited for quiescent state");
446 return 0;
447}
448
449int ustctl_calibrate(int sock, struct lttng_ust_calibrate *calibrate)
450{
9bfc503d
MD
451 if (!calibrate)
452 return -EINVAL;
453
57773204
MD
454 return -ENOSYS;
455}
456
f1fffc57
MD
457int ustctl_sock_flush_buffer(int sock, struct lttng_ust_object_data *object)
458{
459 struct ustcomm_ust_msg lum;
460 struct ustcomm_ust_reply lur;
461 int ret;
462
9bfc503d
MD
463 if (!object)
464 return -EINVAL;
465
f1fffc57
MD
466 memset(&lum, 0, sizeof(lum));
467 lum.handle = object->handle;
468 lum.cmd = LTTNG_UST_FLUSH_BUFFER;
469 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
470 if (ret)
471 return ret;
472 DBG("flushed buffer handle %u", object->handle);
473 return 0;
474}
475
74d81a6c
MD
476static
477int ustctl_send_channel(int sock,
478 enum lttng_ust_chan_type type,
479 void *data,
480 uint64_t size,
ff0f5728 481 int wakeup_fd,
74d81a6c
MD
482 int send_fd_only)
483{
484 ssize_t len;
485
486 if (!send_fd_only) {
487 /* Send mmap size */
488 len = ustcomm_send_unix_sock(sock, &size, sizeof(size));
489 if (len != sizeof(size)) {
490 if (len < 0)
491 return len;
492 else
493 return -EIO;
494 }
495
496 /* Send channel type */
497 len = ustcomm_send_unix_sock(sock, &type, sizeof(type));
498 if (len != sizeof(type)) {
499 if (len < 0)
500 return len;
501 else
502 return -EIO;
503 }
504 }
505
506 /* Send channel data */
507 len = ustcomm_send_unix_sock(sock, data, size);
508 if (len != size) {
509 if (len < 0)
510 return len;
511 else
512 return -EIO;
513 }
57773204 514
ff0f5728
MD
515 /* Send wakeup fd */
516 len = ustcomm_send_fds_unix_sock(sock, &wakeup_fd, 1);
517 if (len <= 0) {
518 if (len < 0)
519 return len;
520 else
521 return -EIO;
522 }
74d81a6c
MD
523 return 0;
524}
525
526static
527int ustctl_send_stream(int sock,
528 uint32_t stream_nr,
529 uint64_t memory_map_size,
530 int shm_fd, int wakeup_fd,
531 int send_fd_only)
57773204 532{
74d81a6c
MD
533 ssize_t len;
534 int fds[2];
535
536 if (!send_fd_only) {
537 if (shm_fd < 0) {
538 /* finish iteration */
539 uint64_t v = -1;
540
541 len = ustcomm_send_unix_sock(sock, &v, sizeof(v));
542 if (len != sizeof(v)) {
543 if (len < 0)
544 return len;
545 else
546 return -EIO;
547 }
548 return 0;
549 }
550
551 /* Send mmap size */
552 len = ustcomm_send_unix_sock(sock, &memory_map_size,
553 sizeof(memory_map_size));
554 if (len != sizeof(memory_map_size)) {
555 if (len < 0)
556 return len;
557 else
558 return -EIO;
559 }
560
561 /* Send stream nr */
562 len = ustcomm_send_unix_sock(sock, &stream_nr,
563 sizeof(stream_nr));
564 if (len != sizeof(stream_nr)) {
565 if (len < 0)
566 return len;
567 else
568 return -EIO;
569 }
570 }
571
572 /* Send shm fd and wakeup fd */
573 fds[0] = shm_fd;
574 fds[1] = wakeup_fd;
575 len = ustcomm_send_fds_unix_sock(sock, fds, 2);
576 if (len <= 0) {
577 if (len < 0)
578 return len;
579 else
580 return -EIO;
581 }
582 return 0;
583}
584
585int ustctl_recv_channel_from_consumer(int sock,
586 struct lttng_ust_object_data **_channel_data)
587{
588 struct lttng_ust_object_data *channel_data;
589 ssize_t len;
ff0f5728 590 int wakeup_fd;
7a784989 591 int ret;
57773204 592
74d81a6c
MD
593 channel_data = zmalloc(sizeof(*channel_data));
594 if (!channel_data) {
595 ret = -ENOMEM;
596 goto error_alloc;
597 }
598 channel_data->type = LTTNG_UST_OBJECT_TYPE_CHANNEL;
12f3dabc 599 channel_data->handle = -1;
74d81a6c
MD
600
601 /* recv mmap size */
602 len = ustcomm_recv_unix_sock(sock, &channel_data->size,
603 sizeof(channel_data->size));
604 if (len != sizeof(channel_data->size)) {
605 if (len < 0)
606 ret = len;
607 else
608 ret = -EINVAL;
609 goto error;
610 }
9bfc503d 611
74d81a6c
MD
612 /* recv channel type */
613 len = ustcomm_recv_unix_sock(sock, &channel_data->u.channel.type,
614 sizeof(channel_data->u.channel.type));
615 if (len != sizeof(channel_data->u.channel.type)) {
616 if (len < 0)
617 ret = len;
618 else
619 ret = -EINVAL;
620 goto error;
621 }
622
623 /* recv channel data */
624 channel_data->u.channel.data = zmalloc(channel_data->size);
625 if (!channel_data->u.channel.data) {
626 ret = -ENOMEM;
627 goto error;
628 }
629 len = ustcomm_recv_unix_sock(sock, channel_data->u.channel.data,
630 channel_data->size);
631 if (len != channel_data->size) {
632 if (len < 0)
633 ret = len;
634 else
635 ret = -EINVAL;
636 goto error_recv_data;
637 }
ff0f5728
MD
638 /* recv wakeup fd */
639 len = ustcomm_recv_fds_unix_sock(sock, &wakeup_fd, 1);
640 if (len <= 0) {
641 if (len < 0) {
642 ret = len;
643 goto error_recv_data;
644 } else {
645 ret = -EIO;
646 goto error_recv_data;
647 }
648 }
649 channel_data->u.channel.wakeup_fd = wakeup_fd;
74d81a6c
MD
650 *_channel_data = channel_data;
651 return 0;
652
653error_recv_data:
654 free(channel_data->u.channel.data);
655error:
656 free(channel_data);
657error_alloc:
658 return ret;
659}
660
661int ustctl_recv_stream_from_consumer(int sock,
662 struct lttng_ust_object_data **_stream_data)
663{
664 struct lttng_ust_object_data *stream_data;
665 ssize_t len;
666 int ret;
667 int fds[2];
668
669 stream_data = zmalloc(sizeof(*stream_data));
670 if (!stream_data) {
671 ret = -ENOMEM;
672 goto error_alloc;
57773204 673 }
74d81a6c
MD
674
675 stream_data->type = LTTNG_UST_OBJECT_TYPE_STREAM;
676 stream_data->handle = -1;
677
678 /* recv mmap size */
679 len = ustcomm_recv_unix_sock(sock, &stream_data->size,
680 sizeof(stream_data->size));
681 if (len != sizeof(stream_data->size)) {
682 if (len < 0)
683 ret = len;
684 else
685 ret = -EINVAL;
686 goto error;
687 }
688 if (stream_data->size == -1) {
689 ret = -LTTNG_UST_ERR_NOENT;
690 goto error;
691 }
692
693 /* recv stream nr */
694 len = ustcomm_recv_unix_sock(sock, &stream_data->u.stream.stream_nr,
695 sizeof(stream_data->u.stream.stream_nr));
696 if (len != sizeof(stream_data->u.stream.stream_nr)) {
697 if (len < 0)
698 ret = len;
699 else
700 ret = -EINVAL;
701 goto error;
702 }
703
704 /* recv shm fd and wakeup fd */
705 len = ustcomm_recv_fds_unix_sock(sock, fds, 2);
706 if (len <= 0) {
707 if (len < 0) {
708 ret = len;
709 goto error;
710 } else {
711 ret = -EIO;
712 goto error;
0bfe09ec 713 }
0bfe09ec 714 }
74d81a6c
MD
715 stream_data->u.stream.shm_fd = fds[0];
716 stream_data->u.stream.wakeup_fd = fds[1];
717 *_stream_data = stream_data;
718 return 0;
0bfe09ec 719
74d81a6c
MD
720error:
721 free(stream_data);
722error_alloc:
723 return ret;
724}
725
726int ustctl_send_channel_to_ust(int sock, int session_handle,
727 struct lttng_ust_object_data *channel_data)
728{
729 struct ustcomm_ust_msg lum;
730 struct ustcomm_ust_reply lur;
731 int ret;
732
733 if (!channel_data)
734 return -EINVAL;
735
736 memset(&lum, 0, sizeof(lum));
737 lum.handle = session_handle;
738 lum.cmd = LTTNG_UST_CHANNEL;
739 lum.u.channel.len = channel_data->size;
740 lum.u.channel.type = channel_data->u.channel.type;
741 ret = ustcomm_send_app_msg(sock, &lum);
742 if (ret)
743 return ret;
744
745 ret = ustctl_send_channel(sock,
746 channel_data->u.channel.type,
747 channel_data->u.channel.data,
748 channel_data->size,
ff0f5728 749 channel_data->u.channel.wakeup_fd,
74d81a6c
MD
750 1);
751 if (ret)
752 return ret;
753 ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
754 if (!ret) {
755 if (lur.ret_val >= 0) {
756 channel_data->handle = lur.ret_val;
757 }
57773204 758 }
74d81a6c
MD
759 return ret;
760}
761
762int ustctl_send_stream_to_ust(int sock,
763 struct lttng_ust_object_data *channel_data,
764 struct lttng_ust_object_data *stream_data)
765{
766 struct ustcomm_ust_msg lum;
767 struct ustcomm_ust_reply lur;
768 int ret;
769
770 memset(&lum, 0, sizeof(lum));
771 lum.handle = channel_data->handle;
772 lum.cmd = LTTNG_UST_STREAM;
773 lum.u.stream.len = stream_data->size;
774 lum.u.stream.stream_nr = stream_data->u.stream.stream_nr;
775 ret = ustcomm_send_app_msg(sock, &lum);
776 if (ret)
777 return ret;
778
779 assert(stream_data);
780 assert(stream_data->type == LTTNG_UST_OBJECT_TYPE_STREAM);
781
782 ret = ustctl_send_stream(sock,
783 stream_data->u.stream.stream_nr,
784 stream_data->size,
785 stream_data->u.stream.shm_fd,
786 stream_data->u.stream.wakeup_fd, 1);
787 if (ret)
788 return ret;
789 return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
790}
791
12f3dabc
MD
792int ustctl_duplicate_ust_object_data(struct lttng_ust_object_data **dest,
793 struct lttng_ust_object_data *src)
794{
795 struct lttng_ust_object_data *obj;
796 int ret;
797
798 if (src->handle != -1) {
799 ret = -EINVAL;
800 goto error;
801 }
802
803 obj = zmalloc(sizeof(*obj));
804 if (!obj) {
805 ret = -ENOMEM;
806 goto error;
807 }
808
809 obj->type = src->type;
810 obj->handle = src->handle;
811 obj->size = src->size;
812
813 switch (obj->type) {
814 case LTTNG_UST_OBJECT_TYPE_CHANNEL:
815 {
816 obj->u.channel.type = src->u.channel.type;
817 if (src->u.channel.wakeup_fd >= 0) {
818 obj->u.channel.wakeup_fd =
819 dup(src->u.channel.wakeup_fd);
820 if (obj->u.channel.wakeup_fd < 0) {
821 ret = errno;
822 goto chan_error_wakeup_fd;
823 }
824 } else {
825 obj->u.channel.wakeup_fd =
826 src->u.channel.wakeup_fd;
827 }
828 obj->u.channel.data = zmalloc(obj->size);
829 if (!obj->u.channel.data) {
830 ret = -ENOMEM;
831 goto chan_error_alloc;
832 }
833 memcpy(obj->u.channel.data, src->u.channel.data, obj->size);
834 break;
835
836 chan_error_alloc:
837 if (src->u.channel.wakeup_fd >= 0) {
838 int closeret;
839
840 closeret = close(obj->u.channel.wakeup_fd);
841 if (closeret) {
842 PERROR("close");
843 }
844 }
845 chan_error_wakeup_fd:
846 goto error_type;
847
848 }
849
850 case LTTNG_UST_OBJECT_TYPE_STREAM:
851 {
852 obj->u.stream.stream_nr = src->u.stream.stream_nr;
853 if (src->u.stream.wakeup_fd >= 0) {
854 obj->u.stream.wakeup_fd =
855 dup(src->u.stream.wakeup_fd);
856 if (obj->u.stream.wakeup_fd < 0) {
857 ret = errno;
858 goto stream_error_wakeup_fd;
859 }
860 } else {
861 obj->u.stream.wakeup_fd =
862 src->u.stream.wakeup_fd;
863 }
864
865 if (src->u.stream.shm_fd >= 0) {
866 obj->u.stream.shm_fd =
867 dup(src->u.stream.shm_fd);
868 if (obj->u.stream.shm_fd < 0) {
869 ret = errno;
870 goto stream_error_shm_fd;
871 }
872 } else {
873 obj->u.stream.shm_fd =
874 src->u.stream.shm_fd;
875 }
876 break;
877
878 stream_error_shm_fd:
879 if (src->u.stream.wakeup_fd >= 0) {
880 int closeret;
881
882 closeret = close(obj->u.stream.wakeup_fd);
883 if (closeret) {
884 PERROR("close");
885 }
886 }
887 stream_error_wakeup_fd:
888 goto error_type;
889 }
890
891 default:
892 ret = -EINVAL;
893 goto error_type;
894 }
895
896 *dest = obj;
897 return 0;
898
899error_type:
900 free(obj);
901error:
902 return ret;
903}
904
74d81a6c
MD
905
906/* Buffer operations */
907
908struct ustctl_consumer_channel *
909 ustctl_create_channel(struct ustctl_consumer_channel_attr *attr)
910{
911 struct ustctl_consumer_channel *chan;
912 const char *transport_name;
913 struct lttng_transport *transport;
914
915 switch (attr->type) {
916 case LTTNG_UST_CHAN_PER_CPU:
917 if (attr->output == LTTNG_UST_MMAP) {
918 transport_name = attr->overwrite ?
919 "relay-overwrite-mmap" : "relay-discard-mmap";
920 } else {
921 return NULL;
922 }
c1fca457 923 break;
74d81a6c
MD
924 case LTTNG_UST_CHAN_METADATA:
925 if (attr->output == LTTNG_UST_MMAP)
926 transport_name = "relay-metadata-mmap";
927 else
928 return NULL;
c1fca457
MD
929 break;
930 default:
74d81a6c 931 transport_name = "<unknown>";
c1fca457
MD
932 return NULL;
933 }
74d81a6c
MD
934
935 transport = lttng_transport_find(transport_name);
936 if (!transport) {
937 DBG("LTTng transport %s not found\n",
32ce8569 938 transport_name);
74d81a6c 939 return NULL;
7a784989 940 }
74d81a6c
MD
941
942 chan = zmalloc(sizeof(*chan));
943 if (!chan)
944 return NULL;
945
946 chan->chan = transport->ops.channel_create(transport_name, NULL,
32ce8569 947 attr->subbuf_size, attr->num_subbuf,
74d81a6c 948 attr->switch_timer_interval,
32ce8569 949 attr->read_timer_interval,
74d81a6c
MD
950 attr->uuid);
951 if (!chan->chan) {
952 goto chan_error;
953 }
954 chan->chan->ops = &transport->ops;
955 memcpy(&chan->attr, attr, sizeof(chan->attr));
cb7378b3
MD
956 chan->wait_fd = ustctl_channel_get_wait_fd(chan);
957 chan->wakeup_fd = ustctl_channel_get_wakeup_fd(chan);
74d81a6c
MD
958 return chan;
959
960chan_error:
961 free(chan);
962 return NULL;
57773204
MD
963}
964
74d81a6c 965void ustctl_destroy_channel(struct ustctl_consumer_channel *chan)
57773204 966{
74d81a6c
MD
967 chan->chan->ops->channel_destroy(chan->chan);
968 free(chan);
969}
970
971int ustctl_send_channel_to_sessiond(int sock,
972 struct ustctl_consumer_channel *channel)
973{
974 struct shm_object_table *table;
57773204 975
74d81a6c
MD
976 table = channel->chan->handle->table;
977 if (table->size <= 0)
9bfc503d 978 return -EINVAL;
74d81a6c
MD
979 return ustctl_send_channel(sock,
980 channel->attr.type,
981 table->objects[0].memory_map,
982 table->objects[0].memory_map_size,
ff0f5728 983 channel->wakeup_fd,
74d81a6c
MD
984 0);
985}
9bfc503d 986
74d81a6c
MD
987int ustctl_send_stream_to_sessiond(int sock,
988 struct ustctl_consumer_stream *stream)
989{
990 if (!stream)
991 return ustctl_send_stream(sock, -1U, -1U, -1, -1, 0);
992
993 return ustctl_send_stream(sock,
994 stream->cpu,
995 stream->memory_map_size,
996 stream->shm_fd, stream->wakeup_fd,
997 0);
57773204
MD
998}
999
c9023c93
MD
1000int ustctl_write_metadata_to_channel(
1001 struct ustctl_consumer_channel *channel,
1002 const char *metadata_str, /* NOT null-terminated */
1003 size_t len) /* metadata length */
1004{
1005 struct lttng_ust_lib_ring_buffer_ctx ctx;
1006 struct lttng_channel *chan = channel->chan;
1007 const char *str = metadata_str;
1008 int ret = 0, waitret;
1009 size_t reserve_len, pos;
1010
1011 for (pos = 0; pos < len; pos += reserve_len) {
1012 reserve_len = min_t(size_t,
1013 chan->ops->packet_avail_size(chan->chan, chan->handle),
1014 len - pos);
1015 lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len,
1016 sizeof(char), -1, chan->handle);
1017 /*
1018 * We don't care about metadata buffer's records lost
1019 * count, because we always retry here. Report error if
1020 * we need to bail out after timeout or being
1021 * interrupted.
1022 */
1023 waitret = wait_cond_interruptible_timeout(
1024 ({
1025 ret = chan->ops->event_reserve(&ctx, 0);
1026 ret != -ENOBUFS || !ret;
1027 }),
1028 LTTNG_METADATA_TIMEOUT_MSEC);
1029 if (waitret == -ETIMEDOUT || waitret == -EINTR || ret) {
1030 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
1031 waitret == -EINTR ? "interrupted" :
1032 (ret == -ENOBUFS ? "timeout" : "I/O error"));
1033 if (waitret == -EINTR)
1034 ret = waitret;
1035 goto end;
1036 }
1037 chan->ops->event_write(&ctx, &str[pos], reserve_len);
1038 chan->ops->event_commit(&ctx);
1039 }
1040end:
1041 return ret;
1042}
1043
ff0f5728
MD
1044int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel *consumer_chan)
1045{
1046 struct channel *chan;
cb7378b3 1047 int ret;
ff0f5728
MD
1048
1049 chan = consumer_chan->chan->chan;
cb7378b3 1050 ret = ring_buffer_channel_close_wait_fd(&chan->backend.config,
ff0f5728 1051 chan, chan->handle);
cb7378b3
MD
1052 if (!ret)
1053 consumer_chan->wait_fd = -1;
1054 return ret;
ff0f5728
MD
1055}
1056
1057int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel *consumer_chan)
1058{
1059 struct channel *chan;
cb7378b3 1060 int ret;
ff0f5728
MD
1061
1062 chan = consumer_chan->chan->chan;
cb7378b3 1063 ret = ring_buffer_channel_close_wakeup_fd(&chan->backend.config,
ff0f5728 1064 chan, chan->handle);
cb7378b3
MD
1065 if (!ret)
1066 consumer_chan->wakeup_fd = -1;
1067 return ret;
ff0f5728
MD
1068}
1069
74d81a6c 1070int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream *stream)
5224b5c8
MD
1071{
1072 struct channel *chan;
1073
74d81a6c 1074 chan = stream->chan->chan->chan;
ff0f5728 1075 return ring_buffer_stream_close_wait_fd(&chan->backend.config,
74d81a6c 1076 chan, stream->handle, stream->cpu);
5224b5c8
MD
1077}
1078
74d81a6c 1079int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream *stream)
6e922b24 1080{
66bdd22a 1081 struct channel *chan;
74d81a6c
MD
1082
1083 chan = stream->chan->chan->chan;
ff0f5728 1084 return ring_buffer_stream_close_wakeup_fd(&chan->backend.config,
74d81a6c
MD
1085 chan, stream->handle, stream->cpu);
1086}
1087
1088struct ustctl_consumer_stream *
1089 ustctl_create_stream(struct ustctl_consumer_channel *channel,
1090 int cpu)
1091{
1092 struct ustctl_consumer_stream *stream;
1093 struct lttng_ust_shm_handle *handle;
1094 struct channel *chan;
1095 int shm_fd, wait_fd, wakeup_fd;
1096 uint64_t memory_map_size;
4cfec15c 1097 struct lttng_ust_lib_ring_buffer *buf;
6e922b24
MD
1098 int ret;
1099
74d81a6c
MD
1100 if (!channel)
1101 return NULL;
1102 handle = channel->chan->handle;
9bfc503d
MD
1103 if (!handle)
1104 return NULL;
1105
74d81a6c 1106 chan = channel->chan->chan;
6e922b24 1107 buf = channel_get_ring_buffer(&chan->backend.config,
74d81a6c
MD
1108 chan, cpu, handle, &shm_fd, &wait_fd,
1109 &wakeup_fd, &memory_map_size);
6e922b24
MD
1110 if (!buf)
1111 return NULL;
74d81a6c 1112 ret = lib_ring_buffer_open_read(buf, handle);
6e922b24
MD
1113 if (ret)
1114 return NULL;
74d81a6c
MD
1115
1116 stream = zmalloc(sizeof(*stream));
1117 if (!stream)
1118 goto alloc_error;
1119 stream->handle = handle;
1120 stream->buf = buf;
1121 stream->chan = channel;
1122 stream->shm_fd = shm_fd;
1123 stream->wait_fd = wait_fd;
1124 stream->wakeup_fd = wakeup_fd;
1125 stream->memory_map_size = memory_map_size;
1126 stream->cpu = cpu;
1127 return stream;
1128
1129alloc_error:
1130 return NULL;
1131}
1132
1133void ustctl_destroy_stream(struct ustctl_consumer_stream *stream)
1134{
1135 struct lttng_ust_lib_ring_buffer *buf;
1136 struct ustctl_consumer_channel *consumer_chan;
1137
1138 assert(stream);
1139 buf = stream->buf;
1140 consumer_chan = stream->chan;
1141 lib_ring_buffer_release_read(buf, consumer_chan->chan->handle);
1142 free(stream);
6e922b24
MD
1143}
1144
ff0f5728
MD
1145int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel *chan)
1146{
1147 if (!chan)
1148 return -EINVAL;
1149 return shm_get_wait_fd(chan->chan->handle,
1150 &chan->chan->handle->chan._ref);
1151}
1152
1153int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel *chan)
1154{
1155 if (!chan)
1156 return -EINVAL;
1157 return shm_get_wakeup_fd(chan->chan->handle,
1158 &chan->chan->handle->chan._ref);
1159}
1160
1161int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream *stream)
6e922b24 1162{
74d81a6c
MD
1163 struct lttng_ust_lib_ring_buffer *buf;
1164 struct ustctl_consumer_channel *consumer_chan;
1165
1166 if (!stream)
1167 return -EINVAL;
1168 buf = stream->buf;
1169 consumer_chan = stream->chan;
1170 return shm_get_wait_fd(consumer_chan->chan->handle, &buf->self._ref);
1171}
1172
ff0f5728 1173int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream *stream)
74d81a6c
MD
1174{
1175 struct lttng_ust_lib_ring_buffer *buf;
1176 struct ustctl_consumer_channel *consumer_chan;
1177
1178 if (!stream)
1179 return -EINVAL;
1180 buf = stream->buf;
1181 consumer_chan = stream->chan;
1182 return shm_get_wakeup_fd(consumer_chan->chan->handle, &buf->self._ref);
6e922b24
MD
1183}
1184
57773204
MD
1185/* For mmap mode, readable without "get" operation */
1186
74d81a6c 1187void *ustctl_get_mmap_base(struct ustctl_consumer_stream *stream)
9095efe9 1188{
74d81a6c
MD
1189 struct lttng_ust_lib_ring_buffer *buf;
1190 struct ustctl_consumer_channel *consumer_chan;
1191
1192 if (!stream)
9bfc503d 1193 return NULL;
74d81a6c
MD
1194 buf = stream->buf;
1195 consumer_chan = stream->chan;
1196 return shmp(consumer_chan->chan->handle, buf->backend.memory_map);
9095efe9
MD
1197}
1198
57773204 1199/* returns the length to mmap. */
74d81a6c 1200int ustctl_get_mmap_len(struct ustctl_consumer_stream *stream,
57773204
MD
1201 unsigned long *len)
1202{
74d81a6c 1203 struct ustctl_consumer_channel *consumer_chan;
57773204 1204 unsigned long mmap_buf_len;
66bdd22a 1205 struct channel *chan;
57773204 1206
74d81a6c 1207 if (!stream)
9bfc503d 1208 return -EINVAL;
74d81a6c
MD
1209 consumer_chan = stream->chan;
1210 chan = consumer_chan->chan->chan;
57773204
MD
1211 if (chan->backend.config.output != RING_BUFFER_MMAP)
1212 return -EINVAL;
1213 mmap_buf_len = chan->backend.buf_size;
1214 if (chan->backend.extra_reader_sb)
1215 mmap_buf_len += chan->backend.subbuf_size;
1216 if (mmap_buf_len > INT_MAX)
1217 return -EFBIG;
1218 *len = mmap_buf_len;
1219 return 0;
1220}
1221
1222/* returns the maximum size for sub-buffers. */
74d81a6c 1223int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream *stream,
57773204
MD
1224 unsigned long *len)
1225{
74d81a6c 1226 struct ustctl_consumer_channel *consumer_chan;
66bdd22a 1227 struct channel *chan;
57773204 1228
74d81a6c 1229 if (!stream)
9bfc503d 1230 return -EINVAL;
74d81a6c
MD
1231 consumer_chan = stream->chan;
1232 chan = consumer_chan->chan->chan;
57773204
MD
1233 *len = chan->backend.subbuf_size;
1234 return 0;
1235}
1236
1237/*
1238 * For mmap mode, operate on the current packet (between get/put or
1239 * get_next/put_next).
1240 */
1241
1242/* returns the offset of the subbuffer belonging to the mmap reader. */
74d81a6c
MD
1243int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream *stream,
1244 unsigned long *off)
57773204 1245{
66bdd22a 1246 struct channel *chan;
57773204 1247 unsigned long sb_bindex;
74d81a6c
MD
1248 struct lttng_ust_lib_ring_buffer *buf;
1249 struct ustctl_consumer_channel *consumer_chan;
57773204 1250
74d81a6c 1251 if (!stream)
9bfc503d 1252 return -EINVAL;
74d81a6c
MD
1253 buf = stream->buf;
1254 consumer_chan = stream->chan;
1255 chan = consumer_chan->chan->chan;
57773204
MD
1256 if (chan->backend.config.output != RING_BUFFER_MMAP)
1257 return -EINVAL;
1258 sb_bindex = subbuffer_id_get_index(&chan->backend.config,
32ce8569 1259 buf->backend.buf_rsb.id);
74d81a6c
MD
1260 *off = shmp(consumer_chan->chan->handle,
1261 shmp_index(consumer_chan->chan->handle, buf->backend.array, sb_bindex)->shmp)->mmap_offset;
57773204
MD
1262 return 0;
1263}
1264
1265/* returns the size of the current sub-buffer, without padding (for mmap). */
74d81a6c
MD
1266int ustctl_get_subbuf_size(struct ustctl_consumer_stream *stream,
1267 unsigned long *len)
57773204 1268{
74d81a6c 1269 struct ustctl_consumer_channel *consumer_chan;
66bdd22a 1270 struct channel *chan;
74d81a6c 1271 struct lttng_ust_lib_ring_buffer *buf;
57773204 1272
74d81a6c 1273 if (!stream)
9bfc503d
MD
1274 return -EINVAL;
1275
74d81a6c
MD
1276 buf = stream->buf;
1277 consumer_chan = stream->chan;
1278 chan = consumer_chan->chan->chan;
57773204 1279 *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf,
74d81a6c 1280 consumer_chan->chan->handle);
57773204
MD
1281 return 0;
1282}
1283
1284/* returns the size of the current sub-buffer, without padding (for mmap). */
74d81a6c
MD
1285int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream *stream,
1286 unsigned long *len)
57773204 1287{
74d81a6c 1288 struct ustctl_consumer_channel *consumer_chan;
66bdd22a 1289 struct channel *chan;
74d81a6c 1290 struct lttng_ust_lib_ring_buffer *buf;
57773204 1291
74d81a6c 1292 if (!stream)
9bfc503d 1293 return -EINVAL;
74d81a6c
MD
1294 buf = stream->buf;
1295 consumer_chan = stream->chan;
1296 chan = consumer_chan->chan->chan;
57773204 1297 *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf,
74d81a6c 1298 consumer_chan->chan->handle);
57773204
MD
1299 *len = PAGE_ALIGN(*len);
1300 return 0;
1301}
1302
1303/* Get exclusive read access to the next sub-buffer that can be read. */
74d81a6c 1304int ustctl_get_next_subbuf(struct ustctl_consumer_stream *stream)
57773204 1305{
74d81a6c
MD
1306 struct lttng_ust_lib_ring_buffer *buf;
1307 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1308
74d81a6c
MD
1309 if (!stream)
1310 return -EINVAL;
1311 buf = stream->buf;
1312 consumer_chan = stream->chan;
1313 return lib_ring_buffer_get_next_subbuf(buf,
1314 consumer_chan->chan->handle);
57773204
MD
1315}
1316
1317
1318/* Release exclusive sub-buffer access, move consumer forward. */
74d81a6c 1319int ustctl_put_next_subbuf(struct ustctl_consumer_stream *stream)
57773204 1320{
74d81a6c
MD
1321 struct lttng_ust_lib_ring_buffer *buf;
1322 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1323
74d81a6c
MD
1324 if (!stream)
1325 return -EINVAL;
1326 buf = stream->buf;
1327 consumer_chan = stream->chan;
1328 lib_ring_buffer_put_next_subbuf(buf, consumer_chan->chan->handle);
57773204
MD
1329 return 0;
1330}
1331
1332/* snapshot */
1333
1334/* Get a snapshot of the current ring buffer producer and consumer positions */
74d81a6c 1335int ustctl_snapshot(struct ustctl_consumer_stream *stream)
57773204 1336{
74d81a6c
MD
1337 struct lttng_ust_lib_ring_buffer *buf;
1338 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1339
74d81a6c
MD
1340 if (!stream)
1341 return -EINVAL;
1342 buf = stream->buf;
1343 consumer_chan = stream->chan;
57773204 1344 return lib_ring_buffer_snapshot(buf, &buf->cons_snapshot,
74d81a6c 1345 &buf->prod_snapshot, consumer_chan->chan->handle);
57773204
MD
1346}
1347
1348/* Get the consumer position (iteration start) */
74d81a6c
MD
1349int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream *stream,
1350 unsigned long *pos)
57773204 1351{
74d81a6c 1352 struct lttng_ust_lib_ring_buffer *buf;
9bfc503d 1353
74d81a6c
MD
1354 if (!stream)
1355 return -EINVAL;
1356 buf = stream->buf;
57773204
MD
1357 *pos = buf->cons_snapshot;
1358 return 0;
1359}
1360
1361/* Get the producer position (iteration end) */
74d81a6c
MD
1362int ustctl_snapshot_get_produced(struct ustctl_consumer_stream *stream,
1363 unsigned long *pos)
57773204 1364{
74d81a6c 1365 struct lttng_ust_lib_ring_buffer *buf;
9bfc503d 1366
74d81a6c
MD
1367 if (!stream)
1368 return -EINVAL;
1369 buf = stream->buf;
57773204
MD
1370 *pos = buf->prod_snapshot;
1371 return 0;
1372}
1373
1374/* Get exclusive read access to the specified sub-buffer position */
74d81a6c
MD
1375int ustctl_get_subbuf(struct ustctl_consumer_stream *stream,
1376 unsigned long *pos)
57773204 1377{
74d81a6c
MD
1378 struct lttng_ust_lib_ring_buffer *buf;
1379 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1380
74d81a6c
MD
1381 if (!stream)
1382 return -EINVAL;
1383 buf = stream->buf;
1384 consumer_chan = stream->chan;
1385 return lib_ring_buffer_get_subbuf(buf, *pos,
1386 consumer_chan->chan->handle);
57773204
MD
1387}
1388
1389/* Release exclusive sub-buffer access */
74d81a6c 1390int ustctl_put_subbuf(struct ustctl_consumer_stream *stream)
57773204 1391{
74d81a6c
MD
1392 struct lttng_ust_lib_ring_buffer *buf;
1393 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1394
74d81a6c
MD
1395 if (!stream)
1396 return -EINVAL;
1397 buf = stream->buf;
1398 consumer_chan = stream->chan;
1399 lib_ring_buffer_put_subbuf(buf, consumer_chan->chan->handle);
57773204
MD
1400 return 0;
1401}
1402
74d81a6c 1403void ustctl_flush_buffer(struct ustctl_consumer_stream *stream,
b52190f2 1404 int producer_active)
57773204 1405{
74d81a6c
MD
1406 struct lttng_ust_lib_ring_buffer *buf;
1407 struct ustctl_consumer_channel *consumer_chan;
1408
1409 assert(stream);
1410 buf = stream->buf;
1411 consumer_chan = stream->chan;
b52190f2
MD
1412 lib_ring_buffer_switch_slow(buf,
1413 producer_active ? SWITCH_ACTIVE : SWITCH_FLUSH,
74d81a6c
MD
1414 consumer_chan->chan->handle);
1415}
1416
32ce8569
MD
1417/*
1418 * Returns 0 on success, negative error value on error.
1419 */
1420int ustctl_recv_reg_msg(int sock,
1421 enum ustctl_socket_type *type,
1422 uint32_t *major,
1423 uint32_t *minor,
1424 uint32_t *pid,
1425 uint32_t *ppid,
1426 uint32_t *uid,
1427 uint32_t *gid,
1428 uint32_t *bits_per_long,
1429 uint32_t *uint8_t_alignment,
1430 uint32_t *uint16_t_alignment,
1431 uint32_t *uint32_t_alignment,
1432 uint32_t *uint64_t_alignment,
1433 uint32_t *long_alignment,
1434 int *byte_order,
1435 char *name)
1436{
1437 ssize_t len;
1438 struct ustctl_reg_msg reg_msg;
1439
1440 len = ustcomm_recv_unix_sock(sock, &reg_msg, sizeof(reg_msg));
1441 if (len > 0 && len != sizeof(reg_msg))
1442 return -EIO;
1443 if (len == 0)
1444 return -EPIPE;
1445 if (len < 0)
1446 return len;
1447
1448 if (reg_msg.magic == LTTNG_UST_COMM_MAGIC) {
1449 *byte_order = BYTE_ORDER == BIG_ENDIAN ?
1450 BIG_ENDIAN : LITTLE_ENDIAN;
1451 } else if (reg_msg.magic == bswap_32(LTTNG_UST_COMM_MAGIC)) {
1452 *byte_order = BYTE_ORDER == BIG_ENDIAN ?
1453 LITTLE_ENDIAN : BIG_ENDIAN;
1454 } else {
1455 return -LTTNG_UST_ERR_INVAL_MAGIC;
1456 }
1457 switch (reg_msg.socket_type) {
1458 case 0: *type = USTCTL_SOCKET_CMD;
1459 break;
1460 case 1: *type = USTCTL_SOCKET_NOTIFY;
1461 break;
1462 default:
1463 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE;
1464 }
1465 *major = reg_msg.major;
1466 *minor = reg_msg.minor;
1467 *pid = reg_msg.pid;
1468 *ppid = reg_msg.ppid;
1469 *uid = reg_msg.uid;
1470 *gid = reg_msg.gid;
1471 *bits_per_long = reg_msg.bits_per_long;
1472 *uint8_t_alignment = reg_msg.uint8_t_alignment;
1473 *uint16_t_alignment = reg_msg.uint16_t_alignment;
1474 *uint32_t_alignment = reg_msg.uint32_t_alignment;
1475 *uint64_t_alignment = reg_msg.uint64_t_alignment;
1476 *long_alignment = reg_msg.long_alignment;
1477 memcpy(name, reg_msg.name, LTTNG_UST_ABI_PROCNAME_LEN);
1478 if (reg_msg.major != LTTNG_UST_ABI_MAJOR_VERSION) {
1479 return -LTTNG_UST_ERR_UNSUP_MAJOR;
1480 }
1481
1482 return 0;
1483}
1484
1485int ustctl_recv_notify(int sock, enum ustctl_notify_cmd *notify_cmd)
1486{
1487 struct ustcomm_notify_hdr header;
1488 ssize_t len;
1489
1490 len = ustcomm_recv_unix_sock(sock, &header, sizeof(header));
1491 if (len > 0 && len != sizeof(header))
1492 return -EIO;
1493 if (len == 0)
1494 return -EPIPE;
1495 if (len < 0)
1496 return len;
1497 switch (header.notify_cmd) {
1498 case 0:
1499 *notify_cmd = USTCTL_NOTIFY_CMD_EVENT;
1500 break;
1501 case 1:
1502 *notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL;
1503 break;
1504 default:
1505 return -EINVAL;
1506 }
1507 return 0;
1508}
1509
1510/*
1511 * Returns 0 on success, negative error value on error.
1512 */
1513int ustctl_recv_register_event(int sock,
1514 int *session_objd,
1515 int *channel_objd,
1516 char *event_name,
1517 int *loglevel,
1518 char **signature,
1519 size_t *nr_fields,
1520 struct ustctl_field **fields,
1521 char **model_emf_uri)
1522{
1523 ssize_t len;
1524 struct ustcomm_notify_event_msg msg;
1525 size_t signature_len, fields_len, model_emf_uri_len;
1526 char *a_sign = NULL, *a_model_emf_uri = NULL;
1527 struct ustctl_field *a_fields = NULL;
1528
1529 len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg));
1530 if (len > 0 && len != sizeof(msg))
1531 return -EIO;
1532 if (len == 0)
1533 return -EPIPE;
1534 if (len < 0)
1535 return len;
1536
1537 *session_objd = msg.session_objd;
1538 *channel_objd = msg.channel_objd;
1539 strncpy(event_name, msg.event_name, LTTNG_UST_SYM_NAME_LEN);
1540 event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1541 *loglevel = msg.loglevel;
1542 signature_len = msg.signature_len;
1543 fields_len = msg.fields_len;
1544
1545 if (fields_len % sizeof(*a_fields) != 0) {
1546 return -EINVAL;
1547 }
1548
1549 model_emf_uri_len = msg.model_emf_uri_len;
1550
1551 /* recv signature. contains at least \0. */
1552 a_sign = zmalloc(signature_len);
1553 if (!a_sign)
1554 return -ENOMEM;
1555 len = ustcomm_recv_unix_sock(sock, a_sign, signature_len);
1556 if (len > 0 && len != signature_len) {
1557 len = -EIO;
1558 goto signature_error;
1559 }
1560 if (len == 0) {
1561 len = -EPIPE;
1562 goto signature_error;
1563 }
1564 if (len < 0) {
1565 goto signature_error;
1566 }
1567 /* Enforce end of string */
1568 signature[signature_len - 1] = '\0';
1569
1570 /* recv fields */
1571 if (fields_len) {
1572 a_fields = zmalloc(fields_len);
1573 if (!a_fields) {
1574 len = -ENOMEM;
1575 goto signature_error;
1576 }
1577 len = ustcomm_recv_unix_sock(sock, a_fields, fields_len);
1578 if (len > 0 && len != fields_len) {
1579 len = -EIO;
1580 goto fields_error;
1581 }
1582 if (len == 0) {
1583 len = -EPIPE;
1584 goto fields_error;
1585 }
1586 if (len < 0) {
1587 goto fields_error;
1588 }
1589 }
1590
1591 if (model_emf_uri_len) {
1592 /* recv model_emf_uri_len */
1593 a_model_emf_uri = zmalloc(model_emf_uri_len);
1594 if (!a_model_emf_uri) {
1595 len = -ENOMEM;
1596 goto fields_error;
1597 }
1598 len = ustcomm_recv_unix_sock(sock, a_model_emf_uri,
1599 model_emf_uri_len);
1600 if (len > 0 && len != model_emf_uri_len) {
1601 len = -EIO;
1602 goto model_error;
1603 }
1604 if (len == 0) {
1605 len = -EPIPE;
1606 goto model_error;
1607 }
1608 if (len < 0) {
1609 goto model_error;
1610 }
1611 /* Enforce end of string */
1612 a_model_emf_uri[model_emf_uri_len - 1] = '\0';
1613 }
1614
1615 *signature = a_sign;
1616 *nr_fields = fields_len / sizeof(*a_fields);
1617 *fields = a_fields;
1618 *model_emf_uri = a_model_emf_uri;
1619
1620 return 0;
1621
1622model_error:
1623 free(a_model_emf_uri);
1624fields_error:
1625 free(a_fields);
1626signature_error:
1627 free(a_sign);
1628 return len;
1629}
1630
1631/*
1632 * Returns 0 on success, negative error value on error.
1633 */
1634int ustctl_reply_register_event(int sock,
1635 uint32_t id,
1636 int ret_code)
1637{
1638 ssize_t len;
1639 struct {
1640 struct ustcomm_notify_hdr header;
1641 struct ustcomm_notify_event_reply r;
1642 } reply;
1643
1644 memset(&reply, 0, sizeof(reply));
1645 reply.header.notify_cmd = USTCTL_NOTIFY_CMD_EVENT;
1646 reply.r.ret_code = ret_code;
1647 reply.r.event_id = id;
1648 len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply));
1649 if (len > 0 && len != sizeof(reply))
1650 return -EIO;
1651 if (len < 0)
1652 return len;
1653 return 0;
1654}
1655
1656/*
1657 * Returns 0 on success, negative UST or system error value on error.
1658 */
1659int ustctl_recv_register_channel(int sock,
1660 int *session_objd, /* session descriptor (output) */
1661 int *channel_objd, /* channel descriptor (output) */
1662 size_t *nr_fields,
1663 struct ustctl_field **fields)
1664{
1665 ssize_t len;
1666 struct ustcomm_notify_channel_msg msg;
1667 size_t fields_len;
1668 struct ustctl_field *a_fields;
1669
1670 len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg));
1671 if (len > 0 && len != sizeof(msg))
1672 return -EIO;
1673 if (len == 0)
1674 return -EPIPE;
1675 if (len < 0)
1676 return len;
1677
1678 *session_objd = msg.session_objd;
1679 *channel_objd = msg.channel_objd;
1680 fields_len = msg.ctx_fields_len;
1681
1682 if (fields_len % sizeof(*a_fields) != 0) {
1683 return -EINVAL;
1684 }
1685
1686 /* recv fields */
1687 if (fields_len) {
1688 a_fields = zmalloc(fields_len);
1689 if (!a_fields) {
1690 len = -ENOMEM;
1691 goto alloc_error;
1692 }
1693 len = ustcomm_recv_unix_sock(sock, a_fields, fields_len);
1694 if (len > 0 && len != fields_len) {
1695 len = -EIO;
1696 goto fields_error;
1697 }
1698 if (len == 0) {
1699 len = -EPIPE;
1700 goto fields_error;
1701 }
1702 if (len < 0) {
1703 goto fields_error;
1704 }
1705 *fields = a_fields;
1706 } else {
1707 *fields = NULL;
1708 }
1709 *nr_fields = fields_len / sizeof(*a_fields);
1710 return 0;
1711
1712fields_error:
1713 free(a_fields);
1714alloc_error:
1715 return len;
1716}
1717
1718/*
1719 * Returns 0 on success, negative error value on error.
1720 */
1721int ustctl_reply_register_channel(int sock,
1722 uint32_t chan_id,
1723 enum ustctl_channel_header header_type,
1724 int ret_code)
1725{
1726 ssize_t len;
1727 struct {
1728 struct ustcomm_notify_hdr header;
1729 struct ustcomm_notify_channel_reply r;
1730 } reply;
1731
1732 memset(&reply, 0, sizeof(reply));
1733 reply.header.notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL;
1734 reply.r.ret_code = ret_code;
1735 reply.r.chan_id = chan_id;
1736 switch (header_type) {
1737 case USTCTL_CHANNEL_HEADER_COMPACT:
1738 reply.r.header_type = 1;
1739 break;
1740 case USTCTL_CHANNEL_HEADER_LARGE:
1741 reply.r.header_type = 2;
1742 break;
1743 default:
1744 reply.r.header_type = 0;
1745 break;
1746 }
1747 len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply));
1748 if (len > 0 && len != sizeof(reply))
1749 return -EIO;
1750 if (len < 0)
1751 return len;
1752 return 0;
1753}
1754
74d81a6c
MD
1755static __attribute__((constructor))
1756void ustctl_init(void)
1757{
1758 init_usterr();
1759 lttng_ring_buffer_metadata_client_init();
1760 lttng_ring_buffer_client_overwrite_init();
1761 lttng_ring_buffer_client_discard_init();
03d2d293 1762 lib_ringbuffer_signal_init();
74d81a6c
MD
1763}
1764
1765static __attribute__((destructor))
1766void ustctl_exit(void)
1767{
1768 lttng_ring_buffer_client_discard_exit();
1769 lttng_ring_buffer_client_overwrite_exit();
1770 lttng_ring_buffer_metadata_client_exit();
57773204 1771}
This page took 0.104551 seconds and 4 git commands to generate.