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 open_socket(void) {
120 struct sockaddr_un sa;
122 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
123 daemon_log(LOG_ERR, "socket(): %s", strerror(errno));
127 if (avahi_set_cloexec(fd) < 0) {
128 daemon_log(LOG_ERR, "fcntl(): %s", strerror(errno));
132 memset(&sa, 0, sizeof(sa));
133 sa.sun_family = AF_UNIX;
134 strncpy(sa.sun_path, AVAHI_SOCKET, sizeof(sa.sun_path)-1);
135 sa.sun_path[sizeof(sa.sun_path)-1] = 0;
137 if (connect(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0) {
138 daemon_log(LOG_ERR, "connect(): %s", strerror(errno));
151 static ssize_t loop_write(int fd, const void*data, size_t size) {
154 g_assert(fd >= 0 && data && size);
159 if ((r = write(fd, data, size)) < 0)
166 data = (const guint8*) data + r;
173 static gchar *concat_dns_servers(gint interface) {
177 for (i = servers; i; i = i->servers_next)
178 if (i->interface == interface || interface <= 0) {
182 t = g_strdup(i->address);
184 t = g_strdup_printf("%s %s", r, i->address);
193 static gchar *getifname(gint interface, gchar *name, guint len) {
198 g_assert(interface >= 0);
200 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
201 daemon_log(LOG_ERR, "socket(): %s", strerror(errno));
205 memset(&ifr, 0, sizeof(ifr));
206 ifr.ifr_ifindex = interface;
208 if (ioctl(fd, SIOCGIFNAME, &ifr) < 0) {
209 daemon_log(LOG_ERR, "SIOCGIFNAME: %s\n", strerror(errno));
213 strncpy(name, ifr.ifr_name, len-1);
224 static void run_script(gboolean new, gint interface, guchar protocol, const gchar *address) {
226 g_assert(interface > 0);
228 gchar ia[16], pa[16];
229 gchar name[IFNAMSIZ+1];
231 if (!getifname(interface, name, sizeof(name)))
234 p = concat_dns_servers(interface);
235 g_setenv("AVAHI_INTERFACE_DNS_SERVERS", p ? p : "", TRUE);
238 p = concat_dns_servers(-1);
239 g_setenv("AVAHI_DNS_SERVERS", p ? p : "", TRUE);
242 g_setenv("AVAHI_INTERFACE", name, TRUE);
244 snprintf(ia, sizeof(ia), "%i", interface);
245 snprintf(pa, sizeof(pa), "%u", protocol);
247 if (daemon_exec("/", &ret, AVAHI_DNSCONF_SCRIPT, AVAHI_DNSCONF_SCRIPT, new ? "+" : "-", address, ia, pa, NULL) < 0)
248 daemon_log(LOG_WARNING, "Failed to run script");
250 daemon_log(LOG_WARNING, "Script returned with non-zero exit code %i", ret);
253 static gint new_line(const gchar *l) {
256 if (state == ACKWAIT) {
258 daemon_log(LOG_ERR, "Avahi command failed: %s", l);
262 daemon_log(LOG_INFO, "Successfully connected to Avahi daemon.");
270 g_assert(state == BROWSING);
272 if (*l != '<' && *l != '>') {
273 daemon_log(LOG_ERR, "Avahi sent us an invalid browsing line: %s", l);
277 if (sscanf(l+1, "%i %u %64s %u", &interface, &protocol, a, &port) != 4) {
278 daemon_log(LOG_ERR, "Failed to parse browsing line: %s", l);
284 daemon_log(LOG_WARNING, "DNS server with port address != 53 found, ignoring");
286 daemon_log(LOG_INFO, "New DNS Server %s (interface: %i.%u)", a, interface, protocol);
287 new_server_info(interface, (guchar) protocol, a);
288 run_script(TRUE, interface, (guchar) protocol, a);
294 if ((i = get_server_info(interface, (guchar) protocol, a))) {
295 daemon_log(LOG_INFO, "DNS Server %s removed (interface: %i.%u)", a, interface, protocol);
297 run_script(FALSE, interface, (guchar) protocol, a);
306 static gint do_connect(void) {
309 if ((fd = open_socket()) < 0)
312 if (loop_write(fd, BROWSE_DNS_SERVERS, sizeof(BROWSE_DNS_SERVERS)-1) < 0) {
313 daemon_log(LOG_ERR, "write(): %s", strerror(errno));
327 static void free_dns_server_info_list(void) {
329 gint interface = servers->interface;
330 guchar protocol = servers->protocol;
331 gchar *address = g_strdup(servers->address);
332 server_info_free(servers);
334 run_script(FALSE, interface, protocol, address);
339 static void help(FILE *f, const gchar *argv0) {
342 " -h --help Show this help\n"
343 " -D --daemonize Daemonize after startup\n"
344 " -k --kill Kill a running daemon\n"
345 " -r --refresh Request a running daemon to refresh DNS server data\n"
346 " -c --check Return 0 if a daemon is already running\n"
347 " -V --version Show version\n",
351 static gint parse_command_line(int argc, char *argv[]) {
354 static const struct option const long_options[] = {
355 { "help", no_argument, NULL, 'h' },
356 { "daemonize", no_argument, NULL, 'D' },
357 { "kill", no_argument, NULL, 'k' },
358 { "version", no_argument, NULL, 'V' },
359 { "refresh", no_argument, NULL, 'r' },
360 { "check", no_argument, NULL, 'c' },
364 while ((c = getopt_long(argc, argv, "hDkVrc", long_options, NULL)) >= 0) {
368 command = DAEMON_HELP;
374 command = DAEMON_KILL;
377 command = DAEMON_VERSION;
380 command = DAEMON_REFRESH;
383 command = DAEMON_CHECK;
386 fprintf(stderr, "Invalid command line argument: %c\n", c);
392 fprintf(stderr, "Too many arguments\n");
399 static int run_daemon(void) {
400 gint fd = -1, ret = -1;
404 AVAHI_LLIST_HEAD_INIT(DNSServerInfo, servers);
406 daemon_signal_init(SIGINT, SIGTERM, SIGCHLD, SIGHUP, 0);
408 if ((fd = do_connect()) < 0)
412 daemon_retval_send(0);
423 FD_SET(daemon_signal_fd(), &rfds);
426 if (select(fd+1, &rfds, NULL, NULL, NULL) < 0) {
430 daemon_log(LOG_ERR, "select(): %s", strerror(errno));
437 if (FD_ISSET(daemon_signal_fd(), &rfds)) {
441 if ((sig = daemon_signal_next()) <= 0) {
442 daemon_log(LOG_ERR, "daemon_signal_next() failed");
449 daemon_log(LOG_INFO, "Got %s, quitting.", sig == SIGINT ? "SIGINT" : "SIGTERM");
454 waitpid(-1, NULL, WNOHANG);
458 daemon_log(LOG_INFO, "Refreshing DNS Server list");
461 free_dns_server_info_list();
463 if ((fd = do_connect()) < 0)
469 } else if (FD_ISSET(fd, &rfds)) {
473 if ((r = read(fd, buf, sizeof(buf) - buflen - 1)) <= 0) {
474 daemon_log(LOG_ERR, "read(): %s", r < 0 ? strerror(errno) : "EOF");
479 g_assert(buflen <= sizeof(buf)-1);
481 while ((n = memchr(buf, '\n', buflen))) {
484 if (new_line(buf) < 0)
488 memmove(buf, n, buflen);
491 if (buflen >= sizeof(buf)-1) {
492 /* The incoming line is horribly long */
493 buf[sizeof(buf)-1] = 0;
495 if (new_line(buf) < 0)
505 free_dns_server_info_list();
510 daemon_signal_done();
512 if (ret != 0 && daemonize)
513 daemon_retval_send(1);
518 static const char* pid_file_proc(void) {
519 return AVAHI_RUNTIME_DIR"/avahi-dnsconfd.pid";
522 gint main(gint argc, gchar *argv[]) {
525 gboolean wrote_pid_file = FALSE;
527 if ((argv0 = strrchr(argv[0], '/')))
532 daemon_pid_file_ident = daemon_log_ident = argv0;
533 daemon_pid_file_proc = pid_file_proc;
535 if (parse_command_line(argc, argv) < 0)
538 if (command == DAEMON_RUN) {
542 daemon_log(LOG_ERR, "This program is intended to be run as root.");
546 if ((pid = daemon_pid_file_is_running()) >= 0) {
547 daemon_log(LOG_ERR, "Daemon already running on PID %u", pid);
552 daemon_retval_init();
554 if ((pid = daemon_fork()) < 0)
560 if ((ret = daemon_retval_wait(20)) < 0) {
561 daemon_log(LOG_ERR, "Could not recieve return value from daemon process.");
574 if (daemon_pid_file_create() < 0) {
575 daemon_log(LOG_ERR, "Failed to create PID file: %s", strerror(errno));
578 daemon_retval_send(1);
581 wrote_pid_file = TRUE;
583 if (run_daemon() < 0)
587 } else if (command == DAEMON_HELP) {
591 } else if (command == DAEMON_VERSION) {
592 printf("%s "PACKAGE_VERSION"\n", argv0);
595 } else if (command == DAEMON_KILL) {
596 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
597 daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
602 } else if (command == DAEMON_REFRESH) {
603 if (daemon_pid_file_kill(SIGHUP) < 0) {
604 daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
609 } else if (command == DAEMON_CHECK)
610 r = (daemon_pid_file_is_running() >= 0) ? 0 : 1;
617 daemon_retval_done();
620 daemon_pid_file_remove();