--- glib2.0-2.12.13/glib/gspawn.c	2007-07-16 20:57:12.000000000 +0200
+++ glib2.0-2.12.13.lennart/glib/gspawn.c	2007-08-22 14:43:16.000000000 +0200
@@ -31,11 +31,16 @@
 #include <signal.h>
 #include <string.h>
 #include <stdlib.h>   /* for fdwalk */
+#include <dirent.h>
 
 #ifdef HAVE_SYS_SELECT_H
 #include <sys/select.h>
 #endif /* HAVE_SYS_SELECT_H */
 
+#ifdef HAVE_SYS_RESOURCE_H
+#include <sys/resource.h>
+#endif /* HAVE_SYS_RESOURCE_H */
+
 #include "glib.h"
 #include "glibintl.h"
 #include "galias.h"
@@ -868,12 +873,62 @@
 {
   gint open_max;
   gint fd;
-  gint res;
+  gint res = 0;
+  
+#ifdef HAVE_SYS_RESOURCE_H
+  struct rlimit rl;
+#endif
+
+#ifdef __linux__  
+  DIR *d;
+
+  if ((d = opendir("/proc/self/fd"))) {
+      struct dirent *de;
+
+      while ((de = readdir(d))) {
+          glong l;
+          gchar *e = NULL;
 
-  res = 0;
-  open_max = sysconf (_SC_OPEN_MAX);
-  for (fd = 0; fd < open_max && res == 0; fd++)
-    res = cb (data, fd);
+          if (de->d_name[0] == '.')
+              continue;
+            
+          errno = 0;
+          l = strtol(de->d_name, &e, 10);
+          if (errno != 0 || !e || *e)
+              continue;
+
+          fd = (gint) l;
+
+          if ((glong) fd != l)
+              continue;
+
+          if (fd == dirfd(d))
+              continue;
+
+          if ((res = cb (data, fd)) != 0)
+              break;
+        }
+      
+      closedir(d);
+      return res;
+  }
+
+  /* If /proc is not mounted or not accessible we fall back to the old
+   * rlimit trick */
+
+#endif
+  
+#ifdef HAVE_SYS_RESOURCE_H
+      
+  if (getrlimit(RLIMIT_NOFILE, &rl) == 0)
+      open_max = rl.rlim_max;
+  else
+#endif
+      open_max = sysconf (_SC_OPEN_MAX);
+
+  for (fd = 0; fd < open_max; fd++)
+      if ((res = cb (data, fd)) != 0)
+          break;
 
   return res;
 }
--- glib2.0-2.12.13/configure.in	2007-07-16 20:57:18.000000000 +0200
+++ glib2.0-2.12.13.lennart/configure.in	2007-08-22 14:09:16.000000000 +0200
@@ -790,7 +790,7 @@
 AC_C_BIGENDIAN
 
 # check for header files
-AC_CHECK_HEADERS([dirent.h float.h limits.h pwd.h sys/param.h sys/poll.h])
+AC_CHECK_HEADERS([dirent.h float.h limits.h pwd.h sys/param.h sys/poll.h sys/resource.h])
 AC_CHECK_HEADERS([sys/time.h sys/times.h sys/wait.h unistd.h values.h])
 AC_CHECK_HEADERS([sys/select.h sys/types.h stdint.h sched.h malloc.h])
 
