From: Florian Weimer Date: Thu, 12 Feb 2026 11:18:54 +0000 (+0100) Subject: support: Add support for starting and stopping nscd X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bba1920c8b20176986ee294d74cef404027d99e8;p=thirdparty%2Fglibc.git support: Add support for starting and stopping nscd Reviewed-by: DJ Delorie --- diff --git a/support/Makefile b/support/Makefile index 5b59394274..3a20ecfc71 100644 --- a/support/Makefile +++ b/support/Makefile @@ -88,6 +88,7 @@ libsupport-routines = \ support_mem_access \ support_mutex_pi_monotonic \ support_need_proc \ + support_nscd \ support_open_and_compare_file_bytes \ support_open_and_compare_file_string \ support_openpty \ diff --git a/support/nscd_test.h b/support/nscd_test.h new file mode 100644 index 0000000000..60208098f4 --- /dev/null +++ b/support/nscd_test.h @@ -0,0 +1,39 @@ +/* Support functions for nscd testing. + Copyright (C) 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 + . */ + +/* The nscd support functionality assumes that a container test is + used. The container script must contain a su directive so that the + container runs with UID 0. */ + +#ifndef SUPPORT_NSCD_TEST_H +#define SUPPORT_NSCD_TEST_H + +/* Copy the default configuration file to /etc/nscd.conf. */ +void support_nscd_copy_configuration (void); + +/* Start nscd in foreground mode. Terminates the test on failure. */ +void support_nscd_start (void); + +/* Stop nscd. */ +void support_nscd_stop (void); + +/* Invalidate the specified database (group, hosts, netgroup, passwd, + services). */ +void support_nscd_invalidate (const char *database); + +#endif /* SUPPORT_NSCD_TEST_H */ diff --git a/support/support_nscd.c b/support/support_nscd.c new file mode 100644 index 0000000000..2d7d503ad6 --- /dev/null +++ b/support/support_nscd.c @@ -0,0 +1,115 @@ +/* Support functions for nscd testing. + Copyright (C) 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 +#include +#include +#include +#include +#include + +#include +#include + +static pid_t nscd_pid; + +void +support_nscd_copy_configuration (void) +{ + char *from = xasprintf ("%s/nscd/nscd.conf", support_srcdir_root); + support_copy_file (from, _PATH_NSCDCONF); + free (from); +} + +void +support_nscd_start (void) +{ + /* The --shutdown directive used in support_nscd_stop has a UID check. */ + if (getuid () != 0) + FAIL_UNSUPPORTED ("nscd needs to run as root" + " (use test-container su directive)"); + + /* Create required directories. Without /var/db/nscd, the shared + cache will not work. */ + { + char socket[] = _PATH_NSCDSOCKET; + xmkdirp (dirname (socket), 0755); + } + { + char var_db_passwd[] = _PATH_NSCD_PASSWD_DB; + xmkdirp (dirname (var_db_passwd), 0755); + } + + /* Create the directory for the persistent database files. Without + this, nscd cannot create the shared memory mappings. */ + xmkdirp ("/var/db/nscd", 0755); + + char *nscd_path = xasprintf ("%s/nscd/nscd", support_objdir_root); + char *args[] = { nscd_path, (char *) "--foreground", + (char *) "--debug", NULL }; + posix_spawn_file_actions_t file_actions; + posix_spawn_file_actions_init (&file_actions); + xposix_spawn_file_actions_adddup2 (&file_actions, + STDOUT_FILENO, STDERR_FILENO); + nscd_pid = xposix_spawn (nscd_path, &file_actions, NULL, args, NULL); + posix_spawn_file_actions_destroy (&file_actions); + free (nscd_path); + + /* Wait for the nscd socket to be come available. Without that, the + following tests will likely fallback to non-nscd NSS processing + instead. */ + while (true) + { + if (access (_PATH_NSCDSOCKET, F_OK) == 0) + break; + int status; + int ret = waitpid (nscd_pid, &status, WNOHANG); + if (ret < 0) + FAIL_EXIT1 ("waitpid on nscd failed: %m"); + else if (ret > 0) + FAIL_EXIT1 ("nscd exited with status %d", status); + usleep (10 * 1000); + } +} + +void +support_nscd_stop (void) +{ + char *cmd = xasprintf ("%s/nscd/nscd --shutdown", support_objdir_root); + TEST_COMPARE (system (cmd), 0); + free (cmd); + int status; + xwaitpid (nscd_pid, &status, 0); + TEST_COMPARE (status, 0); +} + +void +support_nscd_invalidate (const char *database) +{ + char *cmd = xasprintf ("%s/nscd/nscd --invalidate %s", + support_objdir_root, database); + TEST_COMPARE (system (cmd), 0); + free (cmd); +}