Browse Source

gdbsupport: make gdb_open_cloexec return scoped_fd

Make gdb_open_cloexec return a scoped_fd, to encourage using automatic
management of the file descriptor closing.  Except in the most trivial
cases, I changed the callers to just release the fd, which retains their
existing behavior.  That will allow the transition to using scoped_fd
more to go gradually, one caller at a time.

Change-Id: Ife022b403f96e71d5ebb4f1056ef6251b30fe554
binutils-2_38-branch
Simon Marchi 5 years ago
parent
commit
13084383e8
  1. 14
      gdb/auxv.c
  2. 2
      gdb/corelow.c
  3. 2
      gdb/darwin-nat.c
  4. 2
      gdb/gdb_bfd.c
  5. 2
      gdb/inf-child.c
  6. 2
      gdb/linux-nat.c
  7. 13
      gdb/nat/linux-namespaces.c
  8. 2
      gdb/remote-fileio.c
  9. 2
      gdb/ser-unix.c
  10. 7
      gdb/solib.c
  11. 16
      gdb/source.c
  12. 8
      gdb/top.c
  13. 2
      gdb/tracefile-tfile.c
  14. 8
      gdbsupport/filestuff.cc
  15. 7
      gdbsupport/filestuff.h
  16. 2
      gdbsupport/scoped_mmap.cc

14
gdb/auxv.c

@ -46,23 +46,21 @@ procfs_xfer_auxv (gdb_byte *readbuf,
ULONGEST len, ULONGEST len,
ULONGEST *xfered_len) ULONGEST *xfered_len)
{ {
int fd;
ssize_t l; ssize_t l;
std::string pathname = string_printf ("/proc/%d/auxv", inferior_ptid.pid ()); std::string pathname = string_printf ("/proc/%d/auxv", inferior_ptid.pid ());
fd = gdb_open_cloexec (pathname, writebuf != NULL ? O_WRONLY : O_RDONLY, 0); scoped_fd fd
if (fd < 0) = gdb_open_cloexec (pathname, writebuf != NULL ? O_WRONLY : O_RDONLY, 0);
if (fd.get () < 0)
return TARGET_XFER_E_IO; return TARGET_XFER_E_IO;
if (offset != (ULONGEST) 0 if (offset != (ULONGEST) 0
&& lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset) && lseek (fd.get (), (off_t) offset, SEEK_SET) != (off_t) offset)
l = -1; l = -1;
else if (readbuf != NULL) else if (readbuf != NULL)
l = read (fd, readbuf, (size_t) len); l = read (fd.get (), readbuf, (size_t) len);
else else
l = write (fd, writebuf, (size_t) len); l = write (fd.get (), writebuf, (size_t) len);
(void) close (fd);
if (l < 0) if (l < 0)
return TARGET_XFER_E_IO; return TARGET_XFER_E_IO;

2
gdb/corelow.c

@ -437,7 +437,7 @@ core_target_open (const char *arg, int from_tty)
flags |= O_RDWR; flags |= O_RDWR;
else else
flags |= O_RDONLY; flags |= O_RDONLY;
scratch_chan = gdb_open_cloexec (filename.get (), flags, 0); scratch_chan = gdb_open_cloexec (filename.get (), flags, 0).release ();
if (scratch_chan < 0) if (scratch_chan < 0)
perror_with_name (filename.get ()); perror_with_name (filename.get ());

2
gdb/darwin-nat.c

@ -1822,7 +1822,7 @@ may_have_sip ()
static void static void
copy_shell_to_cache (const char *shell, const std::string &new_name) copy_shell_to_cache (const char *shell, const std::string &new_name)
{ {
scoped_fd from_fd (gdb_open_cloexec (shell, O_RDONLY, 0)); scoped_fd from_fd = gdb_open_cloexec (shell, O_RDONLY, 0);
if (from_fd.get () < 0) if (from_fd.get () < 0)
error (_("Could not open shell (%s) for reading: %s"), error (_("Could not open shell (%s) for reading: %s"),
shell, safe_strerror (errno)); shell, safe_strerror (errno));

2
gdb/gdb_bfd.c

@ -541,7 +541,7 @@ gdb_bfd_open (const char *name, const char *target, int fd,
if (fd == -1) if (fd == -1)
{ {
fd = gdb_open_cloexec (name, O_RDONLY | O_BINARY, 0); fd = gdb_open_cloexec (name, O_RDONLY | O_BINARY, 0).release ();
if (fd == -1) if (fd == -1)
{ {
bfd_set_error (bfd_error_system_call); bfd_set_error (bfd_error_system_call);

2
gdb/inf-child.c

@ -261,7 +261,7 @@ inf_child_target::fileio_open (struct inferior *inf, const char *filename,
return -1; return -1;
} }
fd = gdb_open_cloexec (filename, nat_flags, nat_mode); fd = gdb_open_cloexec (filename, nat_flags, nat_mode).release ();
if (fd == -1) if (fd == -1)
*target_errno = host_to_fileio_error (errno); *target_errno = host_to_fileio_error (errno);

2
gdb/linux-nat.c

@ -3832,7 +3832,7 @@ linux_proc_xfer_memory_partial_pid (ptid_t ptid,
"/proc/%d/task/%ld/mem", ptid.pid (), ptid.lwp ()); "/proc/%d/task/%ld/mem", ptid.pid (), ptid.lwp ());
last_proc_mem_file.fd last_proc_mem_file.fd
= gdb_open_cloexec (filename, O_RDWR | O_LARGEFILE, 0); = gdb_open_cloexec (filename, O_RDWR | O_LARGEFILE, 0).release ();
if (last_proc_mem_file.fd == -1) if (last_proc_mem_file.fd == -1)
{ {

13
gdb/nat/linux-namespaces.c

@ -520,13 +520,8 @@ static ssize_t
mnsh_handle_open (int sock, const char *filename, mnsh_handle_open (int sock, const char *filename,
int flags, mode_t mode) int flags, mode_t mode)
{ {
int fd = gdb_open_cloexec (filename, flags, mode); scoped_fd fd = gdb_open_cloexec (filename, flags, mode);
ssize_t result = mnsh_return_fd (sock, fd, errno); return mnsh_return_fd (sock, fd.get (), errno);
if (fd >= 0)
close (fd);
return result;
} }
/* Handle a MNSH_REQ_UNLINK message. Must be async-signal-safe. */ /* Handle a MNSH_REQ_UNLINK message. Must be async-signal-safe. */
@ -901,7 +896,7 @@ linux_mntns_access_fs (pid_t pid)
if (ns == NULL) if (ns == NULL)
return MNSH_FS_DIRECT; return MNSH_FS_DIRECT;
fd = gdb_open_cloexec (linux_ns_filename (ns, pid), O_RDONLY, 0); fd = gdb_open_cloexec (linux_ns_filename (ns, pid), O_RDONLY, 0).release ();
if (fd < 0) if (fd < 0)
return MNSH_FS_ERROR; return MNSH_FS_ERROR;
@ -968,7 +963,7 @@ linux_mntns_open_cloexec (pid_t pid, const char *filename,
return -1; return -1;
if (access == MNSH_FS_DIRECT) if (access == MNSH_FS_DIRECT)
return gdb_open_cloexec (filename, flags, mode); return gdb_open_cloexec (filename, flags, mode).release ();
gdb_assert (access == MNSH_FS_HELPER); gdb_assert (access == MNSH_FS_HELPER);

2
gdb/remote-fileio.c

@ -425,7 +425,7 @@ remote_fileio_func_open (remote_target *remote, char *buf)
} }
} }
fd = gdb_open_cloexec (pathname, flags, mode); fd = gdb_open_cloexec (pathname, flags, mode).release ();
if (fd < 0) if (fd < 0)
{ {
remote_fileio_return_errno (remote, -1); remote_fileio_return_errno (remote, -1);

2
gdb/ser-unix.c

@ -75,7 +75,7 @@ static int hardwire_setstopbits (struct serial *, int);
static int static int
hardwire_open (struct serial *scb, const char *name) hardwire_open (struct serial *scb, const char *name)
{ {
scb->fd = gdb_open_cloexec (name, O_RDWR, 0); scb->fd = gdb_open_cloexec (name, O_RDWR, 0).release ();
if (scb->fd < 0) if (scb->fd < 0)
return -1; return -1;

7
gdb/solib.c

@ -257,7 +257,8 @@ solib_find_1 (const char *in_pathname, int *fd, bool is_solib)
} }
/* Now see if we can open it. */ /* Now see if we can open it. */
found_file = gdb_open_cloexec (temp_pathname.get (), O_RDONLY | O_BINARY, 0); found_file = gdb_open_cloexec (temp_pathname.get (),
O_RDONLY | O_BINARY, 0).release ();
/* If the search in gdb_sysroot failed, and the path name has a /* If the search in gdb_sysroot failed, and the path name has a
drive spec (e.g, c:/foo), try stripping ':' from the drive spec, drive spec (e.g, c:/foo), try stripping ':' from the drive spec,
@ -278,7 +279,7 @@ solib_find_1 (const char *in_pathname, int *fd, bool is_solib)
in_pathname + 2, (char *) NULL)); in_pathname + 2, (char *) NULL));
found_file = gdb_open_cloexec (temp_pathname.get (), found_file = gdb_open_cloexec (temp_pathname.get (),
O_RDONLY | O_BINARY, 0); O_RDONLY | O_BINARY, 0).release ();
if (found_file < 0) if (found_file < 0)
{ {
/* If the search in gdb_sysroot still failed, try fully /* If the search in gdb_sysroot still failed, try fully
@ -292,7 +293,7 @@ solib_find_1 (const char *in_pathname, int *fd, bool is_solib)
in_pathname + 2, (char *) NULL)); in_pathname + 2, (char *) NULL));
found_file = gdb_open_cloexec (temp_pathname.get (), found_file = gdb_open_cloexec (temp_pathname.get (),
O_RDONLY | O_BINARY, 0); O_RDONLY | O_BINARY, 0).release ();
} }
} }

16
gdb/source.c

@ -832,7 +832,7 @@ openp (const char *path, openp_flags opts, const char *string,
{ {
filename = (char *) alloca (strlen (string) + 1); filename = (char *) alloca (strlen (string) + 1);
strcpy (filename, string); strcpy (filename, string);
fd = gdb_open_cloexec (filename, mode, 0); fd = gdb_open_cloexec (filename, mode, 0).release ();
if (fd >= 0) if (fd >= 0)
goto done; goto done;
last_errno = errno; last_errno = errno;
@ -924,7 +924,7 @@ openp (const char *path, openp_flags opts, const char *string,
if (is_regular_file (filename, &reg_file_errno)) if (is_regular_file (filename, &reg_file_errno))
{ {
fd = gdb_open_cloexec (filename, mode, 0); fd = gdb_open_cloexec (filename, mode, 0).release ();
if (fd >= 0) if (fd >= 0)
break; break;
last_errno = errno; last_errno = errno;
@ -1060,7 +1060,6 @@ find_and_open_source (const char *filename,
{ {
char *path = source_path; char *path = source_path;
const char *p; const char *p;
int result;
/* If reading of source files is disabled then return a result indicating /* If reading of source files is disabled then return a result indicating
the attempt to read this source file failed. GDB will then display the attempt to read this source file failed. GDB will then display
@ -1080,12 +1079,11 @@ find_and_open_source (const char *filename,
if (rewritten_fullname != NULL) if (rewritten_fullname != NULL)
*fullname = std::move (rewritten_fullname); *fullname = std::move (rewritten_fullname);
result = gdb_open_cloexec (fullname->get (), OPEN_MODE, 0); scoped_fd result = gdb_open_cloexec (fullname->get (), OPEN_MODE, 0);
if (result.get () >= 0)
if (result >= 0)
{ {
*fullname = gdb_realpath (fullname->get ()); *fullname = gdb_realpath (fullname->get ());
return scoped_fd (result); return result;
} }
/* Didn't work -- free old one, try again. */ /* Didn't work -- free old one, try again. */
@ -1129,8 +1127,8 @@ find_and_open_source (const char *filename,
filename = rewritten_filename.get (); filename = rewritten_filename.get ();
/* Try to locate file using filename. */ /* Try to locate file using filename. */
result = openp (path, OPF_SEARCH_IN_PATH | OPF_RETURN_REALPATH, filename, int result = openp (path, OPF_SEARCH_IN_PATH | OPF_RETURN_REALPATH, filename,
OPEN_MODE, fullname); OPEN_MODE, fullname);
if (result < 0 && dirname != NULL) if (result < 0 && dirname != NULL)
{ {
/* Remove characters from the start of PATH that we don't need when /* Remove characters from the start of PATH that we don't need when

8
gdb/top.c

@ -335,13 +335,11 @@ ui::~ui ()
static gdb_file_up static gdb_file_up
open_terminal_stream (const char *name) open_terminal_stream (const char *name)
{ {
int fd; scoped_fd fd = gdb_open_cloexec (name, O_RDWR | O_NOCTTY, 0);
if (fd.get () < 0)
fd = gdb_open_cloexec (name, O_RDWR | O_NOCTTY, 0);
if (fd < 0)
perror_with_name (_("opening terminal failed")); perror_with_name (_("opening terminal failed"));
return gdb_file_up (fdopen (fd, "w+")); return fd.to_file ("w+");
} }
/* Implementation of the "new-ui" command. */ /* Implementation of the "new-ui" command. */

2
gdb/tracefile-tfile.c

@ -475,7 +475,7 @@ tfile_target_open (const char *arg, int from_tty)
flags = O_BINARY | O_LARGEFILE; flags = O_BINARY | O_LARGEFILE;
flags |= O_RDONLY; flags |= O_RDONLY;
scratch_chan = gdb_open_cloexec (filename.get (), flags, 0); scratch_chan = gdb_open_cloexec (filename.get (), flags, 0).release ();
if (scratch_chan < 0) if (scratch_chan < 0)
perror_with_name (filename.get ()); perror_with_name (filename.get ());

8
gdbsupport/filestuff.cc

@ -306,13 +306,13 @@ socket_mark_cloexec (int fd)
/* See filestuff.h. */ /* See filestuff.h. */
int scoped_fd
gdb_open_cloexec (const char *filename, int flags, unsigned long mode) gdb_open_cloexec (const char *filename, int flags, unsigned long mode)
{ {
int fd = open (filename, flags | O_CLOEXEC, mode); scoped_fd fd (open (filename, flags | O_CLOEXEC, mode));
if (fd >= 0) if (fd.get () >= 0)
maybe_mark_cloexec (fd); maybe_mark_cloexec (fd.get ());
return fd; return fd;
} }

7
gdbsupport/filestuff.h

@ -22,6 +22,7 @@
#include <dirent.h> #include <dirent.h>
#include <fcntl.h> #include <fcntl.h>
#include "gdb_file.h" #include "gdb_file.h"
#include "scoped_fd.h"
/* Note all the file descriptors which are open when this is called. /* Note all the file descriptors which are open when this is called.
These file descriptors will not be closed by close_most_fds. */ These file descriptors will not be closed by close_most_fds. */
@ -47,8 +48,8 @@ extern void close_most_fds (void);
/* Like 'open', but ensures that the returned file descriptor has the /* Like 'open', but ensures that the returned file descriptor has the
close-on-exec flag set. */ close-on-exec flag set. */
extern int gdb_open_cloexec (const char *filename, int flags, extern scoped_fd gdb_open_cloexec (const char *filename, int flags,
/* mode_t */ unsigned long mode); /* mode_t */ unsigned long mode);
/* Like mkstemp, but ensures that the file descriptor is /* Like mkstemp, but ensures that the file descriptor is
close-on-exec. */ close-on-exec. */
@ -63,7 +64,7 @@ gdb_mkostemp_cloexec (char *name_template, int flags = 0)
/* Convenience wrapper for the above, which takes the filename as an /* Convenience wrapper for the above, which takes the filename as an
std::string. */ std::string. */
static inline int static inline scoped_fd
gdb_open_cloexec (const std::string &filename, int flags, gdb_open_cloexec (const std::string &filename, int flags,
/* mode_t */ unsigned long mode) /* mode_t */ unsigned long mode)
{ {

2
gdbsupport/scoped_mmap.cc

@ -27,7 +27,7 @@
scoped_mmap scoped_mmap
mmap_file (const char *filename) mmap_file (const char *filename)
{ {
scoped_fd fd (gdb_open_cloexec (filename, O_RDONLY, 0)); scoped_fd fd = gdb_open_cloexec (filename, O_RDONLY, 0);
if (fd.get () < 0) if (fd.get () < 0)
perror_with_name (("open")); perror_with_name (("open"));

Loading…
Cancel
Save