Blame nptl/sem_open.c

Packit 6c4009
/* Copyright (C) 2002-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <pthread.h>
Packit 6c4009
#include <search.h>
Packit 6c4009
#include <semaphore.h>
Packit 6c4009
#include <stdarg.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/mman.h>
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
#include "semaphoreP.h"
Packit 6c4009
#include <shm-directory.h>
Packit 6c4009
#include <futex-internal.h>
Packit 6c4009
#include <libc-lock.h>
Packit 6c4009
Packit 6c4009
/* Comparison function for search of existing mapping.  */
Packit 6c4009
int
Packit 6c4009
attribute_hidden
Packit 6c4009
__sem_search (const void *a, const void *b)
Packit 6c4009
{
Packit 6c4009
  const struct inuse_sem *as = (const struct inuse_sem *) a;
Packit 6c4009
  const struct inuse_sem *bs = (const struct inuse_sem *) b;
Packit 6c4009
Packit 6c4009
  if (as->ino != bs->ino)
Packit 6c4009
    /* Cannot return the difference the type is larger than int.  */
Packit 6c4009
    return as->ino < bs->ino ? -1 : (as->ino == bs->ino ? 0 : 1);
Packit 6c4009
Packit 6c4009
  if (as->dev != bs->dev)
Packit 6c4009
    /* Cannot return the difference the type is larger than int.  */
Packit 6c4009
    return as->dev < bs->dev ? -1 : (as->dev == bs->dev ? 0 : 1);
Packit 6c4009
Packit 6c4009
  return strcmp (as->name, bs->name);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* The search tree for existing mappings.  */
Packit 6c4009
void *__sem_mappings attribute_hidden;
Packit 6c4009
Packit 6c4009
/* Lock to protect the search tree.  */
Packit 6c4009
int __sem_mappings_lock attribute_hidden = LLL_LOCK_INITIALIZER;
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Search for existing mapping and if possible add the one provided.  */
Packit 6c4009
static sem_t *
Packit 6c4009
check_add_mapping (const char *name, size_t namelen, int fd, sem_t *existing)
Packit 6c4009
{
Packit 6c4009
  sem_t *result = SEM_FAILED;
Packit 6c4009
Packit 6c4009
  /* Get the information about the file.  */
Packit 6c4009
  struct stat64 st;
Packit 6c4009
  if (__fxstat64 (_STAT_VER, fd, &st) == 0)
Packit 6c4009
    {
Packit 6c4009
      /* Get the lock.  */
Packit 6c4009
      lll_lock (__sem_mappings_lock, LLL_PRIVATE);
Packit 6c4009
Packit 6c4009
      /* Search for an existing mapping given the information we have.  */
Packit 6c4009
      struct inuse_sem *fake;
Packit 6c4009
      fake = (struct inuse_sem *) alloca (sizeof (*fake) + namelen);
Packit 6c4009
      memcpy (fake->name, name, namelen);
Packit 6c4009
      fake->dev = st.st_dev;
Packit 6c4009
      fake->ino = st.st_ino;
Packit 6c4009
Packit 6c4009
      struct inuse_sem **foundp = __tfind (fake, &__sem_mappings,
Packit 6c4009
					   __sem_search);
Packit 6c4009
      if (foundp != NULL)
Packit 6c4009
	{
Packit 6c4009
	  /* There is already a mapping.  Use it.  */
Packit 6c4009
	  result = (*foundp)->sem;
Packit 6c4009
	  ++(*foundp)->refcnt;
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  /* We haven't found a mapping.  Install ione.  */
Packit 6c4009
	  struct inuse_sem *newp;
Packit 6c4009
Packit 6c4009
	  newp = (struct inuse_sem *) malloc (sizeof (*newp) + namelen);
Packit 6c4009
	  if (newp != NULL)
Packit 6c4009
	    {
Packit 6c4009
	      /* If the caller hasn't provided any map it now.  */
Packit 6c4009
	      if (existing == SEM_FAILED)
Packit 6c4009
		existing = (sem_t *) mmap (NULL, sizeof (sem_t),
Packit 6c4009
					   PROT_READ | PROT_WRITE, MAP_SHARED,
Packit 6c4009
					   fd, 0);
Packit 6c4009
Packit 6c4009
	      newp->dev = st.st_dev;
Packit 6c4009
	      newp->ino = st.st_ino;
Packit 6c4009
	      newp->refcnt = 1;
Packit 6c4009
	      newp->sem = existing;
Packit 6c4009
	      memcpy (newp->name, name, namelen);
Packit 6c4009
Packit 6c4009
	      /* Insert the new value.  */
Packit 6c4009
	      if (existing != MAP_FAILED
Packit 6c4009
		  && __tsearch (newp, &__sem_mappings, __sem_search) != NULL)
Packit 6c4009
		/* Successful.  */
Packit 6c4009
		result = existing;
Packit 6c4009
	      else
Packit 6c4009
		/* Something went wrong while inserting the new
Packit 6c4009
		   value.  We fail completely.  */
Packit 6c4009
		free (newp);
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* Release the lock.  */
Packit 6c4009
      lll_unlock (__sem_mappings_lock, LLL_PRIVATE);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (result != existing && existing != SEM_FAILED && existing != MAP_FAILED)
Packit 6c4009
    {
Packit 6c4009
      /* Do not disturb errno.  */
Packit 6c4009
      int save = errno;
Packit 6c4009
      munmap (existing, sizeof (sem_t));
Packit 6c4009
      errno = save;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
sem_t *
Packit 6c4009
sem_open (const char *name, int oflag, ...)
Packit 6c4009
{
Packit 6c4009
  int fd;
Packit 6c4009
  sem_t *result;
Packit 6c4009
Packit 6c4009
  /* Check that shared futexes are supported.  */
Packit 6c4009
  int err = futex_supports_pshared (PTHREAD_PROCESS_SHARED);
Packit 6c4009
  if (err != 0)
Packit 6c4009
    {
Packit 6c4009
      __set_errno (err);
Packit 6c4009
      return SEM_FAILED;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Create the name of the final file in local variable SHM_NAME.  */
Packit 6c4009
  SHM_GET_NAME (EINVAL, SEM_FAILED, SEM_SHM_PREFIX);
Packit 6c4009
Packit 6c4009
  /* Disable asynchronous cancellation.  */
Packit 6c4009
#ifdef __libc_ptf_call
Packit 6c4009
  int state;
Packit 6c4009
  __libc_ptf_call (__pthread_setcancelstate,
Packit 6c4009
                   (PTHREAD_CANCEL_DISABLE, &state), 0);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  /* If the semaphore object has to exist simply open it.  */
Packit 6c4009
  if ((oflag & O_CREAT) == 0 || (oflag & O_EXCL) == 0)
Packit 6c4009
    {
Packit 6c4009
    try_again:
Packit 6c4009
      fd = __libc_open (shm_name,
Packit 6c4009
			(oflag & ~(O_CREAT|O_ACCMODE)) | O_NOFOLLOW | O_RDWR);
Packit 6c4009
Packit 6c4009
      if (fd == -1)
Packit 6c4009
	{
Packit 6c4009
	  /* If we are supposed to create the file try this next.  */
Packit 6c4009
	  if ((oflag & O_CREAT) != 0 && errno == ENOENT)
Packit 6c4009
	    goto try_create;
Packit 6c4009
Packit 6c4009
	  /* Return.  errno is already set.  */
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	/* Check whether we already have this semaphore mapped and
Packit 6c4009
	   create one if necessary.  */
Packit 6c4009
	result = check_add_mapping (name, namelen, fd, SEM_FAILED);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      /* We have to open a temporary file first since it must have the
Packit 6c4009
	 correct form before we can start using it.  */
Packit 6c4009
      char *tmpfname;
Packit 6c4009
      mode_t mode;
Packit 6c4009
      unsigned int value;
Packit 6c4009
      va_list ap;
Packit 6c4009
Packit 6c4009
    try_create:
Packit 6c4009
      va_start (ap, oflag);
Packit 6c4009
Packit 6c4009
      mode = va_arg (ap, mode_t);
Packit 6c4009
      value = va_arg (ap, unsigned int);
Packit 6c4009
Packit 6c4009
      va_end (ap);
Packit 6c4009
Packit 6c4009
      if (value > SEM_VALUE_MAX)
Packit 6c4009
	{
Packit 6c4009
	  __set_errno (EINVAL);
Packit 6c4009
	  result = SEM_FAILED;
Packit 6c4009
	  goto out;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* Create the initial file content.  */
Packit 6c4009
      union
Packit 6c4009
      {
Packit 6c4009
	sem_t initsem;
Packit 6c4009
	struct new_sem newsem;
Packit 6c4009
      } sem;
Packit 6c4009
Packit 6c4009
#if __HAVE_64B_ATOMICS
Packit 6c4009
      sem.newsem.data = value;
Packit 6c4009
#else
Packit 6c4009
      sem.newsem.value = value << SEM_VALUE_SHIFT;
Packit 6c4009
      sem.newsem.nwaiters = 0;
Packit 6c4009
#endif
Packit 6c4009
      /* pad is used as a mutex on pre-v9 sparc and ignored otherwise.  */
Packit 6c4009
      sem.newsem.pad = 0;
Packit 6c4009
Packit 6c4009
      /* This always is a shared semaphore.  */
Packit 6c4009
      sem.newsem.private = FUTEX_SHARED;
Packit 6c4009
Packit 6c4009
      /* Initialize the remaining bytes as well.  */
Packit 6c4009
      memset ((char *) &sem.initsem + sizeof (struct new_sem), '\0',
Packit 6c4009
	      sizeof (sem_t) - sizeof (struct new_sem));
Packit 6c4009
Packit 6c4009
      tmpfname = __alloca (shm_dirlen + sizeof SEM_SHM_PREFIX + 6);
Packit 6c4009
      char *xxxxxx = __mempcpy (tmpfname, shm_dir, shm_dirlen);
Packit 6c4009
Packit 6c4009
      int retries = 0;
Packit 6c4009
#define NRETRIES 50
Packit 6c4009
      while (1)
Packit 6c4009
	{
Packit 6c4009
	  /* Add the suffix for mktemp.  */
Packit 6c4009
	  strcpy (xxxxxx, "XXXXXX");
Packit 6c4009
Packit 6c4009
	  /* We really want to use mktemp here.  We cannot use mkstemp
Packit 6c4009
	     since the file must be opened with a specific mode.  The
Packit 6c4009
	     mode cannot later be set since then we cannot apply the
Packit 6c4009
	     file create mask.  */
Packit 6c4009
	  if (__mktemp (tmpfname) == NULL)
Packit 6c4009
	    {
Packit 6c4009
	      result = SEM_FAILED;
Packit 6c4009
	      goto out;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  /* Open the file.  Make sure we do not overwrite anything.  */
Packit 6c4009
	  fd = __libc_open (tmpfname, O_RDWR | O_CREAT | O_EXCL, mode);
Packit 6c4009
	  if (fd == -1)
Packit 6c4009
	    {
Packit 6c4009
	      if (errno == EEXIST)
Packit 6c4009
		{
Packit 6c4009
		  if (++retries < NRETRIES)
Packit 6c4009
		    continue;
Packit 6c4009
Packit 6c4009
		  __set_errno (EAGAIN);
Packit 6c4009
		}
Packit 6c4009
Packit 6c4009
	      result = SEM_FAILED;
Packit 6c4009
	      goto out;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  /* We got a file.  */
Packit 6c4009
	  break;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (TEMP_FAILURE_RETRY (__libc_write (fd, &sem.initsem, sizeof (sem_t)))
Packit 6c4009
	  == sizeof (sem_t)
Packit 6c4009
	  /* Map the sem_t structure from the file.  */
Packit 6c4009
	  && (result = (sem_t *) mmap (NULL, sizeof (sem_t),
Packit 6c4009
				       PROT_READ | PROT_WRITE, MAP_SHARED,
Packit 6c4009
				       fd, 0)) != MAP_FAILED)
Packit 6c4009
	{
Packit 6c4009
	  /* Create the file.  Don't overwrite an existing file.  */
Packit 6c4009
	  if (link (tmpfname, shm_name) != 0)
Packit 6c4009
	    {
Packit 6c4009
	      /* Undo the mapping.  */
Packit 6c4009
	      (void) munmap (result, sizeof (sem_t));
Packit 6c4009
Packit 6c4009
	      /* Reinitialize 'result'.  */
Packit 6c4009
	      result = SEM_FAILED;
Packit 6c4009
Packit 6c4009
	      /* This failed.  If O_EXCL is not set and the problem was
Packit 6c4009
		 that the file exists, try again.  */
Packit 6c4009
	      if ((oflag & O_EXCL) == 0 && errno == EEXIST)
Packit 6c4009
		{
Packit 6c4009
		  /* Remove the file.  */
Packit 6c4009
		  (void) unlink (tmpfname);
Packit 6c4009
Packit 6c4009
		  /* Close the file.  */
Packit 6c4009
		  (void) __libc_close (fd);
Packit 6c4009
Packit 6c4009
		  goto try_again;
Packit 6c4009
		}
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    /* Insert the mapping into the search tree.  This also
Packit 6c4009
	       determines whether another thread sneaked by and already
Packit 6c4009
	       added such a mapping despite the fact that we created it.  */
Packit 6c4009
	    result = check_add_mapping (name, namelen, fd, result);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* Now remove the temporary name.  This should never fail.  If
Packit 6c4009
	 it fails we leak a file name.  Better fix the kernel.  */
Packit 6c4009
      (void) unlink (tmpfname);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Map the mmap error to the error we need.  */
Packit 6c4009
  if (MAP_FAILED != (void *) SEM_FAILED && result == MAP_FAILED)
Packit 6c4009
    result = SEM_FAILED;
Packit 6c4009
Packit 6c4009
  /* We don't need the file descriptor anymore.  */
Packit 6c4009
  if (fd != -1)
Packit 6c4009
    {
Packit 6c4009
      /* Do not disturb errno.  */
Packit 6c4009
      int save = errno;
Packit 6c4009
      __libc_close (fd);
Packit 6c4009
      errno = save;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
out:
Packit 6c4009
#ifdef __libc_ptf_call
Packit 6c4009
  __libc_ptf_call (__pthread_setcancelstate, (state, NULL), 0);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}