linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jan Nieuwenhuizen <janneke@gnu.org>
To: root@chaos.analogic.com
Cc: Pavel Machek <pavel@suse.cz>,
	Erik Hensema <erik@hensema.xs4all.nl>,
	linux-kernel@vger.kernel.org, bug-bash@gnu.org
Subject: [PATCH #3]: print missing interpreter name [Was: Re: binfmt_script and ^M]
Date: 05 Mar 2001 21:18:35 +0100	[thread overview]
Message-ID: <m3y9ukezqc.fsf@appel.lilypond.org> (raw)
In-Reply-To: <Pine.LNX.3.95.1010305105132.9913B-100000@chaos.analogic.com>
In-Reply-To: "Richard B. Johnson"'s message of "Mon, 5 Mar 2001 10:59:48 -0500 (EST)"

"Richard B. Johnson" <root@chaos.analogic.com> writes:

> No. I did not miss the point. The 'No such file or directory' error
> (when you can see the ^$^$)#@@*& filename with 'ls'), usually means
> that there is something wrong with the file.

Now, let's see.  When this error happens, it can be one of these:

    1a) the script itself is broken (eg, it has `#!/bin/bash\r')
    1b) the interpreter is missing, (eg, the script has
        `#!/usr/bin/perl', while perl got removed during the
        grand perl repackaging)
    2)  the interpreter is broken, (eg, missing shared library)
    3)  the script does not exist

In any case, the heart of the problems 1) and 2) is expressed by the
fact that there's something fishy with #!INTERPRETER.  In the case of
1a, the script should be fixed, in case 1b, the system should be
fixed, but this doesn't really matter.  Either way: INTERPRETER is the
file the kernel is talking about when it says ENOENT.  Only in case 3),
the ENOENT is about the script itself.

It seems reasonable, and helpful, for bash to print the name of the
INTERPRETER.

> Usually, I have found that it was an executable linked against some
> other runtime library than what I have. `strace` finds this quickly.

Well, this doesn't mean very much, other than you happen to experience
2) most (and you're fast at identifying the strange error).  But even
in this case, the name of the broken interpreter is what you should be
warned about.  Maybe others usually experience 1b), who cares?

In this third incarnation of my patch (today really isn't my day), you
see these messages for 1-3:

1)
    $ cat /bin/foo.sh 
    #!/foo/bar/baz
    echo bar

    $ /bin/bash -c /bin/foo.sh 
    /bin/bash: /bin/foo.sh: No such file or directory

    $ ./new/bash -c /bin/foo.sh 
    ./new/bash: /bin/foo.sh: /foo/bar/baz: No such file or directory

2)
    $ cat /bin/no-ld.sh 
    #!/usr/bin/urg
    $ ldd /bin/urg                
	    libkpathsea.so.3 => not found
            [..]

    $ /bin/bash -c /bin/no-ld.sh 
    /bin/bash: /bin/no-ld.sh: No such file or directory

    $ ./new/bash -c /bin/no-ld.sh 
    ./new/bash: /bin/no-ld.sh: /usr/bin/urg: No such file or directory

3)
    $ /bin/bash -c /bin/no-such-foo.sh
    /bin/bash: /bin/no-such-foo.sh: No such file or directory

    $ ./new/bash -c /bin/no-such-foo.sh
    ./new/bash: /bin/no-such-foo.sh: No such file or directory


Ok, here goes try #3:

--- ../bash-2.04.orig/ChangeLog	Mon Mar  5 21:06:11 2001
+++ ./ChangeLog	Mon Mar  5 19:22:46 2001
@@ -0,0 +1,5 @@
+2001-03-05  Jan Nieuwenhuizen  <janneke@gnu.org>
+
+	* execute_cmd.c (extract_hash_bang_interpreter): New function.
+	(shell_execve): More informative error message.
+
--- ../bash-2.04.orig/execute_cmd.c	Tue Jan 25 17:29:11 2000
+++ ./execute_cmd.c	Mon Mar  5 20:35:46 2001
@@ -3035,6 +3035,42 @@
     }
 }
 
+/* Look for #!INTERPRETER in file COMMAND, and return INTERPRETER . */
+static char *
+extract_hash_bang_interpreter (char *command, char buf[80])
+{
+  int fd;
+  char *interpreter;
+
+  interpreter = "";
+  fd = open (command, O_RDONLY);
+  if (fd >= 0)
+    {
+      int len;
+	      
+      len = read (fd, (char *)buf, 80);
+      close (fd);
+	      
+      if (len > 0
+	  && buf[0] == '#' && buf[1] == '!')
+	{
+	  int i;
+	  int start;
+		  
+	  for (i = 2; whitespace (buf[i]) && i < len; i++)
+	    ;
+	  
+	  for (start = i;
+	       !whitespace (buf[i]) && buf[i] != '\n' && i < len;
+	       i++)
+	    ;
+
+	  interpreter = substring ((char *)buf, start, i);
+	}
+    }
+  return interpreter;
+}
+
 /* Execute a simple command that is hopefully defined in a disk file
    somewhere.
 
@@ -3326,7 +3362,17 @@
       else
 	{
 	  errno = i;
-	  file_error (command);
+	  if (errno == ENOENT)
+	    {
+	      char buf[80];
+	      char *interpreter = extract_hash_bang_interpreter (command, buf);
+	      if (strlen (interpreter))
+		sys_error ("%s: %s", command, interpreter);
+	      else
+		file_error (command);
+	    }
+	  else
+	    file_error (command);
 	}
       return ((i == ENOENT) ? EX_NOTFOUND : EX_NOEXEC);	/* XXX Posix.2 says that exit status is 126 */
     }


-- 
Jan Nieuwenhuizen <janneke@gnu.org> | GNU LilyPond - The music typesetter
http://www.xs4all.nl/~jantien       | http://www.lilypond.org


  reply	other threads:[~2001-03-05 20:19 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-02-27 13:03 binfmt_script and ^M Ivo Timmermans
2001-02-27 13:44 ` Alan Cox
2001-02-27 13:47   ` Ivo Timmermans
2001-02-27 13:54     ` Alan Cox
2001-02-27 14:36       ` Rogier Wolff
2001-02-28 20:10   ` Erik Hensema
2001-03-01 12:04     ` Pavel Machek
2001-03-05 13:20       ` Jan Nieuwenhuizen
2001-03-05 13:37         ` Andreas Schwab
2001-03-05 13:40         ` Richard B. Johnson
2001-03-05 14:55           ` John Kodis
2001-03-05 15:25             ` Rik van Riel
2001-03-05 15:59               ` Jeff Mcadams
2001-03-05 16:18                 ` Paul Flinders
2001-03-05 17:12                   ` Andreas Schwab
2001-03-05 19:54                     ` Paul Flinders
2001-03-05 20:09                       ` Paul Flinders
2001-03-06 10:41                       ` Andreas Schwab
2001-03-06 12:33                         ` Paul Flinders
2001-03-06 14:45                           ` Laramie Leavitt
2001-03-06 15:12                             ` Sean Hunter
2001-03-06 15:37                               ` David Weinehall
2001-03-06 21:10                                 ` Thorsten Glaser Geuer
     [not found]                                   ` <20010308130145.O20737@dev.sportingbet.com>
2001-03-09 16:52                                     ` Thorsten Glaser Geuer
2001-03-06 15:53                               ` James A. Sutherland
2001-03-07  8:29                               ` Ondrej Sury
2001-03-06 16:59                             ` Xavier Bestel
2001-03-06 18:17                           ` binfmt_script and ^M (historical note) Don Dugger
2001-03-05 18:58                   ` binfmt_script and ^M Pozsar Balazs
2001-03-05 20:39                     ` Robert Read
2001-03-05 21:05                       ` Pozsar Balazs
2001-03-05 22:34                         ` Robert Read
2001-03-06 15:14                           ` Jeff Coy
2001-03-06 18:15                             ` Peter Samuelson
2001-03-06 18:36                               ` Jeff Coy
2001-03-06 20:26                                 ` John Kodis
2001-03-06 20:43                                   ` Andreas Schwab
2001-03-06  2:18                       ` Richard B. Johnson
2001-03-05 15:50             ` Richard B. Johnson
2001-03-05 16:53               ` H. Peter Anvin
2001-03-05 21:48             ` Dr. Kelsey Hudson
2001-03-06 18:19               ` Peter Samuelson
2001-03-06 21:04                 ` Dr. Kelsey Hudson
2001-03-05 14:56           ` [PATCH]: print missing interpreter name [Was: Re: binfmt_script and ^M] Jan Nieuwenhuizen
2001-03-05 15:59             ` Richard B. Johnson
2001-03-05 20:18               ` Jan Nieuwenhuizen [this message]
2001-03-05 16:37           ` binfmt_script and ^M Erik Hensema
2001-03-05 22:13             ` Pavel Machek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m3y9ukezqc.fsf@appel.lilypond.org \
    --to=janneke@gnu.org \
    --cc=bug-bash@gnu.org \
    --cc=erik@hensema.xs4all.nl \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pavel@suse.cz \
    --cc=root@chaos.analogic.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).