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 377B21F8C6 for ; Mon, 23 Aug 2021 20:04:08 +0000 (UTC) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 752F63858038 for ; Mon, 23 Aug 2021 20:04:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 752F63858038 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1629749047; bh=Ay6CBmClNSzypcTesi0d081NH+M4POk/lSVSdN5aHxc=; 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=iUxdOME9hlUMirDQMwkJ0QYhvL21SX1SZrJwEvI4KgKDZs5QNBTmRr6jVF+5fe/fS ZvW+fkO0c1sg/Kwj91kKXLrxhZyG4OiqP1QxQhU4W97QoZSQiJcWiQ9eSCD7e40JTJ LDR60315sRPmQEJNJjmK2uiK1QOhNyNbcJ8Q1ktg= Received: from mail-qk1-x731.google.com (mail-qk1-x731.google.com [IPv6:2607:f8b0:4864:20::731]) by sourceware.org (Postfix) with ESMTPS id 097BA3858010 for ; Mon, 23 Aug 2021 19:52:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 097BA3858010 Received: by mail-qk1-x731.google.com with SMTP id 22so20551495qkg.2 for ; Mon, 23 Aug 2021 12:52:20 -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=Ay6CBmClNSzypcTesi0d081NH+M4POk/lSVSdN5aHxc=; b=H28oelZ9O6SRbeF7HqyjcXiC5HZ8dw7MOY2zpxy01OJyVhioPnuxyQH84OCu4etIyP uAWMLZQftdOkB19nGroTVEGXV3b5YH57Wr9rPw2LB0iW167Inof+t42idMvswc08x8TK Kyt0Kl5OFXneLi9Kgscun7c2lkbzO6nZ0+nX3G5QRCEIYsQ99aLJiabyiZdVVKj71Pyb zmK/+cwa7gihJty/9eARLxfOIupoif/ulDLHybj0otJRWdkEEAWk4PJa177weySqm5mm Ag7NEC/DJE8c824D5EQXTX7GHqR/ibCiXuP+dwN7M37yaj/RAuWwv3EatQdKZ3brbwRz OoMw== X-Gm-Message-State: AOAM532wfMYC9m9x+9SdmWSB99a+nP4gCA01/dWXjF8bojZc0RoYuWnd k1d9MefTnZwLP/0z0xiNyNU9kxCeuultHg== X-Google-Smtp-Source: ABdhPJyxDt17NECzqX3CSlFVOCBcz2zkToxfuR58GAkwXBwyU6x2TYAY7Pf6jNWFJUFwYueBRcI+rA== X-Received: by 2002:ae9:e017:: with SMTP id m23mr23192708qkk.8.1629748339500; Mon, 23 Aug 2021 12:52:19 -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.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Aug 2021 12:52:19 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v2 16/19] nptl: Use tidlock when accessing TID on pthread_setname_np Date: Mon, 23 Aug 2021 16:50:44 -0300 Message-Id: <20210823195047.543237-17-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_setname.c | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/nptl/pthread_setname.c b/nptl/pthread_setname.c index 6d2d8a1723..0f541b4534 100644 --- a/nptl/pthread_setname.c +++ b/nptl/pthread_setname.c @@ -23,14 +23,14 @@ #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,23 +42,33 @@ __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 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_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) + 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; + } + else res = errno; - else if (n != name_len) - res = EIO; __close_nocancel_nostatus (fd); + lll_unlock (pd->tidlock, LLL_PRIVATE); + __libc_signal_restore_set (&oldmask); + return res; } versioned_symbol (libc, __pthread_setname_np, pthread_setname_np, -- 2.30.2