Add support for getpgrp/getpgid/setpgid/TIOCGPGRP/TIOCSPGRP#640
Add support for getpgrp/getpgid/setpgid/TIOCGPGRP/TIOCSPGRP#640
Conversation
- Add Getpgrp variant to SyscallRequest enum in litebox_common_linux - Add syscall number mapping for Sysno::getpgrp - Implement sys_getpgrp() in litebox_shim_linux/syscalls/process.rs - Add dispatch case for SyscallRequest::Getpgrp in litebox_shim_linux - Add unit test for getpgrp syscall The getpgrp syscall returns the process group ID. This implementation returns the process ID, which is the default behavior for a process that hasn't explicitly joined another process group via setpgid. This is required for bash initialization.
- Add TIOCGPGRP constant (0x540F) to litebox_common_linux - Add TIOCGPGRP variant to IoctlArg enum - Add TIOCGPGRP to ioctl syscall parsing - Implement TIOCGPGRP handler in litebox_shim_linux/syscalls/file.rs TIOCGPGRP returns the process group ID of the foreground process group on the terminal. In LiteBox's simplified model, each process forms its own process group where PGID equals PID. This is required for bash shell initialization along with getpgrp syscall.
| // In LiteBox, processes are simplified: each process forms its own process group | ||
| // where the process group ID equals the process ID. This is a simplification | ||
| // compared to standard Unix where processes can explicitly join different groups. | ||
| let pid = self.sys_getpid(); |
There was a problem hiding this comment.
TIOCGPGRP is not equivalent to getpgrp, it returns the pgid of the foreground process on the terminal. If litebox is running in background, it should not return its own pid. I'm not sure here what value we should return as the foreground process on the terminal is outside LiteBox. Maybe just log and return an error code for now?
| /// explicitly joined another process group via `setpgid`. | ||
| pub(crate) fn sys_getpgrp(&self) -> i32 { | ||
| // In a full implementation, we'd track pgid separately. For now, return pid | ||
| // which is the default pgid for a new process. |
There was a problem hiding this comment.
For a forked process, the pgid is the same as its parent, but right now we don't support fork yet. Should we just implement setpgid if it is simple for an agent to do?
There was a problem hiding this comment.
Yes, added the support for setpgid.
… group support Extends the partial getpgrp/TIOCGPGRP implementation with proper pgrp tracking per task, setpgid/getpgid syscalls, and TIOCSPGRP ioctl backed by a global foreground process group state. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
5e3ab6a to
bf807a4
Compare
|
🤖 SemverChecks 🤖 Click for details |
| if pid == 0 || pid == self.pid { | ||
| Ok(self.pgrp.load(core::sync::atomic::Ordering::Relaxed)) | ||
| } else { | ||
| Err(Errno::ESRCH) |
There was a problem hiding this comment.
ERRORS [top](https://man7.org/linux/man-pages/man3/getpgid.3p.html#top_of_page)
The getpgid() function shall fail if:
EPERM The process whose process ID is equal to pid is not in the
same session as the calling process, and the implementation
does not allow access to the process group ID of that
process from the calling process.
ESRCH There is no process with a process ID equal to pid.
The getpgid() function may fail if:
EINVAL The value of the pid argument is invalid.
Add some comment here? Currently there is only process so we always return ESRCH.
| return Err(Errno::EINVAL); | ||
| } | ||
| if target_pid != self.pid { | ||
| return Err(Errno::ESRCH); |
| self.pgrp | ||
| .store(target_pgid, core::sync::atomic::Ordering::Relaxed); |
There was a problem hiding this comment.
We should add some check to ensure that pgid is valid. Since we only have one process, we can just return an error if pgid differs from the current one. Don't need to change pgid at all.
| boot_time: self.platform.now(), | ||
| load_filter: self.load_filter, | ||
| next_thread_id: 2.into(), // start from 2, as 1 is used by the main thread | ||
| fg_pgrp: core::sync::atomic::AtomicI32::new(1), |
There was a problem hiding this comment.
In LiteBox, there is no concept of forground and background process. Initializing it to 1 indicates that LiteBox always runs in foreground, but I can also run it in background (cargo run -p litebox_runner_linux_userland ... &). In the latter case, LiteBox does not control the foreground process. Depending on how applications use this syscall, we may have different designs. For now, maybe we could just return some error code and print out some warnings.
This PR adds the support for getpgrp/getpgid/setpgid/TIOCGPGRP/TIOCSPGRP.