mqueue.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. * If the message queue is full, the function blocks until space becomes available,
  215. * following POSIX standard behavior.
  216. * If the message is successfully sent, the function returns 0.
  217. * If an error occurs during the send operation, errno is set appropriately,
  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, RT_WAITING_FOREVER, RT_UNINTERRUPTIBLE);
  233. if (result == RT_EOK)
  234. return 0;
  235. if (result == -RT_EINTR)
  236. rt_set_errno(EINTR);
  237. else if (result == -RT_ERROR)
  238. rt_set_errno(EMSGSIZE);
  239. else if (result == -RT_EINVAL)
  240. rt_set_errno(EINVAL);
  241. else
  242. rt_set_errno(EBADF);
  243. return -1;
  244. }
  245. RTM_EXPORT(mq_send);
  246. /**
  247. * @brief Receives a message from a message queue with a timeout.
  248. * @param id Message queue descriptor.
  249. * @param msg_ptr Pointer to the buffer where the received message will be stored.
  250. * @param msg_len Maximum size of the message buffer.
  251. * @param msg_prio Pointer to an unsigned integer where the priority of the received message will be stored.
  252. * @param abs_timeout Pointer to a struct timespec specifying the absolute timeout value (ignored if null).
  253. * @return Upon successful completion, returns the number of bytes received;
  254. * otherwise, returns -1 and sets errno to indicate the error.
  255. *
  256. * @note This function receives a message from the message queue identified by id with a specified timeout.
  257. * The received message is stored in the buffer pointed to by msg_ptr, with a maximum size of msg_len bytes.
  258. * The priority of the received message is stored in the unsigned integer pointed to by msg_prio.
  259. * If either the message queue identified by id or the msg_ptr buffer is a null pointer, errno is set to EINVAL,
  260. * indicating an invalid argument, and the function returns -1.
  261. * The function then converts the absolute timeout value specified by abs_timeout to system ticks,
  262. * or sets the timeout to RT_WAITING_FOREVER if abs_timeout is null.
  263. * It attempts to receive a message from the message queue using the rt_mq_recv_prio() function
  264. * with the specified timeout and uninterruptible mode.
  265. * If a message is successfully received, the function returns the number of bytes received.
  266. * If the receive operation times out, errno is set to ETIMEDOUT, indicating a timeout error.
  267. * If the received message is too large for the specified buffer, errno is set to EMSGSIZE, indicating a message too large error.
  268. * If an unknown error occurs during the receive operation, errno is set to EBADMSG, indicating a bad message error,
  269. * and the function returns -1.
  270. */
  271. ssize_t mq_timedreceive(mqd_t id,
  272. char *msg_ptr,
  273. size_t msg_len,
  274. unsigned *msg_prio,
  275. const struct timespec *abs_timeout)
  276. {
  277. rt_mq_t mq;
  278. rt_err_t result;
  279. int tick = 0;
  280. struct mqueue_file *mq_file;
  281. mq_file = fd_get(id)->vnode->data;
  282. mq = (rt_mq_t)mq_file->data;
  283. /* parameters check */
  284. if ((mq == RT_NULL) || (msg_ptr == RT_NULL))
  285. {
  286. rt_set_errno(EINVAL);
  287. return -1;
  288. }
  289. if (abs_timeout != RT_NULL)
  290. tick = rt_timespec_to_tick(abs_timeout);
  291. else
  292. tick = RT_WAITING_FOREVER;
  293. result = rt_mq_recv_prio(mq, msg_ptr, msg_len, (rt_int32_t *)msg_prio, tick, RT_UNINTERRUPTIBLE);
  294. if (result >= 0)
  295. return result;
  296. if (result == -RT_ETIMEOUT)
  297. rt_set_errno(ETIMEDOUT);
  298. else if (result == -RT_ERROR)
  299. rt_set_errno(EMSGSIZE);
  300. else
  301. rt_set_errno(EBADMSG);
  302. return -1;
  303. }
  304. RTM_EXPORT(mq_timedreceive);
  305. /**
  306. * @brief Sends a message to a message queue with a timeout.
  307. * @param id Message queue descriptor.
  308. * @param msg_ptr Pointer to the buffer containing the message to be sent.
  309. * @param msg_len Size of the message to be sent.
  310. * @param msg_prio Priority of the message to be sent.
  311. * @param abs_timeout Pointer to a struct timespec specifying the absolute timeout value (ignored if null).
  312. * @return Upon successful completion, returns 0;
  313. * otherwise, returns -1 and sets errno to indicate the error.
  314. *
  315. * @note This function sends a message to the message queue identified by id with a specified timeout.
  316. * If the message queue is full and the timeout has not expired, the function blocks.
  317. * If abs_timeout is NULL, the function waits indefinitely (same as mq_send).
  318. * If the timeout expires before space becomes available, errno is set to ETIMEDOUT.
  319. */
  320. int mq_timedsend(mqd_t id,
  321. const char *msg_ptr,
  322. size_t msg_len,
  323. unsigned msg_prio,
  324. const struct timespec *abs_timeout)
  325. {
  326. rt_mq_t mq;
  327. rt_err_t result;
  328. int tick = 0;
  329. struct mqueue_file *mq_file;
  330. mq_file = fd_get(id)->vnode->data;
  331. mq = (rt_mq_t)mq_file->data;
  332. if ((mq == RT_NULL) || (msg_ptr == RT_NULL))
  333. {
  334. rt_set_errno(EINVAL);
  335. return -1;
  336. }
  337. if (abs_timeout != RT_NULL)
  338. tick = rt_timespec_to_tick(abs_timeout);
  339. else
  340. tick = RT_WAITING_FOREVER;
  341. result = rt_mq_send_wait_prio(mq, (void *)msg_ptr, msg_len, msg_prio, tick, RT_UNINTERRUPTIBLE);
  342. if (result == RT_EOK)
  343. return 0;
  344. if (result == -RT_ETIMEOUT)
  345. rt_set_errno(ETIMEDOUT);
  346. else if (result == -RT_EINTR)
  347. rt_set_errno(EINTR);
  348. else if (result == -RT_ERROR)
  349. rt_set_errno(EMSGSIZE);
  350. else
  351. rt_set_errno(EBADF);
  352. return -1;
  353. }
  354. RTM_EXPORT(mq_timedsend);
  355. /**
  356. * @brief Registers for notification when a message is available in a message queue (not supported).
  357. * @param id Message queue descriptor.
  358. * @param notification Pointer to a struct sigevent specifying the notification settings (ignored).
  359. * @return Upon successful completion, returns 0;
  360. * otherwise, returns -1 and sets errno to indicate the error.
  361. *
  362. * @note This function attempts to register for notification when a message is available in the message queue identified by id.
  363. * However, message queue notification is not supported in the RT-Thread environment.
  364. * Therefore, this function simply sets errno to EBADF, indicating a bad file descriptor,
  365. * and returns -1 to indicate that the operation is not supported.
  366. */
  367. int mq_notify(mqd_t id, const struct sigevent *notification)
  368. {
  369. rt_mq_t mq;
  370. struct mqueue_file *mq_file;
  371. mq_file = fd_get(id)->vnode->data;
  372. mq = (rt_mq_t)mq_file->data;
  373. if (mq == RT_NULL)
  374. {
  375. rt_set_errno(EBADF);
  376. return -1;
  377. }
  378. rt_set_errno(-RT_ERROR);
  379. return -1;
  380. }
  381. RTM_EXPORT(mq_notify);
  382. /**
  383. * @brief Closes a message queue descriptor.
  384. * @param id Message queue descriptor to be closed.
  385. * @return Upon successful completion, returns 0;
  386. * otherwise, returns -1 and sets errno to indicate the error.
  387. *
  388. * @note This function closes the message queue descriptor specified by id.
  389. * It delegates the closing operation to the close() function, which closes the file descriptor associated with the message queue.
  390. * If the close operation is successful, the function returns 0.
  391. * If an error occurs during the close operation, errno is set to indicate the error, and the function returns -1.
  392. */
  393. int mq_close(mqd_t id)
  394. {
  395. return close(id);
  396. }
  397. RTM_EXPORT(mq_close);
  398. /**
  399. * @brief This function will remove a message queue (REALTIME).
  400. *
  401. * @note The mq_unlink() function shall remove the message queue named by the string name.
  402. * If one or more processes have the message queue open when mq_unlink() is called,
  403. * destruction of the message queue shall be postponed until all references to the message queue have been closed.
  404. * However, the mq_unlink() call need not block until all references have been closed; it may return immediately.
  405. *
  406. * After a successful call to mq_unlink(), reuse of the name shall subsequently cause mq_open() to behave as if
  407. * no message queue of this name exists (that is, mq_open() will fail if O_CREAT is not set,
  408. * or will create a new message queue if O_CREAT is set).
  409. *
  410. * @param name is the name of the message queue.
  411. *
  412. * @return Upon successful completion, the function shall return a value of zero.
  413. * Otherwise, the named message queue shall be unchanged by this function call,
  414. * and the function shall return a value of -1 and set errno to indicate the error.
  415. *
  416. * @warning This function can ONLY be called in the thread context, you can use RT_DEBUG_IN_THREAD_CONTEXT to
  417. * check the context.
  418. * The mq_unlink() function shall fail if:
  419. * [EACCES]
  420. * Permission is denied to unlink the named message queue.
  421. * [EINTR]
  422. * The call to mq_unlink() blocked waiting for all references to the named message queue to be closed and a signal interrupted the call.
  423. * [ENOENT]
  424. * The named message queue does not exist.
  425. * The mq_unlink() function may fail if:
  426. * [ENAMETOOLONG]
  427. * The length of the name argument exceeds {_POSIX_PATH_MAX} on systems that do not support the XSI option
  428. * or exceeds {_XOPEN_PATH_MAX} on XSI systems,or has a pathname component that is longer than {_POSIX_NAME_MAX} on systems that do
  429. * 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
  430. * the same message queue name as was previously used in a successful mq_open() call shall not give an [ENAMETOOLONG] error.
  431. */
  432. int mq_unlink(const char *name)
  433. {
  434. if(*name == '/')
  435. {
  436. name++;
  437. }
  438. const char *mq_path = "/dev/mqueue/";
  439. char mq_name[RT_NAME_MAX + 12] = {0};
  440. rt_sprintf(mq_name, "%s%s", mq_path, name);
  441. return unlink(mq_name);
  442. }
  443. RTM_EXPORT(mq_unlink);