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