| ... | ... | @@ -1,11 +1,11 @@ |
| 1 | const c = @import("../c/index.zig"); |
| 1 | 2 | const assert = @import("../debug.zig").assert; |
| 2 | 3 | const builtin = @import("builtin"); |
| 3 | 4 | const arch = switch (builtin.arch) { |
| 4 | | builtin.Arch.x86_64 => @import("freebsd_x86_64.zig"), |
| 5 | | builtin.Arch.i386 => @import("freebsd_i386.zig"), |
| 5 | builtin.Arch.x86_64 => @import("x86_64.zig"), |
| 6 | 6 | else => @compileError("unsupported arch"), |
| 7 | 7 | }; |
| 8 | | pub use @import("freebsd_errno.zig"); |
| 8 | pub use @import("errno.zig"); |
| 9 | 9 | |
| 10 | 10 | pub const PATH_MAX = 1024; |
| 11 | 11 | |
| ... | ... | @@ -13,85 +13,86 @@ pub const STDIN_FILENO = 0; |
| 13 | 13 | pub const STDOUT_FILENO = 1; |
| 14 | 14 | pub const STDERR_FILENO = 2; |
| 15 | 15 | |
| 16 | | pub const PROT_NONE = 0; |
| 17 | | pub const PROT_READ = 1; |
| 18 | | pub const PROT_WRITE = 2; |
| 19 | | pub const PROT_EXEC = 4; |
| 20 | | |
| 21 | | pub const MAP_FAILED = @maxValue(usize); |
| 22 | | pub const MAP_SHARED = 0x0001; |
| 23 | | pub const MAP_PRIVATE = 0x0002; |
| 24 | | pub const MAP_FIXED = 0x0010; |
| 25 | | pub const MAP_STACK = 0x0400; |
| 26 | | pub const MAP_NOSYNC = 0x0800; |
| 27 | | pub const MAP_ANON = 0x1000; |
| 28 | | pub const MAP_ANONYMOUS = MAP_ANON; |
| 29 | | pub const MAP_FILE = 0; |
| 30 | | |
| 31 | | pub const MAP_GUARD = 0x00002000; |
| 32 | | pub const MAP_EXCL = 0x00004000; |
| 33 | | pub const MAP_NOCORE = 0x00020000; |
| 16 | pub const PROT_NONE = 0; |
| 17 | pub const PROT_READ = 1; |
| 18 | pub const PROT_WRITE = 2; |
| 19 | pub const PROT_EXEC = 4; |
| 20 | |
| 21 | pub const MAP_FAILED = @maxValue(usize); |
| 22 | pub const MAP_SHARED = 0x0001; |
| 23 | pub const MAP_PRIVATE = 0x0002; |
| 24 | pub const MAP_FIXED = 0x0010; |
| 25 | pub const MAP_STACK = 0x0400; |
| 26 | pub const MAP_NOSYNC = 0x0800; |
| 27 | pub const MAP_ANON = 0x1000; |
| 28 | pub const MAP_ANONYMOUS = MAP_ANON; |
| 29 | pub const MAP_FILE = 0; |
| 30 | pub const MAP_NORESERVE = 0; |
| 31 | |
| 32 | pub const MAP_GUARD = 0x00002000; |
| 33 | pub const MAP_EXCL = 0x00004000; |
| 34 | pub const MAP_NOCORE = 0x00020000; |
| 34 | 35 | pub const MAP_PREFAULT_READ = 0x00040000; |
| 35 | | pub const MAP_32BIT = 0x00080000; |
| 36 | pub const MAP_32BIT = 0x00080000; |
| 36 | 37 | |
| 37 | | pub const WNOHANG = 1; |
| 38 | | pub const WUNTRACED = 2; |
| 39 | | pub const WSTOPPED = WUNTRACED; |
| 38 | pub const WNOHANG = 1; |
| 39 | pub const WUNTRACED = 2; |
| 40 | pub const WSTOPPED = WUNTRACED; |
| 40 | 41 | pub const WCONTINUED = 4; |
| 41 | | pub const WNOWAIT = 8; |
| 42 | | pub const WEXITED = 16; |
| 43 | | pub const WTRAPPED = 32; |
| 42 | pub const WNOWAIT = 8; |
| 43 | pub const WEXITED = 16; |
| 44 | pub const WTRAPPED = 32; |
| 44 | 45 | |
| 45 | | pub const SA_ONSTACK = 0x0001; |
| 46 | | pub const SA_RESTART = 0x0002; |
| 46 | pub const SA_ONSTACK = 0x0001; |
| 47 | pub const SA_RESTART = 0x0002; |
| 47 | 48 | pub const SA_RESETHAND = 0x0004; |
| 48 | 49 | pub const SA_NOCLDSTOP = 0x0008; |
| 49 | | pub const SA_NODEFER = 0x0010; |
| 50 | pub const SA_NODEFER = 0x0010; |
| 50 | 51 | pub const SA_NOCLDWAIT = 0x0020; |
| 51 | | pub const SA_SIGINFO = 0x0040; |
| 52 | | |
| 53 | | pub const SIGHUP = 1; |
| 54 | | pub const SIGINT = 2; |
| 55 | | pub const SIGQUIT = 3; |
| 56 | | pub const SIGILL = 4; |
| 57 | | pub const SIGTRAP = 5; |
| 58 | | pub const SIGABRT = 6; |
| 59 | | pub const SIGIOT = SIGABRT; |
| 60 | | pub const SIGEMT = 7; |
| 61 | | pub const SIGFPE = 8; |
| 62 | | pub const SIGKILL = 9; |
| 63 | | pub const SIGBUS = 10; |
| 64 | | pub const SIGSEGV = 11; |
| 65 | | pub const SIGSYS = 12; |
| 66 | | pub const SIGPIPE = 13; |
| 67 | | pub const SIGALRM = 14; |
| 68 | | pub const SIGTERM = 15; |
| 69 | | pub const SIGURG = 16; |
| 70 | | pub const SIGSTOP = 17; |
| 71 | | pub const SIGTSTP = 18; |
| 72 | | pub const SIGCONT = 19; |
| 73 | | pub const SIGCHLD = 20; |
| 74 | | pub const SIGTTIN = 21; |
| 75 | | pub const SIGTTOU = 22; |
| 76 | | pub const SIGIO = 23; |
| 77 | | pub const SIGXCPU = 24; |
| 78 | | pub const SIGXFSZ = 25; |
| 52 | pub const SA_SIGINFO = 0x0040; |
| 53 | |
| 54 | pub const SIGHUP = 1; |
| 55 | pub const SIGINT = 2; |
| 56 | pub const SIGQUIT = 3; |
| 57 | pub const SIGILL = 4; |
| 58 | pub const SIGTRAP = 5; |
| 59 | pub const SIGABRT = 6; |
| 60 | pub const SIGIOT = SIGABRT; |
| 61 | pub const SIGEMT = 7; |
| 62 | pub const SIGFPE = 8; |
| 63 | pub const SIGKILL = 9; |
| 64 | pub const SIGBUS = 10; |
| 65 | pub const SIGSEGV = 11; |
| 66 | pub const SIGSYS = 12; |
| 67 | pub const SIGPIPE = 13; |
| 68 | pub const SIGALRM = 14; |
| 69 | pub const SIGTERM = 15; |
| 70 | pub const SIGURG = 16; |
| 71 | pub const SIGSTOP = 17; |
| 72 | pub const SIGTSTP = 18; |
| 73 | pub const SIGCONT = 19; |
| 74 | pub const SIGCHLD = 20; |
| 75 | pub const SIGTTIN = 21; |
| 76 | pub const SIGTTOU = 22; |
| 77 | pub const SIGIO = 23; |
| 78 | pub const SIGXCPU = 24; |
| 79 | pub const SIGXFSZ = 25; |
| 79 | 80 | pub const SIGVTALRM = 26; |
| 80 | | pub const SIGPROF = 27; |
| 81 | | pub const SIGWINCH = 28; |
| 82 | | pub const SIGINFO = 29; |
| 83 | | pub const SIGUSR1 = 30; |
| 84 | | pub const SIGUSR2 = 31; |
| 85 | | pub const SIGTHR = 32; |
| 86 | | pub const SIGLWP = SIGTHR; |
| 87 | | pub const SIGLIBRT = 33; |
| 88 | | |
| 89 | | pub const SIGRTMIN = 65; |
| 90 | | pub const SIGRTMAX = 126; |
| 81 | pub const SIGPROF = 27; |
| 82 | pub const SIGWINCH = 28; |
| 83 | pub const SIGINFO = 29; |
| 84 | pub const SIGUSR1 = 30; |
| 85 | pub const SIGUSR2 = 31; |
| 86 | pub const SIGTHR = 32; |
| 87 | pub const SIGLWP = SIGTHR; |
| 88 | pub const SIGLIBRT = 33; |
| 89 | |
| 90 | pub const SIGRTMIN = 65; |
| 91 | pub const SIGRTMAX = 126; |
| 91 | 92 | |
| 92 | 93 | pub const O_RDONLY = 0o0; |
| 93 | 94 | pub const O_WRONLY = 0o1; |
| 94 | | pub const O_RDWR = 0o2; |
| 95 | pub const O_RDWR = 0o2; |
| 95 | 96 | pub const O_ACCMODE = 0o3; |
| 96 | 97 | |
| 97 | 98 | pub const O_CREAT = arch.O_CREAT; |
| ... | ... | @@ -119,7 +120,7 @@ pub const SEEK_SET = 0; |
| 119 | 120 | pub const SEEK_CUR = 1; |
| 120 | 121 | pub const SEEK_END = 2; |
| 121 | 122 | |
| 122 | | pub const SIG_BLOCK = 1; |
| 123 | pub const SIG_BLOCK = 1; |
| 123 | 124 | pub const SIG_UNBLOCK = 2; |
| 124 | 125 | pub const SIG_SETMASK = 3; |
| 125 | 126 | |
| ... | ... | @@ -272,7 +273,6 @@ pub const DT_LNK = 10; |
| 272 | 273 | pub const DT_SOCK = 12; |
| 273 | 274 | pub const DT_WHT = 14; |
| 274 | 275 | |
| 275 | | |
| 276 | 276 | pub const TCGETS = 0x5401; |
| 277 | 277 | pub const TCSETS = 0x5402; |
| 278 | 278 | pub const TCSETSW = 0x5403; |
| ... | ... | @@ -329,15 +329,30 @@ pub const TIOCGPKT = 0x80045438; |
| 329 | 329 | pub const TIOCGPTLCK = 0x80045439; |
| 330 | 330 | pub const TIOCGEXCL = 0x80045440; |
| 331 | 331 | |
| 332 | | fn unsigned(s: i32) -> u32 { @bitCast(u32, s) } |
| 333 | | fn signed(s: u32) -> i32 { @bitCast(i32, s) } |
| 334 | | pub fn WEXITSTATUS(s: i32) -> i32 { signed((unsigned(s) & 0xff00) >> 8) } |
| 335 | | pub fn WTERMSIG(s: i32) -> i32 { signed(unsigned(s) & 0x7f) } |
| 336 | | pub fn WSTOPSIG(s: i32) -> i32 { WEXITSTATUS(s) } |
| 337 | | pub fn WIFEXITED(s: i32) -> bool { WTERMSIG(s) == 0 } |
| 338 | | pub fn WIFSTOPPED(s: i32) -> bool { (u16)(((unsigned(s)&0xffff)*%0x10001)>>8) > 0x7f00 } |
| 339 | | pub fn WIFSIGNALED(s: i32) -> bool { (unsigned(s)&0xffff)-%1 < 0xff } |
| 340 | | |
| 332 | fn unsigned(s: i32) u32 { |
| 333 | return @bitCast(u32, s); |
| 334 | } |
| 335 | fn signed(s: u32) i32 { |
| 336 | return @bitCast(i32, s); |
| 337 | } |
| 338 | pub fn WEXITSTATUS(s: i32) i32 { |
| 339 | return signed((unsigned(s) & 0xff00) >> 8); |
| 340 | } |
| 341 | pub fn WTERMSIG(s: i32) i32 { |
| 342 | return signed(unsigned(s) & 0x7f); |
| 343 | } |
| 344 | pub fn WSTOPSIG(s: i32) i32 { |
| 345 | return WEXITSTATUS(s); |
| 346 | } |
| 347 | pub fn WIFEXITED(s: i32) bool { |
| 348 | return WTERMSIG(s) == 0; |
| 349 | } |
| 350 | pub fn WIFSTOPPED(s: i32) bool { |
| 351 | return (u16)(((unsigned(s) & 0xffff) *% 0x10001) >> 8) > 0x7f00; |
| 352 | } |
| 353 | pub fn WIFSIGNALED(s: i32) bool { |
| 354 | return (unsigned(s) & 0xffff) -% 1 < 0xff; |
| 355 | } |
| 341 | 356 | |
| 342 | 357 | pub const winsize = extern struct { |
| 343 | 358 | ws_row: u16, |
| ... | ... | @@ -347,182 +362,160 @@ pub const winsize = extern struct { |
| 347 | 362 | }; |
| 348 | 363 | |
| 349 | 364 | /// Get the errno from a syscall return value, or 0 for no error. |
| 350 | | pub fn getErrno(r: usize) -> usize { |
| 365 | pub fn getErrno(r: usize) usize { |
| 351 | 366 | const signed_r = @bitCast(isize, r); |
| 352 | | if (signed_r > -4096 and signed_r < 0) usize(-signed_r) else 0 |
| 367 | return if (signed_r > -4096 and signed_r < 0) @intCast(usize, -signed_r) else 0; |
| 353 | 368 | } |
| 354 | 369 | |
| 355 | | pub fn dup2(old: i32, new: i32) -> usize { |
| 356 | | arch.syscall2(arch.SYS_dup2, usize(old), usize(new)) |
| 370 | pub fn dup2(old: i32, new: i32) usize { |
| 371 | return arch.syscall2(arch.SYS_dup2, usize(old), usize(new)); |
| 357 | 372 | } |
| 358 | 373 | |
| 359 | | pub fn chdir(path: &const u8) -> usize { |
| 360 | | arch.syscall1(arch.SYS_chdir, @ptrToInt(path)) |
| 374 | pub fn chdir(path: [*]const u8) usize { |
| 375 | return arch.syscall1(arch.SYS_chdir, @ptrToInt(path)); |
| 361 | 376 | } |
| 362 | 377 | |
| 363 | | pub fn execve(path: &const u8, argv: &const ?&const u8, envp: &const ?&const u8) -> usize { |
| 364 | | arch.syscall3(arch.SYS_execve, @ptrToInt(path), @ptrToInt(argv), @ptrToInt(envp)) |
| 378 | pub fn execve(path: [*]const u8, argv: [*]const ?[*]const u8, envp: [*]const ?[*]const u8) usize { |
| 379 | return arch.syscall3(arch.SYS_execve, @ptrToInt(path), @ptrToInt(argv), @ptrToInt(envp)); |
| 365 | 380 | } |
| 366 | 381 | |
| 367 | | pub fn fork() -> usize { |
| 368 | | arch.syscall0(arch.SYS_fork) |
| 382 | pub fn fork() usize { |
| 383 | return arch.syscall0(arch.SYS_fork); |
| 369 | 384 | } |
| 370 | 385 | |
| 371 | | pub fn getcwd(buf: &u8, size: usize) -> usize { |
| 372 | | arch.syscall2(arch.SYS_getcwd, @ptrToInt(buf), size) |
| 386 | pub fn getcwd(buf: [*]u8, size: usize) usize { |
| 387 | return arch.syscall2(arch.SYS___getcwd, @ptrToInt(buf), size); |
| 373 | 388 | } |
| 374 | 389 | |
| 375 | | pub fn getdents(fd: i32, dirp: &u8, count: usize) -> usize { |
| 376 | | arch.syscall3(arch.SYS_getdents, usize(fd), @ptrToInt(dirp), count) |
| 390 | pub fn getdents(fd: i32, dirp: [*]u8, count: usize) usize { |
| 391 | return arch.syscall3(arch.SYS_getdents, usize(fd), @ptrToInt(dirp), count); |
| 377 | 392 | } |
| 378 | 393 | |
| 379 | | pub fn isatty(fd: i32) -> bool { |
| 394 | pub fn isatty(fd: i32) bool { |
| 380 | 395 | var wsz: winsize = undefined; |
| 381 | | return arch.syscall3(arch.SYS_ioctl, usize(fd), TIOCGWINSZ, @ptrToInt(&wsz)) == 0; |
| 396 | return arch.syscall3(arch.SYS_ioctl, @intCast(usize, fd), TIOCGWINSZ, @ptrToInt(&wsz)) == 0; |
| 382 | 397 | } |
| 383 | 398 | |
| 384 | | pub fn readlink(noalias path: &const u8, noalias buf_ptr: &u8, buf_len: usize) -> usize { |
| 385 | | arch.syscall3(arch.SYS_readlink, @ptrToInt(path), @ptrToInt(buf_ptr), buf_len) |
| 399 | pub fn readlink(noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize { |
| 400 | return arch.syscall3(arch.SYS_readlink, @ptrToInt(path), @ptrToInt(buf_ptr), buf_len); |
| 386 | 401 | } |
| 387 | 402 | |
| 388 | | pub fn mkdir(path: &const u8, mode: u32) -> usize { |
| 389 | | arch.syscall2(arch.SYS_mkdir, @ptrToInt(path), mode) |
| 403 | pub fn mkdir(path: [*]const u8, mode: u32) usize { |
| 404 | return arch.syscall2(arch.SYS_mkdir, @ptrToInt(path), mode); |
| 390 | 405 | } |
| 391 | 406 | |
| 392 | | pub fn mmap(address: ?&u8, length: usize, prot: usize, flags: usize, fd: i32, offset: isize) |
| 393 | | -> usize |
| 394 | | { |
| 395 | | arch.syscall6(arch.SYS_mmap, @ptrToInt(address), length, prot, flags, usize(fd), |
| 396 | | @bitCast(usize, offset)) |
| 407 | pub fn mmap(address: ?*u8, length: usize, prot: usize, flags: usize, fd: i32, offset: isize) usize { |
| 408 | return arch.syscall6(arch.SYS_mmap, @ptrToInt(address), length, prot, flags, @intCast(usize, fd), @bitCast(usize, offset)); |
| 397 | 409 | } |
| 398 | 410 | |
| 399 | | pub fn munmap(address: &u8, length: usize) -> usize { |
| 400 | | arch.syscall2(arch.SYS_munmap, @ptrToInt(address), length) |
| 411 | pub fn munmap(address: usize, length: usize) usize { |
| 412 | return arch.syscall2(arch.SYS_munmap, address, length); |
| 401 | 413 | } |
| 402 | 414 | |
| 403 | | pub fn read(fd: i32, buf: &u8, count: usize) -> usize { |
| 404 | | arch.syscall3(arch.SYS_read, usize(fd), @ptrToInt(buf), count) |
| 415 | pub fn read(fd: i32, buf: [*]u8, count: usize) usize { |
| 416 | return arch.syscall3(arch.SYS_read, @intCast(usize, fd), @ptrToInt(buf), count); |
| 405 | 417 | } |
| 406 | 418 | |
| 407 | | pub fn rmdir(path: &const u8) -> usize { |
| 408 | | arch.syscall1(arch.SYS_rmdir, @ptrToInt(path)) |
| 419 | pub fn rmdir(path: [*]const u8) usize { |
| 420 | return arch.syscall1(arch.SYS_rmdir, @ptrToInt(path)); |
| 409 | 421 | } |
| 410 | 422 | |
| 411 | | pub fn symlink(existing: &const u8, new: &const u8) -> usize { |
| 412 | | arch.syscall2(arch.SYS_symlink, @ptrToInt(existing), @ptrToInt(new)) |
| 423 | pub fn symlink(existing: [*]const u8, new: [*]const u8) usize { |
| 424 | return arch.syscall2(arch.SYS_symlink, @ptrToInt(existing), @ptrToInt(new)); |
| 413 | 425 | } |
| 414 | 426 | |
| 415 | | pub fn pread(fd: i32, buf: &u8, count: usize, offset: usize) -> usize { |
| 416 | | arch.syscall4(arch.SYS_pread, usize(fd), @ptrToInt(buf), count, offset) |
| 427 | pub fn pread(fd: i32, buf: [*]u8, count: usize, offset: usize) usize { |
| 428 | return arch.syscall4(arch.SYS_pread, usize(fd), @ptrToInt(buf), count, offset); |
| 417 | 429 | } |
| 418 | 430 | |
| 419 | | pub fn pipe(fd: &[2]i32) -> usize { |
| 420 | | pipe2(fd, 0) |
| 431 | pub fn pipe(fd: *[2]i32) usize { |
| 432 | return pipe2(fd, 0); |
| 421 | 433 | } |
| 422 | 434 | |
| 423 | | pub fn pipe2(fd: &[2]i32, flags: usize) -> usize { |
| 424 | | arch.syscall2(arch.SYS_pipe2, @ptrToInt(fd), flags) |
| 435 | pub fn pipe2(fd: *[2]i32, flags: usize) usize { |
| 436 | return arch.syscall2(arch.SYS_pipe2, @ptrToInt(fd), flags); |
| 425 | 437 | } |
| 426 | 438 | |
| 427 | | pub fn write(fd: i32, buf: &const u8, count: usize) -> usize { |
| 428 | | arch.syscall3(arch.SYS_write, usize(fd), @ptrToInt(buf), count) |
| 439 | pub fn write(fd: i32, buf: [*]const u8, count: usize) usize { |
| 440 | return arch.syscall3(arch.SYS_write, @intCast(usize, fd), @ptrToInt(buf), count); |
| 429 | 441 | } |
| 430 | 442 | |
| 431 | | pub fn pwrite(fd: i32, buf: &const u8, count: usize, offset: usize) -> usize { |
| 432 | | arch.syscall4(arch.SYS_pwrite, usize(fd), @ptrToInt(buf), count, offset) |
| 443 | pub fn pwrite(fd: i32, buf: [*]const u8, count: usize, offset: usize) usize { |
| 444 | return arch.syscall4(arch.SYS_pwrite, @intCast(usize, fd), @ptrToInt(buf), count, offset); |
| 433 | 445 | } |
| 434 | 446 | |
| 435 | | pub fn rename(old: &const u8, new: &const u8) -> usize { |
| 436 | | arch.syscall2(arch.SYS_rename, @ptrToInt(old), @ptrToInt(new)) |
| 447 | pub fn rename(old: [*]const u8, new: [*]const u8) usize { |
| 448 | return arch.syscall2(arch.SYS_rename, @ptrToInt(old), @ptrToInt(new)); |
| 437 | 449 | } |
| 438 | 450 | |
| 439 | | pub fn open(path: &const u8, flags: u32, perm: usize) -> usize { |
| 440 | | arch.syscall3(arch.SYS_open, @ptrToInt(path), flags, perm) |
| 451 | pub fn open(path: [*]const u8, flags: u32, perm: usize) usize { |
| 452 | return arch.syscall3(arch.SYS_open, @ptrToInt(path), flags, perm); |
| 441 | 453 | } |
| 442 | 454 | |
| 443 | | pub fn create(path: &const u8, perm: usize) -> usize { |
| 444 | | arch.syscall2(arch.SYS_creat, @ptrToInt(path), perm) |
| 455 | pub fn create(path: [*]const u8, perm: usize) usize { |
| 456 | return arch.syscall2(arch.SYS_creat, @ptrToInt(path), perm); |
| 445 | 457 | } |
| 446 | 458 | |
| 447 | | pub fn openat(dirfd: i32, path: &const u8, flags: usize, mode: usize) -> usize { |
| 448 | | arch.syscall4(arch.SYS_openat, usize(dirfd), @ptrToInt(path), flags, mode) |
| 459 | pub fn openat(dirfd: i32, path: [*]const u8, flags: usize, mode: usize) usize { |
| 460 | return arch.syscall4(arch.SYS_openat, @intCast(usize, dirfd), @ptrToInt(path), flags, mode); |
| 449 | 461 | } |
| 450 | 462 | |
| 451 | | pub fn close(fd: i32) -> usize { |
| 452 | | arch.syscall1(arch.SYS_close, usize(fd)) |
| 463 | pub fn close(fd: i32) usize { |
| 464 | return arch.syscall1(arch.SYS_close, @intCast(usize, fd)); |
| 453 | 465 | } |
| 454 | 466 | |
| 455 | | pub fn lseek(fd: i32, offset: isize, ref_pos: usize) -> usize { |
| 456 | | arch.syscall3(arch.SYS_lseek, usize(fd), @bitCast(usize, offset), ref_pos) |
| 467 | pub fn lseek(fd: i32, offset: isize, ref_pos: usize) usize { |
| 468 | return arch.syscall3(arch.SYS_lseek, @intCast(usize, fd), @bitCast(usize, offset), ref_pos); |
| 457 | 469 | } |
| 458 | 470 | |
| 459 | | pub fn exit(status: i32) -> noreturn { |
| 471 | pub fn exit(status: i32) noreturn { |
| 460 | 472 | _ = arch.syscall1(arch.SYS_exit, @bitCast(usize, isize(status))); |
| 461 | | unreachable |
| 473 | unreachable; |
| 462 | 474 | } |
| 463 | 475 | |
| 464 | | pub fn getrandom(buf: &u8, count: usize, flags: u32) -> usize { |
| 465 | | arch.syscall3(arch.SYS_getrandom, @ptrToInt(buf), count, usize(flags)) |
| 476 | pub fn getrandom(buf: &u8, count: usize, flags: u32) usize { |
| 477 | return arch.syscall3(arch.SYS_getrandom, @ptrToInt(buf), count, @bitCast(usize, flags)); |
| 466 | 478 | } |
| 467 | 479 | |
| 468 | | pub fn kill(pid: i32, sig: i32) -> usize { |
| 469 | | arch.syscall2(arch.SYS_kill, @bitCast(usize, isize(pid)), usize(sig)) |
| 480 | pub fn kill(pid: i32, sig: i32) usize { |
| 481 | return arch.syscall2(arch.SYS_kill, @bitCast(usize, @intCast(isize, pid)), @intCast(usize, sig)); |
| 470 | 482 | } |
| 471 | 483 | |
| 472 | | pub fn unlink(path: &const u8) -> usize { |
| 473 | | arch.syscall1(arch.SYS_unlink, @ptrToInt(path)) |
| 484 | pub fn unlink(path: [*]const u8) usize { |
| 485 | return arch.syscall1(arch.SYS_unlink, @ptrToInt(path)); |
| 474 | 486 | } |
| 475 | 487 | |
| 476 | | pub fn waitpid(pid: i32, status: &i32, options: i32) -> usize { |
| 477 | | arch.syscall4(arch.SYS_wait4, @bitCast(usize, isize(pid)), @ptrToInt(status), @bitCast(usize, isize(options)), 0) |
| 488 | pub fn waitpid(pid: i32, status: *i32, options: i32) usize { |
| 489 | return arch.syscall4(arch.SYS_wait4, @bitCast(usize, isize(pid)), @ptrToInt(status), @bitCast(usize, isize(options)), 0); |
| 478 | 490 | } |
| 479 | 491 | |
| 480 | | pub fn nanosleep(req: &const timespec, rem: ?&timespec) -> usize { |
| 481 | | arch.syscall2(arch.SYS_nanosleep, @ptrToInt(req), @ptrToInt(rem)) |
| 492 | pub fn nanosleep(req: *const timespec, rem: ?*timespec) usize { |
| 493 | return arch.syscall2(arch.SYS_nanosleep, @ptrToInt(req), @ptrToInt(rem)); |
| 482 | 494 | } |
| 483 | 495 | |
| 484 | | pub fn setuid(uid: u32) -> usize { |
| 485 | | arch.syscall1(arch.SYS_setuid, uid) |
| 496 | pub fn setuid(uid: u32) usize { |
| 497 | return arch.syscall1(arch.SYS_setuid, uid); |
| 486 | 498 | } |
| 487 | 499 | |
| 488 | | pub fn setgid(gid: u32) -> usize { |
| 489 | | arch.syscall1(arch.SYS_setgid, gid) |
| 500 | pub fn setgid(gid: u32) usize { |
| 501 | return arch.syscall1(arch.SYS_setgid, gid); |
| 490 | 502 | } |
| 491 | 503 | |
| 492 | | pub fn setreuid(ruid: u32, euid: u32) -> usize { |
| 493 | | arch.syscall2(arch.SYS_setreuid, ruid, euid) |
| 504 | pub fn setreuid(ruid: u32, euid: u32) usize { |
| 505 | return arch.syscall2(arch.SYS_setreuid, ruid, euid); |
| 494 | 506 | } |
| 495 | 507 | |
| 496 | | pub fn setregid(rgid: u32, egid: u32) -> usize { |
| 497 | | arch.syscall2(arch.SYS_setregid, rgid, egid) |
| 508 | pub fn setregid(rgid: u32, egid: u32) usize { |
| 509 | return arch.syscall2(arch.SYS_setregid, rgid, egid); |
| 498 | 510 | } |
| 499 | 511 | |
| 500 | | pub fn sigprocmask(flags: u32, noalias set: &const sigset_t, noalias oldset: ?&sigset_t) -> usize { |
| 501 | | arch.syscall4(arch.SYS_rt_sigprocmask, flags, @ptrToInt(set), @ptrToInt(oldset), NSIG/8) |
| 512 | pub fn sigprocmask(flags: u32, noalias set: *const sigset_t, noalias oldset: ?*sigset_t) usize { |
| 513 | // TODO: Implement |
| 514 | return 0; |
| 502 | 515 | } |
| 503 | 516 | |
| 504 | | pub fn sigaction(sig: u6, noalias act: &const Sigaction, noalias oact: ?&Sigaction) -> usize { |
| 505 | | assert(sig >= 1); |
| 506 | | assert(sig != SIGKILL); |
| 507 | | assert(sig != SIGSTOP); |
| 508 | | var ksa = k_sigaction { |
| 509 | | .handler = act.handler, |
| 510 | | .flags = act.flags | SA_RESTORER, |
| 511 | | .mask = undefined, |
| 512 | | .restorer = @ptrCast(extern fn(), arch.restore_rt), |
| 513 | | }; |
| 514 | | var ksa_old: k_sigaction = undefined; |
| 515 | | @memcpy(@ptrCast(&u8, &ksa.mask), @ptrCast(&const u8, &act.mask), 8); |
| 516 | | const result = arch.syscall4(arch.SYS_rt_sigaction, sig, @ptrToInt(&ksa), @ptrToInt(&ksa_old), @sizeOf(@typeOf(ksa.mask))); |
| 517 | | const err = getErrno(result); |
| 518 | | if (err != 0) { |
| 519 | | return result; |
| 520 | | } |
| 521 | | if (oact) |old| { |
| 522 | | old.handler = ksa_old.handler; |
| 523 | | old.flags = @truncate(u32, ksa_old.flags); |
| 524 | | @memcpy(@ptrCast(&u8, &old.mask), @ptrCast(&const u8, &ksa_old.mask), @sizeOf(@typeOf(ksa_old.mask))); |
| 525 | | } |
| 517 | pub fn sigaction(sig: u6, noalias act: *const Sigaction, noalias oact: ?*Sigaction) usize { |
| 518 | // TODO: Implement |
| 526 | 519 | return 0; |
| 527 | 520 | } |
| 528 | 521 | |
| ... | ... | @@ -531,203 +524,49 @@ const sigset_t = [128 / @sizeOf(usize)]usize; |
| 531 | 524 | const all_mask = []usize{@maxValue(usize)}; |
| 532 | 525 | const app_mask = []usize{0xfffffffc7fffffff}; |
| 533 | 526 | |
| 534 | | const k_sigaction = extern struct { |
| 535 | | handler: extern fn(i32), |
| 536 | | flags: usize, |
| 537 | | restorer: extern fn(), |
| 538 | | mask: [2]u32, |
| 539 | | }; |
| 540 | | |
| 541 | 527 | /// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall. |
| 542 | 528 | pub const Sigaction = struct { |
| 543 | | handler: extern fn(i32), |
| 529 | // TODO: Adjust to use freebsd struct layout |
| 530 | handler: extern fn (i32) void, |
| 544 | 531 | mask: sigset_t, |
| 545 | 532 | flags: u32, |
| 546 | 533 | }; |
| 547 | 534 | |
| 548 | | pub const SIG_ERR = @intToPtr(extern fn(i32), @maxValue(usize)); |
| 549 | | pub const SIG_DFL = @intToPtr(extern fn(i32), 0); |
| 550 | | pub const SIG_IGN = @intToPtr(extern fn(i32), 1); |
| 535 | pub const SIG_ERR = @intToPtr(extern fn (i32) void, @maxValue(usize)); |
| 536 | pub const SIG_DFL = @intToPtr(extern fn (i32) void, 0); |
| 537 | pub const SIG_IGN = @intToPtr(extern fn (i32) void, 1); |
| 551 | 538 | pub const empty_sigset = []usize{0} ** sigset_t.len; |
| 552 | 539 | |
| 553 | | pub fn raise(sig: i32) -> usize { |
| 540 | pub fn raise(sig: i32) usize { |
| 554 | 541 | // TODO implement, see linux equivalent for what we want to try and do |
| 555 | 542 | return 0; |
| 556 | 543 | } |
| 557 | 544 | |
| 558 | | fn blockAllSignals(set: &sigset_t) { |
| 545 | fn blockAllSignals(set: *sigset_t) void { |
| 559 | 546 | // TODO implement |
| 560 | 547 | } |
| 561 | 548 | |
| 562 | | fn blockAppSignals(set: &sigset_t) { |
| 549 | fn blockAppSignals(set: *sigset_t) void { |
| 563 | 550 | // TODO implement |
| 564 | 551 | } |
| 565 | 552 | |
| 566 | | fn restoreSignals(set: &sigset_t) { |
| 553 | fn restoreSignals(set: *sigset_t) void { |
| 567 | 554 | // TODO implement |
| 568 | 555 | } |
| 569 | 556 | |
| 570 | | pub fn sigaddset(set: &sigset_t, sig: u6) { |
| 557 | pub fn sigaddset(set: *sigset_t, sig: u6) void { |
| 571 | 558 | const s = sig - 1; |
| 572 | 559 | (*set)[usize(s) / usize.bit_count] |= usize(1) << (s & (usize.bit_count - 1)); |
| 573 | 560 | } |
| 574 | 561 | |
| 575 | | pub fn sigismember(set: &const sigset_t, sig: u6) -> bool { |
| 562 | pub fn sigismember(set: *const sigset_t, sig: u6) bool { |
| 576 | 563 | const s = sig - 1; |
| 577 | 564 | return ((*set)[usize(s) / usize.bit_count] & (usize(1) << (s & (usize.bit_count - 1)))) != 0; |
| 578 | 565 | } |
| 579 | 566 | |
| 580 | | |
| 581 | | pub const sa_family_t = u16; |
| 582 | | pub const socklen_t = u32; |
| 583 | | pub const in_addr = u32; |
| 584 | | pub const in6_addr = [16]u8; |
| 585 | | |
| 586 | | pub const sockaddr = extern struct { |
| 587 | | family: sa_family_t, |
| 588 | | port: u16, |
| 589 | | data: [12]u8, |
| 590 | | }; |
| 591 | | |
| 592 | | pub const sockaddr_in = extern struct { |
| 593 | | family: sa_family_t, |
| 594 | | port: u16, |
| 595 | | addr: in_addr, |
| 596 | | zero: [8]u8, |
| 597 | | }; |
| 598 | | |
| 599 | | pub const sockaddr_in6 = extern struct { |
| 600 | | family: sa_family_t, |
| 601 | | port: u16, |
| 602 | | flowinfo: u32, |
| 603 | | addr: in6_addr, |
| 604 | | scope_id: u32, |
| 605 | | }; |
| 606 | | |
| 607 | | pub const iovec = extern struct { |
| 608 | | iov_base: &u8, |
| 609 | | iov_len: usize, |
| 610 | | }; |
| 611 | | |
| 612 | | // |
| 613 | | //const IF_NAMESIZE = 16; |
| 614 | | // |
| 615 | | //export struct ifreq { |
| 616 | | // ifrn_name: [IF_NAMESIZE]u8, |
| 617 | | // union { |
| 618 | | // ifru_addr: sockaddr, |
| 619 | | // ifru_dstaddr: sockaddr, |
| 620 | | // ifru_broadaddr: sockaddr, |
| 621 | | // ifru_netmask: sockaddr, |
| 622 | | // ifru_hwaddr: sockaddr, |
| 623 | | // ifru_flags: i16, |
| 624 | | // ifru_ivalue: i32, |
| 625 | | // ifru_mtu: i32, |
| 626 | | // ifru_map: ifmap, |
| 627 | | // ifru_slave: [IF_NAMESIZE]u8, |
| 628 | | // ifru_newname: [IF_NAMESIZE]u8, |
| 629 | | // ifru_data: &u8, |
| 630 | | // } ifr_ifru; |
| 631 | | //} |
| 632 | | // |
| 633 | | |
| 634 | | pub fn getsockname(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) -> usize { |
| 635 | | arch.syscall3(arch.SYS_getsockname, usize(fd), @ptrToInt(addr), @ptrToInt(len)) |
| 636 | | } |
| 637 | | |
| 638 | | pub fn getpeername(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) -> usize { |
| 639 | | arch.syscall3(arch.SYS_getpeername, usize(fd), @ptrToInt(addr), @ptrToInt(len)) |
| 640 | | } |
| 641 | | |
| 642 | | pub fn socket(domain: i32, socket_type: i32, protocol: i32) -> usize { |
| 643 | | arch.syscall3(arch.SYS_socket, usize(domain), usize(socket_type), usize(protocol)) |
| 644 | | } |
| 645 | | |
| 646 | | pub fn setsockopt(fd: i32, level: i32, optname: i32, optval: &const u8, optlen: socklen_t) -> usize { |
| 647 | | arch.syscall5(arch.SYS_setsockopt, usize(fd), usize(level), usize(optname), usize(optval), @ptrToInt(optlen)) |
| 648 | | } |
| 649 | | |
| 650 | | pub fn getsockopt(fd: i32, level: i32, optname: i32, noalias optval: &u8, noalias optlen: &socklen_t) -> usize { |
| 651 | | arch.syscall5(arch.SYS_getsockopt, usize(fd), usize(level), usize(optname), @ptrToInt(optval), @ptrToInt(optlen)) |
| 652 | | } |
| 653 | | |
| 654 | | pub fn sendmsg(fd: i32, msg: &const arch.msghdr, flags: u32) -> usize { |
| 655 | | arch.syscall3(arch.SYS_sendmsg, usize(fd), @ptrToInt(msg), flags) |
| 656 | | } |
| 657 | | |
| 658 | | pub fn connect(fd: i32, addr: &const sockaddr, len: socklen_t) -> usize { |
| 659 | | arch.syscall3(arch.SYS_connect, usize(fd), @ptrToInt(addr), usize(len)) |
| 660 | | } |
| 661 | | |
| 662 | | pub fn recvmsg(fd: i32, msg: &arch.msghdr, flags: u32) -> usize { |
| 663 | | arch.syscall3(arch.SYS_recvmsg, usize(fd), @ptrToInt(msg), flags) |
| 664 | | } |
| 665 | | |
| 666 | | pub fn recvfrom(fd: i32, noalias buf: &u8, len: usize, flags: u32, |
| 667 | | noalias addr: ?&sockaddr, noalias alen: ?&socklen_t) -> usize |
| 668 | | { |
| 669 | | arch.syscall6(arch.SYS_recvfrom, usize(fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), @ptrToInt(alen)) |
| 670 | | } |
| 671 | | |
| 672 | | pub fn shutdown(fd: i32, how: i32) -> usize { |
| 673 | | arch.syscall2(arch.SYS_shutdown, usize(fd), usize(how)) |
| 674 | | } |
| 675 | | |
| 676 | | pub fn bind(fd: i32, addr: &const sockaddr, len: socklen_t) -> usize { |
| 677 | | arch.syscall3(arch.SYS_bind, usize(fd), @ptrToInt(addr), usize(len)) |
| 678 | | } |
| 679 | | |
| 680 | | pub fn listen(fd: i32, backlog: i32) -> usize { |
| 681 | | arch.syscall2(arch.SYS_listen, usize(fd), usize(backlog)) |
| 682 | | } |
| 683 | | |
| 684 | | pub fn sendto(fd: i32, buf: &const u8, len: usize, flags: u32, addr: ?&const sockaddr, alen: socklen_t) -> usize { |
| 685 | | arch.syscall6(arch.SYS_sendto, usize(fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), usize(alen)) |
| 686 | | } |
| 687 | | |
| 688 | | pub fn socketpair(domain: i32, socket_type: i32, protocol: i32, fd: [2]i32) -> usize { |
| 689 | | arch.syscall4(arch.SYS_socketpair, usize(domain), usize(socket_type), usize(protocol), @ptrToInt(&fd[0])) |
| 690 | | } |
| 691 | | |
| 692 | | pub fn accept(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) -> usize { |
| 693 | | accept4(fd, addr, len, 0) |
| 694 | | } |
| 695 | | |
| 696 | | pub fn accept4(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t, flags: u32) -> usize { |
| 697 | | arch.syscall4(arch.SYS_accept4, usize(fd), @ptrToInt(addr), @ptrToInt(len), flags) |
| 698 | | } |
| 699 | | |
| 700 | | // error NameTooLong; |
| 701 | | // error SystemResources; |
| 702 | | // error Io; |
| 703 | | // |
| 704 | | // pub fn if_nametoindex(name: []u8) -> %u32 { |
| 705 | | // var ifr: ifreq = undefined; |
| 706 | | // |
| 707 | | // if (name.len >= ifr.ifr_name.len) { |
| 708 | | // return error.NameTooLong; |
| 709 | | // } |
| 710 | | // |
| 711 | | // const socket_ret = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0); |
| 712 | | // const socket_err = getErrno(socket_ret); |
| 713 | | // if (socket_err > 0) { |
| 714 | | // return error.SystemResources; |
| 715 | | // } |
| 716 | | // const socket_fd = i32(socket_ret); |
| 717 | | // @memcpy(&ifr.ifr_name[0], &name[0], name.len); |
| 718 | | // ifr.ifr_name[name.len] = 0; |
| 719 | | // const ioctl_ret = ioctl(socket_fd, SIOCGIFINDEX, &ifr); |
| 720 | | // close(socket_fd); |
| 721 | | // const ioctl_err = getErrno(ioctl_ret); |
| 722 | | // if (ioctl_err > 0) { |
| 723 | | // return error.Io; |
| 724 | | // } |
| 725 | | // return ifr.ifr_ifindex; |
| 726 | | // } |
| 727 | | |
| 728 | 567 | pub const Stat = arch.Stat; |
| 729 | 568 | pub const timespec = arch.timespec; |
| 730 | 569 | |
| 731 | | pub fn fstat(fd: i32, stat_buf: &Stat) -> usize { |
| 732 | | arch.syscall2(arch.SYS_fstat, usize(fd), @ptrToInt(stat_buf)) |
| 570 | pub fn fstat(fd: i32, stat_buf: *Stat) usize { |
| 571 | return arch.syscall2(arch.SYS_fstat, @intCast(usize, fd), @ptrToInt(stat_buf)); |
| 733 | 572 | } |