Add 469782 to NEWS
[valgrind.git] / none / tests / fdleak.h
blobf0a6eac4933de707a4a08c3f0a68f057f2a0fbff
1 #ifndef _FDLEAK_H_
2 #define _FDLEAK_H_
4 #define _GNU_SOURCE
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <stdio.h>
8 #include <sys/stat.h>
10 #define DO(op) \
11 ({ \
12 long res = op; \
13 if (res < 0) { \
14 perror(#op); \
15 exit(1); \
16 }; \
17 res; \
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) {
30 struct stat sb;
31 int i; int max_fds = sysconf (_SC_OPEN_MAX);
32 if (max_fds < 0)
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. */
38 close(i);
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_ */