Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Implementing signals for Win32 Perl using named pipes

by BrowserUk (Patriarch)
on Mar 11, 2008 at 10:08 UTC ( [id://673442]=note: print w/replies, xml ) Need Help??


in reply to Implementing signals for Win32 Perl using named pipes

Win32 Perl processes already have an asynchronous, interprocess, queuing mechanism. Its called the process queue.

It is how the few signals currently implemented are done. Its great advantage over any other mechanism is that the system already delivers some signals this way. ^C, ^Break, asynchronous timers, etc. It could be extended to emulate (some) other signals. A good first step would be to not convert all but four signals, (BREAK, INT, QUIT & TERM), sent to a process, into untrappable, fatal ones.

How far you could go with emulating signals is an open question. On *nix, many of them are raised by the system itself. For example, it would be possible to have another perl process signal its parent (SIGCHLD) when it terminates. But it's extremely difficult to arrange this for any non-perl child process.

It might be possible to inject code into the non-perl child process, but only if the required privileges are available. In many cases they would not be, which makes for a consistency problem. Even when the privileges are available, code injection is hard to get right for arbitrary processes. And, in many cases it could be seen as a security risk that opened the door to abuse.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
  • Comment on Re: Implementing signals for Win32 Perl using named pipes

Replies are listed 'Best First'.
Re^2: Implementing signals for Win32 Perl using named pipes
by Corion (Patriarch) on Mar 11, 2008 at 10:27 UTC

    Thanks - this is just the information on Windows that I hoped to get from Perlmonks! I wasn't aware that Windows has a process (message) queue. So far, I was under the impression that for a message queue you always needed a window handle.

    As for sending "signals" from non-Perl processes, I think this is a lesser priority, because any non-Perl process can always use the moral equivalent of

    system('perl','-e',"kill SIGWHATEVER => $pid")
    to send a signal to a Perl process. I think code injection is an interesting toy but far too unreliable/difficult if you want to use IPC. If the easier alternative is shared files, code injection does not look good :).

      I wasn't aware that Windows has a process (message) queue. So far, I was under the impression that for a message queue you always needed a window handle.

      Um. I get a little fuzzy with my terminology. Threads can(*) have message queues, not processes. Of course, in a single threaded process, the two are (roughly) equivalent.

      (*)From PostThreadMessage(): "The system creates a thread's message queue when the thread makes its first call to one of the User or GDI functions.".

      In Perl, this (probably) occurs when Win32_create_message_window() is called:

      ## win32.c HWND win32_create_message_window() { /* "message-only" windows have been implemented in Windows 2000 an +d later. * On earlier versions we'll continue to post messages to a specif +ic * thread and use hwnd==NULL. This is brittle when either an embe +dding * application or an XS module is also posting messages to hwnd=NU +LL * because once removed from the queue they cannot be delivered to + the * "right" place with DispatchMessage() anymore, as there is no Wi +ndowProc * if there is no window handle. */ if (!IsWin2000()) return NULL; return CreateWindow("Static", "", 0, 0, 0, 0, 0, HWND_MESSAGE, 0, +0, NULL); }

      See also win32_kill() in the same file.

      If you use the fork emulation, to start a non-perl, child process, then a thread is spawned to act as a 'placeholder' for the actual process. This then waits for the alien process to terminate and so can (could?) post (raise) a SIGCHLD to the main or spawning thread. The problem is that signals sent to the child pseudo-process' pseudo-pid do not necessarially reflect the state of, or affect, the real alien process. And that's where the emulation falls down.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        For the (imagined) central problem of sending an asynchronous message from outside the Perl process to the running Perl program, I think this emulation is sufficient. This at least brings the possibility to have some form of asynchronous communication between two Perl processes on Windows in a similar fashion as Unix.

        Also, I imagine a fun application of SA_SIGINFO could be a hook like this:

        $SIG{SIGINFO} = sub { my ($info) = @_; eval $info; };

        which basically gives you a detachable console to any Perl process.

Re^2: Implementing signals for Win32 Perl using named pipes
by cdarke (Prior) on Mar 11, 2008 at 12:20 UTC
    BREAK, CTRL-C, QUIT can be handled using a Control Handler. You can setup a handler function using the Win32 API SetConsoleCtrlHandler() and raise one of these "signals" using GenerateConsoleCtrlEvent().
    When a new process is created on Windows it can inherit the console of the parent, but this can be hidden (SW_HIDE). That way it is possible to emulate sending a signal to a "process group".

    Using a named pipe has its advantages, but would also need to be linked to exception handling as well, C0000005 generate a SIGSEGV? Emulating a SIGCHLD would be difficult, since there is no way to identify your parent, unless its PID is passed explicitly.

    I agree with the comments on injection, but if anyone wants to pursue that one I have some code which uses that technique in Win32::EnvProcess.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://673442]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-19 03:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found