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