From patchwork Tue May 12 09:12:52 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Coufal X-Patchwork-Id: 134835 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BB9AF4BA79A1 for ; Tue, 12 May 2026 09:19:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BB9AF4BA79A1 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=Z5Qv7h4E 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 2F54D4BB3BC7 for ; Tue, 12 May 2026 09:18:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2F54D4BB3BC7 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 2F54D4BB3BC7 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1778577500; cv=none; b=OXnaNM/TQkPLfWSXNvp3kPBEsjzEYp9XLIaSW6nYZsjntM/q8/n4NW/AV/HXE9XrVpkF64xlgKyYCq5iV4qU6+OEm9V+P/U5AplmpikrOZlebKKDoNv011RICN77F5W7XimF58P+RpmdkqPivSe02X/kH11Jvoi+J/4hJWiZSEg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1778577500; c=relaxed/simple; bh=A6UVwuBN7XEmTxtDRHHsGvg6oPig66QzhSjdInCa3zI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=lGuqFNA1J8G4kYmGqoYP9FooWq/6DSrA+GPG4RMYpcr6Jjt9Gkv27AgwMXsv4QA1W5H8iyfhBz49lZ3NOD39yXNARywHiAij1vPLxsLR58gYq7vk2p0UcCRG+vO2aHXFx0WFADOUM8iGHOyF5CAYK61kzzgNTQGov5M35mRUchM= ARC-Authentication-Results: i=1; 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=Z5Qv7h4E DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2F54D4BB3BC7 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1778577499; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=ox6PjdaPIn1gLoo07YFQ0hmE1lzgAl9DmIO3sEqbdH0=; b=Z5Qv7h4EGYUMXvfPH+M6Nepqn0xOiV9a/D52z8EEVcjhihwoGgluSd5beEr9MbzLPSH4Tf La4UsiP4nOBkidcpoYef5C2LpQnciWTeC0P9AmkkG2VusToKCaCC2K9eKq+p4iyxaCUDeD eOwsgKMMNsIf057CsyHLGwkCT1dNk4k= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-516-z5lU7DwtOkCCLoWJWThwQA-1; Tue, 12 May 2026 05:18:17 -0400 X-MC-Unique: z5lU7DwtOkCCLoWJWThwQA-1 X-Mimecast-MFC-AGG-ID: z5lU7DwtOkCCLoWJWThwQA_1778577497 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (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-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 2491618005B5 for ; Tue, 12 May 2026 09:18:17 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.44.32.158]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 002541800352; Tue, 12 May 2026 09:18:15 +0000 (UTC) From: Martin Coufal To: libc-alpha@sourceware.org Cc: Martin Coufal Subject: [PATCH v3] misc: add a new test for gethostname Date: Tue, 12 May 2026 11:12:52 +0200 Message-ID: <20260512091808.1394033-1-mcoufal@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: EehaRYZyP8K0Jp4UWMWhrTzqRyDQhzCxj4fqdAS6qG0_1778577497 X-Mimecast-Originator: redhat.com content-type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-13.2 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_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS, SPF_NONE, TXREP shortcircuit=no 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 Add a simple test that runs in a UTS namespace: checks gethostname after various sethostname values (including empty and HOST_NAME_MAX), verifies ENAMETOOLONG when the buffer is too small for a maximal name, EINVAL when sethostname is given a name longer than HOST_NAME_MAX. --- v3 changes: * call support_become_root and enter UTS namespace * use sethostname instead of systemd commands * clear errno * changes to comply with the GNU coding standard --- misc/Makefile | 1 + misc/tst-gethostname.c | 108 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 misc/tst-gethostname.c diff --git a/misc/Makefile b/misc/Makefile index b38e983ed7..3937b5d5ea 100644 --- a/misc/Makefile +++ b/misc/Makefile @@ -245,6 +245,7 @@ tests := \ tst-empty \ tst-error1 \ tst-fdset \ + tst-gethostname \ tst-getusershell \ tst-hsearch \ tst-insremque \ diff --git a/misc/tst-gethostname.c b/misc/tst-gethostname.c new file mode 100644 index 0000000000..bbb58d421a --- /dev/null +++ b/misc/tst-gethostname.c @@ -0,0 +1,108 @@ +/* Basic test for gethostname. + Copyright (C) 2024-2026 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 +#include +#include +#include +#include + +#ifndef HOST_NAME_MAX +#define HOST_NAME_MAX 64 +#endif + +#define MAX_LEN_INCREASE 10 + + +static int +do_test (void) +{ + char default_hostname[HOST_NAME_MAX + 1] = "\0"; + char hostname[HOST_NAME_MAX + 1] = "\0"; + char generated_hostname[HOST_NAME_MAX + MAX_LEN_INCREASE + 1] = "\0"; + + /* Become root and enter a UTS namespace. */ + support_become_root (); +#ifdef CLONE_NEWUTS + if (unshare (CLONE_NEWUTS) != 0) + FAIL_UNSUPPORTED ("unshare (CLONE_NEWUTS): %m"); +#else + FAIL_UNSUPPORTED ("CLONE_NEWUTS not supported"); +#endif + + /* Check default hostname is not an empty string. */ + TEST_VERIFY_EXIT (gethostname (default_hostname, sizeof (default_hostname)) + == 0); + TEST_VERIFY (strlen (default_hostname) > 0); + + /* input: '' + expected output: '' */ + TEST_VERIFY_EXIT (sethostname ("", 0) == 0); + TEST_VERIFY (gethostname (hostname, sizeof (hostname)) == 0); + TEST_VERIFY (strlen (hostname) == 0); + + /* input: 'a' + expected output: 'a' */ + TEST_VERIFY_EXIT (sethostname ("a", strlen ("a")) == 0); + TEST_VERIFY (gethostname (hostname, sizeof (hostname)) == 0); + TEST_COMPARE (strlen (hostname), 1); + TEST_VERIFY (strcmp (hostname, "a") == 0); + + /* input: abc.def.ghi + expected output: abc.def.ghi */ + TEST_VERIFY_EXIT (sethostname ("abc.def.ghi", strlen ("abc.def.ghi")) == 0); + TEST_VERIFY (gethostname (hostname, sizeof (hostname)) == 0); + TEST_COMPARE (strlen (hostname), strlen ("abc.def.ghi")); + TEST_VERIFY (strcmp (hostname, "abc.def.ghi") == 0); + + /* input: exactly HOST_NAME_MAX + expected output: exactly HOST_NAME_MAX, gethostname returns -1, errno set */ + for (int i = 0; i < HOST_NAME_MAX; i++) + { + /* Generate hostname of length == HOST_NAME_MAX. */ + generated_hostname[i] = 'a' + i % 26; + } + TEST_VERIFY_EXIT (sethostname + (generated_hostname, strlen (generated_hostname)) == 0); + errno = 0; + TEST_VERIFY (gethostname (hostname, HOST_NAME_MAX) == -1); + TEST_COMPARE (errno, ENAMETOOLONG); + TEST_COMPARE (strlen (hostname), HOST_NAME_MAX); + TEST_VERIFY (strcmp (hostname, generated_hostname) == 0); + + /* input: longer than HOST_NAME_MAX + expected output: sethostname fails, return -1, errno set */ + for (int i = 0; i < HOST_NAME_MAX + MAX_LEN_INCREASE; i++) + { + /* Generate hostname LONGER than HOST_NAME_MAX. */ + generated_hostname[i] = 'a' + i % 26; + } + errno = 0; + TEST_VERIFY (sethostname (generated_hostname, strlen (generated_hostname)) + == -1); + TEST_COMPARE (errno, EINVAL); + + /* Restore default hostname. */ + TEST_VERIFY_EXIT (sethostname (default_hostname, strlen (default_hostname)) + == 0); + return 0; +} + +#include