blob: 31a529aa3fa7f1dce567cbfc427048fc5c9c801d [file] [log] [blame]
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001/*
2 * Server-side mutex management
3 *
4 * Copyright (C) 1998 Alexandre Julliard
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00005 *
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
Jonathan Ernst360a3f92006-05-18 14:49:52 +020018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000019 */
20
Alexandre Julliard5769d1d2002-04-26 19:05:15 +000021#include "config.h"
22#include "wine/port.h"
23
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000024#include <assert.h>
25#include <stdio.h>
26#include <stdlib.h>
Robert Lunnon95414ef2005-11-22 12:01:05 +000027#include <stdarg.h>
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000028
Ge van Geldorp1a1583a2005-11-28 17:32:54 +010029#include "ntstatus.h"
30#define WIN32_NO_STATUS
Alexandre Julliard435e2e62002-12-10 22:56:43 +000031#include "windef.h"
Vitaliy Margolena9960002005-10-27 18:30:37 +000032#include "winternl.h"
Alexandre Julliard43c190e1999-05-15 10:48:19 +000033
34#include "handle.h"
35#include "thread.h"
Alexandre Julliard5bc78081999-06-22 17:26:53 +000036#include "request.h"
Rob Shearman1f863212007-10-25 15:43:05 +010037#include "security.h"
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000038
39struct 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? */
Alexandre Julliard20894e22005-02-25 16:58:43 +000045 struct list entry; /* entry in owner thread mutex list */
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000046};
47
Alexandre Julliard338e7571998-12-27 15:28:54 +000048static void mutex_dump( struct object *obj, int verbose );
Alexandre Julliard8382eb02007-12-05 18:16:42 +010049static struct object_type *mutex_get_type( struct object *obj );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000050static int mutex_signaled( struct object *obj, struct thread *thread );
51static int mutex_satisfied( struct object *obj, struct thread *thread );
Alexandre Julliard03f46e12005-12-12 14:58:44 +010052static unsigned int mutex_map_access( struct object *obj, unsigned int access );
Alexandre Julliarddbab5e21999-08-04 09:52:33 +000053static void mutex_destroy( struct object *obj );
Mike McCormackf92fff62005-04-24 17:35:52 +000054static int mutex_signal( struct object *obj, unsigned int access );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000055
56static const struct object_ops mutex_ops =
57{
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000058 sizeof(struct mutex), /* size */
59 mutex_dump, /* dump */
Alexandre Julliard8382eb02007-12-05 18:16:42 +010060 mutex_get_type, /* get_type */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000061 add_queue, /* add_queue */
62 remove_queue, /* remove_queue */
63 mutex_signaled, /* signaled */
64 mutex_satisfied, /* satisfied */
Mike McCormackf92fff62005-04-24 17:35:52 +000065 mutex_signal, /* signal */
Alexandre Julliard1ab243b2000-12-19 02:12:45 +000066 no_get_fd, /* get_fd */
Alexandre Julliard03f46e12005-12-12 14:58:44 +010067 mutex_map_access, /* map_access */
Rob Shearmanc1707d82007-10-03 13:10:37 +010068 default_get_sd, /* get_sd */
69 default_set_sd, /* set_sd */
Vitaliy Margolenbaffcb92005-11-22 14:55:42 +000070 no_lookup_name, /* lookup_name */
Alexandre Julliard7e71c1d2007-03-22 11:44:29 +010071 no_open_file, /* open_file */
Alexandre Julliardb9b1ea92005-06-09 15:39:52 +000072 no_close_handle, /* close_handle */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000073 mutex_destroy /* destroy */
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000074};
75
76
Vitaliy Margolenf676bc82005-12-02 15:55:48 +010077static struct mutex *create_mutex( struct directory *root, const struct unicode_str *name,
Rob Shearman1f863212007-10-25 15:43:05 +010078 unsigned int attr, int owned, const struct security_descriptor *sd )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000079{
80 struct mutex *mutex;
81
Vitaliy Margolenf676bc82005-12-02 15:55:48 +010082 if ((mutex = create_named_object_dir( root, name, attr, &mutex_ops )))
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000083 {
Vitaliy Margolen893987b2005-11-21 16:27:03 +000084 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
Alexandre Julliard5bc78081999-06-22 17:26:53 +000085 {
86 /* initialize it if it didn't already exist */
87 mutex->count = 0;
88 mutex->owner = NULL;
89 mutex->abandoned = 0;
Alexandre Julliard5bc78081999-06-22 17:26:53 +000090 if (owned) mutex_satisfied( &mutex->obj, current );
Rob Shearman1f863212007-10-25 15:43:05 +010091 if (sd) default_set_sd( &mutex->obj, sd, OWNER_SECURITY_INFORMATION|
92 GROUP_SECURITY_INFORMATION|
93 DACL_SECURITY_INFORMATION|
94 SACL_SECURITY_INFORMATION );
Alexandre Julliard5bc78081999-06-22 17:26:53 +000095 }
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000096 }
Alexandre Julliard5bc78081999-06-22 17:26:53 +000097 return mutex;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000098}
99
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000100/* release a mutex once the recursion count is 0 */
Alexandre Julliarddbab5e21999-08-04 09:52:33 +0000101static void do_release( struct mutex *mutex )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000102{
103 assert( !mutex->count );
104 /* remove the mutex from the thread list of owned mutexes */
Alexandre Julliard20894e22005-02-25 16:58:43 +0000105 list_remove( &mutex->entry );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000106 mutex->owner = NULL;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000107 wake_up( &mutex->obj, 0 );
108}
109
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000110void abandon_mutexes( struct thread *thread )
111{
Alexandre Julliard20894e22005-02-25 16:58:43 +0000112 struct list *ptr;
113
114 while ((ptr = list_head( &thread->mutex_list )) != NULL)
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000115 {
Alexandre Julliard20894e22005-02-25 16:58:43 +0000116 struct mutex *mutex = LIST_ENTRY( ptr, struct mutex, entry );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000117 assert( mutex->owner == thread );
118 mutex->count = 0;
119 mutex->abandoned = 1;
Alexandre Julliarddbab5e21999-08-04 09:52:33 +0000120 do_release( mutex );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000121 }
122}
123
Alexandre Julliard338e7571998-12-27 15:28:54 +0000124static void mutex_dump( struct object *obj, int verbose )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000125{
126 struct mutex *mutex = (struct mutex *)obj;
127 assert( obj->ops == &mutex_ops );
Alexandre Julliardd16319c1999-11-25 21:30:24 +0000128 fprintf( stderr, "Mutex count=%u owner=%p ", mutex->count, mutex->owner );
129 dump_object_name( &mutex->obj );
130 fputc( '\n', stderr );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000131}
132
Alexandre Julliard8382eb02007-12-05 18:16:42 +0100133static struct object_type *mutex_get_type( struct object *obj )
134{
135 static const WCHAR name[] = {'M','u','t','a','n','t'};
136 static const struct unicode_str str = { name, sizeof(name) };
137 return get_object_type( &str );
138}
139
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000140static int mutex_signaled( struct object *obj, struct thread *thread )
141{
142 struct mutex *mutex = (struct mutex *)obj;
143 assert( obj->ops == &mutex_ops );
144 return (!mutex->count || (mutex->owner == thread));
145}
146
147static int mutex_satisfied( struct object *obj, struct thread *thread )
148{
149 struct mutex *mutex = (struct mutex *)obj;
150 assert( obj->ops == &mutex_ops );
151 assert( !mutex->count || (mutex->owner == thread) );
152
153 if (!mutex->count++) /* FIXME: avoid wrap-around */
154 {
155 assert( !mutex->owner );
156 mutex->owner = thread;
Alexandre Julliard20894e22005-02-25 16:58:43 +0000157 list_add_head( &thread->mutex_list, &mutex->entry );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000158 }
159 if (!mutex->abandoned) return 0;
160 mutex->abandoned = 0;
161 return 1;
162}
163
Alexandre Julliard03f46e12005-12-12 14:58:44 +0100164static unsigned int mutex_map_access( struct object *obj, unsigned int access )
165{
166 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE;
167 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | MUTEX_MODIFY_STATE;
168 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
169 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL | MUTEX_ALL_ACCESS;
170 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
171}
172
Mike McCormackf92fff62005-04-24 17:35:52 +0000173static int mutex_signal( struct object *obj, unsigned int access )
174{
175 struct mutex *mutex = (struct mutex *)obj;
176 assert( obj->ops == &mutex_ops );
177
178 if (!(access & SYNCHRONIZE)) /* FIXME: MUTEX_MODIFY_STATE? */
179 {
180 set_error( STATUS_ACCESS_DENIED );
181 return 0;
182 }
183 if (!mutex->count || (mutex->owner != current))
184 {
185 set_error( STATUS_MUTANT_NOT_OWNED );
186 return 0;
187 }
188 if (!--mutex->count) do_release( mutex );
189 return 1;
190}
191
Alexandre Julliarddbab5e21999-08-04 09:52:33 +0000192static void mutex_destroy( struct object *obj )
193{
194 struct mutex *mutex = (struct mutex *)obj;
195 assert( obj->ops == &mutex_ops );
196
197 if (!mutex->count) return;
198 mutex->count = 0;
199 do_release( mutex );
200}
201
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000202/* create a mutex */
203DECL_HANDLER(create_mutex)
204{
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000205 struct mutex *mutex;
Alexandre Julliardead9b062005-11-18 16:31:18 +0000206 struct unicode_str name;
Vitaliy Margolenf676bc82005-12-02 15:55:48 +0100207 struct directory *root = NULL;
Rob Shearman1f863212007-10-25 15:43:05 +0100208 const struct object_attributes *objattr = get_req_data();
209 const struct security_descriptor *sd;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000210
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000211 reply->handle = 0;
Rob Shearman1f863212007-10-25 15:43:05 +0100212
213 if (!objattr_is_valid( objattr, get_req_data_size() ))
Vitaliy Margolenf676bc82005-12-02 15:55:48 +0100214 return;
215
Rob Shearman1f863212007-10-25 15:43:05 +0100216 sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
Rob Shearmanf98556c2007-10-26 17:01:33 +0100217 objattr_get_name( objattr, &name );
Rob Shearman1f863212007-10-25 15:43:05 +0100218
219 if (objattr->rootdir && !(root = get_directory_obj( current->process, objattr->rootdir, 0 )))
220 return;
221
222 if ((mutex = create_mutex( root, &name, req->attributes, req->owned, sd )))
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000223 {
Rob Shearman92db6d22007-11-05 14:23:36 +0000224 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
225 reply->handle = alloc_handle( current->process, mutex, req->access, req->attributes );
226 else
227 reply->handle = alloc_handle_no_access_check( current->process, mutex, req->access, req->attributes );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000228 release_object( mutex );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000229 }
Vitaliy Margolenf676bc82005-12-02 15:55:48 +0100230
231 if (root) release_object( root );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000232}
233
234/* open a handle to a mutex */
235DECL_HANDLER(open_mutex)
236{
Alexandre Julliardead9b062005-11-18 16:31:18 +0000237 struct unicode_str name;
Vitaliy Margolenf676bc82005-12-02 15:55:48 +0100238 struct directory *root = NULL;
Alexandre Julliard3764da62005-12-05 12:52:05 +0100239 struct mutex *mutex;
Alexandre Julliardead9b062005-11-18 16:31:18 +0000240
241 get_req_unicode_str( &name );
Vitaliy Margolenf676bc82005-12-02 15:55:48 +0100242 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
243 return;
244
Alexandre Julliard3764da62005-12-05 12:52:05 +0100245 if ((mutex = open_object_dir( root, &name, req->attributes, &mutex_ops )))
246 {
Alexandre Julliard24560e72005-12-09 13:58:25 +0100247 reply->handle = alloc_handle( current->process, &mutex->obj, req->access, req->attributes );
Alexandre Julliard3764da62005-12-05 12:52:05 +0100248 release_object( mutex );
249 }
Vitaliy Margolenf676bc82005-12-02 15:55:48 +0100250
251 if (root) release_object( root );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000252}
253
254/* release a mutex */
255DECL_HANDLER(release_mutex)
256{
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000257 struct mutex *mutex;
258
259 if ((mutex = (struct mutex *)get_handle_obj( current->process, req->handle,
260 MUTEX_MODIFY_STATE, &mutex_ops )))
261 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000262 if (!mutex->count || (mutex->owner != current)) set_error( STATUS_MUTANT_NOT_OWNED );
Eric Pouech44158dd2004-12-02 18:05:37 +0000263 else
264 {
265 reply->prev_count = mutex->count;
266 if (!--mutex->count) do_release( mutex );
267 }
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000268 release_object( mutex );
269 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000270}