Add a way to let other gems handle closing of FDs

This commit is contained in:
Asmod4n
2024-04-07 13:46:11 +02:00
parent 307812ff20
commit 96b5cd757d
2 changed files with 8 additions and 4 deletions
+3 -1
View File
@@ -41,7 +41,9 @@ struct mrb_io {
writable:1,
eof:1,
sync:1,
is_socket:1;
is_socket:1,
close_fd:1,
close_fd2:1;
};
#define MRB_O_RDONLY 0x0000
+5 -3
View File
@@ -343,6 +343,8 @@ io_alloc(mrb_state *mrb)
fptr->sync = 0;
fptr->eof = 0;
fptr->is_socket = 0;
fptr->close_fd = 1;
fptr->close_fd2 = 1;
return fptr;
}
@@ -761,13 +763,13 @@ fptr_finalize(mrb_state *mrb, struct mrb_io *fptr, int quiet)
if (fptr->fd >= limit) {
#ifdef _WIN32
if (fptr->is_socket) {
if (closesocket(fptr->fd) != 0) {
if (fptr->close_fd && closesocket(fptr->fd) != 0) {
saved_errno = WSAGetLastError();
}
fptr->fd = -1;
}
#endif
if (fptr->fd != -1) {
if (fptr->fd != -1 && fptr->close_fd) {
if (close(fptr->fd) == -1) {
saved_errno = errno;
}
@@ -776,7 +778,7 @@ fptr_finalize(mrb_state *mrb, struct mrb_io *fptr, int quiet)
}
if (fptr->fd2 >= limit) {
if (close(fptr->fd2) == -1) {
if (fptr->close_fd2 && close(fptr->fd2) == -1) {
if (saved_errno == 0) {
saved_errno = errno;
}