Kernel - Misc fixes and debug in VTerm/PTY, AxWin3 starts again
[tpg/acess2.git] / KernelLand / Kernel / drv / pty.c
index 6dc8f94..abfd9d1 100644 (file)
@@ -5,6 +5,7 @@
  * drv/pty.c
  * - Pseudo Terminals
  */
+#define DEBUG  1
 #include <acess.h>
 #include <vfs.h>
 #include <fs_devfs.h>
@@ -25,8 +26,11 @@ struct sPTY
        
        char    *Name;
         int    NumericName;
+       
        void    *OutputHandle;
        tPTY_OutputFcn  OutputFcn;
+       tPTY_ReqResize  ReqResize;
+       tPTY_ModeSet    ModeSet;
 
        struct ptymode  Mode;
        struct ptydims  Dims;
@@ -56,6 +60,7 @@ struct sPTY
  int   PTY_Install(char **Arguments);
  int   PTY_ReadDir(tVFS_Node *Node, int Pos, char Name[FILENAME_MAX]);
 tVFS_Node      *PTY_FindDir(tVFS_Node *Node, const char *Name, Uint Flags);
+tVFS_Node      *PTY_MkNod(tVFS_Node *Node, const char *Name, Uint Mode);
 
 size_t _rb_write(void *buf, size_t buflen, int *rd, int *wr, const void *data, size_t len);
 size_t _rb_read(void *buf, size_t buflen, int *rd, int *wr, void *data, size_t len);
@@ -77,7 +82,8 @@ MODULE_DEFINE(0, 0x100, PTY, PTY_Install, NULL, NULL);
 tVFS_NodeType  gPTY_NodeType_Root = {
        .TypeName = "PTY-Root",
        .ReadDir = PTY_ReadDir,
-       .FindDir = PTY_FindDir
+       .FindDir = PTY_FindDir,
+       .MkNod = PTY_MkNod
 };
 tVFS_NodeType  gPTY_NodeType_Client = {
        .TypeName = "PTY-Client",
@@ -93,6 +99,13 @@ tVFS_NodeType        gPTY_NodeType_Server = {
        .Write = PTY_WriteServer,
        .IOCtl = PTY_IOCtlServer,
        .Close = PTY_CloseServer
+};
+tDevFS_Driver  gPTY_Driver = {
+       .Name = "pts",
+       .RootNode = {
+               .Flags = VFS_FFLAG_DIRECTORY,
+               .Type = &gPTY_NodeType_Root
+       }
 };
  int   giPTY_NumCount;
 tRWLock        glPTY_NumPTYs;
@@ -104,11 +117,12 @@ tPTY      *gpPTY_FirstNamedPTY;
 // === CODE ===
 int PTY_Install(char **Arguments)
 {
+       DevFS_AddDevice(&gPTY_Driver);
        return MODULE_ERR_OK;
 }
 
 // --- Management ---
-tPTY *PTY_Create(const char *Name, void *Handle, tPTY_OutputFcn Output)
+tPTY *PTY_Create(const char *Name, void *Handle, tPTY_OutputFcn Output, tPTY_ReqResize ReqResize, tPTY_ModeSet ModeSet)
 {
        tPTY    **prev_np = NULL;
        size_t  namelen;
@@ -156,6 +170,10 @@ tPTY *PTY_Create(const char *Name, void *Handle, tPTY_OutputFcn Output)
        }
        
        tPTY *ret = calloc(sizeof(tPTY) + namelen + 1, 1);
+       if(!ret) {
+               errno = ENOMEM;
+               return NULL;
+       }
        
        // - List maintainance
        ret->Next = *prev_np;
@@ -170,6 +188,8 @@ tPTY *PTY_Create(const char *Name, void *Handle, tPTY_OutputFcn Output)
        // - Output function and handle (same again)
        ret->OutputHandle = Handle;
        ret->OutputFcn = Output;
+       ret->ReqResize = ReqResize;
+       ret->ModeSet = ModeSet;
        // - Server node
        ret->ServerNode.ImplPtr = ret;
        ret->ServerNode.Type = &gPTY_NodeType_Server;
@@ -177,7 +197,7 @@ tPTY *PTY_Create(const char *Name, void *Handle, tPTY_OutputFcn Output)
        ret->ServerNode.GID = Threads_GetGID();
        ret->ServerNode.NumACLs = 1;
        ret->ServerNode.ACLs = &ret->OwnerRW;
-       ret->ServerNode.ReferenceCount = (Output ? 1 : 0);      // Prevents a userland open/close killing a kernel pty
+       ret->ServerNode.ReferenceCount = (Output ? 1 : 0);      // Prevent a userland close killing a kernel pty
        // - Client node
        ret->ClientNode.ImplPtr = ret;
        ret->ClientNode.Type = &gPTY_NodeType_Client;
@@ -201,26 +221,59 @@ tPTY *PTY_Create(const char *Name, void *Handle, tPTY_OutputFcn Output)
        return ret;
 }
 
-void PTY_SetAttrib(tPTY *PTY, const struct ptydims *Dims, const struct ptymode *Mode, int WasClient)
+int PTY_SetAttrib(tPTY *PTY, const struct ptydims *Dims, const struct ptymode *Mode, int WasClient)
 {
-       if( Mode ) {
-               PTY->Mode = *Mode;
-               if( !WasClient && !PTY->OutputFcn )
+       if( Mode )
+       {
+               // (for now) userland terminals can't be put into framebuffer mode
+               if( !PTY->OutputFcn && (Mode->OutputMode & PTYOMODE_BUFFMT) == PTYBUFFMT_FB ) {
+                       errno = EINVAL;
+                       return -1;
+               }
+               if( WasClient )
                {
-                       Log_Warning("PTY", "TODO: Need to stop client output until modeset has been ACKed");
-                       // Block write until acked
-                       // ACK by server doing GETMODE
+                       if( PTY->ModeSet && PTY->ModeSet(PTY->OutputHandle, Mode) )
+                       {
+                               errno = EINVAL;
+                               return -1;
+                       }
+                       else if( !PTY->OutputFcn )
+                       {
+                               Log_Warning("PTY", "TODO: Need to stop client output until modeset has been ACKed");
+                               // Block write until acked
+                               // ACK by server doing GETMODE
+                       }
+               }
+               else
+               {
+                       // Should the client be informed that the server just twiddled the modes?
                }
+               LOG("PTY %p mode set to {0%o, 0%o}", PTY, Mode->InputMode, Mode->OutputMode);
+               PTY->Mode = *Mode;
        }
-       if( Dims ) {
-               PTY->Dims = *Dims;
-               if( WasClient ) {
+       if( Dims )
+       {
+               if( WasClient )
+               {
                        // Poke the server?
+                       if( PTY->ReqResize && PTY->ReqResize(PTY->OutputHandle, Dims) )
+                       {
+                               errno = EINVAL;
+                               return -1;
+                       }
+                       else if( !PTY->OutputFcn )
+                       {
+                               // Inform server process... somehow
+                       }
                }
-               else {
+               else
+               {
                        // SIGWINSZ to client
                }
+               LOG("PTY %p dims set to %ix%i", PTY, Dims->W, Dims->H);
+               PTY->Dims = *Dims;
        }
+       return 0;
 }
 
 void PTY_Close(tPTY *PTY)
@@ -230,8 +283,10 @@ void PTY_Close(tPTY *PTY)
 
 size_t _rb_write(void *buf, size_t buflen, int *rd, int *wr, const void *data, size_t len)
 {
-       size_t space = (*rd - *wr + buflen) % buflen - 1;
+       size_t space = (*rd - *wr + buflen - 1) % buflen;
+       ENTER("pbuf ibuflen prd pwr pdata ilen", buf, buflen, rd, wr, data, len);
        len = MIN(space, len);
+       LOG("space = %i, *rd = %i, *wr = %i", space, *rd, *wr);
        if(*wr + len >= buflen) {
                size_t prelen = buflen - *wr;
                memcpy((char*)buf + *wr, data, prelen);
@@ -242,6 +297,7 @@ size_t _rb_write(void *buf, size_t buflen, int *rd, int *wr, const void *data, s
                memcpy((char*)buf + *wr, data, len);
                *wr += len;
        }
+       LEAVE('i', len);
        return len;
 }
 size_t _rb_read(void *buf, size_t buflen, int *rd, int *wr, void *data, size_t len)
@@ -283,8 +339,9 @@ size_t PTY_int_SendInput(tPTY *PTY, const char *Input, size_t Length)
 {
        size_t  ret = 1, print = 1;
        
-       // Only counts for text input modes
-       if( (PTY->Mode.OutputMode & PTYOMODE_BUFFMT) == PTYBUFFMT_TEXT )
+       // Input mode stuff only counts for text output mode
+       // - Any other is Uint32 keypresses
+       if( (PTY->Mode.OutputMode & PTYOMODE_BUFFMT) != PTYBUFFMT_TEXT )
                return PTY_int_WriteInput(PTY, Input, Length);
        // If in raw mode, flush directlr
        if( (PTY->Mode.InputMode & PTYIMODE_RAW) )
@@ -297,6 +354,7 @@ size_t PTY_int_SendInput(tPTY *PTY, const char *Input, size_t Length)
                {
                case 3: // INTR - ^C
                        // TODO: Send SIGINT
+                       // Threads_PostSignalExt(PTY->ClientThreads, SIGINT);
                        print = 0;
                        break;
                case 4: // EOF - ^D
@@ -404,9 +462,9 @@ int PTY_ReadDir(tVFS_Node *Node, int Pos, char Name[FILENAME_MAX])
                return -1;
        
        if( pty->Name[0] )
-               snprintf(Name, 255, "%s%c", pty->Name, (Pos == 0 ? 'c' : 's'));
+               snprintf(Name, FILENAME_MAX, "%s%c", pty->Name, (Pos == 0 ? 'c' : 's'));
        else
-               snprintf(Name, 255, "%i%c", pty->NumericName, (Pos == 0 ? 'c' : 's'));
+               snprintf(Name, FILENAME_MAX, "%i%c", pty->NumericName, (Pos == 0 ? 'c' : 's'));
        return 0;
 }
 
@@ -461,6 +519,26 @@ tVFS_Node *PTY_FindDir(tVFS_Node *Node, const char *Name, Uint Flags)
                return NULL;
 }
 
+tVFS_Node *PTY_MkNod(tVFS_Node *Node, const char *Name, Uint Mode)
+{
+       // zero-length name means a numbered pty has been requested
+       if( Name[0] == '\0' || (Name[0] == '#' && Name[1] == '\0') )
+       {
+               tPTY    *ret = PTY_Create(NULL, NULL, NULL, NULL, NULL);
+               if( !ret )
+                       return NULL;
+               return &ret->ServerNode;
+       }
+       
+       // Otherwise return a named PTY
+       // TODO: Should the request be for '<name>s' or just '<name>'   
+
+       tPTY    *ret = PTY_Create(Name, NULL, NULL, NULL, NULL);
+       if(!ret)
+               return NULL;
+       return &ret->ServerNode;
+}
+
 //\! Read from the client's input
 size_t PTY_ReadClient(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer, Uint Flags)
 {
@@ -470,6 +548,13 @@ size_t PTY_ReadClient(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer
        tTime   timeout_z = 0, *timeout = (Flags & VFS_IOFLAG_NOBLOCK) ? &timeout_z : NULL;
         int    rv;
 _select:
+       // If server has disconnected, return EIO
+       if( pty->ServerNode.ReferenceCount == 0 ) {
+               //Threads_PostSignal(SIGPIPE);
+               errno = EIO;
+               return -1;
+       }
+       // Wait for data to be ready
        rv = VFS_SelectNode(Node, VFS_SELECT_READ, timeout, "PTY_ReadClient");
        if(!rv) {
                errno = (timeout ? EWOULDBLOCK : EINTR);
@@ -481,6 +566,9 @@ _select:
                Buffer, Length);
        Mutex_Release(&pty->InputMutex);
 
+       if(pty->InputReadPos == pty->InputWritePos)
+               VFS_MarkAvaliable(Node, 0);
+
        if(Length == 0 && !pty->HasHitEOF) {
                goto _select;
        }
@@ -493,11 +581,19 @@ _select:
 size_t PTY_WriteClient(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer, Uint Flags)
 {
        tPTY *pty = Node->ImplPtr;
-       
+
+       // If the server has terminated, send SIGPIPE
+       if( pty->ServerNode.ReferenceCount == 0 )
+       {
+               //Threads_PostSignal(SIGPIPE);
+               errno = EIO;
+               return -1;
+       }       
+
        // Write to either FIFO or directly to output function
        if( pty->OutputFcn )
        {
-               pty->OutputFcn(pty->OutputHandle, Buffer, Length, &pty->Dims);
+               pty->OutputFcn(pty->OutputHandle, Length, Buffer);
        }
        else
        {
@@ -520,7 +616,7 @@ int PTY_IOCtlClient(tVFS_Node *Node, int ID, void *Data)
        {
        case DRV_IOCTL_TYPE:    return DRV_TYPE_TERMINAL;
        case DRV_IOCTL_IDENT:   memcpy(Data, "PTY\0", 4);       return 0;
-       case DRV_IOCTL_VER:     return 0x100;
+       case DRV_IOCTL_VERSION: return 0x100;
        case DRV_IOCTL_LOOKUP:  return 0;
        
        case PTY_IOCTL_GETMODE:
@@ -529,17 +625,14 @@ int PTY_IOCtlClient(tVFS_Node *Node, int ID, void *Data)
                return 0;
        case PTY_IOCTL_SETMODE:
                if( !CheckMem(Data, sizeof(*mode)) ) { errno = EINVAL; return -1; }
-               PTY_SetAttrib(pty, NULL, mode, 1);
-               return 0;
+               return PTY_SetAttrib(pty, NULL, mode, 1);
        case PTY_IOCTL_GETDIMS:
                if( !CheckMem(Data, sizeof(*dims)) ) { errno = EINVAL; return -1; }
                *dims = pty->Dims;
                return 0;
        case PTY_IOCTL_SETDIMS:
                if( !CheckMem(Data, sizeof(*dims)) ) { errno = EINVAL; return -1; }
-               PTY_SetAttrib(pty, dims, NULL, 1);
-               // Inform the server?
-               return 0;
+               return PTY_SetAttrib(pty, dims, NULL, 1);
        }
        errno = ENOSYS;
        return -1;
@@ -549,7 +642,7 @@ void PTY_ReferenceClient(tVFS_Node *Node)
 {
        Node->ReferenceCount ++;
        // TODO: Add PID to list of client PIDs
-       Log_Notice("PTY", "TODO: List of client PIDs");
+//     Log_Notice("PTY", "ReferenceClient: TODO - List of client PIDs");
 }
 
 void PTY_CloseClient(tVFS_Node *Node)
@@ -558,6 +651,7 @@ void PTY_CloseClient(tVFS_Node *Node)
        Node->ReferenceCount --;
 
        // Remove PID from list
+       // TODO: Maintain list of client processes
 
        // Free structure if this was the last open handle
        if( Node->ReferenceCount == 0 && pty->ServerNode.ReferenceCount == 0 )
@@ -600,11 +694,7 @@ size_t PTY_ReadServer(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer
 //\! Write to the client's input
 size_t PTY_WriteServer(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer, Uint Flags)
 {
-       // Write to current line buffer, flushing on unknown character or newline
-       // - or line wrap?
-       // Echo back if instructed
-       PTY_SendInput(Node->ImplPtr, Buffer, Length);
-       return -1;
+       return PTY_SendInput(Node->ImplPtr, Buffer, Length);
 }
 
 int PTY_IOCtlServer(tVFS_Node *Node, int ID, void *Data)
@@ -616,7 +706,7 @@ int PTY_IOCtlServer(tVFS_Node *Node, int ID, void *Data)
        {
        case DRV_IOCTL_TYPE:    return DRV_TYPE_TERMINAL;
        case DRV_IOCTL_IDENT:   memcpy(Data, "PTY\0", 4);       return 0;
-       case DRV_IOCTL_VER:     return 0x100;
+       case DRV_IOCTL_VERSION: return 0x100;
        case DRV_IOCTL_LOOKUP:  return 0;
        
        case PTY_IOCTL_GETMODE:
@@ -636,6 +726,8 @@ int PTY_IOCtlServer(tVFS_Node *Node, int ID, void *Data)
                if( !CheckMem(Data, sizeof(*dims)) ) { errno = EINVAL; return -1; }
                PTY_SetAttrib(pty, dims, NULL, 0);
                break;
+       case PTY_IOCTL_GETID:
+               return pty->NumericName;
        }
        errno = ENOSYS;
        return -1;
@@ -643,8 +735,48 @@ int PTY_IOCtlServer(tVFS_Node *Node, int ID, void *Data)
 
 void PTY_CloseServer(tVFS_Node *Node)
 {
+       tPTY    *pty = Node->ImplPtr;
        // Dereference node
        Node->ReferenceCount --;
-       // If reference count == 0, remove from main list and SIGPIPE all clients when they write
+       // If reference count == 0, remove from main list
+       if( Node->ReferenceCount > 0 )
+               return ;
+       
+       // Locate on list and remove
+       tPTY    **prev_np;
+       if( pty->Name[0] ) {
+               RWLock_AcquireWrite(&glPTY_NamedPTYs);
+               prev_np = &gpPTY_FirstNamedPTY;
+       }
+       else {
+               RWLock_AcquireWrite(&glPTY_NumPTYs);
+               prev_np = &gpPTY_FirstNumPTY;
+       }
+
+       // Search list until *prev_np is equal to pty   
+       for( tPTY *tmp = *prev_np; *prev_np != pty && tmp; prev_np = &tmp->Next, tmp = tmp->Next )
+               ;
+       
+       // Remove
+       if( *prev_np != pty ) {
+               Log_Error("PTY", "PTY %p(%i/%s) not on list at deletion time", pty, pty->NumericName, pty->Name);
+       }
+       else {
+               *prev_np = pty->Next;
+       }
+       
+       // Clean up lock
+       if( pty->Name[0] ) {
+               RWLock_Release(&glPTY_NamedPTYs);
+               giPTY_NamedCount --;
+       }
+       else {
+               RWLock_Release(&glPTY_NumPTYs);
+               giPTY_NumCount --;
+       }
+
+       // If there are no open children, we can safely free this PTY
+       if( pty->ClientNode.ReferenceCount == 0 )
+               free(pty);
 }
 

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