1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <inttypes.h>
5 #include <net/if.h>
6 #include <stdbool.h>
7
8 #include "cgroup-util.h"
9 #include "macro.h"
10
11 assert_cc(sizeof(pid_t) == sizeof(int32_t));
12 #define PID_PRI PRIi32
13 #define PID_FMT "%" PID_PRI
14
15 assert_cc(sizeof(uid_t) == sizeof(uint32_t));
16 #define UID_FMT "%" PRIu32
17
18 assert_cc(sizeof(gid_t) == sizeof(uint32_t));
19 #define GID_FMT "%" PRIu32
20
21 #if SIZEOF_TIME_T == 8
22 # define PRI_TIME PRIi64
23 #elif SIZEOF_TIME_T == 4
24 # define PRI_TIME "li"
25 #else
26 # error Unknown time_t size
27 #endif
28
29 #if SIZEOF_TIMEX_MEMBER == 8
30 # define PRI_TIMEX PRIi64
31 #elif SIZEOF_TIMEX_MEMBER == 4
32 # define PRI_TIMEX "li"
33 #else
34 # error Unknown timex member size
35 #endif
36
37 #if SIZEOF_RLIM_T == 8
38 # define RLIM_FMT "%" PRIu64
39 #elif SIZEOF_RLIM_T == 4
40 # define RLIM_FMT "%" PRIu32
41 #else
42 # error Unknown rlim_t size
43 #endif
44
45 #if SIZEOF_DEV_T == 8
46 # define DEV_FMT "%" PRIu64
47 #elif SIZEOF_DEV_T == 4
48 # define DEV_FMT "%" PRIu32
49 #else
50 # error Unknown dev_t size
51 #endif
52
53 #if SIZEOF_INO_T == 8
54 # define INO_FMT "%" PRIu64
55 #elif SIZEOF_INO_T == 4
56 # define INO_FMT "%" PRIu32
57 #else
58 # error Unknown ino_t size
59 #endif
60
61 typedef enum {
62 FORMAT_IFNAME_IFINDEX = 1 << 0,
63 FORMAT_IFNAME_IFINDEX_WITH_PERCENT = (1 << 1) | FORMAT_IFNAME_IFINDEX,
64 } FormatIfnameFlag;
65
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);
68
format_ifname(int ifindex,char buf[static IF_NAMESIZE])69 static inline int format_ifname(int ifindex, char buf[static IF_NAMESIZE]) {
70 return format_ifname_full(ifindex, 0, buf);
71 }
format_ifname_alloc(int ifindex,char ** ret)72 static inline int format_ifname_alloc(int ifindex, char **ret) {
73 return format_ifname_full_alloc(ifindex, 0, ret);
74 }
75
_format_ifname_full(int ifindex,FormatIfnameFlag flag,char buf[static IF_NAMESIZE])76 static inline char *_format_ifname_full(int ifindex, FormatIfnameFlag flag, char buf[static IF_NAMESIZE]) {
77 (void) format_ifname_full(ifindex, flag, buf);
78 return buf;
79 }
80
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]){})
83
84 typedef enum {
85 FORMAT_BYTES_USE_IEC = 1 << 0,
86 FORMAT_BYTES_BELOW_POINT = 1 << 1,
87 FORMAT_BYTES_TRAILING_B = 1 << 2,
88 } FormatBytesFlag;
89
90 #define FORMAT_BYTES_MAX 16U
91
92 char *format_bytes_full(char *buf, size_t l, uint64_t t, FormatBytesFlag flag) _warn_unused_result_;
93
94 _warn_unused_result_
format_bytes(char * buf,size_t l,uint64_t t)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);
97 }
98
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)
104
105 #define FORMAT_BYTES_CGROUP_PROTECTION(t) (t == CGROUP_LIMIT_MAX ? "infinity" : FORMAT_BYTES(t))
106