lib: posix: add stubs for thread-safe grp & pwd functions

Create stubs for getpwnam_r, getpwuid_r, getgrgid_r
& getgrnam_r.

These functions are in the _POSIX_THREAD_SAFE_FUNCTIONS
option group.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
This commit is contained in:
Yong Cong Sin 2024-06-14 17:18:22 +08:00 committed by Carles Cufí
commit 2ccfe8202d
8 changed files with 187 additions and 4 deletions

View file

@ -988,10 +988,10 @@ Enable this option with :kconfig:option:`CONFIG_POSIX_THREAD_SAFE_FUNCTIONS`.
funlockfile(),
getc_unlocked(),
getchar_unlocked(),
getgrgid_r(),
getgrnam_r(),
getpwnam_r(),
getpwuid_r(),
getgrgid_r(),yes :ref:`†<posix_undefined_behaviour>`
getgrnam_r(),yes :ref:`†<posix_undefined_behaviour>`
getpwnam_r(),yes :ref:`†<posix_undefined_behaviour>`
getpwuid_r(),yes :ref:`†<posix_undefined_behaviour>`
gmtime_r(), yes
localtime_r(),
putc_unlocked(),

View file

@ -0,0 +1,35 @@
/*
* Copyright (c) 2024 Meta Platforms
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_INCLUDE_POSIX_GRP_H_
#define ZEPHYR_INCLUDE_POSIX_GRP_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <zephyr/posix/sys/stat.h>
/**
* @brief Group structure
*/
struct group {
/**< the name of the group */
char *gr_name;
/**< numerical group ID */
gid_t gr_gid;
/**< pointer to a null-terminated array of character pointers to member names */
char **gr_mem;
};
int getgrnam_r(const char *name, struct group *grp, char *buffer, size_t bufsize,
struct group **result);
int getgrgid_r(gid_t gid, struct group *grp, char *buffer, size_t bufsize, struct group **result);
#ifdef __cplusplus
}
#endif
#endif /* ZEPHYR_INCLUDE_POSIX_GRP_H_ */

View file

@ -0,0 +1,36 @@
/*
* Copyright (c) 2024 Meta Platforms
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_INCLUDE_POSIX_PWD_H_
#define ZEPHYR_INCLUDE_POSIX_PWD_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <zephyr/posix/sys/stat.h>
struct passwd {
/* user's login name */
char *pw_name;
/* numerical user ID */
uid_t pw_uid;
/* numerical group ID */
gid_t pw_gid;
/* initial working directory */
char *pw_dir;
/* program to use as shell */
char *pw_shell;
};
int getpwnam_r(const char *nam, struct passwd *pwd, char *buffer, size_t bufsize,
struct passwd **result);
int getpwuid_r(uid_t uid, struct passwd *pwd, char *buffer, size_t bufsize, struct passwd **result);
#ifdef __cplusplus
}
#endif
#endif /* ZEPHYR_INCLUDE_POSIX_PWD_H_ */

View file

@ -142,9 +142,11 @@ if (NOT CONFIG_TC_PROVIDES_POSIX_THREADS)
# We have opted to use POSIX_THREADS here to match the Option name.
zephyr_library_sources_ifdef(CONFIG_POSIX_THREADS
cond.c
grp.c
key.c
mutex.c
pthread.c
pwd.c
)
endif()

37
lib/posix/options/grp.c Normal file
View file

@ -0,0 +1,37 @@
/*
* Copyright (c) 2024 Meta Platforms
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <errno.h>
#include <zephyr/sys/util.h>
#include <zephyr/posix/grp.h>
#ifdef CONFIG_POSIX_THREAD_SAFE_FUNCTIONS
int getgrnam_r(const char *name, struct group *grp, char *buffer, size_t bufsize,
struct group **result)
{
ARG_UNUSED(name);
ARG_UNUSED(grp);
ARG_UNUSED(buffer);
ARG_UNUSED(bufsize);
ARG_UNUSED(result);
return ENOSYS;
}
int getgrgid_r(gid_t gid, struct group *grp, char *buffer, size_t bufsize, struct group **result)
{
ARG_UNUSED(gid);
ARG_UNUSED(grp);
ARG_UNUSED(buffer);
ARG_UNUSED(bufsize);
ARG_UNUSED(result);
return ENOSYS;
}
#endif /* CONFIG_POSIX_THREAD_SAFE_FUNCTIONS */

37
lib/posix/options/pwd.c Normal file
View file

@ -0,0 +1,37 @@
/*
* Copyright (c) 2024 Meta Platforms
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <errno.h>
#include <zephyr/sys/util.h>
#include <zephyr/posix/pwd.h>
#ifdef CONFIG_POSIX_THREAD_SAFE_FUNCTIONS
int getpwnam_r(const char *nam, struct passwd *pwd, char *buffer, size_t bufsize,
struct passwd **result)
{
ARG_UNUSED(nam);
ARG_UNUSED(pwd);
ARG_UNUSED(buffer);
ARG_UNUSED(bufsize);
ARG_UNUSED(result);
return ENOSYS;
}
int getpwuid_r(uid_t uid, struct passwd *pwd, char *buffer, size_t bufsize, struct passwd **result)
{
ARG_UNUSED(uid);
ARG_UNUSED(pwd);
ARG_UNUSED(buffer);
ARG_UNUSED(bufsize);
ARG_UNUSED(result);
return ENOSYS;
}
#endif /* CONFIG_POSIX_THREAD_SAFE_FUNCTIONS */

View file

@ -0,0 +1,18 @@
/*
* Copyright (c) 2024 Meta Platforms
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <errno.h>
#include <grp.h>
#include <zephyr/ztest.h>
ZTEST(grp, test_grp_stubs)
{
zassert_equal(getgrnam_r(NULL, NULL, NULL, 42, NULL), ENOSYS);
zassert_equal(getgrgid_r(42, NULL, NULL, 42, NULL), ENOSYS);
}
ZTEST_SUITE(grp, NULL, NULL, NULL, NULL, NULL);

View file

@ -0,0 +1,18 @@
/*
* Copyright (c) 2024 Meta Platforms
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <errno.h>
#include <pwd.h>
#include <zephyr/ztest.h>
ZTEST(pwd, test_pwd_stubs)
{
zassert_equal(getpwnam_r(NULL, NULL, NULL, 42, NULL), ENOSYS);
zassert_equal(getpwuid_r(42, NULL, NULL, 42, NULL), ENOSYS);
}
ZTEST_SUITE(pwd, NULL, NULL, NULL, NULL, NULL);