[svn.haxx.se] · SVN Dev · SVN Users · SVN Org · TSVN Dev · TSVN Users · Subclipse Dev · Subclipse Users · this month's index

Re: Why does SVN + apache try to close non existing file descriptors?

From: Phil Endecott <spam_from_subversion_users_at_chezphil.org>
Date: Wed, 12 Mar 2008 14:41:32 +0000

Jon Bendtsen wrote:
> So i used strace on apache and it turns out it tries to close
> 1024*1024 file descriptors more than once.

> Q) why brute force close all files?
> b) ask the kernel which files do I have open and then close only those

Here is some code to do this. I hereby place this code in the public domain.

#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

void close_all_open_file_descriptors(int min_fd) {
   // Find all open file descriptors >= min_fd using
   // /proc/self/fd and close them.
   // Use min_fd=3 to leave stdin/out/err open.
   // Probably won't work if there are other threads
   // opening or closing files where we're running.

   DIR* proc_self_fd = opendir("/proc/self/fd");
   if (!proc_self_fd) {
     report_error("opendir(/proc/self/fd) failed");
   }
   // We must take care not to close the file descriptor
   // underlying the DIR* that we're using:
   int dir_fd = dirfd(proc_self_fd);
   while (1) {
     struct dirent* d = readdir(proc_self_fd);
     if (!d) {
       // Should mean end-of-directory, but could also be
       // an error; how to distinguish?
       break;
     }
     // We need to ignore the . and .. entries.
     // The portable way to do this is to stat() the
     // pathname. An alternative is to use the non-portable
     // d_type field in struct_dirent. This is less portable
     // - but this code is not very portable anyway, since it
     // uses /proc.
#ifndef _DIRENT_HAVE_D_TYPE
#error This code needs a struct direct with d_type; see 'man readdir'
#endif
     if (d->d_type == DT_DIR) {
       continue;
     }
     int fd = strtol(d->d_name,NULL,10);
     if (fd >= min_fd && fd != dir_fd) {
       close(fd);
     }
   }
   closedir(proc_self_fd);
}

I have no idea where this should live; perhaps somewhere in APR? It
obviously needs to be inside a #if linux block.

Phil.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_subversion.tigris.org
For additional commands, e-mail: users-help_at_subversion.tigris.org
Received on 2008-03-12 15:42:44 CET

This is an archived mail posted to the Subversion Users mailing list.

This site is subject to the Apache Privacy Policy and the Apache Public Forum Archive Policy.