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