2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2014-09-29 23:48:52 +02:00
|
|
|
* fs/mqueue/mq_unlink.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2020-03-30 01:36:19 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2020-03-30 01:36:19 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2020-03-30 01:36:19 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Included Files
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2009-12-14 19:39:29 +01:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2017-02-04 18:46:54 +01:00
|
|
|
#include <stdbool.h>
|
2014-09-29 23:48:52 +02:00
|
|
|
#include <stdio.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <mqueue.h>
|
2014-09-29 23:48:52 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
2009-12-14 22:15:18 +01:00
|
|
|
|
2014-09-29 23:48:52 +02:00
|
|
|
#include <nuttx/mqueue.h>
|
|
|
|
|
|
|
|
#include "inode/inode.h"
|
2014-08-08 20:31:23 +02:00
|
|
|
#include "mqueue/mqueue.h"
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2020-12-22 15:02:48 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mq_inode_release
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Release a reference count on a message queue inode.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* inode - The message queue inode
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void mq_inode_release(FAR struct inode *inode)
|
|
|
|
{
|
|
|
|
if (inode->i_crefs <= 1)
|
|
|
|
{
|
|
|
|
FAR struct mqueue_inode_s *msgq = inode->i_private;
|
|
|
|
|
|
|
|
if (msgq)
|
|
|
|
{
|
|
|
|
nxmq_free_msgq(msgq);
|
|
|
|
inode->i_private = NULL;
|
|
|
|
}
|
2021-10-25 09:52:19 +02:00
|
|
|
|
|
|
|
inode_release(inode);
|
2020-12-22 15:02:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Public Functions
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2020-12-24 13:53:17 +01:00
|
|
|
* Name: file_mq_unlink
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2020-05-03 22:31:05 +02:00
|
|
|
* This is an internal OS interface. It is functionally equivalent to
|
|
|
|
* mq_unlink() except that:
|
|
|
|
*
|
|
|
|
* - It is not a cancellation point, and
|
|
|
|
* - It does not modify the errno value.
|
|
|
|
*
|
|
|
|
* See comments with mq_unlink() for a more complete description of the
|
|
|
|
* behavior of this function
|
2014-04-13 22:32:20 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2007-02-18 00:21:28 +01:00
|
|
|
* mq_name - Name of the message queue
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2020-05-03 22:31:05 +02:00
|
|
|
* This is an internal OS interface and should not be used by applications.
|
|
|
|
* It follows the NuttX internal error return policy: Zero (OK) is
|
|
|
|
* returned on success. A negated errno value is returned on failure.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2020-12-24 13:53:17 +01:00
|
|
|
int file_mq_unlink(FAR const char *mq_name)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2014-09-29 23:48:52 +02:00
|
|
|
FAR struct inode *inode;
|
2017-02-05 16:51:42 +01:00
|
|
|
struct inode_search_s desc;
|
2014-09-29 23:48:52 +02:00
|
|
|
char fullpath[MAX_MQUEUE_PATH];
|
|
|
|
int ret;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2014-09-29 23:48:52 +02:00
|
|
|
/* Get the full path to the message queue */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2022-02-05 17:30:34 +01:00
|
|
|
snprintf(fullpath, MAX_MQUEUE_PATH,
|
|
|
|
CONFIG_FS_MQUEUE_VFS_PATH "/%s", mq_name);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2014-09-29 23:48:52 +02:00
|
|
|
/* Get the inode for this message queue. */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
SETUP_SEARCH(&desc, fullpath, false);
|
2017-02-05 16:51:42 +01:00
|
|
|
|
2014-09-29 23:48:52 +02:00
|
|
|
sched_lock();
|
2017-02-05 16:51:42 +01:00
|
|
|
ret = inode_find(&desc);
|
|
|
|
if (ret < 0)
|
2014-09-29 23:48:52 +02:00
|
|
|
{
|
|
|
|
/* There is no inode that includes in this path */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
goto errout_with_search;
|
2014-09-29 23:48:52 +02:00
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2017-02-05 16:51:42 +01:00
|
|
|
/* Get the search results */
|
|
|
|
|
|
|
|
inode = desc.node;
|
|
|
|
DEBUGASSERT(inode != NULL);
|
|
|
|
|
2014-09-29 23:48:52 +02:00
|
|
|
/* Verify that what we found is, indeed, a message queue */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2014-09-29 23:48:52 +02:00
|
|
|
if (!INODE_IS_MQUEUE(inode))
|
|
|
|
{
|
2020-05-03 22:31:05 +02:00
|
|
|
ret = -ENXIO;
|
2014-09-29 23:48:52 +02:00
|
|
|
goto errout_with_inode;
|
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2014-09-29 23:48:52 +02:00
|
|
|
/* Refuse to unlink the inode if it has children. I.e., if it is
|
|
|
|
* functioning as a directory and the directory is not empty.
|
|
|
|
*/
|
2012-07-14 21:30:31 +02:00
|
|
|
|
2020-03-30 01:36:19 +02:00
|
|
|
ret = inode_semtake();
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_inode;
|
|
|
|
}
|
|
|
|
|
2014-09-29 23:48:52 +02:00
|
|
|
if (inode->i_child != NULL)
|
|
|
|
{
|
2020-05-03 22:31:05 +02:00
|
|
|
ret = -ENOTEMPTY;
|
2014-09-29 23:48:52 +02:00
|
|
|
goto errout_with_semaphore;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2014-09-29 23:48:52 +02:00
|
|
|
/* Remove the old inode from the tree. Because we hold a reference count
|
|
|
|
* on the inode, it will not be deleted now. This will set the
|
|
|
|
* FSNODEFLAG_DELETED bit in the inode flags.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = inode_remove(fullpath);
|
|
|
|
|
|
|
|
/* inode_remove() should always fail with -EBUSY because we hae a reference
|
2020-02-22 19:31:14 +01:00
|
|
|
* on the inode. -EBUSY means that the inode was, indeed, unlinked but
|
|
|
|
* thatis could not be freed because there are references.
|
2014-09-29 23:48:52 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(ret >= 0 || ret == -EBUSY);
|
|
|
|
|
|
|
|
/* Now we do not release the reference count in the normal way (by calling
|
2014-09-30 00:03:56 +02:00
|
|
|
* inode release. Rather, we call mq_inode_release(). mq_inode_release
|
|
|
|
* will decrement the reference count on the inode. But it will also free
|
|
|
|
* the message queue if that reference count decrements to zero. Since we
|
|
|
|
* hold one reference, that can only occur if the message queue is not
|
|
|
|
* in-use.
|
2014-09-29 23:48:52 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
inode_semgive();
|
2014-09-30 00:03:56 +02:00
|
|
|
mq_inode_release(inode);
|
2017-02-05 21:25:45 +01:00
|
|
|
RELEASE_SEARCH(&desc);
|
2014-09-30 00:13:07 +02:00
|
|
|
sched_unlock();
|
2014-09-29 23:48:52 +02:00
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout_with_semaphore:
|
|
|
|
inode_semgive();
|
2017-02-05 21:25:45 +01:00
|
|
|
|
2014-09-29 23:48:52 +02:00
|
|
|
errout_with_inode:
|
|
|
|
inode_release(inode);
|
2017-02-05 21:25:45 +01:00
|
|
|
|
|
|
|
errout_with_search:
|
|
|
|
RELEASE_SEARCH(&desc);
|
2014-09-30 00:13:07 +02:00
|
|
|
sched_unlock();
|
2020-05-03 22:31:05 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-12-24 13:53:17 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxmq_unlink
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is an internal OS interface. It is functionally equivalent to
|
|
|
|
* mq_unlink() except that:
|
|
|
|
*
|
|
|
|
* - It is not a cancellation point, and
|
|
|
|
* - It does not modify the errno value.
|
|
|
|
*
|
|
|
|
* See comments with mq_unlink() for a more complete description of the
|
|
|
|
* behavior of this function
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* mq_name - Name of the message queue
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* This is an internal OS interface and should not be used by applications.
|
|
|
|
* It follows the NuttX internal error return policy: Zero (OK) is
|
|
|
|
* returned on success. A negated errno value is returned on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int nxmq_unlink(FAR const char *mq_name)
|
|
|
|
{
|
|
|
|
return file_mq_unlink(mq_name);
|
|
|
|
}
|
|
|
|
|
2020-05-03 22:31:05 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: mq_unlink
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function removes the message queue named by "mq_name." If one
|
|
|
|
* or more tasks have the message queue open when mq_unlink() is called,
|
|
|
|
* removal of the message queue is postponed until all references to the
|
|
|
|
* message queue have been closed.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* mq_name - Name of the message queue
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int mq_unlink(FAR const char *mq_name)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = nxmq_unlink(mq_name);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
set_errno(-ret);
|
2020-12-22 15:02:48 +01:00
|
|
|
return ERROR;
|
2020-05-03 22:31:05 +02:00
|
|
|
}
|
|
|
|
|
2020-12-22 15:02:48 +01:00
|
|
|
return OK;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|