renodino has asked for the wisdom of the Perl Monks concerning the following question:

In need of some arcane Perl lore...

I'm creating an embedded Perl library for an environment which has its own set of signal handlers. I can't seem to find an explicit description of how to safely merge Perl's (v5.8) signal handling with that environment (nothing mentioned in perlembed or perlcall). The nearest search hit I've found is Apache::SIG, but I'm ignorant of mod_perl internals.

Does anyone know of a reference to describe how to deal with signal handlers in such an environment ? I.e.,

Update/Clarification:

I'm writing a library to install a Perl interpretter into an existing application framework (ala mod_perl). Said framework has its own set of signal handlers, some of which have very important functions to safely rollback the actions of plugins (such as I'm writing), esp. wrt cleaning up memory.

However, if I rely on the framework's current signal handlers, which usually do some heap cleanup then longjmp() back to a known state, its quite likely that my embedded Perl will be left in a confused state. So I'd like to be able to use Perl's "safe" signal handling (dispatching between opcodes), and then rollback safely in an "eval-ish" sortof way. After which, I can dispatch back to the original signal handler.

Hence, I need to understand how an embedded Perl is going to treat existing signal handlers.

Final Empirical Update:

After hacking up some code to verify, here's the results: (WinXP, AS Perl 5.8, using SIGINT, using simple sleep() and simple print/printf() signal handler):

  1. Signal handler installed via C before starting Perl: reverts to default Perl behavior (immediately terminates)
  2. Signal handler installed via embedded Perl script: behaves as for standalone Perl script, ie, defers signal until sleep() ends
  3. Signal handler installed via C after starting embedded Perl, then eval a sleep script in Perl: behaves as for (1)
So it appears embedded Perl does commandeer signals on creation, but doesn't tinker with them afterward unless the Perl script explicitly does so.

Note that (3) is probably dangerous, as it presumably is equivalent to unsafe signals, ie, Perl possibly left in a bad state. I'll probably have to borrow Perl's signal queueing code to restore sanity.

Replies are listed 'Best First'.
Re: Signal handling in embedded Perl ?
by jbert (Priest) on Nov 20, 2006 at 19:02 UTC
    Without knowing the details (so this probably isn't too helpful) I think the normal way of achieving this sort of goal is to chain things so that when you install a signal handler, you save the previous signal handler. When you've finished any handling you want to do, you invoke the saved, chained handler (passing in the same args you received).

    This scheme works even with one set of signal handlers which don't play nicely, if you can arrange for those to be installed first, all the chain-aware ones can be installed afterwards and the unfriendly ones can be called at the end of the chain.

    Sorry this is so vague, but routines for managing these things generally return the previous signal handler in order for you to do this. The old-style 'signal()' returns the previous handler, the POSIX sigaction takes an 'oldact' struct pointer to copy in the previous handling details (if requested)).

    Or are you saying that your platform lacks signal or sigaction? If so, can you mock up an equivalent using your platform's primitives?

      Just for example's sake, something like this?
      sub install_handler { my ($sig, $subref) = @_; my $original = $SIG{$sig} || sub{}; $SIG{$sig} = sub { $subref->(); $original->(); } }
        That would be the equivalent in perl, yes.

        But when embedding perl inside another program/platform, you'd be doing the same thing at the C level, and calling the POSIX sigaction() function (or its equivalent).

        (At least I think you would - I'd be very suprised if the existing value of $SIG{FOO} you stored in $original was a callable C function. But then, I've been surprised before).

        And being in C not perl, you wouldn't have the convenience of having a nice closure to capture your state, so you'd need to have a data structure (perhaps a global array indexed by signal number).