(root)/
glibc-2.38/
support/
support_chroot.c
       1  /* Setup a chroot environment for use within tests.
       2     Copyright (C) 2017-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the License, or (at your option) any later version.
       9  
      10     The GNU C Library is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library; if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #include <stdlib.h>
      20  #include <support/check.h>
      21  #include <support/namespace.h>
      22  #include <support/support.h>
      23  #include <support/temp_file.h>
      24  #include <support/test-driver.h>
      25  #include <support/xunistd.h>
      26  
      27  /* If CONTENTS is not NULL, write it to the file at DIRECTORY/RELPATH,
      28     and store the name in *ABSPATH.  If CONTENTS is NULL, store NULL in
      29     *ABSPATH.  */
      30  static void
      31  write_file (const char *directory, const char *relpath, const char *contents,
      32              char **abspath)
      33  {
      34    if (contents != NULL)
      35      {
      36        *abspath = xasprintf ("%s/%s", directory, relpath);
      37        add_temp_file (*abspath);
      38        support_write_file_string (*abspath, contents);
      39      }
      40    else
      41      *abspath = NULL;
      42  }
      43  
      44  struct support_chroot *
      45  support_chroot_create (struct support_chroot_configuration conf)
      46  {
      47    struct support_chroot *chroot = xmalloc (sizeof (*chroot));
      48    chroot->path_chroot = support_create_temp_directory ("tst-resolv-res_init-");
      49  
      50    /* Create the /etc directory in the chroot environment.  */
      51    char *path_etc = xasprintf ("%s/etc", chroot->path_chroot);
      52    xmkdir (path_etc, 0777);
      53    add_temp_file (path_etc);
      54  
      55    write_file (path_etc, "resolv.conf", conf.resolv_conf,
      56                &chroot->path_resolv_conf);
      57    write_file (path_etc, "hosts", conf.hosts, &chroot->path_hosts);
      58    write_file (path_etc, "host.conf", conf.host_conf, &chroot->path_host_conf);
      59    write_file (path_etc, "aliases", conf.aliases, &chroot->path_aliases);
      60  
      61    free (path_etc);
      62  
      63    /* valgrind needs a temporary directory in the chroot.  */
      64    {
      65      char *path_tmp = xasprintf ("%s/tmp", chroot->path_chroot);
      66      xmkdir (path_tmp, 0777);
      67      add_temp_file (path_tmp);
      68      free (path_tmp);
      69    }
      70  
      71    return chroot;
      72  }
      73  
      74  void
      75  support_chroot_free (struct support_chroot *chroot)
      76  {
      77    free (chroot->path_chroot);
      78    free (chroot->path_resolv_conf);
      79    free (chroot->path_hosts);
      80    free (chroot->path_host_conf);
      81    free (chroot->path_aliases);
      82    free (chroot);
      83  }