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