| 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. |
| 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 | <http://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 | struct support_chroot * |
| 28 | support_chroot_create (struct support_chroot_configuration conf) |
| 29 | { |
| 30 | struct support_chroot *chroot = xmalloc (sizeof (*chroot)); |
| 31 | |
| 32 | chroot->path_chroot = xasprintf ("%s/tst-resolv-res_init-XXXXXX" , test_dir); |
| 33 | if (mkdtemp (chroot->path_chroot) == NULL) |
| 34 | FAIL_EXIT1 ("mkdtemp (\"%s\"): %m" , chroot->path_chroot); |
| 35 | add_temp_file (chroot->path_chroot); |
| 36 | |
| 37 | /* Create the /etc directory in the chroot environment. */ |
| 38 | char *path_etc = xasprintf ("%s/etc" , chroot->path_chroot); |
| 39 | xmkdir (path_etc, 0777); |
| 40 | add_temp_file (path_etc); |
| 41 | |
| 42 | if (conf.resolv_conf != NULL) |
| 43 | { |
| 44 | /* Create an empty resolv.conf file. */ |
| 45 | chroot->path_resolv_conf = xasprintf ("%s/resolv.conf" , path_etc); |
| 46 | add_temp_file (chroot->path_resolv_conf); |
| 47 | support_write_file_string (chroot->path_resolv_conf, conf.resolv_conf); |
| 48 | } |
| 49 | else |
| 50 | chroot->path_resolv_conf = NULL; |
| 51 | |
| 52 | free (path_etc); |
| 53 | |
| 54 | /* valgrind needs a temporary directory in the chroot. */ |
| 55 | { |
| 56 | char *path_tmp = xasprintf ("%s/tmp" , chroot->path_chroot); |
| 57 | xmkdir (path_tmp, 0777); |
| 58 | add_temp_file (path_tmp); |
| 59 | free (path_tmp); |
| 60 | } |
| 61 | |
| 62 | return chroot; |
| 63 | } |
| 64 | |
| 65 | void |
| 66 | support_chroot_free (struct support_chroot *chroot) |
| 67 | { |
| 68 | free (chroot->path_chroot); |
| 69 | free (chroot->path_resolv_conf); |
| 70 | free (chroot); |
| 71 | } |
| 72 | |