2014-09-28 22:35:17 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/semaphore/sem_open.c
|
|
|
|
*
|
2017-02-04 18:46:54 +01:00
|
|
|
* Copyright (C) 2014, 2017 Gregory Nutt. All rights reserved.
|
2014-09-28 22:35:17 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2017-02-04 18:46:54 +01:00
|
|
|
#include <stdbool.h>
|
2014-09-28 22:35:17 +02:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <semaphore.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/semaphore.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
|
2014-09-29 15:14:38 +02:00
|
|
|
#include "inode/inode.h"
|
2014-09-28 23:58:56 +02:00
|
|
|
#include "semaphore/semaphore.h"
|
2014-09-28 22:35:17 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_FS_NAMED_SEMAPHORES
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: sem_open
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function establishes a connection between named semaphores and a
|
|
|
|
* task. Following a call to sem_open() with the semaphore name, the task
|
|
|
|
* may reference the semaphore associated with name using the address
|
|
|
|
* returned by this call. The semaphore may be used in subsequent calls
|
2017-10-05 15:59:06 +02:00
|
|
|
* to sem_wait(), sem_trywait(), and sem_post(). The semaphore remains
|
2014-09-28 22:35:17 +02:00
|
|
|
* usable until the semaphore is closed by a successful call to sem_close().
|
|
|
|
*
|
|
|
|
* If a task makes multiple calls to sem_open() with the same name, then
|
|
|
|
* the same semaphore address is returned (provided there have been no
|
|
|
|
* calls to sem_unlink()).
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2014-09-28 22:35:17 +02:00
|
|
|
* name - Semaphore name
|
|
|
|
* oflags - Semaphore creation options. This may either or both of the
|
|
|
|
* following bit settings.
|
|
|
|
* oflags = 0: Connect to the semaphore only if it already exists.
|
|
|
|
* oflags = O_CREAT: Connect to the semaphore if it exists, otherwise
|
|
|
|
* create the semaphore.
|
|
|
|
* oflags = O_CREAT|O_EXCL: Create a new semaphore
|
|
|
|
* unless one of this name already exists.
|
|
|
|
* Optional parameters. When the O_CREAT flag is specified, two optional
|
|
|
|
* parameters are expected:
|
|
|
|
* 1. mode_t mode (ignored), and
|
|
|
|
* 2. unsigned int value. This initial value of the semaphore. Valid
|
|
|
|
* initial values of the semaphore must be less than or equal to
|
|
|
|
* SEM_VALUE_MAX.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2017-09-08 18:14:51 +02:00
|
|
|
* A pointer to sem_t or SEM_FAILED if unsuccessful.
|
2014-09-28 22:35:17 +02:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR sem_t *sem_open (FAR const char *name, int oflags, ...)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct nsem_inode_s *nsem;
|
2015-10-11 19:39:29 +02:00
|
|
|
FAR sem_t *sem = (FAR sem_t *)ERROR;
|
2017-02-05 16:51:42 +01:00
|
|
|
struct inode_search_s desc;
|
2014-09-28 22:35:17 +02:00
|
|
|
char fullpath[MAX_SEMPATH];
|
2017-02-05 16:51:42 +01:00
|
|
|
mode_t mode;
|
2014-09-28 22:35:17 +02:00
|
|
|
unsigned value;
|
|
|
|
int errcode;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Make sure that a non-NULL name is supplied */
|
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
DEBUGASSERT(name != NULL);
|
2017-02-05 21:57:38 +01:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
/* The POSIX specification requires that the "check for the existence
|
2017-02-05 21:57:38 +01:00
|
|
|
* of a semaphore and the creation of the semaphore if it does not
|
2017-02-05 21:25:45 +01:00
|
|
|
* exist shall be atomic with respect to other processes executing
|
|
|
|
* sem_open()..." A simple sched_lock() should be sufficient to meet
|
|
|
|
* this requirement.
|
|
|
|
*/
|
2014-09-28 22:35:17 +02:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
sched_lock();
|
2014-09-28 22:35:17 +02:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
/* Get the full path to the semaphore */
|
2017-02-05 16:51:42 +01:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
snprintf(fullpath, MAX_SEMPATH, CONFIG_FS_NAMED_SEMPATH "/%s", name);
|
2017-02-05 16:51:42 +01:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
/* Get the inode for this semaphore. This should succeed if the
|
|
|
|
* semaphore has already been created. In this case, inode_find()
|
|
|
|
* will have incremented the reference count on the inode.
|
|
|
|
*/
|
2017-02-05 16:51:42 +01:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
SETUP_SEARCH(&desc, fullpath, false);
|
2014-09-28 22:35:17 +02:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
ret = inode_find(&desc);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
/* Something exists at this path. Get the search results */
|
2014-09-28 22:35:17 +02:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
inode = desc.node;
|
|
|
|
DEBUGASSERT(inode != NULL);
|
2014-09-28 22:35:17 +02:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
/* Verify that the inode is a semaphore */
|
2014-09-28 22:35:17 +02:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
if (!INODE_IS_NAMEDSEM(inode))
|
|
|
|
{
|
|
|
|
errcode = ENXIO;
|
|
|
|
goto errout_with_inode;
|
|
|
|
}
|
2014-09-28 22:35:17 +02:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
/* It exists and is a semaphore. Check if the caller wanted to
|
|
|
|
* create a new semaphore with this name.
|
|
|
|
*/
|
2014-09-28 22:35:17 +02:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
|
2014-09-28 22:35:17 +02:00
|
|
|
{
|
2017-02-05 21:25:45 +01:00
|
|
|
errcode = EEXIST;
|
|
|
|
goto errout_with_inode;
|
|
|
|
}
|
2014-09-28 22:35:17 +02:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
/* Return a reference to the semaphore, retaining the reference
|
|
|
|
* count on the inode.
|
|
|
|
*/
|
2014-09-28 22:35:17 +02:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
sem = &inode->u.i_nsem->ns_sem;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
va_list ap;
|
2014-09-28 22:35:17 +02:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
/* The semaphore does not exists. Were we asked to create it? */
|
2014-09-28 22:35:17 +02:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
if ((oflags & O_CREAT) == 0)
|
|
|
|
{
|
|
|
|
/* The semaphore does not exist and O_CREAT is not set */
|
2014-09-28 22:35:17 +02:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
errcode = ENOENT;
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
2014-09-28 22:35:17 +02:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
/* Create the semaphore. First we have to extract the additional
|
|
|
|
* parameters from the variable argument list.
|
|
|
|
* REVISIT: Mode parameter is not currently used.
|
|
|
|
*/
|
2014-09-28 22:35:17 +02:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
va_start(ap, oflags);
|
|
|
|
mode = va_arg(ap, mode_t);
|
|
|
|
value = va_arg(ap, unsigned);
|
|
|
|
va_end(ap);
|
2014-09-28 22:35:17 +02:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
UNUSED(mode);
|
2014-09-28 22:35:17 +02:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
/* Check the semaphore value */
|
2014-09-28 22:35:17 +02:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
if (value > SEM_VALUE_MAX)
|
|
|
|
{
|
|
|
|
errcode = EINVAL;
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
2014-09-28 22:35:17 +02:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
/* Create an inode in the pseudo-filesystem at this path. The new
|
|
|
|
* inode will be created with a reference count of zero.
|
|
|
|
*/
|
2014-09-28 22:35:17 +02:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
inode_semtake();
|
|
|
|
ret = inode_reserve(fullpath, &inode);
|
|
|
|
inode_semgive();
|
2014-09-28 22:35:17 +02:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
errcode = -ret;
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
2014-09-28 22:35:17 +02:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
/* Allocate the semaphore structure (using the appropriate allocator
|
|
|
|
* for the group)
|
|
|
|
*/
|
2014-09-28 22:35:17 +02:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
nsem = group_malloc(NULL, sizeof(struct nsem_inode_s));
|
|
|
|
if (!nsem)
|
|
|
|
{
|
|
|
|
errcode = ENOMEM;
|
|
|
|
goto errout_with_inode;
|
|
|
|
}
|
2014-09-28 23:58:56 +02:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
/* Link to the inode */
|
2014-09-28 23:58:56 +02:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
inode->u.i_nsem = nsem;
|
|
|
|
nsem->ns_inode = inode;
|
2014-09-28 23:58:56 +02:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
/* Initialize the inode */
|
2014-09-28 23:58:56 +02:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
INODE_SET_NAMEDSEM(inode);
|
|
|
|
inode->i_crefs = 1;
|
2014-09-28 23:58:56 +02:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
/* Initialize the semaphore */
|
2014-09-28 23:58:56 +02:00
|
|
|
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&nsem->ns_sem, 0, value);
|
2014-09-30 00:13:07 +02:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
/* Return a reference to the semaphore */
|
|
|
|
|
|
|
|
sem = &nsem->ns_sem;
|
2014-09-28 22:35:17 +02:00
|
|
|
}
|
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
RELEASE_SEARCH(&desc);
|
|
|
|
sched_unlock();
|
2014-09-28 22:35:17 +02:00
|
|
|
return sem;
|
|
|
|
|
2015-10-04 23:04:00 +02:00
|
|
|
errout_with_inode:
|
2014-09-28 22:35:17 +02:00
|
|
|
inode_release(inode);
|
2017-02-05 21:25:45 +01:00
|
|
|
|
2015-10-04 23:04:00 +02:00
|
|
|
errout_with_lock:
|
2017-02-05 21:25:45 +01:00
|
|
|
RELEASE_SEARCH(&desc);
|
2014-09-28 22:35:17 +02:00
|
|
|
set_errno(errcode);
|
2014-09-30 00:13:07 +02:00
|
|
|
sched_unlock();
|
2017-09-08 18:14:51 +02:00
|
|
|
return SEM_FAILED;
|
2014-09-28 22:35:17 +02:00
|
|
|
}
|
|
|
|
|
2016-04-12 02:16:04 +02:00
|
|
|
#endif /* CONFIG_FS_NAMED_SEMAPHORES */
|