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_INET: i32 = 0;
91pub const AF_INET6: i32 = 1;
93pub const AF_VSOCK: i32 = 2;
95pub const IPPROTO_IP: i32 = 0;
96pub const IPPROTO_IPV6: i32 = 41;
97pub const IPPROTO_UDP: i32 = 17;
98pub const IPPROTO_TCP: i32 = 6;
99pub const IPV6_ADD_MEMBERSHIP: i32 = 12;
100pub const IPV6_DROP_MEMBERSHIP: i32 = 13;
101pub const IPV6_MULTICAST_LOOP: i32 = 19;
102pub const IPV6_V6ONLY: i32 = 27;
103pub const IP_TOS: i32 = 1;
104pub const IP_TTL: i32 = 2;
105pub const IP_MULTICAST_TTL: i32 = 5;
106pub const IP_MULTICAST_LOOP: i32 = 7;
107pub const IP_ADD_MEMBERSHIP: i32 = 3;
108pub const IP_DROP_MEMBERSHIP: i32 = 4;
109pub const SHUT_RD: i32 = 0;
110pub const SHUT_WR: i32 = 1;
111pub const SHUT_RDWR: i32 = 2;
112pub const SOCK_DGRAM: i32 = 2;
114pub const SOCK_STREAM: i32 = 1;
116pub const SOCK_NONBLOCK: i32 = 0o4000;
118pub const SOCK_CLOEXEC: i32 = 0o40000;
120pub const SOL_SOCKET: i32 = 4095;
121pub const SO_REUSEADDR: i32 = 0x0004;
122pub const SO_KEEPALIVE: i32 = 0x0008;
123pub const SO_BROADCAST: i32 = 0x0020;
124pub const SO_LINGER: i32 = 0x0080;
125pub const SO_SNDBUF: i32 = 0x1001;
126pub const SO_RCVBUF: i32 = 0x1002;
127pub const SO_SNDTIMEO: i32 = 0x1005;
128pub const SO_RCVTIMEO: i32 = 0x1006;
129pub const SO_ERROR: i32 = 0x1007;
130pub const TCP_NODELAY: i32 = 1;
131pub const MSG_PEEK: i32 = 1;
132pub const FIONBIO: i32 = 0x8008667eu32 as i32;
133pub const EAI_AGAIN: i32 = 2;
134pub const EAI_BADFLAGS: i32 = 3;
135pub const EAI_FAIL: i32 = 4;
136pub const EAI_FAMILY: i32 = 5;
137pub const EAI_MEMORY: i32 = 6;
138pub const EAI_NODATA: i32 = 7;
139pub const EAI_NONAME: i32 = 8;
140pub const EAI_SERVICE: i32 = 9;
141pub const EAI_SOCKTYPE: i32 = 10;
142pub const EAI_SYSTEM: i32 = 11;
143pub const EAI_OVERFLOW: i32 = 14;
144pub const POLLIN: i16 = 0x1;
145pub const POLLPRI: i16 = 0x2;
146pub const POLLOUT: i16 = 0x4;
147pub const POLLERR: i16 = 0x8;
148pub const POLLHUP: i16 = 0x10;
149pub const POLLNVAL: i16 = 0x20;
150pub const POLLRDNORM: i16 = 0x040;
151pub const POLLRDBAND: i16 = 0x080;
152pub const POLLWRNORM: i16 = 0x0100;
153pub const POLLWRBAND: i16 = 0x0200;
154pub const POLLRDHUP: i16 = 0x2000;
155pub const EFD_SEMAPHORE: i16 = 0o1;
156pub const EFD_NONBLOCK: i16 = 0o4000;
157pub const EFD_CLOEXEC: i16 = 0o40000;
158pub const IOV_MAX: usize = 1024;
159pub const VMADDR_CID_ANY: u32 = u32::MAX;
161pub const VMADDR_CID_HYPERVISOR: u32 = 0;
162pub const VMADDR_CID_LOCAL: u32 = 1;
163pub const VMADDR_CID_HOST: u32 = 2;
164pub type sa_family_t = u8;
165pub type socklen_t = u32;
166pub type in_addr_t = u32;
167pub type in_port_t = u16;
168pub type time_t = i64;
169pub type useconds_t = u32;
170pub type suseconds_t = i32;
171pub type nfds_t = usize;
172pub type sem_t = *const c_void;
173pub type pid_t = i32;
174pub type clockid_t = i32;
175
176#[repr(C)]
177#[derive(Debug, Copy, Clone, Default)]
178pub struct in_addr {
179 pub s_addr: in_addr_t,
180}
181
182#[repr(C, align(4))]
183#[derive(Debug, Copy, Clone, Default)]
184pub struct in6_addr {
185 pub s6_addr: [u8; 16],
186}
187
188#[repr(C)]
189#[derive(Debug, Copy, Clone, Default)]
190pub struct sockaddr {
191 pub sa_len: u8,
192 pub sa_family: sa_family_t,
193 pub sa_data: [c_char; 14],
194}
195
196#[repr(C)]
197#[derive(Debug, Copy, Clone, Default)]
198pub struct sockaddr_vm {
199 pub svm_len: u8,
200 pub svm_family: sa_family_t,
201 pub svm_reserved1: u16,
202 pub svm_port: u32,
203 pub svm_cid: u32,
204 pub svm_zero: [u8; 4],
205}
206
207#[repr(C)]
208#[derive(Debug, Copy, Clone, Default)]
209pub struct sockaddr_in {
210 pub sin_len: u8,
211 pub sin_family: sa_family_t,
212 pub sin_port: in_port_t,
213 pub sin_addr: in_addr,
214 pub sin_zero: [c_char; 8],
215}
216
217#[repr(C)]
218#[derive(Debug, Copy, Clone, Default)]
219pub struct sockaddr_in6 {
220 pub sin6_len: u8,
221 pub sin6_family: sa_family_t,
222 pub sin6_port: in_port_t,
223 pub sin6_flowinfo: u32,
224 pub sin6_addr: in6_addr,
225 pub sin6_scope_id: u32,
226}
227
228#[repr(C)]
229#[derive(Debug, Copy, Clone)]
230pub struct addrinfo {
231 pub ai_flags: i32,
232 pub ai_family: i32,
233 pub ai_socktype: i32,
234 pub ai_protocol: i32,
235 pub ai_addrlen: socklen_t,
236 pub ai_canonname: *mut c_char,
237 pub ai_addr: *mut sockaddr,
238 pub ai_next: *mut addrinfo,
239}
240
241#[repr(C)]
242#[derive(Debug, Copy, Clone)]
243pub struct sockaddr_storage {
244 pub s2_len: u8,
245 pub ss_family: sa_family_t,
246 __ss_pad1: [u8; 6],
247 __ss_align: i64,
248 __ss_pad2: [u8; 112],
249}
250
251#[repr(C)]
252#[derive(Debug, Copy, Clone, Default)]
253pub struct ip_mreq {
254 pub imr_multiaddr: in_addr,
255 pub imr_interface: in_addr,
256}
257
258#[repr(C)]
259#[derive(Debug, Copy, Clone, Default)]
260pub struct ipv6_mreq {
261 pub ipv6mr_multiaddr: in6_addr,
262 pub ipv6mr_interface: u32,
263}
264
265#[repr(C)]
266#[derive(Debug, Copy, Clone, Default)]
267pub struct linger {
268 pub l_onoff: i32,
269 pub l_linger: i32,
270}
271
272#[repr(C)]
273#[derive(Debug, Copy, Clone, Default)]
274pub struct pollfd {
275 pub fd: i32,
277 pub events: i16,
279 pub revents: i16,
281}
282
283#[repr(C)]
284#[derive(Debug, Default, Copy, Clone)]
285pub struct stat {
286 pub st_dev: u64,
287 pub st_ino: u64,
288 pub st_nlink: u64,
289 pub st_mode: u32,
291 pub st_uid: u32,
293 pub st_gid: u32,
295 pub st_rdev: u64,
297 pub st_size: i64,
299 pub st_blksize: i64,
301 pub st_blocks: i64,
303 pub st_atim: timespec,
305 pub st_mtim: timespec,
307 pub st_ctim: timespec,
309}
310
311#[repr(C)]
312#[derive(Debug, Clone, Copy)]
313pub struct dirent64 {
314 pub d_ino: u64,
316 pub d_off: i64,
318 pub d_reclen: u16,
320 pub d_type: u8,
322 pub d_name: [c_char; 256],
324}
325
326#[repr(C)]
327#[derive(Debug, Clone, Copy)]
328pub struct iovec {
330 pub iov_base: *mut c_void,
332 pub iov_len: usize,
334}
335
336pub const DT_UNKNOWN: u8 = 0;
337pub const DT_FIFO: u8 = 1;
338pub const DT_CHR: u8 = 2;
339pub const DT_DIR: u8 = 4;
340pub const DT_BLK: u8 = 6;
341pub const DT_REG: u8 = 8;
342pub const DT_LNK: u8 = 10;
343pub const DT_SOCK: u8 = 12;
344pub const DT_WHT: u8 = 14;
345
346pub const S_IFIFO: u32 = 0o1_0000;
347pub const S_IFCHR: u32 = 0o2_0000;
348pub const S_IFBLK: u32 = 0o6_0000;
349pub const S_IFDIR: u32 = 0o4_0000;
350pub const S_IFREG: u32 = 0o10_0000;
351pub const S_IFLNK: u32 = 0o12_0000;
352pub const S_IFSOCK: u32 = 0o14_0000;
353pub const S_IFMT: u32 = 0o17_0000;
354
355pub const PROT_NONE: u32 = 0;
357pub const PROT_READ: u32 = 1 << 0;
359pub const PROT_WRITE: u32 = 1 << 1;
361pub const PROT_EXEC: u32 = 1 << 2;
363
364pub const SEEK_SET: i32 = 0;
366pub const SEEK_CUR: i32 = 1;
368pub const SEEK_END: i32 = 2;
370
371extern "C" {
373 #[link_name = "sys_get_errno"]
375 pub fn get_errno() -> i32;
376
377 #[link_name = "sys_errno"]
379 pub fn errno() -> i32;
380
381 #[link_name = "sys_getpagesize"]
383 pub fn getpagesize() -> i32;
384
385 #[link_name = "sys_mmap"]
388 pub fn mmap(size: usize, prot_flags: u32, ret: &mut *mut u8) -> i32;
389
390 #[link_name = "sys_munmap"]
392 pub fn munmap(ptr: *mut u8, size: usize) -> i32;
393
394 #[link_name = "sys_mprotect"]
399 pub fn mprotect(ptr: *mut u8, size: usize, prot_flags: u32) -> i32;
400
401 #[link_name = "sys_futex_wait"]
411 pub fn futex_wait(
412 address: *mut u32,
413 expected: u32,
414 timeout: *const timespec,
415 flags: u32,
416 ) -> i32;
417
418 #[link_name = "sys_futex_wake"]
422 pub fn futex_wake(address: *mut u32, count: i32) -> i32;
423
424 #[link_name = "sys_sem_init"]
430 pub fn sem_init(sem: *mut sem_t, pshared: i32, value: u32) -> i32;
431
432 #[link_name = "sys_sem_destroy"]
435 pub fn sem_destroy(sem: *mut sem_t) -> i32;
436
437 #[link_name = "sys_sem_post"]
442 pub fn sem_post(sem: *mut sem_t) -> i32;
443
444 #[link_name = "sys_sem_trywait"]
450 pub fn sem_trywait(sem: *mut sem_t) -> i32;
451
452 #[link_name = "sys_sem_timedwait"]
462 pub fn sem_timedwait(sem: *mut sem_t, abs_timeout: *const timespec) -> i32;
463
464 #[link_name = "sys_getpid"]
466 pub fn getpid() -> pid_t;
467
468 #[link_name = "sys_exit"]
471 pub fn exit(status: i32) -> !;
472
473 #[link_name = "sys_abort"]
475 pub fn abort() -> !;
476
477 #[link_name = "sys_usleep"]
482 pub fn usleep(usecs: u64);
483
484 #[link_name = "sys_nanosleep"]
486 pub fn nanosleep(req: *const timespec) -> i32;
487
488 #[link_name = "sys_spawn"]
498 pub fn spawn(
499 id: *mut Tid,
500 func: extern "C" fn(usize),
501 arg: usize,
502 prio: u8,
503 core_id: isize,
504 ) -> i32;
505
506 #[link_name = "sys_spawn2"]
518 pub fn spawn2(
519 func: extern "C" fn(usize),
520 arg: usize,
521 prio: u8,
522 stack_size: usize,
523 core_id: isize,
524 ) -> Tid;
525
526 #[link_name = "sys_join"]
531 pub fn join(id: Tid) -> i32;
532
533 #[link_name = "sys_yield"]
538 pub fn yield_now();
539
540 #[link_name = "sys_clock_gettime"]
552 pub fn clock_gettime(clockid: clockid_t, tp: *mut timespec) -> i32;
553
554 #[link_name = "sys_open"]
560 pub fn open(name: *const c_char, flags: i32, mode: i32) -> i32;
561
562 #[deprecated(since = "0.4.0", note = "please use `open`")]
566 #[link_name = "sys_opendir"]
567 pub fn opendir(name: *const c_char) -> i32;
568
569 #[link_name = "sys_unlink"]
571 pub fn unlink(name: *const c_char) -> i32;
572
573 #[link_name = "sys_rmdir"]
575 pub fn rmdir(name: *const c_char) -> i32;
576
577 #[link_name = "sys_stat"]
579 pub fn stat(name: *const c_char, stat: *mut stat) -> i32;
580
581 #[link_name = "sys_lstat"]
583 pub fn lstat(name: *const c_char, stat: *mut stat) -> i32;
584
585 #[link_name = "sys_fstat"]
587 pub fn fstat(fd: i32, stat: *mut stat) -> i32;
588
589 #[link_name = "sys_available_parallelism"]
594 pub fn available_parallelism() -> usize;
595
596 #[deprecated(since = "0.4.0", note = "please use `available_parallelism`")]
598 #[link_name = "sys_get_processor_count"]
599 pub fn get_processor_count() -> usize;
600
601 #[link_name = "sys_malloc"]
602 pub fn malloc(size: usize, align: usize) -> *mut u8;
603
604 #[link_name = "sys_alloc"]
605 pub fn alloc(size: usize, align: usize) -> *mut u8;
606
607 #[link_name = "sys_alloc_zeroed"]
608 pub fn alloc_zeroed(size: usize, align: usize) -> *mut u8;
609
610 #[link_name = "sys_realloc"]
611 pub fn realloc(ptr: *mut u8, size: usize, align: usize, new_size: usize) -> *mut u8;
612
613 #[link_name = "sys_free"]
614 pub fn free(ptr: *mut u8, size: usize, align: usize);
615
616 #[link_name = "sys_dealloc"]
617 pub fn dealloc(ptr: *mut u8, size: usize, align: usize);
618
619 #[link_name = "sys_notify"]
620 pub fn notify(id: usize, count: i32) -> i32;
621
622 #[doc(hidden)]
623 #[link_name = "sys_add_queue"]
624 pub fn add_queue(id: usize, timeout_ns: i64) -> i32;
625
626 #[doc(hidden)]
627 #[link_name = "sys_wait"]
628 pub fn wait(id: usize) -> i32;
629
630 #[doc(hidden)]
631 #[link_name = "sys_init_queue"]
632 pub fn init_queue(id: usize) -> i32;
633
634 #[doc(hidden)]
635 #[link_name = "sys_destroy_queue"]
636 pub fn destroy_queue(id: usize) -> i32;
637
638 #[link_name = "sys_network_init"]
640 pub fn network_init() -> i32;
641
642 #[link_name = "sys_block_current_task"]
645 pub fn block_current_task();
646
647 #[link_name = "sys_block_current_task_with_timeout"]
652 pub fn block_current_task_with_timeout(timeout: u64);
653
654 #[link_name = "sys_wakeup_taskt"]
656 pub fn wakeup_task(tid: Tid);
657
658 #[link_name = "sys_getaddrbyname"]
681 pub fn getaddrbyname(name: *const c_char, inaddr: *mut u8, len: usize) -> i32;
682
683 #[link_name = "sys_accept"]
684 pub fn accept(s: i32, addr: *mut sockaddr, addrlen: *mut socklen_t) -> i32;
685
686 #[link_name = "sys_bind"]
688 pub fn bind(s: i32, name: *const sockaddr, namelen: socklen_t) -> i32;
689
690 #[link_name = "sys_connect"]
691 pub fn connect(s: i32, name: *const sockaddr, namelen: socklen_t) -> i32;
692
693 #[link_name = "sys_read"]
699 pub fn read(fd: i32, buf: *mut u8, len: usize) -> isize;
700
701 #[link_name = "sys_readv"]
717 pub fn readv(fd: i32, iov: *const iovec, iovcnt: usize) -> isize;
718
719 #[link_name = "sys_getdents64"]
722 pub fn getdents64(fd: i32, dirp: *mut dirent64, count: usize) -> i64;
723
724 #[link_name = "sys_mkdir"]
727 pub fn mkdir(name: *const i8, mode: u32) -> i32;
728
729 #[link_name = "sys_read_entropy"]
735 pub fn read_entropy(buf: *mut u8, len: usize, flags: u32) -> isize;
736
737 #[link_name = "sys_recv"]
739 pub fn recv(socket: i32, buf: *mut u8, len: usize, flags: i32) -> isize;
740
741 #[link_name = "sys_recvfrom"]
743 pub fn recvfrom(
744 socket: i32,
745 buf: *mut u8,
746 len: usize,
747 flags: i32,
748 addr: *mut sockaddr,
749 addrlen: *mut socklen_t,
750 ) -> isize;
751
752 #[link_name = "sys_lseek"]
758 pub fn lseek(fd: i32, offset: isize, whence: i32) -> isize;
759
760 #[link_name = "sys_write"]
766 pub fn write(fd: i32, buf: *const u8, len: usize) -> isize;
767
768 #[link_name = "sys_writev"]
784 pub fn writev(fd: i32, iov: *const iovec, iovcnt: usize) -> isize;
785
786 #[link_name = "sys_close"]
791 pub fn close(fd: i32) -> i32;
792
793 #[link_name = "sys_dup"]
795 pub fn dup(fd: i32) -> i32;
796
797 #[link_name = "sys_getpeername"]
798 pub fn getpeername(s: i32, name: *mut sockaddr, namelen: *mut socklen_t) -> i32;
799
800 #[link_name = "sys_getsockname"]
801 pub fn getsockname(s: i32, name: *mut sockaddr, namelen: *mut socklen_t) -> i32;
802
803 #[link_name = "sys_getsockopt"]
804 pub fn getsockopt(
805 s: i32,
806 level: i32,
807 optname: i32,
808 optval: *mut c_void,
809 optlen: *mut socklen_t,
810 ) -> i32;
811
812 #[link_name = "sys_setsockopt"]
813 pub fn setsockopt(
814 s: i32,
815 level: i32,
816 optname: i32,
817 optval: *const c_void,
818 optlen: socklen_t,
819 ) -> i32;
820
821 #[link_name = "sys_ioctl"]
822 pub fn ioctl(s: i32, cmd: i32, argp: *mut c_void) -> i32;
823
824 #[link_name = "sys_fcntl"]
825 pub fn fcntl(fd: i32, cmd: i32, arg: i32) -> i32;
826
827 #[link_name = "sys_eventfd"]
844 pub fn eventfd(initval: u64, flags: i16) -> i32;
845
846 #[link_name = "sys_poll"]
851 pub fn poll(fds: *mut pollfd, nfds: nfds_t, timeout: i32) -> i32;
852
853 #[link_name = "sys_listen"]
858 pub fn listen(s: i32, backlog: i32) -> i32;
859
860 #[link_name = "sys_send"]
861 pub fn send(s: i32, mem: *const c_void, len: usize, flags: i32) -> isize;
862
863 #[link_name = "sys_sendto"]
864 pub fn sendto(
865 s: i32,
866 mem: *const c_void,
867 len: usize,
868 flags: i32,
869 to: *const sockaddr,
870 tolen: socklen_t,
871 ) -> isize;
872
873 #[link_name = "sys_shutdown"]
875 pub fn shutdown(sockfd: i32, how: i32) -> i32;
876
877 #[deprecated(since = "0.4.0", note = "use `shutdown` instead")]
878 #[link_name = "sys_shutdown_socket"]
879 pub fn shutdown_socket(s: i32, how: i32) -> i32;
880
881 #[link_name = "sys_socket"]
882 pub fn socket(domain: i32, type_: i32, protocol: i32) -> i32;
883
884 #[link_name = "sys_freeaddrinfo"]
885 pub fn freeaddrinfo(ai: *mut addrinfo);
886
887 #[link_name = "sys_getaddrinfo"]
888 pub fn getaddrinfo(
889 nodename: *const c_char,
890 servname: *const c_char,
891 hints: *const addrinfo,
892 res: *mut *mut addrinfo,
893 ) -> i32;
894
895 fn sys_get_priority() -> u8;
896 fn sys_set_priority(tid: Tid, prio: u8);
897}
898
899#[inline(always)]
901pub unsafe fn get_priority() -> Priority {
902 Priority::from(sys_get_priority())
903}
904
905#[inline(always)]
907pub unsafe fn set_priority(tid: Tid, prio: Priority) {
908 sys_set_priority(tid, prio.into());
909}