mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-26 20:55:35 +03:00
Userspace: Deal with select() returning EINTR on a signal interruption
Add a trivial CSafeSyscall template that calls a callback until it stops returning EINTR, and use it everywhere we use select() now. Thanks to Andreas for the suggestion of using a template parameter for the syscall function to invoke.
This commit is contained in:
parent
a1eff3daba
commit
f2c0e55070
Notes:
sideshowbarker
2024-07-19 13:06:09 +09:00
Author: https://github.com/rburchell Commit: https://github.com/SerenityOS/serenity/commit/f2c0e550704 Pull-request: https://github.com/SerenityOS/serenity/pull/356
@ -4,6 +4,7 @@
|
||||
#include <LibCore/CLock.h>
|
||||
#include <LibCore/CNotifier.h>
|
||||
#include <LibCore/CObject.h>
|
||||
#include <LibCore/CSyscallUtils.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
@ -203,11 +204,7 @@ void CEventLoop::wait_for_event(WaitMode mode)
|
||||
should_wait_forever = false;
|
||||
}
|
||||
|
||||
int marked_fd_count = select(max_fd + 1, &rfds, &wfds, nullptr, should_wait_forever ? nullptr : &timeout);
|
||||
if (marked_fd_count < 0) {
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
int marked_fd_count = CSyscallUtils::safe_syscall(select, max_fd + 1, &rfds, &wfds, nullptr, should_wait_forever ? nullptr : &timeout);
|
||||
if (FD_ISSET(s_wake_pipe_fds[0], &rfds)) {
|
||||
char buffer[32];
|
||||
auto nread = read(s_wake_pipe_fds[0], buffer, sizeof(buffer));
|
||||
|
@ -1,5 +1,6 @@
|
||||
#include <AK/PrintfImplementation.h>
|
||||
#include <LibCore/CIODevice.h>
|
||||
#include <LibCore/CSyscallUtils.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/select.h>
|
||||
@ -71,7 +72,7 @@ bool CIODevice::can_read_from_fd() const
|
||||
struct timeval timeout {
|
||||
0, 0
|
||||
};
|
||||
int rc = select(m_fd + 1, &rfds, nullptr, nullptr, &timeout);
|
||||
int rc = CSyscallUtils::safe_syscall(select, m_fd + 1, &rfds, nullptr, nullptr, &timeout);
|
||||
if (rc < 0) {
|
||||
// NOTE: We don't set m_error here.
|
||||
perror("CIODevice::can_read: select");
|
||||
|
25
Libraries/LibCore/CSyscallUtils.h
Normal file
25
Libraries/LibCore/CSyscallUtils.h
Normal file
@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <AK/StdLibExtras.h>
|
||||
|
||||
namespace CSyscallUtils {
|
||||
|
||||
template <typename Syscall, class... Args>
|
||||
inline int safe_syscall(Syscall syscall, Args&& ... args) {
|
||||
for (;;) {
|
||||
int sysret = syscall(forward<Args>(args)...);
|
||||
if (sysret == -1) {
|
||||
dbgprintf("CSafeSyscall: %d (%d: %s)\n", sysret, errno, strerror(errno));
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
return sysret;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
#include <LibCore/CEventLoop.h>
|
||||
#include <LibCore/CEvent.h>
|
||||
#include <LibCore/CLocalSocket.h>
|
||||
#include <LibCore/CSyscallUtils.h>
|
||||
#include <LibCore/CNotifier.h>
|
||||
#include <LibAudio/ASAPI.h>
|
||||
|
||||
@ -106,7 +107,7 @@ public:
|
||||
fd_set rfds;
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(m_connection.fd(), &rfds);
|
||||
int rc = select(m_connection.fd() + 1, &rfds, nullptr, nullptr, nullptr);
|
||||
int rc = CSyscallUtils::safe_syscall(select, m_connection.fd() + 1, &rfds, nullptr, nullptr, nullptr);
|
||||
if (rc < 0) {
|
||||
perror("select");
|
||||
}
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include <Kernel/Net/IPv4.h>
|
||||
#include <LibCore/CConfigFile.h>
|
||||
#include <LibCore/CFile.h>
|
||||
#include <LibCore/CSyscallUtils.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <stdio.h>
|
||||
@ -108,7 +109,7 @@ int main(int argc, char** argv)
|
||||
fd_set rfds;
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(server_fd, &rfds);
|
||||
rc = select(server_fd + 1, &rfds, nullptr, nullptr, nullptr);
|
||||
rc = CSyscallUtils::safe_syscall(select, server_fd + 1, &rfds, nullptr, nullptr, nullptr);
|
||||
if (rc < 1) {
|
||||
perror("select");
|
||||
return 1;
|
||||
|
Loading…
Reference in New Issue
Block a user