44#include <sys/types.h>
55#include <sys/wait.h>
66#include <signal.h>
7+ #include <pthread.h>
78
89#include "pg_probackup.h"
910#include "file.h"
1011
1112#define MAX_CMDLINE_LENGTH 4096
1213#define MAX_CMDLINE_OPTIONS 256
13- #define ERR_BUF_SIZE 1024
14+ #define ERR_BUF_SIZE 4096
1415
1516static int append_option (char * buf ,size_t buf_size ,size_t dst ,char const * src )
1617{
@@ -78,6 +79,37 @@ static void kill_child(void)
7879kill (child_pid ,SIGTERM );
7980}
8081
82+ static void * error_reader_proc (void * arg )
83+ {
84+ int * errfd = (int * )arg ;
85+ char buf [ERR_BUF_SIZE ];
86+ int offs = 0 ,rc ;
87+
88+ while ((rc = read (errfd [0 ],& buf [offs ],sizeof (buf )- offs ))> 0 )
89+ {
90+ char * nl ;
91+ offs += rc ;
92+ buf [offs ]= '\0' ;
93+ nl = strchr (buf ,'\n' );
94+ if (nl != NULL ) {
95+ * nl = '\0' ;
96+ if (strncmp (buf ,"ERROR: " ,7 )== 0 ) {
97+ elog (ERROR ,"%s" ,buf + 7 );
98+ }if (strncmp (buf ,"WARNING: " ,9 )== 0 ) {
99+ elog (WARNING ,"%s" ,buf + 9 );
100+ }else if (strncmp (buf ,"LOG: " ,5 )== 0 ) {
101+ elog (LOG ,"%s" ,buf + 5 );
102+ }else if (strncmp (buf ,"INFO: " ,6 )== 0 ) {
103+ elog (INFO ,"%s" ,buf + 6 );
104+ }else {
105+ elog (LOG ,"%s" ,buf );
106+ }
107+ memmove (buf ,nl + 1 ,offs -= (nl + 1 - buf ));
108+ }
109+ }
110+ return NULL ;
111+ }
112+
81113int remote_execute (int argc ,char * argv [],bool listen )
82114{
83115char cmd [MAX_CMDLINE_LENGTH ];
@@ -89,6 +121,7 @@ int remote_execute(int argc, char* argv[], bool listen)
89121int infd [2 ];
90122int errfd [2 ];
91123char * pg_probackup = argv [0 ];
124+ pthread_t error_reader_thread ;
92125
93126ssh_argc = 0 ;
94127ssh_argv [ssh_argc ++ ]= instance_config .remote .proto ;
@@ -178,18 +211,13 @@ int remote_execute(int argc, char* argv[], bool listen)
178211SYS_CHECK (close (errfd [1 ]));
179212atexit (kill_child );
180213
214+ pthread_create (& error_reader_thread ,NULL ,error_reader_proc ,errfd );
215+
181216if (listen ) {
182217int status ;
183218fio_communicate (infd [0 ],outfd [1 ]);
219+
184220SYS_CHECK (wait (& status ));
185- if (status != 0 )
186- {
187- char buf [ERR_BUF_SIZE ];
188- int offs ,rc ;
189- for (offs = 0 ; (rc = read (errfd [0 ],& buf [offs ],sizeof (buf )- offs ))> 0 ;offs += rc );
190- buf [offs ]= '\0' ;
191- elog (ERROR ,"%s" ,strncmp (buf ,"ERROR: " ,6 )== 0 ?buf + 6 :buf );
192- }
193221return status ;
194222}else {
195223fio_redirect (infd [0 ],outfd [1 ]);/* write to stdout */