Logo Search packages:      
Sourcecode: condor version File versions  Download package

bool DaemonCore::TooManyRegisteredSockets ( int  fd = -1,
MyString msg = NULL,
int  num_fds = 1 
)

This function is specifically tailored for testing if it is ok to register a new socket (e.g. for non-blocking connect).

Parameters:
fd Recently opened file descriptor, helpful, but not required for determining if we are in danger.
msg Optional string into which this function writes a human readable description of why it was decided that there are too many open sockets.
num_fds Number of file descriptors that the caller plans to register. This will usually be 1, but may be 2 for UDP (UDP socket plus TCP socket for establishing the security session).
Returns:
true of in danger of running out of file descriptors

Definition at line 641 of file daemon_core.cpp.

References FileDescriptorSafetyLimit(), RegisteredSocketCount(), and MyString::sprintf().

{
      int registered_socket_count = RegisteredSocketCount();
      int fds_used = registered_socket_count;
      int safety_limit = FileDescriptorSafetyLimit();

      if( safety_limit < 0 ) {
                  // No limit.
            return false;
      }

            // The following heuristic is only appropriate on systems where
            // file descriptor numbers are allocated using the lowest
            // available number.
#if !defined(WIN32)
      if (fd == -1) {
            // TODO If num_fds>1, should we call open() multiple times?
            fd = safe_open_wrapper( NULL_FILE, O_RDONLY );
            if ( fd >= 0 ) {
                  close( fd );
            }
      }
      if( fd > fds_used ) {
                  // Assume fds are allocated always lowest number first
            fds_used = fd;
      }
#endif

      if( num_fds + fds_used > file_descriptor_safety_limit ) {
            if( registered_socket_count < MIN_REGISTERED_SOCKET_SAFETY_LIMIT ) {
                  // We don't have very many sockets registered, but
                  // we seem to be running out of file descriptors.
                  // Perhaps there is a file descriptor leak or
                  // perhaps the safety limit is insanely low.
                  // Either way, it is better to try and fail than
                  // to risk getting into a stalemate.

                  if (msg) {
                              // If caller didn't ask for error messages, then don't
                              // make noise in the log either, because caller is
                              // just testing the waters.
                        dprintf(D_NETWORK|D_FULLDEBUG,
                                    "Ignoring file descriptor safety limit (%d), because "
                                    "only %d sockets are registered (fd is %d)\n",
                                    file_descriptor_safety_limit,
                                    registered_socket_count,
                                    fd );
                  }
                  return false;
            }
            if(msg) {
                  msg->sprintf( "file descriptor safety level exceeded: "
                                " limit %d, "
                                " registered socket count %d, "
                                " fd %d",
                                safety_limit, registered_socket_count, fd );
            }
            return true;
      }
      return false;
}


Generated by  Doxygen 1.6.0   Back to index