summaryrefslogtreecommitdiffstats
path: root/gl/getloadavg.c
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-12-28 12:13:40 +0100
committerLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-12-28 12:13:40 +0100
commitb0afb8fe0ff1d87165af9df61501197a06240dda (patch)
tree274ac6a96c53ef4c19ab4974ce24a06a233128c5 /gl/getloadavg.c
parent68fc05381ee5fa0aee1413118fbb3d81ca888b09 (diff)
downloadmonitoring-plugins-b0afb8fe0ff1d87165af9df61501197a06240dda.tar.gz
Sync with Gnulib stable-202507 code (a8ac9f9ce5)
Diffstat (limited to 'gl/getloadavg.c')
-rw-r--r--gl/getloadavg.c48
1 files changed, 31 insertions, 17 deletions
diff --git a/gl/getloadavg.c b/gl/getloadavg.c
index c940e4c7..752ec1f5 100644
--- a/gl/getloadavg.c
+++ b/gl/getloadavg.c
@@ -1,6 +1,6 @@
1/* Get the system load averages. 1/* Get the system load averages.
2 2
3 Copyright (C) 1985-1989, 1991-1995, 1997, 1999-2000, 2003-2024 Free Software 3 Copyright (C) 1985-1989, 1991-1995, 1997, 1999-2000, 2003-2025 Free Software
4 Foundation, Inc. 4 Foundation, Inc.
5 5
6 NOTE: The canonical source of this file is maintained with gnulib. 6 NOTE: The canonical source of this file is maintained with gnulib.
@@ -47,8 +47,6 @@
47 N_NAME_POINTER The nlist n_name element is a pointer, 47 N_NAME_POINTER The nlist n_name element is a pointer,
48 not an array. 48 not an array.
49 HAVE_STRUCT_NLIST_N_UN_N_NAME 'n_un.n_name' is member of 'struct nlist'. 49 HAVE_STRUCT_NLIST_N_UN_N_NAME 'n_un.n_name' is member of 'struct nlist'.
50 LINUX_LDAV_FILE [__linux__, __ANDROID__, __CYGWIN__]: File
51 containing load averages.
52 50
53 Specific system predefines this file uses, aside from setting 51 Specific system predefines this file uses, aside from setting
54 default values if not emacs: 52 default values if not emacs:
@@ -65,8 +63,7 @@
65 UMAX4_3 63 UMAX4_3
66 VMS 64 VMS
67 _WIN32 Native Windows (possibly also defined on Cygwin) 65 _WIN32 Native Windows (possibly also defined on Cygwin)
68 __linux__, __ANDROID__ Linux: assumes /proc file system mounted. 66 __linux__, __ANDROID__ Linux: assumes sysinfo() call.
69 Support from Michael K. Johnson.
70 __CYGWIN__ Cygwin emulates linux /proc/loadavg. 67 __CYGWIN__ Cygwin emulates linux /proc/loadavg.
71 __NetBSD__ NetBSD: assumes /kern file system mounted. 68 __NetBSD__ NetBSD: assumes /kern file system mounted.
72 69
@@ -108,10 +105,10 @@
108# endif 105# endif
109 106
110/* Same issues as for NeXT apply to the HURD-based GNU system. */ 107/* Same issues as for NeXT apply to the HURD-based GNU system. */
111# ifdef __GNU__ 108# if defined __gnu_hurd__ || defined NeXT
112# undef BSD 109# undef BSD
113# undef FSCALE 110# undef FSCALE
114# endif /* __GNU__ */ 111# endif /* __gnu_hurd__ || NeXT */
115 112
116/* Set values that are different from the defaults, which are 113/* Set values that are different from the defaults, which are
117 set a little farther down with #ifndef. */ 114 set a little farther down with #ifndef. */
@@ -143,7 +140,7 @@
143# define SUNOS_5 140# define SUNOS_5
144# endif 141# endif
145 142
146# if defined (__osf__) && (defined (__alpha) || defined (__alpha__)) 143# if defined (__osf__) && defined (__alpha)
147# define OSF_ALPHA 144# define OSF_ALPHA
148# include <sys/mbuf.h> 145# include <sys/mbuf.h>
149# include <sys/socket.h> 146# include <sys/socket.h>
@@ -312,8 +309,7 @@
312# endif 309# endif
313# endif 310# endif
314 311
315# if defined (__GNU__) && !defined (NeXT) 312# if defined __gnu_hurd__ && !defined NeXT
316/* Note that NeXT Openstep defines __GNU__ even though it should not. */
317/* GNU system acts much like NeXT, for load average purposes, 313/* GNU system acts much like NeXT, for load average purposes,
318 but not exactly. */ 314 but not exactly. */
319# define NeXT 315# define NeXT
@@ -358,6 +354,11 @@
358# include <sys/dg_sys_info.h> 354# include <sys/dg_sys_info.h>
359# endif 355# endif
360 356
357# if defined __linux__ || defined __ANDROID__
358# include <sys/param.h>
359# include <sys/sysinfo.h>
360# endif
361
361# if (defined __linux__ || defined __ANDROID__ \ 362# if (defined __linux__ || defined __ANDROID__ \
362 || defined __CYGWIN__ || defined SUNOS_5 \ 363 || defined __CYGWIN__ || defined SUNOS_5 \
363 || (defined LOAD_AVE_TYPE && ! defined __VMS)) 364 || (defined LOAD_AVE_TYPE && ! defined __VMS))
@@ -498,20 +499,33 @@ getloadavg (double loadavg[], int nelem)
498 } 499 }
499# endif 500# endif
500 501
501# if !defined (LDAV_DONE) && (defined __linux__ || defined __ANDROID__ || defined __CYGWIN__) 502# if (!defined LDAV_DONE \
502 /* Linux without glibc, Android, Cygwin */ 503 && (defined __ANDROID__ ? 13 <= __ANDROID_API__ : defined __linux__))
504 /* non-Android Linux without glibc, Android 3.2+, Cygwin */
503# define LDAV_DONE 505# define LDAV_DONE
504# undef LOAD_AVE_TYPE 506# undef LOAD_AVE_TYPE
505 507
506# ifndef LINUX_LDAV_FILE 508 {
507# define LINUX_LDAV_FILE "/proc/loadavg" 509 struct sysinfo info;
508# endif 510 if (sysinfo (&info) < 0)
511 return -1;
512 loadavg[0] = info.loads[0] / (double)(1U << SI_LOAD_SHIFT);
513 loadavg[1] = info.loads[1] / (double)(1U << SI_LOAD_SHIFT);
514 loadavg[2] = info.loads[2] / (double)(1U << SI_LOAD_SHIFT);
515 elem = 3;
516 }
517# endif /* __ANDROID__ ? 13 <= __ANDROID_API__ : __linux__ */
518
519# if !defined (LDAV_DONE) && defined __CYGWIN__
520 /* Cygwin */
521# define LDAV_DONE
522# undef LOAD_AVE_TYPE
509 523
510 char ldavgbuf[3 * (INT_STRLEN_BOUND (int) + sizeof ".00 ")]; 524 char ldavgbuf[3 * (INT_STRLEN_BOUND (int) + sizeof ".00 ")];
511 char const *ptr = ldavgbuf; 525 char const *ptr = ldavgbuf;
512 int fd, count, saved_errno; 526 int fd, count, saved_errno;
513 527
514 fd = open (LINUX_LDAV_FILE, O_RDONLY | O_CLOEXEC); 528 fd = open ("/proc/loadavg", O_RDONLY | O_CLOEXEC);
515 if (fd == -1) 529 if (fd == -1)
516 return -1; 530 return -1;
517 count = read (fd, ldavgbuf, sizeof ldavgbuf - 1); 531 count = read (fd, ldavgbuf, sizeof ldavgbuf - 1);
@@ -554,7 +568,7 @@ getloadavg (double loadavg[], int nelem)
554 568
555 return elem; 569 return elem;
556 570
557# endif /* __linux__ || __ANDROID__ || __CYGWIN__ */ 571# endif /* __CYGWIN__ */
558 572
559# if !defined (LDAV_DONE) && defined (__NetBSD__) /* NetBSD < 0.9 */ 573# if !defined (LDAV_DONE) && defined (__NetBSD__) /* NetBSD < 0.9 */
560# define LDAV_DONE 574# define LDAV_DONE