Check module name length in ModuleNext().
[wine/wine-kai.git] / scheduler / k32obj.c
blobe64cb89344dde0d326b32621c13fa6c937f5486e
1 /*
2 * KERNEL32 objects
4 * Copyright 1996 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include "winerror.h"
9 #include "k32obj.h"
10 #include "heap.h"
11 #include "process.h"
12 #include "server.h"
15 /* The declarations are here to avoid including a lot of unnecessary files */
16 extern const K32OBJ_OPS PROCESS_Ops;
17 extern const K32OBJ_OPS THREAD_Ops;
18 extern const K32OBJ_OPS MEM_MAPPED_FILE_Ops;
19 extern const K32OBJ_OPS CONSOLE_Ops;
20 extern const K32OBJ_OPS SNAPSHOT_Ops;
22 static const K32OBJ_OPS K32OBJ_NullOps =
24 NULL /* destroy */
27 static void K32OBJ_Destroy( K32OBJ *obj );
29 static const K32OBJ_OPS K32OBJ_DefaultOps =
31 K32OBJ_Destroy /* destroy */
35 const K32OBJ_OPS * const K32OBJ_Ops[K32OBJ_NBOBJECTS] =
37 NULL,
38 &K32OBJ_DefaultOps, /* K32OBJ_SEMAPHORE */
39 &K32OBJ_DefaultOps, /* K32OBJ_EVENT */
40 &K32OBJ_DefaultOps, /* K32OBJ_MUTEX */
41 &K32OBJ_NullOps, /* K32OBJ_CRITICAL_SECTION */
42 &PROCESS_Ops, /* K32OBJ_PROCESS */
43 &THREAD_Ops, /* K32OBJ_THREAD */
44 &K32OBJ_DefaultOps, /* K32OBJ_FILE */
45 &K32OBJ_DefaultOps, /* K32OBJ_CHANGE */
46 &CONSOLE_Ops, /* K32OBJ_CONSOLE */
47 &K32OBJ_NullOps, /* K32OBJ_SCREEN_BUFFER */
48 &MEM_MAPPED_FILE_Ops, /* K32OBJ_MEM_MAPPED_FILE */
49 &K32OBJ_NullOps, /* K32OBJ_SERIAL */
50 &K32OBJ_NullOps, /* K32OBJ_DEVICE_IOCTL */
51 &K32OBJ_DefaultOps, /* K32OBJ_PIPE */
52 &K32OBJ_NullOps, /* K32OBJ_MAILSLOT */
53 &K32OBJ_NullOps, /* K32OBJ_TOOLHELP_SNAPSHOT */
54 &K32OBJ_NullOps /* K32OBJ_SOCKET */
57 typedef struct _NE
59 struct _NE *next;
60 K32OBJ *obj;
61 UINT32 len;
62 char name[1];
63 } NAME_ENTRY;
65 static NAME_ENTRY *K32OBJ_FirstEntry = NULL;
68 /***********************************************************************
69 * K32OBJ_IncCount
71 void K32OBJ_IncCount( K32OBJ *ptr )
73 assert( ptr->type && ((unsigned)ptr->type < K32OBJ_NBOBJECTS) );
74 SYSTEM_LOCK();
75 ptr->refcount++;
76 SYSTEM_UNLOCK();
77 assert( ptr->refcount > 0 ); /* No wrap-around allowed */
81 /***********************************************************************
82 * K32OBJ_DecCount
84 void K32OBJ_DecCount( K32OBJ *ptr )
86 NAME_ENTRY **pptr;
88 assert( ptr->type && ((unsigned)ptr->type < K32OBJ_NBOBJECTS) );
89 assert( ptr->refcount > 0 );
90 SYSTEM_LOCK();
91 if (--ptr->refcount)
93 SYSTEM_UNLOCK();
94 return;
97 /* Check if the object has a name entry and free it */
99 pptr = &K32OBJ_FirstEntry;
100 while (*pptr && ((*pptr)->obj != ptr)) pptr = &(*pptr)->next;
101 if (*pptr)
103 NAME_ENTRY *entry = *pptr;
104 *pptr = entry->next;
105 HeapFree( SystemHeap, 0, entry );
108 /* Free the object */
110 if (K32OBJ_Ops[ptr->type]->destroy) K32OBJ_Ops[ptr->type]->destroy( ptr );
111 SYSTEM_UNLOCK();
115 /***********************************************************************
116 * K32OBJ_Destroy
118 * Generic destroy functions for objects that don't need any special treatment.
120 static void K32OBJ_Destroy( K32OBJ *obj )
122 obj->type = K32OBJ_UNKNOWN;
123 HeapFree( SystemHeap, 0, obj );
127 /***********************************************************************
128 * K32OBJ_IsValid
130 * Check if a pointer is a valid kernel object
132 BOOL32 K32OBJ_IsValid( K32OBJ *ptr, K32OBJ_TYPE type )
134 if (IsBadReadPtr32( ptr, sizeof(*ptr) )) return FALSE;
135 return (ptr->type == type);
139 /***********************************************************************
140 * K32OBJ_AddName
142 * Add a name entry for an object. We don't check for duplicates here.
143 * FIXME: should use some sort of hashing.
145 BOOL32 K32OBJ_AddName( K32OBJ *obj, LPCSTR name )
147 NAME_ENTRY *entry;
148 UINT32 len;
150 if (!name) return TRUE; /* Anonymous object */
151 len = strlen( name );
152 SYSTEM_LOCK();
153 if (!(entry = HeapAlloc( SystemHeap, 0, sizeof(NAME_ENTRY) + len )))
155 SYSTEM_UNLOCK();
156 SetLastError( ERROR_OUTOFMEMORY );
157 return FALSE;
159 entry->next = K32OBJ_FirstEntry;
160 entry->obj = obj;
161 entry->len = len;
162 lstrcpy32A( entry->name, name );
163 K32OBJ_FirstEntry = entry;
164 SYSTEM_UNLOCK();
165 return TRUE;
169 /***********************************************************************
170 * K32OBJ_Create
172 * Create a named kernel object.
173 * Returns NULL if there was an error _or_ if the object already existed.
174 * The refcount of the object must be decremented once it is initialized.
176 K32OBJ *K32OBJ_Create( K32OBJ_TYPE type, DWORD size, LPCSTR name, int server_handle,
177 DWORD access, SECURITY_ATTRIBUTES *sa, HANDLE32 *handle)
179 BOOL32 inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
181 /* Check if the name already exists */
183 K32OBJ *obj = K32OBJ_FindName( name );
184 if (obj)
186 if (obj->type == type)
188 SetLastError( ERROR_ALREADY_EXISTS );
189 *handle = HANDLE_Alloc( PROCESS_Current(), obj, access, inherit, server_handle );
191 else
193 SetLastError( ERROR_DUP_NAME );
194 *handle = INVALID_HANDLE_VALUE32;
195 if (server_handle != -1) CLIENT_CloseHandle( server_handle );
197 K32OBJ_DecCount( obj );
198 return NULL;
201 /* Create the object */
203 SYSTEM_LOCK();
204 if (!(obj = HeapAlloc( SystemHeap, 0, size )))
206 SYSTEM_UNLOCK();
207 *handle = INVALID_HANDLE_VALUE32;
208 if (server_handle != -1) CLIENT_CloseHandle( server_handle );
209 return NULL;
211 obj->type = type;
212 obj->refcount = 1;
214 /* Add a name for it */
216 if (!K32OBJ_AddName( obj, name ))
218 /* Don't call the destroy function, as the object wasn't
219 * initialized properly */
220 HeapFree( SystemHeap, 0, obj );
221 SYSTEM_UNLOCK();
222 *handle = INVALID_HANDLE_VALUE32;
223 if (server_handle != -1) CLIENT_CloseHandle( server_handle );
224 return NULL;
227 /* Allocate a handle */
229 *handle = HANDLE_Alloc( PROCESS_Current(), obj, access, inherit, server_handle );
230 SYSTEM_UNLOCK();
231 return obj;
235 /***********************************************************************
236 * K32OBJ_FindName
238 * Find the object referenced by a given name.
239 * The reference count is incremented.
241 K32OBJ *K32OBJ_FindName( LPCSTR name )
243 NAME_ENTRY *entry;
244 UINT32 len;
246 if (!name) return NULL; /* Anonymous object */
247 len = strlen( name );
248 SYSTEM_LOCK();
249 entry = K32OBJ_FirstEntry;
250 while (entry)
252 if ((len == entry->len) && !strcmp( name, entry->name))
254 K32OBJ *obj = entry->obj;
255 K32OBJ_IncCount( obj );
256 SYSTEM_UNLOCK();
257 return entry->obj;
259 entry = entry->next;
261 SYSTEM_UNLOCK();
262 return NULL;
266 /***********************************************************************
267 * K32OBJ_FindNameType
269 * Find an object by name and check its type.
270 * The reference count is incremented.
272 K32OBJ *K32OBJ_FindNameType( LPCSTR name, K32OBJ_TYPE type )
274 K32OBJ *obj = K32OBJ_FindName( name );
275 if (!obj) return NULL;
276 if (obj->type == type) return obj;
277 SetLastError( ERROR_DUP_NAME );
278 K32OBJ_DecCount( obj );
279 return NULL;