os: add process-control syscalls
pipe/pipe2, setpgid, kill, sigprocmask, signalfd, chdir, the wait-status decoders, and the signal/flag constants the subprocess stack needs.
This commit is contained in:
96
lib/os/os.ww
96
lib/os/os.ww
@@ -30,20 +30,27 @@ type nr = enum i64 {
|
||||
OPEN = 2,
|
||||
CLOSE = 3,
|
||||
LSEEK = 8,
|
||||
RTSIGPROCMASK = 14,
|
||||
ACCESS = 21,
|
||||
PIPE = 22,
|
||||
DUP2 = 33,
|
||||
GETPID = 39,
|
||||
FORK = 57,
|
||||
EXECVE = 59,
|
||||
EXIT = 60,
|
||||
WAIT4 = 61,
|
||||
KILL = 62,
|
||||
GETCWD = 79,
|
||||
CHDIR = 80,
|
||||
RENAME = 82,
|
||||
MKDIR = 83,
|
||||
RMDIR = 84,
|
||||
UNLINK = 87,
|
||||
GETCWD = 79,
|
||||
SETPGID = 109,
|
||||
GETDENTS64 = 217,
|
||||
NEWFSTATAT = 262,
|
||||
SIGNALFD4 = 289,
|
||||
PIPE2 = 293,
|
||||
};
|
||||
|
||||
// open(2) flags. Linux values, matching <fcntl.h>. Hare names them
|
||||
@@ -106,6 +113,24 @@ export def STDIN_FILENO: i32 = 0;
|
||||
export def STDOUT_FILENO: i32 = 1;
|
||||
export def STDERR_FILENO: i32 = 2;
|
||||
|
||||
// ref/hare/sys/+linux/types.ha:82,87. Narrow pipe2 flags used by the native
|
||||
// process coordinators; the decimal values are Linux O_CLOEXEC and O_NONBLOCK.
|
||||
export def O_CLOEXEC: i32 = 524288;
|
||||
export def O_NONBLOCK: i32 = 2048;
|
||||
|
||||
// ref/hare/sys/+linux/types.ha:156,162,305,452-454. ww folds Hare's sys role
|
||||
// into os; these are the narrow process-control subset required by the
|
||||
// native test coordinators.
|
||||
export def SIGKILL: i32 = 9;
|
||||
export def SIGINT: i32 = 2;
|
||||
export def SIGTERM: i32 = 15;
|
||||
export def WNOHANG: i32 = 1;
|
||||
export def SIG_BLOCK: i32 = 0;
|
||||
export def SIG_UNBLOCK: i32 = 1;
|
||||
export def SIG_SETMASK: i32 = 2;
|
||||
export def SFD_NONBLOCK: i32 = O_NONBLOCK;
|
||||
export def SFD_CLOEXEC: i32 = O_CLOEXEC;
|
||||
|
||||
fn kpath(p: str) *u8 = {
|
||||
if (p.len + 1 >= PATH_MAX) { return nil: *u8; }; // ENAMETOOLONG
|
||||
let i: i32 = 0;
|
||||
@@ -137,6 +162,20 @@ export fn dup2(oldfd: i32, newfd: i32) i32 = {
|
||||
return syscall2(nr.DUP2, oldfd: i64, newfd: i64): i32;
|
||||
};
|
||||
|
||||
// Linux amd64 pipe(2), ref/hare/sys/+linux/syscallno+x86_64.ha:26.
|
||||
// Hare's public syscall layer prefers pipe2 (syscalls.ha:367), but the
|
||||
// completion-token path needs no flags and os is ww's raw syscall floor.
|
||||
export fn pipe(fds: *[2]i32) i32 = {
|
||||
return syscall1(nr.PIPE, fds: i64): i32;
|
||||
};
|
||||
|
||||
// Linux amd64 pipe2(2), ref/hare/sys/+linux/syscalls.ha:367-369 and
|
||||
// syscallno+x86_64.ha:297. O_CLOEXEC makes launch-status writers disappear
|
||||
// atomically at exec rather than leaking into the executed process tree.
|
||||
export fn pipe2(fds: *[2]i32, flags: i32) i32 = {
|
||||
return syscall2(nr.PIPE2, fds: i64, flags: i64): i32;
|
||||
};
|
||||
|
||||
// Fallible wrappers. The error variant is `oserror` (an i64 carrying
|
||||
// -errno). The sum type makes success/failure explicit and lets
|
||||
// callers `?` the result up the stack.
|
||||
@@ -376,6 +415,33 @@ export fn fork() i32 = {
|
||||
return syscall0(nr.FORK): i32;
|
||||
};
|
||||
|
||||
// Raw Linux process-group and signal primitives. Signatures follow the
|
||||
// pinned Hare syscall order (ref/hare/sys/+linux/syscalls.ha:323,363),
|
||||
// while returning the raw negative errno used throughout this module.
|
||||
export fn setpgid(pid: i32, pgid: i32) i32 = {
|
||||
return syscall2(nr.SETPGID, pid: i64, pgid: i64): i32;
|
||||
};
|
||||
|
||||
export fn kill(pid: i32, signal: i32) i32 = {
|
||||
return syscall2(nr.KILL, pid: i64, signal: i64): i32;
|
||||
};
|
||||
|
||||
// ref/hare/sys/+linux/syscalls.ha:642-650 and types.ha:452-454.
|
||||
// Linux amd64's kernel sigset is one u64; size(u64) keeps the syscall
|
||||
// argument tied to that declared representation.
|
||||
export fn sigprocmask(how: i32, set: *u64, oldset: *u64) i32 = {
|
||||
return syscall4(nr.RTSIGPROCMASK, how: i64, set: i64, oldset: i64,
|
||||
size(u64): i64): i32;
|
||||
};
|
||||
|
||||
// Native coordinators cannot install an rt_sigreturn restorer, so interruption
|
||||
// delivery follows Hare's signalfd shape instead; ref/hare/sys/+linux/
|
||||
// syscalls.ha:636-639 and types.ha:577-578.
|
||||
export fn signalfd(fd: i32, mask: *u64, flags: i32) i32 = {
|
||||
return syscall4(nr.SIGNALFD4, fd: i64, mask: i64,
|
||||
size(u64): i64, flags: i64): i32;
|
||||
};
|
||||
|
||||
// execve(2): on success, does not return. Mirrors Hare's
|
||||
// os::exec::exec path arg (str). argv/envp stay `**u8` — the
|
||||
// kernel takes a NUL-pointer-terminated table of NUL-terminated
|
||||
@@ -393,6 +459,26 @@ export fn wait4(pid: i32, status: *i32, options: i32, rusage: *void) i32 = {
|
||||
options: i64, rusage: i64): i32;
|
||||
};
|
||||
|
||||
// Linux wait-status decoding, ref/hare/sys/+linux/types.ha:312-319.
|
||||
// Keeping exit and signal as separate predicates prevents an expected
|
||||
// numeric exit from accepting a signal with the same collapsed status.
|
||||
export fn wexitstatus(status: i32) i32 = {
|
||||
return (status >> 8i32) & 255i32;
|
||||
};
|
||||
|
||||
export fn wtermsig(status: i32) i32 = {
|
||||
return status & 127i32;
|
||||
};
|
||||
|
||||
export fn wifexited(status: i32) bool = {
|
||||
return wtermsig(status) == 0;
|
||||
};
|
||||
|
||||
export fn wifsignaled(status: i32) bool = {
|
||||
let sig: i32 = wtermsig(status);
|
||||
return sig != 0 && sig != 127i32;
|
||||
};
|
||||
|
||||
// getcwd(2) — Linux flavour. Writes the NUL-terminated cwd into `buf`
|
||||
// and returns the number of bytes written (including the NUL), or a
|
||||
// negative errno. The driver uses it to expand `.` to the cwd's
|
||||
@@ -401,6 +487,14 @@ export fn getcwd(buf: *u8, n: u64) i64 = {
|
||||
return syscall2(nr.GETCWD, buf: i64, n: i64);
|
||||
};
|
||||
|
||||
// Child setup must change cwd without importing the higher fs layer;
|
||||
// ref/hare/sys/+linux/syscalls.ha:251-254.
|
||||
export fn chdir(path: str) i32 = {
|
||||
let p: *u8 = kpath(path);
|
||||
if (p == nil: *u8) { return -36i32; };
|
||||
return syscall1(nr.CHDIR, p: i64): i32;
|
||||
};
|
||||
|
||||
// getdents64(2) — Linux directory enumeration. The fd must be opened
|
||||
// with O_RDONLY on a directory. `buf` receives a packed sequence of
|
||||
// linux_dirent64 records:
|
||||
|
||||
Reference in New Issue
Block a user