Move libcounter to 'src/common/'
[lttng-ust.git] / src / lib / lttng-ust-ctl / ustctl.c
1 /*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 * Copyright (C) 2011 Julien Desfossez <julien.desfossez@polymtl.ca>
5 * Copyright (C) 2011-2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 */
7
8 #include <stdint.h>
9 #include <string.h>
10 #include <sys/mman.h>
11 #include <unistd.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
14
15 #include <lttng/ust-config.h>
16 #include <lttng/ust-ctl.h>
17 #include <lttng/ust-abi.h>
18 #include <lttng/ust-endian.h>
19
20 #include "common/logging.h"
21 #include "common/ustcomm.h"
22 #include "common/macros.h"
23 #include "common/align.h"
24
25 #include "libringbuffer/backend.h"
26 #include "libringbuffer/frontend.h"
27 #include "liblttng-ust/ust-events-internal.h"
28 #include "liblttng-ust/wait.h"
29 #include "liblttng-ust/lttng-rb-clients.h"
30 #include "liblttng-ust/clock.h"
31 #include "liblttng-ust/getenv.h"
32 #include "liblttng-ust/lttng-tracer-core.h"
33 #include "liblttng-ust/lttng-counter-client.h"
34
35 #include "common/counter/smp.h"
36 #include "common/counter/counter.h"
37
38 /*
39 * Number of milliseconds to retry before failing metadata writes on
40 * buffer full condition. (10 seconds)
41 */
42 #define LTTNG_METADATA_TIMEOUT_MSEC 10000
43
44 /*
45 * Channel representation within consumer.
46 */
47 struct ustctl_consumer_channel {
48 struct lttng_ust_channel_buffer *chan; /* lttng channel buffers */
49
50 /* initial attributes */
51 struct ustctl_consumer_channel_attr attr;
52 int wait_fd; /* monitor close() */
53 int wakeup_fd; /* monitor close() */
54 };
55
56 /*
57 * Stream representation within consumer.
58 */
59 struct ustctl_consumer_stream {
60 struct lttng_ust_lib_ring_buffer *buf;
61 struct ustctl_consumer_channel *chan;
62 int shm_fd, wait_fd, wakeup_fd;
63 int cpu;
64 uint64_t memory_map_size;
65 };
66
67 #define USTCTL_COUNTER_ATTR_DIMENSION_MAX 8
68 struct ustctl_counter_attr {
69 enum ustctl_counter_arithmetic arithmetic;
70 enum ustctl_counter_bitness bitness;
71 uint32_t nr_dimensions;
72 int64_t global_sum_step;
73 struct ustctl_counter_dimension dimensions[USTCTL_COUNTER_ATTR_DIMENSION_MAX];
74 bool coalesce_hits;
75 };
76
77 /*
78 * Counter representation within daemon.
79 */
80 struct ustctl_daemon_counter {
81 struct lib_counter *counter;
82 const struct lttng_counter_ops *ops;
83 struct ustctl_counter_attr *attr; /* initial attributes */
84 };
85
86 int ustctl_release_handle(int sock, int handle)
87 {
88 struct ustcomm_ust_msg lum;
89 struct ustcomm_ust_reply lur;
90
91 if (sock < 0 || handle < 0)
92 return 0;
93 memset(&lum, 0, sizeof(lum));
94 lum.handle = handle;
95 lum.cmd = LTTNG_UST_ABI_RELEASE;
96 return ustcomm_send_app_cmd(sock, &lum, &lur);
97 }
98
99 /*
100 * If sock is negative, it means we don't have to notify the other side
101 * (e.g. application has already vanished).
102 */
103 int ustctl_release_object(int sock, struct lttng_ust_abi_object_data *data)
104 {
105 int ret;
106
107 if (!data)
108 return -EINVAL;
109
110 switch (data->type) {
111 case LTTNG_UST_ABI_OBJECT_TYPE_CHANNEL:
112 if (data->u.channel.wakeup_fd >= 0) {
113 ret = close(data->u.channel.wakeup_fd);
114 if (ret < 0) {
115 ret = -errno;
116 return ret;
117 }
118 data->u.channel.wakeup_fd = -1;
119 }
120 free(data->u.channel.data);
121 data->u.channel.data = NULL;
122 break;
123 case LTTNG_UST_ABI_OBJECT_TYPE_STREAM:
124 if (data->u.stream.shm_fd >= 0) {
125 ret = close(data->u.stream.shm_fd);
126 if (ret < 0) {
127 ret = -errno;
128 return ret;
129 }
130 data->u.stream.shm_fd = -1;
131 }
132 if (data->u.stream.wakeup_fd >= 0) {
133 ret = close(data->u.stream.wakeup_fd);
134 if (ret < 0) {
135 ret = -errno;
136 return ret;
137 }
138 data->u.stream.wakeup_fd = -1;
139 }
140 break;
141 case LTTNG_UST_ABI_OBJECT_TYPE_EVENT:
142 case LTTNG_UST_ABI_OBJECT_TYPE_CONTEXT:
143 case LTTNG_UST_ABI_OBJECT_TYPE_EVENT_NOTIFIER_GROUP:
144 case LTTNG_UST_ABI_OBJECT_TYPE_EVENT_NOTIFIER:
145 break;
146 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER:
147 free(data->u.counter.data);
148 data->u.counter.data = NULL;
149 break;
150 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_GLOBAL:
151 if (data->u.counter_global.shm_fd >= 0) {
152 ret = close(data->u.counter_global.shm_fd);
153 if (ret < 0) {
154 ret = -errno;
155 return ret;
156 }
157 data->u.counter_global.shm_fd = -1;
158 }
159 break;
160 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_CPU:
161 if (data->u.counter_cpu.shm_fd >= 0) {
162 ret = close(data->u.counter_cpu.shm_fd);
163 if (ret < 0) {
164 ret = -errno;
165 return ret;
166 }
167 data->u.counter_cpu.shm_fd = -1;
168 }
169 break;
170 default:
171 assert(0);
172 }
173 return ustctl_release_handle(sock, data->handle);
174 }
175
176 /*
177 * Send registration done packet to the application.
178 */
179 int ustctl_register_done(int sock)
180 {
181 struct ustcomm_ust_msg lum;
182 struct ustcomm_ust_reply lur;
183 int ret;
184
185 DBG("Sending register done command to %d", sock);
186 memset(&lum, 0, sizeof(lum));
187 lum.handle = LTTNG_UST_ABI_ROOT_HANDLE;
188 lum.cmd = LTTNG_UST_ABI_REGISTER_DONE;
189 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
190 if (ret)
191 return ret;
192 return 0;
193 }
194
195 /*
196 * returns session handle.
197 */
198 int ustctl_create_session(int sock)
199 {
200 struct ustcomm_ust_msg lum;
201 struct ustcomm_ust_reply lur;
202 int ret, session_handle;
203
204 /* Create session */
205 memset(&lum, 0, sizeof(lum));
206 lum.handle = LTTNG_UST_ABI_ROOT_HANDLE;
207 lum.cmd = LTTNG_UST_ABI_SESSION;
208 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
209 if (ret)
210 return ret;
211 session_handle = lur.ret_val;
212 DBG("received session handle %u", session_handle);
213 return session_handle;
214 }
215
216 int ustctl_create_event(int sock, struct lttng_ust_abi_event *ev,
217 struct lttng_ust_abi_object_data *channel_data,
218 struct lttng_ust_abi_object_data **_event_data)
219 {
220 struct ustcomm_ust_msg lum;
221 struct ustcomm_ust_reply lur;
222 struct lttng_ust_abi_object_data *event_data;
223 int ret;
224
225 if (!channel_data || !_event_data)
226 return -EINVAL;
227
228 event_data = zmalloc(sizeof(*event_data));
229 if (!event_data)
230 return -ENOMEM;
231 event_data->type = LTTNG_UST_ABI_OBJECT_TYPE_EVENT;
232 memset(&lum, 0, sizeof(lum));
233 lum.handle = channel_data->handle;
234 lum.cmd = LTTNG_UST_ABI_EVENT;
235 strncpy(lum.u.event.name, ev->name,
236 LTTNG_UST_ABI_SYM_NAME_LEN);
237 lum.u.event.instrumentation = ev->instrumentation;
238 lum.u.event.loglevel_type = ev->loglevel_type;
239 lum.u.event.loglevel = ev->loglevel;
240 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
241 if (ret) {
242 free(event_data);
243 return ret;
244 }
245 event_data->handle = lur.ret_val;
246 DBG("received event handle %u", event_data->handle);
247 *_event_data = event_data;
248 return 0;
249 }
250
251 int ustctl_add_context(int sock, struct lttng_ust_context_attr *ctx,
252 struct lttng_ust_abi_object_data *obj_data,
253 struct lttng_ust_abi_object_data **_context_data)
254 {
255 struct ustcomm_ust_msg lum;
256 struct ustcomm_ust_reply lur;
257 struct lttng_ust_abi_object_data *context_data = NULL;
258 char *buf = NULL;
259 size_t len;
260 int ret;
261
262 if (!obj_data || !_context_data) {
263 ret = -EINVAL;
264 goto end;
265 }
266
267 context_data = zmalloc(sizeof(*context_data));
268 if (!context_data) {
269 ret = -ENOMEM;
270 goto end;
271 }
272 context_data->type = LTTNG_UST_ABI_OBJECT_TYPE_CONTEXT;
273 memset(&lum, 0, sizeof(lum));
274 lum.handle = obj_data->handle;
275 lum.cmd = LTTNG_UST_ABI_CONTEXT;
276
277 lum.u.context.ctx = ctx->ctx;
278 switch (ctx->ctx) {
279 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER:
280 lum.u.context.u.perf_counter = ctx->u.perf_counter;
281 break;
282 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT:
283 {
284 size_t provider_name_len = strlen(
285 ctx->u.app_ctx.provider_name) + 1;
286 size_t ctx_name_len = strlen(ctx->u.app_ctx.ctx_name) + 1;
287
288 lum.u.context.u.app_ctx.provider_name_len = provider_name_len;
289 lum.u.context.u.app_ctx.ctx_name_len = ctx_name_len;
290
291 len = provider_name_len + ctx_name_len;
292 buf = zmalloc(len);
293 if (!buf) {
294 ret = -ENOMEM;
295 goto end;
296 }
297 memcpy(buf, ctx->u.app_ctx.provider_name,
298 provider_name_len);
299 memcpy(buf + provider_name_len, ctx->u.app_ctx.ctx_name,
300 ctx_name_len);
301 break;
302 }
303 default:
304 break;
305 }
306 ret = ustcomm_send_app_msg(sock, &lum);
307 if (ret)
308 goto end;
309 if (buf) {
310 /* send var len ctx_name */
311 ret = ustcomm_send_unix_sock(sock, buf, len);
312 if (ret < 0) {
313 goto end;
314 }
315 if (ret != len) {
316 ret = -EINVAL;
317 goto end;
318 }
319 }
320 ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
321 if (ret < 0) {
322 goto end;
323 }
324 context_data->handle = -1;
325 DBG("Context created successfully");
326 *_context_data = context_data;
327 context_data = NULL;
328 end:
329 free(context_data);
330 free(buf);
331 return ret;
332 }
333
334 int ustctl_set_filter(int sock, struct lttng_ust_abi_filter_bytecode *bytecode,
335 struct lttng_ust_abi_object_data *obj_data)
336 {
337 struct ustcomm_ust_msg lum;
338 struct ustcomm_ust_reply lur;
339 int ret;
340
341 if (!obj_data)
342 return -EINVAL;
343
344 memset(&lum, 0, sizeof(lum));
345 lum.handle = obj_data->handle;
346 lum.cmd = LTTNG_UST_ABI_FILTER;
347 lum.u.filter.data_size = bytecode->len;
348 lum.u.filter.reloc_offset = bytecode->reloc_offset;
349 lum.u.filter.seqnum = bytecode->seqnum;
350
351 ret = ustcomm_send_app_msg(sock, &lum);
352 if (ret)
353 return ret;
354 /* send var len bytecode */
355 ret = ustcomm_send_unix_sock(sock, bytecode->data,
356 bytecode->len);
357 if (ret < 0) {
358 return ret;
359 }
360 if (ret != bytecode->len)
361 return -EINVAL;
362 return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
363 }
364
365 int ustctl_set_capture(int sock, struct lttng_ust_abi_capture_bytecode *bytecode,
366 struct lttng_ust_abi_object_data *obj_data)
367 {
368 struct ustcomm_ust_msg lum;
369 struct ustcomm_ust_reply lur;
370 int ret;
371
372 if (!obj_data)
373 return -EINVAL;
374
375 memset(&lum, 0, sizeof(lum));
376 lum.handle = obj_data->handle;
377 lum.cmd = LTTNG_UST_ABI_CAPTURE;
378 lum.u.capture.data_size = bytecode->len;
379 lum.u.capture.reloc_offset = bytecode->reloc_offset;
380 lum.u.capture.seqnum = bytecode->seqnum;
381
382 ret = ustcomm_send_app_msg(sock, &lum);
383 if (ret)
384 return ret;
385 /* send var len bytecode */
386 ret = ustcomm_send_unix_sock(sock, bytecode->data,
387 bytecode->len);
388 if (ret < 0) {
389 return ret;
390 }
391 if (ret != bytecode->len)
392 return -EINVAL;
393 return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
394 }
395
396 int ustctl_set_exclusion(int sock, struct lttng_ust_abi_event_exclusion *exclusion,
397 struct lttng_ust_abi_object_data *obj_data)
398 {
399 struct ustcomm_ust_msg lum;
400 struct ustcomm_ust_reply lur;
401 int ret;
402
403 if (!obj_data) {
404 return -EINVAL;
405 }
406
407 memset(&lum, 0, sizeof(lum));
408 lum.handle = obj_data->handle;
409 lum.cmd = LTTNG_UST_ABI_EXCLUSION;
410 lum.u.exclusion.count = exclusion->count;
411
412 ret = ustcomm_send_app_msg(sock, &lum);
413 if (ret) {
414 return ret;
415 }
416
417 /* send var len exclusion names */
418 ret = ustcomm_send_unix_sock(sock,
419 exclusion->names,
420 exclusion->count * LTTNG_UST_ABI_SYM_NAME_LEN);
421 if (ret < 0) {
422 return ret;
423 }
424 if (ret != exclusion->count * LTTNG_UST_ABI_SYM_NAME_LEN) {
425 return -EINVAL;
426 }
427 return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
428 }
429
430 /* Enable event, channel and session ioctl */
431 int ustctl_enable(int sock, struct lttng_ust_abi_object_data *object)
432 {
433 struct ustcomm_ust_msg lum;
434 struct ustcomm_ust_reply lur;
435 int ret;
436
437 if (!object)
438 return -EINVAL;
439
440 memset(&lum, 0, sizeof(lum));
441 lum.handle = object->handle;
442 lum.cmd = LTTNG_UST_ABI_ENABLE;
443 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
444 if (ret)
445 return ret;
446 DBG("enabled handle %u", object->handle);
447 return 0;
448 }
449
450 /* Disable event, channel and session ioctl */
451 int ustctl_disable(int sock, struct lttng_ust_abi_object_data *object)
452 {
453 struct ustcomm_ust_msg lum;
454 struct ustcomm_ust_reply lur;
455 int ret;
456
457 if (!object)
458 return -EINVAL;
459
460 memset(&lum, 0, sizeof(lum));
461 lum.handle = object->handle;
462 lum.cmd = LTTNG_UST_ABI_DISABLE;
463 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
464 if (ret)
465 return ret;
466 DBG("disable handle %u", object->handle);
467 return 0;
468 }
469
470 int ustctl_start_session(int sock, int handle)
471 {
472 struct lttng_ust_abi_object_data obj;
473
474 obj.handle = handle;
475 return ustctl_enable(sock, &obj);
476 }
477
478 int ustctl_stop_session(int sock, int handle)
479 {
480 struct lttng_ust_abi_object_data obj;
481
482 obj.handle = handle;
483 return ustctl_disable(sock, &obj);
484 }
485
486 int ustctl_create_event_notifier_group(int sock, int pipe_fd,
487 struct lttng_ust_abi_object_data **_event_notifier_group_data)
488 {
489 struct lttng_ust_abi_object_data *event_notifier_group_data;
490 struct ustcomm_ust_msg lum;
491 struct ustcomm_ust_reply lur;
492 ssize_t len;
493 int ret;
494
495 if (!_event_notifier_group_data)
496 return -EINVAL;
497
498 event_notifier_group_data = zmalloc(sizeof(*event_notifier_group_data));
499 if (!event_notifier_group_data)
500 return -ENOMEM;
501
502 event_notifier_group_data->type = LTTNG_UST_ABI_OBJECT_TYPE_EVENT_NOTIFIER_GROUP;
503
504 memset(&lum, 0, sizeof(lum));
505 lum.handle = LTTNG_UST_ABI_ROOT_HANDLE;
506 lum.cmd = LTTNG_UST_ABI_EVENT_NOTIFIER_GROUP_CREATE;
507
508 ret = ustcomm_send_app_msg(sock, &lum);
509 if (ret)
510 goto error;
511
512 /* Send event_notifier notification pipe. */
513 len = ustcomm_send_fds_unix_sock(sock, &pipe_fd, 1);
514 if (len <= 0) {
515 ret = len;
516 goto error;
517 }
518
519 ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
520 if (ret)
521 goto error;
522
523 event_notifier_group_data->handle = lur.ret_val;
524 DBG("received event_notifier group handle %d", event_notifier_group_data->handle);
525
526 *_event_notifier_group_data = event_notifier_group_data;
527
528 ret = 0;
529 goto end;
530 error:
531 free(event_notifier_group_data);
532
533 end:
534 return ret;
535 }
536
537 int ustctl_create_event_notifier(int sock, struct lttng_ust_abi_event_notifier *event_notifier,
538 struct lttng_ust_abi_object_data *event_notifier_group,
539 struct lttng_ust_abi_object_data **_event_notifier_data)
540 {
541 struct ustcomm_ust_msg lum;
542 struct ustcomm_ust_reply lur;
543 struct lttng_ust_abi_object_data *event_notifier_data;
544 ssize_t len;
545 int ret;
546
547 if (!event_notifier_group || !_event_notifier_data)
548 return -EINVAL;
549
550 event_notifier_data = zmalloc(sizeof(*event_notifier_data));
551 if (!event_notifier_data)
552 return -ENOMEM;
553
554 event_notifier_data->type = LTTNG_UST_ABI_OBJECT_TYPE_EVENT_NOTIFIER;
555
556 memset(&lum, 0, sizeof(lum));
557 lum.handle = event_notifier_group->handle;
558 lum.cmd = LTTNG_UST_ABI_EVENT_NOTIFIER_CREATE;
559 lum.u.event_notifier.len = sizeof(*event_notifier);
560
561 ret = ustcomm_send_app_msg(sock, &lum);
562 if (ret) {
563 free(event_notifier_data);
564 return ret;
565 }
566 /* Send struct lttng_ust_abi_event_notifier */
567 len = ustcomm_send_unix_sock(sock, event_notifier, sizeof(*event_notifier));
568 if (len != sizeof(*event_notifier)) {
569 free(event_notifier_data);
570 if (len < 0)
571 return len;
572 else
573 return -EIO;
574 }
575 ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
576 if (ret) {
577 free(event_notifier_data);
578 return ret;
579 }
580 event_notifier_data->handle = lur.ret_val;
581 DBG("received event_notifier handle %u", event_notifier_data->handle);
582 *_event_notifier_data = event_notifier_data;
583
584 return ret;
585 }
586
587 int ustctl_tracepoint_list(int sock)
588 {
589 struct ustcomm_ust_msg lum;
590 struct ustcomm_ust_reply lur;
591 int ret, tp_list_handle;
592
593 memset(&lum, 0, sizeof(lum));
594 lum.handle = LTTNG_UST_ABI_ROOT_HANDLE;
595 lum.cmd = LTTNG_UST_ABI_TRACEPOINT_LIST;
596 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
597 if (ret)
598 return ret;
599 tp_list_handle = lur.ret_val;
600 DBG("received tracepoint list handle %u", tp_list_handle);
601 return tp_list_handle;
602 }
603
604 int ustctl_tracepoint_list_get(int sock, int tp_list_handle,
605 struct lttng_ust_abi_tracepoint_iter *iter)
606 {
607 struct ustcomm_ust_msg lum;
608 struct ustcomm_ust_reply lur;
609 int ret;
610
611 if (!iter)
612 return -EINVAL;
613
614 memset(&lum, 0, sizeof(lum));
615 lum.handle = tp_list_handle;
616 lum.cmd = LTTNG_UST_ABI_TRACEPOINT_LIST_GET;
617 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
618 if (ret)
619 return ret;
620 DBG("received tracepoint list entry name %s loglevel %d",
621 lur.u.tracepoint.name,
622 lur.u.tracepoint.loglevel);
623 memcpy(iter, &lur.u.tracepoint, sizeof(*iter));
624 return 0;
625 }
626
627 int ustctl_tracepoint_field_list(int sock)
628 {
629 struct ustcomm_ust_msg lum;
630 struct ustcomm_ust_reply lur;
631 int ret, tp_field_list_handle;
632
633 memset(&lum, 0, sizeof(lum));
634 lum.handle = LTTNG_UST_ABI_ROOT_HANDLE;
635 lum.cmd = LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST;
636 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
637 if (ret)
638 return ret;
639 tp_field_list_handle = lur.ret_val;
640 DBG("received tracepoint field list handle %u", tp_field_list_handle);
641 return tp_field_list_handle;
642 }
643
644 int ustctl_tracepoint_field_list_get(int sock, int tp_field_list_handle,
645 struct lttng_ust_abi_field_iter *iter)
646 {
647 struct ustcomm_ust_msg lum;
648 struct ustcomm_ust_reply lur;
649 int ret;
650 ssize_t len;
651
652 if (!iter)
653 return -EINVAL;
654
655 memset(&lum, 0, sizeof(lum));
656 lum.handle = tp_field_list_handle;
657 lum.cmd = LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST_GET;
658 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
659 if (ret)
660 return ret;
661 len = ustcomm_recv_unix_sock(sock, iter, sizeof(*iter));
662 if (len != sizeof(*iter)) {
663 return -EINVAL;
664 }
665 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
666 iter->event_name,
667 iter->loglevel,
668 iter->field_name,
669 iter->type);
670 return 0;
671 }
672
673 int ustctl_tracer_version(int sock, struct lttng_ust_abi_tracer_version *v)
674 {
675 struct ustcomm_ust_msg lum;
676 struct ustcomm_ust_reply lur;
677 int ret;
678
679 if (!v)
680 return -EINVAL;
681
682 memset(&lum, 0, sizeof(lum));
683 lum.handle = LTTNG_UST_ABI_ROOT_HANDLE;
684 lum.cmd = LTTNG_UST_ABI_TRACER_VERSION;
685 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
686 if (ret)
687 return ret;
688 memcpy(v, &lur.u.version, sizeof(*v));
689 DBG("received tracer version");
690 return 0;
691 }
692
693 int ustctl_wait_quiescent(int sock)
694 {
695 struct ustcomm_ust_msg lum;
696 struct ustcomm_ust_reply lur;
697 int ret;
698
699 memset(&lum, 0, sizeof(lum));
700 lum.handle = LTTNG_UST_ABI_ROOT_HANDLE;
701 lum.cmd = LTTNG_UST_ABI_WAIT_QUIESCENT;
702 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
703 if (ret)
704 return ret;
705 DBG("waited for quiescent state");
706 return 0;
707 }
708
709 int ustctl_calibrate(int sock __attribute__((unused)),
710 struct lttng_ust_abi_calibrate *calibrate)
711 {
712 if (!calibrate)
713 return -EINVAL;
714
715 return -ENOSYS;
716 }
717
718 int ustctl_sock_flush_buffer(int sock, struct lttng_ust_abi_object_data *object)
719 {
720 struct ustcomm_ust_msg lum;
721 struct ustcomm_ust_reply lur;
722 int ret;
723
724 if (!object)
725 return -EINVAL;
726
727 memset(&lum, 0, sizeof(lum));
728 lum.handle = object->handle;
729 lum.cmd = LTTNG_UST_ABI_FLUSH_BUFFER;
730 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
731 if (ret)
732 return ret;
733 DBG("flushed buffer handle %u", object->handle);
734 return 0;
735 }
736
737 static
738 int ustctl_send_channel(int sock,
739 enum lttng_ust_abi_chan_type type,
740 void *data,
741 uint64_t size,
742 int wakeup_fd,
743 int send_fd_only)
744 {
745 ssize_t len;
746
747 if (!send_fd_only) {
748 /* Send mmap size */
749 len = ustcomm_send_unix_sock(sock, &size, sizeof(size));
750 if (len != sizeof(size)) {
751 if (len < 0)
752 return len;
753 else
754 return -EIO;
755 }
756
757 /* Send channel type */
758 len = ustcomm_send_unix_sock(sock, &type, sizeof(type));
759 if (len != sizeof(type)) {
760 if (len < 0)
761 return len;
762 else
763 return -EIO;
764 }
765 }
766
767 /* Send channel data */
768 len = ustcomm_send_unix_sock(sock, data, size);
769 if (len != size) {
770 if (len < 0)
771 return len;
772 else
773 return -EIO;
774 }
775
776 /* Send wakeup fd */
777 len = ustcomm_send_fds_unix_sock(sock, &wakeup_fd, 1);
778 if (len <= 0) {
779 if (len < 0)
780 return len;
781 else
782 return -EIO;
783 }
784 return 0;
785 }
786
787 static
788 int ustctl_send_stream(int sock,
789 uint32_t stream_nr,
790 uint64_t memory_map_size,
791 int shm_fd, int wakeup_fd,
792 int send_fd_only)
793 {
794 ssize_t len;
795 int fds[2];
796
797 if (!send_fd_only) {
798 if (shm_fd < 0) {
799 /* finish iteration */
800 uint64_t v = -1;
801
802 len = ustcomm_send_unix_sock(sock, &v, sizeof(v));
803 if (len != sizeof(v)) {
804 if (len < 0)
805 return len;
806 else
807 return -EIO;
808 }
809 return 0;
810 }
811
812 /* Send mmap size */
813 len = ustcomm_send_unix_sock(sock, &memory_map_size,
814 sizeof(memory_map_size));
815 if (len != sizeof(memory_map_size)) {
816 if (len < 0)
817 return len;
818 else
819 return -EIO;
820 }
821
822 /* Send stream nr */
823 len = ustcomm_send_unix_sock(sock, &stream_nr,
824 sizeof(stream_nr));
825 if (len != sizeof(stream_nr)) {
826 if (len < 0)
827 return len;
828 else
829 return -EIO;
830 }
831 }
832
833 /* Send shm fd and wakeup fd */
834 fds[0] = shm_fd;
835 fds[1] = wakeup_fd;
836 len = ustcomm_send_fds_unix_sock(sock, fds, 2);
837 if (len <= 0) {
838 if (len < 0)
839 return len;
840 else
841 return -EIO;
842 }
843 return 0;
844 }
845
846 int ustctl_recv_channel_from_consumer(int sock,
847 struct lttng_ust_abi_object_data **_channel_data)
848 {
849 struct lttng_ust_abi_object_data *channel_data;
850 ssize_t len;
851 int wakeup_fd;
852 int ret;
853
854 channel_data = zmalloc(sizeof(*channel_data));
855 if (!channel_data) {
856 ret = -ENOMEM;
857 goto error_alloc;
858 }
859 channel_data->type = LTTNG_UST_ABI_OBJECT_TYPE_CHANNEL;
860 channel_data->handle = -1;
861
862 /* recv mmap size */
863 len = ustcomm_recv_unix_sock(sock, &channel_data->size,
864 sizeof(channel_data->size));
865 if (len != sizeof(channel_data->size)) {
866 if (len < 0)
867 ret = len;
868 else
869 ret = -EINVAL;
870 goto error;
871 }
872
873 /* recv channel type */
874 len = ustcomm_recv_unix_sock(sock, &channel_data->u.channel.type,
875 sizeof(channel_data->u.channel.type));
876 if (len != sizeof(channel_data->u.channel.type)) {
877 if (len < 0)
878 ret = len;
879 else
880 ret = -EINVAL;
881 goto error;
882 }
883
884 /* recv channel data */
885 channel_data->u.channel.data = zmalloc(channel_data->size);
886 if (!channel_data->u.channel.data) {
887 ret = -ENOMEM;
888 goto error;
889 }
890 len = ustcomm_recv_unix_sock(sock, channel_data->u.channel.data,
891 channel_data->size);
892 if (len != channel_data->size) {
893 if (len < 0)
894 ret = len;
895 else
896 ret = -EINVAL;
897 goto error_recv_data;
898 }
899 /* recv wakeup fd */
900 len = ustcomm_recv_fds_unix_sock(sock, &wakeup_fd, 1);
901 if (len <= 0) {
902 if (len < 0) {
903 ret = len;
904 goto error_recv_data;
905 } else {
906 ret = -EIO;
907 goto error_recv_data;
908 }
909 }
910 channel_data->u.channel.wakeup_fd = wakeup_fd;
911 *_channel_data = channel_data;
912 return 0;
913
914 error_recv_data:
915 free(channel_data->u.channel.data);
916 error:
917 free(channel_data);
918 error_alloc:
919 return ret;
920 }
921
922 int ustctl_recv_stream_from_consumer(int sock,
923 struct lttng_ust_abi_object_data **_stream_data)
924 {
925 struct lttng_ust_abi_object_data *stream_data;
926 ssize_t len;
927 int ret;
928 int fds[2];
929
930 stream_data = zmalloc(sizeof(*stream_data));
931 if (!stream_data) {
932 ret = -ENOMEM;
933 goto error_alloc;
934 }
935
936 stream_data->type = LTTNG_UST_ABI_OBJECT_TYPE_STREAM;
937 stream_data->handle = -1;
938
939 /* recv mmap size */
940 len = ustcomm_recv_unix_sock(sock, &stream_data->size,
941 sizeof(stream_data->size));
942 if (len != sizeof(stream_data->size)) {
943 if (len < 0)
944 ret = len;
945 else
946 ret = -EINVAL;
947 goto error;
948 }
949 if (stream_data->size == -1) {
950 ret = -LTTNG_UST_ERR_NOENT;
951 goto error;
952 }
953
954 /* recv stream nr */
955 len = ustcomm_recv_unix_sock(sock, &stream_data->u.stream.stream_nr,
956 sizeof(stream_data->u.stream.stream_nr));
957 if (len != sizeof(stream_data->u.stream.stream_nr)) {
958 if (len < 0)
959 ret = len;
960 else
961 ret = -EINVAL;
962 goto error;
963 }
964
965 /* recv shm fd and wakeup fd */
966 len = ustcomm_recv_fds_unix_sock(sock, fds, 2);
967 if (len <= 0) {
968 if (len < 0) {
969 ret = len;
970 goto error;
971 } else {
972 ret = -EIO;
973 goto error;
974 }
975 }
976 stream_data->u.stream.shm_fd = fds[0];
977 stream_data->u.stream.wakeup_fd = fds[1];
978 *_stream_data = stream_data;
979 return 0;
980
981 error:
982 free(stream_data);
983 error_alloc:
984 return ret;
985 }
986
987 int ustctl_send_channel_to_ust(int sock, int session_handle,
988 struct lttng_ust_abi_object_data *channel_data)
989 {
990 struct ustcomm_ust_msg lum;
991 struct ustcomm_ust_reply lur;
992 int ret;
993
994 if (!channel_data)
995 return -EINVAL;
996
997 memset(&lum, 0, sizeof(lum));
998 lum.handle = session_handle;
999 lum.cmd = LTTNG_UST_ABI_CHANNEL;
1000 lum.u.channel.len = channel_data->size;
1001 lum.u.channel.type = channel_data->u.channel.type;
1002 ret = ustcomm_send_app_msg(sock, &lum);
1003 if (ret)
1004 return ret;
1005
1006 ret = ustctl_send_channel(sock,
1007 channel_data->u.channel.type,
1008 channel_data->u.channel.data,
1009 channel_data->size,
1010 channel_data->u.channel.wakeup_fd,
1011 1);
1012 if (ret)
1013 return ret;
1014 ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
1015 if (!ret) {
1016 channel_data->handle = lur.ret_val;
1017 }
1018 return ret;
1019 }
1020
1021 int ustctl_send_stream_to_ust(int sock,
1022 struct lttng_ust_abi_object_data *channel_data,
1023 struct lttng_ust_abi_object_data *stream_data)
1024 {
1025 struct ustcomm_ust_msg lum;
1026 struct ustcomm_ust_reply lur;
1027 int ret;
1028
1029 memset(&lum, 0, sizeof(lum));
1030 lum.handle = channel_data->handle;
1031 lum.cmd = LTTNG_UST_ABI_STREAM;
1032 lum.u.stream.len = stream_data->size;
1033 lum.u.stream.stream_nr = stream_data->u.stream.stream_nr;
1034 ret = ustcomm_send_app_msg(sock, &lum);
1035 if (ret)
1036 return ret;
1037
1038 assert(stream_data);
1039 assert(stream_data->type == LTTNG_UST_ABI_OBJECT_TYPE_STREAM);
1040
1041 ret = ustctl_send_stream(sock,
1042 stream_data->u.stream.stream_nr,
1043 stream_data->size,
1044 stream_data->u.stream.shm_fd,
1045 stream_data->u.stream.wakeup_fd, 1);
1046 if (ret)
1047 return ret;
1048 return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
1049 }
1050
1051 int ustctl_duplicate_ust_object_data(struct lttng_ust_abi_object_data **dest,
1052 struct lttng_ust_abi_object_data *src)
1053 {
1054 struct lttng_ust_abi_object_data *obj;
1055 int ret;
1056
1057 if (src->handle != -1) {
1058 ret = -EINVAL;
1059 goto error;
1060 }
1061
1062 obj = zmalloc(sizeof(*obj));
1063 if (!obj) {
1064 ret = -ENOMEM;
1065 goto error;
1066 }
1067
1068 obj->type = src->type;
1069 obj->handle = src->handle;
1070 obj->size = src->size;
1071
1072 switch (obj->type) {
1073 case LTTNG_UST_ABI_OBJECT_TYPE_CHANNEL:
1074 {
1075 obj->u.channel.type = src->u.channel.type;
1076 if (src->u.channel.wakeup_fd >= 0) {
1077 obj->u.channel.wakeup_fd =
1078 dup(src->u.channel.wakeup_fd);
1079 if (obj->u.channel.wakeup_fd < 0) {
1080 ret = errno;
1081 goto chan_error_wakeup_fd;
1082 }
1083 } else {
1084 obj->u.channel.wakeup_fd =
1085 src->u.channel.wakeup_fd;
1086 }
1087 obj->u.channel.data = zmalloc(obj->size);
1088 if (!obj->u.channel.data) {
1089 ret = -ENOMEM;
1090 goto chan_error_alloc;
1091 }
1092 memcpy(obj->u.channel.data, src->u.channel.data, obj->size);
1093 break;
1094
1095 chan_error_alloc:
1096 if (src->u.channel.wakeup_fd >= 0) {
1097 int closeret;
1098
1099 closeret = close(obj->u.channel.wakeup_fd);
1100 if (closeret) {
1101 PERROR("close");
1102 }
1103 }
1104 chan_error_wakeup_fd:
1105 goto error_type;
1106
1107 }
1108
1109 case LTTNG_UST_ABI_OBJECT_TYPE_STREAM:
1110 {
1111 obj->u.stream.stream_nr = src->u.stream.stream_nr;
1112 if (src->u.stream.wakeup_fd >= 0) {
1113 obj->u.stream.wakeup_fd =
1114 dup(src->u.stream.wakeup_fd);
1115 if (obj->u.stream.wakeup_fd < 0) {
1116 ret = errno;
1117 goto stream_error_wakeup_fd;
1118 }
1119 } else {
1120 obj->u.stream.wakeup_fd =
1121 src->u.stream.wakeup_fd;
1122 }
1123
1124 if (src->u.stream.shm_fd >= 0) {
1125 obj->u.stream.shm_fd =
1126 dup(src->u.stream.shm_fd);
1127 if (obj->u.stream.shm_fd < 0) {
1128 ret = errno;
1129 goto stream_error_shm_fd;
1130 }
1131 } else {
1132 obj->u.stream.shm_fd =
1133 src->u.stream.shm_fd;
1134 }
1135 break;
1136
1137 stream_error_shm_fd:
1138 if (src->u.stream.wakeup_fd >= 0) {
1139 int closeret;
1140
1141 closeret = close(obj->u.stream.wakeup_fd);
1142 if (closeret) {
1143 PERROR("close");
1144 }
1145 }
1146 stream_error_wakeup_fd:
1147 goto error_type;
1148 }
1149
1150 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER:
1151 {
1152 obj->u.counter.data = zmalloc(obj->size);
1153 if (!obj->u.counter.data) {
1154 ret = -ENOMEM;
1155 goto error_type;
1156 }
1157 memcpy(obj->u.counter.data, src->u.counter.data, obj->size);
1158 break;
1159 }
1160
1161 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_GLOBAL:
1162 {
1163 if (src->u.counter_global.shm_fd >= 0) {
1164 obj->u.counter_global.shm_fd =
1165 dup(src->u.counter_global.shm_fd);
1166 if (obj->u.counter_global.shm_fd < 0) {
1167 ret = errno;
1168 goto error_type;
1169 }
1170 }
1171 break;
1172 }
1173
1174 case LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_CPU:
1175 {
1176 obj->u.counter_cpu.cpu_nr = src->u.counter_cpu.cpu_nr;
1177 if (src->u.counter_cpu.shm_fd >= 0) {
1178 obj->u.counter_cpu.shm_fd =
1179 dup(src->u.counter_cpu.shm_fd);
1180 if (obj->u.counter_cpu.shm_fd < 0) {
1181 ret = errno;
1182 goto error_type;
1183 }
1184 }
1185 break;
1186 }
1187
1188 default:
1189 ret = -EINVAL;
1190 goto error_type;
1191 }
1192
1193 *dest = obj;
1194 return 0;
1195
1196 error_type:
1197 free(obj);
1198 error:
1199 return ret;
1200 }
1201
1202
1203 /* Buffer operations */
1204
1205 int ustctl_get_nr_stream_per_channel(void)
1206 {
1207 return num_possible_cpus();
1208 }
1209
1210 struct ustctl_consumer_channel *
1211 ustctl_create_channel(struct ustctl_consumer_channel_attr *attr,
1212 const int *stream_fds, int nr_stream_fds)
1213 {
1214 struct ustctl_consumer_channel *chan;
1215 const char *transport_name;
1216 struct lttng_transport *transport;
1217
1218 switch (attr->type) {
1219 case LTTNG_UST_ABI_CHAN_PER_CPU:
1220 if (attr->output == LTTNG_UST_ABI_MMAP) {
1221 if (attr->overwrite) {
1222 if (attr->read_timer_interval == 0) {
1223 transport_name = "relay-overwrite-mmap";
1224 } else {
1225 transport_name = "relay-overwrite-rt-mmap";
1226 }
1227 } else {
1228 if (attr->read_timer_interval == 0) {
1229 transport_name = "relay-discard-mmap";
1230 } else {
1231 transport_name = "relay-discard-rt-mmap";
1232 }
1233 }
1234 } else {
1235 return NULL;
1236 }
1237 break;
1238 case LTTNG_UST_ABI_CHAN_METADATA:
1239 if (attr->output == LTTNG_UST_ABI_MMAP)
1240 transport_name = "relay-metadata-mmap";
1241 else
1242 return NULL;
1243 break;
1244 default:
1245 transport_name = "<unknown>";
1246 return NULL;
1247 }
1248
1249 transport = lttng_ust_transport_find(transport_name);
1250 if (!transport) {
1251 DBG("LTTng transport %s not found\n",
1252 transport_name);
1253 return NULL;
1254 }
1255
1256 chan = zmalloc(sizeof(*chan));
1257 if (!chan)
1258 return NULL;
1259
1260 chan->chan = transport->ops.priv->channel_create(transport_name, NULL,
1261 attr->subbuf_size, attr->num_subbuf,
1262 attr->switch_timer_interval,
1263 attr->read_timer_interval,
1264 attr->uuid, attr->chan_id,
1265 stream_fds, nr_stream_fds,
1266 attr->blocking_timeout);
1267 if (!chan->chan) {
1268 goto chan_error;
1269 }
1270 chan->chan->ops = &transport->ops;
1271 memcpy(&chan->attr, attr, sizeof(chan->attr));
1272 chan->wait_fd = ustctl_channel_get_wait_fd(chan);
1273 chan->wakeup_fd = ustctl_channel_get_wakeup_fd(chan);
1274 return chan;
1275
1276 chan_error:
1277 free(chan);
1278 return NULL;
1279 }
1280
1281 void ustctl_destroy_channel(struct ustctl_consumer_channel *chan)
1282 {
1283 (void) ustctl_channel_close_wait_fd(chan);
1284 (void) ustctl_channel_close_wakeup_fd(chan);
1285 chan->chan->ops->priv->channel_destroy(chan->chan);
1286 free(chan);
1287 }
1288
1289 int ustctl_send_channel_to_sessiond(int sock,
1290 struct ustctl_consumer_channel *channel)
1291 {
1292 struct shm_object_table *table;
1293
1294 table = channel->chan->priv->rb_chan->handle->table;
1295 if (table->size <= 0)
1296 return -EINVAL;
1297 return ustctl_send_channel(sock,
1298 channel->attr.type,
1299 table->objects[0].memory_map,
1300 table->objects[0].memory_map_size,
1301 channel->wakeup_fd,
1302 0);
1303 }
1304
1305 int ustctl_send_stream_to_sessiond(int sock,
1306 struct ustctl_consumer_stream *stream)
1307 {
1308 if (!stream)
1309 return ustctl_send_stream(sock, -1U, -1U, -1, -1, 0);
1310
1311 return ustctl_send_stream(sock,
1312 stream->cpu,
1313 stream->memory_map_size,
1314 stream->shm_fd, stream->wakeup_fd,
1315 0);
1316 }
1317
1318 int ustctl_write_metadata_to_channel(
1319 struct ustctl_consumer_channel *channel,
1320 const char *metadata_str, /* NOT null-terminated */
1321 size_t len) /* metadata length */
1322 {
1323 struct lttng_ust_lib_ring_buffer_ctx ctx;
1324 struct lttng_ust_channel_buffer *lttng_chan_buf = channel->chan;
1325 struct lttng_ust_lib_ring_buffer_channel *rb_chan = lttng_chan_buf->priv->rb_chan;
1326 const char *str = metadata_str;
1327 int ret = 0, waitret;
1328 size_t reserve_len, pos;
1329
1330 for (pos = 0; pos < len; pos += reserve_len) {
1331 reserve_len = min_t(size_t,
1332 lttng_chan_buf->ops->priv->packet_avail_size(lttng_chan_buf),
1333 len - pos);
1334 lttng_ust_lib_ring_buffer_ctx_init(&ctx, rb_chan, reserve_len, sizeof(char), NULL);
1335 /*
1336 * We don't care about metadata buffer's records lost
1337 * count, because we always retry here. Report error if
1338 * we need to bail out after timeout or being
1339 * interrupted.
1340 */
1341 waitret = wait_cond_interruptible_timeout(
1342 ({
1343 ret = lttng_chan_buf->ops->event_reserve(&ctx);
1344 ret != -ENOBUFS || !ret;
1345 }),
1346 LTTNG_METADATA_TIMEOUT_MSEC);
1347 if (waitret == -ETIMEDOUT || waitret == -EINTR || ret) {
1348 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
1349 waitret == -EINTR ? "interrupted" :
1350 (ret == -ENOBUFS ? "timeout" : "I/O error"));
1351 if (waitret == -EINTR)
1352 ret = waitret;
1353 goto end;
1354 }
1355 lttng_chan_buf->ops->event_write(&ctx, &str[pos], reserve_len, 1);
1356 lttng_chan_buf->ops->event_commit(&ctx);
1357 }
1358 end:
1359 return ret;
1360 }
1361
1362 /*
1363 * Write at most one packet in the channel.
1364 * Returns the number of bytes written on success, < 0 on error.
1365 */
1366 ssize_t ustctl_write_one_packet_to_channel(
1367 struct ustctl_consumer_channel *channel,
1368 const char *metadata_str, /* NOT null-terminated */
1369 size_t len) /* metadata length */
1370 {
1371 struct lttng_ust_lib_ring_buffer_ctx ctx;
1372 struct lttng_ust_channel_buffer *lttng_chan_buf = channel->chan;
1373 struct lttng_ust_lib_ring_buffer_channel *rb_chan = lttng_chan_buf->priv->rb_chan;
1374 const char *str = metadata_str;
1375 ssize_t reserve_len;
1376 int ret;
1377
1378 reserve_len = min_t(ssize_t,
1379 lttng_chan_buf->ops->priv->packet_avail_size(lttng_chan_buf),
1380 len);
1381 lttng_ust_lib_ring_buffer_ctx_init(&ctx, rb_chan, reserve_len, sizeof(char), NULL);
1382 ret = lttng_chan_buf->ops->event_reserve(&ctx);
1383 if (ret != 0) {
1384 DBG("LTTng: event reservation failed");
1385 assert(ret < 0);
1386 reserve_len = ret;
1387 goto end;
1388 }
1389 lttng_chan_buf->ops->event_write(&ctx, str, reserve_len, 1);
1390 lttng_chan_buf->ops->event_commit(&ctx);
1391
1392 end:
1393 return reserve_len;
1394 }
1395
1396 int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel *consumer_chan)
1397 {
1398 struct lttng_ust_lib_ring_buffer_channel *chan;
1399 int ret;
1400
1401 chan = consumer_chan->chan->priv->rb_chan;
1402 ret = ring_buffer_channel_close_wait_fd(&chan->backend.config,
1403 chan, chan->handle);
1404 if (!ret)
1405 consumer_chan->wait_fd = -1;
1406 return ret;
1407 }
1408
1409 int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel *consumer_chan)
1410 {
1411 struct lttng_ust_lib_ring_buffer_channel *chan;
1412 int ret;
1413
1414 chan = consumer_chan->chan->priv->rb_chan;
1415 ret = ring_buffer_channel_close_wakeup_fd(&chan->backend.config,
1416 chan, chan->handle);
1417 if (!ret)
1418 consumer_chan->wakeup_fd = -1;
1419 return ret;
1420 }
1421
1422 int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream *stream)
1423 {
1424 struct lttng_ust_lib_ring_buffer_channel *chan;
1425
1426 chan = stream->chan->chan->priv->rb_chan;
1427 return ring_buffer_stream_close_wait_fd(&chan->backend.config,
1428 chan, chan->handle, stream->cpu);
1429 }
1430
1431 int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream *stream)
1432 {
1433 struct lttng_ust_lib_ring_buffer_channel *chan;
1434
1435 chan = stream->chan->chan->priv->rb_chan;
1436 return ring_buffer_stream_close_wakeup_fd(&chan->backend.config,
1437 chan, chan->handle, stream->cpu);
1438 }
1439
1440 struct ustctl_consumer_stream *
1441 ustctl_create_stream(struct ustctl_consumer_channel *channel,
1442 int cpu)
1443 {
1444 struct ustctl_consumer_stream *stream;
1445 struct lttng_ust_shm_handle *handle;
1446 struct lttng_ust_lib_ring_buffer_channel *rb_chan;
1447 int shm_fd, wait_fd, wakeup_fd;
1448 uint64_t memory_map_size;
1449 struct lttng_ust_lib_ring_buffer *buf;
1450 int ret;
1451
1452 if (!channel)
1453 return NULL;
1454 rb_chan = channel->chan->priv->rb_chan;
1455 handle = rb_chan->handle;
1456 if (!handle)
1457 return NULL;
1458
1459 buf = channel_get_ring_buffer(&rb_chan->backend.config,
1460 rb_chan, cpu, handle, &shm_fd, &wait_fd,
1461 &wakeup_fd, &memory_map_size);
1462 if (!buf)
1463 return NULL;
1464 ret = lib_ring_buffer_open_read(buf, handle);
1465 if (ret)
1466 return NULL;
1467
1468 stream = zmalloc(sizeof(*stream));
1469 if (!stream)
1470 goto alloc_error;
1471 stream->buf = buf;
1472 stream->chan = channel;
1473 stream->shm_fd = shm_fd;
1474 stream->wait_fd = wait_fd;
1475 stream->wakeup_fd = wakeup_fd;
1476 stream->memory_map_size = memory_map_size;
1477 stream->cpu = cpu;
1478 return stream;
1479
1480 alloc_error:
1481 return NULL;
1482 }
1483
1484 void ustctl_destroy_stream(struct ustctl_consumer_stream *stream)
1485 {
1486 struct lttng_ust_lib_ring_buffer *buf;
1487 struct ustctl_consumer_channel *consumer_chan;
1488
1489 assert(stream);
1490 buf = stream->buf;
1491 consumer_chan = stream->chan;
1492 (void) ustctl_stream_close_wait_fd(stream);
1493 (void) ustctl_stream_close_wakeup_fd(stream);
1494 lib_ring_buffer_release_read(buf, consumer_chan->chan->priv->rb_chan->handle);
1495 free(stream);
1496 }
1497
1498 int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel *chan)
1499 {
1500 if (!chan)
1501 return -EINVAL;
1502 return shm_get_wait_fd(chan->chan->priv->rb_chan->handle,
1503 &chan->chan->priv->rb_chan->handle->chan._ref);
1504 }
1505
1506 int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel *chan)
1507 {
1508 if (!chan)
1509 return -EINVAL;
1510 return shm_get_wakeup_fd(chan->chan->priv->rb_chan->handle,
1511 &chan->chan->priv->rb_chan->handle->chan._ref);
1512 }
1513
1514 int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream *stream)
1515 {
1516 struct lttng_ust_lib_ring_buffer *buf;
1517 struct ustctl_consumer_channel *consumer_chan;
1518
1519 if (!stream)
1520 return -EINVAL;
1521 buf = stream->buf;
1522 consumer_chan = stream->chan;
1523 return shm_get_wait_fd(consumer_chan->chan->priv->rb_chan->handle, &buf->self._ref);
1524 }
1525
1526 int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream *stream)
1527 {
1528 struct lttng_ust_lib_ring_buffer *buf;
1529 struct ustctl_consumer_channel *consumer_chan;
1530
1531 if (!stream)
1532 return -EINVAL;
1533 buf = stream->buf;
1534 consumer_chan = stream->chan;
1535 return shm_get_wakeup_fd(consumer_chan->chan->priv->rb_chan->handle, &buf->self._ref);
1536 }
1537
1538 /* For mmap mode, readable without "get" operation */
1539
1540 void *ustctl_get_mmap_base(struct ustctl_consumer_stream *stream)
1541 {
1542 struct lttng_ust_lib_ring_buffer *buf;
1543 struct ustctl_consumer_channel *consumer_chan;
1544
1545 if (!stream)
1546 return NULL;
1547 buf = stream->buf;
1548 consumer_chan = stream->chan;
1549 return shmp(consumer_chan->chan->priv->rb_chan->handle, buf->backend.memory_map);
1550 }
1551
1552 /* returns the length to mmap. */
1553 int ustctl_get_mmap_len(struct ustctl_consumer_stream *stream,
1554 unsigned long *len)
1555 {
1556 struct ustctl_consumer_channel *consumer_chan;
1557 unsigned long mmap_buf_len;
1558 struct lttng_ust_lib_ring_buffer_channel *rb_chan;
1559
1560 if (!stream)
1561 return -EINVAL;
1562 consumer_chan = stream->chan;
1563 rb_chan = consumer_chan->chan->priv->rb_chan;
1564 if (rb_chan->backend.config.output != RING_BUFFER_MMAP)
1565 return -EINVAL;
1566 mmap_buf_len = rb_chan->backend.buf_size;
1567 if (rb_chan->backend.extra_reader_sb)
1568 mmap_buf_len += rb_chan->backend.subbuf_size;
1569 if (mmap_buf_len > INT_MAX)
1570 return -EFBIG;
1571 *len = mmap_buf_len;
1572 return 0;
1573 }
1574
1575 /* returns the maximum size for sub-buffers. */
1576 int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream *stream,
1577 unsigned long *len)
1578 {
1579 struct ustctl_consumer_channel *consumer_chan;
1580 struct lttng_ust_lib_ring_buffer_channel *rb_chan;
1581
1582 if (!stream)
1583 return -EINVAL;
1584 consumer_chan = stream->chan;
1585 rb_chan = consumer_chan->chan->priv->rb_chan;
1586 *len = rb_chan->backend.subbuf_size;
1587 return 0;
1588 }
1589
1590 /*
1591 * For mmap mode, operate on the current packet (between get/put or
1592 * get_next/put_next).
1593 */
1594
1595 /* returns the offset of the subbuffer belonging to the mmap reader. */
1596 int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream *stream,
1597 unsigned long *off)
1598 {
1599 struct lttng_ust_lib_ring_buffer_channel *rb_chan;
1600 unsigned long sb_bindex;
1601 struct lttng_ust_lib_ring_buffer *buf;
1602 struct ustctl_consumer_channel *consumer_chan;
1603 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *barray_idx;
1604 struct lttng_ust_lib_ring_buffer_backend_pages *pages;
1605
1606 if (!stream)
1607 return -EINVAL;
1608 buf = stream->buf;
1609 consumer_chan = stream->chan;
1610 rb_chan = consumer_chan->chan->priv->rb_chan;
1611 if (rb_chan->backend.config.output != RING_BUFFER_MMAP)
1612 return -EINVAL;
1613 sb_bindex = subbuffer_id_get_index(&rb_chan->backend.config,
1614 buf->backend.buf_rsb.id);
1615 barray_idx = shmp_index(rb_chan->handle, buf->backend.array,
1616 sb_bindex);
1617 if (!barray_idx)
1618 return -EINVAL;
1619 pages = shmp(rb_chan->handle, barray_idx->shmp);
1620 if (!pages)
1621 return -EINVAL;
1622 *off = pages->mmap_offset;
1623 return 0;
1624 }
1625
1626 /* returns the size of the current sub-buffer, without padding (for mmap). */
1627 int ustctl_get_subbuf_size(struct ustctl_consumer_stream *stream,
1628 unsigned long *len)
1629 {
1630 struct ustctl_consumer_channel *consumer_chan;
1631 struct lttng_ust_lib_ring_buffer_channel *rb_chan;
1632 struct lttng_ust_lib_ring_buffer *buf;
1633
1634 if (!stream)
1635 return -EINVAL;
1636
1637 buf = stream->buf;
1638 consumer_chan = stream->chan;
1639 rb_chan = consumer_chan->chan->priv->rb_chan;
1640 *len = lib_ring_buffer_get_read_data_size(&rb_chan->backend.config, buf,
1641 rb_chan->handle);
1642 return 0;
1643 }
1644
1645 /* returns the size of the current sub-buffer, without padding (for mmap). */
1646 int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream *stream,
1647 unsigned long *len)
1648 {
1649 struct ustctl_consumer_channel *consumer_chan;
1650 struct lttng_ust_lib_ring_buffer_channel *rb_chan;
1651 struct lttng_ust_lib_ring_buffer *buf;
1652
1653 if (!stream)
1654 return -EINVAL;
1655 buf = stream->buf;
1656 consumer_chan = stream->chan;
1657 rb_chan = consumer_chan->chan->priv->rb_chan;
1658 *len = lib_ring_buffer_get_read_data_size(&rb_chan->backend.config, buf,
1659 rb_chan->handle);
1660 *len = LTTNG_UST_PAGE_ALIGN(*len);
1661 return 0;
1662 }
1663
1664 /* Get exclusive read access to the next sub-buffer that can be read. */
1665 int ustctl_get_next_subbuf(struct ustctl_consumer_stream *stream)
1666 {
1667 struct lttng_ust_lib_ring_buffer *buf;
1668 struct ustctl_consumer_channel *consumer_chan;
1669
1670 if (!stream)
1671 return -EINVAL;
1672 buf = stream->buf;
1673 consumer_chan = stream->chan;
1674 return lib_ring_buffer_get_next_subbuf(buf,
1675 consumer_chan->chan->priv->rb_chan->handle);
1676 }
1677
1678
1679 /* Release exclusive sub-buffer access, move consumer forward. */
1680 int ustctl_put_next_subbuf(struct ustctl_consumer_stream *stream)
1681 {
1682 struct lttng_ust_lib_ring_buffer *buf;
1683 struct ustctl_consumer_channel *consumer_chan;
1684
1685 if (!stream)
1686 return -EINVAL;
1687 buf = stream->buf;
1688 consumer_chan = stream->chan;
1689 lib_ring_buffer_put_next_subbuf(buf, consumer_chan->chan->priv->rb_chan->handle);
1690 return 0;
1691 }
1692
1693 /* snapshot */
1694
1695 /* Get a snapshot of the current ring buffer producer and consumer positions */
1696 int ustctl_snapshot(struct ustctl_consumer_stream *stream)
1697 {
1698 struct lttng_ust_lib_ring_buffer *buf;
1699 struct ustctl_consumer_channel *consumer_chan;
1700
1701 if (!stream)
1702 return -EINVAL;
1703 buf = stream->buf;
1704 consumer_chan = stream->chan;
1705 return lib_ring_buffer_snapshot(buf, &buf->cons_snapshot,
1706 &buf->prod_snapshot, consumer_chan->chan->priv->rb_chan->handle);
1707 }
1708
1709 /*
1710 * Get a snapshot of the current ring buffer producer and consumer positions
1711 * even if the consumed and produced positions are contained within the same
1712 * subbuffer.
1713 */
1714 int ustctl_snapshot_sample_positions(struct ustctl_consumer_stream *stream)
1715 {
1716 struct lttng_ust_lib_ring_buffer *buf;
1717 struct ustctl_consumer_channel *consumer_chan;
1718
1719 if (!stream)
1720 return -EINVAL;
1721 buf = stream->buf;
1722 consumer_chan = stream->chan;
1723 return lib_ring_buffer_snapshot_sample_positions(buf,
1724 &buf->cons_snapshot, &buf->prod_snapshot,
1725 consumer_chan->chan->priv->rb_chan->handle);
1726 }
1727
1728 /* Get the consumer position (iteration start) */
1729 int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream *stream,
1730 unsigned long *pos)
1731 {
1732 struct lttng_ust_lib_ring_buffer *buf;
1733
1734 if (!stream)
1735 return -EINVAL;
1736 buf = stream->buf;
1737 *pos = buf->cons_snapshot;
1738 return 0;
1739 }
1740
1741 /* Get the producer position (iteration end) */
1742 int ustctl_snapshot_get_produced(struct ustctl_consumer_stream *stream,
1743 unsigned long *pos)
1744 {
1745 struct lttng_ust_lib_ring_buffer *buf;
1746
1747 if (!stream)
1748 return -EINVAL;
1749 buf = stream->buf;
1750 *pos = buf->prod_snapshot;
1751 return 0;
1752 }
1753
1754 /* Get exclusive read access to the specified sub-buffer position */
1755 int ustctl_get_subbuf(struct ustctl_consumer_stream *stream,
1756 unsigned long *pos)
1757 {
1758 struct lttng_ust_lib_ring_buffer *buf;
1759 struct ustctl_consumer_channel *consumer_chan;
1760
1761 if (!stream)
1762 return -EINVAL;
1763 buf = stream->buf;
1764 consumer_chan = stream->chan;
1765 return lib_ring_buffer_get_subbuf(buf, *pos,
1766 consumer_chan->chan->priv->rb_chan->handle);
1767 }
1768
1769 /* Release exclusive sub-buffer access */
1770 int ustctl_put_subbuf(struct ustctl_consumer_stream *stream)
1771 {
1772 struct lttng_ust_lib_ring_buffer *buf;
1773 struct ustctl_consumer_channel *consumer_chan;
1774
1775 if (!stream)
1776 return -EINVAL;
1777 buf = stream->buf;
1778 consumer_chan = stream->chan;
1779 lib_ring_buffer_put_subbuf(buf, consumer_chan->chan->priv->rb_chan->handle);
1780 return 0;
1781 }
1782
1783 void ustctl_flush_buffer(struct ustctl_consumer_stream *stream,
1784 int producer_active)
1785 {
1786 struct lttng_ust_lib_ring_buffer *buf;
1787 struct ustctl_consumer_channel *consumer_chan;
1788
1789 assert(stream);
1790 buf = stream->buf;
1791 consumer_chan = stream->chan;
1792 lib_ring_buffer_switch_slow(buf,
1793 producer_active ? SWITCH_ACTIVE : SWITCH_FLUSH,
1794 consumer_chan->chan->priv->rb_chan->handle);
1795 }
1796
1797 void ustctl_clear_buffer(struct ustctl_consumer_stream *stream)
1798 {
1799 struct lttng_ust_lib_ring_buffer *buf;
1800 struct ustctl_consumer_channel *consumer_chan;
1801
1802 assert(stream);
1803 buf = stream->buf;
1804 consumer_chan = stream->chan;
1805 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE,
1806 consumer_chan->chan->priv->rb_chan->handle);
1807 lib_ring_buffer_clear_reader(buf, consumer_chan->chan->priv->rb_chan->handle);
1808 }
1809
1810 static
1811 struct lttng_ust_client_lib_ring_buffer_client_cb *get_client_cb(
1812 struct lttng_ust_lib_ring_buffer *buf __attribute__((unused)),
1813 struct lttng_ust_lib_ring_buffer_channel *chan)
1814 {
1815 const struct lttng_ust_lib_ring_buffer_config *config;
1816 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1817
1818 config = &chan->backend.config;
1819 if (!config->cb_ptr)
1820 return NULL;
1821 client_cb = caa_container_of(config->cb_ptr,
1822 struct lttng_ust_client_lib_ring_buffer_client_cb,
1823 parent);
1824 return client_cb;
1825 }
1826
1827 int ustctl_get_timestamp_begin(struct ustctl_consumer_stream *stream,
1828 uint64_t *timestamp_begin)
1829 {
1830 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1831 struct lttng_ust_lib_ring_buffer_channel *chan;
1832 struct lttng_ust_lib_ring_buffer *buf;
1833
1834 if (!stream || !timestamp_begin)
1835 return -EINVAL;
1836 buf = stream->buf;
1837 chan = stream->chan->chan->priv->rb_chan;
1838 client_cb = get_client_cb(buf, chan);
1839 if (!client_cb)
1840 return -ENOSYS;
1841 return client_cb->timestamp_begin(buf, chan, timestamp_begin);
1842 }
1843
1844 int ustctl_get_timestamp_end(struct ustctl_consumer_stream *stream,
1845 uint64_t *timestamp_end)
1846 {
1847 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1848 struct lttng_ust_lib_ring_buffer_channel *chan;
1849 struct lttng_ust_lib_ring_buffer *buf;
1850
1851 if (!stream || !timestamp_end)
1852 return -EINVAL;
1853 buf = stream->buf;
1854 chan = stream->chan->chan->priv->rb_chan;
1855 client_cb = get_client_cb(buf, chan);
1856 if (!client_cb)
1857 return -ENOSYS;
1858 return client_cb->timestamp_end(buf, chan, timestamp_end);
1859 }
1860
1861 int ustctl_get_events_discarded(struct ustctl_consumer_stream *stream,
1862 uint64_t *events_discarded)
1863 {
1864 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1865 struct lttng_ust_lib_ring_buffer_channel *chan;
1866 struct lttng_ust_lib_ring_buffer *buf;
1867
1868 if (!stream || !events_discarded)
1869 return -EINVAL;
1870 buf = stream->buf;
1871 chan = stream->chan->chan->priv->rb_chan;
1872 client_cb = get_client_cb(buf, chan);
1873 if (!client_cb)
1874 return -ENOSYS;
1875 return client_cb->events_discarded(buf, chan, events_discarded);
1876 }
1877
1878 int ustctl_get_content_size(struct ustctl_consumer_stream *stream,
1879 uint64_t *content_size)
1880 {
1881 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1882 struct lttng_ust_lib_ring_buffer_channel *chan;
1883 struct lttng_ust_lib_ring_buffer *buf;
1884
1885 if (!stream || !content_size)
1886 return -EINVAL;
1887 buf = stream->buf;
1888 chan = stream->chan->chan->priv->rb_chan;
1889 client_cb = get_client_cb(buf, chan);
1890 if (!client_cb)
1891 return -ENOSYS;
1892 return client_cb->content_size(buf, chan, content_size);
1893 }
1894
1895 int ustctl_get_packet_size(struct ustctl_consumer_stream *stream,
1896 uint64_t *packet_size)
1897 {
1898 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1899 struct lttng_ust_lib_ring_buffer_channel *chan;
1900 struct lttng_ust_lib_ring_buffer *buf;
1901
1902 if (!stream || !packet_size)
1903 return -EINVAL;
1904 buf = stream->buf;
1905 chan = stream->chan->chan->priv->rb_chan;
1906 client_cb = get_client_cb(buf, chan);
1907 if (!client_cb)
1908 return -ENOSYS;
1909 return client_cb->packet_size(buf, chan, packet_size);
1910 }
1911
1912 int ustctl_get_stream_id(struct ustctl_consumer_stream *stream,
1913 uint64_t *stream_id)
1914 {
1915 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1916 struct lttng_ust_lib_ring_buffer_channel *chan;
1917 struct lttng_ust_lib_ring_buffer *buf;
1918
1919 if (!stream || !stream_id)
1920 return -EINVAL;
1921 buf = stream->buf;
1922 chan = stream->chan->chan->priv->rb_chan;
1923 client_cb = get_client_cb(buf, chan);
1924 if (!client_cb)
1925 return -ENOSYS;
1926 return client_cb->stream_id(buf, chan, stream_id);
1927 }
1928
1929 int ustctl_get_current_timestamp(struct ustctl_consumer_stream *stream,
1930 uint64_t *ts)
1931 {
1932 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1933 struct lttng_ust_lib_ring_buffer_channel *chan;
1934 struct lttng_ust_lib_ring_buffer *buf;
1935
1936 if (!stream || !ts)
1937 return -EINVAL;
1938 buf = stream->buf;
1939 chan = stream->chan->chan->priv->rb_chan;
1940 client_cb = get_client_cb(buf, chan);
1941 if (!client_cb || !client_cb->current_timestamp)
1942 return -ENOSYS;
1943 return client_cb->current_timestamp(buf, chan, ts);
1944 }
1945
1946 int ustctl_get_sequence_number(struct ustctl_consumer_stream *stream,
1947 uint64_t *seq)
1948 {
1949 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1950 struct lttng_ust_lib_ring_buffer_channel *chan;
1951 struct lttng_ust_lib_ring_buffer *buf;
1952
1953 if (!stream || !seq)
1954 return -EINVAL;
1955 buf = stream->buf;
1956 chan = stream->chan->chan->priv->rb_chan;
1957 client_cb = get_client_cb(buf, chan);
1958 if (!client_cb || !client_cb->sequence_number)
1959 return -ENOSYS;
1960 return client_cb->sequence_number(buf, chan, seq);
1961 }
1962
1963 int ustctl_get_instance_id(struct ustctl_consumer_stream *stream,
1964 uint64_t *id)
1965 {
1966 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1967 struct lttng_ust_lib_ring_buffer_channel *chan;
1968 struct lttng_ust_lib_ring_buffer *buf;
1969
1970 if (!stream || !id)
1971 return -EINVAL;
1972 buf = stream->buf;
1973 chan = stream->chan->chan->priv->rb_chan;
1974 client_cb = get_client_cb(buf, chan);
1975 if (!client_cb)
1976 return -ENOSYS;
1977 return client_cb->instance_id(buf, chan, id);
1978 }
1979
1980 #ifdef HAVE_LINUX_PERF_EVENT_H
1981
1982 int ustctl_has_perf_counters(void)
1983 {
1984 return 1;
1985 }
1986
1987 #else
1988
1989 int ustctl_has_perf_counters(void)
1990 {
1991 return 0;
1992 }
1993
1994 #endif
1995
1996 #ifdef __linux__
1997 /*
1998 * Override application pid/uid/gid with unix socket credentials. If
1999 * the application announced a pid matching our view, it means it is
2000 * within the same pid namespace, so expose the ppid provided by the
2001 * application.
2002 */
2003 static
2004 int get_cred(int sock,
2005 const struct ustctl_reg_msg *reg_msg,
2006 uint32_t *pid,
2007 uint32_t *ppid,
2008 uint32_t *uid,
2009 uint32_t *gid)
2010 {
2011 struct ucred ucred;
2012 socklen_t ucred_len = sizeof(struct ucred);
2013 int ret;
2014
2015 ret = getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &ucred, &ucred_len);
2016 if (ret) {
2017 return -LTTNG_UST_ERR_PEERCRED;
2018 }
2019 DBG("Unix socket peercred [ pid: %u, uid: %u, gid: %u ], "
2020 "application registered claiming [ pid: %u, ppid: %u, uid: %u, gid: %u ]",
2021 ucred.pid, ucred.uid, ucred.gid,
2022 reg_msg->pid, reg_msg->ppid, reg_msg->uid, reg_msg->gid);
2023 if (!ucred.pid) {
2024 ERR("Unix socket credential pid=0. Refusing application in distinct, non-nested pid namespace.");
2025 return -LTTNG_UST_ERR_PEERCRED_PID;
2026 }
2027 *pid = ucred.pid;
2028 *uid = ucred.uid;
2029 *gid = ucred.gid;
2030 if (ucred.pid == reg_msg->pid) {
2031 *ppid = reg_msg->ppid;
2032 } else {
2033 *ppid = 0;
2034 }
2035 return 0;
2036 }
2037 #elif defined(__FreeBSD__)
2038 #include <sys/ucred.h>
2039 #include <sys/un.h>
2040
2041 /*
2042 * Override application uid/gid with unix socket credentials. Use the
2043 * first group of the cr_groups.
2044 * Use the pid and ppid provided by the application on registration.
2045 */
2046 static
2047 int get_cred(int sock,
2048 const struct ustctl_reg_msg *reg_msg,
2049 uint32_t *pid,
2050 uint32_t *ppid,
2051 uint32_t *uid,
2052 uint32_t *gid)
2053 {
2054 struct xucred xucred;
2055 socklen_t xucred_len = sizeof(struct xucred);
2056 int ret;
2057
2058 ret = getsockopt(sock, SOL_SOCKET, LOCAL_PEERCRED, &xucred, &xucred_len);
2059 if (ret) {
2060 return -LTTNG_UST_ERR_PEERCRED;
2061 }
2062 if (xucred.cr_version != XUCRED_VERSION || xucred.cr_ngroups < 1) {
2063 return -LTTNG_UST_ERR_PEERCRED;
2064 }
2065 DBG("Unix socket peercred [ uid: %u, gid: %u ], "
2066 "application registered claiming [ pid: %d, ppid: %d, uid: %u, gid: %u ]",
2067 xucred.cr_uid, xucred.cr_groups[0],
2068 reg_msg->pid, reg_msg->ppid, reg_msg->uid, reg_msg->gid);
2069 *pid = reg_msg->pid;
2070 *ppid = reg_msg->ppid;
2071 *uid = xucred.cr_uid;
2072 *gid = xucred.cr_groups[0];
2073 return 0;
2074 }
2075 #else
2076 #warning "Using insecure fallback: trusting user id provided by registered applications. Please consider implementing use of unix socket credentials on your platform."
2077 static
2078 int get_cred(int sock,
2079 const struct ustctl_reg_msg *reg_msg,
2080 uint32_t *pid,
2081 uint32_t *ppid,
2082 uint32_t *uid,
2083 uint32_t *gid)
2084 {
2085 DBG("Application registered claiming [ pid: %u, ppid: %d, uid: %u, gid: %u ]",
2086 reg_msg->pid, reg_msg->ppid, reg_msg->uid, reg_msg->gid);
2087 *pid = reg_msg->pid;
2088 *ppid = reg_msg->ppid;
2089 *uid = reg_msg->uid;
2090 *gid = reg_msg->gid;
2091 return 0;
2092 }
2093 #endif
2094
2095 /*
2096 * Returns 0 on success, negative error value on error.
2097 */
2098 int ustctl_recv_reg_msg(int sock,
2099 enum ustctl_socket_type *type,
2100 uint32_t *major,
2101 uint32_t *minor,
2102 uint32_t *pid,
2103 uint32_t *ppid,
2104 uint32_t *uid,
2105 uint32_t *gid,
2106 uint32_t *bits_per_long,
2107 uint32_t *uint8_t_alignment,
2108 uint32_t *uint16_t_alignment,
2109 uint32_t *uint32_t_alignment,
2110 uint32_t *uint64_t_alignment,
2111 uint32_t *long_alignment,
2112 int *byte_order,
2113 char *name)
2114 {
2115 ssize_t len;
2116 struct ustctl_reg_msg reg_msg;
2117
2118 len = ustcomm_recv_unix_sock(sock, &reg_msg, sizeof(reg_msg));
2119 if (len > 0 && len != sizeof(reg_msg))
2120 return -EIO;
2121 if (len == 0)
2122 return -EPIPE;
2123 if (len < 0)
2124 return len;
2125
2126 if (reg_msg.magic == LTTNG_UST_ABI_COMM_MAGIC) {
2127 *byte_order = BYTE_ORDER == BIG_ENDIAN ?
2128 BIG_ENDIAN : LITTLE_ENDIAN;
2129 } else if (reg_msg.magic == bswap_32(LTTNG_UST_ABI_COMM_MAGIC)) {
2130 *byte_order = BYTE_ORDER == BIG_ENDIAN ?
2131 LITTLE_ENDIAN : BIG_ENDIAN;
2132 } else {
2133 return -LTTNG_UST_ERR_INVAL_MAGIC;
2134 }
2135 switch (reg_msg.socket_type) {
2136 case 0: *type = USTCTL_SOCKET_CMD;
2137 break;
2138 case 1: *type = USTCTL_SOCKET_NOTIFY;
2139 break;
2140 default:
2141 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE;
2142 }
2143 *major = reg_msg.major;
2144 *minor = reg_msg.minor;
2145 *bits_per_long = reg_msg.bits_per_long;
2146 *uint8_t_alignment = reg_msg.uint8_t_alignment;
2147 *uint16_t_alignment = reg_msg.uint16_t_alignment;
2148 *uint32_t_alignment = reg_msg.uint32_t_alignment;
2149 *uint64_t_alignment = reg_msg.uint64_t_alignment;
2150 *long_alignment = reg_msg.long_alignment;
2151 memcpy(name, reg_msg.name, LTTNG_UST_ABI_PROCNAME_LEN);
2152 if (reg_msg.major < LTTNG_UST_ABI_MAJOR_VERSION_OLDEST_COMPATIBLE ||
2153 reg_msg.major > LTTNG_UST_ABI_MAJOR_VERSION) {
2154 return -LTTNG_UST_ERR_UNSUP_MAJOR;
2155 }
2156 return get_cred(sock, &reg_msg, pid, ppid, uid, gid);
2157 }
2158
2159 int ustctl_recv_notify(int sock, enum ustctl_notify_cmd *notify_cmd)
2160 {
2161 struct ustcomm_notify_hdr header;
2162 ssize_t len;
2163
2164 len = ustcomm_recv_unix_sock(sock, &header, sizeof(header));
2165 if (len > 0 && len != sizeof(header))
2166 return -EIO;
2167 if (len == 0)
2168 return -EPIPE;
2169 if (len < 0)
2170 return len;
2171 switch (header.notify_cmd) {
2172 case 0:
2173 *notify_cmd = USTCTL_NOTIFY_CMD_EVENT;
2174 break;
2175 case 1:
2176 *notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL;
2177 break;
2178 case 2:
2179 *notify_cmd = USTCTL_NOTIFY_CMD_ENUM;
2180 break;
2181 default:
2182 return -EINVAL;
2183 }
2184 return 0;
2185 }
2186
2187 /*
2188 * Returns 0 on success, negative error value on error.
2189 */
2190 int ustctl_recv_register_event(int sock,
2191 int *session_objd,
2192 int *channel_objd,
2193 char *event_name,
2194 int *loglevel,
2195 char **signature,
2196 size_t *nr_fields,
2197 struct ustctl_field **fields,
2198 char **model_emf_uri)
2199 {
2200 ssize_t len;
2201 struct ustcomm_notify_event_msg msg;
2202 size_t signature_len, fields_len, model_emf_uri_len;
2203 char *a_sign = NULL, *a_model_emf_uri = NULL;
2204 struct ustctl_field *a_fields = NULL;
2205
2206 len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg));
2207 if (len > 0 && len != sizeof(msg))
2208 return -EIO;
2209 if (len == 0)
2210 return -EPIPE;
2211 if (len < 0)
2212 return len;
2213
2214 *session_objd = msg.session_objd;
2215 *channel_objd = msg.channel_objd;
2216 strncpy(event_name, msg.event_name, LTTNG_UST_ABI_SYM_NAME_LEN);
2217 event_name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] = '\0';
2218 *loglevel = msg.loglevel;
2219 signature_len = msg.signature_len;
2220 fields_len = msg.fields_len;
2221
2222 if (fields_len % sizeof(*a_fields) != 0) {
2223 return -EINVAL;
2224 }
2225
2226 model_emf_uri_len = msg.model_emf_uri_len;
2227
2228 /* recv signature. contains at least \0. */
2229 a_sign = zmalloc(signature_len);
2230 if (!a_sign)
2231 return -ENOMEM;
2232 len = ustcomm_recv_unix_sock(sock, a_sign, signature_len);
2233 if (len > 0 && len != signature_len) {
2234 len = -EIO;
2235 goto signature_error;
2236 }
2237 if (len == 0) {
2238 len = -EPIPE;
2239 goto signature_error;
2240 }
2241 if (len < 0) {
2242 goto signature_error;
2243 }
2244 /* Enforce end of string */
2245 a_sign[signature_len - 1] = '\0';
2246
2247 /* recv fields */
2248 if (fields_len) {
2249 a_fields = zmalloc(fields_len);
2250 if (!a_fields) {
2251 len = -ENOMEM;
2252 goto signature_error;
2253 }
2254 len = ustcomm_recv_unix_sock(sock, a_fields, fields_len);
2255 if (len > 0 && len != fields_len) {
2256 len = -EIO;
2257 goto fields_error;
2258 }
2259 if (len == 0) {
2260 len = -EPIPE;
2261 goto fields_error;
2262 }
2263 if (len < 0) {
2264 goto fields_error;
2265 }
2266 }
2267
2268 if (model_emf_uri_len) {
2269 /* recv model_emf_uri_len */
2270 a_model_emf_uri = zmalloc(model_emf_uri_len);
2271 if (!a_model_emf_uri) {
2272 len = -ENOMEM;
2273 goto fields_error;
2274 }
2275 len = ustcomm_recv_unix_sock(sock, a_model_emf_uri,
2276 model_emf_uri_len);
2277 if (len > 0 && len != model_emf_uri_len) {
2278 len = -EIO;
2279 goto model_error;
2280 }
2281 if (len == 0) {
2282 len = -EPIPE;
2283 goto model_error;
2284 }
2285 if (len < 0) {
2286 goto model_error;
2287 }
2288 /* Enforce end of string */
2289 a_model_emf_uri[model_emf_uri_len - 1] = '\0';
2290 }
2291
2292 *signature = a_sign;
2293 *nr_fields = fields_len / sizeof(*a_fields);
2294 *fields = a_fields;
2295 *model_emf_uri = a_model_emf_uri;
2296
2297 return 0;
2298
2299 model_error:
2300 free(a_model_emf_uri);
2301 fields_error:
2302 free(a_fields);
2303 signature_error:
2304 free(a_sign);
2305 return len;
2306 }
2307
2308 /*
2309 * Returns 0 on success, negative error value on error.
2310 */
2311 int ustctl_reply_register_event(int sock,
2312 uint32_t id,
2313 int ret_code)
2314 {
2315 ssize_t len;
2316 struct {
2317 struct ustcomm_notify_hdr header;
2318 struct ustcomm_notify_event_reply r;
2319 } reply;
2320
2321 memset(&reply, 0, sizeof(reply));
2322 reply.header.notify_cmd = USTCTL_NOTIFY_CMD_EVENT;
2323 reply.r.ret_code = ret_code;
2324 reply.r.event_id = id;
2325 len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply));
2326 if (len > 0 && len != sizeof(reply))
2327 return -EIO;
2328 if (len < 0)
2329 return len;
2330 return 0;
2331 }
2332
2333 /*
2334 * Returns 0 on success, negative UST or system error value on error.
2335 */
2336 int ustctl_recv_register_enum(int sock,
2337 int *session_objd,
2338 char *enum_name,
2339 struct ustctl_enum_entry **entries,
2340 size_t *nr_entries)
2341 {
2342 ssize_t len;
2343 struct ustcomm_notify_enum_msg msg;
2344 size_t entries_len;
2345 struct ustctl_enum_entry *a_entries = NULL;
2346
2347 len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg));
2348 if (len > 0 && len != sizeof(msg))
2349 return -EIO;
2350 if (len == 0)
2351 return -EPIPE;
2352 if (len < 0)
2353 return len;
2354
2355 *session_objd = msg.session_objd;
2356 strncpy(enum_name, msg.enum_name, LTTNG_UST_ABI_SYM_NAME_LEN);
2357 enum_name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] = '\0';
2358 entries_len = msg.entries_len;
2359
2360 if (entries_len % sizeof(*a_entries) != 0) {
2361 return -EINVAL;
2362 }
2363
2364 /* recv entries */
2365 if (entries_len) {
2366 a_entries = zmalloc(entries_len);
2367 if (!a_entries)
2368 return -ENOMEM;
2369 len = ustcomm_recv_unix_sock(sock, a_entries, entries_len);
2370 if (len > 0 && len != entries_len) {
2371 len = -EIO;
2372 goto entries_error;
2373 }
2374 if (len == 0) {
2375 len = -EPIPE;
2376 goto entries_error;
2377 }
2378 if (len < 0) {
2379 goto entries_error;
2380 }
2381 }
2382 *nr_entries = entries_len / sizeof(*a_entries);
2383 *entries = a_entries;
2384
2385 return 0;
2386
2387 entries_error:
2388 free(a_entries);
2389 return len;
2390 }
2391
2392 /*
2393 * Returns 0 on success, negative error value on error.
2394 */
2395 int ustctl_reply_register_enum(int sock,
2396 uint64_t id,
2397 int ret_code)
2398 {
2399 ssize_t len;
2400 struct {
2401 struct ustcomm_notify_hdr header;
2402 struct ustcomm_notify_enum_reply r;
2403 } reply;
2404
2405 memset(&reply, 0, sizeof(reply));
2406 reply.header.notify_cmd = USTCTL_NOTIFY_CMD_ENUM;
2407 reply.r.ret_code = ret_code;
2408 reply.r.enum_id = id;
2409 len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply));
2410 if (len > 0 && len != sizeof(reply))
2411 return -EIO;
2412 if (len < 0)
2413 return len;
2414 return 0;
2415 }
2416
2417 /*
2418 * Returns 0 on success, negative UST or system error value on error.
2419 */
2420 int ustctl_recv_register_channel(int sock,
2421 int *session_objd, /* session descriptor (output) */
2422 int *channel_objd, /* channel descriptor (output) */
2423 size_t *nr_fields,
2424 struct ustctl_field **fields)
2425 {
2426 ssize_t len;
2427 struct ustcomm_notify_channel_msg msg;
2428 size_t fields_len;
2429 struct ustctl_field *a_fields;
2430
2431 len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg));
2432 if (len > 0 && len != sizeof(msg))
2433 return -EIO;
2434 if (len == 0)
2435 return -EPIPE;
2436 if (len < 0)
2437 return len;
2438
2439 *session_objd = msg.session_objd;
2440 *channel_objd = msg.channel_objd;
2441 fields_len = msg.ctx_fields_len;
2442
2443 if (fields_len % sizeof(*a_fields) != 0) {
2444 return -EINVAL;
2445 }
2446
2447 /* recv fields */
2448 if (fields_len) {
2449 a_fields = zmalloc(fields_len);
2450 if (!a_fields) {
2451 len = -ENOMEM;
2452 goto alloc_error;
2453 }
2454 len = ustcomm_recv_unix_sock(sock, a_fields, fields_len);
2455 if (len > 0 && len != fields_len) {
2456 len = -EIO;
2457 goto fields_error;
2458 }
2459 if (len == 0) {
2460 len = -EPIPE;
2461 goto fields_error;
2462 }
2463 if (len < 0) {
2464 goto fields_error;
2465 }
2466 *fields = a_fields;
2467 } else {
2468 *fields = NULL;
2469 }
2470 *nr_fields = fields_len / sizeof(*a_fields);
2471 return 0;
2472
2473 fields_error:
2474 free(a_fields);
2475 alloc_error:
2476 return len;
2477 }
2478
2479 /*
2480 * Returns 0 on success, negative error value on error.
2481 */
2482 int ustctl_reply_register_channel(int sock,
2483 uint32_t chan_id,
2484 enum ustctl_channel_header header_type,
2485 int ret_code)
2486 {
2487 ssize_t len;
2488 struct {
2489 struct ustcomm_notify_hdr header;
2490 struct ustcomm_notify_channel_reply r;
2491 } reply;
2492
2493 memset(&reply, 0, sizeof(reply));
2494 reply.header.notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL;
2495 reply.r.ret_code = ret_code;
2496 reply.r.chan_id = chan_id;
2497 switch (header_type) {
2498 case USTCTL_CHANNEL_HEADER_COMPACT:
2499 reply.r.header_type = 1;
2500 break;
2501 case USTCTL_CHANNEL_HEADER_LARGE:
2502 reply.r.header_type = 2;
2503 break;
2504 default:
2505 reply.r.header_type = 0;
2506 break;
2507 }
2508 len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply));
2509 if (len > 0 && len != sizeof(reply))
2510 return -EIO;
2511 if (len < 0)
2512 return len;
2513 return 0;
2514 }
2515
2516 /* Regenerate the statedump. */
2517 int ustctl_regenerate_statedump(int sock, int handle)
2518 {
2519 struct ustcomm_ust_msg lum;
2520 struct ustcomm_ust_reply lur;
2521 int ret;
2522
2523 memset(&lum, 0, sizeof(lum));
2524 lum.handle = handle;
2525 lum.cmd = LTTNG_UST_ABI_SESSION_STATEDUMP;
2526 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
2527 if (ret)
2528 return ret;
2529 DBG("Regenerated statedump for handle %u", handle);
2530 return 0;
2531 }
2532
2533 /* counter operations */
2534
2535 int ustctl_get_nr_cpu_per_counter(void)
2536 {
2537 return lttng_counter_num_possible_cpus();
2538 }
2539
2540 struct ustctl_daemon_counter *
2541 ustctl_create_counter(size_t nr_dimensions,
2542 const struct ustctl_counter_dimension *dimensions,
2543 int64_t global_sum_step,
2544 int global_counter_fd,
2545 int nr_counter_cpu_fds,
2546 const int *counter_cpu_fds,
2547 enum ustctl_counter_bitness bitness,
2548 enum ustctl_counter_arithmetic arithmetic,
2549 uint32_t alloc_flags,
2550 bool coalesce_hits)
2551 {
2552 const char *transport_name;
2553 struct ustctl_daemon_counter *counter;
2554 struct lttng_counter_transport *transport;
2555 struct lttng_counter_dimension ust_dim[LTTNG_COUNTER_DIMENSION_MAX];
2556 size_t i;
2557
2558 if (nr_dimensions > LTTNG_COUNTER_DIMENSION_MAX)
2559 return NULL;
2560 /* Currently, only per-cpu allocation is supported. */
2561 switch (alloc_flags) {
2562 case USTCTL_COUNTER_ALLOC_PER_CPU:
2563 break;
2564
2565 case USTCTL_COUNTER_ALLOC_PER_CPU | USTCTL_COUNTER_ALLOC_GLOBAL:
2566 case USTCTL_COUNTER_ALLOC_GLOBAL:
2567 default:
2568 return NULL;
2569 }
2570 switch (bitness) {
2571 case USTCTL_COUNTER_BITNESS_32:
2572 switch (arithmetic) {
2573 case USTCTL_COUNTER_ARITHMETIC_MODULAR:
2574 transport_name = "counter-per-cpu-32-modular";
2575 break;
2576 case USTCTL_COUNTER_ARITHMETIC_SATURATION:
2577 transport_name = "counter-per-cpu-32-saturation";
2578 break;
2579 default:
2580 return NULL;
2581 }
2582 break;
2583 case USTCTL_COUNTER_BITNESS_64:
2584 switch (arithmetic) {
2585 case USTCTL_COUNTER_ARITHMETIC_MODULAR:
2586 transport_name = "counter-per-cpu-64-modular";
2587 break;
2588 case USTCTL_COUNTER_ARITHMETIC_SATURATION:
2589 transport_name = "counter-per-cpu-64-saturation";
2590 break;
2591 default:
2592 return NULL;
2593 }
2594 break;
2595 default:
2596 return NULL;
2597 }
2598
2599 transport = lttng_counter_transport_find(transport_name);
2600 if (!transport) {
2601 DBG("LTTng transport %s not found\n",
2602 transport_name);
2603 return NULL;
2604 }
2605
2606 counter = zmalloc(sizeof(*counter));
2607 if (!counter)
2608 return NULL;
2609 counter->attr = zmalloc(sizeof(*counter->attr));
2610 if (!counter->attr)
2611 goto free_counter;
2612 counter->attr->bitness = bitness;
2613 counter->attr->arithmetic = arithmetic;
2614 counter->attr->nr_dimensions = nr_dimensions;
2615 counter->attr->global_sum_step = global_sum_step;
2616 counter->attr->coalesce_hits = coalesce_hits;
2617 for (i = 0; i < nr_dimensions; i++)
2618 counter->attr->dimensions[i] = dimensions[i];
2619
2620 for (i = 0; i < nr_dimensions; i++) {
2621 ust_dim[i].size = dimensions[i].size;
2622 ust_dim[i].underflow_index = dimensions[i].underflow_index;
2623 ust_dim[i].overflow_index = dimensions[i].overflow_index;
2624 ust_dim[i].has_underflow = dimensions[i].has_underflow;
2625 ust_dim[i].has_overflow = dimensions[i].has_overflow;
2626 }
2627 counter->counter = transport->ops.counter_create(nr_dimensions,
2628 ust_dim, global_sum_step, global_counter_fd,
2629 nr_counter_cpu_fds, counter_cpu_fds, true);
2630 if (!counter->counter)
2631 goto free_attr;
2632 counter->ops = &transport->ops;
2633 return counter;
2634
2635 free_attr:
2636 free(counter->attr);
2637 free_counter:
2638 free(counter);
2639 return NULL;
2640 }
2641
2642 int ustctl_create_counter_data(struct ustctl_daemon_counter *counter,
2643 struct lttng_ust_abi_object_data **_counter_data)
2644 {
2645 struct lttng_ust_abi_object_data *counter_data;
2646 struct lttng_ust_abi_counter_conf counter_conf = {0};
2647 size_t i;
2648 int ret;
2649
2650 switch (counter->attr->arithmetic) {
2651 case USTCTL_COUNTER_ARITHMETIC_MODULAR:
2652 counter_conf.arithmetic = LTTNG_UST_ABI_COUNTER_ARITHMETIC_MODULAR;
2653 break;
2654 case USTCTL_COUNTER_ARITHMETIC_SATURATION:
2655 counter_conf.arithmetic = LTTNG_UST_ABI_COUNTER_ARITHMETIC_SATURATION;
2656 break;
2657 default:
2658 return -EINVAL;
2659 }
2660 switch (counter->attr->bitness) {
2661 case USTCTL_COUNTER_BITNESS_32:
2662 counter_conf.bitness = LTTNG_UST_ABI_COUNTER_BITNESS_32;
2663 break;
2664 case USTCTL_COUNTER_BITNESS_64:
2665 counter_conf.bitness = LTTNG_UST_ABI_COUNTER_BITNESS_64;
2666 break;
2667 default:
2668 return -EINVAL;
2669 }
2670 counter_conf.number_dimensions = counter->attr->nr_dimensions;
2671 counter_conf.global_sum_step = counter->attr->global_sum_step;
2672 counter_conf.coalesce_hits = counter->attr->coalesce_hits;
2673 for (i = 0; i < counter->attr->nr_dimensions; i++) {
2674 counter_conf.dimensions[i].size = counter->attr->dimensions[i].size;
2675 counter_conf.dimensions[i].underflow_index = counter->attr->dimensions[i].underflow_index;
2676 counter_conf.dimensions[i].overflow_index = counter->attr->dimensions[i].overflow_index;
2677 counter_conf.dimensions[i].has_underflow = counter->attr->dimensions[i].has_underflow;
2678 counter_conf.dimensions[i].has_overflow = counter->attr->dimensions[i].has_overflow;
2679 }
2680
2681 counter_data = zmalloc(sizeof(*counter_data));
2682 if (!counter_data) {
2683 ret = -ENOMEM;
2684 goto error_alloc;
2685 }
2686 counter_data->type = LTTNG_UST_ABI_OBJECT_TYPE_COUNTER;
2687 counter_data->handle = -1;
2688
2689 counter_data->size = sizeof(counter_conf);
2690 counter_data->u.counter.data = zmalloc(sizeof(counter_conf));
2691 if (!counter_data->u.counter.data) {
2692 ret = -ENOMEM;
2693 goto error_alloc_data;
2694 }
2695
2696 memcpy(counter_data->u.counter.data, &counter_conf, sizeof(counter_conf));
2697 *_counter_data = counter_data;
2698
2699 return 0;
2700
2701 error_alloc_data:
2702 free(counter_data);
2703 error_alloc:
2704 return ret;
2705 }
2706
2707 int ustctl_create_counter_global_data(struct ustctl_daemon_counter *counter,
2708 struct lttng_ust_abi_object_data **_counter_global_data)
2709 {
2710 struct lttng_ust_abi_object_data *counter_global_data;
2711 int ret, fd;
2712 size_t len;
2713
2714 if (lttng_counter_get_global_shm(counter->counter, &fd, &len))
2715 return -EINVAL;
2716 counter_global_data = zmalloc(sizeof(*counter_global_data));
2717 if (!counter_global_data) {
2718 ret = -ENOMEM;
2719 goto error_alloc;
2720 }
2721 counter_global_data->type = LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_GLOBAL;
2722 counter_global_data->handle = -1;
2723 counter_global_data->size = len;
2724 counter_global_data->u.counter_global.shm_fd = fd;
2725 *_counter_global_data = counter_global_data;
2726 return 0;
2727
2728 error_alloc:
2729 return ret;
2730 }
2731
2732 int ustctl_create_counter_cpu_data(struct ustctl_daemon_counter *counter, int cpu,
2733 struct lttng_ust_abi_object_data **_counter_cpu_data)
2734 {
2735 struct lttng_ust_abi_object_data *counter_cpu_data;
2736 int ret, fd;
2737 size_t len;
2738
2739 if (lttng_counter_get_cpu_shm(counter->counter, cpu, &fd, &len))
2740 return -EINVAL;
2741 counter_cpu_data = zmalloc(sizeof(*counter_cpu_data));
2742 if (!counter_cpu_data) {
2743 ret = -ENOMEM;
2744 goto error_alloc;
2745 }
2746 counter_cpu_data->type = LTTNG_UST_ABI_OBJECT_TYPE_COUNTER_CPU;
2747 counter_cpu_data->handle = -1;
2748 counter_cpu_data->size = len;
2749 counter_cpu_data->u.counter_cpu.shm_fd = fd;
2750 counter_cpu_data->u.counter_cpu.cpu_nr = cpu;
2751 *_counter_cpu_data = counter_cpu_data;
2752 return 0;
2753
2754 error_alloc:
2755 return ret;
2756 }
2757
2758 void ustctl_destroy_counter(struct ustctl_daemon_counter *counter)
2759 {
2760 counter->ops->counter_destroy(counter->counter);
2761 free(counter->attr);
2762 free(counter);
2763 }
2764
2765 int ustctl_send_counter_data_to_ust(int sock, int parent_handle,
2766 struct lttng_ust_abi_object_data *counter_data)
2767 {
2768 struct ustcomm_ust_msg lum;
2769 struct ustcomm_ust_reply lur;
2770 int ret;
2771 size_t size;
2772 ssize_t len;
2773
2774 if (!counter_data)
2775 return -EINVAL;
2776
2777 size = counter_data->size;
2778 memset(&lum, 0, sizeof(lum));
2779 lum.handle = parent_handle;
2780 lum.cmd = LTTNG_UST_ABI_COUNTER;
2781 lum.u.counter.len = size;
2782 ret = ustcomm_send_app_msg(sock, &lum);
2783 if (ret)
2784 return ret;
2785
2786 /* Send counter data */
2787 len = ustcomm_send_unix_sock(sock, counter_data->u.counter.data, size);
2788 if (len != size) {
2789 if (len < 0)
2790 return len;
2791 else
2792 return -EIO;
2793 }
2794
2795 ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
2796 if (!ret) {
2797 counter_data->handle = lur.ret_val;
2798 }
2799 return ret;
2800 }
2801
2802 int ustctl_send_counter_global_data_to_ust(int sock,
2803 struct lttng_ust_abi_object_data *counter_data,
2804 struct lttng_ust_abi_object_data *counter_global_data)
2805 {
2806 struct ustcomm_ust_msg lum;
2807 struct ustcomm_ust_reply lur;
2808 int ret, shm_fd[1];
2809 size_t size;
2810 ssize_t len;
2811
2812 if (!counter_data || !counter_global_data)
2813 return -EINVAL;
2814
2815 size = counter_global_data->size;
2816 memset(&lum, 0, sizeof(lum));
2817 lum.handle = counter_data->handle; /* parent handle */
2818 lum.cmd = LTTNG_UST_ABI_COUNTER_GLOBAL;
2819 lum.u.counter_global.len = size;
2820 ret = ustcomm_send_app_msg(sock, &lum);
2821 if (ret)
2822 return ret;
2823
2824 shm_fd[0] = counter_global_data->u.counter_global.shm_fd;
2825 len = ustcomm_send_fds_unix_sock(sock, shm_fd, 1);
2826 if (len <= 0) {
2827 if (len < 0)
2828 return len;
2829 else
2830 return -EIO;
2831 }
2832
2833 ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
2834 if (!ret) {
2835 counter_global_data->handle = lur.ret_val;
2836 }
2837 return ret;
2838 }
2839
2840 int ustctl_send_counter_cpu_data_to_ust(int sock,
2841 struct lttng_ust_abi_object_data *counter_data,
2842 struct lttng_ust_abi_object_data *counter_cpu_data)
2843 {
2844 struct ustcomm_ust_msg lum;
2845 struct ustcomm_ust_reply lur;
2846 int ret, shm_fd[1];
2847 size_t size;
2848 ssize_t len;
2849
2850 if (!counter_data || !counter_cpu_data)
2851 return -EINVAL;
2852
2853 size = counter_cpu_data->size;
2854 memset(&lum, 0, sizeof(lum));
2855 lum.handle = counter_data->handle; /* parent handle */
2856 lum.cmd = LTTNG_UST_ABI_COUNTER_CPU;
2857 lum.u.counter_cpu.len = size;
2858 lum.u.counter_cpu.cpu_nr = counter_cpu_data->u.counter_cpu.cpu_nr;
2859 ret = ustcomm_send_app_msg(sock, &lum);
2860 if (ret)
2861 return ret;
2862
2863 shm_fd[0] = counter_cpu_data->u.counter_global.shm_fd;
2864 len = ustcomm_send_fds_unix_sock(sock, shm_fd, 1);
2865 if (len <= 0) {
2866 if (len < 0)
2867 return len;
2868 else
2869 return -EIO;
2870 }
2871
2872 ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
2873 if (!ret) {
2874 counter_cpu_data->handle = lur.ret_val;
2875 }
2876 return ret;
2877 }
2878
2879 int ustctl_counter_read(struct ustctl_daemon_counter *counter,
2880 const size_t *dimension_indexes,
2881 int cpu, int64_t *value,
2882 bool *overflow, bool *underflow)
2883 {
2884 return counter->ops->counter_read(counter->counter, dimension_indexes, cpu,
2885 value, overflow, underflow);
2886 }
2887
2888 int ustctl_counter_aggregate(struct ustctl_daemon_counter *counter,
2889 const size_t *dimension_indexes,
2890 int64_t *value,
2891 bool *overflow, bool *underflow)
2892 {
2893 return counter->ops->counter_aggregate(counter->counter, dimension_indexes,
2894 value, overflow, underflow);
2895 }
2896
2897 int ustctl_counter_clear(struct ustctl_daemon_counter *counter,
2898 const size_t *dimension_indexes)
2899 {
2900 return counter->ops->counter_clear(counter->counter, dimension_indexes);
2901 }
2902
2903 static
2904 void ustctl_init(void)
2905 __attribute__((constructor));
2906 static
2907 void ustctl_init(void)
2908 {
2909 ust_err_init();
2910 lttng_ust_getenv_init(); /* Needs ust_err_init() to be completed. */
2911 lttng_ust_clock_init();
2912 lttng_ust_ring_buffer_clients_init();
2913 lttng_ust_counter_clients_init();
2914 lib_ringbuffer_signal_init();
2915 }
2916
2917 static
2918 void ustctl_exit(void)
2919 __attribute__((destructor));
2920 static
2921 void ustctl_exit(void)
2922 {
2923 lttng_ust_counter_clients_exit();
2924 lttng_ust_ring_buffer_clients_exit();
2925 }
This page took 0.132124 seconds and 4 git commands to generate.