Skip to content

posix: unistd: support for confstr() #70274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions doc/services/portability/posix/aep/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Minimal Realtime System Profile (PSE51)
POSIX_DEVICE_IO,, :ref:`†<posix_undefined_behaviour>`
POSIX_FILE_LOCKING,,
POSIX_SIGNALS,, :ref:`†<posix_undefined_behaviour>`
POSIX_SINGLE_PROCESS,, :ref:`†<posix_undefined_behaviour>`
POSIX_SINGLE_PROCESS, yes,
POSIX_THREADS_BASE, yes, :ref:`†<posix_undefined_behaviour>`
XSI_THREADS_EXT, yes, :ref:`†<posix_undefined_behaviour>`

Expand Down Expand Up @@ -82,7 +82,7 @@ Realtime Controller System Profile (PSE52)
POSIX_FILE_LOCKING,,
POSIX_FILE_SYSTEM,,
POSIX_SIGNALS,, :ref:`†<posix_undefined_behaviour>`
POSIX_SINGLE_PROCESS,, :ref:`†<posix_undefined_behaviour>`
POSIX_SINGLE_PROCESS, yes,
POSIX_THREADS_BASE, yes, :ref:`†<posix_undefined_behaviour>`
XSI_THREADS_EXT, yes, :ref:`†<posix_undefined_behaviour>`

Expand Down Expand Up @@ -143,7 +143,7 @@ Dedicated Realtime System Profile (PSE53)
POSIX_PIPE,, :ref:`†<posix_undefined_behaviour>`
POSIX_SIGNALS,, :ref:`†<posix_undefined_behaviour>`
POSIX_SIGNAL_JUMP,, :ref:`†<posix_undefined_behaviour>`
POSIX_SINGLE_PROCESS,, :ref:`†<posix_undefined_behaviour>`
POSIX_SINGLE_PROCESS, yes,
POSIX_THREADS_BASE, yes, :ref:`†<posix_undefined_behaviour>`
XSI_THREADS_EXT, yes, :ref:`†<posix_undefined_behaviour>`

Expand Down
2 changes: 1 addition & 1 deletion doc/services/portability/posix/option_groups/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ process applications.
:header: API, Supported
:widths: 50,10

confstr(),
confstr(),yes
environ,yes
errno,yes
getenv(),yes
Expand Down
51 changes: 51 additions & 0 deletions include/zephyr/posix/sys/confstr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2024, Meta
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_INCLUDE_POSIX_SYS_CONFSTR_H_
#define ZEPHYR_INCLUDE_POSIX_SYS_CONFSTR_H_

#ifdef __cplusplus
extern "C" {
#endif

enum {
_CS_PATH,
_CS_POSIX_V7_ILP32_OFF32_CFLAGS,
_CS_POSIX_V7_ILP32_OFF32_LDFLAGS,
_CS_POSIX_V7_ILP32_OFF32_LIBS,
_CS_POSIX_V7_ILP32_OFFBIG_CFLAGS,
_CS_POSIX_V7_ILP32_OFFBIG_LDFLAGS,
_CS_POSIX_V7_ILP32_OFFBIG_LIBS,
_CS_POSIX_V7_LP64_OFF64_CFLAGS,
_CS_POSIX_V7_LP64_OFF64_LDFLAGS,
_CS_POSIX_V7_LP64_OFF64_LIBS,
_CS_POSIX_V7_LPBIG_OFFBIG_CFLAGS,
_CS_POSIX_V7_LPBIG_OFFBIG_LDFLAGS,
_CS_POSIX_V7_LPBIG_OFFBIG_LIBS,
_CS_POSIX_V7_THREADS_CFLAGS,
_CS_POSIX_V7_THREADS_LDFLAGS,
_CS_POSIX_V7_WIDTH_RESTRICTED_ENVS,
_CS_V7_ENV,
_CS_POSIX_V6_ILP32_OFF32_CFLAGS,
_CS_POSIX_V6_ILP32_OFF32_LDFLAGS,
_CS_POSIX_V6_ILP32_OFF32_LIBS,
_CS_POSIX_V6_ILP32_OFFBIG_CFLAGS,
_CS_POSIX_V6_ILP32_OFFBIG_LDFLAGS,
_CS_POSIX_V6_ILP32_OFFBIG_LIBS,
_CS_POSIX_V6_LP64_OFF64_CFLAGS,
_CS_POSIX_V6_LP64_OFF64_LDFLAGS,
_CS_POSIX_V6_LP64_OFF64_LIBS,
_CS_POSIX_V6_LPBIG_OFFBIG_CFLAGS,
_CS_POSIX_V6_LPBIG_OFFBIG_LDFLAGS,
_CS_POSIX_V6_LPBIG_OFFBIG_LIBS,
_CS_POSIX_V6_WIDTH_RESTRICTED_ENVS,
_CS_V6_ENV,
};

#ifdef __cplusplus
}
#endif

#endif /* ZEPHYR_INCLUDE_POSIX_SYS_CONFSTR_H_ */
4 changes: 4 additions & 0 deletions include/zephyr/posix/unistd.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#ifdef CONFIG_POSIX_SYSCONF
#include <zephyr/posix/signal.h>
#endif
#include <zephyr/posix/sys/confstr.h>
#include <zephyr/posix/sys/stat.h>
#include <zephyr/posix/sys/sysconf.h>

Expand Down Expand Up @@ -266,6 +267,9 @@ int usleep(useconds_t useconds);
#ifdef CONFIG_POSIX_SYSCONF_IMPL_FULL
long sysconf(int opt);
#endif
#if _POSIX_C_SOURCE >= 2
size_t confstr(int name, char *buf, size_t len);
#endif

#ifdef __cplusplus
}
Expand Down
1 change: 1 addition & 0 deletions lib/posix/options/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ zephyr_library_sources_ifdef(CONFIG_POSIX_API perror.c)
zephyr_library_sources_ifdef(CONFIG_POSIX_CLOCK clock.c)
zephyr_library_sources_ifdef(CONFIG_POSIX_CLOCK nanosleep.c)
zephyr_library_sources_ifdef(CONFIG_POSIX_CLOCK sleep.c)
zephyr_library_sources_ifdef(CONFIG_POSIX_CONFSTR confstr.c)
zephyr_library_sources_ifdef(CONFIG_POSIX_ENV env.c)
zephyr_library_sources_ifdef(CONFIG_POSIX_FS fs.c)
zephyr_library_sources_ifdef(CONFIG_POSIX_MQUEUE mqueue.c)
Expand Down
1 change: 1 addition & 0 deletions lib/posix/options/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ endif # POSIX_CLOCK
rsource "Kconfig.barrier"
rsource "Kconfig.clock"
rsource "Kconfig.cond"
rsource "Kconfig.confstr"
rsource "Kconfig.env"
rsource "Kconfig.eventfd"
rsource "Kconfig.fdtable"
Expand Down
9 changes: 9 additions & 0 deletions lib/posix/options/Kconfig.confstr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) 2024, Meta
#
# SPDX-License-Identifier: Apache-2.0

config POSIX_CONFSTR
bool "Retrieve string system configuration"
default y if POSIX_API
help
This enables the POSIX confstr() function.
21 changes: 21 additions & 0 deletions lib/posix/options/confstr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2024, Meta
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/posix/unistd.h>

size_t confstr(int name, char *buf, size_t len)
{
if (name < 0 || name > _CS_V6_ENV) {
errno = EINVAL;
return 0;
}

if (buf != NULL && len > 0) {
buf[0] = '\0';
}

return 1;
}
2 changes: 2 additions & 0 deletions tests/posix/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ FILE(GLOB app_sources src/*.c)
zephyr_include_directories(${ZEPHYR_BASE}/lib/posix)

target_sources(app PRIVATE ${app_sources})

target_compile_options(app PRIVATE -U_POSIX_C_SOURCE -D_POSIX_C_SOURCE=200809L)
67 changes: 67 additions & 0 deletions tests/posix/common/src/confstr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2024, Meta
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <errno.h>
#include <unistd.h>

#include <zephyr/ztest.h>
#include <zephyr/sys/util.h>

ZTEST(confstr, test_confstr)
{
char buf[1];

/* degenerate cases */
{
struct arg {
int name;
char *buf;
size_t len;
};

const struct arg arg1s[] = {
{-1, NULL, 0},
{-1, NULL, sizeof(buf)},
{-1, buf, 0},
{-1, buf, sizeof(buf)},
};

const struct arg arg2s[] = {
{_CS_PATH, NULL, 0},
{_CS_PATH, buf, 0},
};

const struct arg arg3s[] = {
{_CS_PATH, NULL, sizeof(buf)},
};

ARRAY_FOR_EACH_PTR(arg1s, arg) {
errno = 0;
zassert_equal(0, confstr(arg->name, arg->buf, arg->len));
zassert_equal(errno, EINVAL);
}

ARRAY_FOR_EACH_PTR(arg2s, arg) {
errno = 0;
buf[0] = 0xff;
zassert_true(confstr(arg->name, arg->buf, arg->len) > 0);
zassert_equal(errno, 0);
zassert_equal((uint8_t)buf[0], 0xff);
}

ARRAY_FOR_EACH_PTR(arg3s, arg) {
errno = 0;
zassert_true(confstr(arg->name, arg->buf, arg->len) > 0);
zassert_equal(errno, 0);
}
}

buf[0] = 0xff;
zassert_true(confstr(_CS_PATH, buf, sizeof(buf) > 0));
zassert_equal(buf[0], '\0');
}

ZTEST_SUITE(confstr, NULL, NULL, NULL, NULL, NULL);