2008-01-30 22:01:22 +08:00
|
|
|
/****************************************************************************
|
2014-09-30 05:48:52 +08:00
|
|
|
* fs/mqueue/mq_open.c
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2017-02-05 01:46:54 +08:00
|
|
|
* Copyright (C) 2007-2009, 2011, 2014-2015, 2017 Gregory Nutt. All rights reserved.
|
2012-07-15 03:30:31 +08:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
2008-01-30 22:01:22 +08:00
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
2007-02-18 07:21:28 +08:00
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
2008-01-30 22:01:22 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2008-01-30 22:01:22 +08:00
|
|
|
/****************************************************************************
|
2007-02-18 07:21:28 +08:00
|
|
|
* Included Files
|
2008-01-30 22:01:22 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2009-12-15 02:39:29 +08:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2017-02-05 01:46:54 +08:00
|
|
|
#include <stdbool.h>
|
2009-12-15 02:39:29 +08:00
|
|
|
#include <stdarg.h>
|
2014-09-30 04:59:31 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <sched.h>
|
2007-02-18 07:21:28 +08:00
|
|
|
#include <mqueue.h>
|
2007-03-21 00:51:12 +08:00
|
|
|
#include <fcntl.h>
|
2007-02-22 05:55:16 +08:00
|
|
|
#include <errno.h>
|
2014-09-30 04:59:31 +08:00
|
|
|
|
|
|
|
#include <nuttx/mqueue.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
|
|
|
|
#include "inode/inode.h"
|
|
|
|
#include "mqueue/mqueue.h"
|
|
|
|
|
2008-01-30 22:01:22 +08:00
|
|
|
/****************************************************************************
|
2007-02-18 07:21:28 +08:00
|
|
|
* Public Functions
|
2008-01-30 22:01:22 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2008-01-30 22:01:22 +08:00
|
|
|
/****************************************************************************
|
2012-07-15 03:30:31 +08:00
|
|
|
* Name: mq_open
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
|
|
|
* Description:
|
2012-07-15 03:30:31 +08:00
|
|
|
* This function establish a connection between a named message queue and
|
|
|
|
* the calling task. After a successful call of mq_open(), the task can
|
|
|
|
* reference the message queue using the address returned by the call. The
|
|
|
|
* message queue remains usable until it is closed by a successful call to
|
|
|
|
* mq_close().
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2018-03-13 23:52:27 +08:00
|
|
|
* Input Parameters:
|
2007-02-18 07:21:28 +08:00
|
|
|
* mq_name - Name of the queue to open
|
|
|
|
* oflags - open flags
|
2012-07-15 03:30:31 +08:00
|
|
|
* Optional parameters. When the O_CREAT flag is specified, two optional
|
|
|
|
* parameters are expected:
|
|
|
|
*
|
2007-02-18 07:21:28 +08:00
|
|
|
* 1. mode_t mode (ignored), and
|
|
|
|
* 2. struct mq_attr *attr. The mq_maxmsg attribute
|
|
|
|
* is used at the time that the message queue is
|
|
|
|
* created to determine the maximum number of
|
|
|
|
* messages that may be placed in the message queue.
|
|
|
|
*
|
2018-02-02 00:00:02 +08:00
|
|
|
* Returned Value:
|
2014-09-30 06:46:01 +08:00
|
|
|
* A message queue descriptor or (mqd_t)-1 (ERROR)
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
2008-01-30 22:01:22 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2014-09-30 06:46:01 +08:00
|
|
|
mqd_t mq_open(FAR const char *mq_name, int oflags, ...)
|
2007-02-18 07:21:28 +08:00
|
|
|
{
|
2014-09-30 04:59:31 +08:00
|
|
|
FAR struct inode *inode;
|
2014-09-30 03:19:11 +08:00
|
|
|
FAR struct mqueue_inode_s *msgq;
|
2017-02-05 23:51:42 +08:00
|
|
|
struct inode_search_s desc;
|
2014-09-30 04:59:31 +08:00
|
|
|
char fullpath[MAX_MQUEUE_PATH];
|
|
|
|
va_list ap;
|
2014-09-30 04:09:31 +08:00
|
|
|
struct mq_attr *attr;
|
2014-09-30 04:59:31 +08:00
|
|
|
mqd_t mqdes;
|
2014-09-30 04:09:31 +08:00
|
|
|
mode_t mode;
|
2014-09-30 04:59:31 +08:00
|
|
|
int errcode;
|
|
|
|
int ret;
|
2007-02-18 07:21:28 +08:00
|
|
|
|
|
|
|
/* Make sure that a non-NULL name is supplied */
|
|
|
|
|
2017-01-10 21:07:17 +08:00
|
|
|
if (mq_name == NULL || *mq_name == '\0')
|
2007-02-18 07:21:28 +08:00
|
|
|
{
|
2014-09-30 06:46:01 +08:00
|
|
|
errcode = EINVAL;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2017-01-08 23:14:11 +08:00
|
|
|
/* Skip over any leading '/'. All message queue paths are relative to
|
|
|
|
* CONFIG_FS_MQUEUE_MPATH.
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (*mq_name == '/')
|
|
|
|
{
|
|
|
|
mq_name++;
|
|
|
|
}
|
|
|
|
|
2014-09-30 06:46:01 +08:00
|
|
|
/* Get the full path to the message queue */
|
2014-09-30 04:59:31 +08:00
|
|
|
|
2014-09-30 06:46:01 +08:00
|
|
|
snprintf(fullpath, MAX_MQUEUE_PATH, CONFIG_FS_MQUEUE_MPATH "/%s", mq_name);
|
2014-09-30 04:59:31 +08:00
|
|
|
|
2014-09-30 06:46:01 +08:00
|
|
|
/* Make sure that the check for the existence of the message queue
|
|
|
|
* and the creation of the message queue are atomic with respect to
|
|
|
|
* other processes executing mq_open(). A simple sched_lock() should
|
|
|
|
* be sufficient.
|
|
|
|
*/
|
2014-09-30 04:59:31 +08:00
|
|
|
|
2014-09-30 06:46:01 +08:00
|
|
|
sched_lock();
|
|
|
|
|
|
|
|
/* Get the inode for this mqueue. This should succeed if the message
|
2017-02-05 01:46:54 +08:00
|
|
|
* queue has already been created. In this case, inode_find() will
|
2015-09-24 00:34:08 +08:00
|
|
|
* have incremented the reference count on the inode.
|
2014-09-30 06:46:01 +08:00
|
|
|
*/
|
|
|
|
|
2017-02-06 04:25:45 +08:00
|
|
|
SETUP_SEARCH(&desc, fullpath, false);
|
2017-02-05 23:51:42 +08:00
|
|
|
|
|
|
|
ret = inode_find(&desc);
|
|
|
|
if (ret >= 0)
|
2014-09-30 06:46:01 +08:00
|
|
|
{
|
2017-02-05 23:51:42 +08:00
|
|
|
/* Something exists at this path. Get the search results */
|
|
|
|
|
|
|
|
inode = desc.node;
|
|
|
|
DEBUGASSERT(inode != NULL);
|
|
|
|
|
|
|
|
/* Verify that the inode is a message queue */
|
2014-09-30 04:59:31 +08:00
|
|
|
|
2014-09-30 06:46:01 +08:00
|
|
|
if (!INODE_IS_MQUEUE(inode))
|
|
|
|
{
|
|
|
|
errcode = ENXIO;
|
|
|
|
goto errout_with_inode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* It exists and is a message queue. Check if the caller wanted to
|
|
|
|
* create a new mqueue with this name.
|
2014-09-30 04:59:31 +08:00
|
|
|
*/
|
|
|
|
|
2015-10-12 01:39:29 +08:00
|
|
|
if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
|
2007-02-18 07:21:28 +08:00
|
|
|
{
|
2014-09-30 06:46:01 +08:00
|
|
|
errcode = EEXIST;
|
|
|
|
goto errout_with_inode;
|
2014-09-30 04:59:31 +08:00
|
|
|
}
|
2014-09-30 06:46:01 +08:00
|
|
|
|
|
|
|
/* Create a message queue descriptor for the current thread */
|
|
|
|
|
|
|
|
msgq = inode->u.i_mqueue;
|
2017-10-10 22:43:10 +08:00
|
|
|
mqdes = nxmq_create_des(NULL, msgq, oflags);
|
2014-09-30 06:46:01 +08:00
|
|
|
if (!mqdes)
|
2014-09-30 04:59:31 +08:00
|
|
|
{
|
2014-09-30 06:46:01 +08:00
|
|
|
errcode = ENOMEM;
|
|
|
|
goto errout_with_inode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* The mqueue does not exists. Were we asked to create it? */
|
2014-09-30 04:09:31 +08:00
|
|
|
|
2014-09-30 06:46:01 +08:00
|
|
|
if ((oflags & O_CREAT) == 0)
|
|
|
|
{
|
|
|
|
/* The mqueue does not exist and O_CREAT is not set */
|
2014-09-30 04:59:31 +08:00
|
|
|
|
2014-09-30 06:46:01 +08:00
|
|
|
errcode = ENOENT;
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
2014-09-30 04:09:31 +08:00
|
|
|
|
2015-10-12 01:39:29 +08:00
|
|
|
/* Create the mqueue. First we have to extract the additional
|
|
|
|
* parameters from the variable argument list.
|
|
|
|
*/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2014-09-30 06:46:01 +08:00
|
|
|
va_start(ap, oflags);
|
|
|
|
mode = va_arg(ap, mode_t);
|
2015-10-12 01:39:29 +08:00
|
|
|
attr = va_arg(ap, FAR struct mq_attr *);
|
2014-09-30 06:46:01 +08:00
|
|
|
va_end(ap);
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2014-09-30 06:46:01 +08:00
|
|
|
/* Create an inode in the pseudo-filesystem at this path */
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2014-09-30 06:46:01 +08:00
|
|
|
inode_semtake();
|
|
|
|
ret = inode_reserve(fullpath, &inode);
|
|
|
|
inode_semgive();
|
2014-09-30 04:59:31 +08:00
|
|
|
|
2014-09-30 06:46:01 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
errcode = -ret;
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
2012-07-15 03:30:31 +08:00
|
|
|
|
2015-09-24 00:34:08 +08:00
|
|
|
/* Allocate memory for the new message queue. The new inode will
|
|
|
|
* be created with a reference count of zero.
|
|
|
|
*/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2017-10-10 22:43:10 +08:00
|
|
|
msgq = (FAR struct mqueue_inode_s *)nxmq_alloc_msgq(mode, attr);
|
2014-09-30 06:46:01 +08:00
|
|
|
if (!msgq)
|
|
|
|
{
|
2014-12-06 21:18:48 +08:00
|
|
|
errcode = ENOSPC;
|
2014-09-30 06:46:01 +08:00
|
|
|
goto errout_with_inode;
|
|
|
|
}
|
2014-09-30 04:59:31 +08:00
|
|
|
|
2014-09-30 06:46:01 +08:00
|
|
|
/* Create a message queue descriptor for the TCB */
|
2014-09-30 04:59:31 +08:00
|
|
|
|
2019-10-28 01:48:14 +08:00
|
|
|
mqdes = nxmq_create_des(NULL, msgq, oflags);
|
|
|
|
if (!mqdes)
|
|
|
|
{
|
|
|
|
errcode = ENOMEM;
|
|
|
|
goto errout_with_msgq;
|
|
|
|
}
|
2014-09-30 04:59:31 +08:00
|
|
|
|
2014-09-30 06:46:01 +08:00
|
|
|
/* Bind the message queue and the inode structure */
|
2014-09-30 04:59:31 +08:00
|
|
|
|
2014-09-30 06:46:01 +08:00
|
|
|
INODE_SET_MQUEUE(inode);
|
|
|
|
inode->u.i_mqueue = msgq;
|
|
|
|
msgq->inode = inode;
|
2014-09-30 06:13:07 +08:00
|
|
|
|
2015-09-24 00:34:08 +08:00
|
|
|
/* Set the initial reference count on this inode to one */
|
|
|
|
|
|
|
|
inode->i_crefs = 1;
|
2007-02-18 07:21:28 +08:00
|
|
|
}
|
2014-09-30 04:59:31 +08:00
|
|
|
|
2017-02-06 04:25:45 +08:00
|
|
|
RELEASE_SEARCH(&desc);
|
2014-09-30 06:46:01 +08:00
|
|
|
sched_unlock();
|
2014-09-30 04:59:31 +08:00
|
|
|
return mqdes;
|
|
|
|
|
|
|
|
errout_with_msgq:
|
2017-10-10 22:43:10 +08:00
|
|
|
nxmq_free_msgq(msgq);
|
2014-09-30 04:59:31 +08:00
|
|
|
inode->u.i_mqueue = NULL;
|
2017-02-06 04:25:45 +08:00
|
|
|
|
2014-09-30 04:59:31 +08:00
|
|
|
errout_with_inode:
|
|
|
|
inode_release(inode);
|
2017-02-06 04:25:45 +08:00
|
|
|
|
2014-09-30 04:59:31 +08:00
|
|
|
errout_with_lock:
|
2017-02-06 04:25:45 +08:00
|
|
|
RELEASE_SEARCH(&desc);
|
2014-09-30 04:59:31 +08:00
|
|
|
sched_unlock();
|
2017-02-06 04:25:45 +08:00
|
|
|
|
2014-09-30 06:46:01 +08:00
|
|
|
errout:
|
2014-09-30 04:59:31 +08:00
|
|
|
set_errno(errcode);
|
|
|
|
return (mqd_t)ERROR;
|
2007-02-18 07:21:28 +08:00
|
|
|
}
|