rt-thread/components/libc/posix/ipc/mqueue.c

282 lines
8.1 KiB
C
Raw Normal View History

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
*/
#include <dfs_file.h>
#include <unistd.h>
2013-01-08 22:40:58 +08:00
#include "mqueue.h"
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)
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);
int mq_getattr(mqd_t id, struct mq_attr *mqstat)
2013-01-08 22:40:58 +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;
}
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, ...)
{
int mq_fd;
2013-06-26 23:18:30 +08:00
va_list arg;
mode_t mode;
2013-06-26 23:18:30 +08:00
struct mq_attr *attr = RT_NULL;
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
int len = rt_strlen(name);
if (len > RT_NAME_MAX)
{
rt_set_errno(ENAMETOOLONG);
return (mqd_t)(-1);
}
rt_size_t size;
struct mqueue_file *mq_file;
mq_file = dfs_mqueue_lookup(name, &size);
if(mq_file != RT_NULL)
{
if (oflag & O_CREAT && oflag & O_EXCL)
{
rt_set_errno(EEXIST);
return (mqd_t)(-1);
}
}
else if (oflag & O_CREAT)
2013-06-26 23:18:30 +08:00
{
if (attr->mq_maxmsg <= 0)
2013-06-26 23:18:30 +08:00
{
rt_set_errno(EINVAL);
return (mqd_t)(-1);
2013-06-26 23:18:30 +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
if (mq_file == RT_NULL)
2013-06-26 23:18:30 +08:00
{
rt_set_errno(ENFILE);
return (mqd_t)(-1);
2013-06-26 23:18:30 +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
{
rt_set_errno(ENOENT);
return (mqd_t)(-1);
2013-06-26 23:18:30 +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
return (mqd_t)(mq_fd);
2013-01-08 22:40:58 +08:00
}
RTM_EXPORT(mq_open);
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
{
rt_mq_t mq;
2013-06-26 23:18:30 +08:00
rt_err_t result;
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
result = rt_mq_recv_prio(mq, msg_ptr, msg_len, (rt_int32_t *)msg_prio, RT_WAITING_FOREVER, RT_UNINTERRUPTIBLE);
if (result >= 0)
return result;
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);
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
{
rt_mq_t mq;
2013-06-26 23:18:30 +08:00
rt_err_t result;
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
if ((mq == RT_NULL) || (msg_ptr == RT_NULL))
2013-06-26 23:18:30 +08:00
{
rt_set_errno(EINVAL);
return -1;
}
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);
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
{
rt_mq_t mq;
2013-06-26 23:18:30 +08:00
rt_err_t result;
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 */
if ((mq == RT_NULL) || (msg_ptr == RT_NULL))
2013-06-26 23:18:30 +08:00
{
rt_set_errno(EINVAL);
return -1;
}
if (abs_timeout != RT_NULL)
tick = rt_timespec_to_tick(abs_timeout);
else
tick = RT_WAITING_FOREVER;
2013-01-08 22:40:58 +08:00
result = rt_mq_recv_prio(mq, msg_ptr, msg_len, (rt_int32_t *)msg_prio, tick, RT_UNINTERRUPTIBLE);
if (result >= 0)
return result;
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
if (result == -RT_ETIMEOUT)
rt_set_errno(ETIMEDOUT);
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);
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 */
return mq_send(id, msg_ptr, msg_len, msg_prio);
2013-01-08 22:40:58 +08:00
}
RTM_EXPORT(mq_timedsend);
int mq_notify(mqd_t id, const struct sigevent *notification)
2013-01-08 22:40:58 +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)
{
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);
int mq_close(mqd_t id)
2013-01-08 22:40:58 +08:00
{
return close(id);
2013-01-08 22:40:58 +08:00
}
RTM_EXPORT(mq_close);
/**
* @brief This function will remove a message queue (REALTIME).
*
* @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.
* However, the mq_unlink() call need not block until all references have been closed; it may return immediately.
*
* 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,
* or will create a new message queue if O_CREAT is set).
*
* @param name is the name of the message queue.
*
* @return Upon successful completion, the function shall return a value of zero.
* Otherwise, the named message queue shall be unchanged by this function call,
* 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]
* 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
* 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)
{
if(*name == '/')
2013-01-08 22:40:58 +08:00
{
name++;
2013-01-08 22:40:58 +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);