]> git.decadent.org.uk Git - nfs-utils.git/blobdiff - support/nfs/cacheio.c
mountd: fix checking for errors when exporting filesystems
[nfs-utils.git] / support / nfs / cacheio.c
index 48292f8a5d49841c48f323c1b5977691396ef6be..61e07a8257c961a7815ac0ae3816352cf14cfba5 100644 (file)
@@ -24,6 +24,7 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <time.h>
+#include <errno.h>
 
 void qword_add(char **bpp, int *lp, char *str)
 {
@@ -125,7 +126,10 @@ void qword_print(FILE *f, char *str)
        char *bp = qword_buf;
        int len = sizeof(qword_buf);
        qword_add(&bp, &len, str);
-       fwrite(qword_buf, bp-qword_buf, 1, f);
+       if (fwrite(qword_buf, bp-qword_buf, 1, f) != 1) {
+               xlog_warn("qword_print: fwrite failed: errno %d (%s)",
+                       errno, strerror(errno));
+       }
 }
 
 void qword_printhex(FILE *f, char *str, int slen)
@@ -133,7 +137,10 @@ void qword_printhex(FILE *f, char *str, int slen)
        char *bp = qword_buf;
        int len = sizeof(qword_buf);
        qword_addhex(&bp, &len, str, slen);
-       fwrite(qword_buf, bp-qword_buf, 1, f);
+       if (fwrite(qword_buf, bp-qword_buf, 1, f) != 1) {
+               xlog_warn("qword_printhex: fwrite failed: errno %d (%s)",
+                       errno, strerror(errno));
+       }
 }
 
 void qword_printint(FILE *f, int num)
@@ -141,21 +148,42 @@ void qword_printint(FILE *f, int num)
        fprintf(f, "%d ", num);
 }
 
+void qword_printuint(FILE *f, unsigned int num)
+{
+       fprintf(f, "%u ", num);
+}
+
+void qword_printtimefrom(FILE *f, unsigned int num)
+{
+       fprintf(f, "%lu ", time(0) + num);
+}
+
 int qword_eol(FILE *f)
 {
        int err;
 
-       fprintf(f,"\n");
-       err = fflush(f);
+       err = fprintf(f,"\n");
+       if (err < 0) {
+               xlog_warn("qword_eol: fprintf failed: errno %d (%s)",
+                           errno, strerror(errno));
+       } else {
+               err = fflush(f);
+               if (err) {
+                       xlog_warn("qword_eol: fflush failed: errno %d (%s)",
+                                 errno, strerror(errno));
+               }
+       }
        /*
         * We must send one line (and one line only) in a single write
         * call.  In case of a write error, libc may accumulate the
         * unwritten data and try to write it again later, resulting in a
         * multi-line write.  So we must explicitly ask it to throw away
-        * any such cached data:
+        * any such cached data.  But we return any original error
+        * indication to the caller.
         */
        __fpurge(f);
-       return fflush(f);
+       fflush(f);
+       return err;
 }
 
 
@@ -223,6 +251,20 @@ int qword_get_int(char **bpp, int *anint)
        return 0;
 }
 
+int qword_get_uint(char **bpp, unsigned int *anint)
+{
+       char buf[50];
+       char *ep;
+       unsigned int rv;
+       int len = qword_get(bpp, buf, 50);
+       if (len < 0) return -1;
+       if (len ==0) return -1;
+       rv = strtoul(buf, &ep, 0);
+       if (*ep) return -1;
+       *anint = rv;
+       return 0;
+}
+
 #define READLINE_BUFFER_INCREMENT 2048
 
 int readline(int fd, char **buf, int *lenp)
@@ -272,9 +314,8 @@ int readline(int fd, char **buf, int *lenp)
 int
 check_new_cache(void)
 {
-       struct stat stb;
-       return  (stat("/proc/fs/nfs/filehandle", &stb) == 0) ||
-               (stat("/proc/fs/nfsd/filehandle", &stb) == 0);
+       return  (access("/proc/fs/nfs/filehandle", F_OK) == 0) ||
+               (access("/proc/fs/nfsd/filehandle", F_OK) == 0);
 }      
 
 
@@ -318,7 +359,10 @@ cache_flush(int force)
                sprintf(path, "/proc/net/rpc/%s/flush", cachelist[c]);
                fd = open(path, O_RDWR);
                if (fd >= 0) {
-                       write(fd, stime, strlen(stime));
+                       if (write(fd, stime, strlen(stime)) != (ssize_t)strlen(stime)) {
+                               xlog_warn("Writing to '%s' failed: errno %d (%s)",
+                               path, errno, strerror(errno));
+                       }
                        close(fd);
                }
        }