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