All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: Junio C Hamano <gitster@pobox.com>
Cc: Jonathan Nieder <jrnieder@gmail.com>,
	James Pickens <jepicken@gmail.com>, Git ML <git@vger.kernel.org>
Subject: Re: [PATCH 2/2] git: continue alias lookup on EACCES errors
Date: Wed, 28 Mar 2012 17:04:07 -0400	[thread overview]
Message-ID: <20120328210407.GC10174@sigill.intra.peff.net> (raw)
In-Reply-To: <7vzkb0tq10.fsf@alter.siamese.dyndns.org>

On Wed, Mar 28, 2012 at 01:43:39PM -0700, Junio C Hamano wrote:

> > +	while (1) {
> > +		const char *end = strchrnul(p, ':');
> > +		const char *path;
> > +		struct stat st;
> > +
> > +		path = mkpath("%.*s/%s", (int)(end - p), p, file);
> 
> Given PATH=":/usr/bin:/bin" and file "frotz" (please call it "cmd" or
> something, by the way), end points at the first colon and path becomes
> "/frotz".  Oops?

Ugh, yeah. This is what I meant when I said "checking afterwards
basically means re-implementing execvp". :)

Regarding the name, I pulled it from the linux-manpages execvp(3), since
this is supposed to be compatible. POSIX uses the even worse "path" as
the first element. But there is no reason we have to follow those naming
conventions.

> > +int sane_execvp(const char *file, char * const argv[])
> > +{
> > +	int ret = execvp(file, argv);
> > +	if (ret < 0 && errno == EACCES && !file_in_path_is_nonexecutable(file))
> > +		errno = ENOENT;
> > +	return ret;
> 
> Double negation makes my head hurt, but unfortunately, we cannot rename it
> to "executable_exists_on_path()" and negate its return value.

Right, technically it could exist in two places, and we care about
finding the first one.

In practice, I think we can just do exists_on_path() and not even worry
about the executable bit. If it exists, execvp did not run it, and we
got EACCES, then it is not executable. Any other error would trump
EACCES (i.e., execvp would have returned immediately; with EACCES it
waits until it has processed all entries before returning EACCES).

I actually think this would be easier to read if we simply
re-implemented execvp.

> Anyway, the logic is to set errno to ENOENT when
> 
>  - We tried to exec, and got EACCES; and
>  - There is a file on the PATH that lacks executable bit.
> 
> In such a case, the error from execvp() is not about the file it tried to
> execute lacked executable bit, but there was nothing that match the name,
> but it couldn't be certain because some directories were not readable.
> 
> OK.  I think I can follow that logic.

I think you are backwards. There is _no_ file on the PATH that lacks the
executable bit, and therefore the error is about an inaccessible
directory.

You could also search for an inaccessible directory, but that is not
quite right. If you have an inaccessible directory _and_ a matching file
with no executable bit, then you would make the wrong assumption.

> If there are more than one entry on PATH, and a system call made during
> first round of the loop fails but a later round finds a non-executable
> file, i.e.
> 
> 	$ PATH=/nosuch:/home/peff/bin; export PATH
>         $ >/home/peff/bin/frotz; chmod -x /home/peff/bin/frotz
>         git frotz
> 
> we would get EACCES from execvp(), the first round runs stat("/nosuch/frotz")
> and sets errno to ENOTDIR, and the second round runs stat() and access()
> on "/home/peff/bin/frotz" and returns 1 to say "Yeah, there is a plain
> file frotz that cannot be executed".
> 
> And sane_execvp() will return ENOTDIR?
>
> So sane_execvp() would probably need to do a bit more (but not that much).
> 
> 	if (ret < 0 && errno == EACCES)
> 		errno = file_in_path_is_nonexecutable(file) ? EACCES : ENOENT;
> 	return ret;
> 
> or something.

Good point. We definitely need to save the EACCES errno across the
second round lookup.

-Peff

  reply	other threads:[~2012-03-28 21:04 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-26 23:48 Bug? Bad permissions in $PATH breaks Git aliases James Pickens
2012-03-27  3:19 ` Jeff King
2012-03-27  7:25   ` James Pickens
2012-03-27 15:11   ` Junio C Hamano
2012-03-27 17:59     ` Jeff King
2012-03-27 18:04       ` [PATCH 1/2] run-command: propagate EACCES errors to parent Jeff King
2012-03-27 18:24         ` Junio C Hamano
2012-03-27 18:33           ` Jeff King
2012-03-27 18:05       ` [PATCH 2/2] git: continue alias lookup on EACCES errors Jeff King
2012-03-27 19:16         ` Junio C Hamano
2012-03-28  4:30           ` Jeff King
2012-03-28 17:42             ` Junio C Hamano
2012-03-28 17:48               ` Jeff King
2012-03-28 18:04                 ` Jonathan Nieder
2012-03-28 18:31                   ` Junio C Hamano
2012-03-28 18:40                     ` Jonathan Nieder
2012-03-28 19:39                       ` Jeff King
2012-03-28 19:45                         ` Jonathan Nieder
2012-03-28 20:18                           ` Jeff King
2012-03-28 20:37                             ` Jeff King
2012-03-28 20:51                               ` Jonathan Nieder
2012-03-28 20:52                                 ` Jeff King
2012-03-28 20:42                             ` Jonathan Nieder
2012-03-28 20:51                               ` Jeff King
2012-03-28 21:01                                 ` Jonathan Nieder
2012-03-28 21:25                                   ` Jeff King
2012-03-28 21:30                               ` Frans Klaver
2012-03-28 20:43                             ` Junio C Hamano
2012-03-28 21:04                               ` Jeff King [this message]
2012-03-28 21:44                                 ` Junio C Hamano
2012-03-28 21:57                             ` Jeff King
2012-03-28 22:07                               ` Jeff King
2012-03-28 22:18                                 ` Junio C Hamano
2012-03-29 11:31                               ` Frans Klaver
2012-03-29 17:20                                 ` Jeff King
2012-03-29 17:23                                   ` Frans Klaver
2012-03-28 19:38                     ` Jeff King
2012-03-28 18:29                 ` Junio C Hamano
2012-03-28 19:40                   ` Jeff King
2012-03-29 11:16                     ` Frans Klaver
2012-03-29 17:15                       ` Jeff King
2012-03-29 17:21                         ` Frans Klaver
2012-03-27  6:14 ` Bug? Bad permissions in $PATH breaks Git aliases Johannes Sixt
2012-03-27  7:37   ` James Pickens
2012-03-27 15:14     ` Junio C Hamano
2012-03-27 17:48       ` James Pickens
2012-03-27 18:03         ` Junio C Hamano

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=20120328210407.GC10174@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=jepicken@gmail.com \
    --cc=jrnieder@gmail.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.