river: make spawn command take only one argument

Currently the spawn command takes any number of arguments and naively
joins them together with spaces before passing them as the single
argument of `/bin/sh -c`. This however produces unexpected results as
soon as shell quoting gets involved in the arguments passed to spawn.
For example, running

riverctl spawn foo "bar baz"

will execute `/bin/sh -c "foo bar baz"`, unexpectedly splitting bar and
baz into separate arguments. To avoid this confusion, make the spawn
command take only a single argument, forcing the user to quote properly
to spawn multi-argument commands.
This commit is contained in:
Isaac Freund
2021-07-16 00:00:28 +02:00
parent dfa2471141
commit b7e15a8ef6
3 changed files with 17 additions and 19 deletions

View File

@ -30,11 +30,9 @@ pub fn spawn(
out: *?[]const u8,
) Error!void {
if (args.len < 2) return Error.NotEnoughArguments;
if (args.len > 2) return Error.TooManyArguments;
const cmd = try std.mem.joinZ(allocator, " ", args[1..]);
defer allocator.free(cmd);
const child_args = [_:null]?[*:0]const u8{ "/bin/sh", "-c", cmd, null };
const child_args = [_:null]?[*:0]const u8{ "/bin/sh", "-c", args[1], null };
const pid = std.os.fork() catch {
out.* = try std.fmt.allocPrint(allocator, "fork/execve failed", .{});