chiark / gitweb /
per-API --without options for configure
[disorder] / server / state.c
CommitLineData
460b9539 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.h"
45#include "printf.h"
46#include "addr.h"
47
48static const char *current_unix;
49static int current_unix_fd;
50
51static struct addrinfo *current_listen_addrinfo;
52static int current_listen_fd;
53
54void quit(ev_source *ev) {
55 quitting(ev);
56 trackdb_close();
57 trackdb_deinit();
58 info("terminating");
59 _exit(0);
60}
61
62static void reset_socket(ev_source *ev) {
63 const char *new_unix;
64 struct addrinfo *res;
65 struct sockaddr_un sun;
66 char *name;
67
68 static const struct addrinfo pref = {
69 AI_PASSIVE,
70 PF_INET,
71 SOCK_STREAM,
72 IPPROTO_TCP,
73 0,
74 0,
75 0,
76 0
77 };
78
79 /* unix first */
80 new_unix = config_get_file("socket");
81 if(!current_unix || strcmp(current_unix, new_unix)) {
82 /* either there was no socket, or there was but a different path */
83 if(current_unix) {
84 /* stop the old one and remove it from the filesystem */
85 server_stop(ev, current_unix_fd);
86 if(unlink(current_unix) < 0)
87 fatal(errno, "unlink %s", current_unix);
88 }
89 /* start the new one */
90 if(strlen(new_unix) >= sizeof sun.sun_path)
91 fatal(0, "socket path %s is too long", new_unix);
92 memset(&sun, 0, sizeof sun);
93 sun.sun_family = AF_UNIX;
94 strcpy(sun.sun_path, new_unix);
95 if(unlink(new_unix) < 0 && errno != ENOENT)
96 fatal(errno, "unlink %s", new_unix);
97 if((current_unix_fd = server_start(ev, PF_UNIX, sizeof sun,
98 (const struct sockaddr *)&sun,
99 new_unix)) >= 0) {
100 current_unix = new_unix;
101 if(chmod(new_unix, 0777) < 0)
102 fatal(errno, "error calling chmod %s", new_unix);
103 } else
104 current_unix = 0;
105 }
106
107 /* get the new listen config */
108 if(config->listen.n)
109 res = get_address(&config->listen, &pref, &name);
110 else
111 res = 0;
112
113 if((res && !current_listen_addrinfo)
114 || (current_listen_addrinfo
115 && (!res
116 || addrinfocmp(res, current_listen_addrinfo)))) {
117 /* something has to change */
118 if(current_listen_addrinfo) {
119 /* delete the old listener */
120 server_stop(ev, current_listen_fd);
121 freeaddrinfo(current_listen_addrinfo);
122 current_listen_addrinfo = 0;
123 }
124 if(res) {
125 /* start the new listener */
126 if((current_listen_fd = server_start(ev, res->ai_family, res->ai_addrlen,
127 res->ai_addr, name)) >= 0) {
128 current_listen_addrinfo = res;
129 res = 0;
130 }
131 }
132 }
133 /* if res is still set it needs freeing */
134 if(res)
135 freeaddrinfo(res);
136}
137
138int reconfigure(ev_source *ev, int reload) {
139 int need_another_rescan = 0;
140 int ret = 0;
141
142 D(("reconfigure(%d)", reload));
143 if(reload) {
144 need_another_rescan = trackdb_rescan_cancel();
145 trackdb_close();
c00fce3a 146 if(config_read(1))
460b9539 147 ret = -1;
148 else {
149 /* Tell the speaker it needs to reload its config too. */
150 speaker_reload();
151 info("%s: installed new configuration", configfile);
152 }
153 }
154 trackdb_open();
155 if(need_another_rescan)
156 trackdb_rescan(ev);
157 if(!ret) {
158 queue_read();
159 recent_read();
160 reset_socket(ev);
161 }
162 return ret;
163}
164
165/*
166Local Variables:
167c-basic-offset:2
168comment-column:40
169End:
170*/