Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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>

authored by

Wander Lairson Costa and committed by
Tomas Glozar
47dd74f6 4bf4ef52

+24 -7
+24 -7
tools/tracing/rtla/src/trace.c
··· 73 73 char buffer[4096]; 74 74 int out_fd, in_fd; 75 75 int retval = -1; 76 + ssize_t n_read; 77 + ssize_t n_written; 76 78 77 79 if (!inst || !filename) 78 80 return 0; ··· 92 90 goto out_close_in; 93 91 } 94 92 95 - do { 96 - retval = read(in_fd, buffer, sizeof(buffer)); 97 - if (retval <= 0) 93 + for (;;) { 94 + n_read = read(in_fd, buffer, sizeof(buffer)); 95 + if (n_read < 0) { 96 + if (errno == EINTR) 97 + continue; 98 + err_msg("Error reading trace file: %s\n", strerror(errno)); 98 99 goto out_close; 100 + } 101 + if (n_read == 0) 102 + break; 99 103 100 - retval = write(out_fd, buffer, retval); 101 - if (retval < 0) 102 - goto out_close; 103 - } while (retval > 0); 104 + n_written = 0; 105 + while (n_written < n_read) { 106 + const ssize_t w = write(out_fd, buffer + n_written, n_read - n_written); 107 + 108 + if (w < 0) { 109 + if (errno == EINTR) 110 + continue; 111 + err_msg("Error writing trace file: %s\n", strerror(errno)); 112 + goto out_close; 113 + } 114 + n_written += w; 115 + } 116 + } 104 117 105 118 retval = 0; 106 119 out_close: