Skip to content
Open
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
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,9 @@ unittest: prepare libhv
$(CC) -g -Wall -O0 -std=c99 -I. -Ibase -o bin/hmutex_test unittest/hmutex_test.c base/htime.c -pthread
$(CC) -g -Wall -O0 -std=c99 -I. -Ibase -o bin/connect_test unittest/connect_test.c base/hsocket.c base/htime.c
$(CC) -g -Wall -O0 -std=c99 -I. -Ibase -o bin/socketpair_test unittest/socketpair_test.c base/hsocket.c base/htime.c
ifneq ($(findstring linux,$(shell $(CC) -dumpmachine)),)
$(CC) -g -Wall -O0 -std=c99 -I. -Ibase -Issl -Ievent -o bin/read_registration_test unittest/read_registration_test.c -Llib -lhv -pthread
endif
$(CC) -g -Wall -O0 -std=c99 -I. -Iutil -o bin/base64 unittest/base64_test.c util/base64.c
$(CC) -g -Wall -O0 -std=c99 -I. -Iutil -o bin/md5 unittest/md5_test.c util/md5.c
$(CC) -g -Wall -O0 -std=c99 -I. -Iutil -o bin/sha1 unittest/sha1_test.c util/sha1.c
Expand Down
13 changes: 8 additions & 5 deletions event/hevent.c
Original file line number Diff line number Diff line change
Expand Up @@ -767,8 +767,9 @@ size_t hio_write_bufsize(hio_t* io) {
}

int hio_read_once (hio_t* io) {
if (hio_watch_read(io) != 0) return -1;
io->read_flags |= HIO_READ_ONCE;
return hio_read_start(io);
return 0;
}

int hio_read_until_length(hio_t* io, unsigned int len) {
Expand All @@ -782,7 +783,8 @@ int hio_read_until_length(hio_t* io, unsigned int len) {
hio_read_cb(io, buf, len);
return len;
}
io->read_flags = HIO_READ_UNTIL_LENGTH;
if (hio_watch_read(io) != 0) return -1;
io->read_flags = HIO_READ_ONCE | HIO_READ_UNTIL_LENGTH;
io->read_until_length = len;
if (io->readbuf.head > 1024 || io->readbuf.tail - io->readbuf.head < 1024) {
hio_memmove_readbuf(io);
Expand All @@ -793,7 +795,7 @@ int hio_read_until_length(hio_t* io, unsigned int len) {
io->readbuf.len < need_len) {
hio_alloc_readbuf(io, need_len);
}
return hio_read_once(io);
return 0;
}

int hio_read_until_delim(hio_t* io, unsigned char delim) {
Expand All @@ -814,14 +816,15 @@ int hio_read_until_delim(hio_t* io, unsigned char delim) {
++p;
}
}
io->read_flags = HIO_READ_UNTIL_DELIM;
if (hio_watch_read(io) != 0) return -1;
io->read_flags = HIO_READ_ONCE | HIO_READ_UNTIL_DELIM;
io->read_until_length = delim;
// NOTE: prepare readbuf
if (hio_is_loop_readbuf(io) ||
io->readbuf.len < HLOOP_READ_BUFSIZE) {
hio_alloc_readbuf(io, HLOOP_READ_BUFSIZE);
}
return hio_read_once(io);
return 0;
}

int hio_read_remain(hio_t* io) {
Expand Down
1 change: 1 addition & 0 deletions event/hevent.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ struct hio_s {
*
* hloop_stop => hloop_free => hio_free => HV_FREE(io)
*/
int hio_watch_read(hio_t* io);
void hio_init(hio_t* io);
void hio_ready(hio_t* io);
void hio_done(hio_t* io);
Expand Down
49 changes: 35 additions & 14 deletions event/hloop.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ static void eventfd_read_cb(hio_t* io, void* buf, int readbytes) {
hmutex_unlock(&loop->custom_events_mutex);
}

static void hloop_destroy_eventfds(hloop_t* loop);

static int hloop_create_eventfds(hloop_t* loop) {
#if defined(OS_UNIX) && HAVE_EVENTFD
int efd = eventfd(0, 0);
Expand All @@ -252,7 +254,14 @@ static int hloop_create_eventfds(hloop_t* loop) {
return -1;
}
#endif
hio_t* io = hread(loop, loop->eventfds[EVENTFDS_READ_INDEX], NULL, 0, eventfd_read_cb);
hio_t* io = hio_get(loop, loop->eventfds[EVENTFDS_READ_INDEX]);
if (hio_watch_read(io) != 0) {
hio_detach(io);
hio_free(io);
hloop_destroy_eventfds(loop);
return -1;
}
io->read_cb = eventfd_read_cb;
io->priority = HEVENT_HIGH_PRIORITY;
++loop->intern_nevents;
return 0;
Expand All @@ -266,8 +275,7 @@ static void hloop_destroy_eventfds(hloop_t* loop) {
SAFE_CLOSE(loop->eventfds[0]);
SAFE_CLOSE(loop->eventfds[1]);
#else
// NOTE: Avoid duplication closesocket in hio_cleanup
// SAFE_CLOSESOCKET(loop->eventfds[EVENTFDS_READ_INDEX]);
// The read hio closes the socketpair reader.
SAFE_CLOSESOCKET(loop->eventfds[EVENTFDS_WRITE_INDEX]);
#endif
loop->eventfds[0] = loop->eventfds[1] = -1;
Expand Down Expand Up @@ -476,12 +484,14 @@ int hloop_run(hloop_t* loop) {
loop->tid = hv_gettid();
hlogd("hloop_run tid=%ld", loop->tid);

int ret = 0;
if (loop->intern_nevents == 0) {
hmutex_lock(&loop->custom_events_mutex);
if (loop->eventfds[EVENTFDS_WRITE_INDEX] == -1) {
hloop_create_eventfds(loop);
ret = hloop_create_eventfds(loop);
}
hmutex_unlock(&loop->custom_events_mutex);
if (ret != 0) goto done;

#ifdef DEBUG
htimer_add(loop, hloop_stat_timer_cb, HLOOP_STAT_TIMEOUT, INFINITE);
Expand All @@ -506,13 +516,14 @@ int hloop_run(hloop_t* loop) {
}
}

done:
loop->status = HLOOP_STATUS_STOP;
loop->end_hrtime = gethrtime_us();

if (loop->flags & HLOOP_FLAG_AUTO_FREE) {
hloop_free(&loop);
}
return 0;
return ret;
}

int hloop_wakeup(hloop_t* loop) {
Expand Down Expand Up @@ -886,17 +897,16 @@ int hio_add(hio_t* io, hio_cb cb, int events) {
hio_ready(io);
}

if (cb) {
io->cb = (hevent_cb)cb;
}

int add_events = events & ~io->events;
if (add_events && iowatcher_add_event(loop, io->fd, add_events) != 0) {
hlogd("iowatcher_add_event failed fd=%d add_events=%d io->events=%d err=%d",
io->fd, add_events, io->events, socket_errno());
return -1;
}
io->events |= add_events;
if (cb) {
io->cb = (hevent_cb)cb;
}

if (!io->active) {
// A NULL callback only changes the watched mask. Keep the transport
Expand Down Expand Up @@ -953,18 +963,29 @@ int hio_close_async(hio_t* io) {
}

//------------------high-level apis-------------------------------------------
hio_t* hread(hloop_t* loop, int fd, void* buf, size_t len, hread_cb read_cb) {
hio_t* io = hio_get(loop, fd);
assert(io != NULL);
static int hio_start_read(hio_t* io, void* buf, size_t len, hread_cb read_cb) {
if (hio_watch_read(io) != 0) return -1;
if (buf && len) {
io->readbuf.base = (char*)buf;
io->readbuf.len = len;
}
if (read_cb) {
io->read_cb = read_cb;
}
hio_read(io);
return io;
if (io->readbuf.tail > io->readbuf.head && io->unpack_setting == NULL && io->read_flags == 0) {
hio_read_remain(io);
}
return 0;
}

int hio_read(hio_t* io) {
return hio_start_read(io, NULL, 0, NULL);
}

hio_t* hread(hloop_t* loop, int fd, void* buf, size_t len, hread_cb read_cb) {
hio_t* io = hio_get(loop, fd);
assert(io != NULL);
return hio_start_read(io, buf, len, read_cb) == 0 ? io : NULL;
}

hio_t* hwrite(hloop_t* loop, int fd, const void* buf, size_t len, hwrite_cb write_cb) {
Expand Down
2 changes: 2 additions & 0 deletions event/hloop.h
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ HV_EXPORT int hio_accept (hio_t* io);
// and TLS handshakes have completed. It is not merely TCP connect completion.
HV_EXPORT int hio_connect(hio_t* io);

// Returns -1 if the backend reports a read-watch registration failure. Buffered input is preserved on failure.
// hio_add(io, HV_READ) => read => hread_cb
HV_EXPORT int hio_read (hio_t* io);
#define hio_read_start(io) hio_read(io)
Expand Down Expand Up @@ -467,6 +468,7 @@ HV_EXPORT int hio_close_async(hio_t* io);

//------------------high-level apis-------------------------------------------
// hio_get -> hio_set_readbuf -> hio_setcb_read -> hio_read
// Returns NULL on registration failure. The buffer and callback are installed only on success.
HV_EXPORT hio_t* hread (hloop_t* loop, int fd, void* buf, size_t len, hread_cb read_cb);
// hio_get -> hio_setcb_write -> hio_write
HV_EXPORT hio_t* hwrite (hloop_t* loop, int fd, const void* buf, size_t len, hwrite_cb write_cb DEFAULT(NULL));
Expand Down
10 changes: 2 additions & 8 deletions event/nio.c
Original file line number Diff line number Diff line change
Expand Up @@ -469,18 +469,12 @@ int hio_connect(hio_t* io) {
return hio_add(io, hio_handle_events, HV_WRITE);
}

int hio_read (hio_t* io) {
int hio_watch_read(hio_t* io) {
if (io->closed) {
hloge("hio_read called but fd[%d] already closed!", io->fd);
return -1;
}
hio_add(io, hio_handle_events, HV_READ);
if (io->readbuf.tail > io->readbuf.head &&
io->unpack_setting == NULL &&
io->read_flags == 0) {
hio_read_remain(io);
}
return 0;
return hio_add(io, hio_handle_events, HV_READ);
}

static int hio_write4 (hio_t* io, const void* buf, size_t len, struct sockaddr* addr) {
Expand Down
3 changes: 3 additions & 0 deletions scripts/unittest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ cd ${ROOT_DIR}

bin/rbtree_test
bin/hbase_test
if [ -x bin/read_registration_test ]; then
bin/read_registration_test || exit $?
fi
bin/date
bin/ifconfig
bin/mkdir_p 123/456
Expand Down
8 changes: 8 additions & 0 deletions unittest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ target_include_directories(connect_test PRIVATE .. ../base)
add_executable(socketpair_test socketpair_test.c ../base/hsocket.c ../base/htime.c)
target_include_directories(socketpair_test PRIVATE .. ../base)

if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND NOT WITH_IO_URING)
add_executable(read_registration_test read_registration_test.c)
target_include_directories(read_registration_test PRIVATE .. ../base ../ssl ../event)
target_link_libraries(read_registration_test ${HV_LIBRARIES})
set(READ_REGISTRATION_UNITTEST_TARGET read_registration_test)
endif()

# ------util------
add_executable(base64 base64_test.c ../util/base64.c)
target_include_directories(base64 PRIVATE .. ../util)
Expand Down Expand Up @@ -235,6 +242,7 @@ add_custom_target(unittest DEPENDS
ping
ftp
sendmail
${READ_REGISTRATION_UNITTEST_TARGET}
http_router_test
${HTTP_UNITTEST_TARGETS}
${HTTP_LUA_UNITTEST_TARGETS}
Expand Down
98 changes: 98 additions & 0 deletions unittest/read_registration_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/* Linux epoll rejects regular files. For an already active socket, inject
* a single failed MOD and let the kernel deliver its original read event. */
#define _GNU_SOURCE
#include "hloop.h"
#include "hbuf.h"
#include <sys/epoll.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#include <unistd.h>

static int reject_mod_fd = -1;
static int reject_next_add;
static int ready_calls;
static int buffered_reads;

int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event) {
if ((op == EPOLL_CTL_MOD && fd == reject_mod_fd) || (op == EPOLL_CTL_ADD && reject_next_add)) {
reject_mod_fd = -1;
reject_next_add = 0;
errno = ENOSPC;
return -1;
}
return (int)syscall(SYS_epoll_ctl, epfd, op, fd, event);
}

static void require(int ok, const char *message) {
if (!ok) {
fprintf(stderr, "read_registration_test: %s\n", message);
exit(EXIT_FAILURE);
}
}

static void unexpected_read(hio_t *io, void *buf, int len) {
(void)io;
(void)buf;
(void)len;
require(0, "rejected read dispatched a callback");
}

static void buffered_read(hio_t *io, void *buf, int len) {
(void)io;
require(len == 1 && *(char *)buf == 'b', "buffered read delivered the wrong bytes");
++buffered_reads;
}

static void original_ready(hio_t *io) {
char byte;
require(read(hio_fd(io), &byte, 1) == 1 && byte == 'r', "could not receive the original read event");
++ready_calls;
}

static void unexpected_ready(hio_t *io) {
(void)io;
require(0, "rejected upgrade replaced the active callback");
}

int main(void) {
if (strcmp(hio_engine(), "epoll") != 0) return 0;
hloop_t *loop = hloop_new(HLOOP_FLAG_RUN_ONCE);
FILE *file = tmpfile();
require(file != NULL, "could not create a regular file");
hio_t *file_io = hio_get(loop, fileno(file));
require(hio_read(file_io) < 0 && hio_events(file_io) == 0, "regular file read reported success");
char readbuf[32] = "b";
hio_set_readbuf(file_io, readbuf, sizeof(readbuf));
hio_setcb_read(file_io, buffered_read);
hio_get_readbuf(file_io)->tail = 1;
require(hio_read(file_io) < 0 && buffered_reads == 0, "failed read consumed buffered input");
require(hio_read_once(file_io) < 0 && hio_read_until_length(file_io, 2) < 0 && hio_read_until_delim(file_io, '\n') < 0,
"failed framed read reported success");
char replacement[32];
require(hread(loop, fileno(file), replacement, sizeof(replacement), unexpected_read) == NULL, "hread reported success");
require(hio_get_readbuf(file_io)->base == readbuf, "failed hread retained the supplied buffer");

require(hio_read_until_length(file_io, 1) == 1 && buffered_reads == 1, "failed hread lost buffered input or its callback");

int pair[2];
require(socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == 0, "could not open socket pair");
hio_t *io = hio_get(loop, pair[0]);
hio_set_readbuf(io, readbuf, sizeof(readbuf));
hio_get_readbuf(io)->tail = 1;
require(hread(loop, pair[0], NULL, 0, buffered_read) == io && buffered_reads == 2, "hread did not immediately deliver buffered input");
require(hio_add(io, original_ready, HV_READ) == 0, "could not register original reader");
reject_mod_fd = pair[0];
require(hio_add(io, unexpected_ready, HV_RDWR) < 0 && hio_events(io) == HV_READ, "failed upgrade changed interests");
require(write(pair[1], "r", 1) == 1 && hloop_run(loop) == 0 && ready_calls == 1, "read interest was lost");

hloop_free(&loop);
fclose(file);
close(pair[1]);

loop = hloop_new(HLOOP_FLAG_RUN_ONCE | HLOOP_FLAG_QUIT_WHEN_NO_ACTIVE_EVENTS);
reject_next_add = 1;
require(hloop_run(loop) < 0, "loop started without a working wake reader");
require(hloop_run(loop) == 0, "could not retry loop startup");
hloop_free(&loop);
return 0;
}
Loading