HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.109.1.lve.el8.x86_64 #1 SMP Thu Mar 5 20:23:46 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/__pycache__/vt.cpython-310.pyc
o

�N�g�}�@s,dZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
ZddlZddl
ZddlmZejdkZz ddlZddlZddlZddlZddlmZmZddlmZWneyvddlZddlZddlZddlZYnwe� e!�Z"Gdd�de#�Z$dd
d�Z%dd
�Z&Gdd�d�Z'dS)ab
    :codeauthor: Pedro Algarvio (pedro@algarvio.me)


    salt.utils.vt
    ~~~~~~~~~~~~~

    Virtual Terminal

    This code has been heavily inspired by Python's subprocess code, the `non
    blocking version of it`__, some minor online snippets about TTY handling
    with python including `Python's own ``pty`` source code`__ and `Pexpect`__
    which has already surpassed some of the pitfalls that some systems would
    get us into.

    .. __: http://code.activestate.com/recipes/440554/
    .. __: https://github.com/python/cpython/blob/3.3/Lib/pty.py
    .. __: https://github.com/pexpect/pexpect

�N)�
LOG_LEVELS�win32)�ReadFile�	WriteFile)�
PeekNamedPipec@seZdZdZdS)�TerminalExceptionz%
    Terminal specific exception
    N)�__name__�
__module__�__qualname__�__doc__�rr�A/opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/vt.pyr<sr�PcCs<ttdd�}|dkrd}t�d||dd�}t�|||�dS)a^
    This sets the terminal window size of the child tty. This will
    cause a SIGWINCH signal to be sent to the child. This does not
    change the physical window size. It changes the size reported to
    TTY-aware applications like vi or curses -- applications that
    respond to the SIGWINCH signal.

    Thank you for the shortcut PEXPECT
    �
TIOCSWINSZigt�lgt�HHHHrN)�getattr�termios�struct�pack�fcntl�ioctl)�child�rows�colsr�packedrrr
�
setwinsizeBs
rcCs@ttdd�}t�ddddd�}t�|||�}t�d|�dd�S)z�
    This returns the terminal window size of the child tty. The return
    value is a tuple of (rows, cols).

    Thank you for the shortcut PEXPECT
    �
TIOCGWINSZ�ht@rr�)rrrrrr�unpack)rrrrrrr
�
getwinsizeVsr c@s\eZdZdZdddddddddddddddddefdd�Zdd�Zejfd	d
�Z	d8dd�Z
d9dd�Zedd��Z
dd�Zdd�Zdd�Zercdd�Zdd�Zdd�Zdd�Zd d!�Zd:d"d#�ZeZdSd$d�Zed;d&d'��Zd(d�Zd)d�Zd*d+�Zd,d-�Zd<d.d/�Zejejej ej!ej"ej#ej$ej%e&j'e(f
d0d1�Z)d:d2d#�Zd3d4�Z*d5d!�Zd6d7�ZdS)=�Terminalz 
    I'm a virtual terminal
    NF�debugc
Cs�|s|std��||_||_||_||_||_||_||_|dur,|dur,|��\}}n|dur;|dur;|��\}}n|durI|durI|��\}}||_	||_
d|_d|_d|_
d|_d|_d|_d|_d|_d|_d|_d|_d|_d|_d|_d|_|dur�tj
|_n*|dur�d|_n"|dur�t|d�r�t|d�r�t|d�s�td��||_ntd	�|���|dur�tj|_n*|dur�d|_n"|dur�t|d�r�t|d�r�t|d�s�td
��||_ntd�|���z|��Wnt �y}zt!j"d|t#j$d
�td|����d}~wwt!�%d|j|j|j�|�r"t&|t'��st(d��||_)nd|_)d�*|j�}|j)�r6|�+|j)d�}d|v�s@d|v�rLt!�%d�t!�,d|�nt!�%d|�t-�.|
|
�|_/|	du�rqt#�0t1�d|j2j1�d|j�d��|_3n|	du�r�t&|	t#j4��s�t(d��|	|_3nd|_3t-�.||�|_5|du�r�t#�0d�t1|j2j1|j��|_6n|du�r�t&|t#j4��s�t(d��||_6nd|_6t-�.||�|_7|
du�r�t#�0d�t1|j2j1|j��|_8dS|
du�r�t&|
t#j4��s�t(d��|
|_8dSd|_8dS)Nz6You need to pass at least one of "args", "executable" �TF�write�flush�closezU'stream_stdout' needs to have at least 3 methods, 'write()', 'flush()' and 'close()'.zDDon't know how to handle '{}' as the VT's 'stream_stdout' parameter.zU'stream_stderr' needs to have at least 3 methods, 'write()', 'flush()' and 'close()'.zDDon't know how to handle '{}' as the VT's 'stream_stderr' parameter.zFailed to spawn the VT: %s)Zexc_info_on_loglevelzFailed to spawn the VT. Error: z3Child Forked! PID: %s  STDOUT_FD: %s  STDERR_FD: %sz%'log_sanitize' needs to be a str type� �******zdecode("base64")zbase64.b64decode(z<VT: Salt-SSH SHIM Terminal Command executed. Logged to TRACEzTerminal Command: %s�.z.PID-z.STDINz.'log_stdin' needs to subclass `logging.Logger`z{}.{}.PID-{}.STDOUTz/'log_stdout' needs to subclass `logging.Logger`z{}.{}.PID-{}.STDERRz/'log_stderr' needs to subclass `logging.Logger`)9r�args�
executable�shell�cwd�env�
preexec_fn�receive_encoding�&_Terminal__detect_parent_terminal_sizerr�pid�stdin�stdout�stderr�child_fd�	child_fde�partial_data_stdout�partial_data_stderr�closed�flag_eof_stdout�flag_eof_stderr�
terminated�
exitstatus�signalstatus�status�sys�
stream_stdout�hasattr�format�
stream_stderr�_spawn�	Exception�log�warning�logging�DEBUGr"�
isinstance�str�RuntimeError�log_sanitize�join�replaceZtracer�get�stdin_logger_level�	getLoggerr�	__class__�stdin_logger�Logger�stdout_logger_level�
stdout_logger�stderr_logger_level�
stderr_logger)�selfr*r+r,r-r.r/rrZ	log_stdinZlog_stdin_levelZ
log_stdoutZlog_stdout_levelZ
log_stderrZlog_stderr_levelrOrBrEZforce_receive_encoding�_�errZterminal_commandrrr
�__init__hs�
�����
���������



�

��

�
�


zTerminal.__init__cCs
|�|�S)zi
        Send data to the terminal. You are responsible to send any required
        line feeds.
        )�_send�r\�datarrr
�sends
z
Terminal.sendcCs|�|�|���S)zO
        Send the provided data to the terminal appending a line feed.
        )rc)r\rb�lineseprrr
�sendline&�zTerminal.sendlinecCs$|durd}n|dkr
d}|�|�S)z�
        Receive data from the terminal as a (``stdout``, ``stderr``) tuple. If
        any of those is ``None`` we can no longer communicate with the
        terminal's child process.
        Ni�)�_recv�r\�maxsizerrr
�recv,s

z
Terminal.recvTcCsl|js4|jdurt�|j�d|_|jdurt�|j�d|_t�d�|r/|�|�s/td��d|_dSdS)a
        Close the communication with the terminal's child.
        If ``terminate`` is ``True`` then additionally try to terminate the
        terminal, and if ``kill`` is also ``True``, kill the terminal if
        terminating it was not enough.
        N皙�����?z"Failed to terminate child process.T)	r:r6�osr&r7�time�sleep�	terminater)r\rp�killrrr
r&8s




�zTerminal.closecCs|jdup	|jduS)NF)r<r;�r\rrr
�has_unread_dataLszTerminal.has_unread_datacCs|dus|sdS|�dtj�S)Nz
)rQrmrdrarrr
�_translate_newlinesPszTerminal._translate_newlinescCs|S�Nrrrrrr
�	__enter__V�zTerminal.__enter__cCs&|jddd�|��r|��dSdS)NT)rprq)r&�isalive�wait)r\�exc_type�	exc_value�	tracebackrrr
�__exit__Ys�zTerminal.__exit__cC�t�ru��NotImplementedErrorrrrrr
�_executearwzTerminal._executecCr~rurrrrrr
rFdrwzTerminal._spawncCr~rurrirrr
rhgrwzTerminal._recvcCr~rurrarrr
r`jrwzTerminal._sendcCs`|tjkr|��dS|tjkrt�|jtj�dS|tjkr)t�|jtj�dStd|����)�6
            Send a signal to the process
            zUnsupported signal: N)	�signal�SIGTERMrpZCTRL_C_EVENTrmrqr2ZCTRL_BREAK_EVENT�
ValueError�r\�sigrrr
�send_signalms


zTerminal.send_signalcCsHz
t�|jd�WdSty#t�|j�}|tjkr�||_YdSw)z0
            Terminates the process
            rgN)	�win32apiZTerminateProcess�_handle�OSError�win32processZGetExitCodeProcess�win32conZSTILL_ACTIVEr>)r\�forceZecoderrr
rp|s
�zTerminal.terminatecCs�t|jt�s|jdurd�|j�|_t��\}}t��\}}t�	|�}t
j|jt�
|j||j|j|j�|j|j|j||||jdd�
}t�|�t�|�||_||_|j|_||_d|_d|_dS)NTr')	r/r,r+r-r3r4r5r.�	close_fdsF)rLr*rMr,rP�pty�openptyrm�pipe�ttyname�
subprocess�Popen�	functools�partial�_preexecrrr/r+r-r.r&r6r7r2�procr:r=)r\�parentrZ
err_parentZ	err_child�
child_namer�rrr
rF�s6
��


rcCs,zt�dtjtjB�}|dkrt�|�Wn	tyYnwzt��Wn	ty.Ynwzt�dtjtjB�}|dkrJt�|�td|����Wn	tyTYnwt�|tj�}t	|||�|dkrmtd|����t�|�tj
dkr�t�dtj�}|dkr�td��t�|�|dur�|�dSdS)Nz/dev/ttyrzCould not open child pty, �posixz(Could not open controlling tty, /dev/tty)rm�open�O_RDWR�O_NOCTTYr&rG�setsidr�rr�name�O_WRONLY)r�rrr/Ztty_fdrrr
r��sF
���
��



�zTerminal._preexecc
Cs�|jdurdSt�g|jggd�dsdSz|jr!|j�|j|�t�|j|�t��}W|St	yP}z|j
t
jkrKt�|j�d|_WYd}~dS�d}~ww)Nrrg)
r6�selectrVrHrSrmr$�encode�__salt_system_encoding__r��errnoZEPIPEr&)r\rbZwritten�whyrrr
r`�s"
���cs�g}�jr|��j��jr|��j����sX|s ���dSt�|ggd�\}}}|sXd�_�_t�	d��j
s>�jrR���tj
j��j
�tj
j��j�fS���dSd}d}�jrgt��jtj�}�jrrt��jtj�}�jr�t��jtj|tjB��jr�t��jtj|tjB�t�|ggd�\}}}|s����s�d�_�_t�	d�dSd�fdd�	}	�j|v�rK�j�sKzzzP|	�j|d	�\}}
|s�|
s�d�_d}n;�jr�j�|��j���j�r|��}�jr�|��jd
�}|�tj��r|ttj�d�}|�r�j��j|�Wnt �y(t��j�d�_d�_d}YnwW�jdu�r9t��jtj|�n�jdu�rJt��jtj|�ww�j|v�r�j�s�z�zY|	�j|d�\}}
|�sl|
�sld�_d}nB�j!�r��j!�tj
j�"|���j!���j#�r�|��}�j�r�|��jd
�}|�tj��r�|ttj�d�}|�r��j#��j$|�Wnt �y�t��j�d�_d�_d}YnwW�jdu�r�t��jtj|�||fS�jdu�r�t��jtj|�ww||fS)
N)NNrTz&End of file(EOL). Brain-dead platform.�z%End of file(EOL). Very slow platform.c
s�t�|d�}|t�||s|nd�7}z��tjj�|�j��}|dur)t	�|d�|dfWSt
y_}z&d}|jt|�|krZ|j
t|�krZ|durRt	�||�n�WYd}~dS�d}~ww)Nr#rgF�)r�T)rrm�readrt�salt�utils�stringutils�
to_unicoder0�setattr�UnicodeDecodeError�start�len�end)�fdrjZpartial_data_attrZ
bytes_readZdecoded_data�exZmax_multibyte_character_lengthrrrr
�read_and_decode_fds0��

�	��z*Terminal._recv.<locals>.read_and_decode_fdr9r(r8ru)%r6�appendr7rxr&r�r;r<rHr"r8r9r�r�r�r�r�F_GETFL�F_SETFLrm�
O_NONBLOCKrEr$r%r[�rstriprOrQ�
startswithrdr�rZr�rBZto_strrYrX)r\rjZrfdsZrlistr]r5r4Zfd_flagsZ	fde_flagsr�Zpartial_data�strippedrrrr
rh�s�
�
'�
����������
�������cCs^z$ttdd�}t�ddddd�}t�tj��||�}t�	d|�dd�WSt
y.YdSw)Nrrrrr)�r)rrrrrrrAr3�filenorr�)r\rrrrrr
Z__detect_parent_terminal_size�s�z&Terminal.__detect_parent_terminal_sizecCs|jdur	td��t|j�S)z�
            This returns the terminal window size of the child tty. The return
            value is a tuple of (rows, cols).

            Thank you for the shortcut PEXPECT
            NzTCan't check the size of the terminal since we're not connected to the child process.)r6rr rrrrr
r �s

�
zTerminal.getwinsizecCst|j||�dSru)rr6)r\rrrrrr
r�szTerminal.setwinsizec
CsZ|jrdS|jdur
d}n|}z
||j|�\}}
Wn0ty3|j��}|dur,YdS|j}|}
Yn|yIt��d}|j|	krH|
d���w|dkruz
||j|�\}}
Wn|yn}z
|j|	kri|
d���d}~ww|dkrudS|dkr{dS||
�r�|
|_	||
�|_
d|_d|_dS||
�r�|
|_	d|_
||
�|_d|_dS||
�r�|
d��dS)	a�
            This tests if the child process is running or not. This is
            non-blocking. If the child was terminated then this will read the
            exitstatus or signalstatus of the child. This returns True if the
            child process appears to be running or False if not. It can take
            literally SECONDS for Solaris to return the right status.
            FrNTrgz�isalive() encountered condition where "terminated" is 0, but there was no child process. Did someone else call waitpid() on our process?z�isalive() encountered condition that should never happen. There was no child process. Did someone else call waitpid() on our process?z�isalive() encountered condition where child process is stopped. This is not supported. Is some other process attempting job control with our child pid?)r=rsr2�ChildProcessErrorr��pollrA�exc_infor�r@r>r?)r\�_waitpidZ_wnohangZ
_wifexitedZ_wexitstatusZ_wifsignaledZ_wifstoppedZ	_wtermsigZ	_os_errorZ
_errno_echildZ_terminal_exceptionZwaitpid_optionsr2r@Zpolledr^�excrrr
rx�sj


��
���
�
��zTerminal.isalivecCs�|js	|jdd�|��sdSzP|�tj�t�d�|��s"WdS|�tj�t�d�|��s4WdS|�tj	�t�d�|��sFWdS|r]|�tj
�t�d�|��sZWdSWdSWdStyut�d�|��srYdSYdSw)a$
            This forces a child process to terminate. It starts nicely with
            SIGHUP and SIGINT. If "force" is True then moves onto SIGKILL. This
            returns True if the child was terminated. This returns False if the
            child could not be terminated.
            F)rpTrl)r:r&rxr�r��SIGHUPrnro�SIGCONT�SIGINT�SIGKILLr�)r\r�rrr
rp
s<




�cCsR|��r%|��r"|��\}}|dur	|jS|dur	|jS|��s|jStd��)z�
            This waits until the child exits internally consuming any remaining
            output from the child, thus, no blocking forever because the child
            has unread data.
            Nz#Cannot wait for dead child process.)rxrkrr>)r\r4r5rrr
ry6s���z
Terminal.waitcCst�|j|�dS)r�N)rmrqr2r�rrr
r�GrfcCs|�tj�dS)z7
            Kill the process with SIGKILL
            N)r�r�r�rrrrr
rqMsz
Terminal.killru)TF)F)rrN�rr)+rr	r
rr�r_rcrmrdrerkr&�propertyrsrtrvr}�	mswindowsr�rFrhr`r�rprq�staticmethodr�r1r r�waitpid�WNOHANG�	WIFEXITED�WEXITSTATUS�WIFSIGNALED�
WIFSTOPPED�WTERMSIG�errorr�ZECHILDrrxryrrrr
r!cs~
�8



(!

�
f,r!r�)(rr�r�rJrmr�r�r�rArnZsalt.utils.cryptr�Zsalt.utils.dataZsalt.utils.stringutilsZ
salt._loggingr�platformr��msvcrtr�r�r�Z	win32filerrZ	win32piper�ImportErrorrr�rrrTrrHrGrrr r!rrrr
�<module>sB
�