From 99e5c8e046dfff53c331eb35394bfbcab4629fd0 Mon Sep 17 00:00:00 2001
From: Anton Kling <anton@kling.gg>
Date: Fri, 17 Nov 2023 21:24:33 +0100
Subject: Kernel: Add isatty syscall

---
 kernel/Makefile            |  4 ++--
 kernel/cpu/syscall.c       |  4 ++--
 kernel/drivers/pst.c       |  9 ++++++---
 kernel/fs/shm.c            |  2 +-
 kernel/fs/tmpfs.c          |  6 +++---
 kernel/fs/vfs.c            |  7 +++++--
 kernel/fs/vfs.h            |  4 +++-
 kernel/includes/syscalls.h |  1 +
 kernel/socket.c            |  4 ++--
 kernel/syscalls/isatty.c   | 12 ++++++++++++
 10 files changed, 37 insertions(+), 16 deletions(-)
 create mode 100644 kernel/syscalls/isatty.c

(limited to 'kernel')

diff --git a/kernel/Makefile b/kernel/Makefile
index 2497d0a..ce4400e 100644
--- a/kernel/Makefile
+++ b/kernel/Makefile
@@ -1,7 +1,7 @@
 CC="i686-sb-gcc"
 AS="i686-sb-as"
-OBJ = arch/i386/boot.o init/kernel.o cpu/gdt.o cpu/reload_gdt.o cpu/idt.o cpu/io.o libc/stdio/print.o drivers/keyboard.o log.o drivers/pit.o libc/string/memcpy.o libc/string/strlen.o libc/string/memcmp.o drivers/ata.o libc/string/memset.o cpu/syscall.o  read_eip.o libc/exit/assert.o process.o cpu/int_syscall.o libc/string/strcpy.o arch/i386/mmu.o kmalloc.o fs/ext2.o fs/vfs.o fs/devfs.o cpu/spinlock.o random.o libc/string/strcmp.o crypto/ChaCha20/chacha20.o crypto/SHA1/sha1.o fs/tmpfs.o libc/string/isequal.o drivers/pst.o halts.o syscalls/ppoll.o syscalls/ftruncate.o kubsan.o syscalls/mmap.o drivers/serial.o syscalls/accept.o syscalls/bind.o syscalls/socket.o socket.o poll.o fs/fifo.o hashmap/hashmap.o fs/shm.o syscalls/shm.o elf.o ksbrk.o sched/scheduler.o syscalls/stat.o libc/string/copy.o libc/string/strncpy.o drivers/mouse.o libc/string/strlcpy.o libc/string/strcat.o drivers/vbe.o syscalls/msleep.o syscalls/uptime.o syscalls/mkdir.o drivers/pci.o drivers/rtl8139.o network/ethernet.o network/arp.o network/bytes.o network/ipv4.o network/udp.o syscalls/recvfrom.o math.o syscalls/sendto.o signal.o syscalls/kill.o syscalls/sigaction.o network/tcp.o drivers/ahci.o crypto/xoshiro256plusplus/xoshiro256plusplus.o syscalls/chdir.o syscalls/getcwd.o
-CFLAGS = -O0 -fsanitize=vla-bound,shift-exponent,pointer-overflow,shift,signed-integer-overflow,bounds -ggdb -ffreestanding -Wall -Werror -mgeneral-regs-only -Wimplicit-fallthrough -I./libc/include/ -I. -Wno-pointer-sign
+OBJ = arch/i386/boot.o init/kernel.o cpu/gdt.o cpu/reload_gdt.o cpu/idt.o cpu/io.o libc/stdio/print.o drivers/keyboard.o log.o drivers/pit.o libc/string/memcpy.o libc/string/strlen.o libc/string/memcmp.o drivers/ata.o libc/string/memset.o cpu/syscall.o  read_eip.o libc/exit/assert.o process.o cpu/int_syscall.o libc/string/strcpy.o arch/i386/mmu.o kmalloc.o fs/ext2.o fs/vfs.o fs/devfs.o cpu/spinlock.o random.o libc/string/strcmp.o crypto/ChaCha20/chacha20.o crypto/SHA1/sha1.o fs/tmpfs.o libc/string/isequal.o drivers/pst.o halts.o syscalls/ppoll.o syscalls/ftruncate.o kubsan.o syscalls/mmap.o drivers/serial.o syscalls/accept.o syscalls/bind.o syscalls/socket.o socket.o poll.o fs/fifo.o hashmap/hashmap.o fs/shm.o syscalls/shm.o elf.o ksbrk.o sched/scheduler.o syscalls/stat.o libc/string/copy.o libc/string/strncpy.o drivers/mouse.o libc/string/strlcpy.o libc/string/strcat.o drivers/vbe.o syscalls/msleep.o syscalls/uptime.o syscalls/mkdir.o drivers/pci.o drivers/rtl8139.o network/ethernet.o network/arp.o network/bytes.o network/ipv4.o network/udp.o syscalls/recvfrom.o math.o syscalls/sendto.o signal.o syscalls/kill.o syscalls/sigaction.o network/tcp.o drivers/ahci.o crypto/xoshiro256plusplus/xoshiro256plusplus.o syscalls/chdir.o syscalls/getcwd.o syscalls/isatty.o
+CFLAGS = -Ofast -fsanitize=vla-bound,shift-exponent,pointer-overflow,shift,signed-integer-overflow,bounds -ggdb -ffreestanding -Wall -Werror -mgeneral-regs-only -Wimplicit-fallthrough -I./libc/include/ -I. -Wno-pointer-sign
 INCLUDE=-I./includes/ -I./libc/include/
 
 all: myos.iso
diff --git a/kernel/cpu/syscall.c b/kernel/cpu/syscall.c
index a1e39ac..7001d63 100644
--- a/kernel/cpu/syscall.c
+++ b/kernel/cpu/syscall.c
@@ -7,8 +7,8 @@
 #include <fs/tmpfs.h>
 #include <fs/vfs.h>
 #include <kmalloc.h>
-#include <syscalls.h>
 #include <string.h>
+#include <syscalls.h>
 #include <typedefs.h>
 
 #pragma GCC diagnostic ignored "-Wpedantic"
@@ -154,7 +154,7 @@ void (*syscall_functions[])() = {
     (void(*))syscall_mkdir,     (void(*))syscall_recvfrom,
     (void(*))syscall_sendto,    (void(*))syscall_kill,
     (void(*))syscall_sigaction, (void(*))syscall_chdir,
-    (void(*))syscall_getcwd,
+    (void(*))syscall_getcwd,    (void(*))syscall_isatty,
 };
 
 void syscall_function_handler(u32 eax, u32 arg1, u32 arg2, u32 arg3, u32 arg4,
diff --git a/kernel/drivers/pst.c b/kernel/drivers/pst.c
index d063ec9..f8a0414 100644
--- a/kernel/drivers/pst.c
+++ b/kernel/drivers/pst.c
@@ -1,16 +1,19 @@
 #include <drivers/pst.h>
 #include <fs/tmpfs.h>
+#include <fs/vfs.h>
 
 int openpty(int *amaster, int *aslave, char *name,
             /*const struct termios*/ void *termp,
             /*const struct winsize*/ void *winp) {
-	(void)name;
-	(void)termp;
-	(void) winp;
+  (void)name;
+  (void)termp;
+  (void)winp;
   int fd[2];
   pipe(fd); // This depends upon that pipe will support read and write
             // through the same fd. In reality this should not be the
             // case.
+  get_vfs_fd(fd[0])->is_tty = 1;
+  get_vfs_fd(fd[1])->is_tty = 1;
   *amaster = fd[0];
   *aslave = fd[1];
   return 0;
diff --git a/kernel/fs/shm.c b/kernel/fs/shm.c
index a77702a..2224cc2 100644
--- a/kernel/fs/shm.c
+++ b/kernel/fs/shm.c
@@ -81,7 +81,7 @@ int shm_open(const char *name, int oflag, mode_t mode) {
       NULL /*stat*/);
 
   vfs_fd_t *fd_ptr;
-  int fd = vfs_create_fd(oflag, mode, inode, &fd_ptr);
+  int fd = vfs_create_fd(oflag, mode, 0/*is_tty*/, inode, &fd_ptr);
   if (-1 == fd) {
     kfree(inode);
     return -EMFILE;
diff --git a/kernel/fs/tmpfs.c b/kernel/fs/tmpfs.c
index 0e82a16..d213005 100644
--- a/kernel/fs/tmpfs.c
+++ b/kernel/fs/tmpfs.c
@@ -52,7 +52,7 @@ void dual_pipe(int fd[2]) {
     assert(inode);
 
     vfs_fd_t *fd_ptr;
-    fd[i] = vfs_create_fd(O_RDWR | O_NONBLOCK, 0, inode, &fd_ptr);
+    fd[i] = vfs_create_fd(O_RDWR | O_NONBLOCK, 0, 0/*is_tty*/, inode, &fd_ptr);
     assert(-1 != fd[i]);
   }
   vfs_inode_t *f_inode = get_current_task()->file_descriptors[fd[0]]->inode;
@@ -78,11 +78,11 @@ void pipe(int fd[2]) {
         0 /*inode_num*/, 0 /*type*/, has_data, can_write, is_open,
         internal_object, 0 /*file_size*/, NULL /*open*/, NULL /*create_file*/,
         tmp_read, tmp_write, tmp_close, NULL /*create_directory*/,
-        NULL /*get_vm_object*/, NULL /*truncate*/, NULL/*stat*/);
+        NULL /*get_vm_object*/, NULL /*truncate*/, NULL /*stat*/);
     assert(inode);
 
     vfs_fd_t *fd_ptr;
-    fd[i] = vfs_create_fd(O_RDWR, 0, inode, &fd_ptr);
+    fd[i] = vfs_create_fd(O_RDWR, 0, 0 /*is_tty*/, inode, &fd_ptr);
     assert(-1 != fd[i]);
   }
   vfs_inode_t *f_inode = get_current_task()->file_descriptors[fd[0]]->inode;
diff --git a/kernel/fs/vfs.c b/kernel/fs/vfs.c
index d0c1b95..d55a2bf 100644
--- a/kernel/fs/vfs.c
+++ b/kernel/fs/vfs.c
@@ -54,7 +54,8 @@ vfs_inode_t *vfs_create_inode(
   return r;
 }
 
-int vfs_create_fd(int flags, int mode, vfs_inode_t *inode, vfs_fd_t **fd) {
+int vfs_create_fd(int flags, int mode, int is_tty, vfs_inode_t *inode,
+                  vfs_fd_t **fd) {
   process_t *p = (process_t *)get_current_task();
   int i;
   for (i = 0; i < 100; i++)
@@ -67,6 +68,7 @@ int vfs_create_fd(int flags, int mode, vfs_inode_t *inode, vfs_fd_t **fd) {
   r->mode = mode;
   r->inode = inode;
   r->reference_count = 1;
+  r->is_tty = is_tty;
   r->offset = 0;
   p->file_descriptors[i] = r;
   if (fd)
@@ -266,7 +268,8 @@ int vfs_open(const char *file, int flags, int mode) {
     return uds_open(resolved_path);
   }
 
-  return vfs_create_fd(flags, mode, inode, NULL);
+  // FIXME: Maybe it is sometimes a TTY?
+  return vfs_create_fd(flags, mode, 0 /*is_tty*/, inode, NULL);
 }
 
 int vfs_close(int fd) {
diff --git a/kernel/fs/vfs.h b/kernel/fs/vfs.h
index b6a3401..76c0a0c 100644
--- a/kernel/fs/vfs.h
+++ b/kernel/fs/vfs.h
@@ -47,6 +47,7 @@ struct vfs_fd {
   size_t offset;
   int flags;
   int mode;
+  int is_tty;
   int reference_count; // Number of usages of this file descriptor,
                        // once it reaches zero then the contents can
                        // be freed.
@@ -84,7 +85,8 @@ vfs_vm_object_t *vfs_get_vm_object(int fd, u64 length, u64 offset);
 int vfs_dup2(int org_fd, int new_fd);
 vfs_inode_t *vfs_internal_open(const char *file);
 int vfs_mkdir(const char *path, int mode);
-int vfs_create_fd(int flags, int mode, vfs_inode_t *inode, vfs_fd_t **fd);
+int vfs_create_fd(int flags, int mode, int is_tty, vfs_inode_t *inode,
+                  vfs_fd_t **fd);
 int vfs_ftruncate(int fd, size_t length);
 int vfs_chdir(const char *path);
 int vfs_fstat(int fd, struct stat *buf);
diff --git a/kernel/includes/syscalls.h b/kernel/includes/syscalls.h
index 7ee0105..6642fea 100644
--- a/kernel/includes/syscalls.h
+++ b/kernel/includes/syscalls.h
@@ -107,3 +107,4 @@ typedef struct SYS_STAT_PARAMS {
 
 int syscall_stat(SYS_STAT_PARAMS *args);
 u32 syscall_uptime(void);
+int syscall_isatty(int fd);
diff --git a/kernel/socket.c b/kernel/socket.c
index ebc83a9..1922a93 100644
--- a/kernel/socket.c
+++ b/kernel/socket.c
@@ -90,7 +90,7 @@ handle_incoming_tcp_connection(u8 ip[4], u16 n_port,
       NULL /*stat*/);
 
   vfs_fd_t *fd;
-  int n = vfs_create_fd(O_RDWR | O_NONBLOCK, 0, inode, &fd);
+  int n = vfs_create_fd(O_RDWR | O_NONBLOCK, 0, 0 /*is_tty*/, inode, &fd);
 
   fd->reference_count++;
   s->incoming_fd = fd;
@@ -282,7 +282,7 @@ int socket(int domain, int type, int protocol) {
       NULL /*truncate*/, NULL /*stat*/);
 
   vfs_fd_t *fd;
-  int n = vfs_create_fd(O_RDWR | O_NONBLOCK, 0, inode, &fd);
+  int n = vfs_create_fd(O_RDWR | O_NONBLOCK, 0, 0/*is_tty*/, inode, &fd);
 
   new_socket->domain = domain;
   new_socket->type = type;
diff --git a/kernel/syscalls/isatty.c b/kernel/syscalls/isatty.c
new file mode 100644
index 0000000..64649e2
--- /dev/null
+++ b/kernel/syscalls/isatty.c
@@ -0,0 +1,12 @@
+#include <fs/vfs.h>
+#include <syscalls.h>
+#include <errno.h>
+
+int syscall_isatty(int fd) {
+  vfs_fd_t *fd_ptr = get_vfs_fd(fd);
+  if (!fd_ptr)
+    return -EBADF;
+  if (!fd_ptr->is_tty)
+    return -ENOTTY;
+  return 1;
+}
-- 
cgit v1.2.3