DaemonForums  

Go Back   DaemonForums > OpenBSD > OpenBSD General

OpenBSD General Other questions regarding OpenBSD which do not fit in any of the categories below.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 16th April 2021
phillbush's Avatar
phillbush phillbush is offline
Port Guard
 
Join Date: Apr 2020
Posts: 15
Default Possible bug in how OpenBSD ksh(1) handle the ERR trap

To help me identify whether a command fails and what is its exit code, I trap the ERR special signal to print the exit code of a command that returns nonzero.

Code:
trap 'printf "\e[1;31mEXIT: %s\e[0m\n" "$?"' ERR
You can try it in your shell.
Run this and then run `false` or `cat nonexistent/file.txt`.

However, OpenBSD pdksh behaves a little different from ksh93 and bash when the command terminates from a signal.

For example. Try this in bash(1) or ksh93(1): run the trap command and then run `cat` (without any arguments) and immediately type `^C` (ctrl+c) to send `SIGINT` to cat. The shell will print `EXIT: 130` (or `EXIT: 258`) right after the command is terminated and before the next prompt.

Try this again in OpenBSD ksh(1). The `EXIT: 130` string will not be printed immediately. Now enter a successful command, such as grep(1) or ls(1). NOW the error string will be printed.

This image illustrates what I am saying:

  • First, I'm on a ksh93(1) subshell.
    I run `false`, and it prints `EXIT: 1`.
    I run `cat` and type `^C` and it prints `EXIT 258`.
    I run `grep -V` and it prints nothing.
  • Then, I exit ksh93(1) and return to ksh(1).
    I run `false`, and it prints `EXIT: 1`.
    I run `cat` and type `^C` and it prints nothing.
    I run `grep -V` and it prints `EXIT 130`.

Is this a bug? If yes, is it known by the developers?
I think that it can be a problem in a script to not handle a ERR trap right after a command terminates from a signal.

Last edited by phillbush; 16th April 2021 at 09:49 PM.
Reply With Quote
  #2   (View Single Post)  
Old 16th April 2021
ibara ibara is offline
OpenBSD language porter
 
Join Date: Jan 2014
Posts: 783
Default

Probably want to report this to bugs@
Reply With Quote
  #3   (View Single Post)  
Old 16th April 2021
phillbush's Avatar
phillbush phillbush is offline
Port Guard
 
Join Date: Apr 2020
Posts: 15
Default

Quote:
Originally Posted by ibara View Post
Probably want to report this to bugs@
I want to certify that this is not happening only with me.
Someone on #openbsd IRC channel tried to replicate the bug and nothing happened... the "EXIT: 130" string was not printed after true(1).
Could you replicate the bug?
Reply With Quote
  #4   (View Single Post)  
Old 16th April 2021
ibara ibara is offline
OpenBSD language porter
 
Join Date: Jan 2014
Posts: 783
Default

On ksh(1):
false
EXIT: 1

cat
^C
(no output, but if you run echo $? you see 130)

true
(no output)
Reply With Quote
  #5   (View Single Post)  
Old 16th April 2021
phillbush's Avatar
phillbush phillbush is offline
Port Guard
 
Join Date: Apr 2020
Posts: 15
Default

Quote:
Originally Posted by ibara View Post
On ksh(1):
false
EXIT: 1

cat
^C
(no output, but if you run echo $? you see 130)

true
(no output)
What happens if you use a non-builtin (like ls(1)) in place of true?
Reply With Quote
  #6   (View Single Post)  
Old 17th April 2021
Head_on_a_Stick's Avatar
Head_on_a_Stick Head_on_a_Stick is offline
Real Name: Matthew
Bitchy Nerd Elitist
 
Join Date: Dec 2015
Location: London
Posts: 468
Default

Hopefully not off-topic but this is from Alpine Linux running ibara's port of OpenBSD's ksh(1):
Code:
~$ trap 'printf "\e[1;31mEXIT: %s\e[0m\n" "$?"' ERR
~$ false
EXIT: 1
1~$ cat
^C
130~$ ls
[snip]
EXIT: 0
~$
Note my prompt, which shows the exit status of the last command if it is non-zero:
Code:
~$ echo $PS1
\e[91m${?#0}\e[0m\w\e[97m$\e[0m
~$
__________________
Destruam et ædificabo
Reply With Quote
  #7   (View Single Post)  
Old 22nd April 2021
phillbush's Avatar
phillbush phillbush is offline
Port Guard
 
Join Date: Apr 2020
Posts: 15
Default

Just sent the bug to bugs@.
Reply With Quote
Reply

Tags
ksh, openbsd


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
openBSD 6.2 Problem kelnerage fault trap Code=0 pawkolor OpenBSD Installation and Upgrading 28 13th October 2017 07:52 PM
why does this simple assembly program abort trap? billy_bazooka Programming 1 26th July 2017 04:45 AM
virtualbox-ose exit code error philo_neo71 FreeBSD Ports and Packages 3 14th June 2017 08:38 PM
Identify rules to behaviour Droid OpenBSD Security 5 25th March 2012 05:58 PM
Scripted sysinstall fails with "Command 'system' failed" PeterSteele FreeBSD Installation and Upgrading 0 13th November 2008 11:31 PM


All times are GMT. The time now is 11:43 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick