o
    th                     @   s   U 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 d dlmZm	Z	m
Z
mZ d dlmZ d dlmZ eeZdZe
ed< dZe
ed	< ejd
ejdejdiZe
e	eef  ed< deeedf ddfddZdd Zdd Zdd Z dS )    N)StringIO)CallableDictFinalUnion)version)log_utilz#Cloud-init {} received {}, exiting
SIG_MESSAGE   BACK_FRAME_TRACE_DEPTHz/Cloud-init %(version)s received SIGINT, exitingz0Cloud-init %(version)s received SIGTERM, exitingz0Cloud-init %(version)s received SIGABRT, exitingSIGNALSsigreturnc                 C   s   t | rt| tjstd|  dS dS | tjkr td dS | du r+td dS | tjkr8td|  dS t	dt
| |  dS )z+inspect_handler() logs signal handler statez.Signal state [%s] - previously custom handler.z,Signal state [SIG_IGN] - previously ignored.Nz;Signal state [None] - previously not installed from Python.zISignal state [%s] - default way of handling signal was previously in use.zSignal state [%s(%s)] - unknown)callable
isinstancetypesBuiltinFunctionTypeLOGinfosignalSIG_IGNSIG_DFLwarningtype)r    r   :/usr/lib/python3/dist-packages/cloudinit/signal_handler.pyinspect_handler   s   

r   c                 C   sz   ||ks| sd S t | }d|d  }|d||jf  |d||jf  |d||jf  t| j|d || d S )N    z%sFilename: %s
z%sFunction: %s
z%sLine number: %s
   )inspectgetframeinfowritefilenamefunctionlineno_pprint_framef_back)framedepth	max_depthcontents
frame_infoprefixr   r   r   r&   5   s   
r&   c                 C   sh   t | tjr	| jn| }ttt |}t	|dt
| tjd| d|  ttjd td d S )Nr   zReceived signal z resulting in exit. Cause:
)log	log_levelr   )r   r   Signalsnamer   r	   formatvrversion_stringr&   r   r   	multi_loggetvaluer   loggingINFOsysexit)signumr(   r1   r+   r   r   r   _handle_exit@   s   
r<   c                  C   s2   d} t  D ]
}tt|t q| tt 7 } | S )zattach cloud-init's handlersr   )r   keysr   r   r<   len)sigs_attachedr;   r   r   r   attach_handlersN   s
   r@   )!r    r7   r   r9   r   ior   typingr   r   r   r   	cloudinitr   r3   cloudinit.logr   	getLogger__name__r   r	   __annotations__r   SIGINTSIGTERMSIGABRTr   intstrr   r&   r<   r@   r   r   r   r   <module>   s(   

