[systemd-commits] 2 commits - .gitignore Makefile-man.am Makefile.am man/systemd-saproxy.xml man/systemd-socket-proxyd.xml src/saproxy src/socket-proxy
Lennart Poettering
lennart at kemper.freedesktop.org
Thu Oct 17 18:13:30 PDT 2013
.gitignore | 2
Makefile-man.am | 2
Makefile.am | 70 ----
man/systemd-saproxy.xml | 254 ----------------
man/systemd-socket-proxyd.xml | 254 ++++++++++++++++
src/saproxy/Makefile | 1
src/saproxy/saproxy.c | 597 ---------------------------------------
src/socket-proxy/Makefile | 1
src/socket-proxy/socket-proxyd.c | 588 ++++++++++++++++++++++++++++++++++++++
9 files changed, 853 insertions(+), 916 deletions(-)
New commits:
commit 96c374d0a536286e18cad64d1b5ebb8f07c90334
Author: Lennart Poettering <lennart at poettering.net>
Date: Fri Oct 18 02:40:26 2013 +0200
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.
diff --git a/.gitignore b/.gitignore
index 22485b4..f5acfce 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 6b286bc..5479845 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 4a399c5..05f6f90 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-saproxy.xml
deleted file mode 100644
index ff8218c..0000000
--- a/man/systemd-saproxy.xml
+++ /dev/null
@@ -1,254 +0,0 @@
-<?xml version="1.0"?>
-<!--*-nxml-*-->
-<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
- "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
-<!--
- This file is part of systemd.
-
- Copyright 2013 David Strauss
-
- systemd is free software; you can redistribute it and/or modify it
- under the terms of the GNU Lesser General Public License as published by
- the Free Software Foundation; either version 2.1 of the License, or
- (at your option) any later version.
-
- systemd is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
-
- 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">
- <refentryinfo>
- <title>systemd-saproxy</title>
- <productname>systemd</productname>
- <authorgroup>
- <author>
- <contrib>Developer</contrib>
- <firstname>David</firstname>
- <surname>Strauss</surname>
- <email>david at davidstrauss.net</email>
- </author>
- </authorgroup>
- </refentryinfo>
- <refmeta>
- <refentrytitle>systemd-saproxy</refentrytitle>
- <manvolnum>1</manvolnum>
- </refmeta>
- <refnamediv>
- <refname>systemd-saproxy</refname>
- <refpurpose>Inherit a socket. Bidirectionally
- proxy.</refpurpose>
- </refnamediv>
- <refsynopsisdiv>
- <cmdsynopsis>
- <command>systemd-saproxy</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>
- <arg choice="opt" rep="repeat">OPTIONS</arg>
- <arg choice="plain"><replaceable>UNIX-DOMAIN-SOCKET-PATH</replaceable>
- </arg>
- </cmdsynopsis>
- </refsynopsisdiv>
- <refsect1>
- <title>Description</title>
- <para>
- <command>systemd-saproxy</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
- each client connection, opens a connection to the server
- for each client, and then bidirectionally forwards
- 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>
- are support for socket activation with
- <literal>Accept=false</literal> and an event-driven
- design that scales better with the number of
- connections.</para>
- </refsect1>
- <refsect1>
- <title>Options</title>
- <para>The following options are understood:</para>
- <variablelist>
- <varlistentry>
- <term><option>-h</option></term>
- <term><option>--help</option></term>
- <listitem>
- <para>Prints a short help
- text and exits.</para>
- </listitem>
- </varlistentry>
- <varlistentry>
- <term><option>--version</option></term>
- <listitem>
- <para>Prints a version
- string and exits.</para>
- </listitem>
- </varlistentry>
- <varlistentry>
- <term><option>--ignore-env</option></term>
- <listitem>
- <para>Skips verification of
- the expected PID and file
- descriptor numbers. Use if
- invoked indirectly, for
- example with a shell script
- rather than with
- <option>ExecStart=/usr/bin/systemd-saproxy</option>
- </para>
- </listitem>
- </varlistentry>
- </variablelist>
- </refsect1>
- <refsect1>
- <title>Exit status</title>
- <para>On success 0 is returned, a non-zero failure
- code otherwise.</para>
- </refsect1>
- <refsect1>
- <title>Examples</title>
- <refsect2>
- <title>Direct-Use Example</title>
- <para>Use two services with a dependency
- and no namespace isolation.</para>
- <example label="proxy socket unit">
- <title>/etc/systemd/system/proxy-to-nginx.socket</title>
- <programlisting>
-<![CDATA[[Socket]
-ListenStream=80
-
-[Install]
-WantedBy=socket.target]]>
-</programlisting>
- </example>
- <example label="proxy service unit">
- <title>/etc/systemd/system/proxy-to-nginx.service</title>
- <programlisting>
-<![CDATA[[Unit]
-After=nginx.service
-Requires=nginx.service
-
-[Service]
-ExecStart=/usr/bin/systemd-saproxy /tmp/nginx.sock
-PrivateTmp=true
-PrivateNetwork=true]]>
-</programlisting>
- </example>
- <example label="nginx configuration">
- <title>/etc/nginx/nginx.conf</title>
- <programlisting>
-<![CDATA[[...]
-server {
- listen unix:/tmp/nginx.sock;
- [...]]]>
-</programlisting>
- </example>
- <example label="commands">
- <programlisting>
-<![CDATA[$ sudo systemctl --system daemon-reload
-$ sudo systemctl start proxy-to-nginx.socket
-$ sudo systemctl enable proxy-to-nginx.socket
-$ curl http://localhost:80/]]>
-</programlisting>
- </example>
- </refsect2>
- <refsect2>
- <title>Indirect-Use Example</title>
- <para>Use a shell script to isolate the
- service and proxy into the same namespace.
- This is particularly useful for running
- TCP-only daemons without the daemon
- affecting ports on regular
- interfaces.</para>
- <example label="combined proxy and nginx socket unit">
-
- <title>
- /etc/systemd/system/proxy-with-nginx.socket</title>
- <programlisting>
-<![CDATA[[Socket]
-ListenStream=80
-
-[Install]
-WantedBy=socket.target]]>
-</programlisting>
- </example>
- <example label="combined proxy and nginx service unit">
-
- <title>
- /etc/systemd/system/proxy-with-nginx.service</title>
- <programlisting>
-<![CDATA[[Unit]
-After=syslog.target remote-fs.target nss-lookup.target
-
-[Service]
-ExecStartPre=/usr/sbin/nginx -t
-ExecStart=/usr/bin/saproxy-nginx.sh
-PrivateTmp=true
-PrivateNetwork=true]]>
-</programlisting>
- </example>
- <example label="shell script">
- <title>
- /usr/bin/saproxy-nginx.sh</title>
- <programlisting>
-<![CDATA[#!/bin/sh
-/usr/sbin/nginx
-while [ ! -f /tmp/nginx.pid ]
- do
- /usr/bin/inotifywait /tmp/nginx.pid
- done
-/usr/bin/systemd-saproxy --ignore-env localhost 8080]]>
-</programlisting>
- </example>
- <example label="nginx configuration">
- <title>
- /etc/nginx/nginx.conf</title>
- <programlisting>
-<![CDATA[[...]
-server {
- listen 8080;
- listen unix:/tmp/nginx.sock;
- [...]]]>
-</programlisting>
- </example>
- <example label="commands">
- <programlisting>
-<![CDATA[$ sudo systemctl --system daemon-reload
-$ sudo systemctl start proxy-with-nginx.socket
-$ sudo systemctl enable proxy-with-nginx.socket
-$ curl http://localhost:80/]]>
-</programlisting>
- </example>
- </refsect2>
- </refsect1>
- <refsect1>
- <title>See Also</title>
- <para>
- <citerefentry>
- <refentrytitle>
- systemd.service</refentrytitle>
- <manvolnum>5</manvolnum>
- </citerefentry>,
- <citerefentry>
- <refentrytitle>
- systemd.socket</refentrytitle>
- <manvolnum>5</manvolnum>
- </citerefentry>,
- <citerefentry>
- <refentrytitle>systemctl</refentrytitle>
- <manvolnum>1</manvolnum>
- </citerefentry>,
- <citerefentry>
- <refentrytitle>socat</refentrytitle>
- <manvolnum>1</manvolnum>
- </citerefentry></para>
- </refsect1>
-</refentry>
diff --git a/man/systemd-socket-proxyd.xml b/man/systemd-socket-proxyd.xml
new file mode 100644
index 0000000..90b3b1c
--- /dev/null
+++ b/man/systemd-socket-proxyd.xml
@@ -0,0 +1,254 @@
+<?xml version="1.0"?>
+<!--*-nxml-*-->
+<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
+ "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
+<!--
+ This file is part of systemd.
+
+ Copyright 2013 David Strauss
+
+ systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ 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-socket-proxyd">
+ <refentryinfo>
+ <title>systemd-socket-proxyd</title>
+ <productname>systemd</productname>
+ <authorgroup>
+ <author>
+ <contrib>Developer</contrib>
+ <firstname>David</firstname>
+ <surname>Strauss</surname>
+ <email>david at davidstrauss.net</email>
+ </author>
+ </authorgroup>
+ </refentryinfo>
+ <refmeta>
+ <refentrytitle>systemd-socket-proxyd</refentrytitle>
+ <manvolnum>1</manvolnum>
+ </refmeta>
+ <refnamediv>
+ <refname>systemd-socket-proxyd</refname>
+ <refpurpose>Inherit a socket. Bidirectionally
+ proxy.</refpurpose>
+ </refnamediv>
+ <refsynopsisdiv>
+ <cmdsynopsis>
+ <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-socket-proxyd</command>
+ <arg choice="opt" rep="repeat">OPTIONS</arg>
+ <arg choice="plain"><replaceable>UNIX-DOMAIN-SOCKET-PATH</replaceable>
+ </arg>
+ </cmdsynopsis>
+ </refsynopsisdiv>
+ <refsect1>
+ <title>Description</title>
+ <para>
+ <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
+ each client connection, opens a connection to the server
+ for each client, and then bidirectionally forwards
+ 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-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
+ connections.</para>
+ </refsect1>
+ <refsect1>
+ <title>Options</title>
+ <para>The following options are understood:</para>
+ <variablelist>
+ <varlistentry>
+ <term><option>-h</option></term>
+ <term><option>--help</option></term>
+ <listitem>
+ <para>Prints a short help
+ text and exits.</para>
+ </listitem>
+ </varlistentry>
+ <varlistentry>
+ <term><option>--version</option></term>
+ <listitem>
+ <para>Prints a version
+ string and exits.</para>
+ </listitem>
+ </varlistentry>
+ <varlistentry>
+ <term><option>--ignore-env</option></term>
+ <listitem>
+ <para>Skips verification of
+ the expected PID and file
+ descriptor numbers. Use if
+ invoked indirectly, for
+ example with a shell script
+ rather than with
+ <option>ExecStart=/usr/bin/systemd-socket-proxyd</option>
+ </para>
+ </listitem>
+ </varlistentry>
+ </variablelist>
+ </refsect1>
+ <refsect1>
+ <title>Exit status</title>
+ <para>On success 0 is returned, a non-zero failure
+ code otherwise.</para>
+ </refsect1>
+ <refsect1>
+ <title>Examples</title>
+ <refsect2>
+ <title>Direct-Use Example</title>
+ <para>Use two services with a dependency
+ and no namespace isolation.</para>
+ <example label="proxy socket unit">
+ <title>/etc/systemd/system/proxy-to-nginx.socket</title>
+ <programlisting>
+<![CDATA[[Socket]
+ListenStream=80
+
+[Install]
+WantedBy=socket.target]]>
+</programlisting>
+ </example>
+ <example label="proxy service unit">
+ <title>/etc/systemd/system/proxy-to-nginx.service</title>
+ <programlisting>
+<![CDATA[[Unit]
+After=nginx.service
+Requires=nginx.service
+
+[Service]
+ExecStart=/usr/bin/systemd-socket-proxyd /tmp/nginx.sock
+PrivateTmp=true
+PrivateNetwork=true]]>
+</programlisting>
+ </example>
+ <example label="nginx configuration">
+ <title>/etc/nginx/nginx.conf</title>
+ <programlisting>
+<![CDATA[[...]
+server {
+ listen unix:/tmp/nginx.sock;
+ [...]]]>
+</programlisting>
+ </example>
+ <example label="commands">
+ <programlisting>
+<![CDATA[$ sudo systemctl --system daemon-reload
+$ sudo systemctl start proxy-to-nginx.socket
+$ sudo systemctl enable proxy-to-nginx.socket
+$ curl http://localhost:80/]]>
+</programlisting>
+ </example>
+ </refsect2>
+ <refsect2>
+ <title>Indirect-Use Example</title>
+ <para>Use a shell script to isolate the
+ service and proxy into the same namespace.
+ This is particularly useful for running
+ TCP-only daemons without the daemon
+ affecting ports on regular
+ interfaces.</para>
+ <example label="combined proxy and nginx socket unit">
+
+ <title>
+ /etc/systemd/system/proxy-with-nginx.socket</title>
+ <programlisting>
+<![CDATA[[Socket]
+ListenStream=80
+
+[Install]
+WantedBy=socket.target]]>
+</programlisting>
+ </example>
+ <example label="combined proxy and nginx service unit">
+
+ <title>
+ /etc/systemd/system/proxy-with-nginx.service</title>
+ <programlisting>
+<![CDATA[[Unit]
+After=syslog.target remote-fs.target nss-lookup.target
+
+[Service]
+ExecStartPre=/usr/sbin/nginx -t
+ExecStart=/usr/bin/socket-proxyd-nginx.sh
+PrivateTmp=true
+PrivateNetwork=true]]>
+</programlisting>
+ </example>
+ <example label="shell script">
+ <title>
+ /usr/bin/socket-proxyd-nginx.sh</title>
+ <programlisting>
+<![CDATA[#!/bin/sh
+/usr/sbin/nginx
+while [ ! -f /tmp/nginx.pid ]
+ do
+ /usr/bin/inotifywait /tmp/nginx.pid
+ done
+/usr/bin/systemd-socket-proxyd --ignore-env localhost 8080]]>
+</programlisting>
+ </example>
+ <example label="nginx configuration">
+ <title>
+ /etc/nginx/nginx.conf</title>
+ <programlisting>
+<![CDATA[[...]
+server {
+ listen 8080;
+ listen unix:/tmp/nginx.sock;
+ [...]]]>
+</programlisting>
+ </example>
+ <example label="commands">
+ <programlisting>
+<![CDATA[$ sudo systemctl --system daemon-reload
+$ sudo systemctl start proxy-with-nginx.socket
+$ sudo systemctl enable proxy-with-nginx.socket
+$ curl http://localhost:80/]]>
+</programlisting>
+ </example>
+ </refsect2>
+ </refsect1>
+ <refsect1>
+ <title>See Also</title>
+ <para>
+ <citerefentry>
+ <refentrytitle>
+ systemd.service</refentrytitle>
+ <manvolnum>5</manvolnum>
+ </citerefentry>,
+ <citerefentry>
+ <refentrytitle>
+ systemd.socket</refentrytitle>
+ <manvolnum>5</manvolnum>
+ </citerefentry>,
+ <citerefentry>
+ <refentrytitle>systemctl</refentrytitle>
+ <manvolnum>1</manvolnum>
+ </citerefentry>,
+ <citerefentry>
+ <refentrytitle>socat</refentrytitle>
+ <manvolnum>1</manvolnum>
+ </citerefentry></para>
+ </refsect1>
+</refentry>
diff --git a/src/saproxy/Makefile b/src/saproxy/Makefile
deleted file mode 120000
index d0b0e8e..0000000
--- a/src/saproxy/Makefile
+++ /dev/null
@@ -1 +0,0 @@
-../Makefile
\ No newline at end of file
diff --git a/src/saproxy/saproxy.c b/src/saproxy/saproxy.c
deleted file mode 100644
index 6504d09..0000000
--- a/src/saproxy/saproxy.c
+++ /dev/null
@@ -1,597 +0,0 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 David Strauss
-
- systemd is free software; you can redistribute it and/or modify it
- under the terms of the GNU Lesser General Public License as published by
- the Free Software Foundation; either version 2.1 of the License, or
- (at your option) any later version.
-
- systemd is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with systemd; If not, see <http://www.gnu.org/licenses/>.
- ***/
-
-#include <arpa/inet.h>
-#include <errno.h>
-#include <getopt.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <netdb.h>
-#include <sys/fcntl.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <unistd.h>
-
-#include "log.h"
-#include "sd-daemon.h"
-#include "sd-event.h"
-#include "socket-util.h"
-#include "util.h"
-
-#define BUFFER_SIZE 16384
-#define _cleanup_freeaddrinfo_ _cleanup_(freeaddrinfop)
-
-unsigned int total_clients = 0;
-
-DEFINE_TRIVIAL_CLEANUP_FUNC(struct addrinfo *, freeaddrinfo);
-
-struct proxy {
- int listen_fd;
- bool ignore_env;
- bool remote_is_inet;
- const char *remote_host;
- const char *remote_service;
-};
-
-struct connection {
- int fd;
- uint32_t events;
- sd_event_source *w;
- struct connection *c_destination;
- size_t buffer_filled_len;
- size_t buffer_sent_len;
- char buffer[BUFFER_SIZE];
-};
-
-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);
- free(c);
-}
-
-static int add_event_to_connection(struct connection *c, uint32_t events) {
- int r;
-
- log_debug("Have revents=%d. Adding revents=%d.", c->events, events);
-
- c->events |= events;
-
- r = sd_event_source_set_io_events(c->w, c->events);
- if (r < 0) {
- log_error("Error %d setting revents: %s", r, strerror(-r));
- return r;
- }
-
- r = sd_event_source_set_enabled(c->w, SD_EVENT_ON);
- if (r < 0) {
- log_error("Error %d enabling source: %s", r, strerror(-r));
- return r;
- }
-
- return 0;
-}
-
-static int remove_event_from_connection(struct connection *c, uint32_t events) {
- int r;
-
- log_debug("Have revents=%d. Removing revents=%d.", c->events, events);
-
- c->events &= ~events;
-
- r = sd_event_source_set_io_events(c->w, c->events);
- if (r < 0) {
- log_error("Error %d setting revents: %s", r, strerror(-r));
- return r;
- }
-
- if (c->events == 0) {
- r = sd_event_source_set_enabled(c->w, SD_EVENT_OFF);
- if (r < 0) {
- log_error("Error %d disabling source: %s", r, strerror(-r));
- return r;
- }
- }
-
- return 0;
-}
-
-static int send_buffer(struct connection *sender) {
- struct connection *receiver = sender->c_destination;
- ssize_t len;
- int r = 0;
-
- /* We cannot assume that even a partial send() indicates that
- * the next send() will block. Loop until it does. */
- while (sender->buffer_filled_len > sender->buffer_sent_len) {
- len = send(receiver->fd, sender->buffer + sender->buffer_sent_len, sender->buffer_filled_len - sender->buffer_sent_len, 0);
- log_debug("send(%d, ...)=%ld", receiver->fd, len);
- if (len < 0) {
- if (errno != EWOULDBLOCK && errno != EAGAIN) {
- log_error("Error %d in send to fd=%d: %m", errno, receiver->fd);
- return -errno;
- }
- else {
- /* send() is in a blocking state. */
- break;
- }
- }
-
- /* len < 0 can't occur here. len == 0 is possible but
- * undefined behavior for nonblocking send(). */
- assert(len > 0);
- sender->buffer_sent_len += len;
- }
-
- log_debug("send(%d, ...) completed with %lu bytes still buffered.", receiver->fd, sender->buffer_filled_len - sender->buffer_sent_len);
-
- /* Detect a would-block state or partial send. */
- if (sender->buffer_filled_len > sender->buffer_sent_len) {
-
- /* If the buffer is full, disable events coming for recv. */
- if (sender->buffer_filled_len == BUFFER_SIZE) {
- r = remove_event_from_connection(sender, EPOLLIN);
- if (r < 0) {
- log_error("Error %d disabling EPOLLIN for fd=%d: %s", r, sender->fd, strerror(-r));
- return r;
- }
- }
-
- /* Watch for when the recipient can be sent data again. */
- r = add_event_to_connection(receiver, EPOLLOUT);
- if (r < 0) {
- log_error("Error %d enabling EPOLLOUT for fd=%d: %s", r, receiver->fd, strerror(-r));
- return r;
- }
- log_debug("Done with recv for fd=%d. Waiting on send for fd=%d.", sender->fd, receiver->fd);
- return r;
- }
-
- /* If we sent everything without blocking, the buffer is now empty. */
- sender->buffer_filled_len = 0;
- sender->buffer_sent_len = 0;
-
- /* Enable the sender's receive watcher, in case the buffer was
- * full and we disabled it. */
- r = add_event_to_connection(sender, EPOLLIN);
- if (r < 0) {
- log_error("Error %d enabling EPOLLIN for fd=%d: %s", r, sender->fd, strerror(-r));
- return r;
- }
-
- /* Disable the other side's send watcher, as we have no data to send now. */
- r = remove_event_from_connection(receiver, EPOLLOUT);
- if (r < 0) {
- log_error("Error %d disabling EPOLLOUT for fd=%d: %s", r, receiver->fd, strerror(-r));
- return r;
- }
-
- return 0;
-}
-
-static int transfer_data_cb(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
- struct connection *c = (struct connection *) userdata;
- int r = 0;
- ssize_t len;
-
- assert(revents & (EPOLLIN | EPOLLOUT));
- assert(fd == c->fd);
- assert(s == c->w);
-
- log_debug("Got event revents=%d from fd=%d (conn %p).", revents, fd, c);
-
- if (revents & EPOLLIN) {
- log_debug("About to recv up to %lu bytes from fd=%d (%lu/BUFFER_SIZE).", BUFFER_SIZE - c->buffer_filled_len, fd, c->buffer_filled_len);
-
- /* Receive until the buffer's full, there's no more data,
- * or the client/server disconnects. */
- while (c->buffer_filled_len < BUFFER_SIZE) {
- len = recv(fd, c->buffer + c->buffer_filled_len, BUFFER_SIZE - c->buffer_filled_len, 0);
- log_debug("recv(%d, ...)=%ld", fd, len);
- if (len < 0) {
- if (errno != EWOULDBLOCK && errno != EAGAIN) {
- log_error("Error %d in recv from fd=%d: %m", errno, fd);
- return -errno;
- }
- else {
- /* recv() is in a blocking state. */
- break;
- }
- }
- else if (len == 0) {
- log_debug("Clean disconnection from fd=%d", fd);
- total_clients--;
- free_connection(c->c_destination);
- free_connection(c);
- return 0;
- }
-
- assert(len > 0);
- log_debug("Recording that the buffer got %ld more bytes full.", len);
- c->buffer_filled_len += len;
- log_debug("Buffer now has %ld bytes full.", c->buffer_filled_len);
- }
-
- /* Try sending the data immediately. */
- return send_buffer(c);
- }
- else {
- return send_buffer(c->c_destination);
- }
-
- return r;
-}
-
-/* Once sending to the server is unblocked, set up the real watchers. */
-static int connected_to_server_cb(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
- struct sd_event *e = NULL;
- struct connection *c_server_to_client = (struct connection *) userdata;
- struct connection *c_client_to_server = c_server_to_client->c_destination;
- int r;
-
- assert(revents & EPOLLOUT);
-
- e = sd_event_get(s);
-
- /* Cancel the initial write watcher for the server. */
- sd_event_source_unref(s);
-
- log_debug("Connected to server. Initializing watchers for receiving data.");
-
- /* A recv watcher for the server. */
- r = sd_event_add_io(e, c_server_to_client->fd, EPOLLIN, transfer_data_cb, c_server_to_client, &c_server_to_client->w);
- if (r < 0) {
- log_error("Error %d creating recv watcher for fd=%d: %s", r, c_server_to_client->fd, strerror(-r));
- goto fail;
- }
- c_server_to_client->events = EPOLLIN;
-
- /* A recv watcher for the client. */
- r = sd_event_add_io(e, c_client_to_server->fd, EPOLLIN, transfer_data_cb, c_client_to_server, &c_client_to_server->w);
- if (r < 0) {
- log_error("Error %d creating recv watcher for fd=%d: %s", r, c_client_to_server->fd, strerror(-r));
- goto fail;
- }
- c_client_to_server->events = EPOLLIN;
-
-goto finish;
-
-fail:
- free_connection(c_client_to_server);
- free_connection(c_server_to_client);
-
-finish:
- return r;
-}
-
-static int get_server_connection_fd(const struct proxy *proxy) {
- int server_fd;
- int r = -EBADF;
- int len;
-
- if (proxy->remote_is_inet) {
- int s;
- _cleanup_freeaddrinfo_ struct addrinfo *result = NULL;
- struct addrinfo hints = {.ai_family = AF_UNSPEC,
- .ai_socktype = SOCK_STREAM,
- .ai_flags = AI_PASSIVE};
-
- log_debug("Looking up address info for %s:%s", proxy->remote_host, proxy->remote_service);
- s = getaddrinfo(proxy->remote_host, proxy->remote_service, &hints, &result);
- if (s != 0) {
- log_error("getaddrinfo error (%d): %s", s, gai_strerror(s));
- return r;
- }
-
- if (result == NULL) {
- log_error("getaddrinfo: no result");
- return r;
- }
-
- /* @TODO: Try connecting to all results instead of just the first. */
- server_fd = socket(result->ai_family, result->ai_socktype | SOCK_NONBLOCK, result->ai_protocol);
- if (server_fd < 0) {
- log_error("Error %d creating socket: %m", errno);
- return r;
- }
-
- r = connect(server_fd, result->ai_addr, result->ai_addrlen);
- /* Ignore EINPROGRESS errors because they're expected for a nonblocking socket. */
- if (r < 0 && errno != EINPROGRESS) {
- log_error("Error %d while connecting to socket %s:%s: %m", errno, proxy->remote_host, proxy->remote_service);
- return r;
- }
- }
- else {
- struct sockaddr_un remote;
-
- server_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
- if (server_fd < 0) {
- log_error("Error %d creating socket: %m", errno);
- return -EBADFD;
- }
-
- remote.sun_family = AF_UNIX;
- strncpy(remote.sun_path, proxy->remote_host, sizeof(remote.sun_path));
- len = strlen(remote.sun_path) + sizeof(remote.sun_family);
- r = connect(server_fd, (struct sockaddr *) &remote, len);
- if (r < 0 && errno != EINPROGRESS) {
- log_error("Error %d while connecting to Unix domain socket %s: %m", errno, proxy->remote_host);
- return -EBADFD;
- }
- }
-
- log_debug("Server connection is fd=%d", server_fd);
- return server_fd;
-}
-
-static int accept_cb(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
- struct proxy *proxy = (struct proxy *) userdata;
- struct connection *c_server_to_client;
- struct connection *c_client_to_server;
- int r = 0;
- union sockaddr_union sa;
- socklen_t salen = sizeof(sa);
-
- assert(revents & EPOLLIN);
-
- c_server_to_client = malloc0(sizeof(struct connection));
- if (c_server_to_client == NULL) {
- log_oom();
- goto fail;
- }
-
- c_client_to_server = malloc0(sizeof(struct connection));
- if (c_client_to_server == NULL) {
- log_oom();
- goto fail;
- }
-
- c_server_to_client->fd = get_server_connection_fd(proxy);
- if (c_server_to_client->fd < 0) {
- log_error("Error initiating server connection.");
- goto fail;
- }
-
- c_client_to_server->fd = accept(fd, (struct sockaddr *) &sa, &salen);
- 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];
- const char *success;
-
- success = inet_ntop(sa.sa.sa_family, &sa.in6.sin6_addr, sa_str, INET6_ADDRSTRLEN);
- if (success == NULL)
- log_warning("Error %d calling inet_ntop: %m", errno);
- else
- log_debug("Accepted client connection from %s as fd=%d", sa_str, c_client_to_server->fd);
- }
- else {
- log_debug("Accepted client connection (non-IP) as fd=%d", c_client_to_server->fd);
- }
-
- total_clients++;
- log_debug("Client fd=%d (conn %p) successfully connected. Total clients: %u", c_client_to_server->fd, c_client_to_server, total_clients);
- log_debug("Server fd=%d (conn %p) successfully initialized.", c_server_to_client->fd, c_server_to_client);
-
- /* Initialize watcher for send to server; this shows connectivity. */
- r = sd_event_add_io(sd_event_get(s), c_server_to_client->fd, EPOLLOUT, connected_to_server_cb, c_server_to_client, &c_server_to_client->w);
- if (r < 0) {
- log_error("Error %d creating connectivity watcher for fd=%d: %s", r, c_server_to_client->fd, strerror(-r));
- goto fail;
- }
-
- /* Allow lookups of the opposite connection. */
- c_server_to_client->c_destination = c_client_to_server;
- c_client_to_server->c_destination = c_server_to_client;
-
- goto finish;
-
-fail:
- log_warning("Accepting a client connection or connecting to the server failed.");
- free_connection(c_client_to_server);
- free_connection(c_server_to_client);
-
-finish:
- /* Preserve the main loop even if a single proxy setup fails. */
- return 0;
-}
-
-static int run_main_loop(struct proxy *proxy) {
- 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;
-
- r = fd_nonblock(proxy->listen_fd, true);
- if (r < 0)
- goto finish;
-
- log_debug("Initializing main listener fd=%d", proxy->listen_fd);
-
- sd_event_add_io(e, proxy->listen_fd, EPOLLIN, accept_cb, proxy, &w_accept);
-
- log_debug("Initialized main listener. Entering loop.");
-
- sd_event_loop(e);
-
-finish:
- sd_event_source_unref(w_accept);
- sd_event_unref(e);
-
- return r;
-}
-
-static int help(void) {
-
- printf("%s hostname-or-ip port-or-service\n"
- "%s unix-domain-socket-path\n\n"
- "Inherit a socket. Bidirectionally proxy.\n\n"
- " -h --help Show this help\n"
- " --version Print version and exit\n"
- " --ignore-env Ignore expected systemd environment\n",
- program_invocation_short_name,
- program_invocation_short_name);
-
- return 0;
-}
-
-static void version(void) {
- puts(PACKAGE_STRING " saproxy");
-}
-
-static int parse_argv(int argc, char *argv[], struct proxy *p) {
-
- enum {
- ARG_VERSION = 0x100,
- ARG_IGNORE_ENV
- };
-
- static const struct option options[] = {
- { "help", no_argument, NULL, 'h' },
- { "version", no_argument, NULL, ARG_VERSION },
- { "ignore-env", no_argument, NULL, ARG_IGNORE_ENV},
- { NULL, 0, NULL, 0 }
- };
-
- int c;
-
- assert(argc >= 0);
- assert(argv);
-
- while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
-
- switch (c) {
-
- case 'h':
- help();
- return 0;
-
- case '?':
- return -EINVAL;
-
- case ARG_VERSION:
- version();
- return 0;
-
- case ARG_IGNORE_ENV:
- p->ignore_env = true;
- continue;
-
- default:
- log_error("Unknown option code %c", c);
- return -EINVAL;
- }
- }
-
- if (optind + 1 != argc && optind + 2 != argc) {
- log_error("Incorrect number of positional arguments.");
- help();
- return -EINVAL;
- }
-
- p->remote_host = argv[optind];
- assert(p->remote_host);
-
- p->remote_is_inet = p->remote_host[0] != '/';
-
- if (optind == argc - 2) {
- if (!p->remote_is_inet) {
- log_error("A port or service is not allowed for Unix socket destinations.");
- help();
- return -EINVAL;
- }
- p->remote_service = argv[optind + 1];
- assert(p->remote_service);
- } else if (p->remote_is_inet) {
- log_error("A port or service is required for IP destinations.");
- help();
- return -EINVAL;
- }
-
- return 1;
-}
-
-int main(int argc, char *argv[]) {
- struct proxy p = {};
- int r;
-
- log_parse_environment();
- log_open();
-
- r = parse_argv(argc, argv, &p);
- if (r <= 0)
- goto finish;
-
- 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;
- }
- }
-
- /* @TODO: Check if this proxy can work with datagram sockets. */
- r = sd_is_socket(p.listen_fd, 0, SOCK_STREAM, 1);
- if (r < 0) {
- log_error("Error %d while checking inherited socket: %s", r, strerror(-r));
- goto finish;
- }
-
- 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;
-}
diff --git a/src/socket-proxy/Makefile b/src/socket-proxy/Makefile
new file mode 120000
index 0000000..d0b0e8e
--- /dev/null
+++ b/src/socket-proxy/Makefile
@@ -0,0 +1 @@
+../Makefile
\ No newline at end of file
diff --git a/src/socket-proxy/socket-proxyd.c b/src/socket-proxy/socket-proxyd.c
new file mode 100644
index 0000000..5921603
--- /dev/null
+++ b/src/socket-proxy/socket-proxyd.c
@@ -0,0 +1,588 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+ This file is part of systemd.
+
+ Copyright 2013 David Strauss
+
+ systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with systemd; If not, see <http://www.gnu.org/licenses/>.
+ ***/
+
+#include <arpa/inet.h>
+#include <errno.h>
+#include <getopt.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <netdb.h>
+#include <sys/fcntl.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <unistd.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)
+
+unsigned int total_clients = 0;
+
+DEFINE_TRIVIAL_CLEANUP_FUNC(struct addrinfo *, freeaddrinfo);
+
+struct proxy {
+ int listen_fd;
+ bool ignore_env;
+ bool remote_is_inet;
+ const char *remote_host;
+ const char *remote_service;
+};
+
+struct connection {
+ int fd;
+ uint32_t events;
+ sd_event_source *w;
+ struct connection *c_destination;
+ size_t buffer_filled_len;
+ size_t buffer_sent_len;
+ char buffer[BUFFER_SIZE];
+};
+
+static void free_connection(struct connection *c) {
+ log_debug("Freeing fd=%d (conn %p).", c->fd, c);
+ sd_event_source_unref(c->w);
+ close_nointr_nofail(c->fd);
+ free(c);
+}
+
+static int add_event_to_connection(struct connection *c, uint32_t events) {
+ int r;
+
+ log_debug("Have revents=%d. Adding revents=%d.", c->events, events);
+
+ c->events |= events;
+
+ r = sd_event_source_set_io_events(c->w, c->events);
+ if (r < 0) {
+ log_error("Error %d setting revents: %s", r, strerror(-r));
+ return r;
+ }
+
+ r = sd_event_source_set_enabled(c->w, SD_EVENT_ON);
+ if (r < 0) {
+ log_error("Error %d enabling source: %s", r, strerror(-r));
+ return r;
+ }
+
+ return 0;
+}
+
+static int remove_event_from_connection(struct connection *c, uint32_t events) {
+ int r;
+
+ log_debug("Have revents=%d. Removing revents=%d.", c->events, events);
+
+ c->events &= ~events;
+
+ r = sd_event_source_set_io_events(c->w, c->events);
+ if (r < 0) {
+ log_error("Error %d setting revents: %s", r, strerror(-r));
+ return r;
+ }
+
+ if (c->events == 0) {
+ r = sd_event_source_set_enabled(c->w, SD_EVENT_OFF);
+ if (r < 0) {
+ log_error("Error %d disabling source: %s", r, strerror(-r));
+ return r;
+ }
+ }
+
+ return 0;
+}
+
+static int send_buffer(struct connection *sender) {
+ struct connection *receiver = sender->c_destination;
+ ssize_t len;
+ int r = 0;
+
+ /* We cannot assume that even a partial send() indicates that
+ * the next send() will block. Loop until it does. */
+ while (sender->buffer_filled_len > sender->buffer_sent_len) {
+ len = send(receiver->fd, sender->buffer + sender->buffer_sent_len, sender->buffer_filled_len - sender->buffer_sent_len, 0);
+ log_debug("send(%d, ...)=%ld", receiver->fd, len);
+ if (len < 0) {
+ if (errno != EWOULDBLOCK && errno != EAGAIN) {
+ log_error("Error %d in send to fd=%d: %m", errno, receiver->fd);
+ return -errno;
+ }
+ else {
+ /* send() is in a blocking state. */
+ break;
+ }
+ }
+
+ /* len < 0 can't occur here. len == 0 is possible but
+ * undefined behavior for nonblocking send(). */
+ assert(len > 0);
+ sender->buffer_sent_len += len;
+ }
+
+ log_debug("send(%d, ...) completed with %lu bytes still buffered.", receiver->fd, sender->buffer_filled_len - sender->buffer_sent_len);
+
+ /* Detect a would-block state or partial send. */
+ if (sender->buffer_filled_len > sender->buffer_sent_len) {
+
+ /* If the buffer is full, disable events coming for recv. */
+ if (sender->buffer_filled_len == BUFFER_SIZE) {
+ r = remove_event_from_connection(sender, EPOLLIN);
+ if (r < 0) {
+ log_error("Error %d disabling EPOLLIN for fd=%d: %s", r, sender->fd, strerror(-r));
+ return r;
+ }
+ }
+
+ /* Watch for when the recipient can be sent data again. */
+ r = add_event_to_connection(receiver, EPOLLOUT);
+ if (r < 0) {
+ log_error("Error %d enabling EPOLLOUT for fd=%d: %s", r, receiver->fd, strerror(-r));
+ return r;
+ }
+ log_debug("Done with recv for fd=%d. Waiting on send for fd=%d.", sender->fd, receiver->fd);
+ return r;
+ }
+
+ /* If we sent everything without blocking, the buffer is now empty. */
+ sender->buffer_filled_len = 0;
+ sender->buffer_sent_len = 0;
+
+ /* Enable the sender's receive watcher, in case the buffer was
+ * full and we disabled it. */
+ r = add_event_to_connection(sender, EPOLLIN);
+ if (r < 0) {
+ log_error("Error %d enabling EPOLLIN for fd=%d: %s", r, sender->fd, strerror(-r));
+ return r;
+ }
+
+ /* Disable the other side's send watcher, as we have no data to send now. */
+ r = remove_event_from_connection(receiver, EPOLLOUT);
+ if (r < 0) {
+ log_error("Error %d disabling EPOLLOUT for fd=%d: %s", r, receiver->fd, strerror(-r));
+ return r;
+ }
+
+ return 0;
+}
+
+static int transfer_data_cb(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
+ struct connection *c = (struct connection *) userdata;
+ int r = 0;
+ ssize_t len;
+
+ assert(revents & (EPOLLIN | EPOLLOUT));
+ assert(fd == c->fd);
+ assert(s == c->w);
+
+ log_debug("Got event revents=%d from fd=%d (conn %p).", revents, fd, c);
+
+ if (revents & EPOLLIN) {
+ log_debug("About to recv up to %lu bytes from fd=%d (%lu/BUFFER_SIZE).", BUFFER_SIZE - c->buffer_filled_len, fd, c->buffer_filled_len);
+
+ /* Receive until the buffer's full, there's no more data,
+ * or the client/server disconnects. */
+ while (c->buffer_filled_len < BUFFER_SIZE) {
+ len = recv(fd, c->buffer + c->buffer_filled_len, BUFFER_SIZE - c->buffer_filled_len, 0);
+ log_debug("recv(%d, ...)=%ld", fd, len);
+ if (len < 0) {
+ if (errno != EWOULDBLOCK && errno != EAGAIN) {
+ log_error("Error %d in recv from fd=%d: %m", errno, fd);
+ return -errno;
+ }
+ else {
+ /* recv() is in a blocking state. */
+ break;
+ }
+ }
+ else if (len == 0) {
+ log_debug("Clean disconnection from fd=%d", fd);
+ total_clients--;
+ free_connection(c->c_destination);
+ free_connection(c);
+ return 0;
+ }
+
+ assert(len > 0);
+ log_debug("Recording that the buffer got %ld more bytes full.", len);
+ c->buffer_filled_len += len;
+ log_debug("Buffer now has %ld bytes full.", c->buffer_filled_len);
+ }
+
+ /* Try sending the data immediately. */
+ return send_buffer(c);
+ }
+ else {
+ return send_buffer(c->c_destination);
+ }
+
+ return r;
+}
+
+/* Once sending to the server is unblocked, set up the real watchers. */
+static int connected_to_server_cb(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
+ struct sd_event *e = NULL;
+ struct connection *c_server_to_client = (struct connection *) userdata;
+ struct connection *c_client_to_server = c_server_to_client->c_destination;
+ int r;
+
+ assert(revents & EPOLLOUT);
+
+ e = sd_event_get(s);
+
+ /* Cancel the initial write watcher for the server. */
+ sd_event_source_unref(s);
+
+ log_debug("Connected to server. Initializing watchers for receiving data.");
+
+ /* A recv watcher for the server. */
+ r = sd_event_add_io(e, c_server_to_client->fd, EPOLLIN, transfer_data_cb, c_server_to_client, &c_server_to_client->w);
+ if (r < 0) {
+ log_error("Error %d creating recv watcher for fd=%d: %s", r, c_server_to_client->fd, strerror(-r));
+ goto fail;
+ }
+ c_server_to_client->events = EPOLLIN;
+
+ /* A recv watcher for the client. */
+ r = sd_event_add_io(e, c_client_to_server->fd, EPOLLIN, transfer_data_cb, c_client_to_server, &c_client_to_server->w);
+ if (r < 0) {
+ log_error("Error %d creating recv watcher for fd=%d: %s", r, c_client_to_server->fd, strerror(-r));
+ goto fail;
+ }
+ c_client_to_server->events = EPOLLIN;
+
+goto finish;
+
+fail:
+ free_connection(c_client_to_server);
+ free_connection(c_server_to_client);
+
+finish:
+ return r;
+}
+
+static int get_server_connection_fd(const struct proxy *proxy) {
+ int server_fd;
+ int r = -EBADF;
+ int len;
+
+ if (proxy->remote_is_inet) {
+ int s;
+ _cleanup_freeaddrinfo_ struct addrinfo *result = NULL;
+ struct addrinfo hints = {.ai_family = AF_UNSPEC,
+ .ai_socktype = SOCK_STREAM,
+ .ai_flags = AI_PASSIVE};
+
+ log_debug("Looking up address info for %s:%s", proxy->remote_host, proxy->remote_service);
+ s = getaddrinfo(proxy->remote_host, proxy->remote_service, &hints, &result);
+ if (s != 0) {
+ log_error("getaddrinfo error (%d): %s", s, gai_strerror(s));
+ return r;
+ }
+
+ if (result == NULL) {
+ log_error("getaddrinfo: no result");
+ return r;
+ }
+
+ /* @TODO: Try connecting to all results instead of just the first. */
+ server_fd = socket(result->ai_family, result->ai_socktype | SOCK_NONBLOCK, result->ai_protocol);
+ if (server_fd < 0) {
+ log_error("Error %d creating socket: %m", errno);
+ return r;
+ }
+
+ r = connect(server_fd, result->ai_addr, result->ai_addrlen);
+ /* Ignore EINPROGRESS errors because they're expected for a nonblocking socket. */
+ if (r < 0 && errno != EINPROGRESS) {
+ log_error("Error %d while connecting to socket %s:%s: %m", errno, proxy->remote_host, proxy->remote_service);
+ return r;
+ }
+ }
+ else {
+ struct sockaddr_un remote;
+
+ server_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
+ if (server_fd < 0) {
+ log_error("Error %d creating socket: %m", errno);
+ return -EBADFD;
+ }
+
+ remote.sun_family = AF_UNIX;
+ strncpy(remote.sun_path, proxy->remote_host, sizeof(remote.sun_path));
+ len = strlen(remote.sun_path) + sizeof(remote.sun_family);
+ r = connect(server_fd, (struct sockaddr *) &remote, len);
+ if (r < 0 && errno != EINPROGRESS) {
+ log_error("Error %d while connecting to Unix domain socket %s: %m", errno, proxy->remote_host);
+ return -EBADFD;
+ }
+ }
+
+ log_debug("Server connection is fd=%d", server_fd);
+ return server_fd;
+}
+
+static int accept_cb(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
+ struct proxy *proxy = (struct proxy *) userdata;
+ struct connection *c_server_to_client;
+ struct connection *c_client_to_server;
+ int r = 0;
+ union sockaddr_union sa;
+ socklen_t salen = sizeof(sa);
+
+ assert(revents & EPOLLIN);
+
+ c_server_to_client = new0(struct connection, 1);
+ if (c_server_to_client == NULL) {
+ log_oom();
+ goto fail;
+ }
+
+ c_client_to_server = new0(struct connection, 1);
+ if (c_client_to_server == NULL) {
+ log_oom();
+ goto fail;
+ }
+
+ c_server_to_client->fd = get_server_connection_fd(proxy);
+ if (c_server_to_client->fd < 0) {
+ log_error("Error initiating server connection.");
+ goto fail;
+ }
+
+ 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;
+ }
+
+
+ if (sa.sa.sa_family == AF_INET || sa.sa.sa_family == AF_INET6) {
+ char sa_str[INET6_ADDRSTRLEN];
+ const char *success;
+
+ success = inet_ntop(sa.sa.sa_family, &sa.in6.sin6_addr, sa_str, INET6_ADDRSTRLEN);
+ if (success == NULL)
+ log_warning("Error %d calling inet_ntop: %m", errno);
+ else
+ log_debug("Accepted client connection from %s as fd=%d", sa_str, c_client_to_server->fd);
+ }
+ else {
+ log_debug("Accepted client connection (non-IP) as fd=%d", c_client_to_server->fd);
+ }
+
+ total_clients++;
+ log_debug("Client fd=%d (conn %p) successfully connected. Total clients: %u", c_client_to_server->fd, c_client_to_server, total_clients);
+ log_debug("Server fd=%d (conn %p) successfully initialized.", c_server_to_client->fd, c_server_to_client);
+
+ /* Initialize watcher for send to server; this shows connectivity. */
+ r = sd_event_add_io(sd_event_get(s), c_server_to_client->fd, EPOLLOUT, connected_to_server_cb, c_server_to_client, &c_server_to_client->w);
+ if (r < 0) {
+ log_error("Error %d creating connectivity watcher for fd=%d: %s", r, c_server_to_client->fd, strerror(-r));
+ goto fail;
+ }
+
+ /* Allow lookups of the opposite connection. */
+ c_server_to_client->c_destination = c_client_to_server;
+ c_client_to_server->c_destination = c_server_to_client;
+
+ goto finish;
+
+fail:
+ log_warning("Accepting a client connection or connecting to the server failed.");
+ free_connection(c_client_to_server);
+ free_connection(c_server_to_client);
+
+finish:
+ /* Preserve the main loop even if a single proxy setup fails. */
+ 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;
+
+ r = sd_event_new(&e);
+ 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) {
+ 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);
+
+ 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.");
+
+ return sd_event_loop(e);
+}
+
+static int help(void) {
+
+ printf("%s hostname-or-ip port-or-service\n"
+ "%s unix-domain-socket-path\n\n"
+ "Inherit a socket. Bidirectionally proxy.\n\n"
+ " -h --help Show this help\n"
+ " --version Print version and exit\n"
+ " --ignore-env Ignore expected systemd environment\n",
+ program_invocation_short_name,
+ program_invocation_short_name);
+
+ return 0;
+}
+
+static void version(void) {
+ puts(PACKAGE_STRING " socket-proxyd");
+}
+
+static int parse_argv(int argc, char *argv[], struct proxy *p) {
+
+ enum {
+ ARG_VERSION = 0x100,
+ ARG_IGNORE_ENV
+ };
+
+ static const struct option options[] = {
+ { "help", no_argument, NULL, 'h' },
+ { "version", no_argument, NULL, ARG_VERSION },
+ { "ignore-env", no_argument, NULL, ARG_IGNORE_ENV},
+ { NULL, 0, NULL, 0 }
+ };
+
+ int c;
+
+ assert(argc >= 0);
+ assert(argv);
+
+ while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
+
+ switch (c) {
+
+ case 'h':
+ help();
+ return 0;
+
+ case '?':
+ return -EINVAL;
+
+ case ARG_VERSION:
+ version();
+ return 0;
+
+ case ARG_IGNORE_ENV:
+ p->ignore_env = true;
+ continue;
+
+ default:
+ log_error("Unknown option code %c", c);
+ return -EINVAL;
+ }
+ }
+
+ if (optind + 1 != argc && optind + 2 != argc) {
+ log_error("Incorrect number of positional arguments.");
+ help();
+ return -EINVAL;
+ }
+
+ p->remote_host = argv[optind];
+ assert(p->remote_host);
+
+ p->remote_is_inet = p->remote_host[0] != '/';
+
+ if (optind == argc - 2) {
+ if (!p->remote_is_inet) {
+ log_error("A port or service is not allowed for Unix socket destinations.");
+ help();
+ return -EINVAL;
+ }
+ p->remote_service = argv[optind + 1];
+ assert(p->remote_service);
+ } else if (p->remote_is_inet) {
+ log_error("A port or service is required for IP destinations.");
+ help();
+ return -EINVAL;
+ }
+
+ return 1;
+}
+
+int main(int argc, char *argv[]) {
+ struct proxy p = {};
+ int r;
+
+ log_parse_environment();
+ log_open();
+
+ r = parse_argv(argc, argv, &p);
+ if (r <= 0)
+ goto finish;
+
+ 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;
+ }
+ }
+
+ /* @TODO: Check if this proxy can work with datagram sockets. */
+ r = sd_is_socket(p.listen_fd, 0, SOCK_STREAM, 1);
+ if (r < 0) {
+ log_error("Error %d while checking inherited socket: %s", r, strerror(-r));
+ goto finish;
+ }
+
+ log_info("Starting the socket activation proxy with listener fd=%d.", p.listen_fd);
+
+ r = run_main_loop(&p);
+
+finish:
+ return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
+}
commit e905a456814eadfb904c49241e3841c1e4a1d119
Author: Lennart Poettering <lennart at poettering.net>
Date: Fri Oct 18 02:37:38 2013 +0200
build-sys: drop a number CFLAGS assignments in Makefile that are pointless
diff --git a/Makefile.am b/Makefile.am
index 06a806c..4a399c5 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -806,9 +806,6 @@ libsystemd_logs_la_SOURCES = \
src/shared/logs-show.c \
src/shared/logs-show.h
-libsystemd_logs_la_CFLAGS = \
- $(AM_CFLAGS)
-
libsystemd_logs_la_LIBADD = \
libsystemd-journal-internal.la \
libsystemd-id128-internal.la \
@@ -1253,36 +1250,24 @@ test_unit_file_LDADD = \
test_utf8_SOURCES = \
src/test/test-utf8.c
-test_utf8_CFLAGS = \
- $(AM_CFLAGS)
-
test_utf8_LDADD = \
libsystemd-shared.la
test_util_SOURCES = \
src/test/test-util.c
-test_util_CFLAGS = \
- $(AM_CFLAGS)
-
test_util_LDADD = \
libsystemd-core.la
test_hashmap_SOURCES = \
src/test/test-hashmap.c
-test_hashmap_CFLAGS = \
- $(AM_CFLAGS)
-
test_hashmap_LDADD = \
libsystemd-core.la
test_list_SOURCES = \
src/test/test-list.c
-test_list_CFLAGS = \
- $(AM_CFLAGS)
-
test_list_LDADD = \
libsystemd-core.la
@@ -1301,27 +1286,18 @@ test_tables_LDADD = \
test_prioq_SOURCES = \
src/test/test-prioq.c
-test_prioq_CFLAGS = \
- $(AM_CFLAGS)
-
test_prioq_LDADD = \
libsystemd-core.la
test_fileio_SOURCES = \
src/test/test-fileio.c
-test_fileio_CFLAGS = \
- $(AM_CFLAGS)
-
test_fileio_LDADD = \
libsystemd-core.la
test_time_SOURCES = \
src/test/test-time.c
-test_time_CFLAGS = \
- $(AM_CFLAGS)
-
test_time_LDADD = \
libsystemd-core.la
@@ -2078,9 +2054,6 @@ test_bus_objects_LDADD = \
test_bus_match_SOURCES = \
src/libsystemd-bus/test-bus-match.c
-test_bus_match_CFLAGS = \
- $(AM_CFLAGS)
-
test_bus_match_LDADD = \
libsystemd-shared.la \
libsystemd-bus.la \
@@ -2089,9 +2062,6 @@ test_bus_match_LDADD = \
test_bus_kernel_SOURCES = \
src/libsystemd-bus/test-bus-kernel.c
-test_bus_kernel_CFLAGS = \
- $(AM_CFLAGS)
-
test_bus_kernel_LDADD = \
libsystemd-shared.la \
libsystemd-bus.la \
@@ -2100,9 +2070,6 @@ test_bus_kernel_LDADD = \
test_bus_kernel_bloom_SOURCES = \
src/libsystemd-bus/test-bus-kernel-bloom.c
-test_bus_kernel_bloom_CFLAGS = \
- $(AM_CFLAGS)
-
test_bus_kernel_bloom_LDADD = \
libsystemd-shared.la \
libsystemd-bus.la \
@@ -2111,9 +2078,6 @@ test_bus_kernel_bloom_LDADD = \
test_bus_kernel_benchmark_SOURCES = \
src/libsystemd-bus/test-bus-kernel-benchmark.c
-test_bus_kernel_benchmark_CFLAGS = \
- $(AM_CFLAGS)
-
test_bus_kernel_benchmark_LDADD = \
libsystemd-shared.la \
libsystemd-bus.la \
@@ -2122,9 +2086,6 @@ test_bus_kernel_benchmark_LDADD = \
test_bus_memfd_SOURCES = \
src/libsystemd-bus/test-bus-memfd.c
-test_bus_memfd_CFLAGS = \
- $(AM_CFLAGS)
-
test_bus_memfd_LDADD = \
libsystemd-shared.la \
libsystemd-bus.la
@@ -2132,9 +2093,6 @@ test_bus_memfd_LDADD = \
test_bus_zero_copy_SOURCES = \
src/libsystemd-bus/test-bus-zero-copy.c
-test_bus_zero_copy_CFLAGS = \
- $(AM_CFLAGS)
-
test_bus_zero_copy_LDADD = \
libsystemd-shared.la \
libsystemd-bus.la
@@ -2142,9 +2100,6 @@ test_bus_zero_copy_LDADD = \
test_bus_introspect_SOURCES = \
src/libsystemd-bus/test-bus-introspect.c
-test_bus_introspect_CFLAGS = \
- $(AM_CFLAGS)
-
test_bus_introspect_LDADD = \
libsystemd-shared.la \
libsystemd-bus.la
@@ -2837,9 +2792,6 @@ systemd_cat_LDADD = \
journalctl_SOURCES = \
src/journal/journalctl.c
-journalctl_CFLAGS = \
- $(AM_CFLAGS)
-
journalctl_LDADD = \
libsystemd-shared.la \
libsystemd-journal-internal.la \
@@ -2856,7 +2808,7 @@ journalctl_SOURCES += \
src/journal/journal-qrcode.c \
src/journal/journal-qrcode.h
-journalctl_CFLAGS += \
+journalctl_CFLAGS = \
$(QRENCODE_CFLAGS)
journalctl_LDADD += \
@@ -3043,7 +2995,7 @@ libsystemd_journal_la_CFLAGS += \
libsystemd_journal_la_LIBADD += \
$(XZ_LIBS)
-libsystemd_journal_internal_la_CFLAGS += \
+libsystemd_journal_internal_la_CFLAGS = \
$(XZ_CFLAGS)
libsystemd_journal_internal_la_LIBADD += \
@@ -3726,9 +3678,6 @@ libnss_myhostname_la_SOURCES = \
src/nss-myhostname/ifconf.h \
src/nss-myhostname/netlink.c
-libnss_myhostname_la_CFLAGS = \
- $(AM_CFLAGS)
-
libnss_myhostname_la_LDFLAGS = \
$(AM_LDFLAGS) \
-module \
More information about the systemd-commits
mailing list