From patchwork Mon Aug 23 19:50:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 44764 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 131073858010 for ; Mon, 23 Aug 2021 20:05:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 131073858010 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1629749135; bh=KBnlNtQpye5hl0hiGBdkc+8wVbqgzY/k+igXVe3mOnU=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=lKA7AZr6Ah4Ch+9glS0fjvpsIcpTnc+Ey0N2nH9Kwde67ERH+h8BYXMkOS2uXuLCz f8dQ4Z0KJbCMvKuOQsy3jBv6G2vF0ZDyMNAToQK5KJwzpIilsyq/NUMzJTiwGUfIII vvl5z2HrmDWKhQZS1ZR5G5DVLlMNnnOCGTcjfrqE= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x733.google.com (mail-qk1-x733.google.com [IPv6:2607:f8b0:4864:20::733]) by sourceware.org (Postfix) with ESMTPS id 94CAA3858003 for ; Mon, 23 Aug 2021 19:52:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 94CAA3858003 Received: by mail-qk1-x733.google.com with SMTP id y144so20541383qkb.6 for ; Mon, 23 Aug 2021 12:52:18 -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:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=KBnlNtQpye5hl0hiGBdkc+8wVbqgzY/k+igXVe3mOnU=; b=i0Bt4DrPmbgDd33FZ9BSjzEN6lHJijoVWx8B4x17qM5n+u71vpyVkelMXfFYR8Xf8g pi6vyafY4c7PliuOo7yIr4bMUUhtdx08ZdSviSJ6Q97dZyzk77a85eqPNcla4I9/0xVx m3wCDvsK//Xy7xarA46SXDVSo/LToeekgO45LZZwJvo5SjNRwVYsdQ0zy+bNb8tziQac rPUwlvyWaxSkiSRAH2YGoUVbiEngRRzcBjZLz28/HRsK0yFpC2TK/0f/SsG5OSp1i4hf RNGJNd+JSpjZuoYYyKMhS7SBB4WaXcV2PqlRorsA2DcNMrHAh98Q/eP6fgOf5NbUEHMU YrvA== X-Gm-Message-State: AOAM530RbFeQIldaieQHxjmZ3hf+GOdJgq829Tq+H5fF0G2URC3HcbOr GHAwpniqX8pmF5CaqYiuAPidb3o+8xXLww== X-Google-Smtp-Source: ABdhPJyflBckvy+VuJoFgQ97fiTjyxSWgBAB2+FBKdyfH3meBe+rILkI4C0PhTtt01wYfUomD1fBEA== X-Received: by 2002:ae9:ef4c:: with SMTP id d73mr23239921qkg.494.1629748338130; Mon, 23 Aug 2021 12:52:18 -0700 (PDT) Received: from birita.. ([2804:431:c7ca:cd83:c38b:b50d:5d9a:43d4]) by smtp.gmail.com with ESMTPSA id s10sm9210935qko.134.2021.08.23.12.52.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Aug 2021 12:52:17 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v2 15/19] nptl: Use tidlock when accessing TID on pthread_getname_np Date: Mon, 23 Aug 2021 16:50:43 -0300 Message-Id: <20210823195047.543237-16-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210823195047.543237-1-adhemerval.zanella@linaro.org> References: <20210823195047.543237-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.8 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 Reply-To: Adhemerval Zanella Cc: Florian Weimer Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Checked on x86_64-linux-gnu. --- nptl/pthread_getname.c | 45 ++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/nptl/pthread_getname.c b/nptl/pthread_getname.c index 8ac814366a..37dd6a360e 100644 --- a/nptl/pthread_getname.c +++ b/nptl/pthread_getname.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -29,7 +30,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 +41,39 @@ __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 signal, since the lock is recursive and used on pthread_cancel + (which should be async-signal-safe). */ + sigset_t oldmask; + __libc_signal_block_all (&oldmask); + lll_lock (pd->tidlock, LLL_PRIVATE); - 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; - __close_nocancel_nostatus (fd); + lll_unlock (pd->tidlock, LLL_PRIVATE); + __libc_signal_restore_set (&oldmask); return res; }