15 Commits
Author SHA1 Message Date
Joachim Wiberg f5841413e2 Open v2.5.0 release cycle
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-16 15:08:56 +01:00
Joachim Wiberg be89142b39 Update ChangeLog with changes to timers, cron, and UEV_NONBLOCK
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-16 15:08:55 +01:00
Joachim Wiberg aedc189672 Reset watcher descriptor on failed cron and signal init
When uev_cron_init() or uev_signal_init() fails to add the watcher to
the event loop, the watcher is left with a descriptor that is closed.
A later set or start on it then operates on that descriptor, which by
then may belong to something else.

Reset it to -1, as the timer and event watchers do.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-16 15:08:43 +01:00
Joachim Wiberg 0b5461d5ff Fix NULL pointer dereference in uev_timer_init()
uev_timer_init() crashes when called without a watcher:

    uev_timer_init(ctx, NULL, cb, NULL, 100, 0);

The cleanup after a failed setup resets the descriptor in the watcher
without checking that there is one.

Check the arguments first, like the other watcher types do.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-16 15:08:43 +01:00
NefelibateLG 397247d567 Run cron watcher with start time zero when the event loop starts
A cron watcher with start time zero, documented to mean 'now', never
runs:

    uev_cron_init(ctx, w, cb, NULL, 0, 3600);

Zero is passed as is to the timerfd, where it disarms the timer.

Replace it with the current time when the event loop is running.  This
also goes for uev_cron_set(), where zero no longer disarms the watcher,
use uev_cron_stop() for that.

The event loop used a start time of zero to mark an at job as done.
Stop the watcher there instead and keep its time, so restarting an at
job that was interrupted by a clock change does not run it at once.

Closes #30
2026-11-16 15:08:42 +01:00
NefelibateLG 28643fa599 Return from uev_run() when UEV_NONBLOCK and no event is pending
uev_run() with only UEV_NONBLOCK never returns when there are watchers
but no events, it spins at full CPU load:

    uev_run(ctx, UEV_NONBLOCK);

The loop polls epoll with a zero timeout and only stops on UEV_ONCE.

Leave the loop when a nonblocking poll finds no events.
2026-11-16 15:08:42 +01:00
omrzk 09d9b6ba04 Keep timer deadlines between calls to uev_run()
Timers never expire when the event loop is run one iteration at a time
and something else keeps it busy, e.g., when polled from another event
loop:

    uev_run(ctx, UEV_ONCE | UEV_NONBLOCK);

Every call to uev_run() starts all timers over from their full timeout.

Start dormant timers on the first call only.  A caller that wants a
timer restarted for each call must now use uev_timer_set().

Closes #36
2026-11-16 15:08:41 +01:00
Joachim Wiberg 354cdcaf7a Update ChangeLog with fixes from pull requests
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-16 14:54:56 +01:00
NefelibateLG 05bc6a0830 Do not leave stdin as descriptor of a failed context
uev_init() leaves the context descriptor at 0 when epoll_create1()
fails.  A following uev_exit() then closes stdin.

Set it to -1 before creating the epoll descriptor.
2026-11-16 14:54:49 +01:00
NefelibateLG 4792b988a1 Fix eventfd leak in uev_event_init()
uev_event_init() leaks its eventfd, and leaves it in the watcher, when
the watcher cannot be added to the event loop.

Close it and reset the descriptor in the watcher.
2026-11-16 14:54:49 +01:00
NefelibateLG ae28276f3f Fix restart of a stopped cron watcher
uev_cron_start() on a stopped cron watcher brings it back as a regular
timer, with a timeout made up of parts of the cron schedule.  It calls
uev_timer_start(), which reads the timer fields of the watcher and
creates a monotonic timer.

Restart with uev_cron_set() and the cron schedule instead.
2026-11-16 14:54:48 +01:00
omrzk e699cb7f5f Honor UEV_ONCE when stdin is redirected from a file
With stdin redirected from a regular file, uev_run() with UEV_ONCE does
not return until end of file:

    app < file.txt

The workaround for regular files, which epoll cannot watch, restarts
the loop before the UEV_ONCE check.

Check the flag there too.

Closes #37
2026-11-16 14:54:47 +01:00
omrzk fbe2c15377 Honor new descriptor and events in uev_io_set()
uev_io_set() on an active oneshot watcher keeps watching the old
descriptor, for the old events:

    uev_io_set(w, newfd, UEV_READ | UEV_ONESHOT);

It only rearms the watcher, with what is already stored in it.

Rearm only when neither has changed, otherwise stop the watcher and set
it up again.

Closes #35
2026-11-16 14:54:46 +01:00
Joachim Wiberg c964bbe8fb .github: bump actions
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2025-04-28 08:35:10 +02:00
Joachim Wiberg faf7667012 Create SECURITY.md policy 2025-04-28 07:56:01 +02:00
21 changed files with 448 additions and 27 deletions

No files matched your search

+12
View File
@@ -0,0 +1,12 @@
# Security Policy
## Supported Versions
libuEv is a small project, as such we have no possibility to support older versions.
The only supported version is the latest released on GitHub:
<https://github.com/troglobit/libuev/releases>
## Reporting a Vulnerability
Contact the project's main author and owner to report and discuss vulnerabilities.
+2 -2
View File
@@ -44,7 +44,7 @@ jobs:
run: |
make check || (cat test/test-suite.log; false)
- name: Upload Test Results
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: libuev-test-${{ matrix.compiler }}
path: test/*
@@ -53,7 +53,7 @@ jobs:
runs-on: ubuntu-latest
container: debian:stable
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Installing dependencies
run: |
apt-get update
+1 -1
View File
@@ -25,7 +25,7 @@ jobs:
--post-data "token=$TOKEN&project=${COVERITY_PROJ}&md5=1" \
-O coverity-latest.tar.gz.md5
echo "md5=$(cat coverity-latest.tar.gz.md5)" | tee -a $GITHUB_OUTPUT
- uses: actions/cache@v3
- uses: actions/cache@v4
id: cache
with:
path: coverity-latest.tar.gz
+32 -1
View File
@@ -4,6 +4,37 @@ Change Log
All notable changes to the project are documented in this file.
[UNRELEASED][]
--------------
### Changes
- Timers now keep their deadline between calls to `uev_run()`, they are
only started by the first call. Previously every call restarted all
timers, so with `UEV_ONCE` in a busy loop they never expired. Use
`uev_timer_set()` to restart a timer for each call
- `uev_run()` with only `UEV_NONBLOCK` now returns when no event is
pending, previously it never returned and used 100% CPU
- A cron watcher with start time zero now runs when the event loop is
started, as documented, previously it never ran. This means that
`uev_cron_set(w, 0, 0)` no longer disarms a cron watcher, it runs it
now. Use `uev_cron_stop()` to stop it
### Fixes
- Fix `uev_io_set()` ignoring a new file descriptor, or new events, for
an active `UEV_ONESHOT` watcher
- Fix `uev_run()` with `UEV_ONCE` not returning until end of file when
stdin is redirected from a regular file
- Fix `uev_cron_start()` restarting a stopped cron watcher as a regular
timer, with the wrong timeout
- Fix file descriptor leak in `uev_event_init()` when the watcher cannot
be added to the event loop
- Fix `uev_exit()` closing stdin after a failed `uev_init()`
- Fix NULL pointer dereference in `uev_timer_init()` when called without
a watcher
- Fix `uev_cron_init()` and `uev_signal_init()` leaving a closed file
descriptor in the watcher on failure
[v2.4.1][] - 2024-01-04
-----------------------
@@ -453,7 +484,7 @@ v0.0.1 - 2012-03-17
Lua users mailing list.
[UNRELEASED]: https://github.com/troglobit/libuev/compare/v2.4.0...HEAD
[UNRELEASED]: https://github.com/troglobit/libuev/compare/v2.4.1...HEAD
[v2.4.1]: https://github.com/troglobit/libuev/compare/v2.4.0...v2.4.1
[v2.4.0]: https://github.com/troglobit/libuev/compare/v2.3.2...v2.4.0
[v2.3.2]: https://github.com/troglobit/libuev/compare/v2.3.1...v2.3.2
+1 -1
View File
@@ -1,4 +1,4 @@
AC_INIT(libuev, 2.4.1, https://github.com/troglobit/libuev/issues)
AC_INIT(libuev, 2.5.0-dev, https://github.com/troglobit/libuev/issues)
AC_CONFIG_AUX_DIR(aux)
AM_INIT_AUTOMAKE([1.11 foreign dist-xz])
AM_SILENT_RULES([yes])
+5 -2
View File
@@ -168,8 +168,11 @@ result = uev_run(&ctx, UEV_NONE);
```
With `flags` set to `UEV_ONCE` the event loop returns as soon as it has
served the first event. If `flags` is set to `UEV_ONCE | UEV_NONBLOCK`
the event loop returns immediately if no event is available.
served the first event. With `UEV_NONBLOCK` the event loop returns as
soon as no event is available, also when combined with `UEV_ONCE`.
Timers are started by the first call to `uev_run()` and keep their
deadline between calls. Use `uev_timer_set()` to restart a timer.
```
if (result < 0)
+24 -6
View File
@@ -25,6 +25,7 @@
#include <errno.h>
#include <string.h> /* memset() */
#include <sys/timerfd.h>
#include <time.h> /* time() */
#include <unistd.h> /* close(), read() */
#include "uev.h"
@@ -67,6 +68,11 @@ int uev_cron_init(uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, time_t when
{
int fd;
if (!w || !ctx) {
errno = EINVAL;
return -1;
}
if (when < 0 || interval < 0) {
errno = ERANGE;
return -1;
@@ -83,6 +89,7 @@ int uev_cron_init(uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, time_t when
_uev_watcher_stop(w);
exit:
close(fd);
w->fd = -1;
return -1;
}
@@ -92,9 +99,12 @@ int uev_cron_init(uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, time_t when
/**
* Reset an at/cron job watcher
* @param w Watcher to reset
* @param when First point in time to call @p cb
* @param when First point in time to call @p cb, zero for 'now'
* @param interval For an at job this is zero, for cron the offset interval
*
* Note, unlike uev_timer_set() a @p when of zero does not disarm the
* watcher, use uev_cron_stop() for that.
*
* @return POSIX OK(0) or non-zero with @p errno set on error.
*/
int uev_cron_set(uev_t *w, time_t when, time_t interval)
@@ -110,12 +120,12 @@ int uev_cron_set(uev_t *w, time_t when, time_t interval)
return -1;
}
/* Zero is 'now', when the event loop is started */
if (!when && w->ctx->running)
when = time(NULL);
/* Handle stopped timers */
if (w->fd < 0) {
/* Timer already stopped */
if (!when && !interval)
return 0;
if (uev_cron_init(w->ctx, w, (uev_cb_t *)w->cb, w->arg, when, interval))
return -1;
}
@@ -144,7 +154,15 @@ int uev_cron_set(uev_t *w, time_t when, time_t interval)
*/
int uev_cron_start(uev_t *w)
{
return uev_timer_start(w);
if (!w) {
errno = EINVAL;
return -1;
}
if (-1 != w->fd)
_uev_watcher_stop(w);
return uev_cron_set(w, w->u.c.when, w->u.c.interval);
}
/**
+8 -2
View File
@@ -57,8 +57,14 @@ int uev_event_init(uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg)
if (fd < 0)
return -1;
return _uev_watcher_init(ctx, w, UEV_EVENT_TYPE, cb, arg, fd, UEV_READ)
|| _uev_watcher_start(w);
if (_uev_watcher_init(ctx, w, UEV_EVENT_TYPE, cb, arg, fd, UEV_READ) ||
_uev_watcher_start(w)) {
close(fd);
w->fd = -1;
return -1;
}
return 0;
}
/**
+2 -1
View File
@@ -64,7 +64,8 @@ int uev_io_init(uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, int fd, int e
*/
int uev_io_set(uev_t *w, int fd, int events)
{
if ((events & UEV_ONESHOT) && _uev_watcher_active(w))
if ((events & UEV_ONESHOT) && _uev_watcher_active(w) &&
fd == w->fd && events == w->events)
return _uev_watcher_rearm(w);
/* Ignore any errors, only to clean up anything lingering ... */
+1
View File
@@ -72,6 +72,7 @@ int uev_signal_init(uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, int signo
_uev_watcher_stop(w);
exit:
close(fd);
w->fd = -1;
return -1;
}
+7 -2
View File
@@ -68,8 +68,8 @@ static void msec2tspec(int msec, struct timespec *ts)
* period and restarted.
*
* A timer is automatically started if the event loop is already
* running, otherwise it is kept on hold until triggered by calling
* uev_run().
* running, otherwise it is kept on hold until triggered by the first
* call to uev_run().
*
* @see uev_timer_set
* @return POSIX OK(0) or non-zero with @p errno set on error.
@@ -78,6 +78,11 @@ int uev_timer_init(uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, int timeou
{
int fd;
if (!w || !ctx) {
errno = EINVAL;
return -1;
}
if (timeout < 0 || period < 0) {
errno = ERANGE;
return -1;
+18 -9
View File
@@ -226,6 +226,7 @@ int uev_init1(uev_ctx_t *ctx, int maxevents)
maxevents = UEV_MAX_EVENTS;
memset(ctx, 0, sizeof(*ctx));
ctx->fd = -1;
ctx->maxevents = maxevents;
return _init(ctx, 0);
@@ -289,9 +290,12 @@ int uev_exit(uev_ctx_t *ctx)
*
* With @p flags set to ::UEV_ONCE the event loop returns after the first
* event has been served, useful for instance to set a timeout on a file
* descriptor. If @p flags also has the ::UEV_NONBLOCK flag set the event
* loop will return immediately if no event is pending, useful when run
* inside another event loop.
* descriptor. With the ::UEV_NONBLOCK flag set the event loop returns
* as soon as no event is pending, useful when run inside another event
* loop.
*
* Timers are started by the first call and keep their deadline between
* calls. Use uev_timer_set() to restart a timer.
*
* @return POSIX OK(0) upon successful termination of the event loop, or
* non-zero on error.
@@ -309,16 +313,17 @@ int uev_run(uev_ctx_t *ctx, int flags)
if (flags & UEV_NONBLOCK)
timeout = 0;
/* Start the event loop */
/* Start the event loop and all dormant timers, first call only */
if (!ctx->running) {
ctx->running = 1;
/* Start all dormant timers */
_UEV_FOREACH(w, ctx->watchers) {
if (UEV_CRON_TYPE == w->type)
uev_cron_set(w, w->u.c.when, w->u.c.interval);
if (UEV_TIMER_TYPE == w->type)
uev_timer_set(w, w->u.t.timeout, w->u.t.period);
}
}
while (ctx->running && ctx->watchers) {
struct epoll_event ee[UEV_MAX_EVENTS];
@@ -344,8 +349,11 @@ int uev_run(uev_ctx_t *ctx, int flags)
}
}
if (rerun)
if (rerun) {
if (flags & UEV_ONCE)
break;
continue;
}
ctx->workaround = 0;
while ((nfds = epoll_wait(ctx->fd, ee, maxevents, timeout)) < 0) {
@@ -361,6 +369,9 @@ int uev_run(uev_ctx_t *ctx, int flags)
return -2;
}
if (!nfds && (flags & UEV_NONBLOCK))
break;
for (i = 0; ctx->running && i < nfds; i++) {
struct signalfd_siginfo fdsi;
ssize_t sz = sizeof(fdsi);
@@ -409,11 +420,9 @@ int uev_run(uev_ctx_t *ctx, int flags)
}
if (!w->u.c.interval)
w->u.c.when = 0;
uev_timer_stop(w);
else
w->u.c.when += w->u.c.interval;
if (!w->u.c.when)
uev_timer_stop(w);
break;
case UEV_EVENT_TYPE:
+7
View File
@@ -3,7 +3,14 @@
active
api
complete
cronnow
cronrun
cronstart
error
io
nonblock
resume
signal
stdin
timer
event
+7
View File
@@ -8,8 +8,15 @@ TESTS =
TESTS += active
TESTS += api
TESTS += complete
TESTS += cronnow
TESTS += cronrun
TESTS += cronstart
TESTS += error
TESTS += io
TESTS += nonblock
TESTS += resume
TESTS += signal
TESTS += stdin
TESTS += timer
TESTS += event
+32
View File
@@ -0,0 +1,32 @@
/* Verifies that a cron watcher with start time zero runs now */
#include "check.h"
static int fired;
static void cb(uev_t *w, void *arg, int events)
{
fired++;
uev_exit(w->ctx);
}
int main(void)
{
uev_ctx_t ctx;
uev_t w;
fail_unless(uev_init(&ctx) == 0);
fail_unless(uev_cron_init(&ctx, &w, cb, NULL, 0, 0) == 0);
alarm(2);
fail_unless(uev_run(&ctx, 0) == 0);
fail_unless(fired == 1);
return 0;
}
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/
+29
View File
@@ -0,0 +1,29 @@
/* Verifies that a stopped cron watcher can be started again */
#include "check.h"
int main(void)
{
time_t when = time(NULL) + 3600;
uev_ctx_t ctx;
uev_t cron;
fail_unless(uev_init(&ctx) == 0);
fail_unless(uev_cron_init(&ctx, &cron, NULL, NULL, when, 3600) == 0);
fail_unless(uev_cron_stop(&cron) == 0);
fail_unless(!uev_cron_active(&cron));
fail_unless(uev_cron_start(&cron) == 0);
fail_unless(uev_cron_active(&cron));
fail_unless(cron.type == UEV_CRON_TYPE);
fail_unless(cron.u.c.when == when);
fail_unless(cron.u.c.interval == 3600);
return uev_exit(&ctx);
}
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/
+87
View File
@@ -0,0 +1,87 @@
/* Verifies cleanup on failure to set up a context or watcher */
#include "check.h"
#include <signal.h>
#include <sys/resource.h>
/* A failed uev_event_init() must not leak its eventfd */
static void event_init(void)
{
uev_ctx_t ctx;
uev_t w;
int fd;
fail_unless(uev_init(&ctx) == 0);
fail_unless(close(ctx.fd) == 0);
fd = dup(STDIN_FILENO);
fail_unless(fd >= 0);
close(fd);
fail_unless(uev_event_init(&ctx, &w, NULL, NULL) != 0);
fail_unless(w.fd == -1);
fail_unless(dup(STDIN_FILENO) == fd);
close(fd);
}
/* A failed uev_init() must not leave stdin as the context descriptor */
static void ctx_init(void)
{
struct rlimit old, lim;
uev_ctx_t ctx;
int rc;
fail_unless(getrlimit(RLIMIT_NOFILE, &old) == 0);
lim = old;
lim.rlim_cur = 0;
fail_unless(setrlimit(RLIMIT_NOFILE, &lim) == 0);
rc = uev_init(&ctx);
fail_unless(setrlimit(RLIMIT_NOFILE, &old) == 0);
fail_unless(rc == -1);
fail_unless(ctx.fd == -1);
}
/* A failed init must not leave a closed descriptor in the watcher */
static void watcher_init(void)
{
uev_ctx_t ctx;
uev_t w;
fail_unless(uev_init(&ctx) == 0);
fail_unless(close(ctx.fd) == 0);
fail_unless(uev_cron_init(&ctx, &w, NULL, NULL, time(NULL) + 3600, 0) != 0);
fail_unless(w.fd == -1);
fail_unless(uev_signal_init(&ctx, &w, NULL, NULL, SIGUSR1) != 0);
fail_unless(w.fd == -1);
}
/* Invalid arguments must not crash */
static void null_init(void)
{
uev_ctx_t ctx;
fail_unless(uev_init(&ctx) == 0);
fail_unless(uev_timer_init(&ctx, NULL, NULL, NULL, 100, 0) != 0);
fail_unless(uev_cron_init(&ctx, NULL, NULL, NULL, time(NULL) + 3600, 0) != 0);
fail_unless(uev_exit(&ctx) == 0);
}
int main(void)
{
event_init();
watcher_init();
null_init();
ctx_init();
return 0;
}
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/
+60
View File
@@ -0,0 +1,60 @@
/* Verifies uev_io_set() on an active oneshot watcher */
#include "check.h"
#include <sys/socket.h>
static int fd = -1;
static int ev;
static void cb(uev_t *w, void *arg, int events)
{
fd = w->fd;
ev = events;
}
static void run(uev_ctx_t *ctx)
{
fd = -1;
ev = 0;
fail_unless(uev_run(ctx, UEV_ONCE | UEV_NONBLOCK) == 0);
}
int main(void)
{
int a[2], b[2];
uev_ctx_t ctx;
uev_t w;
fail_unless(socketpair(AF_UNIX, SOCK_STREAM, 0, a) == 0);
fail_unless(socketpair(AF_UNIX, SOCK_STREAM, 0, b) == 0);
fail_unless(write(b[1], "b", 1) == 1);
fail_unless(uev_init(&ctx) == 0);
fail_unless(uev_io_init(&ctx, &w, cb, NULL, a[0], UEV_READ | UEV_ONESHOT) == 0);
/* New descriptor */
fail_unless(uev_io_set(&w, b[0], UEV_READ | UEV_ONESHOT) == 0);
run(&ctx);
fail_unless(fd == b[0]);
/* New events, same descriptor */
fail_unless(uev_io_set(&w, a[0], UEV_WRITE | UEV_ONESHOT) == 0);
run(&ctx);
fail_unless(fd == a[0]);
fail_unless(ev & UEV_WRITE);
/* Nothing new, only rearm */
run(&ctx);
fail_unless(fd == -1);
fail_unless(uev_io_set(&w, a[0], UEV_WRITE | UEV_ONESHOT) == 0);
run(&ctx);
fail_unless(fd == a[0]);
return uev_exit(&ctx);
}
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/
+35
View File
@@ -0,0 +1,35 @@
/* Verifies that UEV_NONBLOCK returns when no event is pending */
#include "check.h"
static int fired;
static void cb(uev_t *w, void *arg, int events)
{
fired++;
}
int main(void)
{
uev_ctx_t ctx;
uev_t w;
fail_unless(uev_init(&ctx) == 0);
fail_unless(uev_event_init(&ctx, &w, cb, NULL) == 0);
alarm(2);
fail_unless(uev_run(&ctx, UEV_NONBLOCK) == 0);
fail_unless(fired == 0);
fail_unless(uev_event_post(&w) == 0);
fail_unless(uev_run(&ctx, UEV_NONBLOCK) == 0);
fail_unless(fired == 1);
return uev_exit(&ctx);
}
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/
+34
View File
@@ -0,0 +1,34 @@
/* Verifies that timers keep their deadline between calls to uev_run() */
#include "check.h"
static int fired;
static void cb(uev_t *w, void *arg, int events)
{
fired++;
}
int main(void)
{
uev_ctx_t ctx;
uev_t w;
int i;
fail_unless(uev_init(&ctx) == 0);
fail_unless(uev_timer_init(&ctx, &w, cb, NULL, 50, 0) == 0);
for (i = 0; i < 15; i++) {
fail_unless(uev_run(&ctx, UEV_ONCE | UEV_NONBLOCK) == 0);
usleep(10000);
}
fail_unless(fired == 1);
return uev_exit(&ctx);
}
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/
+44
View File
@@ -0,0 +1,44 @@
/* Verifies UEV_ONCE with stdin redirected from a regular file */
#include "check.h"
static int calls;
static void cb(uev_t *w, void *arg, int events)
{
char ch;
calls++;
read(w->fd, &ch, 1);
}
int main(void)
{
FILE *fp = tmpfile();
uev_ctx_t ctx;
uev_t w;
fail_unless(fp != NULL);
fail_unless(fputs("abc", fp) >= 0);
rewind(fp);
fail_unless(dup2(fileno(fp), STDIN_FILENO) == STDIN_FILENO);
fail_unless(uev_init(&ctx) == 0);
fail_unless(uev_io_init(&ctx, &w, cb, NULL, STDIN_FILENO, UEV_READ) == 0);
alarm(5);
fail_unless(uev_run(&ctx, UEV_ONCE) == 0);
fail_unless(calls == 1);
/* Without UEV_ONCE the rest is drained, including EOF */
fail_unless(uev_run(&ctx, 0) == 0);
fail_unless(calls == 4);
return uev_exit(&ctx);
}
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/