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 static const char *current_unix;
24 static int current_unix_fd;
26 /** @brief TCP listener definition */
28 struct listener *next;
33 /** @brief Current listeners */
34 static struct listener *listeners;
36 /** @brief Current audio API */
37 const struct uaudio *api;
39 void quit(ev_source *ev) {
40 info("shutting down...");
48 static struct sockaddr *copy_sockaddr(const struct addrinfo *addr) {
49 struct sockaddr *sa = xmalloc_noptr(addr->ai_addrlen);
50 memcpy(sa, addr->ai_addr, addr->ai_addrlen);
54 static void reset_socket(ev_source *ev) {
56 struct addrinfo *res, *r;
57 struct listener *l, **ll;
58 struct sockaddr_un sun;
61 new_unix = config_get_file("socket");
62 if(!current_unix || strcmp(current_unix, new_unix)) {
63 /* either there was no socket, or there was but a different path */
65 /* stop the old one and remove it from the filesystem */
66 server_stop(ev, current_unix_fd);
67 if(unlink(current_unix) < 0)
68 fatal(errno, "unlink %s", current_unix);
70 /* start the new one */
71 if(strlen(new_unix) >= sizeof sun.sun_path)
72 fatal(0, "socket path %s is too long", new_unix);
73 memset(&sun, 0, sizeof sun);
74 sun.sun_family = AF_UNIX;
75 strcpy(sun.sun_path, new_unix);
76 if(unlink(new_unix) < 0 && errno != ENOENT)
77 fatal(errno, "unlink %s", new_unix);
78 if((current_unix_fd = server_start(ev, PF_UNIX, sizeof sun,
79 (const struct sockaddr *)&sun,
81 current_unix = new_unix;
82 if(chmod(new_unix, 0777) < 0)
83 fatal(errno, "error calling chmod %s", new_unix);
88 /* get the new listen config */
89 if(config->listen.af != -1)
90 res = netaddress_resolve(&config->listen, 1, IPPROTO_TCP);
94 /* Close any current listeners that aren't required any more */
97 for(r = res; r; r = r->ai_next)
98 if(!sockaddrcmp(r->ai_addr, l->sa))
101 /* Didn't find a match, remove this one */
102 server_stop(ev, l->fd);
105 /* This address is still wanted */
110 /* Open any new listeners that are required */
111 for(r = res; r; r = r->ai_next) {
112 for(l = listeners; l; l = l->next)
113 if(!sockaddrcmp(r->ai_addr, l->sa))
116 /* Didn't find a match, need a new listener */
117 int fd = server_start(ev, r->ai_family, r->ai_addrlen, r->ai_addr,
118 format_sockaddr(r->ai_addr));
120 l = xmalloc(sizeof *l);
122 l->sa = copy_sockaddr(r);
128 /* if res is still set it needs freeing */
133 int reconfigure(ev_source *ev, int reload) {
134 int need_another_rescan = 0;
137 D(("reconfigure(%d)", reload));
144 need_another_rescan = trackdb_rescan_cancel();
149 /* Tell the speaker it needs to reload its config too. */
151 info("%s: installed new configuration", configfile);
153 trackdb_open(TRACKDB_NO_UPGRADE);
155 /* We only allow for upgrade at startup */
156 trackdb_open(TRACKDB_CAN_UPGRADE);
157 api = uaudio_find(config->api);
162 if(need_another_rescan)
163 trackdb_rescan(ev, 1/*check*/, 0, 0);
164 /* Arrange timeouts for schedule actions */