mqueue.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. * 2023-07-20 zmq810150896 first version
  9. * 2024-04-30 TroyMitchell Add comments for all functions
  10. */
  11. #include <dfs_file.h>
  12. #include <unistd.h>
  13. #include "mqueue.h"
  14. /**
  15. * @brief Sets the attributes of a message queue.
  16. * @param id Identifier of the message queue.
  17. * @param mqstat Pointer to a struct mq_attr containing the new attributes (ignored).
  18. * @param omqstat Pointer to a struct mq_attr where the old attributes will be stored.
  19. * @return Upon successful completion, returns 0; otherwise, returns -1 and sets errno to indicate the error.
  20. *
  21. * @note This function sets the attributes of the message queue specified by id.
  22. * The new attributes are provided in the mqstat parameter, but this implementation ignores it.
  23. * Instead, the function calls mq_getattr() to retrieve the current attributes of the message queue
  24. * and stores them in the struct mq_attr pointed to by omqstat.
  25. * If mqstat is RT_NULL, the function behaves as a query and retrieves the attributes without setting new values.
  26. * If an error occurs during the operation, errno is set to indicate the error, and the function returns -1.
  27. */
  28. int mq_setattr(mqd_t id,
  29. const struct mq_attr *mqstat,
  30. struct mq_attr *omqstat)
  31. {
  32. if (mqstat == RT_NULL)
  33. return mq_getattr(id, omqstat);
  34. else
  35. rt_set_errno(-RT_ERROR);
  36. return -1;
  37. }
  38. RTM_EXPORT(mq_setattr);
  39. /**
  40. * @brief Gets the attributes of a message queue.
  41. * @param id Identifier of the message queue.
  42. * @param mqstat Pointer to a struct mq_attr where the attributes will be stored.
  43. * @return Upon successful completion, returns 0; otherwise, returns -1 and sets errno to indicate the error.
  44. *
  45. * @note This function retrieves the attributes of the message queue specified by id.
  46. * The attributes include the maximum number of messages that can be queued (mq_maxmsg),
  47. * the maximum size of each message (mq_msgsize), the number of messages currently in the queue (mq_curmsgs),
  48. * and the flags associated with the queue (mq_flags).
  49. * The attributes are stored in the struct mq_attr pointed to by mqstat.
  50. * If the message queue identified by id does not exist or if mqstat is a null pointer, errno is set to EBADF,
  51. * indicating a bad file descriptor, and the function returns -1.
  52. * Otherwise, the function retrieves the attributes from the message queue and stores them in mqstat, returning 0 to indicate success.
  53. */
  54. int mq_getattr(mqd_t id, struct mq_attr *mqstat)
  55. {
  56. rt_mq_t mq;
  57. struct mqueue_file *mq_file;
  58. mq_file = fd_get(id)->vnode->data;
  59. mq = (rt_mq_t)mq_file->data;
  60. if ((mq == RT_NULL) || mqstat == RT_NULL)
  61. {
  62. rt_set_errno(EBADF);
  63. return -1;
  64. }
  65. mqstat->mq_maxmsg = mq->max_msgs;
  66. mqstat->mq_msgsize = mq->msg_size;
  67. mqstat->mq_curmsgs = 0;
  68. mqstat->mq_flags = 0;
  69. return 0;
  70. }
  71. RTM_EXPORT(mq_getattr);
  72. /**
  73. * @brief Opens or creates a message queue.
  74. * @param name Name of the message queue.
  75. * @param oflag Flags indicating the access mode and creation options (O_CREAT, O_EXCL, etc.).
  76. * @param ... Additional arguments for creation options (mode, attr) (ignored).
  77. * @return Upon successful completion, returns a message queue descriptor (mqd_t);
  78. * otherwise, returns (mqd_t)(-1) and sets errno to indicate the error.
  79. *
  80. * @note This function opens or creates a message queue specified by name with the specified flags.
  81. * If the name starts with '/', the leading '/' is ignored.
  82. * The function then checks the length of the name and verifies if it exceeds the maximum allowed length.
  83. * If the name is too long, errno is set to ENAMETOOLONG, indicating a name too long error.
  84. * Next, the function attempts to find the message queue file corresponding to the name.
  85. * If the file exists and O_CREAT and O_EXCL flags are both set, indicating exclusive creation,
  86. * errno is set to EEXIST, indicating that the file already exists.
  87. * If the file does not exist and O_CREAT flag is set, the function checks the message queue attributes.
  88. * If the maximum number of messages (mq_maxmsg) in the attributes is less than or equal to 0,
  89. * errno is set to EINVAL, indicating an invalid argument for the maximum number of messages.
  90. * If the file does not exist and O_CREAT flag is not set, errno is set to ENOENT, indicating no such file or directory.
  91. * If the message queue needs to be created (O_CREAT flag set), a new mqueue_file structure is allocated and initialized
  92. * with the specified message queue attributes, and it is inserted into the message queue filesystem.
  93. * Finally, the function constructs the path to the message queue device file, opens it with the specified flags,
  94. * and returns the file descriptor as a message queue descriptor (mqd_t).
  95. */
  96. mqd_t mq_open(const char *name, int oflag, ...)
  97. {
  98. int mq_fd;
  99. va_list arg;
  100. mode_t mode;
  101. struct mq_attr *attr = RT_NULL;
  102. va_start(arg, oflag);
  103. mode = (mode_t)va_arg(arg, unsigned int);
  104. mode = (mode_t)mode; /* self-assignment avoids compiler optimization */
  105. attr = (struct mq_attr *)va_arg(arg, struct mq_attr *);
  106. attr = (struct mq_attr *)attr; /* self-assignment avoids compiler optimization */
  107. va_end(arg);
  108. if(*name == '/')
  109. {
  110. name++;
  111. }
  112. int len = rt_strlen(name);
  113. if (len > RT_NAME_MAX)
  114. {
  115. rt_set_errno(ENAMETOOLONG);
  116. return (mqd_t)(-1);
  117. }
  118. rt_size_t size;
  119. struct mqueue_file *mq_file;
  120. mq_file = dfs_mqueue_lookup(name, &size);
  121. if(mq_file != RT_NULL)
  122. {
  123. if (oflag & O_CREAT && oflag & O_EXCL)
  124. {
  125. rt_set_errno(EEXIST);
  126. return (mqd_t)(-1);
  127. }
  128. }
  129. else if (oflag & O_CREAT)
  130. {
  131. if (attr->mq_maxmsg <= 0)
  132. {
  133. rt_set_errno(EINVAL);
  134. return (mqd_t)(-1);
  135. }
  136. struct mqueue_file *mq_file;
  137. mq_file = (struct mqueue_file *) rt_malloc (sizeof(struct mqueue_file));
  138. if (mq_file == RT_NULL)
  139. {
  140. rt_set_errno(ENFILE);
  141. return (mqd_t)(-1);
  142. }
  143. mq_file->msg_size = attr->mq_msgsize;
  144. mq_file->max_msgs = attr->mq_maxmsg;
  145. mq_file->data = RT_NULL;
  146. strncpy(mq_file->name, name, RT_NAME_MAX);
  147. dfs_mqueue_insert_after(&(mq_file->list));
  148. }
  149. else
  150. {
  151. rt_set_errno(ENOENT);
  152. return (mqd_t)(-1);
  153. }
  154. const char* mq_path = "/dev/mqueue/";
  155. char mq_name[RT_NAME_MAX + 12] = {0};
  156. rt_sprintf(mq_name, "%s%s", mq_path, name);
  157. mq_fd = open(mq_name, oflag);
  158. return (mqd_t)(mq_fd);
  159. }
  160. RTM_EXPORT(mq_open);
  161. /**
  162. * @brief Receives a message from a message queue.
  163. * @param id Message queue descriptor.
  164. * @param msg_ptr Pointer to the buffer where the received message will be stored.
  165. * @param msg_len Maximum size of the message buffer.
  166. * @param msg_prio Pointer to an unsigned integer where the priority of the received message will be stored (ignored).
  167. * @return Upon successful completion, returns the number of bytes received;
  168. * otherwise, returns -1 and sets errno to indicate the error.
  169. *
  170. * @note This function receives a message from the message queue identified by id.
  171. * The received message is stored in the buffer pointed to by msg_ptr, with a maximum size of msg_len bytes.
  172. * The priority of the received message is stored in the unsigned integer pointed to by msg_prio (ignored in this implementation).
  173. * If either the message queue identified by id or the msg_ptr buffer is a null pointer, errno is set to EINVAL,
  174. * indicating an invalid argument, and the function returns -1.
  175. * The function then attempts to receive a message from the message queue using the rt_mq_recv_prio() function
  176. * with an infinite timeout and uninterruptible mode.
  177. * If a message is successfully received, the function returns the number of bytes received.
  178. * If an error occurs during the receive operation, errno is set to EBADF, indicating a bad file descriptor,
  179. * and the function returns -1.
  180. */
  181. ssize_t mq_receive(mqd_t id, char *msg_ptr, size_t msg_len, unsigned *msg_prio)
  182. {
  183. rt_mq_t mq;
  184. rt_err_t result;
  185. struct mqueue_file *mq_file;
  186. mq_file = fd_get(id)->vnode->data;
  187. mq = (rt_mq_t)mq_file->data;
  188. if ((mq == RT_NULL) || (msg_ptr == RT_NULL))
  189. {
  190. rt_set_errno(EINVAL);
  191. return -1;
  192. }
  193. result = rt_mq_recv_prio(mq, msg_ptr, msg_len, (rt_int32_t *)msg_prio, RT_WAITING_FOREVER, RT_UNINTERRUPTIBLE);
  194. if (result >= 0)
  195. return result;
  196. rt_set_errno(EBADF);
  197. return -1;
  198. }
  199. RTM_EXPORT(mq_receive);
  200. /**
  201. * @brief Sends a message to a message queue.
  202. * @param id Message queue descriptor.
  203. * @param msg_ptr Pointer to the buffer containing the message to be sent.
  204. * @param msg_len Size of the message to be sent.
  205. * @param msg_prio Priority of the message to be sent.
  206. * @return Upon successful completion, returns 0;
  207. * otherwise, returns -1 and sets errno to indicate the error.
  208. *
  209. * @note This function sends a message to the message queue identified by id.
  210. * The message to be sent is contained in the buffer pointed to by msg_ptr, with a size of msg_len bytes.
  211. * The priority of the message is specified by the msg_prio parameter.
  212. * If either the message queue identified by id or the msg_ptr buffer is a null pointer, errno is set to EINVAL,
  213. * indicating an invalid argument, and the function returns -1.
  214. * The function then attempts to send the message to the message queue using the rt_mq_send_wait_prio() function
  215. * with zero timeout and uninterruptible mode.
  216. * If the message is successfully sent, the function returns 0.
  217. * If an error occurs during the send operation, errno is set to EBADF, indicating a bad file descriptor,
  218. * and the function returns -1.
  219. */
  220. int mq_send(mqd_t id, const char *msg_ptr, size_t msg_len, unsigned msg_prio)
  221. {
  222. rt_mq_t mq;
  223. rt_err_t result;
  224. struct mqueue_file *mq_file;
  225. mq_file = fd_get(id)->vnode->data;
  226. mq = (rt_mq_t)mq_file->data;
  227. if ((mq == RT_NULL) || (msg_ptr == RT_NULL))
  228. {
  229. rt_set_errno(EINVAL);
  230. return -1;
  231. }
  232. result = rt_mq_send_wait_prio(mq, (void *)msg_ptr, msg_len, msg_prio, 0, RT_UNINTERRUPTIBLE);
  233. if (result == RT_EOK)
  234. return 0;
  235. rt_set_errno(EBADF);
  236. return -1;
  237. }
  238. RTM_EXPORT(mq_send);
  239. /**
  240. * @brief Receives a message from a message queue with a timeout.
  241. * @param id Message queue descriptor.
  242. * @param msg_ptr Pointer to the buffer where the received message will be stored.
  243. * @param msg_len Maximum size of the message buffer.
  244. * @param msg_prio Pointer to an unsigned integer where the priority of the received message will be stored.
  245. * @param abs_timeout Pointer to a struct timespec specifying the absolute timeout value (ignored if null).
  246. * @return Upon successful completion, returns the number of bytes received;
  247. * otherwise, returns -1 and sets errno to indicate the error.
  248. *
  249. * @note This function receives a message from the message queue identified by id with a specified timeout.
  250. * The received message is stored in the buffer pointed to by msg_ptr, with a maximum size of msg_len bytes.
  251. * The priority of the received message is stored in the unsigned integer pointed to by msg_prio.
  252. * If either the message queue identified by id or the msg_ptr buffer is a null pointer, errno is set to EINVAL,
  253. * indicating an invalid argument, and the function returns -1.
  254. * The function then converts the absolute timeout value specified by abs_timeout to system ticks,
  255. * or sets the timeout to RT_WAITING_FOREVER if abs_timeout is null.
  256. * It attempts to receive a message from the message queue using the rt_mq_recv_prio() function
  257. * with the specified timeout and uninterruptible mode.
  258. * If a message is successfully received, the function returns the number of bytes received.
  259. * If the receive operation times out, errno is set to ETIMEDOUT, indicating a timeout error.
  260. * If the received message is too large for the specified buffer, errno is set to EMSGSIZE, indicating a message too large error.
  261. * If an unknown error occurs during the receive operation, errno is set to EBADMSG, indicating a bad message error,
  262. * and the function returns -1.
  263. */
  264. ssize_t mq_timedreceive(mqd_t id,
  265. char *msg_ptr,
  266. size_t msg_len,
  267. unsigned *msg_prio,
  268. const struct timespec *abs_timeout)
  269. {
  270. rt_mq_t mq;
  271. rt_err_t result;
  272. int tick = 0;
  273. struct mqueue_file *mq_file;
  274. mq_file = fd_get(id)->vnode->data;
  275. mq = (rt_mq_t)mq_file->data;
  276. /* parameters check */
  277. if ((mq == RT_NULL) || (msg_ptr == RT_NULL))
  278. {
  279. rt_set_errno(EINVAL);
  280. return -1;
  281. }
  282. if (abs_timeout != RT_NULL)
  283. tick = rt_timespec_to_tick(abs_timeout);
  284. else
  285. tick = RT_WAITING_FOREVER;
  286. result = rt_mq_recv_prio(mq, msg_ptr, msg_len, (rt_int32_t *)msg_prio, tick, RT_UNINTERRUPTIBLE);
  287. if (result >= 0)
  288. return result;
  289. if (result == -RT_ETIMEOUT)
  290. rt_set_errno(ETIMEDOUT);
  291. else if (result == -RT_ERROR)
  292. rt_set_errno(EMSGSIZE);
  293. else
  294. rt_set_errno(EBADMSG);
  295. return -1;
  296. }
  297. RTM_EXPORT(mq_timedreceive);
  298. /**
  299. * @brief Sends a message to a message queue with a timeout (not supported).
  300. * @param id Message queue descriptor.
  301. * @param msg_ptr Pointer to the buffer containing the message to be sent.
  302. * @param msg_len Size of the message to be sent.
  303. * @param msg_prio Priority of the message to be sent.
  304. * @param abs_timeout Pointer to a struct timespec specifying the absolute timeout value (ignored).
  305. * @return Upon successful completion, returns 0;
  306. * otherwise, returns -1 and sets errno to indicate the error.
  307. *
  308. * @note This function attempts to send a message to the message queue identified by id with a specified timeout,
  309. * but timed send is not supported in the RT-Thread environment.
  310. * Therefore, the function simply delegates the message sending operation to the mq_send() function,
  311. * which does not involve a timeout.
  312. * The abs_timeout parameter is ignored, and the message is sent without waiting for a timeout to occur.
  313. * The function returns the result of the mq_send() function, which indicates whether the message was successfully sent.
  314. */
  315. int mq_timedsend(mqd_t id,
  316. const char *msg_ptr,
  317. size_t msg_len,
  318. unsigned msg_prio,
  319. const struct timespec *abs_timeout)
  320. {
  321. /* RT-Thread does not support timed send */
  322. return mq_send(id, msg_ptr, msg_len, msg_prio);
  323. }
  324. RTM_EXPORT(mq_timedsend);
  325. /**
  326. * @brief Registers for notification when a message is available in a message queue (not supported).
  327. * @param id Message queue descriptor.
  328. * @param notification Pointer to a struct sigevent specifying the notification settings (ignored).
  329. * @return Upon successful completion, returns 0;
  330. * otherwise, returns -1 and sets errno to indicate the error.
  331. *
  332. * @note This function attempts to register for notification when a message is available in the message queue identified by id.
  333. * However, message queue notification is not supported in the RT-Thread environment.
  334. * Therefore, this function simply sets errno to EBADF, indicating a bad file descriptor,
  335. * and returns -1 to indicate that the operation is not supported.
  336. */
  337. int mq_notify(mqd_t id, const struct sigevent *notification)
  338. {
  339. rt_mq_t mq;
  340. struct mqueue_file *mq_file;
  341. mq_file = fd_get(id)->vnode->data;
  342. mq = (rt_mq_t)mq_file->data;
  343. if (mq == RT_NULL)
  344. {
  345. rt_set_errno(EBADF);
  346. return -1;
  347. }
  348. rt_set_errno(-RT_ERROR);
  349. return -1;
  350. }
  351. RTM_EXPORT(mq_notify);
  352. /**
  353. * @brief Closes a message queue descriptor.
  354. * @param id Message queue descriptor to be closed.
  355. * @return Upon successful completion, returns 0;
  356. * otherwise, returns -1 and sets errno to indicate the error.
  357. *
  358. * @note This function closes the message queue descriptor specified by id.
  359. * It delegates the closing operation to the close() function, which closes the file descriptor associated with the message queue.
  360. * If the close operation is successful, the function returns 0.
  361. * If an error occurs during the close operation, errno is set to indicate the error, and the function returns -1.
  362. */
  363. int mq_close(mqd_t id)
  364. {
  365. return close(id);
  366. }
  367. RTM_EXPORT(mq_close);
  368. /**
  369. * @brief This function will remove a message queue (REALTIME).
  370. *
  371. * @note The mq_unlink() function shall remove the message queue named by the string name.
  372. * If one or more processes have the message queue open when mq_unlink() is called,
  373. * destruction of the message queue shall be postponed until all references to the message queue have been closed.
  374. * However, the mq_unlink() call need not block until all references have been closed; it may return immediately.
  375. *
  376. * After a successful call to mq_unlink(), reuse of the name shall subsequently cause mq_open() to behave as if
  377. * no message queue of this name exists (that is, mq_open() will fail if O_CREAT is not set,
  378. * or will create a new message queue if O_CREAT is set).
  379. *
  380. * @param name is the name of the message queue.
  381. *
  382. * @return Upon successful completion, the function shall return a value of zero.
  383. * Otherwise, the named message queue shall be unchanged by this function call,
  384. * and the function shall return a value of -1 and set errno to indicate the error.
  385. *
  386. * @warning This function can ONLY be called in the thread context, you can use RT_DEBUG_IN_THREAD_CONTEXT to
  387. * check the context.
  388. * The mq_unlink() function shall fail if:
  389. * [EACCES]
  390. * Permission is denied to unlink the named message queue.
  391. * [EINTR]
  392. * The call to mq_unlink() blocked waiting for all references to the named message queue to be closed and a signal interrupted the call.
  393. * [ENOENT]
  394. * The named message queue does not exist.
  395. * The mq_unlink() function may fail if:
  396. * [ENAMETOOLONG]
  397. * The length of the name argument exceeds {_POSIX_PATH_MAX} on systems that do not support the XSI option
  398. * or exceeds {_XOPEN_PATH_MAX} on XSI systems,or has a pathname component that is longer than {_POSIX_NAME_MAX} on systems that do
  399. * 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
  400. * the same message queue name as was previously used in a successful mq_open() call shall not give an [ENAMETOOLONG] error.
  401. */
  402. int mq_unlink(const char *name)
  403. {
  404. if(*name == '/')
  405. {
  406. name++;
  407. }
  408. const char *mq_path = "/dev/mqueue/";
  409. char mq_name[RT_NAME_MAX + 12] = {0};
  410. rt_sprintf(mq_name, "%s%s", mq_path, name);
  411. return unlink(mq_name);
  412. }
  413. RTM_EXPORT(mq_unlink);