HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //lib/python3/dist-packages/apport/__pycache__/fileutils.cpython-312.pyc
�

j�8d�K��H�dZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddl
mZddlmZ	ddlmZmZmZmZeeefddlmZddlmZej8j;dd	�Zd
ZdZ dZ!d
Z"Gd�dejFjHe%�Z&d�Z'd�Z(d�Z)d�Z*d�Z+d�Z,d�Z-d�Z.d�Z/d�Z0d�Z1d�Z2d�Z3d�Z4d�Z5d�Z6d0d �Z7d!�Z8d1d"�Z9de9_:d#�Z;d$�Z<d%�Z=d&�Z>d'e?d(e@eAeAffd)�ZBd0d*�ZCd2d+�ZDd,�ZEd-�ZFd.�ZGd/�ZHy#e$rddlmZmZmZmZY��wxYw)3z0Functions to manage apport problem report files.�N)�closing)�
itemgetter)�ConfigParser�
NoOptionError�NoSectionError�MissingSectionHeaderError)�
ProblemReport)�impl�APPORT_REPORT_DIRz
/var/crashz/var/lib/apport/coredump�z~/.config/apport/settingsz/run/snapd.socketc��eZdZd�Zd�Zy)�UHTTPConnectionc�f�tjjj|d�||_y)N�	localhost)�http�client�HTTPConnection�__init__�path)�selfrs  �2/usr/lib/python3/dist-packages/apport/fileutils.pyrzUHTTPConnection.__init__,s#�����"�"�+�+�D�+�>���	�c��tjtjtj�}|j|j�||_y�N)�socket�AF_UNIX�SOCK_STREAM�connectr�sock)rrs  rrzUHTTPConnection.connect0s3���}�}�V�^�^�V�-?�-?�@�����T�Y�Y����	rN)�__name__�
__module__�__qualname__rr�rrrr*s���rrc��tjdtj�sy	tjgd��dk(S#t
$rYywxYw)z)Check whether crash reporting is enabled.z/usr/bin/whoopsieT)z/bin/systemctlz-qz
is-enabledz
whoopsie.pathrF)�os�access�X_OK�
subprocess�call�OSErrorr#rr�allowed_to_reportr+6sG���9�9�(�"�'�'�2������V�W�[\�\�\������s�A�	A
�A
c��|sy|jd�syd|vrydD]}||vs�y|jd�}t|�dk7ry|dS)z'Extract the socket from a DBus address.Nzunix:path=/run/user/z../)�%�,�;�=��)�
startswith�split�len)�	dbus_addr�search�partss   r�get_dbus_socketr9Bsi������� 6�7��
�	���"���Y���"�
�O�O�C� �E�
�5�z�Q�����8�Orc� �|�yd}tj|�D]d}|jd�s�|jd�s|jd�s�8|ryt	|d�5}d|j�vr|}ddd��f|S#1swY�rxYw)z�Return a package's .desktop file.

    If given package is installed and has a single .desktop file, return the
    path to it, otherwise return None.
    Nz.desktopz/etc/xdg/autostartz/usr/share/applications/�rbsNoDisplay=true)�	packaging�	get_files�endswithr3�open�read)�package�desktopfile�line�fs    r�find_package_desktopfilerE]s�������K��#�#�G�,���=�=��$��?�?�#7�8����Qk�Al�����$��%��(�����8�&*��&�%�-���	&�%�s�#B�B
	c��gd�}d}|D]}|j|�s�d}n|xr&|jd�xr|jd�S)a2Check whether the given file is likely to belong to a package.

    This is semi-decidable: A return value of False is definitive, a True value
    is only a guess which needs to be checked with find_file_package().
    However, this function is very fast and does not access the package
    database.
    )	z/bin/z/bootz/etc/z/initrdz/libz/sbin/z/optz/usr/z/varFTz/usr/local/z	/var/lib/)r3)�file�
pkg_whitelist�whitelist_match�is    r�likely_packagedrKysb��.�M��O�
���?�?�1��"�O����%�4�?�?�=�#A�A�%�����$�G%�%rc�D�tjj|�\}}tjj|�}tjj	|�r tjj||�}t
|�sytj|�S)z[Return the package that ships the given file.

    Return None if no package ships it.
    N)	r%rr4�realpath�isdir�joinrKr<�get_file_package)rG�dir�name�resolved_dirs    r�find_file_packagerT�sn���'�'�-�-��%�K�S�$��7�7�#�#�C�(�L�	�w�w�}�}�\�"��w�w�|�|�L�$�/���4� ���%�%�d�+�+rc�J�	ttt��5}d|��}|jd|�|j	�}|j
dk(r/t
j|j��dcddd�S	ddd�y#1swYyxYw#t$rYywxYw)zbReturn the data of the given snap.

    Return None if the snap is not found to be installed.
    z
/v2/snaps/�GET���resultN)
rr�SNAPD_SOCKET�request�getresponse�status�json�loadsr@�	Exception)�snap�c�url�responses    r�	find_snaprd�s���
�
�_�\�2�
3�q��t�f�%�C�
�I�I�e�S�!��}�}��H����#�%��z�z�(�-�-�/�2�8�<�4�
3�&�	4�
3�
3������s5�B�AB
�6	B�B�
B�B�B�	B"�!B"c��tj|�}|j|jkDxs|jdk(S)zACheck whether the report file has already been processed earlier.r)r%�stat�st_atime�st_mtime�st_size)�report�sts  r�seen_reportrl�s1��
�����B��K�K�"�+�+�%�;�2�:�:��?�;rc���d|jdd�dz}d|jdd�dz}tjj|�rwtjj|�rXtj|�}tj|�}|j
|j
krtj|�t|d�5	ddd�y#1swYyxYw)Nz	%s.upload�.r2rz%s.uploaded�a)�rsplitr%r�existsrfrh�unlinkr?)rj�upload�uploaded�	report_st�	upload_sts     r�mark_report_uploadrw�s���
�6�=�=��a�0��3�
3�F��v�}�}�S�!�4�Q�7�7�H�	�w�w�~�~�h��B�G�G�N�N�6�$:��G�G�F�O�	��G�G�F�O�	����	� 2� 2�2��I�I�f��	
�f�c�	��
�	�	�s�C�C'c�*�d|vr|djdd�}ntd��tj�}|�dt	|��d|�d�}tj
j
t|�}t|d�5	ddd�y#1swYyxYw)N�ExecutablePath�/�_z1report does not have the ExecutablePath attributernz.hangingro)	�replace�
ValueErrorr%�geteuid�strrrO�
report_dirr?)rj�pid�subject�uid�basers      r�mark_hanging_processr��ss���6�!��)�*�2�2�3��<���L�M�M�
�*�*�,�C�!(�#�c�(�C�8�D�
�7�7�<�<�
�D�)�D�	
�d�C���
���s�>B	�	Bc��tj|�}	tj||j|jdz
f�y#t$r�d}|dkDr�t|�}|j
d�|j�	tj|�}n#t$rYYywxYw|j|jkDrn tjd�|dz}|dkDr��|dk(r
t|�YyYywxYw)zMark given report file as seen.r2�rNg�������?)r%rf�utimerhr*r?r@�closerg�time�sleep�
delete_report)rjrk�timeoutrDs    r�mark_report_seenr��s���
�����B�"�
����"�+�+�r�{�{�Q��7�8���"�
����k��V��A�
�F�F�1�I�
�G�G�I�
��W�W�V�_����
��
���{�{�R�[�[�(���J�J�s�O��q�L�G���k��a�<��&�!��)"�s;�/A�<C9�B�C9�	B'�#C9�&B'�'<C9�$C9�8C9c�r�g}tjtjjtd��D]k}	tjj|�dkDrFtj|tjtjz�r|j|��m|S#t$rY�{wxYw)zCReturn a list with all report files accessible to the calling user.�*.crashr)�globr%rrOr��getsizer&�R_OK�W_OK�appendr*��reports�rs  r�get_all_reportsr��s����G�
�Y�Y�r�w�w�|�|�J�	�:�
;��	��w�w���q�!�A�%�"�)�)�A�r�w�w����7H�*I����q�!��<��N��	�	�
�	�s�A(B*�*	B6�5B6c��g}t�D]}	t|�s|j|��!|S#t$rY�/wxYw)z�Get new reports for calling user.

    Return a list with all report files which have not yet been processed
    and are accessible to the calling user.
    )r�rlr�r*r�s  r�get_new_reportsr��sL���G�
�
��	��q�>����q�!����N��	�	�
�	�s�1�	=�=c���g}tjtjjtd��D]�}	tj
|�}|jdkDr]|jdkrN	tj|j�}|jjd�r�s	|j|���|S#t$rY� wxYw#t$rY��wxYw)z�Get all system reports.

    Return a list with all report files which belong to a system user (i. e.
    uid < 500 according to LSB).
    r�r���guest)r�r%rrOr�rfri�st_uid�pwd�getpwuid�pw_namer3�KeyErrorr�r*)r�r�rk�pws    r�get_all_system_reportsr�s����G�
�Y�Y�r�w�w�|�|�J�	�:�
;��	������B��z�z�A�~�"�)�)�c�/�����b�i�i�0�B��z�z�,�,�W�5� �6�
���q�!��<�"�N�� ������	�
�	�s5�3C�2:C�-C�	C�C�C�C�	C�Cc�T�t�D�cgc]}t|�r�|��c}Scc}w)z�Get new system reports.

    Return a list with all report files which have not yet been processed
    and belong to a system user (i. e. uid < 500 according to LSB).
    )r�rl)r�s r�get_new_system_reportsr�*s'��.�/�F�/�!�{�1�~�A�/�F�F��Fs�%�%c��	tj|�y#t$r6t|d�5}|j	d�ddd�Yy#1swYYyxYwwxYw)z�Delete the given report file.

    If unlinking the file fails due to a permission error (if report_dir is not
    writable to normal users), the file will be truncated to 0 bytes instead.
    �wrN)r%rrr*r?�truncate)rjrDs  rr�r�3sE���
�	�	�&�����
�&�#�
�!�
�J�J�q�M��
�
���s%��A�A	�A�	A	�A�Ac�D�t�}|j|dddg��	t|d�}tjtj
|d��}tjtj��}||z
dkDry|S#ttf$rYywxYw)z�Return the number of recent crashes for the given report file.

    Return the number of recent crashes (currently, crashes which happened more
    than 24 hours ago are discarded).
    F�CrashCounter�Date)�
key_filteri�Qr)	r	�load�intr��mktime�strptime�	localtimer}r�)rj�pr�count�report_time�cur_times     r�get_recent_crashesr�@s���
��B��G�G�F�E�~�v�&>�G�?�	��B�~�&�'���k�k�$�-�-��6�
�";�<���;�;�t�~�~�/�0���k�!�I�-�������!����s�A(B
�B
�
B�Bc��d|vr|djdd�}n(d|vr|djdd�d}ntd��|stj�}tj
j
t|�d	t|��d
��}tjdk\rt|d�Stjtj|tjtjztjzd
�d�S)a6Construct a canonical pathname for a report and open it for writing

    If uid is not given, it defaults to the effective uid of the current process.
    The report file must not exist already, to prevent losing previous reports
    or symlink attacks.

    Return an open file object for binary writing.
    ryrzr{�PackageNr2rz7report has neither ExecutablePath nor Package attributernz.crash�3�xbi��wb)r|r4r}r%r~rrOr�r�sys�versionr?�fdopen�O_WRONLY�O_CREAT�O_EXCL)rjr�r�rs    r�make_report_filer�Ts����6�!��)�*�2�2�3��<��	�f�	���#�)�)�$��2�1�5���R�S�S���j�j�l��
�7�7�<�<�
�W�c�#�h�$G�H�D�
�{�{�c���D�$����y�y�����r�{�{�R�Z�Z�'?�"�)�)�'K�U�S�UY�Z�Zrc��tjj|�sJ�tjdd|gtj
tj
di��}|j
�dj�}|jdk(rgSg}|j�D]8}|jd�s�|j|jdd�d��:|S)	z�Check file integrity against md5 sum file.

    sumfile must be md5sum(1) format (relative to /).

    Return a list of files that don't match.
    z/usr/bin/md5sumz-crz)�stdout�stderr�cwd�envr�FAILED�:r2)
r%rrqr(�Popen�PIPE�communicate�decode�
returncode�
splitlinesr>r�rp)�sumfile�m�out�
mismatchesrCs     r�check_files_md5r�ns����7�7�>�>�'�"�"�"����+�T�7�;� *���
��� �b�	*�A��-�-�/�!�
�
#�
#�
%�C�	�|�|�q���	��J���� ���=�=��"����d�k�k�#�q�1�!�4�5�!��rc���|s@tjtj��d}tjd|�}d}d}d}tjs�td��t_	tj|tjtjz�}tj|�}	tj|	j�r'tj |d�}|j#d�}|�|j)�n|�tj(|�		tjj+|�	|r tjj/||�Stjj1||�S#t$t&f$rY��wxYw#|�|j)�w|�tj(|�wwxYw#t,$rY��wxYw#t2t4f$r|cYSwxYw)z�Return a setting from user configuration.

    This is read from ~/.config/apport/settings or path. If bool is True, the
    value is interpreted as a boolean.

    Privileges may need to be dropped before calling this.
    r�~�N)�
interpolationr�r�)r�r�r%r~�_config_filer|�
get_config�configrr?�
O_NOFOLLOW�O_RDONLY�fstatrf�S_ISREG�st_moder�r@�IOErrorr*r��read_stringr�
getboolean�getrr)
�section�setting�defaultr�bool�homedir�contents�fdrDrks
          rr�r��s�����,�,�r�z�z�|�,�Q�/���#�#�C��1���H�	
�B��A����(�t�<�
��
	�����r�}�}�r�{�{�:�;�B����"��B��|�|�B�J�J�'��I�I�b�#�&���6�6�#�;���}����	��������
����%�%�h�/����$�$�/�/���A�A��$�$�(�(��'�:�:��#��!�	��	���}����	�������� ��
%�
��
��
�>�*�����sO�/BF
�+G�!G!�-G!�
F�F"�F�F"�"-G�	G�G�!G5�4G5c�h�||jd�dzd}t|j�d�S)z7Extracts the starttime from the contents of a stat file�)r1N�)�rfindr�r4)r��strippeds  r�
get_starttimer��s6������s�+�a�/�0�1�H��x�~�~���#�$�$rc�f�d}d}|j�D]�}|jd�r9t|j��dkDrt	|j�d�}�M|jd�s�_t|j��dkDs�|t	|j�d�}��||fS)z;Extracts the uid and gid from the contents of a status fileNzUid:r2zGid:)r�r3r5r4r�)r��real_uid�real_gidrCs    r�get_uid_and_gidr��s����H��H��#�#�%���?�?�6�"�s�4�:�:�<�'8�1�'<��4�:�:�<��?�+�H�
�_�_�V�
$��T�Z�Z�\�):�Q�)>��4�:�:�<��?�+�H�&�
�h��rc��|D]M}|j�}t|�dk7r�"t|d�}|t|d�z}||k\s�G||ks�Myy)zSearch for an ID in a map fd�r2r1TF)r4r5r�)�mapfdr�rC�fields�
host_start�host_ends      r�
search_mapr��s[����������v�;�!�������^�
���F�1�I��.���*�������rc��td�5}|j�j�}ddd�|S#1swYSxYw)zGets the kernel boot idz/proc/sys/kernel/random/boot_idN)r?r@�strip)rD�boot_ids  r�get_boot_idr�s6��
�/�	0�A��&�&�(�.�.�"��
1��N�
1��N�s�5�?�proc_pid_fd�returnc	�:��dtdtf�fd�}tddd|��5}|j�jd�}d	d	d	�siSt	|jd
�D�cgc]}d|vs�|jdd���c}�S#1swY�JxYwcc}w)
z�Get the process environ from a proc directory file descriptor.

    Raises an OSError in case the environ file could not been read.
    �flagsrc�4��tj||���S)N��dir_fd)r%r?)rrrs  �r�openerz#get_process_environ.<locals>.opener�s����w�w�t�U�;�7�7r�environzutf-8r|)�encoding�errorsr
z N�r0r2)r�r?r@�rstrip�dictr4)rr
�
environ_fdr�entrys`    r�get_process_environr�s����8�C�8�C�8�
��G�I�f�
�	��/�/�#�*�*�5�1��
�
��	��*1�-�-��*=�N�*=��������S�!�	�*=�N���
�
��	Os� B�'	B�1B�Bc�V�|�y	tjd|��S#t$rYywxYw)z;Gets the process path from a proc directory file descriptor�unknown�exer)r%�readlinkr*)rs r�get_process_pathrs5�������{�{�5��5�5������s��	(�(c��|�d}d}n4|�2td|z�5}|j�}ddd�t�}|�t|�}|j	dd�j	dd�}|�tj�}d|�d|�dt��dt|��dt|���
}t
jjt|�}||fS#1swY��xYw)zGet the path to a core fileNrz
/proc/%s/statrzr{rnzcore.)r?r@r�rr|r%�getuidrrrrO�core_dir)	r�rr��	timestampr�	stat_file�
stat_contents�	core_name�	core_paths	         r�
get_core_pathr!s����{����	����o��+�,�	� )��� 0�
�-�%�m�4�I�
�{��{�+��

�+�+�c�3�
�
'�
'��S�
1�C�
�{��i�i�k���+.�s�K�M�*-�c�(�C�	�N�D�I������X�y�1�I��y�!�!�%-�,�s�C�Cc��t|�}g}g}tjjt�rtj
t��}|D]n}	|j
d�d|k(rTtjjtjjt|��}|j||g��p|S#ttf$rY��wxYw)z�Searches the core file directory for files that belong to a
       specified uid. Returns a list of lists containing the filename and
       the file modification time.)rrnr1)rr%rrqr�listdirr4�getmtimerOr��
IndexError�FileNotFoundError)r��
core_files�	uid_filesrDr�s     r�find_core_files_by_uidr),s����c�(�C��J��I�	�w�w�~�~�h���Z�Z�X�.�
�
��	��w�w�s�|�A��#�%��w�w�'�'������X�q�(A�B��� � �!�T��+��	�����-�.�	��	�s�A+C�C�Cc�Z�t|�}t|td���}t|�tdz
kDrutt|�tz
dz�D]S}t
jtjjt|dd��|j|d��Uyy)zcRemoves old files from the core directory if there are more than
       the maximum allowed per uidr2)�keyrN)r)�sortedrr5�max_corefiles_per_uid�ranger%�removerrOr)r�r(�sorted_files�xs    r�clean_core_directoryr2As���'�s�+�I��)��A��7�L��9�~�-��1�1��s�9�~�(=�=��A�B�A��I�I�b�g�g�l�l�8�\�!�_�Q�-?�@�A�����Q��0�C�2rc���i}tjd|gtjtjd��}|jD]e}	|jdd�\}}|j�}d|vr�.|j�dj�}|jd�r�a|||<�g|jj�|j�|jdk7riS|S#t$rY��wxYw)	z�Get libraries with which the specified binary is linked.

    Return a library name -> path mapping, for example 'libc.so.6' ->
    '/lib/x86_64-linux-gnu/libc.so.6'.
    �lddT)r�r��universal_newlinesz=>r2z
linux-vdsor�()r(r�r��STDOUTr�r4r}rr3r��waitr�)r�libsr4rCrR�rests      r�shared_librariesr;Os����D�
�
�
�E�4�=����",�"3�"3�.2�4�C��
�
��	����D�!�,�J�D�$��z�z�|���4����z�z�|�A��$�$�&���?�?�3�����T�
���J�J�����H�H�J�
�~�~����	��K��#�	��	�s�
C+�+	C7�6C7c�^�t|�}||vry|D]}|j|dz�s�yy)z�Check if the binary at path links with the library named lib.

    path should be a fully qualified path (e.g. report['ExecutablePath']),
    lib may be of the form 'lib<name>' or 'lib<name>.so.<version>'
    Tz.so.F)r;r3)r�libr9�
linked_libs    r�links_with_shared_libraryr?qs<���D�!�D�
�d�{���
�� � ��v��.���rr)NNF)NNNNN)I�__doc__r%r�r(�os.pathr�r�r�rfr]r�http.clientr�
contextlibr�operatorr�configparserrrrr�ImportError�problem_reportr	�apport.packaging_implr
r<rr�r�rr-r�rYrr�objectrr+r9rErKrTrdrlrwr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rr�rrrrr!r)r2r;r?r#rr�<module>rJs\��6�I�H�H�H�H����9�9�9��=�.�1�)�3�
�Z�Z�^�^�/��
>�
�%����*��"��	�d�k�k�0�0�&�	�	��6�8%�(,�"
� <�

�

�"�>��$�6G�
��([�4�2.�b�
��	%�
 � 
� ��S��T�#�s�(�^��*�"�:�*1��D��y�9�9�9�9�s�D
�
D!� D!