server: Make create_mutex use struct object_attributes and set the security descripto...
[wine] / server / mutex.c
1 /*
2  * Server-side mutex management
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "windef.h"
32 #include "winternl.h"
33
34 #include "handle.h"
35 #include "thread.h"
36 #include "request.h"
37 #include "security.h"
38
39 struct mutex
40 {
41     struct object  obj;             /* object header */
42     struct thread *owner;           /* mutex owner */
43     unsigned int   count;           /* recursion count */
44     int            abandoned;       /* has it been abandoned? */
45     struct list    entry;           /* entry in owner thread mutex list */
46 };
47
48 static void mutex_dump( struct object *obj, int verbose );
49 static int mutex_signaled( struct object *obj, struct thread *thread );
50 static int mutex_satisfied( struct object *obj, struct thread *thread );
51 static unsigned int mutex_map_access( struct object *obj, unsigned int access );
52 static void mutex_destroy( struct object *obj );
53 static int mutex_signal( struct object *obj, unsigned int access );
54
55 static const struct object_ops mutex_ops =
56 {
57     sizeof(struct mutex),      /* size */
58     mutex_dump,                /* dump */
59     add_queue,                 /* add_queue */
60     remove_queue,              /* remove_queue */
61     mutex_signaled,            /* signaled */
62     mutex_satisfied,           /* satisfied */
63     mutex_signal,              /* signal */
64     no_get_fd,                 /* get_fd */
65     mutex_map_access,          /* map_access */
66     default_get_sd,            /* get_sd */
67     default_set_sd,            /* set_sd */
68     no_lookup_name,            /* lookup_name */
69     no_open_file,              /* open_file */
70     no_close_handle,           /* close_handle */
71     mutex_destroy              /* destroy */
72 };
73
74
75 static struct mutex *create_mutex( struct directory *root, const struct unicode_str *name,
76                                    unsigned int attr, int owned, const struct security_descriptor *sd )
77 {
78     struct mutex *mutex;
79
80     if ((mutex = create_named_object_dir( root, name, attr, &mutex_ops )))
81     {
82         if (get_error() != STATUS_OBJECT_NAME_EXISTS)
83         {
84             /* initialize it if it didn't already exist */
85             mutex->count = 0;
86             mutex->owner = NULL;
87             mutex->abandoned = 0;
88             if (owned) mutex_satisfied( &mutex->obj, current );
89             if (sd) default_set_sd( &mutex->obj, sd, OWNER_SECURITY_INFORMATION|
90                                                      GROUP_SECURITY_INFORMATION|
91                                                      DACL_SECURITY_INFORMATION|
92                                                      SACL_SECURITY_INFORMATION );
93         }
94     }
95     return mutex;
96 }
97
98 /* release a mutex once the recursion count is 0 */
99 static void do_release( struct mutex *mutex )
100 {
101     assert( !mutex->count );
102     /* remove the mutex from the thread list of owned mutexes */
103     list_remove( &mutex->entry );
104     mutex->owner = NULL;
105     wake_up( &mutex->obj, 0 );
106 }
107
108 void abandon_mutexes( struct thread *thread )
109 {
110     struct list *ptr;
111
112     while ((ptr = list_head( &thread->mutex_list )) != NULL)
113     {
114         struct mutex *mutex = LIST_ENTRY( ptr, struct mutex, entry );
115         assert( mutex->owner == thread );
116         mutex->count = 0;
117         mutex->abandoned = 1;
118         do_release( mutex );
119     }
120 }
121
122 static void mutex_dump( struct object *obj, int verbose )
123 {
124     struct mutex *mutex = (struct mutex *)obj;
125     assert( obj->ops == &mutex_ops );
126     fprintf( stderr, "Mutex count=%u owner=%p ", mutex->count, mutex->owner );
127     dump_object_name( &mutex->obj );
128     fputc( '\n', stderr );
129 }
130
131 static int mutex_signaled( struct object *obj, struct thread *thread )
132 {
133     struct mutex *mutex = (struct mutex *)obj;
134     assert( obj->ops == &mutex_ops );
135     return (!mutex->count || (mutex->owner == thread));
136 }
137
138 static int mutex_satisfied( struct object *obj, struct thread *thread )
139 {
140     struct mutex *mutex = (struct mutex *)obj;
141     assert( obj->ops == &mutex_ops );
142     assert( !mutex->count || (mutex->owner == thread) );
143
144     if (!mutex->count++)  /* FIXME: avoid wrap-around */
145     {
146         assert( !mutex->owner );
147         mutex->owner = thread;
148         list_add_head( &thread->mutex_list, &mutex->entry );
149     }
150     if (!mutex->abandoned) return 0;
151     mutex->abandoned = 0;
152     return 1;
153 }
154
155 static unsigned int mutex_map_access( struct object *obj, unsigned int access )
156 {
157     if (access & GENERIC_READ)    access |= STANDARD_RIGHTS_READ | SYNCHRONIZE;
158     if (access & GENERIC_WRITE)   access |= STANDARD_RIGHTS_WRITE | MUTEX_MODIFY_STATE;
159     if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
160     if (access & GENERIC_ALL)     access |= STANDARD_RIGHTS_ALL | MUTEX_ALL_ACCESS;
161     return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
162 }
163
164 static int mutex_signal( struct object *obj, unsigned int access )
165 {
166     struct mutex *mutex = (struct mutex *)obj;
167     assert( obj->ops == &mutex_ops );
168
169     if (!(access & SYNCHRONIZE))  /* FIXME: MUTEX_MODIFY_STATE? */
170     {
171         set_error( STATUS_ACCESS_DENIED );
172         return 0;
173     }
174     if (!mutex->count || (mutex->owner != current))
175     {
176         set_error( STATUS_MUTANT_NOT_OWNED );
177         return 0;
178     }
179     if (!--mutex->count) do_release( mutex );
180     return 1;
181 }
182
183 static void mutex_destroy( struct object *obj )
184 {
185     struct mutex *mutex = (struct mutex *)obj;
186     assert( obj->ops == &mutex_ops );
187
188     if (!mutex->count) return;
189     mutex->count = 0;
190     do_release( mutex );
191 }
192
193 /* create a mutex */
194 DECL_HANDLER(create_mutex)
195 {
196     struct mutex *mutex;
197     struct unicode_str name;
198     struct directory *root = NULL;
199     const struct object_attributes *objattr = get_req_data();
200     const struct security_descriptor *sd;
201
202     reply->handle = 0;
203
204     if (!objattr_is_valid( objattr, get_req_data_size() ))
205         return;
206
207     sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
208
209     /* get unicode string */
210     name.len = ((get_req_data_size() - sizeof(*objattr) - objattr->sd_len) / sizeof(WCHAR)) * sizeof(WCHAR);
211     name.str = (const WCHAR *)get_req_data() + (sizeof(*objattr) + objattr->sd_len) / sizeof(WCHAR);
212
213     if (objattr->rootdir && !(root = get_directory_obj( current->process, objattr->rootdir, 0 )))
214         return;
215
216     if ((mutex = create_mutex( root, &name, req->attributes, req->owned, sd )))
217     {
218         reply->handle = alloc_handle( current->process, mutex, req->access, req->attributes );
219         release_object( mutex );
220     }
221
222     if (root) release_object( root );
223 }
224
225 /* open a handle to a mutex */
226 DECL_HANDLER(open_mutex)
227 {
228     struct unicode_str name;
229     struct directory *root = NULL;
230     struct mutex *mutex;
231
232     get_req_unicode_str( &name );
233     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
234         return;
235
236     if ((mutex = open_object_dir( root, &name, req->attributes, &mutex_ops )))
237     {
238         reply->handle = alloc_handle( current->process, &mutex->obj, req->access, req->attributes );
239         release_object( mutex );
240     }
241
242     if (root) release_object( root );
243 }
244
245 /* release a mutex */
246 DECL_HANDLER(release_mutex)
247 {
248     struct mutex *mutex;
249
250     if ((mutex = (struct mutex *)get_handle_obj( current->process, req->handle,
251                                                  MUTEX_MODIFY_STATE, &mutex_ops )))
252     {
253         if (!mutex->count || (mutex->owner != current)) set_error( STATUS_MUTANT_NOT_OWNED );
254         else
255         {
256             reply->prev_count = mutex->count;
257             if (!--mutex->count) do_release( mutex );
258         }
259         release_object( mutex );
260     }
261 }