OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "nacl_io/kernel_proxy.h" | 5 #include "nacl_io/kernel_proxy.h" |
6 | 6 |
7 #include <assert.h> | 7 #include <assert.h> |
8 #include <errno.h> | 8 #include <errno.h> |
9 #include <fcntl.h> | 9 #include <fcntl.h> |
10 #include <limits.h> | 10 #include <limits.h> |
(...skipping 1011 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1022 | 1022 |
1023 // Unknown signum | 1023 // Unknown signum |
1024 errno = EINVAL; | 1024 errno = EINVAL; |
1025 return -1; | 1025 return -1; |
1026 } | 1026 } |
1027 | 1027 |
1028 #ifdef PROVIDES_SOCKET_API | 1028 #ifdef PROVIDES_SOCKET_API |
1029 | 1029 |
1030 int KernelProxy::select(int nfds, fd_set* readfds, fd_set* writefds, | 1030 int KernelProxy::select(int nfds, fd_set* readfds, fd_set* writefds, |
1031 fd_set* exceptfds, struct timeval* timeout) { | 1031 fd_set* exceptfds, struct timeval* timeout) { |
1032 fd_set ignore; | |
1033 std::vector<pollfd> pollfds; | 1032 std::vector<pollfd> pollfds; |
1034 | 1033 |
1035 // Simplify logic, by using an IGNORE set for any undefined set | |
1036 FD_ZERO(&ignore); | |
1037 if (NULL == readfds) | |
1038 readfds = &ignore; | |
1039 if (NULL == writefds) | |
1040 writefds = &ignore; | |
1041 if (NULL == exceptfds) | |
1042 exceptfds = &ignore; | |
1043 | |
1044 for (int fd = 0; fd < nfds; fd++) { | 1034 for (int fd = 0; fd < nfds; fd++) { |
1045 int events = 0; | 1035 int events = 0; |
1046 if (FD_ISSET(fd, readfds)) | 1036 if (readfds && FD_ISSET(fd, readfds)) { |
1047 events |= POLLIN; | 1037 events |= POLLIN; |
| 1038 FD_CLR(fd, readfds); |
| 1039 } |
1048 | 1040 |
1049 if (FD_ISSET(fd, writefds)) | 1041 if (writefds && FD_ISSET(fd, writefds)) { |
1050 events |= POLLOUT; | 1042 events |= POLLOUT; |
| 1043 FD_CLR(fd, writefds); |
| 1044 } |
1051 | 1045 |
1052 if (FD_ISSET(fd, exceptfds)) | 1046 if (exceptfds && FD_ISSET(fd, exceptfds)) { |
1053 events |= POLLERR | POLLHUP; | 1047 events |= POLLERR | POLLHUP; |
| 1048 FD_CLR(fd, exceptfds); |
| 1049 } |
1054 | 1050 |
1055 if (events) { | 1051 if (events) { |
1056 pollfd info; | 1052 pollfd info; |
1057 info.fd = fd; | 1053 info.fd = fd; |
1058 info.events = events; | 1054 info.events = events; |
1059 pollfds.push_back(info); | 1055 pollfds.push_back(info); |
1060 } | 1056 } |
1061 } | 1057 } |
1062 | 1058 |
1063 FD_ZERO(readfds); | |
1064 FD_ZERO(writefds); | |
1065 FD_ZERO(exceptfds); | |
1066 | |
1067 // NULL timeout signals wait forever. | 1059 // NULL timeout signals wait forever. |
1068 int ms_timeout = -1; | 1060 int ms_timeout = -1; |
1069 if (timeout != NULL) { | 1061 if (timeout != NULL) { |
1070 int64_t ms = timeout->tv_sec * 1000 + ((timeout->tv_usec + 500) / 1000); | 1062 int64_t ms = timeout->tv_sec * 1000 + ((timeout->tv_usec + 500) / 1000); |
1071 | 1063 |
1072 // If the timeout is invalid or too long (larger than signed 32 bit). | 1064 // If the timeout is invalid or too long (larger than signed 32 bit). |
1073 if ((timeout->tv_sec < 0) || (timeout->tv_sec >= (INT_MAX / 1000)) || | 1065 if ((timeout->tv_sec < 0) || (timeout->tv_sec >= (INT_MAX / 1000)) || |
1074 (timeout->tv_usec < 0) || (timeout->tv_usec >= 1000000) || | 1066 (timeout->tv_usec < 0) || (timeout->tv_usec >= 1000000) || |
1075 (ms < 0) || (ms >= INT_MAX)) { | 1067 (ms < 0) || (ms >= INT_MAX)) { |
1076 errno = EINVAL; | 1068 errno = EINVAL; |
(...skipping 567 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1644 errno = ENOTSOCK; | 1636 errno = ENOTSOCK; |
1645 return -1; | 1637 return -1; |
1646 } | 1638 } |
1647 | 1639 |
1648 return 0; | 1640 return 0; |
1649 } | 1641 } |
1650 | 1642 |
1651 #endif // PROVIDES_SOCKET_API | 1643 #endif // PROVIDES_SOCKET_API |
1652 | 1644 |
1653 } // namespace_nacl_io | 1645 } // namespace_nacl_io |
OLD | NEW |