Merge branch 'master' of ssh.ucc.asn.au:tpg/acess2
authorJohn Hodge (sonata) <[email protected]>
Wed, 15 May 2013 08:13:53 +0000 (16:13 +0800)
committerJohn Hodge (sonata) <[email protected]>
Wed, 15 May 2013 08:13:53 +0000 (16:13 +0800)
KernelLand/Kernel/include/vfs_ext.h
KernelLand/Kernel/include/vfs_int.h
KernelLand/Kernel/syscalls.c
KernelLand/Kernel/vfs/handle.c
KernelLand/Kernel/vfs/open.c
Usermode/Libraries/ld-acess.so_src/include_exp/acess/sys.h
Usermode/Libraries/libposix.so_src/Makefile
Usermode/Libraries/libposix.so_src/fcntl.c [new file with mode: 0644]
Usermode/Libraries/libposix.so_src/include_exp/fcntl.h
Usermode/Libraries/libposix.so_src/unistd.c

index 2cd4101..1bae964 100644 (file)
@@ -226,6 +226,13 @@ extern int VFS_OpenInode(Uint32 Mount, Uint64 Inode, int Mode);
  */
 extern void    VFS_Close(int FD);
 
+/**
+ * \brief Copy one FD to another
+ * \param SrcFD        Source file descriptor
+ * \param DstFD        Destination file descriptor (-1 means allocate new)
+ */
+extern int     VFS_DuplicateFD(int SrcFD, int DstFD);
+
 /**
  * \brief Get file information from an open file
  * \param FD   File handle returned by ::VFS_Open
index 232c55e..06ad8da 100644 (file)
@@ -62,6 +62,9 @@ extern int    VFS_CheckACL(tVFS_Node *Node, Uint Permissions);
 extern tVFS_Mount      *VFS_GetMountByIdent(Uint32 MountID);
 // --- dir.c ---
 extern int     VFS_MkNod(const char *Path, Uint Flags);
+// --- handle.c ---
+extern int     VFS_AllocHandle(int bIsUser, tVFS_Node *Node, int Mode);
+extern int     VFS_SetHandle(int FD, tVFS_Node *Node, int Mode);
 
 
 // --- VFS Helpers ---
index 635f69a..4416ec4 100644 (file)
@@ -214,6 +214,11 @@ void SyscallHandler(tSyscallRegs *Regs)
                VFS_Close( Regs->Arg1 );
                break;
        
+       case SYS_COPYFD:
+               LOG("VFS_DuplicateFD(%i,%i)", Regs->Arg1, Regs->Arg2);
+               ret = VFS_DuplicateFD(Regs->Arg1, Regs->Arg2);
+               break;
+       
        case SYS_SEEK:
                #if BITS == 64
                ret = VFS_Seek( Regs->Arg1, Regs->Arg2, Regs->Arg3 );
index cb4fae1..35aa00c 100644 (file)
 #define MAX_KERNEL_FILES       128
 
 // === PROTOTYPES ===
-#if 0
-tVFS_Handle    *VFS_GetHandle(int FD);
-#endif
 inline void    _ReferenceNode(tVFS_Node *Node);
- int   VFS_AllocHandle(int FD, tVFS_Node *Node, int Mode);
 
 // === GLOBALS ===
 tVFS_Handle    *gaUserHandles = (void*)MM_PPD_HANDLES;
@@ -66,10 +62,27 @@ tVFS_Handle *VFS_GetHandle(int FD)
        return h;
 }
 
-int VFS_AllocHandle(int bIsUser, tVFS_Node *Node, int Mode)
+int VFS_SetHandle(int FD, tVFS_Node *Node, int Mode)
 {
-        int    i;
+       tVFS_Handle     *h;
+       if(FD < 0)      return -1;
        
+       if( FD & VFS_KERNEL_FLAG ) {
+               FD &= (VFS_KERNEL_FLAG -1);
+               if( FD >= MAX_KERNEL_FILES )    return -1;
+               h = &gaKernelHandles[FD];
+       }
+       else {
+               if( FD >= *Threads_GetMaxFD())  return -1;
+               h = &gaUserHandles[FD];
+       }
+       h->Node = Node;
+       h->Mode = Mode;
+       return FD;
+}
+
+int VFS_AllocHandle(int bIsUser, tVFS_Node *Node, int Mode)
+{
        // Check for a user open
        if(bIsUser)
        {
@@ -90,7 +103,7 @@ int VFS_AllocHandle(int bIsUser, tVFS_Node *Node, int Mode)
                        memset( gaUserHandles, 0, size );
                }
                // Get a handle
-               for( i = 0; i < max_handles; i ++ )
+               for( int i = 0; i < max_handles; i ++ )
                {
                        if(gaUserHandles[i].Node)       continue;
                        gaUserHandles[i].Node = Node;
@@ -117,7 +130,7 @@ int VFS_AllocHandle(int bIsUser, tVFS_Node *Node, int Mode)
                        memset( gaKernelHandles, 0, size );
                }
                // Get a handle
-               for(i=0;i<MAX_KERNEL_FILES;i++)
+               for(int i=0;i<MAX_KERNEL_FILES;i++)
                {
                        if(gaKernelHandles[i].Node)     continue;
                        gaKernelHandles[i].Node = Node;
index e83ace8..67ef58a 100644 (file)
@@ -18,7 +18,6 @@
 
 // === IMPORTS ===
 extern tVFS_Mount      *gVFS_RootMount;
-extern int     VFS_AllocHandle(int bIsUser, tVFS_Node *Node, int Mode);
 extern tVFS_Node       *VFS_MemFile_Create(const char *Path);
 
 // === PROTOTYPES ===
@@ -752,6 +751,24 @@ void VFS_Close(int FD)
        h->Node = NULL;
 }
 
+int VFS_DuplicateFD(int SrcFD, int DstFD)
+{
+        int    isUser = !(SrcFD & VFS_KERNEL_FLAG);
+       tVFS_Handle     *src = VFS_GetHandle(SrcFD);
+       if( !src )      return -1;
+       if( DstFD == -1 ) {
+               DstFD = VFS_AllocHandle(isUser, src->Node, src->Mode);
+       }
+       else {
+               // Can't overwrite
+               if( VFS_GetHandle(DstFD) )
+                       return -1;
+               VFS_SetHandle(DstFD, src->Node, src->Mode);
+       }
+       memcpy(VFS_GetHandle(DstFD), src, sizeof(tVFS_Handle));
+       return DstFD;
+}
+
 /**
  * \brief Change current working directory
  */
index d45ffb6..bcd3d1f 100644 (file)
@@ -25,6 +25,7 @@
 #define OPENFLAG_APPEND        0x20
 #define        OPENFLAG_NOLINK 0x40
 #define        OPENFLAG_CREATE 0x80
+#define OPENFLAG_NONBLOCK      0x100   // How would this work?
 #ifndef SEEK_CUR
 # define SEEK_SET      1
 # define SEEK_CUR      0
index 20b5fec..6e666e8 100644 (file)
@@ -10,6 +10,7 @@ LDFLAGS  += -soname libposix.so -Map map.txt -lc
 \r
 OBJ  = main.o unistd.o dirent.o stat.o utmpx.o termios.o\r
 OBJ += pwd.o syslog.o sys_time.o sys_ioctl.o sys_resource.o\r
+OBJ += fcntl.o\r
 DEPFILES := $(OBJ:%.o=%.d)\r
 BIN = libposix.so\r
 \r
diff --git a/Usermode/Libraries/libposix.so_src/fcntl.c b/Usermode/Libraries/libposix.so_src/fcntl.c
new file mode 100644 (file)
index 0000000..d57bca5
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * Acess2 POSIX Emulation Library
+ * - By John Hodge (thePowersGang)
+ *
+ * fcntl.c
+ * - File descriptor control
+ */
+#include <sys/types.h> // off_t
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdarg.h>
+#include <acess/sys.h>
+#include <errno.h>
+
+// === CODE ===
+int fcntl(int fd, int cmd, ...)
+{
+        int    ret;
+       va_list args;
+       va_start(args, cmd);
+       
+       switch(cmd)
+       {
+       case F_GETFL: {
+                int    a_flags = _SysFDFlags(fd, 0, 0);
+               if( a_flags == -1 )
+                       return -1;
+               ret = 0;
+               if(a_flags & OPENFLAG_READ)     ret |= O_RDONLY;
+               if(a_flags & OPENFLAG_WRITE)    ret |= O_WRONLY;
+               if(a_flags & OPENFLAG_NONBLOCK) ret |= O_NONBLOCK;
+               if(a_flags & OPENFLAG_APPEND)   ret |= O_APPEND;
+               // TODO: Extra flags for F_GETFL
+               break; }
+       case F_SETFL: {
+               long    p_flags = va_arg(args, long);
+                int    a_flags = 0;
+               const int       mask = OPENFLAG_NONBLOCK|OPENFLAG_APPEND;
+               
+               if(p_flags & O_NONBLOCK)
+                       a_flags |= OPENFLAG_NONBLOCK;
+               if(p_flags & O_APPEND)
+                       a_flags |= OPENFLAG_APPEND;
+               // TODO: Extra flags for F_SETFL
+
+               ret = _SysFDFlags(fd, mask, a_flags);
+               if(ret != -1)
+                       ret = 0;
+
+               break; }
+       default:
+               _SysDebug("fcntl(%i) unknown or unimplimented", cmd);
+               errno = EINVAL;
+               ret = -1;
+               break;
+       }
+       va_end(args);
+       return ret;
+}
index bdb5422..f53ad93 100644 (file)
@@ -1,9 +1,9 @@
 /*
- * Acess2 C Library (UNIX Emulation)
+ * Acess2 POSIX Emulation Library
  * - By John Hodge (thePowersGang)
  *
  * fcntl.h
- * - ??
+ * - File descriptor control?
  */
 
 #ifndef _FCNTL_H_
@@ -32,7 +32,7 @@ enum e_fcntl_cmds
        F_GETLK,        // (struct flock *)
 };
 
-static inline int fcntl(int fd __attribute__((unused)), int cmd __attribute__((unused)), ...) { return -1; }
+extern int fcntl(int fd, int cmd, ...);
 
 #endif
 
index 57d7125..5f97440 100644 (file)
@@ -89,6 +89,7 @@ int execv(const char *b, char *v[])
 
 int dup(int oldfd)
 {
+       _SysDebug("libposix: dup() does not share offsets/flags");
        // NOTE: Acess's CopyFD doesn't cause offset sharing
        // TODO: Check that -1 does cause a new allocation
        return _SysCopyFD(oldfd, -1);
@@ -96,6 +97,7 @@ int dup(int oldfd)
 
 int dup2(int oldfd, int newfd)
 {
+       _SysDebug("libposix: dup2() does not share offsets/flags");
        // NOTE: Acess's CopyFD doesn't cause offset sharing
        return _SysCopyFD(oldfd, newfd);
 }

UCC git Repository :: git.ucc.asn.au