1
- use std:: {
2
- ffi:: c_int,
3
- io:: { self , Read , Write } ,
4
- os:: unix:: { net:: UnixStream , process:: CommandExt } ,
5
- process:: Command ,
6
- } ;
1
+ use std:: { ffi:: c_int, io, os:: unix:: process:: CommandExt , process:: Command } ;
7
2
8
3
use super :: {
9
4
event:: PollEvent ,
10
5
event:: { EventRegistry , Process , StopReason } ,
11
6
io_util:: was_interrupted,
12
7
terminate_process, ExitReason , HandleSigchld , ProcessOutput ,
13
8
} ;
14
- use crate :: system:: signal:: {
15
- consts:: * , register_handlers, SignalHandler , SignalHandlerBehavior , SignalNumber , SignalSet ,
16
- SignalStream ,
9
+ use crate :: {
10
+ common:: bin_serde:: { pair, BinPipe } ,
11
+ system:: signal:: {
12
+ consts:: * , register_handlers, SignalHandler , SignalHandlerBehavior , SignalNumber ,
13
+ SignalSet , SignalStream ,
14
+ } ,
17
15
} ;
18
16
use crate :: {
19
17
exec:: { handle_sigchld, opt_fmt, signal_fmt} ,
@@ -46,11 +44,11 @@ pub(super) fn exec_no_pty(sudo_pid: ProcessId, mut command: Command) -> io::Resu
46
44
// FIXME (ogsudo): Some extra config happens here if selinux is available.
47
45
48
46
// Use a pipe to get the IO error if `exec` fails.
49
- let ( mut errpipe_tx, errpipe_rx) = UnixStream :: pair ( ) ?;
47
+ let ( mut errpipe_tx, errpipe_rx) = pair ( ) ?;
50
48
51
49
// Don't close the error pipe as we need it to retrieve the error code if the command execution
52
50
// fails.
53
- file_closer. except ( & errpipe_tx) ;
51
+ file_closer. except ( & errpipe_tx. sock ) ;
54
52
55
53
let ForkResult :: Parent ( command_pid) = fork ( ) . map_err ( |err| {
56
54
dev_warn ! ( "unable to fork command process: {err}" ) ;
@@ -72,7 +70,7 @@ pub(super) fn exec_no_pty(sudo_pid: ProcessId, mut command: Command) -> io::Resu
72
70
// If `exec` returns, it means that executing the command failed. Send the error to the
73
71
// monitor using the pipe.
74
72
if let Some ( error_code) = err. raw_os_error ( ) {
75
- errpipe_tx. write_all ( & error_code. to_ne_bytes ( ) ) . ok ( ) ;
73
+ errpipe_tx. write ( & error_code) . ok ( ) ;
76
74
}
77
75
78
76
return Ok ( ProcessOutput :: ChildExit ) ;
@@ -108,7 +106,7 @@ struct ExecClosure {
108
106
command_pid : Option < ProcessId > ,
109
107
sudo_pid : ProcessId ,
110
108
parent_pgrp : ProcessId ,
111
- errpipe_rx : UnixStream ,
109
+ errpipe_rx : BinPipe < 4 , i32 > ,
112
110
signal_stream : & ' static SignalStream ,
113
111
signal_handlers : [ SignalHandler ; ExecClosure :: SIGNALS . len ( ) ] ,
114
112
}
@@ -122,10 +120,12 @@ impl ExecClosure {
122
120
fn new (
123
121
command_pid : ProcessId ,
124
122
sudo_pid : ProcessId ,
125
- errpipe_rx : UnixStream ,
123
+ errpipe_rx : BinPipe < 4 , i32 > ,
126
124
registry : & mut EventRegistry < Self > ,
127
125
) -> io:: Result < Self > {
128
- registry. register_event ( & errpipe_rx, PollEvent :: Readable , |_| ExecEvent :: ErrPipe ) ;
126
+ registry. register_event ( & errpipe_rx. sock , PollEvent :: Readable , |_| {
127
+ ExecEvent :: ErrPipe
128
+ } ) ;
129
129
130
130
let signal_stream = SignalStream :: init ( ) ?;
131
131
@@ -287,13 +287,11 @@ impl Process for ExecClosure {
287
287
match event {
288
288
ExecEvent :: Signal => self . on_signal ( registry) ,
289
289
ExecEvent :: ErrPipe => {
290
- let mut buf = 0i32 . to_ne_bytes ( ) ;
291
- match self . errpipe_rx . read_exact ( & mut buf) {
290
+ match self . errpipe_rx . read ( ) {
292
291
Err ( err) if was_interrupted ( & err) => { /* Retry later */ }
293
292
Err ( err) => registry. set_break ( err) ,
294
- Ok ( _ ) => {
293
+ Ok ( error_code ) => {
295
294
// Received error code from the command, forward it to the parent.
296
- let error_code = i32:: from_ne_bytes ( buf) ;
297
295
registry. set_break ( io:: Error :: from_raw_os_error ( error_code) ) ;
298
296
}
299
297
}
0 commit comments