From patchwork Thu Jun 10 19:36:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 43824 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E8B25399C00C for ; Thu, 10 Jun 2021 19:37:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E8B25399C00C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1623353871; bh=6haGXzMGV0mzuWBtM1Qb/ej2e/1AgPI8weiC+LwPBME=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=DbReFWZNS3wapEiysFghfkRYUDOFnkviqr8vd+ZkJTAUY0zLoQy+sZOCVeIXB48ON dJVdxDRozGJJzitiscTrjheW0dYYKKrVNPVCWQL5w59mArZxUgD2SdqexAmBefJGp+ dT8evnBE2erjTCH4iUkmtmtxDEOBv4gBz7j7UbvA= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qv1-xf36.google.com (mail-qv1-xf36.google.com [IPv6:2607:f8b0:4864:20::f36]) by sourceware.org (Postfix) with ESMTPS id 635B8386FC1A for ; Thu, 10 Jun 2021 19:36:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 635B8386FC1A Received: by mail-qv1-xf36.google.com with SMTP id im10so14471717qvb.3 for ; Thu, 10 Jun 2021 12:36:46 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=6haGXzMGV0mzuWBtM1Qb/ej2e/1AgPI8weiC+LwPBME=; b=TUwotAiUh/ZGgMdRDxXR4FuX8fdeXoyap2baoBGMEOwTDEdn7NMimIamExAlKBzP+z GZIf1lnKcgSIbq0WV4UkSnTfdT6oXfE/9L4R0lMDMW/2mpqf83WKP+auh4876yCaVjiX Cfn8G0BAgyv2s6vzV/3khsO2irS1yH6Q5AYfwpAksXF8nR4322aGqA9ZMsJxyWox+Y06 HuqHB6tPvrBXYgBDWYETpI5ZfBFnT4/y8Y/WK5UKFvk1RgbNPtdHJ01pXaDlo5c+i9vE SjRB59hMbf9tMMunhTDBmMHW4wG3lbwvn4FezH8FfVizy5CDvertErpj4lGQnJvDsAz5 O/lA== X-Gm-Message-State: AOAM530kl7PvbvNR6AE3f9WK7Fo1kG+pPN2gkYoROZAdAJhQV+f7VRkg pV4i9D6OY/gr/vXHx8moOulIp2edCGM5pw== X-Google-Smtp-Source: ABdhPJy444LHF6fC42/f4xJFgEX+htgo9qCcz7OjSkREqIpclv1MwPV1tRQdxlJ3RrWHPFsSmQeRsQ== X-Received: by 2002:a05:6214:260f:: with SMTP id gu15mr1254798qvb.21.1623353805721; Thu, 10 Jun 2021 12:36:45 -0700 (PDT) Received: from birita.. ([177.194.59.218]) by smtp.googlemail.com with ESMTPSA id i21sm3012583qkl.20.2021.06.10.12.36.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Jun 2021 12:36:45 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 1/6] nptl: Fix tst-cancel7 and tst-cancelx7 race condition (BZ #14232) Date: Thu, 10 Jun 2021 16:36:34 -0300 Message-Id: <20210610193639.3650754-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210610193639.3650754-1-adhemerval.zanella@linaro.org> References: <20210610193639.3650754-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" A named semaphore is used to synchronize the pid information on the created file, the semaphore is updated once the file contents is flushed. Checked on x86_64-linux-gnu. --- nptl/Makefile | 2 + nptl/tst-cancel7.c | 96 +++++++++++++++++++++------------------------- 2 files changed, 45 insertions(+), 53 deletions(-) diff --git a/nptl/Makefile b/nptl/Makefile index 3e6cf0c21b..0ad9e24b0f 100644 --- a/nptl/Makefile +++ b/nptl/Makefile @@ -539,6 +539,8 @@ else librt = $(common-objpfx)rt/librt.a endif +$(objpfx)tst-cancel7: $(librt) +$(objpfx)tst-cancelx7: $(librt) $(objpfx)tst-cancel17: $(librt) $(objpfx)tst-cancelx17: $(librt) diff --git a/nptl/tst-cancel7.c b/nptl/tst-cancel7.c index 7a1870ac74..e69fa7e7af 100644 --- a/nptl/tst-cancel7.c +++ b/nptl/tst-cancel7.c @@ -18,44 +18,45 @@ #include #include -#include +#include #include -#include #include -#include -#include -#include +#include +#include +#include +#include +#include +#include #include -const char *command; -const char *pidfile; -char pidfilename[] = "/tmp/tst-cancel7-XXXXXX"; +static const char *command; +static const char *pidfile; +static char *pidfilename; + +static const char sem_name[] = "/tst-cancel7-sem"; +static sem_t *sem; static void * tf (void *arg) { - const char *args = " --direct --pidfile "; - char *cmd = alloca (strlen (command) + strlen (args) - + strlen (pidfilename) + 1); - - strcpy (stpcpy (stpcpy (cmd, command), args), pidfilename); + char *cmd = xasprintf ("%s --direct --pidfile %s", command, pidfilename); system (cmd); /* This call should never return. */ return NULL; } - static void sl (void) { - FILE *f = fopen (pidfile, "w"); - if (f == NULL) - exit (1); + FILE *f = xfopen (pidfile, "w"); fprintf (f, "%lld\n", (long long) getpid ()); fflush (f); + if (sem_post (sem) != 0) + FAIL_EXIT1 ("sem_post: %m"); + struct flock fl = { .l_type = F_WRLCK, @@ -64,7 +65,7 @@ sl (void) .l_len = 1 }; if (fcntl (fileno (f), F_SETLK, &fl) != 0) - exit (1); + FAIL_EXIT1 ("fcntl (F_SETFL): %m"); sigset_t ss; sigfillset (&ss); @@ -76,57 +77,51 @@ sl (void) static void do_prepare (int argc, char *argv[]) { + sem = sem_open (sem_name, O_RDWR | O_CREAT | O_EXCL | O_TRUNC, 0666, 0); + if (sem == SEM_FAILED) + { + if (errno != EEXIST) + FAIL_EXIT1 ("sem_open failed: %m"); + sem = sem_open (sem_name, O_RDWR); + if (sem == SEM_FAILED) + FAIL_EXIT1 ("sem_open failed: %m"); + } + if (command == NULL) command = argv[0]; if (pidfile) sl (); - int fd = mkstemp (pidfilename); + int fd = create_temp_file ("tst-cancel7-pid-", &pidfilename); if (fd == -1) - { - puts ("mkstemp failed"); - exit (1); - } + FAIL_EXIT1 ("create_temp_file failed: %m"); - write (fd, " ", 1); - close (fd); + xwrite (fd, " ", 1); + xclose (fd); } static int do_test (void) { - pthread_t th; - if (pthread_create (&th, NULL, tf, NULL) != 0) - { - puts ("pthread_create failed"); - return 1; - } + pthread_t th = xpthread_create (NULL, tf, NULL); do - sleep (1); + nanosleep (&(struct timespec) { .tv_sec = 0, .tv_nsec = 100000000 }, NULL); while (access (pidfilename, R_OK) != 0); xpthread_cancel (th); void *r = xpthread_join (th); - sleep (1); + if (sem_wait (sem) != 0) + FAIL_EXIT1 ("sem_wait: %m"); - FILE *f = fopen (pidfilename, "r+"); - if (f == NULL) - { - puts ("no pidfile"); - return 1; - } + FILE *f = xfopen (pidfilename, "r+"); long long ll; if (fscanf (f, "%lld\n", &ll) != 1) - { - puts ("could not read pid"); - unlink (pidfilename); - return 1; - } + FAIL_EXIT1 ("fscanf: %m"); struct flock fl = { @@ -136,11 +131,7 @@ do_test (void) .l_len = 1 }; if (fcntl (fileno (f), F_GETLK, &fl) != 0) - { - puts ("F_GETLK failed"); - unlink (pidfilename); - return 1; - } + FAIL_EXIT1 ("fcntl: %m"); if (fl.l_type != F_UNLCK) { @@ -148,13 +139,12 @@ do_test (void) if (fl.l_pid == ll) kill (fl.l_pid, SIGKILL); - unlink (pidfilename); return 1; } - fclose (f); + xfclose (f); - unlink (pidfilename); + sem_unlink (sem_name); return r != PTHREAD_CANCELED; } @@ -181,7 +171,7 @@ do_cleanup (void) fclose (f); } - unlink (pidfilename); + sem_unlink (sem_name); } #define OPT_COMMAND 10000