10 #define DEFAULT_RING_SIZE 2048
14 typedef struct sPipe {
27 int FIFO_Install(char **Arguments);
28 int FIFO_IOCtl(tVFS_Node *Node, int Id, void *Data);
29 char *FIFO_ReadDir(tVFS_Node *Node, int Id);
30 tVFS_Node *FIFO_FindDir(tVFS_Node *Node, const char *Filename);
31 int FIFO_MkNod(tVFS_Node *Node, const char *Name, Uint Flags);
32 void FIFO_Close(tVFS_Node *Node);
33 int FIFO_Relink(tVFS_Node *Node, const char *OldName, const char *NewName);
34 Uint64 FIFO_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
35 Uint64 FIFO_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
36 tPipe *FIFO_Int_NewPipe(int Size, char *Name);
39 MODULE_DEFINE(0, 0x0032, FIFO, FIFO_Install, NULL, NULL);
40 tDevFS_Driver gFIFO_DriverInfo = {
45 .ACLs = &gVFS_ACL_EveryoneRW,
46 .Flags = VFS_FFLAG_DIRECTORY,
47 .ReadDir = FIFO_ReadDir,
48 .FindDir = FIFO_FindDir,
50 .Relink = FIFO_Relink,
54 tVFS_Node gFIFO_AnonNode = {
56 .ACLs = &gVFS_ACL_EveryoneRW,
58 tPipe *gFIFO_NamedPipes = NULL;
62 * \fn int FIFO_Install(char **Options)
63 * \brief Installs the FIFO Driver
65 int FIFO_Install(char **Options)
67 DevFS_AddDevice( &gFIFO_DriverInfo );
72 * \fn int FIFO_IOCtl(tVFS_Node *Node, int Id, void *Data)
74 int FIFO_IOCtl(tVFS_Node *Node, int Id, void *Data)
80 * \fn char *FIFO_ReadDir(tVFS_Node *Node, int Id)
81 * \brief Reads from the FIFO root
83 char *FIFO_ReadDir(tVFS_Node *Node, int Id)
85 tPipe *tmp = gFIFO_NamedPipes;
87 // Entry 0 is Anon Pipes
88 if(Id == 0) return strdup("anon");
90 // Find the id'th node
91 while(--Id && tmp) tmp = tmp->Next;
92 // If node found, return it
93 if(tmp) return strdup(tmp->Name);
99 * \fn tVFS_Node *FIFO_FindDir(tVFS_Node *Node, const char *Filename)
100 * \brief Find a file in the FIFO root
101 * \note Creates an anon pipe if anon is requested
103 tVFS_Node *FIFO_FindDir(tVFS_Node *Node, const char *Filename)
106 if(!Filename) return NULL;
109 if(Filename[0] == '\0') return NULL;
112 if(Filename[0] == 'a' && Filename[1] == 'n'
113 && Filename[2] == 'o' && Filename[3] == 'n'
114 && Filename[4] == '\0') {
115 tmp = FIFO_Int_NewPipe(DEFAULT_RING_SIZE, "anon");
120 tmp = gFIFO_NamedPipes;
123 if(strcmp(tmp->Name, Filename) == 0)
131 * \fn int FIFO_MkNod(tVFS_Node *Node, const char *Name, Uint Flags)
133 int FIFO_MkNod(tVFS_Node *Node, const char *Name, Uint Flags)
139 * \fn void FIFO_Close(tVFS_Node *Node)
140 * \brief Close a FIFO end
142 void FIFO_Close(tVFS_Node *Node)
145 if(!Node->ImplPtr) return ;
147 Node->ReferenceCount --;
148 if(Node->ReferenceCount) return ;
150 pipe = Node->ImplPtr;
152 if(strcmp(pipe->Name, "anon") == 0) {
161 * \fn int FIFO_Relink(tVFS_Node *Node, const char *OldName, const char *NewName)
162 * \brief Relink a file (Deletes named pipes)
164 int FIFO_Relink(tVFS_Node *Node, const char *OldName, const char *NewName)
168 if(Node != &gFIFO_DriverInfo.RootNode) return 0;
171 if(strcmp(OldName, "anon")) return 0;
174 for(pipe = gFIFO_NamedPipes;
178 if(strcmp(pipe->Name, OldName) == 0)
183 // Relink a named pipe
186 for(tmp = gFIFO_NamedPipes;
190 if(strcmp(tmp->Name, NewName) == 0) return 0;
194 pipe->Name = malloc(strlen(NewName)+1);
195 strcpy(pipe->Name, NewName);
209 * \fn Uint64 FIFO_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
210 * \brief Read from a fifo pipe
212 Uint64 FIFO_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
214 tPipe *pipe = Node->ImplPtr;
216 Uint remaining = Length;
222 // Wait for buffer to fill
223 if(pipe->Flags & PF_BLOCKING) {
224 len = Semaphore_Wait( &pipe->Semaphore, remaining );
228 if(pipe->ReadPos == pipe->WritePos)
231 if(pipe->WritePos - pipe->ReadPos < remaining)
232 len = pipe->WritePos - pipe->ReadPos;
237 // Check if read overflows buffer
238 if(len > pipe->BufSize - pipe->ReadPos)
240 int ofs = pipe->BufSize - pipe->ReadPos;
241 memcpy(Buffer, &pipe->Buffer[pipe->ReadPos], ofs);
242 memcpy(Buffer + ofs, &pipe->Buffer, len-ofs);
246 memcpy(Buffer, &pipe->Buffer[pipe->ReadPos], len);
249 // Increment read position
250 pipe->ReadPos += len;
251 pipe->ReadPos %= pipe->BufSize;
253 // Decrement Remaining Bytes
255 // Increment Buffer address
264 * \fn Uint64 FIFO_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
265 * \brief Write to a fifo pipe
267 Uint64 FIFO_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
269 tPipe *pipe = Node->ImplPtr;
271 Uint remaining = Length;
277 // Wait for buffer to empty
278 if(pipe->Flags & PF_BLOCKING) {
279 len = Semaphore_Signal( &pipe->Semaphore, remaining );
283 if(pipe->ReadPos == (pipe->WritePos+1)%pipe->BufSize)
286 if(pipe->ReadPos - pipe->WritePos < remaining)
287 len = pipe->ReadPos - pipe->WritePos;
292 // Check if write overflows buffer
293 if(len > pipe->BufSize - pipe->WritePos)
295 int ofs = pipe->BufSize - pipe->WritePos;
296 memcpy(&pipe->Buffer[pipe->WritePos], Buffer, ofs);
297 memcpy(&pipe->Buffer, Buffer + ofs, len-ofs);
301 memcpy(&pipe->Buffer[pipe->WritePos], Buffer, len);
304 // Increment read position
305 pipe->WritePos += len;
306 pipe->WritePos %= pipe->BufSize;
308 // Decrement Remaining Bytes
310 // Increment Buffer address
319 * \fn tPipe *FIFO_Int_NewPipe(int Size, char *Name)
320 * \brief Create a new pipe
322 tPipe *FIFO_Int_NewPipe(int Size, char *Name)
325 int namelen = strlen(Name) + 1;
326 int allocsize = sizeof(tPipe) + sizeof(tVFS_ACL) + Size + namelen;
328 ret = malloc(allocsize);
329 if(!ret) return NULL;
332 memset(ret, 0, allocsize);
333 ret->Flags = PF_BLOCKING;
337 ret->Buffer = (void*)( (Uint)ret + sizeof(tPipe) + sizeof(tVFS_ACL) );
339 // Set name (and FIFO name)
340 ret->Name = ret->Buffer + Size;
341 strcpy(ret->Name, Name);
342 // - Start empty, max of `Size`
343 Semaphore_Init( &ret->Semaphore, 0, Size, "FIFO", ret->Name );
347 ret->Node.ImplPtr = ret;
348 ret->Node.UID = Threads_GetUID();
349 ret->Node.GID = Threads_GetGID();
350 ret->Node.NumACLs = 1;
351 ret->Node.ACLs = (void*)( (Uint)ret + sizeof(tPipe) );
352 ret->Node.ACLs->Group = 0;
353 ret->Node.ACLs->ID = ret->Node.UID;
354 ret->Node.ACLs->Inv = 0;
355 ret->Node.ACLs->Perms = -1;
358 = ret->Node.ATime = now();
359 ret->Node.Read = FIFO_Read;
360 ret->Node.Write = FIFO_Write;
361 ret->Node.Close = FIFO_Close;