sequence type: use previous field for length if length_name is NULL
[lttng-ust.git] / src / common / ustcomm.c
CommitLineData
67c5b804 1/*
c0c0989a 2 * SPDX-License-Identifier: LGPL-2.1-only
67c5b804 3 *
c0c0989a
MJ
4 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
5 * Copyright (C) 2011-2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
67c5b804
MD
6 */
7
67c5b804 8#include <limits.h>
fb31eb73 9#include <stdint.h>
67c5b804
MD
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <sys/socket.h>
14#include <sys/stat.h>
15#include <sys/types.h>
16#include <sys/un.h>
17#include <unistd.h>
18#include <assert.h>
57773204 19#include <errno.h>
11ba4bcb 20#include <fcntl.h>
67c5b804 21
32ce8569 22#include <lttng/ust-ctl.h>
9d315d6d
MJ
23#include "common/ustcomm.h"
24#include "common/ust-fd.h"
25#include "common/macros.h"
9d315d6d
MJ
26#include "common/dynamic-type.h"
27#include "common/logging.h"
32ce8569 28
36c52fff 29#include "common/events.h"
27b98e6c 30#include "common/compat/pthread.h"
7bc53e94 31
74d81a6c
MD
32#define USTCOMM_MAX_SEND_FDS 4
33
53569322
MD
34static
35ssize_t count_fields_recursive(size_t nr_fields,
3d33ca1d 36 const struct lttng_ust_event_field * const *lttng_fields);
53569322 37static
f69fe5fb 38int serialize_one_field(struct lttng_ust_session *session,
249cffb5 39 struct lttng_ust_ctl_field *fields, size_t *iter_output,
28db0827
MD
40 const struct lttng_ust_event_field *lf,
41 const char **prev_field_name);
218deb69 42static
f69fe5fb 43int serialize_fields(struct lttng_ust_session *session,
249cffb5 44 struct lttng_ust_ctl_field *lttng_ust_ctl_fields,
218deb69 45 size_t *iter_output, size_t nr_lttng_fields,
3d33ca1d 46 const struct lttng_ust_event_field * const *lttng_fields);
53569322 47
67c5b804 48/*
74d81a6c 49 * ustcomm_connect_unix_sock
67c5b804 50 *
74d81a6c 51 * Connect to unix socket using the path name.
6548fca4
MD
52 *
53 * Caller handles FD tracker.
67c5b804 54 */
451d66b2 55int ustcomm_connect_unix_sock(const char *pathname, long timeout)
67c5b804
MD
56{
57 struct sockaddr_un sun;
7bc53e94 58 int fd, ret;
67c5b804 59
204d45df
MD
60 /*
61 * libust threads require the close-on-exec flag for all
62 * resources so it does not leak file descriptors upon exec.
6daf0c26 63 * SOCK_CLOEXEC is not used since it is linux specific.
204d45df 64 */
11ba4bcb 65 fd = socket(PF_UNIX, SOCK_STREAM, 0);
67c5b804 66 if (fd < 0) {
32ce8569 67 PERROR("socket");
7bc53e94 68 ret = -errno;
67c5b804
MD
69 goto error;
70 }
451d66b2
MD
71 if (timeout >= 0) {
72 /* Give at least 10ms. */
73 if (timeout < 10)
74 timeout = 10;
75 ret = ustcomm_setsockopt_snd_timeout(fd, timeout);
76 if (ret < 0) {
77 WARN("Error setting connect socket send timeout");
78 }
79 }
11ba4bcb
MD
80 ret = fcntl(fd, F_SETFD, FD_CLOEXEC);
81 if (ret < 0) {
32ce8569 82 PERROR("fcntl");
7bc53e94 83 ret = -errno;
11ba4bcb
MD
84 goto error_fcntl;
85 }
67c5b804
MD
86
87 memset(&sun, 0, sizeof(sun));
88 sun.sun_family = AF_UNIX;
89 strncpy(sun.sun_path, pathname, sizeof(sun.sun_path));
90 sun.sun_path[sizeof(sun.sun_path) - 1] = '\0';
91
92 ret = connect(fd, (struct sockaddr *) &sun, sizeof(sun));
93 if (ret < 0) {
94 /*
0b9aa170
MD
95 * Don't print message on connect ENOENT error, because
96 * connect is used in normal execution to detect if
97 * sessiond is alive. ENOENT is when the unix socket
98 * file does not exist, and ECONNREFUSED is when the
99 * file exists but no sessiond is listening.
67c5b804 100 */
0b9aa170 101 if (errno != ECONNREFUSED && errno != ECONNRESET
bdd8ca83 102 && errno != ENOENT && errno != EACCES)
8cf811d3 103 PERROR("connect");
7bc53e94 104 ret = -errno;
8cf811d3
MD
105 if (ret == -ECONNREFUSED || ret == -ECONNRESET)
106 ret = -EPIPE;
67c5b804
MD
107 goto error_connect;
108 }
109
110 return fd;
111
112error_connect:
11ba4bcb 113error_fcntl:
7bc53e94
MD
114 {
115 int closeret;
116
117 closeret = close(fd);
118 if (closeret)
32ce8569 119 PERROR("close");
7bc53e94 120 }
67c5b804
MD
121error:
122 return ret;
123}
124
125/*
74d81a6c 126 * ustcomm_accept_unix_sock
67c5b804 127 *
74d81a6c
MD
128 * Do an accept(2) on the sock and return the
129 * new file descriptor. The socket MUST be bind(2) before.
67c5b804 130 */
57773204 131int ustcomm_accept_unix_sock(int sock)
67c5b804
MD
132{
133 int new_fd;
134 struct sockaddr_un sun;
135 socklen_t len = 0;
136
137 /* Blocking call */
138 new_fd = accept(sock, (struct sockaddr *) &sun, &len);
139 if (new_fd < 0) {
b869b5ae
MD
140 if (errno != ECONNABORTED)
141 PERROR("accept");
142 new_fd = -errno;
143 if (new_fd == -ECONNABORTED)
144 new_fd = -EPIPE;
67c5b804 145 }
67c5b804 146 return new_fd;
67c5b804
MD
147}
148
149/*
74d81a6c 150 * ustcomm_create_unix_sock
67c5b804 151 *
74d81a6c
MD
152 * Creates a AF_UNIX local socket using pathname
153 * bind the socket upon creation and return the fd.
67c5b804 154 */
57773204 155int ustcomm_create_unix_sock(const char *pathname)
67c5b804
MD
156{
157 struct sockaddr_un sun;
7bc53e94 158 int fd, ret;
67c5b804
MD
159
160 /* Create server socket */
161 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
32ce8569 162 PERROR("socket");
7bc53e94 163 ret = -errno;
67c5b804
MD
164 goto error;
165 }
166
167 memset(&sun, 0, sizeof(sun));
168 sun.sun_family = AF_UNIX;
169 strncpy(sun.sun_path, pathname, sizeof(sun.sun_path));
170 sun.sun_path[sizeof(sun.sun_path) - 1] = '\0';
171
172 /* Unlink the old file if present */
173 (void) unlink(pathname);
174 ret = bind(fd, (struct sockaddr *) &sun, sizeof(sun));
175 if (ret < 0) {
32ce8569 176 PERROR("bind");
7bc53e94
MD
177 ret = -errno;
178 goto error_close;
67c5b804
MD
179 }
180
181 return fd;
182
7bc53e94
MD
183error_close:
184 {
185 int closeret;
186
187 closeret = close(fd);
188 if (closeret) {
32ce8569 189 PERROR("close");
7bc53e94
MD
190 }
191 }
67c5b804
MD
192error:
193 return ret;
194}
195
196/*
74d81a6c 197 * ustcomm_listen_unix_sock
67c5b804 198 *
74d81a6c 199 * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN.
67c5b804 200 */
57773204 201int ustcomm_listen_unix_sock(int sock)
67c5b804
MD
202{
203 int ret;
204
e41474be 205 ret = listen(sock, LTTNG_UST_COMM_MAX_LISTEN);
67c5b804 206 if (ret < 0) {
7bc53e94 207 ret = -errno;
32ce8569 208 PERROR("listen");
67c5b804
MD
209 }
210
211 return ret;
212}
213
214/*
74d81a6c
MD
215 * ustcomm_close_unix_sock
216 *
217 * Shutdown cleanly a unix socket.
6548fca4
MD
218 *
219 * Handles fd tracker internally.
74d81a6c
MD
220 */
221int ustcomm_close_unix_sock(int sock)
222{
223 int ret;
224
6548fca4 225 lttng_ust_lock_fd_tracker();
74d81a6c 226 ret = close(sock);
6548fca4
MD
227 if (!ret) {
228 lttng_ust_delete_fd_from_tracker(sock);
229 } else {
32ce8569 230 PERROR("close");
74d81a6c
MD
231 ret = -errno;
232 }
6548fca4 233 lttng_ust_unlock_fd_tracker();
74d81a6c
MD
234
235 return ret;
236}
237
238/*
239 * ustcomm_recv_unix_sock
67c5b804 240 *
74d81a6c
MD
241 * Receive data of size len in put that data into
242 * the buf param. Using recvmsg API.
243 * Return the size of received data.
244 * Return 0 on orderly shutdown.
67c5b804 245 */
57773204 246ssize_t ustcomm_recv_unix_sock(int sock, void *buf, size_t len)
67c5b804 247{
913b87f1 248 struct msghdr msg;
67c5b804 249 struct iovec iov[1];
89c5b6ec
MD
250 ssize_t ret = -1;
251 size_t len_last;
67c5b804 252
913b87f1
MD
253 memset(&msg, 0, sizeof(msg));
254
67c5b804
MD
255 iov[0].iov_base = buf;
256 iov[0].iov_len = len;
257 msg.msg_iov = iov;
258 msg.msg_iovlen = 1;
259
7e3cfcbe 260 do {
89c5b6ec 261 len_last = iov[0].iov_len;
7e3cfcbe 262 ret = recvmsg(sock, &msg, 0);
89c5b6ec
MD
263 if (ret > 0) {
264 iov[0].iov_base += ret;
265 iov[0].iov_len -= ret;
266 assert(ret <= len_last);
267 }
268 } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR));
7bc53e94
MD
269
270 if (ret < 0) {
271 int shutret;
272
8cf811d3 273 if (errno != EPIPE && errno != ECONNRESET && errno != ECONNREFUSED)
32ce8569 274 PERROR("recvmsg");
7bc53e94 275 ret = -errno;
8cf811d3 276 if (ret == -ECONNRESET || ret == -ECONNREFUSED)
b869b5ae 277 ret = -EPIPE;
7bc53e94
MD
278
279 shutret = shutdown(sock, SHUT_RDWR);
280 if (shutret)
32ce8569 281 ERR("Socket shutdown error");
89c5b6ec
MD
282 } else if (ret > 0) {
283 ret = len;
67c5b804 284 }
89c5b6ec 285 /* ret = 0 means an orderly shutdown. */
67c5b804
MD
286
287 return ret;
288}
289
290/*
74d81a6c 291 * ustcomm_send_unix_sock
67c5b804 292 *
74d81a6c
MD
293 * Send buf data of size len. Using sendmsg API.
294 * Return the size of sent data.
67c5b804 295 */
32ce8569 296ssize_t ustcomm_send_unix_sock(int sock, const void *buf, size_t len)
67c5b804 297{
913b87f1 298 struct msghdr msg;
67c5b804 299 struct iovec iov[1];
7bc53e94 300 ssize_t ret;
67c5b804 301
913b87f1
MD
302 memset(&msg, 0, sizeof(msg));
303
32ce8569 304 iov[0].iov_base = (void *) buf;
67c5b804
MD
305 iov[0].iov_len = len;
306 msg.msg_iov = iov;
307 msg.msg_iovlen = 1;
308
1ea11eab
MD
309 /*
310 * Using the MSG_NOSIGNAL when sending data from sessiond to
311 * libust, so libust does not receive an unhandled SIGPIPE or
312 * SIGURG. The sessiond receiver side can be made more resilient
313 * by ignoring SIGPIPE, but we don't have this luxury on the
314 * libust side.
315 */
51d9d699
MD
316 do {
317 ret = sendmsg(sock, &msg, MSG_NOSIGNAL);
318 } while (ret < 0 && errno == EINTR);
7bc53e94
MD
319
320 if (ret < 0) {
321 int shutret;
322
74d81a6c 323 if (errno != EPIPE && errno != ECONNRESET)
32ce8569 324 PERROR("sendmsg");
7bc53e94 325 ret = -errno;
b869b5ae
MD
326 if (ret == -ECONNRESET)
327 ret = -EPIPE;
7bc53e94
MD
328
329 shutret = shutdown(sock, SHUT_RDWR);
330 if (shutret)
32ce8569 331 ERR("Socket shutdown error");
67c5b804
MD
332 }
333
334 return ret;
335}
336
337/*
74d81a6c 338 * Send a message accompanied by fd(s) over a unix socket.
67c5b804 339 *
74d81a6c 340 * Returns the size of data sent, or negative error value.
67c5b804 341 */
74d81a6c 342ssize_t ustcomm_send_fds_unix_sock(int sock, int *fds, size_t nb_fd)
67c5b804 343{
913b87f1 344 struct msghdr msg;
67c5b804
MD
345 struct cmsghdr *cmptr;
346 struct iovec iov[1];
347 ssize_t ret = -1;
348 unsigned int sizeof_fds = nb_fd * sizeof(int);
349 char tmp[CMSG_SPACE(sizeof_fds)];
74d81a6c 350 char dummy = 0;
67c5b804 351
913b87f1 352 memset(&msg, 0, sizeof(msg));
74d81a6c 353 memset(tmp, 0, CMSG_SPACE(sizeof_fds) * sizeof(char));
913b87f1 354
74d81a6c
MD
355 if (nb_fd > USTCOMM_MAX_SEND_FDS)
356 return -EINVAL;
67c5b804
MD
357
358 msg.msg_control = (caddr_t)tmp;
359 msg.msg_controllen = CMSG_LEN(sizeof_fds);
360
361 cmptr = CMSG_FIRSTHDR(&msg);
34daae3e
MD
362 if (!cmptr)
363 return -EINVAL;
67c5b804
MD
364 cmptr->cmsg_level = SOL_SOCKET;
365 cmptr->cmsg_type = SCM_RIGHTS;
366 cmptr->cmsg_len = CMSG_LEN(sizeof_fds);
367 memcpy(CMSG_DATA(cmptr), fds, sizeof_fds);
368 /* Sum of the length of all control messages in the buffer: */
369 msg.msg_controllen = cmptr->cmsg_len;
370
74d81a6c
MD
371 iov[0].iov_base = &dummy;
372 iov[0].iov_len = 1;
67c5b804
MD
373 msg.msg_iov = iov;
374 msg.msg_iovlen = 1;
375
51d9d699 376 do {
0dafcd63 377 ret = sendmsg(sock, &msg, MSG_NOSIGNAL);
51d9d699 378 } while (ret < 0 && errno == EINTR);
7bc53e94 379 if (ret < 0) {
74d81a6c
MD
380 /*
381 * We consider EPIPE and ECONNRESET as expected.
382 */
383 if (errno != EPIPE && errno != ECONNRESET) {
32ce8569 384 PERROR("sendmsg");
74d81a6c 385 }
b869b5ae
MD
386 ret = -errno;
387 if (ret == -ECONNRESET)
388 ret = -EPIPE;
74d81a6c
MD
389 }
390 return ret;
391}
7bc53e94 392
74d81a6c
MD
393/*
394 * Recv a message accompanied by fd(s) from a unix socket.
395 *
74d81a6c
MD
396 * Expect at most "nb_fd" file descriptors. Returns the number of fd
397 * actually received in nb_fd.
398 * Returns -EPIPE on orderly shutdown.
399 */
400ssize_t ustcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd)
401{
402 struct iovec iov[1];
403 ssize_t ret = 0;
404 struct cmsghdr *cmsg;
405 size_t sizeof_fds = nb_fd * sizeof(int);
406 char recv_fd[CMSG_SPACE(sizeof_fds)];
407 struct msghdr msg;
408 char dummy;
6daf0c26 409 int i;
7bc53e94 410
74d81a6c 411 memset(&msg, 0, sizeof(msg));
67c5b804 412
74d81a6c
MD
413 /* Prepare to receive the structures */
414 iov[0].iov_base = &dummy;
415 iov[0].iov_len = 1;
416 msg.msg_iov = iov;
417 msg.msg_iovlen = 1;
418 msg.msg_control = recv_fd;
419 msg.msg_controllen = sizeof(recv_fd);
420
421 do {
422 ret = recvmsg(sock, &msg, 0);
423 } while (ret < 0 && errno == EINTR);
424 if (ret < 0) {
425 if (errno != EPIPE && errno != ECONNRESET) {
32ce8569 426 PERROR("recvmsg fds");
74d81a6c 427 }
8cf811d3 428 ret = -errno;
b869b5ae
MD
429 if (ret == -ECONNRESET)
430 ret = -EPIPE;
74d81a6c
MD
431 goto end;
432 }
433 if (ret == 0) {
434 /* orderly shutdown */
435 ret = -EPIPE;
436 goto end;
437 }
438 if (ret != 1) {
32ce8569 439 ERR("Error: Received %zd bytes, expected %d\n",
74d81a6c
MD
440 ret, 1);
441 goto end;
442 }
443 if (msg.msg_flags & MSG_CTRUNC) {
32ce8569 444 ERR("Error: Control message truncated.\n");
74d81a6c
MD
445 ret = -1;
446 goto end;
447 }
448 cmsg = CMSG_FIRSTHDR(&msg);
449 if (!cmsg) {
32ce8569 450 ERR("Error: Invalid control message header\n");
74d81a6c
MD
451 ret = -1;
452 goto end;
453 }
454 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
32ce8569 455 ERR("Didn't received any fd\n");
74d81a6c
MD
456 ret = -1;
457 goto end;
458 }
459 if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) {
32ce8569 460 ERR("Error: Received %zu bytes of ancillary data, expected %zu\n",
74d81a6c
MD
461 (size_t) cmsg->cmsg_len, (size_t) CMSG_LEN(sizeof_fds));
462 ret = -1;
463 goto end;
464 }
6daf0c26 465
74d81a6c 466 memcpy(fds, CMSG_DATA(cmsg), sizeof_fds);
6daf0c26
JR
467
468 /* Set FD_CLOEXEC */
469 for (i = 0; i < nb_fd; i++) {
470 ret = fcntl(fds[i], F_SETFD, FD_CLOEXEC);
471 if (ret < 0) {
472 PERROR("fcntl failed to set FD_CLOEXEC on fd %d",
473 fds[i]);
474 }
475 }
476
6b32a5c3 477 ret = nb_fd;
74d81a6c 478end:
67c5b804
MD
479 return ret;
480}
57773204
MD
481
482int ustcomm_send_app_msg(int sock, struct ustcomm_ust_msg *lum)
483{
484 ssize_t len;
485
486 len = ustcomm_send_unix_sock(sock, lum, sizeof(*lum));
487 switch (len) {
488 case sizeof(*lum):
57773204 489 break;
57773204 490 default:
7bc53e94 491 if (len < 0) {
7bc53e94
MD
492 return len;
493 } else {
32ce8569 494 ERR("incorrect message size: %zd\n", len);
7bc53e94
MD
495 return -EINVAL;
496 }
57773204
MD
497 }
498 return 0;
499}
500
501int ustcomm_recv_app_reply(int sock, struct ustcomm_ust_reply *lur,
502 uint32_t expected_handle, uint32_t expected_cmd)
503{
504 ssize_t len;
505
506 memset(lur, 0, sizeof(*lur));
507 len = ustcomm_recv_unix_sock(sock, lur, sizeof(*lur));
508 switch (len) {
509 case 0: /* orderly shutdown */
74d81a6c 510 return -EPIPE;
57773204 511 case sizeof(*lur):
5dafeeaa
MD
512 {
513 int err = 0;
514
57773204 515 if (lur->handle != expected_handle) {
32ce8569 516 ERR("Unexpected result message handle: "
74d81a6c
MD
517 "expected: %u vs received: %u\n",
518 expected_handle, lur->handle);
5dafeeaa 519 err = 1;
57773204 520 }
57773204 521 if (lur->cmd != expected_cmd) {
32ce8569 522 ERR("Unexpected result message command "
74d81a6c
MD
523 "expected: %u vs received: %u\n",
524 expected_cmd, lur->cmd);
5dafeeaa
MD
525 err = 1;
526 }
527 if (err) {
57773204 528 return -EINVAL;
5dafeeaa
MD
529 } else {
530 return lur->ret_code;
57773204 531 }
5dafeeaa 532 }
57773204 533 default:
8cf811d3 534 if (len >= 0) {
32ce8569 535 ERR("incorrect message size: %zd\n", len);
7bc53e94 536 }
8cf811d3 537 return len;
57773204
MD
538 }
539}
540
541int ustcomm_send_app_cmd(int sock,
542 struct ustcomm_ust_msg *lum,
543 struct ustcomm_ust_reply *lur)
544{
545 int ret;
546
547 ret = ustcomm_send_app_msg(sock, lum);
548 if (ret)
549 return ret;
c354a72c
MD
550 ret = ustcomm_recv_app_reply(sock, lur, lum->handle, lum->cmd);
551 if (ret > 0)
552 return -EIO;
553 return ret;
57773204
MD
554}
555
57773204 556/*
74d81a6c
MD
557 * chan_data is allocated internally if this function returns the
558 * expected var_len.
57773204 559 */
74d81a6c 560ssize_t ustcomm_recv_channel_from_sessiond(int sock,
ff0f5728
MD
561 void **_chan_data, uint64_t var_len,
562 int *_wakeup_fd)
57773204 563{
74d81a6c 564 void *chan_data;
ff0f5728 565 ssize_t len, nr_fd;
f5c453e9 566 int wakeup_fd, ret;
57773204 567
fd17d7ce 568 if (var_len > LTTNG_UST_ABI_CHANNEL_DATA_MAX_LEN) {
74d81a6c
MD
569 len = -EINVAL;
570 goto error_check;
57773204 571 }
74d81a6c
MD
572 /* Receive variable length data */
573 chan_data = zmalloc(var_len);
574 if (!chan_data) {
575 len = -ENOMEM;
576 goto error_alloc;
57773204 577 }
74d81a6c
MD
578 len = ustcomm_recv_unix_sock(sock, chan_data, var_len);
579 if (len != var_len) {
580 goto error_recv;
57773204 581 }
ff0f5728 582 /* recv wakeup fd */
6548fca4 583 lttng_ust_lock_fd_tracker();
ff0f5728
MD
584 nr_fd = ustcomm_recv_fds_unix_sock(sock, &wakeup_fd, 1);
585 if (nr_fd <= 0) {
6548fca4 586 lttng_ust_unlock_fd_tracker();
ff0f5728
MD
587 if (nr_fd < 0) {
588 len = nr_fd;
589 goto error_recv;
590 } else {
591 len = -EIO;
592 goto error_recv;
593 }
594 }
f5c453e9
JR
595
596 ret = lttng_ust_add_fd_to_tracker(wakeup_fd);
597 if (ret < 0) {
f5c453e9
JR
598 ret = close(wakeup_fd);
599 if (ret) {
600 PERROR("close on wakeup_fd");
601 }
602 len = -EIO;
20d1999d 603 lttng_ust_unlock_fd_tracker();
f5c453e9
JR
604 goto error_recv;
605 }
606
607 *_wakeup_fd = ret;
6548fca4 608 lttng_ust_unlock_fd_tracker();
f5c453e9 609
74d81a6c
MD
610 *_chan_data = chan_data;
611 return len;
612
613error_recv:
614 free(chan_data);
615error_alloc:
616error_check:
617 return len;
618}
7bc53e94 619
d8d2416d
FD
620ssize_t ustcomm_recv_event_notifier_notif_fd_from_sessiond(int sock,
621 int *_event_notifier_notif_fd)
622{
623 ssize_t nr_fd;
624 int event_notifier_notif_fd, ret;
625
626 /* Receive event_notifier notification fd */
627 lttng_ust_lock_fd_tracker();
628 nr_fd = ustcomm_recv_fds_unix_sock(sock, &event_notifier_notif_fd, 1);
629 if (nr_fd <= 0) {
630 lttng_ust_unlock_fd_tracker();
631 if (nr_fd < 0) {
632 ret = nr_fd;
633 goto error;
634 } else {
635 ret = -EIO;
636 goto error;
637 }
638 }
639
640 ret = lttng_ust_add_fd_to_tracker(event_notifier_notif_fd);
641 if (ret < 0) {
642 ret = close(event_notifier_notif_fd);
643 if (ret) {
644 PERROR("close on event_notifier notif fd");
645 }
646 ret = -EIO;
647 lttng_ust_unlock_fd_tracker();
648 goto error;
649 }
650
651 *_event_notifier_notif_fd = ret;
652 lttng_ust_unlock_fd_tracker();
653
654 ret = nr_fd;
655
656error:
657 return ret;
658}
659
74d81a6c 660int ustcomm_recv_stream_from_sessiond(int sock,
2208d8b5 661 uint64_t *memory_map_size __attribute__((unused)),
74d81a6c
MD
662 int *shm_fd, int *wakeup_fd)
663{
664 ssize_t len;
665 int ret;
666 int fds[2];
667
668 /* recv shm fd and wakeup fd */
6548fca4 669 lttng_ust_lock_fd_tracker();
74d81a6c
MD
670 len = ustcomm_recv_fds_unix_sock(sock, fds, 2);
671 if (len <= 0) {
6548fca4 672 lttng_ust_unlock_fd_tracker();
74d81a6c
MD
673 if (len < 0) {
674 ret = len;
675 goto error;
676 } else {
677 ret = -EIO;
678 goto error;
679 }
7bc53e94 680 }
f5c453e9
JR
681
682 ret = lttng_ust_add_fd_to_tracker(fds[0]);
683 if (ret < 0) {
f5c453e9
JR
684 ret = close(fds[0]);
685 if (ret) {
686 PERROR("close on received shm_fd");
687 }
688 ret = -EIO;
20d1999d 689 lttng_ust_unlock_fd_tracker();
f5c453e9
JR
690 goto error;
691 }
692 *shm_fd = ret;
693
694 ret = lttng_ust_add_fd_to_tracker(fds[1]);
695 if (ret < 0) {
f5c453e9
JR
696 ret = close(*shm_fd);
697 if (ret) {
698 PERROR("close on shm_fd");
699 }
700 *shm_fd = -1;
701 ret = close(fds[1]);
702 if (ret) {
703 PERROR("close on received wakeup_fd");
704 }
705 ret = -EIO;
20d1999d 706 lttng_ust_unlock_fd_tracker();
f5c453e9
JR
707 goto error;
708 }
709 *wakeup_fd = ret;
6548fca4 710 lttng_ust_unlock_fd_tracker();
74d81a6c
MD
711 return 0;
712
713error:
57773204
MD
714 return ret;
715}
32ce8569 716
ebabbf58
MD
717ssize_t ustcomm_recv_counter_from_sessiond(int sock,
718 void **_counter_data, uint64_t var_len)
719{
720 void *counter_data;
721 ssize_t len;
722
fd17d7ce 723 if (var_len > LTTNG_UST_ABI_COUNTER_DATA_MAX_LEN) {
ebabbf58
MD
724 len = -EINVAL;
725 goto error_check;
726 }
727 /* Receive variable length data */
728 counter_data = zmalloc(var_len);
729 if (!counter_data) {
730 len = -ENOMEM;
731 goto error_alloc;
732 }
733 len = ustcomm_recv_unix_sock(sock, counter_data, var_len);
734 if (len != var_len) {
735 goto error_recv;
736 }
737 *_counter_data = counter_data;
738 return len;
739
740error_recv:
741 free(counter_data);
742error_alloc:
743error_check:
744 return len;
745}
746
747int ustcomm_recv_counter_shm_from_sessiond(int sock,
748 int *shm_fd)
749{
750 ssize_t len;
751 int ret;
752 int fds[1];
753
754 /* recv shm fd fd */
755 lttng_ust_lock_fd_tracker();
756 len = ustcomm_recv_fds_unix_sock(sock, fds, 1);
757 if (len <= 0) {
758 lttng_ust_unlock_fd_tracker();
759 if (len < 0) {
760 ret = len;
761 goto error;
762 } else {
763 ret = -EIO;
764 goto error;
765 }
766 }
767
768 ret = lttng_ust_add_fd_to_tracker(fds[0]);
769 if (ret < 0) {
770 ret = close(fds[0]);
771 if (ret) {
772 PERROR("close on received shm_fd");
773 }
774 ret = -EIO;
775 lttng_ust_unlock_fd_tracker();
776 goto error;
777 }
778 *shm_fd = ret;
779 lttng_ust_unlock_fd_tracker();
780 return 0;
781
782error:
783 return ret;
784}
785
32ce8569
MD
786/*
787 * Returns 0 on success, negative error value on error.
788 */
789int ustcomm_send_reg_msg(int sock,
249cffb5 790 enum lttng_ust_ctl_socket_type type,
32ce8569
MD
791 uint32_t bits_per_long,
792 uint32_t uint8_t_alignment,
793 uint32_t uint16_t_alignment,
794 uint32_t uint32_t_alignment,
795 uint32_t uint64_t_alignment,
796 uint32_t long_alignment)
797{
798 ssize_t len;
249cffb5 799 struct lttng_ust_ctl_reg_msg reg_msg;
32ce8569 800
fd17d7ce 801 reg_msg.magic = LTTNG_UST_ABI_COMM_MAGIC;
32ce8569
MD
802 reg_msg.major = LTTNG_UST_ABI_MAJOR_VERSION;
803 reg_msg.minor = LTTNG_UST_ABI_MINOR_VERSION;
804 reg_msg.pid = getpid();
805 reg_msg.ppid = getppid();
806 reg_msg.uid = getuid();
807 reg_msg.gid = getgid();
808 reg_msg.bits_per_long = bits_per_long;
809 reg_msg.uint8_t_alignment = uint8_t_alignment;
810 reg_msg.uint16_t_alignment = uint16_t_alignment;
811 reg_msg.uint32_t_alignment = uint32_t_alignment;
812 reg_msg.uint64_t_alignment = uint64_t_alignment;
813 reg_msg.long_alignment = long_alignment;
814 reg_msg.socket_type = type;
0db3d6ee 815 lttng_pthread_getname_np(reg_msg.name, LTTNG_UST_ABI_PROCNAME_LEN);
32ce8569
MD
816 memset(reg_msg.padding, 0, sizeof(reg_msg.padding));
817
818 len = ustcomm_send_unix_sock(sock, &reg_msg, sizeof(reg_msg));
819 if (len > 0 && len != sizeof(reg_msg))
820 return -EIO;
821 if (len < 0)
822 return len;
823 return 0;
824}
825
53569322 826static
4e48b5d2 827ssize_t count_one_type(const struct lttng_ust_type_common *lt)
53569322 828{
a084756d
MD
829 switch (lt->type) {
830 case lttng_ust_type_integer:
831 case lttng_ust_type_float:
832 case lttng_ust_type_string:
53569322 833 return 1;
a084756d
MD
834 case lttng_ust_type_enum:
835 return count_one_type(lttng_ust_get_type_enum(lt)->container_type) + 1;
836 case lttng_ust_type_array:
837 return count_one_type(lttng_ust_get_type_array(lt)->elem_type) + 1;
838 case lttng_ust_type_sequence:
839 return count_one_type(lttng_ust_get_type_sequence(lt)->elem_type) + 1;
840 case lttng_ust_type_struct:
841 return count_fields_recursive(lttng_ust_get_type_struct(lt)->nr_fields,
842 lttng_ust_get_type_struct(lt)->fields) + 1;
843
844 case lttng_ust_type_dynamic:
53569322 845 {
3d33ca1d 846 const struct lttng_ust_event_field * const *choices;
53569322
MD
847 size_t nr_choices;
848 int ret;
849
850 ret = lttng_ust_dynamic_type_choices(&nr_choices,
851 &choices);
852 if (ret)
853 return ret;
854 /*
cf22367f 855 * Two fields for enum, one field for variant, and
53569322
MD
856 * one field per choice.
857 */
cf22367f 858 return count_fields_recursive(nr_choices, choices) + 3;
53569322 859 }
218deb69 860
53569322
MD
861 default:
862 return -EINVAL;
863 }
864 return 0;
865}
866
867static
868ssize_t count_fields_recursive(size_t nr_fields,
3d33ca1d 869 const struct lttng_ust_event_field * const *lttng_fields)
53569322
MD
870{
871 int i;
872 ssize_t ret, count = 0;
873
874 for (i = 0; i < nr_fields; i++) {
25cff019 875 const struct lttng_ust_event_field *lf;
53569322 876
25cff019 877 lf = lttng_fields[i];
53569322
MD
878 /* skip 'nowrite' fields */
879 if (lf->nowrite)
880 continue;
a084756d 881 ret = count_one_type(lf->type);
53569322
MD
882 if (ret < 0)
883 return ret; /* error */
884 count += ret;
885 }
886 return count;
887}
888
889static
890ssize_t count_ctx_fields_recursive(size_t nr_fields,
4e48b5d2 891 struct lttng_ust_ctx_field *lttng_fields)
53569322
MD
892{
893 int i;
894 ssize_t ret, count = 0;
895
896 for (i = 0; i < nr_fields; i++) {
25cff019 897 const struct lttng_ust_event_field *lf;
53569322 898
4e48b5d2 899 lf = lttng_fields[i].event_field;
53569322
MD
900 /* skip 'nowrite' fields */
901 if (lf->nowrite)
902 continue;
a084756d 903 ret = count_one_type(lf->type);
53569322
MD
904 if (ret < 0)
905 return ret; /* error */
906 count += ret;
907 }
908 return count;
909}
910
31a85ea9
MD
911static
912int serialize_string_encoding(int32_t *ue,
a084756d 913 enum lttng_ust_string_encoding le)
31a85ea9
MD
914{
915 switch (le) {
a084756d 916 case lttng_ust_string_encoding_none:
249cffb5 917 *ue = lttng_ust_ctl_encode_none;
31a85ea9 918 break;
a084756d 919 case lttng_ust_string_encoding_UTF8:
249cffb5 920 *ue = lttng_ust_ctl_encode_UTF8;
31a85ea9 921 break;
a084756d 922 case lttng_ust_string_encoding_ASCII:
249cffb5 923 *ue = lttng_ust_ctl_encode_ASCII;
31a85ea9
MD
924 break;
925 default:
926 return -EINVAL;
927 }
928 return 0;
929}
930
931static
249cffb5 932int serialize_integer_type(struct lttng_ust_ctl_integer_type *uit,
a084756d
MD
933 const struct lttng_ust_type_integer *lit,
934 enum lttng_ust_string_encoding lencoding)
31a85ea9
MD
935{
936 int32_t encoding;
937
938 uit->size = lit->size;
939 uit->signedness = lit->signedness;
940 uit->reverse_byte_order = lit->reverse_byte_order;
941 uit->base = lit->base;
a084756d 942 if (serialize_string_encoding(&encoding, lencoding))
31a85ea9
MD
943 return -EINVAL;
944 uit->encoding = encoding;
945 uit->alignment = lit->alignment;
946 return 0;
947}
948
32ce8569 949static
f69fe5fb 950int serialize_dynamic_type(struct lttng_ust_session *session,
249cffb5 951 struct lttng_ust_ctl_field *fields, size_t *iter_output,
218deb69 952 const char *field_name)
32ce8569 953{
3d33ca1d 954 const struct lttng_ust_event_field * const *choices;
fd17d7ce 955 char tag_field_name[LTTNG_UST_ABI_SYM_NAME_LEN];
4e48b5d2
MD
956 const struct lttng_ust_type_common *tag_type;
957 const struct lttng_ust_event_field *tag_field_generic;
25cff019 958 struct lttng_ust_event_field tag_field = {
53569322
MD
959 .name = tag_field_name,
960 .nowrite = 0,
961 };
249cffb5 962 struct lttng_ust_ctl_field *uf;
53569322 963 size_t nr_choices, i;
32ce8569
MD
964 int ret;
965
53569322 966 tag_field_generic = lttng_ust_dynamic_type_tag_field();
a084756d 967 tag_type = tag_field_generic->type;
53569322
MD
968
969 /* Serialize enum field. */
fd17d7ce
MD
970 strncpy(tag_field_name, field_name, LTTNG_UST_ABI_SYM_NAME_LEN);
971 tag_field_name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] = '\0';
53569322
MD
972 strncat(tag_field_name,
973 "_tag",
fd17d7ce 974 LTTNG_UST_ABI_SYM_NAME_LEN - strlen(tag_field_name) - 1);
a084756d 975 tag_field.type = tag_type;
53569322 976 ret = serialize_one_field(session, fields, iter_output,
28db0827 977 &tag_field, NULL);
53569322
MD
978 if (ret)
979 return ret;
980
981 /* Serialize variant field. */
982 uf = &fields[*iter_output];
983 ret = lttng_ust_dynamic_type_choices(&nr_choices, &choices);
984 if (ret)
985 return ret;
986
fd17d7ce
MD
987 strncpy(uf->name, field_name, LTTNG_UST_ABI_SYM_NAME_LEN);
988 uf->name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] = '\0';
249cffb5 989 uf->type.atype = lttng_ust_ctl_atype_variant;
218deb69
MD
990 uf->type.u.variant_nestable.nr_choices = nr_choices;
991 strncpy(uf->type.u.variant_nestable.tag_name,
53569322 992 tag_field_name,
fd17d7ce
MD
993 LTTNG_UST_ABI_SYM_NAME_LEN);
994 uf->type.u.variant_nestable.tag_name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] = '\0';
218deb69 995 uf->type.u.variant_nestable.alignment = 0;
53569322
MD
996 (*iter_output)++;
997
998 /* Serialize choice fields after variant. */
999 for (i = 0; i < nr_choices; i++) {
1000 ret = serialize_one_field(session, fields,
28db0827 1001 iter_output, choices[i], NULL);
53569322
MD
1002 if (ret)
1003 return ret;
1004 }
1005 return 0;
1006}
1007
1008static
f69fe5fb 1009int serialize_one_type(struct lttng_ust_session *session,
249cffb5 1010 struct lttng_ust_ctl_field *fields, size_t *iter_output,
4e48b5d2 1011 const char *field_name, const struct lttng_ust_type_common *lt,
28db0827
MD
1012 enum lttng_ust_string_encoding parent_encoding,
1013 const char *prev_field_name)
53569322 1014{
53569322
MD
1015 int ret;
1016
218deb69 1017 /*
249cffb5 1018 * Serializing a type (rather than a field) generates a lttng_ust_ctl_field
218deb69
MD
1019 * entry with 0-length name.
1020 */
53569322 1021
a084756d
MD
1022 switch (lt->type) {
1023 case lttng_ust_type_integer:
31a85ea9 1024 {
249cffb5
MJ
1025 struct lttng_ust_ctl_field *uf = &fields[*iter_output];
1026 struct lttng_ust_ctl_type *ut = &uf->type;
31a85ea9
MD
1027
1028 if (field_name) {
fd17d7ce
MD
1029 strncpy(uf->name, field_name, LTTNG_UST_ABI_SYM_NAME_LEN);
1030 uf->name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] = '\0';
31a85ea9
MD
1031 } else {
1032 uf->name[0] = '\0';
1033 }
a084756d
MD
1034 ret = serialize_integer_type(&ut->u.integer, lttng_ust_get_type_integer(lt),
1035 parent_encoding);
31a85ea9
MD
1036 if (ret)
1037 return ret;
249cffb5 1038 ut->atype = lttng_ust_ctl_atype_integer;
31a85ea9
MD
1039 (*iter_output)++;
1040 break;
1041 }
a084756d 1042 case lttng_ust_type_float:
31a85ea9 1043 {
249cffb5
MJ
1044 struct lttng_ust_ctl_field *uf = &fields[*iter_output];
1045 struct lttng_ust_ctl_type *ut = &uf->type;
1046 struct lttng_ust_ctl_float_type *uft;
4e48b5d2 1047 const struct lttng_ust_type_float *lft;
31a85ea9
MD
1048
1049 if (field_name) {
fd17d7ce
MD
1050 strncpy(uf->name, field_name, LTTNG_UST_ABI_SYM_NAME_LEN);
1051 uf->name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] = '\0';
31a85ea9
MD
1052 } else {
1053 uf->name[0] = '\0';
1054 }
1055 uft = &ut->u._float;
a084756d 1056 lft = lttng_ust_get_type_float(lt);
31a85ea9
MD
1057 uft->exp_dig = lft->exp_dig;
1058 uft->mant_dig = lft->mant_dig;
1059 uft->alignment = lft->alignment;
1060 uft->reverse_byte_order = lft->reverse_byte_order;
249cffb5 1061 ut->atype = lttng_ust_ctl_atype_float;
31a85ea9
MD
1062 (*iter_output)++;
1063 break;
1064 }
a084756d 1065 case lttng_ust_type_string:
31a85ea9 1066 {
249cffb5
MJ
1067 struct lttng_ust_ctl_field *uf = &fields[*iter_output];
1068 struct lttng_ust_ctl_type *ut = &uf->type;
31a85ea9
MD
1069 int32_t encoding;
1070
1071 if (field_name) {
fd17d7ce
MD
1072 strncpy(uf->name, field_name, LTTNG_UST_ABI_SYM_NAME_LEN);
1073 uf->name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] = '\0';
31a85ea9
MD
1074 } else {
1075 uf->name[0] = '\0';
1076 }
a084756d 1077 ret = serialize_string_encoding(&encoding, lttng_ust_get_type_string(lt)->encoding);
31a85ea9
MD
1078 if (ret)
1079 return ret;
1080 ut->u.string.encoding = encoding;
249cffb5 1081 ut->atype = lttng_ust_ctl_atype_string;
31a85ea9
MD
1082 (*iter_output)++;
1083 break;
1084 }
a084756d 1085 case lttng_ust_type_array:
218deb69 1086 {
249cffb5
MJ
1087 struct lttng_ust_ctl_field *uf = &fields[*iter_output];
1088 struct lttng_ust_ctl_type *ut = &uf->type;
218deb69
MD
1089
1090 if (field_name) {
fd17d7ce
MD
1091 strncpy(uf->name, field_name, LTTNG_UST_ABI_SYM_NAME_LEN);
1092 uf->name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] = '\0';
218deb69
MD
1093 } else {
1094 uf->name[0] = '\0';
1095 }
249cffb5 1096 ut->atype = lttng_ust_ctl_atype_array_nestable;
a084756d
MD
1097 ut->u.array_nestable.length = lttng_ust_get_type_array(lt)->length;
1098 ut->u.array_nestable.alignment = lttng_ust_get_type_array(lt)->alignment;
218deb69
MD
1099 (*iter_output)++;
1100
1101 ret = serialize_one_type(session, fields, iter_output, NULL,
a084756d 1102 lttng_ust_get_type_array(lt)->elem_type,
28db0827 1103 lttng_ust_get_type_array(lt)->encoding, NULL);
218deb69
MD
1104 if (ret)
1105 return -EINVAL;
1106 break;
1107 }
a084756d 1108 case lttng_ust_type_sequence:
218deb69 1109 {
249cffb5
MJ
1110 struct lttng_ust_ctl_field *uf = &fields[*iter_output];
1111 struct lttng_ust_ctl_type *ut = &uf->type;
28db0827 1112 const char *length_name = lttng_ust_get_type_sequence(lt)->length_name;
218deb69
MD
1113
1114 if (field_name) {
fd17d7ce
MD
1115 strncpy(uf->name, field_name, LTTNG_UST_ABI_SYM_NAME_LEN);
1116 uf->name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] = '\0';
218deb69
MD
1117 } else {
1118 uf->name[0] = '\0';
1119 }
249cffb5 1120 ut->atype = lttng_ust_ctl_atype_sequence_nestable;
28db0827
MD
1121 /*
1122 * If length_name field is NULL, use the previous field
1123 * as length.
1124 */
1125 if (!length_name)
1126 length_name = prev_field_name;
1127 if (!length_name)
1128 return -EINVAL;
218deb69 1129 strncpy(ut->u.sequence_nestable.length_name,
28db0827 1130 length_name, LTTNG_UST_ABI_SYM_NAME_LEN);
fd17d7ce 1131 ut->u.sequence_nestable.length_name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] = '\0';
a084756d 1132 ut->u.sequence_nestable.alignment = lttng_ust_get_type_sequence(lt)->alignment;
218deb69
MD
1133 (*iter_output)++;
1134
1135 ret = serialize_one_type(session, fields, iter_output, NULL,
a084756d 1136 lttng_ust_get_type_sequence(lt)->elem_type,
28db0827 1137 lttng_ust_get_type_sequence(lt)->encoding, NULL);
218deb69
MD
1138 if (ret)
1139 return -EINVAL;
1140 break;
1141 }
a084756d 1142 case lttng_ust_type_dynamic:
53569322 1143 {
218deb69
MD
1144 ret = serialize_dynamic_type(session, fields, iter_output,
1145 field_name);
53569322
MD
1146 if (ret)
1147 return -EINVAL;
1148 break;
1149 }
a084756d 1150 case lttng_ust_type_struct:
218deb69 1151 {
249cffb5 1152 struct lttng_ust_ctl_field *uf = &fields[*iter_output];
218deb69
MD
1153
1154 if (field_name) {
fd17d7ce
MD
1155 strncpy(uf->name, field_name, LTTNG_UST_ABI_SYM_NAME_LEN);
1156 uf->name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] = '\0';
218deb69
MD
1157 } else {
1158 uf->name[0] = '\0';
1159 }
249cffb5 1160 uf->type.atype = lttng_ust_ctl_atype_struct_nestable;
a084756d
MD
1161 uf->type.u.struct_nestable.nr_fields = lttng_ust_get_type_struct(lt)->nr_fields;
1162 uf->type.u.struct_nestable.alignment = lttng_ust_get_type_struct(lt)->alignment;
218deb69
MD
1163 (*iter_output)++;
1164
1165 ret = serialize_fields(session, fields, iter_output,
a084756d
MD
1166 lttng_ust_get_type_struct(lt)->nr_fields,
1167 lttng_ust_get_type_struct(lt)->fields);
218deb69
MD
1168 if (ret)
1169 return -EINVAL;
1170 break;
1171 }
a084756d 1172 case lttng_ust_type_enum:
218deb69 1173 {
249cffb5
MJ
1174 struct lttng_ust_ctl_field *uf = &fields[*iter_output];
1175 struct lttng_ust_ctl_type *ut = &uf->type;
218deb69
MD
1176
1177 if (field_name) {
fd17d7ce
MD
1178 strncpy(uf->name, field_name, LTTNG_UST_ABI_SYM_NAME_LEN);
1179 uf->name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] = '\0';
218deb69
MD
1180 } else {
1181 uf->name[0] = '\0';
1182 }
a084756d 1183 strncpy(ut->u.enum_nestable.name, lttng_ust_get_type_enum(lt)->desc->name,
fd17d7ce
MD
1184 LTTNG_UST_ABI_SYM_NAME_LEN);
1185 ut->u.enum_nestable.name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] = '\0';
249cffb5 1186 ut->atype = lttng_ust_ctl_atype_enum_nestable;
218deb69
MD
1187 (*iter_output)++;
1188
1189 ret = serialize_one_type(session, fields, iter_output, NULL,
a084756d 1190 lttng_ust_get_type_enum(lt)->container_type,
28db0827 1191 lttng_ust_string_encoding_none, NULL);
218deb69
MD
1192 if (ret)
1193 return -EINVAL;
1194 if (session) {
1195 const struct lttng_enum *_enum;
1196
a084756d 1197 _enum = lttng_ust_enum_get_from_desc(session, lttng_ust_get_type_enum(lt)->desc);
218deb69
MD
1198 if (!_enum)
1199 return -EINVAL;
1200 ut->u.enum_nestable.id = _enum->id;
1201 } else {
1202 ut->u.enum_nestable.id = -1ULL;
1203 }
32ce8569
MD
1204 break;
1205 }
32ce8569
MD
1206 default:
1207 return -EINVAL;
1208 }
1209 return 0;
1210}
1211
218deb69 1212static
f69fe5fb 1213int serialize_one_field(struct lttng_ust_session *session,
249cffb5 1214 struct lttng_ust_ctl_field *fields, size_t *iter_output,
28db0827
MD
1215 const struct lttng_ust_event_field *lf,
1216 const char **prev_field_name_p)
218deb69 1217{
28db0827
MD
1218 const char *prev_field_name = NULL;
1219 int ret;
1220
218deb69
MD
1221 /* skip 'nowrite' fields */
1222 if (lf->nowrite)
1223 return 0;
1224
28db0827
MD
1225 if (prev_field_name_p)
1226 prev_field_name = *prev_field_name_p;
1227 ret = serialize_one_type(session, fields, iter_output, lf->name, lf->type,
1228 lttng_ust_string_encoding_none, prev_field_name);
1229 if (prev_field_name_p)
1230 *prev_field_name_p = lf->name;
1231 return ret;
218deb69
MD
1232}
1233
32ce8569 1234static
f69fe5fb 1235int serialize_fields(struct lttng_ust_session *session,
249cffb5 1236 struct lttng_ust_ctl_field *lttng_ust_ctl_fields,
218deb69 1237 size_t *iter_output, size_t nr_lttng_fields,
3d33ca1d 1238 const struct lttng_ust_event_field * const *lttng_fields)
218deb69 1239{
28db0827 1240 const char *prev_field_name = NULL;
218deb69
MD
1241 int ret;
1242 size_t i;
1243
1244 for (i = 0; i < nr_lttng_fields; i++) {
249cffb5 1245 ret = serialize_one_field(session, lttng_ust_ctl_fields,
28db0827
MD
1246 iter_output, lttng_fields[i],
1247 &prev_field_name);
218deb69
MD
1248 if (ret)
1249 return ret;
1250 }
1251 return 0;
1252}
1253
1254static
f69fe5fb 1255int alloc_serialize_fields(struct lttng_ust_session *session,
c785c634 1256 size_t *_nr_write_fields,
249cffb5 1257 struct lttng_ust_ctl_field **lttng_ust_ctl_fields,
32ce8569 1258 size_t nr_fields,
3d33ca1d 1259 const struct lttng_ust_event_field * const *lttng_fields)
32ce8569 1260{
249cffb5 1261 struct lttng_ust_ctl_field *fields;
53569322 1262 int ret;
218deb69 1263 size_t iter_output = 0;
53569322
MD
1264 ssize_t nr_write_fields;
1265
1266 nr_write_fields = count_fields_recursive(nr_fields, lttng_fields);
1267 if (nr_write_fields < 0) {
1268 return (int) nr_write_fields;
1269 }
32ce8569 1270
53569322 1271 fields = zmalloc(nr_write_fields * sizeof(*fields));
32ce8569
MD
1272 if (!fields)
1273 return -ENOMEM;
1274
218deb69
MD
1275 ret = serialize_fields(session, fields, &iter_output, nr_fields,
1276 lttng_fields);
1277 if (ret)
1278 goto error_type;
32ce8569
MD
1279
1280 *_nr_write_fields = nr_write_fields;
249cffb5 1281 *lttng_ust_ctl_fields = fields;
32ce8569
MD
1282 return 0;
1283
1284error_type:
1285 free(fields);
1286 return ret;
1287}
1288
c785c634 1289static
249cffb5 1290int serialize_entries(struct lttng_ust_ctl_enum_entry **_entries,
c785c634 1291 size_t nr_entries,
3d33ca1d 1292 const struct lttng_ust_enum_entry * const *lttng_entries)
c785c634 1293{
249cffb5 1294 struct lttng_ust_ctl_enum_entry *entries;
c785c634
MD
1295 int i;
1296
1297 /* Serialize the entries */
1298 entries = zmalloc(nr_entries * sizeof(*entries));
1299 if (!entries)
1300 return -ENOMEM;
1301 for (i = 0; i < nr_entries; i++) {
249cffb5 1302 struct lttng_ust_ctl_enum_entry *uentry;
891d6b55 1303 const struct lttng_ust_enum_entry *lentry;
c785c634
MD
1304
1305 uentry = &entries[i];
891d6b55 1306 lentry = lttng_entries[i];
c785c634 1307
a6f80644
MD
1308 uentry->start.value = lentry->start.value;
1309 uentry->start.signedness = lentry->start.signedness;
1310 uentry->end.value = lentry->end.value;
1311 uentry->end.signedness = lentry->end.signedness;
fd17d7ce
MD
1312 strncpy(uentry->string, lentry->string, LTTNG_UST_ABI_SYM_NAME_LEN);
1313 uentry->string[LTTNG_UST_ABI_SYM_NAME_LEN - 1] = '\0';
3e762260 1314
1a37a873 1315 if (lentry->options & LTTNG_UST_ENUM_ENTRY_OPTION_IS_AUTO) {
3e762260 1316 uentry->u.extra.options |=
249cffb5 1317 LTTNG_UST_CTL_UST_ENUM_ENTRY_OPTION_IS_AUTO;
3e762260 1318 }
c785c634
MD
1319 }
1320 *_entries = entries;
1321 return 0;
1322}
1323
83e43212 1324static
f69fe5fb 1325int serialize_ctx_fields(struct lttng_ust_session *session,
53569322 1326 size_t *_nr_write_fields,
249cffb5 1327 struct lttng_ust_ctl_field **lttng_ust_ctl_fields,
83e43212 1328 size_t nr_fields,
4e48b5d2 1329 struct lttng_ust_ctx_field *lttng_fields)
83e43212 1330{
249cffb5 1331 struct lttng_ust_ctl_field *fields;
28db0827 1332 const char *prev_field_name = NULL;
53569322
MD
1333 size_t i, iter_output = 0;
1334 ssize_t nr_write_fields;
28db0827 1335 int ret;
83e43212 1336
53569322
MD
1337 nr_write_fields = count_ctx_fields_recursive(nr_fields,
1338 lttng_fields);
1339 if (nr_write_fields < 0) {
1340 return (int) nr_write_fields;
1341 }
1342
1343 fields = zmalloc(nr_write_fields * sizeof(*fields));
83e43212
MD
1344 if (!fields)
1345 return -ENOMEM;
1346
1347 for (i = 0; i < nr_fields; i++) {
53569322 1348 ret = serialize_one_field(session, fields, &iter_output,
28db0827 1349 lttng_fields[i].event_field, &prev_field_name);
83e43212
MD
1350 if (ret)
1351 goto error_type;
83e43212
MD
1352 }
1353
1354 *_nr_write_fields = nr_write_fields;
249cffb5 1355 *lttng_ust_ctl_fields = fields;
83e43212
MD
1356 return 0;
1357
1358error_type:
1359 free(fields);
1360 return ret;
1361}
1362
32ce8569
MD
1363/*
1364 * Returns 0 on success, negative error value on error.
1365 */
1366int ustcomm_register_event(int sock,
f69fe5fb 1367 struct lttng_ust_session *session,
32ce8569
MD
1368 int session_objd, /* session descriptor */
1369 int channel_objd, /* channel descriptor */
1370 const char *event_name, /* event name (input) */
1371 int loglevel,
1372 const char *signature, /* event signature (input) */
1373 size_t nr_fields, /* fields */
3d33ca1d 1374 const struct lttng_ust_event_field * const *lttng_fields,
32ce8569
MD
1375 const char *model_emf_uri,
1376 uint32_t *id) /* event id (output) */
1377{
1378 ssize_t len;
1379 struct {
1380 struct ustcomm_notify_hdr header;
1381 struct ustcomm_notify_event_msg m;
1382 } msg;
1383 struct {
1384 struct ustcomm_notify_hdr header;
1385 struct ustcomm_notify_event_reply r;
1386 } reply;
1387 size_t signature_len, fields_len, model_emf_uri_len;
249cffb5 1388 struct lttng_ust_ctl_field *fields = NULL;
32ce8569
MD
1389 size_t nr_write_fields = 0;
1390 int ret;
1391
1392 memset(&msg, 0, sizeof(msg));
249cffb5 1393 msg.header.notify_cmd = LTTNG_UST_CTL_NOTIFY_CMD_EVENT;
32ce8569
MD
1394 msg.m.session_objd = session_objd;
1395 msg.m.channel_objd = channel_objd;
fd17d7ce
MD
1396 strncpy(msg.m.event_name, event_name, LTTNG_UST_ABI_SYM_NAME_LEN);
1397 msg.m.event_name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] = '\0';
32ce8569
MD
1398 msg.m.loglevel = loglevel;
1399 signature_len = strlen(signature) + 1;
1400 msg.m.signature_len = signature_len;
1401
1402 /* Calculate fields len, serialize fields. */
1403 if (nr_fields > 0) {
218deb69 1404 ret = alloc_serialize_fields(session, &nr_write_fields, &fields,
32ce8569
MD
1405 nr_fields, lttng_fields);
1406 if (ret)
1407 return ret;
1408 }
1409
1410 fields_len = sizeof(*fields) * nr_write_fields;
1411 msg.m.fields_len = fields_len;
1412 if (model_emf_uri) {
1413 model_emf_uri_len = strlen(model_emf_uri) + 1;
1414 } else {
1415 model_emf_uri_len = 0;
1416 }
1417 msg.m.model_emf_uri_len = model_emf_uri_len;
c785c634 1418
32ce8569
MD
1419 len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg));
1420 if (len > 0 && len != sizeof(msg)) {
c785c634
MD
1421 ret = -EIO;
1422 goto error_fields;
32ce8569
MD
1423 }
1424 if (len < 0) {
c785c634
MD
1425 ret = len;
1426 goto error_fields;
32ce8569
MD
1427 }
1428
1429 /* send signature */
1430 len = ustcomm_send_unix_sock(sock, signature, signature_len);
1431 if (len > 0 && len != signature_len) {
6b95617c
MD
1432 ret = -EIO;
1433 goto error_fields;
32ce8569
MD
1434 }
1435 if (len < 0) {
6b95617c
MD
1436 ret = len;
1437 goto error_fields;
32ce8569
MD
1438 }
1439
1440 /* send fields */
1441 if (fields_len > 0) {
1442 len = ustcomm_send_unix_sock(sock, fields, fields_len);
32ce8569 1443 if (len > 0 && len != fields_len) {
c785c634
MD
1444 ret = -EIO;
1445 goto error_fields;
32ce8569
MD
1446 }
1447 if (len < 0) {
c785c634
MD
1448 ret = len;
1449 goto error_fields;
32ce8569
MD
1450 }
1451 }
6b95617c 1452 free(fields);
32ce8569
MD
1453
1454 if (model_emf_uri_len) {
1455 /* send model_emf_uri */
1456 len = ustcomm_send_unix_sock(sock, model_emf_uri,
1457 model_emf_uri_len);
c785c634 1458 if (len > 0 && len != model_emf_uri_len) {
6b95617c 1459 return -EIO;
c785c634
MD
1460 }
1461 if (len < 0) {
6b95617c 1462 return len;
c785c634 1463 }
32ce8569
MD
1464 }
1465
1466 /* receive reply */
1467 len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply));
1468 switch (len) {
1469 case 0: /* orderly shutdown */
1470 return -EPIPE;
1471 case sizeof(reply):
1472 if (reply.header.notify_cmd != msg.header.notify_cmd) {
1473 ERR("Unexpected result message command "
1474 "expected: %u vs received: %u\n",
1475 msg.header.notify_cmd, reply.header.notify_cmd);
1476 return -EINVAL;
1477 }
1478 if (reply.r.ret_code > 0)
1479 return -EINVAL;
1480 if (reply.r.ret_code < 0)
1481 return reply.r.ret_code;
1482 *id = reply.r.event_id;
1483 DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n",
1484 event_name, reply.r.ret_code, reply.r.event_id);
1485 return 0;
1486 default:
1487 if (len < 0) {
1488 /* Transport level error */
1489 if (errno == EPIPE || errno == ECONNRESET)
1490 len = -errno;
1491 return len;
1492 } else {
1493 ERR("incorrect message size: %zd\n", len);
1494 return len;
1495 }
1496 }
6b95617c 1497 /* Unreached. */
c785c634 1498
6b95617c 1499 /* Error path only. */
c785c634
MD
1500error_fields:
1501 free(fields);
1502 return ret;
1503}
1504
1505/*
1506 * Returns 0 on success, negative error value on error.
1507 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1508 */
1509int ustcomm_register_enum(int sock,
1510 int session_objd, /* session descriptor */
1511 const char *enum_name, /* enum name (input) */
1512 size_t nr_entries, /* entries */
3d33ca1d 1513 const struct lttng_ust_enum_entry * const *lttng_entries,
c785c634
MD
1514 uint64_t *id)
1515{
1516 ssize_t len;
1517 struct {
1518 struct ustcomm_notify_hdr header;
1519 struct ustcomm_notify_enum_msg m;
1520 } msg;
1521 struct {
1522 struct ustcomm_notify_hdr header;
1523 struct ustcomm_notify_enum_reply r;
1524 } reply;
1525 size_t entries_len;
249cffb5 1526 struct lttng_ust_ctl_enum_entry *entries = NULL;
c785c634
MD
1527 int ret;
1528
1529 memset(&msg, 0, sizeof(msg));
249cffb5 1530 msg.header.notify_cmd = LTTNG_UST_CTL_NOTIFY_CMD_ENUM;
c785c634 1531 msg.m.session_objd = session_objd;
fd17d7ce
MD
1532 strncpy(msg.m.enum_name, enum_name, LTTNG_UST_ABI_SYM_NAME_LEN);
1533 msg.m.enum_name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] = '\0';
c785c634
MD
1534
1535 /* Calculate entries len, serialize entries. */
1536 if (nr_entries > 0) {
1537 ret = serialize_entries(&entries,
1538 nr_entries, lttng_entries);
1539 if (ret)
1540 return ret;
1541 }
1542
1543 entries_len = sizeof(*entries) * nr_entries;
1544 msg.m.entries_len = entries_len;
1545
1546 len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg));
1547 if (len > 0 && len != sizeof(msg)) {
1548 ret = -EIO;
1549 goto error_entries;
1550 }
1551 if (len < 0) {
1552 ret = len;
1553 goto error_entries;
1554 }
1555
1556 /* send entries */
1557 if (entries_len > 0) {
1558 len = ustcomm_send_unix_sock(sock, entries, entries_len);
1559 if (len > 0 && len != entries_len) {
1560 ret = -EIO;
1561 goto error_entries;
1562 }
1563 if (len < 0) {
1564 ret = len;
1565 goto error_entries;
1566 }
1567 }
1568 free(entries);
1569 entries = NULL;
1570
1571 /* receive reply */
1572 len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply));
1573 switch (len) {
1574 case 0: /* orderly shutdown */
1575 return -EPIPE;
1576 case sizeof(reply):
1577 if (reply.header.notify_cmd != msg.header.notify_cmd) {
1578 ERR("Unexpected result message command "
1579 "expected: %u vs received: %u\n",
1580 msg.header.notify_cmd, reply.header.notify_cmd);
1581 return -EINVAL;
1582 }
1583 if (reply.r.ret_code > 0)
1584 return -EINVAL;
1585 if (reply.r.ret_code < 0)
1586 return reply.r.ret_code;
1587 *id = reply.r.enum_id;
1588 DBG("Sent register enum notification for name \"%s\": ret_code %d\n",
1589 enum_name, reply.r.ret_code);
1590 return 0;
1591 default:
1592 if (len < 0) {
1593 /* Transport level error */
1594 if (errno == EPIPE || errno == ECONNRESET)
1595 len = -errno;
1596 return len;
1597 } else {
1598 ERR("incorrect message size: %zd\n", len);
1599 return len;
1600 }
1601 }
1602 return ret;
1603
1604error_entries:
1605 free(entries);
1606 return ret;
32ce8569
MD
1607}
1608
1609/*
1610 * Returns 0 on success, negative error value on error.
1611 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1612 */
1613int ustcomm_register_channel(int sock,
f69fe5fb 1614 struct lttng_ust_session *session,
32ce8569
MD
1615 int session_objd, /* session descriptor */
1616 int channel_objd, /* channel descriptor */
1617 size_t nr_ctx_fields,
4e48b5d2 1618 struct lttng_ust_ctx_field *ctx_fields,
32ce8569
MD
1619 uint32_t *chan_id, /* channel id (output) */
1620 int *header_type) /* header type (output) */
1621{
1622 ssize_t len;
1623 struct {
1624 struct ustcomm_notify_hdr header;
1625 struct ustcomm_notify_channel_msg m;
1626 } msg;
1627 struct {
1628 struct ustcomm_notify_hdr header;
1629 struct ustcomm_notify_channel_reply r;
1630 } reply;
1631 size_t fields_len;
249cffb5 1632 struct lttng_ust_ctl_field *fields = NULL;
32ce8569
MD
1633 int ret;
1634 size_t nr_write_fields = 0;
1635
1636 memset(&msg, 0, sizeof(msg));
249cffb5 1637 msg.header.notify_cmd = LTTNG_UST_CTL_NOTIFY_CMD_CHANNEL;
32ce8569
MD
1638 msg.m.session_objd = session_objd;
1639 msg.m.channel_objd = channel_objd;
1640
1641 /* Calculate fields len, serialize fields. */
1642 if (nr_ctx_fields > 0) {
53569322 1643 ret = serialize_ctx_fields(session, &nr_write_fields, &fields,
32ce8569
MD
1644 nr_ctx_fields, ctx_fields);
1645 if (ret)
1646 return ret;
1647 }
1648
1649 fields_len = sizeof(*fields) * nr_write_fields;
1650 msg.m.ctx_fields_len = fields_len;
1651 len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg));
1652 if (len > 0 && len != sizeof(msg)) {
1653 free(fields);
1654 return -EIO;
1655 }
1656 if (len < 0) {
1657 free(fields);
1658 return len;
1659 }
1660
1661 /* send fields */
1662 if (fields_len > 0) {
1663 len = ustcomm_send_unix_sock(sock, fields, fields_len);
1664 free(fields);
1665 if (len > 0 && len != fields_len) {
1666 return -EIO;
1667 }
1668 if (len < 0) {
1669 return len;
1670 }
17ea789c
MD
1671 } else {
1672 free(fields);
32ce8569
MD
1673 }
1674
1675 len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply));
1676 switch (len) {
1677 case 0: /* orderly shutdown */
1678 return -EPIPE;
1679 case sizeof(reply):
1680 if (reply.header.notify_cmd != msg.header.notify_cmd) {
1681 ERR("Unexpected result message command "
1682 "expected: %u vs received: %u\n",
1683 msg.header.notify_cmd, reply.header.notify_cmd);
1684 return -EINVAL;
1685 }
1686 if (reply.r.ret_code > 0)
1687 return -EINVAL;
1688 if (reply.r.ret_code < 0)
1689 return reply.r.ret_code;
1690 *chan_id = reply.r.chan_id;
1691 switch (reply.r.header_type) {
1692 case 1:
1693 case 2:
1694 *header_type = reply.r.header_type;
1695 break;
1696 default:
1697 ERR("Unexpected channel header type %u\n",
1698 reply.r.header_type);
1699 return -EINVAL;
1700 }
1701 DBG("Sent register channel notification: chan_id %d, header_type %d\n",
1702 reply.r.chan_id, reply.r.header_type);
1703 return 0;
1704 default:
1705 if (len < 0) {
1706 /* Transport level error */
1707 if (errno == EPIPE || errno == ECONNRESET)
1708 len = -errno;
1709 return len;
1710 } else {
1711 ERR("incorrect message size: %zd\n", len);
1712 return len;
1713 }
1714 }
1715}
ff517991
MD
1716
1717/*
1718 * Set socket reciving timeout.
1719 */
1720int ustcomm_setsockopt_rcv_timeout(int sock, unsigned int msec)
1721{
1722 int ret;
1723 struct timeval tv;
1724
1725 tv.tv_sec = msec / 1000;
1726 tv.tv_usec = (msec * 1000 % 1000000);
1727
1728 ret = setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
1729 if (ret < 0) {
1730 PERROR("setsockopt SO_RCVTIMEO");
1731 ret = -errno;
1732 }
1733
1734 return ret;
1735}
1736
1737/*
1738 * Set socket sending timeout.
1739 */
1740int ustcomm_setsockopt_snd_timeout(int sock, unsigned int msec)
1741{
1742 int ret;
1743 struct timeval tv;
1744
1745 tv.tv_sec = msec / 1000;
1746 tv.tv_usec = (msec * 1000) % 1000000;
1747
1748 ret = setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
1749 if (ret < 0) {
1750 PERROR("setsockopt SO_SNDTIMEO");
1751 ret = -errno;
1752 }
1753
1754 return ret;
1755}
This page took 0.119963 seconds and 4 git commands to generate.