summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2015-08-14 11:24:41 +0200
committerAndreas Baumann <mail@andreasbaumann.cc>2015-08-14 11:24:41 +0200
commit1255c67a0af375a55ec1afb4f552541ed9a365ef (patch)
tree9810c774cf5a169fbf565c1640f262de1bc23d8d
parent270bfc677d74c9ecad26bcc6c9df1b7f23454701 (diff)
downloadcssh-1255c67a0af375a55ec1afb4f552541ed9a365ef.tar.gz
cssh-1255c67a0af375a55ec1afb4f552541ed9a365ef.tar.bz2
changed to polling read
-rw-r--r--src/CMakeLists.txt3
-rw-r--r--src/cssh.c65
-rw-r--r--src/cssh_options.ggo7
-rw-r--r--src/msleep.c11
-rw-r--r--src/msleep.h6
5 files changed, 74 insertions, 18 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 0bde562..71a8023 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -2,10 +2,11 @@ include_directories(${LIBSSH_PUBLIC_INCLUDE_DIRS})
set(SRC
cssh.c
+ msleep.c
)
ADD_GENGETOPT_FILES(SRC cssh_options.ggo)
add_executable(cssh ${SRC})
-target_link_libraries(cssh ssh)
+target_link_libraries(cssh ssh ssh_threads)
diff --git a/src/cssh.c b/src/cssh.c
index f3ac800..85b5b00 100644
--- a/src/cssh.c
+++ b/src/cssh.c
@@ -1,4 +1,5 @@
#include <libssh/libssh.h>
+#include <libssh/callbacks.h>
#include <stdlib.h>
#include <stdio.h>
@@ -8,6 +9,7 @@
#include <unistd.h>
#include "cssh_options.h"
+#include "msleep.h"
#define CSSH_VERSION "0.0.1"
@@ -207,6 +209,9 @@ int main( int argc, char *argv[] )
if( parse_options_and_arguments( argc, argv, &args_info ) != 0 ) {
exit( EXIT_FAILURE );
}
+
+ ssh_threads_set_callbacks( ssh_threads_get_pthread( ) );
+ ssh_init( );
if( args_info.long_version_given ) {
printf( "cssh version: %s\n", CSSH_VERSION );
@@ -325,13 +330,13 @@ int main( int argc, char *argv[] )
exit( EXIT_FAILURE );
}
- char buffer[256];
- unsigned int nread = ssh_channel_read( channel, buffer, sizeof( buffer ), 0 );
- while( nread > 0 ) {
- size_t wrc = fwrite( buffer, 1, nread, stdout );
- if( wrc < 0 ) {
- fprintf( stderr, "ERROR: while writting to stdout: %s\n",
- strerror( errno ) );
+ while( ssh_channel_is_open( channel ) && !ssh_channel_is_eof( channel ) ) {
+ char buffer[256];
+
+ rc = ssh_channel_poll( channel, 0 );
+ if( rc == SSH_ERROR ) {
+ fprintf( stderr, "ERROR: ssh_channel_poll on stdout failed: %s\n",
+ ssh_get_error( session ) );
ssh_channel_close( channel );
ssh_channel_free( channel );
ssh_disconnect( session );
@@ -339,17 +344,45 @@ int main( int argc, char *argv[] )
exit( EXIT_FAILURE );
}
- if( wrc != nread ) {
- fprintf( stderr, "ERROR: Write mismatch on stdout (%zu != %d)\n",
- wrc, nread );
- ssh_channel_close( channel );
- ssh_channel_free( channel );
- ssh_disconnect( session );
- ssh_free( session );
- exit( EXIT_FAILURE );
+ if( rc == 0 ) {
+ cssh_msleep( 10 );
}
- nread = ssh_channel_read( channel, buffer, sizeof( buffer ), 0 );
+ if( rc > 0 ) {
+ unsigned int nread = ssh_channel_read_nonblocking( channel, buffer, sizeof( buffer ), 0 );
+ if( nread == SSH_ERROR ) {
+ fprintf( stderr, "ERROR: ssh_channel_read_nonblocking on stdout failed: %s\n",
+ ssh_get_error( session ) );
+ ssh_channel_close( channel );
+ ssh_channel_free( channel );
+ ssh_disconnect( session );
+ ssh_free( session );
+ exit( EXIT_FAILURE );
+ }
+
+ if( nread > 0 ) {
+ size_t wrc = fwrite( buffer, 1, nread, stdout );
+ if( wrc < 0 ) {
+ fprintf( stderr, "ERROR: while writting to stdout: %s\n",
+ strerror( errno ) );
+ ssh_channel_close( channel );
+ ssh_channel_free( channel );
+ ssh_disconnect( session );
+ ssh_free( session );
+ exit( EXIT_FAILURE );
+ }
+
+ if( wrc != nread ) {
+ fprintf( stderr, "ERROR: Write mismatch on stdout (%zu != %d)\n",
+ wrc, nread );
+ ssh_channel_close( channel );
+ ssh_channel_free( channel );
+ ssh_disconnect( session );
+ ssh_free( session );
+ exit( EXIT_FAILURE );
+ }
+ }
+ }
}
ssh_channel_send_eof( channel );
diff --git a/src/cssh_options.ggo b/src/cssh_options.ggo
index def8c02..0a06621 100644
--- a/src/cssh_options.ggo
+++ b/src/cssh_options.ggo
@@ -1,6 +1,6 @@
package "cssh"
version "0.0.1"
-usage "cssh [options] [hostname] [command]"
+usage "cssh [options] [user@][hostname] [command]"
description "Execute a command on a set of machines"
section "Main Options"
@@ -22,3 +22,8 @@ section "Main Options"
int typestr="port"
optional
+ option "hosts" H
+ "List of hosts to use in parallel"
+ string typestr="host"
+ multiple optional
+
diff --git a/src/msleep.c b/src/msleep.c
new file mode 100644
index 0000000..607cd02
--- /dev/null
+++ b/src/msleep.c
@@ -0,0 +1,11 @@
+#include "msleep.h"
+
+#include <time.h>
+
+void cssh_msleep( unsigned int msec )
+{
+ struct timespec ts;
+ ts.tv_sec = msec / 1000;
+ ts.tv_nsec = ( msec % 1000 ) * 1000000;
+ nanosleep( &ts, NULL );
+}
diff --git a/src/msleep.h b/src/msleep.h
new file mode 100644
index 0000000..bfe8c7a
--- /dev/null
+++ b/src/msleep.h
@@ -0,0 +1,6 @@
+#ifndef _USLEEP_HEADER_INCLUDED
+#define _USLEEP_HEADER_INCLUDED
+
+void cssh_msleep( unsigned int msec );
+
+#endif