sem_open.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /****************************************************************************
  2. * fs/semaphore/sem_open.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <stdint.h>
  25. #include <stdbool.h>
  26. #include <stdarg.h>
  27. #include <stdio.h>
  28. #include <fcntl.h>
  29. #include <string.h>
  30. #include <sched.h>
  31. #include <errno.h>
  32. #include <nuttx/kmalloc.h>
  33. #include <nuttx/semaphore.h>
  34. #include <nuttx/fs/fs.h>
  35. #include "inode/inode.h"
  36. #include "semaphore/semaphore.h"
  37. #ifdef CONFIG_FS_NAMED_SEMAPHORES
  38. /****************************************************************************
  39. * Public Functions
  40. ****************************************************************************/
  41. /****************************************************************************
  42. * Name: sem_open
  43. *
  44. * Description:
  45. * This function establishes a connection between named semaphores and a
  46. * task. Following a call to sem_open() with the semaphore name, the task
  47. * may reference the semaphore associated with name using the address
  48. * returned by this call. The semaphore may be used in subsequent calls
  49. * to sem_wait(), sem_trywait(), and sem_post(). The semaphore remains
  50. * usable until the semaphore is closed by a successful call to
  51. * sem_close().
  52. *
  53. * If a task makes multiple calls to sem_open() with the same name, then
  54. * the same semaphore address is returned (provided there have been no
  55. * calls to sem_unlink()).
  56. *
  57. * Input Parameters:
  58. * name - Semaphore name
  59. * oflags - Semaphore creation options. This may either or both of the
  60. * following bit settings.
  61. * oflags = 0: Connect to the semaphore only if it already exists.
  62. * oflags = O_CREAT: Connect to the semaphore if it exists, otherwise
  63. * create the semaphore.
  64. * oflags = O_CREAT|O_EXCL: Create a new semaphore
  65. * unless one of this name already exists.
  66. * Optional parameters. When the O_CREAT flag is specified, two optional
  67. * parameters are expected:
  68. * 1. mode_t mode (ignored), and
  69. * 2. unsigned int value. This initial value of the semaphore. Valid
  70. * initial values of the semaphore must be less than or equal to
  71. * SEM_VALUE_MAX.
  72. *
  73. * Returned Value:
  74. * A pointer to sem_t or SEM_FAILED if unsuccessful.
  75. *
  76. * Assumptions:
  77. *
  78. ****************************************************************************/
  79. FAR sem_t *sem_open (FAR const char *name, int oflags, ...)
  80. {
  81. FAR struct inode *inode;
  82. FAR struct nsem_inode_s *nsem;
  83. FAR sem_t *sem = (FAR sem_t *)ERROR;
  84. struct inode_search_s desc;
  85. char fullpath[MAX_SEMPATH];
  86. mode_t mode;
  87. unsigned value;
  88. int errcode;
  89. int ret;
  90. /* Make sure that a non-NULL name is supplied */
  91. DEBUGASSERT(name != NULL);
  92. /* The POSIX specification requires that the "check for the existence
  93. * of a semaphore and the creation of the semaphore if it does not
  94. * exist shall be atomic with respect to other processes executing
  95. * sem_open()..." A simple sched_lock() should be sufficient to meet
  96. * this requirement.
  97. */
  98. sched_lock();
  99. /* Get the full path to the semaphore */
  100. snprintf(fullpath, MAX_SEMPATH, CONFIG_FS_NAMED_SEMPATH "/%s", name);
  101. /* Get the inode for this semaphore. This should succeed if the
  102. * semaphore has already been created. In this case, inode_find()
  103. * will have incremented the reference count on the inode.
  104. */
  105. SETUP_SEARCH(&desc, fullpath, false);
  106. ret = inode_find(&desc);
  107. if (ret >= 0)
  108. {
  109. /* Something exists at this path. Get the search results */
  110. inode = desc.node;
  111. DEBUGASSERT(inode != NULL);
  112. /* Verify that the inode is a semaphore */
  113. if (!INODE_IS_NAMEDSEM(inode))
  114. {
  115. errcode = ENXIO;
  116. goto errout_with_inode;
  117. }
  118. /* It exists and is a semaphore. Check if the caller wanted to
  119. * create a new semaphore with this name.
  120. */
  121. if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
  122. {
  123. errcode = EEXIST;
  124. goto errout_with_inode;
  125. }
  126. /* Return a reference to the semaphore, retaining the reference
  127. * count on the inode.
  128. */
  129. sem = &inode->u.i_nsem->ns_sem;
  130. }
  131. else
  132. {
  133. va_list ap;
  134. /* The semaphore does not exists. Were we asked to create it? */
  135. if ((oflags & O_CREAT) == 0)
  136. {
  137. /* The semaphore does not exist and O_CREAT is not set */
  138. errcode = ENOENT;
  139. goto errout_with_lock;
  140. }
  141. /* Create the semaphore. First we have to extract the additional
  142. * parameters from the variable argument list.
  143. * REVISIT: Mode parameter is not currently used.
  144. */
  145. va_start(ap, oflags);
  146. mode = va_arg(ap, mode_t);
  147. value = va_arg(ap, unsigned);
  148. va_end(ap);
  149. UNUSED(mode);
  150. /* Check the semaphore value */
  151. if (value > SEM_VALUE_MAX)
  152. {
  153. errcode = EINVAL;
  154. goto errout_with_lock;
  155. }
  156. /* Create an inode in the pseudo-filesystem at this path. The new
  157. * inode will be created with a reference count of zero.
  158. */
  159. ret = inode_semtake();
  160. if (ret < 0)
  161. {
  162. errcode = -ret;
  163. goto errout_with_lock;
  164. }
  165. ret = inode_reserve(fullpath, &inode);
  166. inode_semgive();
  167. if (ret < 0)
  168. {
  169. errcode = -ret;
  170. goto errout_with_lock;
  171. }
  172. /* Allocate the semaphore structure (using the appropriate allocator
  173. * for the group)
  174. */
  175. nsem = group_malloc(NULL, sizeof(struct nsem_inode_s));
  176. if (!nsem)
  177. {
  178. errcode = ENOMEM;
  179. goto errout_with_inode;
  180. }
  181. /* Link to the inode */
  182. inode->u.i_nsem = nsem;
  183. nsem->ns_inode = inode;
  184. /* Initialize the inode */
  185. INODE_SET_NAMEDSEM(inode);
  186. inode->i_crefs = 1;
  187. /* Initialize the semaphore */
  188. nxsem_init(&nsem->ns_sem, 0, value);
  189. /* Return a reference to the semaphore */
  190. sem = &nsem->ns_sem;
  191. }
  192. RELEASE_SEARCH(&desc);
  193. sched_unlock();
  194. return sem;
  195. errout_with_inode:
  196. inode_release(inode);
  197. errout_with_lock:
  198. RELEASE_SEARCH(&desc);
  199. set_errno(errcode);
  200. sched_unlock();
  201. return SEM_FAILED;
  202. }
  203. #endif /* CONFIG_FS_NAMED_SEMAPHORES */