24 #include <sys/types.h>
30 #define HAVE_SOCKETS 1
31 #elif defined HAVE_SYS_SOCKET_H
32 #include <sys/socket.h>
34 #define HAVE_F_GETFD F_GETFD
35 #define HAVE_SOCKETS 1
38 #ifdef HAVE_SYS_RESOURCE_H
39 #include <sys/resource.h>
47 #define SOCK_CLOEXEC 0
72 dir = opendir (
"/proc/self/fd");
78 for (entry = readdir (dir); entry != NULL; entry = readdir (dir))
85 fd = strtol (entry->d_name, &tail, 10);
86 if (*tail !=
'\0' || errno != 0)
94 if (fd == dirfd (dir))
97 result =
func (arg, fd);
111 #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
114 if (getrlimit (RLIMIT_NOFILE, &rlim) == 0 && rlim.rlim_max != RLIM_INFINITY)
120 max = sysconf (_SC_OPEN_MAX);
127 for (fd = 0; fd <
max; ++fd)
133 if (fstat (fd, &sb) == -1)
136 result =
func (arg, fd);
153 static VEC (
int) *open_fds;
159 do_mark_open_fd (
void *
ignore,
int fd)
170 fdwalk (do_mark_open_fd, NULL);
178 do_mark_open_fd (NULL, fd);
188 for (i = 0;
VEC_iterate (
int, open_fds, i, val); ++i)
208 for (i = 0;
VEC_iterate (
int, open_fds, i, val); ++i)
247 int old = fcntl (fd, F_GETFD, 0);
251 fcntl (fd, F_SETFD, old | FD_CLOEXEC);
253 if (trust_o_cloexec == 0)
255 if ((old & FD_CLOEXEC) != 0)
258 trust_o_cloexec = -1;
269 if (trust_o_cloexec <= 0)
293 int fd = open (filename, flags |
O_CLOEXEC, mode);
313 static int fopen_e_ever_failed_einval =
O_CLOEXEC == 0;
315 if (!fopen_e_ever_failed_einval)
319 copy = alloca (strlen (opentype) + 2);
320 strcpy (copy, opentype);
324 result = fopen (filename, copy);
326 if (result == NULL && errno == EINVAL)
328 result = fopen (filename, opentype);
330 fopen_e_ever_failed_einval = 1;
334 result = fopen (filename, opentype);
349 #ifdef HAVE_SOCKETPAIR
350 int result = socketpair (domain, style |
SOCK_CLOEXEC, protocol, filedes);
369 int result = socket (domain, style |
SOCK_CLOEXEC, protocol);
394 result = pipe (filedes);
423 int *saved_fd =
xmalloc (
sizeof (fd));
static void mark_cloexec(int fd)
void notice_open_fds(void)
static int fdwalk(int(*func)(void *, int), void *arg)
static void socket_mark_cloexec(int fd)
int gdb_pipe_cloexec(int filedes[2])
#define VEC_unordered_remove(T, V, I)
#define VEC_safe_push(T, V, O)
FILE * gdb_fopen_cloexec(const char *filename, const char *opentype)
int gdb_socketpair_cloexec(int domain, int style, int protocol, int filedes[2])
int gdb_socket_cloexec(int domain, int style, int protocol)
static void do_close_cleanup(void *arg)
mach_port_t kern_return_t mach_port_t msgports mach_port_t kern_return_t pid_t pid mach_port_t kern_return_t mach_port_t task mach_port_t kern_return_t int flags
#define VEC_iterate(T, V, I, P)
void unmark_fd_no_cloexec(int fd)
static int do_close(void *ignore, int fd)
struct cleanup * make_cleanup_dtor(make_cleanup_ftype *function, void *arg, make_cleanup_dtor_ftype *dtor)
#define gdb_assert_not_reached(message)
struct cleanup * make_cleanup_close(int fd)
void close_most_fds(void)
static int trust_o_cloexec
int gdb_open_cloexec(const char *filename, int flags, unsigned long mode)
static int ignore(struct target_ops *ops, struct gdbarch *gdbarch, struct bp_target_info *bp_tgt)
void mark_fd_no_cloexec(int fd)
static void maybe_mark_cloexec(int fd)