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