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>
43 #include <avahi-common/llist.h>
44 #include <avahi-common/malloc.h>
46 #include <libdaemon/dfork.h>
47 #include <libdaemon/dsignal.h>
48 #include <libdaemon/dlog.h>
49 #include <libdaemon/dpid.h>
50 #include <libdaemon/dexec.h>
52 #define BROWSE_DNS_SERVERS "BROWSE-DNS-SERVERS\n"
54 #define ENV_INTERFACE_DNS_SERVERS "AVAHI_INTERFACE_DNS_SERVERS"
55 #define ENV_DNS_SERVERS "AVAHI_DNS_SERVERS"
56 #define ENV_INTERFACE "AVAHI_INTERFACE"
72 } command = DAEMON_RUN;
74 static int daemonize = 0;
76 typedef struct DNSServerInfo DNSServerInfo;
78 struct DNSServerInfo {
82 AVAHI_LLIST_FIELDS(DNSServerInfo, servers);
85 static AVAHI_LLIST_HEAD(DNSServerInfo, servers);
87 static void server_info_free(DNSServerInfo *i) {
90 avahi_free(i->address);
92 AVAHI_LLIST_REMOVE(DNSServerInfo, servers, servers, i);
96 static DNSServerInfo* get_server_info(int interface, int protocol, const char *address) {
100 for (i = servers; i; i = i->servers_next)
101 if (i->interface == interface &&
102 i->protocol == protocol &&
103 strcmp(i->address, address) == 0)
109 static DNSServerInfo* new_server_info(int interface, int protocol, const char *address) {
114 i = avahi_new(DNSServerInfo, 1);
115 i->interface = interface;
116 i->protocol = protocol;
117 i->address = avahi_strdup(address);
119 AVAHI_LLIST_PREPEND(DNSServerInfo, servers, servers, i);
124 static int set_cloexec(int fd) {
129 if ((n = fcntl(fd, F_GETFD)) < 0)
135 return fcntl(fd, F_SETFD, n|FD_CLOEXEC);
138 static int open_socket(void) {
140 struct sockaddr_un sa;
142 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
143 daemon_log(LOG_ERR, "socket(): %s", strerror(errno));
147 if (set_cloexec(fd) < 0) {
148 daemon_log(LOG_ERR, "fcntl(): %s", strerror(errno));
152 memset(&sa, 0, sizeof(sa));
153 sa.sun_family = AF_UNIX;
154 strncpy(sa.sun_path, AVAHI_SOCKET, sizeof(sa.sun_path)-1);
155 sa.sun_path[sizeof(sa.sun_path)-1] = 0;
157 if (connect(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0) {
158 daemon_log(LOG_ERR, "connect(): %s", strerror(errno));
159 daemon_log(LOG_INFO, "Failed to connect to the daemon. This probably means that you");
160 daemon_log(LOG_INFO, "didn't start avahi-daemon before avahi-dnsconfd.");
173 static ssize_t loop_write(int fd, const void*data, size_t size) {
176 assert(fd >= 0 && data && size);
181 if ((r = write(fd, data, size)) < 0)
188 data = (const uint8_t*) data + r;
195 static char *concat_dns_servers(int interface) {
199 for (i = servers; i; i = i->servers_next)
200 if (i->interface == interface || interface <= 0) {
204 t = avahi_strdup(i->address);
206 t = avahi_strdup_printf("%s %s", r, i->address);
215 static char *getifname(int interface, char *name, size_t len) {
220 assert(interface >= 0);
222 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
223 daemon_log(LOG_ERR, "socket(): %s", strerror(errno));
227 memset(&ifr, 0, sizeof(ifr));
228 ifr.ifr_ifindex = interface;
230 if (ioctl(fd, SIOCGIFNAME, &ifr) < 0) {
231 daemon_log(LOG_ERR, "SIOCGIFNAME: %s\n", strerror(errno));
235 strncpy(name, ifr.ifr_name, len-1);
246 static void set_env(const char *name, const char *value) {
255 for (e = environ; *e; e++) {
256 /* Search for the variable */
257 if (strlen(*e) < l+1)
260 if (strncmp(*e, name, l) != 0 || *e[l] != '=')
263 /* We simply free the record, sicne we know that we created it previously */
265 *e = avahi_strdup_printf("%s=%s", name, value);
272 static void run_script(int new, int interface, int protocol, const char *address) {
276 char name[IFNAMSIZ+1];
278 assert(interface > 0);
280 if (!getifname(interface, name, sizeof(name)))
283 p = concat_dns_servers(interface);
284 set_env(ENV_INTERFACE_DNS_SERVERS, p ? p : "");
287 p = concat_dns_servers(-1);
288 set_env(ENV_DNS_SERVERS, p ? p : "");
291 set_env(ENV_INTERFACE, name);
293 snprintf(ia, sizeof(ia), "%i", interface);
294 snprintf(pa, sizeof(pa), "%i", protocol);
296 if (daemon_exec("/", &ret, AVAHI_DNSCONF_SCRIPT, AVAHI_DNSCONF_SCRIPT, new ? "+" : "-", address, ia, pa, NULL) < 0)
297 daemon_log(LOG_WARNING, "Failed to run script");
299 daemon_log(LOG_WARNING, "Script returned with non-zero exit code %i", ret);
302 static int new_line(const char *l) {
305 if (state == ACKWAIT) {
307 daemon_log(LOG_ERR, "Avahi command failed: %s", l);
311 daemon_log(LOG_INFO, "Successfully connected to Avahi daemon.");
319 assert(state == BROWSING);
321 if (*l != '<' && *l != '>') {
322 daemon_log(LOG_ERR, "Avahi sent us an invalid browsing line: %s", l);
326 if (sscanf(l+1, "%i %i %64s %i", &interface, &protocol, a, &port) != 4) {
327 daemon_log(LOG_ERR, "Failed to parse browsing line: %s", l);
333 daemon_log(LOG_WARNING, "DNS server with port address != 53 found, ignoring");
335 daemon_log(LOG_INFO, "New DNS Server %s (interface: %i.%u)", a, interface, protocol);
336 new_server_info(interface, protocol, a);
337 run_script(1, interface, protocol, a);
343 if ((i = get_server_info(interface, protocol, a))) {
344 daemon_log(LOG_INFO, "DNS Server %s removed (interface: %i.%u)", a, interface, protocol);
346 run_script(0, interface, protocol, a);
355 static int do_connect(void) {
358 if ((fd = open_socket()) < 0)
361 if (loop_write(fd, BROWSE_DNS_SERVERS, sizeof(BROWSE_DNS_SERVERS)-1) < 0) {
362 daemon_log(LOG_ERR, "write(): %s", strerror(errno));
376 static void free_dns_server_info_list(void) {
378 int interface = servers->interface;
379 int protocol = servers->protocol;
380 char *address = avahi_strdup(servers->address);
381 server_info_free(servers);
383 run_script(0, interface, protocol, address);
388 static void help(FILE *f, const char *argv0) {
391 " -h --help Show this help\n"
392 " -D --daemonize Daemonize after startup\n"
393 " -k --kill Kill a running daemon\n"
394 " -r --refresh Request a running daemon to refresh DNS server data\n"
395 " -c --check Return 0 if a daemon is already running\n"
396 " -V --version Show version\n",
400 static int parse_command_line(int argc, char *argv[]) {
403 static const struct option long_options[] = {
404 { "help", no_argument, NULL, 'h' },
405 { "daemonize", no_argument, NULL, 'D' },
406 { "kill", no_argument, NULL, 'k' },
407 { "version", no_argument, NULL, 'V' },
408 { "refresh", no_argument, NULL, 'r' },
409 { "check", no_argument, NULL, 'c' },
413 while ((c = getopt_long(argc, argv, "hDkVrc", long_options, NULL)) >= 0) {
417 command = DAEMON_HELP;
423 command = DAEMON_KILL;
426 command = DAEMON_VERSION;
429 command = DAEMON_REFRESH;
432 command = DAEMON_CHECK;
435 fprintf(stderr, "Invalid command line argument: %c\n", c);
441 fprintf(stderr, "Too many arguments\n");
448 static int run_daemon(void) {
449 int fd = -1, ret = -1;
453 AVAHI_LLIST_HEAD_INIT(DNSServerInfo, servers);
455 daemon_signal_init(SIGINT, SIGTERM, SIGCHLD, SIGHUP, 0);
457 /* Allocate some memory for our environment variables */
458 putenv(avahi_strdup(ENV_INTERFACE"="));
459 putenv(avahi_strdup(ENV_DNS_SERVERS"="));
460 putenv(avahi_strdup(ENV_INTERFACE_DNS_SERVERS"="));
462 if ((fd = do_connect()) < 0)
466 daemon_retval_send(0);
477 FD_SET(daemon_signal_fd(), &rfds);
480 if (select(fd+1, &rfds, NULL, NULL, NULL) < 0) {
484 daemon_log(LOG_ERR, "select(): %s", strerror(errno));
491 if (FD_ISSET(daemon_signal_fd(), &rfds)) {
495 if ((sig = daemon_signal_next()) <= 0) {
496 daemon_log(LOG_ERR, "daemon_signal_next() failed");
503 daemon_log(LOG_INFO, "Got %s, quitting.", sig == SIGINT ? "SIGINT" : "SIGTERM");
508 waitpid(-1, NULL, WNOHANG);
512 daemon_log(LOG_INFO, "Refreshing DNS Server list");
515 free_dns_server_info_list();
517 if ((fd = do_connect()) < 0)
523 } else if (FD_ISSET(fd, &rfds)) {
527 if ((r = read(fd, buf, sizeof(buf) - buflen - 1)) <= 0) {
528 daemon_log(LOG_ERR, "read(): %s", r < 0 ? strerror(errno) : "EOF");
533 assert(buflen <= sizeof(buf)-1);
535 while ((n = memchr(buf, '\n', buflen))) {
538 if (new_line(buf) < 0)
542 memmove(buf, n, buflen);
545 if (buflen >= sizeof(buf)-1) {
546 /* The incoming line is horribly long */
547 buf[sizeof(buf)-1] = 0;
549 if (new_line(buf) < 0)
559 free_dns_server_info_list();
564 daemon_signal_done();
566 if (ret != 0 && daemonize)
567 daemon_retval_send(1);
572 static const char* pid_file_proc(void) {
573 return AVAHI_RUNTIME_DIR"/avahi-dnsconfd.pid";
576 int main(int argc, char *argv[]) {
579 int wrote_pid_file = 0;
581 if ((argv0 = strrchr(argv[0], '/')))
586 daemon_pid_file_ident = daemon_log_ident = argv0;
587 daemon_pid_file_proc = pid_file_proc;
589 if (parse_command_line(argc, argv) < 0)
592 if (command == DAEMON_RUN) {
596 daemon_log(LOG_ERR, "This program is intended to be run as root.");
600 if ((pid = daemon_pid_file_is_running()) >= 0) {
601 daemon_log(LOG_ERR, "Daemon already running on PID %u", pid);
606 daemon_retval_init();
608 if ((pid = daemon_fork()) < 0)
614 if ((ret = daemon_retval_wait(20)) < 0) {
615 daemon_log(LOG_ERR, "Could not recieve return value from daemon process.");
628 if (daemon_pid_file_create() < 0) {
629 daemon_log(LOG_ERR, "Failed to create PID file: %s", strerror(errno));
632 daemon_retval_send(1);
637 if (run_daemon() < 0)
641 } else if (command == DAEMON_HELP) {
645 } else if (command == DAEMON_VERSION) {
646 printf("%s "PACKAGE_VERSION"\n", argv0);
649 } else if (command == DAEMON_KILL) {
650 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
651 daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
656 } else if (command == DAEMON_REFRESH) {
657 if (daemon_pid_file_kill(SIGHUP) < 0) {
658 daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
663 } else if (command == DAEMON_CHECK)
664 r = (daemon_pid_file_is_running() >= 0) ? 0 : 1;
670 daemon_retval_done();
673 daemon_pid_file_remove();