From patchwork Tue May 31 17:52:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 54588 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 B505C383A605 for ; Tue, 31 May 2022 17:59:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B505C383A605 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1654019987; bh=f9NuShTuo5AlXO0PFDYOFCMFf0p5M/cxWnPY9ECIfmY=; 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=l0dJ2BpXmncIujXBar2Ns//UMhiXgqihF8NdFeIid+buqfMt0BAEtZCs9vGHb4nEb 0WmNR9TJpSJogDK8GwZyj30WbKf619oaJCkP5XTHVgcV/ATBcnNt5Oarp4sgTMJB// uXZ+zpIwaA4a/kYN2Gifw8JY61FyCa42KqkAUTkw= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oi1-x234.google.com (mail-oi1-x234.google.com [IPv6:2607:f8b0:4864:20::234]) by sourceware.org (Postfix) with ESMTPS id 15B41395B407 for ; Tue, 31 May 2022 17:53:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 15B41395B407 Received: by mail-oi1-x234.google.com with SMTP id k187so14203495oif.1 for ; Tue, 31 May 2022 10:53: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=f9NuShTuo5AlXO0PFDYOFCMFf0p5M/cxWnPY9ECIfmY=; b=HLzkUbXPtnOH2BchnGMb+2iSLMB+dQxqkXLDZT9QWfwY4PJcycHDkkt9YqBq4v79Q7 ePtOhShWlV8fHBbY64ukQXQujSTWEyzdVAHbbbGkvTj0Sl+bc2Uo8JIU0P0nXm4+VG+f lE+ute9FOrWl5z4wUxpTVaAKuPGZrT1gTd1aWWx9NljD8imWut23vLGvhvh2PolxEnrP KsTEffzNmc0N4s5XMs/OMdBdiNy45nlQI66aaK2eqLyGjn9CTidhXN+u2pofZ41Xz4Fw SQd3AyP4rVNdQVc5WHbTilfoC6m/ZinXzB6kG462i9j9k+cZ1T44ISTU+SNtv0Lnu4Gq c/Og== X-Gm-Message-State: AOAM530TkQkWJ9+/NIgfvlDMGIEmCn1OnBH2Ai/4Stm3XyG56C05OcDD JyHwJ5UINkkYbBm/sqZFMnfOm+DpEHghbQ== X-Google-Smtp-Source: ABdhPJwvhLiwUwjAmpWRNEpjV3eZr3+PC6Uj2LSZTwnLScLFP0K9z7ORFqHIBQqbJjKpZaZJQxnkBQ== X-Received: by 2002:a05:6808:144d:b0:32b:7fbc:9440 with SMTP id x13-20020a056808144d00b0032b7fbc9440mr12590536oiv.226.1654019589566; Tue, 31 May 2022 10:53:09 -0700 (PDT) Received: from birita.. ([2804:431:c7ca:e39c:980f:eaf1:7a30:10ef]) by smtp.gmail.com with ESMTPSA id f30-20020a4a8921000000b0035f627ac898sm6402626ooi.10.2022.05.31.10.53.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 May 2022 10:53:09 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v3 09/12] nptl: Use exit_lock when accessing TID on pthread_setname_np Date: Tue, 31 May 2022 14:52:52 -0300 Message-Id: <20220531175255.1513396-10-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220531175255.1513396-1-adhemerval.zanella@linaro.org> References: <20220531175255.1513396-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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 Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Also return EINVAL if the thread is already terminated at the time of the call. Checked on x86_64-linux-gnu. --- nptl/pthread_setname.c | 52 +++++++++++++++++----------- sysdeps/pthread/tst-pthread-exited.c | 6 ++++ 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/nptl/pthread_setname.c b/nptl/pthread_setname.c index f24560db47..1dbbdcf63b 100644 --- a/nptl/pthread_setname.c +++ b/nptl/pthread_setname.c @@ -16,21 +16,40 @@ License along with the GNU C Library; see the file COPYING.LIB. If not, see . */ -#include -#include +#include +#include +#include #include +#include #include -#include -#include #include -#include +static int +setname (pid_t tid, const char *name, size_t name_len) +{ + char fname[sizeof ("/proc/self/task//comm") + INT_BUFSIZE_BOUND (pid_t)]; + __snprintf (fname, sizeof (fname), "/proc/self/task/%d/comm", tid); + int fd = __open64_nocancel (fname, O_RDONLY); + if (fd == -1) + return EINVAL; + + 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; + + __close_nocancel_nostatus (fd); + + return res; +} 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 +61,15 @@ __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; - - 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 res = pd->tid > 0 ? setname (pd->tid, name, name_len) : EINVAL; - __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 15d57a0440..7f5f339b3e 100644 --- a/sysdeps/pthread/tst-pthread-exited.c +++ b/sysdeps/pthread/tst-pthread-exited.c @@ -69,6 +69,12 @@ do_test (void) TEST_COMPARE (r, EINVAL); } + { + char thread_name[] = "test"; + int r = pthread_setname_np (thr, thread_name); + TEST_COMPARE (r, EINVAL); + } + xpthread_join (thr); return 0;