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