2014-09-22 22:53:56 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* mm/shm/shmdt.c
|
|
|
|
*
|
2020-05-09 17:05:29 +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-22 22:53:56 +02:00
|
|
|
*
|
2020-05-09 17:05:29 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-09-22 22:53:56 +02:00
|
|
|
*
|
2020-05-09 17:05:29 +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-22 22:53:56 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-09-24 00:07:12 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/shm.h>
|
2021-06-08 20:00:55 +02:00
|
|
|
#include <assert.h>
|
2022-11-16 13:30:18 +01:00
|
|
|
#include <debug.h>
|
2014-09-24 00:07:12 +02:00
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <nuttx/arch.h>
|
|
|
|
#include <nuttx/sched.h>
|
2014-09-24 15:05:02 +02:00
|
|
|
#include <nuttx/mm/shm.h>
|
2022-11-16 13:30:18 +01:00
|
|
|
#include <nuttx/pgalloc.h>
|
2022-04-12 14:34:50 +02:00
|
|
|
#include <nuttx/mm/map.h>
|
2014-09-24 00:07:12 +02:00
|
|
|
|
|
|
|
#include "shm/shm.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_MM_SHM
|
|
|
|
|
2014-09-22 22:53:56 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2022-04-12 14:34:50 +02:00
|
|
|
* Name: shmdt_priv
|
2014-09-22 22:53:56 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2022-04-12 14:34:50 +02:00
|
|
|
* The shmdt_priv() function detaches the shared memory segment located at
|
|
|
|
* the address specified by shmaddr from the address space of the calling
|
2014-09-22 22:53:56 +02:00
|
|
|
* process.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* shmid - Shared memory identifier
|
2022-04-12 14:34:50 +02:00
|
|
|
* group - Current task_group, NULL during process exit
|
2014-09-22 22:53:56 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2022-04-12 14:34:50 +02:00
|
|
|
* Upon successful completion, shmdt_priv() will decrement the value of
|
2014-09-22 22:53:56 +02:00
|
|
|
* shm_nattch in the data structure associated with the shared memory ID
|
|
|
|
* of the attached shared memory segment and return 0.
|
|
|
|
*
|
2022-04-12 14:34:50 +02:00
|
|
|
* Otherwise, the shared memory segment will not be detached, shmdt_priv()
|
2015-10-04 23:04:00 +02:00
|
|
|
* will return -1, and errno will be set to indicate the error.
|
2014-09-22 22:53:56 +02:00
|
|
|
*
|
|
|
|
* - EINVAL
|
|
|
|
* The value of shmaddr is not the data segment start address of a
|
|
|
|
* shared memory segment.
|
|
|
|
*
|
2014-09-24 00:07:12 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2022-04-12 14:34:50 +02:00
|
|
|
int shmdt_priv(FAR struct task_group_s *group, FAR const void *shmaddr,
|
|
|
|
int shmid)
|
2014-09-24 00:07:12 +02:00
|
|
|
{
|
|
|
|
FAR struct shm_region_s *region;
|
2022-04-12 14:34:50 +02:00
|
|
|
pid_t pid;
|
2014-09-24 00:07:12 +02:00
|
|
|
unsigned int npages;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get the region associated with the shmid */
|
|
|
|
|
|
|
|
region = &g_shminfo.si_region[shmid];
|
|
|
|
DEBUGASSERT((region->sr_flags & SRFLAG_INUSE) != 0);
|
|
|
|
|
|
|
|
/* Get exclusive access to the region data structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(®ion->sr_lock);
|
2014-09-24 00:07:12 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
shmerr("ERROR: nxsem_wait failed: %d\n", ret);
|
|
|
|
goto errout_with_errno;
|
2014-09-24 00:07:12 +02:00
|
|
|
}
|
|
|
|
|
2022-04-12 14:34:50 +02:00
|
|
|
if (!group)
|
|
|
|
{
|
|
|
|
pid = 0;
|
|
|
|
goto on_process_exit;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pid = group->tg_pid;
|
|
|
|
}
|
|
|
|
|
2014-09-24 00:07:12 +02:00
|
|
|
/* Free the virtual address space */
|
|
|
|
|
2022-11-17 11:03:22 +01:00
|
|
|
shm_free(group, (FAR void *)shmaddr, region->sr_ds.shm_segsz);
|
2014-09-24 00:07:12 +02:00
|
|
|
|
|
|
|
/* Convert the region size to pages */
|
|
|
|
|
2019-01-26 17:11:06 +01:00
|
|
|
npages = MM_NPAGES(region->sr_ds.shm_segsz);
|
2014-09-24 00:07:12 +02:00
|
|
|
|
|
|
|
/* Detach, i.e, unmap, on shared memory region from a user virtual
|
|
|
|
* address.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = up_shmdt((uintptr_t)shmaddr, npages);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 17:46:23 +02:00
|
|
|
shmerr("ERROR: up_shmdt() failed\n");
|
2014-09-24 00:07:12 +02:00
|
|
|
}
|
|
|
|
|
2022-04-12 14:34:50 +02:00
|
|
|
on_process_exit:
|
2014-09-24 00:07:12 +02:00
|
|
|
|
|
|
|
/* Decrement the count of processes attached to this region.
|
|
|
|
* If the count decrements to zero and there is a pending unlink,
|
|
|
|
* then destroy the shared memory region now and stop any further
|
|
|
|
* operations on it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(region->sr_ds.shm_nattch > 0);
|
|
|
|
if (region->sr_ds.shm_nattch <= 1)
|
|
|
|
{
|
|
|
|
region->sr_ds.shm_nattch = 0;
|
|
|
|
if ((region->sr_flags & SRFLAG_UNLINKED) != 0)
|
|
|
|
{
|
|
|
|
shm_destroy(shmid);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Just decrement the number of processes attached to the shared
|
|
|
|
* memory region.
|
|
|
|
*/
|
|
|
|
|
|
|
|
region->sr_ds.shm_nattch--;
|
|
|
|
}
|
|
|
|
|
2017-05-11 21:35:56 +02:00
|
|
|
/* Save the process ID of the last operation */
|
2014-09-24 00:07:12 +02:00
|
|
|
|
2022-04-12 14:34:50 +02:00
|
|
|
region->sr_ds.shm_lpid = pid;
|
2014-09-24 00:07:12 +02:00
|
|
|
|
|
|
|
/* Save the time of the last shmdt() */
|
|
|
|
|
|
|
|
region->sr_ds.shm_dtime = time(NULL);
|
|
|
|
|
|
|
|
/* Release our lock on the entry */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(®ion->sr_lock);
|
2014-09-24 00:07:12 +02:00
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout_with_errno:
|
|
|
|
set_errno(-ret);
|
|
|
|
return ERROR;
|
|
|
|
}
|
2014-09-22 22:53:56 +02:00
|
|
|
|
2022-04-12 14:34:50 +02:00
|
|
|
int shmdt(FAR const void *shmaddr)
|
|
|
|
{
|
|
|
|
FAR struct tcb_s *tcb;
|
|
|
|
FAR struct mm_map_entry_s *entry;
|
|
|
|
FAR struct task_group_s *group;
|
|
|
|
int shmid;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get the TCB and group containing our virtual memory allocator */
|
|
|
|
|
|
|
|
tcb = nxsched_self();
|
|
|
|
DEBUGASSERT(tcb && tcb->group);
|
|
|
|
group = tcb->group;
|
|
|
|
|
|
|
|
/* Get exclusive access to process' mm_map */
|
|
|
|
|
|
|
|
ret = mm_map_lock();
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* Perform the reverse lookup to get the shmid corresponding to this
|
|
|
|
* shmaddr. The mapping is matched with just shmaddr == map->vaddr.
|
|
|
|
*/
|
|
|
|
|
|
|
|
entry = mm_map_find(shmaddr, 1);
|
|
|
|
if (!entry || entry->vaddr != shmaddr)
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
shmerr("ERROR: No region matching this virtual address: %p\n",
|
|
|
|
shmaddr);
|
|
|
|
|
|
|
|
mm_map_unlock();
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
shmid = entry->priv.i;
|
|
|
|
|
|
|
|
/* Indicate that there is no longer any mapping for this region. */
|
|
|
|
|
|
|
|
if (mm_map_remove(get_group_mm(group), entry) < 0)
|
|
|
|
{
|
|
|
|
shmerr("ERROR: mm_map_remove() failed\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
mm_map_unlock();
|
|
|
|
|
|
|
|
ret = shmdt_priv(tcb->group, shmaddr, shmid);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-09-22 22:53:56 +02:00
|
|
|
#endif /* CONFIG_MM_SHM */
|