Skip to content

Conversation

@wdcui
Copy link
Member

@wdcui wdcui commented Feb 2, 2026

Summary

This PR adds support for the fchdir(2) syscall, which changes the current working directory to the directory referred to by a file descriptor.

Changes

litebox_common_linux

  • Add Fchdir { fd: i32 } variant to SyscallRequest enum
  • Add Sysno::fchdir syscall parsing

litebox_shim_linux

  • Add DirectoryPath metadata type for storing directory paths
  • Add cwd field to FsState for per-process CWD tracking
  • Implement compute_absolute_path() helper method
  • Update sys_open() to store directory paths as entry metadata
  • Update sys_getcwd() to return actual CWD from FsState (was returning hardcoded "/")
  • Implement sys_fchdir() to change CWD based on directory fd
  • Add dispatch case for Fchdir in do_syscall

Error Handling

The implementation correctly handles:

  • EBADF: Invalid or negative file descriptor
  • ENOTDIR: File descriptor refers to a non-directory (regular file, socket, pipe, etc.)

Tests Added

  • test_fchdir_basic: Basic fchdir functionality
  • test_fchdir_ebadf: Invalid fd handling (returns EBADF)
  • test_fchdir_enotdir: Non-directory fd handling (returns ENOTDIR)
  • test_fchdir_to_root: Changing back to root directory

Testing

All existing tests pass. New tests verify the basic functionality and error handling.

cargo test --package litebox_shim_linux test_fchdir
# 4 tests pass

wdcui added 2 commits February 2, 2026 01:29
This commit adds support for the fchdir(2) syscall, which changes the
current working directory to the directory referred to by a file
descriptor.

Changes:
- Add Fchdir { fd: i32 } variant to SyscallRequest enum
- Add Sysno::fchdir syscall parsing in litebox_common_linux
- Add DirectoryPath metadata type for storing directory paths
- Add cwd field to FsState for per-process CWD tracking
- Implement compute_absolute_path() helper method
- Update sys_open() to store directory paths as entry metadata
- Update sys_getcwd() to return actual CWD from FsState
- Implement sys_fchdir() to change CWD based on directory fd
- Add dispatch case for Fchdir in do_syscall

Tests added:
- test_fchdir_basic: Basic fchdir functionality
- test_fchdir_ebadf: Invalid fd handling (returns EBADF)
- test_fchdir_enotdir: Non-directory fd handling (returns ENOTDIR)
- test_fchdir_to_root: Changing back to root directory
Review feedback addressed:
- Add execute permission check in sys_fchdir (returns EACCES if directory
  lacks execute permission)
- Add test_fchdir_eacces test case
- Document known limitation about directory rename behavior in function
  documentation

The directory rename limitation (where fchdir uses the path at open time
rather than the current path after rename) is an architectural issue that
requires more extensive changes to fix properly. This is now documented
in the function documentation.
@github-actions
Copy link

github-actions bot commented Feb 2, 2026

🤖 SemverChecks 🤖 ⚠️ Potential breaking API changes detected ⚠️

Click for details
--- failure enum_no_repr_variant_discriminant_changed: enum variant had its discriminant change value ---

Description:
The enum's variant had its discriminant value change. This breaks downstream code that used its value via a numeric cast like `as isize`.
        ref: https://doc.rust-lang.org/reference/items/enumerations.html#assigning-discriminant-values
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.46.0/src/lints/enum_no_repr_variant_discriminant_changed.ron

Failed in:
  variant SyscallRequest::Prctl 90 -> 91 in /home/runner/work/litebox/litebox/litebox_common_linux/src/lib.rs:2256
  variant SyscallRequest::Alarm 91 -> 92 in /home/runner/work/litebox/litebox/litebox_common_linux/src/lib.rs:2259
  variant SyscallRequest::SetITimer 92 -> 93 in /home/runner/work/litebox/litebox/litebox_common_linux/src/lib.rs:2262

@wdcui wdcui closed this Feb 2, 2026
@wdcui wdcui deleted the wdcui/fchdir branch February 2, 2026 22:53
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