]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - nptl/sem_open.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / nptl / sem_open.c
index 911b1f30a9287cfb16130aecf1056ba85a3fd11c..85802c809dfbbd19783253da11812b64133f7eaf 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2002-2016 Free Software Foundation, Inc.
+/* Copyright (C) 2002-2019 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
 
@@ -14,7 +14,7 @@
 
    You should have received a copy of the GNU Lesser General Public
    License along with the GNU C Library; if not, see
-   <http://www.gnu.org/licenses/>.  */
+   <https://www.gnu.org/licenses/>.  */
 
 #include <errno.h>
 #include <fcntl.h>
@@ -31,7 +31,7 @@
 #include "semaphoreP.h"
 #include <shm-directory.h>
 #include <futex-internal.h>
-
+#include <libc-lock.h>
 
 /* Comparison function for search of existing mapping.  */
 int
@@ -153,6 +153,13 @@ sem_open (const char *name, int oflag, ...)
   /* Create the name of the final file in local variable SHM_NAME.  */
   SHM_GET_NAME (EINVAL, SEM_FAILED, SEM_SHM_PREFIX);
 
+  /* Disable asynchronous cancellation.  */
+#ifdef __libc_ptf_call
+  int state;
+  __libc_ptf_call (__pthread_setcancelstate,
+                   (PTHREAD_CANCEL_DISABLE, &state), 0);
+#endif
+
   /* If the semaphore object has to exist simply open it.  */
   if ((oflag & O_CREAT) == 0 || (oflag & O_EXCL) == 0)
     {
@@ -193,7 +200,8 @@ sem_open (const char *name, int oflag, ...)
       if (value > SEM_VALUE_MAX)
        {
          __set_errno (EINVAL);
-         return SEM_FAILED;
+         result = SEM_FAILED;
+         goto out;
        }
 
       /* Create the initial file content.  */
@@ -209,6 +217,9 @@ sem_open (const char *name, int oflag, ...)
       sem.newsem.value = value << SEM_VALUE_SHIFT;
       sem.newsem.nwaiters = 0;
 #endif
+      /* pad is used as a mutex on pre-v9 sparc and ignored otherwise.  */
+      sem.newsem.pad = 0;
+
       /* This always is a shared semaphore.  */
       sem.newsem.private = FUTEX_SHARED;
 
@@ -231,7 +242,10 @@ sem_open (const char *name, int oflag, ...)
             mode cannot later be set since then we cannot apply the
             file create mask.  */
          if (__mktemp (tmpfname) == NULL)
-           return SEM_FAILED;
+           {
+             result = SEM_FAILED;
+             goto out;
+           }
 
          /* Open the file.  Make sure we do not overwrite anything.  */
          fd = __libc_open (tmpfname, O_RDWR | O_CREAT | O_EXCL, mode);
@@ -245,7 +259,8 @@ sem_open (const char *name, int oflag, ...)
                  __set_errno (EAGAIN);
                }
 
-             return SEM_FAILED;
+             result = SEM_FAILED;
+             goto out;
            }
 
          /* We got a file.  */
@@ -306,5 +321,10 @@ sem_open (const char *name, int oflag, ...)
       errno = save;
     }
 
+out:
+#ifdef __libc_ptf_call
+  __libc_ptf_call (__pthread_setcancelstate, (state, NULL), 0);
+#endif
+
   return result;
 }