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/util.h>
43 #include <avahi-core/llist.h>
45 #include <libdaemon/dfork.h>
46 #include <libdaemon/dsignal.h>
47 #include <libdaemon/dlog.h>
48 #include <libdaemon/dpid.h>
49 #include <libdaemon/dexec.h>
51 #define BROWSE_DNS_SERVERS "BROWSE-DNS-SERVERS\n"
58 static gboolean quit = FALSE;
67 } command = DAEMON_RUN;
69 static gboolean daemonize = FALSE;
71 typedef struct DNSServerInfo DNSServerInfo;
73 struct DNSServerInfo {
77 AVAHI_LLIST_FIELDS(DNSServerInfo, servers);
80 static AVAHI_LLIST_HEAD(DNSServerInfo, servers);
82 static void server_info_free(DNSServerInfo *i) {
87 AVAHI_LLIST_REMOVE(DNSServerInfo, servers, servers, i);
91 static DNSServerInfo* get_server_info(gint interface, guchar protocol, const gchar *address) {
95 for (i = servers; i; i = i->servers_next)
96 if (i->interface == interface &&
97 i->protocol == protocol &&
98 strcmp(i->address, address) == 0)
104 static DNSServerInfo* new_server_info(gint interface, guchar protocol, const gchar *address) {
109 i = g_new(DNSServerInfo, 1);
110 i->interface = interface;
111 i->protocol = protocol;
112 i->address = g_strdup(address);
114 AVAHI_LLIST_PREPEND(DNSServerInfo, servers, servers, i);
119 static int open_socket(void) {
121 struct sockaddr_un sa;
123 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
124 daemon_log(LOG_ERR, "socket(): %s", strerror(errno));
128 if (avahi_set_cloexec(fd) < 0) {
129 daemon_log(LOG_ERR, "fcntl(): %s", strerror(errno));
133 memset(&sa, 0, sizeof(sa));
134 sa.sun_family = AF_UNIX;
135 strncpy(sa.sun_path, AVAHI_SOCKET, sizeof(sa.sun_path)-1);
136 sa.sun_path[sizeof(sa.sun_path)-1] = 0;
138 if (connect(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0) {
139 daemon_log(LOG_ERR, "connect(): %s", strerror(errno));
152 static ssize_t loop_write(int fd, const void*data, size_t size) {
155 g_assert(fd >= 0 && data && size);
160 if ((r = write(fd, data, size)) < 0)
167 data = (const guint8*) data + r;
174 static gchar *concat_dns_servers(gint interface) {
178 for (i = servers; i; i = i->servers_next)
179 if (i->interface == interface || interface <= 0) {
183 t = g_strdup(i->address);
185 t = g_strdup_printf("%s %s", r, i->address);
194 static gchar *getifname(gint interface, gchar *name, guint len) {
199 g_assert(interface >= 0);
201 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
202 daemon_log(LOG_ERR, "socket(): %s", strerror(errno));
206 memset(&ifr, 0, sizeof(ifr));
207 ifr.ifr_ifindex = interface;
209 if (ioctl(fd, SIOCGIFNAME, &ifr) < 0) {
210 daemon_log(LOG_ERR, "SIOCGIFNAME: %s\n", strerror(errno));
214 strncpy(name, ifr.ifr_name, len-1);
225 static void run_script(gboolean new, gint interface, guchar protocol, const gchar *address) {
227 g_assert(interface > 0);
229 gchar ia[16], pa[16];
230 gchar name[IFNAMSIZ+1];
232 if (!getifname(interface, name, sizeof(name)))
235 p = concat_dns_servers(interface);
236 g_setenv("AVAHI_INTERFACE_DNS_SERVERS", p ? p : "", TRUE);
239 p = concat_dns_servers(-1);
240 g_setenv("AVAHI_DNS_SERVERS", p ? p : "", TRUE);
243 g_setenv("AVAHI_INTERFACE", name, TRUE);
245 snprintf(ia, sizeof(ia), "%i", interface);
246 snprintf(pa, sizeof(pa), "%u", protocol);
248 if (daemon_exec("/", &ret, AVAHI_DNSCONF_SCRIPT, AVAHI_DNSCONF_SCRIPT, new ? "+" : "-", address, ia, pa, NULL) < 0)
249 daemon_log(LOG_WARNING, "Failed to run script");
251 daemon_log(LOG_WARNING, "Script returned with non-zero exit code %i", ret);
254 static gint new_line(const gchar *l) {
257 if (state == ACKWAIT) {
259 daemon_log(LOG_ERR, "Avahi command failed: %s", l);
263 daemon_log(LOG_INFO, "Successfully connected to Avahi daemon.");
271 g_assert(state == BROWSING);
273 if (*l != '<' && *l != '>') {
274 daemon_log(LOG_ERR, "Avahi sent us an invalid browsing line: %s", l);
278 if (sscanf(l+1, "%i %u %64s %u", &interface, &protocol, a, &port) != 4) {
279 daemon_log(LOG_ERR, "Failed to parse browsing line: %s", l);
285 daemon_log(LOG_WARNING, "DNS server with port address != 53 found, ignoring");
287 daemon_log(LOG_INFO, "New DNS Server %s (interface: %i.%u)", a, interface, protocol);
288 new_server_info(interface, (guchar) protocol, a);
289 run_script(TRUE, interface, (guchar) protocol, a);
295 if ((i = get_server_info(interface, (guchar) protocol, a))) {
296 daemon_log(LOG_INFO, "DNS Server %s removed (interface: %i.%u)", a, interface, protocol);
298 run_script(FALSE, interface, (guchar) protocol, a);
307 static gint do_connect(void) {
310 if ((fd = open_socket()) < 0)
313 if (loop_write(fd, BROWSE_DNS_SERVERS, sizeof(BROWSE_DNS_SERVERS)-1) < 0) {
314 daemon_log(LOG_ERR, "write(): %s", strerror(errno));
328 static void free_dns_server_info_list(void) {
330 gint interface = servers->interface;
331 guchar protocol = servers->protocol;
332 gchar *address = g_strdup(servers->address);
333 server_info_free(servers);
335 run_script(FALSE, interface, protocol, address);
340 static void help(FILE *f, const gchar *argv0) {
343 " -h --help Show this help\n"
344 " -D --daemonize Daemonize after startup\n"
345 " -k --kill Kill a running daemon\n"
346 " -r --refresh Request a running daemon to refresh DNS server data\n"
347 " -c --check Return 0 if a daemon is already running\n"
348 " -V --version Show version\n",
352 static gint parse_command_line(int argc, char *argv[]) {
355 static const struct option const long_options[] = {
356 { "help", no_argument, NULL, 'h' },
357 { "daemonize", no_argument, NULL, 'D' },
358 { "kill", no_argument, NULL, 'k' },
359 { "version", no_argument, NULL, 'V' },
360 { "refresh", no_argument, NULL, 'r' },
361 { "check", no_argument, NULL, 'c' },
365 while ((c = getopt_long(argc, argv, "hDkVrc", long_options, NULL)) >= 0) {
369 command = DAEMON_HELP;
375 command = DAEMON_KILL;
378 command = DAEMON_VERSION;
381 command = DAEMON_REFRESH;
384 command = DAEMON_CHECK;
387 fprintf(stderr, "Invalid command line argument: %c\n", c);
393 fprintf(stderr, "Too many arguments\n");
400 static int run_daemon(void) {
401 gint fd = -1, ret = -1;
405 AVAHI_LLIST_HEAD_INIT(DNSServerInfo, servers);
407 daemon_signal_init(SIGINT, SIGTERM, SIGCHLD, SIGHUP, 0);
409 if ((fd = do_connect()) < 0)
413 daemon_retval_send(0);
424 FD_SET(daemon_signal_fd(), &rfds);
427 if (select(fd+1, &rfds, NULL, NULL, NULL) < 0) {
431 daemon_log(LOG_ERR, "select(): %s", strerror(errno));
438 if (FD_ISSET(daemon_signal_fd(), &rfds)) {
442 if ((sig = daemon_signal_next()) <= 0) {
443 daemon_log(LOG_ERR, "daemon_signal_next() failed");
450 daemon_log(LOG_INFO, "Got %s, quitting.", sig == SIGINT ? "SIGINT" : "SIGTERM");
455 waitpid(-1, NULL, WNOHANG);
459 daemon_log(LOG_INFO, "Refreshing DNS Server list");
462 free_dns_server_info_list();
464 if ((fd = do_connect()) < 0)
470 } else if (FD_ISSET(fd, &rfds)) {
474 if ((r = read(fd, buf, sizeof(buf) - buflen - 1)) <= 0) {
475 daemon_log(LOG_ERR, "read(): %s", r < 0 ? strerror(errno) : "EOF");
480 g_assert(buflen <= sizeof(buf)-1);
482 while ((n = memchr(buf, '\n', buflen))) {
485 if (new_line(buf) < 0)
489 memmove(buf, n, buflen);
492 if (buflen >= sizeof(buf)-1) {
493 /* The incoming line is horribly long */
494 buf[sizeof(buf)-1] = 0;
496 if (new_line(buf) < 0)
506 free_dns_server_info_list();
511 daemon_signal_done();
513 if (ret != 0 && daemonize)
514 daemon_retval_send(1);
519 static const char* pid_file_proc(void) {
520 return AVAHI_RUNTIME_DIR"/avahi-dnsconfd.pid";
523 gint main(gint argc, gchar *argv[]) {
526 gboolean wrote_pid_file = FALSE;
528 if ((argv0 = strrchr(argv[0], '/')))
533 daemon_pid_file_ident = daemon_log_ident = argv0;
534 daemon_pid_file_proc = pid_file_proc;
536 if (parse_command_line(argc, argv) < 0)
539 if (command == DAEMON_RUN) {
543 daemon_log(LOG_ERR, "This program is intended to be run as root.");
547 if ((pid = daemon_pid_file_is_running()) >= 0) {
548 daemon_log(LOG_ERR, "Daemon already running on PID %u", pid);
553 daemon_retval_init();
555 if ((pid = daemon_fork()) < 0)
561 if ((ret = daemon_retval_wait(20)) < 0) {
562 daemon_log(LOG_ERR, "Could not recieve return value from daemon process.");
575 if (daemon_pid_file_create() < 0) {
576 daemon_log(LOG_ERR, "Failed to create PID file: %s", strerror(errno));
579 daemon_retval_send(1);
582 wrote_pid_file = TRUE;
584 if (run_daemon() < 0)
588 } else if (command == DAEMON_HELP) {
592 } else if (command == DAEMON_VERSION) {
593 printf("%s "PACKAGE_VERSION"\n", argv0);
596 } else if (command == DAEMON_KILL) {
597 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
598 daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
603 } else if (command == DAEMON_REFRESH) {
604 if (daemon_pid_file_kill(SIGHUP) < 0) {
605 daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
610 } else if (command == DAEMON_CHECK)
611 r = (daemon_pid_file_is_running() >= 0) ? 0 : 1;
618 daemon_retval_done();
621 daemon_pid_file_remove();