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.
This commit is contained in:
Knut Hjorth
2023-02-06 17:08:58 +01:00
committed by Beat Küng
parent 6c7ae3d845
commit 99cf1cfdfe
+21
View File
@@ -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;