Debian Codegraph @

src/shared/bus-get-properties.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-bus.h"
4
5 #include "bus-get-properties.h"
6 #include "bus-message-util.h"
7 #include "pidref.h"
8 #include "rlimit-util.h"
9 #include "string-util.h"
10
11 BUS_DEFINE_PROPERTY_GET_GLOBAL(bus_property_get_bool_false, "b", 0);
12 BUS_DEFINE_PROPERTY_GET_GLOBAL(bus_property_get_bool_true, "b", 1);
13 BUS_DEFINE_PROPERTY_GET_GLOBAL(bus_property_get_uint64_0, "t", UINT64_C(0));
14 BUS_DEFINE_PROPERTY_GET_GLOBAL(bus_property_get_uint64_max, "t", UINT64_MAX);
15
16 int bus_property_get_bool(
17 sd_bus *bus,
18 const char *path,
19 const char *interface,
20 const char *property,
21 sd_bus_message *reply,
22 void *userdata,
23 sd_bus_error *reterr_error) {
24
25 int b = *(bool*) userdata;
26
27 return sd_bus_message_append_basic(reply, 'b', &b);
28 }
29
30 int bus_property_set_bool(
31 sd_bus *bus,
32 const char *path,
33 const char *interface,
34 const char *property,
35 sd_bus_message *value,
36 void *userdata,
37 sd_bus_error *reterr_error) {
38
39 int b, r;
40
41 r = sd_bus_message_read(value, "b", &b);
42 if (r < 0)
43 return r;
44
45 *(bool*) userdata = b;
46 return 0;
47 }
48
49 int bus_property_get_tristate(
50 sd_bus *bus,
51 const char *path,
52 const char *interface,
53 const char *property,
54 sd_bus_message *reply,
55 void *userdata,
56 sd_bus_error *reterr_error) {
57
58 /* Defaults to false. */
59
60 int b = (*(int*) userdata) > 0;
61
62 return sd_bus_message_append_basic(reply, 'b', &b);
63 }
64
65 int bus_property_get_id128(
66 sd_bus *bus,
67 const char *path,
68 const char *interface,
69 const char *property,
70 sd_bus_message *reply,
71 void *userdata,
72 sd_bus_error *reterr_error) {
73
74 sd_id128_t *id = ASSERT_PTR(userdata);
75
76 if (sd_id128_is_null(*id)) /* Add an empty array if the ID is zero */
77 return sd_bus_message_append(reply, "ay", 0);
78
79 return sd_bus_message_append_array(reply, 'y', id->bytes, sizeof(sd_id128_t));
80 }
81
82 #if __SIZEOF_SIZE_T__ != 8
83 int bus_property_get_size(
84 sd_bus *bus,
85 const char *path,
86 const char *interface,
87 const char *property,
88 sd_bus_message *reply,
89 void *userdata,
90 sd_bus_error *reterr_error) {
91
92 uint64_t sz = *(size_t*) userdata;
93
94 return sd_bus_message_append_basic(reply, 't', &sz);
95 }
96 #endif
97
98 #if __SIZEOF_LONG__ != 8
99 int bus_property_get_long(
100 sd_bus *bus,
101 const char *path,
102 const char *interface,
103 const char *property,
104 sd_bus_message *reply,
105 void *userdata,
106 sd_bus_error *reterr_error) {
107
108 int64_t l = *(long*) userdata;
109
110 return sd_bus_message_append_basic(reply, 'x', &l);
111 }
112
113 int bus_property_get_ulong(
114 sd_bus *bus,
115 const char *path,
116 const char *interface,
117 const char *property,
118 sd_bus_message *reply,
119 void *userdata,
120 sd_bus_error *reterr_error) {
121
122 uint64_t ul = *(unsigned long*) userdata;
123
124 return sd_bus_message_append_basic(reply, 't', &ul);
125 }
126 #endif
127
128 int bus_property_get_rlimit(
129 sd_bus *bus,
130 const char *path,
131 const char *interface,
132 const char *property,
133 sd_bus_message *reply,
134 void *userdata,
135 sd_bus_error *reterr_error) {
136
137 const char *is_soft;
138 struct rlimit *rl;
139 uint64_t u;
140 rlim_t x;
141
142 assert(bus);
143 assert(reply);
144 assert(userdata);
145
146 is_soft = endswith(property, "Soft");
147
148 rl = *(struct rlimit**) userdata;
149 if (rl)
150 x = is_soft ? rl->rlim_cur : rl->rlim_max;
151 else {
152 struct rlimit buf = {};
153 const char *s, *p;
154 int z;
155
156 /* Chop off "Soft" suffix */
157 s = is_soft ? strndupa_safe(property, is_soft - property) : property;
158
159 /* Skip over any prefix, such as "Default" */
160 assert_se(p = strstrafter(s, "Limit"));
161
162 z = rlimit_from_string(p);
163 assert(z >= 0);
164
165 (void) getrlimit(z, &buf);
166 x = is_soft ? buf.rlim_cur : buf.rlim_max;
167 }
168
169 /* rlim_t might have different sizes, let's map RLIMIT_INFINITY to UINT64_MAX, so that it is the same on all
170 * archs */
171 u = x == RLIM_INFINITY ? UINT64_MAX : (uint64_t) x;
172
173 return sd_bus_message_append(reply, "t", u);
174 }
175
176 int bus_property_get_string_set(
177 sd_bus *bus,
178 const char *path,
179 const char *interface,
180 const char *property,
181 sd_bus_message *reply,
182 void *userdata,
183 sd_bus_error *reterr_error) {
184
185 Set **s = ASSERT_PTR(userdata);
186
187 assert(bus);
188 assert(property);
189 assert(reply);
190
191 return bus_message_append_string_set(reply, *s);
192 }
193
194 int bus_property_get_pidfdid(
195 sd_bus *bus,
196 const char *path,
197 const char *interface,
198 const char *property,
199 sd_bus_message *reply,
200 void *userdata,
201 sd_bus_error *reterr_error) {
202
203 PidRef *pidref = ASSERT_PTR(userdata);
204
205 assert(bus);
206 assert(property);
207 assert(reply);
208
209 (void) pidref_acquire_pidfd_id(pidref);
210
211 return sd_bus_message_append(reply, "t", pidref->fd_id);
212 }