db1e219df55f89630b476e43eff1fd9436d30f73
[lttng-ust.git] / liblttng-ust-ctl / ustctl.c
1 /*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Copyright (C) 2011-2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License only.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #define _GNU_SOURCE
20 #include <string.h>
21 #include <sys/types.h>
22 #include <sys/socket.h>
23
24 #include <lttng/ust-config.h>
25 #include <lttng/ust-ctl.h>
26 #include <lttng/ust-abi.h>
27 #include <lttng/ust-events.h>
28 #include <sys/mman.h>
29 #include <byteswap.h>
30
31 #include <usterr-signal-safe.h>
32 #include <ust-comm.h>
33 #include <helper.h>
34
35 #include "../libringbuffer/backend.h"
36 #include "../libringbuffer/frontend.h"
37 #include "../liblttng-ust/wait.h"
38 #include "../liblttng-ust/lttng-rb-clients.h"
39 #include "../liblttng-ust/clock.h"
40 #include "../liblttng-ust/getenv.h"
41
42 /*
43 * Number of milliseconds to retry before failing metadata writes on
44 * buffer full condition. (10 seconds)
45 */
46 #define LTTNG_METADATA_TIMEOUT_MSEC 10000
47
48 /*
49 * Channel representation within consumer.
50 */
51 struct ustctl_consumer_channel {
52 struct lttng_channel *chan; /* lttng channel buffers */
53
54 /* initial attributes */
55 struct ustctl_consumer_channel_attr attr;
56 int wait_fd; /* monitor close() */
57 int wakeup_fd; /* monitor close() */
58 };
59
60 /*
61 * Stream representation within consumer.
62 */
63 struct ustctl_consumer_stream {
64 struct lttng_ust_shm_handle *handle; /* shared-memory handle */
65 struct lttng_ust_lib_ring_buffer *buf;
66 struct ustctl_consumer_channel *chan;
67 int shm_fd, wait_fd, wakeup_fd;
68 int cpu;
69 uint64_t memory_map_size;
70 };
71
72 extern void lttng_ring_buffer_client_overwrite_init(void);
73 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
74 extern void lttng_ring_buffer_client_discard_init(void);
75 extern void lttng_ring_buffer_client_discard_rt_init(void);
76 extern void lttng_ring_buffer_metadata_client_init(void);
77 extern void lttng_ring_buffer_client_overwrite_exit(void);
78 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
79 extern void lttng_ring_buffer_client_discard_exit(void);
80 extern void lttng_ring_buffer_client_discard_rt_exit(void);
81 extern void lttng_ring_buffer_metadata_client_exit(void);
82
83 int ustctl_release_handle(int sock, int handle)
84 {
85 struct ustcomm_ust_msg lum;
86 struct ustcomm_ust_reply lur;
87
88 if (sock < 0 || handle < 0)
89 return 0;
90 memset(&lum, 0, sizeof(lum));
91 lum.handle = handle;
92 lum.cmd = LTTNG_UST_RELEASE;
93 return ustcomm_send_app_cmd(sock, &lum, &lur);
94 }
95
96 /*
97 * If sock is negative, it means we don't have to notify the other side
98 * (e.g. application has already vanished).
99 */
100 int ustctl_release_object(int sock, struct lttng_ust_object_data *data)
101 {
102 int ret;
103
104 if (!data)
105 return -EINVAL;
106
107 switch (data->type) {
108 case LTTNG_UST_OBJECT_TYPE_CHANNEL:
109 if (data->u.channel.wakeup_fd >= 0) {
110 ret = close(data->u.channel.wakeup_fd);
111 if (ret < 0) {
112 ret = -errno;
113 return ret;
114 }
115 data->u.channel.wakeup_fd = -1;
116 }
117 free(data->u.channel.data);
118 data->u.channel.data = NULL;
119 break;
120 case LTTNG_UST_OBJECT_TYPE_STREAM:
121 if (data->u.stream.shm_fd >= 0) {
122 ret = close(data->u.stream.shm_fd);
123 if (ret < 0) {
124 ret = -errno;
125 return ret;
126 }
127 data->u.stream.shm_fd = -1;
128 }
129 if (data->u.stream.wakeup_fd >= 0) {
130 ret = close(data->u.stream.wakeup_fd);
131 if (ret < 0) {
132 ret = -errno;
133 return ret;
134 }
135 data->u.stream.wakeup_fd = -1;
136 }
137 break;
138 case LTTNG_UST_OBJECT_TYPE_EVENT:
139 case LTTNG_UST_OBJECT_TYPE_CONTEXT:
140 break;
141 default:
142 assert(0);
143 }
144 return ustctl_release_handle(sock, data->handle);
145 }
146
147 /*
148 * Send registration done packet to the application.
149 */
150 int ustctl_register_done(int sock)
151 {
152 struct ustcomm_ust_msg lum;
153 struct ustcomm_ust_reply lur;
154 int ret;
155
156 DBG("Sending register done command to %d", sock);
157 memset(&lum, 0, sizeof(lum));
158 lum.handle = LTTNG_UST_ROOT_HANDLE;
159 lum.cmd = LTTNG_UST_REGISTER_DONE;
160 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
161 if (ret)
162 return ret;
163 return 0;
164 }
165
166 /*
167 * returns session handle.
168 */
169 int ustctl_create_session(int sock)
170 {
171 struct ustcomm_ust_msg lum;
172 struct ustcomm_ust_reply lur;
173 int ret, session_handle;
174
175 /* Create session */
176 memset(&lum, 0, sizeof(lum));
177 lum.handle = LTTNG_UST_ROOT_HANDLE;
178 lum.cmd = LTTNG_UST_SESSION;
179 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
180 if (ret)
181 return ret;
182 session_handle = lur.ret_val;
183 DBG("received session handle %u", session_handle);
184 return session_handle;
185 }
186
187 int ustctl_create_event(int sock, struct lttng_ust_event *ev,
188 struct lttng_ust_object_data *channel_data,
189 struct lttng_ust_object_data **_event_data)
190 {
191 struct ustcomm_ust_msg lum;
192 struct ustcomm_ust_reply lur;
193 struct lttng_ust_object_data *event_data;
194 int ret;
195
196 if (!channel_data || !_event_data)
197 return -EINVAL;
198
199 event_data = zmalloc(sizeof(*event_data));
200 if (!event_data)
201 return -ENOMEM;
202 event_data->type = LTTNG_UST_OBJECT_TYPE_EVENT;
203 memset(&lum, 0, sizeof(lum));
204 lum.handle = channel_data->handle;
205 lum.cmd = LTTNG_UST_EVENT;
206 strncpy(lum.u.event.name, ev->name,
207 LTTNG_UST_SYM_NAME_LEN);
208 lum.u.event.instrumentation = ev->instrumentation;
209 lum.u.event.loglevel_type = ev->loglevel_type;
210 lum.u.event.loglevel = ev->loglevel;
211 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
212 if (ret) {
213 free(event_data);
214 return ret;
215 }
216 event_data->handle = lur.ret_val;
217 DBG("received event handle %u", event_data->handle);
218 *_event_data = event_data;
219 return 0;
220 }
221
222 int ustctl_add_context(int sock, struct lttng_ust_context_attr *ctx,
223 struct lttng_ust_object_data *obj_data,
224 struct lttng_ust_object_data **_context_data)
225 {
226 struct ustcomm_ust_msg lum;
227 struct ustcomm_ust_reply lur;
228 struct lttng_ust_object_data *context_data = NULL;
229 char *buf = NULL;
230 size_t len;
231 int ret;
232
233 if (!obj_data || !_context_data) {
234 ret = -EINVAL;
235 goto end;
236 }
237
238 context_data = zmalloc(sizeof(*context_data));
239 if (!context_data) {
240 ret = -ENOMEM;
241 goto end;
242 }
243 context_data->type = LTTNG_UST_OBJECT_TYPE_CONTEXT;
244 memset(&lum, 0, sizeof(lum));
245 lum.handle = obj_data->handle;
246 lum.cmd = LTTNG_UST_CONTEXT;
247
248 lum.u.context.ctx = ctx->ctx;
249 switch (ctx->ctx) {
250 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER:
251 lum.u.context.u.perf_counter = ctx->u.perf_counter;
252 break;
253 case LTTNG_UST_CONTEXT_APP_CONTEXT:
254 {
255 size_t provider_name_len = strlen(
256 ctx->u.app_ctx.provider_name) + 1;
257 size_t ctx_name_len = strlen(ctx->u.app_ctx.ctx_name) + 1;
258
259 lum.u.context.u.app_ctx.provider_name_len = provider_name_len;
260 lum.u.context.u.app_ctx.ctx_name_len = ctx_name_len;
261
262 len = provider_name_len + ctx_name_len;
263 buf = zmalloc(len);
264 if (!buf) {
265 ret = -ENOMEM;
266 goto end;
267 }
268 memcpy(buf, ctx->u.app_ctx.provider_name,
269 provider_name_len);
270 memcpy(buf + provider_name_len, ctx->u.app_ctx.ctx_name,
271 ctx_name_len);
272 break;
273 }
274 default:
275 break;
276 }
277 ret = ustcomm_send_app_msg(sock, &lum);
278 if (ret)
279 goto end;
280 if (buf) {
281 /* send var len ctx_name */
282 ret = ustcomm_send_unix_sock(sock, buf, len);
283 if (ret < 0) {
284 goto end;
285 }
286 if (ret != len) {
287 ret = -EINVAL;
288 goto end;
289 }
290 }
291 ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
292 if (ret < 0) {
293 goto end;
294 }
295 context_data->handle = -1;
296 DBG("Context created successfully");
297 *_context_data = context_data;
298 context_data = NULL;
299 end:
300 free(context_data);
301 free(buf);
302 return ret;
303 }
304
305 int ustctl_set_filter(int sock, struct lttng_ust_filter_bytecode *bytecode,
306 struct lttng_ust_object_data *obj_data)
307 {
308 struct ustcomm_ust_msg lum;
309 struct ustcomm_ust_reply lur;
310 int ret;
311
312 if (!obj_data)
313 return -EINVAL;
314
315 memset(&lum, 0, sizeof(lum));
316 lum.handle = obj_data->handle;
317 lum.cmd = LTTNG_UST_FILTER;
318 lum.u.filter.data_size = bytecode->len;
319 lum.u.filter.reloc_offset = bytecode->reloc_offset;
320 lum.u.filter.seqnum = bytecode->seqnum;
321
322 ret = ustcomm_send_app_msg(sock, &lum);
323 if (ret)
324 return ret;
325 /* send var len bytecode */
326 ret = ustcomm_send_unix_sock(sock, bytecode->data,
327 bytecode->len);
328 if (ret < 0) {
329 return ret;
330 }
331 if (ret != bytecode->len)
332 return -EINVAL;
333 return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
334 }
335
336 int ustctl_set_exclusion(int sock, struct lttng_ust_event_exclusion *exclusion,
337 struct lttng_ust_object_data *obj_data)
338 {
339 struct ustcomm_ust_msg lum;
340 struct ustcomm_ust_reply lur;
341 int ret;
342
343 if (!obj_data) {
344 return -EINVAL;
345 }
346
347 memset(&lum, 0, sizeof(lum));
348 lum.handle = obj_data->handle;
349 lum.cmd = LTTNG_UST_EXCLUSION;
350 lum.u.exclusion.count = exclusion->count;
351
352 ret = ustcomm_send_app_msg(sock, &lum);
353 if (ret) {
354 return ret;
355 }
356
357 /* send var len exclusion names */
358 ret = ustcomm_send_unix_sock(sock,
359 exclusion->names,
360 exclusion->count * LTTNG_UST_SYM_NAME_LEN);
361 if (ret < 0) {
362 return ret;
363 }
364 if (ret != exclusion->count * LTTNG_UST_SYM_NAME_LEN) {
365 return -EINVAL;
366 }
367 return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
368 }
369
370 /* Enable event, channel and session ioctl */
371 int ustctl_enable(int sock, struct lttng_ust_object_data *object)
372 {
373 struct ustcomm_ust_msg lum;
374 struct ustcomm_ust_reply lur;
375 int ret;
376
377 if (!object)
378 return -EINVAL;
379
380 memset(&lum, 0, sizeof(lum));
381 lum.handle = object->handle;
382 lum.cmd = LTTNG_UST_ENABLE;
383 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
384 if (ret)
385 return ret;
386 DBG("enabled handle %u", object->handle);
387 return 0;
388 }
389
390 /* Disable event, channel and session ioctl */
391 int ustctl_disable(int sock, struct lttng_ust_object_data *object)
392 {
393 struct ustcomm_ust_msg lum;
394 struct ustcomm_ust_reply lur;
395 int ret;
396
397 if (!object)
398 return -EINVAL;
399
400 memset(&lum, 0, sizeof(lum));
401 lum.handle = object->handle;
402 lum.cmd = LTTNG_UST_DISABLE;
403 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
404 if (ret)
405 return ret;
406 DBG("disable handle %u", object->handle);
407 return 0;
408 }
409
410 int ustctl_start_session(int sock, int handle)
411 {
412 struct lttng_ust_object_data obj;
413
414 obj.handle = handle;
415 return ustctl_enable(sock, &obj);
416 }
417
418 int ustctl_stop_session(int sock, int handle)
419 {
420 struct lttng_ust_object_data obj;
421
422 obj.handle = handle;
423 return ustctl_disable(sock, &obj);
424 }
425
426 int ustctl_tracepoint_list(int sock)
427 {
428 struct ustcomm_ust_msg lum;
429 struct ustcomm_ust_reply lur;
430 int ret, tp_list_handle;
431
432 memset(&lum, 0, sizeof(lum));
433 lum.handle = LTTNG_UST_ROOT_HANDLE;
434 lum.cmd = LTTNG_UST_TRACEPOINT_LIST;
435 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
436 if (ret)
437 return ret;
438 tp_list_handle = lur.ret_val;
439 DBG("received tracepoint list handle %u", tp_list_handle);
440 return tp_list_handle;
441 }
442
443 int ustctl_tracepoint_list_get(int sock, int tp_list_handle,
444 struct lttng_ust_tracepoint_iter *iter)
445 {
446 struct ustcomm_ust_msg lum;
447 struct ustcomm_ust_reply lur;
448 int ret;
449
450 if (!iter)
451 return -EINVAL;
452
453 memset(&lum, 0, sizeof(lum));
454 lum.handle = tp_list_handle;
455 lum.cmd = LTTNG_UST_TRACEPOINT_LIST_GET;
456 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
457 if (ret)
458 return ret;
459 DBG("received tracepoint list entry name %s loglevel %d",
460 lur.u.tracepoint.name,
461 lur.u.tracepoint.loglevel);
462 memcpy(iter, &lur.u.tracepoint, sizeof(*iter));
463 return 0;
464 }
465
466 int ustctl_tracepoint_field_list(int sock)
467 {
468 struct ustcomm_ust_msg lum;
469 struct ustcomm_ust_reply lur;
470 int ret, tp_field_list_handle;
471
472 memset(&lum, 0, sizeof(lum));
473 lum.handle = LTTNG_UST_ROOT_HANDLE;
474 lum.cmd = LTTNG_UST_TRACEPOINT_FIELD_LIST;
475 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
476 if (ret)
477 return ret;
478 tp_field_list_handle = lur.ret_val;
479 DBG("received tracepoint field list handle %u", tp_field_list_handle);
480 return tp_field_list_handle;
481 }
482
483 int ustctl_tracepoint_field_list_get(int sock, int tp_field_list_handle,
484 struct lttng_ust_field_iter *iter)
485 {
486 struct ustcomm_ust_msg lum;
487 struct ustcomm_ust_reply lur;
488 int ret;
489 ssize_t len;
490
491 if (!iter)
492 return -EINVAL;
493
494 memset(&lum, 0, sizeof(lum));
495 lum.handle = tp_field_list_handle;
496 lum.cmd = LTTNG_UST_TRACEPOINT_FIELD_LIST_GET;
497 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
498 if (ret)
499 return ret;
500 len = ustcomm_recv_unix_sock(sock, iter, sizeof(*iter));
501 if (len != sizeof(*iter)) {
502 return -EINVAL;
503 }
504 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
505 iter->event_name,
506 iter->loglevel,
507 iter->field_name,
508 iter->type);
509 return 0;
510 }
511
512 int ustctl_tracer_version(int sock, struct lttng_ust_tracer_version *v)
513 {
514 struct ustcomm_ust_msg lum;
515 struct ustcomm_ust_reply lur;
516 int ret;
517
518 if (!v)
519 return -EINVAL;
520
521 memset(&lum, 0, sizeof(lum));
522 lum.handle = LTTNG_UST_ROOT_HANDLE;
523 lum.cmd = LTTNG_UST_TRACER_VERSION;
524 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
525 if (ret)
526 return ret;
527 memcpy(v, &lur.u.version, sizeof(*v));
528 DBG("received tracer version");
529 return 0;
530 }
531
532 int ustctl_wait_quiescent(int sock)
533 {
534 struct ustcomm_ust_msg lum;
535 struct ustcomm_ust_reply lur;
536 int ret;
537
538 memset(&lum, 0, sizeof(lum));
539 lum.handle = LTTNG_UST_ROOT_HANDLE;
540 lum.cmd = LTTNG_UST_WAIT_QUIESCENT;
541 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
542 if (ret)
543 return ret;
544 DBG("waited for quiescent state");
545 return 0;
546 }
547
548 int ustctl_calibrate(int sock, struct lttng_ust_calibrate *calibrate)
549 {
550 if (!calibrate)
551 return -EINVAL;
552
553 return -ENOSYS;
554 }
555
556 int ustctl_sock_flush_buffer(int sock, struct lttng_ust_object_data *object)
557 {
558 struct ustcomm_ust_msg lum;
559 struct ustcomm_ust_reply lur;
560 int ret;
561
562 if (!object)
563 return -EINVAL;
564
565 memset(&lum, 0, sizeof(lum));
566 lum.handle = object->handle;
567 lum.cmd = LTTNG_UST_FLUSH_BUFFER;
568 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
569 if (ret)
570 return ret;
571 DBG("flushed buffer handle %u", object->handle);
572 return 0;
573 }
574
575 static
576 int ustctl_send_channel(int sock,
577 enum lttng_ust_chan_type type,
578 void *data,
579 uint64_t size,
580 int wakeup_fd,
581 int send_fd_only)
582 {
583 ssize_t len;
584
585 if (!send_fd_only) {
586 /* Send mmap size */
587 len = ustcomm_send_unix_sock(sock, &size, sizeof(size));
588 if (len != sizeof(size)) {
589 if (len < 0)
590 return len;
591 else
592 return -EIO;
593 }
594
595 /* Send channel type */
596 len = ustcomm_send_unix_sock(sock, &type, sizeof(type));
597 if (len != sizeof(type)) {
598 if (len < 0)
599 return len;
600 else
601 return -EIO;
602 }
603 }
604
605 /* Send channel data */
606 len = ustcomm_send_unix_sock(sock, data, size);
607 if (len != size) {
608 if (len < 0)
609 return len;
610 else
611 return -EIO;
612 }
613
614 /* Send wakeup fd */
615 len = ustcomm_send_fds_unix_sock(sock, &wakeup_fd, 1);
616 if (len <= 0) {
617 if (len < 0)
618 return len;
619 else
620 return -EIO;
621 }
622 return 0;
623 }
624
625 static
626 int ustctl_send_stream(int sock,
627 uint32_t stream_nr,
628 uint64_t memory_map_size,
629 int shm_fd, int wakeup_fd,
630 int send_fd_only)
631 {
632 ssize_t len;
633 int fds[2];
634
635 if (!send_fd_only) {
636 if (shm_fd < 0) {
637 /* finish iteration */
638 uint64_t v = -1;
639
640 len = ustcomm_send_unix_sock(sock, &v, sizeof(v));
641 if (len != sizeof(v)) {
642 if (len < 0)
643 return len;
644 else
645 return -EIO;
646 }
647 return 0;
648 }
649
650 /* Send mmap size */
651 len = ustcomm_send_unix_sock(sock, &memory_map_size,
652 sizeof(memory_map_size));
653 if (len != sizeof(memory_map_size)) {
654 if (len < 0)
655 return len;
656 else
657 return -EIO;
658 }
659
660 /* Send stream nr */
661 len = ustcomm_send_unix_sock(sock, &stream_nr,
662 sizeof(stream_nr));
663 if (len != sizeof(stream_nr)) {
664 if (len < 0)
665 return len;
666 else
667 return -EIO;
668 }
669 }
670
671 /* Send shm fd and wakeup fd */
672 fds[0] = shm_fd;
673 fds[1] = wakeup_fd;
674 len = ustcomm_send_fds_unix_sock(sock, fds, 2);
675 if (len <= 0) {
676 if (len < 0)
677 return len;
678 else
679 return -EIO;
680 }
681 return 0;
682 }
683
684 int ustctl_recv_channel_from_consumer(int sock,
685 struct lttng_ust_object_data **_channel_data)
686 {
687 struct lttng_ust_object_data *channel_data;
688 ssize_t len;
689 int wakeup_fd;
690 int ret;
691
692 channel_data = zmalloc(sizeof(*channel_data));
693 if (!channel_data) {
694 ret = -ENOMEM;
695 goto error_alloc;
696 }
697 channel_data->type = LTTNG_UST_OBJECT_TYPE_CHANNEL;
698 channel_data->handle = -1;
699
700 /* recv mmap size */
701 len = ustcomm_recv_unix_sock(sock, &channel_data->size,
702 sizeof(channel_data->size));
703 if (len != sizeof(channel_data->size)) {
704 if (len < 0)
705 ret = len;
706 else
707 ret = -EINVAL;
708 goto error;
709 }
710
711 /* recv channel type */
712 len = ustcomm_recv_unix_sock(sock, &channel_data->u.channel.type,
713 sizeof(channel_data->u.channel.type));
714 if (len != sizeof(channel_data->u.channel.type)) {
715 if (len < 0)
716 ret = len;
717 else
718 ret = -EINVAL;
719 goto error;
720 }
721
722 /* recv channel data */
723 channel_data->u.channel.data = zmalloc(channel_data->size);
724 if (!channel_data->u.channel.data) {
725 ret = -ENOMEM;
726 goto error;
727 }
728 len = ustcomm_recv_unix_sock(sock, channel_data->u.channel.data,
729 channel_data->size);
730 if (len != channel_data->size) {
731 if (len < 0)
732 ret = len;
733 else
734 ret = -EINVAL;
735 goto error_recv_data;
736 }
737 /* recv wakeup fd */
738 len = ustcomm_recv_fds_unix_sock(sock, &wakeup_fd, 1);
739 if (len <= 0) {
740 if (len < 0) {
741 ret = len;
742 goto error_recv_data;
743 } else {
744 ret = -EIO;
745 goto error_recv_data;
746 }
747 }
748 channel_data->u.channel.wakeup_fd = wakeup_fd;
749 *_channel_data = channel_data;
750 return 0;
751
752 error_recv_data:
753 free(channel_data->u.channel.data);
754 error:
755 free(channel_data);
756 error_alloc:
757 return ret;
758 }
759
760 int ustctl_recv_stream_from_consumer(int sock,
761 struct lttng_ust_object_data **_stream_data)
762 {
763 struct lttng_ust_object_data *stream_data;
764 ssize_t len;
765 int ret;
766 int fds[2];
767
768 stream_data = zmalloc(sizeof(*stream_data));
769 if (!stream_data) {
770 ret = -ENOMEM;
771 goto error_alloc;
772 }
773
774 stream_data->type = LTTNG_UST_OBJECT_TYPE_STREAM;
775 stream_data->handle = -1;
776
777 /* recv mmap size */
778 len = ustcomm_recv_unix_sock(sock, &stream_data->size,
779 sizeof(stream_data->size));
780 if (len != sizeof(stream_data->size)) {
781 if (len < 0)
782 ret = len;
783 else
784 ret = -EINVAL;
785 goto error;
786 }
787 if (stream_data->size == -1) {
788 ret = -LTTNG_UST_ERR_NOENT;
789 goto error;
790 }
791
792 /* recv stream nr */
793 len = ustcomm_recv_unix_sock(sock, &stream_data->u.stream.stream_nr,
794 sizeof(stream_data->u.stream.stream_nr));
795 if (len != sizeof(stream_data->u.stream.stream_nr)) {
796 if (len < 0)
797 ret = len;
798 else
799 ret = -EINVAL;
800 goto error;
801 }
802
803 /* recv shm fd and wakeup fd */
804 len = ustcomm_recv_fds_unix_sock(sock, fds, 2);
805 if (len <= 0) {
806 if (len < 0) {
807 ret = len;
808 goto error;
809 } else {
810 ret = -EIO;
811 goto error;
812 }
813 }
814 stream_data->u.stream.shm_fd = fds[0];
815 stream_data->u.stream.wakeup_fd = fds[1];
816 *_stream_data = stream_data;
817 return 0;
818
819 error:
820 free(stream_data);
821 error_alloc:
822 return ret;
823 }
824
825 int ustctl_send_channel_to_ust(int sock, int session_handle,
826 struct lttng_ust_object_data *channel_data)
827 {
828 struct ustcomm_ust_msg lum;
829 struct ustcomm_ust_reply lur;
830 int ret;
831
832 if (!channel_data)
833 return -EINVAL;
834
835 memset(&lum, 0, sizeof(lum));
836 lum.handle = session_handle;
837 lum.cmd = LTTNG_UST_CHANNEL;
838 lum.u.channel.len = channel_data->size;
839 lum.u.channel.type = channel_data->u.channel.type;
840 ret = ustcomm_send_app_msg(sock, &lum);
841 if (ret)
842 return ret;
843
844 ret = ustctl_send_channel(sock,
845 channel_data->u.channel.type,
846 channel_data->u.channel.data,
847 channel_data->size,
848 channel_data->u.channel.wakeup_fd,
849 1);
850 if (ret)
851 return ret;
852 ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
853 if (!ret) {
854 channel_data->handle = lur.ret_val;
855 }
856 return ret;
857 }
858
859 int ustctl_send_stream_to_ust(int sock,
860 struct lttng_ust_object_data *channel_data,
861 struct lttng_ust_object_data *stream_data)
862 {
863 struct ustcomm_ust_msg lum;
864 struct ustcomm_ust_reply lur;
865 int ret;
866
867 memset(&lum, 0, sizeof(lum));
868 lum.handle = channel_data->handle;
869 lum.cmd = LTTNG_UST_STREAM;
870 lum.u.stream.len = stream_data->size;
871 lum.u.stream.stream_nr = stream_data->u.stream.stream_nr;
872 ret = ustcomm_send_app_msg(sock, &lum);
873 if (ret)
874 return ret;
875
876 assert(stream_data);
877 assert(stream_data->type == LTTNG_UST_OBJECT_TYPE_STREAM);
878
879 ret = ustctl_send_stream(sock,
880 stream_data->u.stream.stream_nr,
881 stream_data->size,
882 stream_data->u.stream.shm_fd,
883 stream_data->u.stream.wakeup_fd, 1);
884 if (ret)
885 return ret;
886 return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
887 }
888
889 int ustctl_duplicate_ust_object_data(struct lttng_ust_object_data **dest,
890 struct lttng_ust_object_data *src)
891 {
892 struct lttng_ust_object_data *obj;
893 int ret;
894
895 if (src->handle != -1) {
896 ret = -EINVAL;
897 goto error;
898 }
899
900 obj = zmalloc(sizeof(*obj));
901 if (!obj) {
902 ret = -ENOMEM;
903 goto error;
904 }
905
906 obj->type = src->type;
907 obj->handle = src->handle;
908 obj->size = src->size;
909
910 switch (obj->type) {
911 case LTTNG_UST_OBJECT_TYPE_CHANNEL:
912 {
913 obj->u.channel.type = src->u.channel.type;
914 if (src->u.channel.wakeup_fd >= 0) {
915 obj->u.channel.wakeup_fd =
916 dup(src->u.channel.wakeup_fd);
917 if (obj->u.channel.wakeup_fd < 0) {
918 ret = errno;
919 goto chan_error_wakeup_fd;
920 }
921 } else {
922 obj->u.channel.wakeup_fd =
923 src->u.channel.wakeup_fd;
924 }
925 obj->u.channel.data = zmalloc(obj->size);
926 if (!obj->u.channel.data) {
927 ret = -ENOMEM;
928 goto chan_error_alloc;
929 }
930 memcpy(obj->u.channel.data, src->u.channel.data, obj->size);
931 break;
932
933 chan_error_alloc:
934 if (src->u.channel.wakeup_fd >= 0) {
935 int closeret;
936
937 closeret = close(obj->u.channel.wakeup_fd);
938 if (closeret) {
939 PERROR("close");
940 }
941 }
942 chan_error_wakeup_fd:
943 goto error_type;
944
945 }
946
947 case LTTNG_UST_OBJECT_TYPE_STREAM:
948 {
949 obj->u.stream.stream_nr = src->u.stream.stream_nr;
950 if (src->u.stream.wakeup_fd >= 0) {
951 obj->u.stream.wakeup_fd =
952 dup(src->u.stream.wakeup_fd);
953 if (obj->u.stream.wakeup_fd < 0) {
954 ret = errno;
955 goto stream_error_wakeup_fd;
956 }
957 } else {
958 obj->u.stream.wakeup_fd =
959 src->u.stream.wakeup_fd;
960 }
961
962 if (src->u.stream.shm_fd >= 0) {
963 obj->u.stream.shm_fd =
964 dup(src->u.stream.shm_fd);
965 if (obj->u.stream.shm_fd < 0) {
966 ret = errno;
967 goto stream_error_shm_fd;
968 }
969 } else {
970 obj->u.stream.shm_fd =
971 src->u.stream.shm_fd;
972 }
973 break;
974
975 stream_error_shm_fd:
976 if (src->u.stream.wakeup_fd >= 0) {
977 int closeret;
978
979 closeret = close(obj->u.stream.wakeup_fd);
980 if (closeret) {
981 PERROR("close");
982 }
983 }
984 stream_error_wakeup_fd:
985 goto error_type;
986 }
987
988 default:
989 ret = -EINVAL;
990 goto error_type;
991 }
992
993 *dest = obj;
994 return 0;
995
996 error_type:
997 free(obj);
998 error:
999 return ret;
1000 }
1001
1002
1003 /* Buffer operations */
1004
1005 int ustctl_get_nr_stream_per_channel(void)
1006 {
1007 return num_possible_cpus();
1008 }
1009
1010 struct ustctl_consumer_channel *
1011 ustctl_create_channel(struct ustctl_consumer_channel_attr *attr,
1012 const int *stream_fds, int nr_stream_fds)
1013 {
1014 struct ustctl_consumer_channel *chan;
1015 const char *transport_name;
1016 struct lttng_transport *transport;
1017
1018 switch (attr->type) {
1019 case LTTNG_UST_CHAN_PER_CPU:
1020 if (attr->output == LTTNG_UST_MMAP) {
1021 if (attr->overwrite) {
1022 if (attr->read_timer_interval == 0) {
1023 transport_name = "relay-overwrite-mmap";
1024 } else {
1025 transport_name = "relay-overwrite-rt-mmap";
1026 }
1027 } else {
1028 if (attr->read_timer_interval == 0) {
1029 transport_name = "relay-discard-mmap";
1030 } else {
1031 transport_name = "relay-discard-rt-mmap";
1032 }
1033 }
1034 } else {
1035 return NULL;
1036 }
1037 break;
1038 case LTTNG_UST_CHAN_METADATA:
1039 if (attr->output == LTTNG_UST_MMAP)
1040 transport_name = "relay-metadata-mmap";
1041 else
1042 return NULL;
1043 break;
1044 default:
1045 transport_name = "<unknown>";
1046 return NULL;
1047 }
1048
1049 transport = lttng_transport_find(transport_name);
1050 if (!transport) {
1051 DBG("LTTng transport %s not found\n",
1052 transport_name);
1053 return NULL;
1054 }
1055
1056 chan = zmalloc(sizeof(*chan));
1057 if (!chan)
1058 return NULL;
1059
1060 chan->chan = transport->ops.channel_create(transport_name, NULL,
1061 attr->subbuf_size, attr->num_subbuf,
1062 attr->switch_timer_interval,
1063 attr->read_timer_interval,
1064 attr->uuid, attr->chan_id,
1065 stream_fds, nr_stream_fds,
1066 attr->blocking_timeout);
1067 if (!chan->chan) {
1068 goto chan_error;
1069 }
1070 chan->chan->ops = &transport->ops;
1071 memcpy(&chan->attr, attr, sizeof(chan->attr));
1072 chan->wait_fd = ustctl_channel_get_wait_fd(chan);
1073 chan->wakeup_fd = ustctl_channel_get_wakeup_fd(chan);
1074 return chan;
1075
1076 chan_error:
1077 free(chan);
1078 return NULL;
1079 }
1080
1081 void ustctl_destroy_channel(struct ustctl_consumer_channel *chan)
1082 {
1083 (void) ustctl_channel_close_wait_fd(chan);
1084 (void) ustctl_channel_close_wakeup_fd(chan);
1085 chan->chan->ops->channel_destroy(chan->chan);
1086 free(chan);
1087 }
1088
1089 int ustctl_send_channel_to_sessiond(int sock,
1090 struct ustctl_consumer_channel *channel)
1091 {
1092 struct shm_object_table *table;
1093
1094 table = channel->chan->handle->table;
1095 if (table->size <= 0)
1096 return -EINVAL;
1097 return ustctl_send_channel(sock,
1098 channel->attr.type,
1099 table->objects[0].memory_map,
1100 table->objects[0].memory_map_size,
1101 channel->wakeup_fd,
1102 0);
1103 }
1104
1105 int ustctl_send_stream_to_sessiond(int sock,
1106 struct ustctl_consumer_stream *stream)
1107 {
1108 if (!stream)
1109 return ustctl_send_stream(sock, -1U, -1U, -1, -1, 0);
1110
1111 return ustctl_send_stream(sock,
1112 stream->cpu,
1113 stream->memory_map_size,
1114 stream->shm_fd, stream->wakeup_fd,
1115 0);
1116 }
1117
1118 int ustctl_write_metadata_to_channel(
1119 struct ustctl_consumer_channel *channel,
1120 const char *metadata_str, /* NOT null-terminated */
1121 size_t len) /* metadata length */
1122 {
1123 struct lttng_ust_lib_ring_buffer_ctx ctx;
1124 struct lttng_channel *chan = channel->chan;
1125 const char *str = metadata_str;
1126 int ret = 0, waitret;
1127 size_t reserve_len, pos;
1128
1129 for (pos = 0; pos < len; pos += reserve_len) {
1130 reserve_len = min_t(size_t,
1131 chan->ops->packet_avail_size(chan->chan, chan->handle),
1132 len - pos);
1133 lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len,
1134 sizeof(char), -1, chan->handle, NULL);
1135 /*
1136 * We don't care about metadata buffer's records lost
1137 * count, because we always retry here. Report error if
1138 * we need to bail out after timeout or being
1139 * interrupted.
1140 */
1141 waitret = wait_cond_interruptible_timeout(
1142 ({
1143 ret = chan->ops->event_reserve(&ctx, 0);
1144 ret != -ENOBUFS || !ret;
1145 }),
1146 LTTNG_METADATA_TIMEOUT_MSEC);
1147 if (waitret == -ETIMEDOUT || waitret == -EINTR || ret) {
1148 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
1149 waitret == -EINTR ? "interrupted" :
1150 (ret == -ENOBUFS ? "timeout" : "I/O error"));
1151 if (waitret == -EINTR)
1152 ret = waitret;
1153 goto end;
1154 }
1155 chan->ops->event_write(&ctx, &str[pos], reserve_len);
1156 chan->ops->event_commit(&ctx);
1157 }
1158 end:
1159 return ret;
1160 }
1161
1162 /*
1163 * Write at most one packet in the channel.
1164 * Returns the number of bytes written on success, < 0 on error.
1165 */
1166 ssize_t ustctl_write_one_packet_to_channel(
1167 struct ustctl_consumer_channel *channel,
1168 const char *metadata_str, /* NOT null-terminated */
1169 size_t len) /* metadata length */
1170 {
1171 struct lttng_ust_lib_ring_buffer_ctx ctx;
1172 struct lttng_channel *chan = channel->chan;
1173 const char *str = metadata_str;
1174 ssize_t reserve_len;
1175 int ret;
1176
1177 reserve_len = min_t(ssize_t,
1178 chan->ops->packet_avail_size(chan->chan, chan->handle),
1179 len);
1180 lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len,
1181 sizeof(char), -1, chan->handle, NULL);
1182 ret = chan->ops->event_reserve(&ctx, 0);
1183 if (ret != 0) {
1184 DBG("LTTng: event reservation failed");
1185 assert(ret < 0);
1186 reserve_len = ret;
1187 goto end;
1188 }
1189 chan->ops->event_write(&ctx, str, reserve_len);
1190 chan->ops->event_commit(&ctx);
1191
1192 end:
1193 return reserve_len;
1194 }
1195
1196 int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel *consumer_chan)
1197 {
1198 struct channel *chan;
1199 int ret;
1200
1201 chan = consumer_chan->chan->chan;
1202 ret = ring_buffer_channel_close_wait_fd(&chan->backend.config,
1203 chan, chan->handle);
1204 if (!ret)
1205 consumer_chan->wait_fd = -1;
1206 return ret;
1207 }
1208
1209 int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel *consumer_chan)
1210 {
1211 struct channel *chan;
1212 int ret;
1213
1214 chan = consumer_chan->chan->chan;
1215 ret = ring_buffer_channel_close_wakeup_fd(&chan->backend.config,
1216 chan, chan->handle);
1217 if (!ret)
1218 consumer_chan->wakeup_fd = -1;
1219 return ret;
1220 }
1221
1222 int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream *stream)
1223 {
1224 struct channel *chan;
1225
1226 chan = stream->chan->chan->chan;
1227 return ring_buffer_stream_close_wait_fd(&chan->backend.config,
1228 chan, stream->handle, stream->cpu);
1229 }
1230
1231 int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream *stream)
1232 {
1233 struct channel *chan;
1234
1235 chan = stream->chan->chan->chan;
1236 return ring_buffer_stream_close_wakeup_fd(&chan->backend.config,
1237 chan, stream->handle, stream->cpu);
1238 }
1239
1240 struct ustctl_consumer_stream *
1241 ustctl_create_stream(struct ustctl_consumer_channel *channel,
1242 int cpu)
1243 {
1244 struct ustctl_consumer_stream *stream;
1245 struct lttng_ust_shm_handle *handle;
1246 struct channel *chan;
1247 int shm_fd, wait_fd, wakeup_fd;
1248 uint64_t memory_map_size;
1249 struct lttng_ust_lib_ring_buffer *buf;
1250 int ret;
1251
1252 if (!channel)
1253 return NULL;
1254 handle = channel->chan->handle;
1255 if (!handle)
1256 return NULL;
1257
1258 chan = channel->chan->chan;
1259 buf = channel_get_ring_buffer(&chan->backend.config,
1260 chan, cpu, handle, &shm_fd, &wait_fd,
1261 &wakeup_fd, &memory_map_size);
1262 if (!buf)
1263 return NULL;
1264 ret = lib_ring_buffer_open_read(buf, handle);
1265 if (ret)
1266 return NULL;
1267
1268 stream = zmalloc(sizeof(*stream));
1269 if (!stream)
1270 goto alloc_error;
1271 stream->handle = handle;
1272 stream->buf = buf;
1273 stream->chan = channel;
1274 stream->shm_fd = shm_fd;
1275 stream->wait_fd = wait_fd;
1276 stream->wakeup_fd = wakeup_fd;
1277 stream->memory_map_size = memory_map_size;
1278 stream->cpu = cpu;
1279 return stream;
1280
1281 alloc_error:
1282 return NULL;
1283 }
1284
1285 void ustctl_destroy_stream(struct ustctl_consumer_stream *stream)
1286 {
1287 struct lttng_ust_lib_ring_buffer *buf;
1288 struct ustctl_consumer_channel *consumer_chan;
1289
1290 assert(stream);
1291 buf = stream->buf;
1292 consumer_chan = stream->chan;
1293 (void) ustctl_stream_close_wait_fd(stream);
1294 (void) ustctl_stream_close_wakeup_fd(stream);
1295 lib_ring_buffer_release_read(buf, consumer_chan->chan->handle);
1296 free(stream);
1297 }
1298
1299 int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel *chan)
1300 {
1301 if (!chan)
1302 return -EINVAL;
1303 return shm_get_wait_fd(chan->chan->handle,
1304 &chan->chan->handle->chan._ref);
1305 }
1306
1307 int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel *chan)
1308 {
1309 if (!chan)
1310 return -EINVAL;
1311 return shm_get_wakeup_fd(chan->chan->handle,
1312 &chan->chan->handle->chan._ref);
1313 }
1314
1315 int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream *stream)
1316 {
1317 struct lttng_ust_lib_ring_buffer *buf;
1318 struct ustctl_consumer_channel *consumer_chan;
1319
1320 if (!stream)
1321 return -EINVAL;
1322 buf = stream->buf;
1323 consumer_chan = stream->chan;
1324 return shm_get_wait_fd(consumer_chan->chan->handle, &buf->self._ref);
1325 }
1326
1327 int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream *stream)
1328 {
1329 struct lttng_ust_lib_ring_buffer *buf;
1330 struct ustctl_consumer_channel *consumer_chan;
1331
1332 if (!stream)
1333 return -EINVAL;
1334 buf = stream->buf;
1335 consumer_chan = stream->chan;
1336 return shm_get_wakeup_fd(consumer_chan->chan->handle, &buf->self._ref);
1337 }
1338
1339 /* For mmap mode, readable without "get" operation */
1340
1341 void *ustctl_get_mmap_base(struct ustctl_consumer_stream *stream)
1342 {
1343 struct lttng_ust_lib_ring_buffer *buf;
1344 struct ustctl_consumer_channel *consumer_chan;
1345
1346 if (!stream)
1347 return NULL;
1348 buf = stream->buf;
1349 consumer_chan = stream->chan;
1350 return shmp(consumer_chan->chan->handle, buf->backend.memory_map);
1351 }
1352
1353 /* returns the length to mmap. */
1354 int ustctl_get_mmap_len(struct ustctl_consumer_stream *stream,
1355 unsigned long *len)
1356 {
1357 struct ustctl_consumer_channel *consumer_chan;
1358 unsigned long mmap_buf_len;
1359 struct channel *chan;
1360
1361 if (!stream)
1362 return -EINVAL;
1363 consumer_chan = stream->chan;
1364 chan = consumer_chan->chan->chan;
1365 if (chan->backend.config.output != RING_BUFFER_MMAP)
1366 return -EINVAL;
1367 mmap_buf_len = chan->backend.buf_size;
1368 if (chan->backend.extra_reader_sb)
1369 mmap_buf_len += chan->backend.subbuf_size;
1370 if (mmap_buf_len > INT_MAX)
1371 return -EFBIG;
1372 *len = mmap_buf_len;
1373 return 0;
1374 }
1375
1376 /* returns the maximum size for sub-buffers. */
1377 int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream *stream,
1378 unsigned long *len)
1379 {
1380 struct ustctl_consumer_channel *consumer_chan;
1381 struct channel *chan;
1382
1383 if (!stream)
1384 return -EINVAL;
1385 consumer_chan = stream->chan;
1386 chan = consumer_chan->chan->chan;
1387 *len = chan->backend.subbuf_size;
1388 return 0;
1389 }
1390
1391 /*
1392 * For mmap mode, operate on the current packet (between get/put or
1393 * get_next/put_next).
1394 */
1395
1396 /* returns the offset of the subbuffer belonging to the mmap reader. */
1397 int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream *stream,
1398 unsigned long *off)
1399 {
1400 struct channel *chan;
1401 unsigned long sb_bindex;
1402 struct lttng_ust_lib_ring_buffer *buf;
1403 struct ustctl_consumer_channel *consumer_chan;
1404 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *barray_idx;
1405 struct lttng_ust_lib_ring_buffer_backend_pages *pages;
1406
1407 if (!stream)
1408 return -EINVAL;
1409 buf = stream->buf;
1410 consumer_chan = stream->chan;
1411 chan = consumer_chan->chan->chan;
1412 if (chan->backend.config.output != RING_BUFFER_MMAP)
1413 return -EINVAL;
1414 sb_bindex = subbuffer_id_get_index(&chan->backend.config,
1415 buf->backend.buf_rsb.id);
1416 barray_idx = shmp_index(consumer_chan->chan->handle, buf->backend.array,
1417 sb_bindex);
1418 if (!barray_idx)
1419 return -EINVAL;
1420 pages = shmp(consumer_chan->chan->handle, barray_idx->shmp);
1421 if (!pages)
1422 return -EINVAL;
1423 *off = pages->mmap_offset;
1424 return 0;
1425 }
1426
1427 /* returns the size of the current sub-buffer, without padding (for mmap). */
1428 int ustctl_get_subbuf_size(struct ustctl_consumer_stream *stream,
1429 unsigned long *len)
1430 {
1431 struct ustctl_consumer_channel *consumer_chan;
1432 struct channel *chan;
1433 struct lttng_ust_lib_ring_buffer *buf;
1434
1435 if (!stream)
1436 return -EINVAL;
1437
1438 buf = stream->buf;
1439 consumer_chan = stream->chan;
1440 chan = consumer_chan->chan->chan;
1441 *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf,
1442 consumer_chan->chan->handle);
1443 return 0;
1444 }
1445
1446 /* returns the size of the current sub-buffer, without padding (for mmap). */
1447 int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream *stream,
1448 unsigned long *len)
1449 {
1450 struct ustctl_consumer_channel *consumer_chan;
1451 struct channel *chan;
1452 struct lttng_ust_lib_ring_buffer *buf;
1453
1454 if (!stream)
1455 return -EINVAL;
1456 buf = stream->buf;
1457 consumer_chan = stream->chan;
1458 chan = consumer_chan->chan->chan;
1459 *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf,
1460 consumer_chan->chan->handle);
1461 *len = PAGE_ALIGN(*len);
1462 return 0;
1463 }
1464
1465 /* Get exclusive read access to the next sub-buffer that can be read. */
1466 int ustctl_get_next_subbuf(struct ustctl_consumer_stream *stream)
1467 {
1468 struct lttng_ust_lib_ring_buffer *buf;
1469 struct ustctl_consumer_channel *consumer_chan;
1470
1471 if (!stream)
1472 return -EINVAL;
1473 buf = stream->buf;
1474 consumer_chan = stream->chan;
1475 return lib_ring_buffer_get_next_subbuf(buf,
1476 consumer_chan->chan->handle);
1477 }
1478
1479
1480 /* Release exclusive sub-buffer access, move consumer forward. */
1481 int ustctl_put_next_subbuf(struct ustctl_consumer_stream *stream)
1482 {
1483 struct lttng_ust_lib_ring_buffer *buf;
1484 struct ustctl_consumer_channel *consumer_chan;
1485
1486 if (!stream)
1487 return -EINVAL;
1488 buf = stream->buf;
1489 consumer_chan = stream->chan;
1490 lib_ring_buffer_put_next_subbuf(buf, consumer_chan->chan->handle);
1491 return 0;
1492 }
1493
1494 /* snapshot */
1495
1496 /* Get a snapshot of the current ring buffer producer and consumer positions */
1497 int ustctl_snapshot(struct ustctl_consumer_stream *stream)
1498 {
1499 struct lttng_ust_lib_ring_buffer *buf;
1500 struct ustctl_consumer_channel *consumer_chan;
1501
1502 if (!stream)
1503 return -EINVAL;
1504 buf = stream->buf;
1505 consumer_chan = stream->chan;
1506 return lib_ring_buffer_snapshot(buf, &buf->cons_snapshot,
1507 &buf->prod_snapshot, consumer_chan->chan->handle);
1508 }
1509
1510 /*
1511 * Get a snapshot of the current ring buffer producer and consumer positions
1512 * even if the consumed and produced positions are contained within the same
1513 * subbuffer.
1514 */
1515 int ustctl_snapshot_sample_positions(struct ustctl_consumer_stream *stream)
1516 {
1517 struct lttng_ust_lib_ring_buffer *buf;
1518 struct ustctl_consumer_channel *consumer_chan;
1519
1520 if (!stream)
1521 return -EINVAL;
1522 buf = stream->buf;
1523 consumer_chan = stream->chan;
1524 return lib_ring_buffer_snapshot_sample_positions(buf,
1525 &buf->cons_snapshot, &buf->prod_snapshot,
1526 consumer_chan->chan->handle);
1527 }
1528
1529 /* Get the consumer position (iteration start) */
1530 int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream *stream,
1531 unsigned long *pos)
1532 {
1533 struct lttng_ust_lib_ring_buffer *buf;
1534
1535 if (!stream)
1536 return -EINVAL;
1537 buf = stream->buf;
1538 *pos = buf->cons_snapshot;
1539 return 0;
1540 }
1541
1542 /* Get the producer position (iteration end) */
1543 int ustctl_snapshot_get_produced(struct ustctl_consumer_stream *stream,
1544 unsigned long *pos)
1545 {
1546 struct lttng_ust_lib_ring_buffer *buf;
1547
1548 if (!stream)
1549 return -EINVAL;
1550 buf = stream->buf;
1551 *pos = buf->prod_snapshot;
1552 return 0;
1553 }
1554
1555 /* Get exclusive read access to the specified sub-buffer position */
1556 int ustctl_get_subbuf(struct ustctl_consumer_stream *stream,
1557 unsigned long *pos)
1558 {
1559 struct lttng_ust_lib_ring_buffer *buf;
1560 struct ustctl_consumer_channel *consumer_chan;
1561
1562 if (!stream)
1563 return -EINVAL;
1564 buf = stream->buf;
1565 consumer_chan = stream->chan;
1566 return lib_ring_buffer_get_subbuf(buf, *pos,
1567 consumer_chan->chan->handle);
1568 }
1569
1570 /* Release exclusive sub-buffer access */
1571 int ustctl_put_subbuf(struct ustctl_consumer_stream *stream)
1572 {
1573 struct lttng_ust_lib_ring_buffer *buf;
1574 struct ustctl_consumer_channel *consumer_chan;
1575
1576 if (!stream)
1577 return -EINVAL;
1578 buf = stream->buf;
1579 consumer_chan = stream->chan;
1580 lib_ring_buffer_put_subbuf(buf, consumer_chan->chan->handle);
1581 return 0;
1582 }
1583
1584 void ustctl_flush_buffer(struct ustctl_consumer_stream *stream,
1585 int producer_active)
1586 {
1587 struct lttng_ust_lib_ring_buffer *buf;
1588 struct ustctl_consumer_channel *consumer_chan;
1589
1590 assert(stream);
1591 buf = stream->buf;
1592 consumer_chan = stream->chan;
1593 lib_ring_buffer_switch_slow(buf,
1594 producer_active ? SWITCH_ACTIVE : SWITCH_FLUSH,
1595 consumer_chan->chan->handle);
1596 }
1597
1598 static
1599 struct lttng_ust_client_lib_ring_buffer_client_cb *get_client_cb(
1600 struct lttng_ust_lib_ring_buffer *buf,
1601 struct lttng_ust_shm_handle *handle)
1602 {
1603 struct channel *chan;
1604 const struct lttng_ust_lib_ring_buffer_config *config;
1605 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1606
1607 chan = shmp(handle, buf->backend.chan);
1608 if (!chan)
1609 return NULL;
1610 config = &chan->backend.config;
1611 if (!config->cb_ptr)
1612 return NULL;
1613 client_cb = caa_container_of(config->cb_ptr,
1614 struct lttng_ust_client_lib_ring_buffer_client_cb,
1615 parent);
1616 return client_cb;
1617 }
1618
1619 int ustctl_get_timestamp_begin(struct ustctl_consumer_stream *stream,
1620 uint64_t *timestamp_begin)
1621 {
1622 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1623 struct lttng_ust_lib_ring_buffer *buf;
1624 struct lttng_ust_shm_handle *handle;
1625
1626 if (!stream || !timestamp_begin)
1627 return -EINVAL;
1628 buf = stream->buf;
1629 handle = stream->chan->chan->handle;
1630 client_cb = get_client_cb(buf, handle);
1631 if (!client_cb)
1632 return -ENOSYS;
1633 return client_cb->timestamp_begin(buf, handle, timestamp_begin);
1634 }
1635
1636 int ustctl_get_timestamp_end(struct ustctl_consumer_stream *stream,
1637 uint64_t *timestamp_end)
1638 {
1639 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1640 struct lttng_ust_lib_ring_buffer *buf;
1641 struct lttng_ust_shm_handle *handle;
1642
1643 if (!stream || !timestamp_end)
1644 return -EINVAL;
1645 buf = stream->buf;
1646 handle = stream->chan->chan->handle;
1647 client_cb = get_client_cb(buf, handle);
1648 if (!client_cb)
1649 return -ENOSYS;
1650 return client_cb->timestamp_end(buf, handle, timestamp_end);
1651 }
1652
1653 int ustctl_get_events_discarded(struct ustctl_consumer_stream *stream,
1654 uint64_t *events_discarded)
1655 {
1656 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1657 struct lttng_ust_lib_ring_buffer *buf;
1658 struct lttng_ust_shm_handle *handle;
1659
1660 if (!stream || !events_discarded)
1661 return -EINVAL;
1662 buf = stream->buf;
1663 handle = stream->chan->chan->handle;
1664 client_cb = get_client_cb(buf, handle);
1665 if (!client_cb)
1666 return -ENOSYS;
1667 return client_cb->events_discarded(buf, handle, events_discarded);
1668 }
1669
1670 int ustctl_get_content_size(struct ustctl_consumer_stream *stream,
1671 uint64_t *content_size)
1672 {
1673 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1674 struct lttng_ust_lib_ring_buffer *buf;
1675 struct lttng_ust_shm_handle *handle;
1676
1677 if (!stream || !content_size)
1678 return -EINVAL;
1679 buf = stream->buf;
1680 handle = stream->chan->chan->handle;
1681 client_cb = get_client_cb(buf, handle);
1682 if (!client_cb)
1683 return -ENOSYS;
1684 return client_cb->content_size(buf, handle, content_size);
1685 }
1686
1687 int ustctl_get_packet_size(struct ustctl_consumer_stream *stream,
1688 uint64_t *packet_size)
1689 {
1690 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1691 struct lttng_ust_lib_ring_buffer *buf;
1692 struct lttng_ust_shm_handle *handle;
1693
1694 if (!stream || !packet_size)
1695 return -EINVAL;
1696 buf = stream->buf;
1697 handle = stream->chan->chan->handle;
1698 client_cb = get_client_cb(buf, handle);
1699 if (!client_cb)
1700 return -ENOSYS;
1701 return client_cb->packet_size(buf, handle, packet_size);
1702 }
1703
1704 int ustctl_get_stream_id(struct ustctl_consumer_stream *stream,
1705 uint64_t *stream_id)
1706 {
1707 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1708 struct lttng_ust_lib_ring_buffer *buf;
1709 struct lttng_ust_shm_handle *handle;
1710
1711 if (!stream || !stream_id)
1712 return -EINVAL;
1713 buf = stream->buf;
1714 handle = stream->chan->chan->handle;
1715 client_cb = get_client_cb(buf, handle);
1716 if (!client_cb)
1717 return -ENOSYS;
1718 return client_cb->stream_id(buf, handle, stream_id);
1719 }
1720
1721 int ustctl_get_current_timestamp(struct ustctl_consumer_stream *stream,
1722 uint64_t *ts)
1723 {
1724 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1725 struct lttng_ust_lib_ring_buffer *buf;
1726 struct lttng_ust_shm_handle *handle;
1727
1728 if (!stream || !ts)
1729 return -EINVAL;
1730 buf = stream->buf;
1731 handle = stream->chan->chan->handle;
1732 client_cb = get_client_cb(buf, handle);
1733 if (!client_cb || !client_cb->current_timestamp)
1734 return -ENOSYS;
1735 return client_cb->current_timestamp(buf, handle, ts);
1736 }
1737
1738 int ustctl_get_sequence_number(struct ustctl_consumer_stream *stream,
1739 uint64_t *seq)
1740 {
1741 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1742 struct lttng_ust_lib_ring_buffer *buf;
1743 struct lttng_ust_shm_handle *handle;
1744
1745 if (!stream || !seq)
1746 return -EINVAL;
1747 buf = stream->buf;
1748 handle = stream->chan->chan->handle;
1749 client_cb = get_client_cb(buf, handle);
1750 if (!client_cb || !client_cb->sequence_number)
1751 return -ENOSYS;
1752 return client_cb->sequence_number(buf, handle, seq);
1753 }
1754
1755 int ustctl_get_instance_id(struct ustctl_consumer_stream *stream,
1756 uint64_t *id)
1757 {
1758 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1759 struct lttng_ust_lib_ring_buffer *buf;
1760 struct lttng_ust_shm_handle *handle;
1761
1762 if (!stream || !id)
1763 return -EINVAL;
1764 buf = stream->buf;
1765 handle = stream->chan->chan->handle;
1766 client_cb = get_client_cb(buf, handle);
1767 if (!client_cb)
1768 return -ENOSYS;
1769 return client_cb->instance_id(buf, handle, id);
1770 }
1771
1772 #ifdef LTTNG_UST_HAVE_PERF_EVENT
1773
1774 int ustctl_has_perf_counters(void)
1775 {
1776 return 1;
1777 }
1778
1779 #else
1780
1781 int ustctl_has_perf_counters(void)
1782 {
1783 return 0;
1784 }
1785
1786 #endif
1787
1788 #ifdef __linux__
1789 /*
1790 * Override application pid/uid/gid with unix socket credentials. If
1791 * the application announced a pid matching our view, it means it is
1792 * within the same pid namespace, so expose the ppid provided by the
1793 * application.
1794 */
1795 static
1796 int get_cred(int sock,
1797 const struct ustctl_reg_msg *reg_msg,
1798 uint32_t *pid,
1799 uint32_t *ppid,
1800 uint32_t *uid,
1801 uint32_t *gid)
1802 {
1803 struct ucred ucred;
1804 socklen_t ucred_len = sizeof(struct ucred);
1805 int ret;
1806
1807 ret = getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &ucred, &ucred_len);
1808 if (ret) {
1809 return -LTTNG_UST_ERR_PEERCRED;
1810 }
1811 DBG("Unix socket peercred [ pid: %u, uid: %u, gid: %u ], "
1812 "application registered claiming [ pid: %u, ppid: %u, uid: %u, gid: %u ]",
1813 ucred.pid, ucred.uid, ucred.gid,
1814 reg_msg->pid, reg_msg->ppid, reg_msg->uid, reg_msg->gid);
1815 if (!ucred.pid) {
1816 ERR("Unix socket credential pid=0. Refusing application in distinct, non-nested pid namespace.");
1817 return -LTTNG_UST_ERR_PEERCRED_PID;
1818 }
1819 *pid = ucred.pid;
1820 *uid = ucred.uid;
1821 *gid = ucred.gid;
1822 if (ucred.pid == reg_msg->pid) {
1823 *ppid = reg_msg->ppid;
1824 } else {
1825 *ppid = 0;
1826 }
1827 return 0;
1828 }
1829 #elif defined(__FreeBSD__)
1830 #include <sys/ucred.h>
1831
1832 /*
1833 * Override application uid/gid with unix socket credentials. Use the
1834 * first group of the cr_groups.
1835 * Use the pid and ppid provided by the application on registration.
1836 */
1837 static
1838 int get_cred(int sock,
1839 const struct ustctl_reg_msg *reg_msg,
1840 uint32_t *pid,
1841 uint32_t *ppid,
1842 uint32_t *uid,
1843 uint32_t *gid)
1844 {
1845 struct xucred xucred;
1846 socklen_t xucred_len = sizeof(struct xucred);
1847 int ret;
1848
1849 ret = getsockopt(sock, SOL_SOCKET, LOCAL_PEERCRED, &xucred, &xucred_len);
1850 if (ret) {
1851 return -LTTNG_UST_ERR_PEERCRED;
1852 }
1853 if (xucred.cr_version != XUCRED_VERSION || xucred.cr_ngroups < 1) {
1854 return -LTTNG_UST_ERR_PEERCRED;
1855 }
1856 DBG("Unix socket peercred [ uid: %u, gid: %u ], "
1857 "application registered claiming [ pid: %d, ppid: %d, uid: %u, gid: %u ]",
1858 xucred.uid, xucred.cr_groups[0],
1859 reg_msg->pid, reg_msg->ppid, reg_msg->uid, reg_msg->gid);
1860 *pid = reg_msg->pid;
1861 *ppid = reg_msg->ppid;
1862 *uid = xucred.uid;
1863 *gid = xucred.cr_groups[0];
1864 return 0;
1865 }
1866 #else
1867 #warning "Using insecure fallback: trusting user id provided by registered applications. Please consider implementing use of unix socket credentials on your platform."
1868 static
1869 int get_cred(int sock,
1870 const struct ustctl_reg_msg *reg_msg,
1871 uint32_t *pid,
1872 uint32_t *ppid,
1873 uint32_t *uid,
1874 uint32_t *gid)
1875 {
1876 DBG("Application registered claiming [ pid: %u, ppid: %d, uid: %u, gid: %u ]",
1877 reg_msg->pid, reg_msg->ppid, reg_msg->uid, reg_msg->gid);
1878 *pid = reg_msg->pid;
1879 *ppid = reg_msg->ppid;
1880 *uid = reg_msg->uid;
1881 *gid = reg_msg->gid;
1882 return 0;
1883 }
1884 #endif
1885
1886 /*
1887 * Returns 0 on success, negative error value on error.
1888 */
1889 int ustctl_recv_reg_msg(int sock,
1890 enum ustctl_socket_type *type,
1891 uint32_t *major,
1892 uint32_t *minor,
1893 uint32_t *pid,
1894 uint32_t *ppid,
1895 uint32_t *uid,
1896 uint32_t *gid,
1897 uint32_t *bits_per_long,
1898 uint32_t *uint8_t_alignment,
1899 uint32_t *uint16_t_alignment,
1900 uint32_t *uint32_t_alignment,
1901 uint32_t *uint64_t_alignment,
1902 uint32_t *long_alignment,
1903 int *byte_order,
1904 char *name)
1905 {
1906 ssize_t len;
1907 struct ustctl_reg_msg reg_msg;
1908
1909 len = ustcomm_recv_unix_sock(sock, &reg_msg, sizeof(reg_msg));
1910 if (len > 0 && len != sizeof(reg_msg))
1911 return -EIO;
1912 if (len == 0)
1913 return -EPIPE;
1914 if (len < 0)
1915 return len;
1916
1917 if (reg_msg.magic == LTTNG_UST_COMM_MAGIC) {
1918 *byte_order = BYTE_ORDER == BIG_ENDIAN ?
1919 BIG_ENDIAN : LITTLE_ENDIAN;
1920 } else if (reg_msg.magic == bswap_32(LTTNG_UST_COMM_MAGIC)) {
1921 *byte_order = BYTE_ORDER == BIG_ENDIAN ?
1922 LITTLE_ENDIAN : BIG_ENDIAN;
1923 } else {
1924 return -LTTNG_UST_ERR_INVAL_MAGIC;
1925 }
1926 switch (reg_msg.socket_type) {
1927 case 0: *type = USTCTL_SOCKET_CMD;
1928 break;
1929 case 1: *type = USTCTL_SOCKET_NOTIFY;
1930 break;
1931 default:
1932 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE;
1933 }
1934 *major = reg_msg.major;
1935 *minor = reg_msg.minor;
1936 *bits_per_long = reg_msg.bits_per_long;
1937 *uint8_t_alignment = reg_msg.uint8_t_alignment;
1938 *uint16_t_alignment = reg_msg.uint16_t_alignment;
1939 *uint32_t_alignment = reg_msg.uint32_t_alignment;
1940 *uint64_t_alignment = reg_msg.uint64_t_alignment;
1941 *long_alignment = reg_msg.long_alignment;
1942 memcpy(name, reg_msg.name, LTTNG_UST_ABI_PROCNAME_LEN);
1943 if (reg_msg.major != LTTNG_UST_ABI_MAJOR_VERSION) {
1944 return -LTTNG_UST_ERR_UNSUP_MAJOR;
1945 }
1946 return get_cred(sock, &reg_msg, pid, ppid, uid, gid);
1947 }
1948
1949 int ustctl_recv_notify(int sock, enum ustctl_notify_cmd *notify_cmd)
1950 {
1951 struct ustcomm_notify_hdr header;
1952 ssize_t len;
1953
1954 len = ustcomm_recv_unix_sock(sock, &header, sizeof(header));
1955 if (len > 0 && len != sizeof(header))
1956 return -EIO;
1957 if (len == 0)
1958 return -EPIPE;
1959 if (len < 0)
1960 return len;
1961 switch (header.notify_cmd) {
1962 case 0:
1963 *notify_cmd = USTCTL_NOTIFY_CMD_EVENT;
1964 break;
1965 case 1:
1966 *notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL;
1967 break;
1968 case 2:
1969 *notify_cmd = USTCTL_NOTIFY_CMD_ENUM;
1970 break;
1971 default:
1972 return -EINVAL;
1973 }
1974 return 0;
1975 }
1976
1977 /*
1978 * Returns 0 on success, negative error value on error.
1979 */
1980 int ustctl_recv_register_event(int sock,
1981 int *session_objd,
1982 int *channel_objd,
1983 char *event_name,
1984 int *loglevel,
1985 char **signature,
1986 size_t *nr_fields,
1987 struct ustctl_field **fields,
1988 char **model_emf_uri)
1989 {
1990 ssize_t len;
1991 struct ustcomm_notify_event_msg msg;
1992 size_t signature_len, fields_len, model_emf_uri_len;
1993 char *a_sign = NULL, *a_model_emf_uri = NULL;
1994 struct ustctl_field *a_fields = NULL;
1995
1996 len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg));
1997 if (len > 0 && len != sizeof(msg))
1998 return -EIO;
1999 if (len == 0)
2000 return -EPIPE;
2001 if (len < 0)
2002 return len;
2003
2004 *session_objd = msg.session_objd;
2005 *channel_objd = msg.channel_objd;
2006 strncpy(event_name, msg.event_name, LTTNG_UST_SYM_NAME_LEN);
2007 event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
2008 *loglevel = msg.loglevel;
2009 signature_len = msg.signature_len;
2010 fields_len = msg.fields_len;
2011
2012 if (fields_len % sizeof(*a_fields) != 0) {
2013 return -EINVAL;
2014 }
2015
2016 model_emf_uri_len = msg.model_emf_uri_len;
2017
2018 /* recv signature. contains at least \0. */
2019 a_sign = zmalloc(signature_len);
2020 if (!a_sign)
2021 return -ENOMEM;
2022 len = ustcomm_recv_unix_sock(sock, a_sign, signature_len);
2023 if (len > 0 && len != signature_len) {
2024 len = -EIO;
2025 goto signature_error;
2026 }
2027 if (len == 0) {
2028 len = -EPIPE;
2029 goto signature_error;
2030 }
2031 if (len < 0) {
2032 goto signature_error;
2033 }
2034 /* Enforce end of string */
2035 a_sign[signature_len - 1] = '\0';
2036
2037 /* recv fields */
2038 if (fields_len) {
2039 a_fields = zmalloc(fields_len);
2040 if (!a_fields) {
2041 len = -ENOMEM;
2042 goto signature_error;
2043 }
2044 len = ustcomm_recv_unix_sock(sock, a_fields, fields_len);
2045 if (len > 0 && len != fields_len) {
2046 len = -EIO;
2047 goto fields_error;
2048 }
2049 if (len == 0) {
2050 len = -EPIPE;
2051 goto fields_error;
2052 }
2053 if (len < 0) {
2054 goto fields_error;
2055 }
2056 }
2057
2058 if (model_emf_uri_len) {
2059 /* recv model_emf_uri_len */
2060 a_model_emf_uri = zmalloc(model_emf_uri_len);
2061 if (!a_model_emf_uri) {
2062 len = -ENOMEM;
2063 goto fields_error;
2064 }
2065 len = ustcomm_recv_unix_sock(sock, a_model_emf_uri,
2066 model_emf_uri_len);
2067 if (len > 0 && len != model_emf_uri_len) {
2068 len = -EIO;
2069 goto model_error;
2070 }
2071 if (len == 0) {
2072 len = -EPIPE;
2073 goto model_error;
2074 }
2075 if (len < 0) {
2076 goto model_error;
2077 }
2078 /* Enforce end of string */
2079 a_model_emf_uri[model_emf_uri_len - 1] = '\0';
2080 }
2081
2082 *signature = a_sign;
2083 *nr_fields = fields_len / sizeof(*a_fields);
2084 *fields = a_fields;
2085 *model_emf_uri = a_model_emf_uri;
2086
2087 return 0;
2088
2089 model_error:
2090 free(a_model_emf_uri);
2091 fields_error:
2092 free(a_fields);
2093 signature_error:
2094 free(a_sign);
2095 return len;
2096 }
2097
2098 /*
2099 * Returns 0 on success, negative error value on error.
2100 */
2101 int ustctl_reply_register_event(int sock,
2102 uint32_t id,
2103 int ret_code)
2104 {
2105 ssize_t len;
2106 struct {
2107 struct ustcomm_notify_hdr header;
2108 struct ustcomm_notify_event_reply r;
2109 } reply;
2110
2111 memset(&reply, 0, sizeof(reply));
2112 reply.header.notify_cmd = USTCTL_NOTIFY_CMD_EVENT;
2113 reply.r.ret_code = ret_code;
2114 reply.r.event_id = id;
2115 len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply));
2116 if (len > 0 && len != sizeof(reply))
2117 return -EIO;
2118 if (len < 0)
2119 return len;
2120 return 0;
2121 }
2122
2123 /*
2124 * Returns 0 on success, negative UST or system error value on error.
2125 */
2126 int ustctl_recv_register_enum(int sock,
2127 int *session_objd,
2128 char *enum_name,
2129 struct ustctl_enum_entry **entries,
2130 size_t *nr_entries)
2131 {
2132 ssize_t len;
2133 struct ustcomm_notify_enum_msg msg;
2134 size_t entries_len;
2135 struct ustctl_enum_entry *a_entries = NULL;
2136
2137 len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg));
2138 if (len > 0 && len != sizeof(msg))
2139 return -EIO;
2140 if (len == 0)
2141 return -EPIPE;
2142 if (len < 0)
2143 return len;
2144
2145 *session_objd = msg.session_objd;
2146 strncpy(enum_name, msg.enum_name, LTTNG_UST_SYM_NAME_LEN);
2147 enum_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
2148 entries_len = msg.entries_len;
2149
2150 if (entries_len % sizeof(*a_entries) != 0) {
2151 return -EINVAL;
2152 }
2153
2154 /* recv entries */
2155 if (entries_len) {
2156 a_entries = zmalloc(entries_len);
2157 if (!a_entries)
2158 return -ENOMEM;
2159 len = ustcomm_recv_unix_sock(sock, a_entries, entries_len);
2160 if (len > 0 && len != entries_len) {
2161 len = -EIO;
2162 goto entries_error;
2163 }
2164 if (len == 0) {
2165 len = -EPIPE;
2166 goto entries_error;
2167 }
2168 if (len < 0) {
2169 goto entries_error;
2170 }
2171 }
2172 *nr_entries = entries_len / sizeof(*a_entries);
2173 *entries = a_entries;
2174
2175 return 0;
2176
2177 entries_error:
2178 free(a_entries);
2179 return len;
2180 }
2181
2182 /*
2183 * Returns 0 on success, negative error value on error.
2184 */
2185 int ustctl_reply_register_enum(int sock,
2186 uint64_t id,
2187 int ret_code)
2188 {
2189 ssize_t len;
2190 struct {
2191 struct ustcomm_notify_hdr header;
2192 struct ustcomm_notify_enum_reply r;
2193 } reply;
2194
2195 memset(&reply, 0, sizeof(reply));
2196 reply.header.notify_cmd = USTCTL_NOTIFY_CMD_ENUM;
2197 reply.r.ret_code = ret_code;
2198 reply.r.enum_id = id;
2199 len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply));
2200 if (len > 0 && len != sizeof(reply))
2201 return -EIO;
2202 if (len < 0)
2203 return len;
2204 return 0;
2205 }
2206
2207 /*
2208 * Returns 0 on success, negative UST or system error value on error.
2209 */
2210 int ustctl_recv_register_channel(int sock,
2211 int *session_objd, /* session descriptor (output) */
2212 int *channel_objd, /* channel descriptor (output) */
2213 size_t *nr_fields,
2214 struct ustctl_field **fields)
2215 {
2216 ssize_t len;
2217 struct ustcomm_notify_channel_msg msg;
2218 size_t fields_len;
2219 struct ustctl_field *a_fields;
2220
2221 len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg));
2222 if (len > 0 && len != sizeof(msg))
2223 return -EIO;
2224 if (len == 0)
2225 return -EPIPE;
2226 if (len < 0)
2227 return len;
2228
2229 *session_objd = msg.session_objd;
2230 *channel_objd = msg.channel_objd;
2231 fields_len = msg.ctx_fields_len;
2232
2233 if (fields_len % sizeof(*a_fields) != 0) {
2234 return -EINVAL;
2235 }
2236
2237 /* recv fields */
2238 if (fields_len) {
2239 a_fields = zmalloc(fields_len);
2240 if (!a_fields) {
2241 len = -ENOMEM;
2242 goto alloc_error;
2243 }
2244 len = ustcomm_recv_unix_sock(sock, a_fields, fields_len);
2245 if (len > 0 && len != fields_len) {
2246 len = -EIO;
2247 goto fields_error;
2248 }
2249 if (len == 0) {
2250 len = -EPIPE;
2251 goto fields_error;
2252 }
2253 if (len < 0) {
2254 goto fields_error;
2255 }
2256 *fields = a_fields;
2257 } else {
2258 *fields = NULL;
2259 }
2260 *nr_fields = fields_len / sizeof(*a_fields);
2261 return 0;
2262
2263 fields_error:
2264 free(a_fields);
2265 alloc_error:
2266 return len;
2267 }
2268
2269 /*
2270 * Returns 0 on success, negative error value on error.
2271 */
2272 int ustctl_reply_register_channel(int sock,
2273 uint32_t chan_id,
2274 enum ustctl_channel_header header_type,
2275 int ret_code)
2276 {
2277 ssize_t len;
2278 struct {
2279 struct ustcomm_notify_hdr header;
2280 struct ustcomm_notify_channel_reply r;
2281 } reply;
2282
2283 memset(&reply, 0, sizeof(reply));
2284 reply.header.notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL;
2285 reply.r.ret_code = ret_code;
2286 reply.r.chan_id = chan_id;
2287 switch (header_type) {
2288 case USTCTL_CHANNEL_HEADER_COMPACT:
2289 reply.r.header_type = 1;
2290 break;
2291 case USTCTL_CHANNEL_HEADER_LARGE:
2292 reply.r.header_type = 2;
2293 break;
2294 default:
2295 reply.r.header_type = 0;
2296 break;
2297 }
2298 len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply));
2299 if (len > 0 && len != sizeof(reply))
2300 return -EIO;
2301 if (len < 0)
2302 return len;
2303 return 0;
2304 }
2305
2306 /* Regenerate the statedump. */
2307 int ustctl_regenerate_statedump(int sock, int handle)
2308 {
2309 struct ustcomm_ust_msg lum;
2310 struct ustcomm_ust_reply lur;
2311 int ret;
2312
2313 memset(&lum, 0, sizeof(lum));
2314 lum.handle = handle;
2315 lum.cmd = LTTNG_UST_SESSION_STATEDUMP;
2316 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
2317 if (ret)
2318 return ret;
2319 DBG("Regenerated statedump for handle %u", handle);
2320 return 0;
2321 }
2322
2323 static __attribute__((constructor))
2324 void ustctl_init(void)
2325 {
2326 init_usterr();
2327 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
2328 lttng_ust_clock_init();
2329 lttng_ring_buffer_metadata_client_init();
2330 lttng_ring_buffer_client_overwrite_init();
2331 lttng_ring_buffer_client_overwrite_rt_init();
2332 lttng_ring_buffer_client_discard_init();
2333 lttng_ring_buffer_client_discard_rt_init();
2334 lib_ringbuffer_signal_init();
2335 }
2336
2337 static __attribute__((destructor))
2338 void ustctl_exit(void)
2339 {
2340 lttng_ring_buffer_client_discard_rt_exit();
2341 lttng_ring_buffer_client_discard_exit();
2342 lttng_ring_buffer_client_overwrite_rt_exit();
2343 lttng_ring_buffer_client_overwrite_exit();
2344 lttng_ring_buffer_metadata_client_exit();
2345 }
This page took 0.074125 seconds and 3 git commands to generate.