Skip to content

Commit 8e3789d

Browse files
bors[bot]vi
andauthored
Merge #1293
1293: Mark nix::unistd::fork as unsafe. r=asomers a=vi Fix tests. No change in documentation. Resolves #1030. Don't forget to bump major version number to `0.19`. Co-authored-by: Vitaly _Vi Shukela <vi0oss@gmail.com>
2 parents fe0aa23 + 42172a6 commit 8e3789d

File tree

6 files changed

+15
-13
lines changed

6 files changed

+15
-13
lines changed

Diff for: CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1515
(#[1284](https://github.com/nix-rust/nix/pull/1284))
1616
- Changed unistd::{execv,execve,execvp,execvpe,fexecve,execveat} to take both `&[&CStr]` and `&[CString]` as its list argument(s).
1717
(#[1278](https://github.com/nix-rust/nix/pull/1278))
18+
- Made `unistd::fork` an unsafe funtion, bringing it in line with [libstd's decision](https://github.com/rust-lang/rust/pull/58059).
19+
(#[1293](https://github.com/nix-rust/nix/pull/1293))
1820
### Fixed
1921
### Removed
2022

Diff for: src/unistd.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ impl ForkResult {
200200
/// ```no_run
201201
/// use nix::unistd::{fork, ForkResult};
202202
///
203-
/// match fork() {
203+
/// match unsafe{fork()} {
204204
/// Ok(ForkResult::Parent { child, .. }) => {
205205
/// println!("Continuing execution in parent process, new child has pid: {}", child);
206206
/// }
@@ -230,9 +230,9 @@ impl ForkResult {
230230
///
231231
/// [async-signal-safe]: http://man7.org/linux/man-pages/man7/signal-safety.7.html
232232
#[inline]
233-
pub fn fork() -> Result<ForkResult> {
233+
pub unsafe fn fork() -> Result<ForkResult> {
234234
use self::ForkResult::*;
235-
let res = unsafe { libc::fork() };
235+
let res = libc::fork();
236236

237237
Errno::result(res).map(|res| match res {
238238
0 => Child,

Diff for: test/sys/test_ptrace.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ fn test_ptrace_cont() {
8181
return;
8282
}
8383

84-
match fork().expect("Error: Fork Failed") {
84+
match unsafe{fork()}.expect("Error: Fork Failed") {
8585
Child => {
8686
ptrace::traceme().unwrap();
8787
// As recommended by ptrace(2), raise SIGTRAP to pause the child
@@ -132,7 +132,7 @@ fn test_ptrace_syscall() {
132132

133133
let _m = crate::FORK_MTX.lock().expect("Mutex got poisoned by another test");
134134

135-
match fork().expect("Error: Fork Failed") {
135+
match unsafe{fork()}.expect("Error: Fork Failed") {
136136
Child => {
137137
ptrace::traceme().unwrap();
138138
// first sigstop until parent is ready to continue

Diff for: test/sys/test_uio.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ fn test_process_vm_readv() {
212212
let mut vector = vec![1u8, 2, 3, 4, 5];
213213

214214
let (r, w) = pipe().unwrap();
215-
match fork().expect("Error: Fork Failed") {
215+
match unsafe{fork()}.expect("Error: Fork Failed") {
216216
Parent { child } => {
217217
close(w).unwrap();
218218
// wait for child

Diff for: test/sys/test_wait.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn test_wait_signal() {
1111
let _ = crate::FORK_MTX.lock().expect("Mutex got poisoned by another test");
1212

1313
// Safe: The child only calls `pause` and/or `_exit`, which are async-signal-safe.
14-
match fork().expect("Error: Fork Failed") {
14+
match unsafe{fork()}.expect("Error: Fork Failed") {
1515
Child => {
1616
pause();
1717
unsafe { _exit(123) }
@@ -28,7 +28,7 @@ fn test_wait_exit() {
2828
let _m = crate::FORK_MTX.lock().expect("Mutex got poisoned by another test");
2929

3030
// Safe: Child only calls `_exit`, which is async-signal-safe.
31-
match fork().expect("Error: Fork Failed") {
31+
match unsafe{fork()}.expect("Error: Fork Failed") {
3232
Child => unsafe { _exit(12); },
3333
Parent { child } => {
3434
assert_eq!(waitpid(child, None), Ok(WaitStatus::Exited(child, 12)));
@@ -48,7 +48,7 @@ fn test_waitstatus_from_raw() {
4848
fn test_waitstatus_pid() {
4949
let _m = crate::FORK_MTX.lock().expect("Mutex got poisoned by another test");
5050

51-
match fork().unwrap() {
51+
match unsafe{fork()}.unwrap() {
5252
Child => unsafe { _exit(0) },
5353
Parent { child } => {
5454
let status = waitpid(child, None).unwrap();
@@ -98,7 +98,7 @@ mod ptrace {
9898
require_capability!(CAP_SYS_PTRACE);
9999
let _m = crate::FORK_MTX.lock().expect("Mutex got poisoned by another test");
100100

101-
match fork().expect("Error: Fork Failed") {
101+
match unsafe{fork()}.expect("Error: Fork Failed") {
102102
Child => ptrace_child(),
103103
Parent { child } => ptrace_parent(child),
104104
}

Diff for: test/test_unistd.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ fn test_fork_and_waitpid() {
3232
let _m = crate::FORK_MTX.lock().expect("Mutex got poisoned by another test");
3333

3434
// Safe: Child only calls `_exit`, which is signal-safe
35-
match fork().expect("Error: Fork Failed") {
35+
match unsafe{fork()}.expect("Error: Fork Failed") {
3636
Child => unsafe { _exit(0) },
3737
Parent { child } => {
3838
// assert that child was created and pid > 0
@@ -60,7 +60,7 @@ fn test_wait() {
6060
let _m = crate::FORK_MTX.lock().expect("Mutex got poisoned by another test");
6161

6262
// Safe: Child only calls `_exit`, which is signal-safe
63-
match fork().expect("Error: Fork Failed") {
63+
match unsafe{fork()}.expect("Error: Fork Failed") {
6464
Child => unsafe { _exit(0) },
6565
Parent { child } => {
6666
let wait_status = wait();
@@ -302,7 +302,7 @@ macro_rules! execve_test_factory(
302302
// Safe: Child calls `exit`, `dup`, `close` and the provided `exec*` family function.
303303
// NOTE: Technically, this makes the macro unsafe to use because you could pass anything.
304304
// The tests make sure not to do that, though.
305-
match fork().unwrap() {
305+
match unsafe{fork()}.unwrap() {
306306
Child => {
307307
// Make `writer` be the stdout of the new process.
308308
dup2(writer, 1).unwrap();

0 commit comments

Comments
 (0)