I have a file descriptor stored in a variable say var. How can I check whether that descriptor is valid at a later stage?
fdvar1= open(.....);
fdvar2 =
From this forum article:
int is_valid_fd(int fd)
{
return fcntl(fd, F_GETFL) != -1 || errno != EBADF;
}
fcntl(GETFL) is probably the cheapest and least likely to fail operation you can perform on a file descriptor. In particular, the specification suggests that it cannot be interrupted by signals, nor is it affected by any sort of lock held anywhere.