Fix EINTR handling
This commit is contained in:
@@ -28,7 +28,10 @@ Connection::~Connection() {
|
||||
if (auto server_ptr = server_.lock()) {
|
||||
server_ptr->active_connections_.fetch_sub(1, std::memory_order_relaxed);
|
||||
}
|
||||
int e = close(fd_);
|
||||
int e;
|
||||
do {
|
||||
e = close(fd_);
|
||||
} while (e == -1 && errno == EINTR);
|
||||
if (e == -1) {
|
||||
perror("close");
|
||||
std::abort();
|
||||
|
||||
35
src/main.cpp
35
src/main.cpp
@@ -48,7 +48,10 @@ std::vector<int> create_listen_sockets(const weaseldb::Config &config) {
|
||||
addr.sun_family = AF_UNIX;
|
||||
|
||||
if (config.server.unix_socket_path.length() >= sizeof(addr.sun_path)) {
|
||||
close(sfd);
|
||||
int e;
|
||||
do {
|
||||
e = close(sfd);
|
||||
} while (e == -1 && errno == EINTR);
|
||||
std::fprintf(stderr, "Unix socket path too long\n");
|
||||
std::abort();
|
||||
}
|
||||
@@ -58,13 +61,19 @@ std::vector<int> create_listen_sockets(const weaseldb::Config &config) {
|
||||
|
||||
if (bind(sfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
|
||||
perror("bind");
|
||||
close(sfd);
|
||||
int e;
|
||||
do {
|
||||
e = close(sfd);
|
||||
} while (e == -1 && errno == EINTR);
|
||||
std::abort();
|
||||
}
|
||||
|
||||
if (listen(sfd, SOMAXCONN) == -1) {
|
||||
perror("listen");
|
||||
close(sfd);
|
||||
int e;
|
||||
do {
|
||||
e = close(sfd);
|
||||
} while (e == -1 && errno == EINTR);
|
||||
std::abort();
|
||||
}
|
||||
|
||||
@@ -103,7 +112,10 @@ std::vector<int> create_listen_sockets(const weaseldb::Config &config) {
|
||||
int val = 1;
|
||||
if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) == -1) {
|
||||
perror("setsockopt SO_REUSEADDR");
|
||||
close(sfd);
|
||||
int e;
|
||||
do {
|
||||
e = close(sfd);
|
||||
} while (e == -1 && errno == EINTR);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -111,7 +123,10 @@ std::vector<int> create_listen_sockets(const weaseldb::Config &config) {
|
||||
if (rp->ai_family == AF_INET || rp->ai_family == AF_INET6) {
|
||||
if (setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)) == -1) {
|
||||
perror("setsockopt TCP_NODELAY");
|
||||
close(sfd);
|
||||
int e;
|
||||
do {
|
||||
e = close(sfd);
|
||||
} while (e == -1 && errno == EINTR);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@@ -120,7 +135,10 @@ std::vector<int> create_listen_sockets(const weaseldb::Config &config) {
|
||||
break; /* Success */
|
||||
}
|
||||
|
||||
close(sfd);
|
||||
int e;
|
||||
do {
|
||||
e = close(sfd);
|
||||
} while (e == -1 && errno == EINTR);
|
||||
sfd = -1;
|
||||
}
|
||||
|
||||
@@ -133,7 +151,10 @@ std::vector<int> create_listen_sockets(const weaseldb::Config &config) {
|
||||
|
||||
if (listen(sfd, SOMAXCONN) == -1) {
|
||||
perror("listen");
|
||||
close(sfd);
|
||||
int e;
|
||||
do {
|
||||
e = close(sfd);
|
||||
} while (e == -1 && errno == EINTR);
|
||||
std::abort();
|
||||
}
|
||||
|
||||
|
||||
@@ -59,18 +59,27 @@ Server::Server(const weaseldb::Config &config, ConnectionHandler &handler,
|
||||
|
||||
Server::~Server() {
|
||||
if (shutdown_pipe_[0] != -1) {
|
||||
close(shutdown_pipe_[0]);
|
||||
int e;
|
||||
do {
|
||||
e = close(shutdown_pipe_[0]);
|
||||
} while (e == -1 && errno == EINTR);
|
||||
shutdown_pipe_[0] = -1;
|
||||
}
|
||||
if (shutdown_pipe_[1] != -1) {
|
||||
close(shutdown_pipe_[1]);
|
||||
int e;
|
||||
do {
|
||||
e = close(shutdown_pipe_[1]);
|
||||
} while (e == -1 && errno == EINTR);
|
||||
shutdown_pipe_[1] = -1;
|
||||
}
|
||||
|
||||
// Close all epoll instances
|
||||
for (int epollfd : epoll_fds_) {
|
||||
if (epollfd != -1) {
|
||||
close(epollfd);
|
||||
int e;
|
||||
do {
|
||||
e = close(epollfd);
|
||||
} while (e == -1 && errno == EINTR);
|
||||
}
|
||||
}
|
||||
epoll_fds_.clear();
|
||||
@@ -78,7 +87,10 @@ Server::~Server() {
|
||||
// Close all listen sockets (Server always owns them)
|
||||
for (int fd : listen_fds_) {
|
||||
if (fd != -1) {
|
||||
close(fd);
|
||||
int e;
|
||||
do {
|
||||
e = close(fd);
|
||||
} while (e == -1 && errno == EINTR);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -214,8 +226,13 @@ int Server::createLocalConnection() {
|
||||
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, server_fd, &event) == -1) {
|
||||
perror("epoll_ctl ADD local connection");
|
||||
connection_registry_.remove(server_fd);
|
||||
close(server_fd);
|
||||
close(client_fd);
|
||||
int e;
|
||||
do {
|
||||
e = close(server_fd);
|
||||
} while (e == -1 && errno == EINTR);
|
||||
do {
|
||||
e = close(client_fd);
|
||||
} while (e == -1 && errno == EINTR);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -367,7 +384,10 @@ void Server::start_io_threads(std::vector<std::thread> &threads) {
|
||||
if (config_.server.max_connections > 0 &&
|
||||
active_connections_.load(std::memory_order_relaxed) >=
|
||||
config_.server.max_connections) {
|
||||
close(fd);
|
||||
int e;
|
||||
do {
|
||||
e = close(fd);
|
||||
} while (e == -1 && errno == EINTR);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user