2008-01-30 15:01:22 +01:00
|
|
|
/****************************************************************************
|
2014-09-29 23:48:52 +02:00
|
|
|
* fs/mqueue/mq_open.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2015-09-23 18:34:08 +02:00
|
|
|
* Copyright (C) 2007-2009, 2011, 2014-2015 Gregory Nutt. All rights reserved.
|
2012-07-14 21:30:31 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-02-18 00:21:28 +01: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 15:01:22 +01:00
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
2007-02-18 00:21:28 +01: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 15:01:22 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-30 15:01:22 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Included Files
|
2008-01-30 15:01:22 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
2014-09-29 22:59:31 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <sched.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <mqueue.h>
|
2007-03-20 17:51:12 +01:00
|
|
|
#include <fcntl.h>
|
2007-02-21 22:55:16 +01:00
|
|
|
#include <errno.h>
|
2014-09-29 22:59:31 +02:00
|
|
|
|
|
|
|
#include <nuttx/mqueue.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
|
|
|
|
#include "inode/inode.h"
|
|
|
|
#include "mqueue/mqueue.h"
|
|
|
|
|
2008-01-30 15:01:22 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Public Functions
|
2008-01-30 15:01:22 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-30 15:01:22 +01:00
|
|
|
/****************************************************************************
|
2012-07-14 21:30:31 +02:00
|
|
|
* Name: mq_open
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2012-07-14 21:30:31 +02: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 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* mq_name - Name of the queue to open
|
|
|
|
* oflags - open flags
|
2012-07-14 21:30:31 +02:00
|
|
|
* Optional parameters. When the O_CREAT flag is specified, two optional
|
|
|
|
* parameters are expected:
|
|
|
|
*
|
2007-02-18 00:21:28 +01: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.
|
|
|
|
*
|
|
|
|
* Return Value:
|
2014-09-30 00:46:01 +02:00
|
|
|
* A message queue descriptor or (mqd_t)-1 (ERROR)
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
2008-01-30 15:01:22 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2014-09-30 00:46:01 +02:00
|
|
|
mqd_t mq_open(FAR const char *mq_name, int oflags, ...)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2014-09-29 22:59:31 +02:00
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR const char *relpath = NULL;
|
2014-09-29 21:19:11 +02:00
|
|
|
FAR struct mqueue_inode_s *msgq;
|
2014-09-29 22:59:31 +02:00
|
|
|
char fullpath[MAX_MQUEUE_PATH];
|
|
|
|
va_list ap;
|
2014-09-29 22:09:31 +02:00
|
|
|
struct mq_attr *attr;
|
2014-09-29 22:59:31 +02:00
|
|
|
mqd_t mqdes;
|
2014-09-29 22:09:31 +02:00
|
|
|
mode_t mode;
|
2014-09-29 22:59:31 +02:00
|
|
|
int errcode;
|
|
|
|
int ret;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Make sure that a non-NULL name is supplied */
|
|
|
|
|
2014-09-30 00:46:01 +02:00
|
|
|
if (!mq_name)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2014-09-30 00:46:01 +02:00
|
|
|
errcode = EINVAL;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the full path to the message queue */
|
2014-09-29 22:59:31 +02:00
|
|
|
|
2014-09-30 00:46:01 +02:00
|
|
|
snprintf(fullpath, MAX_MQUEUE_PATH, CONFIG_FS_MQUEUE_MPATH "/%s", mq_name);
|
2014-09-29 22:59:31 +02:00
|
|
|
|
2014-09-30 00:46:01 +02: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-29 22:59:31 +02:00
|
|
|
|
2014-09-30 00:46:01 +02:00
|
|
|
sched_lock();
|
|
|
|
|
|
|
|
/* Get the inode for this mqueue. This should succeed if the message
|
2015-09-23 18:34:08 +02:00
|
|
|
* queue has already been created. In this case, inode_finde() will
|
|
|
|
* have incremented the reference count on the inode.
|
2014-09-30 00:46:01 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
inode = inode_find(fullpath, &relpath);
|
|
|
|
if (inode)
|
|
|
|
{
|
|
|
|
/* It exists. Verify that the inode is a message queue */
|
2014-09-29 22:59:31 +02:00
|
|
|
|
2014-09-30 00:46:01 +02: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-29 22:59:31 +02:00
|
|
|
*/
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2014-09-30 00:46:01 +02:00
|
|
|
errcode = EEXIST;
|
|
|
|
goto errout_with_inode;
|
2014-09-29 22:59:31 +02:00
|
|
|
}
|
2014-09-30 00:46:01 +02:00
|
|
|
|
|
|
|
/* Create a message queue descriptor for the current thread */
|
|
|
|
|
|
|
|
msgq = inode->u.i_mqueue;
|
|
|
|
mqdes = mq_descreate(NULL, msgq, oflags);
|
|
|
|
if (!mqdes)
|
2014-09-29 22:59:31 +02:00
|
|
|
{
|
2014-09-30 00:46:01 +02:00
|
|
|
errcode = ENOMEM;
|
|
|
|
goto errout_with_inode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* The mqueue does not exists. Were we asked to create it? */
|
2014-09-29 22:09:31 +02:00
|
|
|
|
2014-09-30 00:46:01 +02:00
|
|
|
if ((oflags & O_CREAT) == 0)
|
|
|
|
{
|
|
|
|
/* The mqueue does not exist and O_CREAT is not set */
|
2014-09-29 22:59:31 +02:00
|
|
|
|
2014-09-30 00:46:01 +02:00
|
|
|
errcode = ENOENT;
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
2014-09-29 22:09:31 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
/* Create the mqueue. First we have to extract the additional
|
|
|
|
* parameters from the variable argument list.
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2014-09-30 00:46:01 +02:00
|
|
|
va_start(ap, oflags);
|
|
|
|
mode = va_arg(ap, mode_t);
|
2015-10-11 19:39:29 +02:00
|
|
|
attr = va_arg(ap, FAR struct mq_attr *);
|
2014-09-30 00:46:01 +02:00
|
|
|
va_end(ap);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2014-09-30 00:46:01 +02:00
|
|
|
/* Create an inode in the pseudo-filesystem at this path */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2014-09-30 00:46:01 +02:00
|
|
|
inode_semtake();
|
|
|
|
ret = inode_reserve(fullpath, &inode);
|
|
|
|
inode_semgive();
|
2014-09-29 22:59:31 +02:00
|
|
|
|
2014-09-30 00:46:01 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
errcode = -ret;
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
2012-07-14 21:30:31 +02:00
|
|
|
|
2015-09-23 18:34:08 +02:00
|
|
|
/* Allocate memory for the new message queue. The new inode will
|
|
|
|
* be created with a reference count of zero.
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
msgq = (FAR struct mqueue_inode_s *)mq_msgqalloc(mode, attr);
|
2014-09-30 00:46:01 +02:00
|
|
|
if (!msgq)
|
|
|
|
{
|
2014-12-06 14:18:48 +01:00
|
|
|
errcode = ENOSPC;
|
2014-09-30 00:46:01 +02:00
|
|
|
goto errout_with_inode;
|
|
|
|
}
|
2014-09-29 22:59:31 +02:00
|
|
|
|
2014-09-30 00:46:01 +02:00
|
|
|
/* Create a message queue descriptor for the TCB */
|
2014-09-29 22:59:31 +02:00
|
|
|
|
2014-09-30 00:46:01 +02:00
|
|
|
mqdes = mq_descreate(NULL, msgq, oflags);
|
|
|
|
if (!mqdes)
|
|
|
|
{
|
|
|
|
errcode = ENOMEM;
|
|
|
|
goto errout_with_msgq;
|
|
|
|
}
|
2014-09-29 22:59:31 +02:00
|
|
|
|
2014-09-30 00:46:01 +02:00
|
|
|
/* Bind the message queue and the inode structure */
|
2014-09-29 22:59:31 +02:00
|
|
|
|
2014-09-30 00:46:01 +02:00
|
|
|
INODE_SET_MQUEUE(inode);
|
|
|
|
inode->u.i_mqueue = msgq;
|
|
|
|
msgq->inode = inode;
|
2014-09-30 00:13:07 +02:00
|
|
|
|
2015-09-23 18:34:08 +02:00
|
|
|
/* Set the initial reference count on this inode to one */
|
|
|
|
|
|
|
|
inode->i_crefs = 1;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
2014-09-29 22:59:31 +02:00
|
|
|
|
2014-09-30 00:46:01 +02:00
|
|
|
sched_unlock();
|
2014-09-29 22:59:31 +02:00
|
|
|
return mqdes;
|
|
|
|
|
|
|
|
errout_with_msgq:
|
|
|
|
mq_msgqfree(msgq);
|
|
|
|
inode->u.i_mqueue = NULL;
|
|
|
|
errout_with_inode:
|
|
|
|
inode_release(inode);
|
|
|
|
errout_with_lock:
|
|
|
|
sched_unlock();
|
2014-09-30 00:46:01 +02:00
|
|
|
errout:
|
2014-09-29 22:59:31 +02:00
|
|
|
set_errno(errcode);
|
|
|
|
return (mqd_t)ERROR;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|