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