1#[repr(C)]
13pub struct Aiocb {
14 pub aio_fildes: c_int,
15 pub aio_lio_opcode: c_int,
16 pub aio_reqprio: c_int,
17 __pad: c_int,
18 pub aio_buf: *mut c_void,
19 pub aio_nbytes: usize,
20 pub aio_sigevent: [u8; 64],
21 pub aio_offset: i64,
22}
23
24const _: [(); 104] = [(); core::mem::size_of::<Aiocb>()];
25
26#[derive(Copy, Clone)]
27struct AioSlot {
28 cb: *mut Aiocb,
29 ret: isize,
30 err: c_int,
31 active: bool,
32 next: *mut AioSlot,
33 prev: *mut AioSlot,
34}
35
36static mut AIO_HEAD: *mut AioSlot = core::ptr::null_mut();
39static mut AIO_FREE: *mut AioSlot = core::ptr::null_mut();
40static AIO_LOCK: AtomicI32 = AtomicI32::new(0);
41
42unsafe fn aio_lock() {
43 while AIO_LOCK
44 .compare_exchange_weak(0, 1, Ordering::Acquire, Ordering::Relaxed)
45 .is_err()
46 {
47 core::hint::spin_loop();
48 }
49}
50
51unsafe fn aio_unlock() {
52 AIO_LOCK.store(0, Ordering::Release);
53}
54
55unsafe fn aio_alloc() -> *mut AioSlot {
56 let slot = if AIO_FREE.is_null() {
57 crate::malloc(core::mem::size_of::<AioSlot>()) as *mut AioSlot
58 } else {
59 let slot = AIO_FREE;
60 AIO_FREE = (*slot).next;
61 slot
62 };
63 if !slot.is_null() {
64 (*slot).cb = core::ptr::null_mut();
65 (*slot).ret = 0;
66 (*slot).err = 0;
67 (*slot).active = false;
68 (*slot).next = core::ptr::null_mut();
69 (*slot).prev = core::ptr::null_mut();
70 }
71 slot
72}
73
74unsafe fn aio_get(cb: *mut Aiocb) -> *mut AioSlot {
75 let mut slot = AIO_HEAD;
76 while !slot.is_null() {
77 if (*slot).cb == cb {
78 return slot;
79 }
80 slot = (*slot).next;
81 }
82 let slot = aio_alloc();
83 if slot.is_null() {
84 return core::ptr::null_mut();
85 }
86 (*slot).cb = cb;
87 (*slot).next = AIO_HEAD;
88 if !AIO_HEAD.is_null() {
89 (*AIO_HEAD).prev = slot;
90 }
91 AIO_HEAD = slot;
92 slot
93}
94
95unsafe fn aio_find(cb: *const Aiocb) -> *mut AioSlot {
96 let mut slot = AIO_HEAD;
97 while !slot.is_null() {
98 if (*slot).cb == cb as *mut Aiocb && (*slot).active {
99 return slot;
100 }
101 slot = (*slot).next;
102 }
103 core::ptr::null_mut()
104}
105
106unsafe fn aio_unlink(slot: *mut AioSlot) {
107 let prev = (*slot).prev;
108 let next = (*slot).next;
109 if prev.is_null() {
110 AIO_HEAD = next;
111 } else {
112 (*prev).next = next;
113 }
114 if !next.is_null() {
115 (*next).prev = prev;
116 }
117 (*slot).prev = core::ptr::null_mut();
118 (*slot).next = core::ptr::null_mut();
119}
120
121unsafe fn aio_finish(cb: *mut Aiocb, ret: isize, err: c_int) -> bool {
122 aio_lock();
123 let slot = aio_get(cb);
124 if slot.is_null() {
125 aio_unlock();
126 return false;
127 }
128 (*slot).ret = ret;
129 (*slot).err = err;
130 (*slot).active = true;
131 aio_unlock();
132 true
133}
134
135#[no_mangle]
136pub unsafe extern "C" fn aio_read(aiocbp: *mut Aiocb) -> c_int {
137 if aiocbp.is_null() { ERRNO!() = EINVAL; return -1; }
138 let r = crate::pread(
139 (*aiocbp).aio_fildes,
140 (*aiocbp).aio_buf,
141 (*aiocbp).aio_nbytes,
142 (*aiocbp).aio_offset,
143 );
144 let ok = if r < 0 {
145 aio_finish(aiocbp, -1, ERRNO!())
146 } else {
147 aio_finish(aiocbp, r, 0)
148 };
149 if !ok { ERRNO!() = EAGAIN; return -1; }
150 0
151}
152
153#[no_mangle]
154pub unsafe extern "C" fn aio_write(aiocbp: *mut Aiocb) -> c_int {
155 if aiocbp.is_null() { ERRNO!() = EINVAL; return -1; }
156 let r = crate::pwrite(
157 (*aiocbp).aio_fildes,
158 (*aiocbp).aio_buf as *const u8,
159 (*aiocbp).aio_nbytes,
160 (*aiocbp).aio_offset,
161 );
162 let ok = if r < 0 {
163 aio_finish(aiocbp, -1, ERRNO!())
164 } else {
165 aio_finish(aiocbp, r, 0)
166 };
167 if !ok { ERRNO!() = EAGAIN; return -1; }
168 0
169}
170
171const O_DSYNC_V: c_int = 0o10000;
172const O_SYNC_V: c_int = 0o4010000;
173const AIO_ALLDONE_V: c_int = 2;
174const LIO_READ_V: c_int = 0;
175const LIO_WRITE_V: c_int = 1;
176const LIO_NOP_V: c_int = 2;
177
178#[no_mangle]
179pub unsafe extern "C" fn aio_fsync(op: c_int, aiocbp: *mut Aiocb) -> c_int {
180 if aiocbp.is_null() { ERRNO!() = EINVAL; return -1; }
181 let r = if op == O_DSYNC_V {
182 crate::fdatasync((*aiocbp).aio_fildes)
183 } else if op == O_SYNC_V {
184 crate::fsync((*aiocbp).aio_fildes)
185 } else {
186 ERRNO!() = EINVAL;
187 return -1;
188 };
189 let ok = if r < 0 {
190 aio_finish(aiocbp, -1, ERRNO!())
191 } else {
192 aio_finish(aiocbp, 0, 0)
193 };
194 if !ok { ERRNO!() = EAGAIN; return -1; }
195 0
196}
197
198#[no_mangle]
199pub unsafe extern "C" fn aio_error(aiocbp: *const Aiocb) -> c_int {
200 if aiocbp.is_null() { return EINVAL; }
201 aio_lock();
202 let slot = aio_find(aiocbp);
203 let err = if slot.is_null() { EINVAL } else { (*slot).err };
204 aio_unlock();
205 err
206}
207
208#[no_mangle]
209pub unsafe extern "C" fn aio_return(aiocbp: *mut Aiocb) -> isize {
210 if aiocbp.is_null() { ERRNO!() = EINVAL; return -1; }
211 aio_lock();
212 let slot = aio_find(aiocbp);
213 if slot.is_null() {
214 aio_unlock();
215 ERRNO!() = EINVAL;
216 return -1;
217 }
218 let ret = (*slot).ret;
219 (*slot).active = false;
220 (*slot).cb = core::ptr::null_mut();
221 (*slot).ret = 0;
222 (*slot).err = 0;
223 aio_unlink(slot);
224 (*slot).next = AIO_FREE;
225 AIO_FREE = slot;
226 aio_unlock();
227 ret
228}
229
230#[no_mangle]
231pub unsafe extern "C" fn aio_cancel(_fd: c_int, _aiocbp: *mut Aiocb) -> c_int {
232 AIO_ALLDONE_V
235}
236
237#[no_mangle]
238pub unsafe extern "C" fn aio_suspend(
239 aiocb_list: *const *const Aiocb,
240 nent: c_int,
241 _timeout: *const timespec,
242) -> c_int {
243 if nent <= 0 || aiocb_list.is_null() { ERRNO!() = EINVAL; return -1; }
244 aio_lock();
246 for i in 0..nent as usize {
247 let cb = *aiocb_list.add(i);
248 if cb.is_null() { continue; }
249 if aio_find(cb).is_null() {
250 aio_unlock();
251 ERRNO!() = EINVAL;
252 return -1;
253 }
254 }
255 aio_unlock();
256 0
257}
258
259#[repr(C)]
260#[derive(Copy, Clone)]
261pub struct SigEvent {
262 pub sigev_value: usize,
263 pub sigev_signo: c_int,
264 pub sigev_notify: c_int,
265 pub sigev_un: [u8; 48],
266}
267
268const SIGEV_SIGNAL_V: c_int = 0;
269const SIGEV_NONE_V: c_int = 1;
270const SIGEV_THREAD_V: c_int = 2;
271
272unsafe fn aio_notify(sev: *const SigEvent) {
273 if sev.is_null() { return; }
274 match (*sev).sigev_notify {
275 SIGEV_SIGNAL_V => {
276 crate::sigqueue(crate::getpid(), (*sev).sigev_signo, (*sev).sigev_value as *mut c_void);
277 }
278 SIGEV_THREAD_V => {
279 let fnptr = core::ptr::read_unaligned((*sev).sigev_un.as_ptr().add(0) as *const usize);
280 if fnptr != 0 {
281 let f: unsafe extern "C" fn(usize) = core::mem::transmute(fnptr);
282 f((*sev).sigev_value);
283 }
284 }
285 _ => {}
286 }
287}
288
289#[no_mangle]
290pub unsafe extern "C" fn lio_listio(
291 mode: c_int,
292 aiocb_list: *const *mut Aiocb,
293 nent: c_int,
294 sevp: *mut SigEvent,
295) -> c_int {
296 if nent < 0 || aiocb_list.is_null() { ERRNO!() = EINVAL; return -1; }
297 let mut ok = 0;
298 for i in 0..nent as usize {
299 let cb = *aiocb_list.add(i);
300 if cb.is_null() { continue; }
301 let r = match (*cb).aio_lio_opcode {
302 LIO_READ_V => aio_read(cb),
303 LIO_WRITE_V => aio_write(cb),
304 LIO_NOP_V => 0,
305 _ => {
306 ERRNO!() = EINVAL;
307 return -1;
308 }
309 };
310 if r == 0 { ok += 1; }
311 }
312 if mode == 1 && !sevp.is_null() {
313 aio_notify(sevp);
314 }
315 if ok == 0 && nent > 0 { -1 } else { 0 }
316}
317
318static mut PTHREAD_DEFAULT_STACK: usize = STACK_SIZE;
323static mut PTHREAD_DEFAULT_GUARD: usize = 4096;
324static mut PTHREAD_CONCURRENCY: c_int = 0;
325
326unsafe fn mapping_containing(addr: usize) -> Option<(usize, usize)> {
330 let fd = crate::open(b"/proc/self/maps\0".as_ptr() as *const core::ffi::c_char, 0, 0);
331 if fd < 0 {
332 return None;
333 }
334 let size: i64 = crate::lseek(fd, 0, 2);
335 let want: i64 = if size > 16384 { 16384 } else { size };
336 let start = if size > want { size - want } else { 0 };
337 if crate::lseek(fd, start, 0) < 0 {
338 crate::close(fd);
339 return None;
340 }
341 let mut buf = [0u8; 16384];
342 let mut len = 0usize;
343 while len < buf.len() {
344 let n = crate::read(
345 fd,
346 buf.as_mut_ptr().add(len) as *mut core::ffi::c_void,
347 buf.len() - len,
348 );
349 if n <= 0 {
350 break;
351 }
352 len += n as usize;
353 }
354 crate::close(fd);
355
356 let mut result = None;
357 let mut line_start = 0usize;
358 while line_start < len {
359 let mut line_end = line_start;
360 while line_end < len && buf[line_end] != b'\n' {
361 line_end += 1;
362 }
363 let line = &buf[line_start..line_end];
364 if let Some(dash) = line.iter().position(|&c| c == b'-') {
365 let mut lo = 0usize;
366 let mut ok = true;
367 for &c in &line[..dash] {
368 if c.is_ascii_digit() {
369 lo = lo * 16 + (c - b'0') as usize;
370 } else if (b'a'..=b'f').contains(&c) {
371 lo = lo * 16 + (c - b'a' + 10) as usize;
372 } else {
373 ok = false;
374 break;
375 }
376 }
377 if ok && !line[..dash].is_empty() {
378 let mut hi = 0usize;
379 let mut i = dash + 1;
380 while i < line.len() {
381 let c = line[i];
382 if c.is_ascii_digit() {
383 hi = hi * 16 + (c - b'0') as usize;
384 } else if (b'a'..=b'f').contains(&c) {
385 hi = hi * 16 + (c - b'a' + 10) as usize;
386 } else {
387 break;
388 }
389 i += 1;
390 }
391 if i > dash + 1 && lo <= addr && addr < hi {
392 result = Some((lo, hi - lo));
393 }
394 }
395 }
396 line_start = line_end + 1;
397 }
398 result
399}
400
401#[no_mangle]
402pub unsafe extern "C" fn pthread_getattr_np(thread: PthreadT, attr: *mut pthread_attr_t) -> c_int {
403 if attr.is_null() || thread == 0 { return EINVAL; }
404 let slot = thread as *mut Thread;
405 core::ptr::write_bytes(attr, 0, 1);
406 let mut stack_base = (*slot).stack as usize;
407 let mut stack_size = if (*slot).stack_size > 0 { (*slot).stack_size } else { PTHREAD_DEFAULT_STACK };
408 if stack_base == 0 {
409 let sp: usize;
413 #[cfg(target_arch = "x86_64")]
414 core::arch::asm!("mov {}, rsp", out(reg) sp, options(nomem, nostack));
415 #[cfg(target_arch = "aarch64")]
416 core::arch::asm!("mov {}, sp", out(reg) sp, options(nomem, nostack));
417 #[cfg(target_arch = "riscv64")]
418 core::arch::asm!("mv {}, sp", out(reg) sp, options(nomem, nostack));
419 #[cfg(target_arch = "powerpc64")]
420 core::arch::asm!("mr {}, 1", out(reg) sp, options(nomem, nostack));
421 #[cfg(target_arch = "loongarch64")]
422 core::arch::asm!("move {}, $sp", out(reg) sp, options(nomem, nostack));
423 if let Some((base, size)) = mapping_containing(sp) {
424 stack_base = base;
425 stack_size = size;
426 } else {
427 stack_base = sp & !0xfff;
428 stack_size = 4096;
429 }
430 }
431 *((*attr).__i.as_mut_ptr() as *mut usize) = stack_size;
432 *((*attr).__i.as_mut_ptr().add(2) as *mut usize) = PTHREAD_DEFAULT_GUARD;
433 *((*attr).__i.as_mut_ptr().add(4) as *mut *mut c_void) =
434 (stack_base + stack_size) as *mut c_void;
435 (*attr).__i[6] = if (*slot).detach_state == DT_DETACHED { 1 } else { 0 };
436 (*attr).__i[7] = 0;
437 0
438}
439
440#[no_mangle]
441pub unsafe extern "C" fn pthread_getattr_default_np(attr: *mut pthread_attr_t) -> c_int {
442 if attr.is_null() { return EINVAL; }
443 core::ptr::write_bytes(attr, 0, 1);
444 *((*attr).__i.as_mut_ptr() as *mut usize) = PTHREAD_DEFAULT_STACK;
445 *((*attr).__i.as_mut_ptr().add(2) as *mut usize) = PTHREAD_DEFAULT_GUARD;
446 (*attr).__i[6] = 0;
447 (*attr).__i[7] = 1;
448 0
449}
450
451#[no_mangle]
452pub unsafe extern "C" fn pthread_setattr_default_np(attr: *const pthread_attr_t) -> c_int {
453 if attr.is_null() { return EINVAL; }
454 let stack = *((*attr).__i.as_ptr() as *const usize);
455 let guard = *((*attr).__i.as_ptr().add(2) as *const usize);
456 if (*attr).__i[6] != 0 { return EINVAL; }
458 if stack > 0 {
459 if stack < 16384 { return EINVAL; }
460 PTHREAD_DEFAULT_STACK = stack;
461 }
462 if guard > 0 { PTHREAD_DEFAULT_GUARD = guard; }
463 0
464}
465
466#[no_mangle]
467pub unsafe extern "C" fn pthread_getconcurrency() -> c_int {
468 PTHREAD_CONCURRENCY
469}
470
471#[no_mangle]
472pub unsafe extern "C" fn pthread_setconcurrency(level: c_int) -> c_int {
473 if level < 0 { return EINVAL; }
474 PTHREAD_CONCURRENCY = level;
475 0
476}
477
478#[no_mangle]
479pub unsafe extern "C" fn pthread_getcpuclockid(thread: PthreadT, clockid: *mut i32) -> c_int {
480 if thread == 0 || clockid.is_null() { return EINVAL; }
481 let slot = thread as *mut Thread;
482 let tid = (*slot).tid as i64;
483 *clockid = (((-tid - 1) * 8 + 6) & 0xffffffff) as i32;
484 0
485}
486
487#[no_mangle]
488pub unsafe extern "C" fn pthread_setname_np(thread: PthreadT, name: *const c_char) -> c_int {
489 if thread == 0 || name.is_null() { return EINVAL; }
490 if crate::strnlen(name as *const u8, 16) > 15 { return 34; }
491 let slot = thread as *mut Thread;
492 if slot == pthread_self() as *mut Thread {
493 return if crate::prctl(15, name as c_long) < 0 { ERRNO!() } else { 0 };
494 }
495 let mut path = [0 as c_char; 48];
496 let mut o = 0usize;
497 for &c in b"/proc/self/task/" { path[o] = c as c_char; o += 1; }
498 let mut tid = (*slot).tid as u32;
499 let mut digits = [0u8; 10];
500 let mut d = 0usize;
501 if tid == 0 { digits[0] = b'0'; d = 1; }
502 while tid > 0 { digits[d] = b'0' + (tid % 10) as u8; tid /= 10; d += 1; }
503 while d > 0 { d -= 1; path[o] = digits[d] as c_char; o += 1; }
504 for &c in b"/comm\0" { path[o] = c as c_char; o += 1; }
505 let fd = crate::open(path.as_ptr(), 1, 0);
506 if fd < 0 { return ERRNO!(); }
507 let n = crate::strlen(name);
508 let w = crate::write(fd, name as *const c_void, n);
509 crate::close(fd);
510 if w < 0 { ERRNO!() } else { 0 }
511}
512
513#[no_mangle]
514pub unsafe extern "C" fn pthread_getname_np(thread: PthreadT, name: *mut c_char, len: usize) -> c_int {
515 if thread == 0 || name.is_null() { return EINVAL; }
516 if len < 16 { return 34; }
517 let slot = thread as *mut Thread;
518 if slot == pthread_self() as *mut Thread {
519 return if crate::prctl(16, name as c_long) < 0 { ERRNO!() } else { 0 };
520 }
521 let mut path = [0 as c_char; 48];
522 let mut o = 0usize;
523 for &c in b"/proc/self/task/" { path[o] = c as c_char; o += 1; }
524 let mut tid = (*slot).tid as u32;
525 let mut digits = [0u8; 10];
526 let mut d = 0usize;
527 if tid == 0 { digits[0] = b'0'; d = 1; }
528 while tid > 0 { digits[d] = b'0' + (tid % 10) as u8; tid /= 10; d += 1; }
529 while d > 0 { d -= 1; path[o] = digits[d] as c_char; o += 1; }
530 for &c in b"/comm\0" { path[o] = c as c_char; o += 1; }
531 let fd = crate::open(path.as_ptr(), 0, 0);
532 if fd < 0 { return ERRNO!(); }
533 let n = crate::read(fd, name as *mut c_void, len);
534 crate::close(fd);
535 if n < 0 { return ERRNO!(); }
536 let mut l = n as usize;
537 if l > 0 && *name.add(l - 1) == b'\n' as c_char { l -= 1; }
538 *name.add(l) = 0;
539 0
540}
541
542#[no_mangle]
543pub unsafe extern "C" fn pthread_getschedparam(thread: PthreadT, policy: *mut c_int, param: *mut sched_param) -> c_int {
544 if thread == 0 || policy.is_null() || param.is_null() { return EINVAL; }
545 let tid = (*(thread as *mut Thread)).tid;
546 *policy = crate::sched_getscheduler(tid);
547 crate::sched_getparam(tid, param as *mut c_void)
548}
549
550#[no_mangle]
551pub unsafe extern "C" fn pthread_setschedparam(thread: PthreadT, policy: c_int, param: *const sched_param) -> c_int {
552 if thread == 0 || param.is_null() { return EINVAL; }
553 let tid = (*(thread as *mut Thread)).tid;
554 crate::sched_setscheduler(tid, policy, param as *const c_void)
555}
556
557#[no_mangle]
558pub unsafe extern "C" fn pthread_setschedprio(thread: PthreadT, prio: c_int) -> c_int {
559 if thread == 0 { return EINVAL; }
560 let tid = (*(thread as *mut Thread)).tid;
561 let p = sched_param { sched_priority: prio };
562 crate::sched_setparam(tid, &p as *const sched_param as *const c_void)
563}
564
565#[no_mangle]
566pub unsafe extern "C" fn pthread_mutex_getprioceiling(mutex: *const pthread_mutex_t, ceiling: *mut c_int) -> c_int {
567 if mutex.is_null() || ceiling.is_null() { return EINVAL; }
568 *ceiling = 0;
569 0
570}
571
572#[no_mangle]
573pub unsafe extern "C" fn pthread_mutex_setprioceiling(mutex: *mut pthread_mutex_t, ceiling: c_int, old: *mut c_int) -> c_int {
574 if mutex.is_null() { return EINVAL; }
575 if !old.is_null() { *old = 0; }
576 let _ = ceiling;
577 0
578}
579
580#[repr(C)]
586pub struct posix_spawn_file_actions_t {
587 __pad0: [c_int; 2],
588 __actions: *mut c_void,
589 __pad: [c_int; 16],
590}
591
592#[repr(C)]
593#[derive(Copy, Clone)]
594pub struct posix_spawnattr_t {
595 __flags: c_int,
596 __pgrp: c_int,
597 __def: [c_ulong; 16],
598 __mask: [c_ulong; 16],
599 __prio: c_int,
600 __pol: c_int,
601 __fn: *mut c_void,
602 __pad: [u8; 64 - core::mem::size_of::<*mut c_void>()],
603}
604
605#[repr(C)]
606struct SpawnAction {
607 next: *mut SpawnAction,
608 kind: c_int,
609 fd: c_int,
610 newfd: c_int,
611 oflag: c_int,
612 mode: u32,
613 path: *mut c_char,
614 path_owned: bool,
615}
616
617static mut SPAWN_DEFAULT_ATTR: posix_spawnattr_t = posix_spawnattr_t {
618 __flags: 0,
619 __pgrp: 0,
620 __def: [0; 16],
621 __mask: [0; 16],
622 __prio: 0,
623 __pol: 0,
624 __fn: core::ptr::null_mut(),
625 __pad: [0; 64 - core::mem::size_of::<*mut c_void>()],
626};
627
628unsafe fn spawn_actions(fa: *const posix_spawn_file_actions_t) -> *mut SpawnAction {
629 (*fa).__actions as *mut SpawnAction
630}
631
632unsafe fn spawn_action_add(
633 fa: *mut posix_spawn_file_actions_t,
634 kind: c_int,
635 fd: c_int,
636 newfd: c_int,
637 oflag: c_int,
638 mode: u32,
639 path: *const c_char,
640) -> c_int {
641 let node = crate::calloc(1, core::mem::size_of::<SpawnAction>()) as *mut SpawnAction;
642 if node.is_null() { return ENOMEM; }
643 (*node).kind = kind;
644 (*node).fd = fd;
645 (*node).newfd = newfd;
646 (*node).oflag = oflag;
647 (*node).mode = mode;
648 (*node).next = core::ptr::null_mut();
649 if !path.is_null() {
650 let n = crate::strlen(path);
651 let p = crate::malloc(n + 1) as *mut c_char;
652 if p.is_null() { crate::free(node as *mut c_void); return ENOMEM; }
653 core::ptr::copy_nonoverlapping(path, p, n + 1);
654 (*node).path = p;
655 (*node).path_owned = true;
656 }
657 if (*fa).__actions.is_null() {
658 (*fa).__actions = node as *mut c_void;
659 } else {
660 let mut tail = (*fa).__actions as *mut SpawnAction;
661 while !(*tail).next.is_null() { tail = (*tail).next; }
662 (*tail).next = node;
663 }
664 0
665}
666
667#[no_mangle]
668pub unsafe extern "C" fn posix_spawn_file_actions_init(fa: *mut posix_spawn_file_actions_t) -> c_int {
669 if fa.is_null() { return EINVAL; }
670 core::ptr::write_bytes(fa, 0, 1);
671 0
672}
673
674#[no_mangle]
675pub unsafe extern "C" fn posix_spawn_file_actions_destroy(fa: *mut posix_spawn_file_actions_t) -> c_int {
676 if fa.is_null() { return EINVAL; }
677 let mut p = spawn_actions(fa);
678 while !p.is_null() {
679 let next = (*p).next;
680 if (*p).path_owned && !(*p).path.is_null() { crate::free((*p).path as *mut c_void); }
681 crate::free(p as *mut c_void);
682 p = next;
683 }
684 (*fa).__actions = core::ptr::null_mut();
685 0
686}
687
688#[no_mangle]
689pub unsafe extern "C" fn posix_spawn_file_actions_addclose(fa: *mut posix_spawn_file_actions_t, fd: c_int) -> c_int {
690 if fa.is_null() || fd < 0 { return EINVAL; }
691 spawn_action_add(fa, 0, fd, 0, 0, 0, core::ptr::null())
692}
693
694#[no_mangle]
695pub unsafe extern "C" fn posix_spawn_file_actions_adddup2(fa: *mut posix_spawn_file_actions_t, fd: c_int, newfd: c_int) -> c_int {
696 if fa.is_null() || fd < 0 || newfd < 0 { return EINVAL; }
697 spawn_action_add(fa, 1, fd, newfd, 0, 0, core::ptr::null())
698}
699
700#[no_mangle]
701pub unsafe extern "C" fn posix_spawn_file_actions_addopen(
702 fa: *mut posix_spawn_file_actions_t,
703 fd: c_int,
704 path: *const c_char,
705 oflag: c_int,
706 mode: u32,
707) -> c_int {
708 if fa.is_null() || fd < 0 || path.is_null() { return EINVAL; }
709 spawn_action_add(fa, 2, fd, 0, oflag, mode, path)
710}
711
712#[no_mangle]
713pub unsafe extern "C" fn posix_spawn_file_actions_addchdir_np(fa: *mut posix_spawn_file_actions_t, path: *const c_char) -> c_int {
714 if fa.is_null() || path.is_null() { return EINVAL; }
715 spawn_action_add(fa, 3, 0, 0, 0, 0, path)
716}
717
718#[no_mangle]
719pub unsafe extern "C" fn posix_spawn_file_actions_addfchdir_np(fa: *mut posix_spawn_file_actions_t, fd: c_int) -> c_int {
720 if fa.is_null() || fd < 0 { return EINVAL; }
721 spawn_action_add(fa, 4, fd, 0, 0, 0, core::ptr::null())
722}
723
724unsafe fn spawn_apply_actions(fa: *const posix_spawn_file_actions_t) {
725 if fa.is_null() { return; }
726 let mut p = spawn_actions(fa);
727 while !p.is_null() {
728 match (*p).kind {
729 0 => { crate::close((*p).fd); }
730 1 => { crate::dup2((*p).fd, (*p).newfd); }
731 2 => {
732 let fd = crate::open((*p).path, (*p).oflag, (*p).mode as c_int);
733 if fd >= 0 && fd != (*p).fd {
734 crate::dup2(fd, (*p).fd);
735 crate::close(fd);
736 }
737 }
738 3 => { crate::chdir((*p).path); }
739 4 => { crate::fchdir((*p).fd); }
740 _ => {}
741 }
742 p = (*p).next;
743 }
744}
745
746unsafe fn spawn_apply_attr(attr: *const posix_spawnattr_t) {
747 if attr.is_null() { return; }
748 let flags = (*attr).__flags;
749 if flags & 1 != 0 {
750 if crate::geteuid() == 0 {
751 crate::setgid(crate::getgid());
752 crate::setuid(crate::getuid());
753 }
754 }
755 if flags & 2 != 0 { crate::setpgid(0, (*attr).__pgrp); }
756 if flags & 4 != 0 {
757 for sig in 1..65 {
758 let idx = (sig - 1) / 64;
759 let bit = (sig - 1) % 64;
760 if idx < 16 && ((*attr).__def[idx] >> bit) & 1 != 0 {
761 crate::signal(sig as c_int, 0);
762 }
763 }
764 }
765 if flags & 8 != 0 {
766 crate::sigprocmask(2, (*attr).__mask.as_ptr() as *const SigSetT, core::ptr::null_mut());
767 }
768 if flags & 16 != 0 {
769 let p = sched_param { sched_priority: (*attr).__prio };
770 crate::sched_setparam(0, &p as *const sched_param as *const c_void);
771 }
772 if flags & 32 != 0 {
773 let p = sched_param { sched_priority: (*attr).__prio };
774 crate::sched_setscheduler(0, (*attr).__pol, &p as *const sched_param as *const c_void);
775 }
776}
777
778#[no_mangle]
779pub unsafe extern "C" fn posix_spawnattr_init(attr: *mut posix_spawnattr_t) -> c_int {
780 if attr.is_null() { return EINVAL; }
781 *attr = SPAWN_DEFAULT_ATTR;
782 0
783}
784
785#[no_mangle]
786pub unsafe extern "C" fn posix_spawnattr_destroy(attr: *mut posix_spawnattr_t) -> c_int {
787 if attr.is_null() { return EINVAL; }
788 0
789}
790
791#[no_mangle]
792pub unsafe extern "C" fn posix_spawnattr_getflags(attr: *const posix_spawnattr_t, flags: *mut i16) -> c_int {
793 if attr.is_null() || flags.is_null() { return EINVAL; }
794 *flags = (*attr).__flags as i16;
795 0
796}
797
798#[no_mangle]
799pub unsafe extern "C" fn posix_spawnattr_setflags(attr: *mut posix_spawnattr_t, flags: i16) -> c_int {
800 if attr.is_null() { return EINVAL; }
801 if (flags as c_int) & !0x7f != 0 { return EINVAL; }
802 (*attr).__flags = flags as c_int;
803 0
804}
805
806#[no_mangle]
807pub unsafe extern "C" fn posix_spawnattr_getpgroup(attr: *const posix_spawnattr_t, pgroup: *mut c_int) -> c_int {
808 if attr.is_null() || pgroup.is_null() { return EINVAL; }
809 *pgroup = (*attr).__pgrp;
810 0
811}
812
813#[no_mangle]
814pub unsafe extern "C" fn posix_spawnattr_setpgroup(attr: *mut posix_spawnattr_t, pgroup: c_int) -> c_int {
815 if attr.is_null() { return EINVAL; }
816 (*attr).__pgrp = pgroup;
817 0
818}
819
820#[no_mangle]
821pub unsafe extern "C" fn posix_spawnattr_getschedparam(attr: *const posix_spawnattr_t, param: *mut sched_param) -> c_int {
822 if attr.is_null() || param.is_null() { return EINVAL; }
823 (*param).sched_priority = (*attr).__prio;
824 0
825}
826
827#[no_mangle]
828pub unsafe extern "C" fn posix_spawnattr_setschedparam(attr: *mut posix_spawnattr_t, param: *const sched_param) -> c_int {
829 if attr.is_null() || param.is_null() { return EINVAL; }
830 (*attr).__prio = (*param).sched_priority;
831 0
832}
833
834#[no_mangle]
835pub unsafe extern "C" fn posix_spawnattr_getschedpolicy(attr: *const posix_spawnattr_t, policy: *mut c_int) -> c_int {
836 if attr.is_null() || policy.is_null() { return EINVAL; }
837 *policy = (*attr).__pol;
838 0
839}
840
841#[no_mangle]
842pub unsafe extern "C" fn posix_spawnattr_setschedpolicy(attr: *mut posix_spawnattr_t, policy: c_int) -> c_int {
843 if attr.is_null() { return EINVAL; }
844 (*attr).__pol = policy;
845 0
846}
847
848#[no_mangle]
849pub unsafe extern "C" fn posix_spawnattr_getsigdefault(attr: *const posix_spawnattr_t, set: *mut SigSetT) -> c_int {
850 if attr.is_null() || set.is_null() { return EINVAL; }
851 *set = (*attr).__def[0];
852 0
853}
854
855#[no_mangle]
856pub unsafe extern "C" fn posix_spawnattr_setsigdefault(attr: *mut posix_spawnattr_t, set: *const SigSetT) -> c_int {
857 if attr.is_null() || set.is_null() { return EINVAL; }
858 (*attr).__def = [0; 16];
859 (*attr).__def[0] = *set;
860 0
861}
862
863#[no_mangle]
864pub unsafe extern "C" fn posix_spawnattr_getsigmask(attr: *const posix_spawnattr_t, set: *mut SigSetT) -> c_int {
865 if attr.is_null() || set.is_null() { return EINVAL; }
866 *set = (*attr).__mask[0];
867 0
868}
869
870#[no_mangle]
871pub unsafe extern "C" fn posix_spawnattr_setsigmask(attr: *mut posix_spawnattr_t, set: *const SigSetT) -> c_int {
872 if attr.is_null() || set.is_null() { return EINVAL; }
873 (*attr).__mask = [0; 16];
874 (*attr).__mask[0] = *set;
875 0
876}
877
878#[no_mangle]
883pub unsafe extern "C" fn __fbufsize(f: *mut FILE) -> usize {
884 if f.is_null() { return 0; }
885 (*f).buf_size
886}
887
888#[no_mangle]
889pub unsafe extern "C" fn __flbf(f: *mut FILE) -> c_int {
890 if f.is_null() { return 0; }
891 if (*f).lbf >= 0 { 1 } else { 0 }
892}
893
894#[no_mangle]
895pub unsafe extern "C" fn __fpending(f: *mut FILE) -> usize {
896 if f.is_null() || (*f).wend.is_null() { return 0; }
897 (*f).wpos as usize - (*f).wbase as usize
898}
899
900#[no_mangle]
901pub unsafe extern "C" fn __fpurge(f: *mut FILE) {
902 if f.is_null() { return; }
903 (*f).wpos = core::ptr::null_mut();
904 (*f).wbase = core::ptr::null_mut();
905 (*f).wend = core::ptr::null_mut();
906 (*f).rpos = core::ptr::null_mut();
907 (*f).rend = core::ptr::null_mut();
908}
909
910#[no_mangle]
911pub unsafe extern "C" fn __freadable(f: *mut FILE) -> c_int {
912 if f.is_null() { return 0; }
913 if (*f).flags & F_NORD == 0 { 1 } else { 0 }
914}
915
916#[no_mangle]
917pub unsafe extern "C" fn __freadahead(f: *mut FILE) -> usize {
918 if f.is_null() || (*f).rend.is_null() { return 0; }
919 (*f).rend as usize - (*f).rpos as usize
920}
921
922#[no_mangle]
923pub unsafe extern "C" fn __freading(f: *mut FILE) -> c_int {
924 if f.is_null() { return 0; }
925 if (*f).flags & F_NOWR != 0 || !(*f).rend.is_null() { 1 } else { 0 }
926}
927
928#[no_mangle]
929pub unsafe extern "C" fn __freadptr(f: *mut FILE, sizep: *mut usize) -> *const c_char {
930 if f.is_null() || sizep.is_null() { return core::ptr::null(); }
931 if (*f).rpos == (*f).rend { *sizep = 0; return core::ptr::null(); }
932 *sizep = (*f).rend as usize - (*f).rpos as usize;
933 (*f).rpos as *const c_char
934}
935
936#[no_mangle]
937pub unsafe extern "C" fn __freadptrinc(f: *mut FILE, inc: usize) {
938 if f.is_null() || (*f).rpos.is_null() { return; }
939 let left = (*f).rend as usize - (*f).rpos as usize;
940 let step = if inc > left { left } else { inc };
941 (*f).rpos = (*f).rpos.add(step);
942}
943
944#[no_mangle]
945pub unsafe extern "C" fn __fsetlocking(_f: *mut FILE, _type: c_int) -> c_int {
946 1
947}
948
949#[no_mangle]
950pub unsafe extern "C" fn __fwritable(f: *mut FILE) -> c_int {
951 if f.is_null() { return 0; }
952 if (*f).flags & F_NOWR == 0 { 1 } else { 0 }
953}
954
955#[no_mangle]
956pub unsafe extern "C" fn __fwriting(f: *mut FILE) -> c_int {
957 if f.is_null() { return 0; }
958 if (*f).flags & F_NORD != 0 || !(*f).wend.is_null() { 1 } else { 0 }
959}
960
961#[no_mangle]
962pub unsafe extern "C" fn __fseterr(f: *mut FILE) {
963 if !f.is_null() { (*f).flags |= F_ERR; }
964}
965
966#[no_mangle]
967pub unsafe extern "C" fn __fputwc_unlocked(c: wchar_t, f: *mut FILE) -> wint_t {
968 crate::fputwc(c, f)
969}
970
971#[no_mangle]
976pub unsafe extern "C" fn sigqueue(pid: c_int, sig: c_int, value: *mut c_void) -> c_int {
977 let mut si = [0u8; 128];
978 *(si.as_mut_ptr() as *mut c_int) = sig;
979 *(si.as_mut_ptr().add(8) as *mut c_int) = -1; *(si.as_mut_ptr().add(16) as *mut c_int) = crate::getpid();
981 *(si.as_mut_ptr().add(20) as *mut u32) = crate::getuid();
982 *(si.as_mut_ptr().add(24) as *mut *mut c_void) = value;
983 let r = <Arch as Syscalls>::syscall3(SYS_RT_SIGQUEUEINFO, pid as i64, sig as i64, si.as_ptr() as i64);
984 if r < 0 { ERRNO!() = (-r) as c_int; -1 } else { 0 }
985}
986
987#[no_mangle]
988pub unsafe extern "C" fn timespec_get(ts: *mut timespec, base: c_int) -> c_int {
989 if base != 1 || ts.is_null() { return 0; }
990 if crate::clock_gettime(0, ts) != 0 { return 0; }
991 base
992}
993
994#[no_mangle]
995pub unsafe extern "C" fn ualarm(usecs: u32, interval: u32) -> u32 {
996 #[repr(C)]
997 struct ITV {
998 sec: i64,
999 usec: i64,
1000 }
1001 let mut new: [ITV; 2] = [
1002 ITV { sec: (interval / 1_000_000) as i64, usec: (interval % 1_000_000) as i64 },
1003 ITV { sec: (usecs / 1_000_000) as i64, usec: (usecs % 1_000_000) as i64 },
1004 ];
1005 let mut old: [ITV; 2] = [ITV { sec: 0, usec: 0 }, ITV { sec: 0, usec: 0 }];
1006 if crate::setitimer(0, new.as_mut_ptr() as *const c_void, old.as_mut_ptr() as *mut c_void) != 0 {
1007 return u32::MAX;
1008 }
1009 (old[0].sec as u32) * 1_000_000 + old[0].usec as u32
1010}
1011
1012static mut LCKPWDF_FD: c_int = -1;
1013
1014#[repr(C)]
1015#[derive(Copy, Clone)]
1016struct FlockT {
1017 l_type: i16,
1018 l_whence: i16,
1019 l_start: i64,
1020 l_len: i64,
1021 l_pid: i32,
1022 __pad: i32,
1023}
1024
1025#[no_mangle]
1026pub unsafe extern "C" fn lckpwdf() -> c_int {
1027 if LCKPWDF_FD >= 0 { return 0; }
1028 let fd = crate::open(
1029 b"/etc/.pwd.lock\0".as_ptr() as *const c_char,
1030 1 | 0x40 | 0x80000,
1031 0o600,
1032 );
1033 if fd < 0 { return -1; }
1034 let fl = FlockT { l_type: 1, l_whence: 0, l_start: 0, l_len: 0, l_pid: 0, __pad: 0 };
1035 if crate::fcntl(fd, 7, &fl as *const FlockT as *mut c_void) != 0 {
1036 crate::close(fd);
1037 return -1;
1038 }
1039 LCKPWDF_FD = fd;
1040 0
1041}
1042
1043#[no_mangle]
1044pub unsafe extern "C" fn ulckpwdf() -> c_int {
1045 if LCKPWDF_FD < 0 { return -1; }
1046 let fd = LCKPWDF_FD;
1047 LCKPWDF_FD = -1;
1048 crate::close(fd);
1049 0
1050}
1051
1052#[no_mangle]
1053pub unsafe extern "C" fn malloc_usable_size(ptr: *mut u8) -> usize {
1054 crate::alloc_usable_size(ptr)
1055}
1056
1057#[no_mangle]
1058pub unsafe extern "C" fn __wcsftime_l(
1059 s: *mut wchar_t,
1060 max: usize,
1061 fmt: *const wchar_t,
1062 tm: *const c_void,
1063 _loc: locale_t,
1064) -> usize {
1065 crate::wcsftime(s as *mut u32, max, fmt as *const u32, tm)
1066}
1067
1068#[no_mangle]
1069pub unsafe extern "C" fn __wctype_l(name: *const c_char, _loc: locale_t) -> wctype_t {
1070 crate::wctype(name)
1071}
1072
1073#[no_mangle]
1074pub unsafe extern "C" fn __wctrans_l(name: *const c_char, _loc: locale_t) -> wctrans_t {
1075 crate::wctrans(name)
1076}
1077
1078#[no_mangle]
1079pub unsafe extern "C" fn __fgetwc_unlocked(f: *mut FILE) -> wint_t {
1080 crate::fgetwc(f)
1081}
1082
1083#[no_mangle]
1084pub unsafe extern "C" fn _flushlbf() {
1085 crate::fflush(core::ptr::null_mut());
1086}