Skip to content

Commit 49de4df

Browse files
init: ensure the std{in,out,err} are avaliable before the X session is
started Signed-off-by: Andy-Python-Programmer <[email protected]>
1 parent 90422c2 commit 49de4df

File tree

4 files changed

+40
-12
lines changed

4 files changed

+40
-12
lines changed

bootstrap/net.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ packages:
2626
- '@THIS_COLLECT_DIR@/usr'
2727
- '-j@PARALLELISM@'
2828
environ:
29-
RUSTFLAGS: '-Cforce-frame-pointers=yes -Clink-args=-no-pie'
29+
RUSTFLAGS: '-Cforce-frame-pointers=yes -Clink-args=-no-pie'

userland/Cargo.lock

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

userland/apps/init/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ edition = "2021"
66

77
[dependencies]
88
aero_syscall = { path = "../../../src/aero_syscall" }
9+
libc = { git = "https://github.com/Andy-Python-Programmer/libc" }

userland/apps/init/src/main.rs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,47 @@
1818
*/
1919

2020
use std::error::Error;
21-
use std::fs::OpenOptions;
21+
use std::fs::{File, OpenOptions};
22+
use std::os::fd::AsRawFd;
2223
use std::process::Command;
2324

24-
use std::mem;
25-
2625
const TTY_PATH: &str = "/dev/tty";
2726

27+
fn remove_cloexec(file: &File) {
28+
// By default rust automatically sets the close-on-exe flag to prevent
29+
// leaking file descriptors.
30+
//
31+
// OpenOptions::custom_flags() only allows insertion of flags and are
32+
// overwritten by the flags set by the standard library. So here, we
33+
// need to manually update the flags after opening the file.
34+
let fd = file.as_raw_fd();
35+
36+
unsafe {
37+
assert!(libc::fcntl(fd, libc::F_SETFD, 0 /* flags */) == 0);
38+
}
39+
}
40+
2841
fn main() -> Result<(), Box<dyn Error>> {
2942
// Open the stdin, stdout and stderr file descriptors.
30-
//
31-
// mem::forget(): don't drop the object which in turn will close the
32-
// file.
33-
mem::forget(OpenOptions::new().read(true).open(TTY_PATH)?); // fd=0 for stdin
34-
mem::forget(OpenOptions::new().write(true).open(TTY_PATH)?); // fd=1 for stdout
35-
mem::forget(OpenOptions::new().write(true).open(TTY_PATH)?); // fd=2 for stderr
43+
let stdin = OpenOptions::new().read(true).open(TTY_PATH)?; // fd=0
44+
let stdout = OpenOptions::new().write(true).open(TTY_PATH)?; // fd=1
45+
let stderr = OpenOptions::new().write(true).open(TTY_PATH)?; // fd=2
46+
47+
remove_cloexec(&stdin);
48+
remove_cloexec(&stdout);
49+
remove_cloexec(&stderr);
3650

3751
Command::new("dhcpd").spawn()?;
52+
53+
// Close the std{in,out,err} file descriptors, since now we are going to
54+
// start an X session.
55+
drop(stdin);
56+
drop(stdout);
57+
drop(stderr);
58+
3859
Command::new("startx")
3960
.env("RUST_BACKTRACE", "full")
4061
.spawn()?;
4162

42-
loop {}
63+
Ok(())
4364
}

0 commit comments

Comments
 (0)