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