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