Skip to content

Commit 47dd74f

Browse files
walaclenticularis39
authored andcommitted
rtla/trace: Fix I/O handling in save_trace_to_file()
The read/write loop in save_trace_to_file() does not correctly handle errors from the read() and write() system calls. If either call is interrupted by a signal, it returns -1 with errno set to EINTR, but the code treats this as a fatal error and aborts the save operation. Additionally, write() may perform a partial write, returning fewer bytes than requested, which the code does not handle. Fix the I/O loop by introducing proper error handling. The return value of read() is now stored in a ssize_t variable and checked for errors, with EINTR causing a retry. For write(), an inner loop ensures all bytes are written, handling both EINTR and partial writes. Error messages now include strerror() output for better debugging. This follows the same pattern established in the previous commit that fixed trace_event_save_hist(), ensuring consistent and robust I/O handling throughout the trace saving code. Signed-off-by: Wander Lairson Costa <wander@redhat.com> Link: https://lore.kernel.org/r/20260309195040.1019085-17-wander@redhat.com Signed-off-by: Tomas Glozar <tglozar@redhat.com>
1 parent 4bf4ef5 commit 47dd74f

1 file changed

Lines changed: 24 additions & 7 deletions

File tree

tools/tracing/rtla/src/trace.c

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ int save_trace_to_file(struct tracefs_instance *inst, const char *filename)
7373
char buffer[4096];
7474
int out_fd, in_fd;
7575
int retval = -1;
76+
ssize_t n_read;
77+
ssize_t n_written;
7678

7779
if (!inst || !filename)
7880
return 0;
@@ -90,15 +92,30 @@ int save_trace_to_file(struct tracefs_instance *inst, const char *filename)
9092
goto out_close_in;
9193
}
9294

93-
do {
94-
retval = read(in_fd, buffer, sizeof(buffer));
95-
if (retval <= 0)
95+
for (;;) {
96+
n_read = read(in_fd, buffer, sizeof(buffer));
97+
if (n_read < 0) {
98+
if (errno == EINTR)
99+
continue;
100+
err_msg("Error reading trace file: %s\n", strerror(errno));
96101
goto out_close;
102+
}
103+
if (n_read == 0)
104+
break;
97105

98-
retval = write(out_fd, buffer, retval);
99-
if (retval < 0)
100-
goto out_close;
101-
} while (retval > 0);
106+
n_written = 0;
107+
while (n_written < n_read) {
108+
const ssize_t w = write(out_fd, buffer + n_written, n_read - n_written);
109+
110+
if (w < 0) {
111+
if (errno == EINTR)
112+
continue;
113+
err_msg("Error writing trace file: %s\n", strerror(errno));
114+
goto out_close;
115+
}
116+
n_written += w;
117+
}
118+
}
102119

103120
retval = 0;
104121
out_close:

0 commit comments

Comments
 (0)