X Tutup
Skip to content

Commit 9b5ed6f

Browse files
committed
machined: allow registering host-side network interfaces for communication with containers
1 parent 4faefc7 commit 9b5ed6f

File tree

4 files changed

+127
-7
lines changed

4 files changed

+127
-7
lines changed

src/machine/machine-dbus.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,31 @@ static int property_get_state(
8383
return 1;
8484
}
8585

86+
static int property_get_netif(
87+
sd_bus *bus,
88+
const char *path,
89+
const char *interface,
90+
const char *property,
91+
sd_bus_message *reply,
92+
void *userdata,
93+
sd_bus_error *error) {
94+
95+
Machine *m = userdata;
96+
int r;
97+
98+
assert(bus);
99+
assert(reply);
100+
assert(m);
101+
102+
assert_cc(sizeof(int) == sizeof(int32_t));
103+
104+
r = sd_bus_message_append_array(reply, 'i', m->netif, m->n_netif * sizeof(int));
105+
if (r < 0)
106+
return r;
107+
108+
return 1;
109+
}
110+
86111
static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_class, machine_class, MachineClass);
87112

88113
int bus_machine_method_terminate(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
@@ -376,6 +401,7 @@ const sd_bus_vtable machine_vtable[] = {
376401
SD_BUS_PROPERTY("Leader", "u", NULL, offsetof(Machine, leader), SD_BUS_VTABLE_PROPERTY_CONST),
377402
SD_BUS_PROPERTY("Class", "s", property_get_class, offsetof(Machine, class), SD_BUS_VTABLE_PROPERTY_CONST),
378403
SD_BUS_PROPERTY("RootDirectory", "s", NULL, offsetof(Machine, root_directory), SD_BUS_VTABLE_PROPERTY_CONST),
404+
SD_BUS_PROPERTY("NetworkInterfaces", "ai", property_get_netif, 0, SD_BUS_VTABLE_PROPERTY_CONST),
379405
SD_BUS_PROPERTY("State", "s", property_get_state, 0, 0),
380406
SD_BUS_METHOD("Terminate", NULL, NULL, bus_machine_method_terminate, SD_BUS_VTABLE_CAPABILITY(CAP_KILL)),
381407
SD_BUS_METHOD("Kill", "si", NULL, bus_machine_method_kill, SD_BUS_VTABLE_CAPABILITY(CAP_KILL)),

src/machine/machine.c

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ void machine_free(Machine *m) {
9494
free(m->state_file);
9595
free(m->service);
9696
free(m->root_directory);
97+
free(m->netif);
9798
free(m);
9899
}
99100

@@ -176,6 +177,21 @@ int machine_save(Machine *m) {
176177
m->timestamp.realtime,
177178
m->timestamp.monotonic);
178179

180+
if (m->n_netif > 0) {
181+
unsigned i;
182+
183+
fputs("NETIF=", f);
184+
185+
for (i = 0; i < m->n_netif; i++) {
186+
if (i != 0)
187+
fputc(' ', f);
188+
189+
fprintf(f, "%i", m->netif[i]);
190+
}
191+
192+
fputc('\n', f);
193+
}
194+
179195
r = fflush_and_check(f);
180196
if (r < 0)
181197
goto finish;
@@ -222,7 +238,7 @@ static void machine_unlink(Machine *m) {
222238
}
223239

224240
int machine_load(Machine *m) {
225-
_cleanup_free_ char *realtime = NULL, *monotonic = NULL, *id = NULL, *leader = NULL, *class = NULL;
241+
_cleanup_free_ char *realtime = NULL, *monotonic = NULL, *id = NULL, *leader = NULL, *class = NULL, *netif = NULL;
226242
int r;
227243

228244
assert(m);
@@ -237,6 +253,7 @@ int machine_load(Machine *m) {
237253
"CLASS", &class,
238254
"REALTIME", &realtime,
239255
"MONOTONIC", &monotonic,
256+
"NETIF", &netif,
240257
NULL);
241258
if (r < 0) {
242259
if (r == -ENOENT)
@@ -272,6 +289,35 @@ int machine_load(Machine *m) {
272289
m->timestamp.monotonic = l;
273290
}
274291

292+
if (netif) {
293+
size_t l, allocated = 0, nr = 0;
294+
char *w, *state;
295+
int *ni = NULL;
296+
297+
FOREACH_WORD(w, l, netif, state) {
298+
char buf[l+1];
299+
int ifi;
300+
301+
*(char*) (mempcpy(buf, w, l)) = 0;
302+
303+
if (safe_atoi(buf, &ifi) < 0)
304+
continue;
305+
if (ifi <= 0)
306+
continue;
307+
308+
if (!GREEDY_REALLOC(ni, allocated, nr+1)) {
309+
free(ni);
310+
return log_oom();
311+
}
312+
313+
ni[nr++] = ifi;
314+
}
315+
316+
free(m->netif);
317+
m->netif = ni;
318+
m->n_netif = nr;
319+
}
320+
275321
return r;
276322
}
277323

src/machine/machine.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ struct Machine {
7676

7777
sd_bus_message *create_message;
7878

79+
int *netif;
80+
unsigned n_netif;
81+
7982
LIST_FIELDS(Machine, gc_queue);
8083
};
8184

src/machine/machined-dbus.c

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,15 @@ static int method_list_machines(sd_bus *bus, sd_bus_message *message, void *user
151151
return sd_bus_send(bus, reply, NULL);
152152
}
153153

154-
static int method_create_or_register_machine(Manager *manager, sd_bus_message *message, Machine **_m, sd_bus_error *error) {
154+
static int method_create_or_register_machine(Manager *manager, sd_bus_message *message, bool read_network, Machine **_m, sd_bus_error *error) {
155155
const char *name, *service, *class, *root_directory;
156+
const int32_t *netif = NULL;
156157
MachineClass c;
157158
uint32_t leader;
158159
sd_id128_t id;
159160
const void *v;
160161
Machine *m;
161-
size_t n;
162+
size_t n, n_netif = 0;
162163
int r;
163164

164165
assert(manager);
@@ -185,6 +186,21 @@ static int method_create_or_register_machine(Manager *manager, sd_bus_message *m
185186
if (r < 0)
186187
return r;
187188

189+
if (read_network) {
190+
size_t i;
191+
192+
r = sd_bus_message_read_array(message, 'i', (const void**) &netif, &n_netif);
193+
if (r < 0)
194+
return r;
195+
196+
n_netif /= sizeof(int32_t);
197+
198+
for (i = 0; i < n_netif; i++) {
199+
if (netif[i] <= 0)
200+
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid network interface index %i", netif[i]);
201+
}
202+
}
203+
188204
if (isempty(class))
189205
c = _MACHINE_CLASS_INVALID;
190206
else {
@@ -240,6 +256,17 @@ static int method_create_or_register_machine(Manager *manager, sd_bus_message *m
240256
}
241257
}
242258

259+
if (n_netif > 0) {
260+
assert_cc(sizeof(int32_t) == sizeof(int));
261+
m->netif = memdup(netif, sizeof(int32_t) * n_netif);
262+
if (!m->netif) {
263+
r = -ENOMEM;
264+
goto fail;
265+
}
266+
267+
m->n_netif = n_netif;
268+
}
269+
243270
*_m = m;
244271

245272
return 1;
@@ -249,12 +276,12 @@ static int method_create_or_register_machine(Manager *manager, sd_bus_message *m
249276
return r;
250277
}
251278

252-
static int method_create_machine(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
279+
static int method_create_machine_internal(sd_bus *bus, sd_bus_message *message, bool read_network, void *userdata, sd_bus_error *error) {
253280
Manager *manager = userdata;
254281
Machine *m = NULL;
255282
int r;
256283

257-
r = method_create_or_register_machine(manager, message, &m, error);
284+
r = method_create_or_register_machine(manager, message, read_network, &m, error);
258285
if (r < 0)
259286
return r;
260287

@@ -274,13 +301,21 @@ static int method_create_machine(sd_bus *bus, sd_bus_message *message, void *use
274301
return r;
275302
}
276303

277-
static int method_register_machine(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
304+
static int method_create_machine_with_network(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
305+
return method_create_machine_internal(bus, message, true, userdata, error);
306+
}
307+
308+
static int method_create_machine(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
309+
return method_create_machine_internal(bus, message, false, userdata, error);
310+
}
311+
312+
static int method_register_machine_internal(sd_bus *bus, sd_bus_message *message, bool read_network, void *userdata, sd_bus_error *error) {
278313
Manager *manager = userdata;
279314
_cleanup_free_ char *p = NULL;
280315
Machine *m = NULL;
281316
int r;
282317

283-
r = method_create_or_register_machine(manager, message, &m, error);
318+
r = method_create_or_register_machine(manager, message, read_network, &m, error);
284319
if (r < 0)
285320
return r;
286321

@@ -309,6 +344,14 @@ static int method_register_machine(sd_bus *bus, sd_bus_message *message, void *u
309344
return r;
310345
}
311346

347+
static int method_register_machine_with_network(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
348+
return method_register_machine_internal(bus, message, true, userdata, error);
349+
}
350+
351+
static int method_register_machine(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
352+
return method_register_machine_internal(bus, message, false, userdata, error);
353+
}
354+
312355
static int method_terminate_machine(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
313356
Manager *m = userdata;
314357
Machine *machine;
@@ -400,6 +443,8 @@ const sd_bus_vtable manager_vtable[] = {
400443
SD_BUS_METHOD("ListMachines", NULL, "a(ssso)", method_list_machines, SD_BUS_VTABLE_UNPRIVILEGED),
401444
SD_BUS_METHOD("CreateMachine", "sayssusa(sv)", "o", method_create_machine, 0),
402445
SD_BUS_METHOD("RegisterMachine", "sayssus", "o", method_register_machine, 0),
446+
SD_BUS_METHOD("CreateMachineWithNetwork", "sayssusaia(sv)", "o", method_create_machine_with_network, 0),
447+
SD_BUS_METHOD("RegisterMachineWithNetwork", "sayssusai", "o", method_register_machine_with_network, 0),
403448
SD_BUS_METHOD("KillMachine", "ssi", NULL, method_kill_machine, SD_BUS_VTABLE_CAPABILITY(CAP_KILL)),
404449
SD_BUS_METHOD("TerminateMachine", "s", NULL, method_terminate_machine, SD_BUS_VTABLE_CAPABILITY(CAP_KILL)),
405450
SD_BUS_METHOD("GetMachineAddresses", "s", "a(yay)", method_get_machine_addresses, SD_BUS_VTABLE_UNPRIVILEGED),

0 commit comments

Comments
 (0)
X Tutup