diff options
| author | gingerBill <gingerBill@users.noreply.github.com> | 2023-02-28 10:48:09 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-02-28 10:48:09 +0000 |
| commit | f0950e2286d355e951402f1bf66015bbc69392f3 (patch) | |
| tree | 4d1709aebc55a60fe51126d84316cd3d30e8ed99 | |
| parent | 76cb3b7874c86eec9c610d74d296d0adfeb4315e (diff) | |
| parent | edd78ae129c967bcc25704434551e221074cd92a (diff) | |
Merge pull request #2355 from colrdavidson/os_linux_cleanup
os/linux cleanup
| -rw-r--r-- | core/crypto/rand_linux.odin | 2 | ||||
| -rw-r--r-- | core/os/os_linux.odin | 198 | ||||
| -rw-r--r-- | core/sync/primitives.odin | 2 | ||||
| -rw-r--r-- | core/sys/unix/syscalls_linux.odin | 39 |
4 files changed, 71 insertions, 170 deletions
diff --git a/core/crypto/rand_linux.odin b/core/crypto/rand_linux.odin index 4d1183757..e5c194220 100644 --- a/core/crypto/rand_linux.odin +++ b/core/crypto/rand_linux.odin @@ -12,7 +12,7 @@ _rand_bytes :: proc (dst: []byte) { for l > 0 { to_read := min(l, _MAX_PER_CALL_BYTES) - ret := unix.sys_getrandom(raw_data(dst), to_read, 0) + ret := unix.sys_getrandom(raw_data(dst), uint(to_read), 0) if ret < 0 { switch os.Errno(-ret) { case os.EINTR: diff --git a/core/os/os_linux.odin b/core/os/os_linux.odin index e4865a2ff..cef394b95 100644 --- a/core/os/os_linux.odin +++ b/core/os/os_linux.odin @@ -270,136 +270,6 @@ AT_FDCWD :: ~uintptr(99) /* -100 */ AT_REMOVEDIR :: uintptr(0x200) AT_SYMLINK_NOFOLLOW :: uintptr(0x100) -_unix_personality :: proc(persona: u64) -> int { - return int(intrinsics.syscall(unix.SYS_personality, uintptr(persona))) -} - -_unix_fork :: proc() -> Pid { - when ODIN_ARCH != .arm64 { - res := int(intrinsics.syscall(unix.SYS_fork)) - } else { - res := int(intrinsics.syscall(unix.SYS_clone, unix.SIGCHLD)) - } - return -1 if res < 0 else Pid(res) -} - -_unix_open :: proc(path: cstring, flags: int, mode: int = 0o000) -> Handle { - when ODIN_ARCH != .arm64 { - res := int(intrinsics.syscall(unix.SYS_open, uintptr(rawptr(path)), uintptr(flags), uintptr(mode))) - } else { // NOTE: arm64 does not have open - res := int(intrinsics.syscall(unix.SYS_openat, AT_FDCWD, uintptr(rawptr(path)), uintptr(flags), uintptr(mode))) - } - return -1 if res < 0 else Handle(res) -} - -_unix_close :: proc(fd: Handle) -> int { - return int(intrinsics.syscall(unix.SYS_close, uintptr(fd))) -} - -_unix_read :: proc(fd: Handle, buf: rawptr, size: uint) -> int { - return int(intrinsics.syscall(unix.SYS_read, uintptr(fd), uintptr(buf), uintptr(size))) -} - -_unix_write :: proc(fd: Handle, buf: rawptr, size: uint) -> int { - return int(intrinsics.syscall(unix.SYS_write, uintptr(fd), uintptr(buf), uintptr(size))) -} - -_unix_seek :: proc(fd: Handle, offset: i64, whence: int) -> i64 { - when ODIN_ARCH == .amd64 || ODIN_ARCH == .arm64 { - return i64(intrinsics.syscall(unix.SYS_lseek, uintptr(fd), uintptr(offset), uintptr(whence))) - } else { - low := uintptr(offset & 0xFFFFFFFF) - high := uintptr(offset >> 32) - result: i64 - res := i64(intrinsics.syscall(unix.SYS__llseek, uintptr(fd), high, low, uintptr(&result), uintptr(whence))) - return -1 if res < 0 else result - } -} - -_unix_stat :: proc(path: cstring, stat: ^OS_Stat) -> int { - when ODIN_ARCH == .amd64 { - return int(intrinsics.syscall(unix.SYS_stat, uintptr(rawptr(path)), uintptr(stat))) - } else when ODIN_ARCH != .arm64 { - return int(intrinsics.syscall(unix.SYS_stat64, uintptr(rawptr(path)), uintptr(stat))) - } else { // NOTE: arm64 does not have stat - return int(intrinsics.syscall(unix.SYS_fstatat, AT_FDCWD, uintptr(rawptr(path)), uintptr(stat), 0)) - } -} - -_unix_fstat :: proc(fd: Handle, stat: ^OS_Stat) -> int { - when ODIN_ARCH == .amd64 || ODIN_ARCH == .arm64 { - return int(intrinsics.syscall(unix.SYS_fstat, uintptr(fd), uintptr(stat))) - } else { - return int(intrinsics.syscall(unix.SYS_fstat64, uintptr(fd), uintptr(stat))) - } -} - -_unix_lstat :: proc(path: cstring, stat: ^OS_Stat) -> int { - when ODIN_ARCH == .amd64 { - return int(intrinsics.syscall(unix.SYS_lstat, uintptr(rawptr(path)), uintptr(stat))) - } else when ODIN_ARCH != .arm64 { - return int(intrinsics.syscall(unix.SYS_lstat64, uintptr(rawptr(path)), uintptr(stat))) - } else { // NOTE: arm64 does not have any lstat - return int(intrinsics.syscall(unix.SYS_fstatat, AT_FDCWD, uintptr(rawptr(path)), uintptr(stat), AT_SYMLINK_NOFOLLOW)) - } -} - -_unix_readlink :: proc(path: cstring, buf: rawptr, bufsiz: uint) -> int { - when ODIN_ARCH != .arm64 { - return int(intrinsics.syscall(unix.SYS_readlink, uintptr(rawptr(path)), uintptr(buf), uintptr(bufsiz))) - } else { // NOTE: arm64 does not have readlink - return int(intrinsics.syscall(unix.SYS_readlinkat, AT_FDCWD, uintptr(rawptr(path)), uintptr(buf), uintptr(bufsiz))) - } -} - -_unix_access :: proc(path: cstring, mask: int) -> int { - when ODIN_ARCH != .arm64 { - return int(intrinsics.syscall(unix.SYS_access, uintptr(rawptr(path)), uintptr(mask))) - } else { // NOTE: arm64 does not have access - return int(intrinsics.syscall(unix.SYS_faccessat, AT_FDCWD, uintptr(rawptr(path)), uintptr(mask))) - } -} - -_unix_getcwd :: proc(buf: rawptr, size: uint) -> int { - return int(intrinsics.syscall(unix.SYS_getcwd, uintptr(buf), uintptr(size))) -} - -_unix_chdir :: proc(path: cstring) -> int { - return int(intrinsics.syscall(unix.SYS_chdir, uintptr(rawptr(path)))) -} - -_unix_rename :: proc(old, new: cstring) -> int { - when ODIN_ARCH != .arm64 { - return int(intrinsics.syscall(unix.SYS_rename, uintptr(rawptr(old)), uintptr(rawptr(new)))) - } else { // NOTE: arm64 does not have rename - return int(intrinsics.syscall(unix.SYS_renameat, AT_FDCWD, uintptr(rawptr(old)), uintptr(rawptr(new)))) - } -} - -_unix_unlink :: proc(path: cstring) -> int { - when ODIN_ARCH != .arm64 { - return int(intrinsics.syscall(unix.SYS_unlink, uintptr(rawptr(path)))) - } else { // NOTE: arm64 does not have unlink - return int(intrinsics.syscall(unix.SYS_unlinkat, AT_FDCWD, uintptr(rawptr(path)), 0)) - } -} - -_unix_rmdir :: proc(path: cstring) -> int { - when ODIN_ARCH != .arm64 { - return int(intrinsics.syscall(unix.SYS_rmdir, uintptr(rawptr(path)))) - } else { // NOTE: arm64 does not have rmdir - return int(intrinsics.syscall(unix.SYS_unlinkat, AT_FDCWD, uintptr(rawptr(path)), AT_REMOVEDIR)) - } -} - -_unix_mkdir :: proc(path: cstring, mode: u32) -> int { - when ODIN_ARCH != .arm64 { - return int(intrinsics.syscall(unix.SYS_mkdir, uintptr(rawptr(path)), uintptr(mode))) - } else { // NOTE: arm64 does not have mkdir - return int(intrinsics.syscall(unix.SYS_mkdirat, AT_FDCWD, uintptr(rawptr(path)), uintptr(mode))) - } -} - foreign libc { @(link_name="__errno_location") __errno_location :: proc() -> ^int --- @@ -415,6 +285,7 @@ foreign libc { @(link_name="free") _unix_free :: proc(ptr: rawptr) --- @(link_name="realloc") _unix_realloc :: proc(ptr: rawptr, size: c.size_t) -> rawptr --- + @(link_name="execvp") _unix_execvp :: proc(path: cstring, argv: [^]cstring) -> int --- @(link_name="getenv") _unix_getenv :: proc(cstring) -> cstring --- @(link_name="putenv") _unix_putenv :: proc(cstring) -> c.int --- @(link_name="realpath") _unix_realpath :: proc(path: cstring, resolved_path: rawptr) -> rawptr --- @@ -447,7 +318,7 @@ get_last_error :: proc "contextless" () -> int { } personality :: proc(persona: u64) -> (Errno) { - res := _unix_personality(persona) + res := unix.sys_personality(persona) if res == -1 { return _get_errno(res) } @@ -455,24 +326,37 @@ personality :: proc(persona: u64) -> (Errno) { } fork :: proc() -> (Pid, Errno) { - pid := _unix_fork() + pid := unix.sys_fork() if pid == -1 { - return -1, _get_errno(int(pid)) + return -1, _get_errno(pid) } - return pid, ERROR_NONE + return Pid(pid), ERROR_NONE +} + +execvp :: proc(path: string, args: []string) -> Errno { + path_cstr := strings.clone_to_cstring(path, context.temp_allocator) + + args_cstrs := make([]cstring, len(args) + 2, context.temp_allocator) + args_cstrs[0] = strings.clone_to_cstring(path, context.temp_allocator) + for i := 0; i < len(args); i += 1 { + args_cstrs[i+1] = strings.clone_to_cstring(args[i], context.temp_allocator) + } + + _unix_execvp(path_cstr, raw_data(args_cstrs)) + return Errno(get_last_error()) } -open :: proc(path: string, flags: int = O_RDONLY, mode: int = 0) -> (Handle, Errno) { +open :: proc(path: string, flags: int = O_RDONLY, mode: int = 0o000) -> (Handle, Errno) { cstr := strings.clone_to_cstring(path, context.temp_allocator) - handle := _unix_open(cstr, flags, mode) + handle := unix.sys_open(cstr, flags, uint(mode)) if handle < 0 { - return INVALID_HANDLE, _get_errno(int(handle)) + return INVALID_HANDLE, _get_errno(handle) } - return handle, ERROR_NONE + return Handle(handle), ERROR_NONE } close :: proc(fd: Handle) -> Errno { - return _get_errno(_unix_close(fd)) + return _get_errno(unix.sys_close(int(fd))) } read :: proc(fd: Handle, data: []byte) -> (int, Errno) { @@ -480,7 +364,7 @@ read :: proc(fd: Handle, data: []byte) -> (int, Errno) { return 0, ERROR_NONE } - bytes_read := _unix_read(fd, raw_data(data), c.size_t(len(data))) + bytes_read := unix.sys_read(int(fd), raw_data(data), len(data)) if bytes_read < 0 { return -1, _get_errno(bytes_read) } @@ -492,7 +376,7 @@ write :: proc(fd: Handle, data: []byte) -> (int, Errno) { return 0, ERROR_NONE } - bytes_written := _unix_write(fd, raw_data(data), uint(len(data))) + bytes_written := unix.sys_write(int(fd), raw_data(data), len(data)) if bytes_written < 0 { return -1, _get_errno(bytes_written) } @@ -503,7 +387,7 @@ read_at :: proc(fd: Handle, data: []byte, offset: i64) -> (int, Errno) { return 0, ERROR_NONE } - bytes_read := unix.sys_pread(int(fd), raw_data(data), c.size_t(len(data)), offset) + bytes_read := unix.sys_pread(int(fd), raw_data(data), len(data), offset) if bytes_read < 0 { return -1, _get_errno(bytes_read) } @@ -523,17 +407,17 @@ write_at :: proc(fd: Handle, data: []byte, offset: i64) -> (int, Errno) { } seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) { - res := _unix_seek(fd, offset, whence) + res := unix.sys_lseek(int(fd), offset, whence) if res < 0 { return -1, _get_errno(int(res)) } - return res, ERROR_NONE + return i64(res), ERROR_NONE } file_size :: proc(fd: Handle) -> (i64, Errno) { // deliberately uninitialized; the syscall fills this buffer for us s: OS_Stat = --- - result := _unix_fstat(fd, &s) + result := unix.sys_fstat(int(fd), rawptr(&s)) if result < 0 { return 0, _get_errno(result) } @@ -543,22 +427,22 @@ file_size :: proc(fd: Handle) -> (i64, Errno) { rename :: proc(old_path, new_path: string) -> Errno { old_path_cstr := strings.clone_to_cstring(old_path, context.temp_allocator) new_path_cstr := strings.clone_to_cstring(new_path, context.temp_allocator) - return _get_errno(_unix_rename(old_path_cstr, new_path_cstr)) + return _get_errno(unix.sys_rename(old_path_cstr, new_path_cstr)) } remove :: proc(path: string) -> Errno { path_cstr := strings.clone_to_cstring(path, context.temp_allocator) - return _get_errno(_unix_unlink(path_cstr)) + return _get_errno(unix.sys_unlink(path_cstr)) } make_directory :: proc(path: string, mode: u32 = 0o775) -> Errno { path_cstr := strings.clone_to_cstring(path, context.temp_allocator) - return _get_errno(_unix_mkdir(path_cstr, mode)) + return _get_errno(unix.sys_mkdir(path_cstr, uint(mode))) } remove_directory :: proc(path: string) -> Errno { path_cstr := strings.clone_to_cstring(path, context.temp_allocator) - return _get_errno(_unix_rmdir(path_cstr)) + return _get_errno(unix.sys_rmdir(path_cstr)) } is_file_handle :: proc(fd: Handle) -> bool { @@ -611,7 +495,7 @@ is_dir :: proc {is_dir_path, is_dir_handle} exists :: proc(path: string) -> bool { cpath := strings.clone_to_cstring(path, context.temp_allocator) - res := _unix_access(cpath, O_RDONLY) + res := unix.sys_access(cpath, O_RDONLY) return res == 0 } @@ -649,7 +533,7 @@ _stat :: proc(path: string) -> (OS_Stat, Errno) { // deliberately uninitialized; the syscall fills this buffer for us s: OS_Stat = --- - result := _unix_stat(cstr, &s) + result := unix.sys_stat(cstr, &s) if result < 0 { return s, _get_errno(result) } @@ -662,7 +546,7 @@ _lstat :: proc(path: string) -> (OS_Stat, Errno) { // deliberately uninitialized; the syscall fills this buffer for us s: OS_Stat = --- - result := _unix_lstat(cstr, &s) + result := unix.sys_lstat(cstr, &s) if result < 0 { return s, _get_errno(result) } @@ -673,7 +557,7 @@ _lstat :: proc(path: string) -> (OS_Stat, Errno) { _fstat :: proc(fd: Handle) -> (OS_Stat, Errno) { // deliberately uninitialized; the syscall fills this buffer for us s: OS_Stat = --- - result := _unix_fstat(fd, &s) + result := unix.sys_fstat(int(fd), rawptr(&s)) if result < 0 { return s, _get_errno(result) } @@ -730,7 +614,7 @@ _readlink :: proc(path: string) -> (string, Errno) { bufsz : uint = 256 buf := make([]byte, bufsz) for { - rc := _unix_readlink(path_cstr, &(buf[0]), bufsz) + rc := unix.sys_readlink(path_cstr, &(buf[0]), bufsz) if rc < 0 { delete(buf) return "", _get_errno(rc) @@ -777,7 +661,7 @@ absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Errno) { access :: proc(path: string, mask: int) -> (bool, Errno) { cstr := strings.clone_to_cstring(path, context.temp_allocator) - result := _unix_access(cstr, mask) + result := unix.sys_access(cstr, mask) if result < 0 { return false, _get_errno(result) } @@ -845,7 +729,7 @@ get_current_directory :: proc() -> string { page_size := get_page_size() buf := make([dynamic]u8, page_size) for { - #no_bounds_check res := _unix_getcwd(&buf[0], uint(len(buf))) + #no_bounds_check res := unix.sys_getcwd(&buf[0], uint(len(buf))) if res >= 0 { return strings.string_from_nul_terminated_ptr(&buf[0], len(buf)) @@ -861,7 +745,7 @@ get_current_directory :: proc() -> string { set_current_directory :: proc(path: string) -> (err: Errno) { cstr := strings.clone_to_cstring(path, context.temp_allocator) - res := _unix_chdir(cstr) + res := unix.sys_chdir(cstr) if res < 0 { return _get_errno(res) } diff --git a/core/sync/primitives.odin b/core/sync/primitives.odin index 00d7812a8..8d6ce6e48 100644 --- a/core/sync/primitives.odin +++ b/core/sync/primitives.odin @@ -236,4 +236,4 @@ _panic :: proc "contextless" (msg: string) -> ! { runtime.print_string(msg) runtime.print_byte('\n') runtime.trap() -}
\ No newline at end of file +} diff --git a/core/sys/unix/syscalls_linux.odin b/core/sys/unix/syscalls_linux.odin index 1e89da2d6..91c0accfa 100644 --- a/core/sys/unix/syscalls_linux.odin +++ b/core/sys/unix/syscalls_linux.odin @@ -1563,6 +1563,9 @@ MADV_WIPEONFORK :: 18 MADV_KEEPONFORK :: 19 MADV_HWPOISON :: 100 +// pipe2 flags +O_CLOEXEC :: 0o2000000 + // perf event data Perf_Sample :: struct #raw_union { period: u64, @@ -1713,14 +1716,14 @@ Perf_Flag :: enum u64 { } sys_gettid :: proc "contextless" () -> int { - return cast(int)intrinsics.syscall(SYS_gettid) + return int(intrinsics.syscall(SYS_gettid)) } -sys_getrandom :: proc "contextless" (buf: [^]byte, buflen: int, flags: uint) -> int { - return cast(int)intrinsics.syscall(SYS_getrandom, uintptr(buf), uintptr(buflen), uintptr(flags)) +sys_getrandom :: proc "contextless" (buf: [^]byte, buflen: uint, flags: int) -> int { + return int(intrinsics.syscall(SYS_getrandom, uintptr(buf), uintptr(buflen), uintptr(flags))) } -sys_open :: proc "contextless" (path: cstring, flags: int, mode: int = 0o000) -> int { +sys_open :: proc "contextless" (path: cstring, flags: int, mode: uint = 0o000) -> int { when ODIN_ARCH != .arm64 { return int(intrinsics.syscall(SYS_open, uintptr(rawptr(path)), uintptr(flags), uintptr(mode))) } else { // NOTE: arm64 does not have open @@ -1728,7 +1731,7 @@ sys_open :: proc "contextless" (path: cstring, flags: int, mode: int = 0o000) -> } } -sys_openat :: proc "contextless" (dfd: int, path: cstring, flags: int, mode: int = 0o000) -> int { +sys_openat :: proc "contextless" (dfd: int, path: cstring, flags: int, mode: uint = 0o000) -> int { return int(intrinsics.syscall(SYS_openat, uintptr(dfd), uintptr(rawptr(path)), uintptr(flags), uintptr(mode))) } @@ -1840,7 +1843,7 @@ sys_fchdir :: proc "contextless" (fd: int) -> int { return int(intrinsics.syscall(SYS_fchdir, uintptr(fd))) } -sys_chmod :: proc "contextless" (path: cstring, mode: int) -> int { +sys_chmod :: proc "contextless" (path: cstring, mode: uint) -> int { when ODIN_ARCH != .arm64 { return int(intrinsics.syscall(SYS_chmod, uintptr(rawptr(path)), uintptr(mode))) } else { // NOTE: arm64 does not have chmod @@ -1848,7 +1851,7 @@ sys_chmod :: proc "contextless" (path: cstring, mode: int) -> int { } } -sys_fchmod :: proc "contextless" (fd: int, mode: int) -> int { +sys_fchmod :: proc "contextless" (fd: int, mode: uint) -> int { return int(intrinsics.syscall(SYS_fchmod, uintptr(fd), uintptr(mode))) } @@ -1908,7 +1911,7 @@ sys_rmdir :: proc "contextless" (path: cstring) -> int { } } -sys_mkdir :: proc "contextless" (path: cstring, mode: int) -> int { +sys_mkdir :: proc "contextless" (path: cstring, mode: uint) -> int { when ODIN_ARCH != .arm64 { return int(intrinsics.syscall(SYS_mkdir, uintptr(rawptr(path)), uintptr(mode))) } else { // NOTE: arm64 does not have mkdir @@ -1916,11 +1919,11 @@ sys_mkdir :: proc "contextless" (path: cstring, mode: int) -> int { } } -sys_mkdirat :: proc "contextless" (dfd: int, path: cstring, mode: int) -> int { +sys_mkdirat :: proc "contextless" (dfd: int, path: cstring, mode: uint) -> int { return int(intrinsics.syscall(SYS_mkdirat, uintptr(dfd), uintptr(rawptr(path)), uintptr(mode))) } -sys_mknod :: proc "contextless" (path: cstring, mode: int, dev: int) -> int { +sys_mknod :: proc "contextless" (path: cstring, mode: uint, dev: int) -> int { when ODIN_ARCH != .arm64 { return int(intrinsics.syscall(SYS_mknod, uintptr(rawptr(path)), uintptr(mode), uintptr(dev))) } else { // NOTE: arm64 does not have mknod @@ -1928,7 +1931,7 @@ sys_mknod :: proc "contextless" (path: cstring, mode: int, dev: int) -> int { } } -sys_mknodat :: proc "contextless" (dfd: int, path: cstring, mode: int, dev: int) -> int { +sys_mknodat :: proc "contextless" (dfd: int, path: cstring, mode: uint, dev: int) -> int { return int(intrinsics.syscall(SYS_mknodat, uintptr(dfd), uintptr(rawptr(path)), uintptr(mode), uintptr(dev))) } @@ -1967,6 +1970,16 @@ sys_fork :: proc "contextless" () -> int { return int(intrinsics.syscall(SYS_clone, SIGCHLD)) } } +sys_pipe2 :: proc "contextless" (fds: rawptr, flags: int) -> int { + return int(intrinsics.syscall(SYS_pipe2, uintptr(fds), uintptr(flags))) +} +sys_dup2 :: proc "contextless" (oldfd: int, newfd: int) -> int { + when ODIN_ARCH != .arm64 { + return int(intrinsics.syscall(SYS_dup2, uintptr(oldfd), uintptr(newfd))) + } else { + return int(intrinsics.syscall(SYS_dup3, uintptr(oldfd), uintptr(newfd), 0)) + } +} sys_mmap :: proc "contextless" (addr: rawptr, length: uint, prot, flags, fd: int, offset: uintptr) -> int { return int(intrinsics.syscall(SYS_mmap, uintptr(addr), uintptr(length), uintptr(prot), uintptr(flags), uintptr(fd), offset)) @@ -1999,6 +2012,10 @@ sys_perf_event_open :: proc "contextless" (event_attr: rawptr, pid: i32, cpu: i3 return int(intrinsics.syscall(SYS_perf_event_open, uintptr(event_attr), uintptr(pid), uintptr(cpu), uintptr(group_fd), uintptr(flags))) } +sys_personality :: proc(persona: u64) -> int { + return int(intrinsics.syscall(SYS_personality, uintptr(persona))) +} + get_errno :: proc "contextless" (res: int) -> i32 { if res < 0 && res > -4096 { return i32(-res) |