From patchwork Fri May 4 16:50:09 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 27111 Received: (qmail 116145 invoked by alias); 4 May 2018 16:50:17 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 115341 invoked by uid 89); 4 May 2018 16:50:16 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.9 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=ntoprocfsc, nto-procfs.c, UD:nto-procfs.c X-HELO: gateway36.websitewelcome.com Received: from gateway36.websitewelcome.com (HELO gateway36.websitewelcome.com) (192.185.193.119) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 04 May 2018 16:50:14 +0000 Received: from cm12.websitewelcome.com (cm12.websitewelcome.com [100.42.49.8]) by gateway36.websitewelcome.com (Postfix) with ESMTP id D0B13400EF7B8 for ; Fri, 4 May 2018 11:50:12 -0500 (CDT) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id EduOfGoczlAdrEduOfp5qP; Fri, 04 May 2018 11:50:12 -0500 X-Authority-Reason: nr=8 Received: from 97-122-176-117.hlrn.qwest.net ([97.122.176.117]:55538 helo=bapiya.Home) by box5379.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.89_1) (envelope-from ) id 1fEduO-002P3O-Jg; Fri, 04 May 2018 11:50:12 -0500 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [RFA] Remove do_closedir_cleanup Date: Fri, 4 May 2018 10:50:09 -0600 Message-Id: <20180504165009.12758-1-tom@tromey.com> X-BWhitelist: no X-Source-L: No X-Exim-ID: 1fEduO-002P3O-Jg X-Source-Sender: 97-122-176-117.hlrn.qwest.net (bapiya.Home) [97.122.176.117]:55538 X-Source-Auth: tom+tromey.com X-Email-Count: 5 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes This removes both copies of do_closedir_cleanup in favor of a new unique_ptr specialization. Tested by the buildbot, though I'm not sure that these code paths are exercised there. ChangeLog 2018-05-04 Tom Tromey * nto-procfs.c (do_closedir_cleanup): Remove. (procfs_pidlist): Use gdb_dir_up. * procfs.c (do_closedir_cleanup): Remove. (proc_update_threads): Use gdb_dir_up. * common/filestuff.h (struct gdb_dir_deleter): New. (gdb_dir_up): New typedef. --- gdb/ChangeLog | 9 +++++++++ gdb/common/filestuff.h | 14 ++++++++++++++ gdb/nto-procfs.c | 24 ++++-------------------- gdb/procfs.c | 16 ++++------------ 4 files changed, 31 insertions(+), 32 deletions(-) diff --git a/gdb/common/filestuff.h b/gdb/common/filestuff.h index 92a2a5f4c7..0e46eb5da0 100644 --- a/gdb/common/filestuff.h +++ b/gdb/common/filestuff.h @@ -19,6 +19,8 @@ #ifndef FILESTUFF_H #define FILESTUFF_H +#include + /* Note all the file descriptors which are open when this is called. These file descriptors will not be closed by close_most_fds. */ @@ -84,4 +86,16 @@ extern int gdb_pipe_cloexec (int filedes[2]); extern struct cleanup *make_cleanup_close (int fd); +struct gdb_dir_deleter +{ + void operator() (DIR *dir) const + { + closedir (dir); + } +}; + +/* A unique pointer to a DIR. */ + +typedef std::unique_ptr gdb_dir_up; + #endif /* FILESTUFF_H */ diff --git a/gdb/nto-procfs.c b/gdb/nto-procfs.c index 51559e6c3e..f0ef9b9eb4 100644 --- a/gdb/nto-procfs.c +++ b/gdb/nto-procfs.c @@ -424,15 +424,8 @@ nto_procfs_target::update_thread_list () } static void -do_closedir_cleanup (void *dir) -{ - closedir (dir); -} - -static void procfs_pidlist (const char *args, int from_tty) { - DIR *dp = NULL; struct dirent *dirp = NULL; char buf[PATH_MAX]; procfs_info *pidinfo = NULL; @@ -441,13 +434,12 @@ procfs_pidlist (const char *args, int from_tty) pid_t num_threads = 0; pid_t pid; char name[512]; - struct cleanup *cleanups; char procfs_dir[PATH_MAX]; snprintf (procfs_dir, sizeof (procfs_dir), "%s%s", (nodestr != NULL) ? nodestr : "", "/proc"); - dp = opendir (procfs_dir); + gdb_dir_up dp (opendir (procfs_dir)); if (dp == NULL) { fprintf_unfiltered (gdb_stderr, "failed to opendir \"%s\" - %d (%s)", @@ -455,22 +447,17 @@ procfs_pidlist (const char *args, int from_tty) return; } - cleanups = make_cleanup (do_closedir_cleanup, dp); - /* Start scan at first pid. */ - rewinddir (dp); + rewinddir (dp.get ()); do { /* Get the right pid and procfs path for the pid. */ do { - dirp = readdir (dp); + dirp = readdir (dp.get ()); if (dirp == NULL) - { - do_cleanups (cleanups); - return; - } + return; snprintf (buf, sizeof (buf), "%s%s/%s/as", (nodestr != NULL) ? nodestr : "", "/proc", dirp->d_name); @@ -521,9 +508,6 @@ procfs_pidlist (const char *args, int from_tty) } } while (dirp != NULL); - - do_cleanups (cleanups); - return; } static void diff --git a/gdb/procfs.c b/gdb/procfs.c index 749b2b4833..a6e06a56ee 100644 --- a/gdb/procfs.c +++ b/gdb/procfs.c @@ -1722,20 +1722,13 @@ proc_delete_dead_threads (procinfo *parent, procinfo *thread, void *ignore) return 0; /* keep iterating */ } -static void -do_closedir_cleanup (void *dir) -{ - closedir ((DIR *) dir); -} - static int proc_update_threads (procinfo *pi) { char pathname[MAX_PROC_NAME_SIZE + 16]; struct dirent *direntry; - struct cleanup *old_chain = NULL; procinfo *thread; - DIR *dirp; + gdb_dir_up dirp; int lwpid; /* We should never have to apply this operation to any procinfo @@ -1756,11 +1749,11 @@ proc_update_threads (procinfo *pi) strcpy (pathname, pi->pathname); strcat (pathname, "/lwp"); - if ((dirp = opendir (pathname)) == NULL) + dir.reset (opendir (pathname)); + if (dirp == NULL) proc_error (pi, "update_threads, opendir", __LINE__); - old_chain = make_cleanup (do_closedir_cleanup, dirp); - while ((direntry = readdir (dirp)) != NULL) + while ((direntry = readdir (dirp.get ())) != NULL) if (direntry->d_name[0] != '.') /* skip '.' and '..' */ { lwpid = atoi (&direntry->d_name[0]); @@ -1768,7 +1761,6 @@ proc_update_threads (procinfo *pi) proc_error (pi, "update_threads, create_procinfo", __LINE__); } pi->threads_valid = 1; - do_cleanups (old_chain); return 1; }