chiark / gitweb /
Use libsamplerate in disorder-normalize, if available. If it's not
[disorder] / server / state.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
b50cfb8a 3 * Copyright (C) 2004, 2005, 2007-2009 Richard Kettlewell
460b9539 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
460b9539 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
460b9539 8 * (at your option) any later version.
9 *
e7eb3a27
RK
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 *
460b9539 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
460b9539 17 */
132a5a4a
RK
18/** @file server/state.c
19 * @brief Global server state
20 */
05b75f8d 21#include "disorder-server.h"
460b9539 22
e04f4cb5 23/** @brief Current AF_UNIX socket path */
460b9539 24static const char *current_unix;
e04f4cb5
RK
25
26/** @brief Current AF_UNIX socket */
460b9539 27static int current_unix_fd;
28
80dc2c5f
RK
29/** @brief TCP listener definition */
30struct listener {
e04f4cb5 31 /** @brief Next listener */
80dc2c5f 32 struct listener *next;
e04f4cb5
RK
33
34 /** @brief Local socket address */
80dc2c5f 35 struct sockaddr *sa;
e04f4cb5
RK
36
37 /** @brief File descriptor */
80dc2c5f
RK
38 int fd;
39};
40
41/** @brief Current listeners */
42static struct listener *listeners;
460b9539 43
b50cfb8a
RK
44/** @brief Current audio API */
45const struct uaudio *api;
46
e04f4cb5 47/** @brief Quit DisOrder */
460b9539 48void quit(ev_source *ev) {
2e9ba080 49 disorder_info("shutting down...");
460b9539 50 quitting(ev);
51 trackdb_close();
8cd7d4bc 52 trackdb_deinit(ev);
18ed984a
RK
53 /* Shutdown subprocesses.
54 *
55 * Subprocesses that use ev_child:
56 * - the speaker
57 * - the current rescan
58 * - any decoders
59 * - ...and players
60 * - the track picker
61 * - mail sender
62 * - the deadlock manager
63 *
64 * Subprocesses that don't:
65 * - any normalizers
66 * These are not shut down currently.
67 */
68 ev_child_killall(ev);
2e9ba080 69 disorder_info("exiting");
445a0f66 70 exit(0);
460b9539 71}
72
e04f4cb5 73/** @brief Create a copy of an @c addrinfo structure */
80dc2c5f
RK
74static struct sockaddr *copy_sockaddr(const struct addrinfo *addr) {
75 struct sockaddr *sa = xmalloc_noptr(addr->ai_addrlen);
76 memcpy(sa, addr->ai_addr, addr->ai_addrlen);
77 return sa;
78}
79
e04f4cb5 80/** @brief Create and destroy sockets to set current configuration */
37f29ce9 81void reset_sockets(ev_source *ev) {
460b9539 82 const char *new_unix;
80dc2c5f
RK
83 struct addrinfo *res, *r;
84 struct listener *l, **ll;
460b9539 85 struct sockaddr_un sun;
460b9539 86
87 /* unix first */
88 new_unix = config_get_file("socket");
89 if(!current_unix || strcmp(current_unix, new_unix)) {
90 /* either there was no socket, or there was but a different path */
91 if(current_unix) {
92 /* stop the old one and remove it from the filesystem */
93 server_stop(ev, current_unix_fd);
94 if(unlink(current_unix) < 0)
2e9ba080 95 disorder_fatal(errno, "unlink %s", current_unix);
460b9539 96 }
97 /* start the new one */
98 if(strlen(new_unix) >= sizeof sun.sun_path)
2e9ba080 99 disorder_fatal(0, "socket path %s is too long", new_unix);
460b9539 100 memset(&sun, 0, sizeof sun);
101 sun.sun_family = AF_UNIX;
102 strcpy(sun.sun_path, new_unix);
103 if(unlink(new_unix) < 0 && errno != ENOENT)
2e9ba080 104 disorder_fatal(errno, "unlink %s", new_unix);
460b9539 105 if((current_unix_fd = server_start(ev, PF_UNIX, sizeof sun,
106 (const struct sockaddr *)&sun,
107 new_unix)) >= 0) {
108 current_unix = new_unix;
109 if(chmod(new_unix, 0777) < 0)
2e9ba080 110 disorder_fatal(errno, "error calling chmod %s", new_unix);
460b9539 111 } else
112 current_unix = 0;
113 }
114
115 /* get the new listen config */
80dc2c5f
RK
116 if(config->listen.af != -1)
117 res = netaddress_resolve(&config->listen, 1, IPPROTO_TCP);
460b9539 118 else
119 res = 0;
120
80dc2c5f
RK
121 /* Close any current listeners that aren't required any more */
122 ll = &listeners;
123 while((l = *ll)) {
124 for(r = res; r; r = r->ai_next)
125 if(!sockaddrcmp(r->ai_addr, l->sa))
126 break;
127 if(!r) {
128 /* Didn't find a match, remove this one */
129 server_stop(ev, l->fd);
130 *ll = l->next;
131 } else {
132 /* This address is still wanted */
133 ll = &l->next;
460b9539 134 }
80dc2c5f
RK
135 }
136
137 /* Open any new listeners that are required */
138 for(r = res; r; r = r->ai_next) {
139 for(l = listeners; l; l = l->next)
140 if(!sockaddrcmp(r->ai_addr, l->sa))
141 break;
142 if(!l) {
143 /* Didn't find a match, need a new listener */
144 int fd = server_start(ev, r->ai_family, r->ai_addrlen, r->ai_addr,
145 format_sockaddr(r->ai_addr));
146 if(fd >= 0) {
147 l = xmalloc(sizeof *l);
148 l->next = listeners;
149 l->sa = copy_sockaddr(r);
150 l->fd = fd;
151 listeners = l;
460b9539 152 }
e04f4cb5
RK
153 /* We ignore any failures (though server_start() will have
154 * logged them). */
460b9539 155 }
156 }
157 /* if res is still set it needs freeing */
158 if(res)
159 freeaddrinfo(res);
160}
161
e04f4cb5 162/** @brief Reconfigure the server
c0f52b2c 163 * @param ev Event loop
37f29ce9
RK
164 * @param flags Flags
165 * @return As config_read(); 0 on success, -1 if could not (re-)read config
e04f4cb5 166 */
37f29ce9 167int reconfigure(ev_source *ev, unsigned flags) {
460b9539 168 int need_another_rescan = 0;
169 int ret = 0;
170
37f29ce9 171 D(("reconfigure(%x)", flags));
e04f4cb5 172 /* Deconfigure the old audio API if there is one */
b50cfb8a
RK
173 if(api) {
174 if(api->close_mixer)
175 api->close_mixer();
176 api = NULL;
177 }
37f29ce9 178 if(flags & RECONFIGURE_RELOADING) {
e04f4cb5 179 /* If there's a rescan in progress, cancel it but remember to start a fresh
36c5e137 180 * one after the reload. */
460b9539 181 need_another_rescan = trackdb_rescan_cancel();
e04f4cb5 182 /* (Re-)read the configuration */
02ba7921 183 if(config_read(1/*server*/, config))
460b9539 184 ret = -1;
185 else {
186 /* Tell the speaker it needs to reload its config too. */
187 speaker_reload();
2e9ba080 188 disorder_info("%s: installed new configuration", configfile);
460b9539 189 }
36c5e137 190 }
e04f4cb5 191 /* New audio API */
b50cfb8a 192 api = uaudio_find(config->api);
ba70caca
RK
193 if(api->configure)
194 api->configure();
b50cfb8a
RK
195 if(api->open_mixer)
196 api->open_mixer();
e04f4cb5 197 /* If we interrupted a rescan of all the tracks, start a new one */
460b9539 198 if(need_another_rescan)
dd9af5cb 199 trackdb_rescan(ev, 1/*check*/, 0, 0);
37f29ce9
RK
200 if(!ret && !(flags & RECONFIGURE_FIRST)) {
201 /* Open/close sockets */
202 reset_sockets(ev);
460b9539 203 }
204 return ret;
205}
206
207/*
208Local Variables:
209c-basic-offset:2
210comment-column:40
e04f4cb5
RK
211fill-column:79
212indent-tabs-mode:nil
460b9539 213End:
214*/