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