chiark / gitweb /
Switch to GPL v3
[disorder] / server / state.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
5aff007d 3 * Copyright (C) 2004, 2005, 2007, 2008 Richard Kettlewell
460b9539 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
460b9539 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
460b9539 8 * (at your option) any later version.
9 *
e7eb3a27
RK
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 *
460b9539 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
460b9539 17 */
18
05b75f8d 19#include "disorder-server.h"
460b9539 20
21static const char *current_unix;
22static int current_unix_fd;
23
24static struct addrinfo *current_listen_addrinfo;
25static int current_listen_fd;
26
27void quit(ev_source *ev) {
4363757e 28 info("shutting down...");
460b9539 29 quitting(ev);
30 trackdb_close();
31 trackdb_deinit();
4363757e 32 info("exiting");
445a0f66 33 exit(0);
460b9539 34}
35
36static void reset_socket(ev_source *ev) {
37 const char *new_unix;
38 struct addrinfo *res;
39 struct sockaddr_un sun;
40 char *name;
41
42 static const struct addrinfo pref = {
66613034
RK
43 .ai_flags = AI_PASSIVE,
44 .ai_family = PF_INET,
45 .ai_socktype = SOCK_STREAM,
46 .ai_protocol = IPPROTO_TCP,
460b9539 47 };
48
49 /* unix first */
50 new_unix = config_get_file("socket");
51 if(!current_unix || strcmp(current_unix, new_unix)) {
52 /* either there was no socket, or there was but a different path */
53 if(current_unix) {
54 /* stop the old one and remove it from the filesystem */
55 server_stop(ev, current_unix_fd);
56 if(unlink(current_unix) < 0)
57 fatal(errno, "unlink %s", current_unix);
58 }
59 /* start the new one */
60 if(strlen(new_unix) >= sizeof sun.sun_path)
61 fatal(0, "socket path %s is too long", new_unix);
62 memset(&sun, 0, sizeof sun);
63 sun.sun_family = AF_UNIX;
64 strcpy(sun.sun_path, new_unix);
65 if(unlink(new_unix) < 0 && errno != ENOENT)
66 fatal(errno, "unlink %s", new_unix);
67 if((current_unix_fd = server_start(ev, PF_UNIX, sizeof sun,
68 (const struct sockaddr *)&sun,
69 new_unix)) >= 0) {
70 current_unix = new_unix;
71 if(chmod(new_unix, 0777) < 0)
72 fatal(errno, "error calling chmod %s", new_unix);
73 } else
74 current_unix = 0;
75 }
76
77 /* get the new listen config */
78 if(config->listen.n)
79 res = get_address(&config->listen, &pref, &name);
80 else
81 res = 0;
82
83 if((res && !current_listen_addrinfo)
84 || (current_listen_addrinfo
85 && (!res
86 || addrinfocmp(res, current_listen_addrinfo)))) {
87 /* something has to change */
88 if(current_listen_addrinfo) {
89 /* delete the old listener */
90 server_stop(ev, current_listen_fd);
91 freeaddrinfo(current_listen_addrinfo);
92 current_listen_addrinfo = 0;
93 }
94 if(res) {
95 /* start the new listener */
96 if((current_listen_fd = server_start(ev, res->ai_family, res->ai_addrlen,
97 res->ai_addr, name)) >= 0) {
98 current_listen_addrinfo = res;
99 res = 0;
100 }
101 }
102 }
103 /* if res is still set it needs freeing */
104 if(res)
105 freeaddrinfo(res);
106}
107
108int reconfigure(ev_source *ev, int reload) {
109 int need_another_rescan = 0;
110 int ret = 0;
111
112 D(("reconfigure(%d)", reload));
113 if(reload) {
114 need_another_rescan = trackdb_rescan_cancel();
115 trackdb_close();
c00fce3a 116 if(config_read(1))
460b9539 117 ret = -1;
118 else {
119 /* Tell the speaker it needs to reload its config too. */
120 speaker_reload();
121 info("%s: installed new configuration", configfile);
122 }
d25c4615
RK
123 trackdb_open(TRACKDB_NO_UPGRADE);
124 } else
125 /* We only allow for upgrade at startup */
126 trackdb_open(TRACKDB_CAN_UPGRADE);
460b9539 127 if(need_another_rescan)
dd9af5cb 128 trackdb_rescan(ev, 1/*check*/, 0, 0);
fdca70ee
RK
129 /* Arrange timeouts for schedule actions */
130 schedule_init(ev);
460b9539 131 if(!ret) {
132 queue_read();
133 recent_read();
134 reset_socket(ev);
135 }
136 return ret;
137}
138
139/*
140Local Variables:
141c-basic-offset:2
142comment-column:40
143End:
144*/