Re: [patch gdbserver 7.6.1 only] Fix fd leak regression

Tom Tromey
2013-08-29T17:22:19Z

Tom> Yeah, I meant the other filestuff.h code, like gdb_socket_cloexec and
Tom> gdb_pipe_cloexec.

Like this. I haven't tested it using native-gdbserver yet.

Tom

commit d190e7c809a8dba4768cb67b9c46610e8e8e1d07
Author: Tom Tromey <tromey@redhat.com>
Date: Thu Aug 29 10:13:50 2013 -0600

use _cloexec functions in gdbserver

This changes gdbserver to use the various _cloexec functions defined
in common/filestuff.c.

Two wrinkles here: first, I couldn't compile the NTO and SPU changes;
second, the in process agent can't use gdb_socket_cloexec -- too bad,
since the setting would be useful there.

* hostio.c (handle_open): Use gdb_open_cloexec.
* linux-low.c (elf_64_file_p, linux_create_inferior)
(linux_read_memory, linux_read_auxv): Use gdb_open_cloexec.
(linux_async): Use gdb_pipe_cloexec.
(linux_qxfer_spu, get_phdr_phnum_from_proc_auxv): Use
gdb_open_cloexec.
* nto-low.c (do_attach): Use gdb_open_cloexec.
* remote-utils.c (remote_prepare): Use gdb_socket_cloexec.
(remote_open): Use gdb_open_cloexec.
* spu-low.c (spu_proc_xfer_spu): Use gdb_open_cloexec.
* tracepointc. (gdb_socket_cloexec): Define in IPA mode.
(init_named_socket): Use gdb_socket_cloexec.

diff --git a/gdb/gdbserver/hostio.c b/gdb/gdbserver/hostio.c
index a74c2f8..edef7bc 100644
--- a/gdb/gdbserver/hostio.c
+++ b/gdb/gdbserver/hostio.c
@@ -20,6 +20,7 @@

#include "server.h"
#include "gdb/fileio.h"
+#include "filestuff.h"

#include <fcntl.h>
#include <limits.h>
@@ -295,7 +296,7 @@ handle_open (char *own_buf)

/* We do not need to convert MODE, since the fileio protocol
uses the standard values. */
- fd = open (filename, flags, mode);
+ fd = gdb_open_cloexec (filename, flags, mode);

if (fd == -1)
{
diff --git a/gdb/gdbserver/linux-low.c b/gdb/gdbserver/linux-low.c
index 7db1fc8..3fa3bf1 100644
--- a/gdb/gdbserver/linux-low.c
+++ b/gdb/gdbserver/linux-low.c
@@ -315,7 +315,7 @@ elf_64_file_p (const char *file, unsigned int *machine)
Elf64_Ehdr header;
int fd;

- fd = open (file, O_RDONLY);
+ fd = gdb_open_cloexec (file, O_RDONLY, 0);
if (fd < 0)
return -1;

@@ -596,7 +596,7 @@ linux_create_inferior (char *program, char **allargs)
if (remote_connection_is_stdio ())
{
close (0);
- open ("/dev/null", O_RDONLY);
+ gdb_open_cloexec ("/dev/null", O_RDONLY, 0);
dup2 (2, 1);
if (write (2, "stdin/stdout redirected\n",
sizeof ("stdin/stdout redirected\n") - 1) < 0)
@@ -4426,7 +4426,7 @@ linux_read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
/* 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", pid);
- fd = open (filename, O_RDONLY | O_LARGEFILE);
+ fd = gdb_open_cloexec (filename, O_RDONLY | O_LARGEFILE, 0);
if (fd == -1)
goto no_proc;

@@ -4626,7 +4626,7 @@ linux_read_auxv (CORE_ADDR offset, unsigned char *myaddr, unsigned int len)

xsnprintf (filename, sizeof filename, "/proc/%d/auxv", pid);

- fd = open (filename, O_RDONLY);
+ fd = gdb_open_cloexec (filename, O_RDONLY, 0);
if (fd < 0)
return -1;

@@ -4859,7 +4859,7 @@ linux_async (int enable)

if (enable)
{
- if (pipe (linux_event_pipe) == -1)
+ if (gdb_pipe_cloexec (linux_event_pipe) == -1)
fatal ("creating event pipe failed.");

fcntl (linux_event_pipe[0], F_SETFL, O_NONBLOCK);
@@ -5000,7 +5000,7 @@ linux_qxfer_spu (const char *annex, unsigned char *readbuf,
}

sprintf (buf, "/proc/%ld/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;

@@ -5224,7 +5224,7 @@ get_phdr_phnum_from_proc_auxv (const int pid, const int is_elf64,

xsnprintf (filename, sizeof filename, "/proc/%d/auxv", pid);

- fd = open (filename, O_RDONLY);
+ fd = gdb_open_cloexec (filename, O_RDONLY, 0);
if (fd < 0)
return 1;

diff --git a/gdb/gdbserver/nto-low.c b/gdb/gdbserver/nto-low.c
index 3670133..91fffbc 100644
--- a/gdb/gdbserver/nto-low.c
+++ b/gdb/gdbserver/nto-low.c
@@ -21,6 +21,7 @@
#include "server.h"
#include "gdbthread.h"
#include "nto-low.h"
+#include "filestuff.h"

#include <limits.h>
#include <fcntl.h>
@@ -178,7 +179,8 @@ do_attach (pid_t pid)
init_nto_inferior (&nto_inferior);
}
xsnprintf (nto_inferior.nto_procfs_path, PATH_MAX - 1, "/proc/%d/as", pid);
- nto_inferior.ctl_fd = open (nto_inferior.nto_procfs_path, O_RDWR);
+ nto_inferior.ctl_fd = gdb_open_cloexec (nto_inferior.nto_procfs_path,
+ O_RDWR, 0);
if (nto_inferior.ctl_fd == -1)
{
TRACE ("Failed to open %s\n", nto_inferior.nto_procfs_path);
diff --git a/gdb/gdbserver/remote-utils.c b/gdb/gdbserver/remote-utils.c
index 5cd6fa1..25f5302 100644
--- a/gdb/gdbserver/remote-utils.c
+++ b/gdb/gdbserver/remote-utils.c
@@ -68,6 +68,8 @@
#include <sys/iomgr.h>
#endif /* __QNX__ */

+#include "filestuff.h"
+
#ifndef HAVE_SOCKLEN_T
typedef int socklen_t;
#endif
@@ -262,7 +264,7 @@ remote_prepare (char *name)
}
#endif

- listen_desc = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
+ listen_desc = gdb_socket_cloexec (PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (listen_desc == -1)
perror_with_name ("Can't open socket");

@@ -316,7 +318,7 @@ remote_open (char *name)

if (stat (name, &statbuf) == 0
&& (S_ISCHR (statbuf.st_mode) || S_ISFIFO (statbuf.st_mode)))
- remote_desc = open (name, O_RDWR);
+ remote_desc = gdb_open_cloexec (name, O_RDWR, 0);
else
{
errno = EINVAL;
diff --git a/gdb/gdbserver/spu-low.c b/gdb/gdbserver/spu-low.c
index e604b9f..63a2efc 100644
--- a/gdb/gdbserver/spu-low.c
+++ b/gdb/gdbserver/spu-low.c
@@ -239,7 +239,7 @@ spu_proc_xfer_spu (const char *annex, unsigned char *readbuf,
return 0;

sprintf (buf, "/proc/%ld/fd/%s", ptid_get_lwp (current_ptid), 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;

diff --git a/gdb/gdbserver/tracepoint.c b/gdb/gdbserver/tracepoint.c
index 5c0dec7..4c4a751 100644
--- a/gdb/gdbserver/tracepoint.c
+++ b/gdb/gdbserver/tracepoint.c
@@ -19,6 +19,7 @@
#include "server.h"
#include "gdbthread.h"
#include "agent.h"
+#include "filestuff.h"

#include <ctype.h>
#include <fcntl.h>
@@ -147,6 +148,9 @@ trace_vdebug (const char *fmt, ...)
# define ust_loaded gdb_agent_ust_loaded
# define helper_thread_id gdb_agent_helper_thread_id
# define cmd_buf gdb_agent_cmd_buf
+
+/* We don't want to use this one in IPA. */
+# define gdb_socket_cloexec socket
#endif

#ifndef IN_PROCESS_AGENT
@@ -6787,7 +6791,7 @@ init_named_socket (const char *name)
int result, fd;
struct sockaddr_un addr;

- result = fd = socket (PF_UNIX, SOCK_STREAM, 0);
+ result = fd = gdb_socket_cloexec (PF_UNIX, SOCK_STREAM, 0);
if (result == -1)
{
warning ("socket creation failed: %s", strerror (errno));