9 #define DEFAULT_RING_SIZE 2048
13 typedef struct sPipe {
25 int FIFO_Install(char **Arguments);
26 int FIFO_IOCtl(tVFS_Node *Node, int Id, void *Data);
27 char *FIFO_ReadDir(tVFS_Node *Node, int Id);
28 tVFS_Node *FIFO_FindDir(tVFS_Node *Node, const char *Filename);
29 int FIFO_MkNod(tVFS_Node *Node, const char *Name, Uint Flags);
30 void FIFO_Close(tVFS_Node *Node);
31 int FIFO_Relink(tVFS_Node *Node, const char *OldName, const char *NewName);
32 Uint64 FIFO_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
33 Uint64 FIFO_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
34 tPipe *FIFO_Int_NewPipe(int Size, char *Name);
37 MODULE_DEFINE(0, 0x0032, FIFO, FIFO_Install, NULL, NULL);
38 tDevFS_Driver gFIFO_DriverInfo = {
43 .ACLs = &gVFS_ACL_EveryoneRW,
44 .Flags = VFS_FFLAG_DIRECTORY,
45 .ReadDir = FIFO_ReadDir,
46 .FindDir = FIFO_FindDir,
48 .Relink = FIFO_Relink,
52 tVFS_Node gFIFO_AnonNode = {
54 .ACLs = &gVFS_ACL_EveryoneRW,
56 tPipe *gFIFO_NamedPipes = NULL;
60 * \fn int FIFO_Install(char **Options)
61 * \brief Installs the FIFO Driver
63 int FIFO_Install(char **Options)
65 DevFS_AddDevice( &gFIFO_DriverInfo );
70 * \fn int FIFO_IOCtl(tVFS_Node *Node, int Id, void *Data)
72 int FIFO_IOCtl(tVFS_Node *Node, int Id, void *Data)
78 * \fn char *FIFO_ReadDir(tVFS_Node *Node, int Id)
79 * \brief Reads from the FIFO root
81 char *FIFO_ReadDir(tVFS_Node *Node, int Id)
83 tPipe *tmp = gFIFO_NamedPipes;
85 // Entry 0 is Anon Pipes
86 if(Id == 0) return strdup("anon");
88 // Find the id'th node
89 while(--Id && tmp) tmp = tmp->Next;
90 // If node found, return it
91 if(tmp) return strdup(tmp->Name);
97 * \fn tVFS_Node *FIFO_FindDir(tVFS_Node *Node, const char *Filename)
98 * \brief Find a file in the FIFO root
99 * \note Creates an anon pipe if anon is requested
101 tVFS_Node *FIFO_FindDir(tVFS_Node *Node, const char *Filename)
104 if(!Filename) return NULL;
107 if(Filename[0] == '\0') return NULL;
110 if(Filename[0] == 'a' && Filename[1] == 'n'
111 && Filename[2] == 'o' && Filename[3] == 'n'
112 && Filename[4] == '\0') {
113 tmp = FIFO_Int_NewPipe(DEFAULT_RING_SIZE, "anon");
118 tmp = gFIFO_NamedPipes;
121 if(strcmp(tmp->Name, Filename) == 0)
129 * \fn int FIFO_MkNod(tVFS_Node *Node, const char *Name, Uint Flags)
131 int FIFO_MkNod(tVFS_Node *Node, const char *Name, Uint Flags)
137 * \fn void FIFO_Close(tVFS_Node *Node)
138 * \brief Close a FIFO end
140 void FIFO_Close(tVFS_Node *Node)
143 if(!Node->ImplPtr) return ;
145 Node->ReferenceCount --;
146 if(Node->ReferenceCount) return ;
148 pipe = Node->ImplPtr;
150 if(strcmp(pipe->Name, "anon") == 0) {
159 * \fn int FIFO_Relink(tVFS_Node *Node, const char *OldName, const char *NewName)
160 * \brief Relink a file (Deletes named pipes)
162 int FIFO_Relink(tVFS_Node *Node, const char *OldName, const char *NewName)
166 if(Node != &gFIFO_DriverInfo.RootNode) return 0;
169 if(strcmp(OldName, "anon")) return 0;
172 for(pipe = gFIFO_NamedPipes;
176 if(strcmp(pipe->Name, OldName) == 0)
181 // Relink a named pipe
184 for(tmp = gFIFO_NamedPipes;
188 if(strcmp(tmp->Name, NewName) == 0) return 0;
192 pipe->Name = malloc(strlen(NewName)+1);
193 strcpy(pipe->Name, NewName);
207 * \fn Uint64 FIFO_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
208 * \brief Read from a fifo pipe
210 Uint64 FIFO_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
212 tPipe *pipe = Node->ImplPtr;
214 Uint remaining = Length;
220 // Wait for buffer to fill
221 if(pipe->Flags & PF_BLOCKING) {
222 while(pipe->ReadPos == pipe->WritePos) {
228 if(pipe->ReadPos == pipe->WritePos)
232 if(pipe->WritePos - pipe->ReadPos < remaining)
233 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 while(pipe->ReadPos == (pipe->WritePos+1)%pipe->BufSize)
282 if(pipe->ReadPos == (pipe->WritePos+1)%pipe->BufSize)
286 if(pipe->ReadPos - pipe->WritePos < remaining)
287 len = pipe->ReadPos - pipe->WritePos;
291 // Check if write overflows buffer
292 if(len > pipe->BufSize - pipe->WritePos)
294 int ofs = pipe->BufSize - pipe->WritePos;
295 memcpy(&pipe->Buffer[pipe->WritePos], Buffer, ofs);
296 memcpy(&pipe->Buffer, Buffer + ofs, len-ofs);
300 memcpy(&pipe->Buffer[pipe->WritePos], Buffer, len);
303 // Increment read position
304 pipe->WritePos += len;
305 pipe->WritePos %= pipe->BufSize;
307 // Decrement Remaining Bytes
309 // Increment Buffer address
318 * \fn tPipe *FIFO_Int_NewPipe(int Size, char *Name)
319 * \brief Create a new pipe
321 tPipe *FIFO_Int_NewPipe(int Size, char *Name)
324 int allocsize = sizeof(tPipe) + sizeof(tVFS_ACL) + Size;
326 ret = malloc(allocsize);
327 if(!ret) return NULL;
330 memset(ret, 0, allocsize);
333 ret->Flags = PF_BLOCKING;
337 ret->Buffer = (void*)( (Uint)ret + sizeof(tPipe) + sizeof(tVFS_ACL) );
345 ret->Node.ImplPtr = ret;
346 ret->Node.UID = Threads_GetUID();
347 ret->Node.GID = Threads_GetGID();
348 ret->Node.NumACLs = 1;
349 ret->Node.ACLs = (void*)( (Uint)ret + sizeof(tPipe) );
350 ret->Node.ACLs->Group = 0;
351 ret->Node.ACLs->ID = ret->Node.UID;
352 ret->Node.ACLs->Inv = 0;
353 ret->Node.ACLs->Perms = -1;
356 = ret->Node.ATime = now();
357 ret->Node.Read = FIFO_Read;
358 ret->Node.Write = FIFO_Write;
359 ret->Node.Close = FIFO_Close;