-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathposix_spawn.rs
More file actions
122 lines (113 loc) · 3.43 KB
/
posix_spawn.rs
File metadata and controls
122 lines (113 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use std::thread;
use fspy_shared_unix::exec::ExecResolveConfig;
use libc::{c_char, c_int};
use crate::{
client::{global_client, raw_exec::RawExec},
macros::intercept,
};
type PosixSpawnFn = unsafe extern "C" fn(
pid: *mut libc::pid_t,
prog: *const c_char,
file_actions: *const libc::posix_spawn_file_actions_t,
attrp: *const libc::posix_spawnattr_t,
argv: *const *mut c_char,
envp: *const *mut c_char,
) -> libc::c_int;
unsafe fn handle_posix_spawn(
config: ExecResolveConfig,
original: PosixSpawnFn,
pid: *mut libc::pid_t,
file: *const c_char,
mut file_actions: *const libc::posix_spawn_file_actions_t,
attrp: *const libc::posix_spawnattr_t,
argv: *const *mut c_char,
envp: *const *mut c_char,
) -> c_int {
struct AssertSend<T>(T);
unsafe impl<T> Send for AssertSend<T> {}
let client = global_client()
.expect("posix_spawn(p) unexpectedly called before client initialized in ctor");
if let Err(errno) = unsafe { client.handle_posix_spawn_opts(&mut file_actions, attrp) } {
return errno as _;
}
let result = unsafe {
client.handle_exec::<c_int>(
config,
RawExec { prog: file, argv: argv.cast(), envp: envp.cast() },
|raw_command, pre_exec| {
let call_original = move || {
original(
pid,
raw_command.prog,
file_actions,
attrp,
raw_command.argv.cast(),
raw_command.envp.cast(),
)
};
if let Some(pre_exec) = pre_exec {
thread::scope(move |s| {
let call_original = AssertSend(call_original);
s.spawn(move || {
let call_original = call_original;
pre_exec.run()?;
nix::Result::Ok((call_original.0)())
})
.join()
.unwrap()
})
} else {
Ok(call_original())
}
},
)
};
match result {
Err(errno) => errno as _,
Ok(ret) => ret,
}
}
intercept!(posix_spawnp(64): PosixSpawnFn);
unsafe extern "C" fn posix_spawnp(
pid: *mut libc::pid_t,
file: *const c_char,
file_actions: *const libc::posix_spawn_file_actions_t,
attrp: *const libc::posix_spawnattr_t,
argv: *const *mut c_char,
envp: *const *mut c_char,
) -> libc::c_int {
unsafe {
handle_posix_spawn(
ExecResolveConfig::search_path_enabled(None),
posix_spawnp::original(),
pid,
file,
file_actions,
attrp,
argv,
envp,
)
}
}
intercept!(posix_spawn(64): PosixSpawnFn);
unsafe extern "C" fn posix_spawn(
pid: *mut libc::pid_t,
file: *const c_char,
file_actions: *const libc::posix_spawn_file_actions_t,
attrp: *const libc::posix_spawnattr_t,
argv: *const *mut c_char,
envp: *const *mut c_char,
) -> libc::c_int {
unsafe {
handle_posix_spawn(
ExecResolveConfig::search_path_disabled(),
posix_spawn::original(),
pid,
file,
file_actions,
attrp,
argv,
envp,
)
}
}