| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "extract-word.h" |
| 7 |
#include "fd-util.h" |
| 8 |
#include "fileio.h" |
| 9 |
#include "parse-util.h" |
| 10 |
#include "process-util.h" |
| 11 |
#include "procfs-util.h" |
| 12 |
#include "stdio-util.h" |
| 13 |
#include "string-util.h" |
| 14 |
#include "time-util.h" |
| 15 |
|
| 16 |
int procfs_get_pid_max(uint64_t *ret) { |
| 17 |
_cleanup_free_ char *value = NULL; |
| 18 |
int r; |
| 19 |
|
| 20 |
assert(ret); |
| 21 |
|
| 22 |
r = read_one_line_file("/proc/sys/kernel/pid_max", &value); |
| 23 |
if (r < 0) |
| 24 |
return r; |
| 25 |
|
| 26 |
return safe_atou64(value, ret); |
| 27 |
} |
| 28 |
|
| 29 |
int procfs_get_threads_max(uint64_t *ret) { |
| 30 |
_cleanup_free_ char *value = NULL; |
| 31 |
int r; |
| 32 |
|
| 33 |
assert(ret); |
| 34 |
|
| 35 |
r = read_one_line_file("/proc/sys/kernel/threads-max", &value); |
| 36 |
if (r < 0) |
| 37 |
return r; |
| 38 |
|
| 39 |
return safe_atou64(value, ret); |
| 40 |
} |
| 41 |
|
| 42 |
int procfs_tasks_set_limit(uint64_t limit) { |
| 43 |
char buffer[DECIMAL_STR_MAX(uint64_t)+1]; |
| 44 |
uint64_t pid_max; |
| 45 |
int r; |
| 46 |
|
| 47 |
if (limit == 0) /* This makes no sense, we are userspace and hence count as tasks too, and we want to live, |
| 48 |
* hence the limit conceptually has to be above 0. Also, most likely if anyone asks for a zero |
| 49 |
* limit they probably mean "no limit", hence let's better refuse this to avoid |
| 50 |
* confusion. */ |
| 51 |
return -EINVAL; |
| 52 |
|
| 53 |
/* The Linux kernel doesn't allow this value to go below 20, hence don't allow this either, higher values than |
| 54 |
* TASKS_MAX are not accepted by the pid_max sysctl. We'll treat anything this high as "unbounded" and hence |
| 55 |
* set it to the maximum. */ |
| 56 |
limit = CLAMP(limit, 20U, TASKS_MAX); |
| 57 |
|
| 58 |
r = procfs_get_pid_max(&pid_max); |
| 59 |
if (r < 0) |
| 60 |
return r; |
| 61 |
|
| 62 |
/* As pid_max is about the numeric pid_t range we'll bump it if necessary, but only ever increase it, never |
| 63 |
* decrease it, as threads-max is the much more relevant sysctl. */ |
| 64 |
if (limit > pid_max-1) { |
| 65 |
xsprintf(buffer, "%" PRIu64, limit+1); /* Add one, since PID 0 is not a valid PID */ |
| 66 |
r = write_string_file("/proc/sys/kernel/pid_max", buffer, WRITE_STRING_FILE_DISABLE_BUFFER); |
| 67 |
if (r < 0) |
| 68 |
return r; |
| 69 |
} |
| 70 |
|
| 71 |
xsprintf(buffer, "%" PRIu64, limit); |
| 72 |
r = write_string_file("/proc/sys/kernel/threads-max", buffer, WRITE_STRING_FILE_DISABLE_BUFFER); |
| 73 |
if (r < 0) { |
| 74 |
uint64_t threads_max; |
| 75 |
|
| 76 |
/* Hmm, we couldn't write this? If so, maybe it was already set properly? In that case let's not |
| 77 |
* generate an error */ |
| 78 |
|
| 79 |
if (procfs_get_threads_max(&threads_max) < 0) |
| 80 |
return r; /* return original error */ |
| 81 |
|
| 82 |
if (MIN(pid_max - 1, threads_max) != limit) |
| 83 |
return r; /* return original error */ |
| 84 |
|
| 85 |
/* Yay! Value set already matches what we were trying to set, hence consider this a success. */ |
| 86 |
} |
| 87 |
|
| 88 |
return 0; |
| 89 |
} |
| 90 |
|
| 91 |
int procfs_tasks_get_current(uint64_t *ret) { |
| 92 |
_cleanup_free_ char *value = NULL; |
| 93 |
const char *p, *nr; |
| 94 |
size_t n; |
| 95 |
int r; |
| 96 |
|
| 97 |
assert(ret); |
| 98 |
|
| 99 |
r = read_one_line_file("/proc/loadavg", &value); |
| 100 |
if (r < 0) |
| 101 |
return r; |
| 102 |
|
| 103 |
/* Look for the second part of the fourth field, which is separated by a slash from the first part. None of the |
| 104 |
* earlier fields use a slash, hence let's use this to find the right spot. */ |
| 105 |
p = strchr(value, '/'); |
| 106 |
if (!p) |
| 107 |
return -EINVAL; |
| 108 |
|
| 109 |
p++; |
| 110 |
n = strspn(p, DIGITS); |
| 111 |
nr = strndupa_safe(p, n); |
| 112 |
|
| 113 |
return safe_atou64(nr, ret); |
| 114 |
} |
| 115 |
|
| 116 |
static uint64_t calc_gcd64(uint64_t a, uint64_t b) { |
| 117 |
|
| 118 |
while (b > 0) { |
| 119 |
uint64_t t; |
| 120 |
|
| 121 |
t = a % b; |
| 122 |
|
| 123 |
a = b; |
| 124 |
b = t; |
| 125 |
} |
| 126 |
|
| 127 |
return a; |
| 128 |
} |
| 129 |
|
| 130 |
int procfs_cpu_get_usage(nsec_t *ret) { |
| 131 |
_cleanup_free_ char *first_line = NULL; |
| 132 |
unsigned long user_ticks, nice_ticks, system_ticks, irq_ticks, softirq_ticks, |
| 133 |
guest_ticks = 0, guest_nice_ticks = 0; |
| 134 |
long ticks_per_second; |
| 135 |
uint64_t sum, gcd, a, b; |
| 136 |
const char *p; |
| 137 |
int r; |
| 138 |
|
| 139 |
assert(ret); |
| 140 |
|
| 141 |
r = read_one_line_file("/proc/stat", &first_line); |
| 142 |
if (r < 0) |
| 143 |
return r; |
| 144 |
|
| 145 |
p = first_word(first_line, "cpu"); |
| 146 |
if (!p) |
| 147 |
return -EINVAL; |
| 148 |
|
| 149 |
if (sscanf(p, "%lu %lu %lu %*u %*u %lu %lu %*u %lu %lu", |
| 150 |
&user_ticks, |
| 151 |
&nice_ticks, |
| 152 |
&system_ticks, |
| 153 |
&irq_ticks, |
| 154 |
&softirq_ticks, |
| 155 |
&guest_ticks, |
| 156 |
&guest_nice_ticks) < 5) /* we only insist on the first five fields */ |
| 157 |
return -EINVAL; |
| 158 |
|
| 159 |
ticks_per_second = sysconf(_SC_CLK_TCK); |
| 160 |
if (ticks_per_second < 0) |
| 161 |
return -errno; |
| 162 |
assert(ticks_per_second > 0); |
| 163 |
|
| 164 |
sum = (uint64_t) user_ticks + (uint64_t) nice_ticks + (uint64_t) system_ticks + |
| 165 |
(uint64_t) irq_ticks + (uint64_t) softirq_ticks + |
| 166 |
(uint64_t) guest_ticks + (uint64_t) guest_nice_ticks; |
| 167 |
|
| 168 |
/* Let's reduce this fraction before we apply it to avoid overflows when converting this to μsec */ |
| 169 |
gcd = calc_gcd64(NSEC_PER_SEC, ticks_per_second); |
| 170 |
|
| 171 |
a = (uint64_t) NSEC_PER_SEC / gcd; |
| 172 |
b = (uint64_t) ticks_per_second / gcd; |
| 173 |
|
| 174 |
*ret = DIV_ROUND_UP((nsec_t) sum * (nsec_t) a, (nsec_t) b); |
| 175 |
return 0; |
| 176 |
} |
| 177 |
|
| 178 |
int convert_meminfo_value_to_uint64_bytes(const char *s, uint64_t *ret) { |
| 179 |
_cleanup_free_ char *w = NULL; |
| 180 |
uint64_t v; |
| 181 |
int r; |
| 182 |
|
| 183 |
assert(s); |
| 184 |
assert(ret); |
| 185 |
|
| 186 |
r = extract_first_word(&s, &w, /* separators= */ NULL, /* flags= */ 0); |
| 187 |
if (r < 0) |
| 188 |
return r; |
| 189 |
if (r == 0) |
| 190 |
return -EINVAL; |
| 191 |
|
| 192 |
/* Ensure the line ends in "kB" */ |
| 193 |
if (!streq(s, "kB")) |
| 194 |
return -EINVAL; |
| 195 |
|
| 196 |
r = safe_atou64(w, &v); |
| 197 |
if (r < 0) |
| 198 |
return r; |
| 199 |
if (v == UINT64_MAX) |
| 200 |
return -EINVAL; |
| 201 |
|
| 202 |
if (!MUL_ASSIGN_SAFE(&v, U64_KB)) |
| 203 |
return -EOVERFLOW; |
| 204 |
|
| 205 |
*ret = v; |
| 206 |
return 0; |
| 207 |
} |
| 208 |
|
| 209 |
int procfs_memory_get(uint64_t *ret_total, uint64_t *ret_used) { |
| 210 |
uint64_t mem_total = UINT64_MAX, mem_available = UINT64_MAX; |
| 211 |
_cleanup_fclose_ FILE *f = NULL; |
| 212 |
int r; |
| 213 |
|
| 214 |
f = fopen("/proc/meminfo", "re"); |
| 215 |
if (!f) |
| 216 |
return -errno; |
| 217 |
|
| 218 |
for (;;) { |
| 219 |
_cleanup_free_ char *line = NULL; |
| 220 |
uint64_t *v; |
| 221 |
char *p; |
| 222 |
|
| 223 |
r = read_line(f, LONG_LINE_MAX, &line); |
| 224 |
if (r < 0) |
| 225 |
return r; |
| 226 |
if (r == 0) |
| 227 |
return -EINVAL; /* EOF: Couldn't find one or both fields? */ |
| 228 |
|
| 229 |
p = first_word(line, "MemTotal:"); |
| 230 |
if (p) |
| 231 |
v = &mem_total; |
| 232 |
else { |
| 233 |
p = first_word(line, "MemAvailable:"); |
| 234 |
if (p) |
| 235 |
v = &mem_available; |
| 236 |
else |
| 237 |
continue; |
| 238 |
} |
| 239 |
|
| 240 |
r = convert_meminfo_value_to_uint64_bytes(p, v); |
| 241 |
if (r < 0) |
| 242 |
return r; |
| 243 |
|
| 244 |
if (mem_total != UINT64_MAX && mem_available != UINT64_MAX) |
| 245 |
break; |
| 246 |
} |
| 247 |
|
| 248 |
if (mem_available > mem_total) |
| 249 |
return -EINVAL; |
| 250 |
|
| 251 |
if (ret_total) |
| 252 |
*ret_total = mem_total; |
| 253 |
if (ret_used) |
| 254 |
*ret_used = mem_total - mem_available; |
| 255 |
return 0; |
| 256 |
} |