|
29 | 29 | // POSSIBILITY OF SUCH DAMAGE. |
30 | 30 |
|
31 | 31 | #include <signal.h> |
32 | | -#include <sys/epoll.h> |
| 32 | +#include <poll.h> |
33 | 33 |
|
34 | 34 | #include "unix_socket.h" |
35 | 35 |
|
|
44 | 44 | #define SIG_THREAD_EXIT SIGUSR2 |
45 | 45 |
|
46 | 46 |
|
47 | | -static void EpollAdd(int epoll_fd, int new_fd, uint32_t events) { |
48 | | - struct epoll_event event; |
49 | | - event.events = events; |
50 | | - event.data.fd = new_fd; |
51 | | - epoll_ctl(epoll_fd, EPOLL_CTL_ADD, new_fd, &event); |
52 | | -} |
53 | | - |
54 | | -static void EpollDel(int epoll_fd, int fd) { |
55 | | - epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, nullptr); |
56 | | -} |
57 | | - |
58 | 47 | void UnixSocketPort::AcceptThread() { |
59 | 48 | sigset_t sigset; |
60 | 49 | sigfillset(&sigset); |
61 | 50 | sigdelset(&sigset, SIG_THREAD_EXIT); |
62 | 51 |
|
| 52 | + struct pollfd fds[2]; |
| 53 | + memset(fds, 0, sizeof(fds)); |
| 54 | + fds[0].fd = listen_fd_; |
| 55 | + fds[0].events = POLLIN; |
| 56 | + fds[1].events = POLLRDHUP; |
| 57 | + |
63 | 58 | while (true) { |
64 | | - struct epoll_event event; |
65 | | - if (epoll_pwait(epoll_fd_, &event, 1, -1, &sigset) < 0) { |
| 59 | + fds[1].fd = client_fd_; |
| 60 | + int res = ppoll(fds, 2, nullptr, &sigset); |
| 61 | + |
| 62 | + if (accept_thread_stop_req_) { |
| 63 | + return; |
| 64 | + |
| 65 | + } else if (res < 0) { |
66 | 66 | if (errno == EINTR) { |
67 | | - if (accept_thread_stop_req_) { |
68 | | - break; |
69 | | - } else { |
70 | | - continue; |
71 | | - } |
| 67 | + continue; |
72 | 68 | } else { |
73 | 69 | PLOG(ERROR) << "[UnixSocketPort]:epoll_wait()"; |
74 | 70 | } |
75 | 71 |
|
76 | | - } else if (event.data.fd == listen_fd_) { |
77 | | - if (event.events & EPOLLIN) { |
78 | | - // new client connected |
79 | | - int fd; |
80 | | - while (true) { |
81 | | - fd = accept4(listen_fd_, nullptr, nullptr, SOCK_NONBLOCK); |
82 | | - if (fd >= 0 || errno != EINTR) { |
83 | | - break; |
84 | | - } |
85 | | - } |
86 | | - if (fd < 0) { |
87 | | - PLOG(ERROR) << "[UnixSocketPort]:accept4()"; |
88 | | - } else { |
89 | | - EpollAdd(epoll_fd_, fd, EPOLLRDHUP); |
90 | | - client_fd_ = fd; |
| 72 | + } else if (fds[0].revents & POLLIN) { |
| 73 | + // new client connected |
| 74 | + int fd; |
| 75 | + while (true) { |
| 76 | + fd = accept4(listen_fd_, nullptr, nullptr, SOCK_NONBLOCK); |
| 77 | + if (fd >= 0 || errno != EINTR) { |
| 78 | + break; |
91 | 79 | } |
92 | | - } else { |
93 | | - LOG(ERROR) << "[UnixSocketPort]:epoll_wait(): " |
94 | | - << "Unexpected event " << event.events |
95 | | - << " in listen_fd_"; |
96 | 80 | } |
97 | | - |
98 | | - } else if (event.data.fd == client_fd_) { |
99 | | - if (event.events & EPOLLRDHUP) { |
100 | | - // connection dropped by client |
101 | | - int fd = client_fd_; |
102 | | - client_fd_ = -1; |
103 | | - EpollDel(epoll_fd_, fd); |
104 | | - close(fd); |
| 81 | + if (fd < 0) { |
| 82 | + PLOG(ERROR) << "[UnixSocketPort]:accept4()"; |
105 | 83 | } else { |
106 | | - LOG(ERROR) << "[UnixSocketPort]:epoll_wait(): " |
107 | | - << "Unexpected event " << event.events |
108 | | - << " in client_fd_"; |
| 84 | + client_fd_ = fd; |
109 | 85 | } |
110 | 86 |
|
111 | | - } else { |
112 | | - LOG(ERROR) << "[UnixSocketPort]:epoll_wait(): " |
113 | | - << "Unexpected fd " << event.data.fd; |
| 87 | + } else if (fds[1].revents & (POLLRDHUP | POLLHUP)) { |
| 88 | + // connection dropped by client |
| 89 | + int fd = client_fd_; |
| 90 | + client_fd_ = -1; |
| 91 | + close(fd); |
114 | 92 | } |
115 | 93 | } |
116 | 94 | } |
@@ -171,14 +149,6 @@ CommandResponse UnixSocketPort::Init(const bess::pb::UnixSocketPortArg &arg) { |
171 | 149 | } |
172 | 150 |
|
173 | 151 |
|
174 | | - epoll_fd_ = epoll_create(1); |
175 | | - if (epoll_fd_ < 0) { |
176 | | - DeInit(); |
177 | | - return CommandFailure(errno, "epoll_create(1) failed"); |
178 | | - } |
179 | | - EpollAdd(epoll_fd_, listen_fd_, EPOLLIN); |
180 | | - |
181 | | - |
182 | 152 | struct sigaction sa; |
183 | 153 | memset(&sa, 0, sizeof(sa)); |
184 | 154 | sa.sa_handler = AcceptThreadHandler; |
@@ -208,9 +178,6 @@ void UnixSocketPort::DeInit() { |
208 | 178 | if (client_fd_ != kNotConnectedFd) { |
209 | 179 | close(client_fd_); |
210 | 180 | } |
211 | | - if (epoll_fd_ != kNotConnectedFd) { |
212 | | - close(epoll_fd_); |
213 | | - } |
214 | 181 | } |
215 | 182 |
|
216 | 183 | int UnixSocketPort::RecvPackets(queue_t qid, bess::Packet **pkts, int cnt) { |
|
0 commit comments