bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

more options for running bashrc


From: Bryan Henderson
Subject: more options for running bashrc
Date: Sun, 19 Jan 2003 04:55:31 +0000

Bash today contains a special case to make Rsh work which can be
fairly maddening.  I have a patch to address the problem.

The '.bashrc' file is designed to run in interactive shells.  As such,
it may do things that assume an intelligent person is there.  When you
run a shell remotely via Rsh/Rshd, it is not an interactive shell,
because Standard Input is not a terminal -- it is a socket.  But
because the Standard Input/Standard Output does ultimately hook up to
a terminal and a live person, Bash makes a special exception and runs
.bashrc anyway when Standard Input is a socket (and a few other
conditions hold).

But what if the socket doesn't really come from an Rsh client?  What
if the party on the other end of the socket is a machine that expects
to send shell commands and parse the responses?  Such is the case if
you run Openssh's Scp remote copy program.  You don't want to run
.bashrc then.  There's no human in the loop.  And in fact, Scp dies if
.bashrc issues any kind of message to Standard Output.

It's particularly maddening because it isn't easy to figure out what's
going on.  Only after a lot of debugging did I find this special (but
documented) exception to the rule that .bashrc is for interactive
shells.

I attach a patch that makes this special case Rshd behavior optional.
If the environment variable BASH_RSHD_NO_BASHRC is set, the special
case does not apply.

Note that Openssh doesn't require the special case for a truly
interactive ssh session.  Sshd invokes the shell with a
pseudo-terminal as Standard Input.


As a bonus, the patch makes the "system bashrc" function
runtime-controllable.  Today, there's a compile-time option to have
Bash run a system startup file such as /etc/bash.bashrc before
.bashrc.  The patch lets you specify a system startup file with the
SYS_BASHRC environment variable.  If you don't, Bash falls back to the
compiled-in name, if any.

-- 
Bryan Henderson                                    Phone 408-621-2000
San Jose, California


--- bash-2.05b/shell.c  Mon Jul  1 15:27:11 2002
+++ bash/shell.c        Sun Jan 19 04:37:55 2003
@@ -264,6 +264,7 @@
 
 static void execute_env_file __P((char *));
 static void run_startup_files __P((void));
+static void run_bashrc __P((void));
 static int open_shell_script __P((char *));
 static void set_bash_input __P((void));
 static int run_one_command __P((char *));
@@ -929,7 +930,8 @@
 
   /* get the rshd/sshd case out of the way first. */
   if (interactive_shell == 0 && no_rc == 0 && login_shell == 0 &&
-      act_like_sh == 0 && local_pending_command)
+      act_like_sh == 0 && local_pending_command &&
+      !getenv("BASH_N0_RSHD_BASHRC"))
     {
 #ifdef SSH_SOURCE_BASHRC
       run_by_ssh = (find_variable ("SSH_CLIENT") != (SHELL_VAR *)0) ||
@@ -942,14 +944,7 @@
         ~/.bashrc if we are a top-level shell. */
       if ((run_by_ssh || isnetconn (fileno (stdin))) && shell_level < 2)
        {
-#ifdef SYS_BASHRC
-#  if defined (__OPENNT)
-         maybe_execute_file (_prefixInstallPath(SYS_BASHRC, NULL, 0), 1);
-#  else
-         maybe_execute_file (SYS_BASHRC, 1);
-#  endif
-#endif
-         maybe_execute_file (bashrc_file, 1);
+          run_bashrc;
          return;
        }
     }
@@ -1026,16 +1021,7 @@
 
       /* bash */
       if (act_like_sh == 0 && no_rc == 0)
-       {
-#ifdef SYS_BASHRC
-#  if defined (__OPENNT)
-         maybe_execute_file (_prefixInstallPath(SYS_BASHRC, NULL, 0), 1);
-#  else
-         maybe_execute_file (SYS_BASHRC, 1);
-#  endif
-#endif
-         maybe_execute_file (bashrc_file, 1);
-       }
+          run_bashrc();
       /* sh */
       else if (act_like_sh && privileged_mode == 0 && sourced_env++ == 0)
        execute_env_file (get_string_value ("ENV"));
@@ -1051,6 +1037,29 @@
   set_job_control (old_job_control);
 #endif
 }
+
+
+/* Run the system bashrc file, if any, and the user's bashrc file */
+static void
+run_bashrc ()
+{
+    const char * sys_bashrc_file;
+
+    sys_bashrc_file = getenv("SYS_BASHRC");
+#ifdef SYS_BASHRC
+    if (sys_bashrc_file == NULL) {
+#  if defined (__OPENNT)
+        sys_bashrc_file = _prefixInstallPath(SYS_BASHRC, NULL, 0);
+#  else
+        sys_bashrc_file = SYS_BASHRC;
+#  endif
+    }
+#endif
+    if (sys_bashrc_file)
+        maybe_execute_file (sys_bashrc_file, 1);
+    maybe_execute_file (bashrc_file, 1);
+}
+
 
 #if defined (RESTRICTED_SHELL)
 /* Return 1 if the shell should be a restricted one based on NAME or the




reply via email to

[Prev in Thread] Current Thread [Next in Thread]