Add CTF enum type support to tracepoint event
[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 = -1;
269 size_t len_last;
270
271 memset(&msg, 0, sizeof(msg));
272
273 iov[0].iov_base = buf;
274 iov[0].iov_len = len;
275 msg.msg_iov = iov;
276 msg.msg_iovlen = 1;
277
278 do {
279 len_last = iov[0].iov_len;
280 ret = recvmsg(sock, &msg, 0);
281 if (ret > 0) {
282 iov[0].iov_base += ret;
283 iov[0].iov_len -= ret;
284 assert(ret <= len_last);
285 }
286 } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR));
287
288 if (ret < 0) {
289 int shutret;
290
291 if (errno != EPIPE && errno != ECONNRESET && errno != ECONNREFUSED)
292 PERROR("recvmsg");
293 ret = -errno;
294 if (ret == -ECONNRESET || ret == -ECONNREFUSED)
295 ret = -EPIPE;
296
297 shutret = shutdown(sock, SHUT_RDWR);
298 if (shutret)
299 ERR("Socket shutdown error");
300 } else if (ret > 0) {
301 ret = len;
302 }
303 /* ret = 0 means an orderly shutdown. */
304
305 return ret;
306 }
307
308 /*
309 * ustcomm_send_unix_sock
310 *
311 * Send buf data of size len. Using sendmsg API.
312 * Return the size of sent data.
313 */
314 ssize_t ustcomm_send_unix_sock(int sock, const void *buf, size_t len)
315 {
316 struct msghdr msg;
317 struct iovec iov[1];
318 ssize_t ret;
319
320 memset(&msg, 0, sizeof(msg));
321
322 iov[0].iov_base = (void *) buf;
323 iov[0].iov_len = len;
324 msg.msg_iov = iov;
325 msg.msg_iovlen = 1;
326
327 /*
328 * Using the MSG_NOSIGNAL when sending data from sessiond to
329 * libust, so libust does not receive an unhandled SIGPIPE or
330 * SIGURG. The sessiond receiver side can be made more resilient
331 * by ignoring SIGPIPE, but we don't have this luxury on the
332 * libust side.
333 */
334 do {
335 ret = sendmsg(sock, &msg, MSG_NOSIGNAL);
336 } while (ret < 0 && errno == EINTR);
337
338 if (ret < 0) {
339 int shutret;
340
341 if (errno != EPIPE && errno != ECONNRESET)
342 PERROR("sendmsg");
343 ret = -errno;
344 if (ret == -ECONNRESET)
345 ret = -EPIPE;
346
347 shutret = shutdown(sock, SHUT_RDWR);
348 if (shutret)
349 ERR("Socket shutdown error");
350 }
351
352 return ret;
353 }
354
355 /*
356 * Send a message accompanied by fd(s) over a unix socket.
357 *
358 * Returns the size of data sent, or negative error value.
359 */
360 ssize_t ustcomm_send_fds_unix_sock(int sock, int *fds, size_t nb_fd)
361 {
362 struct msghdr msg;
363 struct cmsghdr *cmptr;
364 struct iovec iov[1];
365 ssize_t ret = -1;
366 unsigned int sizeof_fds = nb_fd * sizeof(int);
367 char tmp[CMSG_SPACE(sizeof_fds)];
368 char dummy = 0;
369
370 memset(&msg, 0, sizeof(msg));
371 memset(tmp, 0, CMSG_SPACE(sizeof_fds) * sizeof(char));
372
373 if (nb_fd > USTCOMM_MAX_SEND_FDS)
374 return -EINVAL;
375
376 msg.msg_control = (caddr_t)tmp;
377 msg.msg_controllen = CMSG_LEN(sizeof_fds);
378
379 cmptr = CMSG_FIRSTHDR(&msg);
380 if (!cmptr)
381 return -EINVAL;
382 cmptr->cmsg_level = SOL_SOCKET;
383 cmptr->cmsg_type = SCM_RIGHTS;
384 cmptr->cmsg_len = CMSG_LEN(sizeof_fds);
385 memcpy(CMSG_DATA(cmptr), fds, sizeof_fds);
386 /* Sum of the length of all control messages in the buffer: */
387 msg.msg_controllen = cmptr->cmsg_len;
388
389 iov[0].iov_base = &dummy;
390 iov[0].iov_len = 1;
391 msg.msg_iov = iov;
392 msg.msg_iovlen = 1;
393
394 do {
395 ret = sendmsg(sock, &msg, MSG_NOSIGNAL);
396 } while (ret < 0 && errno == EINTR);
397 if (ret < 0) {
398 /*
399 * We consider EPIPE and ECONNRESET as expected.
400 */
401 if (errno != EPIPE && errno != ECONNRESET) {
402 PERROR("sendmsg");
403 }
404 ret = -errno;
405 if (ret == -ECONNRESET)
406 ret = -EPIPE;
407 }
408 return ret;
409 }
410
411 /*
412 * Recv a message accompanied by fd(s) from a unix socket.
413 *
414 * Returns the size of received data, or negative error value.
415 *
416 * Expect at most "nb_fd" file descriptors. Returns the number of fd
417 * actually received in nb_fd.
418 * Returns -EPIPE on orderly shutdown.
419 */
420 ssize_t ustcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd)
421 {
422 struct iovec iov[1];
423 ssize_t ret = 0;
424 struct cmsghdr *cmsg;
425 size_t sizeof_fds = nb_fd * sizeof(int);
426 char recv_fd[CMSG_SPACE(sizeof_fds)];
427 struct msghdr msg;
428 char dummy;
429
430 memset(&msg, 0, sizeof(msg));
431
432 /* Prepare to receive the structures */
433 iov[0].iov_base = &dummy;
434 iov[0].iov_len = 1;
435 msg.msg_iov = iov;
436 msg.msg_iovlen = 1;
437 msg.msg_control = recv_fd;
438 msg.msg_controllen = sizeof(recv_fd);
439
440 do {
441 ret = recvmsg(sock, &msg, 0);
442 } while (ret < 0 && errno == EINTR);
443 if (ret < 0) {
444 if (errno != EPIPE && errno != ECONNRESET) {
445 PERROR("recvmsg fds");
446 }
447 ret = -errno;
448 if (ret == -ECONNRESET)
449 ret = -EPIPE;
450 goto end;
451 }
452 if (ret == 0) {
453 /* orderly shutdown */
454 ret = -EPIPE;
455 goto end;
456 }
457 if (ret != 1) {
458 ERR("Error: Received %zd bytes, expected %d\n",
459 ret, 1);
460 goto end;
461 }
462 if (msg.msg_flags & MSG_CTRUNC) {
463 ERR("Error: Control message truncated.\n");
464 ret = -1;
465 goto end;
466 }
467 cmsg = CMSG_FIRSTHDR(&msg);
468 if (!cmsg) {
469 ERR("Error: Invalid control message header\n");
470 ret = -1;
471 goto end;
472 }
473 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
474 ERR("Didn't received any fd\n");
475 ret = -1;
476 goto end;
477 }
478 if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) {
479 ERR("Error: Received %zu bytes of ancillary data, expected %zu\n",
480 (size_t) cmsg->cmsg_len, (size_t) CMSG_LEN(sizeof_fds));
481 ret = -1;
482 goto end;
483 }
484 memcpy(fds, CMSG_DATA(cmsg), sizeof_fds);
485 ret = sizeof_fds;
486 end:
487 return ret;
488 }
489
490 int ustcomm_send_app_msg(int sock, struct ustcomm_ust_msg *lum)
491 {
492 ssize_t len;
493
494 len = ustcomm_send_unix_sock(sock, lum, sizeof(*lum));
495 switch (len) {
496 case sizeof(*lum):
497 break;
498 default:
499 if (len < 0) {
500 return len;
501 } else {
502 ERR("incorrect message size: %zd\n", len);
503 return -EINVAL;
504 }
505 }
506 return 0;
507 }
508
509 int ustcomm_recv_app_reply(int sock, struct ustcomm_ust_reply *lur,
510 uint32_t expected_handle, uint32_t expected_cmd)
511 {
512 ssize_t len;
513
514 memset(lur, 0, sizeof(*lur));
515 len = ustcomm_recv_unix_sock(sock, lur, sizeof(*lur));
516 switch (len) {
517 case 0: /* orderly shutdown */
518 return -EPIPE;
519 case sizeof(*lur):
520 {
521 int err = 0;
522
523 if (lur->handle != expected_handle) {
524 ERR("Unexpected result message handle: "
525 "expected: %u vs received: %u\n",
526 expected_handle, lur->handle);
527 err = 1;
528 }
529 if (lur->cmd != expected_cmd) {
530 ERR("Unexpected result message command "
531 "expected: %u vs received: %u\n",
532 expected_cmd, lur->cmd);
533 err = 1;
534 }
535 if (err) {
536 return -EINVAL;
537 } else {
538 return lur->ret_code;
539 }
540 }
541 default:
542 if (len >= 0) {
543 ERR("incorrect message size: %zd\n", len);
544 }
545 return len;
546 }
547 }
548
549 int ustcomm_send_app_cmd(int sock,
550 struct ustcomm_ust_msg *lum,
551 struct ustcomm_ust_reply *lur)
552 {
553 int ret;
554
555 ret = ustcomm_send_app_msg(sock, lum);
556 if (ret)
557 return ret;
558 ret = ustcomm_recv_app_reply(sock, lur, lum->handle, lum->cmd);
559 if (ret > 0)
560 return -EIO;
561 return ret;
562 }
563
564 /*
565 * chan_data is allocated internally if this function returns the
566 * expected var_len.
567 */
568 ssize_t ustcomm_recv_channel_from_sessiond(int sock,
569 void **_chan_data, uint64_t var_len,
570 int *_wakeup_fd)
571 {
572 void *chan_data;
573 ssize_t len, nr_fd;
574 int wakeup_fd;
575
576 if (var_len > LTTNG_UST_CHANNEL_DATA_MAX_LEN) {
577 len = -EINVAL;
578 goto error_check;
579 }
580 /* Receive variable length data */
581 chan_data = zmalloc(var_len);
582 if (!chan_data) {
583 len = -ENOMEM;
584 goto error_alloc;
585 }
586 len = ustcomm_recv_unix_sock(sock, chan_data, var_len);
587 if (len != var_len) {
588 goto error_recv;
589 }
590 /* recv wakeup fd */
591 nr_fd = ustcomm_recv_fds_unix_sock(sock, &wakeup_fd, 1);
592 if (nr_fd <= 0) {
593 if (nr_fd < 0) {
594 len = nr_fd;
595 goto error_recv;
596 } else {
597 len = -EIO;
598 goto error_recv;
599 }
600 }
601 *_wakeup_fd = wakeup_fd;
602 *_chan_data = chan_data;
603 return len;
604
605 error_recv:
606 free(chan_data);
607 error_alloc:
608 error_check:
609 return len;
610 }
611
612 int ustcomm_recv_stream_from_sessiond(int sock,
613 uint64_t *memory_map_size,
614 int *shm_fd, int *wakeup_fd)
615 {
616 ssize_t len;
617 int ret;
618 int fds[2];
619
620 /* recv shm fd and wakeup fd */
621 len = ustcomm_recv_fds_unix_sock(sock, fds, 2);
622 if (len <= 0) {
623 if (len < 0) {
624 ret = len;
625 goto error;
626 } else {
627 ret = -EIO;
628 goto error;
629 }
630 }
631 *shm_fd = fds[0];
632 *wakeup_fd = fds[1];
633 return 0;
634
635 error:
636 return ret;
637 }
638
639 /*
640 * Returns 0 on success, negative error value on error.
641 */
642 int ustcomm_send_reg_msg(int sock,
643 enum ustctl_socket_type type,
644 uint32_t bits_per_long,
645 uint32_t uint8_t_alignment,
646 uint32_t uint16_t_alignment,
647 uint32_t uint32_t_alignment,
648 uint32_t uint64_t_alignment,
649 uint32_t long_alignment)
650 {
651 ssize_t len;
652 struct ustctl_reg_msg reg_msg;
653
654 reg_msg.magic = LTTNG_UST_COMM_MAGIC;
655 reg_msg.major = LTTNG_UST_ABI_MAJOR_VERSION;
656 reg_msg.minor = LTTNG_UST_ABI_MINOR_VERSION;
657 reg_msg.pid = getpid();
658 reg_msg.ppid = getppid();
659 reg_msg.uid = getuid();
660 reg_msg.gid = getgid();
661 reg_msg.bits_per_long = bits_per_long;
662 reg_msg.uint8_t_alignment = uint8_t_alignment;
663 reg_msg.uint16_t_alignment = uint16_t_alignment;
664 reg_msg.uint32_t_alignment = uint32_t_alignment;
665 reg_msg.uint64_t_alignment = uint64_t_alignment;
666 reg_msg.long_alignment = long_alignment;
667 reg_msg.socket_type = type;
668 lttng_ust_getprocname(reg_msg.name);
669 memset(reg_msg.padding, 0, sizeof(reg_msg.padding));
670
671 len = ustcomm_send_unix_sock(sock, &reg_msg, sizeof(reg_msg));
672 if (len > 0 && len != sizeof(reg_msg))
673 return -EIO;
674 if (len < 0)
675 return len;
676 return 0;
677 }
678
679 static
680 int serialize_string_encoding(enum ustctl_string_encodings *ue,
681 enum lttng_string_encodings le)
682 {
683 switch (le) {
684 case lttng_encode_none:
685 *ue = ustctl_encode_none;
686 break;
687 case lttng_encode_UTF8:
688 *ue = ustctl_encode_UTF8;
689 break;
690 case lttng_encode_ASCII:
691 *ue = ustctl_encode_ASCII;
692 break;
693 default:
694 return -EINVAL;
695 }
696 return 0;
697 }
698
699 static
700 int serialize_integer_type(struct ustctl_integer_type *uit,
701 const struct lttng_integer_type *lit)
702 {
703 uit->size = lit->size;
704 uit->signedness = lit->signedness;
705 uit->reverse_byte_order = lit->reverse_byte_order;
706 uit->base = lit->base;
707 if (serialize_string_encoding(&uit->encoding, lit->encoding))
708 return -EINVAL;
709 uit->alignment = lit->alignment;
710 return 0;
711 }
712
713 static
714 int serialize_basic_type(struct lttng_session *session,
715 enum ustctl_abstract_types *uatype,
716 enum lttng_abstract_types atype,
717 union _ustctl_basic_type *ubt,
718 const union _lttng_basic_type *lbt)
719 {
720 switch (atype) {
721 case atype_integer:
722 {
723 if (serialize_integer_type(&ubt->integer, &lbt->integer))
724 return -EINVAL;
725 *uatype = ustctl_atype_integer;
726 break;
727 }
728 case atype_string:
729 {
730 if (serialize_string_encoding(&ubt->string.encoding,
731 lbt->string.encoding))
732 return -EINVAL;
733 *uatype = ustctl_atype_string;
734 break;
735 }
736 case atype_float:
737 {
738 struct ustctl_float_type *uft;
739 const struct lttng_float_type *lft;
740
741 uft = &ubt->_float;
742 lft = &lbt->_float;
743 uft->exp_dig = lft->exp_dig;
744 uft->mant_dig = lft->mant_dig;
745 uft->alignment = lft->alignment;
746 uft->reverse_byte_order = lft->reverse_byte_order;
747 *uatype = ustctl_atype_float;
748 break;
749 }
750 case atype_enum:
751 {
752 strncpy(ubt->enumeration.name, lbt->enumeration.desc->name,
753 LTTNG_UST_SYM_NAME_LEN);
754 ubt->enumeration.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
755 if (serialize_integer_type(&ubt->enumeration.container_type,
756 &lbt->enumeration.container_type))
757 return -EINVAL;
758 if (session) {
759 const struct lttng_enum *_enum;
760
761 _enum = lttng_ust_enum_get(session,
762 lbt->enumeration.desc->name);
763 if (!_enum)
764 return -EINVAL;
765 ubt->enumeration.id = _enum->id;
766 } else {
767 ubt->enumeration.id = -1ULL;
768 }
769 *uatype = ustctl_atype_enum;
770 break;
771 }
772 case atype_array:
773 case atype_sequence:
774 default:
775 return -EINVAL;
776 }
777 return 0;
778 }
779
780 static
781 int serialize_one_type(struct lttng_session *session,
782 struct ustctl_type *ut, const struct lttng_type *lt)
783 {
784 int ret;
785
786 switch (lt->atype) {
787 case atype_integer:
788 case atype_float:
789 case atype_string:
790 case atype_enum:
791 ret = serialize_basic_type(session, &ut->atype, lt->atype,
792 &ut->u.basic, &lt->u.basic);
793 if (ret)
794 return ret;
795 break;
796 case atype_array:
797 {
798 struct ustctl_basic_type *ubt;
799 const struct lttng_basic_type *lbt;
800 int ret;
801
802 ubt = &ut->u.array.elem_type;
803 lbt = &lt->u.array.elem_type;
804 ut->u.array.length = lt->u.array.length;
805 ret = serialize_basic_type(session, &ubt->atype, lbt->atype,
806 &ubt->u.basic, &lbt->u.basic);
807 if (ret)
808 return -EINVAL;
809 ut->atype = ustctl_atype_array;
810 break;
811 }
812 case atype_sequence:
813 {
814 struct ustctl_basic_type *ubt;
815 const struct lttng_basic_type *lbt;
816 int ret;
817
818 ubt = &ut->u.sequence.length_type;
819 lbt = &lt->u.sequence.length_type;
820 ret = serialize_basic_type(session, &ubt->atype, lbt->atype,
821 &ubt->u.basic, &lbt->u.basic);
822 if (ret)
823 return -EINVAL;
824 ubt = &ut->u.sequence.elem_type;
825 lbt = &lt->u.sequence.elem_type;
826 ret = serialize_basic_type(session, &ubt->atype, lbt->atype,
827 &ubt->u.basic, &lbt->u.basic);
828 if (ret)
829 return -EINVAL;
830 ut->atype = ustctl_atype_sequence;
831 break;
832 }
833 default:
834 return -EINVAL;
835 }
836 return 0;
837 }
838
839 static
840 int serialize_fields(struct lttng_session *session,
841 size_t *_nr_write_fields,
842 struct ustctl_field **ustctl_fields,
843 size_t nr_fields,
844 const struct lttng_event_field *lttng_fields)
845 {
846 struct ustctl_field *fields;
847 int i, ret;
848 size_t nr_write_fields = 0;
849
850 fields = zmalloc(nr_fields * sizeof(*fields));
851 if (!fields)
852 return -ENOMEM;
853
854 for (i = 0; i < nr_fields; i++) {
855 struct ustctl_field *f;
856 const struct lttng_event_field *lf;
857
858 f = &fields[nr_write_fields];
859 lf = &lttng_fields[i];
860
861 /* skip 'nowrite' fields */
862 if (lf->nowrite)
863 continue;
864 strncpy(f->name, lf->name, LTTNG_UST_SYM_NAME_LEN);
865 f->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
866 ret = serialize_one_type(session, &f->type, &lf->type);
867 if (ret)
868 goto error_type;
869 nr_write_fields++;
870 }
871
872 *_nr_write_fields = nr_write_fields;
873 *ustctl_fields = fields;
874 return 0;
875
876 error_type:
877 free(fields);
878 return ret;
879 }
880
881 static
882 int serialize_entries(struct ustctl_enum_entry **_entries,
883 size_t nr_entries,
884 const struct lttng_enum_entry *lttng_entries)
885 {
886 struct ustctl_enum_entry *entries;
887 int i;
888
889 /* Serialize the entries */
890 entries = zmalloc(nr_entries * sizeof(*entries));
891 if (!entries)
892 return -ENOMEM;
893 for (i = 0; i < nr_entries; i++) {
894 struct ustctl_enum_entry *uentry;
895 const struct lttng_enum_entry *lentry;
896
897 uentry = &entries[i];
898 lentry = &lttng_entries[i];
899
900 uentry->start = lentry->start;
901 uentry->end = lentry->end;
902 strncpy(uentry->string, lentry->string, LTTNG_UST_SYM_NAME_LEN);
903 uentry->string[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
904 }
905 *_entries = entries;
906 return 0;
907 }
908
909 static
910 int serialize_ctx_fields(size_t *_nr_write_fields,
911 struct ustctl_field **ustctl_fields,
912 size_t nr_fields,
913 const struct lttng_ctx_field *lttng_fields)
914 {
915 struct ustctl_field *fields;
916 int i, ret;
917 size_t nr_write_fields = 0;
918
919 fields = zmalloc(nr_fields * sizeof(*fields));
920 if (!fields)
921 return -ENOMEM;
922
923 for (i = 0; i < nr_fields; i++) {
924 struct ustctl_field *f;
925 const struct lttng_event_field *lf;
926
927 f = &fields[nr_write_fields];
928 lf = &lttng_fields[i].event_field;
929
930 /* skip 'nowrite' fields */
931 if (lf->nowrite)
932 continue;
933 strncpy(f->name, lf->name, LTTNG_UST_SYM_NAME_LEN);
934 f->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
935 ret = serialize_one_type(NULL, &f->type, &lf->type);
936 if (ret)
937 goto error_type;
938 nr_write_fields++;
939 }
940
941 *_nr_write_fields = nr_write_fields;
942 *ustctl_fields = fields;
943 return 0;
944
945 error_type:
946 free(fields);
947 return ret;
948 }
949
950 /*
951 * Returns 0 on success, negative error value on error.
952 */
953 int ustcomm_register_event(int sock,
954 struct lttng_session *session,
955 int session_objd, /* session descriptor */
956 int channel_objd, /* channel descriptor */
957 const char *event_name, /* event name (input) */
958 int loglevel,
959 const char *signature, /* event signature (input) */
960 size_t nr_fields, /* fields */
961 const struct lttng_event_field *lttng_fields,
962 const char *model_emf_uri,
963 uint32_t *id) /* event id (output) */
964 {
965 ssize_t len;
966 struct {
967 struct ustcomm_notify_hdr header;
968 struct ustcomm_notify_event_msg m;
969 } msg;
970 struct {
971 struct ustcomm_notify_hdr header;
972 struct ustcomm_notify_event_reply r;
973 } reply;
974 size_t signature_len, fields_len, model_emf_uri_len;
975 struct ustctl_field *fields = NULL;
976 size_t nr_write_fields = 0;
977 int ret;
978
979 memset(&msg, 0, sizeof(msg));
980 msg.header.notify_cmd = USTCTL_NOTIFY_CMD_EVENT;
981 msg.m.session_objd = session_objd;
982 msg.m.channel_objd = channel_objd;
983 strncpy(msg.m.event_name, event_name, LTTNG_UST_SYM_NAME_LEN);
984 msg.m.event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
985 msg.m.loglevel = loglevel;
986 signature_len = strlen(signature) + 1;
987 msg.m.signature_len = signature_len;
988
989 /* Calculate fields len, serialize fields. */
990 if (nr_fields > 0) {
991 ret = serialize_fields(session, &nr_write_fields, &fields,
992 nr_fields, lttng_fields);
993 if (ret)
994 return ret;
995 }
996
997 fields_len = sizeof(*fields) * nr_write_fields;
998 msg.m.fields_len = fields_len;
999 if (model_emf_uri) {
1000 model_emf_uri_len = strlen(model_emf_uri) + 1;
1001 } else {
1002 model_emf_uri_len = 0;
1003 }
1004 msg.m.model_emf_uri_len = model_emf_uri_len;
1005
1006 len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg));
1007 if (len > 0 && len != sizeof(msg)) {
1008 ret = -EIO;
1009 goto error_fields;
1010 }
1011 if (len < 0) {
1012 ret = len;
1013 goto error_fields;
1014 }
1015
1016 /* send signature */
1017 len = ustcomm_send_unix_sock(sock, signature, signature_len);
1018 if (len > 0 && len != signature_len) {
1019 free(fields);
1020 return -EIO;
1021 }
1022 if (len < 0) {
1023 free(fields);
1024 return len;
1025 }
1026
1027 /* send fields */
1028 if (fields_len > 0) {
1029 len = ustcomm_send_unix_sock(sock, fields, fields_len);
1030 free(fields);
1031 if (len > 0 && len != fields_len) {
1032 ret = -EIO;
1033 goto error_fields;
1034 }
1035 if (len < 0) {
1036 ret = len;
1037 goto error_fields;
1038 }
1039 } else {
1040 free(fields);
1041 }
1042
1043 if (model_emf_uri_len) {
1044 /* send model_emf_uri */
1045 len = ustcomm_send_unix_sock(sock, model_emf_uri,
1046 model_emf_uri_len);
1047 if (len > 0 && len != model_emf_uri_len) {
1048 ret = -EIO;
1049 goto error_fields;
1050 }
1051 if (len < 0) {
1052 ret = len;
1053 goto error_fields;
1054 }
1055 }
1056
1057 /* receive reply */
1058 len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply));
1059 switch (len) {
1060 case 0: /* orderly shutdown */
1061 return -EPIPE;
1062 case sizeof(reply):
1063 if (reply.header.notify_cmd != msg.header.notify_cmd) {
1064 ERR("Unexpected result message command "
1065 "expected: %u vs received: %u\n",
1066 msg.header.notify_cmd, reply.header.notify_cmd);
1067 return -EINVAL;
1068 }
1069 if (reply.r.ret_code > 0)
1070 return -EINVAL;
1071 if (reply.r.ret_code < 0)
1072 return reply.r.ret_code;
1073 *id = reply.r.event_id;
1074 DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n",
1075 event_name, reply.r.ret_code, reply.r.event_id);
1076 return 0;
1077 default:
1078 if (len < 0) {
1079 /* Transport level error */
1080 if (errno == EPIPE || errno == ECONNRESET)
1081 len = -errno;
1082 return len;
1083 } else {
1084 ERR("incorrect message size: %zd\n", len);
1085 return len;
1086 }
1087 }
1088
1089 error_fields:
1090 free(fields);
1091 return ret;
1092 }
1093
1094 /*
1095 * Returns 0 on success, negative error value on error.
1096 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1097 */
1098 int ustcomm_register_enum(int sock,
1099 int session_objd, /* session descriptor */
1100 const char *enum_name, /* enum name (input) */
1101 size_t nr_entries, /* entries */
1102 const struct lttng_enum_entry *lttng_entries,
1103 uint64_t *id)
1104 {
1105 ssize_t len;
1106 struct {
1107 struct ustcomm_notify_hdr header;
1108 struct ustcomm_notify_enum_msg m;
1109 } msg;
1110 struct {
1111 struct ustcomm_notify_hdr header;
1112 struct ustcomm_notify_enum_reply r;
1113 } reply;
1114 size_t entries_len;
1115 struct ustctl_enum_entry *entries = NULL;
1116 int ret;
1117
1118 memset(&msg, 0, sizeof(msg));
1119 msg.header.notify_cmd = USTCTL_NOTIFY_CMD_ENUM;
1120 msg.m.session_objd = session_objd;
1121 strncpy(msg.m.enum_name, enum_name, LTTNG_UST_SYM_NAME_LEN);
1122 msg.m.enum_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1123
1124 /* Calculate entries len, serialize entries. */
1125 if (nr_entries > 0) {
1126 ret = serialize_entries(&entries,
1127 nr_entries, lttng_entries);
1128 if (ret)
1129 return ret;
1130 }
1131
1132 entries_len = sizeof(*entries) * nr_entries;
1133 msg.m.entries_len = entries_len;
1134
1135 len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg));
1136 if (len > 0 && len != sizeof(msg)) {
1137 ret = -EIO;
1138 goto error_entries;
1139 }
1140 if (len < 0) {
1141 ret = len;
1142 goto error_entries;
1143 }
1144
1145 /* send entries */
1146 if (entries_len > 0) {
1147 len = ustcomm_send_unix_sock(sock, entries, entries_len);
1148 if (len > 0 && len != entries_len) {
1149 ret = -EIO;
1150 goto error_entries;
1151 }
1152 if (len < 0) {
1153 ret = len;
1154 goto error_entries;
1155 }
1156 }
1157 free(entries);
1158 entries = NULL;
1159
1160 /* receive reply */
1161 len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply));
1162 switch (len) {
1163 case 0: /* orderly shutdown */
1164 return -EPIPE;
1165 case sizeof(reply):
1166 if (reply.header.notify_cmd != msg.header.notify_cmd) {
1167 ERR("Unexpected result message command "
1168 "expected: %u vs received: %u\n",
1169 msg.header.notify_cmd, reply.header.notify_cmd);
1170 return -EINVAL;
1171 }
1172 if (reply.r.ret_code > 0)
1173 return -EINVAL;
1174 if (reply.r.ret_code < 0)
1175 return reply.r.ret_code;
1176 *id = reply.r.enum_id;
1177 DBG("Sent register enum notification for name \"%s\": ret_code %d\n",
1178 enum_name, reply.r.ret_code);
1179 return 0;
1180 default:
1181 if (len < 0) {
1182 /* Transport level error */
1183 if (errno == EPIPE || errno == ECONNRESET)
1184 len = -errno;
1185 return len;
1186 } else {
1187 ERR("incorrect message size: %zd\n", len);
1188 return len;
1189 }
1190 }
1191 return ret;
1192
1193 error_entries:
1194 free(entries);
1195 return ret;
1196 }
1197
1198 /*
1199 * Returns 0 on success, negative error value on error.
1200 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1201 */
1202 int ustcomm_register_channel(int sock,
1203 int session_objd, /* session descriptor */
1204 int channel_objd, /* channel descriptor */
1205 size_t nr_ctx_fields,
1206 const struct lttng_ctx_field *ctx_fields,
1207 uint32_t *chan_id, /* channel id (output) */
1208 int *header_type) /* header type (output) */
1209 {
1210 ssize_t len;
1211 struct {
1212 struct ustcomm_notify_hdr header;
1213 struct ustcomm_notify_channel_msg m;
1214 } msg;
1215 struct {
1216 struct ustcomm_notify_hdr header;
1217 struct ustcomm_notify_channel_reply r;
1218 } reply;
1219 size_t fields_len;
1220 struct ustctl_field *fields = NULL;
1221 int ret;
1222 size_t nr_write_fields = 0;
1223
1224 memset(&msg, 0, sizeof(msg));
1225 msg.header.notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL;
1226 msg.m.session_objd = session_objd;
1227 msg.m.channel_objd = channel_objd;
1228
1229 /* Calculate fields len, serialize fields. */
1230 if (nr_ctx_fields > 0) {
1231 ret = serialize_ctx_fields(&nr_write_fields, &fields,
1232 nr_ctx_fields, ctx_fields);
1233 if (ret)
1234 return ret;
1235 }
1236
1237 fields_len = sizeof(*fields) * nr_write_fields;
1238 msg.m.ctx_fields_len = fields_len;
1239 len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg));
1240 if (len > 0 && len != sizeof(msg)) {
1241 free(fields);
1242 return -EIO;
1243 }
1244 if (len < 0) {
1245 free(fields);
1246 return len;
1247 }
1248
1249 /* send fields */
1250 if (fields_len > 0) {
1251 len = ustcomm_send_unix_sock(sock, fields, fields_len);
1252 free(fields);
1253 if (len > 0 && len != fields_len) {
1254 return -EIO;
1255 }
1256 if (len < 0) {
1257 return len;
1258 }
1259 } else {
1260 free(fields);
1261 }
1262
1263 len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply));
1264 switch (len) {
1265 case 0: /* orderly shutdown */
1266 return -EPIPE;
1267 case sizeof(reply):
1268 if (reply.header.notify_cmd != msg.header.notify_cmd) {
1269 ERR("Unexpected result message command "
1270 "expected: %u vs received: %u\n",
1271 msg.header.notify_cmd, reply.header.notify_cmd);
1272 return -EINVAL;
1273 }
1274 if (reply.r.ret_code > 0)
1275 return -EINVAL;
1276 if (reply.r.ret_code < 0)
1277 return reply.r.ret_code;
1278 *chan_id = reply.r.chan_id;
1279 switch (reply.r.header_type) {
1280 case 1:
1281 case 2:
1282 *header_type = reply.r.header_type;
1283 break;
1284 default:
1285 ERR("Unexpected channel header type %u\n",
1286 reply.r.header_type);
1287 return -EINVAL;
1288 }
1289 DBG("Sent register channel notification: chan_id %d, header_type %d\n",
1290 reply.r.chan_id, reply.r.header_type);
1291 return 0;
1292 default:
1293 if (len < 0) {
1294 /* Transport level error */
1295 if (errno == EPIPE || errno == ECONNRESET)
1296 len = -errno;
1297 return len;
1298 } else {
1299 ERR("incorrect message size: %zd\n", len);
1300 return len;
1301 }
1302 }
1303 }
1304
1305 /*
1306 * Set socket reciving timeout.
1307 */
1308 int ustcomm_setsockopt_rcv_timeout(int sock, unsigned int msec)
1309 {
1310 int ret;
1311 struct timeval tv;
1312
1313 tv.tv_sec = msec / 1000;
1314 tv.tv_usec = (msec * 1000 % 1000000);
1315
1316 ret = setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
1317 if (ret < 0) {
1318 PERROR("setsockopt SO_RCVTIMEO");
1319 ret = -errno;
1320 }
1321
1322 return ret;
1323 }
1324
1325 /*
1326 * Set socket sending timeout.
1327 */
1328 int ustcomm_setsockopt_snd_timeout(int sock, unsigned int msec)
1329 {
1330 int ret;
1331 struct timeval tv;
1332
1333 tv.tv_sec = msec / 1000;
1334 tv.tv_usec = (msec * 1000) % 1000000;
1335
1336 ret = setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
1337 if (ret < 0) {
1338 PERROR("setsockopt SO_SNDTIMEO");
1339 ret = -errno;
1340 }
1341
1342 return ret;
1343 }
This page took 0.083527 seconds and 5 git commands to generate.