Skip to content

Commit

Permalink
CommandLine: add option --systemd
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MaxKellermann committed Nov 5, 2021
1 parent be3eca3 commit ce77b14
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/CommandLine.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"},
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/CommandLine.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
2 changes: 1 addition & 1 deletion systemd/system/mpd.service.in
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion systemd/user/mpd.service.in
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down

0 comments on commit ce77b14

Please sign in to comment.