Impossible to get exit status after ssh_channel_request_send_signal(TERM)
I am currently extending pylibssh with a call to ssh_channel_request_send_signal(), but it seems to be impossible to get confirmation that the process exited. See the following python snippet. Please note, that it contains code from my development branch [1] but I suppose you can easily rewrite it in C.
[1] https://github.com/pbrezina/pylibssh/tree/extras
import logging
from pylibsshext.errors import LibsshSessionException
from pylibsshext.session import Session
ssh = Session()
ssh.set_log_level(logging.DEBUG)
ssh.connect(
host="client.test",
user="root",
password="Secret123",
timeout=1,
port=22,
host_key_checking=False,
)
ssh_channel = ssh.new_channel()
try:
ssh_channel.request_exec("cat")
ssh_channel.send_signal("TERM")
while not ssh_channel.is_eof():
ssh_channel.poll(timeout=1)
ssh_channel.recv()
print(ssh_channel.is_eof()) # Yields True now
rc = -1
while rc == -1:
rc = ssh_channel.get_channel_exit_status()
print(rc)
finally:
ssh_channel.close()
After reading data, we know we reached EOF but get_channel_exit_status() just spins with -1 so the code hangs. Currently, I don't see any function that would check that the process was signaled and did not exit with exist code.
The signal was correctly processes and the process was terminated with TERM as log says:
[2024/02/02 14:34:12.845350, 3] channel_request: Sent a SSH_MSG_CHANNEL_REQUEST signal
[2024/02/02 14:34:12.845885, 3] ssh_packet_socket_callback: packet: read type 96 [len=16,padding=10,comp=5,payload=5]
[2024/02/02 14:34:12.845897, 3] ssh_packet_process: Dispatching handler for packet type 96
[2024/02/02 14:34:12.845913, 3] channel_rcv_eof: Received eof on channel (43:0)
[2024/02/02 14:34:12.845918, 3] ssh_packet_socket_callback: Processing 104 bytes left in socket buffer
[2024/02/02 14:34:12.845934, 3] ssh_packet_socket_callback: packet: read type 98 [len=48,padding=9,comp=38,payload=38]
[2024/02/02 14:34:12.845939, 3] ssh_packet_process: Dispatching handler for packet type 98
[2024/02/02 14:34:12.845945, 3] channel_rcv_request: Remote connection closed by signal SIG TERM
[2024/02/02 14:34:12.845950, 3] ssh_packet_socket_callback: Processing 36 bytes left in socket buffer
[2024/02/02 14:34:12.845956, 3] ssh_packet_socket_callback: packet: read type 97 [len=16,padding=10,comp=5,payload=5]
[2024/02/02 14:34:12.845960, 3] ssh_packet_process: Dispatching handler for packet type 97
[2024/02/02 14:34:12.845966, 3] channel_rcv_close: Received close on channel (43:0)
[2024/02/02 14:34:12.845972, 3] ssh_packet_need_rekey: rekey: [data_rekey_needed=0, out_blocks=9, in_blocks=47]
[2024/02/02 14:34:12.845977, 3] ssh_packet_need_rekey: rekey: [data_rekey_needed=0, out_blocks=9, in_blocks=47]
[2024/02/02 14:34:12.845991, 3] ssh_packet_need_rekey: rekey: [data_rekey_needed=0, out_blocks=9, in_blocks=47]