Skip to content

Commit 2ccfe82

Browse files
ycsincarlescufi
authored andcommitted
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 <[email protected]>
1 parent b7ad4c5 commit 2ccfe82

File tree

8 files changed

+187
-4
lines changed

8 files changed

+187
-4
lines changed

doc/services/portability/posix/option_groups/index.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -988,10 +988,10 @@ Enable this option with :kconfig:option:`CONFIG_POSIX_THREAD_SAFE_FUNCTIONS`.
988988
funlockfile(),
989989
getc_unlocked(),
990990
getchar_unlocked(),
991-
getgrgid_r(),
992-
getgrnam_r(),
993-
getpwnam_r(),
994-
getpwuid_r(),
991+
getgrgid_r(),yes :ref:`†<posix_undefined_behaviour>`
992+
getgrnam_r(),yes :ref:`†<posix_undefined_behaviour>`
993+
getpwnam_r(),yes :ref:`†<posix_undefined_behaviour>`
994+
getpwuid_r(),yes :ref:`†<posix_undefined_behaviour>`
995995
gmtime_r(), yes
996996
localtime_r(),
997997
putc_unlocked(),

include/zephyr/posix/grp.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (c) 2024 Meta Platforms
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#ifndef ZEPHYR_INCLUDE_POSIX_GRP_H_
7+
#define ZEPHYR_INCLUDE_POSIX_GRP_H_
8+
9+
#ifdef __cplusplus
10+
extern "C" {
11+
#endif
12+
13+
#include <zephyr/posix/sys/stat.h>
14+
15+
/**
16+
* @brief Group structure
17+
*/
18+
struct group {
19+
/**< the name of the group */
20+
char *gr_name;
21+
/**< numerical group ID */
22+
gid_t gr_gid;
23+
/**< pointer to a null-terminated array of character pointers to member names */
24+
char **gr_mem;
25+
};
26+
27+
int getgrnam_r(const char *name, struct group *grp, char *buffer, size_t bufsize,
28+
struct group **result);
29+
int getgrgid_r(gid_t gid, struct group *grp, char *buffer, size_t bufsize, struct group **result);
30+
31+
#ifdef __cplusplus
32+
}
33+
#endif
34+
35+
#endif /* ZEPHYR_INCLUDE_POSIX_GRP_H_ */

include/zephyr/posix/pwd.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2024 Meta Platforms
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#ifndef ZEPHYR_INCLUDE_POSIX_PWD_H_
7+
#define ZEPHYR_INCLUDE_POSIX_PWD_H_
8+
9+
#ifdef __cplusplus
10+
extern "C" {
11+
#endif
12+
13+
#include <zephyr/posix/sys/stat.h>
14+
15+
struct passwd {
16+
/* user's login name */
17+
char *pw_name;
18+
/* numerical user ID */
19+
uid_t pw_uid;
20+
/* numerical group ID */
21+
gid_t pw_gid;
22+
/* initial working directory */
23+
char *pw_dir;
24+
/* program to use as shell */
25+
char *pw_shell;
26+
};
27+
28+
int getpwnam_r(const char *nam, struct passwd *pwd, char *buffer, size_t bufsize,
29+
struct passwd **result);
30+
int getpwuid_r(uid_t uid, struct passwd *pwd, char *buffer, size_t bufsize, struct passwd **result);
31+
32+
#ifdef __cplusplus
33+
}
34+
#endif
35+
36+
#endif /* ZEPHYR_INCLUDE_POSIX_PWD_H_ */

lib/posix/options/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,11 @@ if (NOT CONFIG_TC_PROVIDES_POSIX_THREADS)
142142
# We have opted to use POSIX_THREADS here to match the Option name.
143143
zephyr_library_sources_ifdef(CONFIG_POSIX_THREADS
144144
cond.c
145+
grp.c
145146
key.c
146147
mutex.c
147148
pthread.c
149+
pwd.c
148150
)
149151
endif()
150152

lib/posix/options/grp.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2024 Meta Platforms
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <errno.h>
8+
9+
#include <zephyr/sys/util.h>
10+
#include <zephyr/posix/grp.h>
11+
12+
#ifdef CONFIG_POSIX_THREAD_SAFE_FUNCTIONS
13+
14+
int getgrnam_r(const char *name, struct group *grp, char *buffer, size_t bufsize,
15+
struct group **result)
16+
{
17+
ARG_UNUSED(name);
18+
ARG_UNUSED(grp);
19+
ARG_UNUSED(buffer);
20+
ARG_UNUSED(bufsize);
21+
ARG_UNUSED(result);
22+
23+
return ENOSYS;
24+
}
25+
26+
int getgrgid_r(gid_t gid, struct group *grp, char *buffer, size_t bufsize, struct group **result)
27+
{
28+
ARG_UNUSED(gid);
29+
ARG_UNUSED(grp);
30+
ARG_UNUSED(buffer);
31+
ARG_UNUSED(bufsize);
32+
ARG_UNUSED(result);
33+
34+
return ENOSYS;
35+
}
36+
37+
#endif /* CONFIG_POSIX_THREAD_SAFE_FUNCTIONS */

lib/posix/options/pwd.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2024 Meta Platforms
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <errno.h>
8+
9+
#include <zephyr/sys/util.h>
10+
#include <zephyr/posix/pwd.h>
11+
12+
#ifdef CONFIG_POSIX_THREAD_SAFE_FUNCTIONS
13+
14+
int getpwnam_r(const char *nam, struct passwd *pwd, char *buffer, size_t bufsize,
15+
struct passwd **result)
16+
{
17+
ARG_UNUSED(nam);
18+
ARG_UNUSED(pwd);
19+
ARG_UNUSED(buffer);
20+
ARG_UNUSED(bufsize);
21+
ARG_UNUSED(result);
22+
23+
return ENOSYS;
24+
}
25+
26+
int getpwuid_r(uid_t uid, struct passwd *pwd, char *buffer, size_t bufsize, struct passwd **result)
27+
{
28+
ARG_UNUSED(uid);
29+
ARG_UNUSED(pwd);
30+
ARG_UNUSED(buffer);
31+
ARG_UNUSED(bufsize);
32+
ARG_UNUSED(result);
33+
34+
return ENOSYS;
35+
}
36+
37+
#endif /* CONFIG_POSIX_THREAD_SAFE_FUNCTIONS */

tests/posix/common/src/grp.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright (c) 2024 Meta Platforms
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <errno.h>
8+
#include <grp.h>
9+
10+
#include <zephyr/ztest.h>
11+
12+
ZTEST(grp, test_grp_stubs)
13+
{
14+
zassert_equal(getgrnam_r(NULL, NULL, NULL, 42, NULL), ENOSYS);
15+
zassert_equal(getgrgid_r(42, NULL, NULL, 42, NULL), ENOSYS);
16+
}
17+
18+
ZTEST_SUITE(grp, NULL, NULL, NULL, NULL, NULL);

tests/posix/common/src/pwd.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright (c) 2024 Meta Platforms
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <errno.h>
8+
#include <pwd.h>
9+
10+
#include <zephyr/ztest.h>
11+
12+
ZTEST(pwd, test_pwd_stubs)
13+
{
14+
zassert_equal(getpwnam_r(NULL, NULL, NULL, 42, NULL), ENOSYS);
15+
zassert_equal(getpwuid_r(42, NULL, NULL, 42, NULL), ENOSYS);
16+
}
17+
18+
ZTEST_SUITE(pwd, NULL, NULL, NULL, NULL, NULL);

0 commit comments

Comments
 (0)