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