[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Catching SIGINT during ssh_channel_request_exec
[Thread Prev] | [Thread Next]
- Subject: Catching SIGINT during ssh_channel_request_exec
- From: timo.pallach@xxxxxx
- Reply-to: libssh@xxxxxxxxxx
- Date: Fri, 11 Apr 2014 10:40:33 +0200
- To: libssh@xxxxxxxxxx
Hi,
I slightly modified the exec.c example and added a signal handler to catch the SIGINT signal.
I also added a loop to be able to enter multiple commands, one after the other (like a little shell).
When I execute the command "ping localhost" via ssh_channel_request_exec and send a SIGINT (e.g. ctrl+c) the signal handler is executed but the application exits.
I also added a loop to be able to enter multiple commands, one after the other (like a little shell).
When I execute the command "ping localhost" via ssh_channel_request_exec and send a SIGINT (e.g. ctrl+c) the signal handler is executed but the application exits.
Is there a possibility to stop the command executed via ssh_channel_request_exec using the ctrl+c key combination without exiting application?
Attached you find the modified exec.c sources.
===============================================================================
/* simple exec example */
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <libssh/libssh.h>
#include "examples_common.h"
===============================================================================
/* simple exec example */
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <libssh/libssh.h>
#include "examples_common.h"
void handler(int signum)
{
printf("signum=<%d>\n", signum);
}
{
printf("signum=<%d>\n", signum);
}
int main(void)
{
ssh_session session;
ssh_channel channel;
char buffer[256];
int nbytes;
int rc;
char command[256];
{
ssh_session session;
ssh_channel channel;
char buffer[256];
int nbytes;
int rc;
char command[256];
signal(SIGINT, handler);
session = connect_ssh("localhost", NULL, 0);
if (session == NULL) {
ssh_finalize();
return 1;
}
if (session == NULL) {
ssh_finalize();
return 1;
}
// Show prompt and only exit if user enters "exit".
while(1)
{
printf("# ");
if (fgets(command, sizeof(command), stdin) == NULL)
continue;
while(1)
{
printf("# ");
if (fgets(command, sizeof(command), stdin) == NULL)
continue;
// Exit loop if "exit was entered by user.
if (strcmp(command, "exit\n") == 0)
return 0;
if (strcmp(command, "exit\n") == 0)
return 0;
channel = ssh_channel_new(session);;
if (channel == NULL) {
ssh_disconnect(session);
ssh_free(session);
ssh_finalize();
return 1;
}
if (channel == NULL) {
ssh_disconnect(session);
ssh_free(session);
ssh_finalize();
return 1;
}
rc = ssh_channel_open_session(channel);
if (rc < 0) {
goto failed;
}
if (rc < 0) {
goto failed;
}
rc = ssh_channel_request_exec(channel, command);
if (rc < 0) {
goto failed;
}
if (rc < 0) {
goto failed;
}
nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
while (nbytes > 0) {
if (fwrite(buffer, 1, nbytes, stdout) != (unsigned int) nbytes) {
goto failed;
}
nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
}
while (nbytes > 0) {
if (fwrite(buffer, 1, nbytes, stdout) != (unsigned int) nbytes) {
goto failed;
}
nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
}
if (nbytes < 0) {
goto failed;
}
goto failed;
}
ssh_channel_send_eof(channel);
ssh_channel_close(channel);
ssh_channel_free(channel);
}
ssh_channel_close(channel);
ssh_channel_free(channel);
}
ssh_disconnect(session);
ssh_free(session);
ssh_finalize();
ssh_free(session);
ssh_finalize();
return 0;
failed:
ssh_channel_close(channel);
ssh_channel_free(channel);
ssh_disconnect(session);
ssh_free(session);
ssh_finalize();
failed:
ssh_channel_close(channel);
ssh_channel_free(channel);
ssh_disconnect(session);
ssh_free(session);
ssh_finalize();
return 1;
}
===============================================================================
}
===============================================================================
Archive administrator: postmaster@lists.cynapses.org