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