From ea454e9cee1cedb1fb7b8aed7fde46bd76bb031b Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Wed, 17 Nov 2021 18:30:54 +0100 Subject: [PATCH] Add interruptible function to read from pipe This will avoid to block Ctrl+c if the process the pipe is read from takes too much time. --- app/src/util/process_intr.c | 28 ++++++++++++++++++++++++++++ app/src/util/process_intr.h | 8 ++++++++ 2 files changed, 36 insertions(+) diff --git a/app/src/util/process_intr.c b/app/src/util/process_intr.c index ddf94e97..dcb81100 100644 --- a/app/src/util/process_intr.c +++ b/app/src/util/process_intr.c @@ -20,3 +20,31 @@ sc_process_check_success_intr(struct sc_intr *intr, sc_pid pid, return ret; } + +ssize_t +sc_pipe_read_intr(struct sc_intr *intr, sc_pid pid, sc_pipe pipe, char *data, + size_t len) { + if (!sc_intr_set_process(intr, pid)) { + // Already interrupted + return false; + } + + ssize_t ret = sc_pipe_read(pipe, data, len); + + sc_intr_set_process(intr, SC_PROCESS_NONE); + return ret; +} + +ssize_t +sc_pipe_read_all_intr(struct sc_intr *intr, sc_pid pid, sc_pipe pipe, + char *data, size_t len) { + if (!sc_intr_set_process(intr, pid)) { + // Already interrupted + return false; + } + + ssize_t ret = sc_pipe_read_all(pipe, data, len); + + sc_intr_set_process(intr, SC_PROCESS_NONE); + return ret; +} diff --git a/app/src/util/process_intr.h b/app/src/util/process_intr.h index a1574ce3..9406d889 100644 --- a/app/src/util/process_intr.h +++ b/app/src/util/process_intr.h @@ -10,4 +10,12 @@ bool sc_process_check_success_intr(struct sc_intr *intr, sc_pid pid, const char *name, bool close); +ssize_t +sc_pipe_read_intr(struct sc_intr *intr, sc_pid pid, sc_pipe pipe, char *data, + size_t len); + +ssize_t +sc_pipe_read_all_intr(struct sc_intr *intr, sc_pid pid, sc_pipe pipe, + char *data, size_t len); + #endif