]> git.decadent.org.uk Git - nfs-utils.git/blobdiff - utils/statd/sm-notify.c
Add -Wstrict-prototypes to compiler args, and fix warnings caused.
[nfs-utils.git] / utils / statd / sm-notify.c
index a94876d1b160190c819d6b7ae82d9caf800a5805..bb67c378e94839254bbb49ec093bef71dda11317 100644 (file)
@@ -86,8 +86,9 @@ static int            addr_get_port(nsm_address *);
 static void            addr_set_port(nsm_address *, int);
 static struct addrinfo *host_lookup(int, const char *);
 void                   nsm_log(int fac, const char *fmt, ...);
-static int             record_pid();
+static int             record_pid(void);
 static void            drop_privs(void);
+static void set_kernel_nsm_state(int state);
 
 static struct nsm_host *       hosts = NULL;
 
@@ -166,6 +167,10 @@ usage:             fprintf(stderr,
        backup_hosts(_SM_DIR_PATH, _SM_BAK_PATH);
        get_hosts(_SM_BAK_PATH);
 
+       /* Get and update the NSM state. This will call sync() */
+       nsm_state = nsm_get_state(opt_update_state);
+       set_kernel_nsm_state(nsm_state);
+
        if (!opt_debug) {
                if (!opt_quiet)
                        printf("Backgrounding to notify hosts...\n");
@@ -184,9 +189,6 @@ usage:              fprintf(stderr,
                close(2);
        }
 
-       /* Get and update the NSM state. This will call sync() */
-       nsm_state = nsm_get_state(opt_update_state);
-
        notify();
 
        if (hosts) {
@@ -213,7 +215,9 @@ notify(void)
        nsm_address local_addr;
        time_t  failtime = 0;
        int     sock = -1;
+       int retry_cnt = 0;
 
+ retry:
        sock = socket(AF_INET, SOCK_DGRAM, 0);
        if (sock < 0) {
                perror("socket");
@@ -246,7 +250,16 @@ notify(void)
                        exit(1);
                }
        } else {
-               (void) bindresvport(sock, (struct sockaddr_in *) &local_addr);
+               struct servent *se;
+               struct sockaddr_in *sin = (struct sockaddr_in *)&local_addr;
+               (void) bindresvport(sock, sin);
+               /* try to avoid known ports */
+               se = getservbyport(sin->sin_port, "udp");
+               if (se && retry_cnt < 100) {
+                       retry_cnt++;
+                       close(sock);
+                       goto retry;
+               }
        }
 
        if (opt_max_retry)
@@ -717,7 +730,7 @@ nsm_log(int fac, const char *fmt, ...)
  * program exits.
  * If file already exists, fail.
  */
-static int record_pid()
+static int record_pid(void)
 {
        char pid[20];
        int fd;
@@ -758,3 +771,16 @@ static void drop_privs(void)
                exit(1);
        }
 }
+
+static void set_kernel_nsm_state(int state)
+{
+       int fd;
+
+       fd = open("/proc/sys/fs/nfs/nsm_local_state",O_WRONLY);
+       if (fd >= 0) {
+               char buf[20];
+               snprintf(buf, sizeof(buf), "%d", state);
+               write(fd, buf, strlen(buf));
+               close(fd);
+       }
+}