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/socket.h>
29 #include <netpacket/packet.h>
30 #include <net/ethernet.h>
37 #include <sys/types.h>
38 #include <arpa/inet.h>
39 #include <sys/ioctl.h>
47 #include <avahi-common/malloc.h>
48 #include <avahi-common/timeval.h>
50 #include <avahi-daemon/setproctitle.h>
52 #include <libdaemon/dfork.h>
53 #include <libdaemon/dsignal.h>
54 #include <libdaemon/dlog.h>
55 #include <libdaemon/dpid.h>
56 #include <libdaemon/dexec.h>
62 #error "avahi-autoipd is only available on Linux for now"
65 /* An implementation of RFC 3927 */
67 /* Constants from the RFC */
72 #define ANNOUNCE_WAIT 2
73 #define ANNOUNCE_NUM 2
74 #define ANNOUNCE_INTERVAL 2
75 #define MAX_CONFLICTS 10
76 #define RATE_LIMIT_INTERVAL 60
77 #define DEFEND_INTERVAL 10
79 #define IPV4LL_NETWORK 0xA9FE0000L
80 #define IPV4LL_NETMASK 0xFFFF0000L
81 #define IPV4LL_HOSTMASK 0x0000FFFFL
83 #define ETHER_ADDRLEN 6
84 #define ARP_PACKET_SIZE (8+4+4+2*ETHER_ADDRLEN)
86 typedef enum ArpOperation {
91 typedef struct ArpPacketInfo {
92 ArpOperation operation;
94 uint32_t sender_ip_address, target_ip_address;
95 uint8_t sender_hw_address[ETHER_ADDRLEN], target_hw_address[ETHER_ADDRLEN];
98 static State state = STATE_START;
99 static int n_iteration = 0;
100 static int n_conflict = 0;
102 static char *interface_name = NULL;
103 static char *pid_file_name = NULL;
104 static uint32_t start_address = 0;
105 static char *argv0 = NULL;
106 static int daemonize = 0;
107 static int wait_for_address = 0;
108 static int use_syslog = 0;
109 static int debug = 0;
110 static int modify_proc_title = 1;
111 static int force_bind = 0;
120 } command = DAEMON_RUN;
122 typedef enum CalloutEvent {
130 static const char * const callout_event_table[CALLOUT_MAX] = {
131 [CALLOUT_BIND] = "BIND",
132 [CALLOUT_CONFLICT] = "CONFLICT",
133 [CALLOUT_UNBIND] = "UNBIND",
134 [CALLOUT_STOP] = "STOP"
137 typedef struct CalloutEventInfo {
143 #define RANDOM_DEVICE "/dev/urandom"
145 #define DEBUG(x) do {\
151 static void init_rand_seed(void) {
155 /* Try to initialize seed from /dev/urandom, to make it a little
156 * less predictable, and to make sure that multiple machines
157 * booted at the same time choose different random seeds. */
158 if ((fd = open(RANDOM_DEVICE, O_RDONLY)) >= 0) {
159 read(fd, &seed, sizeof(seed));
163 /* If the initialization failed by some reason, we add the time to the seed */
164 seed ^= (unsigned) time(NULL);
169 static uint32_t pick_addr(uint32_t old_addr) {
173 unsigned r = (unsigned) rand();
175 /* Reduce to 16 bits */
177 r = (r >> 16) ^ (r & 0xFFFF);
179 addr = htonl(IPV4LL_NETWORK | (uint32_t) r);
181 } while (addr == old_addr);
186 static void* packet_new(const ArpPacketInfo *info, size_t *packet_len) {
191 assert(info->operation == ARP_REQUEST || info->operation == ARP_RESPONSE);
193 *packet_len = ARP_PACKET_SIZE;
194 r = avahi_new0(uint8_t, *packet_len);
196 r[1] = 1; /* HTYPE */
197 r[2] = 8; /* PTYPE */
198 r[4] = ETHER_ADDRLEN; /* HLEN */
200 r[7] = (uint8_t) info->operation;
202 memcpy(r+8, info->sender_hw_address, ETHER_ADDRLEN);
203 memcpy(r+14, &info->sender_ip_address, 4);
204 memcpy(r+18, info->target_hw_address, ETHER_ADDRLEN);
205 memcpy(r+24, &info->target_ip_address, 4);
210 static void *packet_new_probe(uint32_t ip_address, const uint8_t*hw_address, size_t *packet_len) {
213 memset(&info, 0, sizeof(info));
214 info.operation = ARP_REQUEST;
215 memcpy(info.sender_hw_address, hw_address, ETHER_ADDRLEN);
216 info.target_ip_address = ip_address;
218 return packet_new(&info, packet_len);
221 static void *packet_new_announcement(uint32_t ip_address, const uint8_t* hw_address, size_t *packet_len) {
224 memset(&info, 0, sizeof(info));
225 info.operation = ARP_REQUEST;
226 memcpy(info.sender_hw_address, hw_address, ETHER_ADDRLEN);
227 info.target_ip_address = ip_address;
228 info.sender_ip_address = ip_address;
230 return packet_new(&info, packet_len);
233 static int packet_parse(const void *data, size_t packet_len, ArpPacketInfo *info) {
234 const uint8_t *p = data;
238 if (packet_len < ARP_PACKET_SIZE)
241 /* Check HTYPE and PTYPE */
242 if (p[0] != 0 || p[1] != 1 || p[2] != 8 || p[3] != 0)
245 /* Check HLEN, PLEN, OPERATION */
246 if (p[4] != ETHER_ADDRLEN || p[5] != 4 || p[6] != 0 || (p[7] != 1 && p[7] != 2))
249 info->operation = p[7];
250 memcpy(info->sender_hw_address, p+8, ETHER_ADDRLEN);
251 memcpy(&info->sender_ip_address, p+14, 4);
252 memcpy(info->target_hw_address, p+18, ETHER_ADDRLEN);
253 memcpy(&info->target_ip_address, p+24, 4);
258 static void set_state(State st, int reset_counter, uint32_t address) {
259 static const char* const state_table[] = {
260 [STATE_START] = "START",
261 [STATE_WAITING_PROBE] = "WAITING_PROBE",
262 [STATE_PROBING] = "PROBING",
263 [STATE_WAITING_ANNOUNCE] = "WAITING_ANNOUNCE",
264 [STATE_ANNOUNCING] = "ANNOUNCING",
265 [STATE_RUNNING] = "RUNNING",
266 [STATE_SLEEPING] = "SLEEPING"
270 assert(st < STATE_MAX);
272 if (st == state && !reset_counter) {
274 DEBUG(daemon_log(LOG_DEBUG, "State iteration %s-%i", state_table[state], n_iteration));
276 DEBUG(daemon_log(LOG_DEBUG, "State transition %s-%i -> %s-0", state_table[state], n_iteration, state_table[st]));
281 if (modify_proc_title) {
282 if (state == STATE_SLEEPING)
283 avahi_set_proc_title(argv0, "%s(%s): sleeping", argv0, interface_name);
284 else if (state == STATE_ANNOUNCING)
285 avahi_set_proc_title(argv0, "%s(%s): announcing %s", argv0, interface_name, inet_ntop(AF_INET, &address, buf, sizeof(buf)));
286 else if (state == STATE_RUNNING)
287 avahi_set_proc_title(argv0, "%s(%s): bound %s", argv0, interface_name, inet_ntop(AF_INET, &address, buf, sizeof(buf)));
289 avahi_set_proc_title(argv0, "%s(%s): probing %s", argv0, interface_name, inet_ntop(AF_INET, &address, buf, sizeof(buf)));
293 static int interface_up(int iface) {
297 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
298 daemon_log(LOG_ERR, "socket() failed: %s", strerror(errno));
302 memset(&ifreq, 0, sizeof(ifreq));
303 if (!if_indextoname(iface, ifreq.ifr_name)) {
304 daemon_log(LOG_ERR, "if_indextoname() failed: %s", strerror(errno));
308 if (ioctl(fd, SIOCGIFFLAGS, &ifreq) < 0) {
309 daemon_log(LOG_ERR, "SIOCGIFFLAGS failed: %s", strerror(errno));
313 ifreq.ifr_flags |= IFF_UP;
315 if (ioctl(fd, SIOCSIFFLAGS, &ifreq) < 0) {
316 daemon_log(LOG_ERR, "SIOCSIFFLAGS failed: %s", strerror(errno));
331 static int open_socket(int iface, uint8_t *hw_address) {
333 struct sockaddr_ll sa;
336 if (interface_up(iface) < 0)
339 if ((fd = socket(PF_PACKET, SOCK_DGRAM, 0)) < 0) {
340 daemon_log(LOG_ERR, "socket() failed: %s", strerror(errno));
344 memset(&sa, 0, sizeof(sa));
345 sa.sll_family = AF_PACKET;
346 sa.sll_protocol = htons(ETH_P_ARP);
347 sa.sll_ifindex = iface;
349 if (bind(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0) {
350 daemon_log(LOG_ERR, "bind() failed: %s", strerror(errno));
355 if (getsockname(fd, (struct sockaddr*) &sa, &sa_len) < 0) {
356 daemon_log(LOG_ERR, "getsockname() failed: %s", strerror(errno));
360 if (sa.sll_halen != ETHER_ADDRLEN) {
361 daemon_log(LOG_ERR, "getsockname() returned invalid hardware address.");
365 memcpy(hw_address, sa.sll_addr, ETHER_ADDRLEN);
376 static int send_packet(int fd, int iface, void *packet, size_t packet_len) {
377 struct sockaddr_ll sa;
381 assert(packet_len > 0);
383 memset(&sa, 0, sizeof(sa));
384 sa.sll_family = AF_PACKET;
385 sa.sll_protocol = htons(ETH_P_ARP);
386 sa.sll_ifindex = iface;
387 sa.sll_halen = ETHER_ADDRLEN;
388 memset(sa.sll_addr, 0xFF, ETHER_ADDRLEN);
390 if (sendto(fd, packet, packet_len, 0, (struct sockaddr*) &sa, sizeof(sa)) < 0) {
391 daemon_log(LOG_ERR, "sendto() failed: %s", strerror(errno));
398 static int recv_packet(int fd, void **packet, size_t *packet_len) {
400 struct sockaddr_ll sa;
410 if (ioctl(fd, FIONREAD, &s) < 0) {
411 daemon_log(LOG_ERR, "FIONREAD failed: %s", strerror(errno));
418 *packet = avahi_new(uint8_t, s);
421 if ((r = recvfrom(fd, *packet, s, 0, (struct sockaddr*) &sa, &sa_len)) < 0) {
422 daemon_log(LOG_ERR, "recvfrom() failed: %s", strerror(errno));
426 *packet_len = (size_t) r;
439 int is_ll_address(uint32_t addr) {
440 return (ntohl(addr) & IPV4LL_NETMASK) == IPV4LL_NETWORK;
443 static struct timeval *elapse_time(struct timeval *tv, unsigned msec, unsigned jitter) {
446 gettimeofday(tv, NULL);
449 avahi_timeval_add(tv, (AvahiUsec) msec*1000);
452 avahi_timeval_add(tv, (AvahiUsec) (jitter*1000.0*rand()/(RAND_MAX+1.0)));
457 static FILE* fork_dispatcher(void) {
463 daemon_log(LOG_ERR, "pipe() failed: %s", strerror(errno));
467 if ((pid = fork()) < 0)
473 /* Please note that the signal pipe is not closed at this
474 * point, signals will thus be dispatched in the main
477 daemon_retval_done();
481 avahi_set_proc_title(argv0, "%s(%s): callout dispatcher", argv0, interface_name);
485 if (!(f = fdopen(fds[0], "r"))) {
486 daemon_log(LOG_ERR, "fdopen() failed: %s", strerror(errno));
487 goto dispatcher_fail;
491 CalloutEventInfo info;
492 char name[IFNAMSIZ], buf[64];
495 if (fread(&info, sizeof(info), 1, f) != 1) {
499 daemon_log(LOG_ERR, "fread() failed: %s", strerror(errno));
500 goto dispatcher_fail;
503 assert(info.event <= CALLOUT_MAX);
505 if (!if_indextoname(info.ifindex, name)) {
506 daemon_log(LOG_ERR, "if_indextoname() failed: %s", strerror(errno));
510 if (daemon_exec("/", &k,
511 AVAHI_IPCONF_SCRIPT, AVAHI_IPCONF_SCRIPT,
512 callout_event_table[info.event],
514 inet_ntop(AF_INET, &info.address, buf, sizeof(buf)), NULL) < 0) {
516 daemon_log(LOG_ERR, "Failed to run script: %s", strerror(errno));
521 daemon_log(LOG_WARNING, "Script execution failed with return value %i", k);
539 if (!(ret = fdopen(fds[1], "w"))) {
540 daemon_log(LOG_ERR, "fdopen() failed: %s", strerror(errno));
555 static int do_callout(FILE *f, CalloutEvent event, int iface, uint32_t addr) {
556 CalloutEventInfo info;
557 char buf[64], ifname[IFNAMSIZ];
559 daemon_log(LOG_INFO, "Callout %s, address %s on interface %s",
560 callout_event_table[event],
561 inet_ntop(AF_INET, &addr, buf, sizeof(buf)),
562 if_indextoname(iface, ifname));
565 info.ifindex = iface;
568 if (fwrite(&info, sizeof(info), 1, f) != 1 || fflush(f) != 0) {
569 daemon_log(LOG_ERR, "Failed to write callout event: %s", strerror(errno));
576 static int loop(int iface, uint32_t addr) {
584 int fd = -1, ret = -1;
585 struct timeval next_wakeup;
586 int next_wakeup_valid = 0;
588 void *in_packet = NULL;
589 size_t in_packet_len;
590 void *out_packet = NULL;
591 size_t out_packet_len;
592 uint8_t hw_address[ETHER_ADDRLEN];
593 struct pollfd pollfds[FD_MAX];
595 Event event = EVENT_NULL;
596 int retval_sent = !daemonize;
598 FILE *dispatcher = NULL;
600 daemon_signal_init(SIGINT, SIGTERM, SIGCHLD, SIGHUP,0);
602 if (!(dispatcher = fork_dispatcher()))
605 if ((fd = open_socket(iface, hw_address)) < 0)
608 if ((iface_fd = iface_init(iface)) < 0)
611 /* if (drop_privs() < 0) */
616 else if (iface_get_initial_state(&st) < 0)
619 if (addr && !is_ll_address(addr)) {
620 daemon_log(LOG_WARNING, "Requested address %s is not from IPv4LL range 169.254/16, ignoring.", inet_ntop(AF_INET, &addr, buf, sizeof(buf)));
628 for (i = 0; i < ETHER_ADDRLEN; i++)
629 a += hw_address[i]*i;
631 addr = htonl(IPV4LL_NETWORK | (uint32_t) a);
634 set_state(st, 1, addr);
636 daemon_log(LOG_INFO, "Starting with address %s", inet_ntop(AF_INET, &addr, buf, sizeof(buf)));
638 if (state == STATE_SLEEPING)
639 daemon_log(LOG_INFO, "Routable address already assigned, sleeping.");
641 if (!retval_sent && (!wait_for_address || state == STATE_SLEEPING)) {
642 daemon_retval_send(0);
646 memset(pollfds, 0, sizeof(pollfds));
647 pollfds[FD_ARP].fd = fd;
648 pollfds[FD_ARP].events = POLLIN;
649 pollfds[FD_IFACE].fd = iface_fd;
650 pollfds[FD_IFACE].events = POLLIN;
651 pollfds[FD_SIGNAL].fd = daemon_signal_fd();
652 pollfds[FD_SIGNAL].events = POLLIN;
658 if (state == STATE_START) {
660 /* First, wait a random time */
661 set_state(STATE_WAITING_PROBE, 1, addr);
663 elapse_time(&next_wakeup, 0, PROBE_WAIT*1000);
664 next_wakeup_valid = 1;
666 } else if ((state == STATE_WAITING_PROBE && event == EVENT_TIMEOUT) ||
667 (state == STATE_PROBING && event == EVENT_TIMEOUT && n_iteration < PROBE_NUM-2)) {
670 out_packet = packet_new_probe(addr, hw_address, &out_packet_len);
671 set_state(STATE_PROBING, 0, addr);
673 elapse_time(&next_wakeup, PROBE_MIN*1000, (PROBE_MAX-PROBE_MIN)*1000);
674 next_wakeup_valid = 1;
676 } else if (state == STATE_PROBING && event == EVENT_TIMEOUT && n_iteration >= PROBE_NUM-2) {
678 /* Send the last probe */
679 out_packet = packet_new_probe(addr, hw_address, &out_packet_len);
680 set_state(STATE_WAITING_ANNOUNCE, 1, addr);
682 elapse_time(&next_wakeup, ANNOUNCE_WAIT*1000, 0);
683 next_wakeup_valid = 1;
685 } else if ((state == STATE_WAITING_ANNOUNCE && event == EVENT_TIMEOUT) ||
686 (state == STATE_ANNOUNCING && event == EVENT_TIMEOUT && n_iteration < ANNOUNCE_NUM-1)) {
688 /* Send announcement packet */
689 out_packet = packet_new_announcement(addr, hw_address, &out_packet_len);
690 set_state(STATE_ANNOUNCING, 0, addr);
692 elapse_time(&next_wakeup, ANNOUNCE_INTERVAL*1000, 0);
693 next_wakeup_valid = 1;
695 if (n_iteration == 0) {
696 if (do_callout(dispatcher, CALLOUT_BIND, iface, addr) < 0)
702 daemon_retval_send(0);
707 } else if ((state == STATE_ANNOUNCING && event == EVENT_TIMEOUT && n_iteration >= ANNOUNCE_NUM-1)) {
709 daemon_log(LOG_INFO, "Successfully claimed IP address %s", inet_ntop(AF_INET, &addr, buf, sizeof(buf)));
710 set_state(STATE_RUNNING, 0, addr);
712 next_wakeup_valid = 0;
714 } else if (event == EVENT_PACKET) {
719 if (packet_parse(in_packet, in_packet_len, &info) < 0)
720 daemon_log(LOG_WARNING, "Failed to parse incoming ARP packet.");
724 if (info.sender_ip_address == addr) {
725 /* Normal conflict */
727 daemon_log(LOG_INFO, "Recieved conflicting normal ARP packet.");
728 } else if (state == STATE_WAITING_PROBE || state == STATE_PROBING || state == STATE_WAITING_ANNOUNCE) {
730 conflict = info.target_ip_address == addr && memcmp(hw_address, info.sender_hw_address, ETHER_ADDRLEN);
731 daemon_log(LOG_INFO, "Recieved conflicting probe ARP packet.");
736 if (state == STATE_RUNNING || state == STATE_ANNOUNCING)
737 if (do_callout(dispatcher, CALLOUT_CONFLICT, iface, addr) < 0)
740 /* Pick a new address */
741 addr = pick_addr(addr);
743 daemon_log(LOG_INFO, "Trying address %s", inet_ntop(AF_INET, &addr, buf, sizeof(buf)));
747 set_state(STATE_WAITING_PROBE, 1, addr);
749 if (n_conflict >= MAX_CONFLICTS) {
750 daemon_log(LOG_WARNING, "Got too many conflicts, rate limiting new probes.");
751 elapse_time(&next_wakeup, RATE_LIMIT_INTERVAL*1000, PROBE_WAIT*1000);
753 elapse_time(&next_wakeup, 0, PROBE_WAIT*1000);
755 next_wakeup_valid = 1;
757 DEBUG(daemon_log(LOG_DEBUG, "Ignoring irrelevant ARP packet."));
760 } else if (event == EVENT_ROUTABLE_ADDR_CONFIGURED) {
762 daemon_log(LOG_INFO, "A routable address has been configured.");
764 if (state == STATE_RUNNING || state == STATE_ANNOUNCING)
765 if (do_callout(dispatcher, CALLOUT_UNBIND, iface, addr) < 0)
769 daemon_retval_send(0);
773 set_state(STATE_SLEEPING, 1, addr);
774 next_wakeup_valid = 0;
776 } else if (event == EVENT_ROUTABLE_ADDR_UNCONFIGURED && state == STATE_SLEEPING && !force_bind) {
778 daemon_log(LOG_INFO, "No longer a routable address configured, restarting probe process.");
780 set_state(STATE_WAITING_PROBE, 1, addr);
782 elapse_time(&next_wakeup, 0, PROBE_WAIT*1000);
783 next_wakeup_valid = 1;
785 } else if (event == EVENT_REFRESH_REQUEST && state == STATE_RUNNING && !force_bind) {
787 /* The user requested a reannouncing of the address by a SIGHUP */
788 daemon_log(LOG_INFO, "Reannouncing address.");
790 /* Send announcement packet */
791 out_packet = packet_new_announcement(addr, hw_address, &out_packet_len);
792 set_state(STATE_ANNOUNCING, 1, addr);
794 elapse_time(&next_wakeup, ANNOUNCE_INTERVAL*1000, 0);
795 next_wakeup_valid = 1;
799 DEBUG(daemon_log(LOG_DEBUG, "sending..."));
801 if (send_packet(fd, iface, out_packet, out_packet_len) < 0)
804 avahi_free(out_packet);
809 avahi_free(in_packet);
816 if (next_wakeup_valid) {
817 usec = avahi_age(&next_wakeup);
818 timeout = usec < 0 ? (int) (-usec/1000) : 0;
821 DEBUG(daemon_log(LOG_DEBUG, "sleeping %ims", timeout));
823 while ((r = poll(pollfds, FD_MAX, timeout)) < 0 && errno == EINTR)
827 daemon_log(LOG_ERR, "poll() failed: %s", strerror(r));
830 event = EVENT_TIMEOUT;
831 next_wakeup_valid = 0;
835 if (pollfds[FD_ARP].revents) {
837 if (pollfds[FD_ARP].revents == POLLERR) {
838 /* The interface is probably down, let's recreate our socket */
842 if ((fd = open_socket(iface, hw_address)) < 0)
845 pollfds[FD_ARP].fd = fd;
849 assert(pollfds[FD_ARP].revents == POLLIN);
851 if (recv_packet(fd, &in_packet, &in_packet_len) < 0)
855 event = EVENT_PACKET;
859 if (event == EVENT_NULL &&
860 pollfds[FD_IFACE].revents) {
862 assert(pollfds[FD_IFACE].revents == POLLIN);
864 if (iface_process(&event) < 0)
868 if (event == EVENT_NULL &&
869 pollfds[FD_SIGNAL].revents) {
872 assert(pollfds[FD_SIGNAL].revents == POLLIN);
874 if ((sig = daemon_signal_next()) <= 0) {
875 daemon_log(LOG_ERR, "daemon_signal_next() failed");
882 daemon_log(LOG_INFO, "Got %s, quitting.", sig == SIGINT ? "SIGINT" : "SIGTERM");
887 waitpid(-1, NULL, WNOHANG);
891 event = EVENT_REFRESH_REQUEST;
903 if (state == STATE_RUNNING || state == STATE_ANNOUNCING)
904 do_callout(dispatcher, CALLOUT_STOP, iface, addr);
906 avahi_free(out_packet);
907 avahi_free(in_packet);
915 if (daemonize && !retval_sent)
916 daemon_retval_send(ret);
925 static void help(FILE *f, const char *a0) {
927 "%s [options] INTERFACE\n"
928 " -h --help Show this help\n"
929 " -D --daemonize Daemonize after startup\n"
930 " -s --syslog Write log messages to syslog(3) instead of STDERR\n"
931 " -k --kill Kill a running daemon\n"
932 " -r --refresh Request a running daemon to refresh it's IP address\n"
933 " -c --check Return 0 if a daemon is already running\n"
934 " -V --version Show version\n"
935 " -S --start=ADDRESS Start with this address from the IPv4LL range\n"
937 " -w --wait Wait until an address has been acquired before\n"
939 " --no-proc-title Don't modify process title\n"
940 " --force-bind Assign an IPv4LL address even if routable address\n"
941 " is already assigned\n"
942 " --debug Increase verbosity\n",
946 static int parse_command_line(int argc, char *argv[]) {
950 OPTION_NO_PROC_TITLE = 256,
955 static const struct option long_options[] = {
956 { "help", no_argument, NULL, 'h' },
957 { "daemonize", no_argument, NULL, 'D' },
958 { "syslog", no_argument, NULL, 's' },
959 { "kill", no_argument, NULL, 'k' },
960 { "refresh", no_argument, NULL, 'r' },
961 { "check", no_argument, NULL, 'c' },
962 { "version", no_argument, NULL, 'V' },
963 { "start", required_argument, NULL, 'S' },
964 { "wait", no_argument, NULL, 'w' },
965 { "no-proc-title", no_argument, NULL, OPTION_NO_PROC_TITLE },
966 { "force-bind", no_argument, NULL, OPTION_FORCE_BIND },
967 { "debug", no_argument, NULL, OPTION_DEBUG },
972 while ((c = getopt_long(argc, argv, "hDskrcVS:w", long_options, NULL)) >= 0) {
979 command = DAEMON_HELP;
985 command = DAEMON_KILL;
988 command = DAEMON_VERSION;
991 command = DAEMON_REFRESH;
994 command = DAEMON_CHECK;
998 if ((start_address = inet_addr(optarg)) == (uint32_t) -1) {
999 fprintf(stderr, "Failed to parse IP address '%s'.", optarg);
1004 wait_for_address = 1;
1007 case OPTION_NO_PROC_TITLE:
1008 modify_proc_title = 0;
1015 case OPTION_FORCE_BIND:
1020 fprintf(stderr, "Invalid command line argument: %c\n", c);
1025 if (command == DAEMON_RUN ||
1026 command == DAEMON_KILL ||
1027 command == DAEMON_REFRESH ||
1028 command == DAEMON_CHECK) {
1030 if (optind >= argc) {
1031 fprintf(stderr, "Missing interface name.\n");
1035 interface_name = avahi_strdup(argv[optind++]);
1038 if (optind != argc) {
1039 fprintf(stderr, "Too many arguments\n");
1046 static const char* pid_file_proc(void) {
1047 return pid_file_name;
1050 int main(int argc, char*argv[]) {
1052 int wrote_pid_file = 0;
1053 char *log_ident = NULL;
1055 avahi_init_proc_title(argc, argv);
1057 signal(SIGPIPE, SIG_IGN);
1059 if ((argv0 = strrchr(argv[0], '/')))
1064 argv0 = avahi_strdup(argv0);
1066 daemon_log_ident = argv0;
1068 if (parse_command_line(argc, argv) < 0)
1071 daemon_log_ident = log_ident = avahi_strdup_printf("%s(%s)", argv0, interface_name);
1072 daemon_pid_file_proc = pid_file_proc;
1073 pid_file_name = avahi_strdup_printf(AVAHI_RUNTIME_DIR"/avahi-autoipd.%s.pid", interface_name);
1075 if (command == DAEMON_RUN) {
1081 if ((ifindex = if_nametoindex(interface_name)) <= 0) {
1082 daemon_log(LOG_ERR, "Failed to get index for interface name '%s': %s", interface_name, strerror(errno));
1086 if (getuid() != 0) {
1087 daemon_log(LOG_ERR, "This program is intended to be run as root.");
1091 if ((pid = daemon_pid_file_is_running()) >= 0) {
1092 daemon_log(LOG_ERR, "Daemon already running on PID %u", pid);
1097 daemon_retval_init();
1099 if ((pid = daemon_fork()) < 0)
1101 else if (pid != 0) {
1105 if ((ret = daemon_retval_wait(20)) < 0) {
1106 daemon_log(LOG_ERR, "Could not receive return value from daemon process.");
1117 if (use_syslog || daemonize)
1118 daemon_log_use = DAEMON_LOG_SYSLOG;
1122 if (daemon_pid_file_create() < 0) {
1123 daemon_log(LOG_ERR, "Failed to create PID file: %s", strerror(errno));
1126 daemon_retval_send(1);
1131 avahi_set_proc_title(argv0, "%s(%s): starting up", argv0, interface_name);
1133 if (loop(ifindex, start_address) < 0)
1137 } else if (command == DAEMON_HELP) {
1138 help(stdout, argv0);
1141 } else if (command == DAEMON_VERSION) {
1142 printf("%s "PACKAGE_VERSION"\n", argv0);
1145 } else if (command == DAEMON_KILL) {
1146 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
1147 daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
1152 } else if (command == DAEMON_REFRESH) {
1153 if (daemon_pid_file_kill(SIGHUP) < 0) {
1154 daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
1159 } else if (command == DAEMON_CHECK)
1160 r = (daemon_pid_file_is_running() >= 0) ? 0 : 1;
1166 daemon_retval_done();
1169 daemon_pid_file_remove();
1171 avahi_free(log_ident);
1172 avahi_free(pid_file_name);
1174 avahi_free(interface_name);
1181 - chroot/drop privs/caps
1182 - store last used address