qga: systemd socket activation for AF_UNIX and AF_VSOCK
cover qga: systemd socket activation for AF_UNIX and AF_VSOCK
These patches add optional systemd socket activation support to the QEMU guest agent. The listen socket is created by systemd. qemu-ga is only launched when the first client connects and the listen fd is passed in. The guest agent detects socket activation automatically on startup. There are no new command-line options.
I wrote this code to test systemd AF_VSOCK socket activation but it also works with AF_UNIX.
Stefan Hajnoczi (2):
configure: add libsystemd check
qga: add systemd socket activation support
configure | 34 +++++++++++++++++++++++++++ qga/Makefile.objs | 3 +++ qga/channel.h | 3 ++- qga/channel-posix.c | 66 ++++++++++++++++++++++++++++++----------------------- qga/channel-win32.c | 2 +- qga/main.c | 45 ++++++++++++++++++++++++++++++++---- 6 files changed, 118 insertions(+), 35 deletions(-)
Signature · 1 line
2.9.3
PATCH 1/2 configure: add libsystemd check
libsystemd provides APIs for daemons that wish to integrate socket activation and other systemd-related functionality. This will be used as an optional dependency by qemu-guest-agent.
In the future it could also be used for systemd logging or sd_notify(3) service startup notification.
--- configure | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)
configure +34 -0
@@ -321,6 +321,7 @@ numa="" tcmalloc="no" jemalloc="no" replication="yes" +systemd="" # parse CC options first for opt do
@@ -1168,6 +1169,10 @@ for opt do ;; --enable-replication) replication="yes" ;; + --disable-systemd) systemd="no" + ;; + --enable-systemd) systemd="yes" + ;; *) echo "ERROR: unknown option $opt" echo "Try '$0 --help' for more information"
@@ -1401,6 +1406,7 @@ disabled with --disable-FEATURE, default is enabled if available: tcmalloc tcmalloc support jemalloc jemalloc support replication replication support + systemd systemd support NOTE: The object files are built at the place where configure is launched EOF
@@ -4720,6 +4726,27 @@ if compile_prog "" "" ; then have_af_vsock=yes fi +########################################## +# check for libsystemd +if test "$systemd" != "no" ; then + cat > $TMPC << EOF +#include <systemd/sd-daemon.h> +int main(void) { (void)sd_listen_fds(1); return 0; } +EOF + systemd_cflags=$($pkg_config --cflags libsystemd 2>/dev/null) + systemd_libs=$($pkg_config --libs libsystemd 2>/dev/null) + if $pkg_config libsystemd >/dev/null 2>&1 && \ + compile_prog "$systemd_cflags" "$systemd_libs" ; then + systemd="yes" + else + if test "$systemd" = "yes" ; then + feature_not_found "systemd" + fi + systemd="no" + fi +fi + + ################################################# # Sparc implicitly links with --relax, which is # incompatible with -r, so --no-relax should be
@@ -5112,6 +5139,7 @@ echo "tcmalloc support $tcmalloc" echo "jemalloc support $jemalloc" echo "avx2 optimization $avx2_opt" echo "replication support $replication" +echo "systemd support $systemd" if test "$sdl_too_old" = "yes"; then echo "-> Your SDL version is too old - please upgrade to have SDL support"
@@ -5721,6 +5749,12 @@ if test "$have_af_vsock" = "yes" ; then echo "CONFIG_AF_VSOCK=y" >> $config_host_mak fi +if test "$systemd" = "yes" ; then + echo "CONFIG_SYSTEMD=y" >> $config_host_mak + echo "SYSTEMD_CFLAGS=$systemd_cflags" >> $config_host_mak + echo "SYSTEMD_LIBS=$systemd_libs" >> $config_host_mak +fi + # Hold two types of flag: # CONFIG_THREAD_SETNAME_BYTHREAD - we've got a way of setting the name on # a thread we have a handle to
Signature · 1 line
2.9.3
Re: [Qemu-devel] [PATCH 1/2] configure: add libsystemd check
Quoted 48 lines · Stefan Hajnoczi
libsystemd provides APIs for daemons that wish to integrate socket activation and other systemd-related functionality. This will be used as an optional dependency by qemu-guest-agent.
In the future it could also be used for systemd logging or
sd_notify(3)service startup notification.Signed-off-by Stefan Hajnoczi <[email protected]>--- configure | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)configure +7
-0@@ -321,6 +321,7 @@ numa="" tcmalloc="no" jemalloc="no" replication="yes" +systemd="" # parse CC options first for opt do@@ -1168,6 +1169,10 @@ for opt do ;; --enable-replication) replication="yes" ;; + --disable-systemd) systemd="no" + ;; + --enable-systemd) systemd="yes" + ;; *) echo "ERROR: unknown option $opt" echo "Try '$0 --help' for more information"@@ -1401,6 +1406,7 @@ disabled with --disable-FEATURE, default is enabled if available: tcmalloc tcmalloc support jemalloc jemalloc support replication replication support + systemd systemd support NOTE: The object files are built at the place where configure is launched EOF@@ -4720,6 +4726,27 @@ if compile_prog "" "" ; then have_af_vsock=yes fi +##########################################
configure if compile_prog "" "" ; then · line 4739
Quoted 10 lines
configure +10
-0+# check for libsystemd +if test "$systemd" != "no" ; then + cat > $TMPC << EOF +#include <systemd/sd-daemon.h> +int main(void) { (void)sd_listen_fds(1); return 0; } +EOF + systemd_cflags=$($pkg_config --cflags libsystemd 2>/dev/null) + systemd_libs=$($pkg_config --libs libsystemd 2>/dev/null) + if $pkg_config libsystemd >/dev/null 2>&1 && \ + compile_prog "$systemd_cflags" "$systemd_libs" ; then
Any reason why you're going to the trouble of compiling a program here ? The key benefit of pkg-config is that you don't need to do things like that to test for existance of the library - just trust the result of pkg-config check.
FWIW, it is easy to support socket activation without even using libsystemd. The systemd activation "protocol" is trivial - it merely sets "LISTEN_FDS" env variable to the number of FDs it has been passed down, and they are numbered from stderr fileno + 1.
Regards,
Daniel
Signature · 3 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://entangle-photo.org -o- http://search.cpan.org/~danberr/ :|
Re: [Qemu-devel] [PATCH 1/2] configure: add libsystemd check
Quoted 65 lines · Daniel P. Berrange
Quoted 48 lines · Stefan Hajnoczi
libsystemd provides APIs for daemons that wish to integrate socket activation and other systemd-related functionality. This will be used as an optional dependency by qemu-guest-agent.
In the future it could also be used for systemd logging or
sd_notify(3)service startup notification.Signed-off-by Stefan Hajnoczi <[email protected]>--- configure | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)configure +7
-0@@ -321,6 +321,7 @@ numa="" tcmalloc="no" jemalloc="no" replication="yes" +systemd="" # parse CC options first for opt do@@ -1168,6 +1169,10 @@ for opt do ;; --enable-replication) replication="yes" ;; + --disable-systemd) systemd="no" + ;; + --enable-systemd) systemd="yes" + ;; *) echo "ERROR: unknown option $opt" echo "Try '$0 --help' for more information"@@ -1401,6 +1406,7 @@ disabled with --disable-FEATURE, default is enabled if available: tcmalloc tcmalloc support jemalloc jemalloc support replication replication support + systemd systemd support NOTE: The object files are built at the place where configure is launched EOF@@ -4720,6 +4726,27 @@ if compile_prog "" "" ; then have_af_vsock=yes fi +##########################################configure if compile_prog "" "" ; then · line 4739
Quoted 10 lines
configure +10
-0+# check for libsystemd +if test "$systemd" != "no" ; then + cat > $TMPC << EOF +#include <systemd/sd-daemon.h> +int main(void) { (void)sd_listen_fds(1); return 0; } +EOF + systemd_cflags=$($pkg_config --cflags libsystemd 2>/dev/null) + systemd_libs=$($pkg_config --libs libsystemd 2>/dev/null) + if $pkg_config libsystemd >/dev/null 2>&1 && \ + compile_prog "$systemd_cflags" "$systemd_libs" ; thenAny reason why you're going to the trouble of compiling a program here ? The key benefit of pkg-config is that you don't need to do things like that to test for existance of the library - just trust the result of pkg-config check.
Quoted 5 lines
FWIW, it is easy to support socket activation without even using libsystemd. The systemd activation "protocol" is trivial - it merely sets "
LISTEN_FDS" env variable to the number of FDs it has been passed down, and they are numbered from stderr fileno + 1.
After looking into it I found that LISTEN_FDS is considered stable and "reimplementable independently":
https://www.freedesktop.org/wiki/Software/systemd/InterfacePortabilityAndStabilityChart/
I'll resend this patch without the libsystemd dependency.
I guess that will make packagers happy who wish to provide packages that work both with and without systemd installed.
Stefan
PATCH 2/2 qga: add systemd socket activation support
In reply to Stefan HajnocziAF_UNIX and AF_VSOCK listen sockets can be passed in by systemd on startup. This allows systemd to manage the listen socket until the first client connects and between restarts. Advantages of socket activation are that parallel startup of network services becomes possible and that unused daemons do not consume memory.
The key to achieving this is the sd_listen_fds(3) API provided by libsystemd. It returns zero if there are no passed listen fds. When passed fds are present they override the qga method/path options.
Test as follows:
$ cat ~/.config/systemd/user/qga.service [Unit] Description=qga
[Service] WorkingDirectory=/tmp ExecStart=/path/to/qemu-ga --logfile=/tmp/qga.log --pidfile=/tmp/qga.pid --statedir=/tmp
$ cat ~/.config/systemd/user/qga.socket [Socket] ListenStream=/tmp/qga.sock
[Install]
WantedBy=default.target
$ systemctl --user daemon-reload $ systemctl --user start qga.socket $ nc -U /tmp/qga.sock
--- qga/Makefile.objs | 3 +++ qga/channel.h | 3 ++- qga/channel-posix.c | 66 ++++++++++++++++++++++++++++++----------------------- qga/channel-win32.c | 2 +- qga/main.c | 45 ++++++++++++++++++++++++++++++++---- 5 files changed, 84 insertions(+), 35 deletions(-)
qga/Makefile.objs +3 -0
@@ -6,3 +6,6 @@ qga-obj-y += qapi-generated/qga-qapi-types.o qapi-generated/qga-qapi-visit.o qga-obj-y += qapi-generated/qga-qmp-marshal.o qga-vss-dll-obj-$(CONFIG_QGA_VSS) += vss-win32/ + +main.o-cflags := $(SYSTEMD_CFLAGS) +main.o-libs := $(SYSTEMD_LIBS)
qga/channel.h +2 -1
@@ -25,7 +25,8 @@ typedef enum {typedef gboolean (*GAChannelCallback )(GIOCondition condition,gpointer opaque);GAChannel *ga_channel_new (GAChannelMethod method,const gchar *path, -GAChannelCallback cb,gpointer opaque); +int listen_fd,GAChannelCallback cb, +gpointer opaque);void ga_channel_free (GAChannel *c);GIOStatus ga_channel_read (GAChannel *c,gchar *buf,gsize size,gsize *count);GIOStatus ga_channel_write_all (GAChannel *c,const gchar *buf,gsize size);
qga/channel-posix.c +37 -29
108 lines not on this page · raw message
qga/channel-win32.c +1 -1
@@ -316,7 +316,7 @@ static gboolean ga_channel_open(GAChannel *c, GAChannelMethod method, }GAChannel *ga_channel_new (GAChannelMethod method,const gchar *path, -GAChannelCallback cb,gpointer opaque) +int listen_fd,GAChannelCallback cb,gpointer opaque) {GAChannel *c =g_new0 (GAChannel,1 );SECURITY_ATTRIBUTES sec_attrs;
qga/main.c +41 -4
87 lines not on this page · raw message
Signature · 1 line
2.9.3