Skip to content

posix: implement _POSIX_SYNCHRONIZED_IO #74383

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
Jul 29, 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
2 changes: 1 addition & 1 deletion doc/services/portability/posix/aep/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ The *Minimal Realtime System Profile* (PSE51) includes all of the
:ref:`_POSIX_MEMLOCK_RANGE <posix_option_memlock_range>`, 200809L, :kconfig:option:`CONFIG_POSIX_MEMLOCK_RANGE`
:ref:`_POSIX_MONOTONIC_CLOCK <posix_option_monotonic_clock>`, 200809L, :kconfig:option:`CONFIG_POSIX_MONOTONIC_CLOCK`
:ref:`_POSIX_SHARED_MEMORY_OBJECTS <posix_shared_memory_objects>`, 200809L, :kconfig:option:`CONFIG_POSIX_SHARED_MEMORY_OBJECTS`
:ref:`_POSIX_SYNCHRONIZED_IO <posix_option_synchronized_io>`, -1, :kconfig:option:`CONFIG_POSIX_SYNCHRONIZED_IO`
:ref:`_POSIX_SYNCHRONIZED_IO <posix_option_synchronized_io>`, 200809L, :kconfig:option:`CONFIG_POSIX_SYNCHRONIZED_IO`
:ref:`_POSIX_THREAD_ATTR_STACKADDR<posix_option_thread_attr_stackaddr>`, 200809L, :kconfig:option:`CONFIG_POSIX_THREAD_ATTR_STACKADDR`
:ref:`_POSIX_THREAD_ATTR_STACKSIZE<posix_option_thread_attr_stacksize>`, 200809L, :kconfig:option:`CONFIG_POSIX_THREAD_ATTR_STACKSIZE`
:ref:`_POSIX_THREAD_CPUTIME <posix_option_thread_cputime>`, 200809L, :kconfig:option:`CONFIG_POSIX_CPUTIME`
Expand Down
2 changes: 1 addition & 1 deletion doc/services/portability/posix/conformance/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ POSIX System Interfaces
:ref:`_POSIX_SHARED_MEMORY_OBJECTS <posix_shared_memory_objects>`, 200809L, :kconfig:option:`CONFIG_POSIX_SHARED_MEMORY_OBJECTS`
_POSIX_SPAWN, -1, :ref:`†<posix_undefined_behaviour>`
_POSIX_SPORADIC_SERVER, -1, :ref:`†<posix_undefined_behaviour>`
:ref:`_POSIX_SYNCHRONIZED_IO <posix_option_synchronized_io>`, -1, :kconfig:option:`CONFIG_POSIX_SYNCHRONIZED_IO`
:ref:`_POSIX_SYNCHRONIZED_IO <posix_option_synchronized_io>`, 200809L, :kconfig:option:`CONFIG_POSIX_SYNCHRONIZED_IO`
:ref:`_POSIX_THREAD_ATTR_STACKADDR<posix_option_thread_attr_stackaddr>`, 200809L, :kconfig:option:`CONFIG_POSIX_THREAD_ATTR_STACKADDR`
:ref:`_POSIX_THREAD_ATTR_STACKSIZE<posix_option_thread_attr_stacksize>`, 200809L, :kconfig:option:`CONFIG_POSIX_THREAD_ATTR_STACKSIZE`
:ref:`_POSIX_THREAD_CPUTIME <posix_option_thread_cputime>`, 200809L, :kconfig:option:`CONFIG_POSIX_CPUTIME`
Expand Down
4 changes: 2 additions & 2 deletions doc/services/portability/posix/option_groups/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -803,9 +803,9 @@ _POSIX_SYNCHRONIZED_IO
:header: API, Supported
:widths: 50,10

fdatasync(),
fdatasync(),yes
fsync(),yes
msync(),
msync(),yes

.. _posix_option_thread_attr_stackaddr:

Expand Down
5 changes: 4 additions & 1 deletion include/zephyr/posix/posix_features.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@
#endif

/* #define _POSIX_SPORADIC_SERVER (-1L) */
/* #define _POSIX_SYNCHRONIZED_IO (-1L) */

#ifdef CONFIG_POSIX_SYNCHRONIZED_IO
#define _POSIX_SYNCHRONIZED_IO _POSIX_VERSION
#endif

#ifdef CONFIG_POSIX_THREAD_ATTR_STACKADDR
#define _POSIX_THREAD_ATTR_STACKADDR _POSIX_VERSION
Expand Down
4 changes: 4 additions & 0 deletions include/zephyr/posix/unistd.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ off_t lseek(int file, off_t offset, int whence);
int fsync(int fd);
int ftruncate(int fd, off_t length);

#ifdef CONFIG_POSIX_SYNCHRONIZED_IO
int fdatasync(int fd);
#endif /* CONFIG_POSIX_SYNCHRONIZED_IO */

/* File System related operations */
int rename(const char *old, const char *newp);
int unlink(const char *path);
Expand Down
7 changes: 7 additions & 0 deletions lib/posix/options/fsync.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,10 @@ int fsync(int fd)
#ifdef CONFIG_POSIX_FILE_SYSTEM_ALIAS_FSYNC
FUNC_ALIAS(fsync, _fsync, int);
#endif

#ifdef CONFIG_POSIX_SYNCHRONIZED_IO
int fdatasync(int fd)
{
return fsync(fd);
}
#endif /* CONFIG_POSIX_SYNCHRONIZED_IO */
37 changes: 37 additions & 0 deletions tests/posix/fs/src/test_fs_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,26 @@ static int test_file_fsync(void)
return res;
}

#ifdef CONFIG_POSIX_SYNCHRONIZED_IO
static int test_file_fdatasync(void)
{
int res = 0;

if (file < 0)
return res;

res = fdatasync(file);
if (res < 0) {
TC_ERROR("Failed to sync file: %d, errno = %d\n", res, errno);
res = TC_FAIL;
}

close(file);
file = -1;
return res;
}
#endif /* CONFIG_POSIX_SYNCHRONIZED_IO */

static int test_file_truncate(void)
{
int res = 0;
Expand Down Expand Up @@ -251,6 +271,23 @@ ZTEST(posix_fs_file_test, test_fs_sync)
zassert_true(test_file_fsync() == TC_PASS);
}

/**
* @brief Test for POSIX fdatasync API
*
* @details Test sync the file through POSIX fdatasync API.
*/
ZTEST(posix_fs_file_test, test_fs_datasync)
{
#ifdef CONFIG_POSIX_SYNCHRONIZED_IO
/* FIXME: restructure tests as per #46897 */
zassert_true(test_file_open() == TC_PASS);
zassert_true(test_file_write() == TC_PASS);
zassert_true(test_file_fdatasync() == TC_PASS);
#else
ztest_test_skip();
#endif
}

/**
* @brief Test for POSIX ftruncate API
*
Expand Down
6 changes: 3 additions & 3 deletions tests/posix/headers/src/unistd_h.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,12 @@ ZTEST(posix_headers, test_unistd_h)
/* zassert_not_null(fchdir); */ /* not implemented */
/* zassert_not_null(fchown); */ /* not implemented */
/* zassert_not_null(fchownat); */ /* not implemented */
/* zassert_not_null(fdatasync); */ /* not implemented */
zassert_not_null(fdatasync);
/* zassert_not_null(fexecve); */ /* not implemented */
/* zassert_not_null(fork); */ /* not implemented */
/* zassert_not_null(fpathconf); */ /* not implemented */
/* zassert_not_null(fsync); */ /* not implemented */
/* zassert_not_null(ftruncate); */ /* not implemented */
zassert_not_null(fsync);
zassert_not_null(ftruncate);
/* zassert_not_null(getcwd); */ /* not implemented */
/* zassert_not_null(getegid); */ /* not implemented */
/* zassert_not_null(geteuid); */ /* not implemented */
Expand Down
Loading