chiark / gitweb /
Do some basic compatibility checking when installing a new server
[disorder] / server / state.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2004, 2005, 2007-2009 Richard Kettlewell
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 3 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
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 /** @file server/state.c
19  * @brief Global server state
20  */
21 #include "disorder-server.h"
22
23 /** @brief Current AF_UNIX socket path */
24 static const char *current_unix;
25
26 /** @brief Current AF_UNIX socket */
27 static int current_unix_fd;
28
29 /** @brief TCP listener definition */
30 struct listener {
31   /** @brief Next listener */
32   struct listener *next;
33
34   /** @brief Local socket address */
35   struct sockaddr *sa;
36
37   /** @brief File descriptor */
38   int fd;
39 };
40
41 /** @brief Current listeners */
42 static struct listener *listeners;
43
44 /** @brief Current audio API */
45 const struct uaudio *api;
46
47 /** @brief Quit DisOrder */
48 void quit(ev_source *ev) {
49   info("shutting down...");
50   quitting(ev);
51   trackdb_close();
52   trackdb_deinit();
53   info("exiting");
54   exit(0);
55 }
56
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);
61   return sa;
62 }
63
64 /** @brief Create and destroy sockets to set current configuration */
65 static void reset_socket(ev_source *ev) {
66   const char *new_unix;
67   struct addrinfo *res, *r;
68   struct listener *l, **ll;
69   struct sockaddr_un sun;
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 */
100   if(config->listen.af != -1)
101     res = netaddress_resolve(&config->listen, 1, IPPROTO_TCP);
102   else
103     res = 0;
104
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;
118     }
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;
136       }
137       /* We ignore any failures (though server_start() will have
138        * logged them). */
139     }
140   }
141   /* if res is still set it needs freeing */
142   if(res)
143     freeaddrinfo(res);
144 }
145
146 /** @brief Reconfigure the server
147  * @param reload 0 at startup, 1 for a reload
148  */
149 int reconfigure(ev_source *ev, int reload) {
150   int need_another_rescan = 0;
151   int ret = 0;
152
153   D(("reconfigure(%d)", reload));
154   /* Deconfigure the old audio API if there is one */
155   if(api) {
156     if(api->close_mixer)
157       api->close_mixer();
158     api = NULL;
159   }
160   if(reload) {
161     /* If there's a rescan in progress, cancel it but remember to start a fresh
162      * one after the reload */
163     need_another_rescan = trackdb_rescan_cancel();
164     /* Close the track database */
165     trackdb_close();
166     /* (Re-)read the configuration */
167     if(config_read(1/*server*/, config))
168       ret = -1;
169     else {
170       /* Tell the speaker it needs to reload its config too. */
171       speaker_reload();
172       info("%s: installed new configuration", configfile);
173     }
174     /* Re-open track database.  We don't attempt to support database version
175      * upgrade at this point - the software hasn't changed, after all. */
176     trackdb_open(TRACKDB_NO_UPGRADE);
177   } else
178     /* We only allow for upgrade at startup */
179     trackdb_open(TRACKDB_CAN_UPGRADE);
180   /* New audio API */
181   api = uaudio_find(config->api);
182   if(api->configure)
183     api->configure();
184   if(api->open_mixer)
185     api->open_mixer();
186   /* If we interrupted a rescan of all the tracks, start a new one */
187   if(need_another_rescan)
188     trackdb_rescan(ev, 1/*check*/, 0, 0);
189   /* Arrange timeouts for schedule actions */
190   schedule_init(ev);
191   /* TODO what about leftover callbacks from before the reload? */
192   if(!ret) {
193     /* Read the queue back in */
194     queue_read();
195     recent_read();
196     /* Reset sockets */
197     reset_socket(ev);
198   }
199   /* TODO we need a careful think about what you can and cannot change with a
200    * reload.
201    *
202    * For instance saving and restoring the queue limits what we can usefuly
203    * keep in the queue data structure.  As a general rule, long-term state
204    * ought to be off-limits.  So 'home directory' will have to stay where it
205    * is.  (This actually means the AF_UNIX socket will never be re-opened.)
206    *
207    * Players certainly ought to be reconfigurable but this raises the ugly
208    * question of what to do about already-started background decoders.
209    *
210    * The audio API is easy to do for the server but a pain for the speaker.
211    *
212    * These two points suggest a general approach: if things that affect the
213    * speaker change, we could just restart it and any extant background
214    * decoders, at the next track change.  This would generate a bit of a gap
215    * but presumably reconfiguration is a rare event so this might be
216    * acceptable.
217    */
218   return ret;
219 }
220
221 /*
222 Local Variables:
223 c-basic-offset:2
224 comment-column:40
225 fill-column:79
226 indent-tabs-mode:nil
227 End:
228 */