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