diff options
Diffstat (limited to 'lib/init')
-rw-r--r-- | lib/init/init_socket_create.c | 34 | ||||
-rw-r--r-- | lib/init/init_socket_open.c | 46 | ||||
-rw-r--r-- | lib/init/init_socket_send_request.c | 28 |
3 files changed, 108 insertions, 0 deletions
diff --git a/lib/init/init_socket_create.c b/lib/init/init_socket_create.c new file mode 100644 index 0000000..a5a0972 --- /dev/null +++ b/lib/init/init_socket_create.c @@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: ISC */ +#include <sys/socket.h> +#include <sys/un.h> +#include <unistd.h> +#include <stdio.h> + +#include "initsock.h" + +int init_socket_create(void) +{ + 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, INIT_SOCK_PATH); + unlink(INIT_SOCK_PATH); + + if (bind(fd, (struct sockaddr *)&un, sizeof(un))) { + perror("bind: " INIT_SOCK_PATH); + close(fd); + unlink(INIT_SOCK_PATH); + return -1; + } + + return fd; +} 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 <sys/socket.h> +#include <sys/un.h> +#include <unistd.h> +#include <string.h> +#include <errno.h> +#include <stdio.h> + +#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; +} diff --git a/lib/init/init_socket_send_request.c b/lib/init/init_socket_send_request.c new file mode 100644 index 0000000..dd88856 --- /dev/null +++ b/lib/init/init_socket_send_request.c @@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: ISC */ +#include <unistd.h> +#include <string.h> +#include <stdio.h> +#include <errno.h> + +#include "initsock.h" + +int init_socket_send_request(int fd, E_INIT_REQUEST rq) +{ + init_request_t request; + ssize_t ret; + + memset(&request, 0, sizeof(request)); + request.rq = rq; + +retry: + ret = write(fd, &request, sizeof(request)); + + if (ret < 0) { + if (errno == EINTR) + goto retry; + perror(INIT_SOCK_PATH); + return -1; + } + + return 0; +} |