2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2007, 2008 Richard Kettlewell
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 3 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /** @file server/state.c
19 * @brief Global server state
21 #include "disorder-server.h"
23 static const char *current_unix
;
24 static int current_unix_fd
;
26 static struct addrinfo
*current_listen_addrinfo
;
27 static int current_listen_fd
;
29 void quit(ev_source
*ev
) {
30 info("shutting down...");
38 static void reset_socket(ev_source
*ev
) {
41 struct sockaddr_un sun
;
44 static const struct addrinfo pref
= {
45 .ai_flags
= AI_PASSIVE
,
47 .ai_socktype
= SOCK_STREAM
,
48 .ai_protocol
= IPPROTO_TCP
,
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 */
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
);
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
,
72 current_unix
= new_unix
;
73 if(chmod(new_unix
, 0777) < 0)
74 fatal(errno
, "error calling chmod %s", new_unix
);
79 /* get the new listen config */
81 res
= get_address(&config
->listen
, &pref
, &name
);
85 if((res
&& !current_listen_addrinfo
)
86 || (current_listen_addrinfo
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;
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
;
105 /* if res is still set it needs freeing */
110 int reconfigure(ev_source
*ev
, int reload
) {
111 int need_another_rescan
= 0;
114 D(("reconfigure(%d)", reload
));
116 need_another_rescan
= trackdb_rescan_cancel();
121 /* Tell the speaker it needs to reload its config too. */
123 info("%s: installed new configuration", configfile
);
125 trackdb_open(TRACKDB_NO_UPGRADE
);
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 */