087424fe66626a6dd393315ccb3fa896c54322ff
[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 && errno != EACCES)
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_string_encoding(enum ustctl_string_encodings *ue,
661 enum lttng_string_encodings le)
662 {
663 switch (le) {
664 case lttng_encode_none:
665 *ue = ustctl_encode_none;
666 break;
667 case lttng_encode_UTF8:
668 *ue = ustctl_encode_UTF8;
669 break;
670 case lttng_encode_ASCII:
671 *ue = ustctl_encode_ASCII;
672 break;
673 default:
674 return -EINVAL;
675 }
676 return 0;
677 }
678
679 static
680 int serialize_basic_type(enum ustctl_abstract_types *uatype,
681 enum lttng_abstract_types atype,
682 union _ustctl_basic_type *ubt,
683 const union _lttng_basic_type *lbt)
684 {
685 switch (atype) {
686 case atype_integer:
687 {
688 struct ustctl_integer_type *uit;
689 const struct lttng_integer_type *lit;
690
691 uit = &ubt->integer;
692 lit = &lbt->integer;
693 uit->size = lit->size;
694 uit->signedness = lit->signedness;
695 uit->reverse_byte_order = lit->reverse_byte_order;
696 uit->base = lit->base;
697 if (serialize_string_encoding(&uit->encoding, lit->encoding))
698 return -EINVAL;
699 uit->alignment = lit->alignment;
700 *uatype = ustctl_atype_integer;
701 break;
702 }
703 case atype_string:
704 {
705 if (serialize_string_encoding(&ubt->string.encoding,
706 lbt->string.encoding))
707 return -EINVAL;
708 *uatype = ustctl_atype_string;
709 break;
710 }
711 case atype_float:
712 {
713 struct ustctl_float_type *uft;
714 const struct lttng_float_type *lft;
715
716 uft = &ubt->_float;
717 lft = &lbt->_float;
718 uft->exp_dig = lft->exp_dig;
719 uft->mant_dig = lft->mant_dig;
720 uft->alignment = lft->alignment;
721 uft->reverse_byte_order = lft->reverse_byte_order;
722 *uatype = ustctl_atype_float;
723 break;
724 }
725 case atype_enum:
726 case atype_array:
727 case atype_sequence:
728 default:
729 return -EINVAL;
730 }
731 return 0;
732 }
733
734 static
735 int serialize_one_type(struct ustctl_type *ut, const struct lttng_type *lt)
736 {
737 int ret;
738
739 switch (lt->atype) {
740 case atype_integer:
741 case atype_float:
742 case atype_string:
743 ret = serialize_basic_type(&ut->atype, lt->atype,
744 &ut->u.basic, &lt->u.basic);
745 if (ret)
746 return ret;
747 break;
748 case atype_array:
749 {
750 struct ustctl_basic_type *ubt;
751 const struct lttng_basic_type *lbt;
752 int ret;
753
754 ubt = &ut->u.array.elem_type;
755 lbt = &lt->u.array.elem_type;
756 ut->u.array.length = lt->u.array.length;
757 ret = serialize_basic_type(&ubt->atype, lbt->atype,
758 &ubt->u.basic, &lbt->u.basic);
759 if (ret)
760 return -EINVAL;
761 ut->atype = ustctl_atype_array;
762 break;
763 }
764 case atype_sequence:
765 {
766 struct ustctl_basic_type *ubt;
767 const struct lttng_basic_type *lbt;
768 int ret;
769
770 ubt = &ut->u.sequence.length_type;
771 lbt = &lt->u.sequence.length_type;
772 ret = serialize_basic_type(&ubt->atype, lbt->atype,
773 &ubt->u.basic, &lbt->u.basic);
774 if (ret)
775 return -EINVAL;
776 ubt = &ut->u.sequence.elem_type;
777 lbt = &lt->u.sequence.elem_type;
778 ret = serialize_basic_type(&ubt->atype, lbt->atype,
779 &ubt->u.basic, &lbt->u.basic);
780 if (ret)
781 return -EINVAL;
782 ut->atype = ustctl_atype_sequence;
783 break;
784 }
785 case atype_enum:
786 default:
787 return -EINVAL;
788 }
789 return 0;
790 }
791
792 static
793 int serialize_fields(size_t *_nr_write_fields,
794 struct ustctl_field **ustctl_fields,
795 size_t nr_fields,
796 const struct lttng_event_field *lttng_fields)
797 {
798 struct ustctl_field *fields;
799 int i, ret;
800 size_t nr_write_fields = 0;
801
802 fields = zmalloc(nr_fields * sizeof(*fields));
803 if (!fields)
804 return -ENOMEM;
805
806 for (i = 0; i < nr_fields; i++) {
807 struct ustctl_field *f;
808 const struct lttng_event_field *lf;
809
810 f = &fields[nr_write_fields];
811 lf = &lttng_fields[i];
812
813 /* skip 'nowrite' fields */
814 if (lf->nowrite)
815 continue;
816 strncpy(f->name, lf->name, LTTNG_UST_SYM_NAME_LEN);
817 f->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
818 ret = serialize_one_type(&f->type, &lf->type);
819 if (ret)
820 goto error_type;
821 nr_write_fields++;
822 }
823
824 *_nr_write_fields = nr_write_fields;
825 *ustctl_fields = fields;
826 return 0;
827
828 error_type:
829 free(fields);
830 return ret;
831 }
832
833 /*
834 * Returns 0 on success, negative error value on error.
835 */
836 int ustcomm_register_event(int sock,
837 int session_objd, /* session descriptor */
838 int channel_objd, /* channel descriptor */
839 const char *event_name, /* event name (input) */
840 int loglevel,
841 const char *signature, /* event signature (input) */
842 size_t nr_fields, /* fields */
843 const struct lttng_event_field *lttng_fields,
844 const char *model_emf_uri,
845 uint32_t *id) /* event id (output) */
846 {
847 ssize_t len;
848 struct {
849 struct ustcomm_notify_hdr header;
850 struct ustcomm_notify_event_msg m;
851 } msg;
852 struct {
853 struct ustcomm_notify_hdr header;
854 struct ustcomm_notify_event_reply r;
855 } reply;
856 size_t signature_len, fields_len, model_emf_uri_len;
857 struct ustctl_field *fields = NULL;
858 size_t nr_write_fields = 0;
859 int ret;
860
861 memset(&msg, 0, sizeof(msg));
862 msg.header.notify_cmd = USTCTL_NOTIFY_CMD_EVENT;
863 msg.m.session_objd = session_objd;
864 msg.m.channel_objd = channel_objd;
865 strncpy(msg.m.event_name, event_name, LTTNG_UST_SYM_NAME_LEN);
866 msg.m.event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
867 msg.m.loglevel = loglevel;
868 signature_len = strlen(signature) + 1;
869 msg.m.signature_len = signature_len;
870
871 /* Calculate fields len, serialize fields. */
872 if (nr_fields > 0) {
873 ret = serialize_fields(&nr_write_fields, &fields,
874 nr_fields, lttng_fields);
875 if (ret)
876 return ret;
877 }
878
879 fields_len = sizeof(*fields) * nr_write_fields;
880 msg.m.fields_len = fields_len;
881 if (model_emf_uri) {
882 model_emf_uri_len = strlen(model_emf_uri) + 1;
883 } else {
884 model_emf_uri_len = 0;
885 }
886 msg.m.model_emf_uri_len = model_emf_uri_len;
887 len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg));
888 if (len > 0 && len != sizeof(msg)) {
889 free(fields);
890 return -EIO;
891 }
892 if (len < 0) {
893 free(fields);
894 return len;
895 }
896
897 /* send signature */
898 len = ustcomm_send_unix_sock(sock, signature, signature_len);
899 if (len > 0 && len != signature_len) {
900 free(fields);
901 return -EIO;
902 }
903 if (len < 0) {
904 free(fields);
905 return len;
906 }
907
908 /* send fields */
909 if (fields_len > 0) {
910 len = ustcomm_send_unix_sock(sock, fields, fields_len);
911 free(fields);
912 if (len > 0 && len != fields_len) {
913 return -EIO;
914 }
915 if (len < 0) {
916 return len;
917 }
918 }
919
920 if (model_emf_uri_len) {
921 /* send model_emf_uri */
922 len = ustcomm_send_unix_sock(sock, model_emf_uri,
923 model_emf_uri_len);
924 if (len > 0 && len != model_emf_uri_len)
925 return -EIO;
926 if (len < 0)
927 return len;
928 }
929
930 /* receive reply */
931 len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply));
932 switch (len) {
933 case 0: /* orderly shutdown */
934 return -EPIPE;
935 case sizeof(reply):
936 if (reply.header.notify_cmd != msg.header.notify_cmd) {
937 ERR("Unexpected result message command "
938 "expected: %u vs received: %u\n",
939 msg.header.notify_cmd, reply.header.notify_cmd);
940 return -EINVAL;
941 }
942 if (reply.r.ret_code > 0)
943 return -EINVAL;
944 if (reply.r.ret_code < 0)
945 return reply.r.ret_code;
946 *id = reply.r.event_id;
947 DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n",
948 event_name, reply.r.ret_code, reply.r.event_id);
949 return 0;
950 default:
951 if (len < 0) {
952 /* Transport level error */
953 if (errno == EPIPE || errno == ECONNRESET)
954 len = -errno;
955 return len;
956 } else {
957 ERR("incorrect message size: %zd\n", len);
958 return len;
959 }
960 }
961 }
962
963 /*
964 * Returns 0 on success, negative error value on error.
965 * Returns -EPIPE or -ECONNRESET if other end has hung up.
966 */
967 int ustcomm_register_channel(int sock,
968 int session_objd, /* session descriptor */
969 int channel_objd, /* channel descriptor */
970 size_t nr_ctx_fields,
971 const struct lttng_event_field *ctx_fields,
972 uint32_t *chan_id, /* channel id (output) */
973 int *header_type) /* header type (output) */
974 {
975 ssize_t len;
976 struct {
977 struct ustcomm_notify_hdr header;
978 struct ustcomm_notify_channel_msg m;
979 } msg;
980 struct {
981 struct ustcomm_notify_hdr header;
982 struct ustcomm_notify_channel_reply r;
983 } reply;
984 size_t fields_len;
985 struct ustctl_field *fields;
986 int ret;
987 size_t nr_write_fields = 0;
988
989 memset(&msg, 0, sizeof(msg));
990 msg.header.notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL;
991 msg.m.session_objd = session_objd;
992 msg.m.channel_objd = channel_objd;
993
994 /* Calculate fields len, serialize fields. */
995 if (nr_ctx_fields > 0) {
996 ret = serialize_fields(&nr_write_fields, &fields,
997 nr_ctx_fields, ctx_fields);
998 if (ret)
999 return ret;
1000 }
1001
1002 fields_len = sizeof(*fields) * nr_write_fields;
1003 msg.m.ctx_fields_len = fields_len;
1004 len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg));
1005 if (len > 0 && len != sizeof(msg)) {
1006 free(fields);
1007 return -EIO;
1008 }
1009 if (len < 0) {
1010 free(fields);
1011 return len;
1012 }
1013
1014 /* send fields */
1015 if (fields_len > 0) {
1016 len = ustcomm_send_unix_sock(sock, fields, fields_len);
1017 free(fields);
1018 if (len > 0 && len != fields_len) {
1019 return -EIO;
1020 }
1021 if (len < 0) {
1022 return len;
1023 }
1024 }
1025
1026 len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply));
1027 switch (len) {
1028 case 0: /* orderly shutdown */
1029 return -EPIPE;
1030 case sizeof(reply):
1031 if (reply.header.notify_cmd != msg.header.notify_cmd) {
1032 ERR("Unexpected result message command "
1033 "expected: %u vs received: %u\n",
1034 msg.header.notify_cmd, reply.header.notify_cmd);
1035 return -EINVAL;
1036 }
1037 if (reply.r.ret_code > 0)
1038 return -EINVAL;
1039 if (reply.r.ret_code < 0)
1040 return reply.r.ret_code;
1041 *chan_id = reply.r.chan_id;
1042 switch (reply.r.header_type) {
1043 case 1:
1044 case 2:
1045 *header_type = reply.r.header_type;
1046 break;
1047 default:
1048 ERR("Unexpected channel header type %u\n",
1049 reply.r.header_type);
1050 return -EINVAL;
1051 }
1052 DBG("Sent register channel notification: chan_id %d, header_type %d\n",
1053 reply.r.chan_id, reply.r.header_type);
1054 return 0;
1055 default:
1056 if (len < 0) {
1057 /* Transport level error */
1058 if (errno == EPIPE || errno == ECONNRESET)
1059 len = -errno;
1060 return len;
1061 } else {
1062 ERR("incorrect message size: %zd\n", len);
1063 return len;
1064 }
1065 }
1066 }
1067
1068 /*
1069 * Set socket reciving timeout.
1070 */
1071 int ustcomm_setsockopt_rcv_timeout(int sock, unsigned int msec)
1072 {
1073 int ret;
1074 struct timeval tv;
1075
1076 tv.tv_sec = msec / 1000;
1077 tv.tv_usec = (msec * 1000 % 1000000);
1078
1079 ret = setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
1080 if (ret < 0) {
1081 PERROR("setsockopt SO_RCVTIMEO");
1082 ret = -errno;
1083 }
1084
1085 return ret;
1086 }
1087
1088 /*
1089 * Set socket sending timeout.
1090 */
1091 int ustcomm_setsockopt_snd_timeout(int sock, unsigned int msec)
1092 {
1093 int ret;
1094 struct timeval tv;
1095
1096 tv.tv_sec = msec / 1000;
1097 tv.tv_usec = (msec * 1000) % 1000000;
1098
1099 ret = setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
1100 if (ret < 0) {
1101 PERROR("setsockopt SO_SNDTIMEO");
1102 ret = -errno;
1103 }
1104
1105 return ret;
1106 }
This page took 0.053991 seconds and 3 git commands to generate.