Skip to content

Fix O_APPEND, a getcwd() kernel panic, and console timestamps - #323

Merged
hexagonal-sun merged 3 commits into
hexagonal-sun:masterfrom
nrchnnas:fix/console-ts-o-append-getcwd
Oct 6, 2026
Merged

hexagonal-sun merged 3 commits into
hexagonal-sun:masterfrom
nrchnnas:fix/console-ts-o-append-getcwd

Conversation

@nrchnnas

@nrchnnas nrchnnas commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

Three small, independent fixes, found while running BusyBox on moss.

1. fs: reg: honour O_APPEND

Bug: OpenFlags::O_APPEND is defined but never checked, so every write goes to the current file position. As a result, shell >> overwrites the file instead of appending:

/ # echo a > /tmp/t; echo b >> /tmp/t; cat /tmp/t
b

Fix: RegFile::write now moves the cursor to the end of the file before each write when the file was opened with O_APPEND. writev goes through write, so it is covered too; pwrite keeps its explicit offset.

Test: the new usertest test_o_append also checks that seeking to 0 first doesn't stop the append.

2. fs: getcwd: return ERANGE when the buffer is too small

Bug: sys_getcwd returns KernelError::TooLarge. That has no errno mapping, so kern_err_to_syscall() reaches todo!(), and any process can panic the kernel with getcwd(buf, 1):

usertest::fs::test_getcwd_buffer_too_small ...[    1.1737690] moss: Kernel panicked at
libkernel/src/error/syscall_error.rs:85:14: not yet implemented: Provided object is too large

Fix: return RangeError (ERANGE), which is what Linux returns in this case.

Test: new usertest test_getcwd_buffer_too_small.

3. console: fix the sub-second field of log timestamps

Bug: the console logger prints uptime.as_micros(), the total microseconds, as the fractional part. After one second it prints [ 1.1737690] (see the panic line above) instead of [ 1.173769].

Fix: use subsec_micros(), matching Linux's printk format.

Testing

  • cargo fmt --all -- --check passes, and clippy reports nothing new in the changed files.
  • cargo run -r -- --init /bin/usertest prints All tests passed, including both new tests.
  • Each new test fails without its fix:
    • the getcwd test panics the kernel (output above);
    • test_o_append fails with left: "third\n\n", right: "first\nsecond\nthird\n".

@hexagonal-sun

Copy link
Copy Markdown
Owner

LGTM! I fixed the clippy lint on master. If you rebase your MR should not include that unrelated failure.

ConsoleLogger printed `uptime.as_micros()` (total microseconds since
boot) as the fractional part, so after the first second the timestamps
are wrong, e.g. "[    4.4015238]" instead of "[    4.401523]". Use
`subsec_micros()`, matching Linux's printk format.
OpenFlags::O_APPEND was defined but never checked, so every write went
to the current file position. As a result, shell `>>` overwrote files
instead of appending to them:

  / # echo a > /tmp/t; echo b >> /tmp/t; cat /tmp/t
  b

RegFile::write now moves the cursor to the end of the file before each
write when the file was opened with O_APPEND. writev goes through write,
so it is covered too; pwrite keeps its explicit offset. Add a usertest
that also checks that a seek before the write doesn't stop the append.
sys_getcwd returned KernelError::TooLarge when the cwd didn't fit in the
user's buffer. TooLarge has no errno mapping, so kern_err_to_syscall()
reached its `todo!()` and panicked the kernel. Any process could
trigger this with getcwd(buf, 1).

Return RangeError (ERANGE), which is what Linux returns in this case,
and add a usertest.
@nrchnnas

nrchnnas commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto master, thanks for the review!

@nrchnnas
nrchnnas force-pushed the fix/console-ts-o-append-getcwd branch from 256ff78 to 9a175b3 Compare October 6, 2026 05:02
@hexagonal-sun
hexagonal-sun merged commit 2b7d519 into hexagonal-sun:master Oct 6, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants