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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
|
const std = @import("std");
const assert = std.debug.assert;
const Allocator = std.mem.Allocator;
const builtin = @import("builtin");
const posix = std.posix;
const xev = @import("../global.zig").xev;
const log = std.log.scoped(.flatpak);
/// Returns true if we're running in a Flatpak environment.
pub fn isFlatpak() bool {
// If we're not on Linux then we'll make this comptime false.
if (comptime builtin.os.tag != .linux) return false;
return if (std.fs.accessAbsolute("/.flatpak-info", .{})) true else |_| false;
}
/// A struct to help execute commands on the host via the
/// org.freedesktop.Flatpak.Development DBus module. This uses GIO/GLib
/// under the hood.
///
/// This always spawns its own thread and maintains its own GLib event loop.
/// This makes it easy for the command to behave synchronously similar to
/// std.process.Child.
///
/// There are lots of chances for low-hanging improvements here (automatic
/// pipes, /dev/null, etc.) but this was purpose built for my needs so
/// it doesn't have all of those.
///
/// Requires GIO, GLib to be available and linked.
pub const FlatpakHostCommand = struct {
const fd_t = posix.fd_t;
const EnvMap = std.process.EnvMap;
const c = @cImport({
@cInclude("gio/gio.h");
@cInclude("gio/gunixfdlist.h");
});
/// Argv are the arguments to call on the host with argv[0] being
/// the command to execute.
argv: []const []const u8,
/// The cwd for the new process. If this is not set then it will use
/// the current cwd of the calling process.
cwd: ?[:0]const u8 = null,
/// Environment variables for the child process. If this is null, this
/// does not send any environment variables.
env: ?*const EnvMap = null,
/// File descriptors to send to the child process. It is up to the
/// caller to create the file descriptors and set them up.
stdin: fd_t,
stdout: fd_t,
stderr: fd_t,
/// State of the process. This is updated by the dedicated thread it
/// runs in and is protected by the given lock and condition variable.
state: State = .{ .init = {} },
state_mutex: std.Thread.Mutex = .{},
state_cv: std.Thread.Condition = .{},
/// State the process is in. This can't be inspected directly, you
/// must use getters on the struct to get access.
const State = union(enum) {
/// Initial state
init: void,
/// Error starting. The error message is only available via logs.
/// (This isn't a fundamental limitation, just didn't need the
/// error message yet)
err: void,
/// Process started with the given pid on the host.
started: struct {
pid: u32,
loop_xev: ?*xev.Loop,
completion: ?*Completion,
subscription: c.guint,
loop: *c.GMainLoop,
},
/// Process exited
exited: struct {
pid: u32,
status: u8,
},
};
pub const Completion = struct {
callback: *const fn (ud: ?*anyopaque, l: *xev.Loop, c: *Completion, r: WaitError!u8) void = noopCallback,
c_xev: xev.Completion = .{},
userdata: ?*anyopaque = null,
timer: ?xev.Timer = null,
result: ?WaitError!u8 = null,
};
/// Errors that are possible from us.
pub const Error = error{
FlatpakMustBeStarted,
FlatpakSpawnFail,
FlatpakSetupFail,
FlatpakRPCFail,
};
pub const WaitError = xev.Timer.RunError || Error;
/// Spawn the command. This will start the host command. On return,
/// the pid will be available. This must only be called with the
/// state in "init".
///
/// Precondition: The self pointer MUST be stable.
pub fn spawn(self: *FlatpakHostCommand, alloc: Allocator) !u32 {
const thread = try std.Thread.spawn(.{}, threadMain, .{ self, alloc });
thread.setName("flatpak-host-command") catch {};
// We don't track this thread, it will terminate on its own on command exit
thread.detach();
// Wait for the process to start or error.
self.state_mutex.lock();
defer self.state_mutex.unlock();
while (self.state == .init) self.state_cv.wait(&self.state_mutex);
return switch (self.state) {
.init => unreachable,
.err => Error.FlatpakSpawnFail,
.started => |v| v.pid,
.exited => |v| v.pid,
};
}
/// Wait for the process to end and return the exit status. This
/// can only be called ONCE. Once this returns, the state is reset.
pub fn wait(self: *FlatpakHostCommand) !u8 {
self.state_mutex.lock();
defer self.state_mutex.unlock();
while (true) {
switch (self.state) {
.init => return Error.FlatpakMustBeStarted,
.err => return Error.FlatpakSpawnFail,
.started => {},
.exited => |v| {
self.state = .{ .init = {} };
self.state_cv.broadcast();
return v.status;
},
}
self.state_cv.wait(&self.state_mutex);
}
}
/// Wait for the process to end asynchronously via libxev. This
/// can only be called ONCE.
pub fn waitXev(
self: *FlatpakHostCommand,
loop: *xev.Loop,
completion: *Completion,
comptime Userdata: type,
userdata: ?*Userdata,
comptime cb: *const fn (
ud: ?*Userdata,
l: *xev.Loop,
c: *Completion,
r: WaitError!u8,
) void,
) void {
self.state_mutex.lock();
defer self.state_mutex.unlock();
completion.* = .{
.callback = (struct {
fn callback(
ud_: ?*anyopaque,
l_inner: *xev.Loop,
c_inner: *Completion,
r: WaitError!u8,
) void {
const ud = @as(?*Userdata, if (Userdata == void) null else @ptrCast(@alignCast(ud_)));
@call(.always_inline, cb, .{ ud, l_inner, c_inner, r });
}
}).callback,
.userdata = userdata,
.timer = xev.Timer.init() catch unreachable, // not great, but xev timer can't fail atm
};
switch (self.state) {
.init => completion.result = Error.FlatpakMustBeStarted,
.err => completion.result = Error.FlatpakSpawnFail,
.started => |*v| {
v.loop_xev = loop;
v.completion = completion;
return;
},
.exited => |v| {
completion.result = v.status;
},
}
completion.timer.?.run(
loop,
&completion.c_xev,
0,
anyopaque,
completion.userdata,
(struct {
fn callback(
ud: ?*anyopaque,
l_inner: *xev.Loop,
c_inner: *xev.Completion,
r: xev.Timer.RunError!void,
) xev.CallbackAction {
const c_outer: *Completion = @fieldParentPtr("c_xev", c_inner);
defer if (c_outer.timer) |*t| t.deinit();
const result = if (r) |_| c_outer.result.? else |err| err;
c_outer.callback(ud, l_inner, c_outer, result);
return .disarm;
}
}).callback,
);
}
/// Send a signal to the started command. This does nothing if the
/// command is not in the started state.
pub fn signal(self: *FlatpakHostCommand, sig: u8, pg: bool) !void {
const pid = pid: {
self.state_mutex.lock();
defer self.state_mutex.unlock();
switch (self.state) {
.started => |v| break :pid v.pid,
else => return,
}
};
// Get our bus connection.
var g_err: ?*c.GError = null;
defer if (g_err) |ptr| c.g_error_free(ptr);
const bus = c.g_bus_get_sync(c.G_BUS_TYPE_SESSION, null, &g_err) orelse {
log.warn("signal error getting bus: {s}", .{g_err.?.*.message});
return Error.FlatpakSetupFail;
};
defer c.g_object_unref(bus);
const reply = c.g_dbus_connection_call_sync(
bus,
"org.freedesktop.Flatpak",
"/org/freedesktop/Flatpak/Development",
"org.freedesktop.Flatpak.Development",
"HostCommandSignal",
c.g_variant_new(
"(uub)",
pid,
sig,
@as(c_int, @intCast(@intFromBool(pg))),
),
c.G_VARIANT_TYPE("()"),
c.G_DBUS_CALL_FLAGS_NONE,
c.G_MAXINT,
null,
&g_err,
);
if (g_err != null) {
log.warn("signal send error: {s}", .{g_err.?.*.message});
return;
}
defer c.g_variant_unref(reply);
}
fn threadMain(self: *FlatpakHostCommand, alloc: Allocator) void {
// Create a new thread-local context so that all our sources go
// to this context and we can run our loop correctly.
const ctx = c.g_main_context_new();
defer c.g_main_context_unref(ctx);
c.g_main_context_push_thread_default(ctx);
defer c.g_main_context_pop_thread_default(ctx);
// Get our loop for the current thread
const loop = c.g_main_loop_new(ctx, 1).?;
defer c.g_main_loop_unref(loop);
// Get our bus connection. This has to remain active until we exit
// the thread otherwise our signals won't be called.
var g_err: ?*c.GError = null;
defer if (g_err) |ptr| c.g_error_free(ptr);
const bus = c.g_bus_get_sync(c.G_BUS_TYPE_SESSION, null, &g_err) orelse {
log.warn("spawn error getting bus: {s}", .{g_err.?.*.message});
self.updateState(.{ .err = {} });
return;
};
defer c.g_object_unref(bus);
// Spawn the command first. This will setup all our IO.
self.start(alloc, bus, loop) catch |err| {
log.warn("error starting host command: {}", .{err});
self.updateState(.{ .err = {} });
return;
};
// Run the event loop. It quits in the exit callback.
c.g_main_loop_run(loop);
}
/// Start the command. This will start the host command and set the
/// pid field on success. This will not wait for completion.
///
/// Once this is called, the self pointer MUST remain stable. This
/// requirement is due to using GLib under the covers with callbacks.
fn start(
self: *FlatpakHostCommand,
alloc: Allocator,
bus: *c.GDBusConnection,
loop: *c.GMainLoop,
) !void {
var err: ?*c.GError = null;
defer if (err) |ptr| c.g_error_free(ptr);
var arena_allocator = std.heap.ArenaAllocator.init(alloc);
defer arena_allocator.deinit();
const arena = arena_allocator.allocator();
// Our list of file descriptors that we need to send to the process.
const fd_list = c.g_unix_fd_list_new();
defer c.g_object_unref(fd_list);
if (c.g_unix_fd_list_append(fd_list, self.stdin, &err) < 0) {
log.warn("error adding fd: {s}", .{err.?.*.message});
return Error.FlatpakSetupFail;
}
if (c.g_unix_fd_list_append(fd_list, self.stdout, &err) < 0) {
log.warn("error adding fd: {s}", .{err.?.*.message});
return Error.FlatpakSetupFail;
}
if (c.g_unix_fd_list_append(fd_list, self.stderr, &err) < 0) {
log.warn("error adding fd: {s}", .{err.?.*.message});
return Error.FlatpakSetupFail;
}
// Build our arguments for the file descriptors.
const fd_builder = c.g_variant_builder_new(c.G_VARIANT_TYPE("a{uh}"));
defer c.g_variant_builder_unref(fd_builder);
c.g_variant_builder_add(fd_builder, "{uh}", @as(c_int, 0), self.stdin);
c.g_variant_builder_add(fd_builder, "{uh}", @as(c_int, 1), self.stdout);
c.g_variant_builder_add(fd_builder, "{uh}", @as(c_int, 2), self.stderr);
// Build our env vars
const env_builder = c.g_variant_builder_new(c.G_VARIANT_TYPE("a{ss}"));
defer c.g_variant_builder_unref(env_builder);
if (self.env) |env| {
var it = env.iterator();
while (it.next()) |pair| {
const key = try arena.dupeZ(u8, pair.key_ptr.*);
const value = try arena.dupeZ(u8, pair.value_ptr.*);
c.g_variant_builder_add(env_builder, "{ss}", key.ptr, value.ptr);
}
}
// Build our args
const args = try arena.alloc(?[*:0]u8, self.argv.len + 1);
for (0.., self.argv) |i, arg| {
const argZ = try arena.dupeZ(u8, arg);
args[i] = argZ.ptr;
}
args[args.len - 1] = null;
// Get the cwd in case we don't have ours set. A small optimization
// would be to do this only if we need it but this isn't a
// common code path.
const g_cwd = c.g_get_current_dir();
defer c.g_free(g_cwd);
// The params for our RPC call
const params = c.g_variant_new(
"(^ay^aay@a{uh}@a{ss}u)",
@as(*const anyopaque, if (self.cwd) |*cwd| cwd.ptr else g_cwd),
args.ptr,
c.g_variant_builder_end(fd_builder),
c.g_variant_builder_end(env_builder),
@as(c_int, 0),
);
_ = c.g_variant_ref_sink(params); // take ownership
defer c.g_variant_unref(params);
// Subscribe to exit notifications
const subscription_id = c.g_dbus_connection_signal_subscribe(
bus,
"org.freedesktop.Flatpak",
"org.freedesktop.Flatpak.Development",
"HostCommandExited",
"/org/freedesktop/Flatpak/Development",
null,
0,
onExit,
self,
null,
);
errdefer c.g_dbus_connection_signal_unsubscribe(bus, subscription_id);
// Go!
const reply = c.g_dbus_connection_call_with_unix_fd_list_sync(
bus,
"org.freedesktop.Flatpak",
"/org/freedesktop/Flatpak/Development",
"org.freedesktop.Flatpak.Development",
"HostCommand",
params,
c.G_VARIANT_TYPE("(u)"),
c.G_DBUS_CALL_FLAGS_NONE,
c.G_MAXINT,
fd_list,
null,
null,
&err,
) orelse {
log.warn("Flatpak.HostCommand failed: {s}", .{err.?.*.message});
return Error.FlatpakRPCFail;
};
defer c.g_variant_unref(reply);
var pid: u32 = 0;
c.g_variant_get(reply, "(u)", &pid);
log.debug("HostCommand started pid={} subscription={}", .{
pid,
subscription_id,
});
self.updateState(.{
.started = .{
.pid = pid,
.subscription = subscription_id,
.loop = loop,
.completion = null,
.loop_xev = null,
},
});
}
/// Helper to update the state and notify waiters via the cv.
fn updateState(self: *FlatpakHostCommand, state: State) void {
self.state_mutex.lock();
defer self.state_mutex.unlock();
defer self.state_cv.broadcast();
self.state = state;
}
fn onExit(
bus: ?*c.GDBusConnection,
_: [*c]const u8,
_: [*c]const u8,
_: [*c]const u8,
_: [*c]const u8,
params: ?*c.GVariant,
ud: ?*anyopaque,
) callconv(.c) void {
const self = @as(*FlatpakHostCommand, @ptrCast(@alignCast(ud)));
const state = state: {
self.state_mutex.lock();
defer self.state_mutex.unlock();
break :state self.state.started;
};
var pid: u32 = 0;
var exit_status_raw: u32 = 0;
c.g_variant_get(params.?, "(uu)", &pid, &exit_status_raw);
if (state.pid != pid) return;
const exit_status = posix.W.EXITSTATUS(exit_status_raw);
// Update our state
self.updateState(.{
.exited = .{
.pid = pid,
.status = exit_status,
},
});
if (state.completion) |completion| {
completion.result = exit_status;
completion.timer.?.run(
state.loop_xev.?,
&completion.c_xev,
0,
anyopaque,
completion.userdata,
(struct {
fn callback(
ud_inner: ?*anyopaque,
l_inner: *xev.Loop,
c_inner: *xev.Completion,
r: xev.Timer.RunError!void,
) xev.CallbackAction {
const c_outer: *Completion = @fieldParentPtr("c_xev", c_inner);
defer if (c_outer.timer) |*t| t.deinit();
const result = if (r) |_| c_outer.result.? else |err| err;
c_outer.callback(ud_inner, l_inner, c_outer, result);
return .disarm;
}
}).callback,
);
}
log.debug("HostCommand exited pid={} status={}", .{ pid, exit_status });
// We're done now, so we can unsubscribe
c.g_dbus_connection_signal_unsubscribe(bus.?, state.subscription);
// We are also done with our loop so we can exit.
c.g_main_loop_quit(state.loop);
}
fn noopCallback(_: ?*anyopaque, _: *xev.Loop, _: *Completion, _: WaitError!u8) void {}
};
|