]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #38151 - GuillaumeGomez:exit-examples, r=frewsxcv
authorGuillaume Gomez <guillaume1.gomez@gmail.com>
Wed, 7 Dec 2016 18:42:51 +0000 (10:42 -0800)
committerGitHub <noreply@github.com>
Wed, 7 Dec 2016 18:42:51 +0000 (10:42 -0800)
Add examples for exit function

r? @frewsxcv

1  2 
src/libstd/process.rs

diff --combined src/libstd/process.rs
index bfc36d5b21fe86d650683ed6c8e2c98b26783dfe,70453a74d2c9a7432537df7625bf3e7bf6fe523d..858537dd2de122cd75a9e42010be6119d40e4602
@@@ -827,6 -827,14 +827,14 @@@ impl Child 
  /// will be run. If a clean shutdown is needed it is recommended to only call
  /// this function at a known point where there are no more destructors left
  /// to run.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::process;
+ ///
+ /// process::exit(0);
+ /// ```
  #[stable(feature = "rust1", since = "1.0.0")]
  pub fn exit(code: i32) -> ! {
      ::sys_common::cleanup();
@@@ -1167,62 -1175,4 +1175,62 @@@ mod tests 
              Ok(_) => panic!(),
          }
      }
 +
 +    /// Test that process creation flags work by debugging a process.
 +    /// Other creation flags make it hard or impossible to detect
 +    /// behavioral changes in the process.
 +    #[test]
 +    #[cfg(windows)]
 +    fn test_creation_flags() {
 +        use os::windows::process::CommandExt;
 +        use sys::c::{BOOL, DWORD, INFINITE};
 +        #[repr(C, packed)]
 +        struct DEBUG_EVENT {
 +            pub event_code: DWORD,
 +            pub process_id: DWORD,
 +            pub thread_id: DWORD,
 +            // This is a union in the real struct, but we don't
 +            // need this data for the purposes of this test.
 +            pub _junk: [u8; 164],
 +        }
 +
 +        extern "system" {
 +            fn WaitForDebugEvent(lpDebugEvent: *mut DEBUG_EVENT, dwMilliseconds: DWORD) -> BOOL;
 +            fn ContinueDebugEvent(dwProcessId: DWORD, dwThreadId: DWORD,
 +                                  dwContinueStatus: DWORD) -> BOOL;
 +        }
 +
 +        const DEBUG_PROCESS: DWORD = 1;
 +        const EXIT_PROCESS_DEBUG_EVENT: DWORD = 5;
 +        const DBG_EXCEPTION_NOT_HANDLED: DWORD = 0x80010001;
 +
 +        let mut child = Command::new("cmd")
 +            .creation_flags(DEBUG_PROCESS)
 +            .stdin(Stdio::piped()).spawn().unwrap();
 +        child.stdin.take().unwrap().write_all(b"exit\r\n").unwrap();
 +        let mut events = 0;
 +        let mut event = DEBUG_EVENT {
 +            event_code: 0,
 +            process_id: 0,
 +            thread_id: 0,
 +            _junk: [0; 164],
 +        };
 +        loop {
 +            if unsafe { WaitForDebugEvent(&mut event as *mut DEBUG_EVENT, INFINITE) } == 0 {
 +                panic!("WaitForDebugEvent failed!");
 +            }
 +            events += 1;
 +
 +            if event.event_code == EXIT_PROCESS_DEBUG_EVENT {
 +                break;
 +            }
 +
 +            if unsafe { ContinueDebugEvent(event.process_id,
 +                                           event.thread_id,
 +                                           DBG_EXCEPTION_NOT_HANDLED) } == 0 {
 +                panic!("ContinueDebugEvent failed!");
 +            }
 +        }
 +        assert!(events > 0);
 +    }
  }