2013-06-26 23:18:30 +08:00
|
|
|
/*
|
2023-01-07 15:41:52 +08:00
|
|
|
* Copyright (c) 2006-2023, RT-Thread Development Team
|
2013-06-26 23:18:30 +08:00
|
|
|
*
|
2018-10-14 19:28:18 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2013-06-26 23:18:30 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
*/
|
|
|
|
|
2023-07-05 14:49:31 +08:00
|
|
|
#include <dfs_file.h>
|
|
|
|
#include <unistd.h>
|
2013-01-08 22:40:58 +08:00
|
|
|
#include "mqueue.h"
|
|
|
|
|
2023-05-05 18:14:07 +08:00
|
|
|
int mq_setattr(mqd_t id,
|
2013-06-26 23:18:30 +08:00
|
|
|
const struct mq_attr *mqstat,
|
|
|
|
struct mq_attr *omqstat)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2023-01-07 10:17:53 +08:00
|
|
|
if (mqstat == RT_NULL)
|
2023-05-05 18:14:07 +08:00
|
|
|
return mq_getattr(id, omqstat);
|
2023-01-07 10:17:53 +08:00
|
|
|
else
|
|
|
|
rt_set_errno(-RT_ERROR);
|
2013-06-26 23:18:30 +08:00
|
|
|
|
|
|
|
return -1;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
RTM_EXPORT(mq_setattr);
|
|
|
|
|
2023-05-05 18:14:07 +08:00
|
|
|
int mq_getattr(mqd_t id, struct mq_attr *mqstat)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2023-07-05 14:49:31 +08:00
|
|
|
rt_mq_t mq;
|
|
|
|
struct mqueue_file *mq_file;
|
|
|
|
mq_file = fd_get(id)->vnode->data;
|
|
|
|
mq = (rt_mq_t)mq_file->data;
|
|
|
|
if ((mq == RT_NULL) || mqstat == RT_NULL)
|
2013-06-26 23:18:30 +08:00
|
|
|
{
|
|
|
|
rt_set_errno(EBADF);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-07-05 14:49:31 +08:00
|
|
|
mqstat->mq_maxmsg = mq->max_msgs;
|
|
|
|
mqstat->mq_msgsize = mq->msg_size;
|
2013-06-26 23:18:30 +08:00
|
|
|
mqstat->mq_curmsgs = 0;
|
|
|
|
mqstat->mq_flags = 0;
|
|
|
|
|
|
|
|
return 0;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
RTM_EXPORT(mq_getattr);
|
|
|
|
|
|
|
|
mqd_t mq_open(const char *name, int oflag, ...)
|
|
|
|
{
|
2023-07-05 14:49:31 +08:00
|
|
|
int mq_fd;
|
2013-06-26 23:18:30 +08:00
|
|
|
va_list arg;
|
2014-07-19 06:52:35 +08:00
|
|
|
mode_t mode;
|
2013-06-26 23:18:30 +08:00
|
|
|
struct mq_attr *attr = RT_NULL;
|
2023-07-05 14:49:31 +08:00
|
|
|
va_start(arg, oflag);
|
|
|
|
mode = (mode_t)va_arg(arg, unsigned int);
|
|
|
|
mode = (mode_t)mode; /* self-assignment avoids compiler optimization */
|
|
|
|
attr = (struct mq_attr *)va_arg(arg, struct mq_attr *);
|
|
|
|
attr = (struct mq_attr *)attr; /* self-assignment avoids compiler optimization */
|
|
|
|
va_end(arg);
|
|
|
|
if(*name == '/')
|
|
|
|
{
|
|
|
|
name++;
|
|
|
|
}
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2023-05-05 18:14:07 +08:00
|
|
|
int len = rt_strlen(name);
|
2023-05-23 10:03:23 +08:00
|
|
|
if (len > RT_NAME_MAX)
|
2023-05-05 18:14:07 +08:00
|
|
|
{
|
|
|
|
rt_set_errno(ENAMETOOLONG);
|
2023-07-05 14:49:31 +08:00
|
|
|
return (mqd_t)(-1);
|
2023-05-05 18:14:07 +08:00
|
|
|
}
|
2023-07-05 14:49:31 +08:00
|
|
|
rt_size_t size;
|
|
|
|
struct mqueue_file *mq_file;
|
|
|
|
mq_file = dfs_mqueue_lookup(name, &size);
|
|
|
|
if(mq_file != RT_NULL)
|
2023-01-07 10:09:40 +08:00
|
|
|
{
|
2023-05-05 18:14:07 +08:00
|
|
|
if (oflag & O_CREAT && oflag & O_EXCL)
|
|
|
|
{
|
|
|
|
rt_set_errno(EEXIST);
|
|
|
|
return (mqd_t)(-1);
|
|
|
|
}
|
2023-01-07 10:09:40 +08:00
|
|
|
}
|
|
|
|
else if (oflag & O_CREAT)
|
2013-06-26 23:18:30 +08:00
|
|
|
{
|
2023-05-05 18:14:07 +08:00
|
|
|
if (attr->mq_maxmsg <= 0)
|
2013-06-26 23:18:30 +08:00
|
|
|
{
|
2023-05-05 18:14:07 +08:00
|
|
|
rt_set_errno(EINVAL);
|
2023-07-05 14:49:31 +08:00
|
|
|
return (mqd_t)(-1);
|
2013-06-26 23:18:30 +08:00
|
|
|
}
|
2023-07-05 14:49:31 +08:00
|
|
|
struct mqueue_file *mq_file;
|
|
|
|
mq_file = (struct mqueue_file *) rt_malloc (sizeof(struct mqueue_file));
|
2013-06-26 23:18:30 +08:00
|
|
|
|
2023-07-05 14:49:31 +08:00
|
|
|
if (mq_file == RT_NULL)
|
2013-06-26 23:18:30 +08:00
|
|
|
{
|
|
|
|
rt_set_errno(ENFILE);
|
2023-07-05 14:49:31 +08:00
|
|
|
return (mqd_t)(-1);
|
2013-06-26 23:18:30 +08:00
|
|
|
}
|
2023-07-05 14:49:31 +08:00
|
|
|
mq_file->msg_size = attr->mq_msgsize;
|
|
|
|
mq_file->max_msgs = attr->mq_maxmsg;
|
|
|
|
mq_file->data = RT_NULL;
|
|
|
|
strncpy(mq_file->name, name, RT_NAME_MAX);
|
|
|
|
dfs_mqueue_insert_after(&(mq_file->list));
|
2013-06-26 23:18:30 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-01-07 10:09:40 +08:00
|
|
|
rt_set_errno(ENOENT);
|
2023-07-05 14:49:31 +08:00
|
|
|
return (mqd_t)(-1);
|
2013-06-26 23:18:30 +08:00
|
|
|
}
|
|
|
|
|
2023-07-05 14:49:31 +08:00
|
|
|
const char* mq_path = "/dev/mqueue/";
|
|
|
|
char mq_name[RT_NAME_MAX + 12] = {0};
|
|
|
|
rt_sprintf(mq_name, "%s%s", mq_path, name);
|
|
|
|
mq_fd = open(mq_name, oflag);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2023-07-05 14:49:31 +08:00
|
|
|
return (mqd_t)(mq_fd);
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
RTM_EXPORT(mq_open);
|
|
|
|
|
2023-05-05 18:14:07 +08:00
|
|
|
ssize_t mq_receive(mqd_t id, char *msg_ptr, size_t msg_len, unsigned *msg_prio)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2023-07-05 14:49:31 +08:00
|
|
|
rt_mq_t mq;
|
2013-06-26 23:18:30 +08:00
|
|
|
rt_err_t result;
|
2023-07-05 14:49:31 +08:00
|
|
|
struct mqueue_file *mq_file;
|
|
|
|
mq_file = fd_get(id)->vnode->data;
|
|
|
|
mq = (rt_mq_t)mq_file->data;
|
|
|
|
if ((mq == RT_NULL) || (msg_ptr == RT_NULL))
|
2013-06-26 23:18:30 +08:00
|
|
|
{
|
|
|
|
rt_set_errno(EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2023-07-05 14:49:31 +08:00
|
|
|
result = rt_mq_recv_prio(mq, msg_ptr, msg_len, (rt_int32_t *)msg_prio, RT_WAITING_FOREVER, RT_UNINTERRUPTIBLE);
|
2023-06-21 18:22:42 +08:00
|
|
|
if (result >= 0)
|
2023-05-05 18:14:07 +08:00
|
|
|
return rt_strlen(msg_ptr);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
rt_set_errno(EBADF);
|
|
|
|
return -1;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
RTM_EXPORT(mq_receive);
|
|
|
|
|
2023-05-05 18:14:07 +08:00
|
|
|
int mq_send(mqd_t id, const char *msg_ptr, size_t msg_len, unsigned msg_prio)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2023-07-05 14:49:31 +08:00
|
|
|
rt_mq_t mq;
|
2013-06-26 23:18:30 +08:00
|
|
|
rt_err_t result;
|
2023-07-05 14:49:31 +08:00
|
|
|
struct mqueue_file *mq_file;
|
|
|
|
mq_file = fd_get(id)->vnode->data;
|
|
|
|
mq = (rt_mq_t)mq_file->data;
|
2013-06-26 23:18:30 +08:00
|
|
|
|
2023-07-05 14:49:31 +08:00
|
|
|
if ((mq == RT_NULL) || (msg_ptr == RT_NULL))
|
2013-06-26 23:18:30 +08:00
|
|
|
{
|
|
|
|
rt_set_errno(EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
2023-07-05 14:49:31 +08:00
|
|
|
result = rt_mq_send_wait_prio(mq, (void *)msg_ptr, msg_len, msg_prio, 0, RT_UNINTERRUPTIBLE);
|
2013-06-26 23:18:30 +08:00
|
|
|
if (result == RT_EOK)
|
|
|
|
return 0;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
rt_set_errno(EBADF);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
return -1;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
RTM_EXPORT(mq_send);
|
|
|
|
|
2023-05-05 18:14:07 +08:00
|
|
|
ssize_t mq_timedreceive(mqd_t id,
|
2013-06-26 23:18:30 +08:00
|
|
|
char *msg_ptr,
|
|
|
|
size_t msg_len,
|
|
|
|
unsigned *msg_prio,
|
|
|
|
const struct timespec *abs_timeout)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2023-07-05 14:49:31 +08:00
|
|
|
rt_mq_t mq;
|
2013-06-26 23:18:30 +08:00
|
|
|
rt_err_t result;
|
2023-07-05 14:49:31 +08:00
|
|
|
int tick = 0;
|
|
|
|
struct mqueue_file *mq_file;
|
|
|
|
mq_file = fd_get(id)->vnode->data;
|
|
|
|
mq = (rt_mq_t)mq_file->data;
|
2013-06-26 23:18:30 +08:00
|
|
|
/* parameters check */
|
2023-07-05 14:49:31 +08:00
|
|
|
if ((mq == RT_NULL) || (msg_ptr == RT_NULL))
|
2013-06-26 23:18:30 +08:00
|
|
|
{
|
|
|
|
rt_set_errno(EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
2023-01-06 10:07:06 +08:00
|
|
|
if (abs_timeout != RT_NULL)
|
|
|
|
tick = rt_timespec_to_tick(abs_timeout);
|
2023-07-24 17:01:45 +08:00
|
|
|
else
|
|
|
|
tick = RT_WAITING_FOREVER;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2023-07-05 14:49:31 +08:00
|
|
|
result = rt_mq_recv_prio(mq, msg_ptr, msg_len, (rt_int32_t *)msg_prio, tick, RT_UNINTERRUPTIBLE);
|
2023-06-26 12:06:06 +08:00
|
|
|
|
2023-06-21 18:22:42 +08:00
|
|
|
if (result >= 0)
|
2023-05-05 18:14:07 +08:00
|
|
|
return rt_strlen(msg_ptr);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
if (result == -RT_ETIMEOUT)
|
|
|
|
rt_set_errno(ETIMEDOUT);
|
2023-05-05 18:14:07 +08:00
|
|
|
else if (result == -RT_ERROR)
|
|
|
|
rt_set_errno(EMSGSIZE);
|
2013-06-26 23:18:30 +08:00
|
|
|
else
|
|
|
|
rt_set_errno(EBADMSG);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
return -1;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
RTM_EXPORT(mq_timedreceive);
|
|
|
|
|
2023-05-05 18:14:07 +08:00
|
|
|
int mq_timedsend(mqd_t id,
|
2013-06-26 23:18:30 +08:00
|
|
|
const char *msg_ptr,
|
|
|
|
size_t msg_len,
|
|
|
|
unsigned msg_prio,
|
|
|
|
const struct timespec *abs_timeout)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2013-06-26 23:18:30 +08:00
|
|
|
/* RT-Thread does not support timed send */
|
2023-05-05 18:14:07 +08:00
|
|
|
return mq_send(id, msg_ptr, msg_len, msg_prio);
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
RTM_EXPORT(mq_timedsend);
|
|
|
|
|
2023-05-05 18:14:07 +08:00
|
|
|
int mq_notify(mqd_t id, const struct sigevent *notification)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2023-07-05 14:49:31 +08:00
|
|
|
rt_mq_t mq;
|
|
|
|
struct mqueue_file *mq_file;
|
|
|
|
mq_file = fd_get(id)->vnode->data;
|
|
|
|
mq = (rt_mq_t)mq_file->data;
|
|
|
|
if (mq == RT_NULL)
|
2023-05-05 18:14:07 +08:00
|
|
|
{
|
|
|
|
rt_set_errno(EBADF);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-06-26 23:18:30 +08:00
|
|
|
rt_set_errno(-RT_ERROR);
|
|
|
|
|
|
|
|
return -1;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
RTM_EXPORT(mq_notify);
|
|
|
|
|
2023-05-05 18:14:07 +08:00
|
|
|
int mq_close(mqd_t id)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2023-07-05 14:49:31 +08:00
|
|
|
return close(id);
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
RTM_EXPORT(mq_close);
|
|
|
|
|
2023-05-22 13:47:01 +08:00
|
|
|
/**
|
|
|
|
* @brief This function will remove a message queue (REALTIME).
|
|
|
|
*
|
2023-06-21 18:22:42 +08:00
|
|
|
* @note The mq_unlink() function shall remove the message queue named by the string name.
|
|
|
|
* If one or more processes have the message queue open when mq_unlink() is called,
|
|
|
|
* destruction of the message queue shall be postponed until all references to the message queue have been closed.
|
2023-05-22 13:47:01 +08:00
|
|
|
* However, the mq_unlink() call need not block until all references have been closed; it may return immediately.
|
2023-06-21 18:22:42 +08:00
|
|
|
*
|
|
|
|
* After a successful call to mq_unlink(), reuse of the name shall subsequently cause mq_open() to behave as if
|
|
|
|
* no message queue of this name exists (that is, mq_open() will fail if O_CREAT is not set,
|
2023-05-22 13:47:01 +08:00
|
|
|
* or will create a new message queue if O_CREAT is set).
|
2023-06-21 18:22:42 +08:00
|
|
|
*
|
2023-05-22 13:47:01 +08:00
|
|
|
* @param name is the name of the message queue.
|
|
|
|
*
|
2023-06-21 18:22:42 +08:00
|
|
|
* @return Upon successful completion, the function shall return a value of zero.
|
|
|
|
* Otherwise, the named message queue shall be unchanged by this function call,
|
2023-05-22 13:47:01 +08:00
|
|
|
* and the function shall return a value of -1 and set errno to indicate the error.
|
|
|
|
*
|
|
|
|
* @warning This function can ONLY be called in the thread context, you can use RT_DEBUG_IN_THREAD_CONTEXT to
|
|
|
|
* check the context.
|
|
|
|
* The mq_unlink() function shall fail if:
|
|
|
|
* [EACCES]
|
|
|
|
* Permission is denied to unlink the named message queue.
|
|
|
|
* [EINTR]
|
|
|
|
* The call to mq_unlink() blocked waiting for all references to the named message queue to be closed and a signal interrupted the call.
|
|
|
|
* [ENOENT]
|
|
|
|
* The named message queue does not exist.
|
|
|
|
* The mq_unlink() function may fail if:
|
|
|
|
* [ENAMETOOLONG]
|
2023-06-21 18:22:42 +08:00
|
|
|
* The length of the name argument exceeds {_POSIX_PATH_MAX} on systems that do not support the XSI option
|
|
|
|
* or exceeds {_XOPEN_PATH_MAX} on XSI systems,or has a pathname component that is longer than {_POSIX_NAME_MAX} on systems that do
|
|
|
|
* 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
|
2023-05-22 13:47:01 +08:00
|
|
|
* the same message queue name as was previously used in a successful mq_open() call shall not give an [ENAMETOOLONG] error.
|
|
|
|
*/
|
2013-01-08 22:40:58 +08:00
|
|
|
int mq_unlink(const char *name)
|
|
|
|
{
|
2023-07-05 14:49:31 +08:00
|
|
|
if(*name == '/')
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2023-07-05 14:49:31 +08:00
|
|
|
name++;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
2023-07-05 14:49:31 +08:00
|
|
|
const char *mq_path = "/dev/mqueue/";
|
|
|
|
char mq_name[RT_NAME_MAX + 12] = {0};
|
|
|
|
rt_sprintf(mq_name, "%s%s", mq_path, name);
|
|
|
|
return unlink(mq_name);
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
RTM_EXPORT(mq_unlink);
|