
    \j                        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 d2dZd3dZej                  dk(  rod 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j2                  ej.                  ej2                  gej4                  _        ej(                  ej4                  _        ej(                   ej6                  ej8                         ej6                  ej8                         ej6                  ej8                         ej6                  ej8                        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<   d4dZ"d5dZ#nd4dZ"ej                  dv rdZ$ded<   d6dZ% e%       Z&ded <   d5d!Z#nlej                  d"k(  rYd dlZd dl'Z' ejP                  dd#      Z)d$ed%<   d&Z*ded'<   d(Z+ded)<   d&Z,ded*<   d+Z-d,ed-<    e'j\                  e-      Z/ded.<   d5d/Z#nd5d0Z#g d1Z0y)7    )annotationsN)EPERMESRCH)Path)Finalc                 *    t        j                         S )zfThe hostname recorded alongside an owner, so a marker written on another machine is never probed here.)socketgethostname     E/root/.hermes/venv/lib/python3.12/site-packages/filelock/_identity.py	host_namer      s    r   c                h    |t               k7  ryt        |       sy|yt        |       }|duxr ||k7  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)pidhostnamestart_tokencurrents       r   owner_is_staler      sD     9;!#&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         j                  t        t        |       }|rt         j	                  |       yt        j                         t        k7  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   ;   sA    &&'79LcR!!&)$$&*FFFr   c           	        t         j                  t        t        |       }|syd t	        d      D        \  }}}}	 t         j                  |t        j                  |      t        j                  |      t        j                  |      t        j                  |            s	 t         j                  |       y	 t         j                  |       |j                  dz  |j                  z  S # t         j                  |       w xY w)zXThe process creation FILETIME as a 100ns tick count, or ``None`` when it cannot be read.Nc              3  D   K   | ]  }t        j                           y w)N)r   FILETIME).0_s     r   	<genexpr>z&process_start_token.<locals>.<genexpr>H   s     6]qx7H7H7J6]s           )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6]TYZ[T\6]3)[)
	*,,X&Y'[)Y' !!&) !!&)''2-1G1GGG !!&)s   A%C) )D c                    	 t        j                  | d       y# t        $ r6}|j                  t        k(  rY d}~y|j                  t
        k(  rY d}~y d}~ww xY w)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   sL    	GGCO   	{{e#{{e#	s    	AAAAA>   linuxandroid   _STARTTIME_INDEXc                     	 t        d      j                  d      } t        | j                         j	                  dd      d      S # t
        t        f$ r Y yw xY w)Nz/proc/sys/kernel/random/boot_idascii)encoding-    r   )r   	read_textintstripreplacer;   
ValueError)boot_ids    r   _read_boot_idrN   k   sX    @AKKU\K]7==?223;R@@Z( s   AA AA_BOOT_IDc                (   	 t        d|  d      j                         }||j                  d      dz   d j	                         }t        |      t        k  ry	 t        |t                 }t        dz  |z  S # t        $ r Y yw xY w# t        $ r Y yw xY w)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lenrA   rI   rL   rO   )r   datafields	starttimes       r   r   r   y   s    fSE/0;;= $**T*Q.01779F6{..'7 89	 Ni//    s#   A6 B 6	BB	BBdarwin)	use_errnozFinal[ctypes.CDLL]_LIBCrR   	_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        j                  |ddt        j                  |      dd      dk7  ryt        j                  |j                  z         }t        j                  |d|t        j                  |      dd      dk7  s|j                  t        k  ry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.r-   r   Ni@B )r$   c_intr^   r`   ra   c_size_tr]   sysctlr1   c_charvaluerc   structunpack_fromrb   raw)r   miblengthbuffersecondsmicrosecondss         r   r   r      s    <<!#Y
NCPC__Q'F ||CD&,,v*>aHAMmmfll25F||CFFLL,@$JaOSYS_S_boSo$*$6$67GUV$W!G\Y&55r   c                     ~ y)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    r   )r   r   r   r   )returnstr)r   rI   r   rt   r   
int | Noners   bool)r   rI   rs   rv   )r   rI   rs   ru   )rs   rI   )1
__future__r   r9   r	   sysr<   r   r   pathlibr   typingr   r   r   platformr$   r   WinDLLr   __annotations__HANDLEr#   argtypesBOOLrestypeDWORDr"   POINTERr)   r0   r   r   r   r    r   r   rA   rN   rO   rj   CDLLr]   r^   r`   ra   rb   calcsizerc   __all__r   r   r   <module>r      s   " 	  
    
:( <<7&3fmmJt&TI#T&.oo%6I"$,MMI!&.nnhmmX^^%TI"$,OOI!x(()x(()x(()x(()*I& )1I%#+j+9?*J?/1 *1',,GH*
 ||++
 (**)
	  -*.	0  
	!$/FKK$E!E !	:!!#
J#%&
& (-*,$3FOO4D$EzE	6 	r   