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