From patchwork Mon Aug 22 14:27:30 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 14841 Received: (qmail 72368 invoked by alias); 22 Aug 2016 14:27:52 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 72352 invoked by uid 89); 22 Aug 2016 14:27:51 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 spammy=sem, 153, 6, 15313, 153, 13 X-HELO: mail-ua0-f174.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=gW3eOztlYtP0IQ8+N8G5cllA67G87huGEzdVnKtuOxg=; b=Aru/QG+FUbxscHTcbls82KLHPwUj5MQZrhyk6ox0UhsSWYAPZM6hYLJBiPRBn1YR54 eJzcKFg3SAwxPt0hzjiZvsNJH6658od3W358YHI2bHJeBFNbgcXxoUSu1+FtGY0EoVkY 3hmnb4g0DPl4Erz5LE+q9Jbww0vcqa3P0Dkwr5KUwFwUysdDxoFMfXiP7u8r9ud2szB/ b9gS5uCSYziTsRbHe9Mxiy2wfuuRfq87hWmjAOJ6emTHBnQ/LWPjiJxxqKac3uENzHmY KaUUhSB/VqbWaQXPNEMtPutzGjZnwQvZM8iddWT3MP0ZvwVQLkeSZvqrAWCnIzx11hxE narg== X-Gm-Message-State: AEkooutYtCzqryTlcEKXM7T4feBm9tUlfMhglnBFYZm/zTocV1qjO1SQU4uT4DPkECv8lJOV X-Received: by 10.176.3.22 with SMTP id 22mr9740206uat.89.1471876060364; Mon, 22 Aug 2016 07:27:40 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 2/5] nptl: Set sem_open as a non cancellation point (BZ #15765) Date: Mon, 22 Aug 2016 11:27:30 -0300 Message-Id: <1471876053-780-2-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1471876053-780-1-git-send-email-adhemerval.zanella@linaro.org> References: <1471876053-780-1-git-send-email-adhemerval.zanella@linaro.org> This patch changes sem_open to not act as a cancellation point. Cancellation is disable at start and reenable in function exit. It fixes BZ #15765. Tested on x86_64 and i686. [BZ #15765] * nptl/Makefile (tests): Add tst-sem16. * nptl/tst-sem16.c: New file. * nptl/sem_open.c (sem_open): Disable asynchronous cancellation. --- nptl/Makefile | 2 +- nptl/sem_open.c | 25 ++++++++-- nptl/tst-sem16.c | 136 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 162 insertions(+), 5 deletions(-) create mode 100644 nptl/tst-sem16.c diff --git a/nptl/Makefile b/nptl/Makefile index 2ddcd2b..7c0e082 100644 --- a/nptl/Makefile +++ b/nptl/Makefile @@ -241,7 +241,7 @@ tests = tst-typesizes \ tst-key1 tst-key2 tst-key3 tst-key4 \ tst-sem1 tst-sem2 tst-sem3 tst-sem4 tst-sem5 tst-sem6 tst-sem7 \ tst-sem8 tst-sem9 tst-sem10 tst-sem11 tst-sem12 tst-sem13 tst-sem14 \ - tst-sem15 \ + tst-sem15 tst-sem16 \ tst-barrier1 tst-barrier2 tst-barrier3 tst-barrier4 tst-barrier5 \ tst-align tst-align3 \ tst-basic1 tst-basic2 tst-basic3 tst-basic4 tst-basic5 tst-basic6 \ diff --git a/nptl/sem_open.c b/nptl/sem_open.c index 974cff9..5a04df7 100644 --- a/nptl/sem_open.c +++ b/nptl/sem_open.c @@ -31,7 +31,7 @@ #include "semaphoreP.h" #include #include - +#include /* 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. */ @@ -232,7 +240,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); @@ -246,7 +257,8 @@ sem_open (const char *name, int oflag, ...) __set_errno (EAGAIN); } - return SEM_FAILED; + result = SEM_FAILED; + goto out; } /* We got a file. */ @@ -307,5 +319,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; } diff --git a/nptl/tst-sem16.c b/nptl/tst-sem16.c new file mode 100644 index 0000000..f99571c --- /dev/null +++ b/nptl/tst-sem16.c @@ -0,0 +1,136 @@ +/* Test for sem_open cancellation handling: BZ #15765. + Copyright (C) 2016 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include +#include +#include +#include + +static sem_t sem; /* Use to sync with thread start. */ +volatile int thread_ret; +static const char pipe_name[] = "/glibc-tst-sem16"; + +static void +remove_sem (int status, void *arg) +{ + sem_unlink (arg); +} + +static void * +tf (void *arg) +{ + thread_ret = 0; + + pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, 0); + + if (sem_wait (&sem) != 0) + { + printf ("error: sem_wait failed: %m"); + thread_ret = 1; + return NULL; + } + + if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, 0) != 0) + { + printf ("error: pthread_setcancelstate failed: %m"); + thread_ret = 1; + return NULL; + } + + /* Neither sem_unlink or sem_open should act on thread cancellation. */ + sem_unlink (pipe_name); + on_exit (remove_sem, (void *) pipe_name); + + sem_t *s = sem_open (pipe_name, O_CREAT, 0600, 1); + if (s == SEM_FAILED) + { + if (errno == ENOSYS || errno == EACCES) + thread_ret = 77; + else + thread_ret = 1; + return NULL; + } + + if (pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, 0) != 0) + { + printf ("error: pthread_setcancelstate failed: %m"); + thread_ret = 1; + return NULL; + } + + if (sem_close (s) != 0) + { + printf ("error: sem_close failed: %m"); + thread_ret = 1; + return NULL; + } + + return NULL; +} + +static int +do_test (void) +{ + pthread_t td; + + if (sem_init (&sem, 0, 0)) + { + printf ("error: sem_init failed: %m\n"); + return 1; + } + + if (pthread_create (&td, NULL, tf, NULL) != 0) + { + printf ("error: pthread_create failed: %m\n"); + return 1; + } + + if (pthread_cancel (td) != 0) + { + printf ("error: pthread_cancel failed: %m\n"); + return 1; + } + + if (sem_post (&sem) != 0) + { + printf ("error: sem_post failed: %m\n"); + return 1; + } + + void *r; + if (pthread_join (td, &r) != 0) + { + printf ("error: pthread_join failed: %m\n"); + return 1; + } + + if (r == PTHREAD_CANCELED) + { + puts ("error: pthread_join returned PTHREAD_CANCELED"); + return 1; + } + + return thread_ret; +} + +#define TEST_FUNCTION do_test () +#include