| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-json.h" |
| 4 |
|
| 5 |
#include "bpf-program.h" |
| 6 |
#include "cgroup.h" |
| 7 |
#include "json-util.h" |
| 8 |
#include "in-addr-prefix-util.h" |
| 9 |
#include "ip-protocol-list.h" |
| 10 |
#include "path-util.h" |
| 11 |
#include "set.h" |
| 12 |
#include "unit.h" |
| 13 |
#include "varlink-cgroup.h" |
| 14 |
#include "varlink-common.h" |
| 15 |
|
| 16 |
static int tasks_max_build_json(sd_json_variant **ret, const char *name, void *userdata) { |
| 17 |
CGroupTasksMax *tasks_max = ASSERT_PTR(userdata); |
| 18 |
|
| 19 |
assert(ret); |
| 20 |
assert(name); |
| 21 |
|
| 22 |
if (!cgroup_tasks_max_isset(tasks_max)) { |
| 23 |
*ret = NULL; |
| 24 |
return 0; |
| 25 |
} |
| 26 |
|
| 27 |
return sd_json_buildo( |
| 28 |
ret, |
| 29 |
SD_JSON_BUILD_PAIR_UNSIGNED("value", tasks_max->value), |
| 30 |
SD_JSON_BUILD_PAIR_UNSIGNED("scale", tasks_max->scale)); |
| 31 |
} |
| 32 |
|
| 33 |
static int io_device_weights_build_json(sd_json_variant **ret, const char *name, void *userdata) { |
| 34 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 35 |
CGroupIODeviceWeight *weights = userdata; |
| 36 |
int r; |
| 37 |
|
| 38 |
assert(ret); |
| 39 |
assert(name); |
| 40 |
|
| 41 |
LIST_FOREACH(device_weights, w, weights) { |
| 42 |
r = sd_json_variant_append_arraybo( |
| 43 |
&v, |
| 44 |
SD_JSON_BUILD_PAIR_STRING("path", w->path), |
| 45 |
SD_JSON_BUILD_PAIR_UNSIGNED("weight", w->weight)); |
| 46 |
if (r < 0) |
| 47 |
return r; |
| 48 |
} |
| 49 |
|
| 50 |
*ret = TAKE_PTR(v); |
| 51 |
return 0; |
| 52 |
} |
| 53 |
|
| 54 |
static int io_device_limits_build_json(sd_json_variant **ret, const char *name, void *userdata) { |
| 55 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 56 |
CGroupIODeviceLimit *limits = userdata; |
| 57 |
int r; |
| 58 |
|
| 59 |
assert(ret); |
| 60 |
assert(name); |
| 61 |
|
| 62 |
CGroupIOLimitType type = cgroup_io_limit_type_from_string(name); |
| 63 |
assert(type >= 0); |
| 64 |
|
| 65 |
LIST_FOREACH(device_limits, l, limits) { |
| 66 |
if (l->limits[type] == cgroup_io_limit_defaults[type]) |
| 67 |
continue; |
| 68 |
|
| 69 |
r = sd_json_variant_append_arraybo( |
| 70 |
&v, |
| 71 |
SD_JSON_BUILD_PAIR_STRING("path", l->path), |
| 72 |
SD_JSON_BUILD_PAIR_UNSIGNED("limit", l->limits[type])); |
| 73 |
if (r < 0) |
| 74 |
return r; |
| 75 |
} |
| 76 |
|
| 77 |
*ret = TAKE_PTR(v); |
| 78 |
return 0; |
| 79 |
} |
| 80 |
|
| 81 |
static int io_device_latencies_build_json(sd_json_variant **ret, const char *name, void *userdata) { |
| 82 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 83 |
CGroupIODeviceLatency *latencies = userdata; |
| 84 |
int r; |
| 85 |
|
| 86 |
assert(ret); |
| 87 |
assert(name); |
| 88 |
|
| 89 |
LIST_FOREACH(device_latencies, l, latencies) { |
| 90 |
r = sd_json_variant_append_arraybo( |
| 91 |
&v, |
| 92 |
SD_JSON_BUILD_PAIR_STRING("path", l->path), |
| 93 |
JSON_BUILD_PAIR_FINITE_USEC("targetUSec", l->target_usec)); |
| 94 |
if (r < 0) |
| 95 |
return r; |
| 96 |
} |
| 97 |
|
| 98 |
*ret = TAKE_PTR(v); |
| 99 |
return 0; |
| 100 |
} |
| 101 |
|
| 102 |
static int ip_address_access_build_json(sd_json_variant **ret, const char *name, void *userdata) { |
| 103 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 104 |
Set *prefixes = userdata; |
| 105 |
int r; |
| 106 |
|
| 107 |
assert(ret); |
| 108 |
assert(name); |
| 109 |
|
| 110 |
struct in_addr_prefix *i; |
| 111 |
SET_FOREACH(i, prefixes) { |
| 112 |
r = sd_json_variant_append_arraybo( |
| 113 |
&v, |
| 114 |
SD_JSON_BUILD_PAIR_INTEGER("family", i->family), |
| 115 |
JSON_BUILD_PAIR_IN_ADDR("address", i->family, &i->address), |
| 116 |
SD_JSON_BUILD_PAIR_UNSIGNED("prefixLength", i->prefixlen)); |
| 117 |
if (r < 0) |
| 118 |
return r; |
| 119 |
} |
| 120 |
|
| 121 |
*ret = TAKE_PTR(v); |
| 122 |
return 0; |
| 123 |
} |
| 124 |
|
| 125 |
static int socket_bind_build_json(sd_json_variant **ret, const char *name, void *userdata) { |
| 126 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 127 |
CGroupSocketBindItem *items = userdata; |
| 128 |
int r; |
| 129 |
|
| 130 |
assert(ret); |
| 131 |
assert(name); |
| 132 |
|
| 133 |
LIST_FOREACH(socket_bind_items, i, items) { |
| 134 |
r = sd_json_variant_append_arraybo( |
| 135 |
&v, |
| 136 |
SD_JSON_BUILD_PAIR_INTEGER("family", i->address_family), |
| 137 |
SD_JSON_BUILD_PAIR_STRING("protocol", ip_protocol_to_name(i->ip_protocol)), |
| 138 |
SD_JSON_BUILD_PAIR_UNSIGNED("numberOfPorts", i->nr_ports), |
| 139 |
SD_JSON_BUILD_PAIR_UNSIGNED("minimumPort", i->port_min)); |
| 140 |
if (r < 0) |
| 141 |
return r; |
| 142 |
} |
| 143 |
|
| 144 |
*ret = TAKE_PTR(v); |
| 145 |
return 0; |
| 146 |
} |
| 147 |
|
| 148 |
static int nft_set_build_json(sd_json_variant **ret, const char *name, void *userdata) { |
| 149 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 150 |
NFTSetContext *c = ASSERT_PTR(userdata); |
| 151 |
int r; |
| 152 |
|
| 153 |
assert(ret); |
| 154 |
assert(name); |
| 155 |
|
| 156 |
FOREACH_ARRAY(nft_set, c->sets, c->n_sets) { |
| 157 |
r = sd_json_variant_append_arraybo( |
| 158 |
&v, |
| 159 |
SD_JSON_BUILD_PAIR_STRING("source", nft_set_source_to_string(nft_set->source)), |
| 160 |
SD_JSON_BUILD_PAIR_STRING("protocol", nfproto_to_string(nft_set->nfproto)), |
| 161 |
SD_JSON_BUILD_PAIR_STRING("table", nft_set->table), |
| 162 |
SD_JSON_BUILD_PAIR_STRING("set", nft_set->set)); |
| 163 |
if (r < 0) |
| 164 |
return r; |
| 165 |
} |
| 166 |
|
| 167 |
*ret = TAKE_PTR(v); |
| 168 |
return 0; |
| 169 |
} |
| 170 |
|
| 171 |
static int bpf_program_build_json(sd_json_variant **ret, const char *name, void *userdata) { |
| 172 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 173 |
CGroupBPFForeignProgram *programs = userdata; |
| 174 |
int r; |
| 175 |
|
| 176 |
assert(ret); |
| 177 |
assert(name); |
| 178 |
|
| 179 |
LIST_FOREACH(programs, p, programs) { |
| 180 |
r = sd_json_variant_append_arraybo( |
| 181 |
&v, |
| 182 |
SD_JSON_BUILD_PAIR_STRING("attachType", bpf_cgroup_attach_type_to_string(p->attach_type)), |
| 183 |
SD_JSON_BUILD_PAIR_STRING("path", p->bpffs_path)); |
| 184 |
if (r < 0) |
| 185 |
return r; |
| 186 |
} |
| 187 |
|
| 188 |
*ret = TAKE_PTR(v); |
| 189 |
return 0; |
| 190 |
} |
| 191 |
|
| 192 |
static int device_allow_build_json(sd_json_variant **ret, const char *name, void *userdata) { |
| 193 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 194 |
CGroupDeviceAllow *allow = userdata; |
| 195 |
int r; |
| 196 |
|
| 197 |
assert(ret); |
| 198 |
|
| 199 |
LIST_FOREACH(device_allow, a, allow) { |
| 200 |
r = sd_json_variant_append_arraybo( |
| 201 |
&v, |
| 202 |
SD_JSON_BUILD_PAIR_STRING("path", a->path), |
| 203 |
SD_JSON_BUILD_PAIR_STRING("permissions", cgroup_device_permissions_to_string(a->permissions))); |
| 204 |
if (r < 0) |
| 205 |
return r; |
| 206 |
} |
| 207 |
|
| 208 |
*ret = TAKE_PTR(v); |
| 209 |
return 0; |
| 210 |
} |
| 211 |
|
| 212 |
static int controllers_build_json(sd_json_variant **ret, const char *name, void *userdata) { |
| 213 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 214 |
CGroupMask *mask = ASSERT_PTR(userdata); |
| 215 |
int r; |
| 216 |
|
| 217 |
assert(ret); |
| 218 |
assert(name); |
| 219 |
|
| 220 |
for (CGroupController ctrl = 0; ctrl < _CGROUP_CONTROLLER_MAX; ctrl++) { |
| 221 |
if (!FLAGS_SET(*mask, CGROUP_CONTROLLER_TO_MASK(ctrl))) |
| 222 |
continue; |
| 223 |
|
| 224 |
r = sd_json_variant_append_arrayb(&v, JSON_BUILD_STRING_UNDERSCORIFY(cgroup_controller_to_string(ctrl))); |
| 225 |
if (r < 0) |
| 226 |
return r; |
| 227 |
} |
| 228 |
|
| 229 |
*ret = TAKE_PTR(v); |
| 230 |
return 0; |
| 231 |
} |
| 232 |
|
| 233 |
int unit_cgroup_context_build_json(sd_json_variant **ret, const char *name, void *userdata) { |
| 234 |
Unit *u = ASSERT_PTR(userdata); |
| 235 |
|
| 236 |
assert(ret); |
| 237 |
assert(name); |
| 238 |
|
| 239 |
CGroupContext *c = unit_get_cgroup_context(u); |
| 240 |
if (!c) { |
| 241 |
*ret = NULL; |
| 242 |
return 0; |
| 243 |
} |
| 244 |
|
| 245 |
/* The main principle behind context/runtime split is the following: |
| 246 |
* If it make sense to place a property into a config/unit file it belongs to Context. |
| 247 |
* Otherwise it's a 'Runtime'. */ |
| 248 |
|
| 249 |
return sd_json_buildo( |
| 250 |
ret, |
| 251 |
|
| 252 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("Slice", unit_slice_name(u)), |
| 253 |
|
| 254 |
/* CPU Control */ |
| 255 |
JSON_BUILD_PAIR_UNSIGNED_NOT_EQUAL("CPUWeight", c->cpu_weight, CGROUP_WEIGHT_INVALID), |
| 256 |
JSON_BUILD_PAIR_UNSIGNED_NOT_EQUAL("StartupCPUWeight", c->startup_cpu_weight, CGROUP_WEIGHT_INVALID), |
| 257 |
JSON_BUILD_PAIR_FINITE_USEC("CPUQuotaPerSecUSec", c->cpu_quota_per_sec_usec), |
| 258 |
JSON_BUILD_PAIR_FINITE_USEC("CPUQuotaPeriodUSec", c->cpu_quota_period_usec), |
| 259 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("AllowedCPUs", cpuset_build_json, &c->cpuset_cpus), |
| 260 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("StartupAllowedCPUs", cpuset_build_json, &c->startup_cpuset_cpus), |
| 261 |
JSON_BUILD_PAIR_ENUM("CPUSetPartition", cpuset_partition_to_string(c->cpuset_partition)), |
| 262 |
|
| 263 |
/* Memory Accounting and Control */ |
| 264 |
SD_JSON_BUILD_PAIR_BOOLEAN("MemoryAccounting", c->memory_accounting), |
| 265 |
JSON_BUILD_PAIR_UNSIGNED_NOT_EQUAL("MemoryMin", c->memory_min, CGROUP_LIMIT_MIN), |
| 266 |
JSON_BUILD_PAIR_UNSIGNED_NOT_EQUAL("MemoryLow", c->memory_low, CGROUP_LIMIT_MIN), |
| 267 |
JSON_BUILD_PAIR_CONDITION_UNSIGNED(c->startup_memory_low_set, "StartupMemoryLow", c->startup_memory_low), |
| 268 |
JSON_BUILD_PAIR_UNSIGNED_NOT_EQUAL("MemoryHigh", c->memory_high, CGROUP_LIMIT_MAX), |
| 269 |
JSON_BUILD_PAIR_CONDITION_UNSIGNED(c->startup_memory_high_set, "StartupMemoryHigh", c->startup_memory_high), |
| 270 |
JSON_BUILD_PAIR_UNSIGNED_NOT_EQUAL("MemoryMax", c->memory_max, CGROUP_LIMIT_MAX), |
| 271 |
JSON_BUILD_PAIR_CONDITION_UNSIGNED(c->startup_memory_max_set, "StartupMemoryMax", c->startup_memory_max), |
| 272 |
JSON_BUILD_PAIR_UNSIGNED_NOT_EQUAL("MemorySwapMax", c->memory_swap_max, CGROUP_LIMIT_MAX), |
| 273 |
JSON_BUILD_PAIR_CONDITION_UNSIGNED(c->startup_memory_swap_max_set, "StartupMemorySwapMax", c->startup_memory_swap_max), |
| 274 |
JSON_BUILD_PAIR_UNSIGNED_NOT_EQUAL("MemoryZSwapMax", c->memory_zswap_max, CGROUP_LIMIT_MAX), |
| 275 |
JSON_BUILD_PAIR_CONDITION_UNSIGNED(c->startup_memory_zswap_max_set, "StartupMemoryZSwapMax", c->startup_memory_zswap_max), |
| 276 |
SD_JSON_BUILD_PAIR_BOOLEAN("MemoryZSwapWriteback", c->memory_zswap_writeback), |
| 277 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("AllowedMemoryNodes", cpuset_build_json, &c->cpuset_mems), |
| 278 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("StartupAllowedMemoryNodes", cpuset_build_json, &c->startup_cpuset_mems), |
| 279 |
|
| 280 |
/* Process Accounting and Control */ |
| 281 |
SD_JSON_BUILD_PAIR_BOOLEAN("TasksAccounting", c->tasks_accounting), |
| 282 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("TasksMax", tasks_max_build_json, &c->tasks_max), |
| 283 |
|
| 284 |
/* IO Accounting and Control */ |
| 285 |
SD_JSON_BUILD_PAIR_BOOLEAN("IOAccounting", c->io_accounting), |
| 286 |
JSON_BUILD_PAIR_UNSIGNED_NOT_EQUAL("IOWeight", c->io_weight, CGROUP_WEIGHT_INVALID), |
| 287 |
JSON_BUILD_PAIR_UNSIGNED_NOT_EQUAL("StartupIOWeight", c->startup_io_weight, CGROUP_WEIGHT_INVALID), |
| 288 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("IODeviceWeight", io_device_weights_build_json, c->io_device_weights), |
| 289 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("IOReadBandwidthMax", io_device_limits_build_json, c->io_device_limits), |
| 290 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("IOWriteBandwidthMax", io_device_limits_build_json, c->io_device_limits), |
| 291 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("IOReadIOPSMax", io_device_limits_build_json, c->io_device_limits), |
| 292 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("IOWriteIOPSMax", io_device_limits_build_json, c->io_device_limits), |
| 293 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("IODeviceLatencyTargetUSec", io_device_latencies_build_json, c->io_device_latencies), |
| 294 |
|
| 295 |
/* Network Accounting and Control */ |
| 296 |
SD_JSON_BUILD_PAIR_BOOLEAN("IPAccounting", c->ip_accounting), |
| 297 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("IPAddressAllow", ip_address_access_build_json, c->ip_address_allow), |
| 298 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("IPAddressDeny", ip_address_access_build_json, c->ip_address_deny), |
| 299 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("SocketBindAllow", socket_bind_build_json, c->socket_bind_allow), |
| 300 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("SocketBindDeny", socket_bind_build_json, c->socket_bind_deny), |
| 301 |
SD_JSON_BUILD_PAIR_CONDITION(!set_isempty(c->restrict_network_interfaces), "RestrictNetworkInterfaces", |
| 302 |
SD_JSON_BUILD_OBJECT( |
| 303 |
SD_JSON_BUILD_PAIR_BOOLEAN("isAllowList", c->restrict_network_interfaces_is_allow_list), |
| 304 |
JSON_BUILD_PAIR_STRING_SET("interfaces", c->restrict_network_interfaces))), |
| 305 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("BindNetworkInterface", c->bind_network_interface), |
| 306 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("NFTSet", nft_set_build_json, &c->nft_set_context), |
| 307 |
|
| 308 |
/* BPF programs */ |
| 309 |
JSON_BUILD_PAIR_STRV_NON_EMPTY("IPIngressFilterPath", c->ip_filters_ingress), |
| 310 |
JSON_BUILD_PAIR_STRV_NON_EMPTY("IPEgressFilterPath", c->ip_filters_egress), |
| 311 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("BPFProgram", bpf_program_build_json, c->bpf_foreign_programs), |
| 312 |
|
| 313 |
/* Device Access */ |
| 314 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("DeviceAllow", device_allow_build_json, c->device_allow), |
| 315 |
JSON_BUILD_PAIR_ENUM("DevicePolicy", cgroup_device_policy_to_string(c->device_policy)), |
| 316 |
|
| 317 |
/* Control Group Management */ |
| 318 |
SD_JSON_BUILD_PAIR_BOOLEAN("Delegate", c->delegate), |
| 319 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("DelegateSubgroup", c->delegate_subgroup), |
| 320 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("DelegateControllers", controllers_build_json, &c->delegate_controllers), |
| 321 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("DisableControllers", controllers_build_json, &c->disable_controllers), |
| 322 |
|
| 323 |
/* Memory Pressure Control */ |
| 324 |
JSON_BUILD_PAIR_ENUM("ManagedOOMSwap", managed_oom_mode_to_string(c->moom_swap)), |
| 325 |
JSON_BUILD_PAIR_ENUM("ManagedOOMMemoryPressure", managed_oom_mode_to_string(c->moom_mem_pressure)), |
| 326 |
JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("ManagedOOMMemoryPressureLimit", c->moom_mem_pressure_limit), |
| 327 |
JSON_BUILD_PAIR_FINITE_USEC("ManagedOOMMemoryPressureDurationUSec", c->moom_mem_pressure_duration_usec), |
| 328 |
JSON_BUILD_PAIR_ENUM("ManagedOOMPreference", managed_oom_preference_to_string(c->moom_preference)), |
| 329 |
JSON_BUILD_PAIR_STRV_NON_EMPTY("OOMRules", c->moom_rules), |
| 330 |
JSON_BUILD_PAIR_ENUM("MemoryPressureWatch", cgroup_pressure_watch_to_string(c->pressure[PRESSURE_MEMORY].watch)), |
| 331 |
JSON_BUILD_PAIR_FINITE_USEC("MemoryPressureThresholdUSec", c->pressure[PRESSURE_MEMORY].threshold_usec), |
| 332 |
JSON_BUILD_PAIR_ENUM("CPUPressureWatch", cgroup_pressure_watch_to_string(c->pressure[PRESSURE_CPU].watch)), |
| 333 |
JSON_BUILD_PAIR_FINITE_USEC("CPUPressureThresholdUSec", c->pressure[PRESSURE_CPU].threshold_usec), |
| 334 |
JSON_BUILD_PAIR_ENUM("IOPressureWatch", cgroup_pressure_watch_to_string(c->pressure[PRESSURE_IO].watch)), |
| 335 |
JSON_BUILD_PAIR_FINITE_USEC("IOPressureThresholdUSec", c->pressure[PRESSURE_IO].threshold_usec), |
| 336 |
|
| 337 |
/* Others */ |
| 338 |
SD_JSON_BUILD_PAIR_BOOLEAN("CoredumpReceive", c->coredump_receive)); |
| 339 |
} |
| 340 |
|
| 341 |
static int memory_accounting_metric_build_json(sd_json_variant **ret, const char *name, void *userdata) { |
| 342 |
Unit *u = ASSERT_PTR(userdata); |
| 343 |
CGroupMemoryAccountingMetric metric; |
| 344 |
uint64_t value; |
| 345 |
int r; |
| 346 |
|
| 347 |
assert(ret); |
| 348 |
assert(name); |
| 349 |
|
| 350 |
metric = cgroup_memory_accounting_metric_from_string(name); |
| 351 |
assert(metric >= 0); |
| 352 |
|
| 353 |
r = unit_get_memory_accounting(u, metric, &value); |
| 354 |
if (r == -ENODATA) |
| 355 |
goto empty; |
| 356 |
if (r < 0) |
| 357 |
return log_debug_errno(r, "Failed to get value for '%s': %m", name); |
| 358 |
|
| 359 |
if (value == UINT64_MAX) |
| 360 |
goto empty; |
| 361 |
|
| 362 |
return sd_json_variant_new_unsigned(ret, value); |
| 363 |
|
| 364 |
empty: |
| 365 |
*ret = NULL; |
| 366 |
return 0; |
| 367 |
} |
| 368 |
|
| 369 |
static int memory_available_build_json(sd_json_variant **ret, const char *name, void *userdata) { |
| 370 |
Unit *u = ASSERT_PTR(userdata); |
| 371 |
uint64_t value; |
| 372 |
int r; |
| 373 |
|
| 374 |
assert(ret); |
| 375 |
assert(name); |
| 376 |
|
| 377 |
r = unit_get_memory_available(u, &value); |
| 378 |
if (r == -ENODATA) |
| 379 |
goto empty; |
| 380 |
if (r < 0) |
| 381 |
return log_debug_errno(r, "Failed to get value of available memory: %m"); |
| 382 |
|
| 383 |
if (value == UINT64_MAX) |
| 384 |
goto empty; |
| 385 |
|
| 386 |
return sd_json_variant_new_unsigned(ret, value); |
| 387 |
|
| 388 |
empty: |
| 389 |
*ret = NULL; |
| 390 |
return 0; |
| 391 |
} |
| 392 |
|
| 393 |
static int effective_limit_build_json(sd_json_variant **ret, const char *name, void *userdata) { |
| 394 |
Unit *u = ASSERT_PTR(userdata); |
| 395 |
CGroupLimitType type; |
| 396 |
uint64_t value; |
| 397 |
int r; |
| 398 |
|
| 399 |
assert(ret); |
| 400 |
assert(name); |
| 401 |
|
| 402 |
type = cgroup_effective_limit_type_from_string(name); |
| 403 |
assert(type >= 0); |
| 404 |
|
| 405 |
r = unit_get_effective_limit(u, type, &value); |
| 406 |
if (r < 0) |
| 407 |
return log_debug_errno(r, "Failed to get value for '%s': %m", name); |
| 408 |
|
| 409 |
if (value == UINT64_MAX) { |
| 410 |
*ret = NULL; |
| 411 |
return 0; |
| 412 |
} |
| 413 |
|
| 414 |
return sd_json_variant_new_unsigned(ret, value); |
| 415 |
} |
| 416 |
|
| 417 |
static int cpu_usage_build_json(sd_json_variant **ret, const char *name, void *userdata) { |
| 418 |
Unit *u = ASSERT_PTR(userdata); |
| 419 |
nsec_t ns; |
| 420 |
int r; |
| 421 |
|
| 422 |
assert(ret); |
| 423 |
assert(name); |
| 424 |
|
| 425 |
r = unit_get_cpu_usage(u, &ns); |
| 426 |
if (r == -ENODATA) |
| 427 |
goto empty; |
| 428 |
if (r < 0) |
| 429 |
return log_debug_errno(r, "Failed to get cpu usage: %m"); |
| 430 |
|
| 431 |
if (ns == NSEC_INFINITY) |
| 432 |
goto empty; |
| 433 |
|
| 434 |
return sd_json_variant_new_unsigned(ret, ns); |
| 435 |
|
| 436 |
empty: |
| 437 |
*ret = NULL; |
| 438 |
return 0; |
| 439 |
} |
| 440 |
|
| 441 |
static int effective_cpuset_build_json(sd_json_variant **ret, const char *name, void *userdata, const char *cpuset_name) { |
| 442 |
Unit *u = ASSERT_PTR(userdata); |
| 443 |
_cleanup_(cpu_set_done) CPUSet cpus = {}; |
| 444 |
int r; |
| 445 |
|
| 446 |
assert(ret); |
| 447 |
assert(name); |
| 448 |
assert(cpuset_name); |
| 449 |
|
| 450 |
r = unit_get_cpuset(u, &cpus, cpuset_name); |
| 451 |
if (r == -ENODATA) { |
| 452 |
*ret = NULL; |
| 453 |
return 0; |
| 454 |
} |
| 455 |
if (r < 0) |
| 456 |
return log_debug_errno(r, "Failed to get cpu set '%s': %m", cpuset_name); |
| 457 |
|
| 458 |
return cpuset_build_json(ret, name, &cpus); |
| 459 |
} |
| 460 |
|
| 461 |
static inline int effective_cpus_build_json(sd_json_variant **ret, const char *name, void *userdata) { |
| 462 |
return effective_cpuset_build_json(ret, name, userdata, "cpuset.cpus.effective"); |
| 463 |
} |
| 464 |
|
| 465 |
static inline int effective_mems_build_json(sd_json_variant **ret, const char *name, void *userdata) { |
| 466 |
return effective_cpuset_build_json(ret, name, userdata, "cpuset.mems.effective"); |
| 467 |
} |
| 468 |
|
| 469 |
static int tasks_current_build_json(sd_json_variant **ret, const char *name, void *userdata) { |
| 470 |
Unit *u = ASSERT_PTR(userdata); |
| 471 |
uint64_t cn; |
| 472 |
int r; |
| 473 |
|
| 474 |
assert(ret); |
| 475 |
assert(name); |
| 476 |
|
| 477 |
r = unit_get_tasks_current(u, &cn); |
| 478 |
if (r == -ENODATA) |
| 479 |
goto empty; |
| 480 |
if (r < 0) |
| 481 |
return log_debug_errno(r, "Failed to get count of current tasks: %m"); |
| 482 |
|
| 483 |
if (cn == UINT64_MAX) |
| 484 |
goto empty; |
| 485 |
|
| 486 |
return sd_json_variant_new_unsigned(ret, cn); |
| 487 |
|
| 488 |
empty: |
| 489 |
*ret = NULL; |
| 490 |
return 0; |
| 491 |
} |
| 492 |
|
| 493 |
static int get_ip_counter_build_json(sd_json_variant **ret, const char *name, void *userdata) { |
| 494 |
Unit *u = ASSERT_PTR(userdata); |
| 495 |
CGroupIPAccountingMetric metric; |
| 496 |
uint64_t value; |
| 497 |
int r; |
| 498 |
|
| 499 |
assert(ret); |
| 500 |
assert(name); |
| 501 |
|
| 502 |
metric = cgroup_ip_accounting_metric_from_string(name); |
| 503 |
assert(metric >= 0); |
| 504 |
|
| 505 |
r = unit_get_ip_accounting(u, metric, &value); |
| 506 |
if (r == -ENODATA) |
| 507 |
goto empty; |
| 508 |
if (r < 0) |
| 509 |
return log_debug_errno(r, "Failed to get value for '%s': %m", name); |
| 510 |
|
| 511 |
if (value == UINT64_MAX) |
| 512 |
goto empty; |
| 513 |
|
| 514 |
return sd_json_variant_new_unsigned(ret, value); |
| 515 |
|
| 516 |
empty: |
| 517 |
*ret = NULL; |
| 518 |
return 0; |
| 519 |
} |
| 520 |
|
| 521 |
static int get_io_counter_build_json(sd_json_variant **ret, const char *name, void *userdata) { |
| 522 |
Unit *u = ASSERT_PTR(userdata); |
| 523 |
CGroupIOAccountingMetric metric; |
| 524 |
uint64_t value; |
| 525 |
int r; |
| 526 |
|
| 527 |
assert(ret); |
| 528 |
assert(name); |
| 529 |
|
| 530 |
metric = cgroup_io_accounting_metric_from_string(name); |
| 531 |
assert(metric >= 0); |
| 532 |
|
| 533 |
r = unit_get_io_accounting(u, metric, &value); |
| 534 |
if (r == -ENODATA) |
| 535 |
goto empty; |
| 536 |
if (r < 0) |
| 537 |
return log_debug_errno(r, "Failed to get value for '%s': %m", name); |
| 538 |
|
| 539 |
if (value == UINT64_MAX) |
| 540 |
goto empty; |
| 541 |
|
| 542 |
return sd_json_variant_new_unsigned(ret, value); |
| 543 |
|
| 544 |
empty: |
| 545 |
*ret = NULL; |
| 546 |
return 0; |
| 547 |
} |
| 548 |
|
| 549 |
int unit_cgroup_runtime_build_json(sd_json_variant **ret, const char *name, void *userdata) { |
| 550 |
Unit *u = ASSERT_PTR(userdata); |
| 551 |
|
| 552 |
assert(ret); |
| 553 |
assert(name); |
| 554 |
|
| 555 |
CGroupRuntime *crt = unit_get_cgroup_runtime(u); |
| 556 |
if (!crt) { |
| 557 |
*ret = NULL; |
| 558 |
return 0; |
| 559 |
} |
| 560 |
|
| 561 |
return sd_json_buildo( |
| 562 |
ret, |
| 563 |
|
| 564 |
/* ID */ |
| 565 |
JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("ID", crt->cgroup_id), |
| 566 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("Path", crt->cgroup_path ? empty_to_root(crt->cgroup_path) : NULL), |
| 567 |
|
| 568 |
/* Memory */ |
| 569 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("MemoryCurrent", memory_accounting_metric_build_json, u), |
| 570 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("MemoryPeak", memory_accounting_metric_build_json, u), |
| 571 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("MemorySwapCurrent", memory_accounting_metric_build_json, u), |
| 572 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("MemorySwapPeak", memory_accounting_metric_build_json, u), |
| 573 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("MemoryZSwapCurrent", memory_accounting_metric_build_json, u), |
| 574 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("MemoryAvailable", memory_available_build_json, u), |
| 575 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("EffectiveMemoryMax", effective_limit_build_json, u), |
| 576 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("EffectiveMemoryHigh", effective_limit_build_json, u), |
| 577 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("EffectiveMemoryNodes", effective_mems_build_json, u), |
| 578 |
|
| 579 |
/* CPU */ |
| 580 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("CPUUsageNSec", cpu_usage_build_json, u), |
| 581 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("EffectiveCPUs", effective_cpus_build_json, u), |
| 582 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("TasksCurrent", tasks_current_build_json, u), |
| 583 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("EffectiveTasksMax", effective_limit_build_json, u), |
| 584 |
|
| 585 |
/* IP */ |
| 586 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("IPIngressBytes", get_ip_counter_build_json, u), |
| 587 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("IPIngressPackets", get_ip_counter_build_json, u), |
| 588 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("IPEgressBytes", get_ip_counter_build_json, u), |
| 589 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("IPEgressPackets", get_ip_counter_build_json, u), |
| 590 |
|
| 591 |
/* IO */ |
| 592 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("IOReadBytes", get_io_counter_build_json, u), |
| 593 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("IOReadOperations", get_io_counter_build_json, u), |
| 594 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("IOWriteBytes", get_io_counter_build_json, u), |
| 595 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("IOWriteOperations", get_io_counter_build_json, u), |
| 596 |
|
| 597 |
/* OOM */ |
| 598 |
SD_JSON_BUILD_PAIR_UNSIGNED("OOMKills", crt->oom_kill_last), |
| 599 |
SD_JSON_BUILD_PAIR_UNSIGNED("ManagedOOMKills", crt->managed_oom_kill_last)); |
| 600 |
} |