Debian Codegraph @

src/exec/use_pty/parent.rs

raw symbols SCIP
1 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::VecDequerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/VecDeque#;
2 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_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#;
3 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/;
4 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/::fdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/::{FromRawFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/raw/FromRawFd#, OwnedFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/OwnedFd#};
5 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/::{Commandrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Command#, Stdiorust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Stdio#};
6
7 use libcrust-analyzer cargo libc 0.2.186 crate/::{O_CLOEXECrust-analyzer cargo libc 0.2.186 unix/linux_like/linux/gnu/b64/x86_64/O_CLOEXEC., closerust-analyzer cargo libc 0.2.186 close().};
8
9 use 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().;
10 use craterust-analyzer cargo sudo-rs 0.2.14 crate/::execrust-analyzer cargo sudo-rs 0.2.14 exec/::eventrust-analyzer cargo sudo-rs 0.2.14 exec/event/::{EventHandlerust-analyzer cargo sudo-rs 0.2.14 exec/event/EventHandle#, EventRegistryrust-analyzer cargo sudo-rs 0.2.14 exec/event/EventRegistry#, PollEventrust-analyzer cargo sudo-rs 0.2.14 exec/event/PollEvent#, Processrust-analyzer cargo sudo-rs 0.2.14 exec/event/Process#, StopReasonrust-analyzer cargo sudo-rs 0.2.14 exec/event/StopReason#};
11 use craterust-analyzer cargo sudo-rs 0.2.14 crate/::execrust-analyzer cargo sudo-rs 0.2.14 exec/::use_ptyrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/::SIGCONT_FGrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/SIGCONT_FG.;
12 use craterust-analyzer cargo sudo-rs 0.2.14 crate/::execrust-analyzer cargo sudo-rs 0.2.14 exec/::use_ptyrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/::monitorrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/monitor/::exec_monitorrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/monitor/exec_monitor().;
13 use craterust-analyzer cargo sudo-rs 0.2.14 crate/::execrust-analyzer cargo sudo-rs 0.2.14 exec/::{
14 ExitReasonrust-analyzer cargo sudo-rs 0.2.14 exec/ExitReason#, SpawnNoexecHandlerrust-analyzer cargo sudo-rs 0.2.14 exec/noexec/SpawnNoexecHandler#,
15 io_utilrust-analyzer cargo sudo-rs 0.2.14 exec/io_util/::retry_while_interruptedrust-analyzer cargo sudo-rs 0.2.14 exec/io_util/retry_while_interrupted().,
16 use_ptyrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/::backchannelrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/::{BackchannelPairrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/BackchannelPair#, MonitorMessagerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/MonitorMessage#, ParentBackchannelrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/ParentBackchannel#, ParentMessagerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/ParentMessage#},
17 };
18 use craterust-analyzer cargo sudo-rs 0.2.14 crate/::execrust-analyzer cargo sudo-rs 0.2.14 exec/::{HandleSigchldrust-analyzer cargo sudo-rs 0.2.14 exec/HandleSigchld#, cond_fmtrust-analyzer cargo sudo-rs 0.2.14 exec/cond_fmt()., handle_sigchldrust-analyzer cargo sudo-rs 0.2.14 exec/handle_sigchld()., signal_fmtrust-analyzer cargo sudo-rs 0.2.14 exec/signal_fmt()., terminate_processrust-analyzer cargo sudo-rs 0.2.14 exec/terminate_process().};
19 use craterust-analyzer cargo sudo-rs 0.2.14 crate/::logrust-analyzer cargo sudo-rs 0.2.14 log/::{dev_errorrust-analyzer cargo sudo-rs 0.2.14 log/dev_error!, dev_inforust-analyzer cargo sudo-rs 0.2.14 log/dev_info!, dev_warnrust-analyzer cargo sudo-rs 0.2.14 log/dev_warn!};
20 use craterust-analyzer cargo sudo-rs 0.2.14 crate/::systemrust-analyzer cargo sudo-rs 0.2.14 system/::signalrust-analyzer cargo sudo-rs 0.2.14 system/signal/::{
21 SignalHandlerrust-analyzer cargo sudo-rs 0.2.14 system/signal/handler/SignalHandler#, SignalHandlerBehaviorrust-analyzer cargo sudo-rs 0.2.14 system/signal/handler/SignalHandlerBehavior#, SignalNumberrust-analyzer cargo sudo-rs 0.2.14 system/signal/SignalNumber#, SignalSetrust-analyzer cargo sudo-rs 0.2.14 system/signal/set/SignalSet#, SignalStreamrust-analyzer cargo sudo-rs 0.2.14 system/signal/stream/SignalStream#, SignalsStaterust-analyzer cargo sudo-rs 0.2.14 system/signal/state/SignalsState#,
22 constsrust-analyzer cargo sudo-rs 0.2.14 system/signal/consts/::*, register_handlersrust-analyzer cargo sudo-rs 0.2.14 system/signal/stream/register_handlers().,
23 };
24 use craterust-analyzer cargo sudo-rs 0.2.14 crate/::systemrust-analyzer cargo sudo-rs 0.2.14 system/::termrust-analyzer cargo sudo-rs 0.2.14 system/term/::{Ptyrust-analyzer cargo sudo-rs 0.2.14 system/term/Pty#, PtyFollowerrust-analyzer cargo sudo-rs 0.2.14 system/term/PtyFollower#, PtyLeaderrust-analyzer cargo sudo-rs 0.2.14 system/term/PtyLeader#, TermSizerust-analyzer cargo sudo-rs 0.2.14 system/term/TermSize#, Terminalrust-analyzer cargo sudo-rs 0.2.14 system/term/Terminal#, UserTermrust-analyzer cargo sudo-rs 0.2.14 system/term/user_term/UserTerm#};
25 use craterust-analyzer cargo sudo-rs 0.2.14 crate/::systemrust-analyzer cargo sudo-rs 0.2.14 system/::waitrust-analyzer cargo sudo-rs 0.2.14 system/wait/::WaitOptionsrust-analyzer cargo sudo-rs 0.2.14 system/wait/WaitOptions#;
26 use craterust-analyzer cargo sudo-rs 0.2.14 crate/::systemrust-analyzer cargo sudo-rs 0.2.14 system/::{_exitrust-analyzer cargo sudo-rs 0.2.14 system/_exit()., ForkResultrust-analyzer cargo sudo-rs 0.2.14 system/ForkResult#, Grouprust-analyzer cargo sudo-rs 0.2.14 system/Group#, Userrust-analyzer cargo sudo-rs 0.2.14 system/User#, chownrust-analyzer cargo sudo-rs 0.2.14 system/chown()., forkrust-analyzer cargo sudo-rs 0.2.14 system/fork()., getpgrprust-analyzer cargo sudo-rs 0.2.14 system/getpgrp()., killrust-analyzer cargo sudo-rs 0.2.14 system/kill()., killpgrust-analyzer cargo sudo-rs 0.2.14 system/killpg().};
27 use craterust-analyzer cargo sudo-rs 0.2.14 crate/::systemrust-analyzer cargo sudo-rs 0.2.14 system/::{getpgidrust-analyzer cargo sudo-rs 0.2.14 system/getpgid()., interfacerust-analyzer cargo sudo-rs 0.2.14 system/interface/::ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#};
28
29 use superrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/::piperust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/::Piperust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/Pipe#;
30 use superrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/::{CommandStatusrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/CommandStatus#, SIGCONT_BGrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/SIGCONT_BG.};
31
32 pub(in craterust-analyzer cargo sudo-rs 0.2.14 crate/::execrust-analyzer cargo sudo-rs 0.2.14 exec/) fn exec_ptyrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/exec_pty().(
33 sudo_pid: ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#,
34 spawn_noexec_handler: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<SpawnNoexecHandlerrust-analyzer cargo sudo-rs 0.2.14 exec/noexec/SpawnNoexecHandler#>,
35 mut command: Commandrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Command#,
36 user_tty: UserTermrust-analyzer cargo sudo-rs 0.2.14 system/term/user_term/UserTerm#,
37 pty_owner: &Userrust-analyzer cargo sudo-rs 0.2.14 system/User#,
38 background: bool,
39 ) -> 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#<ExitReasonrust-analyzer cargo sudo-rs 0.2.14 exec/ExitReason#> {
40 // Allocate a pseudoterminal.
41 let pty = get_ptyrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/get_pty().(pty_owner)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
42
43 let mut original_signals = SignalsStaterust-analyzer cargo sudo-rs 0.2.14 system/signal/state/SignalsState#::saverust-analyzer cargo sudo-rs 0.2.14 system/signal/state/impl#[SignalsState]save().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
44
45 // Create backchannels to communicate with the monitor.
46 let mut backchannels = BackchannelPairrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/BackchannelPair#::newrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/impl#[BackchannelPair]new().().map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|err| {
47 dev_errorrust-analyzer cargo sudo-rs 0.2.14 log/dev_error!!("cannot create backchannel: {err}");
48 err
49 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
50
51 // We don't want to receive SIGTTIN/SIGTTOU
52 match SignalHandlerrust-analyzer cargo sudo-rs 0.2.14 system/signal/handler/SignalHandler#::registerrust-analyzer cargo sudo-rs 0.2.14 system/signal/handler/impl#[SignalHandler]register().(
53 SIGTTINrust-analyzer cargo libc 0.2.186 unix/linux_like/linux/gnu/b64/x86_64/SIGTTIN.,
54 SignalHandlerBehaviorrust-analyzer cargo sudo-rs 0.2.14 system/signal/handler/SignalHandlerBehavior#::Ignorerust-analyzer cargo sudo-rs 0.2.14 system/signal/handler/SignalHandlerBehavior#Ignore#,
55 &mut original_signals,
56 ) {
57 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(handler) => handler.forgetrust-analyzer cargo sudo-rs 0.2.14 system/signal/handler/impl#[SignalHandler]forget().(),
58 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => dev_warnrust-analyzer cargo sudo-rs 0.2.14 log/dev_warn!!("cannot set handler for SIGTTIN: {err}"),
59 }
60 match SignalHandlerrust-analyzer cargo sudo-rs 0.2.14 system/signal/handler/SignalHandler#::registerrust-analyzer cargo sudo-rs 0.2.14 system/signal/handler/impl#[SignalHandler]register().(
61 SIGTTOUrust-analyzer cargo libc 0.2.186 unix/linux_like/linux/gnu/b64/x86_64/SIGTTOU.,
62 SignalHandlerBehaviorrust-analyzer cargo sudo-rs 0.2.14 system/signal/handler/SignalHandlerBehavior#::Ignorerust-analyzer cargo sudo-rs 0.2.14 system/signal/handler/SignalHandlerBehavior#Ignore#,
63 &mut original_signals,
64 ) {
65 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(handler) => handler.forgetrust-analyzer cargo sudo-rs 0.2.14 system/signal/handler/impl#[SignalHandler]forget().(),
66 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => dev_warnrust-analyzer cargo sudo-rs 0.2.14 log/dev_warn!!("cannot set handler for SIGTTOU: {err}"),
67 }
68
69 // FIXME (ogsudo): Initialize the policy plugin's session here by calling
70 // `policy_init_session`.
71 // FIXME (ogsudo): initializes ttyblock sigset here by calling `init_ttyblock`
72
73 // Fetch the parent process group so we can send signals to it.
74 let parent_pgrp = getpgrprust-analyzer cargo sudo-rs 0.2.14 system/getpgrp().();
75
76 // Set all the IO streams for the command to the follower side of the pty.
77 let clone_follower = || -> 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#<PtyFollowerrust-analyzer cargo sudo-rs 0.2.14 system/term/PtyFollower#> {
78 pty.followerrust-analyzer cargo sudo-rs 0.2.14 system/term/Pty#follower..try_clonerust-analyzer cargo sudo-rs 0.2.14 system/term/impl#[PtyFollower]try_clone().().map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|err| {
79 dev_errorrust-analyzer cargo sudo-rs 0.2.14 log/dev_error!!("cannot clone pty follower: {err}");
80 err
81 })
82 };
83
84 command.stdinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]stdin().(clone_follower()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().);
85 command.stdoutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]stdout().(clone_follower()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().);
86 command.stderrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]stderr().(clone_follower()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().);
87
88 let mut registry = EventRegistryrust-analyzer cargo sudo-rs 0.2.14 exec/event/EventRegistry#::<ParentClosurerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#>::newrust-analyzer cargo sudo-rs 0.2.14 exec/event/impl#[`EventRegistry<T>`]new().();
89
90 // Pipe data between both terminals
91 let mut tty_pipe = Piperust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/Pipe#::newrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/impl#[`Pipe<L, R>`]new().(
92 user_tty,
93 pty.leaderrust-analyzer cargo sudo-rs 0.2.14 system/term/Pty#leader.,
94 &mut registry,
95 ParentEventrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentEvent#::Ttyrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentEvent#Tty#,
96 ParentEventrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentEvent#::Ptyrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentEvent#Pty#,
97 );
98
99 if background {
100 tty_pipe.disable_inputrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/impl#[`Pipe<L, R>`]disable_input().(&mut registry);
101 }
102
103 let user_tty = tty_pipe.left_mutrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/impl#[`Pipe<L, R>`]left_mut().();
104
105 // Check if we are the foreground process
106 let mut foreground = user_tty
107 .tcgetpgrprust-analyzer cargo sudo-rs 0.2.14 system/term/impl#[F][Terminal]tcgetpgrp().()
108 .is_ok_andrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok_and().(|tty_pgrp| tty_pgrp rust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[ProcessId][`PartialEq<Self>`]eq().==rust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[ProcessId][`PartialEq<Self>`]eq(). rust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[ProcessId][`PartialEq<Self>`]eq().parent_pgrp);
109 dev_inforust-analyzer cargo sudo-rs 0.2.14 log/dev_info!!(
110 "sudo is running in the {}",
111 cond_fmtrust-analyzer cargo sudo-rs 0.2.14 exec/cond_fmt().(foreground, "foreground", "background")
112 );
113
114 // FIXME: maybe all these boolean flags should be on a dedicated type.
115
116 // Whether the command should be executed in the background (this is not the `-b` flag)
117 let mut exec_bg = false;
118 // Whether the user's terminal is in raw mode or not.
119 let mut term_raw = false;
120 // Whether to preserve oflag for the terminal
121 let mut preserve_oflag = false;
122
123 // Check if we are part of a pipeline.
124 // FIXME: Here's where we should intercept the IO streams if we want to implement IO logging.
125 // FIXME: ogsudo creates pipes for the IO streams and uses events to read from the strams to
126 // the pipes. Investigate why.
127 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().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().().is_terminal_for_pgrprust-analyzer cargo sudo-rs 0.2.14 system/term/impl#[F][Terminal]is_terminal_for_pgrp().(parent_pgrp) {
128 if background {
129 // Running in background (sudo -b), no access to terminal input.
130 // In non-pty mode, the command runs in an orphaned process
131 // group and reads from the controlling terminal fail with EIO.
132 // We cannot do the same while running in a pty but if we set
133 // stdin to a half-closed pipe, reads from it will get EOF.
134 dev_inforust-analyzer cargo sudo-rs 0.2.14 log/dev_info!!("stdin is not a terminal, creating a pipe");
135 exec_bg = true;
136
137 let mut pipes = [-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().1, -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().1];
138 // SAFETY: A valid pointer to a mutable array of 2 fds is passed in.
139 unsafe {
140 cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().(libcrust-analyzer cargo libc 0.2.186 crate/::pipe2rust-analyzer cargo libc 0.2.186 pipe2().(pipes.as_mut_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_mut_ptr().(), O_CLOEXECrust-analyzer cargo libc 0.2.186 unix/linux_like/linux/gnu/b64/x86_64/O_CLOEXEC.))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
141 }
142 // SAFETY: pipe2 created two owned pipe fds.
143 unsafe {
144 command.stdinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]stdin().(OwnedFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/OwnedFd#::from_raw_fdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/impl#[OwnedFd][FromRawFd]from_raw_fd().(pipes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().));
145 closerust-analyzer cargo libc 0.2.186 close().(pipes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().);
146 }
147 } else {
148 dev_inforust-analyzer cargo sudo-rs 0.2.14 log/dev_info!!("stdin is not a terminal, command will inherit it");
149 if 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().().is_pipe_or_socketrust-analyzer cargo sudo-rs 0.2.14 system/term/impl#[F][Terminal]is_pipe_or_socket().() {
150 exec_bg = true;
151 }
152 command.stdinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]stdin().(Stdiorust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Stdio#::inheritrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Stdio]inherit().());
153 }
154
155 if foreground && parent_pgrp rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().sudo_pid {
156 // If sudo is not the process group leader and stdin is not a terminal we might be
157 // running as a background job via a shell script. Starting in the foreground would
158 // change the terminal mode.
159 exec_bg = true;
160 }
161 }
162
163 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().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().().is_terminal_for_pgrprust-analyzer cargo sudo-rs 0.2.14 system/term/impl#[F][Terminal]is_terminal_for_pgrp().(parent_pgrp) {
164 dev_inforust-analyzer cargo sudo-rs 0.2.14 log/dev_info!!("stdout is not a terminal, command will inherit it");
165 if 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().().is_pipe_or_socketrust-analyzer cargo sudo-rs 0.2.14 system/term/impl#[F][Terminal]is_pipe_or_socket().() {
166 exec_bg = true;
167 preserve_oflag = true;
168 }
169 command.stdoutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]stdout().(Stdiorust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Stdio#::inheritrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Stdio]inherit().());
170 }
171
172 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().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().().is_terminal_for_pgrprust-analyzer cargo sudo-rs 0.2.14 system/term/impl#[F][Terminal]is_terminal_for_pgrp().(parent_pgrp) {
173 dev_inforust-analyzer cargo sudo-rs 0.2.14 log/dev_info!!("stderr is not a terminal, command will inherit it");
174 command.stderrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]stderr().(Stdiorust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Stdio#::inheritrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Stdio]inherit().());
175 }
176
177 // If there is another process later in the pipeline, don't interfere
178 // with its access to the Tty
179 if 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().().is_pipe_or_socketrust-analyzer cargo sudo-rs 0.2.14 system/term/impl#[F][Terminal]is_pipe_or_socket().() {
180 foreground = false;
181 }
182
183 // Copy terminal settings from `/dev/tty` to the pty.
184 if let Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) = user_tty.copy_torust-analyzer cargo sudo-rs 0.2.14 system/term/user_term/impl#[UserTerm]copy_to().(&pty.followerrust-analyzer cargo sudo-rs 0.2.14 system/term/Pty#follower.) {
185 dev_errorrust-analyzer cargo sudo-rs 0.2.14 log/dev_error!!("cannot copy terminal settings to pty: {err}");
186 foreground = false;
187 }
188
189 // Start in raw mode unless we're part of a pipeline or backgrounded.
190 if foreground && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().exec_bg {
191 // Clearer this way that set_raw_mode only conditionally runs
192 if user_tty.set_raw_moderust-analyzer cargo sudo-rs 0.2.14 system/term/user_term/impl#[UserTerm]set_raw_mode().(false, preserve_oflag).is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().() {
193 term_raw = true;
194 }
195 }
196
197 let tty_size = tty_pipe.leftrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/impl#[`Pipe<L, R>`]left().().get_sizerust-analyzer cargo sudo-rs 0.2.14 system/term/user_term/impl#[UserTerm]get_size().().map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|err| {
198 dev_errorrust-analyzer cargo sudo-rs 0.2.14 log/dev_error!!("cannot get terminal size: {err}");
199 err
200 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
201
202 // Block all the signals until we are done setting up the signal handlers so we don't miss
203 // SIGCHLD.
204 let original_set = match SignalSetrust-analyzer cargo sudo-rs 0.2.14 system/signal/set/SignalSet#::fullrust-analyzer cargo sudo-rs 0.2.14 system/signal/set/impl#[SignalSet]full().().and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]and_then().(|set| set.blockrust-analyzer cargo sudo-rs 0.2.14 system/signal/set/impl#[SignalSet]block().()) {
205 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(original_set) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(original_set),
206 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => {
207 dev_warnrust-analyzer cargo sudo-rs 0.2.14 log/dev_warn!!("cannot block signals: {err}");
208 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#
209 }
210 };
211
212 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().foreground {
213 tty_pipe.disable_inputrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/impl#[`Pipe<L, R>`]disable_input().(&mut registry);
214 }
215
216 // SAFETY: There should be no other threads at this point.
217 let ForkResultrust-analyzer cargo sudo-rs 0.2.14 system/ForkResult#::Parentrust-analyzer cargo sudo-rs 0.2.14 system/ForkResult#Parent#(monitor_pid) = (unsafe { forkrust-analyzer cargo sudo-rs 0.2.14 system/fork().() }).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|err| {
218 dev_errorrust-analyzer cargo sudo-rs 0.2.14 log/dev_error!!("cannot fork monitor process: {err}");
219 err
220 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().
221 else {
222 // Close the file descriptors that we don't access
223 droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(tty_pipe);
224 droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(backchannels.parentrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/BackchannelPair#parent.);
225
226 // If `exec_monitor` returns, it means we failed to execute the command somehow.
227 match exec_monitorrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/monitor/exec_monitor().(
228 pty.followerrust-analyzer cargo sudo-rs 0.2.14 system/term/Pty#follower.,
229 command,
230 foreground && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().exec_bg,
231 &mut backchannels.monitorrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/BackchannelPair#monitor.,
232 original_set,
233 original_signals,
234 ) {
235 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(exec_output) => match exec_output {},
236 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => {
237 // Disable nonblocking assertions as we will not poll the backchannel anymore.
238 backchannels.monitorrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/BackchannelPair#monitor..set_nonblocking_assertionsrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/impl#[MonitorBackchannel]set_nonblocking_assertions().(true);
239
240 match err.try_intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`TryInto<U>`]try_into().() {
241 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(msg) => {
242 if let Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) = backchannels.monitorrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/BackchannelPair#monitor..sendrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/impl#[MonitorBackchannel]send().(&msg) {
243 dev_errorrust-analyzer cargo sudo-rs 0.2.14 log/dev_error!!("cannot send status to parent: {err}");
244 }
245 }
246 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => {
247 dev_warnrust-analyzer cargo sudo-rs 0.2.14 log/dev_warn!!("execution error {err:?} cannot be send over backchannel")
248 }
249 }
250 }
251 }
252
253 // We call `_exit` instead of `exit` to avoid flushing the parent's IO streams by accident.
254 _exitrust-analyzer cargo sudo-rs 0.2.14 system/_exit().(1);
255 };
256
257 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(spawner) = spawn_noexec_handler {
258 spawner.spawnrust-analyzer cargo sudo-rs 0.2.14 exec/noexec/impl#[SpawnNoexecHandler]spawn().();
259 }
260
261 // Close the file descriptors that we don't access
262 droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(pty.followerrust-analyzer cargo sudo-rs 0.2.14 system/term/Pty#follower.);
263 droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(backchannels.monitorrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/BackchannelPair#monitor.);
264
265 // Send green light to the monitor after closing the follower.
266 retry_while_interruptedrust-analyzer cargo sudo-rs 0.2.14 exec/io_util/retry_while_interrupted().(|| backchannels.parentrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/BackchannelPair#parent..sendrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/impl#[ParentBackchannel]send().(&MonitorMessagerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/MonitorMessage#::Edgerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/MonitorMessage#Edge#)).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|err| {
267 dev_errorrust-analyzer cargo sudo-rs 0.2.14 log/dev_error!!("cannot send green light to monitor: {err}");
268 err
269 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
270
271 let mut closure = ParentClosurerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#::newrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]new().(
272 monitor_pid,
273 sudo_pid,
274 parent_pgrp,
275 backchannels.parentrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/BackchannelPair#parent.,
276 tty_pipe,
277 tty_size,
278 foreground,
279 term_raw,
280 preserve_oflag,
281 &mut registry,
282 original_signals,
283 )?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
284
285 // Restore the signal mask now that the handlers have been setup.
286 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(set) = original_set {
287 if let Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) = set.set_maskrust-analyzer cargo sudo-rs 0.2.14 system/signal/set/impl#[SignalSet]set_mask().() {
288 dev_warnrust-analyzer cargo sudo-rs 0.2.14 log/dev_warn!!("cannot restore signal mask: {err}");
289 }
290 }
291
292 let exit_reason = closure.runrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]run().(registry);
293 // FIXME (ogsudo): Retry if `/dev/tty` is revoked.
294
295 // Flush the terminal
296 closure.tty_piperust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#tty_pipe..rightrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/impl#[`Pipe<L, R>`]right().().set_nonblockingrust-analyzer cargo sudo-rs 0.2.14 system/term/impl#[PtyLeader]set_nonblocking().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
297 closure.tty_piperust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#tty_pipe..flush_leftrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/impl#[`Pipe<L, R>`]flush_left().().okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().();
298
299 // Restore the terminal settings
300 if closure.term_rawrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#term_raw. {
301 // Only restore the terminal if sudo is the foreground process.
302 if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(pgrp) = closure.tty_piperust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#tty_pipe..leftrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/impl#[`Pipe<L, R>`]left().().tcgetpgrprust-analyzer cargo sudo-rs 0.2.14 system/term/impl#[F][Terminal]tcgetpgrp().() {
303 if pgrp rust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[ProcessId][`PartialEq<Self>`]eq().==rust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[ProcessId][`PartialEq<Self>`]eq(). rust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[ProcessId][`PartialEq<Self>`]eq().closure.parent_pgrprust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#parent_pgrp. {
304 match closure.tty_piperust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#tty_pipe..left_mutrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/impl#[`Pipe<L, R>`]left_mut().().restorerust-analyzer cargo sudo-rs 0.2.14 system/term/user_term/impl#[UserTerm]restore().(false) {
305 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) => closure.term_rawrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#term_raw. = false,
306 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => dev_warnrust-analyzer cargo sudo-rs 0.2.14 log/dev_warn!!("cannot restore terminal settings: {err}"),
307 }
308 }
309 }
310 }
311
312 // Restore signal handlers
313 droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(closure.signal_handlersrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#signal_handlers.);
314
315 exit_reason
316 }
317
318 fn get_ptyrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/get_pty().(pty_owner: &Userrust-analyzer cargo sudo-rs 0.2.14 system/User#) -> 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#<Ptyrust-analyzer cargo sudo-rs 0.2.14 system/term/Pty#> {
319 let tty_gid = 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"tty")
320 .unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_or().(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#)
321 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|group| group.gidrust-analyzer cargo sudo-rs 0.2.14 system/Group#gid.);
322
323 let pty = Ptyrust-analyzer cargo sudo-rs 0.2.14 system/term/Pty#::openrust-analyzer cargo sudo-rs 0.2.14 system/term/impl#[Pty]open().().map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|err| {
324 dev_errorrust-analyzer cargo sudo-rs 0.2.14 log/dev_error!!("cannot allocate pty: {err}");
325 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().(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#::NotFoundrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#NotFound#, "unable to open pty")
326 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
327
328 let gid = tty_gid.unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(Userrust-analyzer cargo sudo-rs 0.2.14 system/User#::effective_gidrust-analyzer cargo sudo-rs 0.2.14 system/impl#[User]effective_gid().());
329 chownrust-analyzer cargo sudo-rs 0.2.14 system/chown().(&pty.pathrust-analyzer cargo sudo-rs 0.2.14 system/term/Pty#path., pty_owner.uidrust-analyzer cargo sudo-rs 0.2.14 system/User#uid., gid).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|err| {
330 dev_errorrust-analyzer cargo sudo-rs 0.2.14 log/dev_error!!("cannot change owner for pty: {err}");
331 err
332 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
333
334 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(pty)
335 }
336
337 struct ParentClosurerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure# {
338 // The monitor PID.
339 //
340 /// This is `Some` iff the process is still running.
341 monitor_pidrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#monitor_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#>,
342 sudo_pidrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#sudo_pid.: ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#,
343 parent_pgrprust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#parent_pgrp.: ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#,
344 command_pidrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#command_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#>,
345 tty_piperust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#tty_pipe.: Piperust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/Pipe#<UserTermrust-analyzer cargo sudo-rs 0.2.14 system/term/user_term/UserTerm#, PtyLeaderrust-analyzer cargo sudo-rs 0.2.14 system/term/PtyLeader#>,
346 tty_sizerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#tty_size.: TermSizerust-analyzer cargo sudo-rs 0.2.14 system/term/TermSize#,
347 foregroundrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#foreground.: bool,
348 term_rawrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#term_raw.: bool,
349 preserve_oflagrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#preserve_oflag.: bool,
350 backchannelrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#backchannel.: ParentBackchannelrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/ParentBackchannel#,
351 message_queuerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#message_queue.: VecDequerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/VecDeque#<MonitorMessagerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/MonitorMessage#>,
352 backchannel_write_handlerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#backchannel_write_handle.: EventHandlerust-analyzer cargo sudo-rs 0.2.14 exec/event/EventHandle#,
353 original_signalsrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#original_signals.: SignalsStaterust-analyzer cargo sudo-rs 0.2.14 system/signal/state/SignalsState#,
354 signal_streamrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#signal_stream.: &'static SignalStreamrust-analyzer cargo sudo-rs 0.2.14 system/signal/stream/SignalStream#,
355 signal_handlersrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#signal_handlers.: [SignalHandlerrust-analyzer cargo sudo-rs 0.2.14 system/signal/handler/SignalHandler#; ParentClosurerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#::SIGNALS.len()],
356 }
357
358 impl ParentClosurerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure# {
359 const SIGNALSrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]SIGNALS.: [SignalNumberrust-analyzer cargo sudo-rs 0.2.14 system/signal/SignalNumber#; 11] = [
360 SIGINTrust-analyzer cargo libc 0.2.186 unix/linux_like/SIGINT., SIGQUITrust-analyzer cargo libc 0.2.186 unix/linux_like/SIGQUIT., SIGTSTPrust-analyzer cargo libc 0.2.186 unix/linux_like/linux/gnu/b64/x86_64/SIGTSTP., SIGTERMrust-analyzer cargo libc 0.2.186 unix/linux_like/SIGTERM., SIGHUPrust-analyzer cargo libc 0.2.186 unix/linux_like/SIGHUP., SIGALRMrust-analyzer cargo libc 0.2.186 unix/linux_like/SIGALRM., SIGUSR1rust-analyzer cargo libc 0.2.186 unix/linux_like/linux/gnu/b64/x86_64/SIGUSR1., SIGUSR2rust-analyzer cargo libc 0.2.186 unix/linux_like/linux/gnu/b64/x86_64/SIGUSR2., SIGCHLDrust-analyzer cargo libc 0.2.186 unix/linux_like/linux/gnu/b64/x86_64/SIGCHLD., SIGCONTrust-analyzer cargo libc 0.2.186 unix/linux_like/linux/gnu/b64/x86_64/SIGCONT.,
361 SIGWINCHrust-analyzer cargo libc 0.2.186 unix/linux_like/linux/gnu/b64/x86_64/SIGWINCH.,
362 ];
363
364 #[allow(clippy::too_many_arguments)]
365 fn newrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]new().(
366 monitor_pid: ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#,
367 sudo_pid: ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#,
368 parent_pgrp: ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#,
369 mut backchannel: ParentBackchannelrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/ParentBackchannel#,
370 tty_pipe: Piperust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/Pipe#<UserTermrust-analyzer cargo sudo-rs 0.2.14 system/term/user_term/UserTerm#, PtyLeaderrust-analyzer cargo sudo-rs 0.2.14 system/term/PtyLeader#>,
371 tty_size: TermSizerust-analyzer cargo sudo-rs 0.2.14 system/term/TermSize#,
372 foreground: bool,
373 term_raw: bool,
374 preserve_oflag: bool,
375 registry: &mut EventRegistryrust-analyzer cargo sudo-rs 0.2.14 exec/event/EventRegistry#<Selfrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]>,
376 mut original_signals: SignalsStaterust-analyzer cargo sudo-rs 0.2.14 system/signal/state/SignalsState#,
377 ) -> 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#<Selfrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]> {
378 // Enable nonblocking assertions as we will poll this inside the event loop.
379 backchannel.set_nonblocking_assertsrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/impl#[ParentBackchannel]set_nonblocking_asserts().(true);
380
381 registry.register_eventrust-analyzer cargo sudo-rs 0.2.14 exec/event/impl#[`EventRegistry<T>`]register_event().(&backchannel, PollEventrust-analyzer cargo sudo-rs 0.2.14 exec/event/PollEvent#::Readablerust-analyzer cargo sudo-rs 0.2.14 exec/event/PollEvent#Readable#, ParentEventrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentEvent#::Backchannelrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentEvent#Backchannel#);
382 let mut backchannel_write_handle =
383 registry.register_eventrust-analyzer cargo sudo-rs 0.2.14 exec/event/impl#[`EventRegistry<T>`]register_event().(&backchannel, PollEventrust-analyzer cargo sudo-rs 0.2.14 exec/event/PollEvent#::Writablerust-analyzer cargo sudo-rs 0.2.14 exec/event/PollEvent#Writable#, ParentEventrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentEvent#::Backchannelrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentEvent#Backchannel#);
384 // Ignore write events on the backchannel as we don't want to poll it for writing if there
385 // are no messages in the queue.
386 backchannel_write_handle.ignorerust-analyzer cargo sudo-rs 0.2.14 exec/event/impl#[EventHandle]ignore().(registry);
387
388 let signal_stream = SignalStreamrust-analyzer cargo sudo-rs 0.2.14 system/signal/stream/SignalStream#::initrust-analyzer cargo sudo-rs 0.2.14 system/signal/stream/impl#[SignalStream]init().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
389
390 registry.register_eventrust-analyzer cargo sudo-rs 0.2.14 exec/event/impl#[`EventRegistry<T>`]register_event().(signal_stream, PollEventrust-analyzer cargo sudo-rs 0.2.14 exec/event/PollEvent#::Readablerust-analyzer cargo sudo-rs 0.2.14 exec/event/PollEvent#Readable#, |_| ParentEventrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentEvent#::Signalrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentEvent#Signal#);
391
392 let signal_handlers = register_handlersrust-analyzer cargo sudo-rs 0.2.14 system/signal/stream/register_handlers().(Selfrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]::SIGNALSrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]SIGNALS., &mut original_signals)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
393
394 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure] {
395 monitor_pidrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#monitor_pid.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(monitor_pid),
396 sudo_pidrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#sudo_pid.,
397 parent_pgrprust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#parent_pgrp.,
398 command_pidrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#command_pid.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
399 tty_piperust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#tty_pipe.,
400 tty_sizerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#tty_size.,
401 foregroundrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#foreground.,
402 term_rawrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#term_raw.,
403 preserve_oflagrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#preserve_oflag.,
404 backchannelrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#backchannel.,
405 message_queuerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#message_queue.: VecDequerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/VecDeque#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/impl#[`VecDeque<T>`]new().(),
406 backchannel_write_handlerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#backchannel_write_handle.,
407 original_signalsrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#original_signals.,
408 signal_streamrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#signal_stream.,
409 signal_handlersrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#signal_handlers.,
410 })
411 }
412
413 fn runrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]run().(&mut self, registry: EventRegistryrust-analyzer cargo sudo-rs 0.2.14 exec/event/EventRegistry#<Selfrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]>) -> 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#<ExitReasonrust-analyzer cargo sudo-rs 0.2.14 exec/ExitReason#> {
414 let result = match registry.event_looprust-analyzer cargo sudo-rs 0.2.14 exec/event/impl#[`EventRegistry<T>`]event_loop().(self) {
415 StopReasonrust-analyzer cargo sudo-rs 0.2.14 exec/event/StopReason#::Breakrust-analyzer cargo sudo-rs 0.2.14 exec/event/StopReason#Break#(err) | StopReasonrust-analyzer cargo sudo-rs 0.2.14 exec/event/StopReason#::Exitrust-analyzer cargo sudo-rs 0.2.14 exec/event/StopReason#Exit#(ParentExitrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentExit#::Backchannelrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentExit#Backchannel#(err)) => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err),
416 StopReasonrust-analyzer cargo sudo-rs 0.2.14 exec/event/StopReason#::Exitrust-analyzer cargo sudo-rs 0.2.14 exec/event/StopReason#Exit#(ParentExitrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentExit#::Commandrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentExit#Command#(exit_reason)) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(exit_reason),
417 };
418 // Send red light to the monitor after processing all events
419 retry_while_interruptedrust-analyzer cargo sudo-rs 0.2.14 exec/io_util/retry_while_interrupted().(|| self.backchannelrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#backchannel..sendrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/impl#[ParentBackchannel]send().(&MonitorMessagerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/MonitorMessage#::Edgerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/MonitorMessage#Edge#)).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(
420 |err| {
421 dev_errorrust-analyzer cargo sudo-rs 0.2.14 log/dev_error!!("cannot send red light to monitor: {err}");
422 err
423 },
424 )?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
425
426 result
427 }
428
429 /// Read an event from the backchannel and return the event if it should break the event loop.
430 fn on_message_receivedrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]on_message_received().(&mut self, registry: &mut EventRegistryrust-analyzer cargo sudo-rs 0.2.14 exec/event/EventRegistry#<Selfrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]>) {
431 match self.backchannelrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#backchannel..recvrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/impl#[ParentBackchannel]recv().() {
432 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => {
433 match err.kindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]kind().() {
434 // If we get EOF the monitor exited or was killed
435 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# => {
436 dev_inforust-analyzer cargo sudo-rs 0.2.14 log/dev_info!!("received EOF from backchannel");
437 registry.set_exitrust-analyzer cargo sudo-rs 0.2.14 exec/event/impl#[`EventRegistry<T>`]set_exit().(err.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().());
438 }
439 // We can try later if receive is interrupted.
440 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#::Interruptedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#Interrupted# => {}
441 // Failed to read command status. This means that something is wrong with the socket
442 // and we should stop.
443 _ => {
444 dev_errorrust-analyzer cargo sudo-rs 0.2.14 log/dev_error!!("cannot receive message from backchannel: {err}");
445 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().registry.got_breakrust-analyzer cargo sudo-rs 0.2.14 exec/event/impl#[`EventRegistry<T>`]got_break().() {
446 registry.set_breakrust-analyzer cargo sudo-rs 0.2.14 exec/event/impl#[`EventRegistry<T>`]set_break().(err);
447 }
448 }
449 }
450 }
451 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(event) => {
452 match event {
453 // Received the PID of the command. This means that the command is already
454 // executing.
455 ParentMessagerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/ParentMessage#::CommandPidrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/ParentMessage#CommandPid#(pid) => {
456 dev_inforust-analyzer cargo sudo-rs 0.2.14 log/dev_info!!("received command PID ({pid}) from monitor");
457 self.command_pidrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#command_pid. = pid.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().();
458 }
459 ParentMessagerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/ParentMessage#::CommandStatusrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/ParentMessage#CommandStatus#(status) => {
460 // The command terminated or the monitor was not able to spawn it. We should stop
461 // either way.
462 match status {
463 CommandStatusrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/CommandStatus#::Exitrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/CommandStatus#Exit#(exit_code) => {
464 dev_inforust-analyzer cargo sudo-rs 0.2.14 log/dev_info!!("command exited with status code {exit_code}");
465 registry.set_exitrust-analyzer cargo sudo-rs 0.2.14 exec/event/impl#[`EventRegistry<T>`]set_exit().(ExitReasonrust-analyzer cargo sudo-rs 0.2.14 exec/ExitReason#::Coderust-analyzer cargo sudo-rs 0.2.14 exec/ExitReason#Code#(exit_code).intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().());
466 }
467 CommandStatusrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/CommandStatus#::Termrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/CommandStatus#Term#(signal) => {
468 dev_inforust-analyzer cargo sudo-rs 0.2.14 log/dev_info!!("command was terminated by {}", signal_fmtrust-analyzer cargo sudo-rs 0.2.14 exec/signal_fmt().(signal));
469 registry.set_exitrust-analyzer cargo sudo-rs 0.2.14 exec/event/impl#[`EventRegistry<T>`]set_exit().(ExitReasonrust-analyzer cargo sudo-rs 0.2.14 exec/ExitReason#::Signalrust-analyzer cargo sudo-rs 0.2.14 exec/ExitReason#Signal#(signal).intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().());
470 }
471 CommandStatusrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/CommandStatus#::Stoprust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/CommandStatus#Stop#(signal) => {
472 dev_inforust-analyzer cargo sudo-rs 0.2.14 log/dev_info!!(
473 "command was stopped by {}, suspending parent",
474 signal_fmtrust-analyzer cargo sudo-rs 0.2.14 exec/signal_fmt().(signal)
475 );
476 // Suspend parent and tell monitor how to resume on return
477 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(signal) = self.suspend_ptyrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]suspend_pty().(signal, registry) {
478 self.schedule_signalrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]schedule_signal().(signal, registry);
479 }
480
481 self.tty_piperust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#tty_pipe..resume_eventsrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/impl#[`Pipe<L, R>`]resume_events().(registry);
482 }
483 }
484 }
485 ParentMessagerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/ParentMessage#::IoErrorrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/ParentMessage#IoError#(code) => {
486 let 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().(code);
487 dev_inforust-analyzer cargo sudo-rs 0.2.14 log/dev_info!!("received error ({code}) for monitor: {err}");
488 registry.set_breakrust-analyzer cargo sudo-rs 0.2.14 exec/event/impl#[`EventRegistry<T>`]set_break().(err);
489 }
490 ParentMessagerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/ParentMessage#::ShortReadrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/ParentMessage#ShortRead# => {
491 dev_inforust-analyzer cargo sudo-rs 0.2.14 log/dev_info!!("received short read error for monitor");
492 registry.set_breakrust-analyzer cargo sudo-rs 0.2.14 exec/event/impl#[`EventRegistry<T>`]set_break().(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#.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().());
493 }
494 }
495 }
496 }
497 }
498
499 /// Decides if the signal sent by the process with `signaler_pid` PID is self-terminating.
500 ///
501 /// A signal is self-terminating if `signaler_pid`:
502 /// - is the same PID of the command, or
503 /// - is in the process group of the command and either sudo or the command is the leader.
504 fn is_self_terminatingrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]is_self_terminating().(&self, signaler_pid: ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#) -> bool {
505 if signaler_pid.is_validrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[ProcessId]is_valid().() {
506 if Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(signaler_pid) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][`PartialEq<Self>`]eq().self.command_pidrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#command_pid. {
507 return true;
508 }
509
510 if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(signaler_pgrp) = getpgidrust-analyzer cargo sudo-rs 0.2.14 system/getpgid().(signaler_pid) {
511 if Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(signaler_pgrp) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][`PartialEq<Self>`]eq().self.command_pidrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#command_pid. || signaler_pgrp rust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[ProcessId][`PartialEq<Self>`]eq().==rust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[ProcessId][`PartialEq<Self>`]eq(). rust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[ProcessId][`PartialEq<Self>`]eq().self.sudo_pidrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#sudo_pid. {
512 return true;
513 }
514 }
515 }
516
517 false
518 }
519
520 /// Schedule sending a signal event to the monitor using the backchannel.
521 ///
522 /// The signal message will be sent once the backchannel is ready to be written.
523 fn schedule_signalrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]schedule_signal().(&mut self, signal: c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#, registry: &mut EventRegistryrust-analyzer cargo sudo-rs 0.2.14 exec/event/EventRegistry#<Selfrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]>) {
524 dev_inforust-analyzer cargo sudo-rs 0.2.14 log/dev_info!!("scheduling message with {} for monitor", signal_fmtrust-analyzer cargo sudo-rs 0.2.14 exec/signal_fmt().(signal));
525 self.message_queuerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#message_queue..push_backrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/impl#[`VecDeque<T, A>`]push_back().(MonitorMessagerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/MonitorMessage#::Signalrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/MonitorMessage#Signal#(signal));
526
527 // Start polling the backchannel for writing if not already.
528 self.backchannel_write_handlerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#backchannel_write_handle..resumerust-analyzer cargo sudo-rs 0.2.14 exec/event/impl#[EventHandle]resume().(registry);
529 }
530
531 /// Send the first message in the event queue using the backchannel, if any.
532 ///
533 /// Calling this function will block until the backchannel can be written.
534 fn check_message_queuerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]check_message_queue().(&mut self, registry: &mut EventRegistryrust-analyzer cargo sudo-rs 0.2.14 exec/event/EventRegistry#<Selfrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]>) {
535 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(msg) = self.message_queuerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#message_queue..frontrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/impl#[`VecDeque<T, A>`]front().() {
536 dev_inforust-analyzer cargo sudo-rs 0.2.14 log/dev_info!!("sending message {msg:?} to monitor over backchannel");
537 match self.backchannelrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#backchannel..sendrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/backchannel/impl#[ParentBackchannel]send().(msg) {
538 // The event was sent, remove it from the queue
539 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) => {
540 self.message_queuerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#message_queue..pop_frontrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/impl#[`VecDeque<T, A>`]pop_front().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().();
541 // Stop polling the backchannel for writing if the queue is empty.
542 if self.message_queuerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#message_queue..is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/impl#[`VecDeque<T, A>`]is_empty().() {
543 self.backchannel_write_handlerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#backchannel_write_handle..ignorerust-analyzer cargo sudo-rs 0.2.14 exec/event/impl#[EventHandle]ignore().(registry);
544 }
545 }
546 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => {
547 // We can try later if send is interrupted.
548 if err.kindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]kind().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().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#::Interruptedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#Interrupted# {
549 // There's something wrong with the backchannel, break the event loop.
550 dev_errorrust-analyzer cargo sudo-rs 0.2.14 log/dev_error!!("cannot send via backchannel {err}");
551 registry.set_breakrust-analyzer cargo sudo-rs 0.2.14 exec/event/impl#[`EventRegistry<T>`]set_break().(err);
552 }
553 }
554 }
555 }
556 }
557
558 /// Suspend sudo if the command is suspended.
559 ///
560 /// Return `SIGCONT_FG` or `SIGCONT_BG` to state whether the command should be resumed in the
561 /// foreground or not.
562 fn suspend_ptyrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]suspend_pty().(
563 &mut self,
564 signal: SignalNumberrust-analyzer cargo sudo-rs 0.2.14 system/signal/SignalNumber#,
565 registry: &mut EventRegistryrust-analyzer cargo sudo-rs 0.2.14 exec/event/EventRegistry#<Selfrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]>,
566 ) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<SignalNumberrust-analyzer cargo sudo-rs 0.2.14 system/signal/SignalNumber#> {
567 // Ignore `SIGCONT` while suspending to avoid resuming the terminal twice.
568 let sigcont_handler = SignalHandlerrust-analyzer cargo sudo-rs 0.2.14 system/signal/handler/SignalHandler#::registerrust-analyzer cargo sudo-rs 0.2.14 system/signal/handler/impl#[SignalHandler]register().(
569 SIGCONTrust-analyzer cargo libc 0.2.186 unix/linux_like/linux/gnu/b64/x86_64/SIGCONT.,
570 SignalHandlerBehaviorrust-analyzer cargo sudo-rs 0.2.14 system/signal/handler/SignalHandlerBehavior#::Ignorerust-analyzer cargo sudo-rs 0.2.14 system/signal/handler/SignalHandlerBehavior#Ignore#,
571 &mut self.original_signalsrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#original_signals.,
572 )
573 .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|err| dev_warnrust-analyzer cargo sudo-rs 0.2.14 log/dev_warn!!("cannot set handler for SIGCONT: {err}"))
574 .okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().();
575
576 if let SIGTTOUrust-analyzer cargo libc 0.2.186 unix/linux_like/linux/gnu/b64/x86_64/SIGTTOU. | SIGTTINrust-analyzer cargo libc 0.2.186 unix/linux_like/linux/gnu/b64/x86_64/SIGTTIN. = signal {
577 // If sudo is already the foreground process we can resume the command in the
578 // foreground. Otherwise, we have to suspend and resume later.
579 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().self.foregroundrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#foreground. && self.check_foregroundrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]check_foreground().().is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().() {
580 // User's tty was revoked.
581 return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#;
582 }
583
584 if self.foregroundrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#foreground. {
585 dev_inforust-analyzer cargo sudo-rs 0.2.14 log/dev_info!!(
586 "command received {}, parent running in the foreground",
587 signal_fmtrust-analyzer cargo sudo-rs 0.2.14 exec/signal_fmt().(signal)
588 );
589 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().self.term_rawrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#term_raw. {
590 // Format the same as og-sudo to reduce risk of divergence
591 #[allow(clippy::collapsible_if)]
592 if self
593 .tty_piperust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#tty_pipe.
594 .left_mutrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/impl#[`Pipe<L, R>`]left_mut().()
595 .set_raw_moderust-analyzer cargo sudo-rs 0.2.14 system/term/user_term/impl#[UserTerm]set_raw_mode().(false, self.preserve_oflagrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#preserve_oflag.)
596 .is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().()
597 {
598 self.term_rawrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#term_raw. = true;
599 }
600 }
601 // Resume command in the foreground
602 self.tty_piperust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#tty_pipe..enable_inputrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/impl#[`Pipe<L, R>`]enable_input().(registry);
603 return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(SIGCONT_FGrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/SIGCONT_FG.);
604 }
605 }
606
607 // Stop polling the terminals.
608 self.tty_piperust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#tty_pipe..ignore_eventsrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/impl#[`Pipe<L, R>`]ignore_events().(registry);
609
610 if self.term_rawrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#term_raw. {
611 match self.tty_piperust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#tty_pipe..left_mutrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/impl#[`Pipe<L, R>`]left_mut().().restorerust-analyzer cargo sudo-rs 0.2.14 system/term/user_term/impl#[UserTerm]restore().(false) {
612 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) => self.term_rawrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#term_raw. = false,
613 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => dev_warnrust-analyzer cargo sudo-rs 0.2.14 log/dev_warn!!("cannot restore terminal settings: {err}"),
614 }
615 }
616
617 let signal_handler = if signal 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().SIGSTOPrust-analyzer cargo libc 0.2.186 unix/linux_like/linux/gnu/b64/x86_64/SIGSTOP. {
618 SignalHandlerrust-analyzer cargo sudo-rs 0.2.14 system/signal/handler/SignalHandler#::registerrust-analyzer cargo sudo-rs 0.2.14 system/signal/handler/impl#[SignalHandler]register().(
619 signal,
620 SignalHandlerBehaviorrust-analyzer cargo sudo-rs 0.2.14 system/signal/handler/SignalHandlerBehavior#::Defaultrust-analyzer cargo sudo-rs 0.2.14 system/signal/handler/SignalHandlerBehavior#Default#,
621 &mut self.original_signalsrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#original_signals.,
622 )
623 .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|err| dev_warnrust-analyzer cargo sudo-rs 0.2.14 log/dev_warn!!("cannot set handler for {}: {err}", signal_fmtrust-analyzer cargo sudo-rs 0.2.14 exec/signal_fmt().(signal)))
624 .okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()
625 } else {
626 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#
627 };
628
629 if self.parent_pgrprust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#parent_pgrp. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().self.sudo_pidrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#sudo_pid. && killrust-analyzer cargo sudo-rs 0.2.14 system/kill().(self.parent_pgrprust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#parent_pgrp., 0).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().()
630 || killpgrust-analyzer cargo sudo-rs 0.2.14 system/killpg().(self.parent_pgrprust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#parent_pgrp., signal).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().()
631 {
632 dev_errorrust-analyzer cargo sudo-rs 0.2.14 log/dev_error!!("no parent to suspend, terminating command");
633 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(command_pid) = self.command_pidrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#command_pid..takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]take().() {
634 terminate_processrust-analyzer cargo sudo-rs 0.2.14 exec/terminate_process().(command_pid, true);
635 }
636 }
637
638 droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(signal_handler);
639
640 if self.command_pidrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#command_pid..is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().() || self.resume_terminalrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]resume_terminal().().is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().() {
641 return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#;
642 }
643
644 let ret_signal = if self.term_rawrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#term_raw. {
645 SIGCONT_FGrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/SIGCONT_FG.
646 } else {
647 SIGCONT_BGrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/SIGCONT_BG.
648 };
649
650 // Restore the handler for SIGCONT.
651 droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(sigcont_handler);
652
653 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ret_signal)
654 }
655
656 /// Check whether we are part of the foreground process group and update the foreground flag.
657 fn check_foregroundrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]check_foreground().(&mut self) -> 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#<()> {
658 let pgrp = self.tty_piperust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#tty_pipe..leftrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/impl#[`Pipe<L, R>`]left().().tcgetpgrprust-analyzer cargo sudo-rs 0.2.14 system/term/impl#[F][Terminal]tcgetpgrp().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
659 self.foregroundrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#foreground. = pgrp rust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[ProcessId][`PartialEq<Self>`]eq().==rust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[ProcessId][`PartialEq<Self>`]eq(). rust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[ProcessId][`PartialEq<Self>`]eq().self.parent_pgrprust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#parent_pgrp.;
660 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
661 }
662
663 /// Restore the terminal when sudo resumes after receiving `SIGCONT`.
664 fn resume_terminalrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]resume_terminal().(&mut self) -> 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#<()> {
665 self.check_foregroundrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]check_foreground().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
666
667 // Update the pty settings based on the user's tty.
668 self.tty_piperust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#tty_pipe.
669 .leftrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/impl#[`Pipe<L, R>`]left().()
670 .copy_torust-analyzer cargo sudo-rs 0.2.14 system/term/user_term/impl#[UserTerm]copy_to().(self.tty_piperust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#tty_pipe..rightrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/impl#[`Pipe<L, R>`]right().())
671 .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|err| {
672 dev_errorrust-analyzer cargo sudo-rs 0.2.14 log/dev_error!!("cannot copy terminal settings to pty: {err}");
673 err
674 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
675 // FIXME: sync the terminal size here.
676 dev_inforust-analyzer cargo sudo-rs 0.2.14 log/dev_info!!(
677 "parent is in {} ({} -> {})",
678 cond_fmtrust-analyzer cargo sudo-rs 0.2.14 exec/cond_fmt().(self.foregroundrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#foreground., "foreground", "background"),
679 cond_fmtrust-analyzer cargo sudo-rs 0.2.14 exec/cond_fmt().(self.term_rawrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#term_raw., "raw", "cooked"),
680 cond_fmtrust-analyzer cargo sudo-rs 0.2.14 exec/cond_fmt().(self.foregroundrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#foreground., "raw", "cooked"),
681 );
682
683 if self.foregroundrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#foreground. {
684 // We're in the foreground, set tty to raw mode.
685 if self
686 .tty_piperust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#tty_pipe.
687 .left_mutrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/impl#[`Pipe<L, R>`]left_mut().()
688 .set_raw_moderust-analyzer cargo sudo-rs 0.2.14 system/term/user_term/impl#[UserTerm]set_raw_mode().(false, self.preserve_oflagrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#preserve_oflag.)
689 .is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().()
690 {
691 self.term_rawrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#term_raw. = true;
692 }
693 } else {
694 // We're in the background, cannot access tty.
695 self.term_rawrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#term_raw. = false;
696 }
697
698 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
699 }
700
701 fn on_signalrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]on_signal().(&mut self, registry: &mut EventRegistryrust-analyzer cargo sudo-rs 0.2.14 exec/event/EventRegistry#<Selfrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]>) {
702 let info = match self.signal_streamrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#signal_stream..recvrust-analyzer cargo sudo-rs 0.2.14 system/signal/stream/impl#[SignalStream]recv().() {
703 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(info) => info,
704 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => {
705 dev_errorrust-analyzer cargo sudo-rs 0.2.14 log/dev_error!!("parent could not receive signal: {err}");
706 return;
707 }
708 };
709
710 dev_inforust-analyzer cargo sudo-rs 0.2.14 log/dev_info!!("parent received{}", info);
711
712 let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(monitor_pid) = self.monitor_pidrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#monitor_pid. else {
713 dev_inforust-analyzer cargo sudo-rs 0.2.14 log/dev_info!!("monitor was terminated, ignoring signal");
714 return;
715 };
716
717 match info.signalrust-analyzer cargo sudo-rs 0.2.14 system/signal/info/impl#[SignalInfo]signal().() {
718 SIGCHLDrust-analyzer cargo libc 0.2.186 unix/linux_like/linux/gnu/b64/x86_64/SIGCHLD. => handle_sigchldrust-analyzer cargo sudo-rs 0.2.14 exec/handle_sigchld().(self, registry, "monitor", monitor_pid),
719 SIGCONTrust-analyzer cargo libc 0.2.186 unix/linux_like/linux/gnu/b64/x86_64/SIGCONT. => {
720 self.resume_terminalrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]resume_terminal().().okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().();
721 }
722 SIGWINCHrust-analyzer cargo libc 0.2.186 unix/linux_like/linux/gnu/b64/x86_64/SIGWINCH. => {
723 if let Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) = self.handle_sigwinchrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]handle_sigwinch().() {
724 dev_warnrust-analyzer cargo sudo-rs 0.2.14 log/dev_warn!!("cannot resize terminal: {}", err);
725 }
726 }
727 signal => {
728 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(pid) = info.signaler_pidrust-analyzer cargo sudo-rs 0.2.14 system/signal/info/impl#[SignalInfo]signaler_pid().() {
729 if self.is_self_terminatingrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]is_self_terminating().(pid) {
730 // Skip the signal if it was sent by the user and it is self-terminating.
731 return;
732 }
733 }
734
735 // FIXME: check `send_command_status`
736 self.schedule_signalrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]schedule_signal().(signal, registry)
737 }
738 }
739 }
740
741 fn handle_sigwinchrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]handle_sigwinch().(&mut self) -> 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#<()> {
742 let new_size = self.tty_piperust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#tty_pipe..leftrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/impl#[`Pipe<L, R>`]left().().get_sizerust-analyzer cargo sudo-rs 0.2.14 system/term/user_term/impl#[UserTerm]get_size().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
743
744 if new_size rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().self.tty_sizerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#tty_size. {
745 dev_inforust-analyzer cargo sudo-rs 0.2.14 log/dev_info!!("updating pty size from {} to {new_size}", self.tty_sizerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#tty_size.);
746 // Set the pty size.
747 self.tty_piperust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#tty_pipe..rightrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/impl#[`Pipe<L, R>`]right().().set_sizerust-analyzer cargo sudo-rs 0.2.14 system/term/impl#[PtyLeader]set_size().(&new_size)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
748 // Send SIGWINCH to the command.
749 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(command_pid) = self.command_pidrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#command_pid. {
750 killpgrust-analyzer cargo sudo-rs 0.2.14 system/killpg().(command_pid, SIGWINCHrust-analyzer cargo libc 0.2.186 unix/linux_like/linux/gnu/b64/x86_64/SIGWINCH.).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().();
751 }
752 // Update the terminal size.
753 self.tty_sizerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#tty_size. = new_size;
754 }
755
756 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
757 }
758 }
759
760 enum ParentExitrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentExit# {
761 /// Error while reading from the backchannel.
762 Backchannelrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentExit#Backchannel#(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#),
763 /// The command exited.
764 Commandrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentExit#Command#(ExitReasonrust-analyzer cargo sudo-rs 0.2.14 exec/ExitReason#),
765 }
766
767 impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<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#> for ParentExitrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentExit# {
768 fn fromrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentExit][`From<io::Error>`]from().(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#) -> Selfrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentExit][`From<io::Error>`] {
769 Selfrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentExit][`From<io::Error>`]::Backchannelrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentExit#Backchannel#(err)
770 }
771 }
772
773 impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<ExitReasonrust-analyzer cargo sudo-rs 0.2.14 exec/ExitReason#> for ParentExitrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentExit# {
774 fn fromrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentExit][`From<ExitReason>`]from().(reason: ExitReasonrust-analyzer cargo sudo-rs 0.2.14 exec/ExitReason#) -> Selfrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentExit][`From<ExitReason>`] {
775 Selfrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentExit][`From<ExitReason>`]::Commandrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentExit#Command#(reason)
776 }
777 }
778
779 #[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:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:)]
780 enum ParentEventrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentEvent# {
781 Signalrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentEvent#Signal#,
782 Ttyrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentEvent#Tty#(PollEventrust-analyzer cargo sudo-rs 0.2.14 exec/event/PollEvent#),
783 Ptyrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentEvent#Pty#(PollEventrust-analyzer cargo sudo-rs 0.2.14 exec/event/PollEvent#),
784 Backchannelrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentEvent#Backchannel#(PollEventrust-analyzer cargo sudo-rs 0.2.14 exec/event/PollEvent#),
785 }
786
787 impl Processrust-analyzer cargo sudo-rs 0.2.14 exec/event/Process# for ParentClosurerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure# {
788 type Eventrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure][Process]Event# = ParentEventrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentEvent#;
789 type Breakrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure][Process]Break# = 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#;
790 type Exitrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure][Process]Exit# = ParentExitrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentExit#;
791
792 fn on_eventrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure][Process]on_event().(&mut self, event: Selfrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure][Process]::Eventrust-analyzer cargo sudo-rs 0.2.14 exec/event/Process#Event#, registry: &mut EventRegistryrust-analyzer cargo sudo-rs 0.2.14 exec/event/EventRegistry#<Selfrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure][Process]>) {
793 match event {
794 ParentEventrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentEvent#::Signalrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentEvent#Signal# => self.on_signalrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]on_signal().(registry),
795 ParentEventrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentEvent#::Ttyrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentEvent#Tty#(poll_event) => {
796 // Check if tty which existed is now gone.
797 if self.tty_piperust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#tty_pipe..leftrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/impl#[`Pipe<L, R>`]left().().tcgetsidrust-analyzer cargo sudo-rs 0.2.14 system/term/impl#[F][Terminal]tcgetsid().().is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().() {
798 dev_warnrust-analyzer cargo sudo-rs 0.2.14 log/dev_warn!!("tty gone (closed/detached), ignoring future events");
799 self.tty_piperust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#tty_pipe..ignore_eventsrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/impl#[`Pipe<L, R>`]ignore_events().(registry);
800 } else {
801 self.tty_piperust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#tty_pipe..on_left_eventrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/impl#[`Pipe<L, R>`]on_left_event().(poll_event, registry).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().();
802 }
803 }
804 ParentEventrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentEvent#::Ptyrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentEvent#Pty#(poll_event) => {
805 self.tty_piperust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#tty_pipe..on_right_eventrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/impl#[`Pipe<L, R>`]on_right_event().(poll_event, registry).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().();
806 }
807 ParentEventrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentEvent#::Backchannelrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentEvent#Backchannel#(poll_event) => match poll_event {
808 PollEventrust-analyzer cargo sudo-rs 0.2.14 exec/event/PollEvent#::Readablerust-analyzer cargo sudo-rs 0.2.14 exec/event/PollEvent#Readable# => self.on_message_receivedrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]on_message_received().(registry),
809 PollEventrust-analyzer cargo sudo-rs 0.2.14 exec/event/PollEvent#::Writablerust-analyzer cargo sudo-rs 0.2.14 exec/event/PollEvent#Writable# => self.check_message_queuerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]check_message_queue().(registry),
810 },
811 }
812 }
813 }
814
815 impl HandleSigchldrust-analyzer cargo sudo-rs 0.2.14 exec/HandleSigchld# for ParentClosurerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure# {
816 const OPTIONSrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure][HandleSigchld]OPTIONS.: WaitOptionsrust-analyzer cargo sudo-rs 0.2.14 system/wait/WaitOptions# = WaitOptionsrust-analyzer cargo sudo-rs 0.2.14 system/wait/WaitOptions#::newrust-analyzer cargo sudo-rs 0.2.14 system/wait/impl#[WaitOptions]new().().allrust-analyzer cargo sudo-rs 0.2.14 system/wait/impl#[WaitOptions]all().().untracedrust-analyzer cargo sudo-rs 0.2.14 system/wait/impl#[WaitOptions]untraced().().no_hangrust-analyzer cargo sudo-rs 0.2.14 system/wait/impl#[WaitOptions]no_hang().();
817
818 fn on_exitrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure][HandleSigchld]on_exit().(&mut self, _exit_code: c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#, _registry: &mut EventRegistryrust-analyzer cargo sudo-rs 0.2.14 exec/event/EventRegistry#<Selfrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure][HandleSigchld]>) {
819 self.monitor_pidrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#monitor_pid. = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#;
820 }
821
822 fn on_termrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure][HandleSigchld]on_term().(&mut self, _signal: SignalNumberrust-analyzer cargo sudo-rs 0.2.14 system/signal/SignalNumber#, _registry: &mut EventRegistryrust-analyzer cargo sudo-rs 0.2.14 exec/event/EventRegistry#<Selfrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure][HandleSigchld]>) {
823 self.monitor_pidrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#monitor_pid. = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#;
824 }
825
826 fn on_stoprust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure][HandleSigchld]on_stop().(&mut self, signal: SignalNumberrust-analyzer cargo sudo-rs 0.2.14 system/signal/SignalNumber#, registry: &mut EventRegistryrust-analyzer cargo sudo-rs 0.2.14 exec/event/EventRegistry#<Selfrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure][HandleSigchld]>) {
827 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(signal) = self.suspend_ptyrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]suspend_pty().(signal, registry) {
828 self.schedule_signalrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/impl#[ParentClosure]schedule_signal().(signal, registry);
829 }
830 self.tty_piperust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/parent/ParentClosure#tty_pipe..resume_eventsrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/impl#[`Pipe<L, R>`]resume_events().(registry);
831 }
832 }