Fix: build with -fno-common
[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>
c62a3816 21#include <lttng/ust-config.h>
4318ae1b
MD
22#include <lttng/ust-ctl.h>
23#include <lttng/ust-abi.h>
c1fca457 24#include <lttng/ust-events.h>
7a784989 25#include <sys/mman.h>
32ce8569 26#include <byteswap.h>
44c72f10
MD
27
28#include <usterr-signal-safe.h>
b728d87e 29#include <ust-comm.h>
74d81a6c 30#include <helper.h>
57773204
MD
31
32#include "../libringbuffer/backend.h"
33#include "../libringbuffer/frontend.h"
c9023c93 34#include "../liblttng-ust/wait.h"
b2f3252a 35#include "../liblttng-ust/lttng-rb-clients.h"
f9364363 36#include "../liblttng-ust/clock.h"
92ce256d 37#include "../liblttng-ust/getenv.h"
c9023c93
MD
38
39/*
40 * Number of milliseconds to retry before failing metadata writes on
41 * buffer full condition. (10 seconds)
42 */
43#define LTTNG_METADATA_TIMEOUT_MSEC 10000
57773204 44
74d81a6c
MD
45/*
46 * Channel representation within consumer.
47 */
48struct ustctl_consumer_channel {
49 struct lttng_channel *chan; /* lttng channel buffers */
6b120308 50
74d81a6c
MD
51 /* initial attributes */
52 struct ustctl_consumer_channel_attr attr;
ff0f5728
MD
53 int wait_fd; /* monitor close() */
54 int wakeup_fd; /* monitor close() */
74d81a6c
MD
55};
56
57/*
58 * Stream representation within consumer.
59 */
60struct ustctl_consumer_stream {
61 struct lttng_ust_shm_handle *handle; /* shared-memory handle */
62 struct lttng_ust_lib_ring_buffer *buf;
63 struct ustctl_consumer_channel *chan;
64 int shm_fd, wait_fd, wakeup_fd;
65 int cpu;
66 uint64_t memory_map_size;
67};
68
69extern void lttng_ring_buffer_client_overwrite_init(void);
08a3170c 70extern void lttng_ring_buffer_client_overwrite_rt_init(void);
74d81a6c 71extern void lttng_ring_buffer_client_discard_init(void);
08a3170c 72extern void lttng_ring_buffer_client_discard_rt_init(void);
74d81a6c
MD
73extern void lttng_ring_buffer_metadata_client_init(void);
74extern void lttng_ring_buffer_client_overwrite_exit(void);
08a3170c 75extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
74d81a6c 76extern void lttng_ring_buffer_client_discard_exit(void);
08a3170c 77extern void lttng_ring_buffer_client_discard_rt_exit(void);
74d81a6c
MD
78extern void lttng_ring_buffer_metadata_client_exit(void);
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,
b2c5f61a
MD
1058 stream_fds, nr_stream_fds,
1059 attr->blocking_timeout);
74d81a6c
MD
1060 if (!chan->chan) {
1061 goto chan_error;
1062 }
1063 chan->chan->ops = &transport->ops;
1064 memcpy(&chan->attr, attr, sizeof(chan->attr));
cb7378b3
MD
1065 chan->wait_fd = ustctl_channel_get_wait_fd(chan);
1066 chan->wakeup_fd = ustctl_channel_get_wakeup_fd(chan);
74d81a6c
MD
1067 return chan;
1068
1069chan_error:
1070 free(chan);
1071 return NULL;
57773204
MD
1072}
1073
74d81a6c 1074void ustctl_destroy_channel(struct ustctl_consumer_channel *chan)
57773204 1075{
b24e4e91
MD
1076 (void) ustctl_channel_close_wait_fd(chan);
1077 (void) ustctl_channel_close_wakeup_fd(chan);
74d81a6c
MD
1078 chan->chan->ops->channel_destroy(chan->chan);
1079 free(chan);
1080}
1081
1082int ustctl_send_channel_to_sessiond(int sock,
1083 struct ustctl_consumer_channel *channel)
1084{
1085 struct shm_object_table *table;
57773204 1086
74d81a6c
MD
1087 table = channel->chan->handle->table;
1088 if (table->size <= 0)
9bfc503d 1089 return -EINVAL;
74d81a6c
MD
1090 return ustctl_send_channel(sock,
1091 channel->attr.type,
1092 table->objects[0].memory_map,
1093 table->objects[0].memory_map_size,
ff0f5728 1094 channel->wakeup_fd,
74d81a6c
MD
1095 0);
1096}
9bfc503d 1097
74d81a6c
MD
1098int ustctl_send_stream_to_sessiond(int sock,
1099 struct ustctl_consumer_stream *stream)
1100{
1101 if (!stream)
1102 return ustctl_send_stream(sock, -1U, -1U, -1, -1, 0);
1103
1104 return ustctl_send_stream(sock,
1105 stream->cpu,
1106 stream->memory_map_size,
1107 stream->shm_fd, stream->wakeup_fd,
1108 0);
57773204
MD
1109}
1110
c9023c93
MD
1111int ustctl_write_metadata_to_channel(
1112 struct ustctl_consumer_channel *channel,
1113 const char *metadata_str, /* NOT null-terminated */
1114 size_t len) /* metadata length */
1115{
1116 struct lttng_ust_lib_ring_buffer_ctx ctx;
1117 struct lttng_channel *chan = channel->chan;
1118 const char *str = metadata_str;
1119 int ret = 0, waitret;
1120 size_t reserve_len, pos;
1121
1122 for (pos = 0; pos < len; pos += reserve_len) {
1123 reserve_len = min_t(size_t,
1124 chan->ops->packet_avail_size(chan->chan, chan->handle),
1125 len - pos);
1126 lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len,
53569322 1127 sizeof(char), -1, chan->handle, NULL);
c9023c93
MD
1128 /*
1129 * We don't care about metadata buffer's records lost
1130 * count, because we always retry here. Report error if
1131 * we need to bail out after timeout or being
1132 * interrupted.
1133 */
1134 waitret = wait_cond_interruptible_timeout(
1135 ({
1136 ret = chan->ops->event_reserve(&ctx, 0);
1137 ret != -ENOBUFS || !ret;
1138 }),
1139 LTTNG_METADATA_TIMEOUT_MSEC);
1140 if (waitret == -ETIMEDOUT || waitret == -EINTR || ret) {
1141 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
1142 waitret == -EINTR ? "interrupted" :
1143 (ret == -ENOBUFS ? "timeout" : "I/O error"));
1144 if (waitret == -EINTR)
1145 ret = waitret;
1146 goto end;
1147 }
1148 chan->ops->event_write(&ctx, &str[pos], reserve_len);
1149 chan->ops->event_commit(&ctx);
1150 }
1151end:
1152 return ret;
1153}
1154
3ef94b0e
JD
1155/*
1156 * Write at most one packet in the channel.
1157 * Returns the number of bytes written on success, < 0 on error.
1158 */
1159ssize_t ustctl_write_one_packet_to_channel(
1160 struct ustctl_consumer_channel *channel,
1161 const char *metadata_str, /* NOT null-terminated */
1162 size_t len) /* metadata length */
1163{
1164 struct lttng_ust_lib_ring_buffer_ctx ctx;
1165 struct lttng_channel *chan = channel->chan;
1166 const char *str = metadata_str;
1167 ssize_t reserve_len;
1168 int ret;
1169
1170 reserve_len = min_t(ssize_t,
1171 chan->ops->packet_avail_size(chan->chan, chan->handle),
1172 len);
1173 lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len,
53569322 1174 sizeof(char), -1, chan->handle, NULL);
3ef94b0e
JD
1175 ret = chan->ops->event_reserve(&ctx, 0);
1176 if (ret != 0) {
1177 DBG("LTTng: event reservation failed");
1178 assert(ret < 0);
1179 reserve_len = ret;
1180 goto end;
1181 }
1182 chan->ops->event_write(&ctx, str, reserve_len);
1183 chan->ops->event_commit(&ctx);
1184
1185end:
1186 return reserve_len;
1187}
1188
ff0f5728
MD
1189int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel *consumer_chan)
1190{
1191 struct channel *chan;
cb7378b3 1192 int ret;
ff0f5728
MD
1193
1194 chan = consumer_chan->chan->chan;
cb7378b3 1195 ret = ring_buffer_channel_close_wait_fd(&chan->backend.config,
ff0f5728 1196 chan, chan->handle);
cb7378b3
MD
1197 if (!ret)
1198 consumer_chan->wait_fd = -1;
1199 return ret;
ff0f5728
MD
1200}
1201
1202int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel *consumer_chan)
1203{
1204 struct channel *chan;
cb7378b3 1205 int ret;
ff0f5728
MD
1206
1207 chan = consumer_chan->chan->chan;
cb7378b3 1208 ret = ring_buffer_channel_close_wakeup_fd(&chan->backend.config,
ff0f5728 1209 chan, chan->handle);
cb7378b3
MD
1210 if (!ret)
1211 consumer_chan->wakeup_fd = -1;
1212 return ret;
ff0f5728
MD
1213}
1214
74d81a6c 1215int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream *stream)
5224b5c8
MD
1216{
1217 struct channel *chan;
1218
74d81a6c 1219 chan = stream->chan->chan->chan;
ff0f5728 1220 return ring_buffer_stream_close_wait_fd(&chan->backend.config,
74d81a6c 1221 chan, stream->handle, stream->cpu);
5224b5c8
MD
1222}
1223
74d81a6c 1224int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream *stream)
6e922b24 1225{
66bdd22a 1226 struct channel *chan;
74d81a6c
MD
1227
1228 chan = stream->chan->chan->chan;
ff0f5728 1229 return ring_buffer_stream_close_wakeup_fd(&chan->backend.config,
74d81a6c
MD
1230 chan, stream->handle, stream->cpu);
1231}
1232
1233struct ustctl_consumer_stream *
1234 ustctl_create_stream(struct ustctl_consumer_channel *channel,
1235 int cpu)
1236{
1237 struct ustctl_consumer_stream *stream;
1238 struct lttng_ust_shm_handle *handle;
1239 struct channel *chan;
1240 int shm_fd, wait_fd, wakeup_fd;
1241 uint64_t memory_map_size;
4cfec15c 1242 struct lttng_ust_lib_ring_buffer *buf;
6e922b24
MD
1243 int ret;
1244
74d81a6c
MD
1245 if (!channel)
1246 return NULL;
1247 handle = channel->chan->handle;
9bfc503d
MD
1248 if (!handle)
1249 return NULL;
1250
74d81a6c 1251 chan = channel->chan->chan;
6e922b24 1252 buf = channel_get_ring_buffer(&chan->backend.config,
74d81a6c
MD
1253 chan, cpu, handle, &shm_fd, &wait_fd,
1254 &wakeup_fd, &memory_map_size);
6e922b24
MD
1255 if (!buf)
1256 return NULL;
74d81a6c 1257 ret = lib_ring_buffer_open_read(buf, handle);
6e922b24
MD
1258 if (ret)
1259 return NULL;
74d81a6c
MD
1260
1261 stream = zmalloc(sizeof(*stream));
1262 if (!stream)
1263 goto alloc_error;
1264 stream->handle = handle;
1265 stream->buf = buf;
1266 stream->chan = channel;
1267 stream->shm_fd = shm_fd;
1268 stream->wait_fd = wait_fd;
1269 stream->wakeup_fd = wakeup_fd;
1270 stream->memory_map_size = memory_map_size;
1271 stream->cpu = cpu;
1272 return stream;
1273
1274alloc_error:
1275 return NULL;
1276}
1277
1278void ustctl_destroy_stream(struct ustctl_consumer_stream *stream)
1279{
1280 struct lttng_ust_lib_ring_buffer *buf;
1281 struct ustctl_consumer_channel *consumer_chan;
1282
1283 assert(stream);
1284 buf = stream->buf;
1285 consumer_chan = stream->chan;
b24e4e91
MD
1286 (void) ustctl_stream_close_wait_fd(stream);
1287 (void) ustctl_stream_close_wakeup_fd(stream);
74d81a6c
MD
1288 lib_ring_buffer_release_read(buf, consumer_chan->chan->handle);
1289 free(stream);
6e922b24
MD
1290}
1291
ff0f5728
MD
1292int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel *chan)
1293{
1294 if (!chan)
1295 return -EINVAL;
1296 return shm_get_wait_fd(chan->chan->handle,
1297 &chan->chan->handle->chan._ref);
1298}
1299
1300int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel *chan)
1301{
1302 if (!chan)
1303 return -EINVAL;
1304 return shm_get_wakeup_fd(chan->chan->handle,
1305 &chan->chan->handle->chan._ref);
1306}
1307
1308int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream *stream)
6e922b24 1309{
74d81a6c
MD
1310 struct lttng_ust_lib_ring_buffer *buf;
1311 struct ustctl_consumer_channel *consumer_chan;
1312
1313 if (!stream)
1314 return -EINVAL;
1315 buf = stream->buf;
1316 consumer_chan = stream->chan;
1317 return shm_get_wait_fd(consumer_chan->chan->handle, &buf->self._ref);
1318}
1319
ff0f5728 1320int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream *stream)
74d81a6c
MD
1321{
1322 struct lttng_ust_lib_ring_buffer *buf;
1323 struct ustctl_consumer_channel *consumer_chan;
1324
1325 if (!stream)
1326 return -EINVAL;
1327 buf = stream->buf;
1328 consumer_chan = stream->chan;
1329 return shm_get_wakeup_fd(consumer_chan->chan->handle, &buf->self._ref);
6e922b24
MD
1330}
1331
57773204
MD
1332/* For mmap mode, readable without "get" operation */
1333
74d81a6c 1334void *ustctl_get_mmap_base(struct ustctl_consumer_stream *stream)
9095efe9 1335{
74d81a6c
MD
1336 struct lttng_ust_lib_ring_buffer *buf;
1337 struct ustctl_consumer_channel *consumer_chan;
1338
1339 if (!stream)
9bfc503d 1340 return NULL;
74d81a6c
MD
1341 buf = stream->buf;
1342 consumer_chan = stream->chan;
1343 return shmp(consumer_chan->chan->handle, buf->backend.memory_map);
9095efe9
MD
1344}
1345
57773204 1346/* returns the length to mmap. */
74d81a6c 1347int ustctl_get_mmap_len(struct ustctl_consumer_stream *stream,
57773204
MD
1348 unsigned long *len)
1349{
74d81a6c 1350 struct ustctl_consumer_channel *consumer_chan;
57773204 1351 unsigned long mmap_buf_len;
66bdd22a 1352 struct channel *chan;
57773204 1353
74d81a6c 1354 if (!stream)
9bfc503d 1355 return -EINVAL;
74d81a6c
MD
1356 consumer_chan = stream->chan;
1357 chan = consumer_chan->chan->chan;
57773204
MD
1358 if (chan->backend.config.output != RING_BUFFER_MMAP)
1359 return -EINVAL;
1360 mmap_buf_len = chan->backend.buf_size;
1361 if (chan->backend.extra_reader_sb)
1362 mmap_buf_len += chan->backend.subbuf_size;
1363 if (mmap_buf_len > INT_MAX)
1364 return -EFBIG;
1365 *len = mmap_buf_len;
1366 return 0;
1367}
1368
1369/* returns the maximum size for sub-buffers. */
74d81a6c 1370int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream *stream,
57773204
MD
1371 unsigned long *len)
1372{
74d81a6c 1373 struct ustctl_consumer_channel *consumer_chan;
66bdd22a 1374 struct channel *chan;
57773204 1375
74d81a6c 1376 if (!stream)
9bfc503d 1377 return -EINVAL;
74d81a6c
MD
1378 consumer_chan = stream->chan;
1379 chan = consumer_chan->chan->chan;
57773204
MD
1380 *len = chan->backend.subbuf_size;
1381 return 0;
1382}
1383
1384/*
1385 * For mmap mode, operate on the current packet (between get/put or
1386 * get_next/put_next).
1387 */
1388
1389/* returns the offset of the subbuffer belonging to the mmap reader. */
74d81a6c
MD
1390int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream *stream,
1391 unsigned long *off)
57773204 1392{
66bdd22a 1393 struct channel *chan;
57773204 1394 unsigned long sb_bindex;
74d81a6c
MD
1395 struct lttng_ust_lib_ring_buffer *buf;
1396 struct ustctl_consumer_channel *consumer_chan;
34daae3e
MD
1397 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *barray_idx;
1398 struct lttng_ust_lib_ring_buffer_backend_pages *pages;
57773204 1399
74d81a6c 1400 if (!stream)
9bfc503d 1401 return -EINVAL;
74d81a6c
MD
1402 buf = stream->buf;
1403 consumer_chan = stream->chan;
1404 chan = consumer_chan->chan->chan;
57773204
MD
1405 if (chan->backend.config.output != RING_BUFFER_MMAP)
1406 return -EINVAL;
1407 sb_bindex = subbuffer_id_get_index(&chan->backend.config,
32ce8569 1408 buf->backend.buf_rsb.id);
34daae3e
MD
1409 barray_idx = shmp_index(consumer_chan->chan->handle, buf->backend.array,
1410 sb_bindex);
1411 if (!barray_idx)
1412 return -EINVAL;
1413 pages = shmp(consumer_chan->chan->handle, barray_idx->shmp);
1414 if (!pages)
1415 return -EINVAL;
1416 *off = pages->mmap_offset;
57773204
MD
1417 return 0;
1418}
1419
1420/* returns the size of the current sub-buffer, without padding (for mmap). */
74d81a6c
MD
1421int ustctl_get_subbuf_size(struct ustctl_consumer_stream *stream,
1422 unsigned long *len)
57773204 1423{
74d81a6c 1424 struct ustctl_consumer_channel *consumer_chan;
66bdd22a 1425 struct channel *chan;
74d81a6c 1426 struct lttng_ust_lib_ring_buffer *buf;
57773204 1427
74d81a6c 1428 if (!stream)
9bfc503d
MD
1429 return -EINVAL;
1430
74d81a6c
MD
1431 buf = stream->buf;
1432 consumer_chan = stream->chan;
1433 chan = consumer_chan->chan->chan;
57773204 1434 *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf,
74d81a6c 1435 consumer_chan->chan->handle);
57773204
MD
1436 return 0;
1437}
1438
1439/* returns the size of the current sub-buffer, without padding (for mmap). */
74d81a6c
MD
1440int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream *stream,
1441 unsigned long *len)
57773204 1442{
74d81a6c 1443 struct ustctl_consumer_channel *consumer_chan;
66bdd22a 1444 struct channel *chan;
74d81a6c 1445 struct lttng_ust_lib_ring_buffer *buf;
57773204 1446
74d81a6c 1447 if (!stream)
9bfc503d 1448 return -EINVAL;
74d81a6c
MD
1449 buf = stream->buf;
1450 consumer_chan = stream->chan;
1451 chan = consumer_chan->chan->chan;
57773204 1452 *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf,
74d81a6c 1453 consumer_chan->chan->handle);
57773204
MD
1454 *len = PAGE_ALIGN(*len);
1455 return 0;
1456}
1457
1458/* Get exclusive read access to the next sub-buffer that can be read. */
74d81a6c 1459int ustctl_get_next_subbuf(struct ustctl_consumer_stream *stream)
57773204 1460{
74d81a6c
MD
1461 struct lttng_ust_lib_ring_buffer *buf;
1462 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1463
74d81a6c
MD
1464 if (!stream)
1465 return -EINVAL;
1466 buf = stream->buf;
1467 consumer_chan = stream->chan;
1468 return lib_ring_buffer_get_next_subbuf(buf,
1469 consumer_chan->chan->handle);
57773204
MD
1470}
1471
1472
1473/* Release exclusive sub-buffer access, move consumer forward. */
74d81a6c 1474int ustctl_put_next_subbuf(struct ustctl_consumer_stream *stream)
57773204 1475{
74d81a6c
MD
1476 struct lttng_ust_lib_ring_buffer *buf;
1477 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1478
74d81a6c
MD
1479 if (!stream)
1480 return -EINVAL;
1481 buf = stream->buf;
1482 consumer_chan = stream->chan;
1483 lib_ring_buffer_put_next_subbuf(buf, consumer_chan->chan->handle);
57773204
MD
1484 return 0;
1485}
1486
1487/* snapshot */
1488
1489/* Get a snapshot of the current ring buffer producer and consumer positions */
74d81a6c 1490int ustctl_snapshot(struct ustctl_consumer_stream *stream)
57773204 1491{
74d81a6c
MD
1492 struct lttng_ust_lib_ring_buffer *buf;
1493 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1494
74d81a6c
MD
1495 if (!stream)
1496 return -EINVAL;
1497 buf = stream->buf;
1498 consumer_chan = stream->chan;
57773204 1499 return lib_ring_buffer_snapshot(buf, &buf->cons_snapshot,
74d81a6c 1500 &buf->prod_snapshot, consumer_chan->chan->handle);
57773204
MD
1501}
1502
f45930b7
JG
1503/*
1504 * Get a snapshot of the current ring buffer producer and consumer positions
1505 * even if the consumed and produced positions are contained within the same
1506 * subbuffer.
1507 */
1508int ustctl_snapshot_sample_positions(struct ustctl_consumer_stream *stream)
1509{
1510 struct lttng_ust_lib_ring_buffer *buf;
1511 struct ustctl_consumer_channel *consumer_chan;
1512
1513 if (!stream)
1514 return -EINVAL;
1515 buf = stream->buf;
1516 consumer_chan = stream->chan;
1517 return lib_ring_buffer_snapshot_sample_positions(buf,
1518 &buf->cons_snapshot, &buf->prod_snapshot,
1519 consumer_chan->chan->handle);
1520}
1521
57773204 1522/* Get the consumer position (iteration start) */
74d81a6c
MD
1523int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream *stream,
1524 unsigned long *pos)
57773204 1525{
74d81a6c 1526 struct lttng_ust_lib_ring_buffer *buf;
9bfc503d 1527
74d81a6c
MD
1528 if (!stream)
1529 return -EINVAL;
1530 buf = stream->buf;
57773204
MD
1531 *pos = buf->cons_snapshot;
1532 return 0;
1533}
1534
1535/* Get the producer position (iteration end) */
74d81a6c
MD
1536int ustctl_snapshot_get_produced(struct ustctl_consumer_stream *stream,
1537 unsigned long *pos)
57773204 1538{
74d81a6c 1539 struct lttng_ust_lib_ring_buffer *buf;
9bfc503d 1540
74d81a6c
MD
1541 if (!stream)
1542 return -EINVAL;
1543 buf = stream->buf;
57773204
MD
1544 *pos = buf->prod_snapshot;
1545 return 0;
1546}
1547
1548/* Get exclusive read access to the specified sub-buffer position */
74d81a6c
MD
1549int ustctl_get_subbuf(struct ustctl_consumer_stream *stream,
1550 unsigned long *pos)
57773204 1551{
74d81a6c
MD
1552 struct lttng_ust_lib_ring_buffer *buf;
1553 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1554
74d81a6c
MD
1555 if (!stream)
1556 return -EINVAL;
1557 buf = stream->buf;
1558 consumer_chan = stream->chan;
1559 return lib_ring_buffer_get_subbuf(buf, *pos,
1560 consumer_chan->chan->handle);
57773204
MD
1561}
1562
1563/* Release exclusive sub-buffer access */
74d81a6c 1564int ustctl_put_subbuf(struct ustctl_consumer_stream *stream)
57773204 1565{
74d81a6c
MD
1566 struct lttng_ust_lib_ring_buffer *buf;
1567 struct ustctl_consumer_channel *consumer_chan;
9bfc503d 1568
74d81a6c
MD
1569 if (!stream)
1570 return -EINVAL;
1571 buf = stream->buf;
1572 consumer_chan = stream->chan;
1573 lib_ring_buffer_put_subbuf(buf, consumer_chan->chan->handle);
57773204
MD
1574 return 0;
1575}
1576
74d81a6c 1577void ustctl_flush_buffer(struct ustctl_consumer_stream *stream,
b52190f2 1578 int producer_active)
57773204 1579{
74d81a6c
MD
1580 struct lttng_ust_lib_ring_buffer *buf;
1581 struct ustctl_consumer_channel *consumer_chan;
1582
1583 assert(stream);
1584 buf = stream->buf;
1585 consumer_chan = stream->chan;
b52190f2
MD
1586 lib_ring_buffer_switch_slow(buf,
1587 producer_active ? SWITCH_ACTIVE : SWITCH_FLUSH,
74d81a6c
MD
1588 consumer_chan->chan->handle);
1589}
1590
beca55a1
MD
1591void ustctl_clear_buffer(struct ustctl_consumer_stream *stream)
1592{
1593 struct lttng_ust_lib_ring_buffer *buf;
1594 struct ustctl_consumer_channel *consumer_chan;
1595
1596 assert(stream);
1597 buf = stream->buf;
1598 consumer_chan = stream->chan;
1599 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE,
1600 consumer_chan->chan->handle);
1601 lib_ring_buffer_clear_reader(buf, consumer_chan->chan->handle);
1602}
1603
b2f3252a
JD
1604static
1605struct lttng_ust_client_lib_ring_buffer_client_cb *get_client_cb(
1606 struct lttng_ust_lib_ring_buffer *buf,
1607 struct lttng_ust_shm_handle *handle)
1608{
1609 struct channel *chan;
1610 const struct lttng_ust_lib_ring_buffer_config *config;
1611 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1612
1613 chan = shmp(handle, buf->backend.chan);
34daae3e
MD
1614 if (!chan)
1615 return NULL;
b2f3252a
JD
1616 config = &chan->backend.config;
1617 if (!config->cb_ptr)
1618 return NULL;
1619 client_cb = caa_container_of(config->cb_ptr,
1620 struct lttng_ust_client_lib_ring_buffer_client_cb,
1621 parent);
1622 return client_cb;
1623}
1624
1625int ustctl_get_timestamp_begin(struct ustctl_consumer_stream *stream,
1626 uint64_t *timestamp_begin)
1627{
1628 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
e1919a41
MD
1629 struct lttng_ust_lib_ring_buffer *buf;
1630 struct lttng_ust_shm_handle *handle;
b2f3252a
JD
1631
1632 if (!stream || !timestamp_begin)
1633 return -EINVAL;
e1919a41
MD
1634 buf = stream->buf;
1635 handle = stream->chan->chan->handle;
b2f3252a
JD
1636 client_cb = get_client_cb(buf, handle);
1637 if (!client_cb)
1638 return -ENOSYS;
1639 return client_cb->timestamp_begin(buf, handle, timestamp_begin);
1640}
1641
1642int ustctl_get_timestamp_end(struct ustctl_consumer_stream *stream,
1643 uint64_t *timestamp_end)
1644{
1645 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
e1919a41
MD
1646 struct lttng_ust_lib_ring_buffer *buf;
1647 struct lttng_ust_shm_handle *handle;
b2f3252a
JD
1648
1649 if (!stream || !timestamp_end)
1650 return -EINVAL;
e1919a41
MD
1651 buf = stream->buf;
1652 handle = stream->chan->chan->handle;
b2f3252a
JD
1653 client_cb = get_client_cb(buf, handle);
1654 if (!client_cb)
1655 return -ENOSYS;
1656 return client_cb->timestamp_end(buf, handle, timestamp_end);
1657}
1658
1659int ustctl_get_events_discarded(struct ustctl_consumer_stream *stream,
1660 uint64_t *events_discarded)
1661{
1662 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
e1919a41
MD
1663 struct lttng_ust_lib_ring_buffer *buf;
1664 struct lttng_ust_shm_handle *handle;
b2f3252a
JD
1665
1666 if (!stream || !events_discarded)
1667 return -EINVAL;
e1919a41
MD
1668 buf = stream->buf;
1669 handle = stream->chan->chan->handle;
b2f3252a
JD
1670 client_cb = get_client_cb(buf, handle);
1671 if (!client_cb)
1672 return -ENOSYS;
1673 return client_cb->events_discarded(buf, handle, events_discarded);
1674}
1675
1676int ustctl_get_content_size(struct ustctl_consumer_stream *stream,
1677 uint64_t *content_size)
1678{
1679 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
e1919a41
MD
1680 struct lttng_ust_lib_ring_buffer *buf;
1681 struct lttng_ust_shm_handle *handle;
b2f3252a
JD
1682
1683 if (!stream || !content_size)
1684 return -EINVAL;
e1919a41
MD
1685 buf = stream->buf;
1686 handle = stream->chan->chan->handle;
b2f3252a
JD
1687 client_cb = get_client_cb(buf, handle);
1688 if (!client_cb)
1689 return -ENOSYS;
1690 return client_cb->content_size(buf, handle, content_size);
1691}
1692
1693int ustctl_get_packet_size(struct ustctl_consumer_stream *stream,
1694 uint64_t *packet_size)
1695{
1696 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
e1919a41
MD
1697 struct lttng_ust_lib_ring_buffer *buf;
1698 struct lttng_ust_shm_handle *handle;
b2f3252a
JD
1699
1700 if (!stream || !packet_size)
1701 return -EINVAL;
e1919a41
MD
1702 buf = stream->buf;
1703 handle = stream->chan->chan->handle;
b2f3252a
JD
1704 client_cb = get_client_cb(buf, handle);
1705 if (!client_cb)
1706 return -ENOSYS;
1707 return client_cb->packet_size(buf, handle, packet_size);
1708}
1709
1710int ustctl_get_stream_id(struct ustctl_consumer_stream *stream,
1711 uint64_t *stream_id)
1712{
1713 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
e1919a41
MD
1714 struct lttng_ust_lib_ring_buffer *buf;
1715 struct lttng_ust_shm_handle *handle;
b2f3252a
JD
1716
1717 if (!stream || !stream_id)
1718 return -EINVAL;
e1919a41
MD
1719 buf = stream->buf;
1720 handle = stream->chan->chan->handle;
b2f3252a
JD
1721 client_cb = get_client_cb(buf, handle);
1722 if (!client_cb)
1723 return -ENOSYS;
1724 return client_cb->stream_id(buf, handle, stream_id);
1725}
1726
fca361e8
JD
1727int ustctl_get_current_timestamp(struct ustctl_consumer_stream *stream,
1728 uint64_t *ts)
1729{
1730 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
e1919a41
MD
1731 struct lttng_ust_lib_ring_buffer *buf;
1732 struct lttng_ust_shm_handle *handle;
fca361e8
JD
1733
1734 if (!stream || !ts)
1735 return -EINVAL;
e1919a41
MD
1736 buf = stream->buf;
1737 handle = stream->chan->chan->handle;
fca361e8
JD
1738 client_cb = get_client_cb(buf, handle);
1739 if (!client_cb || !client_cb->current_timestamp)
1740 return -ENOSYS;
1741 return client_cb->current_timestamp(buf, handle, ts);
1742}
1743
1ff31389
JD
1744int ustctl_get_sequence_number(struct ustctl_consumer_stream *stream,
1745 uint64_t *seq)
1746{
1747 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1748 struct lttng_ust_lib_ring_buffer *buf;
1749 struct lttng_ust_shm_handle *handle;
1750
1751 if (!stream || !seq)
1752 return -EINVAL;
1753 buf = stream->buf;
1754 handle = stream->chan->chan->handle;
1755 client_cb = get_client_cb(buf, handle);
1756 if (!client_cb || !client_cb->sequence_number)
1757 return -ENOSYS;
1758 return client_cb->sequence_number(buf, handle, seq);
1759}
1760
45a00b05
JD
1761int ustctl_get_instance_id(struct ustctl_consumer_stream *stream,
1762 uint64_t *id)
1763{
1764 struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb;
1765 struct lttng_ust_lib_ring_buffer *buf;
1766 struct lttng_ust_shm_handle *handle;
1767
1768 if (!stream || !id)
1769 return -EINVAL;
1770 buf = stream->buf;
1771 handle = stream->chan->chan->handle;
1772 client_cb = get_client_cb(buf, handle);
1773 if (!client_cb)
1774 return -ENOSYS;
1775 return client_cb->instance_id(buf, handle, id);
1776}
1777
c62a3816 1778#ifdef LTTNG_UST_HAVE_PERF_EVENT
57201bb3
MD
1779
1780int ustctl_has_perf_counters(void)
1781{
1782 return 1;
1783}
1784
1785#else
1786
1787int ustctl_has_perf_counters(void)
1788{
1789 return 0;
1790}
1791
1792#endif
1793
32ce8569
MD
1794/*
1795 * Returns 0 on success, negative error value on error.
1796 */
1797int ustctl_recv_reg_msg(int sock,
1798 enum ustctl_socket_type *type,
1799 uint32_t *major,
1800 uint32_t *minor,
1801 uint32_t *pid,
1802 uint32_t *ppid,
1803 uint32_t *uid,
1804 uint32_t *gid,
1805 uint32_t *bits_per_long,
1806 uint32_t *uint8_t_alignment,
1807 uint32_t *uint16_t_alignment,
1808 uint32_t *uint32_t_alignment,
1809 uint32_t *uint64_t_alignment,
1810 uint32_t *long_alignment,
1811 int *byte_order,
1812 char *name)
1813{
1814 ssize_t len;
1815 struct ustctl_reg_msg reg_msg;
1816
1817 len = ustcomm_recv_unix_sock(sock, &reg_msg, sizeof(reg_msg));
1818 if (len > 0 && len != sizeof(reg_msg))
1819 return -EIO;
1820 if (len == 0)
1821 return -EPIPE;
1822 if (len < 0)
1823 return len;
1824
1825 if (reg_msg.magic == LTTNG_UST_COMM_MAGIC) {
1826 *byte_order = BYTE_ORDER == BIG_ENDIAN ?
1827 BIG_ENDIAN : LITTLE_ENDIAN;
1828 } else if (reg_msg.magic == bswap_32(LTTNG_UST_COMM_MAGIC)) {
1829 *byte_order = BYTE_ORDER == BIG_ENDIAN ?
1830 LITTLE_ENDIAN : BIG_ENDIAN;
1831 } else {
1832 return -LTTNG_UST_ERR_INVAL_MAGIC;
1833 }
1834 switch (reg_msg.socket_type) {
1835 case 0: *type = USTCTL_SOCKET_CMD;
1836 break;
1837 case 1: *type = USTCTL_SOCKET_NOTIFY;
1838 break;
1839 default:
1840 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE;
1841 }
1842 *major = reg_msg.major;
1843 *minor = reg_msg.minor;
1844 *pid = reg_msg.pid;
1845 *ppid = reg_msg.ppid;
1846 *uid = reg_msg.uid;
1847 *gid = reg_msg.gid;
1848 *bits_per_long = reg_msg.bits_per_long;
1849 *uint8_t_alignment = reg_msg.uint8_t_alignment;
1850 *uint16_t_alignment = reg_msg.uint16_t_alignment;
1851 *uint32_t_alignment = reg_msg.uint32_t_alignment;
1852 *uint64_t_alignment = reg_msg.uint64_t_alignment;
1853 *long_alignment = reg_msg.long_alignment;
1854 memcpy(name, reg_msg.name, LTTNG_UST_ABI_PROCNAME_LEN);
1855 if (reg_msg.major != LTTNG_UST_ABI_MAJOR_VERSION) {
1856 return -LTTNG_UST_ERR_UNSUP_MAJOR;
1857 }
1858
1859 return 0;
1860}
1861
1862int ustctl_recv_notify(int sock, enum ustctl_notify_cmd *notify_cmd)
1863{
1864 struct ustcomm_notify_hdr header;
1865 ssize_t len;
1866
1867 len = ustcomm_recv_unix_sock(sock, &header, sizeof(header));
1868 if (len > 0 && len != sizeof(header))
1869 return -EIO;
1870 if (len == 0)
1871 return -EPIPE;
1872 if (len < 0)
1873 return len;
1874 switch (header.notify_cmd) {
1875 case 0:
1876 *notify_cmd = USTCTL_NOTIFY_CMD_EVENT;
1877 break;
1878 case 1:
1879 *notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL;
1880 break;
c785c634
MD
1881 case 2:
1882 *notify_cmd = USTCTL_NOTIFY_CMD_ENUM;
1883 break;
32ce8569
MD
1884 default:
1885 return -EINVAL;
1886 }
1887 return 0;
1888}
1889
1890/*
1891 * Returns 0 on success, negative error value on error.
1892 */
1893int ustctl_recv_register_event(int sock,
1894 int *session_objd,
1895 int *channel_objd,
1896 char *event_name,
1897 int *loglevel,
1898 char **signature,
1899 size_t *nr_fields,
1900 struct ustctl_field **fields,
1901 char **model_emf_uri)
1902{
1903 ssize_t len;
1904 struct ustcomm_notify_event_msg msg;
1905 size_t signature_len, fields_len, model_emf_uri_len;
1906 char *a_sign = NULL, *a_model_emf_uri = NULL;
1907 struct ustctl_field *a_fields = NULL;
1908
1909 len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg));
1910 if (len > 0 && len != sizeof(msg))
1911 return -EIO;
1912 if (len == 0)
1913 return -EPIPE;
1914 if (len < 0)
1915 return len;
1916
1917 *session_objd = msg.session_objd;
1918 *channel_objd = msg.channel_objd;
1919 strncpy(event_name, msg.event_name, LTTNG_UST_SYM_NAME_LEN);
1920 event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1921 *loglevel = msg.loglevel;
1922 signature_len = msg.signature_len;
1923 fields_len = msg.fields_len;
1924
1925 if (fields_len % sizeof(*a_fields) != 0) {
1926 return -EINVAL;
1927 }
1928
1929 model_emf_uri_len = msg.model_emf_uri_len;
1930
1931 /* recv signature. contains at least \0. */
1932 a_sign = zmalloc(signature_len);
1933 if (!a_sign)
1934 return -ENOMEM;
1935 len = ustcomm_recv_unix_sock(sock, a_sign, signature_len);
1936 if (len > 0 && len != signature_len) {
1937 len = -EIO;
1938 goto signature_error;
1939 }
1940 if (len == 0) {
1941 len = -EPIPE;
1942 goto signature_error;
1943 }
1944 if (len < 0) {
1945 goto signature_error;
1946 }
1947 /* Enforce end of string */
111198c2 1948 a_sign[signature_len - 1] = '\0';
32ce8569
MD
1949
1950 /* recv fields */
1951 if (fields_len) {
1952 a_fields = zmalloc(fields_len);
1953 if (!a_fields) {
1954 len = -ENOMEM;
1955 goto signature_error;
1956 }
1957 len = ustcomm_recv_unix_sock(sock, a_fields, fields_len);
1958 if (len > 0 && len != fields_len) {
1959 len = -EIO;
1960 goto fields_error;
1961 }
1962 if (len == 0) {
1963 len = -EPIPE;
1964 goto fields_error;
1965 }
1966 if (len < 0) {
1967 goto fields_error;
1968 }
1969 }
1970
1971 if (model_emf_uri_len) {
1972 /* recv model_emf_uri_len */
1973 a_model_emf_uri = zmalloc(model_emf_uri_len);
1974 if (!a_model_emf_uri) {
1975 len = -ENOMEM;
1976 goto fields_error;
1977 }
1978 len = ustcomm_recv_unix_sock(sock, a_model_emf_uri,
1979 model_emf_uri_len);
1980 if (len > 0 && len != model_emf_uri_len) {
1981 len = -EIO;
1982 goto model_error;
1983 }
1984 if (len == 0) {
1985 len = -EPIPE;
1986 goto model_error;
1987 }
1988 if (len < 0) {
1989 goto model_error;
1990 }
1991 /* Enforce end of string */
1992 a_model_emf_uri[model_emf_uri_len - 1] = '\0';
1993 }
1994
1995 *signature = a_sign;
1996 *nr_fields = fields_len / sizeof(*a_fields);
1997 *fields = a_fields;
1998 *model_emf_uri = a_model_emf_uri;
1999
2000 return 0;
2001
2002model_error:
2003 free(a_model_emf_uri);
2004fields_error:
2005 free(a_fields);
2006signature_error:
2007 free(a_sign);
2008 return len;
2009}
2010
2011/*
2012 * Returns 0 on success, negative error value on error.
2013 */
2014int ustctl_reply_register_event(int sock,
2015 uint32_t id,
2016 int ret_code)
2017{
2018 ssize_t len;
2019 struct {
2020 struct ustcomm_notify_hdr header;
2021 struct ustcomm_notify_event_reply r;
2022 } reply;
2023
2024 memset(&reply, 0, sizeof(reply));
2025 reply.header.notify_cmd = USTCTL_NOTIFY_CMD_EVENT;
2026 reply.r.ret_code = ret_code;
2027 reply.r.event_id = id;
2028 len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply));
2029 if (len > 0 && len != sizeof(reply))
2030 return -EIO;
2031 if (len < 0)
2032 return len;
2033 return 0;
2034}
2035
c785c634
MD
2036/*
2037 * Returns 0 on success, negative UST or system error value on error.
2038 */
2039int ustctl_recv_register_enum(int sock,
2040 int *session_objd,
2041 char *enum_name,
2042 struct ustctl_enum_entry **entries,
2043 size_t *nr_entries)
2044{
2045 ssize_t len;
2046 struct ustcomm_notify_enum_msg msg;
2047 size_t entries_len;
2048 struct ustctl_enum_entry *a_entries = NULL;
2049
2050 len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg));
2051 if (len > 0 && len != sizeof(msg))
2052 return -EIO;
2053 if (len == 0)
2054 return -EPIPE;
2055 if (len < 0)
2056 return len;
2057
2058 *session_objd = msg.session_objd;
2059 strncpy(enum_name, msg.enum_name, LTTNG_UST_SYM_NAME_LEN);
2060 enum_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
2061 entries_len = msg.entries_len;
2062
2063 if (entries_len % sizeof(*a_entries) != 0) {
2064 return -EINVAL;
2065 }
2066
2067 /* recv entries */
2068 if (entries_len) {
2069 a_entries = zmalloc(entries_len);
2070 if (!a_entries)
2071 return -ENOMEM;
2072 len = ustcomm_recv_unix_sock(sock, a_entries, entries_len);
2073 if (len > 0 && len != entries_len) {
2074 len = -EIO;
2075 goto entries_error;
2076 }
2077 if (len == 0) {
2078 len = -EPIPE;
2079 goto entries_error;
2080 }
2081 if (len < 0) {
2082 goto entries_error;
2083 }
2084 }
2085 *nr_entries = entries_len / sizeof(*a_entries);
2086 *entries = a_entries;
2087
2088 return 0;
2089
2090entries_error:
2091 free(a_entries);
2092 return len;
2093}
2094
2095/*
2096 * Returns 0 on success, negative error value on error.
2097 */
2098int ustctl_reply_register_enum(int sock,
2099 uint64_t id,
2100 int ret_code)
2101{
2102 ssize_t len;
2103 struct {
2104 struct ustcomm_notify_hdr header;
2105 struct ustcomm_notify_enum_reply r;
2106 } reply;
2107
2108 memset(&reply, 0, sizeof(reply));
2109 reply.header.notify_cmd = USTCTL_NOTIFY_CMD_ENUM;
2110 reply.r.ret_code = ret_code;
2111 reply.r.enum_id = id;
2112 len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply));
2113 if (len > 0 && len != sizeof(reply))
2114 return -EIO;
2115 if (len < 0)
2116 return len;
2117 return 0;
2118}
2119
32ce8569
MD
2120/*
2121 * Returns 0 on success, negative UST or system error value on error.
2122 */
2123int ustctl_recv_register_channel(int sock,
2124 int *session_objd, /* session descriptor (output) */
2125 int *channel_objd, /* channel descriptor (output) */
2126 size_t *nr_fields,
2127 struct ustctl_field **fields)
2128{
2129 ssize_t len;
2130 struct ustcomm_notify_channel_msg msg;
2131 size_t fields_len;
2132 struct ustctl_field *a_fields;
2133
2134 len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg));
2135 if (len > 0 && len != sizeof(msg))
2136 return -EIO;
2137 if (len == 0)
2138 return -EPIPE;
2139 if (len < 0)
2140 return len;
2141
2142 *session_objd = msg.session_objd;
2143 *channel_objd = msg.channel_objd;
2144 fields_len = msg.ctx_fields_len;
2145
2146 if (fields_len % sizeof(*a_fields) != 0) {
2147 return -EINVAL;
2148 }
2149
2150 /* recv fields */
2151 if (fields_len) {
2152 a_fields = zmalloc(fields_len);
2153 if (!a_fields) {
2154 len = -ENOMEM;
2155 goto alloc_error;
2156 }
2157 len = ustcomm_recv_unix_sock(sock, a_fields, fields_len);
2158 if (len > 0 && len != fields_len) {
2159 len = -EIO;
2160 goto fields_error;
2161 }
2162 if (len == 0) {
2163 len = -EPIPE;
2164 goto fields_error;
2165 }
2166 if (len < 0) {
2167 goto fields_error;
2168 }
2169 *fields = a_fields;
2170 } else {
2171 *fields = NULL;
2172 }
2173 *nr_fields = fields_len / sizeof(*a_fields);
2174 return 0;
2175
2176fields_error:
2177 free(a_fields);
2178alloc_error:
2179 return len;
2180}
2181
2182/*
2183 * Returns 0 on success, negative error value on error.
2184 */
2185int ustctl_reply_register_channel(int sock,
2186 uint32_t chan_id,
2187 enum ustctl_channel_header header_type,
2188 int ret_code)
2189{
2190 ssize_t len;
2191 struct {
2192 struct ustcomm_notify_hdr header;
2193 struct ustcomm_notify_channel_reply r;
2194 } reply;
2195
2196 memset(&reply, 0, sizeof(reply));
2197 reply.header.notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL;
2198 reply.r.ret_code = ret_code;
2199 reply.r.chan_id = chan_id;
2200 switch (header_type) {
2201 case USTCTL_CHANNEL_HEADER_COMPACT:
2202 reply.r.header_type = 1;
2203 break;
2204 case USTCTL_CHANNEL_HEADER_LARGE:
2205 reply.r.header_type = 2;
2206 break;
2207 default:
2208 reply.r.header_type = 0;
2209 break;
2210 }
2211 len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply));
2212 if (len > 0 && len != sizeof(reply))
2213 return -EIO;
2214 if (len < 0)
2215 return len;
2216 return 0;
2217}
2218
f53329f3
JD
2219/* Regenerate the statedump. */
2220int ustctl_regenerate_statedump(int sock, int handle)
2221{
2222 struct ustcomm_ust_msg lum;
2223 struct ustcomm_ust_reply lur;
2224 int ret;
2225
2226 memset(&lum, 0, sizeof(lum));
2227 lum.handle = handle;
2228 lum.cmd = LTTNG_UST_SESSION_STATEDUMP;
2229 ret = ustcomm_send_app_cmd(sock, &lum, &lur);
2230 if (ret)
2231 return ret;
2232 DBG("Regenerated statedump for handle %u", handle);
2233 return 0;
2234}
2235
74d81a6c
MD
2236static __attribute__((constructor))
2237void ustctl_init(void)
2238{
2239 init_usterr();
6f626d28 2240 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
f9364363 2241 lttng_ust_clock_init();
74d81a6c
MD
2242 lttng_ring_buffer_metadata_client_init();
2243 lttng_ring_buffer_client_overwrite_init();
34a91bdb 2244 lttng_ring_buffer_client_overwrite_rt_init();
74d81a6c 2245 lttng_ring_buffer_client_discard_init();
34a91bdb 2246 lttng_ring_buffer_client_discard_rt_init();
03d2d293 2247 lib_ringbuffer_signal_init();
74d81a6c
MD
2248}
2249
2250static __attribute__((destructor))
2251void ustctl_exit(void)
2252{
34a91bdb 2253 lttng_ring_buffer_client_discard_rt_exit();
74d81a6c 2254 lttng_ring_buffer_client_discard_exit();
34a91bdb 2255 lttng_ring_buffer_client_overwrite_rt_exit();
74d81a6c
MD
2256 lttng_ring_buffer_client_overwrite_exit();
2257 lttng_ring_buffer_metadata_client_exit();
57773204 2258}
This page took 0.136731 seconds and 4 git commands to generate.