Add channel wakeup fd to monitor close
[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 <helper.h>
37 #include <lttng/ust-error.h>
38 #include <lttng/ust-events.h>
39 #include <usterr-signal-safe.h>
40
41 #include "../liblttng-ust/compat.h"
42
43 #define USTCOMM_CODE_OFFSET(code) \
44 (code == LTTNG_UST_OK ? 0 : (code - LTTNG_UST_ERR + 1))
45
46 #define USTCOMM_MAX_SEND_FDS 4
47
48 /*
49 * Human readable error message.
50 */
51 static const char *ustcomm_readable_code[] = {
52 [ USTCOMM_CODE_OFFSET(LTTNG_UST_OK) ] = "Success",
53 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR) ] = "Unknown error",
54 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_NOENT) ] = "No entry",
55 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_EXIST) ] = "Object already exists",
56 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL) ] = "Invalid argument",
57 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_PERM) ] = "Permission denied",
58 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_NOSYS) ] = "Not implemented",
59 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_EXITING) ] = "Process is exiting",
60
61 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL_MAGIC) ] = "Invalid magic number",
62 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL_SOCKET_TYPE) ] = "Invalid socket type",
63 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_UNSUP_MAJOR) ] = "Unsupported major version",
64 };
65
66 /*
67 * lttng_ust_strerror
68 *
69 * Receives positive error value.
70 * Return ptr to string representing a human readable
71 * error code from the ustcomm_return_code enum.
72 */
73 const char *lttng_ust_strerror(int code)
74 {
75 if (code == LTTNG_UST_OK)
76 return ustcomm_readable_code[USTCOMM_CODE_OFFSET(code)];
77 if (code < LTTNG_UST_ERR)
78 return strerror(code);
79 if (code >= LTTNG_UST_ERR_NR)
80 code = LTTNG_UST_ERR;
81 return ustcomm_readable_code[USTCOMM_CODE_OFFSET(code)];
82 }
83
84 /*
85 * ustcomm_connect_unix_sock
86 *
87 * Connect to unix socket using the path name.
88 */
89 int ustcomm_connect_unix_sock(const char *pathname)
90 {
91 struct sockaddr_un sun;
92 int fd, ret;
93
94 /*
95 * libust threads require the close-on-exec flag for all
96 * resources so it does not leak file descriptors upon exec.
97 */
98 fd = socket(PF_UNIX, SOCK_STREAM, 0);
99 if (fd < 0) {
100 PERROR("socket");
101 ret = -errno;
102 goto error;
103 }
104 ret = fcntl(fd, F_SETFD, FD_CLOEXEC);
105 if (ret < 0) {
106 PERROR("fcntl");
107 ret = -errno;
108 goto error_fcntl;
109 }
110
111 memset(&sun, 0, sizeof(sun));
112 sun.sun_family = AF_UNIX;
113 strncpy(sun.sun_path, pathname, sizeof(sun.sun_path));
114 sun.sun_path[sizeof(sun.sun_path) - 1] = '\0';
115
116 ret = connect(fd, (struct sockaddr *) &sun, sizeof(sun));
117 if (ret < 0) {
118 /*
119 * Don't print message on connect ENOENT error, because
120 * connect is used in normal execution to detect if
121 * sessiond is alive. ENOENT is when the unix socket
122 * file does not exist, and ECONNREFUSED is when the
123 * file exists but no sessiond is listening.
124 */
125 if (errno != ECONNREFUSED && errno != ECONNRESET
126 && errno != ENOENT)
127 PERROR("connect");
128 ret = -errno;
129 if (ret == -ECONNREFUSED || ret == -ECONNRESET)
130 ret = -EPIPE;
131 goto error_connect;
132 }
133
134 return fd;
135
136 error_connect:
137 error_fcntl:
138 {
139 int closeret;
140
141 closeret = close(fd);
142 if (closeret)
143 PERROR("close");
144 }
145 error:
146 return ret;
147 }
148
149 /*
150 * ustcomm_accept_unix_sock
151 *
152 * Do an accept(2) on the sock and return the
153 * new file descriptor. The socket MUST be bind(2) before.
154 */
155 int ustcomm_accept_unix_sock(int sock)
156 {
157 int new_fd;
158 struct sockaddr_un sun;
159 socklen_t len = 0;
160
161 /* Blocking call */
162 new_fd = accept(sock, (struct sockaddr *) &sun, &len);
163 if (new_fd < 0) {
164 if (errno != ECONNABORTED)
165 PERROR("accept");
166 new_fd = -errno;
167 if (new_fd == -ECONNABORTED)
168 new_fd = -EPIPE;
169 }
170 return new_fd;
171 }
172
173 /*
174 * ustcomm_create_unix_sock
175 *
176 * Creates a AF_UNIX local socket using pathname
177 * bind the socket upon creation and return the fd.
178 */
179 int ustcomm_create_unix_sock(const char *pathname)
180 {
181 struct sockaddr_un sun;
182 int fd, ret;
183
184 /* Create server socket */
185 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
186 PERROR("socket");
187 ret = -errno;
188 goto error;
189 }
190
191 memset(&sun, 0, sizeof(sun));
192 sun.sun_family = AF_UNIX;
193 strncpy(sun.sun_path, pathname, sizeof(sun.sun_path));
194 sun.sun_path[sizeof(sun.sun_path) - 1] = '\0';
195
196 /* Unlink the old file if present */
197 (void) unlink(pathname);
198 ret = bind(fd, (struct sockaddr *) &sun, sizeof(sun));
199 if (ret < 0) {
200 PERROR("bind");
201 ret = -errno;
202 goto error_close;
203 }
204
205 return fd;
206
207 error_close:
208 {
209 int closeret;
210
211 closeret = close(fd);
212 if (closeret) {
213 PERROR("close");
214 }
215 }
216 error:
217 return ret;
218 }
219
220 /*
221 * ustcomm_listen_unix_sock
222 *
223 * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN.
224 */
225 int ustcomm_listen_unix_sock(int sock)
226 {
227 int ret;
228
229 ret = listen(sock, LTTNG_UST_COMM_MAX_LISTEN);
230 if (ret < 0) {
231 ret = -errno;
232 PERROR("listen");
233 }
234
235 return ret;
236 }
237
238 /*
239 * ustcomm_close_unix_sock
240 *
241 * Shutdown cleanly a unix socket.
242 */
243 int ustcomm_close_unix_sock(int sock)
244 {
245 int ret;
246
247 ret = close(sock);
248 if (ret < 0) {
249 PERROR("close");
250 ret = -errno;
251 }
252
253 return ret;
254 }
255
256 /*
257 * ustcomm_recv_unix_sock
258 *
259 * Receive data of size len in put that data into
260 * the buf param. Using recvmsg API.
261 * Return the size of received data.
262 * Return 0 on orderly shutdown.
263 */
264 ssize_t ustcomm_recv_unix_sock(int sock, void *buf, size_t len)
265 {
266 struct msghdr msg;
267 struct iovec iov[1];
268 ssize_t ret;
269
270 memset(&msg, 0, sizeof(msg));
271
272 iov[0].iov_base = buf;
273 iov[0].iov_len = len;
274 msg.msg_iov = iov;
275 msg.msg_iovlen = 1;
276
277 do {
278 ret = recvmsg(sock, &msg, 0);
279 } while (ret < 0 && errno == EINTR);
280
281 if (ret < 0) {
282 int shutret;
283
284 if (errno != EPIPE && errno != ECONNRESET && errno != ECONNREFUSED)
285 PERROR("recvmsg");
286 ret = -errno;
287 if (ret == -ECONNRESET || ret == -ECONNREFUSED)
288 ret = -EPIPE;
289
290 shutret = shutdown(sock, SHUT_RDWR);
291 if (shutret)
292 ERR("Socket shutdown error");
293 }
294
295 return ret;
296 }
297
298 /*
299 * ustcomm_send_unix_sock
300 *
301 * Send buf data of size len. Using sendmsg API.
302 * Return the size of sent data.
303 */
304 ssize_t ustcomm_send_unix_sock(int sock, const void *buf, size_t len)
305 {
306 struct msghdr msg;
307 struct iovec iov[1];
308 ssize_t ret;
309
310 memset(&msg, 0, sizeof(msg));
311
312 iov[0].iov_base = (void *) buf;
313 iov[0].iov_len = len;
314 msg.msg_iov = iov;
315 msg.msg_iovlen = 1;
316
317 /*
318 * Using the MSG_NOSIGNAL when sending data from sessiond to
319 * libust, so libust does not receive an unhandled SIGPIPE or
320 * SIGURG. The sessiond receiver side can be made more resilient
321 * by ignoring SIGPIPE, but we don't have this luxury on the
322 * libust side.
323 */
324 do {
325 ret = sendmsg(sock, &msg, MSG_NOSIGNAL);
326 } while (ret < 0 && errno == EINTR);
327
328 if (ret < 0) {
329 int shutret;
330
331 if (errno != EPIPE && errno != ECONNRESET)
332 PERROR("sendmsg");
333 ret = -errno;
334 if (ret == -ECONNRESET)
335 ret = -EPIPE;
336
337 shutret = shutdown(sock, SHUT_RDWR);
338 if (shutret)
339 ERR("Socket shutdown error");
340 }
341
342 return ret;
343 }
344
345 /*
346 * Send a message accompanied by fd(s) over a unix socket.
347 *
348 * Returns the size of data sent, or negative error value.
349 */
350 ssize_t ustcomm_send_fds_unix_sock(int sock, int *fds, size_t nb_fd)
351 {
352 struct msghdr msg;
353 struct cmsghdr *cmptr;
354 struct iovec iov[1];
355 ssize_t ret = -1;
356 unsigned int sizeof_fds = nb_fd * sizeof(int);
357 char tmp[CMSG_SPACE(sizeof_fds)];
358 char dummy = 0;
359
360 memset(&msg, 0, sizeof(msg));
361 memset(tmp, 0, CMSG_SPACE(sizeof_fds) * sizeof(char));
362
363 if (nb_fd > USTCOMM_MAX_SEND_FDS)
364 return -EINVAL;
365
366 msg.msg_control = (caddr_t)tmp;
367 msg.msg_controllen = CMSG_LEN(sizeof_fds);
368
369 cmptr = CMSG_FIRSTHDR(&msg);
370 cmptr->cmsg_level = SOL_SOCKET;
371 cmptr->cmsg_type = SCM_RIGHTS;
372 cmptr->cmsg_len = CMSG_LEN(sizeof_fds);
373 memcpy(CMSG_DATA(cmptr), fds, sizeof_fds);
374 /* Sum of the length of all control messages in the buffer: */
375 msg.msg_controllen = cmptr->cmsg_len;
376
377 iov[0].iov_base = &dummy;
378 iov[0].iov_len = 1;
379 msg.msg_iov = iov;
380 msg.msg_iovlen = 1;
381
382 do {
383 ret = sendmsg(sock, &msg, 0);
384 } while (ret < 0 && errno == EINTR);
385 if (ret < 0) {
386 /*
387 * We consider EPIPE and ECONNRESET as expected.
388 */
389 if (errno != EPIPE && errno != ECONNRESET) {
390 PERROR("sendmsg");
391 }
392 ret = -errno;
393 if (ret == -ECONNRESET)
394 ret = -EPIPE;
395 }
396 return ret;
397 }
398
399 /*
400 * Recv a message accompanied by fd(s) from a unix socket.
401 *
402 * Returns the size of received data, or negative error value.
403 *
404 * Expect at most "nb_fd" file descriptors. Returns the number of fd
405 * actually received in nb_fd.
406 * Returns -EPIPE on orderly shutdown.
407 */
408 ssize_t ustcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd)
409 {
410 struct iovec iov[1];
411 ssize_t ret = 0;
412 struct cmsghdr *cmsg;
413 size_t sizeof_fds = nb_fd * sizeof(int);
414 char recv_fd[CMSG_SPACE(sizeof_fds)];
415 struct msghdr msg;
416 char dummy;
417
418 memset(&msg, 0, sizeof(msg));
419
420 /* Prepare to receive the structures */
421 iov[0].iov_base = &dummy;
422 iov[0].iov_len = 1;
423 msg.msg_iov = iov;
424 msg.msg_iovlen = 1;
425 msg.msg_control = recv_fd;
426 msg.msg_controllen = sizeof(recv_fd);
427
428 do {
429 ret = recvmsg(sock, &msg, 0);
430 } while (ret < 0 && errno == EINTR);
431 if (ret < 0) {
432 if (errno != EPIPE && errno != ECONNRESET) {
433 PERROR("recvmsg fds");
434 }
435 ret = -errno;
436 if (ret == -ECONNRESET)
437 ret = -EPIPE;
438 goto end;
439 }
440 if (ret == 0) {
441 /* orderly shutdown */
442 ret = -EPIPE;
443 goto end;
444 }
445 if (ret != 1) {
446 ERR("Error: Received %zd bytes, expected %d\n",
447 ret, 1);
448 goto end;
449 }
450 if (msg.msg_flags & MSG_CTRUNC) {
451 ERR("Error: Control message truncated.\n");
452 ret = -1;
453 goto end;
454 }
455 cmsg = CMSG_FIRSTHDR(&msg);
456 if (!cmsg) {
457 ERR("Error: Invalid control message header\n");
458 ret = -1;
459 goto end;
460 }
461 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
462 ERR("Didn't received any fd\n");
463 ret = -1;
464 goto end;
465 }
466 if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) {
467 ERR("Error: Received %zu bytes of ancillary data, expected %zu\n",
468 (size_t) cmsg->cmsg_len, (size_t) CMSG_LEN(sizeof_fds));
469 ret = -1;
470 goto end;
471 }
472 memcpy(fds, CMSG_DATA(cmsg), sizeof_fds);
473 ret = sizeof_fds;
474 end:
475 return ret;
476 }
477
478 int ustcomm_send_app_msg(int sock, struct ustcomm_ust_msg *lum)
479 {
480 ssize_t len;
481
482 len = ustcomm_send_unix_sock(sock, lum, sizeof(*lum));
483 switch (len) {
484 case sizeof(*lum):
485 break;
486 default:
487 if (len < 0) {
488 return len;
489 } else {
490 ERR("incorrect message size: %zd\n", len);
491 return -EINVAL;
492 }
493 }
494 return 0;
495 }
496
497 int ustcomm_recv_app_reply(int sock, struct ustcomm_ust_reply *lur,
498 uint32_t expected_handle, uint32_t expected_cmd)
499 {
500 ssize_t len;
501
502 memset(lur, 0, sizeof(*lur));
503 len = ustcomm_recv_unix_sock(sock, lur, sizeof(*lur));
504 switch (len) {
505 case 0: /* orderly shutdown */
506 return -EPIPE;
507 case sizeof(*lur):
508 if (lur->handle != expected_handle) {
509 ERR("Unexpected result message handle: "
510 "expected: %u vs received: %u\n",
511 expected_handle, lur->handle);
512 return -EINVAL;
513 }
514 if (lur->cmd != expected_cmd) {
515 ERR("Unexpected result message command "
516 "expected: %u vs received: %u\n",
517 expected_cmd, lur->cmd);
518 return -EINVAL;
519 }
520 return lur->ret_code;
521 default:
522 if (len >= 0) {
523 ERR("incorrect message size: %zd\n", len);
524 }
525 return len;
526 }
527 }
528
529 int ustcomm_send_app_cmd(int sock,
530 struct ustcomm_ust_msg *lum,
531 struct ustcomm_ust_reply *lur)
532 {
533 int ret;
534
535 ret = ustcomm_send_app_msg(sock, lum);
536 if (ret)
537 return ret;
538 ret = ustcomm_recv_app_reply(sock, lur, lum->handle, lum->cmd);
539 if (ret > 0)
540 return -EIO;
541 return ret;
542 }
543
544 /*
545 * chan_data is allocated internally if this function returns the
546 * expected var_len.
547 */
548 ssize_t ustcomm_recv_channel_from_sessiond(int sock,
549 void **_chan_data, uint64_t var_len,
550 int *_wakeup_fd)
551 {
552 void *chan_data;
553 ssize_t len, nr_fd;
554 int wakeup_fd;
555
556 if (var_len > LTTNG_UST_CHANNEL_DATA_MAX_LEN) {
557 len = -EINVAL;
558 goto error_check;
559 }
560 /* Receive variable length data */
561 chan_data = zmalloc(var_len);
562 if (!chan_data) {
563 len = -ENOMEM;
564 goto error_alloc;
565 }
566 len = ustcomm_recv_unix_sock(sock, chan_data, var_len);
567 if (len != var_len) {
568 goto error_recv;
569 }
570 /* recv wakeup fd */
571 nr_fd = ustcomm_recv_fds_unix_sock(sock, &wakeup_fd, 1);
572 if (nr_fd <= 0) {
573 if (nr_fd < 0) {
574 len = nr_fd;
575 goto error_recv;
576 } else {
577 len = -EIO;
578 goto error_recv;
579 }
580 }
581 *_wakeup_fd = wakeup_fd;
582 *_chan_data = chan_data;
583 return len;
584
585 error_recv:
586 free(chan_data);
587 error_alloc:
588 error_check:
589 return len;
590 }
591
592 int ustcomm_recv_stream_from_sessiond(int sock,
593 uint64_t *memory_map_size,
594 int *shm_fd, int *wakeup_fd)
595 {
596 ssize_t len;
597 int ret;
598 int fds[2];
599
600 /* recv shm fd and wakeup fd */
601 len = ustcomm_recv_fds_unix_sock(sock, fds, 2);
602 if (len <= 0) {
603 if (len < 0) {
604 ret = len;
605 goto error;
606 } else {
607 ret = -EIO;
608 goto error;
609 }
610 }
611 *shm_fd = fds[0];
612 *wakeup_fd = fds[1];
613 return 0;
614
615 error:
616 return ret;
617 }
618
619 /*
620 * Returns 0 on success, negative error value on error.
621 */
622 int ustcomm_send_reg_msg(int sock,
623 enum ustctl_socket_type type,
624 uint32_t bits_per_long,
625 uint32_t uint8_t_alignment,
626 uint32_t uint16_t_alignment,
627 uint32_t uint32_t_alignment,
628 uint32_t uint64_t_alignment,
629 uint32_t long_alignment)
630 {
631 ssize_t len;
632 struct ustctl_reg_msg reg_msg;
633
634 reg_msg.magic = LTTNG_UST_COMM_MAGIC;
635 reg_msg.major = LTTNG_UST_ABI_MAJOR_VERSION;
636 reg_msg.minor = LTTNG_UST_ABI_MINOR_VERSION;
637 reg_msg.pid = getpid();
638 reg_msg.ppid = getppid();
639 reg_msg.uid = getuid();
640 reg_msg.gid = getgid();
641 reg_msg.bits_per_long = bits_per_long;
642 reg_msg.uint8_t_alignment = uint8_t_alignment;
643 reg_msg.uint16_t_alignment = uint16_t_alignment;
644 reg_msg.uint32_t_alignment = uint32_t_alignment;
645 reg_msg.uint64_t_alignment = uint64_t_alignment;
646 reg_msg.long_alignment = long_alignment;
647 reg_msg.socket_type = type;
648 lttng_ust_getprocname(reg_msg.name);
649 memset(reg_msg.padding, 0, sizeof(reg_msg.padding));
650
651 len = ustcomm_send_unix_sock(sock, &reg_msg, sizeof(reg_msg));
652 if (len > 0 && len != sizeof(reg_msg))
653 return -EIO;
654 if (len < 0)
655 return len;
656 return 0;
657 }
658
659 static
660 int serialize_basic_type(enum ustctl_abstract_types *uatype,
661 enum lttng_abstract_types atype,
662 union _ustctl_basic_type *ubt,
663 const union _lttng_basic_type *lbt)
664 {
665 switch (atype) {
666 case atype_integer:
667 {
668 struct ustctl_integer_type *uit;
669 const struct lttng_integer_type *lit;
670
671 uit = &ubt->integer;
672 lit = &lbt->integer;
673 uit->size = lit->size;
674 uit->signedness = lit->signedness;
675 uit->reverse_byte_order = lit->reverse_byte_order;
676 uit->base = lit->base;
677 uit->encoding = lit->encoding;
678 uit->alignment = lit->alignment;
679 *uatype = ustctl_atype_integer;
680 break;
681 }
682 case atype_string:
683 {
684 ubt->string.encoding = lbt->string.encoding;
685 *uatype = ustctl_atype_string;
686 break;
687 }
688 case atype_float:
689 {
690 struct ustctl_float_type *uft;
691 const struct lttng_float_type *lft;
692
693 uft = &ubt->_float;
694 lft = &lbt->_float;
695 uft->exp_dig = lft->exp_dig;
696 uft->mant_dig = lft->mant_dig;
697 uft->alignment = lft->alignment;
698 uft->reverse_byte_order = lft->reverse_byte_order;
699 *uatype = ustctl_atype_float;
700 break;
701 }
702 case atype_enum:
703 case atype_array:
704 case atype_sequence:
705 default:
706 return -EINVAL;
707 }
708 return 0;
709 }
710
711 static
712 int serialize_one_type(struct ustctl_type *ut, const struct lttng_type *lt)
713 {
714 int ret;
715
716 switch (lt->atype) {
717 case atype_integer:
718 case atype_float:
719 case atype_string:
720 ret = serialize_basic_type(&ut->atype, lt->atype,
721 &ut->u.basic, &lt->u.basic);
722 if (ret)
723 return ret;
724 break;
725 case atype_array:
726 {
727 struct ustctl_basic_type *ubt;
728 const struct lttng_basic_type *lbt;
729 int ret;
730
731 ubt = &ut->u.array.elem_type;
732 lbt = &lt->u.array.elem_type;
733 ut->u.array.length = lt->u.array.length;
734 ret = serialize_basic_type(&ubt->atype, lbt->atype,
735 &ubt->u.basic, &lbt->u.basic);
736 if (ret)
737 return -EINVAL;
738 ut->atype = ustctl_atype_array;
739 break;
740 }
741 case atype_sequence:
742 {
743 struct ustctl_basic_type *ubt;
744 const struct lttng_basic_type *lbt;
745 int ret;
746
747 ubt = &ut->u.sequence.length_type;
748 lbt = &lt->u.sequence.length_type;
749 ret = serialize_basic_type(&ubt->atype, lbt->atype,
750 &ubt->u.basic, &lbt->u.basic);
751 if (ret)
752 return -EINVAL;
753 ubt = &ut->u.sequence.elem_type;
754 lbt = &lt->u.sequence.elem_type;
755 ret = serialize_basic_type(&ubt->atype, lbt->atype,
756 &ubt->u.basic, &lbt->u.basic);
757 if (ret)
758 return -EINVAL;
759 ut->atype = ustctl_atype_sequence;
760 break;
761 }
762 case atype_enum:
763 default:
764 return -EINVAL;
765 }
766 return 0;
767 }
768
769 static
770 int serialize_fields(size_t *_nr_write_fields,
771 struct ustctl_field **ustctl_fields,
772 size_t nr_fields,
773 const struct lttng_event_field *lttng_fields)
774 {
775 struct ustctl_field *fields;
776 int i, ret;
777 size_t nr_write_fields = 0;
778
779 fields = zmalloc(nr_fields * sizeof(*fields));
780 if (!fields)
781 return -ENOMEM;
782
783 for (i = 0; i < nr_fields; i++) {
784 struct ustctl_field *f;
785 const struct lttng_event_field *lf;
786
787 f = &fields[nr_write_fields];
788 lf = &lttng_fields[i];
789
790 /* skip 'nowrite' fields */
791 if (lf->nowrite)
792 continue;
793 strncpy(f->name, lf->name, LTTNG_UST_SYM_NAME_LEN);
794 f->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
795 ret = serialize_one_type(&f->type, &lf->type);
796 if (ret)
797 goto error_type;
798 nr_write_fields++;
799 }
800
801 *_nr_write_fields = nr_write_fields;
802 *ustctl_fields = fields;
803 return 0;
804
805 error_type:
806 free(fields);
807 return ret;
808 }
809
810 /*
811 * Returns 0 on success, negative error value on error.
812 */
813 int ustcomm_register_event(int sock,
814 int session_objd, /* session descriptor */
815 int channel_objd, /* channel descriptor */
816 const char *event_name, /* event name (input) */
817 int loglevel,
818 const char *signature, /* event signature (input) */
819 size_t nr_fields, /* fields */
820 const struct lttng_event_field *lttng_fields,
821 const char *model_emf_uri,
822 uint32_t *id) /* event id (output) */
823 {
824 ssize_t len;
825 struct {
826 struct ustcomm_notify_hdr header;
827 struct ustcomm_notify_event_msg m;
828 } msg;
829 struct {
830 struct ustcomm_notify_hdr header;
831 struct ustcomm_notify_event_reply r;
832 } reply;
833 size_t signature_len, fields_len, model_emf_uri_len;
834 struct ustctl_field *fields;
835 size_t nr_write_fields = 0;
836 int ret;
837
838 memset(&msg, 0, sizeof(msg));
839 msg.header.notify_cmd = USTCTL_NOTIFY_CMD_EVENT;
840 msg.m.session_objd = session_objd;
841 msg.m.channel_objd = channel_objd;
842 strncpy(msg.m.event_name, event_name, LTTNG_UST_SYM_NAME_LEN);
843 msg.m.event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
844 msg.m.loglevel = loglevel;
845 signature_len = strlen(signature) + 1;
846 msg.m.signature_len = signature_len;
847
848 /* Calculate fields len, serialize fields. */
849 if (nr_fields > 0) {
850 ret = serialize_fields(&nr_write_fields, &fields,
851 nr_fields, lttng_fields);
852 if (ret)
853 return ret;
854 }
855
856 fields_len = sizeof(*fields) * nr_write_fields;
857 msg.m.fields_len = fields_len;
858 if (model_emf_uri) {
859 model_emf_uri_len = strlen(model_emf_uri) + 1;
860 } else {
861 model_emf_uri_len = 0;
862 }
863 msg.m.model_emf_uri_len = model_emf_uri_len;
864 len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg));
865 if (len > 0 && len != sizeof(msg)) {
866 free(fields);
867 return -EIO;
868 }
869 if (len < 0) {
870 free(fields);
871 return len;
872 }
873
874 /* send signature */
875 len = ustcomm_send_unix_sock(sock, signature, signature_len);
876 if (len > 0 && len != signature_len) {
877 free(fields);
878 return -EIO;
879 }
880 if (len < 0) {
881 free(fields);
882 return len;
883 }
884
885 /* send fields */
886 if (fields_len > 0) {
887 len = ustcomm_send_unix_sock(sock, fields, fields_len);
888 free(fields);
889 if (len > 0 && len != fields_len) {
890 return -EIO;
891 }
892 if (len < 0) {
893 return len;
894 }
895 }
896
897 if (model_emf_uri_len) {
898 /* send model_emf_uri */
899 len = ustcomm_send_unix_sock(sock, model_emf_uri,
900 model_emf_uri_len);
901 if (len > 0 && len != model_emf_uri_len)
902 return -EIO;
903 if (len < 0)
904 return len;
905 }
906
907 /* receive reply */
908 len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply));
909 switch (len) {
910 case 0: /* orderly shutdown */
911 return -EPIPE;
912 case sizeof(reply):
913 if (reply.header.notify_cmd != msg.header.notify_cmd) {
914 ERR("Unexpected result message command "
915 "expected: %u vs received: %u\n",
916 msg.header.notify_cmd, reply.header.notify_cmd);
917 return -EINVAL;
918 }
919 if (reply.r.ret_code > 0)
920 return -EINVAL;
921 if (reply.r.ret_code < 0)
922 return reply.r.ret_code;
923 *id = reply.r.event_id;
924 DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n",
925 event_name, reply.r.ret_code, reply.r.event_id);
926 return 0;
927 default:
928 if (len < 0) {
929 /* Transport level error */
930 if (errno == EPIPE || errno == ECONNRESET)
931 len = -errno;
932 return len;
933 } else {
934 ERR("incorrect message size: %zd\n", len);
935 return len;
936 }
937 }
938 }
939
940 /*
941 * Returns 0 on success, negative error value on error.
942 * Returns -EPIPE or -ECONNRESET if other end has hung up.
943 */
944 int ustcomm_register_channel(int sock,
945 int session_objd, /* session descriptor */
946 int channel_objd, /* channel descriptor */
947 size_t nr_ctx_fields,
948 const struct lttng_event_field *ctx_fields,
949 uint32_t *chan_id, /* channel id (output) */
950 int *header_type) /* header type (output) */
951 {
952 ssize_t len;
953 struct {
954 struct ustcomm_notify_hdr header;
955 struct ustcomm_notify_channel_msg m;
956 } msg;
957 struct {
958 struct ustcomm_notify_hdr header;
959 struct ustcomm_notify_channel_reply r;
960 } reply;
961 size_t fields_len;
962 struct ustctl_field *fields;
963 int ret;
964 size_t nr_write_fields = 0;
965
966 memset(&msg, 0, sizeof(msg));
967 msg.header.notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL;
968 msg.m.session_objd = session_objd;
969 msg.m.channel_objd = channel_objd;
970
971 /* Calculate fields len, serialize fields. */
972 if (nr_ctx_fields > 0) {
973 ret = serialize_fields(&nr_write_fields, &fields,
974 nr_ctx_fields, ctx_fields);
975 if (ret)
976 return ret;
977 }
978
979 fields_len = sizeof(*fields) * nr_write_fields;
980 msg.m.ctx_fields_len = fields_len;
981 len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg));
982 if (len > 0 && len != sizeof(msg)) {
983 free(fields);
984 return -EIO;
985 }
986 if (len < 0) {
987 free(fields);
988 return len;
989 }
990
991 /* send fields */
992 if (fields_len > 0) {
993 len = ustcomm_send_unix_sock(sock, fields, fields_len);
994 free(fields);
995 if (len > 0 && len != fields_len) {
996 return -EIO;
997 }
998 if (len < 0) {
999 return len;
1000 }
1001 }
1002
1003 len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply));
1004 switch (len) {
1005 case 0: /* orderly shutdown */
1006 return -EPIPE;
1007 case sizeof(reply):
1008 if (reply.header.notify_cmd != msg.header.notify_cmd) {
1009 ERR("Unexpected result message command "
1010 "expected: %u vs received: %u\n",
1011 msg.header.notify_cmd, reply.header.notify_cmd);
1012 return -EINVAL;
1013 }
1014 if (reply.r.ret_code > 0)
1015 return -EINVAL;
1016 if (reply.r.ret_code < 0)
1017 return reply.r.ret_code;
1018 *chan_id = reply.r.chan_id;
1019 switch (reply.r.header_type) {
1020 case 1:
1021 case 2:
1022 *header_type = reply.r.header_type;
1023 break;
1024 default:
1025 ERR("Unexpected channel header type %u\n",
1026 reply.r.header_type);
1027 return -EINVAL;
1028 }
1029 DBG("Sent register channel notification: chan_id %d, header_type %d\n",
1030 reply.r.chan_id, reply.r.header_type);
1031 return 0;
1032 default:
1033 if (len < 0) {
1034 /* Transport level error */
1035 if (errno == EPIPE || errno == ECONNRESET)
1036 len = -errno;
1037 return len;
1038 } else {
1039 ERR("incorrect message size: %zd\n", len);
1040 return len;
1041 }
1042 }
1043 }
1044
1045 /*
1046 * Set socket reciving timeout.
1047 */
1048 int ustcomm_setsockopt_rcv_timeout(int sock, unsigned int msec)
1049 {
1050 int ret;
1051 struct timeval tv;
1052
1053 tv.tv_sec = msec / 1000;
1054 tv.tv_usec = (msec * 1000 % 1000000);
1055
1056 ret = setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
1057 if (ret < 0) {
1058 PERROR("setsockopt SO_RCVTIMEO");
1059 ret = -errno;
1060 }
1061
1062 return ret;
1063 }
1064
1065 /*
1066 * Set socket sending timeout.
1067 */
1068 int ustcomm_setsockopt_snd_timeout(int sock, unsigned int msec)
1069 {
1070 int ret;
1071 struct timeval tv;
1072
1073 tv.tv_sec = msec / 1000;
1074 tv.tv_usec = (msec * 1000) % 1000000;
1075
1076 ret = setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
1077 if (ret < 0) {
1078 PERROR("setsockopt SO_SNDTIMEO");
1079 ret = -errno;
1080 }
1081
1082 return ret;
1083 }
This page took 0.081171 seconds and 5 git commands to generate.