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