]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/bus-get-properties.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "bus-get-properties.h"
4 #include "rlimit-util.h"
5 #include "stdio-util.h"
6 #include "string-util.h"
8 int bus_property_get_bool(
11 const char *interface
,
13 sd_bus_message
*reply
,
15 sd_bus_error
*error
) {
17 int b
= *(bool*) userdata
;
19 return sd_bus_message_append_basic(reply
, 'b', &b
);
22 int bus_property_set_bool(
25 const char *interface
,
27 sd_bus_message
*value
,
29 sd_bus_error
*error
) {
33 r
= sd_bus_message_read(value
, "b", &b
);
37 *(bool*) userdata
= b
;
41 int bus_property_get_id128(
44 const char *interface
,
46 sd_bus_message
*reply
,
48 sd_bus_error
*error
) {
50 sd_id128_t
*id
= userdata
;
52 if (sd_id128_is_null(*id
)) /* Add an empty array if the ID is zero */
53 return sd_bus_message_append(reply
, "ay", 0);
55 return sd_bus_message_append_array(reply
, 'y', id
->bytes
, 16);
58 int bus_property_get_percent(
61 const char *interface
,
63 sd_bus_message
*reply
,
65 sd_bus_error
*error
) {
67 char pstr
[DECIMAL_STR_MAX(int) + 2];
68 int p
= *(int*) userdata
;
70 xsprintf(pstr
, "%d%%", p
);
72 return sd_bus_message_append_basic(reply
, 's', pstr
);
75 #if __SIZEOF_SIZE_T__ != 8
76 int bus_property_get_size(
79 const char *interface
,
81 sd_bus_message
*reply
,
83 sd_bus_error
*error
) {
85 uint64_t sz
= *(size_t*) userdata
;
87 return sd_bus_message_append_basic(reply
, 't', &sz
);
91 #if __SIZEOF_LONG__ != 8
92 int bus_property_get_long(
95 const char *interface
,
97 sd_bus_message
*reply
,
99 sd_bus_error
*error
) {
101 int64_t l
= *(long*) userdata
;
103 return sd_bus_message_append_basic(reply
, 'x', &l
);
106 int bus_property_get_ulong(
109 const char *interface
,
110 const char *property
,
111 sd_bus_message
*reply
,
113 sd_bus_error
*error
) {
115 uint64_t ul
= *(unsigned long*) userdata
;
117 return sd_bus_message_append_basic(reply
, 't', &ul
);
121 int bus_property_get_rlimit(
124 const char *interface
,
125 const char *property
,
126 sd_bus_message
*reply
,
128 sd_bus_error
*error
) {
139 is_soft
= endswith(property
, "Soft");
141 rl
= *(struct rlimit
**) userdata
;
143 x
= is_soft
? rl
->rlim_cur
: rl
->rlim_max
;
145 struct rlimit buf
= {};
149 /* Chop off "Soft" suffix */
150 s
= is_soft
? strndupa(property
, is_soft
- property
) : property
;
152 /* Skip over any prefix, such as "Default" */
153 assert_se(p
= strstr(s
, "Limit"));
155 z
= rlimit_from_string(p
+ 5);
158 (void) getrlimit(z
, &buf
);
159 x
= is_soft
? buf
.rlim_cur
: buf
.rlim_max
;
162 /* rlim_t might have different sizes, let's map RLIMIT_INFINITY to (uint64_t) -1, so that it is the same on all
164 u
= x
== RLIM_INFINITY
? (uint64_t) -1 : (uint64_t) x
;
166 return sd_bus_message_append(reply
, "t", u
);