From f13e160f6aa2f74895ecf1f65099265643836210 Mon Sep 17 00:00:00 2001 From: Minep Date: Thu, 29 Dec 2022 01:03:17 +0000 Subject: [PATCH] refactor: separate syscall interfaces from kernel space, into posix compliant structure. --- lunaix-os/includes/lunaix/fctrl.h | 85 ------------- lunaix-os/includes/lunaix/foptions.h | 10 +- lunaix-os/includes/lunaix/ioctl.h | 15 +-- lunaix-os/includes/lunaix/lunistd.h | 64 ---------- lunaix-os/includes/lunaix/lxsignal.h | 9 -- lunaix-os/includes/lunaix/signal.h | 43 ++----- lunaix-os/includes/lunaix/syscall.h | 15 ++- lunaix-os/includes/lunaix/types.h | 15 --- lunaix-os/includes/usr/errno.h | 9 ++ lunaix-os/includes/usr/fcntl.h | 10 ++ lunaix-os/includes/usr/fcntl_defs.h | 14 +++ lunaix-os/includes/usr/signal.h | 19 +++ lunaix-os/includes/usr/signal_defs.h | 21 ++++ lunaix-os/includes/usr/sys/dirent.h | 9 ++ lunaix-os/includes/usr/sys/dirent_defs.h | 19 +++ lunaix-os/includes/usr/sys/ioctl.h | 7 ++ lunaix-os/includes/usr/sys/ioctl_defs.h | 15 +++ lunaix-os/includes/usr/sys/lunaix.h | 21 ++++ lunaix-os/includes/usr/sys/mount.h | 17 +++ lunaix-os/includes/usr/sys/types.h | 15 +++ lunaix-os/includes/usr/unistd.h | 111 +++++++++++++++++ lunaix-os/kernel/asm/x86/pfault.c | 2 +- lunaix-os/kernel/demos/dir_read.c | 14 +-- lunaix-os/kernel/demos/input_test.c | 11 +- lunaix-os/kernel/demos/iotest.c | 14 +-- lunaix-os/kernel/demos/signal_demo.c | 17 +-- lunaix-os/kernel/demos/simple_sh.c | 16 +-- lunaix-os/kernel/device/devfs.c | 3 +- lunaix-os/kernel/fs/iso9660/directory.c | 3 +- lunaix-os/kernel/fs/vfs.c | 7 +- lunaix-os/kernel/lxinit.c | 5 +- lunaix-os/kernel/proc0.c | 7 +- lunaix-os/kernel/process/signal.c | 5 +- lunaix-os/kernel/process/taskfs.c | 3 +- lunaix-os/kernel/tty/lxconsole.c | 3 +- lunaix-os/libs/ulibc/printf.c | 3 +- lunaix-os/usr/api/dirent.c | 4 + lunaix-os/usr/api/errno.c | 4 + lunaix-os/usr/api/fcntl.c | 4 + lunaix-os/usr/api/ioctl.c | 4 + .../lunaix/lunaix.h => usr/api/lunaix.c} | 9 +- lunaix-os/usr/api/mount.c | 15 +++ lunaix-os/usr/api/signal.c | 16 +++ lunaix-os/usr/api/unistd.c | 117 ++++++++++++++++++ 44 files changed, 535 insertions(+), 294 deletions(-) delete mode 100644 lunaix-os/includes/lunaix/fctrl.h delete mode 100644 lunaix-os/includes/lunaix/lunistd.h delete mode 100644 lunaix-os/includes/lunaix/lxsignal.h create mode 100644 lunaix-os/includes/usr/errno.h create mode 100644 lunaix-os/includes/usr/fcntl.h create mode 100644 lunaix-os/includes/usr/fcntl_defs.h create mode 100644 lunaix-os/includes/usr/signal.h create mode 100644 lunaix-os/includes/usr/signal_defs.h create mode 100644 lunaix-os/includes/usr/sys/dirent.h create mode 100644 lunaix-os/includes/usr/sys/dirent_defs.h create mode 100644 lunaix-os/includes/usr/sys/ioctl.h create mode 100644 lunaix-os/includes/usr/sys/ioctl_defs.h create mode 100644 lunaix-os/includes/usr/sys/lunaix.h create mode 100644 lunaix-os/includes/usr/sys/mount.h create mode 100644 lunaix-os/includes/usr/unistd.h create mode 100644 lunaix-os/usr/api/dirent.c create mode 100644 lunaix-os/usr/api/errno.c create mode 100644 lunaix-os/usr/api/fcntl.c create mode 100644 lunaix-os/usr/api/ioctl.c rename lunaix-os/{includes/lunaix/lunaix.h => usr/api/lunaix.c} (62%) create mode 100644 lunaix-os/usr/api/mount.c create mode 100644 lunaix-os/usr/api/signal.c create mode 100644 lunaix-os/usr/api/unistd.c diff --git a/lunaix-os/includes/lunaix/fctrl.h b/lunaix-os/includes/lunaix/fctrl.h deleted file mode 100644 index 36efb4b..0000000 --- a/lunaix-os/includes/lunaix/fctrl.h +++ /dev/null @@ -1,85 +0,0 @@ -#ifndef __LUNAIX_FCTRL_H -#define __LUNAIX_FCTRL_H - -#include -#include -#include - -__LXSYSCALL2(int, open, const char*, path, int, options) - -__LXSYSCALL1(int, mkdir, const char*, path) -__LXSYSCALL2(int, unlinkat, int, fd, const char*, pathname) - -__LXSYSCALL2(int, sys_readdir, int, fd, struct dirent*, dent) - -__LXSYSCALL4(int, - readlinkat, - int, - dirfd, - const char*, - pathname, - char*, - buf, - size_t, - size) - -__LXSYSCALL3(int, realpathat, int, fd, char*, buf, size_t, size) - -__LXSYSCALL4(int, - mount, - const char*, - source, - const char*, - target, - const char*, - fstype, - int, - options) - -__LXSYSCALL1(int, unmount, const char*, target) - -__LXSYSCALL4(int, - getxattr, - const char*, - path, - const char*, - name, - void*, - value, - size_t, - len) - -__LXSYSCALL4(int, - setxattr, - const char*, - path, - const char*, - name, - void*, - value, - size_t, - len) - -__LXSYSCALL4(int, - fgetxattr, - int, - fd, - const char*, - name, - void*, - value, - size_t, - len) - -__LXSYSCALL4(int, - fsetxattr, - int, - fd, - const char*, - name, - void*, - value, - size_t, - len) - -#endif /* __LUNAIX_FCTRL_H */ diff --git a/lunaix-os/includes/lunaix/foptions.h b/lunaix-os/includes/lunaix/foptions.h index 0bb6ac4..46b1500 100644 --- a/lunaix-os/includes/lunaix/foptions.h +++ b/lunaix-os/includes/lunaix/foptions.h @@ -1,14 +1,6 @@ #ifndef __LUNAIX_FOPTIONS_H #define __LUNAIX_FOPTIONS_H -#define FO_CREATE 0x1 -#define FO_APPEND 0x2 -#define FO_DIRECT 0x4 - -#define FSEEK_SET 0x1 -#define FSEEK_CUR 0x2 -#define FSEEK_END 0x3 - -#define MNT_RO 0x1 +#include #endif /* __LUNAIX_FOPTIONS_H */ diff --git a/lunaix-os/includes/lunaix/ioctl.h b/lunaix-os/includes/lunaix/ioctl.h index e074bba..f72d41e 100644 --- a/lunaix-os/includes/lunaix/ioctl.h +++ b/lunaix-os/includes/lunaix/ioctl.h @@ -1,19 +1,6 @@ #ifndef __LUNAIX_IOCTL_H #define __LUNAIX_IOCTL_H -#include - -#define IOREQ(cmd, arg_num) ((((cmd)&0xffff) << 8) | ((arg_num)&0xff)) - -#define IOCMD(req) ((req) >> 8) - -#define IOARGNUM(req) ((req)&0xff) - -#define TIOCGPGRP IOREQ(1, 0) -#define TIOCSPGRP IOREQ(1, 1) -#define TIOCCLSBUF IOREQ(2, 0) -#define TIOCFLUSH IOREQ(3, 0) - -__LXSYSCALL2_VARG(int, ioctl, int, fd, int, req); +#include #endif /* __LUNAIX_IOCTL_H */ diff --git a/lunaix-os/includes/lunaix/lunistd.h b/lunaix-os/includes/lunaix/lunistd.h deleted file mode 100644 index 9f2ef9e..0000000 --- a/lunaix-os/includes/lunaix/lunistd.h +++ /dev/null @@ -1,64 +0,0 @@ -#ifndef __LUNAIX_UNISTD_H -#define __LUNAIX_UNISTD_H - -#include -#include -#include - -__LXSYSCALL(pid_t, fork) - -__LXSYSCALL1(int, sbrk, void*, addr) - -__LXSYSCALL1(void*, brk, unsigned long, size) - -__LXSYSCALL(pid_t, getpid) - -__LXSYSCALL(pid_t, getppid) - -__LXSYSCALL(pid_t, getpgid) - -__LXSYSCALL2(pid_t, setpgid, pid_t, pid, pid_t, pgid) - -__LXSYSCALL1(void, _exit, int, status) - -__LXSYSCALL1(unsigned int, sleep, unsigned int, seconds) - -__LXSYSCALL(int, pause) - -__LXSYSCALL2(int, kill, pid_t, pid, int, signum) - -__LXSYSCALL1(unsigned int, alarm, unsigned int, seconds) - -__LXSYSCALL2(int, link, const char*, oldpath, const char*, newpath) - -__LXSYSCALL1(int, rmdir, const char*, pathname) - -__LXSYSCALL3(int, read, int, fd, void*, buf, unsigned int, count) - -__LXSYSCALL3(int, write, int, fd, void*, buf, unsigned int, count) - -__LXSYSCALL3(int, readlink, const char*, path, char*, buf, size_t, size) - -__LXSYSCALL3(int, lseek, int, fd, int, offset, int, options) - -__LXSYSCALL1(int, unlink, const char*, pathname) - -__LXSYSCALL1(int, close, int, fd) - -__LXSYSCALL2(int, dup2, int, oldfd, int, newfd) - -__LXSYSCALL1(int, dup, int, oldfd) - -__LXSYSCALL1(int, fsync, int, fildes) - -__LXSYSCALL2(int, symlink, const char*, pathname, const char*, link_target) - -__LXSYSCALL1(int, chdir, const char*, path) - -__LXSYSCALL1(int, fchdir, int, fd) - -__LXSYSCALL2(char*, getcwd, char*, buf, size_t, size) - -__LXSYSCALL2(int, rename, const char*, oldpath, const char*, newpath) - -#endif /* __LUNAIX_UNISTD_H */ diff --git a/lunaix-os/includes/lunaix/lxsignal.h b/lunaix-os/includes/lunaix/lxsignal.h deleted file mode 100644 index 5b54ffc..0000000 --- a/lunaix-os/includes/lunaix/lxsignal.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef __LUNAIX_LXSIGNAL_H -#define __LUNAIX_LXSIGNAL_H - -#include - -int -signal_send(pid_t pid, int signum); - -#endif /* __LUNAIX_LXSIGNAL_H */ diff --git a/lunaix-os/includes/lunaix/signal.h b/lunaix-os/includes/lunaix/signal.h index 43cfb99..55859ba 100644 --- a/lunaix-os/includes/lunaix/signal.h +++ b/lunaix-os/includes/lunaix/signal.h @@ -1,21 +1,21 @@ #ifndef __LUNAIX_SIGNAL_H #define __LUNAIX_SIGNAL_H -#include +#include #define _SIG_NUM 16 #define _SIG_PENDING(bitmap, sig) ((bitmap) & (1 << (sig))) -#define _SIGSEGV 1 -#define _SIGALRM 2 -#define _SIGCHLD 3 -#define _SIGCLD _SIGCHLD -#define _SIGINT 4 -#define _SIGKILL 5 -#define _SIGSTOP 6 -#define _SIGCONT 7 -#define _SIGTERM 8 +#define _SIGSEGV SIGSEGV +#define _SIGALRM SIGALRM +#define _SIGCHLD SIGCHLD +#define _SIGCLD SIGCLD +#define _SIGINT SIGINT +#define _SIGKILL SIGKILL +#define _SIGSTOP SIGSTOP +#define _SIGCONT SIGCONT +#define _SIGTERM SIGTERM #define __SIGNAL(num) (1 << (num)) #define __SIGSET(bitmap, num) (bitmap = bitmap | __SIGNAL(num)) @@ -24,25 +24,8 @@ #define _SIGNAL_UNMASKABLE (__SIGNAL(_SIGKILL) | __SIGNAL(_SIGSTOP)) -#define _SIG_BLOCK 1 -#define _SIG_UNBLOCK 2 -#define _SIG_SETMASK 3 - -typedef unsigned int sigset_t; -typedef void (*sighandler_t)(int); - -__LXSYSCALL2(int, signal, int, signum, sighandler_t, handler); - -__LXSYSCALL1(int, sigpending, sigset_t, *set); -__LXSYSCALL1(int, sigsuspend, const sigset_t, *mask); - -__LXSYSCALL3(int, - sigprocmask, - int, - how, - const sigset_t, - *set, - sigset_t, - *oldset); +#define _SIG_BLOCK SIG_BLOCK +#define _SIG_UNBLOCK SIG_UNBLOCK +#define _SIG_SETMASK SIG_SETMASK #endif /* __LUNAIX_SIGNAL_H */ diff --git a/lunaix-os/includes/lunaix/syscall.h b/lunaix-os/includes/lunaix/syscall.h index 8da4dc5..6c42252 100644 --- a/lunaix-os/includes/lunaix/syscall.h +++ b/lunaix-os/includes/lunaix/syscall.h @@ -112,49 +112,48 @@ syscall_install(); return (rettype)v; #define __LXSYSCALL(rettype, name) \ - static rettype name() \ + rettype name() \ { \ ___DOINT33(__SYSCALL_##name, rettype) \ } #define __LXSYSCALL1(rettype, name, t1, p1) \ - static rettype name(__PARAM_MAP1(t1, p1)) \ + rettype name(__PARAM_MAP1(t1, p1)) \ { \ asm("" ::"b"(p1)); \ ___DOINT33(__SYSCALL_##name, rettype) \ } #define __LXSYSCALL2(rettype, name, t1, p1, t2, p2) \ - static rettype name(__PARAM_MAP2(t1, p1, t2, p2)) \ + rettype name(__PARAM_MAP2(t1, p1, t2, p2)) \ { \ asm("\n" ::"b"(p1), "c"(p2)); \ ___DOINT33(__SYSCALL_##name, rettype) \ } #define __LXSYSCALL3(rettype, name, t1, p1, t2, p2, t3, p3) \ - static rettype name(__PARAM_MAP3(t1, p1, t2, p2, t3, p3)) \ + rettype name(__PARAM_MAP3(t1, p1, t2, p2, t3, p3)) \ { \ asm("\n" ::"b"(p1), "c"(p2), "d"(p3)); \ ___DOINT33(__SYSCALL_##name, rettype) \ } #define __LXSYSCALL4(rettype, name, t1, p1, t2, p2, t3, p3, t4, p4) \ - static rettype name(__PARAM_MAP4(t1, p1, t2, p2, t3, p3, t4, p4)) \ + rettype name(__PARAM_MAP4(t1, p1, t2, p2, t3, p3, t4, p4)) \ { \ asm("\n" ::"b"(p1), "c"(p2), "d"(p3), "D"(p4)); \ ___DOINT33(__SYSCALL_##name, rettype) \ } #define __LXSYSCALL5(rettype, name, t1, p1, t2, p2, t3, p3, t4, p4, t5, p5) \ - static rettype name(__PARAM_MAP5(t1, p1, t2, p2, t3, p3, t4, p4, t5, p5)) \ + rettype name(__PARAM_MAP5(t1, p1, t2, p2, t3, p3, t4, p4, t5, p5)) \ { \ asm("" ::"r"(p5), "b"(p1), "c"(p2), "d"(p3), "D"(p4), "S"(p5)); \ ___DOINT33(__SYSCALL_##name, rettype) \ } #define __LXSYSCALL2_VARG(rettype, name, t1, p1, t2, p2) \ - __attribute__((noinline)) static rettype name( \ - __PARAM_MAP2(t1, p1, t2, p2), ...) \ + __attribute__((noinline)) rettype name(__PARAM_MAP2(t1, p1, t2, p2), ...) \ { \ /* No inlining! This depends on the call frame assumption */ \ void* _last = (void*)&p2 + sizeof(void*); \ diff --git a/lunaix-os/includes/lunaix/types.h b/lunaix-os/includes/lunaix/types.h index 0d1ad19..7e6562c 100644 --- a/lunaix-os/includes/lunaix/types.h +++ b/lunaix-os/includes/lunaix/types.h @@ -5,21 +5,6 @@ #include #include -#define PEXITTERM 0x100 -#define PEXITSTOP 0x200 -#define PEXITSIG 0x400 - -#define PEXITNUM(flag, code) (flag | (code & 0xff)) - -#define WNOHANG 1 -#define WUNTRACED 2 -#define WEXITSTATUS(wstatus) ((wstatus & 0xff)) -#define WIFSTOPPED(wstatus) ((wstatus & PEXITSTOP)) -#define WIFEXITED(wstatus) \ - ((wstatus & PEXITTERM) && ((char)WEXITSTATUS(wstatus) >= 0)) - -#define WIFSIGNALED(wstatus) ((wstatus & PEXITSIG)) - #define PACKED __attribute__((packed)) // TODO: WTERMSIG diff --git a/lunaix-os/includes/usr/errno.h b/lunaix-os/includes/usr/errno.h new file mode 100644 index 0000000..5b9b51b --- /dev/null +++ b/lunaix-os/includes/usr/errno.h @@ -0,0 +1,9 @@ +#ifndef __LUNAIX_SYS_ERRNO_H +#define __LUNAIX_SYS_ERRNO_H + +int +geterrno(); + +#define errno (geterrno()) + +#endif /* __LUNAIX_ERRNO_H */ diff --git a/lunaix-os/includes/usr/fcntl.h b/lunaix-os/includes/usr/fcntl.h new file mode 100644 index 0000000..2a83c93 --- /dev/null +++ b/lunaix-os/includes/usr/fcntl.h @@ -0,0 +1,10 @@ +#ifndef __LUNAIX_SYS_FCNTL_H +#define __LUNAIX_SYS_FCNTL_H + +#include +#include + +int +open(const char* path, int flags); + +#endif /* __LUNAIX_FCNTL_H */ diff --git a/lunaix-os/includes/usr/fcntl_defs.h b/lunaix-os/includes/usr/fcntl_defs.h new file mode 100644 index 0000000..01930cb --- /dev/null +++ b/lunaix-os/includes/usr/fcntl_defs.h @@ -0,0 +1,14 @@ +#ifndef __LUNAIX_SYS_FCNTL_DEFS_H +#define __LUNAIX_SYS_FCNTL_DEFS_H + +#define FO_CREATE 0x1 +#define FO_APPEND 0x2 +#define FO_DIRECT 0x4 + +#define FSEEK_SET 0x1 +#define FSEEK_CUR 0x2 +#define FSEEK_END 0x3 + +#define MNT_RO 0x1 + +#endif /* __LUNAIX_FNCTL_DEFS_H */ diff --git a/lunaix-os/includes/usr/signal.h b/lunaix-os/includes/usr/signal.h new file mode 100644 index 0000000..cc7c7ed --- /dev/null +++ b/lunaix-os/includes/usr/signal.h @@ -0,0 +1,19 @@ +#ifndef __LUNAIX_SYS_SIGNAL_H +#define __LUNAIX_SYS_SIGNAL_H + +#include +#include + +int +signal(int signum, sighandler_t handler); + +int +sigpending(sigset_t* set); + +int +sigsuspend(const sigset_t* mask); + +int +sigprocmask(int how, const sigset_t* set, sigset_t* oldset); + +#endif /* __LUNAIX_SIGNAL_H */ diff --git a/lunaix-os/includes/usr/signal_defs.h b/lunaix-os/includes/usr/signal_defs.h new file mode 100644 index 0000000..ac102ab --- /dev/null +++ b/lunaix-os/includes/usr/signal_defs.h @@ -0,0 +1,21 @@ +#ifndef __LUNAIX_SYS_SIGNAL_DEFS_H +#define __LUNAIX_SYS_SIGNAL_DEFS_H + +#define SIGSEGV 1 +#define SIGALRM 2 +#define SIGCHLD 3 +#define SIGCLD SIGCHLD +#define SIGINT 4 +#define SIGKILL 5 +#define SIGSTOP 6 +#define SIGCONT 7 +#define SIGTERM 8 + +#define SIG_BLOCK 1 +#define SIG_UNBLOCK 2 +#define SIG_SETMASK 3 + +typedef unsigned int sigset_t; +typedef void (*sighandler_t)(int); + +#endif /* __LUNAIX_SIGNAL_DEFS_H */ diff --git a/lunaix-os/includes/usr/sys/dirent.h b/lunaix-os/includes/usr/sys/dirent.h new file mode 100644 index 0000000..54334d3 --- /dev/null +++ b/lunaix-os/includes/usr/sys/dirent.h @@ -0,0 +1,9 @@ +#ifndef __LUNAIX_SYS_DIRENT_H +#define __LUNAIX_SYS_DIRENT_H + +#include + +int +sys_readdir(int fd, struct lx_dirent* dirent); + +#endif /* __LUNAIX_DIRENT_H */ diff --git a/lunaix-os/includes/usr/sys/dirent_defs.h b/lunaix-os/includes/usr/sys/dirent_defs.h new file mode 100644 index 0000000..7792355 --- /dev/null +++ b/lunaix-os/includes/usr/sys/dirent_defs.h @@ -0,0 +1,19 @@ +#ifndef __LUNAIX_SYS_DIRENT_DEFS_H +#define __LUNAIX_SYS_DIRENT_DEFS_H + +#define DIRENT_NAME_MAX_LEN 256 + +#define DT_FILE 0x0 +#define DT_DIR 0x1 +#define DT_SYMLINK 0x2 +#define DT_PIPE 0x2 + +struct lx_dirent +{ + unsigned int d_type; + unsigned int d_offset; + unsigned int d_nlen; + char d_name[DIRENT_NAME_MAX_LEN]; +}; + +#endif /* __LUNAIX_DIRENT_DEFS_H */ diff --git a/lunaix-os/includes/usr/sys/ioctl.h b/lunaix-os/includes/usr/sys/ioctl.h new file mode 100644 index 0000000..827cd44 --- /dev/null +++ b/lunaix-os/includes/usr/sys/ioctl.h @@ -0,0 +1,7 @@ +#ifndef __LUNAIX_SYS_IOCTL_H +#define __LUNAIX_SYS_IOCTL_H + +int +ioctl(int fd, int req, ...); + +#endif /* __LUNAIX_IOCTL_H */ diff --git a/lunaix-os/includes/usr/sys/ioctl_defs.h b/lunaix-os/includes/usr/sys/ioctl_defs.h new file mode 100644 index 0000000..7be48e7 --- /dev/null +++ b/lunaix-os/includes/usr/sys/ioctl_defs.h @@ -0,0 +1,15 @@ +#ifndef __LUNAIX_SYS_IOCTL_DEFS_H +#define __LUNAIX_SYS_IOCTL_DEFS_H + +#define IOREQ(cmd, arg_num) ((((cmd)&0xffff) << 8) | ((arg_num)&0xff)) + +#define IOCMD(req) ((req) >> 8) + +#define IOARGNUM(req) ((req)&0xff) + +#define TIOCGPGRP IOREQ(1, 0) +#define TIOCSPGRP IOREQ(1, 1) +#define TIOCCLSBUF IOREQ(2, 0) +#define TIOCFLUSH IOREQ(3, 0) + +#endif /* __LUNAIX_IOCTL_DEFS_H */ diff --git a/lunaix-os/includes/usr/sys/lunaix.h b/lunaix-os/includes/usr/sys/lunaix.h new file mode 100644 index 0000000..b5ba65e --- /dev/null +++ b/lunaix-os/includes/usr/sys/lunaix.h @@ -0,0 +1,21 @@ +#ifndef __LUNAIX_SYS_LUNAIX_H +#define __LUNAIX_SYS_LUNAIX_H + +#include + +void +yield(); + +pid_t +wait(int* status); + +pid_t +waitpid(pid_t pid, int* status, int flags); + +void +syslog(int level, const char* fmt, ...); + +int +realpathat(int fd, char* buf, size_t size); + +#endif /* __LUNAIX_LUNAIX_H */ diff --git a/lunaix-os/includes/usr/sys/mount.h b/lunaix-os/includes/usr/sys/mount.h new file mode 100644 index 0000000..0a5a5a5 --- /dev/null +++ b/lunaix-os/includes/usr/sys/mount.h @@ -0,0 +1,17 @@ +#ifndef __LUNAIX_SYS_MOUNT_H +#define __LUNAIX_SYS_MOUNT_H + +#include + +int +mount(const char* source, const char* target, const char* fstype, int flags); + +int +unmount(const char* target); + +static inline int +umount(const char* target) +{ + return unmount(target); +} +#endif /* __LUNAIX_MOUNT_H */ diff --git a/lunaix-os/includes/usr/sys/types.h b/lunaix-os/includes/usr/sys/types.h index 712d687..a4b7f29 100644 --- a/lunaix-os/includes/usr/sys/types.h +++ b/lunaix-os/includes/usr/sys/types.h @@ -1,6 +1,21 @@ #ifndef __LUNAIX_SYS_TYPES_H #define __LUNAIX_SYS_TYPES_H +#define PEXITTERM 0x100 +#define PEXITSTOP 0x200 +#define PEXITSIG 0x400 + +#define PEXITNUM(flag, code) (flag | (code & 0xff)) + +#define WNOHANG 1 +#define WUNTRACED 2 +#define WEXITSTATUS(wstatus) ((wstatus & 0xff)) +#define WIFSTOPPED(wstatus) ((wstatus & PEXITSTOP)) +#define WIFEXITED(wstatus) \ + ((wstatus & PEXITTERM) && ((char)WEXITSTATUS(wstatus) >= 0)) + +#define WIFSIGNALED(wstatus) ((wstatus & PEXITSIG)) + typedef signed long ssize_t; typedef int pid_t; diff --git a/lunaix-os/includes/usr/unistd.h b/lunaix-os/includes/usr/unistd.h new file mode 100644 index 0000000..92992be --- /dev/null +++ b/lunaix-os/includes/usr/unistd.h @@ -0,0 +1,111 @@ +#ifndef __LUNAIX_SYS_UNISTD_H +#define __LUNAIX_SYS_UNISTD_H + +#include + +pid_t +fork(); + +pid_t +getpid(); + +pid_t +getppid(); + +pid_t +getpgid(); + +pid_t +setpgid(pid_t pid, pid_t pgid); + +int +sbrk(void* addr); + +void* +brk(size_t size); + +void +_exit(int status); + +unsigned int +sleep(unsigned int); + +int +pause(); + +int +kill(pid_t pid, int signum); + +unsigned int +alarm(unsigned int seconds); + +int +link(const char* oldpath, const char* newpath); + +int +rmdir(const char* pathname); + +int +read(int fd, void* buf, size_t size); + +int +write(int fd, void* buf, size_t size); + +int +readlink(const char* path, char* buffer, size_t size); + +int +readlinkat(int dirfd, const char* pathname, char* buffer, size_t size); + +int +lseek(int fd, off_t offset, int mode); + +int +unlink(const char* pathname); + +int +unlinat(int fd, const char* pathname); + +int +mkdir(const char* path); + +int +close(int fd); + +int +dup2(int oldfd, int newfd); + +int +dup(int oldfd); + +int +fsync(int fd); + +int +symlink(const char* pathname, const char* linktarget); + +int +chdir(const char* path); + +int +fchdir(int fd); + +char* +getcwd(char* buf, size_t size); + +int +rename(const char* oldpath, const char* newpath); + +int +getxattr(const char* path, const char* name, void* value, size_t len); + +int +setxattr(const char* path, const char* name, void* value, size_t len); + +int +fgetxattr(int fd, const char* name, void* value, size_t len); + +int +fsetxattr(int fd, const char* name, void* value, size_t len); + +#endif /* __LUNAIX_UNISTD_H */ diff --git a/lunaix-os/kernel/asm/x86/pfault.c b/lunaix-os/kernel/asm/x86/pfault.c index bbaf59f..7b8a12b 100644 --- a/lunaix-os/kernel/asm/x86/pfault.c +++ b/lunaix-os/kernel/asm/x86/pfault.c @@ -1,11 +1,11 @@ #include #include -#include #include #include #include #include #include +#include #include #include diff --git a/lunaix-os/kernel/demos/dir_read.c b/lunaix-os/kernel/demos/dir_read.c index 70070a1..c97dfc9 100644 --- a/lunaix-os/kernel/demos/dir_read.c +++ b/lunaix-os/kernel/demos/dir_read.c @@ -1,27 +1,27 @@ -#include -#include -#include -#include +#include +#include +#include +#include void _readdir_main() { int fd = open("/dev/./../dev/.", 0); if (fd == -1) { - printf("fail to open (%d)\n", geterrno()); + printf("fail to open (%d)\n", errno); return; } char path[129]; int len = realpathat(fd, path, 128); if (len < 0) { - printf("fail to read (%d)\n", geterrno()); + printf("fail to read (%d)\n", errno); } else { path[len] = 0; printf("%s\n", path); } - struct dirent ent = { .d_offset = 0 }; + struct lx_dirent ent = { .d_offset = 0 }; while (sys_readdir(fd, &ent) == 1) { printf("%s\n", ent.d_name); diff --git a/lunaix-os/kernel/demos/input_test.c b/lunaix-os/kernel/demos/input_test.c index 47e0081..f24a519 100644 --- a/lunaix-os/kernel/demos/input_test.c +++ b/lunaix-os/kernel/demos/input_test.c @@ -1,9 +1,10 @@ -#include -#include #include -#include -#include + #include +#include +#include +#include +#include #define STDIN 1 #define STDOUT 0 @@ -14,7 +15,7 @@ input_test() int fd = open("/dev/input/i8042-kbd", 0); if (fd < 0) { - printf("fail to open (%d)", geterrno()); + printf("fail to open (%d)", errno); return; } diff --git a/lunaix-os/kernel/demos/iotest.c b/lunaix-os/kernel/demos/iotest.c index cba8962..91c3571 100644 --- a/lunaix-os/kernel/demos/iotest.c +++ b/lunaix-os/kernel/demos/iotest.c @@ -1,8 +1,8 @@ -#include -#include -#include -#include #include +#include +#include +#include +#include void _iotest_main() @@ -13,8 +13,8 @@ _iotest_main() char read_out[256]; // 切换工作目录至 /dev - int errno = chdir("/dev"); - if (errno) { + int status = chdir("/dev"); + if (status) { write(stdout, "fail to chdir", 15); return; } @@ -29,7 +29,7 @@ _iotest_main() int fd = open("./sda", 0); if (fd < 0) { - printf("fail to open (%d)\n", geterrno()); + printf("fail to open (%d)\n", errno); return; } diff --git a/lunaix-os/kernel/demos/signal_demo.c b/lunaix-os/kernel/demos/signal_demo.c index 3878a18..4d7897e 100644 --- a/lunaix-os/kernel/demos/signal_demo.c +++ b/lunaix-os/kernel/demos/signal_demo.c @@ -1,10 +1,11 @@ -#include -#include -#include #include -#include + #include +#include +#include +#include + void __USER__ sigchild_handler(int signum) { @@ -29,9 +30,9 @@ sigalrm_handler(int signum) void __USER__ _signal_demo_main() { - signal(_SIGCHLD, sigchild_handler); - signal(_SIGSEGV, sigsegv_handler); - signal(_SIGALRM, sigalrm_handler); + signal(SIGCHLD, sigchild_handler); + signal(SIGSEGV, sigsegv_handler); + signal(SIGALRM, sigalrm_handler); alarm(5); @@ -51,7 +52,7 @@ _signal_demo_main() for (int i = 0; i < 5; i++) { pid_t pid = 0; if (!(pid = fork())) { - signal(_SIGSEGV, sigsegv_handler); + signal(SIGSEGV, sigsegv_handler); sleep(i); if (i == 3) { i = *(int*)0xdeadc0de; // seg fault! diff --git a/lunaix-os/kernel/demos/simple_sh.c b/lunaix-os/kernel/demos/simple_sh.c index 8bb357b..26aa3be 100644 --- a/lunaix-os/kernel/demos/simple_sh.c +++ b/lunaix-os/kernel/demos/simple_sh.c @@ -1,12 +1,12 @@ -#include -#include #include -#include -#include -#include #include +#include +#include +#include +#include #include +#include #include #include @@ -101,7 +101,7 @@ do_ls(const char* path) if (fd < 0) { sh_printerr(); } else { - struct dirent ent = { .d_offset = 0 }; + struct lx_dirent ent = { .d_offset = 0 }; int status; while ((status = sys_readdir(fd, &ent)) == 1) { if (ent.d_type == DT_DIR) { @@ -125,7 +125,7 @@ do_mcat(const char* file) if (fd < 0) { sh_printerr(); } else { - ptr_t p = mmap(NULL, 2048, 0, 0, fd, 0); + void* p = mmap(NULL, 2048, 0, 0, fd, 0); if ((int)p < 0) { sh_printerr(); } else { @@ -143,7 +143,7 @@ sh_loop() char buf[512]; char *cmd, *argpart; pid_t p; - signal(_SIGINT, sigint_handle); + signal(SIGINT, sigint_handle); // set our shell as foreground process // (unistd.h:tcsetpgrp is essentially a wrapper of this) diff --git a/lunaix-os/kernel/device/devfs.c b/lunaix-os/kernel/device/devfs.c index c265c4b..69de7fa 100644 --- a/lunaix-os/kernel/device/devfs.c +++ b/lunaix-os/kernel/device/devfs.c @@ -1,9 +1,10 @@ #include -#include #include #include #include +#include + extern struct v_inode_ops devfs_inode_ops; extern struct v_file_ops devfs_file_ops; diff --git a/lunaix-os/kernel/fs/iso9660/directory.c b/lunaix-os/kernel/fs/iso9660/directory.c index c3150dc..71da904 100644 --- a/lunaix-os/kernel/fs/iso9660/directory.c +++ b/lunaix-os/kernel/fs/iso9660/directory.c @@ -1,4 +1,3 @@ -#include #include #include #include @@ -7,6 +6,8 @@ #include +#include + extern struct cake_pile* drec_cache_pile; void diff --git a/lunaix-os/kernel/fs/vfs.c b/lunaix-os/kernel/fs/vfs.c index cbe3c39..e436eba 100644 --- a/lunaix-os/kernel/fs/vfs.c +++ b/lunaix-os/kernel/fs/vfs.c @@ -44,7 +44,6 @@ */ #include -#include #include #include #include @@ -57,6 +56,8 @@ #include +#include + static struct cake_pile* dnode_pile; static struct cake_pile* inode_pile; static struct cake_pile* file_pile; @@ -625,13 +626,13 @@ __vfs_readdir_callback(struct dir_context* dctx, const int len, const int dtype) { - struct dirent* dent = (struct dirent*)dctx->cb_data; + struct lx_dirent* dent = (struct lx_dirent*)dctx->cb_data; strncpy(dent->d_name, name, DIRENT_NAME_MAX_LEN); dent->d_nlen = len; dent->d_type = dtype; } -__DEFINE_LXSYSCALL2(int, sys_readdir, int, fd, struct dirent*, dent) +__DEFINE_LXSYSCALL2(int, sys_readdir, int, fd, struct lx_dirent*, dent) { struct v_fd* fd_s; int errno; diff --git a/lunaix-os/kernel/lxinit.c b/lunaix-os/kernel/lxinit.c index f53405a..d4622c9 100644 --- a/lunaix-os/kernel/lxinit.c +++ b/lunaix-os/kernel/lxinit.c @@ -1,7 +1,5 @@ #include #include -#include -#include #include #include #include @@ -9,6 +7,9 @@ #include #include +#include +#include + extern uint8_t __kernel_start; LOG_MODULE("INIT") diff --git a/lunaix-os/kernel/proc0.c b/lunaix-os/kernel/proc0.c index 19f04dc..259c8bb 100644 --- a/lunaix-os/kernel/proc0.c +++ b/lunaix-os/kernel/proc0.c @@ -1,11 +1,8 @@ #include #include -#include #include #include #include -#include -#include #include #include #include @@ -18,6 +15,10 @@ #include #include +#include +#include +#include + #include #include diff --git a/lunaix-os/kernel/process/signal.c b/lunaix-os/kernel/process/signal.c index 9a9a349..0f43b1f 100644 --- a/lunaix-os/kernel/process/signal.c +++ b/lunaix-os/kernel/process/signal.c @@ -1,6 +1,3 @@ -#include -#include -#include #include #include #include @@ -8,6 +5,8 @@ #include #include +#include + extern struct scheduler sched_ctx; /* kernel/sched.c */ void __USER__ diff --git a/lunaix-os/kernel/process/taskfs.c b/lunaix-os/kernel/process/taskfs.c index 7534703..6507eb1 100644 --- a/lunaix-os/kernel/process/taskfs.c +++ b/lunaix-os/kernel/process/taskfs.c @@ -1,4 +1,3 @@ -#include #include #include #include @@ -8,6 +7,8 @@ #include #include +#include + #define COUNTER_MASK ((1 << 16) - 1) static struct hbucket* attr_export_table; diff --git a/lunaix-os/kernel/tty/lxconsole.c b/lunaix-os/kernel/tty/lxconsole.c index 21802be..adf577a 100644 --- a/lunaix-os/kernel/tty/lxconsole.c +++ b/lunaix-os/kernel/tty/lxconsole.c @@ -8,11 +8,10 @@ #include #include #include +#include #include #include -#include - static struct console lx_console; int diff --git a/lunaix-os/libs/ulibc/printf.c b/lunaix-os/libs/ulibc/printf.c index 18e2387..86c0b18 100644 --- a/lunaix-os/libs/ulibc/printf.c +++ b/lunaix-os/libs/ulibc/printf.c @@ -1,8 +1,9 @@ #include -#include #include #include +#include + // This is VERY bad implementation as it mixes both kernel and user space // code together. It is here however, just for the convenience of our testing // program. diff --git a/lunaix-os/usr/api/dirent.c b/lunaix-os/usr/api/dirent.c new file mode 100644 index 0000000..403d4e3 --- /dev/null +++ b/lunaix-os/usr/api/dirent.c @@ -0,0 +1,4 @@ +#include +#include + +__LXSYSCALL2(int, sys_readdir, int, fd, struct lx_dirent*, dent) diff --git a/lunaix-os/usr/api/errno.c b/lunaix-os/usr/api/errno.c new file mode 100644 index 0000000..27b4124 --- /dev/null +++ b/lunaix-os/usr/api/errno.c @@ -0,0 +1,4 @@ +#include +#include + +__LXSYSCALL(int, geterrno); \ No newline at end of file diff --git a/lunaix-os/usr/api/fcntl.c b/lunaix-os/usr/api/fcntl.c new file mode 100644 index 0000000..05a2cd4 --- /dev/null +++ b/lunaix-os/usr/api/fcntl.c @@ -0,0 +1,4 @@ +#include +#include + +__LXSYSCALL2(int, open, const char*, path, int, options) diff --git a/lunaix-os/usr/api/ioctl.c b/lunaix-os/usr/api/ioctl.c new file mode 100644 index 0000000..d9ee35b --- /dev/null +++ b/lunaix-os/usr/api/ioctl.c @@ -0,0 +1,4 @@ +#include +#include + +__LXSYSCALL2_VARG(int, ioctl, int, fd, int, req); \ No newline at end of file diff --git a/lunaix-os/includes/lunaix/lunaix.h b/lunaix-os/usr/api/lunaix.c similarity index 62% rename from lunaix-os/includes/lunaix/lunaix.h rename to lunaix-os/usr/api/lunaix.c index adbde1d..c6c5f81 100644 --- a/lunaix-os/includes/lunaix/lunaix.h +++ b/lunaix-os/usr/api/lunaix.c @@ -1,8 +1,5 @@ -#ifndef __LUNAIX_LUNAIX_H -#define __LUNAIX_LUNAIX_H - #include -#include +#include __LXSYSCALL(void, yield); @@ -10,8 +7,6 @@ __LXSYSCALL1(pid_t, wait, int*, status); __LXSYSCALL3(pid_t, waitpid, pid_t, pid, int*, status, int, options); -__LXSYSCALL(int, geterrno); - __LXSYSCALL2_VARG(void, syslog, int, level, const char*, fmt); -#endif /* __LUNAIX_LUNAIX_H */ +__LXSYSCALL3(int, realpathat, int, fd, char*, buf, size_t, size) diff --git a/lunaix-os/usr/api/mount.c b/lunaix-os/usr/api/mount.c new file mode 100644 index 0000000..e073f37 --- /dev/null +++ b/lunaix-os/usr/api/mount.c @@ -0,0 +1,15 @@ +#include +#include + +__LXSYSCALL4(int, + mount, + const char*, + source, + const char*, + target, + const char*, + fstype, + int, + options) + +__LXSYSCALL1(int, unmount, const char*, target) \ No newline at end of file diff --git a/lunaix-os/usr/api/signal.c b/lunaix-os/usr/api/signal.c new file mode 100644 index 0000000..6e9039c --- /dev/null +++ b/lunaix-os/usr/api/signal.c @@ -0,0 +1,16 @@ +#include +#include + +__LXSYSCALL2(int, signal, int, signum, sighandler_t, handler); + +__LXSYSCALL1(int, sigpending, sigset_t, *set); +__LXSYSCALL1(int, sigsuspend, const sigset_t, *mask); + +__LXSYSCALL3(int, + sigprocmask, + int, + how, + const sigset_t, + *set, + sigset_t, + *oldset); diff --git a/lunaix-os/usr/api/unistd.c b/lunaix-os/usr/api/unistd.c new file mode 100644 index 0000000..bd7cb24 --- /dev/null +++ b/lunaix-os/usr/api/unistd.c @@ -0,0 +1,117 @@ +#include +#include + +__LXSYSCALL(pid_t, fork) + +__LXSYSCALL1(int, sbrk, void*, addr) + +__LXSYSCALL1(void*, brk, unsigned long, size) + +__LXSYSCALL(pid_t, getpid) + +__LXSYSCALL(pid_t, getppid) + +__LXSYSCALL(pid_t, getpgid) + +__LXSYSCALL2(pid_t, setpgid, pid_t, pid, pid_t, pgid) + +__LXSYSCALL1(void, _exit, int, status) + +__LXSYSCALL1(unsigned int, sleep, unsigned int, seconds) + +__LXSYSCALL(int, pause) + +__LXSYSCALL2(int, kill, pid_t, pid, int, signum) + +__LXSYSCALL1(unsigned int, alarm, unsigned int, seconds) + +__LXSYSCALL2(int, link, const char*, oldpath, const char*, newpath) + +__LXSYSCALL1(int, rmdir, const char*, pathname) + +__LXSYSCALL3(int, read, int, fd, void*, buf, size_t, count) + +__LXSYSCALL3(int, write, int, fd, void*, buf, size_t, count) + +__LXSYSCALL3(int, readlink, const char*, path, char*, buf, size_t, size) + +__LXSYSCALL3(int, lseek, int, fd, off_t, offset, int, options) + +__LXSYSCALL1(int, unlink, const char*, pathname) + +__LXSYSCALL1(int, close, int, fd) + +__LXSYSCALL2(int, dup2, int, oldfd, int, newfd) + +__LXSYSCALL1(int, dup, int, oldfd) + +__LXSYSCALL1(int, fsync, int, fildes) + +__LXSYSCALL2(int, symlink, const char*, pathname, const char*, link_target) + +__LXSYSCALL1(int, chdir, const char*, path) + +__LXSYSCALL1(int, fchdir, int, fd) + +__LXSYSCALL2(char*, getcwd, char*, buf, size_t, size) + +__LXSYSCALL2(int, rename, const char*, oldpath, const char*, newpath) + +__LXSYSCALL4(int, + getxattr, + const char*, + path, + const char*, + name, + void*, + value, + size_t, + len) + +__LXSYSCALL4(int, + setxattr, + const char*, + path, + const char*, + name, + void*, + value, + size_t, + len) + +__LXSYSCALL4(int, + fgetxattr, + int, + fd, + const char*, + name, + void*, + value, + size_t, + len) + +__LXSYSCALL4(int, + fsetxattr, + int, + fd, + const char*, + name, + void*, + value, + size_t, + len) + +__LXSYSCALL4(int, + readlinkat, + int, + dirfd, + const char*, + pathname, + char*, + buf, + size_t, + size) + +__LXSYSCALL2(int, unlinkat, int, fd, const char*, pathname) + +__LXSYSCALL1(int, mkdir, const char*, path) \ No newline at end of file -- 2.27.0