AcessNative - Cleanup of VFS handle debug
[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();
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();
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                 tUserHandles    *ent;
159                  int    pid = Threads_GetPID();
160                  int    maxhandles = *Threads_GetMaxFD();
161                 
162                 ent = VFS_int_GetUserHandles(pid, 0);
163                 if(!ent) {
164                         Log_Error("VFS", "Client %i does not have a handle list (>)", pid);
165                         return NULL;
166                 }
167                 
168                 if(FD >= maxhandles) {
169                         LOG("FD (%i) > Limit (%i), RETURN NULL", FD, maxhandles);
170                         return NULL;
171                 }
172                 h = &ent->Handles[ FD ];
173                 LOG("FD (%i) -> %p (Mode:0x%x,Node:%p)", FD, h, h->Mode, h->Node);
174         }
175         
176         if(h->Node == NULL) {
177                 LOG("FD (%i) Unused", FD);
178                 return NULL;
179         }
180         //Log_Debug("VFS", "VFS_GetHandle: RETURN %p", h);
181         return h;
182 }
183
184 int VFS_SetHandle(int FD, tVFS_Node *Node, int Mode)
185 {
186         tVFS_Handle     *h;
187         if(FD < 0)      return -1;
188         
189         if( FD & VFS_KERNEL_FLAG ) {
190                 FD &= (VFS_KERNEL_FLAG -1);
191                 if( FD >= MAX_KERNEL_FILES )    return -1;
192                 h = &gaKernelHandles[FD];
193         }
194         else {
195                 tUserHandles    *ent;
196                  int    pid = Threads_GetPID();
197                  int    maxhandles = *Threads_GetMaxFD();
198                 
199                 ent = VFS_int_GetUserHandles(pid, 0);
200                 if(!ent) {
201                         Log_Error("VFS", "Client %i does not have a handle list (>)", pid);
202                         return -1;
203                 }
204                 
205                 if(FD >= maxhandles) {
206                         LOG("FD (%i) > Limit (%i), RETURN NULL", FD, maxhandles);
207                         return -1;
208                 }
209                 h = &ent->Handles[ FD ];
210         }
211         h->Node = Node;
212         h->Mode = Mode;
213         return FD;
214 }
215
216
217 int VFS_AllocHandle(int bIsUser, tVFS_Node *Node, int Mode)
218 {
219         // Check for a user open
220         if(bIsUser)
221         {
222                 tUserHandles    *ent;
223                  int    maxhandles = *Threads_GetMaxFD();
224                 // Find the PID's handle list
225                 ent = VFS_int_GetUserHandles(Threads_GetPID(), 1);
226                 // Get a handle
227                 for( int i = 0; i < maxhandles; i ++ )
228                 {
229                         if(ent->Handles[i].Node)        continue;
230                         ent->Handles[i].Node = Node;
231                         ent->Handles[i].Position = 0;
232                         ent->Handles[i].Mode = Mode;
233                         return i;
234                 }
235         }
236         else
237         {
238                 // Get a handle
239                 for(int i = 0; i < MAX_KERNEL_FILES; i ++)
240                 {
241                         if(gaKernelHandles[i].Node)     continue;
242                         gaKernelHandles[i].Node = Node;
243                         gaKernelHandles[i].Position = 0;
244                         gaKernelHandles[i].Mode = Mode;
245                         return i|VFS_KERNEL_FLAG;
246                 }
247         }
248         
249         return -1;
250 }

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