Add ioctl() and reimplement tcsetpgrp/tcsetpgrp as ioctls.

This commit is contained in:
Andreas Kling 2018-11-16 13:11:21 +01:00
parent 2529925fe9
commit c99f8af66d
Notes: sideshowbarker 2024-07-19 16:10:25 +09:00
16 changed files with 94 additions and 45 deletions

View File

@ -1586,35 +1586,14 @@ int Process::sys$tcsetattr(int fd, int optional_actions, const Unix::termios* tp
return 0; return 0;
} }
pid_t Process::sys$tcgetpgrp(int fd) int Process::sys$ioctl(int fd, unsigned request, unsigned arg)
{ {
auto* descriptor = file_descriptor(fd); auto* descriptor = file_descriptor(fd);
if (!descriptor) if (!descriptor)
return -EBADF; return -EBADF;
if (!descriptor->isTTY()) if (!descriptor->is_character_device())
return -ENOTTY; return -ENOTTY;
auto& tty = *descriptor->tty(); return descriptor->character_device()->ioctl(*this, request, arg);
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* descriptor = file_descriptor(fd);
if (!descriptor)
return -EBADF;
if (!descriptor->isTTY())
return -ENOTTY;
auto& tty = *descriptor->tty();
if (&tty != m_tty)
return -ENOTTY;
tty.set_pgid(pgid);
return 0;
} }
int Process::sys$getdtablesize() int Process::sys$getdtablesize()

View File

@ -118,8 +118,6 @@ 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();
uid_t sys$geteuid(); uid_t sys$geteuid();
@ -175,6 +173,7 @@ public:
int sys$fcntl(int fd, int cmd, dword extra_arg); int sys$fcntl(int fd, int cmd, dword extra_arg);
int sys$tcgetattr(int fd, Unix::termios*); int sys$tcgetattr(int fd, Unix::termios*);
int sys$tcsetattr(int fd, int optional_actions, const Unix::termios*); int sys$tcsetattr(int fd, int optional_actions, const Unix::termios*);
int sys$ioctl(int fd, unsigned request, unsigned arg);
static void initialize(); static void initialize();

View File

@ -125,10 +125,6 @@ static DWORD handle(RegisterDump& regs, DWORD function, DWORD arg1, DWORD arg2,
return current->sys$getpgid((pid_t)arg1); return current->sys$getpgid((pid_t)arg1);
case Syscall::SC_getpgrp: case Syscall::SC_getpgrp:
return current->sys$getpgrp(); return current->sys$getpgrp();
case Syscall::SC_tcgetpgrp:
return current->sys$tcgetpgrp((int)arg1);
case Syscall::SC_tcsetpgrp:
return current->sys$tcsetpgrp((int)arg1, (pid_t)arg2);
case Syscall::SC_fork: case Syscall::SC_fork:
return current->sys$fork(regs); return current->sys$fork(regs);
case Syscall::SC_execve: case Syscall::SC_execve:
@ -179,6 +175,8 @@ static DWORD handle(RegisterDump& regs, DWORD function, DWORD arg1, DWORD arg2,
return current->sys$tcgetattr((int)arg1, (Unix::termios*)arg2); return current->sys$tcgetattr((int)arg1, (Unix::termios*)arg2);
case Syscall::SC_tcsetattr: case Syscall::SC_tcsetattr:
return current->sys$tcsetattr((int)arg1, (int)arg2, (const Unix::termios*)arg3); return current->sys$tcsetattr((int)arg1, (int)arg2, (const Unix::termios*)arg3);
case Syscall::SC_ioctl:
return current->sys$ioctl((int)arg1, (unsigned)arg2, (unsigned)arg3);
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

@ -37,8 +37,6 @@
__ENUMERATE_SYSCALL(getpgid) \ __ENUMERATE_SYSCALL(getpgid) \
__ENUMERATE_SYSCALL(setpgid) \ __ENUMERATE_SYSCALL(setpgid) \
__ENUMERATE_SYSCALL(getpgrp) \ __ENUMERATE_SYSCALL(getpgrp) \
__ENUMERATE_SYSCALL(tcsetpgrp) \
__ENUMERATE_SYSCALL(tcgetpgrp) \
__ENUMERATE_SYSCALL(fork) \ __ENUMERATE_SYSCALL(fork) \
__ENUMERATE_SYSCALL(execve) \ __ENUMERATE_SYSCALL(execve) \
__ENUMERATE_SYSCALL(geteuid) \ __ENUMERATE_SYSCALL(geteuid) \
@ -66,6 +64,7 @@
__ENUMERATE_SYSCALL(fcntl) \ __ENUMERATE_SYSCALL(fcntl) \
__ENUMERATE_SYSCALL(tcsetattr) \ __ENUMERATE_SYSCALL(tcsetattr) \
__ENUMERATE_SYSCALL(tcgetattr) \ __ENUMERATE_SYSCALL(tcgetattr) \
__ENUMERATE_SYSCALL(ioctl) \
#define DO_SYSCALL_A0(function) Syscall::invoke((dword)(function)) #define DO_SYSCALL_A0(function) Syscall::invoke((dword)(function))
@ -104,28 +103,28 @@ struct SC_mmap_params {
void initialize(); void initialize();
inline dword invoke(dword function) inline dword invoke(Function function)
{ {
dword result; dword result;
asm volatile("int $0x80":"=a"(result):"a"(function):"memory"); asm volatile("int $0x80":"=a"(result):"a"(function):"memory");
return result; return result;
} }
inline dword invoke(dword function, dword arg1) inline dword invoke(Function function, dword arg1)
{ {
dword result; dword result;
asm volatile("int $0x80":"=a"(result):"a"(function),"d"(arg1):"memory"); asm volatile("int $0x80":"=a"(result):"a"(function),"d"(arg1):"memory");
return result; return result;
} }
inline dword invoke(dword function, dword arg1, dword arg2) inline dword invoke(Function function, dword arg1, dword arg2)
{ {
dword result; dword result;
asm volatile("int $0x80":"=a"(result):"a"(function),"d"(arg1),"c"(arg2):"memory"); asm volatile("int $0x80":"=a"(result):"a"(function),"d"(arg1),"c"(arg2):"memory");
return result; return result;
} }
inline dword invoke(dword function, dword arg1, dword arg2, dword arg3) inline dword invoke(Function function, dword arg1, dword arg2, dword arg3)
{ {
dword result; dword result;
asm volatile("int $0x80":"=a"(result):"a"(function),"d"(arg1),"c"(arg2),"b"(arg3):"memory"); asm volatile("int $0x80":"=a"(result):"a"(function),"d"(arg1),"c"(arg2),"b"(arg3):"memory");

View File

@ -1,6 +1,8 @@
#include "TTY.h" #include "TTY.h"
#include "Process.h" #include "Process.h"
#include <LibC/errno_numbers.h>
#include <LibC/signal_numbers.h> #include <LibC/signal_numbers.h>
#include <LibC/sys/ioctl_numbers.h>
TTY::TTY(unsigned major, unsigned minor) TTY::TTY(unsigned major, unsigned minor)
: CharacterDevice(major, minor) : CharacterDevice(major, minor)
@ -66,3 +68,22 @@ void TTY::set_termios(const Unix::termios& t)
should_echo_input(), should_echo_input(),
should_generate_signals()); should_generate_signals());
} }
int TTY::ioctl(Process& process, unsigned request, unsigned arg)
{
if (process.tty() != this)
return -ENOTTY;
switch (request) {
case TIOCGPGRP:
return pgid();
case TIOCSPGRP: {
// FIXME: Validate pgid fully.
pid_t pgid = static_cast<pid_t>(arg);
if (pgid < 0)
return -EINVAL;
set_pgid(arg);
return 0;
}
}
return -EINVAL;
}

View File

@ -3,6 +3,8 @@
#include <VirtualFileSystem/CharacterDevice.h> #include <VirtualFileSystem/CharacterDevice.h>
#include <VirtualFileSystem/UnixTypes.h> #include <VirtualFileSystem/UnixTypes.h>
class Process;
class TTY : public CharacterDevice { class TTY : public CharacterDevice {
public: public:
virtual ~TTY() override; virtual ~TTY() override;
@ -10,6 +12,7 @@ public:
virtual ssize_t read(byte*, size_t) override; virtual ssize_t read(byte*, size_t) override;
virtual ssize_t write(const byte*, size_t) override; virtual ssize_t write(const byte*, size_t) override;
virtual bool hasDataAvailableForRead() const override; virtual bool hasDataAvailableForRead() const override;
virtual int ioctl(Process&, unsigned request, unsigned arg) override final;
virtual String ttyName() const = 0; virtual String ttyName() const = 0;

View File

@ -30,6 +30,7 @@ LIBC_OBJS = \
termios.o \ termios.o \
ulimit.o \ ulimit.o \
qsort.o \ qsort.o \
ioctl.o \
entry.o entry.o
OBJS = $(AK_OBJS) $(LIBC_OBJS) OBJS = $(AK_OBJS) $(LIBC_OBJS)

19
LibC/ioctl.cpp Normal file
View File

@ -0,0 +1,19 @@
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <Kernel/Syscall.h>
extern "C" {
int ioctl(int fd, unsigned request, ...)
{
va_list ap;
va_start(ap, request);
unsigned arg = va_arg(ap, unsigned);
int rc = Syscall::invoke(Syscall::SC_ioctl, (dword)fd, (dword)request, (dword)arg);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
}

View File

@ -4,11 +4,11 @@
__BEGIN_DECLS __BEGIN_DECLS
typedef char* va_list; typedef __builtin_va_list va_list;
#define va_start(ap, v) ap = (va_list)&v + sizeof(v) #define va_start(v, l) __builtin_va_start(v, l)
#define va_arg(ap, t) ((t*)(ap += sizeof(t)))[-1] #define va_end(v) __builtin_va_end(v)
#define va_end(ap) ap = nullptr #define va_arg(v, l) __builtin_va_arg(v, l)
__END_DECLS __END_DECLS

View File

@ -0,0 +1,10 @@
#pragma once
#include <sys/cdefs.h>
#include <sys/ioctl_numbers.h>
__BEGIN_DECLS
int ioctl(int fd, unsigned request, ...);
__END_DECLS

7
LibC/sys/ioctl_numbers.h Normal file
View File

@ -0,0 +1,7 @@
#pragma once
enum IOCtlNumber {
TIOCGPGRP,
TIOCSPGRP,
};

View File

@ -108,6 +108,7 @@ char* tgoto(const char* cap, int col, int row)
int tputs(const char* str, int affcnt, int (*putc)(int)) int tputs(const char* str, int affcnt, int (*putc)(int))
{ {
printf("%s", str); printf("%s", str);
return 0;
} }
} }

View File

@ -6,6 +6,7 @@
#include <grp.h> #include <grp.h>
#include <pwd.h> #include <pwd.h>
#include <stdio.h> #include <stdio.h>
#include <sys/ioctl.h>
#include <Kernel/Syscall.h> #include <Kernel/Syscall.h>
extern "C" { extern "C" {
@ -60,14 +61,12 @@ pid_t setsid()
pid_t tcgetpgrp(int fd) pid_t tcgetpgrp(int fd)
{ {
int rc = Syscall::invoke(Syscall::SC_tcgetpgrp, (dword)fd); return ioctl(fd, TIOCGPGRP);
__RETURN_WITH_ERRNO(rc, rc, -1);
} }
int tcsetpgrp(int fd, pid_t pgid) int tcsetpgrp(int fd, pid_t pgid)
{ {
int rc = Syscall::invoke(Syscall::SC_tcsetpgrp, (dword)fd, (dword)pgid); return ioctl(fd, TIOCSPGRP, pgid);
__RETURN_WITH_ERRNO(rc, rc, -1);
} }
int setpgid(pid_t pid, pid_t pgid) int setpgid(pid_t pid, pid_t pgid)

View File

@ -1,4 +1,5 @@
#include "CharacterDevice.h" #include "CharacterDevice.h"
#include <LibC/errno_numbers.h>
CharacterDevice::~CharacterDevice() CharacterDevice::~CharacterDevice()
{ {
@ -8,3 +9,8 @@ RetainPtr<FileDescriptor> CharacterDevice::open(int options)
{ {
return VFS::the().open(*this, options); return VFS::the().open(*this, options);
} }
int CharacterDevice::ioctl(Process&, unsigned, unsigned)
{
return -ENOTTY;
}

View File

@ -4,6 +4,8 @@
#include "Limits.h" #include "Limits.h"
#include "FileDescriptor.h" #include "FileDescriptor.h"
class Process;
class CharacterDevice { class CharacterDevice {
public: public:
virtual ~CharacterDevice(); virtual ~CharacterDevice();
@ -20,6 +22,8 @@ public:
virtual bool isTTY() const { return false; } virtual bool isTTY() const { return false; }
virtual int ioctl(Process&, unsigned request, unsigned arg);
protected: protected:
CharacterDevice(unsigned major, unsigned minor) : m_major(major), m_minor(minor) { } CharacterDevice(unsigned major, unsigned minor) : m_major(major), m_minor(minor) { }

View File

@ -38,6 +38,9 @@ public:
bool isDirectory() const; bool isDirectory() const;
bool is_character_device() const { return m_vnode && m_vnode->isCharacterDevice(); }
CharacterDevice* character_device() { return m_vnode ? m_vnode->characterDevice() : nullptr; }
#ifdef SERENITY #ifdef SERENITY
bool isTTY() const; bool isTTY() const;
const TTY* tty() const; const TTY* tty() const;