"trying to add %d",
socket_fds_max,
fd);
+ errno = EMFILE;
return -1;
}
file_hdr.link_type = 0x0065; /* 101 RAW IP */
if (write(fd, &file_hdr, sizeof(file_hdr)) != sizeof(file_hdr)) {
- close(fd);
+ libc_close(fd);
fd = -1;
}
return fd;
ret = swrap_create_socket(si, fd);
if (ret == -1) {
+ int saved_errno = errno;
+ libc_close(fd);
+ errno = saved_errno;
return -1;
}
&in_addr.sa_socklen);
if (ret == -1) {
SWRAP_UNLOCK_SI(parent_si);
- close(fd);
+ libc_close(fd);
return ret;
}
&un_my_addr.sa.s,
&un_my_addr.sa_socklen);
if (ret == -1) {
- close(fd);
+ libc_close(fd);
return ret;
}
&in_my_addr.sa.s,
&in_my_addr.sa_socklen);
if (ret == -1) {
- close(fd);
+ libc_close(fd);
return ret;
}
idx = swrap_create_socket(&new_si, fd);
if (idx == -1) {
- close (fd);
+ int saved_errno = errno;
+ libc_close(fd);
+ errno = saved_errno;
return -1;
}
return -1;
}
+ if ((size_t)dup_fd >= socket_fds_max) {
+ SWRAP_LOG(SWRAP_LOG_ERROR,
+ "The max socket index limit of %zu has been reached, "
+ "trying to add %d",
+ socket_fds_max,
+ dup_fd);
+ libc_close(dup_fd);
+ errno = EMFILE;
+ return -1;
+ }
+
SWRAP_LOCK_SI(si);
swrap_inc_refcount(si);
return newfd;
}
+ if ((size_t)newfd >= socket_fds_max) {
+ SWRAP_LOG(SWRAP_LOG_ERROR,
+ "The max socket index limit of %zu has been reached, "
+ "trying to add %d",
+ socket_fds_max,
+ newfd);
+ errno = EMFILE;
+ return -1;
+ }
+
if (find_socket_info(newfd)) {
/* dup2() does an implicit close of newfd, which we
* need to emulate */
return -1;
}
+ /* Make sure we don't have an entry for the fd */
+ swrap_remove_stale(dup_fd);
+
+ if ((size_t)dup_fd >= socket_fds_max) {
+ SWRAP_LOG(SWRAP_LOG_ERROR,
+ "The max socket index limit of %zu has been reached, "
+ "trying to add %d",
+ socket_fds_max,
+ dup_fd);
+ libc_close(dup_fd);
+ errno = EMFILE;
+ return -1;
+ }
+
SWRAP_LOCK_SI(si);
swrap_inc_refcount(si);
SWRAP_UNLOCK_SI(si);
- /* Make sure we don't have an entry for the fd */
- swrap_remove_stale(dup_fd);
set_socket_info_index(dup_fd, idx);