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