11//! Start a process via pty
22
3- use crate :: errors :: * ;
3+ use crate :: error :: Error ;
44use nix;
55use nix:: fcntl:: { open, OFlag } ;
66use nix:: libc:: { STDERR_FILENO , STDIN_FILENO , STDOUT_FILENO } ;
@@ -13,7 +13,7 @@ use std::fs::File;
1313use std:: os:: unix:: io:: { AsRawFd , FromRawFd } ;
1414use std:: os:: unix:: process:: CommandExt ;
1515use std:: process:: Command ;
16- use std:: { thread, time} ; // load error-chain
16+ use std:: { thread, time} ;
1717
1818/// Start a process in a forked tty so you can interact with it the same as you would
1919/// within a terminal
@@ -87,7 +87,7 @@ fn ptsname_r(fd: &PtyMaster) -> nix::Result<String> {
8787
8888impl PtyProcess {
8989 /// Start a process in a forked pty
90- pub fn new ( mut command : Command ) -> Result < Self > {
90+ pub fn new ( mut command : Command ) -> Result < Self , Error > {
9191 || -> nix:: Result < Self > {
9292 // Open a new PTY master
9393 let master_fd = posix_openpt ( OFlag :: O_RDWR ) ?;
@@ -128,7 +128,7 @@ impl PtyProcess {
128128 } ) ,
129129 }
130130 } ( )
131- . chain_err ( || format ! ( "could not execute {:?}" , command ) )
131+ . map_err ( Error :: from )
132132 }
133133
134134 /// Get handle to pty fork for reading/writing
@@ -177,19 +177,18 @@ impl PtyProcess {
177177
178178 /// Wait until process has exited. This is a blocking call.
179179 /// If the process doesn't terminate this will block forever.
180- pub fn wait ( & self ) -> Result < wait:: WaitStatus > {
181- wait:: waitpid ( self . child_pid , None ) . chain_err ( || "wait: cannot read status" )
180+ pub fn wait ( & self ) -> Result < wait:: WaitStatus , Error > {
181+ wait:: waitpid ( self . child_pid , None ) . map_err ( Error :: from )
182182 }
183183
184184 /// Regularly exit the process, this method is blocking until the process is dead
185- pub fn exit ( & mut self ) -> Result < wait:: WaitStatus > {
186- self . kill ( signal:: SIGTERM )
185+ pub fn exit ( & mut self ) -> Result < wait:: WaitStatus , Error > {
186+ self . kill ( signal:: SIGTERM ) . map_err ( Error :: from )
187187 }
188188
189189 /// Non-blocking variant of `kill()` (doesn't wait for process to be killed)
190- pub fn signal ( & mut self , sig : signal:: Signal ) -> Result < ( ) > {
191- signal:: kill ( self . child_pid , sig) . chain_err ( || "failed to send signal to process" ) ?;
192- Ok ( ( ) )
190+ pub fn signal ( & mut self , sig : signal:: Signal ) -> Result < ( ) , Error > {
191+ signal:: kill ( self . child_pid , sig) . map_err ( Error :: from)
193192 }
194193
195194 /// Kill the process with a specific signal. This method blocks, until the process is dead
@@ -200,7 +199,7 @@ impl PtyProcess {
200199 ///
201200 /// if `kill_timeout` is set and a repeated sending of signal does not result in the process
202201 /// being killed, then `kill -9` is sent after the `kill_timeout` duration has elapsed.
203- pub fn kill ( & mut self , sig : signal:: Signal ) -> Result < wait:: WaitStatus > {
202+ pub fn kill ( & mut self , sig : signal:: Signal ) -> Result < wait:: WaitStatus , Error > {
204203 let start = time:: Instant :: now ( ) ;
205204 loop {
206205 match signal:: kill ( self . child_pid , sig) {
@@ -209,7 +208,7 @@ impl PtyProcess {
209208 Err ( nix:: Error :: Sys ( nix:: errno:: Errno :: ESRCH ) ) => {
210209 return Ok ( wait:: WaitStatus :: Exited ( Pid :: from_raw ( 0 ) , 0 ) )
211210 }
212- Err ( e) => return Err ( format ! ( "kill resulted in error: {:?}" , e ) . into ( ) ) ,
211+ Err ( e) => return Err ( Error :: from ( e ) ) ,
213212 }
214213
215214 match self . status ( ) {
@@ -219,7 +218,7 @@ impl PtyProcess {
219218 // kill -9 if timout is reached
220219 if let Some ( timeout) = self . kill_timeout {
221220 if start. elapsed ( ) > timeout {
222- signal:: kill ( self . child_pid , signal:: Signal :: SIGKILL ) . chain_err ( || "" ) ?
221+ signal:: kill ( self . child_pid , signal:: Signal :: SIGKILL ) . map_err ( Error :: from ) ?
223222 }
224223 }
225224 }
0 commit comments