Skip to content

Commit 7bfbb0f

Browse files
init: swap /dev/tty std{in,out,err} file descriptors with /dev/null
* This fixes the fwrite() errors in mlibc on Xorg startup. * Implement poll() on `/dev/null` Signed-off-by: Andy-Python-Programmer <[email protected]>
1 parent 73749b9 commit 7bfbb0f

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

src/aero_kernel/src/fs/devfs.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ impl INodeInterface for DevNull {
188188
fn write_at(&self, _offset: usize, buffer: &[u8]) -> Result<usize> {
189189
Ok(buffer.len())
190190
}
191+
192+
fn poll(&self, _table: Option<&mut PollTable>) -> Result<PollFlags> {
193+
Ok(PollFlags::OUT)
194+
}
191195
}
192196

193197
struct DevKmsg(usize);

userland/apps/init/src/main.rs

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2021-2022 The Aero Project Developers.
2+
* Copyright (C) 2021-2022 The Aero Project Developers.
33
*
44
* This file is part of The Aero Project.
55
*
@@ -23,18 +23,29 @@ use std::os::fd::AsRawFd;
2323
use std::process::Command;
2424

2525
const TTY_PATH: &str = "/dev/tty";
26+
const DEV_NULL: &str = "/dev/null";
2627

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();
28+
struct FileSet<const N: usize>([File; N]);
3529

36-
unsafe {
37-
assert!(libc::fcntl(fd, libc::F_SETFD, 0 /* flags */) == 0);
30+
impl<const N: usize> FileSet<N> {
31+
fn new(files: [File; N]) -> Self {
32+
Self(files)
33+
}
34+
35+
fn remove_cloexec(&self) {
36+
for file in self.0.iter() {
37+
// By default rust automatically sets the close-on-exe flag to prevent
38+
// leaking file descriptors.
39+
//
40+
// OpenOptions::custom_flags() only allows insertion of flags and are
41+
// overwritten by the flags set by the standard library. So here, we
42+
// need to manually update the flags after opening the file.
43+
let fd = file.as_raw_fd();
44+
45+
unsafe {
46+
assert!(libc::fcntl(fd, libc::F_SETFD, 0 /* flags */) == 0);
47+
}
48+
}
3849
}
3950
}
4051

@@ -44,17 +55,21 @@ fn main() -> Result<(), Box<dyn Error>> {
4455
let stdout = OpenOptions::new().write(true).open(TTY_PATH)?; // fd=1
4556
let stderr = OpenOptions::new().write(true).open(TTY_PATH)?; // fd=2
4657

47-
remove_cloexec(&stdin);
48-
remove_cloexec(&stdout);
49-
remove_cloexec(&stderr);
58+
{
59+
let stdset = FileSet::new([stdin, stdout, stderr]);
60+
stdset.remove_cloexec();
61+
62+
Command::new("dhcpd").spawn()?;
63+
}
5064

51-
Command::new("dhcpd").spawn()?;
65+
// Swap the `/dev/tty` std{in,out,err} file descriptors with `/dev/null` to suppress the Xorg
66+
// server logs.
67+
let stdin = OpenOptions::new().read(true).open(DEV_NULL)?; // fd=0
68+
let stdout = OpenOptions::new().write(true).open(DEV_NULL)?; // fd=1
69+
let stderr = OpenOptions::new().write(true).open(DEV_NULL)?; // fd=2
5270

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);
71+
let stdset = FileSet::new([stdin, stdout, stderr]);
72+
stdset.remove_cloexec();
5873

5974
Command::new("startx")
6075
.env("RUST_BACKTRACE", "full")

0 commit comments

Comments
 (0)