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