core: Add commented debug helper to posix-io.c.

* src/posix-io.c (_gpgme_is_fd_valid): New out-commented function.
--

This code is sometimes useful to track down invalid file descriptors.
We keep it commented in the code.

Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
Werner Koch 2019-06-06 18:55:57 +02:00
parent 6b9ff1ba39
commit f84abcad25
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B

View File

@ -62,6 +62,70 @@
#include "debug.h"
#ifdef USE_LINUX_GETDENTS
/* This is not declared in public headers; getdents64(2) says that we must
* define it ourselves. */
struct linux_dirent64
{
ino64_t d_ino;
off64_t d_off;
unsigned short d_reclen;
unsigned char d_type;
char d_name[];
};
# define DIR_BUF_SIZE 1024
#endif /*USE_LINUX_GETDENTS*/
/* Return true if FD is valid file descriptor. */
#if 0
int
_gpgme_is_fd_valid (int fd)
{
int dir_fd;
char dir_buf[DIR_BUF_SIZE];
struct linux_dirent64 *dir_entry;
int r, pos, x;
const char *s;
int result = 0;
dir_fd = open ("/proc/self/fd", O_RDONLY | O_DIRECTORY);
if (dir_fd != -1)
{
for (;;)
{
r = syscall(SYS_getdents64, dir_fd, dir_buf, DIR_BUF_SIZE);
if (r == -1)
break; /* Ooops */
if (r == 0)
break;
for (pos = 0; pos < r; pos += dir_entry->d_reclen)
{
dir_entry = (struct linux_dirent64 *) (dir_buf + pos);
s = dir_entry->d_name;
if (*s < '0' || *s > '9')
continue;
/* atoi is not guaranteed to be async-signal-safe. */
for (x = 0; *s >= '0' && *s <= '9'; s++)
x = x * 10 + (*s - '0');
if (*s)
continue; /* Does not look like a file descriptor. */
if (x == fd)
{
result = 1;
goto leave;
}
}
}
leave:
close (dir_fd);
}
return result;
}
#endif /*0*/
void
_gpgme_io_subsystem_init (void)
@ -283,22 +347,6 @@ _gpgme_io_set_nonblocking (int fd)
}
#ifdef USE_LINUX_GETDENTS
/* This is not declared in public headers; getdents64(2) says that we must
* define it ourselves. */
struct linux_dirent64
{
ino64_t d_ino;
off64_t d_off;
unsigned short d_reclen;
unsigned char d_type;
char d_name[];
};
# define DIR_BUF_SIZE 1024
#endif /*USE_LINUX_GETDENTS*/
static long int
get_max_fds (void)
{
@ -483,10 +531,12 @@ _gpgme_io_spawn (const char *path, char *const argv[], unsigned int flags,
i++;
}
for (i = 0; fd_list[i].fd != -1; i++)
{
if (fd_list[i].dup_to == -1)
TRACE_LOG ("fd[%i] = 0x%x", i, fd_list[i].fd);
else
TRACE_LOG ("fd[%i] = 0x%x -> 0x%x", i,fd_list[i].fd,fd_list[i].dup_to);
}
pid = fork ();
if (pid == -1)