HEX
Server: nginx/1.30.4
System: Linux d-twindigital-web-1.host-ed.eu 4.18.0-553.141.2.el8_10.x86_64 #1 SMP Wed Jul 8 10:28:18 EDT 2026 x86_64
User: u_twindigital_html (1009)
PHP: 8.3.30
Disabled: NONE
Upload Files
File: //lib/python3.6/site-packages/fail2ban/server/__pycache__/utils.cpython-36.pyc
3

=�Z]�-�@s�dZdZdZddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZmZm
Z
ejdkrlddlZnddlZee�Zdd	iZed
d�ejj�D��ZGdd
�d
�ZdS)z2Serg G. Brester (sebres) and Fail2Ban ContributorszYCopyright (c) 2004 Cyril Jaquier, 2011-2012 Yaroslav Halchenko, 2012-2015 Serg G. BresterZGPL�N�)�	getLogger�_merge_dicts�
uni_decode��am"Command not found".  Make sure that all commands in %(realCmd)r are in the PATH of fail2ban-server process (grep -a PATH= /proc/`pidof -x fail2ban-server`/environ). You may want to start "fail2ban-server -f" separately, initiate it with "fail2ban-client reload" in another shell session and observe if additional informative error messages appear in the terminals.ccs$|]\}}|jd�r||fVqdS)ZSIGN)�
startswith)�.0�nameZnum�r�/usr/lib/python3.6/utils.py�	<genexpr>5sr
c@s�eZdZdZdZdZdZedZGdd�de�Z	e
dd	��Ze
d
d��Ze
ddd��Z
e
ddd��Zejdkr|e
dd��Zne
dd��Ze
dd��ZdS)�UtilszPUtilities provide diverse static methods like executes OS shell commands, etc.
	rg�������?g����MbP?�dc@sDeZdZdZdd�Zddd�Zdd	�Zddd�Zd
d�Zdd�Z	d
S)zUtils.Cachez.A simple cache with a TTL and limit on size
		cOs|j||�i|_dS)N)�
setOptions�_cache)�self�args�kwargsrrr�__init__FszUtils.Cache.__init__���<cCs||_||_dS)N)�maxCount�maxTime)rrrrrrrJszUtils.Cache.setOptionscCs
t|j�S)N)�lenr)rrrr�__len__NszUtils.Cache.__len__NcCs4|jj|�}|r0|dtj�kr(|dS|j|=|S)N�r)r�get�time)r�kZdefv�vrrrrQszUtils.Cache.getcCsttj�}|j}t|�|jkr^x*t|j��D]\}}|d|kr*||=q*Wt|�|jkr^|j�|||jf||<dS)Nr)rrrr�list�items�popitemr)rrr �t�cacheZckZcvrrr�setYs
zUtils.Cache.setcCs&y|j|=Wntk
r YnXdS)N)r�KeyError)rrrrr�unsetfszUtils.Cache.unset)rr)N)
�__name__�
__module__�__qualname__�__doc__rrrrr&r(rrrr�CacheBs


r-cCs>tj|tj�}|s|tjO}n|tjM}tj|tj|�|S)N)�fcntlZF_GETFL�os�
O_NONBLOCKZF_SETFL)Zfhandle�value�flagsrrr�
setFBlockModemszUtils.setFBlockModecCspd}t|t�s|g}t|�d}x6|j�D]*\}}|d||f7}|j|�|d7}q*W|d|d|d<|S)a�Generates new shell command as array, contains map as variables to
		arguments statement (varsStat), the command (realCmd) used this variables and
		the list of the arguments, mapped from varsDict

		Example:
			buildShellCmd('echo "V2: $v2, V1: $v1"', {"v1": "val 1", "v2": "val 2", "vUnused": "unused var"})
		returns:
			['v1=$0 v2=$1 vUnused=$2 
echo "V2: $v2, V1: $v1"', 'val 1', 'val 2', 'unused var']
		�rz%s=$%s �
r)�
isinstancer!rr"�append)�realCmd�varsDictZvarsStat�irr rrr�
buildShellCmdws

zUtils.buildShellCmdrTFrNc1szd}}d}	d�}
|r6|r*tj�|��nttj|�}
t�����fdd�}�ytj�tjtj||
tj	d���j
�}	|	dkr��fdd�}tj||tj�}	|	r�|	d}	|	dk�rZ|r�|t
j�d}tjd�|f�tj�j�}
tj|
tj�tjtj��j
�}	|	dk�s|�r<tj|
tj�tjtj�|	dk�r<�j
�}	|	dk�rZtj|
��rZtj}	Wnjtk
�r�}zL|�r�|t
j�d}d	�|f}tj|���s�|�s�d
Sd
|||	fSWYdd}~XnX|	|k�r�t
jnt
j}|tj�k�r|�r||d�d}|�s|tj�k�r��j�r�y0|	dk�s4|	dk�rBtj�jd
��jj �}Wn0t!k
�r~}ztjd|�WYdd}~XnX|dk	�r�|d
k�r�|tj�k�r�x&|j"�D]}tj#|d�t$|���q�W�jj%��j&�r�y0|	dk�s�|	dk�rtj�j&d
��j&j �}Wn0t!k
�r<}ztjd|�WYdd}~XnX|dk	�r�|d
k�r�|tj�k�r�x&|j"�D]}tj#|d�t$|���qjW�j&j%�d
}|	|k�r�tj'd�|	�d}n�|	dk�r�tjd��j�n~|	dk�s�|	dk�r|	dk�r�|	n|	d}tjd�t(j)|d|�|	�n4t*j)|	d�}tjd�|	�|�rNtj+d|	|t,��|�r`||||	fSt-|�dk�rr|S||	fS)aExecutes a command.

		Parameters
		----------
		realCmd : str
			The command to execute.
		timeout : int
			The time out in seconds for the command.
		shell : bool
			If shell is True (default), the specified command (may be a string) will be 
			executed through the shell.
		output : bool
			If output is True, the function returns tuple (success, stdoutdata, stderrdata, returncode).
			If False, just indication of success is returned
		varsDict: dict
			variables supplied to the command (or to the shell script)

		Returns
		-------
		bool or (bool, str, str, int)
			True if the command succeeded and with stdout, stderr, returncode if output was set to True

		Raises
		------
		OSError
			If command fails to be executed.
		RuntimeError
			If command execution times out.
		Ncstj|d���S)Nz%x -- exec: %s)�logSys�log)�level)r8�	realCmdIdrr�<lambda>�sz"Utils.executeCmd.<locals>.<lambda>)�stdout�stderr�shell�envZ
preexec_fncs�j�}|dk	rd|fSdS)NT)�poll)�retcode)�popenrr�_popen_wait_end�sz)Utils.executeCmd.<locals>._popen_wait_endrz!%x -- timed out after %s seconds.z%s -- failed with %sFrz  ... -- failed to read stdout %sr4z%x -- stdout: %rz  ... -- failed to read stderr %sz%x -- stderr: %rz%x -- returned successfully %iTz%x -- unable to kill PID %i�z&%x -- killed with %s (return code: %s)z	signal %iz%x -- returned %izHINT on %i: %s).rr;rr/�environ�id�
subprocess�Popen�PIPE�setsidrE�wait_for�DEFAULT_SHORTEST_INTERVAL�loggingZERRORr<�error�getpgid�pid�killpg�signal�SIGTERMr�sleep�DEFAULT_SLEEP_INTERVAL�SIGKILL�
pid_exists�OSError�DEBUGZgetEffectiveLevelrAr3�read�IOError�
splitlinesr=r�closerB�debug�signamer�_RETCODE_HINTS�info�localsr)r8�timeoutrC�outputZtout_kill_treeZ
success_codesr9rArBrFrDZlogCmdrHZpgid�eZ	std_level�l�successZsigcode�msgr)rGr8r?r�
executeCmd�s� 






("
"


zUtils.executeCmdcszd}xp|�}|r|S|rPd}}t|�sBtj�|��fdd�}n|}|sPtj}|�rXPt||tj�}tj|�qW|S)a5Wait until condition expression `cond` is True, up to `timeout` sec

		Parameters
		----------
		cond : callable
			The expression to check condition 
			(should return equivalent to bool True if wait successful).
		timeout : float or callable
			The time out for end of wait
			(in seconds or callable that returns True if timeout occurred).
		interval : float (optional)
			Polling start interval for wait cycle in seconds.

		Returns
		-------
		variable
			The return value of the last call of `cond`, 
			logical False (or None, 0, etc) if timeout occurred.
		rrcstj��kS)N)rr)�time0rrr@3sz Utils.wait_for.<locals>.<lambda>)�callablerrrZ�min�DEFAULT_SLEEP_TIMErY)ZcondrhZintervalZini�retZstmZtimeout_exprr)rorrPs$zUtils.wait_for�posixcCsVddl}|dkrdSytj|d�Wn(tk
rL}z|j|jkSd}~XnXdSdS)z6Check whether pid exists in the current process table.rNFT)�errnor/�killr]ZEPERM)rUrurjrrrr\AszUtils.pid_existscCs@ddl}|jj}d}|j|d|�}|dkr8|j|�dSdSdS)NriTF)�ctypesZwindll�kernel32ZOpenProcessZCloseHandle)rUrwrxZSYNCHRONIZEZprocessrrrr\Ns
cCsFtjjtjj|��d}tjdkr6tjj||�j	�}nt
j||�}|S)Nrr)rr)r/�path�splitext�basename�sys�version_info�	importlib�	machinery�SourceFileLoader�load_module�impZload_source)ZpythonModuleZpythonModuleName�modrrr�load_python_module[s
zUtils.load_python_module�r)rTFTr�N)N)r)r*r+r,rrrZZDEFAULT_SHORT_INTERVALrQ�objectr-�staticmethodr3r;rnrPr/r
r\r�rrrrr8s$+
+


r)rr)�
__author__Z
__copyright__Z__license__r.rRr/rWrLr|rZhelpersrrrr}Zimportlib.machineryr~r�r)r<re�dict�__dict__r"rdrrrrr�<module>s$