4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 #include <sys/select.h>
29 #include <sys/socket.h>
34 #include <sys/ioctl.h>
43 #include <avahi-common/llist.h>
44 #include <avahi-common/malloc.h>
45 #include <avahi-common/address.h>
47 #include <libdaemon/dfork.h>
48 #include <libdaemon/dsignal.h>
49 #include <libdaemon/dlog.h>
50 #include <libdaemon/dpid.h>
51 #include <libdaemon/dexec.h>
53 #define BROWSE_DNS_SERVERS "BROWSE-DNS-SERVERS\n"
55 #define ENV_INTERFACE_DNS_SERVERS "AVAHI_INTERFACE_DNS_SERVERS"
56 #define ENV_DNS_SERVERS "AVAHI_DNS_SERVERS"
57 #define ENV_INTERFACE "AVAHI_INTERFACE"
71 } command = DAEMON_RUN;
74 static int daemonize = 0;
76 #if !HAVE_DECL_ENVIRON
77 extern char **environ;
80 typedef struct DNSServerInfo DNSServerInfo;
82 struct DNSServerInfo {
83 AvahiIfIndex interface;
84 AvahiProtocol protocol;
86 AVAHI_LLIST_FIELDS(DNSServerInfo, servers);
89 static AVAHI_LLIST_HEAD(DNSServerInfo, servers);
91 static void server_info_free(DNSServerInfo *i) {
94 avahi_free(i->address);
96 AVAHI_LLIST_REMOVE(DNSServerInfo, servers, servers, i);
100 static DNSServerInfo* get_server_info(AvahiIfIndex interface, AvahiProtocol protocol, const char *address) {
104 for (i = servers; i; i = i->servers_next)
105 if (i->interface == interface &&
106 i->protocol == protocol &&
107 strcmp(i->address, address) == 0)
113 static DNSServerInfo* new_server_info(AvahiIfIndex interface, AvahiProtocol protocol, const char *address) {
118 i = avahi_new(DNSServerInfo, 1);
119 i->interface = interface;
120 i->protocol = protocol;
121 i->address = avahi_strdup(address);
123 AVAHI_LLIST_PREPEND(DNSServerInfo, servers, servers, i);
128 static int set_cloexec(int fd) {
133 if ((n = fcntl(fd, F_GETFD)) < 0)
139 return fcntl(fd, F_SETFD, n|FD_CLOEXEC);
142 static int open_socket(void) {
144 struct sockaddr_un sa;
146 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
147 daemon_log(LOG_ERR, "socket(): %s", strerror(errno));
151 if (set_cloexec(fd) < 0) {
152 daemon_log(LOG_ERR, "fcntl(): %s", strerror(errno));
156 memset(&sa, 0, sizeof(sa));
157 sa.sun_family = AF_UNIX;
158 strncpy(sa.sun_path, AVAHI_SOCKET, sizeof(sa.sun_path)-1);
159 sa.sun_path[sizeof(sa.sun_path)-1] = 0;
161 if (connect(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0) {
162 daemon_log(LOG_ERR, "connect(): %s", strerror(errno));
163 daemon_log(LOG_INFO, "Failed to connect to the daemon. This probably means that you");
164 daemon_log(LOG_INFO, "didn't start avahi-daemon before avahi-dnsconfd.");
177 static ssize_t loop_write(int fd, const void*data, size_t size) {
180 assert(fd >= 0 && data && size);
185 if ((r = write(fd, data, size)) < 0)
192 data = (const uint8_t*) data + r;
199 static char *concat_dns_servers(AvahiIfIndex interface) {
203 for (i = servers; i; i = i->servers_next)
204 if (i->interface == interface || interface <= 0) {
208 t = avahi_strdup(i->address);
210 t = avahi_strdup_printf("%s %s", r, i->address);
219 static void set_env(const char *name, const char *value) {
228 for (e = environ; *e; e++) {
229 /* Search for the variable */
230 if (strlen(*e) < l+1)
233 if (strncmp(*e, name, l) != 0 || (*e)[l] != '=')
236 /* We simply free the record, sicne we know that we created it previously */
238 *e = avahi_strdup_printf("%s=%s", name, value);
245 static void run_script(int new, AvahiIfIndex interface, AvahiProtocol protocol, const char *address) {
249 char name[IF_NAMESIZE];
251 assert(interface > 0);
253 if (!if_indextoname(interface, name))
256 p = concat_dns_servers(interface);
257 set_env(ENV_INTERFACE_DNS_SERVERS, p ? p : "");
260 p = concat_dns_servers(-1);
261 set_env(ENV_DNS_SERVERS, p ? p : "");
264 set_env(ENV_INTERFACE, name);
266 snprintf(ia, sizeof(ia), "%i", (int) interface);
267 snprintf(pa, sizeof(pa), "%i", (int) protocol);
269 if (daemon_exec("/", &ret, AVAHI_DNSCONF_SCRIPT, AVAHI_DNSCONF_SCRIPT, new ? "+" : "-", address, ia, pa, avahi_proto_to_string(protocol), NULL) < 0)
270 daemon_log(LOG_WARNING, "Failed to run script");
272 daemon_log(LOG_WARNING, "Script returned with non-zero exit code %i", ret);
275 static int new_line(const char *l) {
278 if (state == ACKWAIT) {
280 daemon_log(LOG_ERR, "Avahi command failed: %s", l);
284 daemon_log(LOG_INFO, "Successfully connected to Avahi daemon.");
287 AvahiIfIndex interface;
288 AvahiProtocol protocol;
289 int i_interface, i_protocol, port;
292 assert(state == BROWSING);
294 if (*l != '<' && *l != '>') {
295 daemon_log(LOG_ERR, "Avahi sent us an invalid browsing line: %s", l);
299 if (sscanf(l+1, "%i %i %64s %i", &i_interface, &i_protocol, a, &port) != 4) {
300 daemon_log(LOG_ERR, "Failed to parse browsing line: %s", l);
304 interface = (AvahiIfIndex) i_interface;
305 protocol = (AvahiProtocol) i_protocol;
309 daemon_log(LOG_WARNING, "DNS server with port address != 53 found, ignoring");
311 daemon_log(LOG_INFO, "New DNS Server %s (interface: %i.%s)", a, interface, avahi_proto_to_string(protocol));
312 new_server_info(interface, protocol, a);
313 run_script(1, interface, protocol, a);
319 if ((i = get_server_info(interface, protocol, a))) {
320 daemon_log(LOG_INFO, "DNS Server %s removed (interface: %i.%s)", a, interface, avahi_proto_to_string(protocol));
322 run_script(0, interface, protocol, a);
331 static int do_connect(void) {
334 if ((fd = open_socket()) < 0)
337 if (loop_write(fd, BROWSE_DNS_SERVERS, sizeof(BROWSE_DNS_SERVERS)-1) < 0) {
338 daemon_log(LOG_ERR, "write(): %s", strerror(errno));
352 static void free_dns_server_info_list(void) {
354 AvahiIfIndex interface = servers->interface;
355 AvahiProtocol protocol = servers->protocol;
356 char *address = avahi_strdup(servers->address);
357 server_info_free(servers);
359 run_script(0, interface, protocol, address);
364 static void help(FILE *f, const char *argv0) {
367 " -h --help Show this help\n"
368 " -D --daemonize Daemonize after startup\n"
369 " -k --kill Kill a running daemon\n"
370 " -r --refresh Request a running daemon to refresh DNS server data\n"
371 " -c --check Return 0 if a daemon is already running\n"
372 " -V --version Show version\n",
376 static int parse_command_line(int argc, char *argv[]) {
379 static const struct option long_options[] = {
380 { "help", no_argument, NULL, 'h' },
381 { "daemonize", no_argument, NULL, 'D' },
382 { "kill", no_argument, NULL, 'k' },
383 { "version", no_argument, NULL, 'V' },
384 { "refresh", no_argument, NULL, 'r' },
385 { "check", no_argument, NULL, 'c' },
389 while ((c = getopt_long(argc, argv, "hDkVrc", long_options, NULL)) >= 0) {
393 command = DAEMON_HELP;
399 command = DAEMON_KILL;
402 command = DAEMON_VERSION;
405 command = DAEMON_REFRESH;
408 command = DAEMON_CHECK;
411 fprintf(stderr, "Invalid command line argument: %c\n", c);
417 fprintf(stderr, "Too many arguments\n");
424 static int run_daemon(void) {
425 int fd = -1, ret = -1;
429 AVAHI_LLIST_HEAD_INIT(DNSServerInfo, servers);
431 daemon_signal_init(SIGINT, SIGTERM, SIGCHLD, SIGHUP, 0);
433 /* Allocate some memory for our environment variables */
434 putenv(avahi_strdup(ENV_INTERFACE"="));
435 putenv(avahi_strdup(ENV_DNS_SERVERS"="));
436 putenv(avahi_strdup(ENV_INTERFACE_DNS_SERVERS"="));
438 if ((fd = do_connect()) < 0)
442 daemon_retval_send(0);
453 FD_SET(daemon_signal_fd(), &rfds);
456 if (select(fd+1, &rfds, NULL, NULL, NULL) < 0) {
460 daemon_log(LOG_ERR, "select(): %s", strerror(errno));
467 if (FD_ISSET(daemon_signal_fd(), &rfds)) {
471 if ((sig = daemon_signal_next()) <= 0) {
472 daemon_log(LOG_ERR, "daemon_signal_next() failed");
479 daemon_log(LOG_INFO, "Got %s, quitting.", sig == SIGINT ? "SIGINT" : "SIGTERM");
484 waitpid(-1, NULL, WNOHANG);
488 daemon_log(LOG_INFO, "Refreshing DNS Server list");
491 free_dns_server_info_list();
493 if ((fd = do_connect()) < 0)
499 } else if (FD_ISSET(fd, &rfds)) {
503 if ((r = read(fd, buf, sizeof(buf) - buflen - 1)) <= 0) {
504 daemon_log(LOG_ERR, "read(): %s", r < 0 ? strerror(errno) : "EOF");
509 assert(buflen <= sizeof(buf)-1);
511 while ((n = memchr(buf, '\n', buflen))) {
514 if (new_line(buf) < 0)
518 memmove(buf, n, buflen);
521 if (buflen >= sizeof(buf)-1) {
522 /* The incoming line is horribly long */
523 buf[sizeof(buf)-1] = 0;
525 if (new_line(buf) < 0)
535 free_dns_server_info_list();
540 daemon_signal_done();
542 if (ret != 0 && daemonize)
543 daemon_retval_send(1);
548 static const char* pid_file_proc(void) {
549 return AVAHI_RUNTIME_DIR"/avahi-dnsconfd.pid";
552 int main(int argc, char *argv[]) {
555 int wrote_pid_file = 0;
557 if ((argv0 = strrchr(argv[0], '/')))
562 daemon_pid_file_ident = daemon_log_ident = argv0;
563 daemon_pid_file_proc = pid_file_proc;
565 if (parse_command_line(argc, argv) < 0)
568 if (command == DAEMON_RUN) {
572 daemon_log(LOG_ERR, "This program is intended to be run as root.");
576 if ((pid = daemon_pid_file_is_running()) >= 0) {
577 daemon_log(LOG_ERR, "Daemon already running on PID %u", pid);
582 daemon_retval_init();
584 if ((pid = daemon_fork()) < 0)
590 if ((ret = daemon_retval_wait(20)) < 0) {
591 daemon_log(LOG_ERR, "Could not receive return value from daemon process.");
604 if (daemon_pid_file_create() < 0) {
605 daemon_log(LOG_ERR, "Failed to create PID file: %s", strerror(errno));
608 daemon_retval_send(1);
613 if (run_daemon() < 0)
617 } else if (command == DAEMON_HELP) {
621 } else if (command == DAEMON_VERSION) {
622 printf("%s "PACKAGE_VERSION"\n", argv0);
625 } else if (command == DAEMON_KILL) {
626 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
627 daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
632 } else if (command == DAEMON_REFRESH) {
633 if (daemon_pid_file_kill(SIGHUP) < 0) {
634 daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
639 } else if (command == DAEMON_CHECK)
640 r = (daemon_pid_file_is_running() >= 0) ? 0 : 1;
646 daemon_retval_done();
649 daemon_pid_file_remove();