From patchwork Thu Sep 30 20:00:47 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: 45632 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 3D5563857C73 for ; Thu, 30 Sep 2021 20:08:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3D5563857C73 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1633032518; bh=8y4zVnf/76Txiw60vm+XKTuDxdEaiAVGnIw+E0qbM9A=; 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=E+rHK4RMWGNzPavqq/vmfqCjQi+6j9IQ0rE3pNjTF3wmZpYYbmsWzv2LCD7XqIYoY f+Ydr44OZCxUrCcE89Ltb5pIYflLT86eibjMLlA0NS0t0S13Lf0rLz0mIwtVa52Xw4 rFalHKKhKyN5t2e+BzC3+aRiWhZ7WAsS5lbaAGbA= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x736.google.com (mail-qk1-x736.google.com [IPv6:2607:f8b0:4864:20::736]) by sourceware.org (Postfix) with ESMTPS id 769D03857C56 for ; Thu, 30 Sep 2021 20:01:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 769D03857C56 Received: by mail-qk1-x736.google.com with SMTP id i132so7082940qke.1 for ; Thu, 30 Sep 2021 13:01:09 -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=8y4zVnf/76Txiw60vm+XKTuDxdEaiAVGnIw+E0qbM9A=; b=2nipWy/LOjQqYmn3jwfRvqIiAK2yRBwfVkrpGkCd7NXB6MYqcvCcV8qiHdQcbKBcF/ K2zNMDKUb5iTKh16KVrI48ZQX0sIgRwsjfXt007nv/L1NiRGKsxfrMrHxS4h1ZdB6PJk TOD8y9pzunltMIxO+sId+CsxMfXpc6+owuroU4jGK9jA7I2wZbiKZPyJP3ZHogQoRahj +OOUQxmLr3Oof06JPcudI7VuIcNvJAez7Vio5NRGUppbOrRiUYeEt0VrpLhC5WqTkAyM K6LGOFoGcT+zqCIZq9PuuJSptl2DbRZGH7QkGiCvoNB2XU3oPwWfa0g8QUh71oFr/HJ9 NJww== X-Gm-Message-State: AOAM5324xk28w0X1acLhqbMe8ENs+pY7dBi/NUKlyLz10H+bhoh8p9B2 HIwWjHob4ul/4KID4QSDgW3aOf0HHMQXBg== X-Google-Smtp-Source: ABdhPJzqqWb509ayLCWsgqvGWBfE8k7GYeMrvRResXLfM+vrgKGZfALp44G8tBa5Bd5WD139fNajLA== X-Received: by 2002:a37:bfc3:: with SMTP id p186mr6349048qkf.479.1633032068832; Thu, 30 Sep 2021 13:01:08 -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.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Sep 2021 13:01:08 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 11/15] nptl: Use exit_lock when accessing TID on pthread_getname_np Date: Thu, 30 Sep 2021 17:00:47 -0300 Message-Id: <20210930200051.1017457-12-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_getname.c | 45 +++++++++++++++++----------- sysdeps/pthread/tst-pthread-exited.c | 6 ++++ 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/nptl/pthread_getname.c b/nptl/pthread_getname.c index 8ac814366a..26011b55c9 100644 --- a/nptl/pthread_getname.c +++ b/nptl/pthread_getname.c @@ -18,10 +18,12 @@ #include #include +#include #include #include #include #include +#include #include #include #include @@ -29,7 +31,7 @@ int __pthread_getname_np (pthread_t th, char *buf, size_t len) { - 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. */ @@ -40,29 +42,38 @@ __pthread_getname_np (pthread_t th, char *buf, size_t len) if (pd == THREAD_SELF) return __prctl (PR_GET_NAME, buf) ? 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_RDONLY); - 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 (__read_nocancel (fd, buf, len)); - if (n < 0) - res = errno; - else + int fd = __open64_nocancel (fname, O_RDONLY); + if (fd != -1) { - if (buf[n - 1] == '\n') - buf[n - 1] = '\0'; - else if (n == len) - res = ERANGE; + ssize_t n = TEMP_FAILURE_RETRY (__read_nocancel (fd, buf, len)); + if (n > 0) + { + if (buf[n - 1] == '\n') + buf[n - 1] = '\0'; + else if (n == len) + res = ERANGE; + else + buf[n] = '\0'; + } else - buf[n] = '\0'; + res = errno; + + __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 25015d4f74..6638afbf88 100644 --- a/sysdeps/pthread/tst-pthread-exited.c +++ b/sysdeps/pthread/tst-pthread-exited.c @@ -70,6 +70,12 @@ do_test (void) TEST_COMPARE (r, ESRCH); } + { + char thread_name[32]; + int r = pthread_getname_np (thr, thread_name, 32); + TEST_COMPARE (r, ESRCH); + } + xpthread_join (thr); return 0;