ksh function (not posix) trap not receiving signals -HUP, -TERM but does receive -INT

Solution Verified - Updated -

Environment

  • Red Hat Enterprise Linux (RHEL) 6.8, 7.4
  • ksh

Issue

Using ksh functions (not posix), the first function mainTool sets a trap for a series of signals (trap logToolInterrupt HUP INT ABRT SEGV TERM and trap logToolExit EXIT) in the parent function of a script (mainTool) . Function mainTool then calls the user defined function toolMain which calls whatever functions the use defines. When the -TERM signal is sent to such a function, it is not delivered to the script in such a way at to cause the trap functions to be called. In fact the script just continues to operate... the lowest function terminates but the script continues without ever calling the trap function. However if -INT is the signal it operates as expected.

I expect the function set in the mainTool to be called when the signal is sent regardless of the signal.

Here is the test script:

#!/bin/ksh

function otherfunc2 {
   echo otherfunc2 $1
   trap
   sleep 2
   echo otherfunc2 leaving
}

function otherfunc {
   echo otherfunc $1
   trap
   sleep 2
   otherfunc2 $1
   echo otherfunc leaving
}

function toolMain {
   echo 'toolMain'
   trap
   for i in {1..100}
   do
      otherfunc $i
   done
}


function logToolExit {
   printf "logToolExit hit\n"
}

function logToolInterrupt {
   printf "logToolInterrupt  hit\n"
}


function mainTool {
trap logToolInterrupt HUP INT ABRT SEGV TERM
trap logToolExit EXIT
toolMain
}

mainTool

In the following run -HUP and -TERM stop the function that is active, but the trap function is never triggered...

When -INT is sent, the signal operates as expected and the trap function is called. It should always trigger the trap function regardless of signal as they were all registered.

$  /tmp/xx &
[2]     20433
toolMain
otherfunc 1
otherfunc2 1
otherfunc2 leaving
otherfunc leaving
otherfunc 2
otherfunc2 2
otherfunc2 leaving
otherfunc leaving
otherfunc 3
otherfunc2 3
otherfunc2 leaving
otherfunc leaving
otherfunc 4
otherfunc2 4
otherfunc2 leaving
otherfunc leaving
otherfunc 5
otherfunc2 5
otherfunc2 leaving
otherfunc leaving
otherfunc 6
$ kill -HUP 20433
otherfunc 7
otherfunc2 7
otherfunc2 leaving
otherfunc leaving
otherfunc 8
$ kill -HUP 20433
otherfunc 9
otherfunc2 9
otherfunc2 leaving
otherfunc leaving
otherfunc 10
otherfunc2 10
otherfunc2 leaving
otherfunc leaving
otherfunc 11
otherfunc2 11
$ kill -TERM 20433
otherfunc leaving
otherfunc 12
$ kill -TERM 20433
otherfunc 13
otherfunc2 13
otherfunc2 leaving
otherfunc leaving
otherfunc 14
otherfunc2 14
otherfunc2 leaving
otherfunc leaving
otherfunc 15
otherfunc2 15
otherfunc2 leaving
otherfunc leaving
otherfunc 16
$ kill -INT 20433 
logToolInterrupt  hit
logToolExit hit
[2] +                           /tmp/xx  &

This used to work back in RHEL 6.3-4.

Resolution

Update ksh to
- ksh-20120801-36.el6_9 released with Advisory RHBA-2017:3205 or newer for RHEL 6
- ksh-20120801-137.el7 released with Advisory RHBA-2018:0801 or newer for RHEL 7

The updated packages introduce an environment variable _AST_KSH_SIGNAL_BUBBLE that can be used to mimick old behavior. This variable is turned off by default and it has to be explicitly set to get the old behavior.

Root Cause

The behaviour change was introduced between ksh-20100621 and ksh-20120801 and not explained in ksh upstream mailing lists.

  • Component
  • ksh

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments