zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

blob d3a3bfde (13219B) - Raw


      1 const std = @import("../std.zig");
      2 const builtin = @import("builtin");
      3 const maxInt = std.math.maxInt;
      4 const native_abi = builtin.abi;
      5 const native_arch = builtin.cpu.arch;
      6 const linux = std.os.linux;
      7 const iovec = std.os.iovec;
      8 const iovec_const = std.os.iovec_const;
      9 const FILE = std.c.FILE;
     10 
     11 pub const AF = linux.AF;
     12 pub const ARCH = linux.ARCH;
     13 pub const AT = linux.AT;
     14 pub const CLOCK = linux.CLOCK;
     15 pub const CPU_COUNT = linux.CPU_COUNT;
     16 pub const CPU_SET = linux.CPU_SET;
     17 pub const CPU_ISSET = linux.CPU_ISSET;
     18 pub const CPU_CLR = linux.CPU_CLR;
     19 pub const CPU_ZERO = linux.CPU_ZERO;
     20 pub const E = linux.E;
     21 pub const Elf_Symndx = linux.Elf_Symndx;
     22 pub const F = linux.F;
     23 pub const FD_CLOEXEC = linux.FD_CLOEXEC;
     24 pub const F_OK = linux.F_OK;
     25 pub const Flock = linux.Flock;
     26 pub const HOST_NAME_MAX = linux.HOST_NAME_MAX;
     27 pub const IFNAMESIZE = linux.IFNAMESIZE;
     28 pub const IOV_MAX = linux.IOV_MAX;
     29 pub const IPPROTO = linux.IPPROTO;
     30 pub const LOCK = linux.LOCK;
     31 pub const MADV = linux.MADV;
     32 pub const MAP = struct {
     33     pub usingnamespace linux.MAP;
     34     /// Only used by libc to communicate failure.
     35     pub const FAILED = @ptrFromInt(*anyopaque, maxInt(usize));
     36 };
     37 pub const MSF = linux.MSF;
     38 pub const MMAP2_UNIT = linux.MMAP2_UNIT;
     39 pub const MSG = linux.MSG;
     40 pub const NAME_MAX = linux.NAME_MAX;
     41 pub const O = linux.O;
     42 pub const PATH_MAX = linux.PATH_MAX;
     43 pub const POLL = linux.POLL;
     44 pub const PROT = linux.PROT;
     45 pub const REG = linux.REG;
     46 pub const RLIM = linux.RLIM;
     47 pub const R_OK = linux.R_OK;
     48 pub const S = linux.S;
     49 pub const SA = linux.SA;
     50 pub const SC = linux.SC;
     51 pub const SEEK = linux.SEEK;
     52 pub const SHUT = linux.SHUT;
     53 pub const SIG = linux.SIG;
     54 pub const SIOCGIFINDEX = linux.SIOCGIFINDEX;
     55 pub const SO = linux.SO;
     56 pub const SOCK = linux.SOCK;
     57 pub const SOL = linux.SOL;
     58 pub const STDERR_FILENO = linux.STDERR_FILENO;
     59 pub const STDIN_FILENO = linux.STDIN_FILENO;
     60 pub const STDOUT_FILENO = linux.STDOUT_FILENO;
     61 pub const SYS = linux.SYS;
     62 pub const Sigaction = linux.Sigaction;
     63 pub const TCP = linux.TCP;
     64 pub const TCSA = linux.TCSA;
     65 pub const VDSO = linux.VDSO;
     66 pub const W = linux.W;
     67 pub const W_OK = linux.W_OK;
     68 pub const X_OK = linux.X_OK;
     69 pub const addrinfo = linux.addrinfo;
     70 pub const blkcnt_t = linux.blkcnt_t;
     71 pub const blksize_t = linux.blksize_t;
     72 pub const clock_t = linux.clock_t;
     73 pub const cpu_set_t = linux.cpu_set_t;
     74 pub const dev_t = linux.dev_t;
     75 pub const dl_phdr_info = linux.dl_phdr_info;
     76 pub const empty_sigset = linux.empty_sigset;
     77 pub const epoll_event = linux.epoll_event;
     78 pub const fd_t = linux.fd_t;
     79 pub const gid_t = linux.gid_t;
     80 pub const ifreq = linux.ifreq;
     81 pub const ino_t = linux.ino_t;
     82 pub const mcontext_t = linux.mcontext_t;
     83 pub const mode_t = linux.mode_t;
     84 pub const msghdr = linux.msghdr;
     85 pub const msghdr_const = linux.msghdr_const;
     86 pub const nfds_t = linux.nfds_t;
     87 pub const nlink_t = linux.nlink_t;
     88 pub const off_t = linux.off_t;
     89 pub const pid_t = linux.pid_t;
     90 pub const pollfd = linux.pollfd;
     91 pub const rlim_t = linux.rlim_t;
     92 pub const rlimit = linux.rlimit;
     93 pub const rlimit_resource = linux.rlimit_resource;
     94 pub const rusage = linux.rusage;
     95 pub const siginfo_t = linux.siginfo_t;
     96 pub const sigset_t = linux.sigset_t;
     97 pub const sockaddr = linux.sockaddr;
     98 pub const socklen_t = linux.socklen_t;
     99 pub const stack_t = linux.stack_t;
    100 pub const tcflag_t = linux.tcflag_t;
    101 pub const termios = linux.termios;
    102 pub const time_t = linux.time_t;
    103 pub const timer_t = linux.timer_t;
    104 pub const timespec = linux.timespec;
    105 pub const timeval = linux.timeval;
    106 pub const timezone = linux.timezone;
    107 pub const ucontext_t = linux.ucontext_t;
    108 pub const uid_t = linux.uid_t;
    109 pub const user_desc = linux.user_desc;
    110 pub const utsname = linux.utsname;
    111 pub const PR = linux.PR;
    112 
    113 pub const _errno = switch (native_abi) {
    114     .android => struct {
    115         extern fn __errno() *c_int;
    116     }.__errno,
    117     else => struct {
    118         extern "c" fn __errno_location() *c_int;
    119     }.__errno_location,
    120 };
    121 
    122 pub const Stat = switch (native_arch) {
    123     .sparc64 => extern struct {
    124         dev: u64,
    125         __pad1: u16,
    126         ino: ino_t,
    127         mode: u32,
    128         nlink: u32,
    129 
    130         uid: u32,
    131         gid: u32,
    132         rdev: u64,
    133         __pad2: u16,
    134 
    135         size: off_t,
    136         blksize: isize,
    137         blocks: i64,
    138 
    139         atim: timespec,
    140         mtim: timespec,
    141         ctim: timespec,
    142         __reserved: [2]usize,
    143 
    144         pub fn atime(self: @This()) timespec {
    145             return self.atim;
    146         }
    147 
    148         pub fn mtime(self: @This()) timespec {
    149             return self.mtim;
    150         }
    151 
    152         pub fn ctime(self: @This()) timespec {
    153             return self.ctim;
    154         }
    155     },
    156     .mips, .mipsel => extern struct {
    157         dev: dev_t,
    158         __pad0: [2]u32,
    159         ino: ino_t,
    160         mode: mode_t,
    161         nlink: nlink_t,
    162         uid: uid_t,
    163         gid: gid_t,
    164         rdev: dev_t,
    165         __pad1: [2]u32,
    166         size: off_t,
    167         atim: timespec,
    168         mtim: timespec,
    169         ctim: timespec,
    170         blksize: blksize_t,
    171         __pad3: u32,
    172         blocks: blkcnt_t,
    173         __pad4: [14]u32,
    174 
    175         pub fn atime(self: @This()) timespec {
    176             return self.atim;
    177         }
    178 
    179         pub fn mtime(self: @This()) timespec {
    180             return self.mtim;
    181         }
    182 
    183         pub fn ctime(self: @This()) timespec {
    184             return self.ctim;
    185         }
    186     },
    187 
    188     else => std.os.linux.Stat, // libc stat is the same as kernel stat.
    189 };
    190 
    191 pub const AI = struct {
    192     pub const PASSIVE = 0x01;
    193     pub const CANONNAME = 0x02;
    194     pub const NUMERICHOST = 0x04;
    195     pub const V4MAPPED = 0x08;
    196     pub const ALL = 0x10;
    197     pub const ADDRCONFIG = 0x20;
    198     pub const NUMERICSERV = 0x400;
    199 };
    200 
    201 pub const NI = struct {
    202     pub const NUMERICHOST = 0x01;
    203     pub const NUMERICSERV = 0x02;
    204     pub const NOFQDN = 0x04;
    205     pub const NAMEREQD = 0x08;
    206     pub const DGRAM = 0x10;
    207     pub const NUMERICSCOPE = 0x100;
    208 };
    209 
    210 pub const EAI = enum(c_int) {
    211     BADFLAGS = -1,
    212     NONAME = -2,
    213     AGAIN = -3,
    214     FAIL = -4,
    215     FAMILY = -6,
    216     SOCKTYPE = -7,
    217     SERVICE = -8,
    218     MEMORY = -10,
    219     SYSTEM = -11,
    220     OVERFLOW = -12,
    221 
    222     NODATA = -5,
    223     ADDRFAMILY = -9,
    224     INPROGRESS = -100,
    225     CANCELED = -101,
    226     NOTCANCELED = -102,
    227     ALLDONE = -103,
    228     INTR = -104,
    229     IDN_ENCODE = -105,
    230 
    231     _,
    232 };
    233 
    234 pub extern "c" fn fallocate64(fd: fd_t, mode: c_int, offset: off_t, len: off_t) c_int;
    235 pub extern "c" fn fopen64(noalias filename: [*:0]const u8, noalias modes: [*:0]const u8) ?*FILE;
    236 pub extern "c" fn fstat64(fd: fd_t, buf: *Stat) c_int;
    237 pub extern "c" fn fstatat64(dirfd: fd_t, noalias path: [*:0]const u8, noalias stat_buf: *Stat, flags: u32) c_int;
    238 pub extern "c" fn ftruncate64(fd: c_int, length: off_t) c_int;
    239 pub extern "c" fn getrlimit64(resource: rlimit_resource, rlim: *rlimit) c_int;
    240 pub extern "c" fn lseek64(fd: fd_t, offset: i64, whence: c_int) i64;
    241 pub extern "c" fn mmap64(addr: ?*align(std.mem.page_size) anyopaque, len: usize, prot: c_uint, flags: c_uint, fd: fd_t, offset: i64) *anyopaque;
    242 pub extern "c" fn open64(path: [*:0]const u8, oflag: c_uint, ...) c_int;
    243 pub extern "c" fn openat64(fd: c_int, path: [*:0]const u8, oflag: c_uint, ...) c_int;
    244 pub extern "c" fn pread64(fd: fd_t, buf: [*]u8, nbyte: usize, offset: i64) isize;
    245 pub extern "c" fn preadv64(fd: c_int, iov: [*]const iovec, iovcnt: c_uint, offset: i64) isize;
    246 pub extern "c" fn pwrite64(fd: fd_t, buf: [*]const u8, nbyte: usize, offset: i64) isize;
    247 pub extern "c" fn pwritev64(fd: c_int, iov: [*]const iovec_const, iovcnt: c_uint, offset: i64) isize;
    248 pub extern "c" fn sendfile64(out_fd: fd_t, in_fd: fd_t, offset: ?*i64, count: usize) isize;
    249 pub extern "c" fn setrlimit64(resource: rlimit_resource, rlim: *const rlimit) c_int;
    250 
    251 pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) isize;
    252 pub extern "c" fn sched_getaffinity(pid: c_int, size: usize, set: *cpu_set_t) c_int;
    253 pub extern "c" fn sched_setaffinity(pid: c_int, size: usize, set: *const cpu_set_t) c_int;
    254 pub extern "c" fn eventfd(initval: c_uint, flags: c_uint) c_int;
    255 pub extern "c" fn epoll_ctl(epfd: fd_t, op: c_uint, fd: fd_t, event: ?*epoll_event) c_int;
    256 pub extern "c" fn epoll_create1(flags: c_uint) c_int;
    257 pub extern "c" fn epoll_wait(epfd: fd_t, events: [*]epoll_event, maxevents: c_uint, timeout: c_int) c_int;
    258 pub extern "c" fn epoll_pwait(
    259     epfd: fd_t,
    260     events: [*]epoll_event,
    261     maxevents: c_int,
    262     timeout: c_int,
    263     sigmask: *const sigset_t,
    264 ) c_int;
    265 pub extern "c" fn inotify_init1(flags: c_uint) c_int;
    266 pub extern "c" fn inotify_add_watch(fd: fd_t, pathname: [*:0]const u8, mask: u32) c_int;
    267 pub extern "c" fn inotify_rm_watch(fd: fd_t, wd: c_int) c_int;
    268 
    269 /// See std.elf for constants for this
    270 pub extern "c" fn getauxval(__type: c_ulong) c_ulong;
    271 
    272 pub const dl_iterate_phdr_callback = *const fn (info: *dl_phdr_info, size: usize, data: ?*anyopaque) callconv(.C) c_int;
    273 
    274 pub extern "c" fn dl_iterate_phdr(callback: dl_iterate_phdr_callback, data: ?*anyopaque) c_int;
    275 
    276 pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int;
    277 
    278 pub extern "c" fn memfd_create(name: [*:0]const u8, flags: c_uint) c_int;
    279 pub extern "c" fn pipe2(fds: *[2]fd_t, flags: u32) c_int;
    280 
    281 pub extern "c" fn fallocate(fd: fd_t, mode: c_int, offset: off_t, len: off_t) c_int;
    282 
    283 pub extern "c" fn sendfile(
    284     out_fd: fd_t,
    285     in_fd: fd_t,
    286     offset: ?*off_t,
    287     count: usize,
    288 ) isize;
    289 
    290 pub extern "c" fn copy_file_range(fd_in: fd_t, off_in: ?*i64, fd_out: fd_t, off_out: ?*i64, len: usize, flags: c_uint) isize;
    291 
    292 pub extern "c" fn signalfd(fd: fd_t, mask: *const sigset_t, flags: c_uint) c_int;
    293 
    294 pub extern "c" fn prlimit(pid: pid_t, resource: rlimit_resource, new_limit: *const rlimit, old_limit: *rlimit) c_int;
    295 pub extern "c" fn posix_memalign(memptr: *?*anyopaque, alignment: usize, size: usize) c_int;
    296 pub extern "c" fn malloc_usable_size(?*const anyopaque) usize;
    297 
    298 pub extern "c" fn mincore(
    299     addr: *align(std.mem.page_size) anyopaque,
    300     length: usize,
    301     vec: [*]u8,
    302 ) c_int;
    303 
    304 pub extern "c" fn madvise(
    305     addr: *align(std.mem.page_size) anyopaque,
    306     length: usize,
    307     advice: c_uint,
    308 ) c_int;
    309 
    310 pub const pthread_attr_t = extern struct {
    311     __size: [56]u8,
    312     __align: c_long,
    313 };
    314 
    315 pub const pthread_mutex_t = extern struct {
    316     size: [__SIZEOF_PTHREAD_MUTEX_T]u8 align(@alignOf(usize)) = [_]u8{0} ** __SIZEOF_PTHREAD_MUTEX_T,
    317 };
    318 pub const pthread_cond_t = extern struct {
    319     size: [__SIZEOF_PTHREAD_COND_T]u8 align(@alignOf(usize)) = [_]u8{0} ** __SIZEOF_PTHREAD_COND_T,
    320 };
    321 pub const pthread_rwlock_t = switch (native_abi) {
    322     .android => switch (@sizeOf(usize)) {
    323         4 => extern struct {
    324             size: [40]u8 align(@alignOf(usize)) = [_]u8{0} ** 40,
    325         },
    326         8 => extern struct {
    327             size: [56]u8 align(@alignOf(usize)) = [_]u8{0} ** 56,
    328         },
    329         else => @compileError("impossible pointer size"),
    330     },
    331     else => extern struct {
    332         size: [56]u8 align(@alignOf(usize)) = [_]u8{0} ** 56,
    333     },
    334 };
    335 pub const pthread_key_t = c_uint;
    336 pub const sem_t = extern struct {
    337     __size: [__SIZEOF_SEM_T]u8 align(@alignOf(usize)),
    338 };
    339 
    340 const __SIZEOF_PTHREAD_COND_T = 48;
    341 const __SIZEOF_PTHREAD_MUTEX_T = switch (native_abi) {
    342     .musl, .musleabi, .musleabihf => if (@sizeOf(usize) == 8) 40 else 24,
    343     .gnu, .gnuabin32, .gnuabi64, .gnueabi, .gnueabihf, .gnux32 => switch (native_arch) {
    344         .aarch64 => 48,
    345         .x86_64 => if (native_abi == .gnux32) 40 else 32,
    346         .mips64, .powerpc64, .powerpc64le, .sparc64 => 40,
    347         else => if (@sizeOf(usize) == 8) 40 else 24,
    348     },
    349     .android => if (@sizeOf(usize) == 8) 40 else 4,
    350     else => @compileError("unsupported ABI"),
    351 };
    352 const __SIZEOF_SEM_T = 4 * @sizeOf(usize);
    353 
    354 /// TODO refines if necessary
    355 pub const PTHREAD_STACK_MIN = switch (native_abi) {
    356     .musl, .musleabi, .musleabihf => 2048,
    357     .gnu, .gnuabin32, .gnuabi64, .gnueabi, .gnueabihf, .gnux32 => switch (native_arch) {
    358         .aarch64, .arm, .armeb, .powerpc, .powerpc64, .powerpc64le, .loongarch32, .loongarch64 => 131072,
    359         .sparc64 => 24576,
    360         else => 16 * 1024,
    361     },
    362     .android => if (@sizeOf(usize) == 8) 16 * 1024 else 8 * 1024,
    363     else => 16 * 1024,
    364 };
    365 
    366 pub extern "c" fn pthread_setname_np(thread: std.c.pthread_t, name: [*:0]const u8) E;
    367 pub extern "c" fn pthread_getname_np(thread: std.c.pthread_t, name: [*:0]u8, len: usize) E;
    368 
    369 pub const RTLD = struct {
    370     pub const LAZY = 1;
    371     pub const NOW = 2;
    372     pub const NOLOAD = 4;
    373     pub const NODELETE = 4096;
    374     pub const GLOBAL = 256;
    375     pub const LOCAL = 0;
    376 };
    377 
    378 pub const dirent = struct {
    379     d_ino: c_uint,
    380     d_off: c_uint,
    381     d_reclen: c_ushort,
    382     d_type: u8,
    383     d_name: [256]u8,
    384 };
    385 pub const dirent64 = struct {
    386     d_ino: c_ulong,
    387     d_off: c_ulong,
    388     d_reclen: c_ushort,
    389     d_type: u8,
    390     d_name: [256]u8,
    391 };
    392 
    393 pub const MPOL = struct {
    394     pub const F_NODE = 1 << 0;
    395     pub const F_ADDR = 1 << 1;
    396     pub const F_MEMS_ALLOWED = 1 << 2;
    397     /// flags for SYS_mbind
    398     pub const MF_STRICT = 1 << 0;
    399     pub const MF_MOVE = 1 << 1;
    400     pub const MF_MOVE_ALL = 1 << 2;
    401     pub const MF_LAZY = 1 << 3;
    402     pub const MF_INTERNAL = 1 << 4;
    403     pub const MF_VALID = MPOL.MF_STRICT | MPOL.MF_MOVE | MPOL.MOVE_ALL;
    404 };
    405 
    406 pub extern "c" fn getcpu(cpu: *c_uint, node: *c_uint) c_int;
    407 pub extern "c" fn sched_getcpu() c_int;