Add ust fd mutex nesting getter
[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 */
112 fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
113 if (fd < 0) {
114 PERROR("socket");
115 ret = -errno;
116 goto error;
117 }
118 if (timeout >= 0) {
119 /* Give at least 10ms. */
120 if (timeout < 10)
121 timeout = 10;
122 ret = ustcomm_setsockopt_snd_timeout(fd, timeout);
123 if (ret < 0) {
124 WARN("Error setting connect socket send timeout");
125 }
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 {
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
450 memset(&msg, 0, sizeof(msg));
451
452 /* Prepare to receive the structures */
453 iov[0].iov_base = &dummy;
454 iov[0].iov_len = 1;
455 msg.msg_iov = iov;
456 msg.msg_iovlen = 1;
457 msg.msg_control = recv_fd;
458 msg.msg_controllen = sizeof(recv_fd);
459
460 do {
461 ret = recvmsg(sock, &msg, MSG_CMSG_CLOEXEC);
462 } while (ret < 0 && errno == EINTR);
463 if (ret < 0) {
464 if (errno != EPIPE && errno != ECONNRESET) {
465 PERROR("recvmsg fds");
466 }
467 ret = -errno;
468 if (ret == -ECONNRESET)
469 ret = -EPIPE;
470 goto end;
471 }
472 if (ret == 0) {
473 /* orderly shutdown */
474 ret = -EPIPE;
475 goto end;
476 }
477 if (ret != 1) {
478 ERR("Error: Received %zd bytes, expected %d\n",
479 ret, 1);
480 goto end;
481 }
482 if (msg.msg_flags & MSG_CTRUNC) {
483 ERR("Error: Control message truncated.\n");
484 ret = -1;
485 goto end;
486 }
487 cmsg = CMSG_FIRSTHDR(&msg);
488 if (!cmsg) {
489 ERR("Error: Invalid control message header\n");
490 ret = -1;
491 goto end;
492 }
493 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
494 ERR("Didn't received any fd\n");
495 ret = -1;
496 goto end;
497 }
498 if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) {
499 ERR("Error: Received %zu bytes of ancillary data, expected %zu\n",
500 (size_t) cmsg->cmsg_len, (size_t) CMSG_LEN(sizeof_fds));
501 ret = -1;
502 goto end;
503 }
504
505 memcpy(fds, CMSG_DATA(cmsg), sizeof_fds);
506
507 ret = nb_fd;
508 end:
509 return ret;
510 }
511
512 int ustcomm_send_app_msg(int sock, struct ustcomm_ust_msg *lum)
513 {
514 ssize_t len;
515
516 len = ustcomm_send_unix_sock(sock, lum, sizeof(*lum));
517 switch (len) {
518 case sizeof(*lum):
519 break;
520 default:
521 if (len < 0) {
522 return len;
523 } else {
524 ERR("incorrect message size: %zd\n", len);
525 return -EINVAL;
526 }
527 }
528 return 0;
529 }
530
531 int ustcomm_recv_app_reply(int sock, struct ustcomm_ust_reply *lur,
532 uint32_t expected_handle, uint32_t expected_cmd)
533 {
534 ssize_t len;
535
536 memset(lur, 0, sizeof(*lur));
537 len = ustcomm_recv_unix_sock(sock, lur, sizeof(*lur));
538 switch (len) {
539 case 0: /* orderly shutdown */
540 return -EPIPE;
541 case sizeof(*lur):
542 {
543 int err = 0;
544
545 if (lur->handle != expected_handle) {
546 ERR("Unexpected result message handle: "
547 "expected: %u vs received: %u\n",
548 expected_handle, lur->handle);
549 err = 1;
550 }
551 if (lur->cmd != expected_cmd) {
552 ERR("Unexpected result message command "
553 "expected: %u vs received: %u\n",
554 expected_cmd, lur->cmd);
555 err = 1;
556 }
557 if (err) {
558 return -EINVAL;
559 } else {
560 return lur->ret_code;
561 }
562 }
563 default:
564 if (len >= 0) {
565 ERR("incorrect message size: %zd\n", len);
566 }
567 return len;
568 }
569 }
570
571 int ustcomm_send_app_cmd(int sock,
572 struct ustcomm_ust_msg *lum,
573 struct ustcomm_ust_reply *lur)
574 {
575 int ret;
576
577 ret = ustcomm_send_app_msg(sock, lum);
578 if (ret)
579 return ret;
580 ret = ustcomm_recv_app_reply(sock, lur, lum->handle, lum->cmd);
581 if (ret > 0)
582 return -EIO;
583 return ret;
584 }
585
586 /*
587 * chan_data is allocated internally if this function returns the
588 * expected var_len.
589 */
590 ssize_t ustcomm_recv_channel_from_sessiond(int sock,
591 void **_chan_data, uint64_t var_len,
592 int *_wakeup_fd)
593 {
594 void *chan_data;
595 ssize_t len, nr_fd;
596 int wakeup_fd, ret;
597
598 if (var_len > LTTNG_UST_CHANNEL_DATA_MAX_LEN) {
599 len = -EINVAL;
600 goto error_check;
601 }
602 /* Receive variable length data */
603 chan_data = zmalloc(var_len);
604 if (!chan_data) {
605 len = -ENOMEM;
606 goto error_alloc;
607 }
608 len = ustcomm_recv_unix_sock(sock, chan_data, var_len);
609 if (len != var_len) {
610 goto error_recv;
611 }
612 /* recv wakeup fd */
613 lttng_ust_lock_fd_tracker();
614 nr_fd = ustcomm_recv_fds_unix_sock(sock, &wakeup_fd, 1);
615 if (nr_fd <= 0) {
616 lttng_ust_unlock_fd_tracker();
617 if (nr_fd < 0) {
618 len = nr_fd;
619 goto error_recv;
620 } else {
621 len = -EIO;
622 goto error_recv;
623 }
624 }
625
626 ret = lttng_ust_add_fd_to_tracker(wakeup_fd);
627 if (ret < 0) {
628 lttng_ust_unlock_fd_tracker();
629 ret = close(wakeup_fd);
630 if (ret) {
631 PERROR("close on wakeup_fd");
632 }
633 len = -EIO;
634 goto error_recv;
635 }
636
637 *_wakeup_fd = ret;
638 lttng_ust_unlock_fd_tracker();
639
640 *_chan_data = chan_data;
641 return len;
642
643 error_recv:
644 free(chan_data);
645 error_alloc:
646 error_check:
647 return len;
648 }
649
650 int ustcomm_recv_stream_from_sessiond(int sock,
651 uint64_t *memory_map_size,
652 int *shm_fd, int *wakeup_fd)
653 {
654 ssize_t len;
655 int ret;
656 int fds[2];
657
658 /* recv shm fd and wakeup fd */
659 lttng_ust_lock_fd_tracker();
660 len = ustcomm_recv_fds_unix_sock(sock, fds, 2);
661 if (len <= 0) {
662 lttng_ust_unlock_fd_tracker();
663 if (len < 0) {
664 ret = len;
665 goto error;
666 } else {
667 ret = -EIO;
668 goto error;
669 }
670 }
671
672 ret = lttng_ust_add_fd_to_tracker(fds[0]);
673 if (ret < 0) {
674 lttng_ust_unlock_fd_tracker();
675 ret = close(fds[0]);
676 if (ret) {
677 PERROR("close on received shm_fd");
678 }
679 ret = -EIO;
680 goto error;
681 }
682 *shm_fd = ret;
683
684 ret = lttng_ust_add_fd_to_tracker(fds[1]);
685 if (ret < 0) {
686 lttng_ust_unlock_fd_tracker();
687 ret = close(*shm_fd);
688 if (ret) {
689 PERROR("close on shm_fd");
690 }
691 *shm_fd = -1;
692 ret = close(fds[1]);
693 if (ret) {
694 PERROR("close on received wakeup_fd");
695 }
696 ret = -EIO;
697 goto error;
698 }
699 *wakeup_fd = ret;
700 lttng_ust_unlock_fd_tracker();
701 return 0;
702
703 error:
704 return ret;
705 }
706
707 /*
708 * Returns 0 on success, negative error value on error.
709 */
710 int ustcomm_send_reg_msg(int sock,
711 enum ustctl_socket_type type,
712 uint32_t bits_per_long,
713 uint32_t uint8_t_alignment,
714 uint32_t uint16_t_alignment,
715 uint32_t uint32_t_alignment,
716 uint32_t uint64_t_alignment,
717 uint32_t long_alignment)
718 {
719 ssize_t len;
720 struct ustctl_reg_msg reg_msg;
721
722 reg_msg.magic = LTTNG_UST_COMM_MAGIC;
723 reg_msg.major = LTTNG_UST_ABI_MAJOR_VERSION;
724 reg_msg.minor = LTTNG_UST_ABI_MINOR_VERSION;
725 reg_msg.pid = getpid();
726 reg_msg.ppid = getppid();
727 reg_msg.uid = getuid();
728 reg_msg.gid = getgid();
729 reg_msg.bits_per_long = bits_per_long;
730 reg_msg.uint8_t_alignment = uint8_t_alignment;
731 reg_msg.uint16_t_alignment = uint16_t_alignment;
732 reg_msg.uint32_t_alignment = uint32_t_alignment;
733 reg_msg.uint64_t_alignment = uint64_t_alignment;
734 reg_msg.long_alignment = long_alignment;
735 reg_msg.socket_type = type;
736 lttng_ust_getprocname(reg_msg.name);
737 memset(reg_msg.padding, 0, sizeof(reg_msg.padding));
738
739 len = ustcomm_send_unix_sock(sock, &reg_msg, sizeof(reg_msg));
740 if (len > 0 && len != sizeof(reg_msg))
741 return -EIO;
742 if (len < 0)
743 return len;
744 return 0;
745 }
746
747 static
748 ssize_t count_one_type(const struct lttng_type *lt)
749 {
750 switch (lt->atype) {
751 case atype_integer:
752 case atype_float:
753 case atype_string:
754 case atype_enum:
755 case atype_array:
756 case atype_sequence:
757 return 1;
758 case atype_struct:
759 //TODO: implement non-empty struct.
760 return 1;
761 case atype_dynamic:
762 {
763 const struct lttng_event_field *choices;
764 size_t nr_choices;
765 int ret;
766
767 ret = lttng_ust_dynamic_type_choices(&nr_choices,
768 &choices);
769 if (ret)
770 return ret;
771 /*
772 * One field for enum, one field for variant, and
773 * one field per choice.
774 */
775 return count_fields_recursive(nr_choices, choices) + 2;
776 }
777 default:
778 return -EINVAL;
779 }
780 return 0;
781 }
782
783 static
784 ssize_t count_fields_recursive(size_t nr_fields,
785 const struct lttng_event_field *lttng_fields)
786 {
787 int i;
788 ssize_t ret, count = 0;
789
790 for (i = 0; i < nr_fields; i++) {
791 const struct lttng_event_field *lf;
792
793 lf = &lttng_fields[i];
794 /* skip 'nowrite' fields */
795 if (lf->nowrite)
796 continue;
797 ret = count_one_type(&lf->type);
798 if (ret < 0)
799 return ret; /* error */
800 count += ret;
801 }
802 return count;
803 }
804
805 static
806 ssize_t count_ctx_fields_recursive(size_t nr_fields,
807 const struct lttng_ctx_field *lttng_fields)
808 {
809 int i;
810 ssize_t ret, count = 0;
811
812 for (i = 0; i < nr_fields; i++) {
813 const struct lttng_event_field *lf;
814
815 lf = &lttng_fields[i].event_field;
816 /* skip 'nowrite' fields */
817 if (lf->nowrite)
818 continue;
819 ret = count_one_type(&lf->type);
820 if (ret < 0)
821 return ret; /* error */
822 count += ret;
823 }
824 return count;
825 }
826
827 static
828 int serialize_string_encoding(int32_t *ue,
829 enum lttng_string_encodings le)
830 {
831 switch (le) {
832 case lttng_encode_none:
833 *ue = ustctl_encode_none;
834 break;
835 case lttng_encode_UTF8:
836 *ue = ustctl_encode_UTF8;
837 break;
838 case lttng_encode_ASCII:
839 *ue = ustctl_encode_ASCII;
840 break;
841 default:
842 return -EINVAL;
843 }
844 return 0;
845 }
846
847 static
848 int serialize_integer_type(struct ustctl_integer_type *uit,
849 const struct lttng_integer_type *lit)
850 {
851 int32_t encoding;
852
853 uit->size = lit->size;
854 uit->signedness = lit->signedness;
855 uit->reverse_byte_order = lit->reverse_byte_order;
856 uit->base = lit->base;
857 if (serialize_string_encoding(&encoding, lit->encoding))
858 return -EINVAL;
859 uit->encoding = encoding;
860 uit->alignment = lit->alignment;
861 return 0;
862 }
863
864 static
865 int serialize_basic_type(struct lttng_session *session,
866 enum ustctl_abstract_types *uatype,
867 enum lttng_abstract_types atype,
868 union _ustctl_basic_type *ubt,
869 const union _lttng_basic_type *lbt)
870 {
871 switch (atype) {
872 case atype_integer:
873 {
874 if (serialize_integer_type(&ubt->integer, &lbt->integer))
875 return -EINVAL;
876 *uatype = ustctl_atype_integer;
877 break;
878 }
879 case atype_string:
880 {
881 int32_t encoding;
882
883 if (serialize_string_encoding(&encoding, lbt->string.encoding))
884 return -EINVAL;
885 ubt->string.encoding = encoding;
886 *uatype = ustctl_atype_string;
887 break;
888 }
889 case atype_float:
890 {
891 struct ustctl_float_type *uft;
892 const struct lttng_float_type *lft;
893
894 uft = &ubt->_float;
895 lft = &lbt->_float;
896 uft->exp_dig = lft->exp_dig;
897 uft->mant_dig = lft->mant_dig;
898 uft->alignment = lft->alignment;
899 uft->reverse_byte_order = lft->reverse_byte_order;
900 *uatype = ustctl_atype_float;
901 break;
902 }
903 case atype_enum:
904 {
905 strncpy(ubt->enumeration.name, lbt->enumeration.desc->name,
906 LTTNG_UST_SYM_NAME_LEN);
907 ubt->enumeration.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
908 if (serialize_integer_type(&ubt->enumeration.container_type,
909 &lbt->enumeration.container_type))
910 return -EINVAL;
911 if (session) {
912 const struct lttng_enum *_enum;
913
914 _enum = lttng_ust_enum_get_from_desc(session, lbt->enumeration.desc);
915 if (!_enum)
916 return -EINVAL;
917 ubt->enumeration.id = _enum->id;
918 } else {
919 ubt->enumeration.id = -1ULL;
920 }
921 *uatype = ustctl_atype_enum;
922 break;
923 }
924 case atype_array:
925 case atype_sequence:
926 default:
927 return -EINVAL;
928 }
929 return 0;
930 }
931
932 static
933 int serialize_dynamic_type(struct lttng_session *session,
934 struct ustctl_field *fields, size_t *iter_output,
935 const struct lttng_event_field *lf)
936 {
937 const struct lttng_event_field *choices;
938 char tag_field_name[LTTNG_UST_SYM_NAME_LEN];
939 const struct lttng_type *tag_type;
940 const struct lttng_event_field *tag_field_generic;
941 struct lttng_event_field tag_field = {
942 .name = tag_field_name,
943 .nowrite = 0,
944 };
945 struct ustctl_field *uf;
946 size_t nr_choices, i;
947 int ret;
948
949 tag_field_generic = lttng_ust_dynamic_type_tag_field();
950 tag_type = &tag_field_generic->type;
951
952 /* Serialize enum field. */
953 strncpy(tag_field_name, lf->name, LTTNG_UST_SYM_NAME_LEN);
954 tag_field_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
955 strncat(tag_field_name,
956 "_tag",
957 LTTNG_UST_SYM_NAME_LEN - strlen(tag_field_name) - 1);
958 tag_field.type = *tag_type;
959 ret = serialize_one_field(session, fields, iter_output,
960 &tag_field);
961 if (ret)
962 return ret;
963
964 /* Serialize variant field. */
965 uf = &fields[*iter_output];
966 ret = lttng_ust_dynamic_type_choices(&nr_choices, &choices);
967 if (ret)
968 return ret;
969
970 strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN);
971 uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
972 uf->type.atype = ustctl_atype_variant;
973 uf->type.u.variant.nr_choices = nr_choices;
974 strncpy(uf->type.u.variant.tag_name,
975 tag_field_name,
976 LTTNG_UST_SYM_NAME_LEN);
977 uf->type.u.variant.tag_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
978 (*iter_output)++;
979
980 /* Serialize choice fields after variant. */
981 for (i = 0; i < nr_choices; i++) {
982 ret = serialize_one_field(session, fields,
983 iter_output, &choices[i]);
984 if (ret)
985 return ret;
986 }
987 return 0;
988 }
989
990 static
991 int serialize_one_field(struct lttng_session *session,
992 struct ustctl_field *fields, size_t *iter_output,
993 const struct lttng_event_field *lf)
994 {
995 const struct lttng_type *lt = &lf->type;
996 int ret;
997
998 /* skip 'nowrite' fields */
999 if (lf->nowrite)
1000 return 0;
1001
1002 switch (lt->atype) {
1003 case atype_integer:
1004 case atype_float:
1005 case atype_string:
1006 case atype_enum:
1007 {
1008 struct ustctl_field *uf = &fields[*iter_output];
1009 struct ustctl_type *ut = &uf->type;
1010 enum ustctl_abstract_types atype;
1011
1012 strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN);
1013 uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1014 ret = serialize_basic_type(session, &atype, lt->atype,
1015 &ut->u.basic, &lt->u.basic);
1016 if (ret)
1017 return ret;
1018 ut->atype = atype;
1019 (*iter_output)++;
1020 break;
1021 }
1022 case atype_array:
1023 {
1024 struct ustctl_field *uf = &fields[*iter_output];
1025 struct ustctl_type *ut = &uf->type;
1026 struct ustctl_basic_type *ubt;
1027 const struct lttng_basic_type *lbt;
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 uf->type.atype = ustctl_atype_array;
1033 ubt = &ut->u.array.elem_type;
1034 lbt = &lt->u.array.elem_type;
1035 ut->u.array.length = lt->u.array.length;
1036 ret = serialize_basic_type(session, &atype, lbt->atype,
1037 &ubt->u.basic, &lbt->u.basic);
1038 if (ret)
1039 return -EINVAL;
1040 ubt->atype = atype;
1041 ut->atype = ustctl_atype_array;
1042 (*iter_output)++;
1043 break;
1044 }
1045 case atype_sequence:
1046 {
1047 struct ustctl_field *uf = &fields[*iter_output];
1048 struct ustctl_type *ut = &uf->type;
1049 struct ustctl_basic_type *ubt;
1050 const struct lttng_basic_type *lbt;
1051 enum ustctl_abstract_types atype;
1052 int ret;
1053
1054 strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN);
1055 uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1056 uf->type.atype = ustctl_atype_sequence;
1057 ubt = &ut->u.sequence.length_type;
1058 lbt = &lt->u.sequence.length_type;
1059 ret = serialize_basic_type(session, &atype, lbt->atype,
1060 &ubt->u.basic, &lbt->u.basic);
1061 if (ret)
1062 return -EINVAL;
1063 ubt->atype = atype;
1064 ubt = &ut->u.sequence.elem_type;
1065 lbt = &lt->u.sequence.elem_type;
1066 ret = serialize_basic_type(session, &atype, lbt->atype,
1067 &ubt->u.basic, &lbt->u.basic);
1068 if (ret)
1069 return -EINVAL;
1070 ubt->atype = atype;
1071 ut->atype = ustctl_atype_sequence;
1072 (*iter_output)++;
1073 break;
1074 }
1075 case atype_dynamic:
1076 {
1077 ret = serialize_dynamic_type(session, fields, iter_output, lf);
1078 if (ret)
1079 return -EINVAL;
1080 break;
1081 }
1082 case atype_struct:
1083 {
1084 struct ustctl_field *uf = &fields[*iter_output];
1085
1086 /*
1087 * TODO: add support for non-empty struct.
1088 */
1089 if (lf->type.u._struct.nr_fields != 0) {
1090 return -EINVAL;
1091 }
1092 strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN);
1093 uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1094 uf->type.atype = ustctl_atype_struct;
1095 uf->type.u._struct.nr_fields = 0;
1096 (*iter_output)++;
1097 break;
1098 }
1099 default:
1100 return -EINVAL;
1101 }
1102 return 0;
1103 }
1104
1105 static
1106 int serialize_fields(struct lttng_session *session,
1107 size_t *_nr_write_fields,
1108 struct ustctl_field **ustctl_fields,
1109 size_t nr_fields,
1110 const struct lttng_event_field *lttng_fields)
1111 {
1112 struct ustctl_field *fields;
1113 int ret;
1114 size_t i, iter_output = 0;
1115 ssize_t nr_write_fields;
1116
1117 nr_write_fields = count_fields_recursive(nr_fields, lttng_fields);
1118 if (nr_write_fields < 0) {
1119 return (int) nr_write_fields;
1120 }
1121
1122 fields = zmalloc(nr_write_fields * sizeof(*fields));
1123 if (!fields)
1124 return -ENOMEM;
1125
1126 for (i = 0; i < nr_fields; i++) {
1127 ret = serialize_one_field(session, fields, &iter_output,
1128 &lttng_fields[i]);
1129 if (ret)
1130 goto error_type;
1131 }
1132
1133 *_nr_write_fields = nr_write_fields;
1134 *ustctl_fields = fields;
1135 return 0;
1136
1137 error_type:
1138 free(fields);
1139 return ret;
1140 }
1141
1142 static
1143 int serialize_entries(struct ustctl_enum_entry **_entries,
1144 size_t nr_entries,
1145 const struct lttng_enum_entry *lttng_entries)
1146 {
1147 struct ustctl_enum_entry *entries;
1148 int i;
1149
1150 /* Serialize the entries */
1151 entries = zmalloc(nr_entries * sizeof(*entries));
1152 if (!entries)
1153 return -ENOMEM;
1154 for (i = 0; i < nr_entries; i++) {
1155 struct ustctl_enum_entry *uentry;
1156 const struct lttng_enum_entry *lentry;
1157
1158 uentry = &entries[i];
1159 lentry = &lttng_entries[i];
1160
1161 uentry->start.value = lentry->start.value;
1162 uentry->start.signedness = lentry->start.signedness;
1163 uentry->end.value = lentry->end.value;
1164 uentry->end.signedness = lentry->end.signedness;
1165 strncpy(uentry->string, lentry->string, LTTNG_UST_SYM_NAME_LEN);
1166 uentry->string[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1167
1168 if (lentry->u.extra.options & LTTNG_ENUM_ENTRY_OPTION_IS_AUTO) {
1169 uentry->u.extra.options |=
1170 USTCTL_UST_ENUM_ENTRY_OPTION_IS_AUTO;
1171 }
1172 }
1173 *_entries = entries;
1174 return 0;
1175 }
1176
1177 static
1178 int serialize_ctx_fields(struct lttng_session *session,
1179 size_t *_nr_write_fields,
1180 struct ustctl_field **ustctl_fields,
1181 size_t nr_fields,
1182 const struct lttng_ctx_field *lttng_fields)
1183 {
1184 struct ustctl_field *fields;
1185 int ret;
1186 size_t i, iter_output = 0;
1187 ssize_t nr_write_fields;
1188
1189 nr_write_fields = count_ctx_fields_recursive(nr_fields,
1190 lttng_fields);
1191 if (nr_write_fields < 0) {
1192 return (int) nr_write_fields;
1193 }
1194
1195 fields = zmalloc(nr_write_fields * sizeof(*fields));
1196 if (!fields)
1197 return -ENOMEM;
1198
1199 for (i = 0; i < nr_fields; i++) {
1200 ret = serialize_one_field(session, fields, &iter_output,
1201 &lttng_fields[i].event_field);
1202 if (ret)
1203 goto error_type;
1204 }
1205
1206 *_nr_write_fields = nr_write_fields;
1207 *ustctl_fields = fields;
1208 return 0;
1209
1210 error_type:
1211 free(fields);
1212 return ret;
1213 }
1214
1215 /*
1216 * Returns 0 on success, negative error value on error.
1217 */
1218 int ustcomm_register_event(int sock,
1219 struct lttng_session *session,
1220 int session_objd, /* session descriptor */
1221 int channel_objd, /* channel descriptor */
1222 const char *event_name, /* event name (input) */
1223 int loglevel,
1224 const char *signature, /* event signature (input) */
1225 size_t nr_fields, /* fields */
1226 const struct lttng_event_field *lttng_fields,
1227 const char *model_emf_uri,
1228 uint32_t *id) /* event id (output) */
1229 {
1230 ssize_t len;
1231 struct {
1232 struct ustcomm_notify_hdr header;
1233 struct ustcomm_notify_event_msg m;
1234 } msg;
1235 struct {
1236 struct ustcomm_notify_hdr header;
1237 struct ustcomm_notify_event_reply r;
1238 } reply;
1239 size_t signature_len, fields_len, model_emf_uri_len;
1240 struct ustctl_field *fields = NULL;
1241 size_t nr_write_fields = 0;
1242 int ret;
1243
1244 memset(&msg, 0, sizeof(msg));
1245 msg.header.notify_cmd = USTCTL_NOTIFY_CMD_EVENT;
1246 msg.m.session_objd = session_objd;
1247 msg.m.channel_objd = channel_objd;
1248 strncpy(msg.m.event_name, event_name, LTTNG_UST_SYM_NAME_LEN);
1249 msg.m.event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1250 msg.m.loglevel = loglevel;
1251 signature_len = strlen(signature) + 1;
1252 msg.m.signature_len = signature_len;
1253
1254 /* Calculate fields len, serialize fields. */
1255 if (nr_fields > 0) {
1256 ret = serialize_fields(session, &nr_write_fields, &fields,
1257 nr_fields, lttng_fields);
1258 if (ret)
1259 return ret;
1260 }
1261
1262 fields_len = sizeof(*fields) * nr_write_fields;
1263 msg.m.fields_len = fields_len;
1264 if (model_emf_uri) {
1265 model_emf_uri_len = strlen(model_emf_uri) + 1;
1266 } else {
1267 model_emf_uri_len = 0;
1268 }
1269 msg.m.model_emf_uri_len = model_emf_uri_len;
1270
1271 len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg));
1272 if (len > 0 && len != sizeof(msg)) {
1273 ret = -EIO;
1274 goto error_fields;
1275 }
1276 if (len < 0) {
1277 ret = len;
1278 goto error_fields;
1279 }
1280
1281 /* send signature */
1282 len = ustcomm_send_unix_sock(sock, signature, signature_len);
1283 if (len > 0 && len != signature_len) {
1284 ret = -EIO;
1285 goto error_fields;
1286 }
1287 if (len < 0) {
1288 ret = len;
1289 goto error_fields;
1290 }
1291
1292 /* send fields */
1293 if (fields_len > 0) {
1294 len = ustcomm_send_unix_sock(sock, fields, fields_len);
1295 if (len > 0 && len != fields_len) {
1296 ret = -EIO;
1297 goto error_fields;
1298 }
1299 if (len < 0) {
1300 ret = len;
1301 goto error_fields;
1302 }
1303 }
1304 free(fields);
1305
1306 if (model_emf_uri_len) {
1307 /* send model_emf_uri */
1308 len = ustcomm_send_unix_sock(sock, model_emf_uri,
1309 model_emf_uri_len);
1310 if (len > 0 && len != model_emf_uri_len) {
1311 return -EIO;
1312 }
1313 if (len < 0) {
1314 return len;
1315 }
1316 }
1317
1318 /* receive reply */
1319 len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply));
1320 switch (len) {
1321 case 0: /* orderly shutdown */
1322 return -EPIPE;
1323 case sizeof(reply):
1324 if (reply.header.notify_cmd != msg.header.notify_cmd) {
1325 ERR("Unexpected result message command "
1326 "expected: %u vs received: %u\n",
1327 msg.header.notify_cmd, reply.header.notify_cmd);
1328 return -EINVAL;
1329 }
1330 if (reply.r.ret_code > 0)
1331 return -EINVAL;
1332 if (reply.r.ret_code < 0)
1333 return reply.r.ret_code;
1334 *id = reply.r.event_id;
1335 DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n",
1336 event_name, reply.r.ret_code, reply.r.event_id);
1337 return 0;
1338 default:
1339 if (len < 0) {
1340 /* Transport level error */
1341 if (errno == EPIPE || errno == ECONNRESET)
1342 len = -errno;
1343 return len;
1344 } else {
1345 ERR("incorrect message size: %zd\n", len);
1346 return len;
1347 }
1348 }
1349 /* Unreached. */
1350
1351 /* Error path only. */
1352 error_fields:
1353 free(fields);
1354 return ret;
1355 }
1356
1357 /*
1358 * Returns 0 on success, negative error value on error.
1359 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1360 */
1361 int ustcomm_register_enum(int sock,
1362 int session_objd, /* session descriptor */
1363 const char *enum_name, /* enum name (input) */
1364 size_t nr_entries, /* entries */
1365 const struct lttng_enum_entry *lttng_entries,
1366 uint64_t *id)
1367 {
1368 ssize_t len;
1369 struct {
1370 struct ustcomm_notify_hdr header;
1371 struct ustcomm_notify_enum_msg m;
1372 } msg;
1373 struct {
1374 struct ustcomm_notify_hdr header;
1375 struct ustcomm_notify_enum_reply r;
1376 } reply;
1377 size_t entries_len;
1378 struct ustctl_enum_entry *entries = NULL;
1379 int ret;
1380
1381 memset(&msg, 0, sizeof(msg));
1382 msg.header.notify_cmd = USTCTL_NOTIFY_CMD_ENUM;
1383 msg.m.session_objd = session_objd;
1384 strncpy(msg.m.enum_name, enum_name, LTTNG_UST_SYM_NAME_LEN);
1385 msg.m.enum_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1386
1387 /* Calculate entries len, serialize entries. */
1388 if (nr_entries > 0) {
1389 ret = serialize_entries(&entries,
1390 nr_entries, lttng_entries);
1391 if (ret)
1392 return ret;
1393 }
1394
1395 entries_len = sizeof(*entries) * nr_entries;
1396 msg.m.entries_len = entries_len;
1397
1398 len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg));
1399 if (len > 0 && len != sizeof(msg)) {
1400 ret = -EIO;
1401 goto error_entries;
1402 }
1403 if (len < 0) {
1404 ret = len;
1405 goto error_entries;
1406 }
1407
1408 /* send entries */
1409 if (entries_len > 0) {
1410 len = ustcomm_send_unix_sock(sock, entries, entries_len);
1411 if (len > 0 && len != entries_len) {
1412 ret = -EIO;
1413 goto error_entries;
1414 }
1415 if (len < 0) {
1416 ret = len;
1417 goto error_entries;
1418 }
1419 }
1420 free(entries);
1421 entries = NULL;
1422
1423 /* receive reply */
1424 len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply));
1425 switch (len) {
1426 case 0: /* orderly shutdown */
1427 return -EPIPE;
1428 case sizeof(reply):
1429 if (reply.header.notify_cmd != msg.header.notify_cmd) {
1430 ERR("Unexpected result message command "
1431 "expected: %u vs received: %u\n",
1432 msg.header.notify_cmd, reply.header.notify_cmd);
1433 return -EINVAL;
1434 }
1435 if (reply.r.ret_code > 0)
1436 return -EINVAL;
1437 if (reply.r.ret_code < 0)
1438 return reply.r.ret_code;
1439 *id = reply.r.enum_id;
1440 DBG("Sent register enum notification for name \"%s\": ret_code %d\n",
1441 enum_name, reply.r.ret_code);
1442 return 0;
1443 default:
1444 if (len < 0) {
1445 /* Transport level error */
1446 if (errno == EPIPE || errno == ECONNRESET)
1447 len = -errno;
1448 return len;
1449 } else {
1450 ERR("incorrect message size: %zd\n", len);
1451 return len;
1452 }
1453 }
1454 return ret;
1455
1456 error_entries:
1457 free(entries);
1458 return ret;
1459 }
1460
1461 /*
1462 * Returns 0 on success, negative error value on error.
1463 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1464 */
1465 int ustcomm_register_channel(int sock,
1466 struct lttng_session *session,
1467 int session_objd, /* session descriptor */
1468 int channel_objd, /* channel descriptor */
1469 size_t nr_ctx_fields,
1470 const struct lttng_ctx_field *ctx_fields,
1471 uint32_t *chan_id, /* channel id (output) */
1472 int *header_type) /* header type (output) */
1473 {
1474 ssize_t len;
1475 struct {
1476 struct ustcomm_notify_hdr header;
1477 struct ustcomm_notify_channel_msg m;
1478 } msg;
1479 struct {
1480 struct ustcomm_notify_hdr header;
1481 struct ustcomm_notify_channel_reply r;
1482 } reply;
1483 size_t fields_len;
1484 struct ustctl_field *fields = NULL;
1485 int ret;
1486 size_t nr_write_fields = 0;
1487
1488 memset(&msg, 0, sizeof(msg));
1489 msg.header.notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL;
1490 msg.m.session_objd = session_objd;
1491 msg.m.channel_objd = channel_objd;
1492
1493 /* Calculate fields len, serialize fields. */
1494 if (nr_ctx_fields > 0) {
1495 ret = serialize_ctx_fields(session, &nr_write_fields, &fields,
1496 nr_ctx_fields, ctx_fields);
1497 if (ret)
1498 return ret;
1499 }
1500
1501 fields_len = sizeof(*fields) * nr_write_fields;
1502 msg.m.ctx_fields_len = fields_len;
1503 len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg));
1504 if (len > 0 && len != sizeof(msg)) {
1505 free(fields);
1506 return -EIO;
1507 }
1508 if (len < 0) {
1509 free(fields);
1510 return len;
1511 }
1512
1513 /* send fields */
1514 if (fields_len > 0) {
1515 len = ustcomm_send_unix_sock(sock, fields, fields_len);
1516 free(fields);
1517 if (len > 0 && len != fields_len) {
1518 return -EIO;
1519 }
1520 if (len < 0) {
1521 return len;
1522 }
1523 } else {
1524 free(fields);
1525 }
1526
1527 len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply));
1528 switch (len) {
1529 case 0: /* orderly shutdown */
1530 return -EPIPE;
1531 case sizeof(reply):
1532 if (reply.header.notify_cmd != msg.header.notify_cmd) {
1533 ERR("Unexpected result message command "
1534 "expected: %u vs received: %u\n",
1535 msg.header.notify_cmd, reply.header.notify_cmd);
1536 return -EINVAL;
1537 }
1538 if (reply.r.ret_code > 0)
1539 return -EINVAL;
1540 if (reply.r.ret_code < 0)
1541 return reply.r.ret_code;
1542 *chan_id = reply.r.chan_id;
1543 switch (reply.r.header_type) {
1544 case 1:
1545 case 2:
1546 *header_type = reply.r.header_type;
1547 break;
1548 default:
1549 ERR("Unexpected channel header type %u\n",
1550 reply.r.header_type);
1551 return -EINVAL;
1552 }
1553 DBG("Sent register channel notification: chan_id %d, header_type %d\n",
1554 reply.r.chan_id, reply.r.header_type);
1555 return 0;
1556 default:
1557 if (len < 0) {
1558 /* Transport level error */
1559 if (errno == EPIPE || errno == ECONNRESET)
1560 len = -errno;
1561 return len;
1562 } else {
1563 ERR("incorrect message size: %zd\n", len);
1564 return len;
1565 }
1566 }
1567 }
1568
1569 /*
1570 * Set socket reciving timeout.
1571 */
1572 int ustcomm_setsockopt_rcv_timeout(int sock, unsigned int msec)
1573 {
1574 int ret;
1575 struct timeval tv;
1576
1577 tv.tv_sec = msec / 1000;
1578 tv.tv_usec = (msec * 1000 % 1000000);
1579
1580 ret = setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
1581 if (ret < 0) {
1582 PERROR("setsockopt SO_RCVTIMEO");
1583 ret = -errno;
1584 }
1585
1586 return ret;
1587 }
1588
1589 /*
1590 * Set socket sending timeout.
1591 */
1592 int ustcomm_setsockopt_snd_timeout(int sock, unsigned int msec)
1593 {
1594 int ret;
1595 struct timeval tv;
1596
1597 tv.tv_sec = msec / 1000;
1598 tv.tv_usec = (msec * 1000) % 1000000;
1599
1600 ret = setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
1601 if (ret < 0) {
1602 PERROR("setsockopt SO_SNDTIMEO");
1603 ret = -errno;
1604 }
1605
1606 return ret;
1607 }
This page took 0.064329 seconds and 5 git commands to generate.