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