2014-09-28 22:35:17 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/semaphore/sem_open.c
|
|
|
|
*
|
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
|
2014-09-28 22:35:17 +02:00
|
|
|
*
|
2020-03-30 01:36:19 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-09-28 22:35:17 +02: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.
|
2014-09-28 22:35:17 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2021-07-11 11:44:25 +02:00
|
|
|
#include <sys/stat.h>
|
2014-09-28 22:35:17 +02:00
|
|
|
#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 <string.h>
|
|
|
|
#include <sched.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2014-09-28 22:35:17 +02:00
|
|
|
#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
|
2020-03-30 01:36:19 +02:00
|
|
|
* usable until the semaphore is closed by a successful call to
|
|
|
|
* sem_close().
|
2014-09-28 22:35:17 +02:00
|
|
|
*
|
|
|
|
* 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:
|
2021-07-09 07:43:26 +02:00
|
|
|
* 1. mode_t mode, and
|
2014-09-28 22:35:17 +02:00
|
|
|
* 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:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-07-11 11:44:25 +02:00
|
|
|
FAR sem_t *sem_open(FAR const char *name, int oflags, ...)
|
2014-09-28 22:35:17 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2022-02-05 17:33:29 +01:00
|
|
|
snprintf(fullpath, MAX_SEMPATH,
|
|
|
|
CONFIG_FS_NAMED_SEMAPHORES_VFS_PATH "/%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);
|
2021-07-11 11:44:25 +02:00
|
|
|
mode = va_arg(ap, mode_t) & ~getumask();
|
2017-02-05 21:25:45 +01:00
|
|
|
value = va_arg(ap, unsigned);
|
|
|
|
va_end(ap);
|
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
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = inode_lock();
|
2020-03-30 01:36:19 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
errcode = -ret;
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
|
|
|
|
2021-07-09 07:43:26 +02:00
|
|
|
ret = inode_reserve(fullpath, mode, &inode);
|
2022-09-06 08:18:45 +02:00
|
|
|
inode_unlock();
|
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 */
|