summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Kling <anton@kling.gg>2024-04-17 17:06:16 +0200
committerAnton Kling <anton@kling.gg>2024-04-17 17:06:16 +0200
commit620d37f41747e943648544d1fd1567d386c91e04 (patch)
treebd6d616aca8a853f3b90cfd176bba4d37575e42a
parentd0cca44913356f8ce15e15216b0e26c2e74b4d06 (diff)
Kernel/LibC: Add fstat() and remove stat() syscall
-rw-r--r--include/sys/stat.h1
-rw-r--r--kernel/Makefile2
-rw-r--r--kernel/cpu/syscall.c2
-rw-r--r--kernel/includes/syscalls.h7
-rw-r--r--kernel/syscalls/fstat.c11
-rw-r--r--kernel/syscalls/stat.c15
-rw-r--r--userland/libc/Makefile2
-rw-r--r--userland/libc/include/syscall.h7
-rw-r--r--userland/libc/sys/stat.h30
-rw-r--r--userland/libc/sys/stat/fstat.c9
-rw-r--r--userland/libc/sys/stat/stat.c13
11 files changed, 34 insertions, 65 deletions
diff --git a/include/sys/stat.h b/include/sys/stat.h
index 334bc7d..9f3dfe9 100644
--- a/include/sys/stat.h
+++ b/include/sys/stat.h
@@ -35,5 +35,6 @@ struct stat {
};
#ifndef KERNEL
int stat(const char *path, struct stat *buf);
+int fstat(int fd, struct stat *buf);
#endif
#endif
diff --git a/kernel/Makefile b/kernel/Makefile
index 3f09855..8701f13 100644
--- a/kernel/Makefile
+++ b/kernel/Makefile
@@ -1,6 +1,6 @@
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 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 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 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 syscalls/randomfill.o syscalls/open.o syscalls/write.o syscalls/pwrite.o syscalls/port.o syscalls/map_frames.o syscalls/virtual_to_physical.o syscalls/install_irq.o arch/i386/interrupts.o cpu/isr.o lib/stack.o lib/buffered_write.o lib/list.o cpu/arch_inst.o cpu/int_syscall.o queue.o syscalls/queue.o syscalls/munmap.o syscalls/open_process.o
+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 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 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/fstat.o libc/string/copy.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 syscalls/randomfill.o syscalls/open.o syscalls/write.o syscalls/pwrite.o syscalls/port.o syscalls/map_frames.o syscalls/virtual_to_physical.o syscalls/install_irq.o arch/i386/interrupts.o cpu/isr.o lib/stack.o lib/buffered_write.o lib/list.o cpu/arch_inst.o cpu/int_syscall.o queue.o syscalls/queue.o syscalls/munmap.o syscalls/open_process.o
CFLAGS = -std=c99 -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 -DKERNEL
#CFLAGS = -std=c99 -Ofast -flto -ggdb -ffreestanding -Wall -Werror -mgeneral-regs-only -Wimplicit-fallthrough -I./libc/include/ -I. -Wno-pointer-sign -DKERNEL
#CFLAGS = -std=c99 -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 -DKERNEL
diff --git a/kernel/cpu/syscall.c b/kernel/cpu/syscall.c
index a33a6e2..5412915 100644
--- a/kernel/cpu/syscall.c
+++ b/kernel/cpu/syscall.c
@@ -167,7 +167,7 @@ int (*syscall_functions[])() = {
(void(*))syscall_poll, (void(*))syscall_mmap,
(void(*))syscall_accept, (void(*))syscall_bind,
(void(*))syscall_socket, (void(*))syscall_shm_open,
- (void(*))syscall_ftruncate, (void(*))syscall_stat,
+ (void(*))syscall_ftruncate, (void(*))syscall_fstat,
(void(*))syscall_msleep, (void(*))syscall_uptime,
(void(*))syscall_mkdir, (void(*))syscall_recvfrom,
(void(*))syscall_sendto, (void(*))syscall_kill,
diff --git a/kernel/includes/syscalls.h b/kernel/includes/syscalls.h
index 07a73e3..f187d4b 100644
--- a/kernel/includes/syscalls.h
+++ b/kernel/includes/syscalls.h
@@ -123,11 +123,6 @@ typedef struct SYS_SOCKET_PARAMS {
int syscall_socket(SYS_SOCKET_PARAMS *args);
-typedef struct SYS_STAT_PARAMS {
- const char *pathname;
- struct stat *statbuf;
-} __attribute__((packed)) SYS_STAT_PARAMS;
-
-int syscall_stat(SYS_STAT_PARAMS *args);
+int syscall_fstat(int fd, struct stat *buf);
u32 syscall_uptime(void);
int syscall_isatty(int fd);
diff --git a/kernel/syscalls/fstat.c b/kernel/syscalls/fstat.c
new file mode 100644
index 0000000..38346e3
--- /dev/null
+++ b/kernel/syscalls/fstat.c
@@ -0,0 +1,11 @@
+#include <errno.h>
+#include <fs/vfs.h>
+#include <syscalls.h>
+
+int syscall_fstat(int fd, struct stat *buf) {
+ if (!mmu_is_valid_userpointer(buf, sizeof(struct stat))) {
+ return -EPERM; // TODO: Is this correct? The spec says nothing about
+ // this case.
+ }
+ return vfs_fstat(fd, buf);
+}
diff --git a/kernel/syscalls/stat.c b/kernel/syscalls/stat.c
deleted file mode 100644
index 83ad3f0..0000000
--- a/kernel/syscalls/stat.c
+++ /dev/null
@@ -1,15 +0,0 @@
-#include <errno.h>
-#include <fs/vfs.h>
-#include <syscalls.h>
-
-int syscall_stat(SYS_STAT_PARAMS *args) {
- const char *pathname = copy_and_allocate_user_string(args->pathname);
- struct stat *statbuf = args->statbuf;
- int fd = vfs_open(pathname, O_READ, 0);
- if (0 > fd) {
- return -ENOENT;
- }
- int rc = vfs_fstat(fd, statbuf);
- vfs_close(fd);
- return rc;
-}
diff --git a/userland/libc/Makefile b/userland/libc/Makefile
index af5fa59..b7e14a7 100644
--- a/userland/libc/Makefile
+++ b/userland/libc/Makefile
@@ -3,7 +3,7 @@ AR="i686-sb-ar"
AS="i686-sb-as"
#CFLAGS = -ggdb -ffreestanding -O2 -Wall -Wextra -pedantic -Wimplicit-fallthrough -I./include/ -static -I../../include/ -Wno-int-conversion -Wno-unused-parameter -Wno-return-type
CFLAGS = -ggdb -ffreestanding -Wall -Wextra -pedantic -Wimplicit-fallthrough -I./include/ -static -I../../include/ -Wno-int-conversion -Wno-unused-parameter -Wno-return-type
-OBJ=crt0.o libc.o malloc/malloc.o pty.o sys/mman/mmap.o sys/mman/munmap.o memset.o assert.o stdio/snprintf.o stdio/vfprintf.o string/memcpy.o string/memcmp.o string/strcmp.o ubsan.o string/strcpy.o isspace.o stdio/puts.o stdio/putchar.o dirent/opendir.o dirent/readdir.o dirent/closedir.o unistd/getopt.o dirent/scandir.o dirent/alphasort.o stdio/printf.o stdio/vdprintf.o stdio/vprintf.o stdio/dprintf.o stdio/vprintf.o string/strlen.o string/strnlen.o stdio/stdin.o stdio/getchar.o stdio/fgetc.o arpa/inet/htons.o arpa/inet/htonl.o stdio/fread.o stdio/fwrite.o stdio/fopen.o stdio/fclose.o stdio/fseek.o ctype/isascii.o stdio/fprintf.o stdlib/atoi.o stdlib/strtol.o ctype/toupper.o ctype/tolower.o string/strcat.o string/strchr.o string/sscanf.o sys/stat/stat.o stdlib/getenv.o string/strrchr.o stdio/ftell.o stdio/tmpfile.o stdio/fgets.o stdio/feof.o stdio/fscanf.o stdio/ungetc.o string/strncmp.o stdio/fputc.o string/strncpy.o stdio/remove.o stdio/ferror.o stdio/fputs.o stdlib/rand.o stdlib/srand.o unistd/getpid.o stdlib/strtoul.o stdio/fflush.o stdlib/abort.o string/strcspn.o time/localtime.o time/time.o time/clock_gettime.o time/gmtime.o time/strftime.o string/strpbrk.o ctype/isdigit.o ctype/isalpha.o ctype/isxdigit.o ctype/ispunct.o stdio/setvbuf.o stdio/fileno.o stdio/putc.o stdio/sprintf.o stdlib/abs.o string/strspn.o stdlib/qsort.o string/memmove.o setjmp/longjmp.o setjmp/setjmp.o libgen/basename.o string/strdup.o string/strndup.o string/strlcpy.o stdlib/atexit.o stdio/open_memstream.o libgen/dirname.o unistd/unlink.o string/strstr.o string/strcasecmp.o string/strncasecmp.o stdlib/mkstemp.o string/strtok.o unistd/execvp.o unistd/_exit.o ctype/isalnum.o time/ctime_r.o stdlib/strtold.o sys/time/gettimeofday.o stdio/fgetpos.o stdio/fsetpos.o ctype/isprint.o stdlib/system.o stdio/tmpnam.o unistd/msleep.o stdlib/atof.o stdlib/strtod.o stdio/rename.o sys/stat/mkdir.o unistd/uptime.o unistd/ftruncate.o sys/socket/recvfrom.o sys/socket/sendto.o signal/kill.o signal/sigaction.o unistd/chdir.o unistd/getcwd.o stdio/getdelim.o stdio/getline.o unistd/isatty.o sys/socket/listen.o stdlib/realpath.o systemcall.o sys/random/randomfill.o fcntl/open.o unistd/write.o unistd/pwrite.o fcntl/open_process.o tb/sb.o tb/sv.o string/memchr.o stdlib/atol.o stdlib/atoll.o stdlib/strtoll.o
+OBJ=crt0.o libc.o malloc/malloc.o pty.o sys/mman/mmap.o sys/mman/munmap.o memset.o assert.o stdio/snprintf.o stdio/vfprintf.o string/memcpy.o string/memcmp.o string/strcmp.o ubsan.o string/strcpy.o isspace.o stdio/puts.o stdio/putchar.o dirent/opendir.o dirent/readdir.o dirent/closedir.o unistd/getopt.o dirent/scandir.o dirent/alphasort.o stdio/printf.o stdio/vdprintf.o stdio/vprintf.o stdio/dprintf.o stdio/vprintf.o string/strlen.o string/strnlen.o stdio/stdin.o stdio/getchar.o stdio/fgetc.o arpa/inet/htons.o arpa/inet/htonl.o stdio/fread.o stdio/fwrite.o stdio/fopen.o stdio/fclose.o stdio/fseek.o ctype/isascii.o stdio/fprintf.o stdlib/atoi.o stdlib/strtol.o ctype/toupper.o ctype/tolower.o string/strcat.o string/strchr.o string/sscanf.o sys/stat/stat.o stdlib/getenv.o string/strrchr.o stdio/ftell.o stdio/tmpfile.o stdio/fgets.o stdio/feof.o stdio/fscanf.o stdio/ungetc.o string/strncmp.o stdio/fputc.o string/strncpy.o stdio/remove.o stdio/ferror.o stdio/fputs.o stdlib/rand.o stdlib/srand.o unistd/getpid.o stdlib/strtoul.o stdio/fflush.o stdlib/abort.o string/strcspn.o time/localtime.o time/time.o time/clock_gettime.o time/gmtime.o time/strftime.o string/strpbrk.o ctype/isdigit.o ctype/isalpha.o ctype/isxdigit.o ctype/ispunct.o stdio/setvbuf.o stdio/fileno.o stdio/putc.o stdio/sprintf.o stdlib/abs.o string/strspn.o stdlib/qsort.o string/memmove.o setjmp/longjmp.o setjmp/setjmp.o libgen/basename.o string/strdup.o string/strndup.o string/strlcpy.o stdlib/atexit.o stdio/open_memstream.o libgen/dirname.o unistd/unlink.o string/strstr.o string/strcasecmp.o string/strncasecmp.o stdlib/mkstemp.o string/strtok.o unistd/execvp.o unistd/_exit.o ctype/isalnum.o time/ctime_r.o stdlib/strtold.o sys/time/gettimeofday.o stdio/fgetpos.o stdio/fsetpos.o ctype/isprint.o stdlib/system.o stdio/tmpnam.o unistd/msleep.o stdlib/atof.o stdlib/strtod.o stdio/rename.o sys/stat/mkdir.o unistd/uptime.o unistd/ftruncate.o sys/socket/recvfrom.o sys/socket/sendto.o signal/kill.o signal/sigaction.o unistd/chdir.o unistd/getcwd.o stdio/getdelim.o stdio/getline.o unistd/isatty.o sys/socket/listen.o stdlib/realpath.o systemcall.o sys/random/randomfill.o fcntl/open.o unistd/write.o unistd/pwrite.o fcntl/open_process.o tb/sb.o tb/sv.o string/memchr.o stdlib/atol.o stdlib/atoll.o stdlib/strtoll.o sys/stat/fstat.o
all: libc.a
%.o: %.c
diff --git a/userland/libc/include/syscall.h b/userland/libc/include/syscall.h
index 1e8c86c..245b482 100644
--- a/userland/libc/include/syscall.h
+++ b/userland/libc/include/syscall.h
@@ -28,7 +28,7 @@
#define SYS_SOCKET 20
#define SYS_SHM_OPEN 21
#define SYS_FTRUNCATE 22
-#define SYS_STAT 23
+#define SYS_FSTAT 23
#define SYS_MSLEEP 24
#define SYS_UPTIME 25
#define SYS_MKDIR 26
@@ -167,9 +167,4 @@ typedef struct SYS_FTRUNCATE_PARAMS {
int fildes;
size_t length;
} __attribute__((packed)) SYS_FTRUNCATE_PARAMS;
-
-typedef struct SYS_STAT_PARAMS {
- const char *pathname;
- struct stat *statbuf;
-} __attribute__((packed)) SYS_STAT_PARAMS;
#endif
diff --git a/userland/libc/sys/stat.h b/userland/libc/sys/stat.h
deleted file mode 100644
index 2fe6cdc..0000000
--- a/userland/libc/sys/stat.h
+++ /dev/null
@@ -1,30 +0,0 @@
-#ifndef STAT_H
-#define STAT_H
-#include <sys/types.h>
-#include <time.h>
-
-struct stat {
- dev_t st_dev; // Device ID of device containing file.
- ino_t st_ino; // File serial number.
- mode_t st_mode; // Mode of file (see below).
- nlink_t st_nlink; // Number of hard links to the file.
- uid_t st_uid; // User ID of file.
- gid_t st_gid; // Group ID of file.
- dev_t st_rdev; // Device ID (if file is character or block special).
- off_t st_size; // For regular files, the file size in bytes.
- // For symbolic links, the length in bytes of the
- // pathname contained in the symbolic link.
- // For a shared memory object, the length in bytes.
- // For a typed memory object, the length in bytes.
- // For other file types, the use of this field is
- // unspecified.
- struct timespec st_atime; // Last data access timestamp.
- struct timespec st_mtime; // Last data modification timestamp.
- struct timespec st_ctime; // Last file status change timestamp.
- blksize_t st_blksize; // A file system-specific preferred I/O block size
- // for this object. In some file system types, this
- // may vary from file to file.
- blkcnt_t st_blocks; // Number of blocks allocated for this object.
-};
-int stat(const char *path, struct stat *buf);
-#endif
diff --git a/userland/libc/sys/stat/fstat.c b/userland/libc/sys/stat/fstat.c
new file mode 100644
index 0000000..13b2b04
--- /dev/null
+++ b/userland/libc/sys/stat/fstat.c
@@ -0,0 +1,9 @@
+#include <assert.h>
+#include <errno.h>
+#include <stdio.h>
+#include <sys/stat.h>
+#include <syscall.h>
+
+int fstat(int fd, struct stat *buf) {
+ RC_ERRNO(syscall(SYS_FSTAT, fd, buf, 0, 0, 0));
+}
diff --git a/userland/libc/sys/stat/stat.c b/userland/libc/sys/stat/stat.c
index e37223e..00b4f52 100644
--- a/userland/libc/sys/stat/stat.c
+++ b/userland/libc/sys/stat/stat.c
@@ -1,13 +1,16 @@
#include <assert.h>
#include <errno.h>
+#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <syscall.h>
int stat(const char *path, struct stat *buf) {
- SYS_STAT_PARAMS args = {
- .pathname = path,
- .statbuf = buf,
- };
- RC_ERRNO(syscall(SYS_STAT, &args, 0, 0, 0, 0));
+ int fd = open(path, O_RDONLY);
+ if (-1 == fd) {
+ return -1;
+ }
+ int rc = fstat(fd, buf);
+ close(fd);
+ return rc;
}