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