X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=KernelLand%2FKernel%2Fdrv%2Fpty.c;h=937300343499dfc4516629d71f3f8ebe4f4388ab;hb=ea487239a8b3632d13c6e4ec92d21f0fcd460bdd;hp=8b63b119133665b35dd48e7131858594d198b142;hpb=79c17683913c5ebd7195714affa5567b69a66712;p=tpg%2Facess2.git diff --git a/KernelLand/Kernel/drv/pty.c b/KernelLand/Kernel/drv/pty.c index 8b63b119..93730034 100644 --- a/KernelLand/Kernel/drv/pty.c +++ b/KernelLand/Kernel/drv/pty.c @@ -13,6 +13,7 @@ #include #include #include +#include // === CONSTANTS === #define OUTPUT_RINGBUFFER_LEN 1024 // Number of bytes in output queue before client blocks @@ -52,7 +53,8 @@ struct sPTY tVFS_Node *ServerNode; tVFS_Node ClientNode; tVFS_ACL OwnerRW; - + + tPGID ControllingProcGroup; // TODO: Maintain list of client PIDs }; @@ -274,9 +276,7 @@ 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 - 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); @@ -287,7 +287,6 @@ 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) @@ -319,7 +318,7 @@ size_t PTY_int_WriteInput(tPTY *PTY, const char *Input, size_t Length) Mutex_Release(&PTY->InputMutex); VFS_MarkAvaliable(&PTY->ClientNode, 1); - if(ret < Length) + if(ret < Length && PTY->ServerNode) VFS_MarkFull(PTY->ServerNode, 1); return ret; @@ -442,12 +441,10 @@ int PTY_ReadDir(tVFS_Node *Node, int Pos, char Name[FILENAME_MAX]) } if( !pty ) { - LOG("%i out of range", Pos); return -1; } - strncpy(Name, pty->Name, FILENAME_MAX) - LOG("Return '%s'", Name); + strncpy(Name, pty->Name, FILENAME_MAX); return 0; } @@ -532,6 +529,8 @@ _select: Mutex_Acquire(&pty->InputMutex); Length = _rb_read(pty->InputData, INPUT_RINGBUFFER_LEN, &pty->InputReadPos, &pty->InputWritePos, Buffer, Length); + if( Length && pty->ServerNode ) + VFS_MarkFull(pty->ServerNode, 0); Mutex_Release(&pty->InputMutex); if(pty->InputReadPos == pty->InputWritePos) @@ -553,25 +552,35 @@ size_t PTY_WriteClient(tVFS_Node *Node, off_t Offset, size_t Length, const void // If the server has terminated, send SIGPIPE if( pty->ServerNode && pty->ServerNode->ReferenceCount == 0 ) { - //Threads_PostSignal(SIGPIPE); + Threads_PostSignal(SIGPIPE); errno = EIO; return -1; } // Write to either FIFO or directly to output function - if( pty->OutputFcn ) - { + if( pty->OutputFcn ) { pty->OutputFcn(pty->OutputHandle, Length, Buffer); + return Length; } - else - { - // Write to output ringbuffer - Length = _rb_write(pty->OutputData, OUTPUT_RINGBUFFER_LEN, - &pty->OutputReadPos, &pty->OutputWritePos, - Buffer, Length); - VFS_MarkAvaliable(pty->ServerNode, 1); + + // FIFO + tTime timeout_z, *timeout = (Flags & VFS_IOFLAG_NOBLOCK) ? &timeout_z : NULL; + int rv; + + rv = VFS_SelectNode(Node, VFS_SELECT_WRITE, timeout, "PTY_WriteClient"); + if(!rv) { + errno = (timeout ? EWOULDBLOCK : EINTR); + return -1; } + // Write to output ringbuffer + Length = _rb_write(pty->OutputData, OUTPUT_RINGBUFFER_LEN, + &pty->OutputReadPos, &pty->OutputWritePos, + Buffer, Length); + VFS_MarkAvaliable(pty->ServerNode, 1); + if( (pty->OutputWritePos + 1) % OUTPUT_RINGBUFFER_LEN == pty->OutputReadPos ) + VFS_MarkFull(Node, 1); + return Length; } @@ -605,6 +614,10 @@ void PTY_CloseClient(tVFS_Node *Node) size_t PTY_ReadServer(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer, Uint Flags) { tPTY *pty = Node->ImplPtr; + if( !pty ) { + errno = EIO; + return -1; + } // TODO: Prevent two servers fighting over client's output if( pty->OutputFcn ) @@ -634,7 +647,26 @@ 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) { - return PTY_SendInput(Node->ImplPtr, Buffer, Length); + tPTY *pty = Node->ImplPtr; + if( !pty ) { + errno = EIO; + return -1; + } + + tTime timeout_z = 0, *timeout = (Flags & VFS_IOFLAG_NOBLOCK) ? &timeout_z : NULL; + int rv = VFS_SelectNode(Node, VFS_SELECT_WRITE, timeout, "PTY_WriteServer"); + if(!rv) { + errno = (timeout ? EWOULDBLOCK : EINTR); + return -1; + } + size_t used = 0; + do { + used += PTY_SendInput(Node->ImplPtr, Buffer, Length); + } while( used < Length && !(Flags & VFS_IOFLAG_NOBLOCK) ); + + if( (pty->InputWritePos+1)%INPUT_RINGBUFFER_LEN == pty->InputReadPos ) + VFS_MarkFull(Node, 1); + return used; } void PTY_CloseServer(tVFS_Node *Node) @@ -648,7 +680,7 @@ void PTY_CloseServer(tVFS_Node *Node) // Locate on list and remove tPTY **prev_np; - if( pty->NumericName == 0 ) { + if( pty->NumericName == -1 ) { RWLock_AcquireWrite(&glPTY_NamedPTYs); prev_np = &gpPTY_FirstNamedPTY; } @@ -670,7 +702,7 @@ void PTY_CloseServer(tVFS_Node *Node) } // Clean up lock - if( pty->NumericName == 0 ) { + if( pty->NumericName == -1 ) { RWLock_Release(&glPTY_NamedPTYs); giPTY_NamedCount --; } @@ -679,6 +711,11 @@ void PTY_CloseServer(tVFS_Node *Node) giPTY_NumCount --; } + // Send SIGHUP to controling PGID + if( pty->ControllingProcGroup > 0 ) { + Threads_SignalGroup(pty->ControllingProcGroup, SIGHUP); + } + // If there are no open children, we can safely free this PTY if( pty->ClientNode.ReferenceCount == 0 ) { free(Node); @@ -743,6 +780,15 @@ int PTY_IOCtl(tVFS_Node *Node, int ID, void *Data) Node->ImplPtr = pty; pty->ServerNode = Node; return 0; + case PTY_IOCTL_SETPGRP: + // TODO: Should this only be done by client? + if( Data ) + { + if( !CheckMem(Data, sizeof(tPGID)) ) { errno = EINVAL; return -1; } + pty->ControllingProcGroup = *(tPGID*)Data; + Log_Debug("PTY", "Set controlling PGID to %i", pty->ControllingProcGroup); + } + return pty->ControllingProcGroup; } errno = ENOSYS; return -1;