4
4
#include <sys/types.h>
5
5
#include <sys/wait.h>
6
6
#include <signal.h>
7
+ #include <pthread.h>
7
8
8
9
#include "pg_probackup.h"
9
10
#include "file.h"
10
11
11
12
#define MAX_CMDLINE_LENGTH 4096
12
13
#define MAX_CMDLINE_OPTIONS 256
13
- #define ERR_BUF_SIZE 1024
14
+ #define ERR_BUF_SIZE 4096
14
15
15
16
static int append_option (char * buf ,size_t buf_size ,size_t dst ,char const * src )
16
17
{
@@ -78,6 +79,37 @@ static void kill_child(void)
78
79
kill (child_pid ,SIGTERM );
79
80
}
80
81
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
+
81
113
int remote_execute (int argc ,char * argv [],bool listen )
82
114
{
83
115
char cmd [MAX_CMDLINE_LENGTH ];
@@ -89,6 +121,7 @@ int remote_execute(int argc, char* argv[], bool listen)
89
121
int infd [2 ];
90
122
int errfd [2 ];
91
123
char * pg_probackup = argv [0 ];
124
+ pthread_t error_reader_thread ;
92
125
93
126
ssh_argc = 0 ;
94
127
ssh_argv [ssh_argc ++ ]= instance_config .remote .proto ;
@@ -178,18 +211,13 @@ int remote_execute(int argc, char* argv[], bool listen)
178
211
SYS_CHECK (close (errfd [1 ]));
179
212
atexit (kill_child );
180
213
214
+ pthread_create (& error_reader_thread ,NULL ,error_reader_proc ,errfd );
215
+
181
216
if (listen ) {
182
217
int status ;
183
218
fio_communicate (infd [0 ],outfd [1 ]);
219
+
184
220
SYS_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
- }
193
221
return status ;
194
222
}else {
195
223
fio_redirect (infd [0 ],outfd [1 ]);/* write to stdout */