]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/unix/sysv/linux/getloadavg.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / getloadavg.c
CommitLineData
54e9386d 1/* Get system load averages. Linux (/proc/loadavg) version.
688903eb 2 Copyright (C) 1999-2018 Free Software Foundation, Inc.
54e9386d
RM
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
54e9386d
RM
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 13 Lesser General Public License for more details.
54e9386d 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
54e9386d
RM
18
19#include <errno.h>
54e9386d 20#include <fcntl.h>
84fb7c8c 21#include <locale.h>
54e9386d 22#include <stdlib.h>
84fb7c8c 23#include <unistd.h>
f1c30c98 24#include <not-cancel.h>
54e9386d
RM
25
26/* Put the 1 minute, 5 minute and 15 minute load averages
27 into the first NELEM elements of LOADAVG.
28 Return the number written (never more than 3, but may be less than NELEM),
29 or -1 if an error occurred. */
30
31int
32getloadavg (double loadavg[], int nelem)
33{
34 int fd;
35
c2284574 36 fd = __open_nocancel ("/proc/loadavg", O_RDONLY);
54e9386d
RM
37 if (fd < 0)
38 return -1;
39 else
40 {
41 char buf[65], *p;
42 ssize_t nread;
43 int i;
44
a748eb31 45 nread = __read_nocancel (fd, buf, sizeof buf - 1);
c181840c 46 __close_nocancel_nostatus (fd);
69a64e9b 47 if (nread <= 0)
54e9386d
RM
48 return -1;
49 buf[nread - 1] = '\0';
50
51 if (nelem > 3)
52 nelem = 3;
53 p = buf;
54 for (i = 0; i < nelem; ++i)
55 {
56 char *endp;
4b5b009c 57 loadavg[i] = __strtod_l (p, &endp, _nl_C_locobj_ptr);
69a64e9b 58 if (endp == p)
b73c2df9
UD
59 /* This should not happen. The format of /proc/loadavg
60 must have changed. Don't return with what we have,
61 signal an error. */
62 return -1;
54e9386d
RM
63 p = endp;
64 }
65
66 return i;
67 }
68}