Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Server-side mutex management |
| 3 | * |
| 4 | * Copyright (C) 1998 Alexandre Julliard |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 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 |
Jonathan Ernst | 360a3f9 | 2006-05-18 14:49:52 +0200 | [diff] [blame] | 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
Alexandre Julliard | 5769d1d | 2002-04-26 19:05:15 +0000 | [diff] [blame] | 21 | #include "config.h" |
| 22 | #include "wine/port.h" |
| 23 | |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 24 | #include <assert.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
Robert Lunnon | 95414ef | 2005-11-22 12:01:05 +0000 | [diff] [blame] | 27 | #include <stdarg.h> |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 28 | |
Ge van Geldorp | 1a1583a | 2005-11-28 17:32:54 +0100 | [diff] [blame] | 29 | #include "ntstatus.h" |
| 30 | #define WIN32_NO_STATUS |
Alexandre Julliard | 435e2e6 | 2002-12-10 22:56:43 +0000 | [diff] [blame] | 31 | #include "windef.h" |
Vitaliy Margolen | a996000 | 2005-10-27 18:30:37 +0000 | [diff] [blame] | 32 | #include "winternl.h" |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 33 | |
| 34 | #include "handle.h" |
| 35 | #include "thread.h" |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 36 | #include "request.h" |
Rob Shearman | 1f86321 | 2007-10-25 15:43:05 +0100 | [diff] [blame] | 37 | #include "security.h" |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 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? */ |
Alexandre Julliard | 20894e2 | 2005-02-25 16:58:43 +0000 | [diff] [blame] | 45 | struct list entry; /* entry in owner thread mutex list */ |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 48 | static void mutex_dump( struct object *obj, int verbose ); |
Alexandre Julliard | 8382eb0 | 2007-12-05 18:16:42 +0100 | [diff] [blame] | 49 | static struct object_type *mutex_get_type( struct object *obj ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 50 | static int mutex_signaled( struct object *obj, struct thread *thread ); |
| 51 | static int mutex_satisfied( struct object *obj, struct thread *thread ); |
Alexandre Julliard | 03f46e1 | 2005-12-12 14:58:44 +0100 | [diff] [blame] | 52 | static unsigned int mutex_map_access( struct object *obj, unsigned int access ); |
Alexandre Julliard | dbab5e2 | 1999-08-04 09:52:33 +0000 | [diff] [blame] | 53 | static void mutex_destroy( struct object *obj ); |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 54 | static int mutex_signal( struct object *obj, unsigned int access ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 55 | |
| 56 | static const struct object_ops mutex_ops = |
| 57 | { |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 58 | sizeof(struct mutex), /* size */ |
| 59 | mutex_dump, /* dump */ |
Alexandre Julliard | 8382eb0 | 2007-12-05 18:16:42 +0100 | [diff] [blame] | 60 | mutex_get_type, /* get_type */ |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 61 | add_queue, /* add_queue */ |
| 62 | remove_queue, /* remove_queue */ |
| 63 | mutex_signaled, /* signaled */ |
| 64 | mutex_satisfied, /* satisfied */ |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 65 | mutex_signal, /* signal */ |
Alexandre Julliard | 1ab243b | 2000-12-19 02:12:45 +0000 | [diff] [blame] | 66 | no_get_fd, /* get_fd */ |
Alexandre Julliard | 03f46e1 | 2005-12-12 14:58:44 +0100 | [diff] [blame] | 67 | mutex_map_access, /* map_access */ |
Rob Shearman | c1707d8 | 2007-10-03 13:10:37 +0100 | [diff] [blame] | 68 | default_get_sd, /* get_sd */ |
| 69 | default_set_sd, /* set_sd */ |
Vitaliy Margolen | baffcb9 | 2005-11-22 14:55:42 +0000 | [diff] [blame] | 70 | no_lookup_name, /* lookup_name */ |
Alexandre Julliard | 7e71c1d | 2007-03-22 11:44:29 +0100 | [diff] [blame] | 71 | no_open_file, /* open_file */ |
Alexandre Julliard | b9b1ea9 | 2005-06-09 15:39:52 +0000 | [diff] [blame] | 72 | no_close_handle, /* close_handle */ |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 73 | mutex_destroy /* destroy */ |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | |
Vitaliy Margolen | f676bc8 | 2005-12-02 15:55:48 +0100 | [diff] [blame] | 77 | static struct mutex *create_mutex( struct directory *root, const struct unicode_str *name, |
Rob Shearman | 1f86321 | 2007-10-25 15:43:05 +0100 | [diff] [blame] | 78 | unsigned int attr, int owned, const struct security_descriptor *sd ) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 79 | { |
| 80 | struct mutex *mutex; |
| 81 | |
Vitaliy Margolen | f676bc8 | 2005-12-02 15:55:48 +0100 | [diff] [blame] | 82 | if ((mutex = create_named_object_dir( root, name, attr, &mutex_ops ))) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 83 | { |
Vitaliy Margolen | 893987b | 2005-11-21 16:27:03 +0000 | [diff] [blame] | 84 | if (get_error() != STATUS_OBJECT_NAME_EXISTS) |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 85 | { |
| 86 | /* initialize it if it didn't already exist */ |
| 87 | mutex->count = 0; |
| 88 | mutex->owner = NULL; |
| 89 | mutex->abandoned = 0; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 90 | if (owned) mutex_satisfied( &mutex->obj, current ); |
Rob Shearman | 1f86321 | 2007-10-25 15:43:05 +0100 | [diff] [blame] | 91 | if (sd) default_set_sd( &mutex->obj, sd, OWNER_SECURITY_INFORMATION| |
| 92 | GROUP_SECURITY_INFORMATION| |
| 93 | DACL_SECURITY_INFORMATION| |
| 94 | SACL_SECURITY_INFORMATION ); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 95 | } |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 96 | } |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 97 | return mutex; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 100 | /* release a mutex once the recursion count is 0 */ |
Alexandre Julliard | dbab5e2 | 1999-08-04 09:52:33 +0000 | [diff] [blame] | 101 | static void do_release( struct mutex *mutex ) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 102 | { |
| 103 | assert( !mutex->count ); |
| 104 | /* remove the mutex from the thread list of owned mutexes */ |
Alexandre Julliard | 20894e2 | 2005-02-25 16:58:43 +0000 | [diff] [blame] | 105 | list_remove( &mutex->entry ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 106 | mutex->owner = NULL; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 107 | wake_up( &mutex->obj, 0 ); |
| 108 | } |
| 109 | |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 110 | void abandon_mutexes( struct thread *thread ) |
| 111 | { |
Alexandre Julliard | 20894e2 | 2005-02-25 16:58:43 +0000 | [diff] [blame] | 112 | struct list *ptr; |
| 113 | |
| 114 | while ((ptr = list_head( &thread->mutex_list )) != NULL) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 115 | { |
Alexandre Julliard | 20894e2 | 2005-02-25 16:58:43 +0000 | [diff] [blame] | 116 | struct mutex *mutex = LIST_ENTRY( ptr, struct mutex, entry ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 117 | assert( mutex->owner == thread ); |
| 118 | mutex->count = 0; |
| 119 | mutex->abandoned = 1; |
Alexandre Julliard | dbab5e2 | 1999-08-04 09:52:33 +0000 | [diff] [blame] | 120 | do_release( mutex ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 124 | static void mutex_dump( struct object *obj, int verbose ) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 125 | { |
| 126 | struct mutex *mutex = (struct mutex *)obj; |
| 127 | assert( obj->ops == &mutex_ops ); |
Alexandre Julliard | d16319c | 1999-11-25 21:30:24 +0000 | [diff] [blame] | 128 | fprintf( stderr, "Mutex count=%u owner=%p ", mutex->count, mutex->owner ); |
| 129 | dump_object_name( &mutex->obj ); |
| 130 | fputc( '\n', stderr ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Alexandre Julliard | 8382eb0 | 2007-12-05 18:16:42 +0100 | [diff] [blame] | 133 | static 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 Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 140 | static 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 | |
| 147 | static 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 Julliard | 20894e2 | 2005-02-25 16:58:43 +0000 | [diff] [blame] | 157 | list_add_head( &thread->mutex_list, &mutex->entry ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 158 | } |
| 159 | if (!mutex->abandoned) return 0; |
| 160 | mutex->abandoned = 0; |
| 161 | return 1; |
| 162 | } |
| 163 | |
Alexandre Julliard | 03f46e1 | 2005-12-12 14:58:44 +0100 | [diff] [blame] | 164 | static 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 McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 173 | static 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 Julliard | dbab5e2 | 1999-08-04 09:52:33 +0000 | [diff] [blame] | 192 | static 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 Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 202 | /* create a mutex */ |
| 203 | DECL_HANDLER(create_mutex) |
| 204 | { |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 205 | struct mutex *mutex; |
Alexandre Julliard | ead9b06 | 2005-11-18 16:31:18 +0000 | [diff] [blame] | 206 | struct unicode_str name; |
Vitaliy Margolen | f676bc8 | 2005-12-02 15:55:48 +0100 | [diff] [blame] | 207 | struct directory *root = NULL; |
Rob Shearman | 1f86321 | 2007-10-25 15:43:05 +0100 | [diff] [blame] | 208 | const struct object_attributes *objattr = get_req_data(); |
| 209 | const struct security_descriptor *sd; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 210 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 211 | reply->handle = 0; |
Rob Shearman | 1f86321 | 2007-10-25 15:43:05 +0100 | [diff] [blame] | 212 | |
| 213 | if (!objattr_is_valid( objattr, get_req_data_size() )) |
Vitaliy Margolen | f676bc8 | 2005-12-02 15:55:48 +0100 | [diff] [blame] | 214 | return; |
| 215 | |
Rob Shearman | 1f86321 | 2007-10-25 15:43:05 +0100 | [diff] [blame] | 216 | sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL; |
Rob Shearman | f98556c | 2007-10-26 17:01:33 +0100 | [diff] [blame] | 217 | objattr_get_name( objattr, &name ); |
Rob Shearman | 1f86321 | 2007-10-25 15:43:05 +0100 | [diff] [blame] | 218 | |
| 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 Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 223 | { |
Rob Shearman | 92db6d2 | 2007-11-05 14:23:36 +0000 | [diff] [blame] | 224 | 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 Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 228 | release_object( mutex ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 229 | } |
Vitaliy Margolen | f676bc8 | 2005-12-02 15:55:48 +0100 | [diff] [blame] | 230 | |
| 231 | if (root) release_object( root ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /* open a handle to a mutex */ |
| 235 | DECL_HANDLER(open_mutex) |
| 236 | { |
Alexandre Julliard | ead9b06 | 2005-11-18 16:31:18 +0000 | [diff] [blame] | 237 | struct unicode_str name; |
Vitaliy Margolen | f676bc8 | 2005-12-02 15:55:48 +0100 | [diff] [blame] | 238 | struct directory *root = NULL; |
Alexandre Julliard | 3764da6 | 2005-12-05 12:52:05 +0100 | [diff] [blame] | 239 | struct mutex *mutex; |
Alexandre Julliard | ead9b06 | 2005-11-18 16:31:18 +0000 | [diff] [blame] | 240 | |
| 241 | get_req_unicode_str( &name ); |
Vitaliy Margolen | f676bc8 | 2005-12-02 15:55:48 +0100 | [diff] [blame] | 242 | if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 ))) |
| 243 | return; |
| 244 | |
Alexandre Julliard | 3764da6 | 2005-12-05 12:52:05 +0100 | [diff] [blame] | 245 | if ((mutex = open_object_dir( root, &name, req->attributes, &mutex_ops ))) |
| 246 | { |
Alexandre Julliard | 24560e7 | 2005-12-09 13:58:25 +0100 | [diff] [blame] | 247 | reply->handle = alloc_handle( current->process, &mutex->obj, req->access, req->attributes ); |
Alexandre Julliard | 3764da6 | 2005-12-05 12:52:05 +0100 | [diff] [blame] | 248 | release_object( mutex ); |
| 249 | } |
Vitaliy Margolen | f676bc8 | 2005-12-02 15:55:48 +0100 | [diff] [blame] | 250 | |
| 251 | if (root) release_object( root ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /* release a mutex */ |
| 255 | DECL_HANDLER(release_mutex) |
| 256 | { |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 257 | struct mutex *mutex; |
| 258 | |
| 259 | if ((mutex = (struct mutex *)get_handle_obj( current->process, req->handle, |
| 260 | MUTEX_MODIFY_STATE, &mutex_ops ))) |
| 261 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 262 | if (!mutex->count || (mutex->owner != current)) set_error( STATUS_MUTANT_NOT_OWNED ); |
Eric Pouech | 44158dd | 2004-12-02 18:05:37 +0000 | [diff] [blame] | 263 | else |
| 264 | { |
| 265 | reply->prev_count = mutex->count; |
| 266 | if (!--mutex->count) do_release( mutex ); |
| 267 | } |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 268 | release_object( mutex ); |
| 269 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 270 | } |