/src/systemd/src/shared/bus-get-properties.c
Line | Count | Source |
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 | 0 | BUS_DEFINE_PROPERTY_GET_GLOBAL(bus_property_get_bool_false, "b", 0); |
12 | 0 | BUS_DEFINE_PROPERTY_GET_GLOBAL(bus_property_get_bool_true, "b", 1); |
13 | 0 | BUS_DEFINE_PROPERTY_GET_GLOBAL(bus_property_get_uint64_0, "t", UINT64_C(0)); |
14 | 0 | 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 | 0 | sd_bus_error *reterr_error) { |
24 | |
|
25 | 0 | int b = *(bool*) userdata; |
26 | |
|
27 | 0 | return sd_bus_message_append_basic(reply, 'b', &b); |
28 | 0 | } |
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 | 0 | sd_bus_error *reterr_error) { |
38 | |
|
39 | 0 | int b, r; |
40 | |
|
41 | 0 | r = sd_bus_message_read(value, "b", &b); |
42 | 0 | if (r < 0) |
43 | 0 | return r; |
44 | | |
45 | 0 | *(bool*) userdata = b; |
46 | 0 | return 0; |
47 | 0 | } |
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 | 0 | sd_bus_error *reterr_error) { |
57 | | |
58 | | /* Defaults to false. */ |
59 | |
|
60 | 0 | int b = (*(int*) userdata) > 0; |
61 | |
|
62 | 0 | return sd_bus_message_append_basic(reply, 'b', &b); |
63 | 0 | } |
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 | 0 | sd_bus_error *reterr_error) { |
73 | |
|
74 | 0 | sd_id128_t *id = ASSERT_PTR(userdata); |
75 | |
|
76 | 0 | if (sd_id128_is_null(*id)) /* Add an empty array if the ID is zero */ |
77 | 0 | return sd_bus_message_append(reply, "ay", 0); |
78 | | |
79 | 0 | return sd_bus_message_append_array(reply, 'y', id->bytes, sizeof(sd_id128_t)); |
80 | 0 | } |
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 | 0 | sd_bus_error *reterr_error) { |
136 | |
|
137 | 0 | const char *is_soft; |
138 | 0 | struct rlimit *rl; |
139 | 0 | uint64_t u; |
140 | 0 | rlim_t x; |
141 | |
|
142 | 0 | assert(bus); |
143 | 0 | assert(reply); |
144 | 0 | assert(userdata); |
145 | |
|
146 | 0 | is_soft = endswith(property, "Soft"); |
147 | |
|
148 | 0 | rl = *(struct rlimit**) userdata; |
149 | 0 | if (rl) |
150 | 0 | x = is_soft ? rl->rlim_cur : rl->rlim_max; |
151 | 0 | else { |
152 | 0 | struct rlimit buf = {}; |
153 | 0 | const char *s, *p; |
154 | 0 | int z; |
155 | | |
156 | | /* Chop off "Soft" suffix */ |
157 | 0 | s = is_soft ? strndupa_safe(property, is_soft - property) : property; |
158 | | |
159 | | /* Skip over any prefix, such as "Default" */ |
160 | 0 | assert_se(p = strstrafter(s, "Limit")); |
161 | |
|
162 | 0 | z = rlimit_from_string(p); |
163 | 0 | assert(z >= 0); |
164 | |
|
165 | 0 | (void) getrlimit(z, &buf); |
166 | 0 | x = is_soft ? buf.rlim_cur : buf.rlim_max; |
167 | 0 | } |
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 | 0 | u = x == RLIM_INFINITY ? UINT64_MAX : (uint64_t) x; |
172 | |
|
173 | 0 | return sd_bus_message_append(reply, "t", u); |
174 | 0 | } |
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 | 0 | sd_bus_error *reterr_error) { |
184 | |
|
185 | 0 | Set **s = ASSERT_PTR(userdata); |
186 | |
|
187 | 0 | assert(bus); |
188 | 0 | assert(property); |
189 | 0 | assert(reply); |
190 | |
|
191 | 0 | return bus_message_append_string_set(reply, *s); |
192 | 0 | } |
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 | 0 | sd_bus_error *reterr_error) { |
202 | |
|
203 | 0 | PidRef *pidref = ASSERT_PTR(userdata); |
204 | |
|
205 | 0 | assert(bus); |
206 | 0 | assert(property); |
207 | 0 | assert(reply); |
208 | |
|
209 | 0 | (void) pidref_acquire_pidfd_id(pidref); |
210 | |
|
211 | 0 | return sd_bus_message_append(reply, "t", pidref->fd_id); |
212 | 0 | } |