From patchwork Fri Mar 20 20:42:28 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 132116 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id A2BC84BB3B9C for ; Fri, 20 Mar 2026 20:48:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A2BC84BB3B9C Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Vdqe0L9h 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 298DD4C515F6 for ; Fri, 20 Mar 2026 20:42:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 298DD4C515F6 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine 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 298DD4C515F6 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=1774039354; cv=none; b=LWNxMy4URm6gk9Y0eXNx6mhvIC2/mFeo+NdLxVS+88F89vC24q2rNIn/VU/XgxnwbjprCOWZApeGt6q9x3sY0S7urZHv1N8PnrLvoYMtJze1v7UDwPSCB7TDmCuUNYsPqDp6TTYY2nLg0edK3MrYYRH8fhIujnaLqgQQKqfSpDQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1774039354; c=relaxed/simple; bh=fBrrUgy2OLa16bmm+AjDdwRH0q7trfZTK9+8lc5rOxE=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=D+Wf6voI6m+Bg/6YL8y34q/xr7Eey4yJdMrvJFOxSqx64+pjG7a3bBD5ZLxiEE9FScl//xXqnXIrkQl59Ik5ReknyqYG2KX6FARz29HoAX5ytu4RXwAQvc7R89r3HxYE5KNHj/ok0gjV2Dz+PZ1uzMZRdV5VH5a8W84oy1esBMw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 298DD4C515F6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1774039353; 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=deJQLeyZ0fVQ3n14q3rifgiSAW1BaLCGUk+aLMSGrXk=; b=Vdqe0L9h5o3WnfexyZqAAL9fwiRMOdxVGct69MZprdBva7dJUA58YdcVzIrJz7fEy2EV1j ybeDLP2ZdJgOvqMjoMdL1EH0ubAz6vzap2Egp3BW+08G+P1NSakkPgMSKdIGcHGpA3UmdN 92GMZorNMZu8/Mcl8cyiBAJyqGAOx2c= Received: from mx-prod-mc-03.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-416-mBq_8I2qMUagYJKrVsPCog-1; Fri, 20 Mar 2026 16:42:32 -0400 X-MC-Unique: mBq_8I2qMUagYJKrVsPCog-1 X-Mimecast-MFC-AGG-ID: mBq_8I2qMUagYJKrVsPCog_1774039351 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id BC1FD19560AA for ; Fri, 20 Mar 2026 20:42:31 +0000 (UTC) Received: from fweimer-oldenburg.csb.redhat.com (unknown [10.45.224.63]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 12ABE30002DF for ; Fri, 20 Mar 2026 20:42:30 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v2 13/23] nscd: Introduce __nscd_read_from_socket client function In-Reply-To: Message-ID: <1b7650609687533628085fe716ac9666f5c400bd.1774037705.git.fweimer@redhat.com> References: X-From-Line: 1b7650609687533628085fe716ac9666f5c400bd Mon Sep 17 00:00:00 2001 Date: Fri, 20 Mar 2026 21:42:28 +0100 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: uNfZUfC7QTfO8DsvTYpW5a1Mgio8_EE-XeRegNgPn9M_1774039351 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_HOSTKARMA_W, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_PASS, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on 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~patchwork=sourceware.org@sourceware.org --- nscd/nscd-client.h | 20 ++++++++++++++++- nscd/nscd_helper.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) Reviewed-by: Carlos O'Donell diff --git a/nscd/nscd-client.h b/nscd/nscd-client.h index c507933acb..10284e8ef2 100644 --- a/nscd/nscd-client.h +++ b/nscd/nscd-client.h @@ -366,11 +366,29 @@ struct mapped_database __libc_rwlock_define (, lock); }; -/* Open socket connection to nscd server. */ +/* Open socket connection to nscd server, send the request, and read + the start of the response (update to RESPONSELEN bytes). If the + procided response buffer is exceeded, the socket is left open, so + that more data can be read. */ extern int __nscd_open_socket (const char *key, size_t keylen, request_type type, void *response, size_t responselen) attribute_hidden; +/* Open a socket to nscd, send the request, and read the full + response. *RESPONSE is grown as necessary. + + A non-negative return value indicates the number of response bytes + stored at RESPONSE->data. The response includes only the struct + *_response_header and the data that follows it, not struct + datahead. + + On error, return a negative errno code. */ +struct scratch_buffer; +ssize_t __nscd_read_from_socket (const char *key, size_t keylen, + request_type type, + struct scratch_buffer *response) + attribute_hidden; + /* Acquire reference to the mapping for DB (see ). On success, return a pointer to the mapping descriptor, and lock the mapping. diff --git a/nscd/nscd_helper.c b/nscd/nscd_helper.c index 9bd0e7818e..ed2d8d09da 100644 --- a/nscd/nscd_helper.c +++ b/nscd/nscd_helper.c @@ -37,6 +37,7 @@ #include #include #include +#include #include "nscd-client.h" #include "nscd-dbtype.h" @@ -612,3 +613,55 @@ __nscd_open_socket (const char *key, size_t keylen, request_type type, return -1; } + +ssize_t +__nscd_read_from_socket (const char *key, size_t keylen, request_type type, + struct scratch_buffer *response) +{ + if (keylen > MAXKEYLEN) + return -ENAMETOOLONG; + + int sock = open_socket (type, key, keylen); + if (sock < 0) + return sock; + + /* The nscd stream protocol does not size information in a way that + is independent of request type. However, there is only one + request per connection, so the code below simply reads all data + that is available before end of stream. */ + + ssize_t ret = 0; /* Number of bytes read, or -1 on error. */ + while (true) + { + /* Wait for data. Give up on timeout. */ + if (wait_on_socket (sock, 5 * 1000) == 0) + break; + size_t remaining = response->length - ret; + ssize_t nbytes = TEMP_FAILURE_RETRY (__read (sock, response->data + ret, + remaining)); + if (nbytes < 0) + { + ret = -errno; + break; + } + if (nbytes == 0) + /* The end of the stream marks the end of packet. We cannot + tell if the data was truncated because nscd went away + unexpectedly. This will be recognized by the caller during + packet parsing. */ + break; + + /* Try reading more data in the next iteration after growing the + buffer. */ + ret += nbytes; + if (ret == response->length && !scratch_buffer_grow_preserve (response)) + { + ret = -ENOMEM; + break; + } + } + + __close_nocancel_nostatus (sock); + + return ret; +}