Skip to content

Commit

Permalink
lib: posix: add stubs for thread-safe grp & pwd functions
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
ycsin committed Jun 17, 2024
1 parent b61b676 commit 2c545ce
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 4 deletions.
8 changes: 4 additions & 4 deletions doc/services/portability/posix/option_groups/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -902,10 +902,10 @@ _POSIX_THREAD_SAFE_FUNCTIONS
funlockfile(), yes
getc_unlocked(), yes
getchar_unlocked(), yes
getgrgid_r(),
getgrnam_r(),
getpwnam_r(),
getpwuid_r(),
getgrgid_r(),yes (will fail with ``ENOSYS``:ref:`†<posix_undefined_behaviour>`)
getgrnam_r(),yes (will fail with ``ENOSYS``:ref:`†<posix_undefined_behaviour>`)
getpwnam_r(),yes (will fail with ``ENOSYS``:ref:`†<posix_undefined_behaviour>`)
getpwuid_r(),yes (will fail with ``ENOSYS``:ref:`†<posix_undefined_behaviour>`)
gmtime_r(), yes
localtime_r(),
putc_unlocked(), yes
Expand Down
34 changes: 34 additions & 0 deletions include/zephyr/posix/grp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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/types.h>

typedef unsigned int gid_t;

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_ */
39 changes: 39 additions & 0 deletions include/zephyr/posix/pwd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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/types.h>

typedef unsigned int uid_t;
typedef unsigned int gid_t;

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_ */
2 changes: 2 additions & 0 deletions lib/posix/options/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ zephyr_library_sources_ifdef(CONFIG_POSIX_READER_WRITER_LOCKS rwlock.c)
zephyr_library_sources_ifdef(CONFIG_POSIX_SEMAPHORES semaphore.c)
zephyr_library_sources_ifdef(CONFIG_POSIX_THREADS
cond.c
grp.c
key.c
mutex.c
pthread.c
pwd.c
)
zephyr_library_sources_ifdef(CONFIG_XOPEN_STREAMS stropts.c)
zephyr_library_sources_ifdef(CONFIG_XSI_SYSTEM_LOGGING syslog.c)
Expand Down
37 changes: 37 additions & 0 deletions lib/posix/options/grp.c
Original file line number Diff line number Diff line change
@@ -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 changes: 37 additions & 0 deletions lib/posix/options/pwd.c
Original file line number Diff line number Diff line change
@@ -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 */

0 comments on commit 2c545ce

Please sign in to comment.