1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
8 #include "cgroup-util.h"
11 assert_cc(sizeof(pid_t
) == sizeof(int32_t));
12 #define PID_PRI PRIi32
13 #define PID_FMT "%" PID_PRI
15 assert_cc(sizeof(uid_t
) == sizeof(uint32_t));
16 #define UID_FMT "%" PRIu32
18 assert_cc(sizeof(gid_t
) == sizeof(uint32_t));
19 #define GID_FMT "%" PRIu32
21 #if SIZEOF_TIME_T == 8
22 # define PRI_TIME PRIi64
23 #elif SIZEOF_TIME_T == 4
24 # define PRI_TIME "li"
26 # error Unknown time_t size
29 #if SIZEOF_TIMEX_MEMBER == 8
30 # define PRI_TIMEX PRIi64
31 #elif SIZEOF_TIMEX_MEMBER == 4
32 # define PRI_TIMEX "li"
34 # error Unknown timex member size
37 #if SIZEOF_RLIM_T == 8
38 # define RLIM_FMT "%" PRIu64
39 #elif SIZEOF_RLIM_T == 4
40 # define RLIM_FMT "%" PRIu32
42 # error Unknown rlim_t size
46 # define DEV_FMT "%" PRIu64
47 #elif SIZEOF_DEV_T == 4
48 # define DEV_FMT "%" PRIu32
50 # error Unknown dev_t size
54 # define INO_FMT "%" PRIu64
55 #elif SIZEOF_INO_T == 4
56 # define INO_FMT "%" PRIu32
58 # error Unknown ino_t size
62 FORMAT_IFNAME_IFINDEX
= 1 << 0,
63 FORMAT_IFNAME_IFINDEX_WITH_PERCENT
= (1 << 1) | FORMAT_IFNAME_IFINDEX
,
66 int format_ifname_full(int ifindex
, FormatIfnameFlag flag
, char buf
[static IF_NAMESIZE
]);
67 int format_ifname_full_alloc(int ifindex
, FormatIfnameFlag flag
, char **ret
);
69 static inline int format_ifname(int ifindex
, char buf
[static IF_NAMESIZE
]) {
70 return format_ifname_full(ifindex
, 0, buf
);
72 static inline int format_ifname_alloc(int ifindex
, char **ret
) {
73 return format_ifname_full_alloc(ifindex
, 0, ret
);
76 static inline char *_format_ifname_full(int ifindex
, FormatIfnameFlag flag
, char buf
[static IF_NAMESIZE
]) {
77 (void) format_ifname_full(ifindex
, flag
, buf
);
81 #define FORMAT_IFNAME_FULL(index, flag) _format_ifname_full(index, flag, (char[IF_NAMESIZE]){})
82 #define FORMAT_IFNAME(index) _format_ifname_full(index, 0, (char[IF_NAMESIZE]){})
85 FORMAT_BYTES_USE_IEC
= 1 << 0,
86 FORMAT_BYTES_BELOW_POINT
= 1 << 1,
87 FORMAT_BYTES_TRAILING_B
= 1 << 2,
90 #define FORMAT_BYTES_MAX 16U
92 char *format_bytes_full(char *buf
, size_t l
, uint64_t t
, FormatBytesFlag flag
) _warn_unused_result_
;
95 static inline char *format_bytes(char *buf
, size_t l
, uint64_t t
) {
96 return format_bytes_full(buf
, l
, t
, FORMAT_BYTES_USE_IEC
| FORMAT_BYTES_BELOW_POINT
| FORMAT_BYTES_TRAILING_B
);
99 /* Note: the lifetime of the compound literal is the immediately surrounding block,
100 * see C11 ยง6.5.2.5, and
101 * https://stackoverflow.com/questions/34880638/compound-literal-lifetime-and-if-blocks */
102 #define FORMAT_BYTES(t) format_bytes((char[FORMAT_BYTES_MAX]){}, FORMAT_BYTES_MAX, t)
103 #define FORMAT_BYTES_FULL(t, flag) format_bytes_full((char[FORMAT_BYTES_MAX]){}, FORMAT_BYTES_MAX, t, flag)
105 #define FORMAT_BYTES_CGROUP_PROTECTION(t) (t == CGROUP_LIMIT_MAX ? "infinity" : FORMAT_BYTES(t))