0
   The socket() function shall fail if:

   EAFNOSUPPORT
          The implementation does not support the specified address family.

   EMFILE All file descriptors available to the process are currently open.

   ENFILE No more file descriptors are available for the system.

   EPROTONOSUPPORT
          The protocol is not supported by the address family, or the protocol is not supported by the implementation.

   EPROTOTYPE
          The socket type is not supported by the protocol.

   The socket() function may fail if:

   EACCES The process does not have appropriate privileges.

   ENOBUFS
          Insufficient resources were available in the system to perform the operation.

   ENOMEM Insufficient memory was available to fulfill the request.

(man 3 socket)

I've recently encountered the case that socket() returned a non-negative fd, while all operations(accept(), connect(), etc.) on it would fail. This error was found by valgrind --track-fds=yes -v.

Does this mean that even if socket() returns a non-negative fd, fd is still possible to be invalid?

lz96
  • 2,816
  • 2
  • 28
  • 46
  • What errors do you get on the accept or connect? If, for example, you're out of fd's, the initial socket() call could work but since an accept() is another fd it may still fail. – stdunbar Jan 26 '17 at 18:41
  • @stdunbar valgrind output 'invalid fd 1022 for syscall socket' which looks like reaching fd limit, but in some cases connect would fail either. Is there any possibility for this? (Actually I'm testing a toy server under high pressure) – lz96 Jan 26 '17 at 19:02
  • A non-negative fd returned by socket() will be a valid socket. Whether or not it actually does anything useful, OTOH, will depend on the condition of the networking stack, etc. If it doesn't work things must have gone very very wrong, though :) – Jeremy Friesner Jan 26 '17 at 21:54

0 Answers0