posix: fdtable: ensure stdin, stdout, and stderr are initialized

Ensure that stdin, stdout, and stderr are initialized statically.

Previously, the mutex and condition variable were uninitialized.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
This commit is contained in:
Christopher Friedt 2023-10-12 20:59:31 -04:00 committed by Chris Friedt
commit 12ea06cac2

View file

@ -14,6 +14,8 @@
*/
#include <errno.h>
#include <string.h>
#include <zephyr/posix/fcntl.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/fdtable.h>
@ -41,17 +43,23 @@ static struct fd_entry fdtable[CONFIG_POSIX_MAX_FDS] = {
{
/* STDIN */
.vtable = &stdinout_fd_op_vtable,
.refcount = ATOMIC_INIT(1)
.refcount = ATOMIC_INIT(1),
.lock = Z_MUTEX_INITIALIZER(fdtable[0].lock),
.cond = Z_CONDVAR_INITIALIZER(fdtable[0].cond),
},
{
/* STDOUT */
.vtable = &stdinout_fd_op_vtable,
.refcount = ATOMIC_INIT(1)
.refcount = ATOMIC_INIT(1),
.lock = Z_MUTEX_INITIALIZER(fdtable[1].lock),
.cond = Z_CONDVAR_INITIALIZER(fdtable[1].cond),
},
{
/* STDERR */
.vtable = &stdinout_fd_op_vtable,
.refcount = ATOMIC_INIT(1)
.refcount = ATOMIC_INIT(1),
.lock = Z_MUTEX_INITIALIZER(fdtable[2].lock),
.cond = Z_CONDVAR_INITIALIZER(fdtable[2].cond),
},
#else
{
@ -124,6 +132,30 @@ static int _check_fd(int fd)
return 0;
}
#ifdef CONFIG_ZTEST
bool fdtable_fd_is_initialized(int fd)
{
struct k_mutex ref_lock;
struct k_condvar ref_cond;
if (fd < 0 || fd >= ARRAY_SIZE(fdtable)) {
return false;
}
ref_lock = (struct k_mutex)Z_MUTEX_INITIALIZER(fdtable[fd].lock);
if (memcmp(&ref_lock, &fdtable[fd].lock, sizeof(ref_lock)) != 0) {
return false;
}
ref_cond = (struct k_condvar)Z_CONDVAR_INITIALIZER(fdtable[fd].cond);
if (memcmp(&ref_cond, &fdtable[fd].cond, sizeof(ref_cond)) != 0) {
return false;
}
return true;
}
#endif /* CONFIG_ZTEST */
void *z_get_fd_obj(int fd, const struct fd_op_vtable *vtable, int err)
{
struct fd_entry *entry;