HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //lib/python3/dist-packages/uaclient/__pycache__/system.cpython-38.pyc
U

��Jhyp�@slddlZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlmZddl
mZddlmZmZmZmZmZmZmZddlmZmZmZdZdZdZd	Zd
Ze� e�!e"��Z#dZ$dZ%ed
dej&fdej&fde'fde'fde'fg�Z(edde'fde'fdeejfdee'fdee)fdee)fdee)fdee'fdee'fg	�Z*edde'fde'fde'fde'fg�Z+ed d!ee'fd"ee'fd#ee'fd$ee'fd%ee'fd&ee'fd'ee'fd(ee'fd)ee'fd*ee'fd+ee'fd,ee'fd-ee'fd.ee'fg�Z,ed/d0eee'fd1eee'fg�Z-d2Z.ej/eejd3�d4d5�Z0ej/eejd3�d6d7�Z1edd8�e*d9�d:d;��Z2d<d=�Z3edd8�e'd9�d>d?��Z4edd8�e'd9�d@dA��Z5edd8�e,d9�dBdC��Z6edd8�e'd9�dDdE��Z7edd8�e+d9�dFdG��Z8edd8�e'e9dH�dIdJ��Z:edd8�e9d9�dKdL��Z;edd8�e'e9dH�dMdN��Z<edd8�e'e9dH�dOdP��Z=edd8�e9d9�dQdR��Z>edd8�d�e'e9dT�dUdV��Z?edd8�e9d9�dWdX��Z@edd8�ee'e'fd9�dYdZ��ZAedd8�e'e(dH�d[d\��ZBe'ee'd]�d^d_�ZCd�eee'eee'e9d`�dadb�ZDe'e9dc�ddde�ZEe'e'df�dgdh�ZFd�e'e)ddj�dkdl�ZGd�e'e'ee)ddm�dndo�ZHe'ddp�dqdr�ZId�ee'eee)e9eeJeee'e'fe9ee'e'fdu�dvdw�ZKd�ee'eee)e9eeJeeeJeee'e'fe9ee'e'fdx�dydz�ZLe'dd{�d|d}�ZMe'e9d~�dd��ZNe'ee'd~�d�d��ZOe'd9�d�d��ZPee-d9�d�d��ZQdS)��N)�	lru_cache)�rmtree)�Dict�List�
NamedTuple�Optional�Sequence�Set�Tuple)�defaults�
exceptions�utilz/var/run/reboot-requiredz/var/run/reboot-required.pkgsz/etc/machine-idz/var/lib/dbus/machine-idz!/usr/share/distro-info/ubuntu.csvz5(?P<release>\d+\.\d+) (LTS\s*)?(\((?P<series>\w+))?.*zd^(?P<major>[\d]+)[.-](?P<minor>[\d]+)[.-](?P<patch>[\d]+)-(?P<abi>[\d]+)-(?P<flavor>[A-Za-z0-9_-]+)$�
DistroInfo�eol�eol_esm�series�release�series_codename�
KernelInfo�uname_machine_arch�
uname_release�
build_date�proc_version_signature_version�major�minor�patch�abi�flavor�ReleaseInfo�distribution�pretty_version�CpuInfo�cpuinfo_cpu�cpuinfo_cpu_architecture�cpuinfo_cpu_family�cpuinfo_cpu_implementer�cpuinfo_cpu_part�cpuinfo_cpu_revision�cpuinfo_cpu_variant�
cpuinfo_model�cpuinfo_model_name�cpuinfo_stepping�cpuinfo_vendor_id�"sys_firmware_devicetree_base_model�
sysinfo_model�sysinfo_type�RebootRequiredPkgs�standard_packages�kernel_packagesz(Mon|Tue|Wed|Thu|Fri|Sat|Sun).*)�uname�returncCslt�rt�d�dSt�d�z(t�d�|j��}tj�|j	tj
j�WStk
rft�d�YdSXdS)NzPNot attempting to use timestamp of kernel changelog because we're in a containerz3Falling back to using timestamp of kernel changelogz1/usr/share/doc/linux-image-{}/changelog.Debian.gzzUnable to stat kernel changelog)
�is_container�LOG�warning�os�stat�formatr�datetimeZ
fromtimestamp�st_mtime�timezone�utc�	Exception)r3�stat_result�rA�1/usr/lib/python3/dist-packages/uaclient/system.py�_get_kernel_changelog_timestampqs&�
���
rCcCs�t�t|j�}|dkr(t�d�t|�S|�d�}ztj�	|d�}Wn&t
k
rjt�d�t|�YSX|jdkr�|jtj
jd�}|S)Nz*Unable to find build date in uname versionrz%a %b %d %H:%M:%S %Z %Yz-Unable to parse build date from uname version)�tzinfo)�re�search�RE_KERNEL_EXTRACT_BUILD_DATE�versionr6r7rC�groupr;�strptime�
ValueErrorrD�replacer=r>)r3Z
date_matchZdate_strZdtrArArB�_get_kernel_build_date�s



rM)�maxsize)r4cCs�d}ztd�}|��d}Wntk
r:t�d�YnXt��}|j��}t	|�}|j
��}t�t
|�}|dkr�t�d|�t||||dddddd�	St||||t|�d��t|�d��t|�d��|�d	�|�d
�d�	SdS)Nz/proc/version_signature�z*failed to process /proc/version_signature.zFailed to parse kernel: %s)	rrrrrrrrrrrrrr)�	load_file�splitr?r6r7r8r3�machine�striprMrrE�match�RE_KERNEL_UNAMEr�intrI)rZproc_version_signature_fullr3rrrZuname_matchrArArB�get_kernel_info�sF

��rWcsnddlm}t��std��dd�|�D�}dd�t�d�D�}dd�|D��d	d�|D�}�fd
d�|D�S)Nr)�get_installed_packages_namesz9get_installed_ubuntu_kernels needs to be executed as rootcSsg|]}d|kr|�qS)�linux-image-rA)�.0�packagerArArB�
<listcomp>�s�z0get_installed_ubuntu_kernels.<locals>.<listcomp>cSs$g|]}dtd|g�dkr|�qS)zLinux kernel�filer��subp)rZr]rArArBr\�s�z/boot/vmlinu[x|z]-*cSsg|]}|td�d��qS)rYN��len)rZZpackage_namerArArBr\�scSsg|]}|td�d��qS)z/boot/vmlinu?-Nr`)rZ�	file_namerArArBr\�s�csg|]}|�kr|�qSrArA)rZrH�Zlinux_image_versionsrArBr\�s�)Zuaclient.aptrXr
�we_are_currently_root�RuntimeError�glob)rXZlinux_imageZvmlinux_kernel_filesZvmlinuz_versionsrArcrB�get_installed_ubuntu_kernels�s(�����
�rgcCstddg�\}}|��S)NZdpkgz--print-architecture)r_rS)�out�_errrArArB�
get_dpkg_arch�srjcCs�ztdg�\}}|��WStjk
r�z<td�}d|ksDd|krLWYdSd|kr\WYdSWYdSWntk
r~YYdSXYnXdS)N�systemd-detect-virtz/proc/1/cgroupZdockerZbuildkitZbuildahZpodman�)r_rSr�ProcessExecutionErrorrPr?)rh�_Z
proc_1_cgrouprArArB�
get_virt_type�s
roc
Cs�d}d}d}i}i}d}zt|�}Wn$tk
rHt�d�|��YnXX|�d�}tdd�|D�d�}|�d	�D],}	d
|	krr|	�d
d�\}
}|��||
��<qrzt|�}Wn$tk
r�t�d�|��Yn:X|�d	�D],}	d
|	kr�|	�d
d�\}
}|��||
��<q�zt|�}Wn&tk
�r<t�d�|��YnXt|�	d�|�	d
�|�	d�|�	d�|�	d�|�	d�|�	d�|�	d�|�	d�|�	d�|�	d�||�	d�|�	d�d�S)Nz
/proc/cpuinfoz
/proc/sysinfoz#/sys/firmware/devicetree/base/modelzcould not open {}z

css|]}|�d�r|VqdS)Z	processorN)�
startswith)rZ�srArArB�	<genexpr>s
zget_cpu_info.<locals>.<genexpr>rl�
�:rOZcpuzCPU architecturez
cpu familyzCPU implementerzCPU partzCPU revisionzCPU variantZmodelz
model nameZsteppingZ	vendor_idZModelZType)r"r#r$r%r&r'r(r)r*r+r,r-r.r/)
rP�FileNotFoundErrorr6�infor:rQ�nextrSr!�get)
ZCPUINFO_FILEZSYSINFO_FILEZDEVICETREE_FILEZcpuinfo_valuesZsysinfo_valuesZdevicetree_base_modelZcpuinfo_contentZcpuinfo_sectionsZfirst_cpu_content�line�key�valueZsysinfo_contentrArArB�get_cpu_info	s^
��r|cCs�ddlm}ddlm}|��}|jrB|j�di��d�}|rB|S|��}ttfD]*}t	j
�|�rRt|��
d�}|rR|SqR|r�|Stt���}|�|�|S)z�
    Get system's unique machine-id or create our own in data_dir.
    We first check for the machine-id in machine-token.json before
    looking at the system file.
    r)�
machine_token)�machine_id_fileZmachineTokenInfoZ	machineIdrs)Zuaclient.filesr}Zuaclient.files.state_filesr~Zget_machine_token_filerx�read�ETC_MACHINE_ID�DBUS_MACHINE_IDr8�path�existsrP�rstrip�str�uuidZuuid4�write)Zcfgr}r~Zmachine_token_fileZ
machine_idZfallback_machine_idr��contentrArArB�get_machine_idFs.��

r�cCs�t�}|�dd�}t�dd|�dd��}|�dd�}|�dd�}|rH|s�t�t|�}|sntj|�dd�|d	��|��}|p�|�d
d�}|s�tj	|d��|p�|�dd�}t
|||��|d
�S)N�NAMEZUNKNOWNz\.\d LTSz LTSZVERSIONrlZVERSION_CODENAMEZ
VERSION_ID)Zorig_verZmod_verr)rHr)rrrr )�_parse_os_releaserxrE�subrT�REGEX_OS_RELEASE_VERSIONrZParsingErrorOnOSReleaseFile�	groupdictZMissingSeriesOnOSReleaseFiler�lower)Z
os_releaserr rrrTZ
match_dictrArArB�get_release_infohs2
���r�)rr4cCstddg�\}}||kS)N�/usr/bin/ubuntu-distro-infoz--supported-esmr^�rrhrirArArB�is_lts�sr�cCstt�j�S�N)r�r�rrArArArB�is_current_series_lts�sr�cCstddg�\}}||kS)Nr�z--supportedr^r�rArArB�is_supported�sr�cCs,t|�sdStdd|dg�\}}t|�dkS)zCReturn True when Ubuntu series supports ESM and is actively in ESM.Fr�z--seriesz-yeolr)r�r_rVr�rArArB�
is_active_esm�s
�r�cCstt�j�Sr�)r�r�rrArArArB�is_current_series_active_esm�sr��/run)�run_pathr4c	Cs�ztdg�WdStjk
r&YnXztdddg�WdSttfk
rTYnXdD]$}tj�||�}tj�|�rZdSqZdS)z>Checks to see if this code running in a container of some sortZischrootFrk�--quietz--containerT)Zcontainer_typezsystemd/container)	r_rrm�IOError�OSErrorr8r��joinr�)r��filenamer�rArArBr5�s
r5cCs.ddlm}|��D]}d|jkrdSqdS)z�Returns True if any package installed has "ubuntu-desktop" in the name.

    This includes ubuntu-desktop, ubuntu-desktop-minimal, kubuntu-desktop, etc.
    r)�aptzubuntu-desktopTF)�uaclientr�Zget_installed_packages�name)r�r[rArArB�
is_desktop�s

r�cCsfztd�}Wntk
r(td�}YnXi}|��D]*}|�dd�\}}|r6|���d�||<q6|S)Nz/etc/os-releasez/usr/lib/os-release�=rO�")rPru�
splitlinesrQrS)Z
file_contents�dataryrzr{rArArBr��sr�c
Cs�ztt���}Wntk
r,t���YnX|D]�}|�d�}|d|kr2|dkrZd}nd|dkrn|dn|d}t|d|d	|dtj�	|dd
��
�tj�	|d
��
�d�Sq2tj|d��dS)
N�,�Zxenialz
2026-04-23ZLTSr��rOz%Y-%m-%d)rrrrr)r)rP�DISTRO_INFO_CSVr�rurZMissingDistroInfoFilerQrr;rJ�dateZMissingSeriesInDistroInfoFile)r�linesry�valuesrrArArB�get_distro_info�s$
�r�)�programr4cCsrtjj|krt|�r|Sdd�tj�dd��tj�D�}dd�|D�}|D]"}tj�||�}t|�rJ|SqJdS)z;Find whether the provided program is executable in our PATHcSsg|]}|�d��qS)r�)rS�rZ�prArArBr\�szwhich.<locals>.<listcomp>�PATHrlcSsg|]}tj�|��qSrA)r8r��abspathr�rArArBr\sN)	r8r��sep�is_exe�environrxrQ�pathsepr�)r��pathsZnormalized_pathsr�Zprogram_pathrArArB�which�s�
r�)�installed_pkgs�installed_pkgs_regexr4cCs�tj�t�sdS|dkr$|dkr$dSzttt��d��}Wntk
rPYdSX|dk	rpt	|�
|��dkrpdS|dk	r�|D]"}|D]}t�||�r�dSq�q|dS)a�Check if the system needs to be rebooted.

    :param installed_pkgs: If provided, verify if the any packages in
        the list are present on /var/run/reboot-required.pkgs. If that
        param is provided, we will only return true if we have the
        reboot-required marker file and any package in reboot-required.pkgs
        file. When both installed_pkgs and installed_pkgs_regex are
        provided, they act as an OR, so only one of the two lists must have
        a match to return True.
    :param installed_pkgs_regex: If provided, verify if the any regex in
        the list matches any line in /var/run/reboot-required.pkgs. If that
        param is provided, we will only return true if we have the
        reboot-required marker file and any match in reboot-required.pkgs
        file. When both installed_pkgs and installed_pkgs_regex are
        provided, they act as an OR, so only one of the two lists must have
        a match to return True.
    FNTrsr)
r8r�r��REBOOT_FILE_CHECK_PATH�setrP�REBOOT_PKGS_FILE_PATHrQrura�intersectionrErF)r�r�Zreboot_required_pkgs�pkg_nameZ	pkg_regexrArArB�
should_reboot	s&�r�)r�r4cCstj�|�ot�|tj�Sr�)r8r��isfile�access�X_OK)r�rArArBr�?sr�)r�r4c	Cs^t|d��}t�d|�|��}W5QRXz|�d�WStk
rXtj|dd��YnXdS)z!Read filename and decode content.�rbzReading file: %s�utf-8)rb�
file_encodingN)�openr6�debugr�decode�UnicodeDecodeErrorrZInvalidFileEncodingError)r��streamr�rArArBrPDs�rP�)r��moder4cCs@t�d|�tjtj�|�dd�t�|���t�	||�dS)NzCreating file: %sT��exist_ok)
r6r�r8�makedirsr��dirname�pathlib�PathZtouch�chmod)r�r�rArArB�create_fileQsr�)r�r�r�r4c
Cs"d}tj�|�}|r<t�|���}t�|j�}|dkrH|}n|dkrHd}z�tjtj�	|�dd�t
jddtj�	|�d�}t�
d||j�|�|�d	��|��|��t�|j|�|r�t�|j|j|j�t�|j|�Wn>tk
�r}z|dk	�rt�|j�|�W5d}~XYnXdS)
a_Write content to the provided filename encoding it if necessary.

    We preserve the file ownership and permissions if the file is present
    and no mode argument is provided.

    @param filename: The full path of the file to write.
    @param content: The content to write to the file.
    @param mode: The filesystem mode to set on the file.
    Nr�Tr��wbF)r��delete�dirz*Writing file %s atomically via tempfile %sr�)r8r�r�r�r�r9�S_IMODE�st_moder�r��tempfileZNamedTemporaryFiler6r�r�r��encode�flush�closer��chown�st_uid�st_gid�renamer?�unlink)r�r�r�ZtmpfZis_file_presentZ	file_statZf_mode�erArArB�
write_fileXs@
��
r�)�	file_pathr4cCs@zt�|�t�d|�Wn tk
r:t�d|�YnXdS)z<Remove a file if it exists, logging a message about removal.zRemoved file: %sz*Tried to remove %s but file does not existN)r8r�r6r�ru)r�rArArB�ensure_file_absent�s

r�FT)�args�rcs�capture�timeout�override_env_vars�pipe_stdouterrr4cCs�dd�|D�}d}d}i}	|r4tj}tj}ddd�}	|dkr@i}tj|	|�}
|dkrZdg}t�d�|��}z&tj||||
d�}|j|d	�\}
}Wn~t	k
�rzD|
r�|
j
d
dd�nd
}|r�|j
d
dd�nd
}tj||j
||d��Wn"tk
�rtj|d��YnXYnX|
�r$|
j
d
dd�nd
}|�r<|j
d
dd�nd
}|j
|k�r`tj||j
||d��|�rxt�d||j
|�||fS)a�Run a command and return a tuple of decoded stdout, stderr.

    @param args: A list of arguments to feed to subprocess.Popen
    @param rcs: A list of allowed return_codes. If returncode not in rcs
        raise a ProcessExecutionError.
    @param capture: Boolean set True to log the command and response.
    @param timeout: Optional float indicating number of seconds to wait for
        subp to return.
    @param override_env_vars: Optional dictionary of environment variables.
        If None, the current os.environ is used for the subprocess.
        If defined, these env vars get merged with the current process'
        os.environ for the subprocess, overriding any values that already
        existed in os.environ.

    @return: Tuple of utf-8 decoded stdout, stderr
    @raises ProcessExecutionError on invalid command or returncode not in rcs.
    @raises subprocess.TimeoutError when timeout specified and the command
        exceeds that number of seconds.
    cSs$g|]}t|t�r|n|�d��qS)r�)�
isinstance�bytesr�)rZ�xrArArBr\�sz_subp.<locals>.<listcomp>NzC.UTF-8)ZLANG�LC_ALLr� )�stdout�stderr�env)r�r��ignore)�errorsrl)�cmdZ	exit_coder�r�)r�zRan cmd: %s, rc: %s stderr: %s)�
subprocess�PIPEr8r�r
Zredact_sensitive_logsr��PopenZcommunicater�r�rrm�
returncode�UnboundLocalErrorr6r�)r�r�r�r�r�r�Z
bytes_argsr�r�Zset_langZ
merged_envZredacted_cmd�procrh�errZ
out_resultZ
err_resultrArArB�_subp�sj�
��
��r)r�r�r�r��retry_sleepsr�r�r4c

Cs�|dk	r|��nd}z t||||||d�\}}Wq�Wqtjk
r�}	z\|rlt�t|	��t�d|	j|	j	�|sr�t�t|	��t�dt
|��t�|�
d��W5d}	~	XYqXq||fS)a�Run a command and return a tuple of decoded stdout, stderr.

     @param subp: A list of arguments to feed to subprocess.Popen
     @param rcs: A list of allowed return_codes. If returncode not in rcs
         raise a ProcessExecutionError.
     @param capture: Boolean set True to log the command and response.
     @param timeout: Optional float indicating number of seconds to wait for a
         subp call to return.
     @param retry_sleeps: Optional list of sleep lengths to apply between
        retries. Specifying a list of [0.5, 1] instructs subp to retry twice
        on failure; sleeping half a second before the first retry and 1 second
        before the next retry.
     @param override_env_vars: Optional dictionary of environment variables.
        If None, the current os.environ is used for the subprocess.
        If defined, these env vars get merged with the current process'
        os.environ for the subprocess, overriding any values that already
        existed in os.environ.

    @return: Tuple of utf-8 decoded stdout, stderr
    @raises ProcessExecutionError on invalid command or returncode not in rcs.
    @raises subprocess.TimeoutError when timeout specified and the command
        exceeds that number of seconds.
    N)r�r�zStderr: %s
Stdout: %szRetrying %d more times.r)�copyrrrmr6r�r�r7r�r�ra�time�sleep�pop)
r�r�r�r�rr�r�rhrr�rArArBr_�s* �
$r_)�folder_pathr4cCs>zt|�t�d|�Wn tk
r8t�d|�YnXdS)NzRemoved folder: %sz,Tried to remove %s but folder does not exist)rr6r�ru)r	rArArB�ensure_folder_absents
r
)�service_namer4cCs2ztddd|g�Wntjk
r,YdSXdS)a^
    Get if the systemd job is active in the system. Note that any status
    different from "active" will make this function return False.
    Additionally, if the system doesn't exist we will also return False
    here.

    @param service_name: Name of the systemd job to look at

    @return: A Boolean specifying if the job is active or not
    �	systemctlz	is-activer�FT)r_rrm)rrArArB�is_systemd_unit_active$s
r
c
Cs�zHtdddd|g�\}}|r:|�d�r:|�d�d��WSt�d|�Wn4tjk
r|}ztjd	||d
�W5d}~XYnXdS)NrZshowz--property=ActiveStatez
--no-pagerzActiveState=r�rOz9Couldn't find ActiveState in systemctl show output for %sz-Failed to get ActiveState for systemd unit %s)�exc_info)r_rprQrSr6r7rrm)rrhrnr�rArArB�get_systemd_unit_active_state6s,��	��rcCsHt��rtjStj�d�}|r.tj�|tj	�Stj�tj�
d�dtj	�S)NZXDG_CACHE_HOME�~z.cache)r
rdrZUAC_RUN_PATHr8r�rxr�r�ZUSER_CACHE_SUBDIR�
expanduser)Zxdg_cache_homerArArB�get_user_cache_dirQs
�rcCstztt�}Wntk
r"YdSXg}g}d}|��D]&}t�||�rT|�|�q8|�|�q8tt|�t|�d�S)Nz^(linux-image|linux-base).*)r1r2)	rPr�rurQrErT�appendr0�sorted)Zpkg_list_strr1r2Zkernel_regex�pkgrArArB�get_reboot_required_pkgs^s�r)r�)NN)r�)N)NFNNT)NFNNNT)Rr;rfZloggingr8r�rEr9r�r�rr��	functoolsrZshutilr�typingrrrrrr	r
r�rrr
r�r�r�r�r�Z	getLoggerZreplace_top_level_logger_name�__name__r6r�rUr�r�rrVrrr!r0rG�uname_resultrCrMrWrgrjror|r�r��boolr�r�r�r�r�r5r�r�r�r�r�r�rPr�r�r��floatrr_r
r
rrrrArArArB�<module>sT$����





����














����	��+#<!


�

�6
��+�

�Z�


�8