Skip to content

Add support for getpgrp/getpgid/setpgid/TIOCGPGRP/TIOCSPGRP#640

Open
wdcui wants to merge 4 commits intomainfrom
wdcui/implement-getpgrp-syscall
Open

Add support for getpgrp/getpgid/setpgid/TIOCGPGRP/TIOCSPGRP#640
wdcui wants to merge 4 commits intomainfrom
wdcui/implement-getpgrp-syscall

Conversation

@wdcui
Copy link
Member

@wdcui wdcui commented Feb 4, 2026

This PR adds the support for getpgrp/getpgid/setpgid/TIOCGPGRP/TIOCSPGRP.

wdcui added 2 commits February 4, 2026 05:41
- 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.
@wdcui wdcui requested a review from CvvT February 4, 2026 21:16
// 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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. I added TIOCSPGRP.

/// 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@wdcui wdcui force-pushed the wdcui/implement-getpgrp-syscall branch from 5e3ab6a to bf807a4 Compare February 5, 2026 04:46
@github-actions
Copy link

github-actions bot commented Feb 5, 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::Getuid 78 -> 81 in /home/runner/work/litebox/litebox/litebox_common_linux/src/lib.rs:2234
  variant SyscallRequest::Geteuid 79 -> 82 in /home/runner/work/litebox/litebox/litebox_common_linux/src/lib.rs:2235
  variant SyscallRequest::Getgid 80 -> 83 in /home/runner/work/litebox/litebox/litebox_common_linux/src/lib.rs:2236
  variant SyscallRequest::Getegid 81 -> 84 in /home/runner/work/litebox/litebox/litebox_common_linux/src/lib.rs:2237
  variant SyscallRequest::Sysinfo 82 -> 85 in /home/runner/work/litebox/litebox/litebox_common_linux/src/lib.rs:2238
  variant SyscallRequest::CapGet 83 -> 86 in /home/runner/work/litebox/litebox/litebox_common_linux/src/lib.rs:2241
  variant SyscallRequest::GetDirent64 84 -> 87 in /home/runner/work/litebox/litebox/litebox_common_linux/src/lib.rs:2245
  variant SyscallRequest::SchedGetAffinity 85 -> 88 in /home/runner/work/litebox/litebox/litebox_common_linux/src/lib.rs:2250
  variant SyscallRequest::SchedYield 86 -> 89 in /home/runner/work/litebox/litebox/litebox_common_linux/src/lib.rs:2255
  variant SyscallRequest::Futex 87 -> 90 in /home/runner/work/litebox/litebox/litebox_common_linux/src/lib.rs:2256
  variant SyscallRequest::Execve 88 -> 91 in /home/runner/work/litebox/litebox/litebox_common_linux/src/lib.rs:2259
  variant SyscallRequest::Umask 89 -> 92 in /home/runner/work/litebox/litebox/litebox_common_linux/src/lib.rs:2264
  variant SyscallRequest::Prctl 90 -> 93 in /home/runner/work/litebox/litebox/litebox_common_linux/src/lib.rs:2267
  variant SyscallRequest::Alarm 91 -> 94 in /home/runner/work/litebox/litebox/litebox_common_linux/src/lib.rs:2270
  variant SyscallRequest::SetITimer 92 -> 95 in /home/runner/work/litebox/litebox/litebox_common_linux/src/lib.rs:2273

@wdcui wdcui changed the title Add support for getpgrp Add support for getpgrp/getpgid/setpgid/TIOCGPGRP/TIOCSPGRP Feb 5, 2026
if pid == 0 || pid == self.pid {
Ok(self.pgrp.load(core::sync::atomic::Ordering::Relaxed))
} else {
Err(Errno::ESRCH)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar

Comment on lines +1188 to +1189
self.pgrp
.store(target_pgid, core::sync::atomic::Ordering::Relaxed);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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