mq_open.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /****************************************************************************
  2. * fs/mqueue/mq_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 <stdbool.h>
  25. #include <stdarg.h>
  26. #include <stdio.h>
  27. #include <sched.h>
  28. #include <mqueue.h>
  29. #include <fcntl.h>
  30. #include <errno.h>
  31. #include <nuttx/mqueue.h>
  32. #include <nuttx/fs/fs.h>
  33. #include "inode/inode.h"
  34. #include "mqueue/mqueue.h"
  35. /****************************************************************************
  36. * Public Functions
  37. ****************************************************************************/
  38. /****************************************************************************
  39. * Name: nxmq_open
  40. *
  41. * Description:
  42. * This function establish a connection between a named message queue and
  43. * the calling task. This is an internal OS interface. It is functionally
  44. * equivalent to mq_open() except that:
  45. *
  46. * - It is not a cancellation point, and
  47. * - It does not modify the errno value.
  48. *
  49. * See comments with mq_open() for a more complete description of the
  50. * behavior of this function
  51. *
  52. * Input Parameters:
  53. * mq_name - Name of the queue to open
  54. * oflags - open flags
  55. * Optional parameters. When the O_CREAT flag is specified, two optional
  56. * parameters are expected:
  57. *
  58. * 1. mode_t mode (ignored), and
  59. * 2. struct mq_attr *attr. The mq_maxmsg attribute
  60. * is used at the time that the message queue is
  61. * created to determine the maximum number of
  62. * messages that may be placed in the message queue.
  63. *
  64. * Returned Value:
  65. * This is an internal OS interface and should not be used by applications.
  66. * It follows the NuttX internal error return policy: Zero (OK) is
  67. * returned on success, mqdes point to the new message queue descriptor.
  68. * A negated errno value is returned on failure.
  69. *
  70. ****************************************************************************/
  71. int nxmq_open(FAR const char *mq_name, int oflags, mode_t mode,
  72. FAR struct mq_attr *attr, FAR mqd_t *mqdes)
  73. {
  74. FAR struct inode *inode;
  75. FAR struct mqueue_inode_s *msgq;
  76. struct inode_search_s desc;
  77. char fullpath[MAX_MQUEUE_PATH];
  78. int ret;
  79. /* Make sure that a non-NULL name is supplied */
  80. if (mq_name == NULL || *mq_name == '\0')
  81. {
  82. ret = -EINVAL;
  83. goto errout;
  84. }
  85. /* Skip over any leading '/'. All message queue paths are relative to
  86. * CONFIG_FS_MQUEUE_MPATH.
  87. */
  88. while (*mq_name == '/')
  89. {
  90. mq_name++;
  91. }
  92. /* Get the full path to the message queue */
  93. snprintf(fullpath, MAX_MQUEUE_PATH, CONFIG_FS_MQUEUE_MPATH "/%s", mq_name);
  94. /* Make sure that the check for the existence of the message queue
  95. * and the creation of the message queue are atomic with respect to
  96. * other processes executing mq_open(). A simple sched_lock() should
  97. * be sufficient.
  98. */
  99. sched_lock();
  100. /* Get the inode for this mqueue. This should succeed if the message
  101. * queue has already been created. In this case, inode_find() will
  102. * have incremented the reference count on the inode.
  103. */
  104. SETUP_SEARCH(&desc, fullpath, false);
  105. ret = inode_find(&desc);
  106. if (ret >= 0)
  107. {
  108. /* Something exists at this path. Get the search results */
  109. inode = desc.node;
  110. DEBUGASSERT(inode != NULL);
  111. /* Verify that the inode is a message queue */
  112. if (!INODE_IS_MQUEUE(inode))
  113. {
  114. ret = -ENXIO;
  115. goto errout_with_inode;
  116. }
  117. /* It exists and is a message queue. Check if the caller wanted to
  118. * create a new mqueue with this name.
  119. */
  120. if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
  121. {
  122. ret = -EEXIST;
  123. goto errout_with_inode;
  124. }
  125. /* Create a message queue descriptor for the current thread */
  126. msgq = inode->u.i_mqueue;
  127. *mqdes = nxmq_create_des(NULL, msgq, oflags);
  128. if (!*mqdes)
  129. {
  130. ret = -ENOMEM;
  131. goto errout_with_inode;
  132. }
  133. }
  134. else
  135. {
  136. /* The mqueue does not exists. Were we asked to create it? */
  137. if ((oflags & O_CREAT) == 0)
  138. {
  139. /* The mqueue does not exist and O_CREAT is not set */
  140. ret = -ENOENT;
  141. goto errout_with_lock;
  142. }
  143. /* Create an inode in the pseudo-filesystem at this path */
  144. ret = inode_semtake();
  145. if (ret < 0)
  146. {
  147. goto errout_with_lock;
  148. }
  149. ret = inode_reserve(fullpath, &inode);
  150. inode_semgive();
  151. if (ret < 0)
  152. {
  153. goto errout_with_lock;
  154. }
  155. /* Allocate memory for the new message queue. The new inode will
  156. * be created with a reference count of zero.
  157. */
  158. msgq = (FAR struct mqueue_inode_s *)nxmq_alloc_msgq(mode, attr);
  159. if (!msgq)
  160. {
  161. ret = -ENOSPC;
  162. goto errout_with_inode;
  163. }
  164. /* Create a message queue descriptor for the TCB */
  165. *mqdes = nxmq_create_des(NULL, msgq, oflags);
  166. if (!*mqdes)
  167. {
  168. ret = -ENOMEM;
  169. goto errout_with_msgq;
  170. }
  171. /* Bind the message queue and the inode structure */
  172. INODE_SET_MQUEUE(inode);
  173. inode->u.i_mqueue = msgq;
  174. msgq->inode = inode;
  175. /* Set the initial reference count on this inode to one */
  176. inode->i_crefs = 1;
  177. }
  178. RELEASE_SEARCH(&desc);
  179. sched_unlock();
  180. return OK;
  181. errout_with_msgq:
  182. nxmq_free_msgq(msgq);
  183. inode->u.i_mqueue = NULL;
  184. errout_with_inode:
  185. inode_release(inode);
  186. errout_with_lock:
  187. RELEASE_SEARCH(&desc);
  188. sched_unlock();
  189. errout:
  190. return ret;
  191. }
  192. /****************************************************************************
  193. * Name: mq_open
  194. *
  195. * Description:
  196. * This function establish a connection between a named message queue and
  197. * the calling task. After a successful call of mq_open(), the task can
  198. * reference the message queue using the address returned by the call. The
  199. * message queue remains usable until it is closed by a successful call to
  200. * mq_close().
  201. *
  202. * Input Parameters:
  203. * mq_name - Name of the queue to open
  204. * oflags - open flags
  205. * Optional parameters. When the O_CREAT flag is specified, two optional
  206. * parameters are expected:
  207. *
  208. * 1. mode_t mode (ignored), and
  209. * 2. struct mq_attr *attr. The mq_maxmsg attribute
  210. * is used at the time that the message queue is
  211. * created to determine the maximum number of
  212. * messages that may be placed in the message queue.
  213. *
  214. * Returned Value:
  215. * A message queue descriptor or (mqd_t)-1 (ERROR)
  216. *
  217. * Assumptions:
  218. *
  219. ****************************************************************************/
  220. mqd_t mq_open(FAR const char *mq_name, int oflags, ...)
  221. {
  222. FAR struct mq_attr *attr = NULL;
  223. mode_t mode = 0;
  224. mqd_t mqdes;
  225. va_list ap;
  226. int ret;
  227. /* Were we asked to create it? */
  228. if ((oflags & O_CREAT) != 0)
  229. {
  230. /* We have to extract the additional
  231. * parameters from the variable argument list.
  232. */
  233. va_start(ap, oflags);
  234. mode = va_arg(ap, mode_t);
  235. attr = va_arg(ap, FAR struct mq_attr *);
  236. va_end(ap);
  237. }
  238. ret = nxmq_open(mq_name, oflags, mode, attr, &mqdes);
  239. if (ret < 0)
  240. {
  241. set_errno(-ret);
  242. mqdes = (mqd_t)ERROR;
  243. }
  244. return mqdes;
  245. }