Debian Codegraph @

debian/rust-vendor/rustix/src/backend/libc/process/syscalls.rs

raw
1 //! libc syscalls supporting `rustix::process`.
2
3 use crate::backend::c;
4 #[cfg(not(any(target_os = "fuchsia", target_os = "wasi")))]
5 use crate::backend::conv::borrowed_fd;
6 #[cfg(any(target_os = "linux", feature = "fs"))]
7 use crate::backend::conv::c_str;
8 #[cfg(all(feature = "alloc", feature = "fs", not(target_os = "wasi")))]
9 use crate::backend::conv::ret_discarded_char_ptr;
10 #[cfg(not(any(
11 target_os = "espidf",
12 target_os = "fuchsia",
13 target_os = "redox",
14 target_os = "vita",
15 target_os = "wasi"
16 )))]
17 use crate::backend::conv::ret_infallible;
18 #[cfg(not(target_os = "wasi"))]
19 use crate::backend::conv::ret_pid_t;
20 #[cfg(all(feature = "alloc", not(target_os = "wasi")))]
21 use crate::backend::conv::ret_usize;
22 use crate::backend::conv::{ret, ret_c_int};
23 #[cfg(not(any(target_os = "fuchsia", target_os = "wasi")))]
24 use crate::fd::BorrowedFd;
25 #[cfg(target_os = "linux")]
26 use crate::fd::{AsRawFd as _, OwnedFd, RawFd};
27 #[cfg(any(target_os = "linux", feature = "fs"))]
28 use crate::ffi::CStr;
29 #[cfg(feature = "fs")]
30 use crate::fs::Mode;
31 use crate::io;
32 #[cfg(not(any(
33 target_os = "emscripten",
34 target_os = "espidf",
35 target_os = "fuchsia",
36 target_os = "horizon",
37 target_os = "redox",
38 target_os = "vita",
39 target_os = "wasi"
40 )))]
41 use crate::process::Flock;
42 #[cfg(all(feature = "alloc", not(target_os = "wasi")))]
43 use crate::process::Gid;
44 #[cfg(not(target_os = "wasi"))]
45 use crate::process::Pid;
46 #[cfg(not(any(target_os = "espidf", target_os = "wasi")))]
47 use crate::process::Signal;
48 #[cfg(not(any(
49 target_os = "espidf",
50 target_os = "fuchsia",
51 target_os = "vita",
52 target_os = "wasi"
53 )))]
54 use crate::process::Uid;
55 #[cfg(not(any(target_os = "espidf", target_os = "vita", target_os = "wasi")))]
56 use crate::process::{RawPid, WaitOptions, WaitStatus};
57 #[cfg(not(any(
58 target_os = "espidf",
59 target_os = "fuchsia",
60 target_os = "horizon",
61 target_os = "redox",
62 target_os = "vita",
63 target_os = "wasi"
64 )))]
65 use crate::process::{Resource, Rlimit};
66 #[cfg(not(any(
67 target_os = "cygwin",
68 target_os = "espidf",
69 target_os = "horizon",
70 target_os = "openbsd",
71 target_os = "redox",
72 target_os = "vita",
73 target_os = "wasi",
74 )))]
75 use crate::process::{WaitId, WaitIdOptions, WaitIdStatus};
76 use core::mem::MaybeUninit;
77 #[cfg(target_os = "linux")]
78 use {
79 super::super::conv::ret_owned_fd, crate::process::PidfdFlags, crate::process::PidfdGetfdFlags,
80 };
81
82 #[cfg(feature = "fs")]
83 #[cfg(not(target_os = "wasi"))]
84 pub(crate) fn chdir(path: &CStr) -> io::Result<()> {
85 unsafe { ret(c::chdir(c_str(path))) }
86 }
87
88 #[cfg(not(any(target_os = "fuchsia", target_os = "wasi")))]
89 pub(crate) fn fchdir(dirfd: BorrowedFd<'_>) -> io::Result<()> {
90 unsafe { ret(c::fchdir(borrowed_fd(dirfd))) }
91 }
92
93 #[cfg(feature = "fs")]
94 #[cfg(not(any(target_os = "fuchsia", target_os = "wasi")))]
95 pub(crate) fn chroot(path: &CStr) -> io::Result<()> {
96 unsafe { ret(c::chroot(c_str(path))) }
97 }
98
99 #[cfg(all(feature = "alloc", feature = "fs"))]
100 #[cfg(not(target_os = "wasi"))]
101 pub(crate) fn getcwd(buf: &mut [MaybeUninit<u8>]) -> io::Result<()> {
102 unsafe { ret_discarded_char_ptr(c::getcwd(buf.as_mut_ptr().cast(), buf.len())) }
103 }
104
105 #[cfg(not(target_os = "wasi"))]
106 #[inline]
107 #[must_use]
108 pub(crate) fn getppid() -> Option<Pid> {
109 unsafe {
110 let pid: i32 = c::getppid();
111 Pid::from_raw(pid)
112 }
113 }
114
115 #[cfg(not(target_os = "wasi"))]
116 #[inline]
117 pub(crate) fn getpgid(pid: Option<Pid>) -> io::Result<Pid> {
118 unsafe {
119 let pgid = ret_pid_t(c::getpgid(Pid::as_raw(pid) as _))?;
120 Ok(Pid::from_raw_unchecked(pgid))
121 }
122 }
123
124 #[cfg(not(target_os = "wasi"))]
125 #[inline]
126 pub(crate) fn setpgid(pid: Option<Pid>, pgid: Option<Pid>) -> io::Result<()> {
127 unsafe { ret(c::setpgid(Pid::as_raw(pid) as _, Pid::as_raw(pgid) as _)) }
128 }
129
130 #[cfg(not(target_os = "wasi"))]
131 #[inline]
132 #[must_use]
133 pub(crate) fn getpgrp() -> Pid {
134 unsafe {
135 let pgid = c::getpgrp();
136 Pid::from_raw_unchecked(pgid)
137 }
138 }
139
140 #[cfg(not(target_os = "wasi"))]
141 #[cfg(feature = "fs")]
142 #[inline]
143 pub(crate) fn umask(mask: Mode) -> Mode {
144 unsafe { Mode::from_bits_retain(c::umask(mask.bits() as c::mode_t).into()) }
145 }
146
147 #[cfg(not(any(target_os = "fuchsia", target_os = "vita", target_os = "wasi")))]
148 #[inline]
149 pub(crate) fn nice(inc: i32) -> io::Result<i32> {
150 libc_errno::set_errno(libc_errno::Errno(0));
151 let r = unsafe { c::nice(inc) };
152 if libc_errno::errno().0 != 0 {
153 ret_c_int(r)
154 } else {
155 Ok(r)
156 }
157 }
158
159 #[cfg(not(any(
160 target_os = "espidf",
161 target_os = "fuchsia",
162 target_os = "horizon",
163 target_os = "vita",
164 target_os = "wasi"
165 )))]
166 #[inline]
167 pub(crate) fn getpriority_user(uid: Uid) -> io::Result<i32> {
168 libc_errno::set_errno(libc_errno::Errno(0));
169 let r = unsafe { c::getpriority(c::PRIO_USER, uid.as_raw() as _) };
170 if libc_errno::errno().0 != 0 {
171 ret_c_int(r)
172 } else {
173 Ok(r)
174 }
175 }
176
177 #[cfg(not(any(
178 target_os = "espidf",
179 target_os = "fuchsia",
180 target_os = "horizon",
181 target_os = "vita",
182 target_os = "wasi"
183 )))]
184 #[inline]
185 pub(crate) fn getpriority_pgrp(pgid: Option<Pid>) -> io::Result<i32> {
186 libc_errno::set_errno(libc_errno::Errno(0));
187 let r = unsafe { c::getpriority(c::PRIO_PGRP, Pid::as_raw(pgid) as _) };
188 if libc_errno::errno().0 != 0 {
189 ret_c_int(r)
190 } else {
191 Ok(r)
192 }
193 }
194
195 #[cfg(not(any(
196 target_os = "espidf",
197 target_os = "fuchsia",
198 target_os = "horizon",
199 target_os = "vita",
200 target_os = "wasi"
201 )))]
202 #[inline]
203 pub(crate) fn getpriority_process(pid: Option<Pid>) -> io::Result<i32> {
204 libc_errno::set_errno(libc_errno::Errno(0));
205 let r = unsafe { c::getpriority(c::PRIO_PROCESS, Pid::as_raw(pid) as _) };
206 if libc_errno::errno().0 != 0 {
207 ret_c_int(r)
208 } else {
209 Ok(r)
210 }
211 }
212
213 #[cfg(not(any(
214 target_os = "espidf",
215 target_os = "fuchsia",
216 target_os = "horizon",
217 target_os = "vita",
218 target_os = "wasi"
219 )))]
220 #[inline]
221 pub(crate) fn setpriority_user(uid: Uid, priority: i32) -> io::Result<()> {
222 unsafe { ret(c::setpriority(c::PRIO_USER, uid.as_raw() as _, priority)) }
223 }
224
225 #[cfg(not(any(
226 target_os = "espidf",
227 target_os = "fuchsia",
228 target_os = "horizon",
229 target_os = "vita",
230 target_os = "wasi"
231 )))]
232 #[inline]
233 pub(crate) fn setpriority_pgrp(pgid: Option<Pid>, priority: i32) -> io::Result<()> {
234 unsafe {
235 ret(c::setpriority(
236 c::PRIO_PGRP,
237 Pid::as_raw(pgid) as _,
238 priority,
239 ))
240 }
241 }
242
243 #[cfg(not(any(
244 target_os = "espidf",
245 target_os = "fuchsia",
246 target_os = "horizon",
247 target_os = "vita",
248 target_os = "wasi"
249 )))]
250 #[inline]
251 pub(crate) fn setpriority_process(pid: Option<Pid>, priority: i32) -> io::Result<()> {
252 unsafe {
253 ret(c::setpriority(
254 c::PRIO_PROCESS,
255 Pid::as_raw(pid) as _,
256 priority,
257 ))
258 }
259 }
260
261 #[cfg(not(any(
262 target_os = "espidf",
263 target_os = "fuchsia",
264 target_os = "horizon",
265 target_os = "redox",
266 target_os = "vita",
267 target_os = "wasi"
268 )))]
269 #[inline]
270 pub(crate) fn getrlimit(limit: Resource) -> Rlimit {
271 let mut result = MaybeUninit::<c::rlimit>::uninit();
272 unsafe {
273 ret_infallible(c::getrlimit(limit as _, result.as_mut_ptr()));
274 rlimit_from_libc(result.assume_init())
275 }
276 }
277
278 #[cfg(not(any(
279 target_os = "espidf",
280 target_os = "fuchsia",
281 target_os = "horizon",
282 target_os = "redox",
283 target_os = "vita",
284 target_os = "wasi"
285 )))]
286 #[inline]
287 pub(crate) fn setrlimit(limit: Resource, new: Rlimit) -> io::Result<()> {
288 let lim = rlimit_to_libc(new)?;
289 unsafe { ret(c::setrlimit(limit as _, &lim)) }
290 }
291
292 #[cfg(linux_kernel)]
293 #[inline]
294 pub(crate) fn prlimit(pid: Option<Pid>, limit: Resource, new: Rlimit) -> io::Result<Rlimit> {
295 let lim = rlimit_to_libc(new)?;
296 let mut result = MaybeUninit::<c::rlimit>::uninit();
297 unsafe {
298 ret(c::prlimit(
299 Pid::as_raw(pid),
300 limit as _,
301 &lim,
302 result.as_mut_ptr(),
303 ))?;
304 Ok(rlimit_from_libc(result.assume_init()))
305 }
306 }
307
308 /// Convert a C `c::rlimit` to a Rust `Rlimit`.
309 #[cfg(not(any(
310 target_os = "espidf",
311 target_os = "fuchsia",
312 target_os = "horizon",
313 target_os = "redox",
314 target_os = "vita",
315 target_os = "wasi"
316 )))]
317 fn rlimit_from_libc(lim: c::rlimit) -> Rlimit {
318 let current = if lim.rlim_cur == c::RLIM_INFINITY {
319 None
320 } else {
321 Some(lim.rlim_cur.try_into().unwrap())
322 };
323 let maximum = if lim.rlim_max == c::RLIM_INFINITY {
324 None
325 } else {
326 Some(lim.rlim_max.try_into().unwrap())
327 };
328 Rlimit { current, maximum }
329 }
330
331 /// Convert a Rust [`Rlimit`] to a C `c::rlimit`.
332 #[cfg(not(any(
333 target_os = "espidf",
334 target_os = "fuchsia",
335 target_os = "horizon",
336 target_os = "redox",
337 target_os = "vita",
338 target_os = "wasi"
339 )))]
340 fn rlimit_to_libc(lim: Rlimit) -> io::Result<c::rlimit> {
341 let Rlimit { current, maximum } = lim;
342 let rlim_cur = match current {
343 Some(r) => r.try_into().map_err(|_e| io::Errno::INVAL)?,
344 None => c::RLIM_INFINITY as _,
345 };
346 let rlim_max = match maximum {
347 Some(r) => r.try_into().map_err(|_e| io::Errno::INVAL)?,
348 None => c::RLIM_INFINITY as _,
349 };
350 Ok(c::rlimit { rlim_cur, rlim_max })
351 }
352
353 #[cfg(not(any(target_os = "espidf", target_os = "vita", target_os = "wasi")))]
354 #[inline]
355 pub(crate) fn wait(waitopts: WaitOptions) -> io::Result<Option<(Pid, WaitStatus)>> {
356 _waitpid(!0, waitopts)
357 }
358
359 #[cfg(not(any(target_os = "espidf", target_os = "vita", target_os = "wasi")))]
360 #[inline]
361 pub(crate) fn waitpid(
362 pid: Option<Pid>,
363 waitopts: WaitOptions,
364 ) -> io::Result<Option<(Pid, WaitStatus)>> {
365 _waitpid(Pid::as_raw(pid), waitopts)
366 }
367
368 #[cfg(not(any(target_os = "espidf", target_os = "vita", target_os = "wasi")))]
369 #[inline]
370 pub(crate) fn waitpgid(pgid: Pid, waitopts: WaitOptions) -> io::Result<Option<(Pid, WaitStatus)>> {
371 _waitpid(-pgid.as_raw_nonzero().get(), waitopts)
372 }
373
374 #[cfg(not(any(target_os = "espidf", target_os = "vita", target_os = "wasi")))]
375 #[inline]
376 pub(crate) fn _waitpid(
377 pid: RawPid,
378 waitopts: WaitOptions,
379 ) -> io::Result<Option<(Pid, WaitStatus)>> {
380 unsafe {
381 let mut status: c::c_int = 0;
382 let pid = ret_c_int(c::waitpid(pid as _, &mut status, waitopts.bits() as _))?;
383 Ok(Pid::from_raw(pid).map(|pid| (pid, WaitStatus::new(status as _))))
384 }
385 }
386
387 #[cfg(not(any(
388 target_os = "cygwin",
389 target_os = "espidf",
390 target_os = "horizon",
391 target_os = "openbsd",
392 target_os = "redox",
393 target_os = "vita",
394 target_os = "wasi",
395 )))]
396 #[inline]
397 pub(crate) fn waitid(id: WaitId<'_>, options: WaitIdOptions) -> io::Result<Option<WaitIdStatus>> {
398 // Get the id to wait on.
399 match id {
400 WaitId::All => _waitid_all(options),
401 WaitId::Pid(pid) => _waitid_pid(pid, options),
402 WaitId::Pgid(pgid) => _waitid_pgid(pgid, options),
403 #[cfg(target_os = "linux")]
404 WaitId::PidFd(fd) => _waitid_pidfd(fd, options),
405 #[cfg(not(target_os = "linux"))]
406 WaitId::__EatLifetime(_) => unreachable!(),
407 }
408 }
409
410 #[cfg(not(any(
411 target_os = "cygwin",
412 target_os = "espidf",
413 target_os = "horizon",
414 target_os = "openbsd",
415 target_os = "redox",
416 target_os = "vita",
417 target_os = "wasi",
418 )))]
419 #[inline]
420 fn _waitid_all(options: WaitIdOptions) -> io::Result<Option<WaitIdStatus>> {
421 // `waitid` can return successfully without initializing the struct (no
422 // children found when using `WNOHANG`)
423 let mut status = MaybeUninit::<c::siginfo_t>::zeroed();
424 unsafe {
425 ret(c::waitid(
426 c::P_ALL,
427 0,
428 status.as_mut_ptr(),
429 options.bits() as _,
430 ))?
431 };
432
433 Ok(unsafe { cvt_waitid_status(status) })
434 }
435
436 #[cfg(not(any(
437 target_os = "cygwin",
438 target_os = "espidf",
439 target_os = "horizon",
440 target_os = "openbsd",
441 target_os = "redox",
442 target_os = "vita",
443 target_os = "wasi",
444 )))]
445 #[inline]
446 fn _waitid_pid(pid: Pid, options: WaitIdOptions) -> io::Result<Option<WaitIdStatus>> {
447 // `waitid` can return successfully without initializing the struct (no
448 // children found when using `WNOHANG`)
449 let mut status = MaybeUninit::<c::siginfo_t>::zeroed();
450 unsafe {
451 ret(c::waitid(
452 c::P_PID,
453 Pid::as_raw(Some(pid)) as _,
454 status.as_mut_ptr(),
455 options.bits() as _,
456 ))?
457 };
458
459 Ok(unsafe { cvt_waitid_status(status) })
460 }
461
462 #[cfg(not(any(
463 target_os = "cygwin",
464 target_os = "espidf",
465 target_os = "horizon",
466 target_os = "openbsd",
467 target_os = "redox",
468 target_os = "vita",
469 target_os = "wasi",
470 )))]
471 #[inline]
472 fn _waitid_pgid(pgid: Option<Pid>, options: WaitIdOptions) -> io::Result<Option<WaitIdStatus>> {
473 // `waitid` can return successfully without initializing the struct (no
474 // children found when using `WNOHANG`)
475 let mut status = MaybeUninit::<c::siginfo_t>::zeroed();
476 unsafe {
477 ret(c::waitid(
478 c::P_PGID,
479 Pid::as_raw(pgid) as _,
480 status.as_mut_ptr(),
481 options.bits() as _,
482 ))?
483 };
484
485 Ok(unsafe { cvt_waitid_status(status) })
486 }
487
488 #[cfg(target_os = "linux")]
489 #[inline]
490 fn _waitid_pidfd(fd: BorrowedFd<'_>, options: WaitIdOptions) -> io::Result<Option<WaitIdStatus>> {
491 // `waitid` can return successfully without initializing the struct (no
492 // children found when using `WNOHANG`)
493 let mut status = MaybeUninit::<c::siginfo_t>::zeroed();
494 unsafe {
495 ret(c::waitid(
496 c::P_PIDFD,
497 fd.as_raw_fd() as _,
498 status.as_mut_ptr(),
499 options.bits() as _,
500 ))?
501 };
502
503 Ok(unsafe { cvt_waitid_status(status) })
504 }
505
506 /// Convert a `siginfo_t` to a `WaitIdStatus`.
507 ///
508 /// # Safety
509 ///
510 /// The caller must ensure that `status` is initialized and that `waitid`
511 /// returned successfully.
512 #[cfg(not(any(
513 target_os = "cygwin",
514 target_os = "espidf",
515 target_os = "horizon",
516 target_os = "openbsd",
517 target_os = "redox",
518 target_os = "vita",
519 target_os = "wasi",
520 )))]
521 #[inline]
522 unsafe fn cvt_waitid_status(status: MaybeUninit<c::siginfo_t>) -> Option<WaitIdStatus> {
523 let status = status.assume_init();
524 // `si_pid` is supposedly the better way to check that the struct has been
525 // filled, e.g. the Linux manual page says about the `WNOHANG` case “zero
526 // out the si_pid field before the call and check for a nonzero value”.
527 // But e.g. NetBSD/OpenBSD don't have it exposed in the libc crate for now,
528 // and some platforms don't have it at all. For simplicity, always check
529 // `si_signo`. We have zero-initialized the whole struct, and all kernels
530 // should set `SIGCHLD` here.
531 if status.si_signo == 0 {
532 None
533 } else {
534 Some(WaitIdStatus(status))
535 }
536 }
537
538 #[cfg(not(any(target_os = "redox", target_os = "wasi")))]
539 #[inline]
540 pub(crate) fn getsid(pid: Option<Pid>) -> io::Result<Pid> {
541 unsafe {
542 let pid = ret_pid_t(c::getsid(Pid::as_raw(pid) as _))?;
543 Ok(Pid::from_raw_unchecked(pid))
544 }
545 }
546
547 #[cfg(not(target_os = "wasi"))]
548 #[inline]
549 pub(crate) fn setsid() -> io::Result<Pid> {
550 unsafe {
551 let pid = ret_c_int(c::setsid())?;
552 Ok(Pid::from_raw_unchecked(pid))
553 }
554 }
555
556 #[cfg(not(any(target_os = "espidf", target_os = "wasi")))]
557 #[inline]
558 pub(crate) fn kill_process(pid: Pid, sig: Signal) -> io::Result<()> {
559 unsafe { ret(c::kill(pid.as_raw_nonzero().get(), sig.as_raw())) }
560 }
561
562 #[cfg(not(any(target_os = "espidf", target_os = "wasi")))]
563 #[inline]
564 pub(crate) fn kill_process_group(pid: Pid, sig: Signal) -> io::Result<()> {
565 unsafe {
566 ret(c::kill(
567 pid.as_raw_nonzero().get().wrapping_neg(),
568 sig.as_raw(),
569 ))
570 }
571 }
572
573 #[cfg(not(any(target_os = "espidf", target_os = "wasi")))]
574 #[inline]
575 pub(crate) fn kill_current_process_group(sig: Signal) -> io::Result<()> {
576 unsafe { ret(c::kill(0, sig.as_raw())) }
577 }
578
579 #[cfg(not(any(target_os = "espidf", target_os = "wasi")))]
580 pub(crate) fn test_kill_process(pid: Pid) -> io::Result<()> {
581 unsafe { ret(c::kill(pid.as_raw_nonzero().get(), 0)) }
582 }
583
584 #[cfg(not(any(target_os = "espidf", target_os = "wasi")))]
585 #[inline]
586 pub(crate) fn test_kill_process_group(pid: Pid) -> io::Result<()> {
587 unsafe { ret(c::kill(pid.as_raw_nonzero().get().wrapping_neg(), 0)) }
588 }
589
590 #[cfg(not(any(target_os = "espidf", target_os = "wasi")))]
591 #[inline]
592 pub(crate) fn test_kill_current_process_group() -> io::Result<()> {
593 unsafe { ret(c::kill(0, 0)) }
594 }
595
596 #[cfg(freebsdlike)]
597 #[inline]
598 pub(crate) unsafe fn procctl(
599 idtype: c::idtype_t,
600 id: c::id_t,
601 option: c::c_int,
602 data: *mut c::c_void,
603 ) -> io::Result<()> {
604 ret(c::procctl(idtype, id, option, data))
605 }
606
607 #[cfg(target_os = "linux")]
608 pub(crate) fn pidfd_open(pid: Pid, flags: PidfdFlags) -> io::Result<OwnedFd> {
609 syscall! {
610 fn pidfd_open(
611 pid: c::pid_t,
612 flags: c::c_uint
613 ) via SYS_pidfd_open -> c::c_int
614 }
615 unsafe {
616 ret_owned_fd(pidfd_open(
617 pid.as_raw_nonzero().get(),
618 bitflags_bits!(flags),
619 ))
620 }
621 }
622
623 #[cfg(target_os = "linux")]
624 pub(crate) fn pidfd_send_signal(pidfd: BorrowedFd<'_>, sig: Signal) -> io::Result<()> {
625 syscall! {
626 fn pidfd_send_signal(
627 pid: c::pid_t,
628 sig: c::c_int,
629 info: *const c::siginfo_t,
630 flags: c::c_int
631 ) via SYS_pidfd_send_signal -> c::c_int
632 }
633 unsafe {
634 ret(pidfd_send_signal(
635 borrowed_fd(pidfd),
636 sig.as_raw(),
637 core::ptr::null(),
638 0,
639 ))
640 }
641 }
642
643 #[cfg(target_os = "linux")]
644 pub(crate) fn pidfd_getfd(
645 pidfd: BorrowedFd<'_>,
646 targetfd: RawFd,
647 flags: PidfdGetfdFlags,
648 ) -> io::Result<OwnedFd> {
649 syscall! {
650 fn pidfd_getfd(
651 pidfd: c::c_int,
652 targetfd: c::c_int,
653 flags: c::c_uint
654 ) via SYS_pidfd_getfd -> c::c_int
655 }
656 unsafe {
657 ret_owned_fd(pidfd_getfd(
658 borrowed_fd(pidfd),
659 targetfd,
660 bitflags_bits!(flags),
661 ))
662 }
663 }
664
665 #[cfg(target_os = "linux")]
666 pub(crate) fn pivot_root(new_root: &CStr, put_old: &CStr) -> io::Result<()> {
667 syscall! {
668 fn pivot_root(
669 new_root: *const c::c_char,
670 put_old: *const c::c_char
671 ) via SYS_pivot_root -> c::c_int
672 }
673 unsafe { ret(pivot_root(c_str(new_root), c_str(put_old))) }
674 }
675
676 #[cfg(all(feature = "alloc", not(target_os = "wasi")))]
677 pub(crate) fn getgroups(buf: &mut [Gid]) -> io::Result<usize> {
678 let len = buf.len().try_into().map_err(|_| io::Errno::NOMEM)?;
679
680 unsafe { ret_usize(c::getgroups(len, buf.as_mut_ptr().cast()) as isize) }
681 }
682
683 #[cfg(not(any(
684 target_os = "emscripten",
685 target_os = "espidf",
686 target_os = "fuchsia",
687 target_os = "horizon",
688 target_os = "redox",
689 target_os = "vita",
690 target_os = "wasi"
691 )))]
692 #[inline]
693 pub(crate) fn fcntl_getlk(fd: BorrowedFd<'_>, lock: &Flock) -> io::Result<Option<Flock>> {
694 let mut curr_lock: c::flock = lock.as_raw();
695 unsafe { ret(c::fcntl(borrowed_fd(fd), c::F_GETLK, &mut curr_lock))? };
696
697 // If no blocking lock is found, `fcntl(GETLK, ..)` sets `l_type` to
698 // `F_UNLCK`.
699 if curr_lock.l_type == c::F_UNLCK as _ {
700 Ok(None)
701 } else {
702 Ok(Some(unsafe { Flock::from_raw_unchecked(curr_lock) }))
703 }
704 }