]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/bus-get-properties.c
Merge pull request #17185 from yuwata/ethtool-update
[thirdparty/systemd.git] / src / shared / bus-get-properties.c
CommitLineData
40af3d02
LP
1/* SPDX-License-Identifier: LGPL-2.1+ */
2
3#include "bus-get-properties.h"
4#include "rlimit-util.h"
4d824a4e 5#include "stdio-util.h"
40af3d02
LP
6#include "string-util.h"
7
8int bus_property_get_bool(
9 sd_bus *bus,
10 const char *path,
11 const char *interface,
12 const char *property,
13 sd_bus_message *reply,
14 void *userdata,
15 sd_bus_error *error) {
16
17 int b = *(bool*) userdata;
18
19 return sd_bus_message_append_basic(reply, 'b', &b);
20}
21
22int bus_property_set_bool(
23 sd_bus *bus,
24 const char *path,
25 const char *interface,
26 const char *property,
27 sd_bus_message *value,
28 void *userdata,
29 sd_bus_error *error) {
30
31 int b, r;
32
33 r = sd_bus_message_read(value, "b", &b);
34 if (r < 0)
35 return r;
36
37 *(bool*) userdata = b;
38 return 0;
39}
40
41int bus_property_get_id128(
42 sd_bus *bus,
43 const char *path,
44 const char *interface,
45 const char *property,
46 sd_bus_message *reply,
47 void *userdata,
48 sd_bus_error *error) {
49
50 sd_id128_t *id = userdata;
51
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);
54 else
55 return sd_bus_message_append_array(reply, 'y', id->bytes, 16);
56}
57
4d824a4e
AZ
58int bus_property_get_percent(
59 sd_bus *bus,
60 const char *path,
61 const char *interface,
62 const char *property,
63 sd_bus_message *reply,
64 void *userdata,
65 sd_bus_error *error) {
66
67 char pstr[DECIMAL_STR_MAX(int) + 2];
68 int p = *(int*) userdata;
69
70 xsprintf(pstr, "%d%%", p);
71
72 return sd_bus_message_append_basic(reply, 's', pstr);
73}
74
40af3d02
LP
75#if __SIZEOF_SIZE_T__ != 8
76int bus_property_get_size(
77 sd_bus *bus,
78 const char *path,
79 const char *interface,
80 const char *property,
81 sd_bus_message *reply,
82 void *userdata,
83 sd_bus_error *error) {
84
85 uint64_t sz = *(size_t*) userdata;
86
87 return sd_bus_message_append_basic(reply, 't', &sz);
88}
89#endif
90
91#if __SIZEOF_LONG__ != 8
92int bus_property_get_long(
93 sd_bus *bus,
94 const char *path,
95 const char *interface,
96 const char *property,
97 sd_bus_message *reply,
98 void *userdata,
99 sd_bus_error *error) {
100
101 int64_t l = *(long*) userdata;
102
103 return sd_bus_message_append_basic(reply, 'x', &l);
104}
105
106int bus_property_get_ulong(
107 sd_bus *bus,
108 const char *path,
109 const char *interface,
110 const char *property,
111 sd_bus_message *reply,
112 void *userdata,
113 sd_bus_error *error) {
114
115 uint64_t ul = *(unsigned long*) userdata;
116
117 return sd_bus_message_append_basic(reply, 't', &ul);
118}
119#endif
120
121int bus_property_get_rlimit(
122 sd_bus *bus,
123 const char *path,
124 const char *interface,
125 const char *property,
126 sd_bus_message *reply,
127 void *userdata,
128 sd_bus_error *error) {
129
130 const char *is_soft;
131 struct rlimit *rl;
132 uint64_t u;
133 rlim_t x;
134
135 assert(bus);
136 assert(reply);
137 assert(userdata);
138
139 is_soft = endswith(property, "Soft");
140
141 rl = *(struct rlimit**) userdata;
142 if (rl)
143 x = is_soft ? rl->rlim_cur : rl->rlim_max;
144 else {
145 struct rlimit buf = {};
146 const char *s, *p;
147 int z;
148
149 /* Chop off "Soft" suffix */
150 s = is_soft ? strndupa(property, is_soft - property) : property;
151
152 /* Skip over any prefix, such as "Default" */
153 assert_se(p = strstr(s, "Limit"));
154
155 z = rlimit_from_string(p + 5);
156 assert(z >= 0);
157
158 (void) getrlimit(z, &buf);
159 x = is_soft ? buf.rlim_cur : buf.rlim_max;
160 }
161
162 /* rlim_t might have different sizes, let's map RLIMIT_INFINITY to (uint64_t) -1, so that it is the same on all
163 * archs */
164 u = x == RLIM_INFINITY ? (uint64_t) -1 : (uint64_t) x;
165
166 return sd_bus_message_append(reply, "t", u);
167}