chiark / gitweb /
lib/addr.c: Introduce our own `freeaddrinfo' function.
[disorder] / server / state.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
8a886602 3 * Copyright (C) 2004, 2005, 2007-2009, 2012 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 */
132a5a4a
RK
18/** @file server/state.c
19 * @brief Global server state
20 */
05b75f8d 21#include "disorder-server.h"
460b9539 22
de37b640
RK
23/** @brief A unix domain socket */
24struct unix_socket {
25 const char *path;
26 int fd;
27};
e04f4cb5 28
de37b640 29struct unix_socket main_socket[1], priv_socket[1];
460b9539 30
80dc2c5f
RK
31/** @brief TCP listener definition */
32struct listener {
e04f4cb5 33 /** @brief Next listener */
80dc2c5f 34 struct listener *next;
e04f4cb5
RK
35
36 /** @brief Local socket address */
80dc2c5f 37 struct sockaddr *sa;
e04f4cb5
RK
38
39 /** @brief File descriptor */
80dc2c5f
RK
40 int fd;
41};
42
43/** @brief Current listeners */
44static struct listener *listeners;
460b9539 45
b50cfb8a
RK
46/** @brief Current audio API */
47const struct uaudio *api;
48
e04f4cb5 49/** @brief Quit DisOrder */
460b9539 50void quit(ev_source *ev) {
2e9ba080 51 disorder_info("shutting down...");
460b9539 52 quitting(ev);
53 trackdb_close();
8cd7d4bc 54 trackdb_deinit(ev);
18ed984a
RK
55 /* Shutdown subprocesses.
56 *
57 * Subprocesses that use ev_child:
58 * - the speaker
59 * - the current rescan
60 * - any decoders
61 * - ...and players
62 * - the track picker
63 * - mail sender
64 * - the deadlock manager
65 *
66 * Subprocesses that don't:
67 * - any normalizers
68 * These are not shut down currently.
69 */
70 ev_child_killall(ev);
2e9ba080 71 disorder_info("exiting");
445a0f66 72 exit(0);
460b9539 73}
74
e04f4cb5 75/** @brief Create a copy of an @c addrinfo structure */
80dc2c5f
RK
76static struct sockaddr *copy_sockaddr(const struct addrinfo *addr) {
77 struct sockaddr *sa = xmalloc_noptr(addr->ai_addrlen);
78 memcpy(sa, addr->ai_addr, addr->ai_addrlen);
79 return sa;
80}
81
de37b640
RK
82static void reset_unix_socket(ev_source *ev,
83 struct unix_socket *s,
84 const char *new_path,
85 int privileged) {
460b9539 86 struct sockaddr_un sun;
de37b640 87 if(!s->path || strcmp(s->path, new_path)) {
460b9539 88 /* either there was no socket, or there was but a different path */
de37b640 89 if(s->path) {
460b9539 90 /* stop the old one and remove it from the filesystem */
de37b640
RK
91 server_stop(ev, s->fd);
92 if(unlink(s->path) < 0)
93 disorder_fatal(errno, "unlink %s", s->path);
460b9539 94 }
95 /* start the new one */
de37b640
RK
96 if(strlen(new_path) >= sizeof sun.sun_path)
97 disorder_fatal(0, "socket path %s is too long", new_path);
460b9539 98 memset(&sun, 0, sizeof sun);
99 sun.sun_family = AF_UNIX;
de37b640
RK
100 strcpy(sun.sun_path, new_path);
101 if(unlink(new_path) < 0 && errno != ENOENT)
102 disorder_fatal(errno, "unlink %s", new_path);
103 if((s->fd = server_start(ev, PF_UNIX, sizeof sun,
104 (const struct sockaddr *)&sun,
105 new_path,
106 privileged)) >= 0) {
107 s->path = new_path;
108 if(chmod(new_path, 0777) < 0) /* TODO */
109 disorder_fatal(errno, "error calling chmod %s", new_path);
460b9539 110 } else
de37b640 111 s->path = 0;
460b9539 112 }
de37b640
RK
113}
114
115/** @brief Create and destroy sockets to set current configuration */
116void reset_sockets(ev_source *ev) {
117 struct addrinfo *res, *r;
118 struct listener *l, **ll;
119 const char *private_dir = config_get_file("private");
120
121 /* create the private socket directory */
122 unlink(private_dir);
123 if(mkdir(private_dir, 0700) < 0 && errno != EEXIST)
124 disorder_fatal(errno, "error creating %s", private_dir);
125
126 /* unix first */
127 reset_unix_socket(ev, main_socket, config_get_file("socket"), 0);
128 reset_unix_socket(ev, priv_socket, config_get_file("private/socket"), 1);
460b9539 129
130 /* get the new listen config */
80dc2c5f
RK
131 if(config->listen.af != -1)
132 res = netaddress_resolve(&config->listen, 1, IPPROTO_TCP);
460b9539 133 else
134 res = 0;
135
80dc2c5f
RK
136 /* Close any current listeners that aren't required any more */
137 ll = &listeners;
138 while((l = *ll)) {
139 for(r = res; r; r = r->ai_next)
140 if(!sockaddrcmp(r->ai_addr, l->sa))
141 break;
142 if(!r) {
143 /* Didn't find a match, remove this one */
144 server_stop(ev, l->fd);
145 *ll = l->next;
146 } else {
147 /* This address is still wanted */
148 ll = &l->next;
460b9539 149 }
80dc2c5f
RK
150 }
151
152 /* Open any new listeners that are required */
153 for(r = res; r; r = r->ai_next) {
154 for(l = listeners; l; l = l->next)
155 if(!sockaddrcmp(r->ai_addr, l->sa))
156 break;
157 if(!l) {
158 /* Didn't find a match, need a new listener */
159 int fd = server_start(ev, r->ai_family, r->ai_addrlen, r->ai_addr,
de37b640 160 format_sockaddr(r->ai_addr), 0);
80dc2c5f
RK
161 if(fd >= 0) {
162 l = xmalloc(sizeof *l);
163 l->next = listeners;
164 l->sa = copy_sockaddr(r);
165 l->fd = fd;
166 listeners = l;
460b9539 167 }
e04f4cb5
RK
168 /* We ignore any failures (though server_start() will have
169 * logged them). */
460b9539 170 }
171 }
172 /* if res is still set it needs freeing */
173 if(res)
81abb3f1 174 netaddress_freeaddrinfo(res);
460b9539 175}
176
e04f4cb5 177/** @brief Reconfigure the server
c0f52b2c 178 * @param ev Event loop
37f29ce9
RK
179 * @param flags Flags
180 * @return As config_read(); 0 on success, -1 if could not (re-)read config
e04f4cb5 181 */
37f29ce9 182int reconfigure(ev_source *ev, unsigned flags) {
460b9539 183 int need_another_rescan = 0;
184 int ret = 0;
185
37f29ce9 186 D(("reconfigure(%x)", flags));
e04f4cb5 187 /* Deconfigure the old audio API if there is one */
b50cfb8a
RK
188 if(api) {
189 if(api->close_mixer)
190 api->close_mixer();
191 api = NULL;
192 }
37f29ce9 193 if(flags & RECONFIGURE_RELOADING) {
e04f4cb5 194 /* If there's a rescan in progress, cancel it but remember to start a fresh
36c5e137 195 * one after the reload. */
460b9539 196 need_another_rescan = trackdb_rescan_cancel();
e04f4cb5 197 /* (Re-)read the configuration */
02ba7921 198 if(config_read(1/*server*/, config))
460b9539 199 ret = -1;
200 else {
201 /* Tell the speaker it needs to reload its config too. */
202 speaker_reload();
2e9ba080 203 disorder_info("%s: installed new configuration", configfile);
460b9539 204 }
36c5e137 205 }
e04f4cb5 206 /* New audio API */
b50cfb8a 207 api = uaudio_find(config->api);
ba70caca
RK
208 if(api->configure)
209 api->configure();
b50cfb8a
RK
210 if(api->open_mixer)
211 api->open_mixer();
e04f4cb5 212 /* If we interrupted a rescan of all the tracks, start a new one */
460b9539 213 if(need_another_rescan)
dd9af5cb 214 trackdb_rescan(ev, 1/*check*/, 0, 0);
37f29ce9
RK
215 if(!ret && !(flags & RECONFIGURE_FIRST)) {
216 /* Open/close sockets */
217 reset_sockets(ev);
460b9539 218 }
219 return ret;
220}
221
222/*
223Local Variables:
224c-basic-offset:2
225comment-column:40
e04f4cb5
RK
226fill-column:79
227indent-tabs-mode:nil
460b9539 228End:
229*/