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>
45 #include <avahi-common/address.h>
47 #include <libdaemon/dfork.h>
48 #include <libdaemon/dsignal.h>
49 #include <libdaemon/dlog.h>
50 #include <libdaemon/dpid.h>
51 #include <libdaemon/dexec.h>
53 #define BROWSE_DNS_SERVERS "BROWSE-DNS-SERVERS\n"
55 #define ENV_INTERFACE_DNS_SERVERS "AVAHI_INTERFACE_DNS_SERVERS"
56 #define ENV_DNS_SERVERS "AVAHI_DNS_SERVERS"
57 #define ENV_INTERFACE "AVAHI_INTERFACE"
73 } command = DAEMON_RUN;
75 static int daemonize = 0;
77 typedef struct DNSServerInfo DNSServerInfo;
79 struct DNSServerInfo {
80 AvahiIfIndex interface;
81 AvahiProtocol protocol;
83 AVAHI_LLIST_FIELDS(DNSServerInfo, servers);
86 static AVAHI_LLIST_HEAD(DNSServerInfo, servers);
88 static void server_info_free(DNSServerInfo *i) {
91 avahi_free(i->address);
93 AVAHI_LLIST_REMOVE(DNSServerInfo, servers, servers, i);
97 static DNSServerInfo* get_server_info(AvahiIfIndex interface, AvahiProtocol protocol, const char *address) {
101 for (i = servers; i; i = i->servers_next)
102 if (i->interface == interface &&
103 i->protocol == protocol &&
104 strcmp(i->address, address) == 0)
110 static DNSServerInfo* new_server_info(AvahiIfIndex interface, AvahiProtocol protocol, const char *address) {
115 i = avahi_new(DNSServerInfo, 1);
116 i->interface = interface;
117 i->protocol = protocol;
118 i->address = avahi_strdup(address);
120 AVAHI_LLIST_PREPEND(DNSServerInfo, servers, servers, i);
125 static int set_cloexec(int fd) {
130 if ((n = fcntl(fd, F_GETFD)) < 0)
136 return fcntl(fd, F_SETFD, n|FD_CLOEXEC);
139 static int open_socket(void) {
141 struct sockaddr_un sa;
143 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
144 daemon_log(LOG_ERR, "socket(): %s", strerror(errno));
148 if (set_cloexec(fd) < 0) {
149 daemon_log(LOG_ERR, "fcntl(): %s", strerror(errno));
153 memset(&sa, 0, sizeof(sa));
154 sa.sun_family = AF_UNIX;
155 strncpy(sa.sun_path, AVAHI_SOCKET, sizeof(sa.sun_path)-1);
156 sa.sun_path[sizeof(sa.sun_path)-1] = 0;
158 if (connect(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0) {
159 daemon_log(LOG_ERR, "connect(): %s", strerror(errno));
160 daemon_log(LOG_INFO, "Failed to connect to the daemon. This probably means that you");
161 daemon_log(LOG_INFO, "didn't start avahi-daemon before avahi-dnsconfd.");
174 static ssize_t loop_write(int fd, const void*data, size_t size) {
177 assert(fd >= 0 && data && size);
182 if ((r = write(fd, data, size)) < 0)
189 data = (const uint8_t*) data + r;
196 static char *concat_dns_servers(AvahiIfIndex interface) {
200 for (i = servers; i; i = i->servers_next)
201 if (i->interface == interface || interface <= 0) {
205 t = avahi_strdup(i->address);
207 t = avahi_strdup_printf("%s %s", r, i->address);
216 static char *getifname(AvahiIfIndex interface, char *name, size_t len) {
221 assert(interface >= 0);
223 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
224 daemon_log(LOG_ERR, "socket(): %s", strerror(errno));
228 memset(&ifr, 0, sizeof(ifr));
229 ifr.ifr_ifindex = (int) interface;
231 if (ioctl(fd, SIOCGIFNAME, &ifr) < 0) {
232 daemon_log(LOG_ERR, "SIOCGIFNAME: %s\n", strerror(errno));
236 strncpy(name, ifr.ifr_name, len-1);
247 static void set_env(const char *name, const char *value) {
256 for (e = environ; *e; e++) {
257 /* Search for the variable */
258 if (strlen(*e) < l+1)
261 if (strncmp(*e, name, l) != 0 || (*e)[l] != '=')
264 /* We simply free the record, sicne we know that we created it previously */
266 *e = avahi_strdup_printf("%s=%s", name, value);
273 static void run_script(int new, AvahiIfIndex interface, AvahiProtocol protocol, const char *address) {
277 char name[IFNAMSIZ+1];
279 assert(interface > 0);
281 if (!getifname(interface, name, sizeof(name)))
284 p = concat_dns_servers(interface);
285 set_env(ENV_INTERFACE_DNS_SERVERS, p ? p : "");
288 p = concat_dns_servers(-1);
289 set_env(ENV_DNS_SERVERS, p ? p : "");
292 set_env(ENV_INTERFACE, name);
294 snprintf(ia, sizeof(ia), "%i", (int) interface);
295 snprintf(pa, sizeof(pa), "%i", (int) protocol);
297 if (daemon_exec("/", &ret, AVAHI_DNSCONF_SCRIPT, AVAHI_DNSCONF_SCRIPT, new ? "+" : "-", address, ia, pa, avahi_proto_to_string(protocol), NULL) < 0)
298 daemon_log(LOG_WARNING, "Failed to run script");
300 daemon_log(LOG_WARNING, "Script returned with non-zero exit code %i", ret);
303 static int new_line(const char *l) {
306 if (state == ACKWAIT) {
308 daemon_log(LOG_ERR, "Avahi command failed: %s", l);
312 daemon_log(LOG_INFO, "Successfully connected to Avahi daemon.");
315 AvahiIfIndex interface;
316 AvahiProtocol protocol;
317 int i_interface, i_protocol, port;
320 assert(state == BROWSING);
322 if (*l != '<' && *l != '>') {
323 daemon_log(LOG_ERR, "Avahi sent us an invalid browsing line: %s", l);
327 if (sscanf(l+1, "%i %i %64s %i", &i_interface, &i_protocol, a, &port) != 4) {
328 daemon_log(LOG_ERR, "Failed to parse browsing line: %s", l);
332 interface = (AvahiIfIndex) i_interface;
333 protocol = (AvahiProtocol) i_protocol;
337 daemon_log(LOG_WARNING, "DNS server with port address != 53 found, ignoring");
339 daemon_log(LOG_INFO, "New DNS Server %s (interface: %i.%s)", a, interface, avahi_proto_to_string(protocol));
340 new_server_info(interface, protocol, a);
341 run_script(1, interface, protocol, a);
347 if ((i = get_server_info(interface, protocol, a))) {
348 daemon_log(LOG_INFO, "DNS Server %s removed (interface: %i.%s)", a, interface, avahi_proto_to_string(protocol));
350 run_script(0, interface, protocol, a);
359 static int do_connect(void) {
362 if ((fd = open_socket()) < 0)
365 if (loop_write(fd, BROWSE_DNS_SERVERS, sizeof(BROWSE_DNS_SERVERS)-1) < 0) {
366 daemon_log(LOG_ERR, "write(): %s", strerror(errno));
380 static void free_dns_server_info_list(void) {
382 AvahiIfIndex interface = servers->interface;
383 AvahiProtocol protocol = servers->protocol;
384 char *address = avahi_strdup(servers->address);
385 server_info_free(servers);
387 run_script(0, interface, protocol, address);
392 static void help(FILE *f, const char *argv0) {
395 " -h --help Show this help\n"
396 " -D --daemonize Daemonize after startup\n"
397 " -k --kill Kill a running daemon\n"
398 " -r --refresh Request a running daemon to refresh DNS server data\n"
399 " -c --check Return 0 if a daemon is already running\n"
400 " -V --version Show version\n",
404 static int parse_command_line(int argc, char *argv[]) {
407 static const struct option long_options[] = {
408 { "help", no_argument, NULL, 'h' },
409 { "daemonize", no_argument, NULL, 'D' },
410 { "kill", no_argument, NULL, 'k' },
411 { "version", no_argument, NULL, 'V' },
412 { "refresh", no_argument, NULL, 'r' },
413 { "check", no_argument, NULL, 'c' },
417 while ((c = getopt_long(argc, argv, "hDkVrc", long_options, NULL)) >= 0) {
421 command = DAEMON_HELP;
427 command = DAEMON_KILL;
430 command = DAEMON_VERSION;
433 command = DAEMON_REFRESH;
436 command = DAEMON_CHECK;
439 fprintf(stderr, "Invalid command line argument: %c\n", c);
445 fprintf(stderr, "Too many arguments\n");
452 static int run_daemon(void) {
453 int fd = -1, ret = -1;
457 AVAHI_LLIST_HEAD_INIT(DNSServerInfo, servers);
459 daemon_signal_init(SIGINT, SIGTERM, SIGCHLD, SIGHUP, 0);
461 /* Allocate some memory for our environment variables */
462 putenv(avahi_strdup(ENV_INTERFACE"="));
463 putenv(avahi_strdup(ENV_DNS_SERVERS"="));
464 putenv(avahi_strdup(ENV_INTERFACE_DNS_SERVERS"="));
466 if ((fd = do_connect()) < 0)
470 daemon_retval_send(0);
481 FD_SET(daemon_signal_fd(), &rfds);
484 if (select(fd+1, &rfds, NULL, NULL, NULL) < 0) {
488 daemon_log(LOG_ERR, "select(): %s", strerror(errno));
495 if (FD_ISSET(daemon_signal_fd(), &rfds)) {
499 if ((sig = daemon_signal_next()) <= 0) {
500 daemon_log(LOG_ERR, "daemon_signal_next() failed");
507 daemon_log(LOG_INFO, "Got %s, quitting.", sig == SIGINT ? "SIGINT" : "SIGTERM");
512 waitpid(-1, NULL, WNOHANG);
516 daemon_log(LOG_INFO, "Refreshing DNS Server list");
519 free_dns_server_info_list();
521 if ((fd = do_connect()) < 0)
527 } else if (FD_ISSET(fd, &rfds)) {
531 if ((r = read(fd, buf, sizeof(buf) - buflen - 1)) <= 0) {
532 daemon_log(LOG_ERR, "read(): %s", r < 0 ? strerror(errno) : "EOF");
537 assert(buflen <= sizeof(buf)-1);
539 while ((n = memchr(buf, '\n', buflen))) {
542 if (new_line(buf) < 0)
546 memmove(buf, n, buflen);
549 if (buflen >= sizeof(buf)-1) {
550 /* The incoming line is horribly long */
551 buf[sizeof(buf)-1] = 0;
553 if (new_line(buf) < 0)
563 free_dns_server_info_list();
568 daemon_signal_done();
570 if (ret != 0 && daemonize)
571 daemon_retval_send(1);
576 static const char* pid_file_proc(void) {
577 return AVAHI_RUNTIME_DIR"/avahi-dnsconfd.pid";
580 int main(int argc, char *argv[]) {
583 int wrote_pid_file = 0;
585 if ((argv0 = strrchr(argv[0], '/')))
590 daemon_pid_file_ident = daemon_log_ident = argv0;
591 daemon_pid_file_proc = pid_file_proc;
593 if (parse_command_line(argc, argv) < 0)
596 if (command == DAEMON_RUN) {
600 daemon_log(LOG_ERR, "This program is intended to be run as root.");
604 if ((pid = daemon_pid_file_is_running()) >= 0) {
605 daemon_log(LOG_ERR, "Daemon already running on PID %u", pid);
610 daemon_retval_init();
612 if ((pid = daemon_fork()) < 0)
618 if ((ret = daemon_retval_wait(20)) < 0) {
619 daemon_log(LOG_ERR, "Could not recieve return value from daemon process.");
632 if (daemon_pid_file_create() < 0) {
633 daemon_log(LOG_ERR, "Failed to create PID file: %s", strerror(errno));
636 daemon_retval_send(1);
641 if (run_daemon() < 0)
645 } else if (command == DAEMON_HELP) {
649 } else if (command == DAEMON_VERSION) {
650 printf("%s "PACKAGE_VERSION"\n", argv0);
653 } else if (command == DAEMON_KILL) {
654 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
655 daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
660 } else if (command == DAEMON_REFRESH) {
661 if (daemon_pid_file_kill(SIGHUP) < 0) {
662 daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
667 } else if (command == DAEMON_CHECK)
668 r = (daemon_pid_file_is_running() >= 0) ? 0 : 1;
674 daemon_retval_done();
677 daemon_pid_file_remove();