hermit_abi/
lib.rs

1//! `hermit-abi` is small interface to call functions from the
2//! [Hermit unikernel](https://github.com/hermit-os/kernel).
3
4#![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
17/// A thread handle type
18pub type Tid = u32;
19
20/// Maximum number of priorities
21pub const NO_PRIORITIES: usize = 31;
22
23/// Priority of a thread
24#[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
63/// returns true if file descriptor `fd` is a tty
64pub fn isatty(_fd: c_int) -> bool {
65	false
66}
67
68/// `timespec` is used by `clock_gettime` to retrieve the
69/// current time
70#[derive(Default, Copy, Clone, Debug)]
71#[repr(C)]
72pub struct timespec {
73	/// seconds
74	pub tv_sec: time_t,
75	/// nanoseconds
76	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
86/// The largest number `rand` will return
87pub const RAND_MAX: i32 = 2_147_483_647;
88
89pub const AF_UNSPEC: i32 = 0;
90/// Socket address family: IPv4
91pub const AF_INET: i32 = 3;
92/// Socket address family: IPv6
93pub const AF_INET6: i32 = 1;
94/// Socket address family: VSOCK protocol for hypervisor-guest communication
95pub 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;
113/// Socket supports datagrams (connectionless,  unreliable  messages of a fixed maximum length)
114pub const SOCK_DGRAM: i32 = 2;
115/// Socket provides sequenced, reliable,  two-way,  connection-based byte streams.
116pub const SOCK_STREAM: i32 = 1;
117/// Set the O_NONBLOCK file status flag on the open socket
118pub const SOCK_NONBLOCK: i32 = 0o4000;
119/// Set  the  close-on-exec flag on the new socket
120pub 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;
160/// VMADDR_CID_ANY means that any address is possible for binding
161pub 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	/// file descriptor
277	pub fd: i32,
278	/// events to look for
279	pub events: i16,
280	/// events returned
281	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	/// access permissions
291	pub st_mode: u32,
292	/// user id
293	pub st_uid: u32,
294	/// group id
295	pub st_gid: u32,
296	/// device id
297	pub st_rdev: u64,
298	/// size in bytes
299	pub st_size: i64,
300	/// block size
301	pub st_blksize: i64,
302	/// size in blocks
303	pub st_blocks: i64,
304	/// time of last access
305	pub st_atim: timespec,
306	/// time of last modification
307	pub st_mtim: timespec,
308	/// time of last status change
309	pub st_ctim: timespec,
310}
311
312#[repr(C)]
313#[derive(Debug, Clone, Copy)]
314pub struct dirent64 {
315	/// 64-bit inode number
316	pub d_ino: u64,
317	/// 64-bit offset to next structure
318	pub d_off: i64,
319	/// Size of this dirent
320	pub d_reclen: u16,
321	/// File type
322	pub d_type: u8,
323	/// Filename (null-terminated)
324	pub d_name: [c_char; 256],
325}
326
327#[repr(C)]
328#[derive(Debug, Clone, Copy)]
329/// Describes  a  region  of  memory, beginning at `iov_base` address and with the size of `iov_len` bytes.
330pub struct iovec {
331	/// Starting address
332	pub iov_base: *mut c_void,
333	/// Size of the memory pointed to by iov_base.
334	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
356/// Pages may not be accessed.
357pub const PROT_NONE: u32 = 0;
358/// Indicates that the memory region should be readable.
359pub const PROT_READ: u32 = 1 << 0;
360/// Indicates that the memory region should be writable.
361pub const PROT_WRITE: u32 = 1 << 1;
362/// Indicates that the memory region should be executable.
363pub const PROT_EXEC: u32 = 1 << 2;
364
365/// The file offset is set to offset bytes.
366pub const SEEK_SET: i32 = 0;
367/// The file offset is set to its current location plus offset bytes.
368pub const SEEK_CUR: i32 = 1;
369/// The file offset is set to the size of the file plus offset bytes.
370pub const SEEK_END: i32 = 2;
371
372// symbols, which are part of the library operating system
373extern "C" {
374	/// Get the last error number from the thread local storage
375	#[link_name = "sys_get_errno"]
376	pub fn get_errno() -> i32;
377
378	/// Get the last error number from the thread local storage
379	#[link_name = "sys_errno"]
380	pub fn errno() -> i32;
381
382	/// Get memory page size
383	#[link_name = "sys_getpagesize"]
384	pub fn getpagesize() -> i32;
385
386	/// Creates a new virtual memory mapping of the `size` specified with
387	/// protection bits specified in `prot_flags`.
388	#[link_name = "sys_mmap"]
389	pub fn mmap(size: usize, prot_flags: u32, ret: &mut *mut u8) -> i32;
390
391	/// Unmaps memory at the specified `ptr` for `size` bytes.
392	#[link_name = "sys_munmap"]
393	pub fn munmap(ptr: *mut u8, size: usize) -> i32;
394
395	/// Configures the protections associated with a region of virtual memory
396	/// starting at `ptr` and going to `size`.
397	///
398	/// Returns 0 on success and an error code on failure.
399	#[link_name = "sys_mprotect"]
400	pub fn mprotect(ptr: *mut u8, size: usize, prot_flags: u32) -> i32;
401
402	/// If the value at address matches the expected value, park the current thread until it is either
403	/// woken up with [`futex_wake`] (returns 0) or an optional timeout elapses (returns -ETIMEDOUT).
404	///
405	/// Setting `timeout` to null means the function will only return if [`futex_wake`] is called.
406	/// Otherwise, `timeout` is interpreted as an absolute time measured with [`CLOCK_MONOTONIC`].
407	/// If [`FUTEX_RELATIVE_TIMEOUT`] is set in `flags` the timeout is understood to be relative
408	/// to the current time.
409	///
410	/// Returns -EINVAL if `address` is null, the timeout is negative or `flags` contains unknown values.
411	#[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	/// Wake `count` threads waiting on the futex at `address`. Returns the number of threads
420	/// woken up (saturates to `i32::MAX`). If `count` is `i32::MAX`, wake up all matching
421	/// waiting threads. If `count` is negative or `address` is null, returns -EINVAL.
422	#[link_name = "sys_futex_wake"]
423	pub fn futex_wake(address: *mut u32, count: i32) -> i32;
424
425	/// sem_init() initializes the unnamed semaphore at the address
426	/// pointed to by `sem`.  The `value` argument specifies the
427	/// initial value for the semaphore. If `pshared` is nonzero,
428	/// then the semaphore is shared between processes (currently
429	/// not supported).
430	#[link_name = "sys_sem_init"]
431	pub fn sem_init(sem: *mut sem_t, pshared: i32, value: u32) -> i32;
432
433	/// sem_destroy() frees the unnamed semaphore at the address
434	/// pointed to by `sem`.
435	#[link_name = "sys_sem_destroy"]
436	pub fn sem_destroy(sem: *mut sem_t) -> i32;
437
438	/// sem_post() increments the semaphore pointed to by `sem`.
439	/// If the semaphore's value consequently becomes greater
440	/// than zero, then another thread blocked in a sem_wait call
441	/// will be woken up and proceed to lock the semaphore.
442	#[link_name = "sys_sem_post"]
443	pub fn sem_post(sem: *mut sem_t) -> i32;
444
445	/// try to decrement a semaphore
446	///
447	/// sem_trywait() is the same as sem_timedwait(), except that
448	/// if the  decrement cannot be immediately performed, then  call
449	/// returns a negative value instead of blocking.
450	#[link_name = "sys_sem_trywait"]
451	pub fn sem_trywait(sem: *mut sem_t) -> i32;
452
453	/// decrement a semaphore
454	///
455	/// sem_timedwait() decrements the semaphore pointed to by `sem`.
456	/// If the semaphore's value is greater than zero, then the
457	/// the function returns immediately. If the semaphore currently
458	/// has the value zero, then the call blocks until either
459	/// it becomes possible to perform the decrement of the time limit
460	/// to wait for the semaphore is expired. A time limit `ms` of
461	/// means infinity waiting time.
462	#[link_name = "sys_sem_timedwait"]
463	pub fn sem_timedwait(sem: *mut sem_t, abs_timeout: *const timespec) -> i32;
464
465	/// Determines the id of the current thread
466	#[link_name = "sys_getpid"]
467	pub fn getpid() -> pid_t;
468
469	/// cause normal termination and return `status`
470	/// to the host system
471	#[link_name = "sys_exit"]
472	pub fn exit(status: i32) -> !;
473
474	/// cause abnormal termination
475	#[link_name = "sys_abort"]
476	pub fn abort() -> !;
477
478	/// suspend execution for microsecond intervals
479	///
480	/// The usleep() function suspends execution of the calling
481	/// thread for (at least) `usecs` microseconds.
482	#[link_name = "sys_usleep"]
483	pub fn usleep(usecs: u64);
484
485	/// suspend thread execution for an interval measured in nanoseconds
486	#[link_name = "sys_nanosleep"]
487	pub fn nanosleep(req: *const timespec) -> i32;
488
489	/// spawn a new thread
490	///
491	/// spawn() starts a new thread. The new thread starts execution
492	/// by invoking `func(usize)`; `arg` is passed as the argument
493	/// to `func`. `prio` defines the priority of the new thread,
494	/// which can be between `LOW_PRIO` and `HIGH_PRIO`.
495	/// `core_id` defines the core, where the thread is located.
496	/// A negative value give the operating system the possibility
497	/// to select the core by its own.
498	#[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	/// spawn a new thread with user-specified stack size
508	///
509	/// spawn2() starts a new thread. The new thread starts execution
510	/// by invoking `func(usize)`; `arg` is passed as the argument
511	/// to `func`. `prio` defines the priority of the new thread,
512	/// which can be between `LOW_PRIO` and `HIGH_PRIO`.
513	/// `core_id` defines the core, where the thread is located.
514	/// A negative value give the operating system the possibility
515	/// to select the core by its own.
516	/// In contrast to spawn(), spawn2() is able to define the
517	/// stack size.
518	#[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	/// join with a terminated thread
528	///
529	/// The join() function waits for the thread specified by `id`
530	/// to terminate.
531	#[link_name = "sys_join"]
532	pub fn join(id: Tid) -> i32;
533
534	/// yield the processor
535	///
536	/// causes the calling thread to relinquish the CPU. The thread
537	/// is moved to the end of the queue for its static priority.
538	#[link_name = "sys_yield"]
539	pub fn yield_now();
540
541	/// get current time
542	///
543	/// The clock_gettime() functions allow the calling thread
544	/// to retrieve the value used by a clock which is specified
545	/// by `clockid`.
546	///
547	/// `CLOCK_REALTIME`: the system's real time clock,
548	/// expressed as the amount of time since the Epoch.
549	///
550	/// `CLOCK_MONOTONIC`: clock that increments monotonically,
551	/// tracking the time since an arbitrary point
552	#[link_name = "sys_clock_gettime"]
553	pub fn clock_gettime(clockid: clockid_t, tp: *mut timespec) -> i32;
554
555	/// open and possibly create a file
556	///
557	/// The open() system call opens the file specified by `name`.
558	/// If the specified file does not exist, it may optionally
559	/// be created by open().
560	#[link_name = "sys_open"]
561	pub fn open(name: *const c_char, flags: i32, mode: i32) -> i32;
562
563	/// open a directory
564	///
565	/// The opendir() system call opens the directory specified by `name`.
566	#[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	/// delete the file it refers to `name`
571	#[link_name = "sys_unlink"]
572	pub fn unlink(name: *const c_char) -> i32;
573
574	/// remove directory it refers to `name`
575	#[link_name = "sys_rmdir"]
576	pub fn rmdir(name: *const c_char) -> i32;
577
578	/// stat
579	#[link_name = "sys_stat"]
580	pub fn stat(name: *const c_char, stat: *mut stat) -> i32;
581
582	/// lstat
583	#[link_name = "sys_lstat"]
584	pub fn lstat(name: *const c_char, stat: *mut stat) -> i32;
585
586	/// fstat
587	#[link_name = "sys_fstat"]
588	pub fn fstat(fd: i32, stat: *mut stat) -> i32;
589
590	/// Returns an estimate of the default amount of parallelism
591	/// a program should use. This number often corresponds to the
592	/// amount of CPUs a computer has, but it may diverge in
593	/// various cases.
594	#[link_name = "sys_available_parallelism"]
595	pub fn available_parallelism() -> usize;
596
597	/// determines the number of activated processors
598	#[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	/// initialize the network stack
640	#[link_name = "sys_network_init"]
641	pub fn network_init() -> i32;
642
643	/// Add current task to the queue of blocked tasks. After calling `block_current_task`,
644	/// call `yield_now` to switch to another task.
645	#[link_name = "sys_block_current_task"]
646	pub fn block_current_task();
647
648	/// Add current task to the queue of blocked tasks, but wake it when `timeout` milliseconds
649	/// have elapsed.
650	///
651	/// After calling `block_current_task`, call `yield_now` to switch to another task.
652	#[link_name = "sys_block_current_task_with_timeout"]
653	pub fn block_current_task_with_timeout(timeout: u64);
654
655	/// Wakeup task with the thread id `tid`
656	#[link_name = "sys_wakeup_taskt"]
657	pub fn wakeup_task(tid: Tid);
658
659	/// The system call `getaddrbyname` determine the network host entry.
660	/// It expects an array of u8 with a size of in_addr or of in6_addr.
661	/// The result of the DNS request will be stored in this array.
662	///
663	/// # Example
664	///
665	/// ```
666	/// use hermit_abi::in_addr;
667	/// let c_string = std::ffi::CString::new("rust-lang.org").expect("CString::new failed");
668	/// let name = c_string.into_raw();
669	/// let mut inaddr: in_addr = Default::default();
670	/// let _ = unsafe {
671	///         hermit_abi::getaddrbyname(
672	///                 name,
673	///                 &mut inaddr as *mut _ as *mut u8,
674	///                 std::mem::size_of::<in_addr>(),
675	///         )
676	/// };
677	///
678	/// // retake pointer to free memory
679	/// let _ = CString::from_raw(name);
680	/// ```
681	#[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	/// bind a name to a socket
688	#[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	/// read from a file descriptor
695	///
696	/// read() attempts to read `len` bytes of data from the object
697	/// referenced by the descriptor `fd` into the buffer pointed
698	/// to by `buf`.
699	#[link_name = "sys_read"]
700	pub fn read(fd: i32, buf: *mut u8, len: usize) -> isize;
701
702	/// `read()` attempts to read `nbyte` of data to the object referenced by the
703	/// descriptor `fd` from a buffer. `read()` performs the same
704	/// action, but scatters the input data from the `iovcnt` buffers specified by the
705	/// members of the iov array: `iov[0], iov[1], ..., iov[iovcnt-1]`.
706	///
707	/// ```
708	/// struct iovec {
709	///     char   *iov_base;  /* Base address. */
710	///     size_t iov_len;    /* Length. */
711	/// };
712	/// ```
713	///
714	/// Each `iovec` entry specifies the base address and length of an area in memory from
715	/// which data should be written.  `readv()` will always fill an completely
716	/// before proceeding to the next.
717	#[link_name = "sys_readv"]
718	pub fn readv(fd: i32, iov: *const iovec, iovcnt: usize) -> isize;
719
720	/// `getdents64` reads directory entries from the directory referenced
721	/// by the file descriptor `fd` into the buffer pointed to by `buf`.
722	#[link_name = "sys_getdents64"]
723	pub fn getdents64(fd: i32, dirp: *mut dirent64, count: usize) -> i64;
724
725	/// 'mkdir' attempts to create a directory,
726	/// it returns 0 on success and -1 on error
727	#[link_name = "sys_mkdir"]
728	pub fn mkdir(name: *const i8, mode: u32) -> i32;
729
730	/// Fill `len` bytes in `buf` with cryptographically secure random data.
731	///
732	/// Returns either the number of bytes written to buf (a positive value) or
733	/// * `-EINVAL` if `flags` contains unknown flags.
734	/// * `-ENOSYS` if the system does not support random data generation.
735	#[link_name = "sys_read_entropy"]
736	pub fn read_entropy(buf: *mut u8, len: usize, flags: u32) -> isize;
737
738	/// receive() a message from a socket
739	#[link_name = "sys_recv"]
740	pub fn recv(socket: i32, buf: *mut u8, len: usize, flags: i32) -> isize;
741
742	/// receive() a message from a socket
743	#[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	/// The fseek() function sets the file position indicator for the stream pointed to by stream.
754	/// The new position, measured in bytes, is obtained by adding offset bytes to the position
755	/// specified by whence.  If whence is set to SEEK_SET, SEEK_CUR, or SEEK_END, the offset is
756	/// relative to the start of the file, the current position indicator, or end-of-file,
757	/// respectively.
758	#[link_name = "sys_lseek"]
759	pub fn lseek(fd: i32, offset: isize, whence: i32) -> isize;
760
761	/// write to a file descriptor
762	///
763	/// write() attempts to write `len` of data to the object
764	/// referenced by the descriptor `fd` from the
765	/// buffer pointed to by `buf`.
766	#[link_name = "sys_write"]
767	pub fn write(fd: i32, buf: *const u8, len: usize) -> isize;
768
769	/// `write()` attempts to write `nbyte` of data to the object referenced by the
770	/// descriptor `fd` from a buffer. `writev()` performs the same
771	/// action, but gathers the output data from the `iovcnt` buffers specified by the
772	/// members of the iov array: `iov[0], iov[1], ..., iov[iovcnt-1]`.
773	///
774	/// ```
775	/// struct iovec {
776	///     char   *iov_base;  /* Base address. */
777	///     size_t iov_len;    /* Length. */
778	/// };
779	/// ```
780	///
781	/// Each `iovec` entry specifies the base address and length of an area in memory from
782	/// which data should be written.  `writev()` will always write a
783	/// complete area before proceeding to the next.
784	#[link_name = "sys_writev"]
785	pub fn writev(fd: i32, iov: *const iovec, iovcnt: usize) -> isize;
786
787	/// close a file descriptor
788	///
789	/// The close() call deletes a file descriptor `fd` from the object
790	/// reference table.
791	#[link_name = "sys_close"]
792	pub fn close(fd: i32) -> i32;
793
794	/// duplicate an existing file descriptor
795	#[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	/// `eventfd` creates an linux-like "eventfd object" that can be used
829	/// as an event wait/notify mechanism by user-space applications, and by
830	/// the kernel to notify user-space applications of events. The
831	/// object contains an unsigned 64-bit integer counter
832	/// that is maintained by the kernel. This counter is initialized
833	/// with the value specified in the argument `initval`.
834	///
835	/// As its return value, `eventfd` returns a new file descriptor that
836	/// can be used to refer to the eventfd object.
837	///
838	/// The following values may be bitwise set in flags to change the
839	/// behavior of `eventfd`:
840	///
841	/// `EFD_NONBLOCK`: Set the file descriptor in non-blocking mode
842	/// `EFD_SEMAPHORE`: Provide semaphore-like semantics for reads
843	/// from the new file descriptor.
844	#[link_name = "sys_eventfd"]
845	pub fn eventfd(initval: u64, flags: i16) -> i32;
846
847	/// The unix-like `poll` waits for one of a set of file descriptors
848	/// to become ready to perform I/O. The set of file descriptors to be
849	/// monitored is specified in the `fds` argument, which is an array
850	/// of structures of `pollfd`.
851	#[link_name = "sys_poll"]
852	pub fn poll(fds: *mut pollfd, nfds: nfds_t, timeout: i32) -> i32;
853
854	/// listen for connections on a socket
855	///
856	/// The `backlog` parameter defines the maximum length for the queue of pending
857	/// connections. Currently, the `backlog` must be one.
858	#[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	/// shut down part of a full-duplex connection
875	#[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/// Determine the priority of the current thread
901#[inline(always)]
902pub unsafe fn get_priority() -> Priority {
903	Priority::from(sys_get_priority())
904}
905
906/// Determine the priority of the current thread
907#[inline(always)]
908pub unsafe fn set_priority(tid: Tid, prio: Priority) {
909	sys_set_priority(tid, prio.into());
910}