Debian Codegraph @

src/core/varlink-common.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-bus.h"
4 #include "sd-varlink.h"
5
6 #include "bus-common-errors.h"
7 #include "cpu-set-util.h"
8 #include "execute.h"
9 #include "json-util.h"
10 #include "pidref.h"
11 #include "process-util.h"
12 #include "rlimit-util.h"
13 #include "varlink-common.h"
14 #include "varlink-unit.h"
15
16 const char* varlink_error_id_from_bus_error(const sd_bus_error *e) {
17 static const struct {
18 const char *bus_error;
19 const char *varlink_error;
20 } map[] = {
21 { BUS_ERROR_NO_SUCH_UNIT, VARLINK_ERROR_UNIT_NO_SUCH_UNIT },
22 { BUS_ERROR_ONLY_BY_DEPENDENCY, VARLINK_ERROR_UNIT_ONLY_BY_DEPENDENCY },
23 { BUS_ERROR_SHUTTING_DOWN, VARLINK_ERROR_UNIT_DBUS_SHUTTING_DOWN },
24 { BUS_ERROR_UNIT_EXISTS, VARLINK_ERROR_UNIT_UNIT_EXISTS },
25 { BUS_ERROR_BAD_UNIT_SETTING, VARLINK_ERROR_UNIT_BAD_SETTING },
26 };
27
28 if (!sd_bus_error_is_set(e))
29 return NULL;
30
31 FOREACH_ELEMENT(i, map)
32 if (sd_bus_error_has_name(e, i->bus_error))
33 return i->varlink_error;
34
35 return NULL;
36 }
37
38 int varlink_reply_bus_error(sd_varlink *link, int r, const sd_bus_error *e) {
39 const char *error_id = varlink_error_id_from_bus_error(e);
40 if (error_id)
41 return sd_varlink_error(link, error_id, NULL);
42 return sd_varlink_error_errno(link, r);
43 }
44
45 int rlimit_build_json(sd_json_variant **ret, const char *name, void *userdata) {
46 const struct rlimit *rl = userdata;
47 struct rlimit buf = {};
48
49 assert(ret);
50 assert(name);
51
52 if (!rl) {
53 int z = rlimit_from_string(name);
54 if (z < 0)
55 return log_debug_errno(z, "Failed to get rlimit for '%s': %m", name);
56
57 if (getrlimit(z, &buf) < 0) {
58 log_debug_errno(errno, "Failed to getrlimit(%s), ignoring: %m", name);
59 *ret = NULL;
60 return 0;
61 }
62
63 rl = &buf;
64 }
65
66 if (rl->rlim_cur == RLIM_INFINITY && rl->rlim_max == RLIM_INFINITY) {
67 *ret = NULL;
68 return 0;
69 }
70
71 return sd_json_buildo(ret,
72 JSON_BUILD_PAIR_UNSIGNED_NOT_EQUAL("soft", rl->rlim_cur, RLIM_INFINITY),
73 JSON_BUILD_PAIR_UNSIGNED_NOT_EQUAL("hard", rl->rlim_max, RLIM_INFINITY));
74 }
75
76 int rlimit_table_build_json(sd_json_variant **ret, const char *name, void *userdata) {
77 _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
78 struct rlimit **rl = ASSERT_PTR(userdata);
79 int r;
80
81 assert(ret);
82
83 for (int i = 0; i < _RLIMIT_MAX; i++) {
84 r = sd_json_variant_merge_objectbo(
85 &v,
86 JSON_BUILD_PAIR_CALLBACK_NON_NULL(rlimit_to_string(i), rlimit_build_json, rl[i]));
87 if (r < 0)
88 return r;
89 }
90
91 *ret = TAKE_PTR(v);
92
93 return 0;
94 }
95
96 int cpuset_build_json(sd_json_variant **ret, const char *name, void *userdata) {
97 _cleanup_free_ uint8_t *array = NULL;
98 CPUSet *cpuset = ASSERT_PTR(userdata);
99 size_t allocated;
100 int r;
101
102 assert(ret);
103
104 if (!cpuset->set)
105 goto empty;
106
107 r = cpu_set_to_dbus(cpuset, &array, &allocated);
108 if (r < 0)
109 return log_debug_errno(r, "Failed to serialize cpu set to dbus: %m");
110
111 if (allocated == 0)
112 goto empty;
113
114 return sd_json_variant_new_array_bytes(ret, array, allocated);
115
116 empty:
117 *ret = NULL;
118 return 0;
119 }
120
121 int exec_command_build_json(sd_json_variant **ret, const char *name, void *userdata) {
122 ExecCommand *cmd = ASSERT_PTR(userdata);
123
124 assert(ret);
125
126 if (isempty(cmd->path)) {
127 *ret = NULL;
128 return 0;
129 }
130
131 return sd_json_buildo(
132 ret,
133 SD_JSON_BUILD_PAIR_STRING("path", cmd->path),
134 JSON_BUILD_PAIR_STRV_NON_EMPTY("arguments", cmd->argv),
135 SD_JSON_BUILD_PAIR_BOOLEAN("ignoreFailure", FLAGS_SET(cmd->flags, EXEC_COMMAND_IGNORE_FAILURE)),
136 SD_JSON_BUILD_PAIR_BOOLEAN("privileged", FLAGS_SET(cmd->flags, EXEC_COMMAND_FULLY_PRIVILEGED)),
137 SD_JSON_BUILD_PAIR_BOOLEAN("noSetuid", FLAGS_SET(cmd->flags, EXEC_COMMAND_NO_SETUID)),
138 SD_JSON_BUILD_PAIR_BOOLEAN("noEnvExpand", FLAGS_SET(cmd->flags, EXEC_COMMAND_NO_ENV_EXPAND)),
139 SD_JSON_BUILD_PAIR_BOOLEAN("viaShell", FLAGS_SET(cmd->flags, EXEC_COMMAND_VIA_SHELL)));
140 }
141
142 int exec_command_list_build_json(sd_json_variant **ret, const char *name, void *userdata) {
143 _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
144 ExecCommand *list = userdata;
145 int r;
146
147 assert(ret);
148
149 LIST_FOREACH(command, c, list) {
150 _cleanup_(sd_json_variant_unrefp) sd_json_variant *entry = NULL;
151
152 r = exec_command_build_json(&entry, /* name= */ NULL, c);
153 if (r < 0)
154 return r;
155
156 r = sd_json_variant_append_array(&v, entry);
157 if (r < 0)
158 return r;
159 }
160
161 *ret = TAKE_PTR(v);
162 return 0;
163 }
164
165 int exec_command_status_build_json(sd_json_variant **ret, const char *name, void *userdata) {
166 ExecStatus *status = ASSERT_PTR(userdata);
167
168 assert(ret);
169
170 if (!pid_is_valid(status->pid)) {
171 *ret = NULL;
172 return 0;
173 }
174
175 return sd_json_buildo(
176 ret,
177 /* TODO: replace with a real PidRef once ExecStatus carries one */
178 SD_JSON_BUILD_PAIR("PID", JSON_BUILD_PIDREF(&PIDREF_MAKE_FROM_PID(status->pid))),
179 JSON_BUILD_PAIR_DUAL_TIMESTAMP_NON_NULL("StartTimestamp", &status->start_timestamp),
180 JSON_BUILD_PAIR_DUAL_TIMESTAMP_NON_NULL("ExitTimestamp", &status->exit_timestamp),
181 JSON_BUILD_PAIR_DUAL_TIMESTAMP_NON_NULL("HandoffTimestamp", &status->handoff_timestamp),
182 SD_JSON_BUILD_PAIR_CONDITION(status->code > 0, "Code", SD_JSON_BUILD_INTEGER(status->code)),
183 SD_JSON_BUILD_PAIR_CONDITION(status->code > 0, "Status", SD_JSON_BUILD_INTEGER(status->status)));
184 }
185
186 /* exec_command_status_list_build_json() is the runtime counterpart of exec_command_list_build_json().
187 * The two arrays are positionally aligned: index N in the status array corresponds to index N in the
188 * command array. Commands that have not yet run produce null entries to preserve alignment. */
189 int exec_command_status_list_build_json(sd_json_variant **ret, const char *name, void *userdata) {
190 _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
191 ExecCommand *list = userdata;
192 bool any_ran = false;
193 int r;
194
195 assert(ret);
196
197 LIST_FOREACH(command, c, list) {
198 _cleanup_(sd_json_variant_unrefp) sd_json_variant *entry = NULL;
199
200 r = exec_command_status_build_json(&entry, /* name= */ NULL, &c->exec_status);
201 if (r < 0)
202 return r;
203
204 if (entry) {
205 any_ran = true;
206 r = sd_json_variant_append_array(&v, entry);
207 } else
208 r = sd_json_variant_append_arrayb(&v, SD_JSON_BUILD_NULL);
209 if (r < 0)
210 return r;
211 }
212
213 if (!any_ran) {
214 *ret = NULL;
215 return 0;
216 }
217
218 *ret = TAKE_PTR(v);
219 return 0;
220 }