1 /* Setup a chroot environment for use within tests.
2 Copyright (C) 2017-2020 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
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>
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
31 write_file (const char *directory
, const char *relpath
, const char *contents
,
36 *abspath
= xasprintf ("%s/%s", directory
, relpath
);
37 add_temp_file (*abspath
);
38 support_write_file_string (*abspath
, contents
);
44 struct support_chroot
*
45 support_chroot_create (struct support_chroot_configuration conf
)
47 struct support_chroot
*chroot
= xmalloc (sizeof (*chroot
));
48 chroot
->path_chroot
= support_create_temp_directory ("tst-resolv-res_init-");
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
);
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
);
63 /* valgrind needs a temporary directory in the chroot. */
65 char *path_tmp
= xasprintf ("%s/tmp", chroot
->path_chroot
);
66 xmkdir (path_tmp
, 0777);
67 add_temp_file (path_tmp
);
75 support_chroot_free (struct support_chroot
*chroot
)
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
);