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