Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6a721aa

Browse files
knizhnikgsmolk
authored andcommitted
Grab abnd log agent messages to stderr
1 parent 736fc41 commit 6a721aa

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

src/utils/file.c

+20-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ static __thread unsigned long fio_fdset = 0;
2020
static __thread void* fio_stdin_buffer;
2121
static __thread int fio_stdout = 0;
2222
static __thread int fio_stdin = 0;
23+
static __thread int fio_stderr = 0;
2324

2425
fio_location MyLocation;
2526

@@ -38,10 +39,28 @@ typedef struct
3839
#define fio_fileno(f) (((size_t)f - 1) | FIO_PIPE_MARKER)
3940

4041
/* Use specified file descriptors as stdin/stdout for FIO functions */
41-
void fio_redirect(int in, int out)
42+
void fio_redirect(int in, int out, int err)
4243
{
4344
fio_stdin = in;
4445
fio_stdout = out;
46+
fio_stderr = err;
47+
}
48+
49+
void fio_error(int rc, int size, char const* file, int line)
50+
{
51+
if (remote_agent)
52+
{
53+
fprintf(stderr, "%s:%d: proceeds %d bytes instead of %d: %s\n", file, line, rc, size, rc >= 0 ? "end of data" : strerror(errno));
54+
exit(EXIT_FAILURE);
55+
}
56+
else
57+
{
58+
char buf[PRINTF_BUF_SIZE];
59+
int err_size = read(fio_stderr, buf, sizeof(buf));
60+
if (err_size > 0)
61+
elog(LOG, "Agent error: %s", buf);
62+
elog(ERROR, "Communication error: %s", rc >= 0 ? "end of data" : strerror(errno));
63+
}
4564
}
4665

4766
/* Check if file descriptor is local or remote (created by FIO) */

src/utils/file.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ typedef enum
4949
#define PAGE_CHECKSUM_MISMATCH (-256)
5050

5151
#define SYS_CHECK(cmd) do if ((cmd) < 0) { fprintf(stderr, "%s:%d: (%s) %s\n", __FILE__, __LINE__, #cmd, strerror(errno)); exit(EXIT_FAILURE); } while (0)
52-
#define IO_CHECK(cmd, size) do { int _rc = (cmd); if (_rc != (size)) { if (remote_agent) { fprintf(stderr, "%s:%d: proceeds %d bytes instead of %d: %s\n", __FILE__, __LINE__, _rc, (int)(size), _rc >= 0 ? "end of data" : strerror(errno)); exit(EXIT_FAILURE); } else elog(ERROR, "Communication error: %s", _rc >= 0 ? "end of data" : strerror(errno)); } } while (0)
52+
#define IO_CHECK(cmd, size) do { int _rc = (cmd); if (_rc != (size)) fio_error(_rc, size, __FILE__, __LINE__); } while (0)
5353

5454
typedef struct
5555
{
@@ -64,7 +64,7 @@ extern fio_location MyLocation;
6464
/* Check if FILE handle is local or remote (created by FIO) */
6565
#define fio_is_remote_file(file) ((size_t)(file) <= FIO_FDMAX)
6666

67-
extern void fio_redirect(int in, int out);
67+
extern void fio_redirect(int in, int out, int err);
6868
extern void fio_communicate(int in, int out);
6969

7070
extern FILE* fio_fopen(char const* name, char const* mode, fio_location location);
@@ -77,6 +77,7 @@ extern int fio_fseek(FILE* f, off_t offs);
7777
extern int fio_ftruncate(FILE* f, off_t size);
7878
extern int fio_fclose(FILE* f);
7979
extern int fio_ffstat(FILE* f, struct stat* st);
80+
extern void fio_error(int rc, int size, char const* file, int line);
8081

8182
struct pgFile;
8283
extern int fio_send_pages(FILE* in, FILE* out, struct pgFile *file, XLogRecPtr horizonLsn,

src/utils/remote.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ bool launch_agent(void)
110110
int ssh_argc;
111111
int outfd[2];
112112
int infd[2];
113+
int errfd[2];
113114

114115
ssh_argc = 0;
115116
#ifdef WIN32
@@ -195,20 +196,25 @@ bool launch_agent(void)
195196
#else
196197
SYS_CHECK(pipe(infd));
197198
SYS_CHECK(pipe(outfd));
199+
SYS_CHECK(pipe(errfd));
198200

199201
SYS_CHECK(child_pid = fork());
200202

201203
if (child_pid == 0) { /* child */
202204
SYS_CHECK(close(STDIN_FILENO));
203205
SYS_CHECK(close(STDOUT_FILENO));
206+
SYS_CHECK(close(STDERR_FILENO));
204207

205208
SYS_CHECK(dup2(outfd[0], STDIN_FILENO));
206209
SYS_CHECK(dup2(infd[1], STDOUT_FILENO));
210+
SYS_CHECK(dup2(errfd[1], STDERR_FILENO));
207211

208212
SYS_CHECK(close(infd[0]));
209213
SYS_CHECK(close(infd[1]));
210214
SYS_CHECK(close(outfd[0]));
211215
SYS_CHECK(close(outfd[1]));
216+
SYS_CHECK(close(errfd[0]));
217+
SYS_CHECK(close(errfd[1]));
212218

213219
if (execvp(ssh_argv[0], ssh_argv) < 0)
214220
return false;
@@ -217,9 +223,10 @@ bool launch_agent(void)
217223
elog(LOG, "Spawn agent %d version %s", child_pid, PROGRAM_VERSION);
218224
SYS_CHECK(close(infd[1])); /* These are being used by the child */
219225
SYS_CHECK(close(outfd[0]));
226+
SYS_CHECK(close(errfd[1]));
220227
/*atexit(kill_child);*/
221228

222-
fio_redirect(infd[0], outfd[1]); /* write to stdout */
229+
fio_redirect(infd[0], outfd[1], errfd[0]); /* write to stdout */
223230
}
224231
return true;
225232
}

0 commit comments

Comments
 (0)