xref: /llvm-project/lldb/source/Host/posix/MainLoopPosix.cpp (revision b9c1b51e45b845debb76d8658edabca70ca56079)
1 //===-- MainLoopPosix.cpp ---------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/Host/posix/MainLoopPosix.h"
11 
12 #include <vector>
13 
14 #include "lldb/Core/Error.h"
15 
16 using namespace lldb;
17 using namespace lldb_private;
18 
19 static sig_atomic_t g_signal_flags[NSIG];
20 
21 static void SignalHandler(int signo, siginfo_t *info, void *) {
22   assert(signo < NSIG);
23   g_signal_flags[signo] = 1;
24 }
25 
26 MainLoopPosix::~MainLoopPosix() {
27   assert(m_read_fds.size() == 0);
28   assert(m_signals.size() == 0);
29 }
30 
31 MainLoopPosix::ReadHandleUP
32 MainLoopPosix::RegisterReadObject(const IOObjectSP &object_sp,
33                                   const Callback &callback, Error &error) {
34   if (!object_sp || !object_sp->IsValid()) {
35     error.SetErrorString("IO object is not valid.");
36     return nullptr;
37   }
38 
39   const bool inserted =
40       m_read_fds.insert({object_sp->GetWaitableHandle(), callback}).second;
41   if (!inserted) {
42     error.SetErrorStringWithFormat("File descriptor %d already monitored.",
43                                    object_sp->GetWaitableHandle());
44     return nullptr;
45   }
46 
47   return CreateReadHandle(object_sp);
48 }
49 
50 // We shall block the signal, then install the signal handler. The signal will
51 // be unblocked in
52 // the Run() function to check for signal delivery.
53 MainLoopPosix::SignalHandleUP
54 MainLoopPosix::RegisterSignal(int signo, const Callback &callback,
55                               Error &error) {
56   if (m_signals.find(signo) != m_signals.end()) {
57     error.SetErrorStringWithFormat("Signal %d already monitored.", signo);
58     return nullptr;
59   }
60 
61   SignalInfo info;
62   info.callback = callback;
63   struct sigaction new_action;
64   new_action.sa_sigaction = &SignalHandler;
65   new_action.sa_flags = SA_SIGINFO;
66   sigemptyset(&new_action.sa_mask);
67   sigaddset(&new_action.sa_mask, signo);
68 
69   sigset_t old_set;
70   if (int ret = pthread_sigmask(SIG_BLOCK, &new_action.sa_mask, &old_set)) {
71     error.SetErrorStringWithFormat("pthread_sigmask failed with error %d\n",
72                                    ret);
73     return nullptr;
74   }
75 
76   info.was_blocked = sigismember(&old_set, signo);
77   if (sigaction(signo, &new_action, &info.old_action) == -1) {
78     error.SetErrorToErrno();
79     if (!info.was_blocked)
80       pthread_sigmask(SIG_UNBLOCK, &new_action.sa_mask, nullptr);
81     return nullptr;
82   }
83 
84   m_signals.insert({signo, info});
85   g_signal_flags[signo] = 0;
86 
87   return SignalHandleUP(new SignalHandle(*this, signo));
88 }
89 
90 void MainLoopPosix::UnregisterReadObject(IOObject::WaitableHandle handle) {
91   bool erased = m_read_fds.erase(handle);
92   UNUSED_IF_ASSERT_DISABLED(erased);
93   assert(erased);
94 }
95 
96 void MainLoopPosix::UnregisterSignal(int signo) {
97   // We undo the actions of RegisterSignal on a best-effort basis.
98   auto it = m_signals.find(signo);
99   assert(it != m_signals.end());
100 
101   sigaction(signo, &it->second.old_action, nullptr);
102 
103   sigset_t set;
104   sigemptyset(&set);
105   sigaddset(&set, signo);
106   pthread_sigmask(it->second.was_blocked ? SIG_BLOCK : SIG_UNBLOCK, &set,
107                   nullptr);
108 
109   m_signals.erase(it);
110 }
111 
112 Error MainLoopPosix::Run() {
113   std::vector<int> signals;
114   sigset_t sigmask;
115   std::vector<int> read_fds;
116   fd_set read_fd_set;
117   m_terminate_request = false;
118 
119   // run until termination or until we run out of things to listen to
120   while (!m_terminate_request && (!m_read_fds.empty() || !m_signals.empty())) {
121     // To avoid problems with callbacks changing the things we're supposed to
122     // listen to, we
123     // will store the *real* list of events separately.
124     signals.clear();
125     read_fds.clear();
126     FD_ZERO(&read_fd_set);
127     int nfds = 0;
128 
129     if (int ret = pthread_sigmask(SIG_SETMASK, nullptr, &sigmask))
130       return Error("pthread_sigmask failed with error %d\n", ret);
131 
132     for (const auto &fd : m_read_fds) {
133       read_fds.push_back(fd.first);
134       FD_SET(fd.first, &read_fd_set);
135       nfds = std::max(nfds, fd.first + 1);
136     }
137 
138     for (const auto &sig : m_signals) {
139       signals.push_back(sig.first);
140       sigdelset(&sigmask, sig.first);
141     }
142 
143     if (pselect(nfds, &read_fd_set, nullptr, nullptr, nullptr, &sigmask) ==
144             -1 &&
145         errno != EINTR)
146       return Error(errno, eErrorTypePOSIX);
147 
148     for (int sig : signals) {
149       if (g_signal_flags[sig] == 0)
150         continue; // No signal
151       g_signal_flags[sig] = 0;
152 
153       auto it = m_signals.find(sig);
154       if (it == m_signals.end())
155         continue; // Signal must have gotten unregistered in the meantime
156 
157       it->second.callback(*this); // Do the work
158 
159       if (m_terminate_request)
160         return Error();
161     }
162 
163     for (int fd : read_fds) {
164       if (!FD_ISSET(fd, &read_fd_set))
165         continue; // Not ready
166 
167       auto it = m_read_fds.find(fd);
168       if (it == m_read_fds.end())
169         continue; // File descriptor must have gotten unregistered in the
170                   // meantime
171 
172       it->second(*this); // Do the work
173 
174       if (m_terminate_request)
175         return Error();
176     }
177   }
178   return Error();
179 }
180