KSH and BASH return different exit codes from same program for SIGSEGV
Here is the C program:
#include <stdio.h> #include <stdlib.h> #include <sys/signal.h> #include <string.h> #include <sys/errno.h> int main(int argc, char **argv) { pid_t pid; pid=getpid(); kill(pid, SIGSEGV); exit(0); } Here is the script and the output: #!/bin/ksh test.xec ecode=$? echo "trapped exit code $ecode" exit $ecode test.sh: line 2: 22557: Memory fault(coredump) trapped exit code 267 Memory fault(coredump) This resulted in KSH crashing. If I change the first line to #!/bin/bash and run it again, I get: test.sh: line 3: 24296 Segmentation fault (core dumped) test.xec trapped exit code 139 BASH appears to behave as expected. I am running on CentOS 6.5. /bin/ksh points to /etc/alternatives/ksh. I tried using the /bin/ksh93, but it behaved the same as /bin/ksh - and resulted in ksh93 crashing. Is this a BUG is ksh?? |
What ksh behavior do you suspect being a bug and why?
|
Quote:
2) A crash report for KSH is created after a command exits with exit code 267 - the shell shouldn't (in my opinion) crash due to the exit code returned by the command it was running. |
For extra info on this, i did this:
kshtest.sh Code:
#!/bin/ksh Code:
for i in {259..300}; do sed -i '/exit/d' kshtest.sh; echo "exit $i" >> kshtest.sh; ./kshtest.sh; done Code:
Quit Which leads me to believe you are exiting while sending a 'segmentation fault' signal. "Out of range exit values can result in unexpected exit codes. An exit value greater than 255 returns an exit code modulo 256. For example, exit 3809 gives an exit code of 225 (3809 % 256 = 225)." So 267 % 256 is ... you guessed it ... 11, which is the Segmentation Fault signal. |
Quote:
The shell standard (POSIX) says Quote:
The bash manual page states: Quote:
bash behaves as documented. The ksh93 manual page states: Quote:
No bugs on this side, both shells are POSIX compliant and follow their documented behavior. Quote:
If you look again to the POSIX standard, exit builtin, you can read: Quote:
Quote:
Finally, you suspect ksh shouldn't crash even when one of its builtin is given a out of range argument. This is a reasonable assumption but not shared by ksh implementors who decided to allow propagating the command exception to the shell itself. They were free to do it as it doesn't break the POSIX standard and gives ksh the same feature you used in your C program, a feature that bash is AFAIK missing. I guess you'll agree that your C program, even while its return status shows it got a signal, is not buggy and is behaving as designed. There is no reason not to allow a shell to support such a feature. |
Quote:
|
Quote:
|
It does crash by design, or expressed differently, it doesn't really crash.
It simply happens to behave like it would have should it had really crashed. No actual segmentation violation did happen in your C program but it reports a segmentation violation, the very same situation happens with ksh. You told ksh to return the exit status of a command (exit $ecode) but this command had no exit status. A segmentation violation has been reported instead to the shell. The shell is propagating this information to its caller, a useful ksh feature bash is to the best of my knowledge unable to achieve. |
Quote:
#!/bin/ksh test.xec ecode=$? echo "trapped exit code $ecode" exit $ecode I get the output: test.sh: line 2: 31748: Memory fault(coredump) trapped exit code 267 Memory fault(coredump) AND the ABRT kicks in and generates a crash report for KSH. However, if I run: #!/bin/ksh test.xec ecode=$? echo "trapped exit code $ecode" if [[ $ecode -gt 255 ]]; then echo changing ecode let "ecode = $ecode - 128" echo "ecode now $ecode" fi exit $ecode I get the output: test1.sh: line 2: 1015: Memory fault(coredump) trapped exit code 267 changing ecode ecode now 139 THIS is what I think is NORMAL behaviour - there is no ABRT crash report generated, there is only ONE Memory fault message (which I assume comes from the executable that created the SIGSEGV), AND ksh exits with the expected exit code. Since the program is the same, KSH cannot seem to successfully exit with the same exit code which it genereated as a result of the SIGSEGV (ie 267) and causes it to crash. I ran another test by wrapping the test1.sh script with yet another script. You are correct - KSH does NOT CRASH - however it does create a crash report indicating that it crashed - which is what confused me. So now my question is how come it behaves differently when exit codes 267 and 139 are both SIGSEGV, why does one (267) create a crash report, and the other (139) does not? Semi-solved |
Quote:
Quote:
Quote:
|
All times are GMT -5. The time now is 02:34 PM. |