1 //! Hermit C type definitions 2 3 use c_void; 4 5 cfg_if! { 6 if #[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))] { 7 pub type c_char = u8; 8 } else { 9 pub type c_char = i8; 10 } 11 } 12 13 pub type c_schar = i8; 14 pub type c_uchar = u8; 15 pub type c_short = i16; 16 pub type c_ushort = u16; 17 pub type c_int = i32; 18 pub type c_uint = u32; 19 pub type c_long = i64; 20 pub type c_ulong = u64; 21 pub type c_longlong = i64; 22 pub type c_ulonglong = u64; 23 pub type intmax_t = i64; 24 pub type uintmax_t = u64; 25 pub type intptr_t = isize; 26 pub type uintptr_t = usize; 27 28 pub type c_float = f32; 29 pub type c_double = f64; 30 31 pub type size_t = usize; 32 pub type ssize_t = isize; 33 pub type ptrdiff_t = isize; 34 35 pub type clockid_t = i32; 36 pub type in_addr_t = u32; 37 pub type in_port_t = u16; 38 pub type mode_t = u32; 39 pub type nfds_t = usize; 40 pub type pid_t = i32; 41 pub type sa_family_t = u8; 42 pub type socklen_t = u32; 43 pub type time_t = i64; 44 45 s! { 46 pub struct addrinfo { 47 pub ai_flags: i32, 48 pub ai_family: i32, 49 pub ai_socktype: i32, 50 pub ai_protocol: i32, 51 pub ai_addrlen: socklen_t, 52 pub ai_canonname: *mut c_char, 53 pub ai_addr: *mut sockaddr, 54 pub ai_next: *mut addrinfo, 55 } 56 57 pub struct dirent64 { 58 pub d_ino: u64, 59 pub d_off: i64, 60 pub d_reclen: u16, 61 pub d_type: u8, 62 pub d_name: [c_char; 256], 63 } 64 65 #[repr(align(4))] 66 pub struct in6_addr { 67 pub s6_addr: [u8; 16], 68 } 69 70 pub struct in_addr { 71 pub s_addr: in_addr_t, 72 } 73 74 pub struct iovec { 75 iov_base: *mut c_void, 76 iov_len: usize, 77 } 78 79 pub struct pollfd { 80 pub fd: i32, 81 pub events: i16, 82 pub revents: i16, 83 } 84 85 pub struct sockaddr { 86 pub sa_len: u8, 87 pub sa_family: sa_family_t, 88 pub sa_data: [c_char; 14], 89 } 90 91 pub struct sockaddr_in { 92 pub sin_len: u8, 93 pub sin_family: sa_family_t, 94 pub sin_port: in_port_t, 95 pub sin_addr: in_addr, 96 pub sin_zero: [c_char; 8], 97 } 98 99 pub struct sockaddr_in6 { 100 pub sin6_len: u8, 101 pub sin6_family: sa_family_t, 102 pub sin6_port: in_port_t, 103 pub sin6_flowinfo: u32, 104 pub sin6_addr: in6_addr, 105 pub sin6_scope_id: u32, 106 } 107 108 pub struct sockaddr_storage { 109 pub ss_len: u8, 110 pub ss_family: sa_family_t, 111 __ss_pad1: [u8; 6], 112 __ss_align: i64, 113 __ss_pad2: [u8; 112], 114 } 115 116 pub struct stat { 117 pub st_dev: u64, 118 pub st_ino: u64, 119 pub st_nlink: u64, 120 pub st_mode: mode_t, 121 pub st_uid: u32, 122 pub st_gid: u32, 123 pub st_rdev: u64, 124 pub st_size: u64, 125 pub st_blksize: i64, 126 pub st_blocks: i64, 127 pub st_atim: timespec, 128 pub st_mtim: timespec, 129 pub st_ctim: timespec, 130 } 131 132 pub struct timespec { 133 pub tv_sec: time_t, 134 pub tv_nsec: i32, 135 } 136 } 137 138 pub const AF_INET: i32 = 0; 139 pub const AF_INET6: i32 = 1; 140 141 pub const CLOCK_REALTIME: clockid_t = 1; 142 pub const CLOCK_MONOTONIC: clockid_t = 4; 143 144 pub const DT_UNKNOWN: u8 = 0; 145 pub const DT_FIFO: u8 = 1; 146 pub const DT_CHR: u8 = 2; 147 pub const DT_DIR: u8 = 4; 148 pub const DT_BLK: u8 = 6; 149 pub const DT_REG: u8 = 8; 150 pub const DT_LNK: u8 = 10; 151 pub const DT_SOCK: u8 = 12; 152 pub const DT_WHT: u8 = 14; 153 154 pub const EAI_AGAIN: i32 = 2; 155 pub const EAI_BADFLAGS: i32 = 3; 156 pub const EAI_FAIL: i32 = 4; 157 pub const EAI_FAMILY: i32 = 5; 158 pub const EAI_MEMORY: i32 = 6; 159 pub const EAI_NODATA: i32 = 7; 160 pub const EAI_NONAME: i32 = 8; 161 pub const EAI_SERVICE: i32 = 9; 162 pub const EAI_SOCKTYPE: i32 = 10; 163 pub const EAI_SYSTEM: i32 = 11; 164 pub const EAI_OVERFLOW: i32 = 14; 165 166 pub const EFD_SEMAPHORE: i16 = 0o1; 167 pub const EFD_NONBLOCK: i16 = 0o4000; 168 pub const EFD_CLOEXEC: i16 = 0o40000; 169 170 pub const F_DUPFD: i32 = 0; 171 pub const F_GETFD: i32 = 1; 172 pub const F_SETFD: i32 = 2; 173 pub const F_GETFL: i32 = 3; 174 pub const F_SETFL: i32 = 4; 175 176 pub const FD_CLOEXEC: i32 = 1; 177 178 pub const FIONBIO: i32 = 0x8008667e; 179 180 pub const FUTEX_RELATIVE_TIMEOUT: u32 = 1; 181 182 pub const IP_TOS: i32 = 1; 183 pub const IP_TTL: i32 = 2; 184 pub const IP_ADD_MEMBERSHIP: i32 = 3; 185 pub const IP_DROP_MEMBERSHIP: i32 = 4; 186 pub const IP_MULTICAST_TTL: i32 = 5; 187 pub const IP_MULTICAST_LOOP: i32 = 7; 188 189 pub const IPPROTO_IP: i32 = 0; 190 pub const IPPROTO_TCP: i32 = 6; 191 pub const IPPROTO_UDP: i32 = 17; 192 pub const IPPROTO_IPV6: i32 = 41; 193 194 pub const IPV6_ADD_MEMBERSHIP: i32 = 12; 195 pub const IPV6_DROP_MEMBERSHIP: i32 = 13; 196 pub const IPV6_MULTICAST_LOOP: i32 = 19; 197 pub const IPV6_V6ONLY: i32 = 27; 198 199 pub const MSG_PEEK: i32 = 1; 200 201 pub const O_RDONLY: i32 = 0o0; 202 pub const O_WRONLY: i32 = 0o1; 203 pub const O_RDWR: i32 = 0o2; 204 pub const O_CREAT: i32 = 0o100; 205 pub const O_EXCL: i32 = 0o200; 206 pub const O_TRUNC: i32 = 0o1000; 207 pub const O_APPEND: i32 = 0o2000; 208 pub const O_NONBLOCK: i32 = 0o4000; 209 pub const O_DIRECTORY: i32 = 0o200000; 210 211 pub const POLLIN: i16 = 0x1; 212 pub const POLLPRI: i16 = 0x2; 213 pub const POLLOUT: i16 = 0x4; 214 pub const POLLERR: i16 = 0x8; 215 pub const POLLHUP: i16 = 0x10; 216 pub const POLLNVAL: i16 = 0x20; 217 pub const POLLRDNORM: i16 = 0x040; 218 pub const POLLRDBAND: i16 = 0x080; 219 pub const POLLWRNORM: i16 = 0x0100; 220 pub const POLLWRBAND: i16 = 0x0200; 221 pub const POLLRDHUP: i16 = 0x2000; 222 223 pub const S_IRWXU: mode_t = 0o0700; 224 pub const S_IRUSR: mode_t = 0o0400; 225 pub const S_IWUSR: mode_t = 0o0200; 226 pub const S_IXUSR: mode_t = 0o0100; 227 pub const S_IRWXG: mode_t = 0o0070; 228 pub const S_IRGRP: mode_t = 0o0040; 229 pub const S_IWGRP: mode_t = 0o0020; 230 pub const S_IXGRP: mode_t = 0o0010; 231 pub const S_IRWXO: mode_t = 0o0007; 232 pub const S_IROTH: mode_t = 0o0004; 233 pub const S_IWOTH: mode_t = 0o0002; 234 pub const S_IXOTH: mode_t = 0o0001; 235 236 pub const S_IFMT: mode_t = 0o17_0000; 237 pub const S_IFSOCK: mode_t = 0o14_0000; 238 pub const S_IFLNK: mode_t = 0o12_0000; 239 pub const S_IFREG: mode_t = 0o10_0000; 240 pub const S_IFBLK: mode_t = 0o6_0000; 241 pub const S_IFDIR: mode_t = 0o4_0000; 242 pub const S_IFCHR: mode_t = 0o2_0000; 243 pub const S_IFIFO: mode_t = 0o1_0000; 244 245 pub const SHUT_RD: i32 = 0; 246 pub const SHUT_WR: i32 = 1; 247 pub const SHUT_RDWR: i32 = 2; 248 249 pub const SO_REUSEADDR: i32 = 0x0004; 250 pub const SO_KEEPALIVE: i32 = 0x0008; 251 pub const SO_BROADCAST: i32 = 0x0020; 252 pub const SO_LINGER: i32 = 0x0080; 253 pub const SO_SNDBUF: i32 = 0x1001; 254 pub const SO_RCVBUF: i32 = 0x1002; 255 pub const SO_SNDTIMEO: i32 = 0x1005; 256 pub const SO_RCVTIMEO: i32 = 0x1006; 257 pub const SO_ERROR: i32 = 0x1007; 258 259 pub const SOCK_STREAM: i32 = 1; 260 pub const SOCK_DGRAM: i32 = 2; 261 pub const SOCK_NONBLOCK: i32 = 0o4000; 262 pub const SOCK_CLOEXEC: i32 = 0o40000; 263 264 pub const SOL_SOCKET: i32 = 4095; 265 266 pub const STDIN_FILENO: c_int = 0; 267 pub const STDOUT_FILENO: c_int = 1; 268 pub const STDERR_FILENO: c_int = 2; 269 270 pub const TCP_NODELAY: i32 = 1; 271 272 pub const EPERM: i32 = 1; 273 pub const ENOENT: i32 = 2; 274 pub const ESRCH: i32 = 3; 275 pub const EINTR: i32 = 4; 276 pub const EIO: i32 = 5; 277 pub const ENXIO: i32 = 6; 278 pub const E2BIG: i32 = 7; 279 pub const ENOEXEC: i32 = 8; 280 pub const EBADF: i32 = 9; 281 pub const ECHILD: i32 = 10; 282 pub const EAGAIN: i32 = 11; 283 pub const ENOMEM: i32 = 12; 284 pub const EACCES: i32 = 13; 285 pub const EFAULT: i32 = 14; 286 pub const ENOTBLK: i32 = 15; 287 pub const EBUSY: i32 = 16; 288 pub const EEXIST: i32 = 17; 289 pub const EXDEV: i32 = 18; 290 pub const ENODEV: i32 = 19; 291 pub const ENOTDIR: i32 = 20; 292 pub const EISDIR: i32 = 21; 293 pub const EINVAL: i32 = 22; 294 pub const ENFILE: i32 = 23; 295 pub const EMFILE: i32 = 24; 296 pub const ENOTTY: i32 = 25; 297 pub const ETXTBSY: i32 = 26; 298 pub const EFBIG: i32 = 27; 299 pub const ENOSPC: i32 = 28; 300 pub const ESPIPE: i32 = 29; 301 pub const EROFS: i32 = 30; 302 pub const EMLINK: i32 = 31; 303 pub const EPIPE: i32 = 32; 304 pub const EDOM: i32 = 33; 305 pub const ERANGE: i32 = 34; 306 pub const EDEADLK: i32 = 35; 307 pub const ENAMETOOLONG: i32 = 36; 308 pub const ENOLCK: i32 = 37; 309 pub const ENOSYS: i32 = 38; 310 pub const ENOTEMPTY: i32 = 39; 311 pub const ELOOP: i32 = 40; 312 pub const EWOULDBLOCK: i32 = EAGAIN; 313 pub const ENOMSG: i32 = 42; 314 pub const EIDRM: i32 = 43; 315 pub const ECHRNG: i32 = 44; 316 pub const EL2NSYNC: i32 = 45; 317 pub const EL3HLT: i32 = 46; 318 pub const EL3RST: i32 = 47; 319 pub const ELNRNG: i32 = 48; 320 pub const EUNATCH: i32 = 49; 321 pub const ENOCSI: i32 = 50; 322 pub const EL2HLT: i32 = 51; 323 pub const EBADE: i32 = 52; 324 pub const EBADR: i32 = 53; 325 pub const EXFULL: i32 = 54; 326 pub const ENOANO: i32 = 55; 327 pub const EBADRQC: i32 = 56; 328 pub const EBADSLT: i32 = 57; 329 pub const EDEADLOCK: i32 = EDEADLK; 330 pub const EBFONT: i32 = 59; 331 pub const ENOSTR: i32 = 60; 332 pub const ENODATA: i32 = 61; 333 pub const ETIME: i32 = 62; 334 pub const ENOSR: i32 = 63; 335 pub const ENONET: i32 = 64; 336 pub const ENOPKG: i32 = 65; 337 pub const EREMOTE: i32 = 66; 338 pub const ENOLINK: i32 = 67; 339 pub const EADV: i32 = 68; 340 pub const ESRMNT: i32 = 69; 341 pub const ECOMM: i32 = 70; 342 pub const EPROTO: i32 = 71; 343 pub const EMULTIHOP: i32 = 72; 344 pub const EDOTDOT: i32 = 73; 345 pub const EBADMSG: i32 = 74; 346 pub const EOVERFLOW: i32 = 75; 347 pub const ENOTUNIQ: i32 = 76; 348 pub const EBADFD: i32 = 77; 349 pub const EREMCHG: i32 = 78; 350 pub const ELIBACC: i32 = 79; 351 pub const ELIBBAD: i32 = 80; 352 pub const ELIBSCN: i32 = 81; 353 pub const ELIBMAX: i32 = 82; 354 pub const ELIBEXEC: i32 = 83; 355 pub const EILSEQ: i32 = 84; 356 pub const ERESTART: i32 = 85; 357 pub const ESTRPIPE: i32 = 86; 358 pub const EUSERS: i32 = 87; 359 pub const ENOTSOCK: i32 = 88; 360 pub const EDESTADDRREQ: i32 = 89; 361 pub const EMSGSIZE: i32 = 90; 362 pub const EPROTOTYPE: i32 = 91; 363 pub const ENOPROTOOPT: i32 = 92; 364 pub const EPROTONOSUPPORT: i32 = 93; 365 pub const ESOCKTNOSUPPORT: i32 = 94; 366 pub const EOPNOTSUPP: i32 = 95; 367 pub const EPFNOSUPPORT: i32 = 96; 368 pub const EAFNOSUPPORT: i32 = 97; 369 pub const EADDRINUSE: i32 = 98; 370 pub const EADDRNOTAVAIL: i32 = 99; 371 pub const ENETDOWN: i32 = 100; 372 pub const ENETUNREACH: i32 = 101; 373 pub const ENETRESET: i32 = 102; 374 pub const ECONNABORTED: i32 = 103; 375 pub const ECONNRESET: i32 = 104; 376 pub const ENOBUFS: i32 = 105; 377 pub const EISCONN: i32 = 106; 378 pub const ENOTCONN: i32 = 107; 379 pub const ESHUTDOWN: i32 = 108; 380 pub const ETOOMANYREFS: i32 = 109; 381 pub const ETIMEDOUT: i32 = 110; 382 pub const ECONNREFUSED: i32 = 111; 383 pub const EHOSTDOWN: i32 = 112; 384 pub const EHOSTUNREACH: i32 = 113; 385 pub const EALREADY: i32 = 114; 386 pub const EINPROGRESS: i32 = 115; 387 pub const ESTALE: i32 = 116; 388 pub const EUCLEAN: i32 = 117; 389 pub const ENOTNAM: i32 = 118; 390 pub const ENAVAIL: i32 = 119; 391 pub const EISNAM: i32 = 120; 392 pub const EREMOTEIO: i32 = 121; 393 pub const EDQUOT: i32 = 122; 394 pub const ENOMEDIUM: i32 = 123; 395 pub const EMEDIUMTYPE: i32 = 124; 396 pub const ECANCELED: i32 = 125; 397 pub const ENOKEY: i32 = 126; 398 pub const EKEYEXPIRED: i32 = 127; 399 pub const EKEYREVOKED: i32 = 128; 400 pub const EKEYREJECTED: i32 = 129; 401 pub const EOWNERDEAD: i32 = 130; 402 pub const ENOTRECOVERABLE: i32 = 131; 403 pub const ERFKILL: i32 = 132; 404 pub const EHWPOISON: i32 = 133; 405 406 extern "C" { 407 #[link_name = "sys_alloc"] 408 pub fn alloc(size: usize, align: usize) -> *mut u8; 409 410 #[link_name = "sys_alloc_zeroed"] 411 pub fn alloc_zeroed(size: usize, align: usize) -> *mut u8; 412 413 #[link_name = "sys_realloc"] 414 pub fn realloc(ptr: *mut u8, size: usize, align: usize, new_size: usize) -> *mut u8; 415 416 #[link_name = "sys_dealloc"] 417 pub fn dealloc(ptr: *mut u8, size: usize, align: usize); 418 419 #[link_name = "sys_exit"] 420 pub fn exit(status: i32) -> !; 421 422 #[link_name = "sys_abort"] 423 pub fn abort() -> !; 424 425 #[link_name = "sys_errno"] 426 pub fn errno() -> i32; 427 428 #[link_name = "sys_clock_gettime"] 429 pub fn clock_gettime(clockid: clockid_t, tp: *mut timespec) -> i32; 430 431 #[link_name = "sys_nanosleep"] 432 pub fn nanosleep(req: *const timespec) -> i32; 433 434 #[link_name = "sys_available_parallelism"] 435 pub fn available_parallelism() -> usize; 436 437 #[link_name = "sys_futex_wait"] 438 pub fn futex_wait( 439 address: *mut u32, 440 expected: u32, 441 timeout: *const timespec, 442 flags: u32, 443 ) -> i32; 444 445 #[link_name = "sys_futex_wake"] 446 pub fn futex_wake(address: *mut u32, count: i32) -> i32; 447 448 #[link_name = "sys_stat"] 449 pub fn stat(path: *const c_char, stat: *mut stat) -> i32; 450 451 #[link_name = "sys_fstat"] 452 pub fn fstat(fd: i32, stat: *mut stat) -> i32; 453 454 #[link_name = "sys_lstat"] 455 pub fn lstat(path: *const c_char, stat: *mut stat) -> i32; 456 457 #[link_name = "sys_open"] 458 pub fn open(path: *const c_char, flags: i32, mode: mode_t) -> i32; 459 460 #[link_name = "sys_unlink"] 461 pub fn unlink(path: *const c_char) -> i32; 462 463 #[link_name = "sys_mkdir"] 464 pub fn mkdir(path: *const c_char, mode: mode_t) -> i32; 465 466 #[link_name = "sys_rmdir"] 467 pub fn rmdir(path: *const c_char) -> i32; 468 469 #[link_name = "sys_read"] 470 pub fn read(fd: i32, buf: *mut u8, len: usize) -> isize; 471 472 #[link_name = "sys_write"] 473 pub fn write(fd: i32, buf: *const u8, len: usize) -> isize; 474 475 #[link_name = "sys_readv"] 476 pub fn readv(fd: i32, iov: *const iovec, iovcnt: usize) -> isize; 477 478 #[link_name = "sys_writev"] 479 pub fn writev(fd: i32, iov: *const iovec, iovcnt: usize) -> isize; 480 481 #[link_name = "sys_close"] 482 pub fn close(fd: i32) -> i32; 483 484 #[link_name = "sys_dup"] 485 pub fn dup(fd: i32) -> i32; 486 487 #[link_name = "sys_fcntl"] 488 pub fn fcntl(fd: i32, cmd: i32, arg: i32) -> i32; 489 490 #[link_name = "sys_getdents64"] 491 pub fn getdents64(fd: i32, dirp: *mut dirent64, count: usize) -> isize; 492 493 #[link_name = "sys_getaddrinfo"] 494 pub fn getaddrinfo( 495 nodename: *const c_char, 496 servname: *const c_char, 497 hints: *const addrinfo, 498 res: *mut *mut addrinfo, 499 ) -> i32; 500 501 #[link_name = "sys_freeaddrinfo"] 502 pub fn freeaddrinfo(ai: *mut addrinfo); 503 504 #[link_name = "sys_socket"] 505 pub fn socket(domain: i32, ty: i32, protocol: i32) -> i32; 506 507 #[link_name = "sys_bind"] 508 pub fn bind(sockfd: i32, addr: *const sockaddr, addrlen: socklen_t) -> i32; 509 510 #[link_name = "sys_listen"] 511 pub fn listen(sockfd: i32, backlog: i32) -> i32; 512 513 #[link_name = "sys_accept"] 514 pub fn accept(sockfd: i32, addr: *mut sockaddr, addrlen: *mut socklen_t) -> i32; 515 516 #[link_name = "sys_connect"] 517 pub fn connect(sockfd: i32, addr: *const sockaddr, addrlen: socklen_t) -> i32; 518 519 #[link_name = "sys_recv"] 520 pub fn recv(sockfd: i32, buf: *mut u8, len: usize, flags: i32) -> isize; 521 522 #[link_name = "sys_recvfrom"] 523 pub fn recvfrom( 524 sockfd: i32, 525 buf: *mut c_void, 526 len: usize, 527 flags: i32, 528 addr: *mut sockaddr, 529 addrlen: *mut socklen_t, 530 ) -> isize; 531 532 #[link_name = "sys_send"] 533 pub fn send(sockfd: i32, buf: *const c_void, len: usize, flags: i32) -> isize; 534 535 #[link_name = "sys_sendto"] 536 pub fn sendto( 537 sockfd: i32, 538 buf: *const c_void, 539 len: usize, 540 flags: i32, 541 to: *const sockaddr, 542 tolen: socklen_t, 543 ) -> isize; 544 545 #[link_name = "sys_getpeername"] 546 pub fn getpeername(sockfd: i32, addr: *mut sockaddr, addrlen: *mut socklen_t) -> i32; 547 548 #[link_name = "sys_getsockname"] 549 pub fn getsockname(sockfd: i32, addr: *mut sockaddr, addrlen: *mut socklen_t) -> i32; 550 551 #[link_name = "sys_getsockopt"] 552 pub fn getsockopt( 553 sockfd: i32, 554 level: i32, 555 optname: i32, 556 optval: *mut c_void, 557 optlen: *mut socklen_t, 558 ) -> i32; 559 560 #[link_name = "sys_setsockopt"] 561 pub fn setsockopt( 562 sockfd: i32, 563 level: i32, 564 optname: i32, 565 optval: *const c_void, 566 optlen: socklen_t, 567 ) -> i32; 568 569 #[link_name = "sys_ioctl"] 570 pub fn ioctl(sockfd: i32, cmd: i32, argp: *mut c_void) -> i32; 571 572 #[link_name = "sys_shutdown"] 573 pub fn shutdown(sockfd: i32, how: i32) -> i32; 574 575 #[link_name = "sys_eventfd"] 576 pub fn eventfd(initval: u64, flags: i16) -> i32; 577 578 #[link_name = "sys_poll"] 579 pub fn poll(fds: *mut pollfd, nfds: nfds_t, timeout: i32) -> i32; 580 } 581