1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 22:07:34 +00:00

Add tcsetpgrp()+tcgetpgrp().

One more step on the path to being able to ^C a runaway process. :^)
This commit is contained in:
Andreas Kling 2018-11-02 13:14:25 +01:00
parent d8f0dd6f3b
commit 621217ffeb
11 changed files with 72 additions and 4 deletions

View file

@ -277,10 +277,11 @@ ByteBuffer procfs$summary()
auto processes = Process::allProcesses(); auto processes = Process::allProcesses();
auto buffer = ByteBuffer::createUninitialized(processes.size() * 256); auto buffer = ByteBuffer::createUninitialized(processes.size() * 256);
char* ptr = (char*)buffer.pointer(); char* ptr = (char*)buffer.pointer();
ptr += ksprintf(ptr, "PID PGP SID OWNER STATE PPID NSCHED FDS TTY NAME\n"); ptr += ksprintf(ptr, "PID TPG PGP SID OWNER STATE PPID NSCHED FDS TTY NAME\n");
for (auto* process : processes) { for (auto* process : processes) {
ptr += ksprintf(ptr, "% 3u % 3u % 3u % 4u % 8s % 3u % 9u % 3u % 4s %s\n", ptr += ksprintf(ptr, "% 3u % 3u % 3u % 3u % 4u % 8s % 3u % 9u % 3u % 4s %s\n",
process->pid(), process->pid(),
process->tty() ? process->tty()->pgid() : 0,
process->pgid(), process->pgid(),
process->sid(), process->sid(),
process->uid(), process->uid(),

View file

@ -1249,3 +1249,34 @@ int Process::sys$setpgid(pid_t specified_pid, pid_t specified_pgid)
process->m_pgid = new_pgid; process->m_pgid = new_pgid;
return 0; return 0;
} }
pid_t Process::sys$tcgetpgrp(int fd)
{
auto* handle = fileHandleIfExists(fd);
if (!handle)
return -EBADF;
if (!handle->isTTY())
return -ENOTTY;
auto& tty = *handle->tty();
if (&tty != m_tty)
return -ENOTTY;
return tty.pgid();
}
int Process::sys$tcsetpgrp(int fd, pid_t pgid)
{
if (pgid < 0)
return -EINVAL;
if (get_sid_from_pgid(pgid) != m_sid)
return -EINVAL;
auto* handle = fileHandleIfExists(fd);
if (!handle)
return -EBADF;
if (!handle->isTTY())
return -ENOTTY;
auto& tty = *handle->tty();
if (&tty != m_tty)
return -ENOTTY;
tty.set_pgid(pgid);
return 0;
}

View file

@ -86,6 +86,8 @@ public:
int sys$setpgid(pid_t pid, pid_t pgid); int sys$setpgid(pid_t pid, pid_t pgid);
pid_t sys$getpgrp(); pid_t sys$getpgrp();
pid_t sys$getpgid(pid_t); pid_t sys$getpgid(pid_t);
pid_t sys$tcgetpgrp(int fd);
int sys$tcsetpgrp(int fd, pid_t pgid);
uid_t sys$getuid(); uid_t sys$getuid();
gid_t sys$getgid(); gid_t sys$getgid();
pid_t sys$getpid(); pid_t sys$getpid();

View file

@ -124,6 +124,10 @@ DWORD handle(DWORD function, DWORD arg1, DWORD arg2, DWORD arg3)
return current->sys$getpgid((pid_t)arg1); return current->sys$getpgid((pid_t)arg1);
case Syscall::PosixGetpgrp: case Syscall::PosixGetpgrp:
return current->sys$getpgrp(); return current->sys$getpgrp();
case Syscall::PosixTcgetpgrp:
return current->sys$tcgetpgrp((int)arg1);
case Syscall::PosixTcsetpgrp:
return current->sys$tcsetpgrp((int)arg1, (pid_t)arg2);
default: default:
kprintf("<%u> int0x80: Unknown function %x requested {%x, %x, %x}\n", current->pid(), function, arg1, arg2, arg3); kprintf("<%u> int0x80: Unknown function %x requested {%x, %x, %x}\n", current->pid(), function, arg1, arg2, arg3);
break; break;

View file

@ -45,6 +45,8 @@ enum Function {
PosixGetpgid = 0x2013, PosixGetpgid = 0x2013,
PosixSetpgid = 0x2014, PosixSetpgid = 0x2014,
PosixGetpgrp = 0x2015, PosixGetpgrp = 0x2015,
PosixTcsetpgrp = 0x2016,
PosixTcgetpgrp = 0x2017,
}; };
void initialize(); void initialize();

View file

@ -12,6 +12,9 @@ public:
virtual String ttyName() const = 0; virtual String ttyName() const = 0;
void set_pgid(pid_t pgid) { m_pgid = pgid; }
pid_t pgid() const { return m_pgid; }
protected: protected:
virtual bool isTTY() const final { return true; } virtual bool isTTY() const final { return true; }
@ -22,5 +25,6 @@ protected:
private: private:
Vector<byte> m_buffer; Vector<byte> m_buffer;
pid_t m_pgid { 0 };
}; };

View file

@ -25,9 +25,14 @@ pid_t setsid()
return Syscall::invoke(Syscall::PosixSetsid); return Syscall::invoke(Syscall::PosixSetsid);
} }
pid_t sys$getsid(pid_t pid) pid_t tcgetpgrp(int fd)
{ {
return Syscall::invoke(Syscall::PosixSetsid, (dword)pid); return Syscall::invoke(Syscall::PosixTcgetpgrp, (dword)fd);
}
int tcsetpgrp(int fd, pid_t pgid)
{
return Syscall::invoke(Syscall::PosixTcsetpgrp, (dword)fd, (dword)pgid);
} }
int setpgid(pid_t pid, pid_t pgid) int setpgid(pid_t pid, pid_t pgid)

View file

@ -16,6 +16,8 @@ pid_t getpgrp();
uid_t getuid(); uid_t getuid();
gid_t getgid(); gid_t getgid();
pid_t getpid(); pid_t getpid();
pid_t tcgetpgrp(int fd);
int tcsetpgrp(int fd, pid_t pgid);
int open(const char* path, int options); int open(const char* path, int options);
ssize_t read(int fd, void* buf, size_t count); ssize_t read(int fd, void* buf, size_t count);
ssize_t write(int fd, const void* buf, size_t count); ssize_t write(int fd, const void* buf, size_t count);

View file

@ -142,10 +142,17 @@ static int runcmd(char* cmd)
return 1; return 1;
} }
// FIXME: This is racy, since spawn has already started the new process.
// Once I have fork()+exec(), pgrp setup can be done in the child before exec()ing.
tcsetpgrp(0, ret);
// FIXME: waitpid should give us the spawned process's exit status // FIXME: waitpid should give us the spawned process's exit status
int wstatus = 0; int wstatus = 0;
waitpid(ret, &wstatus, 0); waitpid(ret, &wstatus, 0);
// FIXME: Racy as mentioned above. Rework once we have fork()+exec().
tcsetpgrp(0, getpid());
if (WIFEXITED(wstatus)) { if (WIFEXITED(wstatus)) {
//printf("Exited normally with status %d\n", WEXITSTATUS(wstatus)); //printf("Exited normally with status %d\n", WEXITSTATUS(wstatus));
} else { } else {
@ -173,6 +180,8 @@ int main(int, char**)
{ {
g = new GlobalState; g = new GlobalState;
g->sid = setsid(); g->sid = setsid();
tcsetpgrp(0, getpgrp());
int rc = gethostname(g->hostname, sizeof(g->hostname)); int rc = gethostname(g->hostname, sizeof(g->hostname));
if (rc < 0) if (rc < 0)
perror("gethostname"); perror("gethostname");

View file

@ -176,6 +176,13 @@ const TTY* FileHandle::tty() const
return nullptr; return nullptr;
} }
TTY* FileHandle::tty()
{
if (auto* device = m_vnode->characterDevice())
return static_cast<TTY*>(device);
return nullptr;
}
int FileHandle::close() int FileHandle::close()
{ {
return 0; return 0;

View file

@ -30,6 +30,7 @@ public:
bool isTTY() const; bool isTTY() const;
const TTY* tty() const; const TTY* tty() const;
TTY* tty();
InodeMetadata metadata() const { return m_vnode->metadata(); } InodeMetadata metadata() const { return m_vnode->metadata(); }