Bash – Wrapper program that sets signal handler

bashsignalssystem-callstrap:

I would like to have a wrapper program that runs a given command and sets a signal handler so that it gets run when the command receives a specified signal.

The question is this:

  1. Is there an utility program to do this?
  2. If not, is it possible to do this by using bash's commands trap and exec?
  3. If not, how can I do this? (e.g. by writing a program myself in C which does a few system calls)

EDIT: The target platform is GNU/Linux.

EDIT 2: Following Ignacio's answer, I managed to write a preload SO which looks like this.

Best Answer

Can't be done. From the exec(3p) man page:

Signals set to be caught by the calling process image shall be set to the default action in the new process image.

You would have to write a preload SO which would hook up the signal handlers before the program started.

Related Question