Pārlūkot izejas kodu

add sys_dup sys_dup2系统调用

shaojinchun 5 gadi atpakaļ
vecāks
revīzija
22e171aaa8

+ 16 - 10
components/dfs/filesystems/cromfs/dfs_cromfs.c

@@ -129,7 +129,7 @@ static struct cromfs_avl_struct* cromfs_avl_find(avl_key_t key, struct cromfs_av
 
 static void cromfs_avl_rebalance(struct cromfs_avl_struct ***nodeplaces_ptr, int count)
 {
-    for ( ;count > 0; count--)
+    for (;count > 0; count--)
     {
         struct cromfs_avl_struct **nodeplace = *--nodeplaces_ptr;
         struct cromfs_avl_struct *node = *nodeplace;
@@ -144,7 +144,8 @@ static void cromfs_avl_rebalance(struct cromfs_avl_struct ***nodeplaces_ptr, int
             int heightleftright = heightof(nodeleftright);
             if (heightof(nodeleftleft) >= heightleftright)
             {
-                node->avl_left = nodeleftright; nodeleft->avl_right = node;
+                node->avl_left = nodeleftright;
+                nodeleft->avl_right = node;
                 nodeleft->avl_height = 1 + (node->avl_height = 1 + heightleftright);
                 *nodeplace = nodeleft;
             }
@@ -166,7 +167,8 @@ static void cromfs_avl_rebalance(struct cromfs_avl_struct ***nodeplaces_ptr, int
             int heightrightleft = heightof(noderightleft);
             if (heightof(noderightright) >= heightrightleft)
             {
-                node->avl_right = noderightleft; noderight->avl_left = node;
+                node->avl_right = noderightleft;
+                noderight->avl_left = node;
                 noderight->avl_height = 1 + (node->avl_height = 1 + heightrightleft);
                 *nodeplace = noderight;
             }
@@ -207,7 +209,8 @@ static void cromfs_avl_remove(struct cromfs_avl_struct *node_to_delete, struct c
         {
             return;
         }
-        *stack_ptr++ = nodeplace; stack_count++;
+        *stack_ptr++ = nodeplace;
+        stack_count++;
         if (key == node->avl_key)
         {
             break;
@@ -225,7 +228,8 @@ static void cromfs_avl_remove(struct cromfs_avl_struct *node_to_delete, struct c
     if (node_to_delete->avl_left == AVL_EMPTY)
     {
         *nodeplace_to_delete = node_to_delete->avl_right;
-        stack_ptr--; stack_count--;
+        stack_ptr--;
+        stack_count--;
     }
     else
     {
@@ -239,7 +243,8 @@ static void cromfs_avl_remove(struct cromfs_avl_struct *node_to_delete, struct c
             {
                 break;
             }
-            *stack_ptr++ = nodeplace; stack_count++;
+            *stack_ptr++ = nodeplace;
+            stack_count++;
             nodeplace = &node->avl_right;
         }
         *nodeplace = node->avl_left;
@@ -266,7 +271,8 @@ static void cromfs_avl_insert(struct cromfs_avl_struct *new_node, struct cromfs_
         {
             break;
         }
-        *stack_ptr++ = nodeplace; stack_count++;
+        *stack_ptr++ = nodeplace;
+        stack_count++;
         if (key < node->avl_key)
         {
             nodeplace = &node->avl_left;
@@ -589,12 +595,12 @@ static uint32_t cromfs_lookup(cromfs_info *ci, const char *path, int* is_dir, ui
         /* skip /// */
         while (*subpath_end && *subpath_end == '/')
         {
-            subpath_end ++;
+            subpath_end++;
         }
         subpath = subpath_end;
         while ((*subpath_end != '/') && *subpath_end)
         {
-            subpath_end ++;
+            subpath_end++;
         }
         if (*subpath == '\0')
         {
@@ -1087,7 +1093,7 @@ static int dfs_cromfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_
         return -EINVAL;
     }
 
-    for (index = 0; index < count && file->pos < file->fnode->size; index ++)
+    for (index = 0; index < count && file->pos < file->fnode->size; index++)
     {
         uint32_t name_size = 0;
 

+ 2 - 0
components/dfs/filesystems/devfs/devfs.c

@@ -172,7 +172,9 @@ int dfs_device_fs_open(struct dfs_fd *file)
 
     device = rt_device_find(&file->fnode->path[1]);
     if (device == RT_NULL)
+    {
         return -ENODEV;
+    }
 
 #ifdef RT_USING_POSIX
     if (device->fops)

+ 17 - 3
components/dfs/filesystems/elmfat/dfs_elm.c

@@ -342,7 +342,9 @@ int dfs_elm_open(struct dfs_fd *file)
         return -ENOENT;
     drivers_fn = (char *)rt_malloc(256);
     if (drivers_fn == RT_NULL)
+    {
         return -ENOMEM;
+    }
 
     rt_snprintf(drivers_fn, 256, "%d:%s", vol, file->fnode->path);
 #else
@@ -393,18 +395,28 @@ int dfs_elm_open(struct dfs_fd *file)
         mode = FA_READ;
 
         if (file->fnode->flags & O_WRONLY)
+        {
             mode |= FA_WRITE;
+        }
         if ((file->fnode->flags & O_ACCMODE) & O_RDWR)
+        {
             mode |= FA_WRITE;
+        }
         /* Opens the file, if it is existing. If not, a new file is created. */
         if (file->fnode->flags & O_CREAT)
+        {
             mode |= FA_OPEN_ALWAYS;
+        }
         /* Creates a new file. If the file is existing, it is truncated and overwritten. */
         if (file->fnode->flags & O_TRUNC)
+        {
             mode |= FA_CREATE_ALWAYS;
+        }
         /* Creates a new file. The function fails if the file is already existing. */
         if (file->fnode->flags & O_EXCL)
+        {
             mode |= FA_CREATE_NEW;
+        }
 
         /* allocate a fd */
         fd = (FIL *)rt_malloc(sizeof(FIL));
@@ -451,7 +463,7 @@ int dfs_elm_close(struct dfs_fd *file)
     result = FR_OK;
     if (file->fnode->type == FT_DIRECTORY)
     {
-        DIR *dir;
+        DIR *dir = RT_NULL;
 
         dir = (DIR *)(file->data);
         RT_ASSERT(dir != RT_NULL);
@@ -461,7 +473,7 @@ int dfs_elm_close(struct dfs_fd *file)
     }
     else if (file->fnode->type == FT_REGULAR)
     {
-        FIL *fd;
+        FIL *fd = RT_NULL;
         fd = (FIL *)(file->data);
         RT_ASSERT(fd != RT_NULL);
 
@@ -550,7 +562,9 @@ int dfs_elm_write(struct dfs_fd *file, const void *buf, size_t len)
     file->pos  = fd->fptr;
     file->fnode->size = f_size(fd);
     if (result == FR_OK)
+    {
         return byte_write;
+    }
 
     return elm_result_to_dfs(result);
 }
@@ -589,7 +603,7 @@ int dfs_elm_lseek(struct dfs_fd *file, off_t offset)
     else if (file->fnode->type == FT_DIRECTORY)
     {
         /* which is a directory */
-        DIR *dir;
+        DIR *dir = RT_NULL;
 
         dir = (DIR *)(file->data);
         RT_ASSERT(dir != RT_NULL);

+ 58 - 0
components/dfs/filesystems/jffs2/dfs_jffs2.c

@@ -102,14 +102,20 @@ static int dfs_jffs2_mount(struct dfs_filesystem* fs,
     for (index = 0; index < DEVICE_PART_MAX; index ++)
     {
         if (device_partition[index].dev == RT_NULL)
+        {
             break;
+        }
     }
     if (index == DEVICE_PART_MAX)
+    {
         return -ENOSPC;
+    }
 
     mte = rt_malloc(sizeof(struct cyg_mtab_entry));
     if (mte == RT_NULL)
+    {
         return -ENOMEM;
+    }
 
     mte->name = fs->path;
     mte->fsname = "jffs2";
@@ -192,7 +198,9 @@ static int dfs_jffs2_statfs(struct dfs_filesystem* fs,
 
     result = _find_fs(&mte, fs->dev_id);
     if (result)
+    {
         return -ENOENT;
+    }
 
     RT_ASSERT(mte->data != 0);
 
@@ -221,14 +229,20 @@ static int dfs_jffs2_open(struct dfs_fd* file)
 
     jffs2_file = rt_malloc(sizeof(cyg_file));
     if (jffs2_file == RT_NULL)
+    {
         return -ENOMEM;
+    }
 
     /* just escape '/' provided by dfs code */
     name = file->fnode->path;
     if ((name[0] == '/') && (name[1] == 0))
+    {
         name = jffs2_root_path;
+    }
     else /* name[0] still will be '/' */
+    {
         name ++;
+    }
 
     result = _find_fs(&mte, fs->dev_id);
     if (result)
@@ -322,7 +336,9 @@ static int dfs_jffs2_close(struct dfs_fd* file)
         result = jffs2_dir_colse(jffs2_file);
         rt_mutex_release(&jffs2_lock);
         if (result)
+        {
             return jffs2_result_to_dfs(result);
+        }
 
         rt_free(jffs2_file);
         return 0;
@@ -332,7 +348,9 @@ static int dfs_jffs2_close(struct dfs_fd* file)
     result = jffs2_file_colse(jffs2_file);
     rt_mutex_release(&jffs2_lock);
     if (result)
+    {
         return jffs2_result_to_dfs(result);
+    }
 
     /* release memory */
     rt_free(jffs2_file);
@@ -366,7 +384,9 @@ static int dfs_jffs2_read(struct dfs_fd* file, void* buf, size_t len)
     result = jffs2_file_read(jffs2_file, &uio_s);
     rt_mutex_release(&jffs2_lock);
     if (result)
+    {
         return jffs2_result_to_dfs(result);
+    }
 
     /* update position */
     file->pos = jffs2_file->f_offset;
@@ -398,7 +418,9 @@ static int dfs_jffs2_write(struct dfs_fd* file,
     result = jffs2_file_write(jffs2_file, &uio_s);
     rt_mutex_release(&jffs2_lock);
     if (result)
+    {
         return jffs2_result_to_dfs(result);
+    }
 
     /* update position */
     file->pos = jffs2_file->f_offset;
@@ -427,7 +449,9 @@ static int dfs_jffs2_lseek(struct dfs_fd* file,
     result = jffs2_file_lseek(jffs2_file, &offset, SEEK_SET);
     rt_mutex_release(&jffs2_lock);
     if (result)
+    {
         return jffs2_result_to_dfs(result);
+    }
     /* update file position */
     file->pos = offset;
     return offset;
@@ -479,7 +503,9 @@ static int dfs_jffs2_getdents(struct dfs_fd* file,
         rt_mutex_release(&jffs2_lock);
         /* if met a error or all entry are read over, break while*/
         if (result || jffs2_d.d_name[0] == 0)
+        {
             break;
+        }
 
 #if defined (CYGPKG_FS_JFFS2_RET_DIRENT_DTYPE)
         switch(jffs2_d.d_type & JFFS2_S_IFMT)
@@ -497,17 +523,25 @@ static int dfs_jffs2_getdents(struct dfs_fd* file,
         if ((file->fnode->path[0] == '/') )
         {
             if (file->fnode->path[1] == 0)
+            {
                 strcpy(fullname, jffs2_d.d_name);
+            }
             else
+            {
                 rt_sprintf(fullname, "%s/%s", file->fnode->path+1, jffs2_d.d_name);
+            }
         }
         else
+        {
             rt_sprintf(fullname, "%s/%s", file->fnode->path, jffs2_d.d_name);
+        }
         rt_mutex_take(&jffs2_lock, RT_WAITING_FOREVER);
         result = jffs2_porting_stat(mte, mte->root, fullname, (void *)&s);
         rt_mutex_release(&jffs2_lock);
         if (result)
+        {
             return jffs2_result_to_dfs(result);
+        }
 
         rt_free(fullname);
         /* convert to dfs stat structure */
@@ -525,10 +559,14 @@ static int dfs_jffs2_getdents(struct dfs_fd* file,
 
         index ++;
         if (index * sizeof(struct dirent) >= count)
+        {
             break;
+        }
     }
     if (result)
+    {
         return jffs2_result_to_dfs(result);
+    }
     return index * sizeof(struct dirent);
 }
 
@@ -540,11 +578,15 @@ static int dfs_jffs2_unlink(struct dfs_filesystem* fs, const char* path)
 
     result = _find_fs(&mte, fs->dev_id);
     if (result)
+    {
         return -ENOENT;
+    }
 
     /* deal path */
     if (path[0] == '/')
+    {
         path++;
+    }
 
     /* judge file type, dir is to be delete by rmdir, others by unlink */
     rt_mutex_take(&jffs2_lock, RT_WAITING_FOREVER);
@@ -570,7 +612,9 @@ static int dfs_jffs2_unlink(struct dfs_filesystem* fs, const char* path)
     }
     rt_mutex_release(&jffs2_lock);
     if (result)
+    {
         return jffs2_result_to_dfs(result);
+    }
     return 0;
 }
 
@@ -583,17 +627,25 @@ static int dfs_jffs2_rename(struct dfs_filesystem* fs,
 
     result = _find_fs(&mte, fs->dev_id);
     if (result)
+    {
         return -ENOENT;
+    }
 
     if (*oldpath == '/')
+    {
         oldpath += 1;
+    }
     if (*newpath == '/')
+    {
         newpath += 1;
+    }
     rt_mutex_take(&jffs2_lock, RT_WAITING_FOREVER);
     result = jffs2_rename(mte, mte->root, oldpath, mte->root, newpath);
     rt_mutex_release(&jffs2_lock);
     if (result)
+    {
         return jffs2_result_to_dfs(result);
+    }
     return 0;
 }
 
@@ -607,18 +659,24 @@ static int dfs_jffs2_stat(struct dfs_filesystem* fs, const char *path, struct st
     RT_ASSERT(!((path[0] == '/') && (path[1] == 0)));
 
     if (path[0] == '/')
+    {
         path++;
+    }
 
     result = _find_fs(&mte, fs->dev_id);
     if (result)
+    {
         return -ENOENT;
+    }
 
     rt_mutex_take(&jffs2_lock, RT_WAITING_FOREVER);
     result = jffs2_porting_stat(mte, mte->root, path, (void *)&s);
     rt_mutex_release(&jffs2_lock);
 
     if (result)
+    {
         return jffs2_result_to_dfs(result);
+    }
     /* convert to dfs stat structure */
     switch(s.st_mode & JFFS2_S_IFMT)
     {

+ 0 - 1
components/dfs/include/dfs.h

@@ -105,7 +105,6 @@ int fdt_fd_new(struct dfs_fdtable *fdt);
 struct dfs_fd *fdt_fd_get(struct dfs_fdtable* fdt, int fd);
 void fdt_fd_release(struct dfs_fdtable* fdt, int fd);
 int fd_new(void);
-int fd_dup(int fd);
 int fd_associate(struct dfs_fdtable *fdt, int fd, struct dfs_fd *file);
 struct dfs_fd *fd_get(int fd);
 void fd_release(int fd);

+ 105 - 40
components/dfs/src/dfs.c

@@ -143,44 +143,64 @@ void dfs_fd_unlock(void)
     rt_mutex_release(&fdlock);
 }
 
-static int fd_slot_alloc(struct dfs_fdtable *fdt, int startfd)
+static int fd_slot_expand(struct dfs_fdtable *fdt, int fd)
 {
-    int idx = 0;
+    int nr = 0;
+    int index = 0;
+    struct dfs_fd **fds = NULL;
 
-    /* find an empty fd slot */
-    for (idx = startfd; idx < (int)fdt->maxfd; idx++)
+    if (fd < fdt->maxfd)
     {
-        if (fdt->fds[idx] == RT_NULL)
-            break;
-        if (fdt->fds[idx]->ref_count == 0)
-            break;
+        return fd;
+    }
+    if (fd >= DFS_FD_MAX)
+    {
+        return -1;
     }
 
-    /* allocate a larger FD container */
-    if (idx >= fdt->maxfd && fdt->maxfd < DFS_FD_MAX)
+    nr = ((fd + 4) & ~3);
+    if (nr > DFS_FD_MAX)
     {
-        int cnt = 0;
-        int index = 0;
-        struct dfs_fd **fds = NULL;
+        nr = DFS_FD_MAX;
+    }
+    fds = (struct dfs_fd **)rt_realloc(fdt->fds, nr * sizeof(struct dfs_fd *));
+    if (!fds)
+    {
+        return -1;
+    }
 
-        /* increase the number of FD with 4 step length */
-        cnt = fdt->maxfd + 4;
-        cnt = cnt > DFS_FD_MAX ? DFS_FD_MAX : cnt;
+    /* clean the new allocated fds */
+    for (index = fdt->maxfd; index < nr; index++)
+    {
+        fds[index] = NULL;
+    }
+    fdt->fds   = fds;
+    fdt->maxfd = nr;
 
-        fds = (struct dfs_fd **)rt_realloc(fdt->fds, cnt * sizeof(struct dfs_fd *));
-        if (fds == NULL) /* return fdt->maxfd */
-        {
-            return fdt->maxfd;
-        }
+    return fd;
+}
 
-        /* clean the new allocated fds */
-        for (index = fdt->maxfd; index < cnt; index ++)
+static int fd_slot_alloc(struct dfs_fdtable *fdt, int startfd)
+{
+    int idx = 0;
+
+    /* find an empty fd slot */
+    for (idx = startfd; idx < (int)fdt->maxfd; idx++)
+    {
+        if (fdt->fds[idx] == RT_NULL)
         {
-            fds[index] = NULL;
+            return idx;
         }
+    }
 
-        fdt->fds   = fds;
-        fdt->maxfd = cnt;
+    idx = fdt->maxfd;
+    if (idx < startfd)
+    {
+        idx = startfd;
+    }
+    if (fd_slot_expand(fdt, idx) < 0)
+    {
+        return -1;
     }
     return idx;
 }
@@ -193,18 +213,19 @@ static int fd_alloc(struct dfs_fdtable *fdt, int startfd)
     idx = fd_slot_alloc(fdt, startfd);
 
     /* allocate  'struct dfs_fd' */
-    if (idx < (int)fdt->maxfd && fdt->fds[idx] == RT_NULL)
+    if (idx < 0)
     {
-        fd = (struct dfs_fd *)rt_calloc(1, sizeof(struct dfs_fd));
-        if (!fd)
-        {
-            return fdt->maxfd;
-        }
-        fd->ref_count = 1;
-        fd->magic = DFS_FD_MAGIC;
-        fd->fnode = NULL;
-        fdt->fds[idx] = fd;
+        return -1;
     }
+    fd = (struct dfs_fd *)rt_calloc(1, sizeof(struct dfs_fd));
+    if (!fd)
+    {
+        return -1;
+    }
+    fd->ref_count = 1;
+    fd->magic = DFS_FD_MAGIC;
+    fd->fnode = NULL;
+    fdt->fds[idx] = fd;
 
     return idx;
 }
@@ -226,9 +247,8 @@ int fdt_fd_new(struct dfs_fdtable *fdt)
     idx = fd_alloc(fdt, DFS_STDIO_OFFSET);
 
     /* can't find an empty fd entry */
-    if (idx == fdt->maxfd)
+    if (idx < 0)
     {
-        idx = -1;
         LOG_E("DFS fd new is failed! Could not found an empty fd entry.");
         goto __result;
     }
@@ -261,7 +281,9 @@ struct dfs_fd *fdt_fd_get(struct dfs_fdtable* fdt, int fd)
     struct dfs_fd *d;
 
     if (fd < 0 || fd >= (int)fdt->maxfd)
+    {
         return NULL;
+    }
 
     dfs_fd_lock();
     d = fdt->fds[fd];
@@ -339,7 +361,7 @@ void fd_release(int fd)
     fdt_fd_release(fdt, fd);
 }
 
-int fd_dup(int oldfd)
+int sys_dup(int oldfd)
 {
     int newfd = -1;
     struct dfs_fdtable *fdt = NULL;
@@ -357,8 +379,50 @@ int fd_dup(int oldfd)
     }
     /* get a new fd */
     newfd = fd_slot_alloc(fdt, DFS_STDIO_OFFSET);
+    if (newfd >= 0)
+    {
+        fdt->fds[newfd] = fdt->fds[oldfd];
+        /* inc ref_count */
+        fdt->fds[newfd]->ref_count++;
+    }
+exit:
+    dfs_fd_unlock();
+    return newfd;
+}
+
+int sys_dup2(int oldfd, int newfd)
+{
+    struct dfs_fdtable *fdt = NULL;
+    int ret = 0;
+    int retfd = -1;
+
+    dfs_fd_lock();
+    /* check old fd */
+    fdt = dfs_fdtable_get();
+    if ((oldfd < 0) || (oldfd >= fdt->maxfd))
+    {
+        goto exit;
+    }
+    if (!fdt->fds[oldfd])
+    {
+        goto exit;
+    }
+    if (newfd < 0)
+    {
+        goto exit;
+    }
     if (newfd >= fdt->maxfd)
     {
+        newfd = fd_slot_expand(fdt, newfd);
+        if (newfd < 0)
+        {
+            goto exit;
+        }
+    }
+    if (fdt->fds[newfd] == fdt->fds[oldfd])
+    {
+        /* ok, return newfd */
+        retfd = newfd;
         goto exit;
     }
 
@@ -375,9 +439,10 @@ int fd_dup(int oldfd)
     fdt->fds[newfd] = fdt->fds[oldfd];
     /* inc ref_count */
     fdt->fds[newfd]->ref_count++;
+    retfd = newfd;
 exit:
     dfs_fd_unlock();
-    return newfd;
+    return retfd;
 }
 
 int fd_associate(struct dfs_fdtable *fdt, int fd, struct dfs_fd *file)

+ 4 - 4
components/libc/compilers/musl/stdio.c

@@ -19,6 +19,7 @@
 int	_EXFUN(fileno, (FILE *));
 
 static FILE* std_console = NULL;
+int sys_dup2(int oldfd, int new);
 
 int libc_stdio_set_console(const char* device_name, int mode)
 {
@@ -48,13 +49,12 @@ int libc_stdio_set_console(const char* device_name, int mode)
 
     if (std_console)
     {
-        struct dfs_fdtable *fdt = dfs_fdtable_get();
         int fd = fileno(std_console);
 
         /* set fd (0, 1, 2) */
-        fd_associate(fdt, 0, fd_get(fd));
-        fd_associate(fdt, 1, fd_get(fd));
-        fd_associate(fdt, 2, fd_get(fd));
+        sys_dup2(fd, 0);
+        sys_dup2(fd, 1);
+        sys_dup2(fd, 2);
         return fd;
     }
 

+ 5 - 0
components/lwp/lwp_syscall.c

@@ -2515,6 +2515,9 @@ int sys_clock_getres(clockid_t clk, struct timespec *ts)
 int sys_futex(int *uaddr, int op, int val, void *timeout, void *uaddr2, int val3);
 int sys_pmutex(void *umutex, int op, void *arg);
 
+int sys_dup(int oldfd);
+int sys_dup2(int oldfd, int new);
+
 const static void* func_table[] =
 {
     (void*)sys_exit,            /* 01 */
@@ -2665,6 +2668,8 @@ const static void* func_table[] =
     (void *)sys_clone,           /* 130 */
     (void *)sys_futex,
     (void *)sys_pmutex,
+    (void *)sys_dup,
+    (void *)sys_dup2,
 };
 
 const void *lwp_get_sys_api(rt_uint32_t number)