Only include 'sys/user.h' for Linux. Fixes a compilation error on
[wine] / server / object.c
1 /*
2  * Server-side objects
3  * These are the server equivalent of K32OBJ
4  *
5  * Copyright (C) 1998 Alexandre Julliard
6  */
7
8 #include <assert.h>
9 #include <limits.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <unistd.h>
14
15 #include "thread.h"
16 #include "unicode.h"
17
18
19 struct object_name
20 {
21     struct object_name *next;
22     struct object_name *prev;
23     struct object      *obj;
24     size_t              len;
25     WCHAR               name[1];
26 };
27
28 #define NAME_HASH_SIZE 37
29
30 static struct object_name *names[NAME_HASH_SIZE];
31
32 #ifdef DEBUG_OBJECTS
33 static struct object *first;
34
35 void dump_objects(void)
36 {
37     struct object *ptr = first;
38     while (ptr)
39     {
40         fprintf( stderr, "%p:%d: ", ptr, ptr->refcount );
41         ptr->ops->dump( ptr, 1 );
42         ptr = ptr->next;
43     }
44 }
45 #endif
46
47 /*****************************************************************/
48
49 /* malloc replacement */
50 void *mem_alloc( size_t size )
51 {
52     void *ptr = malloc( size );
53     if (ptr) memset( ptr, 0x55, size );
54     else set_error( STATUS_NO_MEMORY );
55     return ptr;
56 }
57
58 /* duplicate a block of memory */
59 void *memdup( const void *data, size_t len )
60 {
61     void *ptr = malloc( len );
62     if (ptr) memcpy( ptr, data, len );
63     else set_error( STATUS_NO_MEMORY );
64     return ptr;
65 }
66
67
68 /*****************************************************************/
69
70 static int get_name_hash( const WCHAR *name, size_t len )
71 {
72     WCHAR hash = 0;
73     len /= sizeof(WCHAR);
74     while (len--) hash ^= *name++;
75     return hash % NAME_HASH_SIZE;
76 }
77
78 /* allocate a name for an object */
79 static struct object_name *alloc_name( const WCHAR *name, size_t len )
80 {
81     struct object_name *ptr;
82
83     if ((ptr = mem_alloc( sizeof(*ptr) + len - sizeof(ptr->name) )))
84     {
85         ptr->len = len;
86         memcpy( ptr->name, name, len );
87     }
88     return ptr;
89 }
90
91 /* free the name of an object */
92 static void free_name( struct object *obj )
93 {
94     struct object_name *ptr = obj->name;
95     if (ptr->next) ptr->next->prev = ptr->prev;
96     if (ptr->prev) ptr->prev->next = ptr->next;
97     else
98     {
99         int hash;
100         for (hash = 0; hash < NAME_HASH_SIZE; hash++)
101             if (names[hash] == ptr)
102             {
103                 names[hash] = ptr->next;
104                 break;
105             }
106     }
107     free( ptr );
108 }
109
110 /* set the name of an existing object */
111 static void set_object_name( struct object *obj, struct object_name *ptr )
112 {
113     int hash = get_name_hash( ptr->name, ptr->len );
114
115     if ((ptr->next = names[hash]) != NULL) ptr->next->prev = ptr;
116     ptr->obj = obj;
117     ptr->prev = NULL;
118     names[hash] = ptr;
119     assert( !obj->name );
120     obj->name = ptr;
121 }
122
123 /* allocate and initialize an object */
124 /* if the function fails the fd is closed */
125 void *alloc_object( const struct object_ops *ops, int fd )
126 {
127     struct object *obj = mem_alloc( ops->size );
128     if (obj)
129     {
130         obj->refcount = 1;
131         obj->fd       = fd;
132         obj->select   = -1;
133         obj->ops      = ops;
134         obj->head     = NULL;
135         obj->tail     = NULL;
136         obj->name     = NULL;
137         if ((fd != -1) && (add_select_user( obj ) == -1))
138         {
139             close( fd );
140             free( obj );
141             return NULL;
142         }
143 #ifdef DEBUG_OBJECTS
144         obj->prev = NULL;
145         if ((obj->next = first) != NULL) obj->next->prev = obj;
146         first = obj;
147 #endif
148         return obj;
149     }
150     if (fd != -1) close( fd );
151     return NULL;
152 }
153
154 void *create_named_object( const struct object_ops *ops, const WCHAR *name, size_t len )
155 {
156     struct object *obj;
157     struct object_name *name_ptr;
158
159     if (!name || !len) return alloc_object( ops, -1 );
160     if (!(name_ptr = alloc_name( name, len ))) return NULL;
161
162     if ((obj = find_object( name_ptr->name, name_ptr->len )))
163     {
164         free( name_ptr );  /* we no longer need it */
165         if (obj->ops == ops)
166         {
167             set_error( STATUS_OBJECT_NAME_COLLISION );
168             return obj;
169         }
170         set_error( STATUS_OBJECT_TYPE_MISMATCH );
171         return NULL;
172     }
173     if ((obj = alloc_object( ops, -1 )))
174     {
175         set_object_name( obj, name_ptr );
176         clear_error();
177     }
178     else free( name_ptr );
179     return obj;
180 }
181
182 /* dump the name of an object to stderr */
183 void dump_object_name( struct object *obj )
184 {
185     if (!obj->name) fprintf( stderr, "name=\"\"" );
186     else
187     {
188         fprintf( stderr, "name=L\"" );
189         dump_strW( obj->name->name, obj->name->len/sizeof(WCHAR), stderr, "\"\"" );
190         fputc( '\"', stderr );
191     }
192 }
193
194 /* grab an object (i.e. increment its refcount) and return the object */
195 struct object *grab_object( void *ptr )
196 {
197     struct object *obj = (struct object *)ptr;
198     assert( obj->refcount < INT_MAX );
199     obj->refcount++;
200     return obj;
201 }
202
203 /* release an object (i.e. decrement its refcount) */
204 void release_object( void *ptr )
205 {
206     struct object *obj = (struct object *)ptr;
207     assert( obj->refcount );
208     if (!--obj->refcount)
209     {
210         /* if the refcount is 0, nobody can be in the wait queue */
211         assert( !obj->head );
212         assert( !obj->tail );
213         obj->ops->destroy( obj );
214         if (obj->name) free_name( obj );
215         if (obj->select != -1) remove_select_user( obj );
216         if (obj->fd != -1) close( obj->fd );
217 #ifdef DEBUG_OBJECTS
218         if (obj->next) obj->next->prev = obj->prev;
219         if (obj->prev) obj->prev->next = obj->next;
220         else first = obj->next;
221         memset( obj, 0xaa, obj->ops->size );
222 #endif
223         free( obj );
224     }
225 }
226
227 /* find an object by its name; the refcount is incremented */
228 struct object *find_object( const WCHAR *name, size_t len )
229 {
230     struct object_name *ptr;
231
232     if (!name || !len) return NULL;
233     for (ptr = names[ get_name_hash( name, len ) ]; ptr; ptr = ptr->next)
234     {
235         if (ptr->len != len) continue;
236         if (!memcmp( ptr->name, name, len )) return grab_object( ptr->obj );
237     }
238     return NULL;
239 }
240
241 /* functions for unimplemented/default object operations */
242
243 int no_add_queue( struct object *obj, struct wait_queue_entry *entry )
244 {
245     set_error( STATUS_OBJECT_TYPE_MISMATCH );
246     return 0;
247 }
248
249 int no_satisfied( struct object *obj, struct thread *thread )
250 {
251     return 0;  /* not abandoned */
252 }
253
254 int no_get_fd( struct object *obj )
255 {
256     set_error( STATUS_OBJECT_TYPE_MISMATCH );
257     return -1;
258 }
259
260 int no_flush( struct object *obj )
261 {
262     set_error( STATUS_OBJECT_TYPE_MISMATCH );
263     return 0;
264 }
265
266 int no_get_file_info( struct object *obj, struct get_file_info_request *info )
267 {
268     set_error( STATUS_OBJECT_TYPE_MISMATCH );
269     return FD_TYPE_INVALID;
270 }
271
272 void no_destroy( struct object *obj )
273 {
274 }
275
276 /* default add_queue() routine for objects that poll() on an fd */
277 int default_poll_add_queue( struct object *obj, struct wait_queue_entry *entry )
278 {
279     if (!obj->head)  /* first on the queue */
280         set_select_events( obj, obj->ops->get_poll_events( obj ) );
281     add_queue( obj, entry );
282     return 1;
283 }
284
285 /* default remove_queue() routine for objects that poll() on an fd */
286 void default_poll_remove_queue( struct object *obj, struct wait_queue_entry *entry )
287 {
288     grab_object(obj);
289     remove_queue( obj, entry );
290     if (!obj->head)  /* last on the queue is gone */
291         set_select_events( obj, 0 );
292     release_object( obj );
293 }
294
295 /* default signaled() routine for objects that poll() on an fd */
296 int default_poll_signaled( struct object *obj, struct thread *thread )
297 {
298     int events = obj->ops->get_poll_events( obj );
299
300     if (check_select_events( obj->fd, events ))
301     {
302         /* stop waiting on select() if we are signaled */
303         set_select_events( obj, 0 );
304         return 1;
305     }
306     /* restart waiting on select() if we are no longer signaled */
307     if (obj->head) set_select_events( obj, events );
308     return 0;
309 }
310
311 /* default handler for poll() events */
312 void default_poll_event( struct object *obj, int event )
313 {
314     /* an error occurred, stop polling this fd to avoid busy-looping */
315     if (event & (POLLERR | POLLHUP)) set_select_events( obj, -1 );
316     wake_up( obj, 0 );
317 }