Debian Codegraph @

src/system/mod.rs

raw symbols SCIP
1 // TODO: remove unused attribute when system is cleaned up
2 #[cfg(target_os = "linux")]
3 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#;
4 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{
5 ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::{CStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/CStr#, c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#, c_longrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_long#, c_uintrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_uint#},
6 fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/, fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/, iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/,
7 memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#,
8 opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/,
9 osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/::unixrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/,
10 pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#,
11 };
12
13 use craterust-analyzer cargo sudo-rs 0.2.14 crate/::{
14 commonrust-analyzer cargo sudo-rs 0.2.14 common/::{Errorrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#, SudoPathrust-analyzer cargo sudo-rs 0.2.14 common/path/SudoPath#, SudoStringrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString#},
15 cutilsrust-analyzer cargo sudo-rs 0.2.14 cutils/::*,
16 };
17 use interfacerust-analyzer cargo sudo-rs 0.2.14 system/interface/::{DeviceIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/DeviceId#, GroupIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/GroupId#, ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#, UserIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/UserId#};
18 pub use libcrust-analyzer cargo libc 0.2.186 crate/::PATH_MAXrust-analyzer cargo libc 0.2.186 unix/linux_like/PATH_MAX.;
19 use libcrust-analyzer cargo libc 0.2.186 crate/::{CLOSE_RANGE_CLOEXECrust-analyzer cargo libc 0.2.186 unix/linux_like/linux/CLOSE_RANGE_CLOEXEC., EINVALrust-analyzer cargo libc 0.2.186 unix/linux_like/linux_l4re_shared/EINVAL., ENOSYSrust-analyzer cargo libc 0.2.186 unix/linux_like/linux/gnu/b64/x86_64/ENOSYS., STDERR_FILENOrust-analyzer cargo libc 0.2.186 new/common/posix/unistd/STDERR_FILENO.};
20 use timerust-analyzer cargo sudo-rs 0.2.14 system/time/::ProcessCreateTimerust-analyzer cargo sudo-rs 0.2.14 system/time/ProcessCreateTime#;
21
22 use selfrust-analyzer cargo sudo-rs 0.2.14 system/::signalrust-analyzer cargo sudo-rs 0.2.14 system/signal/::SignalNumberrust-analyzer cargo sudo-rs 0.2.14 system/signal/SignalNumber#;
23
24 pub(craterust-analyzer cargo sudo-rs 0.2.14 crate/) mod auditrust-analyzer cargo sudo-rs 0.2.14 system/audit/;
25 // generalized traits for when we want to hide implementations
26 pub mod interfacerust-analyzer cargo sudo-rs 0.2.14 system/interface/;
27
28 pub mod filerust-analyzer cargo sudo-rs 0.2.14 system/file/;
29
30 pub mod timerust-analyzer cargo sudo-rs 0.2.14 system/time/;
31
32 pub mod timestamprust-analyzer cargo sudo-rs 0.2.14 system/timestamp/;
33
34 pub mod signalrust-analyzer cargo sudo-rs 0.2.14 system/signal/;
35
36 pub mod termrust-analyzer cargo sudo-rs 0.2.14 system/term/;
37
38 pub mod waitrust-analyzer cargo sudo-rs 0.2.14 system/wait/;
39
40 #[cfg(not(any(target_os = "freebsd", target_os = "linux")))]
41 compile_errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/compile_error!!("sudo-rs only works on Linux and FreeBSD");
42
43 pub(craterust-analyzer cargo sudo-rs 0.2.14 crate/) fn _exitrust-analyzer cargo sudo-rs 0.2.14 system/_exit().(status: c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#) -> ! {
44 // SAFETY: this function is safe to call
45 unsafe { libcrust-analyzer cargo libc 0.2.186 crate/::_exitrust-analyzer cargo libc 0.2.186 _exit().(status) }
46 }
47
48 /// Mark every file descriptor that is not one of the IO streams as CLOEXEC.
49 pub(craterust-analyzer cargo sudo-rs 0.2.14 crate/) fn mark_fds_as_cloexecrust-analyzer cargo sudo-rs 0.2.14 system/mark_fds_as_cloexec().() -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> {
50 let lowfd = STDERR_FILENOrust-analyzer cargo libc 0.2.186 new/common/posix/unistd/STDERR_FILENO. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Add<Self>`]add().1;
51
52 // SAFETY: this function is safe to call:
53 // - any errors while closing a specific fd will be effectively ignored
54 #[allow(clippy::diverging_sub_expression)]
55 let res = unsafe {
56 'a: {
57 #[cfg(not(target_os = "linux"))]
58 break 'a cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().(libcrust-analyzer cargo libc 0.2.186 crate/::close_rangerust-analyzer cargo libc 0.2.186 close_range().(
59 lowfd as c_uintrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_uint#,
60 c_uintrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_uint#::MAX,
61 CLOSE_RANGE_CLOEXECrust-analyzer cargo libc 0.2.186 unix/linux_like/linux/CLOSE_RANGE_CLOEXEC. as c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#,
62 ));
63 // on Linux, close_range was only added in glibc 2.34, and is not
64 // part of musl, so we go perform a straight syscall instead
65 #[cfg(target_os = "linux")]
66 break 'a cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().(libcrust-analyzer cargo libc 0.2.186 crate/::syscallrust-analyzer cargo libc 0.2.186 syscall().(
67 libcrust-analyzer cargo libc 0.2.186 crate/::SYS_close_rangerust-analyzer cargo libc 0.2.186 unix/linux_like/linux/gnu/b64/x86_64/not_x32/SYS_close_range.,
68 lowfd as c_uintrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_uint#,
69 c_uintrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_uint#::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]MAX.,
70 CLOSE_RANGE_CLOEXECrust-analyzer cargo libc 0.2.186 unix/linux_like/linux/CLOSE_RANGE_CLOEXEC. as c_uintrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_uint#,
71 ));
72 }
73 };
74
75 match res {
76 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) if err.raw_os_errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]raw_os_error().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#eq().Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ENOSYSrust-analyzer cargo libc 0.2.186 unix/linux_like/linux/gnu/b64/x86_64/ENOSYS.) || err.raw_os_errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]raw_os_error().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#eq().Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(EINVALrust-analyzer cargo libc 0.2.186 unix/linux_like/linux_l4re_shared/EINVAL.) => {
77 // The kernel doesn't support close_range or CLOSE_RANGE_CLOEXEC,
78 // fallback to finding all open fds using /proc/self/fd.
79
80 // FIXME use /dev/fd on macOS
81 for entry in fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::read_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/read_dir().("/proc/self/fd")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). {
82 let entry = entry?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
83 let file_name = entry.file_namerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[DirEntry]file_name().();
84 let file_name = file_name.to_strrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsStr]to_str().().ok_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or().(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(
85 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::InvalidDatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#InvalidData#,
86 "procfs returned non-integer fd name",
87 ))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
88 if file_name rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq()."." || file_name rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().".." {
89 continue;
90 }
91 let fd = file_name.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#>().map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|_| {
92 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(
93 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::InvalidDatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#InvalidData#,
94 "procfs returned non-integer fd name",
95 )
96 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
97 if fd rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialOrd<Self>`]lt().lowfd {
98 continue;
99 }
100 // SAFETY: This only sets the CLOEXEC flag for the given fd. Nothing is
101 // going to need it after exec.
102 unsafe {
103 cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().(libcrust-analyzer cargo libc 0.2.186 crate/::fcntlrust-analyzer cargo libc 0.2.186 fcntl().(fd, libcrust-analyzer cargo libc 0.2.186 crate/::F_SETFDrust-analyzer cargo libc 0.2.186 unix/linux_like/F_SETFD., libcrust-analyzer cargo libc 0.2.186 crate/::FD_CLOEXECrust-analyzer cargo libc 0.2.186 unix/FD_CLOEXEC.))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
104 }
105 }
106
107 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
108 }
109 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err),
110 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(_) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()),
111 }
112 }
113
114 pub(craterust-analyzer cargo sudo-rs 0.2.14 crate/) enum ForkResultrust-analyzer cargo sudo-rs 0.2.14 system/ForkResult# {
115 // Parent process branch with the child process' PID.
116 Parentrust-analyzer cargo sudo-rs 0.2.14 system/ForkResult#Parent#(ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#),
117 // Child process branch.
118 Childrust-analyzer cargo sudo-rs 0.2.14 system/ForkResult#Child#,
119 }
120
121 /// Create a new process.
122 ///
123 /// # Safety
124 ///
125 /// Must not be called in multithreaded programs.
126 pub(craterust-analyzer cargo sudo-rs 0.2.14 crate/) unsafe fn forkrust-analyzer cargo sudo-rs 0.2.14 system/fork().() -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<ForkResultrust-analyzer cargo sudo-rs 0.2.14 system/ForkResult#> {
127 // FIXME add debug assertion that we are not currently using multiple threads.
128 // SAFETY: Calling async-signal-unsafe functions after fork is safe as the program is single
129 // threaded at this point according to the safety invariant of this function.
130 let pid = cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().(unsafe { libcrust-analyzer cargo libc 0.2.186 crate/::forkrust-analyzer cargo libc 0.2.186 fork().() })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
131 if pid rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq().0 {
132 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ForkResultrust-analyzer cargo sudo-rs 0.2.14 system/ForkResult#::Childrust-analyzer cargo sudo-rs 0.2.14 system/ForkResult#Child#)
133 } else {
134 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ForkResultrust-analyzer cargo sudo-rs 0.2.14 system/ForkResult#::Parentrust-analyzer cargo sudo-rs 0.2.14 system/ForkResult#Parent#(ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#::newrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[ProcessId]new().(pid)))
135 }
136 }
137
138 /// Create a new process with extra precautions for usage in tests.
139 ///
140 /// # Safety
141 ///
142 /// In a multithreaded program, only async-signal-safe functions are guaranteed to work in the
143 /// child process until a call to `execve` or a similar function is done.
144 #[cfg(test)]
145 unsafe fn fork_for_testrust-analyzer cargo sudo-rs 0.2.14 system/fork_for_test().(child_func: impl FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#() -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::convertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/::Infalliblerust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Infallible#) -> ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId# {
146 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#;
147 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/::{AssertUnwindSaferust-analyzer cargo core https://github.com/rust-lang/rust/library/core panic/unwind_safe/AssertUnwindSafe#, catch_unwindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/catch_unwind().};
148 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::exitrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/exit().;
149
150 // SAFETY: Not really safe, but this is test only code.
151 match unsafe { forkrust-analyzer cargo sudo-rs 0.2.14 system/fork().() }.unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().() {
152 ForkResultrust-analyzer cargo sudo-rs 0.2.14 system/ForkResult#::Childrust-analyzer cargo sudo-rs 0.2.14 system/ForkResult#Child# => {
153 // Make sure that panics in the child always abort the process.
154 let err = match catch_unwindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/catch_unwind().(AssertUnwindSaferust-analyzer cargo core https://github.com/rust-lang/rust/library/core panic/unwind_safe/AssertUnwindSafe#(child_func)) {
155 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(res) => match res {},
156 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => err,
157 };
158
159 let s = if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(s) = err.downcast_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/impl#[`dyn Any + Send + 'static`]downcast_ref().::<&str>() {
160 s
161 } else if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(s) = err.downcast_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/impl#[`dyn Any + Send + 'static`]downcast_ref().::<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>() {
162 s
163 } else {
164 "Box<dyn Any>"
165 };
166 let _ = writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::stderrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/stderr().(), "{s}");
167
168 exitrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/exit().(101);
169 }
170 ForkResultrust-analyzer cargo sudo-rs 0.2.14 system/ForkResult#::Parentrust-analyzer cargo sudo-rs 0.2.14 system/ForkResult#Parent#(pid) => pid,
171 }
172 }
173
174 pub fn setsidrust-analyzer cargo sudo-rs 0.2.14 system/setsid().() -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#> {
175 // SAFETY: this function is memory-safe to call
176 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#::newrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[ProcessId]new().(cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().(unsafe { libcrust-analyzer cargo libc 0.2.186 crate/::setsidrust-analyzer cargo libc 0.2.186 setsid().() })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().))
177 }
178
179 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)]
180 #[cfg_attr(test, derive(PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:))]
181 pub struct Hostnamerust-analyzer cargo sudo-rs 0.2.14 system/Hostname# {
182 innerrust-analyzer cargo sudo-rs 0.2.14 system/Hostname#inner.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#,
183 }
184
185 impl fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for Hostnamerust-analyzer cargo sudo-rs 0.2.14 system/Hostname# {
186 fn fmtrust-analyzer cargo sudo-rs 0.2.14 system/impl#[Hostname][Debug]fmt().(&self, f: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
187 f.debug_tuplerust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]debug_tuple().("Hostname").fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugTuple<'a, 'b>`]field().(&self.innerrust-analyzer cargo sudo-rs 0.2.14 system/Hostname#inner.).finishrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugTuple<'a, 'b>`]finish().()
188 }
189 }
190
191 impl fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for Hostnamerust-analyzer cargo sudo-rs 0.2.14 system/Hostname# {
192 fn fmtrust-analyzer cargo sudo-rs 0.2.14 system/impl#[Hostname][Display]fmt().(&self, f: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
193 f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().(&self.innerrust-analyzer cargo sudo-rs 0.2.14 system/Hostname#inner.)
194 }
195 }
196
197 impl opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref# for Hostnamerust-analyzer cargo sudo-rs 0.2.14 system/Hostname# {
198 type Targetrust-analyzer cargo sudo-rs 0.2.14 system/impl#[Hostname][Deref]Target# = str;
199
200 fn derefrust-analyzer cargo sudo-rs 0.2.14 system/impl#[Hostname][Deref]deref().(&self) -> &str {
201 &self.innerrust-analyzer cargo sudo-rs 0.2.14 system/Hostname#inner.
202 }
203 }
204
205 impl Hostnamerust-analyzer cargo sudo-rs 0.2.14 system/Hostname# {
206 #[cfg(test)]
207 pub fn fakerust-analyzer cargo sudo-rs 0.2.14 system/impl#[Hostname]fake().(hostname: &str) -> Selfrust-analyzer cargo sudo-rs 0.2.14 system/impl#[Hostname] {
208 Selfrust-analyzer cargo sudo-rs 0.2.14 system/impl#[Hostname] {
209 innerrust-analyzer cargo sudo-rs 0.2.14 system/Hostname#inner.: hostname.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
210 }
211 }
212
213 pub fn resolverust-analyzer cargo sudo-rs 0.2.14 system/impl#[Hostname]resolve().() -> Selfrust-analyzer cargo sudo-rs 0.2.14 system/impl#[Hostname] {
214 // see `man 2 gethostname`
215 const MAX_HOST_NAME_SIZE_ACCORDING_TO_SUSV2rust-analyzer cargo sudo-rs 0.2.14 system/MAX_HOST_NAME_SIZE_ACCORDING_TO_SUSV2.: c_longrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_long# = 255;
216
217 // POSIX.1 systems limit hostnames to `HOST_NAME_MAX` bytes
218 // not including null-byte in the count
219 let max_hostname_size = sysconfrust-analyzer cargo sudo-rs 0.2.14 cutils/sysconf().(libcrust-analyzer cargo libc 0.2.186 crate/::_SC_HOST_NAME_MAXrust-analyzer cargo libc 0.2.186 unix/linux_like/linux_l4re_shared/_SC_HOST_NAME_MAX.)
220 .unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(MAX_HOST_NAME_SIZE_ACCORDING_TO_SUSV2rust-analyzer cargo sudo-rs 0.2.14 system/MAX_HOST_NAME_SIZE_ACCORDING_TO_SUSV2.)
221 as usize;
222
223 let buffer_size = max_hostname_size rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1 /* null byte delimiter */ ;
224 let mut buf = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0; buffer_size];
225
226 // SAFETY: we are passing a valid pointer to gethostname
227 match cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().(unsafe { libcrust-analyzer cargo libc 0.2.186 crate/::gethostnamerust-analyzer cargo libc 0.2.186 gethostname().(buf.as_mut_ptrrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_mut_ptr().(), buffer_size) }) {
228 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(_) => Selfrust-analyzer cargo sudo-rs 0.2.14 system/impl#[Hostname] {
229 // SAFETY: gethostname succeeded, so `buf` will hold a null-terminated C string
230 innerrust-analyzer cargo sudo-rs 0.2.14 system/Hostname#inner.: unsafe { string_from_ptrrust-analyzer cargo sudo-rs 0.2.14 cutils/string_from_ptr().(buf.as_ptrrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_ptr().()) },
231 },
232
233 // ENAMETOOLONG is returned when hostname is greater than `buffer_size`
234 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => {
235 // but we have chosen a `buffer_size` larger than `max_hostname_size` so no truncation error is possible
236 panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("Unexpected error while retrieving hostname, this should not happen");
237 }
238 }
239 }
240 }
241
242 pub fn syslogrust-analyzer cargo sudo-rs 0.2.14 system/syslog().(priority: c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#, facility: c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#, message: &CStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/CStr#) {
243 const MSGrust-analyzer cargo sudo-rs 0.2.14 system/MSG.: &CStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/CStr# = c"%s";
244
245 // SAFETY:
246 // - "MSG" is a constant expression that is a null-terminated C string that represents "%s";
247 // this also means that to achieve safety we MUST pass one more argument to syslog that is a proper
248 // pointer to a null-terminated C string
249 // - message.as_ptr() is a pointer to a proper null-terminated C string (message being a &CStr)
250 // for more info: read the manpage for syslog(2)
251 unsafe {
252 libcrust-analyzer cargo libc 0.2.186 crate/::syslogrust-analyzer cargo libc 0.2.186 syslog().(priority rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOr<Self>`]bitor().|rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOr<Self>`]bitor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOr<Self>`]bitor().facility, MSGrust-analyzer cargo sudo-rs 0.2.14 system/MSG..as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]as_ptr().(), message.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]as_ptr().());
253 }
254 }
255
256 /// Makes sure that that the target is included in the groups, and is its first element
257 fn inject_grouprust-analyzer cargo sudo-rs 0.2.14 system/inject_group().(target: GroupIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/GroupId#, groups: &mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<GroupIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/GroupId#>) {
258 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(index) = groups.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().positionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]position().(|id| id rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().&target) {
259 // make sure the requested group id is the first in the list (necessary on FreeBSD)
260 groups.swaprust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]swap().(0, index)
261 } else {
262 // add target group to list of additional groups if not present
263 groups.insertrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]insert().(0, target);
264 }
265 }
266
267 /// Set the supplementary groups -- returns a c_int to mimic a libc function
268 fn set_supplementary_groupsrust-analyzer cargo sudo-rs 0.2.14 system/set_supplementary_groups().(groups: &[GroupIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/GroupId#]) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> {
269 // On FreeBSD, setgruops expects the size to be passed as a i32, so the below
270 // conversion protects a very extreme case of arithmetic conversion error
271 #[allow(irrefutable_let_patterns)]
272 #[allow(clippy::useless_conversion)]
273 let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(len) = groups.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().().try_intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`TryInto<U>`]try_into().() else {
274 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::otherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]other().("too many groups"));
275 };
276 // SAFETY: setgroups is passed a valid pointer to a chunk of memory of the correct size
277 // We can cast to gid_t because `GroupId` is marked as transparent
278 cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().(unsafe { libcrust-analyzer cargo libc 0.2.186 crate/::setgroupsrust-analyzer cargo libc 0.2.186 setgroups().(len, groups.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_ptr().().castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/const_ptr/impl#[`*const T`]cast().::<libcrust-analyzer cargo libc 0.2.186 crate/::gid_trust-analyzer cargo libc 0.2.186 unix/gid_t#>()) })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
279
280 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
281 }
282
283 /// set target user and groups (uid, gid, additional groups) for a command
284 pub fn set_target_userrust-analyzer cargo sudo-rs 0.2.14 system/set_target_user().(
285 cmd: &mut stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::Commandrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Command#,
286 mut target_user: Userrust-analyzer cargo sudo-rs 0.2.14 system/User#,
287 target_group: Grouprust-analyzer cargo sudo-rs 0.2.14 system/Group#,
288 ) {
289 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/::unixrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/::processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/process/::CommandExtrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/process/CommandExt#;
290
291 inject_grouprust-analyzer cargo sudo-rs 0.2.14 system/inject_group().(target_group.gidrust-analyzer cargo sudo-rs 0.2.14 system/Group#gid., &mut target_user.groupsrust-analyzer cargo sudo-rs 0.2.14 system/User#groups.);
292
293 // we need to do this in a `pre_exec` call since the `groups` method in `process::Command` is unstable
294 // see https://github.com/rust-lang/rust/blob/a01b4cc9f375f1b95fa8195daeea938d3d9c4c34/library/std/src/sys/unix/process/process_unix.rs#L329-L352
295 // for the std implementation of the libc calls to `setgroups`, `setgid` and `setuid`
296 // SAFETY: Setuid, setgid and setgroups are async-signal-safe.
297 unsafe {
298 cmd.pre_execrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/process/impl#[`process::Command`][CommandExt]pre_exec().(move || {
299 set_supplementary_groupsrust-analyzer cargo sudo-rs 0.2.14 system/set_supplementary_groups().(&target_user.groupsrust-analyzer cargo sudo-rs 0.2.14 system/User#groups.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
300 // setgid and setuid set the real, effective and saved version of the gid and uid
301 // respectively rather than just the real gid and uid. The original sudo uses setresgid
302 // and setresuid instead with all three arguments equal, but as this does the same as
303 // setgid and setuid using the latter is fine too.
304 cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().(libcrust-analyzer cargo libc 0.2.186 crate/::setgidrust-analyzer cargo libc 0.2.186 setgid().(target_group.gidrust-analyzer cargo sudo-rs 0.2.14 system/Group#gid..innerrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[GroupId]inner().()))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
305 cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().(libcrust-analyzer cargo libc 0.2.186 crate/::setuidrust-analyzer cargo libc 0.2.186 setuid().(target_user.uidrust-analyzer cargo sudo-rs 0.2.14 system/User#uid..innerrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[UserId]inner().()))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
306
307 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
308 });
309 }
310 }
311
312 /// Send a signal to a process with the specified ID.
313 pub fn killrust-analyzer cargo sudo-rs 0.2.14 system/kill().(pid: ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#, signal: SignalNumberrust-analyzer cargo sudo-rs 0.2.14 system/signal/SignalNumber#) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> {
314 // SAFETY: This function cannot cause UB even if `pid` is not a valid process ID or if
315 // `signal` is not a valid signal code.
316 cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().(unsafe { libcrust-analyzer cargo libc 0.2.186 crate/::killrust-analyzer cargo libc 0.2.186 kill().(pid.innerrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[ProcessId]inner().(), signal) }).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(|_| ())
317 }
318
319 /// Send a signal to a process group with the specified ID.
320 pub fn killpgrust-analyzer cargo sudo-rs 0.2.14 system/killpg().(pgid: ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#, signal: SignalNumberrust-analyzer cargo sudo-rs 0.2.14 system/signal/SignalNumber#) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> {
321 // SAFETY: This function cannot cause UB even if `pgid` is not a valid process ID or if
322 // `signal` is not a valid signal code.
323 cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().(unsafe { libcrust-analyzer cargo libc 0.2.186 crate/::killpgrust-analyzer cargo libc 0.2.186 killpg().(pgid.innerrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[ProcessId]inner().(), signal) }).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(|_| ())
324 }
325
326 /// Get the process group ID of the current process.
327 pub fn getpgrprust-analyzer cargo sudo-rs 0.2.14 system/getpgrp().() -> ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId# {
328 // SAFETY: This function is always safe to call
329 ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#::newrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[ProcessId]new().(unsafe { libcrust-analyzer cargo libc 0.2.186 crate/::getpgrprust-analyzer cargo libc 0.2.186 getpgrp().() })
330 }
331
332 /// Get a process group ID.
333 pub fn getpgidrust-analyzer cargo sudo-rs 0.2.14 system/getpgid().(pid: ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#> {
334 // SAFETY: This function cannot cause UB even if `pid` is not a valid process ID
335 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#::newrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[ProcessId]new().(cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().(unsafe { libcrust-analyzer cargo libc 0.2.186 crate/::getpgidrust-analyzer cargo libc 0.2.186 getpgid().(pid.innerrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[ProcessId]inner().()) })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().))
336 }
337
338 /// Set a process group ID.
339 pub fn setpgidrust-analyzer cargo sudo-rs 0.2.14 system/setpgid().(pid: ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#, pgid: ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> {
340 // SAFETY: This function cannot cause UB even if `pid` or `pgid` are not a valid process IDs:
341 // https://pubs.opengroup.org/onlinepubs/007904975/functions/setpgid.html
342 cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().(unsafe { libcrust-analyzer cargo libc 0.2.186 crate/::setpgidrust-analyzer cargo libc 0.2.186 setpgid().(pid.innerrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[ProcessId]inner().(), pgid.innerrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[ProcessId]inner().()) }).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(|_| ())
343 }
344
345 pub fn chownrust-analyzer cargo sudo-rs 0.2.14 system/chown().<S: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<CStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/CStr#>>(
346 path: &S,
347 uid: impl Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<UserIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/UserId#>,
348 gid: impl Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<GroupIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/GroupId#>,
349 ) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> {
350 let path = path.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().().as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]as_ptr().();
351 let uid = uid.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().();
352 let gid = gid.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().();
353
354 // SAFETY: path is a valid pointer to a null-terminated C string; chown cannot cause safety
355 // issues even if uid and/or gid would be invalid identifiers.
356 cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().(unsafe { libcrust-analyzer cargo libc 0.2.186 crate/::chownrust-analyzer cargo libc 0.2.186 chown().(path, uid.innerrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[UserId]inner().(), gid.innerrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[GroupId]inner().()) }).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(|_| ())
357 }
358
359 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)]
360 pub struct Userrust-analyzer cargo sudo-rs 0.2.14 system/User# {
361 pub uidrust-analyzer cargo sudo-rs 0.2.14 system/User#uid.: UserIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/UserId#,
362 pub gidrust-analyzer cargo sudo-rs 0.2.14 system/User#gid.: GroupIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/GroupId#,
363 pub namerust-analyzer cargo sudo-rs 0.2.14 system/User#name.: SudoStringrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString#,
364 pub homerust-analyzer cargo sudo-rs 0.2.14 system/User#home.: SudoPathrust-analyzer cargo sudo-rs 0.2.14 common/path/SudoPath#,
365 pub shellrust-analyzer cargo sudo-rs 0.2.14 system/User#shell.: PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#,
366 pub groupsrust-analyzer cargo sudo-rs 0.2.14 system/User#groups.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<GroupIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/GroupId#>,
367 }
368
369 impl Userrust-analyzer cargo sudo-rs 0.2.14 system/User# {
370 /// # Safety
371 /// This function expects `pwd` to be a result from a successful call to `getpwXXX_r`.
372 /// (It can cause UB if any of `pwd`'s pointed-to strings does not have a null-terminator.)
373 unsafe fn from_libcrust-analyzer cargo sudo-rs 0.2.14 system/impl#[User]from_libc().(pwd: &libcrust-analyzer cargo libc 0.2.186 crate/::passwdrust-analyzer cargo libc 0.2.186 unix/linux_like/linux_l4re_shared/passwd#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Userrust-analyzer cargo sudo-rs 0.2.14 system/User#, Errorrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#> {
374 // NOTE: on Linux, getgrouplist could be used to simply inquire as to the size needed;
375 // but on FreeBSD, getgrouplist does not specify this in its function contract, so a
376 // blind allocation loop is needed.
377 let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(groups_buffer) =
378 dynamic_fillrust-analyzer cargo sudo-rs 0.2.14 cutils/dynamic_fill().::<libcrust-analyzer cargo libc 0.2.186 crate/::gid_trust-analyzer cargo libc 0.2.186 unix/gid_t#, stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::numrust-analyzer cargo std https://github.com/rust-lang/rust/library/std num/::TryFromIntErrorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/error/TryFromIntError#>(32..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#65536, |groups_buffer| {
379 let mut buf_len: c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int# = groups_buffer.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() as c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#;
380 // SAFETY: getgrouplist is passed valid pointers
381 // in particular `groups_buffer` is an array of `buf.len()` bytes, as required
382 let result = unsafe {
383 libcrust-analyzer cargo libc 0.2.186 crate/::getgrouplistrust-analyzer cargo libc 0.2.186 getgrouplist().(
384 pwd.pw_namerust-analyzer cargo libc 0.2.186 unix/linux_like/linux_l4re_shared/passwd#pw_name.,
385 pwd.pw_gidrust-analyzer cargo libc 0.2.186 unix/linux_like/linux_l4re_shared/passwd#pw_gid.,
386 groups_buffer.as_mut_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_mut_ptr().(),
387 &mut buf_len,
388 )
389 };
390
391 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(if result rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]ne().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().1 {
392 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(buf_len.try_intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`TryInto<U>`]try_into().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().)
393 } else {
394 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#
395 })
396 })
397 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("negative group size, this should not happen")
398 else {
399 panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("user has too many groups (> 65536), this should not happen");
400 };
401
402 // SAFETY: All pointers were initialized by a successful call to `getpwXXX_r` as per the
403 // safety invariant of this function.
404 unsafe {
405 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Userrust-analyzer cargo sudo-rs 0.2.14 system/User# {
406 uidrust-analyzer cargo sudo-rs 0.2.14 system/User#uid.: UserIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/UserId#::newrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[UserId]new().(pwd.pw_uidrust-analyzer cargo libc 0.2.186 unix/linux_like/linux_l4re_shared/passwd#pw_uid.),
407 gidrust-analyzer cargo sudo-rs 0.2.14 system/User#gid.: GroupIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/GroupId#::newrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[GroupId]new().(pwd.pw_gidrust-analyzer cargo libc 0.2.186 unix/linux_like/linux_l4re_shared/passwd#pw_gid.),
408 namerust-analyzer cargo sudo-rs 0.2.14 system/User#name.: SudoStringrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString#::newrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString]new().(string_from_ptrrust-analyzer cargo sudo-rs 0.2.14 cutils/string_from_ptr().(pwd.pw_namerust-analyzer cargo libc 0.2.186 unix/linux_like/linux_l4re_shared/passwd#pw_name.))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().,
409 homerust-analyzer cargo sudo-rs 0.2.14 system/User#home.: SudoPathrust-analyzer cargo sudo-rs 0.2.14 common/path/SudoPath#::newrust-analyzer cargo sudo-rs 0.2.14 common/path/impl#[SudoPath]new().(os_string_from_ptrrust-analyzer cargo sudo-rs 0.2.14 cutils/os_string_from_ptr().(pwd.pw_dirrust-analyzer cargo libc 0.2.186 unix/linux_like/linux_l4re_shared/passwd#pw_dir.).intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().,
410 shellrust-analyzer cargo sudo-rs 0.2.14 system/User#shell.: os_string_from_ptrrust-analyzer cargo sudo-rs 0.2.14 cutils/os_string_from_ptr().(pwd.pw_shellrust-analyzer cargo libc 0.2.186 unix/linux_like/linux_l4re_shared/passwd#pw_shell.).intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(),
411 groupsrust-analyzer cargo sudo-rs 0.2.14 system/User#groups.: groups_buffer
412 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
413 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|id| GroupIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/GroupId#::newrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[GroupId]new().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().id))
414 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>>(),
415 })
416 }
417 }
418
419 pub fn from_uidrust-analyzer cargo sudo-rs 0.2.14 system/impl#[User]from_uid().(uid: UserIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/UserId#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Userrust-analyzer cargo sudo-rs 0.2.14 system/User#>, Errorrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#> {
420 let max_pw_size = sysconfrust-analyzer cargo sudo-rs 0.2.14 cutils/sysconf().(libcrust-analyzer cargo libc 0.2.186 crate/::_SC_GETPW_R_SIZE_MAXrust-analyzer cargo libc 0.2.186 unix/linux_like/linux_l4re_shared/_SC_GETPW_R_SIZE_MAX.).unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(16_384);
421 let mut buf = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0; max_pw_size as usize];
422 let mut pwd = MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#::uninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/impl#[`MaybeUninit<T>`]uninit().();
423 let mut pwd_ptr = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::null_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/null_mut().();
424 // SAFETY: getpwuid_r is passed valid (although partly uninitialized) pointers to memory,
425 // in particular `buf` points to an array of `buf.len()` bytes, as required.
426 // After this call, if `pwd_ptr` is not NULL, `*pwd_ptr` and `pwd` will be aliased;
427 // but we never dereference `pwd_ptr`.
428 cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().(unsafe {
429 libcrust-analyzer cargo libc 0.2.186 crate/::getpwuid_rrust-analyzer cargo libc 0.2.186 getpwuid_r().(
430 uid.innerrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[UserId]inner().(),
431 pwd.as_mut_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/impl#[`MaybeUninit<T>`]as_mut_ptr().(),
432 buf.as_mut_ptrrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_mut_ptr().(),
433 buf.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(),
434 &mut pwd_ptr,
435 )
436 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
437 if pwd_ptr.is_nullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]is_null().() {
438 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#)
439 } else {
440 // SAFETY: pwd_ptr was not null, and getpwuid_r succeeded, so we have assurances that
441 // the `pwd` structure was written to by getpwuid_r
442 let pwd = unsafe { pwd.assume_initrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/impl#[`MaybeUninit<T>`]assume_init().() };
443 // SAFETY: `pwd` was obtained by a call to getpwXXX_r, as required.
444 unsafe { Selfrust-analyzer cargo sudo-rs 0.2.14 system/impl#[User]::from_libcrust-analyzer cargo sudo-rs 0.2.14 system/impl#[User]from_libc().(&pwd).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#) }
445 }
446 }
447
448 pub fn effective_uidrust-analyzer cargo sudo-rs 0.2.14 system/impl#[User]effective_uid().() -> UserIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/UserId# {
449 // SAFETY: this function cannot cause memory safety issues
450 UserIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/UserId#::newrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[UserId]new().(unsafe { libcrust-analyzer cargo libc 0.2.186 crate/::geteuidrust-analyzer cargo libc 0.2.186 geteuid().() })
451 }
452
453 pub fn effective_gidrust-analyzer cargo sudo-rs 0.2.14 system/impl#[User]effective_gid().() -> GroupIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/GroupId# {
454 // SAFETY: this function cannot cause memory safety issues
455 GroupIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/GroupId#::newrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[GroupId]new().(unsafe { libcrust-analyzer cargo libc 0.2.186 crate/::getegidrust-analyzer cargo libc 0.2.186 getegid().() })
456 }
457
458 pub fn real_uidrust-analyzer cargo sudo-rs 0.2.14 system/impl#[User]real_uid().() -> UserIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/UserId# {
459 // SAFETY: this function cannot cause memory safety issues
460 UserIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/UserId#::newrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[UserId]new().(unsafe { libcrust-analyzer cargo libc 0.2.186 crate/::getuidrust-analyzer cargo libc 0.2.186 getuid().() })
461 }
462
463 pub fn real_gidrust-analyzer cargo sudo-rs 0.2.14 system/impl#[User]real_gid().() -> GroupIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/GroupId# {
464 // SAFETY: this function cannot cause memory safety issues
465 GroupIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/GroupId#::newrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[GroupId]new().(unsafe { libcrust-analyzer cargo libc 0.2.186 crate/::getgidrust-analyzer cargo libc 0.2.186 getgid().() })
466 }
467
468 pub fn realrust-analyzer cargo sudo-rs 0.2.14 system/impl#[User]real().() -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Userrust-analyzer cargo sudo-rs 0.2.14 system/User#>, Errorrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#> {
469 Selfrust-analyzer cargo sudo-rs 0.2.14 system/impl#[User]::from_uidrust-analyzer cargo sudo-rs 0.2.14 system/impl#[User]from_uid().(Selfrust-analyzer cargo sudo-rs 0.2.14 system/impl#[User]::real_uidrust-analyzer cargo sudo-rs 0.2.14 system/impl#[User]real_uid().())
470 }
471
472 pub fn primary_grouprust-analyzer cargo sudo-rs 0.2.14 system/impl#[User]primary_group().(&self) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<Grouprust-analyzer cargo sudo-rs 0.2.14 system/Group#> {
473 // Use from_gid_unchecked here to ensure that we can still resolve when
474 // the /etc/group entry for the primary group is missing.
475 Grouprust-analyzer cargo sudo-rs 0.2.14 system/Group#::from_gid_uncheckedrust-analyzer cargo sudo-rs 0.2.14 system/impl#[Group]from_gid_unchecked().(self.gidrust-analyzer cargo sudo-rs 0.2.14 system/User#gid.)
476 }
477
478 pub fn from_namerust-analyzer cargo sudo-rs 0.2.14 system/impl#[User]from_name().(name_c: &CStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/CStr#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Userrust-analyzer cargo sudo-rs 0.2.14 system/User#>, Errorrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#> {
479 let max_pw_size = sysconfrust-analyzer cargo sudo-rs 0.2.14 cutils/sysconf().(libcrust-analyzer cargo libc 0.2.186 crate/::_SC_GETPW_R_SIZE_MAXrust-analyzer cargo libc 0.2.186 unix/linux_like/linux_l4re_shared/_SC_GETPW_R_SIZE_MAX.).unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(16_384);
480 let mut buf = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0; max_pw_size as usize];
481 let mut pwd = MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#::uninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/impl#[`MaybeUninit<T>`]uninit().();
482 let mut pwd_ptr = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::null_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/null_mut().();
483
484 // SAFETY: analogous to getpwuid_r above
485 cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().(unsafe {
486 libcrust-analyzer cargo libc 0.2.186 crate/::getpwnam_rrust-analyzer cargo libc 0.2.186 getpwnam_r().(
487 name_c.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]as_ptr().(),
488 pwd.as_mut_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/impl#[`MaybeUninit<T>`]as_mut_ptr().(),
489 buf.as_mut_ptrrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_mut_ptr().(),
490 buf.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(),
491 &mut pwd_ptr,
492 )
493 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
494 if pwd_ptr.is_nullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]is_null().() {
495 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#)
496 } else {
497 // SAFETY: pwd_ptr was not null, and getpwnam_r succeeded, so we have assurances that
498 // the `pwd` structure was written to by getpwnam_r
499 let pwd = unsafe { pwd.assume_initrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/impl#[`MaybeUninit<T>`]assume_init().() };
500 // SAFETY: `pwd` was obtained by a call to getpwXXX_r, as required.
501 unsafe { Selfrust-analyzer cargo sudo-rs 0.2.14 system/impl#[User]::from_libcrust-analyzer cargo sudo-rs 0.2.14 system/impl#[User]from_libc().(&pwd).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#) }
502 }
503 }
504 }
505
506 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)]
507 #[cfg_attr(test, derive(PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:))]
508 pub struct Grouprust-analyzer cargo sudo-rs 0.2.14 system/Group# {
509 pub gidrust-analyzer cargo sudo-rs 0.2.14 system/Group#gid.: GroupIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/GroupId#,
510 pub namerust-analyzer cargo sudo-rs 0.2.14 system/Group#name.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>,
511 }
512
513 impl Grouprust-analyzer cargo sudo-rs 0.2.14 system/Group# {
514 /// # Safety
515 /// This function expects `grp` to be a result from a successful call to `getgrXXX_r`.
516 /// In particular the grp.gr_mem pointer is assumed to be non-null, and pointing to a
517 /// null-terminated list; the pointed-to strings are expected to be null-terminated.
518 unsafe fn from_libcrust-analyzer cargo sudo-rs 0.2.14 system/impl#[Group]from_libc().(grp: &libcrust-analyzer cargo libc 0.2.186 crate/::grouprust-analyzer cargo libc 0.2.186 unix/group#) -> Grouprust-analyzer cargo sudo-rs 0.2.14 system/Group# {
519 // SAFETY: The name pointer is initialized by a successful call to `getgrXXX_r` as per the
520 // safety invariant of this function.
521 let name = unsafe { string_from_ptrrust-analyzer cargo sudo-rs 0.2.14 cutils/string_from_ptr().(grp.gr_namerust-analyzer cargo libc 0.2.186 unix/group#gr_name.) };
522 Grouprust-analyzer cargo sudo-rs 0.2.14 system/Group# {
523 gidrust-analyzer cargo sudo-rs 0.2.14 system/Group#gid.: GroupIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/GroupId#::newrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[GroupId]new().(grp.gr_gidrust-analyzer cargo libc 0.2.186 unix/group#gr_gid.),
524 namerust-analyzer cargo sudo-rs 0.2.14 system/Group#name.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(name),
525 }
526 }
527
528 /// Lookup group for gid without returning an error when a /etc/group entry is missing.
529 fn from_gid_uncheckedrust-analyzer cargo sudo-rs 0.2.14 system/impl#[Group]from_gid_unchecked().(gid: GroupIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/GroupId#) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<Grouprust-analyzer cargo sudo-rs 0.2.14 system/Group#> {
530 // Set an arbitrary upper limit of two terabytes/two gigabytes for the temporary buffer
531 let initial_gr_size = sysconfrust-analyzer cargo sudo-rs 0.2.14 cutils/sysconf().(libcrust-analyzer cargo libc 0.2.186 crate/::_SC_GETGR_R_SIZE_MAXrust-analyzer cargo libc 0.2.186 unix/linux_like/linux_l4re_shared/_SC_GETGR_R_SIZE_MAX.).unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(16_384) as usize;
532 let max_gr_size = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::maxrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/max().(i32::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i32]MAX. as usize, usize::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]MAX. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[usize][`Shr<i32>`]shr().>>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[usize][`Shr<i32>`]shr(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[usize][`Shr<i32>`]shr().24);
533
534 let mut grp = MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#::uninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/impl#[`MaybeUninit<T>`]uninit().();
535 let mut grp_ptr = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::null_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/null_mut().();
536 let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(_buf) = dynamic_fillrust-analyzer cargo sudo-rs 0.2.14 cutils/dynamic_fill().(initial_gr_size..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#max_gr_size, |buf| {
537 // SAFETY: analogous to getpwuid_r above
538 let result = unsafe {
539 libcrust-analyzer cargo libc 0.2.186 crate/::getgrgid_rrust-analyzer cargo libc 0.2.186 getgrgid_r().(
540 gid.innerrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[GroupId]inner().(),
541 grp.as_mut_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/impl#[`MaybeUninit<T>`]as_mut_ptr().(),
542 buf.as_mut_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_mut_ptr().(),
543 buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(),
544 &mut grp_ptr,
545 )
546 };
547 match result {
548 0 => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().())),
549 libcrust-analyzer cargo libc 0.2.186 crate/::ERANGErust-analyzer cargo libc 0.2.186 unix/linux_like/linux_l4re_shared/ERANGE. => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#),
550 _ => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::from_raw_os_errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]from_raw_os_error().(result)),
551 }
552 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().
553 else {
554 panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("group buffer size exceeds limit (>{max_gr_size})");
555 };
556
557 if grp_ptr.is_nullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]is_null().() {
558 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Grouprust-analyzer cargo sudo-rs 0.2.14 system/Group# { gidrust-analyzer cargo sudo-rs 0.2.14 system/Group#gid., namerust-analyzer cargo sudo-rs 0.2.14 system/Group#name.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# })
559 } else {
560 // SAFETY: grp_ptr was not null, and getgrgid_r succeeded, so we have assurances that
561 // the `grp` structure was written to by getgrgid_r
562 let grp = unsafe { grp.assume_initrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/impl#[`MaybeUninit<T>`]assume_init().() };
563 // SAFETY: `pwd` was obtained by a call to getgrXXX_r, as required.
564 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(unsafe { Grouprust-analyzer cargo sudo-rs 0.2.14 system/Group#::from_libcrust-analyzer cargo sudo-rs 0.2.14 system/impl#[Group]from_libc().(&grp) })
565 }
566 }
567
568 pub fn from_gidrust-analyzer cargo sudo-rs 0.2.14 system/impl#[Group]from_gid().(gid: GroupIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/GroupId#) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Grouprust-analyzer cargo sudo-rs 0.2.14 system/Group#>> {
569 let group = Selfrust-analyzer cargo sudo-rs 0.2.14 system/impl#[Group]::from_gid_uncheckedrust-analyzer cargo sudo-rs 0.2.14 system/impl#[Group]from_gid_unchecked().(gid)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
570 if group.namerust-analyzer cargo sudo-rs 0.2.14 system/Group#name..is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().() {
571 // No entry in /etc/group
572 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#)
573 } else {
574 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(group))
575 }
576 }
577
578 pub fn from_namerust-analyzer cargo sudo-rs 0.2.14 system/impl#[Group]from_name().(name_c: &CStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/CStr#) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Grouprust-analyzer cargo sudo-rs 0.2.14 system/Group#>> {
579 // Set an arbitrary upper limit of two terabytes/two gigabytes for the temporary buffer
580 let initial_gr_size = sysconfrust-analyzer cargo sudo-rs 0.2.14 cutils/sysconf().(libcrust-analyzer cargo libc 0.2.186 crate/::_SC_GETGR_R_SIZE_MAXrust-analyzer cargo libc 0.2.186 unix/linux_like/linux_l4re_shared/_SC_GETGR_R_SIZE_MAX.).unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(16_384) as usize;
581 let max_gr_size = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::maxrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/max().(i32::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i32]MAX. as usize, usize::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]MAX. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[usize][`Shr<i32>`]shr().>>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[usize][`Shr<i32>`]shr(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[usize][`Shr<i32>`]shr().24);
582 let mut grp = MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#::uninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/impl#[`MaybeUninit<T>`]uninit().();
583 let mut grp_ptr = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::null_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/null_mut().();
584 let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(_buf) = dynamic_fillrust-analyzer cargo sudo-rs 0.2.14 cutils/dynamic_fill().(initial_gr_size..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#max_gr_size, |buf| {
585 // SAFETY: analogous to getpwuid_r above
586 let result = unsafe {
587 libcrust-analyzer cargo libc 0.2.186 crate/::getgrnam_rrust-analyzer cargo libc 0.2.186 getgrnam_r().(
588 name_c.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]as_ptr().(),
589 grp.as_mut_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/impl#[`MaybeUninit<T>`]as_mut_ptr().(),
590 buf.as_mut_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_mut_ptr().(),
591 buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(),
592 &mut grp_ptr,
593 )
594 };
595 match result {
596 0 => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().())),
597 libcrust-analyzer cargo libc 0.2.186 crate/::ERANGErust-analyzer cargo libc 0.2.186 unix/linux_like/linux_l4re_shared/ERANGE. => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#),
598 _ => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::from_raw_os_errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]from_raw_os_error().(result)),
599 }
600 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().
601 else {
602 panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("group buffer size exceeds limit (>{max_gr_size})");
603 };
604
605 if grp_ptr.is_nullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]is_null().() {
606 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#)
607 } else {
608 // SAFETY: grp_ptr was not null, and getgrgid_r succeeded, so we have assurances that
609 // the `grp` structure was written to by getgrgid_r
610 let grp = unsafe { grp.assume_initrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/impl#[`MaybeUninit<T>`]assume_init().() };
611 // SAFETY: `pwd` was obtained by a call to getgrXXX_r, as required.
612 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(unsafe { Grouprust-analyzer cargo sudo-rs 0.2.14 system/Group#::from_libcrust-analyzer cargo sudo-rs 0.2.14 system/impl#[Group]from_libc().(&grp) }))
613 }
614 }
615 }
616
617 pub enum WithProcessrust-analyzer cargo sudo-rs 0.2.14 system/WithProcess# {
618 Currentrust-analyzer cargo sudo-rs 0.2.14 system/WithProcess#Current#,
619 Otherrust-analyzer cargo sudo-rs 0.2.14 system/WithProcess#Other#(ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#),
620 }
621
622 impl WithProcessrust-analyzer cargo sudo-rs 0.2.14 system/WithProcess# {
623 #[cfg(target_os = "linux")]
624 fn to_proc_stringrust-analyzer cargo sudo-rs 0.2.14 system/impl#[WithProcess]to_proc_string().(&self) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
625 match self {
626 WithProcessrust-analyzer cargo sudo-rs 0.2.14 system/WithProcess#::Currentrust-analyzer cargo sudo-rs 0.2.14 system/WithProcess#Current# => "self".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(),
627 WithProcessrust-analyzer cargo sudo-rs 0.2.14 system/WithProcess#::Otherrust-analyzer cargo sudo-rs 0.2.14 system/WithProcess#Other#(pid) => pid.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
628 }
629 }
630 }
631
632 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)]
633 pub struct Processrust-analyzer cargo sudo-rs 0.2.14 system/Process# {
634 pub parent_pidrust-analyzer cargo sudo-rs 0.2.14 system/Process#parent_pid.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#>,
635 pub session_idrust-analyzer cargo sudo-rs 0.2.14 system/Process#session_id.: ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#,
636 }
637
638 impl Processrust-analyzer cargo sudo-rs 0.2.14 system/Process# {
639 pub fn newrust-analyzer cargo sudo-rs 0.2.14 system/impl#[Process]new().() -> Processrust-analyzer cargo sudo-rs 0.2.14 system/Process# {
640 Processrust-analyzer cargo sudo-rs 0.2.14 system/Process# {
641 parent_pidrust-analyzer cargo sudo-rs 0.2.14 system/Process#parent_pid.: Selfrust-analyzer cargo sudo-rs 0.2.14 system/impl#[Process]::parent_idrust-analyzer cargo sudo-rs 0.2.14 system/impl#[Process]parent_id().(),
642 session_idrust-analyzer cargo sudo-rs 0.2.14 system/Process#session_id.: Selfrust-analyzer cargo sudo-rs 0.2.14 system/impl#[Process]::session_idrust-analyzer cargo sudo-rs 0.2.14 system/impl#[Process]session_id().(),
643 }
644 }
645
646 /// Return the process identifier for the current process
647 pub fn process_idrust-analyzer cargo sudo-rs 0.2.14 system/impl#[Process]process_id().() -> ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId# {
648 // NOTE libstd casts the `i32` that `libc::getpid` returns into `u32`
649 // here we cast it back into `i32` (`ProcessId`)
650 ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#::newrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[ProcessId]new().(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::idrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/id().() as i32)
651 }
652
653 /// Return the parent process identifier for the current process
654 pub fn parent_idrust-analyzer cargo sudo-rs 0.2.14 system/impl#[Process]parent_id().() -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#> {
655 // NOTE libstd casts the `i32` that `libc::getppid` returns into `u32`
656 // here we cast it back into `i32` (`ProcessId`)
657 let pid = ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#::newrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[ProcessId]new().(unixrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/::processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/process/::parent_idrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/process/parent_id().() as i32);
658 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().pid.is_validrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[ProcessId]is_valid().() { Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# } else { Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(pid) }
659 }
660
661 /// Get the session id for the current process
662 pub fn session_idrust-analyzer cargo sudo-rs 0.2.14 system/impl#[Process]session_id().() -> ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId# {
663 // SAFETY: this function is explicitly safe to call with argument 0,
664 // and more generally getsid will never cause memory safety issues.
665 ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#::newrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[ProcessId]new().(unsafe { libcrust-analyzer cargo libc 0.2.186 crate/::getsidrust-analyzer cargo libc 0.2.186 getsid().(0) })
666 }
667
668 /// Returns the device identifier of the TTY device that is currently
669 /// attached to the given process
670 #[cfg(target_os = "linux")]
671 pub fn tty_device_idrust-analyzer cargo sudo-rs 0.2.14 system/impl#[Process]tty_device_id().(pid: WithProcessrust-analyzer cargo sudo-rs 0.2.14 system/WithProcess#) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<DeviceIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/DeviceId#>> {
672 // device id of tty is displayed as a signed integer of 32 bits
673 let data: i32 = read_proc_statrust-analyzer cargo sudo-rs 0.2.14 system/read_proc_stat().(pid, 6 /* tty_nr */)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
674 if data rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq().0 {
675 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#)
676 } else {
677 // While the integer was displayed as signed in the proc stat file,
678 // we actually need to interpret the bits of that integer as an unsigned
679 // int. We convert via u32 because a direct conversion to DeviceId
680 // would use sign extension, which would result in a different bit
681 // representation
682 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(DeviceIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/DeviceId#::newrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[DeviceId]new().(data as u64)))
683 }
684 }
685
686 #[cfg(target_os = "freebsd")]
687 fn get_proc_info(pid: WithProcessrust-analyzer cargo sudo-rs 0.2.14 system/WithProcess#) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<libcrust-analyzer cargo libc 0.2.186 crate/::kinfo_proc> {
688 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::c_voidrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_void#;
689 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/;
690
691 let mut ki_proc: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<libcrust-analyzer cargo libc 0.2.186 crate/::kinfo_proc> = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacity(1);
692
693 let pid = match pid {
694 WithProcessrust-analyzer cargo sudo-rs 0.2.14 system/WithProcess#::Currentrust-analyzer cargo sudo-rs 0.2.14 system/WithProcess#Current# => stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::idrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/id().() as i32,
695 WithProcessrust-analyzer cargo sudo-rs 0.2.14 system/WithProcess#::Otherrust-analyzer cargo sudo-rs 0.2.14 system/WithProcess#Other#(pid) => pid.inner(),
696 };
697
698 loop {
699 let mut size = ki_proc.capacity() * size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<libcrust-analyzer cargo libc 0.2.186 crate/::kinfo_proc>();
700 // SAFETY: KERN_PROC_PID only reads data into the ki_proc list. It
701 // does not write more than `size` bytes to the pointer.
702 match cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().(unsafe {
703 libcrust-analyzer cargo libc 0.2.186 crate/::sysctlrust-analyzer cargo libc 0.2.186 sysctl().(
704 [
705 libcrust-analyzer cargo libc 0.2.186 crate/::CTL_KERNrust-analyzer cargo libc 0.2.186 unix/linux_like/linux/CTL_KERN.,
706 libcrust-analyzer cargo libc 0.2.186 crate/::KERN_PROC,
707 libcrust-analyzer cargo libc 0.2.186 crate/::KERN_PROC_PID,
708 pid,
709 size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<libcrust-analyzer cargo libc 0.2.186 crate/::kinfo_proc>() as i32,
710 1,
711 ]
712 .as_ptr(),
713 4,
714 ki_proc.as_mut_ptr().cast::<c_void>(),
715 &mut size,
716 ptr::null(),
717 0,
718 )
719 }) {
720 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(_) => {
721 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(size >= size_of::<libc::kinfo_proc>());
722 // SAFETY: The above sysctl has initialized at least `size` bytes. We have
723 // asserted that this is at least a single element.
724 unsafe {
725 ki_proc.set_len(1);
726 }
727 break;
728 }
729 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) if e.raw_os_error() == Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(libcrust-analyzer cargo libc 0.2.186 crate/::ENOMEMrust-analyzer cargo libc 0.2.186 unix/linux_like/linux_l4re_shared/ENOMEM.) => {
730 // Vector not big enough. Grow it by 10% and try again.
731 ki_proc.reserve(ki_proc.capacity() + (ki_proc.capacity() + 9) / 10);
732 }
733 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) => return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e),
734 }
735 }
736
737 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ki_proc[0])
738 }
739
740 /// Returns the device identifier of the TTY device that is currently
741 /// attached to the given process
742 #[cfg(target_os = "freebsd")]
743 pub fn tty_device_id(pid: WithProcessrust-analyzer cargo sudo-rs 0.2.14 system/WithProcess#) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<DeviceIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/DeviceId#>> {
744 let ki_proc = Selfrust-analyzer cargo sudo-rs 0.2.14 system/impl#[Process]::get_proc_info(pid)?;
745
746 if ki_proc.ki_tdev == !0 {
747 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#)
748 } else {
749 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(DeviceIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/DeviceId#::new(ki_proc.ki_tdev)))
750 }
751 }
752
753 /// Get the process starting time of a specific process
754 #[cfg(target_os = "linux")]
755 pub fn starting_timerust-analyzer cargo sudo-rs 0.2.14 system/impl#[Process]starting_time().(pid: WithProcessrust-analyzer cargo sudo-rs 0.2.14 system/WithProcess#) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<ProcessCreateTimerust-analyzer cargo sudo-rs 0.2.14 system/time/ProcessCreateTime#> {
756 let process_start: u64 = read_proc_statrust-analyzer cargo sudo-rs 0.2.14 system/read_proc_stat().(pid, 21 /* start_time */)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
757
758 // the startime field is stored in ticks since the system start, so we need to know how many
759 // ticks go into a second
760 let ticks_per_second = craterust-analyzer cargo sudo-rs 0.2.14 crate/::cutilsrust-analyzer cargo sudo-rs 0.2.14 cutils/::sysconfrust-analyzer cargo sudo-rs 0.2.14 cutils/sysconf().(libcrust-analyzer cargo libc 0.2.186 crate/::_SC_CLK_TCKrust-analyzer cargo libc 0.2.186 unix/linux_like/linux_l4re_shared/_SC_CLK_TCK.).ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| {
761 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::otherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]other().("Could not retrieve system config variable for ticks per second")
762 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). as u64;
763
764 // finally compute the system time at which the process was started
765 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ProcessCreateTimerust-analyzer cargo sudo-rs 0.2.14 system/time/ProcessCreateTime#::newrust-analyzer cargo sudo-rs 0.2.14 system/time/impl#[ProcessCreateTime]new().(
766 (process_start rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Div<Self>`]div().ticks_per_second) as i64,
767 ((process_start rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Rem<Self>`]rem().%rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Rem<Self>`]rem(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Rem<Self>`]rem().ticks_per_second) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul().(1_000_000_000 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Div<Self>`]div().ticks_per_second)) as i64,
768 ))
769 }
770
771 /// Get the process starting time of a specific process
772 #[cfg(target_os = "freebsd")]
773 pub fn starting_time(pid: WithProcessrust-analyzer cargo sudo-rs 0.2.14 system/WithProcess#) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<ProcessCreateTimerust-analyzer cargo sudo-rs 0.2.14 system/time/ProcessCreateTime#> {
774 let ki_proc = Selfrust-analyzer cargo sudo-rs 0.2.14 system/impl#[Process]::get_proc_info(pid)?;
775
776 let ki_start = ki_proc.ki_start;
777 #[allow(clippy::useless_conversion)]
778 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ProcessCreateTimerust-analyzer cargo sudo-rs 0.2.14 system/time/ProcessCreateTime#::new(
779 i64::from(ki_start.tv_sec),
780 i64::from(ki_start.tv_usec) * 1000,
781 ))
782 }
783 }
784
785 /// Read the n-th field (with 0-based indexing) from `/proc/<pid>/self`.
786 ///
787 /// See ["Table 1-4: Contents of the stat fields" of "The /proc
788 /// Filesystem"][proc_stat_fields] in the Linux docs for all available fields.
789 ///
790 /// IMPORTANT: the first two fields are not accessible with this routine.
791 ///
792 /// [proc_stat_fields]: https://www.kernel.org/doc/html/latest/filesystems/proc.html#id10
793 #[cfg(target_os = "linux")]
794 fn read_proc_statrust-analyzer cargo sudo-rs 0.2.14 system/read_proc_stat().<T: FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#>(pid: WithProcessrust-analyzer cargo sudo-rs 0.2.14 system/WithProcess#, field_idx: isize) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<T> {
795 // the first two fields are skipped by the code below, and we never need them,
796 // so no point in implementing code for it in this private function.
797 debug_assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert!!(field_idx >= 2);
798
799 // read from a specific pid file, or use `self` to refer to our own process
800 let pidref = pid.to_proc_stringrust-analyzer cargo sudo-rs 0.2.14 system/impl#[WithProcess]to_proc_string().();
801
802 // read the data from the stat file for the process with the given pid
803 let path = PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#::from_iterrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf][`FromIterator<P>`]from_iter().(&["/proc", &pidref, "stat"]);
804 let proc_stat = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/read().(path)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
805
806 // first get the part of the stat file past the second argument, we then reverse
807 // search for a ')' character and start the search for the starttime field from there on
808 let skip_past_second_arg = proc_stat.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().rpositionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]rposition().(|b| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().b rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().b')').ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| {
809 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(
810 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::InvalidInputrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#InvalidInput#,
811 "Could not find position of 'comm' field in process stat",
812 )
813 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
814 let mut stat = &proc_stat[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().skip_past_second_arg..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().;
815
816 // we've now passed the first two fields, so we are at index 1, now we skip over
817 // fields until we arrive at the field we are searching for
818 let mut curr_field = 1;
819 while curr_field rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]lt().field_idx && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().stat.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().() {
820 if stat[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().b' ' {
821 curr_field rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`AddAssign<Self>`]add_assign().1;
822 }
823 stat = &stat[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().;
824 }
825
826 // The expected field cannot be in the file anymore when we are at EOF
827 if stat.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().() {
828 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(
829 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::InvalidDatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#InvalidData#,
830 "Stat file was not of the expected format",
831 ));
832 }
833
834 // we've now arrived at the field we are looking for, we now check how
835 // long this field is by finding where the next space is
836 let mut idx = 0;
837 while stat[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().idx]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne().b' ' && idx rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().stat.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() {
838 idx rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1;
839 }
840 let field = &stat[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#idx]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().;
841
842 // we first convert the data to a string slice, this should not fail with a normal /proc filesystem
843 let fielddata = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::from_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8().(field).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|_| {
844 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(
845 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::InvalidInputrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#InvalidInput#,
846 "Could not interpret byte slice as string",
847 )
848 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
849
850 // then we convert the string slice to whatever the requested type was
851 fielddata.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().().map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|_| {
852 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(
853 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::InvalidInputrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#InvalidInput#,
854 "Could not interpret string as number",
855 )
856 })
857 }
858
859 pub fn escape_os_str_lossyrust-analyzer cargo sudo-rs 0.2.14 system/escape_os_str_lossy().(s: &stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
860 s.to_string_lossyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsStr]to_string_lossy().().escape_defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]escape_default().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().()
861 }
862
863 pub fn make_zeroed_sigactionrust-analyzer cargo sudo-rs 0.2.14 system/make_zeroed_sigaction().() -> libcrust-analyzer cargo libc 0.2.186 crate/::sigactionrust-analyzer cargo libc 0.2.186 unix/linux_like/linux/gnu/b64/x86_64/sigaction# {
864 // SAFETY: since sigaction is a C struct, all-zeroes is a valid representation
865 // We cannot use a "literal struct" initialization method since the exact representation
866 // of libc::sigaction is not fixed, see e.g. https://github.com/trifectatechfoundation/sudo-rs/issues/829
867 unsafe { stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::zeroedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/zeroed().() }
868 }
869
870 #[cfg(all(test, target_os = "linux"))]
871 pub(craterust-analyzer cargo sudo-rs 0.2.14 crate/) const ROOT_GROUP_NAMErust-analyzer cargo sudo-rs 0.2.14 system/ROOT_GROUP_NAME.: &str = "root";
872
873 #[cfg(all(test, not(target_os = "linux")))]
874 pub(craterust-analyzer cargo sudo-rs 0.2.14 crate/) const ROOT_GROUP_NAME: &str = "wheel";
875
876 #[allow(clippy::undocumented_unsafe_blocks)]
877 #[cfg(test)]
878 mod testsrust-analyzer cargo sudo-rs 0.2.14 system/tests/ {
879 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{
880 ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::c_charrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_char#,
881 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{selfrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/, Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#, Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#},
882 osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/::{
883 fdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/::{AsFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/AsFd#, AsRawFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/raw/AsRawFd#},
884 unixrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/::netrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/net/::UnixStreamrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/net/stream/UnixStream#,
885 },
886 processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::exitrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/exit().,
887 };
888
889 use libcrust-analyzer cargo libc 0.2.186 crate/::SIGKILLrust-analyzer cargo libc 0.2.186 unix/linux_like/SIGKILL.;
890
891 use craterust-analyzer cargo sudo-rs 0.2.14 crate/::systemrust-analyzer cargo sudo-rs 0.2.14 system/::interfacerust-analyzer cargo sudo-rs 0.2.14 system/interface/::{GroupIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/GroupId#, ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#, UserIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/UserId#};
892
893 use superrust-analyzer cargo sudo-rs 0.2.14 system/::{
894 Grouprust-analyzer cargo sudo-rs 0.2.14 system/Group#, ROOT_GROUP_NAMErust-analyzer cargo sudo-rs 0.2.14 system/ROOT_GROUP_NAME., Userrust-analyzer cargo sudo-rs 0.2.14 system/User#, WithProcessrust-analyzer cargo sudo-rs 0.2.14 system/WithProcess#, fork_for_testrust-analyzer cargo sudo-rs 0.2.14 system/fork_for_test()., getpgrprust-analyzer cargo sudo-rs 0.2.14 system/getpgrp()., setpgidrust-analyzer cargo sudo-rs 0.2.14 system/setpgid().,
895 waitrust-analyzer cargo sudo-rs 0.2.14 system/wait/::{Waitrust-analyzer cargo sudo-rs 0.2.14 system/wait/Wait#, WaitOptionsrust-analyzer cargo sudo-rs 0.2.14 system/wait/WaitOptions#},
896 };
897
898 pub(superrust-analyzer cargo sudo-rs 0.2.14 system/) fn tempfilerust-analyzer cargo sudo-rs 0.2.14 system/tests/tempfile().() -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#> {
899 let timestamp = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::SystemTimerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/SystemTime#::nowrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[SystemTime]now().()
900 .duration_sincerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[SystemTime]duration_since().(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::UNIX_EPOCHrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/UNIX_EPOCH.)
901 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("Failed to get system time")
902 .as_nanosrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]as_nanos().();
903 let pid = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::idrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/id().();
904
905 let filename = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("sudo_rs_test_{pid}_{timestamp}");
906 let path = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#::fromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf][`From<&T>`]from().("/tmp").joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().(filename);
907 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::optionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]options().()
908 .readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]read().(true)
909 .writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]write().(true)
910 .create_newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]create_new().(true)
911 .openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]open().(path)
912 }
913
914 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
915 fn test_get_user_and_group_by_idrust-analyzer cargo sudo-rs 0.2.14 system/tests/test_get_user_and_group_by_id().() {
916 let fixed_users = &[
917 (UserIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/UserId#::ROOTrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[UserId]ROOT., "root"),
918 (Userrust-analyzer cargo sudo-rs 0.2.14 system/User#::from_namerust-analyzer cargo sudo-rs 0.2.14 system/impl#[User]from_name().(c"daemon").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().uidrust-analyzer cargo sudo-rs 0.2.14 system/User#uid., "daemon"),
919 ];
920 for &(id, name) in fixed_users {
921 let root = Userrust-analyzer cargo sudo-rs 0.2.14 system/User#::from_uidrust-analyzer cargo sudo-rs 0.2.14 system/impl#[User]from_uid().(id).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().();
922 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(root.uidrust-analyzer cargo sudo-rs 0.2.14 system/User#uid., id);
923 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(root.namerust-analyzer cargo sudo-rs 0.2.14 system/User#name., name);
924 }
925
926 let fixed_groups = &[
927 (GroupIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/GroupId#::newrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[GroupId]new().(0), ROOT_GROUP_NAMErust-analyzer cargo sudo-rs 0.2.14 system/ROOT_GROUP_NAME.),
928 (Grouprust-analyzer cargo sudo-rs 0.2.14 system/Group#::from_namerust-analyzer cargo sudo-rs 0.2.14 system/impl#[Group]from_name().(c"daemon").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().gidrust-analyzer cargo sudo-rs 0.2.14 system/Group#gid., "daemon"),
929 ];
930 for &(id, name) in fixed_groups {
931 let root = Grouprust-analyzer cargo sudo-rs 0.2.14 system/Group#::from_gidrust-analyzer cargo sudo-rs 0.2.14 system/impl#[Group]from_gid().(id).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().();
932 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(root.gidrust-analyzer cargo sudo-rs 0.2.14 system/Group#gid., id);
933 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(root.namerust-analyzer cargo sudo-rs 0.2.14 system/Group#name..unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(), name);
934 }
935 }
936
937 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
938 fn miri_test_group_implrust-analyzer cargo sudo-rs 0.2.14 system/tests/miri_test_group_impl().() {
939 use superrust-analyzer cargo sudo-rs 0.2.14 system/::Grouprust-analyzer cargo sudo-rs 0.2.14 system/Group#;
940 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::CStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/CString#;
941
942 fn testrust-analyzer cargo sudo-rs 0.2.14 system/tests/test().(name: &str, passwd: &str, gid: libcrust-analyzer cargo libc 0.2.186 crate/::gid_trust-analyzer cargo libc 0.2.186 unix/gid_t#, mem: &[&str]) {
943 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
944 {
945 let c_mem: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<CStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/CString#> =
946 mem.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|&s| CStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/CString#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/impl#[CString]new().(s).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
947 let c_name = CStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/CString#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/impl#[CString]new().(name).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
948 let c_passwd = CStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/CString#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/impl#[CString]new().(passwd).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
949 unsafe {
950 Grouprust-analyzer cargo sudo-rs 0.2.14 system/Group#::from_libcrust-analyzer cargo sudo-rs 0.2.14 system/impl#[Group]from_libc().(&libcrust-analyzer cargo libc 0.2.186 crate/::grouprust-analyzer cargo libc 0.2.186 unix/group# {
951 gr_namerust-analyzer cargo libc 0.2.186 unix/group#gr_name.: c_name.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]as_ptr().() as *mut _,
952 gr_passwdrust-analyzer cargo libc 0.2.186 unix/group#gr_passwd.: c_passwd.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]as_ptr().() as *mut _,
953 gr_gidrust-analyzer cargo libc 0.2.186 unix/group#gr_gid.: gid,
954 gr_memrust-analyzer cargo libc 0.2.186 unix/group#gr_mem.: c_mem
955 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
956 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|cs| cs.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]as_ptr().() as *mut _)
957 .chainrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#chain().(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/::oncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/sources/once/once().(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::null_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/null_mut().()))
958 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<*mut c_charrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_char#>>()
959 .as_mut_ptrrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_mut_ptr().(),
960 })
961 }
962 },
963 Grouprust-analyzer cargo sudo-rs 0.2.14 system/Group# {
964 namerust-analyzer cargo sudo-rs 0.2.14 system/Group#name.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(name.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()),
965 gidrust-analyzer cargo sudo-rs 0.2.14 system/Group#gid.: GroupIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/GroupId#::newrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[GroupId]new().(gid),
966 }
967 )
968 }
969
970 testrust-analyzer cargo sudo-rs 0.2.14 system/tests/test().("dr. bill", "fidelio", 1999, &["eyes", "wide", "shut"]);
971 testrust-analyzer cargo sudo-rs 0.2.14 system/tests/test().("eris", "fnord", 5, &[]);
972 testrust-analyzer cargo sudo-rs 0.2.14 system/tests/test().("abc", "password123", 42, &[""]);
973 }
974
975 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
976 fn get_process_tty_devicerust-analyzer cargo sudo-rs 0.2.14 system/tests/get_process_tty_device().() {
977 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(superrust-analyzer cargo sudo-rs 0.2.14 system/::Processrust-analyzer cargo sudo-rs 0.2.14 system/Process#::tty_device_idrust-analyzer cargo sudo-rs 0.2.14 system/impl#[Process]tty_device_id().(WithProcessrust-analyzer cargo sudo-rs 0.2.14 system/WithProcess#::Currentrust-analyzer cargo sudo-rs 0.2.14 system/WithProcess#Current#).is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().());
978 }
979
980 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
981 fn pgid_testrust-analyzer cargo sudo-rs 0.2.14 system/tests/pgid_test().() {
982 use superrust-analyzer cargo sudo-rs 0.2.14 system/::{getpgidrust-analyzer cargo sudo-rs 0.2.14 system/getpgid()., setpgidrust-analyzer cargo sudo-rs 0.2.14 system/setpgid().};
983
984 let pgrp = getpgrprust-analyzer cargo sudo-rs 0.2.14 system/getpgrp().();
985 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(getpgidrust-analyzer cargo sudo-rs 0.2.14 system/getpgid().(ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#::newrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[ProcessId]new().(0)).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), pgrp);
986 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
987 getpgidrust-analyzer cargo sudo-rs 0.2.14 system/getpgid().(ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#::newrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[ProcessId]new().(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::idrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/id().() as i32)).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(),
988 pgrp
989 );
990
991 let child_pid = unsafe {
992 superrust-analyzer cargo sudo-rs 0.2.14 system/::fork_for_testrust-analyzer cargo sudo-rs 0.2.14 system/fork_for_test().(|| {
993 // wait for the parent.
994 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::sleeprust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/sleep().(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_secsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_secs().(1));
995 exitrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/exit().(0);
996 })
997 };
998
999 // The child should be in our process group.
1000 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
1001 getpgidrust-analyzer cargo sudo-rs 0.2.14 system/getpgid().(child_pid).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(),
1002 getpgidrust-analyzer cargo sudo-rs 0.2.14 system/getpgid().(ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#::newrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[ProcessId]new().(0)).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(),
1003 );
1004 // Move the child to its own process group
1005 setpgidrust-analyzer cargo sudo-rs 0.2.14 system/setpgid().(child_pid, child_pid).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
1006 // The process group of the child should have changed.
1007 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(getpgidrust-analyzer cargo sudo-rs 0.2.14 system/getpgid().(child_pid).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), child_pid);
1008 }
1009 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
1010 fn kill_testrust-analyzer cargo sudo-rs 0.2.14 system/tests/kill_test().() {
1011 let mut child = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::Commandrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Command#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]new().("/bin/sleep")
1012 .argrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]arg().("1")
1013 .spawnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]spawn().()
1014 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
1015 superrust-analyzer cargo sudo-rs 0.2.14 system/::killrust-analyzer cargo sudo-rs 0.2.14 system/kill().(ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#::newrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[ProcessId]new().(child.idrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Child]id().() as i32), SIGKILLrust-analyzer cargo libc 0.2.186 unix/linux_like/SIGKILL.).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
1016 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().child.waitrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Child]wait().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().successrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[ExitStatus]success().());
1017 }
1018 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
1019 fn killpg_testrust-analyzer cargo sudo-rs 0.2.14 system/tests/killpg_test().() {
1020 // Create a socket so the children write to it if they aren't terminated by `killpg`.
1021 let (mut rx, mut tx) = UnixStreamrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/net/stream/UnixStream#::pairrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/net/stream/impl#[UnixStream]pair().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
1022
1023 let pid1 = unsafe {
1024 fork_for_testrust-analyzer cargo sudo-rs 0.2.14 system/fork_for_test().(|| {
1025 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::sleeprust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/sleep().(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_secsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_secs().(1));
1026 tx.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(&[42]).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
1027 exitrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/exit().(0);
1028 })
1029 };
1030
1031 let pid2 = unsafe {
1032 fork_for_testrust-analyzer cargo sudo-rs 0.2.14 system/fork_for_test().(|| {
1033 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::sleeprust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/sleep().(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_secsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_secs().(1));
1034 tx.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(&[42]).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
1035 exitrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/exit().(0);
1036 })
1037 };
1038
1039 droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(tx);
1040
1041 let pgid = pid1;
1042 // Move the children to their own process group.
1043 setpgidrust-analyzer cargo sudo-rs 0.2.14 system/setpgid().(pid1, pgid).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
1044 setpgidrust-analyzer cargo sudo-rs 0.2.14 system/setpgid().(pid2, pgid).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
1045 // Send `SIGKILL` to the children process group.
1046 superrust-analyzer cargo sudo-rs 0.2.14 system/::killpgrust-analyzer cargo sudo-rs 0.2.14 system/killpg().(pgid, SIGKILLrust-analyzer cargo libc 0.2.186 unix/linux_like/SIGKILL.).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
1047 // Ensure that the child were terminated before writing.
1048 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
1049 rx.read_exactrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_exact().(&mut [0; 2]).unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().().kindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]kind().(),
1050 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::UnexpectedEofrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#UnexpectedEof#
1051 );
1052 }
1053
1054 fn is_cloexecrust-analyzer cargo sudo-rs 0.2.14 system/tests/is_cloexec().<F: AsFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/AsFd#>(fd: &F) -> bool {
1055 craterust-analyzer cargo sudo-rs 0.2.14 crate/::cutilsrust-analyzer cargo sudo-rs 0.2.14 cutils/::cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().(unsafe { libcrust-analyzer cargo libc 0.2.186 crate/::fcntlrust-analyzer cargo libc 0.2.186 fcntl().(fd.as_fdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/AsFd#as_fd().().as_raw_fdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/impl#[`BorrowedFd<'_>`][AsRawFd]as_raw_fd().(), libcrust-analyzer cargo libc 0.2.186 crate/::F_GETFDrust-analyzer cargo libc 0.2.186 unix/linux_like/F_GETFD.) }).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()
1056 &rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitAnd<Self>`]bitand().libcrust-analyzer cargo libc 0.2.186 crate/::FD_CLOEXECrust-analyzer cargo libc 0.2.186 unix/FD_CLOEXEC.
1057 ==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq().libcrust-analyzer cargo libc 0.2.186 crate/::FD_CLOEXECrust-analyzer cargo libc 0.2.186 unix/FD_CLOEXEC.
1058 }
1059
1060 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
1061 fn mark_fds_as_cloexecrust-analyzer cargo sudo-rs 0.2.14 system/tests/mark_fds_as_cloexec().() {
1062 let child_pid = unsafe {
1063 fork_for_testrust-analyzer cargo sudo-rs 0.2.14 system/fork_for_test().(|| {
1064 let should_close =
1065 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::createrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]create().(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::temp_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/temp_dir().().joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().("should_close.txt")).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
1066 craterust-analyzer cargo sudo-rs 0.2.14 crate/::cutilsrust-analyzer cargo sudo-rs 0.2.14 cutils/::cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().(libcrust-analyzer cargo libc 0.2.186 crate/::fcntlrust-analyzer cargo libc 0.2.186 fcntl().(
1067 should_close.as_fdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/impl#[`fs::File`][AsFd]as_fd().().as_raw_fdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/impl#[`BorrowedFd<'_>`][AsRawFd]as_raw_fd().(),
1068 libcrust-analyzer cargo libc 0.2.186 crate/::F_SETFDrust-analyzer cargo libc 0.2.186 unix/linux_like/F_SETFD.,
1069 0,
1070 ))
1071 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
1072 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().is_cloexecrust-analyzer cargo sudo-rs 0.2.14 system/tests/is_cloexec().(&should_close));
1073
1074 superrust-analyzer cargo sudo-rs 0.2.14 system/::mark_fds_as_cloexecrust-analyzer cargo sudo-rs 0.2.14 system/mark_fds_as_cloexec().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
1075
1076 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(is_cloexecrust-analyzer cargo sudo-rs 0.2.14 system/tests/is_cloexec().(&should_close));
1077
1078 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().is_cloexecrust-analyzer cargo sudo-rs 0.2.14 system/tests/is_cloexec().(&iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::stdinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/stdin().()));
1079 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().is_cloexecrust-analyzer cargo sudo-rs 0.2.14 system/tests/is_cloexec().(&iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::stdoutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/stdout().()));
1080 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().is_cloexecrust-analyzer cargo sudo-rs 0.2.14 system/tests/is_cloexec().(&iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::stderrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/stderr().()));
1081
1082 exitrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/exit().(0)
1083 })
1084 };
1085
1086 let (_, status) = child_pid.waitrust-analyzer cargo sudo-rs 0.2.14 system/wait/impl#[ProcessId][Wait]wait().(WaitOptionsrust-analyzer cargo sudo-rs 0.2.14 system/wait/WaitOptions#::newrust-analyzer cargo sudo-rs 0.2.14 system/wait/impl#[WaitOptions]new().()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
1087 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(status.exit_statusrust-analyzer cargo sudo-rs 0.2.14 system/wait/impl#[WaitStatus]exit_status().(), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(0));
1088 }
1089
1090 #[cfg(target_os = "linux")]
1091 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
1092 fn proc_stat_testrust-analyzer cargo sudo-rs 0.2.14 system/tests/proc_stat_test().() {
1093 use superrust-analyzer cargo sudo-rs 0.2.14 system/::{Processrust-analyzer cargo sudo-rs 0.2.14 system/Process#, WithProcessrust-analyzer cargo sudo-rs 0.2.14 system/WithProcess#::Currentrust-analyzer cargo sudo-rs 0.2.14 system/WithProcess#Current#, read_proc_statrust-analyzer cargo sudo-rs 0.2.14 system/read_proc_stat().};
1094 // The process can be '(uninterruptible) sleeping' or 'running': it looks like the state
1095 // field of /proc/pid/stat will show the state for the main thread of the process rather
1096 // than for the process as a whole.
1097 let state = read_proc_statrust-analyzer cargo sudo-rs 0.2.14 system/read_proc_stat().::<char>(Currentrust-analyzer cargo sudo-rs 0.2.14 system/WithProcess#Current#, 2).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
1098 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!("SDR".containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]contains().(state), "{state} is not S, D or R");
1099 let parent = Processrust-analyzer cargo sudo-rs 0.2.14 system/Process#::parent_idrust-analyzer cargo sudo-rs 0.2.14 system/impl#[Process]parent_id().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().();
1100 // field 3 is always the parent process
1101 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
1102 parent,
1103 ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#::newrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[ProcessId]new().(read_proc_statrust-analyzer cargo sudo-rs 0.2.14 system/read_proc_stat().::<i32>(Currentrust-analyzer cargo sudo-rs 0.2.14 system/WithProcess#Current#, 3).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().())
1104 );
1105 // this next field should always be 0 (which precedes an important bit of info for us!)
1106 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(0, read_proc_statrust-analyzer cargo sudo-rs 0.2.14 system/read_proc_stat().::<i32>(Currentrust-analyzer cargo sudo-rs 0.2.14 system/WithProcess#Current#, 20).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().());
1107 }
1108 }