diff --git a/sys/vfs/vfs.c b/sys/vfs/vfs.c index 9eb4c1d309..807a95d0d8 100644 --- a/sys/vfs/vfs.c +++ b/sys/vfs/vfs.c @@ -21,6 +21,7 @@ #include /* for struct stat */ #include /* for struct statvfs */ #include /* for O_ACCMODE, ..., fcntl */ +#include /* for STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO */ #include "vfs.h" #include "mutex.h" @@ -867,6 +868,13 @@ static inline int _allocate_fd(int fd) { if (fd < 0) { for (fd = 0; fd < VFS_MAX_OPEN_FILES; ++fd) { + if ((fd == STDIN_FILENO) || (fd == STDOUT_FILENO) || (fd == STDERR_FILENO)) { + /* Do not auto-allocate the stdio file descriptor numbers to + * avoid conflicts between normal file system users and stdio + * drivers such as uart_stdio, rtt_stdio which need to be able + * to bind to these specific file descriptor numbers. */ + continue; + } if (_vfs_open_files[fd].pid == KERNEL_PID_UNDEF) { break; } diff --git a/tests/unittests/tests-vfs/tests-vfs-bind.c b/tests/unittests/tests-vfs/tests-vfs-bind.c index 8efa809537..7bd0b9783b 100644 --- a/tests/unittests/tests-vfs/tests-vfs-bind.c +++ b/tests/unittests/tests-vfs/tests-vfs-bind.c @@ -19,6 +19,7 @@ #include #include #include +#include #include "embUnit/embUnit.h" @@ -71,6 +72,9 @@ static void test_vfs_bind(void) uint8_t buf[_VFS_TEST_BIND_BUFSIZE]; fd = vfs_bind(VFS_ANY_FD, O_RDWR, &_test_bind_ops, &buf[0]); TEST_ASSERT(fd >= 0); + TEST_ASSERT(fd != STDIN_FILENO); + TEST_ASSERT(fd != STDOUT_FILENO); + TEST_ASSERT(fd != STDERR_FILENO); if (fd < 0) { return; }