]> git.decadent.org.uk Git - nfs-utils.git/blobdiff - utils/statd/statd.c
rpc.statd: Stop overloading sockfd in utils/statd/rmtcall.c
[nfs-utils.git] / utils / statd / statd.c
index 62009111edd5a0a594c823c3dc62972fa58f2193..321f7a945a81533da91ff8e1a8bc7b317c1e349f 100644 (file)
@@ -70,11 +70,12 @@ static struct option longopts[] =
        { "state-directory-path", 1, 0, 'P' },
        { "notify-mode", 0, 0, 'N' },
        { "ha-callout", 1, 0, 'H' },
+       { "no-notify", 0, 0, 'L' },
        { NULL, 0, 0, 0 }
 };
 
 extern void sm_prog_1 (struct svc_req *, register SVCXPRT *);
-extern int statd_get_socket(void);
+static void load_state_number(void);
 
 #ifdef SIMULATIONS
 extern void simulator (int, char **);
@@ -146,7 +147,7 @@ static void log_modes(void)
  * help the occasional admin. 
  */
 static void 
-usage()
+usage(void)
 {
        fprintf(stderr,"usage: %s [options]\n", name_p);
        fprintf(stderr,"      -h, -?, --help       Print this help screen.\n");
@@ -158,6 +159,7 @@ usage()
        fprintf(stderr,"      -n, --name           Specify a local hostname.\n");
        fprintf(stderr,"      -P                   State directory path.\n");
        fprintf(stderr,"      -N                   Run in notify only mode.\n");
+       fprintf(stderr,"      -L, --no-notify      Do not perform any notification.\n");
        fprintf(stderr,"      -H                   Specify a high-availability callout program.\n");
 }
 
@@ -228,12 +230,10 @@ static void run_sm_notify(int outport)
                av[ac++] = op;
        }
        if (run_mode & STATIC_HOSTNAME) {
-               av[ac++] = "-N";
+               av[ac++] = "-v";
                av[ac++] = MY_NAME;
        }
        av[ac] = NULL;
-       fprintf(stderr, "%s: -N deprecated, consider using /usr/sbin/sm-notify directly\n",
-               name_p);
        execv(av[0], av);
        fprintf(stderr, "%s: failed to run %s\n", name_p, av[0]);
        exit(2);
@@ -274,7 +274,7 @@ int main (int argc, char **argv)
        MY_NAME = NULL;
 
        /* Process command line switches */
-       while ((arg = getopt_long(argc, argv, "h?vVFNH:dn:p:o:P:", longopts, NULL)) != EOF) {
+       while ((arg = getopt_long(argc, argv, "h?vVFNH:dn:p:o:P:L", longopts, NULL)) != EOF) {
                switch (arg) {
                case 'V':       /* Version */
                case 'v':
@@ -286,6 +286,9 @@ int main (int argc, char **argv)
                case 'N':
                        run_mode |= MODE_NOTIFY_ONLY;
                        break;
+               case 'L': /* Listen only */
+                       run_mode |= MODE_NO_NOTIFY;
+                       break;
                case 'd':       /* No daemon only - log to stderr */
                        run_mode |= MODE_LOG_STDERR;
                        break;
@@ -363,8 +366,11 @@ int main (int argc, char **argv)
                exit(-1);
        }
 
-       if (run_mode & MODE_NOTIFY_ONLY)
+       if (run_mode & MODE_NOTIFY_ONLY) {
+               fprintf(stderr, "%s: -N deprecated, consider using /usr/sbin/sm-notify directly\n",
+                       name_p);
                run_sm_notify(out_port);
+       }
 
 
        if (!(run_mode & MODE_NODAEMON)) {
@@ -438,7 +444,7 @@ int main (int argc, char **argv)
 
        /* Child. */
 
-       log_init (name_p,version_p);
+       log_init (/*name_p,version_p*/);
 
        log_modes();
 
@@ -458,32 +464,43 @@ int main (int argc, char **argv)
        create_pidfile();
        atexit(truncate_pidfile);
 
-       switch (pid = fork()) {
-       case 0:
-               run_sm_notify(out_port);
-               break;
-       case -1:
-               break;
-       default:
-               waitpid(pid, NULL, 0);
-       }
+       if (! (run_mode & MODE_NO_NOTIFY))
+               switch (pid = fork()) {
+               case 0:
+                       run_sm_notify(out_port);
+                       break;
+               case -1:
+                       break;
+               default:
+                       waitpid(pid, NULL, 0);
+               }
 
-       drop_privs();
+       /* Make sure we have a privilege port for calling into the kernel */
+       if (statd_get_socket() < 0)
+               exit(1);
 
-       for (;;) {
-               pmap_unset (SM_PROG, SM_VERS);
+       /* If sm-notify didn't take all the state files, load
+        * state information into our notify-list so we can
+        * pass on any SM_NOTIFY that arrives
+        */
+       load_state();
+       load_state_number();
+       pmap_unset (SM_PROG, SM_VERS);
 
-               /* If we got this far, we have successfully started, so notify parent */
-               if (pipefds[1] > 0) {
-                       status = 0;
-                       write(pipefds[1], &status, 1);
-                       close(pipefds[1]);
-                       pipefds[1] = -1;
-               }
+       /* this registers both UDP and TCP services */
+       rpc_init("statd", SM_PROG, SM_VERS, sm_prog_1, port);
+
+       /* If we got this far, we have successfully started, so notify parent */
+       if (pipefds[1] > 0) {
+               status = 0;
+               write(pipefds[1], &status, 1);
+               close(pipefds[1]);
+               pipefds[1] = -1;
+       }
 
-               /* this registers both UDP and TCP services */
-               rpc_init("statd", SM_PROG, SM_VERS, sm_prog_1, port);
+       drop_privs();
 
+       for (;;) {
                /*
                 * Handle incoming requests:  SM_NOTIFY socket requests, as
                 * well as callbacks from lockd.
@@ -496,15 +513,37 @@ int main (int argc, char **argv)
                 * responding to SM_SIMU_CRASH is an important use cases to
                 * get perfect.
                 */
-               switch (pid = fork()) {
-               case 0:
-                       run_sm_notify(out_port);
-                       break;
-               case -1:
-                       break;
-               default:
-                       waitpid(pid, NULL, 0);
-               }
+               if (! (run_mode & MODE_NO_NOTIFY))
+                       switch (pid = fork()) {
+                       case 0:
+                               run_sm_notify(out_port);
+                               break;
+                       case -1:
+                               break;
+                       default:
+                               waitpid(pid, NULL, 0);
+                       }
+
        }
        return 0;
 }
+
+static void
+load_state_number(void)
+{
+       int fd;
+
+       if ((fd = open(SM_STAT_PATH, O_RDONLY)) == -1)
+               return;
+
+       read(fd, &MY_STATE, sizeof(MY_STATE));
+       close(fd);
+       fd = open("/proc/sys/fs/nfs/nsm_local_state",O_WRONLY);
+       if (fd >= 0) {
+               char buf[20];
+               snprintf(buf, sizeof(buf), "%d", MY_STATE);
+               write(fd, buf, strlen(buf));
+               close(fd);
+       }
+
+}