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