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