diff --git a/meson.build b/meson.build index fe9bc43..b165ace 100644 --- a/meson.build +++ b/meson.build @@ -38,6 +38,7 @@ src_libei = [ 'src/libei.c', 'src/libei-device.c', 'src/libei-socket.c', + 'src/libei-fd.c', proto_headers, ] diff --git a/src/libei-fd.c b/src/libei-fd.c new file mode 100644 index 0000000..7d2b1cc --- /dev/null +++ b/src/libei-fd.c @@ -0,0 +1,90 @@ +/* + * Copyright © 2020 Red Hat, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include "config.h" + +#include +#include +#include +#include +#include + + +#include "util-logger.h" +#include "util-mem.h" +#include "util-macros.h" +#include "util-sources.h" +#include "util-strings.h" +#include "util-object.h" + +#include "libei.h" +#include "libei-private.h" + +/** + * This is the simplest backend: the caller prepares a socket and we just + * take that and use it. + */ +struct ei_fd { + struct object object; +}; + +static inline struct ei_fd * +ei_fd(struct ei *ei) +{ + return ei->backend; +} + +static inline void +ei_fd_destroy(struct ei_fd *backend) +{ + /* nothing to do here */ +} + +OBJECT_IMPLEMENT_CREATE(ei_fd); +static +OBJECT_IMPLEMENT_UNREF(ei_fd); + +static void +interface_fd_destroy(struct ei *ei, void *backend) +{ + struct ei_fd *ei_fd = backend; + ei_fd_unref(ei_fd); +} + +static const struct ei_backend_interface interface = { + .destroy = interface_fd_destroy, +}; + +_public_ int +ei_setup_backend_fd(struct ei *ei, int fd) +{ + assert(ei); + assert(!ei->backend); + + struct ei_fd *ei_fd = ei_fd_create(&ei->object); + + ei->backend = ei_fd; + ei->backend_interface = interface; + + return ei_set_connection(ei, fd); +} diff --git a/src/libei.h b/src/libei.h index 9bcac73..32be7b6 100644 --- a/src/libei.h +++ b/src/libei.h @@ -114,6 +114,12 @@ ei_configure_name(struct ei * ei, const char *name); int ei_setup_backend_socket(struct ei *ei, const char *socketpath); +/** + * Initialize the ei context on the given socket + */ +int +ei_setup_backend_fd(struct ei *ei, int fd); + struct ei * ei_ref(struct ei *ctx);