1/* Setup a chroot environment for use within tests.
2 Copyright (C) 2017-2022 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. */
30static void
31write_file (const char *directory, const char *relpath, const char *contents,
32 char **abspath)
33{
34 if (contents != NULL)
35 {
36 *abspath = xasprintf (format: "%s/%s", directory, relpath);
37 add_temp_file (name: *abspath);
38 support_write_file_string (path: *abspath, contents);
39 }
40 else
41 *abspath = NULL;
42}
43
44struct support_chroot *
45support_chroot_create (struct support_chroot_configuration conf)
46{
47 struct support_chroot *chroot = xmalloc (n: sizeof (*chroot));
48 chroot->path_chroot = support_create_temp_directory (base: "tst-resolv-res_init-");
49
50 /* Create the /etc directory in the chroot environment. */
51 char *path_etc = xasprintf (format: "%s/etc", chroot->path_chroot);
52 xmkdir (path: path_etc, 0777);
53 add_temp_file (name: path_etc);
54
55 write_file (directory: path_etc, relpath: "resolv.conf", contents: conf.resolv_conf,
56 abspath: &chroot->path_resolv_conf);
57 write_file (directory: path_etc, relpath: "hosts", contents: conf.hosts, abspath: &chroot->path_hosts);
58 write_file (directory: path_etc, relpath: "host.conf", contents: conf.host_conf, abspath: &chroot->path_host_conf);
59 write_file (directory: path_etc, relpath: "aliases", contents: conf.aliases, abspath: &chroot->path_aliases);
60
61 free (ptr: path_etc);
62
63 /* valgrind needs a temporary directory in the chroot. */
64 {
65 char *path_tmp = xasprintf (format: "%s/tmp", chroot->path_chroot);
66 xmkdir (path: path_tmp, 0777);
67 add_temp_file (name: path_tmp);
68 free (ptr: path_tmp);
69 }
70
71 return chroot;
72}
73
74void
75support_chroot_free (struct support_chroot *chroot)
76{
77 free (ptr: chroot->path_chroot);
78 free (ptr: chroot->path_resolv_conf);
79 free (ptr: chroot->path_hosts);
80 free (ptr: chroot->path_host_conf);
81 free (ptr: chroot->path_aliases);
82 free (ptr: chroot);
83}
84

source code of glibc/support/support_chroot.c