Commit 877a1f2a29

Andrew Kelley <andrew@ziglang.org>
2021-12-15 12:19:12
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 parent 3532abe
Changed files (1)
lib
std
lib/std/os.zig
@@ -1544,32 +1544,6 @@ pub fn execveZ(
     child_argv: [*:null]const ?[*:0]const u8,
     envp: [*:null]const ?[*:0]const u8,
 ) ExecveError {
-    if (comptime builtin.target.isDarwin()) {
-        // Darwin gets its own branch because it has BADEXEC and BADARCH
-        // which are beyond posix.
-        switch (errno(system.execve(path, child_argv, envp))) {
-            .SUCCESS => unreachable,
-            .FAULT => unreachable,
-            .@"2BIG" => return error.SystemResources,
-            .MFILE => return error.ProcessFdQuotaExceeded,
-            .NAMETOOLONG => return error.NameTooLong,
-            .NFILE => return error.SystemFdQuotaExceeded,
-            .NOMEM => return error.SystemResources,
-            .ACCES => return error.AccessDenied,
-            .PERM => return error.AccessDenied,
-            .INVAL => return error.InvalidExe,
-            .NOEXEC => return error.InvalidExe,
-            .BADEXEC => return error.InvalidExe,
-            .BADARCH => return error.InvalidExe,
-            .IO => return error.FileSystem,
-            .LOOP => return error.FileSystem,
-            .ISDIR => return error.IsDir,
-            .NOENT => return error.FileNotFound,
-            .NOTDIR => return error.NotDir,
-            .TXTBSY => return error.FileBusy,
-            else => |err| return unexpectedErrno(err),
-        }
-    }
     switch (errno(system.execve(path, child_argv, envp))) {
         .SUCCESS => unreachable,
         .FAULT => unreachable,
@@ -1588,7 +1562,18 @@ pub fn execveZ(
         .NOENT => return error.FileNotFound,
         .NOTDIR => return error.NotDir,
         .TXTBSY => return error.FileBusy,
-        else => |err| return unexpectedErrno(err),
+        else => |err| switch (builtin.os.tag) {
+            .macos, .ios, .tvos, .watchos => switch (err) {
+                .BADEXEC => return error.InvalidExe,
+                .BADARCH => return error.InvalidExe,
+                else => return unexpectedErrno(err),
+            },
+            .linux, .solaris => switch (err) {
+                .LIBBAD => return error.InvalidExe,
+                else => return unexpectedErrno(err),
+            },
+            else => return unexpectedErrno(err),
+        },
     }
 }