o
    jĎi7-                     @   sN  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ZddlZddlm	Z
 ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ erJdd
lmZ g dZdd Zeddd ejD Zdd Zdd ZdejeedejeejefddZdd Zd(ddZd(ddZ edd Z!ed d! Z"d(d"d#Z#ej e#_ d$d% Z$ed&d' Z%dS ))z%Routines common to all posix systems.    N   )_ntuples)MACOSTimeoutExpired)debug)memoize)usage_percent)_psutil_osx)
pid_existswait_pid
disk_usageget_terminal_mapc                 C   sF   | dkrdS z	t | d W dS  ty   Y dS  ty"   Y dS w )z6Check whether pid exists in the current process table.r   TF)oskillProcessLookupErrorPermissionError)pid r   K/home/jeff/fluffinator/venv/lib/python3.10/site-packages/psutil/_psposix.pyr      s   	r   	Negsignalc                 C   s   i | ]}|j |j qS r   )namevalue.0xr   r   r   
<dictcomp>4   s    r   c                 C   s"   zt | W S  ty   |  Y S w )z+Convert a negative signal value to an enum.)r   
ValueError)numr   r   r   negsig_to_enum8   s
   
r   c                 C   s@   t | r
t | S t | rtt |  S d| }t|)z.Convert a os.waitpid() status to an exit code.zunknown process exit status )r   	WIFEXITEDWEXITSTATUSWIFSIGNALEDr   WTERMSIGr   )statusmsgr   r   r   convert_exit_code@   s   



r&   	monotonicc                    s   d}dd}ddur|t jO }dkr   fdd}		 z
t | |\}
}W n tyF   || rC|	|}|| s;Y dS w |
dkrP|	|}nt|S q&)a  Wait for a process PID to terminate.

    If the process terminated normally by calling exit(3) or _exit(2),
    or by returning from main(), the return value is the positive integer
    passed to *exit().

    If it was terminated by a signal it returns the negated value of the
    signal which caused the termination (e.g. -SIGTERM).

    If PID is not a children of os.getpid() (current process) just
    wait until the process disappears and return None.

    If PID does not exist at all return None immediately.

    If timeout is specified and process is still alive raise
    TimeoutExpired.

    If timeout=0 either return immediately or raise TimeoutExpired
    (non-blocking).
    g-C6?g{Gz?r   Nc                    s8   dksd ur krt |   | d S )Nr      r   )interval_min_sleep_timermax_intervalstop_attimeoutr   r   sleep_or_timeout   s   z(wait_pid_posix.<locals>.sleep_or_timeout)r   WNOHANGwaitpidChildProcessErrorr&   )r   r0   _waitpidr-   r+   r,   _pid_existsr)   flagsr1   retpidr$   r   r*   r   wait_pid_posix_   s,   


r9   c                 C   sF   z
t | d\}}W n ty   t| | Y S w |dksJ t|S )zhWrapper around os.waitpid(). PID is supposed to be gone already,
    it just returns the exit code.
    r   )r   r3   r4   r9   r&   )r   r0   r8   r$   r   r   r   r5      s   r5   c              
   C   s   zt | d}W n< tyD } z0|jtjkr!t| |W  Y d}~S |jtjtjtjhv r?t	d|d t| |W  Y d}~S  d}~ww z-t
 }||t
j |du rWdnt|d }||}|sht|t| |W t | S t | w )zbWait for PID to terminate using pidfd_open() + poll(). Linux >=
    5.3 + Python >= 3.9 only.
    r   Nzpidfd_open() failed (); use fallbacki  )r   
pidfd_openOSErrorerrnoESRCHr9   EMFILEENFILEENODEVr   selectpollregisterPOLLINintr   r5   close)r   r0   pidfderrpoller
timeout_mseventsr   r   r   wait_pid_pidfd_open   s(   

rM   c              
   C   s   zt  }W n) ty/ } z|jtjtjhv r*td|d t| |W  Y d}~S  d}~ww zZt j| t j	t j
t jB t jd}z
||gd|}W n0 tyz } z$|jtjtjtjhv rutd|d t| |W  Y d}~W |  S  d}~ww |st|t| |W |  S |  w )z=Wait for PID to terminate using kqueue(). macOS and BSD only.zkqueue() failed (r:   Nfilterr7   fflagsr   zkqueue.control() failed ()rB   kqueuer<   r=   r?   r@   r   r9   keventKQ_FILTER_PROC	KQ_EV_ADDKQ_EV_ONESHOTKQ_NOTE_EXITcontrolEACCESEPERMr>   rG   r   r5   )r   r0   kqrI   kevrL   r   r   r   wait_pid_kqueue   s<   


r\   c               
   C   sx   t tdsdS z
tt d} W n# ty4 } z|jtjtjhv r)W Y d }~dS W Y d }~dS d }~ww t|  dS )Nr;   Fr   T)	hasattrr   r;   getpidr<   r=   r?   r@   rG   )rH   rI   r   r   r   can_use_pidfd_open   s   

r_   c               
   C   s   d} t dd | D sdS d }zez+t }tjt tjtjtjB tj	d}|
|gdd W W |d ur:|  dS dS  tyt } z-|jtjtjhv r^W Y d }~W |d ur\|  dS dS W Y d }~W |d urn|  dS dS d }~ww |d ur~|  w w )	N)rQ   rT   rU   rS   rV   c                 s   s    | ]}t t|V  qd S N)r]   rB   r   r   r   r   	<genexpr>  s    z!can_use_kqueue.<locals>.<genexpr>FrN   r   r   T)allrB   rQ   rR   r   r^   rS   rT   rU   rV   rW   rG   r<   r=   r?   r@   )namesrZ   r[   rI   r   r   r   can_use_kqueue  s@   

rd   c                 C   sJ   | dksJ |d ur|dksJ t  rt| |S t r t| |S t| |S )Nr   )r_   rM   rd   r\   r9   )r   r0   r   r   r   r   (  s   


r   c                 C   sn   t | }|j|j }|j|j }|j|j }|| }tr#t| |}|| }t	||dd}t
j||||dS )a.  Return disk usage associated with path.
    Note: UNIX usually reserves 5% disk space which is not accessible
    by user. In this function "total" and "used" values reflect the
    total and used disk space whereas "free" and "percent" represent
    the "free" and "used percent" user disk space.
    r   )round_)totalusedfreepercent)r   statvfsf_blocksf_frsizef_bfreef_bavailr   r
   disk_usage_usedr	   ntp
sdiskusage)pathstrf   avail_to_rootavail_to_userrg   
total_userusage_percent_userr   r   r   r   :  s   
r   c               	   C   s\   i } t  dt  d }|D ]}|| vsJ |z
|| t|j< W q ty+   Y qw | S )zNGet a map of device-id -> path as a dict.
    Used by Process.terminal().
    z	/dev/tty*z
/dev/pts/*)globr   statst_rdevFileNotFoundError)retlsr   r   r   r   r   ^  s   r   r`   )&__doc__enumr=   rx   r   rB   signaltime r   rp   _commonr   r   r   r   r	   r
   __all__r   IntEnumSignalsr   r   r&   r3   getattrminsleepr9   r5   rM   r\   r_   rd   r   r   r   r   r   r   r   <module>   sT   !
D

# 


 $