1 /* Setup a chroot environment for use within tests.
2 Copyright (C) 2017 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 <http://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
));
49 chroot
->path_chroot
= xasprintf ("%s/tst-resolv-res_init-XXXXXX", test_dir
);
50 if (mkdtemp (chroot
->path_chroot
) == NULL
)
51 FAIL_EXIT1 ("mkdtemp (\"%s\"): %m", chroot
->path_chroot
);
52 add_temp_file (chroot
->path_chroot
);
54 /* Create the /etc directory in the chroot environment. */
55 char *path_etc
= xasprintf ("%s/etc", chroot
->path_chroot
);
56 xmkdir (path_etc
, 0777);
57 add_temp_file (path_etc
);
59 write_file (path_etc
, "resolv.conf", conf
.resolv_conf
,
60 &chroot
->path_resolv_conf
);
61 write_file (path_etc
, "hosts", conf
.hosts
, &chroot
->path_hosts
);
62 write_file (path_etc
, "host.conf", conf
.host_conf
, &chroot
->path_host_conf
);
66 /* valgrind needs a temporary directory in the chroot. */
68 char *path_tmp
= xasprintf ("%s/tmp", chroot
->path_chroot
);
69 xmkdir (path_tmp
, 0777);
70 add_temp_file (path_tmp
);
78 support_chroot_free (struct support_chroot
*chroot
)
80 free (chroot
->path_chroot
);
81 free (chroot
->path_resolv_conf
);
82 free (chroot
->path_hosts
);
83 free (chroot
->path_host_conf
);