setpgid: support changing between process groups and doing it for other processes

This commit is contained in:
Matheus
2024-05-22 09:46:31 -03:00
parent 13e7a6c1cc
commit ff13369073
+39 -4
View File
@@ -12,10 +12,45 @@ syscallret_t syscall_setsid(context_t *) {
syscallret_t syscall_setpgid(context_t *, pid_t pid, pid_t pgid) {
syscallret_t ret;
__assert(pid == 0 || pid == _cpu()->thread->proc->pid);
__assert(pgid == 0 || pgid == _cpu()->thread->proc->pid);
ret.errno = jobctl_newgroup(_cpu()->thread->proc);
ret.ret = ret.errno ? -1 : _cpu()->thread->proc->pid;
proc_t *proc = NULL;
if (pid == 0) {
proc = _cpu()->thread->proc;
PROC_HOLD(proc);
} else {
proc = sched_getprocfrompid(pid);
}
if (proc == NULL) {
ret.errno = ESRCH;
return ret;
}
proc_t *pgrp = NULL;
if (pgid == 0) {
pgrp = _cpu()->thread->proc;
PROC_HOLD(pgrp);
} else {
pgrp = sched_getprocfrompid(pgid);
}
if (pgrp == NULL) {
PROC_RELEASE(proc);
ret.errno = EPERM;
return ret;
}
if (pgid == pid || pgid == 0) {
// create new group for process
ret.errno = jobctl_newgroup(proc);
} else {
// switch process group
ret.errno = jobctl_changegroup(proc, pgrp);
}
PROC_RELEASE(pgrp);
PROC_RELEASE(proc);
ret.ret = ret.errno ? -1 : 0;
return ret;
}