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