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
29 #include <sys/select.h>
30 #include <sys/socket.h>
34 #include <sys/ioctl.h>
42 #include <avahi-common/llist.h>
44 #include <libdaemon/dfork.h>
45 #include <libdaemon/dsignal.h>
46 #include <libdaemon/dlog.h>
47 #include <libdaemon/dpid.h>
48 #include <libdaemon/dexec.h>
50 #define BROWSE_DNS_SERVERS "BROWSE-DNS-SERVERS\n"
57 static gboolean quit = FALSE;
66 } command = DAEMON_RUN;
68 static gboolean daemonize = FALSE;
70 typedef struct DNSServerInfo DNSServerInfo;
72 struct DNSServerInfo {
76 AVAHI_LLIST_FIELDS(DNSServerInfo, servers);
79 static AVAHI_LLIST_HEAD(DNSServerInfo, servers);
81 static void server_info_free(DNSServerInfo *i) {
86 AVAHI_LLIST_REMOVE(DNSServerInfo, servers, servers, i);
90 static DNSServerInfo* get_server_info(gint interface, guchar protocol, const gchar *address) {
94 for (i = servers; i; i = i->servers_next)
95 if (i->interface == interface &&
96 i->protocol == protocol &&
97 strcmp(i->address, address) == 0)
103 static DNSServerInfo* new_server_info(gint interface, guchar protocol, const gchar *address) {
108 i = g_new(DNSServerInfo, 1);
109 i->interface = interface;
110 i->protocol = protocol;
111 i->address = g_strdup(address);
113 AVAHI_LLIST_PREPEND(DNSServerInfo, servers, servers, i);
118 static int set_cloexec(int fd) {
123 if ((n = fcntl(fd, F_GETFD)) < 0)
129 return fcntl(fd, F_SETFD, n|FD_CLOEXEC);
132 static int open_socket(void) {
134 struct sockaddr_un sa;
136 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
137 daemon_log(LOG_ERR, "socket(): %s", strerror(errno));
141 if (set_cloexec(fd) < 0) {
142 daemon_log(LOG_ERR, "fcntl(): %s", strerror(errno));
146 memset(&sa, 0, sizeof(sa));
147 sa.sun_family = AF_UNIX;
148 strncpy(sa.sun_path, AVAHI_SOCKET, sizeof(sa.sun_path)-1);
149 sa.sun_path[sizeof(sa.sun_path)-1] = 0;
151 if (connect(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0) {
152 daemon_log(LOG_ERR, "connect(): %s", strerror(errno));
165 static ssize_t loop_write(int fd, const void*data, size_t size) {
168 g_assert(fd >= 0 && data && size);
173 if ((r = write(fd, data, size)) < 0)
180 data = (const guint8*) data + r;
187 static gchar *concat_dns_servers(gint interface) {
191 for (i = servers; i; i = i->servers_next)
192 if (i->interface == interface || interface <= 0) {
196 t = g_strdup(i->address);
198 t = g_strdup_printf("%s %s", r, i->address);
207 static gchar *getifname(gint interface, gchar *name, guint len) {
212 g_assert(interface >= 0);
214 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
215 daemon_log(LOG_ERR, "socket(): %s", strerror(errno));
219 memset(&ifr, 0, sizeof(ifr));
220 ifr.ifr_ifindex = interface;
222 if (ioctl(fd, SIOCGIFNAME, &ifr) < 0) {
223 daemon_log(LOG_ERR, "SIOCGIFNAME: %s\n", strerror(errno));
227 strncpy(name, ifr.ifr_name, len-1);
238 static void run_script(gboolean new, gint interface, guchar protocol, const gchar *address) {
241 gchar ia[16], pa[16];
242 gchar name[IFNAMSIZ+1];
244 g_assert(interface > 0);
246 if (!getifname(interface, name, sizeof(name)))
249 p = concat_dns_servers(interface);
250 g_setenv("AVAHI_INTERFACE_DNS_SERVERS", p ? p : "", TRUE);
253 p = concat_dns_servers(-1);
254 g_setenv("AVAHI_DNS_SERVERS", p ? p : "", TRUE);
257 g_setenv("AVAHI_INTERFACE", name, TRUE);
259 snprintf(ia, sizeof(ia), "%i", interface);
260 snprintf(pa, sizeof(pa), "%u", protocol);
262 if (daemon_exec("/", &ret, AVAHI_DNSCONF_SCRIPT, AVAHI_DNSCONF_SCRIPT, new ? "+" : "-", address, ia, pa, NULL) < 0)
263 daemon_log(LOG_WARNING, "Failed to run script");
265 daemon_log(LOG_WARNING, "Script returned with non-zero exit code %i", ret);
268 static gint new_line(const gchar *l) {
271 if (state == ACKWAIT) {
273 daemon_log(LOG_ERR, "Avahi command failed: %s", l);
277 daemon_log(LOG_INFO, "Successfully connected to Avahi daemon.");
285 g_assert(state == BROWSING);
287 if (*l != '<' && *l != '>') {
288 daemon_log(LOG_ERR, "Avahi sent us an invalid browsing line: %s", l);
292 if (sscanf(l+1, "%i %u %64s %u", &interface, &protocol, a, &port) != 4) {
293 daemon_log(LOG_ERR, "Failed to parse browsing line: %s", l);
299 daemon_log(LOG_WARNING, "DNS server with port address != 53 found, ignoring");
301 daemon_log(LOG_INFO, "New DNS Server %s (interface: %i.%u)", a, interface, protocol);
302 new_server_info(interface, (guchar) protocol, a);
303 run_script(TRUE, interface, (guchar) protocol, a);
309 if ((i = get_server_info(interface, (guchar) protocol, a))) {
310 daemon_log(LOG_INFO, "DNS Server %s removed (interface: %i.%u)", a, interface, protocol);
312 run_script(FALSE, interface, (guchar) protocol, a);
321 static gint do_connect(void) {
324 if ((fd = open_socket()) < 0)
327 if (loop_write(fd, BROWSE_DNS_SERVERS, sizeof(BROWSE_DNS_SERVERS)-1) < 0) {
328 daemon_log(LOG_ERR, "write(): %s", strerror(errno));
342 static void free_dns_server_info_list(void) {
344 gint interface = servers->interface;
345 guchar protocol = servers->protocol;
346 gchar *address = g_strdup(servers->address);
347 server_info_free(servers);
349 run_script(FALSE, interface, protocol, address);
354 static void help(FILE *f, const gchar *argv0) {
357 " -h --help Show this help\n"
358 " -D --daemonize Daemonize after startup\n"
359 " -k --kill Kill a running daemon\n"
360 " -r --refresh Request a running daemon to refresh DNS server data\n"
361 " -c --check Return 0 if a daemon is already running\n"
362 " -V --version Show version\n",
366 static gint parse_command_line(int argc, char *argv[]) {
369 static const struct option const long_options[] = {
370 { "help", no_argument, NULL, 'h' },
371 { "daemonize", no_argument, NULL, 'D' },
372 { "kill", no_argument, NULL, 'k' },
373 { "version", no_argument, NULL, 'V' },
374 { "refresh", no_argument, NULL, 'r' },
375 { "check", no_argument, NULL, 'c' },
379 while ((c = getopt_long(argc, argv, "hDkVrc", long_options, NULL)) >= 0) {
383 command = DAEMON_HELP;
389 command = DAEMON_KILL;
392 command = DAEMON_VERSION;
395 command = DAEMON_REFRESH;
398 command = DAEMON_CHECK;
401 fprintf(stderr, "Invalid command line argument: %c\n", c);
407 fprintf(stderr, "Too many arguments\n");
414 static int run_daemon(void) {
415 gint fd = -1, ret = -1;
419 AVAHI_LLIST_HEAD_INIT(DNSServerInfo, servers);
421 daemon_signal_init(SIGINT, SIGTERM, SIGCHLD, SIGHUP, 0);
423 if ((fd = do_connect()) < 0)
427 daemon_retval_send(0);
438 FD_SET(daemon_signal_fd(), &rfds);
441 if (select(fd+1, &rfds, NULL, NULL, NULL) < 0) {
445 daemon_log(LOG_ERR, "select(): %s", strerror(errno));
452 if (FD_ISSET(daemon_signal_fd(), &rfds)) {
456 if ((sig = daemon_signal_next()) <= 0) {
457 daemon_log(LOG_ERR, "daemon_signal_next() failed");
464 daemon_log(LOG_INFO, "Got %s, quitting.", sig == SIGINT ? "SIGINT" : "SIGTERM");
469 waitpid(-1, NULL, WNOHANG);
473 daemon_log(LOG_INFO, "Refreshing DNS Server list");
476 free_dns_server_info_list();
478 if ((fd = do_connect()) < 0)
484 } else if (FD_ISSET(fd, &rfds)) {
488 if ((r = read(fd, buf, sizeof(buf) - buflen - 1)) <= 0) {
489 daemon_log(LOG_ERR, "read(): %s", r < 0 ? strerror(errno) : "EOF");
494 g_assert(buflen <= sizeof(buf)-1);
496 while ((n = memchr(buf, '\n', buflen))) {
499 if (new_line(buf) < 0)
503 memmove(buf, n, buflen);
506 if (buflen >= sizeof(buf)-1) {
507 /* The incoming line is horribly long */
508 buf[sizeof(buf)-1] = 0;
510 if (new_line(buf) < 0)
520 free_dns_server_info_list();
525 daemon_signal_done();
527 if (ret != 0 && daemonize)
528 daemon_retval_send(1);
533 static const char* pid_file_proc(void) {
534 return AVAHI_RUNTIME_DIR"/avahi-dnsconfd.pid";
537 gint main(gint argc, gchar *argv[]) {
540 gboolean wrote_pid_file = FALSE;
542 if ((argv0 = strrchr(argv[0], '/')))
547 daemon_pid_file_ident = daemon_log_ident = argv0;
548 daemon_pid_file_proc = pid_file_proc;
550 if (parse_command_line(argc, argv) < 0)
553 if (command == DAEMON_RUN) {
557 daemon_log(LOG_ERR, "This program is intended to be run as root.");
561 if ((pid = daemon_pid_file_is_running()) >= 0) {
562 daemon_log(LOG_ERR, "Daemon already running on PID %u", pid);
567 daemon_retval_init();
569 if ((pid = daemon_fork()) < 0)
575 if ((ret = daemon_retval_wait(20)) < 0) {
576 daemon_log(LOG_ERR, "Could not recieve return value from daemon process.");
589 if (daemon_pid_file_create() < 0) {
590 daemon_log(LOG_ERR, "Failed to create PID file: %s", strerror(errno));
593 daemon_retval_send(1);
596 wrote_pid_file = TRUE;
598 if (run_daemon() < 0)
602 } else if (command == DAEMON_HELP) {
606 } else if (command == DAEMON_VERSION) {
607 printf("%s "PACKAGE_VERSION"\n", argv0);
610 } else if (command == DAEMON_KILL) {
611 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
612 daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
617 } else if (command == DAEMON_REFRESH) {
618 if (daemon_pid_file_kill(SIGHUP) < 0) {
619 daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
624 } else if (command == DAEMON_CHECK)
625 r = (daemon_pid_file_is_running() >= 0) ? 0 : 1;
631 daemon_retval_done();
634 daemon_pid_file_remove();