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
|
|
|
|
*/
|
|
|
|
|
2013-08-12 22:21:29 +08:00
|
|
|
#include <string.h>
|
2021-12-17 15:34:17 +08:00
|
|
|
#include <fcntl.h>
|
2021-07-21 01:43:48 +08:00
|
|
|
#include <sys/signal.h>
|
2021-12-17 15:34:17 +08:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <rtthread.h>
|
2023-05-05 18:14:07 +08:00
|
|
|
#include <limits.h>
|
2013-01-08 22:40:58 +08:00
|
|
|
#include "mqueue.h"
|
|
|
|
|
2023-05-05 18:14:07 +08:00
|
|
|
static mqdes_t posix_mq_list = RT_NULL;
|
2013-01-08 22:40:58 +08:00
|
|
|
static struct rt_semaphore posix_mq_lock;
|
2021-12-17 15:34:17 +08:00
|
|
|
|
|
|
|
/* initialize posix mqueue */
|
|
|
|
static int posix_mq_system_init(void)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2013-06-26 23:18:30 +08:00
|
|
|
rt_sem_init(&posix_mq_lock, "pmq", 1, RT_IPC_FLAG_FIFO);
|
2021-12-17 15:34:17 +08:00
|
|
|
return 0;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
2021-12-17 15:34:17 +08:00
|
|
|
INIT_COMPONENT_EXPORT(posix_mq_system_init);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2023-05-05 18:14:07 +08:00
|
|
|
rt_inline void posix_mq_insert(mqdes_t pmq)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2023-05-05 18:14:07 +08:00
|
|
|
if (posix_mq_list == RT_NULL)
|
|
|
|
pmq->mq_id = 1;
|
|
|
|
else
|
|
|
|
pmq->mq_id = posix_mq_list->mq_id + 1;
|
2013-06-26 23:18:30 +08:00
|
|
|
pmq->next = posix_mq_list;
|
|
|
|
posix_mq_list = pmq;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
|
2023-05-05 18:14:07 +08:00
|
|
|
static void posix_mq_delete(mqdes_t pmq)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2023-05-05 18:14:07 +08:00
|
|
|
mqdes_t iter;
|
2013-06-26 23:18:30 +08:00
|
|
|
if (posix_mq_list == pmq)
|
|
|
|
{
|
|
|
|
posix_mq_list = pmq->next;
|
|
|
|
|
|
|
|
rt_mq_delete(pmq->mq);
|
|
|
|
rt_free(pmq);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (iter = posix_mq_list; iter->next != RT_NULL; iter = iter->next)
|
|
|
|
{
|
|
|
|
if (iter->next == pmq)
|
|
|
|
{
|
|
|
|
/* delete this mq */
|
|
|
|
if (pmq->next != RT_NULL)
|
|
|
|
iter->next = pmq->next;
|
|
|
|
else
|
|
|
|
iter->next = RT_NULL;
|
|
|
|
|
|
|
|
/* delete RT-Thread mqueue */
|
|
|
|
rt_mq_delete(pmq->mq);
|
|
|
|
rt_free(pmq);
|
|
|
|
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
}
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
|
2023-05-05 18:14:07 +08:00
|
|
|
static mqdes_t posix_mq_find(const char *name)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2023-05-05 18:14:07 +08:00
|
|
|
mqdes_t iter;
|
2013-06-26 23:18:30 +08:00
|
|
|
rt_object_t object;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
for (iter = posix_mq_list; iter != RT_NULL; iter = iter->next)
|
|
|
|
{
|
|
|
|
object = (rt_object_t)(iter->mq);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
if (strncmp(object->name, name, RT_NAME_MAX) == 0)
|
|
|
|
{
|
|
|
|
return iter;
|
|
|
|
}
|
|
|
|
}
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
return RT_NULL;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
|
2023-05-05 18:14:07 +08:00
|
|
|
static mqdes_t posix_mq_id_find(mqd_t id)
|
|
|
|
{
|
|
|
|
for (mqdes_t iter = posix_mq_list; iter != RT_NULL; iter = iter->next)
|
|
|
|
if (iter->mq_id == id)
|
|
|
|
return iter;
|
|
|
|
return RT_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-05 18:14:07 +08:00
|
|
|
rt_sem_take(&posix_mq_lock, RT_WAITING_FOREVER);
|
|
|
|
mqdes_t mqdes = posix_mq_id_find(id);
|
|
|
|
rt_sem_release(&posix_mq_lock);
|
2013-06-26 23:18:30 +08:00
|
|
|
if ((mqdes == RT_NULL) || mqstat == RT_NULL)
|
|
|
|
{
|
|
|
|
rt_set_errno(EBADF);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
mqstat->mq_maxmsg = mqdes->mq->max_msgs;
|
|
|
|
mqstat->mq_msgsize = mqdes->mq->msg_size;
|
|
|
|
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, ...)
|
|
|
|
{
|
2013-06-26 23:18:30 +08:00
|
|
|
va_list arg;
|
2014-07-19 06:52:35 +08:00
|
|
|
mode_t mode;
|
2023-05-05 18:14:07 +08:00
|
|
|
mqdes_t mqdes = RT_NULL;
|
2013-06-26 23:18:30 +08:00
|
|
|
struct mq_attr *attr = RT_NULL;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
/* lock posix mqueue list */
|
|
|
|
rt_sem_take(&posix_mq_lock, RT_WAITING_FOREVER);
|
2023-05-05 18:14:07 +08:00
|
|
|
int len = rt_strlen(name);
|
2023-05-14 11:15:36 +08:00
|
|
|
if (len > PATH_MAX || len > RT_NAME_MAX)
|
2023-05-05 18:14:07 +08:00
|
|
|
{
|
|
|
|
rt_set_errno(ENAMETOOLONG);
|
|
|
|
goto __return;
|
|
|
|
}
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2023-01-07 10:09:40 +08:00
|
|
|
mqdes = posix_mq_find(name);
|
|
|
|
if (mqdes != RT_NULL)
|
|
|
|
{
|
2023-05-05 18:14:07 +08:00
|
|
|
if (oflag & O_CREAT && oflag & O_EXCL)
|
|
|
|
{
|
|
|
|
rt_set_errno(EEXIST);
|
|
|
|
rt_sem_release(&posix_mq_lock);
|
|
|
|
return (mqd_t)(-1);
|
|
|
|
}
|
|
|
|
mqdes->refcount++; /* increase reference count */
|
2023-01-07 10:09:40 +08:00
|
|
|
}
|
|
|
|
else if (oflag & O_CREAT)
|
2013-06-26 23:18:30 +08:00
|
|
|
{
|
|
|
|
va_start(arg, oflag);
|
2014-07-19 06:52:35 +08:00
|
|
|
mode = (mode_t)va_arg(arg, unsigned int);
|
2023-05-14 11:15:36 +08:00
|
|
|
mode = (mode_t)mode; /* self-assignment avoids compiler optimization */
|
2013-06-26 23:18:30 +08:00
|
|
|
attr = (struct mq_attr *)va_arg(arg, struct mq_attr *);
|
2023-05-14 11:15:36 +08:00
|
|
|
attr = (struct mq_attr *)attr; /* self-assignment avoids compiler optimization */
|
2013-06-26 23:18:30 +08:00
|
|
|
va_end(arg);
|
|
|
|
|
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);
|
|
|
|
goto __return;
|
2013-06-26 23:18:30 +08:00
|
|
|
}
|
2023-05-05 18:14:07 +08:00
|
|
|
|
|
|
|
mqdes = (mqdes_t) rt_malloc (sizeof(struct mqdes));
|
2013-06-26 23:18:30 +08:00
|
|
|
if (mqdes == RT_NULL)
|
|
|
|
{
|
|
|
|
rt_set_errno(ENFILE);
|
|
|
|
goto __return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create RT-Thread message queue */
|
|
|
|
mqdes->mq = rt_mq_create(name, attr->mq_msgsize, attr->mq_maxmsg, RT_IPC_FLAG_FIFO);
|
|
|
|
if (mqdes->mq == RT_NULL) /* create failed */
|
|
|
|
{
|
|
|
|
rt_set_errno(ENFILE);
|
|
|
|
goto __return;
|
|
|
|
}
|
|
|
|
/* initialize reference count */
|
|
|
|
mqdes->refcount = 1;
|
|
|
|
mqdes->unlinked = 0;
|
|
|
|
|
|
|
|
/* insert mq to posix mq list */
|
|
|
|
posix_mq_insert(mqdes);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-01-07 10:09:40 +08:00
|
|
|
rt_set_errno(ENOENT);
|
|
|
|
goto __return;
|
2013-06-26 23:18:30 +08:00
|
|
|
}
|
|
|
|
rt_sem_release(&posix_mq_lock);
|
|
|
|
|
2023-05-05 18:14:07 +08:00
|
|
|
return (mqd_t)(mqdes->mq_id);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
__return:
|
2013-06-26 23:18:30 +08:00
|
|
|
/* release lock */
|
|
|
|
rt_sem_release(&posix_mq_lock);
|
|
|
|
|
|
|
|
/* release allocated memory */
|
|
|
|
if (mqdes != RT_NULL)
|
|
|
|
{
|
|
|
|
if (mqdes->mq != RT_NULL)
|
|
|
|
{
|
|
|
|
/* delete RT-Thread message queue */
|
|
|
|
rt_mq_delete(mqdes->mq);
|
|
|
|
}
|
|
|
|
rt_free(mqdes);
|
|
|
|
}
|
2023-05-05 18:14:07 +08:00
|
|
|
return (mqd_t)(-1);
|
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-05-05 18:14:07 +08:00
|
|
|
rt_sem_take(&posix_mq_lock, RT_WAITING_FOREVER);
|
|
|
|
mqdes_t mqdes = posix_mq_id_find(id);
|
|
|
|
rt_sem_release(&posix_mq_lock);
|
2013-06-26 23:18:30 +08:00
|
|
|
rt_err_t result;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
if ((mqdes == RT_NULL) || (msg_ptr == RT_NULL))
|
|
|
|
{
|
|
|
|
rt_set_errno(EINVAL);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
result = rt_mq_recv(mqdes->mq, msg_ptr, msg_len, RT_WAITING_FOREVER);
|
|
|
|
if (result == RT_EOK)
|
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-05-05 18:14:07 +08:00
|
|
|
rt_sem_take(&posix_mq_lock, RT_WAITING_FOREVER);
|
|
|
|
mqdes_t mqdes = posix_mq_id_find(id);
|
|
|
|
rt_sem_release(&posix_mq_lock);
|
2013-06-26 23:18:30 +08:00
|
|
|
rt_err_t result;
|
|
|
|
|
|
|
|
if ((mqdes == RT_NULL) || (msg_ptr == RT_NULL))
|
|
|
|
{
|
|
|
|
rt_set_errno(EINVAL);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = rt_mq_send(mqdes->mq, (void*)msg_ptr, msg_len);
|
|
|
|
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-05-05 18:14:07 +08:00
|
|
|
rt_sem_take(&posix_mq_lock, RT_WAITING_FOREVER);
|
|
|
|
mqdes_t mqdes = posix_mq_id_find(id);
|
|
|
|
rt_sem_release(&posix_mq_lock);
|
2023-01-06 09:55:15 +08:00
|
|
|
int tick = 0;
|
2013-06-26 23:18:30 +08:00
|
|
|
rt_err_t result;
|
|
|
|
|
|
|
|
/* parameters check */
|
|
|
|
if ((mqdes == RT_NULL) || (msg_ptr == RT_NULL))
|
|
|
|
{
|
|
|
|
rt_set_errno(EINVAL);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2023-01-06 10:07:06 +08:00
|
|
|
if (abs_timeout != RT_NULL)
|
|
|
|
tick = rt_timespec_to_tick(abs_timeout);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
result = rt_mq_recv(mqdes->mq, msg_ptr, msg_len, tick);
|
|
|
|
if (result == RT_EOK)
|
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-05-05 18:14:07 +08:00
|
|
|
rt_sem_take(&posix_mq_lock, RT_WAITING_FOREVER);
|
|
|
|
mqdes_t mqdes = posix_mq_id_find(id);
|
|
|
|
rt_sem_release(&posix_mq_lock);
|
|
|
|
if (mqdes == RT_NULL || mqdes->refcount == 0)
|
|
|
|
{
|
|
|
|
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-05-05 18:14:07 +08:00
|
|
|
rt_sem_take(&posix_mq_lock, RT_WAITING_FOREVER);
|
|
|
|
mqdes_t mqdes = posix_mq_id_find(id);
|
|
|
|
rt_sem_release(&posix_mq_lock);
|
2013-06-26 23:18:30 +08:00
|
|
|
if (mqdes == RT_NULL)
|
|
|
|
{
|
2023-05-05 18:14:07 +08:00
|
|
|
rt_set_errno(EBADF);
|
2013-06-26 23:18:30 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
/* lock posix mqueue list */
|
|
|
|
rt_sem_take(&posix_mq_lock, RT_WAITING_FOREVER);
|
|
|
|
mqdes->refcount --;
|
|
|
|
if (mqdes->refcount == 0)
|
|
|
|
{
|
2013-06-26 23:18:30 +08:00
|
|
|
/* delete from posix mqueue list */
|
|
|
|
if (mqdes->unlinked)
|
|
|
|
posix_mq_delete(mqdes);
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
rt_sem_release(&posix_mq_lock);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(mq_close);
|
|
|
|
|
2023-05-22 13:47:01 +08:00
|
|
|
/**
|
|
|
|
* @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)
|
|
|
|
{
|
2023-05-05 18:14:07 +08:00
|
|
|
mqdes_t pmq;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
/* lock posix mqueue list */
|
|
|
|
rt_sem_take(&posix_mq_lock, RT_WAITING_FOREVER);
|
|
|
|
pmq = posix_mq_find(name);
|
|
|
|
if (pmq != RT_NULL)
|
|
|
|
{
|
2013-06-26 23:18:30 +08:00
|
|
|
pmq->unlinked = 1;
|
|
|
|
if (pmq->refcount == 0)
|
|
|
|
{
|
|
|
|
/* remove this mqueue */
|
|
|
|
posix_mq_delete(pmq);
|
|
|
|
}
|
2013-01-08 22:40:58 +08:00
|
|
|
rt_sem_release(&posix_mq_lock);
|
2013-06-26 23:18:30 +08:00
|
|
|
|
2013-01-08 22:40:58 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
rt_sem_release(&posix_mq_lock);
|
|
|
|
|
|
|
|
/* no this entry */
|
|
|
|
rt_set_errno(ENOENT);
|
2013-06-26 23:18:30 +08:00
|
|
|
|
2013-01-08 22:40:58 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(mq_unlink);
|