Skip to content

Commit 5f2de72

Browse files
Rollup merge of #51359 - cramertj:fdio_spawn, r=sfackler
[fuchsia] Migrate from launchpad to fdio_spawn_etc fdio_spawn_etc is the preferred way of creating processes on Fuchsia now. cc @abarth
2 parents 7d0d7ea + aa23aba commit 5f2de72

File tree

3 files changed

+51
-110
lines changed

3 files changed

+51
-110
lines changed

src/libstd/build.rs

-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ fn main() {
7474
}
7575
println!("cargo:rustc-link-lib=zircon");
7676
println!("cargo:rustc-link-lib=fdio");
77-
println!("cargo:rustc-link-lib=launchpad"); // for std::process
7877
} else if target.contains("cloudabi") {
7978
if cfg!(feature = "backtrace") {
8079
println!("cargo:rustc-link-lib=unwind");

src/libstd/sys/unix/process/process_fuchsia.rs

+28-47
Original file line numberDiff line numberDiff line change
@@ -56,68 +56,49 @@ impl Command {
5656
-> io::Result<zx_handle_t> {
5757
use sys::process::zircon::*;
5858

59-
let job_handle = zx_job_default();
6059
let envp = match maybe_envp {
6160
Some(envp) => envp.as_ptr(),
6261
None => ptr::null(),
6362
};
6463

65-
// To make sure launchpad_destroy gets called on the launchpad if this function fails
66-
struct LaunchpadDestructor(*mut launchpad_t);
67-
impl Drop for LaunchpadDestructor {
68-
fn drop(&mut self) { unsafe { launchpad_destroy(self.0); } }
69-
}
70-
71-
// Duplicate the job handle
72-
let mut job_copy: zx_handle_t = ZX_HANDLE_INVALID;
73-
zx_cvt(zx_handle_duplicate(job_handle, ZX_RIGHT_SAME_RIGHTS, &mut job_copy))?;
74-
// Create a launchpad
75-
let mut launchpad: *mut launchpad_t = ptr::null_mut();
76-
zx_cvt(launchpad_create(job_copy, self.get_argv()[0], &mut launchpad))?;
77-
let launchpad_destructor = LaunchpadDestructor(launchpad);
78-
79-
// Set the process argv
80-
zx_cvt(launchpad_set_args(launchpad, self.get_argv().len() as i32 - 1,
81-
self.get_argv().as_ptr()))?;
82-
// Setup the environment vars
83-
zx_cvt(launchpad_set_environ(launchpad, envp))?;
84-
zx_cvt(launchpad_add_vdso_vmo(launchpad))?;
85-
// Load the executable
86-
zx_cvt(launchpad_elf_load(launchpad, launchpad_vmo_from_file(self.get_argv()[0])))?;
87-
zx_cvt(launchpad_load_vdso(launchpad, ZX_HANDLE_INVALID))?;
88-
zx_cvt(launchpad_clone(launchpad, LP_CLONE_FDIO_NAMESPACE | LP_CLONE_FDIO_CWD))?;
64+
let transfer_or_clone = |opt_fd, target_fd| if let Some(local_fd) = opt_fd {
65+
fdio_spawn_action_t {
66+
action: FDIO_SPAWN_ACTION_TRANSFER_FD,
67+
local_fd,
68+
target_fd,
69+
..Default::default()
70+
}
71+
} else {
72+
fdio_spawn_action_t {
73+
action: FDIO_SPAWN_ACTION_CLONE_FD,
74+
local_fd: target_fd,
75+
target_fd,
76+
..Default::default()
77+
}
78+
};
8979

9080
// Clone stdin, stdout, and stderr
91-
if let Some(fd) = stdio.stdin.fd() {
92-
zx_cvt(launchpad_transfer_fd(launchpad, fd, 0))?;
93-
} else {
94-
zx_cvt(launchpad_clone_fd(launchpad, 0, 0))?;
95-
}
96-
if let Some(fd) = stdio.stdout.fd() {
97-
zx_cvt(launchpad_transfer_fd(launchpad, fd, 1))?;
98-
} else {
99-
zx_cvt(launchpad_clone_fd(launchpad, 1, 1))?;
100-
}
101-
if let Some(fd) = stdio.stderr.fd() {
102-
zx_cvt(launchpad_transfer_fd(launchpad, fd, 2))?;
103-
} else {
104-
zx_cvt(launchpad_clone_fd(launchpad, 2, 2))?;
105-
}
81+
let action1 = transfer_or_clone(stdio.stdin.fd(), 0);
82+
let action2 = transfer_or_clone(stdio.stdout.fd(), 1);
83+
let action3 = transfer_or_clone(stdio.stderr.fd(), 2);
84+
let actions = [action1, action2, action3];
10685

107-
// We don't want FileDesc::drop to be called on any stdio. It would close their fds. The
108-
// fds will be closed once the child process finishes.
86+
// We don't want FileDesc::drop to be called on any stdio. fdio_spawn_etc
87+
// always consumes transferred file descriptors.
10988
mem::forget(stdio);
11089

11190
for callback in self.get_closures().iter_mut() {
11291
callback()?;
11392
}
11493

115-
// `launchpad_go` destroys the launchpad, so we must not
116-
mem::forget(launchpad_destructor);
117-
11894
let mut process_handle: zx_handle_t = 0;
119-
let mut err_msg: *const libc::c_char = ptr::null();
120-
zx_cvt(launchpad_go(launchpad, &mut process_handle, &mut err_msg))?;
95+
zx_cvt(fdio_spawn_etc(
96+
0,
97+
FDIO_SPAWN_SHARE_JOB | FDIO_SPAWN_CLONE_LDSVC | FDIO_SPAWN_CLONE_NAMESPACE,
98+
self.get_argv()[0], self.get_argv().as_ptr(), envp, 3, actions.as_ptr(),
99+
&mut process_handle,
100+
ptr::null_mut(),
101+
))?;
121102
// FIXME: See if we want to do something with that err_msg
122103

123104
Ok(process_handle)

src/libstd/sys/unix/process/zircon.rs

+23-62
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![allow(non_camel_case_types)]
11+
#![allow(non_camel_case_types, unused)]
1212

1313
use convert::TryInto;
1414
use io;
@@ -117,75 +117,36 @@ extern {
117117
avail: *mut size_t) -> zx_status_t;
118118
}
119119

120-
// From `enum special_handles` in system/ulib/launchpad/launchpad.c
121-
// HND_LOADER_SVC = 0
122-
// HND_EXEC_VMO = 1
123-
// HND_SEGMENTS_VMAR = 2
124-
const HND_SPECIAL_COUNT: c_int = 3;
125-
120+
#[derive(Default)]
126121
#[repr(C)]
127-
pub struct launchpad_t {
128-
argc: u32,
129-
envc: u32,
130-
args: *const c_char,
131-
args_len: size_t,
132-
env: *const c_char,
133-
env_len: size_t,
134-
135-
handles: *mut zx_handle_t,
136-
handles_info: *mut u32,
137-
handle_count: size_t,
138-
handle_alloc: size_t,
139-
140-
entry: zx_vaddr_t,
141-
base: zx_vaddr_t,
142-
vdso_base: zx_vaddr_t,
143-
144-
stack_size: size_t,
145-
146-
special_handles: [zx_handle_t; HND_SPECIAL_COUNT as usize],
147-
loader_message: bool,
122+
pub struct fdio_spawn_action_t {
123+
pub action: u32,
124+
pub reserved0: u32,
125+
pub local_fd: i32,
126+
pub target_fd: i32,
127+
pub reserved1: u64,
148128
}
149129

150130
extern {
151-
pub fn launchpad_create(job: zx_handle_t, name: *const c_char,
152-
lp: *mut *mut launchpad_t) -> zx_status_t;
153-
154-
pub fn launchpad_go(lp: *mut launchpad_t,
155-
proc_handle: *mut zx_handle_t,
156-
err_msg: *mut *const c_char) -> zx_status_t;
157-
158-
pub fn launchpad_destroy(lp: *mut launchpad_t);
159-
160-
pub fn launchpad_set_args(lp: *mut launchpad_t, argc: c_int,
161-
argv: *const *const c_char) -> zx_status_t;
162-
163-
pub fn launchpad_set_environ(lp: *mut launchpad_t, envp: *const *const c_char) -> zx_status_t;
164-
165-
pub fn launchpad_clone(lp: *mut launchpad_t, what: u32) -> zx_status_t;
166-
167-
pub fn launchpad_clone_fd(lp: *mut launchpad_t, fd: c_int, target_fd: c_int) -> zx_status_t;
168-
169-
pub fn launchpad_transfer_fd(lp: *mut launchpad_t, fd: c_int, target_fd: c_int) -> zx_status_t;
170-
171-
pub fn launchpad_elf_load(lp: *mut launchpad_t, vmo: zx_handle_t) -> zx_status_t;
172-
173-
pub fn launchpad_add_vdso_vmo(lp: *mut launchpad_t) -> zx_status_t;
131+
pub fn fdio_spawn_etc(job: zx_handle_t, flags: u32, path: *const c_char,
132+
argv: *const *const c_char, envp: *const *const c_char,
133+
action_count: u64, actions: *const fdio_spawn_action_t,
134+
process: *mut zx_handle_t, err_msg: *mut c_char) -> zx_status_t;
135+
}
174136

175-
pub fn launchpad_load_vdso(lp: *mut launchpad_t, vmo: zx_handle_t) -> zx_status_t;
137+
// fdio_spawn_etc flags
176138

177-
pub fn launchpad_vmo_from_file(filename: *const c_char) -> zx_handle_t;
178-
}
139+
pub const FDIO_SPAWN_CLONE_JOB: u32 = 0x0001;
140+
pub const FDIO_SPAWN_CLONE_LDSVC: u32 = 0x0002;
141+
pub const FDIO_SPAWN_CLONE_NAMESPACE: u32 = 0x0004;
142+
pub const FDIO_SPAWN_CLONE_STDIO: u32 = 0x0008;
143+
pub const FDIO_SPAWN_CLONE_ENVIRON: u32 = 0x0010;
144+
pub const FDIO_SPAWN_CLONE_ALL: u32 = 0xFFFF;
179145

180-
// Launchpad clone constants
146+
// fdio_spawn_etc actions
181147

182-
pub const LP_CLONE_FDIO_NAMESPACE: u32 = 0x0001;
183-
pub const LP_CLONE_FDIO_CWD: u32 = 0x0002;
184-
// LP_CLONE_FDIO_STDIO = 0x0004
185-
// LP_CLONE_FDIO_ALL = 0x00FF
186-
// LP_CLONE_ENVIRON = 0x0100
187-
// LP_CLONE_DEFAULT_JOB = 0x0200
188-
// LP_CLONE_ALL = 0xFFFF
148+
pub const FDIO_SPAWN_ACTION_CLONE_FD: u32 = 0x0001;
149+
pub const FDIO_SPAWN_ACTION_TRANSFER_FD: u32 = 0x0002;
189150

190151
// Errors
191152

0 commit comments

Comments
 (0)