diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2011-02-15 13:18:20 -0800 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2011-02-15 13:18:20 -0800 |
commit | 83ca5b985b375b6445d54d99e523508f075a497e (patch) | |
tree | 1a9766c9dfa76e6e2446f8fc76bd72ba0eb25b91 /lib/getloadavg.c | |
parent | e7c16927f1b26641a2516c42e690cfe64451309f (diff) | |
download | gnulib-83ca5b985b375b6445d54d99e523508f075a497e.tar.gz |
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>.
getloadavg: trim unused parts and speed up 'configure'
* NEWS: Document this.
* lib/getloadavg.c: Ignore HAVE_GETLOADAVG; this file is now
always compiled if getloadavg is absent.
Move test code to ...
* tests/test-getloadavg.c: New file, containing previous
contents of test from lib/getloadavg.c. It also contains
suggestions by Bruno Haible in
<http://lists.gnu.org/archive/html/bug-gnulib/2011-02/msg00186.html>.
* modules/getloadavg-tests: New file.
* m4/getloadavg.m4 (gl_GETLOADAVG): Do not check for getloadavg twice.
Do tests in the same order as they're needed for getloadavg.c.
Omit setgid-related tests that generate symbols KMEM_GROUP,
NEET_SETGID, GETLOADAVG_PRIVILEGED; nobody seems to use those any more.
Do only the tests that are needed to see whether the system has
getloadavg, moving the other tests into ...
(gl_PREREQ_GETLOADAVG): ... here. Do not define obsolete symbol
NLIST_NAME_UNION; nobody should be using it. Do not define
symbols C_GETLOADAVG and HAVE_GETLOADAVG; they're no longer
relevant, as the user of this module shouldn't care how getloadavg
is implemented.
Diffstat (limited to 'lib/getloadavg.c')
-rw-r--r-- | lib/getloadavg.c | 88 |
1 files changed, 29 insertions, 59 deletions
diff --git a/lib/getloadavg.c b/lib/getloadavg.c index 29c9bfff00..28e2ea0164 100644 --- a/lib/getloadavg.c +++ b/lib/getloadavg.c @@ -94,11 +94,6 @@ #include <errno.h> #include <stdio.h> -/* Exclude all the code except the test program at the end - if the system has its own `getloadavg' function. */ - -#ifndef HAVE_GETLOADAVG - # include <sys/types.h> /* Both the Emacs and non-Emacs sections want this. Some @@ -500,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) @@ -509,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 @@ -521,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) @@ -559,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) @@ -605,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'; @@ -629,7 +627,10 @@ getloadavg (double loadavg[], int nelem) if (! ('0' <= *ptr && *ptr <= '9')) { if (elem == 0) - return -1; + { + errno = ENOTSUP; + return -1; + } break; } @@ -667,7 +668,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; @@ -708,7 +712,10 @@ getloadavg (double loadavg[], int nelem) } if (!getloadavg_initialized) - return -1; + { + errno = ENOTSUP; + return -1; + } # endif /* NeXT */ # if !defined (LDAV_DONE) && defined (UMAX) @@ -893,7 +900,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 @@ -1009,7 +1019,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. */ @@ -1024,51 +1037,8 @@ 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; } - -#endif /* ! HAVE_GETLOADAVG */ - -#ifdef TEST -int -main (int argc, char **argv) -{ - int naptime = 0; - - if (argc > 1) - naptime = atoi (argv[1]); - - while (1) - { - double avg[3]; - int loads; - - errno = 0; /* Don't be misled if it doesn't set errno. */ - loads = getloadavg (avg, 3); - if (loads == -1) - { - perror ("Error getting load average"); - return EXIT_FAILURE; - } - if (loads > 0) - printf ("1-minute: %f ", avg[0]); - if (loads > 1) - printf ("5-minute: %f ", avg[1]); - if (loads > 2) - printf ("15-minute: %f ", avg[2]); - if (loads > 0) - putchar ('\n'); - - if (naptime == 0) - break; - sleep (naptime); - } - - return EXIT_SUCCESS; -} -#endif /* TEST */ |