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 2 of the License, or
8 * (at your option) any later version.
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.
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
26 #include <sys/types.h>
27 #include <sys/socket.h>
35 #include <netinet/in.h>
42 #include "configuration.h"
45 #include "server-queue.h"
50 static const char *current_unix;
51 static int current_unix_fd;
53 static struct addrinfo *current_listen_addrinfo;
54 static int current_listen_fd;
56 void quit(ev_source *ev) {
57 info("shutting down...");
65 static void reset_socket(ev_source *ev) {
68 struct sockaddr_un sun;
71 static const struct addrinfo pref = {
72 .ai_flags = AI_PASSIVE,
74 .ai_socktype = SOCK_STREAM,
75 .ai_protocol = IPPROTO_TCP,
79 new_unix = config_get_file("socket");
80 if(!current_unix || strcmp(current_unix, new_unix)) {
81 /* either there was no socket, or there was but a different path */
83 /* stop the old one and remove it from the filesystem */
84 server_stop(ev, current_unix_fd);
85 if(unlink(current_unix) < 0)
86 fatal(errno, "unlink %s", current_unix);
88 /* start the new one */
89 if(strlen(new_unix) >= sizeof sun.sun_path)
90 fatal(0, "socket path %s is too long", new_unix);
91 memset(&sun, 0, sizeof sun);
92 sun.sun_family = AF_UNIX;
93 strcpy(sun.sun_path, new_unix);
94 if(unlink(new_unix) < 0 && errno != ENOENT)
95 fatal(errno, "unlink %s", new_unix);
96 if((current_unix_fd = server_start(ev, PF_UNIX, sizeof sun,
97 (const struct sockaddr *)&sun,
99 current_unix = new_unix;
100 if(chmod(new_unix, 0777) < 0)
101 fatal(errno, "error calling chmod %s", new_unix);
106 /* get the new listen config */
108 res = get_address(&config->listen, &pref, &name);
112 if((res && !current_listen_addrinfo)
113 || (current_listen_addrinfo
115 || addrinfocmp(res, current_listen_addrinfo)))) {
116 /* something has to change */
117 if(current_listen_addrinfo) {
118 /* delete the old listener */
119 server_stop(ev, current_listen_fd);
120 freeaddrinfo(current_listen_addrinfo);
121 current_listen_addrinfo = 0;
124 /* start the new listener */
125 if((current_listen_fd = server_start(ev, res->ai_family, res->ai_addrlen,
126 res->ai_addr, name)) >= 0) {
127 current_listen_addrinfo = res;
132 /* if res is still set it needs freeing */
137 int reconfigure(ev_source *ev, int reload) {
138 int need_another_rescan = 0;
141 D(("reconfigure(%d)", reload));
143 need_another_rescan = trackdb_rescan_cancel();
148 /* Tell the speaker it needs to reload its config too. */
150 info("%s: installed new configuration", configfile);
152 trackdb_open(TRACKDB_NO_UPGRADE);
154 /* We only allow for upgrade at startup */
155 trackdb_open(TRACKDB_CAN_UPGRADE);
156 if(need_another_rescan)
157 trackdb_rescan(ev, 1/*check*/);