2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2022-07-14 17:58:12 +02:00
|
|
|
* Copyright 2003-2022 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2004-02-24 00:41:20 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2004-02-24 00:41:20 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-01-03 11:05:44 +01:00
|
|
|
#include "Listen.hxx"
|
2018-10-30 18:37:32 +01:00
|
|
|
#include "Log.hxx"
|
2018-01-29 23:48:16 +01:00
|
|
|
#include "client/Listener.hxx"
|
2015-01-21 21:14:25 +01:00
|
|
|
#include "config/Param.hxx"
|
2018-07-17 23:02:53 +02:00
|
|
|
#include "config/Data.hxx"
|
2018-07-16 19:50:07 +02:00
|
|
|
#include "config/Option.hxx"
|
2018-08-07 19:14:14 +02:00
|
|
|
#include "config/Net.hxx"
|
2021-10-13 16:08:48 +02:00
|
|
|
#include "lib/fmt/ExceptionFormatter.hxx"
|
|
|
|
#include "lib/fmt/PathFormatter.hxx"
|
2018-10-30 18:37:32 +01:00
|
|
|
#include "net/AllocatedSocketAddress.hxx"
|
2018-10-30 20:05:57 +01:00
|
|
|
#include "net/UniqueSocketDescriptor.hxx"
|
2018-10-30 18:37:32 +01:00
|
|
|
#include "net/SocketUtil.hxx"
|
2016-10-28 10:36:05 +02:00
|
|
|
#include "system/Error.hxx"
|
2013-10-17 21:59:35 +02:00
|
|
|
#include "fs/AllocatedPath.hxx"
|
2021-10-26 09:04:42 +02:00
|
|
|
#include "fs/StandardDirectory.hxx"
|
2018-10-31 14:30:34 +01:00
|
|
|
#include "fs/XDG.hxx"
|
2021-10-13 16:08:48 +02:00
|
|
|
#include "util/Domain.hxx"
|
|
|
|
#include "util/RuntimeError.hxx"
|
2008-12-29 17:28:32 +01:00
|
|
|
|
2018-10-30 18:37:32 +01:00
|
|
|
#include <sys/stat.h>
|
2008-12-30 19:07:10 +01:00
|
|
|
|
2012-02-13 20:48:51 +01:00
|
|
|
#ifdef ENABLE_SYSTEMD_DAEMON
|
|
|
|
#include <systemd/sd-daemon.h>
|
|
|
|
#endif
|
|
|
|
|
2005-03-06 20:00:58 +01:00
|
|
|
#define DEFAULT_PORT 6600
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2021-10-13 16:08:48 +02:00
|
|
|
#if defined(USE_XDG) && defined(HAVE_UN)
|
|
|
|
static constexpr Domain listen_domain("listen");
|
|
|
|
#endif
|
|
|
|
|
2013-01-30 13:20:27 +01:00
|
|
|
int listen_port;
|
2008-10-30 18:03:18 +01:00
|
|
|
|
2014-02-19 23:49:34 +01:00
|
|
|
#ifdef ENABLE_SYSTEMD_DAEMON
|
|
|
|
|
2012-02-13 20:48:51 +01:00
|
|
|
static bool
|
2018-01-29 23:53:52 +01:00
|
|
|
listen_systemd_activation(ClientListener &listener)
|
2012-02-13 20:48:51 +01:00
|
|
|
{
|
|
|
|
int n = sd_listen_fds(true);
|
|
|
|
if (n <= 0) {
|
|
|
|
if (n < 0)
|
2016-10-28 10:36:05 +02:00
|
|
|
throw MakeErrno(-n, "sd_listen_fds() failed");
|
2012-02-13 20:48:51 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = SD_LISTEN_FDS_START, end = SD_LISTEN_FDS_START + n;
|
|
|
|
i != end; ++i)
|
2018-10-30 20:05:57 +01:00
|
|
|
listener.AddFD(UniqueSocketDescriptor(i));
|
2012-02-13 20:48:51 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-19 23:49:34 +01:00
|
|
|
#endif
|
|
|
|
|
2018-10-30 18:37:32 +01:00
|
|
|
/**
|
|
|
|
* Listen on "$XDG_RUNTIME_DIR/mpd/socket" (if applicable).
|
|
|
|
*
|
|
|
|
* @return true if a listener socket was added
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
ListenXdgRuntimeDir(ClientListener &listener) noexcept
|
|
|
|
{
|
2018-10-31 14:30:34 +01:00
|
|
|
#if defined(USE_XDG) && defined(HAVE_UN)
|
2018-10-30 18:37:32 +01:00
|
|
|
if (geteuid() == 0)
|
|
|
|
/* this MPD instance is a system-wide daemon; don't
|
|
|
|
use $XDG_RUNTIME_DIR */
|
|
|
|
return false;
|
|
|
|
|
2021-10-26 09:15:25 +02:00
|
|
|
const auto mpd_runtime_dir = GetAppRuntimeDir();
|
|
|
|
if (mpd_runtime_dir.IsNull())
|
2018-10-30 18:37:32 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
const auto socket_path = mpd_runtime_dir / Path::FromFS("socket");
|
|
|
|
unlink(socket_path.c_str());
|
|
|
|
|
|
|
|
AllocatedSocketAddress address;
|
|
|
|
address.SetLocal(socket_path.c_str());
|
|
|
|
|
|
|
|
try {
|
|
|
|
auto fd = socket_bind_listen(AF_LOCAL, SOCK_STREAM, 0,
|
|
|
|
address, 5);
|
|
|
|
chmod(socket_path.c_str(), 0600);
|
|
|
|
listener.AddFD(std::move(fd), std::move(address));
|
|
|
|
return true;
|
|
|
|
} catch (...) {
|
2021-10-13 16:08:48 +02:00
|
|
|
FmtError(listen_domain,
|
|
|
|
"Failed to listen on '{}' (not fatal): {}",
|
|
|
|
socket_path, std::current_exception());
|
2018-10-30 18:37:32 +01:00
|
|
|
return false;
|
|
|
|
}
|
2018-10-31 14:30:34 +01:00
|
|
|
#else
|
|
|
|
(void)listener;
|
|
|
|
return false;
|
2018-10-30 18:37:32 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-10-28 10:36:05 +02:00
|
|
|
void
|
2018-07-17 23:02:53 +02:00
|
|
|
listen_global_init(const ConfigData &config, ClientListener &listener)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2018-07-17 23:02:53 +02:00
|
|
|
int port = config.GetPositive(ConfigOption::PORT, DEFAULT_PORT);
|
2007-01-11 21:41:17 +01:00
|
|
|
|
2014-02-19 23:49:34 +01:00
|
|
|
#ifdef ENABLE_SYSTEMD_DAEMON
|
2018-01-29 23:53:52 +01:00
|
|
|
if (listen_systemd_activation(listener))
|
2016-10-28 10:36:05 +02:00
|
|
|
return;
|
2014-02-19 23:49:34 +01:00
|
|
|
#endif
|
2012-02-13 20:48:51 +01:00
|
|
|
|
2018-07-18 11:03:19 +02:00
|
|
|
for (const auto ¶m : config.GetParamList(ConfigOption::BIND_TO_ADDRESS)) {
|
2018-07-15 21:34:16 +02:00
|
|
|
try {
|
2018-10-30 21:14:39 +01:00
|
|
|
ServerSocketAddGeneric(listener, param.value.c_str(),
|
|
|
|
port);
|
2018-07-15 21:34:16 +02:00
|
|
|
} catch (...) {
|
|
|
|
std::throw_with_nested(FormatRuntimeError("Failed to listen on %s (line %i)",
|
2018-07-18 11:03:19 +02:00
|
|
|
param.value.c_str(),
|
|
|
|
param.line));
|
2018-07-15 21:34:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 18:37:32 +01:00
|
|
|
bool have_xdg_runtime_listener = false;
|
|
|
|
|
2018-07-15 21:34:16 +02:00
|
|
|
if (listener.IsEmpty()) {
|
2009-02-24 18:36:31 +01:00
|
|
|
/* no "bind_to_address" configured, bind the
|
|
|
|
configured port on all interfaces */
|
|
|
|
|
2018-10-30 18:37:32 +01:00
|
|
|
have_xdg_runtime_listener = ListenXdgRuntimeDir(listener);
|
|
|
|
|
2016-10-28 10:36:05 +02:00
|
|
|
try {
|
2018-01-29 23:53:52 +01:00
|
|
|
listener.AddPort(port);
|
2017-12-19 10:56:23 +01:00
|
|
|
} catch (...) {
|
2016-10-28 10:36:05 +02:00
|
|
|
std::throw_with_nested(FormatRuntimeError("Failed to listen on *:%d: ", port));
|
2009-09-24 21:40:04 +02:00
|
|
|
}
|
2009-02-24 18:36:31 +01:00
|
|
|
}
|
|
|
|
|
2018-10-30 18:37:32 +01:00
|
|
|
try {
|
|
|
|
listener.Open();
|
|
|
|
} catch (...) {
|
|
|
|
if (have_xdg_runtime_listener)
|
|
|
|
LogError(std::current_exception(),
|
|
|
|
"Default TCP listener setup failed, but this is okay because we have a $XDG_RUNTIME_DIR listener");
|
|
|
|
else
|
|
|
|
throw;
|
|
|
|
}
|
2010-10-05 20:29:41 +02:00
|
|
|
|
2009-02-24 17:42:36 +01:00
|
|
|
listen_port = port;
|
2004-11-03 20:42:54 +01:00
|
|
|
}
|