Add mutex for channel wakeup fd update
[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 chan->wait_fd = ustctl_channel_get_wait_fd(chan);
957 chan->wakeup_fd = ustctl_channel_get_wakeup_fd(chan);
958 return chan;
959
960 chan_error:
961 free(chan);
962 return NULL;
963 }
964
965 void ustctl_destroy_channel(struct ustctl_consumer_channel *chan)
966 {
967 chan->chan->ops->channel_destroy(chan->chan);
968 free(chan);
969 }
970
971 int ustctl_send_channel_to_sessiond(int sock,
972 struct ustctl_consumer_channel *channel)
973 {
974 struct shm_object_table *table;
975
976 table = channel->chan->handle->table;
977 if (table->size <= 0)
978 return -EINVAL;
979 return ustctl_send_channel(sock,
980 channel->attr.type,
981 table->objects[0].memory_map,
982 table->objects[0].memory_map_size,
983 channel->wakeup_fd,
984 0);
985 }
986
987 int ustctl_send_stream_to_sessiond(int sock,
988 struct ustctl_consumer_stream *stream)
989 {
990 if (!stream)
991 return ustctl_send_stream(sock, -1U, -1U, -1, -1, 0);
992
993 return ustctl_send_stream(sock,
994 stream->cpu,
995 stream->memory_map_size,
996 stream->shm_fd, stream->wakeup_fd,
997 0);
998 }
999
1000 int ustctl_write_metadata_to_channel(
1001 struct ustctl_consumer_channel *channel,
1002 const char *metadata_str, /* NOT null-terminated */
1003 size_t len) /* metadata length */
1004 {
1005 struct lttng_ust_lib_ring_buffer_ctx ctx;
1006 struct lttng_channel *chan = channel->chan;
1007 const char *str = metadata_str;
1008 int ret = 0, waitret;
1009 size_t reserve_len, pos;
1010
1011 for (pos = 0; pos < len; pos += reserve_len) {
1012 reserve_len = min_t(size_t,
1013 chan->ops->packet_avail_size(chan->chan, chan->handle),
1014 len - pos);
1015 lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len,
1016 sizeof(char), -1, chan->handle);
1017 /*
1018 * We don't care about metadata buffer's records lost
1019 * count, because we always retry here. Report error if
1020 * we need to bail out after timeout or being
1021 * interrupted.
1022 */
1023 waitret = wait_cond_interruptible_timeout(
1024 ({
1025 ret = chan->ops->event_reserve(&ctx, 0);
1026 ret != -ENOBUFS || !ret;
1027 }),
1028 LTTNG_METADATA_TIMEOUT_MSEC);
1029 if (waitret == -ETIMEDOUT || waitret == -EINTR || ret) {
1030 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
1031 waitret == -EINTR ? "interrupted" :
1032 (ret == -ENOBUFS ? "timeout" : "I/O error"));
1033 if (waitret == -EINTR)
1034 ret = waitret;
1035 goto end;
1036 }
1037 chan->ops->event_write(&ctx, &str[pos], reserve_len);
1038 chan->ops->event_commit(&ctx);
1039 }
1040 end:
1041 return ret;
1042 }
1043
1044 int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel *consumer_chan)
1045 {
1046 struct channel *chan;
1047 int ret;
1048
1049 chan = consumer_chan->chan->chan;
1050 ret = ring_buffer_channel_close_wait_fd(&chan->backend.config,
1051 chan, chan->handle);
1052 if (!ret)
1053 consumer_chan->wait_fd = -1;
1054 return ret;
1055 }
1056
1057 int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel *consumer_chan)
1058 {
1059 struct channel *chan;
1060 int ret;
1061
1062 chan = consumer_chan->chan->chan;
1063 ret = ring_buffer_channel_close_wakeup_fd(&chan->backend.config,
1064 chan, chan->handle);
1065 if (!ret)
1066 consumer_chan->wakeup_fd = -1;
1067 return ret;
1068 }
1069
1070 int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream *stream)
1071 {
1072 struct channel *chan;
1073
1074 chan = stream->chan->chan->chan;
1075 return ring_buffer_stream_close_wait_fd(&chan->backend.config,
1076 chan, stream->handle, stream->cpu);
1077 }
1078
1079 int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream *stream)
1080 {
1081 struct channel *chan;
1082
1083 chan = stream->chan->chan->chan;
1084 return ring_buffer_stream_close_wakeup_fd(&chan->backend.config,
1085 chan, stream->handle, stream->cpu);
1086 }
1087
1088 struct ustctl_consumer_stream *
1089 ustctl_create_stream(struct ustctl_consumer_channel *channel,
1090 int cpu)
1091 {
1092 struct ustctl_consumer_stream *stream;
1093 struct lttng_ust_shm_handle *handle;
1094 struct channel *chan;
1095 int shm_fd, wait_fd, wakeup_fd;
1096 uint64_t memory_map_size;
1097 struct lttng_ust_lib_ring_buffer *buf;
1098 int ret;
1099
1100 if (!channel)
1101 return NULL;
1102 handle = channel->chan->handle;
1103 if (!handle)
1104 return NULL;
1105
1106 chan = channel->chan->chan;
1107 buf = channel_get_ring_buffer(&chan->backend.config,
1108 chan, cpu, handle, &shm_fd, &wait_fd,
1109 &wakeup_fd, &memory_map_size);
1110 if (!buf)
1111 return NULL;
1112 ret = lib_ring_buffer_open_read(buf, handle);
1113 if (ret)
1114 return NULL;
1115
1116 stream = zmalloc(sizeof(*stream));
1117 if (!stream)
1118 goto alloc_error;
1119 stream->handle = handle;
1120 stream->buf = buf;
1121 stream->chan = channel;
1122 stream->shm_fd = shm_fd;
1123 stream->wait_fd = wait_fd;
1124 stream->wakeup_fd = wakeup_fd;
1125 stream->memory_map_size = memory_map_size;
1126 stream->cpu = cpu;
1127 return stream;
1128
1129 alloc_error:
1130 return NULL;
1131 }
1132
1133 void ustctl_destroy_stream(struct ustctl_consumer_stream *stream)
1134 {
1135 struct lttng_ust_lib_ring_buffer *buf;
1136 struct ustctl_consumer_channel *consumer_chan;
1137
1138 assert(stream);
1139 buf = stream->buf;
1140 consumer_chan = stream->chan;
1141 lib_ring_buffer_release_read(buf, consumer_chan->chan->handle);
1142 free(stream);
1143 }
1144
1145 int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel *chan)
1146 {
1147 if (!chan)
1148 return -EINVAL;
1149 return shm_get_wait_fd(chan->chan->handle,
1150 &chan->chan->handle->chan._ref);
1151 }
1152
1153 int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel *chan)
1154 {
1155 if (!chan)
1156 return -EINVAL;
1157 return shm_get_wakeup_fd(chan->chan->handle,
1158 &chan->chan->handle->chan._ref);
1159 }
1160
1161 int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream *stream)
1162 {
1163 struct lttng_ust_lib_ring_buffer *buf;
1164 struct ustctl_consumer_channel *consumer_chan;
1165
1166 if (!stream)
1167 return -EINVAL;
1168 buf = stream->buf;
1169 consumer_chan = stream->chan;
1170 return shm_get_wait_fd(consumer_chan->chan->handle, &buf->self._ref);
1171 }
1172
1173 int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream *stream)
1174 {
1175 struct lttng_ust_lib_ring_buffer *buf;
1176 struct ustctl_consumer_channel *consumer_chan;
1177
1178 if (!stream)
1179 return -EINVAL;
1180 buf = stream->buf;
1181 consumer_chan = stream->chan;
1182 return shm_get_wakeup_fd(consumer_chan->chan->handle, &buf->self._ref);
1183 }
1184
1185 /* For mmap mode, readable without "get" operation */
1186
1187 void *ustctl_get_mmap_base(struct ustctl_consumer_stream *stream)
1188 {
1189 struct lttng_ust_lib_ring_buffer *buf;
1190 struct ustctl_consumer_channel *consumer_chan;
1191
1192 if (!stream)
1193 return NULL;
1194 buf = stream->buf;
1195 consumer_chan = stream->chan;
1196 return shmp(consumer_chan->chan->handle, buf->backend.memory_map);
1197 }
1198
1199 /* returns the length to mmap. */
1200 int ustctl_get_mmap_len(struct ustctl_consumer_stream *stream,
1201 unsigned long *len)
1202 {
1203 struct ustctl_consumer_channel *consumer_chan;
1204 unsigned long mmap_buf_len;
1205 struct channel *chan;
1206
1207 if (!stream)
1208 return -EINVAL;
1209 consumer_chan = stream->chan;
1210 chan = consumer_chan->chan->chan;
1211 if (chan->backend.config.output != RING_BUFFER_MMAP)
1212 return -EINVAL;
1213 mmap_buf_len = chan->backend.buf_size;
1214 if (chan->backend.extra_reader_sb)
1215 mmap_buf_len += chan->backend.subbuf_size;
1216 if (mmap_buf_len > INT_MAX)
1217 return -EFBIG;
1218 *len = mmap_buf_len;
1219 return 0;
1220 }
1221
1222 /* returns the maximum size for sub-buffers. */
1223 int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream *stream,
1224 unsigned long *len)
1225 {
1226 struct ustctl_consumer_channel *consumer_chan;
1227 struct channel *chan;
1228
1229 if (!stream)
1230 return -EINVAL;
1231 consumer_chan = stream->chan;
1232 chan = consumer_chan->chan->chan;
1233 *len = chan->backend.subbuf_size;
1234 return 0;
1235 }
1236
1237 /*
1238 * For mmap mode, operate on the current packet (between get/put or
1239 * get_next/put_next).
1240 */
1241
1242 /* returns the offset of the subbuffer belonging to the mmap reader. */
1243 int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream *stream,
1244 unsigned long *off)
1245 {
1246 struct channel *chan;
1247 unsigned long sb_bindex;
1248 struct lttng_ust_lib_ring_buffer *buf;
1249 struct ustctl_consumer_channel *consumer_chan;
1250
1251 if (!stream)
1252 return -EINVAL;
1253 buf = stream->buf;
1254 consumer_chan = stream->chan;
1255 chan = consumer_chan->chan->chan;
1256 if (chan->backend.config.output != RING_BUFFER_MMAP)
1257 return -EINVAL;
1258 sb_bindex = subbuffer_id_get_index(&chan->backend.config,
1259 buf->backend.buf_rsb.id);
1260 *off = shmp(consumer_chan->chan->handle,
1261 shmp_index(consumer_chan->chan->handle, buf->backend.array, sb_bindex)->shmp)->mmap_offset;
1262 return 0;
1263 }
1264
1265 /* returns the size of the current sub-buffer, without padding (for mmap). */
1266 int ustctl_get_subbuf_size(struct ustctl_consumer_stream *stream,
1267 unsigned long *len)
1268 {
1269 struct ustctl_consumer_channel *consumer_chan;
1270 struct channel *chan;
1271 struct lttng_ust_lib_ring_buffer *buf;
1272
1273 if (!stream)
1274 return -EINVAL;
1275
1276 buf = stream->buf;
1277 consumer_chan = stream->chan;
1278 chan = consumer_chan->chan->chan;
1279 *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf,
1280 consumer_chan->chan->handle);
1281 return 0;
1282 }
1283
1284 /* returns the size of the current sub-buffer, without padding (for mmap). */
1285 int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream *stream,
1286 unsigned long *len)
1287 {
1288 struct ustctl_consumer_channel *consumer_chan;
1289 struct channel *chan;
1290 struct lttng_ust_lib_ring_buffer *buf;
1291
1292 if (!stream)
1293 return -EINVAL;
1294 buf = stream->buf;
1295 consumer_chan = stream->chan;
1296 chan = consumer_chan->chan->chan;
1297 *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf,
1298 consumer_chan->chan->handle);
1299 *len = PAGE_ALIGN(*len);
1300 return 0;
1301 }
1302
1303 /* Get exclusive read access to the next sub-buffer that can be read. */
1304 int ustctl_get_next_subbuf(struct ustctl_consumer_stream *stream)
1305 {
1306 struct lttng_ust_lib_ring_buffer *buf;
1307 struct ustctl_consumer_channel *consumer_chan;
1308
1309 if (!stream)
1310 return -EINVAL;
1311 buf = stream->buf;
1312 consumer_chan = stream->chan;
1313 return lib_ring_buffer_get_next_subbuf(buf,
1314 consumer_chan->chan->handle);
1315 }
1316
1317
1318 /* Release exclusive sub-buffer access, move consumer forward. */
1319 int ustctl_put_next_subbuf(struct ustctl_consumer_stream *stream)
1320 {
1321 struct lttng_ust_lib_ring_buffer *buf;
1322 struct ustctl_consumer_channel *consumer_chan;
1323
1324 if (!stream)
1325 return -EINVAL;
1326 buf = stream->buf;
1327 consumer_chan = stream->chan;
1328 lib_ring_buffer_put_next_subbuf(buf, consumer_chan->chan->handle);
1329 return 0;
1330 }
1331
1332 /* snapshot */
1333
1334 /* Get a snapshot of the current ring buffer producer and consumer positions */
1335 int ustctl_snapshot(struct ustctl_consumer_stream *stream)
1336 {
1337 struct lttng_ust_lib_ring_buffer *buf;
1338 struct ustctl_consumer_channel *consumer_chan;
1339
1340 if (!stream)
1341 return -EINVAL;
1342 buf = stream->buf;
1343 consumer_chan = stream->chan;
1344 return lib_ring_buffer_snapshot(buf, &buf->cons_snapshot,
1345 &buf->prod_snapshot, consumer_chan->chan->handle);
1346 }
1347
1348 /* Get the consumer position (iteration start) */
1349 int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream *stream,
1350 unsigned long *pos)
1351 {
1352 struct lttng_ust_lib_ring_buffer *buf;
1353
1354 if (!stream)
1355 return -EINVAL;
1356 buf = stream->buf;
1357 *pos = buf->cons_snapshot;
1358 return 0;
1359 }
1360
1361 /* Get the producer position (iteration end) */
1362 int ustctl_snapshot_get_produced(struct ustctl_consumer_stream *stream,
1363 unsigned long *pos)
1364 {
1365 struct lttng_ust_lib_ring_buffer *buf;
1366
1367 if (!stream)
1368 return -EINVAL;
1369 buf = stream->buf;
1370 *pos = buf->prod_snapshot;
1371 return 0;
1372 }
1373
1374 /* Get exclusive read access to the specified sub-buffer position */
1375 int ustctl_get_subbuf(struct ustctl_consumer_stream *stream,
1376 unsigned long *pos)
1377 {
1378 struct lttng_ust_lib_ring_buffer *buf;
1379 struct ustctl_consumer_channel *consumer_chan;
1380
1381 if (!stream)
1382 return -EINVAL;
1383 buf = stream->buf;
1384 consumer_chan = stream->chan;
1385 return lib_ring_buffer_get_subbuf(buf, *pos,
1386 consumer_chan->chan->handle);
1387 }
1388
1389 /* Release exclusive sub-buffer access */
1390 int ustctl_put_subbuf(struct ustctl_consumer_stream *stream)
1391 {
1392 struct lttng_ust_lib_ring_buffer *buf;
1393 struct ustctl_consumer_channel *consumer_chan;
1394
1395 if (!stream)
1396 return -EINVAL;
1397 buf = stream->buf;
1398 consumer_chan = stream->chan;
1399 lib_ring_buffer_put_subbuf(buf, consumer_chan->chan->handle);
1400 return 0;
1401 }
1402
1403 void ustctl_flush_buffer(struct ustctl_consumer_stream *stream,
1404 int producer_active)
1405 {
1406 struct lttng_ust_lib_ring_buffer *buf;
1407 struct ustctl_consumer_channel *consumer_chan;
1408
1409 assert(stream);
1410 buf = stream->buf;
1411 consumer_chan = stream->chan;
1412 lib_ring_buffer_switch_slow(buf,
1413 producer_active ? SWITCH_ACTIVE : SWITCH_FLUSH,
1414 consumer_chan->chan->handle);
1415 }
1416
1417 /*
1418 * Returns 0 on success, negative error value on error.
1419 */
1420 int ustctl_recv_reg_msg(int sock,
1421 enum ustctl_socket_type *type,
1422 uint32_t *major,
1423 uint32_t *minor,
1424 uint32_t *pid,
1425 uint32_t *ppid,
1426 uint32_t *uid,
1427 uint32_t *gid,
1428 uint32_t *bits_per_long,
1429 uint32_t *uint8_t_alignment,
1430 uint32_t *uint16_t_alignment,
1431 uint32_t *uint32_t_alignment,
1432 uint32_t *uint64_t_alignment,
1433 uint32_t *long_alignment,
1434 int *byte_order,
1435 char *name)
1436 {
1437 ssize_t len;
1438 struct ustctl_reg_msg reg_msg;
1439
1440 len = ustcomm_recv_unix_sock(sock, &reg_msg, sizeof(reg_msg));
1441 if (len > 0 && len != sizeof(reg_msg))
1442 return -EIO;
1443 if (len == 0)
1444 return -EPIPE;
1445 if (len < 0)
1446 return len;
1447
1448 if (reg_msg.magic == LTTNG_UST_COMM_MAGIC) {
1449 *byte_order = BYTE_ORDER == BIG_ENDIAN ?
1450 BIG_ENDIAN : LITTLE_ENDIAN;
1451 } else if (reg_msg.magic == bswap_32(LTTNG_UST_COMM_MAGIC)) {
1452 *byte_order = BYTE_ORDER == BIG_ENDIAN ?
1453 LITTLE_ENDIAN : BIG_ENDIAN;
1454 } else {
1455 return -LTTNG_UST_ERR_INVAL_MAGIC;
1456 }
1457 switch (reg_msg.socket_type) {
1458 case 0: *type = USTCTL_SOCKET_CMD;
1459 break;
1460 case 1: *type = USTCTL_SOCKET_NOTIFY;
1461 break;
1462 default:
1463 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE;
1464 }
1465 *major = reg_msg.major;
1466 *minor = reg_msg.minor;
1467 *pid = reg_msg.pid;
1468 *ppid = reg_msg.ppid;
1469 *uid = reg_msg.uid;
1470 *gid = reg_msg.gid;
1471 *bits_per_long = reg_msg.bits_per_long;
1472 *uint8_t_alignment = reg_msg.uint8_t_alignment;
1473 *uint16_t_alignment = reg_msg.uint16_t_alignment;
1474 *uint32_t_alignment = reg_msg.uint32_t_alignment;
1475 *uint64_t_alignment = reg_msg.uint64_t_alignment;
1476 *long_alignment = reg_msg.long_alignment;
1477 memcpy(name, reg_msg.name, LTTNG_UST_ABI_PROCNAME_LEN);
1478 if (reg_msg.major != LTTNG_UST_ABI_MAJOR_VERSION) {
1479 return -LTTNG_UST_ERR_UNSUP_MAJOR;
1480 }
1481
1482 return 0;
1483 }
1484
1485 int ustctl_recv_notify(int sock, enum ustctl_notify_cmd *notify_cmd)
1486 {
1487 struct ustcomm_notify_hdr header;
1488 ssize_t len;
1489
1490 len = ustcomm_recv_unix_sock(sock, &header, sizeof(header));
1491 if (len > 0 && len != sizeof(header))
1492 return -EIO;
1493 if (len == 0)
1494 return -EPIPE;
1495 if (len < 0)
1496 return len;
1497 switch (header.notify_cmd) {
1498 case 0:
1499 *notify_cmd = USTCTL_NOTIFY_CMD_EVENT;
1500 break;
1501 case 1:
1502 *notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL;
1503 break;
1504 default:
1505 return -EINVAL;
1506 }
1507 return 0;
1508 }
1509
1510 /*
1511 * Returns 0 on success, negative error value on error.
1512 */
1513 int ustctl_recv_register_event(int sock,
1514 int *session_objd,
1515 int *channel_objd,
1516 char *event_name,
1517 int *loglevel,
1518 char **signature,
1519 size_t *nr_fields,
1520 struct ustctl_field **fields,
1521 char **model_emf_uri)
1522 {
1523 ssize_t len;
1524 struct ustcomm_notify_event_msg msg;
1525 size_t signature_len, fields_len, model_emf_uri_len;
1526 char *a_sign = NULL, *a_model_emf_uri = NULL;
1527 struct ustctl_field *a_fields = NULL;
1528
1529 len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg));
1530 if (len > 0 && len != sizeof(msg))
1531 return -EIO;
1532 if (len == 0)
1533 return -EPIPE;
1534 if (len < 0)
1535 return len;
1536
1537 *session_objd = msg.session_objd;
1538 *channel_objd = msg.channel_objd;
1539 strncpy(event_name, msg.event_name, LTTNG_UST_SYM_NAME_LEN);
1540 event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1541 *loglevel = msg.loglevel;
1542 signature_len = msg.signature_len;
1543 fields_len = msg.fields_len;
1544
1545 if (fields_len % sizeof(*a_fields) != 0) {
1546 return -EINVAL;
1547 }
1548
1549 model_emf_uri_len = msg.model_emf_uri_len;
1550
1551 /* recv signature. contains at least \0. */
1552 a_sign = zmalloc(signature_len);
1553 if (!a_sign)
1554 return -ENOMEM;
1555 len = ustcomm_recv_unix_sock(sock, a_sign, signature_len);
1556 if (len > 0 && len != signature_len) {
1557 len = -EIO;
1558 goto signature_error;
1559 }
1560 if (len == 0) {
1561 len = -EPIPE;
1562 goto signature_error;
1563 }
1564 if (len < 0) {
1565 goto signature_error;
1566 }
1567 /* Enforce end of string */
1568 signature[signature_len - 1] = '\0';
1569
1570 /* recv fields */
1571 if (fields_len) {
1572 a_fields = zmalloc(fields_len);
1573 if (!a_fields) {
1574 len = -ENOMEM;
1575 goto signature_error;
1576 }
1577 len = ustcomm_recv_unix_sock(sock, a_fields, fields_len);
1578 if (len > 0 && len != fields_len) {
1579 len = -EIO;
1580 goto fields_error;
1581 }
1582 if (len == 0) {
1583 len = -EPIPE;
1584 goto fields_error;
1585 }
1586 if (len < 0) {
1587 goto fields_error;
1588 }
1589 }
1590
1591 if (model_emf_uri_len) {
1592 /* recv model_emf_uri_len */
1593 a_model_emf_uri = zmalloc(model_emf_uri_len);
1594 if (!a_model_emf_uri) {
1595 len = -ENOMEM;
1596 goto fields_error;
1597 }
1598 len = ustcomm_recv_unix_sock(sock, a_model_emf_uri,
1599 model_emf_uri_len);
1600 if (len > 0 && len != model_emf_uri_len) {
1601 len = -EIO;
1602 goto model_error;
1603 }
1604 if (len == 0) {
1605 len = -EPIPE;
1606 goto model_error;
1607 }
1608 if (len < 0) {
1609 goto model_error;
1610 }
1611 /* Enforce end of string */
1612 a_model_emf_uri[model_emf_uri_len - 1] = '\0';
1613 }
1614
1615 *signature = a_sign;
1616 *nr_fields = fields_len / sizeof(*a_fields);
1617 *fields = a_fields;
1618 *model_emf_uri = a_model_emf_uri;
1619
1620 return 0;
1621
1622 model_error:
1623 free(a_model_emf_uri);
1624 fields_error:
1625 free(a_fields);
1626 signature_error:
1627 free(a_sign);
1628 return len;
1629 }
1630
1631 /*
1632 * Returns 0 on success, negative error value on error.
1633 */
1634 int ustctl_reply_register_event(int sock,
1635 uint32_t id,
1636 int ret_code)
1637 {
1638 ssize_t len;
1639 struct {
1640 struct ustcomm_notify_hdr header;
1641 struct ustcomm_notify_event_reply r;
1642 } reply;
1643
1644 memset(&reply, 0, sizeof(reply));
1645 reply.header.notify_cmd = USTCTL_NOTIFY_CMD_EVENT;
1646 reply.r.ret_code = ret_code;
1647 reply.r.event_id = id;
1648 len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply));
1649 if (len > 0 && len != sizeof(reply))
1650 return -EIO;
1651 if (len < 0)
1652 return len;
1653 return 0;
1654 }
1655
1656 /*
1657 * Returns 0 on success, negative UST or system error value on error.
1658 */
1659 int ustctl_recv_register_channel(int sock,
1660 int *session_objd, /* session descriptor (output) */
1661 int *channel_objd, /* channel descriptor (output) */
1662 size_t *nr_fields,
1663 struct ustctl_field **fields)
1664 {
1665 ssize_t len;
1666 struct ustcomm_notify_channel_msg msg;
1667 size_t fields_len;
1668 struct ustctl_field *a_fields;
1669
1670 len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg));
1671 if (len > 0 && len != sizeof(msg))
1672 return -EIO;
1673 if (len == 0)
1674 return -EPIPE;
1675 if (len < 0)
1676 return len;
1677
1678 *session_objd = msg.session_objd;
1679 *channel_objd = msg.channel_objd;
1680 fields_len = msg.ctx_fields_len;
1681
1682 if (fields_len % sizeof(*a_fields) != 0) {
1683 return -EINVAL;
1684 }
1685
1686 /* recv fields */
1687 if (fields_len) {
1688 a_fields = zmalloc(fields_len);
1689 if (!a_fields) {
1690 len = -ENOMEM;
1691 goto alloc_error;
1692 }
1693 len = ustcomm_recv_unix_sock(sock, a_fields, fields_len);
1694 if (len > 0 && len != fields_len) {
1695 len = -EIO;
1696 goto fields_error;
1697 }
1698 if (len == 0) {
1699 len = -EPIPE;
1700 goto fields_error;
1701 }
1702 if (len < 0) {
1703 goto fields_error;
1704 }
1705 *fields = a_fields;
1706 } else {
1707 *fields = NULL;
1708 }
1709 *nr_fields = fields_len / sizeof(*a_fields);
1710 return 0;
1711
1712 fields_error:
1713 free(a_fields);
1714 alloc_error:
1715 return len;
1716 }
1717
1718 /*
1719 * Returns 0 on success, negative error value on error.
1720 */
1721 int ustctl_reply_register_channel(int sock,
1722 uint32_t chan_id,
1723 enum ustctl_channel_header header_type,
1724 int ret_code)
1725 {
1726 ssize_t len;
1727 struct {
1728 struct ustcomm_notify_hdr header;
1729 struct ustcomm_notify_channel_reply r;
1730 } reply;
1731
1732 memset(&reply, 0, sizeof(reply));
1733 reply.header.notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL;
1734 reply.r.ret_code = ret_code;
1735 reply.r.chan_id = chan_id;
1736 switch (header_type) {
1737 case USTCTL_CHANNEL_HEADER_COMPACT:
1738 reply.r.header_type = 1;
1739 break;
1740 case USTCTL_CHANNEL_HEADER_LARGE:
1741 reply.r.header_type = 2;
1742 break;
1743 default:
1744 reply.r.header_type = 0;
1745 break;
1746 }
1747 len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply));
1748 if (len > 0 && len != sizeof(reply))
1749 return -EIO;
1750 if (len < 0)
1751 return len;
1752 return 0;
1753 }
1754
1755 static __attribute__((constructor))
1756 void ustctl_init(void)
1757 {
1758 init_usterr();
1759 lttng_ring_buffer_metadata_client_init();
1760 lttng_ring_buffer_client_overwrite_init();
1761 lttng_ring_buffer_client_discard_init();
1762 lib_ringbuffer_signal_init();
1763 }
1764
1765 static __attribute__((destructor))
1766 void ustctl_exit(void)
1767 {
1768 lttng_ring_buffer_client_discard_exit();
1769 lttng_ring_buffer_client_overwrite_exit();
1770 lttng_ring_buffer_metadata_client_exit();
1771 }
This page took 0.063417 seconds and 5 git commands to generate.