release the dnode lock after EACCESS in path walk
authorLunaixsky <lunaixsky@qq.com>
Mon, 31 Mar 2025 16:36:34 +0000 (17:36 +0100)
committerLunaixsky <lunaixsky@qq.com>
Tue, 1 Apr 2025 13:32:02 +0000 (14:32 +0100)
make struct stats conforming to POSIX
minor tweaks stock shell environment for better look and feel

17 files changed:
lunaix-os/includes/lunaix/fs_acl.h
lunaix-os/includes/lunaix/types.h
lunaix-os/includes/usr/lunaix/fcntl_defs.h
lunaix-os/includes/usr/lunaix/types.h
lunaix-os/kernel/device/devfs.c
lunaix-os/kernel/fs/path_walk.c
lunaix-os/kernel/fs/ramfs/ramfs.c
lunaix-os/kernel/fs/twifs/twifs.c
lunaix-os/kernel/fs/vfs.c
lunaix-os/kernel/kprint/kprintf.c
lunaix-os/usr/cat.c
lunaix-os/usr/init/init.c
lunaix-os/usr/libc/src/_vprintf.c
lunaix-os/usr/ls.c
lunaix-os/usr/rm.c
lunaix-os/usr/sh/sh.c
lunaix-os/usr/stat.c

index 23aab232dc13da401712647dca1fb5a9b5b7633d..fde7225080f98650ad5f2aa1ea19c63aafa97485 100644 (file)
@@ -8,6 +8,7 @@
 #define FSACL_WRITE    2
 #define FSACL_EXEC     1
 
+#define FSACL_MASK     03777
 #define FSACL_RWXMASK  0777
 #define FSACL_U(x)    (((x) & 0b111) << 6)
 #define FSACL_G(x)    (((x) & 0b111) << 3)
@@ -57,6 +58,9 @@
 #define FSACL_o(r, w, x)            \
         (v__(__fsacl_sel(o, r)) | v__(__fsacl_sel(o, w)) | v__(__fsacl_sel(o, x)))
 
+#define FSACL_DEFAULT               \
+        (FSACL_u(R, W, X) | FSACL_g(R, W, X) | FSACL_o(R, _, X))
+
 #define fsacl_test(acl, type)   ((acl) & (FSACL_##type))
 
 static inline bool must_inline
index fe06333f1939e18ac07f32aa8dd4ea7210c8653d..6ce5fcf3b71873e3ea2b7ce4243c03bff658aac0 100644 (file)
@@ -16,9 +16,6 @@ typedef unsigned int u32_t;
 typedef unsigned long ptr_t;
 typedef unsigned long reg_t;
 
-typedef unsigned int uid_t;
-typedef unsigned int gid_t;
-
 #ifndef CONFIG_ARCH_BITS_64
 typedef unsigned long long u64_t;
 #else
index 81be1e7a889767f00fe9fa24fedfd51f6316f8d3..36af62e74467fe38dda4297286299a412f9ffad5 100644 (file)
 /* Mount with block-cache-disabled flag */
 #define MNT_NC (1 << 1)
 
+typedef unsigned int mode_t;
+typedef unsigned int nlink_t;
+
 struct file_stat
 {
-    dev_t st_dev;
-    ino_t st_ino;
-    unsigned int mode;
-    dev_t st_rdev;
-    off_t st_size;
-    size_t st_blksize;
-    size_t st_ioblksize;
-    size_t st_blocks;
+    dev_t   st_dev;
+    ino_t   st_ino;
+    mode_t  st_mode;
+    nlink_t st_nlink;
+    uid_t   st_uid;
+    gid_t   st_gid;
+    dev_t   st_rdev;
+    off_t   st_size;
+    size_t  st_blksize;
+    size_t  st_blocks;
+
+    unsigned long st_atim;
+    unsigned long st_ctim;
+    unsigned long st_mtim;
+
+    size_t  st_ioblksize;
 };
 
 #endif /* __LUNAIX_FNCTL_DEFS_H */
index 19094e97fd9fc8426edc11e3a01aa123242208c9..3dbddb528c7d6f3a53019644229e4db94ce4002e 100644 (file)
@@ -24,6 +24,8 @@ typedef signed long ssize_t;
 
 typedef int pid_t;
 typedef int tid_t;
+typedef int uid_t;
+typedef int gid_t;
 
 typedef __SIZE_TYPE__ size_t;
 
index 78846177a4c957fa6ef719c9f610833b19f8a7ec..969c8288b2f0b58b91d8c6183e16dc017f770284 100644 (file)
@@ -180,10 +180,10 @@ devfs_init_inode(struct v_superblock* vsb, struct v_inode* inode)
     inode->ops = &devfs_inode_ops;
     inode->default_fops = &devfs_file_ops;
 
-    // we set default access right to be 0660.
+    // we set default access right to be 0775.
     // TODO need a way to allow this to be changed
     
-    fsapi_inode_setaccess(inode, FSACL_u(R, W, X) | FSACL_g(R, W, X) | FSACL_oX);
+    fsapi_inode_setaccess(inode, FSACL_DEFAULT);
     fsapi_inode_setowner(inode, 0, 0);
 }
 
index 7b49219edbad435fd1c9892676be55d110e2bf68..246e90327cc3219af3f03d418c0fba366dc4aea6 100644 (file)
@@ -95,6 +95,7 @@ __vfs_walk(struct v_dnode* start,
 
         if (!check_allow_execute(current_inode)) {
             errno = EACCESS;
+            unlock_dnode(current_level);
             goto error;
         }
 
@@ -106,6 +107,7 @@ __vfs_walk(struct v_dnode* start,
 
             if (!dnode) {
                 errno = ENOMEM;
+                unlock_dnode(current_level);
                 goto error;
             }
 
index d4e2f6fe662d578f481acc62f65c6df359ea3348..094408876320adc47c955f34b346555465280589 100644 (file)
@@ -135,7 +135,7 @@ ramfs_mount(struct v_superblock* vsb, struct v_dnode* mount_point)
     int errno = __ramfs_mknod(mount_point, NULL, RAMF_DIR);
 
     if (!errno) {
-        fsapi_inode_setaccess(mount_point->inode, FSACL_aX | FSACL_aR);
+        fsapi_inode_setaccess(mount_point->inode, FSACL_DEFAULT);
     }
 
     return errno;
index 3679ee1303f70bc5517544dfc4b0de60d8f34ce8..9d474958bbe10763fc81c4b5d9a599a936ee7dbd 100644 (file)
@@ -63,7 +63,7 @@ __twifs_init_inode(struct v_superblock* vsb, struct v_inode* inode)
     // we set default access right to be 0660.
     // TODO need a way to allow this to be changed
     
-    fsapi_inode_setaccess(inode, FSACL_u(R, W, _) | FSACL_g(R, W, _));
+    fsapi_inode_setaccess(inode, FSACL_DEFAULT);
     fsapi_inode_setowner(inode, 0, 0);
 }
 
index c117f18044d71e3ff66880f66ab82c2977827245..32014d34a871e2783ff696ef0bffdce663b4386e 100644 (file)
@@ -1733,12 +1733,21 @@ __DEFINE_LXSYSCALL2(int, fstat, int, fd, struct file_stat*, stat)
     struct v_inode* vino = fds->file->inode;
     struct device* fdev = vino->sb->dev;
 
-    *stat = (struct file_stat){.st_ino = vino->id,
-                               .st_blocks = vino->lb_usage,
-                               .st_size = vino->fsize,
-                               .mode = vino->itype,
-                               .st_ioblksize = PAGE_SIZE,
-                               .st_blksize = vino->sb->blksize};
+    stat->st_ino     = vino->id;
+    stat->st_blocks  = vino->lb_usage;
+    stat->st_size    = vino->fsize;
+    stat->st_blksize = vino->sb->blksize;
+    stat->st_nlink   = vino->link_count;
+    stat->st_uid     = vino->uid;
+    stat->st_gid     = vino->gid;
+
+    stat->st_ctim    = vino->ctime;
+    stat->st_atim    = vino->atime;
+    stat->st_mtim    = vino->mtime;
+
+    stat->st_mode    = (vino->itype << 16) | vino->acl;
+
+    stat->st_ioblksize = PAGE_SIZE;
 
     if (check_device_node(vino)) {
         struct device* rdev = resolve_device(vino->data);
index 363abc687b5d770113e74fd98f848e529dbe8a7c..376df360e4d19d25c492c09af3ae690b0af81b11 100644 (file)
@@ -112,7 +112,7 @@ __twimap_kprintf_read(struct twimap* map)
     {
         time_t s = pos->time / 1000;
         time_t ms = pos->time % 1000;
-        twimap_printf(map, "[%05d.%03d] %s\n", s, ms, pos->content);
+        twimap_printf(map, "[%05d.%03d] %s", s, ms, pos->content);
     }
 }
 
index cd8cad08ebdfaa18f05f9de1c73fb2244639502f..1fe6c1610bfb12a11d9690b71290df4d24be4c7e 100644 (file)
@@ -26,7 +26,7 @@ main(int argc, const char* argv[])
             return 1;
         }
 
-        if ((stat.mode & F_DIR)) {
+        if (((stat.st_mode >> 16) & F_DIR)) {
             printf("%s is a directory", argv[i]);
             return 1;
         }
index 25996ce6c03b368b1d5d57d1a66532bc7c89af55..804b8f82199a210660ba6ff5029073e710429b95 100644 (file)
@@ -39,7 +39,7 @@ init_termios(int fd) {
 
     check(tcgetattr(fd, &term));
 
-    term.c_lflag = ICANON | IEXTEN | ISIG | ECHO | ECHOE | ECHONL;
+    term.c_lflag = ICANON | IEXTEN | ISIG | ECHO | ECHOE;
     term.c_iflag = ICRNL | IGNBRK;
     term.c_oflag = ONLCR | OPOST;
     term.c_cflag = CREAD | CLOCAL | CS8 | CPARENB;
index 16d81338b90bdb9f9c1aff5779e0bd8d5836e885..7659e28c283586bc31905610cc3cf66a8d03ac6b 100644 (file)
@@ -112,6 +112,9 @@ __vprintf_internal(char* buffer, const char* fmt, size_t max_len, va_list vargs)
                 flags = flags | FLAG_CAPS;
                 base = 16;
                 goto format_unsigned;
+            case 'o':
+                base = 8;
+                goto format_unsigned;
             case 'p':
                 num = (uintptr_t)va_arg(vargs, void*);
                 base = 16;
index caea32d8f63c481b14da836ed616dad61a603a69..3fdfc6eb772475633321d1543e2476cefe98a0d7 100644 (file)
@@ -13,27 +13,43 @@ main(int argc, const char* argv[])
     DIR* dir = opendir(path);
 
     if (!dir) {
-        return errno;
+        goto done;
     }
 
     struct dirent* dent;
+    int i = 0, sz;
+    char bf[100];
 
     while ((dent = readdir(dir))) {
         if (dent->d_type == DT_DIR) {
-            printf(" %s/\n", dent->d_name);
+            sz = snprintf(bf, 100, "%s/", dent->d_name);
         } else if (dent->d_type == DT_SYMLINK) {
-            printf(" %s@\n", dent->d_name);
+            sz = snprintf(bf, 100, "%s@", dent->d_name);
         } else {
-            printf(" %s\n", dent->d_name);
+            sz = snprintf(bf, 100, "%s", dent->d_name);
         }
+
+        bf[sz] = 0;
+        printf("%-15s ", bf);
+
+        i++;
+        if ((i % 4) == 0) {
+            printf("\n");
+        }
+    }
+
+    if ((i % 4) != 0) {
+        printf("\n");
     }
 
+done:
     int err = errno;
     if (err) {
         printf("failed: %d\n",err);
     }
 
-    closedir(dir);
+    if (dir)
+        closedir(dir);
 
     return err;
 }
\ No newline at end of file
index 7fea8332528af9e30baa0542fd4178b30da3d342..c6046ce695c608d5c3b10d7bf51797213e6b40c2 100644 (file)
@@ -29,7 +29,7 @@ main(int argc, const char* argv[])
 
     close(fd);
 
-    if ((stat.mode & F_DIR)) {
+    if (((stat.st_mode >> 16) & F_DIR)) {
         err = rmdir(path);
     }
     else {
index 5ca47ed093bc0495a25c5dd65558b11676539280..764404b0910a54fe50c33891b20967388c3de20e 100644 (file)
@@ -182,7 +182,7 @@ sh_loop()
 
     while (1) {
         getcwd(pwd, 512);
-        printf("[%s]$ ", pwd);
+        printf("%s # ", pwd);
         int sz = read(stdin, buf, 511);
 
         if (sz < 0) {
@@ -195,8 +195,7 @@ sh_loop()
 
         // currently, this shell only support single argument
         if (!parse_cmdline(buf, argv)) {
-            printf("\n");
-            goto cont;
+             continue;
         }
 
         // cmd=="exit"
@@ -204,9 +203,7 @@ sh_loop()
             break;
         }
 
-        sh_exec((const char**)argv);
-    cont:
-        printf("\n");
+        sh_exec((const char**)argv);       
     }
 }
 
index ea402000553e3273fe3d060205e623784f269b7d..1bd79f3a25a270720b0ff8b9a509f41819c3808c 100644 (file)
@@ -28,7 +28,7 @@ main(int argc, char* argv[])
     printf("File: %s ", argv[1]);
 
     char* ftype = "directory";
-    int mode = stat.mode;
+    int mode = stat.st_mode >> 16;
     if ((mode & F_DEV)) {
         ftype = "mappable (sequential) device";
 
@@ -53,16 +53,25 @@ main(int argc, char* argv[])
            stat.st_blocks,
            stat.st_blksize,
            stat.st_ioblksize);
-    printf("Inode: %d;  ", stat.st_ino);
 
     dev_t* dev;
-    if (!(stat.mode & F_DEV)) {
+    if (!(mode & F_DEV)) {
         dev = &stat.st_dev;
     } else {
         dev = &stat.st_rdev;
     }
+    printf("Device: %xh:%xh@%d;  Inode: %d;  Links: %d\n", 
+           dev->meta, dev->unique, dev->index,
+           stat.st_ino, stat.st_nlink);
 
-    printf("Device: %xh:%xh@%d;\n", dev->meta, dev->unique, dev->index);
+    printf("Access: 0%o;  Uid: %d;  Gid: %d\n",
+           stat.st_mode & 0xffff,
+           stat.st_uid,
+           stat.st_gid);
+
+    printf("Access: %lu\n", stat.st_atim);
+    printf("Modify: %lu\n", stat.st_mtim);
+    printf("Create: %lu\n", stat.st_ctim);
 
     close(fd);
     return 0;