bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: getloadavg and errno


From: Paul Eggert
Subject: Re: getloadavg and errno
Date: Tue, 15 Feb 2011 11:30:14 -0800
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101209 Fedora/3.1.7-0.35.b3pre.fc14 Thunderbird/3.1.7

On 02/15/2011 03:25 AM, Jim Meyering wrote:
Specifying that errno may be changed sounds better, even though
coreutils' sole use in uptime.c does not use that.

I think I'd prefer a getloadavg that always sets errno when it
fails; that's simpler and regular, and it's what getloadavg
does on GNU/Linux and on Solaris.  Here's an addition to the
proposed patch, to do that.

From c40b13d678953540adb6a381a74dd61cbfc4e3c4 Mon Sep 17 00:00:00 2001
From: Paul Eggert <address@hidden>
Date: Tue, 15 Feb 2011 11:13:28 -0800
Subject: [PATCH] getloadavg: set errno

* lib/getloadavg.c: Set errno when returning -1.  If no other
error number looks appropriate, set it to ENOSYS if the getloadavg
looks like it can't possibly ever work, ENOTSUP otherwise.
Suggested by Bruno Haible in
<http://lists.gnu.org/archive/html/bug-gnulib/2011-02/msg00187.html>.
---
 ChangeLog        |   10 ++++++++++
 lib/getloadavg.c |   42 +++++++++++++++++++++++++++++-------------
 2 files changed, 39 insertions(+), 13 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 4fc846d..bcc8aa7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2011-02-15  Paul Eggert  <address@hidden>
+
+       getloadavg: set errno
+
+       * lib/getloadavg.c: Set errno when returning -1.  If no other
+       error number looks appropriate, set it to ENOSYS if the getloadavg
+       looks like it can't possibly ever work, ENOTSUP otherwise.
+       Suggested by Bruno Haible in
+       <http://lists.gnu.org/archive/html/bug-gnulib/2011-02/msg00187.html>.
+
 2011-02-14  Paul Eggert  <address@hidden>
getloadavg: trim unused parts and speed up 'configure'
diff --git a/lib/getloadavg.c b/lib/getloadavg.c
index 8e91714..e278e0c 100644
--- a/lib/getloadavg.c
+++ b/lib/getloadavg.c
@@ -495,7 +495,7 @@ static kvm_t *kd;
 /* Put the 1 minute, 5 minute and 15 minute load averages
    into the first NELEM elements of LOADAVG.
    Return the number written (never more than 3, but may be less than NELEM),
-   or -1 if an error occurred.  */
+   or -1 (setting errno) if an error occurred.  */
int
 getloadavg (double loadavg[], int nelem)
@@ -504,9 +504,7 @@ getloadavg (double loadavg[], int nelem)
# ifdef NO_GET_LOAD_AVG
 #  define LDAV_DONE
-  /* Set errno to zero to indicate that there was no particular error;
-     this function just can't work at all on this system.  */
-  errno = 0;
+  errno = ENOSYS;
   elem = -1;
 # endif
@@ -516,6 +514,7 @@ getloadavg (double loadavg[], int nelem)
   kstat_ctl_t *kc;
   kstat_t *ksp;
   kstat_named_t *kn;
+  int saved_errno;
kc = kstat_open ();
   if (kc == 0)
@@ -554,7 +553,9 @@ getloadavg (double loadavg[], int nelem)
         }
     }
+ saved_errno = errno;
   kstat_close (kc);
+  errno = saved_errno;
 # endif /* HAVE_LIBKSTAT */
# if !defined (LDAV_DONE) && defined (hpux) && defined (HAVE_PSTAT_GETDYNAMIC)
@@ -600,13 +601,15 @@ getloadavg (double loadavg[], int nelem)
char ldavgbuf[3 * (INT_STRLEN_BOUND (int) + sizeof ".00 ")];
   char const *ptr = ldavgbuf;
-  int fd, count;
+  int fd, count, saved_errno;
fd = open (LINUX_LDAV_FILE, O_RDONLY);
   if (fd == -1)
     return -1;
   count = read (fd, ldavgbuf, sizeof ldavgbuf - 1);
+  saved_errno = errno;
   (void) close (fd);
+  errno = saved_errno;
   if (count <= 0)
     return -1;
   ldavgbuf[count] = '\0';
@@ -625,7 +628,10 @@ getloadavg (double loadavg[], int nelem)
       if (! ('0' <= *ptr && *ptr <= '9'))
         {
           if (elem == 0)
-            return -1;
+            {
+              errno = ENOTSUP;
+              return -1;
+            }
           break;
         }
@@ -663,7 +669,10 @@ getloadavg (double loadavg[], int nelem)
                   &scale);
   (void) fclose (fp);
   if (count != 4)
-    return -1;
+    {
+      errno = ENOTSUP;
+      return -1;
+    }
for (elem = 0; elem < nelem; elem++)
     loadavg[elem] = (double) load_ave[elem] / (double) scale;
@@ -704,7 +713,10 @@ getloadavg (double loadavg[], int nelem)
     }
if (!getloadavg_initialized)
-    return -1;
+    {
+      errno = ENOTSUP;
+      return -1;
+    }
 # endif /* NeXT */
# if !defined (LDAV_DONE) && defined (UMAX)
@@ -889,7 +901,10 @@ getloadavg (double loadavg[], int nelem)
     }
if (!getloadavg_initialized)
-    return -1;
+    {
+      errno = ENOTSUP;
+      return -1;
+    }
 # endif /* ! defined LDAV_DONE && defined __VMS */
# if ! defined LDAV_DONE && defined LOAD_AVE_TYPE && ! defined __VMS
@@ -1005,7 +1020,10 @@ getloadavg (double loadavg[], int nelem)
     }
if (offset == 0 || !getloadavg_initialized)
-    return -1;
+    {
+      errno = ENOTSUP;
+      return -1;
+    }
 # endif /* ! defined LDAV_DONE && defined LOAD_AVE_TYPE && ! defined __VMS */
# if !defined (LDAV_DONE) && defined (LOAD_AVE_TYPE) /* Including VMS. */
@@ -1020,9 +1038,7 @@ getloadavg (double loadavg[], int nelem)
 # endif /* !LDAV_DONE && LOAD_AVE_TYPE */
# if !defined LDAV_DONE
-  /* Set errno to zero to indicate that there was no particular error;
-     this function just can't work at all on this system.  */
-  errno = 0;
+  errno = ENOSYS;
   elem = -1;
 # endif
   return elem;
--
1.7.4




reply via email to

[Prev in Thread] Current Thread [Next in Thread]