92d86d4ecbfb0c2309a7126366ae5da6fdd7272b
[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 <ust-fd.h>
37 #include <helper.h>
38 #include <lttng/ust-error.h>
39 #include <lttng/ust-events.h>
40 #include <lttng/ust-dynamic-type.h>
41 #include <usterr-signal-safe.h>
42
43 #include "../liblttng-ust/compat.h"
44
45 #define USTCOMM_CODE_OFFSET(code) \
46 (code == LTTNG_UST_OK ? 0 : (code - LTTNG_UST_ERR + 1))
47
48 #define USTCOMM_MAX_SEND_FDS 4
49
50 static
51 ssize_t count_fields_recursive(size_t nr_fields,
52 const struct lttng_event_field *lttng_fields);
53 static
54 int serialize_one_field(struct lttng_session *session,
55 struct ustctl_field *fields, size_t *iter_output,
56 const struct lttng_event_field *lf);
57
58 /*
59 * Human readable error message.
60 */
61 static const char *ustcomm_readable_code[] = {
62 [ USTCOMM_CODE_OFFSET(LTTNG_UST_OK) ] = "Success",
63 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR) ] = "Unknown error",
64 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_NOENT) ] = "No entry",
65 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_EXIST) ] = "Object already exists",
66 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL) ] = "Invalid argument",
67 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_PERM) ] = "Permission denied",
68 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_NOSYS) ] = "Not implemented",
69 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_EXITING) ] = "Process is exiting",
70
71 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL_MAGIC) ] = "Invalid magic number",
72 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL_SOCKET_TYPE) ] = "Invalid socket type",
73 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_UNSUP_MAJOR) ] = "Unsupported major version",
74 };
75
76 /*
77 * lttng_ust_strerror
78 *
79 * Receives positive error value.
80 * Return ptr to string representing a human readable
81 * error code from the ustcomm_return_code enum.
82 */
83 const char *lttng_ust_strerror(int code)
84 {
85 if (code == LTTNG_UST_OK)
86 return ustcomm_readable_code[USTCOMM_CODE_OFFSET(code)];
87 if (code < LTTNG_UST_ERR)
88 return strerror(code);
89 if (code >= LTTNG_UST_ERR_NR)
90 code = LTTNG_UST_ERR;
91 return ustcomm_readable_code[USTCOMM_CODE_OFFSET(code)];
92 }
93
94 /*
95 * ustcomm_connect_unix_sock
96 *
97 * Connect to unix socket using the path name.
98 *
99 * Caller handles FD tracker.
100 */
101 int ustcomm_connect_unix_sock(const char *pathname, long timeout)
102 {
103 struct sockaddr_un sun;
104 int fd, ret;
105
106 /*
107 * libust threads require the close-on-exec flag for all
108 * resources so it does not leak file descriptors upon exec.
109 */
110 fd = socket(PF_UNIX, SOCK_STREAM, 0);
111 if (fd < 0) {
112 PERROR("socket");
113 ret = -errno;
114 goto error;
115 }
116 if (timeout >= 0) {
117 /* Give at least 10ms. */
118 if (timeout < 10)
119 timeout = 10;
120 ret = ustcomm_setsockopt_snd_timeout(fd, timeout);
121 if (ret < 0) {
122 WARN("Error setting connect socket send timeout");
123 }
124 }
125 ret = fcntl(fd, F_SETFD, FD_CLOEXEC);
126 if (ret < 0) {
127 PERROR("fcntl");
128 ret = -errno;
129 goto error_fcntl;
130 }
131
132 memset(&sun, 0, sizeof(sun));
133 sun.sun_family = AF_UNIX;
134 strncpy(sun.sun_path, pathname, sizeof(sun.sun_path));
135 sun.sun_path[sizeof(sun.sun_path) - 1] = '\0';
136
137 ret = connect(fd, (struct sockaddr *) &sun, sizeof(sun));
138 if (ret < 0) {
139 /*
140 * Don't print message on connect ENOENT error, because
141 * connect is used in normal execution to detect if
142 * sessiond is alive. ENOENT is when the unix socket
143 * file does not exist, and ECONNREFUSED is when the
144 * file exists but no sessiond is listening.
145 */
146 if (errno != ECONNREFUSED && errno != ECONNRESET
147 && errno != ENOENT && errno != EACCES)
148 PERROR("connect");
149 ret = -errno;
150 if (ret == -ECONNREFUSED || ret == -ECONNRESET)
151 ret = -EPIPE;
152 goto error_connect;
153 }
154
155 return fd;
156
157 error_connect:
158 error_fcntl:
159 {
160 int closeret;
161
162 closeret = close(fd);
163 if (closeret)
164 PERROR("close");
165 }
166 error:
167 return ret;
168 }
169
170 /*
171 * ustcomm_accept_unix_sock
172 *
173 * Do an accept(2) on the sock and return the
174 * new file descriptor. The socket MUST be bind(2) before.
175 */
176 int ustcomm_accept_unix_sock(int sock)
177 {
178 int new_fd;
179 struct sockaddr_un sun;
180 socklen_t len = 0;
181
182 /* Blocking call */
183 new_fd = accept(sock, (struct sockaddr *) &sun, &len);
184 if (new_fd < 0) {
185 if (errno != ECONNABORTED)
186 PERROR("accept");
187 new_fd = -errno;
188 if (new_fd == -ECONNABORTED)
189 new_fd = -EPIPE;
190 }
191 return new_fd;
192 }
193
194 /*
195 * ustcomm_create_unix_sock
196 *
197 * Creates a AF_UNIX local socket using pathname
198 * bind the socket upon creation and return the fd.
199 */
200 int ustcomm_create_unix_sock(const char *pathname)
201 {
202 struct sockaddr_un sun;
203 int fd, ret;
204
205 /* Create server socket */
206 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
207 PERROR("socket");
208 ret = -errno;
209 goto error;
210 }
211
212 memset(&sun, 0, sizeof(sun));
213 sun.sun_family = AF_UNIX;
214 strncpy(sun.sun_path, pathname, sizeof(sun.sun_path));
215 sun.sun_path[sizeof(sun.sun_path) - 1] = '\0';
216
217 /* Unlink the old file if present */
218 (void) unlink(pathname);
219 ret = bind(fd, (struct sockaddr *) &sun, sizeof(sun));
220 if (ret < 0) {
221 PERROR("bind");
222 ret = -errno;
223 goto error_close;
224 }
225
226 return fd;
227
228 error_close:
229 {
230 int closeret;
231
232 closeret = close(fd);
233 if (closeret) {
234 PERROR("close");
235 }
236 }
237 error:
238 return ret;
239 }
240
241 /*
242 * ustcomm_listen_unix_sock
243 *
244 * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN.
245 */
246 int ustcomm_listen_unix_sock(int sock)
247 {
248 int ret;
249
250 ret = listen(sock, LTTNG_UST_COMM_MAX_LISTEN);
251 if (ret < 0) {
252 ret = -errno;
253 PERROR("listen");
254 }
255
256 return ret;
257 }
258
259 /*
260 * ustcomm_close_unix_sock
261 *
262 * Shutdown cleanly a unix socket.
263 *
264 * Handles fd tracker internally.
265 */
266 int ustcomm_close_unix_sock(int sock)
267 {
268 int ret;
269
270 lttng_ust_lock_fd_tracker();
271 ret = close(sock);
272 if (!ret) {
273 lttng_ust_delete_fd_from_tracker(sock);
274 } else {
275 PERROR("close");
276 ret = -errno;
277 }
278 lttng_ust_unlock_fd_tracker();
279
280 return ret;
281 }
282
283 /*
284 * ustcomm_recv_unix_sock
285 *
286 * Receive data of size len in put that data into
287 * the buf param. Using recvmsg API.
288 * Return the size of received data.
289 * Return 0 on orderly shutdown.
290 */
291 ssize_t ustcomm_recv_unix_sock(int sock, void *buf, size_t len)
292 {
293 struct msghdr msg;
294 struct iovec iov[1];
295 ssize_t ret = -1;
296 size_t len_last;
297
298 memset(&msg, 0, sizeof(msg));
299
300 iov[0].iov_base = buf;
301 iov[0].iov_len = len;
302 msg.msg_iov = iov;
303 msg.msg_iovlen = 1;
304
305 do {
306 len_last = iov[0].iov_len;
307 ret = recvmsg(sock, &msg, 0);
308 if (ret > 0) {
309 iov[0].iov_base += ret;
310 iov[0].iov_len -= ret;
311 assert(ret <= len_last);
312 }
313 } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR));
314
315 if (ret < 0) {
316 int shutret;
317
318 if (errno != EPIPE && errno != ECONNRESET && errno != ECONNREFUSED)
319 PERROR("recvmsg");
320 ret = -errno;
321 if (ret == -ECONNRESET || ret == -ECONNREFUSED)
322 ret = -EPIPE;
323
324 shutret = shutdown(sock, SHUT_RDWR);
325 if (shutret)
326 ERR("Socket shutdown error");
327 } else if (ret > 0) {
328 ret = len;
329 }
330 /* ret = 0 means an orderly shutdown. */
331
332 return ret;
333 }
334
335 /*
336 * ustcomm_send_unix_sock
337 *
338 * Send buf data of size len. Using sendmsg API.
339 * Return the size of sent data.
340 */
341 ssize_t ustcomm_send_unix_sock(int sock, const void *buf, size_t len)
342 {
343 struct msghdr msg;
344 struct iovec iov[1];
345 ssize_t ret;
346
347 memset(&msg, 0, sizeof(msg));
348
349 iov[0].iov_base = (void *) buf;
350 iov[0].iov_len = len;
351 msg.msg_iov = iov;
352 msg.msg_iovlen = 1;
353
354 /*
355 * Using the MSG_NOSIGNAL when sending data from sessiond to
356 * libust, so libust does not receive an unhandled SIGPIPE or
357 * SIGURG. The sessiond receiver side can be made more resilient
358 * by ignoring SIGPIPE, but we don't have this luxury on the
359 * libust side.
360 */
361 do {
362 ret = sendmsg(sock, &msg, MSG_NOSIGNAL);
363 } while (ret < 0 && errno == EINTR);
364
365 if (ret < 0) {
366 int shutret;
367
368 if (errno != EPIPE && errno != ECONNRESET)
369 PERROR("sendmsg");
370 ret = -errno;
371 if (ret == -ECONNRESET)
372 ret = -EPIPE;
373
374 shutret = shutdown(sock, SHUT_RDWR);
375 if (shutret)
376 ERR("Socket shutdown error");
377 }
378
379 return ret;
380 }
381
382 /*
383 * Send a message accompanied by fd(s) over a unix socket.
384 *
385 * Returns the size of data sent, or negative error value.
386 */
387 ssize_t ustcomm_send_fds_unix_sock(int sock, int *fds, size_t nb_fd)
388 {
389 struct msghdr msg;
390 struct cmsghdr *cmptr;
391 struct iovec iov[1];
392 ssize_t ret = -1;
393 unsigned int sizeof_fds = nb_fd * sizeof(int);
394 char tmp[CMSG_SPACE(sizeof_fds)];
395 char dummy = 0;
396
397 memset(&msg, 0, sizeof(msg));
398 memset(tmp, 0, CMSG_SPACE(sizeof_fds) * sizeof(char));
399
400 if (nb_fd > USTCOMM_MAX_SEND_FDS)
401 return -EINVAL;
402
403 msg.msg_control = (caddr_t)tmp;
404 msg.msg_controllen = CMSG_LEN(sizeof_fds);
405
406 cmptr = CMSG_FIRSTHDR(&msg);
407 if (!cmptr)
408 return -EINVAL;
409 cmptr->cmsg_level = SOL_SOCKET;
410 cmptr->cmsg_type = SCM_RIGHTS;
411 cmptr->cmsg_len = CMSG_LEN(sizeof_fds);
412 memcpy(CMSG_DATA(cmptr), fds, sizeof_fds);
413 /* Sum of the length of all control messages in the buffer: */
414 msg.msg_controllen = cmptr->cmsg_len;
415
416 iov[0].iov_base = &dummy;
417 iov[0].iov_len = 1;
418 msg.msg_iov = iov;
419 msg.msg_iovlen = 1;
420
421 do {
422 ret = sendmsg(sock, &msg, MSG_NOSIGNAL);
423 } while (ret < 0 && errno == EINTR);
424 if (ret < 0) {
425 /*
426 * We consider EPIPE and ECONNRESET as expected.
427 */
428 if (errno != EPIPE && errno != ECONNRESET) {
429 PERROR("sendmsg");
430 }
431 ret = -errno;
432 if (ret == -ECONNRESET)
433 ret = -EPIPE;
434 }
435 return ret;
436 }
437
438 /*
439 * Recv a message accompanied by fd(s) from a unix socket.
440 *
441 * Returns the size of received data, or negative error value.
442 *
443 * Expect at most "nb_fd" file descriptors. Returns the number of fd
444 * actually received in nb_fd.
445 * Returns -EPIPE on orderly shutdown.
446 */
447 ssize_t ustcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd)
448 {
449 struct iovec iov[1];
450 ssize_t ret = 0;
451 struct cmsghdr *cmsg;
452 size_t sizeof_fds = nb_fd * sizeof(int);
453 char recv_fd[CMSG_SPACE(sizeof_fds)];
454 struct msghdr msg;
455 char dummy;
456
457 memset(&msg, 0, sizeof(msg));
458
459 /* Prepare to receive the structures */
460 iov[0].iov_base = &dummy;
461 iov[0].iov_len = 1;
462 msg.msg_iov = iov;
463 msg.msg_iovlen = 1;
464 msg.msg_control = recv_fd;
465 msg.msg_controllen = sizeof(recv_fd);
466
467 do {
468 ret = recvmsg(sock, &msg, 0);
469 } while (ret < 0 && errno == EINTR);
470 if (ret < 0) {
471 if (errno != EPIPE && errno != ECONNRESET) {
472 PERROR("recvmsg fds");
473 }
474 ret = -errno;
475 if (ret == -ECONNRESET)
476 ret = -EPIPE;
477 goto end;
478 }
479 if (ret == 0) {
480 /* orderly shutdown */
481 ret = -EPIPE;
482 goto end;
483 }
484 if (ret != 1) {
485 ERR("Error: Received %zd bytes, expected %d\n",
486 ret, 1);
487 goto end;
488 }
489 if (msg.msg_flags & MSG_CTRUNC) {
490 ERR("Error: Control message truncated.\n");
491 ret = -1;
492 goto end;
493 }
494 cmsg = CMSG_FIRSTHDR(&msg);
495 if (!cmsg) {
496 ERR("Error: Invalid control message header\n");
497 ret = -1;
498 goto end;
499 }
500 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
501 ERR("Didn't received any fd\n");
502 ret = -1;
503 goto end;
504 }
505 if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) {
506 ERR("Error: Received %zu bytes of ancillary data, expected %zu\n",
507 (size_t) cmsg->cmsg_len, (size_t) CMSG_LEN(sizeof_fds));
508 ret = -1;
509 goto end;
510 }
511 memcpy(fds, CMSG_DATA(cmsg), sizeof_fds);
512 ret = sizeof_fds;
513 end:
514 return ret;
515 }
516
517 int ustcomm_send_app_msg(int sock, struct ustcomm_ust_msg *lum)
518 {
519 ssize_t len;
520
521 len = ustcomm_send_unix_sock(sock, lum, sizeof(*lum));
522 switch (len) {
523 case sizeof(*lum):
524 break;
525 default:
526 if (len < 0) {
527 return len;
528 } else {
529 ERR("incorrect message size: %zd\n", len);
530 return -EINVAL;
531 }
532 }
533 return 0;
534 }
535
536 int ustcomm_recv_app_reply(int sock, struct ustcomm_ust_reply *lur,
537 uint32_t expected_handle, uint32_t expected_cmd)
538 {
539 ssize_t len;
540
541 memset(lur, 0, sizeof(*lur));
542 len = ustcomm_recv_unix_sock(sock, lur, sizeof(*lur));
543 switch (len) {
544 case 0: /* orderly shutdown */
545 return -EPIPE;
546 case sizeof(*lur):
547 {
548 int err = 0;
549
550 if (lur->handle != expected_handle) {
551 ERR("Unexpected result message handle: "
552 "expected: %u vs received: %u\n",
553 expected_handle, lur->handle);
554 err = 1;
555 }
556 if (lur->cmd != expected_cmd) {
557 ERR("Unexpected result message command "
558 "expected: %u vs received: %u\n",
559 expected_cmd, lur->cmd);
560 err = 1;
561 }
562 if (err) {
563 return -EINVAL;
564 } else {
565 return lur->ret_code;
566 }
567 }
568 default:
569 if (len >= 0) {
570 ERR("incorrect message size: %zd\n", len);
571 }
572 return len;
573 }
574 }
575
576 int ustcomm_send_app_cmd(int sock,
577 struct ustcomm_ust_msg *lum,
578 struct ustcomm_ust_reply *lur)
579 {
580 int ret;
581
582 ret = ustcomm_send_app_msg(sock, lum);
583 if (ret)
584 return ret;
585 ret = ustcomm_recv_app_reply(sock, lur, lum->handle, lum->cmd);
586 if (ret > 0)
587 return -EIO;
588 return ret;
589 }
590
591 /*
592 * chan_data is allocated internally if this function returns the
593 * expected var_len.
594 */
595 ssize_t ustcomm_recv_channel_from_sessiond(int sock,
596 void **_chan_data, uint64_t var_len,
597 int *_wakeup_fd)
598 {
599 void *chan_data;
600 ssize_t len, nr_fd;
601 int wakeup_fd, ret;
602
603 if (var_len > LTTNG_UST_CHANNEL_DATA_MAX_LEN) {
604 len = -EINVAL;
605 goto error_check;
606 }
607 /* Receive variable length data */
608 chan_data = zmalloc(var_len);
609 if (!chan_data) {
610 len = -ENOMEM;
611 goto error_alloc;
612 }
613 len = ustcomm_recv_unix_sock(sock, chan_data, var_len);
614 if (len != var_len) {
615 goto error_recv;
616 }
617 /* recv wakeup fd */
618 lttng_ust_lock_fd_tracker();
619 nr_fd = ustcomm_recv_fds_unix_sock(sock, &wakeup_fd, 1);
620 if (nr_fd <= 0) {
621 lttng_ust_unlock_fd_tracker();
622 if (nr_fd < 0) {
623 len = nr_fd;
624 goto error_recv;
625 } else {
626 len = -EIO;
627 goto error_recv;
628 }
629 }
630
631 ret = lttng_ust_add_fd_to_tracker(wakeup_fd);
632 if (ret < 0) {
633 lttng_ust_unlock_fd_tracker();
634 ret = close(wakeup_fd);
635 if (ret) {
636 PERROR("close on wakeup_fd");
637 }
638 len = -EIO;
639 goto error_recv;
640 }
641
642 *_wakeup_fd = ret;
643 lttng_ust_unlock_fd_tracker();
644
645 *_chan_data = chan_data;
646 return len;
647
648 error_recv:
649 free(chan_data);
650 error_alloc:
651 error_check:
652 return len;
653 }
654
655 int ustcomm_recv_stream_from_sessiond(int sock,
656 uint64_t *memory_map_size,
657 int *shm_fd, int *wakeup_fd)
658 {
659 ssize_t len;
660 int ret;
661 int fds[2];
662
663 /* recv shm fd and wakeup fd */
664 lttng_ust_lock_fd_tracker();
665 len = ustcomm_recv_fds_unix_sock(sock, fds, 2);
666 if (len <= 0) {
667 lttng_ust_unlock_fd_tracker();
668 if (len < 0) {
669 ret = len;
670 goto error;
671 } else {
672 ret = -EIO;
673 goto error;
674 }
675 }
676
677 ret = lttng_ust_add_fd_to_tracker(fds[0]);
678 if (ret < 0) {
679 lttng_ust_unlock_fd_tracker();
680 ret = close(fds[0]);
681 if (ret) {
682 PERROR("close on received shm_fd");
683 }
684 ret = -EIO;
685 goto error;
686 }
687 *shm_fd = ret;
688
689 ret = lttng_ust_add_fd_to_tracker(fds[1]);
690 if (ret < 0) {
691 lttng_ust_unlock_fd_tracker();
692 ret = close(*shm_fd);
693 if (ret) {
694 PERROR("close on shm_fd");
695 }
696 *shm_fd = -1;
697 ret = close(fds[1]);
698 if (ret) {
699 PERROR("close on received wakeup_fd");
700 }
701 ret = -EIO;
702 goto error;
703 }
704 *wakeup_fd = ret;
705 lttng_ust_unlock_fd_tracker();
706 return 0;
707
708 error:
709 return ret;
710 }
711
712 /*
713 * Returns 0 on success, negative error value on error.
714 */
715 int ustcomm_send_reg_msg(int sock,
716 enum ustctl_socket_type type,
717 uint32_t bits_per_long,
718 uint32_t uint8_t_alignment,
719 uint32_t uint16_t_alignment,
720 uint32_t uint32_t_alignment,
721 uint32_t uint64_t_alignment,
722 uint32_t long_alignment)
723 {
724 ssize_t len;
725 struct ustctl_reg_msg reg_msg;
726
727 reg_msg.magic = LTTNG_UST_COMM_MAGIC;
728 reg_msg.major = LTTNG_UST_ABI_MAJOR_VERSION;
729 reg_msg.minor = LTTNG_UST_ABI_MINOR_VERSION;
730 reg_msg.pid = getpid();
731 reg_msg.ppid = getppid();
732 reg_msg.uid = getuid();
733 reg_msg.gid = getgid();
734 reg_msg.bits_per_long = bits_per_long;
735 reg_msg.uint8_t_alignment = uint8_t_alignment;
736 reg_msg.uint16_t_alignment = uint16_t_alignment;
737 reg_msg.uint32_t_alignment = uint32_t_alignment;
738 reg_msg.uint64_t_alignment = uint64_t_alignment;
739 reg_msg.long_alignment = long_alignment;
740 reg_msg.socket_type = type;
741 lttng_ust_getprocname(reg_msg.name);
742 memset(reg_msg.padding, 0, sizeof(reg_msg.padding));
743
744 len = ustcomm_send_unix_sock(sock, &reg_msg, sizeof(reg_msg));
745 if (len > 0 && len != sizeof(reg_msg))
746 return -EIO;
747 if (len < 0)
748 return len;
749 return 0;
750 }
751
752 static
753 ssize_t count_one_type(const struct lttng_type *lt)
754 {
755 switch (lt->atype) {
756 case atype_integer:
757 case atype_float:
758 case atype_string:
759 case atype_enum:
760 case atype_array:
761 case atype_sequence:
762 return 1;
763 case atype_struct:
764 //TODO: implement non-empty struct.
765 return 1;
766 case atype_dynamic:
767 {
768 const struct lttng_event_field *choices;
769 size_t nr_choices;
770 int ret;
771
772 ret = lttng_ust_dynamic_type_choices(&nr_choices,
773 &choices);
774 if (ret)
775 return ret;
776 /*
777 * One field for enum, one field for variant, and
778 * one field per choice.
779 */
780 return count_fields_recursive(nr_choices, choices) + 2;
781 }
782 default:
783 return -EINVAL;
784 }
785 return 0;
786 }
787
788 static
789 ssize_t count_fields_recursive(size_t nr_fields,
790 const struct lttng_event_field *lttng_fields)
791 {
792 int i;
793 ssize_t ret, count = 0;
794
795 for (i = 0; i < nr_fields; i++) {
796 const struct lttng_event_field *lf;
797
798 lf = &lttng_fields[i];
799 /* skip 'nowrite' fields */
800 if (lf->nowrite)
801 continue;
802 ret = count_one_type(&lf->type);
803 if (ret < 0)
804 return ret; /* error */
805 count += ret;
806 }
807 return count;
808 }
809
810 static
811 ssize_t count_ctx_fields_recursive(size_t nr_fields,
812 const struct lttng_ctx_field *lttng_fields)
813 {
814 int i;
815 ssize_t ret, count = 0;
816
817 for (i = 0; i < nr_fields; i++) {
818 const struct lttng_event_field *lf;
819
820 lf = &lttng_fields[i].event_field;
821 /* skip 'nowrite' fields */
822 if (lf->nowrite)
823 continue;
824 ret = count_one_type(&lf->type);
825 if (ret < 0)
826 return ret; /* error */
827 count += ret;
828 }
829 return count;
830 }
831
832 static
833 int serialize_string_encoding(int32_t *ue,
834 enum lttng_string_encodings le)
835 {
836 switch (le) {
837 case lttng_encode_none:
838 *ue = ustctl_encode_none;
839 break;
840 case lttng_encode_UTF8:
841 *ue = ustctl_encode_UTF8;
842 break;
843 case lttng_encode_ASCII:
844 *ue = ustctl_encode_ASCII;
845 break;
846 default:
847 return -EINVAL;
848 }
849 return 0;
850 }
851
852 static
853 int serialize_integer_type(struct ustctl_integer_type *uit,
854 const struct lttng_integer_type *lit)
855 {
856 int32_t encoding;
857
858 uit->size = lit->size;
859 uit->signedness = lit->signedness;
860 uit->reverse_byte_order = lit->reverse_byte_order;
861 uit->base = lit->base;
862 if (serialize_string_encoding(&encoding, lit->encoding))
863 return -EINVAL;
864 uit->encoding = encoding;
865 uit->alignment = lit->alignment;
866 return 0;
867 }
868
869 static
870 int serialize_basic_type(struct lttng_session *session,
871 enum ustctl_abstract_types *uatype,
872 enum lttng_abstract_types atype,
873 union _ustctl_basic_type *ubt,
874 const union _lttng_basic_type *lbt)
875 {
876 switch (atype) {
877 case atype_integer:
878 {
879 if (serialize_integer_type(&ubt->integer, &lbt->integer))
880 return -EINVAL;
881 *uatype = ustctl_atype_integer;
882 break;
883 }
884 case atype_string:
885 {
886 int32_t encoding;
887
888 if (serialize_string_encoding(&encoding, lbt->string.encoding))
889 return -EINVAL;
890 ubt->string.encoding = encoding;
891 *uatype = ustctl_atype_string;
892 break;
893 }
894 case atype_float:
895 {
896 struct ustctl_float_type *uft;
897 const struct lttng_float_type *lft;
898
899 uft = &ubt->_float;
900 lft = &lbt->_float;
901 uft->exp_dig = lft->exp_dig;
902 uft->mant_dig = lft->mant_dig;
903 uft->alignment = lft->alignment;
904 uft->reverse_byte_order = lft->reverse_byte_order;
905 *uatype = ustctl_atype_float;
906 break;
907 }
908 case atype_enum:
909 {
910 strncpy(ubt->enumeration.name, lbt->enumeration.desc->name,
911 LTTNG_UST_SYM_NAME_LEN);
912 ubt->enumeration.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
913 if (serialize_integer_type(&ubt->enumeration.container_type,
914 &lbt->enumeration.container_type))
915 return -EINVAL;
916 if (session) {
917 const struct lttng_enum *_enum;
918
919 _enum = lttng_ust_enum_get_from_desc(session, lbt->enumeration.desc);
920 if (!_enum)
921 return -EINVAL;
922 ubt->enumeration.id = _enum->id;
923 } else {
924 ubt->enumeration.id = -1ULL;
925 }
926 *uatype = ustctl_atype_enum;
927 break;
928 }
929 case atype_array:
930 case atype_sequence:
931 default:
932 return -EINVAL;
933 }
934 return 0;
935 }
936
937 static
938 int serialize_dynamic_type(struct lttng_session *session,
939 struct ustctl_field *fields, size_t *iter_output,
940 const struct lttng_event_field *lf)
941 {
942 const struct lttng_event_field *choices;
943 char tag_field_name[LTTNG_UST_SYM_NAME_LEN];
944 const struct lttng_type *tag_type;
945 const struct lttng_event_field *tag_field_generic;
946 struct lttng_event_field tag_field = {
947 .name = tag_field_name,
948 .nowrite = 0,
949 };
950 struct ustctl_field *uf;
951 size_t nr_choices, i;
952 int ret;
953
954 tag_field_generic = lttng_ust_dynamic_type_tag_field();
955 tag_type = &tag_field_generic->type;
956
957 /* Serialize enum field. */
958 strncpy(tag_field_name, lf->name, LTTNG_UST_SYM_NAME_LEN);
959 tag_field_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
960 strncat(tag_field_name,
961 "_tag",
962 LTTNG_UST_SYM_NAME_LEN - strlen(tag_field_name) - 1);
963 tag_field.type = *tag_type;
964 ret = serialize_one_field(session, fields, iter_output,
965 &tag_field);
966 if (ret)
967 return ret;
968
969 /* Serialize variant field. */
970 uf = &fields[*iter_output];
971 ret = lttng_ust_dynamic_type_choices(&nr_choices, &choices);
972 if (ret)
973 return ret;
974
975 strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN);
976 uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
977 uf->type.atype = ustctl_atype_variant;
978 uf->type.u.variant.nr_choices = nr_choices;
979 strncpy(uf->type.u.variant.tag_name,
980 tag_field_name,
981 LTTNG_UST_SYM_NAME_LEN);
982 uf->type.u.variant.tag_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
983 (*iter_output)++;
984
985 /* Serialize choice fields after variant. */
986 for (i = 0; i < nr_choices; i++) {
987 ret = serialize_one_field(session, fields,
988 iter_output, &choices[i]);
989 if (ret)
990 return ret;
991 }
992 return 0;
993 }
994
995 static
996 int serialize_one_field(struct lttng_session *session,
997 struct ustctl_field *fields, size_t *iter_output,
998 const struct lttng_event_field *lf)
999 {
1000 const struct lttng_type *lt = &lf->type;
1001 int ret;
1002
1003 /* skip 'nowrite' fields */
1004 if (lf->nowrite)
1005 return 0;
1006
1007 switch (lt->atype) {
1008 case atype_integer:
1009 case atype_float:
1010 case atype_string:
1011 case atype_enum:
1012 {
1013 struct ustctl_field *uf = &fields[*iter_output];
1014 struct ustctl_type *ut = &uf->type;
1015 enum ustctl_abstract_types atype;
1016
1017 strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN);
1018 uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1019 ret = serialize_basic_type(session, &atype, lt->atype,
1020 &ut->u.basic, &lt->u.basic);
1021 if (ret)
1022 return ret;
1023 ut->atype = atype;
1024 (*iter_output)++;
1025 break;
1026 }
1027 case atype_array:
1028 {
1029 struct ustctl_field *uf = &fields[*iter_output];
1030 struct ustctl_type *ut = &uf->type;
1031 struct ustctl_basic_type *ubt;
1032 const struct lttng_basic_type *lbt;
1033 enum ustctl_abstract_types atype;
1034
1035 strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN);
1036 uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1037 uf->type.atype = ustctl_atype_array;
1038 ubt = &ut->u.array.elem_type;
1039 lbt = &lt->u.array.elem_type;
1040 ut->u.array.length = lt->u.array.length;
1041 ret = serialize_basic_type(session, &atype, lbt->atype,
1042 &ubt->u.basic, &lbt->u.basic);
1043 if (ret)
1044 return -EINVAL;
1045 ubt->atype = atype;
1046 ut->atype = ustctl_atype_array;
1047 (*iter_output)++;
1048 break;
1049 }
1050 case atype_sequence:
1051 {
1052 struct ustctl_field *uf = &fields[*iter_output];
1053 struct ustctl_type *ut = &uf->type;
1054 struct ustctl_basic_type *ubt;
1055 const struct lttng_basic_type *lbt;
1056 enum ustctl_abstract_types atype;
1057 int ret;
1058
1059 strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN);
1060 uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1061 uf->type.atype = ustctl_atype_sequence;
1062 ubt = &ut->u.sequence.length_type;
1063 lbt = &lt->u.sequence.length_type;
1064 ret = serialize_basic_type(session, &atype, lbt->atype,
1065 &ubt->u.basic, &lbt->u.basic);
1066 if (ret)
1067 return -EINVAL;
1068 ubt->atype = atype;
1069 ubt = &ut->u.sequence.elem_type;
1070 lbt = &lt->u.sequence.elem_type;
1071 ret = serialize_basic_type(session, &atype, lbt->atype,
1072 &ubt->u.basic, &lbt->u.basic);
1073 if (ret)
1074 return -EINVAL;
1075 ubt->atype = atype;
1076 ut->atype = ustctl_atype_sequence;
1077 (*iter_output)++;
1078 break;
1079 }
1080 case atype_dynamic:
1081 {
1082 ret = serialize_dynamic_type(session, fields, iter_output, lf);
1083 if (ret)
1084 return -EINVAL;
1085 break;
1086 }
1087 case atype_struct:
1088 {
1089 struct ustctl_field *uf = &fields[*iter_output];
1090
1091 /*
1092 * TODO: add support for non-empty struct.
1093 */
1094 if (lf->type.u._struct.nr_fields != 0) {
1095 return -EINVAL;
1096 }
1097 strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN);
1098 uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1099 uf->type.atype = ustctl_atype_struct;
1100 uf->type.u._struct.nr_fields = 0;
1101 (*iter_output)++;
1102 break;
1103 }
1104 default:
1105 return -EINVAL;
1106 }
1107 return 0;
1108 }
1109
1110 static
1111 int serialize_fields(struct lttng_session *session,
1112 size_t *_nr_write_fields,
1113 struct ustctl_field **ustctl_fields,
1114 size_t nr_fields,
1115 const struct lttng_event_field *lttng_fields)
1116 {
1117 struct ustctl_field *fields;
1118 int ret;
1119 size_t i, iter_output = 0;
1120 ssize_t nr_write_fields;
1121
1122 nr_write_fields = count_fields_recursive(nr_fields, lttng_fields);
1123 if (nr_write_fields < 0) {
1124 return (int) nr_write_fields;
1125 }
1126
1127 fields = zmalloc(nr_write_fields * sizeof(*fields));
1128 if (!fields)
1129 return -ENOMEM;
1130
1131 for (i = 0; i < nr_fields; i++) {
1132 ret = serialize_one_field(session, fields, &iter_output,
1133 &lttng_fields[i]);
1134 if (ret)
1135 goto error_type;
1136 }
1137
1138 *_nr_write_fields = nr_write_fields;
1139 *ustctl_fields = fields;
1140 return 0;
1141
1142 error_type:
1143 free(fields);
1144 return ret;
1145 }
1146
1147 static
1148 int serialize_entries(struct ustctl_enum_entry **_entries,
1149 size_t nr_entries,
1150 const struct lttng_enum_entry *lttng_entries)
1151 {
1152 struct ustctl_enum_entry *entries;
1153 int i;
1154
1155 /* Serialize the entries */
1156 entries = zmalloc(nr_entries * sizeof(*entries));
1157 if (!entries)
1158 return -ENOMEM;
1159 for (i = 0; i < nr_entries; i++) {
1160 struct ustctl_enum_entry *uentry;
1161 const struct lttng_enum_entry *lentry;
1162
1163 uentry = &entries[i];
1164 lentry = &lttng_entries[i];
1165
1166 uentry->start.value = lentry->start.value;
1167 uentry->start.signedness = lentry->start.signedness;
1168 uentry->end.value = lentry->end.value;
1169 uentry->end.signedness = lentry->end.signedness;
1170 strncpy(uentry->string, lentry->string, LTTNG_UST_SYM_NAME_LEN);
1171 uentry->string[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1172
1173 if (lentry->u.extra.options & LTTNG_ENUM_ENTRY_OPTION_IS_AUTO) {
1174 uentry->u.extra.options |=
1175 USTCTL_UST_ENUM_ENTRY_OPTION_IS_AUTO;
1176 }
1177 }
1178 *_entries = entries;
1179 return 0;
1180 }
1181
1182 static
1183 int serialize_ctx_fields(struct lttng_session *session,
1184 size_t *_nr_write_fields,
1185 struct ustctl_field **ustctl_fields,
1186 size_t nr_fields,
1187 const struct lttng_ctx_field *lttng_fields)
1188 {
1189 struct ustctl_field *fields;
1190 int ret;
1191 size_t i, iter_output = 0;
1192 ssize_t nr_write_fields;
1193
1194 nr_write_fields = count_ctx_fields_recursive(nr_fields,
1195 lttng_fields);
1196 if (nr_write_fields < 0) {
1197 return (int) nr_write_fields;
1198 }
1199
1200 fields = zmalloc(nr_write_fields * sizeof(*fields));
1201 if (!fields)
1202 return -ENOMEM;
1203
1204 for (i = 0; i < nr_fields; i++) {
1205 ret = serialize_one_field(session, fields, &iter_output,
1206 &lttng_fields[i].event_field);
1207 if (ret)
1208 goto error_type;
1209 }
1210
1211 *_nr_write_fields = nr_write_fields;
1212 *ustctl_fields = fields;
1213 return 0;
1214
1215 error_type:
1216 free(fields);
1217 return ret;
1218 }
1219
1220 /*
1221 * Returns 0 on success, negative error value on error.
1222 */
1223 int ustcomm_register_event(int sock,
1224 struct lttng_session *session,
1225 int session_objd, /* session descriptor */
1226 int channel_objd, /* channel descriptor */
1227 const char *event_name, /* event name (input) */
1228 int loglevel,
1229 const char *signature, /* event signature (input) */
1230 size_t nr_fields, /* fields */
1231 const struct lttng_event_field *lttng_fields,
1232 const char *model_emf_uri,
1233 uint32_t *id) /* event id (output) */
1234 {
1235 ssize_t len;
1236 struct {
1237 struct ustcomm_notify_hdr header;
1238 struct ustcomm_notify_event_msg m;
1239 } msg;
1240 struct {
1241 struct ustcomm_notify_hdr header;
1242 struct ustcomm_notify_event_reply r;
1243 } reply;
1244 size_t signature_len, fields_len, model_emf_uri_len;
1245 struct ustctl_field *fields = NULL;
1246 size_t nr_write_fields = 0;
1247 int ret;
1248
1249 memset(&msg, 0, sizeof(msg));
1250 msg.header.notify_cmd = USTCTL_NOTIFY_CMD_EVENT;
1251 msg.m.session_objd = session_objd;
1252 msg.m.channel_objd = channel_objd;
1253 strncpy(msg.m.event_name, event_name, LTTNG_UST_SYM_NAME_LEN);
1254 msg.m.event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1255 msg.m.loglevel = loglevel;
1256 signature_len = strlen(signature) + 1;
1257 msg.m.signature_len = signature_len;
1258
1259 /* Calculate fields len, serialize fields. */
1260 if (nr_fields > 0) {
1261 ret = serialize_fields(session, &nr_write_fields, &fields,
1262 nr_fields, lttng_fields);
1263 if (ret)
1264 return ret;
1265 }
1266
1267 fields_len = sizeof(*fields) * nr_write_fields;
1268 msg.m.fields_len = fields_len;
1269 if (model_emf_uri) {
1270 model_emf_uri_len = strlen(model_emf_uri) + 1;
1271 } else {
1272 model_emf_uri_len = 0;
1273 }
1274 msg.m.model_emf_uri_len = model_emf_uri_len;
1275
1276 len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg));
1277 if (len > 0 && len != sizeof(msg)) {
1278 ret = -EIO;
1279 goto error_fields;
1280 }
1281 if (len < 0) {
1282 ret = len;
1283 goto error_fields;
1284 }
1285
1286 /* send signature */
1287 len = ustcomm_send_unix_sock(sock, signature, signature_len);
1288 if (len > 0 && len != signature_len) {
1289 ret = -EIO;
1290 goto error_fields;
1291 }
1292 if (len < 0) {
1293 ret = len;
1294 goto error_fields;
1295 }
1296
1297 /* send fields */
1298 if (fields_len > 0) {
1299 len = ustcomm_send_unix_sock(sock, fields, fields_len);
1300 if (len > 0 && len != fields_len) {
1301 ret = -EIO;
1302 goto error_fields;
1303 }
1304 if (len < 0) {
1305 ret = len;
1306 goto error_fields;
1307 }
1308 }
1309 free(fields);
1310
1311 if (model_emf_uri_len) {
1312 /* send model_emf_uri */
1313 len = ustcomm_send_unix_sock(sock, model_emf_uri,
1314 model_emf_uri_len);
1315 if (len > 0 && len != model_emf_uri_len) {
1316 return -EIO;
1317 }
1318 if (len < 0) {
1319 return len;
1320 }
1321 }
1322
1323 /* receive reply */
1324 len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply));
1325 switch (len) {
1326 case 0: /* orderly shutdown */
1327 return -EPIPE;
1328 case sizeof(reply):
1329 if (reply.header.notify_cmd != msg.header.notify_cmd) {
1330 ERR("Unexpected result message command "
1331 "expected: %u vs received: %u\n",
1332 msg.header.notify_cmd, reply.header.notify_cmd);
1333 return -EINVAL;
1334 }
1335 if (reply.r.ret_code > 0)
1336 return -EINVAL;
1337 if (reply.r.ret_code < 0)
1338 return reply.r.ret_code;
1339 *id = reply.r.event_id;
1340 DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n",
1341 event_name, reply.r.ret_code, reply.r.event_id);
1342 return 0;
1343 default:
1344 if (len < 0) {
1345 /* Transport level error */
1346 if (errno == EPIPE || errno == ECONNRESET)
1347 len = -errno;
1348 return len;
1349 } else {
1350 ERR("incorrect message size: %zd\n", len);
1351 return len;
1352 }
1353 }
1354 /* Unreached. */
1355
1356 /* Error path only. */
1357 error_fields:
1358 free(fields);
1359 return ret;
1360 }
1361
1362 /*
1363 * Returns 0 on success, negative error value on error.
1364 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1365 */
1366 int ustcomm_register_enum(int sock,
1367 int session_objd, /* session descriptor */
1368 const char *enum_name, /* enum name (input) */
1369 size_t nr_entries, /* entries */
1370 const struct lttng_enum_entry *lttng_entries,
1371 uint64_t *id)
1372 {
1373 ssize_t len;
1374 struct {
1375 struct ustcomm_notify_hdr header;
1376 struct ustcomm_notify_enum_msg m;
1377 } msg;
1378 struct {
1379 struct ustcomm_notify_hdr header;
1380 struct ustcomm_notify_enum_reply r;
1381 } reply;
1382 size_t entries_len;
1383 struct ustctl_enum_entry *entries = NULL;
1384 int ret;
1385
1386 memset(&msg, 0, sizeof(msg));
1387 msg.header.notify_cmd = USTCTL_NOTIFY_CMD_ENUM;
1388 msg.m.session_objd = session_objd;
1389 strncpy(msg.m.enum_name, enum_name, LTTNG_UST_SYM_NAME_LEN);
1390 msg.m.enum_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1391
1392 /* Calculate entries len, serialize entries. */
1393 if (nr_entries > 0) {
1394 ret = serialize_entries(&entries,
1395 nr_entries, lttng_entries);
1396 if (ret)
1397 return ret;
1398 }
1399
1400 entries_len = sizeof(*entries) * nr_entries;
1401 msg.m.entries_len = entries_len;
1402
1403 len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg));
1404 if (len > 0 && len != sizeof(msg)) {
1405 ret = -EIO;
1406 goto error_entries;
1407 }
1408 if (len < 0) {
1409 ret = len;
1410 goto error_entries;
1411 }
1412
1413 /* send entries */
1414 if (entries_len > 0) {
1415 len = ustcomm_send_unix_sock(sock, entries, entries_len);
1416 if (len > 0 && len != entries_len) {
1417 ret = -EIO;
1418 goto error_entries;
1419 }
1420 if (len < 0) {
1421 ret = len;
1422 goto error_entries;
1423 }
1424 }
1425 free(entries);
1426 entries = NULL;
1427
1428 /* receive reply */
1429 len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply));
1430 switch (len) {
1431 case 0: /* orderly shutdown */
1432 return -EPIPE;
1433 case sizeof(reply):
1434 if (reply.header.notify_cmd != msg.header.notify_cmd) {
1435 ERR("Unexpected result message command "
1436 "expected: %u vs received: %u\n",
1437 msg.header.notify_cmd, reply.header.notify_cmd);
1438 return -EINVAL;
1439 }
1440 if (reply.r.ret_code > 0)
1441 return -EINVAL;
1442 if (reply.r.ret_code < 0)
1443 return reply.r.ret_code;
1444 *id = reply.r.enum_id;
1445 DBG("Sent register enum notification for name \"%s\": ret_code %d\n",
1446 enum_name, reply.r.ret_code);
1447 return 0;
1448 default:
1449 if (len < 0) {
1450 /* Transport level error */
1451 if (errno == EPIPE || errno == ECONNRESET)
1452 len = -errno;
1453 return len;
1454 } else {
1455 ERR("incorrect message size: %zd\n", len);
1456 return len;
1457 }
1458 }
1459 return ret;
1460
1461 error_entries:
1462 free(entries);
1463 return ret;
1464 }
1465
1466 /*
1467 * Returns 0 on success, negative error value on error.
1468 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1469 */
1470 int ustcomm_register_channel(int sock,
1471 struct lttng_session *session,
1472 int session_objd, /* session descriptor */
1473 int channel_objd, /* channel descriptor */
1474 size_t nr_ctx_fields,
1475 const struct lttng_ctx_field *ctx_fields,
1476 uint32_t *chan_id, /* channel id (output) */
1477 int *header_type) /* header type (output) */
1478 {
1479 ssize_t len;
1480 struct {
1481 struct ustcomm_notify_hdr header;
1482 struct ustcomm_notify_channel_msg m;
1483 } msg;
1484 struct {
1485 struct ustcomm_notify_hdr header;
1486 struct ustcomm_notify_channel_reply r;
1487 } reply;
1488 size_t fields_len;
1489 struct ustctl_field *fields = NULL;
1490 int ret;
1491 size_t nr_write_fields = 0;
1492
1493 memset(&msg, 0, sizeof(msg));
1494 msg.header.notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL;
1495 msg.m.session_objd = session_objd;
1496 msg.m.channel_objd = channel_objd;
1497
1498 /* Calculate fields len, serialize fields. */
1499 if (nr_ctx_fields > 0) {
1500 ret = serialize_ctx_fields(session, &nr_write_fields, &fields,
1501 nr_ctx_fields, ctx_fields);
1502 if (ret)
1503 return ret;
1504 }
1505
1506 fields_len = sizeof(*fields) * nr_write_fields;
1507 msg.m.ctx_fields_len = fields_len;
1508 len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg));
1509 if (len > 0 && len != sizeof(msg)) {
1510 free(fields);
1511 return -EIO;
1512 }
1513 if (len < 0) {
1514 free(fields);
1515 return len;
1516 }
1517
1518 /* send fields */
1519 if (fields_len > 0) {
1520 len = ustcomm_send_unix_sock(sock, fields, fields_len);
1521 free(fields);
1522 if (len > 0 && len != fields_len) {
1523 return -EIO;
1524 }
1525 if (len < 0) {
1526 return len;
1527 }
1528 } else {
1529 free(fields);
1530 }
1531
1532 len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply));
1533 switch (len) {
1534 case 0: /* orderly shutdown */
1535 return -EPIPE;
1536 case sizeof(reply):
1537 if (reply.header.notify_cmd != msg.header.notify_cmd) {
1538 ERR("Unexpected result message command "
1539 "expected: %u vs received: %u\n",
1540 msg.header.notify_cmd, reply.header.notify_cmd);
1541 return -EINVAL;
1542 }
1543 if (reply.r.ret_code > 0)
1544 return -EINVAL;
1545 if (reply.r.ret_code < 0)
1546 return reply.r.ret_code;
1547 *chan_id = reply.r.chan_id;
1548 switch (reply.r.header_type) {
1549 case 1:
1550 case 2:
1551 *header_type = reply.r.header_type;
1552 break;
1553 default:
1554 ERR("Unexpected channel header type %u\n",
1555 reply.r.header_type);
1556 return -EINVAL;
1557 }
1558 DBG("Sent register channel notification: chan_id %d, header_type %d\n",
1559 reply.r.chan_id, reply.r.header_type);
1560 return 0;
1561 default:
1562 if (len < 0) {
1563 /* Transport level error */
1564 if (errno == EPIPE || errno == ECONNRESET)
1565 len = -errno;
1566 return len;
1567 } else {
1568 ERR("incorrect message size: %zd\n", len);
1569 return len;
1570 }
1571 }
1572 }
1573
1574 /*
1575 * Set socket reciving timeout.
1576 */
1577 int ustcomm_setsockopt_rcv_timeout(int sock, unsigned int msec)
1578 {
1579 int ret;
1580 struct timeval tv;
1581
1582 tv.tv_sec = msec / 1000;
1583 tv.tv_usec = (msec * 1000 % 1000000);
1584
1585 ret = setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
1586 if (ret < 0) {
1587 PERROR("setsockopt SO_RCVTIMEO");
1588 ret = -errno;
1589 }
1590
1591 return ret;
1592 }
1593
1594 /*
1595 * Set socket sending timeout.
1596 */
1597 int ustcomm_setsockopt_snd_timeout(int sock, unsigned int msec)
1598 {
1599 int ret;
1600 struct timeval tv;
1601
1602 tv.tv_sec = msec / 1000;
1603 tv.tv_usec = (msec * 1000) % 1000000;
1604
1605 ret = setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
1606 if (ret < 0) {
1607 PERROR("setsockopt SO_SNDTIMEO");
1608 ret = -errno;
1609 }
1610
1611 return ret;
1612 }
This page took 0.080587 seconds and 3 git commands to generate.