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
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2023-11-17 13:06:48 +01:00
|
|
|
* Name: nxsem_open
|
2014-09-28 22:35:17 +02:00
|
|
|
*
|
|
|
|
* 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:
|
2024-06-05 04:25:08 +02:00
|
|
|
* sem - Location to return the semaphore reference.
|
|
|
|
* name - Semaphore name.
|
2014-09-28 22:35:17 +02:00
|
|
|
* 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:
|
2024-06-05 04:25:08 +02:00
|
|
|
* 0 (OK), or negated errno if unsuccessful.
|
2014-09-28 22:35:17 +02:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-06-05 04:25:08 +02:00
|
|
|
int nxsem_open(FAR sem_t **sem, FAR const char *name, int oflags, ...)
|
2014-09-28 22:35:17 +02:00
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct nsem_inode_s *nsem;
|
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 ret;
|
|
|
|
|
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;
|
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))
|
|
|
|
{
|
2023-11-17 13:06:48 +01:00
|
|
|
ret = -ENXIO;
|
2017-02-05 21:25:45 +01:00
|
|
|
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
|
|
|
{
|
2023-11-17 13:06:48 +01:00
|
|
|
ret = -EEXIST;
|
2017-02-05 21:25:45 +01:00
|
|
|
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
|
|
|
|
2024-06-05 04:25:08 +02:00
|
|
|
*sem = &inode->u.i_nsem->ns_sem;
|
2017-02-05 21:25:45 +01:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2023-11-17 13:06:48 +01:00
|
|
|
ret = -ENOENT;
|
2017-02-05 21:25:45 +01:00
|
|
|
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)
|
|
|
|
{
|
2023-11-17 13:06:48 +01:00
|
|
|
ret = -EINVAL;
|
2017-02-05 21:25:45 +01:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2023-11-17 13:06:48 +01:00
|
|
|
ret = -ENOMEM;
|
2017-02-05 21:25:45 +01:00
|
|
|
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 */
|
|
|
|
|
2024-06-05 04:25:08 +02:00
|
|
|
*sem = &nsem->ns_sem;
|
2014-09-28 22:35:17 +02:00
|
|
|
}
|
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
RELEASE_SEARCH(&desc);
|
2024-06-05 04:25:08 +02:00
|
|
|
return OK;
|
2014-09-28 22:35:17 +02:00
|
|
|
|
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);
|
2024-06-05 04:25:08 +02:00
|
|
|
return ret;
|
2014-09-28 22:35:17 +02:00
|
|
|
}
|
|
|
|
|
2016-04-12 02:16:04 +02:00
|
|
|
#endif /* CONFIG_FS_NAMED_SEMAPHORES */
|