| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include "basic-forward.h" |
| 5 |
#include "cgroup-util.h" |
| 6 |
#include "stdio-util.h" /* IWYU pragma: keep */ |
| 7 |
|
| 8 |
assert_cc(sizeof(pid_t) == sizeof(int32_t)); |
| 9 |
#define PID_PRI PRIi32 |
| 10 |
#define PID_FMT "%" PID_PRI |
| 11 |
|
| 12 |
assert_cc(sizeof(uid_t) == sizeof(uint32_t)); |
| 13 |
#define UID_FMT "%" PRIu32 |
| 14 |
|
| 15 |
assert_cc(sizeof(gid_t) == sizeof(uint32_t)); |
| 16 |
#define GID_FMT "%" PRIu32 |
| 17 |
|
| 18 |
/* Note: the lifetime of the compound literal is the immediately surrounding block, |
| 19 |
* see C11 §6.5.2.5, and |
| 20 |
* https://stackoverflow.com/questions/34880638/compound-literal-lifetime-and-if-blocks */ |
| 21 |
#define FORMAT_UID(uid) \ |
| 22 |
snprintf_ok((char[DECIMAL_STR_MAX(uid_t)]){}, DECIMAL_STR_MAX(uid_t), UID_FMT, uid) |
| 23 |
#define FORMAT_GID(gid) \ |
| 24 |
snprintf_ok((char[DECIMAL_STR_MAX(gid_t)]){}, DECIMAL_STR_MAX(gid_t), GID_FMT, gid) |
| 25 |
|
| 26 |
#if SIZEOF_TIME_T == 8 |
| 27 |
# define PRI_TIME PRIi64 |
| 28 |
#elif SIZEOF_TIME_T == 4 |
| 29 |
# define PRI_TIME "li" |
| 30 |
#else |
| 31 |
# error Unknown time_t size |
| 32 |
#endif |
| 33 |
|
| 34 |
#if SIZEOF_TIMEX_MEMBER == 8 |
| 35 |
# define PRI_TIMEX PRIi64 |
| 36 |
#elif SIZEOF_TIMEX_MEMBER == 4 |
| 37 |
# define PRI_TIMEX "li" |
| 38 |
#else |
| 39 |
# error Unknown timex member size |
| 40 |
#endif |
| 41 |
|
| 42 |
#ifdef __GLIBC__ |
| 43 |
# if SIZEOF_RLIM_T == 8 |
| 44 |
# define RLIM_FMT "%" PRIu64 |
| 45 |
# elif SIZEOF_RLIM_T == 4 |
| 46 |
# define RLIM_FMT "%" PRIu32 |
| 47 |
# else |
| 48 |
# error Unknown rlim_t size |
| 49 |
# endif |
| 50 |
#else |
| 51 |
/* Assume musl, and it unconditionally uses unsigned long long. */ |
| 52 |
assert_cc(SIZEOF_RLIM_T == 8); |
| 53 |
# define RLIM_FMT "%llu" |
| 54 |
#endif |
| 55 |
|
| 56 |
#if SIZEOF_DEV_T == 8 |
| 57 |
# define DEV_FMT "%" PRIu64 |
| 58 |
#elif SIZEOF_DEV_T == 4 |
| 59 |
# define DEV_FMT "%" PRIu32 |
| 60 |
#else |
| 61 |
# error Unknown dev_t size |
| 62 |
#endif |
| 63 |
|
| 64 |
#if SIZEOF_INO_T == 8 |
| 65 |
# define INO_FMT "%" PRIu64 |
| 66 |
#elif SIZEOF_INO_T == 4 |
| 67 |
# define INO_FMT "%" PRIu32 |
| 68 |
#else |
| 69 |
# error Unknown ino_t size |
| 70 |
#endif |
| 71 |
|
| 72 |
typedef enum { |
| 73 |
FORMAT_BYTES_USE_IEC = 1 << 0, /* use base 1024 rather than 1000 */ |
| 74 |
FORMAT_BYTES_BELOW_POINT = 1 << 1, /* show one digit after the point, if non-zero */ |
| 75 |
FORMAT_BYTES_ALWAYS_POINT = 1 << 2, /* show one digit after the point, always */ |
| 76 |
FORMAT_BYTES_TRAILING_B = 1 << 3, /* suffix the expression with a "B" for "bytes" */ |
| 77 |
} FormatBytesFlag; |
| 78 |
|
| 79 |
#define FORMAT_BYTES_MAX 16U |
| 80 |
|
| 81 |
char* format_bytes_full(char *buf, size_t l, uint64_t t, FormatBytesFlag flag) _warn_unused_result_; |
| 82 |
|
| 83 |
_warn_unused_result_ |
| 84 |
static inline char* format_bytes(char *buf, size_t l, uint64_t t) { |
| 85 |
return format_bytes_full(buf, l, t, FORMAT_BYTES_USE_IEC | FORMAT_BYTES_BELOW_POINT | FORMAT_BYTES_TRAILING_B); |
| 86 |
} |
| 87 |
|
| 88 |
/* Note: the lifetime of the compound literal is the immediately surrounding block, |
| 89 |
* see C11 §6.5.2.5, and |
| 90 |
* https://stackoverflow.com/questions/34880638/compound-literal-lifetime-and-if-blocks */ |
| 91 |
#define FORMAT_BYTES(t) format_bytes((char[FORMAT_BYTES_MAX]){}, FORMAT_BYTES_MAX, t) |
| 92 |
#define FORMAT_BYTES_FULL(t, flags) format_bytes_full((char[FORMAT_BYTES_MAX]){}, FORMAT_BYTES_MAX, t, flags) |
| 93 |
#define FORMAT_BYTES_WITH_POINT(t) format_bytes_full((char[FORMAT_BYTES_MAX]){}, FORMAT_BYTES_MAX, t, FORMAT_BYTES_USE_IEC|FORMAT_BYTES_ALWAYS_POINT|FORMAT_BYTES_TRAILING_B) |
| 94 |
|
| 95 |
#define FORMAT_BYTES_CGROUP_PROTECTION(t) (t == CGROUP_LIMIT_MAX ? "infinity" : FORMAT_BYTES(t)) |