cli: Fix missing closing file descriptors

This commit is contained in:
Samuel Thibault 2024-02-04 04:36:09 +01:00
parent 32799f196a
commit fdf19f5467
4 changed files with 42 additions and 5 deletions

31
cli.c
View file

@ -18,6 +18,7 @@
#include <sys/socket.h>
#include <sys/types.h>
#include <signal.h>
#include <dirent.h>
#include <dlfcn.h>
#include <netdb.h>
#include <libcli.h>
@ -316,6 +317,36 @@ void cli_do(int sockfd)
socklen_t l = sizeof(addr);
if (fork_and_close()) return;
/* Check that fork_and_close has closed everything but std* and the socket */
int fdfd = open("/dev/fd", O_RDONLY|O_DIRECTORY);
if (fdfd >= 0)
{
DIR *fds = fdopendir(fdfd);
if (fds)
{
struct dirent *ent;
while ((ent = readdir(fds)))
{
if (!strcmp(ent->d_name, ".")
|| !strcmp(ent->d_name, ".."))
continue;
int fd = atoi(ent->d_name);
if (fd <= STDERR_FILENO)
continue;
if (fd == fdfd || fd == sockfd)
continue;
if (log_stream && fd == fileno(log_stream))
continue;
LOG(0, 0, 0, "Warning: fd %d is still open within cli. This may interfere with operations.\n", fd);
}
closedir(fds);
}
close(fdfd);
}
if (getpeername(sockfd, (struct sockaddr *) &addr, &l) == 0)
{
require_auth = addr.sin_addr.s_addr != inet_addr("127.0.0.1");