From ce77b148d927c3012d04f629266476c8ffeb183a Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 5 Nov 2021 08:48:19 +0100 Subject: [PATCH] CommandLine: add option --systemd This way, MPD can reliably detect whether it was started as systemd service, which is better than checking sd_booted(), which only checks whether systemd manages all services, but still MPD could be started manually. --- src/CommandLine.cxx | 13 +++++++++++++ src/CommandLine.hxx | 5 +++++ systemd/system/mpd.service.in | 2 +- systemd/user/mpd.service.in | 2 +- 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/CommandLine.cxx b/src/CommandLine.cxx index e046d5ca4..eb47df39b 100644 --- a/src/CommandLine.cxx +++ b/src/CommandLine.cxx @@ -86,6 +86,9 @@ enum Option { OPTION_KILL, OPTION_NO_CONFIG, OPTION_NO_DAEMON, +#ifdef __linux__ + OPTION_SYSTEMD, +#endif OPTION_STDOUT, OPTION_STDERR, OPTION_VERBOSE, @@ -98,6 +101,9 @@ static constexpr OptionDef option_defs[] = { {"kill", "kill the currently running mpd session"}, {"no-config", "don't read from config"}, {"no-daemon", "don't detach from console"}, +#ifdef __linux__ + {"systemd", "systemd service mode"}, +#endif {"stdout", nullptr}, // hidden, compatibility with old versions {"stderr", "print messages to stderr"}, {"verbose", 'v', "verbose logging"}, @@ -349,6 +355,13 @@ ParseCommandLine(int argc, char **argv, struct options &options, options.daemon = false; break; +#ifdef __linux__ + case OPTION_SYSTEMD: + options.daemon = false; + options.systemd = true; + break; +#endif + case OPTION_STDOUT: case OPTION_STDERR: options.log_stderr = true; diff --git a/src/CommandLine.hxx b/src/CommandLine.hxx index b7f48e926..744341299 100644 --- a/src/CommandLine.hxx +++ b/src/CommandLine.hxx @@ -25,6 +25,11 @@ struct ConfigData; struct options { bool kill = false; bool daemon = true; + +#ifdef __linux__ + bool systemd = false; +#endif + bool log_stderr = false; bool verbose = false; }; diff --git a/systemd/system/mpd.service.in b/systemd/system/mpd.service.in index c54954246..e0707507d 100644 --- a/systemd/system/mpd.service.in +++ b/systemd/system/mpd.service.in @@ -5,7 +5,7 @@ After=network.target sound.target [Service] Type=notify -ExecStart=@prefix@/bin/mpd --no-daemon +ExecStart=@prefix@/bin/mpd --systemd # Create /run/mpd (if MPD is launched without the socket unit and is # configured to bind listener sockets there). diff --git a/systemd/user/mpd.service.in b/systemd/user/mpd.service.in index 8b5a39fd4..6f8474e02 100644 --- a/systemd/user/mpd.service.in +++ b/systemd/user/mpd.service.in @@ -5,7 +5,7 @@ After=network.target sound.target [Service] Type=notify -ExecStart=@prefix@/bin/mpd --no-daemon +ExecStart=@prefix@/bin/mpd --systemd # Create /run/user/$UID/mpd (if MPD is launched without the socket # unit and is configured to bind listener sockets there).