Implement ustctl_duplicate_ust_object_data
[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 <string.h>
21 #include <lttng/ust-ctl.h>
22 #include <lttng/ust-abi.h>
23 #include <lttng/ust-events.h>
24 #include <sys/mman.h>
25 #include <byteswap.h>
26
27 #include <usterr-signal-safe.h>
28 #include <ust-comm.h>
29 #include <helper.h>
30
31 #include "../libringbuffer/backend.h"
32 #include "../libringbuffer/frontend.h"
33 #include "../liblttng-ust/wait.h"
34
35 /*
36 * Number of milliseconds to retry before failing metadata writes on
37 * buffer full condition. (10 seconds)
38 */
39 #define LTTNG_METADATA_TIMEOUT_MSEC 10000
40
41 /*
42 * Channel representation within consumer.
43 */
44 struct ustctl_consumer_channel {
45 struct lttng_channel *chan; /* lttng channel buffers */
46
47 /* initial attributes */
48 struct ustctl_consumer_channel_attr attr;
49 int wait_fd; /* monitor close() */
50 int wakeup_fd; /* monitor close() */
51 };
52
53 /*
54 * Stream representation within consumer.
55 */
56 struct ustctl_consumer_stream {
57 struct lttng_ust_shm_handle *handle; /* shared-memory handle */
58 struct lttng_ust_lib_ring_buffer *buf;
59 struct ustctl_consumer_channel *chan;
60 int shm_fd, wait_fd, wakeup_fd;
61 int cpu;
62 uint64_t memory_map_size;
63 };
64
65 extern void lttng_ring_buffer_client_overwrite_init(void);
66 extern void lttng_ring_buffer_client_discard_init(void);
67 extern void lttng_ring_buffer_metadata_client_init(void);
68 extern void lttng_ring_buffer_client_overwrite_exit(void);
69 extern void lttng_ring_buffer_client_discard_exit(void);
70 extern void lttng_ring_buffer_metadata_client_exit(void);
71
72 volatile enum ust_loglevel ust_loglevel;
73
74 int ustctl_release_handle(int sock, int handle)
75 {
76 struct ustcomm_ust_msg lum;
77 struct ustcomm_ust_reply lur;
78
79 if (sock < 0 || handle < 0)
80 return 0;
81 memset(&lum, 0, sizeof(lum));
82 lum.handle = handle;
83 lum.cmd = LTTNG_UST_RELEASE;
84 return ustcomm_send_app_cmd(sock, &lum, &lur);
85 }
86
87 /*
88 * If sock is negative, it means we don't have to notify the other side
89 * (e.g. application has already vanished).
90 */
91 int ustctl_release_object(int sock, struct lttng_ust_object_data *data)
92 {
93 int ret;
94
95 if (!data)
96 return -EINVAL;
97
98 switch (data->type) {
99 case LTTNG_UST_OBJECT_TYPE_CHANNEL:
100 if (data->u.channel.wakeup_fd >= 0) {
101 ret = close(data->u.channel.wakeup_fd);
102 if (ret < 0) {
103 ret = -errno;
104 return ret;
105 }
106 }
107 free(data->u.channel.data);
108 break;
109 case LTTNG_UST_OBJECT_TYPE_STREAM:
110 if (data->u.stream.shm_fd >= 0) {
111 ret = close(data->u.stream.shm_fd);
112 if (ret < 0) {
113 ret = -errno;
114 return ret;
115 }
116 }
117 if (data->u.stream.wakeup_fd >= 0) {
118 ret = close(data->u.stream.wakeup_fd);
119 if (ret < 0) {
120 ret = -errno;
121 return ret;
122 }
123 }
124 break;
125 case LTTNG_UST_OBJECT_TYPE_EVENT:
126 case LTTNG_UST_OBJECT_TYPE_CONTEXT:
127 break;
128 default:
129 assert(0);
130 }
131 return ustctl_release_handle(sock, data->handle);
132 }
133
134 /*
135 * Send registration done packet to the application.
136 */
137 int ustctl_register_done(int sock)
138 {
139 struct ustcomm_ust_msg lum;
140 struct ustcomm_ust_reply lur;
141 int ret;
142
143 DBG("Sending register done command to %d", sock);
144 memset(&lum, 0, sizeof(lum));
145 lum.handle = LTTNG_UST_ROOT_HANDLE;
146 lum.cmd = LTTNG_UST_REGISTER_DONE;
147 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
148 if (ret)
149 return ret;
150 return 0;
151 }
152
153 /*
154 * returns session handle.
155 */
156 int ustctl_create_session(int sock)
157 {
158 struct ustcomm_ust_msg lum;
159 struct ustcomm_ust_reply lur;
160 int ret, session_handle;
161
162 /* Create session */
163 memset(&lum, 0, sizeof(lum));
164 lum.handle = LTTNG_UST_ROOT_HANDLE;
165 lum.cmd = LTTNG_UST_SESSION;
166 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
167 if (ret)
168 return ret;
169 session_handle = lur.ret_val;
170 DBG("received session handle %u", session_handle);
171 return session_handle;
172 }
173
174 int ustctl_create_event(int sock, struct lttng_ust_event *ev,
175 struct lttng_ust_object_data *channel_data,
176 struct lttng_ust_object_data **_event_data)
177 {
178 struct ustcomm_ust_msg lum;
179 struct ustcomm_ust_reply lur;
180 struct lttng_ust_object_data *event_data;
181 int ret;
182
183 if (!channel_data || !_event_data)
184 return -EINVAL;
185
186 event_data = zmalloc(sizeof(*event_data));
187 if (!event_data)
188 return -ENOMEM;
189 event_data->type = LTTNG_UST_OBJECT_TYPE_EVENT;
190 memset(&lum, 0, sizeof(lum));
191 lum.handle = channel_data->handle;
192 lum.cmd = LTTNG_UST_EVENT;
193 strncpy(lum.u.event.name, ev->name,
194 LTTNG_UST_SYM_NAME_LEN);
195 lum.u.event.instrumentation = ev->instrumentation;
196 lum.u.event.loglevel_type = ev->loglevel_type;
197 lum.u.event.loglevel = ev->loglevel;
198 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
199 if (ret) {
200 free(event_data);
201 return ret;
202 }
203 event_data->handle = lur.ret_val;
204 DBG("received event handle %u", event_data->handle);
205 *_event_data = event_data;
206 return 0;
207 }
208
209 int ustctl_add_context(int sock, struct lttng_ust_context *ctx,
210 struct lttng_ust_object_data *obj_data,
211 struct lttng_ust_object_data **_context_data)
212 {
213 struct ustcomm_ust_msg lum;
214 struct ustcomm_ust_reply lur;
215 struct lttng_ust_object_data *context_data;
216 int ret;
217
218 if (!obj_data || !_context_data)
219 return -EINVAL;
220
221 context_data = zmalloc(sizeof(*context_data));
222 if (!context_data)
223 return -ENOMEM;
224 context_data->type = LTTNG_UST_OBJECT_TYPE_CONTEXT;
225 memset(&lum, 0, sizeof(lum));
226 lum.handle = obj_data->handle;
227 lum.cmd = LTTNG_UST_CONTEXT;
228 lum.u.context.ctx = ctx->ctx;
229 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
230 if (ret) {
231 free(context_data);
232 return ret;
233 }
234 context_data->handle = -1;
235 DBG("Context created successfully");
236 *_context_data = context_data;
237 return ret;
238 }
239
240 int ustctl_set_filter(int sock, struct lttng_ust_filter_bytecode *bytecode,
241 struct lttng_ust_object_data *obj_data)
242 {
243 struct ustcomm_ust_msg lum;
244 struct ustcomm_ust_reply lur;
245 int ret;
246
247 if (!obj_data)
248 return -EINVAL;
249
250 memset(&lum, 0, sizeof(lum));
251 lum.handle = obj_data->handle;
252 lum.cmd = LTTNG_UST_FILTER;
253 lum.u.filter.data_size = bytecode->len;
254 lum.u.filter.reloc_offset = bytecode->reloc_offset;
255 lum.u.filter.seqnum = bytecode->seqnum;
256
257 ret = ustcomm_send_app_msg(sock, &lum);
258 if (ret)
259 return ret;
260 /* send var len bytecode */
261 ret = ustcomm_send_unix_sock(sock, bytecode->data,
262 bytecode->len);
263 if (ret < 0) {
264 return ret;
265 }
266 if (ret != bytecode->len)
267 return -EINVAL;
268 return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
269 }
270
271 /* Enable event, channel and session ioctl */
272 int ustctl_enable(int sock, struct lttng_ust_object_data *object)
273 {
274 struct ustcomm_ust_msg lum;
275 struct ustcomm_ust_reply lur;
276 int ret;
277
278 if (!object)
279 return -EINVAL;
280
281 memset(&lum, 0, sizeof(lum));
282 lum.handle = object->handle;
283 lum.cmd = LTTNG_UST_ENABLE;
284 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
285 if (ret)
286 return ret;
287 DBG("enabled handle %u", object->handle);
288 return 0;
289 }
290
291 /* Disable event, channel and session ioctl */
292 int ustctl_disable(int sock, struct lttng_ust_object_data *object)
293 {
294 struct ustcomm_ust_msg lum;
295 struct ustcomm_ust_reply lur;
296 int ret;
297
298 if (!object)
299 return -EINVAL;
300
301 memset(&lum, 0, sizeof(lum));
302 lum.handle = object->handle;
303 lum.cmd = LTTNG_UST_DISABLE;
304 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
305 if (ret)
306 return ret;
307 DBG("disable handle %u", object->handle);
308 return 0;
309 }
310
311 int ustctl_start_session(int sock, int handle)
312 {
313 struct lttng_ust_object_data obj;
314
315 obj.handle = handle;
316 return ustctl_enable(sock, &obj);
317 }
318
319 int ustctl_stop_session(int sock, int handle)
320 {
321 struct lttng_ust_object_data obj;
322
323 obj.handle = handle;
324 return ustctl_disable(sock, &obj);
325 }
326
327 int ustctl_tracepoint_list(int sock)
328 {
329 struct ustcomm_ust_msg lum;
330 struct ustcomm_ust_reply lur;
331 int ret, tp_list_handle;
332
333 memset(&lum, 0, sizeof(lum));
334 lum.handle = LTTNG_UST_ROOT_HANDLE;
335 lum.cmd = LTTNG_UST_TRACEPOINT_LIST;
336 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
337 if (ret)
338 return ret;
339 tp_list_handle = lur.ret_val;
340 DBG("received tracepoint list handle %u", tp_list_handle);
341 return tp_list_handle;
342 }
343
344 int ustctl_tracepoint_list_get(int sock, int tp_list_handle,
345 struct lttng_ust_tracepoint_iter *iter)
346 {
347 struct ustcomm_ust_msg lum;
348 struct ustcomm_ust_reply lur;
349 int ret;
350
351 if (!iter)
352 return -EINVAL;
353
354 memset(&lum, 0, sizeof(lum));
355 lum.handle = tp_list_handle;
356 lum.cmd = LTTNG_UST_TRACEPOINT_LIST_GET;
357 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
358 if (ret)
359 return ret;
360 DBG("received tracepoint list entry name %s loglevel %d",
361 lur.u.tracepoint.name,
362 lur.u.tracepoint.loglevel);
363 memcpy(iter, &lur.u.tracepoint, sizeof(*iter));
364 return 0;
365 }
366
367 int ustctl_tracepoint_field_list(int sock)
368 {
369 struct ustcomm_ust_msg lum;
370 struct ustcomm_ust_reply lur;
371 int ret, tp_field_list_handle;
372
373 memset(&lum, 0, sizeof(lum));
374 lum.handle = LTTNG_UST_ROOT_HANDLE;
375 lum.cmd = LTTNG_UST_TRACEPOINT_FIELD_LIST;
376 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
377 if (ret)
378 return ret;
379 tp_field_list_handle = lur.ret_val;
380 DBG("received tracepoint field list handle %u", tp_field_list_handle);
381 return tp_field_list_handle;
382 }
383
384 int ustctl_tracepoint_field_list_get(int sock, int tp_field_list_handle,
385 struct lttng_ust_field_iter *iter)
386 {
387 struct ustcomm_ust_msg lum;
388 struct ustcomm_ust_reply lur;
389 int ret;
390 ssize_t len;
391
392 if (!iter)
393 return -EINVAL;
394
395 memset(&lum, 0, sizeof(lum));
396 lum.handle = tp_field_list_handle;
397 lum.cmd = LTTNG_UST_TRACEPOINT_FIELD_LIST_GET;
398 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
399 if (ret)
400 return ret;
401 len = ustcomm_recv_unix_sock(sock, iter, sizeof(*iter));
402 if (len != sizeof(*iter)) {
403 return -EINVAL;
404 }
405 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
406 iter->event_name,
407 iter->loglevel,
408 iter->field_name,
409 iter->type);
410 return 0;
411 }
412
413 int ustctl_tracer_version(int sock, struct lttng_ust_tracer_version *v)
414 {
415 struct ustcomm_ust_msg lum;
416 struct ustcomm_ust_reply lur;
417 int ret;
418
419 if (!v)
420 return -EINVAL;
421
422 memset(&lum, 0, sizeof(lum));
423 lum.handle = LTTNG_UST_ROOT_HANDLE;
424 lum.cmd = LTTNG_UST_TRACER_VERSION;
425 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
426 if (ret)
427 return ret;
428 memcpy(v, &lur.u.version, sizeof(*v));
429 DBG("received tracer version");
430 return 0;
431 }
432
433 int ustctl_wait_quiescent(int sock)
434 {
435 struct ustcomm_ust_msg lum;
436 struct ustcomm_ust_reply lur;
437 int ret;
438
439 memset(&lum, 0, sizeof(lum));
440 lum.handle = LTTNG_UST_ROOT_HANDLE;
441 lum.cmd = LTTNG_UST_WAIT_QUIESCENT;
442 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
443 if (ret)
444 return ret;
445 DBG("waited for quiescent state");
446 return 0;
447 }
448
449 int ustctl_calibrate(int sock, struct lttng_ust_calibrate *calibrate)
450 {
451 if (!calibrate)
452 return -EINVAL;
453
454 return -ENOSYS;
455 }
456
457 int ustctl_sock_flush_buffer(int sock, struct lttng_ust_object_data *object)
458 {
459 struct ustcomm_ust_msg lum;
460 struct ustcomm_ust_reply lur;
461 int ret;
462
463 if (!object)
464 return -EINVAL;
465
466 memset(&lum, 0, sizeof(lum));
467 lum.handle = object->handle;
468 lum.cmd = LTTNG_UST_FLUSH_BUFFER;
469 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
470 if (ret)
471 return ret;
472 DBG("flushed buffer handle %u", object->handle);
473 return 0;
474 }
475
476 static
477 int ustctl_send_channel(int sock,
478 enum lttng_ust_chan_type type,
479 void *data,
480 uint64_t size,
481 int wakeup_fd,
482 int send_fd_only)
483 {
484 ssize_t len;
485
486 if (!send_fd_only) {
487 /* Send mmap size */
488 len = ustcomm_send_unix_sock(sock, &size, sizeof(size));
489 if (len != sizeof(size)) {
490 if (len < 0)
491 return len;
492 else
493 return -EIO;
494 }
495
496 /* Send channel type */
497 len = ustcomm_send_unix_sock(sock, &type, sizeof(type));
498 if (len != sizeof(type)) {
499 if (len < 0)
500 return len;
501 else
502 return -EIO;
503 }
504 }
505
506 /* Send channel data */
507 len = ustcomm_send_unix_sock(sock, data, size);
508 if (len != size) {
509 if (len < 0)
510 return len;
511 else
512 return -EIO;
513 }
514
515 /* Send wakeup fd */
516 len = ustcomm_send_fds_unix_sock(sock, &wakeup_fd, 1);
517 if (len <= 0) {
518 if (len < 0)
519 return len;
520 else
521 return -EIO;
522 }
523 return 0;
524 }
525
526 static
527 int ustctl_send_stream(int sock,
528 uint32_t stream_nr,
529 uint64_t memory_map_size,
530 int shm_fd, int wakeup_fd,
531 int send_fd_only)
532 {
533 ssize_t len;
534 int fds[2];
535
536 if (!send_fd_only) {
537 if (shm_fd < 0) {
538 /* finish iteration */
539 uint64_t v = -1;
540
541 len = ustcomm_send_unix_sock(sock, &v, sizeof(v));
542 if (len != sizeof(v)) {
543 if (len < 0)
544 return len;
545 else
546 return -EIO;
547 }
548 return 0;
549 }
550
551 /* Send mmap size */
552 len = ustcomm_send_unix_sock(sock, &memory_map_size,
553 sizeof(memory_map_size));
554 if (len != sizeof(memory_map_size)) {
555 if (len < 0)
556 return len;
557 else
558 return -EIO;
559 }
560
561 /* Send stream nr */
562 len = ustcomm_send_unix_sock(sock, &stream_nr,
563 sizeof(stream_nr));
564 if (len != sizeof(stream_nr)) {
565 if (len < 0)
566 return len;
567 else
568 return -EIO;
569 }
570 }
571
572 /* Send shm fd and wakeup fd */
573 fds[0] = shm_fd;
574 fds[1] = wakeup_fd;
575 len = ustcomm_send_fds_unix_sock(sock, fds, 2);
576 if (len <= 0) {
577 if (len < 0)
578 return len;
579 else
580 return -EIO;
581 }
582 return 0;
583 }
584
585 int ustctl_recv_channel_from_consumer(int sock,
586 struct lttng_ust_object_data **_channel_data)
587 {
588 struct lttng_ust_object_data *channel_data;
589 ssize_t len;
590 int wakeup_fd;
591 int ret;
592
593 channel_data = zmalloc(sizeof(*channel_data));
594 if (!channel_data) {
595 ret = -ENOMEM;
596 goto error_alloc;
597 }
598 channel_data->type = LTTNG_UST_OBJECT_TYPE_CHANNEL;
599 channel_data->handle = -1;
600
601 /* recv mmap size */
602 len = ustcomm_recv_unix_sock(sock, &channel_data->size,
603 sizeof(channel_data->size));
604 if (len != sizeof(channel_data->size)) {
605 if (len < 0)
606 ret = len;
607 else
608 ret = -EINVAL;
609 goto error;
610 }
611
612 /* recv channel type */
613 len = ustcomm_recv_unix_sock(sock, &channel_data->u.channel.type,
614 sizeof(channel_data->u.channel.type));
615 if (len != sizeof(channel_data->u.channel.type)) {
616 if (len < 0)
617 ret = len;
618 else
619 ret = -EINVAL;
620 goto error;
621 }
622
623 /* recv channel data */
624 channel_data->u.channel.data = zmalloc(channel_data->size);
625 if (!channel_data->u.channel.data) {
626 ret = -ENOMEM;
627 goto error;
628 }
629 len = ustcomm_recv_unix_sock(sock, channel_data->u.channel.data,
630 channel_data->size);
631 if (len != channel_data->size) {
632 if (len < 0)
633 ret = len;
634 else
635 ret = -EINVAL;
636 goto error_recv_data;
637 }
638 /* recv wakeup fd */
639 len = ustcomm_recv_fds_unix_sock(sock, &wakeup_fd, 1);
640 if (len <= 0) {
641 if (len < 0) {
642 ret = len;
643 goto error_recv_data;
644 } else {
645 ret = -EIO;
646 goto error_recv_data;
647 }
648 }
649 channel_data->u.channel.wakeup_fd = wakeup_fd;
650 *_channel_data = channel_data;
651 return 0;
652
653 error_recv_data:
654 free(channel_data->u.channel.data);
655 error:
656 free(channel_data);
657 error_alloc:
658 return ret;
659 }
660
661 int ustctl_recv_stream_from_consumer(int sock,
662 struct lttng_ust_object_data **_stream_data)
663 {
664 struct lttng_ust_object_data *stream_data;
665 ssize_t len;
666 int ret;
667 int fds[2];
668
669 stream_data = zmalloc(sizeof(*stream_data));
670 if (!stream_data) {
671 ret = -ENOMEM;
672 goto error_alloc;
673 }
674
675 stream_data->type = LTTNG_UST_OBJECT_TYPE_STREAM;
676 stream_data->handle = -1;
677
678 /* recv mmap size */
679 len = ustcomm_recv_unix_sock(sock, &stream_data->size,
680 sizeof(stream_data->size));
681 if (len != sizeof(stream_data->size)) {
682 if (len < 0)
683 ret = len;
684 else
685 ret = -EINVAL;
686 goto error;
687 }
688 if (stream_data->size == -1) {
689 ret = -LTTNG_UST_ERR_NOENT;
690 goto error;
691 }
692
693 /* recv stream nr */
694 len = ustcomm_recv_unix_sock(sock, &stream_data->u.stream.stream_nr,
695 sizeof(stream_data->u.stream.stream_nr));
696 if (len != sizeof(stream_data->u.stream.stream_nr)) {
697 if (len < 0)
698 ret = len;
699 else
700 ret = -EINVAL;
701 goto error;
702 }
703
704 /* recv shm fd and wakeup fd */
705 len = ustcomm_recv_fds_unix_sock(sock, fds, 2);
706 if (len <= 0) {
707 if (len < 0) {
708 ret = len;
709 goto error;
710 } else {
711 ret = -EIO;
712 goto error;
713 }
714 }
715 stream_data->u.stream.shm_fd = fds[0];
716 stream_data->u.stream.wakeup_fd = fds[1];
717 *_stream_data = stream_data;
718 return 0;
719
720 error:
721 free(stream_data);
722 error_alloc:
723 return ret;
724 }
725
726 int ustctl_send_channel_to_ust(int sock, int session_handle,
727 struct lttng_ust_object_data *channel_data)
728 {
729 struct ustcomm_ust_msg lum;
730 struct ustcomm_ust_reply lur;
731 int ret;
732
733 if (!channel_data)
734 return -EINVAL;
735
736 memset(&lum, 0, sizeof(lum));
737 lum.handle = session_handle;
738 lum.cmd = LTTNG_UST_CHANNEL;
739 lum.u.channel.len = channel_data->size;
740 lum.u.channel.type = channel_data->u.channel.type;
741 ret = ustcomm_send_app_msg(sock, &lum);
742 if (ret)
743 return ret;
744
745 ret = ustctl_send_channel(sock,
746 channel_data->u.channel.type,
747 channel_data->u.channel.data,
748 channel_data->size,
749 channel_data->u.channel.wakeup_fd,
750 1);
751 if (ret)
752 return ret;
753 ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
754 if (!ret) {
755 if (lur.ret_val >= 0) {
756 channel_data->handle = lur.ret_val;
757 }
758 }
759 return ret;
760 }
761
762 int ustctl_send_stream_to_ust(int sock,
763 struct lttng_ust_object_data *channel_data,
764 struct lttng_ust_object_data *stream_data)
765 {
766 struct ustcomm_ust_msg lum;
767 struct ustcomm_ust_reply lur;
768 int ret;
769
770 memset(&lum, 0, sizeof(lum));
771 lum.handle = channel_data->handle;
772 lum.cmd = LTTNG_UST_STREAM;
773 lum.u.stream.len = stream_data->size;
774 lum.u.stream.stream_nr = stream_data->u.stream.stream_nr;
775 ret = ustcomm_send_app_msg(sock, &lum);
776 if (ret)
777 return ret;
778
779 assert(stream_data);
780 assert(stream_data->type == LTTNG_UST_OBJECT_TYPE_STREAM);
781
782 ret = ustctl_send_stream(sock,
783 stream_data->u.stream.stream_nr,
784 stream_data->size,
785 stream_data->u.stream.shm_fd,
786 stream_data->u.stream.wakeup_fd, 1);
787 if (ret)
788 return ret;
789 return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
790 }
791
792 int ustctl_duplicate_ust_object_data(struct lttng_ust_object_data **dest,
793 struct lttng_ust_object_data *src)
794 {
795 struct lttng_ust_object_data *obj;
796 int ret;
797
798 if (src->handle != -1) {
799 ret = -EINVAL;
800 goto error;
801 }
802
803 obj = zmalloc(sizeof(*obj));
804 if (!obj) {
805 ret = -ENOMEM;
806 goto error;
807 }
808
809 obj->type = src->type;
810 obj->handle = src->handle;
811 obj->size = src->size;
812
813 switch (obj->type) {
814 case LTTNG_UST_OBJECT_TYPE_CHANNEL:
815 {
816 obj->u.channel.type = src->u.channel.type;
817 if (src->u.channel.wakeup_fd >= 0) {
818 obj->u.channel.wakeup_fd =
819 dup(src->u.channel.wakeup_fd);
820 if (obj->u.channel.wakeup_fd < 0) {
821 ret = errno;
822 goto chan_error_wakeup_fd;
823 }
824 } else {
825 obj->u.channel.wakeup_fd =
826 src->u.channel.wakeup_fd;
827 }
828 obj->u.channel.data = zmalloc(obj->size);
829 if (!obj->u.channel.data) {
830 ret = -ENOMEM;
831 goto chan_error_alloc;
832 }
833 memcpy(obj->u.channel.data, src->u.channel.data, obj->size);
834 break;
835
836 chan_error_alloc:
837 if (src->u.channel.wakeup_fd >= 0) {
838 int closeret;
839
840 closeret = close(obj->u.channel.wakeup_fd);
841 if (closeret) {
842 PERROR("close");
843 }
844 }
845 chan_error_wakeup_fd:
846 goto error_type;
847
848 }
849
850 case LTTNG_UST_OBJECT_TYPE_STREAM:
851 {
852 obj->u.stream.stream_nr = src->u.stream.stream_nr;
853 if (src->u.stream.wakeup_fd >= 0) {
854 obj->u.stream.wakeup_fd =
855 dup(src->u.stream.wakeup_fd);
856 if (obj->u.stream.wakeup_fd < 0) {
857 ret = errno;
858 goto stream_error_wakeup_fd;
859 }
860 } else {
861 obj->u.stream.wakeup_fd =
862 src->u.stream.wakeup_fd;
863 }
864
865 if (src->u.stream.shm_fd >= 0) {
866 obj->u.stream.shm_fd =
867 dup(src->u.stream.shm_fd);
868 if (obj->u.stream.shm_fd < 0) {
869 ret = errno;
870 goto stream_error_shm_fd;
871 }
872 } else {
873 obj->u.stream.shm_fd =
874 src->u.stream.shm_fd;
875 }
876 break;
877
878 stream_error_shm_fd:
879 if (src->u.stream.wakeup_fd >= 0) {
880 int closeret;
881
882 closeret = close(obj->u.stream.wakeup_fd);
883 if (closeret) {
884 PERROR("close");
885 }
886 }
887 stream_error_wakeup_fd:
888 goto error_type;
889 }
890
891 default:
892 ret = -EINVAL;
893 goto error_type;
894 }
895
896 *dest = obj;
897 return 0;
898
899 error_type:
900 free(obj);
901 error:
902 return ret;
903 }
904
905
906 /* Buffer operations */
907
908 struct ustctl_consumer_channel *
909 ustctl_create_channel(struct ustctl_consumer_channel_attr *attr)
910 {
911 struct ustctl_consumer_channel *chan;
912 const char *transport_name;
913 struct lttng_transport *transport;
914
915 switch (attr->type) {
916 case LTTNG_UST_CHAN_PER_CPU:
917 if (attr->output == LTTNG_UST_MMAP) {
918 transport_name = attr->overwrite ?
919 "relay-overwrite-mmap" : "relay-discard-mmap";
920 } else {
921 return NULL;
922 }
923 break;
924 case LTTNG_UST_CHAN_METADATA:
925 if (attr->output == LTTNG_UST_MMAP)
926 transport_name = "relay-metadata-mmap";
927 else
928 return NULL;
929 break;
930 default:
931 transport_name = "<unknown>";
932 return NULL;
933 }
934
935 transport = lttng_transport_find(transport_name);
936 if (!transport) {
937 DBG("LTTng transport %s not found\n",
938 transport_name);
939 return NULL;
940 }
941
942 chan = zmalloc(sizeof(*chan));
943 if (!chan)
944 return NULL;
945
946 chan->chan = transport->ops.channel_create(transport_name, NULL,
947 attr->subbuf_size, attr->num_subbuf,
948 attr->switch_timer_interval,
949 attr->read_timer_interval,
950 attr->uuid);
951 if (!chan->chan) {
952 goto chan_error;
953 }
954 chan->chan->ops = &transport->ops;
955 memcpy(&chan->attr, attr, sizeof(chan->attr));
956 return chan;
957
958 chan_error:
959 free(chan);
960 return NULL;
961 }
962
963 void ustctl_destroy_channel(struct ustctl_consumer_channel *chan)
964 {
965 chan->chan->ops->channel_destroy(chan->chan);
966 free(chan);
967 }
968
969 int ustctl_send_channel_to_sessiond(int sock,
970 struct ustctl_consumer_channel *channel)
971 {
972 struct shm_object_table *table;
973
974 table = channel->chan->handle->table;
975 if (table->size <= 0)
976 return -EINVAL;
977 return ustctl_send_channel(sock,
978 channel->attr.type,
979 table->objects[0].memory_map,
980 table->objects[0].memory_map_size,
981 channel->wakeup_fd,
982 0);
983 }
984
985 int ustctl_send_stream_to_sessiond(int sock,
986 struct ustctl_consumer_stream *stream)
987 {
988 if (!stream)
989 return ustctl_send_stream(sock, -1U, -1U, -1, -1, 0);
990
991 return ustctl_send_stream(sock,
992 stream->cpu,
993 stream->memory_map_size,
994 stream->shm_fd, stream->wakeup_fd,
995 0);
996 }
997
998 int ustctl_write_metadata_to_channel(
999 struct ustctl_consumer_channel *channel,
1000 const char *metadata_str, /* NOT null-terminated */
1001 size_t len) /* metadata length */
1002 {
1003 struct lttng_ust_lib_ring_buffer_ctx ctx;
1004 struct lttng_channel *chan = channel->chan;
1005 const char *str = metadata_str;
1006 int ret = 0, waitret;
1007 size_t reserve_len, pos;
1008
1009 for (pos = 0; pos < len; pos += reserve_len) {
1010 reserve_len = min_t(size_t,
1011 chan->ops->packet_avail_size(chan->chan, chan->handle),
1012 len - pos);
1013 lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len,
1014 sizeof(char), -1, chan->handle);
1015 /*
1016 * We don't care about metadata buffer's records lost
1017 * count, because we always retry here. Report error if
1018 * we need to bail out after timeout or being
1019 * interrupted.
1020 */
1021 waitret = wait_cond_interruptible_timeout(
1022 ({
1023 ret = chan->ops->event_reserve(&ctx, 0);
1024 ret != -ENOBUFS || !ret;
1025 }),
1026 LTTNG_METADATA_TIMEOUT_MSEC);
1027 if (waitret == -ETIMEDOUT || waitret == -EINTR || ret) {
1028 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
1029 waitret == -EINTR ? "interrupted" :
1030 (ret == -ENOBUFS ? "timeout" : "I/O error"));
1031 if (waitret == -EINTR)
1032 ret = waitret;
1033 goto end;
1034 }
1035 chan->ops->event_write(&ctx, &str[pos], reserve_len);
1036 chan->ops->event_commit(&ctx);
1037 }
1038 end:
1039 return ret;
1040 }
1041
1042 int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel *consumer_chan)
1043 {
1044 struct channel *chan;
1045
1046 chan = consumer_chan->chan->chan;
1047 return ring_buffer_channel_close_wait_fd(&chan->backend.config,
1048 chan, chan->handle);
1049 }
1050
1051 int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel *consumer_chan)
1052 {
1053 struct channel *chan;
1054
1055 chan = consumer_chan->chan->chan;
1056 return ring_buffer_channel_close_wakeup_fd(&chan->backend.config,
1057 chan, chan->handle);
1058 }
1059
1060 int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream *stream)
1061 {
1062 struct channel *chan;
1063
1064 chan = stream->chan->chan->chan;
1065 return ring_buffer_stream_close_wait_fd(&chan->backend.config,
1066 chan, stream->handle, stream->cpu);
1067 }
1068
1069 int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream *stream)
1070 {
1071 struct channel *chan;
1072
1073 chan = stream->chan->chan->chan;
1074 return ring_buffer_stream_close_wakeup_fd(&chan->backend.config,
1075 chan, stream->handle, stream->cpu);
1076 }
1077
1078 struct ustctl_consumer_stream *
1079 ustctl_create_stream(struct ustctl_consumer_channel *channel,
1080 int cpu)
1081 {
1082 struct ustctl_consumer_stream *stream;
1083 struct lttng_ust_shm_handle *handle;
1084 struct channel *chan;
1085 int shm_fd, wait_fd, wakeup_fd;
1086 uint64_t memory_map_size;
1087 struct lttng_ust_lib_ring_buffer *buf;
1088 int ret;
1089
1090 if (!channel)
1091 return NULL;
1092 handle = channel->chan->handle;
1093 if (!handle)
1094 return NULL;
1095
1096 chan = channel->chan->chan;
1097 buf = channel_get_ring_buffer(&chan->backend.config,
1098 chan, cpu, handle, &shm_fd, &wait_fd,
1099 &wakeup_fd, &memory_map_size);
1100 if (!buf)
1101 return NULL;
1102 ret = lib_ring_buffer_open_read(buf, handle);
1103 if (ret)
1104 return NULL;
1105
1106 stream = zmalloc(sizeof(*stream));
1107 if (!stream)
1108 goto alloc_error;
1109 stream->handle = handle;
1110 stream->buf = buf;
1111 stream->chan = channel;
1112 stream->shm_fd = shm_fd;
1113 stream->wait_fd = wait_fd;
1114 stream->wakeup_fd = wakeup_fd;
1115 stream->memory_map_size = memory_map_size;
1116 stream->cpu = cpu;
1117 return stream;
1118
1119 alloc_error:
1120 return NULL;
1121 }
1122
1123 void ustctl_destroy_stream(struct ustctl_consumer_stream *stream)
1124 {
1125 struct lttng_ust_lib_ring_buffer *buf;
1126 struct ustctl_consumer_channel *consumer_chan;
1127
1128 assert(stream);
1129 buf = stream->buf;
1130 consumer_chan = stream->chan;
1131 lib_ring_buffer_release_read(buf, consumer_chan->chan->handle);
1132 free(stream);
1133 }
1134
1135 int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel *chan)
1136 {
1137 if (!chan)
1138 return -EINVAL;
1139 return shm_get_wait_fd(chan->chan->handle,
1140 &chan->chan->handle->chan._ref);
1141 }
1142
1143 int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel *chan)
1144 {
1145 if (!chan)
1146 return -EINVAL;
1147 return shm_get_wakeup_fd(chan->chan->handle,
1148 &chan->chan->handle->chan._ref);
1149 }
1150
1151 int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream *stream)
1152 {
1153 struct lttng_ust_lib_ring_buffer *buf;
1154 struct ustctl_consumer_channel *consumer_chan;
1155
1156 if (!stream)
1157 return -EINVAL;
1158 buf = stream->buf;
1159 consumer_chan = stream->chan;
1160 return shm_get_wait_fd(consumer_chan->chan->handle, &buf->self._ref);
1161 }
1162
1163 int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream *stream)
1164 {
1165 struct lttng_ust_lib_ring_buffer *buf;
1166 struct ustctl_consumer_channel *consumer_chan;
1167
1168 if (!stream)
1169 return -EINVAL;
1170 buf = stream->buf;
1171 consumer_chan = stream->chan;
1172 return shm_get_wakeup_fd(consumer_chan->chan->handle, &buf->self._ref);
1173 }
1174
1175 /* For mmap mode, readable without "get" operation */
1176
1177 void *ustctl_get_mmap_base(struct ustctl_consumer_stream *stream)
1178 {
1179 struct lttng_ust_lib_ring_buffer *buf;
1180 struct ustctl_consumer_channel *consumer_chan;
1181
1182 if (!stream)
1183 return NULL;
1184 buf = stream->buf;
1185 consumer_chan = stream->chan;
1186 return shmp(consumer_chan->chan->handle, buf->backend.memory_map);
1187 }
1188
1189 /* returns the length to mmap. */
1190 int ustctl_get_mmap_len(struct ustctl_consumer_stream *stream,
1191 unsigned long *len)
1192 {
1193 struct ustctl_consumer_channel *consumer_chan;
1194 unsigned long mmap_buf_len;
1195 struct channel *chan;
1196
1197 if (!stream)
1198 return -EINVAL;
1199 consumer_chan = stream->chan;
1200 chan = consumer_chan->chan->chan;
1201 if (chan->backend.config.output != RING_BUFFER_MMAP)
1202 return -EINVAL;
1203 mmap_buf_len = chan->backend.buf_size;
1204 if (chan->backend.extra_reader_sb)
1205 mmap_buf_len += chan->backend.subbuf_size;
1206 if (mmap_buf_len > INT_MAX)
1207 return -EFBIG;
1208 *len = mmap_buf_len;
1209 return 0;
1210 }
1211
1212 /* returns the maximum size for sub-buffers. */
1213 int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream *stream,
1214 unsigned long *len)
1215 {
1216 struct ustctl_consumer_channel *consumer_chan;
1217 struct channel *chan;
1218
1219 if (!stream)
1220 return -EINVAL;
1221 consumer_chan = stream->chan;
1222 chan = consumer_chan->chan->chan;
1223 *len = chan->backend.subbuf_size;
1224 return 0;
1225 }
1226
1227 /*
1228 * For mmap mode, operate on the current packet (between get/put or
1229 * get_next/put_next).
1230 */
1231
1232 /* returns the offset of the subbuffer belonging to the mmap reader. */
1233 int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream *stream,
1234 unsigned long *off)
1235 {
1236 struct channel *chan;
1237 unsigned long sb_bindex;
1238 struct lttng_ust_lib_ring_buffer *buf;
1239 struct ustctl_consumer_channel *consumer_chan;
1240
1241 if (!stream)
1242 return -EINVAL;
1243 buf = stream->buf;
1244 consumer_chan = stream->chan;
1245 chan = consumer_chan->chan->chan;
1246 if (chan->backend.config.output != RING_BUFFER_MMAP)
1247 return -EINVAL;
1248 sb_bindex = subbuffer_id_get_index(&chan->backend.config,
1249 buf->backend.buf_rsb.id);
1250 *off = shmp(consumer_chan->chan->handle,
1251 shmp_index(consumer_chan->chan->handle, buf->backend.array, sb_bindex)->shmp)->mmap_offset;
1252 return 0;
1253 }
1254
1255 /* returns the size of the current sub-buffer, without padding (for mmap). */
1256 int ustctl_get_subbuf_size(struct ustctl_consumer_stream *stream,
1257 unsigned long *len)
1258 {
1259 struct ustctl_consumer_channel *consumer_chan;
1260 struct channel *chan;
1261 struct lttng_ust_lib_ring_buffer *buf;
1262
1263 if (!stream)
1264 return -EINVAL;
1265
1266 buf = stream->buf;
1267 consumer_chan = stream->chan;
1268 chan = consumer_chan->chan->chan;
1269 *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf,
1270 consumer_chan->chan->handle);
1271 return 0;
1272 }
1273
1274 /* returns the size of the current sub-buffer, without padding (for mmap). */
1275 int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream *stream,
1276 unsigned long *len)
1277 {
1278 struct ustctl_consumer_channel *consumer_chan;
1279 struct channel *chan;
1280 struct lttng_ust_lib_ring_buffer *buf;
1281
1282 if (!stream)
1283 return -EINVAL;
1284 buf = stream->buf;
1285 consumer_chan = stream->chan;
1286 chan = consumer_chan->chan->chan;
1287 *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf,
1288 consumer_chan->chan->handle);
1289 *len = PAGE_ALIGN(*len);
1290 return 0;
1291 }
1292
1293 /* Get exclusive read access to the next sub-buffer that can be read. */
1294 int ustctl_get_next_subbuf(struct ustctl_consumer_stream *stream)
1295 {
1296 struct lttng_ust_lib_ring_buffer *buf;
1297 struct ustctl_consumer_channel *consumer_chan;
1298
1299 if (!stream)
1300 return -EINVAL;
1301 buf = stream->buf;
1302 consumer_chan = stream->chan;
1303 return lib_ring_buffer_get_next_subbuf(buf,
1304 consumer_chan->chan->handle);
1305 }
1306
1307
1308 /* Release exclusive sub-buffer access, move consumer forward. */
1309 int ustctl_put_next_subbuf(struct ustctl_consumer_stream *stream)
1310 {
1311 struct lttng_ust_lib_ring_buffer *buf;
1312 struct ustctl_consumer_channel *consumer_chan;
1313
1314 if (!stream)
1315 return -EINVAL;
1316 buf = stream->buf;
1317 consumer_chan = stream->chan;
1318 lib_ring_buffer_put_next_subbuf(buf, consumer_chan->chan->handle);
1319 return 0;
1320 }
1321
1322 /* snapshot */
1323
1324 /* Get a snapshot of the current ring buffer producer and consumer positions */
1325 int ustctl_snapshot(struct ustctl_consumer_stream *stream)
1326 {
1327 struct lttng_ust_lib_ring_buffer *buf;
1328 struct ustctl_consumer_channel *consumer_chan;
1329
1330 if (!stream)
1331 return -EINVAL;
1332 buf = stream->buf;
1333 consumer_chan = stream->chan;
1334 return lib_ring_buffer_snapshot(buf, &buf->cons_snapshot,
1335 &buf->prod_snapshot, consumer_chan->chan->handle);
1336 }
1337
1338 /* Get the consumer position (iteration start) */
1339 int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream *stream,
1340 unsigned long *pos)
1341 {
1342 struct lttng_ust_lib_ring_buffer *buf;
1343
1344 if (!stream)
1345 return -EINVAL;
1346 buf = stream->buf;
1347 *pos = buf->cons_snapshot;
1348 return 0;
1349 }
1350
1351 /* Get the producer position (iteration end) */
1352 int ustctl_snapshot_get_produced(struct ustctl_consumer_stream *stream,
1353 unsigned long *pos)
1354 {
1355 struct lttng_ust_lib_ring_buffer *buf;
1356
1357 if (!stream)
1358 return -EINVAL;
1359 buf = stream->buf;
1360 *pos = buf->prod_snapshot;
1361 return 0;
1362 }
1363
1364 /* Get exclusive read access to the specified sub-buffer position */
1365 int ustctl_get_subbuf(struct ustctl_consumer_stream *stream,
1366 unsigned long *pos)
1367 {
1368 struct lttng_ust_lib_ring_buffer *buf;
1369 struct ustctl_consumer_channel *consumer_chan;
1370
1371 if (!stream)
1372 return -EINVAL;
1373 buf = stream->buf;
1374 consumer_chan = stream->chan;
1375 return lib_ring_buffer_get_subbuf(buf, *pos,
1376 consumer_chan->chan->handle);
1377 }
1378
1379 /* Release exclusive sub-buffer access */
1380 int ustctl_put_subbuf(struct ustctl_consumer_stream *stream)
1381 {
1382 struct lttng_ust_lib_ring_buffer *buf;
1383 struct ustctl_consumer_channel *consumer_chan;
1384
1385 if (!stream)
1386 return -EINVAL;
1387 buf = stream->buf;
1388 consumer_chan = stream->chan;
1389 lib_ring_buffer_put_subbuf(buf, consumer_chan->chan->handle);
1390 return 0;
1391 }
1392
1393 void ustctl_flush_buffer(struct ustctl_consumer_stream *stream,
1394 int producer_active)
1395 {
1396 struct lttng_ust_lib_ring_buffer *buf;
1397 struct ustctl_consumer_channel *consumer_chan;
1398
1399 assert(stream);
1400 buf = stream->buf;
1401 consumer_chan = stream->chan;
1402 lib_ring_buffer_switch_slow(buf,
1403 producer_active ? SWITCH_ACTIVE : SWITCH_FLUSH,
1404 consumer_chan->chan->handle);
1405 }
1406
1407 /*
1408 * Returns 0 on success, negative error value on error.
1409 */
1410 int ustctl_recv_reg_msg(int sock,
1411 enum ustctl_socket_type *type,
1412 uint32_t *major,
1413 uint32_t *minor,
1414 uint32_t *pid,
1415 uint32_t *ppid,
1416 uint32_t *uid,
1417 uint32_t *gid,
1418 uint32_t *bits_per_long,
1419 uint32_t *uint8_t_alignment,
1420 uint32_t *uint16_t_alignment,
1421 uint32_t *uint32_t_alignment,
1422 uint32_t *uint64_t_alignment,
1423 uint32_t *long_alignment,
1424 int *byte_order,
1425 char *name)
1426 {
1427 ssize_t len;
1428 struct ustctl_reg_msg reg_msg;
1429
1430 len = ustcomm_recv_unix_sock(sock, &reg_msg, sizeof(reg_msg));
1431 if (len > 0 && len != sizeof(reg_msg))
1432 return -EIO;
1433 if (len == 0)
1434 return -EPIPE;
1435 if (len < 0)
1436 return len;
1437
1438 if (reg_msg.magic == LTTNG_UST_COMM_MAGIC) {
1439 *byte_order = BYTE_ORDER == BIG_ENDIAN ?
1440 BIG_ENDIAN : LITTLE_ENDIAN;
1441 } else if (reg_msg.magic == bswap_32(LTTNG_UST_COMM_MAGIC)) {
1442 *byte_order = BYTE_ORDER == BIG_ENDIAN ?
1443 LITTLE_ENDIAN : BIG_ENDIAN;
1444 } else {
1445 return -LTTNG_UST_ERR_INVAL_MAGIC;
1446 }
1447 switch (reg_msg.socket_type) {
1448 case 0: *type = USTCTL_SOCKET_CMD;
1449 break;
1450 case 1: *type = USTCTL_SOCKET_NOTIFY;
1451 break;
1452 default:
1453 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE;
1454 }
1455 *major = reg_msg.major;
1456 *minor = reg_msg.minor;
1457 *pid = reg_msg.pid;
1458 *ppid = reg_msg.ppid;
1459 *uid = reg_msg.uid;
1460 *gid = reg_msg.gid;
1461 *bits_per_long = reg_msg.bits_per_long;
1462 *uint8_t_alignment = reg_msg.uint8_t_alignment;
1463 *uint16_t_alignment = reg_msg.uint16_t_alignment;
1464 *uint32_t_alignment = reg_msg.uint32_t_alignment;
1465 *uint64_t_alignment = reg_msg.uint64_t_alignment;
1466 *long_alignment = reg_msg.long_alignment;
1467 memcpy(name, reg_msg.name, LTTNG_UST_ABI_PROCNAME_LEN);
1468 if (reg_msg.major != LTTNG_UST_ABI_MAJOR_VERSION) {
1469 return -LTTNG_UST_ERR_UNSUP_MAJOR;
1470 }
1471
1472 return 0;
1473 }
1474
1475 int ustctl_recv_notify(int sock, enum ustctl_notify_cmd *notify_cmd)
1476 {
1477 struct ustcomm_notify_hdr header;
1478 ssize_t len;
1479
1480 len = ustcomm_recv_unix_sock(sock, &header, sizeof(header));
1481 if (len > 0 && len != sizeof(header))
1482 return -EIO;
1483 if (len == 0)
1484 return -EPIPE;
1485 if (len < 0)
1486 return len;
1487 switch (header.notify_cmd) {
1488 case 0:
1489 *notify_cmd = USTCTL_NOTIFY_CMD_EVENT;
1490 break;
1491 case 1:
1492 *notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL;
1493 break;
1494 default:
1495 return -EINVAL;
1496 }
1497 return 0;
1498 }
1499
1500 /*
1501 * Returns 0 on success, negative error value on error.
1502 */
1503 int ustctl_recv_register_event(int sock,
1504 int *session_objd,
1505 int *channel_objd,
1506 char *event_name,
1507 int *loglevel,
1508 char **signature,
1509 size_t *nr_fields,
1510 struct ustctl_field **fields,
1511 char **model_emf_uri)
1512 {
1513 ssize_t len;
1514 struct ustcomm_notify_event_msg msg;
1515 size_t signature_len, fields_len, model_emf_uri_len;
1516 char *a_sign = NULL, *a_model_emf_uri = NULL;
1517 struct ustctl_field *a_fields = NULL;
1518
1519 len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg));
1520 if (len > 0 && len != sizeof(msg))
1521 return -EIO;
1522 if (len == 0)
1523 return -EPIPE;
1524 if (len < 0)
1525 return len;
1526
1527 *session_objd = msg.session_objd;
1528 *channel_objd = msg.channel_objd;
1529 strncpy(event_name, msg.event_name, LTTNG_UST_SYM_NAME_LEN);
1530 event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1531 *loglevel = msg.loglevel;
1532 signature_len = msg.signature_len;
1533 fields_len = msg.fields_len;
1534
1535 if (fields_len % sizeof(*a_fields) != 0) {
1536 return -EINVAL;
1537 }
1538
1539 model_emf_uri_len = msg.model_emf_uri_len;
1540
1541 /* recv signature. contains at least \0. */
1542 a_sign = zmalloc(signature_len);
1543 if (!a_sign)
1544 return -ENOMEM;
1545 len = ustcomm_recv_unix_sock(sock, a_sign, signature_len);
1546 if (len > 0 && len != signature_len) {
1547 len = -EIO;
1548 goto signature_error;
1549 }
1550 if (len == 0) {
1551 len = -EPIPE;
1552 goto signature_error;
1553 }
1554 if (len < 0) {
1555 goto signature_error;
1556 }
1557 /* Enforce end of string */
1558 signature[signature_len - 1] = '\0';
1559
1560 /* recv fields */
1561 if (fields_len) {
1562 a_fields = zmalloc(fields_len);
1563 if (!a_fields) {
1564 len = -ENOMEM;
1565 goto signature_error;
1566 }
1567 len = ustcomm_recv_unix_sock(sock, a_fields, fields_len);
1568 if (len > 0 && len != fields_len) {
1569 len = -EIO;
1570 goto fields_error;
1571 }
1572 if (len == 0) {
1573 len = -EPIPE;
1574 goto fields_error;
1575 }
1576 if (len < 0) {
1577 goto fields_error;
1578 }
1579 }
1580
1581 if (model_emf_uri_len) {
1582 /* recv model_emf_uri_len */
1583 a_model_emf_uri = zmalloc(model_emf_uri_len);
1584 if (!a_model_emf_uri) {
1585 len = -ENOMEM;
1586 goto fields_error;
1587 }
1588 len = ustcomm_recv_unix_sock(sock, a_model_emf_uri,
1589 model_emf_uri_len);
1590 if (len > 0 && len != model_emf_uri_len) {
1591 len = -EIO;
1592 goto model_error;
1593 }
1594 if (len == 0) {
1595 len = -EPIPE;
1596 goto model_error;
1597 }
1598 if (len < 0) {
1599 goto model_error;
1600 }
1601 /* Enforce end of string */
1602 a_model_emf_uri[model_emf_uri_len - 1] = '\0';
1603 }
1604
1605 *signature = a_sign;
1606 *nr_fields = fields_len / sizeof(*a_fields);
1607 *fields = a_fields;
1608 *model_emf_uri = a_model_emf_uri;
1609
1610 return 0;
1611
1612 model_error:
1613 free(a_model_emf_uri);
1614 fields_error:
1615 free(a_fields);
1616 signature_error:
1617 free(a_sign);
1618 return len;
1619 }
1620
1621 /*
1622 * Returns 0 on success, negative error value on error.
1623 */
1624 int ustctl_reply_register_event(int sock,
1625 uint32_t id,
1626 int ret_code)
1627 {
1628 ssize_t len;
1629 struct {
1630 struct ustcomm_notify_hdr header;
1631 struct ustcomm_notify_event_reply r;
1632 } reply;
1633
1634 memset(&reply, 0, sizeof(reply));
1635 reply.header.notify_cmd = USTCTL_NOTIFY_CMD_EVENT;
1636 reply.r.ret_code = ret_code;
1637 reply.r.event_id = id;
1638 len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply));
1639 if (len > 0 && len != sizeof(reply))
1640 return -EIO;
1641 if (len < 0)
1642 return len;
1643 return 0;
1644 }
1645
1646 /*
1647 * Returns 0 on success, negative UST or system error value on error.
1648 */
1649 int ustctl_recv_register_channel(int sock,
1650 int *session_objd, /* session descriptor (output) */
1651 int *channel_objd, /* channel descriptor (output) */
1652 size_t *nr_fields,
1653 struct ustctl_field **fields)
1654 {
1655 ssize_t len;
1656 struct ustcomm_notify_channel_msg msg;
1657 size_t fields_len;
1658 struct ustctl_field *a_fields;
1659
1660 len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg));
1661 if (len > 0 && len != sizeof(msg))
1662 return -EIO;
1663 if (len == 0)
1664 return -EPIPE;
1665 if (len < 0)
1666 return len;
1667
1668 *session_objd = msg.session_objd;
1669 *channel_objd = msg.channel_objd;
1670 fields_len = msg.ctx_fields_len;
1671
1672 if (fields_len % sizeof(*a_fields) != 0) {
1673 return -EINVAL;
1674 }
1675
1676 /* recv fields */
1677 if (fields_len) {
1678 a_fields = zmalloc(fields_len);
1679 if (!a_fields) {
1680 len = -ENOMEM;
1681 goto alloc_error;
1682 }
1683 len = ustcomm_recv_unix_sock(sock, a_fields, fields_len);
1684 if (len > 0 && len != fields_len) {
1685 len = -EIO;
1686 goto fields_error;
1687 }
1688 if (len == 0) {
1689 len = -EPIPE;
1690 goto fields_error;
1691 }
1692 if (len < 0) {
1693 goto fields_error;
1694 }
1695 *fields = a_fields;
1696 } else {
1697 *fields = NULL;
1698 }
1699 *nr_fields = fields_len / sizeof(*a_fields);
1700 return 0;
1701
1702 fields_error:
1703 free(a_fields);
1704 alloc_error:
1705 return len;
1706 }
1707
1708 /*
1709 * Returns 0 on success, negative error value on error.
1710 */
1711 int ustctl_reply_register_channel(int sock,
1712 uint32_t chan_id,
1713 enum ustctl_channel_header header_type,
1714 int ret_code)
1715 {
1716 ssize_t len;
1717 struct {
1718 struct ustcomm_notify_hdr header;
1719 struct ustcomm_notify_channel_reply r;
1720 } reply;
1721
1722 memset(&reply, 0, sizeof(reply));
1723 reply.header.notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL;
1724 reply.r.ret_code = ret_code;
1725 reply.r.chan_id = chan_id;
1726 switch (header_type) {
1727 case USTCTL_CHANNEL_HEADER_COMPACT:
1728 reply.r.header_type = 1;
1729 break;
1730 case USTCTL_CHANNEL_HEADER_LARGE:
1731 reply.r.header_type = 2;
1732 break;
1733 default:
1734 reply.r.header_type = 0;
1735 break;
1736 }
1737 len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply));
1738 if (len > 0 && len != sizeof(reply))
1739 return -EIO;
1740 if (len < 0)
1741 return len;
1742 return 0;
1743 }
1744
1745 static __attribute__((constructor))
1746 void ustctl_init(void)
1747 {
1748 init_usterr();
1749 lttng_ring_buffer_metadata_client_init();
1750 lttng_ring_buffer_client_overwrite_init();
1751 lttng_ring_buffer_client_discard_init();
1752 lib_ringbuffer_signal_init();
1753 }
1754
1755 static __attribute__((destructor))
1756 void ustctl_exit(void)
1757 {
1758 lttng_ring_buffer_client_discard_exit();
1759 lttng_ring_buffer_client_overwrite_exit();
1760 lttng_ring_buffer_metadata_client_exit();
1761 }
This page took 0.061957 seconds and 5 git commands to generate.