
    xj                        U d dl mZ d dlZd dlZd dlZd dlmZmZ d dlm	Z	 d dl
mZ d:dZd;dZej        dk    rd dlZd dlmZ  ej        dd          Zded<   ej        gej        _        ej        ej        _        ej        ej        ej        gej        _        ej        ej        _        ej         ej        ej                   ej        ej                   ej        ej                   ej        ej                  gej        _        ej        ej        _        dZded<   dZded<   dZ ded<   dZ!ded <   d<d!Z"d=d"Z#nd<d#Z"ej        d$v rd%Z$ded&<   d>d'Z% e%            Z&ded(<   d=d)Z#ndej        d*k    rUd dlZd dl'Z' ej(        dd+          Z)d,ed-<   d.Z*ded/<   d0Z+ded1<   d.Z,ded2<   d3Z-d4ed5<    e'j.        e-          Z/ded6<   d=d7Z#nd=d8Z#g d9Z0dS )?    )annotationsN)EPERMESRCH)Path)Finalreturnstrc                 (    t          j                    S )zfThe hostname recorded alongside an owner, so a marker written on another machine is never probed here.)socketgethostname     X/home/agent/.hermes/hermes-agent/venv/lib/python3.11/site-packages/filelock/_identity.py	host_namer      s    r   pidinthostnamestart_token
int | Noneboolc                    |t                      k    rdS t          |           sdS |dS t          |           }|duo||k    S )a  
    Whether the recorded owner is provably gone, so reclaiming its marker cannot detach a live holder.

    Fail closed: return ``True`` only when this process can prove the exact recorded owner is dead. A marker from
    another host cannot be probed; a live PID whose start token still matches, or whose token cannot be read, is the
    holder or is indistinguishable from it; a live PID whose start token differs is a recycled PID, so the process that
    wrote the marker is gone. PostgreSQL, Qt ``QLockFile`` and Mercurial all break a stale lock only on proof of death
    and treat an unreadable or foreign owner as still holding.
    FTN)r   process_aliveprocess_start_token)r   r   r   currents       r   owner_is_staler      sZ     9;;u tu!#&&G$97k#99r   win32)wintypeskernel32T)use_last_errorzFinal[ctypes.WinDLL]	_KERNEL32i   z
Final[int]_WIN_SYNCHRONIZEi   &_WIN_PROCESS_QUERY_LIMITED_INFORMATIONW   _WIN_ERROR_INVALID_PARAMETERFzFinal[bool]_WIN_INHERIT_HANDLEc                    t                               t          t          |           }|rt                               |           dS t          j                    t          k    S )zSWhether a process with this PID exists, treating an access denial as proof it does.T)r    OpenProcessr!   r%   CloseHandlectypesget_last_errorr$   )r   handles     r   r   r   ;   sQ    &&'79LcRR 	!!&)))4$&&*FFFr   c           	     4   t                               t          t          |           }|sdS d t	          d          D             \  }}}}	 t                               |t          j        |          t          j        |          t          j        |          t          j        |                    s	 t                               |           dS 	 t                               |           n# t                               |           w xY w|j	        dz  |j
        z  S )zXThe process creation FILETIME as a 100ns tick count, or ``None`` when it cannot be read.Nc              3  <   K   | ]}t          j                    V  d S )N)r   FILETIME).0_s     r   	<genexpr>z&process_start_token.<locals>.<genexpr>H   s+      6]6]qx7H7J7J6]6]6]6]6]6]r          )r    r'   r"   r%   rangeGetProcessTimesr)   byrefr(   dwHighDateTimedwLowDateTime)r   r+   creation	exit_timekernel_time	user_times         r   r   r   C   s   &&'MObdghh 	46]6]TYZ[T\T\6]6]6]3)[)
	*,,X&&Y''[))Y''   !!&))))) !!&))))I!!&))))'2-1GGGs   
A&C) )Dc                    	 t          j        | d           n?# t          $ r2}|j        t          k    rY d}~dS |j        t
          k    rY d}~dS  d}~ww xY wdS )z_Whether a process with this PID exists, treating an access denial (``EPERM``) as proof it does.r   NFT)oskillOSErrorerrnor   r   )r   errors     r   r   r   X   su    	GCOOOO 	 	 	{e##uuuuu{e##ttttt	 ts    
AAAAA>   linuxandroid   _STARTTIME_INDEXc                     	 t          d                              d          } t          |                                                     dd          d          S # t
          t          f$ r Y dS w xY w)Nz/proc/sys/kernel/random/boot_idascii)encoding-    r   )r   	read_textr   stripreplacer@   
ValueError)boot_ids    r   _read_boot_idrR   k   sv    @AAKKU\K]]7==??223;;R@@@Z(   qqs   AA A0/A0_BOOT_IDc                ~   	 t          d|  d                                          }n# t          $ r Y dS w xY w||                    d          dz   d                                         }t          |          t          k    rdS 	 t          |t                             }n# t          $ r Y dS w xY wt          dz  |z  S )zeThe ``/proc/<pid>/stat`` ``starttime`` folded with the boot id, or ``None`` when the process is gone.z/proc/z/statN   )   @   )
r   
read_bytesr@   rfindsplitlenrF   r   rP   rS   )r   datafields	starttimes       r   r   r   y   s    /S///00;;==   tt $**T**Q.0017799F6{{...t'7 899		   ttNi//s   %( 
66B! !
B/.B/darwin)	use_errnozFinal[ctypes.CDLL]_LIBCrV   	_CTL_KERN   
_KERN_PROC_KERN_PROC_PIDz<qiz
Final[str]_TIMEVAL_AT_ZERO_TIMEVAL_SIZEc                   t          j        dz  t          t          t          |           }t          j        d          }t                              |ddt          j        |          dd          dk    rdS t          j	        |j
        z              }t                              |d|t          j        |          dd          dk    s|j
        t          k     rdS t          j        t          |j        d          \  }}|dz  |z   S )zcThe process start time in microseconds from ``sysctl(KERN_PROC_PID)``, or ``None`` when it is gone.r2   r   Ni@B )r)   c_intrb   rd   re   c_size_tra   sysctlr6   c_charvaluerg   structunpack_fromrf   raw)r   miblengthbuffersecondsmicrosecondss         r   r   r      s    <!#Y
NCPPC_Q''F ||CD&,v*>*>aHHAMMtmfl255F||CFFL,@,@$JJaOOSYS_boSoSot$*$67GUV$W$W!G\Y&55r   c                    ~ dS )z_No proven start-time source, so the owner carries no token and liveness rests on the PID alone.Nr   )r   s    r   r   r      s
    4r   )r   r   r   r   )r   r	   )r   r   r   r	   r   r   r   r   )r   r   r   r   )r   r   r   r   )r   r   )1
__future__r   r>   r   sysrA   r   r   pathlibr   typingr   r   r   platformr)   r   WinDLLr    __annotations__HANDLEr(   argtypesBOOLrestypeDWORDr'   POINTERr.   r5   r!   r"   r$   r%   r   r   rF   rR   rS   rn   CDLLra   rb   rd   re   rf   calcsizerg   __all__r   r   r   <module>r      sv   " " " " " " " 				  



                           
: : : :( <7MMM&3fmJt&T&T&TITTTT&.o%6I"$,MI!&.nhmX^%TI"$,OI!x())x())x())x())*I& )1I%#+++++9?*????/1 1111',,,,,G G G GH H H H H*
 
 
 
 |+++
 (*))))
	 
	 
	 
	  -}....	0 	0 	0 	0 	0  
	!	!$/FK$E$E$EEEEE !	!!!!!#
####%&&&&& (-,,,,$3FO4D$E$EEEEE	6 	6 	6 	6 	6 	 	 	 	  r   