chiark / gitweb /
socket-proxyd: rename from saproxy
authorLennart Poettering <lennart@poettering.net>
Fri, 18 Oct 2013 00:40:26 +0000 (02:40 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 18 Oct 2013 00:58:47 +0000 (02:58 +0200)
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
Makefile-man.am
Makefile.am
man/systemd-socket-proxyd.xml [moved from man/systemd-saproxy.xml with 92% similarity]
src/socket-proxy/Makefile [moved from src/saproxy/Makefile with 100% similarity]
src/socket-proxy/socket-proxyd.c [moved from src/saproxy/saproxy.c with 91% similarity]

index 22485b4..f5acfce 100644 (file)
@@ -71,7 +71,7 @@
 /systemd-reply-password
 /systemd-rfkill
 /systemd-run
-/systemd-saproxy
+/systemd-socket-proxyd
 /systemd-shutdown
 /systemd-shutdownd
 /systemd-sleep
index 6b286bc..5479845 100644 (file)
@@ -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 \
index 4a399c5..05f6f90 100644 (file)
@@ -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 = \
similarity index 92%
rename from man/systemd-saproxy.xml
rename to man/systemd-socket-proxyd.xml
index ff8218c..90b3b1c 100644 (file)
@@ -20,9 +20,9 @@
   You should have received a copy of the GNU Lesser General Public License
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 -->
-<refentry id="systemd-saproxy">
+<refentry id="systemd-socket-proxyd">
         <refentryinfo>
-                <title>systemd-saproxy</title>
+                <title>systemd-socket-proxyd</title>
                 <productname>systemd</productname>
                 <authorgroup>
                         <author>
                 </authorgroup>
         </refentryinfo>
         <refmeta>
-                <refentrytitle>systemd-saproxy</refentrytitle>
+                <refentrytitle>systemd-socket-proxyd</refentrytitle>
                 <manvolnum>1</manvolnum>
         </refmeta>
         <refnamediv>
-                <refname>systemd-saproxy</refname>
+                <refname>systemd-socket-proxyd</refname>
                 <refpurpose>Inherit a socket. Bidirectionally
                 proxy.</refpurpose>
         </refnamediv>
         <refsynopsisdiv>
                 <cmdsynopsis>
-                        <command>systemd-saproxy</command>
+                        <command>systemd-socket-proxyd</command>
                         <arg choice="opt" rep="repeat">OPTIONS</arg>
                         <arg choice="plain"><replaceable>HOSTNAME-OR-IP</replaceable></arg>
                         <arg choice="plain"><replaceable>PORT-OR-SERVICE</replaceable></arg>
                 </cmdsynopsis>
                 <cmdsynopsis>
-                        <command>systemd-saproxy</command>
+                        <command>systemd-socket-proxyd</command>
                         <arg choice="opt" rep="repeat">OPTIONS</arg>
                         <arg choice="plain"><replaceable>UNIX-DOMAIN-SOCKET-PATH</replaceable>
                         </arg>
@@ -59,7 +59,7 @@
         <refsect1>
                 <title>Description</title>
                 <para>
-                <command>systemd-saproxy</command> provides a proxy
+                <command>systemd-socket-proxyd</command> 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.</para>
                 <para>This utility's behavior is similar to
                 <citerefentry><refentrytitle>socat</refentrytitle><manvolnum>1</manvolnum> </citerefentry>.
-                The main differences for <command>systemd-saproxy</command>
+                The main differences for <command>systemd-socket-proxyd</command>
                 are support for socket activation with
                 <literal>Accept=false</literal> and an event-driven
                 design that scales better with the number of
                                         invoked indirectly, for
                                         example with a shell script
                                         rather than with
-                                        <option>ExecStart=/usr/bin/systemd-saproxy</option>
+                                        <option>ExecStart=/usr/bin/systemd-socket-proxyd</option>
                                         </para>
                                 </listitem>
                         </varlistentry>
@@ -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]]>
 </programlisting>
@@ -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]]>
 </programlisting>
                         </example>
                         <example label="shell script">
                                 <title>
-                                /usr/bin/saproxy-nginx.sh</title>
+                                /usr/bin/socket-proxyd-nginx.sh</title>
                                 <programlisting>
 <![CDATA[#!/bin/sh
 /usr/sbin/nginx
@@ -205,7 +205,7 @@ while [ ! -f /tmp/nginx.pid ]
   do
      /usr/bin/inotifywait /tmp/nginx.pid
   done
-/usr/bin/systemd-saproxy --ignore-env localhost 8080]]>
+/usr/bin/systemd-socket-proxyd --ignore-env localhost 8080]]>
 </programlisting>
                         </example>
                         <example label="nginx configuration">
similarity index 91%
rename from src/saproxy/saproxy.c
rename to src/socket-proxy/socket-proxyd.c
index 6504d09..5921603 100644 (file)
 #include <sys/un.h>
 #include <unistd.h>
 
-#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;
 }