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;
74 #if !HAVE_DECL_ENVIRON
75 extern char **environ;
78 typedef struct DNSServerInfo DNSServerInfo;
80 struct DNSServerInfo {
81 AvahiIfIndex interface;
82 AvahiProtocol protocol;
84 AVAHI_LLIST_FIELDS(DNSServerInfo, servers);
87 static AVAHI_LLIST_HEAD(DNSServerInfo, servers);
89 static void server_info_free(DNSServerInfo *i) {
92 avahi_free(i->address);
94 AVAHI_LLIST_REMOVE(DNSServerInfo, servers, servers, i);
98 static DNSServerInfo* get_server_info(AvahiIfIndex interface, AvahiProtocol protocol, const char *address) {
102 for (i = servers; i; i = i->servers_next)
103 if (i->interface == interface &&
104 i->protocol == protocol &&
105 strcmp(i->address, address) == 0)
111 static DNSServerInfo* new_server_info(AvahiIfIndex interface, AvahiProtocol protocol, const char *address) {
116 i = avahi_new(DNSServerInfo, 1);
117 i->interface = interface;
118 i->protocol = protocol;
119 i->address = avahi_strdup(address);
121 AVAHI_LLIST_PREPEND(DNSServerInfo, servers, servers, i);
126 static int set_cloexec(int fd) {
131 if ((n = fcntl(fd, F_GETFD)) < 0)
137 return fcntl(fd, F_SETFD, n|FD_CLOEXEC);
140 static int open_socket(void) {
142 struct sockaddr_un sa;
144 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
145 daemon_log(LOG_ERR, "socket(): %s", strerror(errno));
149 if (set_cloexec(fd) < 0) {
150 daemon_log(LOG_ERR, "fcntl(): %s", strerror(errno));
154 memset(&sa, 0, sizeof(sa));
155 sa.sun_family = AF_UNIX;
156 strncpy(sa.sun_path, AVAHI_SOCKET, sizeof(sa.sun_path)-1);
157 sa.sun_path[sizeof(sa.sun_path)-1] = 0;
159 if (connect(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0) {
160 daemon_log(LOG_ERR, "connect(): %s", strerror(errno));
161 daemon_log(LOG_INFO, "Failed to connect to the daemon. This probably means that you");
162 daemon_log(LOG_INFO, "didn't start avahi-daemon before avahi-dnsconfd.");
175 static ssize_t loop_write(int fd, const void*data, size_t size) {
178 assert(fd >= 0 && data && size);
183 if ((r = write(fd, data, size)) < 0)
190 data = (const uint8_t*) data + r;
197 static char *concat_dns_servers(AvahiIfIndex interface) {
201 for (i = servers; i; i = i->servers_next)
202 if (i->interface == interface || interface <= 0) {
206 /* Filter out double entries */
207 for (j = servers; j != i; j = j->servers_next)
208 if (j->interface == interface || interface <= 0)
209 if (strcmp(i->address, j->address) == 0)
216 t = avahi_strdup(i->address);
218 t = avahi_strdup_printf("%s %s", r, i->address);
227 static void set_env(const char *name, const char *value) {
236 for (e = environ; *e; e++) {
237 /* Search for the variable */
238 if (strlen(*e) < l+1)
241 if (strncmp(*e, name, l) != 0 || (*e)[l] != '=')
244 /* We simply free the record, sicne we know that we created it previously */
246 *e = avahi_strdup_printf("%s=%s", name, value);
253 static void run_script(int new, AvahiIfIndex interface, AvahiProtocol protocol, const char *address) {
257 char name[IF_NAMESIZE];
259 assert(interface > 0);
261 if (!if_indextoname(interface, name))
264 p = concat_dns_servers(interface);
265 set_env(ENV_INTERFACE_DNS_SERVERS, p ? p : "");
268 p = concat_dns_servers(-1);
269 set_env(ENV_DNS_SERVERS, p ? p : "");
272 set_env(ENV_INTERFACE, name);
274 snprintf(ia, sizeof(ia), "%i", (int) interface);
275 snprintf(pa, sizeof(pa), "%i", (int) protocol);
277 if (daemon_exec("/", &ret, AVAHI_DNSCONF_SCRIPT, AVAHI_DNSCONF_SCRIPT, new ? "+" : "-", address, ia, pa, avahi_proto_to_string(protocol), NULL) < 0)
278 daemon_log(LOG_WARNING, "Failed to run script");
280 daemon_log(LOG_WARNING, "Script returned with non-zero exit code %i", ret);
283 static int new_line(const char *l) {
286 if (state == ACKWAIT) {
288 daemon_log(LOG_ERR, "Avahi command failed: %s", l);
292 daemon_log(LOG_INFO, "Successfully connected to Avahi daemon.");
295 AvahiIfIndex interface;
296 AvahiProtocol protocol;
297 int i_interface, i_protocol, port;
298 char a[AVAHI_ADDRESS_STR_MAX];
300 assert(state == BROWSING);
302 if (*l != '<' && *l != '>') {
303 daemon_log(LOG_ERR, "Avahi sent us an invalid browsing line: %s", l);
307 if (sscanf(l+1, "%i %i %39s %i", &i_interface, &i_protocol, a, &port) != 4) {
308 daemon_log(LOG_ERR, "Failed to parse browsing line: %s", l);
312 interface = (AvahiIfIndex) i_interface;
313 protocol = (AvahiProtocol) i_protocol;
317 daemon_log(LOG_WARNING, "DNS server with port address != 53 found, ignoring");
319 daemon_log(LOG_INFO, "New DNS Server %s (interface: %i.%s)", a, interface, avahi_proto_to_string(protocol));
320 new_server_info(interface, protocol, a);
321 run_script(1, interface, protocol, a);
327 if ((i = get_server_info(interface, protocol, a))) {
328 daemon_log(LOG_INFO, "DNS Server %s removed (interface: %i.%s)", a, interface, avahi_proto_to_string(protocol));
330 run_script(0, interface, protocol, a);
339 static int do_connect(void) {
342 if ((fd = open_socket()) < 0)
345 if (loop_write(fd, BROWSE_DNS_SERVERS, sizeof(BROWSE_DNS_SERVERS)-1) < 0) {
346 daemon_log(LOG_ERR, "write(): %s", strerror(errno));
360 static void free_dns_server_info_list(void) {
362 AvahiIfIndex interface = servers->interface;
363 AvahiProtocol protocol = servers->protocol;
364 char *address = avahi_strdup(servers->address);
365 server_info_free(servers);
367 run_script(0, interface, protocol, address);
372 static void help(FILE *f, const char *argv0) {
375 " -h --help Show this help\n"
376 " -D --daemonize Daemonize after startup\n"
377 " -k --kill Kill a running daemon\n"
378 " -r --refresh Request a running daemon to refresh DNS server data\n"
379 " -c --check Return 0 if a daemon is already running\n"
380 " -V --version Show version\n",
384 static int parse_command_line(int argc, char *argv[]) {
387 static const struct option long_options[] = {
388 { "help", no_argument, NULL, 'h' },
389 { "daemonize", no_argument, NULL, 'D' },
390 { "kill", no_argument, NULL, 'k' },
391 { "version", no_argument, NULL, 'V' },
392 { "refresh", no_argument, NULL, 'r' },
393 { "check", no_argument, NULL, 'c' },
397 while ((c = getopt_long(argc, argv, "hDkVrc", long_options, NULL)) >= 0) {
401 command = DAEMON_HELP;
407 command = DAEMON_KILL;
410 command = DAEMON_VERSION;
413 command = DAEMON_REFRESH;
416 command = DAEMON_CHECK;
424 fprintf(stderr, "Too many arguments\n");
431 static int run_daemon(void) {
432 int fd = -1, ret = -1;
436 AVAHI_LLIST_HEAD_INIT(DNSServerInfo, servers);
438 daemon_signal_init(SIGINT, SIGTERM, SIGCHLD, SIGHUP, 0);
440 /* Allocate some memory for our environment variables */
441 putenv(avahi_strdup(ENV_INTERFACE"="));
442 putenv(avahi_strdup(ENV_DNS_SERVERS"="));
443 putenv(avahi_strdup(ENV_INTERFACE_DNS_SERVERS"="));
445 if ((fd = do_connect()) < 0)
449 daemon_retval_send(0);
460 FD_SET(daemon_signal_fd(), &rfds);
463 if (select(fd+1, &rfds, NULL, NULL, NULL) < 0) {
467 daemon_log(LOG_ERR, "select(): %s", strerror(errno));
474 if (FD_ISSET(daemon_signal_fd(), &rfds)) {
478 if ((sig = daemon_signal_next()) <= 0) {
479 daemon_log(LOG_ERR, "daemon_signal_next() failed");
486 daemon_log(LOG_INFO, "Got %s, quitting.", sig == SIGINT ? "SIGINT" : "SIGTERM");
491 waitpid(-1, NULL, WNOHANG);
495 daemon_log(LOG_INFO, "Refreshing DNS Server list");
498 free_dns_server_info_list();
500 if ((fd = do_connect()) < 0)
506 } else if (FD_ISSET(fd, &rfds)) {
510 if ((r = read(fd, buf, sizeof(buf) - buflen - 1)) <= 0) {
511 daemon_log(LOG_ERR, "read(): %s", r < 0 ? strerror(errno) : "EOF");
516 assert(buflen <= sizeof(buf)-1);
518 while ((n = memchr(buf, '\n', buflen))) {
521 if (new_line(buf) < 0)
525 memmove(buf, n, buflen);
528 if (buflen >= sizeof(buf)-1) {
529 /* The incoming line is horribly long */
530 buf[sizeof(buf)-1] = 0;
532 if (new_line(buf) < 0)
542 free_dns_server_info_list();
547 daemon_signal_done();
549 if (ret != 0 && daemonize)
550 daemon_retval_send(1);
555 static const char* pid_file_proc(void) {
556 return AVAHI_RUNTIME_DIR"/avahi-dnsconfd.pid";
559 int main(int argc, char *argv[]) {
562 int wrote_pid_file = 0;
564 if ((argv0 = strrchr(argv[0], '/')))
569 daemon_pid_file_ident = daemon_log_ident = argv0;
570 daemon_pid_file_proc = pid_file_proc;
572 if (parse_command_line(argc, argv) < 0)
575 if (command == DAEMON_RUN) {
579 daemon_log(LOG_ERR, "This program is intended to be run as root.");
583 if ((pid = daemon_pid_file_is_running()) >= 0) {
584 daemon_log(LOG_ERR, "Daemon already running on PID %u", pid);
589 daemon_retval_init();
591 if ((pid = daemon_fork()) < 0)
597 if ((ret = daemon_retval_wait(20)) < 0) {
598 daemon_log(LOG_ERR, "Could not receive return value from daemon process.");
611 if (daemon_pid_file_create() < 0) {
612 daemon_log(LOG_ERR, "Failed to create PID file: %s", strerror(errno));
615 daemon_retval_send(1);
620 if (run_daemon() < 0)
624 } else if (command == DAEMON_HELP) {
628 } else if (command == DAEMON_VERSION) {
629 printf("%s "PACKAGE_VERSION"\n", argv0);
632 } else if (command == DAEMON_KILL) {
633 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
634 daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
639 } else if (command == DAEMON_REFRESH) {
640 if (daemon_pid_file_kill(SIGHUP) < 0) {
641 daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
646 } else if (command == DAEMON_CHECK)
647 r = (daemon_pid_file_is_running() >= 0) ? 0 : 1;
653 daemon_retval_done();
656 daemon_pid_file_remove();