Skip to content

Commit 07fa00a

Browse files
committed
posix: options: implement POSIX_SYSTEM_DATABASE Option Group
Provide an implementation for the POSIX_SYSTEM_DATABASE Option Group. Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
1 parent e06af5b commit 07fa00a

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

lib/posix/options/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ if (NOT CONFIG_TC_PROVIDES_POSIX_SPIN_LOCKS)
121121
zephyr_library_sources_ifdef(CONFIG_POSIX_SPIN_LOCKS spinlock.c)
122122
endif()
123123

124+
if (NOT CONFIG_TC_PROVIDES_POSIX_SYSTEM_DATABASE)
125+
zephyr_library_sources_ifdef(CONFIG_POSIX_SYSTEM_DATABASE
126+
system_database_priv.c
127+
system_database.c
128+
)
129+
endif()
130+
124131
if (NOT CONFIG_TC_PROVIDES_POSIX_SYSTEM_DATABASE_R)
125132
zephyr_library_sources_ifdef(CONFIG_POSIX_SYSTEM_DATABASE_R
126133
system_database_priv.c

lib/posix/options/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ rsource "Kconfig.semaphore"
2828
rsource "Kconfig.signal"
2929
rsource "Kconfig.spinlock"
3030
rsource "Kconfig.sync_io"
31+
rsource "Kconfig.system_database"
3132
rsource "Kconfig.system_database_r"
3233
rsource "Kconfig.timer"
3334
rsource "Kconfig.xsi"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright (c) 2024 Tenstorrent AI ULC
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
config POSIX_SYSTEM_DATABASE
6+
bool "POSIX System Database"
7+
select POSIX_SYSTEM_DATABASE_R
8+
help
9+
Select 'y' here, and the system will support getgrgid(), getgrnam(), getpwnam(), and
10+
getpwuid().
11+
12+
For more information, please see
13+
https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_subprofiles.html

lib/posix/options/system_database.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright (c) 2024 Tenstorrent AI ULC
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "system_database_priv.h"
8+
9+
#include <errno.h>
10+
#include <stddef.h>
11+
#include <string.h>
12+
13+
#include <zephyr/posix/grp.h>
14+
#include <zephyr/posix/pwd.h>
15+
#include <zephyr/sys/util.h>
16+
17+
static char gr_line_buf[CONFIG_POSIX_GETGR_R_SIZE_MAX];
18+
static char pw_line_buf[CONFIG_POSIX_GETPW_R_SIZE_MAX];
19+
20+
static struct group gr;
21+
static struct passwd pw;
22+
23+
struct group *getgrgid(gid_t gid)
24+
{
25+
int ret;
26+
struct group *result;
27+
28+
ret = __getgr_r(NULL, gid, &gr, gr_line_buf, sizeof(gr_line_buf), &result);
29+
if (ret != 0) {
30+
errno = ret;
31+
return NULL;
32+
}
33+
34+
return result;
35+
}
36+
37+
struct group *getgrnam(const char *name)
38+
{
39+
int ret;
40+
struct group *result;
41+
42+
if (name == NULL) {
43+
return NULL;
44+
}
45+
46+
ret = __getgr_r(name, -1, &gr, gr_line_buf, sizeof(gr_line_buf), &result);
47+
if (ret != 0) {
48+
errno = ret;
49+
return NULL;
50+
}
51+
52+
return result;
53+
}
54+
55+
struct passwd *getpwnam(const char *name)
56+
{
57+
int ret;
58+
struct passwd *result;
59+
60+
if (name == NULL) {
61+
return NULL;
62+
}
63+
64+
ret = __getpw_r(name, -1, &pw, pw_line_buf, sizeof(pw_line_buf), &result);
65+
if (ret != 0) {
66+
errno = ret;
67+
return NULL;
68+
}
69+
70+
return result;
71+
}
72+
73+
struct passwd *getpwuid(uid_t uid)
74+
{
75+
int ret;
76+
struct passwd *result;
77+
78+
ret = __getpw_r(NULL, uid, &pw, pw_line_buf, sizeof(pw_line_buf), &result);
79+
if (ret != 0) {
80+
errno = ret;
81+
return NULL;
82+
}
83+
84+
return result;
85+
}

0 commit comments

Comments
 (0)