From 8f3d186549465c1bd062e1478be700b5a43eda6d Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 17 Feb 2023 09:25:21 +0900 Subject: [PATCH 1/4] activate: use DEFINE_MAIN_FUNC() macro --- src/activate/activate.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/activate/activate.c b/src/activate/activate.c index 16463a9b93..ef1289f0e3 100644 --- a/src/activate/activate.c +++ b/src/activate/activate.c @@ -16,6 +16,7 @@ #include "fd-util.h" #include "log.h" #include "macro.h" +#include "main-func.h" #include "pretty-print.h" #include "process-util.h" #include "signal-util.h" @@ -432,7 +433,7 @@ static int parse_argv(int argc, char *argv[]) { return 1 /* work to do */; } -int main(int argc, char **argv) { +static int run(int argc, char **argv) { int r, n; int epoll_fd = -EBADF; @@ -442,19 +443,17 @@ int main(int argc, char **argv) { r = parse_argv(argc, argv); if (r <= 0) - return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE; + return r; r = install_chld_handler(); if (r < 0) - return EXIT_FAILURE; + return r; n = open_sockets(&epoll_fd, arg_accept); if (n < 0) - return EXIT_FAILURE; - if (n == 0) { - log_error("No sockets to listen on specified or passed in."); - return EXIT_FAILURE; - } + return n; + if (n == 0) + return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "No sockets to listen on specified or passed in."); for (;;) { struct epoll_event event; @@ -463,20 +462,19 @@ int main(int argc, char **argv) { if (errno == EINTR) continue; - log_error_errno(errno, "epoll_wait() failed: %m"); - return EXIT_FAILURE; + return log_error_errno(errno, "epoll_wait() failed: %m"); } log_info("Communication attempt on fd %i.", event.data.fd); if (arg_accept) { r = do_accept(argv[optind], argv + optind, event.data.fd); if (r < 0) - return EXIT_FAILURE; + return r; } else break; } - exec_process(argv[optind], argv + optind, SD_LISTEN_FDS_START, (size_t) n); - - return EXIT_SUCCESS; + return exec_process(argv[optind], argv + optind, SD_LISTEN_FDS_START, (size_t) n); } + +DEFINE_MAIN_FUNCTION(run); From a601e702d5a857d6645c15e9c476df296ba537e8 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 17 Feb 2023 09:26:34 +0900 Subject: [PATCH 2/4] activate: fix typo --- src/activate/activate.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/activate/activate.c b/src/activate/activate.c index ef1289f0e3..66429c8ec9 100644 --- a/src/activate/activate.c +++ b/src/activate/activate.c @@ -82,10 +82,8 @@ static int open_sockets(int *epoll_fd, bool accept) { return log_error_errno(r, "Failed to close all file descriptors: %m"); } - /** Note: we leak some fd's on error here. I doesn't matter - * much, since the program will exit immediately anyway, but - * would be a pain to fix. - */ + /* Note: we leak some fd's on error here. It doesn't matter much, since the program will exit + * immediately anyway, but would be a pain to fix. */ STRV_FOREACH(address, arg_listen) { r = make_socket_fd(LOG_DEBUG, *address, arg_socket_type, (arg_accept * SOCK_CLOEXEC)); From a723521fd26d40ce90357e4e9b8131f1e1656ab5 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 17 Feb 2023 09:31:01 +0900 Subject: [PATCH 3/4] activate: use log_set_open_when_needed() Otherwise, several error logs may not be shown. --- src/activate/activate.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/activate/activate.c b/src/activate/activate.c index 66429c8ec9..6d9a4c41cc 100644 --- a/src/activate/activate.c +++ b/src/activate/activate.c @@ -77,6 +77,8 @@ static int open_sockets(int *epoll_fd, bool accept) { except[i] = SD_LISTEN_FDS_START + i; log_close(); + log_set_open_when_needed(true); + r = close_all_fds(except, n); if (r < 0) return log_error_errno(r, "Failed to close all file descriptors: %m"); @@ -87,17 +89,17 @@ static int open_sockets(int *epoll_fd, bool accept) { STRV_FOREACH(address, arg_listen) { r = make_socket_fd(LOG_DEBUG, *address, arg_socket_type, (arg_accept * SOCK_CLOEXEC)); - if (r < 0) { - log_open(); + if (r < 0) return log_error_errno(r, "Failed to open '%s': %m", *address); - } assert(r == SD_LISTEN_FDS_START + count); count++; } - if (arg_listen) + if (arg_listen) { log_open(); + log_set_open_when_needed(false); + } *epoll_fd = epoll_create1(EPOLL_CLOEXEC); if (*epoll_fd < 0) From 9c9e1ceecc10e9894e0105e4c526b532a0059a0c Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 17 Feb 2023 09:49:01 +0900 Subject: [PATCH 4/4] activate: use _cleanup_close_ attribute --- src/activate/activate.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/activate/activate.c b/src/activate/activate.c index 6d9a4c41cc..ea141c54bd 100644 --- a/src/activate/activate.c +++ b/src/activate/activate.c @@ -49,9 +49,12 @@ static int add_epoll(int epoll_fd, int fd) { return 0; } -static int open_sockets(int *epoll_fd, bool accept) { +static int open_sockets(int *ret_epoll_fd, bool accept) { + _cleanup_close_ int epoll_fd = -EBADF; int n, r, count = 0; + assert(ret_epoll_fd); + n = sd_listen_fds(true); if (n < 0) return log_error_errno(n, "Failed to read listening file descriptors from environment: %m"); @@ -101,8 +104,8 @@ static int open_sockets(int *epoll_fd, bool accept) { log_set_open_when_needed(false); } - *epoll_fd = epoll_create1(EPOLL_CLOEXEC); - if (*epoll_fd < 0) + epoll_fd = epoll_create1(EPOLL_CLOEXEC); + if (epoll_fd < 0) return log_error_errno(errno, "Failed to create epoll object: %m"); for (int fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + count; fd++) { @@ -111,11 +114,12 @@ static int open_sockets(int *epoll_fd, bool accept) { getsockname_pretty(fd, &name); log_info("Listening on %s as %i.", strna(name), fd); - r = add_epoll(*epoll_fd, fd); + r = add_epoll(epoll_fd, fd); if (r < 0) return r; } + *ret_epoll_fd = TAKE_FD(epoll_fd); return count; } @@ -434,8 +438,8 @@ static int parse_argv(int argc, char *argv[]) { } static int run(int argc, char **argv) { + _cleanup_close_ int epoll_fd = -EBADF; int r, n; - int epoll_fd = -EBADF; log_show_color(true); log_parse_environment();