o
    e                     @   s   d dl Z d dlZd dlZd dlZd dlZg aejdZdd Z	dd Z
dai add	 Zd d
dZdd Zdd Zdd Zdd Zdd Zdd Zdd Zdadd Zdadd Zdd ZdS )!    Nzfatal-signalc                 C   s   t   t| ||f d S N)_init_hooksappendhookcancelrun_at_exit r
   2/usr/lib/python3/dist-packages/ovs/fatal_signal.pyadd_hook   s   r   c                  C   s"   t D ]
\} }}|r|  qg a dS )a  Clears all of the fatal signal hooks without executing them.  If any of
    the hooks passed a 'cancel' function to add_hook(), then those functions
    will be called, allowing them to free resources, etc.

    Following a fork, one of the resulting processes can call this function to
    allow it to terminate without calling the hooks registered before calling
    this function.  New hooks registered after calling this function will take
    effect normally.N)r   r   r
   r
   r   fork   s
   
r   Fc                 C   s    t s
da tttd dt| < dS )zaRegisters 'file' to be unlinked when the program terminates via
    sys.exit() or a fatal signal.TN_added_hookr   _unlink_files_cancel_files_filesfiler
   r
   r   add_file_to_unlink4   s   r   c                 C   s    t s
da tttd |t| < dS )zRegisters 'file' to be unlinked when the program terminates via
    sys.exit() or a fatal signal and the 'fd' to be closed. On Windows a file
    cannot be removed while it is open for writing.TNr   )r   fdr
   r
   r   add_file_to_close_and_unlink>   s   r   c                 C   s   | t v r	t | = dS dS )zhUnregisters 'file' from being unlinked when the program terminates via
    sys.exit() or a fatal signal.N)r   r   r
   r
   r   remove_file_to_unlinkI   s   
r   c                 C   s0   t | }|rtd| t|f  t|  |S )zLike fatal_signal_remove_file_to_unlink(), but also unlinks 'file'.
    Returns 0 if successful, otherwise a positive errno value.zcould not unlink "%s" (%s))_unlinkvlogwarnosstrerrorr   )r   errorr
   r
   r   unlink_file_nowP   s
   r   c                  C   s4   t D ]} tjdkrt |  rt |    t|  qd S )Nwin32)r   sysplatformcloser   )file_r
   r
   r   r   Z   s
   
r   c                   C   s   da i ad S )NF)r   r   r
   r
   r
   r   r   a   s   r   c              
   C   s<   zt |  W dS  ty } z	|jW  Y d }~S d }~ww Nr   )r   unlinkOSErrorerrno)r$   er
   r
   r   r   h   s   
r   c                 C   s*   t |  t| tj tt |  d S r   )_call_hookssignalSIG_DFLr   killgetpid)signr_r
   r
   r   _signal_handlerp   s   r1   c                   C   s   t d d S r%   )r*   r
   r
   r
   r   _atexit_handlery   s   r2   c                 C   s2   t rd S da tD ]\}}}| dks|r|  qd S )NTr   )recurser   )r/   r   r   r	   r
   r
   r   r*      s   r*   c                  C   sl   t s4da tjdkrtjtjg} n
tjtjtjtjg} | D ]}t|tj	kr,t|t
 qtt d S d S )NTr    )_initedr!   r"   r+   SIGTERMSIGINTSIGHUPSIGALRM	getsignalr,   r1   atexitregisterr2   )signalsr/   r
   r
   r   r      s   
r   c                    sx   | st jd}|rt|} | sd S tjdkr5dd ldd l}G  fddd|j  | }|	  d S t
|  d S )NOVS_CTL_TIMEOUTr    r   c                       s*   e Zd Z fddZfddZ  ZS )zsignal_alarm.<locals>.Alarmc                    s"   t  |   || _| d d S )NT)super__init__timeout	setDaemon)selfr@   )Alarm	__class__r
   r   r?      s   z$signal_alarm.<locals>.Alarm.__init__c                    s     | j td d S )N   )sleepr@   r   _exit)rB   )timer
   r   run   s   zsignal_alarm.<locals>.Alarm.run)__name__
__module____qualname__r?   rI   __classcell__r
   rC   rH   )rD   r   rC      s    rC   )r   environgetintr!   r"   rH   	threadingThreadstartr+   alarm)r@   env_timeoutrR   rU   r
   rN   r   signal_alarm   s   

rW   r   )r:   r   r+   r!   ovs.vlogovsr   r   Vlogr   r   r   r   r   r   r   r   r   r   r   r1   r2   r3   r*   r4   r   rW   r
   r
   r
   r   <module>   s2   


	