21 * The macro below closes file descriptors inherited from the process
22 * that forked the current process. Close these file descriptors right
23 * after the start of main() in order to get consistent results across
24 * different releases. Known behavior:
25 * - Fedora Core 1's Perl opens /dev/pts/2 as fd 10.
26 * - For Ubuntu 8.04, see also
27 * https://bugs.launchpad.net/ubuntu/+source/seahorse/+bug/235184
29 static void close_inherited (void) {
31 int i
; int max_fds
= sysconf (_SC_OPEN_MAX
);
33 max_fds
= 1024; /* Fallback if sysconf fails, returns -1. */
35 /* Only leave 0 (stdin), 1 (stdout) and 2 (stderr) open. */
36 for (i
= 3; i
< max_fds
; i
++)
37 if (fstat (i
, &sb
) != -1) /* Test if the file descriptor exists first. */
40 #define CLOSE_INHERITED_FDS close_inherited ()
41 /* Note that the following would be nicer, but close_range is fairly new. */
42 // #define CLOSE_INHERITED_FDS close_range (3, ~0U, 0)
44 #endif /* _FDLEAK_H_ */