summaryrefslogtreecommitdiffstats
path: root/bin/sh/jobs.c
Commit message (Collapse)AuthorAgeFilesLines
* MFC r272575: sh: Eliminate some gotos.jilles2015-09-131-5/+3
| | | | This MFC is to avoid conflicts in the MFC of r287148.
* MFC r263195: sh: Add some consts.jilles2014-04-051-4/+4
|
* Sync sh(1) in stable/10 to head.jmmv2014-03-091-2/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This is a MFC of all the commits listed below. My original goal of this change was to only merge the move of the tests from tools/regression/bin/ into the new layout (which include tests for sh(1) and other tools as well). However, doing so is tricky due to the ongoing work in sh(1) and, especially, the many changes to its tests since stable/10 was first branched. Merging everything is the simplest way to achieve this goal and, as a bonus point, we get various fixes and miscellaneous improvements into the branch. Per jilles' suggestion, I'm avoiding the merge of a couple of changes (r256850 and r257506) that required depending kernel changes. I'm also avoiding very recent changes that have not had a long enough time to be validated in current. This is "make tinderbox" clean. r256735 sh: Remove one syscall when waiting for a foreground job. r257399 sh: Allow trapping SIGINT/SIGQUIT after ignore because of '&'. r257504 sh: Reorder union node to reduce its size on 64-bit platforms. r257920 sh: Add a test case for would-be assignments that are not due to quoting. r257929 sh: Properly quote alias output from command -v. r258489 sh: Add tests for the </dev/null implicit in a background command. r258533 sh: Add more tests for the </dev/null implicit in a background command. r258535 sh: Make <&0 disable the </dev/null implicit in a background command. r258776 sh: Prefer memcpy() to strcpy() in most cases. Remove the scopy macro. r259047 sh: Split set -x output into a separate function. r259210 Migrate tools/regression/bin/ tests to the new layout. r259844 sh: Remove an unused variable. r259846 sh: Initialize OPTIND=1 even if it came from the environment. r259874 sh: Simplify code related to PPID variable. r259946 sh: Don't check input for non-whitespace if history is disabled. r260246 sh(1): Discourage use of -e. r260506 Run the sh(1) and test(1) tests as unprivileged. r260586 Mark the bin/pax tests as requiring perl. r260634 Use TAP_TESTS_PERL to register the legacy_test in bin/pax. r260635 Replace hand-crafted Kyuafiles with automatic generation. r260654 sh: Remove SIGWINCH handler and just check for resize before every read. r261121 sh: Add test for nested alias. r261125 sh: Solve the alias recursion problem in a less hackish way. r261141 sh: Do not depend on parse/execute split in new alias test. r261160 sh: Add tests for alias names after another alias. r261192 sh: Allow aliases to force alias substitution on the following word. r262533 sh: Make expari() static. r262565 sh: Do not corrupt internal representation if LINENO inner expansion fails. r262697 sh: Simplify expari(). Reviewed by: jilles
* sh: Fix race condition with signals and wait or set -T.jilles2013-09-021-17/+15
| | | | | | | | | | The change in r238888 was incomplete. It was still possible for a trapped signal to arrive before the shell went to sleep (sigsuspend()) because a check was missing or because the signal arrived before in_waitcmd was set. On SMP, this bug sometimes caused the builtins/wait4.0 test to take 1 second to execute; it then might or might not fail. On UP, the test almost always failed.
* sh: Do not prematurely discard stopped jobs in a wait builtin.jilles2013-08-241-5/+1
| | | | | | | | | | | | | If a job is specified to 'wait', wait for it to complete. Formerly, in interactive mode, the job was deleted if it stopped. If no jobs are specified in interactive mode, 'wait' still waits for all jobs to complete or stop. In non-interactive mode, WUNTRACED is not passed to wait3() so stopped jobs are not detected. PR: bin/181435
* sh: Recognize "--" as end of options in bg/fg/jobid builtins.jilles2013-08-161-6/+9
|
* sh: Remove #define MKINIT.jilles2013-07-251-3/+3
| | | | MKINIT only served for the removed mkinit. Many variables can be static now.
* sh: Return status 127 for unknown jobs in wait builtin.jilles2013-06-051-5/+19
| | | | | | | | | | This is required by POSIX, at least for pids that are not known child processes. Other problems with job specifications still cause wait to abort with exit status 2. PR: 176916
* sh: Allow multiple operands in wait builtin.jilles2013-06-051-6/+18
| | | | | | | | This is only part of the PR; the behaviour for unknown/invalid pids/jobs remains unchanged (aborts the builtin with status 2). PR: 176916 Submitted by: Vadim Goncharov
* sh: Use O_CLOEXEC and F_DUPFD_CLOEXEC instead of separate fcntl() call.jilles2013-05-051-8/+4
|
* sh: Don't consider jobs -s/-p as reporting the status of jobs.jilles2013-04-271-7/+9
| | | | | This ensures that something like j=$(jobs -p) does not prevent any status from being written to the terminal.
* sh: Fix various compiler warnings.jilles2013-04-011-2/+3
| | | | | | | It now passes WARNS=7 with clang on i386. GCC 4.2.1 does not understand setjmp() properly so will always trigger -Wuninitialized. I will not add the volatile keywords to suppress this.
* sh: Recognize "--" and explicitly reject options in wait builtin.jilles2013-03-151-3/+4
| | | | | | If syntactically invalid job identifiers are to be taken as jobs that exited with status 127, this should not apply to options, so that we can add options later if need be.
* sh: If a SIGINT or SIGQUIT interrupts "wait", return status 128+sig.jilles2013-02-231-1/+1
|
* Catch TRACE parameters up with r238888. This change is only needed whendelphij2013-02-071-1/+1
| | | | debugging is enabled.
* sh: Show negated commands (!) in jobs output.jilles2013-01-311-0/+4
|
* sh: Prefer strsignal() to accessing sys_siglist directly.jilles2012-12-251-8/+13
| | | | | | | | Accessing sys_siglist directly requires rtld to copy it from libc to the sh executable's BSS. Also, strsignal() will put in the signal number for unknown signals (FreeBSD-specific) so we need not do that ourselves. Unfortunately, there is no function for sys_signame.
* sh: Prefer internal nextopt() to libc getopt().jilles2012-09-151-11/+4
| | | | | | | | | This reduces code duplication and code size. /usr/bin/printf is not affected. Side effect: different error messages when certain builtins are passed invalid options.
* sh: Fix EINTR race condition in "wait" and "set -T" using sigsuspend().jilles2012-07-291-4/+40
| | | | | | | | | | | | | | | | | When waiting for child processes using "wait" or if "set -T" is in effect, a signal interrupts the wait. Make sure there is no window where the signal handler may be invoked (setting a flag) just before going to sleep. There is a similar race condition in the shell language, but scripts can avoid it by exiting from the trap handler or enforcing synchronization using a fifo. If SIGCHLD is not trapped, a signal handler must be installed for it. Only install this handler for the duration of the wait to avoid triggering unexpected [EINTR] errors elsewhere. Note that for some reason only SIGINT and SIGQUIT interrupt a "wait" command. This remains the case.
* sh: Do not ask for stopped/continued processes if we do not need themjilles2012-07-281-6/+5
| | | | rather than retrying wait3 if they happen.
* sh: Inline waitproc() into its only caller.jilles2012-07-281-22/+9
|
* sh: Track continued jobs (even if not continued by bg or fg).jilles2012-07-281-3/+8
| | | | | | | This uses wait3's WCONTINUED flag. There is no message for this. The change is visible in "jobs" or if the job stops again.
* sh: Remove unused variable in_dowait.jilles2012-07-151-3/+0
|
* sh: Fix build with -DDEBUG=2.jilles2012-04-021-2/+2
| | | | | Reported by: Kristof Provost MFC after: 1 week
* sh: Use vfork in a few common cases.jilles2012-02-041-0/+49
| | | | | | | | | | | | | | | | | This uses vfork() for simple commands and command substitutions containing a single simple command, invoking an external program under certain conditions (no redirections or variable assignments, non-interactive shell, no job control). These restrictions limit the amount of code executed in a vforked child. There is a large speedup (for example 35%) in microbenchmarks. The difference in buildkernel is smaller (for example 0.5%) but still statistically significant. See http://lists.freebsd.org/pipermail/freebsd-hackers/2012-January/037581.html for some numbers. The use of vfork() can be disabled by setting a variable named SH_DISABLE_VFORK.
* Add prototypes, ANSIfy functions definitions to reduce WARNS=6 output.charnier2012-01-251-0/+1
|
* sh: Fix duplicate prototypes for builtins.jilles2011-06-131-0/+1
| | | | | | Have mkbuiltins write the prototypes for the *cmd functions to builtins.h instead of builtins.c and include builtins.h in more .c files instead of duplicating prototypes for *cmd functions in other headers.
* sh: Save/restore changed variables in optimized command substitution.jilles2011-06-121-1/+3
| | | | | | | | | | | In optimized command substitution, save and restore any variables changed by expansions (${var=value} and $((var=assigned))), instead of trying to determine if an expansion may cause such changes. If $! is referenced in optimized command substitution, do not cause jobs to be remembered longer. This fixes $(jobs $!) again, simplifies the man page and shortens the code.
* sh: Reduce more needless differences between error messages.jilles2011-06-041-2/+2
|
* sh: Remove special code for shell scripts without magic number.jilles2011-02-041-16/+0
| | | | | | | | | | These are called "shell procedures" in the source. If execve() failed with [ENOEXEC], the shell would reinitialize itself and execute the program as a script. This requires a fair amount of code which is not frequently used (most scripts have a #! magic number). Therefore just execute a new instance of sh (_PATH_BSHELL) to run the script.
* sh: Send messages about signals to stderr.jilles2011-01-301-5/+5
| | | | | | This is required by POSIX and seems to make more sense. See also r217557.
* sh: Fix signal messages being sent to the wrong file sometimes.jilles2011-01-181-0/+1
| | | | | | | | | | | | | When a foreground job exits on a signal, a message is printed to stdout about this. The buffer was not flushed after this which could result in the message being written to the wrong file if the next command was a builtin and had stdout redirected. Example: sh -c 'kill -9 $$'; : > foo; echo FOO:; cat foo Reported by: gcooper MFC after: 1 week
* sh: Add kill builtin.jilles2010-12-211-0/+8
| | | | | | | | | | | | | | | | | This allows specifying a %job (which is equivalent to the corresponding process group). Additionally, it improves reliability of kill from sh in high-load situations and ensures "kill" finds the correct utility regardless of PATH, as required by POSIX (unless the undocumented %builtin mechanism is used). Side effect: fatal errors (any error other than kill(2) failure) now return exit status 2 instead of 1. (This is consistent with other sh builtins, but not in NetBSD.) Code size increases about 1K on i386. Obtained from: NetBSD
* sh: Various simplifications to jobs.c:jilles2010-12-121-11/+5
| | | | | | | | * Prefer kill(-X) to killpg(X). * Remove some dead code. * No additional SIGINT is needed if int_pending() is already true. No functional change is intended.
* sh: Improve internal-representation-to-text code to avoid binary output.jilles2010-12-061-3/+36
| | | | | | | | | | | The code to translate the internal representation to text did not know about various additions to the internal representation since the original ash and therefore wrote binary stuff to the terminal. The code is used in the jobs command and similar output. Note that the output is far from complete and mostly serves for recognition purposes.
* sh: POSIX says there should not be a space between Done and (exitstatus).jilles2010-12-051-1/+1
| | | | (On the other hand, (core dumped) does need a space and so does [1] +.)
* sh: Improve jobs output of pipelines.jilles2010-12-051-66/+83
| | | | | | | | | | | | | | If describing the status of a pipeline, write all elements of the pipeline and show the status of the last process (which would also end up in $?). Only write one report per job, not one for every process that exits. To keep some earlier behaviour, if any process started by the shell in a foreground job terminates because of a signal, write a message about the signal (at most one message per job, however). Also, do not write messages about signals in the wait builtin in non-interactive shells. Only true foreground jobs now write such messages (for example, "Terminated").
* sh: Avoid marking a job as done before it is fully created.jilles2010-12-051-2/+2
| | | | | | | | | In r208489, I added code to reap zombies when forking new processes, to limit the amount of zombies. However, this can lead to marking a job as done or stopped if it consists of multiple processes and the first process ends very quickly. Fix this by only checking for zombies before forking the first process of a job and not marking any jobs without processes as done or stopped.
* sh: jobs -p: Do not ask the kernel for the pgid.jilles2010-12-051-4/+1
| | | | | | | | The getpgid() call will fail if the first process in the job has already terminated, resulting in output of "-1". The pgid of a job is always the pid of the first process in the job and other code already relies on this.
* sh: Use <stddef.h> rather than <sys/stddef.h>.jilles2010-10-161-1/+1
| | | | <sys/stddef.h> is only for the kernel and conflicts with <stddef.h>.
* In the spirit of r90111, depend on c89 and remove the "STATIC" macroobrien2010-10-131-24/+24
| | | | and its usage.
* Make DEBUG traces 64-bit clean:jhb2010-10-131-13/+13
| | | | | | | | | - Use %t to print ptrdiff_t values. - Cast a ptrdiff_t value explicitly to int for a field width specifier. While here, sort includes. Submitted by: Garrett Cooper
* Consistently use "STATIC" for all functions in order to be able to setobrien2010-10-131-6/+6
| | | | | | breakpoints with in a debugger. And use naked "static" for variables. Noticed by: bde
* sh: Forget about terminated background processes sooner.jilles2010-06-291-5/+56
| | | | | | | | | | | | | | | | | | | | | | | | Unless $! has been referenced for a particular job or $! still contains that job's pid, forget about it after it has terminated. If $! has been referenced, remember the job until the wait builtin has reported its completion (either with the pid as parameter or without parameters). In interactive mode, jobs are forgotten after termination has been reported, which happens before primary prompts and through the jobs builtin. Even then, though, remember a job if $! has been referenced. This is similar to what is suggested by POSIX and should fix most memory leaks (which also tend to cause sh to use more CPU time) with long running scripts that start background jobs. Caveats: * Repeatedly referencing $! without ever doing 'wait', like while :; do foo & echo started foo: $!; sleep 60; done will still use a lot of memory and CPU time in the long run. * The jobs and jobid builtins do not cause a job to be remembered for longer like expanding $! does. PR: bin/55346
* sh: Pass through SIGINT from a child if interactive and job controljilles2010-06-061-0/+6
| | | | | | | | | | | | | | | is enabled. This already worked if without job control. In either case, this depends on it that a process that terminates due to SIGINT exits on it (so not with status 1, or worse, 0). Example: sleep 5; echo continued This does not print "continued" any more if sleep is aborted via ctrl+c. MFC after: 1 month
* sh: Reap any zombies before forking for a background command.jilles2010-05-241-1/+13
| | | | | | | | | | | | | | This prevents accumulating huge amounts of zombies if a script executes many background commands but no external commands or subshells. Note that zombies will not be reaped during long calculations (within the shell process) or read builtins, but those actions do not create more zombies. The terminated background commands will also still be remembered by the shell. PR: bin/55346
* sh: Various warning fixes (from WARNS=6 NO_WERROR=1):jilles2009-12-271-4/+5
| | | | | | | - const - initializations to silence -Wuninitialized (it was safe anyway) - remove nested extern declarations - rename "index" locals to "idx"
* sh: Do not run callers' exception handlers in subshells.jilles2009-12-251-0/+1
| | | | | | | Reset the exception handler in the child to main's. This avoids inappropriate double cleanups or shell duplication when the exception is caught, such as 'fc' and future 'command eval' and 'command .'.
* sh: Some changes to stderr flushing:jilles2009-11-211-2/+2
| | | | | | | | | * increase buffer size from 100 to 256 bytes * remove implied flush from out2str(), in particular this avoids unnecessary flushing in the middle of a -x tracing line * rename dprintf() to out2fmt_flush(), make it flush out2 and use this function in various places where flushing is desired after an error message
* Add the POSIX option -p to the jobs builtin command. It prints the PID of thestefanf2006-10-071-17/+23
| | | | | | | | process leader for each job. Now the last specified option for the output format (-l, -p or -s) wins, previously -s trumped -l. PR: 99926 Submitted by: Ed Schouten and novel (patches modified by me)
OpenPOWER on IntegriCloud