From patchwork Sun Jul 28 19:02:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1965746 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=CAlhc1YX; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WX9t142Zcz1ybX for ; Mon, 29 Jul 2024 05:05:13 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C002D385DDCA for ; Sun, 28 Jul 2024 19:05:11 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 2F97F3858283 for ; Sun, 28 Jul 2024 19:03:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2F97F3858283 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 2F97F3858283 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722193389; cv=none; b=RXF4bZdfRRzQqHa87AKyTgnO4RNpJtqwGw2mNdK7AUHz6lm9vvLi0nG1yRed68oCOVFBzldZUJdbyviZ1JcuMtjXmOePmTBJCyAmFJ8rL3pXe9Keu8/6gTmg0heNoAj61M6YsdG53gAKAiv4f21UEWCiqIGMAJWDKcdCSgyHPjU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722193389; c=relaxed/simple; bh=UN/4H/Bv9710ZHZiryGfUj+sTbMJ0Fe+HrqEXMD6CFc=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=jAb0mk6ukFy+S7Xz+y1OCiMmAUSTkhl6BEXJ5sGrAsd/3SorJp2H0P9oVKEAHuULGk+0t6NmtzRRvu3vLcbulcYNYzA16WitB1B5zrQ68iJgC9blfVWgpcPj1J9GM5gV4CggG973z1hQJEaGc9ueEY5zBrunR+7kOp7r3xFypiU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722193380; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=m84xgeIsfbHXrn/XoVCDWJbvmq1fwvFgNQ8NubReKrw=; b=CAlhc1YXOOfP6Gth04ZTq7wv0cPB+NaaocLP7Mgv2vtjJWVZ99PZV4R9hnxYu9CNw/FPwV 7l4wd9pDs0sJPk1BK5lyK+X/8+oBLqOrLakjRBxzfZOe3nxxjc71rI7rtcqInY56tREsJ0 awF8exMR/C3HLnwy+2ADN4DZ8ATHNo4= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-470-6nhLUpIdOO6VQH2sW-Z34w-1; Sun, 28 Jul 2024 15:02:59 -0400 X-MC-Unique: 6nhLUpIdOO6VQH2sW-Z34w-1 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 69C611955F43 for ; Sun, 28 Jul 2024 19:02:58 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.45.224.24]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 4C3AD1955F3B for ; Sun, 28 Jul 2024 19:02:57 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v2 07/13] stdlib: Add internal __getenvarray function In-Reply-To: Message-ID: References: X-From-Line: d1036d65c3f18dbe2e0a78cdec0bdee449e7c7a9 Mon Sep 17 00:00:00 2001 Date: Sun, 28 Jul 2024 21:02:54 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP 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.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org --- stdlib/Makefile | 1 + stdlib/getenvarray.c | 64 ++++++++++++++++++++++++++++++++++++++++++++ stdlib/setenv.h | 7 +++++ 3 files changed, 72 insertions(+) create mode 100644 stdlib/getenvarray.c diff --git a/stdlib/Makefile b/stdlib/Makefile index 697b547540..e1f1848439 100644 --- a/stdlib/Makefile +++ b/stdlib/Makefile @@ -79,6 +79,7 @@ routines := \ getcontext \ getentropy \ getenv \ + getenvarray \ getrandom \ getsubopt \ jrand48 \ diff --git a/stdlib/getenvarray.c b/stdlib/getenvarray.c new file mode 100644 index 0000000000..cac308fb32 --- /dev/null +++ b/stdlib/getenvarray.c @@ -0,0 +1,64 @@ +/* Obtain a consistent copy of the environ array. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include + +size_t +__getenvarray (char **result, size_t length) +{ + while (true) + { + /* The snapshotting approach follows getenv. Duplicates are + filtered out explicitly. */ + environ_counter start_counter = atomic_load_acquire (&__environ_counter); + char **start_environ = atomic_load_relaxed (&__environ); + if (start_environ == NULL) + return 0; + + const char *previous = NULL; + size_t count = 0; + for (char *const *ep = start_environ; ; ++ep) + { + char *entry = atomic_load_relaxed (ep); + if (entry == NULL) + break; + if (entry == previous) + /* Filter out a duplicate created by concurrent unsetenv. */ + continue; + if (count < length) + result[count] = entry; + ++count; + previous = entry; + } + + atomic_thread_fence_acquire (); + + if (atomic_load_acquire (&__environ_counter) != start_counter) + /* Retry if there has been more than one write to the array + from within unsetenv. Writes in setenv/putenv do not + matter because they do not change the key, so they cannot + introduce duplicates. */ + continue; + + if (count < length) + result[count] = NULL; + return count; + } +} diff --git a/stdlib/setenv.h b/stdlib/setenv.h index 3919943f87..69fa9367ed 100644 --- a/stdlib/setenv.h +++ b/stdlib/setenv.h @@ -47,6 +47,13 @@ __environ_is_from_array_list (char **ep) return eal != NULL && &eal->array[0] == ep; } +/* Make a copy of the environ array. Writes up to LENGTH pointers to + *RESULT, including a terminating null pointer if there is room. + *Returns the total number of non-null elements in the environ + *array. This number can be larger than LENGTH. */ +size_t __getenvarray (char **result, size_t length) + __attr_access ((__write_only__, 1, 2)) attribute_hidden; + /* Counter for detecting concurrent modification in unsetenv. Ideally, this should be a 64-bit counter that cannot wrap around, but given that counter wrapround is probably impossible to hit