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 <vcl/vcom_socket_wrapper.h>
28 #include <vcl/vppcom.h>
29 #include <vcl/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)
892 #ifdef VCOM_USE_TIMESPEC_EQUAL
894 timespec_equal (const struct timespec *a, const struct timespec *b)
896 return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec);
901 * lhs < rhs: return <0
902 * lhs == rhs: return 0
903 * lhs > rhs: return >0
906 timespec_compare (const struct timespec *lhs, const struct timespec *rhs)
908 if (lhs->tv_sec < rhs->tv_sec)
910 if (lhs->tv_sec > rhs->tv_sec)
912 return lhs->tv_nsec - rhs->tv_nsec;
915 #ifdef VCOM_USE_TIMEVAL_COMPARE
917 timeval_compare (const struct timeval *lhs, const struct timeval *rhs)
919 if (lhs->tv_sec < rhs->tv_sec)
921 if (lhs->tv_sec > rhs->tv_sec)
923 return lhs->tv_usec - rhs->tv_usec;
927 extern void set_normalized_timespec (struct timespec *ts, time_t sec,
930 static inline struct timespec
931 timespec_add (struct timespec lhs, struct timespec rhs)
933 struct timespec ts_delta;
934 set_normalized_timespec (&ts_delta, lhs.tv_sec + rhs.tv_sec,
935 lhs.tv_nsec + rhs.tv_nsec);
940 * sub = lhs - rhs, in normalized form
942 static inline struct timespec
943 timespec_sub (struct timespec lhs, struct timespec rhs)
945 struct timespec ts_delta;
946 set_normalized_timespec (&ts_delta, lhs.tv_sec - rhs.tv_sec,
947 lhs.tv_nsec - rhs.tv_nsec);
959 * set_normalized_timespec - set timespec sec and nsec parts and normalize
961 * @ts: pointer to timespec variable to be set
962 * @sec: seconds to set
963 * @nsec: nanoseconds to set
965 * Set seconds and nanoseconds field of a timespec variable and
966 * normalize to the timespec storage format
968 * Note: The tv_nsec part is always in the range of
969 * 0 <= tv_nsec < NSEC_PER_SEC
970 * For negative values only the tv_sec field is negative !
973 set_normalized_timespec (struct timespec *ts, time_t sec, s64 nsec)
975 while (nsec >= NSEC_PER_SEC)
978 * The following asm() prevents the compiler from
979 * optimising this loop into a modulo operation. See
980 * also __iter_div_u64_rem() in include/linux/time.h
982 asm ("":"+rm" (nsec));
983 nsec -= NSEC_PER_SEC;
988 asm ("":"+rm" (nsec));
989 nsec += NSEC_PER_SEC;
996 #define vcom_timerisvalid(tvp) (!((tvp)->tv_sec < 0 || (tvp)->tv_usec < 0))
998 /* Macros for converting between `struct timeval' and `struct timespec'. */
999 #define VCOM_TIMEVAL_TO_TIMESPEC(tv, ts) { \
1000 (ts)->tv_sec = (tv)->tv_sec; \
1001 (ts)->tv_nsec = (tv)->tv_usec * 1000; \
1003 #define VCOM_TIMESPEC_TO_TIMEVAL(tv, ts) { \
1004 (tv)->tv_sec = (ts)->tv_sec; \
1005 (tv)->tv_usec = (ts)->tv_nsec / 1000; \
1009 vcom_select_impl (int vcom_nfds, fd_set * __restrict vcom_readfds,
1010 fd_set * __restrict vcom_writefds,
1011 fd_set * __restrict vcom_exceptfds,
1012 struct timeval *__restrict timeout)
1014 return vcom_socket_select (vcom_nfds, vcom_readfds,
1015 vcom_writefds, vcom_exceptfds, timeout);
1019 vcom_select (int __nfds, fd_set * __restrict __readfds,
1020 fd_set * __restrict __writefds,
1021 fd_set * __restrict __exceptfds,
1022 struct timeval *__restrict __timeout)
1026 pid_t pid = getpid ();
1029 /* block indefinitely */
1031 int first_clock_gettime_failed = 0;
1032 /* timeout value in units of timespec */
1033 struct timespec timeout_ts;
1034 struct timespec start_time, now, end_time;
1036 /* select sets attributes - after merge */
1042 fd_set vcom_readfds;
1043 fd_set vcom_writefds;
1044 fd_set vcom_exceptfds;
1049 fd_set libc_readfds;
1050 fd_set libc_writefds;
1051 fd_set libc_exceptfds;
1055 struct timeval tv = {.tv_sec = 0,.tv_usec = 0 };
1057 /* validate __timeout */
1060 /* validate tv_sec */
1062 if (!vcom_timerisvalid (__timeout))
1068 /* validate tv_usec */
1070 /* init timeout_ts */
1071 VCOM_TIMEVAL_TO_TIMESPEC (__timeout, &timeout_ts);
1072 set_normalized_timespec (&timeout_ts,
1073 timeout_ts.tv_sec, timeout_ts.tv_nsec);
1076 rv = clock_gettime (CLOCK_MONOTONIC, &start_time);
1080 first_clock_gettime_failed = 1;
1087 if (timerisset (__timeout))
1089 end_time = timespec_add (start_time, timeout_ts);
1094 * if both fields of the timeout structure are zero,
1095 * then select returns immediately
1097 end_time = start_time;
1102 /* block indefinitely */
1108 if (vcom_init () != 0)
1114 /* validate __nfds */
1115 if (__nfds < 0 || __nfds > FD_SETSIZE)
1123 * usleep(3) emulation
1126 /* call libc_select() with a finite timeout and
1127 * no file descriptors or empty fd sets and
1130 (!__readfds || fd_set_iszero (__readfds)) &&
1131 (!__writefds || fd_set_iszero (__writefds)) &&
1132 (!__exceptfds || fd_set_iszero (__exceptfds)))
1136 rv = libc_select (__nfds,
1137 __readfds, __writefds, __exceptfds, __timeout);
1143 /* TBD: block indefinitely or return -EINVAL */
1149 /* init once before the polling loop */
1151 /* zero vcom and libc fd sets */
1165 _(__readfds, &vcom_readfds, &libc_readfds);
1166 _(__writefds, &vcom_writefds, &libc_writefds);
1167 _(__exceptfds, &vcom_exceptfds, &libc_exceptfds);
1178 /* src, select sets */
1179 __nfds, __readfds, __writefds, __exceptfds,
1180 /* dest1, vcom sets */
1181 __readfds || __writefds || __exceptfds ?
1183 __readfds ? &vcom_readfds : NULL,
1184 __writefds ? &vcom_writefds : NULL,
1185 __exceptfds ? &vcom_exceptfds : NULL,
1186 __readfds || __writefds || __exceptfds ?
1188 /* dest2, libc sets */
1189 __readfds || __writefds || __exceptfds ?
1191 __readfds ? &libc_readfds : NULL,
1192 __writefds ? &libc_writefds : NULL,
1193 __exceptfds ? &libc_exceptfds : NULL,
1194 __readfds || __writefds || __exceptfds ?
1207 * if both fields of timeval structure are zero,
1208 * vcom_select_impl and libc_select returns immediately.
1209 * useful for polling and ensure fairness among
1210 * file descriptors watched.
1217 /* select on vcom fds */
1220 vcom_nfd = vcom_select_impl (vcom_nfds,
1221 __readfds ? &vcom_readfds : NULL,
1222 __writefds ? &vcom_writefds : NULL,
1223 __exceptfds ? &vcom_exceptfds : NULL,
1227 "[%d] select vcom: "
1228 "'%04d'='%04d'\n", pid, vcom_nfd, vcom_nfds);
1236 /* select on libc fds */
1239 libc_nfd = libc_select (libc_nfds,
1240 __readfds ? &libc_readfds : NULL,
1241 __writefds ? &libc_writefds : NULL,
1242 __exceptfds ? &libc_exceptfds : NULL, &tv);
1245 "[%d] select libc: "
1246 "'%04d'='%04d'\n", pid, libc_nfd, libc_nfds);
1250 /* tv becomes undefined */
1257 /* check if any file descriptors changed status */
1258 if ((vcom_nfds && vcom_nfd > 0) || (libc_nfds && libc_nfd > 0))
1260 /* zero the sets before merge and exit */
1280 * on exit, sets are modified in place to indicate which
1281 * file descriptors actually changed status
1284 /* dest, select sets */
1285 __readfds || __writefds || __exceptfds ?
1290 __readfds || __writefds || __exceptfds ?
1292 /* src1, vcom sets */
1294 __readfds ? &vcom_readfds : NULL,
1295 __writefds ? &vcom_writefds : NULL,
1296 __exceptfds ? &vcom_exceptfds : NULL, vcom_nfd,
1297 /* src2, libc sets */
1299 __readfds ? &libc_readfds : NULL,
1300 __writefds ? &libc_writefds : NULL,
1301 __exceptfds ? &libc_exceptfds : NULL, libc_nfd);
1303 * return the number of file descriptors contained in the
1304 * three returned sets
1316 rv = new_nfd == -1 ? 0 : new_nfd;
1320 rv = clock_gettime (CLOCK_MONOTONIC, &now);
1327 while (no_timeout || timespec_compare (&now, &end_time) < 0);
1329 /* timeout expired before anything interesting happened */
1335 fprintf (stderr, "[%d] vselect1: " "'%04d'='%04d'\n", pid, rv, __nfds);
1337 * modify timeout parameter to reflect the amount of time not slept
1341 if (vcom_timerisvalid (__timeout))
1343 /* timeout expired */
1346 timerclear (__timeout);
1348 else if (!first_clock_gettime_failed)
1350 rv2 = clock_gettime (CLOCK_MONOTONIC, &now);
1357 struct timespec ts_delta;
1358 ts_delta = timespec_sub (end_time, now);
1359 VCOM_TIMESPEC_TO_TIMEVAL (__timeout, &ts_delta);
1365 fprintf (stderr, "[%d] vselect2: " "'%04d',='%04d'\n", pid, rv, __nfds);
1371 vcom_select_internal (int __nfds, fd_set * __restrict __readfds,
1372 fd_set * __restrict __writefds,
1373 fd_set * __restrict __exceptfds,
1374 struct timeval *__restrict __timeout)
1379 pid_t pid = getpid ();
1381 fd_set saved_readfds;
1382 fd_set saved_writefds;
1383 fd_set saved_exceptfds;
1385 /* validate __nfds */
1392 /* validate __timeout */
1395 /* validate tv_sec */
1397 if (__timeout->tv_sec < 0 || __timeout->tv_usec < 0)
1403 /* validate tv_usec */
1407 /* init saved_x fds */
1410 saved_readfds = *__readfds;
1412 memcpy (&saved_readfds, __readfds, sizeof (*__readfds));
1417 FD_ZERO (&saved_readfds);
1422 saved_writefds = *__writefds;
1424 memcpy (&saved_writefds, __writefds, sizeof (*__writefds));
1430 FD_ZERO (&saved_writefds);
1435 saved_exceptfds = *__exceptfds;
1437 memcpy (&saved_exceptfds, __exceptfds, sizeof (*__exceptfds));
1443 FD_ZERO (&saved_exceptfds);
1446 /* clear vcom fds */
1447 nfd = vcom_fd_clear (__nfds, &new_nfds, __readfds, __writefds, __exceptfds);
1449 /* set to an invalid value */
1451 /* have kernel fds */
1453 rv = libc_select (new_nfds, __readfds,
1454 __writefds, __exceptfds, __timeout);
1456 if (new_nfds && rv == -1)
1458 /* on error, the file descriptor sets are unmodified */
1460 *__readfds = saved_readfds;
1462 *__writefds = saved_writefds;
1464 *__exceptfds = saved_exceptfds;
1467 else if ((new_nfds && rv != -1) || (rv == -2))
1469 /* restore vcom fds */
1470 nfd = vcom_fd_set (__nfds,
1475 &saved_readfds, &saved_writefds, &saved_exceptfds);
1480 fprintf (stderr, "[%d] select: " "'%04d'='%04d'\n", pid, rv, __nfds);
1485 select (int __nfds, fd_set * __restrict __readfds,
1486 fd_set * __restrict __writefds,
1487 fd_set * __restrict __exceptfds, struct timeval *__restrict __timeout)
1490 pid_t pid = getpid ();
1493 fprintf (stderr, "[%d] select1: " "'%04d'='%04d'\n", pid, rv, __nfds);
1494 rv = vcom_select (__nfds, __readfds, __writefds, __exceptfds, __timeout);
1496 fprintf (stderr, "[%d] select2: " "'%04d'='%04d'\n", pid, rv, __nfds);
1505 #ifdef __USE_XOPEN2K
1507 * Same as above only that the TIMEOUT value is given with higher
1508 * resolution and a sigmask which is been set temporarily. This
1509 * version should be used.
1511 * This function is a cancellation point and therefore not marked
1515 vcom_pselect (int __nfds, fd_set * __restrict __readfds,
1516 fd_set * __restrict __writefds,
1517 fd_set * __restrict __exceptfds,
1518 const struct timespec *__restrict __timeout,
1519 const __sigset_t * __restrict __sigmask)
1524 for (fd = 0; fd < __nfds; fd++)
1526 if (__readfds && FD_ISSET (fd, __readfds))
1528 if (is_vcom_socket_fd (fd))
1534 if (__writefds && FD_ISSET (fd, __writefds))
1536 if (is_vcom_socket_fd (fd))
1541 if (__exceptfds && FD_ISSET (fd, __exceptfds))
1543 if (is_vcom_socket_fd (fd))
1545 FD_CLR (fd, __exceptfds);
1553 pselect (int __nfds, fd_set * __restrict __readfds,
1554 fd_set * __restrict __writefds,
1555 fd_set * __restrict __exceptfds,
1556 const struct timespec *__restrict __timeout,
1557 const __sigset_t * __restrict __sigmask)
1562 pid_t pid = getpid ();
1564 fd_set saved_readfds;
1565 fd_set saved_writefds;
1566 fd_set saved_exceptfds;
1568 /* validate __nfds */
1575 /* validate __timeout */
1578 /* validate tv_sec */
1580 if (__timeout->tv_sec < 0 || __timeout->tv_nsec < 0)
1586 /* validate tv_usec */
1590 /* init saved fds */
1593 saved_readfds = *__readfds;
1595 memcpy (&saved_readfds, __readfds, sizeof (*__readfds));
1600 FD_ZERO (&saved_readfds);
1605 saved_writefds = *__writefds;
1607 memcpy (&saved_writefds, __writefds, sizeof (*__writefds));
1613 FD_ZERO (&saved_writefds);
1618 saved_exceptfds = *__exceptfds;
1620 memcpy (&saved_exceptfds, __exceptfds, sizeof (*__exceptfds));
1626 FD_ZERO (&saved_exceptfds);
1629 /* clear vcom fds */
1630 nfd = vcom_fd_clear (__nfds, &new_nfds, __readfds, __writefds, __exceptfds);
1632 /* set to an invalid value */
1635 rv = libc_pselect (new_nfds,
1637 __writefds, __exceptfds, __timeout, __sigmask);
1639 if (new_nfds && rv == -1)
1641 /* on error, the file descriptor sets are unmodified */
1643 *__readfds = saved_readfds;
1645 *__writefds = saved_writefds;
1647 *__exceptfds = saved_exceptfds;
1650 else if ((new_nfds && rv != -1) || (rv == -2))
1652 /* restore vcom fds */
1653 nfd = vcom_fd_set (__nfds,
1658 &saved_readfds, &saved_writefds, &saved_exceptfds);
1663 fprintf (stderr, "[%d] pselect: " "'%04d'='%04d'\n", pid, rv, __nfds);
1670 * Socket specific glibc api
1674 /* Create a new socket of type TYPE in domain DOMAIN, using
1675 * protocol PROTOCOL. If PROTOCOL is zero, one is chosen
1676 * automatically. Returns a file descriptor for the new socket,
1678 * RETURN: a valid file descriptor for the new socket,
1683 vcom_socket (int __domain, int __type, int __protocol)
1685 if (vcom_init () != 0)
1690 return vcom_socket_socket (__domain, __type, __protocol);
1694 socket (int __domain, int __type, int __protocol)
1697 pid_t pid = getpid ();
1698 pthread_t tid = pthread_self ();
1700 /* handle domains implemented by vpp */
1705 /* handle types implemented by vpp */
1706 switch (__type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1711 vcom_socket_main_show ();
1712 rv = vcom_socket (__domain, __type, __protocol);
1715 "[%d][%lu (0x%lx)] socket: "
1716 "'%04d'= D='%04d', T='%04d', P='%04d'\n",
1717 pid, (unsigned long) tid, (unsigned long) tid,
1718 rv, __domain, __type, __protocol);
1720 vcom_socket_main_show ();
1730 goto CALL_GLIBC_SOCKET_API;
1737 goto CALL_GLIBC_SOCKET_API;
1741 CALL_GLIBC_SOCKET_API:
1742 return libc_socket (__domain, __type, __protocol);
1746 * Create two new sockets, of type TYPE in domain DOMAIN and using
1747 * protocol PROTOCOL, which are connected to each other, and put file
1748 * descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
1749 * one will be chosen automatically.
1750 * Returns 0 on success, -1 for errors.
1753 vcom_socketpair (int __domain, int __type, int __protocol, int __fds[2])
1755 if (vcom_init () != 0)
1760 return vcom_socket_socketpair (__domain, __type, __protocol, __fds);
1764 socketpair (int __domain, int __type, int __protocol, int __fds[2])
1767 pid_t pid = getpid ();
1769 /* handle domains implemented by vpp */
1774 /* handle types implemented by vpp */
1779 rv = vcom_socketpair (__domain, __type, __protocol, __fds);
1783 "'%04d'= D='%04d', T='%04d', P='%04d'\n",
1784 pid, rv, __domain, __type, __protocol);
1794 goto CALL_GLIBC_SOCKET_API;
1801 goto CALL_GLIBC_SOCKET_API;
1805 CALL_GLIBC_SOCKET_API:
1806 return libc_socketpair (__domain, __type, __protocol, __fds);
1810 * Give the socket FD the local address ADDR
1811 * (which is LEN bytes long).
1814 vcom_bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
1818 if (vcom_init () != 0)
1823 /* validate __len */
1824 switch (__addr->sa_family)
1827 if (__len != sizeof (struct sockaddr_in))
1831 if (__len != sizeof (struct sockaddr_in6))
1840 /* handle domains implemented by vpp */
1841 switch (__addr->sa_family)
1845 rv = vcom_socket_bind (__fd, __addr, __len);
1858 bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
1861 pid_t pid = getpid ();
1863 if (is_vcom_socket_fd (__fd))
1866 rv = vcom_bind (__fd, __addr, __len);
1870 "'%04d'='%04d', '%p', '%04d'\n",
1871 pid, rv, __fd, __addr, __len);
1879 return libc_bind (__fd, __addr, __len);
1883 * Put the local address of FD into *ADDR and its length in *LEN.
1886 vcom_getsockname (int __fd, __SOCKADDR_ARG __addr,
1887 socklen_t * __restrict __len)
1889 if (vcom_init () != 0)
1894 return vcom_socket_getsockname (__fd, __addr, __len);
1898 getsockname (int __fd, __SOCKADDR_ARG __addr, socklen_t * __restrict __len)
1901 pid_t pid = getpid ();
1903 if (is_vcom_socket_fd (__fd))
1905 rv = vcom_getsockname (__fd, __addr, __len);
1908 "[%d] getsockname: "
1909 "'%04d'='%04d', '%p', '%p'\n", pid, rv, __fd, __addr, __len);
1917 return libc_getsockname (__fd, __addr, __len);
1921 * Open a connection on socket FD to peer at ADDR
1922 * (which LEN bytes long). For connectionless socket types, just set
1923 * the default address to send to and the only address from which to
1924 * accept transmissions. Return 0 on success, -1 for errors.
1925 * This function is a cancellation point and therefore not marked
1929 vcom_connect (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
1933 if (vcom_init () != 0)
1938 /* validate __len */
1939 switch (__addr->sa_family)
1942 if (__len != INET_ADDRSTRLEN)
1946 if (__len != INET6_ADDRSTRLEN)
1955 /* handle domains implemented by vpp */
1956 switch (__addr->sa_family)
1960 rv = vcom_socket_connect (__fd, __addr, __len);
1972 connect (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
1975 pid_t pid = getpid ();
1976 pthread_t tid = pthread_self ();
1978 if (is_vcom_socket_fd (__fd))
1980 rv = vcom_connect (__fd, __addr, __len);
1983 "[%d][%lu (0x%lx)] connect: "
1984 "'%04d'='%04d', '%p', '%04d'\n",
1985 pid, (unsigned long) tid, (unsigned long) tid,
1986 rv, __fd, __addr, __len);
1995 return libc_connect (__fd, __addr, __len);
1999 * Put the address of the peer connected to socket FD into *ADDR
2000 * (which is *LEN bytes long), and its actual length into *LEN.
2003 vcom_getpeername (int __fd, __SOCKADDR_ARG __addr,
2004 socklen_t * __restrict __len)
2006 if (vcom_init () != 0)
2011 return vcom_socket_getpeername (__fd, __addr, __len);
2015 getpeername (int __fd, __SOCKADDR_ARG __addr, socklen_t * __restrict __len)
2018 pid_t pid = getpid ();
2020 if (is_vcom_socket_fd (__fd))
2022 rv = vcom_getpeername (__fd, __addr, __len);
2025 "[%d] getpeername: "
2026 "'%04d'='%04d', '%p', '%p'\n", pid, rv, __fd, __addr, __len);
2034 return libc_getpeername (__fd, __addr, __len);
2038 * Send N bytes of BUF to socket FD. Returns the number sent or -1.
2039 * This function is a cancellation point and therefore not marked
2043 vcom_send (int __fd, const void *__buf, size_t __n, int __flags)
2046 if (vcom_init () != 0)
2051 return vcom_socket_send (__fd, (void *) __buf, (int) __n, __flags);
2055 send (int __fd, const void *__buf, size_t __n, int __flags)
2058 pid_t pid = getpid ();
2060 if (is_vcom_socket_fd (__fd))
2062 size = vcom_send (__fd, __buf, __n, __flags);
2066 "'%04d'='%04d', '%p', '%04d', '%04x'\n",
2067 pid, (int) size, __fd, __buf, (int) __n, __flags);
2075 return libc_send (__fd, __buf, __n, __flags);
2079 * Read N bytes into BUF from socket FD.
2080 * Returns the number read or -1 for errors.
2081 * This function is a cancellation point and therefore not marked
2085 vcom_recv (int __fd, void *__buf, size_t __n, int __flags)
2087 if (vcom_init () != 0)
2092 return vcom_socket_recv (__fd, __buf, __n, __flags);
2096 recv (int __fd, void *__buf, size_t __n, int __flags)
2099 pid_t pid = getpid ();
2101 if (is_vcom_socket_fd (__fd))
2103 size = vcom_recv (__fd, __buf, __n, __flags);
2107 "'%04d'='%04d', '%p', '%04d', '%04x'\n",
2108 pid, (int) size, __fd, __buf, (int) __n, __flags);
2116 return libc_recv (__fd, __buf, __n, __flags);
2120 * Send N bytes of BUF on socket FD to peer at address ADDR (which is
2121 * ADDR_LEN bytes long). Returns the number sent, or -1 for errors.
2122 * This function is a cancellation point and therefore not marked
2126 vcom_sendto (int __fd, const void *__buf, size_t __n, int __flags,
2127 __CONST_SOCKADDR_ARG __addr, socklen_t __addr_len)
2129 if (vcom_init () != 0)
2134 return vcom_socket_sendto (__fd, __buf, __n, __flags, __addr, __addr_len);
2138 sendto (int __fd, const void *__buf, size_t __n, int __flags,
2139 __CONST_SOCKADDR_ARG __addr, socklen_t __addr_len)
2142 pid_t pid = getpid ();
2144 if (is_vcom_socket_fd (__fd))
2146 size = vcom_sendto (__fd, __buf, __n, __flags, __addr, __addr_len);
2150 "'%04d'='%04d', '%p', '%04d', '%04x', "
2152 pid, (int) size, __fd, __buf, (int) __n, __flags,
2153 __addr, __addr_len);
2161 return libc_sendto (__fd, __buf, __n, __flags, __addr, __addr_len);
2165 * Read N bytes into BUF through socket FD.
2166 * If ADDR is not NULL, fill in *ADDR_LEN bytes of it with the
2167 * address of the sender, and store the actual size of the address
2169 * Returns the number of bytes read or -1 for errors.
2170 * This function is a cancellation point and therefore not marked
2174 vcom_recvfrom (int __fd, void *__restrict __buf, size_t __n,
2176 __SOCKADDR_ARG __addr, socklen_t * __restrict __addr_len)
2178 if (vcom_init () != 0)
2183 return vcom_socket_recvfrom (__fd, __buf, __n, __flags, __addr, __addr_len);
2187 recvfrom (int __fd, void *__restrict __buf, size_t __n,
2189 __SOCKADDR_ARG __addr, socklen_t * __restrict __addr_len)
2192 pid_t pid = getpid ();
2194 if (is_vcom_socket_fd (__fd))
2196 size = vcom_recvfrom (__fd, __buf, __n, __flags, __addr, __addr_len);
2200 "'%04d'='%04d', '%p', '%04d', '%04x', "
2202 pid, (int) size, __fd, __buf, (int) __n, __flags,
2203 __addr, __addr_len);
2211 return libc_recvfrom (__fd, __buf, __n, __flags, __addr, __addr_len);
2215 * Send a message described MESSAGE on socket FD.
2216 * Returns the number of bytes sent, or -1 for errors.
2217 * This function is a cancellation point and therefore not marked
2221 vcom_sendmsg (int __fd, const struct msghdr * __message, int __flags)
2223 if (vcom_init () != 0)
2228 return vcom_socket_sendmsg (__fd, __message, __flags);
2232 sendmsg (int __fd, const struct msghdr * __message, int __flags)
2235 pid_t pid = getpid ();
2237 if (is_vcom_socket_fd (__fd))
2239 size = vcom_sendmsg (__fd, __message, __flags);
2243 "'%04d'='%04d', '%p', '%04x'\n",
2244 pid, (int) size, __fd, __message, __flags);
2252 return libc_sendmsg (__fd, __message, __flags);
2257 * Send a VLEN messages as described by VMESSAGES to socket FD.
2258 * Returns the number of datagrams successfully written
2260 * This function is a cancellation point and therefore not marked
2264 vcom_sendmmsg (int __fd, struct mmsghdr *__vmessages,
2265 unsigned int __vlen, int __flags)
2267 if (vcom_init () != 0)
2272 return vcom_socket_sendmmsg (__fd, __message, __vlen, __flags);
2276 sendmmsg (int __fd, struct mmsghdr *__vmessages,
2277 unsigned int __vlen, int __flags)
2280 pid_t pid = getpid ();
2282 if (is_vcom_socket_fd (__fd))
2284 size = vcom_sendmmsg (__fd, __message, __vlen, __flags);
2288 "'%04d'='%04d', '%p', '%04d', '%04x'\n",
2289 pid, (int) size, __fd, __vmessages, __vlen, __flags);
2297 return libc_sendmmsg (__fd, __message, __vlen, __flags);
2303 * Receive a message as described by MESSAGE from socket FD.
2304 * Returns the number of bytes read or -1 for errors.
2305 * This function is a cancellation point and therefore not marked
2309 vcom_recvmsg (int __fd, struct msghdr * __message, int __flags)
2311 if (vcom_init () != 0)
2316 return vcom_socket_recvmsg (__fd, __message, __flags);
2320 recvmsg (int __fd, struct msghdr * __message, int __flags)
2323 pid_t pid = getpid ();
2325 if (is_vcom_socket_fd (__fd))
2327 size = vcom_recvmsg (__fd, __message, __flags);
2331 "'%04d'='%04d', '%p', '%04x'\n",
2332 pid, (int) size, __fd, __message, __flags);
2340 return libc_recvmsg (__fd, __message, __flags);
2345 * Receive up to VLEN messages as described by VMESSAGES from socket FD.
2346 * Returns the number of messages received or -1 for errors.
2347 * This function is a cancellation point and therefore not marked
2351 vcom_recvmmsg (int __fd, struct mmsghdr *__vmessages,
2352 unsigned int __vlen, int __flags, struct timespec *__tmo)
2354 if (vcom_init () != 0)
2359 return vcom_socket_recvmmsg (__fd, __message, __vlen, __flags, __tmo);
2363 recvmmsg (int __fd, struct mmsghdr *__vmessages,
2364 unsigned int __vlen, int __flags, struct timespec *__tmo)
2367 pid_t pid = getpid ();
2369 if (is_vcom_socket_fd (__fd))
2371 size = vcom_recvmmsg (__fd, __message, __vlen, __flags, __tmo);
2375 "'%04d'='%04d', '%p', "
2376 "'%04d', '%04x', '%p'\n",
2377 pid, (int) size, __fd, __vmessages, __vlen, __flags, __tmo);
2385 return libc_recvmmsg (__fd, __message, __vlen, __flags, __tmo);
2391 * Put the current value for socket FD's option OPTNAME
2392 * at protocol level LEVEL into OPTVAL (which is *OPTLEN bytes long),
2393 * and set *OPTLEN to the value's actual length.
2394 * Returns 0 on success, -1 for errors.
2397 vcom_getsockopt (int __fd, int __level, int __optname,
2398 void *__restrict __optval, socklen_t * __restrict __optlen)
2400 if (vcom_init () != 0)
2405 return vcom_socket_getsockopt (__fd, __level, __optname,
2406 __optval, __optlen);
2410 getsockopt (int __fd, int __level, int __optname,
2411 void *__restrict __optval, socklen_t * __restrict __optlen)
2414 pid_t pid = getpid ();
2416 if (is_vcom_socket_fd (__fd))
2418 rv = vcom_getsockopt (__fd, __level, __optname, __optval, __optlen);
2422 "'%04d'='%04d', '%04d', '%04d', "
2424 pid, rv, __fd, __level, __optname, __optval, __optlen);
2432 return libc_getsockopt (__fd, __level, __optname, __optval, __optlen);
2436 * Set socket FD's option OPTNAME at protocol level LEVEL
2437 * to *OPTVAL (which is OPTLEN bytes long).
2438 * Returns 0 on success, -1 for errors.
2441 vcom_setsockopt (int __fd, int __level, int __optname,
2442 const void *__optval, socklen_t __optlen)
2444 if (vcom_init () != 0)
2449 return vcom_socket_setsockopt (__fd, __level, __optname,
2450 __optval, __optlen);
2454 setsockopt (int __fd, int __level, int __optname,
2455 const void *__optval, socklen_t __optlen)
2458 pid_t pid = getpid ();
2460 if (is_vcom_socket_fd (__fd))
2462 rv = vcom_setsockopt (__fd, __level, __optname, __optval, __optlen);
2466 "'%04d'='%04d', '%04d', '%04d', "
2468 pid, rv, __fd, __level, __optname, __optval, __optlen);
2476 return libc_setsockopt (__fd, __level, __optname, __optval, __optlen);
2480 * Prepare to accept connections on socket FD.
2481 * N connection requests will be queued before further
2482 * requests are refused.
2483 * Returns 0 on success, -1 for errors.
2486 vcom_listen (int __fd, int __n)
2488 if (vcom_init () != 0)
2493 return vcom_socket_listen (__fd, __n);
2497 listen (int __fd, int __n)
2500 pid_t pid = getpid ();
2502 if (is_vcom_socket_fd (__fd))
2504 rv = vcom_listen (__fd, __n);
2508 "'%04d'='%04d', '%04d'\n", pid, rv, __fd, __n);
2516 return libc_listen (__fd, __n);
2520 * Await a connection on socket FD.
2521 * When a connection arrives, open a new socket to communicate
2522 * with it, set *ADDR (which is *ADDR_LEN bytes long) to the address
2523 * of the connecting peer and *ADDR_LEN to the address's actual
2524 * length, and return the new socket's descriptor, or -1 for errors.
2525 * This function is a cancellation point and therefore not marked
2529 vcom_accept (int __fd, __SOCKADDR_ARG __addr,
2530 socklen_t * __restrict __addr_len)
2533 if (vcom_init () != 0)
2537 return vcom_socket_accept (__fd, __addr, __addr_len);
2541 accept (int __fd, __SOCKADDR_ARG __addr, socklen_t * __restrict __addr_len)
2544 pid_t pid = getpid ();
2545 pthread_t tid = pthread_self ();
2547 if (is_vcom_socket_fd (__fd))
2550 vcom_socket_main_show ();
2553 "[%d][%lu (0x%lx)] accept1: "
2554 "'%04d'='%04d', '%p', '%p'\n",
2555 pid, (unsigned long) tid, (unsigned long) tid,
2556 rv, __fd, __addr, __addr_len);
2557 rv = vcom_accept (__fd, __addr, __addr_len);
2560 "[%d][%lu (0x%lx)] accept2: "
2561 "'%04d'='%04d', '%p', '%p'\n",
2562 pid, (unsigned long) tid, (unsigned long) tid,
2563 rv, __fd, __addr, __addr_len);
2565 vcom_socket_main_show ();
2573 return libc_accept (__fd, __addr, __addr_len);
2578 * Similar to 'accept' but takes an additional parameter to specify
2580 * This function is a cancellation point and therefore not marked
2584 vcom_accept4 (int __fd, __SOCKADDR_ARG __addr,
2585 socklen_t * __restrict __addr_len, int __flags)
2588 if (vcom_init () != 0)
2593 return vcom_socket_accept4 (__fd, __addr, __addr_len, __flags);
2597 accept4 (int __fd, __SOCKADDR_ARG __addr,
2598 socklen_t * __restrict __addr_len, int __flags)
2601 pid_t pid = getpid ();
2603 if (is_vcom_socket_fd (__fd))
2606 vcom_socket_main_show ();
2607 rv = vcom_accept4 (__fd, __addr, __addr_len, __flags);
2611 "'%04d'='%04d', '%p', '%p', '%04x'\n",
2612 pid, rv, __fd, __addr, __addr_len, __flags);
2614 vcom_socket_main_show ();
2622 return libc_accept4 (__fd, __addr, __addr_len, __flags);
2628 * Shut down all or part of the connection open on socket FD.
2629 * HOW determines what to shut down:
2630 * SHUT_RD = No more receptions;
2631 * SHUT_WR = No more transmissions;
2632 * SHUT_RDWR = No more receptions or transmissions.
2633 * Returns 0 on success, -1 for errors.
2636 vcom_shutdown (int __fd, int __how)
2638 if (vcom_init () != 0)
2642 return vcom_socket_shutdown (__fd, __how);
2646 shutdown (int __fd, int __how)
2649 pid_t pid = getpid ();
2651 if (is_vcom_socket_fd (__fd))
2653 rv = vcom_shutdown (__fd, __how);
2657 "'%04d'='%04d', '%04d'\n", pid, rv, __fd, __how);
2665 return libc_shutdown (__fd, __how);
2669 vcom_epoll_create (int __size)
2672 if (vcom_init () != 0)
2682 /* __size argument is ignored "thereafter" */
2683 return vcom_epoll_create1 (0);
2687 * __size argument is ignored, but must be greater than zero
2690 epoll_create (int __size)
2693 pid_t pid = getpid ();
2695 rv = vcom_epoll_create (__size);
2698 "[%d] epoll_create: " "'%04d'='%04d'\n", pid, rv, __size);
2708 vcom_epoll_create1 (int __flags)
2710 if (vcom_init () != 0)
2719 if (__flags & ~EPOLL_CLOEXEC)
2723 /* __flags can be either zero or EPOLL_CLOEXEC */
2724 /* implementation */
2725 return vcom_socket_epoll_create1 (__flags);
2729 * __flags can be either zero or EPOLL_CLOEXEC
2732 epoll_create1 (int __flags)
2735 pid_t pid = getpid ();
2737 rv = vcom_epoll_create1 (__flags);
2740 "[%d] epoll_create: " "'%04d'='%08x'\n", pid, rv, __flags);
2750 ep_op_has_event (int op)
2752 return op != EPOLL_CTL_DEL;
2756 vcom_epoll_ctl (int __epfd, int __op, int __fd, struct epoll_event *__event)
2758 if (vcom_init () != 0)
2764 * the requested operation __op is not supported
2765 * by this interface */
2766 if (!((__op == EPOLL_CTL_ADD) ||
2767 (__op == EPOLL_CTL_MOD) || (__op == EPOLL_CTL_DEL)))
2772 /* op is ADD or MOD but event parameter is NULL */
2773 if ((ep_op_has_event (__op) && !__event))
2778 /* fd is same as epfd */
2779 /* do not permit adding an epoll file descriptor inside itself */
2785 /* implementation */
2786 return vcom_socket_epoll_ctl (__epfd, __op, __fd, __event);
2790 * implement the controller interface for epoll
2791 * that enables the insertion/removal/change of
2792 * file descriptors inside the interest set.
2795 epoll_ctl (int __epfd, int __op, int __fd, struct epoll_event *__event)
2798 pid_t pid = getpid ();
2800 if (is_vcom_epfd (__epfd))
2802 /* TBD: currently limiting epoll to support only vcom fds */
2803 if (is_vcom_socket_fd (__fd))
2805 rv = vcom_epoll_ctl (__epfd, __op, __fd, __event);
2809 "'%04d'='%04d', '%04d', '%04d'\n",
2810 pid, rv, __epfd, __op, __fd);
2821 * TBD: currently epoll does not support kernel fds
2829 /* epfd is not an epoll file descriptor */
2837 vcom_epoll_wait (int __epfd, struct epoll_event *__events,
2838 int __maxevents, int __timeout)
2840 if (vcom_init () != 0)
2845 return vcom_epoll_pwait (__epfd, __events, __maxevents, __timeout, NULL);
2849 epoll_wait (int __epfd, struct epoll_event *__events,
2850 int __maxevents, int __timeout)
2853 pid_t pid = getpid ();
2855 if (__maxevents <= 0 || __maxevents > EP_MAX_EVENTS)
2861 if (is_vcom_epfd (__epfd))
2863 rv = vcom_epoll_wait (__epfd, __events, __maxevents, __timeout);
2867 "'%04d'='%04d', '%p', "
2869 pid, rv, __epfd, __events, __maxevents, __timeout);
2887 vcom_epoll_pwait (int __epfd, struct epoll_event *__events,
2888 int __maxevents, int __timeout, const __sigset_t * __ss)
2890 if (vcom_init () != 0)
2895 /* implementation */
2896 return vcom_socket_epoll_pwait (__epfd, __events,
2897 __maxevents, __timeout, __ss);
2901 epoll_pwait (int __epfd, struct epoll_event *__events,
2902 int __maxevents, int __timeout, const __sigset_t * __ss)
2905 pid_t pid = getpid ();
2907 if (__maxevents <= 0 || __maxevents > EP_MAX_EVENTS)
2913 if (is_vcom_epfd (__epfd))
2915 rv = vcom_epoll_pwait (__epfd, __events, __maxevents, __timeout, __ss);
2918 "[%d] epoll_pwait: "
2919 "'%04d'='%04d', '%p', "
2922 pid, rv, __epfd, __events, __maxevents, __timeout, __ss);
2939 /* Poll the file descriptors described by the NFDS structures starting at
2940 FDS. If TIMEOUT is nonzero and not -1, allow TIMEOUT milliseconds for
2941 an event to occur; if TIMEOUT is -1, block until an event occurs.
2942 Returns the number of file descriptors with events, zero if timed out,
2945 This function is a cancellation point and therefore not marked with
2949 vcom_poll (struct pollfd *__fds, nfds_t __nfds, int __timeout)
2952 pid_t pid = getpid ();
2954 struct rlimit nofile_limit;
2955 struct pollfd vcom_fds[MAX_POLL_NFDS_DEFAULT];
2958 /* actual set of file descriptors to be monitored */
2959 nfds_t libc_nfds = 0;
2960 nfds_t vcom_nfds = 0;
2962 /* ready file descriptors
2964 * number of structures which have nonzero revents fields
2965 * in other words, descriptors with events or errors reported.
2967 /* after call to libc_poll () */
2969 /* after call to vcom_socket_poll () */
2973 /* timeout value in units of timespec */
2974 struct timespec timeout_ts;
2975 struct timespec start_time, now, end_time;
2978 /* get start_time */
2979 rv = clock_gettime (CLOCK_MONOTONIC, &start_time);
2986 /* set timeout_ts & end_time */
2989 /* set timeout_ts */
2990 timeout_ts.tv_sec = __timeout / MSEC_PER_SEC;
2991 timeout_ts.tv_nsec = (__timeout % MSEC_PER_SEC) * NSEC_PER_MSEC;
2992 set_normalized_timespec (&timeout_ts,
2993 timeout_ts.tv_sec, timeout_ts.tv_nsec);
2997 end_time = timespec_add (start_time, timeout_ts);
3001 end_time = start_time;
3005 if (vcom_init () != 0)
3011 /* validate __fds */
3018 /* validate __nfds */
3019 /*TBD: call getrlimit once when vcl-ldpreload library is init */
3020 rv = getrlimit (RLIMIT_NOFILE, &nofile_limit);
3026 if (__nfds >= nofile_limit.rlim_cur)
3033 * for the POC, it's fair to assume that nfds is less than 1024
3035 if (__nfds >= MAX_POLL_NFDS_DEFAULT)
3041 /* set revents field (output parameter)
3044 for (fds_idx = 0; fds_idx < __nfds; fds_idx++)
3046 __fds[fds_idx].revents = 0;
3050 /* set revents field (output parameter)
3051 * to zero for user ignored fds
3053 for (fds_idx = 0; fds_idx < __nfds; fds_idx++)
3056 * if negative fd, ignore events field
3057 * and set output parameter (revents field) to zero */
3058 if (__fds[fds_idx].fd < 0)
3060 __fds[fds_idx].revents = 0;
3066 * 00. prepare __fds and vcom_fds for polling
3067 * copy __fds to vcom_fds
3068 * 01. negate all except libc fds in __fds,
3069 * ignore user negated fds
3070 * 02. negate all except vcom_fds in vocm fds,
3071 * ignore user negated fds
3072 * ignore fd 0 by setting it to negative number
3074 memcpy (vcom_fds, __fds, sizeof (*__fds) * __nfds);
3077 for (fds_idx = 0; fds_idx < __nfds; fds_idx++)
3079 /* ignore negative fds */
3080 if (__fds[fds_idx].fd < 0)
3086 * 00. ignore vcom fds in __fds
3087 * 01. ignore libc fds in vcom_fds,
3088 * ignore fd 0 by setting it to negative number.
3089 * as fd 0 cannot be ignored.
3091 if (is_vcom_socket_fd (__fds[fds_idx].fd) ||
3092 is_vcom_epfd (__fds[fds_idx].fd))
3094 __fds[fds_idx].fd = -__fds[fds_idx].fd;
3100 /* ignore fd 0 by setting it to negative number */
3101 if (!vcom_fds[fds_idx].fd)
3103 vcom_fds[fds_idx].fd = -1;
3105 vcom_fds[fds_idx].fd = -vcom_fds[fds_idx].fd;
3112 * poll on libc fds and vcom fds
3114 * specifying a timeout of zero causes libc_poll() and
3115 * vcom_socket_poll() to return immediately, even if no
3116 * file descriptors are ready
3124 * timeout parameter for libc_poll () set to zero
3125 * to poll on libc fds
3128 /* poll on libc fds */
3132 * a timeout of zero causes libc_poll()
3133 * to return immediately
3135 rlibc_nfds = libc_poll (__fds, __nfds, 0);
3139 "'%04d'='%08lu'\n", pid, rlibc_nfds, __nfds);
3144 goto poll_done_update_nfds;
3149 * timeout parameter for vcom_socket_poll () set to zero
3150 * to poll on vcom fds
3153 /* poll on vcom fds */
3157 * a timeout of zero causes vcom_socket_poll()
3158 * to return immediately
3160 rvcom_nfds = vcom_socket_poll (vcom_fds, __nfds, 0);
3164 "'%04d'='%08lu'\n", pid, rvcom_nfds, __nfds);
3168 goto poll_done_update_nfds;
3172 /* check if any file descriptors changed status */
3173 if ((libc_nfds && rlibc_nfds > 0) || (vcom_nfds && rvcom_nfds > 0))
3175 /* something interesting happened */
3176 rv = rlibc_nfds + rvcom_nfds;
3177 goto poll_done_update_nfds;
3180 rv = clock_gettime (CLOCK_MONOTONIC, &now);
3184 goto poll_done_update_nfds;
3188 /* block indefinitely || timeout elapsed */
3189 while ((__timeout < 0) || timespec_compare (&now, &end_time) < 0);
3191 /* timeout expired before anything interesting happened */
3194 poll_done_update_nfds:
3195 for (fds_idx = 0; fds_idx < __nfds; fds_idx++)
3197 /* ignore negative fds in vcom_fds
3198 * 00. user negated fds
3201 if (vcom_fds[fds_idx].fd < 0)
3206 /* from here on handle positive vcom fds */
3208 * restore vcom fds to positive number in __fds
3209 * and update revents in __fds with the events
3210 * that actually occurred in vcom fds
3212 __fds[fds_idx].fd = -__fds[fds_idx].fd;
3215 __fds[fds_idx].revents = vcom_fds[fds_idx].revents;
3221 fprintf (stderr, "[%d] vpoll: " "'%04d'='%08lu'\n", pid, rv, __nfds);
3226 * 00. The field __fds[i].fd contains a file descriptor for an
3228 * If this field is negative, then the corresponding
3229 * events field is ignored and the revents field returns zero.
3230 * The field __fds[i].events is an input parameter.
3231 * The field __fds[i].revents is an output parameter.
3232 * 01. Specifying a negative value in timeout
3233 * means an infinite timeout.
3234 * Specifying a timeout of zero causes poll() to return
3235 * immediately, even if no file descriptors are ready.
3237 * NOTE: observed __nfds is less than 128 from kubecon strace files
3242 poll (struct pollfd *__fds, nfds_t __nfds, int __timeout)
3245 pid_t pid = getpid ();
3249 fprintf (stderr, "[%d] poll1: " "'%04d'='%08lu, %d, 0x%x'\n",
3250 pid, rv, __nfds, __fds[0].fd, __fds[0].events);
3251 rv = vcom_poll (__fds, __nfds, __timeout);
3253 fprintf (stderr, "[%d] poll2: " "'%04d'='%08lu, %d, 0x%x'\n",
3254 pid, rv, __nfds, __fds[0].fd, __fds[0].revents);
3264 /* Like poll, but before waiting the threads signal mask is replaced
3265 with that specified in the fourth parameter. For better usability,
3266 the timeout value is specified using a TIMESPEC object.
3268 This function is a cancellation point and therefore not marked with
3271 vcom_ppoll (struct pollfd *__fds, nfds_t __nfds,
3272 const struct timespec *__timeout, const __sigset_t * __ss)
3274 if (vcom_init () != 0)
3283 ppoll (struct pollfd *__fds, nfds_t __nfds,
3284 const struct timespec *__timeout, const __sigset_t * __ss)
3294 void CONSTRUCTOR_ATTRIBUTE vcom_constructor (void);
3296 void DESTRUCTOR_ATTRIBUTE vcom_destructor (void);
3299 vcom_constructor (void)
3301 pid_t pid = getpid ();
3303 swrap_constructor ();
3304 if (vcom_init () != 0)
3306 printf ("\n[%d] vcom_constructor...failed!\n", pid);
3310 printf ("\n[%d] vcom_constructor...done!\n", pid);
3315 * This function is called when the library is unloaded
3318 vcom_destructor (void)
3320 pid_t pid = getpid ();
3323 swrap_destructor ();
3324 printf ("\n[%d] vcom_destructor...done!\n", pid);
3329 * fd.io coding-style-patch-verification: ON
3332 * eval: (c-set-style "gnu")