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