semaphore.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. #include "config.h"
  21. #include "wine/port.h"
  22. #include <assert.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <stdarg.h>
  26. #include "ntstatus.h"
  27. #define WIN32_NO_STATUS
  28. #include "windef.h"
  29. #include "winternl.h"
  30. #include "handle.h"
  31. #include "thread.h"
  32. #include "request.h"
  33. #include "security.h"
  34. struct semaphore
  35. {
  36. struct object obj; /* object header */
  37. unsigned int count; /* current count */
  38. unsigned int max; /* maximum possible count */
  39. };
  40. static void semaphore_dump( struct object *obj, int verbose );
  41. static struct object_type *semaphore_get_type( struct object *obj );
  42. static int semaphore_signaled( struct object *obj, struct wait_queue_entry *entry );
  43. static void semaphore_satisfied( struct object *obj, struct wait_queue_entry *entry );
  44. static unsigned int semaphore_map_access( struct object *obj, unsigned int access );
  45. static int semaphore_signal( struct object *obj, unsigned int access );
  46. static const struct object_ops semaphore_ops =
  47. {
  48. sizeof(struct semaphore), /* size */
  49. semaphore_dump, /* dump */
  50. semaphore_get_type, /* get_type */
  51. add_queue, /* add_queue */
  52. remove_queue, /* remove_queue */
  53. semaphore_signaled, /* signaled */
  54. NULL, /* get_esync_fd */
  55. semaphore_satisfied, /* satisfied */
  56. semaphore_signal, /* signal */
  57. no_get_fd, /* get_fd */
  58. semaphore_map_access, /* map_access */
  59. default_get_sd, /* get_sd */
  60. default_set_sd, /* set_sd */
  61. no_lookup_name, /* lookup_name */
  62. directory_link_name, /* link_name */
  63. default_unlink_name, /* unlink_name */
  64. no_open_file, /* open_file */
  65. no_kernel_obj_list, /* get_kernel_obj_list */
  66. no_alloc_handle, /* alloc_handle */
  67. no_close_handle, /* close_handle */
  68. no_destroy /* destroy */
  69. };
  70. static struct semaphore *create_semaphore( struct object *root, const struct unicode_str *name,
  71. unsigned int attr, unsigned int initial, unsigned int max,
  72. const struct security_descriptor *sd )
  73. {
  74. struct semaphore *sem;
  75. if (!max || (initial > max))
  76. {
  77. set_error( STATUS_INVALID_PARAMETER );
  78. return NULL;
  79. }
  80. if ((sem = create_named_object( root, &semaphore_ops, name, attr, sd )))
  81. {
  82. if (get_error() != STATUS_OBJECT_NAME_EXISTS)
  83. {
  84. /* initialize it if it didn't already exist */
  85. sem->count = initial;
  86. sem->max = max;
  87. }
  88. }
  89. return sem;
  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. 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\n", sem->count, sem->max );
  117. }
  118. static struct object_type *semaphore_get_type( struct object *obj )
  119. {
  120. static const struct unicode_str str = { type_Semaphore, sizeof(type_Semaphore) };
  121. return get_object_type( &str );
  122. }
  123. static int semaphore_signaled( struct object *obj, struct wait_queue_entry *entry )
  124. {
  125. struct semaphore *sem = (struct semaphore *)obj;
  126. assert( obj->ops == &semaphore_ops );
  127. return (sem->count > 0);
  128. }
  129. static void semaphore_satisfied( struct object *obj, struct wait_queue_entry *entry )
  130. {
  131. struct semaphore *sem = (struct semaphore *)obj;
  132. assert( obj->ops == &semaphore_ops );
  133. assert( sem->count );
  134. sem->count--;
  135. }
  136. static unsigned int semaphore_map_access( struct object *obj, unsigned int access )
  137. {
  138. if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SEMAPHORE_QUERY_STATE;
  139. if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SEMAPHORE_MODIFY_STATE;
  140. if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE;
  141. if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL | SEMAPHORE_ALL_ACCESS;
  142. return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
  143. }
  144. static int semaphore_signal( struct object *obj, unsigned int access )
  145. {
  146. struct semaphore *sem = (struct semaphore *)obj;
  147. assert( obj->ops == &semaphore_ops );
  148. if (!(access & SEMAPHORE_MODIFY_STATE))
  149. {
  150. set_error( STATUS_ACCESS_DENIED );
  151. return 0;
  152. }
  153. return release_semaphore( sem, 1, NULL );
  154. }
  155. /* create a semaphore */
  156. DECL_HANDLER(create_semaphore)
  157. {
  158. struct semaphore *sem;
  159. struct unicode_str name;
  160. struct object *root;
  161. const struct security_descriptor *sd;
  162. const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
  163. if (!objattr) return;
  164. if ((sem = create_semaphore( root, &name, objattr->attributes, req->initial, req->max, sd )))
  165. {
  166. if (get_error() == STATUS_OBJECT_NAME_EXISTS)
  167. reply->handle = alloc_handle( current->process, sem, req->access, objattr->attributes );
  168. else
  169. reply->handle = alloc_handle_no_access_check( current->process, sem,
  170. req->access, objattr->attributes );
  171. release_object( sem );
  172. }
  173. if (root) release_object( root );
  174. }
  175. /* open a handle to a semaphore */
  176. DECL_HANDLER(open_semaphore)
  177. {
  178. struct unicode_str name = get_req_unicode_str();
  179. reply->handle = open_object( current->process, req->rootdir, req->access,
  180. &semaphore_ops, &name, req->attributes );
  181. }
  182. /* release a semaphore */
  183. DECL_HANDLER(release_semaphore)
  184. {
  185. struct semaphore *sem;
  186. if ((sem = (struct semaphore *)get_handle_obj( current->process, req->handle,
  187. SEMAPHORE_MODIFY_STATE, &semaphore_ops )))
  188. {
  189. release_semaphore( sem, req->count, &reply->prev_count );
  190. release_object( sem );
  191. }
  192. }
  193. /* query details about the semaphore */
  194. DECL_HANDLER(query_semaphore)
  195. {
  196. struct semaphore *sem;
  197. if ((sem = (struct semaphore *)get_handle_obj( current->process, req->handle,
  198. SEMAPHORE_QUERY_STATE, &semaphore_ops )))
  199. {
  200. reply->current = sem->count;
  201. reply->max = sem->max;
  202. release_object( sem );
  203. }
  204. }