o
    i                     @   s<   d dl Z d dlZd dlZG dd deZG dd deZdS )    Nc                   @   s   e Zd ZdZdS )UnresolvableContainerIDz1
    Unable to get container ID from cgroup.
    N)__name__
__module____qualname____doc__ r   r   O/home/ubuntu/.local/lib/python3.10/site-packages/datadog/dogstatsd/container.pyr      s    r   c                   @   sr   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZedZede
eeZdd Zdd Zdd Zdd ZdS )CgroupaM  
    A reader class that retrieves either:
    - The current container ID parsed from the cgroup file
    - The cgroup controller inode.

    Returns:
    object: Cgroup

    Raises:
        `NotImplementedError`: No proc filesystem is found (non-Linux systems)
        `UnresolvableContainerID`: Unable to read the container ID
    z/proc/self/cgroupz/sys/fs/cgroupz/proc/self/ns/cgroupmemory l   _ zH[0-9a-f]{8}[-_][0-9a-f]{4}[-_][0-9a-f]{4}[-_][0-9a-f]{4}[-_][0-9a-f]{12}z[0-9a-f]{64}z[0-9a-f]{32}-\d+z^(\d+):([^:]*):(.+)$z!(?:.+)?({0}|{1}|{2})(?:\.scope)?$c                 C   s$   |   r|  | _d S |  | _d S )N)_is_host_cgroup_namespace_read_cgroup_pathcontainer_id_get_cgroup_from_inodeselfr   r   r   __init__,   s   
zCgroup.__init__c                 C   s@   zt j| jrt | jj| jkW S dW S  ty   Y dS w )z;Check if the current process is in a host cgroup namespace.F)ospathexistsCGROUP_NS_PATHstatst_inoHOST_CGROUP_NAMESPACE_INODE	Exceptionr   r   r   r   r   2   s   z Cgroup._is_host_cgroup_namespacec              
   C   s(  z_t | jddM}|D ]@}| }| j|}|sq| \}}}dd |dD }t|rK| j|	 }|rKd
|d  W  d   W S qW d   W dS 1 sXw   Y  W dS  ty } z|jtjkrutd	
| jW Y d}~dS d}~w ty } ztd
t| d}~ww )z+Read the container ID from the cgroup file.rmodec                 S   s   g | ]}|qS r   r   ).0pr   r   r   
<listcomp>G   s    z,Cgroup._read_cgroup_path.<locals>.<listcomp>/zci-{0}   NzUnable to open {}.z!Unable to read the container ID: )openCGROUP_PATHstripLINE_REmatchgroupssplitlenCONTAINER_REpopformatgroupIOErrorerrnoENOENTNotImplementedErrorr   r   str)r   fpliner'   _r   partser   r   r   r   =   s<   
zCgroup._read_cgroup_pathc                 C   s   i }t | jdd1}|D ]&}| d}t|dkrq|d | jks*|d | jkr2|d ||d < qW d   n1 s=w   Y  | j| jfD ]*}||v rrtj	| j
||| dkr^|| nd	}t|j}|dkrrd
|  S qHdS )z,Read the container ID from the cgroup inode.r   r   :   r"      Nr!   r   zin-{0})r#   r$   r%   r)   r*   CGROUPV1_BASE_CONTROLLERCGROUPV2_BASE_CONTROLLERr   r   joinCGROUP_MOUNT_PATHr   r   r-   )r   cgroup_controllers_pathsr4   r5   tokens
controller
inode_pathinoder   r   r   r   S   s2   
zCgroup._get_cgroup_from_inodeN)r   r   r   r   r$   r?   r   r<   r=   r   UUID_SOURCECONTAINER_SOURCETASK_SOURCErecompiler&   r-   r+   r   r   r   r   r   r   r   r   r	      s"    
r	   )r0   r   rH   r   r   objectr	   r   r   r   r   <module>   s
   