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