support_chroot.c 2.6 KB

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