Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Server-side semaphore 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 | b0e5fb4 | 2007-10-25 15:42:53 +0100 | [diff] [blame] | 37 | #include "security.h" |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 38 | |
| 39 | struct semaphore |
| 40 | { |
| 41 | struct object obj; /* object header */ |
| 42 | unsigned int count; /* current count */ |
| 43 | unsigned int max; /* maximum possible count */ |
| 44 | }; |
| 45 | |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 46 | static void semaphore_dump( struct object *obj, int verbose ); |
Alexandre Julliard | 8382eb0 | 2007-12-05 18:16:42 +0100 | [diff] [blame] | 47 | static struct object_type *semaphore_get_type( struct object *obj ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 48 | static int semaphore_signaled( struct object *obj, struct thread *thread ); |
| 49 | static int semaphore_satisfied( struct object *obj, struct thread *thread ); |
Alexandre Julliard | 03f46e1 | 2005-12-12 14:58:44 +0100 | [diff] [blame] | 50 | static unsigned int semaphore_map_access( struct object *obj, unsigned int access ); |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 51 | static int semaphore_signal( struct object *obj, unsigned int access ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 52 | |
| 53 | static const struct object_ops semaphore_ops = |
| 54 | { |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 55 | sizeof(struct semaphore), /* size */ |
| 56 | semaphore_dump, /* dump */ |
Alexandre Julliard | 8382eb0 | 2007-12-05 18:16:42 +0100 | [diff] [blame] | 57 | semaphore_get_type, /* get_type */ |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 58 | add_queue, /* add_queue */ |
| 59 | remove_queue, /* remove_queue */ |
| 60 | semaphore_signaled, /* signaled */ |
| 61 | semaphore_satisfied, /* satisfied */ |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 62 | semaphore_signal, /* signal */ |
Alexandre Julliard | 1ab243b | 2000-12-19 02:12:45 +0000 | [diff] [blame] | 63 | no_get_fd, /* get_fd */ |
Alexandre Julliard | 03f46e1 | 2005-12-12 14:58:44 +0100 | [diff] [blame] | 64 | semaphore_map_access, /* map_access */ |
Rob Shearman | c1707d8 | 2007-10-03 13:10:37 +0100 | [diff] [blame] | 65 | default_get_sd, /* get_sd */ |
| 66 | default_set_sd, /* set_sd */ |
Vitaliy Margolen | baffcb9 | 2005-11-22 14:55:42 +0000 | [diff] [blame] | 67 | no_lookup_name, /* lookup_name */ |
Alexandre Julliard | 7e71c1d | 2007-03-22 11:44:29 +0100 | [diff] [blame] | 68 | no_open_file, /* open_file */ |
Alexandre Julliard | b9b1ea9 | 2005-06-09 15:39:52 +0000 | [diff] [blame] | 69 | no_close_handle, /* close_handle */ |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 70 | no_destroy /* destroy */ |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | |
Vitaliy Margolen | 5daae3d | 2005-12-02 16:01:17 +0100 | [diff] [blame] | 74 | static struct semaphore *create_semaphore( struct directory *root, const struct unicode_str *name, |
Rob Shearman | b0e5fb4 | 2007-10-25 15:42:53 +0100 | [diff] [blame] | 75 | unsigned int attr, unsigned int initial, unsigned int max, |
| 76 | const struct security_descriptor *sd ) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 77 | { |
| 78 | struct semaphore *sem; |
| 79 | |
| 80 | if (!max || (initial > max)) |
| 81 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 82 | set_error( STATUS_INVALID_PARAMETER ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 83 | return NULL; |
| 84 | } |
Vitaliy Margolen | 5daae3d | 2005-12-02 16:01:17 +0100 | [diff] [blame] | 85 | if ((sem = create_named_object_dir( root, name, attr, &semaphore_ops ))) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 86 | { |
Vitaliy Margolen | 893987b | 2005-11-21 16:27:03 +0000 | [diff] [blame] | 87 | if (get_error() != STATUS_OBJECT_NAME_EXISTS) |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 88 | { |
| 89 | /* initialize it if it didn't already exist */ |
| 90 | sem->count = initial; |
| 91 | sem->max = max; |
Rob Shearman | b0e5fb4 | 2007-10-25 15:42:53 +0100 | [diff] [blame] | 92 | if (sd) default_set_sd( &sem->obj, sd, OWNER_SECURITY_INFORMATION| |
| 93 | GROUP_SECURITY_INFORMATION| |
| 94 | DACL_SECURITY_INFORMATION| |
| 95 | SACL_SECURITY_INFORMATION ); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 96 | } |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 97 | } |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 98 | return sem; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 101 | static int release_semaphore( struct semaphore *sem, unsigned int count, |
| 102 | unsigned int *prev ) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 103 | { |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 104 | if (prev) *prev = sem->count; |
| 105 | if (sem->count + count < sem->count || sem->count + count > sem->max) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 106 | { |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 107 | set_error( STATUS_SEMAPHORE_LIMIT_EXCEEDED ); |
| 108 | return 0; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 109 | } |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 110 | else if (sem->count) |
| 111 | { |
| 112 | /* there cannot be any thread to wake up if the count is != 0 */ |
| 113 | sem->count += count; |
| 114 | } |
| 115 | else |
| 116 | { |
| 117 | sem->count = count; |
| 118 | wake_up( &sem->obj, count ); |
| 119 | } |
| 120 | return 1; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 123 | static void semaphore_dump( struct object *obj, int verbose ) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 124 | { |
| 125 | struct semaphore *sem = (struct semaphore *)obj; |
| 126 | assert( obj->ops == &semaphore_ops ); |
Alexandre Julliard | d16319c | 1999-11-25 21:30:24 +0000 | [diff] [blame] | 127 | fprintf( stderr, "Semaphore count=%d max=%d ", sem->count, sem->max ); |
| 128 | dump_object_name( &sem->obj ); |
| 129 | fputc( '\n', stderr ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Alexandre Julliard | 8382eb0 | 2007-12-05 18:16:42 +0100 | [diff] [blame] | 132 | static struct object_type *semaphore_get_type( struct object *obj ) |
| 133 | { |
| 134 | static const WCHAR name[] = {'S','e','m','a','p','h','o','r','e'}; |
| 135 | static const struct unicode_str str = { name, sizeof(name) }; |
| 136 | return get_object_type( &str ); |
| 137 | } |
| 138 | |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 139 | static int semaphore_signaled( struct object *obj, struct thread *thread ) |
| 140 | { |
| 141 | struct semaphore *sem = (struct semaphore *)obj; |
| 142 | assert( obj->ops == &semaphore_ops ); |
| 143 | return (sem->count > 0); |
| 144 | } |
| 145 | |
| 146 | static int semaphore_satisfied( struct object *obj, struct thread *thread ) |
| 147 | { |
| 148 | struct semaphore *sem = (struct semaphore *)obj; |
| 149 | assert( obj->ops == &semaphore_ops ); |
| 150 | assert( sem->count ); |
| 151 | sem->count--; |
| 152 | return 0; /* not abandoned */ |
| 153 | } |
| 154 | |
Alexandre Julliard | 03f46e1 | 2005-12-12 14:58:44 +0100 | [diff] [blame] | 155 | static unsigned int semaphore_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 | SEMAPHORE_MODIFY_STATE; |
| 159 | if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE; |
| 160 | if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL | SEMAPHORE_ALL_ACCESS; |
| 161 | return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL); |
| 162 | } |
| 163 | |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 164 | static int semaphore_signal( struct object *obj, unsigned int access ) |
| 165 | { |
| 166 | struct semaphore *sem = (struct semaphore *)obj; |
| 167 | assert( obj->ops == &semaphore_ops ); |
| 168 | |
| 169 | if (!(access & SEMAPHORE_MODIFY_STATE)) |
| 170 | { |
| 171 | set_error( STATUS_ACCESS_DENIED ); |
| 172 | return 0; |
| 173 | } |
| 174 | return release_semaphore( sem, 1, NULL ); |
| 175 | } |
| 176 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 177 | /* create a semaphore */ |
| 178 | DECL_HANDLER(create_semaphore) |
| 179 | { |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 180 | struct semaphore *sem; |
Alexandre Julliard | ead9b06 | 2005-11-18 16:31:18 +0000 | [diff] [blame] | 181 | struct unicode_str name; |
Vitaliy Margolen | 5daae3d | 2005-12-02 16:01:17 +0100 | [diff] [blame] | 182 | struct directory *root = NULL; |
Rob Shearman | b0e5fb4 | 2007-10-25 15:42:53 +0100 | [diff] [blame] | 183 | const struct object_attributes *objattr = get_req_data(); |
| 184 | const struct security_descriptor *sd; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 185 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 186 | reply->handle = 0; |
Rob Shearman | b0e5fb4 | 2007-10-25 15:42:53 +0100 | [diff] [blame] | 187 | |
| 188 | if (!objattr_is_valid( objattr, get_req_data_size() )) |
Vitaliy Margolen | 5daae3d | 2005-12-02 16:01:17 +0100 | [diff] [blame] | 189 | return; |
| 190 | |
Rob Shearman | b0e5fb4 | 2007-10-25 15:42:53 +0100 | [diff] [blame] | 191 | sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL; |
Rob Shearman | f98556c | 2007-10-26 17:01:33 +0100 | [diff] [blame] | 192 | objattr_get_name( objattr, &name ); |
Rob Shearman | b0e5fb4 | 2007-10-25 15:42:53 +0100 | [diff] [blame] | 193 | |
Rob Shearman | b0e5fb4 | 2007-10-25 15:42:53 +0100 | [diff] [blame] | 194 | if (objattr->rootdir && !(root = get_directory_obj( current->process, objattr->rootdir, 0 ))) |
| 195 | return; |
| 196 | |
| 197 | if ((sem = create_semaphore( root, &name, req->attributes, req->initial, req->max, sd ))) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 198 | { |
Rob Shearman | 92db6d2 | 2007-11-05 14:23:36 +0000 | [diff] [blame] | 199 | if (get_error() == STATUS_OBJECT_NAME_EXISTS) |
| 200 | reply->handle = alloc_handle( current->process, sem, req->access, req->attributes ); |
| 201 | else |
| 202 | reply->handle = alloc_handle_no_access_check( current->process, sem, req->access, req->attributes ); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 203 | release_object( sem ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 204 | } |
Vitaliy Margolen | 5daae3d | 2005-12-02 16:01:17 +0100 | [diff] [blame] | 205 | |
| 206 | if (root) release_object( root ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /* open a handle to a semaphore */ |
| 210 | DECL_HANDLER(open_semaphore) |
| 211 | { |
Alexandre Julliard | ead9b06 | 2005-11-18 16:31:18 +0000 | [diff] [blame] | 212 | struct unicode_str name; |
Vitaliy Margolen | 5daae3d | 2005-12-02 16:01:17 +0100 | [diff] [blame] | 213 | struct directory *root = NULL; |
Alexandre Julliard | 3764da6 | 2005-12-05 12:52:05 +0100 | [diff] [blame] | 214 | struct semaphore *sem; |
Alexandre Julliard | ead9b06 | 2005-11-18 16:31:18 +0000 | [diff] [blame] | 215 | |
| 216 | get_req_unicode_str( &name ); |
Vitaliy Margolen | 5daae3d | 2005-12-02 16:01:17 +0100 | [diff] [blame] | 217 | if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 ))) |
| 218 | return; |
| 219 | |
Alexandre Julliard | 3764da6 | 2005-12-05 12:52:05 +0100 | [diff] [blame] | 220 | if ((sem = open_object_dir( root, &name, req->attributes, &semaphore_ops ))) |
| 221 | { |
Alexandre Julliard | 24560e7 | 2005-12-09 13:58:25 +0100 | [diff] [blame] | 222 | reply->handle = alloc_handle( current->process, &sem->obj, req->access, req->attributes ); |
Alexandre Julliard | 3764da6 | 2005-12-05 12:52:05 +0100 | [diff] [blame] | 223 | release_object( sem ); |
| 224 | } |
Vitaliy Margolen | 5daae3d | 2005-12-02 16:01:17 +0100 | [diff] [blame] | 225 | |
| 226 | if (root) release_object( root ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /* release a semaphore */ |
| 230 | DECL_HANDLER(release_semaphore) |
| 231 | { |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 232 | struct semaphore *sem; |
| 233 | |
| 234 | if ((sem = (struct semaphore *)get_handle_obj( current->process, req->handle, |
| 235 | SEMAPHORE_MODIFY_STATE, &semaphore_ops ))) |
| 236 | { |
| 237 | release_semaphore( sem, req->count, &reply->prev_count ); |
| 238 | release_object( sem ); |
| 239 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 240 | } |