]> git.decadent.org.uk Git - nfs-utils.git/blobdiff - utils/statd/sm-notify.c
sm-notify: Use correct retransmit timeout when sending a fresh RPC
[nfs-utils.git] / utils / statd / sm-notify.c
index 3259a3e973d774de310f93065fcc1c867a026124..aa202d3a934a430ba62234327eadebaf33d07be5 100644 (file)
@@ -34,8 +34,9 @@
 #include "nsm.h"
 #include "nfsrpc.h"
 
-#ifndef HAVE_DECL_AI_ADDRCONFIG
-#define AI_ADDRCONFIG  0
+/* glibc before 2.3.4 */
+#ifndef AI_NUMERICSERV
+#define AI_NUMERICSERV 0
 #endif
 
 #define NSM_TIMEOUT    2
@@ -54,7 +55,7 @@ struct nsm_host {
        uint32_t                xid;
 };
 
-static char            nsm_hostname[256];
+static char            nsm_hostname[SM_MAXSTRLEN + 1];
 static int             nsm_state;
 static int             nsm_family = AF_INET;
 static int             opt_debug = 0;
@@ -78,7 +79,6 @@ smn_lookup(const char *name)
 {
        struct addrinfo *ai = NULL;
        struct addrinfo hint = {
-               .ai_flags       = AI_ADDRCONFIG,
                .ai_family      = (nsm_family == AF_INET ? AF_INET: AF_UNSPEC),
                .ai_protocol    = (int)IPPROTO_UDP,
        };
@@ -93,6 +93,101 @@ smn_lookup(const char *name)
        return ai;
 }
 
+#ifdef HAVE_GETNAMEINFO
+static char *
+smn_get_hostname(const struct sockaddr *sap, const socklen_t salen,
+               const char *name)
+{
+       char buf[NI_MAXHOST];
+       int error;
+
+       error = getnameinfo(sap, salen, buf, sizeof(buf), NULL, 0, NI_NAMEREQD);
+       if (error != 0) {
+               xlog(L_ERROR, "my_name '%s' is unusable: %s",
+                       name, gai_strerror(error));
+               return NULL;
+       }
+       return strdup(buf);
+}
+#else  /* !HAVE_GETNAMEINFO */
+static char *
+smn_get_hostname(const struct sockaddr *sap,
+               __attribute__ ((unused)) const socklen_t salen,
+               const char *name)
+{
+       const struct sockaddr_in *sin = (const struct sockaddr_in *)(char *)sap;
+       const struct in_addr *addr = &sin->sin_addr;
+       struct hostent *hp;
+
+       if (sap->sa_family != AF_INET) {
+               xlog(L_ERROR, "my_name '%s' is unusable: Bad address family",
+                       name);
+               return NULL;
+       }
+
+       hp = gethostbyaddr(addr, (socklen_t)sizeof(addr), AF_INET);
+       if (hp == NULL) {
+               xlog(L_ERROR, "my_name '%s' is unusable: %s",
+                       name, hstrerror(h_errno));
+               return NULL;
+       }
+       return strdup(hp->h_name);
+}
+#endif /* !HAVE_GETNAMEINFO */
+
+/*
+ * Presentation addresses are converted to their canonical hostnames.
+ * If the IP address does not map to a hostname, it is an error:
+ * we never send a presentation address as the argument of SM_NOTIFY.
+ *
+ * If "name" is not a presentation address, it is left alone.  This
+ * allows the administrator some flexibility if DNS isn't configured
+ * exactly how sm-notify prefers it.
+ *
+ * Returns NUL-terminated C string containing the result, or NULL
+ * if the canonical name doesn't exist or cannot be determined.
+ * The caller must free the result with free(3).
+ */
+__attribute_malloc__
+static char *
+smn_verify_my_name(const char *name)
+{
+       struct addrinfo *ai = NULL;
+       struct addrinfo hint = {
+#ifdef IPV6_SUPPORTED
+               .ai_family      = AF_UNSPEC,
+#else  /* !IPV6_SUPPORTED */
+               .ai_family      = AF_INET,
+#endif /* !IPV6_SUPPORTED */
+               .ai_flags       = AI_NUMERICHOST,
+       };
+       char *retval;
+       int error;
+
+       error = getaddrinfo(name, NULL, &hint, &ai);
+       switch (error) {
+       case 0:
+               /* @name was a presentation address */
+               retval = smn_get_hostname(ai->ai_addr, ai->ai_addrlen, name);
+               freeaddrinfo(ai);
+               if (retval == NULL)
+                       return NULL;
+               break;
+       case EAI_NONAME:
+               /* @name was not a presentation address */
+               retval = strdup(name);
+               break;
+       default:
+               xlog(L_ERROR, "my_name '%s' is unusable: %s",
+                       name, gai_strerror(error));
+               return NULL;
+       }
+
+       xlog(D_GENERAL, "Canonical name for my_name '%s': %s",
+                       name, retval);
+       return retval;
+}
+
 __attribute_malloc__
 static struct nsm_host *
 smn_alloc_host(const char *hostname, const char *mon_name,
@@ -157,7 +252,6 @@ smn_get_host(const char *hostname,
                return 0;
 
        insert_host(host);
-       xlog(D_GENERAL, "Added host %s to notify list", hostname);
        return 1;
 }
 
@@ -253,6 +347,7 @@ smn_bind_address(const char *srcaddr, const char *srcport)
        if (srcaddr == NULL)
                hint.ai_flags |= AI_PASSIVE;
 
+       /* Do not allow "node" and "service" parameters both to be NULL */
        if (srcport == NULL)
                error = getaddrinfo(srcaddr, "", &hint, &ai);
        else
@@ -394,12 +489,14 @@ usage:            fprintf(stderr,
                exit(1);
        }
 
-       xlog_syslog(1);
        if (opt_debug) {
+               xlog_syslog(0);
                xlog_stderr(1);
                xlog_config(D_ALL, 1);
-       } else
+       } else {
+               xlog_syslog(1);
                xlog_stderr(0);
+       }
 
        xlog_open(progname);
        xlog(L_NOTICE, "Version " VERSION " starting");
@@ -412,12 +509,15 @@ usage:            fprintf(stderr,
                }
        }
 
-       if (opt_srcaddr) {
-               strncpy(nsm_hostname, opt_srcaddr, sizeof(nsm_hostname)-1);
-       } else
-       if (gethostname(nsm_hostname, sizeof(nsm_hostname)) < 0) {
-               xlog(L_ERROR, "Failed to obtain name of local host: %m");
-               exit(1);
+       if (opt_srcaddr != NULL) {
+               char *name;
+
+               name = smn_verify_my_name(opt_srcaddr);
+               if (name == NULL)
+                       exit(1);
+
+               strncpy(nsm_hostname, name, sizeof(nsm_hostname));
+               free(name);
        }
 
        (void)nsm_retire_monitored_hosts();
@@ -535,6 +635,8 @@ notify(const int sock)
 static int
 notify_host(int sock, struct nsm_host *host)
 {
+       const char *my_name = (opt_srcaddr != NULL ?
+                                       nsm_hostname : host->my_name);
        struct sockaddr *sap;
        socklen_t salen;
 
@@ -580,11 +682,29 @@ notify_host(int sock, struct nsm_host *host)
                host->xid = nsm_xmit_rpcbind(sock, sap, SM_PROG, SM_VERS);
        else
                host->xid = nsm_xmit_notify(sock, sap, salen,
-                               SM_PROG, nsm_hostname, nsm_state);
-       
+                                       SM_PROG, my_name, nsm_state);
+
        return 0;
 }
 
+static void
+smn_defer(struct nsm_host *host)
+{
+       host->xid = 0;
+       host->send_next = time(NULL) + NSM_MAX_TIMEOUT;
+       host->timeout = NSM_MAX_TIMEOUT;
+       insert_host(host);
+}
+
+static void
+smn_schedule(struct nsm_host *host)
+{
+       host->xid = 0;
+       host->send_next = time(NULL);
+       host->timeout = NSM_TIMEOUT;
+       insert_host(host);
+}
+
 /*
  * Extract the returned port number and set up the SM_NOTIFY call.
  */
@@ -593,33 +713,37 @@ recv_rpcbind_reply(struct sockaddr *sap, struct nsm_host *host, XDR *xdr)
 {
        uint16_t port = nsm_recv_rpcbind(sap->sa_family, xdr);
 
-       host->send_next = time(NULL);
-       host->xid = 0;
-
        if (port == 0) {
                /* No binding for statd... */
                xlog(D_GENERAL, "No statd on host %s", host->name);
-               host->timeout = NSM_MAX_TIMEOUT;
-               host->send_next += NSM_MAX_TIMEOUT;
+               smn_defer(host);
        } else {
+               xlog(D_GENERAL, "Processing rpcbind reply for %s (port %u)",
+                       host->name, port);
                nfs_set_port(sap, port);
-               if (host->timeout >= NSM_MAX_TIMEOUT / 4)
-                       host->timeout = NSM_MAX_TIMEOUT / 4;
+               smn_schedule(host);
        }
-
-       insert_host(host);
 }
 
 /*
- * Successful NOTIFY call. Server returns void, so nothing
- * we need to do here.
+ * Successful NOTIFY call. Server returns void.
+ *
+ * Try sending another SM_NOTIFY with an unqualified "my_name"
+ * argument.  Reuse the port number.  If "my_name" is already
+ * unqualified, we're done.
  */
 static void
 recv_notify_reply(struct nsm_host *host)
 {
-       xlog(D_GENERAL, "Host %s notified successfully", host->name);
+       char *dot = strchr(host->my_name, '.');
 
-       smn_forget_host(host);
+       if (dot != NULL) {
+               *dot = '\0';
+               smn_schedule(host);
+       } else {
+               xlog(D_GENERAL, "Host %s notified successfully", host->name);
+               smn_forget_host(host);
+       }
 }
 
 /*
@@ -664,7 +788,7 @@ out:
 }
 
 /*
- * Insert host into sorted list
+ * Insert host into notification list, sorted by next send time
  */
 static void
 insert_host(struct nsm_host *host)
@@ -689,6 +813,7 @@ insert_host(struct nsm_host *host)
 
        host->next = *where;
        *where = host;
+       xlog(D_GENERAL, "Added host %s to notify list", host->name);
 }
 
 /*