AcessNative - Fixing for recent kernel changes
[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                         return ent;
44                 }
45                 if( ent->PID > PID )    break;
46         }
47         
48         if(!bCreate)
49                 return NULL;
50         
51         ent = calloc( 1, sizeof(tUserHandles) );
52         ent->PID = PID;
53         if( prev ) {
54                 ent->Next = prev->Next;
55                 prev->Next = ent;
56         }
57         else {
58                 ent->Next = gpUserHandles;
59                 gpUserHandles = ent;
60         }
61         Log_Notice("VFS", "Created handle list for process %i", PID);
62         return ent;
63 }
64
65 /**
66  * \brief Clone the handle list of the current process into another
67  */
68 void VFS_CloneHandleList(int PID)
69 {
70         tUserHandles    *ent;
71         tUserHandles    *cur;
72          int    i, maxhandles;
73         
74         cur = VFS_int_GetUserHandles(Threads_GetPID(), 0);
75         if(!cur)        return ;        // Don't need to do anything if the current list is empty
76         
77         ent = VFS_int_GetUserHandles(PID, 1);
78         
79         maxhandles = *Threads_GetMaxFD();
80         memcpy(ent->Handles, cur->Handles, maxhandles*sizeof(tVFS_Handle));
81         
82         for( i = 0; i < maxhandles; i ++ )
83         {
84                 if(!cur->Handles[i].Node)       continue;
85                 
86                 if(ent->Handles[i].Node->Type->Reference)
87                         ent->Handles[i].Node->Type->Reference(ent->Handles[i].Node);
88         }
89 }
90
91 /**
92  * \fn tVFS_Handle *VFS_GetHandle(int FD)
93  * \brief Gets a pointer to the handle information structure
94  */
95 tVFS_Handle *VFS_GetHandle(int FD)
96 {
97         tVFS_Handle     *h;
98         
99         //Log_Debug("VFS", "VFS_GetHandle: (FD=0x%x)", FD);
100         
101         if(FD < 0) {
102                 LOG("FD (%i) < 0, RETURN NULL", FD);
103                 return NULL;
104         }
105         
106         if(FD & VFS_KERNEL_FLAG) {
107                 FD &= (VFS_KERNEL_FLAG - 1);
108                 if(FD >= MAX_KERNEL_FILES) {
109                         LOG("FD (%i) > MAX_KERNEL_FILES (%i), RETURN NULL", FD, MAX_KERNEL_FILES);
110                         return NULL;
111                 }
112                 h = &gaKernelHandles[ FD ];
113         }
114         else
115         {
116                 tUserHandles    *ent;
117                  int    pid = Threads_GetPID();
118                  int    maxhandles = *Threads_GetMaxFD();
119                 
120                 ent = VFS_int_GetUserHandles(pid, 0);
121                 if(!ent) {
122                         Log_Error("VFS", "Client %i does not have a handle list (>)", pid);
123                         return NULL;
124                 }
125                 
126                 if(FD >= maxhandles) {
127                         LOG("FD (%i) > Limit (%i), RETURN NULL", FD, maxhandles);
128                         return NULL;
129                 }
130                 h = &ent->Handles[ FD ];
131                 LOG("FD (%i) -> %p (Mode:0x%x,Node:%p)", FD, h, h->Mode, h->Node);
132         }
133         
134         if(h->Node == NULL) {
135                 LOG("FD (%i) Unused", FD);
136                 return NULL;
137         }
138         //Log_Debug("VFS", "VFS_GetHandle: RETURN %p", h);
139         return h;
140 }
141
142 int VFS_AllocHandle(int bIsUser, tVFS_Node *Node, int Mode)
143 {
144          int    i;
145         
146         // Check for a user open
147         if(bIsUser)
148         {
149                 tUserHandles    *ent;
150                  int    maxhandles = *Threads_GetMaxFD();
151                 // Find the PID's handle list
152                 ent = VFS_int_GetUserHandles(Threads_GetPID(), 1);
153                 // Get a handle
154                 for( i = 0; i < maxhandles; i ++ )
155                 {
156                         if(ent->Handles[i].Node)        continue;
157                         ent->Handles[i].Node = Node;
158                         ent->Handles[i].Position = 0;
159                         ent->Handles[i].Mode = Mode;
160                         return i;
161                 }
162         }
163         else
164         {
165                 // Get a handle
166                 for(i=0;i<MAX_KERNEL_FILES;i++)
167                 {
168                         if(gaKernelHandles[i].Node)     continue;
169                         gaKernelHandles[i].Node = Node;
170                         gaKernelHandles[i].Position = 0;
171                         gaKernelHandles[i].Mode = Mode;
172                         return i|VFS_KERNEL_FLAG;
173                 }
174         }
175         
176         return -1;
177 }

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