2 * Send NSM notify calls to all hosts listed in /var/lib/sm
4 * Copyright (C) 2004-2006 Olaf Kirch <okir@suse.de>
12 #include <sys/types.h>
13 #include <sys/socket.h>
16 #include <sys/param.h>
17 #include <sys/syslog.h>
18 #include <arpa/inet.h>
37 #ifndef HAVE_DECL_AI_ADDRCONFIG
38 #define AI_ADDRCONFIG 0
42 #define NSM_MAX_TIMEOUT 120 /* don't make this too big */
45 struct nsm_host * next;
57 static char nsm_hostname[SM_MAXSTRLEN + 1];
59 static int nsm_family = AF_INET;
60 static int opt_debug = 0;
61 static _Bool opt_update_state = true;
62 static unsigned int opt_max_retry = 15 * 60;
63 static char * opt_srcaddr = NULL;
64 static char * opt_srcport = NULL;
66 static void notify(const int sock);
67 static int notify_host(int, struct nsm_host *);
68 static void recv_reply(int);
69 static void insert_host(struct nsm_host *);
70 static struct nsm_host *find_host(uint32_t);
71 static int record_pid(void);
73 static struct nsm_host * hosts = NULL;
76 static struct addrinfo *
77 smn_lookup(const char *name)
79 struct addrinfo *ai = NULL;
80 struct addrinfo hint = {
81 .ai_flags = AI_ADDRCONFIG,
82 .ai_family = (nsm_family == AF_INET ? AF_INET: AF_UNSPEC),
83 .ai_protocol = (int)IPPROTO_UDP,
87 error = getaddrinfo(name, NULL, &hint, &ai);
89 xlog(D_GENERAL, "getaddrinfo(3): %s", gai_strerror(error));
97 static struct nsm_host *
98 smn_alloc_host(const char *hostname, const char *mon_name,
99 const char *my_name, const time_t timestamp)
101 struct nsm_host *host;
103 host = calloc(1, sizeof(*host));
107 host->name = strdup(hostname);
108 host->mon_name = strdup(mon_name);
109 host->my_name = strdup(my_name);
110 if (host->name == NULL ||
111 host->mon_name == NULL ||
112 host->my_name == NULL) {
114 free(host->mon_name);
120 host->last_used = timestamp;
121 host->timeout = NSM_TIMEOUT;
122 host->retries = 100; /* force address retry */
127 xlog_warn("Unable to allocate memory");
131 static void smn_forget_host(struct nsm_host *host)
133 xlog(D_CALL, "Removing %s (%s, %s) from notify list",
134 host->name, host->mon_name, host->my_name);
136 nsm_delete_notified_host(host->name, host->mon_name, host->my_name);
139 free(host->mon_name);
142 freeaddrinfo(host->ai);
148 smn_get_host(const char *hostname,
149 __attribute__ ((unused)) const struct sockaddr *sap,
150 const struct mon *m, const time_t timestamp)
152 struct nsm_host *host;
154 host = smn_alloc_host(hostname,
155 m->mon_id.mon_name, m->mon_id.my_id.my_name, timestamp);
160 xlog(D_GENERAL, "Added host %s to notify list", hostname);
164 #ifdef IPV6_SUPPORTED
165 static int smn_socket(void)
170 * Use an AF_INET socket if IPv6 is disabled on the
173 sock = socket(AF_INET6, SOCK_DGRAM, 0);
175 if (errno != EAFNOSUPPORT) {
176 xlog(L_ERROR, "Failed to create RPC socket: %m");
179 sock = socket(AF_INET, SOCK_DGRAM, 0);
181 xlog(L_ERROR, "Failed to create RPC socket: %m");
185 nsm_family = AF_INET6;
187 if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) {
188 xlog(L_ERROR, "fcntl(3) on RPC socket failed: %m");
193 * TI-RPC over IPv6 (udp6/tcp6) does not handle IPv4. However,
194 * since sm-notify open-codes all of its RPC support, it can
195 * use a single socket and let the local network stack provide
196 * the correct mapping between address families automatically.
197 * This is the same thing that is done in the kernel.
199 if (nsm_family == AF_INET6) {
201 socklen_t zerolen = (socklen_t)sizeof(zero);
203 if (setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
204 (char *)&zero, zerolen) == -1) {
205 xlog(L_ERROR, "setsockopt(3) on RPC socket failed: %m");
216 #else /* !IPV6_SUPPORTED */
217 static int smn_socket(void)
221 sock = socket(AF_INET, SOCK_DGRAM, 0);
223 xlog(L_ERROR, "Failed to create RPC socket: %m");
227 if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) {
228 xlog(L_ERROR, "fcntl(3) on RPC socket failed: %m");
235 #endif /* !IPV6_SUPPORTED */
238 * If admin specified a source address or srcport, then convert those
239 * to a sockaddr and return it. Otherwise, return an ANYADDR address.
242 static struct addrinfo *
243 smn_bind_address(const char *srcaddr, const char *srcport)
245 struct addrinfo *ai = NULL;
246 struct addrinfo hint = {
247 .ai_flags = AI_NUMERICSERV,
248 .ai_family = nsm_family,
249 .ai_protocol = (int)IPPROTO_UDP,
254 hint.ai_flags |= AI_PASSIVE;
257 error = getaddrinfo(srcaddr, "", &hint, &ai);
259 error = getaddrinfo(srcaddr, srcport, &hint, &ai);
262 "Invalid bind address or port for RPC socket: %s",
263 gai_strerror(error));
272 smn_bindresvport(int sock, struct sockaddr *sap)
274 return bindresvport_sa(sock, sap);
277 #else /* !HAVE_LIBTIRPC */
279 smn_bindresvport(int sock, struct sockaddr *sap)
281 if (sap->sa_family != AF_INET) {
282 errno = EAFNOSUPPORT;
286 return bindresvport(sock, (struct sockaddr_in *)(char *)sap);
288 #endif /* !HAVE_LIBTIRPC */
291 * Prepare a socket for sending RPC requests
293 * Returns a bound datagram socket file descriptor, or -1 if
297 smn_create_socket(const char *srcaddr, const char *srcport)
299 int sock, retry_cnt = 0;
307 ai = smn_bind_address(srcaddr, srcport);
313 /* Use source port if provided on the command line,
314 * otherwise use bindresvport */
316 if (bind(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
317 xlog(L_ERROR, "Failed to bind RPC socket: %m");
325 if (smn_bindresvport(sock, ai->ai_addr) == -1) {
327 "bindresvport on RPC socket failed: %m");
333 /* try to avoid known ports */
334 se = getservbyport((int)nfs_get_port(ai->ai_addr), "udp");
335 if (se != NULL && retry_cnt < 100) {
348 main(int argc, char **argv)
350 int c, sock, force = 0;
353 progname = strrchr(argv[0], '/');
354 if (progname != NULL)
359 while ((c = getopt(argc, argv, "dm:np:v:P:f")) != -1) {
368 opt_max_retry = atoi(optarg) * 60;
371 opt_update_state = false;
374 opt_srcport = optarg;
377 opt_srcaddr = optarg;
380 if (!nsm_setup_pathnames(argv[0], optarg))
390 usage: fprintf(stderr,
391 "Usage: %s -notify [-dfq] [-m max-retry-minutes] [-p srcport]\n"
392 " [-P /path/to/state/directory] [-v my_host_name]\n",
400 xlog_config(D_ALL, 1);
405 xlog(L_NOTICE, "Version " VERSION " starting");
407 if (nsm_is_default_parentdir()) {
408 if (record_pid() == 0 && force == 0 && opt_update_state) {
409 /* already run, don't try again */
410 xlog(L_NOTICE, "Already notifying clients; Exiting!");
415 if (opt_srcaddr != NULL) {
416 struct addrinfo *ai = NULL;
417 struct addrinfo hint = {
418 .ai_family = AF_UNSPEC,
419 .ai_flags = AI_NUMERICHOST,
422 if (getaddrinfo(opt_srcaddr, NULL, &hint, &ai))
423 /* not a presentation address - use it */
424 strncpy(nsm_hostname, opt_srcaddr, sizeof(nsm_hostname));
426 /* was a presentation address - look it up in
427 * /etc/hosts, so it can be used for my_name */
431 hint.ai_flags = AI_CANONNAME;
432 error = getaddrinfo(opt_srcaddr, NULL, &hint, &ai);
434 xlog(L_ERROR, "Bind address %s is unusable: %s",
435 opt_srcaddr, gai_strerror(error));
438 strncpy(nsm_hostname, ai->ai_canonname,
439 sizeof(nsm_hostname));
444 (void)nsm_retire_monitored_hosts();
445 if (nsm_load_notify_list(smn_get_host) == 0) {
446 xlog(D_GENERAL, "No hosts to notify; exiting");
450 nsm_state = nsm_get_state(opt_update_state);
453 nsm_update_kernel_state(nsm_state);
456 xlog(L_NOTICE, "Backgrounding to notify hosts...\n");
458 if (daemon(0, 0) < 0) {
459 xlog(L_ERROR, "unable to background: %m");
468 sock = smn_create_socket(opt_srcaddr, opt_srcport);
472 if (!nsm_drop_privileges(-1))
480 while ((hp = hosts) != 0) {
482 xlog(L_NOTICE, "Unable to notify %s, giving up",
495 notify(const int sock)
500 failtime = time(NULL) + opt_max_retry;
504 time_t now = time(NULL);
505 unsigned int sent = 0;
509 if (failtime && now >= failtime)
512 while (hosts && ((wait = hosts->send_next - now) <= 0)) {
513 /* Never send more than 10 packets at once */
517 /* Remove queue head */
521 if (notify_host(sock, hp))
524 /* Set the timeout for this call, using an
525 exponential timeout strategy */
527 if ((hp->timeout <<= 1) > NSM_MAX_TIMEOUT)
528 hp->timeout = NSM_MAX_TIMEOUT;
529 hp->send_next = now + wait;
537 xlog(D_GENERAL, "Host %s due in %ld seconds",
546 if (poll(&pfd, 1, wait) != 1)
554 * Send notification to a single host
557 notify_host(int sock, struct nsm_host *host)
559 const char *my_name = (opt_srcaddr != NULL ?
560 nsm_hostname : host->my_name);
561 struct sockaddr *sap;
564 if (host->ai == NULL) {
565 host->ai = smn_lookup(host->name);
566 if (host->ai == NULL) {
567 xlog_warn("DNS resolution of %s failed; "
568 "retrying later", host->name);
573 /* If we retransmitted 4 times, reset the port to force
574 * a new portmap lookup (in case statd was restarted).
575 * We also rotate through multiple IP addresses at this
578 if (host->retries >= 4) {
579 /* don't rotate if there is only one addrinfo */
580 if (host->ai->ai_next != NULL) {
581 struct addrinfo *first = host->ai;
582 struct addrinfo **next = &host->ai;
584 /* remove the first entry from the list */
585 host->ai = first->ai_next;
586 first->ai_next = NULL;
587 /* find the end of the list */
588 next = &first->ai_next;
590 next = & (*next)->ai_next;
591 /* put first entry at end */
595 nfs_set_port(host->ai->ai_addr, 0);
599 sap = host->ai->ai_addr;
600 salen = host->ai->ai_addrlen;
602 if (nfs_get_port(sap) == 0)
603 host->xid = nsm_xmit_rpcbind(sock, sap, SM_PROG, SM_VERS);
605 host->xid = nsm_xmit_notify(sock, sap, salen,
606 SM_PROG, my_name, nsm_state);
612 * Extract the returned port number and set up the SM_NOTIFY call.
615 recv_rpcbind_reply(struct sockaddr *sap, struct nsm_host *host, XDR *xdr)
617 uint16_t port = nsm_recv_rpcbind(sap->sa_family, xdr);
619 host->send_next = time(NULL);
623 /* No binding for statd... */
624 xlog(D_GENERAL, "No statd on host %s", host->name);
625 host->timeout = NSM_MAX_TIMEOUT;
626 host->send_next += NSM_MAX_TIMEOUT;
628 nfs_set_port(sap, port);
629 if (host->timeout >= NSM_MAX_TIMEOUT / 4)
630 host->timeout = NSM_MAX_TIMEOUT / 4;
637 * Successful NOTIFY call. Server returns void.
639 * Try sending another SM_NOTIFY with an unqualified "my_name"
640 * argument. Reuse the port number. If "my_name" is already
641 * unqualified, we're done.
644 recv_notify_reply(struct nsm_host *host)
646 char *dot = strchr(host->my_name, '.');
650 host->send_next = time(NULL);
652 if (host->timeout >= NSM_MAX_TIMEOUT / 4)
653 host->timeout = NSM_MAX_TIMEOUT / 4;
656 xlog(D_GENERAL, "Host %s notified successfully", host->name);
657 smn_forget_host(host);
662 * Receive reply from remote host
668 struct sockaddr *sap;
669 char msgbuf[NSM_MAXMSGSIZE];
674 memset(msgbuf, 0 , sizeof(msgbuf));
675 msglen = recv(sock, msgbuf, sizeof(msgbuf), 0);
679 xlog(D_GENERAL, "Received packet...");
681 memset(&xdr, 0, sizeof(xdr));
682 xdrmem_create(&xdr, msgbuf, (unsigned int)msglen, XDR_DECODE);
683 xid = nsm_parse_reply(&xdr);
687 /* Before we look at the data, find the host struct for
689 if ((hp = find_host(xid)) == NULL)
692 sap = hp->ai->ai_addr;
693 if (nfs_get_port(sap) == 0)
694 recv_rpcbind_reply(sap, hp, &xdr);
696 recv_notify_reply(hp);
703 * Insert host into sorted list
706 insert_host(struct nsm_host *host)
708 struct nsm_host **where, *p;
711 while ((p = *where) != 0) {
712 /* Sort in ascending order of timeout */
713 if (host->send_next < p->send_next)
715 /* If we have the same timeout, put the
716 * most recently used host first.
717 * This makes sure that "recent" hosts
718 * get notified first.
720 if (host->send_next == p->send_next
721 && host->last_used > p->last_used)
731 * Find host given the XID
733 static struct nsm_host *
734 find_host(uint32_t xid)
736 struct nsm_host **where, *p;
739 while ((p = *where) != 0) {
750 * Record pid in /var/run/sm-notify.pid
751 * This file should remain until a reboot, even if the
753 * If file already exists, fail.
755 static int record_pid(void)
761 (void)snprintf(pid, sizeof(pid), "%d\n", (int)getpid());
762 fd = open("/var/run/sm-notify.pid", O_CREAT|O_EXCL|O_WRONLY, 0600);
766 len = write(fd, pid, strlen(pid));
767 if ((len < 0) || ((size_t)len != strlen(pid))) {
768 xlog_warn("Writing to pid file failed: errno %d (%m)",