Commit Graph

59 Commits

Author SHA1 Message Date
christos ac33f4ea5e Drop privileges when executed set{u,g}id unless -p is specified like other
shells do to avoid system() and popen() abuse.
2015-05-26 21:35:15 +00:00
christos 01f35fcceb PR/48843: Jarmo Jaakkola: dot commands mess up scope nesting tracking
Evaluation of commands goes completely haywire if a file containing
a break/continue/return command outside its "intended" scope is sourced
using a dot command inside its "intended" scope.  The main symptom is
not exiting from the sourced file when supposed to, leading to evaluation
of commands that were not supposed to be evaluated.  A secondary symptom
is that these extra commands are not evaluated correctly, as some of them
are skipped.  Some examples are listed in the How-To-Repeat section.

According to the POSIX standard, this is how it should work:
    dot:
        The shell shall execute commands from the file in the current
        environment.
    break:
        The break utility shall exit from the smallest enclosing for, while,
        or until loop, [...]
    continue:
        The continue utility shall return to the top of the smallest
        enclosing for, while, or until loop, [...]
    return:
        The return utility shall cause the shell to stop executing
        the current function or dot script.  If the shell is not currently
        executing a function or dot script, the results are unspecified.

It is clear that return should return from a sourced file, which
it does not do.  Whether break and continue should work from the sourced
file might be debatable.  Because the dot command says "in the current
environment", I'd say yes.  In any case, it should not fail in weird
ways like it does now!

The problems occur with return (a) and break/continue (b) because:
    1)  dotcmd() does not record the function nesting level prior to
        sourcing the file nor does it touch the loopnest variable,
        leading to either
    2   a) returncmd() being unable to detect that it should not set
           evalskip to SKIPFUNC but SKIPFILE, or
        b) breakcmd() setting evalskip to SKIPCONT or SKIPBREAK,
        leading to
    3)  cmdloop() not detecting that it should skip the rest of
        the file, due to only checking for SKIPFILE.
The result is that cmdloop() keeps executing lines from the file
whilst evalskip is set, which is the main symptom.  Because
evalskip is checked in multiple places in eval.c, the secondary
symptom appears.
>How-To-Repeat:
Run the following script:

    printf "break\necho break1; echo break2" >break
    printf "continue\necho continue1; echo continue2" >continue
    printf "return\necho return1; echo return2" >return

    while true; do . ./break; done

    for i in 1 2; do . ./continue; done

    func() {
        . ./return
    }
    func

No output should be produced, but instead this is the result:
    break1
    continue1
    continue1
    return1

The main symptom is evident from the unexpected output and the secondary
one from the fact that there are no lines with '2' in them.
>Fix:
Here is patch to src/bin/sh to fix the above problems.  It keeps
track of the function nesting level at the beginning of a dot command
to enable the return command to work properly.

I also changed the undefined-by-standard functionality of the return
command when it's not in a dot command or function from (indirectly)
exiting the shell to being silently ignored.  This was done because
the previous way has at least one bug: the shell exits without asking
for confirmation when there are stopped jobs.

Because I read the standard to mean that break and continue should have
an effect outside the sourced file, that's how I implemented it.  For what
it's worth, this also seems to be what bash does.  Also laziness, because
this way required no changes to loopnesting tracking.  If this is not
wanted, it might make sense to move the nesting tracking to the inputfile
stack.

The patch also does some clean-up to reduce the amount of global
variables by moving the dotcmd() and the find_dot_file() functions from
main.c to eval.c and making in_function() a proper function.
2014-05-31 14:42:18 +00:00
christos 4fc4fe2edf PR/45069: Henning Petersen: Use prototypes from builtins.h . 2011-06-18 21:18:46 +00:00
christos d464f08166 fix faulty logic in previous change. 2010-02-21 09:54:57 +00:00
christos 72f71b4cd6 default to the original behavior for $ENV unless POSIXLY_CORRECT is set. 2010-02-20 23:15:17 +00:00
christos 8752bdfe89 PR/42828: Richard Hansen: Don't evaluate ${ENV} if not interactive.
http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_05_03
2010-02-17 15:57:24 +00:00
lukem c6144e484f fix -Wsign-compare issues 2009-01-18 00:24:29 +00:00
dholland e54c6daca4 Remove unused global variables 2008-10-16 14:55:28 +00:00
lukem 2fe2731d3f Remove the \n and tabs from the __COPYRIGHT() strings. 2008-07-20 00:52:39 +00:00
christos 8d6ffce2ad PR/31548: Alexander Rigbo: $PWD not set when shell starts up, before pwd
or cd get executed.
2005-10-10 21:14:42 +00:00
christos 0df4f0ca31 Only do certain actions when we are trully interactive, not just when we
are connected to a tty:
	1. enable editor
	2. print directory on directory change.
	3. print job messages
2005-07-15 17:49:43 +00:00
jmmv af90286087 Fix crash when DEBUG=2, by opening the log file ASAP. Reviewed by simonb@. 2003-09-14 12:09:29 +00:00
agc b5b2954259 Move UCB-licensed code from 4-clause to 3-clause licence.
Patches provided by Joel Baker in PR 22249, verified by myself.
2003-08-07 09:05:01 +00:00
christos aa0e549885 PR/19453: Christopher Richards: Call setlocale() in /bin/sh 2002-12-11 19:12:18 +00:00
christos c02b3bbdf4 Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
  (so I could unset -e PS1 before running the test shell...)
2002-11-24 22:35:38 +00:00
christos 4783843fdc Revert previous change. No need to save rootshell. It is only affecting
the non-vfork case. Having said that, it would be nice if pipelines of
simple commands were vforked too. Right now they are not.
Explain that setpgid() might fail because we are doing it both in the
parent and the child case, because we don't know which one will come
first.
Suspending a pipeline prints %1 Suspended n times where n is the number
of processes, but that was there before. It is easy to fix, but I'll
leave the code alone for now.
2002-09-28 01:25:01 +00:00
christos 6f48233483 Deal with rootshell not being maintained correctly in the vfork() case.
Propagate isroot, throughout the eval process and maintain it properly.
Fixes sleep 10 | cat^C not exiting because sleep and cat ended up in
their own process groups, because wasroot was always true in the children.
2002-09-27 22:56:24 +00:00
christos edcb454443 VFork()ing shell: From elric@netbsd.org:
Plus my changes:
	- walking process group fix in foregrounding a job.
	- reset of process group in parent shell if interrupted before the wait.
	- move INTON lower in the dowait so that the job structure is
	  consistent.
	- error check all setpgid(), tcsetpgrp() calls.
	- eliminate unneeded strpgid() call.
	- check that we don't belong in the process group before we try to
	  set it.
2002-09-27 18:56:50 +00:00
wiz 40ac848024 Fix various misspellings of compatible/compatibility. 2001-06-11 01:50:48 +00:00
christos 3380980447 remove redundant declarations and nexted externs. 2001-02-04 19:52:06 +00:00
christos 8e2797bc1e PR/11283: Hubert Feyrer: random memory corruption executing commands:
Fix from FreeBSD:

    growstackblock() sometimes relocates a stack_block considered empty
    without properly relocating stack marks referencing that block.
    The first call to popstackmark() with the unrelocated stack mark
    as argument then causes sh to abort.

    Relocating the relevant stack marks seems to solve this problem.

    The patch changes the semantics of popstackmark() somewhat.  It can
    only be called once after a call to setstackmark(), thus cmdloop() in
    main.c needs an extra call to setstackmark().
2000-11-01 19:56:01 +00:00
elric e6bccfe4be Back out previous vfork changes. 2000-05-22 10:18:46 +00:00
elric 756a2ca1bd Now we use vfork(2) instead of fork(2) when we can. 2000-05-13 20:50:14 +00:00
simonb d2f6a4723d Include <errno.h> instead of using "extern int errno;". 2000-04-14 05:54:20 +00:00
christos d6ac7627ea Fix bin/9184, bin/9194, bin/9265, bin/9266
Exitcode and negation problems (From Martin Husemann)
2000-01-27 23:39:38 +00:00
christos 3d42469030 compile with WARNS = 2 1999-07-09 03:05:49 +00:00
christos 832949b912 When we execute commands from a shell script, make sure that the signals
are being caught (reported by Alexis Rosen), similar to the -c case.

#!/bin/sh
vi "$@"

^C when the script is running...
1999-03-27 13:46:19 +00:00
christos 1666f88d5d Fix the -c problem differently. We cannot just ignore SIGINT etc, otherwise
we cannot interrupt sh -c <command>
1999-02-05 21:21:27 +00:00
christos dd65252560 PR/1788: Shinya Taniguchi: sh -c does not setup signals properly.
[Reminded to me by Alexis Rosen -- thx!]
1999-02-04 11:20:40 +00:00
cjs 9063efaca0 Add -q option, which when used with -v and/or -x, turns off the tracing
during the execution of /etc/profile, .profile and $ENV.
1999-02-04 00:27:07 +00:00
itohy 362c3e31c9 The builtin . (dot) command no longer sees the current working directory
even if the specified file is not in the PATH.
This change enforces security and makes it conform to POSIX.

Closes PR #6794.
I thought Christos committed this but not appeared yet. :)

The setstackmark()/popstackmark() pair in dotcmd(), used for freeing
stack storage possibly allocated by find_dot_file(), is redundant for now
since dotcmd() is surrounded by another pair in evalcommand().
This redundancy, however, may help future modifications
(suggested by Christos).
1999-02-02 15:49:52 +00:00
mycroft ee9e50eacb Be more retentive about use of NOTREACHED and noreturn. 1998-07-28 11:41:40 +00:00
mycroft 9dc385beb1 Delint. 1998-07-28 05:31:22 +00:00
christos cd799663e4 Fix compiler warnings. 1997-07-04 21:01:48 +00:00
christos 23745b89db Don't call getpwd() because if that fails the shell cannot start. (from scott) 1997-04-11 23:01:44 +00:00
christos 2a9c11dd53 PR/287: Shell does not exit with 126/127 when permission denied/
command not found. Add extra exception type and generalize
	error handling routines to take that exception type. Use
	a global variable exerrno to keep the last exec error.
1996-10-16 14:35:42 +00:00
christos ff008dabf6 Fix PR/1760, where 'cd -' before any other command could cause a reference
to an uninitialized pointer. Use getcwd() to get the current working directory,
instead of forking /bin/pwd [per Scott's suggestion]
1995-11-19 23:27:37 +00:00
christos f9382bca1a Fix return builtin to work like it does in ksh:
When not in a function, it skips the rest of the current input file.
Instances of `return' outside function definitions were previously ignored.
What does joe posix have to say about this?
[fixes PR/1444]
1995-09-11 17:05:41 +00:00
christos 782cb68126 Fix bin/1240, from Dennis Ferguson. the exit builtin with no arguments
should return the exitcode of the previous command but it always returned 0.
1995-07-20 15:04:16 +00:00
christos 58e18fc330 Fixed bug where
$ cat foo
echo $FOO

$ FOO=1 . ./foo
$ echo $FOO

Did not echo 1 at all. Now variable assignments before sourced scripts
are honored during and after the execution of the script.
1995-05-28 18:09:48 +00:00
christos 4fb6f609aa Changed so that syntax errors (EXERROR) set the exit status to 2,
and commands that are not found set the exit status to 1 like all
other bourne shells.
[It used to be 0 and 2 respectively]
1995-05-19 15:08:55 +00:00
christos 07bae7eddd Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
1995-05-11 21:28:33 +00:00
cgd 49f0ad8601 convert to new RCS id conventions. 1995-03-21 09:01:59 +00:00
cgd 4ce0d34a31 clean up further. more patches from Jim Jegers 1994-12-05 19:07:32 +00:00
cgd 5dad143982 from James Jegers <jimj@miller.cs.uwm.edu>: quiet -Wall, and squelch
some of the worst style errors.
1994-12-04 07:11:37 +00:00
jtc 8cc67c8556 Don't source the file specified by $ENV if either the real and effective
user IDs or the real and effective group IDs of the process are different.
Thanks to Mark Weaver who pointed this out in PR 262.
1994-08-03 16:25:26 +00:00
mycroft cafd1f7e9f Add RCS ids. 1994-06-11 16:11:35 +00:00
jtc a81e412494 Include appropriate header files to bring function prototypes into scope. 1994-05-12 17:03:32 +00:00
jtc a3f60d8978 forgot some include files 1994-05-11 17:54:32 +00:00
jtc 52a20605b9 integrate NetBSD's path searching for dot files 1994-05-11 17:47:12 +00:00