chiark / gitweb /
exit not _exit, for benefit of coverage testing
[disorder] / server / state.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2004, 2005 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 2 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, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * 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, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20
21 #include <config.h>
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <locale.h>
31 #include <stdio.h>
32 #include <pcre.h>
33 #include <netdb.h>
34 #include <sys/un.h>
35 #include <netinet/in.h>
36
37 #include "event.h"
38 #include "play.h"
39 #include "trackdb.h"
40 #include "state.h"
41 #include "configuration.h"
42 #include "log.h"
43 #include "queue.h"
44 #include "server-queue.h"
45 #include "server.h"
46 #include "printf.h"
47 #include "addr.h"
48
49 static const char *current_unix;
50 static int current_unix_fd;
51
52 static struct addrinfo *current_listen_addrinfo;
53 static int current_listen_fd;
54
55 void quit(ev_source *ev) {
56   quitting(ev);
57   trackdb_close();
58   trackdb_deinit();
59   info("terminating");
60   exit(0);
61 }
62
63 static void reset_socket(ev_source *ev) {
64   const char *new_unix;
65   struct addrinfo *res;
66   struct sockaddr_un sun;
67   char *name;
68   
69   static const struct addrinfo pref = {
70     AI_PASSIVE,
71     PF_INET,
72     SOCK_STREAM,
73     IPPROTO_TCP,
74     0,
75     0,
76     0,
77     0
78   };
79
80   /* unix first */
81   new_unix = config_get_file("socket");
82   if(!current_unix || strcmp(current_unix, new_unix)) {
83     /* either there was no socket, or there was but a different path */
84     if(current_unix) {
85       /* stop the old one and remove it from the filesystem */
86       server_stop(ev, current_unix_fd);
87       if(unlink(current_unix) < 0)
88         fatal(errno, "unlink %s", current_unix);
89     }
90     /* start the new one */
91     if(strlen(new_unix) >= sizeof sun.sun_path)
92       fatal(0, "socket path %s is too long", new_unix);
93     memset(&sun, 0, sizeof sun);
94     sun.sun_family = AF_UNIX;
95     strcpy(sun.sun_path, new_unix);
96     if(unlink(new_unix) < 0 && errno != ENOENT)
97       fatal(errno, "unlink %s", new_unix);
98     if((current_unix_fd = server_start(ev, PF_UNIX, sizeof sun,
99                                        (const struct sockaddr *)&sun,
100                                        new_unix)) >= 0) {
101       current_unix = new_unix;
102       if(chmod(new_unix, 0777) < 0)
103         fatal(errno, "error calling chmod %s", new_unix);
104     } else
105       current_unix = 0;
106   }
107
108   /* get the new listen config */
109   if(config->listen.n)
110     res = get_address(&config->listen, &pref, &name);
111   else
112     res = 0;
113
114   if((res && !current_listen_addrinfo)
115      || (current_listen_addrinfo
116          && (!res
117              || addrinfocmp(res, current_listen_addrinfo)))) {
118     /* something has to change */
119     if(current_listen_addrinfo) {
120       /* delete the old listener */
121       server_stop(ev, current_listen_fd);
122       freeaddrinfo(current_listen_addrinfo);
123       current_listen_addrinfo = 0;
124     }
125     if(res) {
126       /* start the new listener */
127       if((current_listen_fd = server_start(ev, res->ai_family, res->ai_addrlen,
128                                            res->ai_addr, name)) >= 0) {
129         current_listen_addrinfo = res;
130         res = 0;
131       }
132     }
133   }
134   /* if res is still set it needs freeing */
135   if(res)
136     freeaddrinfo(res);
137 }
138
139 int reconfigure(ev_source *ev, int reload) {
140   int need_another_rescan = 0;
141   int ret = 0;
142
143   D(("reconfigure(%d)", reload));
144   if(reload) {
145     need_another_rescan = trackdb_rescan_cancel();
146     trackdb_close();
147     if(config_read(1))
148       ret = -1;
149     else {
150       /* Tell the speaker it needs to reload its config too. */
151       speaker_reload();
152       info("%s: installed new configuration", configfile);
153     }
154     trackdb_open(TRACKDB_NO_UPGRADE);
155   } else
156     /* We only allow for upgrade at startup */
157     trackdb_open(TRACKDB_CAN_UPGRADE);
158   if(need_another_rescan)
159     trackdb_rescan(ev);
160   if(!ret) {
161     queue_read();
162     recent_read();
163     reset_socket(ev);
164   }
165   return ret;
166 }
167
168 /*
169 Local Variables:
170 c-basic-offset:2
171 comment-column:40
172 End:
173 */