-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix multiple bugs in executing scripts without a #! path
When executing a script without a hashbang path like #!/bin/ksh, ksh forks itself, longjmps back to sh_main(), and then (among other things) calling sh_reinit() which is the function that tries to reinitialise as much of the shell as it can. This is its way of ensuring the child script is run in ksh and not some other shell. However, this appraoch is incredibly buggy. Among other things, changes in built-in commands and custom type definitions survived the reinitialisation, "exporting" variables didn't work properly, and the hash table and ${.sh.stats} weren't reset. As a result, depending on what the invoking script did, the invoked script could easily fail or malfunction. It is not actually possible to reinitialise the shell correctly, because some of the shell state is in locally scoped static variables that cannot simply be reinitialised. There are probably huge memory leaks with this approach as well. At some point, all this is going to need a total redesign. Clearly, the only reliable way involves execve(2) and a start from scratch. For now though, this seems to fix the known bugs at least. I'm sure there are more to be discovered. This commit makes another change: instead of the -h/trackall option (which has been a no-op for decades), the posix option is now inherited by the child script. Since there is no hashbang path from which to decide whether the shell should run in POSIX mode nor not, the best guess is probably the invoking script's setting. src/cmd/ksh93/sh/init.c: sh_reinit(): - Keep the SH_INIT state on during the entire procedure. - Delete remaining non-exported, non-default variables. - Remove attributes from exported variables. In POSIX mode, remove all attributes; otherwise, only remove readonly. - Unset discipline function pointers for variables. - Delete all custom types. - Delete all functions and built-ins, then reinitialise the built-ins table from scatch. - Free the alias values before clearing the alias table. - Same with hash table entries (tracked aliases). - Reset statistics. - Inherit SH_POSIX instead of SH_TRACKALL. - Call user init function last, not somewhere in the middle. src/cmd/ksh93/sh/name.c: sh_envnolocal(): - Be sure to preserve the export attribute of kept variables. Resolves: #350
- Loading branch information
Showing
5 changed files
with
186 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters