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