Skip to main content

c/
legacy_compat.rs

1// Copyright (c) 2026 Meng Zhuo
2// SPDX-License-Identifier: BSD-3-Clause
3
4// ============================================================
5// Legacy syscall/stdio/signal compatibility
6// ============================================================
7
8#[no_mangle]
9pub unsafe extern "C" fn settimeofday(tv: *const c_void, tz: *const c_void) -> c_int {
10    let r = <Arch as Syscalls>::syscall2(SYS_SETTIMEOFDAY, tv as i64, tz as i64);
11    syscall_ret(r) as c_int
12}
13
14#[no_mangle]
15pub unsafe extern "C" fn stime(t: *const i64) -> c_int {
16    if t.is_null() { ERRNO!() = EINVAL; return -1; }
17    let tv = timeval { tv_sec: *t, tv_usec: 0 };
18    settimeofday(&tv as *const timeval as *const c_void, core::ptr::null())
19}
20
21#[no_mangle]
22pub unsafe extern "C" fn times(buf: *mut c_void) -> c_long {
23    let r = <Arch as Syscalls>::syscall1(SYS_TIMES, buf as i64);
24    if r < 0 { ERRNO!() = (-r) as c_int; -1 } else { r as c_long }
25}
26
27#[no_mangle]
28pub unsafe extern "C" fn splice(fd_in: c_int, off_in: *mut i64, fd_out: c_int, off_out: *mut i64, len: usize, flags: u32) -> isize {
29    let r = <Arch as Syscalls>::syscall6(SYS_SPLICE, fd_in as i64, off_in as i64, fd_out as i64, off_out as i64, len as i64, flags as i64);
30    if r < 0 { ERRNO!() = (-r) as c_int; -1 } else { r as isize }
31}
32
33#[no_mangle]
34pub unsafe extern "C" fn tee(fd_in: c_int, fd_out: c_int, len: usize, flags: u32) -> isize {
35    let r = <Arch as Syscalls>::syscall4(SYS_TEE, fd_in as i64, fd_out as i64, len as i64, flags as i64);
36    if r < 0 { ERRNO!() = (-r) as c_int; -1 } else { r as isize }
37}
38
39#[no_mangle]
40pub unsafe extern "C" fn statx(dirfd: c_int, path: *const c_char, flags: c_int, mask: u32, buf: *mut c_void) -> c_int {
41    let r = <Arch as Syscalls>::syscall5(SYS_STATX, dirfd as i64, path as i64, flags as i64, mask as i64, buf as i64);
42    syscall_ret(r) as c_int
43}
44
45#[no_mangle]
46pub unsafe extern "C" fn setns(fd: c_int, nstype: c_int) -> c_int {
47    let r = <Arch as Syscalls>::syscall2(SYS_SETNS, fd as i64, nstype as i64);
48    syscall_ret(r) as c_int
49}
50
51#[no_mangle]
52pub unsafe extern "C" fn setfsuid(uid: u32) -> c_int {
53    <Arch as Syscalls>::syscall1(SYS_SETFSUID, uid as i64) as c_int
54}
55
56#[no_mangle]
57pub unsafe extern "C" fn setfsgid(gid: u32) -> c_int {
58    <Arch as Syscalls>::syscall1(SYS_SETFSGID, gid as i64) as c_int
59}
60
61#[no_mangle]
62pub unsafe extern "C" fn setregid(rgid: u32, egid: u32) -> c_int {
63    let r = <Arch as Syscalls>::syscall2(SYS_SETREGID, rgid as i64, egid as i64);
64    syscall_ret(r) as c_int
65}
66
67#[no_mangle]
68pub unsafe extern "C" fn process_vm_readv(pid: c_int, local: *const c_void, liovcnt: usize, remote: *const c_void, riovcnt: usize, flags: u64) -> isize {
69    let r = <Arch as Syscalls>::syscall6(SYS_PROCESS_VM_READV, pid as i64, local as i64, liovcnt as i64, remote as i64, riovcnt as i64, flags as i64);
70    if r < 0 { ERRNO!() = (-r) as c_int; -1 } else { r as isize }
71}
72
73#[no_mangle]
74pub unsafe extern "C" fn process_vm_writev(pid: c_int, local: *const c_void, liovcnt: usize, remote: *const c_void, riovcnt: usize, flags: u64) -> isize {
75    let r = <Arch as Syscalls>::syscall6(SYS_PROCESS_VM_WRITEV, pid as i64, local as i64, liovcnt as i64, remote as i64, riovcnt as i64, flags as i64);
76    if r < 0 { ERRNO!() = (-r) as c_int; -1 } else { r as isize }
77}
78
79#[no_mangle]
80pub unsafe extern "C" fn tcgetwinsize(fd: c_int, ws: *mut c_void) -> c_int {
81    if fd < 0 || ws.is_null() { ERRNO!() = EINVAL; return -1; }
82    let r = <Arch as Syscalls>::syscall3(SYS_IOCTL, fd as i64, 0x5413, ws as i64);
83    syscall_ret(r) as c_int
84}
85
86#[no_mangle]
87pub unsafe extern "C" fn tcsetwinsize(fd: c_int, ws: *const c_void) -> c_int {
88    if fd < 0 || ws.is_null() { ERRNO!() = EINVAL; return -1; }
89    let r = <Arch as Syscalls>::syscall3(SYS_IOCTL, fd as i64, 0x5414, ws as i64);
90    syscall_ret(r) as c_int
91}
92
93#[no_mangle]
94pub unsafe extern "C" fn sockatmark(fd: c_int) -> c_int {
95    let mut at: c_int = 0;
96    let r = <Arch as Syscalls>::syscall3(SYS_IOCTL, fd as i64, 0x8905, &mut at as *mut c_int as i64);
97    if r < 0 { ERRNO!() = (-r) as c_int; -1 } else { at }
98}
99
100#[no_mangle]
101pub unsafe extern "C" fn isastream(fd: c_int) -> c_int {
102    let mut buf = [0u8; 64];
103    let r = <Arch as Syscalls>::syscall3(SYS_IOCTL, fd as i64, 0x5401, buf.as_mut_ptr() as i64);
104    if r < 0 { ERRNO!() = (-r) as c_int; 0 } else { 1 }
105}
106
107#[no_mangle]
108pub unsafe extern "C" fn posix_madvise(addr: *mut c_void, len: usize, advice: c_int) -> c_int {
109    madvise(addr as *mut u8, len, advice)
110}
111
112#[no_mangle]
113pub unsafe extern "C" fn secure_getenv(name: *const c_char) -> *mut c_char {
114    if getauxval(23) != 0 { return core::ptr::null_mut(); }
115    getenv(name)
116}
117
118#[no_mangle]
119pub unsafe extern "C" fn sigandset(dest: *mut c_ulong, left: *const c_ulong, right: *const c_ulong) -> c_int {
120    if dest.is_null() || left.is_null() || right.is_null() { return -1; }
121    *dest = *left & *right;
122    0
123}
124
125#[no_mangle]
126pub unsafe extern "C" fn sigorset(dest: *mut c_ulong, left: *const c_ulong, right: *const c_ulong) -> c_int {
127    if dest.is_null() || left.is_null() || right.is_null() { return -1; }
128    *dest = *left | *right;
129    0
130}
131
132#[no_mangle]
133pub unsafe extern "C" fn sigisemptyset(set: *const c_ulong) -> c_int {
134    if set.is_null() { return 1; }
135    (*set == 0) as c_int
136}
137
138#[no_mangle]
139pub unsafe extern "C" fn sighold(sig: c_int) -> c_int {
140    let set: c_ulong = 1u64 << (sig - 1) as u32;
141    sigprocmask(0, &set, core::ptr::null_mut());
142    0
143}
144
145#[no_mangle]
146pub unsafe extern "C" fn sigignore(sig: c_int) -> c_int {
147    let _ = signal(sig, 1);
148    0
149}
150
151#[no_mangle]
152pub unsafe extern "C" fn sigrelse(sig: c_int) -> c_int {
153    let set: c_ulong = 1u64 << (sig - 1) as u32;
154    sigprocmask(1, &set, core::ptr::null_mut());
155    0
156}
157
158#[no_mangle]
159pub unsafe extern "C" fn siginterrupt(sig: c_int, flag: c_int) -> c_int {
160    let mut sa: sigaction = core::mem::zeroed();
161    if sigaction(sig, core::ptr::null(), &mut sa) != 0 { return -1; }
162    if flag != 0 { sa.sa_flags &= !(SA_RESTART as c_int); } else { sa.sa_flags |= SA_RESTART as c_int; }
163    sigaction(sig, &sa, core::ptr::null_mut())
164}
165
166#[no_mangle]
167pub unsafe extern "C" fn sigpause(sig: c_int) -> c_int {
168    let set: c_ulong = 1u64 << (sig - 1) as u32;
169    sigsuspend(&set)
170}
171
172#[no_mangle]
173pub unsafe extern "C" fn semtimedop(semid: c_int, sops: *mut c_void, nsops: usize, timeout: *const timespec) -> c_int {
174    let r = <Arch as Syscalls>::syscall4(SYS_SEMTIMEDOP, semid as i64, sops as i64, nsops as i64, timeout as i64);
175    syscall_ret(r) as c_int
176}
177
178#[no_mangle]
179pub unsafe extern "C" fn wait3(status: *mut c_int, options: c_int, rusage: *mut c_void) -> i32 {
180    wait4(-1, status, options, rusage) as i32
181}
182
183#[no_mangle]
184pub unsafe extern "C" fn tempnam(dir: *const c_char, pfx: *const c_char) -> *mut c_char {
185    let mut path = [0 as c_char; 256];
186    let mut i = 0;
187    if !dir.is_null() {
188        while *dir.add(i) != 0 && i < 200 { path[i] = *dir.add(i); i += 1; }
189        if i > 0 && path[i-1] != b'/' as c_char { path[i] = b'/' as c_char; i += 1; }
190    } else {
191        for &c in b"/tmp/" { path[i] = c as c_char; i += 1; }
192    }
193    if !pfx.is_null() {
194        let mut j = 0;
195        while *pfx.add(j) != 0 && i < 220 { path[i] = *pfx.add(j); i += 1; j += 1; }
196    }
197    for &c in b"XXXXXX" { path[i] = c as c_char; i += 1; }
198    path[i] = 0;
199    let fd = mkstemp(path.as_mut_ptr());
200    if fd < 0 { return core::ptr::null_mut(); }
201    close(fd);
202    unlink(path.as_ptr());
203    let n = strlen(path.as_ptr());
204    let out = malloc(n + 1) as *mut c_char;
205    if out.is_null() { return core::ptr::null_mut(); }
206    core::ptr::copy_nonoverlapping(path.as_ptr(), out, n + 1);
207    out
208}
209
210#[no_mangle]
211pub unsafe extern "C" fn putwc(c: u32, f: *mut c_void) -> u32 {
212    fputwc(c as c_int, f as *mut FILE) as u32
213}
214
215#[no_mangle]
216pub unsafe extern "C" fn __uflow(f: *mut c_void) -> c_int {
217    fgetc(f as *mut FILE)
218}
219
220#[no_mangle]
221pub unsafe extern "C" fn __xmknod(_ver: c_int, path: *const c_char, mode: c_uint, dev: u64) -> c_int {
222    mknod(path, mode, dev)
223}
224
225#[no_mangle]
226pub unsafe extern "C" fn __xmknodat(_ver: c_int, dirfd: c_int, path: *const c_char, mode: c_uint, dev: u64) -> c_int {
227    mknodat(dirfd, path, mode, dev)
228}
229
230#[no_mangle]
231pub unsafe extern "C" fn name_to_handle_at(dirfd: c_int, path: *const c_char, handle: *mut c_void, mount_id: *mut c_int, flags: c_int) -> c_int {
232    let r = <Arch as Syscalls>::syscall5(SYS_NAME_TO_HANDLE_AT, dirfd as i64, path as i64, handle as i64, mount_id as i64, flags as i64);
233    syscall_ret(r) as c_int
234}
235
236#[no_mangle]
237pub unsafe extern "C" fn open_by_handle_at(mount_fd: c_int, handle: *mut c_void, flags: c_int) -> c_int {
238    let r = <Arch as Syscalls>::syscall3(SYS_OPEN_BY_HANDLE_AT, mount_fd as i64, handle as i64, flags as i64);
239    syscall_ret(r) as c_int
240}
241
242#[no_mangle]
243pub unsafe extern "C" fn dn_skipname(comp: *const u8, eom: *const u8) -> c_int {
244    let len = match (eom as usize).checked_sub(comp as usize) {
245        Some(n) => n,
246        None => return -1,
247    };
248    let msg = match InBuf::new(comp, len) {
249        Some(msg) => msg,
250        None => return -1,
251    };
252    match dns_skip_name(&msg, 0) {
253        Some(n) if n <= c_int::MAX as usize => n as c_int,
254        _ => -1,
255    }
256}
257
258#[no_mangle]
259pub unsafe extern "C" fn dn_comp(exp: *const c_char, dst: *mut u8, dstsiz: c_int, _dnptrs: *mut *mut u8, _lastdnptr: *mut *mut u8) -> c_int {
260    if exp.is_null() || dst.is_null() || dstsiz <= 0 { return -1; }
261    let mut o = 0usize;
262    let mut p = exp;
263    loop {
264        let start = p;
265        while *p != 0 && *p != b'.' as c_char { p = p.add(1); }
266        let l = p as usize - start as usize;
267        if l > 63 || o + l + 2 > dstsiz as usize { return -1; }
268        *dst.add(o) = l as u8; o += 1;
269        core::ptr::copy_nonoverlapping(start as *const u8, dst.add(o), l);
270        o += l;
271        if *p == 0 { break; }
272        p = p.add(1);
273    }
274    if o + 1 > dstsiz as usize { return -1; }
275    *dst.add(o) = 0;
276    (o + 1) as c_int
277}
278