Tools/NetTest - Add TCP retransmit test
[tpg/acess2.git] / AcessNative / acesskernel_src / vfs_handle.c
1 /*
2  * Acess2 VFS
3  * - AllocHandle, GetHandle
4  */
5 #define DEBUG   0
6 #include <acess.h>
7 #include <vfs.h>
8 #include <vfs_int.h>
9 #include <vfs_ext.h>
10 #include <threads.h>
11
12 // === CONSTANTS ===
13 #define MAX_KERNEL_FILES        128
14 #define MAX_USER_FILES  64
15
16 // === IMPORTS ===
17 extern int      Server_GetClientID(void);
18
19 // === PROTOTYPES ===
20 tVFS_Handle     *VFS_GetHandle(int FD);
21  int    VFS_AllocHandle(int FD, tVFS_Node *Node, int Mode);
22
23 // === Types ===
24 typedef struct sUserHandles
25 {
26         struct sUserHandles     *Next;
27          int    PID;
28         tVFS_Handle     Handles[MAX_USER_FILES];
29 }       tUserHandles;
30
31 // === GLOBALS ===
32 tUserHandles    *gpUserHandles = NULL;
33 tVFS_Handle     gaKernelHandles[MAX_KERNEL_FILES];
34
35 // === CODE ===
36 tUserHandles *VFS_int_GetUserHandles(int PID, int bCreate)
37 {
38         tUserHandles    *ent, *prev = NULL;
39         for( ent = gpUserHandles; ent; prev = ent, ent = ent->Next ) {
40                 if( ent->PID == PID ) {
41                         //if( bCreate )
42                         //      Log_Warning("VFS", "Process %i already has a handle list", PID);
43                         LOG("Found list for %i", PID);
44                         return ent;
45                 }
46                 if( ent->PID > PID )    break;
47         }
48         
49         if(!bCreate) {
50                 LOG("Not creating for %i", PID);
51                 return NULL;
52         }
53         
54         ent = calloc( 1, sizeof(tUserHandles) );
55         ent->PID = PID;
56         if( prev ) {
57                 ent->Next = prev->Next;
58                 prev->Next = ent;
59         }
60         else {
61                 ent->Next = gpUserHandles;
62                 gpUserHandles = ent;
63         }
64         LOG("Created handle list for process %i", PID);
65         return ent;
66 }
67
68 /**
69  * \brief Clone the handle list of the current process into another
70  */
71 void VFS_CloneHandleList(int PID)
72 {
73         tUserHandles    *ent;
74         const tUserHandles      *cur;
75          int    maxhandles;
76         
77         cur = VFS_int_GetUserHandles(Threads_GetPID(), 0);
78         if(!cur)        return ;        // Don't need to do anything if the current list is empty
79         
80         ent = VFS_int_GetUserHandles(PID, 1);
81         
82         maxhandles = *Threads_GetMaxFD(NULL);
83         memcpy(ent->Handles, cur->Handles, maxhandles*sizeof(tVFS_Handle));
84         
85         // Reference all
86         for( int i = 0; i < maxhandles; i ++ )
87         {
88                 if(!ent->Handles[i].Node)       continue;
89                 
90                 if(ent->Handles[i].Node->Type->Reference)
91                         ent->Handles[i].Node->Type->Reference(ent->Handles[i].Node);
92         }
93 }
94
95 void VFS_CloneHandlesFromList(int PID, int nFD, int FDs[])
96 {
97         tUserHandles    *ent;
98         const tUserHandles      *cur;
99          int    maxhandles;
100         
101         cur = VFS_int_GetUserHandles(Threads_GetPID(), 0);
102         if(!cur)        return ;        // Don't need to do anything if the current list is empty
103         
104         ent = VFS_int_GetUserHandles(PID, 1);
105
106         LOG("Copying %i FDs from %i", nFD, PID);
107
108         maxhandles = *Threads_GetMaxFD(NULL);
109         if( nFD > maxhandles )
110                 nFD = maxhandles;
111         for( int i = 0; i < nFD; i ++ )
112         {
113                  int    fd = FDs[i];
114                 if( fd >= maxhandles ) {
115                         ent->Handles[i].Node = NULL;
116                         continue ;
117                 }
118                 memcpy(&ent->Handles[i], &cur->Handles[fd], sizeof(tVFS_Handle));
119         }
120         for( int i = nFD; i < maxhandles; i ++ )
121                 ent->Handles[i].Node = NULL;
122         
123         // Reference
124         for( int i = 0; i < maxhandles; i ++ )
125         {
126                 if(!ent->Handles[i].Node)       continue;
127                 
128                 if(ent->Handles[i].Node->Type->Reference)
129                         ent->Handles[i].Node->Type->Reference(ent->Handles[i].Node);
130         }
131 }
132
133 /**
134  * \fn tVFS_Handle *VFS_GetHandle(int FD)
135  * \brief Gets a pointer to the handle information structure
136  */
137 tVFS_Handle *VFS_GetHandle(int FD)
138 {
139         tVFS_Handle     *h;
140         
141         //Log_Debug("VFS", "VFS_GetHandle: (FD=0x%x)", FD);
142         
143         if(FD < 0) {
144                 LOG("FD (%i) < 0, RETURN NULL", FD);
145                 return NULL;
146         }
147         
148         if(FD & VFS_KERNEL_FLAG) {
149                 FD &= (VFS_KERNEL_FLAG - 1);
150                 if(FD >= MAX_KERNEL_FILES) {
151                         LOG("FD (%i) > MAX_KERNEL_FILES (%i), RETURN NULL", FD, MAX_KERNEL_FILES);
152                         return NULL;
153                 }
154                 h = &gaKernelHandles[ FD ];
155         }
156         else
157         {
158                  int    pid = Threads_GetPID();
159                  int    maxhandles = *Threads_GetMaxFD(NULL);
160                 
161                 tUserHandles *ent = VFS_int_GetUserHandles(pid, 0);
162                 if(!ent) {
163                         Log_Error("VFS", "Client %i does not have a handle list (>)", pid);
164                         return NULL;
165                 }
166                 
167                 if(FD >= maxhandles) {
168                         LOG("FD (%i) > Limit (%i), RETURN NULL", FD, maxhandles);
169                         return NULL;
170                 }
171                 h = &ent->Handles[ FD ];
172                 LOG("FD (%i) -> %p (Mode:0x%x,Node:%p)", FD, h, h->Mode, h->Node);
173         }
174         
175         if(h->Node == NULL) {
176                 LOG("FD (%i) Unused", FD);
177                 return NULL;
178         }
179         //Log_Debug("VFS", "VFS_GetHandle: RETURN %p", h);
180         return h;
181 }
182
183 int VFS_SetHandle(int FD, tVFS_Node *Node, int Mode)
184 {
185         tVFS_Handle     *h;
186         if(FD < 0)      return -1;
187         
188         if( FD & VFS_KERNEL_FLAG ) {
189                 FD &= (VFS_KERNEL_FLAG -1);
190                 if( FD >= MAX_KERNEL_FILES )    return -1;
191                 h = &gaKernelHandles[FD];
192         }
193         else {
194                 tUserHandles    *ent;
195                  int    pid = Threads_GetPID();
196                  int    maxhandles = *Threads_GetMaxFD(NULL);
197                 
198                 ent = VFS_int_GetUserHandles(pid, 0);
199                 if(!ent) {
200                         Log_Error("VFS", "Client %i does not have a handle list (>)", pid);
201                         return -1;
202                 }
203                 
204                 if(FD >= maxhandles) {
205                         LOG("FD (%i) > Limit (%i), RETURN NULL", FD, maxhandles);
206                         return -1;
207                 }
208                 h = &ent->Handles[ FD ];
209         }
210         h->Node = Node;
211         h->Mode = Mode;
212         return FD;
213 }
214
215
216 int VFS_AllocHandle(int bIsUser, tVFS_Node *Node, int Mode)
217 {
218         // Check for a user open
219         if(bIsUser)
220         {
221                 tUserHandles    *ent;
222                  int    maxhandles = *Threads_GetMaxFD(NULL);
223                 // Find the PID's handle list
224                 ent = VFS_int_GetUserHandles(Threads_GetPID(), 1);
225                 // Get a handle
226                 for( int i = 0; i < maxhandles; i ++ )
227                 {
228                         if(ent->Handles[i].Node)        continue;
229                         ent->Handles[i].Node = Node;
230                         ent->Handles[i].Position = 0;
231                         ent->Handles[i].Mode = Mode;
232                         return i;
233                 }
234         }
235         else
236         {
237                 // Get a handle
238                 for(int i = 0; i < MAX_KERNEL_FILES; i ++)
239                 {
240                         if(gaKernelHandles[i].Node)     continue;
241                         gaKernelHandles[i].Node = Node;
242                         gaKernelHandles[i].Position = 0;
243                         gaKernelHandles[i].Mode = Mode;
244                         return i|VFS_KERNEL_FLAG;
245                 }
246         }
247         
248         return -1;
249 }
250
251 void VFS_ClearHandles(int PID)
252 {
253         // Find the PID's handle list
254         tUserHandles *ent = VFS_int_GetUserHandles(PID, 0);
255         if( !ent )      return;
256         // Get a handle
257          int    maxhandles = *Threads_GetMaxFD(NULL);
258         for( int i = 0; i < maxhandles; i ++ )
259         {
260                 if(ent->Handles[i].Node)        continue;
261                 _CloseNode(ent->Handles[i].Node);
262                 ent->Handles[i].Node = NULL;
263         }
264 }

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