From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS3215 2.6.0.0/16 X-Spam-Status: No, score=-4.5 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,SPF_HELO_PASS,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id B90341F8C6 for ; Mon, 23 Aug 2021 20:05:33 +0000 (UTC) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0461D3858010 for ; Mon, 23 Aug 2021 20:05:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0461D3858010 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1629749133; 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=PwOUtN/CTYU70lenLW7p7GDbFOPmpItVk+FhGVeQWsYj3tUCJrrAGlmQySZ+sIk3S BpY8dc1IMD3u0VNsxsN3GHOrglTM9mUIltsLZ5knQuy/Bz3lUuv8KiHRzgz0hlFzqX /MoYSYdx0AUHYICTxFntmIdisM1KbNRVwp3z/s54= 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 Content-Transfer-Encoding: 8bit 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: , From: Adhemerval Zanella via Libc-alpha Reply-To: Adhemerval Zanella Cc: Florian Weimer Errors-To: libc-alpha-bounces+e=80x24.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; } -- 2.30.2