make_requests: Grab status names from async_terminate calls too.
[wine] / server / semaphore.c
1 /*
2  * Server-side semaphore 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
38 struct semaphore
39 {
40     struct object  obj;    /* object header */
41     unsigned int   count;  /* current count */
42     unsigned int   max;    /* maximum possible count */
43 };
44
45 static void semaphore_dump( struct object *obj, int verbose );
46 static int semaphore_signaled( struct object *obj, struct thread *thread );
47 static int semaphore_satisfied( struct object *obj, struct thread *thread );
48 static unsigned int semaphore_map_access( struct object *obj, unsigned int access );
49 static int semaphore_signal( struct object *obj, unsigned int access );
50
51 static const struct object_ops semaphore_ops =
52 {
53     sizeof(struct semaphore),      /* size */
54     semaphore_dump,                /* dump */
55     add_queue,                     /* add_queue */
56     remove_queue,                  /* remove_queue */
57     semaphore_signaled,            /* signaled */
58     semaphore_satisfied,           /* satisfied */
59     semaphore_signal,              /* signal */
60     no_get_fd,                     /* get_fd */
61     semaphore_map_access,          /* map_access */
62     no_lookup_name,                /* lookup_name */
63     no_close_handle,               /* close_handle */
64     no_destroy                     /* destroy */
65 };
66
67
68 static struct semaphore *create_semaphore( struct directory *root, const struct unicode_str *name,
69                                            unsigned int attr, unsigned int initial, unsigned int max )
70 {
71     struct semaphore *sem;
72
73     if (!max || (initial > max))
74     {
75         set_error( STATUS_INVALID_PARAMETER );
76         return NULL;
77     }
78     if ((sem = create_named_object_dir( root, name, attr, &semaphore_ops )))
79     {
80         if (get_error() != STATUS_OBJECT_NAME_EXISTS)
81         {
82             /* initialize it if it didn't already exist */
83             sem->count = initial;
84             sem->max   = max;
85         }
86     }
87     return sem;
88 }
89
90 static int release_semaphore( struct semaphore *sem, unsigned int count,
91                               unsigned int *prev )
92 {
93     if (prev) *prev = sem->count;
94     if (sem->count + count < sem->count || sem->count + count > sem->max)
95     {
96         set_error( STATUS_SEMAPHORE_LIMIT_EXCEEDED );
97         return 0;
98     }
99     else if (sem->count)
100     {
101         /* there cannot be any thread to wake up if the count is != 0 */
102         sem->count += count;
103     }
104     else
105     {
106         sem->count = count;
107         wake_up( &sem->obj, count );
108     }
109     return 1;
110 }
111
112 static void semaphore_dump( struct object *obj, int verbose )
113 {
114     struct semaphore *sem = (struct semaphore *)obj;
115     assert( obj->ops == &semaphore_ops );
116     fprintf( stderr, "Semaphore count=%d max=%d ", sem->count, sem->max );
117     dump_object_name( &sem->obj );
118     fputc( '\n', stderr );
119 }
120
121 static int semaphore_signaled( struct object *obj, struct thread *thread )
122 {
123     struct semaphore *sem = (struct semaphore *)obj;
124     assert( obj->ops == &semaphore_ops );
125     return (sem->count > 0);
126 }
127
128 static int semaphore_satisfied( struct object *obj, struct thread *thread )
129 {
130     struct semaphore *sem = (struct semaphore *)obj;
131     assert( obj->ops == &semaphore_ops );
132     assert( sem->count );
133     sem->count--;
134     return 0;  /* not abandoned */
135 }
136
137 static unsigned int semaphore_map_access( struct object *obj, unsigned int access )
138 {
139     if (access & GENERIC_READ)    access |= STANDARD_RIGHTS_READ | SYNCHRONIZE;
140     if (access & GENERIC_WRITE)   access |= STANDARD_RIGHTS_WRITE | SEMAPHORE_MODIFY_STATE;
141     if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
142     if (access & GENERIC_ALL)     access |= STANDARD_RIGHTS_ALL | SEMAPHORE_ALL_ACCESS;
143     return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
144 }
145
146 static int semaphore_signal( struct object *obj, unsigned int access )
147 {
148     struct semaphore *sem = (struct semaphore *)obj;
149     assert( obj->ops == &semaphore_ops );
150
151     if (!(access & SEMAPHORE_MODIFY_STATE))
152     {
153         set_error( STATUS_ACCESS_DENIED );
154         return 0;
155     }
156     return release_semaphore( sem, 1, NULL );
157 }
158
159 /* create a semaphore */
160 DECL_HANDLER(create_semaphore)
161 {
162     struct semaphore *sem;
163     struct unicode_str name;
164     struct directory *root = NULL;
165
166     reply->handle = 0;
167     get_req_unicode_str( &name );
168     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
169         return;
170
171     if ((sem = create_semaphore( root, &name, req->attributes, req->initial, req->max )))
172     {
173         reply->handle = alloc_handle( current->process, sem, req->access, req->attributes );
174         release_object( sem );
175     }
176
177     if (root) release_object( root );
178 }
179
180 /* open a handle to a semaphore */
181 DECL_HANDLER(open_semaphore)
182 {
183     struct unicode_str name;
184     struct directory *root = NULL;
185     struct semaphore *sem;
186
187     get_req_unicode_str( &name );
188     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
189         return;
190
191     if ((sem = open_object_dir( root, &name, req->attributes, &semaphore_ops )))
192     {
193         reply->handle = alloc_handle( current->process, &sem->obj, req->access, req->attributes );
194         release_object( sem );
195     }
196
197     if (root) release_object( root );
198 }
199
200 /* release a semaphore */
201 DECL_HANDLER(release_semaphore)
202 {
203     struct semaphore *sem;
204
205     if ((sem = (struct semaphore *)get_handle_obj( current->process, req->handle,
206                                                    SEMAPHORE_MODIFY_STATE, &semaphore_ops )))
207     {
208         release_semaphore( sem, req->count, &reply->prev_count );
209         release_object( sem );
210     }
211 }