1#![no_std]
5#![allow(nonstandard_style)]
6#![allow(dead_code)]
7#![allow(clippy::missing_safety_doc)]
8#![allow(clippy::result_unit_err)]
9
10pub mod errno;
11
12use core::ffi::c_char;
13pub use core::ffi::{c_int, c_short, c_void};
14
15pub use self::errno::*;
16
17pub type Tid = u32;
19
20pub const NO_PRIORITIES: usize = 31;
22
23#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Copy)]
25pub struct Priority(u8);
26
27impl Priority {
28 pub const fn into(self) -> u8 {
29 self.0
30 }
31
32 pub const fn from(x: u8) -> Self {
33 Priority(x)
34 }
35}
36
37pub const HIGH_PRIO: Priority = Priority::from(3);
38pub const NORMAL_PRIO: Priority = Priority::from(2);
39pub const LOW_PRIO: Priority = Priority::from(1);
40
41pub const FUTEX_RELATIVE_TIMEOUT: u32 = 1;
42pub const CLOCK_REALTIME: clockid_t = 1;
43pub const CLOCK_MONOTONIC: clockid_t = 4;
44pub const STDIN_FILENO: c_int = 0;
45pub const STDOUT_FILENO: c_int = 1;
46pub const STDERR_FILENO: c_int = 2;
47pub const O_RDONLY: i32 = 0o0;
48pub const O_WRONLY: i32 = 0o1;
49pub const O_RDWR: i32 = 0o2;
50pub const O_CREAT: i32 = 0o100;
51pub const O_EXCL: i32 = 0o200;
52pub const O_TRUNC: i32 = 0o1000;
53pub const O_APPEND: i32 = 0o2000;
54pub const O_NONBLOCK: i32 = 0o4000;
55pub const O_DIRECTORY: i32 = 0o200000;
56pub const F_DUPFD: i32 = 0;
57pub const F_GETFD: i32 = 1;
58pub const F_SETFD: i32 = 2;
59pub const F_GETFL: i32 = 3;
60pub const F_SETFL: i32 = 4;
61pub const FD_CLOEXEC: i32 = 1;
62
63pub fn isatty(_fd: c_int) -> bool {
65 false
66}
67
68#[derive(Default, Copy, Clone, Debug)]
71#[repr(C)]
72pub struct timespec {
73 pub tv_sec: time_t,
75 pub tv_nsec: i32,
77}
78
79#[repr(C)]
80#[derive(Debug, Copy, Clone)]
81pub struct timeval {
82 pub tv_sec: time_t,
83 pub tv_usec: suseconds_t,
84}
85
86pub const RAND_MAX: i32 = 2_147_483_647;
88
89pub const AF_UNSPEC: i32 = 0;
90pub const AF_INET: i32 = 3;
92pub const AF_INET6: i32 = 1;
94pub const AF_VSOCK: i32 = 2;
96pub const IPPROTO_IP: i32 = 0;
97pub const IPPROTO_IPV6: i32 = 41;
98pub const IPPROTO_UDP: i32 = 17;
99pub const IPPROTO_TCP: i32 = 6;
100pub const IPV6_ADD_MEMBERSHIP: i32 = 12;
101pub const IPV6_DROP_MEMBERSHIP: i32 = 13;
102pub const IPV6_MULTICAST_LOOP: i32 = 19;
103pub const IPV6_V6ONLY: i32 = 27;
104pub const IP_TOS: i32 = 1;
105pub const IP_TTL: i32 = 2;
106pub const IP_MULTICAST_TTL: i32 = 5;
107pub const IP_MULTICAST_LOOP: i32 = 7;
108pub const IP_ADD_MEMBERSHIP: i32 = 3;
109pub const IP_DROP_MEMBERSHIP: i32 = 4;
110pub const SHUT_RD: i32 = 0;
111pub const SHUT_WR: i32 = 1;
112pub const SHUT_RDWR: i32 = 2;
113pub const SOCK_DGRAM: i32 = 2;
115pub const SOCK_STREAM: i32 = 1;
117pub const SOCK_NONBLOCK: i32 = 0o4000;
119pub const SOCK_CLOEXEC: i32 = 0o40000;
121pub const SOL_SOCKET: i32 = 4095;
122pub const SO_REUSEADDR: i32 = 0x0004;
123pub const SO_KEEPALIVE: i32 = 0x0008;
124pub const SO_BROADCAST: i32 = 0x0020;
125pub const SO_LINGER: i32 = 0x0080;
126pub const SO_SNDBUF: i32 = 0x1001;
127pub const SO_RCVBUF: i32 = 0x1002;
128pub const SO_SNDTIMEO: i32 = 0x1005;
129pub const SO_RCVTIMEO: i32 = 0x1006;
130pub const SO_ERROR: i32 = 0x1007;
131pub const TCP_NODELAY: i32 = 1;
132pub const MSG_PEEK: i32 = 1;
133pub const FIONBIO: i32 = 0x8008667eu32 as i32;
134pub const EAI_AGAIN: i32 = 2;
135pub const EAI_BADFLAGS: i32 = 3;
136pub const EAI_FAIL: i32 = 4;
137pub const EAI_FAMILY: i32 = 5;
138pub const EAI_MEMORY: i32 = 6;
139pub const EAI_NODATA: i32 = 7;
140pub const EAI_NONAME: i32 = 8;
141pub const EAI_SERVICE: i32 = 9;
142pub const EAI_SOCKTYPE: i32 = 10;
143pub const EAI_SYSTEM: i32 = 11;
144pub const EAI_OVERFLOW: i32 = 14;
145pub const POLLIN: i16 = 0x1;
146pub const POLLPRI: i16 = 0x2;
147pub const POLLOUT: i16 = 0x4;
148pub const POLLERR: i16 = 0x8;
149pub const POLLHUP: i16 = 0x10;
150pub const POLLNVAL: i16 = 0x20;
151pub const POLLRDNORM: i16 = 0x040;
152pub const POLLRDBAND: i16 = 0x080;
153pub const POLLWRNORM: i16 = 0x0100;
154pub const POLLWRBAND: i16 = 0x0200;
155pub const POLLRDHUP: i16 = 0x2000;
156pub const EFD_SEMAPHORE: i16 = 0o1;
157pub const EFD_NONBLOCK: i16 = 0o4000;
158pub const EFD_CLOEXEC: i16 = 0o40000;
159pub const IOV_MAX: usize = 1024;
160pub const VMADDR_CID_ANY: u32 = u32::MAX;
162pub const VMADDR_CID_HYPERVISOR: u32 = 0;
163pub const VMADDR_CID_LOCAL: u32 = 1;
164pub const VMADDR_CID_HOST: u32 = 2;
165pub type sa_family_t = u8;
166pub type socklen_t = u32;
167pub type in_addr_t = u32;
168pub type in_port_t = u16;
169pub type time_t = i64;
170pub type useconds_t = u32;
171pub type suseconds_t = i32;
172pub type nfds_t = usize;
173pub type sem_t = *const c_void;
174pub type pid_t = i32;
175pub type clockid_t = i32;
176
177#[repr(C)]
178#[derive(Debug, Copy, Clone, Default)]
179pub struct in_addr {
180 pub s_addr: in_addr_t,
181}
182
183#[repr(C, align(4))]
184#[derive(Debug, Copy, Clone, Default)]
185pub struct in6_addr {
186 pub s6_addr: [u8; 16],
187}
188
189#[repr(C)]
190#[derive(Debug, Copy, Clone, Default)]
191pub struct sockaddr {
192 pub sa_len: u8,
193 pub sa_family: sa_family_t,
194 pub sa_data: [c_char; 14],
195}
196
197#[repr(C)]
198#[derive(Debug, Copy, Clone, Default)]
199pub struct sockaddr_vm {
200 pub svm_len: u8,
201 pub svm_family: sa_family_t,
202 pub svm_reserved1: u16,
203 pub svm_port: u32,
204 pub svm_cid: u32,
205 pub svm_zero: [u8; 4],
206}
207
208#[repr(C)]
209#[derive(Debug, Copy, Clone, Default)]
210pub struct sockaddr_in {
211 pub sin_len: u8,
212 pub sin_family: sa_family_t,
213 pub sin_port: in_port_t,
214 pub sin_addr: in_addr,
215 pub sin_zero: [c_char; 8],
216}
217
218#[repr(C)]
219#[derive(Debug, Copy, Clone, Default)]
220pub struct sockaddr_in6 {
221 pub sin6_len: u8,
222 pub sin6_family: sa_family_t,
223 pub sin6_port: in_port_t,
224 pub sin6_flowinfo: u32,
225 pub sin6_addr: in6_addr,
226 pub sin6_scope_id: u32,
227}
228
229#[repr(C)]
230#[derive(Debug, Copy, Clone)]
231pub struct addrinfo {
232 pub ai_flags: i32,
233 pub ai_family: i32,
234 pub ai_socktype: i32,
235 pub ai_protocol: i32,
236 pub ai_addrlen: socklen_t,
237 pub ai_canonname: *mut c_char,
238 pub ai_addr: *mut sockaddr,
239 pub ai_next: *mut addrinfo,
240}
241
242#[repr(C)]
243#[derive(Debug, Copy, Clone)]
244pub struct sockaddr_storage {
245 pub s2_len: u8,
246 pub ss_family: sa_family_t,
247 __ss_pad1: [u8; 6],
248 __ss_align: i64,
249 __ss_pad2: [u8; 112],
250}
251
252#[repr(C)]
253#[derive(Debug, Copy, Clone, Default)]
254pub struct ip_mreq {
255 pub imr_multiaddr: in_addr,
256 pub imr_interface: in_addr,
257}
258
259#[repr(C)]
260#[derive(Debug, Copy, Clone, Default)]
261pub struct ipv6_mreq {
262 pub ipv6mr_multiaddr: in6_addr,
263 pub ipv6mr_interface: u32,
264}
265
266#[repr(C)]
267#[derive(Debug, Copy, Clone, Default)]
268pub struct linger {
269 pub l_onoff: i32,
270 pub l_linger: i32,
271}
272
273#[repr(C)]
274#[derive(Debug, Copy, Clone, Default)]
275pub struct pollfd {
276 pub fd: i32,
278 pub events: i16,
280 pub revents: i16,
282}
283
284#[repr(C)]
285#[derive(Debug, Default, Copy, Clone)]
286pub struct stat {
287 pub st_dev: u64,
288 pub st_ino: u64,
289 pub st_nlink: u64,
290 pub st_mode: u32,
292 pub st_uid: u32,
294 pub st_gid: u32,
296 pub st_rdev: u64,
298 pub st_size: i64,
300 pub st_blksize: i64,
302 pub st_blocks: i64,
304 pub st_atim: timespec,
306 pub st_mtim: timespec,
308 pub st_ctim: timespec,
310}
311
312#[repr(C)]
313#[derive(Debug, Clone, Copy)]
314pub struct dirent64 {
315 pub d_ino: u64,
317 pub d_off: i64,
319 pub d_reclen: u16,
321 pub d_type: u8,
323 pub d_name: [c_char; 256],
325}
326
327#[repr(C)]
328#[derive(Debug, Clone, Copy)]
329pub struct iovec {
331 pub iov_base: *mut c_void,
333 pub iov_len: usize,
335}
336
337pub const DT_UNKNOWN: u8 = 0;
338pub const DT_FIFO: u8 = 1;
339pub const DT_CHR: u8 = 2;
340pub const DT_DIR: u8 = 4;
341pub const DT_BLK: u8 = 6;
342pub const DT_REG: u8 = 8;
343pub const DT_LNK: u8 = 10;
344pub const DT_SOCK: u8 = 12;
345pub const DT_WHT: u8 = 14;
346
347pub const S_IFIFO: u32 = 0o1_0000;
348pub const S_IFCHR: u32 = 0o2_0000;
349pub const S_IFBLK: u32 = 0o6_0000;
350pub const S_IFDIR: u32 = 0o4_0000;
351pub const S_IFREG: u32 = 0o10_0000;
352pub const S_IFLNK: u32 = 0o12_0000;
353pub const S_IFSOCK: u32 = 0o14_0000;
354pub const S_IFMT: u32 = 0o17_0000;
355
356pub const PROT_NONE: u32 = 0;
358pub const PROT_READ: u32 = 1 << 0;
360pub const PROT_WRITE: u32 = 1 << 1;
362pub const PROT_EXEC: u32 = 1 << 2;
364
365pub const SEEK_SET: i32 = 0;
367pub const SEEK_CUR: i32 = 1;
369pub const SEEK_END: i32 = 2;
371
372extern "C" {
374 #[link_name = "sys_get_errno"]
376 pub fn get_errno() -> i32;
377
378 #[link_name = "sys_errno"]
380 pub fn errno() -> i32;
381
382 #[link_name = "sys_getpagesize"]
384 pub fn getpagesize() -> i32;
385
386 #[link_name = "sys_mmap"]
389 pub fn mmap(size: usize, prot_flags: u32, ret: &mut *mut u8) -> i32;
390
391 #[link_name = "sys_munmap"]
393 pub fn munmap(ptr: *mut u8, size: usize) -> i32;
394
395 #[link_name = "sys_mprotect"]
400 pub fn mprotect(ptr: *mut u8, size: usize, prot_flags: u32) -> i32;
401
402 #[link_name = "sys_futex_wait"]
412 pub fn futex_wait(
413 address: *mut u32,
414 expected: u32,
415 timeout: *const timespec,
416 flags: u32,
417 ) -> i32;
418
419 #[link_name = "sys_futex_wake"]
423 pub fn futex_wake(address: *mut u32, count: i32) -> i32;
424
425 #[link_name = "sys_sem_init"]
431 pub fn sem_init(sem: *mut sem_t, pshared: i32, value: u32) -> i32;
432
433 #[link_name = "sys_sem_destroy"]
436 pub fn sem_destroy(sem: *mut sem_t) -> i32;
437
438 #[link_name = "sys_sem_post"]
443 pub fn sem_post(sem: *mut sem_t) -> i32;
444
445 #[link_name = "sys_sem_trywait"]
451 pub fn sem_trywait(sem: *mut sem_t) -> i32;
452
453 #[link_name = "sys_sem_timedwait"]
463 pub fn sem_timedwait(sem: *mut sem_t, abs_timeout: *const timespec) -> i32;
464
465 #[link_name = "sys_getpid"]
467 pub fn getpid() -> pid_t;
468
469 #[link_name = "sys_exit"]
472 pub fn exit(status: i32) -> !;
473
474 #[link_name = "sys_abort"]
476 pub fn abort() -> !;
477
478 #[link_name = "sys_usleep"]
483 pub fn usleep(usecs: u64);
484
485 #[link_name = "sys_nanosleep"]
487 pub fn nanosleep(req: *const timespec) -> i32;
488
489 #[link_name = "sys_spawn"]
499 pub fn spawn(
500 id: *mut Tid,
501 func: extern "C" fn(usize),
502 arg: usize,
503 prio: u8,
504 core_id: isize,
505 ) -> i32;
506
507 #[link_name = "sys_spawn2"]
519 pub fn spawn2(
520 func: extern "C" fn(usize),
521 arg: usize,
522 prio: u8,
523 stack_size: usize,
524 core_id: isize,
525 ) -> Tid;
526
527 #[link_name = "sys_join"]
532 pub fn join(id: Tid) -> i32;
533
534 #[link_name = "sys_yield"]
539 pub fn yield_now();
540
541 #[link_name = "sys_clock_gettime"]
553 pub fn clock_gettime(clockid: clockid_t, tp: *mut timespec) -> i32;
554
555 #[link_name = "sys_open"]
561 pub fn open(name: *const c_char, flags: i32, mode: i32) -> i32;
562
563 #[deprecated(since = "0.4.0", note = "please use `open`")]
567 #[link_name = "sys_opendir"]
568 pub fn opendir(name: *const c_char) -> i32;
569
570 #[link_name = "sys_unlink"]
572 pub fn unlink(name: *const c_char) -> i32;
573
574 #[link_name = "sys_rmdir"]
576 pub fn rmdir(name: *const c_char) -> i32;
577
578 #[link_name = "sys_stat"]
580 pub fn stat(name: *const c_char, stat: *mut stat) -> i32;
581
582 #[link_name = "sys_lstat"]
584 pub fn lstat(name: *const c_char, stat: *mut stat) -> i32;
585
586 #[link_name = "sys_fstat"]
588 pub fn fstat(fd: i32, stat: *mut stat) -> i32;
589
590 #[link_name = "sys_available_parallelism"]
595 pub fn available_parallelism() -> usize;
596
597 #[deprecated(since = "0.4.0", note = "please use `available_parallelism`")]
599 #[link_name = "sys_get_processor_count"]
600 pub fn get_processor_count() -> usize;
601
602 #[link_name = "sys_malloc"]
603 pub fn malloc(size: usize, align: usize) -> *mut u8;
604
605 #[link_name = "sys_alloc"]
606 pub fn alloc(size: usize, align: usize) -> *mut u8;
607
608 #[link_name = "sys_alloc_zeroed"]
609 pub fn alloc_zeroed(size: usize, align: usize) -> *mut u8;
610
611 #[link_name = "sys_realloc"]
612 pub fn realloc(ptr: *mut u8, size: usize, align: usize, new_size: usize) -> *mut u8;
613
614 #[link_name = "sys_free"]
615 pub fn free(ptr: *mut u8, size: usize, align: usize);
616
617 #[link_name = "sys_dealloc"]
618 pub fn dealloc(ptr: *mut u8, size: usize, align: usize);
619
620 #[link_name = "sys_notify"]
621 pub fn notify(id: usize, count: i32) -> i32;
622
623 #[doc(hidden)]
624 #[link_name = "sys_add_queue"]
625 pub fn add_queue(id: usize, timeout_ns: i64) -> i32;
626
627 #[doc(hidden)]
628 #[link_name = "sys_wait"]
629 pub fn wait(id: usize) -> i32;
630
631 #[doc(hidden)]
632 #[link_name = "sys_init_queue"]
633 pub fn init_queue(id: usize) -> i32;
634
635 #[doc(hidden)]
636 #[link_name = "sys_destroy_queue"]
637 pub fn destroy_queue(id: usize) -> i32;
638
639 #[link_name = "sys_network_init"]
641 pub fn network_init() -> i32;
642
643 #[link_name = "sys_block_current_task"]
646 pub fn block_current_task();
647
648 #[link_name = "sys_block_current_task_with_timeout"]
653 pub fn block_current_task_with_timeout(timeout: u64);
654
655 #[link_name = "sys_wakeup_taskt"]
657 pub fn wakeup_task(tid: Tid);
658
659 #[link_name = "sys_getaddrbyname"]
682 pub fn getaddrbyname(name: *const c_char, inaddr: *mut u8, len: usize) -> i32;
683
684 #[link_name = "sys_accept"]
685 pub fn accept(s: i32, addr: *mut sockaddr, addrlen: *mut socklen_t) -> i32;
686
687 #[link_name = "sys_bind"]
689 pub fn bind(s: i32, name: *const sockaddr, namelen: socklen_t) -> i32;
690
691 #[link_name = "sys_connect"]
692 pub fn connect(s: i32, name: *const sockaddr, namelen: socklen_t) -> i32;
693
694 #[link_name = "sys_read"]
700 pub fn read(fd: i32, buf: *mut u8, len: usize) -> isize;
701
702 #[link_name = "sys_readv"]
718 pub fn readv(fd: i32, iov: *const iovec, iovcnt: usize) -> isize;
719
720 #[link_name = "sys_getdents64"]
723 pub fn getdents64(fd: i32, dirp: *mut dirent64, count: usize) -> i64;
724
725 #[link_name = "sys_mkdir"]
728 pub fn mkdir(name: *const i8, mode: u32) -> i32;
729
730 #[link_name = "sys_read_entropy"]
736 pub fn read_entropy(buf: *mut u8, len: usize, flags: u32) -> isize;
737
738 #[link_name = "sys_recv"]
740 pub fn recv(socket: i32, buf: *mut u8, len: usize, flags: i32) -> isize;
741
742 #[link_name = "sys_recvfrom"]
744 pub fn recvfrom(
745 socket: i32,
746 buf: *mut u8,
747 len: usize,
748 flags: i32,
749 addr: *mut sockaddr,
750 addrlen: *mut socklen_t,
751 ) -> isize;
752
753 #[link_name = "sys_lseek"]
759 pub fn lseek(fd: i32, offset: isize, whence: i32) -> isize;
760
761 #[link_name = "sys_write"]
767 pub fn write(fd: i32, buf: *const u8, len: usize) -> isize;
768
769 #[link_name = "sys_writev"]
785 pub fn writev(fd: i32, iov: *const iovec, iovcnt: usize) -> isize;
786
787 #[link_name = "sys_close"]
792 pub fn close(fd: i32) -> i32;
793
794 #[link_name = "sys_dup"]
796 pub fn dup(fd: i32) -> i32;
797
798 #[link_name = "sys_getpeername"]
799 pub fn getpeername(s: i32, name: *mut sockaddr, namelen: *mut socklen_t) -> i32;
800
801 #[link_name = "sys_getsockname"]
802 pub fn getsockname(s: i32, name: *mut sockaddr, namelen: *mut socklen_t) -> i32;
803
804 #[link_name = "sys_getsockopt"]
805 pub fn getsockopt(
806 s: i32,
807 level: i32,
808 optname: i32,
809 optval: *mut c_void,
810 optlen: *mut socklen_t,
811 ) -> i32;
812
813 #[link_name = "sys_setsockopt"]
814 pub fn setsockopt(
815 s: i32,
816 level: i32,
817 optname: i32,
818 optval: *const c_void,
819 optlen: socklen_t,
820 ) -> i32;
821
822 #[link_name = "sys_ioctl"]
823 pub fn ioctl(s: i32, cmd: i32, argp: *mut c_void) -> i32;
824
825 #[link_name = "sys_fcntl"]
826 pub fn fcntl(fd: i32, cmd: i32, arg: i32) -> i32;
827
828 #[link_name = "sys_eventfd"]
845 pub fn eventfd(initval: u64, flags: i16) -> i32;
846
847 #[link_name = "sys_poll"]
852 pub fn poll(fds: *mut pollfd, nfds: nfds_t, timeout: i32) -> i32;
853
854 #[link_name = "sys_listen"]
859 pub fn listen(s: i32, backlog: i32) -> i32;
860
861 #[link_name = "sys_send"]
862 pub fn send(s: i32, mem: *const c_void, len: usize, flags: i32) -> isize;
863
864 #[link_name = "sys_sendto"]
865 pub fn sendto(
866 s: i32,
867 mem: *const c_void,
868 len: usize,
869 flags: i32,
870 to: *const sockaddr,
871 tolen: socklen_t,
872 ) -> isize;
873
874 #[link_name = "sys_shutdown"]
876 pub fn shutdown(sockfd: i32, how: i32) -> i32;
877
878 #[deprecated(since = "0.4.0", note = "use `shutdown` instead")]
879 #[link_name = "sys_shutdown_socket"]
880 pub fn shutdown_socket(s: i32, how: i32) -> i32;
881
882 #[link_name = "sys_socket"]
883 pub fn socket(domain: i32, type_: i32, protocol: i32) -> i32;
884
885 #[link_name = "sys_freeaddrinfo"]
886 pub fn freeaddrinfo(ai: *mut addrinfo);
887
888 #[link_name = "sys_getaddrinfo"]
889 pub fn getaddrinfo(
890 nodename: *const c_char,
891 servname: *const c_char,
892 hints: *const addrinfo,
893 res: *mut *mut addrinfo,
894 ) -> i32;
895
896 fn sys_get_priority() -> u8;
897 fn sys_set_priority(tid: Tid, prio: u8);
898}
899
900#[inline(always)]
902pub unsafe fn get_priority() -> Priority {
903 Priority::from(sys_get_priority())
904}
905
906#[inline(always)]
908pub unsafe fn set_priority(tid: Tid, prio: Priority) {
909 sys_set_priority(tid, prio.into());
910}