From 13084383e8955c2ff7017ac8839301688a9ee34d Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Thu, 22 Jul 2021 11:56:33 -0400 Subject: 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 --- gdbsupport/filestuff.cc | 8 ++++---- gdbsupport/filestuff.h | 7 ++++--- gdbsupport/scoped_mmap.cc | 2 +- 3 files changed, 9 insertions(+), 8 deletions(-) (limited to 'gdbsupport') diff --git a/gdbsupport/filestuff.cc b/gdbsupport/filestuff.cc index 6ea2ad8..2975a0e 100644 --- a/gdbsupport/filestuff.cc +++ b/gdbsupport/filestuff.cc @@ -306,13 +306,13 @@ socket_mark_cloexec (int fd) /* See filestuff.h. */ -int +scoped_fd 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) - maybe_mark_cloexec (fd); + if (fd.get () >= 0) + maybe_mark_cloexec (fd.get ()); return fd; } diff --git a/gdbsupport/filestuff.h b/gdbsupport/filestuff.h index aa15f89..a2cb916 100644 --- a/gdbsupport/filestuff.h +++ b/gdbsupport/filestuff.h @@ -22,6 +22,7 @@ #include #include #include "gdb_file.h" +#include "scoped_fd.h" /* Note all the file descriptors which are open when this is called. 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 close-on-exec flag set. */ -extern int gdb_open_cloexec (const char *filename, int flags, - /* mode_t */ unsigned long mode); +extern scoped_fd gdb_open_cloexec (const char *filename, int flags, + /* mode_t */ unsigned long mode); /* Like mkstemp, but ensures that the file descriptor is 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 std::string. */ -static inline int +static inline scoped_fd gdb_open_cloexec (const std::string &filename, int flags, /* mode_t */ unsigned long mode) { diff --git a/gdbsupport/scoped_mmap.cc b/gdbsupport/scoped_mmap.cc index c76fb77..598b328 100644 --- a/gdbsupport/scoped_mmap.cc +++ b/gdbsupport/scoped_mmap.cc @@ -27,7 +27,7 @@ scoped_mmap 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) perror_with_name (("open")); -- cgit v1.1