The theory is described here, but in my experience this doesn't work as advertised...

For example, this is what I get on the Linux box (SuSE 10.2) I'm sending this from:

#!/usr/local/perl/5.10.0/bin/perl use strict; use warnings; use POSIX; use Data::Dumper; $Data::Dumper::Useqq = 1; my $inf; my $sender_pid; POSIX::sigaction( SIGINT, POSIX::SigAction->new( sub { $inf = Dumper \@_; $sender_pid = unpack "x16S", $_[2]; # <-- }, 0, POSIX::SA_SIGINFO ), ); kill 'INT', $$; print $inf; print "Sender PID: $sender_pid\n"; print "Own PID: $$\n"; __END__ $VAR1 = [ "INT", { "code" => 0, "signo" => 2 }, "\2\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0RQ\0\0005\4\0\0\270\273}\0\ +0\0\0\0`\257i\0\0\0\0\0\3342E\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 +\0\370\25e\0\0\0\0\0\30 e\0\0\0\0\0\1\0\0\0\0\0\0\0\r\0\0\0\0\0\0\0\3 +60b8\213\377\177\0\0\370b8\213\377\177\0\0\1\0\0\0\0\0\0\0\3IM\0\0\0\ +0\0" ]; Sender PID: 20818 (as we're sending the signal ourself, the sender i +s $$) Own PID: 20818

Ideally - as I'm reading the docs - there should also be hash fields for 'pid' (the process id generating the signal), 'uid', etc. — not available in this case, though.

With a bit of luck (and reading through your system's siginfo.h — (e.g. /usr/include/asm-generic/siginfo.h on my system)), you might be able to extract the relevant info from the raw siginfo struct dump...

Update:  unpack "x16S", ... looks promising — at least in my case, it extracts the appropriate PID  (see $sender_pid in the above updated snippet).  But be sure to test its portability, before you make this a vital part of your application...! :)


In reply to Re: Signal Handler by almut
in thread Signal Handler by lakshmananindia

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.