ULONGEST len,
ULONGEST *xfered_len)
{
- int fd;
ssize_t l;
std::string pathname = string_printf ("/proc/%d/auxv", inferior_ptid.pid ());
- fd = gdb_open_cloexec (pathname, writebuf != NULL ? O_WRONLY : O_RDONLY, 0);
- if (fd < 0)
+ scoped_fd fd
+ = gdb_open_cloexec (pathname, writebuf != NULL ? O_WRONLY : O_RDONLY, 0);
+ if (fd.get () < 0)
return TARGET_XFER_E_IO;
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;
else if (readbuf != NULL)
- l = read (fd, readbuf, (size_t) len);
+ l = read (fd.get (), readbuf, (size_t) len);
else
- l = write (fd, writebuf, (size_t) len);
-
- (void) close (fd);
+ l = write (fd.get (), writebuf, (size_t) len);
if (l < 0)
return TARGET_XFER_E_IO;
flags |= O_RDWR;
else
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)
perror_with_name (filename.get ());
static void
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)
error (_("Could not open shell (%s) for reading: %s"),
shell, safe_strerror (errno));
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)
{
bfd_set_error (bfd_error_system_call);
return -1;
}
- fd = gdb_open_cloexec (filename, nat_flags, nat_mode);
+ fd = gdb_open_cloexec (filename, nat_flags, nat_mode).release ();
if (fd == -1)
*target_errno = host_to_fileio_error (errno);
"/proc/%d/task/%ld/mem", ptid.pid (), ptid.lwp ());
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)
{
mnsh_handle_open (int sock, const char *filename,
int flags, mode_t mode)
{
- int fd = gdb_open_cloexec (filename, flags, mode);
- ssize_t result = mnsh_return_fd (sock, fd, errno);
-
- if (fd >= 0)
- close (fd);
-
- return result;
+ scoped_fd fd = gdb_open_cloexec (filename, flags, mode);
+ return mnsh_return_fd (sock, fd.get (), errno);
}
/* Handle a MNSH_REQ_UNLINK message. Must be async-signal-safe. */
if (ns == NULL)
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)
return MNSH_FS_ERROR;
return -1;
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);
}
}
- fd = gdb_open_cloexec (pathname, flags, mode);
+ fd = gdb_open_cloexec (pathname, flags, mode).release ();
if (fd < 0)
{
remote_fileio_return_errno (remote, -1);
static int
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)
return -1;
}
/* 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
drive spec (e.g, c:/foo), try stripping ':' from the drive spec,
in_pathname + 2, (char *) NULL));
found_file = gdb_open_cloexec (temp_pathname.get (),
- O_RDONLY | O_BINARY, 0);
+ O_RDONLY | O_BINARY, 0).release ();
if (found_file < 0)
{
/* If the search in gdb_sysroot still failed, try fully
in_pathname + 2, (char *) NULL));
found_file = gdb_open_cloexec (temp_pathname.get (),
- O_RDONLY | O_BINARY, 0);
+ O_RDONLY | O_BINARY, 0).release ();
}
}
{
filename = (char *) alloca (strlen (string) + 1);
strcpy (filename, string);
- fd = gdb_open_cloexec (filename, mode, 0);
+ fd = gdb_open_cloexec (filename, mode, 0).release ();
if (fd >= 0)
goto done;
last_errno = errno;
if (is_regular_file (filename, ®_file_errno))
{
- fd = gdb_open_cloexec (filename, mode, 0);
+ fd = gdb_open_cloexec (filename, mode, 0).release ();
if (fd >= 0)
break;
last_errno = errno;
{
char *path = source_path;
const char *p;
- int result;
/* If reading of source files is disabled then return a result indicating
the attempt to read this source file failed. GDB will then display
if (rewritten_fullname != NULL)
*fullname = std::move (rewritten_fullname);
- result = gdb_open_cloexec (fullname->get (), OPEN_MODE, 0);
-
- if (result >= 0)
+ scoped_fd result = gdb_open_cloexec (fullname->get (), OPEN_MODE, 0);
+ if (result.get () >= 0)
{
*fullname = gdb_realpath (fullname->get ());
- return scoped_fd (result);
+ return result;
}
/* Didn't work -- free old one, try again. */
filename = rewritten_filename.get ();
/* Try to locate file using filename. */
- result = openp (path, OPF_SEARCH_IN_PATH | OPF_RETURN_REALPATH, filename,
- OPEN_MODE, fullname);
+ int result = openp (path, OPF_SEARCH_IN_PATH | OPF_RETURN_REALPATH, filename,
+ OPEN_MODE, fullname);
if (result < 0 && dirname != NULL)
{
/* Remove characters from the start of PATH that we don't need when
static gdb_file_up
open_terminal_stream (const char *name)
{
- int fd;
-
- fd = gdb_open_cloexec (name, O_RDWR | O_NOCTTY, 0);
- if (fd < 0)
+ scoped_fd fd = gdb_open_cloexec (name, O_RDWR | O_NOCTTY, 0);
+ if (fd.get () < 0)
perror_with_name (_("opening terminal failed"));
- return gdb_file_up (fdopen (fd, "w+"));
+ return fd.to_file ("w+");
}
/* Implementation of the "new-ui" command. */
flags = O_BINARY | O_LARGEFILE;
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)
perror_with_name (filename.get ());
/* 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;
}
#include <dirent.h>
#include <fcntl.h>
#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. */
/* 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. */
/* 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)
{
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"));