Skip to content

Commit cd740a9

Browse files
committed
Add sigaction
1 parent 905e11d commit cd740a9

File tree

1 file changed

+179
-23
lines changed

1 file changed

+179
-23
lines changed

src/wasi.rs

Lines changed: 179 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub type time_t = c_longlong;
3333
pub type c_double = f64;
3434
pub type c_float = f32;
3535
pub type ino_t = u64;
36+
#[cfg(not(target_vendor = "wasmer"))]
3637
pub type sigset_t = c_uchar;
3738
pub type suseconds_t = c_longlong;
3839
pub type mode_t = u32;
@@ -61,20 +62,12 @@ pub type pthread_t = c_ulong;
6162
#[cfg(target_vendor = "wasmer")]
6263
pub type pthread_key_t = ::c_uint;
6364

64-
#[cfg(target_vendor = "wasmer")]
65-
pub struct pthread_attr_t {
66-
#[cfg(target_pointer_width = "32")]
67-
__size: [u32; 8],
68-
#[cfg(target_pointer_width = "64")]
69-
__size: [u64; 7]
70-
}
71-
7265
s! {
7366
#[repr(C)]
7467
pub struct in_addr {
7568
pub s_addr: ::in_addr_t,
7669
}
77-
70+
7871
#[repr(C)]
7972
#[repr(align(4))]
8073
pub struct in6_addr {
@@ -262,6 +255,43 @@ s! {
262255
pub len: ::c_ushort,
263256
pub filter: *mut sock_filter,
264257
}
258+
259+
#[cfg(target_vendor = "wasmer")]
260+
#[repr(C)]
261+
pub struct pthread_attr_t {
262+
#[cfg(target_pointer_width = "32")]
263+
__size: [u32; 8],
264+
#[cfg(target_pointer_width = "64")]
265+
__size: [u64; 7],
266+
}
267+
268+
#[cfg(target_vendor = "wasmer")]
269+
#[repr(C)]
270+
pub struct sigset_t {
271+
#[cfg(target_pointer_width = "32")]
272+
__val: [u32; 2],
273+
#[cfg(target_pointer_width = "64")]
274+
__val: [u64; 1],
275+
}
276+
277+
#[cfg(target_vendor = "wasmer")]
278+
#[repr(C)]
279+
pub struct siginfo_t {
280+
pub si_signo: ::c_int,
281+
pub si_errno: ::c_int,
282+
pub si_code: ::c_int,
283+
_pad: [::c_int; 31],
284+
_align: [u64; 0],
285+
}
286+
287+
#[cfg(target_vendor = "wasmer")]
288+
#[repr(C)]
289+
pub struct sigaction {
290+
pub sa_sigaction: ::sighandler_t,
291+
pub sa_mask: ::sigset_t,
292+
pub sa_flags: ::c_int,
293+
pub sa_restorer: ::Option<extern fn()>,
294+
}
265295
}
266296

267297
pub const __WASI_SDFLAGS_RD: ::c_int = 1;
@@ -843,13 +873,34 @@ pub const SIGHUP: ::c_int = 1;
843873
pub const SIGINT: ::c_int = 2;
844874
pub const SIGQUIT: ::c_int = 3;
845875
pub const SIGILL: ::c_int = 4;
876+
pub const SIGTRAP: ::c_int = 5;
846877
pub const SIGABRT: ::c_int = 6;
878+
pub const SIGBUS: ::c_int = 7;
847879
pub const SIGFPE: ::c_int = 8;
848880
pub const SIGKILL: ::c_int = 9;
881+
pub const SIGUSR1: ::c_int = 10;
849882
pub const SIGSEGV: ::c_int = 11;
883+
pub const SIGUSR2: ::c_int = 12;
850884
pub const SIGPIPE: ::c_int = 13;
851885
pub const SIGALRM: ::c_int = 14;
852886
pub const SIGTERM: ::c_int = 15;
887+
pub const SIGSTKFLT: c_int = 16;
888+
pub const SIGCHLD: c_int = 17;
889+
pub const SIGCONT: c_int = 18;
890+
pub const SIGSTOP: c_int = 19;
891+
pub const SIGTSTP: c_int = 20;
892+
pub const SIGTTIN: c_int = 21;
893+
pub const SIGTTOU: c_int = 22;
894+
pub const SIGURG: c_int = 23;
895+
pub const SIGXCPU: c_int = 24;
896+
pub const SIGXFSZ: c_int = 25;
897+
pub const SIGVTALRM: c_int = 26;
898+
pub const SIGPROF: c_int = 27;
899+
pub const SIGWINCH: c_int = 28;
900+
pub const SIGIO: c_int = 29;
901+
pub const SIGPOLL: c_int = 29;
902+
pub const SIGPWR: c_int = 30;
903+
pub const SIGSYS: c_int = 31;
853904

854905
#[cfg(target_vendor = "wasmer")]
855906
pub const SIG_DFL: sighandler_t = 0 as sighandler_t;
@@ -858,6 +909,19 @@ pub const SIG_IGN: sighandler_t = 1 as sighandler_t;
858909
#[cfg(target_vendor = "wasmer")]
859910
pub const SIG_ERR: sighandler_t = !0 as sighandler_t;
860911

912+
pub const SIG_BLOCK: c_int = 0;
913+
pub const SIG_UNBLOCK: c_int = 1;
914+
pub const SIG_SETMASK: c_int = 2;
915+
916+
pub const SA_NOCLDSTOP: c_int = 1;
917+
pub const SA_NOCLDWAIT: c_int = 2;
918+
pub const SA_SIGINFO: c_int = 4;
919+
pub const SA_ONSTACK: c_int = 0x08000000;
920+
pub const SA_RESTART: c_int = 0x10000000;
921+
pub const SA_NODEFER: c_int = 0x40000000;
922+
pub const SA_RESETHAND: c_int = 0x80000000;
923+
pub const SA_RESTORER: c_int = 0x04000000;
924+
861925
pub const SEEK_SET: c_int = 0;
862926
pub const SEEK_CUR: c_int = 1;
863927
pub const SEEK_END: c_int = 2;
@@ -1400,29 +1464,80 @@ extern "C" {
14001464
pub fn localeconv() -> *mut lconv;
14011465

14021466
pub fn accept(socket: ::c_int, addr: *mut sockaddr, addrlen: *mut socklen_t) -> ::c_int;
1403-
pub fn accept4(socket: ::c_int, addr: *mut sockaddr, addrlen: *mut socklen_t, flags: ::c_int) -> ::c_int;
1467+
pub fn accept4(
1468+
socket: ::c_int,
1469+
addr: *mut sockaddr,
1470+
addrlen: *mut socklen_t,
1471+
flags: ::c_int,
1472+
) -> ::c_int;
14041473
pub fn bind(socket: ::c_int, addr: *const ::sockaddr, addrlen: ::socklen_t) -> ::c_int;
14051474
pub fn connect(socket: ::c_int, addr: *const sockaddr, addrlen: socklen_t) -> ::c_int;
14061475
pub fn freeifaddrs(ifa: *mut ::ifaddrs);
14071476
pub fn getifaddrs(ifap: *mut *mut ::ifaddrs) -> ::c_int;
14081477
pub fn getpeername(socket: ::c_int, addr: *mut sockaddr, addrlen: *mut socklen_t) -> ::c_int;
14091478
pub fn getsockname(socket: ::c_int, addr: *mut sockaddr, addrlen: *mut socklen_t) -> ::c_int;
1410-
pub fn getsockopt(sockfd: ::c_int, level: ::c_int, option_name: ::c_int, option_value: *mut ::c_void, option_len: *mut ::socklen_t) -> ::c_int;
1479+
pub fn getsockopt(
1480+
sockfd: ::c_int,
1481+
level: ::c_int,
1482+
option_name: ::c_int,
1483+
option_value: *mut ::c_void,
1484+
option_len: *mut ::socklen_t,
1485+
) -> ::c_int;
14111486
pub fn listen(socket: ::c_int, backlog: ::c_int) -> ::c_int;
1412-
pub fn recv(socket: ::c_int, buffer: *mut ::c_void, length: ::size_t, flags: ::c_int) -> ::ssize_t;
1413-
pub fn recvfrom(socket: ::c_int, buffer: *mut ::c_void, length: ::size_t, flags: ::c_int, addr: *mut ::sockaddr, addrlen: *mut ::socklen_t) -> ::ssize_t;
1487+
pub fn recv(
1488+
socket: ::c_int,
1489+
buffer: *mut ::c_void,
1490+
length: ::size_t,
1491+
flags: ::c_int,
1492+
) -> ::ssize_t;
1493+
pub fn recvfrom(
1494+
socket: ::c_int,
1495+
buffer: *mut ::c_void,
1496+
length: ::size_t,
1497+
flags: ::c_int,
1498+
addr: *mut ::sockaddr,
1499+
addrlen: *mut ::socklen_t,
1500+
) -> ::ssize_t;
14141501
pub fn recvmsg(socket: ::c_int, msg: *mut ::msghdr, flags: ::c_int) -> ::ssize_t;
1415-
pub fn send(socket: ::c_int, buffer: *const ::c_void, length: ::size_t, flags: ::c_int) -> ::ssize_t;
1416-
pub fn sendfile(socket: ::c_int, in_fd: ::c_int, ofs: *const ::off_t, count: ::size_t) -> ::ssize_t;
1502+
pub fn send(
1503+
socket: ::c_int,
1504+
buffer: *const ::c_void,
1505+
length: ::size_t,
1506+
flags: ::c_int,
1507+
) -> ::ssize_t;
1508+
pub fn sendfile(
1509+
socket: ::c_int,
1510+
in_fd: ::c_int,
1511+
ofs: *const ::off_t,
1512+
count: ::size_t,
1513+
) -> ::ssize_t;
14171514
pub fn sendmsg(socket: ::c_int, msg: *const ::msghdr, flags: ::c_int) -> ::ssize_t;
1418-
pub fn sendto(socket: ::c_int, buffer: *const ::c_void, length: ::size_t, flags: ::c_int, addr: *const sockaddr, addrlen: socklen_t) -> ::ssize_t;
1419-
pub fn setsockopt(socket: ::c_int, level: ::c_int, option_name: ::c_int, option_value: *const ::c_void, option_len: socklen_t) -> ::c_int;
1515+
pub fn sendto(
1516+
socket: ::c_int,
1517+
buffer: *const ::c_void,
1518+
length: ::size_t,
1519+
flags: ::c_int,
1520+
addr: *const sockaddr,
1521+
addrlen: socklen_t,
1522+
) -> ::ssize_t;
1523+
pub fn setsockopt(
1524+
socket: ::c_int,
1525+
level: ::c_int,
1526+
option_name: ::c_int,
1527+
option_value: *const ::c_void,
1528+
option_len: socklen_t,
1529+
) -> ::c_int;
14201530
pub fn shutdown(socket: ::c_int, how: ::c_int) -> ::c_int;
14211531
pub fn socket(domain: ::c_int, ty: ::c_int, protocol: ::c_int) -> ::c_int;
1422-
pub fn socketpair(domain: ::c_int, ty: ::c_int, protocol: ::c_int, socket_vector: *mut ::c_int) -> ::c_int;
1532+
pub fn socketpair(
1533+
domain: ::c_int,
1534+
ty: ::c_int,
1535+
protocol: ::c_int,
1536+
socket_vector: *mut ::c_int,
1537+
) -> ::c_int;
14231538

14241539
pub fn getpid() -> ::pid_t;
1425-
1540+
14261541
pub fn readlink(path: *const c_char, buf: *mut c_char, bufsz: ::size_t) -> ::ssize_t;
14271542

14281543
pub fn timegm(tm: *mut ::tm) -> time_t;
@@ -1681,7 +1796,7 @@ extern "C" {
16811796
pub fn waitpid(pid: pid_t, status: *mut ::c_int, options: ::c_int) -> pid_t;
16821797
#[cfg(target_vendor = "wasmer")]
16831798
pub fn kill(pid: pid_t, sig: ::c_int) -> ::c_int;
1684-
1799+
16851800
#[cfg(target_vendor = "wasmer")]
16861801
pub fn sigemptyset(set: *mut sigset_t) -> ::c_int;
16871802
#[cfg(target_vendor = "wasmer")]
@@ -1732,16 +1847,57 @@ extern "C" {
17321847
pub fn pthread_getspecific(key: pthread_key_t) -> *mut ::c_void;
17331848
#[cfg(target_vendor = "wasmer")]
17341849
pub fn pthread_setspecific(key: pthread_key_t, value: *const ::c_void) -> ::c_int;
1850+
#[cfg(target_vendor = "wasmer")]
1851+
pub fn raise(signum: ::c_int) -> ::c_int;
1852+
1853+
#[cfg(target_vendor = "wasmer")]
1854+
fn sigaction_external_default(
1855+
sig: ::c_int,
1856+
sa: *const sigaction,
1857+
old: *mut sigaction,
1858+
_external_handler: ::Option<unsafe extern "C" fn(::c_int)>
1859+
) -> ::c_int;
1860+
#[cfg(target_vendor = "wasmer")]
1861+
fn __wasm_signal(signum: ::c_int);
1862+
}
1863+
1864+
#[cfg(target_vendor = "wasmer")]
1865+
pub unsafe fn sigaction(
1866+
sig: ::c_int,
1867+
sa: *const sigaction,
1868+
old: *mut sigaction,
1869+
) -> ::c_int {
1870+
sigaction_external_default(sig, sa, old, Some(default_handler))
1871+
}
1872+
1873+
#[cfg(target_vendor = "wasmer")]
1874+
extern "C" fn default_handler(sig: ::c_int) {
1875+
if sig == SIGCHLD
1876+
|| sig == SIGURG
1877+
|| sig == SIGWINCH
1878+
|| sig == SIGCONT {
1879+
return;
1880+
} else {
1881+
unsafe { abort() };
1882+
}
1883+
}
1884+
1885+
#[cfg(target_vendor = "wasmer")]
1886+
mod wasm_signal {
1887+
#[no_mangle]
1888+
extern "C" fn __wasm_signal(signum: ::c_int) {
1889+
unsafe { super::__wasm_signal(signum) };
1890+
}
17351891
}
17361892

17371893
/// mocked functions that dont do anything in WASI land
1738-
pub fn mlock(addr: *const ::c_void, len: ::size_t) -> ::c_int {
1894+
pub fn mlock(_addr: *const ::c_void, _len: ::size_t) -> ::c_int {
17391895
0
17401896
}
1741-
pub fn munlock(addr: *const ::c_void, len: ::size_t) -> ::c_int {
1897+
pub fn munlock(_addr: *const ::c_void, _len: ::size_t) -> ::c_int {
17421898
0
17431899
}
1744-
pub fn mlockall(flags: ::c_int) -> ::c_int {
1900+
pub fn mlockall(_flags: ::c_int) -> ::c_int {
17451901
0
17461902
}
17471903
pub fn munlockall() -> ::c_int {

0 commit comments

Comments
 (0)