1 use crate::convert
::{TryFrom, TryInto}
;
5 use crate::num
::{NonZeroI32, NonZeroI64}
;
8 use crate::sys
::process
::process_common
::*;
9 use crate::sys
::process
::zircon
::{zx_handle_t, Handle}
;
11 use libc
::{c_int, size_t}
;
13 ////////////////////////////////////////////////////////////////////////////////
15 ////////////////////////////////////////////////////////////////////////////////
22 ) -> io
::Result
<(Process
, StdioPipes
)> {
23 let envp
= self.capture_env();
26 return Err(io
::Error
::new_const(
27 io
::ErrorKind
::InvalidInput
,
28 &"nul byte found in provided data",
32 let (ours
, theirs
) = self.setup_io(default, needs_stdin
)?
;
34 let process_handle
= unsafe { self.do_exec(theirs, envp.as_ref())? }
;
36 Ok((Process { handle: Handle::new(process_handle) }
, ours
))
39 pub fn exec(&mut self, default: Stdio
) -> io
::Error
{
41 return io
::Error
::new_const(
42 io
::ErrorKind
::InvalidInput
,
43 &"nul byte found in provided data",
47 match self.setup_io(default, true) {
49 // FIXME: This is tough because we don't support the exec syscalls
59 maybe_envp
: Option
<&CStringArray
>,
60 ) -> io
::Result
<zx_handle_t
> {
61 use crate::sys
::process
::zircon
::*;
63 let envp
= match maybe_envp
{
64 // None means to clone the current environment, which is done in the
67 Some(envp
) => envp
.as_ptr(),
70 let make_action
= |local_io
: &ChildStdio
, target_fd
| -> io
::Result
<fdio_spawn_action_t
> {
71 if let Some(local_fd
) = local_io
.fd() {
72 Ok(fdio_spawn_action_t
{
73 action
: FDIO_SPAWN_ACTION_TRANSFER_FD
,
79 if let ChildStdio
::Null
= local_io
{
81 return Ok(Default
::default());
84 let mut handle
= ZX_HANDLE_INVALID
;
85 let status
= fdio_fd_clone(target_fd
, &mut handle
);
86 if status
== ERR_INVALID_ARGS
|| status
== ERR_NOT_SUPPORTED
{
87 // This descriptor is closed; skip it rather than generating an
89 return Ok(Default
::default());
93 let mut cloned_fd
= 0;
94 zx_cvt(fdio_fd_create(handle
, &mut cloned_fd
))?
;
96 Ok(fdio_spawn_action_t
{
97 action
: FDIO_SPAWN_ACTION_TRANSFER_FD
,
98 local_fd
: cloned_fd
as i32,
105 // Clone stdin, stdout, and stderr
106 let action1
= make_action(&stdio
.stdin
, 0)?
;
107 let action2
= make_action(&stdio
.stdout
, 1)?
;
108 let action3
= make_action(&stdio
.stderr
, 2)?
;
109 let actions
= [action1
, action2
, action3
];
111 // We don't want FileDesc::drop to be called on any stdio. fdio_spawn_etc
112 // always consumes transferred file descriptors.
115 for callback
in self.get_closures().iter_mut() {
119 let mut process_handle
: zx_handle_t
= 0;
120 zx_cvt(fdio_spawn_etc(
123 | FDIO_SPAWN_CLONE_LDSVC
124 | FDIO_SPAWN_CLONE_NAMESPACE
125 | FDIO_SPAWN_CLONE_ENVIRON
// this is ignored when envp is non-null
126 | FDIO_SPAWN_CLONE_UTC_CLOCK
,
127 self.get_program_cstr().as_ptr(),
128 self.get_argv().as_ptr(),
130 actions
.len() as size_t
,
135 // FIXME: See if we want to do something with that err_msg
141 ////////////////////////////////////////////////////////////////////////////////
143 ////////////////////////////////////////////////////////////////////////////////
150 pub fn id(&self) -> u32 {
151 self.handle
.raw() as u32
154 pub fn kill(&mut self) -> io
::Result
<()> {
155 use crate::sys
::process
::zircon
::*;
158 zx_cvt(zx_task_kill(self.handle
.raw()))?
;
164 pub fn wait(&mut self) -> io
::Result
<ExitStatus
> {
165 use crate::default::Default
;
166 use crate::sys
::process
::zircon
::*;
168 let mut proc_info
: zx_info_process_t
= Default
::default();
169 let mut actual
: size_t
= 0;
170 let mut avail
: size_t
= 0;
173 zx_cvt(zx_object_wait_one(
179 zx_cvt(zx_object_get_info(
182 &mut proc_info
as *mut _
as *mut libc
::c_void
,
183 mem
::size_of
::<zx_info_process_t
>(),
189 return Err(io
::Error
::new_const(
190 io
::ErrorKind
::InvalidData
,
191 &"Failed to get exit status of process",
194 Ok(ExitStatus(proc_info
.return_code
))
197 pub fn try_wait(&mut self) -> io
::Result
<Option
<ExitStatus
>> {
198 use crate::default::Default
;
199 use crate::sys
::process
::zircon
::*;
201 let mut proc_info
: zx_info_process_t
= Default
::default();
202 let mut actual
: size_t
= 0;
203 let mut avail
: size_t
= 0;
207 zx_object_wait_one(self.handle
.raw(), ZX_TASK_TERMINATED
, 0, ptr
::null_mut());
210 x
if x
== ERR_TIMED_OUT
=> {
214 panic
!("Failed to wait on process handle: {}", status
);
217 zx_cvt(zx_object_get_info(
220 &mut proc_info
as *mut _
as *mut libc
::c_void
,
221 mem
::size_of
::<zx_info_process_t
>(),
227 return Err(io
::Error
::new_const(
228 io
::ErrorKind
::InvalidData
,
229 &"Failed to get exit status of process",
232 Ok(Some(ExitStatus(proc_info
.return_code
)))
236 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
237 pub struct ExitStatus(i64);
240 pub fn exit_ok(&self) -> Result
<(), ExitStatusError
> {
241 match NonZeroI64
::try_from(self.0) {
242 /* was nonzero */ Ok(failure
) => Err(ExitStatusError(failure
)),
243 /* was zero, couldn't convert */ Err(_
) => Ok(()),
247 pub fn code(&self) -> Option
<i32> {
248 // FIXME: support extracting return code as an i64
249 self.0.try_into().ok()
252 pub fn signal(&self) -> Option
<i32> {
256 // FIXME: The actually-Unix implementation in process_unix.rs uses WSTOPSIG, WCOREDUMP et al.
257 // I infer from the implementation of `success`, `code` and `signal` above that these are not
258 // available on Fuchsia.
260 // It does not appear that Fuchsia is Unix-like enough to implement ExitStatus (or indeed many
261 // other things from std::os::unix) properly. This veneer is always going to be a bodge. So
262 // while I don't know if these implementations are actually correct, I think they will do for
264 pub fn core_dumped(&self) -> bool
{
267 pub fn stopped_signal(&self) -> Option
<i32> {
270 pub fn continued(&self) -> bool
{
274 pub fn into_raw(&self) -> c_int
{
275 // We don't know what someone who calls into_raw() will do with this value, but it should
276 // have the conventional Unix representation. Despite the fact that this is not
277 // standardised in SuS or POSIX, all Unix systems encode the signal and exit status the
278 // same way. (Ie the WIFEXITED, WEXITSTATUS etc. macros have identical behaviour on every
281 // The caller of `std::os::unix::into_raw` is probably wanting a Unix exit status, and may
282 // do their own shifting and masking, or even pass the status to another computer running a
283 // different Unix variant.
285 // The other view would be to say that the caller on Fuchsia ought to know that `into_raw`
286 // will give a raw Fuchsia status (whatever that is - I don't know, personally). That is
287 // not possible here becaause we must return a c_int because that's what Unix (including
288 // SuS and POSIX) say a wait status is, but Fuchsia apparently uses a u64, so it won't
291 // It seems to me that that the right answer would be to provide std::os::fuchsia with its
292 // own ExitStatusExt, rather that trying to provide a not very convincing imitation of
293 // Unix. Ie, std::os::unix::process:ExitStatusExt ought not to exist on Fuchsia. But
294 // fixing this up that is beyond the scope of my efforts now.
295 let exit_status_as_if_unix
: u8 = self.0.try_into().expect("Fuchsia process return code bigger than 8 bits, but std::os::unix::ExitStatusExt::into_raw() was called to try to convert the value into a traditional Unix-style wait status, which cannot represent values greater than 255.");
296 let wait_status_as_if_unix
= (exit_status_as_if_unix
as c_int
) << 8;
297 wait_status_as_if_unix
301 /// Converts a raw `c_int` to a type-safe `ExitStatus` by wrapping it without copying.
302 impl From
<c_int
> for ExitStatus
{
303 fn from(a
: c_int
) -> ExitStatus
{
308 impl fmt
::Display
for ExitStatus
{
309 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
310 write
!(f
, "exit code: {}", self.0)
314 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
315 pub struct ExitStatusError(NonZeroI64
);
317 impl Into
<ExitStatus
> for ExitStatusError
{
318 fn into(self) -> ExitStatus
{
319 ExitStatus(self.0.into
())
323 impl ExitStatusError
{
324 pub fn code(self) -> Option
<NonZeroI32
> {
325 // fixme: affected by the same bug as ExitStatus::code()
326 ExitStatus(self.0.into
()).code().map(|st
| st
.try_into().unwrap())