bug-bash
[Top][All Lists]
Advanced

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

Re: using exec to close a fd in a var crashes bash


From: Martin D Kealey
Subject: Re: using exec to close a fd in a var crashes bash
Date: Tue, 22 Aug 2023 14:58:49 +1000

On Sun, 20 Aug 2023, 14:15 Grisha Levit, <grishalevit@gmail.com> wrote:

> [...] it would probably make sense to undo them if the exec fails and the
> shell is not going to exit.
>

Just one question: how?

The sequence of dup2() and close() calls has to occur before the execve()
call. Whilst they could be undone by using fcntl(fd,F_DUPFD_CLOEXEC) (to
stash initial open filedescriptors), and/or fcntl(fd,F_SETFD,O_CLOEXEC) (in
lieu of close(fd)), I rather doubt it would be worthwhile.

1. This approach needs extra syscalls per manipulated fd, plus bookkeeping
logic and comprehensive testing;2. It increases the number of open
filedescriptors, so _SC_OPEN_MAX or RLIMIT_NOFILE limits could cause
failures;
3. (I'm guessing) there are some platforms where there are other potential
failure cases, or even can't work at all.
4. In a case like
    exec $foo <&-
it would be necessary to expand `$foo` to see if it's empty before deciding
whether to use close(0) or fcntl(0,F_SETFD,O_CLOEXEC). I suspect that this
would be a non-trivial code change.

Overall I'm not sure that "fixing" this is really the right thing to do.
It'd be a lot of work to get right, could make things worse in corner
cases, and could conceivably break scripts that rely on the current
behaviour.

-Martin

>


reply via email to

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