From patchwork Wed Sep 26 11:11:25 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 29542 Received: (qmail 90449 invoked by alias); 26 Sep 2018 11:11:44 -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 90429 invoked by uid 89); 26 Sep 2018 11:11:43 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.4 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=TUI, sk:cygwin_, UD:cli-cmds.c, clicmdsc X-HELO: gateway23.websitewelcome.com Received: from gateway23.websitewelcome.com (HELO gateway23.websitewelcome.com) (192.185.48.84) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 26 Sep 2018 11:11:41 +0000 Received: from cm17.websitewelcome.com (cm17.websitewelcome.com [100.42.49.20]) by gateway23.websitewelcome.com (Postfix) with ESMTP id 928E121ED0 for ; Wed, 26 Sep 2018 06:11:39 -0500 (CDT) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id 57jHgTUCBPvAd57jHgLaiY; Wed, 26 Sep 2018 06:11:39 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Sender:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=cGaJ31d0OwNVSfkpyTQ8CbmBdP3ko3lKP7sZvU3XPCs=; b=eNAzMNiZM+ICDk9/DtDAbBLbgX QtOw7i0DrvTeAKHn8bllGlEC3+TNy74d5K2QrN4/D/pEtVyD2cUJjpbXj20pTzhVYeTa5qAiehDYL gGyK80Zj1L2xV/U4jJEfTscQ9; Received: from 97-122-190-66.hlrn.qwest.net ([97.122.190.66]:44312 helo=bapiya.Home) by box5379.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.91) (envelope-from ) id 1g57jH-0014pw-5q; Wed, 26 Sep 2018 06:11:39 -0500 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [RFC 1/6] Unify shell-finding logic Date: Wed, 26 Sep 2018 05:11:25 -0600 Message-Id: <20180926111130.18956-2-tom@tromey.com> In-Reply-To: <20180926111130.18956-1-tom@tromey.com> References: <20180926111130.18956-1-tom@tromey.com> I noticed several places in gdb that were using getenv("SHELL") and then falling back to "/bin/sh" if it returned NULL. This unifies these into a single function. gdb/ChangeLog 2018-09-26 Tom Tromey * procfs.c (procfs_target::create_inferior): Use get_shell. * cli/cli-cmds.c (shell_escape): Use get_shell. * windows-nat.c (windows_nat_target::create_inferior): Use get_shell. * common/pathstuff.c (get_shell): New function. * nat/fork-inferior.c (SHELL_FILE, get_startup_shell): Remove. (fork_inferior): Use get_shell. * common/pathstuff.h (get_shell): Declare. --- gdb/ChangeLog | 11 +++++++++++ gdb/cli/cli-cmds.c | 6 ++---- gdb/common/pathstuff.c | 12 ++++++++++++ gdb/common/pathstuff.h | 4 ++++ gdb/nat/fork-inferior.c | 21 ++------------------- gdb/procfs.c | 4 ++-- gdb/windows-nat.c | 5 ++--- 7 files changed, 35 insertions(+), 28 deletions(-) diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c index c60e5efd0c..37988dd5d7 100644 --- a/gdb/cli/cli-cmds.c +++ b/gdb/cli/cli-cmds.c @@ -50,6 +50,7 @@ #include "cli/cli-utils.h" #include "extension.h" +#include "common/pathstuff.h" #ifdef TUI #include "tui/tui.h" /* For tui_active et.al. */ @@ -726,13 +727,10 @@ shell_escape (const char *arg, int from_tty) if ((pid = vfork ()) == 0) { - const char *p, *user_shell; + const char *p, *user_shell = get_shell (); close_most_fds (); - if ((user_shell = getenv ("SHELL")) == NULL) - user_shell = "/bin/sh"; - /* Get the name of the shell for arg0. */ p = lbasename (user_shell); diff --git a/gdb/common/pathstuff.c b/gdb/common/pathstuff.c index 82905c9e68..6d8e53f4e1 100644 --- a/gdb/common/pathstuff.c +++ b/gdb/common/pathstuff.c @@ -190,3 +190,15 @@ get_standard_cache_dir () return {}; } + +/* See common/pathstuff.h. */ + +const char * +get_shell () +{ + const char *ret = getenv ("SHELL"); + if (ret == NULL) + ret = "/bin/sh"; + + return ret; +} diff --git a/gdb/common/pathstuff.h b/gdb/common/pathstuff.h index a43b963651..40fbda8d85 100644 --- a/gdb/common/pathstuff.h +++ b/gdb/common/pathstuff.h @@ -64,4 +64,8 @@ extern bool contains_dir_separator (const char *path); extern std::string get_standard_cache_dir (); +/* Return the file name of the user's shell. */ + +extern const char *get_shell (); + #endif /* PATHSTUFF_H */ diff --git a/gdb/nat/fork-inferior.c b/gdb/nat/fork-inferior.c index 40cd05a0f8..f1032b43c9 100644 --- a/gdb/nat/fork-inferior.c +++ b/gdb/nat/fork-inferior.c @@ -24,16 +24,13 @@ #include "target/target.h" #include "common-inferior.h" #include "common-gdbthread.h" +#include "common/pathstuff.h" #include "signals-state-save-restore.h" #include "gdb_tilde_expand.h" #include extern char **environ; -/* Default shell file to be used if 'startup-with-shell' is set but - $SHELL is not. */ -#define SHELL_FILE "/bin/sh" - /* Build the argument vector for execv(3). */ class execv_argv @@ -265,20 +262,6 @@ execv_argv::init_for_shell (const char *exec_file, m_argv.push_back (NULL); } -/* Return the shell that must be used to startup the inferior. The - first attempt is the environment variable SHELL; if it is not set, - then we default to SHELL_FILE. */ - -static const char * -get_startup_shell () -{ - const char *ret = getenv ("SHELL"); - if (ret == NULL) - ret = SHELL_FILE; - - return ret; -} - /* See nat/fork-inferior.h. */ pid_t @@ -316,7 +299,7 @@ fork_inferior (const char *exec_file_arg, const std::string &allargs, /* Figure out what shell to start up the user program under. */ if (shell_file == NULL) - shell_file = get_startup_shell (); + shell_file = get_shell (); gdb_assert (shell_file != NULL); } diff --git a/gdb/procfs.c b/gdb/procfs.c index 6ffe569e69..ca381a71ae 100644 --- a/gdb/procfs.c +++ b/gdb/procfs.c @@ -3035,11 +3035,11 @@ procfs_target::create_inferior (const char *exec_file, const std::string &allargs, char **env, int from_tty) { - char *shell_file = getenv ("SHELL"); + const char *shell_file = get_shell (); char *tryname; int pid; - if (shell_file != NULL && strchr (shell_file, '/') == NULL) + if (strchr (shell_file, '/') == NULL) { /* We will be looking down the PATH to find shell_file. If we diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c index 0047a26418..8292cf4212 100644 --- a/gdb/windows-nat.c +++ b/gdb/windows-nat.c @@ -68,6 +68,7 @@ #include "complaints.h" #include "inf-child.h" #include "gdb_tilde_expand.h" +#include "common/pathstuff.h" #define AdjustTokenPrivileges dyn_AdjustTokenPrivileges #define DebugActiveProcessStop dyn_DebugActiveProcessStop @@ -2578,9 +2579,7 @@ windows_nat_target::create_inferior (const char *exec_file, } else { - sh = getenv ("SHELL"); - if (!sh) - sh = "/bin/sh"; + sh = get_shell (); if (cygwin_conv_path (CCP_POSIX_TO_WIN_W, sh, shell, __PMAX) < 0) error (_("Error starting executable via shell: %d"), errno); #ifdef __USEWIDE