From 13aa3840cc94ce37ef1e63c093c0f71ac84e90fd Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Thu, 7 May 2020 22:04:42 +0200 Subject: Temporarily remove initsock handling code Signed-off-by: David Oberhollenzer --- cmd/Makemodule.am | 3 +- cmd/service/startstop.c | 104 ---------------------- cmd/service/status.c | 170 ------------------------------------ initd/Makemodule.am | 2 +- initd/init.h | 15 ---- initd/initsock.c | 88 ------------------- initd/main.c | 51 ----------- initd/supervisor.c | 89 ------------------- lib/Makemodule.am | 3 - lib/include/initsock.h | 65 -------------- lib/init/free_init_status.c | 10 --- lib/init/init_socket_open.c | 46 ---------- lib/init/init_socket_recv_status.c | 87 ------------------ lib/init/init_socket_send_request.c | 45 ---------- 14 files changed, 2 insertions(+), 776 deletions(-) delete mode 100644 cmd/service/startstop.c delete mode 100644 cmd/service/status.c delete mode 100644 initd/initsock.c delete mode 100644 lib/include/initsock.h delete mode 100644 lib/init/free_init_status.c delete mode 100644 lib/init/init_socket_open.c delete mode 100644 lib/init/init_socket_recv_status.c delete mode 100644 lib/init/init_socket_send_request.c diff --git a/cmd/Makemodule.am b/cmd/Makemodule.am index 7b8296f..e1e9964 100644 --- a/cmd/Makemodule.am +++ b/cmd/Makemodule.am @@ -18,8 +18,7 @@ SRVHEADERS = cmd/service/servicecmd.h service_SOURCES = cmd/service/servicecmd.c cmd/service/help.c service_SOURCES += cmd/service/enable.c cmd/service/disable.c service_SOURCES += cmd/service/dumpscript.c cmd/service/list.c -service_SOURCES += cmd/service/status.c cmd/service/loadsvc.c -service_SOURCES += cmd/service/startstop.c +service_SOURCES += cmd/service/loadsvc.c service_SOURCES += $(SRVHEADERS) service_CPPFLAGS = $(AM_CPPFLAGS) service_CFLAGS = $(AM_CFLAGS) diff --git a/cmd/service/startstop.c b/cmd/service/startstop.c deleted file mode 100644 index 8c30efa..0000000 --- a/cmd/service/startstop.c +++ /dev/null @@ -1,104 +0,0 @@ -/* SPDX-License-Identifier: ISC */ -#include "servicecmd.h" -#include "initsock.h" -#include "service.h" -#include "config.h" - -#include -#include -#include - -static int cmd_startstop(int argc, char **argv, - E_SERVICE_STATE filter, E_INIT_REQUEST action) -{ - int i, fd, ret = EXIT_FAILURE; - init_status_t resp; - char tmppath[256]; - bool found; - - if (check_arguments(argv[0], argc, 2, 2)) - return EXIT_FAILURE; - - sprintf(tmppath, "/tmp/svcstatus.%d.sock", (int)getpid()); - fd = init_socket_open(tmppath); - - if (fd < 0) { - unlink(tmppath); - return EXIT_FAILURE; - } - - if (init_socket_send_request(fd, EIR_STATUS, filter)) - goto out; - - for (;;) { - memset(&resp, 0, sizeof(resp)); - - if (init_socket_recv_status(fd, &resp)) { - perror("reading from initd socket"); - free_init_status(&resp); - goto out; - } - - if (resp.state == ESS_NONE) { - free_init_status(&resp); - break; - } - - found = false; - - for (i = optind; i < argc; ++i) { - if (fnmatch(argv[i], resp.service_name, 0) == 0) { - found = true; - break; - } - if (fnmatch(argv[i], resp.filename, 0) == 0) { - found = true; - break; - } - } - - if (found) { - if (init_socket_send_request(fd, action, resp.id)) - goto out; - } - - free_init_status(&resp); - } - - ret = EXIT_SUCCESS; -out: - close(fd); - unlink(tmppath); - return ret; -} - -static int cmd_start(int argc, char **argv) -{ - return cmd_startstop(argc, argv, ESS_NONE, EIR_START); -} - -static int cmd_stop(int argc, char **argv) -{ - return cmd_startstop(argc, argv, ESS_NONE, EIR_STOP); -} - -static command_t start = { - .cmd = "start", - .usage = "services...", - .s_desc = "start a currently not running service", - .l_desc = "Start one or more service that are currently not running. " - "Shell style globbing patterns can used for service names.", - .run_cmd = cmd_start, -}; - -static command_t stop = { - .cmd = "stop", - .usage = "services...", - .s_desc = "stop a currently running service", - .l_desc = "Stop one or more service that are currently running. " - "Shell style globbing patterns can used for service names.", - .run_cmd = cmd_stop, -}; - -REGISTER_COMMAND(start) -REGISTER_COMMAND(stop) diff --git a/cmd/service/status.c b/cmd/service/status.c deleted file mode 100644 index a13a450..0000000 --- a/cmd/service/status.c +++ /dev/null @@ -1,170 +0,0 @@ -/* SPDX-License-Identifier: ISC */ -#include "servicecmd.h" -#include "initsock.h" -#include "service.h" -#include "config.h" - -#include -#include -#include - -static const struct option long_opts[] = { - { "detail", no_argument, NULL, 'd' }, - { NULL, 0, NULL, 0 }, -}; - -static const char *short_opts = "d"; - -static int cmd_status(int argc, char **argv) -{ - bool is_tty, found, show_details = false; - int i, fd, ret = EXIT_FAILURE; - init_status_t resp; - char tmppath[256]; - const char *state; - service_t *svc; - - for (;;) { - i = getopt_long(argc, argv, short_opts, long_opts, NULL); - if (i == -1) - break; - - switch (i) { - case 'd': - show_details = true; - break; - default: - tell_read_help(argv[0]); - return EXIT_FAILURE; - } - } - - sprintf(tmppath, "/tmp/svcstatus.%d.sock", (int)getpid()); - fd = init_socket_open(tmppath); - - if (fd < 0) { - unlink(tmppath); - return EXIT_FAILURE; - } - - if (init_socket_send_request(fd, EIR_STATUS, ESS_NONE)) - goto out; - - is_tty = (isatty(STDOUT_FILENO) == 1); - - for (;;) { - memset(&resp, 0, sizeof(resp)); - - if (init_socket_recv_status(fd, &resp)) { - perror("reading from initd socket"); - free_init_status(&resp); - goto out; - } - - if (resp.state == ESS_NONE) { - free_init_status(&resp); - break; - } - - if (optind < argc) { - found = false; - - for (i = optind; i < argc; ++i) { - if (fnmatch(argv[i], - resp.service_name, 0) == 0) { - found = true; - break; - } - if (fnmatch(argv[i], resp.filename, 0) == 0) { - found = true; - break; - } - } - - if (!found) { - free_init_status(&resp); - continue; - } - } - - switch (resp.state) { - case ESS_RUNNING: - if (!is_tty) { - state = "UP"; - break; - } - state = "\033[22;32m UP \033[0m"; - break; - case ESS_ENQUEUED: - state = "SCHED"; - break; - case ESS_FAILED: - if (!is_tty) { - state = "FAIL"; - break; - } - state = "\033[22;31mFAIL\033[0m"; - break; - case ESS_DONE: - if (!is_tty) { - state = "DONE"; - break; - } - - state = "\033[22;33mDONE\033[0m"; - break; - default: - if (!is_tty) { - state = "UNKNOWN"; - break; - } - state = "\033[22;31mUNKNOWN\033[0m"; - break; - } - - if (show_details) { - printf("Service: %s\n", resp.filename); - printf("\tStatus: %s\n", state); - printf("\tTemplate name: %s\n", resp.service_name); - printf("\tExit status: %d\n", resp.exit_status); - - svc = loadsvc(SVCDIR, resp.filename); - - if (svc == NULL) { - fputs("\tError loading service file\n", stdout); - } else { - printf("\tDescription: %s\n", svc->desc); - printf("\tType: %s\n", - svc_type_to_string(svc->type)); - printf("\tTarget: %s\n", - svc_target_to_string(svc->target)); - delsvc(svc); - } - } else { - printf("[%s] %s\n", state, resp.filename); - } - - free_init_status(&resp); - } - - ret = EXIT_SUCCESS; -out: - close(fd); - unlink(tmppath); - return ret; -} - -static command_t status = { - .cmd = "status", - .usage = "[-d|--detail] [services...]", - .s_desc = "report the status of the currently enabled services", - .l_desc = "Gathers a list of all currently running services and the " - "state that they are in (currently running, done, failed, " - "wating to get scheduled). A list of services with wildcard " - "globbing patterns can be specified. If ommitted, produces " - "a general overview of all services. If the --detail " - "is given, more details are shown about a service.", - .run_cmd = cmd_status, -}; - -REGISTER_COMMAND(status) diff --git a/initd/Makemodule.am b/initd/Makemodule.am index c68e098..9491fa9 100644 --- a/initd/Makemodule.am +++ b/initd/Makemodule.am @@ -1,5 +1,5 @@ init_SOURCES = initd/main.c initd/init.h initd/runsvc.c -init_SOURCES += initd/status.c initd/supervisor.c initd/initsock.c +init_SOURCES += initd/status.c initd/supervisor.c init_CPPFLAGS = $(AM_CPPFLAGS) init_CFLAGS = $(AM_CFLAGS) init_LDFLAGS = $(AM_LDFLAGS) diff --git a/initd/init.h b/initd/init.h index 0196861..67d5fc4 100644 --- a/initd/init.h +++ b/initd/init.h @@ -20,7 +20,6 @@ #include #include -#include "initsock.h" #include "service.h" #include "config.h" @@ -70,18 +69,4 @@ void supervisor_init(void); bool supervisor_process_queues(void); -void supervisor_answer_status_request(int fd, const void *dest_addr, - size_t addrlen, E_SERVICE_STATE filter); - -void supervisor_start(int id); - -void supervisor_stop(int id); - -/********** 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 deleted file mode 100644 index 7016ef1..0000000 --- a/initd/initsock.c +++ /dev/null @@ -1,88 +0,0 @@ -/* 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); - uint16_t raw; - - if (len > 0xFFFF) - return -1; - - raw = htobe16(len); - if (send_retry(fd, dst, addrlen, &raw, 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) -{ - init_response_status_t info; - - memset(&info, 0, sizeof(info)); - - if (svc == NULL || state == ESS_NONE) { - info.state = ESS_NONE; - info.id = -1; - } else { - info.state = state; - info.exit_status = svc->status & 0xFF; - info.id = htobe32(svc->id); - } - - 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/initd/main.c b/initd/main.c index e59d809..5a4c37a 100644 --- a/initd/main.c +++ b/initd/main.c @@ -2,7 +2,6 @@ #include "init.h" static int sigfd = -1; -static int sockfd = -1; static void handle_signal(void) { @@ -33,46 +32,6 @@ static void handle_signal(void) case SIGHUP: break; case SIGUSR1: - if (sockfd >= 0) { - close(sockfd); - unlink(INIT_SOCK_PATH); - sockfd = -1; - } - sockfd = init_socket_create(); - break; - } -} - -static void handle_request(void) -{ - struct sockaddr_un addr; - init_request_t rq; - socklen_t addrlen; - ssize_t ret; -retry: - memset(&rq, 0, sizeof(rq)); - addrlen = sizeof(addr); - ret = recvfrom(sockfd, &rq, sizeof(rq), MSG_DONTWAIT | MSG_TRUNC, - (struct sockaddr *)&addr, &addrlen); - - if (ret < 0 && errno == EINTR) - goto retry; - - if ((size_t)ret < sizeof(rq)) - return; - - switch (rq.rq) { - case EIR_STATUS: - supervisor_answer_status_request(sockfd, &addr, addrlen, - rq.arg.status.filter); - break; - case EIR_START: - rq.arg.startstop.id = be32toh(rq.arg.startstop.id); - supervisor_start(rq.arg.startstop.id); - break; - case EIR_STOP: - rq.arg.startstop.id = be32toh(rq.arg.startstop.id); - supervisor_stop(rq.arg.startstop.id); break; } } @@ -81,8 +40,6 @@ void target_completed(int target) { switch (target) { case TGT_BOOT: - if (sockfd < 0) - sockfd = init_socket_create(); break; case TGT_SHUTDOWN: for (;;) @@ -145,12 +102,6 @@ int main(void) pfd[count].events = POLLIN; ++count; - if (sockfd >= 0) { - pfd[count].fd = sockfd; - pfd[count].events = POLLIN; - ++count; - } - ret = poll(pfd, count, -1); if (ret <= 0) continue; @@ -159,8 +110,6 @@ int main(void) if (pfd[i].revents & POLLIN) { if (pfd[i].fd == sigfd) handle_signal(); - if (pfd[i].fd == sockfd) - handle_request(); } } } diff --git a/initd/supervisor.c b/initd/supervisor.c index 8c33bf3..32a23f2 100644 --- a/initd/supervisor.c +++ b/initd/supervisor.c @@ -191,92 +191,3 @@ out: target_completed(target); return true; } - -static int send_svc_list(int fd, const void *dst, size_t addrlen, - E_SERVICE_STATE filter, E_SERVICE_STATE state, - service_t *list) -{ - if (filter != ESS_NONE && filter != state) - return 0; - - while (list != NULL) { - if (init_socket_send_status(fd, dst, addrlen, state, list)) - return -1; - - list = list->next; - } - - return 0; -} - -void supervisor_answer_status_request(int fd, const void *dst, size_t addrlen, - E_SERVICE_STATE filter) -{ - if (send_svc_list(fd, dst, addrlen, filter, ESS_RUNNING, running)) - return; - if (send_svc_list(fd, dst, addrlen, filter, ESS_DONE, completed)) - return; - if (send_svc_list(fd, dst, addrlen, filter, ESS_FAILED, failed)) - return; - if (send_svc_list(fd, dst, addrlen, filter, ESS_ENQUEUED, queue)) - return; - if (send_svc_list(fd, dst, addrlen, filter, ESS_ENQUEUED, terminated)) - return; - init_socket_send_status(fd, dst, addrlen, ESS_NONE, NULL); -} - -static service_t *remove_by_id(service_t **list, int id) -{ - service_t *svc = *list, *prev = NULL; - - while (svc != NULL && svc->id != id) { - prev = svc; - svc = svc->next; - } - - if (svc != NULL) { - if (prev == NULL) { - *list = svc->next; - } else { - prev->next = svc->next; - } - } - - return svc; -} - -void supervisor_start(int id) -{ - service_t *svc; - - svc = remove_by_id(&completed, id); - if (svc != NULL) - goto found; - - svc = remove_by_id(&failed, id); - if (svc != NULL) - goto found; - - return; -found: - svc->rspwn_count = 0; - svc->flags &= ~SVC_FLAG_ADMIN_STOPPED; - svc->next = queue; - queue = svc; -} - -void supervisor_stop(int id) -{ - service_t *svc; - - for (svc = running; svc != NULL; svc = svc->next) { - if (svc->id == id) - break; - } - - if (svc != NULL) { - /* TODO: something more sophisticated? */ - svc->flags |= SVC_FLAG_ADMIN_STOPPED; - kill(svc->pid, SIGTERM); - } -} diff --git a/lib/Makemodule.am b/lib/Makemodule.am index d8b94c8..fa8789b 100644 --- a/lib/Makemodule.am +++ b/lib/Makemodule.am @@ -1,9 +1,6 @@ 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/free_init_status.c -libinit_a_SOURCES += lib/include/initsock.h lib/init/init_socket_send_request.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 deleted file mode 100644 index ef8b9e3..0000000 --- a/lib/include/initsock.h +++ /dev/null @@ -1,65 +0,0 @@ -/* SPDX-License-Identifier: ISC */ -#ifndef INITSOCK_H -#define INITSOCK_H - -#include - -#include "config.h" -#include "service.h" - -#define INIT_SOCK_PATH SOCKDIR "/init.sock" - -typedef enum { - EIR_STATUS = 0x00, - EIR_START = 0x01, - EIR_STOP = 0x02, -} E_INIT_REQUEST; - -typedef enum { - ESS_NONE = 0x00, - ESS_RUNNING = 0x01, - ESS_ENQUEUED = 0x02, - ESS_DONE = 0x03, - ESS_FAILED = 0x04 -} E_SERVICE_STATE; - -typedef struct { - uint8_t rq; - uint8_t padd[3]; - - union { - struct { - uint8_t filter; - uint8_t padd[3]; - } status; - - struct { - uint32_t id; - } startstop; - } arg; -} init_request_t; - -typedef struct { - uint8_t state; - uint8_t exit_status; - uint8_t padd[2]; - int32_t id; -} init_response_status_t; - -typedef struct { - E_SERVICE_STATE state; - int exit_status; - int id; - char *filename; - char *service_name; -} init_status_t; - -int init_socket_open(const char *tmppath); - -int init_socket_send_request(int fd, E_INIT_REQUEST rq, ...); - -int init_socket_recv_status(int fd, init_status_t *resp); - -void free_init_status(init_status_t *resp); - -#endif /* INITSOCK_H */ diff --git a/lib/init/free_init_status.c b/lib/init/free_init_status.c deleted file mode 100644 index 945d407..0000000 --- a/lib/init/free_init_status.c +++ /dev/null @@ -1,10 +0,0 @@ -/* SPDX-License-Identifier: ISC */ -#include - -#include "initsock.h" - -void free_init_status(init_status_t *resp) -{ - free(resp->filename); - free(resp->service_name); -} diff --git a/lib/init/init_socket_open.c b/lib/init/init_socket_open.c deleted file mode 100644 index d0cf168..0000000 --- a/lib/init/init_socket_open.c +++ /dev/null @@ -1,46 +0,0 @@ -/* 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; -} diff --git a/lib/init/init_socket_recv_status.c b/lib/init/init_socket_recv_status.c deleted file mode 100644 index 996f8de..0000000 --- a/lib/init/init_socket_recv_status.c +++ /dev/null @@ -1,87 +0,0 @@ -/* SPDX-License-Identifier: ISC */ -#include -#include -#include -#include -#include -#include -#include - -#include "initsock.h" - -static int read_retry(int fd, void *buffer, size_t size) -{ - ssize_t ret; -retry: - ret = read(fd, buffer, size); - - if (ret < 0) { - if (errno == EINTR) - goto retry; - return -1; - } - - if ((size_t)ret < size) { - errno = EPROTO; - return 0; - } - - return 1; -} - -static char *read_string(int fd) -{ - uint16_t len; - char *buffer; - int ret; - - ret = read_retry(fd, &len, sizeof(len)); - if (ret <= 0) - return NULL; - - len = be16toh(len); - - buffer = calloc(1, len + 1); - if (buffer == NULL) - return NULL; - - if (len > 0) { - ret = read_retry(fd, buffer, len); - - if (ret <= 0) { - ret = errno; - free(buffer); - errno = ret; - return NULL; - } - } - - return buffer; -} - -int init_socket_recv_status(int fd, init_status_t *resp) -{ - init_response_status_t info; - - memset(resp, 0, sizeof(*resp)); - - if (read_retry(fd, &info, sizeof(info)) <= 0) - return -1; - - resp->state = info.state; - resp->exit_status = info.exit_status; - resp->id = be32toh(info.id); - - if (resp->state == ESS_NONE) - return 0; - - resp->filename = read_string(fd); - if (resp->filename == NULL) - return -1; - - resp->service_name = read_string(fd); - if (resp->service_name == NULL) - return -1; - - return 0; -} diff --git a/lib/init/init_socket_send_request.c b/lib/init/init_socket_send_request.c deleted file mode 100644 index 53b31f6..0000000 --- a/lib/init/init_socket_send_request.c +++ /dev/null @@ -1,45 +0,0 @@ -/* SPDX-License-Identifier: ISC */ -#include -#include -#include -#include -#include -#include - -#include "initsock.h" - -int init_socket_send_request(int fd, E_INIT_REQUEST rq, ...) -{ - init_request_t request; - ssize_t ret; - va_list ap; - - memset(&request, 0, sizeof(request)); - request.rq = rq; - - va_start(ap, rq); - switch (rq) { - case EIR_STATUS: - request.arg.status.filter = va_arg(ap, E_SERVICE_STATE); - break; - case EIR_START: - case EIR_STOP: - request.arg.startstop.id = htobe32(va_arg(ap, int)); - break; - default: - break; - } - va_end(ap); - -retry: - ret = write(fd, &request, sizeof(request)); - - if (ret < 0) { - if (errno == EINTR) - goto retry; - perror(INIT_SOCK_PATH); - return -1; - } - - return 0; -} -- cgit v1.2.3