mavlink: fix bug when opening /dev/null as default stdin/stdout/stderr

Prior commit added opening of /dev/null as 0, 1 and/or 2 file
descriptors, if they where not present. However, if the temporary
file descriptor used to open /dev/null matched the target file
descriptor, it would be immediately closed again. This commit fixes that,
and does not duplicate and close the temporary file descriptor if it is
already at the correct number.
This commit is contained in:
Knut Hjorth 2023-02-09 08:09:52 +01:00 committed by Beat Küng
parent 5880fe4153
commit db539d15bd

View File

@ -1864,20 +1864,30 @@ Mavlink::task_main(int argc, char *argv[])
// 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 (tmp != 0) {
dup2(tmp, 0);
close(tmp);
}
}
if (fcntl(1, F_GETFD) == -1) {
int tmp = open("/dev/null", O_WRONLY);
dup2(tmp, 1);
close(tmp);
if (tmp != 1) {
dup2(tmp, 1);
close(tmp);
}
}
if (fcntl(2, F_GETFD) == -1) {
int tmp = open("/dev/null", O_WRONLY);
dup2(tmp, 2);
close(tmp);
if (tmp != 2) {
dup2(tmp, 2);
close(tmp);
}
}
int ch;