From c78bbd2f731ae44c7d2588b9bb3d19005f192fc6 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Fri, 15 Mar 2019 11:23:40 +0100 Subject: Add helpers for initd socket Signed-off-by: David Oberhollenzer --- lib/init/init_socket_open.c | 46 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 lib/init/init_socket_open.c (limited to 'lib/init/init_socket_open.c') diff --git a/lib/init/init_socket_open.c b/lib/init/init_socket_open.c new file mode 100644 index 0000000..d0cf168 --- /dev/null +++ b/lib/init/init_socket_open.c @@ -0,0 +1,46 @@ +/* SPDX-License-Identifier: ISC */ +#include +#include +#include +#include +#include +#include + +#include "initsock.h" + +int init_socket_open(const char *tmppath) +{ + struct sockaddr_un un; + int fd; + + fd = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0); + if (fd < 0) { + perror("socket"); + return -1; + } + + memset(&un, 0, sizeof(un)); + un.sun_family = AF_UNIX; + + strcpy(un.sun_path, tmppath); + + if (bind(fd, (struct sockaddr *)&un, sizeof(un))) { + fprintf(stderr, "bind: %s: %s", tmppath, strerror(errno)); + close(fd); + unlink(tmppath); + return -1; + } + + memset(&un, 0, sizeof(un)); + un.sun_family = AF_UNIX; + + strcpy(un.sun_path, INIT_SOCK_PATH); + + if (connect(fd, (struct sockaddr *)&un, sizeof(un))) { + perror("connect: " INIT_SOCK_PATH); + close(fd); + return -1; + } + + return fd; +} -- cgit v1.2.3