2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2007-2009 Richard Kettlewell
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 3 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
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /** @file server/state.c
19 * @brief Global server state
21 #include "disorder-server.h"
23 /** @brief Current AF_UNIX socket path */
24 static const char *current_unix;
26 /** @brief Current AF_UNIX socket */
27 static int current_unix_fd;
29 /** @brief TCP listener definition */
31 /** @brief Next listener */
32 struct listener *next;
34 /** @brief Local socket address */
37 /** @brief File descriptor */
41 /** @brief Current listeners */
42 static struct listener *listeners;
44 /** @brief Current audio API */
45 const struct uaudio *api;
47 /** @brief Quit DisOrder */
48 void quit(ev_source *ev) {
49 info("shutting down...");
57 /** @brief Create a copy of an @c addrinfo structure */
58 static 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);
64 /** @brief Create and destroy sockets to set current configuration */
65 void reset_sockets(ev_source *ev) {
67 struct addrinfo *res, *r;
68 struct listener *l, **ll;
69 struct sockaddr_un sun;
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 */
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);
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,
92 current_unix = new_unix;
93 if(chmod(new_unix, 0777) < 0)
94 fatal(errno, "error calling chmod %s", new_unix);
99 /* get the new listen config */
100 if(config->listen.af != -1)
101 res = netaddress_resolve(&config->listen, 1, IPPROTO_TCP);
105 /* Close any current listeners that aren't required any more */
108 for(r = res; r; r = r->ai_next)
109 if(!sockaddrcmp(r->ai_addr, l->sa))
112 /* Didn't find a match, remove this one */
113 server_stop(ev, l->fd);
116 /* This address is still wanted */
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))
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));
131 l = xmalloc(sizeof *l);
133 l->sa = copy_sockaddr(r);
137 /* We ignore any failures (though server_start() will have
141 /* if res is still set it needs freeing */
146 /** @brief Reconfigure the server
148 * @return As config_read(); 0 on success, -1 if could not (re-)read config
150 int reconfigure(ev_source *ev, unsigned flags) {
151 int need_another_rescan = 0;
154 D(("reconfigure(%x)", flags));
155 /* Deconfigure the old audio API if there is one */
161 if(flags & RECONFIGURE_RELOADING) {
162 /* If there's a rescan in progress, cancel it but remember to start a fresh
163 * one after the reload. */
164 need_another_rescan = trackdb_rescan_cancel();
165 /* (Re-)read the configuration */
166 if(config_read(1/*server*/, config))
169 /* Tell the speaker it needs to reload its config too. */
171 info("%s: installed new configuration", configfile);
175 api = uaudio_find(config->api);
180 /* If we interrupted a rescan of all the tracks, start a new one */
181 if(need_another_rescan)
182 trackdb_rescan(ev, 1/*check*/, 0, 0);
183 if(!ret && !(flags & RECONFIGURE_FIRST)) {
184 /* Open/close sockets */