Merge pull request #17065 from PX4/pr/fix-mavlink-shell

MAVLink shell: Ensure that nothing is written to the file handles during shell creation.
This commit is contained in:
Lorenz Meier 2021-03-15 09:47:58 +01:00 committed by GitHub
parent 2257c3767e
commit a5151f92ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 3 deletions

View File

@ -37,7 +37,7 @@ px4_add_board(
imu/invensense/icm20649
imu/invensense/icm20689
irlock
lights/blinkm
#lights/blinkm
lights/rgbled
lights/rgbled_ncp5623c
lights/rgbled_pwm

View File

@ -84,8 +84,6 @@ int MavlinkShell::start()
* keeps (duplicates) the first 3 fd's when creating a new task, all others are not inherited.
* This means we need to temporarily change the first 3 fd's of the current task (or at least
* the first 2 if stdout=stderr).
* And we hope :-) that during the temporary phase, no other thread from the same task writes to
* stdout (as it would end up in the pipe).
*/
if (pipe(p1) != 0) {
@ -105,6 +103,16 @@ int MavlinkShell::start()
_shell_fds[0] = p2[0];
_shell_fds[1] = p1[1];
/*
* Ensure that during the temporary phase no other thread from the same task writes to
* stdout (as it would end up in the pipe).
*/
#ifdef __PX4_NUTTX
sched_lock();
#endif /* __PX4_NUTTX */
fflush(stdout);
fflush(stderr);
int fd_backups[2]; //we don't touch stderr, we will redirect it to stdout in the startup of the shell task
for (int i = 0; i < 2; ++i) {
@ -140,6 +148,10 @@ int MavlinkShell::start()
close(fd_backups[i]);
}
#ifdef __PX4_NUTTX
sched_unlock();
#endif /* __PX4_NUTTX */
//close unused pipe fd's
close(_shell_fds[0]);
close(_shell_fds[1]);