chiark / gitweb /
More reliable gapless play.
[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 static const char *current_unix;
24 static int current_unix_fd;
25
26 static struct addrinfo *current_listen_addrinfo;
27 static int current_listen_fd;
28
29 /** @brief Current audio API */
30 const struct uaudio *api;
31
32 void quit(ev_source *ev) {
33   info("shutting down...");
34   quitting(ev);
35   trackdb_close();
36   trackdb_deinit();
37   info("exiting");
38   exit(0);
39 }
40
41 static void reset_socket(ev_source *ev) {
42   const char *new_unix;
43   struct addrinfo *res;
44   struct sockaddr_un sun;
45   char *name;
46   
47   static const struct addrinfo pref = {
48     .ai_flags = AI_PASSIVE,
49     .ai_family = PF_INET,
50     .ai_socktype = SOCK_STREAM,
51     .ai_protocol = IPPROTO_TCP,
52   };
53
54   /* unix first */
55   new_unix = config_get_file("socket");
56   if(!current_unix || strcmp(current_unix, new_unix)) {
57     /* either there was no socket, or there was but a different path */
58     if(current_unix) {
59       /* stop the old one and remove it from the filesystem */
60       server_stop(ev, current_unix_fd);
61       if(unlink(current_unix) < 0)
62         fatal(errno, "unlink %s", current_unix);
63     }
64     /* start the new one */
65     if(strlen(new_unix) >= sizeof sun.sun_path)
66       fatal(0, "socket path %s is too long", new_unix);
67     memset(&sun, 0, sizeof sun);
68     sun.sun_family = AF_UNIX;
69     strcpy(sun.sun_path, new_unix);
70     if(unlink(new_unix) < 0 && errno != ENOENT)
71       fatal(errno, "unlink %s", new_unix);
72     if((current_unix_fd = server_start(ev, PF_UNIX, sizeof sun,
73                                        (const struct sockaddr *)&sun,
74                                        new_unix)) >= 0) {
75       current_unix = new_unix;
76       if(chmod(new_unix, 0777) < 0)
77         fatal(errno, "error calling chmod %s", new_unix);
78     } else
79       current_unix = 0;
80   }
81
82   /* get the new listen config */
83   if(config->listen.n)
84     res = get_address(&config->listen, &pref, &name);
85   else
86     res = 0;
87
88   if((res && !current_listen_addrinfo)
89      || (current_listen_addrinfo
90          && (!res
91              || addrinfocmp(res, current_listen_addrinfo)))) {
92     /* something has to change */
93     if(current_listen_addrinfo) {
94       /* delete the old listener */
95       server_stop(ev, current_listen_fd);
96       freeaddrinfo(current_listen_addrinfo);
97       current_listen_addrinfo = 0;
98     }
99     if(res) {
100       /* start the new listener */
101       if((current_listen_fd = server_start(ev, res->ai_family, res->ai_addrlen,
102                                            res->ai_addr, name)) >= 0) {
103         current_listen_addrinfo = res;
104         res = 0;
105       }
106     }
107   }
108   /* if res is still set it needs freeing */
109   if(res)
110     freeaddrinfo(res);
111 }
112
113 int reconfigure(ev_source *ev, int reload) {
114   int need_another_rescan = 0;
115   int ret = 0;
116
117   D(("reconfigure(%d)", reload));
118   if(api) {
119     if(api->close_mixer)
120       api->close_mixer();
121     api = NULL;
122   }
123   if(reload) {
124     need_another_rescan = trackdb_rescan_cancel();
125     trackdb_close();
126     if(config_read(1))
127       ret = -1;
128     else {
129       /* Tell the speaker it needs to reload its config too. */
130       speaker_reload();
131       info("%s: installed new configuration", configfile);
132     }
133     trackdb_open(TRACKDB_NO_UPGRADE);
134   } else
135     /* We only allow for upgrade at startup */
136     trackdb_open(TRACKDB_CAN_UPGRADE);
137   api = uaudio_find(config->api);
138   if(api->configure)
139     api->configure();
140   if(api->open_mixer)
141     api->open_mixer();
142   if(need_another_rescan)
143     trackdb_rescan(ev, 1/*check*/, 0, 0);
144   /* Arrange timeouts for schedule actions */
145   schedule_init(ev);
146   if(!ret) {
147     queue_read();
148     recent_read();
149     reset_socket(ev);
150   }
151   return ret;
152 }
153
154 /*
155 Local Variables:
156 c-basic-offset:2
157 comment-column:40
158 End:
159 */