From 99cf1cfdfe81d49c794ca6d827cd095387441000 Mon Sep 17 00:00:00 2001 From: Knut Hjorth Date: Mon, 6 Feb 2023 17:08:58 +0100 Subject: [PATCH] mavlink: use /dev/null as default stdin, stdout and stderr If 0, 1 and/or 2 file descriptors are not open when mavlink module starts (as might be the case for USB auto-start), use default /dev/null so that these numbers are not used by other other files. --- src/modules/mavlink/mavlink_main.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/modules/mavlink/mavlink_main.cpp b/src/modules/mavlink/mavlink_main.cpp index bfff5180a1..5b31482d39 100644 --- a/src/modules/mavlink/mavlink_main.cpp +++ b/src/modules/mavlink/mavlink_main.cpp @@ -1859,6 +1859,27 @@ Mavlink::configure_streams_to_default(const char *configure_single_stream) int Mavlink::task_main(int argc, char *argv[]) { + // If stdin, stdout and/or stderr file descriptors (0, 1, 2) + // are not open when mavlink module starts (as might be the case for USB auto-start), + // use default /dev/null so that these numbers are not used by other other files. + if (fcntl(0, F_GETFD) == -1) { + int tmp = open("/dev/null", O_RDONLY); + dup2(tmp, 0); + close(tmp); + } + + if (fcntl(1, F_GETFD) == -1) { + int tmp = open("/dev/null", O_WRONLY); + dup2(tmp, 1); + close(tmp); + } + + if (fcntl(2, F_GETFD) == -1) { + int tmp = open("/dev/null", O_WRONLY); + dup2(tmp, 2); + close(tmp); + } + int ch; _baudrate = 57600; _datarate = 0;