3 * - By John Hodge (thePowersGang)
18 #define OUTPUT_RINGBUFFER_LEN 1024 // Number of bytes in output queue before client blocks
19 #define INPUT_RINGBUFFER_LEN 256 // Number of bytes in input queue before being dropped
20 #define INPUT_LINE_LEN 256
31 tPTY_OutputFcn OutputFcn;
32 tPTY_ReqResize ReqResize;
42 char InputData[INPUT_RINGBUFFER_LEN];
45 char LineData[INPUT_LINE_LEN];
50 char OutputData[OUTPUT_RINGBUFFER_LEN];
56 // TODO: Maintain list of client PIDs
60 int PTY_Install(char **Arguments);
61 int PTY_ReadDir(tVFS_Node *Node, int Pos, char Name[FILENAME_MAX]);
62 tVFS_Node *PTY_FindDir(tVFS_Node *Node, const char *Name, Uint Flags);
63 tVFS_Node *PTY_MkNod(tVFS_Node *Node, const char *Name, Uint Mode);
65 size_t _rb_write(void *buf, size_t buflen, int *rd, int *wr, const void *data, size_t len);
66 size_t _rb_read(void *buf, size_t buflen, int *rd, int *wr, void *data, size_t len);
67 size_t PTY_int_WriteInput(tPTY *PTY, const char *Input, size_t Length);
68 size_t PTY_int_SendInput(tPTY *PTY, const char *Input, size_t Length);
70 size_t PTY_ReadClient(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer, Uint Flags);
71 size_t PTY_WriteClient(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer, Uint Flags);
72 int PTY_IOCtlClient(tVFS_Node *Node, int ID, void *Arg);
73 void PTY_ReferenceClient(tVFS_Node *Node);
74 void PTY_CloseClient(tVFS_Node *Node);
75 size_t PTY_ReadServer(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer, Uint Flags);
76 size_t PTY_WriteServer(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer, Uint Flags);
77 int PTY_IOCtlServer(tVFS_Node *Node, int ID, void *Arg);
78 void PTY_CloseServer(tVFS_Node *Node);
81 MODULE_DEFINE(0, 0x100, PTY, PTY_Install, NULL, NULL);
82 tVFS_NodeType gPTY_NodeType_Root = {
83 .TypeName = "PTY-Root",
84 .ReadDir = PTY_ReadDir,
85 .FindDir = PTY_FindDir,
88 tVFS_NodeType gPTY_NodeType_Client = {
89 .TypeName = "PTY-Client",
90 .Read = PTY_ReadClient,
91 .Write = PTY_WriteClient,
92 .IOCtl = PTY_IOCtlClient,
93 .Reference = PTY_ReferenceClient,
94 .Close = PTY_CloseClient
96 tVFS_NodeType gPTY_NodeType_Server = {
97 .TypeName = "PTY-Server",
98 .Read = PTY_ReadServer,
99 .Write = PTY_WriteServer,
100 .IOCtl = PTY_IOCtlServer,
101 .Close = PTY_CloseServer
103 tDevFS_Driver gPTY_Driver = {
106 .Flags = VFS_FFLAG_DIRECTORY,
107 .Type = &gPTY_NodeType_Root
111 tRWLock glPTY_NumPTYs;
112 tPTY *gpPTY_FirstNumPTY;
113 int giPTY_NamedCount;
114 tRWLock glPTY_NamedPTYs;
115 tPTY *gpPTY_FirstNamedPTY;
118 int PTY_Install(char **Arguments)
120 DevFS_AddDevice(&gPTY_Driver);
121 return MODULE_ERR_OK;
124 // --- Management ---
125 tPTY *PTY_Create(const char *Name, void *Handle, tPTY_OutputFcn Output, tPTY_ReqResize ReqResize, tPTY_ModeSet ModeSet)
127 tPTY **prev_np = NULL;
130 if( Name && Name[0] )
132 prev_np = &gpPTY_FirstNamedPTY;
134 // Check the name isn't decimal
136 if( strtol(Name, &end, 10) != 0 && *end == '\0' ) {
141 RWLock_AcquireWrite(&glPTY_NamedPTYs);
143 for( tPTY *pty = gpPTY_FirstNamedPTY; pty; prev_np = &pty->Next, pty = pty->Next )
145 int cmp = strcmp(pty->Name, Name);
149 RWLock_Release(&glPTY_NamedPTYs);
155 namelen = strlen(Name);
160 RWLock_AcquireWrite(&glPTY_NumPTYs);
161 // Get a pty ID if Name==NULL
162 prev_np = &gpPTY_FirstNumPTY;
163 for( tPTY *pty = gpPTY_FirstNumPTY; pty; prev_np = &pty->Next, pty = pty->Next )
165 if( pty->NumericName > idx )
172 tPTY *ret = calloc(sizeof(tPTY) + namelen + 1, 1);
178 // - List maintainance
179 ret->Next = *prev_np;
181 // - PTY Name (Used by VT)
182 ret->Name = (char*)(ret + 1);
184 strcpy(ret->Name, Name);
187 ret->NumericName = idx;
188 // - Output function and handle (same again)
189 ret->OutputHandle = Handle;
190 ret->OutputFcn = Output;
191 ret->ReqResize = ReqResize;
192 ret->ModeSet = ModeSet;
194 ret->ServerNode.ImplPtr = ret;
195 ret->ServerNode.Type = &gPTY_NodeType_Server;
196 ret->ServerNode.UID = Threads_GetUID();
197 ret->ServerNode.GID = Threads_GetGID();
198 ret->ServerNode.NumACLs = 1;
199 ret->ServerNode.ACLs = &ret->OwnerRW;
200 ret->ServerNode.ReferenceCount = (Output ? 1 : 0); // Prevent a userland close killing a kernel pty
202 ret->ClientNode.ImplPtr = ret;
203 ret->ClientNode.Type = &gPTY_NodeType_Client;
204 ret->ClientNode.UID = Threads_GetUID();
205 ret->ClientNode.GID = Threads_GetGID();
206 ret->ClientNode.NumACLs = 1;
207 ret->ClientNode.ACLs = &ret->OwnerRW;
208 // - Owner Read-Write ACL
209 ret->OwnerRW.Ent.ID = Threads_GetUID();
210 ret->OwnerRW.Perm.Perms = -1;
212 if( Name && Name[0] ) {
214 RWLock_Release(&glPTY_NamedPTYs);
218 RWLock_Release(&glPTY_NumPTYs);
224 int PTY_SetAttrib(tPTY *PTY, const struct ptydims *Dims, const struct ptymode *Mode, int WasClient)
228 // (for now) userland terminals can't be put into framebuffer mode
229 if( !PTY->OutputFcn && (Mode->OutputMode & PTYOMODE_BUFFMT) == PTYBUFFMT_FB ) {
236 if( PTY->ModeSet && PTY->ModeSet(PTY->OutputHandle, Mode) )
241 else if( !PTY->OutputFcn )
243 Log_Warning("PTY", "TODO: Need to stop client output until modeset has been ACKed");
244 // Block write until acked
245 // ACK by server doing GETMODE
253 if( PTY->ReqResize && PTY->ReqResize(PTY->OutputHandle, Dims) )
258 else if( !PTY->OutputFcn )
260 // Inform server process... somehow
264 // SIGWINSZ to client
271 void PTY_Close(tPTY *PTY)
276 size_t _rb_write(void *buf, size_t buflen, int *rd, int *wr, const void *data, size_t len)
278 size_t space = (*rd - *wr + buflen - 1) % buflen;
279 ENTER("pbuf ibuflen prd pwr pdata ilen", buf, buflen, rd, wr, data, len);
280 len = MIN(space, len);
281 LOG("space = %i, *rd = %i, *wr = %i", space, *rd, *wr);
282 if(*wr + len >= buflen) {
283 size_t prelen = buflen - *wr;
284 memcpy((char*)buf + *wr, data, prelen);
285 memcpy(buf, (char*)data + prelen, len - prelen);
289 memcpy((char*)buf + *wr, data, len);
295 size_t _rb_read(void *buf, size_t buflen, int *rd, int *wr, void *data, size_t len)
297 size_t space = (*wr - *rd + buflen) % buflen;
298 len = MIN(space, len);
299 if(*rd + len >= buflen) {
300 size_t prelen = buflen - *rd;
301 memcpy(data, (char*)buf + *rd, prelen);
302 memcpy((char*)data + prelen, buf, len - prelen);
306 memcpy(data, (char*)buf + *rd, len);
312 size_t PTY_int_WriteInput(tPTY *PTY, const char *Input, size_t Length)
316 Mutex_Acquire(&PTY->InputMutex);
318 ret = _rb_write(PTY->InputData, INPUT_RINGBUFFER_LEN, &PTY->InputReadPos, &PTY->InputWritePos,
321 Mutex_Release(&PTY->InputMutex);
323 VFS_MarkAvaliable(&PTY->ClientNode, 1);
325 VFS_MarkFull(&PTY->ServerNode, 1);
330 size_t PTY_int_SendInput(tPTY *PTY, const char *Input, size_t Length)
332 size_t ret = 1, print = 1;
334 // Input mode stuff only counts for text output mode
335 // - Any other is Uint32 keypresses
336 if( (PTY->Mode.OutputMode & PTYOMODE_BUFFMT) != PTYBUFFMT_TEXT )
337 return PTY_int_WriteInput(PTY, Input, Length);
338 // If in raw mode, flush directlr
339 if( (PTY->Mode.InputMode & PTYIMODE_RAW) )
340 return PTY_int_WriteInput(PTY, Input, Length);
342 if( PTY->Mode.InputMode & PTYIMODE_CANON )
344 const char char_bs = '\b';
349 // Threads_PostSignalExt(PTY->ClientThreads, SIGINT);
353 PTY_int_WriteInput(PTY, PTY->LineData, PTY->LineLength);
354 PTY->HasHitEOF = (PTY->LineLength == 0);
359 if(PTY->LineLength != 0)
362 case 'w'-'a': // Word erase
363 while(PTY->LineLength != 0 && isalnum(PTY->LineData[--PTY->LineLength]))
364 PTY_WriteClient(&PTY->ClientNode, 0, 1, &char_bs, 0);
367 case 'u'-'a': // Kill
368 while(PTY->LineLength > 0)
369 PTY_WriteClient(&PTY->ClientNode, 0, 1, &char_bs, 0);
379 if(PTY->LineLength == INPUT_LINE_LEN) {
380 PTY_int_WriteInput(PTY, PTY->LineData, PTY->LineLength);
383 PTY->LineData[PTY->LineLength++] = '\n';
384 PTY_int_WriteInput(PTY, PTY->LineData, PTY->LineLength);
387 // TODO: Handle ^[[D and ^[[C for in-line editing, also ^[[1~/^[[4~ (home/end)
392 if(PTY->LineLength == INPUT_LINE_LEN) {
393 PTY_int_WriteInput(PTY, PTY->LineData, PTY->LineLength);
396 PTY->LineData[PTY->LineLength++] = Input[0];
402 ret = PTY_int_WriteInput(PTY, Input, Length);
406 if( PTY->Mode.InputMode & PTYIMODE_ECHO )
408 PTY_WriteClient(&PTY->ClientNode, 0, print, Input, 0);
414 size_t PTY_SendInput(tPTY *PTY, const char *Input, size_t Length)
417 while( ret < Length && !PTY->ClientNode.BufferFull )
419 // TODO: Detect blocking?
420 ret += PTY_int_SendInput(PTY, Input + ret, Length - ret);
426 int PTY_ReadDir(tVFS_Node *Node, int Pos, char Name[FILENAME_MAX])
429 if( Pos < giPTY_NumCount * 2 )
431 RWLock_AcquireRead(&glPTY_NumPTYs);
432 for( pty = gpPTY_FirstNumPTY; pty; pty = pty->Next )
438 RWLock_Release(&glPTY_NumPTYs);
440 else if( Pos < (giPTY_NumCount + giPTY_NamedCount) * 2 )
442 RWLock_AcquireRead(&glPTY_NamedPTYs);
443 for( pty = gpPTY_FirstNamedPTY; pty; pty = pty->Next )
449 RWLock_Release(&glPTY_NamedPTYs);
457 snprintf(Name, FILENAME_MAX, "%s%c", pty->Name, (Pos == 0 ? 'c' : 's'));
459 snprintf(Name, FILENAME_MAX, "%i%c", pty->NumericName, (Pos == 0 ? 'c' : 's'));
463 tVFS_Node *PTY_FindDir(tVFS_Node *Node, const char *Name, Uint Flags)
466 int num = strtol(Name, &end, 10);
468 if( Name[0] == '\0' || Name[1] == '\0' )
471 size_t len = strlen(Name);
472 if( Name[len-1] != 'c' && Name[len-1] != 's' )
474 int isServer = (Name[len-1] != 'c');
477 if( num && (end[0] == 'c' || end[0] == 's') && end[1] == '\0' )
480 RWLock_AcquireRead(&glPTY_NumPTYs);
481 for( tPTY *pty = gpPTY_FirstNumPTY; pty; pty = pty->Next )
483 if( pty->NumericName > num )
485 if( pty->NumericName == num ) {
490 RWLock_Release(&glPTY_NumPTYs);
495 RWLock_AcquireRead(&glPTY_NamedPTYs);
496 for( tPTY *pty = gpPTY_FirstNamedPTY; pty; pty = pty->Next )
498 int cmp = strncmp(pty->Name, Name, len-1);
501 if(cmp == 0 && pty->Name[len-1] == '\0' ) {
506 RWLock_Release(&glPTY_NamedPTYs);
509 return (isServer ? &ret->ServerNode : &ret->ClientNode);
514 tVFS_Node *PTY_MkNod(tVFS_Node *Node, const char *Name, Uint Mode)
516 // zero-length name means a numbered pty has been requested
517 if( Name[0] == '\0' || (Name[0] == '#' && Name[1] == '\0') )
519 tPTY *ret = PTY_Create(NULL, NULL, NULL, NULL, NULL);
522 return &ret->ServerNode;
525 // Otherwise return a named PTY
526 // TODO: Should the request be for '<name>s' or just '<name>'
528 tPTY *ret = PTY_Create(Name, NULL, NULL, NULL, NULL);
531 return &ret->ServerNode;
534 //\! Read from the client's input
535 size_t PTY_ReadClient(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer, Uint Flags)
537 tPTY *pty = Node->ImplPtr;
539 // Read from flushed queue
540 tTime timeout_z = 0, *timeout = (Flags & VFS_IOFLAG_NOBLOCK) ? &timeout_z : NULL;
543 // If server has disconnected, return EIO
544 if( pty->ServerNode.ReferenceCount == 0 ) {
545 //Threads_PostSignal(SIGPIPE);
549 // Wait for data to be ready
550 rv = VFS_SelectNode(Node, VFS_SELECT_READ, timeout, "PTY_ReadClient");
552 errno = (timeout ? EWOULDBLOCK : EINTR);
556 Mutex_Acquire(&pty->InputMutex);
557 Length = _rb_read(pty->InputData, INPUT_RINGBUFFER_LEN, &pty->InputReadPos, &pty->InputWritePos,
559 Mutex_Release(&pty->InputMutex);
561 if(pty->InputReadPos == pty->InputWritePos)
562 VFS_MarkAvaliable(Node, 0);
564 if(Length == 0 && !pty->HasHitEOF) {
572 //\! Write to the client's output
573 size_t PTY_WriteClient(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer, Uint Flags)
575 tPTY *pty = Node->ImplPtr;
577 // If the server has terminated, send SIGPIPE
578 if( pty->ServerNode.ReferenceCount == 0 )
580 //Threads_PostSignal(SIGPIPE);
585 // Write to either FIFO or directly to output function
588 pty->OutputFcn(pty->OutputHandle, Length, Buffer);
592 // Write to output ringbuffer
593 Length = _rb_write(pty->OutputData, OUTPUT_RINGBUFFER_LEN,
594 &pty->OutputReadPos, &pty->OutputWritePos,
596 VFS_MarkAvaliable(&pty->ServerNode, 1);
602 int PTY_IOCtlClient(tVFS_Node *Node, int ID, void *Data)
604 tPTY *pty = Node->ImplPtr;
605 struct ptymode *mode = Data;
606 struct ptydims *dims = Data;
609 case DRV_IOCTL_TYPE: return DRV_TYPE_TERMINAL;
610 case DRV_IOCTL_IDENT: memcpy(Data, "PTY\0", 4); return 0;
611 case DRV_IOCTL_VERSION: return 0x100;
612 case DRV_IOCTL_LOOKUP: return 0;
614 case PTY_IOCTL_GETMODE:
615 if( !CheckMem(Data, sizeof(*mode)) ) { errno = EINVAL; return -1; }
618 case PTY_IOCTL_SETMODE:
619 if( !CheckMem(Data, sizeof(*mode)) ) { errno = EINVAL; return -1; }
620 return PTY_SetAttrib(pty, NULL, mode, 1);
621 case PTY_IOCTL_GETDIMS:
622 if( !CheckMem(Data, sizeof(*dims)) ) { errno = EINVAL; return -1; }
625 case PTY_IOCTL_SETDIMS:
626 if( !CheckMem(Data, sizeof(*dims)) ) { errno = EINVAL; return -1; }
627 return PTY_SetAttrib(pty, dims, NULL, 1);
633 void PTY_ReferenceClient(tVFS_Node *Node)
635 Node->ReferenceCount ++;
636 // TODO: Add PID to list of client PIDs
637 // Log_Notice("PTY", "ReferenceClient: TODO - List of client PIDs");
640 void PTY_CloseClient(tVFS_Node *Node)
642 tPTY *pty = Node->ImplPtr;
643 Node->ReferenceCount --;
645 // Remove PID from list
646 // TODO: Maintain list of client processes
648 // Free structure if this was the last open handle
649 if( Node->ReferenceCount == 0 && pty->ServerNode.ReferenceCount == 0 )
651 // Free the structure! (Should be off the PTY list now)
656 //\! Read from the client's output
657 size_t PTY_ReadServer(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer, Uint Flags)
659 tPTY *pty = Node->ImplPtr;
661 // TODO: Prevent two servers fighting over client's output
664 // Kernel-land PTYs can't be read from userland
668 // Read back from fifo
669 tTime timeout_z = 0, *timeout = (Flags & VFS_IOFLAG_NOBLOCK) ? &timeout_z : NULL;
670 int rv = VFS_SelectNode(Node, VFS_SELECT_READ, timeout, "PTY_ReadServer");
672 errno = (timeout ? EWOULDBLOCK : EINTR);
676 Length = _rb_read(pty->OutputData, OUTPUT_RINGBUFFER_LEN,
677 &pty->OutputReadPos, &pty->OutputWritePos,
679 if( pty->OutputReadPos == pty->OutputWritePos )
680 VFS_MarkAvaliable(Node, 0);
681 VFS_MarkFull(&pty->ClientNode, 0);
686 //\! Write to the client's input
687 size_t PTY_WriteServer(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer, Uint Flags)
689 return PTY_SendInput(Node->ImplPtr, Buffer, Length);
692 int PTY_IOCtlServer(tVFS_Node *Node, int ID, void *Data)
694 tPTY *pty = Node->ImplPtr;
695 struct ptymode *mode = Data;
696 struct ptydims *dims = Data;
699 case DRV_IOCTL_TYPE: return DRV_TYPE_TERMINAL;
700 case DRV_IOCTL_IDENT: memcpy(Data, "PTY\0", 4); return 0;
701 case DRV_IOCTL_VERSION: return 0x100;
702 case DRV_IOCTL_LOOKUP: return 0;
704 case PTY_IOCTL_GETMODE:
705 if( !CheckMem(Data, sizeof(*mode)) ) { errno = EINVAL; return -1; }
707 // ACK client's SETMODE
709 case PTY_IOCTL_SETMODE:
710 if( !CheckMem(Data, sizeof(*mode)) ) { errno = EINVAL; return -1; }
711 PTY_SetAttrib(pty, NULL, mode, 0);
713 case PTY_IOCTL_GETDIMS:
714 if( !CheckMem(Data, sizeof(*dims)) ) { errno = EINVAL; return -1; }
717 case PTY_IOCTL_SETDIMS:
718 if( !CheckMem(Data, sizeof(*dims)) ) { errno = EINVAL; return -1; }
719 PTY_SetAttrib(pty, dims, NULL, 0);
721 case PTY_IOCTL_GETID:
722 return pty->NumericName;
728 void PTY_CloseServer(tVFS_Node *Node)
730 tPTY *pty = Node->ImplPtr;
732 Node->ReferenceCount --;
733 // If reference count == 0, remove from main list
734 if( Node->ReferenceCount > 0 )
737 // Locate on list and remove
740 RWLock_AcquireWrite(&glPTY_NamedPTYs);
741 prev_np = &gpPTY_FirstNamedPTY;
744 RWLock_AcquireWrite(&glPTY_NumPTYs);
745 prev_np = &gpPTY_FirstNumPTY;
748 // Search list until *prev_np is equal to pty
749 for( tPTY *tmp = *prev_np; *prev_np != pty && tmp; prev_np = &tmp->Next, tmp = tmp->Next )
753 if( *prev_np != pty ) {
754 Log_Error("PTY", "PTY %p(%i/%s) not on list at deletion time", pty, pty->NumericName, pty->Name);
757 *prev_np = pty->Next;
762 RWLock_Release(&glPTY_NamedPTYs);
766 RWLock_Release(&glPTY_NumPTYs);
770 // If there are no open children, we can safely free this PTY
771 if( pty->ClientNode.ReferenceCount == 0 )