You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix /dev/fd/1 memory fault and $0 for /dev/fd scripts
Changes:
- sh_init(): Don't set $0 to the path to ksh. The correct behavior is to
always use the script name, even when it's a /dev/fd file. The behavior
of ksh93 should match that of bash and other shells. Please read:
ksh93#866 (comment)ksh93#874 (comment)
- sh_main(): Do not attempt to optimize away sh_open() for the
stdio fds (0, 1, 2) because that can cause buggy behavior.
Ksh's handling of /dev/fd/1 and /dev/stdin should not differ. See:
ksh93#879 (comment)
- exfile(): Don't dup or close fds for /dev/fd scripts with a file
descriptor > 2. Those should be left open pursuant to the $0 name
of the script. (This cannot be done safely for fds 1 and 2, so the
stdio fds are all dup'ed, but not closed.)
- sh_main(): If the fd obtained for the script by sh_open() is <= 2,
that means the shell was opened with one of the stdio fds closed,
so for correct behavior use sh_iomovefd() to keep the stdio fd closed.
- sh_iomovefd(): As an optimization, move the fd to a number that's
either >= 3 or >= 10, depending on what is most appropriate. This
avoids a possible inefficient scenario of dup'ing an fd from
0 -> 3 -> 10 (less syscalls is better for performance).
- sh_main(): Use O_CLOEXEC in advance for the fd (which is guaranteed to
get close-on-exec via a later fcntl call; if we don't end up calling
fcntl for whatever reason O_CLOEXEC will help avoid an extra syscall).
- basic.sh: Add regression tests for $0 and its associated fd. For the
time being I've omitted a /dev/fd/1 regression test because I can't
get one to work correctly with the pty tool.
Patch originally posted here:
ksh93#879 (comment)Fixesksh93#874Fixesksh93#877
0 commit comments