]> git.decadent.org.uk Git - nfs-utils.git/blobdiff - utils/nfsd/nfssvc.c
nfs-utils: move check for active knfsd to helper function
[nfs-utils.git] / utils / nfsd / nfssvc.c
index 6c5289a58979836fa5a73e1430ad7e6404229c1d..7ecaea9e76cc26eee99037a9897794ceafbe4017 100644 (file)
 #include <unistd.h>
 #include <fcntl.h>
 #include <errno.h>
-#include <syslog.h>
-
 
 #include "nfslib.h"
+#include "xlog.h"
 
 #define NFSD_PORTS_FILE     "/proc/fs/nfsd/portlist"
 #define NFSD_VERS_FILE    "/proc/fs/nfsd/versions"
 #define NFSD_THREAD_FILE  "/proc/fs/nfsd/threads"
 
+/*
+ * declaring a common static scratch buffer here keeps us from having to
+ * continually thrash the stack. The value of 128 bytes here is really just a
+ * SWAG and can be increased if necessary. It ought to be enough for the
+ * routines below however.
+ */
+char buf[128];
+
+/*
+ * Are there already sockets configured? If not, then it is safe to try to
+ * open some and pass them through.
+ *
+ * Note: If the user explicitly asked for 'udp', then we should probably check
+ * if that is open, and should open it if not. However we don't yet. All
+ * sockets have to be opened when the first daemon is started.
+ */
+int
+nfssvc_inuse(void)
+{
+       int fd, n;
+
+       fd = open(NFSD_PORTS_FILE, O_RDONLY);
+
+       /* problem opening file, assume that nothing is configured */
+       if (fd < 0)
+               return 0;
+
+       n = read(fd, buf, sizeof(buf));
+       close(fd);
+
+       xlog(D_GENERAL, "knfsd is currently %s", (n > 0) ? "up" : "down");
+
+       return (n > 0);
+}
+
 static void
 nfssvc_setfds(int port, unsigned int ctlbits, char *haddr)
 {
-       int fd, n, on=1;
-       char buf[BUFSIZ];
+       int fd, on=1;
        int udpfd = -1, tcpfd = -1;
        struct sockaddr_in sin;
 
-       fd = open(NFSD_PORTS_FILE, O_RDONLY);
-       if (fd < 0)
+       if (nfssvc_inuse())
                return;
-       n = read(fd, buf, BUFSIZ);
-       close(fd);
-       if (n != 0)
-               return;
-       /* there are no ports currently open, so it is safe to
-        * try to open some and pass them through.
-        * Note: If the user explicitly asked for 'udp', then
-        * we should probably check if that is open, and should
-        * open it if not.  However we don't yet.  All sockets
-        * have to be opened when the first daemon is started.
-        */
+
        fd = open(NFSD_PORTS_FILE, O_WRONLY);
        if (fd < 0)
                return;
@@ -57,13 +79,13 @@ nfssvc_setfds(int port, unsigned int ctlbits, char *haddr)
        if (NFSCTL_UDPISSET(ctlbits)) {
                udpfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
                if (udpfd < 0) {
-                       syslog(LOG_ERR, "nfssvc: unable to create UPD socket: "
-                               "errno %d (%s)\n", errno, strerror(errno));
+                       xlog(L_ERROR, "unable to create UDP socket: "
+                               "errno %d (%m)", errno);
                        exit(1);
                }
                if (bind(udpfd, (struct  sockaddr  *)&sin, sizeof(sin)) < 0){
-                       syslog(LOG_ERR, "nfssvc: unable to bind UPD socket: "
-                               "errno %d (%s)\n", errno, strerror(errno));
+                       xlog(L_ERROR, "unable to bind UDP socket: "
+                               "errno %d (%m)", errno);
                        exit(1);
                }
        }
@@ -71,32 +93,32 @@ nfssvc_setfds(int port, unsigned int ctlbits, char *haddr)
        if (NFSCTL_TCPISSET(ctlbits)) {
                tcpfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
                if (tcpfd < 0) {
-                       syslog(LOG_ERR, "nfssvc: unable to createt tcp socket: "
-                               "errno %d (%s)\n", errno, strerror(errno));
+                       xlog(L_ERROR, "unable to create TCP socket: "
+                               "errno %d (%m)", errno);
                        exit(1);
                }
                if (setsockopt(tcpfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
-                       syslog(LOG_ERR, "nfssvc: unable to set SO_REUSEADDR: "
-                               "errno %d (%s)\n", errno, strerror(errno));
+                       xlog(L_ERROR, "unable to set SO_REUSEADDR: "
+                               "errno %d (%m)", errno);
                        exit(1);
                }
                if (bind(tcpfd, (struct  sockaddr  *)&sin, sizeof(sin)) < 0){
-                       syslog(LOG_ERR, "nfssvc: unable to bind TCP socket: "
-                               "errno %d (%s)\n", errno, strerror(errno));
+                       xlog(L_ERROR, "unable to bind TCP socket: "
+                               "errno %d (%m)", errno);
                        exit(1);
                }
                if (listen(tcpfd, 64) < 0){
-                       syslog(LOG_ERR, "nfssvc: unable to create listening socket: "
-                               "errno %d (%s)\n", errno, strerror(errno));
+                       xlog(L_ERROR, "unable to create listening socket: "
+                               "errno %d (%m)", errno);
                        exit(1);
                }
        }
        if (udpfd >= 0) {
-               snprintf(buf, BUFSIZ,"%d\n", udpfd); 
+               snprintf(buf, sizeof(buf), "%d\n", udpfd); 
                if (write(fd, buf, strlen(buf)) != strlen(buf)) {
-                       syslog(LOG_ERR, 
-                              "nfssvc: writing fds to kernel failed: errno %d (%s)", 
-                              errno, strerror(errno));
+                       xlog(L_ERROR, 
+                              "writing fds to kernel failed: errno %d (%m)", 
+                              errno);
                }
                close(fd);
                fd = -1;
@@ -104,11 +126,11 @@ nfssvc_setfds(int port, unsigned int ctlbits, char *haddr)
        if (tcpfd >= 0) {
                if (fd < 0)
                        fd = open(NFSD_PORTS_FILE, O_WRONLY);
-               snprintf(buf, BUFSIZ,"%d\n", tcpfd); 
+               snprintf(buf, sizeof(buf), "%d\n", tcpfd); 
                if (write(fd, buf, strlen(buf)) != strlen(buf)) {
-                       syslog(LOG_ERR, 
-                              "nfssvc: writing fds to kernel failed: errno %d (%s)", 
-                              errno, strerror(errno));
+                       xlog(L_ERROR, 
+                              "writing fds to kernel failed: errno %d (%m)", 
+                              errno);
                }
        }
        close(fd);
@@ -119,7 +141,7 @@ static void
 nfssvc_versbits(unsigned int ctlbits, int minorvers4)
 {
        int fd, n, off;
-       char buf[BUFSIZ], *ptr;
+       char *ptr;
 
        ptr = buf;
        off = 0;
@@ -129,20 +151,20 @@ nfssvc_versbits(unsigned int ctlbits, int minorvers4)
 
        for (n = NFSD_MINVERS; n <= NFSD_MAXVERS; n++) {
                if (NFSCTL_VERISSET(ctlbits, n))
-                   off += snprintf(ptr+off, BUFSIZ - off, "+%d ", n);
+                   off += snprintf(ptr+off, sizeof(buf) - off, "+%d ", n);
                else
-                   off += snprintf(ptr+off, BUFSIZ - off, "-%d ", n);
+                   off += snprintf(ptr+off, sizeof(buf) - off, "-%d ", n);
        }
        n = minorvers4 >= 0 ? minorvers4 : -minorvers4;
        if (n >= NFSD_MINMINORVERS4 && n <= NFSD_MAXMINORVERS4)
-                   off += snprintf(ptr+off, BUFSIZ - off, "%c4.%d",
+                   off += snprintf(ptr+off, sizeof(buf) - off, "%c4.%d",
                                    minorvers4 > 0 ? '+' : '-',
                                    n);
-       snprintf(ptr+off, BUFSIZ - off, "\n");
-       if (write(fd, buf, strlen(buf)) != strlen(buf)) {
-               syslog(LOG_ERR, "nfssvc: Setting version failed: errno %d (%s)", 
-                       errno, strerror(errno));
-       }
+       xlog(D_GENERAL, "Writing version string to kernel: %s", buf);
+       snprintf(ptr+off, sizeof(buf) - off, "\n");
+       if (write(fd, buf, strlen(buf)) != strlen(buf))
+               xlog(L_ERROR, "Setting version failed: errno %d (%m)", errno);
+
        close(fd);
 
        return;
@@ -169,9 +191,8 @@ nfssvc(int port, int nrservs, unsigned int versbits, int minorvers4,
                 * Just write the number in.
                 * Cannot handle port number yet, but does anyone care?
                 */
-               char buf[20];
                int n;
-               snprintf(buf, 20,"%d\n", nrservs);
+               snprintf(buf, sizeof(buf), "%d\n", nrservs);
                n = write(fd, buf, strlen(buf));
                close(fd);
                if (n != strlen(buf))