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
use fs;
use ;
// #[test]
// fn it_should_dup() {
// match unsafe { fork() } {
// Ok(ForkResult::Parent { child, .. }) => {
// waitpid(child, None).unwrap();
// }
// Ok(ForkResult::Child { .. }) => {
// let null_fd = OpenOptions::new()
// .read(true)
// .write(true)
// .open("/dev/null")
// .unwrap()
// .into_raw_fd();
// let file = Path::new("a.txt");
// let file = OpenOptions::new()
// .write(true)
// .create(true)
// .truncate(true)
// .mode(S_IWUSR | S_IRUSR | S_IRGRP | S_IWGRP)
// .open(file)
// .unwrap();
// let fd = File::into_raw_fd(file);
// println!("fd: {}", fd);
// println!("null: {}", null_fd);
// dup2(null_fd, STDIN_FILENO).unwrap();
// dup2(fd, STDOUT_FILENO).unwrap();
// dup2(null_fd, STDERR_FILENO).unwrap();
// close(fd).unwrap();
// close(null_fd).unwrap();
// write(libc::STDOUT_FILENO, "I'm a new child process\n".as_bytes()).ok();
// execvp(
// into_c_string("echo").as_c_str(),
// &[into_c_string("echo"), into_c_string("1234444")],
// )
// .unwrap();
// }
// Err(_) => {}
// };
// }
// fn into_c_string<S: Into<String>>(string: S) -> CString {
// let string = string.into();
// let string = string.as_str();
// CString::new(string).expect("Convert &str to CString should work")
// }