From 96c374d0a536286e18cad64d1b5ebb8f07c90334 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 18 Oct 2013 02:40:26 +0200 Subject: [PATCH] socket-proxyd: rename from saproxy The thing is a daemon, hence needs a "d" prefix. Also, we tend to not abbreviate names of background components unnecessarily, since they are not primary commands people type. Then, the fact that this thing does socket actviation is mostly in implementationd detail for the proxy. Also, do some minor indenting clean-ups and other code updates. --- .gitignore | 2 +- Makefile-man.am | 2 +- Makefile.am | 15 ++-- ...-saproxy.xml => systemd-socket-proxyd.xml} | 26 +++--- src/{saproxy => socket-proxy}/Makefile | 0 .../socket-proxyd.c} | 87 +++++++++---------- 6 files changed, 60 insertions(+), 72 deletions(-) rename man/{systemd-saproxy.xml => systemd-socket-proxyd.xml} (92%) rename src/{saproxy => socket-proxy}/Makefile (100%) rename src/{saproxy/saproxy.c => socket-proxy/socket-proxyd.c} (91%) diff --git a/.gitignore b/.gitignore index 22485b42d..f5acfce0a 100644 --- a/.gitignore +++ b/.gitignore @@ -71,7 +71,7 @@ /systemd-reply-password /systemd-rfkill /systemd-run -/systemd-saproxy +/systemd-socket-proxyd /systemd-shutdown /systemd-shutdownd /systemd-sleep diff --git a/Makefile-man.am b/Makefile-man.am index 6b286bc48..54798450f 100644 --- a/Makefile-man.am +++ b/Makefile-man.am @@ -66,7 +66,7 @@ MANPAGES += \ man/systemd-nspawn.1 \ man/systemd-remount-fs.service.8 \ man/systemd-run.1 \ - man/systemd-saproxy.1 \ + man/systemd-socket-proxyd.1 \ man/systemd-shutdownd.service.8 \ man/systemd-sleep.conf.5 \ man/systemd-suspend.service.8 \ diff --git a/Makefile.am b/Makefile.am index 4a399c523..05f6f9029 100644 --- a/Makefile.am +++ b/Makefile.am @@ -299,8 +299,7 @@ bin_PROGRAMS = \ systemd-detect-virt \ systemd-delta \ systemd-analyze \ - systemd-run \ - systemd-saproxy + systemd-run dist_bin_SCRIPTS = \ src/kernel-install/kernel-install @@ -321,7 +320,8 @@ rootlibexec_PROGRAMS = \ systemd-fsck \ systemd-ac-power \ systemd-sysctl \ - systemd-sleep + systemd-sleep \ + systemd-socket-proxyd systemgenerator_PROGRAMS = \ systemd-getty-generator \ @@ -3167,10 +3167,10 @@ EXTRA_DIST += \ # ------------------------------------------------------------------------------ -systemd_saproxy_SOURCES = \ - src/saproxy/saproxy.c +systemd_socket_proxyd_SOURCES = \ + src/socket-proxy/socket-proxyd.c -systemd_saproxy_LDADD = \ +systemd_socket_proxyd_LDADD = \ libsystemd-shared.la \ libsystemd-logs.la \ libsystemd-journal-internal.la \ @@ -3178,9 +3178,6 @@ systemd_saproxy_LDADD = \ libsystemd-daemon.la \ libsystemd-bus.la -systemd_saproxy_CFLAGS = \ - $(AM_CFLAGS) - # ------------------------------------------------------------------------------ if ENABLE_COREDUMP systemd_coredump_SOURCES = \ diff --git a/man/systemd-saproxy.xml b/man/systemd-socket-proxyd.xml similarity index 92% rename from man/systemd-saproxy.xml rename to man/systemd-socket-proxyd.xml index ff8218c1e..90b3b1c3a 100644 --- a/man/systemd-saproxy.xml +++ b/man/systemd-socket-proxyd.xml @@ -20,9 +20,9 @@ You should have received a copy of the GNU Lesser General Public License along with systemd; If not, see . --> - + - systemd-saproxy + systemd-socket-proxyd systemd @@ -34,23 +34,23 @@ - systemd-saproxy + systemd-socket-proxyd 1 - systemd-saproxy + systemd-socket-proxyd Inherit a socket. Bidirectionally proxy. - systemd-saproxy + systemd-socket-proxyd OPTIONS HOSTNAME-OR-IP PORT-OR-SERVICE - systemd-saproxy + systemd-socket-proxyd OPTIONS UNIX-DOMAIN-SOCKET-PATH @@ -59,7 +59,7 @@ Description - systemd-saproxy provides a proxy + systemd-socket-proxyd provides a proxy to socket-activate services that do not yet support native socket activation. On behalf of the daemon, the proxy inherits the socket from systemd, accepts @@ -68,7 +68,7 @@ data between the two. This utility's behavior is similar to socat1 . - The main differences for systemd-saproxy + The main differences for systemd-socket-proxyd are support for socket activation with Accept=false and an event-driven design that scales better with the number of @@ -102,7 +102,7 @@ invoked indirectly, for example with a shell script rather than with - + @@ -137,7 +137,7 @@ After=nginx.service Requires=nginx.service [Service] -ExecStart=/usr/bin/systemd-saproxy /tmp/nginx.sock +ExecStart=/usr/bin/systemd-socket-proxyd /tmp/nginx.sock PrivateTmp=true PrivateNetwork=true]]> @@ -190,14 +190,14 @@ After=syslog.target remote-fs.target nss-lookup.target [Service] ExecStartPre=/usr/sbin/nginx -t -ExecStart=/usr/bin/saproxy-nginx.sh +ExecStart=/usr/bin/socket-proxyd-nginx.sh PrivateTmp=true PrivateNetwork=true]]> - /usr/bin/saproxy-nginx.sh + /usr/bin/socket-proxyd-nginx.sh +/usr/bin/systemd-socket-proxyd --ignore-env localhost 8080]]> diff --git a/src/saproxy/Makefile b/src/socket-proxy/Makefile similarity index 100% rename from src/saproxy/Makefile rename to src/socket-proxy/Makefile diff --git a/src/saproxy/saproxy.c b/src/socket-proxy/socket-proxyd.c similarity index 91% rename from src/saproxy/saproxy.c rename to src/socket-proxy/socket-proxyd.c index 6504d0925..59216038d 100644 --- a/src/saproxy/saproxy.c +++ b/src/socket-proxy/socket-proxyd.c @@ -31,11 +31,12 @@ #include #include -#include "log.h" #include "sd-daemon.h" #include "sd-event.h" +#include "log.h" #include "socket-util.h" #include "util.h" +#include "event-util.h" #define BUFFER_SIZE 16384 #define _cleanup_freeaddrinfo_ _cleanup_(freeaddrinfop) @@ -65,7 +66,7 @@ struct connection { static void free_connection(struct connection *c) { log_debug("Freeing fd=%d (conn %p).", c->fd, c); sd_event_source_unref(c->w); - close(c->fd); + close_nointr_nofail(c->fd); free(c); } @@ -354,13 +355,13 @@ static int accept_cb(sd_event_source *s, int fd, uint32_t revents, void *userdat assert(revents & EPOLLIN); - c_server_to_client = malloc0(sizeof(struct connection)); + c_server_to_client = new0(struct connection, 1); if (c_server_to_client == NULL) { log_oom(); goto fail; } - c_client_to_server = malloc0(sizeof(struct connection)); + c_client_to_server = new0(struct connection, 1); if (c_client_to_server == NULL) { log_oom(); goto fail; @@ -372,19 +373,12 @@ static int accept_cb(sd_event_source *s, int fd, uint32_t revents, void *userdat goto fail; } - c_client_to_server->fd = accept(fd, (struct sockaddr *) &sa, &salen); + c_client_to_server->fd = accept4(fd, (struct sockaddr *) &sa, &salen, SOCK_NONBLOCK|SOCK_CLOEXEC); if (c_client_to_server->fd < 0) { log_error("Error accepting client connection."); goto fail; } - /* Unlike on BSD, client sockets do not inherit nonblocking status - * from the listening socket. */ - r = fd_nonblock(c_client_to_server->fd, true); - if (r < 0) { - log_error("Error %d marking client connection as nonblocking: %s", r, strerror(-r)); - goto fail; - } if (sa.sa.sa_family == AF_INET || sa.sa.sa_family == AF_INET6) { char sa_str[INET6_ADDRSTRLEN]; @@ -424,35 +418,37 @@ fail: finish: /* Preserve the main loop even if a single proxy setup fails. */ - return 0; + return 1; } static int run_main_loop(struct proxy *proxy) { + _cleanup_event_source_unref_ sd_event_source *w_accept = NULL; + _cleanup_event_unref_ sd_event *e = NULL; int r = EXIT_SUCCESS; - struct sd_event *e = NULL; - sd_event_source *w_accept = NULL; r = sd_event_new(&e); - if (r < 0) - goto finish; + if (r < 0) { + log_error("Failed to allocate event loop: %s", strerror(-r)); + return r; + } r = fd_nonblock(proxy->listen_fd, true); - if (r < 0) - goto finish; + if (r < 0) { + log_error("Failed to make listen file descriptor non-blocking: %s", strerror(-r)); + return r; + } log_debug("Initializing main listener fd=%d", proxy->listen_fd); - sd_event_add_io(e, proxy->listen_fd, EPOLLIN, accept_cb, proxy, &w_accept); + r = sd_event_add_io(e, proxy->listen_fd, EPOLLIN, accept_cb, proxy, &w_accept); + if (r < 0) { + log_error("Failed to add event IO source: %s", strerror(-r)); + return r; + } log_debug("Initialized main listener. Entering loop."); - sd_event_loop(e); - -finish: - sd_event_source_unref(w_accept); - sd_event_unref(e); - - return r; + return sd_event_loop(e); } static int help(void) { @@ -470,7 +466,7 @@ static int help(void) { } static void version(void) { - puts(PACKAGE_STRING " saproxy"); + puts(PACKAGE_STRING " socket-proxyd"); } static int parse_argv(int argc, char *argv[], struct proxy *p) { @@ -559,21 +555,21 @@ int main(int argc, char *argv[]) { p.listen_fd = SD_LISTEN_FDS_START; if (!p.ignore_env) { - int n; - n = sd_listen_fds(1); - if (n == 0) { - log_error("Found zero inheritable sockets. Are you sure this is running as a socket-activated service?"); - r = EXIT_FAILURE; - goto finish; - } else if (n < 0) { - log_error("Error %d while finding inheritable sockets: %s", n, strerror(-n)); - r = EXIT_FAILURE; - goto finish; - } else if (n > 1) { - log_error("Can't listen on more than one socket."); - r = EXIT_FAILURE; - goto finish; - } + int n; + n = sd_listen_fds(1); + if (n == 0) { + log_error("Found zero inheritable sockets. Are you sure this is running as a socket-activated service?"); + r = EXIT_FAILURE; + goto finish; + } else if (n < 0) { + log_error("Error %d while finding inheritable sockets: %s", n, strerror(-n)); + r = EXIT_FAILURE; + goto finish; + } else if (n > 1) { + log_error("Can't listen on more than one socket."); + r = EXIT_FAILURE; + goto finish; + } } /* @TODO: Check if this proxy can work with datagram sockets. */ @@ -586,12 +582,7 @@ int main(int argc, char *argv[]) { log_info("Starting the socket activation proxy with listener fd=%d.", p.listen_fd); r = run_main_loop(&p); - if (r < 0) { - log_error("Error %d from main loop.", r); - goto finish; - } finish: - log_close(); return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; } -- 2.30.2