From patchwork Thu Sep 30 20:00:48 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: 45633 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 834C73858001 for ; Thu, 30 Sep 2021 20:09:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 834C73858001 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1633032560; bh=AkJTYeEnxhQuiUp+hDM7PB/IFaq/rn9bB+LFBNfDIBA=; 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=hZ8LBcpRSW9aJgB/6WtyIizHozub1xuvrbtOQpsoeC2Pk8D4mZDSPfKD1GWlqNHJ/ iqnX0SdasiSsvaVRWW92Hj1t5R0P4Wq5s7O2thlnCeNYiQrcKr5sSkDi5YA/27Uq+P ZnXixXHHHm7nD7gEYZUwrD8FbFHpsHGBvBc6dLks= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qt1-x82a.google.com (mail-qt1-x82a.google.com [IPv6:2607:f8b0:4864:20::82a]) by sourceware.org (Postfix) with ESMTPS id 7E8A53857C51 for ; Thu, 30 Sep 2021 20:01:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7E8A53857C51 Received: by mail-qt1-x82a.google.com with SMTP id r1so6852990qta.12 for ; Thu, 30 Sep 2021 13:01:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=AkJTYeEnxhQuiUp+hDM7PB/IFaq/rn9bB+LFBNfDIBA=; b=DPgMIA0zDbCzp6bPI07oLQHN3hUhAsEu+eR547upGMdO6Ew/UkRz0yJ5y/rav6XtDD KYVFTEPS20G3nt+Vmbi9Y11v4HEI+s0X9Z+ZByEUSIIIpcIi5kYTfFTxRcTxktYaeG/Q H0LbxPLVi8Qm7mlcMoAMqgBh7fgIPqgnYh4Fz3908FBcmsQJTvjVn3AB1pKXWBbIlrXU XjpAsnF0B6oqBhqgAvvjAZURv0uh2gI9f1cgsegHFhZv+cm/cWiF6ZtuRptzX72257vT tMYbD3PCYLbH1KVCFPYoVeSfmBk8JNwY3m+MgZ0TBNH7kiNFWUNq4wSNM1y23GLHfm8a lo1Q== X-Gm-Message-State: AOAM530B0SFQlH2vpQxc9JsuTPmdry8C5TpfKbvgJ66iTkB/6SCYTiSp IFD8MQ/JosBsTA2f7U+9V5I0MwEzyj24bA== X-Google-Smtp-Source: ABdhPJwURctA1qNcFs+Fy7usb9F1zYkaG56wP0nCSPDm1hHmO/o69bKt01J02qz5apFi6u75tCrDQg== X-Received: by 2002:ac8:5287:: with SMTP id s7mr8833567qtn.359.1633032069930; Thu, 30 Sep 2021 13:01:09 -0700 (PDT) Received: from birita.. ([2804:431:c7cb:b338:b20e:53f8:8b5:1bb2]) by smtp.gmail.com with ESMTPSA id r196sm1910203qka.119.2021.09.30.13.01.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Sep 2021 13:01:09 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 12/15] nptl: Use exit_lock when accessing TID on pthread_setname_np Date: Thu, 30 Sep 2021 17:00:48 -0300 Message-Id: <20210930200051.1017457-13-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210930200051.1017457-1-adhemerval.zanella@linaro.org> References: <20210930200051.1017457-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.6 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.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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" Checked on x86_64-linux-gnu. --- nptl/pthread_setname.c | 38 ++++++++++++++++++---------- sysdeps/pthread/tst-pthread-exited.c | 6 +++++ 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/nptl/pthread_setname.c b/nptl/pthread_setname.c index 6d2d8a1723..8aa31616d2 100644 --- a/nptl/pthread_setname.c +++ b/nptl/pthread_setname.c @@ -18,19 +18,20 @@ #include #include +#include #include #include #include #include #include - +#include #include int __pthread_setname_np (pthread_t th, const char *name) { - const struct pthread *pd = (const struct pthread *) th; + struct pthread *pd = (struct pthread *) th; /* Unfortunately the kernel headers do not export the TASK_COMM_LEN macro. So we have to define it here. */ @@ -42,22 +43,31 @@ __pthread_setname_np (pthread_t th, const char *name) if (pd == THREAD_SELF) return __prctl (PR_SET_NAME, name) ? errno : 0; -#define FMT "/proc/self/task/%u/comm" - char fname[sizeof (FMT) + 8]; - sprintf (fname, FMT, (unsigned int) pd->tid); + /* Block all signals, as required by pd->exit_lock. */ + sigset_t old_mask; + __libc_signal_block_all (&old_mask); + __libc_lock_lock (pd->exit_lock); - int fd = __open64_nocancel (fname, O_RDWR); - if (fd == -1) - return errno; + char fname[sizeof ("/proc/self/task//comm" ) + INT_BUFSIZE_BOUND (pid_t)]; + __snprintf (fname, sizeof (fname), "/proc/self/task/%d/comm", pd->tid); int res = 0; - ssize_t n = TEMP_FAILURE_RETRY (__write_nocancel (fd, name, name_len)); - if (n < 0) - res = errno; - else if (n != name_len) - res = EIO; + int fd = __open64_nocancel (fname, O_RDWR); + if (fd != -1) + { + ssize_t n = TEMP_FAILURE_RETRY (__write_nocancel (fd, name, name_len)); + if (n < 0) + res = errno; + else if (n != name_len) + res = EIO; + + __close_nocancel_nostatus (fd); + } + else + res = errno == ENOENT ? ESRCH : errno; - __close_nocancel_nostatus (fd); + __libc_lock_unlock (pd->exit_lock); + __libc_signal_restore_set (&old_mask); return res; } diff --git a/sysdeps/pthread/tst-pthread-exited.c b/sysdeps/pthread/tst-pthread-exited.c index 6638afbf88..eaaf00a9b4 100644 --- a/sysdeps/pthread/tst-pthread-exited.c +++ b/sysdeps/pthread/tst-pthread-exited.c @@ -76,6 +76,12 @@ do_test (void) TEST_COMPARE (r, ESRCH); } + { + char thread_name[] = "test"; + int r = pthread_setname_np (thr, thread_name); + TEST_COMPARE (r, ESRCH); + } + xpthread_join (thr); return 0;