kernel: Add basic process groups
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-07-11 11:51:07 +02:00
parent 6546f490b6
commit 7328cfe734
Signed by: apio
GPG Key ID: B8A7D06E42258954
8 changed files with 58 additions and 3 deletions

View File

@ -117,6 +117,8 @@ Result<u64> sys_execve(Registers* regs, SyscallArgs args)
current->signal_handlers[i] = { .sa_handler = SIG_DFL, .sa_mask = 0, .sa_flags = 0 };
}
current->has_called_exec = true;
kinfoln("exec: thread %lu was replaced with %s", current->id, path.chars());
return 0;

View File

@ -95,6 +95,39 @@ Result<u64> sys_setegid(Registers*, SyscallArgs args)
return 0;
}
Result<u64> sys_setpgid(Registers*, SyscallArgs args)
{
pid_t pid = (pid_t)args[0];
pid_t pgid = (pid_t)args[1];
auto* current = Scheduler::current();
if (pid == 0) pid = (pid_t)current->id;
if (pgid == 0) pgid = (pid_t)current->id;
if (pgid < 0) return err(EINVAL);
auto* thread = TRY(Result<Thread*>::from_option(Scheduler::find_by_pid(pid), ESRCH));
if (thread != current && thread->parent != current) return err(ESRCH);
// FIXME: Weird session stuff, we don't have that currently.
if (thread->has_called_exec) return err(EPERM);
if (pgid != (pid_t)current->id)
{
bool pgid_exists = false;
Scheduler::for_each_in_process_group(pgid, [&pgid_exists](Thread*) {
pgid_exists = true;
return false;
});
if (!pgid_exists) return err(EPERM);
}
thread->pgid = (u64)pgid;
return 0;
}
Result<u64> sys_fchmodat(Registers*, SyscallArgs args)
{
int dirfd = (int)args[0];

View File

@ -45,6 +45,19 @@ namespace Scheduler
}
}
template <typename Callback> void for_each_in_process_group(pid_t group, Callback callback)
{
for (Thread* current = g_threads.first().value_or(nullptr); current;
current = g_threads.next(current).value_or(nullptr))
{
if (current->pgid == (u64)group)
{
bool should_continue = callback(current);
if (!should_continue) return;
}
}
}
void dump_state();
bool has_children(Thread* thread);

View File

@ -57,6 +57,7 @@ struct Thread : public LinkedListNode<Thread>
Registers regs;
u64 id;
u64 pgid { 0 };
Credentials auth;
@ -82,14 +83,14 @@ struct Thread : public LinkedListNode<Thread>
struct sigaction signal_handlers[NSIG];
sigset_t signal_mask { 0 };
int pending_signals { 0 };
sigset_t pending_signals { 0 };
FPData fp_data;
ThreadState state = ThreadState::Runnable;
bool is_kernel { true };
bool has_called_exec { false };
int status { 0 };

0
libc/include/math.h Normal file
View File

0
libc/include/wchar.h Normal file
View File

View File

@ -144,6 +144,12 @@ extern "C"
__errno_return(rc, int);
}
int setpgid(pid_t pid, pid_t pgid)
{
long rc = syscall(SYS_setpgid, pid, pgid);
__errno_return(rc, int);
}
int chown(const char* path, uid_t uid, gid_t gid)
{
long rc = syscall(SYS_fchownat, AT_FDCWD, path, uid, gid, 0);

View File

@ -6,7 +6,7 @@
_e(getgid) _e(getegid) _e(setuid) _e(setgid) _e(seteuid) _e(setegid) _e(fchmodat) _e(fchownat) _e(ioctl) \
_e(fstatat) _e(chdir) _e(getcwd) _e(unlinkat) _e(uname) _e(sethostname) _e(dup2) _e(pipe) _e(mount) \
_e(umount) _e(pstat) _e(getrusage) _e(symlinkat) _e(readlinkat) _e(umask) _e(linkat) _e(faccessat) \
_e(pivot_root) _e(sigreturn) _e(sigaction) _e(kill) _e(sigprocmask)
_e(pivot_root) _e(sigreturn) _e(sigaction) _e(kill) _e(sigprocmask) _e(setpgid)
enum Syscalls
{