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