1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <limits.h>
#include "sd-id128.h"
#include "sd-json.h"
#include "sd-varlink.h"
#include "bus-polkit.h"
#include "hostname-util.h"
#include "json-util.h"
#include "machine-varlink.h"
#include "machine.h"
#include "path-util.h"
#include "pidref.h"
#include "process-util.h"
#include "signal-util.h"
#include "socket-util.h"
#include "string-util.h"
#include "varlink-util.h"
static JSON_DISPATCH_ENUM_DEFINE(dispatch_machine_class, MachineClass, machine_class_from_string);
static int machine_name(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
char **m = ASSERT_PTR(userdata);
const char *hostname;
int r;
assert(variant);
if (!sd_json_variant_is_string(variant))
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name));
hostname = sd_json_variant_string(variant);
if (!hostname_is_valid(hostname, /* flags= */ 0))
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "Invalid machine name");
r = free_and_strdup(m, hostname);
if (r < 0)
return json_log_oom(variant, flags);
return 0;
}
static int machine_leader(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
PidRef *leader = ASSERT_PTR(userdata);
_cleanup_(pidref_done) PidRef temp = PIDREF_NULL;
uint64_t k;
int r;
if (!sd_json_variant_is_unsigned(variant))
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an integer.", strna(name));
k = sd_json_variant_unsigned(variant);
if (k > PID_T_MAX || !pid_is_valid(k))
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid PID.", strna(name));
if (k == 1)
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid leader PID.", strna(name));
r = pidref_set_pid(&temp, k);
if (r < 0)
return json_log(variant, flags, r, "Failed to pin process " PID_FMT ": %m", leader->pid);
pidref_done(leader);
*leader = TAKE_PIDREF(temp);
return 0;
}
static int machine_ifindices(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
Machine *m = ASSERT_PTR(userdata);
_cleanup_free_ int *netif = NULL;
size_t n_netif, k = 0;
assert(variant);
if (!sd_json_variant_is_array(variant))
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array.", strna(name));
n_netif = sd_json_variant_elements(variant);
netif = new(int, n_netif);
if (!netif)
return json_log_oom(variant, flags);
sd_json_variant *i;
JSON_VARIANT_ARRAY_FOREACH(i, variant) {
uint64_t b;
if (!sd_json_variant_is_unsigned(i))
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "Element %zu of JSON field '%s' is not an unsigned integer.", k, strna(name));
b = sd_json_variant_unsigned(i);
if (b > INT_MAX || b <= 0)
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "Invalid network interface index %"PRIu64, b);
netif[k++] = (int) b;
}
assert(k == n_netif);
free_and_replace(m->netif, netif);
m->n_netif = n_netif;
return 0;
}
static int machine_cid(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
unsigned cid, *c = ASSERT_PTR(userdata);
int r;
assert(variant);
r = sd_json_dispatch_uint(name, variant, flags, &cid);
if (r < 0)
return r;
if (!VSOCK_CID_IS_REGULAR(cid))
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a regular VSOCK CID.", strna(name));
*c = cid;
return 0;
}
int vl_method_register(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) {
Manager *manager = ASSERT_PTR(userdata);
_cleanup_(machine_freep) Machine *machine = NULL;
int r;
static const sd_json_dispatch_field dispatch_table[] = {
{ "name", SD_JSON_VARIANT_STRING, machine_name, offsetof(Machine, name), SD_JSON_MANDATORY },
{ "id", SD_JSON_VARIANT_STRING, sd_json_dispatch_id128, offsetof(Machine, id), 0 },
{ "service", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(Machine, service), 0 },
{ "class", SD_JSON_VARIANT_STRING, dispatch_machine_class, offsetof(Machine, class), SD_JSON_MANDATORY },
{ "leader", SD_JSON_VARIANT_UNSIGNED, machine_leader, offsetof(Machine, leader), 0 },
{ "rootDirectory", SD_JSON_VARIANT_STRING, json_dispatch_path, offsetof(Machine, root_directory), 0 },
{ "ifIndices", SD_JSON_VARIANT_ARRAY, machine_ifindices, 0, 0 },
{ "vSockCid", _SD_JSON_VARIANT_TYPE_INVALID, machine_cid, offsetof(Machine, vsock_cid), 0 },
{ "sshAddress", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(Machine, ssh_address), SD_JSON_STRICT },
{ "sshPrivateKeyPath", SD_JSON_VARIANT_STRING, json_dispatch_path, offsetof(Machine, ssh_private_key_path), 0 },
{ "allocateUnit", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(Machine, allocate_unit), 0 },
VARLINK_DISPATCH_POLKIT_FIELD,
{}
};
r = machine_new(_MACHINE_CLASS_INVALID, NULL, &machine);
if (r < 0)
return r;
r = sd_varlink_dispatch(link, parameters, dispatch_table, machine);
if (r != 0)
return r;
r = varlink_verify_polkit_async(
link,
manager->bus,
"org.freedesktop.machine1.create-machine",
(const char**) STRV_MAKE("name", machine->name,
"class", machine_class_to_string(machine->class)),
&manager->polkit_registry);
if (r <= 0)
return r;
if (!pidref_is_set(&machine->leader)) {
r = varlink_get_peer_pidref(link, &machine->leader);
if (r < 0)
return r;
}
r = machine_link(manager, machine);
if (r == -EEXIST)
return sd_varlink_error(link, "io.systemd.Machine.MachineExists", NULL);
if (r < 0)
return r;
if (!machine->allocate_unit) {
r = cg_pidref_get_unit(&machine->leader, &machine->unit);
if (r < 0)
return r;
}
r = machine_start(machine, /* properties= */ NULL, /* error= */ NULL);
if (r < 0)
return r;
/* the manager will free this machine */
TAKE_PTR(machine);
return sd_varlink_reply(link, NULL);
}
static int lookup_machine_by_name(sd_varlink *link, Manager *manager, const char *machine_name, Machine **ret_machine) {
assert(link);
assert(manager);
assert(ret_machine);
if (!machine_name)
return -EINVAL;
if (!hostname_is_valid(machine_name, /* flags= */ VALID_HOSTNAME_DOT_HOST))
return -EINVAL;
Machine *machine = hashmap_get(manager->machines, machine_name);
if (!machine)
return -ESRCH;
*ret_machine = machine;
return 0;
}
static int lookup_machine_by_pid(sd_varlink *link, Manager *manager, pid_t pid, Machine **ret_machine) {
_cleanup_(pidref_done) PidRef pidref = PIDREF_MAKE_FROM_PID(pid);
Machine *machine;
int r;
assert(link);
assert(manager);
assert(ret_machine);
assert_cc(sizeof(pid_t) == sizeof(uint32_t));
if (pid == 0) {
r = varlink_get_peer_pidref(link, &pidref);
if (r < 0)
return log_debug_errno(r, "Failed to get peer pidref: %m");
}
if (!pidref_is_set(&pidref))
return -EINVAL;
r = manager_get_machine_by_pidref(manager, &pidref, &machine);
if (r < 0)
return r;
if (!machine)
return -ESRCH;
*ret_machine = machine;
return 0;
}
int lookup_machine_by_name_or_pid(sd_varlink *link, Manager *manager, const char *machine_name, pid_t pid, Machine **ret_machine) {
Machine *machine = NULL, *pid_machine = NULL;
int r;
assert(link);
assert(manager);
assert(ret_machine);
if (machine_name) {
r = lookup_machine_by_name(link, manager, machine_name, &machine);
if (r == -EINVAL)
return sd_varlink_error_invalid_parameter_name(link, "name");
if (r < 0)
return r;
}
if (pid >= 0) {
r = lookup_machine_by_pid(link, manager, pid, &pid_machine);
if (r == -EINVAL)
return sd_varlink_error_invalid_parameter_name(link, "pid");
if (r < 0)
return r;
}
if (machine && pid_machine && machine != pid_machine)
return log_debug_errno(SYNTHETIC_ERRNO(ESRCH), "Search by machine name '%s' and pid %d resulted in two different machines", machine_name, pid);
if (machine)
*ret_machine = machine;
else if (pid_machine)
*ret_machine = pid_machine;
else
return -ESRCH;
return 0;
}
int vl_method_unregister_internal(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) {
Machine *machine = ASSERT_PTR(userdata);
Manager *manager = ASSERT_PTR(machine->manager);
int r;
r = varlink_verify_polkit_async(
link,
manager->bus,
"org.freedesktop.machine1.manage-machines",
(const char**) STRV_MAKE("name", machine->name,
"verb", "unregister"),
&manager->polkit_registry);
if (r <= 0)
return r;
r = machine_finalize(machine);
if (r < 0)
return log_debug_errno(r, "Failed to finalize machine: %m");
return sd_varlink_reply(link, NULL);
}
int vl_method_terminate_internal(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) {
Machine *machine = ASSERT_PTR(userdata);
Manager *manager = ASSERT_PTR(machine->manager);
int r;
r = varlink_verify_polkit_async(
link,
manager->bus,
"org.freedesktop.machine1.manage-machines",
(const char**) STRV_MAKE("name", machine->name,
"verb", "terminate"),
&manager->polkit_registry);
if (r <= 0)
return r;
r = machine_stop(machine);
if (r < 0)
return log_debug_errno(r, "Failed to stop machine: %m");
return sd_varlink_reply(link, NULL);
}
int vl_method_kill(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) {
struct params {
const char *machine_name;
pid_t pid;
const char *swhom;
int32_t signo;
};
static const sd_json_dispatch_field dispatch_table[] = {
VARLINK_DISPATCH_MACHINE_LOOKUP_FIELDS(struct params),
{ "whom", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct params, swhom), 0 },
{ "signal", _SD_JSON_VARIANT_TYPE_INVALID , sd_json_dispatch_signal, offsetof(struct params, signo), SD_JSON_MANDATORY },
VARLINK_DISPATCH_POLKIT_FIELD,
{}
};
Manager *manager = ASSERT_PTR(userdata);
struct params p = { .pid = -1 };
KillWhom whom;
int r;
assert(link);
assert(parameters);
r = sd_varlink_dispatch(link, parameters, dispatch_table, &p);
if (r != 0)
return r;
Machine *machine;
r = lookup_machine_by_name_or_pid(link, manager, p.machine_name, p.pid, &machine);
if (r == -ESRCH)
return sd_varlink_error(link, "io.systemd.Machine.NoSuchMachine", NULL);
if (r < 0)
return r;
if (isempty(p.swhom))
whom = KILL_ALL;
else {
whom = kill_whom_from_string(p.swhom);
if (whom < 0)
return sd_varlink_error_invalid_parameter_name(link, "whom");
}
r = varlink_verify_polkit_async(
link,
manager->bus,
"org.freedesktop.machine1.manage-machines",
(const char**) STRV_MAKE("name", machine->name,
"verb", "kill"),
&manager->polkit_registry);
if (r <= 0)
return r;
r = machine_kill(machine, whom, p.signo);
if (r < 0)
return log_debug_errno(r, "Failed to send signal to machine: %m");
return sd_varlink_reply(link, NULL);
}
|