9fe6d288d633a2ec67c8d78a82cf90d2e52dc2c2
[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;
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 *_wakeup_fd = wakeup_fd;
631 lttng_ust_add_fd_to_tracker(wakeup_fd);
632 lttng_ust_unlock_fd_tracker();
633 *_chan_data = chan_data;
634 return len;
635
636 error_recv:
637 free(chan_data);
638 error_alloc:
639 error_check:
640 return len;
641 }
642
643 int ustcomm_recv_stream_from_sessiond(int sock,
644 uint64_t *memory_map_size,
645 int *shm_fd, int *wakeup_fd)
646 {
647 ssize_t len;
648 int ret;
649 int fds[2];
650
651 /* recv shm fd and wakeup fd */
652 lttng_ust_lock_fd_tracker();
653 len = ustcomm_recv_fds_unix_sock(sock, fds, 2);
654 if (len <= 0) {
655 lttng_ust_unlock_fd_tracker();
656 if (len < 0) {
657 ret = len;
658 goto error;
659 } else {
660 ret = -EIO;
661 goto error;
662 }
663 }
664 *shm_fd = fds[0];
665 *wakeup_fd = fds[1];
666 lttng_ust_add_fd_to_tracker(fds[0]);
667 lttng_ust_add_fd_to_tracker(fds[1]);
668 lttng_ust_unlock_fd_tracker();
669 return 0;
670
671 error:
672 return ret;
673 }
674
675 /*
676 * Returns 0 on success, negative error value on error.
677 */
678 int ustcomm_send_reg_msg(int sock,
679 enum ustctl_socket_type type,
680 uint32_t bits_per_long,
681 uint32_t uint8_t_alignment,
682 uint32_t uint16_t_alignment,
683 uint32_t uint32_t_alignment,
684 uint32_t uint64_t_alignment,
685 uint32_t long_alignment)
686 {
687 ssize_t len;
688 struct ustctl_reg_msg reg_msg;
689
690 reg_msg.magic = LTTNG_UST_COMM_MAGIC;
691 reg_msg.major = LTTNG_UST_ABI_MAJOR_VERSION;
692 reg_msg.minor = LTTNG_UST_ABI_MINOR_VERSION;
693 reg_msg.pid = getpid();
694 reg_msg.ppid = getppid();
695 reg_msg.uid = getuid();
696 reg_msg.gid = getgid();
697 reg_msg.bits_per_long = bits_per_long;
698 reg_msg.uint8_t_alignment = uint8_t_alignment;
699 reg_msg.uint16_t_alignment = uint16_t_alignment;
700 reg_msg.uint32_t_alignment = uint32_t_alignment;
701 reg_msg.uint64_t_alignment = uint64_t_alignment;
702 reg_msg.long_alignment = long_alignment;
703 reg_msg.socket_type = type;
704 lttng_ust_getprocname(reg_msg.name);
705 memset(reg_msg.padding, 0, sizeof(reg_msg.padding));
706
707 len = ustcomm_send_unix_sock(sock, &reg_msg, sizeof(reg_msg));
708 if (len > 0 && len != sizeof(reg_msg))
709 return -EIO;
710 if (len < 0)
711 return len;
712 return 0;
713 }
714
715 static
716 ssize_t count_one_type(const struct lttng_type *lt)
717 {
718 switch (lt->atype) {
719 case atype_integer:
720 case atype_float:
721 case atype_string:
722 case atype_enum:
723 case atype_array:
724 case atype_sequence:
725 return 1;
726 case atype_struct:
727 //TODO: implement non-empty struct.
728 return 1;
729 case atype_dynamic:
730 {
731 const struct lttng_event_field *choices;
732 size_t nr_choices;
733 int ret;
734
735 ret = lttng_ust_dynamic_type_choices(&nr_choices,
736 &choices);
737 if (ret)
738 return ret;
739 /*
740 * One field for enum, one field for variant, and
741 * one field per choice.
742 */
743 return count_fields_recursive(nr_choices, choices) + 2;
744 }
745 default:
746 return -EINVAL;
747 }
748 return 0;
749 }
750
751 static
752 ssize_t count_fields_recursive(size_t nr_fields,
753 const struct lttng_event_field *lttng_fields)
754 {
755 int i;
756 ssize_t ret, count = 0;
757
758 for (i = 0; i < nr_fields; i++) {
759 const struct lttng_event_field *lf;
760
761 lf = &lttng_fields[i];
762 /* skip 'nowrite' fields */
763 if (lf->nowrite)
764 continue;
765 ret = count_one_type(&lf->type);
766 if (ret < 0)
767 return ret; /* error */
768 count += ret;
769 }
770 return count;
771 }
772
773 static
774 ssize_t count_ctx_fields_recursive(size_t nr_fields,
775 const struct lttng_ctx_field *lttng_fields)
776 {
777 int i;
778 ssize_t ret, count = 0;
779
780 for (i = 0; i < nr_fields; i++) {
781 const struct lttng_event_field *lf;
782
783 lf = &lttng_fields[i].event_field;
784 /* skip 'nowrite' fields */
785 if (lf->nowrite)
786 continue;
787 ret = count_one_type(&lf->type);
788 if (ret < 0)
789 return ret; /* error */
790 count += ret;
791 }
792 return count;
793 }
794
795 static
796 int serialize_string_encoding(int32_t *ue,
797 enum lttng_string_encodings le)
798 {
799 switch (le) {
800 case lttng_encode_none:
801 *ue = ustctl_encode_none;
802 break;
803 case lttng_encode_UTF8:
804 *ue = ustctl_encode_UTF8;
805 break;
806 case lttng_encode_ASCII:
807 *ue = ustctl_encode_ASCII;
808 break;
809 default:
810 return -EINVAL;
811 }
812 return 0;
813 }
814
815 static
816 int serialize_integer_type(struct ustctl_integer_type *uit,
817 const struct lttng_integer_type *lit)
818 {
819 uit->size = lit->size;
820 uit->signedness = lit->signedness;
821 uit->reverse_byte_order = lit->reverse_byte_order;
822 uit->base = lit->base;
823 if (serialize_string_encoding(&uit->encoding, lit->encoding))
824 return -EINVAL;
825 uit->alignment = lit->alignment;
826 return 0;
827 }
828
829 static
830 int serialize_basic_type(struct lttng_session *session,
831 enum ustctl_abstract_types *uatype,
832 enum lttng_abstract_types atype,
833 union _ustctl_basic_type *ubt,
834 const union _lttng_basic_type *lbt)
835 {
836 switch (atype) {
837 case atype_integer:
838 {
839 if (serialize_integer_type(&ubt->integer, &lbt->integer))
840 return -EINVAL;
841 *uatype = ustctl_atype_integer;
842 break;
843 }
844 case atype_string:
845 {
846 if (serialize_string_encoding(&ubt->string.encoding,
847 lbt->string.encoding))
848 return -EINVAL;
849 *uatype = ustctl_atype_string;
850 break;
851 }
852 case atype_float:
853 {
854 struct ustctl_float_type *uft;
855 const struct lttng_float_type *lft;
856
857 uft = &ubt->_float;
858 lft = &lbt->_float;
859 uft->exp_dig = lft->exp_dig;
860 uft->mant_dig = lft->mant_dig;
861 uft->alignment = lft->alignment;
862 uft->reverse_byte_order = lft->reverse_byte_order;
863 *uatype = ustctl_atype_float;
864 break;
865 }
866 case atype_enum:
867 {
868 strncpy(ubt->enumeration.name, lbt->enumeration.desc->name,
869 LTTNG_UST_SYM_NAME_LEN);
870 ubt->enumeration.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
871 if (serialize_integer_type(&ubt->enumeration.container_type,
872 &lbt->enumeration.container_type))
873 return -EINVAL;
874 if (session) {
875 const struct lttng_enum *_enum;
876
877 _enum = lttng_ust_enum_get(session,
878 lbt->enumeration.desc->name);
879 if (!_enum)
880 return -EINVAL;
881 ubt->enumeration.id = _enum->id;
882 } else {
883 ubt->enumeration.id = -1ULL;
884 }
885 *uatype = ustctl_atype_enum;
886 break;
887 }
888 case atype_array:
889 case atype_sequence:
890 default:
891 return -EINVAL;
892 }
893 return 0;
894 }
895
896 static
897 int serialize_dynamic_type(struct lttng_session *session,
898 struct ustctl_field *fields, size_t *iter_output,
899 const struct lttng_event_field *lf)
900 {
901 const struct lttng_event_field *choices;
902 char tag_field_name[LTTNG_UST_SYM_NAME_LEN];
903 const struct lttng_type *tag_type;
904 const struct lttng_event_field *tag_field_generic;
905 struct lttng_event_field tag_field = {
906 .name = tag_field_name,
907 .nowrite = 0,
908 };
909 struct ustctl_field *uf;
910 size_t nr_choices, i;
911 int ret;
912
913 tag_field_generic = lttng_ust_dynamic_type_tag_field();
914 tag_type = &tag_field_generic->type;
915
916 /* Serialize enum field. */
917 strncpy(tag_field_name, lf->name, LTTNG_UST_SYM_NAME_LEN);
918 tag_field_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
919 strncat(tag_field_name,
920 "_tag",
921 LTTNG_UST_SYM_NAME_LEN - strlen(tag_field_name) - 1);
922 tag_field.type = *tag_type;
923 ret = serialize_one_field(session, fields, iter_output,
924 &tag_field);
925 if (ret)
926 return ret;
927
928 /* Serialize variant field. */
929 uf = &fields[*iter_output];
930 ret = lttng_ust_dynamic_type_choices(&nr_choices, &choices);
931 if (ret)
932 return ret;
933
934 strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN);
935 uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
936 uf->type.atype = ustctl_atype_variant;
937 uf->type.u.variant.nr_choices = nr_choices;
938 strncpy(uf->type.u.variant.tag_name,
939 tag_field_name,
940 LTTNG_UST_SYM_NAME_LEN);
941 uf->type.u.variant.tag_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
942 (*iter_output)++;
943
944 /* Serialize choice fields after variant. */
945 for (i = 0; i < nr_choices; i++) {
946 ret = serialize_one_field(session, fields,
947 iter_output, &choices[i]);
948 if (ret)
949 return ret;
950 }
951 return 0;
952 }
953
954 static
955 int serialize_one_field(struct lttng_session *session,
956 struct ustctl_field *fields, size_t *iter_output,
957 const struct lttng_event_field *lf)
958 {
959 const struct lttng_type *lt = &lf->type;
960 int ret;
961
962 /* skip 'nowrite' fields */
963 if (lf->nowrite)
964 return 0;
965
966 switch (lt->atype) {
967 case atype_integer:
968 case atype_float:
969 case atype_string:
970 case atype_enum:
971 {
972 struct ustctl_field *uf = &fields[*iter_output];
973 struct ustctl_type *ut = &uf->type;
974
975 strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN);
976 uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
977 ret = serialize_basic_type(session, &ut->atype, lt->atype,
978 &ut->u.basic, &lt->u.basic);
979 if (ret)
980 return ret;
981 (*iter_output)++;
982 break;
983 }
984 case atype_array:
985 {
986 struct ustctl_field *uf = &fields[*iter_output];
987 struct ustctl_type *ut = &uf->type;
988 struct ustctl_basic_type *ubt;
989 const struct lttng_basic_type *lbt;
990
991 strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN);
992 uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
993 uf->type.atype = ustctl_atype_array;
994 ubt = &ut->u.array.elem_type;
995 lbt = &lt->u.array.elem_type;
996 ut->u.array.length = lt->u.array.length;
997 ret = serialize_basic_type(session, &ubt->atype, lbt->atype,
998 &ubt->u.basic, &lbt->u.basic);
999 if (ret)
1000 return -EINVAL;
1001 ut->atype = ustctl_atype_array;
1002 (*iter_output)++;
1003 break;
1004 }
1005 case atype_sequence:
1006 {
1007 struct ustctl_field *uf = &fields[*iter_output];
1008 struct ustctl_type *ut = &uf->type;
1009 struct ustctl_basic_type *ubt;
1010 const struct lttng_basic_type *lbt;
1011 int ret;
1012
1013 strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN);
1014 uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1015 uf->type.atype = ustctl_atype_sequence;
1016 ubt = &ut->u.sequence.length_type;
1017 lbt = &lt->u.sequence.length_type;
1018 ret = serialize_basic_type(session, &ubt->atype, lbt->atype,
1019 &ubt->u.basic, &lbt->u.basic);
1020 if (ret)
1021 return -EINVAL;
1022 ubt = &ut->u.sequence.elem_type;
1023 lbt = &lt->u.sequence.elem_type;
1024 ret = serialize_basic_type(session, &ubt->atype, lbt->atype,
1025 &ubt->u.basic, &lbt->u.basic);
1026 if (ret)
1027 return -EINVAL;
1028 ut->atype = ustctl_atype_sequence;
1029 (*iter_output)++;
1030 break;
1031 }
1032 case atype_dynamic:
1033 {
1034 ret = serialize_dynamic_type(session, fields, iter_output, lf);
1035 if (ret)
1036 return -EINVAL;
1037 break;
1038 }
1039 case atype_struct:
1040 {
1041 struct ustctl_field *uf = &fields[*iter_output];
1042
1043 /*
1044 * TODO: add support for non-empty struct.
1045 */
1046 if (lf->type.u._struct.nr_fields != 0) {
1047 return -EINVAL;
1048 }
1049 strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN);
1050 uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1051 uf->type.atype = ustctl_atype_struct;
1052 uf->type.u._struct.nr_fields = 0;
1053 (*iter_output)++;
1054 break;
1055 }
1056 default:
1057 return -EINVAL;
1058 }
1059 return 0;
1060 }
1061
1062 static
1063 int serialize_fields(struct lttng_session *session,
1064 size_t *_nr_write_fields,
1065 struct ustctl_field **ustctl_fields,
1066 size_t nr_fields,
1067 const struct lttng_event_field *lttng_fields)
1068 {
1069 struct ustctl_field *fields;
1070 int ret;
1071 size_t i, iter_output = 0;
1072 ssize_t nr_write_fields;
1073
1074 nr_write_fields = count_fields_recursive(nr_fields, lttng_fields);
1075 if (nr_write_fields < 0) {
1076 return (int) nr_write_fields;
1077 }
1078
1079 fields = zmalloc(nr_write_fields * sizeof(*fields));
1080 if (!fields)
1081 return -ENOMEM;
1082
1083 for (i = 0; i < nr_fields; i++) {
1084 ret = serialize_one_field(session, fields, &iter_output,
1085 &lttng_fields[i]);
1086 if (ret)
1087 goto error_type;
1088 }
1089
1090 *_nr_write_fields = nr_write_fields;
1091 *ustctl_fields = fields;
1092 return 0;
1093
1094 error_type:
1095 free(fields);
1096 return ret;
1097 }
1098
1099 static
1100 int serialize_entries(struct ustctl_enum_entry **_entries,
1101 size_t nr_entries,
1102 const struct lttng_enum_entry *lttng_entries)
1103 {
1104 struct ustctl_enum_entry *entries;
1105 int i;
1106
1107 /* Serialize the entries */
1108 entries = zmalloc(nr_entries * sizeof(*entries));
1109 if (!entries)
1110 return -ENOMEM;
1111 for (i = 0; i < nr_entries; i++) {
1112 struct ustctl_enum_entry *uentry;
1113 const struct lttng_enum_entry *lentry;
1114
1115 uentry = &entries[i];
1116 lentry = &lttng_entries[i];
1117
1118 uentry->start.value = lentry->start.value;
1119 uentry->start.signedness = lentry->start.signedness;
1120 uentry->end.value = lentry->end.value;
1121 uentry->end.signedness = lentry->end.signedness;
1122 strncpy(uentry->string, lentry->string, LTTNG_UST_SYM_NAME_LEN);
1123 uentry->string[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1124
1125 if (lentry->u.extra.options & LTTNG_ENUM_ENTRY_OPTION_IS_AUTO) {
1126 uentry->u.extra.options |=
1127 USTCTL_UST_ENUM_ENTRY_OPTION_IS_AUTO;
1128 }
1129 }
1130 *_entries = entries;
1131 return 0;
1132 }
1133
1134 static
1135 int serialize_ctx_fields(struct lttng_session *session,
1136 size_t *_nr_write_fields,
1137 struct ustctl_field **ustctl_fields,
1138 size_t nr_fields,
1139 const struct lttng_ctx_field *lttng_fields)
1140 {
1141 struct ustctl_field *fields;
1142 int ret;
1143 size_t i, iter_output = 0;
1144 ssize_t nr_write_fields;
1145
1146 nr_write_fields = count_ctx_fields_recursive(nr_fields,
1147 lttng_fields);
1148 if (nr_write_fields < 0) {
1149 return (int) nr_write_fields;
1150 }
1151
1152 fields = zmalloc(nr_write_fields * sizeof(*fields));
1153 if (!fields)
1154 return -ENOMEM;
1155
1156 for (i = 0; i < nr_fields; i++) {
1157 ret = serialize_one_field(session, fields, &iter_output,
1158 &lttng_fields[i].event_field);
1159 if (ret)
1160 goto error_type;
1161 }
1162
1163 *_nr_write_fields = nr_write_fields;
1164 *ustctl_fields = fields;
1165 return 0;
1166
1167 error_type:
1168 free(fields);
1169 return ret;
1170 }
1171
1172 /*
1173 * Returns 0 on success, negative error value on error.
1174 */
1175 int ustcomm_register_event(int sock,
1176 struct lttng_session *session,
1177 int session_objd, /* session descriptor */
1178 int channel_objd, /* channel descriptor */
1179 const char *event_name, /* event name (input) */
1180 int loglevel,
1181 const char *signature, /* event signature (input) */
1182 size_t nr_fields, /* fields */
1183 const struct lttng_event_field *lttng_fields,
1184 const char *model_emf_uri,
1185 uint32_t *id) /* event id (output) */
1186 {
1187 ssize_t len;
1188 struct {
1189 struct ustcomm_notify_hdr header;
1190 struct ustcomm_notify_event_msg m;
1191 } msg;
1192 struct {
1193 struct ustcomm_notify_hdr header;
1194 struct ustcomm_notify_event_reply r;
1195 } reply;
1196 size_t signature_len, fields_len, model_emf_uri_len;
1197 struct ustctl_field *fields = NULL;
1198 size_t nr_write_fields = 0;
1199 int ret;
1200
1201 memset(&msg, 0, sizeof(msg));
1202 msg.header.notify_cmd = USTCTL_NOTIFY_CMD_EVENT;
1203 msg.m.session_objd = session_objd;
1204 msg.m.channel_objd = channel_objd;
1205 strncpy(msg.m.event_name, event_name, LTTNG_UST_SYM_NAME_LEN);
1206 msg.m.event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1207 msg.m.loglevel = loglevel;
1208 signature_len = strlen(signature) + 1;
1209 msg.m.signature_len = signature_len;
1210
1211 /* Calculate fields len, serialize fields. */
1212 if (nr_fields > 0) {
1213 ret = serialize_fields(session, &nr_write_fields, &fields,
1214 nr_fields, lttng_fields);
1215 if (ret)
1216 return ret;
1217 }
1218
1219 fields_len = sizeof(*fields) * nr_write_fields;
1220 msg.m.fields_len = fields_len;
1221 if (model_emf_uri) {
1222 model_emf_uri_len = strlen(model_emf_uri) + 1;
1223 } else {
1224 model_emf_uri_len = 0;
1225 }
1226 msg.m.model_emf_uri_len = model_emf_uri_len;
1227
1228 len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg));
1229 if (len > 0 && len != sizeof(msg)) {
1230 ret = -EIO;
1231 goto error_fields;
1232 }
1233 if (len < 0) {
1234 ret = len;
1235 goto error_fields;
1236 }
1237
1238 /* send signature */
1239 len = ustcomm_send_unix_sock(sock, signature, signature_len);
1240 if (len > 0 && len != signature_len) {
1241 ret = -EIO;
1242 goto error_fields;
1243 }
1244 if (len < 0) {
1245 ret = len;
1246 goto error_fields;
1247 }
1248
1249 /* send fields */
1250 if (fields_len > 0) {
1251 len = ustcomm_send_unix_sock(sock, fields, fields_len);
1252 if (len > 0 && len != fields_len) {
1253 ret = -EIO;
1254 goto error_fields;
1255 }
1256 if (len < 0) {
1257 ret = len;
1258 goto error_fields;
1259 }
1260 }
1261 free(fields);
1262
1263 if (model_emf_uri_len) {
1264 /* send model_emf_uri */
1265 len = ustcomm_send_unix_sock(sock, model_emf_uri,
1266 model_emf_uri_len);
1267 if (len > 0 && len != model_emf_uri_len) {
1268 return -EIO;
1269 }
1270 if (len < 0) {
1271 return len;
1272 }
1273 }
1274
1275 /* receive reply */
1276 len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply));
1277 switch (len) {
1278 case 0: /* orderly shutdown */
1279 return -EPIPE;
1280 case sizeof(reply):
1281 if (reply.header.notify_cmd != msg.header.notify_cmd) {
1282 ERR("Unexpected result message command "
1283 "expected: %u vs received: %u\n",
1284 msg.header.notify_cmd, reply.header.notify_cmd);
1285 return -EINVAL;
1286 }
1287 if (reply.r.ret_code > 0)
1288 return -EINVAL;
1289 if (reply.r.ret_code < 0)
1290 return reply.r.ret_code;
1291 *id = reply.r.event_id;
1292 DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n",
1293 event_name, reply.r.ret_code, reply.r.event_id);
1294 return 0;
1295 default:
1296 if (len < 0) {
1297 /* Transport level error */
1298 if (errno == EPIPE || errno == ECONNRESET)
1299 len = -errno;
1300 return len;
1301 } else {
1302 ERR("incorrect message size: %zd\n", len);
1303 return len;
1304 }
1305 }
1306 /* Unreached. */
1307
1308 /* Error path only. */
1309 error_fields:
1310 free(fields);
1311 return ret;
1312 }
1313
1314 /*
1315 * Returns 0 on success, negative error value on error.
1316 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1317 */
1318 int ustcomm_register_enum(int sock,
1319 int session_objd, /* session descriptor */
1320 const char *enum_name, /* enum name (input) */
1321 size_t nr_entries, /* entries */
1322 const struct lttng_enum_entry *lttng_entries,
1323 uint64_t *id)
1324 {
1325 ssize_t len;
1326 struct {
1327 struct ustcomm_notify_hdr header;
1328 struct ustcomm_notify_enum_msg m;
1329 } msg;
1330 struct {
1331 struct ustcomm_notify_hdr header;
1332 struct ustcomm_notify_enum_reply r;
1333 } reply;
1334 size_t entries_len;
1335 struct ustctl_enum_entry *entries = NULL;
1336 int ret;
1337
1338 memset(&msg, 0, sizeof(msg));
1339 msg.header.notify_cmd = USTCTL_NOTIFY_CMD_ENUM;
1340 msg.m.session_objd = session_objd;
1341 strncpy(msg.m.enum_name, enum_name, LTTNG_UST_SYM_NAME_LEN);
1342 msg.m.enum_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1343
1344 /* Calculate entries len, serialize entries. */
1345 if (nr_entries > 0) {
1346 ret = serialize_entries(&entries,
1347 nr_entries, lttng_entries);
1348 if (ret)
1349 return ret;
1350 }
1351
1352 entries_len = sizeof(*entries) * nr_entries;
1353 msg.m.entries_len = entries_len;
1354
1355 len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg));
1356 if (len > 0 && len != sizeof(msg)) {
1357 ret = -EIO;
1358 goto error_entries;
1359 }
1360 if (len < 0) {
1361 ret = len;
1362 goto error_entries;
1363 }
1364
1365 /* send entries */
1366 if (entries_len > 0) {
1367 len = ustcomm_send_unix_sock(sock, entries, entries_len);
1368 if (len > 0 && len != entries_len) {
1369 ret = -EIO;
1370 goto error_entries;
1371 }
1372 if (len < 0) {
1373 ret = len;
1374 goto error_entries;
1375 }
1376 }
1377 free(entries);
1378 entries = NULL;
1379
1380 /* receive reply */
1381 len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply));
1382 switch (len) {
1383 case 0: /* orderly shutdown */
1384 return -EPIPE;
1385 case sizeof(reply):
1386 if (reply.header.notify_cmd != msg.header.notify_cmd) {
1387 ERR("Unexpected result message command "
1388 "expected: %u vs received: %u\n",
1389 msg.header.notify_cmd, reply.header.notify_cmd);
1390 return -EINVAL;
1391 }
1392 if (reply.r.ret_code > 0)
1393 return -EINVAL;
1394 if (reply.r.ret_code < 0)
1395 return reply.r.ret_code;
1396 *id = reply.r.enum_id;
1397 DBG("Sent register enum notification for name \"%s\": ret_code %d\n",
1398 enum_name, reply.r.ret_code);
1399 return 0;
1400 default:
1401 if (len < 0) {
1402 /* Transport level error */
1403 if (errno == EPIPE || errno == ECONNRESET)
1404 len = -errno;
1405 return len;
1406 } else {
1407 ERR("incorrect message size: %zd\n", len);
1408 return len;
1409 }
1410 }
1411 return ret;
1412
1413 error_entries:
1414 free(entries);
1415 return ret;
1416 }
1417
1418 /*
1419 * Returns 0 on success, negative error value on error.
1420 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1421 */
1422 int ustcomm_register_channel(int sock,
1423 struct lttng_session *session,
1424 int session_objd, /* session descriptor */
1425 int channel_objd, /* channel descriptor */
1426 size_t nr_ctx_fields,
1427 const struct lttng_ctx_field *ctx_fields,
1428 uint32_t *chan_id, /* channel id (output) */
1429 int *header_type) /* header type (output) */
1430 {
1431 ssize_t len;
1432 struct {
1433 struct ustcomm_notify_hdr header;
1434 struct ustcomm_notify_channel_msg m;
1435 } msg;
1436 struct {
1437 struct ustcomm_notify_hdr header;
1438 struct ustcomm_notify_channel_reply r;
1439 } reply;
1440 size_t fields_len;
1441 struct ustctl_field *fields = NULL;
1442 int ret;
1443 size_t nr_write_fields = 0;
1444
1445 memset(&msg, 0, sizeof(msg));
1446 msg.header.notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL;
1447 msg.m.session_objd = session_objd;
1448 msg.m.channel_objd = channel_objd;
1449
1450 /* Calculate fields len, serialize fields. */
1451 if (nr_ctx_fields > 0) {
1452 ret = serialize_ctx_fields(session, &nr_write_fields, &fields,
1453 nr_ctx_fields, ctx_fields);
1454 if (ret)
1455 return ret;
1456 }
1457
1458 fields_len = sizeof(*fields) * nr_write_fields;
1459 msg.m.ctx_fields_len = fields_len;
1460 len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg));
1461 if (len > 0 && len != sizeof(msg)) {
1462 free(fields);
1463 return -EIO;
1464 }
1465 if (len < 0) {
1466 free(fields);
1467 return len;
1468 }
1469
1470 /* send fields */
1471 if (fields_len > 0) {
1472 len = ustcomm_send_unix_sock(sock, fields, fields_len);
1473 free(fields);
1474 if (len > 0 && len != fields_len) {
1475 return -EIO;
1476 }
1477 if (len < 0) {
1478 return len;
1479 }
1480 } else {
1481 free(fields);
1482 }
1483
1484 len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply));
1485 switch (len) {
1486 case 0: /* orderly shutdown */
1487 return -EPIPE;
1488 case sizeof(reply):
1489 if (reply.header.notify_cmd != msg.header.notify_cmd) {
1490 ERR("Unexpected result message command "
1491 "expected: %u vs received: %u\n",
1492 msg.header.notify_cmd, reply.header.notify_cmd);
1493 return -EINVAL;
1494 }
1495 if (reply.r.ret_code > 0)
1496 return -EINVAL;
1497 if (reply.r.ret_code < 0)
1498 return reply.r.ret_code;
1499 *chan_id = reply.r.chan_id;
1500 switch (reply.r.header_type) {
1501 case 1:
1502 case 2:
1503 *header_type = reply.r.header_type;
1504 break;
1505 default:
1506 ERR("Unexpected channel header type %u\n",
1507 reply.r.header_type);
1508 return -EINVAL;
1509 }
1510 DBG("Sent register channel notification: chan_id %d, header_type %d\n",
1511 reply.r.chan_id, reply.r.header_type);
1512 return 0;
1513 default:
1514 if (len < 0) {
1515 /* Transport level error */
1516 if (errno == EPIPE || errno == ECONNRESET)
1517 len = -errno;
1518 return len;
1519 } else {
1520 ERR("incorrect message size: %zd\n", len);
1521 return len;
1522 }
1523 }
1524 }
1525
1526 /*
1527 * Set socket reciving timeout.
1528 */
1529 int ustcomm_setsockopt_rcv_timeout(int sock, unsigned int msec)
1530 {
1531 int ret;
1532 struct timeval tv;
1533
1534 tv.tv_sec = msec / 1000;
1535 tv.tv_usec = (msec * 1000 % 1000000);
1536
1537 ret = setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
1538 if (ret < 0) {
1539 PERROR("setsockopt SO_RCVTIMEO");
1540 ret = -errno;
1541 }
1542
1543 return ret;
1544 }
1545
1546 /*
1547 * Set socket sending timeout.
1548 */
1549 int ustcomm_setsockopt_snd_timeout(int sock, unsigned int msec)
1550 {
1551 int ret;
1552 struct timeval tv;
1553
1554 tv.tv_sec = msec / 1000;
1555 tv.tv_usec = (msec * 1000) % 1000000;
1556
1557 ret = setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
1558 if (ret < 0) {
1559 PERROR("setsockopt SO_SNDTIMEO");
1560 ret = -errno;
1561 }
1562
1563 return ret;
1564 }
This page took 0.063704 seconds and 3 git commands to generate.