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