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