
    \d                         d Z ddlZddlZddlZddlZddlZddlmZmZm	Z	 ddl
mZmZmZ ddlmZ edk    rddlmZmZ d	 Zd
 Z G d de          Z e	e           G d dej                              Z G d dej                  Z e	e           G d de                      Zedk    reZneZ G d de          ZdS )a9  
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>}.

From Python version 2.6, C{signal.siginterrupt} and C{signal.set_wakeup_fd}
provide the necessary C signal handler which writes to the pipe to be
registered with C{SA_RESTART}.
    N)	Attribute	Interfaceimplementer)failurelogutil)platformTypeposix   )fdescprocessc                    | dk    r*t          j         t           j        t           j                   nAd }t          j         t           j        |           t          j        t           j        d           t          j        |           S )a  
    Install a signal handler which will write a byte to C{fd} when
    I{SIGCHLD} is received.

    This is implemented by installing a SIGCHLD handler that does nothing,
    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}
    c                      d S N )argss    9lib/python3.11/site-packages/twisted/internet/_signals.pynoopSignalHandlerz)installHandler.<locals>.noopSignalHandlerD   s    D    F)signalSIGCHLDSIG_DFLsiginterruptset_wakeup_fd)fdr   s     r   installHandlerr   3   sr     
Rxxfnfn5555	 	 	 	fn&7888FNE222###r   c                  Z    t          j        t           j                  t           j        k    S )zI
    Determine whether the I{SIGCHLD} handler is the default or not.
    )r   	getsignalr   r   r   r   r   isDefaultHandlerr    L   s     FN++v~==r   c                   N    e Zd ZdZ ed          Zd Zd Zdej	        ddfdZ
dS )	_IWakeraQ  
    Interface to wake up the event loop based on the self-pipe trick.

    The U{I{self-pipe trick}<http://cr.yp.to/docs/selfpipe.html>}, used to wake
    up the main loop from another thread or a signal handler.
    This is why we have wakeUp together with doRead

    This is used by threads or signals to wake up the event loop.
     c                      dS )z:
        Called when the event should be wake up.
        Nr   r   r   r   wakeUpz_IWaker.wakeUp`         r   c                      dS )zC
        Read some data from my connection and discard it.
        Nr   r   r   r   doReadz_IWaker.doReade   r&   r   reasonreturnNc                     dS )zB
        Called when connection was closed and the pipes.
        Nr   )r)   s    r   connectionLostz_IWaker.connectionLostj   r&   r   )__name__
__module____qualname____doc__r   disconnectedr%   r(   r   Failurer,   r   r   r   r"   r"   S   so          9R==L  
  
w 4      r   r"   c                   .    e Zd ZdZdZd Zd Zd Zd ZdS )_SocketWakerz
    The I{self-pipe trick<http://cr.yp.to/docs/selfpipe.html>}, implemented
    using a pair of sockets rather than pipes (due to the lack of support in
    select() on Windows for pipes), used to wake up the main loop from
    another thread.
    r   c                    || _         t          j        t          j        t          j                  }|                    t          j        t          j        d           t          j        t          j        t          j        t          j                            5 }|	                    d           |
                    d           |                    |                                           |                                \  }}ddd           n# 1 swxY w Y   |                    d           |                    d           || _        || _        | j        j        | _        dS )Initialize.r   )z	127.0.0.1r   Nr   )reactorsocketAF_INETSOCK_STREAM
setsockoptIPPROTO_TCPTCP_NODELAY
contextlibclosingbindlistenconnectgetsocknameacceptsetblockingrwfileno)selfr7   clientserverreader
clientaddrs         r   __init__z_SocketWaker.__init__{   sS   v~v/ABB&,f.@!DDDM&.&*<==
 
 	1KK()))MM!NN6--//000!'FJ	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	11fms   A)DDDc                     	 t          j        | j        j        d           dS # t          $ r'}|j        d         t          j        k    r Y d}~dS d}~ww xY w)zSend a byte to my connection.   xr   N)r   untilConcludesrG   sendOSErrorr   errnoWSAEWOULDBLOCKrI   es     r   r%   z_SocketWaker.wakeUp   sn    	T22222 	 	 	vayE000 100000	s   # 
AAAc                 ^    	 | j                             d           dS # t          $ r Y dS w xY w)z4
        Read some data from my connection.
        i    N)rF   recvrS   rI   s    r   r(   z_SocketWaker.doRead   sC    	FKK 	 	 	DD	s    
,,c                 j    | j                                          | j                                         d S r   )rF   closerG   )rI   r)   s     r   r,   z_SocketWaker.connectionLost   s$    r   N)	r-   r.   r/   r0   r1   rN   r%   r(   r,   r   r   r   r4   r4   p   sa          L$ $ $&        r   r4   c                   0    e Zd ZdZdZdZdZd Zd Zd Z	dS )_FDWakera  
    The I{self-pipe trick<http://cr.yp.to/docs/selfpipe.html>}, used to wake
    up the main loop from another thread or a signal handler.

    L{_FDWaker} is a base class for waker implementations based on
    writing to a pipe being monitored by the reactor.

    @ivar o: The file descriptor for the end of the pipe which can be
        written to wake up a reactor monitoring this waker.

    @ivar i: The file descriptor which should be monitored in order to
        be awoken by this waker.
    r   Nc                 2    | _         t          j                    \   _         _        t          j         j                   t          j         j                   t          j         j                   t          j         j                    fd _        dS )r6   c                       j         S r   )irZ   s   r   <lambda>z#_FDWaker.__init__.<locals>.<lambda>   s	    df r   N)	r7   ospipera   or   setNonBlocking_setCloseOnExecrH   rI   r7   s   ` r   rN   z_FDWaker.__init__   sz    TV$$$df%%%TV$$$df%%%$nnnr   c                 V    t          j        |                                 d            dS )zA
        Read some bytes from the pipe and discard them.
        c                     d S r   r   )datas    r   rb   z!_FDWaker.doRead.<locals>.<lambda>   s    T r   N)r   
readFromFDrH   rZ   s    r   r(   z_FDWaker.doRead   s)     	(9(9:::::r   c                     t          | d          sdS | j        | j        fD ]'}	 t          j        |           # t
          $ r Y $w xY w| `| `dS )zClose both ends of my pipe.re   N)hasattrra   re   rc   r\   rS   )rI   r)   r   s      r   r,   z_FDWaker.connectionLost   sq    tS!! 	F&$&. 	 	B   FDFFFs   9
AA)
r-   r.   r/   r0   r1   ra   re   rN   r(   r,   r   r   r   r^   r^      s\          LAA% % %; ; ;	 	 	 	 	r   r^   c                       e Zd ZdZd ZdS )
_UnixWakerz
    This class provides a simple interface to wake up the event loop.

    This is used by threads or signals to wake up the event loop.
    c                     | j         V	 t          j        t          j        | j         d           dS # t
          $ r!}|j        t          j        k    r Y d}~dS d}~ww xY wdS )z)Write one byte to the pipe, and flush it.NrP   )re   r   rQ   rc   writerS   rT   EAGAINrV   s     r   r%   z_UnixWaker.wakeUp   s     6#BHdfd;;;;;    7el** +***** s   %0 
AAAN)r-   r.   r/   r0   r%   r   r   r   rp   rp      s-             r   rp   c                   *    e Zd ZdZd Zd Zd Zd ZdS )_SIGCHLDWakerzU
    L{_SIGCHLDWaker} can wake up a reactor whenever C{SIGCHLD} is
    received.
    c                 <    t                               | |           d S r   )r^   rN   rh   s     r   rN   z_SIGCHLDWaker.__init__   s    $(((((r   c                 .    t          | j                   dS )zJ
        Install the handler necessary to make this waker active.
        N)r   re   rZ   s    r   installz_SIGCHLDWaker.install   s     	tvr   c                 $    t          d           dS )zC
        Remove the handler which makes this waker active.
        r   N)r   rZ   s    r   	uninstallz_SIGCHLDWaker.uninstall   s     	rr   c                 `    t                               |            t          j                     dS )a  
        Having woken up the reactor in response to receipt of
        C{SIGCHLD}, reap the process which exited.

        This is called whenever the reactor notices the waker pipe is
        writeable, which happens soon after any call to the C{wakeUp}
        method.
        N)r^   r(   r   reapAllProcessesrZ   s    r   r(   z_SIGCHLDWaker.doRead  s+     	 """""r   N)r-   r.   r/   r0   rN   rx   rz   r(   r   r   r   ru   ru      sZ         
) ) )    
# 
# 
# 
# 
#r   ru   )r0   r>   rT   rc   r   r8   zope.interfacer   r   r   twisted.pythonr   r   r   twisted.python.runtimer	   r#   r   r   r   r    r"   Loggerr4   r^   rp   _Wakerru   r   r   r   <module>r      s  
 >      				   < < < < < < < < < < - - - - - - - - - - / / / / / /7        $ $ $2> > >    i   : W0 0 0 0 03: 0 0 0f- - - - -sz - - -` W       * 7FF F# # # # #H # # # # #r   