2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
22 #include <sys/resource.h>
24 #include <libvcl-ldpreload/vcom_socket_wrapper.h>
25 #include <libvcl-ldpreload/vcom.h>
28 #include <uri/vppcom.h>
29 #include <libvcl-ldpreload/vcom_socket.h>
31 /* GCC have printf type attribute check. */
32 #ifdef HAVE_FUNCTION_ATTRIBUTE_FORMAT
33 #define PRINTF_ATTRIBUTE(a,b) \
34 __attribute__ ((__format__ (__printf__, a, b)))
36 #define PRINTF_ATTRIBUTE(a,b)
37 #endif /* HAVE_FUNCTION_ATTRIBUTE_FORMAT */
39 #define HAVE_CONSTRUCTOR_ATTRIBUTE
40 #ifdef HAVE_CONSTRUCTOR_ATTRIBUTE
41 #define CONSTRUCTOR_ATTRIBUTE \
42 __attribute__ ((constructor))
44 #define CONSTRUCTOR_ATTRIBUTE
45 #endif /* HAVE_CONSTRUCTOR_ATTRIBUTE */
47 #define HAVE_DESTRUCTOR_ATTRIBUTE
48 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
49 #define DESTRUCTOR_ATTRIBUTE \
50 __attribute__ ((destructor))
52 #define DESTRUCTOR_ATTRIBUTE
55 #define HAVE_ADDRESS_SANITIZER_ATTRIBUTE
56 #ifdef HAVE_ADDRESS_SANITIZER_ATTRIBUTE
57 #define DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE \
58 __attribute__((no_sanitize_address))
60 #define DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE
63 #define VCOM_SOCKET_FD_MAX 0x10000
65 static char vcom_app_name[MAX_VCOM_APP_NAME];
68 * RETURN: 0 on success or -1 on error.
71 vcom_set_app_name (char *__app_name)
73 return snprintf (vcom_app_name, MAX_VCOM_APP_NAME, "vcom-%s-%d",
74 __app_name, getpid ()) < 0 ? -1 : 0;
80 if (vcom_app_name[0] == '\0')
82 snprintf (vcom_app_name, MAX_VCOM_APP_NAME, "vcom-app-%d", getpid ());
88 * 1 if init, 0 otherwise
90 static int is_vcom_init;
93 * TBD: Make it thread safe
97 * constructor function called before main is called
98 * RETURN: 0 on success -1 on failure
103 pid_t pid = getpid ();
107 if (vppcom_app_create (vcom_get_app_name ()) != 0)
109 printf ("\n[%d] vcom_init...failed!\n", pid);
112 "[%d] vcom_init: vppcom_app_create failed!\n", pid);
115 if (vcom_socket_main_init () != 0)
117 printf ("\n[%d] vcom_init...failed!\n", pid);
120 "[%d] vcom_init: vcom_socket_main_init failed!\n", pid);
125 printf ("\n[%d] vcom_init...done!\n", pid);
133 pid_t pid = getpid ();
137 vcom_socket_main_destroy ();
138 vppcom_app_destroy ();
140 fprintf (stderr, "\n[%d] vcom_destroy...done!\n", pid);
145 is_vcom_socket_fd (int fd)
147 return vcom_socket_is_vcom_fd (fd);
151 is_vcom_epfd (int epfd)
153 return vcom_socket_is_vcom_epfd (epfd);
159 * Generic glibc fd api
163 /* Close the file descriptor FD.
165 This function is a cancellation point and therefore
166 not marked with __THROW. */
168 * PRE: is_vcom_socket_fd(__fd) == 1
169 * RETURN: 0 on success and -1 for errors.
172 vcom_close (int __fd)
174 if (vcom_init () != 0)
179 if (vcom_socket_close (__fd) != 0)
188 * RETURN: 0 on success, or -1 on error
194 pid_t pid = getpid ();
196 if (is_vcom_socket_fd (__fd) || is_vcom_epfd (__fd))
199 vcom_socket_main_show ();
200 rv = vcom_close (__fd);
202 fprintf (stderr, "[%d] close: " "'%04d'='%04d'\n", pid, rv, __fd);
204 vcom_socket_main_show ();
212 return libc_close (__fd);
215 /* Read NBYTES into BUF from FD. Return the
216 number read, -1 for errors or 0 for EOF.
218 This function is a cancellation point and therefore
219 not marked with __THROW. */
221 vcom_read (int __fd, void *__buf, size_t __nbytes)
223 if (vcom_init () != 0)
228 return vcom_socket_read (__fd, __buf, __nbytes);
232 read (int __fd, void *__buf, size_t __nbytes)
235 pid_t pid = getpid ();
236 pthread_t tid = pthread_self ();
238 if (is_vcom_socket_fd (__fd))
242 "[%d][%lu (0x%lx)] read:1 "
243 "'%04d'='%04d', '%p', '%04d'\n",
244 pid, (unsigned long) tid, (unsigned long) tid,
245 (int) size, __fd, __buf, (int) __nbytes);
246 size = vcom_read (__fd, __buf, __nbytes);
249 "[%d][%lu (0x%lx)] read:2 "
250 "'%04d'='%04d', '%p', '%04d'\n",
251 pid, (unsigned long) tid, (unsigned long) tid,
252 (int) size, __fd, __buf, (int) __nbytes);
260 return libc_read (__fd, __buf, __nbytes);
264 vcom_readv (int __fd, const struct iovec * __iov, int __iovcnt)
266 if (vcom_init () != 0)
271 return vcom_socket_readv (__fd, __iov, __iovcnt);
275 readv (int __fd, const struct iovec * __iov, int __iovcnt)
279 if (is_vcom_socket_fd (__fd))
281 size = vcom_readv (__fd, __iov, __iovcnt);
290 return libc_readv (__fd, __iov, __iovcnt);
293 /* Write N bytes of BUF to FD. Return the number written, or -1.
295 This function is a cancellation point and therefore
296 not marked with __THROW. */
298 vcom_write (int __fd, const void *__buf, size_t __n)
300 if (vcom_init () != 0)
305 return vcom_socket_write (__fd, (void *) __buf, __n);
309 write (int __fd, const void *__buf, size_t __n)
312 pid_t pid = getpid ();
313 pthread_t tid = pthread_self ();
315 if (is_vcom_socket_fd (__fd))
319 "[%d][%lu (0x%lx)] write:1 "
320 "'%04d'='%04d', '%p', '%04d'\n",
321 pid, (unsigned long) tid, (unsigned long) tid,
322 (int) size, __fd, __buf, (int) __n);
323 size = vcom_write (__fd, __buf, __n);
326 "[%d][%lu (0x%lx)] write:2 "
327 "'%04d'='%04d', '%p', '%04d'\n",
328 pid, (unsigned long) tid, (unsigned long) tid,
329 (int) size, __fd, __buf, (int) __n);
337 return libc_write (__fd, __buf, __n);
341 vcom_writev (int __fd, const struct iovec * __iov, int __iovcnt)
343 if (vcom_init () != 0)
348 return vcom_socket_writev (__fd, __iov, __iovcnt);
352 writev (int __fd, const struct iovec * __iov, int __iovcnt)
356 if (is_vcom_socket_fd (__fd))
358 size = vcom_writev (__fd, __iov, __iovcnt);
367 return libc_writev (__fd, __iov, __iovcnt);
370 /* Do the file control operation described by CMD on FD.
371 The remaining arguments are interpreted depending on CMD.
373 This function is a cancellation point and therefore
374 not marked with __THROW. */
376 vcom_fcntl_va (int __fd, int __cmd, va_list __ap)
378 if (vcom_init () != 0)
383 return vcom_socket_fcntl_va (__fd, __cmd, __ap);
387 vcom_fcntl (int __fd, int __cmd, ...)
392 if (is_vcom_socket_fd (__fd))
394 va_start (ap, __cmd);
395 rv = vcom_fcntl_va (__fd, __cmd, ap);
402 fcntl (int __fd, int __cmd, ...)
406 pid_t pid = getpid ();
408 va_start (ap, __cmd);
409 if (is_vcom_socket_fd (__fd))
411 rv = vcom_fcntl_va (__fd, __cmd, ap);
415 "'%04d'='%04d', '%04d'\n", pid, rv, __fd, __cmd);
423 rv = libc_vfcntl (__fd, __cmd, ap);
431 vcom_ioctl_va (int __fd, unsigned long int __cmd, va_list __ap)
433 if (vcom_init () != 0)
438 return vcom_socket_ioctl_va (__fd, __cmd, __ap);
442 vcom_ioctl (int __fd, unsigned long int __cmd, ...)
447 if (is_vcom_socket_fd (__fd))
449 va_start (ap, __cmd);
450 rv = vcom_ioctl_va (__fd, __cmd, ap);
457 ioctl (int __fd, unsigned long int __cmd, ...)
461 pid_t pid = getpid ();
463 va_start (ap, __cmd);
464 if (is_vcom_socket_fd (__fd))
466 rv = vcom_ioctl_va (__fd, __cmd, ap);
470 "'%04d'='%04d', '%04ld'\n", pid, rv, __fd, __cmd);
478 rv = libc_vioctl (__fd, __cmd, ap);
486 * Check the first NFDS descriptors each in READFDS (if not NULL) for
487 * read readiness, in WRITEFDS (if not NULL) for write readiness,
488 * and in EXCEPTFDS (if not NULL) for exceptional conditions.
489 * If TIMEOUT is not NULL, time out after waiting the interval
490 * specified therein. Returns the number of ready descriptors,
493 * This function is a cancellation point and therefore not marked
498 * clear all vcom FDs from fd_sets __readfds, __writefds and
499 * __exceptfds and update the new nfds
501 * new nfds is the highest-numbered file descriptor
502 * in any of the three sets, plus 1
504 * Return the number of file descriptors contained in the
505 * three descriptor sets. ie. the total number of the bits
506 * that are set in __readfds, __writefds and __exceptfds
509 vcom_fd_clear (int __nfds,
511 fd_set * __restrict __readfds,
512 fd_set * __restrict __writefds,
513 fd_set * __restrict __exceptfds)
516 /* invalid max_fd is -1 */
521 /* clear all vcom fd from the sets */
522 for (fd = 0; fd < __nfds; fd++)
525 /* clear vcom fd from set */
530 if ((F) && FD_ISSET (fd, (F))) \
532 if (is_vcom_socket_fd (fd)) \
546 * compute nfd and __new_nfds
548 for (fd = 0; fd < __nfds; fd++)
555 if ((F) && FD_ISSET (fd, (F))) \
571 *__new_nfds = max_fd != -1 ? max_fd + 1 : 0;
576 * Return the number of file descriptors contained in the
577 * three descriptor sets. ie. the total number of the bits
578 * that are set in __readfds, __writefds and __exceptfds
581 vcom_fd_set (int __nfds,
584 fd_set * __restrict __readfds,
585 fd_set * __restrict __writefds, fd_set * __restrict __exceptfds,
587 fd_set * __restrict __saved_readfds,
588 fd_set * __restrict __saved_writefds,
589 fd_set * __restrict __saved_exceptfds)
592 /* invalid max_fd is -1 */
596 for (fd = 0; fd < __nfds; fd++)
603 if ((F) && (S) && FD_ISSET (fd, (S))) \
605 if (is_vcom_socket_fd (fd)) \
612 _(__saved_readfds, __readfds);
613 _(__saved_writefds, __writefds);
619 * compute nfd and __new_nfds
621 for (fd = 0; fd < __nfds; fd++)
628 if ((F) && FD_ISSET (fd, (F))) \
644 *__new_nfds = max_fd != -1 ? max_fd + 1 : 0;
649 * split select sets(src) into
650 * vcom sets(dest1) and libc sets(dest2)
654 /* src, select sets */
656 fd_set * __restrict readfds,
657 fd_set * __restrict writefds,
658 fd_set * __restrict exceptfds,
659 /* dest1, vcom sets */
661 fd_set * __restrict vcom_readfds,
662 fd_set * __restrict vcom_writefds,
663 fd_set * __restrict vcom_exceptfds, int *vcom_nfd,
664 /* dest2, libc sets */
666 fd_set * __restrict libc_readfds,
667 fd_set * __restrict libc_writefds,
668 fd_set * __restrict libc_exceptfds, int *libc_nfd)
673 /* invalid max_fd is -1 */
674 int vcom_max_fd = -1;
678 /* invalid max_fd is -1 */
679 int libc_max_fd = -1;
683 for (fd = 0; fd < nfds; fd++)
691 if ((S) && FD_ISSET (fd, (S))) \
693 if (is_vcom_socket_fd (fd)) \
698 if (fd > vcom_max_fd) \
710 if (fd > libc_max_fd) \
720 _(readfds, vcom_readfds, libc_readfds);
721 _(writefds, vcom_writefds, libc_writefds);
722 _(exceptfds, vcom_exceptfds, libc_exceptfds);
727 *vcom_nfds = vcom_max_fd != -1 ? vcom_max_fd + 1 : 0;
729 *vcom_nfd = vcom_nfd2;
731 *libc_nfds = libc_max_fd != -1 ? libc_max_fd + 1 : 0;
733 *libc_nfd = libc_nfd2;
737 * merge vcom sets(src1) and libc sets(src2)
738 * into select sets(dest)
742 /* dest, select sets */
744 fd_set * __restrict readfds,
745 fd_set * __restrict writefds,
746 fd_set * __restrict exceptfds, int *nfd,
747 /* src1, vcom sets */
749 fd_set * __restrict vcom_readfds,
750 fd_set * __restrict vcom_writefds,
751 fd_set * __restrict vcom_exceptfds, int vcom_nfd,
752 /* src2, libc sets */
754 fd_set * __restrict libc_readfds,
755 fd_set * __restrict libc_writefds,
756 fd_set * __restrict libc_exceptfds, int libc_nfd)
759 /* invalid max_fd is -1 */
766 * dest |= src at current bit index
767 * update MAX and NFD of dest fd set
773 * MAX current max_fd of dest fd sets
774 * NFD current nfd of dest fd sets
775 * N nfds of source fd set
777 #define FD_BIT_OR(FD,FS,BI, \
779 if ((FS) && (FD) && FD_ISSET ((BI), (FS))) \
781 FD_SET ((BI), (FD)); \
792 * SR,SW,SE source RWE fd sets
793 * DR,DW,DE dest RWE fd sets
795 * NFDS nfds of source fd sets
796 * MAX current max_fd of dest fd sets
797 * NFD current nfd of dest fd sets
799 #define FD_RWE_SETS_OR(DR,DW,DE, \
805 for ((BI) = 0; (BI) < (NFDS); (BI)++) \
807 FD_BIT_OR((DR), (SR), (BI), (MAX), (NFD)); \
808 FD_BIT_OR((DW), (SW), (BI), (MAX), (NFD)); \
809 FD_BIT_OR((DE), (SE), (BI), (MAX), (NFD)); \
815 /* source(vcom) to dest(select) rwe fd sets */
816 FD_RWE_SETS_OR (readfds, writefds, exceptfds,
817 vcom_readfds, vcom_writefds, vcom_exceptfds,
818 fd, vcom_nfds, max_fd, nfd2);
820 /* source(libc) to dest(select) rwe fd sets */
821 FD_RWE_SETS_OR (readfds, writefds, exceptfds,
822 libc_readfds, libc_writefds, libc_exceptfds,
823 fd, libc_nfds, max_fd, nfd2);
825 #undef FD_RWE_SETS_OR
829 *nfds = max_fd != -1 ? max_fd + 1 : 0;
835 * RETURN 1 if fds is NULL or empty. 0 otherwise
838 fd_set_iszero (fd_set * __restrict fds)
846 for (fd = 0; fd < FD_SETSIZE; fd++)
848 if (FD_ISSET (fd, fds))
864 typedef long int s64;
865 typedef unsigned long int u64;
867 typedef long long int __s64;
868 typedef unsigned long long int __u64;
870 typedef __s64 time64_t;
871 typedef __u64 timeu64_t;
873 /* Parameters used to convert the timespec values: */
874 #define MSEC_PER_SEC 1000L
875 #define USEC_PER_MSEC 1000L
876 #define NSEC_PER_USEC 1000L
877 #define NSEC_PER_MSEC 1000000L
878 #define USEC_PER_SEC 1000000L
879 #define NSEC_PER_SEC 1000000000L
880 #define FSEC_PER_SEC 1000000000000000LL
890 #define TIME_T_MAX (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1)
893 timespec_equal (const struct timespec *a, const struct timespec *b)
895 return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec);
899 * lhs < rhs: return <0
900 * lhs == rhs: return 0
901 * lhs > rhs: return >0
904 timespec_compare (const struct timespec *lhs, const struct timespec *rhs)
906 if (lhs->tv_sec < rhs->tv_sec)
908 if (lhs->tv_sec > rhs->tv_sec)
910 return lhs->tv_nsec - rhs->tv_nsec;
914 timeval_compare (const struct timeval *lhs, const struct timeval *rhs)
916 if (lhs->tv_sec < rhs->tv_sec)
918 if (lhs->tv_sec > rhs->tv_sec)
920 return lhs->tv_usec - rhs->tv_usec;
923 extern void set_normalized_timespec (struct timespec *ts, time_t sec,
927 static inline struct timespec
928 timespec_add (struct timespec lhs, struct timespec rhs)
930 struct timespec ts_delta;
931 set_normalized_timespec (&ts_delta, lhs.tv_sec + rhs.tv_sec,
932 lhs.tv_nsec + rhs.tv_nsec);
937 * sub = lhs - rhs, in normalized form
939 static inline struct timespec
940 timespec_sub (struct timespec lhs, struct timespec rhs)
942 struct timespec ts_delta;
943 set_normalized_timespec (&ts_delta, lhs.tv_sec - rhs.tv_sec,
944 lhs.tv_nsec - rhs.tv_nsec);
956 * set_normalized_timespec - set timespec sec and nsec parts and normalize
958 * @ts: pointer to timespec variable to be set
959 * @sec: seconds to set
960 * @nsec: nanoseconds to set
962 * Set seconds and nanoseconds field of a timespec variable and
963 * normalize to the timespec storage format
965 * Note: The tv_nsec part is always in the range of
966 * 0 <= tv_nsec < NSEC_PER_SEC
967 * For negative values only the tv_sec field is negative !
970 set_normalized_timespec (struct timespec *ts, time_t sec, s64 nsec)
972 while (nsec >= NSEC_PER_SEC)
975 * The following asm() prevents the compiler from
976 * optimising this loop into a modulo operation. See
977 * also __iter_div_u64_rem() in include/linux/time.h
979 asm ("":"+rm" (nsec));
980 nsec -= NSEC_PER_SEC;
985 asm ("":"+rm" (nsec));
986 nsec += NSEC_PER_SEC;
993 #define vcom_timerisvalid(tvp) (!((tvp)->tv_sec < 0 || (tvp)->tv_usec < 0))
995 /* Macros for converting between `struct timeval' and `struct timespec'. */
996 #define VCOM_TIMEVAL_TO_TIMESPEC(tv, ts) { \
997 (ts)->tv_sec = (tv)->tv_sec; \
998 (ts)->tv_nsec = (tv)->tv_usec * 1000; \
1000 #define VCOM_TIMESPEC_TO_TIMEVAL(tv, ts) { \
1001 (tv)->tv_sec = (ts)->tv_sec; \
1002 (tv)->tv_usec = (ts)->tv_nsec / 1000; \
1006 vcom_select_impl (int vcom_nfds, fd_set * __restrict vcom_readfds,
1007 fd_set * __restrict vcom_writefds,
1008 fd_set * __restrict vcom_exceptfds,
1009 struct timeval *__restrict timeout)
1011 return vcom_socket_select (vcom_nfds, vcom_readfds,
1012 vcom_writefds, vcom_exceptfds, timeout);
1016 vcom_select (int __nfds, fd_set * __restrict __readfds,
1017 fd_set * __restrict __writefds,
1018 fd_set * __restrict __exceptfds,
1019 struct timeval *__restrict __timeout)
1023 pid_t pid = getpid ();
1026 /* block indefinitely */
1028 int first_clock_gettime_failed = 0;
1029 /* timeout value in units of timespec */
1030 struct timespec timeout_ts;
1031 struct timespec start_time, now, end_time;
1033 /* select sets attributes - after merge */
1039 fd_set vcom_readfds;
1040 fd_set vcom_writefds;
1041 fd_set vcom_exceptfds;
1046 fd_set libc_readfds;
1047 fd_set libc_writefds;
1048 fd_set libc_exceptfds;
1052 struct timeval tv = {.tv_sec = 0,.tv_usec = 0 };
1054 /* validate __timeout */
1057 /* validate tv_sec */
1059 if (!vcom_timerisvalid (__timeout))
1065 /* validate tv_usec */
1067 /* init timeout_ts */
1068 VCOM_TIMEVAL_TO_TIMESPEC (__timeout, &timeout_ts);
1069 set_normalized_timespec (&timeout_ts,
1070 timeout_ts.tv_sec, timeout_ts.tv_nsec);
1073 rv = clock_gettime (CLOCK_MONOTONIC, &start_time);
1077 first_clock_gettime_failed = 1;
1084 if (timerisset (__timeout))
1086 end_time = timespec_add (start_time, timeout_ts);
1091 * if both fields of the timeout structure are zero,
1092 * then select returns immediately
1094 end_time = start_time;
1099 /* block indefinitely */
1105 if (vcom_init () != 0)
1111 /* validate __nfds */
1112 if (__nfds < 0 || __nfds > FD_SETSIZE)
1120 * usleep(3) emulation
1123 /* call libc_select() with a finite timeout and
1124 * no file descriptors or empty fd sets and
1127 (!__readfds || fd_set_iszero (__readfds)) &&
1128 (!__writefds || fd_set_iszero (__writefds)) &&
1129 (!__exceptfds || fd_set_iszero (__exceptfds)))
1133 rv = libc_select (__nfds,
1134 __readfds, __writefds, __exceptfds, __timeout);
1140 /* TBD: block indefinitely or return -EINVAL */
1146 /* init once before the polling loop */
1148 /* zero vcom and libc fd sets */
1162 _(__readfds, &vcom_readfds, &libc_readfds);
1163 _(__writefds, &vcom_writefds, &libc_writefds);
1164 _(__exceptfds, &vcom_exceptfds, &libc_exceptfds);
1175 /* src, select sets */
1176 __nfds, __readfds, __writefds, __exceptfds,
1177 /* dest1, vcom sets */
1178 __readfds || __writefds || __exceptfds ?
1180 __readfds ? &vcom_readfds : NULL,
1181 __writefds ? &vcom_writefds : NULL,
1182 __exceptfds ? &vcom_exceptfds : NULL,
1183 __readfds || __writefds || __exceptfds ?
1185 /* dest2, libc sets */
1186 __readfds || __writefds || __exceptfds ?
1188 __readfds ? &libc_readfds : NULL,
1189 __writefds ? &libc_writefds : NULL,
1190 __exceptfds ? &libc_exceptfds : NULL,
1191 __readfds || __writefds || __exceptfds ?
1204 * if both fields of timeval structure are zero,
1205 * vcom_select_impl and libc_select returns immediately.
1206 * useful for polling and ensure fairness among
1207 * file descriptors watched.
1214 /* select on vcom fds */
1217 vcom_nfd = vcom_select_impl (vcom_nfds,
1218 __readfds ? &vcom_readfds : NULL,
1219 __writefds ? &vcom_writefds : NULL,
1220 __exceptfds ? &vcom_exceptfds : NULL,
1224 "[%d] select vcom: "
1225 "'%04d'='%04d'\n", pid, vcom_nfd, vcom_nfds);
1233 /* select on libc fds */
1236 libc_nfd = libc_select (libc_nfds,
1237 __readfds ? &libc_readfds : NULL,
1238 __writefds ? &libc_writefds : NULL,
1239 __exceptfds ? &libc_exceptfds : NULL, &tv);
1242 "[%d] select libc: "
1243 "'%04d'='%04d'\n", pid, libc_nfd, libc_nfds);
1247 /* tv becomes undefined */
1254 /* check if any file descriptors changed status */
1255 if ((vcom_nfds && vcom_nfd > 0) || (libc_nfds && libc_nfd > 0))
1257 /* zero the sets before merge and exit */
1277 * on exit, sets are modified in place to indicate which
1278 * file descriptors actually changed status
1281 /* dest, select sets */
1282 __readfds || __writefds || __exceptfds ?
1287 __readfds || __writefds || __exceptfds ?
1289 /* src1, vcom sets */
1291 __readfds ? &vcom_readfds : NULL,
1292 __writefds ? &vcom_writefds : NULL,
1293 __exceptfds ? &vcom_exceptfds : NULL, vcom_nfd,
1294 /* src2, libc sets */
1296 __readfds ? &libc_readfds : NULL,
1297 __writefds ? &libc_writefds : NULL,
1298 __exceptfds ? &libc_exceptfds : NULL, libc_nfd);
1300 * return the number of file descriptors contained in the
1301 * three returned sets
1313 rv = new_nfd == -1 ? 0 : new_nfd;
1317 rv = clock_gettime (CLOCK_MONOTONIC, &now);
1324 while (no_timeout || timespec_compare (&now, &end_time) < 0);
1326 /* timeout expired before anything interesting happened */
1332 fprintf (stderr, "[%d] vselect1: " "'%04d'='%04d'\n", pid, rv, __nfds);
1334 * modify timeout parameter to reflect the amount of time not slept
1338 if (vcom_timerisvalid (__timeout))
1340 /* timeout expired */
1343 timerclear (__timeout);
1345 else if (!first_clock_gettime_failed)
1347 rv2 = clock_gettime (CLOCK_MONOTONIC, &now);
1354 struct timespec ts_delta;
1355 ts_delta = timespec_sub (end_time, now);
1356 VCOM_TIMESPEC_TO_TIMEVAL (__timeout, &ts_delta);
1362 fprintf (stderr, "[%d] vselect2: " "'%04d',='%04d'\n", pid, rv, __nfds);
1368 vcom_select_internal (int __nfds, fd_set * __restrict __readfds,
1369 fd_set * __restrict __writefds,
1370 fd_set * __restrict __exceptfds,
1371 struct timeval *__restrict __timeout)
1376 pid_t pid = getpid ();
1378 fd_set saved_readfds;
1379 fd_set saved_writefds;
1380 fd_set saved_exceptfds;
1382 /* validate __nfds */
1389 /* validate __timeout */
1392 /* validate tv_sec */
1394 if (__timeout->tv_sec < 0 || __timeout->tv_usec < 0)
1400 /* validate tv_usec */
1404 /* init saved_x fds */
1407 saved_readfds = *__readfds;
1409 memcpy (&saved_readfds, __readfds, sizeof (*__readfds));
1414 FD_ZERO (&saved_readfds);
1419 saved_writefds = *__writefds;
1421 memcpy (&saved_writefds, __writefds, sizeof (*__writefds));
1427 FD_ZERO (&saved_writefds);
1432 saved_exceptfds = *__exceptfds;
1434 memcpy (&saved_exceptfds, __exceptfds, sizeof (*__exceptfds));
1440 FD_ZERO (&saved_exceptfds);
1443 /* clear vcom fds */
1444 nfd = vcom_fd_clear (__nfds, &new_nfds, __readfds, __writefds, __exceptfds);
1446 /* set to an invalid value */
1448 /* have kernel fds */
1450 rv = libc_select (new_nfds, __readfds,
1451 __writefds, __exceptfds, __timeout);
1453 if (new_nfds && rv == -1)
1455 /* on error, the file descriptor sets are unmodified */
1457 *__readfds = saved_readfds;
1459 *__writefds = saved_writefds;
1461 *__exceptfds = saved_exceptfds;
1464 else if ((new_nfds && rv != -1) || (rv == -2))
1466 /* restore vcom fds */
1467 nfd = vcom_fd_set (__nfds,
1472 &saved_readfds, &saved_writefds, &saved_exceptfds);
1477 fprintf (stderr, "[%d] select: " "'%04d'='%04d'\n", pid, rv, __nfds);
1482 select (int __nfds, fd_set * __restrict __readfds,
1483 fd_set * __restrict __writefds,
1484 fd_set * __restrict __exceptfds, struct timeval *__restrict __timeout)
1487 pid_t pid = getpid ();
1490 fprintf (stderr, "[%d] select1: " "'%04d'='%04d'\n", pid, rv, __nfds);
1491 rv = vcom_select (__nfds, __readfds, __writefds, __exceptfds, __timeout);
1493 fprintf (stderr, "[%d] select2: " "'%04d'='%04d'\n", pid, rv, __nfds);
1502 #ifdef __USE_XOPEN2K
1504 * Same as above only that the TIMEOUT value is given with higher
1505 * resolution and a sigmask which is been set temporarily. This
1506 * version should be used.
1508 * This function is a cancellation point and therefore not marked
1512 vcom_pselect (int __nfds, fd_set * __restrict __readfds,
1513 fd_set * __restrict __writefds,
1514 fd_set * __restrict __exceptfds,
1515 const struct timespec *__restrict __timeout,
1516 const __sigset_t * __restrict __sigmask)
1521 for (fd = 0; fd < __nfds; fd++)
1523 if (__readfds && FD_ISSET (fd, __readfds))
1525 if (is_vcom_socket_fd (fd))
1531 if (__writefds && FD_ISSET (fd, __writefds))
1533 if (is_vcom_socket_fd (fd))
1538 if (__exceptfds && FD_ISSET (fd, __exceptfds))
1540 if (is_vcom_socket_fd (fd))
1542 FD_CLR (fd, __exceptfds);
1550 pselect (int __nfds, fd_set * __restrict __readfds,
1551 fd_set * __restrict __writefds,
1552 fd_set * __restrict __exceptfds,
1553 const struct timespec *__restrict __timeout,
1554 const __sigset_t * __restrict __sigmask)
1559 pid_t pid = getpid ();
1561 fd_set saved_readfds;
1562 fd_set saved_writefds;
1563 fd_set saved_exceptfds;
1565 /* validate __nfds */
1572 /* validate __timeout */
1575 /* validate tv_sec */
1577 if (__timeout->tv_sec < 0 || __timeout->tv_nsec < 0)
1583 /* validate tv_usec */
1587 /* init saved fds */
1590 saved_readfds = *__readfds;
1592 memcpy (&saved_readfds, __readfds, sizeof (*__readfds));
1597 FD_ZERO (&saved_readfds);
1602 saved_writefds = *__writefds;
1604 memcpy (&saved_writefds, __writefds, sizeof (*__writefds));
1610 FD_ZERO (&saved_writefds);
1615 saved_exceptfds = *__exceptfds;
1617 memcpy (&saved_exceptfds, __exceptfds, sizeof (*__exceptfds));
1623 FD_ZERO (&saved_exceptfds);
1626 /* clear vcom fds */
1627 nfd = vcom_fd_clear (__nfds, &new_nfds, __readfds, __writefds, __exceptfds);
1629 /* set to an invalid value */
1632 rv = libc_pselect (new_nfds,
1634 __writefds, __exceptfds, __timeout, __sigmask);
1636 if (new_nfds && rv == -1)
1638 /* on error, the file descriptor sets are unmodified */
1640 *__readfds = saved_readfds;
1642 *__writefds = saved_writefds;
1644 *__exceptfds = saved_exceptfds;
1647 else if ((new_nfds && rv != -1) || (rv == -2))
1649 /* restore vcom fds */
1650 nfd = vcom_fd_set (__nfds,
1655 &saved_readfds, &saved_writefds, &saved_exceptfds);
1660 fprintf (stderr, "[%d] pselect: " "'%04d'='%04d'\n", pid, rv, __nfds);
1667 * Socket specific glibc api
1671 /* Create a new socket of type TYPE in domain DOMAIN, using
1672 * protocol PROTOCOL. If PROTOCOL is zero, one is chosen
1673 * automatically. Returns a file descriptor for the new socket,
1675 * RETURN: a valid file descriptor for the new socket,
1680 vcom_socket (int __domain, int __type, int __protocol)
1682 if (vcom_init () != 0)
1687 return vcom_socket_socket (__domain, __type, __protocol);
1691 socket (int __domain, int __type, int __protocol)
1694 pid_t pid = getpid ();
1695 pthread_t tid = pthread_self ();
1697 /* handle domains implemented by vpp */
1702 /* handle types implemented by vpp */
1703 switch (__type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1708 vcom_socket_main_show ();
1709 rv = vcom_socket (__domain, __type, __protocol);
1712 "[%d][%lu (0x%lx)] socket: "
1713 "'%04d'= D='%04d', T='%04d', P='%04d'\n",
1714 pid, (unsigned long) tid, (unsigned long) tid,
1715 rv, __domain, __type, __protocol);
1717 vcom_socket_main_show ();
1727 goto CALL_GLIBC_SOCKET_API;
1734 goto CALL_GLIBC_SOCKET_API;
1738 CALL_GLIBC_SOCKET_API:
1739 return libc_socket (__domain, __type, __protocol);
1743 * Create two new sockets, of type TYPE in domain DOMAIN and using
1744 * protocol PROTOCOL, which are connected to each other, and put file
1745 * descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
1746 * one will be chosen automatically.
1747 * Returns 0 on success, -1 for errors.
1750 vcom_socketpair (int __domain, int __type, int __protocol, int __fds[2])
1752 if (vcom_init () != 0)
1757 return vcom_socket_socketpair (__domain, __type, __protocol, __fds);
1761 socketpair (int __domain, int __type, int __protocol, int __fds[2])
1764 pid_t pid = getpid ();
1766 /* handle domains implemented by vpp */
1771 /* handle types implemented by vpp */
1776 rv = vcom_socketpair (__domain, __type, __protocol, __fds);
1780 "'%04d'= D='%04d', T='%04d', P='%04d'\n",
1781 pid, rv, __domain, __type, __protocol);
1791 goto CALL_GLIBC_SOCKET_API;
1798 goto CALL_GLIBC_SOCKET_API;
1802 CALL_GLIBC_SOCKET_API:
1803 return libc_socketpair (__domain, __type, __protocol, __fds);
1807 * Give the socket FD the local address ADDR
1808 * (which is LEN bytes long).
1811 vcom_bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
1815 if (vcom_init () != 0)
1820 /* validate __len */
1821 switch (__addr->sa_family)
1824 if (__len != sizeof (struct sockaddr_in))
1828 if (__len != sizeof (struct sockaddr_in6))
1837 /* handle domains implemented by vpp */
1838 switch (__addr->sa_family)
1842 rv = vcom_socket_bind (__fd, __addr, __len);
1855 bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
1858 pid_t pid = getpid ();
1860 if (is_vcom_socket_fd (__fd))
1863 rv = vcom_bind (__fd, __addr, __len);
1867 "'%04d'='%04d', '%p', '%04d'\n",
1868 pid, rv, __fd, __addr, __len);
1876 return libc_bind (__fd, __addr, __len);
1880 * Put the local address of FD into *ADDR and its length in *LEN.
1883 vcom_getsockname (int __fd, __SOCKADDR_ARG __addr,
1884 socklen_t * __restrict __len)
1886 if (vcom_init () != 0)
1891 return vcom_socket_getsockname (__fd, __addr, __len);
1895 getsockname (int __fd, __SOCKADDR_ARG __addr, socklen_t * __restrict __len)
1898 pid_t pid = getpid ();
1900 if (is_vcom_socket_fd (__fd))
1902 rv = vcom_getsockname (__fd, __addr, __len);
1905 "[%d] getsockname: "
1906 "'%04d'='%04d', '%p', '%p'\n", pid, rv, __fd, __addr, __len);
1914 return libc_getsockname (__fd, __addr, __len);
1918 * Open a connection on socket FD to peer at ADDR
1919 * (which LEN bytes long). For connectionless socket types, just set
1920 * the default address to send to and the only address from which to
1921 * accept transmissions. Return 0 on success, -1 for errors.
1922 * This function is a cancellation point and therefore not marked
1926 vcom_connect (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
1930 if (vcom_init () != 0)
1935 /* validate __len */
1936 switch (__addr->sa_family)
1939 if (__len != INET_ADDRSTRLEN)
1943 if (__len != INET6_ADDRSTRLEN)
1952 /* handle domains implemented by vpp */
1953 switch (__addr->sa_family)
1957 rv = vcom_socket_connect (__fd, __addr, __len);
1969 connect (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
1972 pid_t pid = getpid ();
1973 pthread_t tid = pthread_self ();
1975 if (is_vcom_socket_fd (__fd))
1977 rv = vcom_connect (__fd, __addr, __len);
1980 "[%d][%lu (0x%lx)] connect: "
1981 "'%04d'='%04d', '%p', '%04d'\n",
1982 pid, (unsigned long) tid, (unsigned long) tid,
1983 rv, __fd, __addr, __len);
1992 return libc_connect (__fd, __addr, __len);
1996 * Put the address of the peer connected to socket FD into *ADDR
1997 * (which is *LEN bytes long), and its actual length into *LEN.
2000 vcom_getpeername (int __fd, __SOCKADDR_ARG __addr,
2001 socklen_t * __restrict __len)
2003 if (vcom_init () != 0)
2008 return vcom_socket_getpeername (__fd, __addr, __len);
2012 getpeername (int __fd, __SOCKADDR_ARG __addr, socklen_t * __restrict __len)
2015 pid_t pid = getpid ();
2017 if (is_vcom_socket_fd (__fd))
2019 rv = vcom_getpeername (__fd, __addr, __len);
2022 "[%d] getpeername: "
2023 "'%04d'='%04d', '%p', '%p'\n", pid, rv, __fd, __addr, __len);
2031 return libc_getpeername (__fd, __addr, __len);
2035 * Send N bytes of BUF to socket FD. Returns the number sent or -1.
2036 * This function is a cancellation point and therefore not marked
2040 vcom_send (int __fd, const void *__buf, size_t __n, int __flags)
2043 if (vcom_init () != 0)
2048 return vcom_socket_send (__fd, (void *) __buf, (int) __n, __flags);
2052 send (int __fd, const void *__buf, size_t __n, int __flags)
2055 pid_t pid = getpid ();
2057 if (is_vcom_socket_fd (__fd))
2059 size = vcom_send (__fd, __buf, __n, __flags);
2063 "'%04d'='%04d', '%p', '%04d', '%04x'\n",
2064 pid, (int) size, __fd, __buf, (int) __n, __flags);
2072 return libc_send (__fd, __buf, __n, __flags);
2076 * Read N bytes into BUF from socket FD.
2077 * Returns the number read or -1 for errors.
2078 * This function is a cancellation point and therefore not marked
2082 vcom_recv (int __fd, void *__buf, size_t __n, int __flags)
2084 if (vcom_init () != 0)
2089 return vcom_socket_recv (__fd, __buf, __n, __flags);
2093 recv (int __fd, void *__buf, size_t __n, int __flags)
2096 pid_t pid = getpid ();
2098 if (is_vcom_socket_fd (__fd))
2100 size = vcom_recv (__fd, __buf, __n, __flags);
2104 "'%04d'='%04d', '%p', '%04d', '%04x'\n",
2105 pid, (int) size, __fd, __buf, (int) __n, __flags);
2113 return libc_recv (__fd, __buf, __n, __flags);
2117 * Send N bytes of BUF on socket FD to peer at address ADDR (which is
2118 * ADDR_LEN bytes long). Returns the number sent, or -1 for errors.
2119 * This function is a cancellation point and therefore not marked
2123 vcom_sendto (int __fd, const void *__buf, size_t __n, int __flags,
2124 __CONST_SOCKADDR_ARG __addr, socklen_t __addr_len)
2126 if (vcom_init () != 0)
2131 return vcom_socket_sendto (__fd, __buf, __n, __flags, __addr, __addr_len);
2135 sendto (int __fd, const void *__buf, size_t __n, int __flags,
2136 __CONST_SOCKADDR_ARG __addr, socklen_t __addr_len)
2139 pid_t pid = getpid ();
2141 if (is_vcom_socket_fd (__fd))
2143 size = vcom_sendto (__fd, __buf, __n, __flags, __addr, __addr_len);
2147 "'%04d'='%04d', '%p', '%04d', '%04x', "
2149 pid, (int) size, __fd, __buf, (int) __n, __flags,
2150 __addr, __addr_len);
2158 return libc_sendto (__fd, __buf, __n, __flags, __addr, __addr_len);
2162 * Read N bytes into BUF through socket FD.
2163 * If ADDR is not NULL, fill in *ADDR_LEN bytes of it with the
2164 * address of the sender, and store the actual size of the address
2166 * Returns the number of bytes read or -1 for errors.
2167 * This function is a cancellation point and therefore not marked
2171 vcom_recvfrom (int __fd, void *__restrict __buf, size_t __n,
2173 __SOCKADDR_ARG __addr, socklen_t * __restrict __addr_len)
2175 if (vcom_init () != 0)
2180 return vcom_socket_recvfrom (__fd, __buf, __n, __flags, __addr, __addr_len);
2184 recvfrom (int __fd, void *__restrict __buf, size_t __n,
2186 __SOCKADDR_ARG __addr, socklen_t * __restrict __addr_len)
2189 pid_t pid = getpid ();
2191 if (is_vcom_socket_fd (__fd))
2193 size = vcom_recvfrom (__fd, __buf, __n, __flags, __addr, __addr_len);
2197 "'%04d'='%04d', '%p', '%04d', '%04x', "
2199 pid, (int) size, __fd, __buf, (int) __n, __flags,
2200 __addr, __addr_len);
2208 return libc_recvfrom (__fd, __buf, __n, __flags, __addr, __addr_len);
2212 * Send a message described MESSAGE on socket FD.
2213 * Returns the number of bytes sent, or -1 for errors.
2214 * This function is a cancellation point and therefore not marked
2218 vcom_sendmsg (int __fd, const struct msghdr * __message, int __flags)
2220 if (vcom_init () != 0)
2225 return vcom_socket_sendmsg (__fd, __message, __flags);
2229 sendmsg (int __fd, const struct msghdr * __message, int __flags)
2232 pid_t pid = getpid ();
2234 if (is_vcom_socket_fd (__fd))
2236 size = vcom_sendmsg (__fd, __message, __flags);
2240 "'%04d'='%04d', '%p', '%04x'\n",
2241 pid, (int) size, __fd, __message, __flags);
2249 return libc_sendmsg (__fd, __message, __flags);
2254 * Send a VLEN messages as described by VMESSAGES to socket FD.
2255 * Returns the number of datagrams successfully written
2257 * This function is a cancellation point and therefore not marked
2261 vcom_sendmmsg (int __fd, struct mmsghdr *__vmessages,
2262 unsigned int __vlen, int __flags)
2264 if (vcom_init () != 0)
2269 return vcom_socket_sendmmsg (__fd, __message, __vlen, __flags);
2273 sendmmsg (int __fd, struct mmsghdr *__vmessages,
2274 unsigned int __vlen, int __flags)
2277 pid_t pid = getpid ();
2279 if (is_vcom_socket_fd (__fd))
2281 size = vcom_sendmmsg (__fd, __message, __vlen, __flags);
2285 "'%04d'='%04d', '%p', '%04d', '%04x'\n",
2286 pid, (int) size, __fd, __vmessages, __vlen, __flags);
2294 return libc_sendmmsg (__fd, __message, __vlen, __flags);
2300 * Receive a message as described by MESSAGE from socket FD.
2301 * Returns the number of bytes read or -1 for errors.
2302 * This function is a cancellation point and therefore not marked
2306 vcom_recvmsg (int __fd, struct msghdr * __message, int __flags)
2308 if (vcom_init () != 0)
2313 return vcom_socket_recvmsg (__fd, __message, __flags);
2317 recvmsg (int __fd, struct msghdr * __message, int __flags)
2320 pid_t pid = getpid ();
2322 if (is_vcom_socket_fd (__fd))
2324 size = vcom_recvmsg (__fd, __message, __flags);
2328 "'%04d'='%04d', '%p', '%04x'\n",
2329 pid, (int) size, __fd, __message, __flags);
2337 return libc_recvmsg (__fd, __message, __flags);
2342 * Receive up to VLEN messages as described by VMESSAGES from socket FD.
2343 * Returns the number of messages received or -1 for errors.
2344 * This function is a cancellation point and therefore not marked
2348 vcom_recvmmsg (int __fd, struct mmsghdr *__vmessages,
2349 unsigned int __vlen, int __flags, struct timespec *__tmo)
2351 if (vcom_init () != 0)
2356 return vcom_socket_recvmmsg (__fd, __message, __vlen, __flags, __tmo);
2360 recvmmsg (int __fd, struct mmsghdr *__vmessages,
2361 unsigned int __vlen, int __flags, struct timespec *__tmo)
2364 pid_t pid = getpid ();
2366 if (is_vcom_socket_fd (__fd))
2368 size = vcom_recvmmsg (__fd, __message, __vlen, __flags, __tmo);
2372 "'%04d'='%04d', '%p', "
2373 "'%04d', '%04x', '%p'\n",
2374 pid, (int) size, __fd, __vmessages, __vlen, __flags, __tmo);
2382 return libc_recvmmsg (__fd, __message, __vlen, __flags, __tmo);
2388 * Put the current value for socket FD's option OPTNAME
2389 * at protocol level LEVEL into OPTVAL (which is *OPTLEN bytes long),
2390 * and set *OPTLEN to the value's actual length.
2391 * Returns 0 on success, -1 for errors.
2394 vcom_getsockopt (int __fd, int __level, int __optname,
2395 void *__restrict __optval, socklen_t * __restrict __optlen)
2397 if (vcom_init () != 0)
2402 return vcom_socket_getsockopt (__fd, __level, __optname,
2403 __optval, __optlen);
2407 getsockopt (int __fd, int __level, int __optname,
2408 void *__restrict __optval, socklen_t * __restrict __optlen)
2411 pid_t pid = getpid ();
2413 if (is_vcom_socket_fd (__fd))
2415 rv = vcom_getsockopt (__fd, __level, __optname, __optval, __optlen);
2419 "'%04d'='%04d', '%04d', '%04d', "
2421 pid, rv, __fd, __level, __optname, __optval, __optlen);
2429 return libc_getsockopt (__fd, __level, __optname, __optval, __optlen);
2433 * Set socket FD's option OPTNAME at protocol level LEVEL
2434 * to *OPTVAL (which is OPTLEN bytes long).
2435 * Returns 0 on success, -1 for errors.
2438 vcom_setsockopt (int __fd, int __level, int __optname,
2439 const void *__optval, socklen_t __optlen)
2441 if (vcom_init () != 0)
2446 return vcom_socket_setsockopt (__fd, __level, __optname,
2447 __optval, __optlen);
2451 setsockopt (int __fd, int __level, int __optname,
2452 const void *__optval, socklen_t __optlen)
2455 pid_t pid = getpid ();
2457 if (is_vcom_socket_fd (__fd))
2459 rv = vcom_setsockopt (__fd, __level, __optname, __optval, __optlen);
2463 "'%04d'='%04d', '%04d', '%04d', "
2465 pid, rv, __fd, __level, __optname, __optval, __optlen);
2473 return libc_setsockopt (__fd, __level, __optname, __optval, __optlen);
2477 * Prepare to accept connections on socket FD.
2478 * N connection requests will be queued before further
2479 * requests are refused.
2480 * Returns 0 on success, -1 for errors.
2483 vcom_listen (int __fd, int __n)
2485 if (vcom_init () != 0)
2490 return vcom_socket_listen (__fd, __n);
2494 listen (int __fd, int __n)
2497 pid_t pid = getpid ();
2499 if (is_vcom_socket_fd (__fd))
2501 rv = vcom_listen (__fd, __n);
2505 "'%04d'='%04d', '%04d'\n", pid, rv, __fd, __n);
2513 return libc_listen (__fd, __n);
2517 * Await a connection on socket FD.
2518 * When a connection arrives, open a new socket to communicate
2519 * with it, set *ADDR (which is *ADDR_LEN bytes long) to the address
2520 * of the connecting peer and *ADDR_LEN to the address's actual
2521 * length, and return the new socket's descriptor, or -1 for errors.
2522 * This function is a cancellation point and therefore not marked
2526 vcom_accept (int __fd, __SOCKADDR_ARG __addr,
2527 socklen_t * __restrict __addr_len)
2530 if (vcom_init () != 0)
2534 return vcom_socket_accept (__fd, __addr, __addr_len);
2538 accept (int __fd, __SOCKADDR_ARG __addr, socklen_t * __restrict __addr_len)
2541 pid_t pid = getpid ();
2542 pthread_t tid = pthread_self ();
2544 if (is_vcom_socket_fd (__fd))
2547 vcom_socket_main_show ();
2550 "[%d][%lu (0x%lx)] accept1: "
2551 "'%04d'='%04d', '%p', '%p'\n",
2552 pid, (unsigned long) tid, (unsigned long) tid,
2553 rv, __fd, __addr, __addr_len);
2554 rv = vcom_accept (__fd, __addr, __addr_len);
2557 "[%d][%lu (0x%lx)] accept2: "
2558 "'%04d'='%04d', '%p', '%p'\n",
2559 pid, (unsigned long) tid, (unsigned long) tid,
2560 rv, __fd, __addr, __addr_len);
2562 vcom_socket_main_show ();
2570 return libc_accept (__fd, __addr, __addr_len);
2575 * Similar to 'accept' but takes an additional parameter to specify
2577 * This function is a cancellation point and therefore not marked
2581 vcom_accept4 (int __fd, __SOCKADDR_ARG __addr,
2582 socklen_t * __restrict __addr_len, int __flags)
2585 if (vcom_init () != 0)
2590 return vcom_socket_accept4 (__fd, __addr, __addr_len, __flags);
2594 accept4 (int __fd, __SOCKADDR_ARG __addr,
2595 socklen_t * __restrict __addr_len, int __flags)
2598 pid_t pid = getpid ();
2600 if (is_vcom_socket_fd (__fd))
2603 vcom_socket_main_show ();
2604 rv = vcom_accept4 (__fd, __addr, __addr_len, __flags);
2608 "'%04d'='%04d', '%p', '%p', '%04x'\n",
2609 pid, rv, __fd, __addr, __addr_len, __flags);
2611 vcom_socket_main_show ();
2619 return libc_accept4 (__fd, __addr, __addr_len, __flags);
2625 * Shut down all or part of the connection open on socket FD.
2626 * HOW determines what to shut down:
2627 * SHUT_RD = No more receptions;
2628 * SHUT_WR = No more transmissions;
2629 * SHUT_RDWR = No more receptions or transmissions.
2630 * Returns 0 on success, -1 for errors.
2633 vcom_shutdown (int __fd, int __how)
2635 if (vcom_init () != 0)
2639 return vcom_socket_shutdown (__fd, __how);
2643 shutdown (int __fd, int __how)
2646 pid_t pid = getpid ();
2648 if (is_vcom_socket_fd (__fd))
2650 rv = vcom_shutdown (__fd, __how);
2654 "'%04d'='%04d', '%04d'\n", pid, rv, __fd, __how);
2662 return libc_shutdown (__fd, __how);
2666 vcom_epoll_create (int __size)
2669 if (vcom_init () != 0)
2679 /* __size argument is ignored "thereafter" */
2680 return vcom_epoll_create1 (0);
2684 * __size argument is ignored, but must be greater than zero
2687 epoll_create (int __size)
2690 pid_t pid = getpid ();
2692 rv = vcom_epoll_create (__size);
2695 "[%d] epoll_create: " "'%04d'='%04d'\n", pid, rv, __size);
2705 vcom_epoll_create1 (int __flags)
2707 if (vcom_init () != 0)
2716 if (__flags & ~EPOLL_CLOEXEC)
2720 /* __flags can be either zero or EPOLL_CLOEXEC */
2721 /* implementation */
2722 return vcom_socket_epoll_create1 (__flags);
2726 * __flags can be either zero or EPOLL_CLOEXEC
2729 epoll_create1 (int __flags)
2732 pid_t pid = getpid ();
2734 rv = vcom_epoll_create1 (__flags);
2737 "[%d] epoll_create: " "'%04d'='%08x'\n", pid, rv, __flags);
2747 ep_op_has_event (int op)
2749 return op != EPOLL_CTL_DEL;
2753 vcom_epoll_ctl (int __epfd, int __op, int __fd, struct epoll_event *__event)
2755 if (vcom_init () != 0)
2761 * the requested operation __op is not supported
2762 * by this interface */
2763 if (!((__op == EPOLL_CTL_ADD) ||
2764 (__op == EPOLL_CTL_MOD) || (__op == EPOLL_CTL_DEL)))
2769 /* op is ADD or MOD but event parameter is NULL */
2770 if ((ep_op_has_event (__op) && !__event))
2775 /* fd is same as epfd */
2776 /* do not permit adding an epoll file descriptor inside itself */
2782 /* implementation */
2783 return vcom_socket_epoll_ctl (__epfd, __op, __fd, __event);
2787 * implement the controller interface for epoll
2788 * that enables the insertion/removal/change of
2789 * file descriptors inside the interest set.
2792 epoll_ctl (int __epfd, int __op, int __fd, struct epoll_event *__event)
2795 pid_t pid = getpid ();
2797 if (is_vcom_epfd (__epfd))
2799 /* TBD: currently limiting epoll to support only vcom fds */
2800 if (is_vcom_socket_fd (__fd))
2802 rv = vcom_epoll_ctl (__epfd, __op, __fd, __event);
2806 "'%04d'='%04d', '%04d', '%04d'\n",
2807 pid, rv, __epfd, __op, __fd);
2818 * TBD: currently epoll does not support kernel fds
2826 /* epfd is not an epoll file descriptor */
2834 vcom_epoll_wait (int __epfd, struct epoll_event *__events,
2835 int __maxevents, int __timeout)
2837 if (vcom_init () != 0)
2842 return vcom_epoll_pwait (__epfd, __events, __maxevents, __timeout, NULL);
2846 epoll_wait (int __epfd, struct epoll_event *__events,
2847 int __maxevents, int __timeout)
2850 pid_t pid = getpid ();
2852 if (__maxevents <= 0 || __maxevents > EP_MAX_EVENTS)
2858 if (is_vcom_epfd (__epfd))
2860 rv = vcom_epoll_wait (__epfd, __events, __maxevents, __timeout);
2864 "'%04d'='%04d', '%p', "
2866 pid, rv, __epfd, __events, __maxevents, __timeout);
2884 vcom_epoll_pwait (int __epfd, struct epoll_event *__events,
2885 int __maxevents, int __timeout, const __sigset_t * __ss)
2887 if (vcom_init () != 0)
2892 /* implementation */
2893 return vcom_socket_epoll_pwait (__epfd, __events,
2894 __maxevents, __timeout, __ss);
2898 epoll_pwait (int __epfd, struct epoll_event *__events,
2899 int __maxevents, int __timeout, const __sigset_t * __ss)
2902 pid_t pid = getpid ();
2904 if (__maxevents <= 0 || __maxevents > EP_MAX_EVENTS)
2910 if (is_vcom_epfd (__epfd))
2912 rv = vcom_epoll_pwait (__epfd, __events, __maxevents, __timeout, __ss);
2915 "[%d] epoll_pwait: "
2916 "'%04d'='%04d', '%p', "
2919 pid, rv, __epfd, __events, __maxevents, __timeout, __ss);
2936 /* Poll the file descriptors described by the NFDS structures starting at
2937 FDS. If TIMEOUT is nonzero and not -1, allow TIMEOUT milliseconds for
2938 an event to occur; if TIMEOUT is -1, block until an event occurs.
2939 Returns the number of file descriptors with events, zero if timed out,
2942 This function is a cancellation point and therefore not marked with
2946 vcom_poll (struct pollfd *__fds, nfds_t __nfds, int __timeout)
2949 pid_t pid = getpid ();
2951 struct rlimit nofile_limit;
2952 struct pollfd vcom_fds[MAX_POLL_NFDS_DEFAULT];
2955 /* actual set of file descriptors to be monitored */
2956 nfds_t libc_nfds = 0;
2957 nfds_t vcom_nfds = 0;
2959 /* ready file descriptors
2961 * number of structures which have nonzero revents fields
2962 * in other words, descriptors with events or errors reported.
2964 /* after call to libc_poll () */
2966 /* after call to vcom_socket_poll () */
2970 /* timeout value in units of timespec */
2971 struct timespec timeout_ts;
2972 struct timespec start_time, now, end_time;
2975 /* get start_time */
2976 rv = clock_gettime (CLOCK_MONOTONIC, &start_time);
2983 /* set timeout_ts & end_time */
2986 /* set timeout_ts */
2987 timeout_ts.tv_sec = __timeout / MSEC_PER_SEC;
2988 timeout_ts.tv_nsec = (__timeout % MSEC_PER_SEC) * NSEC_PER_MSEC;
2989 set_normalized_timespec (&timeout_ts,
2990 timeout_ts.tv_sec, timeout_ts.tv_nsec);
2994 end_time = timespec_add (start_time, timeout_ts);
2998 end_time = start_time;
3002 if (vcom_init () != 0)
3008 /* validate __fds */
3015 /* validate __nfds */
3016 /*TBD: call getrlimit once when vcl-ldpreload library is init */
3017 rv = getrlimit (RLIMIT_NOFILE, &nofile_limit);
3023 if (__nfds >= nofile_limit.rlim_cur || __nfds < 0)
3030 * for the POC, it's fair to assume that nfds is less than 1024
3032 if (__nfds >= MAX_POLL_NFDS_DEFAULT)
3038 /* set revents field (output parameter)
3041 for (fds_idx = 0; fds_idx < __nfds; fds_idx++)
3043 __fds[fds_idx].revents = 0;
3047 /* set revents field (output parameter)
3048 * to zero for user ignored fds
3050 for (fds_idx = 0; fds_idx < __nfds; fds_idx++)
3053 * if negative fd, ignore events field
3054 * and set output parameter (revents field) to zero */
3055 if (__fds[fds_idx].fd < 0)
3057 __fds[fds_idx].revents = 0;
3063 * 00. prepare __fds and vcom_fds for polling
3064 * copy __fds to vcom_fds
3065 * 01. negate all except libc fds in __fds,
3066 * ignore user negated fds
3067 * 02. negate all except vcom_fds in vocm fds,
3068 * ignore user negated fds
3069 * ignore fd 0 by setting it to negative number
3071 memcpy (vcom_fds, __fds, sizeof (*__fds) * __nfds);
3074 for (fds_idx = 0; fds_idx < __nfds; fds_idx++)
3076 /* ignore negative fds */
3077 if (__fds[fds_idx].fd < 0)
3083 * 00. ignore vcom fds in __fds
3084 * 01. ignore libc fds in vcom_fds,
3085 * ignore fd 0 by setting it to negative number.
3086 * as fd 0 cannot be ignored.
3088 if (is_vcom_socket_fd (__fds[fds_idx].fd) ||
3089 is_vcom_epfd (__fds[fds_idx].fd))
3091 __fds[fds_idx].fd = -__fds[fds_idx].fd;
3097 /* ignore fd 0 by setting it to negative number */
3098 if (!vcom_fds[fds_idx].fd)
3100 vcom_fds[fds_idx].fd = -1;
3102 vcom_fds[fds_idx].fd = -vcom_fds[fds_idx].fd;
3109 * poll on libc fds and vcom fds
3111 * specifying a timeout of zero causes libc_poll() and
3112 * vcom_socket_poll() to return immediately, even if no
3113 * file descriptors are ready
3121 * timeout parameter for libc_poll () set to zero
3122 * to poll on libc fds
3125 /* poll on libc fds */
3129 * a timeout of zero causes libc_poll()
3130 * to return immediately
3132 rlibc_nfds = libc_poll (__fds, __nfds, 0);
3136 "'%04d'='%08lu'\n", pid, rlibc_nfds, __nfds);
3141 goto poll_done_update_nfds;
3146 * timeout parameter for vcom_socket_poll () set to zero
3147 * to poll on vcom fds
3150 /* poll on vcom fds */
3154 * a timeout of zero causes vcom_socket_poll()
3155 * to return immediately
3157 rvcom_nfds = vcom_socket_poll (vcom_fds, __nfds, 0);
3161 "'%04d'='%08lu'\n", pid, rvcom_nfds, __nfds);
3165 goto poll_done_update_nfds;
3169 /* check if any file descriptors changed status */
3170 if ((libc_nfds && rlibc_nfds > 0) || (vcom_nfds && rvcom_nfds > 0))
3172 /* something interesting happened */
3173 rv = rlibc_nfds + rvcom_nfds;
3174 goto poll_done_update_nfds;
3177 rv = clock_gettime (CLOCK_MONOTONIC, &now);
3181 goto poll_done_update_nfds;
3185 /* block indefinitely || timeout elapsed */
3186 while ((__timeout < 0) || timespec_compare (&now, &end_time) < 0);
3188 /* timeout expired before anything interesting happened */
3191 poll_done_update_nfds:
3192 for (fds_idx = 0; fds_idx < __nfds; fds_idx++)
3194 /* ignore negative fds in vcom_fds
3195 * 00. user negated fds
3198 if (vcom_fds[fds_idx].fd < 0)
3203 /* from here on handle positive vcom fds */
3205 * restore vcom fds to positive number in __fds
3206 * and update revents in __fds with the events
3207 * that actually occurred in vcom fds
3209 __fds[fds_idx].fd = -__fds[fds_idx].fd;
3212 __fds[fds_idx].revents = vcom_fds[fds_idx].revents;
3218 fprintf (stderr, "[%d] vpoll: " "'%04d'='%08lu'\n", pid, rv, __nfds);
3223 * 00. The field __fds[i].fd contains a file descriptor for an
3225 * If this field is negative, then the corresponding
3226 * events field is ignored and the revents field returns zero.
3227 * The field __fds[i].events is an input parameter.
3228 * The field __fds[i].revents is an output parameter.
3229 * 01. Specifying a negative value in timeout
3230 * means an infinite timeout.
3231 * Specifying a timeout of zero causes poll() to return
3232 * immediately, even if no file descriptors are ready.
3234 * NOTE: observed __nfds is less than 128 from kubecon strace files
3239 poll (struct pollfd *__fds, nfds_t __nfds, int __timeout)
3242 pid_t pid = getpid ();
3246 fprintf (stderr, "[%d] poll1: " "'%04d'='%08lu, %d, 0x%x'\n",
3247 pid, rv, __nfds, __fds[0].fd, __fds[0].events);
3248 rv = vcom_poll (__fds, __nfds, __timeout);
3250 fprintf (stderr, "[%d] poll2: " "'%04d'='%08lu, %d, 0x%x'\n",
3251 pid, rv, __nfds, __fds[0].fd, __fds[0].revents);
3261 /* Like poll, but before waiting the threads signal mask is replaced
3262 with that specified in the fourth parameter. For better usability,
3263 the timeout value is specified using a TIMESPEC object.
3265 This function is a cancellation point and therefore not marked with
3268 vcom_ppoll (struct pollfd *__fds, nfds_t __nfds,
3269 const struct timespec *__timeout, const __sigset_t * __ss)
3271 if (vcom_init () != 0)
3280 ppoll (struct pollfd *__fds, nfds_t __nfds,
3281 const struct timespec *__timeout, const __sigset_t * __ss)
3291 void CONSTRUCTOR_ATTRIBUTE vcom_constructor (void);
3293 void DESTRUCTOR_ATTRIBUTE vcom_destructor (void);
3296 vcom_constructor (void)
3298 pid_t pid = getpid ();
3300 swrap_constructor ();
3301 if (vcom_init () != 0)
3303 printf ("\n[%d] vcom_constructor...failed!\n", pid);
3307 printf ("\n[%d] vcom_constructor...done!\n", pid);
3312 * This function is called when the library is unloaded
3315 vcom_destructor (void)
3317 pid_t pid = getpid ();
3320 swrap_destructor ();
3321 printf ("\n[%d] vcom_destructor...done!\n", pid);
3326 * fd.io coding-style-patch-verification: ON
3329 * eval: (c-set-style "gnu")