From a0de781e1bd3c2fb39e42e1bc05a5059723ca03c Mon Sep 17 00:00:00 2001 From: Dmitry Baryshkov Date: Sat, 23 Sep 2023 18:28:16 +0300 Subject: [PATCH] cdba-server: sanitise file descriptors before device_close() If we got to device_close(), the parent SSH might have been exited, making standard file descriptors unusable. Rebind them to /dev/null, so that any output during device_close (both from the cdba server and the helpers) doesn't cause SIGPIPE. Signed-off-by: Dmitry Baryshkov --- cdba-server.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/cdba-server.c b/cdba-server.c index 5bd7a25..df6db01 100644 --- a/cdba-server.c +++ b/cdba-server.c @@ -406,6 +406,17 @@ int main(int argc, char **argv) done: + /* if we got here, stdin/out/err might be not accessible anymore */ + ret = open("/dev/null", O_RDWR); + if (ret >= 0) { + close(STDIN_FILENO); + dup2(ret, STDIN_FILENO); + close(STDOUT_FILENO); + dup2(ret, STDOUT_FILENO); + close(STDERR_FILENO); + dup2(ret, STDERR_FILENO); + } + if (selected_device) device_close(selected_device);