Skip to content

Conversation

@casaroli
Copy link

@casaroli casaroli commented Nov 15, 2025

We know timerfd is not POSIX, nor is eventfd, however these interfaces are popular enough to be available on Linux, NuttX, FreeBSD, etc.

We already have eventfd, we introduce here timerfd, which is a similar implementation as eventfd, and can be used as a no-thread, no-SIGNAL implementation alternative to POSIX timers.

This will allow applications to use a similar timerfd as defined in timerfd_create(2).

@casaroli casaroli force-pushed the timerfd branch 3 times, most recently from 124b4f3 to 47f8221 Compare November 15, 2025 11:26
This add a timerfd-like interface similar to other systems.
Although this is not POSIX, it is very popular alternative to
POSIX timers.

The implementation is based on the current eventfd implementation,
with the relevant improvements and changes.

Add the ZVFS_TIMERFD so we can selectively enable/disable the
timerfd implementation.

Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
zassert_equal(ret, 0, "timerfd blocked after write");
zassert_equal(event, POLLIN, "POLLIN not set");

zassert_equal(read(fd, &val, sizeof(val)), sizeof(val), "read failed");

Check failure

Code scanning / SonarCloud

POSIX functions should not be called with arguments that trigger buffer overflows High test

"read" overflows write buffer "&val"; passed size "sizeof(val)" (64) exceeds buffer size (8) See more on SonarQube Cloud
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go home, SonarCloud. You are drunk. This is read(2). I suspect SonarCloud thinks this is like fread(3). I don't know why it thinks read(2) would write 64 bytes instead of 8.

ret = is_blocked(fd, &event);
zassert_equal(ret, 0, "timerfd is blocked with initval != 0");

ret = read(fd, &val, sizeof(val));

Check failure

Code scanning / SonarCloud

POSIX functions should not be called with arguments that trigger buffer overflows High test

"read" overflows write buffer "&val"; passed size "sizeof(val)" (64) exceeds buffer size (8) See more on SonarQube Cloud
uint64_t value;
struct timerfd_fixture *fixture = arg1;

zassert_equal(read(fixture->fd, &value, sizeof(value)), sizeof(value));

Check failure

Code scanning / SonarCloud

POSIX functions should not be called with arguments that trigger buffer overflows High test

"read" overflows write buffer "&value"; passed size "sizeof(value)" (64) exceeds buffer size (8) See more on SonarQube Cloud
zassert_equal(fds[0].revents, ZSOCK_POLLIN);

/* Check value */
zassert_equal(read(fixture->fd, &value, sizeof(value)), sizeof(value));

Check failure

Code scanning / SonarCloud

POSIX functions should not be called with arguments that trigger buffer overflows High test

"read" overflows write buffer "&value"; passed size "sizeof(value)" (64) exceeds buffer size (8) See more on SonarQube Cloud
/* Try writing and reading. Should not fail. */
zassert_ok(ioctl(fixture->fd, TFD_IOC_SET_TICKS, (uint64_t)3));

ret = read(fixture->fd, &val, sizeof(val));

Check failure

Code scanning / SonarCloud

POSIX functions should not be called with arguments that trigger buffer overflows High test

"read" overflows write buffer "&val"; passed size "sizeof(val)" (64) exceeds buffer size (8) See more on SonarQube Cloud
{
timerfd_t value;

read(fd, &value, sizeof(value));

Check failure

Code scanning / SonarCloud

POSIX functions should not be called with arguments that trigger buffer overflows High test

"read" overflows write buffer "&value"; passed size "sizeof(value)" (64) exceeds buffer size (8) See more on SonarQube Cloud

/*TESTPOINT: Check if timerfd test passed*/
uint64_t exp_count;
zassert_equal(read(fixture->fd, &exp_count, sizeof(exp_count)), sizeof(exp_count));

Check failure

Code scanning / SonarCloud

POSIX functions should not be called with arguments that trigger buffer overflows High test

"read" overflows write buffer "&exp\_count"; passed size "sizeof(exp\_count)" (64) exceeds buffer size (8) See more on SonarQube Cloud
@casaroli casaroli force-pushed the timerfd branch 2 times, most recently from 43622a1 to 5a90157 Compare November 15, 2025 11:35
This is a wrapper around zvfs_timerfd, so we provide a standard
POSIX "compatible" timerfd interface.

Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
This will test the timerfd interface and behavior similar to the
popular interface available on other POSIX OS.

The tests are based on the eventfd plus the POSIX timer tests.

Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
Add the timerfd sample and README file.

The timerfd follows a similar behavior as specified in other
POSIX OS.

Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
@casaroli casaroli marked this pull request as ready for review November 15, 2025 11:41
@zephyrbot zephyrbot added area: POSIX POSIX API Library area: Samples Samples area: Tests Issues related to a particular existing or missing test area: Base OS Base OS Library (lib/os) labels Nov 15, 2025
@sonarqubecloud
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
E Security Rating on New Code (required ≥ C)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: Base OS Base OS Library (lib/os) area: POSIX POSIX API Library area: Samples Samples area: Tests Issues related to a particular existing or missing test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants