mqueue.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. */
  9. #include <dfs_file.h>
  10. #include <unistd.h>
  11. #include "mqueue.h"
  12. int mq_setattr(mqd_t id,
  13. const struct mq_attr *mqstat,
  14. struct mq_attr *omqstat)
  15. {
  16. if (mqstat == RT_NULL)
  17. return mq_getattr(id, omqstat);
  18. else
  19. rt_set_errno(-RT_ERROR);
  20. return -1;
  21. }
  22. RTM_EXPORT(mq_setattr);
  23. int mq_getattr(mqd_t id, struct mq_attr *mqstat)
  24. {
  25. rt_mq_t mq;
  26. struct mqueue_file *mq_file;
  27. mq_file = fd_get(id)->vnode->data;
  28. mq = (rt_mq_t)mq_file->data;
  29. if ((mq == RT_NULL) || mqstat == RT_NULL)
  30. {
  31. rt_set_errno(EBADF);
  32. return -1;
  33. }
  34. mqstat->mq_maxmsg = mq->max_msgs;
  35. mqstat->mq_msgsize = mq->msg_size;
  36. mqstat->mq_curmsgs = 0;
  37. mqstat->mq_flags = 0;
  38. return 0;
  39. }
  40. RTM_EXPORT(mq_getattr);
  41. mqd_t mq_open(const char *name, int oflag, ...)
  42. {
  43. int mq_fd;
  44. va_list arg;
  45. mode_t mode;
  46. struct mq_attr *attr = RT_NULL;
  47. va_start(arg, oflag);
  48. mode = (mode_t)va_arg(arg, unsigned int);
  49. mode = (mode_t)mode; /* self-assignment avoids compiler optimization */
  50. attr = (struct mq_attr *)va_arg(arg, struct mq_attr *);
  51. attr = (struct mq_attr *)attr; /* self-assignment avoids compiler optimization */
  52. va_end(arg);
  53. if(*name == '/')
  54. {
  55. name++;
  56. }
  57. int len = rt_strlen(name);
  58. if (len > RT_NAME_MAX)
  59. {
  60. rt_set_errno(ENAMETOOLONG);
  61. return (mqd_t)(-1);
  62. }
  63. rt_size_t size;
  64. struct mqueue_file *mq_file;
  65. mq_file = dfs_mqueue_lookup(name, &size);
  66. if(mq_file != RT_NULL)
  67. {
  68. if (oflag & O_CREAT && oflag & O_EXCL)
  69. {
  70. rt_set_errno(EEXIST);
  71. return (mqd_t)(-1);
  72. }
  73. }
  74. else if (oflag & O_CREAT)
  75. {
  76. if (attr->mq_maxmsg <= 0)
  77. {
  78. rt_set_errno(EINVAL);
  79. return (mqd_t)(-1);
  80. }
  81. struct mqueue_file *mq_file;
  82. mq_file = (struct mqueue_file *) rt_malloc (sizeof(struct mqueue_file));
  83. if (mq_file == RT_NULL)
  84. {
  85. rt_set_errno(ENFILE);
  86. return (mqd_t)(-1);
  87. }
  88. mq_file->msg_size = attr->mq_msgsize;
  89. mq_file->max_msgs = attr->mq_maxmsg;
  90. mq_file->data = RT_NULL;
  91. strncpy(mq_file->name, name, RT_NAME_MAX);
  92. dfs_mqueue_insert_after(&(mq_file->list));
  93. }
  94. else
  95. {
  96. rt_set_errno(ENOENT);
  97. return (mqd_t)(-1);
  98. }
  99. const char* mq_path = "/dev/mqueue/";
  100. char mq_name[RT_NAME_MAX + 12] = {0};
  101. rt_sprintf(mq_name, "%s%s", mq_path, name);
  102. mq_fd = open(mq_name, oflag);
  103. return (mqd_t)(mq_fd);
  104. }
  105. RTM_EXPORT(mq_open);
  106. ssize_t mq_receive(mqd_t id, char *msg_ptr, size_t msg_len, unsigned *msg_prio)
  107. {
  108. rt_mq_t mq;
  109. rt_err_t result;
  110. struct mqueue_file *mq_file;
  111. mq_file = fd_get(id)->vnode->data;
  112. mq = (rt_mq_t)mq_file->data;
  113. if ((mq == RT_NULL) || (msg_ptr == RT_NULL))
  114. {
  115. rt_set_errno(EINVAL);
  116. return -1;
  117. }
  118. result = rt_mq_recv_prio(mq, msg_ptr, msg_len, (rt_int32_t *)msg_prio, RT_WAITING_FOREVER, RT_UNINTERRUPTIBLE);
  119. if (result >= 0)
  120. return result;
  121. rt_set_errno(EBADF);
  122. return -1;
  123. }
  124. RTM_EXPORT(mq_receive);
  125. int mq_send(mqd_t id, const char *msg_ptr, size_t msg_len, unsigned msg_prio)
  126. {
  127. rt_mq_t mq;
  128. rt_err_t result;
  129. struct mqueue_file *mq_file;
  130. mq_file = fd_get(id)->vnode->data;
  131. mq = (rt_mq_t)mq_file->data;
  132. if ((mq == RT_NULL) || (msg_ptr == RT_NULL))
  133. {
  134. rt_set_errno(EINVAL);
  135. return -1;
  136. }
  137. result = rt_mq_send_wait_prio(mq, (void *)msg_ptr, msg_len, msg_prio, 0, RT_UNINTERRUPTIBLE);
  138. if (result == RT_EOK)
  139. return 0;
  140. rt_set_errno(EBADF);
  141. return -1;
  142. }
  143. RTM_EXPORT(mq_send);
  144. ssize_t mq_timedreceive(mqd_t id,
  145. char *msg_ptr,
  146. size_t msg_len,
  147. unsigned *msg_prio,
  148. const struct timespec *abs_timeout)
  149. {
  150. rt_mq_t mq;
  151. rt_err_t result;
  152. int tick = 0;
  153. struct mqueue_file *mq_file;
  154. mq_file = fd_get(id)->vnode->data;
  155. mq = (rt_mq_t)mq_file->data;
  156. /* parameters check */
  157. if ((mq == RT_NULL) || (msg_ptr == RT_NULL))
  158. {
  159. rt_set_errno(EINVAL);
  160. return -1;
  161. }
  162. if (abs_timeout != RT_NULL)
  163. tick = rt_timespec_to_tick(abs_timeout);
  164. else
  165. tick = RT_WAITING_FOREVER;
  166. result = rt_mq_recv_prio(mq, msg_ptr, msg_len, (rt_int32_t *)msg_prio, tick, RT_UNINTERRUPTIBLE);
  167. if (result >= 0)
  168. return result;
  169. if (result == -RT_ETIMEOUT)
  170. rt_set_errno(ETIMEDOUT);
  171. else if (result == -RT_ERROR)
  172. rt_set_errno(EMSGSIZE);
  173. else
  174. rt_set_errno(EBADMSG);
  175. return -1;
  176. }
  177. RTM_EXPORT(mq_timedreceive);
  178. int mq_timedsend(mqd_t id,
  179. const char *msg_ptr,
  180. size_t msg_len,
  181. unsigned msg_prio,
  182. const struct timespec *abs_timeout)
  183. {
  184. /* RT-Thread does not support timed send */
  185. return mq_send(id, msg_ptr, msg_len, msg_prio);
  186. }
  187. RTM_EXPORT(mq_timedsend);
  188. int mq_notify(mqd_t id, const struct sigevent *notification)
  189. {
  190. rt_mq_t mq;
  191. struct mqueue_file *mq_file;
  192. mq_file = fd_get(id)->vnode->data;
  193. mq = (rt_mq_t)mq_file->data;
  194. if (mq == RT_NULL)
  195. {
  196. rt_set_errno(EBADF);
  197. return -1;
  198. }
  199. rt_set_errno(-RT_ERROR);
  200. return -1;
  201. }
  202. RTM_EXPORT(mq_notify);
  203. int mq_close(mqd_t id)
  204. {
  205. return close(id);
  206. }
  207. RTM_EXPORT(mq_close);
  208. /**
  209. * @brief This function will remove a message queue (REALTIME).
  210. *
  211. * @note The mq_unlink() function shall remove the message queue named by the string name.
  212. * If one or more processes have the message queue open when mq_unlink() is called,
  213. * destruction of the message queue shall be postponed until all references to the message queue have been closed.
  214. * However, the mq_unlink() call need not block until all references have been closed; it may return immediately.
  215. *
  216. * After a successful call to mq_unlink(), reuse of the name shall subsequently cause mq_open() to behave as if
  217. * no message queue of this name exists (that is, mq_open() will fail if O_CREAT is not set,
  218. * or will create a new message queue if O_CREAT is set).
  219. *
  220. * @param name is the name of the message queue.
  221. *
  222. * @return Upon successful completion, the function shall return a value of zero.
  223. * Otherwise, the named message queue shall be unchanged by this function call,
  224. * and the function shall return a value of -1 and set errno to indicate the error.
  225. *
  226. * @warning This function can ONLY be called in the thread context, you can use RT_DEBUG_IN_THREAD_CONTEXT to
  227. * check the context.
  228. * The mq_unlink() function shall fail if:
  229. * [EACCES]
  230. * Permission is denied to unlink the named message queue.
  231. * [EINTR]
  232. * The call to mq_unlink() blocked waiting for all references to the named message queue to be closed and a signal interrupted the call.
  233. * [ENOENT]
  234. * The named message queue does not exist.
  235. * The mq_unlink() function may fail if:
  236. * [ENAMETOOLONG]
  237. * The length of the name argument exceeds {_POSIX_PATH_MAX} on systems that do not support the XSI option
  238. * or exceeds {_XOPEN_PATH_MAX} on XSI systems,or has a pathname component that is longer than {_POSIX_NAME_MAX} on systems that do
  239. * not support the XSI option or longer than {_XOPEN_NAME_MAX} on XSI systems.A call to mq_unlink() with a name argument that contains
  240. * the same message queue name as was previously used in a successful mq_open() call shall not give an [ENAMETOOLONG] error.
  241. */
  242. int mq_unlink(const char *name)
  243. {
  244. if(*name == '/')
  245. {
  246. name++;
  247. }
  248. const char *mq_path = "/dev/mqueue/";
  249. char mq_name[RT_NAME_MAX + 12] = {0};
  250. rt_sprintf(mq_name, "%s%s", mq_path, name);
  251. return unlink(mq_name);
  252. }
  253. RTM_EXPORT(mq_unlink);