]> git.proxmox.com Git - rustc.git/blob - src/libstd/sys/unix/ext/process.rs
Imported Upstream version 1.3.0+dfsg1
[rustc.git] / src / libstd / sys / unix / ext / process.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Unix-specific extensions to primitives in the `std::process` module.
12
13 #![stable(feature = "rust1", since = "1.0.0")]
14
15 use os::unix::raw::{uid_t, gid_t};
16 use os::unix::io::{FromRawFd, RawFd, AsRawFd, IntoRawFd};
17 use prelude::v1::*;
18 use process;
19 use sys;
20 use sys_common::{AsInnerMut, AsInner, FromInner, IntoInner};
21
22 /// Unix-specific extensions to the `std::process::Command` builder
23 #[stable(feature = "rust1", since = "1.0.0")]
24 pub trait CommandExt {
25 /// Sets the child process's user id. This translates to a
26 /// `setuid` call in the child process. Failure in the `setuid`
27 /// call will cause the spawn to fail.
28 #[stable(feature = "rust1", since = "1.0.0")]
29 fn uid(&mut self, id: uid_t) -> &mut process::Command;
30
31 /// Similar to `uid`, but sets the group id of the child process. This has
32 /// the same semantics as the `uid` field.
33 #[stable(feature = "rust1", since = "1.0.0")]
34 fn gid(&mut self, id: gid_t) -> &mut process::Command;
35 }
36
37 #[stable(feature = "rust1", since = "1.0.0")]
38 impl CommandExt for process::Command {
39 fn uid(&mut self, id: uid_t) -> &mut process::Command {
40 self.as_inner_mut().uid = Some(id);
41 self
42 }
43
44 fn gid(&mut self, id: gid_t) -> &mut process::Command {
45 self.as_inner_mut().gid = Some(id);
46 self
47 }
48 }
49
50 /// Unix-specific extensions to `std::process::ExitStatus`
51 #[stable(feature = "rust1", since = "1.0.0")]
52 pub trait ExitStatusExt {
53 /// If the process was terminated by a signal, returns that signal.
54 #[stable(feature = "rust1", since = "1.0.0")]
55 fn signal(&self) -> Option<i32>;
56 }
57
58 #[stable(feature = "rust1", since = "1.0.0")]
59 impl ExitStatusExt for process::ExitStatus {
60 fn signal(&self) -> Option<i32> {
61 match *self.as_inner() {
62 sys::process::ExitStatus::Signal(s) => Some(s),
63 _ => None
64 }
65 }
66 }
67
68 #[stable(feature = "process_extensions", since = "1.2.0")]
69 impl FromRawFd for process::Stdio {
70 unsafe fn from_raw_fd(fd: RawFd) -> process::Stdio {
71 process::Stdio::from_inner(sys::fd::FileDesc::new(fd))
72 }
73 }
74
75 #[stable(feature = "process_extensions", since = "1.2.0")]
76 impl AsRawFd for process::ChildStdin {
77 fn as_raw_fd(&self) -> RawFd {
78 self.as_inner().fd().raw()
79 }
80 }
81
82 #[stable(feature = "process_extensions", since = "1.2.0")]
83 impl AsRawFd for process::ChildStdout {
84 fn as_raw_fd(&self) -> RawFd {
85 self.as_inner().fd().raw()
86 }
87 }
88
89 #[stable(feature = "process_extensions", since = "1.2.0")]
90 impl AsRawFd for process::ChildStderr {
91 fn as_raw_fd(&self) -> RawFd {
92 self.as_inner().fd().raw()
93 }
94 }
95
96 impl IntoRawFd for process::ChildStdin {
97 fn into_raw_fd(self) -> RawFd {
98 self.into_inner().into_fd().into_raw()
99 }
100 }
101
102 impl IntoRawFd for process::ChildStdout {
103 fn into_raw_fd(self) -> RawFd {
104 self.into_inner().into_fd().into_raw()
105 }
106 }
107
108 impl IntoRawFd for process::ChildStderr {
109 fn into_raw_fd(self) -> RawFd {
110 self.into_inner().into_fd().into_raw()
111 }
112 }