From 7cfe6e845878d67f578fa846e784c064a178d9c5 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Thu, 28 Mar 2019 16:33:01 +0100 Subject: cleanup: move init specific stuff of init socket to initd Signed-off-by: David Oberhollenzer --- initd/Makemodule.am | 2 +- initd/init.h | 7 +++ initd/initsock.c | 91 ++++++++++++++++++++++++++++++++++++++ lib/Makemodule.am | 3 +- lib/include/initsock.h | 5 --- lib/init/init_socket_create.c | 35 --------------- lib/init/init_socket_send_status.c | 72 ------------------------------ 7 files changed, 100 insertions(+), 115 deletions(-) create mode 100644 initd/initsock.c delete mode 100644 lib/init/init_socket_create.c delete mode 100644 lib/init/init_socket_send_status.c diff --git a/initd/Makemodule.am b/initd/Makemodule.am index 8552e45..7e7fd37 100644 --- a/initd/Makemodule.am +++ b/initd/Makemodule.am @@ -1,5 +1,5 @@ init_SOURCES = initd/main.c initd/init.h initd/signal_linux.c initd/runsvc.c -init_SOURCES += initd/status.c initd/supervisor.c +init_SOURCES += initd/status.c initd/supervisor.c initd/initsock.c init_CPPFLAGS = $(AM_CPPFLAGS) init_CFLAGS = $(AM_CFLAGS) init_LDFLAGS = $(AM_LDFLAGS) diff --git a/initd/init.h b/initd/init.h index ffc7181..50f799a 100644 --- a/initd/init.h +++ b/initd/init.h @@ -100,4 +100,11 @@ int sigsetup(void); */ void sigreset(void); +/********** initsock.c **********/ + +int init_socket_create(void); + +int init_socket_send_status(int fd, const void *dest_addr, size_t addrlen, + E_SERVICE_STATE state, service_t *svc); + #endif /* INIT_H */ diff --git a/initd/initsock.c b/initd/initsock.c new file mode 100644 index 0000000..718ab61 --- /dev/null +++ b/initd/initsock.c @@ -0,0 +1,91 @@ +/* SPDX-License-Identifier: ISC */ +#include "init.h" + +static int send_retry(int fd, const void *dst, size_t addrlen, + const void *buffer, size_t size) +{ + ssize_t ret; +retry: + ret = sendto(fd, buffer, size, MSG_NOSIGNAL, dst, addrlen); + if (ret < 0 && errno == EINTR) + goto retry; + + if (ret < 0 || (size_t)ret < size) + return -1; + + return 0; +} + +static int send_string(int fd, const void *dst, size_t addrlen, + const char *str) +{ + size_t len = strlen(str); + uint8_t raw_len[2]; + + raw_len[0] = (len >> 8) & 0xFF; + raw_len[1] = len & 0xFF; + + if (send_retry(fd, dst, addrlen, raw_len, 2)) + return -1; + + return len > 0 ? send_retry(fd, dst, addrlen, str, len) : 0; +} + +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; +} + +int init_socket_send_status(int fd, const void *dest_addr, size_t addrlen, + E_SERVICE_STATE state, service_t *svc) +{ + uint8_t info[8]; + + if (svc == NULL || state == ESS_NONE) { + info[0] = ESS_NONE; + info[1] = 0; + info[2] = info[3] = 0; + info[4] = info[5] = info[6] = info[7] = 0xFF; + } else { + info[0] = state; + info[1] = svc->status & 0xFF; + info[2] = info[3] = 0; + info[4] = (svc->id >> 24) & 0xFF; + info[5] = (svc->id >> 16) & 0xFF; + info[6] = (svc->id >> 8) & 0xFF; + info[7] = svc->id & 0xFF; + } + + if (send_retry(fd, dest_addr, addrlen, info, sizeof(info))) + return -1; + + if (svc != NULL && state != ESS_NONE) { + if (send_string(fd, dest_addr, addrlen, svc->fname)) + return -1; + if (send_string(fd, dest_addr, addrlen, svc->name)) + return -1; + } + return 0; +} diff --git a/lib/Makemodule.am b/lib/Makemodule.am index 48c5767..a131463 100644 --- a/lib/Makemodule.am +++ b/lib/Makemodule.am @@ -1,9 +1,8 @@ libinit_a_SOURCES = lib/init/delsvc.c lib/init/svcmap.c lib/init/rdsvc.c libinit_a_SOURCES += lib/init/svcscan.c lib/init/del_svc_list.c libinit_a_SOURCES += lib/init/svc_tsort.c lib/include/service.h -libinit_a_SOURCES += lib/init/init_socket_open.c lib/init/init_socket_create.c +libinit_a_SOURCES += lib/init/init_socket_open.c libinit_a_SOURCES += lib/include/initsock.h lib/init/init_socket_send_request.c -libinit_a_SOURCES += lib/init/init_socket_send_status.c libinit_a_SOURCES += lib/init/init_socket_recv_status.c libinit_a_CPPFLAGS = $(AM_CPPFLAGS) libinit_a_CFLAGS = $(AM_CFLAGS) diff --git a/lib/include/initsock.h b/lib/include/initsock.h index a687481..afbf861 100644 --- a/lib/include/initsock.h +++ b/lib/include/initsock.h @@ -47,15 +47,10 @@ typedef struct { char *service_name; } init_status_response_t; -int init_socket_create(void); - int init_socket_open(const char *tmppath); int init_socket_send_request(int fd, E_INIT_REQUEST rq, ...); -int init_socket_send_status(int fd, const void *dest_addr, size_t addrlen, - E_SERVICE_STATE state, service_t *svc); - int init_socket_recv_status(int fd, init_status_response_t *resp); #endif /* INITSOCK_H */ diff --git a/lib/init/init_socket_create.c b/lib/init/init_socket_create.c deleted file mode 100644 index 82a528c..0000000 --- a/lib/init/init_socket_create.c +++ /dev/null @@ -1,35 +0,0 @@ -/* SPDX-License-Identifier: ISC */ -#include -#include -#include -#include -#include - -#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_send_status.c b/lib/init/init_socket_send_status.c deleted file mode 100644 index 8cd5d8a..0000000 --- a/lib/init/init_socket_send_status.c +++ /dev/null @@ -1,72 +0,0 @@ -/* SPDX-License-Identifier: ISC */ -#include -#include -#include -#include -#include -#include - -#include "initsock.h" - - -static int send_retry(int fd, const void *dst, size_t addrlen, - const void *buffer, size_t size) -{ - ssize_t ret; -retry: - ret = sendto(fd, buffer, size, MSG_NOSIGNAL, dst, addrlen); - if (ret < 0 && errno == EINTR) - goto retry; - - if (ret < 0 || (size_t)ret < size) - return -1; - - return 0; -} - -static int send_string(int fd, const void *dst, size_t addrlen, - const char *str) -{ - size_t len = strlen(str); - uint8_t raw_len[2]; - - raw_len[0] = (len >> 8) & 0xFF; - raw_len[1] = len & 0xFF; - - if (send_retry(fd, dst, addrlen, raw_len, 2)) - return -1; - - return len > 0 ? send_retry(fd, dst, addrlen, str, len) : 0; -} - -int init_socket_send_status(int fd, const void *dest_addr, size_t addrlen, - E_SERVICE_STATE state, service_t *svc) -{ - uint8_t info[8]; - - if (svc == NULL || state == ESS_NONE) { - info[0] = ESS_NONE; - info[1] = 0; - info[2] = info[3] = 0; - info[4] = info[5] = info[6] = info[7] = 0xFF; - } else { - info[0] = state; - info[1] = svc->status & 0xFF; - info[2] = info[3] = 0; - info[4] = (svc->id >> 24) & 0xFF; - info[5] = (svc->id >> 16) & 0xFF; - info[6] = (svc->id >> 8) & 0xFF; - info[7] = svc->id & 0xFF; - } - - if (send_retry(fd, dest_addr, addrlen, info, sizeof(info))) - return -1; - - if (svc != NULL && state != ESS_NONE) { - if (send_string(fd, dest_addr, addrlen, svc->fname)) - return -1; - if (send_string(fd, dest_addr, addrlen, svc->name)) - return -1; - } - return 0; -} -- cgit v1.2.3