Skip to content
Draft
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: 2 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,8 @@ else
ac_cv_func_port_create=no
fi

AC_CHECK_FUNCS([eventfd])

# --with-persistent-storage
AC_ARG_WITH(persistent-storage,
AS_HELP_STRING([--with-persistent-storage],
Expand Down
1 change: 1 addition & 0 deletions include/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ nobase_noinst_HEADERS = \
vcs_version.h \
vct.h \
vcurses.h \
vefd.h \
venc.h \
vend.h \
vev.h \
Expand Down
50 changes: 50 additions & 0 deletions include/vefd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*-
* Copyright (c) 2025 Varnish Software AS
* All rights reserved.
*
* Author: Dridi Boukelmoune <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer
* in this position and unchanged.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/

struct vefd {
unsigned magic;
#define VEFD_MAGIC 0x1548c1a6
int poll_fd;
int priv_fd;
};

#define VEFD_INIT(vefd) \
do { \
INIT_OBJ(vefd, VEFD_MAGIC); \
(vefd)->poll_fd = -1; \
(vefd)->priv_fd = -1; \
} while (0)

int VEFD_Open(struct vefd *);
int VEFD_Signal(struct vefd *);
int VEFD_Clear(struct vefd *);
int VEFD_Close(struct vefd *);
1 change: 1 addition & 0 deletions lib/libvarnish/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ libvarnish_la_SOURCES = \
vcli_serve.c \
vct.c \
venc.c \
vefd.c \
version.c \
vev.c \
vfil.c \
Expand Down
163 changes: 163 additions & 0 deletions lib/libvarnish/vefd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*-
* Copyright (c) 2025 Varnish Software AS
* All rights reserved.
*
* Author: Dridi Boukelmoune <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer
* in this position and unchanged.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/

#include "config.h"

#if HAVE_EVENTFD
# include <sys/eventfd.h>
#else
# include <fcntl.h>
#endif

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

#include <vdef.h>
#include <vefd.h>
#include <vas.h>
#include <miniobj.h>

#if HAVE_EVENTFD
int
VEFD_Open(struct vefd *vefd)
{

CHECK_OBJ_NOTNULL(vefd, VEFD_MAGIC);
assert(vefd->poll_fd == -1);
assert(vefd->priv_fd == -1);

vefd->poll_fd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
return (vefd->poll_fd);
}

int
VEFD_Signal(struct vefd *vefd)
{
int64_t buf = 1;

CHECK_OBJ_NOTNULL(vefd, VEFD_MAGIC);
assert(vefd->poll_fd >= 0);
assert(vefd->priv_fd == -1);
assert(write(vefd->poll_fd, &buf, sizeof buf) == sizeof buf);
return (0);
}

int
VEFD_Clear(struct vefd *vefd)
{
int64_t buf;

CHECK_OBJ_NOTNULL(vefd, VEFD_MAGIC);
assert(vefd->poll_fd >= 0);
assert(vefd->priv_fd == -1);
assert(read(vefd->poll_fd, &buf, sizeof buf) == sizeof buf);
return (0);
}

int
VEFD_Close(struct vefd *vefd)
{

CHECK_OBJ_NOTNULL(vefd, VEFD_MAGIC);
assert(vefd->poll_fd >= 0);
assert(vefd->priv_fd == -1);
closefd(&vefd->poll_fd);
return (0);
}
#else /* !HAVE_EVENTFD */
int
VEFD_Open(struct vefd *vefd)
{
int fd[2];

CHECK_OBJ_NOTNULL(vefd, VEFD_MAGIC);
assert(vefd->poll_fd == -1);
assert(vefd->priv_fd == -1);

if (pipe(fd) < 0)
return (-1);

AZ(fcntl(fd[0], F_SETFL, O_CLOEXEC|O_NONBLOCK));
AZ(fcntl(fd[1], F_SETFL, O_CLOEXEC|O_NONBLOCK));
vefd->poll_fd = fd[0];
vefd->priv_fd = fd[1];
return (0);
}

int
VEFD_Signal(struct vefd *vefd)
{
ssize_t r;

CHECK_OBJ_NOTNULL(vefd, VEFD_MAGIC);
assert(vefd->poll_fd >= 0);
assert(vefd->priv_fd >= 0);
assert(vefd->poll_fd != vefd->priv_fd);
r = write(vefd->priv_fd, "", 1);
if (r < 0 && errno != EAGAIN && errno != EWOULDBLOCK)
return (-1);
return (0);
}

int
VEFD_Clear(struct vefd *vefd)
{
char buf[64];
ssize_t r;

CHECK_OBJ_NOTNULL(vefd, VEFD_MAGIC);
assert(vefd->poll_fd >= 0);
assert(vefd->priv_fd >= 0);
assert(vefd->poll_fd != vefd->priv_fd);
do {
r = read(vefd->poll_fd, buf, sizeof buf);
} while (r > 0);
if (errno != EAGAIN && errno != EWOULDBLOCK)
return (-1);
return (0);
}

int
VEFD_Close(struct vefd *vefd)
{

CHECK_OBJ_NOTNULL(vefd, VEFD_MAGIC);
assert(vefd->poll_fd >= 0);
assert(vefd->priv_fd >= 0);
assert(vefd->poll_fd != vefd->priv_fd);
closefd(&vefd->poll_fd);
closefd(&vefd->priv_fd);
return (0);
}
#endif /* HAVE_EVENTFD */