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
28 #include <sys/select.h>
29 #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"
71 } command = DAEMON_RUN;
74 static int daemonize = 0;
76 #if !HAVE_DECL_ENVIRON
77 extern char **environ;
80 typedef struct DNSServerInfo DNSServerInfo;
82 struct DNSServerInfo {
83 AvahiIfIndex interface;
84 AvahiProtocol protocol;
86 AVAHI_LLIST_FIELDS(DNSServerInfo, servers);
89 static AVAHI_LLIST_HEAD(DNSServerInfo, servers);
91 static void server_info_free(DNSServerInfo *i) {
94 avahi_free(i->address);
96 AVAHI_LLIST_REMOVE(DNSServerInfo, servers, servers, i);
100 static DNSServerInfo* get_server_info(AvahiIfIndex interface, AvahiProtocol protocol, const char *address) {
104 for (i = servers; i; i = i->servers_next)
105 if (i->interface == interface &&
106 i->protocol == protocol &&
107 strcmp(i->address, address) == 0)
113 static DNSServerInfo* new_server_info(AvahiIfIndex interface, AvahiProtocol protocol, const char *address) {
118 i = avahi_new(DNSServerInfo, 1);
119 i->interface = interface;
120 i->protocol = protocol;
121 i->address = avahi_strdup(address);
123 AVAHI_LLIST_PREPEND(DNSServerInfo, servers, servers, i);
128 static int set_cloexec(int fd) {
133 if ((n = fcntl(fd, F_GETFD)) < 0)
139 return fcntl(fd, F_SETFD, n|FD_CLOEXEC);
142 static int open_socket(void) {
144 struct sockaddr_un sa;
146 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
147 daemon_log(LOG_ERR, "socket(): %s", strerror(errno));
151 if (set_cloexec(fd) < 0) {
152 daemon_log(LOG_ERR, "fcntl(): %s", strerror(errno));
156 memset(&sa, 0, sizeof(sa));
157 sa.sun_family = AF_UNIX;
158 strncpy(sa.sun_path, AVAHI_SOCKET, sizeof(sa.sun_path)-1);
159 sa.sun_path[sizeof(sa.sun_path)-1] = 0;
161 if (connect(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0) {
162 daemon_log(LOG_ERR, "connect(): %s", strerror(errno));
163 daemon_log(LOG_INFO, "Failed to connect to the daemon. This probably means that you");
164 daemon_log(LOG_INFO, "didn't start avahi-daemon before avahi-dnsconfd.");
177 static ssize_t loop_write(int fd, const void*data, size_t size) {
180 assert(fd >= 0 && data && size);
185 if ((r = write(fd, data, size)) < 0)
192 data = (const uint8_t*) data + r;
199 static char *concat_dns_servers(AvahiIfIndex interface) {
203 for (i = servers; i; i = i->servers_next)
204 if (i->interface == interface || interface <= 0) {
208 /* Filter out double entries */
209 for (j = servers; j != i; j = j->servers_next)
210 if (j->interface == interface || interface <= 0)
211 if (strcmp(i->address, j->address) == 0)
218 t = avahi_strdup(i->address);
220 t = avahi_strdup_printf("%s %s", r, i->address);
229 static void set_env(const char *name, const char *value) {
238 for (e = environ; *e; e++) {
239 /* Search for the variable */
240 if (strlen(*e) < l+1)
243 if (strncmp(*e, name, l) != 0 || (*e)[l] != '=')
246 /* We simply free the record, sicne we know that we created it previously */
248 *e = avahi_strdup_printf("%s=%s", name, value);
255 static void run_script(int new, AvahiIfIndex interface, AvahiProtocol protocol, const char *address) {
259 char name[IF_NAMESIZE];
261 assert(interface > 0);
263 if (!if_indextoname(interface, name))
266 p = concat_dns_servers(interface);
267 set_env(ENV_INTERFACE_DNS_SERVERS, p ? p : "");
270 p = concat_dns_servers(-1);
271 set_env(ENV_DNS_SERVERS, p ? p : "");
274 set_env(ENV_INTERFACE, name);
276 snprintf(ia, sizeof(ia), "%i", (int) interface);
277 snprintf(pa, sizeof(pa), "%i", (int) protocol);
279 if (daemon_exec("/", &ret, AVAHI_DNSCONF_SCRIPT, AVAHI_DNSCONF_SCRIPT, new ? "+" : "-", address, ia, pa, avahi_proto_to_string(protocol), NULL) < 0)
280 daemon_log(LOG_WARNING, "Failed to run script");
282 daemon_log(LOG_WARNING, "Script returned with non-zero exit code %i", ret);
285 static int new_line(const char *l) {
288 if (state == ACKWAIT) {
290 daemon_log(LOG_ERR, "Avahi command failed: %s", l);
294 daemon_log(LOG_INFO, "Successfully connected to Avahi daemon.");
297 AvahiIfIndex interface;
298 AvahiProtocol protocol;
299 int i_interface, i_protocol, port;
300 char a[AVAHI_ADDRESS_STR_MAX];
302 assert(state == BROWSING);
304 if (*l != '<' && *l != '>') {
305 daemon_log(LOG_ERR, "Avahi sent us an invalid browsing line: %s", l);
309 if (sscanf(l+1, "%i %i %39s %i", &i_interface, &i_protocol, a, &port) != 4) {
310 daemon_log(LOG_ERR, "Failed to parse browsing line: %s", l);
314 interface = (AvahiIfIndex) i_interface;
315 protocol = (AvahiProtocol) i_protocol;
319 daemon_log(LOG_WARNING, "DNS server with port address != 53 found, ignoring");
321 daemon_log(LOG_INFO, "New DNS Server %s (interface: %i.%s)", a, interface, avahi_proto_to_string(protocol));
322 new_server_info(interface, protocol, a);
323 run_script(1, interface, protocol, a);
329 if ((i = get_server_info(interface, protocol, a))) {
330 daemon_log(LOG_INFO, "DNS Server %s removed (interface: %i.%s)", a, interface, avahi_proto_to_string(protocol));
332 run_script(0, interface, protocol, a);
341 static int do_connect(void) {
344 if ((fd = open_socket()) < 0)
347 if (loop_write(fd, BROWSE_DNS_SERVERS, sizeof(BROWSE_DNS_SERVERS)-1) < 0) {
348 daemon_log(LOG_ERR, "write(): %s", strerror(errno));
362 static void free_dns_server_info_list(void) {
364 AvahiIfIndex interface = servers->interface;
365 AvahiProtocol protocol = servers->protocol;
366 char *address = avahi_strdup(servers->address);
367 server_info_free(servers);
369 run_script(0, interface, protocol, address);
374 static void help(FILE *f, const char *argv0) {
377 " -h --help Show this help\n"
378 " -D --daemonize Daemonize after startup\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 { "kill", no_argument, NULL, 'k' },
393 { "version", no_argument, NULL, 'V' },
394 { "refresh", no_argument, NULL, 'r' },
395 { "check", no_argument, NULL, 'c' },
399 while ((c = getopt_long(argc, argv, "hDkVrc", long_options, NULL)) >= 0) {
403 command = DAEMON_HELP;
409 command = DAEMON_KILL;
412 command = DAEMON_VERSION;
415 command = DAEMON_REFRESH;
418 command = DAEMON_CHECK;
426 fprintf(stderr, "Too many arguments\n");
433 static int run_daemon(void) {
434 int fd = -1, ret = -1;
438 AVAHI_LLIST_HEAD_INIT(DNSServerInfo, servers);
440 daemon_signal_init(SIGINT, SIGTERM, SIGCHLD, SIGHUP, 0);
442 /* Allocate some memory for our environment variables */
443 putenv(avahi_strdup(ENV_INTERFACE"="));
444 putenv(avahi_strdup(ENV_DNS_SERVERS"="));
445 putenv(avahi_strdup(ENV_INTERFACE_DNS_SERVERS"="));
447 if ((fd = do_connect()) < 0)
451 daemon_retval_send(0);
462 FD_SET(daemon_signal_fd(), &rfds);
465 if (select(fd+1, &rfds, NULL, NULL, NULL) < 0) {
469 daemon_log(LOG_ERR, "select(): %s", strerror(errno));
476 if (FD_ISSET(daemon_signal_fd(), &rfds)) {
480 if ((sig = daemon_signal_next()) <= 0) {
481 daemon_log(LOG_ERR, "daemon_signal_next() failed");
488 daemon_log(LOG_INFO, "Got %s, quitting.", sig == SIGINT ? "SIGINT" : "SIGTERM");
493 waitpid(-1, NULL, WNOHANG);
497 daemon_log(LOG_INFO, "Refreshing DNS Server list");
500 free_dns_server_info_list();
502 if ((fd = do_connect()) < 0)
508 } else if (FD_ISSET(fd, &rfds)) {
512 if ((r = read(fd, buf, sizeof(buf) - buflen - 1)) <= 0) {
513 daemon_log(LOG_ERR, "read(): %s", r < 0 ? strerror(errno) : "EOF");
518 assert(buflen <= sizeof(buf)-1);
520 while ((n = memchr(buf, '\n', buflen))) {
523 if (new_line(buf) < 0)
527 memmove(buf, n, buflen);
530 if (buflen >= sizeof(buf)-1) {
531 /* The incoming line is horribly long */
532 buf[sizeof(buf)-1] = 0;
534 if (new_line(buf) < 0)
544 free_dns_server_info_list();
549 daemon_signal_done();
551 if (ret != 0 && daemonize)
552 daemon_retval_send(1);
557 static const char* pid_file_proc(void) {
558 return AVAHI_RUNTIME_DIR"/avahi-dnsconfd.pid";
561 int main(int argc, char *argv[]) {
564 int wrote_pid_file = 0;
566 if ((argv0 = strrchr(argv[0], '/')))
571 daemon_pid_file_ident = daemon_log_ident = argv0;
572 daemon_pid_file_proc = pid_file_proc;
574 if (parse_command_line(argc, argv) < 0)
577 if (command == DAEMON_RUN) {
581 daemon_log(LOG_ERR, "This program is intended to be run as root.");
585 if ((pid = daemon_pid_file_is_running()) >= 0) {
586 daemon_log(LOG_ERR, "Daemon already running on PID %u", pid);
591 daemon_retval_init();
593 if ((pid = daemon_fork()) < 0)
599 if ((ret = daemon_retval_wait(20)) < 0) {
600 daemon_log(LOG_ERR, "Could not receive return value from daemon process.");
613 if (daemon_pid_file_create() < 0) {
614 daemon_log(LOG_ERR, "Failed to create PID file: %s", strerror(errno));
617 daemon_retval_send(1);
622 if (run_daemon() < 0)
626 } else if (command == DAEMON_HELP) {
630 } else if (command == DAEMON_VERSION) {
631 printf("%s "PACKAGE_VERSION"\n", argv0);
634 } else if (command == DAEMON_KILL) {
635 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
636 daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
641 } else if (command == DAEMON_REFRESH) {
642 if (daemon_pid_file_kill(SIGHUP) < 0) {
643 daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
648 } else if (command == DAEMON_CHECK)
649 r = (daemon_pid_file_is_running() >= 0) ? 0 : 1;
655 daemon_retval_done();
658 daemon_pid_file_remove();