tst-gethostid.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* Basic test for gethostid.
  2. Copyright (C) 2018-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 <gnu/lib-names.h>
  16. #include <nss.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <support/namespace.h>
  21. #include <support/support.h>
  22. #include <support/temp_file.h>
  23. #include <support/xdlfcn.h>
  24. #include <support/xstdio.h>
  25. #include <support/xunistd.h>
  26. #include <unistd.h>
  27. /* Initial test is run outside a chroot, to increase the likelihood of
  28. success. */
  29. static void
  30. outside_chroot (void *closure)
  31. {
  32. long id = gethostid ();
  33. printf ("info: host ID outside chroot: 0x%lx\n", id);
  34. }
  35. /* The same, but this time perform a chroot operation. */
  36. static void
  37. in_chroot (void *closure)
  38. {
  39. const char *chroot_path = closure;
  40. xchroot (chroot_path);
  41. long id = gethostid ();
  42. printf ("info: host ID in chroot: 0x%lx\n", id);
  43. }
  44. static int
  45. do_test (void)
  46. {
  47. support_isolate_in_subprocess (outside_chroot, NULL);
  48. /* Now run the test inside a chroot. */
  49. support_become_root ();
  50. if (!support_can_chroot ())
  51. /* Cannot perform further tests. */
  52. return 0;
  53. /* Only use nss_files. */
  54. __nss_configure_lookup ("hosts", "files");
  55. /* Load the DSO outside of the chroot. */
  56. xdlopen (LIBNSS_FILES_SO, RTLD_LAZY);
  57. char *chroot_dir = support_create_temp_directory ("tst-gethostid-");
  58. support_isolate_in_subprocess (in_chroot, chroot_dir);
  59. /* Tests with /etc/hosts in the chroot. */
  60. {
  61. char *path = xasprintf ("%s/etc", chroot_dir);
  62. add_temp_file (path);
  63. xmkdir (path, 0777);
  64. free (path);
  65. path = xasprintf ("%s/etc/hosts", chroot_dir);
  66. add_temp_file (path);
  67. FILE *fp = xfopen (path, "w");
  68. xfclose (fp);
  69. printf ("info: chroot test with an empty /etc/hosts file\n");
  70. support_isolate_in_subprocess (in_chroot, chroot_dir);
  71. char hostname[1024];
  72. int ret = gethostname (hostname, sizeof (hostname));
  73. if (ret < 0)
  74. printf ("warning: invalid result from gethostname: %d\n", ret);
  75. else if (strlen (hostname) == 0)
  76. puts ("warning: gethostname returned empty string");
  77. else
  78. {
  79. printf ("info: chroot test with IPv6 address in /etc/hosts for: %s\n",
  80. hostname);
  81. fp = xfopen (path, "w");
  82. /* Use an IPv6 address to induce another lookup failure. */
  83. fprintf (fp, "2001:db8::1 %s\n", hostname);
  84. xfclose (fp);
  85. support_isolate_in_subprocess (in_chroot, chroot_dir);
  86. }
  87. free (path);
  88. }
  89. free (chroot_dir);
  90. return 0;
  91. }
  92. #include <support/test-driver.c>