chiark / gitweb /
Fix mis-merged trackdb_open().
[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) {
4363757e 49 info("shutting down...");
460b9539 50 quitting(ev);
51 trackdb_close();
52 trackdb_deinit();
4363757e 53 info("exiting");
445a0f66 54 exit(0);
460b9539 55}
56
e04f4cb5 57/** @brief Create a copy of an @c addrinfo structure */
80dc2c5f
RK
58static struct sockaddr *copy_sockaddr(const struct addrinfo *addr) {
59 struct sockaddr *sa = xmalloc_noptr(addr->ai_addrlen);
60 memcpy(sa, addr->ai_addr, addr->ai_addrlen);
61 return sa;
62}
63
e04f4cb5 64/** @brief Create and destroy sockets to set current configuration */
37f29ce9 65void reset_sockets(ev_source *ev) {
460b9539 66 const char *new_unix;
80dc2c5f
RK
67 struct addrinfo *res, *r;
68 struct listener *l, **ll;
460b9539 69 struct sockaddr_un sun;
460b9539 70
71 /* unix first */
72 new_unix = config_get_file("socket");
73 if(!current_unix || strcmp(current_unix, new_unix)) {
74 /* either there was no socket, or there was but a different path */
75 if(current_unix) {
76 /* stop the old one and remove it from the filesystem */
77 server_stop(ev, current_unix_fd);
78 if(unlink(current_unix) < 0)
79 fatal(errno, "unlink %s", current_unix);
80 }
81 /* start the new one */
82 if(strlen(new_unix) >= sizeof sun.sun_path)
83 fatal(0, "socket path %s is too long", new_unix);
84 memset(&sun, 0, sizeof sun);
85 sun.sun_family = AF_UNIX;
86 strcpy(sun.sun_path, new_unix);
87 if(unlink(new_unix) < 0 && errno != ENOENT)
88 fatal(errno, "unlink %s", new_unix);
89 if((current_unix_fd = server_start(ev, PF_UNIX, sizeof sun,
90 (const struct sockaddr *)&sun,
91 new_unix)) >= 0) {
92 current_unix = new_unix;
93 if(chmod(new_unix, 0777) < 0)
94 fatal(errno, "error calling chmod %s", new_unix);
95 } else
96 current_unix = 0;
97 }
98
99 /* get the new listen config */
80dc2c5f
RK
100 if(config->listen.af != -1)
101 res = netaddress_resolve(&config->listen, 1, IPPROTO_TCP);
460b9539 102 else
103 res = 0;
104
80dc2c5f
RK
105 /* Close any current listeners that aren't required any more */
106 ll = &listeners;
107 while((l = *ll)) {
108 for(r = res; r; r = r->ai_next)
109 if(!sockaddrcmp(r->ai_addr, l->sa))
110 break;
111 if(!r) {
112 /* Didn't find a match, remove this one */
113 server_stop(ev, l->fd);
114 *ll = l->next;
115 } else {
116 /* This address is still wanted */
117 ll = &l->next;
460b9539 118 }
80dc2c5f
RK
119 }
120
121 /* Open any new listeners that are required */
122 for(r = res; r; r = r->ai_next) {
123 for(l = listeners; l; l = l->next)
124 if(!sockaddrcmp(r->ai_addr, l->sa))
125 break;
126 if(!l) {
127 /* Didn't find a match, need a new listener */
128 int fd = server_start(ev, r->ai_family, r->ai_addrlen, r->ai_addr,
129 format_sockaddr(r->ai_addr));
130 if(fd >= 0) {
131 l = xmalloc(sizeof *l);
132 l->next = listeners;
133 l->sa = copy_sockaddr(r);
134 l->fd = fd;
135 listeners = l;
460b9539 136 }
e04f4cb5
RK
137 /* We ignore any failures (though server_start() will have
138 * logged them). */
460b9539 139 }
140 }
141 /* if res is still set it needs freeing */
142 if(res)
143 freeaddrinfo(res);
144}
145
e04f4cb5 146/** @brief Reconfigure the server
37f29ce9
RK
147 * @param flags Flags
148 * @return As config_read(); 0 on success, -1 if could not (re-)read config
e04f4cb5 149 */
37f29ce9 150int reconfigure(ev_source *ev, unsigned flags) {
460b9539 151 int need_another_rescan = 0;
152 int ret = 0;
153
37f29ce9 154 D(("reconfigure(%x)", flags));
e04f4cb5 155 /* Deconfigure the old audio API if there is one */
b50cfb8a
RK
156 if(api) {
157 if(api->close_mixer)
158 api->close_mixer();
159 api = NULL;
160 }
37f29ce9 161 if(flags & RECONFIGURE_RELOADING) {
e04f4cb5 162 /* If there's a rescan in progress, cancel it but remember to start a fresh
36c5e137 163 * one after the reload. */
460b9539 164 need_another_rescan = trackdb_rescan_cancel();
e04f4cb5 165 /* (Re-)read the configuration */
02ba7921 166 if(config_read(1/*server*/, config))
460b9539 167 ret = -1;
168 else {
169 /* Tell the speaker it needs to reload its config too. */
170 speaker_reload();
171 info("%s: installed new configuration", configfile);
172 }
36c5e137 173 }
e04f4cb5 174 /* New audio API */
b50cfb8a 175 api = uaudio_find(config->api);
ba70caca
RK
176 if(api->configure)
177 api->configure();
b50cfb8a
RK
178 if(api->open_mixer)
179 api->open_mixer();
e04f4cb5 180 /* If we interrupted a rescan of all the tracks, start a new one */
460b9539 181 if(need_another_rescan)
dd9af5cb 182 trackdb_rescan(ev, 1/*check*/, 0, 0);
37f29ce9
RK
183 if(!ret && !(flags & RECONFIGURE_FIRST)) {
184 /* Open/close sockets */
185 reset_sockets(ev);
460b9539 186 }
187 return ret;
188}
189
190/*
191Local Variables:
192c-basic-offset:2
193comment-column:40
e04f4cb5
RK
194fill-column:79
195indent-tabs-mode:nil
460b9539 196End:
197*/