2
* Copyright (C) 2003-2013 The Music Player Daemon Project
3
* http://www.musicpd.org
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License along
16
* with this program; if not, write to the Free Software Foundation, Inc.,
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23
#include "Instance.hxx"
25
#include "ConfigData.hxx"
26
#include "ConfigGlobal.hxx"
27
#include "ConfigOption.hxx"
28
#include "event/ServerSocket.hxx"
29
#include "util/Error.hxx"
30
#include "util/Domain.hxx"
31
#include "fs/AllocatedPath.hxx"
37
#ifdef ENABLE_SYSTEMD_DAEMON
38
#include <systemd/sd-daemon.h>
41
static constexpr Domain listen_domain("listen");
43
#define DEFAULT_PORT 6600
45
class ClientListener final : public ServerSocket {
47
ClientListener():ServerSocket(*main_loop) {}
50
virtual void OnAccept(int fd, const sockaddr &address,
51
size_t address_length, int uid) {
52
client_new(*main_loop, *instance->partition,
53
fd, &address, address_length, uid);
57
static ClientListener *listen_socket;
61
listen_add_config_param(unsigned int port,
62
const struct config_param *param,
65
assert(param != nullptr);
67
if (0 == strcmp(param->value.c_str(), "any")) {
68
return listen_socket->AddPort(port, error_r);
69
} else if (param->value[0] == '/' || param->value[0] == '~') {
70
auto path = config_parse_path(param, error_r);
71
return !path.IsNull() &&
72
listen_socket->AddPath(std::move(path), error_r);
74
return listen_socket->AddHost(param->value.c_str(), port,
80
listen_systemd_activation(Error &error_r)
82
#ifdef ENABLE_SYSTEMD_DAEMON
83
int n = sd_listen_fds(true);
86
FormatErrno(listen_domain, -n,
87
"sd_listen_fds() failed");
91
for (int i = SD_LISTEN_FDS_START, end = SD_LISTEN_FDS_START + n;
93
if (!listen_socket->AddFD(i, error_r))
104
listen_global_init(Error &error)
106
assert(main_loop != nullptr);
108
int port = config_get_positive(CONF_PORT, DEFAULT_PORT);
109
const struct config_param *param =
110
config_get_next_param(CONF_BIND_TO_ADDRESS, nullptr);
113
listen_socket = new ClientListener();
115
if (listen_systemd_activation(error))
118
if (error.IsDefined())
121
if (param != nullptr) {
122
/* "bind_to_address" is configured, create listeners
126
if (!listen_add_config_param(port, param, error)) {
127
delete listen_socket;
128
error.FormatPrefix("Failed to listen on %s (line %i): ",
129
param->value.c_str(),
134
param = config_get_next_param(CONF_BIND_TO_ADDRESS,
136
} while (param != nullptr);
138
/* no "bind_to_address" configured, bind the
139
configured port on all interfaces */
141
success = listen_socket->AddPort(port, error);
143
delete listen_socket;
144
error.FormatPrefix("Failed to listen on *:%d: ", port);
149
if (!listen_socket->Open(error)) {
150
delete listen_socket;
158
void listen_global_finish(void)
160
LogDebug(listen_domain, "listen_global_finish called");
162
assert(listen_socket != nullptr);
164
delete listen_socket;