
[XMc           @   s  d  Z  d d l Z y d d l m Z m Z Wn e k
 rI d Z Z n Xy d d l Z Wn e k
 rs d Z n Xd d l m Z y d d l	 m
 Z m Z Wn e k
 r d Z Z n Xd e f d     YZ d   Z d	   Z d
   Z d   Z d   Z e d k	 r$e d  e Z
 e Z n` e d k	 rIe d  e Z
 e Z n; e d k	 rne d  e Z
 e Z n e d  e Z
 e Z d S(   s  
This module provides a uniform interface to the several mechanisms which are
possibly available for dealing with signals.

This module is used to integrate child process termination into a
reactor event loop.  This is a challenging feature to provide because
most platforms indicate process termination via SIGCHLD and do not
provide a way to wait for that signal and arbitrary I/O events at the
same time.  The naive implementation involves installing a Python
SIGCHLD handler; unfortunately this leads to other syscalls being
interrupted (whenever SIGCHLD is received) and failing with EINTR
(which almost no one is prepared to handle).  This interruption can be
disabled via siginterrupt(2) (or one of the equivalent mechanisms);
however, if the SIGCHLD is delivered by the platform to a non-main
thread (not a common occurrence, but difficult to prove impossible),
the main thread (waiting on select() or another event notification
API) may not wake up leading to an arbitrary delay before the child
termination is noticed.

The basic solution to all these issues involves enabling SA_RESTART
(ie, disabling system call interruption) and registering a C signal
handler which writes a byte to a pipe.  The other end of the pipe is
registered with the event loop, allowing it to wake up shortly after
SIGCHLD is received.  See L{twisted.internet.posixbase._SIGCHLDWaker}
for the implementation of the event loop side of this solution.  The
use of a pipe this way is known as the U{self-pipe
trick<http://cr.yp.to/docs/selfpipe.html>}.

The actual solution implemented in this module depends on the version
of Python.  From version 2.6, C{signal.siginterrupt} and
C{signal.set_wakeup_fd} allow the necessary C signal handler which
writes to the pipe to be registered with C{SA_RESTART}.  Prior to 2.6,
the L{twisted.internet._sigchld} extension module provides similar
functionality.

If neither of these is available, a Python signal handler is used
instead.  This is essentially the naive solution mentioned above and
has the problems described there.
iN(   t   set_wakeup_fdt   siginterrupt(   t   msg(   t   installHandlert   isDefaultHandlert   _Handlerc           B   s    e  Z d  Z d   Z d   Z RS(   s   
    L{_Handler} is a signal handler which writes a byte to a file descriptor
    whenever it is invoked.

    @ivar fd: The file descriptor to which to write.  If this is C{None},
        nothing will be written.
    c         C   s   | |  _  d  S(   N(   t   fd(   t   selfR   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/internet/_signals.pyt   __init__J   s    c         G   s7   |  j  d k	 r3 y t j |  j  d  Wq3 q3 Xn  d S(   s   
        L{_Handler.__call__} is the signal handler.  It will write a byte to
        the wrapped file descriptor, if there is one.
        t    N(   R   t   Nonet   ost   write(   R   t   args(    (    s=   /usr/lib/python2.7/dist-packages/twisted/internet/_signals.pyt   __call__N   s
    (   t   __name__t
   __module__t   __doc__R   R   (    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/internet/_signals.pyR   B   s   	c         C   s\   |  d k r' t  j  t  j t  j  } n t  j  t  j t |    } t | t  rX | j Sd S(   sL  
    Install a signal handler which will write a byte to C{fd} when
    I{SIGCHLD} is received.

    This is implemented by creating an instance of L{_Handler} with C{fd}
    and installing it as the signal handler.

    @param fd: The file descriptor to which to write when I{SIGCHLD} is
        received.
    @type fd: C{int}
    i(   t   signalt   SIGCHLDt   SIG_DFLR   t
   isinstanceR   (   R   t   previous(    (    s=   /usr/lib/python2.7/dist-packages/twisted/internet/_signals.pyt   _installHandlerUsingSignal[   s    c         C   sX   |  d k r% t  j  t  j t  j  n) t  j  t  j t d   t t  j t  t |   S(   s  
    Install a signal handler which will write a byte to C{fd} when
    I{SIGCHLD} is received.

    This is implemented by installing an instance of L{_Handler} wrapped
    around C{None}, setting the I{SIGCHLD} handler as not allowed to
    interrupt system calls, and using L{signal.set_wakeup_fd} to do the
    actual writing.

    @param fd: The file descriptor to which to write when I{SIGCHLD} is
        received.
    @type fd: C{int}
    iN(   R   R   R   R   R
   R   t   FalseR    (   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/internet/_signals.pyt   _installHandlerUsingSetWakeupq   s
    c           C   s   t  j t  j  t  j k S(   sI   
    Determine whether the I{SIGCHLD} handler is the default or not.
    (   R   t	   getsignalR   R   (    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/internet/_signals.pyt   _isDefaultHandler   s    c         C   s   t  d   d S(   s  
    Fail to install a signal handler for I{SIGCHLD}.

    This implementation is used when the supporting code for the other
    implementations is unavailable (on Python versions 2.5 and older where
    neither the L{twisted.internet._sigchld} extension nor the standard
    L{signal} module is available).

    @param fd: Ignored; only for compatibility with the other
        implementations of this interface.

    @raise RuntimeError: Always raised to indicate no I{SIGCHLD} handler can
        be installed.
    s    Cannot install a SIGCHLD handlerN(   t   RuntimeError(   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/internet/_signals.pyt   _cannotInstallHandler   s    c           C   s   t  d   d  S(   Ns   No usable signal API available(   R   (    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/internet/_signals.pyt   _cannotDetermineDefault   s    s   using set_wakeup_fds   using _sigchlds   using signal modules   nothing unavailable(   R   R   R   R    R   t   ImportErrorR
   t   twisted.python.logR   t   twisted.internet._sigchldR   t   _extInstallHandlerR   t   _extIsDefaultHandlert   objectR   R   R   R   R   R   (    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/internet/_signals.pyt   <module>+   sF   
					
	
	
	
