Commit acefcdbca5

Andrew Kelley <superjoe30@gmail.com>
2018-10-02 20:08:32
add std.os.linux.vfork and std.os.linux.exit_group
1 parent 364bc66
Changed files (2)
std
std/os/linux/index.zig
@@ -719,6 +719,15 @@ pub fn fork() usize {
     return syscall0(SYS_fork);
 }
 
+/// This must be inline, and inline call the syscall function, because if the
+/// child does a return it will clobber the parent's stack.
+/// It is advised to avoid this function and use clone instead, because
+/// the compiler is not aware of how vfork affects control flow and you may
+/// see different results in optimized builds.
+pub inline fn vfork() usize {
+    return @inlineCall(syscall0, SYS_vfork);
+}
+
 pub fn futex_wait(uaddr: usize, futex_op: u32, val: i32, timeout: ?*timespec) usize {
     return syscall4(SYS_futex, uaddr, futex_op, @bitCast(u32, val), @ptrToInt(timeout));
 }
@@ -883,6 +892,11 @@ pub fn exit(status: i32) noreturn {
     unreachable;
 }
 
+pub fn exit_group(status: i32) noreturn {
+    _ = syscall1(SYS_exit_group, @bitCast(usize, isize(status)));
+    unreachable;
+}
+
 pub fn getrandom(buf: [*]u8, count: usize, flags: u32) usize {
     return syscall3(SYS_getrandom, @ptrToInt(buf), count, @intCast(usize, flags));
 }
std/os/index.zig
@@ -634,18 +634,25 @@ pub const PosixExecveError = error{
 
 fn posixExecveErrnoToErr(err: usize) PosixExecveError {
     assert(err > 0);
-    return switch (err) {
+    switch (err) {
         posix.EFAULT => unreachable,
-        posix.E2BIG, posix.EMFILE, posix.ENAMETOOLONG, posix.ENFILE, posix.ENOMEM => error.SystemResources,
-        posix.EACCES, posix.EPERM => error.AccessDenied,
-        posix.EINVAL, posix.ENOEXEC => error.InvalidExe,
-        posix.EIO, posix.ELOOP => error.FileSystem,
-        posix.EISDIR => error.IsDir,
-        posix.ENOENT => error.FileNotFound,
-        posix.ENOTDIR => error.NotDir,
-        posix.ETXTBSY => error.FileBusy,
-        else => unexpectedErrorPosix(err),
-    };
+        posix.E2BIG => return error.SystemResources,
+        posix.EMFILE => return error.SystemResources,
+        posix.ENAMETOOLONG => return error.SystemResources,
+        posix.ENFILE => return error.SystemResources,
+        posix.ENOMEM => return error.SystemResources,
+        posix.EACCES => return error.AccessDenied,
+        posix.EPERM => return error.AccessDenied,
+        posix.EINVAL => return error.InvalidExe,
+        posix.ENOEXEC => return error.InvalidExe,
+        posix.EIO => return error.FileSystem,
+        posix.ELOOP => return error.FileSystem,
+        posix.EISDIR => return error.IsDir,
+        posix.ENOENT => return error.FileNotFound,
+        posix.ENOTDIR => return error.NotDir,
+        posix.ETXTBSY => return error.FileBusy,
+        else => return unexpectedErrorPosix(err),
+    }
 }
 
 pub var linux_aux_raw = []usize{0} ** 38;