~ubuntu-branches/ubuntu/utopic/mpd/utopic-proposed

« back to all changes in this revision

Viewing changes to src/Listen.cxx

  • Committer: Package Import Robot
  • Author(s): Steve Kowalik
  • Date: 2013-11-12 18:17:40 UTC
  • mfrom: (2.2.36 sid)
  • Revision ID: package-import@ubuntu.com-20131112181740-72aa4zihehoobedp
Tags: 0.18.3-1ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - Add libmp3lame-dev to Build-Depends, and enable LAME.
  - Read the user for the daemon from the config file in the init script.
  - Move avahi-daemon from Suggests to Recommends.
  - Added apport hook to include user configuration file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
 
3
 * http://www.musicpd.org
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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.
 
18
 */
 
19
 
 
20
#include "config.h"
 
21
#include "Listen.hxx"
 
22
#include "Main.hxx"
 
23
#include "Instance.hxx"
 
24
#include "Client.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"
 
32
#include "Log.hxx"
 
33
 
 
34
#include <string.h>
 
35
#include <assert.h>
 
36
 
 
37
#ifdef ENABLE_SYSTEMD_DAEMON
 
38
#include <systemd/sd-daemon.h>
 
39
#endif
 
40
 
 
41
static constexpr Domain listen_domain("listen");
 
42
 
 
43
#define DEFAULT_PORT    6600
 
44
 
 
45
class ClientListener final : public ServerSocket {
 
46
public:
 
47
        ClientListener():ServerSocket(*main_loop) {}
 
48
 
 
49
private:
 
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);
 
54
        }
 
55
};
 
56
 
 
57
static ClientListener *listen_socket;
 
58
int listen_port;
 
59
 
 
60
static bool
 
61
listen_add_config_param(unsigned int port,
 
62
                        const struct config_param *param,
 
63
                        Error &error_r)
 
64
{
 
65
        assert(param != nullptr);
 
66
 
 
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);
 
73
        } else {
 
74
                return listen_socket->AddHost(param->value.c_str(), port,
 
75
                                              error_r);
 
76
        }
 
77
}
 
78
 
 
79
static bool
 
80
listen_systemd_activation(Error &error_r)
 
81
{
 
82
#ifdef ENABLE_SYSTEMD_DAEMON
 
83
        int n = sd_listen_fds(true);
 
84
        if (n <= 0) {
 
85
                if (n < 0)
 
86
                        FormatErrno(listen_domain, -n,
 
87
                                    "sd_listen_fds() failed");
 
88
                return false;
 
89
        }
 
90
 
 
91
        for (int i = SD_LISTEN_FDS_START, end = SD_LISTEN_FDS_START + n;
 
92
             i != end; ++i)
 
93
                if (!listen_socket->AddFD(i, error_r))
 
94
                        return false;
 
95
 
 
96
        return true;
 
97
#else
 
98
        (void)error_r;
 
99
        return false;
 
100
#endif
 
101
}
 
102
 
 
103
bool
 
104
listen_global_init(Error &error)
 
105
{
 
106
        assert(main_loop != nullptr);
 
107
 
 
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);
 
111
        bool success;
 
112
 
 
113
        listen_socket = new ClientListener();
 
114
 
 
115
        if (listen_systemd_activation(error))
 
116
                return true;
 
117
 
 
118
        if (error.IsDefined())
 
119
                return false;
 
120
 
 
121
        if (param != nullptr) {
 
122
                /* "bind_to_address" is configured, create listeners
 
123
                   for all values */
 
124
 
 
125
                do {
 
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(),
 
130
                                                   param->line);
 
131
                                return false;
 
132
                        }
 
133
 
 
134
                        param = config_get_next_param(CONF_BIND_TO_ADDRESS,
 
135
                                                      param);
 
136
                } while (param != nullptr);
 
137
        } else {
 
138
                /* no "bind_to_address" configured, bind the
 
139
                   configured port on all interfaces */
 
140
 
 
141
                success = listen_socket->AddPort(port, error);
 
142
                if (!success) {
 
143
                        delete listen_socket;
 
144
                        error.FormatPrefix("Failed to listen on *:%d: ", port);
 
145
                        return false;
 
146
                }
 
147
        }
 
148
 
 
149
        if (!listen_socket->Open(error)) {
 
150
                delete listen_socket;
 
151
                return false;
 
152
        }
 
153
 
 
154
        listen_port = port;
 
155
        return true;
 
156
}
 
157
 
 
158
void listen_global_finish(void)
 
159
{
 
160
        LogDebug(listen_domain, "listen_global_finish called");
 
161
 
 
162
        assert(listen_socket != nullptr);
 
163
 
 
164
        delete listen_socket;
 
165
}