authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-12-15 04:19:12-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-12-15 14:23:32-07:00
log877a1f2a2986e02269c64a05456dff521da27ac1
treef942b400d4edf23e64b70f81b3ce4660c542e924
parent3532abe0c605746e048dae4bf31d0167151bbfd7

std.os: fix error codes for execve

execve can return EBADLIB on Linux. I observed this when passing an x86_64 interpreter path to qemu-i386. This error code is Linux and Solaris-only. I came up with an improved pattern for dealing with OS-specific error codes.

1 files changed, 12 insertions(+), 27 deletions(-)

lib/std/os.zig+12-27
......@@ -1544,32 +1544,6 @@ pub fn execveZ(
15441544 child_argv: [*:null]const ?[*:0]const u8,
15451545 envp: [*:null]const ?[*:0]const u8,
15461546) ExecveError {
1547 if (comptime builtin.target.isDarwin()) {
1548 // Darwin gets its own branch because it has BADEXEC and BADARCH
1549 // which are beyond posix.
1550 switch (errno(system.execve(path, child_argv, envp))) {
1551 .SUCCESS => unreachable,
1552 .FAULT => unreachable,
1553 .@"2BIG" => return error.SystemResources,
1554 .MFILE => return error.ProcessFdQuotaExceeded,
1555 .NAMETOOLONG => return error.NameTooLong,
1556 .NFILE => return error.SystemFdQuotaExceeded,
1557 .NOMEM => return error.SystemResources,
1558 .ACCES => return error.AccessDenied,
1559 .PERM => return error.AccessDenied,
1560 .INVAL => return error.InvalidExe,
1561 .NOEXEC => return error.InvalidExe,
1562 .BADEXEC => return error.InvalidExe,
1563 .BADARCH => return error.InvalidExe,
1564 .IO => return error.FileSystem,
1565 .LOOP => return error.FileSystem,
1566 .ISDIR => return error.IsDir,
1567 .NOENT => return error.FileNotFound,
1568 .NOTDIR => return error.NotDir,
1569 .TXTBSY => return error.FileBusy,
1570 else => |err| return unexpectedErrno(err),
1571 }
1572 }
15731547 switch (errno(system.execve(path, child_argv, envp))) {
15741548 .SUCCESS => unreachable,
15751549 .FAULT => unreachable,
......@@ -1588,7 +1562,18 @@ pub fn execveZ(
15881562 .NOENT => return error.FileNotFound,
15891563 .NOTDIR => return error.NotDir,
15901564 .TXTBSY => return error.FileBusy,
1591 else => |err| return unexpectedErrno(err),
1565 else => |err| switch (builtin.os.tag) {
1566 .macos, .ios, .tvos, .watchos => switch (err) {
1567 .BADEXEC => return error.InvalidExe,
1568 .BADARCH => return error.InvalidExe,
1569 else => return unexpectedErrno(err),
1570 },
1571 .linux, .solaris => switch (err) {
1572 .LIBBAD => return error.InvalidExe,
1573 else => return unexpectedErrno(err),
1574 },
1575 else => return unexpectedErrno(err),
1576 },
15921577 }
15931578}
15941579