feat: stdin/stdout into process
authorMinep <zelong56@gmail.com>
Tue, 9 Aug 2022 12:34:37 +0000 (13:34 +0100)
committerMinep <zelong56@gmail.com>
Tue, 9 Aug 2022 12:34:37 +0000 (13:34 +0100)
lunaix-os/includes/lunaix/fs.h
lunaix-os/kernel/demos/iotest.c
lunaix-os/kernel/fs/vfs.c
lunaix-os/kernel/proc0.c
lunaix-os/kernel/process.c

index 87f259af83dfbc57f2833a6743d3c7c4f2a49df9..0764a61c26af42b9562d97d13b4252b5699b17d7 100644 (file)
@@ -234,6 +234,9 @@ vfs_i_alloc();
 void
 vfs_i_free(struct v_inode* inode);
 
 void
 vfs_i_free(struct v_inode* inode);
 
+int
+vfs_dup_fd(struct v_fd* old, struct v_fd** new);
+
 void
 pcache_init(struct pcache* pcache);
 
 void
 pcache_init(struct pcache* pcache);
 
index f41c9a8e499e63eaeb7854a5d522ad43c3dbe567..5e17b02db766f66f56d4c2ac625c73e1bb47c8ed 100644 (file)
@@ -6,6 +6,9 @@
 
 LOG_MODULE("IOTEST")
 
 
 LOG_MODULE("IOTEST")
 
+#define STDIN 1
+#define STDOUT 0
+
 void
 _iotest_main()
 {
 void
 _iotest_main()
 {
@@ -18,12 +21,7 @@ _iotest_main()
     //  Lunaix会尽可能缓存任何对此设备的上层读写,并使用延迟写入策略。(FO_DIRECT可用于屏蔽该功能)
     int fd = open("/dev/sda", 0);
 
     //  Lunaix会尽可能缓存任何对此设备的上层读写,并使用延迟写入策略。(FO_DIRECT可用于屏蔽该功能)
     int fd = open("/dev/sda", 0);
 
-    // tty 设备 - 控制台。
-    //  tty设备属于序列设备(Sequential Device),该类型设备的上层读写
-    //  无须经过Lunaix的缓存层,而是直接下发到底层驱动。(不受FO_DIRECT的影响)
-    int tty = open("/dev/tty", 0);
-
-    if (fd < 0 || tty < 0) {
+    if (fd < 0) {
         kprintf(KERROR "fail to open (%d)\n", geterrno());
         return;
     }
         kprintf(KERROR "fail to open (%d)\n", geterrno());
         return;
     }
@@ -36,29 +34,29 @@ _iotest_main()
         write(fd, test_sequence, sizeof(test_sequence));
     }
 
         write(fd, test_sequence, sizeof(test_sequence));
     }
 
-    lseek(fd, 521, FSEEK_SET);
+    // 随机读写测试
+    lseek(fd, 4 * 4096, FSEEK_SET);
     write(fd, test_sequence, sizeof(test_sequence));
 
     char read_out[256];
     write(fd, test_sequence, sizeof(test_sequence));
 
     char read_out[256];
-    write(tty, "input: ", 8);
-    int size = read(tty, read_out, 256);
+    write(STDOUT, "input: ", 8);
+    int size = read(STDIN, read_out, 256);
 
 
-    write(tty, "your input: ", 13);
-    write(tty, read_out, size);
+    write(STDOUT, "your input: ", 13);
+    write(STDOUT, read_out, size);
     write(fd, read_out, size);
     write(fd, read_out, size);
-    write(tty, "\n", 1);
+    write(STDOUT, "\n", 1);
 
     // 读出我们写的内容
     lseek(fd, 512, FSEEK_SET);
     read(fd, read_out, sizeof(read_out));
 
     // 将读出的内容直接写入tty设备
 
     // 读出我们写的内容
     lseek(fd, 512, FSEEK_SET);
     read(fd, read_out, sizeof(read_out));
 
     // 将读出的内容直接写入tty设备
-    write(tty, read_out, sizeof(read_out));
-    write(tty, "\n", 1);
+    write(STDOUT, read_out, sizeof(read_out));
+    write(STDOUT, "\n", 1);
 
     // 关闭文件,这同时会将页缓存中的数据下发到底层驱动
     close(fd);
 
     // 关闭文件,这同时会将页缓存中的数据下发到底层驱动
     close(fd);
-    close(tty);
 
     kprint_hex(read_out, sizeof(read_out));
 }
\ No newline at end of file
 
     kprint_hex(read_out, sizeof(read_out));
 }
\ No newline at end of file
index 1104d9b5533d9673f58722162c091a8da1d3c6ad..11896ec6730e4377a24d3d4d72b7644622663c51 100644 (file)
@@ -945,7 +945,7 @@ __DEFINE_LXSYSCALL1(int, fsync, int, fildes)
 }
 
 int
 }
 
 int
-__vfs_dup_fd(struct v_fd* old, struct v_fd** new)
+vfs_dup_fd(struct v_fd* old, struct v_fd** new)
 {
     int errno = 0;
     struct v_fd* copied = cake_grab(fd_pile);
 {
     int errno = 0;
     struct v_fd* copied = cake_grab(fd_pile);
@@ -953,10 +953,13 @@ __vfs_dup_fd(struct v_fd* old, struct v_fd** new)
     memcpy(copied, old, sizeof(struct v_fd));
     old->file->ref_count++;
 
     memcpy(copied, old, sizeof(struct v_fd));
     old->file->ref_count++;
 
+    *new = copied;
+
     return errno;
 }
 
     return errno;
 }
 
-__DEFINE_LXSYSCALL2(int, dup2, int, oldfd, int, newfd)
+int
+vfs_dup2(int oldfd, int newfd)
 {
     if (newfd == oldfd) {
         return newfd;
 {
     if (newfd == oldfd) {
         return newfd;
@@ -964,7 +967,7 @@ __DEFINE_LXSYSCALL2(int, dup2, int, oldfd, int, newfd)
 
     int errno;
     struct v_fd *oldfd_s, *newfd_s;
 
     int errno;
     struct v_fd *oldfd_s, *newfd_s;
-    if (!GET_FD(oldfd, oldfd_s) || TEST_FD(newfd)) {
+    if (!GET_FD(oldfd, oldfd_s) || !TEST_FD(newfd)) {
         errno = EBADF;
         goto done;
     }
         errno = EBADF;
         goto done;
     }
@@ -973,7 +976,7 @@ __DEFINE_LXSYSCALL2(int, dup2, int, oldfd, int, newfd)
         goto done;
     }
 
         goto done;
     }
 
-    if (!(errno = __vfs_dup_fd(oldfd_s, &newfd_s))) {
+    if (!(errno = vfs_dup_fd(oldfd_s, &newfd_s))) {
         __current->fdtable->fds[newfd] = newfd_s;
         return newfd;
     }
         __current->fdtable->fds[newfd] = newfd_s;
         return newfd;
     }
@@ -982,6 +985,11 @@ done:
     return DO_STATUS(errno);
 }
 
     return DO_STATUS(errno);
 }
 
+__DEFINE_LXSYSCALL2(int, dup2, int, oldfd, int, newfd)
+{
+    return vfs_dup2(oldfd, newfd);
+}
+
 __DEFINE_LXSYSCALL1(int, dup, int, oldfd)
 {
     int errno, newfd;
 __DEFINE_LXSYSCALL1(int, dup, int, oldfd)
 {
     int errno, newfd;
@@ -989,7 +997,7 @@ __DEFINE_LXSYSCALL1(int, dup, int, oldfd)
     if (!GET_FD(oldfd, oldfd_s)) {
         errno = EBADF;
     } else if (!(errno = vfs_alloc_fdslot(&newfd)) &&
     if (!GET_FD(oldfd, oldfd_s)) {
         errno = EBADF;
     } else if (!(errno = vfs_alloc_fdslot(&newfd)) &&
-               !(errno = __vfs_dup_fd(oldfd_s, &newfd_s))) {
+               !(errno = vfs_dup_fd(oldfd_s, &newfd_s))) {
         __current->fdtable->fds[newfd] = newfd_s;
         return newfd;
     }
         __current->fdtable->fds[newfd] = newfd_s;
         return newfd;
     }
index ec997425b9c0161ab817d19f91591c9c4c5585d2..5fac5f86f609e3efc078701234451291fe7ca454 100644 (file)
@@ -1,6 +1,7 @@
 #include <arch/x86/boot/multiboot.h>
 #include <lunaix/block.h>
 #include <lunaix/common.h>
 #include <arch/x86/boot/multiboot.h>
 #include <lunaix/block.h>
 #include <lunaix/common.h>
+#include <lunaix/fctrl.h>
 #include <lunaix/fs.h>
 #include <lunaix/fs/twifs.h>
 #include <lunaix/lunistd.h>
 #include <lunaix/fs.h>
 #include <lunaix/fs/twifs.h>
 #include <lunaix/lunistd.h>
@@ -64,6 +65,12 @@ _iotest_main();
 void __USER__
 __proc0_usr()
 {
 void __USER__
 __proc0_usr()
 {
+    // 打开tty设备(控制台),作为标准输入输出。
+    //  tty设备属于序列设备(Sequential Device),该类型设备的上层读写
+    //  无须经过Lunaix的缓存层,而是直接下发到底层驱动。(不受FO_DIRECT的影响)
+    int stdout = open("/dev/tty", 0);
+    int stdin = dup2(stdout, 1);
+
     pid_t p;
     if (!fork()) {
         _pconsole_main();
     pid_t p;
     if (!fork()) {
         _pconsole_main();
index 4d6a77ee2f7a5f2ea5ba3ea7617d543c04a626e8..086a5e3105a184ca3ccf784f3f8fb7cba46445f9 100644 (file)
@@ -3,6 +3,7 @@
 #include <lunaix/common.h>
 #include <lunaix/mm/pmm.h>
 #include <lunaix/mm/region.h>
 #include <lunaix/common.h>
 #include <lunaix/mm/pmm.h>
 #include <lunaix/mm/region.h>
+#include <lunaix/mm/valloc.h>
 #include <lunaix/mm/vmm.h>
 #include <lunaix/process.h>
 #include <lunaix/spike.h>
 #include <lunaix/mm/vmm.h>
 #include <lunaix/process.h>
 #include <lunaix/spike.h>
@@ -176,6 +177,17 @@ __mark_region(uintptr_t start_vpn, uintptr_t end_vpn, int attr)
     }
 }
 
     }
 }
 
+void
+__copy_fdtable(struct proc_info* pcb)
+{
+    for (size_t i = 0; i < VFS_MAX_FD; i++) {
+        struct v_fd* fd = __current->fdtable->fds[i];
+        if (!fd)
+            continue;
+        vfs_dup_fd(fd, &pcb->fdtable->fds[i]);
+    }
+}
+
 pid_t
 dup_proc()
 {
 pid_t
 dup_proc()
 {
@@ -184,7 +196,7 @@ dup_proc()
     pcb->intr_ctx = __current->intr_ctx;
     pcb->parent = __current;
 
     pcb->intr_ctx = __current->intr_ctx;
     pcb->parent = __current;
 
-    memcpy(pcb->fdtable, __current->fdtable, sizeof(struct v_fdtable));
+    __copy_fdtable(pcb);
     region_copy(&__current->mm.regions, &pcb->mm.regions);
 
     setup_proc_mem(pcb, PD_REFERENCED);
     region_copy(&__current->mm.regions, &pcb->mm.regions);
 
     setup_proc_mem(pcb, PD_REFERENCED);