diff --git a/main.c b/main.c index 3fb7a57..7da6466 100644 --- a/main.c +++ b/main.c @@ -62,6 +62,7 @@ #include #include #include +#include #endif #include @@ -715,6 +716,101 @@ void insert_marker_to_file(char* logfilename, char* marker_text){ } #ifndef _WIN32 +// Function to check if pipe reader is present and ready +// Returns: 1 if ready to write, 0 if should wait, -1 on error +int check_pipe_ready(int pipe_fd) { + if (pipe_fd < 0) { + return -1; + } + + struct pollfd pfd; + pfd.fd = pipe_fd; + pfd.events = POLLOUT; + pfd.revents = 0; + + // Check with 0 timeout (non-blocking check) + int ret = poll(&pfd, 1, 0); + + if (ret < 0) { + fprintf(stderr, "Error polling pipe: %s\n", strerror(errno)); + return -1; + } + + if (ret == 0) { + // Timeout - pipe buffer is full, reader is slow or absent + return 0; + } + + // Check for errors + if (pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) { + if (pfd.revents & POLLERR) { + fprintf(stderr, "Pipe error detected\n"); + } + if (pfd.revents & POLLHUP) { + fprintf(stderr, "Pipe hangup - reader disconnected\n"); + } + if (pfd.revents & POLLNVAL) { + fprintf(stderr, "Invalid pipe fd\n"); + } + return -1; + } + + // POLLOUT is set - ready to write + if (pfd.revents & POLLOUT) { + return 1; + } + + return 0; +} + +// Function to wait for pipe to become ready +// Returns: 1 if ready, 0 if interrupted, -1 on error +int wait_for_pipe_reader(int pipe_fd) { + if (pipe_fd < 0) { + return -1; + } + + printf("Waiting for pipe reader to become ready...\n"); + + struct pollfd pfd; + pfd.fd = pipe_fd; + pfd.events = POLLOUT; + + while (1) { + pfd.revents = 0; + + // Wait with 1 second timeout + int ret = poll(&pfd, 1, 1000); + + if (ret < 0) { + if (errno == EINTR) { + // Interrupted by signal, continue + continue; + } + fprintf(stderr, "Error waiting for pipe: %s\n", strerror(errno)); + return -1; + } + + if (ret == 0) { + // Timeout - print status and continue waiting + printf("Still waiting for pipe reader...\n"); + continue; + } + + // Check for errors + if (pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) { + fprintf(stderr, "Pipe error while waiting\n"); + return -1; + } + + // Ready to write + if (pfd.revents & POLLOUT) { + printf("Pipe reader is ready!\n"); + return 1; + } + } +} + // Function to initialize named pipe (FIFO) int init_pipe(const char* pipe_path) { // Remove existing pipe if it exists @@ -802,14 +898,35 @@ void receive_to_file(t_x502_hnd hnd, char* logfilename, uint32_t* inp_buff, uint #ifndef _WIN32 // Write data to pipe if it's open if (pipe_fd >= 0) { - // Write raw binary data to pipe + // Check if pipe is ready before writing + int pipe_status = check_pipe_ready(pipe_fd); + + if (pipe_status == 0) { + // Pipe not ready (buffer full or no reader) - wait for reader + printf("Pipe not ready - waiting for reader to catch up...\n"); + int wait_result = wait_for_pipe_reader(pipe_fd); + if (wait_result != 1) { + fprintf(stderr, "Warning: Could not write to pipe\n"); + return; + } + } else if (pipe_status < 0) { + fprintf(stderr, "Warning: Pipe error detected, skipping pipe write\n"); + return; + } + + // Pipe is ready, write data ssize_t bytes_written = write(pipe_fd, inp_buff, received_words * sizeof(uint32_t)); if (bytes_written < 0) { if (errno == EPIPE) { fprintf(stderr, "Warning: Broken pipe (reader disconnected)\n"); + } else if (errno == EAGAIN || errno == EWOULDBLOCK) { + fprintf(stderr, "Warning: Pipe write would block\n"); } else { fprintf(stderr, "Warning: Error writing to pipe: %s\n", strerror(errno)); } + } else if (bytes_written < (ssize_t)(received_words * sizeof(uint32_t))) { + printf("Warning: Partial write to pipe: %zd of %zu bytes\n", + bytes_written, received_words * sizeof(uint32_t)); } else { printf("Written %zd bytes to pipe\n", bytes_written); }