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;
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 = (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 --reload Request a running daemon to reload static services\n"
346 " -V --version Show version\n",
350 static gint parse_command_line(int argc, char *argv[]) {
353 static const struct option const long_options[] = {
354 { "help", no_argument, NULL, 'h' },
355 { "daemonize", no_argument, NULL, 'D' },
356 { "kill", no_argument, NULL, 'k' },
357 { "version", no_argument, NULL, 'V' },
358 { "reload", no_argument, NULL, 'r' },
362 while ((c = getopt_long(argc, argv, "hDkVr", long_options, NULL)) >= 0) {
366 command = DAEMON_HELP;
372 command = DAEMON_KILL;
375 command = DAEMON_VERSION;
378 command = DAEMON_RELOAD;
381 fprintf(stderr, "Invalid command line argument: %c\n", c);
387 fprintf(stderr, "Too many arguments\n");
394 static int run_daemon(void) {
395 gint fd = -1, ret = -1;
399 AVAHI_LLIST_HEAD_INIT(DNSServerInfo, servers);
401 daemon_signal_init(SIGINT, SIGTERM, SIGCHLD, SIGHUP, 0);
403 if ((fd = do_connect()) < 0)
407 daemon_retval_send(0);
416 FD_SET(daemon_signal_fd(), &rfds);
419 if (select(fd+1, &rfds, NULL, NULL, NULL) < 0) {
423 daemon_log(LOG_ERR, "select(): %s", strerror(errno));
430 if (FD_ISSET(daemon_signal_fd(), &rfds)) {
434 if ((sig = daemon_signal_next()) <= 0) {
435 daemon_log(LOG_ERR, "daemon_signal_next() failed");
442 daemon_log(LOG_INFO, "Got %s, quitting.", sig == SIGINT ? "SIGINT" : "SIGTERM");
447 waitpid(-1, NULL, WNOHANG);
451 daemon_log(LOG_INFO, "Rrefreshing DNS Server list");
454 free_dns_server_info_list();
456 if ((fd = do_connect()) < 0)
462 } else if (FD_ISSET(fd, &rfds)) {
466 if ((r = read(fd, buf, sizeof(buf) - buflen - 1)) <= 0) {
467 daemon_log(LOG_ERR, "read(): %s", r < 0 ? strerror(errno) : "EOF");
472 g_assert(buflen <= sizeof(buf)-1);
474 while ((n = memchr(buf, '\n', buflen))) {
477 if (new_line(buf) < 0)
481 memmove(buf, n, buflen);
484 if (buflen >= sizeof(buf)-1) {
485 /* The incoming line is horribly long */
486 buf[sizeof(buf)-1] = 0;
488 if (new_line(buf) < 0)
500 free_dns_server_info_list();
505 daemon_signal_done();
510 static const char* pid_file_proc(void) {
511 return AVAHI_RUNTIME_DIR"/avahi-dnsconfd.pid";
514 gint main(gint argc, gchar *argv[]) {
517 gboolean wrote_pid_file = FALSE;
519 if ((argv0 = strrchr(argv[0], '/')))
524 daemon_pid_file_ident = daemon_log_ident = argv0;
525 daemon_pid_file_proc = pid_file_proc;
527 if (parse_command_line(argc, argv) < 0)
530 if (command == DAEMON_RUN) {
534 daemon_log(LOG_ERR, "This program is intended to be run as root.");
538 if ((pid = daemon_pid_file_is_running()) >= 0) {
539 daemon_log(LOG_ERR, "Daemon already running on PID %u", pid);
544 daemon_retval_init();
546 if ((pid = daemon_fork()) < 0)
552 if ((ret = daemon_retval_wait(20)) < 0) {
553 daemon_log(LOG_ERR, "Could not recieve return value from daemon process.");
566 if (daemon_pid_file_create() < 0) {
567 daemon_log(LOG_ERR, "Failed to create PID file: %s", strerror(errno));
570 daemon_retval_send(1);
573 wrote_pid_file = TRUE;
575 if (run_daemon() < 0)
578 } else if (command == DAEMON_HELP) {
581 } else if (command == DAEMON_VERSION) {
582 printf("%s "PACKAGE_VERSION"\n", argv0);
584 } else if (command == DAEMON_KILL) {
585 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
586 daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
590 } else if (command == DAEMON_RELOAD) {
591 if (daemon_pid_file_kill(SIGHUP) < 0) {
592 daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
602 daemon_retval_done();
605 daemon_pid_file_remove();