2 This file is part of avahi.
4 avahi is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 avahi is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include <sys/select.h>
27 #include <sys/socket.h>
32 #include <sys/ioctl.h>
41 #include <avahi-common/llist.h>
42 #include <avahi-common/malloc.h>
43 #include <avahi-common/address.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"
53 #define ENV_INTERFACE_DNS_SERVERS "AVAHI_INTERFACE_DNS_SERVERS"
54 #define ENV_DNS_SERVERS "AVAHI_DNS_SERVERS"
55 #define ENV_INTERFACE "AVAHI_INTERFACE"
69 } command = DAEMON_RUN;
72 static int daemonize = 0;
73 static int use_syslog = 0;
75 #if !HAVE_DECL_ENVIRON
76 extern char **environ;
79 typedef struct DNSServerInfo DNSServerInfo;
81 struct DNSServerInfo {
82 AvahiIfIndex interface;
83 AvahiProtocol protocol;
85 AVAHI_LLIST_FIELDS(DNSServerInfo, servers);
88 static AVAHI_LLIST_HEAD(DNSServerInfo, servers);
90 static void server_info_free(DNSServerInfo *i) {
93 avahi_free(i->address);
95 AVAHI_LLIST_REMOVE(DNSServerInfo, servers, servers, i);
99 static DNSServerInfo* get_server_info(AvahiIfIndex interface, AvahiProtocol protocol, const char *address) {
103 for (i = servers; i; i = i->servers_next)
104 if (i->interface == interface &&
105 i->protocol == protocol &&
106 strcmp(i->address, address) == 0)
112 static DNSServerInfo* new_server_info(AvahiIfIndex interface, AvahiProtocol protocol, const char *address) {
117 i = avahi_new(DNSServerInfo, 1);
118 i->interface = interface;
119 i->protocol = protocol;
120 i->address = avahi_strdup(address);
122 AVAHI_LLIST_PREPEND(DNSServerInfo, servers, servers, i);
127 static int set_cloexec(int fd) {
132 if ((n = fcntl(fd, F_GETFD)) < 0)
138 return fcntl(fd, F_SETFD, n|FD_CLOEXEC);
141 static int open_socket(void) {
143 struct sockaddr_un sa;
145 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
146 daemon_log(LOG_ERR, "socket(): %s", strerror(errno));
150 if (set_cloexec(fd) < 0) {
151 daemon_log(LOG_ERR, "fcntl(): %s", strerror(errno));
155 memset(&sa, 0, sizeof(sa));
156 sa.sun_family = AF_UNIX;
157 strncpy(sa.sun_path, AVAHI_SOCKET, sizeof(sa.sun_path)-1);
158 sa.sun_path[sizeof(sa.sun_path)-1] = 0;
160 if (connect(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0) {
161 daemon_log(LOG_ERR, "connect(): %s", strerror(errno));
162 daemon_log(LOG_INFO, "Failed to connect to the daemon. This probably means that you");
163 daemon_log(LOG_INFO, "didn't start avahi-daemon before avahi-dnsconfd.");
176 static ssize_t loop_write(int fd, const void*data, size_t size) {
179 assert(fd >= 0 && data && size);
184 if ((r = write(fd, data, size)) < 0)
191 data = (const uint8_t*) data + r;
198 static char *concat_dns_servers(AvahiIfIndex interface) {
202 for (i = servers; i; i = i->servers_next)
203 if (i->interface == interface || interface <= 0) {
207 /* Filter out double entries */
208 for (j = servers; j != i; j = j->servers_next)
209 if (j->interface == interface || interface <= 0)
210 if (strcmp(i->address, j->address) == 0)
217 t = avahi_strdup(i->address);
219 t = avahi_strdup_printf("%s %s", r, i->address);
228 static void set_env(const char *name, const char *value) {
237 for (e = environ; *e; e++) {
238 /* Search for the variable */
239 if (strlen(*e) < l+1)
242 if (strncmp(*e, name, l) != 0 || (*e)[l] != '=')
245 /* We simply free the record, sicne we know that we created it previously */
247 *e = avahi_strdup_printf("%s=%s", name, value);
254 static void run_script(int new, AvahiIfIndex interface, AvahiProtocol protocol, const char *address) {
258 char name[IF_NAMESIZE];
260 assert(interface > 0);
262 if (!if_indextoname(interface, name))
265 p = concat_dns_servers(interface);
266 set_env(ENV_INTERFACE_DNS_SERVERS, p ? p : "");
269 p = concat_dns_servers(-1);
270 set_env(ENV_DNS_SERVERS, p ? p : "");
273 set_env(ENV_INTERFACE, name);
275 snprintf(ia, sizeof(ia), "%i", (int) interface);
276 snprintf(pa, sizeof(pa), "%i", (int) protocol);
278 if (daemon_exec("/", &ret, AVAHI_DNSCONF_SCRIPT, AVAHI_DNSCONF_SCRIPT, new ? "+" : "-", address, ia, pa, avahi_proto_to_string(protocol), NULL) < 0)
279 daemon_log(LOG_WARNING, "Failed to run script");
281 daemon_log(LOG_WARNING, "Script returned with non-zero exit code %i", ret);
284 static int new_line(const char *l) {
287 if (state == ACKWAIT) {
289 daemon_log(LOG_ERR, "Avahi command failed: %s", l);
293 daemon_log(LOG_INFO, "Successfully connected to Avahi daemon.");
296 AvahiIfIndex interface;
297 AvahiProtocol protocol;
298 int i_interface, i_protocol, port;
299 char a[AVAHI_ADDRESS_STR_MAX];
301 assert(state == BROWSING);
303 if (*l != '<' && *l != '>') {
304 daemon_log(LOG_ERR, "Avahi sent us an invalid browsing line: %s", l);
308 if (sscanf(l+1, "%i %i %39s %i", &i_interface, &i_protocol, a, &port) != 4) {
309 daemon_log(LOG_ERR, "Failed to parse browsing line: %s", l);
313 interface = (AvahiIfIndex) i_interface;
314 protocol = (AvahiProtocol) i_protocol;
318 daemon_log(LOG_WARNING, "DNS server with port address != 53 found, ignoring");
320 daemon_log(LOG_INFO, "New DNS Server %s (interface: %i.%s)", a, interface, avahi_proto_to_string(protocol));
321 new_server_info(interface, protocol, a);
322 run_script(1, interface, protocol, a);
328 if ((i = get_server_info(interface, protocol, a))) {
329 daemon_log(LOG_INFO, "DNS Server %s removed (interface: %i.%s)", a, interface, avahi_proto_to_string(protocol));
331 run_script(0, interface, protocol, a);
340 static int do_connect(void) {
343 if ((fd = open_socket()) < 0)
346 if (loop_write(fd, BROWSE_DNS_SERVERS, sizeof(BROWSE_DNS_SERVERS)-1) < 0) {
347 daemon_log(LOG_ERR, "write(): %s", strerror(errno));
361 static void free_dns_server_info_list(void) {
363 AvahiIfIndex interface = servers->interface;
364 AvahiProtocol protocol = servers->protocol;
365 char *address = avahi_strdup(servers->address);
366 server_info_free(servers);
368 run_script(0, interface, protocol, address);
373 static void help(FILE *f, const char *argv0) {
376 " -h --help Show this help\n"
377 " -D --daemonize Daemonize after startup\n"
378 " -s --syslog Write log messages to syslog(3) instead of STDERR\n"
379 " -k --kill Kill a running daemon\n"
380 " -r --refresh Request a running daemon to refresh DNS server data\n"
381 " -c --check Return 0 if a daemon is already running\n"
382 " -V --version Show version\n",
386 static int parse_command_line(int argc, char *argv[]) {
389 static const struct option long_options[] = {
390 { "help", no_argument, NULL, 'h' },
391 { "daemonize", no_argument, NULL, 'D' },
392 { "syslog", no_argument, NULL, 's' },
393 { "kill", no_argument, NULL, 'k' },
394 { "version", no_argument, NULL, 'V' },
395 { "refresh", no_argument, NULL, 'r' },
396 { "check", no_argument, NULL, 'c' },
400 while ((c = getopt_long(argc, argv, "hDkVrcs", long_options, NULL)) >= 0) {
404 command = DAEMON_HELP;
413 command = DAEMON_KILL;
416 command = DAEMON_VERSION;
419 command = DAEMON_REFRESH;
422 command = DAEMON_CHECK;
430 fprintf(stderr, "Too many arguments\n");
437 static int run_daemon(void) {
438 int fd = -1, ret = -1;
442 AVAHI_LLIST_HEAD_INIT(DNSServerInfo, servers);
444 daemon_signal_init(SIGINT, SIGTERM, SIGCHLD, SIGHUP, 0);
446 /* Allocate some memory for our environment variables */
447 putenv(avahi_strdup(ENV_INTERFACE"="));
448 putenv(avahi_strdup(ENV_DNS_SERVERS"="));
449 putenv(avahi_strdup(ENV_INTERFACE_DNS_SERVERS"="));
451 if ((fd = do_connect()) < 0)
455 daemon_retval_send(0);
466 FD_SET(daemon_signal_fd(), &rfds);
469 if (select(fd+1, &rfds, NULL, NULL, NULL) < 0) {
473 daemon_log(LOG_ERR, "select(): %s", strerror(errno));
480 if (FD_ISSET(daemon_signal_fd(), &rfds)) {
484 if ((sig = daemon_signal_next()) <= 0) {
485 daemon_log(LOG_ERR, "daemon_signal_next() failed");
492 daemon_log(LOG_INFO, "Got %s, quitting.", sig == SIGINT ? "SIGINT" : "SIGTERM");
497 waitpid(-1, NULL, WNOHANG);
501 daemon_log(LOG_INFO, "Refreshing DNS Server list");
504 free_dns_server_info_list();
506 if ((fd = do_connect()) < 0)
512 } else if (FD_ISSET(fd, &rfds)) {
516 if ((r = read(fd, buf, sizeof(buf) - buflen - 1)) <= 0) {
517 daemon_log(LOG_ERR, "read(): %s", r < 0 ? strerror(errno) : "EOF");
522 assert(buflen <= sizeof(buf)-1);
524 while ((n = memchr(buf, '\n', buflen))) {
527 if (new_line(buf) < 0)
531 memmove(buf, n, buflen);
534 if (buflen >= sizeof(buf)-1) {
535 /* The incoming line is horribly long */
536 buf[sizeof(buf)-1] = 0;
538 if (new_line(buf) < 0)
548 free_dns_server_info_list();
553 daemon_signal_done();
555 if (ret != 0 && daemonize)
556 daemon_retval_send(1);
561 static const char* pid_file_proc(void) {
562 return AVAHI_RUNTIME_DIR"/avahi-dnsconfd.pid";
565 int main(int argc, char *argv[]) {
568 int wrote_pid_file = 0;
570 if ((argv0 = strrchr(argv[0], '/')))
575 daemon_pid_file_ident = daemon_log_ident = argv0;
576 daemon_pid_file_proc = pid_file_proc;
578 if (parse_command_line(argc, argv) < 0)
581 if (command == DAEMON_RUN) {
585 daemon_log(LOG_ERR, "This program is intended to be run as root.");
589 if ((pid = daemon_pid_file_is_running()) >= 0) {
590 daemon_log(LOG_ERR, "Daemon already running on PID %u", pid);
595 daemon_retval_init();
597 if ((pid = daemon_fork()) < 0)
603 if ((ret = daemon_retval_wait(20)) < 0) {
604 daemon_log(LOG_ERR, "Could not receive return value from daemon process.");
615 if (use_syslog || daemonize)
616 daemon_log_use = DAEMON_LOG_SYSLOG;
620 if (daemon_pid_file_create() < 0) {
621 daemon_log(LOG_ERR, "Failed to create PID file: %s", strerror(errno));
624 daemon_retval_send(1);
629 if (run_daemon() < 0)
633 } else if (command == DAEMON_HELP) {
637 } else if (command == DAEMON_VERSION) {
638 printf("%s "PACKAGE_VERSION"\n", argv0);
641 } else if (command == DAEMON_KILL) {
642 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
643 daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
648 } else if (command == DAEMON_REFRESH) {
649 if (daemon_pid_file_kill(SIGHUP) < 0) {
650 daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
655 } else if (command == DAEMON_CHECK)
656 r = (daemon_pid_file_is_running() >= 0) ? 0 : 1;
662 daemon_retval_done();
665 daemon_pid_file_remove();