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