1*0b57cec5SDimitry Andric //===-- SelectHelper.cpp --------------------------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #if defined(__APPLE__)
10*0b57cec5SDimitry Andric // Enable this special support for Apple builds where we can have unlimited
11*0b57cec5SDimitry Andric // select bounds. We tried switching to poll() and kqueue and we were panicing
12*0b57cec5SDimitry Andric // the kernel, so we have to stick with select for now.
13*0b57cec5SDimitry Andric #define _DARWIN_UNLIMITED_SELECT
14*0b57cec5SDimitry Andric #endif
15*0b57cec5SDimitry Andric 
16*0b57cec5SDimitry Andric #include "lldb/Utility/SelectHelper.h"
17*0b57cec5SDimitry Andric #include "lldb/Utility/LLDBAssert.h"
18*0b57cec5SDimitry Andric #include "lldb/Utility/Status.h"
19*0b57cec5SDimitry Andric #include "lldb/lldb-enumerations.h"
20*0b57cec5SDimitry Andric #include "lldb/lldb-types.h"
21*0b57cec5SDimitry Andric 
22*0b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
23*0b57cec5SDimitry Andric 
24*0b57cec5SDimitry Andric #include <algorithm>
25*0b57cec5SDimitry Andric #include <chrono>
26*0b57cec5SDimitry Andric #include <optional>
27*0b57cec5SDimitry Andric 
28*0b57cec5SDimitry Andric #include <cerrno>
29*0b57cec5SDimitry Andric #if defined(_WIN32)
30*0b57cec5SDimitry Andric // Define NOMINMAX to avoid macros that conflict with std::min and std::max
31*0b57cec5SDimitry Andric #define NOMINMAX
32*0b57cec5SDimitry Andric #include <winsock2.h>
33*0b57cec5SDimitry Andric #else
34*0b57cec5SDimitry Andric #include <sys/time.h>
35*0b57cec5SDimitry Andric #include <sys/select.h>
36*0b57cec5SDimitry Andric #endif
37*0b57cec5SDimitry Andric 
38*0b57cec5SDimitry Andric 
SelectHelper()39*0b57cec5SDimitry Andric SelectHelper::SelectHelper()
40*0b57cec5SDimitry Andric     : m_fd_map(), m_end_time() // Infinite timeout unless
41*0b57cec5SDimitry Andric                                // SelectHelper::SetTimeout() gets called
42*0b57cec5SDimitry Andric {}
43*0b57cec5SDimitry Andric 
SetTimeout(const std::chrono::microseconds & timeout)44*0b57cec5SDimitry Andric void SelectHelper::SetTimeout(const std::chrono::microseconds &timeout) {
45*0b57cec5SDimitry Andric   using namespace std::chrono;
46*0b57cec5SDimitry Andric   m_end_time = steady_clock::time_point(steady_clock::now() + timeout);
47*0b57cec5SDimitry Andric }
48*0b57cec5SDimitry Andric 
FDSetRead(lldb::socket_t fd)49*0b57cec5SDimitry Andric void SelectHelper::FDSetRead(lldb::socket_t fd) {
50*0b57cec5SDimitry Andric   m_fd_map[fd].read_set = true;
51*0b57cec5SDimitry Andric }
52*0b57cec5SDimitry Andric 
FDSetWrite(lldb::socket_t fd)53*0b57cec5SDimitry Andric void SelectHelper::FDSetWrite(lldb::socket_t fd) {
54*0b57cec5SDimitry Andric   m_fd_map[fd].write_set = true;
55*0b57cec5SDimitry Andric }
56*0b57cec5SDimitry Andric 
FDSetError(lldb::socket_t fd)57*0b57cec5SDimitry Andric void SelectHelper::FDSetError(lldb::socket_t fd) {
58*0b57cec5SDimitry Andric   m_fd_map[fd].error_set = true;
59*0b57cec5SDimitry Andric }
60*0b57cec5SDimitry Andric 
FDIsSetRead(lldb::socket_t fd) const61*0b57cec5SDimitry Andric bool SelectHelper::FDIsSetRead(lldb::socket_t fd) const {
62*0b57cec5SDimitry Andric   auto pos = m_fd_map.find(fd);
63*0b57cec5SDimitry Andric   if (pos != m_fd_map.end())
64*0b57cec5SDimitry Andric     return pos->second.read_is_set;
65*0b57cec5SDimitry Andric   else
66*0b57cec5SDimitry Andric     return false;
67*0b57cec5SDimitry Andric }
68*0b57cec5SDimitry Andric 
FDIsSetWrite(lldb::socket_t fd) const69*0b57cec5SDimitry Andric bool SelectHelper::FDIsSetWrite(lldb::socket_t fd) const {
70*0b57cec5SDimitry Andric   auto pos = m_fd_map.find(fd);
71*0b57cec5SDimitry Andric   if (pos != m_fd_map.end())
72*0b57cec5SDimitry Andric     return pos->second.write_is_set;
73*0b57cec5SDimitry Andric   else
74*0b57cec5SDimitry Andric     return false;
75*0b57cec5SDimitry Andric }
76*0b57cec5SDimitry Andric 
FDIsSetError(lldb::socket_t fd) const77*0b57cec5SDimitry Andric bool SelectHelper::FDIsSetError(lldb::socket_t fd) const {
78*0b57cec5SDimitry Andric   auto pos = m_fd_map.find(fd);
79*0b57cec5SDimitry Andric   if (pos != m_fd_map.end())
80*0b57cec5SDimitry Andric     return pos->second.error_is_set;
81*0b57cec5SDimitry Andric   else
82*0b57cec5SDimitry Andric     return false;
83*0b57cec5SDimitry Andric }
84*0b57cec5SDimitry Andric 
updateMaxFd(std::optional<lldb::socket_t> & vold,lldb::socket_t vnew)85*0b57cec5SDimitry Andric static void updateMaxFd(std::optional<lldb::socket_t> &vold,
86*0b57cec5SDimitry Andric                         lldb::socket_t vnew) {
87*0b57cec5SDimitry Andric   if (!vold)
88*0b57cec5SDimitry Andric     vold = vnew;
89*0b57cec5SDimitry Andric   else
90*0b57cec5SDimitry Andric     vold = std::max(*vold, vnew);
91*0b57cec5SDimitry Andric }
92*0b57cec5SDimitry Andric 
Select()93*0b57cec5SDimitry Andric lldb_private::Status SelectHelper::Select() {
94*0b57cec5SDimitry Andric   lldb_private::Status error;
95*0b57cec5SDimitry Andric #ifdef _WIN32
96*0b57cec5SDimitry Andric   // On windows FD_SETSIZE limits the number of file descriptors, not their
97*0b57cec5SDimitry Andric   // numeric value.
98*0b57cec5SDimitry Andric   lldbassert(m_fd_map.size() <= FD_SETSIZE);
99*0b57cec5SDimitry Andric   if (m_fd_map.size() > FD_SETSIZE)
100*0b57cec5SDimitry Andric     return lldb_private::Status("Too many file descriptors for select()");
101*0b57cec5SDimitry Andric #endif
102*0b57cec5SDimitry Andric 
103*0b57cec5SDimitry Andric   std::optional<lldb::socket_t> max_read_fd;
104*0b57cec5SDimitry Andric   std::optional<lldb::socket_t> max_write_fd;
105*0b57cec5SDimitry Andric   std::optional<lldb::socket_t> max_error_fd;
106*0b57cec5SDimitry Andric   std::optional<lldb::socket_t> max_fd;
107*0b57cec5SDimitry Andric   for (auto &pair : m_fd_map) {
108*0b57cec5SDimitry Andric     pair.second.PrepareForSelect();
109*0b57cec5SDimitry Andric     const lldb::socket_t fd = pair.first;
110*0b57cec5SDimitry Andric #if !defined(__APPLE__) && !defined(_WIN32)
111*0b57cec5SDimitry Andric     lldbassert(fd < static_cast<int>(FD_SETSIZE));
112*0b57cec5SDimitry Andric     if (fd >= static_cast<int>(FD_SETSIZE)) {
113*0b57cec5SDimitry Andric       error.SetErrorStringWithFormat("%i is too large for select()", fd);
114*0b57cec5SDimitry Andric       return error;
115*0b57cec5SDimitry Andric     }
116*0b57cec5SDimitry Andric #endif
117*0b57cec5SDimitry Andric     if (pair.second.read_set)
118*0b57cec5SDimitry Andric       updateMaxFd(max_read_fd, fd);
119*0b57cec5SDimitry Andric     if (pair.second.write_set)
120*0b57cec5SDimitry Andric       updateMaxFd(max_write_fd, fd);
121*0b57cec5SDimitry Andric     if (pair.second.error_set)
122*0b57cec5SDimitry Andric       updateMaxFd(max_error_fd, fd);
123*0b57cec5SDimitry Andric     updateMaxFd(max_fd, fd);
124*0b57cec5SDimitry Andric   }
125*0b57cec5SDimitry Andric 
126*0b57cec5SDimitry Andric   if (!max_fd) {
127*0b57cec5SDimitry Andric     error.SetErrorString("no valid file descriptors");
128*0b57cec5SDimitry Andric     return error;
129*0b57cec5SDimitry Andric   }
130*0b57cec5SDimitry Andric 
131*0b57cec5SDimitry Andric   const unsigned nfds = static_cast<unsigned>(*max_fd) + 1;
132*0b57cec5SDimitry Andric   fd_set *read_fdset_ptr = nullptr;
133*0b57cec5SDimitry Andric   fd_set *write_fdset_ptr = nullptr;
134*0b57cec5SDimitry Andric   fd_set *error_fdset_ptr = nullptr;
135*0b57cec5SDimitry Andric // Initialize and zero out the fdsets
136*0b57cec5SDimitry Andric #if defined(__APPLE__)
137*0b57cec5SDimitry Andric   llvm::SmallVector<fd_set, 1> read_fdset;
138*0b57cec5SDimitry Andric   llvm::SmallVector<fd_set, 1> write_fdset;
139*0b57cec5SDimitry Andric   llvm::SmallVector<fd_set, 1> error_fdset;
140*0b57cec5SDimitry Andric 
141*0b57cec5SDimitry Andric   if (max_read_fd.has_value()) {
142*0b57cec5SDimitry Andric     read_fdset.resize((nfds / FD_SETSIZE) + 1);
143*0b57cec5SDimitry Andric     read_fdset_ptr = read_fdset.data();
144*0b57cec5SDimitry Andric   }
145*0b57cec5SDimitry Andric   if (max_write_fd.has_value()) {
146*0b57cec5SDimitry Andric     write_fdset.resize((nfds / FD_SETSIZE) + 1);
147*0b57cec5SDimitry Andric     write_fdset_ptr = write_fdset.data();
148*0b57cec5SDimitry Andric   }
149*0b57cec5SDimitry Andric   if (max_error_fd.has_value()) {
150*0b57cec5SDimitry Andric     error_fdset.resize((nfds / FD_SETSIZE) + 1);
151*0b57cec5SDimitry Andric     error_fdset_ptr = error_fdset.data();
152*0b57cec5SDimitry Andric   }
153*0b57cec5SDimitry Andric   for (auto &fd_set : read_fdset)
154*0b57cec5SDimitry Andric     FD_ZERO(&fd_set);
155*0b57cec5SDimitry Andric   for (auto &fd_set : write_fdset)
156*0b57cec5SDimitry Andric     FD_ZERO(&fd_set);
157*0b57cec5SDimitry Andric   for (auto &fd_set : error_fdset)
158*0b57cec5SDimitry Andric     FD_ZERO(&fd_set);
159*0b57cec5SDimitry Andric #else
160*0b57cec5SDimitry Andric   fd_set read_fdset;
161*0b57cec5SDimitry Andric   fd_set write_fdset;
162*0b57cec5SDimitry Andric   fd_set error_fdset;
163*0b57cec5SDimitry Andric 
164*0b57cec5SDimitry Andric   if (max_read_fd) {
165*0b57cec5SDimitry Andric     FD_ZERO(&read_fdset);
166*0b57cec5SDimitry Andric     read_fdset_ptr = &read_fdset;
167*0b57cec5SDimitry Andric   }
168*0b57cec5SDimitry Andric   if (max_write_fd) {
169*0b57cec5SDimitry Andric     FD_ZERO(&write_fdset);
170*0b57cec5SDimitry Andric     write_fdset_ptr = &write_fdset;
171*0b57cec5SDimitry Andric   }
172*0b57cec5SDimitry Andric   if (max_error_fd) {
173*0b57cec5SDimitry Andric     FD_ZERO(&error_fdset);
174*0b57cec5SDimitry Andric     error_fdset_ptr = &error_fdset;
175*0b57cec5SDimitry Andric   }
176*0b57cec5SDimitry Andric #endif
177*0b57cec5SDimitry Andric   // Set the FD bits in the fdsets for read/write/error
178*0b57cec5SDimitry Andric   for (auto &pair : m_fd_map) {
179*0b57cec5SDimitry Andric     const lldb::socket_t fd = pair.first;
180*0b57cec5SDimitry Andric 
181*0b57cec5SDimitry Andric     if (pair.second.read_set)
182*0b57cec5SDimitry Andric       FD_SET(fd, read_fdset_ptr);
183*0b57cec5SDimitry Andric 
184*0b57cec5SDimitry Andric     if (pair.second.write_set)
185*0b57cec5SDimitry Andric       FD_SET(fd, write_fdset_ptr);
186*0b57cec5SDimitry Andric 
187*0b57cec5SDimitry Andric     if (pair.second.error_set)
188*0b57cec5SDimitry Andric       FD_SET(fd, error_fdset_ptr);
189*0b57cec5SDimitry Andric   }
190*0b57cec5SDimitry Andric 
191*0b57cec5SDimitry Andric   // Setup our timeout time value if needed
192*0b57cec5SDimitry Andric   struct timeval *tv_ptr = nullptr;
193*0b57cec5SDimitry Andric   struct timeval tv = {0, 0};
194*0b57cec5SDimitry Andric 
195*0b57cec5SDimitry Andric   while (true) {
196*0b57cec5SDimitry Andric     using namespace std::chrono;
197*0b57cec5SDimitry Andric     // Setup out relative timeout based on the end time if we have one
198*0b57cec5SDimitry Andric     if (m_end_time) {
199*0b57cec5SDimitry Andric       tv_ptr = &tv;
200*0b57cec5SDimitry Andric       const auto remaining_dur =
201*0b57cec5SDimitry Andric           duration_cast<microseconds>(*m_end_time - steady_clock::now());
202*0b57cec5SDimitry Andric       if (remaining_dur.count() > 0) {
203*0b57cec5SDimitry Andric         // Wait for a specific amount of time
204*0b57cec5SDimitry Andric         const auto dur_secs = duration_cast<seconds>(remaining_dur);
205*0b57cec5SDimitry Andric         const auto dur_usecs = remaining_dur % seconds(1);
206*0b57cec5SDimitry Andric         tv.tv_sec = dur_secs.count();
207*0b57cec5SDimitry Andric         tv.tv_usec = dur_usecs.count();
208*0b57cec5SDimitry Andric       } else {
209*0b57cec5SDimitry Andric         // Just poll once with no timeout
210*0b57cec5SDimitry Andric         tv.tv_sec = 0;
211*0b57cec5SDimitry Andric         tv.tv_usec = 0;
212*0b57cec5SDimitry Andric       }
213*0b57cec5SDimitry Andric     }
214*0b57cec5SDimitry Andric     const int num_set_fds = ::select(nfds, read_fdset_ptr, write_fdset_ptr,
215*0b57cec5SDimitry Andric                                      error_fdset_ptr, tv_ptr);
216*0b57cec5SDimitry Andric     if (num_set_fds < 0) {
217*0b57cec5SDimitry Andric       // We got an error
218*0b57cec5SDimitry Andric       error.SetErrorToErrno();
219*0b57cec5SDimitry Andric       if (error.GetError() == EINTR) {
220*0b57cec5SDimitry Andric         error.Clear();
221*0b57cec5SDimitry Andric         continue; // Keep calling select if we get EINTR
222*0b57cec5SDimitry Andric       } else
223*0b57cec5SDimitry Andric         return error;
224*0b57cec5SDimitry Andric     } else if (num_set_fds == 0) {
225*0b57cec5SDimitry Andric       // Timeout
226*0b57cec5SDimitry Andric       error.SetError(ETIMEDOUT, lldb::eErrorTypePOSIX);
227*0b57cec5SDimitry Andric       error.SetErrorString("timed out");
228*0b57cec5SDimitry Andric       return error;
229*0b57cec5SDimitry Andric     } else {
230*0b57cec5SDimitry Andric       // One or more descriptors were set, update the FDInfo::select_is_set
231*0b57cec5SDimitry Andric       // mask so users can ask the SelectHelper class so clients can call one
232*0b57cec5SDimitry Andric       // of:
233*0b57cec5SDimitry Andric 
234*0b57cec5SDimitry Andric       for (auto &pair : m_fd_map) {
235*0b57cec5SDimitry Andric         const int fd = pair.first;
236*0b57cec5SDimitry Andric 
237*0b57cec5SDimitry Andric         if (pair.second.read_set) {
238*0b57cec5SDimitry Andric           if (FD_ISSET(fd, read_fdset_ptr))
239*0b57cec5SDimitry Andric             pair.second.read_is_set = true;
240*0b57cec5SDimitry Andric         }
241*0b57cec5SDimitry Andric         if (pair.second.write_set) {
242*0b57cec5SDimitry Andric           if (FD_ISSET(fd, write_fdset_ptr))
243*0b57cec5SDimitry Andric             pair.second.write_is_set = true;
244*0b57cec5SDimitry Andric         }
245*0b57cec5SDimitry Andric         if (pair.second.error_set) {
246*0b57cec5SDimitry Andric           if (FD_ISSET(fd, error_fdset_ptr))
247*0b57cec5SDimitry Andric             pair.second.error_is_set = true;
248*0b57cec5SDimitry Andric         }
249*0b57cec5SDimitry Andric       }
250*0b57cec5SDimitry Andric       break;
251*0b57cec5SDimitry Andric     }
252*0b57cec5SDimitry Andric   }
253*0b57cec5SDimitry Andric   return error;
254*0b57cec5SDimitry Andric }
255