summaryrefslogtreecommitdiff
path: root/support/support_nscd.c
blob: 2d7d503ad63a4e95bef3221fee8a348fbac2c559 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
   <https://www.gnu.org/licenses/>.  */

#include <support/nscd_test.h>

#include <libgen.h>
#include <signal.h>
#include <spawn.h>
#include <stdbool.h>
#include <stdlib.h>
#include <support/check.h>
#include <support/support.h>
#include <support/xspawn.h>
#include <support/xunistd.h>
#include <sys/wait.h>
#include <unistd.h>

#include <nscd/nscd.h>
#include <nscd/nscd-client.h>

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);
}