Add channel wakeup fd to monitor close
[lttng-ust.git] / liblttng-ust-ctl / ustctl.c
CommitLineData
57773204
MD
1/*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
74d81a6c 3 * Copyright (C) 2011-2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
57773204 4 *
e92f3e28
MD
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.
57773204
MD
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 *
e92f3e28
MD
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.
57773204
MD
17 */
18
9d335227 19#define _GNU_SOURCE
57773204 20#include <string.h>
4318ae1b
MD
21#include <lttng/ust-ctl.h>
22#include <lttng/ust-abi.h>
c1fca457 23#include <lttng/ust-events.h>
7a784989 24#include <sys/mman.h>
32ce8569 25#include <byteswap.h>
44c72f10
MD
26
27#include <usterr-signal-safe.h>
b728d87e 28#include <ust-comm.h>
74d81a6c 29#include <helper.h>
57773204
MD
30
31#include "../libringbuffer/backend.h"
32#include "../libringbuffer/frontend.h"
c9023c93
MD
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
57773204 40
74d81a6c
MD
41/*
42 * Channel representation within consumer.
43 */
44struct ustctl_consumer_channel {
45 struct lttng_channel *chan; /* lttng channel buffers */
6b120308 46
74d81a6c
MD
47 /* initial attributes */
48 struct ustctl_consumer_channel_attr attr;
ff0f5728
MD
49 int wait_fd; /* monitor close() */
50 int wakeup_fd; /* monitor close() */
74d81a6c
MD
51};
52
53/*
54 * Stream representation within consumer.
55 */
56struct 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
65extern void lttng_ring_buffer_client_overwrite_init(void);
66extern void lttng_ring_buffer_client_discard_init(void);
67extern void lttng_ring_buffer_metadata_client_init(void);
68extern void lttng_ring_buffer_client_overwrite_exit(void);
69extern void lttng_ring_buffer_client_discard_exit(void);
70extern void lttng_ring_buffer_metadata_client_exit(void);
71
72volatile enum ust_loglevel ust_loglevel;
57773204 73
2be0e72c
MD
74int ustctl_release_handle(int sock, int handle)
75{
76 struct ustcomm_ust_msg lum;
77 struct ustcomm_ust_reply lur;
2be0e72c 78
74d81a6c
MD
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);
2be0e72c 85}
74d81a6c 86
12388166
MD
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 */
d26228ae 91int ustctl_release_object(int sock, struct lttng_ust_object_data *data)
57773204 92{
57773204
MD
93 int ret;
94
9bfc503d
MD
95 if (!data)
96 return -EINVAL;
97
74d81a6c
MD
98 switch (data->type) {
99 case LTTNG_UST_OBJECT_TYPE_CHANNEL:
ff0f5728
MD
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 }
74d81a6c
MD
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 }
d26228ae 116 }
74d81a6c
MD
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 }
d26228ae 123 }
74d81a6c 124 break;
32ce8569
MD
125 case LTTNG_UST_OBJECT_TYPE_EVENT:
126 case LTTNG_UST_OBJECT_TYPE_CONTEXT:
127 break;
74d81a6c
MD
128 default:
129 assert(0);
d26228ae 130 }
2be0e72c 131 return ustctl_release_handle(sock, data->handle);
57773204
MD
132}
133
1c5e467e
MD
134/*
135 * Send registration done packet to the application.
136 */
137int 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;
1c5e467e 150 return 0;
1c5e467e
MD
151}
152
57773204
MD
153/*
154 * returns session handle.
155 */
156int 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
57773204 174int ustctl_create_event(int sock, struct lttng_ust_event *ev,
61f02aea
MD
175 struct lttng_ust_object_data *channel_data,
176 struct lttng_ust_object_data **_event_data)
57773204
MD
177{
178 struct ustcomm_ust_msg lum;
179 struct ustcomm_ust_reply lur;
61f02aea 180 struct lttng_ust_object_data *event_data;
57773204
MD
181 int ret;
182
9bfc503d
MD
183 if (!channel_data || !_event_data)
184 return -EINVAL;
185
74d81a6c 186 event_data = zmalloc(sizeof(*event_data));
57773204
MD
187 if (!event_data)
188 return -ENOMEM;
32ce8569 189 event_data->type = LTTNG_UST_OBJECT_TYPE_EVENT;
57773204
MD
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;
457a6b58
MD
196 lum.u.event.loglevel_type = ev->loglevel_type;
197 lum.u.event.loglevel = ev->loglevel;
57773204
MD
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
209int ustctl_add_context(int sock, struct lttng_ust_context *ctx,
61f02aea
MD
210 struct lttng_ust_object_data *obj_data,
211 struct lttng_ust_object_data **_context_data)
57773204
MD
212{
213 struct ustcomm_ust_msg lum;
214 struct ustcomm_ust_reply lur;
61f02aea 215 struct lttng_ust_object_data *context_data;
57773204
MD
216 int ret;
217
9bfc503d
MD
218 if (!obj_data || !_context_data)
219 return -EINVAL;
220
74d81a6c 221 context_data = zmalloc(sizeof(*context_data));
57773204
MD
222 if (!context_data)
223 return -ENOMEM;
32ce8569 224 context_data->type = LTTNG_UST_OBJECT_TYPE_CONTEXT;
57773204 225 memset(&lum, 0, sizeof(lum));
3039d8ed 226 lum.handle = obj_data->handle;
57773204
MD
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 }
32ce8569
MD
234 context_data->handle = -1;
235 DBG("Context created successfully");
57773204
MD
236 *_context_data = context_data;
237 return ret;
238}
239
cd54f6d9
MD
240int 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;
e695af51 255 lum.u.filter.seqnum = bytecode->seqnum;
cd54f6d9
MD
256
257 ret = ustcomm_send_app_msg(sock, &lum);
258 if (ret)
259 return ret;
cd54f6d9
MD
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 }
7bc53e94
MD
266 if (ret != bytecode->len)
267 return -EINVAL;
268 return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
cd54f6d9
MD
269}
270
57773204 271/* Enable event, channel and session ioctl */
61f02aea 272int ustctl_enable(int sock, struct lttng_ust_object_data *object)
57773204
MD
273{
274 struct ustcomm_ust_msg lum;
275 struct ustcomm_ust_reply lur;
276 int ret;
277
9bfc503d
MD
278 if (!object)
279 return -EINVAL;
280
57773204
MD
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 */
61f02aea 292int ustctl_disable(int sock, struct lttng_ust_object_data *object)
57773204
MD
293{
294 struct ustcomm_ust_msg lum;
295 struct ustcomm_ust_reply lur;
296 int ret;
297
9bfc503d
MD
298 if (!object)
299 return -EINVAL;
300
57773204
MD
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
4a6ca058 311int ustctl_start_session(int sock, int handle)
57773204 312{
61f02aea 313 struct lttng_ust_object_data obj;
4a6ca058
MD
314
315 obj.handle = handle;
316 return ustctl_enable(sock, &obj);
57773204
MD
317}
318
4a6ca058 319int ustctl_stop_session(int sock, int handle)
57773204 320{
61f02aea 321 struct lttng_ust_object_data obj;
4a6ca058
MD
322
323 obj.handle = handle;
324 return ustctl_disable(sock, &obj);
57773204
MD
325}
326
57773204
MD
327int ustctl_tracepoint_list(int sock)
328{
b115631f
MD
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
344int ustctl_tracepoint_list_get(int sock, int tp_list_handle,
cbef6901 345 struct lttng_ust_tracepoint_iter *iter)
b115631f
MD
346{
347 struct ustcomm_ust_msg lum;
348 struct ustcomm_ust_reply lur;
349 int ret;
350
9bfc503d
MD
351 if (!iter)
352 return -EINVAL;
353
b115631f
MD
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;
882a56d7 360 DBG("received tracepoint list entry name %s loglevel %d",
cbef6901 361 lur.u.tracepoint.name,
882a56d7 362 lur.u.tracepoint.loglevel);
cbef6901 363 memcpy(iter, &lur.u.tracepoint, sizeof(*iter));
b115631f 364 return 0;
57773204
MD
365}
366
40003310
MD
367int 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
384int 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
57773204
MD
413int 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
9bfc503d
MD
419 if (!v)
420 return -EINVAL;
421
57773204
MD
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
433int 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
449int ustctl_calibrate(int sock, struct lttng_ust_calibrate *calibrate)
450{
9bfc503d
MD
451 if (!calibrate)
452 return -EINVAL;
453
57773204
MD
454 return -ENOSYS;
455}
456
f1fffc57
MD
457int 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
9bfc503d
MD
463 if (!object)
464 return -EINVAL;
465
f1fffc57
MD
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
74d81a6c
MD
476static
477int ustctl_send_channel(int sock,
478 enum lttng_ust_chan_type type,
479 void *data,
480 uint64_t size,
ff0f5728 481 int wakeup_fd,
74d81a6c
MD
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 }
57773204 514
ff0f5728
MD
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 }
74d81a6c
MD
523 return 0;
524}
525
526static
527int 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)
57773204 532{
74d81a6c
MD
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
585int 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;
ff0f5728 590 int wakeup_fd;
7a784989 591 int ret;
57773204 592
74d81a6c
MD
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
600 /* recv mmap size */
601 len = ustcomm_recv_unix_sock(sock, &channel_data->size,
602 sizeof(channel_data->size));
603 if (len != sizeof(channel_data->size)) {
604 if (len < 0)
605 ret = len;
606 else
607 ret = -EINVAL;
608 goto error;
609 }
9bfc503d 610
74d81a6c
MD
611 /* recv channel type */
612 len = ustcomm_recv_unix_sock(sock, &channel_data->u.channel.type,
613 sizeof(channel_data->u.channel.type));
614 if (len != sizeof(channel_data->u.channel.type)) {
615 if (len < 0)
616 ret = len;
617 else
618 ret = -EINVAL;
619 goto error;
620 }
621
622 /* recv channel data */
623 channel_data->u.channel.data = zmalloc(channel_data->size);
624 if (!channel_data->u.channel.data) {
625 ret = -ENOMEM;
626 goto error;
627 }
628 len = ustcomm_recv_unix_sock(sock, channel_data->u.channel.data,
629 channel_data->size);
630 if (len != channel_data->size) {
631 if (len < 0)
632 ret = len;
633 else
634 ret = -EINVAL;
635 goto error_recv_data;
636 }
ff0f5728
MD
637 /* recv wakeup fd */
638 len = ustcomm_recv_fds_unix_sock(sock, &wakeup_fd, 1);
639 if (len <= 0) {
640 if (len < 0) {
641 ret = len;
642 goto error_recv_data;
643 } else {
644 ret = -EIO;
645 goto error_recv_data;
646 }
647 }
648 channel_data->u.channel.wakeup_fd = wakeup_fd;
74d81a6c
MD
649 *_channel_data = channel_data;
650 return 0;
651
652error_recv_data:
653 free(channel_data->u.channel.data);
654error:
655 free(channel_data);
656error_alloc:
657 return ret;
658}
659
660int ustctl_recv_stream_from_consumer(int sock,
661 struct lttng_ust_object_data **_stream_data)
662{
663 struct lttng_ust_object_data *stream_data;
664 ssize_t len;
665 int ret;
666 int fds[2];
667
668 stream_data = zmalloc(sizeof(*stream_data));
669 if (!stream_data) {
670 ret = -ENOMEM;
671 goto error_alloc;
57773204 672 }
74d81a6c
MD
673
674 stream_data->type = LTTNG_UST_OBJECT_TYPE_STREAM;
675 stream_data->handle = -1;
676
677 /* recv mmap size */
678 len = ustcomm_recv_unix_sock(sock, &stream_data->size,
679 sizeof(stream_data->size));
680 if (len != sizeof(stream_data->size)) {
681 if (len < 0)
682 ret = len;
683 else
684 ret = -EINVAL;
685 goto error;
686 }
687 if (stream_data->size == -1) {
688 ret = -LTTNG_UST_ERR_NOENT;
689 goto error;
690 }
691
692 /* recv stream nr */
693 len = ustcomm_recv_unix_sock(sock, &stream_data->u.stream.stream_nr,
694 sizeof(stream_data->u.stream.stream_nr));
695 if (len != sizeof(stream_data->u.stream.stream_nr)) {
696 if (len < 0)
697 ret = len;
698 else
699 ret = -EINVAL;
700 goto error;
701 }
702
703 /* recv shm fd and wakeup fd */
704 len = ustcomm_recv_fds_unix_sock(sock, fds, 2);
705 if (len <= 0) {
706 if (len < 0) {
707 ret = len;
708 goto error;
709 } else {
710 ret = -EIO;
711 goto error;
0bfe09ec 712 }
0bfe09ec 713 }
74d81a6c
MD
714 stream_data->u.stream.shm_fd = fds[0];
715 stream_data->u.stream.wakeup_fd = fds[1];
716 *_stream_data = stream_data;
717 return 0;
0bfe09ec 718
74d81a6c
MD
719error:
720 free(stream_data);
721error_alloc:
722 return ret;
723}
724
725int ustctl_send_channel_to_ust(int sock, int session_handle,
726 struct lttng_ust_object_data *channel_data)
727{
728 struct ustcomm_ust_msg lum;
729 struct ustcomm_ust_reply lur;
730 int ret;
731
732 if (!channel_data)
733 return -EINVAL;
734
735 memset(&lum, 0, sizeof(lum));
736 lum.handle = session_handle;
737 lum.cmd = LTTNG_UST_CHANNEL;
738 lum.u.channel.len = channel_data->size;
739 lum.u.channel.type = channel_data->u.channel.type;
740 ret = ustcomm_send_app_msg(sock, &lum);
741 if (ret)
742 return ret;
743
744 ret = ustctl_send_channel(sock,
745 channel_data->u.channel.type,
746 channel_data->u.channel.data,
747 channel_data->size,
ff0f5728 748 channel_data->u.channel.wakeup_fd,
74d81a6c
MD
749 1);
750 if (ret)
751 return ret;
752 ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
753 if (!ret) {
754 if (lur.ret_val >= 0) {
755 channel_data->handle = lur.ret_val;
756 }
57773204 757 }
74d81a6c
MD
758 return ret;
759}
760
761int ustctl_send_stream_to_ust(int sock,
762 struct lttng_ust_object_data *channel_data,
763 struct lttng_ust_object_data *stream_data)
764{
765 struct ustcomm_ust_msg lum;
766 struct ustcomm_ust_reply lur;
767 int ret;
768
769 memset(&lum, 0, sizeof(lum));
770 lum.handle = channel_data->handle;
771 lum.cmd = LTTNG_UST_STREAM;
772 lum.u.stream.len = stream_data->size;
773 lum.u.stream.stream_nr = stream_data->u.stream.stream_nr;
774 ret = ustcomm_send_app_msg(sock, &lum);
775 if (ret)
776 return ret;
777
778 assert(stream_data);
779 assert(stream_data->type == LTTNG_UST_OBJECT_TYPE_STREAM);
780
781 ret = ustctl_send_stream(sock,
782 stream_data->u.stream.stream_nr,
783 stream_data->size,
784 stream_data->u.stream.shm_fd,
785 stream_data->u.stream.wakeup_fd, 1);
786 if (ret)
787 return ret;
788 return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd);
789}
790
791
792/* Buffer operations */
793
794struct ustctl_consumer_channel *
795 ustctl_create_channel(struct ustctl_consumer_channel_attr *attr)
796{
797 struct ustctl_consumer_channel *chan;
798 const char *transport_name;
799 struct lttng_transport *transport;
800
801 switch (attr->type) {
802 case LTTNG_UST_CHAN_PER_CPU:
803 if (attr->output == LTTNG_UST_MMAP) {
804 transport_name = attr->overwrite ?
805 "relay-overwrite-mmap" : "relay-discard-mmap";
806 } else {
807 return NULL;
808 }
c1fca457 809 break;
74d81a6c
MD
810 case LTTNG_UST_CHAN_METADATA:
811 if (attr->output == LTTNG_UST_MMAP)
812 transport_name = "relay-metadata-mmap";
813 else
814 return NULL;
c1fca457
MD
815 break;
816 default:
74d81a6c 817 transport_name = "<unknown>";
c1fca457
MD
818 return NULL;
819 }
74d81a6c
MD
820
821 transport = lttng_transport_find(transport_name);
822 if (!transport) {
823 DBG("LTTng transport %s not found\n",
32ce8569 824 transport_name);
74d81a6c 825 return NULL;
7a784989 826 }
74d81a6c
MD
827
828 chan = zmalloc(sizeof(*chan));
829 if (!chan)
830 return NULL;
831
832 chan->chan = transport->ops.channel_create(transport_name, NULL,
32ce8569 833 attr->subbuf_size, attr->num_subbuf,
74d81a6c 834 attr->switch_timer_interval,
32ce8569 835 attr->read_timer_interval,
74d81a6c
MD
836 attr->uuid);
837 if (!chan->chan) {
838 goto chan_error;
839 }
840 chan->chan->ops = &transport->ops;
841 memcpy(&chan->attr, attr, sizeof(chan->attr));
842 return chan;
843
844chan_error:
845 free(chan);
846 return NULL;
57773204
MD
847}
848
74d81a6c 849void ustctl_destroy_channel(struct ustctl_consumer_channel *chan)
57773204 850{
74d81a6c
MD
851 chan->chan->ops->channel_destroy(chan->chan);
852 free(chan);
853}
854
855int ustctl_send_channel_to_sessiond(int sock,
856 struct ustctl_consumer_channel *channel)
857{
858 struct shm_object_table *table;
57773204 859
74d81a6c
MD
860 table = channel->chan->handle->table;
861 if (table->size <= 0)
9bfc503d 862 return -EINVAL;
74d81a6c
MD
863 return ustctl_send_channel(sock,
864 channel->attr.type,
865 table->objects[0].memory_map,
866 table->objects[0].memory_map_size,
ff0f5728 867 channel->wakeup_fd,
74d81a6c
MD
868 0);
869}
9bfc503d 870
74d81a6c
MD
871int ustctl_send_stream_to_sessiond(int sock,
872 struct ustctl_consumer_stream *stream)
873{
874 if (!stream)
875 return ustctl_send_stream(sock, -1U, -1U, -1, -1, 0);
876
877 return ustctl_send_stream(sock,
878 stream->cpu,
879 stream->memory_map_size,
880 stream->shm_fd, stream->wakeup_fd,
881 0);
57773204
MD
882}
883
c9023c93
MD
884int ustctl_write_metadata_to_channel(
885 struct ustctl_consumer_channel *channel,
886 const char *metadata_str, /* NOT null-terminated */
887 size_t len) /* metadata length */
888{
889 struct lttng_ust_lib_ring_buffer_ctx ctx;
890 struct lttng_channel *chan = channel->chan;
891 const char *str = metadata_str;
892 int ret = 0, waitret;
893 size_t reserve_len, pos;
894
895 for (pos = 0; pos < len; pos += reserve_len) {
896 reserve_len = min_t(size_t,
897 chan->ops->packet_avail_size(chan->chan, chan->handle),
898 len - pos);
899 lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len,
900 sizeof(char), -1, chan->handle);
901 /*
902 * We don't care about metadata buffer's records lost
903 * count, because we always retry here. Report error if
904 * we need to bail out after timeout or being
905 * interrupted.
906 */
907 waitret = wait_cond_interruptible_timeout(
908 ({
909 ret = chan->ops->event_reserve(&ctx, 0);
910 ret != -ENOBUFS || !ret;
911 }),
912 LTTNG_METADATA_TIMEOUT_MSEC);
913 if (waitret == -ETIMEDOUT || waitret == -EINTR || ret) {
914 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
915 waitret == -EINTR ? "interrupted" :
916 (ret == -ENOBUFS ? "timeout" : "I/O error"));
917 if (waitret == -EINTR)
918 ret = waitret;
919 goto end;
920 }
921 chan->ops->event_write(&ctx, &str[pos], reserve_len);
922 chan->ops->event_commit(&ctx);
923 }
924end:
925 return ret;
926}
927
ff0f5728
MD
928int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel *consumer_chan)
929{
930 struct channel *chan;
931
932 chan = consumer_chan->chan->chan;
933 return ring_buffer_channel_close_wait_fd(&chan->backend.config,
934 chan, chan->handle);
935}
936
937int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel *consumer_chan)
938{
939 struct channel *chan;
940
941 chan = consumer_chan->chan->chan;
942 return ring_buffer_channel_close_wakeup_fd(&chan->backend.config,
943 chan, chan->handle);
944}
945
74d81a6c 946int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream *stream)
5224b5c8
MD
947{
948 struct channel *chan;
949
74d81a6c 950 chan = stream->chan->chan->chan;
ff0f5728 951 return ring_buffer_stream_close_wait_fd(&chan->backend.config,
74d81a6c 952 chan, stream->handle, stream->cpu);
5224b5c8
MD
953}
954
74d81a6c 955int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream *stream)
6e922b24 956{
66bdd22a 957 struct channel *chan;
74d81a6c
MD
958
959 chan = stream->chan->chan->chan;
ff0f5728 960 return ring_buffer_stream_close_wakeup_fd(&chan->backend.config,
74d81a6c
MD
961 chan, stream->handle, stream->cpu);
962}
963
964struct ustctl_consumer_stream *
965 ustctl_create_stream(struct ustctl_consumer_channel *channel,
966 int cpu)
967{
968 struct ustctl_consumer_stream *stream;
969 struct lttng_ust_shm_handle *handle;
970 struct channel *chan;
971 int shm_fd, wait_fd, wakeup_fd;
972 uint64_t memory_map_size;
4cfec15c 973 struct lttng_ust_lib_ring_buffer *buf;
6e922b24
MD
974 int ret;
975
74d81a6c
MD
976 if (!channel)
977 return NULL;
978 handle = channel->chan->handle;
9bfc503d
MD
979 if (!handle)
980 return NULL;
981
74d81a6c 982 chan = channel->chan->chan;
6e922b24 983 buf = channel_get_ring_buffer(&chan->backend.config,
74d81a6c
MD
984 chan, cpu, handle, &shm_fd, &wait_fd,
985 &wakeup_fd, &memory_map_size);
6e922b24
MD
986 if (!buf)
987 return NULL;
74d81a6c 988 ret = lib_ring_buffer_open_read(buf, handle);
6e922b24
MD
989 if (ret)
990 return NULL;
74d81a6c
MD
991
992 stream = zmalloc(sizeof(*stream));
993 if (!stream)
994 goto alloc_error;
995 stream->handle = handle;
996 stream->buf = buf;
997 stream->chan = channel;
998 stream->shm_fd = shm_fd;
999 stream->wait_fd = wait_fd;
1000 stream->wakeup_fd = wakeup_fd;
1001 stream->memory_map_size = memory_map_size;
1002 stream->cpu = cpu;
1003 return stream;
1004
1005alloc_error:
1006 return NULL;
1007}
1008
1009void ustctl_destroy_stream(struct ustctl_consumer_stream *stream)
1010{
1011 struct lttng_ust_lib_ring_buffer *buf;
1012 struct ustctl_consumer_channel *consumer_chan;
1013
1014 assert(stream);
1015 buf = stream->buf;
1016 consumer_chan = stream->chan;
1017 lib_ring_buffer_release_read(buf, consumer_chan->chan->handle);
1018 free(stream);
6e922b24
MD
1019}
1020
ff0f5728
MD
1021int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel *chan)
1022{
1023 if (!chan)
1024 return -EINVAL;
1025 return shm_get_wait_fd(chan->chan->handle,
1026 &chan->chan->handle->chan._ref);
1027}
1028
1029int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel *chan)
1030{
1031 if (!chan)
1032 return -EINVAL;
1033 return shm_get_wakeup_fd(chan->chan->handle,
1034 &chan->chan->handle->chan._ref);
1035}
1036
1037int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream *stream)
6e922b24 1038{
74d81a6c
MD
1039 struct lttng_ust_lib_ring_buffer *buf;
1040 struct ustctl_consumer_channel *consumer_chan;
1041
1042 if (!stream)
1043 return -EINVAL;
1044 buf = stream->buf;
1045 consumer_chan = stream->chan;
1046 return shm_get_wait_fd(consumer_chan->chan->handle, &buf->self._ref);
1047}
1048
ff0f5728 1049int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream *stream)
74d81a6c
MD
1050{
1051 struct lttng_ust_lib_ring_buffer *buf;
1052 struct ustctl_consumer_channel *consumer_chan;
1053
1054 if (!stream)
1055 return -EINVAL;
1056 buf = stream->buf;
1057 consumer_chan = stream->chan;
1058 return shm_get_wakeup_fd(consumer_chan->chan->handle, &buf->self._ref);
6e922b24
MD
1059}
1060
57773204
MD
1061/* For mmap mode, readable without "get" operation */
1062
74d81a6c 1063void *ustctl_get_mmap_base(struct ustctl_consumer_stream *stream)
9095efe9 1064{
74d81a6c
MD
1065 struct lttng_ust_lib_ring_buffer *buf;
1066 struct ustctl_consumer_channel *consumer_chan;
1067
1068 if (!stream)
9bfc503d 1069 return NULL;
74d81a6c
MD
1070 buf = stream->buf;
1071 consumer_chan = stream->chan;
1072 return shmp(consumer_chan->chan->handle, buf->backend.memory_map);
9095efe9
MD
1073}
1074
57773204 1075/* returns the length to mmap. */
74d81a6c 1076int ustctl_get_mmap_len(struct ustctl_consumer_stream *stream,
57773204
MD
1077 unsigned long *len)
1078{
74d81a6c 1079 struct ustctl_consumer_channel *consumer_chan;
57773204 1080 unsigned long mmap_buf_len;
66bdd22a 1081 struct channel *chan;
57773204 1082
74d81a6c 1083 if (!stream)
9bfc503d 1084 return -EINVAL;
74d81a6c
MD
1085 consumer_chan = stream->chan;
1086 chan = consumer_chan->chan->chan;
57773204
MD
1087 if (chan->backend.config.output != RING_BUFFER_MMAP)
1088 return -EINVAL;
1089 mmap_buf_len = chan->backend.buf_size;
1090 if (chan->backend.extra_reader_sb)
1091 mmap_buf_len += chan->backend.subbuf_size;
1092 if (mmap_buf_len > INT_MAX)
1093 return -EFBIG;
1094 *len = mmap_buf_len;
1095 return 0;
1096}
1097
1098/* returns the maximum size for sub-buffers. */
74d81a6c 1099int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream *stream,
57773204
MD
1100 unsigned long *len)
1101{
74d81a6c 1102 struct ustctl_consumer_channel *consumer_chan;
66bdd22a 1103 struct channel *chan;
57773204 1104
74d81a6c 1105 if (!stream)
9bfc503d 1106 return -EINVAL;
74d81a6c
MD
1107 consumer_chan = stream->chan;
1108 chan = consumer_chan->chan->chan;
57773204
MD
1109 *len = chan->backend.subbuf_size;
1110 return 0;
1111}
1112
1113/*
1114 * For mmap mode, operate on the current packet (between get/put or
1115 * get_next/put_next).
1116 */
1117
1118/* returns the offset of the subbuffer belonging to the mmap reader. */
74d81a6c
MD
1119int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream *stream,
1120 unsigned long *off)
57773204 1121{
66bdd22a 1122 struct channel *chan;
57773204 1123 unsigned long sb_bindex;
74d81a6c
MD
1124 struct lttng_ust_lib_ring_buffer *buf;
1125 struct ustctl_consumer_channel *consumer_chan;
57773204 1126
74d81a6c 1127 if (!stream)
9bfc503d 1128 return -EINVAL;
74d81a6c
MD
1129 buf = stream->buf;
1130 consumer_chan = stream->chan;
1131 chan = consumer_chan->chan->chan;
57773204
MD
1132 if (chan->backend.config.output != RING_BUFFER_MMAP)
1133 return -EINVAL;
1134 sb_bindex = subbuffer_id_get_index(&chan->backend.config,
32ce8569 1135 buf->backend.buf_rsb.id);
74d81a6c
MD
1136 *off = shmp(consumer_chan->chan->handle,
1137 shmp_index(consumer_chan->chan->handle, buf->backend.array, sb_bindex)->shmp)->mmap_offset;
57773204
MD
1138 return 0;
1139}
1140
1141/* returns the size of the current sub-buffer, without padding (for mmap). */
74d81a6c
MD
1142int ustctl_get_subbuf_size(struct ustctl_consumer_stream *stream,
1143 unsigned long *len)
57773204 1144{
74d81a6c 1145 struct ustctl_consumer_channel *consumer_chan;
66bdd22a 1146 struct channel *chan;
74d81a6c 1147 struct lttng_ust_lib_ring_buffer *buf;
57773204 1148
74d81a6c 1149 if (!stream)
9bfc503d
MD
1150 return -EINVAL;
1151
74d81a6c
MD
1152 buf = stream->buf;
1153 consumer_chan = stream->chan;
1154 chan = consumer_chan->chan->chan;
57773204 1155 *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf,
74d81a6c 1156 consumer_chan->chan->handle);
57773204
MD
1157 return 0;
1158}
1159
1160/* returns the size of the current sub-buffer, without padding (for mmap). */
74d81a6c
MD
1161int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream *stream,
1162 unsigned long *len)
57773204 1163{
74d81a6c 1164 struct ustctl_consumer_channel *consumer_chan;
66bdd22a 1165 struct channel *chan;
74d81a6c 1166 struct lttng_ust_lib_ring_buffer *buf;
57773204 1167
74d81a6c 1168 if (!stream)
9bfc503d 1169 return -EINVAL;
74d81a6c
MD
1170 buf = stream->buf;
1171 consumer_chan = stream->chan;
1172 chan = consumer_chan->chan->chan;
57773204 1173 *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf,
74d81a6c 1174 consumer_chan->chan->handle);
57773204
MD
1175 *len = PAGE_ALIGN(*len);
1176 return 0;
1177}
1178
1179/* Get exclusive read access to the next sub-buffer that can be read. */
74d81a6c 1180int ustctl_get_next_subbuf(struct ustctl_consumer_stream *stream)
57773204 1181{
74d81a6c
MD
1182 struct lttng_ust_lib_ring_buffer *buf;
1183 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1184
74d81a6c
MD
1185 if (!stream)
1186 return -EINVAL;
1187 buf = stream->buf;
1188 consumer_chan = stream->chan;
1189 return lib_ring_buffer_get_next_subbuf(buf,
1190 consumer_chan->chan->handle);
57773204
MD
1191}
1192
1193
1194/* Release exclusive sub-buffer access, move consumer forward. */
74d81a6c 1195int ustctl_put_next_subbuf(struct ustctl_consumer_stream *stream)
57773204 1196{
74d81a6c
MD
1197 struct lttng_ust_lib_ring_buffer *buf;
1198 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1199
74d81a6c
MD
1200 if (!stream)
1201 return -EINVAL;
1202 buf = stream->buf;
1203 consumer_chan = stream->chan;
1204 lib_ring_buffer_put_next_subbuf(buf, consumer_chan->chan->handle);
57773204
MD
1205 return 0;
1206}
1207
1208/* snapshot */
1209
1210/* Get a snapshot of the current ring buffer producer and consumer positions */
74d81a6c 1211int ustctl_snapshot(struct ustctl_consumer_stream *stream)
57773204 1212{
74d81a6c
MD
1213 struct lttng_ust_lib_ring_buffer *buf;
1214 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1215
74d81a6c
MD
1216 if (!stream)
1217 return -EINVAL;
1218 buf = stream->buf;
1219 consumer_chan = stream->chan;
57773204 1220 return lib_ring_buffer_snapshot(buf, &buf->cons_snapshot,
74d81a6c 1221 &buf->prod_snapshot, consumer_chan->chan->handle);
57773204
MD
1222}
1223
1224/* Get the consumer position (iteration start) */
74d81a6c
MD
1225int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream *stream,
1226 unsigned long *pos)
57773204 1227{
74d81a6c 1228 struct lttng_ust_lib_ring_buffer *buf;
9bfc503d 1229
74d81a6c
MD
1230 if (!stream)
1231 return -EINVAL;
1232 buf = stream->buf;
57773204
MD
1233 *pos = buf->cons_snapshot;
1234 return 0;
1235}
1236
1237/* Get the producer position (iteration end) */
74d81a6c
MD
1238int ustctl_snapshot_get_produced(struct ustctl_consumer_stream *stream,
1239 unsigned long *pos)
57773204 1240{
74d81a6c 1241 struct lttng_ust_lib_ring_buffer *buf;
9bfc503d 1242
74d81a6c
MD
1243 if (!stream)
1244 return -EINVAL;
1245 buf = stream->buf;
57773204
MD
1246 *pos = buf->prod_snapshot;
1247 return 0;
1248}
1249
1250/* Get exclusive read access to the specified sub-buffer position */
74d81a6c
MD
1251int ustctl_get_subbuf(struct ustctl_consumer_stream *stream,
1252 unsigned long *pos)
57773204 1253{
74d81a6c
MD
1254 struct lttng_ust_lib_ring_buffer *buf;
1255 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1256
74d81a6c
MD
1257 if (!stream)
1258 return -EINVAL;
1259 buf = stream->buf;
1260 consumer_chan = stream->chan;
1261 return lib_ring_buffer_get_subbuf(buf, *pos,
1262 consumer_chan->chan->handle);
57773204
MD
1263}
1264
1265/* Release exclusive sub-buffer access */
74d81a6c 1266int ustctl_put_subbuf(struct ustctl_consumer_stream *stream)
57773204 1267{
74d81a6c
MD
1268 struct lttng_ust_lib_ring_buffer *buf;
1269 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1270
74d81a6c
MD
1271 if (!stream)
1272 return -EINVAL;
1273 buf = stream->buf;
1274 consumer_chan = stream->chan;
1275 lib_ring_buffer_put_subbuf(buf, consumer_chan->chan->handle);
57773204
MD
1276 return 0;
1277}
1278
74d81a6c 1279void ustctl_flush_buffer(struct ustctl_consumer_stream *stream,
b52190f2 1280 int producer_active)
57773204 1281{
74d81a6c
MD
1282 struct lttng_ust_lib_ring_buffer *buf;
1283 struct ustctl_consumer_channel *consumer_chan;
1284
1285 assert(stream);
1286 buf = stream->buf;
1287 consumer_chan = stream->chan;
b52190f2
MD
1288 lib_ring_buffer_switch_slow(buf,
1289 producer_active ? SWITCH_ACTIVE : SWITCH_FLUSH,
74d81a6c
MD
1290 consumer_chan->chan->handle);
1291}
1292
32ce8569
MD
1293/*
1294 * Returns 0 on success, negative error value on error.
1295 */
1296int ustctl_recv_reg_msg(int sock,
1297 enum ustctl_socket_type *type,
1298 uint32_t *major,
1299 uint32_t *minor,
1300 uint32_t *pid,
1301 uint32_t *ppid,
1302 uint32_t *uid,
1303 uint32_t *gid,
1304 uint32_t *bits_per_long,
1305 uint32_t *uint8_t_alignment,
1306 uint32_t *uint16_t_alignment,
1307 uint32_t *uint32_t_alignment,
1308 uint32_t *uint64_t_alignment,
1309 uint32_t *long_alignment,
1310 int *byte_order,
1311 char *name)
1312{
1313 ssize_t len;
1314 struct ustctl_reg_msg reg_msg;
1315
1316 len = ustcomm_recv_unix_sock(sock, &reg_msg, sizeof(reg_msg));
1317 if (len > 0 && len != sizeof(reg_msg))
1318 return -EIO;
1319 if (len == 0)
1320 return -EPIPE;
1321 if (len < 0)
1322 return len;
1323
1324 if (reg_msg.magic == LTTNG_UST_COMM_MAGIC) {
1325 *byte_order = BYTE_ORDER == BIG_ENDIAN ?
1326 BIG_ENDIAN : LITTLE_ENDIAN;
1327 } else if (reg_msg.magic == bswap_32(LTTNG_UST_COMM_MAGIC)) {
1328 *byte_order = BYTE_ORDER == BIG_ENDIAN ?
1329 LITTLE_ENDIAN : BIG_ENDIAN;
1330 } else {
1331 return -LTTNG_UST_ERR_INVAL_MAGIC;
1332 }
1333 switch (reg_msg.socket_type) {
1334 case 0: *type = USTCTL_SOCKET_CMD;
1335 break;
1336 case 1: *type = USTCTL_SOCKET_NOTIFY;
1337 break;
1338 default:
1339 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE;
1340 }
1341 *major = reg_msg.major;
1342 *minor = reg_msg.minor;
1343 *pid = reg_msg.pid;
1344 *ppid = reg_msg.ppid;
1345 *uid = reg_msg.uid;
1346 *gid = reg_msg.gid;
1347 *bits_per_long = reg_msg.bits_per_long;
1348 *uint8_t_alignment = reg_msg.uint8_t_alignment;
1349 *uint16_t_alignment = reg_msg.uint16_t_alignment;
1350 *uint32_t_alignment = reg_msg.uint32_t_alignment;
1351 *uint64_t_alignment = reg_msg.uint64_t_alignment;
1352 *long_alignment = reg_msg.long_alignment;
1353 memcpy(name, reg_msg.name, LTTNG_UST_ABI_PROCNAME_LEN);
1354 if (reg_msg.major != LTTNG_UST_ABI_MAJOR_VERSION) {
1355 return -LTTNG_UST_ERR_UNSUP_MAJOR;
1356 }
1357
1358 return 0;
1359}
1360
1361int ustctl_recv_notify(int sock, enum ustctl_notify_cmd *notify_cmd)
1362{
1363 struct ustcomm_notify_hdr header;
1364 ssize_t len;
1365
1366 len = ustcomm_recv_unix_sock(sock, &header, sizeof(header));
1367 if (len > 0 && len != sizeof(header))
1368 return -EIO;
1369 if (len == 0)
1370 return -EPIPE;
1371 if (len < 0)
1372 return len;
1373 switch (header.notify_cmd) {
1374 case 0:
1375 *notify_cmd = USTCTL_NOTIFY_CMD_EVENT;
1376 break;
1377 case 1:
1378 *notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL;
1379 break;
1380 default:
1381 return -EINVAL;
1382 }
1383 return 0;
1384}
1385
1386/*
1387 * Returns 0 on success, negative error value on error.
1388 */
1389int ustctl_recv_register_event(int sock,
1390 int *session_objd,
1391 int *channel_objd,
1392 char *event_name,
1393 int *loglevel,
1394 char **signature,
1395 size_t *nr_fields,
1396 struct ustctl_field **fields,
1397 char **model_emf_uri)
1398{
1399 ssize_t len;
1400 struct ustcomm_notify_event_msg msg;
1401 size_t signature_len, fields_len, model_emf_uri_len;
1402 char *a_sign = NULL, *a_model_emf_uri = NULL;
1403 struct ustctl_field *a_fields = NULL;
1404
1405 len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg));
1406 if (len > 0 && len != sizeof(msg))
1407 return -EIO;
1408 if (len == 0)
1409 return -EPIPE;
1410 if (len < 0)
1411 return len;
1412
1413 *session_objd = msg.session_objd;
1414 *channel_objd = msg.channel_objd;
1415 strncpy(event_name, msg.event_name, LTTNG_UST_SYM_NAME_LEN);
1416 event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1417 *loglevel = msg.loglevel;
1418 signature_len = msg.signature_len;
1419 fields_len = msg.fields_len;
1420
1421 if (fields_len % sizeof(*a_fields) != 0) {
1422 return -EINVAL;
1423 }
1424
1425 model_emf_uri_len = msg.model_emf_uri_len;
1426
1427 /* recv signature. contains at least \0. */
1428 a_sign = zmalloc(signature_len);
1429 if (!a_sign)
1430 return -ENOMEM;
1431 len = ustcomm_recv_unix_sock(sock, a_sign, signature_len);
1432 if (len > 0 && len != signature_len) {
1433 len = -EIO;
1434 goto signature_error;
1435 }
1436 if (len == 0) {
1437 len = -EPIPE;
1438 goto signature_error;
1439 }
1440 if (len < 0) {
1441 goto signature_error;
1442 }
1443 /* Enforce end of string */
1444 signature[signature_len - 1] = '\0';
1445
1446 /* recv fields */
1447 if (fields_len) {
1448 a_fields = zmalloc(fields_len);
1449 if (!a_fields) {
1450 len = -ENOMEM;
1451 goto signature_error;
1452 }
1453 len = ustcomm_recv_unix_sock(sock, a_fields, fields_len);
1454 if (len > 0 && len != fields_len) {
1455 len = -EIO;
1456 goto fields_error;
1457 }
1458 if (len == 0) {
1459 len = -EPIPE;
1460 goto fields_error;
1461 }
1462 if (len < 0) {
1463 goto fields_error;
1464 }
1465 }
1466
1467 if (model_emf_uri_len) {
1468 /* recv model_emf_uri_len */
1469 a_model_emf_uri = zmalloc(model_emf_uri_len);
1470 if (!a_model_emf_uri) {
1471 len = -ENOMEM;
1472 goto fields_error;
1473 }
1474 len = ustcomm_recv_unix_sock(sock, a_model_emf_uri,
1475 model_emf_uri_len);
1476 if (len > 0 && len != model_emf_uri_len) {
1477 len = -EIO;
1478 goto model_error;
1479 }
1480 if (len == 0) {
1481 len = -EPIPE;
1482 goto model_error;
1483 }
1484 if (len < 0) {
1485 goto model_error;
1486 }
1487 /* Enforce end of string */
1488 a_model_emf_uri[model_emf_uri_len - 1] = '\0';
1489 }
1490
1491 *signature = a_sign;
1492 *nr_fields = fields_len / sizeof(*a_fields);
1493 *fields = a_fields;
1494 *model_emf_uri = a_model_emf_uri;
1495
1496 return 0;
1497
1498model_error:
1499 free(a_model_emf_uri);
1500fields_error:
1501 free(a_fields);
1502signature_error:
1503 free(a_sign);
1504 return len;
1505}
1506
1507/*
1508 * Returns 0 on success, negative error value on error.
1509 */
1510int ustctl_reply_register_event(int sock,
1511 uint32_t id,
1512 int ret_code)
1513{
1514 ssize_t len;
1515 struct {
1516 struct ustcomm_notify_hdr header;
1517 struct ustcomm_notify_event_reply r;
1518 } reply;
1519
1520 memset(&reply, 0, sizeof(reply));
1521 reply.header.notify_cmd = USTCTL_NOTIFY_CMD_EVENT;
1522 reply.r.ret_code = ret_code;
1523 reply.r.event_id = id;
1524 len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply));
1525 if (len > 0 && len != sizeof(reply))
1526 return -EIO;
1527 if (len < 0)
1528 return len;
1529 return 0;
1530}
1531
1532/*
1533 * Returns 0 on success, negative UST or system error value on error.
1534 */
1535int ustctl_recv_register_channel(int sock,
1536 int *session_objd, /* session descriptor (output) */
1537 int *channel_objd, /* channel descriptor (output) */
1538 size_t *nr_fields,
1539 struct ustctl_field **fields)
1540{
1541 ssize_t len;
1542 struct ustcomm_notify_channel_msg msg;
1543 size_t fields_len;
1544 struct ustctl_field *a_fields;
1545
1546 len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg));
1547 if (len > 0 && len != sizeof(msg))
1548 return -EIO;
1549 if (len == 0)
1550 return -EPIPE;
1551 if (len < 0)
1552 return len;
1553
1554 *session_objd = msg.session_objd;
1555 *channel_objd = msg.channel_objd;
1556 fields_len = msg.ctx_fields_len;
1557
1558 if (fields_len % sizeof(*a_fields) != 0) {
1559 return -EINVAL;
1560 }
1561
1562 /* recv fields */
1563 if (fields_len) {
1564 a_fields = zmalloc(fields_len);
1565 if (!a_fields) {
1566 len = -ENOMEM;
1567 goto alloc_error;
1568 }
1569 len = ustcomm_recv_unix_sock(sock, a_fields, fields_len);
1570 if (len > 0 && len != fields_len) {
1571 len = -EIO;
1572 goto fields_error;
1573 }
1574 if (len == 0) {
1575 len = -EPIPE;
1576 goto fields_error;
1577 }
1578 if (len < 0) {
1579 goto fields_error;
1580 }
1581 *fields = a_fields;
1582 } else {
1583 *fields = NULL;
1584 }
1585 *nr_fields = fields_len / sizeof(*a_fields);
1586 return 0;
1587
1588fields_error:
1589 free(a_fields);
1590alloc_error:
1591 return len;
1592}
1593
1594/*
1595 * Returns 0 on success, negative error value on error.
1596 */
1597int ustctl_reply_register_channel(int sock,
1598 uint32_t chan_id,
1599 enum ustctl_channel_header header_type,
1600 int ret_code)
1601{
1602 ssize_t len;
1603 struct {
1604 struct ustcomm_notify_hdr header;
1605 struct ustcomm_notify_channel_reply r;
1606 } reply;
1607
1608 memset(&reply, 0, sizeof(reply));
1609 reply.header.notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL;
1610 reply.r.ret_code = ret_code;
1611 reply.r.chan_id = chan_id;
1612 switch (header_type) {
1613 case USTCTL_CHANNEL_HEADER_COMPACT:
1614 reply.r.header_type = 1;
1615 break;
1616 case USTCTL_CHANNEL_HEADER_LARGE:
1617 reply.r.header_type = 2;
1618 break;
1619 default:
1620 reply.r.header_type = 0;
1621 break;
1622 }
1623 len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply));
1624 if (len > 0 && len != sizeof(reply))
1625 return -EIO;
1626 if (len < 0)
1627 return len;
1628 return 0;
1629}
1630
74d81a6c
MD
1631static __attribute__((constructor))
1632void ustctl_init(void)
1633{
1634 init_usterr();
1635 lttng_ring_buffer_metadata_client_init();
1636 lttng_ring_buffer_client_overwrite_init();
1637 lttng_ring_buffer_client_discard_init();
03d2d293 1638 lib_ringbuffer_signal_init();
74d81a6c
MD
1639}
1640
1641static __attribute__((destructor))
1642void ustctl_exit(void)
1643{
1644 lttng_ring_buffer_client_discard_exit();
1645 lttng_ring_buffer_client_overwrite_exit();
1646 lttng_ring_buffer_metadata_client_exit();
57773204 1647}
This page took 0.113427 seconds and 4 git commands to generate.