Lines Matching +full:use +full:- +full:libc
1 //! Safe wrappers around functions found in libc "unistd.h" header
3 use crate::errno::{self, Errno};
6 use crate::fcntl::{at_rawfd, AtFlags};
8 use crate::fcntl::{fcntl, FcntlArg::F_SETFD, FdFlag, OFlag};
20 use crate::sys::stat::FileFlag;
22 use crate::sys::stat::Mode;
23 use crate::{Error, NixPath, Result};
25 use cfg_if::cfg_if;
26 use libc::{
30 use std::convert::Infallible;
31 use std::ffi::{CStr, OsString};
33 use std::ffi::{CString, OsStr};
35 use std::os::unix::ffi::OsStrExt;
36 use std::os::unix::ffi::OsStringExt;
37 use std::os::unix::io::RawFd;
38 use std::path::PathBuf;
39 use std::{fmt, mem, ptr};
44 pub use self::pivot_root::*;
54 pub use self::setres::*;
63 pub use self::getres::*;
77 pub const fn from_raw(uid: uid_t) -> Self {
83 pub fn current() -> Self {
89 pub fn effective() -> Self {
93 /// Returns true if the `Uid` represents privileged user - root. (If it equals zero.)
94 pub const fn is_root(self) -> bool {
99 pub const fn as_raw(self) -> uid_t {
105 fn from(uid: Uid) -> Self {
111 fn from(uid: uid_t) -> Self {
117 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
134 pub const fn from_raw(gid: gid_t) -> Self {
140 pub fn current() -> Self {
146 pub fn effective() -> Self {
151 pub const fn as_raw(self) -> gid_t {
157 fn from(gid: Gid) -> Self {
163 fn from(gid: gid_t) -> Self {
169 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
186 pub const fn from_raw(pid: pid_t) -> Self {
192 pub fn this() -> Self {
198 pub fn parent() -> Self {
203 pub const fn as_raw(self) -> pid_t {
209 fn from(pid: Pid) -> Self {
215 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
236 pub fn is_child(self) -> bool {
242 pub fn is_parent(self) -> bool {
255 /// use nix::{sys::wait::waitpid,unistd::{fork, ForkResult, write}};
263 /// // Unsafe to use `println!` (or `unwrap`) here. See Safety.
264 /// write(libc::STDOUT_FILENO, "I'm a new child process\n".as_bytes()).ok();
265 /// unsafe { libc::_exit(0) };
282 /// In a multithreaded program, only [async-signal-safe] functions like `pause`
284 /// that memory allocation may **not** be async-signal-safe and thus must be
290 /// [async-signal-safe]: https://man7.org/linux/man-pages/man7/signal-safety.7.html
292 pub unsafe fn fork() -> Result<ForkResult> {
293 use self::ForkResult::*;
294 let res = libc::fork();
308 pub fn getpid() -> Pid {
309 Pid(unsafe { libc::getpid() })
318 pub fn getppid() -> Pid {
319 …Pid(unsafe { libc::getppid() }) // no error handling, according to man page: "These functions are …
332 pub fn setpgid(pid: Pid, pgid: Pid) -> Result<()> {
333 let res = unsafe { libc::setpgid(pid.into(), pgid.into()) };
337 pub fn getpgid(pid: Option<Pid>) -> Result<Pid> {
338 let res = unsafe { libc::getpgid(pid.unwrap_or(Pid(0)).into()) };
345 pub fn setsid() -> Result<Pid> {
346 Errno::result(unsafe { libc::setsid() }).map(Pid)
356 pub fn getsid(pid: Option<Pid>) -> Result<Pid> {
357 let res = unsafe { libc::getsid(pid.unwrap_or(Pid(0)).into()) };
370 pub fn tcgetpgrp(fd: c_int) -> Result<Pid> {
371 let res = unsafe { libc::tcgetpgrp(fd) };
380 pub fn tcsetpgrp(fd: c_int, pgrp: Pid) -> Result<()> {
381 let res = unsafe { libc::tcsetpgrp(fd, pgrp.into()) };
394 pub fn getpgrp() -> Pid {
395 Pid(unsafe { libc::getpgrp() })
399 /// [gettid(2)](https://man7.org/linux/man-pages/man2/gettid.2.html).
410 pub fn gettid() -> Pid {
411 Pid(unsafe { libc::syscall(libc::SYS_gettid) as pid_t })
428 pub fn dup(oldfd: RawFd) -> Result<RawFd> {
429 let res = unsafe { libc::dup(oldfd) };
437 /// This function behaves similar to `dup()` except that it will try to use the
441 pub fn dup2(oldfd: RawFd, newfd: RawFd) -> Result<RawFd> {
442 let res = unsafe { libc::dup2(oldfd, newfd) };
448 /// and flags (see [dup(2)](https://man7.org/linux/man-pages/man2/dup.2.html)).
452 pub fn dup3(oldfd: RawFd, newfd: RawFd, flags: OFlag) -> Result<RawFd> {
457 fn dup3_polyfill(oldfd: RawFd, newfd: RawFd, flags: OFlag) -> Result<RawFd> {
480 pub fn chdir<P: ?Sized + NixPath>(path: &P) -> Result<()> {
482 unsafe { libc::chdir(cstr.as_ptr()) }
496 pub fn fchdir(dirfd: RawFd) -> Result<()> {
497 let res = unsafe { libc::fchdir(dirfd) };
508 /// - current user has insufficient rights in the parent directory
509 /// - the path already exists
510 /// - the path name is too long (longer than `PATH_MAX`, usually 4096 on linux, 1024 on OS X)
515 /// use nix::unistd;
516 /// use nix::sys::stat;
517 /// use tempfile::tempdir;
529 pub fn mkdir<P: ?Sized + NixPath>(path: &P, mode: Mode) -> Result<()> {
531 unsafe { libc::mkdir(cstr.as_ptr(), mode.bits() as mode_t) }
543 /// - current user has insufficient rights in the parent directory
544 /// - the path already exists
545 /// - the path name is too long (longer than `PATH_MAX`, usually 4096 on linux, 1024 on OS X)
553 /// use nix::unistd;
554 /// use nix::sys::stat;
555 /// use tempfile::tempdir;
568 pub fn mkfifo<P: ?Sized + NixPath>(path: &P, mode: Mode) -> Result<()> {
570 unsafe { libc::mkfifo(cstr.as_ptr(), mode.bits() as mode_t) }
590 pub fn mkfifoat<P: ?Sized + NixPath>(dirfd: Option<RawFd>, path: &P, mode: Mode) -> Result<()> {
592 libc::mkfifoat(at_rawfd(dirfd), cstr.as_ptr(), mode.bits() as mode_t)
604 /// directory. This is identical to `libc::symlink(path1, path2)`.
611 path2: &P2) -> Result<()> {
616 libc::symlinkat(
618 dirfd.unwrap_or(libc::AT_FDCWD),
631 fn reserve_double_buffer_size<T>(buf: &mut Vec<T>, limit: usize) -> Result<()> { in reserve_double_buffer_size()
632 use std::cmp::min; in reserve_double_buffer_size()
655 /// use nix::unistd;
662 pub fn getcwd() -> Result<PathBuf> {
672 if !libc::getcwd(ptr, buf.capacity()).is_null() {
698 fn chown_raw_ids(owner: Option<Uid>, group: Option<Gid>) -> (libc::uid_t, libc::gid_t) {
699 // According to the POSIX specification, -1 is used to indicate that owner and group
701 // around to get -1.
717 pub fn chown<P: ?Sized + NixPath>(path: &P, owner: Option<Uid>, group: Option<Gid>) -> Result<()> {
720 unsafe { libc::chown(cstr.as_ptr(), uid, gid) }
734 pub fn fchown(fd: RawFd, owner: Option<Uid>, group: Option<Gid>) -> Result<()> {
736 let res = unsafe { libc::fchown(fd, uid, gid) };
762 /// a call `libc::lchown(path, owner, group)`. That's why `lchown` is unimplemented in
775 ) -> Result<()> {
783 libc::fchownat(at_rawfd(dirfd), cstr.as_ptr(), uid, gid,
784 atflag.bits() as libc::c_int)
793 fn to_exec_array<S: AsRef<CStr>>(args: &[S]) -> Vec<*const c_char> {
794 use std::iter::once;
808 pub fn execv<S: AsRef<CStr>>(path: &CStr, argv: &[S]) -> Result<Infallible> {
812 libc::execv(path.as_ptr(), args_p.as_ptr())
832 pub fn execve<SA: AsRef<CStr>, SE: AsRef<CStr>>(path: &CStr, args: &[SA], env: &[SE]) -> Result<Inf…
837 libc::execve(path.as_ptr(), args_p.as_ptr(), env_p.as_ptr())
853 pub fn execvp<S: AsRef<CStr>>(filename: &CStr, args: &[S]) -> Result<Infallible> {
857 libc::execvp(filename.as_ptr(), args_p.as_ptr())
865 /// [`execvpe(3)`](https://man7.org/linux/man-pages/man3/exec.3.html)).
873 pub fn execvpe<SA: AsRef<CStr>, SE: AsRef<CStr>>(filename: &CStr, args: &[SA], env: &[SE]) -> Resul…
878 libc::execvpe(filename.as_ptr(), args_p.as_ptr(), env_p.as_ptr())
899 pub fn fexecve<SA: AsRef<CStr> ,SE: AsRef<CStr>>(fd: RawFd, args: &[SA], env: &[SE]) -> Result<Infa…
904 libc::fexecve(fd, args_p.as_ptr(), env_p.as_ptr())
911 /// [execveat(2)](https://man7.org/linux/man-pages/man2/execveat.2.html)).
923 env: &[SE], flags: super::fcntl::AtFlags) -> Result<Infallible> {
928 libc::syscall(libc::SYS_execveat, dirfd, pathname.as_ptr(),
936 /// [daemon(3)](https://man7.org/linux/man-pages/man3/daemon.3.html)).
968 pub fn daemon(nochdir: bool, noclose: bool) -> Result<()> {
969 let res = unsafe { libc::daemon(nochdir as c_int, noclose as c_int) };
978 /// [sethostname(2)](https://man7.org/linux/man-pages/man2/gethostname.2.html)).
985 pub fn sethostname<S: AsRef<OsStr>>(name: S) -> Result<()> {
1002 let res = unsafe { libc::sethostname(ptr, len) };
1014 /// use nix::unistd;
1017 /// let hostname = hostname.into_string().expect("Hostname wasn't valid UTF-8");
1020 pub fn gethostname() -> Result<OsString> {
1026 let res = unsafe { libc::gethostname(ptr, len) };
1029 buffer.as_mut_ptr().wrapping_add(len - 1).write(0); // ensure always null-terminated
1040 /// Be aware that many Rust types implicitly close-on-drop, including
1042 /// a double-close condition, which can cause confusing `EBADF` errors in
1049 /// use std::os::unix::io::AsRawFd;
1050 /// use nix::unistd::close;
1057 /// use std::os::unix::io::IntoRawFd;
1058 /// use nix::unistd::close;
1063 pub fn close(fd: RawFd) -> Result<()> { in close()
1064 let res = unsafe { libc::close(fd) }; in close()
1071 pub fn read(fd: RawFd, buf: &mut [u8]) -> Result<usize> { in read()
1073 libc::read(fd, buf.as_mut_ptr() as *mut c_void, buf.len() as size_t) in read()
1082 pub fn write(fd: RawFd, buf: &[u8]) -> Result<usize> { in write()
1084 libc::write(fd, buf.as_ptr() as *const c_void, buf.len() as size_t) in write()
1101 SeekSet = libc::SEEK_SET,
1103 SeekCur = libc::SEEK_CUR,
1105 SeekEnd = libc::SEEK_END,
1114 SeekData = libc::SEEK_DATA,
1125 SeekHole = libc::SEEK_HOLE
1131 pub fn lseek(fd: RawFd, offset: off_t, whence: Whence) -> Result<off_t> {
1132 let res = unsafe { libc::lseek(fd, offset, whence as i32) };
1138 pub fn lseek64(fd: RawFd, offset: libc::off64_t, whence: Whence) -> Result<libc::off64_t> {
1139 let res = unsafe { libc::lseek64(fd, offset, whence as i32) };
1141 Errno::result(res).map(|r| r as libc::off64_t)
1148 pub fn pipe() -> std::result::Result<(RawFd, RawFd), Error> { in pipe()
1151 let res = unsafe { libc::pipe(fds.as_mut_ptr() as *mut c_int) }; in pipe()
1165 /// - `O_CLOEXEC`: Set the close-on-exec flag for the new file descriptors.
1166 #[cfg_attr(target_os = "linux", doc = "- `O_DIRECT`: Create a pipe that performs I/O in \"packet\" …
1167 #[cfg_attr(target_os = "netbsd", doc = "- `O_NOSIGPIPE`: Return `EPIPE` instead of raising `SIGPIPE…
1168 /// - `O_NONBLOCK`: Set the non-blocking flag for the ends of the pipe.
1170 /// See also [pipe(2)](https://man7.org/linux/man-pages/man2/pipe.2.html)
1181 pub fn pipe2(flags: OFlag) -> Result<(RawFd, RawFd)> {
1185 libc::pipe2(fds.as_mut_ptr() as *mut c_int, flags.bits())
1198 pub fn truncate<P: ?Sized + NixPath>(path: &P, len: off_t) -> Result<()> {
1201 libc::truncate(cstr.as_ptr(), len)
1212 pub fn ftruncate(fd: RawFd, len: off_t) -> Result<()> {
1213 Errno::result(unsafe { libc::ftruncate(fd, len) }).map(drop)
1216 pub fn isatty(fd: RawFd) -> Result<bool> {
1220 if libc::isatty(fd) == 1 {
1258 ) -> Result<()> {
1270 libc::linkat(
1275 atflag.bits() as libc::c_int
1287 pub fn unlink<P: ?Sized + NixPath>(path: &P) -> Result<()> {
1290 libc::unlink(cstr.as_ptr())
1318 ) -> Result<()> {
1326 libc::unlinkat(at_rawfd(dirfd), cstr.as_ptr(), atflag.bits() as libc::c_int)
1335 pub fn chroot<P: ?Sized + NixPath>(path: &P) -> Result<()> {
1337 unsafe { libc::chroot(cstr.as_ptr()) }
1354 unsafe { libc::sync() };
1360 /// See also [syncfs(2)](https://man7.org/linux/man-pages/man2/sync.2.html)
1362 pub fn syncfs(fd: RawFd) -> Result<()> {
1363 let res = unsafe { libc::syncfs(fd) };
1372 pub fn fsync(fd: RawFd) -> Result<()> {
1373 let res = unsafe { libc::fsync(fd) };
1392 pub fn fdatasync(fd: RawFd) -> Result<()> {
1393 let res = unsafe { libc::fdatasync(fd) };
1408 pub fn getuid() -> Uid {
1409 Uid(unsafe { libc::getuid() })
1418 pub fn geteuid() -> Uid {
1419 Uid(unsafe { libc::geteuid() })
1428 pub fn getgid() -> Gid {
1429 Gid(unsafe { libc::getgid() })
1438 pub fn getegid() -> Gid {
1439 Gid(unsafe { libc::getegid() })
1446 pub fn seteuid(euid: Uid) -> Result<()> {
1447 let res = unsafe { libc::seteuid(euid.into()) };
1456 pub fn setegid(egid: Gid) -> Result<()> {
1457 let res = unsafe { libc::setegid(egid.into()) };
1466 pub fn setuid(uid: Uid) -> Result<()> {
1467 let res = unsafe { libc::setuid(uid.into()) };
1476 pub fn setgid(gid: Gid) -> Result<()> {
1477 let res = unsafe { libc::setgid(gid.into()) };
1485 /// Set the user identity used for filesystem checks per-thread.
1489 /// See also [setfsuid(2)](https://man7.org/linux/man-pages/man2/setfsuid.2.html)
1491 pub fn setfsuid(uid: Uid) -> Uid {
1492 let prev_fsuid = unsafe { libc::setfsuid(uid.into()) };
1496 /// Set the group identity used for filesystem checks per-thread.
1500 /// See also [setfsgid(2)](https://man7.org/linux/man-pages/man2/setfsgid.2.html)
1502 pub fn setfsgid(gid: Gid) -> Gid {
1503 let prev_fsgid = unsafe { libc::setfsgid(gid.into()) };
1519 pub fn getgroups() -> Result<Vec<Gid>> {
1529 let ngroups = unsafe { libc::getgroups(0, ptr::null_mut()) };
1532 // This prevents a potential buffer over-read if the number of groups
1548 libc::getgroups(groups.capacity() as c_int, groups.as_mut_ptr() as *mut gid_t)
1569 /// [Further reading](https://man7.org/linux/man-pages/man2/getgroups.2.html)
1578 /// specific user and group. For example, given the user `www-data` with UID
1583 /// # use std::error::Error;
1584 /// # use nix::unistd::*;
1586 /// # fn try_main() -> Result<(), Box<dyn Error>> {
1599 pub fn setgroups(groups: &[Gid]) -> Result<()> {
1618 libc::setgroups(groups.len() as setgroups_ngroups_t, groups.as_ptr() as *const gid_t)
1629 /// [Further reading](https://man7.org/linux/man-pages/man3/getgrouplist.3.html)
1648 pub fn getgrouplist(user: &CStr, group: Gid) -> Result<Vec<Gid>> {
1653 use std::cmp::min;
1666 libc::getgrouplist(user.as_ptr(),
1672 // BSD systems only return 0 or -1, Linux returns ngroups on success.
1676 } else if ret == -1 {
1677 // Returns -1 if ngroups is too small, but does not set errno.
1693 /// [Further reading](https://man7.org/linux/man-pages/man3/initgroups.3.html)
1702 /// another user. For example, given the user `www-data`, we could look up the
1704 /// in `/etc/passwd`). If the `www-data` user's UID and GID were `33` and `33`,
1708 /// # use std::error::Error;
1709 /// # use std::ffi::CString;
1710 /// # use nix::unistd::*;
1712 /// # fn try_main() -> Result<(), Box<dyn Error>> {
1713 /// let user = CString::new("www-data").unwrap();
1726 pub fn initgroups(user: &CStr, group: Gid) -> Result<()> {
1735 let res = unsafe { libc::initgroups(user.as_ptr(), gid as initgroups_group_t) };
1750 unsafe { libc::pause() };
1767 //! use nix::unistd::alarm;
1781 //! use std::time::{Duration, Instant};
1783 //! use nix::unistd::{alarm, pause};
1784 //! use nix::sys::signal::*;
1788 //! extern fn signal_handler(_: nix::libc::c_int) { }
1821 pub fn set(secs: libc::c_uint) -> Option<libc::c_uint> {
1822 …assert!(secs != 0, "passing 0 to `alarm::set` is not allowed, to cancel an alarm use `alarm::cance…
1829 pub fn cancel() -> Option<libc::c_uint> {
1833 fn alarm(secs: libc::c_uint) -> Option<libc::c_uint> {
1834 match unsafe { libc::alarm(secs) } {
1847 pub fn sleep(seconds: c_uint) -> c_uint { in sleep()
1848 unsafe { libc::sleep(seconds) } in sleep()
1856 use crate::{Result, NixPath};
1857 use crate::errno::Errno;
1858 use std::ptr;
1863 pub fn enable<P: ?Sized + NixPath>(filename: &P) -> Result<()> {
1865 unsafe { libc::acct(cstr.as_ptr()) }
1872 pub fn disable() -> Result<()> {
1873 let res = unsafe { libc::acct(ptr::null()) };
1895 /// use nix::unistd;
1907 pub fn mkstemp<P: ?Sized + NixPath>(template: &P) -> Result<(RawFd, PathBuf)> {
1910 let fd = unsafe { libc::mkstemp(p) };
1930 /// POSIX 1003.1-2008 standardizes all of these variables, but some OSes choose
1935 /// - [pathconf(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/pathconf.html)
1936 /// - [limits.h](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/limits.h.html)
1937 /// - [unistd.h](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html)
1947 FILESIZEBITS = libc::_PC_FILESIZEBITS,
1949 LINK_MAX = libc::_PC_LINK_MAX,
1951 MAX_CANON = libc::_PC_MAX_CANON,
1955 MAX_INPUT = libc::_PC_MAX_INPUT,
1958 NAME_MAX = libc::_PC_NAME_MAX,
1960 /// user-supplied buffer of unspecified size, including the terminating null
1963 PATH_MAX = libc::_PC_PATH_MAX,
1966 PIPE_BUF = libc::_PC_PIPE_BUF,
1972 POSIX2_SYMLINKS = libc::_PC_2_SYMLINKS,
1978 POSIX_ALLOC_SIZE_MIN = libc::_PC_ALLOC_SIZE_MIN,
1984 POSIX_REC_INCR_XFER_SIZE = libc::_PC_REC_INCR_XFER_SIZE,
1989 POSIX_REC_MAX_XFER_SIZE = libc::_PC_REC_MAX_XFER_SIZE,
1994 POSIX_REC_MIN_XFER_SIZE = libc::_PC_REC_MIN_XFER_SIZE,
1999 POSIX_REC_XFER_ALIGN = libc::_PC_REC_XFER_ALIGN,
2005 SYMLINK_MAX = libc::_PC_SYMLINK_MAX,
2006 /// The use of `chown` and `fchown` is restricted to a process with
2010 _POSIX_CHOWN_RESTRICTED = libc::_PC_CHOWN_RESTRICTED,
2012 _POSIX_NO_TRUNC = libc::_PC_NO_TRUNC,
2015 _POSIX_VDISABLE = libc::_PC_VDISABLE,
2022 _POSIX_ASYNC_IO = libc::_PC_ASYNC_IO,
2029 _POSIX_PRIO_IO = libc::_PC_PRIO_IO,
2036 _POSIX_SYNC_IO = libc::_PC_SYNC_IO,
2040 _POSIX_TIMESTAMP_RESOLUTION = libc::_PC_TIMESTAMP_RESOLUTION
2048 /// - `fd`: The file descriptor whose variable should be interrogated
2049 /// - `var`: The pathconf variable to lookup
2053 /// - `Ok(Some(x))`: the variable's limit (for limit variables) or its
2055 /// usually a decimal-coded date, such as 200112 for POSIX 2001.12
2056 /// - `Ok(None)`: the variable has no limit (for limit variables) or is
2058 /// - `Err(x)`: an error occurred
2059 pub fn fpathconf(fd: RawFd, var: PathconfVar) -> Result<Option<c_long>> {
2062 libc::fpathconf(fd, var as c_int)
2064 if raw == -1 {
2075 /// Get path-dependent configurable system variables (see
2078 /// Returns the value of a path-dependent configurable system variable. Most
2079 /// supported variables also have associated compile-time constants, but POSIX
2085 /// - `path`: Lookup the value of `var` for this file or directory
2086 /// - `var`: The `pathconf` variable to lookup
2090 /// - `Ok(Some(x))`: the variable's limit (for limit variables) or its
2092 /// usually a decimal-coded date, such as 200112 for POSIX 2001.12
2093 /// - `Ok(None)`: the variable has no limit (for limit variables) or is
2095 /// - `Err(x)`: an error occurred
2096 pub fn pathconf<P: ?Sized + NixPath>(path: &P, var: PathconfVar) -> Result<Option<c_long>> {
2100 libc::pathconf(cstr.as_ptr(), var as c_int)
2103 if raw == -1 {
2126 /// All of these symbols are standardized by POSIX 1003.1-2008, but haven't been
2131 /// - [sysconf(3)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/sysconf.html)
2132 /// - [unistd.h](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html)
2133 /// - [limits.h](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/limits.h.html)
2142 AIO_LISTIO_MAX = libc::_SC_AIO_LISTIO_MAX,
2147 AIO_MAX = libc::_SC_AIO_MAX,
2154 AIO_PRIO_DELTA_MAX = libc::_SC_AIO_PRIO_DELTA_MAX,
2156 ARG_MAX = libc::_SC_ARG_MAX,
2160 ATEXIT_MAX = libc::_SC_ATEXIT_MAX,
2164 BC_BASE_MAX = libc::_SC_BC_BASE_MAX,
2168 BC_DIM_MAX = libc::_SC_BC_DIM_MAX,
2172 BC_SCALE_MAX = libc::_SC_BC_SCALE_MAX,
2176 BC_STRING_MAX = libc::_SC_BC_STRING_MAX,
2178 CHILD_MAX = libc::_SC_CHILD_MAX,
2180 CLK_TCK = libc::_SC_CLK_TCK,
2185 COLL_WEIGHTS_MAX = libc::_SC_COLL_WEIGHTS_MAX,
2189 DELAYTIMER_MAX = libc::_SC_DELAYTIMER_MAX,
2194 EXPR_NEST_MAX = libc::_SC_EXPR_NEST_MAX,
2201 HOST_NAME_MAX = libc::_SC_HOST_NAME_MAX,
2203 /// use with `readv` or `writev`.
2206 IOV_MAX = libc::_SC_IOV_MAX,
2213 LINE_MAX = libc::_SC_LINE_MAX,
2216 LOGIN_NAME_MAX = libc::_SC_LOGIN_NAME_MAX,
2218 NGROUPS_MAX = libc::_SC_NGROUPS_MAX,
2222 GETGR_R_SIZE_MAX = libc::_SC_GETGR_R_SIZE_MAX,
2226 GETPW_R_SIZE_MAX = libc::_SC_GETPW_R_SIZE_MAX,
2230 MQ_OPEN_MAX = libc::_SC_MQ_OPEN_MAX,
2234 MQ_PRIO_MAX = libc::_SC_MQ_PRIO_MAX,
2236 /// a newly-created file descriptor.
2237 OPEN_MAX = libc::_SC_OPEN_MAX,
2242 _POSIX_ADVISORY_INFO = libc::_SC_ADVISORY_INFO,
2248 _POSIX_BARRIERS = libc::_SC_BARRIERS,
2252 _POSIX_ASYNCHRONOUS_IO = libc::_SC_ASYNCHRONOUS_IO,
2258 _POSIX_CLOCK_SELECTION = libc::_SC_CLOCK_SELECTION,
2263 /// The implementation supports the Process CPU-Time Clocks option.
2264 _POSIX_CPUTIME = libc::_SC_CPUTIME,
2268 _POSIX_FSYNC = libc::_SC_FSYNC,
2274 _POSIX_IPV6 = libc::_SC_IPV6,
2278 _POSIX_JOB_CONTROL = libc::_SC_JOB_CONTROL,
2282 _POSIX_MAPPED_FILES = libc::_SC_MAPPED_FILES,
2286 _POSIX_MEMLOCK = libc::_SC_MEMLOCK,
2290 _POSIX_MEMLOCK_RANGE = libc::_SC_MEMLOCK_RANGE,
2294 _POSIX_MEMORY_PROTECTION = libc::_SC_MEMORY_PROTECTION,
2298 _POSIX_MESSAGE_PASSING = libc::_SC_MESSAGE_PASSING,
2302 _POSIX_MONOTONIC_CLOCK = libc::_SC_MONOTONIC_CLOCK,
2308 _POSIX_PRIORITIZED_IO = libc::_SC_PRIORITIZED_IO,
2312 _POSIX_PRIORITY_SCHEDULING = libc::_SC_PRIORITY_SCHEDULING,
2318 _POSIX_RAW_SOCKETS = libc::_SC_RAW_SOCKETS,
2323 /// The implementation supports read-write locks.
2324 _POSIX_READER_WRITER_LOCKS = libc::_SC_READER_WRITER_LOCKS,
2330 _POSIX_REALTIME_SIGNALS = libc::_SC_REALTIME_SIGNALS,
2336 _POSIX_REGEXP = libc::_SC_REGEXP,
2337 /// Each process has a saved set-user-ID and a saved set-group-ID.
2340 _POSIX_SAVED_IDS = libc::_SC_SAVED_IDS,
2344 _POSIX_SEMAPHORES = libc::_SC_SEMAPHORES,
2348 _POSIX_SHARED_MEMORY_OBJECTS = libc::_SC_SHARED_MEMORY_OBJECTS,
2354 _POSIX_SHELL = libc::_SC_SHELL,
2360 _POSIX_SPAWN = libc::_SC_SPAWN,
2366 _POSIX_SPIN_LOCKS = libc::_SC_SPIN_LOCKS,
2371 _POSIX_SPORADIC_SERVER = libc::_SC_SPORADIC_SERVER,
2375 _POSIX_SS_REPL_MAX = libc::_SC_SS_REPL_MAX,
2379 _POSIX_SYNCHRONIZED_IO = libc::_SC_SYNCHRONIZED_IO,
2383 _POSIX_THREAD_ATTR_STACKADDR = libc::_SC_THREAD_ATTR_STACKADDR,
2387 _POSIX_THREAD_ATTR_STACKSIZE = libc::_SC_THREAD_ATTR_STACKSIZE,
2391 /// The implementation supports the Thread CPU-Time Clocks option.
2392 _POSIX_THREAD_CPUTIME = libc::_SC_THREAD_CPUTIME,
2393 /// The implementation supports the Non-Robust Mutex Priority Inheritance
2397 _POSIX_THREAD_PRIO_INHERIT = libc::_SC_THREAD_PRIO_INHERIT,
2398 /// The implementation supports the Non-Robust Mutex Priority Protection option.
2401 _POSIX_THREAD_PRIO_PROTECT = libc::_SC_THREAD_PRIO_PROTECT,
2405 _POSIX_THREAD_PRIORITY_SCHEDULING = libc::_SC_THREAD_PRIORITY_SCHEDULING,
2410 /// The implementation supports the Thread Process-Shared Synchronization
2412 _POSIX_THREAD_PROCESS_SHARED = libc::_SC_THREAD_PROCESS_SHARED,
2416 _POSIX_THREAD_ROBUST_PRIO_INHERIT = libc::_SC_THREAD_ROBUST_PRIO_INHERIT,
2420 _POSIX_THREAD_ROBUST_PRIO_PROTECT = libc::_SC_THREAD_ROBUST_PRIO_PROTECT,
2421 /// The implementation supports thread-safe functions.
2424 _POSIX_THREAD_SAFE_FUNCTIONS = libc::_SC_THREAD_SAFE_FUNCTIONS,
2429 _POSIX_THREAD_SPORADIC_SERVER = libc::_SC_THREAD_SPORADIC_SERVER,
2433 _POSIX_THREADS = libc::_SC_THREADS,
2438 _POSIX_TIMEOUTS = libc::_SC_TIMEOUTS,
2442 _POSIX_TIMERS = libc::_SC_TIMERS,
2447 _POSIX_TRACE = libc::_SC_TRACE,
2452 _POSIX_TRACE_EVENT_FILTER = libc::_SC_TRACE_EVENT_FILTER,
2456 _POSIX_TRACE_EVENT_NAME_MAX = libc::_SC_TRACE_EVENT_NAME_MAX,
2461 _POSIX_TRACE_INHERIT = libc::_SC_TRACE_INHERIT,
2466 _POSIX_TRACE_LOG = libc::_SC_TRACE_LOG,
2470 _POSIX_TRACE_NAME_MAX = libc::_SC_TRACE_NAME_MAX,
2474 _POSIX_TRACE_SYS_MAX = libc::_SC_TRACE_SYS_MAX,
2478 _POSIX_TRACE_USER_EVENT_MAX = libc::_SC_TRACE_USER_EVENT_MAX,
2483 _POSIX_TYPED_MEMORY_OBJECTS = libc::_SC_TYPED_MEMORY_OBJECTS,
2484 /// Integer value indicating version of this standard (C-language binding)
2486 /// POSIX.1-2008, the value shall be 200809L.
2487 _POSIX_VERSION = libc::_SC_VERSION,
2492 /// The implementation provides a C-language compilation environment with
2493 /// 32-bit `int`, `long`, `pointer`, and `off_t` types.
2494 _POSIX_V6_ILP32_OFF32 = libc::_SC_V6_ILP32_OFF32,
2499 /// The implementation provides a C-language compilation environment with
2500 /// 32-bit `int`, `long`, and pointer types and an `off_t` type using at
2502 _POSIX_V6_ILP32_OFFBIG = libc::_SC_V6_ILP32_OFFBIG,
2507 /// The implementation provides a C-language compilation environment with
2508 /// 32-bit `int` and 64-bit `long`, `pointer`, and `off_t` types.
2509 _POSIX_V6_LP64_OFF64 = libc::_SC_V6_LP64_OFF64,
2514 /// The implementation provides a C-language compilation environment with an
2517 _POSIX_V6_LPBIG_OFFBIG = libc::_SC_V6_LPBIG_OFFBIG,
2518 /// The implementation supports the C-Language Binding option.
2521 _POSIX2_C_BIND = libc::_SC_2_C_BIND,
2522 /// The implementation supports the C-Language Development Utilities option.
2525 _POSIX2_C_DEV = libc::_SC_2_C_DEV,
2529 _POSIX2_CHAR_TERM = libc::_SC_2_CHAR_TERM,
2533 _POSIX2_FORT_DEV = libc::_SC_2_FORT_DEV,
2537 _POSIX2_FORT_RUN = libc::_SC_2_FORT_RUN,
2542 _POSIX2_LOCALEDEF = libc::_SC_2_LOCALEDEF,
2549 _POSIX2_PBS = libc::_SC_2_PBS,
2555 _POSIX2_PBS_ACCOUNTING = libc::_SC_2_PBS_ACCOUNTING,
2561 _POSIX2_PBS_CHECKPOINT = libc::_SC_2_PBS_CHECKPOINT,
2567 _POSIX2_PBS_LOCATE = libc::_SC_2_PBS_LOCATE,
2573 _POSIX2_PBS_MESSAGE = libc::_SC_2_PBS_MESSAGE,
2579 _POSIX2_PBS_TRACK = libc::_SC_2_PBS_TRACK,
2583 _POSIX2_SW_DEV = libc::_SC_2_SW_DEV,
2587 _POSIX2_UPE = libc::_SC_2_UPE,
2592 _POSIX2_VERSION = libc::_SC_2_VERSION,
2597 PAGE_SIZE = libc::_SC_PAGE_SIZE,
2600 PTHREAD_DESTRUCTOR_ITERATIONS = libc::_SC_THREAD_DESTRUCTOR_ITERATIONS,
2603 PTHREAD_KEYS_MAX = libc::_SC_THREAD_KEYS_MAX,
2606 PTHREAD_STACK_MIN = libc::_SC_THREAD_STACK_MIN,
2609 PTHREAD_THREADS_MAX = libc::_SC_THREAD_THREADS_MAX,
2611 RE_DUP_MAX = libc::_SC_RE_DUP_MAX,
2616 RTSIG_MAX = libc::_SC_RTSIG_MAX,
2619 SEM_NSEMS_MAX = libc::_SC_SEM_NSEMS_MAX,
2624 SEM_VALUE_MAX = libc::_SC_SEM_VALUE_MAX,
2629 SIGQUEUE_MAX = libc::_SC_SIGQUEUE_MAX,
2630 STREAM_MAX = libc::_SC_STREAM_MAX,
2635 SYMLOOP_MAX = libc::_SC_SYMLOOP_MAX,
2638 TIMER_MAX = libc::_SC_TIMER_MAX,
2639 TTY_NAME_MAX = libc::_SC_TTY_NAME_MAX,
2640 TZNAME_MAX = libc::_SC_TZNAME_MAX,
2646 _XOPEN_CRYPT = libc::_SC_XOPEN_CRYPT,
2653 _XOPEN_ENH_I18N = libc::_SC_XOPEN_ENH_I18N,
2658 _XOPEN_LEGACY = libc::_SC_XOPEN_LEGACY,
2664 _XOPEN_REALTIME = libc::_SC_XOPEN_REALTIME,
2670 _XOPEN_REALTIME_THREADS = libc::_SC_XOPEN_REALTIME_THREADS,
2675 _XOPEN_SHM = libc::_SC_XOPEN_SHM,
2680 _XOPEN_STREAMS = libc::_SC_XOPEN_STREAMS,
2686 _XOPEN_UNIX = libc::_SC_XOPEN_UNIX,
2693 _XOPEN_VERSION = libc::_SC_XOPEN_VERSION,
2697 _PHYS_PAGES = libc::_SC_PHYS_PAGES,
2700 _AVPHYS_PAGES = libc::_SC_AVPHYS_PAGES,
2703 _NPROCESSORS_CONF = libc::_SC_NPROCESSORS_CONF,
2706 _NPROCESSORS_ONLN = libc::_SC_NPROCESSORS_ONLN,
2713 /// variables also have associated compile-time constants, but POSIX
2719 /// - `Ok(Some(x))`: the variable's limit (for limit variables) or its
2721 /// usually a decimal-coded date, such as 200112 for POSIX 2001.12
2722 /// - `Ok(None)`: the variable has no limit (for limit variables) or is
2724 /// - `Err(x)`: an error occurred
2725 pub fn sysconf(var: SysconfVar) -> Result<Option<c_long>> {
2728 libc::sysconf(var as c_int)
2730 if raw == -1 {
2747 use crate::{Result, NixPath};
2748 use crate::errno::Errno;
2751 new_root: &P1, put_old: &P2) -> Result<()> {
2755 libc::syscall(libc::SYS_pivot_root, new_root.as_ptr(), put_old.as_ptr())
2776 use crate::Result;
2777 use crate::errno::Errno;
2778 use super::{Uid, Gid};
2781 /// ([see setresuid(2)](https://man7.org/linux/man-pages/man2/setresuid.2.html))
2786 /// * returns: Ok or libc error code.
2790 pub fn setresuid(ruid: Uid, euid: Uid, suid: Uid) -> Result<()> {
2791 let res = unsafe { libc::setresuid(ruid.into(), euid.into(), suid.into()) };
2797 /// ([see setresuid(2)](https://man7.org/linux/man-pages/man2/setresuid.2.html))
2802 /// * returns: Ok or libc error code.
2806 pub fn setresgid(rgid: Gid, egid: Gid, sgid: Gid) -> Result<()> {
2807 let res = unsafe { libc::setresgid(rgid.into(), egid.into(), sgid.into()) };
2825 use crate::Result;
2826 use crate::errno::Errno;
2827 use super::{Uid, Gid};
2847 /// ([see getresuid(2)](http://man7.org/linux/man-pages/man2/getresuid.2.html))
2851 /// - `Ok((Uid, Uid, Uid))`: tuple of real, effective and saved uids on success.
2852 /// - `Err(x)`: libc error code on failure.
2855 pub fn getresuid() -> Result<ResUid> {
2856 let mut ruid = libc::uid_t::max_value();
2857 let mut euid = libc::uid_t::max_value();
2858 let mut suid = libc::uid_t::max_value();
2859 let res = unsafe { libc::getresuid(&mut ruid, &mut euid, &mut suid) };
2866 /// ([see getresgid(2)](http://man7.org/linux/man-pages/man2/getresgid.2.html))
2870 /// - `Ok((Gid, Gid, Gid))`: tuple of real, effective and saved gids on success.
2871 /// - `Err(x)`: libc error code on failure.
2874 pub fn getresgid() -> Result<ResGid> {
2875 let mut rgid = libc::gid_t::max_value();
2876 let mut egid = libc::gid_t::max_value();
2877 let mut sgid = libc::gid_t::max_value();
2878 let res = unsafe { libc::getresgid(&mut rgid, &mut egid, &mut sgid) };
2906 pub fn access<P: ?Sized + NixPath>(path: &P, amode: AccessFlags) -> Result<()> {
2909 libc::access(cstr.as_ptr(), amode.bits)
2926 …Sized + NixPath>(dirfd: Option<RawFd>, path: &P, mode: AccessFlags, flags: AtFlags) -> Result<()> {
2929 libc::faccessat(at_rawfd(dirfd), cstr.as_ptr(), mode.bits(), flags.bits())
2941 /// * [Linux man page](https://man7.org/linux/man-pages/man3/euidaccess.3.html)
2947 pub fn eaccess<P: ?Sized + NixPath>(path: &P, mode: AccessFlags) -> Result<()> {
2950 libc::eaccess(cstr.as_ptr(), mode.bits)
2960 /// Representation of a User, based on `libc::passwd`
2963 /// fields are based on the user's locale, which could be non-UTF8, while other fields are
3001 pub change: libc::time_t,
3010 pub expire: libc::time_t
3014 impl From<&libc::passwd> for User {
3015 fn from(pw: &libc::passwd) -> User {
3053 impl From<User> for libc::passwd {
3054 fn from(u: User) -> Self {
3109 fn from_anything<F>(f: F) -> Result<Option<Self>>
3111 F: Fn(*mut libc::passwd,
3113 libc::size_t,
3114 *mut *mut libc::passwd) -> libc::c_int
3123 let mut pwd = mem::MaybeUninit::<libc::passwd>::uninit();
3152 /// use nix::unistd::{Uid, User};
3157 pub fn from_uid(uid: Uid) -> Result<Option<Self>> {
3159 unsafe { libc::getpwuid_r(uid.0, pwd, cbuf, cap, res) }
3171 /// use nix::unistd::User;
3176 pub fn from_name(name: &str) -> Result<Option<Self>> {
3182 unsafe { libc::getpwnam_r(name.as_ptr(), pwd, cbuf, cap, res) }
3187 /// Representation of a Group, based on `libc::group`
3202 impl From<&libc::group> for Group {
3203 fn from(gr: &libc::group) -> Group {
3217 unsafe fn members(mem: *mut *mut c_char) -> Vec<String> {
3233 fn from_anything<F>(f: F) -> Result<Option<Self>>
3235 F: Fn(*mut libc::group,
3237 libc::size_t,
3238 *mut *mut libc::group) -> libc::c_int
3247 let mut grp = mem::MaybeUninit::<libc::group>::uninit();
3278 /// use nix::unistd::{Gid, Group};
3283 pub fn from_gid(gid: Gid) -> Result<Option<Self>> {
3285 unsafe { libc::getgrgid_r(gid.0, grp, cbuf, cap, res) }
3299 /// use nix::unistd::Group;
3304 pub fn from_name(name: &str) -> Result<Option<Self>> {
3310 unsafe { libc::getgrnam_r(name.as_ptr(), grp, cbuf, cap, res) }
3320 /// (see [`ttyname(3)`](https://man7.org/linux/man-pages/man3/ttyname.3.html)).
3322 pub fn ttyname(fd: RawFd) -> Result<PathBuf> {
3323 const PATH_MAX: usize = libc::PATH_MAX as usize;
3325 let c_buf = buf.as_mut_ptr() as *mut libc::c_char;
3327 let ret = unsafe { libc::ttyname_r(fd, c_buf, buf.len()) };
3352 pub fn getpeereid(fd: RawFd) -> Result<(Uid, Gid)> {
3356 let ret = unsafe { libc::getpeereid(fd, &mut uid, &mut gid) };
3376 pub fn chflags<P: ?Sized + NixPath>(path: &P, flags: FileFlag) -> Result<()> {
3378 libc::chflags(cstr.as_ptr(), flags.bits())