* RFC: close-on-exec internal file descriptors
@ 2012-12-18 17:45 Tom Tromey
2012-12-18 22:12 ` Andreas Schwab
2013-04-23 15:45 ` Yao Qi
0 siblings, 2 replies; 13+ messages in thread
From: Tom Tromey @ 2012-12-18 17:45 UTC (permalink / raw)
To: gdb-patches
gdb leaks file descriptors to programs it starts. This is PR 7912.
The simplest way to see it is "gdb gdb" and then:
(gdb) shell ls -l /proc/self/fd
total 0
lrwx------. 1 tromey tromey 64 Dec 18 10:32 0 -> /dev/pts/3
lrwx------. 1 tromey tromey 64 Dec 18 10:32 1 -> /dev/pts/3
lrwx------. 1 tromey tromey 64 Dec 18 10:32 2 -> /dev/pts/3
lrwx------. 1 tromey tromey 64 Dec 18 10:32 3 -> socket:[3180025]
lrwx------. 1 tromey tromey 64 Dec 18 10:32 4 -> socket:[3180026]
lr-x------. 1 tromey tromey 64 Dec 18 10:32 5 -> pipe:[3180027]
l-wx------. 1 tromey tromey 64 Dec 18 10:32 6 -> pipe:[3180027]
lr-x------. 1 tromey tromey 64 Dec 18 10:32 7 -> /proc/24690/fd
Ages and ages ago I sent a patch to fix this:
http://sourceware.org/ml/gdb-patches/2008-12/msg00107.html
Mark didn't like my chosen approach:
http://sourceware.org/ml/gdb-patches/2008-12/msg00137.html
I've rewritten this patch using both approaches: O_CLOEXEC and
close-all-file-descriptors.
The reason we want O_CLOEXEC is that it is clearly better in the
multi-threaded case. It allows the file descriptor to be created
O_CLOEXEC, so that there is no race window with other threads,
regardless of what they may be doing.
O_CLOEXEC is POSIX now, which ought to provide some relief. I don't
know whether it is widely implemented or whether SOCK_CLOEXEC or pipe2
are POSIX; but it doesn't really matter given the rest of the patch.
This patch also supplies an implementation of the BSD closefrom and
arranges to use it where needed.
This might seem redundant given the CLOEXEC business, but it is needed
for the mirror image problem: a library using open poorly, either not
using O_CLOEXEC or using fcntl and allowing a race.
I looked at using gnulib here, but it seems to define O_CLOEXEC to 0
when it is not available (ok) but then also not provide any backup plan
(not ok).
So in the end this patch introduces some wrapper function that ensure
that CLOEXEC is used. Then it changes most of gdb to use these. I
didn't touch some areas that I thought that I couldn't possibly test, so
there are still some calls to fopen and whatnot in the tree. Fixing
these is trivial for someone with the right host.
Built and regtested on x86-64 Fedora 16.
Let me know what you think.
Tom
2012-12-18 Tom Tromey <tromey@redhat.com>
PR gdb/7912:
* Makefile.in (SFILES): Add filestuff.c
(COMMON_OBS): Add filestuff.o.
(filestuff.o): New target.
* auto-load.c (auto_load_objfile_script_1): Use
gdb_fopen_cloexec.
* auxv.c (procfs_xfer_auxv): Use gdb_open_cloexec.
* cli/cli-cmds.c (shell_escape): Call closefrom.
* cli/cli-dump.c (fopen_with_cleanup): Use gdb_fopen_cloexec.
* common/agent.c (gdb_connect_sync_socket): Use
gdb_socket_cloexec.
* common/filestuff.c: New file.
* common/filestuff.h: New file.
* common/linux-osdata.c (linux_common_core_of_thread)
(command_from_pid, commandline_from_pid, print_source_lines)
(linux_xfer_osdata_shm, linux_xfer_osdata_sem)
(linux_xfer_osdata_msg, linux_xfer_osdata_modules): Use
gdb_fopen_cloexec.
* common/linux-procfs.c (linux_proc_get_int)
(linux_proc_pid_has_state): Use gdb_fopen_cloexec.
* config.in, configure: Rebuild.
* configure.ac: Don't check for sys/socket.h. Check for
fdwalk, closefrom, pipe2.
* corelow.c (core_open): Use gdb_open_cloexec.
* dwarf2read.c (write_psymtabs_to_index): Use gdb_fopen_cloexec.
* fork-child.c (fork_inferior): Call closefrom.
* gdb_bfd.c (gdb_bfd_open): Use gdb_open_cloexec.
* inf-child.c (inf_child_fileio_readlink): Use gdb_open_cloexec.
* linux-nat.c (linux_nat_thread_name, linux_proc_pending_signals):
Use gdb_fopen_cloexec.
(linux_proc_xfer_partial, linux_proc_xfer_spu): Use
gdb_open_cloexec.
(linux_async_pipe): Use gdb_pipe_cloexec.
* remote-fileio.c (remote_fileio_func_open): Use
gdb_open_cloexec.
* remote.c (remote_file_put, remote_file_get): Use
gdb_fopen_cloexec.
* ser-pipe.c (pipe_open): Use gdb_socketpair_cloexec, closefrom.
* ser-tcp.c (net_open): Use gdb_socket_cloexec.
* ser-unix.c (hardwire_open): Use gdb_open_cloexec.
* solib.c (solib_find): Use gdb_open_cloexec.
* source.c (openp, find_and_open_source): Use gdb_open_cloexec.
* tracepoint.c (trace_save): Use gdb_fopen_cloexec.
(tfile_open): Use gdb_open_cloexec.
* tui/tui-io.c (tui_initialize_io): Use gdb_pipe_cloexec.
* ui-file.c (gdb_fopen): Use gdb_fopen_cloexec.
* xml-support.c (xml_fetch_content_from_file): Use
gdb_fopen_cloexec.
2012-12-18 Tom Tromey <tromey@redhat.com>
* Makefile.in (SFILES): Add filestuff.c.
(OBS): Add filestuff.o.
(filestuff.o): New target.
* config.in, configure: Rebuild.
* configure.ac: Check for fdwalk, closefrom, pipe2.
diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 244694c..6fb13b5 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -754,7 +754,7 @@ SFILES = ada-exp.y ada-lang.c ada-typeprint.c ada-valprint.c ada-tasks.c \
regset.c sol-thread.c windows-termcap.c \
common/gdb_vecs.c common/common-utils.c common/xml-utils.c \
common/ptid.c common/buffer.c gdb-dlfcn.c common/agent.c \
- common/format.c
+ common/format.c common/filestuff.c
LINTFILES = $(SFILES) $(YYFILES) $(CONFIG_SRCS) init.c
@@ -874,6 +874,7 @@ COMMON_OBS = $(DEPFILES) $(CONFIG_OBS) $(YYOBJ) \
expprint.o environ.o stack.o thread.o \
exceptions.o \
filesystem.o \
+ filestuff.o \
inf-child.o \
interps.o \
minidebug.o \
@@ -1928,6 +1929,10 @@ buffer.o: ${srcdir}/common/buffer.c
$(COMPILE) $(srcdir)/common/buffer.c
$(POSTCOMPILE)
+filestuff.o: $(srcdir)/common/filestuff.c
+ $(COMPILE) $(srcdir)/common/filestuff.c
+ $(POSTCOMPILE)
+
format.o: ${srcdir}/common/format.c
$(COMPILE) $(srcdir)/common/format.c
$(POSTCOMPILE)
diff --git a/gdb/auto-load.c b/gdb/auto-load.c
index c5eb8d1..cdbfd6f 100644
--- a/gdb/auto-load.c
+++ b/gdb/auto-load.c
@@ -38,6 +38,7 @@
#include "observer.h"
#include "fnmatch.h"
#include "top.h"
+#include "filestuff.h"
/* The suffix of per-objfile scripts to auto-load as non-Python command files.
E.g. When the program loads libfoo.so, look for libfoo-gdb.gdb. */
@@ -739,7 +740,7 @@ auto_load_objfile_script_1 (struct objfile *objfile, const char *realname,
cleanups = make_cleanup (xfree, filename);
- input = fopen (filename, "r");
+ input = gdb_fopen_cloexec (filename, "r");
debugfile = filename;
if (debug_auto_load)
fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file \"%s\" %s.\n"),
@@ -771,7 +772,7 @@ auto_load_objfile_script_1 (struct objfile *objfile, const char *realname,
strcat (debugfile, filename);
make_cleanup (xfree, debugfile);
- input = fopen (debugfile, "r");
+ input = gdb_fopen_cloexec (debugfile, "r");
if (debug_auto_load)
fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file "
"\"%s\" %s.\n"),
diff --git a/gdb/auxv.c b/gdb/auxv.c
index e2aeb21..93c520e 100644
--- a/gdb/auxv.c
+++ b/gdb/auxv.c
@@ -26,6 +26,7 @@
#include "gdb_assert.h"
#include "gdbcore.h"
#include "observer.h"
+#include "filestuff.h"
#include "auxv.h"
#include "elf/common.h"
@@ -48,7 +49,7 @@ procfs_xfer_auxv (gdb_byte *readbuf,
LONGEST n;
pathname = xstrprintf ("/proc/%d/auxv", PIDGET (inferior_ptid));
- fd = open (pathname, writebuf != NULL ? O_WRONLY : O_RDONLY);
+ fd = gdb_open_cloexec (pathname, writebuf != NULL ? O_WRONLY : O_RDONLY, 0);
xfree (pathname);
if (fd < 0)
return -1;
diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c
index b65262e..810c11b 100644
--- a/gdb/cli/cli-cmds.c
+++ b/gdb/cli/cli-cmds.c
@@ -39,6 +39,7 @@
#include "source.h"
#include "disasm.h"
#include "tracepoint.h"
+#include "filestuff.h"
#include "ui-out.h"
@@ -730,6 +731,8 @@ shell_escape (char *arg, int from_tty)
{
const char *p, *user_shell;
+ closefrom (3);
+
if ((user_shell = (char *) getenv ("SHELL")) == NULL)
user_shell = "/bin/sh";
diff --git a/gdb/cli/cli-dump.c b/gdb/cli/cli-dump.c
index c2d1397..0cd0d75 100644
--- a/gdb/cli/cli-dump.c
+++ b/gdb/cli/cli-dump.c
@@ -33,6 +33,7 @@
#include "gdbcore.h"
#include "cli/cli-utils.h"
#include "gdb_bfd.h"
+#include "filestuff.h"
#define XMALLOC(TYPE) ((TYPE*) xmalloc (sizeof (TYPE)))
@@ -99,7 +100,7 @@ scan_filename_with_cleanup (char **cmd, const char *defname)
FILE *
fopen_with_cleanup (const char *filename, const char *mode)
{
- FILE *file = fopen (filename, mode);
+ FILE *file = gdb_fopen_cloexec (filename, mode);
if (file == NULL)
perror_with_name (filename);
diff --git a/gdb/common/agent.c b/gdb/common/agent.c
index 156b154..b2baba9 100644
--- a/gdb/common/agent.c
+++ b/gdb/common/agent.c
@@ -28,6 +28,7 @@
#include <string.h>
#include <unistd.h>
#include "agent.h"
+#include "filestuff.h"
int debug_agent = 0;
@@ -168,7 +169,7 @@ gdb_connect_sync_socket (int pid)
if (res >= UNIX_PATH_MAX)
return -1;
- res = fd = socket (PF_UNIX, SOCK_STREAM, 0);
+ res = fd = gdb_socket_cloexec (PF_UNIX, SOCK_STREAM, 0);
if (res == -1)
{
warning (_("error opening sync socket: %s"), strerror (errno));
diff --git a/gdb/common/filestuff.c b/gdb/common/filestuff.c
new file mode 100644
index 0000000..4aa7361
--- /dev/null
+++ b/gdb/common/filestuff.c
@@ -0,0 +1,317 @@
+/* Low-level file-handling.
+ Copyright (C) 2012 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifdef GDBSERVER
+#include "server.h"
+#else
+#include "defs.h"
+#include "gdb_string.h"
+#endif
+#include "filestuff.h"
+
+#include <string.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/socket.h>
+
+#ifdef HAVE_SYS_RESOURCE_H
+#include <sys/resource.h>
+#endif /* HAVE_SYS_RESOURCE_H */
+
+#ifndef O_CLOEXEC
+#define O_CLOEXEC 0
+#endif
+
+#ifndef SOCK_CLOEXEC
+#define SOCK_CLOEXEC 0
+#endif
+
+\f
+
+#ifndef HAVE_CLOSEFROM
+
+/* We only need to check for fdwalk if we also need to implement
+ closefrom. */
+
+#ifndef HAVE_FDWALK
+
+#include <dirent.h>
+
+/* Replacement for fdwalk, if the system doesn't define it. Walks all
+ open file descriptors (though this implementation may walk closed
+ ones as well, depending on the host platform's capabilities) and
+ call FUNC with ARG. If FUNC returns non-zero, stops immediately
+ and returns the same value. Otherwise, returns zero when
+ finished. */
+
+static int
+fdwalk (int (*func) (void *, int), void *arg)
+{
+ /* Checking __linux__ isn't great but it isn't clear what would be
+ better. There doesn't seem to be a good way to check for this in
+ configure. */
+#ifdef __linux__
+ DIR *dir;
+
+ dir = opendir ("/proc/self/fd");
+ if (dir != NULL)
+ {
+ struct dirent *entry;
+ int result = 0;
+
+ for (entry = readdir (dir); entry != NULL; entry = readdir (dir))
+ {
+ long fd;
+ char *tail;
+ int result;
+
+ errno = 0;
+ fd = strtol (entry->d_name, &tail, 10);
+ if (*tail != '\0' || errno != 0)
+ continue;
+ if ((int) fd != fd)
+ {
+ /* What can we do here really? */
+ continue;
+ }
+
+ if (fd == dirfd (dir))
+ continue;
+
+ result = func (arg, fd);
+ if (result != 0)
+ break;
+ }
+
+ closedir (dir);
+ return result;
+ }
+ /* We may fall through to the next case. */
+#endif
+
+ {
+ int max, fd;
+
+#ifdef HAVE_GETRLIMIT
+ struct rlimit rlim;
+
+ if (getrlimit (RLIMIT_NOFILE, &rlim) == 0 && rlim.rlim_max != RLIM_INFINITY)
+ max = rlim.rlim_max;
+ else
+#endif
+ {
+#ifdef _SC_OPEN_MAX
+ max = sysconf (_SC_OPEN_MAX);
+#else
+ /* Whoops. */
+ return 0;
+#endif /* _SC_OPEN_MAX */
+ }
+
+ for (fd = 0; fd < max; ++fd)
+ {
+ int result = func (arg, fd);
+
+ if (result != 0)
+ return result;
+ }
+
+ return 0;
+ }
+}
+
+#endif /* HAVE_FDWALK */
+
+/* Helper function for closefrom that closes the file descriptor if
+ appropriate. */
+
+static int
+do_close (void *arg, int fd)
+{
+ int low = *(int *) arg;
+
+ if (fd >= low)
+ close (fd);
+
+ return 0;
+}
+
+/* See filestuff.h. */
+
+void
+closefrom (int low)
+{
+ fdwalk (do_close, &low);
+}
+
+#endif /* HAVE_CLOSEFROM */
+
+\f
+
+/* This is a tri-state flag. When zero it means we haven't yet tried
+ O_CLOEXEC. When positive it means that O_CLOEXEC works on this
+ host. When negative, it means that O_CLOEXEC doesn't work. We
+ track this state because, while gdb might have been compiled
+ against a libc that supplies O_CLOEXEC, there is no guarantee that
+ the kernel supports it. */
+
+static int trust_o_cloexec;
+
+/* Mark FD as close-on-exec, ignoring errors. Update
+ TRUST_O_CLOEXEC. */
+
+static void
+mark_cloexec (int fd)
+{
+ int old = fcntl (fd, F_GETFD, 0);
+
+ if (old != -1)
+ {
+ fcntl (fd, F_SETFD, old | FD_CLOEXEC);
+
+ if (trust_o_cloexec == 0)
+ {
+ if ((old & FD_CLOEXEC) != 0)
+ trust_o_cloexec = 1;
+ else
+ trust_o_cloexec = -1;
+ }
+ }
+}
+
+/* Depending on TRUST_O_CLOEXEC, mark FD as close-on-exec. */
+
+static void
+maybe_mark_cloexec (int fd)
+{
+ if (trust_o_cloexec <= 0)
+ mark_cloexec (fd);
+}
+
+/* Like maybe_mark_cloexec, but for callers that use SOCK_CLOEXEC. */
+
+static void
+socket_mark_cloexec (int fd)
+{
+ if (SOCK_CLOEXEC == 0 || trust_o_cloexec <= 0)
+ mark_cloexec (fd);
+}
+
+\f
+
+/* See filestuff.h. */
+
+int
+gdb_open_cloexec (const char *filename, int flags, mode_t mode)
+{
+ int fd = open (filename, flags | O_CLOEXEC, mode);
+
+ if (fd >= 0)
+ maybe_mark_cloexec (fd);
+
+ return fd;
+}
+
+/* See filestuff.h. */
+
+FILE *
+gdb_fopen_cloexec (const char *filename, const char *opentype)
+{
+ FILE *result;
+ static int fopen_e_ever_failed;
+
+ if (!fopen_e_ever_failed)
+ {
+ char *copy;
+
+ copy = alloca (strlen (opentype) + 2);
+ strcpy (copy, opentype);
+ /* This is a glibc extension but we try it unconditionally on
+ this path. */
+ strcat (copy, "e");
+ result = fopen (filename, copy);
+ }
+
+ if (result == NULL)
+ {
+ /* Fallback. */
+ result = fopen (filename, opentype);
+ if (result != NULL)
+ fopen_e_ever_failed = 1;
+ }
+
+ if (result != NULL)
+ maybe_mark_cloexec (fileno (result));
+
+ return result;
+}
+
+/* See filestuff.h. */
+
+int
+gdb_socketpair_cloexec (int namespace, int style, int protocol, int filedes[2])
+{
+ int result = socketpair (namespace, style | SOCK_CLOEXEC, protocol, filedes);
+
+ if (result != -1)
+ {
+ socket_mark_cloexec (filedes[0]);
+ socket_mark_cloexec (filedes[1]);
+ }
+
+ return result;
+}
+
+/* See filestuff.h. */
+
+int
+gdb_socket_cloexec (int namespace, int style, int protocol)
+{
+ int result = socket (namespace, style | SOCK_CLOEXEC, protocol);
+
+ if (result != -1)
+ socket_mark_cloexec (result);
+
+ return result;
+}
+
+/* See filestuff.h. */
+
+int
+gdb_pipe_cloexec (int filedes[2])
+{
+ int result;
+
+#ifdef HAVE_PIPE2
+ result = pipe2 (filedes, O_CLOEXEC);
+ if (result != -1)
+ {
+ maybe_mark_cloexec (filedes[0]);
+ maybe_mark_cloexec (filedes[1]);
+ }
+#else
+ result = pipe (filedes);
+ if (result != -1)
+ {
+ mark_cloexec (filedes[0]);
+ mark_cloexec (filedes[1]);
+ }
+#endif
+
+ return result;
+}
diff --git a/gdb/common/filestuff.h b/gdb/common/filestuff.h
new file mode 100644
index 0000000..d63b633
--- /dev/null
+++ b/gdb/common/filestuff.h
@@ -0,0 +1,57 @@
+/* Low-level file-handling.
+ Copyright (C) 2012 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifndef FILESTUFF_H
+#define FILESTUFF_H
+
+#ifndef HAVE_CLOSEFROM
+
+/* Close all open file descriptors greater than or equal to LOW.
+ Errors that occur while closing are ignored. */
+
+extern void closefrom (int low);
+
+#endif /* HAVE_CLOSEFROM */
+
+/* Like 'open', but ensures that the returned file descriptor has the
+ close-on-exec flag set. */
+
+extern int gdb_open_cloexec (const char *filename, int flags, mode_t mode);
+
+/* Like 'fopen', but ensures that the returned file descriptor has the
+ close-on-exec flag set. */
+
+extern FILE *gdb_fopen_cloexec (const char *filename, const char *opentype);
+
+/* Like 'socketpair', but ensures that the returned file descriptors
+ have the close-on-exec flag set. */
+
+extern int gdb_socketpair_cloexec (int namespace, int style, int protocol,
+ int filedes[2]);
+
+/* Like 'socket', but ensures that the returned file descriptor has
+ the close-on-exec flag set. */
+
+extern int gdb_socket_cloexec (int namespace, int style, int protocol);
+
+/* Like 'pipe', but ensures that the returned file descriptors have
+ the close-on-exec flag set. */
+
+extern int gdb_pipe_cloexec (int filedes[2]);
+
+#endif /* FILESTUFF_H */
diff --git a/gdb/common/linux-osdata.c b/gdb/common/linux-osdata.c
index b275495..06fa17d 100644
--- a/gdb/common/linux-osdata.c
+++ b/gdb/common/linux-osdata.c
@@ -44,6 +44,7 @@
#include "gdb_assert.h"
#include "gdb_dirent.h"
#include "gdb_stat.h"
+#include "filestuff.h"
/* Define PID_T to be a fixed size that is at least as large as pid_t,
so that reading pid values embedded in /proc works
@@ -76,7 +77,7 @@ linux_common_core_of_thread (ptid_t ptid)
sprintf (filename, "/proc/%lld/task/%lld/stat",
(PID_T) ptid_get_pid (ptid), (PID_T) ptid_get_lwp (ptid));
- f = fopen (filename, "r");
+ f = gdb_fopen_cloexec (filename, "r");
if (!f)
return -1;
@@ -125,7 +126,7 @@ static void
command_from_pid (char *command, int maxlen, PID_T pid)
{
char *stat_path = xstrprintf ("/proc/%lld/stat", pid);
- FILE *fp = fopen (stat_path, "r");
+ FILE *fp = gdb_fopen_cloexec (stat_path, "r");
command[0] = '\0';
@@ -165,7 +166,7 @@ commandline_from_pid (PID_T pid)
{
char *pathname = xstrprintf ("/proc/%lld/cmdline", pid);
char *commandline = NULL;
- FILE *f = fopen (pathname, "r");
+ FILE *f = gdb_fopen_cloexec (pathname, "r");
if (f)
{
@@ -860,7 +861,7 @@ print_sockets (unsigned short family, int tcp, struct buffer *buffer)
else
return;
- fp = fopen (proc_file, "r");
+ fp = gdb_fopen_cloexec (proc_file, "r");
if (fp)
{
char buf[8192];
@@ -1088,7 +1089,7 @@ linux_xfer_osdata_shm (gdb_byte *readbuf,
buffer_init (&buffer);
buffer_grow_str (&buffer, "<osdata type=\"shared memory\">\n");
- fp = fopen ("/proc/sysvipc/shm", "r");
+ fp = gdb_fopen_cloexec ("/proc/sysvipc/shm", "r");
if (fp)
{
char buf[8192];
@@ -1216,7 +1217,7 @@ linux_xfer_osdata_sem (gdb_byte *readbuf,
buffer_init (&buffer);
buffer_grow_str (&buffer, "<osdata type=\"semaphores\">\n");
- fp = fopen ("/proc/sysvipc/sem", "r");
+ fp = gdb_fopen_cloexec ("/proc/sysvipc/sem", "r");
if (fp)
{
char buf[8192];
@@ -1328,7 +1329,7 @@ linux_xfer_osdata_msg (gdb_byte *readbuf,
buffer_init (&buffer);
buffer_grow_str (&buffer, "<osdata type=\"message queues\">\n");
- fp = fopen ("/proc/sysvipc/msg", "r");
+ fp = gdb_fopen_cloexec ("/proc/sysvipc/msg", "r");
if (fp)
{
char buf[8192];
@@ -1454,7 +1455,7 @@ linux_xfer_osdata_modules (gdb_byte *readbuf,
buffer_init (&buffer);
buffer_grow_str (&buffer, "<osdata type=\"modules\">\n");
- fp = fopen ("/proc/modules", "r");
+ fp = gdb_fopen_cloexec ("/proc/modules", "r");
if (fp)
{
char buf[8192];
diff --git a/gdb/common/linux-procfs.c b/gdb/common/linux-procfs.c
index a5aa830..5cbbd0f 100644
--- a/gdb/common/linux-procfs.c
+++ b/gdb/common/linux-procfs.c
@@ -24,6 +24,7 @@
#endif
#include "linux-procfs.h"
+#include "filestuff.h"
/* Return the TGID of LWPID from /proc/pid/status. Returns -1 if not
found. */
@@ -37,7 +38,7 @@ linux_proc_get_int (pid_t lwpid, const char *field)
int retval = -1;
snprintf (buf, sizeof (buf), "/proc/%d/status", (int) lwpid);
- status_file = fopen (buf, "r");
+ status_file = gdb_fopen_cloexec (buf, "r");
if (status_file == NULL)
{
warning (_("unable to open /proc file '%s'"), buf);
@@ -83,7 +84,7 @@ linux_proc_pid_has_state (pid_t pid, const char *state)
int have_state;
xsnprintf (buffer, sizeof (buffer), "/proc/%d/status", (int) pid);
- procfile = fopen (buffer, "r");
+ procfile = gdb_fopen_cloexec (buffer, "r");
if (procfile == NULL)
{
warning (_("unable to open /proc file '%s'"), buffer);
diff --git a/gdb/config.in b/gdb/config.in
index 9e21325..c053b17 100644
--- a/gdb/config.in
+++ b/gdb/config.in
@@ -75,6 +75,9 @@
/* Define to 1 if you have the `canonicalize_file_name' function. */
#undef HAVE_CANONICALIZE_FILE_NAME
+/* Define to 1 if you have the `closefrom' function. */
+#undef HAVE_CLOSEFROM
+
/* Define to 1 if you have the <cursesX.h> header file. */
#undef HAVE_CURSESX_H
@@ -141,6 +144,9 @@
/* Define to 1 if your system has the etext variable. */
#undef HAVE_ETEXT
+/* Define to 1 if you have the `fdwalk' function. */
+#undef HAVE_FDWALK
+
/* Define to 1 if you have the `fork' function. */
#undef HAVE_FORK
@@ -267,6 +273,9 @@
/* Define to 1 if you have the `pipe' function. */
#undef HAVE_PIPE
+/* Define to 1 if you have the `pipe2' function. */
+#undef HAVE_PIPE2
+
/* Define to 1 if you have the `poll' function. */
#undef HAVE_POLL
@@ -500,9 +509,6 @@
/* Define to 1 if you have the <sys/select.h> header file. */
#undef HAVE_SYS_SELECT_H
-/* Define to 1 if you have the <sys/socket.h> header file. */
-#undef HAVE_SYS_SOCKET_H
-
/* Define to 1 if you have the <sys/stat.h> header file. */
#undef HAVE_SYS_STAT_H
diff --git a/gdb/configure b/gdb/configure
index 53a6ca9..9a33164 100755
--- a/gdb/configure
+++ b/gdb/configure
@@ -8893,7 +8893,7 @@ for ac_header in nlist.h machine/reg.h poll.h sys/poll.h proc_service.h \
sys/reg.h sys/debugreg.h sys/select.h sys/syscall.h \
sys/types.h sys/wait.h wait.h termios.h termio.h \
sgtty.h unistd.h elf_hp.h locale.h \
- dlfcn.h sys/socket.h sys/un.h linux/perf_event.h
+ dlfcn.h sys/un.h linux/perf_event.h
do :
as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh`
ac_fn_c_check_header_mongrel "$LINENO" "$ac_header" "$as_ac_Header" "$ac_includes_default"
@@ -10151,7 +10151,8 @@ for ac_func in canonicalize_file_name realpath getrusage getuid getgid \
sbrk setpgid setpgrp setsid \
sigaction sigprocmask sigsetmask socketpair syscall \
ttrace wborder wresize setlocale iconvlist libiconvlist btowc \
- setrlimit getrlimit posix_madvise waitpid lstat
+ setrlimit getrlimit posix_madvise waitpid lstat \
+ fdwalk closefrom pipe2
do :
as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
diff --git a/gdb/configure.ac b/gdb/configure.ac
index 5797561..e672788 100644
--- a/gdb/configure.ac
+++ b/gdb/configure.ac
@@ -1082,7 +1082,7 @@ AC_CHECK_HEADERS([nlist.h machine/reg.h poll.h sys/poll.h proc_service.h \
sys/reg.h sys/debugreg.h sys/select.h sys/syscall.h \
sys/types.h sys/wait.h wait.h termios.h termio.h \
sgtty.h unistd.h elf_hp.h locale.h \
- dlfcn.h sys/socket.h sys/un.h linux/perf_event.h])
+ dlfcn.h sys/un.h linux/perf_event.h])
AC_CHECK_HEADERS(link.h, [], [],
[#if HAVE_SYS_TYPES_H
# include <sys/types.h>
@@ -1164,7 +1164,8 @@ AC_CHECK_FUNCS([canonicalize_file_name realpath getrusage getuid getgid \
sbrk setpgid setpgrp setsid \
sigaction sigprocmask sigsetmask socketpair syscall \
ttrace wborder wresize setlocale iconvlist libiconvlist btowc \
- setrlimit getrlimit posix_madvise waitpid lstat])
+ setrlimit getrlimit posix_madvise waitpid lstat \
+ fdwalk closefrom pipe2])
AM_LANGINFO_CODESET
# Check the return and argument types of ptrace. No canned test for
diff --git a/gdb/corelow.c b/gdb/corelow.c
index 589f231..3b17789 100644
--- a/gdb/corelow.c
+++ b/gdb/corelow.c
@@ -47,6 +47,7 @@
#include "progspace.h"
#include "objfiles.h"
#include "gdb_bfd.h"
+#include "filestuff.h"
#ifndef O_LARGEFILE
#define O_LARGEFILE 0
@@ -314,7 +315,7 @@ core_open (char *filename, int from_tty)
flags |= O_RDWR;
else
flags |= O_RDONLY;
- scratch_chan = open (filename, flags, 0);
+ scratch_chan = gdb_open_cloexec (filename, flags, 0);
if (scratch_chan < 0)
perror_with_name (filename);
diff --git a/gdb/dwarf2read.c b/gdb/dwarf2read.c
index 4d5323e..6963abc 100644
--- a/gdb/dwarf2read.c
+++ b/gdb/dwarf2read.c
@@ -67,6 +67,7 @@
#include <ctype.h>
#include "gdb_bfd.h"
#include "f-lang.h"
+#include "filestuff.h"
#include <fcntl.h>
#include "gdb_string.h"
@@ -20277,7 +20278,7 @@ write_psymtabs_to_index (struct objfile *objfile, const char *dir)
INDEX_SUFFIX, (char *) NULL);
cleanup = make_cleanup (xfree, filename);
- out_file = fopen (filename, "wb");
+ out_file = gdb_fopen_cloexec (filename, "wb");
if (!out_file)
error (_("Can't open `%s' for writing"), filename);
diff --git a/gdb/fork-child.c b/gdb/fork-child.c
index b47019d..895e5c1 100644
--- a/gdb/fork-child.c
+++ b/gdb/fork-child.c
@@ -33,6 +33,7 @@
#include "command.h" /* for dont_repeat () */
#include "gdbcmd.h"
#include "solib.h"
+#include "filestuff.h"
#include <signal.h>
@@ -314,6 +315,8 @@ fork_inferior (char *exec_file_arg, char *allargs, char **env,
if (pid == 0)
{
+ closefrom (3);
+
if (debug_fork)
sleep (debug_fork);
diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c
index f0e349b..3af788c 100644
--- a/gdb/gdb_bfd.c
+++ b/gdb/gdb_bfd.c
@@ -25,6 +25,7 @@
#include "ui-out.h"
#include "gdbcmd.h"
#include "hashtab.h"
+#include "filestuff.h"
#ifdef HAVE_ZLIB_H
#include <zlib.h>
#endif
@@ -153,7 +154,7 @@ gdb_bfd_open (const char *name, const char *target, int fd)
if (fd == -1)
{
- fd = open (name, O_RDONLY | O_BINARY);
+ fd = gdb_open_cloexec (name, O_RDONLY | O_BINARY, 0);
if (fd == -1)
{
bfd_set_error (bfd_error_system_call);
diff --git a/gdb/gdbserver/Makefile.in b/gdb/gdbserver/Makefile.in
index c4f1a2d..53cfe70 100644
--- a/gdb/gdbserver/Makefile.in
+++ b/gdb/gdbserver/Makefile.in
@@ -155,7 +155,7 @@ SFILES= $(srcdir)/gdbreplay.c $(srcdir)/inferiors.c $(srcdir)/dll.c \
$(srcdir)/common/vec.c $(srcdir)/common/gdb_vecs.c \
$(srcdir)/common/common-utils.c $(srcdir)/common/xml-utils.c \
$(srcdir)/common/linux-osdata.c $(srcdir)/common/ptid.c \
- $(srcdir)/common/buffer.c
+ $(srcdir)/common/buffer.c $(srcdir)/common/filestuff.c
DEPFILES = @GDBSERVER_DEPFILES@
@@ -167,7 +167,7 @@ TAGFILES = $(SOURCES) ${HFILES} ${ALLPARAM} ${POSSLIBS}
OBS = agent.o ax.o inferiors.o regcache.o remote-utils.o server.o signals.o target.o \
utils.o version.o vec.o gdb_vecs.o \
mem-break.o hostio.o event-loop.o tracepoint.o \
- xml-utils.o common-utils.o ptid.o buffer.o format.o \
+ xml-utils.o common-utils.o ptid.o buffer.o format.o filestuff.o \
dll.o notif.o \
$(XML_BUILTIN) \
$(DEPFILES) $(LIBOBJS)
@@ -538,6 +538,9 @@ buffer.o: ../common/buffer.c
format.o: ../common/format.c
$(COMPILE) $<
$(POSTCOMPILE)
+filestuff.o: ../common/filestuff.c
+ $(COMPILE) $<
+ $(POSTCOMPILE)
agent.o: ../common/agent.c
$(COMPILE) $<
$(POSTCOMPILE)
diff --git a/gdb/gdbserver/config.in b/gdb/gdbserver/config.in
index b791b89..eb5cdc3 100644
--- a/gdb/gdbserver/config.in
+++ b/gdb/gdbserver/config.in
@@ -18,6 +18,9 @@
/* Define to 1 if you have the <arpa/inet.h> header file. */
#undef HAVE_ARPA_INET_H
+/* Define to 1 if you have the `closefrom' function. */
+#undef HAVE_CLOSEFROM
+
/* Define to 1 if you have the declaration of `ADDR_NO_RANDOMIZE', and to 0 if
you don't. */
#undef HAVE_DECL_ADDR_NO_RANDOMIZE
@@ -67,6 +70,9 @@
/* Define to 1 if you have the <fcntl.h> header file. */
#undef HAVE_FCNTL_H
+/* Define to 1 if you have the `fdwalk' function. */
+#undef HAVE_FDWALK
+
/* Define to 1 if you have the <inttypes.h> header file. */
#undef HAVE_INTTYPES_H
@@ -109,6 +115,9 @@
/* Define if you support the personality syscall. */
#undef HAVE_PERSONALITY
+/* Define to 1 if you have the `pipe2' function. */
+#undef HAVE_PIPE2
+
/* Define to 1 if you have the `pread' function. */
#undef HAVE_PREAD
diff --git a/gdb/gdbserver/configure b/gdb/gdbserver/configure
index 9ec9340..487a5f0 100755
--- a/gdb/gdbserver/configure
+++ b/gdb/gdbserver/configure
@@ -4596,7 +4596,7 @@ fi
done
-for ac_func in pread pwrite pread64 readlink
+for ac_func in pread pwrite pread64 readlink fdwalk closefrom pipe2
do :
as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
diff --git a/gdb/gdbserver/configure.ac b/gdb/gdbserver/configure.ac
index 6493c0f..7e3a4d3 100644
--- a/gdb/gdbserver/configure.ac
+++ b/gdb/gdbserver/configure.ac
@@ -69,7 +69,7 @@ AC_CHECK_HEADERS(sgtty.h termio.h termios.h sys/reg.h string.h dnl
sys/ioctl.h netinet/in.h sys/socket.h netdb.h dnl
netinet/tcp.h arpa/inet.h sys/wait.h wait.h sys/un.h dnl
linux/perf_event.h)
-AC_CHECK_FUNCS(pread pwrite pread64 readlink)
+AC_CHECK_FUNCS(pread pwrite pread64 readlink fdwalk closefrom pipe2)
AC_REPLACE_FUNCS(vasprintf vsnprintf)
# Check for UST
diff --git a/gdb/inf-child.c b/gdb/inf-child.c
index 3530e75..15eea00 100644
--- a/gdb/inf-child.c
+++ b/gdb/inf-child.c
@@ -31,6 +31,7 @@
#include "gdb/fileio.h"
#include "agent.h"
#include "gdb_wait.h"
+#include "filestuff.h"
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h> /* for MAXPATHLEN */
@@ -246,7 +247,7 @@ inf_child_fileio_open (const char *filename, int flags, int mode,
/* We do not need to convert MODE, since the fileio protocol uses
the standard values. */
- fd = open (filename, nat_flags, mode);
+ fd = gdb_open_cloexec (filename, nat_flags, mode);
if (fd == -1)
*target_errno = inf_child_errno_to_fileio_error (errno);
diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
index f5ca977..64b819d 100644
--- a/gdb/linux-nat.c
+++ b/gdb/linux-nat.c
@@ -67,6 +67,7 @@
#include "linux-ptrace.h"
#include "buffer.h"
#include "target-descriptions.h"
+#include "filestuff.h"
#ifndef SPUFS_MAGIC
#define SPUFS_MAGIC 0x23c9b64e
@@ -4278,7 +4279,7 @@ linux_nat_thread_name (struct thread_info *thr)
char *result = NULL;
snprintf (buf, sizeof (buf), FORMAT, pid, lwp);
- comm_file = fopen (buf, "r");
+ comm_file = gdb_fopen_cloexec (buf, "r");
if (comm_file)
{
/* Not exported by the kernel, so we define it here. */
@@ -4405,7 +4406,7 @@ linux_proc_xfer_partial (struct target_ops *ops, enum target_object object,
/* We could keep this file open and cache it - possibly one per
thread. That requires some juggling, but is even faster. */
sprintf (filename, "/proc/%d/mem", PIDGET (inferior_ptid));
- fd = open (filename, O_RDONLY | O_LARGEFILE);
+ fd = gdb_open_cloexec (filename, O_RDONLY | O_LARGEFILE, 0);
if (fd == -1)
return 0;
@@ -4499,7 +4500,7 @@ linux_proc_xfer_spu (struct target_ops *ops, enum target_object object,
}
xsnprintf (buf, sizeof buf, "/proc/%d/fd/%s", pid, annex);
- fd = open (buf, writebuf? O_WRONLY : O_RDONLY);
+ fd = gdb_open_cloexec (buf, writebuf? O_WRONLY : O_RDONLY, 0);
if (fd <= 0)
return -1;
@@ -4575,7 +4576,7 @@ linux_proc_pending_signals (int pid, sigset_t *pending,
sigemptyset (blocked);
sigemptyset (ignored);
sprintf (fname, "/proc/%d/status", pid);
- procfile = fopen (fname, "r");
+ procfile = gdb_fopen_cloexec (fname, "r");
if (procfile == NULL)
error (_("Could not open %s"), fname);
cleanup = make_cleanup_fclose (procfile);
@@ -4918,7 +4919,7 @@ linux_async_pipe (int enable)
if (enable)
{
- if (pipe (linux_nat_event_pipe) == -1)
+ if (gdb_pipe_cloexec (linux_nat_event_pipe) == -1)
internal_error (__FILE__, __LINE__,
"creating event pipe failed.");
diff --git a/gdb/remote-fileio.c b/gdb/remote-fileio.c
index 0e3d533..4a59e9e 100644
--- a/gdb/remote-fileio.c
+++ b/gdb/remote-fileio.c
@@ -31,6 +31,7 @@
#include "event-loop.h"
#include "target.h"
#include "filenames.h"
+#include "filestuff.h"
#include <fcntl.h>
#include <sys/time.h>
@@ -639,7 +640,7 @@ remote_fileio_func_open (char *buf)
}
remote_fio_no_longjmp = 1;
- fd = open (pathname, flags, mode);
+ fd = gdb_open_cloexec (pathname, flags, mode);
if (fd < 0)
{
remote_fileio_return_errno (-1);
diff --git a/gdb/remote.c b/gdb/remote.c
index a6a6227..5ae3752 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -44,6 +44,7 @@
#include "cli/cli-setshow.h"
#include "target-descriptions.h"
#include "gdb_bfd.h"
+#include "filestuff.h"
#include <ctype.h>
#include <sys/time.h>
@@ -9899,7 +9900,7 @@ remote_file_put (const char *local_file, const char *remote_file, int from_tty)
if (!remote_desc)
error (_("command can only be used with remote target"));
- file = fopen (local_file, "rb");
+ file = gdb_fopen_cloexec (local_file, "rb");
if (file == NULL)
perror_with_name (local_file);
back_to = make_cleanup_fclose (file);
@@ -9991,7 +9992,7 @@ remote_file_get (const char *remote_file, const char *local_file, int from_tty)
if (fd == -1)
remote_hostio_error (remote_errno);
- file = fopen (local_file, "wb");
+ file = gdb_fopen_cloexec (local_file, "wb");
if (file == NULL)
perror_with_name (local_file);
back_to = make_cleanup_fclose (file);
diff --git a/gdb/ser-pipe.c b/gdb/ser-pipe.c
index 4f20cad..bb75f84 100644
--- a/gdb/ser-pipe.c
+++ b/gdb/ser-pipe.c
@@ -30,6 +30,7 @@
#include <sys/time.h>
#include <fcntl.h>
#include "gdb_string.h"
+#include "filestuff.h"
#include <signal.h>
@@ -63,9 +64,9 @@ pipe_open (struct serial *scb, const char *name)
int err_pdes[2];
int pid;
- if (socketpair (AF_UNIX, SOCK_STREAM, 0, pdes) < 0)
+ if (gdb_socketpair_cloexec (AF_UNIX, SOCK_STREAM, 0, pdes) < 0)
return -1;
- if (socketpair (AF_UNIX, SOCK_STREAM, 0, err_pdes) < 0)
+ if (gdb_socketpair_cloexec (AF_UNIX, SOCK_STREAM, 0, err_pdes) < 0)
{
close (pdes[0]);
close (pdes[1]);
@@ -122,14 +123,8 @@ pipe_open (struct serial *scb, const char *name)
dup2 (err_pdes[1], STDERR_FILENO);
close (err_pdes[1]);
}
-#if 0
- /* close any stray FD's - FIXME - how? */
- /* POSIX.2 B.3.2.2 "popen() shall ensure that any streams
- from previous popen() calls that remain open in the
- parent process are closed in the new child process. */
- for (old = pidlist; old; old = old->next)
- close (fileno (old->fp)); /* Don't allow a flush. */
-#endif
+
+ closefrom (3);
execl ("/bin/sh", "sh", "-c", name, (char *) 0);
_exit (127);
}
@@ -201,7 +196,7 @@ gdb_pipe (int pdes[2])
return -1;
#else
- if (socketpair (AF_UNIX, SOCK_STREAM, 0, pdes) < 0)
+ if (gdb_socketpair_cloexec (AF_UNIX, SOCK_STREAM, 0, pdes) < 0)
return -1;
/* If we don't do this, GDB simply exits when the remote side
diff --git a/gdb/ser-tcp.c b/gdb/ser-tcp.c
index d96a8e5..3a2b242 100644
--- a/gdb/ser-tcp.c
+++ b/gdb/ser-tcp.c
@@ -25,6 +25,7 @@
#include "gdbcmd.h"
#include "cli/cli-decode.h"
#include "cli/cli-setshow.h"
+#include "filestuff.h"
#include <sys/types.h>
@@ -208,9 +209,9 @@ net_open (struct serial *scb, const char *name)
retry:
if (use_udp)
- scb->fd = socket (PF_INET, SOCK_DGRAM, 0);
+ scb->fd = gdb_socket_cloexec (PF_INET, SOCK_DGRAM, 0);
else
- scb->fd = socket (PF_INET, SOCK_STREAM, 0);
+ scb->fd = gdb_socket_cloexec (PF_INET, SOCK_STREAM, 0);
if (scb->fd == -1)
return -1;
diff --git a/gdb/ser-unix.c b/gdb/ser-unix.c
index 4aab7ca..6b90d2e 100644
--- a/gdb/ser-unix.c
+++ b/gdb/ser-unix.c
@@ -32,6 +32,7 @@
#include "gdb_select.h"
#include "gdb_string.h"
#include "gdbcmd.h"
+#include "filestuff.h"
#ifdef HAVE_TERMIOS
@@ -108,7 +109,7 @@ void _initialize_ser_hardwire (void);
static int
hardwire_open (struct serial *scb, const char *name)
{
- scb->fd = open (name, O_RDWR);
+ scb->fd = gdb_open_cloexec (name, O_RDWR, 0);
if (scb->fd < 0)
return -1;
diff --git a/gdb/solib.c b/gdb/solib.c
index db3842a..6cb7dfc 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -47,6 +47,7 @@
#include "interps.h"
#include "filesystem.h"
#include "gdb_bfd.h"
+#include "filestuff.h"
/* Architecture-specific operations. */
@@ -246,7 +247,7 @@ solib_find (char *in_pathname, int *fd)
}
/* Now see if we can open it. */
- found_file = open (temp_pathname, O_RDONLY | O_BINARY, 0);
+ found_file = gdb_open_cloexec (temp_pathname, O_RDONLY | O_BINARY, 0);
if (found_file < 0)
xfree (temp_pathname);
@@ -269,7 +270,7 @@ solib_find (char *in_pathname, int *fd)
in_pathname + 2, (char *) NULL);
xfree (drive);
- found_file = open (temp_pathname, O_RDONLY | O_BINARY, 0);
+ found_file = gdb_open_cloexec (temp_pathname, O_RDONLY | O_BINARY, 0);
if (found_file < 0)
{
xfree (temp_pathname);
@@ -284,7 +285,7 @@ solib_find (char *in_pathname, int *fd)
need_dir_separator ? SLASH_STRING : "",
in_pathname + 2, (char *) NULL);
- found_file = open (temp_pathname, O_RDONLY | O_BINARY, 0);
+ found_file = gdb_open_cloexec (temp_pathname, O_RDONLY | O_BINARY, 0);
if (found_file < 0)
xfree (temp_pathname);
}
diff --git a/gdb/source.c b/gdb/source.c
index 7e14fc6..d41fb49 100644
--- a/gdb/source.c
+++ b/gdb/source.c
@@ -27,6 +27,7 @@
#include "frame.h"
#include "value.h"
#include "gdb_assert.h"
+#include "filestuff.h"
#include <sys/types.h>
#include "gdb_string.h"
@@ -716,7 +717,7 @@ openp (const char *path, int opts, const char *string,
{
filename = alloca (strlen (string) + 1);
strcpy (filename, string);
- fd = open (filename, mode);
+ fd = gdb_open_cloexec (filename, mode, 0);
if (fd >= 0)
goto done;
}
@@ -814,7 +815,7 @@ openp (const char *path, int opts, const char *string,
if (is_regular_file (filename))
{
- fd = open (filename, mode);
+ fd = gdb_open_cloexec (filename, mode, 0);
if (fd >= 0)
break;
}
@@ -983,7 +984,7 @@ find_and_open_source (const char *filename,
*fullname = rewritten_fullname;
}
- result = open (*fullname, OPEN_MODE);
+ result = gdb_open_cloexec (*fullname, OPEN_MODE, 0);
if (result >= 0)
{
/* Call xfullpath here to be consistent with openp
diff --git a/gdb/tracepoint.c b/gdb/tracepoint.c
index f61ede7..6da7fc6 100644
--- a/gdb/tracepoint.c
+++ b/gdb/tracepoint.c
@@ -53,6 +53,7 @@
#include "exceptions.h"
#include "cli/cli-utils.h"
#include "probe.h"
+#include "filestuff.h"
/* readline include files */
#include "readline/readline.h"
@@ -3000,7 +3001,7 @@ trace_save (const char *filename, int target_does_save)
pathname = tilde_expand (filename);
cleanup = make_cleanup (xfree, pathname);
- fp = fopen (pathname, "wb");
+ fp = gdb_fopen_cloexec (pathname, "wb");
if (!fp)
error (_("Unable to open file '%s' for saving trace data (%s)"),
filename, safe_strerror (errno));
@@ -3721,7 +3722,7 @@ tfile_open (char *filename, int from_tty)
flags = O_BINARY | O_LARGEFILE;
flags |= O_RDONLY;
- scratch_chan = open (filename, flags, 0);
+ scratch_chan = gdb_open_cloexec (filename, flags, 0);
if (scratch_chan < 0)
perror_with_name (filename);
diff --git a/gdb/tui/tui-io.c b/gdb/tui/tui-io.c
index fe88f1a..11b8849 100644
--- a/gdb/tui/tui-io.c
+++ b/gdb/tui/tui-io.c
@@ -37,6 +37,7 @@
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
+#include "filestuff.h"
#include "gdb_curses.h"
@@ -616,7 +617,7 @@ tui_initialize_io (void)
/* Temporary solution for readline writing to stdout: redirect
readline output in a pipe, read that pipe and output the content
in the curses command window. */
- if (pipe (tui_readline_pipe) != 0)
+ if (gdb_pipe_cloexec (tui_readline_pipe) != 0)
{
fprintf_unfiltered (gdb_stderr, "Cannot create pipe for readline");
exit (1);
diff --git a/gdb/ui-file.c b/gdb/ui-file.c
index 68089e6..d432652 100644
--- a/gdb/ui-file.c
+++ b/gdb/ui-file.c
@@ -24,6 +24,7 @@
#include "gdb_obstack.h"
#include "gdb_string.h"
#include "gdb_select.h"
+#include "filestuff.h"
#include <errno.h>
@@ -627,7 +628,7 @@ stdio_fileopen (FILE *file)
struct ui_file *
gdb_fopen (char *name, char *mode)
{
- FILE *f = fopen (name, mode);
+ FILE *f = gdb_fopen_cloexec (name, mode);
if (f == NULL)
return NULL;
diff --git a/gdb/xml-support.c b/gdb/xml-support.c
index 884b716..90b9602 100644
--- a/gdb/xml-support.c
+++ b/gdb/xml-support.c
@@ -21,6 +21,7 @@
#include "gdbcmd.h"
#include "exceptions.h"
#include "xml-support.h"
+#include "filestuff.h"
#include "gdb_string.h"
#include "safe-ctype.h"
@@ -1044,11 +1045,11 @@ xml_fetch_content_from_file (const char *filename, void *baton)
if (fullname == NULL)
malloc_failure (0);
- file = fopen (fullname, FOPEN_RT);
+ file = gdb_fopen_cloexec (fullname, FOPEN_RT);
xfree (fullname);
}
else
- file = fopen (filename, FOPEN_RT);
+ file = gdb_fopen_cloexec (filename, FOPEN_RT);
if (file == NULL)
return NULL;
^ permalink raw reply [flat|nested] 13+ messages in thread* Re: RFC: close-on-exec internal file descriptors 2012-12-18 17:45 RFC: close-on-exec internal file descriptors Tom Tromey @ 2012-12-18 22:12 ` Andreas Schwab 2012-12-19 14:53 ` Tom Tromey 2013-04-23 15:45 ` Yao Qi 1 sibling, 1 reply; 13+ messages in thread From: Andreas Schwab @ 2012-12-18 22:12 UTC (permalink / raw) To: Tom Tromey; +Cc: gdb-patches Tom Tromey <tromey@redhat.com> writes: > @@ -314,6 +315,8 @@ fork_inferior (char *exec_file_arg, char *allargs, char **env, > > if (pid == 0) > { > + closefrom (3); > + I don't think you can use closefrom here. The debuggee might depend on inherited descriptors, and gdb should be fully transparent here. Andreas. -- Andreas Schwab, schwab@linux-m68k.org GPG Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5 "And now for something completely different." ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: RFC: close-on-exec internal file descriptors 2012-12-18 22:12 ` Andreas Schwab @ 2012-12-19 14:53 ` Tom Tromey 2013-01-03 16:17 ` Tom Tromey 0 siblings, 1 reply; 13+ messages in thread From: Tom Tromey @ 2012-12-19 14:53 UTC (permalink / raw) To: Andreas Schwab; +Cc: gdb-patches >>>>> "Andreas" == Andreas Schwab <schwab@linux-m68k.org> writes: Andreas> I don't think you can use closefrom here. The debuggee might Andreas> depend on inherited descriptors, and gdb should be fully Andreas> transparent here. I had never heard of anybody doing this. I'll give it a try. If it works I suppose we'll need a new plan. One idea would be to see which file descriptors are open when gdb starts, and then preserve just those. Tom ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: RFC: close-on-exec internal file descriptors 2012-12-19 14:53 ` Tom Tromey @ 2013-01-03 16:17 ` Tom Tromey 2013-04-22 23:55 ` Tom Tromey 0 siblings, 1 reply; 13+ messages in thread From: Tom Tromey @ 2013-01-03 16:17 UTC (permalink / raw) To: Andreas Schwab; +Cc: gdb-patches Tom> I'll give it a try. If it works I suppose we'll need a new plan. Tom> One idea would be to see which file descriptors are open when gdb Tom> starts, and then preserve just those. Here's the implementation of this idea. Built and regtested on x86-64 Fedora 16. Tom 2013-01-03 Tom Tromey <tromey@redhat.com> PR gdb/7912: * Makefile.in (SFILES): Add filestuff.c (COMMON_OBS): Add filestuff.o. (filestuff.o): New target. * auto-load.c (auto_load_objfile_script_1): Use gdb_fopen_cloexec. * auxv.c (procfs_xfer_auxv): Use gdb_open_cloexec. * cli/cli-cmds.c (shell_escape): Call close_most_fds. * cli/cli-dump.c (fopen_with_cleanup): Use gdb_fopen_cloexec. * common/agent.c (gdb_connect_sync_socket): Use gdb_socket_cloexec. * common/filestuff.c: New file. * common/filestuff.h: New file. * common/linux-osdata.c (linux_common_core_of_thread) (command_from_pid, commandline_from_pid, print_source_lines) (linux_xfer_osdata_shm, linux_xfer_osdata_sem) (linux_xfer_osdata_msg, linux_xfer_osdata_modules): Use gdb_fopen_cloexec. * common/linux-procfs.c (linux_proc_get_int) (linux_proc_pid_has_state): Use gdb_fopen_cloexec. * config.in, configure: Rebuild. * configure.ac: Don't check for sys/socket.h. Check for fdwalk, pipe2. * corelow.c (core_open): Use gdb_open_cloexec. * dwarf2read.c (write_psymtabs_to_index): Use gdb_fopen_cloexec. * fork-child.c (fork_inferior): Call close_most_fds. * gdb_bfd.c (gdb_bfd_open): Use gdb_open_cloexec. * inf-child.c (inf_child_fileio_readlink): Use gdb_open_cloexec. * linux-nat.c (linux_nat_thread_name, linux_proc_pending_signals): Use gdb_fopen_cloexec. (linux_proc_xfer_partial, linux_proc_xfer_spu): Use gdb_open_cloexec. (linux_async_pipe): Use gdb_pipe_cloexec. * remote-fileio.c (remote_fileio_func_open): Use gdb_open_cloexec. * remote.c (remote_file_put, remote_file_get): Use gdb_fopen_cloexec. * ser-pipe.c (pipe_open): Use gdb_socketpair_cloexec, close_most_fds. * ser-tcp.c (net_open): Use gdb_socket_cloexec. * ser-unix.c (hardwire_open): Use gdb_open_cloexec. * solib.c (solib_find): Use gdb_open_cloexec. * source.c (openp, find_and_open_source): Use gdb_open_cloexec. * tracepoint.c (trace_save): Use gdb_fopen_cloexec. (tfile_open): Use gdb_open_cloexec. * tui/tui-io.c (tui_initialize_io): Use gdb_pipe_cloexec. * ui-file.c (gdb_fopen): Use gdb_fopen_cloexec. * xml-support.c (xml_fetch_content_from_file): Use gdb_fopen_cloexec. * main.c (captured_main): Call notice_open_fds. 2012-12-18 Tom Tromey <tromey@redhat.com> * Makefile.in (SFILES): Add filestuff.c. (OBS): Add filestuff.o. (filestuff.o): New target. * config.in, configure: Rebuild. * configure.ac: Check for fdwalk, pipe2. diff --git a/gdb/Makefile.in b/gdb/Makefile.in index b065d41..9c8d96b 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -755,7 +755,7 @@ SFILES = ada-exp.y ada-lang.c ada-typeprint.c ada-valprint.c ada-tasks.c \ regset.c sol-thread.c windows-termcap.c \ common/gdb_vecs.c common/common-utils.c common/xml-utils.c \ common/ptid.c common/buffer.c gdb-dlfcn.c common/agent.c \ - common/format.c + common/format.c common/filestuff.c LINTFILES = $(SFILES) $(YYFILES) $(CONFIG_SRCS) init.c @@ -875,6 +875,7 @@ COMMON_OBS = $(DEPFILES) $(CONFIG_OBS) $(YYOBJ) \ expprint.o environ.o stack.o thread.o \ exceptions.o \ filesystem.o \ + filestuff.o \ inf-child.o \ interps.o \ minidebug.o \ @@ -1930,6 +1931,10 @@ buffer.o: ${srcdir}/common/buffer.c $(COMPILE) $(srcdir)/common/buffer.c $(POSTCOMPILE) +filestuff.o: $(srcdir)/common/filestuff.c + $(COMPILE) $(srcdir)/common/filestuff.c + $(POSTCOMPILE) + format.o: ${srcdir}/common/format.c $(COMPILE) $(srcdir)/common/format.c $(POSTCOMPILE) diff --git a/gdb/auto-load.c b/gdb/auto-load.c index 850c704..f4f9367 100644 --- a/gdb/auto-load.c +++ b/gdb/auto-load.c @@ -38,6 +38,7 @@ #include "observer.h" #include "fnmatch.h" #include "top.h" +#include "filestuff.h" /* The suffix of per-objfile scripts to auto-load as non-Python command files. E.g. When the program loads libfoo.so, look for libfoo-gdb.gdb. */ @@ -739,7 +740,7 @@ auto_load_objfile_script_1 (struct objfile *objfile, const char *realname, cleanups = make_cleanup (xfree, filename); - input = fopen (filename, "r"); + input = gdb_fopen_cloexec (filename, "r"); debugfile = filename; if (debug_auto_load) fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file \"%s\" %s.\n"), @@ -771,7 +772,7 @@ auto_load_objfile_script_1 (struct objfile *objfile, const char *realname, strcat (debugfile, filename); make_cleanup (xfree, debugfile); - input = fopen (debugfile, "r"); + input = gdb_fopen_cloexec (debugfile, "r"); if (debug_auto_load) fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file " "\"%s\" %s.\n"), diff --git a/gdb/auxv.c b/gdb/auxv.c index 3a63a65..879d7e1 100644 --- a/gdb/auxv.c +++ b/gdb/auxv.c @@ -26,6 +26,7 @@ #include "gdb_assert.h" #include "gdbcore.h" #include "observer.h" +#include "filestuff.h" #include "auxv.h" #include "elf/common.h" @@ -48,7 +49,7 @@ procfs_xfer_auxv (gdb_byte *readbuf, LONGEST n; pathname = xstrprintf ("/proc/%d/auxv", PIDGET (inferior_ptid)); - fd = open (pathname, writebuf != NULL ? O_WRONLY : O_RDONLY); + fd = gdb_open_cloexec (pathname, writebuf != NULL ? O_WRONLY : O_RDONLY, 0); xfree (pathname); if (fd < 0) return -1; diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c index 3a58c1b..3ead1b1 100644 --- a/gdb/cli/cli-cmds.c +++ b/gdb/cli/cli-cmds.c @@ -39,6 +39,7 @@ #include "source.h" #include "disasm.h" #include "tracepoint.h" +#include "filestuff.h" #include "ui-out.h" @@ -728,6 +729,8 @@ shell_escape (char *arg, int from_tty) { const char *p, *user_shell; + close_most_fds (); + if ((user_shell = (char *) getenv ("SHELL")) == NULL) user_shell = "/bin/sh"; diff --git a/gdb/cli/cli-dump.c b/gdb/cli/cli-dump.c index 1e59464..4a1f561 100644 --- a/gdb/cli/cli-dump.c +++ b/gdb/cli/cli-dump.c @@ -33,6 +33,7 @@ #include "gdbcore.h" #include "cli/cli-utils.h" #include "gdb_bfd.h" +#include "filestuff.h" #define XMALLOC(TYPE) ((TYPE*) xmalloc (sizeof (TYPE))) @@ -99,7 +100,7 @@ scan_filename_with_cleanup (char **cmd, const char *defname) FILE * fopen_with_cleanup (const char *filename, const char *mode) { - FILE *file = fopen (filename, mode); + FILE *file = gdb_fopen_cloexec (filename, mode); if (file == NULL) perror_with_name (filename); diff --git a/gdb/common/agent.c b/gdb/common/agent.c index 632310d..f37aae3 100644 --- a/gdb/common/agent.c +++ b/gdb/common/agent.c @@ -28,6 +28,7 @@ #include <string.h> #include <unistd.h> #include "agent.h" +#include "filestuff.h" int debug_agent = 0; @@ -168,7 +169,7 @@ gdb_connect_sync_socket (int pid) if (res >= UNIX_PATH_MAX) return -1; - res = fd = socket (PF_UNIX, SOCK_STREAM, 0); + res = fd = gdb_socket_cloexec (PF_UNIX, SOCK_STREAM, 0); if (res == -1) { warning (_("error opening sync socket: %s"), strerror (errno)); diff --git a/gdb/common/filestuff.c b/gdb/common/filestuff.c new file mode 100644 index 0000000..29efc10 --- /dev/null +++ b/gdb/common/filestuff.c @@ -0,0 +1,354 @@ +/* Low-level file-handling. + Copyright (C) 2012, 2013 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#ifdef GDBSERVER +#include "server.h" +#else +#include "defs.h" +#include "gdb_string.h" +#endif +#include "filestuff.h" +#include "gdb_vecs.h" + +#include <string.h> +#include <fcntl.h> +#include <unistd.h> +#include <sys/socket.h> +#include <sys/types.h> +#include <sys/stat.h> + +#ifdef HAVE_SYS_RESOURCE_H +#include <sys/resource.h> +#endif /* HAVE_SYS_RESOURCE_H */ + +#ifndef O_CLOEXEC +#define O_CLOEXEC 0 +#endif + +#ifndef SOCK_CLOEXEC +#define SOCK_CLOEXEC 0 +#endif + +\f + +#ifndef HAVE_FDWALK + +#include <dirent.h> + +/* Replacement for fdwalk, if the system doesn't define it. Walks all + open file descriptors (though this implementation may walk closed + ones as well, depending on the host platform's capabilities) and + call FUNC with ARG. If FUNC returns non-zero, stops immediately + and returns the same value. Otherwise, returns zero when + finished. */ + +static int +fdwalk (int (*func) (void *, int), void *arg) +{ + /* Checking __linux__ isn't great but it isn't clear what would be + better. There doesn't seem to be a good way to check for this in + configure. */ +#ifdef __linux__ + DIR *dir; + + dir = opendir ("/proc/self/fd"); + if (dir != NULL) + { + struct dirent *entry; + int result = 0; + + for (entry = readdir (dir); entry != NULL; entry = readdir (dir)) + { + long fd; + char *tail; + int result; + + errno = 0; + fd = strtol (entry->d_name, &tail, 10); + if (*tail != '\0' || errno != 0) + continue; + if ((int) fd != fd) + { + /* What can we do here really? */ + continue; + } + + if (fd == dirfd (dir)) + continue; + + result = func (arg, fd); + if (result != 0) + break; + } + + closedir (dir); + return result; + } + /* We may fall through to the next case. */ +#endif + + { + int max, fd; + +#ifdef HAVE_GETRLIMIT + struct rlimit rlim; + + if (getrlimit (RLIMIT_NOFILE, &rlim) == 0 && rlim.rlim_max != RLIM_INFINITY) + max = rlim.rlim_max; + else +#endif + { +#ifdef _SC_OPEN_MAX + max = sysconf (_SC_OPEN_MAX); +#else + /* Whoops. */ + return 0; +#endif /* _SC_OPEN_MAX */ + } + + for (fd = 0; fd < max; ++fd) + { + struct stat sb; + int result; + + /* Only call FUNC for open fds. */ + if (fstat (fd, &sb) == -1) + continue; + + result = func (arg, fd); + if (result != 0) + return result; + } + + return 0; + } +} + +#endif /* HAVE_FDWALK */ + +\f + +/* A VEC holding all the fds open when notice_open_fds was called. We + don't use a hashtab because libiberty isn't linked into gdbserver; + and anyway we don't expect there to be many open fds. */ + +DEF_VEC_I (int); + +static VEC (int) *open_fds; + +/* An fdwalk callback function used by notice_open_fds. It puts the + given file descriptor into the vec. */ + +static int +do_mark_open_fd (void *ignore, int fd) +{ + VEC_safe_push (int, open_fds, fd); + return 0; +} + +/* See filestuff.h. */ + +void +notice_open_fds (void) +{ + fdwalk (do_mark_open_fd, NULL); +} + +/* Helper function for close_most_fds that closes the file descriptor + if appropriate. */ + +static int +do_close (void *ignore, int fd) +{ + int i, val; + + for (i = 0; VEC_iterate (int, open_fds, i, val); ++i) + { + if (fd == val) + { + /* Keep this one open. */ + return 0; + } + } + + close (fd); + return 0; +} + +/* See filestuff.h. */ + +void +close_most_fds (void) +{ + fdwalk (do_close, NULL); +} + +\f + +/* This is a tri-state flag. When zero it means we haven't yet tried + O_CLOEXEC. When positive it means that O_CLOEXEC works on this + host. When negative, it means that O_CLOEXEC doesn't work. We + track this state because, while gdb might have been compiled + against a libc that supplies O_CLOEXEC, there is no guarantee that + the kernel supports it. */ + +static int trust_o_cloexec; + +/* Mark FD as close-on-exec, ignoring errors. Update + TRUST_O_CLOEXEC. */ + +static void +mark_cloexec (int fd) +{ + int old = fcntl (fd, F_GETFD, 0); + + if (old != -1) + { + fcntl (fd, F_SETFD, old | FD_CLOEXEC); + + if (trust_o_cloexec == 0) + { + if ((old & FD_CLOEXEC) != 0) + trust_o_cloexec = 1; + else + trust_o_cloexec = -1; + } + } +} + +/* Depending on TRUST_O_CLOEXEC, mark FD as close-on-exec. */ + +static void +maybe_mark_cloexec (int fd) +{ + if (trust_o_cloexec <= 0) + mark_cloexec (fd); +} + +/* Like maybe_mark_cloexec, but for callers that use SOCK_CLOEXEC. */ + +static void +socket_mark_cloexec (int fd) +{ + if (SOCK_CLOEXEC == 0 || trust_o_cloexec <= 0) + mark_cloexec (fd); +} + +\f + +/* See filestuff.h. */ + +int +gdb_open_cloexec (const char *filename, int flags, mode_t mode) +{ + int fd = open (filename, flags | O_CLOEXEC, mode); + + if (fd >= 0) + maybe_mark_cloexec (fd); + + return fd; +} + +/* See filestuff.h. */ + +FILE * +gdb_fopen_cloexec (const char *filename, const char *opentype) +{ + FILE *result; + static int fopen_e_ever_failed; + + if (!fopen_e_ever_failed) + { + char *copy; + + copy = alloca (strlen (opentype) + 2); + strcpy (copy, opentype); + /* This is a glibc extension but we try it unconditionally on + this path. */ + strcat (copy, "e"); + result = fopen (filename, copy); + } + + if (result == NULL) + { + /* Fallback. */ + result = fopen (filename, opentype); + if (result != NULL) + fopen_e_ever_failed = 1; + } + + if (result != NULL) + maybe_mark_cloexec (fileno (result)); + + return result; +} + +/* See filestuff.h. */ + +int +gdb_socketpair_cloexec (int namespace, int style, int protocol, int filedes[2]) +{ + int result = socketpair (namespace, style | SOCK_CLOEXEC, protocol, filedes); + + if (result != -1) + { + socket_mark_cloexec (filedes[0]); + socket_mark_cloexec (filedes[1]); + } + + return result; +} + +/* See filestuff.h. */ + +int +gdb_socket_cloexec (int namespace, int style, int protocol) +{ + int result = socket (namespace, style | SOCK_CLOEXEC, protocol); + + if (result != -1) + socket_mark_cloexec (result); + + return result; +} + +/* See filestuff.h. */ + +int +gdb_pipe_cloexec (int filedes[2]) +{ + int result; + +#ifdef HAVE_PIPE2 + result = pipe2 (filedes, O_CLOEXEC); + if (result != -1) + { + maybe_mark_cloexec (filedes[0]); + maybe_mark_cloexec (filedes[1]); + } +#else + result = pipe (filedes); + if (result != -1) + { + mark_cloexec (filedes[0]); + mark_cloexec (filedes[1]); + } +#endif + + return result; +} diff --git a/gdb/common/filestuff.h b/gdb/common/filestuff.h new file mode 100644 index 0000000..747bff2 --- /dev/null +++ b/gdb/common/filestuff.h @@ -0,0 +1,59 @@ +/* Low-level file-handling. + Copyright (C) 2012, 2013 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#ifndef FILESTUFF_H +#define FILESTUFF_H + +/* Note all the file descriptors which are open when this is called. + These file descriptors will not be closed by close_most_fds. */ + +extern void notice_open_fds (void); + +/* Close all open file descriptors other than those marked by + 'notice_open_fds', and stdin, stdout, and stderr. Errors that + occur while closing are ignored. */ + +extern void close_most_fds (void); + +/* Like 'open', but ensures that the returned file descriptor has the + close-on-exec flag set. */ + +extern int gdb_open_cloexec (const char *filename, int flags, mode_t mode); + +/* Like 'fopen', but ensures that the returned file descriptor has the + close-on-exec flag set. */ + +extern FILE *gdb_fopen_cloexec (const char *filename, const char *opentype); + +/* Like 'socketpair', but ensures that the returned file descriptors + have the close-on-exec flag set. */ + +extern int gdb_socketpair_cloexec (int namespace, int style, int protocol, + int filedes[2]); + +/* Like 'socket', but ensures that the returned file descriptor has + the close-on-exec flag set. */ + +extern int gdb_socket_cloexec (int namespace, int style, int protocol); + +/* Like 'pipe', but ensures that the returned file descriptors have + the close-on-exec flag set. */ + +extern int gdb_pipe_cloexec (int filedes[2]); + +#endif /* FILESTUFF_H */ diff --git a/gdb/common/linux-osdata.c b/gdb/common/linux-osdata.c index d55470b..9723839 100644 --- a/gdb/common/linux-osdata.c +++ b/gdb/common/linux-osdata.c @@ -44,6 +44,7 @@ #include "gdb_assert.h" #include "gdb_dirent.h" #include "gdb_stat.h" +#include "filestuff.h" /* Define PID_T to be a fixed size that is at least as large as pid_t, so that reading pid values embedded in /proc works @@ -76,7 +77,7 @@ linux_common_core_of_thread (ptid_t ptid) sprintf (filename, "/proc/%lld/task/%lld/stat", (PID_T) ptid_get_pid (ptid), (PID_T) ptid_get_lwp (ptid)); - f = fopen (filename, "r"); + f = gdb_fopen_cloexec (filename, "r"); if (!f) return -1; @@ -125,7 +126,7 @@ static void command_from_pid (char *command, int maxlen, PID_T pid) { char *stat_path = xstrprintf ("/proc/%lld/stat", pid); - FILE *fp = fopen (stat_path, "r"); + FILE *fp = gdb_fopen_cloexec (stat_path, "r"); command[0] = '\0'; @@ -165,7 +166,7 @@ commandline_from_pid (PID_T pid) { char *pathname = xstrprintf ("/proc/%lld/cmdline", pid); char *commandline = NULL; - FILE *f = fopen (pathname, "r"); + FILE *f = gdb_fopen_cloexec (pathname, "r"); if (f) { @@ -860,7 +861,7 @@ print_sockets (unsigned short family, int tcp, struct buffer *buffer) else return; - fp = fopen (proc_file, "r"); + fp = gdb_fopen_cloexec (proc_file, "r"); if (fp) { char buf[8192]; @@ -1088,7 +1089,7 @@ linux_xfer_osdata_shm (gdb_byte *readbuf, buffer_init (&buffer); buffer_grow_str (&buffer, "<osdata type=\"shared memory\">\n"); - fp = fopen ("/proc/sysvipc/shm", "r"); + fp = gdb_fopen_cloexec ("/proc/sysvipc/shm", "r"); if (fp) { char buf[8192]; @@ -1216,7 +1217,7 @@ linux_xfer_osdata_sem (gdb_byte *readbuf, buffer_init (&buffer); buffer_grow_str (&buffer, "<osdata type=\"semaphores\">\n"); - fp = fopen ("/proc/sysvipc/sem", "r"); + fp = gdb_fopen_cloexec ("/proc/sysvipc/sem", "r"); if (fp) { char buf[8192]; @@ -1328,7 +1329,7 @@ linux_xfer_osdata_msg (gdb_byte *readbuf, buffer_init (&buffer); buffer_grow_str (&buffer, "<osdata type=\"message queues\">\n"); - fp = fopen ("/proc/sysvipc/msg", "r"); + fp = gdb_fopen_cloexec ("/proc/sysvipc/msg", "r"); if (fp) { char buf[8192]; @@ -1454,7 +1455,7 @@ linux_xfer_osdata_modules (gdb_byte *readbuf, buffer_init (&buffer); buffer_grow_str (&buffer, "<osdata type=\"modules\">\n"); - fp = fopen ("/proc/modules", "r"); + fp = gdb_fopen_cloexec ("/proc/modules", "r"); if (fp) { char buf[8192]; diff --git a/gdb/common/linux-procfs.c b/gdb/common/linux-procfs.c index f5dccfd..583ec98 100644 --- a/gdb/common/linux-procfs.c +++ b/gdb/common/linux-procfs.c @@ -24,6 +24,7 @@ #endif #include "linux-procfs.h" +#include "filestuff.h" /* Return the TGID of LWPID from /proc/pid/status. Returns -1 if not found. */ @@ -37,7 +38,7 @@ linux_proc_get_int (pid_t lwpid, const char *field) int retval = -1; snprintf (buf, sizeof (buf), "/proc/%d/status", (int) lwpid); - status_file = fopen (buf, "r"); + status_file = gdb_fopen_cloexec (buf, "r"); if (status_file == NULL) { warning (_("unable to open /proc file '%s'"), buf); @@ -83,7 +84,7 @@ linux_proc_pid_has_state (pid_t pid, const char *state) int have_state; xsnprintf (buffer, sizeof (buffer), "/proc/%d/status", (int) pid); - procfile = fopen (buffer, "r"); + procfile = gdb_fopen_cloexec (buffer, "r"); if (procfile == NULL) { warning (_("unable to open /proc file '%s'"), buffer); diff --git a/gdb/config.in b/gdb/config.in index 9e21325..bda940e 100644 --- a/gdb/config.in +++ b/gdb/config.in @@ -141,6 +141,9 @@ /* Define to 1 if your system has the etext variable. */ #undef HAVE_ETEXT +/* Define to 1 if you have the `fdwalk' function. */ +#undef HAVE_FDWALK + /* Define to 1 if you have the `fork' function. */ #undef HAVE_FORK @@ -267,6 +270,9 @@ /* Define to 1 if you have the `pipe' function. */ #undef HAVE_PIPE +/* Define to 1 if you have the `pipe2' function. */ +#undef HAVE_PIPE2 + /* Define to 1 if you have the `poll' function. */ #undef HAVE_POLL @@ -500,9 +506,6 @@ /* Define to 1 if you have the <sys/select.h> header file. */ #undef HAVE_SYS_SELECT_H -/* Define to 1 if you have the <sys/socket.h> header file. */ -#undef HAVE_SYS_SOCKET_H - /* Define to 1 if you have the <sys/stat.h> header file. */ #undef HAVE_SYS_STAT_H diff --git a/gdb/configure b/gdb/configure index 6273193..061fb39 100755 --- a/gdb/configure +++ b/gdb/configure @@ -8893,7 +8893,7 @@ for ac_header in nlist.h machine/reg.h poll.h sys/poll.h proc_service.h \ sys/reg.h sys/debugreg.h sys/select.h sys/syscall.h \ sys/types.h sys/wait.h wait.h termios.h termio.h \ sgtty.h unistd.h elf_hp.h locale.h \ - dlfcn.h sys/socket.h sys/un.h linux/perf_event.h + dlfcn.h sys/un.h linux/perf_event.h do : as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh` ac_fn_c_check_header_mongrel "$LINENO" "$ac_header" "$as_ac_Header" "$ac_includes_default" @@ -10151,7 +10151,8 @@ for ac_func in canonicalize_file_name realpath getrusage getuid getgid \ sbrk setpgid setpgrp setsid \ sigaction sigprocmask sigsetmask socketpair syscall \ ttrace wborder wresize setlocale iconvlist libiconvlist btowc \ - setrlimit getrlimit posix_madvise waitpid lstat + setrlimit getrlimit posix_madvise waitpid lstat \ + fdwalk pipe2 do : as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh` ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var" diff --git a/gdb/configure.ac b/gdb/configure.ac index de096b8..9344c7e 100644 --- a/gdb/configure.ac +++ b/gdb/configure.ac @@ -1082,7 +1082,7 @@ AC_CHECK_HEADERS([nlist.h machine/reg.h poll.h sys/poll.h proc_service.h \ sys/reg.h sys/debugreg.h sys/select.h sys/syscall.h \ sys/types.h sys/wait.h wait.h termios.h termio.h \ sgtty.h unistd.h elf_hp.h locale.h \ - dlfcn.h sys/socket.h sys/un.h linux/perf_event.h]) + dlfcn.h sys/un.h linux/perf_event.h]) AC_CHECK_HEADERS(link.h, [], [], [#if HAVE_SYS_TYPES_H # include <sys/types.h> @@ -1164,7 +1164,8 @@ AC_CHECK_FUNCS([canonicalize_file_name realpath getrusage getuid getgid \ sbrk setpgid setpgrp setsid \ sigaction sigprocmask sigsetmask socketpair syscall \ ttrace wborder wresize setlocale iconvlist libiconvlist btowc \ - setrlimit getrlimit posix_madvise waitpid lstat]) + setrlimit getrlimit posix_madvise waitpid lstat \ + fdwalk pipe2]) AM_LANGINFO_CODESET # Check the return and argument types of ptrace. No canned test for diff --git a/gdb/corelow.c b/gdb/corelow.c index 121c846..fa244a5 100644 --- a/gdb/corelow.c +++ b/gdb/corelow.c @@ -46,6 +46,7 @@ #include "progspace.h" #include "objfiles.h" #include "gdb_bfd.h" +#include "filestuff.h" #ifndef O_LARGEFILE #define O_LARGEFILE 0 @@ -313,7 +314,7 @@ core_open (char *filename, int from_tty) flags |= O_RDWR; else flags |= O_RDONLY; - scratch_chan = open (filename, flags, 0); + scratch_chan = gdb_open_cloexec (filename, flags, 0); if (scratch_chan < 0) perror_with_name (filename); diff --git a/gdb/dwarf2read.c b/gdb/dwarf2read.c index 5a061f5..1f103810 100644 --- a/gdb/dwarf2read.c +++ b/gdb/dwarf2read.c @@ -67,6 +67,7 @@ #include <ctype.h> #include "gdb_bfd.h" #include "f-lang.h" +#include "filestuff.h" #include <fcntl.h> #include "gdb_string.h" @@ -20274,7 +20275,7 @@ write_psymtabs_to_index (struct objfile *objfile, const char *dir) INDEX_SUFFIX, (char *) NULL); cleanup = make_cleanup (xfree, filename); - out_file = fopen (filename, "wb"); + out_file = gdb_fopen_cloexec (filename, "wb"); if (!out_file) error (_("Can't open `%s' for writing"), filename); diff --git a/gdb/fork-child.c b/gdb/fork-child.c index 69a59cc..6820872 100644 --- a/gdb/fork-child.c +++ b/gdb/fork-child.c @@ -32,6 +32,7 @@ #include "command.h" /* for dont_repeat () */ #include "gdbcmd.h" #include "solib.h" +#include "filestuff.h" #include <signal.h> @@ -313,6 +314,8 @@ fork_inferior (char *exec_file_arg, char *allargs, char **env, if (pid == 0) { + close_most_fds (); + if (debug_fork) sleep (debug_fork); diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c index ae561d3..ebd9279 100644 --- a/gdb/gdb_bfd.c +++ b/gdb/gdb_bfd.c @@ -24,6 +24,7 @@ #include "ui-out.h" #include "gdbcmd.h" #include "hashtab.h" +#include "filestuff.h" #ifdef HAVE_ZLIB_H #include <zlib.h> #endif @@ -152,7 +153,7 @@ gdb_bfd_open (const char *name, const char *target, int fd) if (fd == -1) { - fd = open (name, O_RDONLY | O_BINARY); + fd = gdb_open_cloexec (name, O_RDONLY | O_BINARY, 0); if (fd == -1) { bfd_set_error (bfd_error_system_call); diff --git a/gdb/gdbserver/Makefile.in b/gdb/gdbserver/Makefile.in index f8b1794..24c5a1b 100644 --- a/gdb/gdbserver/Makefile.in +++ b/gdb/gdbserver/Makefile.in @@ -155,7 +155,7 @@ SFILES= $(srcdir)/gdbreplay.c $(srcdir)/inferiors.c $(srcdir)/dll.c \ $(srcdir)/common/vec.c $(srcdir)/common/gdb_vecs.c \ $(srcdir)/common/common-utils.c $(srcdir)/common/xml-utils.c \ $(srcdir)/common/linux-osdata.c $(srcdir)/common/ptid.c \ - $(srcdir)/common/buffer.c + $(srcdir)/common/buffer.c $(srcdir)/common/filestuff.c DEPFILES = @GDBSERVER_DEPFILES@ @@ -167,7 +167,7 @@ TAGFILES = $(SOURCES) ${HFILES} ${ALLPARAM} ${POSSLIBS} OBS = agent.o ax.o inferiors.o regcache.o remote-utils.o server.o signals.o target.o \ utils.o version.o vec.o gdb_vecs.o \ mem-break.o hostio.o event-loop.o tracepoint.o \ - xml-utils.o common-utils.o ptid.o buffer.o format.o \ + xml-utils.o common-utils.o ptid.o buffer.o format.o filestuff.o \ dll.o notif.o \ $(XML_BUILTIN) \ $(DEPFILES) $(LIBOBJS) @@ -538,6 +538,9 @@ buffer.o: ../common/buffer.c format.o: ../common/format.c $(COMPILE) $< $(POSTCOMPILE) +filestuff.o: ../common/filestuff.c + $(COMPILE) $< + $(POSTCOMPILE) agent.o: ../common/agent.c $(COMPILE) $< $(POSTCOMPILE) diff --git a/gdb/gdbserver/config.in b/gdb/gdbserver/config.in index b791b89..59c2bd4 100644 --- a/gdb/gdbserver/config.in +++ b/gdb/gdbserver/config.in @@ -67,6 +67,9 @@ /* Define to 1 if you have the <fcntl.h> header file. */ #undef HAVE_FCNTL_H +/* Define to 1 if you have the `fdwalk' function. */ +#undef HAVE_FDWALK + /* Define to 1 if you have the <inttypes.h> header file. */ #undef HAVE_INTTYPES_H @@ -109,6 +112,9 @@ /* Define if you support the personality syscall. */ #undef HAVE_PERSONALITY +/* Define to 1 if you have the `pipe2' function. */ +#undef HAVE_PIPE2 + /* Define to 1 if you have the `pread' function. */ #undef HAVE_PREAD diff --git a/gdb/gdbserver/configure b/gdb/gdbserver/configure index 9ec9340..730a01f 100755 --- a/gdb/gdbserver/configure +++ b/gdb/gdbserver/configure @@ -4596,7 +4596,7 @@ fi done -for ac_func in pread pwrite pread64 readlink +for ac_func in pread pwrite pread64 readlink fdwalk pipe2 do : as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh` ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var" diff --git a/gdb/gdbserver/configure.ac b/gdb/gdbserver/configure.ac index 55fb461..0e892f7 100644 --- a/gdb/gdbserver/configure.ac +++ b/gdb/gdbserver/configure.ac @@ -69,7 +69,7 @@ AC_CHECK_HEADERS(sgtty.h termio.h termios.h sys/reg.h string.h dnl sys/ioctl.h netinet/in.h sys/socket.h netdb.h dnl netinet/tcp.h arpa/inet.h sys/wait.h wait.h sys/un.h dnl linux/perf_event.h) -AC_CHECK_FUNCS(pread pwrite pread64 readlink) +AC_CHECK_FUNCS(pread pwrite pread64 readlink fdwalk pipe2) AC_REPLACE_FUNCS(vasprintf vsnprintf) # Check for UST diff --git a/gdb/inf-child.c b/gdb/inf-child.c index 5f34922..15d8613 100644 --- a/gdb/inf-child.c +++ b/gdb/inf-child.c @@ -30,6 +30,7 @@ #include "gdb/fileio.h" #include "agent.h" #include "gdb_wait.h" +#include "filestuff.h" #ifdef HAVE_SYS_PARAM_H #include <sys/param.h> /* for MAXPATHLEN */ @@ -245,7 +246,7 @@ inf_child_fileio_open (const char *filename, int flags, int mode, /* We do not need to convert MODE, since the fileio protocol uses the standard values. */ - fd = open (filename, nat_flags, mode); + fd = gdb_open_cloexec (filename, nat_flags, mode); if (fd == -1) *target_errno = inf_child_errno_to_fileio_error (errno); diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c index e9438b5..d74025d 100644 --- a/gdb/linux-nat.c +++ b/gdb/linux-nat.c @@ -67,6 +67,7 @@ #include "linux-ptrace.h" #include "buffer.h" #include "target-descriptions.h" +#include "filestuff.h" #ifndef SPUFS_MAGIC #define SPUFS_MAGIC 0x23c9b64e @@ -4278,7 +4279,7 @@ linux_nat_thread_name (struct thread_info *thr) char *result = NULL; snprintf (buf, sizeof (buf), FORMAT, pid, lwp); - comm_file = fopen (buf, "r"); + comm_file = gdb_fopen_cloexec (buf, "r"); if (comm_file) { /* Not exported by the kernel, so we define it here. */ @@ -4405,7 +4406,7 @@ linux_proc_xfer_partial (struct target_ops *ops, enum target_object object, /* We could keep this file open and cache it - possibly one per thread. That requires some juggling, but is even faster. */ sprintf (filename, "/proc/%d/mem", PIDGET (inferior_ptid)); - fd = open (filename, O_RDONLY | O_LARGEFILE); + fd = gdb_open_cloexec (filename, O_RDONLY | O_LARGEFILE, 0); if (fd == -1) return 0; @@ -4499,7 +4500,7 @@ linux_proc_xfer_spu (struct target_ops *ops, enum target_object object, } xsnprintf (buf, sizeof buf, "/proc/%d/fd/%s", pid, annex); - fd = open (buf, writebuf? O_WRONLY : O_RDONLY); + fd = gdb_open_cloexec (buf, writebuf? O_WRONLY : O_RDONLY, 0); if (fd <= 0) return -1; @@ -4575,7 +4576,7 @@ linux_proc_pending_signals (int pid, sigset_t *pending, sigemptyset (blocked); sigemptyset (ignored); sprintf (fname, "/proc/%d/status", pid); - procfile = fopen (fname, "r"); + procfile = gdb_fopen_cloexec (fname, "r"); if (procfile == NULL) error (_("Could not open %s"), fname); cleanup = make_cleanup_fclose (procfile); @@ -4918,7 +4919,7 @@ linux_async_pipe (int enable) if (enable) { - if (pipe (linux_nat_event_pipe) == -1) + if (gdb_pipe_cloexec (linux_nat_event_pipe) == -1) internal_error (__FILE__, __LINE__, "creating event pipe failed."); diff --git a/gdb/main.c b/gdb/main.c index 14893bd..a5f8007 100644 --- a/gdb/main.c +++ b/gdb/main.c @@ -42,6 +42,7 @@ #include "python/python.h" #include "objfiles.h" #include "auto-load.h" +#include "filestuff.h" /* The selected interpreter. This will be used as a set command variable, so it should always be malloc'ed - since @@ -357,6 +358,8 @@ captured_main (void *data) bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); + notice_open_fds (); + make_cleanup (VEC_cleanup (cmdarg_s), &cmdarg_vec); dirsize = 1; dirarg = (char **) xmalloc (dirsize * sizeof (*dirarg)); diff --git a/gdb/remote-fileio.c b/gdb/remote-fileio.c index 2f9d644..cc39bdf 100644 --- a/gdb/remote-fileio.c +++ b/gdb/remote-fileio.c @@ -31,6 +31,7 @@ #include "event-loop.h" #include "target.h" #include "filenames.h" +#include "filestuff.h" #include <fcntl.h> #include <sys/time.h> @@ -639,7 +640,7 @@ remote_fileio_func_open (char *buf) } remote_fio_no_longjmp = 1; - fd = open (pathname, flags, mode); + fd = gdb_open_cloexec (pathname, flags, mode); if (fd < 0) { remote_fileio_return_errno (-1); diff --git a/gdb/remote.c b/gdb/remote.c index 59b2eb6..730211f 100644 --- a/gdb/remote.c +++ b/gdb/remote.c @@ -44,6 +44,7 @@ #include "cli/cli-setshow.h" #include "target-descriptions.h" #include "gdb_bfd.h" +#include "filestuff.h" #include <ctype.h> #include <sys/time.h> @@ -9899,7 +9900,7 @@ remote_file_put (const char *local_file, const char *remote_file, int from_tty) if (!remote_desc) error (_("command can only be used with remote target")); - file = fopen (local_file, "rb"); + file = gdb_fopen_cloexec (local_file, "rb"); if (file == NULL) perror_with_name (local_file); back_to = make_cleanup_fclose (file); @@ -9991,7 +9992,7 @@ remote_file_get (const char *remote_file, const char *local_file, int from_tty) if (fd == -1) remote_hostio_error (remote_errno); - file = fopen (local_file, "wb"); + file = gdb_fopen_cloexec (local_file, "wb"); if (file == NULL) perror_with_name (local_file); back_to = make_cleanup_fclose (file); diff --git a/gdb/ser-pipe.c b/gdb/ser-pipe.c index 9a98923..1b30f78 100644 --- a/gdb/ser-pipe.c +++ b/gdb/ser-pipe.c @@ -30,6 +30,7 @@ #include <sys/time.h> #include <fcntl.h> #include "gdb_string.h" +#include "filestuff.h" #include <signal.h> @@ -63,9 +64,9 @@ pipe_open (struct serial *scb, const char *name) int err_pdes[2]; int pid; - if (socketpair (AF_UNIX, SOCK_STREAM, 0, pdes) < 0) + if (gdb_socketpair_cloexec (AF_UNIX, SOCK_STREAM, 0, pdes) < 0) return -1; - if (socketpair (AF_UNIX, SOCK_STREAM, 0, err_pdes) < 0) + if (gdb_socketpair_cloexec (AF_UNIX, SOCK_STREAM, 0, err_pdes) < 0) { close (pdes[0]); close (pdes[1]); @@ -122,14 +123,8 @@ pipe_open (struct serial *scb, const char *name) dup2 (err_pdes[1], STDERR_FILENO); close (err_pdes[1]); } -#if 0 - /* close any stray FD's - FIXME - how? */ - /* POSIX.2 B.3.2.2 "popen() shall ensure that any streams - from previous popen() calls that remain open in the - parent process are closed in the new child process. */ - for (old = pidlist; old; old = old->next) - close (fileno (old->fp)); /* Don't allow a flush. */ -#endif + + close_most_fds (); execl ("/bin/sh", "sh", "-c", name, (char *) 0); _exit (127); } @@ -201,7 +196,7 @@ gdb_pipe (int pdes[2]) return -1; #else - if (socketpair (AF_UNIX, SOCK_STREAM, 0, pdes) < 0) + if (gdb_socketpair_cloexec (AF_UNIX, SOCK_STREAM, 0, pdes) < 0) return -1; /* If we don't do this, GDB simply exits when the remote side diff --git a/gdb/ser-tcp.c b/gdb/ser-tcp.c index f9615d5..e621a3c 100644 --- a/gdb/ser-tcp.c +++ b/gdb/ser-tcp.c @@ -24,6 +24,7 @@ #include "gdbcmd.h" #include "cli/cli-decode.h" #include "cli/cli-setshow.h" +#include "filestuff.h" #include <sys/types.h> @@ -207,9 +208,9 @@ net_open (struct serial *scb, const char *name) retry: if (use_udp) - scb->fd = socket (PF_INET, SOCK_DGRAM, 0); + scb->fd = gdb_socket_cloexec (PF_INET, SOCK_DGRAM, 0); else - scb->fd = socket (PF_INET, SOCK_STREAM, 0); + scb->fd = gdb_socket_cloexec (PF_INET, SOCK_STREAM, 0); if (scb->fd == -1) return -1; diff --git a/gdb/ser-unix.c b/gdb/ser-unix.c index 9ec8bb1..cd1d9a9 100644 --- a/gdb/ser-unix.c +++ b/gdb/ser-unix.c @@ -31,6 +31,7 @@ #include "gdb_select.h" #include "gdb_string.h" #include "gdbcmd.h" +#include "filestuff.h" #ifdef HAVE_TERMIOS @@ -107,7 +108,7 @@ void _initialize_ser_hardwire (void); static int hardwire_open (struct serial *scb, const char *name) { - scb->fd = open (name, O_RDWR); + scb->fd = gdb_open_cloexec (name, O_RDWR, 0); if (scb->fd < 0) return -1; diff --git a/gdb/solib.c b/gdb/solib.c index 8129c0f..21919c2 100644 --- a/gdb/solib.c +++ b/gdb/solib.c @@ -47,6 +47,7 @@ #include "interps.h" #include "filesystem.h" #include "gdb_bfd.h" +#include "filestuff.h" /* Architecture-specific operations. */ @@ -246,7 +247,7 @@ solib_find (char *in_pathname, int *fd) } /* Now see if we can open it. */ - found_file = open (temp_pathname, O_RDONLY | O_BINARY, 0); + found_file = gdb_open_cloexec (temp_pathname, O_RDONLY | O_BINARY, 0); if (found_file < 0) xfree (temp_pathname); @@ -269,7 +270,7 @@ solib_find (char *in_pathname, int *fd) in_pathname + 2, (char *) NULL); xfree (drive); - found_file = open (temp_pathname, O_RDONLY | O_BINARY, 0); + found_file = gdb_open_cloexec (temp_pathname, O_RDONLY | O_BINARY, 0); if (found_file < 0) { xfree (temp_pathname); @@ -284,7 +285,7 @@ solib_find (char *in_pathname, int *fd) need_dir_separator ? SLASH_STRING : "", in_pathname + 2, (char *) NULL); - found_file = open (temp_pathname, O_RDONLY | O_BINARY, 0); + found_file = gdb_open_cloexec (temp_pathname, O_RDONLY | O_BINARY, 0); if (found_file < 0) xfree (temp_pathname); } diff --git a/gdb/source.c b/gdb/source.c index 6e12896..3e6aaa3 100644 --- a/gdb/source.c +++ b/gdb/source.c @@ -27,6 +27,7 @@ #include "frame.h" #include "value.h" #include "gdb_assert.h" +#include "filestuff.h" #include <sys/types.h> #include "gdb_string.h" @@ -716,7 +717,7 @@ openp (const char *path, int opts, const char *string, { filename = alloca (strlen (string) + 1); strcpy (filename, string); - fd = open (filename, mode); + fd = gdb_open_cloexec (filename, mode, 0); if (fd >= 0) goto done; } @@ -814,7 +815,7 @@ openp (const char *path, int opts, const char *string, if (is_regular_file (filename)) { - fd = open (filename, mode); + fd = gdb_open_cloexec (filename, mode, 0); if (fd >= 0) break; } @@ -983,7 +984,7 @@ find_and_open_source (const char *filename, *fullname = rewritten_fullname; } - result = open (*fullname, OPEN_MODE); + result = gdb_open_cloexec (*fullname, OPEN_MODE, 0); if (result >= 0) { /* Call xfullpath here to be consistent with openp diff --git a/gdb/tracepoint.c b/gdb/tracepoint.c index 53a4988..0254da1 100644 --- a/gdb/tracepoint.c +++ b/gdb/tracepoint.c @@ -53,6 +53,7 @@ #include "exceptions.h" #include "cli/cli-utils.h" #include "probe.h" +#include "filestuff.h" /* readline include files */ #include "readline/readline.h" @@ -2999,7 +3000,7 @@ trace_save (const char *filename, int target_does_save) pathname = tilde_expand (filename); cleanup = make_cleanup (xfree, pathname); - fp = fopen (pathname, "wb"); + fp = gdb_fopen_cloexec (pathname, "wb"); if (!fp) error (_("Unable to open file '%s' for saving trace data (%s)"), filename, safe_strerror (errno)); @@ -3720,7 +3721,7 @@ tfile_open (char *filename, int from_tty) flags = O_BINARY | O_LARGEFILE; flags |= O_RDONLY; - scratch_chan = open (filename, flags, 0); + scratch_chan = gdb_open_cloexec (filename, flags, 0); if (scratch_chan < 0) perror_with_name (filename); diff --git a/gdb/tui/tui-io.c b/gdb/tui/tui-io.c index ef658f5..f255fdf 100644 --- a/gdb/tui/tui-io.c +++ b/gdb/tui/tui-io.c @@ -37,6 +37,7 @@ #include <fcntl.h> #include <signal.h> #include <stdio.h> +#include "filestuff.h" #include "gdb_curses.h" @@ -616,7 +617,7 @@ tui_initialize_io (void) /* Temporary solution for readline writing to stdout: redirect readline output in a pipe, read that pipe and output the content in the curses command window. */ - if (pipe (tui_readline_pipe) != 0) + if (gdb_pipe_cloexec (tui_readline_pipe) != 0) { fprintf_unfiltered (gdb_stderr, "Cannot create pipe for readline"); exit (1); diff --git a/gdb/ui-file.c b/gdb/ui-file.c index bb904c9..5671abd 100644 --- a/gdb/ui-file.c +++ b/gdb/ui-file.c @@ -24,6 +24,7 @@ #include "gdb_obstack.h" #include "gdb_string.h" #include "gdb_select.h" +#include "filestuff.h" #include <errno.h> @@ -664,7 +665,7 @@ stdio_fileopen (FILE *file) struct ui_file * gdb_fopen (char *name, char *mode) { - FILE *f = fopen (name, mode); + FILE *f = gdb_fopen_cloexec (name, mode); if (f == NULL) return NULL; diff --git a/gdb/xml-support.c b/gdb/xml-support.c index 7ace5b9..b777814 100644 --- a/gdb/xml-support.c +++ b/gdb/xml-support.c @@ -21,6 +21,7 @@ #include "gdbcmd.h" #include "exceptions.h" #include "xml-support.h" +#include "filestuff.h" #include "gdb_string.h" #include "safe-ctype.h" @@ -1044,11 +1045,11 @@ xml_fetch_content_from_file (const char *filename, void *baton) if (fullname == NULL) malloc_failure (0); - file = fopen (fullname, FOPEN_RT); + file = gdb_fopen_cloexec (fullname, FOPEN_RT); xfree (fullname); } else - file = fopen (filename, FOPEN_RT); + file = gdb_fopen_cloexec (filename, FOPEN_RT); if (file == NULL) return NULL; ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: RFC: close-on-exec internal file descriptors 2013-01-03 16:17 ` Tom Tromey @ 2013-04-22 23:55 ` Tom Tromey 2013-04-23 1:51 ` Tom Tromey 2013-05-01 14:47 ` Joel Brobecker 0 siblings, 2 replies; 13+ messages in thread From: Tom Tromey @ 2013-04-22 23:55 UTC (permalink / raw) To: Andreas Schwab; +Cc: gdb-patches >>>>> "Tom" == Tom Tromey <tromey@redhat.com> writes: Tom> I'll give it a try. If it works I suppose we'll need a new plan. Tom> One idea would be to see which file descriptors are open when gdb Tom> starts, and then preserve just those. Tom> Here's the implementation of this idea. Tom> Built and regtested on x86-64 Fedora 16. Tom> Tom Tom> 2013-01-03 Tom Tromey <tromey@redhat.com> Tom> PR gdb/7912: Tom> * Makefile.in (SFILES): Add filestuff.c Tom> (COMMON_OBS): Add filestuff.o. Tom> (filestuff.o): New target. [...] I'm checking this in now. I rebased it and then regression tested it on x86-64 Fedora 18. Tom ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: RFC: close-on-exec internal file descriptors 2013-04-22 23:55 ` Tom Tromey @ 2013-04-23 1:51 ` Tom Tromey 2013-05-01 14:47 ` Joel Brobecker 1 sibling, 0 replies; 13+ messages in thread From: Tom Tromey @ 2013-04-23 1:51 UTC (permalink / raw) To: gdb-patches Tom> I rebased it and then regression tested it on x86-64 Fedora 18. ... and somehow it still regressed selftest.exp. I don't understand how since I remember re-running the test suite and running my comparison script. Anyhow, here is the obvious fix. I am checking it in. Tom 2013-04-22 Tom Tromey <tromey@redhat.com> * gdb.gdb/selftest.exp (do_steps_and_nexts): Check for notice_open_fds. Index: testsuite/gdb.gdb/selftest.exp =================================================================== RCS file: /cvs/src/src/gdb/testsuite/gdb.gdb/selftest.exp,v retrieving revision 1.33 diff -u -r1.33 selftest.exp --- testsuite/gdb.gdb/selftest.exp 22 Jan 2013 15:47:54 -0000 1.33 +++ testsuite/gdb.gdb/selftest.exp 22 Apr 2013 20:16:37 -0000 @@ -154,6 +154,10 @@ set description "next over bfd_init" set command "next" } + -re ".*notice_open_fds ..;.*$gdb_prompt $" { + set description "next over notice_open_fds" + set command "next" + } -re ".*VEC_cleanup .cmdarg_s.*$gdb_prompt $" { set description "next over cmdarg_s VEC_cleanup" set command "next" ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: RFC: close-on-exec internal file descriptors 2013-04-22 23:55 ` Tom Tromey 2013-04-23 1:51 ` Tom Tromey @ 2013-05-01 14:47 ` Joel Brobecker 2013-05-06 18:53 ` Tom Tromey 1 sibling, 1 reply; 13+ messages in thread From: Joel Brobecker @ 2013-05-01 14:47 UTC (permalink / raw) To: Tom Tromey; +Cc: gdb-patches Hi Tom, > Tom> 2013-01-03 Tom Tromey <tromey@redhat.com> > > Tom> PR gdb/7912: > Tom> * Makefile.in (SFILES): Add filestuff.c > Tom> (COMMON_OBS): Add filestuff.o. > Tom> (filestuff.o): New target. > [...] Unfortunately, this is causing problems on Darwin and HP/UX, because both platforms use pipes to synchronize parent and child during the fork/exec. See for instance darwin-nat.c: /* The child must synchronize with gdb: gdb must set the exception port before the child call PTRACE_SIGEXC. We use a pipe to achieve this. FIXME: is there a lighter way ? */ static int ptrace_fds[2]; Same mechanism, and therefore problem, with HP/UX targets: /* File descriptors for pipes used as semaphores during initial startup of an inferior. */ static int inf_ttrace_pfd1[2]; static int inf_ttrace_pfd2[2]; In terms of behavior, GDB either hangs during process creation, or reports a fatal failure during one of the system calls. I don't think there is a way of opening a FD with the intention of protecting them from the close-at-exec, is there? So, the solution would seem to add an interface to setup the protection, as well as a function to remove that protection (necessary when we close the fd). What do you think? -- Joel ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: RFC: close-on-exec internal file descriptors 2013-05-01 14:47 ` Joel Brobecker @ 2013-05-06 18:53 ` Tom Tromey 2013-05-07 6:46 ` Joel Brobecker 0 siblings, 1 reply; 13+ messages in thread From: Tom Tromey @ 2013-05-06 18:53 UTC (permalink / raw) To: Joel Brobecker; +Cc: gdb-patches >>>>> "Joel" == Joel Brobecker <brobecker@adacore.com> writes: Joel> Unfortunately, this is causing problems on Darwin and HP/UX, Joel> because both platforms use pipes to synchronize parent and Joel> child during the fork/exec. See for instance darwin-nat.c: Sorry Joel. I did not even consider this possibility. Joel> I don't think there is a way of opening a FD with the intention Joel> of protecting them from the close-at-exec, is there? So, the solution Joel> would seem to add an interface to setup the protection, as well Joel> as a function to remove that protection (necessary when we close Joel> the fd). Joel> What do you think? Could you try the appended? I can't even build parts of it, so... I think it is pretty ugly, since it is a layer on top of the kernel-level cloexec flags. However, that is another discussion, from the earlier reviews. Tom diff --git a/gdb/common/filestuff.c b/gdb/common/filestuff.c index 68f66ca..e7af3a5 100644 --- a/gdb/common/filestuff.c +++ b/gdb/common/filestuff.c @@ -177,6 +177,33 @@ notice_open_fds (void) fdwalk (do_mark_open_fd, NULL); } +/* See filestuff.h. */ + +void +mark_fd_no_cloexec (int fd) +{ + do_mark_open_fd (NULL, fd); +} + +/* See filestuff.h. */ + +void +unmark_fd_no_cloexec (int fd) +{ + int i, val; + + for (i = 0; VEC_iterate (int, open_fds, i, val); ++i) + { + if (fd == val) + { + VEC_unordered_remove (int, open_fds, i); + return; + } + } + + gdb_assert_not_reached (_("fd not found in open_fds")); +} + /* Helper function for close_most_fds that closes the file descriptor if appropriate. */ diff --git a/gdb/common/filestuff.h b/gdb/common/filestuff.h index 0db33f0..b162a0c 100644 --- a/gdb/common/filestuff.h +++ b/gdb/common/filestuff.h @@ -24,6 +24,16 @@ extern void notice_open_fds (void); +/* Mark a file descriptor as inheritable across an exec. */ + +extern void mark_fd_no_cloexec (int fd); + +/* Mark a file descriptor as no longer being inheritable across an + exec. This is only meaningful when FD was previously passed to + mark_fd_no_cloexec. */ + +extern void unmark_fd_no_cloexec (int fd); + /* Close all open file descriptors other than those marked by 'notice_open_fds', and stdin, stdout, and stderr. Errors that occur while closing are ignored. */ diff --git a/gdb/darwin-nat.c b/gdb/darwin-nat.c index acdbf36..b7a8b35 100644 --- a/gdb/darwin-nat.c +++ b/gdb/darwin-nat.c @@ -1516,6 +1516,9 @@ darwin_pre_ptrace (void) ptrace_fds[1] = -1; error (_("unable to create a pipe: %s"), safe_strerror (errno)); } + + mark_fd_no_cloexec (ptrace_fds[0]); + mark_fd_no_cloexec (ptrace_fds[1]); } static void @@ -1533,6 +1536,9 @@ darwin_ptrace_him (int pid) close (ptrace_fds[0]); close (ptrace_fds[1]); + unmark_fd_no_cloexec (ptrace_fds[0]); + unmark_fd_no_cloexec (ptrace_fds[1]); + darwin_init_thread_list (inf); startup_inferior (START_INFERIOR_TRAPS_EXPECTED); diff --git a/gdb/inf-ttrace.c b/gdb/inf-ttrace.c index 642e520..f39df49 100644 --- a/gdb/inf-ttrace.c +++ b/gdb/inf-ttrace.c @@ -558,6 +558,11 @@ do_cleanup_pfds (void *dummy) close (inf_ttrace_pfd1[1]); close (inf_ttrace_pfd2[0]); close (inf_ttrace_pfd2[1]); + + unmark_fd_no_cloexec (inf_ttrace_pfd1[0]); + unmark_fd_no_cloexec (inf_ttrace_pfd1[1]); + unmark_fd_no_cloexec (inf_ttrace_pfd2[0]); + unmark_fd_no_cloexec (inf_ttrace_pfd2[1]); } static void @@ -572,6 +577,11 @@ inf_ttrace_prepare (void) close (inf_ttrace_pfd2[0]); perror_with_name (("pipe")); } + + mark_fd_no_cloexec (inf_ttrace_pfd1[0]); + mark_fd_no_cloexec (inf_ttrace_pfd1[1]); + mark_fd_no_cloexec (inf_ttrace_pfd2[0]); + mark_fd_no_cloexec (inf_ttrace_pfd2[1]); } /* Prepare to be traced. */ ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: RFC: close-on-exec internal file descriptors 2013-05-06 18:53 ` Tom Tromey @ 2013-05-07 6:46 ` Joel Brobecker 2013-05-10 17:00 ` Tom Tromey 0 siblings, 1 reply; 13+ messages in thread From: Joel Brobecker @ 2013-05-07 6:46 UTC (permalink / raw) To: Tom Tromey; +Cc: gdb-patches [-- Attachment #1: Type: text/plain, Size: 597 bytes --] > Sorry Joel. > I did not even consider this possibility. No worries at all! > Could you try the appended? I can't even build parts of it, so... > > I think it is pretty ugly, since it is a layer on top of the > kernel-level cloexec flags. However, that is another discussion, from > the earlier reviews. Yeah, hard to see an alternative based on the decisions made. Seems OK to me. I tested the patches, and they look good to me, except for the fact that we were missing the #include of filestuff.h. Attached is patch that builds and tests fine on x86_64-darwin and ia64-hpux... -- Joel [-- Attachment #2: 0001-Do-not-close-pipes-used-to-communicate-with-child-du.patch --] [-- Type: text/x-diff, Size: 3700 bytes --] From 7118b53028599be9b5d2957bd1676c48ca87970b Mon Sep 17 00:00:00 2001 From: Joel Brobecker <brobecker@adacore.com> Date: Tue, 7 May 2013 02:33:02 -0400 Subject: [PATCH] Do not close pipes used to communicate with child during inferior fork --- gdb/common/filestuff.c | 27 +++++++++++++++++++++++++++ gdb/common/filestuff.h | 10 ++++++++++ gdb/darwin-nat.c | 7 +++++++ gdb/inf-ttrace.c | 11 +++++++++++ 4 files changed, 55 insertions(+), 0 deletions(-) diff --git a/gdb/common/filestuff.c b/gdb/common/filestuff.c index 68f66ca..e7af3a5 100644 --- a/gdb/common/filestuff.c +++ b/gdb/common/filestuff.c @@ -177,6 +177,33 @@ notice_open_fds (void) fdwalk (do_mark_open_fd, NULL); } +/* See filestuff.h. */ + +void +mark_fd_no_cloexec (int fd) +{ + do_mark_open_fd (NULL, fd); +} + +/* See filestuff.h. */ + +void +unmark_fd_no_cloexec (int fd) +{ + int i, val; + + for (i = 0; VEC_iterate (int, open_fds, i, val); ++i) + { + if (fd == val) + { + VEC_unordered_remove (int, open_fds, i); + return; + } + } + + gdb_assert_not_reached (_("fd not found in open_fds")); +} + /* Helper function for close_most_fds that closes the file descriptor if appropriate. */ diff --git a/gdb/common/filestuff.h b/gdb/common/filestuff.h index 0db33f0..b162a0c 100644 --- a/gdb/common/filestuff.h +++ b/gdb/common/filestuff.h @@ -24,6 +24,16 @@ extern void notice_open_fds (void); +/* Mark a file descriptor as inheritable across an exec. */ + +extern void mark_fd_no_cloexec (int fd); + +/* Mark a file descriptor as no longer being inheritable across an + exec. This is only meaningful when FD was previously passed to + mark_fd_no_cloexec. */ + +extern void unmark_fd_no_cloexec (int fd); + /* Close all open file descriptors other than those marked by 'notice_open_fds', and stdin, stdout, and stderr. Errors that occur while closing are ignored. */ diff --git a/gdb/darwin-nat.c b/gdb/darwin-nat.c index acdbf36..a2da924 100644 --- a/gdb/darwin-nat.c +++ b/gdb/darwin-nat.c @@ -66,6 +66,7 @@ #include <mach/port.h> #include "darwin-nat.h" +#include "common/filestuff.h" /* Quick overview. Darwin kernel is Mach + BSD derived kernel. Note that they share the @@ -1516,6 +1517,9 @@ darwin_pre_ptrace (void) ptrace_fds[1] = -1; error (_("unable to create a pipe: %s"), safe_strerror (errno)); } + + mark_fd_no_cloexec (ptrace_fds[0]); + mark_fd_no_cloexec (ptrace_fds[1]); } static void @@ -1533,6 +1537,9 @@ darwin_ptrace_him (int pid) close (ptrace_fds[0]); close (ptrace_fds[1]); + unmark_fd_no_cloexec (ptrace_fds[0]); + unmark_fd_no_cloexec (ptrace_fds[1]); + darwin_init_thread_list (inf); startup_inferior (START_INFERIOR_TRAPS_EXPECTED); diff --git a/gdb/inf-ttrace.c b/gdb/inf-ttrace.c index 96b473d..2ecf482 100644 --- a/gdb/inf-ttrace.c +++ b/gdb/inf-ttrace.c @@ -38,6 +38,7 @@ #include "inf-child.h" #include "inf-ttrace.h" +#include "common/filestuff.h" \f @@ -558,6 +559,11 @@ do_cleanup_pfds (void *dummy) close (inf_ttrace_pfd1[1]); close (inf_ttrace_pfd2[0]); close (inf_ttrace_pfd2[1]); + + unmark_fd_no_cloexec (inf_ttrace_pfd1[0]); + unmark_fd_no_cloexec (inf_ttrace_pfd1[1]); + unmark_fd_no_cloexec (inf_ttrace_pfd2[0]); + unmark_fd_no_cloexec (inf_ttrace_pfd2[1]); } static void @@ -572,6 +578,11 @@ inf_ttrace_prepare (void) close (inf_ttrace_pfd2[0]); perror_with_name (("pipe")); } + + mark_fd_no_cloexec (inf_ttrace_pfd1[0]); + mark_fd_no_cloexec (inf_ttrace_pfd1[1]); + mark_fd_no_cloexec (inf_ttrace_pfd2[0]); + mark_fd_no_cloexec (inf_ttrace_pfd2[1]); } /* Prepare to be traced. */ -- 1.7.0.4 ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: RFC: close-on-exec internal file descriptors 2013-05-07 6:46 ` Joel Brobecker @ 2013-05-10 17:00 ` Tom Tromey 0 siblings, 0 replies; 13+ messages in thread From: Tom Tromey @ 2013-05-10 17:00 UTC (permalink / raw) To: Joel Brobecker; +Cc: gdb-patches >>>>> "Joel" == Joel Brobecker <brobecker@adacore.com> writes: Joel> I tested the patches, and they look good to me, except for the fact Joel> that we were missing the #include of filestuff.h. Attached is patch Joel> that builds and tests fine on x86_64-darwin and ia64-hpux... Thanks Joel. I'm checking this in with this ChangeLog: 2013-05-10 Joel Brobecker <brobecker@adacore.com> Tom Tromey <tromey@redhat.com> * common/filestuff.c (mark_fd_no_cloexec, unmark_fd_no_cloexec): New functions. * common/filestuff.c (mark_fd_no_cloexec, unmark_fd_no_cloexec): Declare. * darwin-nat.c (darwin_pre_ptrace): Use mark_fd_no_cloexec. (darwin_ptrace_him): Use unmark_fd_no_cloexec. * inf-ttrace.c (do_cleanup_pfds): Use unmark_fd_no_cloexec. (inf_ttrace_prepare): Use mark_fd_no_cloexec. Tom ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: RFC: close-on-exec internal file descriptors 2012-12-18 17:45 RFC: close-on-exec internal file descriptors Tom Tromey 2012-12-18 22:12 ` Andreas Schwab @ 2013-04-23 15:45 ` Yao Qi 2013-04-24 2:45 ` Tom Tromey 1 sibling, 1 reply; 13+ messages in thread From: Yao Qi @ 2013-04-23 15:45 UTC (permalink / raw) To: Tom Tromey; +Cc: gdb-patches Tom, This patch causes a build regression with mingw32 cross gcc, reported here http://sourceware.org/ml/gdb-patches/2013-04/msg00682.html and I can reproduce it on my fedora 16: i686-pc-mingw32-gcc -g -O2 -D__USE_MINGW_ACCESS -I. -I../../../git/gdb -I../../../git/gdb/common -I../../../git/gdb/config -DLOCALEDIR="\"/usr/local/share/locale\"" -DHAVE_CONFIG_H -I../../../git/gdb/../include/opcode -I../../../git/gdb/../opcodes/.. -I../../../git/gdb/../readline/.. -I../bfd -I../../../git/gdb/../bfd -I../../../git/gdb/../include -I../libdecnumber -I../../../git/gdb/../libdecnumber -I./../intl -I../../../git/gdb/gnulib/import -Ibuild-gnulib/import -Wall -Wdeclaration-after-statement -Wpointer-arith -Wformat-nonliteral -Wpointer-sign -Wno-unused -Wunused-value -Wunused-function -Wno-switch -Wno-char-subscripts -Wmissing-prototypes -Wdeclaration-after-statement -Wempty-body -Wno-format -Werror -c -o filestuff.o -MT filestuff.o -MMD -MP -MF .deps/filestuff.Tpo ../../../git/gdb/common/filestuff.c ../../../git/gdb/common/filestuff.c:31:24: fatal error: sys/socket.h: No such file or directory On 12/19/2012 01:44 AM, Tom Tromey wrote: > * configure.ac: Don't check for sys/socket.h. Check for > fdwalk, closefrom, pipe2. Your patch doesn't mention much on why not to check sys/socket.h. Probably we still need to check it. -- Yao (é½å°§) ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: RFC: close-on-exec internal file descriptors 2013-04-23 15:45 ` Yao Qi @ 2013-04-24 2:45 ` Tom Tromey 2013-04-24 20:35 ` Jan Kratochvil 0 siblings, 1 reply; 13+ messages in thread From: Tom Tromey @ 2013-04-24 2:45 UTC (permalink / raw) To: Yao Qi; +Cc: gdb-patches >>>>> "Yao" == Yao Qi <yao@codesourcery.com> writes: Yao> This patch causes a build regression with mingw32 cross gcc, reported Yao> here http://sourceware.org/ml/gdb-patches/2013-04/msg00682.html and I Yao> can reproduce it on my fedora 16: Thanks for the note. I'm Sorry about the breakage. Yao> Your patch doesn't mention much on why not to check Yao> sys/socket.h. Probably we still need to check it. I looked and other places use USE_WIN32API. I am checking in the appended. It fixes the mingw build for me using the mingw cross compilers that come with Fedora 18. I also built and regression tested this natively. I did consider gnulib but it seems we already have a reasonably working solution; and using it just for mode_t (see the patch) seemed like overkill. Tom 2013-04-23 Tom Tromey <tromey@redhat.com> * common/filestuff.c: Check USE_WIN32API before including sys/socket.h. (HAVE_F_GETFD): New define. (mark_cloexec): Check HAVE_F_GETFD. (gdb_open_cloexec): Change 'mode' to unsigned long. (gdb_socketpair_cloexec): Check HAVE_SOCKETPAIR. (gdb_pipe_cloexec): Check HAVE_PIPE. * common/filestuff.h (gdb_open_cloexec): Change 'mode' to unsigned long. Index: common/filestuff.c =================================================================== RCS file: /cvs/src/src/gdb/common/filestuff.c,v retrieving revision 1.1 diff -u -r1.1 filestuff.c --- common/filestuff.c 22 Apr 2013 16:46:15 -0000 1.1 +++ common/filestuff.c 23 Apr 2013 15:43:46 -0000 @@ -28,10 +28,18 @@ #include <string.h> #include <fcntl.h> #include <unistd.h> -#include <sys/socket.h> #include <sys/types.h> #include <sys/stat.h> +#ifdef USE_WIN32API +#include <winsock2.h> +#include <windows.h> +#else +#include <sys/socket.h> +/* Define HAVE_F_GETFD if we plan to use F_GETFD. */ +#define HAVE_F_GETFD F_GETFD +#endif + #ifdef HAVE_SYS_RESOURCE_H #include <sys/resource.h> #endif /* HAVE_SYS_RESOURCE_H */ @@ -215,6 +223,7 @@ static void mark_cloexec (int fd) { +#ifdef HAVE_F_GETFD int old = fcntl (fd, F_GETFD, 0); if (old != -1) @@ -229,6 +238,7 @@ trust_o_cloexec = -1; } } +#endif /* HAVE_F_GETFD */ } /* Depending on TRUST_O_CLOEXEC, mark FD as close-on-exec. */ @@ -254,7 +264,7 @@ /* See filestuff.h. */ int -gdb_open_cloexec (const char *filename, int flags, mode_t mode) +gdb_open_cloexec (const char *filename, int flags, unsigned long mode) { int fd = open (filename, flags | O_CLOEXEC, mode); @@ -303,6 +313,7 @@ int gdb_socketpair_cloexec (int namespace, int style, int protocol, int filedes[2]) { +#ifdef HAVE_SOCKETPAIR int result = socketpair (namespace, style | SOCK_CLOEXEC, protocol, filedes); if (result != -1) @@ -312,6 +323,9 @@ } return result; +#else + gdb_assert_not_reached (_("socketpair not available on this host")); +#endif } /* See filestuff.h. */ @@ -342,13 +356,17 @@ maybe_mark_cloexec (filedes[1]); } #else +#ifdef HAVE_PIPE result = pipe (filedes); if (result != -1) { mark_cloexec (filedes[0]); mark_cloexec (filedes[1]); } -#endif +#else /* HAVE_PIPE */ + gdb_assert_not_reached (_("pipe not available on this host")); +#endif /* HAVE_PIPE */ +#endif /* HAVE_PIPE2 */ return result; } Index: common/filestuff.h =================================================================== RCS file: /cvs/src/src/gdb/common/filestuff.h,v retrieving revision 1.1 diff -u -r1.1 filestuff.h --- common/filestuff.h 22 Apr 2013 16:46:15 -0000 1.1 +++ common/filestuff.h 23 Apr 2013 15:43:46 -0000 @@ -33,7 +33,8 @@ /* Like 'open', but ensures that the returned file descriptor has the close-on-exec flag set. */ -extern int gdb_open_cloexec (const char *filename, int flags, mode_t mode); +extern int gdb_open_cloexec (const char *filename, int flags, + /* mode_t */ unsigned long mode); /* Like 'fopen', but ensures that the returned file descriptor has the close-on-exec flag set. */ ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: RFC: close-on-exec internal file descriptors 2013-04-24 2:45 ` Tom Tromey @ 2013-04-24 20:35 ` Jan Kratochvil 0 siblings, 0 replies; 13+ messages in thread From: Jan Kratochvil @ 2013-04-24 20:35 UTC (permalink / raw) To: Tom Tromey; +Cc: Yao Qi, gdb-patches On Tue, 23 Apr 2013 17:49:00 +0200, Tom Tromey wrote: > 2013-04-23 Tom Tromey <tromey@redhat.com> > > * common/filestuff.c: Check USE_WIN32API before including > sys/socket.h. > (HAVE_F_GETFD): New define. > (mark_cloexec): Check HAVE_F_GETFD. > (gdb_open_cloexec): Change 'mode' to unsigned long. > (gdb_socketpair_cloexec): Check HAVE_SOCKETPAIR. > (gdb_pipe_cloexec): Check HAVE_PIPE. > * common/filestuff.h (gdb_open_cloexec): Change 'mode' to unsigned > long. Just confirming it fixed the MinGW problem for me for both 32 and 64 bits. Thanks, Jan ^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2013-05-10 17:00 UTC | newest] Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2012-12-18 17:45 RFC: close-on-exec internal file descriptors Tom Tromey 2012-12-18 22:12 ` Andreas Schwab 2012-12-19 14:53 ` Tom Tromey 2013-01-03 16:17 ` Tom Tromey 2013-04-22 23:55 ` Tom Tromey 2013-04-23 1:51 ` Tom Tromey 2013-05-01 14:47 ` Joel Brobecker 2013-05-06 18:53 ` Tom Tromey 2013-05-07 6:46 ` Joel Brobecker 2013-05-10 17:00 ` Tom Tromey 2013-04-23 15:45 ` Yao Qi 2013-04-24 2:45 ` Tom Tromey 2013-04-24 20:35 ` Jan Kratochvil
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox