1pub type thrd_start_t = Option<unsafe extern "C" fn(arg: *mut core::ffi::c_void) -> i32>;
11
12#[no_mangle]
14pub unsafe extern "C" fn thrd_create(thr: *mut usize, func: thrd_start_t, arg: *mut core::ffi::c_void) -> i32 {
15 let mut thread: u64 = 0;
16 let r = pthread_create(&mut thread, core::ptr::null(), core::mem::transmute(func), arg);
17 if r == 0 {
18 *thr = thread as usize;
19 0 } else {
21 -1 }
23}
24
25#[no_mangle]
26pub unsafe extern "C" fn thrd_join(thr: usize, res: *mut i32) -> i32 {
27 let mut retval: *mut core::ffi::c_void = core::ptr::null_mut();
28 let r = pthread_join(thr as u64, &mut retval);
29 if r == 0 {
30 if !res.is_null() { *res = retval as i32; }
31 0
32 } else {
33 -1
34 }
35}
36
37#[no_mangle]
38pub unsafe extern "C" fn thrd_exit(res: i32) {
39 pthread_exit(res as *mut core::ffi::c_void);
40}
41
42#[no_mangle]
43pub unsafe extern "C" fn thrd_detach(thr: usize) -> i32 {
44 let r = pthread_detach(thr as u64);
45 if r == 0 { 0 } else { -1 }
46}
47
48#[no_mangle]
49pub unsafe extern "C" fn thrd_current() -> usize {
50 pthread_self() as usize
51}
52
53#[no_mangle]
54pub unsafe extern "C" fn thrd_equal(thr1: usize, thr2: usize) -> i32 {
55 (thr1 == thr2) as i32
56}
57
58#[no_mangle]
59pub unsafe extern "C" fn thrd_sleep(duration: *const core::ffi::c_void, remaining: *mut core::ffi::c_void) -> i32 {
60 nanosleep(duration as *const timespec, remaining as *mut timespec)
61}
62
63#[no_mangle]
64pub unsafe extern "C" fn thrd_yield() {
65 sched_yield();
66}
67
68#[no_mangle]
70pub unsafe extern "C" fn mtx_init(mtx: *mut usize, _mtx_type: i32) -> i32 {
71 let r = pthread_mutex_init(mtx as *mut pthread_mutex_t, core::ptr::null());
72 if r == 0 { 0 } else { -1 }
73}
74
75#[no_mangle]
76pub unsafe extern "C" fn mtx_lock(mtx: *mut usize) -> i32 {
77 let r = pthread_mutex_lock(mtx as *mut pthread_mutex_t);
78 if r == 0 { 0 } else { -1 }
79}
80
81#[no_mangle]
82pub unsafe extern "C" fn mtx_trylock(mtx: *mut usize) -> i32 {
83 let r = pthread_mutex_trylock(mtx as *mut pthread_mutex_t);
84 if r == 0 { 0 } else { -1 }
85}
86
87#[no_mangle]
88pub unsafe extern "C" fn mtx_unlock(mtx: *mut usize) -> i32 {
89 let r = pthread_mutex_unlock(mtx as *mut pthread_mutex_t);
90 if r == 0 { 0 } else { -1 }
91}
92
93#[no_mangle]
94pub unsafe extern "C" fn mtx_destroy(mtx: *mut usize) {
95 pthread_mutex_destroy(mtx as *mut pthread_mutex_t);
96}
97
98#[no_mangle]
99pub unsafe extern "C" fn mtx_timedlock(mtx: *mut usize, ts: *const core::ffi::c_void) -> i32 {
100 let r = pthread_mutex_timedlock(mtx as *mut pthread_mutex_t, ts as *const timespec);
101 if r == 0 { 0 } else { -1 }
102}
103
104#[no_mangle]
106pub unsafe extern "C" fn cnd_init(cnd: *mut usize) -> i32 {
107 let r = pthread_cond_init(cnd as *mut pthread_cond_t, core::ptr::null());
108 if r == 0 { 0 } else { -1 }
109}
110
111#[no_mangle]
112pub unsafe extern "C" fn cnd_signal(cnd: *mut usize) -> i32 {
113 let r = pthread_cond_signal(cnd as *mut pthread_cond_t);
114 if r == 0 { 0 } else { -1 }
115}
116
117#[no_mangle]
118pub unsafe extern "C" fn cnd_broadcast(cnd: *mut usize) -> i32 {
119 let r = pthread_cond_broadcast(cnd as *mut pthread_cond_t);
120 if r == 0 { 0 } else { -1 }
121}
122
123#[no_mangle]
124pub unsafe extern "C" fn cnd_wait(cnd: *mut usize, mtx: *mut usize) -> i32 {
125 let r = pthread_cond_wait(cnd as *mut pthread_cond_t, mtx as *mut pthread_mutex_t);
126 if r == 0 { 0 } else { -1 }
127}
128
129#[no_mangle]
130pub unsafe extern "C" fn cnd_timedwait(cnd: *mut usize, mtx: *mut usize, ts: *const core::ffi::c_void) -> i32 {
131 let r = pthread_cond_timedwait(cnd as *mut pthread_cond_t, mtx as *mut pthread_mutex_t, ts as *const timespec);
132 if r == 0 { 0 } else { -1 }
133}
134
135#[no_mangle]
136pub unsafe extern "C" fn cnd_destroy(cnd: *mut usize) {
137 pthread_cond_destroy(cnd as *mut pthread_cond_t);
138}
139
140#[no_mangle]
142pub unsafe extern "C" fn tss_create(key: *mut u32, dtor: Option<unsafe extern "C" fn(*mut core::ffi::c_void)>) -> i32 {
143 let r = pthread_key_create(key, dtor);
144 if r == 0 { 0 } else { -1 }
145}
146
147#[no_mangle]
148pub unsafe extern "C" fn tss_delete(key: u32) {
149 pthread_key_delete(key);
150}
151
152#[no_mangle]
153pub unsafe extern "C" fn tss_set(key: u32, val: *mut core::ffi::c_void) -> i32 {
154 let r = pthread_setspecific(key, val);
155 if r == 0 { 0 } else { -1 }
156}
157
158#[no_mangle]
159pub unsafe extern "C" fn tss_get(key: u32) -> *mut core::ffi::c_void {
160 pthread_getspecific(key)
161}
162
163#[no_mangle]
165pub unsafe extern "C" fn call_once(flag: *mut u32, func: Option<unsafe extern "C" fn()>) {
166 if flag.is_null() { return; }
167 let state = &*(flag as *const AtomicU32);
168 if state.load(Ordering::Acquire) == 2 { return; }
169 if state.compare_exchange(0, 1, Ordering::AcqRel, Ordering::Acquire).is_ok() {
170 if let Some(f) = func { f(); }
171 state.store(2, Ordering::Release);
172 } else {
173 while state.load(Ordering::Acquire) != 2 {
174 core::hint::spin_loop();
175 }
176 }
177}
178
179#[no_mangle]
185pub unsafe extern "C" fn accept4(fd: i32, addr: *mut core::ffi::c_void, addrlen: *mut u32, flags: i32) -> i32 {
186 syscall_ret(<Arch as Syscalls>::syscall4(SYS_ACCEPT4, fd as i64, addr as i64, addrlen as i64, flags as i64)) as i32
187}
188
189#[no_mangle]
195pub unsafe extern "C" fn epoll_create(size: i32) -> i32 {
196 #[cfg(any(target_arch = "x86_64", target_arch = "powerpc64"))]
197 { syscall_ret(<Arch as Syscalls>::syscall1(SYS_EPOLL_CREATE, size as i64)) as i32 }
198 #[cfg(any(target_arch = "aarch64", target_arch = "riscv64", target_arch = "loongarch64"))]
199 {
200 if size < 1 {
201 ERRNO!() = EINVAL;
202 return -1;
203 }
204 syscall_ret(<Arch as Syscalls>::syscall1(SYS_EPOLL_CREATE1, 0)) as i32
205 }
206}
207
208#[no_mangle]
209pub unsafe extern "C" fn epoll_create1(flags: i32) -> i32 {
210 syscall_ret(<Arch as Syscalls>::syscall1(SYS_EPOLL_CREATE1, flags as i64)) as i32
211}
212
213#[no_mangle]
214pub unsafe extern "C" fn epoll_ctl(epfd: i32, op: i32, fd: i32, event: *mut core::ffi::c_void) -> i32 {
215 syscall_ret(<Arch as Syscalls>::syscall4(SYS_EPOLL_CTL, epfd as i64, op as i64, fd as i64, event as i64)) as i32
216}
217
218#[no_mangle]
219pub unsafe extern "C" fn epoll_wait(epfd: i32, events: *mut core::ffi::c_void, maxevents: i32, timeout: i32) -> i32 {
220 #[cfg(any(target_arch = "x86_64", target_arch = "powerpc64"))]
221 {
222 syscall_ret(<Arch as Syscalls>::syscall4(
223 SYS_EPOLL_WAIT,
224 epfd as i64,
225 events as i64,
226 maxevents as i64,
227 timeout as i64,
228 )) as i32
229 }
230 #[cfg(any(target_arch = "aarch64", target_arch = "riscv64", target_arch = "loongarch64"))]
231 {
232 epoll_pwait(epfd, events, maxevents, timeout, core::ptr::null())
233 }
234}
235
236#[no_mangle]
237pub unsafe extern "C" fn epoll_pwait(
238 epfd: i32,
239 events: *mut core::ffi::c_void,
240 maxevents: i32,
241 timeout: i32,
242 sigmask: *const u64,
243) -> i32 {
244 syscall_ret(<Arch as Syscalls>::syscall6(
245 SYS_EPOLL_PWAIT,
246 epfd as i64,
247 events as i64,
248 maxevents as i64,
249 timeout as i64,
250 sigmask as i64,
251 8,
252 )) as i32
253}
254
255#[no_mangle]
256pub unsafe extern "C" fn eventfd(initval: u32, flags: i32) -> i32 {
257 syscall_ret(<Arch as Syscalls>::syscall2(SYS_EVENTFD2, initval as i64, flags as i64)) as i32
258}
259
260#[no_mangle]
261pub unsafe extern "C" fn eventfd_read(fd: i32, value: *mut u64) -> i32 {
262 let n = crate::read(fd, value as *mut core::ffi::c_void, 8);
263 if n == 8 { 0 } else { -1 }
264}
265
266#[no_mangle]
267pub unsafe extern "C" fn eventfd_write(fd: i32, value: u64) -> i32 {
268 let n = crate::write(fd, &value as *const u64 as *const core::ffi::c_void, 8);
269 if n == 8 { 0 } else { -1 }
270}
271
272#[no_mangle]
273pub unsafe extern "C" fn signalfd(fd: i32, mask: *const u64, flags: i32) -> i32 {
274 syscall_ret(<Arch as Syscalls>::syscall4(SYS_SIGNALFD4, fd as i64, mask as i64, 8, flags as i64)) as i32
275}
276
277#[no_mangle]
278pub unsafe extern "C" fn timerfd_create(clockid: i32, flags: i32) -> i32 {
279 syscall_ret(<Arch as Syscalls>::syscall2(SYS_TIMERFD_CREATE, clockid as i64, flags as i64)) as i32
280}
281
282#[no_mangle]
283pub unsafe extern "C" fn timerfd_settime(fd: i32, flags: i32, new_value: *const core::ffi::c_void, old_value: *mut core::ffi::c_void) -> i32 {
284 syscall_ret(<Arch as Syscalls>::syscall4(SYS_TIMERFD_SETTIME, fd as i64, flags as i64, new_value as i64, old_value as i64)) as i32
285}
286
287#[no_mangle]
288pub unsafe extern "C" fn timerfd_gettime(fd: i32, curr_value: *mut core::ffi::c_void) -> i32 {
289 syscall_ret(<Arch as Syscalls>::syscall2(SYS_TIMERFD_GETTIME, fd as i64, curr_value as i64)) as i32
290}
291
292#[no_mangle]
297pub unsafe extern "C" fn prctl(option: i32, mut args: ...) -> i32 {
298 let a1 = args.next_arg::<i64>();
299 let a2 = args.next_arg::<i64>();
300 let a3 = args.next_arg::<i64>();
301 let a4 = args.next_arg::<i64>();
302 syscall_ret(<Arch as Syscalls>::syscall5(SYS_PRCTL, option as i64, a1, a2, a3, a4)) as i32
303}
304
305#[no_mangle]
306pub unsafe extern "C" fn quotactl(cmd: i32, special: *const core::ffi::c_char, id: i32, addr: *mut core::ffi::c_char) -> i32 {
307 syscall_ret(<Arch as Syscalls>::syscall4(SYS_QUOTACTL, cmd as i64, special as i64, id as i64, addr as i64)) as i32
308}
309
310#[no_mangle]
311pub unsafe extern "C" fn klogctl(type_: i32, bufp: *mut core::ffi::c_char, len: i32) -> i32 {
312 syscall_ret(<Arch as Syscalls>::syscall3(SYS_SYSLOG, type_ as i64, bufp as i64, len as i64)) as i32
313}
314
315static mut SYSLOG_IDENT: [core::ffi::c_char; 64] = [0; 64];
316static mut SYSLOG_OPTIONS: i32 = 0;
317static mut SYSLOG_FACILITY: i32 = 1;
318const SYSLOG_FACILITY_MASK: i32 = 0x3f8;
319
320#[no_mangle]
321pub unsafe extern "C" fn openlog(ident: *const core::ffi::c_char, option: i32, facility: i32) {
322 SYSLOG_OPTIONS = option;
323 SYSLOG_FACILITY = facility & SYSLOG_FACILITY_MASK;
324 SYSLOG_IDENT[0] = 0;
325 if !ident.is_null() {
326 let mut i = 0;
327 while i < 63 && *ident.add(i) != 0 {
328 SYSLOG_IDENT[i] = *ident.add(i);
329 i += 1;
330 }
331 SYSLOG_IDENT[i] = 0;
332 }
333}
334
335#[repr(C)]
336struct SockAddrUn {
337 sun_family: u16,
338 sun_path: [core::ffi::c_char; 108],
339}
340
341unsafe fn syslog_emit(priority: i32, msg: *const core::ffi::c_char, len: usize) {
342 let mut out = [0u8; 1200];
343 let mut o = 0usize;
344 let pri = (SYSLOG_FACILITY | (priority & 7)) as u32;
345 out[o] = b'<'; o += 1;
346 if pri >= 100 { out[o] = b'0' + (pri / 100) as u8; o += 1; }
347 if pri >= 10 { out[o] = b'0' + ((pri / 10) % 10) as u8; o += 1; }
348 out[o] = b'0' + (pri % 10) as u8; o += 1;
349 out[o] = b'>'; o += 1;
350 let mut i = 0;
351 while i < 63 && SYSLOG_IDENT[i] != 0 && o + 1 < out.len() {
352 out[o] = SYSLOG_IDENT[i] as u8; o += 1; i += 1;
353 }
354 if SYSLOG_OPTIONS & 1 != 0 {
355 out[o] = b'['; o += 1;
356 let mut pid = crate::getpid() as u32;
357 let mut digits = [0u8; 10];
358 let mut d = 0;
359 if pid == 0 { digits[0] = b'0'; d = 1; }
360 while pid > 0 { digits[d] = b'0' + (pid % 10) as u8; pid /= 10; d += 1; }
361 while d > 0 { d -= 1; out[o] = digits[d]; o += 1; }
362 out[o] = b']'; o += 1;
363 }
364 out[o] = b':'; o += 1; out[o] = b' '; o += 1;
365 let copy = core::cmp::min(len, out.len() - o - 1);
366 core::ptr::copy_nonoverlapping(msg as *const u8, out.as_mut_ptr().add(o), copy);
367 o += copy;
368 let fd = crate::socket(AF_UNIX, SOCK_DGRAM, 0);
369 if fd >= 0 {
370 let mut sa: SockAddrUn = core::mem::zeroed();
371 sa.sun_family = AF_UNIX as u16;
372 let path = b"/dev/log";
373 for i in 0..path.len() { sa.sun_path[i] = path[i] as core::ffi::c_char; }
374 let r = crate::sendto(fd, out.as_ptr() as *const core::ffi::c_void, o, 0, &sa as *const SockAddrUn as *const crate::sockaddr, core::mem::size_of::<SockAddrUn>() as u32);
375 crate::close(fd);
376 if r >= 0 { return; }
377 }
378 crate::write(2, out.as_ptr() as *const core::ffi::c_void, o);
379 crate::write(2, b"\n".as_ptr() as *const core::ffi::c_void, 1);
380}
381
382#[no_mangle]
383pub unsafe extern "C" fn vsyslog(priority: c_int, fmt: *const c_char, ap: VaList) {
384 let mut msg = [0 as c_char; 1024];
385 let n = vsnprintf(msg.as_mut_ptr(), msg.len(), fmt, ap);
386 if n > 0 { syslog_emit(priority, msg.as_ptr(), core::cmp::min(n as usize, msg.len() - 1)); }
387}
388
389#[no_mangle]
390pub unsafe extern "C" fn syslog(priority: c_int, fmt: *const c_char, args: ...) {
391 vsyslog(priority, fmt, args);
392}
393
394#[no_mangle]
395pub unsafe extern "C" fn closelog() {
396 }
398
399#[no_mangle]
400pub unsafe extern "C" fn setlogmask(mask: i32) -> i32 {
401 static mut LOG_MASK: i32 = 0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80;
402 let old = LOG_MASK;
403 if mask != 0 { LOG_MASK = mask; }
404 old
405}
406
407#[no_mangle]
408pub unsafe extern "C" fn acct(filename: *const core::ffi::c_char) -> i32 {
409 syscall_ret(<Arch as Syscalls>::syscall1(SYS_ACCT, filename as i64)) as i32
410}
411
412#[no_mangle]
413pub unsafe extern "C" fn swapon(path: *const core::ffi::c_char, flags: i32) -> i32 {
414 syscall_ret(<Arch as Syscalls>::syscall2(SYS_SWAPON, path as i64, flags as i64)) as i32
415}
416
417#[no_mangle]
418pub unsafe extern "C" fn swapoff(path: *const core::ffi::c_char) -> i32 {
419 syscall_ret(<Arch as Syscalls>::syscall1(SYS_SWAPOFF, path as i64)) as i32
420}
421
422#[no_mangle]
423pub unsafe extern "C" fn syncfs(fd: i32) -> i32 {
424 syscall_ret(<Arch as Syscalls>::syscall1(SYS_SYNCFS, fd as i64)) as i32
425}
426
427#[no_mangle]
428pub unsafe extern "C" fn sync_file_range(fd: i32, offset: i64, nbytes: u64, flags: u32) -> i32 {
429 #[cfg(target_arch = "powerpc64")]
430 { syscall_ret(<Arch as Syscalls>::syscall4(SYS_SYNC_FILE_RANGE2, fd as i64, flags as i64, offset, nbytes as i64)) as i32 }
431 #[cfg(not(target_arch = "powerpc64"))]
432 { syscall_ret(<Arch as Syscalls>::syscall4(SYS_SYNC_FILE_RANGE, fd as i64, offset, nbytes as i64, flags as i64)) as i32 }
433}
434
435#[no_mangle]
436pub unsafe extern "C" fn posix_fadvise(fd: i32, offset: i64, len: u64, advice: i32) -> i32 {
437 let r = <Arch as Syscalls>::syscall4(SYS_FADVISE64, fd as i64, offset, len as i64, advice as i64);
439 if r < 0 { (-r) as i32 } else { 0 }
440}
441
442#[no_mangle]
443pub unsafe extern "C" fn posix_fallocate(fd: i32, offset: i64, len: u64) -> i32 {
444 let r = <Arch as Syscalls>::syscall3(SYS_FALLOCATE, fd as i64, offset, len as i64);
445 if r < 0 { (-r) as i32 } else { 0 }
446}
447
448#[no_mangle]
449pub unsafe extern "C" fn readahead(fd: i32, offset: i64, count: usize) -> isize {
450 syscall_ret(<Arch as Syscalls>::syscall3(SYS_READAHEAD, fd as i64, offset, count as i64)) as isize
453}
454
455#[no_mangle]
456pub unsafe extern "C" fn fallocate(fd: i32, mode: i32, offset: i64, len: u64) -> i32 {
457 syscall_ret(<Arch as Syscalls>::syscall4(SYS_FALLOCATE, fd as i64, mode as i64, offset, len as i64)) as i32
458}
459
460#[no_mangle]
465pub unsafe extern "C" fn tcgetattr(fd: i32, termios_p: *mut core::ffi::c_void) -> i32 {
466 syscall_ret(<Arch as Syscalls>::syscall3(SYS_IOCTL, fd as i64, 0x5401, termios_p as i64)) as i32
467}
468
469#[no_mangle]
470pub unsafe extern "C" fn tcsetattr(fd: i32, optional_actions: i32, termios_p: *const core::ffi::c_void) -> i32 {
471 let cmd = match optional_actions {
472 0 => 0x5402,
473 1 => 0x5403,
474 2 => 0x5404,
475 _ => 0x5402,
476 };
477 syscall_ret(<Arch as Syscalls>::syscall3(SYS_IOCTL, fd as i64, cmd, termios_p as i64)) as i32
478}
479
480#[no_mangle]
481pub unsafe extern "C" fn tcdrain(fd: i32) -> i32 {
482 syscall_ret(<Arch as Syscalls>::syscall3(SYS_IOCTL, fd as i64, 0x5409, 1)) as i32
483}
484
485#[no_mangle]
486pub unsafe extern "C" fn tcflush(fd: i32, queue_selector: i32) -> i32 {
487 syscall_ret(<Arch as Syscalls>::syscall3(SYS_IOCTL, fd as i64, 0x540B, queue_selector as i64)) as i32
488}
489
490#[no_mangle]
491pub unsafe extern "C" fn tcflow(fd: i32, action: i32) -> i32 {
492 syscall_ret(<Arch as Syscalls>::syscall3(SYS_IOCTL, fd as i64, 0x540A, action as i64)) as i32
493}
494
495#[no_mangle]
496pub unsafe extern "C" fn tcsendbreak(fd: i32, duration: i32) -> i32 {
497 syscall_ret(<Arch as Syscalls>::syscall3(SYS_IOCTL, fd as i64, 0x5425, duration as i64)) as i32
498}
499
500#[no_mangle]
501pub unsafe extern "C" fn tcgetsid(fd: i32) -> i32 {
502 let mut sid: i32 = 0;
503 let r = <Arch as Syscalls>::syscall3(SYS_IOCTL, fd as i64, 0x5429, &mut sid as *mut i32 as i64);
504 if r < 0 { -1 } else { sid }
505}
506
507#[no_mangle]
508pub unsafe extern "C" fn tcgetpgrp(fd: i32) -> i32 {
509 let mut pgrp: i32 = 0;
510 let r = <Arch as Syscalls>::syscall3(SYS_IOCTL, fd as i64, 0x540F, &mut pgrp as *mut i32 as i64);
511 if r < 0 { -1 } else { pgrp }
512}
513
514#[no_mangle]
515pub unsafe extern "C" fn tcsetpgrp(fd: i32, pgrp: i32) -> i32 {
516 syscall_ret(<Arch as Syscalls>::syscall3(SYS_IOCTL, fd as i64, 0x5410, &pgrp as *const i32 as i64)) as i32
517}
518
519#[no_mangle]
530pub unsafe extern "C" fn mq_open(name: *const core::ffi::c_char, oflag: i32, mut args: ...) -> i32 {
531 if name.is_null() { ERRNO!() = EINVAL; return -1; }
532 let mut p = name as *const u8;
533 if *p == b'/' { p = p.add(1); }
534 let mut mode: u32 = 0;
535 let mut attr: *const core::ffi::c_void = core::ptr::null();
536 if oflag & 0o100 != 0 {
537 mode = args.next_arg::<u32>();
538 attr = args.next_arg::<*const core::ffi::c_void>();
539 }
540 syscall_ret(<Arch as Syscalls>::syscall4(
541 SYS_MQ_OPEN,
542 p as i64,
543 oflag as i64,
544 mode as i64,
545 attr as i64,
546 )) as i32
547}
548
549#[no_mangle]
550pub unsafe extern "C" fn mq_close(mqdes: i32) -> i32 {
551 crate::close(mqdes as i32)
552}
553
554#[no_mangle]
555pub unsafe extern "C" fn mq_unlink(name: *const core::ffi::c_char) -> i32 {
556 if name.is_null() { ERRNO!() = EINVAL; return -1; }
557 let mut p = name as *const u8;
558 if *p == b'/' { p = p.add(1); }
559 let r = <Arch as Syscalls>::syscall1(SYS_MQ_UNLINK, p as i64);
560 if r < 0 {
561 let e = if (-r) as c_int == 1 { 13 } else { (-r) as c_int };
563 ERRNO!() = e;
564 -1
565 } else {
566 0
567 }
568}
569
570#[no_mangle]
571pub unsafe extern "C" fn mq_send(mqdes: i32, msg_ptr: *const core::ffi::c_char, msg_len: usize, msg_prio: u32) -> i32 {
572 mq_timedsend(mqdes, msg_ptr, msg_len, msg_prio, core::ptr::null())
573}
574
575#[no_mangle]
576pub unsafe extern "C" fn mq_receive(mqdes: i32, msg_ptr: *mut core::ffi::c_char, msg_len: usize, msg_prio: *mut u32) -> isize {
577 mq_timedreceive(mqdes, msg_ptr, msg_len, msg_prio, core::ptr::null())
578}
579
580#[no_mangle]
581pub unsafe extern "C" fn mq_timedsend(
582 mqdes: i32,
583 msg_ptr: *const core::ffi::c_char,
584 msg_len: usize,
585 msg_prio: u32,
586 abs_timeout: *const core::ffi::c_void,
587) -> i32 {
588 syscall_ret(<Arch as Syscalls>::syscall5(
589 SYS_MQ_TIMEDSEND,
590 mqdes as i64,
591 msg_ptr as i64,
592 msg_len as i64,
593 msg_prio as i64,
594 abs_timeout as i64,
595 )) as i32
596}
597
598#[no_mangle]
599pub unsafe extern "C" fn mq_timedreceive(
600 mqdes: i32,
601 msg_ptr: *mut core::ffi::c_char,
602 msg_len: usize,
603 msg_prio: *mut u32,
604 abs_timeout: *const core::ffi::c_void,
605) -> isize {
606 syscall_ret(<Arch as Syscalls>::syscall5(
607 SYS_MQ_TIMEDRECEIVE,
608 mqdes as i64,
609 msg_ptr as i64,
610 msg_len as i64,
611 msg_prio as i64,
612 abs_timeout as i64,
613 )) as isize
614}
615
616#[repr(C)]
617pub struct SigEventFull {
618 sigev_value: usize,
619 sigev_signo: i32,
620 sigev_notify: i32,
621 sigev_function: usize,
622 sigev_attributes: usize,
623 sigev_pad: [u8; 32],
624}
625
626struct MqNotifyArgs {
627 sem: sem_t,
628 sock: i32,
629 mqd: i32,
630 err: i32,
631 func: usize,
632 value: usize,
633}
634
635unsafe extern "C" fn mq_notify_thread(arg: *mut c_void) -> *mut c_void {
636 let a = &mut *(arg as *mut MqNotifyArgs);
637 let sock = a.sock;
638 let mqd = a.mqd;
639 let func = a.func;
640 let value = a.value;
641 let zeros = [0u8; 32];
642 let sev2 = SigEventFull {
643 sigev_value: zeros.as_ptr() as usize,
644 sigev_signo: sock,
645 sigev_notify: 2, sigev_function: 0,
647 sigev_attributes: 0,
648 sigev_pad: [0; 32],
649 };
650 let r = <Arch as Syscalls>::syscall2(SYS_MQ_NOTIFY, mqd as i64, &sev2 as *const SigEventFull as i64);
651 a.err = if r < 0 { (-r) as i32 } else { 0 };
652 sem_post(&mut a.sem);
653 if a.err != 0 { return core::ptr::null_mut(); }
654 pthread_detach(pthread_self());
655 let mut buf = [0u8; 32];
656 let n = recv(sock, buf.as_mut_ptr() as *mut c_void, buf.len(), 0x4000 | 0x100);
657 crate::close(sock);
658 if n as usize == buf.len() && buf[buf.len() - 1] == 1 && func != 0 {
659 let f: extern "C" fn(usize) = core::mem::transmute(func);
660 f(value);
661 }
662 core::ptr::null_mut()
663}
664
665#[no_mangle]
666pub unsafe extern "C" fn mq_notify(mqdes: i32, sev: *const core::ffi::c_void) -> i32 {
667 if sev.is_null() {
668 return syscall_ret(<Arch as Syscalls>::syscall2(SYS_MQ_NOTIFY, mqdes as i64, 0)) as i32;
669 }
670 let ev = &*(sev as *const SigEventFull);
671 if ev.sigev_notify != 2 {
672 return syscall_ret(<Arch as Syscalls>::syscall2(SYS_MQ_NOTIFY, mqdes as i64, sev as i64)) as i32;
673 }
674
675 let sock = socket(16 , 3 | 0x80000 , 0);
678 if sock < 0 { return -1; }
679 let mut a = MqNotifyArgs {
680 sem: core::mem::zeroed(),
681 sock,
682 mqd: mqdes,
683 err: 0,
684 func: ev.sigev_function,
685 value: ev.sigev_value,
686 };
687 sem_init(&mut a.sem, 0, 0);
688
689 let mut attr: pthread_attr_t = core::mem::zeroed();
690 if ev.sigev_attributes != 0 {
691 attr = core::ptr::read(ev.sigev_attributes as *const pthread_attr_t);
692 } else {
693 pthread_attr_init(&mut attr);
694 }
695 pthread_attr_setdetachstate(&mut attr, PTHREAD_CREATE_JOINABLE);
696
697 let mut all: SigSetT = 0;
698 sigfillset(&mut all);
699 let mut orig: SigSetT = 0;
700 pthread_sigmask(0 , &all, &mut orig);
701
702 let mut td: PthreadT = 0;
703 let r = pthread_create(&mut td, &attr, mq_notify_thread as *const () as usize, &mut a as *mut MqNotifyArgs as *mut c_void);
704 pthread_sigmask(2 , &orig, core::ptr::null_mut());
705 if r != 0 {
706 crate::close(sock);
707 ERRNO!() = EAGAIN;
708 return -1;
709 }
710 sem_wait(&mut a.sem);
711 sem_destroy(&mut a.sem);
712 if a.err != 0 {
713 crate::close(sock);
714 pthread_join(td, core::ptr::null_mut());
715 ERRNO!() = a.err;
716 return -1;
717 }
718 0
719}
720
721#[no_mangle]
722pub unsafe extern "C" fn mq_getattr(mqdes: i32, attr: *mut core::ffi::c_void) -> i32 {
723 mq_setattr(mqdes, core::ptr::null(), attr)
724}
725
726#[no_mangle]
727pub unsafe extern "C" fn mq_setattr(
728 mqdes: i32,
729 new: *const core::ffi::c_void,
730 old: *mut core::ffi::c_void,
731) -> i32 {
732 syscall_ret(<Arch as Syscalls>::syscall3(
733 SYS_MQ_GETSETATTR,
734 mqdes as i64,
735 new as i64,
736 old as i64,
737 )) as i32
738}
739
740#[no_mangle]
745pub unsafe extern "C" fn pthread_getaffinity_np(thread: usize, cpusetsize: usize, cpuset: *mut u8) -> i32 {
746 sched_getaffinity(thread as i32, cpusetsize, cpuset)
747}
748
749#[no_mangle]
750pub unsafe extern "C" fn pthread_setaffinity_np(thread: usize, cpusetsize: usize, cpuset: *const u8) -> i32 {
751 sched_setaffinity(thread as i32, cpusetsize, cpuset)
752}
753
754#[no_mangle]
759pub unsafe extern "C" fn sched_yield() -> i32 {
760 syscall_ret(<Arch as Syscalls>::syscall0(SYS_SCHED_YIELD)) as i32
761}
762
763#[no_mangle]
764pub unsafe extern "C" fn sched_get_priority_max(policy: i32) -> i32 {
765 syscall_ret(<Arch as Syscalls>::syscall1(SYS_SCHED_GET_PRIORITY_MAX, policy as i64)) as i32
766}
767
768#[no_mangle]
769pub unsafe extern "C" fn sched_get_priority_min(policy: i32) -> i32 {
770 syscall_ret(<Arch as Syscalls>::syscall1(SYS_SCHED_GET_PRIORITY_MIN, policy as i64)) as i32
771}
772
773#[no_mangle]
774pub unsafe extern "C" fn sched_getparam(pid: i32, param: *mut core::ffi::c_void) -> i32 {
775 syscall_ret(<Arch as Syscalls>::syscall2(SYS_SCHED_GETPARAM, pid as i64, param as i64)) as i32
776}
777
778#[no_mangle]
779pub unsafe extern "C" fn sched_setparam(pid: i32, param: *const core::ffi::c_void) -> i32 {
780 syscall_ret(<Arch as Syscalls>::syscall2(SYS_SCHED_SETPARAM, pid as i64, param as i64)) as i32
781}
782
783#[no_mangle]
784pub unsafe extern "C" fn sched_getscheduler(pid: i32) -> i32 {
785 syscall_ret(<Arch as Syscalls>::syscall1(SYS_SCHED_GETSCHEDULER, pid as i64)) as i32
786}
787
788#[no_mangle]
789pub unsafe extern "C" fn sched_setscheduler(pid: i32, policy: i32, param: *const core::ffi::c_void) -> i32 {
790 syscall_ret(<Arch as Syscalls>::syscall3(SYS_SCHED_SETSCHEDULER, pid as i64, policy as i64, param as i64)) as i32
791}
792
793#[no_mangle]
794pub unsafe extern "C" fn sched_setaffinity(pid: i32, cpusetsize: usize, cpuset: *const u8) -> i32 {
795 syscall_ret(<Arch as Syscalls>::syscall3(SYS_SCHED_SETAFFINITY, pid as i64, cpusetsize as i64, cpuset as i64)) as i32
796}
797
798#[no_mangle]
799pub unsafe extern "C" fn sched_getaffinity(pid: i32, cpusetsize: usize, cpuset: *mut u8) -> i32 {
800 let r = <Arch as Syscalls>::syscall3(SYS_SCHED_GETAFFINITY, pid as i64, cpusetsize as i64, cpuset as i64);
801 if r < 0 {
802 ERRNO!() = (-r) as c_int;
803 return -1;
804 }
805 0
807}
808
809#[cold]
810unsafe fn sched_getcpu_syscall(cpu: *mut u32) -> i32 {
811 let r = <Arch as Syscalls>::syscall3(SYS_GETCPU, cpu as i64, 0, 0);
812 if r == 0 {
813 core::ptr::read_unaligned(cpu) as i32
814 } else {
815 syscall_ret(r) as i32
816 }
817}
818
819#[no_mangle]
820pub unsafe extern "C" fn sched_getcpu() -> i32 {
821 let mut cpu = core::mem::MaybeUninit::<[u64; 2]>::uninit();
826 let cpu_ptr = cpu.as_mut_ptr() as *mut u32;
827 if vdso_getcpu(cpu_ptr) {
828 return core::ptr::read_unaligned(cpu_ptr) as i32;
829 }
830 sched_getcpu_syscall(cpu_ptr)
831}
832
833#[no_mangle]
834pub unsafe extern "C" fn sched_rr_get_interval(pid: i32, tp: *mut core::ffi::c_void) -> i32 {
835 syscall_ret(<Arch as Syscalls>::syscall2(SYS_SCHED_RR_GET_INTERVAL, pid as i64, tp as i64)) as i32
836}
837