]> git.ipfire.org Git - thirdparty/rrdtool-1.x.git/commitdiff
src/rrd_list.c: fix errno
authorAlan Jenkins <alan.christopher.jenkins@gmail.com>
Tue, 12 Sep 2017 15:26:50 +0000 (16:26 +0100)
committerAlan Jenkins <alan.christopher.jenkins@gmail.com>
Tue, 12 Sep 2017 15:40:56 +0000 (16:40 +0100)
rrd_daemon expected zero errno iff there was not an error
(and we forgot to provide it).

  list = rrd_list_r(recursive, fullpath);

  if (list == NULL) {
    /* Empty directory listing */
    if (errno == 0) {
      goto out_send_response;
    }

src/rrd_list.c

index 7a244cfd8e2e40c0eabaeac2aa14ee112b23ccf6..5d7f41ea96cf84669ea080212893c659356ca77e 100644 (file)
@@ -123,6 +123,7 @@ static char *rrd_list_rec(int recursive, char *root, char *dirname)
        }
        closedir(dir);
 
+       errno = 0;
        return out;
 }
 
@@ -195,12 +196,23 @@ char *rrd_list_r(int recursive, char *dirname)
 
        if (ptr != NULL && strlen(ptr) == 4) {
 
-               if (!stat(dirname, &st) && S_ISREG(st.st_mode)) {
-                       ptr = strrchr(dirname, '/');
+               if (stat(dirname, &st)) {
+                       return NULL;
+                }
 
-                       if (ptr) {
-                               SANE_ASPRINTF(out, "%s\n", ptr + 1);
-                       }
+               if (!S_ISREG(st.st_mode)) {
+                       /* Same errno as if you try to open() a named socket.
+                        * This is less misleading than e.g. EISDIR */
+                       errno = ENXIO;
+                       return NULL;
+               }
+
+               ptr = strrchr(dirname, '/');
+
+               if (ptr) {
+                       SANE_ASPRINTF(out, "%s\n", ptr + 1);
+               } else {
+                       errno = EINVAL;
                }
                return out;
        }
@@ -211,6 +223,7 @@ char *rrd_list_r(int recursive, char *dirname)
        }
 
        if (!S_ISDIR(st.st_mode)) {
+               errno = ENOTDIR;
                return NULL;
        }
        return rrd_list_rec(recursive, dirname, dirname);